xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_ctldir.c (revision 72924ce99c7f177db486bd2764a76bcf25892a17)
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 	ASSERT0P(zfsvfs->z_ctldir);
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 = NODEV;
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 		if (eofp != NULL)
692 			*eofp = 1;
693 		return (0);
694 	}
695 
696 	error = sfs_readdir_common(zfsvfs->z_root, ZFSCTL_INO_ROOT, ap, &uio,
697 	    &dots_offset);
698 	if (error != 0) {
699 		if (error == ENAMETOOLONG) /* ran out of destination space */
700 			error = 0;
701 		return (error);
702 	}
703 	if (zfs_uio_offset(&uio) != dots_offset)
704 		return (SET_ERROR(EINVAL));
705 
706 	_Static_assert(sizeof (node->snapdir->sn_name) <= sizeof (entry.d_name),
707 	    "node->snapdir->sn_name too big for entry.d_name");
708 	entry.d_fileno = node->snapdir->sn_id;
709 	entry.d_type = DT_DIR;
710 	strcpy(entry.d_name, node->snapdir->sn_name);
711 	entry.d_namlen = strlen(entry.d_name);
712 	entry.d_reclen = sizeof (entry);
713 	error = vfs_read_dirent(ap, &entry, zfs_uio_offset(&uio));
714 	if (error != 0) {
715 		if (error == ENAMETOOLONG)
716 			error = 0;
717 		return (SET_ERROR(error));
718 	}
719 	if (eofp != NULL)
720 		*eofp = 1;
721 	return (0);
722 }
723 
724 static int
zfsctl_root_vptocnp(struct vop_vptocnp_args * ap)725 zfsctl_root_vptocnp(struct vop_vptocnp_args *ap)
726 {
727 	static const char dotzfs_name[4] = ".zfs";
728 	vnode_t *dvp;
729 	int error;
730 
731 	if (*ap->a_buflen < sizeof (dotzfs_name))
732 		return (SET_ERROR(ENOMEM));
733 
734 	error = vn_vget_ino_gen(ap->a_vp, zfsctl_fs_root_vnode, NULL,
735 	    LK_SHARED, &dvp);
736 	if (error != 0)
737 		return (SET_ERROR(error));
738 
739 	VOP_UNLOCK(dvp);
740 	*ap->a_vpp = dvp;
741 	*ap->a_buflen -= sizeof (dotzfs_name);
742 	memcpy(ap->a_buf + *ap->a_buflen, dotzfs_name, sizeof (dotzfs_name));
743 	return (0);
744 }
745 
746 static int
zfsctl_common_pathconf(struct vop_pathconf_args * ap)747 zfsctl_common_pathconf(struct vop_pathconf_args *ap)
748 {
749 	/*
750 	 * We care about ACL variables so that user land utilities like ls
751 	 * can display them correctly.  Since the ctldir's st_dev is set to be
752 	 * the same as the parent dataset, we must support all variables that
753 	 * it supports.
754 	 */
755 	switch (ap->a_name) {
756 	case _PC_LINK_MAX:
757 		*ap->a_retval = MIN(LONG_MAX, ZFS_LINK_MAX);
758 		return (0);
759 
760 	case _PC_FILESIZEBITS:
761 		*ap->a_retval = 64;
762 		return (0);
763 
764 	case _PC_MIN_HOLE_SIZE:
765 		*ap->a_retval = (int)SPA_MINBLOCKSIZE;
766 		return (0);
767 
768 	case _PC_ACL_EXTENDED:
769 		*ap->a_retval = 0;
770 		return (0);
771 
772 	case _PC_ACL_NFS4:
773 		*ap->a_retval = 1;
774 		return (0);
775 
776 	case _PC_ACL_PATH_MAX:
777 		*ap->a_retval = ACL_MAX_ENTRIES;
778 		return (0);
779 
780 	case _PC_NAME_MAX:
781 		*ap->a_retval = NAME_MAX;
782 		return (0);
783 
784 	default:
785 		return (vop_stdpathconf(ap));
786 	}
787 }
788 
789 /*
790  * Returns a trivial ACL
791  */
792 static int
zfsctl_common_getacl(struct vop_getacl_args * ap)793 zfsctl_common_getacl(struct vop_getacl_args *ap)
794 {
795 	int i;
796 
797 	if (ap->a_type != ACL_TYPE_NFS4)
798 		return (EINVAL);
799 
800 	acl_nfs4_sync_acl_from_mode(ap->a_aclp, zfsctl_ctldir_mode, 0);
801 	/*
802 	 * acl_nfs4_sync_acl_from_mode assumes that the owner can always modify
803 	 * attributes.  That is not the case for the ctldir, so we must clear
804 	 * those bits.  We also must clear ACL_READ_NAMED_ATTRS, because xattrs
805 	 * aren't supported by the ctldir.
806 	 */
807 	for (i = 0; i < ap->a_aclp->acl_cnt; i++) {
808 		struct acl_entry *entry;
809 		entry = &(ap->a_aclp->acl_entry[i]);
810 		entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER |
811 		    ACL_WRITE_ATTRIBUTES | ACL_WRITE_NAMED_ATTRS |
812 		    ACL_READ_NAMED_ATTRS);
813 	}
814 
815 	return (0);
816 }
817 
818 static struct vop_vector zfsctl_ops_root = {
819 	.vop_default =	&default_vnodeops,
820 	.vop_fplookup_vexec = VOP_EAGAIN,
821 	.vop_fplookup_symlink = VOP_EAGAIN,
822 	.vop_open =	zfsctl_common_open,
823 	.vop_close =	zfsctl_common_close,
824 	.vop_ioctl =	VOP_EINVAL,
825 	.vop_getattr =	zfsctl_root_getattr,
826 	.vop_access =	zfsctl_common_access,
827 	.vop_readdir =	zfsctl_root_readdir,
828 	.vop_lookup =	zfsctl_root_lookup,
829 	.vop_inactive =	VOP_NULL,
830 	.vop_reclaim =	zfsctl_common_reclaim,
831 	.vop_fid =	zfsctl_common_fid,
832 	.vop_print =	zfsctl_common_print,
833 	.vop_vptocnp =	zfsctl_root_vptocnp,
834 	.vop_pathconf =	zfsctl_common_pathconf,
835 	.vop_getacl =	zfsctl_common_getacl,
836 #if __FreeBSD_version >= 1400043
837 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
838 #endif
839 };
840 VFS_VOP_VECTOR_REGISTER(zfsctl_ops_root);
841 
842 static int
zfsctl_snapshot_zname(vnode_t * vp,const char * name,int len,char * zname)843 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
844 {
845 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
846 
847 	dmu_objset_name(os, zname);
848 	if (strlen(zname) + 1 + strlen(name) >= len)
849 		return (SET_ERROR(ENAMETOOLONG));
850 	(void) strcat(zname, "@");
851 	(void) strcat(zname, name);
852 	return (0);
853 }
854 
855 static int
zfsctl_snapshot_lookup(vnode_t * vp,const char * name,uint64_t * id)856 zfsctl_snapshot_lookup(vnode_t *vp, const char *name, uint64_t *id)
857 {
858 	objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
859 	int err;
860 
861 	err = dsl_dataset_snap_lookup(dmu_objset_ds(os), name, id);
862 	return (err);
863 }
864 
865 /*
866  * Given a vnode get a root vnode of a filesystem mounted on top of
867  * the vnode, if any.  The root vnode is referenced and locked.
868  * If no filesystem is mounted then the orinal vnode remains referenced
869  * and locked.  If any error happens the orinal vnode is unlocked and
870  * released.
871  */
872 static int
zfsctl_mounted_here(vnode_t ** vpp,int flags)873 zfsctl_mounted_here(vnode_t **vpp, int flags)
874 {
875 	struct mount *mp;
876 	int err;
877 
878 	ASSERT_VOP_LOCKED(*vpp, __func__);
879 	ASSERT3S((*vpp)->v_type, ==, VDIR);
880 
881 	if ((mp = (*vpp)->v_mountedhere) != NULL) {
882 		err = vfs_busy(mp, 0);
883 		KASSERT(err == 0, ("vfs_busy(mp, 0) failed with %d", err));
884 		KASSERT(vrefcnt(*vpp) > 1, ("unreferenced mountpoint"));
885 		vput(*vpp);
886 		err = VFS_ROOT(mp, flags, vpp);
887 		vfs_unbusy(mp);
888 		return (err);
889 	}
890 	return (EJUSTRETURN);
891 }
892 
893 typedef struct {
894 	const char *snap_name;
895 	uint64_t    snap_id;
896 } snapshot_setup_arg_t;
897 
898 static void
zfsctl_snapshot_vnode_setup(vnode_t * vp,void * arg)899 zfsctl_snapshot_vnode_setup(vnode_t *vp, void *arg)
900 {
901 	snapshot_setup_arg_t *ssa = arg;
902 	sfs_node_t *node;
903 
904 	ASSERT_VOP_ELOCKED(vp, __func__);
905 
906 	node = sfs_alloc_node(sizeof (sfs_node_t),
907 	    ssa->snap_name, ZFSCTL_INO_SNAPDIR, ssa->snap_id);
908 	zfsctl_common_vnode_setup(vp, node);
909 
910 	/* We have to support recursive locking. */
911 	VN_LOCK_AREC(vp);
912 }
913 
914 /*
915  * Lookup entry point for the 'snapshot' directory.  Try to open the
916  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
917  * Perform a mount of the associated dataset on top of the vnode.
918  * There are four possibilities:
919  * - the snapshot node and vnode do not exist
920  * - the snapshot vnode is covered by the mounted snapshot
921  * - the snapshot vnode is not covered yet, the mount operation is in progress
922  * - the snapshot vnode is not covered, because the snapshot has been unmounted
923  * The last two states are transient and should be relatively short-lived.
924  */
925 static int
zfsctl_snapdir_lookup(struct vop_lookup_args * ap)926 zfsctl_snapdir_lookup(struct vop_lookup_args *ap)
927 {
928 	vnode_t *dvp = ap->a_dvp;
929 	vnode_t **vpp = ap->a_vpp;
930 	struct componentname *cnp = ap->a_cnp;
931 	char name[NAME_MAX + 1];
932 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
933 	char *mountpoint;
934 	size_t mountpoint_len;
935 	zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
936 	uint64_t snap_id;
937 	int nameiop = cnp->cn_nameiop;
938 	int lkflags = cnp->cn_lkflags;
939 	int flags = cnp->cn_flags;
940 	int err;
941 
942 	ASSERT3S(dvp->v_type, ==, VDIR);
943 
944 	if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP)
945 		return (SET_ERROR(ENOTSUP));
946 
947 	if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
948 		err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK);
949 		if (err == 0)
950 			*vpp = dvp;
951 		return (err);
952 	}
953 	if (flags & ISDOTDOT) {
954 		err = vn_vget_ino_gen(dvp, zfsctl_root_vnode, NULL, lkflags,
955 		    vpp);
956 		return (err);
957 	}
958 
959 	if (cnp->cn_namelen >= sizeof (name))
960 		return (SET_ERROR(ENAMETOOLONG));
961 
962 	strlcpy(name, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
963 	err = zfsctl_snapshot_lookup(dvp, name, &snap_id);
964 	if (err != 0)
965 		return (SET_ERROR(ENOENT));
966 
967 	for (;;) {
968 		snapshot_setup_arg_t ssa;
969 
970 		ssa.snap_name = name;
971 		ssa.snap_id = snap_id;
972 		err = sfs_vgetx(dvp->v_mount, LK_SHARED, ZFSCTL_INO_SNAPDIR,
973 		    snap_id, "zfs", &zfsctl_ops_snapshot,
974 		    zfsctl_snapshot_vnode_setup, &ssa, vpp);
975 		if (err != 0)
976 			return (err);
977 
978 		/* Check if a new vnode has just been created. */
979 		if (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE)
980 			break;
981 
982 		/*
983 		 * Check if a snapshot is already mounted on top of the vnode.
984 		 */
985 		err = zfsctl_mounted_here(vpp, lkflags);
986 		if (err != EJUSTRETURN)
987 			return (err);
988 
989 		/*
990 		 * If the vnode is not covered, then either the mount operation
991 		 * is in progress or the snapshot has already been unmounted
992 		 * but the vnode hasn't been inactivated and reclaimed yet.
993 		 * We can try to re-use the vnode in the latter case.
994 		 */
995 		VI_LOCK(*vpp);
996 		if (((*vpp)->v_iflag & VI_MOUNT) == 0) {
997 			VI_UNLOCK(*vpp);
998 			/*
999 			 * Upgrade to exclusive lock in order to:
1000 			 * - avoid race conditions
1001 			 * - satisfy the contract of mount_snapshot()
1002 			 */
1003 			err = VOP_LOCK(*vpp, LK_TRYUPGRADE);
1004 			if (err == 0)
1005 				break;
1006 		} else {
1007 			VI_UNLOCK(*vpp);
1008 		}
1009 
1010 		/*
1011 		 * In this state we can loop on uncontested locks and starve
1012 		 * the thread doing the lengthy, non-trivial mount operation.
1013 		 * So, yield to prevent that from happening.
1014 		 */
1015 		vput(*vpp);
1016 		kern_yield(PRI_USER);
1017 	}
1018 
1019 	VERIFY0(zfsctl_snapshot_zname(dvp, name, sizeof (fullname), fullname));
1020 
1021 	mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1022 	    strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(name) + 1;
1023 	mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1024 	(void) snprintf(mountpoint, mountpoint_len,
1025 	    "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1026 	    dvp->v_vfsp->mnt_stat.f_mntonname, name);
1027 
1028 	err = mount_snapshot(curthread, vpp, "zfs", mountpoint, fullname, 0,
1029 	    dvp->v_vfsp);
1030 	kmem_free(mountpoint, mountpoint_len);
1031 	if (err == 0) {
1032 		/*
1033 		 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1034 		 *
1035 		 * This is where we lie about our v_vfsp in order to
1036 		 * make .zfs/snapshot/<snapname> accessible over NFS
1037 		 * without requiring manual mounts of <snapname>.
1038 		 */
1039 		ASSERT3P(VTOZ(*vpp)->z_zfsvfs, !=, zfsvfs);
1040 		VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1041 
1042 		/* Clear the root flag (set via VFS_ROOT) as well. */
1043 		(*vpp)->v_vflag &= ~VV_ROOT;
1044 	}
1045 
1046 	if (err != 0)
1047 		*vpp = NULL;
1048 	return (err);
1049 }
1050 
1051 static int
zfsctl_snapdir_readdir(struct vop_readdir_args * ap)1052 zfsctl_snapdir_readdir(struct vop_readdir_args *ap)
1053 {
1054 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
1055 	struct dirent entry;
1056 	vnode_t *vp = ap->a_vp;
1057 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1058 	zfs_uio_t uio;
1059 	int *eofp = ap->a_eofflag;
1060 	off_t dots_offset;
1061 	int error;
1062 
1063 	zfs_uio_init(&uio, ap->a_uio);
1064 
1065 	ASSERT3S(vp->v_type, ==, VDIR);
1066 
1067 	error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap,
1068 	    &uio, &dots_offset);
1069 	if (error != 0) {
1070 		if (error == ENAMETOOLONG) /* ran out of destination space */
1071 			error = 0;
1072 		return (error);
1073 	}
1074 
1075 	if ((error = zfs_enter(zfsvfs, FTAG)) != 0)
1076 		return (error);
1077 	for (;;) {
1078 		uint64_t cookie;
1079 		uint64_t id;
1080 
1081 		cookie = zfs_uio_offset(&uio) - dots_offset;
1082 
1083 		dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1084 		error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof (snapname),
1085 		    snapname, &id, &cookie, NULL);
1086 		dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1087 		if (error != 0) {
1088 			if (error == ENOENT) {
1089 				if (eofp != NULL)
1090 					*eofp = 1;
1091 				error = 0;
1092 			}
1093 			zfs_exit(zfsvfs, FTAG);
1094 			return (error);
1095 		}
1096 
1097 		entry.d_fileno = id;
1098 		entry.d_type = DT_DIR;
1099 		strcpy(entry.d_name, snapname);
1100 		entry.d_namlen = strlen(entry.d_name);
1101 		entry.d_reclen = sizeof (entry);
1102 		error = vfs_read_dirent(ap, &entry, zfs_uio_offset(&uio));
1103 		if (error != 0) {
1104 			if (error == ENAMETOOLONG)
1105 				error = 0;
1106 			zfs_exit(zfsvfs, FTAG);
1107 			return (SET_ERROR(error));
1108 		}
1109 		zfs_uio_setoffset(&uio, cookie + dots_offset);
1110 	}
1111 	__builtin_unreachable();
1112 }
1113 
1114 static int
zfsctl_snapdir_getattr(struct vop_getattr_args * ap)1115 zfsctl_snapdir_getattr(struct vop_getattr_args *ap)
1116 {
1117 	vnode_t *vp = ap->a_vp;
1118 	vattr_t *vap = ap->a_vap;
1119 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1120 	dsl_dataset_t *ds;
1121 	uint64_t snap_count;
1122 	int err;
1123 
1124 	if ((err = zfs_enter(zfsvfs, FTAG)) != 0)
1125 		return (err);
1126 	ds = dmu_objset_ds(zfsvfs->z_os);
1127 	zfsctl_common_getattr(vp, vap);
1128 	vap->va_ctime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1129 	vap->va_mtime = vap->va_ctime;
1130 	vap->va_birthtime = vap->va_ctime;
1131 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
1132 		err = zap_count(dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
1133 		    dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
1134 		if (err != 0) {
1135 			zfs_exit(zfsvfs, FTAG);
1136 			return (err);
1137 		}
1138 		vap->va_nlink += snap_count;
1139 	}
1140 	vap->va_size = vap->va_nlink;
1141 
1142 	zfs_exit(zfsvfs, FTAG);
1143 	return (0);
1144 }
1145 
1146 static struct vop_vector zfsctl_ops_snapdir = {
1147 	.vop_default =	&default_vnodeops,
1148 	.vop_fplookup_vexec = VOP_EAGAIN,
1149 	.vop_fplookup_symlink = VOP_EAGAIN,
1150 	.vop_open =	zfsctl_common_open,
1151 	.vop_close =	zfsctl_common_close,
1152 	.vop_getattr =	zfsctl_snapdir_getattr,
1153 	.vop_access =	zfsctl_common_access,
1154 	.vop_readdir =	zfsctl_snapdir_readdir,
1155 	.vop_lookup =	zfsctl_snapdir_lookup,
1156 	.vop_reclaim =	zfsctl_common_reclaim,
1157 	.vop_fid =	zfsctl_common_fid,
1158 	.vop_print =	zfsctl_common_print,
1159 	.vop_pathconf =	zfsctl_common_pathconf,
1160 	.vop_getacl =	zfsctl_common_getacl,
1161 #if __FreeBSD_version >= 1400043
1162 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
1163 #endif
1164 };
1165 VFS_VOP_VECTOR_REGISTER(zfsctl_ops_snapdir);
1166 
1167 
1168 static int
zfsctl_snapshot_inactive(struct vop_inactive_args * ap)1169 zfsctl_snapshot_inactive(struct vop_inactive_args *ap)
1170 {
1171 	vnode_t *vp = ap->a_vp;
1172 
1173 	vrecycle(vp);
1174 	return (0);
1175 }
1176 
1177 static int
zfsctl_snapshot_reclaim(struct vop_reclaim_args * ap)1178 zfsctl_snapshot_reclaim(struct vop_reclaim_args *ap)
1179 {
1180 	vnode_t *vp = ap->a_vp;
1181 	void *data = vp->v_data;
1182 
1183 	sfs_reclaim_vnode(vp);
1184 	sfs_destroy_node(data);
1185 	return (0);
1186 }
1187 
1188 static int
zfsctl_snapshot_vptocnp(struct vop_vptocnp_args * ap)1189 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1190 {
1191 	struct mount *mp;
1192 	vnode_t *dvp;
1193 	vnode_t *vp;
1194 	sfs_node_t *node;
1195 	size_t len;
1196 	int locked;
1197 	int error;
1198 
1199 	vp = ap->a_vp;
1200 	node = vp->v_data;
1201 	len = strlen(node->sn_name);
1202 	if (*ap->a_buflen < len)
1203 		return (SET_ERROR(ENOMEM));
1204 
1205 	/*
1206 	 * Prevent unmounting of the snapshot while the vnode lock
1207 	 * is not held.  That is not strictly required, but allows
1208 	 * us to assert that an uncovered snapshot vnode is never
1209 	 * "leaked".
1210 	 */
1211 	mp = vp->v_mountedhere;
1212 	if (mp == NULL)
1213 		return (SET_ERROR(ENOENT));
1214 	error = vfs_busy(mp, 0);
1215 	KASSERT(error == 0, ("vfs_busy(mp, 0) failed with %d", error));
1216 
1217 	/*
1218 	 * We can vput the vnode as we can now depend on the reference owned
1219 	 * by the busied mp.  But we also need to hold the vnode, because
1220 	 * the reference may go after vfs_unbusy() which has to be called
1221 	 * before we can lock the vnode again.
1222 	 */
1223 	locked = VOP_ISLOCKED(vp);
1224 	enum vgetstate vs = vget_prep(vp);
1225 	vput(vp);
1226 
1227 	/* Look up .zfs/snapshot, our parent. */
1228 	error = zfsctl_snapdir_vnode(vp->v_mount, NULL, LK_SHARED, &dvp);
1229 	if (error == 0) {
1230 		VOP_UNLOCK(dvp);
1231 		*ap->a_vpp = dvp;
1232 		*ap->a_buflen -= len;
1233 		memcpy(ap->a_buf + *ap->a_buflen, node->sn_name, len);
1234 	}
1235 	vfs_unbusy(mp);
1236 	vget_finish(vp, locked | LK_RETRY, vs);
1237 	return (error);
1238 }
1239 
1240 /*
1241  * These VP's should never see the light of day.  They should always
1242  * be covered.
1243  */
1244 static struct vop_vector zfsctl_ops_snapshot = {
1245 	.vop_default =		NULL, /* ensure very restricted access */
1246 	.vop_fplookup_vexec =	VOP_EAGAIN,
1247 	.vop_fplookup_symlink = VOP_EAGAIN,
1248 	.vop_open =		zfsctl_common_open,
1249 	.vop_close =		zfsctl_common_close,
1250 	.vop_inactive =		zfsctl_snapshot_inactive,
1251 	.vop_need_inactive =	vop_stdneed_inactive,
1252 	.vop_reclaim =		zfsctl_snapshot_reclaim,
1253 	.vop_vptocnp =		zfsctl_snapshot_vptocnp,
1254 	.vop_lock1 =		vop_stdlock,
1255 	.vop_unlock =		vop_stdunlock,
1256 	.vop_islocked =		vop_stdislocked,
1257 	.vop_advlockpurge =	vop_stdadvlockpurge, /* called by vgone */
1258 	.vop_print =		zfsctl_common_print,
1259 #if __FreeBSD_version >= 1400043
1260 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
1261 #endif
1262 };
1263 VFS_VOP_VECTOR_REGISTER(zfsctl_ops_snapshot);
1264 
1265 int
zfsctl_lookup_objset(vfs_t * vfsp,uint64_t objsetid,zfsvfs_t ** zfsvfsp)1266 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1267 {
1268 	zfsvfs_t *zfsvfs __unused = vfsp->vfs_data;
1269 	vnode_t *vp;
1270 	int error;
1271 
1272 	ASSERT3P(zfsvfs->z_ctldir, !=, NULL);
1273 	*zfsvfsp = NULL;
1274 	error = sfs_vnode_get(vfsp, LK_EXCLUSIVE,
1275 	    ZFSCTL_INO_SNAPDIR, objsetid, &vp);
1276 	if (error == 0 && vp != NULL) {
1277 		/*
1278 		 * XXX Probably need to at least reference, if not busy, the mp.
1279 		 */
1280 		if (vp->v_mountedhere != NULL)
1281 			*zfsvfsp = vp->v_mountedhere->mnt_data;
1282 		vput(vp);
1283 	}
1284 	if (*zfsvfsp == NULL)
1285 		return (SET_ERROR(EINVAL));
1286 	return (0);
1287 }
1288 
1289 /*
1290  * Unmount any snapshots for the given filesystem.  This is called from
1291  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1292  * snapshots.
1293  */
1294 int
zfsctl_umount_snapshots(vfs_t * vfsp,int fflags,cred_t * cr)1295 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1296 {
1297 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
1298 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1299 	struct mount *mp;
1300 	vnode_t *vp;
1301 	uint64_t cookie;
1302 	int error;
1303 
1304 	ASSERT3P(zfsvfs->z_ctldir, !=, NULL);
1305 
1306 	cookie = 0;
1307 	for (;;) {
1308 		uint64_t id;
1309 
1310 		dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1311 		error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof (snapname),
1312 		    snapname, &id, &cookie, NULL);
1313 		dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1314 		if (error != 0) {
1315 			if (error == ENOENT)
1316 				error = 0;
1317 			break;
1318 		}
1319 
1320 		for (;;) {
1321 			error = sfs_vnode_get(vfsp, LK_EXCLUSIVE,
1322 			    ZFSCTL_INO_SNAPDIR, id, &vp);
1323 			if (error != 0 || vp == NULL)
1324 				break;
1325 
1326 			mp = vp->v_mountedhere;
1327 
1328 			/*
1329 			 * v_mountedhere being NULL means that the
1330 			 * (uncovered) vnode is in a transient state
1331 			 * (mounting or unmounting), so loop until it
1332 			 * settles down.
1333 			 */
1334 			if (mp != NULL)
1335 				break;
1336 			vput(vp);
1337 		}
1338 		if (error != 0)
1339 			break;
1340 		if (vp == NULL)
1341 			continue;	/* no mountpoint, nothing to do */
1342 
1343 		/*
1344 		 * The mount-point vnode is kept locked to avoid spurious EBUSY
1345 		 * from a concurrent umount.
1346 		 * The vnode lock must have recursive locking enabled.
1347 		 */
1348 		vfs_ref(mp);
1349 		error = dounmount(mp, fflags, curthread);
1350 		KASSERT_IMPLY(error == 0, vrefcnt(vp) == 1,
1351 		    ("extra references after unmount"));
1352 		vput(vp);
1353 		if (error != 0)
1354 			break;
1355 	}
1356 	KASSERT_IMPLY((fflags & MS_FORCE) != 0, error == 0,
1357 	    ("force unmounting failed"));
1358 	return (error);
1359 }
1360 
1361 int
zfsctl_snapshot_unmount(const char * snapname,int flags __unused)1362 zfsctl_snapshot_unmount(const char *snapname, int flags __unused)
1363 {
1364 	vfs_t *vfsp = NULL;
1365 	zfsvfs_t *zfsvfs = NULL;
1366 
1367 	if (strchr(snapname, '@') == NULL)
1368 		return (0);
1369 
1370 	int err = getzfsvfs(snapname, &zfsvfs);
1371 	if (err != 0) {
1372 		ASSERT0P(zfsvfs);
1373 		return (0);
1374 	}
1375 	vfsp = zfsvfs->z_vfs;
1376 
1377 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
1378 
1379 	vfs_ref(vfsp);
1380 	vfs_unbusy(vfsp);
1381 	return (dounmount(vfsp, MS_FORCE, curthread));
1382 }
1383