zfs_ctldir.c revision 297077
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
297/*
298 * Common open routine.  Disallow any write access.
299 */
300/* ARGSUSED */
301static int
302zfsctl_common_open(struct vop_open_args *ap)
303{
304	int flags = ap->a_mode;
305
306	if (flags & FWRITE)
307		return (SET_ERROR(EACCES));
308
309	return (0);
310}
311
312/*
313 * Common close routine.  Nothing to do here.
314 */
315/* ARGSUSED */
316static int
317zfsctl_common_close(struct vop_close_args *ap)
318{
319	return (0);
320}
321
322/*
323 * Common access routine.  Disallow writes.
324 */
325/* ARGSUSED */
326static int
327zfsctl_common_access(ap)
328	struct vop_access_args /* {
329		struct vnode *a_vp;
330		accmode_t a_accmode;
331		struct ucred *a_cred;
332		struct thread *a_td;
333	} */ *ap;
334{
335	accmode_t accmode = ap->a_accmode;
336
337#ifdef TODO
338	if (flags & V_ACE_MASK) {
339		if (accmode & ACE_ALL_WRITE_PERMS)
340			return (SET_ERROR(EACCES));
341	} else {
342#endif
343		if (accmode & VWRITE)
344			return (SET_ERROR(EACCES));
345#ifdef TODO
346	}
347#endif
348
349	return (0);
350}
351
352/*
353 * Common getattr function.  Fill in basic information.
354 */
355static void
356zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
357{
358	timestruc_t	now;
359
360	vap->va_uid = 0;
361	vap->va_gid = 0;
362	vap->va_rdev = 0;
363	/*
364	 * We are a purely virtual object, so we have no
365	 * blocksize or allocated blocks.
366	 */
367	vap->va_blksize = 0;
368	vap->va_nblocks = 0;
369	vap->va_seq = 0;
370	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
371	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
372	    S_IROTH | S_IXOTH;
373	vap->va_type = VDIR;
374	/*
375	 * We live in the now (for atime).
376	 */
377	gethrestime(&now);
378	vap->va_atime = now;
379	/* FreeBSD: Reset chflags(2) flags. */
380	vap->va_flags = 0;
381}
382
383/*ARGSUSED*/
384static int
385zfsctl_common_fid(ap)
386	struct vop_fid_args /* {
387		struct vnode *a_vp;
388		struct fid *a_fid;
389	} */ *ap;
390{
391	vnode_t		*vp = ap->a_vp;
392	fid_t		*fidp = (void *)ap->a_fid;
393	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
394	zfsctl_node_t	*zcp = vp->v_data;
395	uint64_t	object = zcp->zc_id;
396	zfid_short_t	*zfid;
397	int		i;
398
399	ZFS_ENTER(zfsvfs);
400
401#ifdef illumos
402	if (fidp->fid_len < SHORT_FID_LEN) {
403		fidp->fid_len = SHORT_FID_LEN;
404		ZFS_EXIT(zfsvfs);
405		return (SET_ERROR(ENOSPC));
406	}
407#else
408	fidp->fid_len = SHORT_FID_LEN;
409#endif
410
411	zfid = (zfid_short_t *)fidp;
412
413	zfid->zf_len = SHORT_FID_LEN;
414
415	for (i = 0; i < sizeof (zfid->zf_object); i++)
416		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
417
418	/* .zfs znodes always have a generation number of 0 */
419	for (i = 0; i < sizeof (zfid->zf_gen); i++)
420		zfid->zf_gen[i] = 0;
421
422	ZFS_EXIT(zfsvfs);
423	return (0);
424}
425
426
427/*ARGSUSED*/
428static int
429zfsctl_shares_fid(ap)
430	struct vop_fid_args /* {
431		struct vnode *a_vp;
432		struct fid *a_fid;
433	} */ *ap;
434{
435	vnode_t		*vp = ap->a_vp;
436	fid_t		*fidp = (void *)ap->a_fid;
437	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
438	znode_t		*dzp;
439	int		error;
440
441	ZFS_ENTER(zfsvfs);
442
443	if (zfsvfs->z_shares_dir == 0) {
444		ZFS_EXIT(zfsvfs);
445		return (SET_ERROR(ENOTSUP));
446	}
447
448	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
449		error = VOP_FID(ZTOV(dzp), fidp);
450		VN_RELE(ZTOV(dzp));
451	}
452
453	ZFS_EXIT(zfsvfs);
454	return (error);
455}
456
457static int
458zfsctl_common_reclaim(ap)
459	struct vop_reclaim_args /* {
460		struct vnode *a_vp;
461		struct thread *a_td;
462	} */ *ap;
463{
464	vnode_t *vp = ap->a_vp;
465
466	/*
467	 * Destroy the vm object and flush associated pages.
468	 */
469	vnode_destroy_vobject(vp);
470	VI_LOCK(vp);
471	vp->v_data = NULL;
472	VI_UNLOCK(vp);
473	return (0);
474}
475
476/*
477 * .zfs inode namespace
478 *
479 * We need to generate unique inode numbers for all files and directories
480 * within the .zfs pseudo-filesystem.  We use the following scheme:
481 *
482 * 	ENTRY			ZFSCTL_INODE
483 * 	.zfs			1
484 * 	.zfs/snapshot		2
485 * 	.zfs/snapshot/<snap>	objectid(snap)
486 */
487
488#define	ZFSCTL_INO_SNAP(id)	(id)
489
490/*
491 * Get root directory attributes.
492 */
493/* ARGSUSED */
494static int
495zfsctl_root_getattr(ap)
496	struct vop_getattr_args /* {
497		struct vnode *a_vp;
498		struct vattr *a_vap;
499		struct ucred *a_cred;
500	} */ *ap;
501{
502	struct vnode *vp = ap->a_vp;
503	struct vattr *vap = ap->a_vap;
504	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
505	zfsctl_node_t *zcp = vp->v_data;
506
507	ZFS_ENTER(zfsvfs);
508	vap->va_nodeid = ZFSCTL_INO_ROOT;
509	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
510	vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
511	vap->va_birthtime = vap->va_ctime;
512
513	zfsctl_common_getattr(vp, vap);
514	ZFS_EXIT(zfsvfs);
515
516	return (0);
517}
518
519/*
520 * Special case the handling of "..".
521 */
522/* ARGSUSED */
523int
524zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
525    int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
526    int *direntflags, pathname_t *realpnp)
527{
528	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
529	int err;
530
531	/*
532	 * No extended attributes allowed under .zfs
533	 */
534	if (flags & LOOKUP_XATTR)
535		return (SET_ERROR(EINVAL));
536
537	ZFS_ENTER(zfsvfs);
538
539	if (strcmp(nm, "..") == 0) {
540		err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
541		if (err == 0)
542			VOP_UNLOCK(*vpp, 0);
543	} else {
544		err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
545		    cr, ct, direntflags, realpnp);
546	}
547
548	ZFS_EXIT(zfsvfs);
549
550	return (err);
551}
552
553#ifdef illumos
554static int
555zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
556    caller_context_t *ct)
557{
558	/*
559	 * We only care about ACL_ENABLED so that libsec can
560	 * display ACL correctly and not default to POSIX draft.
561	 */
562	if (cmd == _PC_ACL_ENABLED) {
563		*valp = _ACL_ACE_ENABLED;
564		return (0);
565	}
566
567	return (fs_pathconf(vp, cmd, valp, cr, ct));
568}
569#endif	/* illumos */
570
571#ifdef illumos
572static const fs_operation_def_t zfsctl_tops_root[] = {
573	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
574	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
575	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
576	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_root_getattr }	},
577	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
578	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } 	},
579	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_root_lookup }	},
580	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
581	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive }	},
582	{ VOPNAME_PATHCONF,	{ .vop_pathconf = zfsctl_pathconf }	},
583	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid	}	},
584	{ NULL }
585};
586#endif	/* illumos */
587
588/*
589 * Special case the handling of "..".
590 */
591/* ARGSUSED */
592int
593zfsctl_freebsd_root_lookup(ap)
594	struct vop_lookup_args /* {
595		struct vnode *a_dvp;
596		struct vnode **a_vpp;
597		struct componentname *a_cnp;
598	} */ *ap;
599{
600	vnode_t *dvp = ap->a_dvp;
601	vnode_t **vpp = ap->a_vpp;
602	cred_t *cr = ap->a_cnp->cn_cred;
603	int flags = ap->a_cnp->cn_flags;
604	int nameiop = ap->a_cnp->cn_nameiop;
605	char nm[NAME_MAX + 1];
606	int err;
607	int ltype;
608
609	if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
610		return (EOPNOTSUPP);
611
612	ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
613	strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
614	err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
615	if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) {
616		ltype = VOP_ISLOCKED(dvp);
617		if (flags & ISDOTDOT) {
618			VN_HOLD(*vpp);
619			VOP_UNLOCK(dvp, 0);
620		}
621		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
622		if (flags & ISDOTDOT) {
623			VN_RELE(*vpp);
624			vn_lock(dvp, ltype| LK_RETRY);
625		}
626	}
627
628	return (err);
629}
630
631static struct vop_vector zfsctl_ops_root = {
632	.vop_default =	&default_vnodeops,
633	.vop_open =	zfsctl_common_open,
634	.vop_close =	zfsctl_common_close,
635	.vop_ioctl =	VOP_EINVAL,
636	.vop_getattr =	zfsctl_root_getattr,
637	.vop_access =	zfsctl_common_access,
638	.vop_readdir =	gfs_vop_readdir,
639	.vop_lookup =	zfsctl_freebsd_root_lookup,
640	.vop_inactive =	VOP_NULL,
641	.vop_reclaim =	gfs_vop_reclaim,
642#ifdef TODO
643	.vop_pathconf =	zfsctl_pathconf,
644#endif
645	.vop_fid =	zfsctl_common_fid,
646};
647
648/*
649 * Gets the full dataset name that corresponds to the given snapshot name
650 * Example:
651 * 	zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
652 */
653static int
654zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
655{
656	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
657
658	if (zfs_component_namecheck(name, NULL, NULL) != 0)
659		return (SET_ERROR(EILSEQ));
660	dmu_objset_name(os, zname);
661	if (strlen(zname) + 1 + strlen(name) >= len)
662		return (SET_ERROR(ENAMETOOLONG));
663	(void) strcat(zname, "@");
664	(void) strcat(zname, name);
665	return (0);
666}
667
668static int
669zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
670{
671	vnode_t *svp = sep->se_root;
672	int error;
673
674	ASSERT(vn_ismntpt(svp));
675
676	/* this will be dropped by dounmount() */
677	if ((error = vn_vfswlock(svp)) != 0)
678		return (error);
679
680#ifdef illumos
681	VN_HOLD(svp);
682	error = dounmount(vn_mountedvfs(svp), fflags, cr);
683	if (error) {
684		VN_RELE(svp);
685		return (error);
686	}
687
688	/*
689	 * We can't use VN_RELE(), as that will try to invoke
690	 * zfsctl_snapdir_inactive(), which would cause us to destroy
691	 * the sd_lock mutex held by our caller.
692	 */
693	ASSERT(svp->v_count == 1);
694	gfs_vop_reclaim(svp, cr, NULL);
695
696	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
697	kmem_free(sep, sizeof (zfs_snapentry_t));
698
699	return (0);
700#else
701	vfs_ref(vn_mountedvfs(svp));
702	return (dounmount(vn_mountedvfs(svp), fflags, curthread));
703#endif
704}
705
706#ifdef illumos
707static void
708zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
709{
710	avl_index_t where;
711	vfs_t *vfsp;
712	refstr_t *pathref;
713	char newpath[MAXNAMELEN];
714	char *tail;
715
716	ASSERT(MUTEX_HELD(&sdp->sd_lock));
717	ASSERT(sep != NULL);
718
719	vfsp = vn_mountedvfs(sep->se_root);
720	ASSERT(vfsp != NULL);
721
722	vfs_lock_wait(vfsp);
723
724	/*
725	 * Change the name in the AVL tree.
726	 */
727	avl_remove(&sdp->sd_snaps, sep);
728	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
729	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
730	(void) strcpy(sep->se_name, nm);
731	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
732	avl_insert(&sdp->sd_snaps, sep, where);
733
734	/*
735	 * Change the current mountpoint info:
736	 * 	- update the tail of the mntpoint path
737	 *	- update the tail of the resource path
738	 */
739	pathref = vfs_getmntpoint(vfsp);
740	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
741	VERIFY((tail = strrchr(newpath, '/')) != NULL);
742	*(tail+1) = '\0';
743	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
744	(void) strcat(newpath, nm);
745	refstr_rele(pathref);
746	vfs_setmntpoint(vfsp, newpath, 0);
747
748	pathref = vfs_getresource(vfsp);
749	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
750	VERIFY((tail = strrchr(newpath, '@')) != NULL);
751	*(tail+1) = '\0';
752	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
753	(void) strcat(newpath, nm);
754	refstr_rele(pathref);
755	vfs_setresource(vfsp, newpath, 0);
756
757	vfs_unlock(vfsp);
758}
759#endif	/* illumos */
760
761#ifdef illumos
762/*ARGSUSED*/
763static int
764zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
765    cred_t *cr, caller_context_t *ct, int flags)
766{
767	zfsctl_snapdir_t *sdp = sdvp->v_data;
768	zfs_snapentry_t search, *sep;
769	zfsvfs_t *zfsvfs;
770	avl_index_t where;
771	char from[MAXNAMELEN], to[MAXNAMELEN];
772	char real[MAXNAMELEN], fsname[MAXNAMELEN];
773	int err;
774
775	zfsvfs = sdvp->v_vfsp->vfs_data;
776	ZFS_ENTER(zfsvfs);
777
778	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
779		err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
780		    MAXNAMELEN, NULL);
781		if (err == 0) {
782			snm = real;
783		} else if (err != ENOTSUP) {
784			ZFS_EXIT(zfsvfs);
785			return (err);
786		}
787	}
788
789	ZFS_EXIT(zfsvfs);
790
791	dmu_objset_name(zfsvfs->z_os, fsname);
792
793	err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
794	if (err == 0)
795		err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
796	if (err == 0)
797		err = zfs_secpolicy_rename_perms(from, to, cr);
798	if (err != 0)
799		return (err);
800
801	/*
802	 * Cannot move snapshots out of the snapdir.
803	 */
804	if (sdvp != tdvp)
805		return (SET_ERROR(EINVAL));
806
807	if (strcmp(snm, tnm) == 0)
808		return (0);
809
810	mutex_enter(&sdp->sd_lock);
811
812	search.se_name = (char *)snm;
813	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
814		mutex_exit(&sdp->sd_lock);
815		return (SET_ERROR(ENOENT));
816	}
817
818	err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0);
819	if (err == 0)
820		zfsctl_rename_snap(sdp, sep, tnm);
821
822	mutex_exit(&sdp->sd_lock);
823
824	return (err);
825}
826#endif	/* illumos */
827
828#ifdef illumos
829/* ARGSUSED */
830static int
831zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
832    caller_context_t *ct, int flags)
833{
834	zfsctl_snapdir_t *sdp = dvp->v_data;
835	zfs_snapentry_t *sep;
836	zfs_snapentry_t search;
837	zfsvfs_t *zfsvfs;
838	char snapname[MAXNAMELEN];
839	char real[MAXNAMELEN];
840	int err;
841
842	zfsvfs = dvp->v_vfsp->vfs_data;
843	ZFS_ENTER(zfsvfs);
844
845	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
846
847		err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
848		    MAXNAMELEN, NULL);
849		if (err == 0) {
850			name = real;
851		} else if (err != ENOTSUP) {
852			ZFS_EXIT(zfsvfs);
853			return (err);
854		}
855	}
856
857	ZFS_EXIT(zfsvfs);
858
859	err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
860	if (err == 0)
861		err = zfs_secpolicy_destroy_perms(snapname, cr);
862	if (err != 0)
863		return (err);
864
865	mutex_enter(&sdp->sd_lock);
866
867	search.se_name = name;
868	sep = avl_find(&sdp->sd_snaps, &search, NULL);
869	if (sep) {
870		avl_remove(&sdp->sd_snaps, sep);
871		err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
872		if (err != 0)
873			avl_add(&sdp->sd_snaps, sep);
874		else
875			err = dsl_destroy_snapshot(snapname, B_FALSE);
876	} else {
877		err = SET_ERROR(ENOENT);
878	}
879
880	mutex_exit(&sdp->sd_lock);
881
882	return (err);
883}
884#endif	/* illumos */
885
886/*
887 * This creates a snapshot under '.zfs/snapshot'.
888 */
889/* ARGSUSED */
890static int
891zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
892    cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
893{
894	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
895	char name[MAXNAMELEN];
896	int err;
897	static enum symfollow follow = NO_FOLLOW;
898	static enum uio_seg seg = UIO_SYSSPACE;
899
900	if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
901		return (SET_ERROR(EILSEQ));
902
903	dmu_objset_name(zfsvfs->z_os, name);
904
905	*vpp = NULL;
906
907	err = zfs_secpolicy_snapshot_perms(name, cr);
908	if (err != 0)
909		return (err);
910
911	if (err == 0) {
912		err = dmu_objset_snapshot_one(name, dirname);
913		if (err != 0)
914			return (err);
915		err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
916	}
917
918	return (err);
919}
920
921static int
922zfsctl_freebsd_snapdir_mkdir(ap)
923        struct vop_mkdir_args /* {
924                struct vnode *a_dvp;
925                struct vnode **a_vpp;
926                struct componentname *a_cnp;
927                struct vattr *a_vap;
928        } */ *ap;
929{
930
931	ASSERT(ap->a_cnp->cn_flags & SAVENAME);
932
933	return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
934	    ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
935}
936
937/*
938 * Lookup entry point for the 'snapshot' directory.  Try to open the
939 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
940 * Perform a mount of the associated dataset on top of the vnode.
941 */
942/* ARGSUSED */
943int
944zfsctl_snapdir_lookup(ap)
945	struct vop_lookup_args /* {
946		struct vnode *a_dvp;
947		struct vnode **a_vpp;
948		struct componentname *a_cnp;
949	} */ *ap;
950{
951	vnode_t *dvp = ap->a_dvp;
952	vnode_t **vpp = ap->a_vpp;
953	struct componentname *cnp = ap->a_cnp;
954	char nm[NAME_MAX + 1];
955	zfsctl_snapdir_t *sdp = dvp->v_data;
956	objset_t *snap;
957	char snapname[MAXNAMELEN];
958	char real[MAXNAMELEN];
959	char *mountpoint;
960	zfs_snapentry_t *sep, search;
961	size_t mountpoint_len;
962	avl_index_t where;
963	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
964	int err;
965	int ltype, flags = 0;
966
967	/*
968	 * No extended attributes allowed under .zfs
969	 */
970	if (flags & LOOKUP_XATTR)
971		return (SET_ERROR(EINVAL));
972	ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
973	strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
974
975	ASSERT(dvp->v_type == VDIR);
976
977	*vpp = NULL;
978
979	/*
980	 * If we get a recursive call, that means we got called
981	 * from the domount() code while it was trying to look up the
982	 * spec (which looks like a local path for zfs).  We need to
983	 * add some flag to domount() to tell it not to do this lookup.
984	 */
985	if (MUTEX_HELD(&sdp->sd_lock))
986		return (SET_ERROR(ENOENT));
987
988	ZFS_ENTER(zfsvfs);
989	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
990		ZFS_EXIT(zfsvfs);
991		return (0);
992	}
993
994	if (flags & FIGNORECASE) {
995		boolean_t conflict = B_FALSE;
996
997		err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
998		    MAXNAMELEN, &conflict);
999		if (err == 0) {
1000			strlcpy(nm, real, sizeof(nm));
1001		} else if (err != ENOTSUP) {
1002			ZFS_EXIT(zfsvfs);
1003			return (err);
1004		}
1005#if 0
1006		if (realpnp)
1007			(void) strlcpy(realpnp->pn_buf, nm,
1008			    realpnp->pn_bufsize);
1009		if (conflict && direntflags)
1010			*direntflags = ED_CASE_CONFLICT;
1011#endif
1012	}
1013
1014	mutex_enter(&sdp->sd_lock);
1015	search.se_name = (char *)nm;
1016	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
1017		*vpp = sep->se_root;
1018		VN_HOLD(*vpp);
1019		err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
1020		if (err != 0) {
1021			VN_RELE(*vpp);
1022			*vpp = NULL;
1023		} else if (*vpp == sep->se_root) {
1024			/*
1025			 * The snapshot was unmounted behind our backs,
1026			 * try to remount it.
1027			 */
1028			VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0);
1029			goto domount;
1030		} else {
1031			/*
1032			 * VROOT was set during the traverse call.  We need
1033			 * to clear it since we're pretending to be part
1034			 * of our parent's vfs.
1035			 */
1036			(*vpp)->v_flag &= ~VROOT;
1037		}
1038		mutex_exit(&sdp->sd_lock);
1039		ZFS_EXIT(zfsvfs);
1040		return (err);
1041	}
1042
1043	/*
1044	 * The requested snapshot is not currently mounted, look it up.
1045	 */
1046	err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
1047	if (err != 0) {
1048		mutex_exit(&sdp->sd_lock);
1049		ZFS_EXIT(zfsvfs);
1050		/*
1051		 * handle "ls *" or "?" in a graceful manner,
1052		 * forcing EILSEQ to ENOENT.
1053		 * Since shell ultimately passes "*" or "?" as name to lookup
1054		 */
1055		return (err == EILSEQ ? ENOENT : err);
1056	}
1057	if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
1058		mutex_exit(&sdp->sd_lock);
1059#ifdef illumos
1060		ZFS_EXIT(zfsvfs);
1061		return (SET_ERROR(ENOENT));
1062#else	/* !illumos */
1063		/* Translate errors and add SAVENAME when needed. */
1064		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
1065			err = EJUSTRETURN;
1066			cnp->cn_flags |= SAVENAME;
1067		} else {
1068			err = SET_ERROR(ENOENT);
1069		}
1070		ZFS_EXIT(zfsvfs);
1071		return (err);
1072#endif	/* illumos */
1073	}
1074
1075	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
1076	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
1077	(void) strcpy(sep->se_name, nm);
1078	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
1079	VN_HOLD(*vpp);
1080	avl_insert(&sdp->sd_snaps, sep, where);
1081
1082	dmu_objset_rele(snap, FTAG);
1083domount:
1084	mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1085	    strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1;
1086	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1087	(void) snprintf(mountpoint, mountpoint_len,
1088	    "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1089	    dvp->v_vfsp->mnt_stat.f_mntonname, nm);
1090	err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
1091	kmem_free(mountpoint, mountpoint_len);
1092	if (err == 0) {
1093		/*
1094		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1095		 *
1096		 * This is where we lie about our v_vfsp in order to
1097		 * make .zfs/snapshot/<snapname> accessible over NFS
1098		 * without requiring manual mounts of <snapname>.
1099		 */
1100		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
1101		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1102	}
1103	mutex_exit(&sdp->sd_lock);
1104	ZFS_EXIT(zfsvfs);
1105
1106#ifdef illumos
1107	/*
1108	 * If we had an error, drop our hold on the vnode and
1109	 * zfsctl_snapshot_inactive() will clean up.
1110	 */
1111	if (err != 0) {
1112		VN_RELE(*vpp);
1113		*vpp = NULL;
1114	}
1115#else
1116	if (err != 0)
1117		*vpp = NULL;
1118#endif
1119	return (err);
1120}
1121
1122/* ARGSUSED */
1123int
1124zfsctl_shares_lookup(ap)
1125	struct vop_lookup_args /* {
1126		struct vnode *a_dvp;
1127		struct vnode **a_vpp;
1128		struct componentname *a_cnp;
1129	} */ *ap;
1130{
1131	vnode_t *dvp = ap->a_dvp;
1132	vnode_t **vpp = ap->a_vpp;
1133	struct componentname *cnp = ap->a_cnp;
1134	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1135	char nm[NAME_MAX + 1];
1136	znode_t *dzp;
1137	int error;
1138
1139	ZFS_ENTER(zfsvfs);
1140
1141	ASSERT(cnp->cn_namelen < sizeof(nm));
1142	strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1143
1144	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1145		ZFS_EXIT(zfsvfs);
1146		return (0);
1147	}
1148
1149	if (zfsvfs->z_shares_dir == 0) {
1150		ZFS_EXIT(zfsvfs);
1151		return (SET_ERROR(ENOTSUP));
1152	}
1153	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1154		error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1155		VN_RELE(ZTOV(dzp));
1156	}
1157
1158	ZFS_EXIT(zfsvfs);
1159
1160	return (error);
1161}
1162
1163/* ARGSUSED */
1164static int
1165zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1166    offset_t *offp, offset_t *nextp, void *data, int flags)
1167{
1168	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1169	char snapname[MAXNAMELEN];
1170	uint64_t id, cookie;
1171	boolean_t case_conflict;
1172	int error;
1173
1174	ZFS_ENTER(zfsvfs);
1175
1176	cookie = *offp;
1177	dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1178	error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1179	    &cookie, &case_conflict);
1180	dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1181	if (error) {
1182		ZFS_EXIT(zfsvfs);
1183		if (error == ENOENT) {
1184			*eofp = 1;
1185			return (0);
1186		}
1187		return (error);
1188	}
1189
1190	if (flags & V_RDDIR_ENTFLAGS) {
1191		edirent_t *eodp = dp;
1192
1193		(void) strcpy(eodp->ed_name, snapname);
1194		eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1195		eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1196	} else {
1197		struct dirent64 *odp = dp;
1198
1199		(void) strcpy(odp->d_name, snapname);
1200		odp->d_ino = ZFSCTL_INO_SNAP(id);
1201	}
1202	*nextp = cookie;
1203
1204	ZFS_EXIT(zfsvfs);
1205
1206	return (0);
1207}
1208
1209/* ARGSUSED */
1210static int
1211zfsctl_shares_readdir(ap)
1212	struct vop_readdir_args /* {
1213		struct vnode *a_vp;
1214		struct uio *a_uio;
1215		struct ucred *a_cred;
1216		int *a_eofflag;
1217		int *a_ncookies;
1218		u_long **a_cookies;
1219	} */ *ap;
1220{
1221	vnode_t *vp = ap->a_vp;
1222	uio_t *uiop = ap->a_uio;
1223	cred_t *cr = ap->a_cred;
1224	int *eofp = ap->a_eofflag;
1225	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1226	znode_t *dzp;
1227	int error;
1228
1229	ZFS_ENTER(zfsvfs);
1230
1231	if (zfsvfs->z_shares_dir == 0) {
1232		ZFS_EXIT(zfsvfs);
1233		return (SET_ERROR(ENOTSUP));
1234	}
1235	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1236		vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1237		error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1238		VN_URELE(ZTOV(dzp));
1239	} else {
1240		*eofp = 1;
1241		error = SET_ERROR(ENOENT);
1242	}
1243
1244	ZFS_EXIT(zfsvfs);
1245	return (error);
1246}
1247
1248/*
1249 * pvp is the '.zfs' directory (zfsctl_node_t).
1250 *
1251 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1252 *
1253 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1254 * when a lookup is performed on .zfs for "snapshot".
1255 */
1256vnode_t *
1257zfsctl_mknode_snapdir(vnode_t *pvp)
1258{
1259	vnode_t *vp;
1260	zfsctl_snapdir_t *sdp;
1261
1262	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1263	    &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1264	    zfsctl_snapdir_readdir_cb, NULL);
1265	sdp = vp->v_data;
1266	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1267	sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1268	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1269	avl_create(&sdp->sd_snaps, snapentry_compare,
1270	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1271	VOP_UNLOCK(vp, 0);
1272	return (vp);
1273}
1274
1275vnode_t *
1276zfsctl_mknode_shares(vnode_t *pvp)
1277{
1278	vnode_t *vp;
1279	zfsctl_node_t *sdp;
1280
1281	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1282	    &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1283	    NULL, NULL);
1284	sdp = vp->v_data;
1285	sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1286	VOP_UNLOCK(vp, 0);
1287	return (vp);
1288
1289}
1290
1291/* ARGSUSED */
1292static int
1293zfsctl_shares_getattr(ap)
1294	struct vop_getattr_args /* {
1295		struct vnode *a_vp;
1296		struct vattr *a_vap;
1297		struct ucred *a_cred;
1298		struct thread *a_td;
1299	} */ *ap;
1300{
1301	vnode_t *vp = ap->a_vp;
1302	vattr_t *vap = ap->a_vap;
1303	cred_t *cr = ap->a_cred;
1304	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1305	znode_t *dzp;
1306	int error;
1307
1308	ZFS_ENTER(zfsvfs);
1309	if (zfsvfs->z_shares_dir == 0) {
1310		ZFS_EXIT(zfsvfs);
1311		return (SET_ERROR(ENOTSUP));
1312	}
1313	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1314		vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1315		error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1316		VN_URELE(ZTOV(dzp));
1317	}
1318	ZFS_EXIT(zfsvfs);
1319	return (error);
1320
1321
1322}
1323
1324/* ARGSUSED */
1325static int
1326zfsctl_snapdir_getattr(ap)
1327	struct vop_getattr_args /* {
1328		struct vnode *a_vp;
1329		struct vattr *a_vap;
1330		struct ucred *a_cred;
1331	} */ *ap;
1332{
1333	vnode_t *vp = ap->a_vp;
1334	vattr_t *vap = ap->a_vap;
1335	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1336	zfsctl_snapdir_t *sdp = vp->v_data;
1337
1338	ZFS_ENTER(zfsvfs);
1339	zfsctl_common_getattr(vp, vap);
1340	vap->va_nodeid = gfs_file_inode(vp);
1341	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1342	vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1343	vap->va_birthtime = vap->va_ctime;
1344	ZFS_EXIT(zfsvfs);
1345
1346	return (0);
1347}
1348
1349/* ARGSUSED */
1350static int
1351zfsctl_snapdir_inactive(ap)
1352	struct vop_inactive_args /* {
1353		struct vnode *a_vp;
1354		struct thread *a_td;
1355	} */ *ap;
1356{
1357	vnode_t *vp = ap->a_vp;
1358	zfsctl_snapdir_t *sdp = vp->v_data;
1359	zfs_snapentry_t *sep;
1360
1361	/*
1362	 * On forced unmount we have to free snapshots from here.
1363	 */
1364	mutex_enter(&sdp->sd_lock);
1365	while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1366		avl_remove(&sdp->sd_snaps, sep);
1367		kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1368		kmem_free(sep, sizeof (zfs_snapentry_t));
1369	}
1370	mutex_exit(&sdp->sd_lock);
1371	gfs_dir_inactive(vp);
1372	ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1373	mutex_destroy(&sdp->sd_lock);
1374	avl_destroy(&sdp->sd_snaps);
1375	kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1376
1377	return (0);
1378}
1379
1380#ifdef illumos
1381static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1382	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1383	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1384	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1385	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_snapdir_getattr } },
1386	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1387	{ VOPNAME_RENAME,	{ .vop_rename = zfsctl_snapdir_rename }	},
1388	{ VOPNAME_RMDIR,	{ .vop_rmdir = zfsctl_snapdir_remove }	},
1389	{ VOPNAME_MKDIR,	{ .vop_mkdir = zfsctl_snapdir_mkdir }	},
1390	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir }	},
1391	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_snapdir_lookup }	},
1392	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1393	{ VOPNAME_INACTIVE,	{ .vop_inactive = zfsctl_snapdir_inactive } },
1394	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid }	},
1395	{ NULL }
1396};
1397
1398static const fs_operation_def_t zfsctl_tops_shares[] = {
1399	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1400	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1401	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1402	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_shares_getattr } },
1403	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1404	{ VOPNAME_READDIR,	{ .vop_readdir = zfsctl_shares_readdir } },
1405	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_shares_lookup }	},
1406	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1407	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1408	{ VOPNAME_FID,		{ .vop_fid = zfsctl_shares_fid } },
1409	{ NULL }
1410};
1411#else	/* !illumos */
1412static struct vop_vector zfsctl_ops_snapdir = {
1413	.vop_default =	&default_vnodeops,
1414	.vop_open =	zfsctl_common_open,
1415	.vop_close =	zfsctl_common_close,
1416	.vop_ioctl =	VOP_EINVAL,
1417	.vop_getattr =	zfsctl_snapdir_getattr,
1418	.vop_access =	zfsctl_common_access,
1419	.vop_mkdir =	zfsctl_freebsd_snapdir_mkdir,
1420	.vop_readdir =	gfs_vop_readdir,
1421	.vop_lookup =	zfsctl_snapdir_lookup,
1422	.vop_inactive =	zfsctl_snapdir_inactive,
1423	.vop_reclaim =	zfsctl_common_reclaim,
1424	.vop_fid =	zfsctl_common_fid,
1425};
1426
1427static struct vop_vector zfsctl_ops_shares = {
1428	.vop_default =	&default_vnodeops,
1429	.vop_open =	zfsctl_common_open,
1430	.vop_close =	zfsctl_common_close,
1431	.vop_ioctl =	VOP_EINVAL,
1432	.vop_getattr =	zfsctl_shares_getattr,
1433	.vop_access =	zfsctl_common_access,
1434	.vop_readdir =	zfsctl_shares_readdir,
1435	.vop_lookup =	zfsctl_shares_lookup,
1436	.vop_inactive =	VOP_NULL,
1437	.vop_reclaim =	gfs_vop_reclaim,
1438	.vop_fid =	zfsctl_shares_fid,
1439};
1440#endif	/* illumos */
1441
1442/*
1443 * pvp is the GFS vnode '.zfs/snapshot'.
1444 *
1445 * This creates a GFS node under '.zfs/snapshot' representing each
1446 * snapshot.  This newly created GFS node is what we mount snapshot
1447 * vfs_t's ontop of.
1448 */
1449static vnode_t *
1450zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1451{
1452	vnode_t *vp;
1453	zfsctl_node_t *zcp;
1454
1455	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1456	    &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1457	VN_HOLD(vp);
1458	zcp = vp->v_data;
1459	zcp->zc_id = objset;
1460	VOP_UNLOCK(vp, 0);
1461
1462	return (vp);
1463}
1464
1465
1466static int
1467zfsctl_snapshot_reclaim(ap)
1468	struct vop_inactive_args /* {
1469		struct vnode *a_vp;
1470		struct thread *a_td;
1471	} */ *ap;
1472{
1473	vnode_t *vp = ap->a_vp;
1474	cred_t *cr = ap->a_td->td_ucred;
1475	struct vop_reclaim_args iap;
1476	zfsctl_snapdir_t *sdp;
1477	zfs_snapentry_t *sep, *next;
1478	int locked;
1479	vnode_t *dvp;
1480
1481	VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1482	sdp = dvp->v_data;
1483	VOP_UNLOCK(dvp, 0);
1484	/* this may already have been unmounted */
1485	if (sdp == NULL) {
1486		VN_RELE(dvp);
1487		return (0);
1488	}
1489	if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1490		mutex_enter(&sdp->sd_lock);
1491
1492	ASSERT(!vn_ismntpt(vp));
1493
1494	sep = avl_first(&sdp->sd_snaps);
1495	while (sep != NULL) {
1496		next = AVL_NEXT(&sdp->sd_snaps, sep);
1497
1498		if (sep->se_root == vp) {
1499			avl_remove(&sdp->sd_snaps, sep);
1500			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1501			kmem_free(sep, sizeof (zfs_snapentry_t));
1502			break;
1503		}
1504		sep = next;
1505	}
1506	ASSERT(sep != NULL);
1507
1508	if (!locked)
1509		mutex_exit(&sdp->sd_lock);
1510	VN_RELE(dvp);
1511
1512	/*
1513	 * Dispose of the vnode for the snapshot mount point.
1514	 * This is safe to do because once this entry has been removed
1515	 * from the AVL tree, it can't be found again, so cannot become
1516	 * "active".  If we lookup the same name again we will end up
1517	 * creating a new vnode.
1518	 */
1519	iap.a_vp = vp;
1520	gfs_vop_reclaim(&iap);
1521	return (0);
1522
1523}
1524
1525static int
1526zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1527{
1528
1529	VN_HOLD(*vpp);
1530	/* Snapshot should be already mounted, but just in case. */
1531	if (vn_mountedvfs(*vpp) == NULL)
1532		return (ENOENT);
1533	return (traverse(vpp, lktype));
1534}
1535
1536static void
1537zfsctl_traverse_end(vnode_t *vp, int err)
1538{
1539
1540	if (err == 0)
1541		vput(vp);
1542	else
1543		VN_RELE(vp);
1544}
1545
1546static int
1547zfsctl_snapshot_getattr(ap)
1548	struct vop_getattr_args /* {
1549		struct vnode *a_vp;
1550		struct vattr *a_vap;
1551		struct ucred *a_cred;
1552	} */ *ap;
1553{
1554	vnode_t *vp = ap->a_vp;
1555	int err;
1556
1557	err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1558	if (err == 0)
1559		err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1560	zfsctl_traverse_end(vp, err);
1561	return (err);
1562}
1563
1564static int
1565zfsctl_snapshot_fid(ap)
1566	struct vop_fid_args /* {
1567		struct vnode *a_vp;
1568		struct fid *a_fid;
1569	} */ *ap;
1570{
1571	vnode_t *vp = ap->a_vp;
1572	int err;
1573
1574	err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1575	if (err == 0)
1576		err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1577	zfsctl_traverse_end(vp, err);
1578	return (err);
1579}
1580
1581static int
1582zfsctl_snapshot_lookup(ap)
1583	struct vop_lookup_args /* {
1584		struct vnode *a_dvp;
1585		struct vnode **a_vpp;
1586		struct componentname *a_cnp;
1587	} */ *ap;
1588{
1589	vnode_t *dvp = ap->a_dvp;
1590	vnode_t **vpp = ap->a_vpp;
1591	struct componentname *cnp = ap->a_cnp;
1592	cred_t *cr = ap->a_cnp->cn_cred;
1593	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1594	int error;
1595
1596	if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1597	    cnp->cn_nameptr[1] != '.') {
1598		return (ENOENT);
1599	}
1600
1601	ASSERT(dvp->v_type == VDIR);
1602	ASSERT(zfsvfs->z_ctldir != NULL);
1603
1604	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1605	    NULL, 0, NULL, cr, NULL, NULL, NULL);
1606	if (error == 0) {
1607		int ltype = VOP_ISLOCKED(dvp);
1608		VN_HOLD(*vpp);
1609		VOP_UNLOCK(dvp, 0);
1610		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1611		VN_RELE(*vpp);
1612		vn_lock(dvp, ltype | LK_RETRY);
1613	}
1614
1615	return (error);
1616}
1617
1618static int
1619zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1620{
1621	zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1622	vnode_t *dvp, *vp;
1623	zfsctl_snapdir_t *sdp;
1624	zfs_snapentry_t *sep;
1625	int error;
1626
1627	ASSERT(zfsvfs->z_ctldir != NULL);
1628	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1629	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1630	if (error != 0)
1631		return (error);
1632	sdp = dvp->v_data;
1633
1634	mutex_enter(&sdp->sd_lock);
1635	sep = avl_first(&sdp->sd_snaps);
1636	while (sep != NULL) {
1637		vp = sep->se_root;
1638		if (vp == ap->a_vp)
1639			break;
1640		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1641	}
1642	if (sep == NULL) {
1643		mutex_exit(&sdp->sd_lock);
1644		error = ENOENT;
1645	} else {
1646		size_t len;
1647
1648		len = strlen(sep->se_name);
1649		*ap->a_buflen -= len;
1650		bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1651		mutex_exit(&sdp->sd_lock);
1652		vref(dvp);
1653		*ap->a_vpp = dvp;
1654	}
1655	VN_RELE(dvp);
1656
1657	return (error);
1658}
1659
1660/*
1661 * These VP's should never see the light of day.  They should always
1662 * be covered.
1663 */
1664static struct vop_vector zfsctl_ops_snapshot = {
1665	.vop_default =	&default_vnodeops,
1666	.vop_inactive =	VOP_NULL,
1667	.vop_lookup =	zfsctl_snapshot_lookup,
1668	.vop_reclaim =	zfsctl_snapshot_reclaim,
1669	.vop_getattr =	zfsctl_snapshot_getattr,
1670	.vop_fid =	zfsctl_snapshot_fid,
1671	.vop_vptocnp =	zfsctl_snapshot_vptocnp,
1672};
1673
1674int
1675zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1676{
1677	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1678	vnode_t *dvp, *vp;
1679	zfsctl_snapdir_t *sdp;
1680	zfsctl_node_t *zcp;
1681	zfs_snapentry_t *sep;
1682	int error;
1683
1684	ASSERT(zfsvfs->z_ctldir != NULL);
1685	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1686	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1687	if (error != 0)
1688		return (error);
1689	sdp = dvp->v_data;
1690
1691	mutex_enter(&sdp->sd_lock);
1692	sep = avl_first(&sdp->sd_snaps);
1693	while (sep != NULL) {
1694		vp = sep->se_root;
1695		zcp = vp->v_data;
1696		if (zcp->zc_id == objsetid)
1697			break;
1698
1699		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1700	}
1701
1702	if (sep != NULL) {
1703		VN_HOLD(vp);
1704		/*
1705		 * Return the mounted root rather than the covered mount point.
1706		 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1707		 * and returns the ZFS vnode mounted on top of the GFS node.
1708		 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1709		 */
1710		error = traverse(&vp, LK_SHARED | LK_RETRY);
1711		if (error == 0) {
1712			if (vp == sep->se_root)
1713				error = SET_ERROR(EINVAL);
1714			else
1715				*zfsvfsp = VTOZ(vp)->z_zfsvfs;
1716		}
1717		mutex_exit(&sdp->sd_lock);
1718		if (error == 0)
1719			VN_URELE(vp);
1720		else
1721			VN_RELE(vp);
1722	} else {
1723		error = SET_ERROR(EINVAL);
1724		mutex_exit(&sdp->sd_lock);
1725	}
1726
1727	VN_RELE(dvp);
1728
1729	return (error);
1730}
1731
1732/*
1733 * Unmount any snapshots for the given filesystem.  This is called from
1734 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1735 * snapshots.
1736 */
1737int
1738zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1739{
1740	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1741	vnode_t *dvp;
1742	zfsctl_snapdir_t *sdp;
1743	zfs_snapentry_t *sep, *next;
1744	int error;
1745
1746	ASSERT(zfsvfs->z_ctldir != NULL);
1747	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1748	    NULL, 0, NULL, cr, NULL, NULL, NULL);
1749	if (error != 0)
1750		return (error);
1751	sdp = dvp->v_data;
1752
1753	mutex_enter(&sdp->sd_lock);
1754
1755	sep = avl_first(&sdp->sd_snaps);
1756	while (sep != NULL) {
1757		next = AVL_NEXT(&sdp->sd_snaps, sep);
1758
1759		/*
1760		 * If this snapshot is not mounted, then it must
1761		 * have just been unmounted by somebody else, and
1762		 * will be cleaned up by zfsctl_snapdir_inactive().
1763		 */
1764		if (vn_ismntpt(sep->se_root)) {
1765			error = zfsctl_unmount_snap(sep, fflags, cr);
1766			if (error) {
1767				avl_index_t where;
1768
1769				/*
1770				 * Before reinserting snapshot to the tree,
1771				 * check if it was actually removed. For example
1772				 * when snapshot mount point is busy, we will
1773				 * have an error here, but there will be no need
1774				 * to reinsert snapshot.
1775				 */
1776				if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1777					avl_insert(&sdp->sd_snaps, sep, where);
1778				break;
1779			}
1780		}
1781		sep = next;
1782	}
1783
1784	mutex_exit(&sdp->sd_lock);
1785	VN_RELE(dvp);
1786
1787	return (error);
1788}
1789