xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_ctldir.c (revision 7f0b8309074a5d8e9f9d8ffe7aad7bb0b1ee6b1f)
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 <fs/fs_subr.h>
68 #include <sys/zfs_ctldir.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/zfs_vfsops.h>
71 #include <sys/vfs_opreg.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 vnodeops_t *zfsctl_ops_root;
115 vnodeops_t *zfsctl_ops_snapdir;
116 vnodeops_t *zfsctl_ops_snapshot;
117 vnodeops_t *zfsctl_ops_shares;
118 vnodeops_t *zfsctl_ops_shares_dir;
119 
120 static const fs_operation_def_t zfsctl_tops_root[];
121 static const fs_operation_def_t zfsctl_tops_snapdir[];
122 static const fs_operation_def_t zfsctl_tops_snapshot[];
123 static const fs_operation_def_t zfsctl_tops_shares[];
124 
125 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
126 static vnode_t *zfsctl_mknode_shares(vnode_t *);
127 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
128 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
129 
130 static gfs_opsvec_t zfsctl_opsvec[] = {
131 	{ ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
132 	{ ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
133 	{ ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
134 	{ ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
135 	{ ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
136 	{ NULL }
137 };
138 
139 /*
140  * Root directory elements.  We only have two entries
141  * snapshot and shares.
142  */
143 static gfs_dirent_t zfsctl_root_entries[] = {
144 	{ "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
145 	{ "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
146 	{ NULL }
147 };
148 
149 /* include . and .. in the calculation */
150 #define	NROOT_ENTRIES	((sizeof (zfsctl_root_entries) / \
151     sizeof (gfs_dirent_t)) + 1)
152 
153 
154 /*
155  * Initialize the various GFS pieces we'll need to create and manipulate .zfs
156  * directories.  This is called from the ZFS init routine, and initializes the
157  * vnode ops vectors that we'll be using.
158  */
159 void
160 zfsctl_init(void)
161 {
162 	VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
163 }
164 
165 void
166 zfsctl_fini(void)
167 {
168 	/*
169 	 * Remove vfsctl vnode ops
170 	 */
171 	if (zfsctl_ops_root)
172 		vn_freevnodeops(zfsctl_ops_root);
173 	if (zfsctl_ops_snapdir)
174 		vn_freevnodeops(zfsctl_ops_snapdir);
175 	if (zfsctl_ops_snapshot)
176 		vn_freevnodeops(zfsctl_ops_snapshot);
177 	if (zfsctl_ops_shares)
178 		vn_freevnodeops(zfsctl_ops_shares);
179 	if (zfsctl_ops_shares_dir)
180 		vn_freevnodeops(zfsctl_ops_shares_dir);
181 
182 	zfsctl_ops_root = NULL;
183 	zfsctl_ops_snapdir = NULL;
184 	zfsctl_ops_snapshot = NULL;
185 	zfsctl_ops_shares = NULL;
186 	zfsctl_ops_shares_dir = NULL;
187 }
188 
189 /*
190  * Return the inode number associated with the 'snapshot' or
191  * 'shares' directory.
192  */
193 /* ARGSUSED */
194 static ino64_t
195 zfsctl_root_inode_cb(vnode_t *vp, int index)
196 {
197 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
198 
199 	ASSERT(index <= 2);
200 
201 	if (index == 0)
202 		return (ZFSCTL_INO_SNAPDIR);
203 
204 	return (zfsvfs->z_shares_dir);
205 }
206 
207 /*
208  * Create the '.zfs' directory.  This directory is cached as part of the VFS
209  * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
210  * therefore checks against a vfs_count of 2 instead of 1.  This reference
211  * is removed when the ctldir is destroyed in the unmount.
212  */
213 void
214 zfsctl_create(zfsvfs_t *zfsvfs)
215 {
216 	vnode_t *vp, *rvp;
217 	zfsctl_node_t *zcp;
218 
219 	ASSERT(zfsvfs->z_ctldir == NULL);
220 
221 	vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
222 	    zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
223 	    zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
224 	zcp = vp->v_data;
225 	zcp->zc_id = ZFSCTL_INO_ROOT;
226 
227 	VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0);
228 	ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
229 	VN_RELE(rvp);
230 
231 	/*
232 	 * We're only faking the fact that we have a root of a filesystem for
233 	 * the sake of the GFS interfaces.  Undo the flag manipulation it did
234 	 * for us.
235 	 */
236 	vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
237 
238 	zfsvfs->z_ctldir = vp;
239 }
240 
241 /*
242  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
243  * There might still be more references if we were force unmounted, but only
244  * new zfs_inactive() calls can occur and they don't reference .zfs
245  */
246 void
247 zfsctl_destroy(zfsvfs_t *zfsvfs)
248 {
249 	VN_RELE(zfsvfs->z_ctldir);
250 	zfsvfs->z_ctldir = NULL;
251 }
252 
253 /*
254  * Given a root znode, retrieve the associated .zfs directory.
255  * Add a hold to the vnode and return it.
256  */
257 vnode_t *
258 zfsctl_root(znode_t *zp)
259 {
260 	ASSERT(zfs_has_ctldir(zp));
261 	VN_HOLD(zp->z_zfsvfs->z_ctldir);
262 	return (zp->z_zfsvfs->z_ctldir);
263 }
264 
265 /*
266  * Common open routine.  Disallow any write access.
267  */
268 /* ARGSUSED */
269 static int
270 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
271 {
272 	if (flags & FWRITE)
273 		return (EACCES);
274 
275 	return (0);
276 }
277 
278 /*
279  * Common close routine.  Nothing to do here.
280  */
281 /* ARGSUSED */
282 static int
283 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
284     cred_t *cr, caller_context_t *ct)
285 {
286 	return (0);
287 }
288 
289 /*
290  * Common access routine.  Disallow writes.
291  */
292 /* ARGSUSED */
293 static int
294 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr,
295     caller_context_t *ct)
296 {
297 	if (flags & V_ACE_MASK) {
298 		if (mode & ACE_ALL_WRITE_PERMS)
299 			return (EACCES);
300 	} else {
301 		if (mode & VWRITE)
302 			return (EACCES);
303 	}
304 
305 	return (0);
306 }
307 
308 /*
309  * Common getattr function.  Fill in basic information.
310  */
311 static void
312 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
313 {
314 	zfsctl_node_t	*zcp = vp->v_data;
315 	timestruc_t	now;
316 
317 	vap->va_uid = 0;
318 	vap->va_gid = 0;
319 	vap->va_rdev = 0;
320 	/*
321 	 * We are a purly virtual object, so we have no
322 	 * blocksize or allocated blocks.
323 	 */
324 	vap->va_blksize = 0;
325 	vap->va_nblocks = 0;
326 	vap->va_seq = 0;
327 	vap->va_fsid = vp->v_vfsp->vfs_dev;
328 	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
329 	    S_IROTH | S_IXOTH;
330 	vap->va_type = VDIR;
331 	/*
332 	 * We live in the now (for atime).
333 	 */
334 	gethrestime(&now);
335 	vap->va_atime = now;
336 	vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
337 }
338 
339 /*ARGSUSED*/
340 static int
341 zfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
342 {
343 	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
344 	zfsctl_node_t	*zcp = vp->v_data;
345 	uint64_t	object = zcp->zc_id;
346 	zfid_short_t	*zfid;
347 	int		i;
348 
349 	ZFS_ENTER(zfsvfs);
350 
351 	if (fidp->fid_len < SHORT_FID_LEN) {
352 		fidp->fid_len = SHORT_FID_LEN;
353 		ZFS_EXIT(zfsvfs);
354 		return (ENOSPC);
355 	}
356 
357 	zfid = (zfid_short_t *)fidp;
358 
359 	zfid->zf_len = SHORT_FID_LEN;
360 
361 	for (i = 0; i < sizeof (zfid->zf_object); i++)
362 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
363 
364 	/* .zfs znodes always have a generation number of 0 */
365 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
366 		zfid->zf_gen[i] = 0;
367 
368 	ZFS_EXIT(zfsvfs);
369 	return (0);
370 }
371 
372 
373 /*ARGSUSED*/
374 static int
375 zfsctl_shares_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
376 {
377 	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
378 	znode_t		*dzp;
379 	int		error;
380 
381 	ZFS_ENTER(zfsvfs);
382 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
383 		error = VOP_FID(ZTOV(dzp), fidp, ct);
384 		VN_RELE(ZTOV(dzp));
385 	}
386 
387 	ZFS_EXIT(zfsvfs);
388 	return (error);
389 }
390 /*
391  * .zfs inode namespace
392  *
393  * We need to generate unique inode numbers for all files and directories
394  * within the .zfs pseudo-filesystem.  We use the following scheme:
395  *
396  * 	ENTRY			ZFSCTL_INODE
397  * 	.zfs			1
398  * 	.zfs/snapshot		2
399  * 	.zfs/snapshot/<snap>	objectid(snap)
400  */
401 
402 #define	ZFSCTL_INO_SNAP(id)	(id)
403 
404 /*
405  * Get root directory attributes.
406  */
407 /* ARGSUSED */
408 static int
409 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
410     caller_context_t *ct)
411 {
412 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
413 
414 	ZFS_ENTER(zfsvfs);
415 	vap->va_nodeid = ZFSCTL_INO_ROOT;
416 	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
417 
418 	zfsctl_common_getattr(vp, vap);
419 	ZFS_EXIT(zfsvfs);
420 
421 	return (0);
422 }
423 
424 /*
425  * Special case the handling of "..".
426  */
427 /* ARGSUSED */
428 int
429 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
430     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
431     int *direntflags, pathname_t *realpnp)
432 {
433 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
434 	int err;
435 
436 	/*
437 	 * No extended attributes allowed under .zfs
438 	 */
439 	if (flags & LOOKUP_XATTR)
440 		return (EINVAL);
441 
442 	ZFS_ENTER(zfsvfs);
443 
444 	if (strcmp(nm, "..") == 0) {
445 		err = VFS_ROOT(dvp->v_vfsp, vpp);
446 	} else {
447 		err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
448 		    cr, ct, direntflags, realpnp);
449 	}
450 
451 	ZFS_EXIT(zfsvfs);
452 
453 	return (err);
454 }
455 
456 static int
457 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
458     caller_context_t *ct)
459 {
460 	/*
461 	 * We only care about ACL_ENABLED so that libsec can
462 	 * display ACL correctly and not default to POSIX draft.
463 	 */
464 	if (cmd == _PC_ACL_ENABLED) {
465 		*valp = _ACL_ACE_ENABLED;
466 		return (0);
467 	}
468 
469 	return (fs_pathconf(vp, cmd, valp, cr, ct));
470 }
471 
472 static const fs_operation_def_t zfsctl_tops_root[] = {
473 	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
474 	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
475 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
476 	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_root_getattr }	},
477 	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
478 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } 	},
479 	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_root_lookup }	},
480 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
481 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive }	},
482 	{ VOPNAME_PATHCONF,	{ .vop_pathconf = zfsctl_pathconf }	},
483 	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid	}	},
484 	{ NULL }
485 };
486 
487 static int
488 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
489 {
490 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
491 
492 	if (snapshot_namecheck(name, NULL, NULL) != 0)
493 		return (EILSEQ);
494 	dmu_objset_name(os, zname);
495 	if (strlen(zname) + 1 + strlen(name) >= len)
496 		return (ENAMETOOLONG);
497 	(void) strcat(zname, "@");
498 	(void) strcat(zname, name);
499 	return (0);
500 }
501 
502 static int
503 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
504 {
505 	vnode_t *svp = sep->se_root;
506 	int error;
507 
508 	ASSERT(vn_ismntpt(svp));
509 
510 	/* this will be dropped by dounmount() */
511 	if ((error = vn_vfswlock(svp)) != 0)
512 		return (error);
513 
514 	VN_HOLD(svp);
515 	error = dounmount(vn_mountedvfs(svp), fflags, cr);
516 	if (error) {
517 		VN_RELE(svp);
518 		return (error);
519 	}
520 	VFS_RELE(svp->v_vfsp);
521 	/*
522 	 * We can't use VN_RELE(), as that will try to invoke
523 	 * zfsctl_snapdir_inactive(), which would cause us to destroy
524 	 * the sd_lock mutex held by our caller.
525 	 */
526 	ASSERT(svp->v_count == 1);
527 	gfs_vop_inactive(svp, cr, NULL);
528 
529 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
530 	kmem_free(sep, sizeof (zfs_snapentry_t));
531 
532 	return (0);
533 }
534 
535 static void
536 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
537 {
538 	avl_index_t where;
539 	vfs_t *vfsp;
540 	refstr_t *pathref;
541 	char newpath[MAXNAMELEN];
542 	char *tail;
543 
544 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
545 	ASSERT(sep != NULL);
546 
547 	vfsp = vn_mountedvfs(sep->se_root);
548 	ASSERT(vfsp != NULL);
549 
550 	vfs_lock_wait(vfsp);
551 
552 	/*
553 	 * Change the name in the AVL tree.
554 	 */
555 	avl_remove(&sdp->sd_snaps, sep);
556 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
557 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
558 	(void) strcpy(sep->se_name, nm);
559 	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
560 	avl_insert(&sdp->sd_snaps, sep, where);
561 
562 	/*
563 	 * Change the current mountpoint info:
564 	 * 	- update the tail of the mntpoint path
565 	 *	- update the tail of the resource path
566 	 */
567 	pathref = vfs_getmntpoint(vfsp);
568 	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
569 	VERIFY((tail = strrchr(newpath, '/')) != NULL);
570 	*(tail+1) = '\0';
571 	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
572 	(void) strcat(newpath, nm);
573 	refstr_rele(pathref);
574 	vfs_setmntpoint(vfsp, newpath);
575 
576 	pathref = vfs_getresource(vfsp);
577 	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
578 	VERIFY((tail = strrchr(newpath, '@')) != NULL);
579 	*(tail+1) = '\0';
580 	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
581 	(void) strcat(newpath, nm);
582 	refstr_rele(pathref);
583 	vfs_setresource(vfsp, newpath);
584 
585 	vfs_unlock(vfsp);
586 }
587 
588 /*ARGSUSED*/
589 static int
590 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
591     cred_t *cr, caller_context_t *ct, int flags)
592 {
593 	zfsctl_snapdir_t *sdp = sdvp->v_data;
594 	zfs_snapentry_t search, *sep;
595 	zfsvfs_t *zfsvfs;
596 	avl_index_t where;
597 	char from[MAXNAMELEN], to[MAXNAMELEN];
598 	char real[MAXNAMELEN];
599 	int err;
600 
601 	zfsvfs = sdvp->v_vfsp->vfs_data;
602 	ZFS_ENTER(zfsvfs);
603 
604 	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
605 		err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
606 		    MAXNAMELEN, NULL);
607 		if (err == 0) {
608 			snm = real;
609 		} else if (err != ENOTSUP) {
610 			ZFS_EXIT(zfsvfs);
611 			return (err);
612 		}
613 	}
614 
615 	ZFS_EXIT(zfsvfs);
616 
617 	err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
618 	if (!err)
619 		err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
620 	if (!err)
621 		err = zfs_secpolicy_rename_perms(from, to, cr);
622 	if (err)
623 		return (err);
624 
625 	/*
626 	 * Cannot move snapshots out of the snapdir.
627 	 */
628 	if (sdvp != tdvp)
629 		return (EINVAL);
630 
631 	if (strcmp(snm, tnm) == 0)
632 		return (0);
633 
634 	mutex_enter(&sdp->sd_lock);
635 
636 	search.se_name = (char *)snm;
637 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
638 		mutex_exit(&sdp->sd_lock);
639 		return (ENOENT);
640 	}
641 
642 	err = dmu_objset_rename(from, to, B_FALSE);
643 	if (err == 0)
644 		zfsctl_rename_snap(sdp, sep, tnm);
645 
646 	mutex_exit(&sdp->sd_lock);
647 
648 	return (err);
649 }
650 
651 /* ARGSUSED */
652 static int
653 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
654     caller_context_t *ct, int flags)
655 {
656 	zfsctl_snapdir_t *sdp = dvp->v_data;
657 	zfs_snapentry_t *sep;
658 	zfs_snapentry_t search;
659 	zfsvfs_t *zfsvfs;
660 	char snapname[MAXNAMELEN];
661 	char real[MAXNAMELEN];
662 	int err;
663 
664 	zfsvfs = dvp->v_vfsp->vfs_data;
665 	ZFS_ENTER(zfsvfs);
666 
667 	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
668 
669 		err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
670 		    MAXNAMELEN, NULL);
671 		if (err == 0) {
672 			name = real;
673 		} else if (err != ENOTSUP) {
674 			ZFS_EXIT(zfsvfs);
675 			return (err);
676 		}
677 	}
678 
679 	ZFS_EXIT(zfsvfs);
680 
681 	err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
682 	if (!err)
683 		err = zfs_secpolicy_destroy_perms(snapname, cr);
684 	if (err)
685 		return (err);
686 
687 	mutex_enter(&sdp->sd_lock);
688 
689 	search.se_name = name;
690 	sep = avl_find(&sdp->sd_snaps, &search, NULL);
691 	if (sep) {
692 		avl_remove(&sdp->sd_snaps, sep);
693 		err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
694 		if (err)
695 			avl_add(&sdp->sd_snaps, sep);
696 		else
697 			err = dmu_objset_destroy(snapname);
698 	} else {
699 		err = ENOENT;
700 	}
701 
702 	mutex_exit(&sdp->sd_lock);
703 
704 	return (err);
705 }
706 
707 /*
708  * This creates a snapshot under '.zfs/snapshot'.
709  */
710 /* ARGSUSED */
711 static int
712 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
713     cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
714 {
715 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
716 	char name[MAXNAMELEN];
717 	int err;
718 	static enum symfollow follow = NO_FOLLOW;
719 	static enum uio_seg seg = UIO_SYSSPACE;
720 
721 	if (snapshot_namecheck(dirname, NULL, NULL) != 0)
722 		return (EILSEQ);
723 
724 	dmu_objset_name(zfsvfs->z_os, name);
725 
726 	*vpp = NULL;
727 
728 	err = zfs_secpolicy_snapshot_perms(name, cr);
729 	if (err)
730 		return (err);
731 
732 	if (err == 0) {
733 		err = dmu_objset_snapshot(name, dirname, B_FALSE);
734 		if (err)
735 			return (err);
736 		err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
737 	}
738 
739 	return (err);
740 }
741 
742 /*
743  * Lookup entry point for the 'snapshot' directory.  Try to open the
744  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
745  * Perform a mount of the associated dataset on top of the vnode.
746  */
747 /* ARGSUSED */
748 static int
749 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
750     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
751     int *direntflags, pathname_t *realpnp)
752 {
753 	zfsctl_snapdir_t *sdp = dvp->v_data;
754 	objset_t *snap;
755 	char snapname[MAXNAMELEN];
756 	char real[MAXNAMELEN];
757 	char *mountpoint;
758 	zfs_snapentry_t *sep, search;
759 	struct mounta margs;
760 	vfs_t *vfsp;
761 	size_t mountpoint_len;
762 	avl_index_t where;
763 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
764 	int err;
765 
766 	/*
767 	 * No extended attributes allowed under .zfs
768 	 */
769 	if (flags & LOOKUP_XATTR)
770 		return (EINVAL);
771 
772 	ASSERT(dvp->v_type == VDIR);
773 
774 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
775 		return (0);
776 
777 	/*
778 	 * If we get a recursive call, that means we got called
779 	 * from the domount() code while it was trying to look up the
780 	 * spec (which looks like a local path for zfs).  We need to
781 	 * add some flag to domount() to tell it not to do this lookup.
782 	 */
783 	if (MUTEX_HELD(&sdp->sd_lock))
784 		return (ENOENT);
785 
786 	ZFS_ENTER(zfsvfs);
787 
788 	if (flags & FIGNORECASE) {
789 		boolean_t conflict = B_FALSE;
790 
791 		err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
792 		    MAXNAMELEN, &conflict);
793 		if (err == 0) {
794 			nm = real;
795 		} else if (err != ENOTSUP) {
796 			ZFS_EXIT(zfsvfs);
797 			return (err);
798 		}
799 		if (realpnp)
800 			(void) strlcpy(realpnp->pn_buf, nm,
801 			    realpnp->pn_bufsize);
802 		if (conflict && direntflags)
803 			*direntflags = ED_CASE_CONFLICT;
804 	}
805 
806 	mutex_enter(&sdp->sd_lock);
807 	search.se_name = (char *)nm;
808 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
809 		*vpp = sep->se_root;
810 		VN_HOLD(*vpp);
811 		err = traverse(vpp);
812 		if (err) {
813 			VN_RELE(*vpp);
814 			*vpp = NULL;
815 		} else if (*vpp == sep->se_root) {
816 			/*
817 			 * The snapshot was unmounted behind our backs,
818 			 * try to remount it.
819 			 */
820 			goto domount;
821 		} else {
822 			/*
823 			 * VROOT was set during the traverse call.  We need
824 			 * to clear it since we're pretending to be part
825 			 * of our parent's vfs.
826 			 */
827 			(*vpp)->v_flag &= ~VROOT;
828 		}
829 		mutex_exit(&sdp->sd_lock);
830 		ZFS_EXIT(zfsvfs);
831 		return (err);
832 	}
833 
834 	/*
835 	 * The requested snapshot is not currently mounted, look it up.
836 	 */
837 	err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
838 	if (err) {
839 		mutex_exit(&sdp->sd_lock);
840 		ZFS_EXIT(zfsvfs);
841 		/*
842 		 * handle "ls *" or "?" in a graceful manner,
843 		 * forcing EILSEQ to ENOENT.
844 		 * Since shell ultimately passes "*" or "?" as name to lookup
845 		 */
846 		return (err == EILSEQ ? ENOENT : err);
847 	}
848 	if (dmu_objset_open(snapname, DMU_OST_ZFS,
849 	    DS_MODE_USER | DS_MODE_READONLY, &snap) != 0) {
850 		mutex_exit(&sdp->sd_lock);
851 		ZFS_EXIT(zfsvfs);
852 		return (ENOENT);
853 	}
854 
855 	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
856 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
857 	(void) strcpy(sep->se_name, nm);
858 	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
859 	avl_insert(&sdp->sd_snaps, sep, where);
860 
861 	dmu_objset_close(snap);
862 domount:
863 	mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
864 	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
865 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
866 	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
867 	    refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
868 
869 	margs.spec = snapname;
870 	margs.dir = mountpoint;
871 	margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
872 	margs.fstype = "zfs";
873 	margs.dataptr = NULL;
874 	margs.datalen = 0;
875 	margs.optptr = NULL;
876 	margs.optlen = 0;
877 
878 	err = domount("zfs", &margs, *vpp, kcred, &vfsp);
879 	kmem_free(mountpoint, mountpoint_len);
880 
881 	if (err == 0) {
882 		/*
883 		 * Return the mounted root rather than the covered mount point.
884 		 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
885 		 * the ZFS vnode mounted on top of the GFS node.  This ZFS
886 		 * vnode is the root the newly created vfsp.
887 		 */
888 		VFS_RELE(vfsp);
889 		err = traverse(vpp);
890 	}
891 
892 	if (err == 0) {
893 		/*
894 		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
895 		 *
896 		 * This is where we lie about our v_vfsp in order to
897 		 * make .zfs/snapshot/<snapname> accessible over NFS
898 		 * without requiring manual mounts of <snapname>.
899 		 */
900 		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
901 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
902 		(*vpp)->v_vfsp = zfsvfs->z_vfs;
903 		(*vpp)->v_flag &= ~VROOT;
904 	}
905 	mutex_exit(&sdp->sd_lock);
906 	ZFS_EXIT(zfsvfs);
907 
908 	/*
909 	 * If we had an error, drop our hold on the vnode and
910 	 * zfsctl_snapshot_inactive() will clean up.
911 	 */
912 	if (err) {
913 		VN_RELE(*vpp);
914 		*vpp = NULL;
915 	}
916 	return (err);
917 }
918 
919 /* ARGSUSED */
920 static int
921 zfsctl_shares_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
922     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
923     int *direntflags, pathname_t *realpnp)
924 {
925 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
926 	znode_t *dzp;
927 	int error;
928 
929 	ZFS_ENTER(zfsvfs);
930 
931 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
932 		ZFS_EXIT(zfsvfs);
933 		return (0);
934 	}
935 
936 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
937 		error = VOP_LOOKUP(ZTOV(dzp), nm, vpp, pnp,
938 		    flags, rdir, cr, ct, direntflags, realpnp);
939 
940 	VN_RELE(ZTOV(dzp));
941 	ZFS_EXIT(zfsvfs);
942 
943 	return (error);
944 }
945 
946 /* ARGSUSED */
947 static int
948 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
949     offset_t *offp, offset_t *nextp, void *data, int flags)
950 {
951 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
952 	char snapname[MAXNAMELEN];
953 	uint64_t id, cookie;
954 	boolean_t case_conflict;
955 	int error;
956 
957 	ZFS_ENTER(zfsvfs);
958 
959 	cookie = *offp;
960 	error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
961 	    &cookie, &case_conflict);
962 	if (error) {
963 		ZFS_EXIT(zfsvfs);
964 		if (error == ENOENT) {
965 			*eofp = 1;
966 			return (0);
967 		}
968 		return (error);
969 	}
970 
971 	if (flags & V_RDDIR_ENTFLAGS) {
972 		edirent_t *eodp = dp;
973 
974 		(void) strcpy(eodp->ed_name, snapname);
975 		eodp->ed_ino = ZFSCTL_INO_SNAP(id);
976 		eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
977 	} else {
978 		struct dirent64 *odp = dp;
979 
980 		(void) strcpy(odp->d_name, snapname);
981 		odp->d_ino = ZFSCTL_INO_SNAP(id);
982 	}
983 	*nextp = cookie;
984 
985 	ZFS_EXIT(zfsvfs);
986 
987 	return (0);
988 }
989 
990 /* ARGSUSED */
991 static int
992 zfsctl_shares_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
993     caller_context_t *ct, int flags)
994 {
995 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
996 	znode_t *dzp;
997 	int error;
998 
999 	ZFS_ENTER(zfsvfs);
1000 
1001 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1002 		error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ct, flags);
1003 		VN_RELE(ZTOV(dzp));
1004 	} else {
1005 		*eofp = 1;
1006 		error = ENOENT;
1007 	}
1008 
1009 	ZFS_EXIT(zfsvfs);
1010 	return (error);
1011 }
1012 
1013 /*
1014  * pvp is the '.zfs' directory (zfsctl_node_t).
1015  * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1016  *
1017  * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1018  * when a lookup is performed on .zfs for "snapshot".
1019  */
1020 vnode_t *
1021 zfsctl_mknode_snapdir(vnode_t *pvp)
1022 {
1023 	vnode_t *vp;
1024 	zfsctl_snapdir_t *sdp;
1025 
1026 	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
1027 	    zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1028 	    zfsctl_snapdir_readdir_cb, NULL);
1029 	sdp = vp->v_data;
1030 	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1031 	sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1032 	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1033 	avl_create(&sdp->sd_snaps, snapentry_compare,
1034 	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1035 	return (vp);
1036 }
1037 
1038 vnode_t *
1039 zfsctl_mknode_shares(vnode_t *pvp)
1040 {
1041 	vnode_t *vp;
1042 	zfsctl_node_t *sdp;
1043 
1044 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1045 	    zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1046 	    NULL, NULL);
1047 	sdp = vp->v_data;
1048 	sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1049 	return (vp);
1050 
1051 }
1052 
1053 /* ARGSUSED */
1054 static int
1055 zfsctl_shares_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1056     caller_context_t *ct)
1057 {
1058 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1059 	znode_t *dzp;
1060 	int error;
1061 
1062 	ZFS_ENTER(zfsvfs);
1063 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1064 		error = VOP_GETATTR(ZTOV(dzp), vap, flags, cr, ct);
1065 		VN_RELE(ZTOV(dzp));
1066 	}
1067 	ZFS_EXIT(zfsvfs);
1068 	return (error);
1069 
1070 
1071 }
1072 
1073 /* ARGSUSED */
1074 static int
1075 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1076     caller_context_t *ct)
1077 {
1078 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1079 	zfsctl_snapdir_t *sdp = vp->v_data;
1080 
1081 	ZFS_ENTER(zfsvfs);
1082 	zfsctl_common_getattr(vp, vap);
1083 	vap->va_nodeid = gfs_file_inode(vp);
1084 	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1085 	ZFS_EXIT(zfsvfs);
1086 
1087 	return (0);
1088 }
1089 
1090 /* ARGSUSED */
1091 static void
1092 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1093 {
1094 	zfsctl_snapdir_t *sdp = vp->v_data;
1095 	void *private;
1096 
1097 	private = gfs_dir_inactive(vp);
1098 	if (private != NULL) {
1099 		ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1100 		mutex_destroy(&sdp->sd_lock);
1101 		avl_destroy(&sdp->sd_snaps);
1102 		kmem_free(private, sizeof (zfsctl_snapdir_t));
1103 	}
1104 }
1105 
1106 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1107 	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1108 	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1109 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1110 	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_snapdir_getattr } },
1111 	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1112 	{ VOPNAME_RENAME,	{ .vop_rename = zfsctl_snapdir_rename }	},
1113 	{ VOPNAME_RMDIR,	{ .vop_rmdir = zfsctl_snapdir_remove }	},
1114 	{ VOPNAME_MKDIR,	{ .vop_mkdir = zfsctl_snapdir_mkdir }	},
1115 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir }	},
1116 	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_snapdir_lookup }	},
1117 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1118 	{ VOPNAME_INACTIVE,	{ .vop_inactive = zfsctl_snapdir_inactive } },
1119 	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid }	},
1120 	{ NULL }
1121 };
1122 
1123 static const fs_operation_def_t zfsctl_tops_shares[] = {
1124 	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1125 	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1126 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1127 	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_shares_getattr } },
1128 	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1129 	{ VOPNAME_READDIR,	{ .vop_readdir = zfsctl_shares_readdir } },
1130 	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_shares_lookup }	},
1131 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1132 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1133 	{ VOPNAME_FID,		{ .vop_fid = zfsctl_shares_fid } },
1134 	{ NULL }
1135 };
1136 
1137 /*
1138  * pvp is the GFS vnode '.zfs/snapshot'.
1139  *
1140  * This creates a GFS node under '.zfs/snapshot' representing each
1141  * snapshot.  This newly created GFS node is what we mount snapshot
1142  * vfs_t's ontop of.
1143  */
1144 static vnode_t *
1145 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1146 {
1147 	vnode_t *vp;
1148 	zfsctl_node_t *zcp;
1149 
1150 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1151 	    zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1152 	zcp = vp->v_data;
1153 	zcp->zc_id = objset;
1154 	VFS_HOLD(vp->v_vfsp);
1155 
1156 	return (vp);
1157 }
1158 
1159 static void
1160 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1161 {
1162 	zfsctl_snapdir_t *sdp;
1163 	zfs_snapentry_t *sep, *next;
1164 	vnode_t *dvp;
1165 
1166 	VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1167 	sdp = dvp->v_data;
1168 
1169 	mutex_enter(&sdp->sd_lock);
1170 
1171 	if (vp->v_count > 1) {
1172 		mutex_exit(&sdp->sd_lock);
1173 		return;
1174 	}
1175 	ASSERT(!vn_ismntpt(vp));
1176 
1177 	sep = avl_first(&sdp->sd_snaps);
1178 	while (sep != NULL) {
1179 		next = AVL_NEXT(&sdp->sd_snaps, sep);
1180 
1181 		if (sep->se_root == vp) {
1182 			avl_remove(&sdp->sd_snaps, sep);
1183 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1184 			kmem_free(sep, sizeof (zfs_snapentry_t));
1185 			break;
1186 		}
1187 		sep = next;
1188 	}
1189 	ASSERT(sep != NULL);
1190 
1191 	mutex_exit(&sdp->sd_lock);
1192 	VN_RELE(dvp);
1193 	VFS_RELE(vp->v_vfsp);
1194 
1195 	/*
1196 	 * Dispose of the vnode for the snapshot mount point.
1197 	 * This is safe to do because once this entry has been removed
1198 	 * from the AVL tree, it can't be found again, so cannot become
1199 	 * "active".  If we lookup the same name again we will end up
1200 	 * creating a new vnode.
1201 	 */
1202 	gfs_vop_inactive(vp, cr, ct);
1203 }
1204 
1205 
1206 /*
1207  * These VP's should never see the light of day.  They should always
1208  * be covered.
1209  */
1210 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
1211 	VOPNAME_INACTIVE, { .vop_inactive =  zfsctl_snapshot_inactive },
1212 	NULL, NULL
1213 };
1214 
1215 int
1216 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1217 {
1218 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1219 	vnode_t *dvp, *vp;
1220 	zfsctl_snapdir_t *sdp;
1221 	zfsctl_node_t *zcp;
1222 	zfs_snapentry_t *sep;
1223 	int error;
1224 
1225 	ASSERT(zfsvfs->z_ctldir != NULL);
1226 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1227 	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1228 	if (error != 0)
1229 		return (error);
1230 	sdp = dvp->v_data;
1231 
1232 	mutex_enter(&sdp->sd_lock);
1233 	sep = avl_first(&sdp->sd_snaps);
1234 	while (sep != NULL) {
1235 		vp = sep->se_root;
1236 		zcp = vp->v_data;
1237 		if (zcp->zc_id == objsetid)
1238 			break;
1239 
1240 		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1241 	}
1242 
1243 	if (sep != NULL) {
1244 		VN_HOLD(vp);
1245 		/*
1246 		 * Return the mounted root rather than the covered mount point.
1247 		 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1248 		 * and returns the ZFS vnode mounted on top of the GFS node.
1249 		 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1250 		 */
1251 		error = traverse(&vp);
1252 		if (error == 0) {
1253 			if (vp == sep->se_root)
1254 				error = EINVAL;
1255 			else
1256 				*zfsvfsp = VTOZ(vp)->z_zfsvfs;
1257 		}
1258 		mutex_exit(&sdp->sd_lock);
1259 		VN_RELE(vp);
1260 	} else {
1261 		error = EINVAL;
1262 		mutex_exit(&sdp->sd_lock);
1263 	}
1264 
1265 	VN_RELE(dvp);
1266 
1267 	return (error);
1268 }
1269 
1270 /*
1271  * Unmount any snapshots for the given filesystem.  This is called from
1272  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1273  * snapshots.
1274  */
1275 int
1276 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1277 {
1278 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1279 	vnode_t *dvp;
1280 	zfsctl_snapdir_t *sdp;
1281 	zfs_snapentry_t *sep, *next;
1282 	int error;
1283 
1284 	ASSERT(zfsvfs->z_ctldir != NULL);
1285 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1286 	    NULL, 0, NULL, cr, NULL, NULL, NULL);
1287 	if (error != 0)
1288 		return (error);
1289 	sdp = dvp->v_data;
1290 
1291 	mutex_enter(&sdp->sd_lock);
1292 
1293 	sep = avl_first(&sdp->sd_snaps);
1294 	while (sep != NULL) {
1295 		next = AVL_NEXT(&sdp->sd_snaps, sep);
1296 
1297 		/*
1298 		 * If this snapshot is not mounted, then it must
1299 		 * have just been unmounted by somebody else, and
1300 		 * will be cleaned up by zfsctl_snapdir_inactive().
1301 		 */
1302 		if (vn_ismntpt(sep->se_root)) {
1303 			avl_remove(&sdp->sd_snaps, sep);
1304 			error = zfsctl_unmount_snap(sep, fflags, cr);
1305 			if (error) {
1306 				avl_add(&sdp->sd_snaps, sep);
1307 				break;
1308 			}
1309 		}
1310 		sep = next;
1311 	}
1312 
1313 	mutex_exit(&sdp->sd_lock);
1314 	VN_RELE(dvp);
1315 
1316 	return (error);
1317 }
1318