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