zfs_ctldir.c revision 182371
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 2007 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 <sys/zfs_context.h>
59#include <sys/zfs_ctldir.h>
60#include <sys/zfs_ioctl.h>
61#include <sys/zfs_vfsops.h>
62#include <sys/namei.h>
63#include <sys/gfs.h>
64#include <sys/stat.h>
65#include <sys/dmu.h>
66#include <sys/mount.h>
67
68typedef struct {
69	char		*se_name;
70	vnode_t		*se_root;
71	avl_node_t	se_node;
72} zfs_snapentry_t;
73
74static int
75snapentry_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
89static struct vop_vector zfsctl_ops_root;
90static struct vop_vector zfsctl_ops_snapdir;
91static struct vop_vector zfsctl_ops_snapshot;
92
93static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
94static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
95
96typedef struct zfsctl_node {
97	gfs_dir_t	zc_gfs_private;
98	uint64_t	zc_id;
99	timestruc_t	zc_cmtime;	/* ctime and mtime, always the same */
100} zfsctl_node_t;
101
102typedef struct zfsctl_snapdir {
103	zfsctl_node_t	sd_node;
104	kmutex_t	sd_lock;
105	avl_tree_t	sd_snaps;
106} zfsctl_snapdir_t;
107
108/*
109 * Root directory elements.  We have only a single static entry, 'snapshot'.
110 */
111static gfs_dirent_t zfsctl_root_entries[] = {
112	{ "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
113	{ NULL }
114};
115
116/* include . and .. in the calculation */
117#define	NROOT_ENTRIES	((sizeof (zfsctl_root_entries) / \
118    sizeof (gfs_dirent_t)) + 1)
119
120
121/*
122 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
123 * directories.  This is called from the ZFS init routine, and initializes the
124 * vnode ops vectors that we'll be using.
125 */
126void
127zfsctl_init(void)
128{
129}
130
131void
132zfsctl_fini(void)
133{
134}
135
136/*
137 * Return the inode number associated with the 'snapshot' directory.
138 */
139/* ARGSUSED */
140static ino64_t
141zfsctl_root_inode_cb(vnode_t *vp, int index)
142{
143	ASSERT(index == 0);
144	return (ZFSCTL_INO_SNAPDIR);
145}
146
147/*
148 * Create the '.zfs' directory.  This directory is cached as part of the VFS
149 * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
150 * therefore checks against a vfs_count of 2 instead of 1.  This reference
151 * is removed when the ctldir is destroyed in the unmount.
152 */
153void
154zfsctl_create(zfsvfs_t *zfsvfs)
155{
156	vnode_t *vp, *rvp;
157	zfsctl_node_t *zcp;
158
159	ASSERT(zfsvfs->z_ctldir == NULL);
160
161	vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
162	    &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
163	    zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
164	zcp = vp->v_data;
165	zcp->zc_id = ZFSCTL_INO_ROOT;
166
167	VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp, curthread) == 0);
168	ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
169	VN_URELE(rvp);
170
171	/*
172	 * We're only faking the fact that we have a root of a filesystem for
173	 * the sake of the GFS interfaces.  Undo the flag manipulation it did
174	 * for us.
175	 */
176	vp->v_vflag &= ~VV_ROOT;
177
178	zfsvfs->z_ctldir = vp;
179}
180
181/*
182 * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
183 * There might still be more references if we were force unmounted, but only
184 * new zfs_inactive() calls can occur and they don't reference .zfs
185 */
186void
187zfsctl_destroy(zfsvfs_t *zfsvfs)
188{
189	VN_RELE(zfsvfs->z_ctldir);
190	zfsvfs->z_ctldir = NULL;
191}
192
193/*
194 * Given a root znode, retrieve the associated .zfs directory.
195 * Add a hold to the vnode and return it.
196 */
197vnode_t *
198zfsctl_root(znode_t *zp)
199{
200	ASSERT(zfs_has_ctldir(zp));
201	VN_HOLD(zp->z_zfsvfs->z_ctldir);
202	return (zp->z_zfsvfs->z_ctldir);
203}
204
205/*
206 * Common open routine.  Disallow any write access.
207 */
208/* ARGSUSED */
209static int
210zfsctl_common_open(struct vop_open_args *ap)
211{
212	int flags = ap->a_mode;
213
214	if (flags & FWRITE)
215		return (EACCES);
216
217	return (0);
218}
219
220/*
221 * Common close routine.  Nothing to do here.
222 */
223/* ARGSUSED */
224static int
225zfsctl_common_close(struct vop_close_args *ap)
226{
227	return (0);
228}
229
230/*
231 * Common access routine.  Disallow writes.
232 */
233/* ARGSUSED */
234static int
235zfsctl_common_access(ap)
236	struct vop_access_args /* {
237		struct vnode *a_vp;
238		int  a_mode;
239		struct ucred *a_cred;
240		struct thread *a_td;
241	} */ *ap;
242{
243	int mode = ap->a_mode;
244
245	if (mode & VWRITE)
246		return (EACCES);
247
248	return (0);
249}
250
251/*
252 * Common getattr function.  Fill in basic information.
253 */
254static void
255zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
256{
257	zfsctl_node_t	*zcp = vp->v_data;
258	timestruc_t	now;
259
260	vap->va_uid = 0;
261	vap->va_gid = 0;
262	vap->va_rdev = 0;
263	/*
264	 * We are a purly virtual object, so we have no
265	 * blocksize or allocated blocks.
266	 */
267	vap->va_blksize = 0;
268	vap->va_nblocks = 0;
269	vap->va_seq = 0;
270	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
271	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
272	    S_IROTH | S_IXOTH;
273	vap->va_type = VDIR;
274	/*
275	 * We live in the now (for atime).
276	 */
277	gethrestime(&now);
278	vap->va_atime = now;
279	vap->va_mtime = vap->va_ctime = vap->va_birthtime = zcp->zc_cmtime;
280	/* FreeBSD: Reset chflags(2) flags. */
281	vap->va_flags = 0;
282}
283
284static int
285zfsctl_common_fid(ap)
286	struct vop_fid_args /* {
287		struct vnode *a_vp;
288		struct fid *a_fid;
289	} */ *ap;
290{
291	vnode_t		*vp = ap->a_vp;
292	fid_t		*fidp = (void *)ap->a_fid;
293	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
294	zfsctl_node_t	*zcp = vp->v_data;
295	uint64_t	object = zcp->zc_id;
296	zfid_short_t	*zfid;
297	int		i;
298
299	ZFS_ENTER(zfsvfs);
300
301	fidp->fid_len = SHORT_FID_LEN;
302
303	zfid = (zfid_short_t *)fidp;
304
305	zfid->zf_len = SHORT_FID_LEN;
306
307	for (i = 0; i < sizeof (zfid->zf_object); i++)
308		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
309
310	/* .zfs znodes always have a generation number of 0 */
311	for (i = 0; i < sizeof (zfid->zf_gen); i++)
312		zfid->zf_gen[i] = 0;
313
314	ZFS_EXIT(zfsvfs);
315	return (0);
316}
317
318static int
319zfsctl_common_reclaim(ap)
320	struct vop_reclaim_args /* {
321		struct vnode *a_vp;
322		struct thread *a_td;
323	} */ *ap;
324{
325	vnode_t *vp = ap->a_vp;
326
327	/*
328	 * Destroy the vm object and flush associated pages.
329	 */
330	vnode_destroy_vobject(vp);
331	VI_LOCK(vp);
332	vp->v_data = NULL;
333	VI_UNLOCK(vp);
334	return (0);
335}
336
337/*
338 * .zfs inode namespace
339 *
340 * We need to generate unique inode numbers for all files and directories
341 * within the .zfs pseudo-filesystem.  We use the following scheme:
342 *
343 * 	ENTRY			ZFSCTL_INODE
344 * 	.zfs			1
345 * 	.zfs/snapshot		2
346 * 	.zfs/snapshot/<snap>	objectid(snap)
347 */
348
349#define	ZFSCTL_INO_SNAP(id)	(id)
350
351/*
352 * Get root directory attributes.
353 */
354/* ARGSUSED */
355static int
356zfsctl_root_getattr(ap)
357	struct vop_getattr_args /* {
358		struct vnode *a_vp;
359		struct vattr *a_vap;
360		struct ucred *a_cred;
361	} */ *ap;
362{
363	struct vnode *vp = ap->a_vp;
364	struct vattr *vap = ap->a_vap;
365	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
366
367	ZFS_ENTER(zfsvfs);
368	vap->va_nodeid = ZFSCTL_INO_ROOT;
369	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
370
371	zfsctl_common_getattr(vp, vap);
372	ZFS_EXIT(zfsvfs);
373
374	return (0);
375}
376
377/*
378 * Special case the handling of "..".
379 */
380/* ARGSUSED */
381int
382zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
383    int flags, vnode_t *rdir, cred_t *cr)
384{
385	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
386	int err;
387
388	ZFS_ENTER(zfsvfs);
389
390	if (strcmp(nm, "..") == 0) {
391		err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp, curthread);
392		if (err == 0)
393			VOP_UNLOCK(*vpp, 0);
394	} else {
395		err = gfs_dir_lookup(dvp, nm, vpp);
396	}
397
398	ZFS_EXIT(zfsvfs);
399
400	return (err);
401}
402
403/*
404 * Special case the handling of "..".
405 */
406/* ARGSUSED */
407int
408zfsctl_root_lookup_vop(ap)
409	struct vop_lookup_args /* {
410		struct vnode *a_dvp;
411		struct vnode **a_vpp;
412		struct componentname *a_cnp;
413	} */ *ap;
414{
415	vnode_t *dvp = ap->a_dvp;
416	vnode_t **vpp = ap->a_vpp;
417	cred_t *cr = ap->a_cnp->cn_cred;
418	int flags = ap->a_cnp->cn_flags;
419	int nameiop = ap->a_cnp->cn_nameiop;
420	char nm[NAME_MAX + 1];
421	int err;
422
423	if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
424		return (EOPNOTSUPP);
425
426	ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
427	strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
428
429	err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr);
430	if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
431		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
432
433	return (err);
434}
435
436static struct vop_vector zfsctl_ops_root = {
437	.vop_default =	&default_vnodeops,
438	.vop_open =	zfsctl_common_open,
439	.vop_close =	zfsctl_common_close,
440	.vop_ioctl =	VOP_EINVAL,
441	.vop_getattr =	zfsctl_root_getattr,
442	.vop_access =	zfsctl_common_access,
443	.vop_readdir =	gfs_vop_readdir,
444	.vop_lookup =	zfsctl_root_lookup_vop,
445	.vop_inactive =	gfs_vop_inactive,
446	.vop_reclaim =	zfsctl_common_reclaim,
447	.vop_fid =	zfsctl_common_fid,
448};
449
450static int
451zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
452{
453	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
454
455	dmu_objset_name(os, zname);
456	if (strlen(zname) + 1 + strlen(name) >= len)
457		return (ENAMETOOLONG);
458	(void) strcat(zname, "@");
459	(void) strcat(zname, name);
460	return (0);
461}
462
463static int
464zfsctl_unmount_snap(vnode_t *dvp, const char *name, int force, cred_t *cr)
465{
466	zfsctl_snapdir_t *sdp = dvp->v_data;
467	zfs_snapentry_t search, *sep;
468	struct vop_inactive_args ap;
469	avl_index_t where;
470	int err;
471
472	ASSERT(MUTEX_HELD(&sdp->sd_lock));
473
474	search.se_name = (char *)name;
475	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL)
476		return (ENOENT);
477
478	ASSERT(vn_ismntpt(sep->se_root));
479
480	/* this will be dropped by dounmount() */
481	if ((err = vn_vfswlock(sep->se_root)) != 0)
482		return (err);
483
484	err = dounmount(vn_mountedvfs(sep->se_root), force, curthread);
485	if (err)
486		return (err);
487	ASSERT(sep->se_root->v_count == 1);
488	ap.a_vp = sep->se_root;
489	gfs_vop_inactive(&ap);
490
491	avl_remove(&sdp->sd_snaps, sep);
492	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
493	kmem_free(sep, sizeof (zfs_snapentry_t));
494
495	return (0);
496}
497
498#if 0
499static void
500zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
501{
502	avl_index_t where;
503	vfs_t *vfsp;
504	refstr_t *pathref;
505	char newpath[MAXNAMELEN];
506	char *tail;
507
508	ASSERT(MUTEX_HELD(&sdp->sd_lock));
509	ASSERT(sep != NULL);
510
511	vfsp = vn_mountedvfs(sep->se_root);
512	ASSERT(vfsp != NULL);
513
514	vfs_lock_wait(vfsp);
515
516	/*
517	 * Change the name in the AVL tree.
518	 */
519	avl_remove(&sdp->sd_snaps, sep);
520	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
521	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
522	(void) strcpy(sep->se_name, nm);
523	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
524	avl_insert(&sdp->sd_snaps, sep, where);
525
526	/*
527	 * Change the current mountpoint info:
528	 * 	- update the tail of the mntpoint path
529	 *	- update the tail of the resource path
530	 */
531	pathref = vfs_getmntpoint(vfsp);
532	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
533	VERIFY((tail = strrchr(newpath, '/')) != NULL);
534	*(tail+1) = '\0';
535	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
536	(void) strcat(newpath, nm);
537	refstr_rele(pathref);
538	vfs_setmntpoint(vfsp, newpath);
539
540	pathref = vfs_getresource(vfsp);
541	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
542	VERIFY((tail = strrchr(newpath, '@')) != NULL);
543	*(tail+1) = '\0';
544	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
545	(void) strcat(newpath, nm);
546	refstr_rele(pathref);
547	vfs_setresource(vfsp, newpath);
548
549	vfs_unlock(vfsp);
550}
551#endif
552
553#if 0
554static int
555zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
556    cred_t *cr)
557{
558	zfsctl_snapdir_t *sdp = sdvp->v_data;
559	zfs_snapentry_t search, *sep;
560	avl_index_t where;
561	char from[MAXNAMELEN], to[MAXNAMELEN];
562	int err;
563
564	err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
565	if (err)
566		return (err);
567	err = zfs_secpolicy_write(from, cr);
568	if (err)
569		return (err);
570
571	/*
572	 * Cannot move snapshots out of the snapdir.
573	 */
574	if (sdvp != tdvp)
575		return (EINVAL);
576
577	if (strcmp(snm, tnm) == 0)
578		return (0);
579
580	err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
581	if (err)
582		return (err);
583
584	mutex_enter(&sdp->sd_lock);
585
586	search.se_name = (char *)snm;
587	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
588		mutex_exit(&sdp->sd_lock);
589		return (ENOENT);
590	}
591
592	err = dmu_objset_rename(from, to, B_FALSE);
593	if (err == 0)
594		zfsctl_rename_snap(sdp, sep, tnm);
595
596	mutex_exit(&sdp->sd_lock);
597
598	return (err);
599}
600#endif
601
602#if 0
603/* ARGSUSED */
604static int
605zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
606{
607	zfsctl_snapdir_t *sdp = dvp->v_data;
608	char snapname[MAXNAMELEN];
609	int err;
610
611	err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
612	if (err)
613		return (err);
614	err = zfs_secpolicy_write(snapname, cr);
615	if (err)
616		return (err);
617
618	mutex_enter(&sdp->sd_lock);
619
620	err = zfsctl_unmount_snap(dvp, name, 0, cr);
621	if (err) {
622		mutex_exit(&sdp->sd_lock);
623		return (err);
624	}
625
626	err = dmu_objset_destroy(snapname);
627
628	mutex_exit(&sdp->sd_lock);
629
630	return (err);
631}
632#endif
633
634/*
635 * Lookup entry point for the 'snapshot' directory.  Try to open the
636 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
637 * Perform a mount of the associated dataset on top of the vnode.
638 */
639/* ARGSUSED */
640int
641zfsctl_snapdir_lookup(ap)
642	struct vop_lookup_args /* {
643		struct vnode *a_dvp;
644		struct vnode **a_vpp;
645		struct componentname *a_cnp;
646	} */ *ap;
647{
648	vnode_t *dvp = ap->a_dvp;
649	vnode_t **vpp = ap->a_vpp;
650	char nm[NAME_MAX + 1];
651	zfsctl_snapdir_t *sdp = dvp->v_data;
652	objset_t *snap;
653	char snapname[MAXNAMELEN];
654	char *mountpoint;
655	zfs_snapentry_t *sep, search;
656	size_t mountpoint_len;
657	avl_index_t where;
658	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
659	int err;
660
661	ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
662	strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
663
664	ASSERT(dvp->v_type == VDIR);
665
666	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
667		return (0);
668
669	*vpp = NULL;
670
671	/*
672	 * If we get a recursive call, that means we got called
673	 * from the domount() code while it was trying to look up the
674	 * spec (which looks like a local path for zfs).  We need to
675	 * add some flag to domount() to tell it not to do this lookup.
676	 */
677	if (MUTEX_HELD(&sdp->sd_lock))
678		return (ENOENT);
679
680	ZFS_ENTER(zfsvfs);
681
682	mutex_enter(&sdp->sd_lock);
683	search.se_name = (char *)nm;
684	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
685		*vpp = sep->se_root;
686		VN_HOLD(*vpp);
687		if ((*vpp)->v_mountedhere == NULL) {
688			/*
689			 * The snapshot was unmounted behind our backs,
690			 * try to remount it.
691			 */
692			goto domount;
693		}
694		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
695		mutex_exit(&sdp->sd_lock);
696		ZFS_EXIT(zfsvfs);
697		return (0);
698	}
699
700	/*
701	 * The requested snapshot is not currently mounted, look it up.
702	 */
703	err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
704	if (err) {
705		mutex_exit(&sdp->sd_lock);
706		ZFS_EXIT(zfsvfs);
707		return (err);
708	}
709	if (dmu_objset_open(snapname, DMU_OST_ZFS,
710	    DS_MODE_STANDARD | DS_MODE_READONLY, &snap) != 0) {
711		mutex_exit(&sdp->sd_lock);
712		ZFS_EXIT(zfsvfs);
713		return (ENOENT);
714	}
715
716	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
717	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
718	(void) strcpy(sep->se_name, nm);
719	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
720	VN_HOLD(*vpp);
721	avl_insert(&sdp->sd_snaps, sep, where);
722
723	dmu_objset_close(snap);
724domount:
725	mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
726	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
727	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
728	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
729	    dvp->v_vfsp->mnt_stat.f_mntonname, nm);
730	err = domount(curthread, *vpp, "zfs", mountpoint, snapname, 0);
731	kmem_free(mountpoint, mountpoint_len);
732	/* FreeBSD: This line was moved from below to avoid a lock recursion. */
733	if (err == 0)
734		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
735	mutex_exit(&sdp->sd_lock);
736
737	/*
738	 * If we had an error, drop our hold on the vnode and
739	 * zfsctl_snapshot_inactive() will clean up.
740	 */
741	if (err) {
742		VN_RELE(*vpp);
743		*vpp = NULL;
744	}
745	return (err);
746}
747
748/* ARGSUSED */
749static int
750zfsctl_snapdir_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp,
751    offset_t *offp, offset_t *nextp, void *data)
752{
753	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
754	char snapname[MAXNAMELEN];
755	uint64_t id, cookie;
756
757	ZFS_ENTER(zfsvfs);
758
759	cookie = *offp;
760	if (dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
761	    &cookie) == ENOENT) {
762		*eofp = 1;
763		ZFS_EXIT(zfsvfs);
764		return (0);
765	}
766
767	(void) strcpy(dp->d_name, snapname);
768	dp->d_ino = ZFSCTL_INO_SNAP(id);
769	*nextp = cookie;
770
771	ZFS_EXIT(zfsvfs);
772
773	return (0);
774}
775
776vnode_t *
777zfsctl_mknode_snapdir(vnode_t *pvp)
778{
779	vnode_t *vp;
780	zfsctl_snapdir_t *sdp;
781
782	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
783	    &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
784	    zfsctl_snapdir_readdir_cb, NULL);
785	sdp = vp->v_data;
786	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
787	sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
788	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
789	avl_create(&sdp->sd_snaps, snapentry_compare,
790	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
791	return (vp);
792}
793
794/* ARGSUSED */
795static int
796zfsctl_snapdir_getattr(ap)
797	struct vop_getattr_args /* {
798		struct vnode *a_vp;
799		struct vattr *a_vap;
800		struct ucred *a_cred;
801	} */ *ap;
802{
803	struct vnode *vp = ap->a_vp;
804	struct vattr *vap = ap->a_vap;
805	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
806	zfsctl_snapdir_t *sdp = vp->v_data;
807
808	ZFS_ENTER(zfsvfs);
809	zfsctl_common_getattr(vp, vap);
810	vap->va_nodeid = gfs_file_inode(vp);
811	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
812	ZFS_EXIT(zfsvfs);
813
814	return (0);
815}
816
817/* ARGSUSED */
818static int
819zfsctl_snapdir_inactive(ap)
820	struct vop_inactive_args /* {
821		struct vnode *a_vp;
822		struct thread *a_td;
823	} */ *ap;
824{
825	vnode_t *vp = ap->a_vp;
826	zfsctl_snapdir_t *sdp = vp->v_data;
827	void *private;
828
829	private = gfs_dir_inactive(vp);
830	if (private != NULL) {
831		ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
832		mutex_destroy(&sdp->sd_lock);
833		avl_destroy(&sdp->sd_snaps);
834		kmem_free(private, sizeof (zfsctl_snapdir_t));
835	}
836	return (0);
837}
838
839static struct vop_vector zfsctl_ops_snapdir = {
840	.vop_default =	&default_vnodeops,
841	.vop_open =	zfsctl_common_open,
842	.vop_close =	zfsctl_common_close,
843	.vop_ioctl =	VOP_EINVAL,
844	.vop_getattr =	zfsctl_snapdir_getattr,
845	.vop_access =	zfsctl_common_access,
846	.vop_readdir =	gfs_vop_readdir,
847	.vop_lookup =	zfsctl_snapdir_lookup,
848	.vop_inactive =	zfsctl_snapdir_inactive,
849	.vop_reclaim =	zfsctl_common_reclaim,
850	.vop_fid =	zfsctl_common_fid,
851};
852
853static vnode_t *
854zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
855{
856	vnode_t *vp;
857	zfsctl_node_t *zcp;
858
859	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
860	    &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
861	zcp = vp->v_data;
862	zcp->zc_id = objset;
863
864	return (vp);
865}
866
867static int
868zfsctl_snapshot_inactive(ap)
869	struct vop_inactive_args /* {
870		struct vnode *a_vp;
871		struct thread *a_td;
872	} */ *ap;
873{
874	vnode_t *vp = ap->a_vp;
875	struct vop_inactive_args iap;
876	zfsctl_snapdir_t *sdp;
877	zfs_snapentry_t *sep, *next;
878	int locked;
879	vnode_t *dvp;
880
881	VERIFY(gfs_dir_lookup(vp, "..", &dvp) == 0);
882	sdp = dvp->v_data;
883	VOP_UNLOCK(dvp, 0);
884
885	if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
886		mutex_enter(&sdp->sd_lock);
887
888	if (vp->v_count > 1) {
889		if (!locked)
890			mutex_exit(&sdp->sd_lock);
891		return (0);
892	}
893	ASSERT(!vn_ismntpt(vp));
894
895	sep = avl_first(&sdp->sd_snaps);
896	while (sep != NULL) {
897		next = AVL_NEXT(&sdp->sd_snaps, sep);
898
899		if (sep->se_root == vp) {
900			avl_remove(&sdp->sd_snaps, sep);
901			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
902			kmem_free(sep, sizeof (zfs_snapentry_t));
903			break;
904		}
905		sep = next;
906	}
907	ASSERT(sep != NULL);
908
909	if (!locked)
910		mutex_exit(&sdp->sd_lock);
911	VN_RELE(dvp);
912
913	/*
914	 * Dispose of the vnode for the snapshot mount point.
915	 * This is safe to do because once this entry has been removed
916	 * from the AVL tree, it can't be found again, so cannot become
917	 * "active".  If we lookup the same name again we will end up
918	 * creating a new vnode.
919	 */
920	iap.a_vp = vp;
921	return (gfs_vop_inactive(&iap));
922}
923
924static int
925zfsctl_traverse_begin(vnode_t **vpp, int lktype)
926{
927
928	VN_HOLD(*vpp);
929
930	/* Snapshot should be already mounted, but just in case. */
931	if (vn_mountedvfs(*vpp) == NULL)
932		return (ENOENT);
933	return (traverse(vpp, lktype));
934}
935
936static void
937zfsctl_traverse_end(vnode_t *vp, int err)
938{
939
940	if (err == 0)
941		vput(vp);
942	else
943		VN_RELE(vp);
944}
945
946static int
947zfsctl_snapshot_getattr(ap)
948	struct vop_getattr_args /* {
949		struct vnode *a_vp;
950		struct vattr *a_vap;
951		struct ucred *a_cred;
952	} */ *ap;
953{
954	vnode_t *vp = ap->a_vp;
955	int err;
956
957	err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
958	if (err == 0)
959		err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
960	zfsctl_traverse_end(vp, err);
961	return (err);
962}
963
964static int
965zfsctl_snapshot_fid(ap)
966	struct vop_fid_args /* {
967		struct vnode *a_vp;
968		struct fid *a_fid;
969	} */ *ap;
970{
971	vnode_t *vp = ap->a_vp;
972	int err;
973
974	err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
975	if (err == 0)
976		err = VOP_VPTOFH(vp, (void *)ap->a_fid);
977	zfsctl_traverse_end(vp, err);
978	return (err);
979}
980
981/*
982 * These VP's should never see the light of day.  They should always
983 * be covered.
984 */
985static struct vop_vector zfsctl_ops_snapshot = {
986	.vop_default =	&default_vnodeops,
987	.vop_inactive =	zfsctl_snapshot_inactive,
988	.vop_reclaim =	zfsctl_common_reclaim,
989	.vop_getattr =	zfsctl_snapshot_getattr,
990	.vop_fid =	zfsctl_snapshot_fid,
991};
992
993int
994zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
995{
996	zfsvfs_t *zfsvfs = vfsp->vfs_data;
997	vnode_t *dvp, *vp;
998	zfsctl_snapdir_t *sdp;
999	zfsctl_node_t *zcp;
1000	zfs_snapentry_t *sep;
1001	int error;
1002
1003	ASSERT(zfsvfs->z_ctldir != NULL);
1004	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1005	    NULL, 0, NULL, kcred);
1006	if (error != 0)
1007		return (error);
1008	sdp = dvp->v_data;
1009
1010	mutex_enter(&sdp->sd_lock);
1011	sep = avl_first(&sdp->sd_snaps);
1012	while (sep != NULL) {
1013		vp = sep->se_root;
1014		zcp = vp->v_data;
1015		if (zcp->zc_id == objsetid)
1016			break;
1017
1018		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1019	}
1020
1021	if (sep != NULL) {
1022		VN_HOLD(vp);
1023		error = traverse(&vp, LK_SHARED | LK_RETRY);
1024		if (error == 0) {
1025			if (vp == sep->se_root)
1026				error = EINVAL;
1027			else
1028				*zfsvfsp = VTOZ(vp)->z_zfsvfs;
1029		}
1030		mutex_exit(&sdp->sd_lock);
1031		if (error == 0)
1032			VN_URELE(vp);
1033		else
1034			VN_RELE(vp);
1035	} else {
1036		error = EINVAL;
1037		mutex_exit(&sdp->sd_lock);
1038	}
1039
1040	VN_RELE(dvp);
1041
1042	return (error);
1043}
1044
1045/*
1046 * Unmount any snapshots for the given filesystem.  This is called from
1047 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1048 * snapshots.
1049 */
1050int
1051zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1052{
1053	struct vop_inactive_args ap;
1054	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1055	vnode_t *dvp, *svp;
1056	zfsctl_snapdir_t *sdp;
1057	zfs_snapentry_t *sep, *next;
1058	int error;
1059
1060	ASSERT(zfsvfs->z_ctldir != NULL);
1061	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1062	    NULL, 0, NULL, cr);
1063	if (error != 0)
1064		return (error);
1065	sdp = dvp->v_data;
1066
1067	mutex_enter(&sdp->sd_lock);
1068
1069	sep = avl_first(&sdp->sd_snaps);
1070	while (sep != NULL) {
1071		svp = sep->se_root;
1072		next = AVL_NEXT(&sdp->sd_snaps, sep);
1073
1074		/*
1075		 * If this snapshot is not mounted, then it must
1076		 * have just been unmounted by somebody else, and
1077		 * will be cleaned up by zfsctl_snapdir_inactive().
1078		 */
1079		if (vn_ismntpt(svp)) {
1080			if ((error = vn_vfswlock(svp)) != 0)
1081				goto out;
1082
1083			/*
1084			 * Increase usecount, so dounmount() won't vrele() it
1085			 * to 0 and call zfsctl_snapdir_inactive().
1086			 */
1087			VN_HOLD(svp);
1088			vfsp = vn_mountedvfs(svp);
1089			mtx_lock(&Giant);
1090			error = dounmount(vfsp, fflags, curthread);
1091			mtx_unlock(&Giant);
1092			if (error != 0) {
1093				VN_RELE(svp);
1094				goto out;
1095			}
1096
1097			avl_remove(&sdp->sd_snaps, sep);
1098			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1099			kmem_free(sep, sizeof (zfs_snapentry_t));
1100
1101			/*
1102			 * We can't use VN_RELE(), as that will try to
1103			 * invoke zfsctl_snapdir_inactive(), and that
1104			 * would lead to an attempt to re-grab the sd_lock.
1105			 */
1106			ASSERT3U(svp->v_count, ==, 1);
1107			ap.a_vp = svp;
1108			gfs_vop_inactive(&ap);
1109		}
1110		sep = next;
1111	}
1112out:
1113	mutex_exit(&sdp->sd_lock);
1114	VN_RELE(dvp);
1115
1116	return (error);
1117}
1118