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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * ZFS control directory (a.k.a. ".zfs")
29 * This directory provides a common location for all ZFS meta-objects.
30 * Currently, this is only the 'snapshot' directory, but this may expand in the
31 * future. The elements are built using the GFS primitives, as the hierarchy
32 * does not actually exist on disk.
34 * For 'snapshot', we don't want to have all snapshots always mounted, because
35 * this would take up a huge amount of space in /etc/mnttab. We have three
38 * ctldir ------> snapshotdir -------> snapshot
44 * The 'snapshot' node contains just enough information to lookup '..' and act
45 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
46 * perform an automount of the underlying filesystem and return the
47 * corresponding vnode.
49 * All mounts are handled automatically by the kernel, but unmounts are
50 * (currently) handled from user land. The main reason is that there is no
51 * reliable way to auto-unmount the filesystem when it's "no longer in use".
52 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
53 * unmounts any snapshots within the snapshot directory.
55 * The '.zfs', '.zfs/snapshot', and all directories created under
56 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
57 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
59 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
60 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
61 * However, vnodes within these mounted on file systems have their v_vfsp
62 * fields set to the head filesystem to make NFS happy (see
63 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
64 * so that it cannot be freed until all snapshots have been unmounted.
67 #include <fs/fs_subr.h>
68 #include <sys/zfs_ctldir.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/zfs_vfsops.h>
71 #include <sys/vfs_opreg.h>
75 #include <sys/dsl_deleg.h>
76 #include <sys/mount.h>
77 #include <sys/sunddi.h>
79 #include "zfs_namecheck.h"
81 typedef struct zfsctl_node
{
82 gfs_dir_t zc_gfs_private
;
84 timestruc_t zc_cmtime
; /* ctime and mtime, always the same */
87 typedef struct zfsctl_snapdir
{
88 zfsctl_node_t sd_node
;
100 snapentry_compare(const void *a
, const void *b
)
102 const zfs_snapentry_t
*sa
= a
;
103 const zfs_snapentry_t
*sb
= b
;
104 int ret
= strcmp(sa
->se_name
, sb
->se_name
);
114 vnodeops_t
*zfsctl_ops_root
;
115 vnodeops_t
*zfsctl_ops_snapdir
;
116 vnodeops_t
*zfsctl_ops_snapshot
;
117 vnodeops_t
*zfsctl_ops_shares
;
118 vnodeops_t
*zfsctl_ops_shares_dir
;
120 static const fs_operation_def_t zfsctl_tops_root
[];
121 static const fs_operation_def_t zfsctl_tops_snapdir
[];
122 static const fs_operation_def_t zfsctl_tops_snapshot
[];
123 static const fs_operation_def_t zfsctl_tops_shares
[];
125 static vnode_t
*zfsctl_mknode_snapdir(vnode_t
*);
126 static vnode_t
*zfsctl_mknode_shares(vnode_t
*);
127 static vnode_t
*zfsctl_snapshot_mknode(vnode_t
*, uint64_t objset
);
128 static int zfsctl_unmount_snap(zfs_snapentry_t
*, int, cred_t
*);
130 static gfs_opsvec_t zfsctl_opsvec
[] = {
131 { ".zfs", zfsctl_tops_root
, &zfsctl_ops_root
},
132 { ".zfs/snapshot", zfsctl_tops_snapdir
, &zfsctl_ops_snapdir
},
133 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot
, &zfsctl_ops_snapshot
},
134 { ".zfs/shares", zfsctl_tops_shares
, &zfsctl_ops_shares_dir
},
135 { ".zfs/shares/vnode", zfsctl_tops_shares
, &zfsctl_ops_shares
},
140 * Root directory elements. We only have two entries
141 * snapshot and shares.
143 static gfs_dirent_t zfsctl_root_entries
[] = {
144 { "snapshot", zfsctl_mknode_snapdir
, GFS_CACHE_VNODE
},
145 { "shares", zfsctl_mknode_shares
, GFS_CACHE_VNODE
},
149 /* include . and .. in the calculation */
150 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
151 sizeof (gfs_dirent_t)) + 1)
155 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
156 * directories. This is called from the ZFS init routine, and initializes the
157 * vnode ops vectors that we'll be using.
162 VERIFY(gfs_make_opsvec(zfsctl_opsvec
) == 0);
169 * Remove vfsctl vnode ops
172 vn_freevnodeops(zfsctl_ops_root
);
173 if (zfsctl_ops_snapdir
)
174 vn_freevnodeops(zfsctl_ops_snapdir
);
175 if (zfsctl_ops_snapshot
)
176 vn_freevnodeops(zfsctl_ops_snapshot
);
177 if (zfsctl_ops_shares
)
178 vn_freevnodeops(zfsctl_ops_shares
);
179 if (zfsctl_ops_shares_dir
)
180 vn_freevnodeops(zfsctl_ops_shares_dir
);
182 zfsctl_ops_root
= NULL
;
183 zfsctl_ops_snapdir
= NULL
;
184 zfsctl_ops_snapshot
= NULL
;
185 zfsctl_ops_shares
= NULL
;
186 zfsctl_ops_shares_dir
= NULL
;
190 * Return the inode number associated with the 'snapshot' or
191 * 'shares' directory.
195 zfsctl_root_inode_cb(vnode_t
*vp
, int index
)
197 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
202 return (ZFSCTL_INO_SNAPDIR
);
204 return (zfsvfs
->z_shares_dir
);
208 * Create the '.zfs' directory. This directory is cached as part of the VFS
209 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
210 * therefore checks against a vfs_count of 2 instead of 1. This reference
211 * is removed when the ctldir is destroyed in the unmount.
214 zfsctl_create(zfsvfs_t
*zfsvfs
)
219 ASSERT(zfsvfs
->z_ctldir
== NULL
);
221 vp
= gfs_root_create(sizeof (zfsctl_node_t
), zfsvfs
->z_vfs
,
222 zfsctl_ops_root
, ZFSCTL_INO_ROOT
, zfsctl_root_entries
,
223 zfsctl_root_inode_cb
, MAXNAMELEN
, NULL
, NULL
);
225 zcp
->zc_id
= ZFSCTL_INO_ROOT
;
227 VERIFY(VFS_ROOT(zfsvfs
->z_vfs
, &rvp
) == 0);
228 ZFS_TIME_DECODE(&zcp
->zc_cmtime
, VTOZ(rvp
)->z_phys
->zp_crtime
);
232 * We're only faking the fact that we have a root of a filesystem for
233 * the sake of the GFS interfaces. Undo the flag manipulation it did
236 vp
->v_flag
&= ~(VROOT
| VNOCACHE
| VNOMAP
| VNOSWAP
| VNOMOUNT
);
238 zfsvfs
->z_ctldir
= vp
;
242 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
243 * There might still be more references if we were force unmounted, but only
244 * new zfs_inactive() calls can occur and they don't reference .zfs
247 zfsctl_destroy(zfsvfs_t
*zfsvfs
)
249 VN_RELE(zfsvfs
->z_ctldir
);
250 zfsvfs
->z_ctldir
= NULL
;
254 * Given a root znode, retrieve the associated .zfs directory.
255 * Add a hold to the vnode and return it.
258 zfsctl_root(znode_t
*zp
)
260 ASSERT(zfs_has_ctldir(zp
));
261 VN_HOLD(zp
->z_zfsvfs
->z_ctldir
);
262 return (zp
->z_zfsvfs
->z_ctldir
);
266 * Common open routine. Disallow any write access.
270 zfsctl_common_open(vnode_t
**vpp
, int flags
, cred_t
*cr
, caller_context_t
*ct
)
279 * Common close routine. Nothing to do here.
283 zfsctl_common_close(vnode_t
*vpp
, int flags
, int count
, offset_t off
,
284 cred_t
*cr
, caller_context_t
*ct
)
290 * Common access routine. Disallow writes.
294 zfsctl_common_access(vnode_t
*vp
, int mode
, int flags
, cred_t
*cr
,
295 caller_context_t
*ct
)
297 if (flags
& V_ACE_MASK
) {
298 if (mode
& ACE_ALL_WRITE_PERMS
)
309 * Common getattr function. Fill in basic information.
312 zfsctl_common_getattr(vnode_t
*vp
, vattr_t
*vap
)
314 zfsctl_node_t
*zcp
= vp
->v_data
;
321 * We are a purly virtual object, so we have no
322 * blocksize or allocated blocks.
327 vap
->va_fsid
= vp
->v_vfsp
->vfs_dev
;
328 vap
->va_mode
= S_IRUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
|
332 * We live in the now (for atime).
336 vap
->va_mtime
= vap
->va_ctime
= zcp
->zc_cmtime
;
341 zfsctl_common_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
343 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
344 zfsctl_node_t
*zcp
= vp
->v_data
;
345 uint64_t object
= zcp
->zc_id
;
351 if (fidp
->fid_len
< SHORT_FID_LEN
) {
352 fidp
->fid_len
= SHORT_FID_LEN
;
357 zfid
= (zfid_short_t
*)fidp
;
359 zfid
->zf_len
= SHORT_FID_LEN
;
361 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
362 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
364 /* .zfs znodes always have a generation number of 0 */
365 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
375 zfsctl_shares_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
377 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
383 if (zfsvfs
->z_shares_dir
== 0) {
388 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
)) == 0) {
389 error
= VOP_FID(ZTOV(dzp
), fidp
, ct
);
397 * .zfs inode namespace
399 * We need to generate unique inode numbers for all files and directories
400 * within the .zfs pseudo-filesystem. We use the following scheme:
405 * .zfs/snapshot/<snap> objectid(snap)
408 #define ZFSCTL_INO_SNAP(id) (id)
411 * Get root directory attributes.
415 zfsctl_root_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
416 caller_context_t
*ct
)
418 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
421 vap
->va_nodeid
= ZFSCTL_INO_ROOT
;
422 vap
->va_nlink
= vap
->va_size
= NROOT_ENTRIES
;
424 zfsctl_common_getattr(vp
, vap
);
431 * Special case the handling of "..".
435 zfsctl_root_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, pathname_t
*pnp
,
436 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
437 int *direntflags
, pathname_t
*realpnp
)
439 zfsvfs_t
*zfsvfs
= dvp
->v_vfsp
->vfs_data
;
443 * No extended attributes allowed under .zfs
445 if (flags
& LOOKUP_XATTR
)
450 if (strcmp(nm
, "..") == 0) {
451 err
= VFS_ROOT(dvp
->v_vfsp
, vpp
);
453 err
= gfs_vop_lookup(dvp
, nm
, vpp
, pnp
, flags
, rdir
,
454 cr
, ct
, direntflags
, realpnp
);
463 zfsctl_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
464 caller_context_t
*ct
)
467 * We only care about ACL_ENABLED so that libsec can
468 * display ACL correctly and not default to POSIX draft.
470 if (cmd
== _PC_ACL_ENABLED
) {
471 *valp
= _ACL_ACE_ENABLED
;
475 return (fs_pathconf(vp
, cmd
, valp
, cr
, ct
));
478 static const fs_operation_def_t zfsctl_tops_root
[] = {
479 { VOPNAME_OPEN
, { .vop_open
= zfsctl_common_open
} },
480 { VOPNAME_CLOSE
, { .vop_close
= zfsctl_common_close
} },
481 { VOPNAME_IOCTL
, { .error
= fs_inval
} },
482 { VOPNAME_GETATTR
, { .vop_getattr
= zfsctl_root_getattr
} },
483 { VOPNAME_ACCESS
, { .vop_access
= zfsctl_common_access
} },
484 { VOPNAME_READDIR
, { .vop_readdir
= gfs_vop_readdir
} },
485 { VOPNAME_LOOKUP
, { .vop_lookup
= zfsctl_root_lookup
} },
486 { VOPNAME_SEEK
, { .vop_seek
= fs_seek
} },
487 { VOPNAME_INACTIVE
, { .vop_inactive
= gfs_vop_inactive
} },
488 { VOPNAME_PATHCONF
, { .vop_pathconf
= zfsctl_pathconf
} },
489 { VOPNAME_FID
, { .vop_fid
= zfsctl_common_fid
} },
494 zfsctl_snapshot_zname(vnode_t
*vp
, const char *name
, int len
, char *zname
)
496 objset_t
*os
= ((zfsvfs_t
*)((vp
)->v_vfsp
->vfs_data
))->z_os
;
498 if (snapshot_namecheck(name
, NULL
, NULL
) != 0)
500 dmu_objset_name(os
, zname
);
501 if (strlen(zname
) + 1 + strlen(name
) >= len
)
502 return (ENAMETOOLONG
);
503 (void) strcat(zname
, "@");
504 (void) strcat(zname
, name
);
509 zfsctl_unmount_snap(zfs_snapentry_t
*sep
, int fflags
, cred_t
*cr
)
511 vnode_t
*svp
= sep
->se_root
;
514 ASSERT(vn_ismntpt(svp
));
516 /* this will be dropped by dounmount() */
517 if ((error
= vn_vfswlock(svp
)) != 0)
521 error
= dounmount(vn_mountedvfs(svp
), fflags
, cr
);
528 * We can't use VN_RELE(), as that will try to invoke
529 * zfsctl_snapdir_inactive(), which would cause us to destroy
530 * the sd_lock mutex held by our caller.
532 ASSERT(svp
->v_count
== 1);
533 gfs_vop_inactive(svp
, cr
, NULL
);
535 kmem_free(sep
->se_name
, strlen(sep
->se_name
) + 1);
536 kmem_free(sep
, sizeof (zfs_snapentry_t
));
542 zfsctl_rename_snap(zfsctl_snapdir_t
*sdp
, zfs_snapentry_t
*sep
, const char *nm
)
547 char newpath
[MAXNAMELEN
];
550 ASSERT(MUTEX_HELD(&sdp
->sd_lock
));
553 vfsp
= vn_mountedvfs(sep
->se_root
);
554 ASSERT(vfsp
!= NULL
);
559 * Change the name in the AVL tree.
561 avl_remove(&sdp
->sd_snaps
, sep
);
562 kmem_free(sep
->se_name
, strlen(sep
->se_name
) + 1);
563 sep
->se_name
= kmem_alloc(strlen(nm
) + 1, KM_SLEEP
);
564 (void) strcpy(sep
->se_name
, nm
);
565 VERIFY(avl_find(&sdp
->sd_snaps
, sep
, &where
) == NULL
);
566 avl_insert(&sdp
->sd_snaps
, sep
, where
);
569 * Change the current mountpoint info:
570 * - update the tail of the mntpoint path
571 * - update the tail of the resource path
573 pathref
= vfs_getmntpoint(vfsp
);
574 (void) strncpy(newpath
, refstr_value(pathref
), sizeof (newpath
));
575 VERIFY((tail
= strrchr(newpath
, '/')) != NULL
);
577 ASSERT3U(strlen(newpath
) + strlen(nm
), <, sizeof (newpath
));
578 (void) strcat(newpath
, nm
);
579 refstr_rele(pathref
);
580 vfs_setmntpoint(vfsp
, newpath
);
582 pathref
= vfs_getresource(vfsp
);
583 (void) strncpy(newpath
, refstr_value(pathref
), sizeof (newpath
));
584 VERIFY((tail
= strrchr(newpath
, '@')) != NULL
);
586 ASSERT3U(strlen(newpath
) + strlen(nm
), <, sizeof (newpath
));
587 (void) strcat(newpath
, nm
);
588 refstr_rele(pathref
);
589 vfs_setresource(vfsp
, newpath
);
596 zfsctl_snapdir_rename(vnode_t
*sdvp
, char *snm
, vnode_t
*tdvp
, char *tnm
,
597 cred_t
*cr
, caller_context_t
*ct
, int flags
)
599 zfsctl_snapdir_t
*sdp
= sdvp
->v_data
;
600 zfs_snapentry_t search
, *sep
;
603 char from
[MAXNAMELEN
], to
[MAXNAMELEN
];
604 char real
[MAXNAMELEN
];
607 zfsvfs
= sdvp
->v_vfsp
->vfs_data
;
610 if ((flags
& FIGNORECASE
) || zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) {
611 err
= dmu_snapshot_realname(zfsvfs
->z_os
, snm
, real
,
615 } else if (err
!= ENOTSUP
) {
623 err
= zfsctl_snapshot_zname(sdvp
, snm
, MAXNAMELEN
, from
);
625 err
= zfsctl_snapshot_zname(tdvp
, tnm
, MAXNAMELEN
, to
);
627 err
= zfs_secpolicy_rename_perms(from
, to
, cr
);
632 * Cannot move snapshots out of the snapdir.
637 if (strcmp(snm
, tnm
) == 0)
640 mutex_enter(&sdp
->sd_lock
);
642 search
.se_name
= (char *)snm
;
643 if ((sep
= avl_find(&sdp
->sd_snaps
, &search
, &where
)) == NULL
) {
644 mutex_exit(&sdp
->sd_lock
);
648 err
= dmu_objset_rename(from
, to
, B_FALSE
);
650 zfsctl_rename_snap(sdp
, sep
, tnm
);
652 mutex_exit(&sdp
->sd_lock
);
659 zfsctl_snapdir_remove(vnode_t
*dvp
, char *name
, vnode_t
*cwd
, cred_t
*cr
,
660 caller_context_t
*ct
, int flags
)
662 zfsctl_snapdir_t
*sdp
= dvp
->v_data
;
663 zfs_snapentry_t
*sep
;
664 zfs_snapentry_t search
;
666 char snapname
[MAXNAMELEN
];
667 char real
[MAXNAMELEN
];
670 zfsvfs
= dvp
->v_vfsp
->vfs_data
;
673 if ((flags
& FIGNORECASE
) || zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) {
675 err
= dmu_snapshot_realname(zfsvfs
->z_os
, name
, real
,
679 } else if (err
!= ENOTSUP
) {
687 err
= zfsctl_snapshot_zname(dvp
, name
, MAXNAMELEN
, snapname
);
689 err
= zfs_secpolicy_destroy_perms(snapname
, cr
);
693 mutex_enter(&sdp
->sd_lock
);
695 search
.se_name
= name
;
696 sep
= avl_find(&sdp
->sd_snaps
, &search
, NULL
);
698 avl_remove(&sdp
->sd_snaps
, sep
);
699 err
= zfsctl_unmount_snap(sep
, MS_FORCE
, cr
);
701 avl_add(&sdp
->sd_snaps
, sep
);
703 err
= dmu_objset_destroy(snapname
);
708 mutex_exit(&sdp
->sd_lock
);
714 * This creates a snapshot under '.zfs/snapshot'.
718 zfsctl_snapdir_mkdir(vnode_t
*dvp
, char *dirname
, vattr_t
*vap
, vnode_t
**vpp
,
719 cred_t
*cr
, caller_context_t
*cc
, int flags
, vsecattr_t
*vsecp
)
721 zfsvfs_t
*zfsvfs
= dvp
->v_vfsp
->vfs_data
;
722 char name
[MAXNAMELEN
];
724 static enum symfollow follow
= NO_FOLLOW
;
725 static enum uio_seg seg
= UIO_SYSSPACE
;
727 if (snapshot_namecheck(dirname
, NULL
, NULL
) != 0)
730 dmu_objset_name(zfsvfs
->z_os
, name
);
734 err
= zfs_secpolicy_snapshot_perms(name
, cr
);
739 err
= dmu_objset_snapshot(name
, dirname
, NULL
, B_FALSE
);
742 err
= lookupnameat(dirname
, seg
, follow
, NULL
, vpp
, dvp
);
749 * Lookup entry point for the 'snapshot' directory. Try to open the
750 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
751 * Perform a mount of the associated dataset on top of the vnode.
755 zfsctl_snapdir_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, pathname_t
*pnp
,
756 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
757 int *direntflags
, pathname_t
*realpnp
)
759 zfsctl_snapdir_t
*sdp
= dvp
->v_data
;
761 char snapname
[MAXNAMELEN
];
762 char real
[MAXNAMELEN
];
764 zfs_snapentry_t
*sep
, search
;
767 size_t mountpoint_len
;
769 zfsvfs_t
*zfsvfs
= dvp
->v_vfsp
->vfs_data
;
773 * No extended attributes allowed under .zfs
775 if (flags
& LOOKUP_XATTR
)
778 ASSERT(dvp
->v_type
== VDIR
);
781 * If we get a recursive call, that means we got called
782 * from the domount() code while it was trying to look up the
783 * spec (which looks like a local path for zfs). We need to
784 * add some flag to domount() to tell it not to do this lookup.
786 if (MUTEX_HELD(&sdp
->sd_lock
))
791 if (gfs_lookup_dot(vpp
, dvp
, zfsvfs
->z_ctldir
, nm
) == 0) {
796 if (flags
& FIGNORECASE
) {
797 boolean_t conflict
= B_FALSE
;
799 err
= dmu_snapshot_realname(zfsvfs
->z_os
, nm
, real
,
800 MAXNAMELEN
, &conflict
);
803 } else if (err
!= ENOTSUP
) {
808 (void) strlcpy(realpnp
->pn_buf
, nm
,
809 realpnp
->pn_bufsize
);
810 if (conflict
&& direntflags
)
811 *direntflags
= ED_CASE_CONFLICT
;
814 mutex_enter(&sdp
->sd_lock
);
815 search
.se_name
= (char *)nm
;
816 if ((sep
= avl_find(&sdp
->sd_snaps
, &search
, &where
)) != NULL
) {
823 } else if (*vpp
== sep
->se_root
) {
825 * The snapshot was unmounted behind our backs,
831 * VROOT was set during the traverse call. We need
832 * to clear it since we're pretending to be part
833 * of our parent's vfs.
835 (*vpp
)->v_flag
&= ~VROOT
;
837 mutex_exit(&sdp
->sd_lock
);
843 * The requested snapshot is not currently mounted, look it up.
845 err
= zfsctl_snapshot_zname(dvp
, nm
, MAXNAMELEN
, snapname
);
847 mutex_exit(&sdp
->sd_lock
);
850 * handle "ls *" or "?" in a graceful manner,
851 * forcing EILSEQ to ENOENT.
852 * Since shell ultimately passes "*" or "?" as name to lookup
854 return (err
== EILSEQ
? ENOENT
: err
);
856 if (dmu_objset_open(snapname
, DMU_OST_ZFS
,
857 DS_MODE_USER
| DS_MODE_READONLY
, &snap
) != 0) {
858 mutex_exit(&sdp
->sd_lock
);
863 sep
= kmem_alloc(sizeof (zfs_snapentry_t
), KM_SLEEP
);
864 sep
->se_name
= kmem_alloc(strlen(nm
) + 1, KM_SLEEP
);
865 (void) strcpy(sep
->se_name
, nm
);
866 *vpp
= sep
->se_root
= zfsctl_snapshot_mknode(dvp
, dmu_objset_id(snap
));
867 avl_insert(&sdp
->sd_snaps
, sep
, where
);
869 dmu_objset_close(snap
);
871 mountpoint_len
= strlen(refstr_value(dvp
->v_vfsp
->vfs_mntpt
)) +
872 strlen("/.zfs/snapshot/") + strlen(nm
) + 1;
873 mountpoint
= kmem_alloc(mountpoint_len
, KM_SLEEP
);
874 (void) snprintf(mountpoint
, mountpoint_len
, "%s/.zfs/snapshot/%s",
875 refstr_value(dvp
->v_vfsp
->vfs_mntpt
), nm
);
877 margs
.spec
= snapname
;
878 margs
.dir
= mountpoint
;
879 margs
.flags
= MS_SYSSPACE
| MS_NOMNTTAB
;
880 margs
.fstype
= "zfs";
881 margs
.dataptr
= NULL
;
886 err
= domount("zfs", &margs
, *vpp
, kcred
, &vfsp
);
887 kmem_free(mountpoint
, mountpoint_len
);
891 * Return the mounted root rather than the covered mount point.
892 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
893 * the ZFS vnode mounted on top of the GFS node. This ZFS
894 * vnode is the root of the newly created vfsp.
902 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
904 * This is where we lie about our v_vfsp in order to
905 * make .zfs/snapshot/<snapname> accessible over NFS
906 * without requiring manual mounts of <snapname>.
908 ASSERT(VTOZ(*vpp
)->z_zfsvfs
!= zfsvfs
);
909 VTOZ(*vpp
)->z_zfsvfs
->z_parent
= zfsvfs
;
910 (*vpp
)->v_vfsp
= zfsvfs
->z_vfs
;
911 (*vpp
)->v_flag
&= ~VROOT
;
913 mutex_exit(&sdp
->sd_lock
);
917 * If we had an error, drop our hold on the vnode and
918 * zfsctl_snapshot_inactive() will clean up.
929 zfsctl_shares_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, pathname_t
*pnp
,
930 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
931 int *direntflags
, pathname_t
*realpnp
)
933 zfsvfs_t
*zfsvfs
= dvp
->v_vfsp
->vfs_data
;
939 if (gfs_lookup_dot(vpp
, dvp
, zfsvfs
->z_ctldir
, nm
) == 0) {
944 if (zfsvfs
->z_shares_dir
== 0) {
948 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
)) == 0)
949 error
= VOP_LOOKUP(ZTOV(dzp
), nm
, vpp
, pnp
,
950 flags
, rdir
, cr
, ct
, direntflags
, realpnp
);
960 zfsctl_snapdir_readdir_cb(vnode_t
*vp
, void *dp
, int *eofp
,
961 offset_t
*offp
, offset_t
*nextp
, void *data
, int flags
)
963 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
964 char snapname
[MAXNAMELEN
];
966 boolean_t case_conflict
;
972 error
= dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
, snapname
, &id
,
973 &cookie
, &case_conflict
);
976 if (error
== ENOENT
) {
983 if (flags
& V_RDDIR_ENTFLAGS
) {
984 edirent_t
*eodp
= dp
;
986 (void) strcpy(eodp
->ed_name
, snapname
);
987 eodp
->ed_ino
= ZFSCTL_INO_SNAP(id
);
988 eodp
->ed_eflags
= case_conflict
? ED_CASE_CONFLICT
: 0;
990 struct dirent64
*odp
= dp
;
992 (void) strcpy(odp
->d_name
, snapname
);
993 odp
->d_ino
= ZFSCTL_INO_SNAP(id
);
1004 zfsctl_shares_readdir(vnode_t
*vp
, uio_t
*uiop
, cred_t
*cr
, int *eofp
,
1005 caller_context_t
*ct
, int flags
)
1007 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
1013 if (zfsvfs
->z_shares_dir
== 0) {
1017 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
)) == 0) {
1018 error
= VOP_READDIR(ZTOV(dzp
), uiop
, cr
, eofp
, ct
, flags
);
1030 * pvp is the '.zfs' directory (zfsctl_node_t).
1031 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1033 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1034 * when a lookup is performed on .zfs for "snapshot".
1037 zfsctl_mknode_snapdir(vnode_t
*pvp
)
1040 zfsctl_snapdir_t
*sdp
;
1042 vp
= gfs_dir_create(sizeof (zfsctl_snapdir_t
), pvp
,
1043 zfsctl_ops_snapdir
, NULL
, NULL
, MAXNAMELEN
,
1044 zfsctl_snapdir_readdir_cb
, NULL
);
1046 sdp
->sd_node
.zc_id
= ZFSCTL_INO_SNAPDIR
;
1047 sdp
->sd_node
.zc_cmtime
= ((zfsctl_node_t
*)pvp
->v_data
)->zc_cmtime
;
1048 mutex_init(&sdp
->sd_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1049 avl_create(&sdp
->sd_snaps
, snapentry_compare
,
1050 sizeof (zfs_snapentry_t
), offsetof(zfs_snapentry_t
, se_node
));
1055 zfsctl_mknode_shares(vnode_t
*pvp
)
1060 vp
= gfs_dir_create(sizeof (zfsctl_node_t
), pvp
,
1061 zfsctl_ops_shares
, NULL
, NULL
, MAXNAMELEN
,
1064 sdp
->zc_cmtime
= ((zfsctl_node_t
*)pvp
->v_data
)->zc_cmtime
;
1071 zfsctl_shares_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
1072 caller_context_t
*ct
)
1074 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
1079 if (zfsvfs
->z_shares_dir
== 0) {
1083 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
)) == 0) {
1084 error
= VOP_GETATTR(ZTOV(dzp
), vap
, flags
, cr
, ct
);
1095 zfsctl_snapdir_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
1096 caller_context_t
*ct
)
1098 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
1099 zfsctl_snapdir_t
*sdp
= vp
->v_data
;
1102 zfsctl_common_getattr(vp
, vap
);
1103 vap
->va_nodeid
= gfs_file_inode(vp
);
1104 vap
->va_nlink
= vap
->va_size
= avl_numnodes(&sdp
->sd_snaps
) + 2;
1112 zfsctl_snapdir_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
1114 zfsctl_snapdir_t
*sdp
= vp
->v_data
;
1117 private = gfs_dir_inactive(vp
);
1118 if (private != NULL
) {
1119 ASSERT(avl_numnodes(&sdp
->sd_snaps
) == 0);
1120 mutex_destroy(&sdp
->sd_lock
);
1121 avl_destroy(&sdp
->sd_snaps
);
1122 kmem_free(private, sizeof (zfsctl_snapdir_t
));
1126 static const fs_operation_def_t zfsctl_tops_snapdir
[] = {
1127 { VOPNAME_OPEN
, { .vop_open
= zfsctl_common_open
} },
1128 { VOPNAME_CLOSE
, { .vop_close
= zfsctl_common_close
} },
1129 { VOPNAME_IOCTL
, { .error
= fs_inval
} },
1130 { VOPNAME_GETATTR
, { .vop_getattr
= zfsctl_snapdir_getattr
} },
1131 { VOPNAME_ACCESS
, { .vop_access
= zfsctl_common_access
} },
1132 { VOPNAME_RENAME
, { .vop_rename
= zfsctl_snapdir_rename
} },
1133 { VOPNAME_RMDIR
, { .vop_rmdir
= zfsctl_snapdir_remove
} },
1134 { VOPNAME_MKDIR
, { .vop_mkdir
= zfsctl_snapdir_mkdir
} },
1135 { VOPNAME_READDIR
, { .vop_readdir
= gfs_vop_readdir
} },
1136 { VOPNAME_LOOKUP
, { .vop_lookup
= zfsctl_snapdir_lookup
} },
1137 { VOPNAME_SEEK
, { .vop_seek
= fs_seek
} },
1138 { VOPNAME_INACTIVE
, { .vop_inactive
= zfsctl_snapdir_inactive
} },
1139 { VOPNAME_FID
, { .vop_fid
= zfsctl_common_fid
} },
1143 static const fs_operation_def_t zfsctl_tops_shares
[] = {
1144 { VOPNAME_OPEN
, { .vop_open
= zfsctl_common_open
} },
1145 { VOPNAME_CLOSE
, { .vop_close
= zfsctl_common_close
} },
1146 { VOPNAME_IOCTL
, { .error
= fs_inval
} },
1147 { VOPNAME_GETATTR
, { .vop_getattr
= zfsctl_shares_getattr
} },
1148 { VOPNAME_ACCESS
, { .vop_access
= zfsctl_common_access
} },
1149 { VOPNAME_READDIR
, { .vop_readdir
= zfsctl_shares_readdir
} },
1150 { VOPNAME_LOOKUP
, { .vop_lookup
= zfsctl_shares_lookup
} },
1151 { VOPNAME_SEEK
, { .vop_seek
= fs_seek
} },
1152 { VOPNAME_INACTIVE
, { .vop_inactive
= gfs_vop_inactive
} },
1153 { VOPNAME_FID
, { .vop_fid
= zfsctl_shares_fid
} },
1158 * pvp is the GFS vnode '.zfs/snapshot'.
1160 * This creates a GFS node under '.zfs/snapshot' representing each
1161 * snapshot. This newly created GFS node is what we mount snapshot
1165 zfsctl_snapshot_mknode(vnode_t
*pvp
, uint64_t objset
)
1170 vp
= gfs_dir_create(sizeof (zfsctl_node_t
), pvp
,
1171 zfsctl_ops_snapshot
, NULL
, NULL
, MAXNAMELEN
, NULL
, NULL
);
1173 zcp
->zc_id
= objset
;
1179 zfsctl_snapshot_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
1181 zfsctl_snapdir_t
*sdp
;
1182 zfs_snapentry_t
*sep
, *next
;
1185 VERIFY(gfs_dir_lookup(vp
, "..", &dvp
, cr
, 0, NULL
, NULL
) == 0);
1188 mutex_enter(&sdp
->sd_lock
);
1190 if (vp
->v_count
> 1) {
1191 mutex_exit(&sdp
->sd_lock
);
1194 ASSERT(!vn_ismntpt(vp
));
1196 sep
= avl_first(&sdp
->sd_snaps
);
1197 while (sep
!= NULL
) {
1198 next
= AVL_NEXT(&sdp
->sd_snaps
, sep
);
1200 if (sep
->se_root
== vp
) {
1201 avl_remove(&sdp
->sd_snaps
, sep
);
1202 kmem_free(sep
->se_name
, strlen(sep
->se_name
) + 1);
1203 kmem_free(sep
, sizeof (zfs_snapentry_t
));
1208 ASSERT(sep
!= NULL
);
1210 mutex_exit(&sdp
->sd_lock
);
1214 * Dispose of the vnode for the snapshot mount point.
1215 * This is safe to do because once this entry has been removed
1216 * from the AVL tree, it can't be found again, so cannot become
1217 * "active". If we lookup the same name again we will end up
1218 * creating a new vnode.
1220 gfs_vop_inactive(vp
, cr
, ct
);
1225 * These VP's should never see the light of day. They should always
1228 static const fs_operation_def_t zfsctl_tops_snapshot
[] = {
1229 VOPNAME_INACTIVE
, { .vop_inactive
= zfsctl_snapshot_inactive
},
1234 zfsctl_lookup_objset(vfs_t
*vfsp
, uint64_t objsetid
, zfsvfs_t
**zfsvfsp
)
1236 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1238 zfsctl_snapdir_t
*sdp
;
1240 zfs_snapentry_t
*sep
;
1243 ASSERT(zfsvfs
->z_ctldir
!= NULL
);
1244 error
= zfsctl_root_lookup(zfsvfs
->z_ctldir
, "snapshot", &dvp
,
1245 NULL
, 0, NULL
, kcred
, NULL
, NULL
, NULL
);
1250 mutex_enter(&sdp
->sd_lock
);
1251 sep
= avl_first(&sdp
->sd_snaps
);
1252 while (sep
!= NULL
) {
1255 if (zcp
->zc_id
== objsetid
)
1258 sep
= AVL_NEXT(&sdp
->sd_snaps
, sep
);
1264 * Return the mounted root rather than the covered mount point.
1265 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1266 * and returns the ZFS vnode mounted on top of the GFS node.
1267 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1269 error
= traverse(&vp
);
1271 if (vp
== sep
->se_root
)
1274 *zfsvfsp
= VTOZ(vp
)->z_zfsvfs
;
1276 mutex_exit(&sdp
->sd_lock
);
1280 mutex_exit(&sdp
->sd_lock
);
1289 * Unmount any snapshots for the given filesystem. This is called from
1290 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1294 zfsctl_umount_snapshots(vfs_t
*vfsp
, int fflags
, cred_t
*cr
)
1296 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1298 zfsctl_snapdir_t
*sdp
;
1299 zfs_snapentry_t
*sep
, *next
;
1302 ASSERT(zfsvfs
->z_ctldir
!= NULL
);
1303 error
= zfsctl_root_lookup(zfsvfs
->z_ctldir
, "snapshot", &dvp
,
1304 NULL
, 0, NULL
, cr
, NULL
, NULL
, NULL
);
1309 mutex_enter(&sdp
->sd_lock
);
1311 sep
= avl_first(&sdp
->sd_snaps
);
1312 while (sep
!= NULL
) {
1313 next
= AVL_NEXT(&sdp
->sd_snaps
, sep
);
1316 * If this snapshot is not mounted, then it must
1317 * have just been unmounted by somebody else, and
1318 * will be cleaned up by zfsctl_snapdir_inactive().
1320 if (vn_ismntpt(sep
->se_root
)) {
1321 avl_remove(&sdp
->sd_snaps
, sep
);
1322 error
= zfsctl_unmount_snap(sep
, fflags
, cr
);
1324 avl_add(&sdp
->sd_snaps
, sep
);
1331 mutex_exit(&sdp
->sd_lock
);