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