xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ctldir.c (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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 	err = dounmount(vn_mountedvfs(sep->se_root), force, kcred);
435 	if (err) {
436 		VN_RELE(sep->se_root);
437 		return (err);
438 	}
439 	ASSERT(sep->se_root->v_count == 1);
440 	gfs_vop_inactive(sep->se_root, cr);
441 
442 	avl_remove(&sdp->sd_snaps, sep);
443 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
444 	kmem_free(sep, sizeof (zfs_snapentry_t));
445 
446 	return (0);
447 }
448 
449 
450 static void
451 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
452 {
453 	avl_index_t where;
454 	vfs_t *vfsp;
455 	refstr_t *pathref;
456 	char newpath[MAXNAMELEN];
457 	const char *oldpath;
458 	char *tail;
459 
460 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
461 	ASSERT(sep != NULL);
462 
463 	vfsp = vn_mountedvfs(sep->se_root);
464 	ASSERT(vfsp != NULL);
465 
466 	vfs_lock_wait(vfsp);
467 
468 	/*
469 	 * Change the name in the AVL tree.
470 	 */
471 	avl_remove(&sdp->sd_snaps, sep);
472 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
473 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
474 	(void) strcpy(sep->se_name, nm);
475 	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
476 	avl_insert(&sdp->sd_snaps, sep, where);
477 
478 	/*
479 	 * Change the current mountpoint info:
480 	 * 	- update the tail of the mntpoint path
481 	 *	- update the tail of the resource path
482 	 */
483 	pathref = vfs_getmntpoint(vfsp);
484 	oldpath = refstr_value(pathref);
485 	VERIFY((tail = strrchr(oldpath, '/')) != NULL);
486 	ASSERT((tail - oldpath) + strlen(nm) + 2 < MAXNAMELEN);
487 	(void) strncpy(newpath, oldpath, tail - oldpath + 1);
488 	(void) strcat(newpath, nm);
489 	refstr_rele(pathref);
490 	vfs_setmntpoint(vfsp, newpath);
491 
492 	pathref = vfs_getresource(vfsp);
493 	oldpath = refstr_value(pathref);
494 	VERIFY((tail = strrchr(oldpath, '@')) != NULL);
495 	ASSERT((tail - oldpath) + strlen(nm) + 2 < MAXNAMELEN);
496 	(void) strncpy(newpath, oldpath, tail - oldpath + 1);
497 	(void) strcat(newpath, nm);
498 	refstr_rele(pathref);
499 	vfs_setresource(vfsp, newpath);
500 
501 	vfs_unlock(vfsp);
502 }
503 
504 static int
505 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
506     cred_t *cr)
507 {
508 	zfsctl_snapdir_t *sdp = sdvp->v_data;
509 	zfs_snapentry_t search, *sep;
510 	avl_index_t where;
511 	char from[MAXNAMELEN], to[MAXNAMELEN];
512 	int err;
513 
514 	err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
515 	if (err)
516 		return (err);
517 	err = zfs_secpolicy_write(from, NULL, cr);
518 	if (err)
519 		return (err);
520 
521 	/*
522 	 * Cannot move snapshots out of the snapdir.
523 	 */
524 	if (sdvp != tdvp)
525 		return (EINVAL);
526 
527 	if (strcmp(snm, tnm) == 0)
528 		return (0);
529 
530 	err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
531 	if (err)
532 		return (err);
533 
534 	mutex_enter(&sdp->sd_lock);
535 
536 	search.se_name = (char *)snm;
537 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
538 		mutex_exit(&sdp->sd_lock);
539 		return (ENOENT);
540 	}
541 
542 	err = dmu_objset_rename(from, to);
543 	if (err == 0)
544 		zfsctl_rename_snap(sdp, sep, tnm);
545 
546 	mutex_exit(&sdp->sd_lock);
547 
548 	return (err);
549 }
550 
551 /* ARGSUSED */
552 static int
553 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
554 {
555 	zfsctl_snapdir_t *sdp = dvp->v_data;
556 	char snapname[MAXNAMELEN];
557 	int err;
558 
559 	err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
560 	if (err)
561 		return (err);
562 	err = zfs_secpolicy_write(snapname, NULL, cr);
563 	if (err)
564 		return (err);
565 
566 	mutex_enter(&sdp->sd_lock);
567 
568 	err = zfsctl_unmount_snap(dvp, name, 0, cr);
569 	if (err) {
570 		mutex_exit(&sdp->sd_lock);
571 		return (err);
572 	}
573 
574 	err = dmu_objset_destroy(snapname);
575 
576 	mutex_exit(&sdp->sd_lock);
577 
578 	return (err);
579 }
580 
581 /*
582  * Lookup entry point for the 'snapshot' directory.  Try to open the
583  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
584  * Perform a mount of the associated dataset on top of the vnode.
585  */
586 /* ARGSUSED */
587 static int
588 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
589     int flags, vnode_t *rdir, cred_t *cr)
590 {
591 	zfsctl_snapdir_t *sdp = dvp->v_data;
592 	objset_t *snap;
593 	char snapname[MAXNAMELEN];
594 	char *mountpoint;
595 	zfs_snapentry_t *sep, search;
596 	struct mounta margs;
597 	vfs_t *vfsp;
598 	size_t mountpoint_len;
599 	avl_index_t where;
600 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
601 	int err;
602 
603 	ASSERT(dvp->v_type == VDIR);
604 
605 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
606 		return (0);
607 
608 	/*
609 	 * If we get a recursive call, that means we got called
610 	 * from the domount() code while it was trying to look up the
611 	 * spec (which looks like a local path for zfs).  We need to
612 	 * add some flag to domount() to tell it not to do this lookup.
613 	 */
614 	if (MUTEX_HELD(&sdp->sd_lock))
615 		return (ENOENT);
616 
617 	ZFS_ENTER(zfsvfs);
618 
619 	mutex_enter(&sdp->sd_lock);
620 	search.se_name = (char *)nm;
621 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
622 		*vpp = sep->se_root;
623 		VN_HOLD(*vpp);
624 		err = traverse(vpp);
625 		if (err) {
626 			VN_RELE(*vpp);
627 			*vpp = NULL;
628 		} else if (*vpp == sep->se_root) {
629 			/*
630 			 * The snapshot was unmounted behind our backs,
631 			 * try to remount it.
632 			 */
633 			goto domount;
634 		}
635 		mutex_exit(&sdp->sd_lock);
636 		ZFS_EXIT(zfsvfs);
637 		return (err);
638 	}
639 
640 	/*
641 	 * The requested snapshot is not currently mounted, look it up.
642 	 */
643 	err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
644 	if (err) {
645 		mutex_exit(&sdp->sd_lock);
646 		ZFS_EXIT(zfsvfs);
647 		return (err);
648 	}
649 	if (dmu_objset_open(snapname, DMU_OST_ZFS,
650 	    DS_MODE_STANDARD | DS_MODE_READONLY, &snap) != 0) {
651 		mutex_exit(&sdp->sd_lock);
652 		ZFS_EXIT(zfsvfs);
653 		return (ENOENT);
654 	}
655 
656 	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
657 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
658 	(void) strcpy(sep->se_name, nm);
659 	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
660 	avl_insert(&sdp->sd_snaps, sep, where);
661 
662 	dmu_objset_close(snap);
663 domount:
664 	mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
665 	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
666 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
667 	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
668 	    refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
669 
670 	margs.spec = snapname;
671 	margs.dir = mountpoint;
672 	margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
673 	margs.fstype = "zfs";
674 	margs.dataptr = NULL;
675 	margs.datalen = 0;
676 	margs.optptr = NULL;
677 	margs.optlen = 0;
678 
679 	err = domount("zfs", &margs, *vpp, kcred, &vfsp);
680 	kmem_free(mountpoint, mountpoint_len);
681 
682 	if (err == 0) {
683 		/*
684 		 * Return the mounted root rather than the covered mount point.
685 		 */
686 		VFS_RELE(vfsp);
687 		err = traverse(vpp);
688 	}
689 
690 	if (err == 0) {
691 		/*
692 		 * Fix up the root vnode.
693 		 */
694 		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
695 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
696 		(*vpp)->v_vfsp = zfsvfs->z_vfs;
697 		(*vpp)->v_flag &= ~VROOT;
698 	}
699 	mutex_exit(&sdp->sd_lock);
700 	ZFS_EXIT(zfsvfs);
701 
702 	/*
703 	 * If we had an error, drop our hold on the vnode and
704 	 * zfsctl_snapshot_inactive() will clean up.
705 	 */
706 	if (err) {
707 		VN_RELE(*vpp);
708 		*vpp = NULL;
709 	}
710 	return (err);
711 }
712 
713 /* ARGSUSED */
714 static int
715 zfsctl_snapdir_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp,
716     offset_t *offp, offset_t *nextp, void *data)
717 {
718 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
719 	char snapname[MAXNAMELEN];
720 	uint64_t id, cookie;
721 
722 	ZFS_ENTER(zfsvfs);
723 
724 	cookie = *offp;
725 	if (dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
726 	    &cookie) == ENOENT) {
727 		*eofp = 1;
728 		ZFS_EXIT(zfsvfs);
729 		return (0);
730 	}
731 
732 	(void) strcpy(dp->d_name, snapname);
733 	dp->d_ino = ZFSCTL_INO_SNAP(id);
734 	*nextp = cookie;
735 
736 	ZFS_EXIT(zfsvfs);
737 
738 	return (0);
739 }
740 
741 vnode_t *
742 zfsctl_mknode_snapdir(vnode_t *pvp)
743 {
744 	vnode_t *vp;
745 	zfsctl_snapdir_t *sdp;
746 
747 	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
748 	    zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
749 	    zfsctl_snapdir_readdir_cb, NULL);
750 	sdp = vp->v_data;
751 	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
752 	sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
753 	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
754 	avl_create(&sdp->sd_snaps, snapentry_compare,
755 	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
756 	return (vp);
757 }
758 
759 /* ARGSUSED */
760 static int
761 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
762 {
763 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
764 	zfsctl_snapdir_t *sdp = vp->v_data;
765 
766 	ZFS_ENTER(zfsvfs);
767 	zfsctl_common_getattr(vp, vap);
768 	vap->va_nodeid = gfs_file_inode(vp);
769 	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
770 	ZFS_EXIT(zfsvfs);
771 
772 	return (0);
773 }
774 
775 /* ARGSUSED */
776 static void
777 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr)
778 {
779 	zfsctl_snapdir_t *sdp = vp->v_data;
780 	void *private;
781 
782 	private = gfs_dir_inactive(vp);
783 	if (private != NULL) {
784 		ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
785 		mutex_destroy(&sdp->sd_lock);
786 		avl_destroy(&sdp->sd_snaps);
787 		kmem_free(private, sizeof (zfsctl_snapdir_t));
788 	}
789 }
790 
791 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
792 	{ VOPNAME_OPEN,		zfsctl_common_open			},
793 	{ VOPNAME_CLOSE,	zfsctl_common_close			},
794 	{ VOPNAME_IOCTL,	fs_inval				},
795 	{ VOPNAME_GETATTR,	zfsctl_snapdir_getattr			},
796 	{ VOPNAME_ACCESS,	zfsctl_common_access			},
797 	{ VOPNAME_RENAME,	zfsctl_snapdir_rename			},
798 	{ VOPNAME_RMDIR,	zfsctl_snapdir_remove			},
799 	{ VOPNAME_READDIR,	gfs_vop_readdir				},
800 	{ VOPNAME_LOOKUP,	zfsctl_snapdir_lookup			},
801 	{ VOPNAME_SEEK,		fs_seek					},
802 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) zfsctl_snapdir_inactive },
803 	{ VOPNAME_FID,		zfsctl_common_fid			},
804 	{ NULL }
805 };
806 
807 static vnode_t *
808 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
809 {
810 	vnode_t *vp;
811 	zfsctl_node_t *zcp;
812 
813 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
814 	    zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
815 	zcp = vp->v_data;
816 	zcp->zc_id = objset;
817 
818 	return (vp);
819 }
820 
821 static void
822 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr)
823 {
824 	zfsctl_snapdir_t *sdp;
825 	zfs_snapentry_t *sep, *next;
826 	vnode_t *dvp;
827 
828 	VERIFY(gfs_dir_lookup(vp, "..", &dvp) == 0);
829 	sdp = dvp->v_data;
830 
831 	mutex_enter(&sdp->sd_lock);
832 
833 	if (vp->v_count > 1) {
834 		mutex_exit(&sdp->sd_lock);
835 		return;
836 	}
837 	ASSERT(!vn_ismntpt(vp));
838 
839 	sep = avl_first(&sdp->sd_snaps);
840 	while (sep != NULL) {
841 		next = AVL_NEXT(&sdp->sd_snaps, sep);
842 
843 		if (sep->se_root == vp) {
844 			avl_remove(&sdp->sd_snaps, sep);
845 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
846 			kmem_free(sep, sizeof (zfs_snapentry_t));
847 			break;
848 		}
849 		sep = next;
850 	}
851 	ASSERT(sep != NULL);
852 
853 	mutex_exit(&sdp->sd_lock);
854 	VN_RELE(dvp);
855 
856 	/*
857 	 * Dispose of the vnode for the snapshot mount point.
858 	 * This is safe to do because once this entry has been removed
859 	 * from the AVL tree, it can't be found again, so cannot become
860 	 * "active".  If we lookup the same name again we will end up
861 	 * creating a new vnode.
862 	 */
863 	gfs_vop_inactive(vp, cr);
864 }
865 
866 
867 /*
868  * These VP's should never see the light of day.  They should always
869  * be covered.
870  */
871 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
872 	VOPNAME_INACTIVE, (fs_generic_func_p) zfsctl_snapshot_inactive,
873 	NULL, NULL
874 };
875 
876 int
877 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
878 {
879 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
880 	vnode_t *dvp, *vp;
881 	zfsctl_snapdir_t *sdp;
882 	zfsctl_node_t *zcp;
883 	zfs_snapentry_t *sep;
884 	int error;
885 
886 	ASSERT(zfsvfs->z_ctldir != NULL);
887 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
888 	    NULL, 0, NULL, kcred);
889 	if (error != 0)
890 		return (error);
891 	sdp = dvp->v_data;
892 
893 	mutex_enter(&sdp->sd_lock);
894 	sep = avl_first(&sdp->sd_snaps);
895 	while (sep != NULL) {
896 		vp = sep->se_root;
897 		zcp = vp->v_data;
898 		if (zcp->zc_id == objsetid)
899 			break;
900 
901 		sep = AVL_NEXT(&sdp->sd_snaps, sep);
902 	}
903 
904 	if (sep != NULL) {
905 		VN_HOLD(vp);
906 		error = traverse(&vp);
907 		if (error == 0) {
908 			if (vp == sep->se_root)
909 				error = EINVAL;
910 			else
911 				*zfsvfsp = VTOZ(vp)->z_zfsvfs;
912 		}
913 		mutex_exit(&sdp->sd_lock);
914 		VN_RELE(vp);
915 	} else {
916 		error = EINVAL;
917 		mutex_exit(&sdp->sd_lock);
918 	}
919 
920 	VN_RELE(dvp);
921 
922 	return (error);
923 }
924 
925 /*
926  * Unmount any snapshots for the given filesystem.  This is called from
927  * zfs_umount() - if we have a ctldir, then go through and unmount all the
928  * snapshots.
929  */
930 int
931 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
932 {
933 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
934 	vnode_t *dvp, *svp;
935 	zfsctl_snapdir_t *sdp;
936 	zfs_snapentry_t *sep, *next;
937 	int error;
938 
939 	ASSERT(zfsvfs->z_ctldir != NULL);
940 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
941 	    NULL, 0, NULL, cr);
942 	if (error != 0)
943 		return (error);
944 	sdp = dvp->v_data;
945 
946 	mutex_enter(&sdp->sd_lock);
947 
948 	sep = avl_first(&sdp->sd_snaps);
949 	while (sep != NULL) {
950 		svp = sep->se_root;
951 		next = AVL_NEXT(&sdp->sd_snaps, sep);
952 
953 		/*
954 		 * If this snapshot is not mounted, then it must
955 		 * have just been unmounted by somebody else, and
956 		 * will be cleaned up by zfsctl_snapdir_inactive().
957 		 */
958 		if (vn_ismntpt(svp)) {
959 			if ((error = vn_vfswlock(svp)) != 0)
960 				goto out;
961 
962 			VN_HOLD(svp);
963 			error = dounmount(vn_mountedvfs(svp), fflags, cr);
964 			if (error) {
965 				VN_RELE(svp);
966 				goto out;
967 			}
968 
969 			avl_remove(&sdp->sd_snaps, sep);
970 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
971 			kmem_free(sep, sizeof (zfs_snapentry_t));
972 
973 			/*
974 			 * We can't use VN_RELE(), as that will try to
975 			 * invoke zfsctl_snapdir_inactive(), and that
976 			 * would lead to an attempt to re-grab the sd_lock.
977 			 */
978 			ASSERT3U(svp->v_count, ==, 1);
979 			gfs_vop_inactive(svp, cr);
980 		}
981 		sep = next;
982 	}
983 out:
984 	mutex_exit(&sdp->sd_lock);
985 	VN_RELE(dvp);
986 
987 	return (error);
988 }
989