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