xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_ctldir.c (revision a1988827cf132ed4264c002279c6126834830520)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
50f2dc02eSek110237  * Common Development and Distribution License (the "License").
60f2dc02eSek110237  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22d39ee142SMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*a1988827SMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * ZFS control directory (a.k.a. ".zfs")
28fa9e4066Sahrens  *
29fa9e4066Sahrens  * This directory provides a common location for all ZFS meta-objects.
30fa9e4066Sahrens  * Currently, this is only the 'snapshot' directory, but this may expand in the
31fa9e4066Sahrens  * future.  The elements are built using the GFS primitives, as the hierarchy
32fa9e4066Sahrens  * does not actually exist on disk.
33fa9e4066Sahrens  *
34fa9e4066Sahrens  * For 'snapshot', we don't want to have all snapshots always mounted, because
35fa9e4066Sahrens  * this would take up a huge amount of space in /etc/mnttab.  We have three
36fa9e4066Sahrens  * types of objects:
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * 	ctldir ------> snapshotdir -------> snapshot
39fa9e4066Sahrens  *                                             |
40fa9e4066Sahrens  *                                             |
41fa9e4066Sahrens  *                                             V
42fa9e4066Sahrens  *                                         mounted fs
43fa9e4066Sahrens  *
44fa9e4066Sahrens  * The 'snapshot' node contains just enough information to lookup '..' and act
45fa9e4066Sahrens  * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
46fa9e4066Sahrens  * perform an automount of the underlying filesystem and return the
47fa9e4066Sahrens  * corresponding vnode.
48fa9e4066Sahrens  *
49fa9e4066Sahrens  * All mounts are handled automatically by the kernel, but unmounts are
50fa9e4066Sahrens  * (currently) handled from user land.  The main reason is that there is no
51fa9e4066Sahrens  * reliable way to auto-unmount the filesystem when it's "no longer in use".
52fa9e4066Sahrens  * When the user unmounts a filesystem, we call zfsctl_unmount(), which
53fa9e4066Sahrens  * unmounts any snapshots within the snapshot directory.
54f18faf3fSek110237  *
55f18faf3fSek110237  * The '.zfs', '.zfs/snapshot', and all directories created under
56f18faf3fSek110237  * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
57f18faf3fSek110237  * share the same vfs_t as the head filesystem (what '.zfs' lives under).
58f18faf3fSek110237  *
59f18faf3fSek110237  * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
60f18faf3fSek110237  * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
61f18faf3fSek110237  * However, vnodes within these mounted on file systems have their v_vfsp
62f18faf3fSek110237  * fields set to the head filesystem to make NFS happy (see
638bf40bf0Sck153898  * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
648bf40bf0Sck153898  * so that it cannot be freed until all snapshots have been unmounted.
65fa9e4066Sahrens  */
66fa9e4066Sahrens 
67fa9e4066Sahrens #include <fs/fs_subr.h>
68fa9e4066Sahrens #include <sys/zfs_ctldir.h>
69fa9e4066Sahrens #include <sys/zfs_ioctl.h>
70fa9e4066Sahrens #include <sys/zfs_vfsops.h>
71aa59c4cbSrsb #include <sys/vfs_opreg.h>
72fa9e4066Sahrens #include <sys/gfs.h>
73fa9e4066Sahrens #include <sys/stat.h>
74fa9e4066Sahrens #include <sys/dmu.h>
753b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
76ecd6cf80Smarks #include <sys/dsl_deleg.h>
77fa9e4066Sahrens #include <sys/mount.h>
78ab04eb8eStimh #include <sys/sunddi.h>
79fa9e4066Sahrens 
806d1e0b89Smarks #include "zfs_namecheck.h"
816d1e0b89Smarks 
828bf40bf0Sck153898 typedef struct zfsctl_node {
838bf40bf0Sck153898 	gfs_dir_t	zc_gfs_private;
848bf40bf0Sck153898 	uint64_t	zc_id;
858bf40bf0Sck153898 	timestruc_t	zc_cmtime;	/* ctime and mtime, always the same */
868bf40bf0Sck153898 } zfsctl_node_t;
878bf40bf0Sck153898 
888bf40bf0Sck153898 typedef struct zfsctl_snapdir {
898bf40bf0Sck153898 	zfsctl_node_t	sd_node;
908bf40bf0Sck153898 	kmutex_t	sd_lock;
918bf40bf0Sck153898 	avl_tree_t	sd_snaps;
928bf40bf0Sck153898 } zfsctl_snapdir_t;
938bf40bf0Sck153898 
94fa9e4066Sahrens typedef struct {
95fa9e4066Sahrens 	char		*se_name;
96fa9e4066Sahrens 	vnode_t		*se_root;
97fa9e4066Sahrens 	avl_node_t	se_node;
98fa9e4066Sahrens } zfs_snapentry_t;
99fa9e4066Sahrens 
100fa9e4066Sahrens static int
snapentry_compare(const void * a,const void * b)101fa9e4066Sahrens snapentry_compare(const void *a, const void *b)
102fa9e4066Sahrens {
103fa9e4066Sahrens 	const zfs_snapentry_t *sa = a;
104fa9e4066Sahrens 	const zfs_snapentry_t *sb = b;
105fa9e4066Sahrens 	int ret = strcmp(sa->se_name, sb->se_name);
106fa9e4066Sahrens 
107fa9e4066Sahrens 	if (ret < 0)
108fa9e4066Sahrens 		return (-1);
109fa9e4066Sahrens 	else if (ret > 0)
110fa9e4066Sahrens 		return (1);
111fa9e4066Sahrens 	else
112fa9e4066Sahrens 		return (0);
113fa9e4066Sahrens }
114fa9e4066Sahrens 
115fa9e4066Sahrens vnodeops_t *zfsctl_ops_root;
116fa9e4066Sahrens vnodeops_t *zfsctl_ops_snapdir;
117fa9e4066Sahrens vnodeops_t *zfsctl_ops_snapshot;
118743a77edSAlan Wright vnodeops_t *zfsctl_ops_shares;
119743a77edSAlan Wright vnodeops_t *zfsctl_ops_shares_dir;
120fa9e4066Sahrens 
121fa9e4066Sahrens static const fs_operation_def_t zfsctl_tops_root[];
122fa9e4066Sahrens static const fs_operation_def_t zfsctl_tops_snapdir[];
123fa9e4066Sahrens static const fs_operation_def_t zfsctl_tops_snapshot[];
124743a77edSAlan Wright static const fs_operation_def_t zfsctl_tops_shares[];
125fa9e4066Sahrens 
126fa9e4066Sahrens static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
127743a77edSAlan Wright static vnode_t *zfsctl_mknode_shares(vnode_t *);
128fa9e4066Sahrens static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
1298bf40bf0Sck153898 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
130fa9e4066Sahrens 
131fa9e4066Sahrens static gfs_opsvec_t zfsctl_opsvec[] = {
132fa9e4066Sahrens 	{ ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
133fa9e4066Sahrens 	{ ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
134fa9e4066Sahrens 	{ ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
135743a77edSAlan Wright 	{ ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
136743a77edSAlan Wright 	{ ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
137fa9e4066Sahrens 	{ NULL }
138fa9e4066Sahrens };
139fa9e4066Sahrens 
140fa9e4066Sahrens /*
141743a77edSAlan Wright  * Root directory elements.  We only have two entries
142743a77edSAlan Wright  * snapshot and shares.
143fa9e4066Sahrens  */
144fa9e4066Sahrens static gfs_dirent_t zfsctl_root_entries[] = {
145fa9e4066Sahrens 	{ "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
146743a77edSAlan Wright 	{ "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
147fa9e4066Sahrens 	{ NULL }
148fa9e4066Sahrens };
149fa9e4066Sahrens 
150fa9e4066Sahrens /* include . and .. in the calculation */
151fa9e4066Sahrens #define	NROOT_ENTRIES	((sizeof (zfsctl_root_entries) / \
152fa9e4066Sahrens     sizeof (gfs_dirent_t)) + 1)
153fa9e4066Sahrens 
154fa9e4066Sahrens 
155fa9e4066Sahrens /*
156fa9e4066Sahrens  * Initialize the various GFS pieces we'll need to create and manipulate .zfs
157fa9e4066Sahrens  * directories.  This is called from the ZFS init routine, and initializes the
158fa9e4066Sahrens  * vnode ops vectors that we'll be using.
159fa9e4066Sahrens  */
160fa9e4066Sahrens void
zfsctl_init(void)161fa9e4066Sahrens zfsctl_init(void)
162fa9e4066Sahrens {
163fa9e4066Sahrens 	VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
164fa9e4066Sahrens }
165fa9e4066Sahrens 
166fa9e4066Sahrens void
zfsctl_fini(void)167fa9e4066Sahrens zfsctl_fini(void)
168fa9e4066Sahrens {
169fa9e4066Sahrens 	/*
170fa9e4066Sahrens 	 * Remove vfsctl vnode ops
171fa9e4066Sahrens 	 */
172fa9e4066Sahrens 	if (zfsctl_ops_root)
173fa9e4066Sahrens 		vn_freevnodeops(zfsctl_ops_root);
174fa9e4066Sahrens 	if (zfsctl_ops_snapdir)
175fa9e4066Sahrens 		vn_freevnodeops(zfsctl_ops_snapdir);
176fa9e4066Sahrens 	if (zfsctl_ops_snapshot)
177fa9e4066Sahrens 		vn_freevnodeops(zfsctl_ops_snapshot);
178743a77edSAlan Wright 	if (zfsctl_ops_shares)
179743a77edSAlan Wright 		vn_freevnodeops(zfsctl_ops_shares);
180743a77edSAlan Wright 	if (zfsctl_ops_shares_dir)
181743a77edSAlan Wright 		vn_freevnodeops(zfsctl_ops_shares_dir);
182fa9e4066Sahrens 
183fa9e4066Sahrens 	zfsctl_ops_root = NULL;
184fa9e4066Sahrens 	zfsctl_ops_snapdir = NULL;
185fa9e4066Sahrens 	zfsctl_ops_snapshot = NULL;
186743a77edSAlan Wright 	zfsctl_ops_shares = NULL;
187743a77edSAlan Wright 	zfsctl_ops_shares_dir = NULL;
188fa9e4066Sahrens }
189fa9e4066Sahrens 
190d39ee142SMark Shellenbaum boolean_t
zfsctl_is_node(vnode_t * vp)191d39ee142SMark Shellenbaum zfsctl_is_node(vnode_t *vp)
192d39ee142SMark Shellenbaum {
193d39ee142SMark Shellenbaum 	return (vn_matchops(vp, zfsctl_ops_root) ||
194d39ee142SMark Shellenbaum 	    vn_matchops(vp, zfsctl_ops_snapdir) ||
195d39ee142SMark Shellenbaum 	    vn_matchops(vp, zfsctl_ops_snapshot) ||
196d39ee142SMark Shellenbaum 	    vn_matchops(vp, zfsctl_ops_shares) ||
197d39ee142SMark Shellenbaum 	    vn_matchops(vp, zfsctl_ops_shares_dir));
198d39ee142SMark Shellenbaum 
199d39ee142SMark Shellenbaum }
200d39ee142SMark Shellenbaum 
201fa9e4066Sahrens /*
202743a77edSAlan Wright  * Return the inode number associated with the 'snapshot' or
203743a77edSAlan Wright  * 'shares' directory.
204fa9e4066Sahrens  */
205fa9e4066Sahrens /* ARGSUSED */
206fa9e4066Sahrens static ino64_t
zfsctl_root_inode_cb(vnode_t * vp,int index)207fa9e4066Sahrens zfsctl_root_inode_cb(vnode_t *vp, int index)
208fa9e4066Sahrens {
209743a77edSAlan Wright 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
210743a77edSAlan Wright 
211743a77edSAlan Wright 	ASSERT(index <= 2);
212743a77edSAlan Wright 
213743a77edSAlan Wright 	if (index == 0)
214fa9e4066Sahrens 		return (ZFSCTL_INO_SNAPDIR);
215743a77edSAlan Wright 
216743a77edSAlan Wright 	return (zfsvfs->z_shares_dir);
217fa9e4066Sahrens }
218fa9e4066Sahrens 
219fa9e4066Sahrens /*
220fa9e4066Sahrens  * Create the '.zfs' directory.  This directory is cached as part of the VFS
221fa9e4066Sahrens  * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
222fa9e4066Sahrens  * therefore checks against a vfs_count of 2 instead of 1.  This reference
223fa9e4066Sahrens  * is removed when the ctldir is destroyed in the unmount.
224fa9e4066Sahrens  */
225fa9e4066Sahrens void
zfsctl_create(zfsvfs_t * zfsvfs)226fa9e4066Sahrens zfsctl_create(zfsvfs_t *zfsvfs)
227fa9e4066Sahrens {
228a32fded1Sek110237 	vnode_t *vp, *rvp;
229fa9e4066Sahrens 	zfsctl_node_t *zcp;
2300a586ceaSMark Shellenbaum 	uint64_t crtime[2];
231fa9e4066Sahrens 
232fa9e4066Sahrens 	ASSERT(zfsvfs->z_ctldir == NULL);
233fa9e4066Sahrens 
234fa9e4066Sahrens 	vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
235fa9e4066Sahrens 	    zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
236fa9e4066Sahrens 	    zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
237fa9e4066Sahrens 	zcp = vp->v_data;
238fa9e4066Sahrens 	zcp->zc_id = ZFSCTL_INO_ROOT;
239fa9e4066Sahrens 
240a32fded1Sek110237 	VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0);
2410a586ceaSMark Shellenbaum 	VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2420a586ceaSMark Shellenbaum 	    &crtime, sizeof (crtime)));
2430a586ceaSMark Shellenbaum 	ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
244a32fded1Sek110237 	VN_RELE(rvp);
245a32fded1Sek110237 
246fa9e4066Sahrens 	/*
247fa9e4066Sahrens 	 * We're only faking the fact that we have a root of a filesystem for
248fa9e4066Sahrens 	 * the sake of the GFS interfaces.  Undo the flag manipulation it did
249fa9e4066Sahrens 	 * for us.
250fa9e4066Sahrens 	 */
251fa9e4066Sahrens 	vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
252fa9e4066Sahrens 
253fa9e4066Sahrens 	zfsvfs->z_ctldir = vp;
254fa9e4066Sahrens }
255fa9e4066Sahrens 
256fa9e4066Sahrens /*
2578afd4dd6Sperrin  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
2588afd4dd6Sperrin  * There might still be more references if we were force unmounted, but only
2598afd4dd6Sperrin  * new zfs_inactive() calls can occur and they don't reference .zfs
260fa9e4066Sahrens  */
261fa9e4066Sahrens void
zfsctl_destroy(zfsvfs_t * zfsvfs)262fa9e4066Sahrens zfsctl_destroy(zfsvfs_t *zfsvfs)
263fa9e4066Sahrens {
264fa9e4066Sahrens 	VN_RELE(zfsvfs->z_ctldir);
265fa9e4066Sahrens 	zfsvfs->z_ctldir = NULL;
266fa9e4066Sahrens }
267fa9e4066Sahrens 
268fa9e4066Sahrens /*
269fa9e4066Sahrens  * Given a root znode, retrieve the associated .zfs directory.
270fa9e4066Sahrens  * Add a hold to the vnode and return it.
271fa9e4066Sahrens  */
272fa9e4066Sahrens vnode_t *
zfsctl_root(znode_t * zp)273fa9e4066Sahrens zfsctl_root(znode_t *zp)
274fa9e4066Sahrens {
275fa9e4066Sahrens 	ASSERT(zfs_has_ctldir(zp));
276fa9e4066Sahrens 	VN_HOLD(zp->z_zfsvfs->z_ctldir);
277fa9e4066Sahrens 	return (zp->z_zfsvfs->z_ctldir);
278fa9e4066Sahrens }
279fa9e4066Sahrens 
280fa9e4066Sahrens /*
281fa9e4066Sahrens  * Common open routine.  Disallow any write access.
282fa9e4066Sahrens  */
283fa9e4066Sahrens /* ARGSUSED */
284fa9e4066Sahrens static int
zfsctl_common_open(vnode_t ** vpp,int flags,cred_t * cr,caller_context_t * ct)285da6c28aaSamw zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
286fa9e4066Sahrens {
287fa9e4066Sahrens 	if (flags & FWRITE)
288be6fd75aSMatthew Ahrens 		return (SET_ERROR(EACCES));
289fa9e4066Sahrens 
290fa9e4066Sahrens 	return (0);
291fa9e4066Sahrens }
292fa9e4066Sahrens 
293fa9e4066Sahrens /*
294fa9e4066Sahrens  * Common close routine.  Nothing to do here.
295fa9e4066Sahrens  */
296fa9e4066Sahrens /* ARGSUSED */
297fa9e4066Sahrens static int
zfsctl_common_close(vnode_t * vpp,int flags,int count,offset_t off,cred_t * cr,caller_context_t * ct)298fa9e4066Sahrens zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
299da6c28aaSamw     cred_t *cr, caller_context_t *ct)
300fa9e4066Sahrens {
301fa9e4066Sahrens 	return (0);
302fa9e4066Sahrens }
303fa9e4066Sahrens 
304fa9e4066Sahrens /*
305fa9e4066Sahrens  * Common access routine.  Disallow writes.
306fa9e4066Sahrens  */
307fa9e4066Sahrens /* ARGSUSED */
308fa9e4066Sahrens static int
zfsctl_common_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)309da6c28aaSamw zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr,
310da6c28aaSamw     caller_context_t *ct)
311fa9e4066Sahrens {
31260030f27SMark Shellenbaum 	if (flags & V_ACE_MASK) {
31360030f27SMark Shellenbaum 		if (mode & ACE_ALL_WRITE_PERMS)
314be6fd75aSMatthew Ahrens 			return (SET_ERROR(EACCES));
31560030f27SMark Shellenbaum 	} else {
316fa9e4066Sahrens 		if (mode & VWRITE)
317be6fd75aSMatthew Ahrens 			return (SET_ERROR(EACCES));
31860030f27SMark Shellenbaum 	}
319fa9e4066Sahrens 
320fa9e4066Sahrens 	return (0);
321fa9e4066Sahrens }
322fa9e4066Sahrens 
323fa9e4066Sahrens /*
324fa9e4066Sahrens  * Common getattr function.  Fill in basic information.
325fa9e4066Sahrens  */
326fa9e4066Sahrens static void
zfsctl_common_getattr(vnode_t * vp,vattr_t * vap)327fa9e4066Sahrens zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
328fa9e4066Sahrens {
329fa9e4066Sahrens 	timestruc_t	now;
330fa9e4066Sahrens 
331fa9e4066Sahrens 	vap->va_uid = 0;
332fa9e4066Sahrens 	vap->va_gid = 0;
333fa9e4066Sahrens 	vap->va_rdev = 0;
334fa9e4066Sahrens 	/*
33571eb0538SChris Kirby 	 * We are a purely virtual object, so we have no
336fa9e4066Sahrens 	 * blocksize or allocated blocks.
337fa9e4066Sahrens 	 */
338fa9e4066Sahrens 	vap->va_blksize = 0;
339fa9e4066Sahrens 	vap->va_nblocks = 0;
340fa9e4066Sahrens 	vap->va_seq = 0;
341fa9e4066Sahrens 	vap->va_fsid = vp->v_vfsp->vfs_dev;
342fa9e4066Sahrens 	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
343fa9e4066Sahrens 	    S_IROTH | S_IXOTH;
344fa9e4066Sahrens 	vap->va_type = VDIR;
345fa9e4066Sahrens 	/*
346a32fded1Sek110237 	 * We live in the now (for atime).
347fa9e4066Sahrens 	 */
348fa9e4066Sahrens 	gethrestime(&now);
349a32fded1Sek110237 	vap->va_atime = now;
350fa9e4066Sahrens }
351fa9e4066Sahrens 
352da6c28aaSamw /*ARGSUSED*/
353fa9e4066Sahrens static int
zfsctl_common_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)354da6c28aaSamw zfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
355fa9e4066Sahrens {
356fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
357fa9e4066Sahrens 	zfsctl_node_t	*zcp = vp->v_data;
358fa9e4066Sahrens 	uint64_t	object = zcp->zc_id;
359fa9e4066Sahrens 	zfid_short_t	*zfid;
360fa9e4066Sahrens 	int		i;
361fa9e4066Sahrens 
362fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
363fa9e4066Sahrens 
364fa9e4066Sahrens 	if (fidp->fid_len < SHORT_FID_LEN) {
365fa9e4066Sahrens 		fidp->fid_len = SHORT_FID_LEN;
3660f2dc02eSek110237 		ZFS_EXIT(zfsvfs);
367be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
368fa9e4066Sahrens 	}
369fa9e4066Sahrens 
370fa9e4066Sahrens 	zfid = (zfid_short_t *)fidp;
371fa9e4066Sahrens 
372fa9e4066Sahrens 	zfid->zf_len = SHORT_FID_LEN;
373fa9e4066Sahrens 
374fa9e4066Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
375fa9e4066Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
376fa9e4066Sahrens 
377fa9e4066Sahrens 	/* .zfs znodes always have a generation number of 0 */
378fa9e4066Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
379fa9e4066Sahrens 		zfid->zf_gen[i] = 0;
380fa9e4066Sahrens 
381fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
382fa9e4066Sahrens 	return (0);
383fa9e4066Sahrens }
384fa9e4066Sahrens 
385743a77edSAlan Wright 
386743a77edSAlan Wright /*ARGSUSED*/
387743a77edSAlan Wright static int
zfsctl_shares_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)388743a77edSAlan Wright zfsctl_shares_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
389743a77edSAlan Wright {
390743a77edSAlan Wright 	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
391743a77edSAlan Wright 	znode_t		*dzp;
392743a77edSAlan Wright 	int		error;
393743a77edSAlan Wright 
394743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
3959e1320c0SMark Shellenbaum 
3969e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
3979e1320c0SMark Shellenbaum 		ZFS_EXIT(zfsvfs);
398be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3999e1320c0SMark Shellenbaum 	}
4009e1320c0SMark Shellenbaum 
401743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
402743a77edSAlan Wright 		error = VOP_FID(ZTOV(dzp), fidp, ct);
403743a77edSAlan Wright 		VN_RELE(ZTOV(dzp));
404743a77edSAlan Wright 	}
405743a77edSAlan Wright 
406743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
407743a77edSAlan Wright 	return (error);
408743a77edSAlan Wright }
409fa9e4066Sahrens /*
410fa9e4066Sahrens  * .zfs inode namespace
411fa9e4066Sahrens  *
412fa9e4066Sahrens  * We need to generate unique inode numbers for all files and directories
413fa9e4066Sahrens  * within the .zfs pseudo-filesystem.  We use the following scheme:
414fa9e4066Sahrens  *
415fa9e4066Sahrens  * 	ENTRY			ZFSCTL_INODE
416fa9e4066Sahrens  * 	.zfs			1
417fa9e4066Sahrens  * 	.zfs/snapshot		2
418fa9e4066Sahrens  * 	.zfs/snapshot/<snap>	objectid(snap)
419fa9e4066Sahrens  */
420fa9e4066Sahrens 
421fa9e4066Sahrens #define	ZFSCTL_INO_SNAP(id)	(id)
422fa9e4066Sahrens 
423fa9e4066Sahrens /*
424fa9e4066Sahrens  * Get root directory attributes.
425fa9e4066Sahrens  */
426fa9e4066Sahrens /* ARGSUSED */
427fa9e4066Sahrens static int
zfsctl_root_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)428da6c28aaSamw zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
429da6c28aaSamw     caller_context_t *ct)
430fa9e4066Sahrens {
431fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
43271eb0538SChris Kirby 	zfsctl_node_t *zcp = vp->v_data;
433fa9e4066Sahrens 
434fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
435fa9e4066Sahrens 	vap->va_nodeid = ZFSCTL_INO_ROOT;
436fa9e4066Sahrens 	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
43771eb0538SChris Kirby 	vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
438fa9e4066Sahrens 
439fa9e4066Sahrens 	zfsctl_common_getattr(vp, vap);
440fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
441fa9e4066Sahrens 
442fa9e4066Sahrens 	return (0);
443fa9e4066Sahrens }
444fa9e4066Sahrens 
445fa9e4066Sahrens /*
446fa9e4066Sahrens  * Special case the handling of "..".
447fa9e4066Sahrens  */
448fa9e4066Sahrens /* ARGSUSED */
449fa9e4066Sahrens int
zfsctl_root_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,pathname_t * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)450fa9e4066Sahrens zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
451da6c28aaSamw     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
452da6c28aaSamw     int *direntflags, pathname_t *realpnp)
453fa9e4066Sahrens {
454fa9e4066Sahrens 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
455fa9e4066Sahrens 	int err;
456fa9e4066Sahrens 
457da6c28aaSamw 	/*
458da6c28aaSamw 	 * No extended attributes allowed under .zfs
459da6c28aaSamw 	 */
460da6c28aaSamw 	if (flags & LOOKUP_XATTR)
461be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
462da6c28aaSamw 
463fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
464fa9e4066Sahrens 
465fa9e4066Sahrens 	if (strcmp(nm, "..") == 0) {
466fa9e4066Sahrens 		err = VFS_ROOT(dvp->v_vfsp, vpp);
467fa9e4066Sahrens 	} else {
468ab04eb8eStimh 		err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
469ab04eb8eStimh 		    cr, ct, direntflags, realpnp);
470fa9e4066Sahrens 	}
471fa9e4066Sahrens 
472fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
473fa9e4066Sahrens 
474fa9e4066Sahrens 	return (err);
475fa9e4066Sahrens }
476fa9e4066Sahrens 
47760030f27SMark Shellenbaum static int
zfsctl_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)47860030f27SMark Shellenbaum zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
47960030f27SMark Shellenbaum     caller_context_t *ct)
48060030f27SMark Shellenbaum {
48160030f27SMark Shellenbaum 	/*
48260030f27SMark Shellenbaum 	 * We only care about ACL_ENABLED so that libsec can
48360030f27SMark Shellenbaum 	 * display ACL correctly and not default to POSIX draft.
48460030f27SMark Shellenbaum 	 */
48560030f27SMark Shellenbaum 	if (cmd == _PC_ACL_ENABLED) {
48660030f27SMark Shellenbaum 		*valp = _ACL_ACE_ENABLED;
48760030f27SMark Shellenbaum 		return (0);
48860030f27SMark Shellenbaum 	}
48960030f27SMark Shellenbaum 
49060030f27SMark Shellenbaum 	return (fs_pathconf(vp, cmd, valp, cr, ct));
49160030f27SMark Shellenbaum }
49260030f27SMark Shellenbaum 
493fa9e4066Sahrens static const fs_operation_def_t zfsctl_tops_root[] = {
494aa59c4cbSrsb 	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
495aa59c4cbSrsb 	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
496aa59c4cbSrsb 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
497aa59c4cbSrsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_root_getattr }	},
498aa59c4cbSrsb 	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
499aa59c4cbSrsb 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } 	},
500aa59c4cbSrsb 	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_root_lookup }	},
501aa59c4cbSrsb 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
502aa59c4cbSrsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive }	},
50360030f27SMark Shellenbaum 	{ VOPNAME_PATHCONF,	{ .vop_pathconf = zfsctl_pathconf }	},
504aa59c4cbSrsb 	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid	}	},
505fa9e4066Sahrens 	{ NULL }
506fa9e4066Sahrens };
507fa9e4066Sahrens 
5083e30c24aSWill Andrews /*
5093e30c24aSWill Andrews  * Gets the full dataset name that corresponds to the given snapshot name
5103e30c24aSWill Andrews  * Example:
5113e30c24aSWill Andrews  * 	zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
5123e30c24aSWill Andrews  */
513fa9e4066Sahrens static int
zfsctl_snapshot_zname(vnode_t * vp,const char * name,int len,char * zname)514fa9e4066Sahrens zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
515fa9e4066Sahrens {
516fa9e4066Sahrens 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
517fa9e4066Sahrens 
51878f17100SMatthew Ahrens 	if (zfs_component_namecheck(name, NULL, NULL) != 0)
519be6fd75aSMatthew Ahrens 		return (SET_ERROR(EILSEQ));
520fa9e4066Sahrens 	dmu_objset_name(os, zname);
5215422900bSmaybee 	if (strlen(zname) + 1 + strlen(name) >= len)
522be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
5235422900bSmaybee 	(void) strcat(zname, "@");
524fa9e4066Sahrens 	(void) strcat(zname, name);
525fa9e4066Sahrens 	return (0);
526fa9e4066Sahrens }
527fa9e4066Sahrens 
5288bf40bf0Sck153898 static int
zfsctl_unmount_snap(zfs_snapentry_t * sep,int fflags,cred_t * cr)5298bf40bf0Sck153898 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
530fa9e4066Sahrens {
5318bf40bf0Sck153898 	vnode_t *svp = sep->se_root;
5328bf40bf0Sck153898 	int error;
533fa9e4066Sahrens 
5348bf40bf0Sck153898 	ASSERT(vn_ismntpt(svp));
535fa9e4066Sahrens 
536fa9e4066Sahrens 	/* this will be dropped by dounmount() */
5378bf40bf0Sck153898 	if ((error = vn_vfswlock(svp)) != 0)
5388bf40bf0Sck153898 		return (error);
539fa9e4066Sahrens 
5408bf40bf0Sck153898 	VN_HOLD(svp);
5418bf40bf0Sck153898 	error = dounmount(vn_mountedvfs(svp), fflags, cr);
5428bf40bf0Sck153898 	if (error) {
5438bf40bf0Sck153898 		VN_RELE(svp);
5448bf40bf0Sck153898 		return (error);
545bbf4a8dfSmaybee 	}
546142ae85dSChris Kirby 
5478bf40bf0Sck153898 	/*
5488bf40bf0Sck153898 	 * We can't use VN_RELE(), as that will try to invoke
5498bf40bf0Sck153898 	 * zfsctl_snapdir_inactive(), which would cause us to destroy
5508bf40bf0Sck153898 	 * the sd_lock mutex held by our caller.
5518bf40bf0Sck153898 	 */
5528bf40bf0Sck153898 	ASSERT(svp->v_count == 1);
5538bf40bf0Sck153898 	gfs_vop_inactive(svp, cr, NULL);
554fa9e4066Sahrens 
555fa9e4066Sahrens 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
556fa9e4066Sahrens 	kmem_free(sep, sizeof (zfs_snapentry_t));
557fa9e4066Sahrens 
558fa9e4066Sahrens 	return (0);
559fa9e4066Sahrens }
560fa9e4066Sahrens 
5615422900bSmaybee static void
zfsctl_rename_snap(zfsctl_snapdir_t * sdp,zfs_snapentry_t * sep,const char * nm)562fa9e4066Sahrens zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
563fa9e4066Sahrens {
564fa9e4066Sahrens 	avl_index_t where;
565fa9e4066Sahrens 	vfs_t *vfsp;
566fa9e4066Sahrens 	refstr_t *pathref;
567fa9e4066Sahrens 	char newpath[MAXNAMELEN];
568fa9e4066Sahrens 	char *tail;
569fa9e4066Sahrens 
570fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&sdp->sd_lock));
571fa9e4066Sahrens 	ASSERT(sep != NULL);
572fa9e4066Sahrens 
573fa9e4066Sahrens 	vfsp = vn_mountedvfs(sep->se_root);
574fa9e4066Sahrens 	ASSERT(vfsp != NULL);
575fa9e4066Sahrens 
5765422900bSmaybee 	vfs_lock_wait(vfsp);
577fa9e4066Sahrens 
578fa9e4066Sahrens 	/*
579fa9e4066Sahrens 	 * Change the name in the AVL tree.
580fa9e4066Sahrens 	 */
581fa9e4066Sahrens 	avl_remove(&sdp->sd_snaps, sep);
582fa9e4066Sahrens 	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
583fa9e4066Sahrens 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
584fa9e4066Sahrens 	(void) strcpy(sep->se_name, nm);
585fa9e4066Sahrens 	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
586fa9e4066Sahrens 	avl_insert(&sdp->sd_snaps, sep, where);
587fa9e4066Sahrens 
588fa9e4066Sahrens 	/*
589fa9e4066Sahrens 	 * Change the current mountpoint info:
590fa9e4066Sahrens 	 * 	- update the tail of the mntpoint path
591fa9e4066Sahrens 	 *	- update the tail of the resource path
592fa9e4066Sahrens 	 */
593fa9e4066Sahrens 	pathref = vfs_getmntpoint(vfsp);
5940b69c2f0Sahrens 	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
5950b69c2f0Sahrens 	VERIFY((tail = strrchr(newpath, '/')) != NULL);
5960b69c2f0Sahrens 	*(tail+1) = '\0';
5970b69c2f0Sahrens 	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
598fa9e4066Sahrens 	(void) strcat(newpath, nm);
599fa9e4066Sahrens 	refstr_rele(pathref);
600d7de0ceaSRobert Harris 	vfs_setmntpoint(vfsp, newpath, 0);
601fa9e4066Sahrens 
602fa9e4066Sahrens 	pathref = vfs_getresource(vfsp);
6030b69c2f0Sahrens 	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
6040b69c2f0Sahrens 	VERIFY((tail = strrchr(newpath, '@')) != NULL);
6050b69c2f0Sahrens 	*(tail+1) = '\0';
6060b69c2f0Sahrens 	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
607fa9e4066Sahrens 	(void) strcat(newpath, nm);
608fa9e4066Sahrens 	refstr_rele(pathref);
609d7de0ceaSRobert Harris 	vfs_setresource(vfsp, newpath, 0);
610fa9e4066Sahrens 
611fa9e4066Sahrens 	vfs_unlock(vfsp);
612fa9e4066Sahrens }
613fa9e4066Sahrens 
614da6c28aaSamw /*ARGSUSED*/
615fa9e4066Sahrens static int
zfsctl_snapdir_rename(vnode_t * sdvp,char * snm,vnode_t * tdvp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)616fa9e4066Sahrens zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
617da6c28aaSamw     cred_t *cr, caller_context_t *ct, int flags)
618fa9e4066Sahrens {
619fa9e4066Sahrens 	zfsctl_snapdir_t *sdp = sdvp->v_data;
620fa9e4066Sahrens 	zfs_snapentry_t search, *sep;
621ab04eb8eStimh 	zfsvfs_t *zfsvfs;
622fa9e4066Sahrens 	avl_index_t where;
623*a1988827SMatthew Ahrens 	char from[ZFS_MAX_DATASET_NAME_LEN], to[ZFS_MAX_DATASET_NAME_LEN];
624*a1988827SMatthew Ahrens 	char real[ZFS_MAX_DATASET_NAME_LEN], fsname[ZFS_MAX_DATASET_NAME_LEN];
625fa9e4066Sahrens 	int err;
626fa9e4066Sahrens 
627ab04eb8eStimh 	zfsvfs = sdvp->v_vfsp->vfs_data;
628ab04eb8eStimh 	ZFS_ENTER(zfsvfs);
629ab04eb8eStimh 
630ab04eb8eStimh 	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
631ab04eb8eStimh 		err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
632*a1988827SMatthew Ahrens 		    sizeof (real), NULL);
633ab04eb8eStimh 		if (err == 0) {
634ab04eb8eStimh 			snm = real;
635ab04eb8eStimh 		} else if (err != ENOTSUP) {
636ab04eb8eStimh 			ZFS_EXIT(zfsvfs);
637ab04eb8eStimh 			return (err);
638ab04eb8eStimh 		}
639ab04eb8eStimh 	}
640ab04eb8eStimh 
641ab04eb8eStimh 	ZFS_EXIT(zfsvfs);
642ab04eb8eStimh 
6433b2aab18SMatthew Ahrens 	dmu_objset_name(zfsvfs->z_os, fsname);
6443b2aab18SMatthew Ahrens 
645*a1988827SMatthew Ahrens 	err = zfsctl_snapshot_zname(sdvp, snm, sizeof (from), from);
6463b2aab18SMatthew Ahrens 	if (err == 0)
647*a1988827SMatthew Ahrens 		err = zfsctl_snapshot_zname(tdvp, tnm, sizeof (to), to);
6483b2aab18SMatthew Ahrens 	if (err == 0)
649ab04eb8eStimh 		err = zfs_secpolicy_rename_perms(from, to, cr);
6503b2aab18SMatthew Ahrens 	if (err != 0)
651fa9e4066Sahrens 		return (err);
652fa9e4066Sahrens 
653fa9e4066Sahrens 	/*
654fa9e4066Sahrens 	 * Cannot move snapshots out of the snapdir.
655fa9e4066Sahrens 	 */
656fa9e4066Sahrens 	if (sdvp != tdvp)
657be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
658fa9e4066Sahrens 
659fa9e4066Sahrens 	if (strcmp(snm, tnm) == 0)
660fa9e4066Sahrens 		return (0);
661fa9e4066Sahrens 
662fa9e4066Sahrens 	mutex_enter(&sdp->sd_lock);
663fa9e4066Sahrens 
664fa9e4066Sahrens 	search.se_name = (char *)snm;
6655422900bSmaybee 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
666fa9e4066Sahrens 		mutex_exit(&sdp->sd_lock);
667be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
668fa9e4066Sahrens 	}
669fa9e4066Sahrens 
6703b2aab18SMatthew Ahrens 	err = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
6715422900bSmaybee 	if (err == 0)
6725422900bSmaybee 		zfsctl_rename_snap(sdp, sep, tnm);
673fa9e4066Sahrens 
674fa9e4066Sahrens 	mutex_exit(&sdp->sd_lock);
675fa9e4066Sahrens 
676fa9e4066Sahrens 	return (err);
677fa9e4066Sahrens }
678fa9e4066Sahrens 
679fa9e4066Sahrens /* ARGSUSED */
680fa9e4066Sahrens static int
zfsctl_snapdir_remove(vnode_t * dvp,char * name,vnode_t * cwd,cred_t * cr,caller_context_t * ct,int flags)681da6c28aaSamw zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
682da6c28aaSamw     caller_context_t *ct, int flags)
683fa9e4066Sahrens {
684fa9e4066Sahrens 	zfsctl_snapdir_t *sdp = dvp->v_data;
6858bf40bf0Sck153898 	zfs_snapentry_t *sep;
6868bf40bf0Sck153898 	zfs_snapentry_t search;
687ab04eb8eStimh 	zfsvfs_t *zfsvfs;
688*a1988827SMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
689*a1988827SMatthew Ahrens 	char real[ZFS_MAX_DATASET_NAME_LEN];
690fa9e4066Sahrens 	int err;
691fa9e4066Sahrens 
692ab04eb8eStimh 	zfsvfs = dvp->v_vfsp->vfs_data;
693ab04eb8eStimh 	ZFS_ENTER(zfsvfs);
694ecd6cf80Smarks 
695ab04eb8eStimh 	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
696ab04eb8eStimh 
697ab04eb8eStimh 		err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
698*a1988827SMatthew Ahrens 		    sizeof (real), NULL);
699ab04eb8eStimh 		if (err == 0) {
700ab04eb8eStimh 			name = real;
701ab04eb8eStimh 		} else if (err != ENOTSUP) {
702ab04eb8eStimh 			ZFS_EXIT(zfsvfs);
703ab04eb8eStimh 			return (err);
704ab04eb8eStimh 		}
705ab04eb8eStimh 	}
706ab04eb8eStimh 
707ab04eb8eStimh 	ZFS_EXIT(zfsvfs);
708ab04eb8eStimh 
709*a1988827SMatthew Ahrens 	err = zfsctl_snapshot_zname(dvp, name, sizeof (snapname), snapname);
7103b2aab18SMatthew Ahrens 	if (err == 0)
711ab04eb8eStimh 		err = zfs_secpolicy_destroy_perms(snapname, cr);
7123b2aab18SMatthew Ahrens 	if (err != 0)
713fa9e4066Sahrens 		return (err);
714fa9e4066Sahrens 
715fa9e4066Sahrens 	mutex_enter(&sdp->sd_lock);
716fa9e4066Sahrens 
7178bf40bf0Sck153898 	search.se_name = name;
7188bf40bf0Sck153898 	sep = avl_find(&sdp->sd_snaps, &search, NULL);
7198bf40bf0Sck153898 	if (sep) {
7208bf40bf0Sck153898 		avl_remove(&sdp->sd_snaps, sep);
7218bf40bf0Sck153898 		err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
7223b2aab18SMatthew Ahrens 		if (err != 0)
7238bf40bf0Sck153898 			avl_add(&sdp->sd_snaps, sep);
7248bf40bf0Sck153898 		else
7253b2aab18SMatthew Ahrens 			err = dsl_destroy_snapshot(snapname, B_FALSE);
7268bf40bf0Sck153898 	} else {
727be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
7288bf40bf0Sck153898 	}
729fa9e4066Sahrens 
730fa9e4066Sahrens 	mutex_exit(&sdp->sd_lock);
731fa9e4066Sahrens 
732fa9e4066Sahrens 	return (err);
733fa9e4066Sahrens }
734fa9e4066Sahrens 
735f18faf3fSek110237 /*
736f18faf3fSek110237  * This creates a snapshot under '.zfs/snapshot'.
737f18faf3fSek110237  */
738ecd6cf80Smarks /* ARGSUSED */
739ecd6cf80Smarks static int
zfsctl_snapdir_mkdir(vnode_t * dvp,char * dirname,vattr_t * vap,vnode_t ** vpp,cred_t * cr,caller_context_t * cc,int flags,vsecattr_t * vsecp)740ecd6cf80Smarks zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
741da6c28aaSamw     cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
742ecd6cf80Smarks {
743ecd6cf80Smarks 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
744*a1988827SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
745ecd6cf80Smarks 	int err;
746ecd6cf80Smarks 	static enum symfollow follow = NO_FOLLOW;
747ecd6cf80Smarks 	static enum uio_seg seg = UIO_SYSSPACE;
748ecd6cf80Smarks 
74978f17100SMatthew Ahrens 	if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
750be6fd75aSMatthew Ahrens 		return (SET_ERROR(EILSEQ));
7516d1e0b89Smarks 
752ecd6cf80Smarks 	dmu_objset_name(zfsvfs->z_os, name);
753ecd6cf80Smarks 
754ecd6cf80Smarks 	*vpp = NULL;
755ecd6cf80Smarks 
756ecd6cf80Smarks 	err = zfs_secpolicy_snapshot_perms(name, cr);
7573b2aab18SMatthew Ahrens 	if (err != 0)
758ecd6cf80Smarks 		return (err);
759ecd6cf80Smarks 
760ecd6cf80Smarks 	if (err == 0) {
7614445fffbSMatthew Ahrens 		err = dmu_objset_snapshot_one(name, dirname);
7623b2aab18SMatthew Ahrens 		if (err != 0)
763ecd6cf80Smarks 			return (err);
764ecd6cf80Smarks 		err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
765ecd6cf80Smarks 	}
766ecd6cf80Smarks 
767ecd6cf80Smarks 	return (err);
768ecd6cf80Smarks }
769ecd6cf80Smarks 
770fa9e4066Sahrens /*
771fa9e4066Sahrens  * Lookup entry point for the 'snapshot' directory.  Try to open the
772fa9e4066Sahrens  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
773fa9e4066Sahrens  * Perform a mount of the associated dataset on top of the vnode.
774fa9e4066Sahrens  */
775fa9e4066Sahrens /* ARGSUSED */
776fa9e4066Sahrens static int
zfsctl_snapdir_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,pathname_t * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)777fa9e4066Sahrens zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
778da6c28aaSamw     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
779da6c28aaSamw     int *direntflags, pathname_t *realpnp)
780fa9e4066Sahrens {
781fa9e4066Sahrens 	zfsctl_snapdir_t *sdp = dvp->v_data;
782fa9e4066Sahrens 	objset_t *snap;
783*a1988827SMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
784*a1988827SMatthew Ahrens 	char real[ZFS_MAX_DATASET_NAME_LEN];
785fa9e4066Sahrens 	char *mountpoint;
786fa9e4066Sahrens 	zfs_snapentry_t *sep, search;
787fa9e4066Sahrens 	struct mounta margs;
788fa9e4066Sahrens 	vfs_t *vfsp;
789fa9e4066Sahrens 	size_t mountpoint_len;
790fa9e4066Sahrens 	avl_index_t where;
791fa9e4066Sahrens 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
792fa9e4066Sahrens 	int err;
793fa9e4066Sahrens 
794da6c28aaSamw 	/*
795da6c28aaSamw 	 * No extended attributes allowed under .zfs
796da6c28aaSamw 	 */
797da6c28aaSamw 	if (flags & LOOKUP_XATTR)
798be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
799da6c28aaSamw 
800fa9e4066Sahrens 	ASSERT(dvp->v_type == VDIR);
801fa9e4066Sahrens 
802fa9e4066Sahrens 	/*
803fa9e4066Sahrens 	 * If we get a recursive call, that means we got called
804fa9e4066Sahrens 	 * from the domount() code while it was trying to look up the
805fa9e4066Sahrens 	 * spec (which looks like a local path for zfs).  We need to
806fa9e4066Sahrens 	 * add some flag to domount() to tell it not to do this lookup.
807fa9e4066Sahrens 	 */
808fa9e4066Sahrens 	if (MUTEX_HELD(&sdp->sd_lock))
809be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
810fa9e4066Sahrens 
811fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
812fa9e4066Sahrens 
813142ae85dSChris Kirby 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
814142ae85dSChris Kirby 		ZFS_EXIT(zfsvfs);
815142ae85dSChris Kirby 		return (0);
816142ae85dSChris Kirby 	}
817142ae85dSChris Kirby 
818ab04eb8eStimh 	if (flags & FIGNORECASE) {
819ab04eb8eStimh 		boolean_t conflict = B_FALSE;
820ab04eb8eStimh 
821ab04eb8eStimh 		err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
822*a1988827SMatthew Ahrens 		    sizeof (real), &conflict);
823ab04eb8eStimh 		if (err == 0) {
824ab04eb8eStimh 			nm = real;
825ab04eb8eStimh 		} else if (err != ENOTSUP) {
826ab04eb8eStimh 			ZFS_EXIT(zfsvfs);
827ab04eb8eStimh 			return (err);
828ab04eb8eStimh 		}
829ab04eb8eStimh 		if (realpnp)
830ab04eb8eStimh 			(void) strlcpy(realpnp->pn_buf, nm,
831ab04eb8eStimh 			    realpnp->pn_bufsize);
832ab04eb8eStimh 		if (conflict && direntflags)
833ab04eb8eStimh 			*direntflags = ED_CASE_CONFLICT;
834ab04eb8eStimh 	}
835ab04eb8eStimh 
836fa9e4066Sahrens 	mutex_enter(&sdp->sd_lock);
837fa9e4066Sahrens 	search.se_name = (char *)nm;
838fa9e4066Sahrens 	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
839fa9e4066Sahrens 		*vpp = sep->se_root;
840fa9e4066Sahrens 		VN_HOLD(*vpp);
841bbf4a8dfSmaybee 		err = traverse(vpp);
8423b2aab18SMatthew Ahrens 		if (err != 0) {
843bbf4a8dfSmaybee 			VN_RELE(*vpp);
844bbf4a8dfSmaybee 			*vpp = NULL;
845bbf4a8dfSmaybee 		} else if (*vpp == sep->se_root) {
846fa9e4066Sahrens 			/*
847bbf4a8dfSmaybee 			 * The snapshot was unmounted behind our backs,
848bb963f1cSmaybee 			 * try to remount it.
849fa9e4066Sahrens 			 */
850fa9e4066Sahrens 			goto domount;
8518bf40bf0Sck153898 		} else {
8528bf40bf0Sck153898 			/*
8538bf40bf0Sck153898 			 * VROOT was set during the traverse call.  We need
8548bf40bf0Sck153898 			 * to clear it since we're pretending to be part
8558bf40bf0Sck153898 			 * of our parent's vfs.
8568bf40bf0Sck153898 			 */
8578bf40bf0Sck153898 			(*vpp)->v_flag &= ~VROOT;
858bb963f1cSmaybee 		}
859fa9e4066Sahrens 		mutex_exit(&sdp->sd_lock);
860fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
861bbf4a8dfSmaybee 		return (err);
862fa9e4066Sahrens 	}
863fa9e4066Sahrens 
864fa9e4066Sahrens 	/*
865fa9e4066Sahrens 	 * The requested snapshot is not currently mounted, look it up.
866fa9e4066Sahrens 	 */
867*a1988827SMatthew Ahrens 	err = zfsctl_snapshot_zname(dvp, nm, sizeof (snapname), snapname);
8683b2aab18SMatthew Ahrens 	if (err != 0) {
8695422900bSmaybee 		mutex_exit(&sdp->sd_lock);
8705422900bSmaybee 		ZFS_EXIT(zfsvfs);
8712283022cSmarks 		/*
8722283022cSmarks 		 * handle "ls *" or "?" in a graceful manner,
8732283022cSmarks 		 * forcing EILSEQ to ENOENT.
8742283022cSmarks 		 * Since shell ultimately passes "*" or "?" as name to lookup
8752283022cSmarks 		 */
8762283022cSmarks 		return (err == EILSEQ ? ENOENT : err);
8775422900bSmaybee 	}
878503ad85cSMatthew Ahrens 	if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
879fa9e4066Sahrens 		mutex_exit(&sdp->sd_lock);
880fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
881be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
882fa9e4066Sahrens 	}
883fa9e4066Sahrens 
884fa9e4066Sahrens 	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
885fa9e4066Sahrens 	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
886fa9e4066Sahrens 	(void) strcpy(sep->se_name, nm);
887fa9e4066Sahrens 	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
888fa9e4066Sahrens 	avl_insert(&sdp->sd_snaps, sep, where);
889fa9e4066Sahrens 
890503ad85cSMatthew Ahrens 	dmu_objset_rele(snap, FTAG);
891fa9e4066Sahrens domount:
892fa9e4066Sahrens 	mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
893fa9e4066Sahrens 	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
894fa9e4066Sahrens 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
895fa9e4066Sahrens 	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
896fa9e4066Sahrens 	    refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
897fa9e4066Sahrens 
898fa9e4066Sahrens 	margs.spec = snapname;
899fa9e4066Sahrens 	margs.dir = mountpoint;
900fa9e4066Sahrens 	margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
901fa9e4066Sahrens 	margs.fstype = "zfs";
902fa9e4066Sahrens 	margs.dataptr = NULL;
903fa9e4066Sahrens 	margs.datalen = 0;
904fa9e4066Sahrens 	margs.optptr = NULL;
905fa9e4066Sahrens 	margs.optlen = 0;
906fa9e4066Sahrens 
907fa9e4066Sahrens 	err = domount("zfs", &margs, *vpp, kcred, &vfsp);
908fa9e4066Sahrens 	kmem_free(mountpoint, mountpoint_len);
909fa9e4066Sahrens 
91095c9592aSmaybee 	if (err == 0) {
91195c9592aSmaybee 		/*
91295c9592aSmaybee 		 * Return the mounted root rather than the covered mount point.
913f18faf3fSek110237 		 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
914f18faf3fSek110237 		 * the ZFS vnode mounted on top of the GFS node.  This ZFS
915142ae85dSChris Kirby 		 * vnode is the root of the newly created vfsp.
91695c9592aSmaybee 		 */
917fa9e4066Sahrens 		VFS_RELE(vfsp);
91895c9592aSmaybee 		err = traverse(vpp);
91995c9592aSmaybee 	}
920fa9e4066Sahrens 
92195c9592aSmaybee 	if (err == 0) {
922fa9e4066Sahrens 		/*
923f18faf3fSek110237 		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
924ed097989Sek110237 		 *
925ed097989Sek110237 		 * This is where we lie about our v_vfsp in order to
926f18faf3fSek110237 		 * make .zfs/snapshot/<snapname> accessible over NFS
927f18faf3fSek110237 		 * without requiring manual mounts of <snapname>.
928fa9e4066Sahrens 		 */
929fa9e4066Sahrens 		ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
930fa9e4066Sahrens 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
931fa9e4066Sahrens 		(*vpp)->v_vfsp = zfsvfs->z_vfs;
932fa9e4066Sahrens 		(*vpp)->v_flag &= ~VROOT;
93395c9592aSmaybee 	}
934fa9e4066Sahrens 	mutex_exit(&sdp->sd_lock);
935fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
936fa9e4066Sahrens 
937bb963f1cSmaybee 	/*
938bb963f1cSmaybee 	 * If we had an error, drop our hold on the vnode and
939bb963f1cSmaybee 	 * zfsctl_snapshot_inactive() will clean up.
940bb963f1cSmaybee 	 */
9413b2aab18SMatthew Ahrens 	if (err != 0) {
94295c9592aSmaybee 		VN_RELE(*vpp);
943bb963f1cSmaybee 		*vpp = NULL;
944bb963f1cSmaybee 	}
94595c9592aSmaybee 	return (err);
946fa9e4066Sahrens }
947fa9e4066Sahrens 
948fa9e4066Sahrens /* ARGSUSED */
949fa9e4066Sahrens static int
zfsctl_shares_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,pathname_t * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)950743a77edSAlan Wright zfsctl_shares_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
951743a77edSAlan Wright     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
952743a77edSAlan Wright     int *direntflags, pathname_t *realpnp)
953743a77edSAlan Wright {
954743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
955743a77edSAlan Wright 	znode_t *dzp;
956743a77edSAlan Wright 	int error;
957743a77edSAlan Wright 
958743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
959743a77edSAlan Wright 
960743a77edSAlan Wright 	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
961743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
962743a77edSAlan Wright 		return (0);
963743a77edSAlan Wright 	}
964743a77edSAlan Wright 
9659e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
9669e1320c0SMark Shellenbaum 		ZFS_EXIT(zfsvfs);
967be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
9689e1320c0SMark Shellenbaum 	}
969743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
970743a77edSAlan Wright 		error = VOP_LOOKUP(ZTOV(dzp), nm, vpp, pnp,
971743a77edSAlan Wright 		    flags, rdir, cr, ct, direntflags, realpnp);
972743a77edSAlan Wright 
973743a77edSAlan Wright 	VN_RELE(ZTOV(dzp));
974743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
975743a77edSAlan Wright 
976743a77edSAlan Wright 	return (error);
977743a77edSAlan Wright }
978743a77edSAlan Wright 
979743a77edSAlan Wright /* ARGSUSED */
980743a77edSAlan Wright static int
zfsctl_snapdir_readdir_cb(vnode_t * vp,void * dp,int * eofp,offset_t * offp,offset_t * nextp,void * data,int flags)981b38f0970Sck153898 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
982b38f0970Sck153898     offset_t *offp, offset_t *nextp, void *data, int flags)
983fa9e4066Sahrens {
984fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
985*a1988827SMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
986fa9e4066Sahrens 	uint64_t id, cookie;
987b38f0970Sck153898 	boolean_t case_conflict;
988b38f0970Sck153898 	int error;
989fa9e4066Sahrens 
990fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
991fa9e4066Sahrens 
992fa9e4066Sahrens 	cookie = *offp;
9933b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
994*a1988827SMatthew Ahrens 	error = dmu_snapshot_list_next(zfsvfs->z_os,
995*a1988827SMatthew Ahrens 	    sizeof (snapname), snapname, &id, &cookie, &case_conflict);
9963b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
997b38f0970Sck153898 	if (error) {
998fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
999b38f0970Sck153898 		if (error == ENOENT) {
1000b38f0970Sck153898 			*eofp = 1;
1001fa9e4066Sahrens 			return (0);
1002fa9e4066Sahrens 		}
1003b38f0970Sck153898 		return (error);
1004b38f0970Sck153898 	}
1005fa9e4066Sahrens 
1006b38f0970Sck153898 	if (flags & V_RDDIR_ENTFLAGS) {
1007b38f0970Sck153898 		edirent_t *eodp = dp;
1008b38f0970Sck153898 
1009b38f0970Sck153898 		(void) strcpy(eodp->ed_name, snapname);
1010b38f0970Sck153898 		eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1011b38f0970Sck153898 		eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1012b38f0970Sck153898 	} else {
1013b38f0970Sck153898 		struct dirent64 *odp = dp;
1014b38f0970Sck153898 
1015b38f0970Sck153898 		(void) strcpy(odp->d_name, snapname);
1016b38f0970Sck153898 		odp->d_ino = ZFSCTL_INO_SNAP(id);
1017b38f0970Sck153898 	}
1018fa9e4066Sahrens 	*nextp = cookie;
1019fa9e4066Sahrens 
1020fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
1021fa9e4066Sahrens 
1022fa9e4066Sahrens 	return (0);
1023fa9e4066Sahrens }
1024fa9e4066Sahrens 
1025743a77edSAlan Wright /* ARGSUSED */
1026743a77edSAlan Wright static int
zfsctl_shares_readdir(vnode_t * vp,uio_t * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)1027743a77edSAlan Wright zfsctl_shares_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
1028743a77edSAlan Wright     caller_context_t *ct, int flags)
1029743a77edSAlan Wright {
1030743a77edSAlan Wright 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1031743a77edSAlan Wright 	znode_t *dzp;
1032743a77edSAlan Wright 	int error;
1033743a77edSAlan Wright 
1034743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
1035743a77edSAlan Wright 
10369e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
10379e1320c0SMark Shellenbaum 		ZFS_EXIT(zfsvfs);
1038be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
10399e1320c0SMark Shellenbaum 	}
1040743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1041743a77edSAlan Wright 		error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ct, flags);
1042743a77edSAlan Wright 		VN_RELE(ZTOV(dzp));
1043743a77edSAlan Wright 	} else {
1044743a77edSAlan Wright 		*eofp = 1;
1045be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
1046743a77edSAlan Wright 	}
1047743a77edSAlan Wright 
1048743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
1049743a77edSAlan Wright 	return (error);
1050743a77edSAlan Wright }
1051743a77edSAlan Wright 
1052f18faf3fSek110237 /*
1053f18faf3fSek110237  * pvp is the '.zfs' directory (zfsctl_node_t).
1054f7170741SWill Andrews  *
1055f18faf3fSek110237  * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1056f18faf3fSek110237  *
1057f18faf3fSek110237  * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1058f18faf3fSek110237  * when a lookup is performed on .zfs for "snapshot".
1059f18faf3fSek110237  */
1060fa9e4066Sahrens vnode_t *
zfsctl_mknode_snapdir(vnode_t * pvp)1061fa9e4066Sahrens zfsctl_mknode_snapdir(vnode_t *pvp)
1062fa9e4066Sahrens {
1063fa9e4066Sahrens 	vnode_t *vp;
1064fa9e4066Sahrens 	zfsctl_snapdir_t *sdp;
1065fa9e4066Sahrens 
1066fa9e4066Sahrens 	vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
1067fa9e4066Sahrens 	    zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1068fa9e4066Sahrens 	    zfsctl_snapdir_readdir_cb, NULL);
1069fa9e4066Sahrens 	sdp = vp->v_data;
1070fa9e4066Sahrens 	sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1071a32fded1Sek110237 	sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1072fa9e4066Sahrens 	mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1073fa9e4066Sahrens 	avl_create(&sdp->sd_snaps, snapentry_compare,
1074fa9e4066Sahrens 	    sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1075fa9e4066Sahrens 	return (vp);
1076fa9e4066Sahrens }
1077fa9e4066Sahrens 
1078743a77edSAlan Wright vnode_t *
zfsctl_mknode_shares(vnode_t * pvp)1079743a77edSAlan Wright zfsctl_mknode_shares(vnode_t *pvp)
1080743a77edSAlan Wright {
1081743a77edSAlan Wright 	vnode_t *vp;
1082743a77edSAlan Wright 	zfsctl_node_t *sdp;
1083743a77edSAlan Wright 
1084743a77edSAlan Wright 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1085743a77edSAlan Wright 	    zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1086743a77edSAlan Wright 	    NULL, NULL);
1087743a77edSAlan Wright 	sdp = vp->v_data;
1088743a77edSAlan Wright 	sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1089743a77edSAlan Wright 	return (vp);
1090743a77edSAlan Wright 
1091743a77edSAlan Wright }
1092743a77edSAlan Wright 
1093743a77edSAlan Wright /* ARGSUSED */
1094743a77edSAlan Wright static int
zfsctl_shares_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)1095743a77edSAlan Wright zfsctl_shares_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1096743a77edSAlan Wright     caller_context_t *ct)
1097743a77edSAlan Wright {
1098743a77edSAlan Wright 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1099743a77edSAlan Wright 	znode_t *dzp;
1100743a77edSAlan Wright 	int error;
1101743a77edSAlan Wright 
1102743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
11039e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
11049e1320c0SMark Shellenbaum 		ZFS_EXIT(zfsvfs);
1105be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
11069e1320c0SMark Shellenbaum 	}
1107743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1108743a77edSAlan Wright 		error = VOP_GETATTR(ZTOV(dzp), vap, flags, cr, ct);
1109743a77edSAlan Wright 		VN_RELE(ZTOV(dzp));
1110743a77edSAlan Wright 	}
1111743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
1112743a77edSAlan Wright 	return (error);
1113743a77edSAlan Wright 
1114743a77edSAlan Wright 
1115743a77edSAlan Wright }
1116743a77edSAlan Wright 
1117fa9e4066Sahrens /* ARGSUSED */
1118fa9e4066Sahrens static int
zfsctl_snapdir_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)1119da6c28aaSamw zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1120da6c28aaSamw     caller_context_t *ct)
1121fa9e4066Sahrens {
1122fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1123fa9e4066Sahrens 	zfsctl_snapdir_t *sdp = vp->v_data;
1124fa9e4066Sahrens 
1125fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
1126fa9e4066Sahrens 	zfsctl_common_getattr(vp, vap);
1127fa9e4066Sahrens 	vap->va_nodeid = gfs_file_inode(vp);
1128fa9e4066Sahrens 	vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
112971eb0538SChris Kirby 	vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1130fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	return (0);
1133fa9e4066Sahrens }
1134fa9e4066Sahrens 
1135bb963f1cSmaybee /* ARGSUSED */
1136fa9e4066Sahrens static void
zfsctl_snapdir_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)1137da6c28aaSamw zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1138fa9e4066Sahrens {
1139fa9e4066Sahrens 	zfsctl_snapdir_t *sdp = vp->v_data;
1140bb963f1cSmaybee 	void *private;
1141fa9e4066Sahrens 
1142bb963f1cSmaybee 	private = gfs_dir_inactive(vp);
1143bb963f1cSmaybee 	if (private != NULL) {
1144fa9e4066Sahrens 		ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1145fa9e4066Sahrens 		mutex_destroy(&sdp->sd_lock);
1146fa9e4066Sahrens 		avl_destroy(&sdp->sd_snaps);
1147bb963f1cSmaybee 		kmem_free(private, sizeof (zfsctl_snapdir_t));
1148bb963f1cSmaybee 	}
1149fa9e4066Sahrens }
1150fa9e4066Sahrens 
1151fa9e4066Sahrens static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1152aa59c4cbSrsb 	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1153aa59c4cbSrsb 	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1154aa59c4cbSrsb 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1155aa59c4cbSrsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_snapdir_getattr } },
1156aa59c4cbSrsb 	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1157aa59c4cbSrsb 	{ VOPNAME_RENAME,	{ .vop_rename = zfsctl_snapdir_rename }	},
1158aa59c4cbSrsb 	{ VOPNAME_RMDIR,	{ .vop_rmdir = zfsctl_snapdir_remove }	},
1159ecd6cf80Smarks 	{ VOPNAME_MKDIR,	{ .vop_mkdir = zfsctl_snapdir_mkdir }	},
1160aa59c4cbSrsb 	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir }	},
1161aa59c4cbSrsb 	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_snapdir_lookup }	},
1162aa59c4cbSrsb 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1163aa59c4cbSrsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = zfsctl_snapdir_inactive } },
1164aa59c4cbSrsb 	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid }	},
1165fa9e4066Sahrens 	{ NULL }
1166fa9e4066Sahrens };
1167fa9e4066Sahrens 
1168743a77edSAlan Wright static const fs_operation_def_t zfsctl_tops_shares[] = {
1169743a77edSAlan Wright 	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
1170743a77edSAlan Wright 	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
1171743a77edSAlan Wright 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1172743a77edSAlan Wright 	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_shares_getattr } },
1173743a77edSAlan Wright 	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
1174743a77edSAlan Wright 	{ VOPNAME_READDIR,	{ .vop_readdir = zfsctl_shares_readdir } },
1175743a77edSAlan Wright 	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_shares_lookup }	},
1176743a77edSAlan Wright 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1177743a77edSAlan Wright 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1178743a77edSAlan Wright 	{ VOPNAME_FID,		{ .vop_fid = zfsctl_shares_fid } },
1179743a77edSAlan Wright 	{ NULL }
1180743a77edSAlan Wright };
1181743a77edSAlan Wright 
1182f18faf3fSek110237 /*
1183f18faf3fSek110237  * pvp is the GFS vnode '.zfs/snapshot'.
1184f18faf3fSek110237  *
1185f18faf3fSek110237  * This creates a GFS node under '.zfs/snapshot' representing each
1186f18faf3fSek110237  * snapshot.  This newly created GFS node is what we mount snapshot
1187f18faf3fSek110237  * vfs_t's ontop of.
1188f18faf3fSek110237  */
1189fa9e4066Sahrens static vnode_t *
zfsctl_snapshot_mknode(vnode_t * pvp,uint64_t objset)1190fa9e4066Sahrens zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1191fa9e4066Sahrens {
1192fa9e4066Sahrens 	vnode_t *vp;
1193fa9e4066Sahrens 	zfsctl_node_t *zcp;
1194fa9e4066Sahrens 
1195fa9e4066Sahrens 	vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1196fa9e4066Sahrens 	    zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1197fa9e4066Sahrens 	zcp = vp->v_data;
1198fa9e4066Sahrens 	zcp->zc_id = objset;
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 	return (vp);
1201fa9e4066Sahrens }
1202fa9e4066Sahrens 
1203fa9e4066Sahrens static void
zfsctl_snapshot_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)1204da6c28aaSamw zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1205fa9e4066Sahrens {
1206fa9e4066Sahrens 	zfsctl_snapdir_t *sdp;
1207fa9e4066Sahrens 	zfs_snapentry_t *sep, *next;
1208fa9e4066Sahrens 	vnode_t *dvp;
1209fa9e4066Sahrens 
1210ab04eb8eStimh 	VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1211fa9e4066Sahrens 	sdp = dvp->v_data;
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	mutex_enter(&sdp->sd_lock);
1214fa9e4066Sahrens 
121520a95fb2SMatthew Ahrens 	mutex_enter(&vp->v_lock);
1216fa9e4066Sahrens 	if (vp->v_count > 1) {
121720a95fb2SMatthew Ahrens 		vp->v_count--;
121820a95fb2SMatthew Ahrens 		mutex_exit(&vp->v_lock);
1219fa9e4066Sahrens 		mutex_exit(&sdp->sd_lock);
122020a95fb2SMatthew Ahrens 		VN_RELE(dvp);
1221fa9e4066Sahrens 		return;
1222fa9e4066Sahrens 	}
122320a95fb2SMatthew Ahrens 	mutex_exit(&vp->v_lock);
1224fa9e4066Sahrens 	ASSERT(!vn_ismntpt(vp));
1225fa9e4066Sahrens 
1226fa9e4066Sahrens 	sep = avl_first(&sdp->sd_snaps);
1227fa9e4066Sahrens 	while (sep != NULL) {
1228fa9e4066Sahrens 		next = AVL_NEXT(&sdp->sd_snaps, sep);
1229fa9e4066Sahrens 
1230fa9e4066Sahrens 		if (sep->se_root == vp) {
1231fa9e4066Sahrens 			avl_remove(&sdp->sd_snaps, sep);
1232fa9e4066Sahrens 			kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1233fa9e4066Sahrens 			kmem_free(sep, sizeof (zfs_snapentry_t));
1234fa9e4066Sahrens 			break;
1235fa9e4066Sahrens 		}
1236fa9e4066Sahrens 		sep = next;
1237fa9e4066Sahrens 	}
1238fa9e4066Sahrens 	ASSERT(sep != NULL);
1239fa9e4066Sahrens 
1240fa9e4066Sahrens 	mutex_exit(&sdp->sd_lock);
1241fa9e4066Sahrens 	VN_RELE(dvp);
1242fa9e4066Sahrens 
1243bb963f1cSmaybee 	/*
1244bb963f1cSmaybee 	 * Dispose of the vnode for the snapshot mount point.
1245bb963f1cSmaybee 	 * This is safe to do because once this entry has been removed
1246bb963f1cSmaybee 	 * from the AVL tree, it can't be found again, so cannot become
1247bb963f1cSmaybee 	 * "active".  If we lookup the same name again we will end up
1248bb963f1cSmaybee 	 * creating a new vnode.
1249bb963f1cSmaybee 	 */
1250da6c28aaSamw 	gfs_vop_inactive(vp, cr, ct);
1251fa9e4066Sahrens }
1252fa9e4066Sahrens 
1253fa9e4066Sahrens 
1254fa9e4066Sahrens /*
1255fa9e4066Sahrens  * These VP's should never see the light of day.  They should always
1256fa9e4066Sahrens  * be covered.
1257fa9e4066Sahrens  */
1258fa9e4066Sahrens static const fs_operation_def_t zfsctl_tops_snapshot[] = {
1259aa59c4cbSrsb 	VOPNAME_INACTIVE, { .vop_inactive =  zfsctl_snapshot_inactive },
1260fa9e4066Sahrens 	NULL, NULL
1261fa9e4066Sahrens };
1262fa9e4066Sahrens 
1263fa9e4066Sahrens int
zfsctl_lookup_objset(vfs_t * vfsp,uint64_t objsetid,zfsvfs_t ** zfsvfsp)1264fa9e4066Sahrens zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1265fa9e4066Sahrens {
1266fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1267fa9e4066Sahrens 	vnode_t *dvp, *vp;
1268fa9e4066Sahrens 	zfsctl_snapdir_t *sdp;
1269fa9e4066Sahrens 	zfsctl_node_t *zcp;
1270fa9e4066Sahrens 	zfs_snapentry_t *sep;
1271fa9e4066Sahrens 	int error;
1272fa9e4066Sahrens 
1273fa9e4066Sahrens 	ASSERT(zfsvfs->z_ctldir != NULL);
1274fa9e4066Sahrens 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1275da6c28aaSamw 	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1276fa9e4066Sahrens 	if (error != 0)
1277fa9e4066Sahrens 		return (error);
1278fa9e4066Sahrens 	sdp = dvp->v_data;
1279fa9e4066Sahrens 
1280fa9e4066Sahrens 	mutex_enter(&sdp->sd_lock);
1281fa9e4066Sahrens 	sep = avl_first(&sdp->sd_snaps);
1282fa9e4066Sahrens 	while (sep != NULL) {
1283fa9e4066Sahrens 		vp = sep->se_root;
1284fa9e4066Sahrens 		zcp = vp->v_data;
1285fa9e4066Sahrens 		if (zcp->zc_id == objsetid)
1286fa9e4066Sahrens 			break;
1287fa9e4066Sahrens 
1288fa9e4066Sahrens 		sep = AVL_NEXT(&sdp->sd_snaps, sep);
1289fa9e4066Sahrens 	}
1290fa9e4066Sahrens 
1291fa9e4066Sahrens 	if (sep != NULL) {
1292fa9e4066Sahrens 		VN_HOLD(vp);
1293f18faf3fSek110237 		/*
1294f18faf3fSek110237 		 * Return the mounted root rather than the covered mount point.
1295f18faf3fSek110237 		 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1296f18faf3fSek110237 		 * and returns the ZFS vnode mounted on top of the GFS node.
1297f18faf3fSek110237 		 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1298f18faf3fSek110237 		 */
1299fa9e4066Sahrens 		error = traverse(&vp);
1300bbf4a8dfSmaybee 		if (error == 0) {
1301bbf4a8dfSmaybee 			if (vp == sep->se_root)
1302be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
1303bbf4a8dfSmaybee 			else
1304fa9e4066Sahrens 				*zfsvfsp = VTOZ(vp)->z_zfsvfs;
1305bbf4a8dfSmaybee 		}
1306fea70b54Snd150628 		mutex_exit(&sdp->sd_lock);
1307fa9e4066Sahrens 		VN_RELE(vp);
1308fa9e4066Sahrens 	} else {
1309be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1310fea70b54Snd150628 		mutex_exit(&sdp->sd_lock);
1311fa9e4066Sahrens 	}
1312fa9e4066Sahrens 
1313fa9e4066Sahrens 	VN_RELE(dvp);
1314fa9e4066Sahrens 
1315fa9e4066Sahrens 	return (error);
1316fa9e4066Sahrens }
1317fa9e4066Sahrens 
1318fa9e4066Sahrens /*
1319fa9e4066Sahrens  * Unmount any snapshots for the given filesystem.  This is called from
1320fa9e4066Sahrens  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1321fa9e4066Sahrens  * snapshots.
1322fa9e4066Sahrens  */
1323fa9e4066Sahrens int
zfsctl_umount_snapshots(vfs_t * vfsp,int fflags,cred_t * cr)1324fa9e4066Sahrens zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1325fa9e4066Sahrens {
1326fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
13278bf40bf0Sck153898 	vnode_t *dvp;
1328fa9e4066Sahrens 	zfsctl_snapdir_t *sdp;
1329fa9e4066Sahrens 	zfs_snapentry_t *sep, *next;
1330fa9e4066Sahrens 	int error;
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	ASSERT(zfsvfs->z_ctldir != NULL);
1333fa9e4066Sahrens 	error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1334da6c28aaSamw 	    NULL, 0, NULL, cr, NULL, NULL, NULL);
1335fa9e4066Sahrens 	if (error != 0)
1336fa9e4066Sahrens 		return (error);
1337fa9e4066Sahrens 	sdp = dvp->v_data;
1338fa9e4066Sahrens 
1339fa9e4066Sahrens 	mutex_enter(&sdp->sd_lock);
1340fa9e4066Sahrens 
1341fa9e4066Sahrens 	sep = avl_first(&sdp->sd_snaps);
1342fa9e4066Sahrens 	while (sep != NULL) {
1343fa9e4066Sahrens 		next = AVL_NEXT(&sdp->sd_snaps, sep);
1344fa9e4066Sahrens 
1345fa9e4066Sahrens 		/*
1346fa9e4066Sahrens 		 * If this snapshot is not mounted, then it must
1347fa9e4066Sahrens 		 * have just been unmounted by somebody else, and
1348fa9e4066Sahrens 		 * will be cleaned up by zfsctl_snapdir_inactive().
1349fa9e4066Sahrens 		 */
13508bf40bf0Sck153898 		if (vn_ismntpt(sep->se_root)) {
1351fa9e4066Sahrens 			avl_remove(&sdp->sd_snaps, sep);
13528bf40bf0Sck153898 			error = zfsctl_unmount_snap(sep, fflags, cr);
13538bf40bf0Sck153898 			if (error) {
13548bf40bf0Sck153898 				avl_add(&sdp->sd_snaps, sep);
13558bf40bf0Sck153898 				break;
13568bf40bf0Sck153898 			}
1357fa9e4066Sahrens 		}
1358fa9e4066Sahrens 		sep = next;
1359fa9e4066Sahrens 	}
13608bf40bf0Sck153898 
1361fa9e4066Sahrens 	mutex_exit(&sdp->sd_lock);
1362fa9e4066Sahrens 	VN_RELE(dvp);
1363fa9e4066Sahrens 
1364fa9e4066Sahrens 	return (error);
1365fa9e4066Sahrens }
1366