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