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