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