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