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