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