zfs_ctldir.c revision 302762
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
25 */
26
27/*
28 * ZFS control directory (a.k.a. ".zfs")
29 *
30 * This directory provides a common location for all ZFS meta-objects.
31 * Currently, this is only the 'snapshot' directory, but this may expand in the
32 * future.  The elements are built using the GFS primitives, as the hierarchy
33 * does not actually exist on disk.
34 *
35 * For 'snapshot', we don't want to have all snapshots always mounted, because
36 * this would take up a huge amount of space in /etc/mnttab.  We have three
37 * types of objects:
38 *
39 * 	ctldir ------> snapshotdir -------> snapshot
40 *                                             |
41 *                                             |
42 *                                             V
43 *                                         mounted fs
44 *
45 * The 'snapshot' node contains just enough information to lookup '..' and act
46 * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
47 * perform an automount of the underlying filesystem and return the
48 * corresponding vnode.
49 *
50 * All mounts are handled automatically by the kernel, but unmounts are
51 * (currently) handled from user land.  The main reason is that there is no
52 * reliable way to auto-unmount the filesystem when it's "no longer in use".
53 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
54 * unmounts any snapshots within the snapshot directory.
55 *
56 * The '.zfs', '.zfs/snapshot', and all directories created under
57 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
58 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
59 *
60 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
61 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
62 * However, vnodes within these mounted on file systems have their v_vfsp
63 * fields set to the head filesystem to make NFS happy (see
64 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
65 * so that it cannot be freed until all snapshots have been unmounted.
66 */
67
68#include <sys/zfs_context.h>
69#include <sys/zfs_ctldir.h>
70#include <sys/zfs_ioctl.h>
71#include <sys/zfs_vfsops.h>
72#include <sys/namei.h>
73#include <sys/gfs.h>
74#include <sys/stat.h>
75#include <sys/dmu.h>
76#include <sys/dsl_destroy.h>
77#include <sys/dsl_deleg.h>
78#include <sys/mount.h>
79#include <sys/sunddi.h>
80
81#include "zfs_namecheck.h"
82
83typedef struct zfsctl_node {
84	gfs_dir_t	zc_gfs_private;
85	uint64_t	zc_id;
86	timestruc_t	zc_cmtime;	/* ctime and mtime, always the same */
87} zfsctl_node_t;
88
89typedef struct zfsctl_snapdir {
90	zfsctl_node_t	sd_node;
91	kmutex_t	sd_lock;
92	avl_tree_t	sd_snaps;
93} zfsctl_snapdir_t;
94
95typedef struct {
96	char		*se_name;
97	vnode_t		*se_root;
98	avl_node_t	se_node;
99} zfs_snapentry_t;
100
101static int
102snapentry_compare(const void *a, const void *b)
103{
104	const zfs_snapentry_t *sa = a;
105	const zfs_snapentry_t *sb = b;
106	int ret = strcmp(sa->se_name, sb->se_name);
107
108	if (ret < 0)
109		return (-1);
110	else if (ret > 0)
111		return (1);
112	else
113		return (0);
114}
115
116#ifdef illumos
117vnodeops_t *zfsctl_ops_root;
118vnodeops_t *zfsctl_ops_snapdir;
119vnodeops_t *zfsctl_ops_snapshot;
120vnodeops_t *zfsctl_ops_shares;
121vnodeops_t *zfsctl_ops_shares_dir;
122
123static const fs_operation_def_t zfsctl_tops_root[];
124static const fs_operation_def_t zfsctl_tops_snapdir[];
125static const fs_operation_def_t zfsctl_tops_snapshot[];
126static const fs_operation_def_t zfsctl_tops_shares[];
127#else
128static struct vop_vector zfsctl_ops_root;
129static struct vop_vector zfsctl_ops_snapdir;
130static struct vop_vector zfsctl_ops_snapshot;
131static struct vop_vector zfsctl_ops_shares;
132static struct vop_vector zfsctl_ops_shares_dir;
133#endif
134
135static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
136static vnode_t *zfsctl_mknode_shares(vnode_t *);
137static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
138static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
139
140#ifdef illumos
141static gfs_opsvec_t zfsctl_opsvec[] = {
142	{ ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
143	{ ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
144	{ ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
145	{ ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
146	{ ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
147	{ NULL }
148};
149#endif
150
151/*
152 * Root directory elements.  We only have two entries
153 * snapshot and shares.
154 */
155static gfs_dirent_t zfsctl_root_entries[] = {
156	{ "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
157	{ "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
158	{ NULL }
159};
160
161/* include . and .. in the calculation */
162#define	NROOT_ENTRIES	((sizeof (zfsctl_root_entries) / \
163    sizeof (gfs_dirent_t)) + 1)
164
165
166/*
167 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
168 * directories.  This is called from the ZFS init routine, and initializes the
169 * vnode ops vectors that we'll be using.
170 */
171void
172zfsctl_init(void)
173{
174#ifdef illumos
175	VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
176#endif
177}
178
179void
180zfsctl_fini(void)
181{
182#ifdef illumos
183	/*
184	 * Remove vfsctl vnode ops
185	 */
186	if (zfsctl_ops_root)
187		vn_freevnodeops(zfsctl_ops_root);
188	if (zfsctl_ops_snapdir)
189		vn_freevnodeops(zfsctl_ops_snapdir);
190	if (zfsctl_ops_snapshot)
191		vn_freevnodeops(zfsctl_ops_snapshot);
192	if (zfsctl_ops_shares)
193		vn_freevnodeops(zfsctl_ops_shares);
194	if (zfsctl_ops_shares_dir)
195		vn_freevnodeops(zfsctl_ops_shares_dir);
196
197	zfsctl_ops_root = NULL;
198	zfsctl_ops_snapdir = NULL;
199	zfsctl_ops_snapshot = NULL;
200	zfsctl_ops_shares = NULL;
201	zfsctl_ops_shares_dir = NULL;
202#endif	/* illumos */
203}
204
205boolean_t
206zfsctl_is_node(vnode_t *vp)
207{
208	return (vn_matchops(vp, zfsctl_ops_root) ||
209	    vn_matchops(vp, zfsctl_ops_snapdir) ||
210	    vn_matchops(vp, zfsctl_ops_snapshot) ||
211	    vn_matchops(vp, zfsctl_ops_shares) ||
212	    vn_matchops(vp, zfsctl_ops_shares_dir));
213
214}
215
216/*
217 * Return the inode number associated with the 'snapshot' or
218 * 'shares' directory.
219 */
220/* ARGSUSED */
221static ino64_t
222zfsctl_root_inode_cb(vnode_t *vp, int index)
223{
224	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
225
226	ASSERT(index < 2);
227
228	if (index == 0)
229		return (ZFSCTL_INO_SNAPDIR);
230
231	return (zfsvfs->z_shares_dir);
232}
233
234/*
235 * Create the '.zfs' directory.  This directory is cached as part of the VFS
236 * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
237 * therefore checks against a vfs_count of 2 instead of 1.  This reference
238 * is removed when the ctldir is destroyed in the unmount.
239 */
240void
241zfsctl_create(zfsvfs_t *zfsvfs)
242{
243	vnode_t *vp, *rvp;
244	zfsctl_node_t *zcp;
245	uint64_t crtime[2];
246
247	ASSERT(zfsvfs->z_ctldir == NULL);
248
249	vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
250	    &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
251	    zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
252	zcp = vp->v_data;
253	zcp->zc_id = ZFSCTL_INO_ROOT;
254
255	VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
256	VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
257	    &crtime, sizeof (crtime)));
258	ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
259	VN_URELE(rvp);
260
261	/*
262	 * We're only faking the fact that we have a root of a filesystem for
263	 * the sake of the GFS interfaces.  Undo the flag manipulation it did
264	 * for us.
265	 */
266	vp->v_vflag &= ~VV_ROOT;
267
268	zfsvfs->z_ctldir = vp;
269
270	VOP_UNLOCK(vp, 0);
271}
272
273/*
274 * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
275 * There might still be more references if we were force unmounted, but only
276 * new zfs_inactive() calls can occur and they don't reference .zfs
277 */
278void
279zfsctl_destroy(zfsvfs_t *zfsvfs)
280{
281	VN_RELE(zfsvfs->z_ctldir);
282	zfsvfs->z_ctldir = NULL;
283}
284
285/*
286 * Given a root znode, retrieve the associated .zfs directory.
287 * Add a hold to the vnode and return it.
288 */
289vnode_t *
290zfsctl_root(znode_t *zp)
291{
292	ASSERT(zfs_has_ctldir(zp));
293	VN_HOLD(zp->z_zfsvfs->z_ctldir);
294	return (zp->z_zfsvfs->z_ctldir);
295}
296
297static int
298zfsctl_common_print(ap)
299	struct vop_print_args /* {
300		struct vnode *a_vp;
301	} */ *ap;
302{
303	vnode_t *vp = ap->a_vp;
304	gfs_file_t *fp = vp->v_data;
305
306	printf("    parent = %p\n", fp->gfs_parent);
307	printf("    type = %d\n", fp->gfs_type);
308	printf("    index = %d\n", fp->gfs_index);
309	printf("    ino = %ju\n", (uintmax_t)fp->gfs_ino);
310	return (0);
311}
312
313/*
314 * Common open routine.  Disallow any write access.
315 */
316/* ARGSUSED */
317static int
318zfsctl_common_open(struct vop_open_args *ap)
319{
320	int flags = ap->a_mode;
321
322	if (flags & FWRITE)
323		return (SET_ERROR(EACCES));
324
325	return (0);
326}
327
328/*
329 * Common close routine.  Nothing to do here.
330 */
331/* ARGSUSED */
332static int
333zfsctl_common_close(struct vop_close_args *ap)
334{
335	return (0);
336}
337
338/*
339 * Common access routine.  Disallow writes.
340 */
341/* ARGSUSED */
342static int
343zfsctl_common_access(ap)
344	struct vop_access_args /* {
345		struct vnode *a_vp;
346		accmode_t a_accmode;
347		struct ucred *a_cred;
348		struct thread *a_td;
349	} */ *ap;
350{
351	accmode_t accmode = ap->a_accmode;
352
353#ifdef TODO
354	if (flags & V_ACE_MASK) {
355		if (accmode & ACE_ALL_WRITE_PERMS)
356			return (SET_ERROR(EACCES));
357	} else {
358#endif
359		if (accmode & VWRITE)
360			return (SET_ERROR(EACCES));
361#ifdef TODO
362	}
363#endif
364
365	return (0);
366}
367
368/*
369 * Common getattr function.  Fill in basic information.
370 */
371static void
372zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
373{
374	timestruc_t	now;
375
376	vap->va_uid = 0;
377	vap->va_gid = 0;
378	vap->va_rdev = 0;
379	/*
380	 * We are a purely virtual object, so we have no
381	 * blocksize or allocated blocks.
382	 */
383	vap->va_blksize = 0;
384	vap->va_nblocks = 0;
385	vap->va_seq = 0;
386	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
387	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
388	    S_IROTH | S_IXOTH;
389	vap->va_type = VDIR;
390	/*
391	 * We live in the now (for atime).
392	 */
393	gethrestime(&now);
394	vap->va_atime = now;
395	/* FreeBSD: Reset chflags(2) flags. */
396	vap->va_flags = 0;
397}
398
399/*ARGSUSED*/
400static int
401zfsctl_common_fid(ap)
402	struct vop_fid_args /* {
403		struct vnode *a_vp;
404		struct fid *a_fid;
405	} */ *ap;
406{
407	vnode_t		*vp = ap->a_vp;
408	fid_t		*fidp = (void *)ap->a_fid;
409	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
410	zfsctl_node_t	*zcp = vp->v_data;
411	uint64_t	object = zcp->zc_id;
412	zfid_short_t	*zfid;
413	int		i;
414
415	ZFS_ENTER(zfsvfs);
416
417#ifdef illumos
418	if (fidp->fid_len < SHORT_FID_LEN) {
419		fidp->fid_len = SHORT_FID_LEN;
420		ZFS_EXIT(zfsvfs);
421		return (SET_ERROR(ENOSPC));
422	}
423#endif
424
425	zfid = (zfid_short_t *)fidp;
426
427	zfid->zf_len = SHORT_FID_LEN;
428
429	for (i = 0; i < sizeof (zfid->zf_object); i++)
430		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
431
432	/* .zfs znodes always have a generation number of 0 */
433	for (i = 0; i < sizeof (zfid->zf_gen); i++)
434		zfid->zf_gen[i] = 0;
435
436	ZFS_EXIT(zfsvfs);
437	return (0);
438}
439
440
441/*ARGSUSED*/
442static int
443zfsctl_shares_fid(ap)
444	struct vop_fid_args /* {
445		struct vnode *a_vp;
446		struct fid *a_fid;
447	} */ *ap;
448{
449	vnode_t		*vp = ap->a_vp;
450	fid_t		*fidp = (void *)ap->a_fid;
451	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
452	znode_t		*dzp;
453	int		error;
454
455	ZFS_ENTER(zfsvfs);
456
457	if (zfsvfs->z_shares_dir == 0) {
458		ZFS_EXIT(zfsvfs);
459		return (SET_ERROR(ENOTSUP));
460	}
461
462	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
463		error = VOP_FID(ZTOV(dzp), fidp);
464		VN_RELE(ZTOV(dzp));
465	}
466
467	ZFS_EXIT(zfsvfs);
468	return (error);
469}
470
471/*
472 * .zfs inode namespace
473 *
474 * We need to generate unique inode numbers for all files and directories
475 * within the .zfs pseudo-filesystem.  We use the following scheme:
476 *
477 * 	ENTRY			ZFSCTL_INODE
478 * 	.zfs			1
479 * 	.zfs/snapshot		2
480 * 	.zfs/snapshot/<snap>	objectid(snap)
481 */
482
483#define	ZFSCTL_INO_SNAP(id)	(id)
484
485/*
486 * Get root directory attributes.
487 */
488/* ARGSUSED */
489static int
490zfsctl_root_getattr(ap)
491	struct vop_getattr_args /* {
492		struct vnode *a_vp;
493		struct vattr *a_vap;
494		struct ucred *a_cred;
495	} */ *ap;
496{
497	struct vnode *vp = ap->a_vp;
498	struct vattr *vap = ap->a_vap;
499	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
500	zfsctl_node_t *zcp = vp->v_data;
501
502	ZFS_ENTER(zfsvfs);
503	vap->va_nodeid = ZFSCTL_INO_ROOT;
504	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
505	vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
506	vap->va_birthtime = vap->va_ctime;
507
508	zfsctl_common_getattr(vp, vap);
509	ZFS_EXIT(zfsvfs);
510
511	return (0);
512}
513
514/*
515 * Special case the handling of "..".
516 */
517/* ARGSUSED */
518int
519zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
520    int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
521    int *direntflags, pathname_t *realpnp)
522{
523	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
524	int err;
525
526	/*
527	 * No extended attributes allowed under .zfs
528	 */
529	if (flags & LOOKUP_XATTR)
530		return (SET_ERROR(EINVAL));
531
532	ZFS_ENTER(zfsvfs);
533
534	if (strcmp(nm, "..") == 0) {
535#ifdef illumos
536		err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
537#else
538		/*
539		 * NB: can not use VFS_ROOT here as it would acquire
540		 * the vnode lock of the parent (root) vnode while
541		 * holding the child's (.zfs) lock.
542		 */
543		znode_t *rootzp;
544
545		err = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
546		if (err == 0)
547			*vpp = ZTOV(rootzp);
548#endif
549	} else {
550		err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
551		    cr, ct, direntflags, realpnp);
552	}
553
554	ZFS_EXIT(zfsvfs);
555
556	return (err);
557}
558
559static int
560zfsctl_root_print(ap)
561	struct vop_print_args /* {
562		struct vnode *a_vp;
563	} */ *ap;
564{
565	printf("    .zfs node\n");
566	zfsctl_common_print(ap);
567	return (0);
568}
569
570#ifdef illumos
571static int
572zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
573    caller_context_t *ct)
574{
575	/*
576	 * We only care about ACL_ENABLED so that libsec can
577	 * display ACL correctly and not default to POSIX draft.
578	 */
579	if (cmd == _PC_ACL_ENABLED) {
580		*valp = _ACL_ACE_ENABLED;
581		return (0);
582	}
583
584	return (fs_pathconf(vp, cmd, valp, cr, ct));
585}
586#endif	/* illumos */
587
588#ifdef illumos
589static const fs_operation_def_t zfsctl_tops_root[] = {
590	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
591	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
592	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
593	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_root_getattr }	},
594	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
595	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } 	},
596	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_root_lookup }	},
597	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
598	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive }	},
599	{ VOPNAME_PATHCONF,	{ .vop_pathconf = zfsctl_pathconf }	},
600	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid	}	},
601	{ NULL }
602};
603#endif	/* illumos */
604
605/*
606 * Special case the handling of "..".
607 */
608/* ARGSUSED */
609int
610zfsctl_freebsd_root_lookup(ap)
611	struct vop_lookup_args /* {
612		struct vnode *a_dvp;
613		struct vnode **a_vpp;
614		struct componentname *a_cnp;
615	} */ *ap;
616{
617	vnode_t *dvp = ap->a_dvp;
618	vnode_t **vpp = ap->a_vpp;
619	cred_t *cr = ap->a_cnp->cn_cred;
620	int flags = ap->a_cnp->cn_flags;
621	int lkflags = ap->a_cnp->cn_lkflags;
622	int nameiop = ap->a_cnp->cn_nameiop;
623	char nm[NAME_MAX + 1];
624	int err;
625
626	if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
627		return (EOPNOTSUPP);
628
629	ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
630	strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
631relookup:
632	err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
633	if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) {
634		if (flags & ISDOTDOT) {
635			VOP_UNLOCK(dvp, 0);
636			err = vn_lock(*vpp, lkflags);
637			if (err != 0) {
638				vrele(*vpp);
639				*vpp = NULL;
640			}
641			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
642		} else {
643			err = vn_lock(*vpp, LK_EXCLUSIVE);
644			if (err != 0) {
645				VERIFY3S(err, ==, ENOENT);
646				goto relookup;
647			}
648		}
649	}
650	return (err);
651}
652
653static struct vop_vector zfsctl_ops_root = {
654	.vop_default =	&default_vnodeops,
655	.vop_open =	zfsctl_common_open,
656	.vop_close =	zfsctl_common_close,
657	.vop_ioctl =	VOP_EINVAL,
658	.vop_getattr =	zfsctl_root_getattr,
659	.vop_access =	zfsctl_common_access,
660	.vop_readdir =	gfs_vop_readdir,
661	.vop_lookup =	zfsctl_freebsd_root_lookup,
662	.vop_inactive =	VOP_NULL,
663	.vop_reclaim =	gfs_vop_reclaim,
664#ifdef TODO
665	.vop_pathconf =	zfsctl_pathconf,
666#endif
667	.vop_fid =	zfsctl_common_fid,
668	.vop_print =	zfsctl_root_print,
669};
670
671/*
672 * Gets the full dataset name that corresponds to the given snapshot name
673 * Example:
674 * 	zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
675 */
676static int
677zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
678{
679	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
680
681	if (zfs_component_namecheck(name, NULL, NULL) != 0)
682		return (SET_ERROR(EILSEQ));
683	dmu_objset_name(os, zname);
684	if (strlen(zname) + 1 + strlen(name) >= len)
685		return (SET_ERROR(ENAMETOOLONG));
686	(void) strcat(zname, "@");
687	(void) strcat(zname, name);
688	return (0);
689}
690
691static int
692zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
693{
694	vnode_t *svp = sep->se_root;
695	int error;
696
697	ASSERT(vn_ismntpt(svp));
698
699	/* this will be dropped by dounmount() */
700	if ((error = vn_vfswlock(svp)) != 0)
701		return (error);
702
703#ifdef illumos
704	VN_HOLD(svp);
705	error = dounmount(vn_mountedvfs(svp), fflags, cr);
706	if (error) {
707		VN_RELE(svp);
708		return (error);
709	}
710
711	/*
712	 * We can't use VN_RELE(), as that will try to invoke
713	 * zfsctl_snapdir_inactive(), which would cause us to destroy
714	 * the sd_lock mutex held by our caller.
715	 */
716	ASSERT(svp->v_count == 1);
717	gfs_vop_reclaim(svp, cr, NULL);
718
719	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
720	kmem_free(sep, sizeof (zfs_snapentry_t));
721
722	return (0);
723#else
724	vfs_ref(vn_mountedvfs(svp));
725	return (dounmount(vn_mountedvfs(svp), fflags, curthread));
726#endif
727}
728
729#ifdef illumos
730static void
731zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
732{
733	avl_index_t where;
734	vfs_t *vfsp;
735	refstr_t *pathref;
736	char newpath[MAXNAMELEN];
737	char *tail;
738
739	ASSERT(MUTEX_HELD(&sdp->sd_lock));
740	ASSERT(sep != NULL);
741
742	vfsp = vn_mountedvfs(sep->se_root);
743	ASSERT(vfsp != NULL);
744
745	vfs_lock_wait(vfsp);
746
747	/*
748	 * Change the name in the AVL tree.
749	 */
750	avl_remove(&sdp->sd_snaps, sep);
751	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
752	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
753	(void) strcpy(sep->se_name, nm);
754	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
755	avl_insert(&sdp->sd_snaps, sep, where);
756
757	/*
758	 * Change the current mountpoint info:
759	 * 	- update the tail of the mntpoint path
760	 *	- update the tail of the resource path
761	 */
762	pathref = vfs_getmntpoint(vfsp);
763	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
764	VERIFY((tail = strrchr(newpath, '/')) != NULL);
765	*(tail+1) = '\0';
766	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
767	(void) strcat(newpath, nm);
768	refstr_rele(pathref);
769	vfs_setmntpoint(vfsp, newpath, 0);
770
771	pathref = vfs_getresource(vfsp);
772	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
773	VERIFY((tail = strrchr(newpath, '@')) != NULL);
774	*(tail+1) = '\0';
775	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
776	(void) strcat(newpath, nm);
777	refstr_rele(pathref);
778	vfs_setresource(vfsp, newpath, 0);
779
780	vfs_unlock(vfsp);
781}
782#endif	/* illumos */
783
784#ifdef illumos
785/*ARGSUSED*/
786static int
787zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
788    cred_t *cr, caller_context_t *ct, int flags)
789{
790	zfsctl_snapdir_t *sdp = sdvp->v_data;
791	zfs_snapentry_t search, *sep;
792	zfsvfs_t *zfsvfs;
793	avl_index_t where;
794	char from[MAXNAMELEN], to[MAXNAMELEN];
795	char real[MAXNAMELEN], fsname[MAXNAMELEN];
796	int err;
797
798	zfsvfs = sdvp->v_vfsp->vfs_data;
799	ZFS_ENTER(zfsvfs);
800
801	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
802		err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
803		    MAXNAMELEN, NULL);
804		if (err == 0) {
805			snm = real;
806		} else if (err != ENOTSUP) {
807			ZFS_EXIT(zfsvfs);
808			return (err);
809		}
810	}
811
812	ZFS_EXIT(zfsvfs);
813
814	dmu_objset_name(zfsvfs->z_os, fsname);
815
816	err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
817	if (err == 0)
818		err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
819	if (err == 0)
820		err = zfs_secpolicy_rename_perms(from, to, cr);
821	if (err != 0)
822		return (err);
823
824	/*
825	 * Cannot move snapshots out of the snapdir.
826	 */
827	if (sdvp != tdvp)
828		return (SET_ERROR(EINVAL));
829
830	if (strcmp(snm, tnm) == 0)
831		return (0);
832
833	mutex_enter(&sdp->sd_lock);
834
835	search.se_name = (char *)snm;
836	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
837		mutex_exit(&sdp->sd_lock);
838		return (SET_ERROR(ENOENT));
839	}
840
841	err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0);
842	if (err == 0)
843		zfsctl_rename_snap(sdp, sep, tnm);
844
845	mutex_exit(&sdp->sd_lock);
846
847	return (err);
848}
849#endif	/* illumos */
850
851#ifdef illumos
852/* ARGSUSED */
853static int
854zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
855    caller_context_t *ct, int flags)
856{
857	zfsctl_snapdir_t *sdp = dvp->v_data;
858	zfs_snapentry_t *sep;
859	zfs_snapentry_t search;
860	zfsvfs_t *zfsvfs;
861	char snapname[MAXNAMELEN];
862	char real[MAXNAMELEN];
863	int err;
864
865	zfsvfs = dvp->v_vfsp->vfs_data;
866	ZFS_ENTER(zfsvfs);
867
868	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
869
870		err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
871		    MAXNAMELEN, NULL);
872		if (err == 0) {
873			name = real;
874		} else if (err != ENOTSUP) {
875			ZFS_EXIT(zfsvfs);
876			return (err);
877		}
878	}
879
880	ZFS_EXIT(zfsvfs);
881
882	err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
883	if (err == 0)
884		err = zfs_secpolicy_destroy_perms(snapname, cr);
885	if (err != 0)
886		return (err);
887
888	mutex_enter(&sdp->sd_lock);
889
890	search.se_name = name;
891	sep = avl_find(&sdp->sd_snaps, &search, NULL);
892	if (sep) {
893		avl_remove(&sdp->sd_snaps, sep);
894		err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
895		if (err != 0)
896			avl_add(&sdp->sd_snaps, sep);
897		else
898			err = dsl_destroy_snapshot(snapname, B_FALSE);
899	} else {
900		err = SET_ERROR(ENOENT);
901	}
902
903	mutex_exit(&sdp->sd_lock);
904
905	return (err);
906}
907#endif	/* illumos */
908
909/*
910 * This creates a snapshot under '.zfs/snapshot'.
911 */
912/* ARGSUSED */
913static int
914zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
915    cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
916{
917	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
918	char name[MAXNAMELEN];
919	int err;
920	static enum symfollow follow = NO_FOLLOW;
921	static enum uio_seg seg = UIO_SYSSPACE;
922
923	if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
924		return (SET_ERROR(EILSEQ));
925
926	dmu_objset_name(zfsvfs->z_os, name);
927
928	*vpp = NULL;
929
930	err = zfs_secpolicy_snapshot_perms(name, cr);
931	if (err != 0)
932		return (err);
933
934	if (err == 0) {
935		err = dmu_objset_snapshot_one(name, dirname);
936		if (err != 0)
937			return (err);
938		err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
939	}
940
941	return (err);
942}
943
944static int
945zfsctl_freebsd_snapdir_mkdir(ap)
946        struct vop_mkdir_args /* {
947                struct vnode *a_dvp;
948                struct vnode **a_vpp;
949                struct componentname *a_cnp;
950                struct vattr *a_vap;
951        } */ *ap;
952{
953
954	ASSERT(ap->a_cnp->cn_flags & SAVENAME);
955
956	return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
957	    ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
958}
959
960/*
961 * Lookup entry point for the 'snapshot' directory.  Try to open the
962 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
963 * Perform a mount of the associated dataset on top of the vnode.
964 */
965/* ARGSUSED */
966int
967zfsctl_snapdir_lookup(ap)
968	struct vop_lookup_args /* {
969		struct vnode *a_dvp;
970		struct vnode **a_vpp;
971		struct componentname *a_cnp;
972	} */ *ap;
973{
974	vnode_t *dvp = ap->a_dvp;
975	vnode_t **vpp = ap->a_vpp;
976	struct componentname *cnp = ap->a_cnp;
977	char nm[NAME_MAX + 1];
978	zfsctl_snapdir_t *sdp = dvp->v_data;
979	objset_t *snap;
980	char snapname[MAXNAMELEN];
981	char real[MAXNAMELEN];
982	char *mountpoint;
983	zfs_snapentry_t *sep, search;
984	size_t mountpoint_len;
985	avl_index_t where;
986	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
987	int err;
988	int ltype, flags = 0;
989
990	/*
991	 * No extended attributes allowed under .zfs
992	 */
993	if (flags & LOOKUP_XATTR)
994		return (SET_ERROR(EINVAL));
995	ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
996	strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
997
998	ASSERT(dvp->v_type == VDIR);
999
1000	*vpp = NULL;
1001
1002	/*
1003	 * If we get a recursive call, that means we got called
1004	 * from the domount() code while it was trying to look up the
1005	 * spec (which looks like a local path for zfs).  We need to
1006	 * add some flag to domount() to tell it not to do this lookup.
1007	 */
1008	if (MUTEX_HELD(&sdp->sd_lock))
1009		return (SET_ERROR(ENOENT));
1010
1011	ZFS_ENTER(zfsvfs);
1012	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1013		if (nm[0] == '.' && nm[1] == '.' && nm[2] =='\0') {
1014			VOP_UNLOCK(dvp, 0);
1015			VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
1016			VERIFY0(vn_lock(dvp, LK_EXCLUSIVE));
1017		}
1018		ZFS_EXIT(zfsvfs);
1019		return (0);
1020	}
1021
1022	if (flags & FIGNORECASE) {
1023		boolean_t conflict = B_FALSE;
1024
1025		err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
1026		    MAXNAMELEN, &conflict);
1027		if (err == 0) {
1028			strlcpy(nm, real, sizeof(nm));
1029		} else if (err != ENOTSUP) {
1030			ZFS_EXIT(zfsvfs);
1031			return (err);
1032		}
1033#if 0
1034		if (realpnp)
1035			(void) strlcpy(realpnp->pn_buf, nm,
1036			    realpnp->pn_bufsize);
1037		if (conflict && direntflags)
1038			*direntflags = ED_CASE_CONFLICT;
1039#endif
1040	}
1041
1042relookup:
1043	mutex_enter(&sdp->sd_lock);
1044	search.se_name = (char *)nm;
1045	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
1046		*vpp = sep->se_root;
1047		VN_HOLD(*vpp);
1048		err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
1049		if (err != 0) {
1050			*vpp = NULL;
1051		} else if (*vpp == sep->se_root) {
1052			/*
1053			 * The snapshot was unmounted behind our backs,
1054			 * try to remount it.
1055			 */
1056			VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0);
1057			goto domount;
1058		}
1059		mutex_exit(&sdp->sd_lock);
1060		ZFS_EXIT(zfsvfs);
1061		return (err);
1062	}
1063
1064	/*
1065	 * The requested snapshot is not currently mounted, look it up.
1066	 */
1067	err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
1068	if (err != 0) {
1069		mutex_exit(&sdp->sd_lock);
1070		ZFS_EXIT(zfsvfs);
1071		/*
1072		 * handle "ls *" or "?" in a graceful manner,
1073		 * forcing EILSEQ to ENOENT.
1074		 * Since shell ultimately passes "*" or "?" as name to lookup
1075		 */
1076		return (err == EILSEQ ? ENOENT : err);
1077	}
1078	if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
1079		mutex_exit(&sdp->sd_lock);
1080#ifdef illumos
1081		ZFS_EXIT(zfsvfs);
1082		return (SET_ERROR(ENOENT));
1083#else	/* !illumos */
1084		/* Translate errors and add SAVENAME when needed. */
1085		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
1086			err = EJUSTRETURN;
1087			cnp->cn_flags |= SAVENAME;
1088		} else {
1089			err = SET_ERROR(ENOENT);
1090		}
1091		ZFS_EXIT(zfsvfs);
1092		return (err);
1093#endif	/* illumos */
1094	}
1095
1096	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
1097	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
1098	(void) strcpy(sep->se_name, nm);
1099	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
1100	avl_insert(&sdp->sd_snaps, sep, where);
1101
1102	dmu_objset_rele(snap, FTAG);
1103domount:
1104	mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1105	    strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1;
1106	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1107	(void) snprintf(mountpoint, mountpoint_len,
1108	    "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1109	    dvp->v_vfsp->mnt_stat.f_mntonname, nm);
1110	mutex_exit(&sdp->sd_lock);
1111
1112	/*
1113	 * The vnode may get reclaimed between dropping sd_lock and
1114	 * getting the vnode lock.
1115	 * */
1116	err = vn_lock(*vpp, LK_EXCLUSIVE);
1117	if (err == ENOENT)
1118		goto relookup;
1119	VERIFY0(err);
1120	err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
1121	kmem_free(mountpoint, mountpoint_len);
1122	if (err == 0) {
1123		/*
1124		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1125		 *
1126		 * This is where we lie about our v_vfsp in order to
1127		 * make .zfs/snapshot/<snapname> accessible over NFS
1128		 * without requiring manual mounts of <snapname>.
1129		 */
1130		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
1131		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1132	}
1133	ZFS_EXIT(zfsvfs);
1134
1135#ifdef illumos
1136	/*
1137	 * If we had an error, drop our hold on the vnode and
1138	 * zfsctl_snapshot_inactive() will clean up.
1139	 */
1140	if (err != 0) {
1141		VN_RELE(*vpp);
1142		*vpp = NULL;
1143	}
1144#else
1145	if (err != 0)
1146		*vpp = NULL;
1147#endif
1148	return (err);
1149}
1150
1151/* ARGSUSED */
1152int
1153zfsctl_shares_lookup(ap)
1154	struct vop_lookup_args /* {
1155		struct vnode *a_dvp;
1156		struct vnode **a_vpp;
1157		struct componentname *a_cnp;
1158	} */ *ap;
1159{
1160	vnode_t *dvp = ap->a_dvp;
1161	vnode_t **vpp = ap->a_vpp;
1162	struct componentname *cnp = ap->a_cnp;
1163	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1164	char nm[NAME_MAX + 1];
1165	znode_t *dzp;
1166	int error;
1167
1168	ZFS_ENTER(zfsvfs);
1169
1170	ASSERT(cnp->cn_namelen < sizeof(nm));
1171	strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1172
1173	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1174		if (nm[0] == '.' && nm[1] == '.' && nm[2] =='\0') {
1175			VOP_UNLOCK(dvp, 0);
1176			VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
1177			VERIFY0(vn_lock(dvp, LK_EXCLUSIVE));
1178		}
1179		ZFS_EXIT(zfsvfs);
1180		return (0);
1181	}
1182
1183	if (zfsvfs->z_shares_dir == 0) {
1184		ZFS_EXIT(zfsvfs);
1185		return (SET_ERROR(ENOTSUP));
1186	}
1187	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1188		error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1189		VN_RELE(ZTOV(dzp));
1190	}
1191
1192	ZFS_EXIT(zfsvfs);
1193
1194	return (error);
1195}
1196
1197/* ARGSUSED */
1198static int
1199zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1200    offset_t *offp, offset_t *nextp, void *data, int flags)
1201{
1202	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1203	char snapname[MAXNAMELEN];
1204	uint64_t id, cookie;
1205	boolean_t case_conflict;
1206	int error;
1207
1208	ZFS_ENTER(zfsvfs);
1209
1210	cookie = *offp;
1211	dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1212	error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1213	    &cookie, &case_conflict);
1214	dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1215	if (error) {
1216		ZFS_EXIT(zfsvfs);
1217		if (error == ENOENT) {
1218			*eofp = 1;
1219			return (0);
1220		}
1221		return (error);
1222	}
1223
1224	if (flags & V_RDDIR_ENTFLAGS) {
1225		edirent_t *eodp = dp;
1226
1227		(void) strcpy(eodp->ed_name, snapname);
1228		eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1229		eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1230	} else {
1231		struct dirent64 *odp = dp;
1232
1233		(void) strcpy(odp->d_name, snapname);
1234		odp->d_ino = ZFSCTL_INO_SNAP(id);
1235	}
1236	*nextp = cookie;
1237
1238	ZFS_EXIT(zfsvfs);
1239
1240	return (0);
1241}
1242
1243/* ARGSUSED */
1244static int
1245zfsctl_shares_readdir(ap)
1246	struct vop_readdir_args /* {
1247		struct vnode *a_vp;
1248		struct uio *a_uio;
1249		struct ucred *a_cred;
1250		int *a_eofflag;
1251		int *a_ncookies;
1252		u_long **a_cookies;
1253	} */ *ap;
1254{
1255	vnode_t *vp = ap->a_vp;
1256	uio_t *uiop = ap->a_uio;
1257	cred_t *cr = ap->a_cred;
1258	int *eofp = ap->a_eofflag;
1259	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1260	znode_t *dzp;
1261	int error;
1262
1263	ZFS_ENTER(zfsvfs);
1264
1265	if (zfsvfs->z_shares_dir == 0) {
1266		ZFS_EXIT(zfsvfs);
1267		return (SET_ERROR(ENOTSUP));
1268	}
1269	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1270		vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1271		error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1272		VN_URELE(ZTOV(dzp));
1273	} else {
1274		*eofp = 1;
1275		error = SET_ERROR(ENOENT);
1276	}
1277
1278	ZFS_EXIT(zfsvfs);
1279	return (error);
1280}
1281
1282/*
1283 * pvp is the '.zfs' directory (zfsctl_node_t).
1284 *
1285 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1286 *
1287 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1288 * when a lookup is performed on .zfs for "snapshot".
1289 */
1290vnode_t *
1291zfsctl_mknode_snapdir(vnode_t *pvp)
1292{
1293	vnode_t *vp;
1294	zfsctl_snapdir_t *sdp;
1295
1296	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1297	    &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1298	    zfsctl_snapdir_readdir_cb, NULL);
1299	sdp = vp->v_data;
1300	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1301	sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1302	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1303	avl_create(&sdp->sd_snaps, snapentry_compare,
1304	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1305	VOP_UNLOCK(vp, 0);
1306	return (vp);
1307}
1308
1309vnode_t *
1310zfsctl_mknode_shares(vnode_t *pvp)
1311{
1312	vnode_t *vp;
1313	zfsctl_node_t *sdp;
1314
1315	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1316	    &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1317	    NULL, NULL);
1318	sdp = vp->v_data;
1319	sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1320	VOP_UNLOCK(vp, 0);
1321	return (vp);
1322
1323}
1324
1325/* ARGSUSED */
1326static int
1327zfsctl_shares_getattr(ap)
1328	struct vop_getattr_args /* {
1329		struct vnode *a_vp;
1330		struct vattr *a_vap;
1331		struct ucred *a_cred;
1332		struct thread *a_td;
1333	} */ *ap;
1334{
1335	vnode_t *vp = ap->a_vp;
1336	vattr_t *vap = ap->a_vap;
1337	cred_t *cr = ap->a_cred;
1338	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1339	znode_t *dzp;
1340	int error;
1341
1342	ZFS_ENTER(zfsvfs);
1343	if (zfsvfs->z_shares_dir == 0) {
1344		ZFS_EXIT(zfsvfs);
1345		return (SET_ERROR(ENOTSUP));
1346	}
1347	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1348		vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1349		error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1350		VN_URELE(ZTOV(dzp));
1351	}
1352	ZFS_EXIT(zfsvfs);
1353	return (error);
1354
1355
1356}
1357
1358/* ARGSUSED */
1359static int
1360zfsctl_snapdir_getattr(ap)
1361	struct vop_getattr_args /* {
1362		struct vnode *a_vp;
1363		struct vattr *a_vap;
1364		struct ucred *a_cred;
1365	} */ *ap;
1366{
1367	vnode_t *vp = ap->a_vp;
1368	vattr_t *vap = ap->a_vap;
1369	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1370	zfsctl_snapdir_t *sdp = vp->v_data;
1371
1372	ZFS_ENTER(zfsvfs);
1373	zfsctl_common_getattr(vp, vap);
1374	vap->va_nodeid = gfs_file_inode(vp);
1375	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1376	vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1377	vap->va_birthtime = vap->va_ctime;
1378	ZFS_EXIT(zfsvfs);
1379
1380	return (0);
1381}
1382
1383/* ARGSUSED */
1384static int
1385zfsctl_snapdir_reclaim(ap)
1386	struct vop_reclaim_args /* {
1387		struct vnode *a_vp;
1388		struct thread *a_td;
1389	} */ *ap;
1390{
1391	vnode_t *vp = ap->a_vp;
1392	zfsctl_snapdir_t *sdp = vp->v_data;
1393	zfs_snapentry_t *sep;
1394
1395	ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1396	mutex_destroy(&sdp->sd_lock);
1397	avl_destroy(&sdp->sd_snaps);
1398	gfs_vop_reclaim(ap);
1399
1400	return (0);
1401}
1402
1403static int
1404zfsctl_shares_print(ap)
1405	struct vop_print_args /* {
1406		struct vnode *a_vp;
1407	} */ *ap;
1408{
1409	printf("    .zfs/shares node\n");
1410	zfsctl_common_print(ap);
1411	return (0);
1412}
1413
1414static int
1415zfsctl_snapdir_print(ap)
1416	struct vop_print_args /* {
1417		struct vnode *a_vp;
1418	} */ *ap;
1419{
1420	vnode_t *vp = ap->a_vp;
1421	zfsctl_snapdir_t *sdp = vp->v_data;
1422
1423	printf("    .zfs/snapshot node\n");
1424	printf("    number of children = %lu\n", avl_numnodes(&sdp->sd_snaps));
1425	zfsctl_common_print(ap);
1426	return (0);
1427}
1428
1429#ifdef illumos
1430static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1431	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1432	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1433	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1434	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_snapdir_getattr } },
1435	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1436	{ VOPNAME_RENAME,	{ .vop_rename = zfsctl_snapdir_rename }	},
1437	{ VOPNAME_RMDIR,	{ .vop_rmdir = zfsctl_snapdir_remove }	},
1438	{ VOPNAME_MKDIR,	{ .vop_mkdir = zfsctl_snapdir_mkdir }	},
1439	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir }	},
1440	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_snapdir_lookup }	},
1441	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1442	{ VOPNAME_INACTIVE,	{ .vop_inactive = zfsctl_snapdir_inactive } },
1443	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid }	},
1444	{ NULL }
1445};
1446
1447static const fs_operation_def_t zfsctl_tops_shares[] = {
1448	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1449	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1450	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1451	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_shares_getattr } },
1452	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1453	{ VOPNAME_READDIR,	{ .vop_readdir = zfsctl_shares_readdir } },
1454	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_shares_lookup }	},
1455	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1456	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1457	{ VOPNAME_FID,		{ .vop_fid = zfsctl_shares_fid } },
1458	{ NULL }
1459};
1460#else	/* !illumos */
1461static struct vop_vector zfsctl_ops_snapdir = {
1462	.vop_default =	&default_vnodeops,
1463	.vop_open =	zfsctl_common_open,
1464	.vop_close =	zfsctl_common_close,
1465	.vop_ioctl =	VOP_EINVAL,
1466	.vop_getattr =	zfsctl_snapdir_getattr,
1467	.vop_access =	zfsctl_common_access,
1468	.vop_mkdir =	zfsctl_freebsd_snapdir_mkdir,
1469	.vop_readdir =	gfs_vop_readdir,
1470	.vop_lookup =	zfsctl_snapdir_lookup,
1471	.vop_inactive =	VOP_NULL,
1472	.vop_reclaim =	zfsctl_snapdir_reclaim,
1473	.vop_fid =	zfsctl_common_fid,
1474	.vop_print =	zfsctl_snapdir_print,
1475};
1476
1477static struct vop_vector zfsctl_ops_shares = {
1478	.vop_default =	&default_vnodeops,
1479	.vop_open =	zfsctl_common_open,
1480	.vop_close =	zfsctl_common_close,
1481	.vop_ioctl =	VOP_EINVAL,
1482	.vop_getattr =	zfsctl_shares_getattr,
1483	.vop_access =	zfsctl_common_access,
1484	.vop_readdir =	zfsctl_shares_readdir,
1485	.vop_lookup =	zfsctl_shares_lookup,
1486	.vop_inactive =	VOP_NULL,
1487	.vop_reclaim =	gfs_vop_reclaim,
1488	.vop_fid =	zfsctl_shares_fid,
1489	.vop_print =	zfsctl_shares_print,
1490};
1491#endif	/* illumos */
1492
1493/*
1494 * pvp is the GFS vnode '.zfs/snapshot'.
1495 *
1496 * This creates a GFS node under '.zfs/snapshot' representing each
1497 * snapshot.  This newly created GFS node is what we mount snapshot
1498 * vfs_t's ontop of.
1499 */
1500static vnode_t *
1501zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1502{
1503	vnode_t *vp;
1504	zfsctl_node_t *zcp;
1505
1506	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1507	    &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1508	zcp = vp->v_data;
1509	zcp->zc_id = objset;
1510	VOP_UNLOCK(vp, 0);
1511
1512	return (vp);
1513}
1514
1515static int
1516zfsctl_snapshot_inactive(ap)
1517	struct vop_inactive_args /* {
1518		struct vnode *a_vp;
1519		struct thread *a_td;
1520	} */ *ap;
1521{
1522	vnode_t *vp = ap->a_vp;
1523
1524	vrecycle(vp);
1525	return (0);
1526}
1527
1528static int
1529zfsctl_snapshot_reclaim(ap)
1530	struct vop_reclaim_args /* {
1531		struct vnode *a_vp;
1532		struct thread *a_td;
1533	} */ *ap;
1534{
1535	vnode_t *vp = ap->a_vp;
1536	cred_t *cr = ap->a_td->td_ucred;
1537	zfsctl_snapdir_t *sdp;
1538	zfs_snapentry_t *sep, *next;
1539	int locked;
1540	vnode_t *dvp;
1541
1542	VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1543	sdp = dvp->v_data;
1544	/* this may already have been unmounted */
1545	if (sdp == NULL) {
1546		VN_RELE(dvp);
1547		return (0);
1548	}
1549	if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1550		mutex_enter(&sdp->sd_lock);
1551
1552	ASSERT(!vn_ismntpt(vp));
1553
1554	sep = avl_first(&sdp->sd_snaps);
1555	while (sep != NULL) {
1556		next = AVL_NEXT(&sdp->sd_snaps, sep);
1557
1558		if (sep->se_root == vp) {
1559			avl_remove(&sdp->sd_snaps, sep);
1560			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1561			kmem_free(sep, sizeof (zfs_snapentry_t));
1562			break;
1563		}
1564		sep = next;
1565	}
1566	ASSERT(sep != NULL);
1567
1568	if (!locked)
1569		mutex_exit(&sdp->sd_lock);
1570	VN_RELE(dvp);
1571
1572	/*
1573	 * Dispose of the vnode for the snapshot mount point.
1574	 * This is safe to do because once this entry has been removed
1575	 * from the AVL tree, it can't be found again, so cannot become
1576	 * "active".  If we lookup the same name again we will end up
1577	 * creating a new vnode.
1578	 */
1579	gfs_vop_reclaim(ap);
1580	return (0);
1581
1582}
1583
1584static int
1585zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1586{
1587	zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1588	vnode_t *dvp, *vp;
1589	zfsctl_snapdir_t *sdp;
1590	zfs_snapentry_t *sep;
1591	int error;
1592
1593	ASSERT(zfsvfs->z_ctldir != NULL);
1594	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1595	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1596	if (error != 0)
1597		return (error);
1598	sdp = dvp->v_data;
1599
1600	mutex_enter(&sdp->sd_lock);
1601	sep = avl_first(&sdp->sd_snaps);
1602	while (sep != NULL) {
1603		vp = sep->se_root;
1604		if (vp == ap->a_vp)
1605			break;
1606		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1607	}
1608	if (sep == NULL) {
1609		mutex_exit(&sdp->sd_lock);
1610		error = ENOENT;
1611	} else {
1612		size_t len;
1613
1614		len = strlen(sep->se_name);
1615		*ap->a_buflen -= len;
1616		bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1617		mutex_exit(&sdp->sd_lock);
1618		vref(dvp);
1619		*ap->a_vpp = dvp;
1620	}
1621	VN_RELE(dvp);
1622
1623	return (error);
1624}
1625
1626static int
1627zfsctl_snaphot_print(ap)
1628	struct vop_print_args /* {
1629		struct vnode *a_vp;
1630	} */ *ap;
1631{
1632	vnode_t *vp = ap->a_vp;
1633	zfsctl_node_t *zcp = vp->v_data;
1634
1635	printf("    .zfs/snapshot/<snap> node\n");
1636	printf("    id = %ju\n", (uintmax_t)zcp->zc_id);
1637	zfsctl_common_print(ap);
1638	return (0);
1639}
1640
1641/*
1642 * These VP's should never see the light of day.  They should always
1643 * be covered.
1644 */
1645static struct vop_vector zfsctl_ops_snapshot = {
1646	.vop_default =	&default_vnodeops,
1647	.vop_inactive =	zfsctl_snapshot_inactive,
1648	.vop_reclaim =	zfsctl_snapshot_reclaim,
1649	.vop_vptocnp =	zfsctl_snapshot_vptocnp,
1650	.vop_print =	zfsctl_snaphot_print,
1651};
1652
1653int
1654zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1655{
1656	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1657	vnode_t *dvp, *vp;
1658	zfsctl_snapdir_t *sdp;
1659	zfsctl_node_t *zcp;
1660	zfs_snapentry_t *sep;
1661	int error;
1662
1663	ASSERT(zfsvfs->z_ctldir != NULL);
1664	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1665	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1666	if (error != 0)
1667		return (error);
1668	sdp = dvp->v_data;
1669
1670	mutex_enter(&sdp->sd_lock);
1671	sep = avl_first(&sdp->sd_snaps);
1672	while (sep != NULL) {
1673		vp = sep->se_root;
1674		zcp = vp->v_data;
1675		if (zcp->zc_id == objsetid)
1676			break;
1677
1678		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1679	}
1680
1681	if (sep != NULL) {
1682		VN_HOLD(vp);
1683		/*
1684		 * Return the mounted root rather than the covered mount point.
1685		 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1686		 * and returns the ZFS vnode mounted on top of the GFS node.
1687		 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1688		 */
1689		error = traverse(&vp, LK_SHARED | LK_RETRY);
1690		if (error == 0) {
1691			if (vp == sep->se_root) {
1692				VN_RELE(vp);	/* release covered vp */
1693				error = SET_ERROR(EINVAL);
1694			} else {
1695				*zfsvfsp = VTOZ(vp)->z_zfsvfs;
1696				VN_URELE(vp);	/* put snapshot's root vp */
1697			}
1698		}
1699		mutex_exit(&sdp->sd_lock);
1700	} else {
1701		error = SET_ERROR(EINVAL);
1702		mutex_exit(&sdp->sd_lock);
1703	}
1704
1705	VN_RELE(dvp);
1706
1707	return (error);
1708}
1709
1710/*
1711 * Unmount any snapshots for the given filesystem.  This is called from
1712 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1713 * snapshots.
1714 */
1715int
1716zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1717{
1718	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1719	vnode_t *dvp;
1720	zfsctl_snapdir_t *sdp;
1721	zfs_snapentry_t *sep, *next;
1722	int error;
1723
1724	ASSERT(zfsvfs->z_ctldir != NULL);
1725	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1726	    NULL, 0, NULL, cr, NULL, NULL, NULL);
1727	if (error != 0)
1728		return (error);
1729	sdp = dvp->v_data;
1730
1731	mutex_enter(&sdp->sd_lock);
1732
1733	sep = avl_first(&sdp->sd_snaps);
1734	while (sep != NULL) {
1735		next = AVL_NEXT(&sdp->sd_snaps, sep);
1736
1737		/*
1738		 * If this snapshot is not mounted, then it must
1739		 * have just been unmounted by somebody else, and
1740		 * will be cleaned up by zfsctl_snapdir_inactive().
1741		 */
1742		if (vn_ismntpt(sep->se_root)) {
1743			error = zfsctl_unmount_snap(sep, fflags, cr);
1744			if (error) {
1745				avl_index_t where;
1746
1747				/*
1748				 * Before reinserting snapshot to the tree,
1749				 * check if it was actually removed. For example
1750				 * when snapshot mount point is busy, we will
1751				 * have an error here, but there will be no need
1752				 * to reinsert snapshot.
1753				 */
1754				if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1755					avl_insert(&sdp->sd_snaps, sep, where);
1756				break;
1757			}
1758		}
1759		sep = next;
1760	}
1761
1762	mutex_exit(&sdp->sd_lock);
1763	VN_RELE(dvp);
1764
1765	return (error);
1766}
1767