]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_ctldir.c
MFC r209962, r211970-r211972, r212050, r212605, r212611
[FreeBSD/stable/8.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_ctldir.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 /*
27  * ZFS control directory (a.k.a. ".zfs")
28  *
29  * This directory provides a common location for all ZFS meta-objects.
30  * Currently, this is only the 'snapshot' directory, but this may expand in the
31  * future.  The elements are built using the GFS primitives, as the hierarchy
32  * does not actually exist on disk.
33  *
34  * For 'snapshot', we don't want to have all snapshots always mounted, because
35  * this would take up a huge amount of space in /etc/mnttab.  We have three
36  * types of objects:
37  *
38  *      ctldir ------> snapshotdir -------> snapshot
39  *                                             |
40  *                                             |
41  *                                             V
42  *                                         mounted fs
43  *
44  * The 'snapshot' node contains just enough information to lookup '..' and act
45  * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
46  * perform an automount of the underlying filesystem and return the
47  * corresponding vnode.
48  *
49  * All mounts are handled automatically by the kernel, but unmounts are
50  * (currently) handled from user land.  The main reason is that there is no
51  * reliable way to auto-unmount the filesystem when it's "no longer in use".
52  * When the user unmounts a filesystem, we call zfsctl_unmount(), which
53  * unmounts any snapshots within the snapshot directory.
54  *
55  * The '.zfs', '.zfs/snapshot', and all directories created under
56  * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
57  * share the same vfs_t as the head filesystem (what '.zfs' lives under).
58  *
59  * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
60  * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
61  * However, vnodes within these mounted on file systems have their v_vfsp
62  * fields set to the head filesystem to make NFS happy (see
63  * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
64  * so that it cannot be freed until all snapshots have been unmounted.
65  */
66
67 #include <sys/zfs_context.h>
68 #include <sys/zfs_ctldir.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/zfs_vfsops.h>
71 #include <sys/namei.h>
72 #include <sys/gfs.h>
73 #include <sys/stat.h>
74 #include <sys/dmu.h>
75 #include <sys/dsl_deleg.h>
76 #include <sys/mount.h>
77 #include <sys/sunddi.h>
78
79 #include "zfs_namecheck.h"
80
81 typedef struct zfsctl_node {
82         gfs_dir_t       zc_gfs_private;
83         uint64_t        zc_id;
84         timestruc_t     zc_cmtime;      /* ctime and mtime, always the same */
85 } zfsctl_node_t;
86
87 typedef struct zfsctl_snapdir {
88         zfsctl_node_t   sd_node;
89         kmutex_t        sd_lock;
90         avl_tree_t      sd_snaps;
91 } zfsctl_snapdir_t;
92
93 typedef struct {
94         char            *se_name;
95         vnode_t         *se_root;
96         avl_node_t      se_node;
97 } zfs_snapentry_t;
98
99 static int
100 snapentry_compare(const void *a, const void *b)
101 {
102         const zfs_snapentry_t *sa = a;
103         const zfs_snapentry_t *sb = b;
104         int ret = strcmp(sa->se_name, sb->se_name);
105
106         if (ret < 0)
107                 return (-1);
108         else if (ret > 0)
109                 return (1);
110         else
111                 return (0);
112 }
113
114 static struct vop_vector zfsctl_ops_root;
115 static struct vop_vector zfsctl_ops_snapdir;
116 static struct vop_vector zfsctl_ops_snapshot;
117 static struct vop_vector zfsctl_ops_shares;
118 static struct vop_vector zfsctl_ops_shares_dir;
119
120 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
121 static vnode_t *zfsctl_mknode_shares(vnode_t *);
122 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
123 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
124
125 /*
126  * Root directory elements.  We only have two entries
127  * snapshot and shares.
128  */
129 static gfs_dirent_t zfsctl_root_entries[] = {
130         { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
131         { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
132         { NULL }
133 };
134
135 /* include . and .. in the calculation */
136 #define NROOT_ENTRIES   ((sizeof (zfsctl_root_entries) / \
137     sizeof (gfs_dirent_t)) + 1)
138
139
140 /*
141  * Initialize the various GFS pieces we'll need to create and manipulate .zfs
142  * directories.  This is called from the ZFS init routine, and initializes the
143  * vnode ops vectors that we'll be using.
144  */
145 void
146 zfsctl_init(void)
147 {
148 }
149
150 void
151 zfsctl_fini(void)
152 {
153 }
154
155 /*
156  * Return the inode number associated with the 'snapshot' or
157  * 'shares' directory.
158  */
159 /* ARGSUSED */
160 static ino64_t
161 zfsctl_root_inode_cb(vnode_t *vp, int index)
162 {
163         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
164
165         ASSERT(index <= 2);
166
167         if (index == 0)
168                 return (ZFSCTL_INO_SNAPDIR);
169
170         return (zfsvfs->z_shares_dir);
171 }
172
173 /*
174  * Create the '.zfs' directory.  This directory is cached as part of the VFS
175  * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
176  * therefore checks against a vfs_count of 2 instead of 1.  This reference
177  * is removed when the ctldir is destroyed in the unmount.
178  */
179 void
180 zfsctl_create(zfsvfs_t *zfsvfs)
181 {
182         vnode_t *vp, *rvp;
183         zfsctl_node_t *zcp;
184
185         ASSERT(zfsvfs->z_ctldir == NULL);
186
187         vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
188             &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
189             zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
190         zcp = vp->v_data;
191         zcp->zc_id = ZFSCTL_INO_ROOT;
192
193         VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
194         ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
195         VN_URELE(rvp);
196
197         /*
198          * We're only faking the fact that we have a root of a filesystem for
199          * the sake of the GFS interfaces.  Undo the flag manipulation it did
200          * for us.
201          */
202         vp->v_vflag &= ~VV_ROOT;
203
204         zfsvfs->z_ctldir = vp;
205
206         VOP_UNLOCK(vp, 0);
207 }
208
209 /*
210  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
211  * There might still be more references if we were force unmounted, but only
212  * new zfs_inactive() calls can occur and they don't reference .zfs
213  */
214 void
215 zfsctl_destroy(zfsvfs_t *zfsvfs)
216 {
217         VN_RELE(zfsvfs->z_ctldir);
218         zfsvfs->z_ctldir = NULL;
219 }
220
221 /*
222  * Given a root znode, retrieve the associated .zfs directory.
223  * Add a hold to the vnode and return it.
224  */
225 vnode_t *
226 zfsctl_root(znode_t *zp)
227 {
228         ASSERT(zfs_has_ctldir(zp));
229         VN_HOLD(zp->z_zfsvfs->z_ctldir);
230         return (zp->z_zfsvfs->z_ctldir);
231 }
232
233 /*
234  * Common open routine.  Disallow any write access.
235  */
236 /* ARGSUSED */
237 static int
238 zfsctl_common_open(struct vop_open_args *ap)
239 {
240         int flags = ap->a_mode;
241
242         if (flags & FWRITE)
243                 return (EACCES);
244
245         return (0);
246 }
247
248 /*
249  * Common close routine.  Nothing to do here.
250  */
251 /* ARGSUSED */
252 static int
253 zfsctl_common_close(struct vop_close_args *ap)
254 {
255         return (0);
256 }
257
258 /*
259  * Common access routine.  Disallow writes.
260  */
261 /* ARGSUSED */
262 static int
263 zfsctl_common_access(ap)
264         struct vop_access_args /* {
265                 struct vnode *a_vp;
266                 int  a_accmode;
267                 struct ucred *a_cred;
268                 struct thread *a_td;
269         } */ *ap;
270 {
271         int mode = ap->a_accmode;
272
273 #ifdef TODO
274         if (flags & V_ACE_MASK) {
275                 if (accmode & ACE_ALL_WRITE_PERMS)
276                         return (EACCES);
277         } else {
278 #endif
279         if (mode & VWRITE)
280                 return (EACCES);
281 #ifdef TODO
282         }
283 #endif
284
285         return (0);
286 }
287
288 /*
289  * Common getattr function.  Fill in basic information.
290  */
291 static void
292 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
293 {
294         zfsctl_node_t   *zcp = vp->v_data;
295         timestruc_t     now;
296
297         vap->va_uid = 0;
298         vap->va_gid = 0;
299         vap->va_rdev = 0;
300         /*
301          * We are a purly virtual object, so we have no
302          * blocksize or allocated blocks.
303          */
304         vap->va_blksize = 0;
305         vap->va_nblocks = 0;
306         vap->va_seq = 0;
307         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
308         vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
309             S_IROTH | S_IXOTH;
310         vap->va_type = VDIR;
311         /*
312          * We live in the now (for atime).
313          */
314         gethrestime(&now);
315         vap->va_atime = now;
316         vap->va_mtime = vap->va_ctime = vap->va_birthtime = zcp->zc_cmtime;
317         /* FreeBSD: Reset chflags(2) flags. */
318         vap->va_flags = 0;
319 }
320
321 /*ARGSUSED*/
322 static int
323 zfsctl_common_fid(ap)
324         struct vop_fid_args /* {
325                 struct vnode *a_vp;
326                 struct fid *a_fid;
327         } */ *ap;
328 {
329         vnode_t         *vp = ap->a_vp;
330         fid_t           *fidp = (void *)ap->a_fid;
331         zfsvfs_t        *zfsvfs = vp->v_vfsp->vfs_data;
332         zfsctl_node_t   *zcp = vp->v_data;
333         uint64_t        object = zcp->zc_id;
334         zfid_short_t    *zfid;
335         int             i;
336
337         ZFS_ENTER(zfsvfs);
338
339         fidp->fid_len = SHORT_FID_LEN;
340
341         zfid = (zfid_short_t *)fidp;
342
343         zfid->zf_len = SHORT_FID_LEN;
344
345         for (i = 0; i < sizeof (zfid->zf_object); i++)
346                 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
347
348         /* .zfs znodes always have a generation number of 0 */
349         for (i = 0; i < sizeof (zfid->zf_gen); i++)
350                 zfid->zf_gen[i] = 0;
351
352         ZFS_EXIT(zfsvfs);
353         return (0);
354 }
355
356 /*ARGSUSED*/
357 static int
358 zfsctl_shares_fid(ap)
359         struct vop_fid_args /* {
360                 struct vnode *a_vp;
361                 struct fid *a_fid;
362         } */ *ap;
363 {
364         vnode_t         *vp = ap->a_vp;
365         fid_t           *fidp = (void *)ap->a_fid;
366         zfsvfs_t        *zfsvfs = vp->v_vfsp->vfs_data;
367         znode_t         *dzp;
368         int             error;
369
370         ZFS_ENTER(zfsvfs);
371
372         if (zfsvfs->z_shares_dir == 0) {
373                 ZFS_EXIT(zfsvfs);
374                 return (ENOTSUP);
375         }
376
377         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
378                 error = VOP_FID(ZTOV(dzp), fidp);
379                 VN_RELE(ZTOV(dzp));
380         }
381
382         ZFS_EXIT(zfsvfs);
383         return (error);
384 }
385
386 static int
387 zfsctl_common_reclaim(ap)
388         struct vop_reclaim_args /* {
389                 struct vnode *a_vp;
390                 struct thread *a_td;
391         } */ *ap;
392 {
393         vnode_t *vp = ap->a_vp;
394
395         /*
396          * Destroy the vm object and flush associated pages.
397          */
398         vnode_destroy_vobject(vp);
399         VI_LOCK(vp);
400         vp->v_data = NULL;
401         VI_UNLOCK(vp);
402         return (0);
403 }
404
405 /*
406  * .zfs inode namespace
407  *
408  * We need to generate unique inode numbers for all files and directories
409  * within the .zfs pseudo-filesystem.  We use the following scheme:
410  *
411  *      ENTRY                   ZFSCTL_INODE
412  *      .zfs                    1
413  *      .zfs/snapshot           2
414  *      .zfs/snapshot/<snap>    objectid(snap)
415  */
416
417 #define ZFSCTL_INO_SNAP(id)     (id)
418
419 /*
420  * Get root directory attributes.
421  */
422 /* ARGSUSED */
423 static int
424 zfsctl_root_getattr(ap)
425         struct vop_getattr_args /* {
426                 struct vnode *a_vp;
427                 struct vattr *a_vap;
428                 struct ucred *a_cred;
429                 struct thread *a_td;
430         } */ *ap;
431 {
432         struct vnode *vp = ap->a_vp;
433         struct vattr *vap = ap->a_vap;
434         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
435
436         ZFS_ENTER(zfsvfs);
437         vap->va_nodeid = ZFSCTL_INO_ROOT;
438         vap->va_nlink = vap->va_size = NROOT_ENTRIES;
439
440         zfsctl_common_getattr(vp, vap);
441         ZFS_EXIT(zfsvfs);
442
443         return (0);
444 }
445
446 #ifdef sun
447 static int
448 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
449     caller_context_t *ct)
450 {
451         /*
452          * We only care about ACL_ENABLED so that libsec can
453          * display ACL correctly and not default to POSIX draft.
454          */
455         if (cmd == _PC_ACL_ENABLED) {
456                 *valp = _ACL_ACE_ENABLED;
457                 return (0);
458         }
459
460         return (fs_pathconf(vp, cmd, valp, cr, ct));
461 }
462 #endif  /* sun */
463
464 #ifdef sun
465 static const fs_operation_def_t zfsctl_tops_root[] = {
466         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
467         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
468         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
469         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_root_getattr }  },
470         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
471         { VOPNAME_READDIR,      { .vop_readdir = gfs_vop_readdir }      },
472         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_root_lookup }    },
473         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
474         { VOPNAME_INACTIVE,     { .vop_inactive = gfs_vop_inactive }    },
475         { VOPNAME_PATHCONF,     { .vop_pathconf = zfsctl_pathconf }     },
476         { VOPNAME_FID,          { .vop_fid = zfsctl_common_fid  }       },
477         { NULL }
478 };
479 #endif  /* sun */
480
481 /*
482  * Special case the handling of "..".
483  */
484 /* ARGSUSED */
485 int
486 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
487     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
488     int *direntflags, pathname_t *realpnp)
489 {
490         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
491         int err;
492
493         /*
494          * No extended attributes allowed under .zfs
495          */
496         if (flags & LOOKUP_XATTR)
497                 return (EINVAL);
498
499         ZFS_ENTER(zfsvfs);
500
501         if (strcmp(nm, "..") == 0) {
502                 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
503                 if (err == 0)
504                         VOP_UNLOCK(*vpp, 0);
505         } else {
506                 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
507                     cr, ct, direntflags, realpnp);
508         }
509
510         ZFS_EXIT(zfsvfs);
511
512         return (err);
513 }
514
515 /*
516  * Special case the handling of "..".
517  */
518 /* ARGSUSED */
519 int
520 zfsctl_freebsd_root_lookup(ap)
521         struct vop_lookup_args /* {
522                 struct vnode *a_dvp;
523                 struct vnode **a_vpp;
524                 struct componentname *a_cnp;
525         } */ *ap;
526 {
527         vnode_t *dvp = ap->a_dvp;
528         vnode_t **vpp = ap->a_vpp;
529         cred_t *cr = ap->a_cnp->cn_cred;
530         int flags = ap->a_cnp->cn_flags;
531         int nameiop = ap->a_cnp->cn_nameiop;
532         char nm[NAME_MAX + 1];
533         int err;
534
535         if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
536                 return (EOPNOTSUPP);
537
538         ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
539         strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
540
541         err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
542         if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
543                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
544
545         return (err);
546 }
547
548 static struct vop_vector zfsctl_ops_root = {
549         .vop_default =  &default_vnodeops,
550         .vop_open =     zfsctl_common_open,
551         .vop_close =    zfsctl_common_close,
552         .vop_ioctl =    VOP_EINVAL,
553         .vop_getattr =  zfsctl_root_getattr,
554         .vop_access =   zfsctl_common_access,
555         .vop_readdir =  gfs_vop_readdir,
556         .vop_lookup =   zfsctl_freebsd_root_lookup,
557         .vop_inactive = gfs_vop_inactive,
558         .vop_reclaim =  zfsctl_common_reclaim,
559         .vop_fid =      zfsctl_common_fid,
560 };
561
562 static int
563 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
564 {
565         objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
566
567         if (snapshot_namecheck(name, NULL, NULL) != 0)
568                 return (EILSEQ);
569         dmu_objset_name(os, zname);
570         if (strlen(zname) + 1 + strlen(name) >= len)
571                 return (ENAMETOOLONG);
572         (void) strcat(zname, "@");
573         (void) strcat(zname, name);
574         return (0);
575 }
576
577 static int
578 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
579 {
580         vnode_t *svp = sep->se_root;
581         int error;
582
583         ASSERT(vn_ismntpt(svp));
584
585         /* this will be dropped by dounmount() */
586         if ((error = vn_vfswlock(svp)) != 0)
587                 return (error);
588
589         return (dounmount(vn_mountedvfs(svp), fflags, curthread));
590 }
591
592 #if 0
593 static void
594 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
595 {
596         avl_index_t where;
597         vfs_t *vfsp;
598         refstr_t *pathref;
599         char newpath[MAXNAMELEN];
600         char *tail;
601
602         ASSERT(MUTEX_HELD(&sdp->sd_lock));
603         ASSERT(sep != NULL);
604
605         vfsp = vn_mountedvfs(sep->se_root);
606         ASSERT(vfsp != NULL);
607
608         vfs_lock_wait(vfsp);
609
610         /*
611          * Change the name in the AVL tree.
612          */
613         avl_remove(&sdp->sd_snaps, sep);
614         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
615         sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
616         (void) strcpy(sep->se_name, nm);
617         VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
618         avl_insert(&sdp->sd_snaps, sep, where);
619
620         /*
621          * Change the current mountpoint info:
622          *      - update the tail of the mntpoint path
623          *      - update the tail of the resource path
624          */
625         pathref = vfs_getmntpoint(vfsp);
626         (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
627         VERIFY((tail = strrchr(newpath, '/')) != NULL);
628         *(tail+1) = '\0';
629         ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
630         (void) strcat(newpath, nm);
631         refstr_rele(pathref);
632         vfs_setmntpoint(vfsp, newpath);
633
634         pathref = vfs_getresource(vfsp);
635         (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
636         VERIFY((tail = strrchr(newpath, '@')) != NULL);
637         *(tail+1) = '\0';
638         ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
639         (void) strcat(newpath, nm);
640         refstr_rele(pathref);
641         vfs_setresource(vfsp, newpath);
642
643         vfs_unlock(vfsp);
644 }
645 #endif
646
647 #if 0
648 /*ARGSUSED*/
649 static int
650 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
651     cred_t *cr, caller_context_t *ct, int flags)
652 {
653         zfsctl_snapdir_t *sdp = sdvp->v_data;
654         zfs_snapentry_t search, *sep;
655         zfsvfs_t *zfsvfs;
656         avl_index_t where;
657         char from[MAXNAMELEN], to[MAXNAMELEN];
658         char real[MAXNAMELEN];
659         int err;
660
661         zfsvfs = sdvp->v_vfsp->vfs_data;
662         ZFS_ENTER(zfsvfs);
663
664         if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
665                 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
666                     MAXNAMELEN, NULL);
667                 if (err == 0) {
668                         snm = real;
669                 } else if (err != ENOTSUP) {
670                         ZFS_EXIT(zfsvfs);
671                         return (err);
672                 }
673         }
674
675         ZFS_EXIT(zfsvfs);
676
677         err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
678         if (!err)
679                 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
680         if (!err)
681                 err = zfs_secpolicy_rename_perms(from, to, cr);
682         if (err)
683                 return (err);
684
685         /*
686          * Cannot move snapshots out of the snapdir.
687          */
688         if (sdvp != tdvp)
689                 return (EINVAL);
690
691         if (strcmp(snm, tnm) == 0)
692                 return (0);
693
694         mutex_enter(&sdp->sd_lock);
695
696         search.se_name = (char *)snm;
697         if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
698                 mutex_exit(&sdp->sd_lock);
699                 return (ENOENT);
700         }
701
702         err = dmu_objset_rename(from, to, B_FALSE);
703         if (err == 0)
704                 zfsctl_rename_snap(sdp, sep, tnm);
705
706         mutex_exit(&sdp->sd_lock);
707
708         return (err);
709 }
710 #endif
711
712 #if 0
713 /* ARGSUSED */
714 static int
715 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
716     caller_context_t *ct, int flags)
717 {
718         zfsctl_snapdir_t *sdp = dvp->v_data;
719         zfs_snapentry_t *sep;
720         zfs_snapentry_t search;
721         zfsvfs_t *zfsvfs;
722         char snapname[MAXNAMELEN];
723         char real[MAXNAMELEN];
724         int err;
725
726         zfsvfs = dvp->v_vfsp->vfs_data;
727         ZFS_ENTER(zfsvfs);
728
729         if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
730
731                 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
732                     MAXNAMELEN, NULL);
733                 if (err == 0) {
734                         name = real;
735                 } else if (err != ENOTSUP) {
736                         ZFS_EXIT(zfsvfs);
737                         return (err);
738                 }
739         }
740
741         ZFS_EXIT(zfsvfs);
742
743         err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
744         if (!err)
745                 err = zfs_secpolicy_destroy_perms(snapname, cr);
746         if (err)
747                 return (err);
748
749         mutex_enter(&sdp->sd_lock);
750
751         search.se_name = name;
752         sep = avl_find(&sdp->sd_snaps, &search, NULL);
753         if (sep) {
754                 avl_remove(&sdp->sd_snaps, sep);
755                 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
756                 if (err) {
757                         avl_index_t where;
758
759                         if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
760                                 avl_insert(&sdp->sd_snaps, sep, where);
761                 } else
762                         err = dmu_objset_destroy(snapname);
763         } else {
764                 err = ENOENT;
765         }
766
767         mutex_exit(&sdp->sd_lock);
768
769         return (err);
770 }
771 #endif
772
773 /*
774  * This creates a snapshot under '.zfs/snapshot'.
775  */
776 /* ARGSUSED */
777 static int
778 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
779     cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
780 {
781         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
782         char name[MAXNAMELEN];
783         int err;
784         static enum symfollow follow = NO_FOLLOW;
785         static enum uio_seg seg = UIO_SYSSPACE;
786
787         if (snapshot_namecheck(dirname, NULL, NULL) != 0)
788                 return (EILSEQ);
789
790         dmu_objset_name(zfsvfs->z_os, name);
791
792         *vpp = NULL;
793
794         err = zfs_secpolicy_snapshot_perms(name, cr);
795         if (err)
796                 return (err);
797
798         if (err == 0) {
799                 err = dmu_objset_snapshot(name, dirname, NULL, B_FALSE);
800                 if (err)
801                         return (err);
802                 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
803         }
804
805         return (err);
806 }
807
808 static int
809 zfsctl_freebsd_snapdir_mkdir(ap)
810         struct vop_mkdir_args /* {
811                 struct vnode *a_dvp;
812                 struct vnode **a_vpp;
813                 struct componentname *a_cnp;
814                 struct vattr *a_vap;
815         } */ *ap;
816 {
817
818         ASSERT(ap->a_cnp->cn_flags & SAVENAME);
819
820         return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
821             ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
822 }
823
824 /*
825  * Lookup entry point for the 'snapshot' directory.  Try to open the
826  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
827  * Perform a mount of the associated dataset on top of the vnode.
828  */
829 /* ARGSUSED */
830 int
831 zfsctl_snapdir_lookup(ap)
832         struct vop_lookup_args /* {
833                 struct vnode *a_dvp;
834                 struct vnode **a_vpp;
835                 struct componentname *a_cnp;
836         } */ *ap;
837 {
838         vnode_t *dvp = ap->a_dvp;
839         vnode_t **vpp = ap->a_vpp;
840         struct componentname *cnp = ap->a_cnp;
841         char nm[NAME_MAX + 1];
842         zfsctl_snapdir_t *sdp = dvp->v_data;
843         objset_t *snap;
844         char snapname[MAXNAMELEN];
845         char real[MAXNAMELEN];
846         char *mountpoint;
847         zfs_snapentry_t *sep, search;
848         size_t mountpoint_len;
849         avl_index_t where;
850         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
851         int err;
852         int flags = 0;
853
854         /*
855          * No extended attributes allowed under .zfs
856          */
857         if (flags & LOOKUP_XATTR)
858                 return (EINVAL);
859         ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
860         strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
861
862         ASSERT(dvp->v_type == VDIR);
863
864         *vpp = NULL;
865
866         /*
867          * If we get a recursive call, that means we got called
868          * from the domount() code while it was trying to look up the
869          * spec (which looks like a local path for zfs).  We need to
870          * add some flag to domount() to tell it not to do this lookup.
871          */
872         if (MUTEX_HELD(&sdp->sd_lock))
873                 return (ENOENT);
874
875         ZFS_ENTER(zfsvfs);
876
877         if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
878                 ZFS_EXIT(zfsvfs);
879                 return (0);
880         }
881
882         if (flags & FIGNORECASE) {
883                 boolean_t conflict = B_FALSE;
884
885                 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
886                     MAXNAMELEN, &conflict);
887                 if (err == 0) {
888                         strlcpy(nm, real, sizeof(nm));
889                 } else if (err != ENOTSUP) {
890                         ZFS_EXIT(zfsvfs);
891                         return (err);
892                 }
893 #if 0
894                 if (realpnp)
895                         (void) strlcpy(realpnp->pn_buf, nm,
896                             realpnp->pn_bufsize);
897                 if (conflict && direntflags)
898                         *direntflags = ED_CASE_CONFLICT;
899 #endif
900         }
901
902         mutex_enter(&sdp->sd_lock);
903         search.se_name = (char *)nm;
904         if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
905                 *vpp = sep->se_root;
906                 VN_HOLD(*vpp);
907                 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
908                 if (err) {
909                         VN_RELE(*vpp);
910                         *vpp = NULL;
911                 } else if (*vpp == sep->se_root) {
912                         /*
913                          * The snapshot was unmounted behind our backs,
914                          * try to remount it.
915                          */
916                         goto domount;
917                 } else {
918                         /*
919                          * VROOT was set during the traverse call.  We need
920                          * to clear it since we're pretending to be part
921                          * of our parent's vfs.
922                          */
923                         (*vpp)->v_flag &= ~VROOT;
924                 }
925                 mutex_exit(&sdp->sd_lock);
926                 ZFS_EXIT(zfsvfs);
927                 return (err);
928         }
929
930         /*
931          * The requested snapshot is not currently mounted, look it up.
932          */
933         err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
934         if (err) {
935                 mutex_exit(&sdp->sd_lock);
936                 ZFS_EXIT(zfsvfs);
937                 /*
938                  * handle "ls *" or "?" in a graceful manner,
939                  * forcing EILSEQ to ENOENT.
940                  * Since shell ultimately passes "*" or "?" as name to lookup
941                  */
942                 return (err == EILSEQ ? ENOENT : err);
943         }
944         if (dmu_objset_open(snapname, DMU_OST_ZFS,
945             DS_MODE_USER | DS_MODE_READONLY, &snap) != 0) {
946                 mutex_exit(&sdp->sd_lock);
947                 /* Translate errors and add SAVENAME when needed. */
948                 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
949                         err = EJUSTRETURN;
950                         cnp->cn_flags |= SAVENAME;
951                 } else {
952                         err = ENOENT;
953                 }
954                 ZFS_EXIT(zfsvfs);
955                 return (err);
956         }
957
958         sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
959         sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
960         (void) strcpy(sep->se_name, nm);
961         *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
962         VN_HOLD(*vpp);
963         avl_insert(&sdp->sd_snaps, sep, where);
964
965         dmu_objset_close(snap);
966 domount:
967         mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
968             strlen("/.zfs/snapshot/") + strlen(nm) + 1;
969         mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
970         (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
971             dvp->v_vfsp->mnt_stat.f_mntonname, nm);
972         err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
973         kmem_free(mountpoint, mountpoint_len);
974         if (err == 0) {
975                 /*
976                  * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
977                  *
978                  * This is where we lie about our v_vfsp in order to
979                  * make .zfs/snapshot/<snapname> accessible over NFS
980                  * without requiring manual mounts of <snapname>.
981                  */
982                 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
983                 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
984         }
985         mutex_exit(&sdp->sd_lock);
986         ZFS_EXIT(zfsvfs);
987         if (err != 0)
988                 *vpp = NULL;
989         return (err);
990 }
991
992 /* ARGSUSED */
993 int
994 zfsctl_shares_lookup(ap)
995         struct vop_lookup_args /* {
996                 struct vnode *a_dvp;
997                 struct vnode **a_vpp;
998                 struct componentname *a_cnp;
999         } */ *ap;
1000 {
1001         vnode_t *dvp = ap->a_dvp;
1002         vnode_t **vpp = ap->a_vpp;
1003         struct componentname *cnp = ap->a_cnp;
1004         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1005         char nm[NAME_MAX + 1];
1006         znode_t *dzp;
1007         int error;
1008
1009         ZFS_ENTER(zfsvfs);
1010
1011         ASSERT(cnp->cn_namelen < sizeof(nm));
1012         strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1013
1014         if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1015                 ZFS_EXIT(zfsvfs);
1016                 return (0);
1017         }
1018
1019         if (zfsvfs->z_shares_dir == 0) {
1020                 ZFS_EXIT(zfsvfs);
1021                 return (ENOTSUP);
1022         }
1023         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
1024                 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1025
1026         VN_RELE(ZTOV(dzp));
1027         ZFS_EXIT(zfsvfs);
1028
1029         return (error);
1030 }
1031
1032 /* ARGSUSED */
1033 static int
1034 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1035     offset_t *offp, offset_t *nextp, void *data, int flags)
1036 {
1037         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1038         char snapname[MAXNAMELEN];
1039         uint64_t id, cookie;
1040         boolean_t case_conflict;
1041         int error;
1042
1043         ZFS_ENTER(zfsvfs);
1044
1045         cookie = *offp;
1046         error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1047             &cookie, &case_conflict);
1048         if (error) {
1049                 ZFS_EXIT(zfsvfs);
1050                 if (error == ENOENT) {
1051                         *eofp = 1;
1052                         return (0);
1053                 }
1054                 return (error);
1055         }
1056
1057         if (flags & V_RDDIR_ENTFLAGS) {
1058                 edirent_t *eodp = dp;
1059
1060                 (void) strcpy(eodp->ed_name, snapname);
1061                 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1062                 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1063         } else {
1064                 struct dirent64 *odp = dp;
1065
1066                 (void) strcpy(odp->d_name, snapname);
1067                 odp->d_ino = ZFSCTL_INO_SNAP(id);
1068         }
1069         *nextp = cookie;
1070
1071         ZFS_EXIT(zfsvfs);
1072
1073         return (0);
1074 }
1075
1076 /* ARGSUSED */
1077 static int
1078 zfsctl_shares_readdir(ap)
1079         struct vop_readdir_args /* {
1080                 struct vnode *a_vp;
1081                 struct uio *a_uio;
1082                 struct ucred *a_cred;
1083                 int *a_eofflag;
1084                 int *a_ncookies;
1085                 u_long **a_cookies;
1086         } */ *ap;
1087 {
1088         vnode_t *vp = ap->a_vp;
1089         uio_t *uiop = ap->a_uio;
1090         cred_t *cr = ap->a_cred;
1091         int *eofp = ap->a_eofflag;
1092         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1093         znode_t *dzp;
1094         int error;
1095
1096         ZFS_ENTER(zfsvfs);
1097
1098         if (zfsvfs->z_shares_dir == 0) {
1099                 ZFS_EXIT(zfsvfs);
1100                 return (ENOTSUP);
1101         }
1102         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1103                 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1104                 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1105                 VN_URELE(ZTOV(dzp));
1106         } else {
1107                 *eofp = 1;
1108                 error = ENOENT;
1109         }
1110
1111         ZFS_EXIT(zfsvfs);
1112         return (error);
1113 }
1114
1115 /*
1116  * pvp is the '.zfs' directory (zfsctl_node_t).
1117  * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1118  *
1119  * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1120  * when a lookup is performed on .zfs for "snapshot".
1121  */
1122 vnode_t *
1123 zfsctl_mknode_snapdir(vnode_t *pvp)
1124 {
1125         vnode_t *vp;
1126         zfsctl_snapdir_t *sdp;
1127
1128         vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1129             &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1130             zfsctl_snapdir_readdir_cb, NULL);
1131         sdp = vp->v_data;
1132         sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1133         sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1134         mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1135         avl_create(&sdp->sd_snaps, snapentry_compare,
1136             sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1137         VOP_UNLOCK(vp, 0);
1138         return (vp);
1139 }
1140
1141 vnode_t *
1142 zfsctl_mknode_shares(vnode_t *pvp)
1143 {
1144         vnode_t *vp;
1145         zfsctl_node_t *sdp;
1146
1147         vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1148             &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1149             NULL, NULL);
1150         sdp = vp->v_data;
1151         sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1152         VOP_UNLOCK(vp, 0);
1153         return (vp);
1154
1155 }
1156
1157 /* ARGSUSED */
1158 static int
1159 zfsctl_shares_getattr(ap)
1160         struct vop_getattr_args /* {
1161                 struct vnode *a_vp;
1162                 struct vattr *a_vap;
1163                 struct ucred *a_cred;
1164                 struct thread *a_td;
1165         } */ *ap;
1166 {
1167         vnode_t *vp = ap->a_vp;
1168         vattr_t *vap = ap->a_vap;
1169         cred_t *cr = ap->a_cred;
1170         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1171         znode_t *dzp;
1172         int error;
1173
1174         ZFS_ENTER(zfsvfs);
1175         if (zfsvfs->z_shares_dir == 0) {
1176                 ZFS_EXIT(zfsvfs);
1177                 return (ENOTSUP);
1178         }
1179         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1180                 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1181                 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1182                 VN_URELE(ZTOV(dzp));
1183         }
1184         ZFS_EXIT(zfsvfs);
1185         return (error);
1186 }
1187
1188 /* ARGSUSED */
1189 static int
1190 zfsctl_snapdir_getattr(ap)
1191         struct vop_getattr_args /* {
1192                 struct vnode *a_vp;
1193                 struct vattr *a_vap;
1194                 struct ucred *a_cred;
1195                 struct thread *a_td;
1196         } */ *ap;
1197 {
1198         struct vnode *vp = ap->a_vp;
1199         struct vattr *vap = ap->a_vap;
1200         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1201         zfsctl_snapdir_t *sdp = vp->v_data;
1202
1203         ZFS_ENTER(zfsvfs);
1204         zfsctl_common_getattr(vp, vap);
1205         vap->va_nodeid = gfs_file_inode(vp);
1206         vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1207         ZFS_EXIT(zfsvfs);
1208
1209         return (0);
1210 }
1211
1212 /* ARGSUSED */
1213 static int
1214 zfsctl_snapdir_inactive(ap)
1215         struct vop_inactive_args /* {
1216                 struct vnode *a_vp;
1217                 struct thread *a_td;
1218         } */ *ap;
1219 {
1220         vnode_t *vp = ap->a_vp;
1221         zfsctl_snapdir_t *sdp = vp->v_data;
1222         zfs_snapentry_t *sep;
1223
1224         /*
1225          * On forced unmount we have to free snapshots from here.
1226          */
1227         mutex_enter(&sdp->sd_lock);
1228         while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1229                 avl_remove(&sdp->sd_snaps, sep);
1230                 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1231                 kmem_free(sep, sizeof (zfs_snapentry_t));
1232         }
1233         mutex_exit(&sdp->sd_lock);
1234         gfs_dir_inactive(vp);
1235         ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1236         mutex_destroy(&sdp->sd_lock);
1237         avl_destroy(&sdp->sd_snaps);
1238         kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1239
1240         return (0);
1241 }
1242
1243 static struct vop_vector zfsctl_ops_snapdir = {
1244         .vop_default =  &default_vnodeops,
1245         .vop_open =     zfsctl_common_open,
1246         .vop_close =    zfsctl_common_close,
1247         .vop_ioctl =    VOP_EINVAL,
1248         .vop_getattr =  zfsctl_snapdir_getattr,
1249         .vop_access =   zfsctl_common_access,
1250         .vop_mkdir =    zfsctl_freebsd_snapdir_mkdir,
1251         .vop_readdir =  gfs_vop_readdir,
1252         .vop_lookup =   zfsctl_snapdir_lookup,
1253         .vop_inactive = zfsctl_snapdir_inactive,
1254         .vop_reclaim =  zfsctl_common_reclaim,
1255         .vop_fid =      zfsctl_common_fid,
1256 };
1257
1258 static struct vop_vector zfsctl_ops_shares = {
1259         .vop_default =  &default_vnodeops,
1260         .vop_open =     zfsctl_common_open,
1261         .vop_close =    zfsctl_common_close,
1262         .vop_ioctl =    VOP_EINVAL,
1263         .vop_getattr =  zfsctl_shares_getattr,
1264         .vop_access =   zfsctl_common_access,
1265         .vop_readdir =  zfsctl_shares_readdir,
1266         .vop_lookup =   zfsctl_shares_lookup,
1267         .vop_inactive = gfs_vop_inactive,
1268         .vop_reclaim =  zfsctl_common_reclaim,
1269         .vop_fid =      zfsctl_shares_fid,
1270 };
1271
1272 /*
1273  * pvp is the GFS vnode '.zfs/snapshot'.
1274  *
1275  * This creates a GFS node under '.zfs/snapshot' representing each
1276  * snapshot.  This newly created GFS node is what we mount snapshot
1277  * vfs_t's ontop of.
1278  */
1279 static vnode_t *
1280 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1281 {
1282         vnode_t *vp;
1283         zfsctl_node_t *zcp;
1284
1285         vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1286             &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1287         VN_HOLD(vp);
1288         zcp = vp->v_data;
1289         zcp->zc_id = objset;
1290         VOP_UNLOCK(vp, 0);
1291
1292         return (vp);
1293 }
1294
1295 static int
1296 zfsctl_snapshot_inactive(ap)
1297         struct vop_inactive_args /* {
1298                 struct vnode *a_vp;
1299                 struct thread *a_td;
1300         } */ *ap;
1301 {
1302         vnode_t *vp = ap->a_vp;
1303         cred_t *cr = ap->a_td->td_ucred;
1304         struct vop_inactive_args iap;
1305         zfsctl_snapdir_t *sdp;
1306         zfs_snapentry_t *sep, *next;
1307         int locked;
1308         vnode_t *dvp;
1309
1310         if (vp->v_count > 0)
1311                 goto end;
1312
1313         VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1314         sdp = dvp->v_data;
1315         VOP_UNLOCK(dvp, 0);
1316
1317         if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1318                 mutex_enter(&sdp->sd_lock);
1319
1320         ASSERT(!vn_ismntpt(vp));
1321
1322         sep = avl_first(&sdp->sd_snaps);
1323         while (sep != NULL) {
1324                 next = AVL_NEXT(&sdp->sd_snaps, sep);
1325
1326                 if (sep->se_root == vp) {
1327                         avl_remove(&sdp->sd_snaps, sep);
1328                         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1329                         kmem_free(sep, sizeof (zfs_snapentry_t));
1330                         break;
1331                 }
1332                 sep = next;
1333         }
1334         ASSERT(sep != NULL);
1335
1336         if (!locked)
1337                 mutex_exit(&sdp->sd_lock);
1338         VN_RELE(dvp);
1339 end:
1340
1341         /*
1342          * Dispose of the vnode for the snapshot mount point.
1343          * This is safe to do because once this entry has been removed
1344          * from the AVL tree, it can't be found again, so cannot become
1345          * "active".  If we lookup the same name again we will end up
1346          * creating a new vnode.
1347          */
1348         iap.a_vp = vp;
1349         return (gfs_vop_inactive(&iap));
1350 }
1351
1352 static int
1353 zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1354 {
1355
1356         VN_HOLD(*vpp);
1357         /* Snapshot should be already mounted, but just in case. */
1358         if (vn_mountedvfs(*vpp) == NULL)
1359                 return (ENOENT);
1360         return (traverse(vpp, lktype));
1361 }
1362
1363 static void
1364 zfsctl_traverse_end(vnode_t *vp, int err)
1365 {
1366
1367         if (err == 0)
1368                 vput(vp);
1369         else
1370                 VN_RELE(vp);
1371 }
1372
1373 static int
1374 zfsctl_snapshot_getattr(ap)
1375         struct vop_getattr_args /* {
1376                 struct vnode *a_vp;
1377                 struct vattr *a_vap;
1378                 struct ucred *a_cred;
1379         } */ *ap;
1380 {
1381         vnode_t *vp = ap->a_vp;
1382         int err;
1383
1384         err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1385         if (err == 0)
1386                 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1387         zfsctl_traverse_end(vp, err);
1388         return (err);
1389 }
1390
1391 static int
1392 zfsctl_snapshot_fid(ap)
1393         struct vop_fid_args /* {
1394                 struct vnode *a_vp;
1395                 struct fid *a_fid;
1396         } */ *ap;
1397 {
1398         vnode_t *vp = ap->a_vp;
1399         int err;
1400
1401         err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1402         if (err == 0)
1403                 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1404         zfsctl_traverse_end(vp, err);
1405         return (err);
1406 }
1407
1408 static int
1409 zfsctl_snapshot_lookup(ap)
1410         struct vop_lookup_args /* {
1411                 struct vnode *a_dvp;
1412                 struct vnode **a_vpp;
1413                 struct componentname *a_cnp;
1414         } */ *ap;
1415 {
1416         vnode_t *dvp = ap->a_dvp;
1417         vnode_t **vpp = ap->a_vpp;
1418         struct componentname *cnp = ap->a_cnp;
1419         cred_t *cr = ap->a_cnp->cn_cred;
1420         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1421         int error;
1422
1423         if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1424             cnp->cn_nameptr[1] != '.') {
1425                 return (ENOENT);
1426         }
1427
1428         ASSERT(dvp->v_type == VDIR);
1429         ASSERT(zfsvfs->z_ctldir != NULL);
1430
1431         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1432             NULL, 0, NULL, cr, NULL, NULL, NULL);
1433         if (error == 0)
1434                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1435         return (error);
1436 }
1437
1438 static int
1439 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1440 {
1441         zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1442         vnode_t *dvp, *vp;
1443         zfsctl_snapdir_t *sdp;
1444         zfs_snapentry_t *sep;
1445         int error;
1446
1447         ASSERT(zfsvfs->z_ctldir != NULL);
1448         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1449             NULL, 0, NULL, kcred, NULL, NULL, NULL);
1450         if (error != 0)
1451                 return (error);
1452         sdp = dvp->v_data;
1453
1454         mutex_enter(&sdp->sd_lock);
1455         sep = avl_first(&sdp->sd_snaps);
1456         while (sep != NULL) {
1457                 vp = sep->se_root;
1458                 if (vp == ap->a_vp)
1459                         break;
1460                 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1461         }
1462         if (sep == NULL) {
1463                 mutex_exit(&sdp->sd_lock);
1464                 error = ENOENT;
1465         } else {
1466                 size_t len;
1467
1468                 len = strlen(sep->se_name);
1469                 *ap->a_buflen -= len;
1470                 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1471                 mutex_exit(&sdp->sd_lock);
1472                 vhold(dvp);
1473                 *ap->a_vpp = dvp;
1474         }
1475         VN_RELE(dvp);
1476
1477         return (error);
1478 }
1479
1480 /*
1481  * These VP's should never see the light of day.  They should always
1482  * be covered.
1483  */
1484 static struct vop_vector zfsctl_ops_snapshot = {
1485         .vop_default =  &default_vnodeops,
1486         .vop_inactive = zfsctl_snapshot_inactive,
1487         .vop_lookup =   zfsctl_snapshot_lookup,
1488         .vop_reclaim =  zfsctl_common_reclaim,
1489         .vop_getattr =  zfsctl_snapshot_getattr,
1490         .vop_fid =      zfsctl_snapshot_fid,
1491         .vop_vptocnp =  zfsctl_snapshot_vptocnp,
1492 };
1493
1494 int
1495 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1496 {
1497         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1498         vnode_t *dvp, *vp;
1499         zfsctl_snapdir_t *sdp;
1500         zfsctl_node_t *zcp;
1501         zfs_snapentry_t *sep;
1502         int error;
1503
1504         ASSERT(zfsvfs->z_ctldir != NULL);
1505         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1506             NULL, 0, NULL, kcred, NULL, NULL, NULL);
1507         if (error != 0)
1508                 return (error);
1509         sdp = dvp->v_data;
1510
1511         mutex_enter(&sdp->sd_lock);
1512         sep = avl_first(&sdp->sd_snaps);
1513         while (sep != NULL) {
1514                 vp = sep->se_root;
1515                 zcp = vp->v_data;
1516                 if (zcp->zc_id == objsetid)
1517                         break;
1518
1519                 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1520         }
1521
1522         if (sep != NULL) {
1523                 VN_HOLD(vp);
1524                 /*
1525                  * Return the mounted root rather than the covered mount point.
1526                  * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1527                  * and returns the ZFS vnode mounted on top of the GFS node.
1528                  * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1529                  */
1530                 error = traverse(&vp, LK_SHARED | LK_RETRY);
1531                 if (error == 0) {
1532                         if (vp == sep->se_root)
1533                                 error = EINVAL;
1534                         else
1535                                 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1536                 }
1537                 mutex_exit(&sdp->sd_lock);
1538                 if (error == 0)
1539                         VN_URELE(vp);
1540                 else
1541                         VN_RELE(vp);
1542         } else {
1543                 error = EINVAL;
1544                 mutex_exit(&sdp->sd_lock);
1545         }
1546
1547         VN_RELE(dvp);
1548
1549         return (error);
1550 }
1551
1552 /*
1553  * Unmount any snapshots for the given filesystem.  This is called from
1554  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1555  * snapshots.
1556  */
1557 int
1558 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1559 {
1560         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1561         vnode_t *dvp;
1562         zfsctl_snapdir_t *sdp;
1563         zfs_snapentry_t *sep, *next;
1564         int error;
1565
1566         ASSERT(zfsvfs->z_ctldir != NULL);
1567         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1568             NULL, 0, NULL, cr, NULL, NULL, NULL);
1569         if (error != 0)
1570                 return (error);
1571         sdp = dvp->v_data;
1572
1573         mutex_enter(&sdp->sd_lock);
1574
1575         sep = avl_first(&sdp->sd_snaps);
1576         while (sep != NULL) {
1577                 next = AVL_NEXT(&sdp->sd_snaps, sep);
1578
1579                 /*
1580                  * If this snapshot is not mounted, then it must
1581                  * have just been unmounted by somebody else, and
1582                  * will be cleaned up by zfsctl_snapdir_inactive().
1583                  */
1584                 if (vn_ismntpt(sep->se_root)) {
1585                         error = zfsctl_unmount_snap(sep, fflags, cr);
1586                         if (error) {
1587                                 avl_index_t where;
1588
1589                                 /*
1590                                  * Before reinserting snapshot to the tree,
1591                                  * check if it was actually removed. For example
1592                                  * when snapshot mount point is busy, we will
1593                                  * have an error here, but there will be no need
1594                                  * to reinsert snapshot.
1595                                  */
1596                                 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1597                                         avl_insert(&sdp->sd_snaps, sep, where);
1598                                 break;
1599                         }
1600                 }
1601                 sep = next;
1602         }
1603
1604         mutex_exit(&sdp->sd_lock);
1605         VN_RELE(dvp);
1606
1607         return (error);
1608 }