xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_ctldir.c (revision fe0e7ec4d916b05b52d8c7cc8a3e6a1b28e77b6f)
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 2005 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
203  * unmounted, and there are no more references.  Release the vnode,
204  * which will release the hold on the vfs structure.
205  */
206 void
207 zfsctl_destroy(zfsvfs_t *zfsvfs)
208 {
209 	ASSERT(zfsvfs->z_ctldir->v_count == 1);
210 	VN_RELE(zfsvfs->z_ctldir);
211 	zfsvfs->z_ctldir = NULL;
212 }
213 
214 /*
215  * Given a root znode, retrieve the associated .zfs directory.
216  * Add a hold to the vnode and return it.
217  */
218 vnode_t *
219 zfsctl_root(znode_t *zp)
220 {
221 	ASSERT(zfs_has_ctldir(zp));
222 	VN_HOLD(zp->z_zfsvfs->z_ctldir);
223 	return (zp->z_zfsvfs->z_ctldir);
224 }
225 
226 /*
227  * Common open routine.  Disallow any write access.
228  */
229 /* ARGSUSED */
230 static int
231 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr)
232 {
233 	if (flags & FWRITE)
234 		return (EACCES);
235 
236 	return (0);
237 }
238 
239 /*
240  * Common close routine.  Nothing to do here.
241  */
242 /* ARGSUSED */
243 static int
244 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
245     cred_t *cr)
246 {
247 	return (0);
248 }
249 
250 /*
251  * Common access routine.  Disallow writes.
252  */
253 /* ARGSUSED */
254 static int
255 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr)
256 {
257 	if (mode & VWRITE)
258 		return (EACCES);
259 
260 	return (0);
261 }
262 
263 /*
264  * Common getattr function.  Fill in basic information.
265  */
266 static void
267 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
268 {
269 	timestruc_t now;
270 
271 	vap->va_uid = 0;
272 	vap->va_gid = 0;
273 	vap->va_rdev = 0;
274 	/*
275 	 * We are a purly virtual object, so we have no
276 	 * blocksize or allocated blocks.
277 	 */
278 	vap->va_blksize = 0;
279 	vap->va_nblocks = 0;
280 	vap->va_seq = 0;
281 	vap->va_fsid = vp->v_vfsp->vfs_dev;
282 	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
283 	    S_IROTH | S_IXOTH;
284 	vap->va_type = VDIR;
285 	/*
286 	 * We live in the now.
287 	 */
288 	gethrestime(&now);
289 	vap->va_mtime = vap->va_ctime = vap->va_atime = now;
290 }
291 
292 static int
293 zfsctl_common_fid(vnode_t *vp, fid_t *fidp)
294 {
295 	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
296 	zfsctl_node_t	*zcp = vp->v_data;
297 	uint64_t	object = zcp->zc_id;
298 	zfid_short_t	*zfid;
299 	int		i;
300 
301 	ZFS_ENTER(zfsvfs);
302 
303 	if (fidp->fid_len < SHORT_FID_LEN) {
304 		fidp->fid_len = SHORT_FID_LEN;
305 		return (ENOSPC);
306 	}
307 
308 	zfid = (zfid_short_t *)fidp;
309 
310 	zfid->zf_len = SHORT_FID_LEN;
311 
312 	for (i = 0; i < sizeof (zfid->zf_object); i++)
313 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
314 
315 	/* .zfs znodes always have a generation number of 0 */
316 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
317 		zfid->zf_gen[i] = 0;
318 
319 	ZFS_EXIT(zfsvfs);
320 	return (0);
321 }
322 
323 /*
324  * .zfs inode namespace
325  *
326  * We need to generate unique inode numbers for all files and directories
327  * within the .zfs pseudo-filesystem.  We use the following scheme:
328  *
329  * 	ENTRY			ZFSCTL_INODE
330  * 	.zfs			1
331  * 	.zfs/snapshot		2
332  * 	.zfs/snapshot/<snap>	objectid(snap)
333  */
334 
335 #define	ZFSCTL_INO_SNAP(id)	(id)
336 
337 /*
338  * Get root directory attributes.
339  */
340 /* ARGSUSED */
341 static int
342 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
343 {
344 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
345 
346 	ZFS_ENTER(zfsvfs);
347 	vap->va_nodeid = ZFSCTL_INO_ROOT;
348 	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
349 
350 	zfsctl_common_getattr(vp, vap);
351 	ZFS_EXIT(zfsvfs);
352 
353 	return (0);
354 }
355 
356 /*
357  * Special case the handling of "..".
358  */
359 /* ARGSUSED */
360 int
361 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
362     int flags, vnode_t *rdir, cred_t *cr)
363 {
364 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
365 	int err;
366 
367 	ZFS_ENTER(zfsvfs);
368 
369 	if (strcmp(nm, "..") == 0) {
370 		err = VFS_ROOT(dvp->v_vfsp, vpp);
371 	} else {
372 		err = gfs_dir_lookup(dvp, nm, vpp);
373 	}
374 
375 	ZFS_EXIT(zfsvfs);
376 
377 	return (err);
378 }
379 
380 static const fs_operation_def_t zfsctl_tops_root[] = {
381 	{ VOPNAME_OPEN,		zfsctl_common_open			},
382 	{ VOPNAME_CLOSE,	zfsctl_common_close			},
383 	{ VOPNAME_IOCTL,	fs_inval				},
384 	{ VOPNAME_GETATTR,	zfsctl_root_getattr			},
385 	{ VOPNAME_ACCESS,	zfsctl_common_access			},
386 	{ VOPNAME_READDIR,	gfs_vop_readdir				},
387 	{ VOPNAME_LOOKUP,	zfsctl_root_lookup			},
388 	{ VOPNAME_SEEK,		fs_seek					},
389 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) gfs_vop_inactive	},
390 	{ VOPNAME_FID,		zfsctl_common_fid			},
391 	{ NULL }
392 };
393 
394 static int
395 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
396 {
397 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
398 
399 	dmu_objset_name(os, zname);
400 	(void) strcat(zname, "@");
401 	if (strlen(zname) + strlen(name) >= len)
402 		return (ENAMETOOLONG);
403 	(void) strcat(zname, name);
404 	return (0);
405 }
406 
407 static int
408 zfsctl_unmount_snap(vnode_t *dvp, const char *name, int force, cred_t *cr)
409 {
410 	zfsctl_snapdir_t *sdp = dvp->v_data;
411 	zfs_snapentry_t search, *sep;
412 	avl_index_t where;
413 	int err;
414 
415 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
416 
417 	search.se_name = (char *)name;
418 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL)
419 		return (ENOENT);
420 
421 	ASSERT(vn_ismntpt(sep->se_root));
422 
423 	/* this will be dropped by dounmount() */
424 	if ((err = vn_vfswlock(sep->se_root)) != 0)
425 		return (err);
426 
427 	VN_HOLD(sep->se_root);
428 	if ((err = dounmount(vn_mountedvfs(sep->se_root), force, kcred)) != 0)
429 		return (err);
430 	ASSERT(sep->se_root->v_count == 1);
431 	gfs_vop_inactive(sep->se_root, cr);
432 
433 	avl_remove(&sdp->sd_snaps, sep);
434 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
435 	kmem_free(sep, sizeof (zfs_snapentry_t));
436 
437 	return (0);
438 }
439 
440 
441 static int
442 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
443 {
444 	avl_index_t where;
445 	vfs_t *vfsp;
446 	refstr_t *pathref;
447 	char newpath[MAXNAMELEN];
448 	const char *oldpath;
449 	char *tail;
450 	int err;
451 
452 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
453 	ASSERT(sep != NULL);
454 
455 	vfsp = vn_mountedvfs(sep->se_root);
456 	ASSERT(vfsp != NULL);
457 
458 	if (err = vfs_lock(vfsp))
459 		return (err);
460 
461 	/*
462 	 * Change the name in the AVL tree.
463 	 */
464 	avl_remove(&sdp->sd_snaps, sep);
465 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
466 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
467 	(void) strcpy(sep->se_name, nm);
468 	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
469 	avl_insert(&sdp->sd_snaps, sep, where);
470 
471 	/*
472 	 * Change the current mountpoint info:
473 	 * 	- update the tail of the mntpoint path
474 	 *	- update the tail of the resource path
475 	 */
476 	pathref = vfs_getmntpoint(vfsp);
477 	oldpath = refstr_value(pathref);
478 	VERIFY((tail = strrchr(oldpath, '/')) != NULL);
479 	ASSERT((tail - oldpath) + strlen(nm) + 2 < MAXNAMELEN);
480 	(void) strncpy(newpath, oldpath, tail - oldpath + 1);
481 	(void) strcat(newpath, nm);
482 	refstr_rele(pathref);
483 	vfs_setmntpoint(vfsp, newpath);
484 
485 	pathref = vfs_getresource(vfsp);
486 	oldpath = refstr_value(pathref);
487 	VERIFY((tail = strrchr(oldpath, '@')) != NULL);
488 	ASSERT((tail - oldpath) + strlen(nm) + 2 < MAXNAMELEN);
489 	(void) strncpy(newpath, oldpath, tail - oldpath + 1);
490 	(void) strcat(newpath, nm);
491 	refstr_rele(pathref);
492 	vfs_setresource(vfsp, newpath);
493 
494 	vfs_unlock(vfsp);
495 	return (0);
496 }
497 
498 static int
499 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
500     cred_t *cr)
501 {
502 	zfsctl_snapdir_t *sdp = sdvp->v_data;
503 	zfs_snapentry_t search, *sep;
504 	avl_index_t where;
505 	char from[MAXNAMELEN], to[MAXNAMELEN];
506 	int err;
507 
508 	VERIFY(zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from) == 0);
509 	err = zfs_secpolicy_write(from, NULL, cr);
510 	if (err)
511 		return (err);
512 
513 	/*
514 	 * Cannot move snapshots out of the snapdir.
515 	 */
516 	if (sdvp != tdvp)
517 		return (EINVAL);
518 
519 	if (strcmp(snm, tnm) == 0)
520 		return (0);
521 
522 	mutex_enter(&sdp->sd_lock);
523 
524 	search.se_name = (char *)snm;
525 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
526 		err = zfsctl_rename_snap(sdp, sep, tnm);
527 		if (err) {
528 			mutex_exit(&sdp->sd_lock);
529 			return (err);
530 		}
531 	}
532 
533 
534 	VERIFY(zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to) == 0);
535 	err = dmu_objset_rename(from, to);
536 
537 	mutex_exit(&sdp->sd_lock);
538 
539 	return (err);
540 }
541 
542 /* ARGSUSED */
543 static int
544 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
545 {
546 	zfsctl_snapdir_t *sdp = dvp->v_data;
547 	char snapname[MAXNAMELEN];
548 	int err;
549 
550 	VERIFY(zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname) == 0);
551 	err = zfs_secpolicy_write(snapname, NULL, cr);
552 	if (err)
553 		return (err);
554 
555 	mutex_enter(&sdp->sd_lock);
556 
557 	err = zfsctl_unmount_snap(dvp, name, 0, cr);
558 	if (err) {
559 		mutex_exit(&sdp->sd_lock);
560 		return (err);
561 	}
562 
563 	err = dmu_objset_destroy(snapname);
564 
565 	mutex_exit(&sdp->sd_lock);
566 
567 	return (err);
568 }
569 
570 /*
571  * Lookup entry point for the 'snapshot' directory.  Try to open the
572  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
573  * Perform a mount of the associated dataset on top of the vnode.
574  */
575 /* ARGSUSED */
576 static int
577 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
578     int flags, vnode_t *rdir, cred_t *cr)
579 {
580 	zfsctl_snapdir_t *sdp = dvp->v_data;
581 	objset_t *snap;
582 	char snapname[MAXNAMELEN];
583 	char *mountpoint;
584 	zfs_snapentry_t *sep, search;
585 	struct mounta margs;
586 	vfs_t *vfsp;
587 	size_t mountpoint_len;
588 	avl_index_t where;
589 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
590 	int err;
591 
592 	ASSERT(dvp->v_type == VDIR);
593 
594 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
595 		return (0);
596 
597 	/*
598 	 * If we get a recursive call, that means we got called
599 	 * from the domount() code while it was trying to look up the
600 	 * spec (which looks like a local path for zfs).  We need to
601 	 * add some flag to domount() to tell it not to do this lookup.
602 	 */
603 	if (MUTEX_HELD(&sdp->sd_lock))
604 		return (ENOENT);
605 
606 	ZFS_ENTER(zfsvfs);
607 
608 	mutex_enter(&sdp->sd_lock);
609 	search.se_name = (char *)nm;
610 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
611 		*vpp = sep->se_root;
612 		VN_HOLD(*vpp);
613 		/*
614 		 * If the snapshot was unmounted behind our backs, remount it.
615 		 */
616 		if (!vn_ismntpt(*vpp))
617 			goto domount;
618 		VERIFY(traverse(vpp) == 0);
619 		mutex_exit(&sdp->sd_lock);
620 		ZFS_EXIT(zfsvfs);
621 		return (0);
622 	}
623 
624 	/*
625 	 * The requested snapshot is not currently mounted, look it up.
626 	 */
627 	VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0);
628 	if (dmu_objset_open(snapname, DMU_OST_ZFS,
629 	    DS_MODE_STANDARD | DS_MODE_READONLY, &snap) != 0) {
630 		mutex_exit(&sdp->sd_lock);
631 		ZFS_EXIT(zfsvfs);
632 		return (ENOENT);
633 	}
634 
635 	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
636 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
637 	(void) strcpy(sep->se_name, nm);
638 	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
639 	avl_insert(&sdp->sd_snaps, sep, where);
640 
641 	dmu_objset_close(snap);
642 domount:
643 	mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
644 	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
645 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
646 	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
647 	    refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
648 
649 	margs.spec = snapname;
650 	margs.dir = mountpoint;
651 	margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
652 	margs.fstype = "zfs";
653 	margs.dataptr = NULL;
654 	margs.datalen = 0;
655 	margs.optptr = NULL;
656 	margs.optlen = 0;
657 
658 	err = domount("zfs", &margs, *vpp, kcred, &vfsp);
659 	kmem_free(mountpoint, mountpoint_len);
660 
661 	if (err == 0) {
662 		/*
663 		 * Return the mounted root rather than the covered mount point.
664 		 */
665 		VFS_RELE(vfsp);
666 		err = traverse(vpp);
667 	}
668 
669 	if (err == 0) {
670 		/*
671 		 * Fix up the root vnode.
672 		 */
673 		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
674 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
675 		(*vpp)->v_vfsp = zfsvfs->z_vfs;
676 		(*vpp)->v_flag &= ~VROOT;
677 	}
678 	mutex_exit(&sdp->sd_lock);
679 	ZFS_EXIT(zfsvfs);
680 
681 	if (err)
682 		VN_RELE(*vpp);
683 	return (err);
684 }
685 
686 /* ARGSUSED */
687 static int
688 zfsctl_snapdir_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp,
689     offset_t *offp, offset_t *nextp, void *data)
690 {
691 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
692 	char snapname[MAXNAMELEN];
693 	uint64_t id, cookie;
694 
695 	ZFS_ENTER(zfsvfs);
696 
697 	cookie = *offp;
698 	if (dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
699 	    &cookie) == ENOENT) {
700 		*eofp = 1;
701 		ZFS_EXIT(zfsvfs);
702 		return (0);
703 	}
704 
705 	(void) strcpy(dp->d_name, snapname);
706 	dp->d_ino = ZFSCTL_INO_SNAP(id);
707 	*nextp = cookie;
708 
709 	ZFS_EXIT(zfsvfs);
710 
711 	return (0);
712 }
713 
714 vnode_t *
715 zfsctl_mknode_snapdir(vnode_t *pvp)
716 {
717 	vnode_t *vp;
718 	zfsctl_snapdir_t *sdp;
719 
720 	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
721 	    zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
722 	    zfsctl_snapdir_readdir_cb, NULL);
723 	sdp = vp->v_data;
724 	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
725 	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
726 	avl_create(&sdp->sd_snaps, snapentry_compare,
727 	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
728 	return (vp);
729 }
730 
731 /* ARGSUSED */
732 static int
733 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
734 {
735 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
736 	zfsctl_snapdir_t *sdp = vp->v_data;
737 
738 	ZFS_ENTER(zfsvfs);
739 	zfsctl_common_getattr(vp, vap);
740 	vap->va_nodeid = gfs_file_inode(vp);
741 	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
742 	ZFS_EXIT(zfsvfs);
743 
744 	return (0);
745 }
746 
747 static void
748 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr)
749 {
750 	zfsctl_snapdir_t *sdp = vp->v_data;
751 
752 	ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
753 	mutex_destroy(&sdp->sd_lock);
754 	avl_destroy(&sdp->sd_snaps);
755 	gfs_vop_inactive(vp, cr);
756 }
757 
758 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
759 	{ VOPNAME_OPEN,		zfsctl_common_open			},
760 	{ VOPNAME_CLOSE,	zfsctl_common_close			},
761 	{ VOPNAME_IOCTL,	fs_inval				},
762 	{ VOPNAME_GETATTR,	zfsctl_snapdir_getattr			},
763 	{ VOPNAME_ACCESS,	zfsctl_common_access			},
764 	{ VOPNAME_RENAME,	zfsctl_snapdir_rename			},
765 	{ VOPNAME_RMDIR,	zfsctl_snapdir_remove			},
766 	{ VOPNAME_READDIR,	gfs_vop_readdir				},
767 	{ VOPNAME_LOOKUP,	zfsctl_snapdir_lookup			},
768 	{ VOPNAME_SEEK,		fs_seek					},
769 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) zfsctl_snapdir_inactive },
770 	{ VOPNAME_FID,		zfsctl_common_fid			},
771 	{ NULL }
772 };
773 
774 static vnode_t *
775 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
776 {
777 	vnode_t *vp;
778 	zfsctl_node_t *zcp;
779 
780 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
781 	    zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
782 	zcp = vp->v_data;
783 	zcp->zc_id = objset;
784 
785 	return (vp);
786 }
787 
788 static void
789 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr)
790 {
791 	zfsctl_snapdir_t *sdp;
792 	zfs_snapentry_t *sep, *next;
793 	vnode_t *dvp;
794 
795 	VERIFY(gfs_dir_lookup(vp, "..", &dvp) == 0);
796 	sdp = dvp->v_data;
797 
798 	mutex_enter(&sdp->sd_lock);
799 
800 	if (vp->v_count > 1) {
801 		mutex_exit(&sdp->sd_lock);
802 		return;
803 	}
804 	ASSERT(!vn_ismntpt(vp));
805 
806 	sep = avl_first(&sdp->sd_snaps);
807 	while (sep != NULL) {
808 		next = AVL_NEXT(&sdp->sd_snaps, sep);
809 
810 		if (sep->se_root == vp) {
811 			avl_remove(&sdp->sd_snaps, sep);
812 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
813 			kmem_free(sep, sizeof (zfs_snapentry_t));
814 			break;
815 		}
816 		sep = next;
817 	}
818 	ASSERT(sep != NULL);
819 
820 	mutex_exit(&sdp->sd_lock);
821 	VN_RELE(dvp);
822 
823 	gfs_vop_inactive(vp, cr);
824 }
825 
826 
827 /*
828  * These VP's should never see the light of day.  They should always
829  * be covered.
830  */
831 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
832 	VOPNAME_INACTIVE, (fs_generic_func_p) zfsctl_snapshot_inactive,
833 	NULL, NULL
834 };
835 
836 int
837 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
838 {
839 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
840 	vnode_t *dvp, *vp;
841 	zfsctl_snapdir_t *sdp;
842 	zfsctl_node_t *zcp;
843 	zfs_snapentry_t *sep;
844 	int error;
845 
846 	ASSERT(zfsvfs->z_ctldir != NULL);
847 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
848 	    NULL, 0, NULL, kcred);
849 	if (error != 0)
850 		return (error);
851 	sdp = dvp->v_data;
852 
853 	mutex_enter(&sdp->sd_lock);
854 	sep = avl_first(&sdp->sd_snaps);
855 	while (sep != NULL) {
856 		vp = sep->se_root;
857 		zcp = vp->v_data;
858 		if (zcp->zc_id == objsetid)
859 			break;
860 
861 		sep = AVL_NEXT(&sdp->sd_snaps, sep);
862 	}
863 
864 	if (sep != NULL) {
865 		VN_HOLD(vp);
866 		error = traverse(&vp);
867 		if (error == 0)
868 			*zfsvfsp = VTOZ(vp)->z_zfsvfs;
869 		VN_RELE(vp);
870 	} else {
871 		error = EINVAL;
872 	}
873 
874 	mutex_exit(&sdp->sd_lock);
875 	VN_RELE(dvp);
876 
877 	return (error);
878 }
879 
880 /*
881  * Unmount any snapshots for the given filesystem.  This is called from
882  * zfs_umount() - if we have a ctldir, then go through and unmount all the
883  * snapshots.
884  */
885 int
886 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
887 {
888 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
889 	vnode_t *dvp, *svp;
890 	zfsctl_snapdir_t *sdp;
891 	zfs_snapentry_t *sep, *next;
892 	int error;
893 
894 	ASSERT(zfsvfs->z_ctldir != NULL);
895 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
896 	    NULL, 0, NULL, cr);
897 	if (error != 0)
898 		return (error);
899 	sdp = dvp->v_data;
900 
901 	mutex_enter(&sdp->sd_lock);
902 
903 	sep = avl_first(&sdp->sd_snaps);
904 	while (sep != NULL) {
905 		svp = sep->se_root;
906 		next = AVL_NEXT(&sdp->sd_snaps, sep);
907 
908 		/*
909 		 * If this snapshot is not mounted, then it must
910 		 * have just been unmounted by somebody else, and
911 		 * will be cleaned up by zfsctl_snapdir_inactive().
912 		 */
913 		if (vn_ismntpt(svp)) {
914 			if ((error = vn_vfswlock(svp)) != 0)
915 				goto out;
916 
917 			VN_HOLD(svp);
918 			error = dounmount(vn_mountedvfs(svp), fflags, cr);
919 			if (error) {
920 				VN_RELE(svp);
921 				goto out;
922 			}
923 
924 			avl_remove(&sdp->sd_snaps, sep);
925 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
926 			kmem_free(sep, sizeof (zfs_snapentry_t));
927 
928 			/*
929 			 * We can't use VN_RELE(), as that will try to
930 			 * invoke zfsctl_snapdir_inactive(), and that
931 			 * would lead to an attempt to re-grab the sd_lock.
932 			 */
933 			ASSERT3U(svp->v_count, ==, 1);
934 			gfs_vop_inactive(svp, cr);
935 		}
936 		sep = next;
937 	}
938 out:
939 	mutex_exit(&sdp->sd_lock);
940 	VN_RELE(dvp);
941 
942 	return (error);
943 }
944