xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_ctldir.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26  */
27 
28 /*
29  * ZFS control directory (a.k.a. ".zfs")
30  *
31  * This directory provides a common location for all ZFS meta-objects.
32  * Currently, this is only the 'snapshot' directory, but this may expand in the
33  * future.  The elements are built using the GFS primitives, as the hierarchy
34  * does not actually exist on disk.
35  *
36  * For 'snapshot', we don't want to have all snapshots always mounted, because
37  * this would take up a huge amount of space in /etc/mnttab.  We have three
38  * types of objects:
39  *
40  * 	ctldir ------> snapshotdir -------> snapshot
41  *                                             |
42  *                                             |
43  *                                             V
44  *                                         mounted fs
45  *
46  * The 'snapshot' node contains just enough information to lookup '..' and act
47  * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
48  * perform an automount of the underlying filesystem and return the
49  * corresponding vnode.
50  *
51  * All mounts are handled automatically by the kernel, but unmounts are
52  * (currently) handled from user land.  The main reason is that there is no
53  * reliable way to auto-unmount the filesystem when it's "no longer in use".
54  * When the user unmounts a filesystem, we call zfsctl_unmount(), which
55  * unmounts any snapshots within the snapshot directory.
56  *
57  * The '.zfs', '.zfs/snapshot', and all directories created under
58  * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
59  * share the same vfs_t as the head filesystem (what '.zfs' lives under).
60  *
61  * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
62  * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
63  * However, vnodes within these mounted on file systems have their v_vfsp
64  * fields set to the head filesystem to make NFS happy (see
65  * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
66  * so that it cannot be freed until all snapshots have been unmounted.
67  */
68 
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/libkern.h>
72 #include <sys/dirent.h>
73 #include <sys/zfs_context.h>
74 #include <sys/zfs_ctldir.h>
75 #include <sys/zfs_ioctl.h>
76 #include <sys/zfs_vfsops.h>
77 #include <sys/namei.h>
78 #include <sys/stat.h>
79 #include <sys/dmu.h>
80 #include <sys/dsl_dataset.h>
81 #include <sys/dsl_destroy.h>
82 #include <sys/dsl_deleg.h>
83 #include <sys/mount.h>
84 #include <sys/zap.h>
85 #include <sys/sysproto.h>
86 
87 #include "zfs_namecheck.h"
88 
89 #include <sys/kernel.h>
90 #include <sys/ccompat.h>
91 
92 /* Common access mode for all virtual directories under the ctldir */
93 const uint16_t zfsctl_ctldir_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
94     S_IROTH | S_IXOTH;
95 
96 /*
97  * "Synthetic" filesystem implementation.
98  */
99 
100 /*
101  * Assert that A implies B.
102  */
103 #define	KASSERT_IMPLY(A, B, msg)	KASSERT(!(A) || (B), (msg));
104 
105 static MALLOC_DEFINE(M_SFSNODES, "sfs_nodes", "synthetic-fs nodes");
106 
107 typedef struct sfs_node {
108 	char		sn_name[ZFS_MAX_DATASET_NAME_LEN];
109 	uint64_t	sn_parent_id;
110 	uint64_t	sn_id;
111 } sfs_node_t;
112 
113 /*
114  * Check the parent's ID as well as the node's to account for a chance
115  * that IDs originating from different domains (snapshot IDs, artificial
116  * IDs, znode IDs) may clash.
117  */
118 static int
sfs_compare_ids(struct vnode * vp,void * arg)119 sfs_compare_ids(struct vnode *vp, void *arg)
120 {
121 	sfs_node_t *n1 = vp->v_data;
122 	sfs_node_t *n2 = arg;
123 	bool equal;
124 
125 	equal = n1->sn_id == n2->sn_id &&
126 	    n1->sn_parent_id == n2->sn_parent_id;
127 
128 	/* Zero means equality. */
129 	return (!equal);
130 }
131 
132 static int
sfs_vnode_get(const struct mount * mp,int flags,uint64_t parent_id,uint64_t id,struct vnode ** vpp)133 sfs_vnode_get(const struct mount *mp, int flags, uint64_t parent_id,
134     uint64_t id, struct vnode **vpp)
135 {
136 	sfs_node_t search;
137 	int err;
138 
139 	search.sn_id = id;
140 	search.sn_parent_id = parent_id;
141 	err = vfs_hash_get(mp, (uint32_t)id, flags, curthread, vpp,
142 	    sfs_compare_ids, &search);
143 	return (err);
144 }
145 
146 static int
sfs_vnode_insert(struct vnode * vp,int flags,uint64_t parent_id,uint64_t id,struct vnode ** vpp)147 sfs_vnode_insert(struct vnode *vp, int flags, uint64_t parent_id,
148     uint64_t id, struct vnode **vpp)
149 {
150 	int err;
151 
152 	KASSERT(vp->v_data != NULL, ("sfs_vnode_insert with NULL v_data"));
153 	err = vfs_hash_insert(vp, (uint32_t)id, flags, curthread, vpp,
154 	    sfs_compare_ids, vp->v_data);
155 	return (err);
156 }
157 
158 static void
sfs_vnode_remove(struct vnode * vp)159 sfs_vnode_remove(struct vnode *vp)
160 {
161 	vfs_hash_remove(vp);
162 }
163 
164 typedef void sfs_vnode_setup_fn(vnode_t *vp, void *arg);
165 
166 static int
sfs_vgetx(struct mount * mp,int flags,uint64_t parent_id,uint64_t id,const char * tag,struct vop_vector * vops,sfs_vnode_setup_fn setup,void * arg,struct vnode ** vpp)167 sfs_vgetx(struct mount *mp, int flags, uint64_t parent_id, uint64_t id,
168     const char *tag, struct vop_vector *vops,
169     sfs_vnode_setup_fn setup, void *arg,
170     struct vnode **vpp)
171 {
172 	struct vnode *vp;
173 	int error;
174 
175 	error = sfs_vnode_get(mp, flags, parent_id, id, vpp);
176 	if (error != 0 || *vpp != NULL) {
177 		KASSERT_IMPLY(error == 0, (*vpp)->v_data != NULL,
178 		    "sfs vnode with no data");
179 		return (error);
180 	}
181 
182 	/* Allocate a new vnode/inode. */
183 	error = getnewvnode(tag, mp, vops, &vp);
184 	if (error != 0) {
185 		*vpp = NULL;
186 		return (error);
187 	}
188 
189 	/*
190 	 * Exclusively lock the vnode vnode while it's being constructed.
191 	 */
192 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
193 	error = insmntque(vp, mp);
194 	if (error != 0) {
195 		*vpp = NULL;
196 		return (error);
197 	}
198 
199 	setup(vp, arg);
200 
201 	error = sfs_vnode_insert(vp, flags, parent_id, id, vpp);
202 	if (error != 0 || *vpp != NULL) {
203 		KASSERT_IMPLY(error == 0, (*vpp)->v_data != NULL,
204 		    "sfs vnode with no data");
205 		return (error);
206 	}
207 
208 #if __FreeBSD_version >= 1400077
209 	vn_set_state(vp, VSTATE_CONSTRUCTED);
210 #endif
211 
212 	*vpp = vp;
213 	return (0);
214 }
215 
216 static void
sfs_print_node(sfs_node_t * node)217 sfs_print_node(sfs_node_t *node)
218 {
219 	printf("\tname = %s\n", node->sn_name);
220 	printf("\tparent_id = %ju\n", (uintmax_t)node->sn_parent_id);
221 	printf("\tid = %ju\n", (uintmax_t)node->sn_id);
222 }
223 
224 static sfs_node_t *
sfs_alloc_node(size_t size,const char * name,uint64_t parent_id,uint64_t id)225 sfs_alloc_node(size_t size, const char *name, uint64_t parent_id, uint64_t id)
226 {
227 	struct sfs_node *node;
228 
229 	KASSERT(strlen(name) < sizeof (node->sn_name),
230 	    ("sfs node name is too long"));
231 	KASSERT(size >= sizeof (*node), ("sfs node size is too small"));
232 	node = malloc(size, M_SFSNODES, M_WAITOK | M_ZERO);
233 	strlcpy(node->sn_name, name, sizeof (node->sn_name));
234 	node->sn_parent_id = parent_id;
235 	node->sn_id = id;
236 
237 	return (node);
238 }
239 
240 static void
sfs_destroy_node(sfs_node_t * node)241 sfs_destroy_node(sfs_node_t *node)
242 {
243 	free(node, M_SFSNODES);
244 }
245 
246 static void *
sfs_reclaim_vnode(vnode_t * vp)247 sfs_reclaim_vnode(vnode_t *vp)
248 {
249 	void *data;
250 
251 	sfs_vnode_remove(vp);
252 	data = vp->v_data;
253 	vp->v_data = NULL;
254 	return (data);
255 }
256 
257 static int
sfs_readdir_common(uint64_t parent_id,uint64_t id,struct vop_readdir_args * ap,zfs_uio_t * uio,off_t * offp)258 sfs_readdir_common(uint64_t parent_id, uint64_t id, struct vop_readdir_args *ap,
259     zfs_uio_t *uio, off_t *offp)
260 {
261 	struct dirent entry;
262 	int error;
263 
264 	/* Reset ncookies for subsequent use of vfs_read_dirent. */
265 	if (ap->a_ncookies != NULL)
266 		*ap->a_ncookies = 0;
267 
268 	if (zfs_uio_resid(uio) < sizeof (entry))
269 		return (SET_ERROR(EINVAL));
270 
271 	if (zfs_uio_offset(uio) < 0)
272 		return (SET_ERROR(EINVAL));
273 	if (zfs_uio_offset(uio) == 0) {
274 		entry.d_fileno = id;
275 		entry.d_type = DT_DIR;
276 		entry.d_name[0] = '.';
277 		entry.d_name[1] = '\0';
278 		entry.d_namlen = 1;
279 		entry.d_reclen = sizeof (entry);
280 		error = vfs_read_dirent(ap, &entry, zfs_uio_offset(uio));
281 		if (error != 0)
282 			return (SET_ERROR(error));
283 	}
284 
285 	if (zfs_uio_offset(uio) < sizeof (entry))
286 		return (SET_ERROR(EINVAL));
287 	if (zfs_uio_offset(uio) == sizeof (entry)) {
288 		entry.d_fileno = parent_id;
289 		entry.d_type = DT_DIR;
290 		entry.d_name[0] = '.';
291 		entry.d_name[1] = '.';
292 		entry.d_name[2] = '\0';
293 		entry.d_namlen = 2;
294 		entry.d_reclen = sizeof (entry);
295 		error = vfs_read_dirent(ap, &entry, zfs_uio_offset(uio));
296 		if (error != 0)
297 			return (SET_ERROR(error));
298 	}
299 
300 	if (offp != NULL)
301 		*offp = 2 * sizeof (entry);
302 	return (0);
303 }
304 
305 
306 /*
307  * .zfs inode namespace
308  *
309  * We need to generate unique inode numbers for all files and directories
310  * within the .zfs pseudo-filesystem.  We use the following scheme:
311  *
312  * 	ENTRY			ZFSCTL_INODE
313  * 	.zfs			1
314  * 	.zfs/snapshot		2
315  * 	.zfs/snapshot/<snap>	objectid(snap)
316  */
317 #define	ZFSCTL_INO_SNAP(id)	(id)
318 
319 static struct vop_vector zfsctl_ops_root;
320 static struct vop_vector zfsctl_ops_snapdir;
321 static struct vop_vector zfsctl_ops_snapshot;
322 
323 void
zfsctl_init(void)324 zfsctl_init(void)
325 {
326 }
327 
328 void
zfsctl_fini(void)329 zfsctl_fini(void)
330 {
331 }
332 
333 boolean_t
zfsctl_is_node(vnode_t * vp)334 zfsctl_is_node(vnode_t *vp)
335 {
336 	return (vn_matchops(vp, zfsctl_ops_root) ||
337 	    vn_matchops(vp, zfsctl_ops_snapdir) ||
338 	    vn_matchops(vp, zfsctl_ops_snapshot));
339 
340 }
341 
342 typedef struct zfsctl_root {
343 	sfs_node_t	node;
344 	sfs_node_t	*snapdir;
345 	timestruc_t	cmtime;
346 } zfsctl_root_t;
347 
348 
349 /*
350  * Create the '.zfs' directory.
351  */
352 void
zfsctl_create(zfsvfs_t * zfsvfs)353 zfsctl_create(zfsvfs_t *zfsvfs)
354 {
355 	zfsctl_root_t *dot_zfs;
356 	sfs_node_t *snapdir;
357 	vnode_t *rvp;
358 	uint64_t crtime[2];
359 
360 	ASSERT3P(zfsvfs->z_ctldir, ==, NULL);
361 
362 	snapdir = sfs_alloc_node(sizeof (*snapdir), "snapshot", ZFSCTL_INO_ROOT,
363 	    ZFSCTL_INO_SNAPDIR);
364 	dot_zfs = (zfsctl_root_t *)sfs_alloc_node(sizeof (*dot_zfs), ".zfs", 0,
365 	    ZFSCTL_INO_ROOT);
366 	dot_zfs->snapdir = snapdir;
367 
368 	VERIFY0(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp));
369 	VERIFY0(sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
370 	    &crtime, sizeof (crtime)));
371 	ZFS_TIME_DECODE(&dot_zfs->cmtime, crtime);
372 	vput(rvp);
373 
374 	zfsvfs->z_ctldir = dot_zfs;
375 }
376 
377 /*
378  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
379  * The nodes must not have any associated vnodes by now as they should be
380  * vflush-ed.
381  */
382 void
zfsctl_destroy(zfsvfs_t * zfsvfs)383 zfsctl_destroy(zfsvfs_t *zfsvfs)
384 {
385 	sfs_destroy_node(zfsvfs->z_ctldir->snapdir);
386 	sfs_destroy_node((sfs_node_t *)zfsvfs->z_ctldir);
387 	zfsvfs->z_ctldir = NULL;
388 }
389 
390 static int
zfsctl_fs_root_vnode(struct mount * mp,void * arg __unused,int flags,struct vnode ** vpp)391 zfsctl_fs_root_vnode(struct mount *mp, void *arg __unused, int flags,
392     struct vnode **vpp)
393 {
394 	return (VFS_ROOT(mp, flags, vpp));
395 }
396 
397 static void
zfsctl_common_vnode_setup(vnode_t * vp,void * arg)398 zfsctl_common_vnode_setup(vnode_t *vp, void *arg)
399 {
400 	ASSERT_VOP_ELOCKED(vp, __func__);
401 
402 	/* We support shared locking. */
403 	VN_LOCK_ASHARE(vp);
404 	vp->v_type = VDIR;
405 	vp->v_data = arg;
406 }
407 
408 static int
zfsctl_root_vnode(struct mount * mp,void * arg __unused,int flags,struct vnode ** vpp)409 zfsctl_root_vnode(struct mount *mp, void *arg __unused, int flags,
410     struct vnode **vpp)
411 {
412 	void *node;
413 	int err;
414 
415 	node = ((zfsvfs_t *)mp->mnt_data)->z_ctldir;
416 	err = sfs_vgetx(mp, flags, 0, ZFSCTL_INO_ROOT, "zfs", &zfsctl_ops_root,
417 	    zfsctl_common_vnode_setup, node, vpp);
418 	return (err);
419 }
420 
421 static int
zfsctl_snapdir_vnode(struct mount * mp,void * arg __unused,int flags,struct vnode ** vpp)422 zfsctl_snapdir_vnode(struct mount *mp, void *arg __unused, int flags,
423     struct vnode **vpp)
424 {
425 	void *node;
426 	int err;
427 
428 	node = ((zfsvfs_t *)mp->mnt_data)->z_ctldir->snapdir;
429 	err = sfs_vgetx(mp, flags, ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, "zfs",
430 	    &zfsctl_ops_snapdir, zfsctl_common_vnode_setup, node, vpp);
431 	return (err);
432 }
433 
434 /*
435  * Given a root znode, retrieve the associated .zfs directory.
436  * Add a hold to the vnode and return it.
437  */
438 int
zfsctl_root(zfsvfs_t * zfsvfs,int flags,vnode_t ** vpp)439 zfsctl_root(zfsvfs_t *zfsvfs, int flags, vnode_t **vpp)
440 {
441 	int error;
442 
443 	error = zfsctl_root_vnode(zfsvfs->z_vfs, NULL, flags, vpp);
444 	return (error);
445 }
446 
447 /*
448  * Common open routine.  Disallow any write access.
449  */
450 static int
zfsctl_common_open(struct vop_open_args * ap)451 zfsctl_common_open(struct vop_open_args *ap)
452 {
453 	int flags = ap->a_mode;
454 
455 	if (flags & FWRITE)
456 		return (SET_ERROR(EACCES));
457 
458 	return (0);
459 }
460 
461 /*
462  * Common close routine.  Nothing to do here.
463  */
464 static int
zfsctl_common_close(struct vop_close_args * ap)465 zfsctl_common_close(struct vop_close_args *ap)
466 {
467 	(void) ap;
468 	return (0);
469 }
470 
471 /*
472  * Common access routine.  Disallow writes.
473  */
474 static int
zfsctl_common_access(struct vop_access_args * ap)475 zfsctl_common_access(struct vop_access_args *ap)
476 {
477 	accmode_t accmode = ap->a_accmode;
478 
479 	if (accmode & VWRITE)
480 		return (SET_ERROR(EACCES));
481 	return (0);
482 }
483 
484 /*
485  * Common getattr function.  Fill in basic information.
486  */
487 static void
zfsctl_common_getattr(vnode_t * vp,vattr_t * vap)488 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
489 {
490 	timestruc_t	now;
491 	sfs_node_t *node;
492 
493 	node = vp->v_data;
494 
495 	vap->va_uid = 0;
496 	vap->va_gid = 0;
497 	vap->va_rdev = 0;
498 	/*
499 	 * We are a purely virtual object, so we have no
500 	 * blocksize or allocated blocks.
501 	 */
502 	vap->va_blksize = 0;
503 	vap->va_nblocks = 0;
504 	vap->va_gen = 0;
505 	vn_fsid(vp, vap);
506 	vap->va_mode = zfsctl_ctldir_mode;
507 	vap->va_type = VDIR;
508 	/*
509 	 * We live in the now (for atime).
510 	 */
511 	gethrestime(&now);
512 	vap->va_atime = now;
513 	/* FreeBSD: Reset chflags(2) flags. */
514 	vap->va_flags = 0;
515 
516 	vap->va_nodeid = node->sn_id;
517 
518 	/* At least '.' and '..'. */
519 	vap->va_nlink = 2;
520 }
521 
522 #ifndef _OPENSOLARIS_SYS_VNODE_H_
523 struct vop_fid_args {
524 	struct vnode *a_vp;
525 	struct fid *a_fid;
526 };
527 #endif
528 
529 static int
zfsctl_common_fid(struct vop_fid_args * ap)530 zfsctl_common_fid(struct vop_fid_args *ap)
531 {
532 	vnode_t		*vp = ap->a_vp;
533 	fid_t		*fidp = (void *)ap->a_fid;
534 	sfs_node_t	*node = vp->v_data;
535 	uint64_t	object = node->sn_id;
536 	zfid_short_t	*zfid;
537 	int		i;
538 
539 	zfid = (zfid_short_t *)fidp;
540 	zfid->zf_len = SHORT_FID_LEN;
541 
542 	for (i = 0; i < sizeof (zfid->zf_object); i++)
543 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
544 
545 	/* .zfs nodes always have a generation number of 0 */
546 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
547 		zfid->zf_gen[i] = 0;
548 
549 	return (0);
550 }
551 
552 #ifndef _SYS_SYSPROTO_H_
553 struct vop_reclaim_args {
554 	struct vnode *a_vp;
555 	struct thread *a_td;
556 };
557 #endif
558 
559 static int
zfsctl_common_reclaim(struct vop_reclaim_args * ap)560 zfsctl_common_reclaim(struct vop_reclaim_args *ap)
561 {
562 	vnode_t *vp = ap->a_vp;
563 
564 	(void) sfs_reclaim_vnode(vp);
565 	return (0);
566 }
567 
568 #ifndef _SYS_SYSPROTO_H_
569 struct vop_print_args {
570 	struct vnode *a_vp;
571 };
572 #endif
573 
574 static int
zfsctl_common_print(struct vop_print_args * ap)575 zfsctl_common_print(struct vop_print_args *ap)
576 {
577 	sfs_print_node(ap->a_vp->v_data);
578 	return (0);
579 }
580 
581 #ifndef _SYS_SYSPROTO_H_
582 struct vop_getattr_args {
583 	struct vnode *a_vp;
584 	struct vattr *a_vap;
585 	struct ucred *a_cred;
586 };
587 #endif
588 
589 /*
590  * Get root directory attributes.
591  */
592 static int
zfsctl_root_getattr(struct vop_getattr_args * ap)593 zfsctl_root_getattr(struct vop_getattr_args *ap)
594 {
595 	struct vnode *vp = ap->a_vp;
596 	struct vattr *vap = ap->a_vap;
597 	zfsctl_root_t *node = vp->v_data;
598 
599 	zfsctl_common_getattr(vp, vap);
600 	vap->va_ctime = node->cmtime;
601 	vap->va_mtime = vap->va_ctime;
602 	vap->va_birthtime = vap->va_ctime;
603 	vap->va_nlink += 1; /* snapdir */
604 	vap->va_size = vap->va_nlink;
605 	return (0);
606 }
607 
608 /*
609  * When we lookup "." we still can be asked to lock it
610  * differently, can't we?
611  */
612 static int
zfsctl_relock_dot(vnode_t * dvp,int ltype)613 zfsctl_relock_dot(vnode_t *dvp, int ltype)
614 {
615 	vref(dvp);
616 	if (ltype != VOP_ISLOCKED(dvp)) {
617 		if (ltype == LK_EXCLUSIVE)
618 			vn_lock(dvp, LK_UPGRADE | LK_RETRY);
619 		else /* if (ltype == LK_SHARED) */
620 			vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
621 
622 		/* Relock for the "." case may left us with reclaimed vnode. */
623 		if (VN_IS_DOOMED(dvp)) {
624 			vrele(dvp);
625 			return (SET_ERROR(ENOENT));
626 		}
627 	}
628 	return (0);
629 }
630 
631 /*
632  * Special case the handling of "..".
633  */
634 static int
zfsctl_root_lookup(struct vop_lookup_args * ap)635 zfsctl_root_lookup(struct vop_lookup_args *ap)
636 {
637 	struct componentname *cnp = ap->a_cnp;
638 	vnode_t *dvp = ap->a_dvp;
639 	vnode_t **vpp = ap->a_vpp;
640 	int flags = ap->a_cnp->cn_flags;
641 	int lkflags = ap->a_cnp->cn_lkflags;
642 	int nameiop = ap->a_cnp->cn_nameiop;
643 	int err;
644 
645 	ASSERT3S(dvp->v_type, ==, VDIR);
646 
647 	if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP)
648 		return (SET_ERROR(ENOTSUP));
649 
650 	if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
651 		err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK);
652 		if (err == 0)
653 			*vpp = dvp;
654 	} else if ((flags & ISDOTDOT) != 0) {
655 		err = vn_vget_ino_gen(dvp, zfsctl_fs_root_vnode, NULL,
656 		    lkflags, vpp);
657 	} else if (strncmp(cnp->cn_nameptr, "snapshot", cnp->cn_namelen) == 0) {
658 		err = zfsctl_snapdir_vnode(dvp->v_mount, NULL, lkflags, vpp);
659 	} else {
660 		err = SET_ERROR(ENOENT);
661 	}
662 	if (err != 0)
663 		*vpp = NULL;
664 	return (err);
665 }
666 
667 static int
zfsctl_root_readdir(struct vop_readdir_args * ap)668 zfsctl_root_readdir(struct vop_readdir_args *ap)
669 {
670 	struct dirent entry;
671 	vnode_t *vp = ap->a_vp;
672 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
673 	zfsctl_root_t *node = vp->v_data;
674 	zfs_uio_t uio;
675 	int *eofp = ap->a_eofflag;
676 	off_t dots_offset;
677 	int error;
678 
679 	zfs_uio_init(&uio, ap->a_uio);
680 
681 	ASSERT3S(vp->v_type, ==, VDIR);
682 
683 	/*
684 	 * FIXME: this routine only ever emits 3 entries and does not tolerate
685 	 * being called with a buffer too small to handle all of them.
686 	 *
687 	 * The check below facilitates the idiom of repeating calls until the
688 	 * count to return is 0.
689 	 */
690 	if (zfs_uio_offset(&uio) == 3 * sizeof (entry)) {
691 		return (0);
692 	}
693 
694 	error = sfs_readdir_common(zfsvfs->z_root, ZFSCTL_INO_ROOT, ap, &uio,
695 	    &dots_offset);
696 	if (error != 0) {
697 		if (error == ENAMETOOLONG) /* ran out of destination space */
698 			error = 0;
699 		return (error);
700 	}
701 	if (zfs_uio_offset(&uio) != dots_offset)
702 		return (SET_ERROR(EINVAL));
703 
704 	_Static_assert(sizeof (node->snapdir->sn_name) <= sizeof (entry.d_name),
705 	    "node->snapdir->sn_name too big for entry.d_name");
706 	entry.d_fileno = node->snapdir->sn_id;
707 	entry.d_type = DT_DIR;
708 	strcpy(entry.d_name, node->snapdir->sn_name);
709 	entry.d_namlen = strlen(entry.d_name);
710 	entry.d_reclen = sizeof (entry);
711 	error = vfs_read_dirent(ap, &entry, zfs_uio_offset(&uio));
712 	if (error != 0) {
713 		if (error == ENAMETOOLONG)
714 			error = 0;
715 		return (SET_ERROR(error));
716 	}
717 	if (eofp != NULL)
718 		*eofp = 1;
719 	return (0);
720 }
721 
722 static int
zfsctl_root_vptocnp(struct vop_vptocnp_args * ap)723 zfsctl_root_vptocnp(struct vop_vptocnp_args *ap)
724 {
725 	static const char dotzfs_name[4] = ".zfs";
726 	vnode_t *dvp;
727 	int error;
728 
729 	if (*ap->a_buflen < sizeof (dotzfs_name))
730 		return (SET_ERROR(ENOMEM));
731 
732 	error = vn_vget_ino_gen(ap->a_vp, zfsctl_fs_root_vnode, NULL,
733 	    LK_SHARED, &dvp);
734 	if (error != 0)
735 		return (SET_ERROR(error));
736 
737 	VOP_UNLOCK(dvp);
738 	*ap->a_vpp = dvp;
739 	*ap->a_buflen -= sizeof (dotzfs_name);
740 	memcpy(ap->a_buf + *ap->a_buflen, dotzfs_name, sizeof (dotzfs_name));
741 	return (0);
742 }
743 
744 static int
zfsctl_common_pathconf(struct vop_pathconf_args * ap)745 zfsctl_common_pathconf(struct vop_pathconf_args *ap)
746 {
747 	/*
748 	 * We care about ACL variables so that user land utilities like ls
749 	 * can display them correctly.  Since the ctldir's st_dev is set to be
750 	 * the same as the parent dataset, we must support all variables that
751 	 * it supports.
752 	 */
753 	switch (ap->a_name) {
754 	case _PC_LINK_MAX:
755 		*ap->a_retval = MIN(LONG_MAX, ZFS_LINK_MAX);
756 		return (0);
757 
758 	case _PC_FILESIZEBITS:
759 		*ap->a_retval = 64;
760 		return (0);
761 
762 	case _PC_MIN_HOLE_SIZE:
763 		*ap->a_retval = (int)SPA_MINBLOCKSIZE;
764 		return (0);
765 
766 	case _PC_ACL_EXTENDED:
767 		*ap->a_retval = 0;
768 		return (0);
769 
770 	case _PC_ACL_NFS4:
771 		*ap->a_retval = 1;
772 		return (0);
773 
774 	case _PC_ACL_PATH_MAX:
775 		*ap->a_retval = ACL_MAX_ENTRIES;
776 		return (0);
777 
778 	case _PC_NAME_MAX:
779 		*ap->a_retval = NAME_MAX;
780 		return (0);
781 
782 	default:
783 		return (vop_stdpathconf(ap));
784 	}
785 }
786 
787 /*
788  * Returns a trivial ACL
789  */
790 static int
zfsctl_common_getacl(struct vop_getacl_args * ap)791 zfsctl_common_getacl(struct vop_getacl_args *ap)
792 {
793 	int i;
794 
795 	if (ap->a_type != ACL_TYPE_NFS4)
796 		return (EINVAL);
797 
798 	acl_nfs4_sync_acl_from_mode(ap->a_aclp, zfsctl_ctldir_mode, 0);
799 	/*
800 	 * acl_nfs4_sync_acl_from_mode assumes that the owner can always modify
801 	 * attributes.  That is not the case for the ctldir, so we must clear
802 	 * those bits.  We also must clear ACL_READ_NAMED_ATTRS, because xattrs
803 	 * aren't supported by the ctldir.
804 	 */
805 	for (i = 0; i < ap->a_aclp->acl_cnt; i++) {
806 		struct acl_entry *entry;
807 		entry = &(ap->a_aclp->acl_entry[i]);
808 		entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER |
809 		    ACL_WRITE_ATTRIBUTES | ACL_WRITE_NAMED_ATTRS |
810 		    ACL_READ_NAMED_ATTRS);
811 	}
812 
813 	return (0);
814 }
815 
816 static struct vop_vector zfsctl_ops_root = {
817 	.vop_default =	&default_vnodeops,
818 	.vop_fplookup_vexec = VOP_EAGAIN,
819 	.vop_fplookup_symlink = VOP_EAGAIN,
820 	.vop_open =	zfsctl_common_open,
821 	.vop_close =	zfsctl_common_close,
822 	.vop_ioctl =	VOP_EINVAL,
823 	.vop_getattr =	zfsctl_root_getattr,
824 	.vop_access =	zfsctl_common_access,
825 	.vop_readdir =	zfsctl_root_readdir,
826 	.vop_lookup =	zfsctl_root_lookup,
827 	.vop_inactive =	VOP_NULL,
828 	.vop_reclaim =	zfsctl_common_reclaim,
829 	.vop_fid =	zfsctl_common_fid,
830 	.vop_print =	zfsctl_common_print,
831 	.vop_vptocnp =	zfsctl_root_vptocnp,
832 	.vop_pathconf =	zfsctl_common_pathconf,
833 	.vop_getacl =	zfsctl_common_getacl,
834 #if __FreeBSD_version >= 1400043
835 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
836 #endif
837 };
838 VFS_VOP_VECTOR_REGISTER(zfsctl_ops_root);
839 
840 static int
zfsctl_snapshot_zname(vnode_t * vp,const char * name,int len,char * zname)841 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
842 {
843 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
844 
845 	dmu_objset_name(os, zname);
846 	if (strlen(zname) + 1 + strlen(name) >= len)
847 		return (SET_ERROR(ENAMETOOLONG));
848 	(void) strcat(zname, "@");
849 	(void) strcat(zname, name);
850 	return (0);
851 }
852 
853 static int
zfsctl_snapshot_lookup(vnode_t * vp,const char * name,uint64_t * id)854 zfsctl_snapshot_lookup(vnode_t *vp, const char *name, uint64_t *id)
855 {
856 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
857 	int err;
858 
859 	err = dsl_dataset_snap_lookup(dmu_objset_ds(os), name, id);
860 	return (err);
861 }
862 
863 /*
864  * Given a vnode get a root vnode of a filesystem mounted on top of
865  * the vnode, if any.  The root vnode is referenced and locked.
866  * If no filesystem is mounted then the orinal vnode remains referenced
867  * and locked.  If any error happens the orinal vnode is unlocked and
868  * released.
869  */
870 static int
zfsctl_mounted_here(vnode_t ** vpp,int flags)871 zfsctl_mounted_here(vnode_t **vpp, int flags)
872 {
873 	struct mount *mp;
874 	int err;
875 
876 	ASSERT_VOP_LOCKED(*vpp, __func__);
877 	ASSERT3S((*vpp)->v_type, ==, VDIR);
878 
879 	if ((mp = (*vpp)->v_mountedhere) != NULL) {
880 		err = vfs_busy(mp, 0);
881 		KASSERT(err == 0, ("vfs_busy(mp, 0) failed with %d", err));
882 		KASSERT(vrefcnt(*vpp) > 1, ("unreferenced mountpoint"));
883 		vput(*vpp);
884 		err = VFS_ROOT(mp, flags, vpp);
885 		vfs_unbusy(mp);
886 		return (err);
887 	}
888 	return (EJUSTRETURN);
889 }
890 
891 typedef struct {
892 	const char *snap_name;
893 	uint64_t    snap_id;
894 } snapshot_setup_arg_t;
895 
896 static void
zfsctl_snapshot_vnode_setup(vnode_t * vp,void * arg)897 zfsctl_snapshot_vnode_setup(vnode_t *vp, void *arg)
898 {
899 	snapshot_setup_arg_t *ssa = arg;
900 	sfs_node_t *node;
901 
902 	ASSERT_VOP_ELOCKED(vp, __func__);
903 
904 	node = sfs_alloc_node(sizeof (sfs_node_t),
905 	    ssa->snap_name, ZFSCTL_INO_SNAPDIR, ssa->snap_id);
906 	zfsctl_common_vnode_setup(vp, node);
907 
908 	/* We have to support recursive locking. */
909 	VN_LOCK_AREC(vp);
910 }
911 
912 /*
913  * Lookup entry point for the 'snapshot' directory.  Try to open the
914  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
915  * Perform a mount of the associated dataset on top of the vnode.
916  * There are four possibilities:
917  * - the snapshot node and vnode do not exist
918  * - the snapshot vnode is covered by the mounted snapshot
919  * - the snapshot vnode is not covered yet, the mount operation is in progress
920  * - the snapshot vnode is not covered, because the snapshot has been unmounted
921  * The last two states are transient and should be relatively short-lived.
922  */
923 static int
zfsctl_snapdir_lookup(struct vop_lookup_args * ap)924 zfsctl_snapdir_lookup(struct vop_lookup_args *ap)
925 {
926 	vnode_t *dvp = ap->a_dvp;
927 	vnode_t **vpp = ap->a_vpp;
928 	struct componentname *cnp = ap->a_cnp;
929 	char name[NAME_MAX + 1];
930 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
931 	char *mountpoint;
932 	size_t mountpoint_len;
933 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
934 	uint64_t snap_id;
935 	int nameiop = cnp->cn_nameiop;
936 	int lkflags = cnp->cn_lkflags;
937 	int flags = cnp->cn_flags;
938 	int err;
939 
940 	ASSERT3S(dvp->v_type, ==, VDIR);
941 
942 	if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP)
943 		return (SET_ERROR(ENOTSUP));
944 
945 	if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
946 		err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK);
947 		if (err == 0)
948 			*vpp = dvp;
949 		return (err);
950 	}
951 	if (flags & ISDOTDOT) {
952 		err = vn_vget_ino_gen(dvp, zfsctl_root_vnode, NULL, lkflags,
953 		    vpp);
954 		return (err);
955 	}
956 
957 	if (cnp->cn_namelen >= sizeof (name))
958 		return (SET_ERROR(ENAMETOOLONG));
959 
960 	strlcpy(name, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
961 	err = zfsctl_snapshot_lookup(dvp, name, &snap_id);
962 	if (err != 0)
963 		return (SET_ERROR(ENOENT));
964 
965 	for (;;) {
966 		snapshot_setup_arg_t ssa;
967 
968 		ssa.snap_name = name;
969 		ssa.snap_id = snap_id;
970 		err = sfs_vgetx(dvp->v_mount, LK_SHARED, ZFSCTL_INO_SNAPDIR,
971 		    snap_id, "zfs", &zfsctl_ops_snapshot,
972 		    zfsctl_snapshot_vnode_setup, &ssa, vpp);
973 		if (err != 0)
974 			return (err);
975 
976 		/* Check if a new vnode has just been created. */
977 		if (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE)
978 			break;
979 
980 		/*
981 		 * Check if a snapshot is already mounted on top of the vnode.
982 		 */
983 		err = zfsctl_mounted_here(vpp, lkflags);
984 		if (err != EJUSTRETURN)
985 			return (err);
986 
987 		/*
988 		 * If the vnode is not covered, then either the mount operation
989 		 * is in progress or the snapshot has already been unmounted
990 		 * but the vnode hasn't been inactivated and reclaimed yet.
991 		 * We can try to re-use the vnode in the latter case.
992 		 */
993 		VI_LOCK(*vpp);
994 		if (((*vpp)->v_iflag & VI_MOUNT) == 0) {
995 			VI_UNLOCK(*vpp);
996 			/*
997 			 * Upgrade to exclusive lock in order to:
998 			 * - avoid race conditions
999 			 * - satisfy the contract of mount_snapshot()
1000 			 */
1001 			err = VOP_LOCK(*vpp, LK_TRYUPGRADE);
1002 			if (err == 0)
1003 				break;
1004 		} else {
1005 			VI_UNLOCK(*vpp);
1006 		}
1007 
1008 		/*
1009 		 * In this state we can loop on uncontested locks and starve
1010 		 * the thread doing the lengthy, non-trivial mount operation.
1011 		 * So, yield to prevent that from happening.
1012 		 */
1013 		vput(*vpp);
1014 		kern_yield(PRI_USER);
1015 	}
1016 
1017 	VERIFY0(zfsctl_snapshot_zname(dvp, name, sizeof (fullname), fullname));
1018 
1019 	mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1020 	    strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(name) + 1;
1021 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1022 	(void) snprintf(mountpoint, mountpoint_len,
1023 	    "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1024 	    dvp->v_vfsp->mnt_stat.f_mntonname, name);
1025 
1026 	err = mount_snapshot(curthread, vpp, "zfs", mountpoint, fullname, 0,
1027 	    dvp->v_vfsp);
1028 	kmem_free(mountpoint, mountpoint_len);
1029 	if (err == 0) {
1030 		/*
1031 		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1032 		 *
1033 		 * This is where we lie about our v_vfsp in order to
1034 		 * make .zfs/snapshot/<snapname> accessible over NFS
1035 		 * without requiring manual mounts of <snapname>.
1036 		 */
1037 		ASSERT3P(VTOZ(*vpp)->z_zfsvfs, !=, zfsvfs);
1038 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1039 
1040 		/* Clear the root flag (set via VFS_ROOT) as well. */
1041 		(*vpp)->v_vflag &= ~VV_ROOT;
1042 	}
1043 
1044 	if (err != 0)
1045 		*vpp = NULL;
1046 	return (err);
1047 }
1048 
1049 static int
zfsctl_snapdir_readdir(struct vop_readdir_args * ap)1050 zfsctl_snapdir_readdir(struct vop_readdir_args *ap)
1051 {
1052 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
1053 	struct dirent entry;
1054 	vnode_t *vp = ap->a_vp;
1055 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1056 	zfs_uio_t uio;
1057 	int *eofp = ap->a_eofflag;
1058 	off_t dots_offset;
1059 	int error;
1060 
1061 	zfs_uio_init(&uio, ap->a_uio);
1062 
1063 	ASSERT3S(vp->v_type, ==, VDIR);
1064 
1065 	error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap,
1066 	    &uio, &dots_offset);
1067 	if (error != 0) {
1068 		if (error == ENAMETOOLONG) /* ran out of destination space */
1069 			error = 0;
1070 		return (error);
1071 	}
1072 
1073 	if ((error = zfs_enter(zfsvfs, FTAG)) != 0)
1074 		return (error);
1075 	for (;;) {
1076 		uint64_t cookie;
1077 		uint64_t id;
1078 
1079 		cookie = zfs_uio_offset(&uio) - dots_offset;
1080 
1081 		dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1082 		error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof (snapname),
1083 		    snapname, &id, &cookie, NULL);
1084 		dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1085 		if (error != 0) {
1086 			if (error == ENOENT) {
1087 				if (eofp != NULL)
1088 					*eofp = 1;
1089 				error = 0;
1090 			}
1091 			zfs_exit(zfsvfs, FTAG);
1092 			return (error);
1093 		}
1094 
1095 		entry.d_fileno = id;
1096 		entry.d_type = DT_DIR;
1097 		strcpy(entry.d_name, snapname);
1098 		entry.d_namlen = strlen(entry.d_name);
1099 		entry.d_reclen = sizeof (entry);
1100 		error = vfs_read_dirent(ap, &entry, zfs_uio_offset(&uio));
1101 		if (error != 0) {
1102 			if (error == ENAMETOOLONG)
1103 				error = 0;
1104 			zfs_exit(zfsvfs, FTAG);
1105 			return (SET_ERROR(error));
1106 		}
1107 		zfs_uio_setoffset(&uio, cookie + dots_offset);
1108 	}
1109 	__builtin_unreachable();
1110 }
1111 
1112 static int
zfsctl_snapdir_getattr(struct vop_getattr_args * ap)1113 zfsctl_snapdir_getattr(struct vop_getattr_args *ap)
1114 {
1115 	vnode_t *vp = ap->a_vp;
1116 	vattr_t *vap = ap->a_vap;
1117 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1118 	dsl_dataset_t *ds;
1119 	uint64_t snap_count;
1120 	int err;
1121 
1122 	if ((err = zfs_enter(zfsvfs, FTAG)) != 0)
1123 		return (err);
1124 	ds = dmu_objset_ds(zfsvfs->z_os);
1125 	zfsctl_common_getattr(vp, vap);
1126 	vap->va_ctime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1127 	vap->va_mtime = vap->va_ctime;
1128 	vap->va_birthtime = vap->va_ctime;
1129 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
1130 		err = zap_count(dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
1131 		    dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
1132 		if (err != 0) {
1133 			zfs_exit(zfsvfs, FTAG);
1134 			return (err);
1135 		}
1136 		vap->va_nlink += snap_count;
1137 	}
1138 	vap->va_size = vap->va_nlink;
1139 
1140 	zfs_exit(zfsvfs, FTAG);
1141 	return (0);
1142 }
1143 
1144 static struct vop_vector zfsctl_ops_snapdir = {
1145 	.vop_default =	&default_vnodeops,
1146 	.vop_fplookup_vexec = VOP_EAGAIN,
1147 	.vop_fplookup_symlink = VOP_EAGAIN,
1148 	.vop_open =	zfsctl_common_open,
1149 	.vop_close =	zfsctl_common_close,
1150 	.vop_getattr =	zfsctl_snapdir_getattr,
1151 	.vop_access =	zfsctl_common_access,
1152 	.vop_readdir =	zfsctl_snapdir_readdir,
1153 	.vop_lookup =	zfsctl_snapdir_lookup,
1154 	.vop_reclaim =	zfsctl_common_reclaim,
1155 	.vop_fid =	zfsctl_common_fid,
1156 	.vop_print =	zfsctl_common_print,
1157 	.vop_pathconf =	zfsctl_common_pathconf,
1158 	.vop_getacl =	zfsctl_common_getacl,
1159 #if __FreeBSD_version >= 1400043
1160 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
1161 #endif
1162 };
1163 VFS_VOP_VECTOR_REGISTER(zfsctl_ops_snapdir);
1164 
1165 
1166 static int
zfsctl_snapshot_inactive(struct vop_inactive_args * ap)1167 zfsctl_snapshot_inactive(struct vop_inactive_args *ap)
1168 {
1169 	vnode_t *vp = ap->a_vp;
1170 
1171 	vrecycle(vp);
1172 	return (0);
1173 }
1174 
1175 static int
zfsctl_snapshot_reclaim(struct vop_reclaim_args * ap)1176 zfsctl_snapshot_reclaim(struct vop_reclaim_args *ap)
1177 {
1178 	vnode_t *vp = ap->a_vp;
1179 	void *data = vp->v_data;
1180 
1181 	sfs_reclaim_vnode(vp);
1182 	sfs_destroy_node(data);
1183 	return (0);
1184 }
1185 
1186 static int
zfsctl_snapshot_vptocnp(struct vop_vptocnp_args * ap)1187 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1188 {
1189 	struct mount *mp;
1190 	vnode_t *dvp;
1191 	vnode_t *vp;
1192 	sfs_node_t *node;
1193 	size_t len;
1194 	int locked;
1195 	int error;
1196 
1197 	vp = ap->a_vp;
1198 	node = vp->v_data;
1199 	len = strlen(node->sn_name);
1200 	if (*ap->a_buflen < len)
1201 		return (SET_ERROR(ENOMEM));
1202 
1203 	/*
1204 	 * Prevent unmounting of the snapshot while the vnode lock
1205 	 * is not held.  That is not strictly required, but allows
1206 	 * us to assert that an uncovered snapshot vnode is never
1207 	 * "leaked".
1208 	 */
1209 	mp = vp->v_mountedhere;
1210 	if (mp == NULL)
1211 		return (SET_ERROR(ENOENT));
1212 	error = vfs_busy(mp, 0);
1213 	KASSERT(error == 0, ("vfs_busy(mp, 0) failed with %d", error));
1214 
1215 	/*
1216 	 * We can vput the vnode as we can now depend on the reference owned
1217 	 * by the busied mp.  But we also need to hold the vnode, because
1218 	 * the reference may go after vfs_unbusy() which has to be called
1219 	 * before we can lock the vnode again.
1220 	 */
1221 	locked = VOP_ISLOCKED(vp);
1222 	enum vgetstate vs = vget_prep(vp);
1223 	vput(vp);
1224 
1225 	/* Look up .zfs/snapshot, our parent. */
1226 	error = zfsctl_snapdir_vnode(vp->v_mount, NULL, LK_SHARED, &dvp);
1227 	if (error == 0) {
1228 		VOP_UNLOCK(dvp);
1229 		*ap->a_vpp = dvp;
1230 		*ap->a_buflen -= len;
1231 		memcpy(ap->a_buf + *ap->a_buflen, node->sn_name, len);
1232 	}
1233 	vfs_unbusy(mp);
1234 	vget_finish(vp, locked | LK_RETRY, vs);
1235 	return (error);
1236 }
1237 
1238 /*
1239  * These VP's should never see the light of day.  They should always
1240  * be covered.
1241  */
1242 static struct vop_vector zfsctl_ops_snapshot = {
1243 	.vop_default =		NULL, /* ensure very restricted access */
1244 	.vop_fplookup_vexec =	VOP_EAGAIN,
1245 	.vop_fplookup_symlink = VOP_EAGAIN,
1246 	.vop_open =		zfsctl_common_open,
1247 	.vop_close =		zfsctl_common_close,
1248 	.vop_inactive =		zfsctl_snapshot_inactive,
1249 	.vop_need_inactive =	vop_stdneed_inactive,
1250 	.vop_reclaim =		zfsctl_snapshot_reclaim,
1251 	.vop_vptocnp =		zfsctl_snapshot_vptocnp,
1252 	.vop_lock1 =		vop_stdlock,
1253 	.vop_unlock =		vop_stdunlock,
1254 	.vop_islocked =		vop_stdislocked,
1255 	.vop_advlockpurge =	vop_stdadvlockpurge, /* called by vgone */
1256 	.vop_print =		zfsctl_common_print,
1257 #if __FreeBSD_version >= 1400043
1258 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
1259 #endif
1260 };
1261 VFS_VOP_VECTOR_REGISTER(zfsctl_ops_snapshot);
1262 
1263 int
zfsctl_lookup_objset(vfs_t * vfsp,uint64_t objsetid,zfsvfs_t ** zfsvfsp)1264 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1265 {
1266 	zfsvfs_t *zfsvfs __unused = vfsp->vfs_data;
1267 	vnode_t *vp;
1268 	int error;
1269 
1270 	ASSERT3P(zfsvfs->z_ctldir, !=, NULL);
1271 	*zfsvfsp = NULL;
1272 	error = sfs_vnode_get(vfsp, LK_EXCLUSIVE,
1273 	    ZFSCTL_INO_SNAPDIR, objsetid, &vp);
1274 	if (error == 0 && vp != NULL) {
1275 		/*
1276 		 * XXX Probably need to at least reference, if not busy, the mp.
1277 		 */
1278 		if (vp->v_mountedhere != NULL)
1279 			*zfsvfsp = vp->v_mountedhere->mnt_data;
1280 		vput(vp);
1281 	}
1282 	if (*zfsvfsp == NULL)
1283 		return (SET_ERROR(EINVAL));
1284 	return (0);
1285 }
1286 
1287 /*
1288  * Unmount any snapshots for the given filesystem.  This is called from
1289  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1290  * snapshots.
1291  */
1292 int
zfsctl_umount_snapshots(vfs_t * vfsp,int fflags,cred_t * cr)1293 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1294 {
1295 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
1296 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1297 	struct mount *mp;
1298 	vnode_t *vp;
1299 	uint64_t cookie;
1300 	int error;
1301 
1302 	ASSERT3P(zfsvfs->z_ctldir, !=, NULL);
1303 
1304 	cookie = 0;
1305 	for (;;) {
1306 		uint64_t id;
1307 
1308 		dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1309 		error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof (snapname),
1310 		    snapname, &id, &cookie, NULL);
1311 		dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1312 		if (error != 0) {
1313 			if (error == ENOENT)
1314 				error = 0;
1315 			break;
1316 		}
1317 
1318 		for (;;) {
1319 			error = sfs_vnode_get(vfsp, LK_EXCLUSIVE,
1320 			    ZFSCTL_INO_SNAPDIR, id, &vp);
1321 			if (error != 0 || vp == NULL)
1322 				break;
1323 
1324 			mp = vp->v_mountedhere;
1325 
1326 			/*
1327 			 * v_mountedhere being NULL means that the
1328 			 * (uncovered) vnode is in a transient state
1329 			 * (mounting or unmounting), so loop until it
1330 			 * settles down.
1331 			 */
1332 			if (mp != NULL)
1333 				break;
1334 			vput(vp);
1335 		}
1336 		if (error != 0)
1337 			break;
1338 		if (vp == NULL)
1339 			continue;	/* no mountpoint, nothing to do */
1340 
1341 		/*
1342 		 * The mount-point vnode is kept locked to avoid spurious EBUSY
1343 		 * from a concurrent umount.
1344 		 * The vnode lock must have recursive locking enabled.
1345 		 */
1346 		vfs_ref(mp);
1347 		error = dounmount(mp, fflags, curthread);
1348 		KASSERT_IMPLY(error == 0, vrefcnt(vp) == 1,
1349 		    ("extra references after unmount"));
1350 		vput(vp);
1351 		if (error != 0)
1352 			break;
1353 	}
1354 	KASSERT_IMPLY((fflags & MS_FORCE) != 0, error == 0,
1355 	    ("force unmounting failed"));
1356 	return (error);
1357 }
1358 
1359 int
zfsctl_snapshot_unmount(const char * snapname,int flags __unused)1360 zfsctl_snapshot_unmount(const char *snapname, int flags __unused)
1361 {
1362 	vfs_t *vfsp = NULL;
1363 	zfsvfs_t *zfsvfs = NULL;
1364 
1365 	if (strchr(snapname, '@') == NULL)
1366 		return (0);
1367 
1368 	int err = getzfsvfs(snapname, &zfsvfs);
1369 	if (err != 0) {
1370 		ASSERT3P(zfsvfs, ==, NULL);
1371 		return (0);
1372 	}
1373 	vfsp = zfsvfs->z_vfs;
1374 
1375 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
1376 
1377 	vfs_ref(vfsp);
1378 	vfs_unbusy(vfsp);
1379 	return (dounmount(vfsp, MS_FORCE, curthread));
1380 }
1381