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