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