]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_ctldir.c
MFC r299902,299938: mount_snapshot: consolidate all error handling
[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 static int
455 zfsctl_common_reclaim(ap)
456         struct vop_reclaim_args /* {
457                 struct vnode *a_vp;
458                 struct thread *a_td;
459         } */ *ap;
460 {
461         vnode_t *vp = ap->a_vp;
462
463         /*
464          * Destroy the vm object and flush associated pages.
465          */
466         vnode_destroy_vobject(vp);
467         VI_LOCK(vp);
468         vp->v_data = NULL;
469         VI_UNLOCK(vp);
470         return (0);
471 }
472
473 /*
474  * .zfs inode namespace
475  *
476  * We need to generate unique inode numbers for all files and directories
477  * within the .zfs pseudo-filesystem.  We use the following scheme:
478  *
479  *      ENTRY                   ZFSCTL_INODE
480  *      .zfs                    1
481  *      .zfs/snapshot           2
482  *      .zfs/snapshot/<snap>    objectid(snap)
483  */
484
485 #define ZFSCTL_INO_SNAP(id)     (id)
486
487 /*
488  * Get root directory attributes.
489  */
490 /* ARGSUSED */
491 static int
492 zfsctl_root_getattr(ap)
493         struct vop_getattr_args /* {
494                 struct vnode *a_vp;
495                 struct vattr *a_vap;
496                 struct ucred *a_cred;
497         } */ *ap;
498 {
499         struct vnode *vp = ap->a_vp;
500         struct vattr *vap = ap->a_vap;
501         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
502         zfsctl_node_t *zcp = vp->v_data;
503
504         ZFS_ENTER(zfsvfs);
505         vap->va_nodeid = ZFSCTL_INO_ROOT;
506         vap->va_nlink = vap->va_size = NROOT_ENTRIES;
507         vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
508         vap->va_birthtime = vap->va_ctime;
509
510         zfsctl_common_getattr(vp, vap);
511         ZFS_EXIT(zfsvfs);
512
513         return (0);
514 }
515
516 /*
517  * Special case the handling of "..".
518  */
519 /* ARGSUSED */
520 int
521 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
522     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
523     int *direntflags, pathname_t *realpnp)
524 {
525         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
526         int err;
527
528         /*
529          * No extended attributes allowed under .zfs
530          */
531         if (flags & LOOKUP_XATTR)
532                 return (SET_ERROR(EINVAL));
533
534         ZFS_ENTER(zfsvfs);
535
536         if (strcmp(nm, "..") == 0) {
537                 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
538                 if (err == 0)
539                         VOP_UNLOCK(*vpp, 0);
540         } else {
541                 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
542                     cr, ct, direntflags, realpnp);
543         }
544
545         ZFS_EXIT(zfsvfs);
546
547         return (err);
548 }
549
550 #ifdef sun
551 static int
552 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
553     caller_context_t *ct)
554 {
555         /*
556          * We only care about ACL_ENABLED so that libsec can
557          * display ACL correctly and not default to POSIX draft.
558          */
559         if (cmd == _PC_ACL_ENABLED) {
560                 *valp = _ACL_ACE_ENABLED;
561                 return (0);
562         }
563
564         return (fs_pathconf(vp, cmd, valp, cr, ct));
565 }
566 #endif  /* sun */
567
568 #ifdef sun
569 static const fs_operation_def_t zfsctl_tops_root[] = {
570         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
571         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
572         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
573         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_root_getattr }  },
574         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
575         { VOPNAME_READDIR,      { .vop_readdir = gfs_vop_readdir }      },
576         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_root_lookup }    },
577         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
578         { VOPNAME_INACTIVE,     { .vop_inactive = gfs_vop_inactive }    },
579         { VOPNAME_PATHCONF,     { .vop_pathconf = zfsctl_pathconf }     },
580         { VOPNAME_FID,          { .vop_fid = zfsctl_common_fid  }       },
581         { NULL }
582 };
583 #endif  /* sun */
584
585 /*
586  * Special case the handling of "..".
587  */
588 /* ARGSUSED */
589 int
590 zfsctl_freebsd_root_lookup(ap)
591         struct vop_lookup_args /* {
592                 struct vnode *a_dvp;
593                 struct vnode **a_vpp;
594                 struct componentname *a_cnp;
595         } */ *ap;
596 {
597         vnode_t *dvp = ap->a_dvp;
598         vnode_t **vpp = ap->a_vpp;
599         cred_t *cr = ap->a_cnp->cn_cred;
600         int flags = ap->a_cnp->cn_flags;
601         int nameiop = ap->a_cnp->cn_nameiop;
602         char nm[NAME_MAX + 1];
603         int err;
604
605         if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
606                 return (EOPNOTSUPP);
607
608         ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
609         strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
610
611         err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
612         if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
613                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
614         return (err);
615 }
616
617 static struct vop_vector zfsctl_ops_root = {
618         .vop_default =  &default_vnodeops,
619         .vop_open =     zfsctl_common_open,
620         .vop_close =    zfsctl_common_close,
621         .vop_ioctl =    VOP_EINVAL,
622         .vop_getattr =  zfsctl_root_getattr,
623         .vop_access =   zfsctl_common_access,
624         .vop_readdir =  gfs_vop_readdir,
625         .vop_lookup =   zfsctl_freebsd_root_lookup,
626         .vop_inactive = gfs_vop_inactive,
627         .vop_reclaim =  zfsctl_common_reclaim,
628 #ifdef TODO
629         .vop_pathconf = zfsctl_pathconf,
630 #endif
631         .vop_fid =      zfsctl_common_fid,
632 };
633
634 /*
635  * Gets the full dataset name that corresponds to the given snapshot name
636  * Example:
637  *      zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
638  */
639 static int
640 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
641 {
642         objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
643
644         if (zfs_component_namecheck(name, NULL, NULL) != 0)
645                 return (SET_ERROR(EILSEQ));
646         dmu_objset_name(os, zname);
647         if (strlen(zname) + 1 + strlen(name) >= len)
648                 return (SET_ERROR(ENAMETOOLONG));
649         (void) strcat(zname, "@");
650         (void) strcat(zname, name);
651         return (0);
652 }
653
654 static int
655 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
656 {
657         vnode_t *svp = sep->se_root;
658         int error;
659
660         ASSERT(vn_ismntpt(svp));
661
662         /* this will be dropped by dounmount() */
663         if ((error = vn_vfswlock(svp)) != 0)
664                 return (error);
665
666 #ifdef sun
667         VN_HOLD(svp);
668         error = dounmount(vn_mountedvfs(svp), fflags, cr);
669         if (error) {
670                 VN_RELE(svp);
671                 return (error);
672         }
673
674         /*
675          * We can't use VN_RELE(), as that will try to invoke
676          * zfsctl_snapdir_inactive(), which would cause us to destroy
677          * the sd_lock mutex held by our caller.
678          */
679         ASSERT(svp->v_count == 1);
680         gfs_vop_inactive(svp, cr, NULL);
681
682         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
683         kmem_free(sep, sizeof (zfs_snapentry_t));
684
685         return (0);
686 #else   /* !sun */
687         return (dounmount(vn_mountedvfs(svp), fflags, curthread));
688 #endif  /* !sun */
689 }
690
691 #ifdef sun
692 static void
693 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
694 {
695         avl_index_t where;
696         vfs_t *vfsp;
697         refstr_t *pathref;
698         char newpath[MAXNAMELEN];
699         char *tail;
700
701         ASSERT(MUTEX_HELD(&sdp->sd_lock));
702         ASSERT(sep != NULL);
703
704         vfsp = vn_mountedvfs(sep->se_root);
705         ASSERT(vfsp != NULL);
706
707         vfs_lock_wait(vfsp);
708
709         /*
710          * Change the name in the AVL tree.
711          */
712         avl_remove(&sdp->sd_snaps, sep);
713         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
714         sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
715         (void) strcpy(sep->se_name, nm);
716         VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
717         avl_insert(&sdp->sd_snaps, sep, where);
718
719         /*
720          * Change the current mountpoint info:
721          *      - update the tail of the mntpoint path
722          *      - update the tail of the resource path
723          */
724         pathref = vfs_getmntpoint(vfsp);
725         (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
726         VERIFY((tail = strrchr(newpath, '/')) != NULL);
727         *(tail+1) = '\0';
728         ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
729         (void) strcat(newpath, nm);
730         refstr_rele(pathref);
731         vfs_setmntpoint(vfsp, newpath, 0);
732
733         pathref = vfs_getresource(vfsp);
734         (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
735         VERIFY((tail = strrchr(newpath, '@')) != NULL);
736         *(tail+1) = '\0';
737         ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
738         (void) strcat(newpath, nm);
739         refstr_rele(pathref);
740         vfs_setresource(vfsp, newpath, 0);
741
742         vfs_unlock(vfsp);
743 }
744 #endif  /* sun */
745
746 #ifdef sun
747 /*ARGSUSED*/
748 static int
749 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
750     cred_t *cr, caller_context_t *ct, int flags)
751 {
752         zfsctl_snapdir_t *sdp = sdvp->v_data;
753         zfs_snapentry_t search, *sep;
754         zfsvfs_t *zfsvfs;
755         avl_index_t where;
756         char from[MAXNAMELEN], to[MAXNAMELEN];
757         char real[MAXNAMELEN], fsname[MAXNAMELEN];
758         int err;
759
760         zfsvfs = sdvp->v_vfsp->vfs_data;
761         ZFS_ENTER(zfsvfs);
762
763         if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
764                 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
765                     MAXNAMELEN, NULL);
766                 if (err == 0) {
767                         snm = real;
768                 } else if (err != ENOTSUP) {
769                         ZFS_EXIT(zfsvfs);
770                         return (err);
771                 }
772         }
773
774         ZFS_EXIT(zfsvfs);
775
776         dmu_objset_name(zfsvfs->z_os, fsname);
777
778         err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
779         if (err == 0)
780                 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
781         if (err == 0)
782                 err = zfs_secpolicy_rename_perms(from, to, cr);
783         if (err != 0)
784                 return (err);
785
786         /*
787          * Cannot move snapshots out of the snapdir.
788          */
789         if (sdvp != tdvp)
790                 return (SET_ERROR(EINVAL));
791
792         if (strcmp(snm, tnm) == 0)
793                 return (0);
794
795         mutex_enter(&sdp->sd_lock);
796
797         search.se_name = (char *)snm;
798         if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
799                 mutex_exit(&sdp->sd_lock);
800                 return (SET_ERROR(ENOENT));
801         }
802
803         err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0);
804         if (err == 0)
805                 zfsctl_rename_snap(sdp, sep, tnm);
806
807         mutex_exit(&sdp->sd_lock);
808
809         return (err);
810 }
811 #endif  /* sun */
812
813 #ifdef sun
814 /* ARGSUSED */
815 static int
816 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
817     caller_context_t *ct, int flags)
818 {
819         zfsctl_snapdir_t *sdp = dvp->v_data;
820         zfs_snapentry_t *sep;
821         zfs_snapentry_t search;
822         zfsvfs_t *zfsvfs;
823         char snapname[MAXNAMELEN];
824         char real[MAXNAMELEN];
825         int err;
826
827         zfsvfs = dvp->v_vfsp->vfs_data;
828         ZFS_ENTER(zfsvfs);
829
830         if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
831
832                 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
833                     MAXNAMELEN, NULL);
834                 if (err == 0) {
835                         name = real;
836                 } else if (err != ENOTSUP) {
837                         ZFS_EXIT(zfsvfs);
838                         return (err);
839                 }
840         }
841
842         ZFS_EXIT(zfsvfs);
843
844         err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
845         if (err == 0)
846                 err = zfs_secpolicy_destroy_perms(snapname, cr);
847         if (err != 0)
848                 return (err);
849
850         mutex_enter(&sdp->sd_lock);
851
852         search.se_name = name;
853         sep = avl_find(&sdp->sd_snaps, &search, NULL);
854         if (sep) {
855                 avl_remove(&sdp->sd_snaps, sep);
856                 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
857                 if (err != 0)
858                         avl_add(&sdp->sd_snaps, sep);
859                 else
860                         err = dsl_destroy_snapshot(snapname, B_FALSE);
861         } else {
862                 err = SET_ERROR(ENOENT);
863         }
864
865         mutex_exit(&sdp->sd_lock);
866
867         return (err);
868 }
869 #endif  /* sun */
870
871 /*
872  * This creates a snapshot under '.zfs/snapshot'.
873  */
874 /* ARGSUSED */
875 static int
876 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
877     cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
878 {
879         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
880         char name[MAXNAMELEN];
881         int err;
882         static enum symfollow follow = NO_FOLLOW;
883         static enum uio_seg seg = UIO_SYSSPACE;
884
885         if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
886                 return (SET_ERROR(EILSEQ));
887
888         dmu_objset_name(zfsvfs->z_os, name);
889
890         *vpp = NULL;
891
892         err = zfs_secpolicy_snapshot_perms(name, cr);
893         if (err != 0)
894                 return (err);
895
896         if (err == 0) {
897                 err = dmu_objset_snapshot_one(name, dirname);
898                 if (err != 0)
899                         return (err);
900                 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
901         }
902
903         return (err);
904 }
905
906 static int
907 zfsctl_freebsd_snapdir_mkdir(ap)
908         struct vop_mkdir_args /* {
909                 struct vnode *a_dvp;
910                 struct vnode **a_vpp;
911                 struct componentname *a_cnp;
912                 struct vattr *a_vap;
913         } */ *ap;
914 {
915
916         ASSERT(ap->a_cnp->cn_flags & SAVENAME);
917
918         return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
919             ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
920 }
921
922 /*
923  * Lookup entry point for the 'snapshot' directory.  Try to open the
924  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
925  * Perform a mount of the associated dataset on top of the vnode.
926  */
927 /* ARGSUSED */
928 int
929 zfsctl_snapdir_lookup(ap)
930         struct vop_lookup_args /* {
931                 struct vnode *a_dvp;
932                 struct vnode **a_vpp;
933                 struct componentname *a_cnp;
934         } */ *ap;
935 {
936         vnode_t *dvp = ap->a_dvp;
937         vnode_t **vpp = ap->a_vpp;
938         struct componentname *cnp = ap->a_cnp;
939         char nm[NAME_MAX + 1];
940         zfsctl_snapdir_t *sdp = dvp->v_data;
941         objset_t *snap;
942         char snapname[MAXNAMELEN];
943         char real[MAXNAMELEN];
944         char *mountpoint;
945         zfs_snapentry_t *sep, search;
946         size_t mountpoint_len;
947         avl_index_t where;
948         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
949         int err;
950         int flags = 0;
951
952         /*
953          * No extended attributes allowed under .zfs
954          */
955         if (flags & LOOKUP_XATTR)
956                 return (SET_ERROR(EINVAL));
957         ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
958         strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
959
960         ASSERT(dvp->v_type == VDIR);
961
962         *vpp = NULL;
963
964         /*
965          * If we get a recursive call, that means we got called
966          * from the domount() code while it was trying to look up the
967          * spec (which looks like a local path for zfs).  We need to
968          * add some flag to domount() to tell it not to do this lookup.
969          */
970         if (MUTEX_HELD(&sdp->sd_lock))
971                 return (SET_ERROR(ENOENT));
972
973         ZFS_ENTER(zfsvfs);
974
975         if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
976                 ZFS_EXIT(zfsvfs);
977                 return (0);
978         }
979
980         if (flags & FIGNORECASE) {
981                 boolean_t conflict = B_FALSE;
982
983                 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
984                     MAXNAMELEN, &conflict);
985                 if (err == 0) {
986                         strlcpy(nm, real, sizeof(nm));
987                 } else if (err != ENOTSUP) {
988                         ZFS_EXIT(zfsvfs);
989                         return (err);
990                 }
991 #if 0
992                 if (realpnp)
993                         (void) strlcpy(realpnp->pn_buf, nm,
994                             realpnp->pn_bufsize);
995                 if (conflict && direntflags)
996                         *direntflags = ED_CASE_CONFLICT;
997 #endif
998         }
999
1000         mutex_enter(&sdp->sd_lock);
1001         search.se_name = (char *)nm;
1002         if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
1003                 *vpp = sep->se_root;
1004                 VN_HOLD(*vpp);
1005                 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
1006                 if (err != 0) {
1007                         VN_RELE(*vpp);
1008                         *vpp = NULL;
1009                 } else if (*vpp == sep->se_root) {
1010                         /*
1011                          * The snapshot was unmounted behind our backs,
1012                          * try to remount it.
1013                          */
1014                         VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0);
1015                         goto domount;
1016                 } else {
1017                         /*
1018                          * VROOT was set during the traverse call.  We need
1019                          * to clear it since we're pretending to be part
1020                          * of our parent's vfs.
1021                          */
1022                         (*vpp)->v_flag &= ~VROOT;
1023                 }
1024                 mutex_exit(&sdp->sd_lock);
1025                 ZFS_EXIT(zfsvfs);
1026                 return (err);
1027         }
1028
1029         /*
1030          * The requested snapshot is not currently mounted, look it up.
1031          */
1032         err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
1033         if (err != 0) {
1034                 mutex_exit(&sdp->sd_lock);
1035                 ZFS_EXIT(zfsvfs);
1036                 /*
1037                  * handle "ls *" or "?" in a graceful manner,
1038                  * forcing EILSEQ to ENOENT.
1039                  * Since shell ultimately passes "*" or "?" as name to lookup
1040                  */
1041                 return (err == EILSEQ ? ENOENT : err);
1042         }
1043         if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
1044                 mutex_exit(&sdp->sd_lock);
1045 #ifdef illumos
1046                 ZFS_EXIT(zfsvfs);
1047                 return (SET_ERROR(ENOENT));
1048 #else   /* !illumos */
1049                 /* Translate errors and add SAVENAME when needed. */
1050                 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
1051                         err = EJUSTRETURN;
1052                         cnp->cn_flags |= SAVENAME;
1053                 } else {
1054                         err = SET_ERROR(ENOENT);
1055                 }
1056                 ZFS_EXIT(zfsvfs);
1057                 return (err);
1058 #endif  /* !illumos */
1059         }
1060
1061         sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
1062         sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
1063         (void) strcpy(sep->se_name, nm);
1064         *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
1065         avl_insert(&sdp->sd_snaps, sep, where);
1066
1067         dmu_objset_rele(snap, FTAG);
1068 domount:
1069         mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1070             strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1;
1071         mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1072         (void) snprintf(mountpoint, mountpoint_len,
1073             "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1074             dvp->v_vfsp->mnt_stat.f_mntonname, nm);
1075         VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
1076         err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
1077         kmem_free(mountpoint, mountpoint_len);
1078         if (err == 0) {
1079                 /*
1080                  * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1081                  *
1082                  * This is where we lie about our v_vfsp in order to
1083                  * make .zfs/snapshot/<snapname> accessible over NFS
1084                  * without requiring manual mounts of <snapname>.
1085                  */
1086                 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
1087                 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1088         }
1089         mutex_exit(&sdp->sd_lock);
1090         ZFS_EXIT(zfsvfs);
1091
1092 #ifdef illumos
1093         /*
1094          * If we had an error, drop our hold on the vnode and
1095          * zfsctl_snapshot_inactive() will clean up.
1096          */
1097         if (err != 0) {
1098                 VN_RELE(*vpp);
1099                 *vpp = NULL;
1100         }
1101 #else
1102         if (err != 0)
1103                 *vpp = NULL;
1104 #endif
1105         return (err);
1106 }
1107
1108 /* ARGSUSED */
1109 int
1110 zfsctl_shares_lookup(ap)
1111         struct vop_lookup_args /* {
1112                 struct vnode *a_dvp;
1113                 struct vnode **a_vpp;
1114                 struct componentname *a_cnp;
1115         } */ *ap;
1116 {
1117         vnode_t *dvp = ap->a_dvp;
1118         vnode_t **vpp = ap->a_vpp;
1119         struct componentname *cnp = ap->a_cnp;
1120         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1121         char nm[NAME_MAX + 1];
1122         znode_t *dzp;
1123         int error;
1124
1125         ZFS_ENTER(zfsvfs);
1126
1127         ASSERT(cnp->cn_namelen < sizeof(nm));
1128         strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1129
1130         if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1131                 ZFS_EXIT(zfsvfs);
1132                 return (0);
1133         }
1134
1135         if (zfsvfs->z_shares_dir == 0) {
1136                 ZFS_EXIT(zfsvfs);
1137                 return (SET_ERROR(ENOTSUP));
1138         }
1139         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
1140                 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1141
1142         VN_RELE(ZTOV(dzp));
1143         ZFS_EXIT(zfsvfs);
1144
1145         return (error);
1146 }
1147
1148 /* ARGSUSED */
1149 static int
1150 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1151     offset_t *offp, offset_t *nextp, void *data, int flags)
1152 {
1153         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1154         char snapname[MAXNAMELEN];
1155         uint64_t id, cookie;
1156         boolean_t case_conflict;
1157         int error;
1158
1159         ZFS_ENTER(zfsvfs);
1160
1161         cookie = *offp;
1162         dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1163         error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1164             &cookie, &case_conflict);
1165         dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1166         if (error) {
1167                 ZFS_EXIT(zfsvfs);
1168                 if (error == ENOENT) {
1169                         *eofp = 1;
1170                         return (0);
1171                 }
1172                 return (error);
1173         }
1174
1175         if (flags & V_RDDIR_ENTFLAGS) {
1176                 edirent_t *eodp = dp;
1177
1178                 (void) strcpy(eodp->ed_name, snapname);
1179                 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1180                 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1181         } else {
1182                 struct dirent64 *odp = dp;
1183
1184                 (void) strcpy(odp->d_name, snapname);
1185                 odp->d_ino = ZFSCTL_INO_SNAP(id);
1186         }
1187         *nextp = cookie;
1188
1189         ZFS_EXIT(zfsvfs);
1190
1191         return (0);
1192 }
1193
1194 /* ARGSUSED */
1195 static int
1196 zfsctl_shares_readdir(ap)
1197         struct vop_readdir_args /* {
1198                 struct vnode *a_vp;
1199                 struct uio *a_uio;
1200                 struct ucred *a_cred;
1201                 int *a_eofflag;
1202                 int *a_ncookies;
1203                 u_long **a_cookies;
1204         } */ *ap;
1205 {
1206         vnode_t *vp = ap->a_vp;
1207         uio_t *uiop = ap->a_uio;
1208         cred_t *cr = ap->a_cred;
1209         int *eofp = ap->a_eofflag;
1210         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1211         znode_t *dzp;
1212         int error;
1213
1214         ZFS_ENTER(zfsvfs);
1215
1216         if (zfsvfs->z_shares_dir == 0) {
1217                 ZFS_EXIT(zfsvfs);
1218                 return (SET_ERROR(ENOTSUP));
1219         }
1220         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1221                 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1222                 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1223                 VN_URELE(ZTOV(dzp));
1224         } else {
1225                 *eofp = 1;
1226                 error = SET_ERROR(ENOENT);
1227         }
1228
1229         ZFS_EXIT(zfsvfs);
1230         return (error);
1231 }
1232
1233 /*
1234  * pvp is the '.zfs' directory (zfsctl_node_t).
1235  *
1236  * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1237  *
1238  * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1239  * when a lookup is performed on .zfs for "snapshot".
1240  */
1241 vnode_t *
1242 zfsctl_mknode_snapdir(vnode_t *pvp)
1243 {
1244         vnode_t *vp;
1245         zfsctl_snapdir_t *sdp;
1246
1247         vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1248             &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1249             zfsctl_snapdir_readdir_cb, NULL);
1250         sdp = vp->v_data;
1251         sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1252         sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1253         mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1254         avl_create(&sdp->sd_snaps, snapentry_compare,
1255             sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1256         VOP_UNLOCK(vp, 0);
1257         return (vp);
1258 }
1259
1260 vnode_t *
1261 zfsctl_mknode_shares(vnode_t *pvp)
1262 {
1263         vnode_t *vp;
1264         zfsctl_node_t *sdp;
1265
1266         vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1267             &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1268             NULL, NULL);
1269         sdp = vp->v_data;
1270         sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1271         VOP_UNLOCK(vp, 0);
1272         return (vp);
1273
1274 }
1275
1276 /* ARGSUSED */
1277 static int
1278 zfsctl_shares_getattr(ap)
1279         struct vop_getattr_args /* {
1280                 struct vnode *a_vp;
1281                 struct vattr *a_vap;
1282                 struct ucred *a_cred;
1283                 struct thread *a_td;
1284         } */ *ap;
1285 {
1286         vnode_t *vp = ap->a_vp;
1287         vattr_t *vap = ap->a_vap;
1288         cred_t *cr = ap->a_cred;
1289         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1290         znode_t *dzp;
1291         int error;
1292
1293         ZFS_ENTER(zfsvfs);
1294         if (zfsvfs->z_shares_dir == 0) {
1295                 ZFS_EXIT(zfsvfs);
1296                 return (SET_ERROR(ENOTSUP));
1297         }
1298         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1299                 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1300                 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1301                 VN_URELE(ZTOV(dzp));
1302         }
1303         ZFS_EXIT(zfsvfs);
1304         return (error);
1305
1306
1307 }
1308
1309 /* ARGSUSED */
1310 static int
1311 zfsctl_snapdir_getattr(ap)
1312         struct vop_getattr_args /* {
1313                 struct vnode *a_vp;
1314                 struct vattr *a_vap;
1315                 struct ucred *a_cred;
1316         } */ *ap;
1317 {
1318         vnode_t *vp = ap->a_vp;
1319         vattr_t *vap = ap->a_vap;
1320         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1321         zfsctl_snapdir_t *sdp = vp->v_data;
1322
1323         ZFS_ENTER(zfsvfs);
1324         zfsctl_common_getattr(vp, vap);
1325         vap->va_nodeid = gfs_file_inode(vp);
1326         vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1327         vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1328         vap->va_birthtime = vap->va_ctime;
1329         ZFS_EXIT(zfsvfs);
1330
1331         return (0);
1332 }
1333
1334 /* ARGSUSED */
1335 static int
1336 zfsctl_snapdir_inactive(ap)
1337         struct vop_inactive_args /* {
1338                 struct vnode *a_vp;
1339                 struct thread *a_td;
1340         } */ *ap;
1341 {
1342         vnode_t *vp = ap->a_vp;
1343         zfsctl_snapdir_t *sdp = vp->v_data;
1344         zfs_snapentry_t *sep;
1345
1346         /*
1347          * On forced unmount we have to free snapshots from here.
1348          */
1349         mutex_enter(&sdp->sd_lock);
1350         while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1351                 avl_remove(&sdp->sd_snaps, sep);
1352                 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1353                 kmem_free(sep, sizeof (zfs_snapentry_t));
1354         }
1355         mutex_exit(&sdp->sd_lock);
1356         gfs_dir_inactive(vp);
1357         ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1358         mutex_destroy(&sdp->sd_lock);
1359         avl_destroy(&sdp->sd_snaps);
1360         kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1361
1362         return (0);
1363 }
1364
1365 #ifdef sun
1366 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1367         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
1368         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
1369         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
1370         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_snapdir_getattr } },
1371         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
1372         { VOPNAME_RENAME,       { .vop_rename = zfsctl_snapdir_rename } },
1373         { VOPNAME_RMDIR,        { .vop_rmdir = zfsctl_snapdir_remove }  },
1374         { VOPNAME_MKDIR,        { .vop_mkdir = zfsctl_snapdir_mkdir }   },
1375         { VOPNAME_READDIR,      { .vop_readdir = gfs_vop_readdir }      },
1376         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_snapdir_lookup } },
1377         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
1378         { VOPNAME_INACTIVE,     { .vop_inactive = zfsctl_snapdir_inactive } },
1379         { VOPNAME_FID,          { .vop_fid = zfsctl_common_fid }        },
1380         { NULL }
1381 };
1382
1383 static const fs_operation_def_t zfsctl_tops_shares[] = {
1384         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
1385         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
1386         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
1387         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_shares_getattr } },
1388         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
1389         { VOPNAME_READDIR,      { .vop_readdir = zfsctl_shares_readdir } },
1390         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_shares_lookup }  },
1391         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
1392         { VOPNAME_INACTIVE,     { .vop_inactive = gfs_vop_inactive } },
1393         { VOPNAME_FID,          { .vop_fid = zfsctl_shares_fid } },
1394         { NULL }
1395 };
1396 #else   /* !sun */
1397 static struct vop_vector zfsctl_ops_snapdir = {
1398         .vop_default =  &default_vnodeops,
1399         .vop_open =     zfsctl_common_open,
1400         .vop_close =    zfsctl_common_close,
1401         .vop_ioctl =    VOP_EINVAL,
1402         .vop_getattr =  zfsctl_snapdir_getattr,
1403         .vop_access =   zfsctl_common_access,
1404         .vop_mkdir =    zfsctl_freebsd_snapdir_mkdir,
1405         .vop_readdir =  gfs_vop_readdir,
1406         .vop_lookup =   zfsctl_snapdir_lookup,
1407         .vop_inactive = zfsctl_snapdir_inactive,
1408         .vop_reclaim =  zfsctl_common_reclaim,
1409         .vop_fid =      zfsctl_common_fid,
1410 };
1411
1412 static struct vop_vector zfsctl_ops_shares = {
1413         .vop_default =  &default_vnodeops,
1414         .vop_open =     zfsctl_common_open,
1415         .vop_close =    zfsctl_common_close,
1416         .vop_ioctl =    VOP_EINVAL,
1417         .vop_getattr =  zfsctl_shares_getattr,
1418         .vop_access =   zfsctl_common_access,
1419         .vop_readdir =  zfsctl_shares_readdir,
1420         .vop_lookup =   zfsctl_shares_lookup,
1421         .vop_inactive = gfs_vop_inactive,
1422         .vop_reclaim =  zfsctl_common_reclaim,
1423         .vop_fid =      zfsctl_shares_fid,
1424 };
1425 #endif  /* !sun */
1426
1427 /*
1428  * pvp is the GFS vnode '.zfs/snapshot'.
1429  *
1430  * This creates a GFS node under '.zfs/snapshot' representing each
1431  * snapshot.  This newly created GFS node is what we mount snapshot
1432  * vfs_t's ontop of.
1433  */
1434 static vnode_t *
1435 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1436 {
1437         vnode_t *vp;
1438         zfsctl_node_t *zcp;
1439
1440         vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1441             &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1442         zcp = vp->v_data;
1443         zcp->zc_id = objset;
1444         VOP_UNLOCK(vp, 0);
1445
1446         return (vp);
1447 }
1448
1449 static int
1450 zfsctl_snapshot_inactive(ap)
1451         struct vop_inactive_args /* {
1452                 struct vnode *a_vp;
1453                 struct thread *a_td;
1454         } */ *ap;
1455 {
1456         vnode_t *vp = ap->a_vp;
1457         cred_t *cr = ap->a_td->td_ucred;
1458         struct vop_inactive_args iap;
1459         zfsctl_snapdir_t *sdp;
1460         zfs_snapentry_t *sep, *next;
1461         int locked;
1462         vnode_t *dvp;
1463
1464         if (vp->v_count > 0)
1465                 goto end;
1466
1467         VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1468         sdp = dvp->v_data;
1469         VOP_UNLOCK(dvp, 0);
1470
1471         if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1472                 mutex_enter(&sdp->sd_lock);
1473
1474         ASSERT(!vn_ismntpt(vp));
1475
1476         sep = avl_first(&sdp->sd_snaps);
1477         while (sep != NULL) {
1478                 next = AVL_NEXT(&sdp->sd_snaps, sep);
1479
1480                 if (sep->se_root == vp) {
1481                         avl_remove(&sdp->sd_snaps, sep);
1482                         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1483                         kmem_free(sep, sizeof (zfs_snapentry_t));
1484                         break;
1485                 }
1486                 sep = next;
1487         }
1488         ASSERT(sep != NULL);
1489
1490         if (!locked)
1491                 mutex_exit(&sdp->sd_lock);
1492         VN_RELE(dvp);
1493
1494 end:
1495         /*
1496          * Dispose of the vnode for the snapshot mount point.
1497          * This is safe to do because once this entry has been removed
1498          * from the AVL tree, it can't be found again, so cannot become
1499          * "active".  If we lookup the same name again we will end up
1500          * creating a new vnode.
1501          */
1502         iap.a_vp = vp;
1503         return (gfs_vop_inactive(&iap));
1504 }
1505
1506 static int
1507 zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1508 {
1509
1510         VN_HOLD(*vpp);
1511         /* Snapshot should be already mounted, but just in case. */
1512         if (vn_mountedvfs(*vpp) == NULL)
1513                 return (ENOENT);
1514         return (traverse(vpp, lktype));
1515 }
1516
1517 static void
1518 zfsctl_traverse_end(vnode_t *vp, int err)
1519 {
1520
1521         if (err == 0)
1522                 vput(vp);
1523         else
1524                 VN_RELE(vp);
1525 }
1526
1527 static int
1528 zfsctl_snapshot_getattr(ap)
1529         struct vop_getattr_args /* {
1530                 struct vnode *a_vp;
1531                 struct vattr *a_vap;
1532                 struct ucred *a_cred;
1533         } */ *ap;
1534 {
1535         vnode_t *vp = ap->a_vp;
1536         int err;
1537
1538         err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1539         if (err == 0)
1540                 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1541         zfsctl_traverse_end(vp, err);
1542         return (err);
1543 }
1544
1545 static int
1546 zfsctl_snapshot_fid(ap)
1547         struct vop_fid_args /* {
1548                 struct vnode *a_vp;
1549                 struct fid *a_fid;
1550         } */ *ap;
1551 {
1552         vnode_t *vp = ap->a_vp;
1553         int err;
1554
1555         err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1556         if (err == 0)
1557                 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1558         zfsctl_traverse_end(vp, err);
1559         return (err);
1560 }
1561
1562 static int
1563 zfsctl_snapshot_lookup(ap)
1564         struct vop_lookup_args /* {
1565                 struct vnode *a_dvp;
1566                 struct vnode **a_vpp;
1567                 struct componentname *a_cnp;
1568         } */ *ap;
1569 {
1570         vnode_t *dvp = ap->a_dvp;
1571         vnode_t **vpp = ap->a_vpp;
1572         struct componentname *cnp = ap->a_cnp;
1573         cred_t *cr = ap->a_cnp->cn_cred;
1574         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1575         int error;
1576
1577         if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1578             cnp->cn_nameptr[1] != '.') {
1579                 return (ENOENT);
1580         }
1581
1582         ASSERT(dvp->v_type == VDIR);
1583         ASSERT(zfsvfs->z_ctldir != NULL);
1584
1585         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1586             NULL, 0, NULL, cr, NULL, NULL, NULL);
1587         if (error == 0)
1588                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1589         return (error);
1590 }
1591
1592 static int
1593 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1594 {
1595         zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1596         vnode_t *dvp, *vp;
1597         zfsctl_snapdir_t *sdp;
1598         zfs_snapentry_t *sep;
1599         int error;
1600
1601         ASSERT(zfsvfs->z_ctldir != NULL);
1602         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1603             NULL, 0, NULL, kcred, NULL, NULL, NULL);
1604         if (error != 0)
1605                 return (error);
1606         sdp = dvp->v_data;
1607
1608         mutex_enter(&sdp->sd_lock);
1609         sep = avl_first(&sdp->sd_snaps);
1610         while (sep != NULL) {
1611                 vp = sep->se_root;
1612                 if (vp == ap->a_vp)
1613                         break;
1614                 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1615         }
1616         if (sep == NULL) {
1617                 mutex_exit(&sdp->sd_lock);
1618                 error = ENOENT;
1619         } else {
1620                 size_t len;
1621
1622                 len = strlen(sep->se_name);
1623                 *ap->a_buflen -= len;
1624                 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1625                 mutex_exit(&sdp->sd_lock);
1626                 vref(dvp);
1627                 *ap->a_vpp = dvp;
1628         }
1629         VN_RELE(dvp);
1630
1631         return (error);
1632 }
1633
1634 /*
1635  * These VP's should never see the light of day.  They should always
1636  * be covered.
1637  */
1638 static struct vop_vector zfsctl_ops_snapshot = {
1639         .vop_default =  &default_vnodeops,
1640         .vop_inactive = zfsctl_snapshot_inactive,
1641         .vop_lookup =   zfsctl_snapshot_lookup,
1642         .vop_reclaim =  zfsctl_common_reclaim,
1643         .vop_getattr =  zfsctl_snapshot_getattr,
1644         .vop_fid =      zfsctl_snapshot_fid,
1645         .vop_vptocnp =  zfsctl_snapshot_vptocnp,
1646 };
1647
1648 int
1649 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1650 {
1651         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1652         vnode_t *dvp, *vp;
1653         zfsctl_snapdir_t *sdp;
1654         zfsctl_node_t *zcp;
1655         zfs_snapentry_t *sep;
1656         int error;
1657
1658         ASSERT(zfsvfs->z_ctldir != NULL);
1659         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1660             NULL, 0, NULL, kcred, NULL, NULL, NULL);
1661         if (error != 0)
1662                 return (error);
1663         sdp = dvp->v_data;
1664
1665         mutex_enter(&sdp->sd_lock);
1666         sep = avl_first(&sdp->sd_snaps);
1667         while (sep != NULL) {
1668                 vp = sep->se_root;
1669                 zcp = vp->v_data;
1670                 if (zcp->zc_id == objsetid)
1671                         break;
1672
1673                 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1674         }
1675
1676         if (sep != NULL) {
1677                 VN_HOLD(vp);
1678                 /*
1679                  * Return the mounted root rather than the covered mount point.
1680                  * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1681                  * and returns the ZFS vnode mounted on top of the GFS node.
1682                  * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1683                  */
1684                 error = traverse(&vp, LK_SHARED | LK_RETRY);
1685                 if (error == 0) {
1686                         if (vp == sep->se_root)
1687                                 error = SET_ERROR(EINVAL);
1688                         else
1689                                 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1690                 }
1691                 mutex_exit(&sdp->sd_lock);
1692                 if (error == 0)
1693                         VN_URELE(vp);
1694                 else
1695                         VN_RELE(vp);
1696         } else {
1697                 error = SET_ERROR(EINVAL);
1698                 mutex_exit(&sdp->sd_lock);
1699         }
1700
1701         VN_RELE(dvp);
1702
1703         return (error);
1704 }
1705
1706 /*
1707  * Unmount any snapshots for the given filesystem.  This is called from
1708  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1709  * snapshots.
1710  */
1711 int
1712 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1713 {
1714         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1715         vnode_t *dvp;
1716         zfsctl_snapdir_t *sdp;
1717         zfs_snapentry_t *sep, *next;
1718         int error;
1719
1720         ASSERT(zfsvfs->z_ctldir != NULL);
1721         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1722             NULL, 0, NULL, cr, NULL, NULL, NULL);
1723         if (error != 0)
1724                 return (error);
1725         sdp = dvp->v_data;
1726
1727         mutex_enter(&sdp->sd_lock);
1728
1729         sep = avl_first(&sdp->sd_snaps);
1730         while (sep != NULL) {
1731                 next = AVL_NEXT(&sdp->sd_snaps, sep);
1732
1733                 /*
1734                  * If this snapshot is not mounted, then it must
1735                  * have just been unmounted by somebody else, and
1736                  * will be cleaned up by zfsctl_snapdir_inactive().
1737                  */
1738                 if (vn_ismntpt(sep->se_root)) {
1739                         error = zfsctl_unmount_snap(sep, fflags, cr);
1740                         if (error) {
1741                                 avl_index_t where;
1742
1743                                 /*
1744                                  * Before reinserting snapshot to the tree,
1745                                  * check if it was actually removed. For example
1746                                  * when snapshot mount point is busy, we will
1747                                  * have an error here, but there will be no need
1748                                  * to reinsert snapshot.
1749                                  */
1750                                 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1751                                         avl_insert(&sdp->sd_snaps, sep, where);
1752                                 break;
1753                         }
1754                 }
1755                 sep = next;
1756         }
1757
1758         mutex_exit(&sdp->sd_lock);
1759         VN_RELE(dvp);
1760
1761         return (error);
1762 }