1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
50f2dc02ek * Common Development and Distribution License (the "License").
60f2dc02ek * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
22d39ee14Mark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ade42b5Sebastien Roy * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
240f92170Dan McDonald * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens/*
28fa9e406ahrens * ZFS control directory (a.k.a. ".zfs")
29fa9e406ahrens *
30fa9e406ahrens * This directory provides a common location for all ZFS meta-objects.
31fa9e406ahrens * Currently, this is only the 'snapshot' directory, but this may expand in the
32fa9e406ahrens * future.  The elements are built using the GFS primitives, as the hierarchy
33fa9e406ahrens * does not actually exist on disk.
34fa9e406ahrens *
35fa9e406ahrens * For 'snapshot', we don't want to have all snapshots always mounted, because
36fa9e406ahrens * this would take up a huge amount of space in /etc/mnttab.  We have three
37fa9e406ahrens * types of objects:
38fa9e406ahrens *
39fa9e406ahrens * 	ctldir ------> snapshotdir -------> snapshot
40fa9e406ahrens *                                             |
41fa9e406ahrens *                                             |
42fa9e406ahrens *                                             V
43fa9e406ahrens *                                         mounted fs
44fa9e406ahrens *
45fa9e406ahrens * The 'snapshot' node contains just enough information to lookup '..' and act
46fa9e406ahrens * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
47fa9e406ahrens * perform an automount of the underlying filesystem and return the
48fa9e406ahrens * corresponding vnode.
49fa9e406ahrens *
50fa9e406ahrens * All mounts are handled automatically by the kernel, but unmounts are
51fa9e406ahrens * (currently) handled from user land.  The main reason is that there is no
52fa9e406ahrens * reliable way to auto-unmount the filesystem when it's "no longer in use".
53fa9e406ahrens * When the user unmounts a filesystem, we call zfsctl_unmount(), which
54fa9e406ahrens * unmounts any snapshots within the snapshot directory.
55f18faf3ek *
56f18faf3ek * The '.zfs', '.zfs/snapshot', and all directories created under
57f18faf3ek * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
58f18faf3ek * share the same vfs_t as the head filesystem (what '.zfs' lives under).
59f18faf3ek *
60f18faf3ek * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
61f18faf3ek * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
62f18faf3ek * However, vnodes within these mounted on file systems have their v_vfsp
63f18faf3ek * fields set to the head filesystem to make NFS happy (see
648bf40bfck * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
658bf40bfck * so that it cannot be freed until all snapshots have been unmounted.
66fa9e406ahrens */
67fa9e406ahrens
68fa9e406ahrens#include <fs/fs_subr.h>
69fa9e406ahrens#include <sys/zfs_ctldir.h>
70fa9e406ahrens#include <sys/zfs_ioctl.h>
71fa9e406ahrens#include <sys/zfs_vfsops.h>
72aa59c4crsb#include <sys/vfs_opreg.h>
73fa9e406ahrens#include <sys/gfs.h>
74fa9e406ahrens#include <sys/stat.h>
75fa9e406ahrens#include <sys/dmu.h>
763b2aab1Matthew Ahrens#include <sys/dsl_destroy.h>
77ecd6cf8marks#include <sys/dsl_deleg.h>
78fa9e406ahrens#include <sys/mount.h>
79ab04eb8timh#include <sys/sunddi.h>
80fa9e406ahrens
816d1e0b8marks#include "zfs_namecheck.h"
826d1e0b8marks
838bf40bfcktypedef struct zfsctl_node {
848bf40bfck	gfs_dir_t	zc_gfs_private;
858bf40bfck	uint64_t	zc_id;
868bf40bfck	timestruc_t	zc_cmtime;	/* ctime and mtime, always the same */
878bf40bfck} zfsctl_node_t;
888bf40bfck
898bf40bfcktypedef struct zfsctl_snapdir {
908bf40bfck	zfsctl_node_t	sd_node;
918bf40bfck	kmutex_t	sd_lock;
928bf40bfck	avl_tree_t	sd_snaps;
938bf40bfck} zfsctl_snapdir_t;
948bf40bfck
95fa9e406ahrenstypedef struct {
96fa9e406ahrens	char		*se_name;
97fa9e406ahrens	vnode_t		*se_root;
98fa9e406ahrens	avl_node_t	se_node;
99fa9e406ahrens} zfs_snapentry_t;
100fa9e406ahrens
101fa9e406ahrensstatic int
102fa9e406ahrenssnapentry_compare(const void *a, const void *b)
103fa9e406ahrens{
104fa9e406ahrens	const zfs_snapentry_t *sa = a;
105fa9e406ahrens	const zfs_snapentry_t *sb = b;
106fa9e406ahrens	int ret = strcmp(sa->se_name, sb->se_name);
107fa9e406ahrens
108fa9e406ahrens	if (ret < 0)
109fa9e406ahrens		return (-1);
110fa9e406ahrens	else if (ret > 0)
111fa9e406ahrens		return (1);
112fa9e406ahrens	else
113fa9e406ahrens		return (0);
114fa9e406ahrens}
115fa9e406ahrens
116fa9e406ahrensvnodeops_t *zfsctl_ops_root;
117fa9e406ahrensvnodeops_t *zfsctl_ops_snapdir;
118fa9e406ahrensvnodeops_t *zfsctl_ops_snapshot;
119743a77eAlan Wrightvnodeops_t *zfsctl_ops_shares;
120fa9e406ahrens
121fa9e406ahrensstatic const fs_operation_def_t zfsctl_tops_root[];
122fa9e406ahrensstatic const fs_operation_def_t zfsctl_tops_snapdir[];
123fa9e406ahrensstatic const fs_operation_def_t zfsctl_tops_snapshot[];
124743a77eAlan Wrightstatic const fs_operation_def_t zfsctl_tops_shares[];
125fa9e406ahrens
126fa9e406ahrensstatic vnode_t *zfsctl_mknode_snapdir(vnode_t *);
127743a77eAlan Wrightstatic vnode_t *zfsctl_mknode_shares(vnode_t *);
128fa9e406ahrensstatic vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
1298bf40bfckstatic int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
130fa9e406ahrens
131fa9e406ahrensstatic gfs_opsvec_t zfsctl_opsvec[] = {
132fa9e406ahrens	{ ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
133fa9e406ahrens	{ ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
134fa9e406ahrens	{ ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
135b7f9f60Andriy Gapon	{ ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares },
136fa9e406ahrens	{ NULL }
137fa9e406ahrens};
138fa9e406ahrens
139fa9e406ahrens/*
140743a77eAlan Wright * Root directory elements.  We only have two entries
141743a77eAlan Wright * snapshot and shares.
142fa9e406ahrens */
143fa9e406ahrensstatic gfs_dirent_t zfsctl_root_entries[] = {
144fa9e406ahrens	{ "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
145743a77eAlan Wright	{ "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
146fa9e406ahrens	{ NULL }
147fa9e406ahrens};
148fa9e406ahrens
149fa9e406ahrens/* include . and .. in the calculation */
150fa9e406ahrens#define	NROOT_ENTRIES	((sizeof (zfsctl_root_entries) / \
151fa9e406ahrens    sizeof (gfs_dirent_t)) + 1)
152fa9e406ahrens
153fa9e406ahrens
154fa9e406ahrens/*
155fa9e406ahrens * Initialize the various GFS pieces we'll need to create and manipulate .zfs
156fa9e406ahrens * directories.  This is called from the ZFS init routine, and initializes the
157fa9e406ahrens * vnode ops vectors that we'll be using.
158fa9e406ahrens */
159fa9e406ahrensvoid
160fa9e406ahrenszfsctl_init(void)
161fa9e406ahrens{
162fa9e406ahrens	VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
163fa9e406ahrens}
164fa9e406ahrens
165fa9e406ahrensvoid
166fa9e406ahrenszfsctl_fini(void)
167fa9e406ahrens{
168fa9e406ahrens	/*
169fa9e406ahrens	 * Remove vfsctl vnode ops
170fa9e406ahrens	 */
171fa9e406ahrens	if (zfsctl_ops_root)
172fa9e406ahrens		vn_freevnodeops(zfsctl_ops_root);
173fa9e406ahrens	if (zfsctl_ops_snapdir)
174fa9e406ahrens		vn_freevnodeops(zfsctl_ops_snapdir);
175fa9e406ahrens	if (zfsctl_ops_snapshot)
176fa9e406ahrens		vn_freevnodeops(zfsctl_ops_snapshot);
177743a77eAlan Wright	if (zfsctl_ops_shares)
178743a77eAlan Wright		vn_freevnodeops(zfsctl_ops_shares);
179fa9e406ahrens
180fa9e406ahrens	zfsctl_ops_root = NULL;
181fa9e406ahrens	zfsctl_ops_snapdir = NULL;
182fa9e406ahrens	zfsctl_ops_snapshot = NULL;
183743a77eAlan Wright	zfsctl_ops_shares = NULL;
184fa9e406ahrens}
185fa9e406ahrens
186d39ee14Mark Shellenbaumboolean_t
187d39ee14Mark Shellenbaumzfsctl_is_node(vnode_t *vp)
188d39ee14Mark Shellenbaum{
189d39ee14Mark Shellenbaum	return (vn_matchops(vp, zfsctl_ops_root) ||
190d39ee14Mark Shellenbaum	    vn_matchops(vp, zfsctl_ops_snapdir) ||
191d39ee14Mark Shellenbaum	    vn_matchops(vp, zfsctl_ops_snapshot) ||
192b7f9f60Andriy Gapon	    vn_matchops(vp, zfsctl_ops_shares));
193d39ee14Mark Shellenbaum
194d39ee14Mark Shellenbaum}
195d39ee14Mark Shellenbaum
196fa9e406ahrens/*
197743a77eAlan Wright * Return the inode number associated with the 'snapshot' or
198743a77eAlan Wright * 'shares' directory.
199fa9e406ahrens */
200fa9e406ahrens/* ARGSUSED */
201fa9e406ahrensstatic ino64_t
202fa9e406ahrenszfsctl_root_inode_cb(vnode_t *vp, int index)
203fa9e406ahrens{
204743a77eAlan Wright	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
205743a77eAlan Wright
206b7f9f60Andriy Gapon	ASSERT(index < 2);
207743a77eAlan Wright
208743a77eAlan Wright	if (index == 0)
209743a77eAlan Wright		return (ZFSCTL_INO_SNAPDIR);
210743a77eAlan Wright
211743a77eAlan Wright	return (zfsvfs->z_shares_dir);
212fa9e406ahrens}
213fa9e406ahrens
214fa9e406ahrens/*
215fa9e406ahrens * Create the '.zfs' directory.  This directory is cached as part of the VFS
216fa9e406ahrens * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
217fa9e406ahrens * therefore checks against a vfs_count of 2 instead of 1.  This reference
218fa9e406ahrens * is removed when the ctldir is destroyed in the unmount.
219fa9e406ahrens */
220fa9e406ahrensvoid
221fa9e406ahrenszfsctl_create(zfsvfs_t *zfsvfs)
222fa9e406ahrens{
223a32fdedek	vnode_t *vp, *rvp;
224fa9e406ahrens	zfsctl_node_t *zcp;
2250a586ceMark Shellenbaum	uint64_t crtime[2];
226fa9e406ahrens
227fa9e406ahrens	ASSERT(zfsvfs->z_ctldir == NULL);
228fa9e406ahrens
229fa9e406ahrens	vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
230fa9e406ahrens	    zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
231fa9e406ahrens	    zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
232fa9e406ahrens	zcp = vp->v_data;
233fa9e406ahrens	zcp->zc_id = ZFSCTL_INO_ROOT;
234fa9e406ahrens
235a32fdedek	VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0);
2360a586ceMark Shellenbaum	VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2370a586ceMark Shellenbaum	    &crtime, sizeof (crtime)));
2380a586ceMark Shellenbaum	ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
239a32fdedek	VN_RELE(rvp);
240a32fdedek
241fa9e406ahrens	/*
242fa9e406ahrens	 * We're only faking the fact that we have a root of a filesystem for
243fa9e406ahrens	 * the sake of the GFS interfaces.  Undo the flag manipulation it did
244fa9e406ahrens	 * for us.
245fa9e406ahrens	 */
246fa9e406ahrens	vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
247fa9e406ahrens
248fa9e406ahrens	zfsvfs->z_ctldir = vp;
249fa9e406ahrens}
250fa9e406ahrens
251fa9e406ahrens/*
2528afd4ddperrin * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
2538afd4ddperrin * There might still be more references if we were force unmounted, but only
2548afd4ddperrin * new zfs_inactive() calls can occur and they don't reference .zfs
255fa9e406ahrens */
256fa9e406ahrensvoid
257fa9e406ahrenszfsctl_destroy(zfsvfs_t *zfsvfs)
258fa9e406ahrens{
259fa9e406ahrens	VN_RELE(zfsvfs->z_ctldir);
260fa9e406ahrens	zfsvfs->z_ctldir = NULL;
261fa9e406ahrens}
262fa9e406ahrens
263fa9e406ahrens/*
264fa9e406ahrens * Given a root znode, retrieve the associated .zfs directory.
265fa9e406ahrens * Add a hold to the vnode and return it.
266fa9e406ahrens */
267fa9e406ahrensvnode_t *
268fa9e406ahrenszfsctl_root(znode_t *zp)
269fa9e406ahrens{
270fa9e406ahrens	ASSERT(zfs_has_ctldir(zp));
271fa9e406ahrens	VN_HOLD(zp->z_zfsvfs->z_ctldir);
272fa9e406ahrens	return (zp->z_zfsvfs->z_ctldir);
273fa9e406ahrens}
274fa9e406ahrens
275fa9e406ahrens/*
276fa9e406ahrens * Common open routine.  Disallow any write access.
277fa9e406ahrens */
278fa9e406ahrens/* ARGSUSED */
279fa9e406ahrensstatic int
280da6c28aamwzfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
281fa9e406ahrens{
282fa9e406ahrens	if (flags & FWRITE)
283be6fd75Matthew Ahrens		return (SET_ERROR(EACCES));
284fa9e406ahrens
285fa9e406ahrens	return (0);
286fa9e406ahrens}
287fa9e406ahrens
288fa9e406ahrens/*
289fa9e406ahrens * Common close routine.  Nothing to do here.
290fa9e406ahrens */
291fa9e406ahrens/* ARGSUSED */
292fa9e406ahrensstatic int
293fa9e406ahrenszfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
294da6c28aamw    cred_t *cr, caller_context_t *ct)
295fa9e406ahrens{
296fa9e406ahrens	return (0);
297fa9e406ahrens}
298fa9e406ahrens
299fa9e406ahrens/*
300fa9e406ahrens * Common access routine.  Disallow writes.
301fa9e406ahrens */
302fa9e406ahrens/* ARGSUSED */
303fa9e406ahrensstatic int
304da6c28aamwzfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr,
305da6c28aamw    caller_context_t *ct)
306fa9e406ahrens{
30760030f2Mark Shellenbaum	if (flags & V_ACE_MASK) {
30860030f2Mark Shellenbaum		if (mode & ACE_ALL_WRITE_PERMS)
309be6fd75Matthew Ahrens			return (SET_ERROR(EACCES));
31060030f2Mark Shellenbaum	} else {
31160030f2Mark Shellenbaum		if (mode & VWRITE)
312be6fd75Matthew Ahrens			return (SET_ERROR(EACCES));
31360030f2Mark Shellenbaum	}
314fa9e406ahrens
315fa9e406ahrens	return (0);
316fa9e406ahrens}
317fa9e406ahrens
318fa9e406ahrens/*
319fa9e406ahrens * Common getattr function.  Fill in basic information.
320fa9e406ahrens */
321fa9e406ahrensstatic void
322fa9e406ahrenszfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
323fa9e406ahrens{
324a32fdedek	timestruc_t	now;
325fa9e406ahrens
326fa9e406ahrens	vap->va_uid = 0;
327fa9e406ahrens	vap->va_gid = 0;
328fa9e406ahrens	vap->va_rdev = 0;
329fa9e406ahrens	/*
33071eb053Chris Kirby	 * We are a purely virtual object, so we have no
331fa9e406ahrens	 * blocksize or allocated blocks.
332fa9e406ahrens	 */
333fa9e406ahrens	vap->va_blksize = 0;
334fa9e406ahrens	vap->va_nblocks = 0;
335fa9e406ahrens	vap->va_seq = 0;
336fa9e406ahrens	vap->va_fsid = vp->v_vfsp->vfs_dev;
337fa9e406ahrens	vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
338fa9e406ahrens	    S_IROTH | S_IXOTH;
339fa9e406ahrens	vap->va_type = VDIR;
340fa9e406ahrens	/*
341a32fdedek	 * We live in the now (for atime).
342fa9e406ahrens	 */
343fa9e406ahrens	gethrestime(&now);
344a32fdedek	vap->va_atime = now;
345fa9e406ahrens}
346fa9e406ahrens
347da6c28aamw/*ARGSUSED*/
348fa9e406ahrensstatic int
349da6c28aamwzfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
350fa9e406ahrens{
351fa9e406ahrens	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
352fa9e406ahrens	zfsctl_node_t	*zcp = vp->v_data;
353fa9e406ahrens	uint64_t	object = zcp->zc_id;
354fa9e406ahrens	zfid_short_t	*zfid;
355fa9e406ahrens	int		i;
356fa9e406ahrens
357fa9e406ahrens	ZFS_ENTER(zfsvfs);
358fa9e406ahrens
359fa9e406ahrens	if (fidp->fid_len < SHORT_FID_LEN) {
360fa9e406ahrens		fidp->fid_len = SHORT_FID_LEN;
3610f2dc02ek		ZFS_EXIT(zfsvfs);
362be6fd75Matthew Ahrens		return (SET_ERROR(ENOSPC));
363fa9e406ahrens	}
364fa9e406ahrens
365fa9e406ahrens	zfid = (zfid_short_t *)fidp;
366fa9e406ahrens
367fa9e406ahrens	zfid->zf_len = SHORT_FID_LEN;
368fa9e406ahrens
369fa9e406ahrens	for (i = 0; i < sizeof (zfid->zf_object); i++)
370fa9e406ahrens		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
371fa9e406ahrens
372fa9e406ahrens	/* .zfs znodes always have a generation number of 0 */
373fa9e406ahrens	for (i = 0; i < sizeof (zfid->zf_gen); i++)
374fa9e406ahrens		zfid->zf_gen[i] = 0;
375fa9e406ahrens
376fa9e406ahrens	ZFS_EXIT(zfsvfs);
377fa9e406ahrens	return (0);
378fa9e406ahrens}
379fa9e406ahrens
380743a77eAlan Wright
381743a77eAlan Wright/*ARGSUSED*/
382743a77eAlan Wrightstatic int
383743a77eAlan Wrightzfsctl_shares_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
384743a77eAlan Wright{
385743a77eAlan Wright	zfsvfs_t	*zfsvfs = vp->v_vfsp->vfs_data;
386743a77eAlan Wright	znode_t		*dzp;
387743a77eAlan Wright	int		error;
388743a77eAlan Wright
389743a77eAlan Wright	ZFS_ENTER(zfsvfs);
3909e1320cMark Shellenbaum
3919e1320cMark Shellenbaum	if (zfsvfs->z_shares_dir == 0) {
3929e1320cMark Shellenbaum		ZFS_EXIT(zfsvfs);
393be6fd75Matthew Ahrens		return (SET_ERROR(ENOTSUP));
3949e1320cMark Shellenbaum	}
3959e1320cMark Shellenbaum
396743a77eAlan Wright	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
397743a77eAlan Wright		error = VOP_FID(ZTOV(dzp), fidp, ct);
398743a77eAlan Wright		VN_RELE(ZTOV(dzp));
399743a77eAlan Wright	}
400743a77eAlan Wright
401743a77eAlan Wright	ZFS_EXIT(zfsvfs);
402743a77eAlan Wright	return (error);
403743a77eAlan Wright}
404fa9e406ahrens/*
405fa9e406ahrens * .zfs inode namespace
406fa9e406ahrens *
407fa9e406ahrens * We need to generate unique inode numbers for all files and directories
408fa9e406ahrens * within the .zfs pseudo-filesystem.  We use the following scheme:
409fa9e406ahrens *
410fa9e406ahrens * 	ENTRY			ZFSCTL_INODE
411fa9e406ahrens * 	.zfs			1
412fa9e406ahrens * 	.zfs/snapshot		2
413fa9e406ahrens * 	.zfs/snapshot/<snap>	objectid(snap)
414fa9e406ahrens */
415fa9e406ahrens
416fa9e406ahrens#define	ZFSCTL_INO_SNAP(id)	(id)
417fa9e406ahrens
418fa9e406ahrens/*
419fa9e406ahrens * Get root directory attributes.
420fa9e406ahrens */
421fa9e406ahrens/* ARGSUSED */
422fa9e406ahrensstatic int
423da6c28aamwzfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
424da6c28aamw    caller_context_t *ct)
425fa9e406ahrens{
426fa9e406ahrens	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
42771eb053Chris Kirby	zfsctl_node_t *zcp = vp->v_data;
428fa9e406ahrens
429fa9e406ahrens	ZFS_ENTER(zfsvfs);
430fa9e406ahrens	vap->va_nodeid = ZFSCTL_INO_ROOT;
431fa9e406ahrens	vap->va_nlink = vap->va_size = NROOT_ENTRIES;
43271eb053Chris Kirby	vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
433fa9e406ahrens
434fa9e406ahrens	zfsctl_common_getattr(vp, vap);
435fa9e406ahrens	ZFS_EXIT(zfsvfs);
436fa9e406ahrens
437fa9e406ahrens	return (0);
438fa9e406ahrens}
439fa9e406ahrens
440fa9e406ahrens/*
441fa9e406ahrens * Special case the handling of "..".
442fa9e406ahrens */
443fa9e406ahrens/* ARGSUSED */
444fa9e406ahrensint
445fa9e406ahrenszfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
446da6c28aamw    int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
447da6c28aamw    int *direntflags, pathname_t *realpnp)
448fa9e406ahrens{
449fa9e406ahrens	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
450fa9e406ahrens	int err;
451fa9e406ahrens
452da6c28aamw	/*
453da6c28aamw	 * No extended attributes allowed under .zfs
454da6c28aamw	 */
455da6c28aamw	if (flags & LOOKUP_XATTR)
456be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
457da6c28aamw
458fa9e406ahrens	ZFS_ENTER(zfsvfs);
459fa9e406ahrens
460fa9e406ahrens	if (strcmp(nm, "..") == 0) {
461fa9e406ahrens		err = VFS_ROOT(dvp->v_vfsp, vpp);
462fa9e406ahrens	} else {
463ab04eb8timh		err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
464ab04eb8timh		    cr, ct, direntflags, realpnp);
465fa9e406ahrens	}
466fa9e406ahrens
467fa9e406ahrens	ZFS_EXIT(zfsvfs);
468fa9e406ahrens
469fa9e406ahrens	return (err);
470fa9e406ahrens}
471fa9e406ahrens
47260030f2Mark Shellenbaumstatic int
47360030f2Mark Shellenbaumzfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
47460030f2Mark Shellenbaum    caller_context_t *ct)
47560030f2Mark Shellenbaum{
47660030f2Mark Shellenbaum	/*
47760030f2Mark Shellenbaum	 * We only care about ACL_ENABLED so that libsec can
47860030f2Mark Shellenbaum	 * display ACL correctly and not default to POSIX draft.
47960030f2Mark Shellenbaum	 */
48060030f2Mark Shellenbaum	if (cmd == _PC_ACL_ENABLED) {
48160030f2Mark Shellenbaum		*valp = _ACL_ACE_ENABLED;
48260030f2Mark Shellenbaum		return (0);
48360030f2Mark Shellenbaum	}
48460030f2Mark Shellenbaum
48560030f2Mark Shellenbaum	return (fs_pathconf(vp, cmd, valp, cr, ct));
48660030f2Mark Shellenbaum}
48760030f2Mark Shellenbaum
488fa9e406ahrensstatic const fs_operation_def_t zfsctl_tops_root[] = {
489aa59c4crsb	{ VOPNAME_OPEN,		{ .vop_open = zfsctl_common_open }	},
490aa59c4crsb	{ VOPNAME_CLOSE,	{ .vop_close = zfsctl_common_close }	},
491aa59c4crsb	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
492aa59c4crsb	{ VOPNAME_GETATTR,	{ .vop_getattr = zfsctl_root_getattr }	},
493aa59c4crsb	{ VOPNAME_ACCESS,	{ .vop_access = zfsctl_common_access }	},
494aa59c4crsb	{ VOPNAME_READDIR,	{ .vop_readdir = gfs_vop_readdir } 	},
495aa59c4crsb	{ VOPNAME_LOOKUP,	{ .vop_lookup = zfsctl_root_lookup }	},
496aa59c4crsb	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
497aa59c4crsb	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive }	},
49860030f2Mark Shellenbaum	{ VOPNAME_PATHCONF,	{ .vop_pathconf = zfsctl_pathconf }	},
499aa59c4crsb	{ VOPNAME_FID,		{ .vop_fid = zfsctl_common_fid	}	},
500fa9e406ahrens	{ NULL }
501fa9e406ahrens};
502fa9e406ahrens
5033e30c24Will Andrews/*
5043e30c24Will Andrews * Gets the full dataset name that corresponds to the given snapshot name
5053e30c24Will Andrews * Example:
5063e30c24Will Andrews * 	zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
5073e30c24Will Andrews */
508fa9e406ahrensstatic int
509fa9e406ahrenszfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
510fa9e406ahrens{
511fa9e406ahrens	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
512fa9e406ahrens
51378f1710Matthew Ahrens	if (zfs_component_namecheck(name, NULL, NULL) != 0)
514be6fd75Matthew Ahrens		return (SET_ERROR(EILSEQ));
515fa9e406ahrens	dmu_objset_name(os, zname);
5165422900maybee	if (strlen(zname) + 1 + strlen(name) >= len)
517be6fd75Matthew Ahrens		return (SET_ERROR(ENAMETOOLONG));
5185422900maybee	(void) strcat(zname, "@");
519fa9e406ahrens	(void) strcat(zname, name);
520fa9e406ahrens	return (0);
521fa9e406ahrens}
522fa9e406ahrens
5238bf40bfckstatic int
5248bf40bfckzfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
525fa9e406ahrens{
5268bf40bfck	vnode_t *svp = sep->se_root;
5278bf40bfck	int error;
528fa9e406ahrens
5298bf40bfck	ASSERT(vn_ismntpt(svp));
530fa9e406ahrens
531fa9e406ahrens	/* this will be dropped by dounmount() */
5328bf40bfck	if ((error = vn_vfswlock(svp)) != 0)
5338bf40bfck		return (error);
534fa9e406ahrens
5358bf40bfck	VN_HOLD(svp);
5368bf40bfck	error = dounmount(vn_mountedvfs(svp), fflags, cr);
5378bf40bfck	if (error) {
5388bf40bfck		VN_RELE(svp);
5398bf40bfck		return (error);
540bbf4a8dmaybee	}
541142ae85Chris Kirby
5428bf40bfck	/*
5438bf40bfck	 * We can't use VN_RELE(), as that will try to invoke
5448bf40bfck	 * zfsctl_snapdir_inactive(), which would cause us to destroy
5458bf40bfck	 * the sd_lock mutex held by our caller.
5468bf40bfck	 */
5478bf40bfck	ASSERT(svp->v_count == 1);
5488bf40bfck	gfs_vop_inactive(svp, cr, NULL);
549fa9e406ahrens
550fa9e406ahrens	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
551fa9e406ahrens	kmem_free(sep, sizeof (zfs_snapentry_t));
552fa9e406ahrens
553fa9e406ahrens	return (0);
554fa9e406ahrens}
555fa9e406ahrens
5565422900maybeestatic void
557fa9e406ahrenszfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
558fa9e406ahrens{
559fa9e406ahrens	avl_index_t where;
560fa9e406ahrens	vfs_t *vfsp;
561fa9e406ahrens	refstr_t *pathref;
562fa9e406ahrens	char newpath[MAXNAMELEN];
563fa9e406ahrens	char *tail;
564fa9e406ahrens
565fa9e406ahrens	ASSERT(MUTEX_HELD(&sdp->sd_lock));
566fa9e406ahrens	ASSERT(sep != NULL);
567fa9e406ahrens
568fa9e406ahrens	vfsp = vn_mountedvfs(sep->se_root);
569fa9e406ahrens	ASSERT(vfsp != NULL);
570fa9e406ahrens
5715422900maybee	vfs_lock_wait(vfsp);
572fa9e406ahrens
573fa9e406ahrens	/*
574fa9e406ahrens	 * Change the name in the AVL tree.
575fa9e406ahrens	 */
576fa9e406ahrens	avl_remove(&sdp->sd_snaps, sep);
577fa9e406ahrens	kmem_free(sep->se_name, strlen(sep->se_name) + 1);
578fa9e406ahrens	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
579fa9e406ahrens	(void) strcpy(sep->se_name, nm);
580fa9e406ahrens	VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
581fa9e406ahrens	avl_insert(&sdp->sd_snaps, sep, where);
582fa9e406ahrens
583fa9e406ahrens	/*
584fa9e406ahrens	 * Change the current mountpoint info:
585fa9e406ahrens	 * 	- update the tail of the mntpoint path
586fa9e406ahrens	 *	- update the tail of the resource path
587fa9e406ahrens	 */
588fa9e406ahrens	pathref = vfs_getmntpoint(vfsp);
5890b69c2fahrens	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
5900b69c2fahrens	VERIFY((tail = strrchr(newpath, '/')) != NULL);
5910b69c2fahrens	*(tail+1) = '\0';
5920b69c2fahrens	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
593fa9e406ahrens	(void) strcat(newpath, nm);
594fa9e406ahrens	refstr_rele(pathref);
595d7de0ceRobert Harris	vfs_setmntpoint(vfsp, newpath, 0);
596fa9e406ahrens
597fa9e406ahrens	pathref = vfs_getresource(vfsp);
5980b69c2fahrens	(void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
5990b69c2fahrens	VERIFY((tail = strrchr(newpath, '@')) != NULL);
6000b69c2fahrens	*(tail+1) = '\0';
6010b69c2fahrens	ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
602fa9e406ahrens	(void) strcat(newpath, nm);
603fa9e406ahrens	refstr_rele(pathref);
604d7de0ceRobert Harris	vfs_setresource(vfsp, newpath, 0);
605fa9e406ahrens
606fa9e406ahrens	vfs_unlock(vfsp);
607fa9e406ahrens}
608fa9e406ahrens
609da6c28aamw/*ARGSUSED*/
610fa9e406ahrensstatic int
611fa9e406ahrenszfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
612da6c28aamw    cred_t *cr, caller_context_t *ct, int flags)
613fa9e406ahrens{
614fa9e406ahrens	zfsctl_snapdir_t *sdp = sdvp->v_data;
615fa9e406ahrens	zfs_snapentry_t search, *sep;
616ab04eb8timh	zfsvfs_t *zfsvfs;
617fa9e406ahrens	avl_index_t where;
6189adfa60Matthew Ahrens	char from[ZFS_MAX_DATASET_NAME_LEN], to[ZFS_MAX_DATASET_NAME_LEN];
6199adfa60Matthew Ahrens	char real[ZFS_MAX_DATASET_NAME_LEN], fsname[ZFS_MAX_DATASET_NAME_LEN];
620fa9e406ahrens	int err;
621fa9e406ahrens
622ab04eb8timh	zfsvfs = sdvp->v_vfsp->vfs_data;
623ab04eb8timh	ZFS_ENTER(zfsvfs);
624ab04eb8timh
625ab04eb8timh	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
626ab04eb8timh		err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
6279adfa60Matthew Ahrens		    sizeof (real), NULL);
628ab04eb8timh		if (err == 0) {
629ab04eb8timh			snm = real;
630ab04eb8timh		} else if (err != ENOTSUP) {
631ab04eb8timh			ZFS_EXIT(zfsvfs);
632ab04eb8timh			return (err);
633ab04eb8timh		}
634ab04eb8timh	}
635ab04eb8timh
636ab04eb8timh	ZFS_EXIT(zfsvfs);
637ecd6cf8marks
6383b2aab1Matthew Ahrens	dmu_objset_name(zfsvfs->z_os, fsname);
6393b2aab1Matthew Ahrens
6409adfa60Matthew Ahrens	err = zfsctl_snapshot_zname(sdvp, snm, sizeof (from), from);
6413b2aab1Matthew Ahrens	if (err == 0)
6429adfa60Matthew Ahrens		err = zfsctl_snapshot_zname(tdvp, tnm, sizeof (to), to);
6433b2aab1Matthew Ahrens	if (err == 0)
644ab04eb8timh		err = zfs_secpolicy_rename_perms(from, to, cr);
6453b2aab1Matthew Ahrens	if (err != 0)
646fa9e406ahrens		return (err);
647fa9e406ahrens
648fa9e406ahrens	/*
649fa9e406ahrens	 * Cannot move snapshots out of the snapdir.
650fa9e406ahrens	 */
651fa9e406ahrens	if (sdvp != tdvp)
652be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
653fa9e406ahrens
654fa9e406ahrens	if (strcmp(snm, tnm) == 0)
655fa9e406ahrens		return (0);
656fa9e406ahrens
657fa9e406ahrens	mutex_enter(&sdp->sd_lock);
658fa9e406ahrens
659fa9e406ahrens	search.se_name = (char *)snm;
6605422900maybee	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
6615422900maybee		mutex_exit(&sdp->sd_lock);
662be6fd75Matthew Ahrens		return (SET_ERROR(ENOENT));
663fa9e406ahrens	}
664fa9e406ahrens
6653b2aab1Matthew Ahrens	err = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
6665422900maybee	if (err == 0)
6675422900maybee		zfsctl_rename_snap(sdp, sep, tnm);
668fa9e406ahrens
669fa9e406ahrens	mutex_exit(&sdp->sd_lock);
670fa9e406ahrens
671fa9e406ahrens	return (err);
672fa9e406ahrens}
673fa9e406ahrens
674fa9e406ahrens/* ARGSUSED */
675fa9e406ahrensstatic int
676da6c28aamwzfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
677da6c28aamw    caller_context_t *ct, int flags)
678fa9e406ahrens{
679fa9e406ahrens	zfsctl_snapdir_t *sdp = dvp->v_data;
6808bf40bfck	zfs_snapentry_t *sep;
6818bf40bfck	zfs_snapentry_t search;
682ab04eb8timh	zfsvfs_t *zfsvfs;
6839adfa60Matthew Ahrens	char snapname[ZFS_MAX_DATASET_NAME_LEN];
6849adfa60Matthew Ahrens	char real[ZFS_MAX_DATASET_NAME_LEN];
685fa9e406ahrens	int err;
686fa9e406ahrens
687ab04eb8timh	zfsvfs = dvp->v_vfsp->vfs_data;
688ab04eb8timh	ZFS_ENTER(zfsvfs);
689ab04eb8timh
690ab04eb8timh	if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
691ab04eb8timh
692ab04eb8timh		err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
6939adfa60Matthew Ahrens		    sizeof (real), NULL);
694ab04eb8timh		if (err == 0) {
695ab04eb8timh			name = real;
696ab04eb8timh		} else if (err != ENOTSUP) {
697ab04eb8timh			ZFS_EXIT(zfsvfs);
698ab04eb8timh			return (err);
699ab04eb8timh		}
700ab04eb8timh	}
701ab04eb8timh
702ab04eb8timh	ZFS_EXIT(zfsvfs);
703ab04eb8timh
7049adfa60Matthew Ahrens	err = zfsctl_snapshot_zname(dvp, name, sizeof (snapname), snapname);
7053b2aab1Matthew Ahrens	if (err == 0)
706ab04eb8timh		err = zfs_secpolicy_destroy_perms(snapname, cr);
7073b2aab1Matthew Ahrens	if (err != 0)
7085422900maybee		return (err);
709ecd6cf8marks
710fa9e406ahrens	mutex_enter(&sdp->sd_lock);
711fa9e406ahrens
7128bf40bfck	search.se_name = name;
7138bf40bfck	sep = avl_find(&sdp->sd_snaps, &search, NULL);
7148bf40bfck	if (sep) {
7158bf40bfck		avl_remove(&sdp->sd_snaps, sep);
7168bf40bfck		err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
7173b2aab1Matthew Ahrens		if (err != 0)
7188bf40bfck			avl_add(&sdp->sd_snaps, sep);
7198bf40bfck		else
7203b2aab1Matthew Ahrens			err = dsl_destroy_snapshot(snapname, B_FALSE);
7218bf40bfck	} else {
722be6fd75Matthew Ahrens		err = SET_ERROR(ENOENT);
723fa9e406ahrens	}
724fa9e406ahrens
725fa9e406ahrens	mutex_exit(&sdp->sd_lock);
726fa9e406ahrens
727fa9e406ahrens	return (err);
728fa9e406ahrens}
729fa9e406ahrens
730f18faf3ek/*
731f18faf3ek * This creates a snapshot under '.zfs/snapshot'.
732f18faf3ek */
733ecd6cf8marks/* ARGSUSED */
734ecd6cf8marksstatic int
735ecd6cf8markszfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
736da6c28aamw    cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
737ecd6cf8marks{
738ecd6cf8marks	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
7399adfa60Matthew Ahrens	char name[ZFS_MAX_DATASET_NAME_LEN];
740ecd6cf8marks	int err;
741ecd6cf8marks	static enum symfollow follow = NO_FOLLOW;
742ecd6cf8marks	static enum uio_seg seg = UIO_SYSSPACE;
743ecd6cf8marks
74478f1710Matthew Ahrens	if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
745be6fd75Matthew Ahrens		return (SET_ERROR(EILSEQ));
7466d1e0b8marks
747ecd6cf8marks	dmu_objset_name(zfsvfs->z_os, name);
748ecd6cf8marks
749ecd6cf8marks	*vpp = NULL;
750ecd6cf8marks
751ecd6cf8marks	err = zfs_secpolicy_snapshot_perms(name, cr);
7523b2aab1Matthew Ahrens	if (err != 0)
753ecd6cf8marks		return (err);
754ecd6cf8marks
755ecd6cf8marks	if (err == 0) {
7564445fffMatthew Ahrens		err = dmu_objset_snapshot_one(name, dirname);
7573b2aab1Matthew Ahrens		if (err != 0)
758ecd6cf8marks			return (err);
759ecd6cf8marks		err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
760ecd6cf8marks	}
761ecd6cf8marks
762ecd6cf8marks	return (err);
763ecd6cf8marks}
764ecd6cf8marks
765fa9e406ahrens/*
766fa9e406ahrens * Lookup entry point for the 'snapshot' directory.  Try to open the
767fa9e406ahrens * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
768fa9e406ahrens * Perform a mount of the associated dataset on top of the vnode.
769fa9e406ahrens */
770fa9e406ahrens/* ARGSUSED */
771fa9e406ahrensstatic int
772fa9e406ahrenszfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
773da6c28aamw    int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
774da6c28aamw    int *direntflags, pathname_t *realpnp)
775fa9e406ahrens{
776fa9e406ahrens	zfsctl_snapdir_t *sdp = dvp->v_data;
777fa9e406ahrens	objset_t *snap;
7789adfa60Matthew Ahrens	char snapname[ZFS_MAX_DATASET_NAME_LEN];
7799adfa60Matthew Ahrens	char real[ZFS_MAX_DATASET_NAME_LEN];
780fa9e406ahrens	char *mountpoint;
781fa9e406ahrens	zfs_snapentry_t *sep, search;
782fa9e406ahrens	struct mounta margs;
783fa9e406ahrens	vfs_t *vfsp;
784fa9e406ahrens	size_t mountpoint_len;
785fa9e406ahrens	avl_index_t where;
786fa9e406ahrens	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
787fa9e406ahrens	int err;
788fa9e406ahrens
789da6c28aamw	/*
790da6c28aamw	 * No extended attributes allowed under .zfs
791da6c28aamw	 */
792da6c28aamw	if (flags & LOOKUP_XATTR)
793be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
794da6c28aamw
795fa9e406ahrens	ASSERT(dvp->v_type == VDIR);
796fa9e406ahrens
797fa9e406ahrens	/*
798fa9e406ahrens	 * If we get a recursive call, that means we got called
799fa9e406ahrens	 * from the domount() code while it was trying to look up the
800fa9e406ahrens	 * spec (which looks like a local path for zfs).  We need to
801fa9e406ahrens	 * add some flag to domount() to tell it not to do this lookup.
802fa9e406ahrens	 */
803fa9e406ahrens	if (MUTEX_HELD(&sdp->sd_lock))
804be6fd75Matthew Ahrens		return (SET_ERROR(ENOENT));
805fa9e406ahrens
806fa9e406ahrens	ZFS_ENTER(zfsvfs);
807fa9e406ahrens
808142ae85Chris Kirby	if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
809142ae85Chris Kirby		ZFS_EXIT(zfsvfs);
810142ae85Chris Kirby		return (0);
811142ae85Chris Kirby	}
812142ae85Chris Kirby
813ab04eb8timh	if (flags & FIGNORECASE) {
814ab04eb8timh		boolean_t conflict = B_FALSE;
815ab04eb8timh
816ab04eb8timh		err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
8179adfa60Matthew Ahrens		    sizeof (real), &conflict);
818ab04eb8timh		if (err == 0) {
819ab04eb8timh			nm = real;
820ab04eb8timh		} else if (err != ENOTSUP) {
821ab04eb8timh			ZFS_EXIT(zfsvfs);
822ab04eb8timh			return (err);
823ab04eb8timh		}
824ab04eb8timh		if (realpnp)
825ab04eb8timh			(void) strlcpy(realpnp->pn_buf, nm,
826ab04eb8timh			    realpnp->pn_bufsize);
827ab04eb8timh		if (conflict && direntflags)
828ab04eb8timh			*direntflags = ED_CASE_CONFLICT;
829ab04eb8timh	}
830ab04eb8timh
831fa9e406ahrens	mutex_enter(&sdp->sd_lock);
832fa9e406ahrens	search.se_name = (char *)nm;
833fa9e406ahrens	if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
834fa9e406ahrens		*vpp = sep->se_root;
835fa9e406ahrens		VN_HOLD(*vpp);
836bbf4a8dmaybee		err = traverse(vpp);
8373b2aab1Matthew Ahrens		if (err != 0) {
838bbf4a8dmaybee			VN_RELE(*vpp);
839bbf4a8dmaybee			*vpp = NULL;
840bbf4a8dmaybee		} else if (*vpp == sep->se_root) {
841bbf4a8dmaybee			/*
842bbf4a8dmaybee			 * The snapshot was unmounted behind our backs,
843bbf4a8dmaybee			 * try to remount it.
844bbf4a8dmaybee			 */
845fa9e406ahrens			goto domount;
8468bf40bfck		} else {
8478bf40bfck			/*
8488bf40bfck			 * VROOT was set during the traverse call.  We need
8498bf40bfck			 * to clear it since we're pretending to be part
8508bf40bfck			 * of our parent's vfs.
8518bf40bfck			 */
8528bf40bfck			(*vpp)->v_flag &= ~VROOT;
853bb963f1maybee		}
854fa9e406ahrens		mutex_exit(&sdp->sd_lock);
855fa9e406ahrens		ZFS_EXIT(zfsvfs);
856bbf4a8dmaybee		return (err);
857fa9e406ahrens	}
858fa9e406ahrens
859fa9e406ahrens	/*
860fa9e406ahrens	 * The requested snapshot is not currently mounted, look it up.
861fa9e406ahrens	 */
8629adfa60Matthew Ahrens	err = zfsctl_snapshot_zname(dvp, nm, sizeof (snapname), snapname);
8633b2aab1Matthew Ahrens	if (err != 0) {
8645422900maybee		mutex_exit(&sdp->sd_lock);
8655422900maybee		ZFS_EXIT(zfsvfs);
8662283022marks		/*
8672283022marks		 * handle "ls *" or "?" in a graceful manner,
8682283022marks		 * forcing EILSEQ to ENOENT.
8692283022marks		 * Since shell ultimately passes "*" or "?" as name to lookup
8702283022marks		 */
8712283022marks		return (err == EILSEQ ? ENOENT : err);
8725422900maybee	}
873503ad85Matthew Ahrens	if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
874fa9e406ahrens		mutex_exit(&sdp->sd_lock);
875fa9e406ahrens		ZFS_EXIT(zfsvfs);
876be6fd75Matthew Ahrens		return (SET_ERROR(ENOENT));
877fa9e406ahrens	}
878fa9e406ahrens
879fa9e406ahrens	sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
880fa9e406ahrens	sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
881fa9e406ahrens	(void) strcpy(sep->se_name, nm);
882fa9e406ahrens	*vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
883fa9e406ahrens	avl_insert(&sdp->sd_snaps, sep, where);
884fa9e406ahrens
885503ad85Matthew Ahrens	dmu_objset_rele(snap, FTAG);
886fa9e406ahrensdomount:
887fa9e406ahrens	mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
888fa9e406ahrens	    strlen("/.zfs/snapshot/") + strlen(nm) + 1;
889fa9e406ahrens	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
890fa9e406ahrens	(void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
891fa9e406ahrens	    refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
892fa9e406ahrens
893fa9e406ahrens	margs.spec = snapname;
894fa9e406ahrens	margs.dir = mountpoint;
895fa9e406ahrens	margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
896fa9e406ahrens	margs.fstype = "zfs";
897fa9e406ahrens	margs.dataptr = NULL;
898fa9e406ahrens	margs.datalen = 0;
899fa9e406ahrens	margs.optptr = NULL;
900fa9e406ahrens	margs.optlen = 0;
901fa9e406ahrens
902fa9e406ahrens	err = domount("zfs", &margs, *vpp, kcred, &vfsp);
903fa9e406ahrens	kmem_free(mountpoint, mountpoint_len);
904fa9e406ahrens
90595c9592maybee	if (err == 0) {
90695c9592maybee		/*
90795c9592maybee		 * Return the mounted root rather than the covered mount point.
908f18faf3ek		 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
909f18faf3ek		 * the ZFS vnode mounted on top of the GFS node.  This ZFS
910142ae85Chris Kirby		 * vnode is the root of the newly created vfsp.
91195c9592maybee		 */
91295c9592maybee		VFS_RELE(vfsp);
91395c9592maybee		err = traverse(vpp);
91495c9592maybee	}
915fa9e406ahrens
91695c9592maybee	if (err == 0) {
91795c9592maybee		/*
918f18faf3ek		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
919ed09798ek		 *
920ed09798ek		 * This is where we lie about our v_vfsp in order to
921