xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_ctldir.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * ZFS control directory (a.k.a. ".zfs")
31  *
32  * This directory provides a common location for all ZFS meta-objects.
33  * Currently, this is only the 'snapshot' directory, but this may expand in the
34  * future.  The elements are built using the GFS primitives, as the hierarchy
35  * does not actually exist on disk.
36  *
37  * For 'snapshot', we don't want to have all snapshots always mounted, because
38  * this would take up a huge amount of space in /etc/mnttab.  We have three
39  * types of objects:
40  *
41  * 	ctldir ------> snapshotdir -------> snapshot
42  *                                             |
43  *                                             |
44  *                                             V
45  *                                         mounted fs
46  *
47  * The 'snapshot' node contains just enough information to lookup '..' and act
48  * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
49  * perform an automount of the underlying filesystem and return the
50  * corresponding vnode.
51  *
52  * All mounts are handled automatically by the kernel, but unmounts are
53  * (currently) handled from user land.  The main reason is that there is no
54  * reliable way to auto-unmount the filesystem when it's "no longer in use".
55  * When the user unmounts a filesystem, we call zfsctl_unmount(), which
56  * unmounts any snapshots within the snapshot directory.
57  */
58 
59 #include <fs/fs_subr.h>
60 #include <sys/zfs_ctldir.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/zfs_vfsops.h>
63 #include <sys/gfs.h>
64 #include <sys/stat.h>
65 #include <sys/dmu.h>
66 #include <sys/mount.h>
67 
68 typedef struct {
69 	char		*se_name;
70 	vnode_t		*se_root;
71 	avl_node_t	se_node;
72 } zfs_snapentry_t;
73 
74 static int
75 snapentry_compare(const void *a, const void *b)
76 {
77 	const zfs_snapentry_t *sa = a;
78 	const zfs_snapentry_t *sb = b;
79 	int ret = strcmp(sa->se_name, sb->se_name);
80 
81 	if (ret < 0)
82 		return (-1);
83 	else if (ret > 0)
84 		return (1);
85 	else
86 		return (0);
87 }
88 
89 vnodeops_t *zfsctl_ops_root;
90 vnodeops_t *zfsctl_ops_snapdir;
91 vnodeops_t *zfsctl_ops_snapshot;
92 
93 static const fs_operation_def_t zfsctl_tops_root[];
94 static const fs_operation_def_t zfsctl_tops_snapdir[];
95 static const fs_operation_def_t zfsctl_tops_snapshot[];
96 
97 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
98 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
99 
100 static gfs_opsvec_t zfsctl_opsvec[] = {
101 	{ ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
102 	{ ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
103 	{ ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
104 	{ NULL }
105 };
106 
107 typedef struct zfsctl_node {
108 	gfs_dir_t	zc_gfs_private;
109 	uint64_t	zc_id;
110 } zfsctl_node_t;
111 
112 typedef struct zfsctl_snapdir {
113 	zfsctl_node_t	sd_node;
114 	kmutex_t	sd_lock;
115 	avl_tree_t	sd_snaps;
116 } zfsctl_snapdir_t;
117 
118 /*
119  * Root directory elements.  We have only a single static entry, 'snapshot'.
120  */
121 static gfs_dirent_t zfsctl_root_entries[] = {
122 	{ "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
123 	{ NULL }
124 };
125 
126 /* include . and .. in the calculation */
127 #define	NROOT_ENTRIES	((sizeof (zfsctl_root_entries) / \
128     sizeof (gfs_dirent_t)) + 1)
129 
130 
131 /*
132  * Initialize the various GFS pieces we'll need to create and manipulate .zfs
133  * directories.  This is called from the ZFS init routine, and initializes the
134  * vnode ops vectors that we'll be using.
135  */
136 void
137 zfsctl_init(void)
138 {
139 	VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
140 }
141 
142 void
143 zfsctl_fini(void)
144 {
145 	/*
146 	 * Remove vfsctl vnode ops
147 	 */
148 	if (zfsctl_ops_root)
149 		vn_freevnodeops(zfsctl_ops_root);
150 	if (zfsctl_ops_snapdir)
151 		vn_freevnodeops(zfsctl_ops_snapdir);
152 	if (zfsctl_ops_snapshot)
153 		vn_freevnodeops(zfsctl_ops_snapshot);
154 
155 	zfsctl_ops_root = NULL;
156 	zfsctl_ops_snapdir = NULL;
157 	zfsctl_ops_snapshot = NULL;
158 }
159 
160 /*
161  * Return the inode number associated with the 'snapshot' directory.
162  */
163 /* ARGSUSED */
164 static ino64_t
165 zfsctl_root_inode_cb(vnode_t *vp, int index)
166 {
167 	ASSERT(index == 0);
168 	return (ZFSCTL_INO_SNAPDIR);
169 }
170 
171 /*
172  * Create the '.zfs' directory.  This directory is cached as part of the VFS
173  * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
174  * therefore checks against a vfs_count of 2 instead of 1.  This reference
175  * is removed when the ctldir is destroyed in the unmount.
176  */
177 void
178 zfsctl_create(zfsvfs_t *zfsvfs)
179 {
180 	vnode_t *vp;
181 	zfsctl_node_t *zcp;
182 
183 	ASSERT(zfsvfs->z_ctldir == NULL);
184 
185 	vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
186 	    zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
187 	    zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
188 	zcp = vp->v_data;
189 	zcp->zc_id = ZFSCTL_INO_ROOT;
190 
191 	/*
192 	 * We're only faking the fact that we have a root of a filesystem for
193 	 * the sake of the GFS interfaces.  Undo the flag manipulation it did
194 	 * for us.
195 	 */
196 	vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
197 
198 	zfsvfs->z_ctldir = vp;
199 }
200 
201 /*
202  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
203  * There might still be more references if we were force unmounted, but only
204  * new zfs_inactive() calls can occur and they don't reference .zfs
205  */
206 void
207 zfsctl_destroy(zfsvfs_t *zfsvfs)
208 {
209 	VN_RELE(zfsvfs->z_ctldir);
210 	zfsvfs->z_ctldir = NULL;
211 }
212 
213 /*
214  * Given a root znode, retrieve the associated .zfs directory.
215  * Add a hold to the vnode and return it.
216  */
217 vnode_t *
218 zfsctl_root(znode_t *zp)
219 {
220 	ASSERT(zfs_has_ctldir(zp));
221 	VN_HOLD(zp->z_zfsvfs->z_ctldir);
222 	return (zp->z_zfsvfs->z_ctldir);
223 }
224 
225 /*
226  * Common open routine.  Disallow any write access.
227  */
228 /* ARGSUSED */
229 static int
230 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr)
231 {
232 	if (flags & FWRITE)
233 		return (EACCES);
234 
235 	return (0);
236 }
237 
238 /*
239  * Common close routine.  Nothing to do here.
240  */
241 /* ARGSUSED */
242 static int
243 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
244     cred_t *cr)
245 {
246 	return (0);
247 }
248 
249 /*
250  * Common access routine.  Disallow writes.
251  */
252 /* ARGSUSED */
253 static int
254 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr)
255 {
256 	if (mode & VWRITE)
257 		return (EACCES);
258 
259 	return (0);
260 }
261 
262 /*
263  * Common getattr function.  Fill in basic information.
264  */
265 static void
266 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
267 {
268 	timestruc_t now;
269 
270 	vap->va_uid = 0;
271 	vap->va_gid = 0;
272 	vap->va_rdev = 0;
273 	/*
274 	 * We are a purly virtual object, so we have no
275 	 * blocksize or allocated blocks.
276 	 */
277 	vap->va_blksize = 0;
278 	vap->va_nblocks = 0;
279 	vap->va_seq = 0;
280 	vap->va_fsid = vp->v_vfsp->vfs_dev;
281 	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
282 	    S_IROTH | S_IXOTH;
283 	vap->va_type = VDIR;
284 	/*
285 	 * We live in the now.
286 	 */
287 	gethrestime(&now);
288 	vap->va_mtime = vap->va_ctime = vap->va_atime = now;
289 }
290 
291 static int
292 zfsctl_common_fid(vnode_t *vp, fid_t *fidp)
293 {
294 	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
295 	zfsctl_node_t	*zcp = vp->v_data;
296 	uint64_t	object = zcp->zc_id;
297 	zfid_short_t	*zfid;
298 	int		i;
299 
300 	ZFS_ENTER(zfsvfs);
301 
302 	if (fidp->fid_len < SHORT_FID_LEN) {
303 		fidp->fid_len = SHORT_FID_LEN;
304 		return (ENOSPC);
305 	}
306 
307 	zfid = (zfid_short_t *)fidp;
308 
309 	zfid->zf_len = SHORT_FID_LEN;
310 
311 	for (i = 0; i < sizeof (zfid->zf_object); i++)
312 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
313 
314 	/* .zfs znodes always have a generation number of 0 */
315 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
316 		zfid->zf_gen[i] = 0;
317 
318 	ZFS_EXIT(zfsvfs);
319 	return (0);
320 }
321 
322 /*
323  * .zfs inode namespace
324  *
325  * We need to generate unique inode numbers for all files and directories
326  * within the .zfs pseudo-filesystem.  We use the following scheme:
327  *
328  * 	ENTRY			ZFSCTL_INODE
329  * 	.zfs			1
330  * 	.zfs/snapshot		2
331  * 	.zfs/snapshot/<snap>	objectid(snap)
332  */
333 
334 #define	ZFSCTL_INO_SNAP(id)	(id)
335 
336 /*
337  * Get root directory attributes.
338  */
339 /* ARGSUSED */
340 static int
341 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
342 {
343 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
344 
345 	ZFS_ENTER(zfsvfs);
346 	vap->va_nodeid = ZFSCTL_INO_ROOT;
347 	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
348 
349 	zfsctl_common_getattr(vp, vap);
350 	ZFS_EXIT(zfsvfs);
351 
352 	return (0);
353 }
354 
355 /*
356  * Special case the handling of "..".
357  */
358 /* ARGSUSED */
359 int
360 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
361     int flags, vnode_t *rdir, cred_t *cr)
362 {
363 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
364 	int err;
365 
366 	ZFS_ENTER(zfsvfs);
367 
368 	if (strcmp(nm, "..") == 0) {
369 		err = VFS_ROOT(dvp->v_vfsp, vpp);
370 	} else {
371 		err = gfs_dir_lookup(dvp, nm, vpp);
372 	}
373 
374 	ZFS_EXIT(zfsvfs);
375 
376 	return (err);
377 }
378 
379 static const fs_operation_def_t zfsctl_tops_root[] = {
380 	{ VOPNAME_OPEN,		zfsctl_common_open			},
381 	{ VOPNAME_CLOSE,	zfsctl_common_close			},
382 	{ VOPNAME_IOCTL,	fs_inval				},
383 	{ VOPNAME_GETATTR,	zfsctl_root_getattr			},
384 	{ VOPNAME_ACCESS,	zfsctl_common_access			},
385 	{ VOPNAME_READDIR,	gfs_vop_readdir				},
386 	{ VOPNAME_LOOKUP,	zfsctl_root_lookup			},
387 	{ VOPNAME_SEEK,		fs_seek					},
388 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) gfs_vop_inactive	},
389 	{ VOPNAME_FID,		zfsctl_common_fid			},
390 	{ NULL }
391 };
392 
393 static int
394 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
395 {
396 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
397 
398 	dmu_objset_name(os, zname);
399 	if (strlen(zname) + 1 + strlen(name) >= len)
400 		return (ENAMETOOLONG);
401 	(void) strcat(zname, "@");
402 	(void) strcat(zname, name);
403 	return (0);
404 }
405 
406 static int
407 zfsctl_unmount_snap(vnode_t *dvp, const char *name, int force, cred_t *cr)
408 {
409 	zfsctl_snapdir_t *sdp = dvp->v_data;
410 	zfs_snapentry_t search, *sep;
411 	avl_index_t where;
412 	int err;
413 
414 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
415 
416 	search.se_name = (char *)name;
417 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL)
418 		return (ENOENT);
419 
420 	ASSERT(vn_ismntpt(sep->se_root));
421 
422 	/* this will be dropped by dounmount() */
423 	if ((err = vn_vfswlock(sep->se_root)) != 0)
424 		return (err);
425 
426 	VN_HOLD(sep->se_root);
427 	if ((err = dounmount(vn_mountedvfs(sep->se_root), force, kcred)) != 0)
428 		return (err);
429 	ASSERT(sep->se_root->v_count == 1);
430 	gfs_vop_inactive(sep->se_root, cr);
431 
432 	avl_remove(&sdp->sd_snaps, sep);
433 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
434 	kmem_free(sep, sizeof (zfs_snapentry_t));
435 
436 	return (0);
437 }
438 
439 
440 static void
441 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
442 {
443 	avl_index_t where;
444 	vfs_t *vfsp;
445 	refstr_t *pathref;
446 	char newpath[MAXNAMELEN];
447 	const char *oldpath;
448 	char *tail;
449 
450 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
451 	ASSERT(sep != NULL);
452 
453 	vfsp = vn_mountedvfs(sep->se_root);
454 	ASSERT(vfsp != NULL);
455 
456 	vfs_lock_wait(vfsp);
457 
458 	/*
459 	 * Change the name in the AVL tree.
460 	 */
461 	avl_remove(&sdp->sd_snaps, sep);
462 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
463 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
464 	(void) strcpy(sep->se_name, nm);
465 	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
466 	avl_insert(&sdp->sd_snaps, sep, where);
467 
468 	/*
469 	 * Change the current mountpoint info:
470 	 * 	- update the tail of the mntpoint path
471 	 *	- update the tail of the resource path
472 	 */
473 	pathref = vfs_getmntpoint(vfsp);
474 	oldpath = refstr_value(pathref);
475 	VERIFY((tail = strrchr(oldpath, '/')) != NULL);
476 	ASSERT((tail - oldpath) + strlen(nm) + 2 < MAXNAMELEN);
477 	(void) strncpy(newpath, oldpath, tail - oldpath + 1);
478 	(void) strcat(newpath, nm);
479 	refstr_rele(pathref);
480 	vfs_setmntpoint(vfsp, newpath);
481 
482 	pathref = vfs_getresource(vfsp);
483 	oldpath = refstr_value(pathref);
484 	VERIFY((tail = strrchr(oldpath, '@')) != NULL);
485 	ASSERT((tail - oldpath) + strlen(nm) + 2 < MAXNAMELEN);
486 	(void) strncpy(newpath, oldpath, tail - oldpath + 1);
487 	(void) strcat(newpath, nm);
488 	refstr_rele(pathref);
489 	vfs_setresource(vfsp, newpath);
490 
491 	vfs_unlock(vfsp);
492 }
493 
494 static int
495 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
496     cred_t *cr)
497 {
498 	zfsctl_snapdir_t *sdp = sdvp->v_data;
499 	zfs_snapentry_t search, *sep;
500 	avl_index_t where;
501 	char from[MAXNAMELEN], to[MAXNAMELEN];
502 	int err;
503 
504 	err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
505 	if (err)
506 		return (err);
507 	err = zfs_secpolicy_write(from, NULL, cr);
508 	if (err)
509 		return (err);
510 
511 	/*
512 	 * Cannot move snapshots out of the snapdir.
513 	 */
514 	if (sdvp != tdvp)
515 		return (EINVAL);
516 
517 	if (strcmp(snm, tnm) == 0)
518 		return (0);
519 
520 	err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
521 	if (err)
522 		return (err);
523 
524 	mutex_enter(&sdp->sd_lock);
525 
526 	search.se_name = (char *)snm;
527 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
528 		mutex_exit(&sdp->sd_lock);
529 		return (ENOENT);
530 	}
531 
532 	err = dmu_objset_rename(from, to);
533 	if (err == 0)
534 		zfsctl_rename_snap(sdp, sep, tnm);
535 
536 	mutex_exit(&sdp->sd_lock);
537 
538 	return (err);
539 }
540 
541 /* ARGSUSED */
542 static int
543 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
544 {
545 	zfsctl_snapdir_t *sdp = dvp->v_data;
546 	char snapname[MAXNAMELEN];
547 	int err;
548 
549 	err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
550 	if (err)
551 		return (err);
552 	err = zfs_secpolicy_write(snapname, NULL, cr);
553 	if (err)
554 		return (err);
555 
556 	mutex_enter(&sdp->sd_lock);
557 
558 	err = zfsctl_unmount_snap(dvp, name, 0, cr);
559 	if (err) {
560 		mutex_exit(&sdp->sd_lock);
561 		return (err);
562 	}
563 
564 	err = dmu_objset_destroy(snapname);
565 
566 	mutex_exit(&sdp->sd_lock);
567 
568 	return (err);
569 }
570 
571 /*
572  * Lookup entry point for the 'snapshot' directory.  Try to open the
573  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
574  * Perform a mount of the associated dataset on top of the vnode.
575  */
576 /* ARGSUSED */
577 static int
578 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
579     int flags, vnode_t *rdir, cred_t *cr)
580 {
581 	zfsctl_snapdir_t *sdp = dvp->v_data;
582 	objset_t *snap;
583 	char snapname[MAXNAMELEN];
584 	char *mountpoint;
585 	zfs_snapentry_t *sep, search;
586 	struct mounta margs;
587 	vfs_t *vfsp;
588 	size_t mountpoint_len;
589 	avl_index_t where;
590 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
591 	int err;
592 
593 	ASSERT(dvp->v_type == VDIR);
594 
595 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
596 		return (0);
597 
598 	/*
599 	 * If we get a recursive call, that means we got called
600 	 * from the domount() code while it was trying to look up the
601 	 * spec (which looks like a local path for zfs).  We need to
602 	 * add some flag to domount() to tell it not to do this lookup.
603 	 */
604 	if (MUTEX_HELD(&sdp->sd_lock))
605 		return (ENOENT);
606 
607 	ZFS_ENTER(zfsvfs);
608 
609 	mutex_enter(&sdp->sd_lock);
610 	search.se_name = (char *)nm;
611 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
612 		*vpp = sep->se_root;
613 		VN_HOLD(*vpp);
614 		/*
615 		 * If the snapshot was unmounted behind our backs, remount it.
616 		 */
617 		if (!vn_ismntpt(*vpp))
618 			goto domount;
619 		VERIFY(traverse(vpp) == 0);
620 		mutex_exit(&sdp->sd_lock);
621 		ZFS_EXIT(zfsvfs);
622 		return (0);
623 	}
624 
625 	/*
626 	 * The requested snapshot is not currently mounted, look it up.
627 	 */
628 	err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
629 	if (err) {
630 		mutex_exit(&sdp->sd_lock);
631 		ZFS_EXIT(zfsvfs);
632 		return (err);
633 	}
634 	if (dmu_objset_open(snapname, DMU_OST_ZFS,
635 	    DS_MODE_STANDARD | DS_MODE_READONLY, &snap) != 0) {
636 		mutex_exit(&sdp->sd_lock);
637 		ZFS_EXIT(zfsvfs);
638 		return (ENOENT);
639 	}
640 
641 	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
642 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
643 	(void) strcpy(sep->se_name, nm);
644 	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
645 	avl_insert(&sdp->sd_snaps, sep, where);
646 
647 	dmu_objset_close(snap);
648 domount:
649 	mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
650 	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
651 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
652 	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
653 	    refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
654 
655 	margs.spec = snapname;
656 	margs.dir = mountpoint;
657 	margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
658 	margs.fstype = "zfs";
659 	margs.dataptr = NULL;
660 	margs.datalen = 0;
661 	margs.optptr = NULL;
662 	margs.optlen = 0;
663 
664 	err = domount("zfs", &margs, *vpp, kcred, &vfsp);
665 	kmem_free(mountpoint, mountpoint_len);
666 
667 	if (err == 0) {
668 		/*
669 		 * Return the mounted root rather than the covered mount point.
670 		 */
671 		VFS_RELE(vfsp);
672 		err = traverse(vpp);
673 	}
674 
675 	if (err == 0) {
676 		/*
677 		 * Fix up the root vnode.
678 		 */
679 		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
680 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
681 		(*vpp)->v_vfsp = zfsvfs->z_vfs;
682 		(*vpp)->v_flag &= ~VROOT;
683 	}
684 	mutex_exit(&sdp->sd_lock);
685 	ZFS_EXIT(zfsvfs);
686 
687 	if (err)
688 		VN_RELE(*vpp);
689 	return (err);
690 }
691 
692 /* ARGSUSED */
693 static int
694 zfsctl_snapdir_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp,
695     offset_t *offp, offset_t *nextp, void *data)
696 {
697 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
698 	char snapname[MAXNAMELEN];
699 	uint64_t id, cookie;
700 
701 	ZFS_ENTER(zfsvfs);
702 
703 	cookie = *offp;
704 	if (dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
705 	    &cookie) == ENOENT) {
706 		*eofp = 1;
707 		ZFS_EXIT(zfsvfs);
708 		return (0);
709 	}
710 
711 	(void) strcpy(dp->d_name, snapname);
712 	dp->d_ino = ZFSCTL_INO_SNAP(id);
713 	*nextp = cookie;
714 
715 	ZFS_EXIT(zfsvfs);
716 
717 	return (0);
718 }
719 
720 vnode_t *
721 zfsctl_mknode_snapdir(vnode_t *pvp)
722 {
723 	vnode_t *vp;
724 	zfsctl_snapdir_t *sdp;
725 
726 	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
727 	    zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
728 	    zfsctl_snapdir_readdir_cb, NULL);
729 	sdp = vp->v_data;
730 	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
731 	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
732 	avl_create(&sdp->sd_snaps, snapentry_compare,
733 	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
734 	return (vp);
735 }
736 
737 /* ARGSUSED */
738 static int
739 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
740 {
741 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
742 	zfsctl_snapdir_t *sdp = vp->v_data;
743 
744 	ZFS_ENTER(zfsvfs);
745 	zfsctl_common_getattr(vp, vap);
746 	vap->va_nodeid = gfs_file_inode(vp);
747 	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
748 	ZFS_EXIT(zfsvfs);
749 
750 	return (0);
751 }
752 
753 static void
754 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr)
755 {
756 	zfsctl_snapdir_t *sdp = vp->v_data;
757 
758 	ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
759 	mutex_destroy(&sdp->sd_lock);
760 	avl_destroy(&sdp->sd_snaps);
761 	gfs_vop_inactive(vp, cr);
762 }
763 
764 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
765 	{ VOPNAME_OPEN,		zfsctl_common_open			},
766 	{ VOPNAME_CLOSE,	zfsctl_common_close			},
767 	{ VOPNAME_IOCTL,	fs_inval				},
768 	{ VOPNAME_GETATTR,	zfsctl_snapdir_getattr			},
769 	{ VOPNAME_ACCESS,	zfsctl_common_access			},
770 	{ VOPNAME_RENAME,	zfsctl_snapdir_rename			},
771 	{ VOPNAME_RMDIR,	zfsctl_snapdir_remove			},
772 	{ VOPNAME_READDIR,	gfs_vop_readdir				},
773 	{ VOPNAME_LOOKUP,	zfsctl_snapdir_lookup			},
774 	{ VOPNAME_SEEK,		fs_seek					},
775 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) zfsctl_snapdir_inactive },
776 	{ VOPNAME_FID,		zfsctl_common_fid			},
777 	{ NULL }
778 };
779 
780 static vnode_t *
781 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
782 {
783 	vnode_t *vp;
784 	zfsctl_node_t *zcp;
785 
786 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
787 	    zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
788 	zcp = vp->v_data;
789 	zcp->zc_id = objset;
790 
791 	return (vp);
792 }
793 
794 static void
795 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr)
796 {
797 	zfsctl_snapdir_t *sdp;
798 	zfs_snapentry_t *sep, *next;
799 	vnode_t *dvp;
800 
801 	VERIFY(gfs_dir_lookup(vp, "..", &dvp) == 0);
802 	sdp = dvp->v_data;
803 
804 	mutex_enter(&sdp->sd_lock);
805 
806 	if (vp->v_count > 1) {
807 		mutex_exit(&sdp->sd_lock);
808 		return;
809 	}
810 	ASSERT(!vn_ismntpt(vp));
811 
812 	sep = avl_first(&sdp->sd_snaps);
813 	while (sep != NULL) {
814 		next = AVL_NEXT(&sdp->sd_snaps, sep);
815 
816 		if (sep->se_root == vp) {
817 			avl_remove(&sdp->sd_snaps, sep);
818 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
819 			kmem_free(sep, sizeof (zfs_snapentry_t));
820 			break;
821 		}
822 		sep = next;
823 	}
824 	ASSERT(sep != NULL);
825 
826 	mutex_exit(&sdp->sd_lock);
827 	VN_RELE(dvp);
828 
829 	gfs_vop_inactive(vp, cr);
830 }
831 
832 
833 /*
834  * These VP's should never see the light of day.  They should always
835  * be covered.
836  */
837 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
838 	VOPNAME_INACTIVE, (fs_generic_func_p) zfsctl_snapshot_inactive,
839 	NULL, NULL
840 };
841 
842 int
843 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
844 {
845 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
846 	vnode_t *dvp, *vp;
847 	zfsctl_snapdir_t *sdp;
848 	zfsctl_node_t *zcp;
849 	zfs_snapentry_t *sep;
850 	int error;
851 
852 	ASSERT(zfsvfs->z_ctldir != NULL);
853 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
854 	    NULL, 0, NULL, kcred);
855 	if (error != 0)
856 		return (error);
857 	sdp = dvp->v_data;
858 
859 	mutex_enter(&sdp->sd_lock);
860 	sep = avl_first(&sdp->sd_snaps);
861 	while (sep != NULL) {
862 		vp = sep->se_root;
863 		zcp = vp->v_data;
864 		if (zcp->zc_id == objsetid)
865 			break;
866 
867 		sep = AVL_NEXT(&sdp->sd_snaps, sep);
868 	}
869 
870 	if (sep != NULL) {
871 		VN_HOLD(vp);
872 		error = traverse(&vp);
873 		if (error == 0)
874 			*zfsvfsp = VTOZ(vp)->z_zfsvfs;
875 		VN_RELE(vp);
876 	} else {
877 		error = EINVAL;
878 	}
879 
880 	mutex_exit(&sdp->sd_lock);
881 	VN_RELE(dvp);
882 
883 	return (error);
884 }
885 
886 /*
887  * Unmount any snapshots for the given filesystem.  This is called from
888  * zfs_umount() - if we have a ctldir, then go through and unmount all the
889  * snapshots.
890  */
891 int
892 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
893 {
894 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
895 	vnode_t *dvp, *svp;
896 	zfsctl_snapdir_t *sdp;
897 	zfs_snapentry_t *sep, *next;
898 	int error;
899 
900 	ASSERT(zfsvfs->z_ctldir != NULL);
901 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
902 	    NULL, 0, NULL, cr);
903 	if (error != 0)
904 		return (error);
905 	sdp = dvp->v_data;
906 
907 	mutex_enter(&sdp->sd_lock);
908 
909 	sep = avl_first(&sdp->sd_snaps);
910 	while (sep != NULL) {
911 		svp = sep->se_root;
912 		next = AVL_NEXT(&sdp->sd_snaps, sep);
913 
914 		/*
915 		 * If this snapshot is not mounted, then it must
916 		 * have just been unmounted by somebody else, and
917 		 * will be cleaned up by zfsctl_snapdir_inactive().
918 		 */
919 		if (vn_ismntpt(svp)) {
920 			if ((error = vn_vfswlock(svp)) != 0)
921 				goto out;
922 
923 			VN_HOLD(svp);
924 			error = dounmount(vn_mountedvfs(svp), fflags, cr);
925 			if (error) {
926 				VN_RELE(svp);
927 				goto out;
928 			}
929 
930 			avl_remove(&sdp->sd_snaps, sep);
931 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
932 			kmem_free(sep, sizeof (zfs_snapentry_t));
933 
934 			/*
935 			 * We can't use VN_RELE(), as that will try to
936 			 * invoke zfsctl_snapdir_inactive(), and that
937 			 * would lead to an attempt to re-grab the sd_lock.
938 			 */
939 			ASSERT3U(svp->v_count, ==, 1);
940 			gfs_vop_inactive(svp, cr);
941 		}
942 		sep = next;
943 	}
944 out:
945 	mutex_exit(&sdp->sd_lock);
946 	VN_RELE(dvp);
947 
948 	return (error);
949 }
950