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