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