]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ctldir.c
Documentation updates
[mirror_zfs.git] / module / zfs / zfs_ctldir.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 /*
26 * ZFS control directory (a.k.a. ".zfs")
27 *
28 * This directory provides a common location for all ZFS meta-objects.
29 * Currently, this is only the 'snapshot' directory, but this may expand in the
30 * future. The elements are built using the GFS primitives, as the hierarchy
31 * does not actually exist on disk.
32 *
33 * For 'snapshot', we don't want to have all snapshots always mounted, because
34 * this would take up a huge amount of space in /etc/mtab. We have three
35 * types of objects:
36 *
37 * ctldir ------> snapshotdir -------> snapshot
38 * |
39 * |
40 * V
41 * mounted fs
42 *
43 * The 'snapshot' node contains just enough information to lookup '..' and act
44 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
45 * perform an automount of the underlying filesystem and return the
46 * corresponding vnode.
47 *
48 * All mounts are handled automatically by the kernel, but unmounts are
49 * (currently) handled from user land. The main reason is that there is no
50 * reliable way to auto-unmount the filesystem when it's "no longer in use".
51 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
52 * unmounts any snapshots within the snapshot directory.
53 *
54 * The '.zfs', '.zfs/snapshot', and all directories created under
55 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
56 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
57 *
58 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
59 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
60 * However, vnodes within these mounted on file systems have their v_vfsp
61 * fields set to the head filesystem to make NFS happy (see
62 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
63 * so that it cannot be freed until all snapshots have been unmounted.
64 */
65
66 #ifdef HAVE_ZPL
67
68 #include <fs/fs_subr.h>
69 #include <sys/zfs_ctldir.h>
70 #include <sys/zfs_ioctl.h>
71 #include <sys/zfs_vfsops.h>
72 #include <sys/vfs_opreg.h>
73 #include <sys/gfs.h>
74 #include <sys/stat.h>
75 #include <sys/dmu.h>
76 #include <sys/dsl_deleg.h>
77 #include <sys/mount.h>
78 #include <sys/sunddi.h>
79
80 #include "zfs_namecheck.h"
81
82 typedef struct zfsctl_node {
83 gfs_dir_t zc_gfs_private;
84 uint64_t zc_id;
85 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
86 } zfsctl_node_t;
87
88 typedef struct zfsctl_snapdir {
89 zfsctl_node_t sd_node;
90 kmutex_t sd_lock;
91 avl_tree_t sd_snaps;
92 } zfsctl_snapdir_t;
93
94 typedef struct {
95 char *se_name;
96 vnode_t *se_root;
97 avl_node_t se_node;
98 } zfs_snapentry_t;
99
100 static int
101 snapentry_compare(const void *a, const void *b)
102 {
103 const zfs_snapentry_t *sa = a;
104 const zfs_snapentry_t *sb = b;
105 int ret = strcmp(sa->se_name, sb->se_name);
106
107 if (ret < 0)
108 return (-1);
109 else if (ret > 0)
110 return (1);
111 else
112 return (0);
113 }
114
115 vnodeops_t *zfsctl_ops_root;
116 vnodeops_t *zfsctl_ops_snapdir;
117 vnodeops_t *zfsctl_ops_snapshot;
118 vnodeops_t *zfsctl_ops_shares;
119 vnodeops_t *zfsctl_ops_shares_dir;
120
121 static const fs_operation_def_t zfsctl_tops_root[];
122 static const fs_operation_def_t zfsctl_tops_snapdir[];
123 static const fs_operation_def_t zfsctl_tops_snapshot[];
124 static const fs_operation_def_t zfsctl_tops_shares[];
125
126 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
127 static vnode_t *zfsctl_mknode_shares(vnode_t *);
128 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
129 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
130
131 static gfs_opsvec_t zfsctl_opsvec[] = {
132 { ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
133 { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
134 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
135 { ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
136 { ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
137 { NULL }
138 };
139
140 /*
141 * Root directory elements. We only have two entries
142 * snapshot and shares.
143 */
144 static gfs_dirent_t zfsctl_root_entries[] = {
145 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
146 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
147 { NULL }
148 };
149
150 /* include . and .. in the calculation */
151 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
152 sizeof (gfs_dirent_t)) + 1)
153
154
155 /*
156 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
157 * directories. This is called from the ZFS init routine, and initializes the
158 * vnode ops vectors that we'll be using.
159 */
160 void
161 zfsctl_init(void)
162 {
163 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
164 }
165
166 void
167 zfsctl_fini(void)
168 {
169 /*
170 * Remove vfsctl vnode ops
171 */
172 if (zfsctl_ops_root)
173 vn_freevnodeops(zfsctl_ops_root);
174 if (zfsctl_ops_snapdir)
175 vn_freevnodeops(zfsctl_ops_snapdir);
176 if (zfsctl_ops_snapshot)
177 vn_freevnodeops(zfsctl_ops_snapshot);
178 if (zfsctl_ops_shares)
179 vn_freevnodeops(zfsctl_ops_shares);
180 if (zfsctl_ops_shares_dir)
181 vn_freevnodeops(zfsctl_ops_shares_dir);
182
183 zfsctl_ops_root = NULL;
184 zfsctl_ops_snapdir = NULL;
185 zfsctl_ops_snapshot = NULL;
186 zfsctl_ops_shares = NULL;
187 zfsctl_ops_shares_dir = NULL;
188 }
189
190 boolean_t
191 zfsctl_is_node(vnode_t *vp)
192 {
193 return (vn_matchops(vp, zfsctl_ops_root) ||
194 vn_matchops(vp, zfsctl_ops_snapdir) ||
195 vn_matchops(vp, zfsctl_ops_snapshot) ||
196 vn_matchops(vp, zfsctl_ops_shares) ||
197 vn_matchops(vp, zfsctl_ops_shares_dir));
198
199 }
200
201 /*
202 * Return the inode number associated with the 'snapshot' or
203 * 'shares' directory.
204 */
205 /* ARGSUSED */
206 static ino64_t
207 zfsctl_root_inode_cb(vnode_t *vp, int index)
208 {
209 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
210
211 ASSERT(index <= 2);
212
213 if (index == 0)
214 return (ZFSCTL_INO_SNAPDIR);
215
216 return (zfsvfs->z_shares_dir);
217 }
218
219 /*
220 * Create the '.zfs' directory. This directory is cached as part of the VFS
221 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
222 * therefore checks against a vfs_count of 2 instead of 1. This reference
223 * is removed when the ctldir is destroyed in the unmount.
224 */
225 void
226 zfsctl_create(zfsvfs_t *zfsvfs)
227 {
228 vnode_t *vp, *rvp;
229 zfsctl_node_t *zcp;
230 uint64_t crtime[2];
231
232 ASSERT(zfsvfs->z_ctldir == NULL);
233
234 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
235 zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
236 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
237 zcp = vp->v_data;
238 zcp->zc_id = ZFSCTL_INO_ROOT;
239
240 VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0);
241 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
242 &crtime, sizeof (crtime)));
243 ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
244 VN_RELE(rvp);
245
246 /*
247 * We're only faking the fact that we have a root of a filesystem for
248 * the sake of the GFS interfaces. Undo the flag manipulation it did
249 * for us.
250 */
251 vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
252
253 zfsvfs->z_ctldir = vp;
254 }
255
256 /*
257 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
258 * There might still be more references if we were force unmounted, but only
259 * new zfs_inactive() calls can occur and they don't reference .zfs
260 */
261 void
262 zfsctl_destroy(zfsvfs_t *zfsvfs)
263 {
264 VN_RELE(zfsvfs->z_ctldir);
265 zfsvfs->z_ctldir = NULL;
266 }
267
268 /*
269 * Given a root znode, retrieve the associated .zfs directory.
270 * Add a hold to the vnode and return it.
271 */
272 vnode_t *
273 zfsctl_root(znode_t *zp)
274 {
275 ASSERT(zfs_has_ctldir(zp));
276 VN_HOLD(zp->z_zfsvfs->z_ctldir);
277 return (zp->z_zfsvfs->z_ctldir);
278 }
279
280 /*
281 * Common open routine. Disallow any write access.
282 */
283 /* ARGSUSED */
284 static int
285 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
286 {
287 if (flags & FWRITE)
288 return (EACCES);
289
290 return (0);
291 }
292
293 /*
294 * Common close routine. Nothing to do here.
295 */
296 /* ARGSUSED */
297 static int
298 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
299 cred_t *cr, caller_context_t *ct)
300 {
301 return (0);
302 }
303
304 /*
305 * Common access routine. Disallow writes.
306 */
307 /* ARGSUSED */
308 static int
309 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr,
310 caller_context_t *ct)
311 {
312 if (flags & V_ACE_MASK) {
313 if (mode & ACE_ALL_WRITE_PERMS)
314 return (EACCES);
315 } else {
316 if (mode & VWRITE)
317 return (EACCES);
318 }
319
320 return (0);
321 }
322
323 /*
324 * Common getattr function. Fill in basic information.
325 */
326 static void
327 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
328 {
329 timestruc_t now;
330
331 vap->va_uid = 0;
332 vap->va_gid = 0;
333 vap->va_rdev = 0;
334 /*
335 * We are a purely virtual object, so we have no
336 * blocksize or allocated blocks.
337 */
338 vap->va_blksize = 0;
339 vap->va_nblocks = 0;
340 vap->va_seq = 0;
341 vap->va_fsid = vp->v_vfsp->vfs_dev;
342 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
343 S_IROTH | S_IXOTH;
344 vap->va_type = VDIR;
345 /*
346 * We live in the now (for atime).
347 */
348 gethrestime(&now);
349 vap->va_atime = now;
350 }
351
352 /*ARGSUSED*/
353 static int
354 zfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
355 {
356 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
357 zfsctl_node_t *zcp = vp->v_data;
358 uint64_t object = zcp->zc_id;
359 zfid_short_t *zfid;
360 int i;
361
362 ZFS_ENTER(zfsvfs);
363
364 if (fidp->fid_len < SHORT_FID_LEN) {
365 fidp->fid_len = SHORT_FID_LEN;
366 ZFS_EXIT(zfsvfs);
367 return (ENOSPC);
368 }
369
370 zfid = (zfid_short_t *)fidp;
371
372 zfid->zf_len = SHORT_FID_LEN;
373
374 for (i = 0; i < sizeof (zfid->zf_object); i++)
375 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
376
377 /* .zfs znodes always have a generation number of 0 */
378 for (i = 0; i < sizeof (zfid->zf_gen); i++)
379 zfid->zf_gen[i] = 0;
380
381 ZFS_EXIT(zfsvfs);
382 return (0);
383 }
384
385
386 /*ARGSUSED*/
387 static int
388 zfsctl_shares_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
389 {
390 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
391 znode_t *dzp;
392 int error;
393
394 ZFS_ENTER(zfsvfs);
395
396 if (zfsvfs->z_shares_dir == 0) {
397 ZFS_EXIT(zfsvfs);
398 return (ENOTSUP);
399 }
400
401 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
402 error = VOP_FID(ZTOV(dzp), fidp, ct);
403 VN_RELE(ZTOV(dzp));
404 }
405
406 ZFS_EXIT(zfsvfs);
407 return (error);
408 }
409 /*
410 * .zfs inode namespace
411 *
412 * We need to generate unique inode numbers for all files and directories
413 * within the .zfs pseudo-filesystem. We use the following scheme:
414 *
415 * ENTRY ZFSCTL_INODE
416 * .zfs 1
417 * .zfs/snapshot 2
418 * .zfs/snapshot/<snap> objectid(snap)
419 */
420
421 #define ZFSCTL_INO_SNAP(id) (id)
422
423 /*
424 * Get root directory attributes.
425 */
426 /* ARGSUSED */
427 static int
428 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
429 caller_context_t *ct)
430 {
431 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
432 zfsctl_node_t *zcp = vp->v_data;
433
434 ZFS_ENTER(zfsvfs);
435 vap->va_nodeid = ZFSCTL_INO_ROOT;
436 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
437 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
438
439 zfsctl_common_getattr(vp, vap);
440 ZFS_EXIT(zfsvfs);
441
442 return (0);
443 }
444
445 /*
446 * Special case the handling of "..".
447 */
448 /* ARGSUSED */
449 int
450 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
451 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
452 int *direntflags, pathname_t *realpnp)
453 {
454 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
455 int err;
456
457 /*
458 * No extended attributes allowed under .zfs
459 */
460 if (flags & LOOKUP_XATTR)
461 return (EINVAL);
462
463 ZFS_ENTER(zfsvfs);
464
465 if (strcmp(nm, "..") == 0) {
466 err = VFS_ROOT(dvp->v_vfsp, vpp);
467 } else {
468 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
469 cr, ct, direntflags, realpnp);
470 }
471
472 ZFS_EXIT(zfsvfs);
473
474 return (err);
475 }
476
477 static int
478 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
479 caller_context_t *ct)
480 {
481 /*
482 * We only care about ACL_ENABLED so that libsec can
483 * display ACL correctly and not default to POSIX draft.
484 */
485 if (cmd == _PC_ACL_ENABLED) {
486 *valp = _ACL_ACE_ENABLED;
487 return (0);
488 }
489
490 return (fs_pathconf(vp, cmd, valp, cr, ct));
491 }
492
493 static const fs_operation_def_t zfsctl_tops_root[] = {
494 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
495 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
496 { VOPNAME_IOCTL, { .error = fs_inval } },
497 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } },
498 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
499 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
500 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } },
501 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
502 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
503 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } },
504 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
505 { NULL }
506 };
507
508 static int
509 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
510 {
511 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
512
513 if (snapshot_namecheck(name, NULL, NULL) != 0)
514 return (EILSEQ);
515 dmu_objset_name(os, zname);
516 if (strlen(zname) + 1 + strlen(name) >= len)
517 return (ENAMETOOLONG);
518 (void) strcat(zname, "@");
519 (void) strcat(zname, name);
520 return (0);
521 }
522
523 static int
524 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
525 {
526 vnode_t *svp = sep->se_root;
527 int error;
528
529 ASSERT(vn_ismntpt(svp));
530
531 /* this will be dropped by dounmount() */
532 if ((error = vn_vfswlock(svp)) != 0)
533 return (error);
534
535 VN_HOLD(svp);
536 error = dounmount(vn_mountedvfs(svp), fflags, cr);
537 if (error) {
538 VN_RELE(svp);
539 return (error);
540 }
541
542 /*
543 * We can't use VN_RELE(), as that will try to invoke
544 * zfsctl_snapdir_inactive(), which would cause us to destroy
545 * the sd_lock mutex held by our caller.
546 */
547 ASSERT(svp->v_count == 1);
548 gfs_vop_inactive(svp, cr, NULL);
549
550 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
551 kmem_free(sep, sizeof (zfs_snapentry_t));
552
553 return (0);
554 }
555
556 static void
557 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
558 {
559 avl_index_t where;
560 vfs_t *vfsp;
561 refstr_t *pathref;
562 char newpath[MAXNAMELEN];
563 char *tail;
564
565 ASSERT(MUTEX_HELD(&sdp->sd_lock));
566 ASSERT(sep != NULL);
567
568 vfsp = vn_mountedvfs(sep->se_root);
569 ASSERT(vfsp != NULL);
570
571 vfs_lock_wait(vfsp);
572
573 /*
574 * Change the name in the AVL tree.
575 */
576 avl_remove(&sdp->sd_snaps, sep);
577 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
578 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
579 (void) strcpy(sep->se_name, nm);
580 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
581 avl_insert(&sdp->sd_snaps, sep, where);
582
583 /*
584 * Change the current mountpoint info:
585 * - update the tail of the mntpoint path
586 * - update the tail of the resource path
587 */
588 pathref = vfs_getmntpoint(vfsp);
589 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
590 VERIFY((tail = strrchr(newpath, '/')) != NULL);
591 *(tail+1) = '\0';
592 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
593 (void) strcat(newpath, nm);
594 refstr_rele(pathref);
595 vfs_setmntpoint(vfsp, newpath, 0);
596
597 pathref = vfs_getresource(vfsp);
598 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
599 VERIFY((tail = strrchr(newpath, '@')) != NULL);
600 *(tail+1) = '\0';
601 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
602 (void) strcat(newpath, nm);
603 refstr_rele(pathref);
604 vfs_setresource(vfsp, newpath, 0);
605
606 vfs_unlock(vfsp);
607 }
608
609 /*ARGSUSED*/
610 static int
611 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
612 cred_t *cr, caller_context_t *ct, int flags)
613 {
614 zfsctl_snapdir_t *sdp = sdvp->v_data;
615 zfs_snapentry_t search, *sep;
616 zfsvfs_t *zfsvfs;
617 avl_index_t where;
618 char from[MAXNAMELEN], to[MAXNAMELEN];
619 char real[MAXNAMELEN];
620 int err;
621
622 zfsvfs = sdvp->v_vfsp->vfs_data;
623 ZFS_ENTER(zfsvfs);
624
625 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
626 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
627 MAXNAMELEN, NULL);
628 if (err == 0) {
629 snm = real;
630 } else if (err != ENOTSUP) {
631 ZFS_EXIT(zfsvfs);
632 return (err);
633 }
634 }
635
636 ZFS_EXIT(zfsvfs);
637
638 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
639 if (!err)
640 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
641 if (!err)
642 err = zfs_secpolicy_rename_perms(from, to, cr);
643 if (err)
644 return (err);
645
646 /*
647 * Cannot move snapshots out of the snapdir.
648 */
649 if (sdvp != tdvp)
650 return (EINVAL);
651
652 if (strcmp(snm, tnm) == 0)
653 return (0);
654
655 mutex_enter(&sdp->sd_lock);
656
657 search.se_name = (char *)snm;
658 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
659 mutex_exit(&sdp->sd_lock);
660 return (ENOENT);
661 }
662
663 err = dmu_objset_rename(from, to, B_FALSE);
664 if (err == 0)
665 zfsctl_rename_snap(sdp, sep, tnm);
666
667 mutex_exit(&sdp->sd_lock);
668
669 return (err);
670 }
671
672 /* ARGSUSED */
673 static int
674 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
675 caller_context_t *ct, int flags)
676 {
677 zfsctl_snapdir_t *sdp = dvp->v_data;
678 zfs_snapentry_t *sep;
679 zfs_snapentry_t search;
680 zfsvfs_t *zfsvfs;
681 char snapname[MAXNAMELEN];
682 char real[MAXNAMELEN];
683 int err;
684
685 zfsvfs = dvp->v_vfsp->vfs_data;
686 ZFS_ENTER(zfsvfs);
687
688 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
689
690 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
691 MAXNAMELEN, NULL);
692 if (err == 0) {
693 name = real;
694 } else if (err != ENOTSUP) {
695 ZFS_EXIT(zfsvfs);
696 return (err);
697 }
698 }
699
700 ZFS_EXIT(zfsvfs);
701
702 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
703 if (!err)
704 err = zfs_secpolicy_destroy_perms(snapname, cr);
705 if (err)
706 return (err);
707
708 mutex_enter(&sdp->sd_lock);
709
710 search.se_name = name;
711 sep = avl_find(&sdp->sd_snaps, &search, NULL);
712 if (sep) {
713 avl_remove(&sdp->sd_snaps, sep);
714 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
715 if (err)
716 avl_add(&sdp->sd_snaps, sep);
717 else
718 err = dmu_objset_destroy(snapname, B_FALSE);
719 } else {
720 err = ENOENT;
721 }
722
723 mutex_exit(&sdp->sd_lock);
724
725 return (err);
726 }
727
728 /*
729 * This creates a snapshot under '.zfs/snapshot'.
730 */
731 /* ARGSUSED */
732 static int
733 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
734 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
735 {
736 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
737 char name[MAXNAMELEN];
738 int err;
739 static enum symfollow follow = NO_FOLLOW;
740 static enum uio_seg seg = UIO_SYSSPACE;
741
742 if (snapshot_namecheck(dirname, NULL, NULL) != 0)
743 return (EILSEQ);
744
745 dmu_objset_name(zfsvfs->z_os, name);
746
747 *vpp = NULL;
748
749 err = zfs_secpolicy_snapshot_perms(name, cr);
750 if (err)
751 return (err);
752
753 if (err == 0) {
754 err = dmu_objset_snapshot(name, dirname, NULL, NULL,
755 B_FALSE, B_FALSE, -1);
756 if (err)
757 return (err);
758 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
759 }
760
761 return (err);
762 }
763
764 /*
765 * Lookup entry point for the 'snapshot' directory. Try to open the
766 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
767 * Perform a mount of the associated dataset on top of the vnode.
768 */
769 /* ARGSUSED */
770 static int
771 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
772 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
773 int *direntflags, pathname_t *realpnp)
774 {
775 zfsctl_snapdir_t *sdp = dvp->v_data;
776 objset_t *snap;
777 char snapname[MAXNAMELEN];
778 char real[MAXNAMELEN];
779 char *mountpoint;
780 zfs_snapentry_t *sep, search;
781 struct mounta margs;
782 vfs_t *vfsp;
783 size_t mountpoint_len;
784 avl_index_t where;
785 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
786 int err;
787
788 /*
789 * No extended attributes allowed under .zfs
790 */
791 if (flags & LOOKUP_XATTR)
792 return (EINVAL);
793
794 ASSERT(dvp->v_type == VDIR);
795
796 /*
797 * If we get a recursive call, that means we got called
798 * from the domount() code while it was trying to look up the
799 * spec (which looks like a local path for zfs). We need to
800 * add some flag to domount() to tell it not to do this lookup.
801 */
802 if (MUTEX_HELD(&sdp->sd_lock))
803 return (ENOENT);
804
805 ZFS_ENTER(zfsvfs);
806
807 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
808 ZFS_EXIT(zfsvfs);
809 return (0);
810 }
811
812 if (flags & FIGNORECASE) {
813 boolean_t conflict = B_FALSE;
814
815 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
816 MAXNAMELEN, &conflict);
817 if (err == 0) {
818 nm = real;
819 } else if (err != ENOTSUP) {
820 ZFS_EXIT(zfsvfs);
821 return (err);
822 }
823 if (realpnp)
824 (void) strlcpy(realpnp->pn_buf, nm,
825 realpnp->pn_bufsize);
826 if (conflict && direntflags)
827 *direntflags = ED_CASE_CONFLICT;
828 }
829
830 mutex_enter(&sdp->sd_lock);
831 search.se_name = (char *)nm;
832 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
833 *vpp = sep->se_root;
834 VN_HOLD(*vpp);
835 err = traverse(vpp);
836 if (err) {
837 VN_RELE(*vpp);
838 *vpp = NULL;
839 } else if (*vpp == sep->se_root) {
840 /*
841 * The snapshot was unmounted behind our backs,
842 * try to remount it.
843 */
844 goto domount;
845 } else {
846 /*
847 * VROOT was set during the traverse call. We need
848 * to clear it since we're pretending to be part
849 * of our parent's vfs.
850 */
851 (*vpp)->v_flag &= ~VROOT;
852 }
853 mutex_exit(&sdp->sd_lock);
854 ZFS_EXIT(zfsvfs);
855 return (err);
856 }
857
858 /*
859 * The requested snapshot is not currently mounted, look it up.
860 */
861 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
862 if (err) {
863 mutex_exit(&sdp->sd_lock);
864 ZFS_EXIT(zfsvfs);
865 /*
866 * handle "ls *" or "?" in a graceful manner,
867 * forcing EILSEQ to ENOENT.
868 * Since shell ultimately passes "*" or "?" as name to lookup
869 */
870 return (err == EILSEQ ? ENOENT : err);
871 }
872 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
873 mutex_exit(&sdp->sd_lock);
874 ZFS_EXIT(zfsvfs);
875 return (ENOENT);
876 }
877
878 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
879 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
880 (void) strcpy(sep->se_name, nm);
881 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
882 avl_insert(&sdp->sd_snaps, sep, where);
883
884 dmu_objset_rele(snap, FTAG);
885 domount:
886 mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
887 strlen("/.zfs/snapshot/") + strlen(nm) + 1;
888 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
889 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
890 refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
891
892 margs.spec = snapname;
893 margs.dir = mountpoint;
894 margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
895 margs.fstype = "zfs";
896 margs.dataptr = NULL;
897 margs.datalen = 0;
898 margs.optptr = NULL;
899 margs.optlen = 0;
900
901 err = domount("zfs", &margs, *vpp, kcred, &vfsp);
902 kmem_free(mountpoint, mountpoint_len);
903
904 if (err == 0) {
905 /*
906 * Return the mounted root rather than the covered mount point.
907 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
908 * the ZFS vnode mounted on top of the GFS node. This ZFS
909 * vnode is the root of the newly created vfsp.
910 */
911 VFS_RELE(vfsp);
912 err = traverse(vpp);
913 }
914
915 if (err == 0) {
916 /*
917 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
918 *
919 * This is where we lie about our v_vfsp in order to
920 * make .zfs/snapshot/<snapname> accessible over NFS
921 * without requiring manual mounts of <snapname>.
922 */
923 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
924 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
925 (*vpp)->v_vfsp = zfsvfs->z_vfs;
926 (*vpp)->v_flag &= ~VROOT;
927 }
928 mutex_exit(&sdp->sd_lock);
929 ZFS_EXIT(zfsvfs);
930
931 /*
932 * If we had an error, drop our hold on the vnode and
933 * zfsctl_snapshot_inactive() will clean up.
934 */
935 if (err) {
936 VN_RELE(*vpp);
937 *vpp = NULL;
938 }
939 return (err);
940 }
941
942 /* ARGSUSED */
943 static int
944 zfsctl_shares_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
945 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
946 int *direntflags, pathname_t *realpnp)
947 {
948 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
949 znode_t *dzp;
950 int error;
951
952 ZFS_ENTER(zfsvfs);
953
954 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
955 ZFS_EXIT(zfsvfs);
956 return (0);
957 }
958
959 if (zfsvfs->z_shares_dir == 0) {
960 ZFS_EXIT(zfsvfs);
961 return (ENOTSUP);
962 }
963 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
964 error = VOP_LOOKUP(ZTOV(dzp), nm, vpp, pnp,
965 flags, rdir, cr, ct, direntflags, realpnp);
966
967 VN_RELE(ZTOV(dzp));
968 ZFS_EXIT(zfsvfs);
969
970 return (error);
971 }
972
973 /* ARGSUSED */
974 static int
975 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
976 offset_t *offp, offset_t *nextp, void *data, int flags)
977 {
978 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
979 char snapname[MAXNAMELEN];
980 uint64_t id, cookie;
981 boolean_t case_conflict;
982 int error;
983
984 ZFS_ENTER(zfsvfs);
985
986 cookie = *offp;
987 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
988 &cookie, &case_conflict);
989 if (error) {
990 ZFS_EXIT(zfsvfs);
991 if (error == ENOENT) {
992 *eofp = 1;
993 return (0);
994 }
995 return (error);
996 }
997
998 if (flags & V_RDDIR_ENTFLAGS) {
999 edirent_t *eodp = dp;
1000
1001 (void) strcpy(eodp->ed_name, snapname);
1002 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1003 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1004 } else {
1005 struct dirent64 *odp = dp;
1006
1007 (void) strcpy(odp->d_name, snapname);
1008 odp->d_ino = ZFSCTL_INO_SNAP(id);
1009 }
1010 *nextp = cookie;
1011
1012 ZFS_EXIT(zfsvfs);
1013
1014 return (0);
1015 }
1016
1017 /* ARGSUSED */
1018 static int
1019 zfsctl_shares_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
1020 caller_context_t *ct, int flags)
1021 {
1022 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1023 znode_t *dzp;
1024 int error;
1025
1026 ZFS_ENTER(zfsvfs);
1027
1028 if (zfsvfs->z_shares_dir == 0) {
1029 ZFS_EXIT(zfsvfs);
1030 return (ENOTSUP);
1031 }
1032 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1033 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ct, flags);
1034 VN_RELE(ZTOV(dzp));
1035 } else {
1036 *eofp = 1;
1037 error = ENOENT;
1038 }
1039
1040 ZFS_EXIT(zfsvfs);
1041 return (error);
1042 }
1043
1044 /*
1045 * pvp is the '.zfs' directory (zfsctl_node_t).
1046 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1047 *
1048 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1049 * when a lookup is performed on .zfs for "snapshot".
1050 */
1051 vnode_t *
1052 zfsctl_mknode_snapdir(vnode_t *pvp)
1053 {
1054 vnode_t *vp;
1055 zfsctl_snapdir_t *sdp;
1056
1057 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
1058 zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1059 zfsctl_snapdir_readdir_cb, NULL);
1060 sdp = vp->v_data;
1061 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1062 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1063 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1064 avl_create(&sdp->sd_snaps, snapentry_compare,
1065 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1066 return (vp);
1067 }
1068
1069 vnode_t *
1070 zfsctl_mknode_shares(vnode_t *pvp)
1071 {
1072 vnode_t *vp;
1073 zfsctl_node_t *sdp;
1074
1075 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1076 zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1077 NULL, NULL);
1078 sdp = vp->v_data;
1079 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1080 return (vp);
1081
1082 }
1083
1084 /* ARGSUSED */
1085 static int
1086 zfsctl_shares_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1087 caller_context_t *ct)
1088 {
1089 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1090 znode_t *dzp;
1091 int error;
1092
1093 ZFS_ENTER(zfsvfs);
1094 if (zfsvfs->z_shares_dir == 0) {
1095 ZFS_EXIT(zfsvfs);
1096 return (ENOTSUP);
1097 }
1098 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1099 error = VOP_GETATTR(ZTOV(dzp), vap, flags, cr, ct);
1100 VN_RELE(ZTOV(dzp));
1101 }
1102 ZFS_EXIT(zfsvfs);
1103 return (error);
1104
1105
1106 }
1107
1108 /* ARGSUSED */
1109 static int
1110 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1111 caller_context_t *ct)
1112 {
1113 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1114 zfsctl_snapdir_t *sdp = vp->v_data;
1115
1116 ZFS_ENTER(zfsvfs);
1117 zfsctl_common_getattr(vp, vap);
1118 vap->va_nodeid = gfs_file_inode(vp);
1119 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1120 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1121 ZFS_EXIT(zfsvfs);
1122
1123 return (0);
1124 }
1125
1126 /* ARGSUSED */
1127 static void
1128 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1129 {
1130 zfsctl_snapdir_t *sdp = vp->v_data;
1131 void *private;
1132
1133 private = gfs_dir_inactive(vp);
1134 if (private != NULL) {
1135 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1136 mutex_destroy(&sdp->sd_lock);
1137 avl_destroy(&sdp->sd_snaps);
1138 kmem_free(private, sizeof (zfsctl_snapdir_t));
1139 }
1140 }
1141
1142 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1143 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1144 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1145 { VOPNAME_IOCTL, { .error = fs_inval } },
1146 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } },
1147 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1148 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } },
1149 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } },
1150 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } },
1151 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
1152 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } },
1153 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1154 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } },
1155 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
1156 { NULL }
1157 };
1158
1159 static const fs_operation_def_t zfsctl_tops_shares[] = {
1160 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1161 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1162 { VOPNAME_IOCTL, { .error = fs_inval } },
1163 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } },
1164 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1165 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } },
1166 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } },
1167 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1168 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
1169 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } },
1170 { NULL }
1171 };
1172
1173 /*
1174 * pvp is the GFS vnode '.zfs/snapshot'.
1175 *
1176 * This creates a GFS node under '.zfs/snapshot' representing each
1177 * snapshot. This newly created GFS node is what we mount snapshot
1178 * vfs_t's ontop of.
1179 */
1180 static vnode_t *
1181 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1182 {
1183 vnode_t *vp;
1184 zfsctl_node_t *zcp;
1185
1186 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1187 zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1188 zcp = vp->v_data;
1189 zcp->zc_id = objset;
1190
1191 return (vp);
1192 }
1193
1194 static void
1195 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1196 {
1197 zfsctl_snapdir_t *sdp;
1198 zfs_snapentry_t *sep, *next;
1199 vnode_t *dvp;
1200
1201 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1202 sdp = dvp->v_data;
1203
1204 mutex_enter(&sdp->sd_lock);
1205
1206 if (vp->v_count > 1) {
1207 mutex_exit(&sdp->sd_lock);
1208 return;
1209 }
1210 ASSERT(!vn_ismntpt(vp));
1211
1212 sep = avl_first(&sdp->sd_snaps);
1213 while (sep != NULL) {
1214 next = AVL_NEXT(&sdp->sd_snaps, sep);
1215
1216 if (sep->se_root == vp) {
1217 avl_remove(&sdp->sd_snaps, sep);
1218 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1219 kmem_free(sep, sizeof (zfs_snapentry_t));
1220 break;
1221 }
1222 sep = next;
1223 }
1224 ASSERT(sep != NULL);
1225
1226 mutex_exit(&sdp->sd_lock);
1227 VN_RELE(dvp);
1228
1229 /*
1230 * Dispose of the vnode for the snapshot mount point.
1231 * This is safe to do because once this entry has been removed
1232 * from the AVL tree, it can't be found again, so cannot become
1233 * "active". If we lookup the same name again we will end up
1234 * creating a new vnode.
1235 */
1236 gfs_vop_inactive(vp, cr, ct);
1237 }
1238
1239
1240 /*
1241 * These VP's should never see the light of day. They should always
1242 * be covered.
1243 */
1244 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
1245 VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapshot_inactive },
1246 NULL, NULL
1247 };
1248
1249 int
1250 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1251 {
1252 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1253 vnode_t *dvp, *vp;
1254 zfsctl_snapdir_t *sdp;
1255 zfsctl_node_t *zcp;
1256 zfs_snapentry_t *sep;
1257 int error;
1258
1259 ASSERT(zfsvfs->z_ctldir != NULL);
1260 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1261 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1262 if (error != 0)
1263 return (error);
1264 sdp = dvp->v_data;
1265
1266 mutex_enter(&sdp->sd_lock);
1267 sep = avl_first(&sdp->sd_snaps);
1268 while (sep != NULL) {
1269 vp = sep->se_root;
1270 zcp = vp->v_data;
1271 if (zcp->zc_id == objsetid)
1272 break;
1273
1274 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1275 }
1276
1277 if (sep != NULL) {
1278 VN_HOLD(vp);
1279 /*
1280 * Return the mounted root rather than the covered mount point.
1281 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1282 * and returns the ZFS vnode mounted on top of the GFS node.
1283 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1284 */
1285 error = traverse(&vp);
1286 if (error == 0) {
1287 if (vp == sep->se_root)
1288 error = EINVAL;
1289 else
1290 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1291 }
1292 mutex_exit(&sdp->sd_lock);
1293 VN_RELE(vp);
1294 } else {
1295 error = EINVAL;
1296 mutex_exit(&sdp->sd_lock);
1297 }
1298
1299 VN_RELE(dvp);
1300
1301 return (error);
1302 }
1303
1304 /*
1305 * Unmount any snapshots for the given filesystem. This is called from
1306 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1307 * snapshots.
1308 */
1309 int
1310 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1311 {
1312 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1313 vnode_t *dvp;
1314 zfsctl_snapdir_t *sdp;
1315 zfs_snapentry_t *sep, *next;
1316 int error;
1317
1318 ASSERT(zfsvfs->z_ctldir != NULL);
1319 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1320 NULL, 0, NULL, cr, NULL, NULL, NULL);
1321 if (error != 0)
1322 return (error);
1323 sdp = dvp->v_data;
1324
1325 mutex_enter(&sdp->sd_lock);
1326
1327 sep = avl_first(&sdp->sd_snaps);
1328 while (sep != NULL) {
1329 next = AVL_NEXT(&sdp->sd_snaps, sep);
1330
1331 /*
1332 * If this snapshot is not mounted, then it must
1333 * have just been unmounted by somebody else, and
1334 * will be cleaned up by zfsctl_snapdir_inactive().
1335 */
1336 if (vn_ismntpt(sep->se_root)) {
1337 avl_remove(&sdp->sd_snaps, sep);
1338 error = zfsctl_unmount_snap(sep, fflags, cr);
1339 if (error) {
1340 avl_add(&sdp->sd_snaps, sep);
1341 break;
1342 }
1343 }
1344 sep = next;
1345 }
1346
1347 mutex_exit(&sdp->sd_lock);
1348 VN_RELE(dvp);
1349
1350 return (error);
1351 }
1352 #endif /* HAVE_ZPL */