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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
25 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
27 * Rewritten for Linux by:
28 * Rohan Puri <rohan.puri15@gmail.com>
29 * Brian Behlendorf <behlendorf1@llnl.gov>
30 * Copyright (c) 2013 by Delphix. All rights reserved.
34 * ZFS control directory (a.k.a. ".zfs")
36 * This directory provides a common location for all ZFS meta-objects.
37 * Currently, this is only the 'snapshot' and 'shares' directory, but this may
38 * expand in the future. The elements are built dynamically, as the hierarchy
39 * does not actually exist on disk.
41 * For 'snapshot', we don't want to have all snapshots always mounted, because
42 * this would take up a huge amount of space in /etc/mnttab. We have three
45 * ctldir ------> snapshotdir -------> snapshot
51 * The 'snapshot' node contains just enough information to lookup '..' and act
52 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
53 * perform an automount of the underlying filesystem and return the
54 * corresponding inode.
56 * All mounts are handled automatically by an user mode helper which invokes
57 * the mount mount procedure. Unmounts are handled by allowing the mount
58 * point to expire so the kernel may automatically unmount it.
60 * The '.zfs', '.zfs/snapshot', and all directories created under
61 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') all share the same
62 * share the same zfs_sb_t as the head filesystem (what '.zfs' lives under).
64 * File systems mounted on top of the '.zfs/snapshot/<snapname>' paths
65 * (ie: snapshots) are complete ZFS filesystems and have their own unique
66 * zfs_sb_t. However, the fsid reported by these mounts will be the same
67 * as that used by the parent zfs_sb_t to make NFS happy.
70 #include <sys/types.h>
71 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/sysmacros.h>
75 #include <sys/pathname.h>
77 #include <sys/vfs_opreg.h>
78 #include <sys/zfs_ctldir.h>
79 #include <sys/zfs_ioctl.h>
80 #include <sys/zfs_vfsops.h>
81 #include <sys/zfs_vnops.h>
84 #include <sys/dsl_destroy.h>
85 #include <sys/dsl_deleg.h>
86 #include <sys/mount.h>
88 #include "zfs_namecheck.h"
91 * Control Directory Tunables (.zfs)
93 int zfs_expire_snapshot
= ZFSCTL_EXPIRE_SNAPSHOT
;
96 * Dedicated task queue for unmounting snapshots.
98 static taskq_t
*zfs_expire_taskq
;
100 static zfs_snapentry_t
*
101 zfsctl_sep_alloc(void)
103 return kmem_zalloc(sizeof (zfs_snapentry_t
), KM_SLEEP
);
107 zfsctl_sep_free(zfs_snapentry_t
*sep
)
109 kmem_free(sep
->se_name
, MAXNAMELEN
);
110 kmem_free(sep
->se_path
, PATH_MAX
);
111 kmem_free(sep
, sizeof (zfs_snapentry_t
));
115 * Attempt to expire an automounted snapshot, unmounts are attempted every
116 * 'zfs_expire_snapshot' seconds until they succeed. The work request is
117 * responsible for rescheduling itself and freeing the zfs_expire_snapshot_t.
120 zfsctl_expire_snapshot(void *data
)
122 zfs_snapentry_t
*sep
= (zfs_snapentry_t
*)data
;
123 zfs_sb_t
*zsb
= ITOZSB(sep
->se_inode
);
126 error
= zfsctl_unmount_snapshot(zsb
, sep
->se_name
, MNT_EXPIRE
);
128 sep
->se_taskqid
= taskq_dispatch_delay(zfs_expire_taskq
,
129 zfsctl_expire_snapshot
, sep
, TQ_SLEEP
,
130 ddi_get_lbolt() + zfs_expire_snapshot
* HZ
);
134 snapentry_compare(const void *a
, const void *b
)
136 const zfs_snapentry_t
*sa
= a
;
137 const zfs_snapentry_t
*sb
= b
;
138 int ret
= strcmp(sa
->se_name
, sb
->se_name
);
149 zfsctl_is_node(struct inode
*ip
)
151 return (ITOZ(ip
)->z_is_ctldir
);
155 zfsctl_is_snapdir(struct inode
*ip
)
157 return (zfsctl_is_node(ip
) && (ip
->i_ino
<= ZFSCTL_INO_SNAPDIRS
));
161 * Allocate a new inode with the passed id and ops.
163 static struct inode
*
164 zfsctl_inode_alloc(zfs_sb_t
*zsb
, uint64_t id
,
165 const struct file_operations
*fops
, const struct inode_operations
*ops
)
167 struct timespec now
= current_fs_time(zsb
->z_sb
);
171 ip
= new_inode(zsb
->z_sb
);
176 ASSERT3P(zp
->z_dirlocks
, ==, NULL
);
177 ASSERT3P(zp
->z_acl_cached
, ==, NULL
);
178 ASSERT3P(zp
->z_xattr_cached
, ==, NULL
);
181 zp
->z_atime_dirty
= 0;
182 zp
->z_zn_prefetch
= 0;
198 zp
->z_is_zvol
= B_FALSE
;
199 zp
->z_is_mapped
= B_FALSE
;
200 zp
->z_is_ctldir
= B_TRUE
;
201 zp
->z_is_sa
= B_FALSE
;
202 zp
->z_is_stale
= B_FALSE
;
204 ip
->i_mode
= (S_IFDIR
| S_IRUGO
| S_IXUGO
);
205 ip
->i_uid
= SUID_TO_KUID(0);
206 ip
->i_gid
= SGID_TO_KGID(0);
207 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
214 if (insert_inode_locked(ip
)) {
215 unlock_new_inode(ip
);
220 mutex_enter(&zsb
->z_znodes_lock
);
221 list_insert_tail(&zsb
->z_all_znodes
, zp
);
224 mutex_exit(&zsb
->z_znodes_lock
);
226 unlock_new_inode(ip
);
232 * Lookup the inode with given id, it will be allocated if needed.
234 static struct inode
*
235 zfsctl_inode_lookup(zfs_sb_t
*zsb
, uint64_t id
,
236 const struct file_operations
*fops
, const struct inode_operations
*ops
)
238 struct inode
*ip
= NULL
;
241 ip
= ilookup(zsb
->z_sb
, (unsigned long)id
);
245 /* May fail due to concurrent zfsctl_inode_alloc() */
246 ip
= zfsctl_inode_alloc(zsb
, id
, fops
, ops
);
253 * Free zfsctl inode specific structures, currently there are none.
256 zfsctl_inode_destroy(struct inode
*ip
)
262 * An inode is being evicted from the cache.
265 zfsctl_inode_inactive(struct inode
*ip
)
267 if (zfsctl_is_snapdir(ip
))
268 zfsctl_snapdir_inactive(ip
);
272 * Create the '.zfs' directory. This directory is cached as part of the VFS
273 * structure. This results in a hold on the zfs_sb_t. The code in zfs_umount()
274 * therefore checks against a vfs_count of 2 instead of 1. This reference
275 * is removed when the ctldir is destroyed in the unmount. All other entities
276 * under the '.zfs' directory are created dynamically as needed.
278 * Because the dynamically created '.zfs' directory entries assume the use
279 * of 64-bit inode numbers this support must be disabled on 32-bit systems.
282 zfsctl_create(zfs_sb_t
*zsb
)
284 #if defined(CONFIG_64BIT)
285 ASSERT(zsb
->z_ctldir
== NULL
);
287 zsb
->z_ctldir
= zfsctl_inode_alloc(zsb
, ZFSCTL_INO_ROOT
,
288 &zpl_fops_root
, &zpl_ops_root
);
289 if (zsb
->z_ctldir
== NULL
)
290 return (SET_ERROR(ENOENT
));
294 return (SET_ERROR(EOPNOTSUPP
));
295 #endif /* CONFIG_64BIT */
299 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
302 zfsctl_destroy(zfs_sb_t
*zsb
)
305 zsb
->z_ctldir
= NULL
;
309 * Given a root znode, retrieve the associated .zfs directory.
310 * Add a hold to the vnode and return it.
313 zfsctl_root(znode_t
*zp
)
315 ASSERT(zfs_has_ctldir(zp
));
316 igrab(ZTOZSB(zp
)->z_ctldir
);
317 return (ZTOZSB(zp
)->z_ctldir
);
322 zfsctl_fid(struct inode
*ip
, fid_t
*fidp
)
324 znode_t
*zp
= ITOZ(ip
);
325 zfs_sb_t
*zsb
= ITOZSB(ip
);
326 uint64_t object
= zp
->z_id
;
332 if (fidp
->fid_len
< SHORT_FID_LEN
) {
333 fidp
->fid_len
= SHORT_FID_LEN
;
335 return (SET_ERROR(ENOSPC
));
338 zfid
= (zfid_short_t
*)fidp
;
340 zfid
->zf_len
= SHORT_FID_LEN
;
342 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
343 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
345 /* .zfs znodes always have a generation number of 0 */
346 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
354 zfsctl_snapshot_zname(struct inode
*ip
, const char *name
, int len
, char *zname
)
356 objset_t
*os
= ITOZSB(ip
)->z_os
;
358 if (snapshot_namecheck(name
, NULL
, NULL
) != 0)
359 return (SET_ERROR(EILSEQ
));
361 dmu_objset_name(os
, zname
);
362 if ((strlen(zname
) + 1 + strlen(name
)) >= len
)
363 return (SET_ERROR(ENAMETOOLONG
));
365 (void) strcat(zname
, "@");
366 (void) strcat(zname
, name
);
372 zfsctl_snapshot_zpath(struct path
*path
, int len
, char *zpath
)
374 char *path_buffer
, *path_ptr
;
375 int path_len
, error
= 0;
377 path_buffer
= kmem_alloc(len
, KM_SLEEP
);
379 path_ptr
= d_path(path
, path_buffer
, len
);
380 if (IS_ERR(path_ptr
)) {
381 error
= -PTR_ERR(path_ptr
);
385 path_len
= path_buffer
+ len
- 1 - path_ptr
;
386 if (path_len
> len
) {
387 error
= SET_ERROR(EFAULT
);
391 memcpy(zpath
, path_ptr
, path_len
);
392 zpath
[path_len
] = '\0';
394 kmem_free(path_buffer
, len
);
400 * Special case the handling of "..".
404 zfsctl_root_lookup(struct inode
*dip
, char *name
, struct inode
**ipp
,
405 int flags
, cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
407 zfs_sb_t
*zsb
= ITOZSB(dip
);
412 if (strcmp(name
, "..") == 0) {
413 *ipp
= dip
->i_sb
->s_root
->d_inode
;
414 } else if (strcmp(name
, ZFS_SNAPDIR_NAME
) == 0) {
415 *ipp
= zfsctl_inode_lookup(zsb
, ZFSCTL_INO_SNAPDIR
,
416 &zpl_fops_snapdir
, &zpl_ops_snapdir
);
417 } else if (strcmp(name
, ZFS_SHAREDIR_NAME
) == 0) {
418 *ipp
= zfsctl_inode_lookup(zsb
, ZFSCTL_INO_SHARES
,
419 &zpl_fops_shares
, &zpl_ops_shares
);
425 error
= SET_ERROR(ENOENT
);
433 * Lookup entry point for the 'snapshot' directory. Try to open the
434 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
435 * Perform a mount of the associated dataset on top of the inode.
439 zfsctl_snapdir_lookup(struct inode
*dip
, char *name
, struct inode
**ipp
,
440 int flags
, cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
442 zfs_sb_t
*zsb
= ITOZSB(dip
);
448 error
= dmu_snapshot_lookup(zsb
->z_os
, name
, &id
);
454 *ipp
= zfsctl_inode_lookup(zsb
, ZFSCTL_INO_SNAPDIRS
- id
,
455 &simple_dir_operations
, &simple_dir_inode_operations
);
457 #ifdef HAVE_AUTOMOUNT
458 (*ipp
)->i_flags
|= S_AUTOMOUNT
;
459 #endif /* HAVE_AUTOMOUNT */
461 error
= SET_ERROR(ENOENT
);
470 zfsctl_rename_snap(zfs_sb_t
*zsb
, zfs_snapentry_t
*sep
, const char *name
)
474 ASSERT(MUTEX_HELD(&zsb
->z_ctldir_lock
));
478 * Change the name in the AVL tree.
480 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
481 (void) strcpy(sep
->se_name
, name
);
482 VERIFY(avl_find(&zsb
->z_ctldir_snaps
, sep
, &where
) == NULL
);
483 avl_insert(&zsb
->z_ctldir_snaps
, sep
, where
);
487 * Renaming a directory under '.zfs/snapshot' will automatically trigger
488 * a rename of the snapshot to the new given name. The rename is confined
489 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
493 zfsctl_snapdir_rename(struct inode
*sdip
, char *snm
,
494 struct inode
*tdip
, char *tnm
, cred_t
*cr
, int flags
)
496 zfs_sb_t
*zsb
= ITOZSB(sdip
);
497 zfs_snapentry_t search
, *sep
;
499 char *to
, *from
, *real
, *fsname
;
504 to
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
505 from
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
506 real
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
507 fsname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
509 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
) {
510 error
= dmu_snapshot_realname(zsb
->z_os
, snm
, real
,
514 } else if (error
!= ENOTSUP
) {
519 dmu_objset_name(zsb
->z_os
, fsname
);
521 error
= zfsctl_snapshot_zname(sdip
, snm
, MAXNAMELEN
, from
);
523 error
= zfsctl_snapshot_zname(tdip
, tnm
, MAXNAMELEN
, to
);
525 error
= zfs_secpolicy_rename_perms(from
, to
, cr
);
530 * Cannot move snapshots out of the snapdir.
533 error
= SET_ERROR(EINVAL
);
538 * No-op when names are identical.
540 if (strcmp(snm
, tnm
) == 0) {
545 mutex_enter(&zsb
->z_ctldir_lock
);
547 error
= dsl_dataset_rename_snapshot(fsname
, snm
, tnm
, B_FALSE
);
551 search
.se_name
= (char *)snm
;
552 sep
= avl_find(&zsb
->z_ctldir_snaps
, &search
, &where
);
554 zfsctl_rename_snap(zsb
, sep
, tnm
);
557 mutex_exit(&zsb
->z_ctldir_lock
);
559 kmem_free(from
, MAXNAMELEN
);
560 kmem_free(to
, MAXNAMELEN
);
561 kmem_free(real
, MAXNAMELEN
);
562 kmem_free(fsname
, MAXNAMELEN
);
570 * Removing a directory under '.zfs/snapshot' will automatically trigger
571 * the removal of the snapshot with the given name.
575 zfsctl_snapdir_remove(struct inode
*dip
, char *name
, cred_t
*cr
, int flags
)
577 zfs_sb_t
*zsb
= ITOZSB(dip
);
578 char *snapname
, *real
;
583 snapname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
584 real
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
586 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
) {
587 error
= dmu_snapshot_realname(zsb
->z_os
, name
, real
,
591 } else if (error
!= ENOTSUP
) {
596 error
= zfsctl_snapshot_zname(dip
, name
, MAXNAMELEN
, snapname
);
598 error
= zfs_secpolicy_destroy_perms(snapname
, cr
);
602 error
= zfsctl_unmount_snapshot(zsb
, name
, MNT_FORCE
);
603 if ((error
== 0) || (error
== ENOENT
))
604 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
606 kmem_free(snapname
, MAXNAMELEN
);
607 kmem_free(real
, MAXNAMELEN
);
615 * Creating a directory under '.zfs/snapshot' will automatically trigger
616 * the creation of a new snapshot with the given name.
620 zfsctl_snapdir_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
,
621 struct inode
**ipp
, cred_t
*cr
, int flags
)
623 zfs_sb_t
*zsb
= ITOZSB(dip
);
627 dsname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
629 if (snapshot_namecheck(dirname
, NULL
, NULL
) != 0) {
630 error
= SET_ERROR(EILSEQ
);
634 dmu_objset_name(zsb
->z_os
, dsname
);
636 error
= zfs_secpolicy_snapshot_perms(dsname
, cr
);
641 error
= dmu_objset_snapshot_one(dsname
, dirname
);
645 error
= zfsctl_snapdir_lookup(dip
, dirname
, ipp
,
649 kmem_free(dsname
, MAXNAMELEN
);
655 * When a .zfs/snapshot/<snapshot> inode is evicted they must be removed
656 * from the snapshot list. This will normally happen as part of the auto
657 * unmount, however in the case of a manual snapshot unmount this will be
658 * the only notification we receive.
661 zfsctl_snapdir_inactive(struct inode
*ip
)
663 zfs_sb_t
*zsb
= ITOZSB(ip
);
664 zfs_snapentry_t
*sep
, *next
;
666 mutex_enter(&zsb
->z_ctldir_lock
);
668 sep
= avl_first(&zsb
->z_ctldir_snaps
);
669 while (sep
!= NULL
) {
670 next
= AVL_NEXT(&zsb
->z_ctldir_snaps
, sep
);
672 if (sep
->se_inode
== ip
) {
673 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
674 taskq_cancel_id(zfs_expire_taskq
, sep
->se_taskqid
);
675 zfsctl_sep_free(sep
);
681 mutex_exit(&zsb
->z_ctldir_lock
);
685 * Attempt to unmount a snapshot by making a call to user space.
686 * There is no assurance that this can or will succeed, is just a
687 * best effort. In the case where it does fail, perhaps because
688 * it's in use, the unmount will fail harmlessly.
690 #define SET_UNMOUNT_CMD \
691 "exec 0</dev/null " \
694 "umount -t zfs -n %s'%s'"
697 __zfsctl_unmount_snapshot(zfs_snapentry_t
*sep
, int flags
)
699 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
700 char *envp
[] = { NULL
};
703 argv
[2] = kmem_asprintf(SET_UNMOUNT_CMD
,
704 flags
& MNT_FORCE
? "-f " : "", sep
->se_path
);
705 error
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
709 * The umount system utility will return 256 on error. We must
710 * assume this error is because the file system is busy so it is
711 * converted to the more sensible EBUSY.
714 error
= SET_ERROR(EBUSY
);
717 * This was the result of a manual unmount, cancel the delayed work
718 * to prevent zfsctl_expire_snapshot() from attempting a unmount.
720 if ((error
== 0) && !(flags
& MNT_EXPIRE
))
721 taskq_cancel_id(zfs_expire_taskq
, sep
->se_taskqid
);
728 zfsctl_unmount_snapshot(zfs_sb_t
*zsb
, char *name
, int flags
)
730 zfs_snapentry_t search
;
731 zfs_snapentry_t
*sep
;
734 mutex_enter(&zsb
->z_ctldir_lock
);
736 search
.se_name
= name
;
737 sep
= avl_find(&zsb
->z_ctldir_snaps
, &search
, NULL
);
739 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
740 mutex_exit(&zsb
->z_ctldir_lock
);
742 error
= __zfsctl_unmount_snapshot(sep
, flags
);
744 mutex_enter(&zsb
->z_ctldir_lock
);
746 avl_add(&zsb
->z_ctldir_snaps
, sep
);
748 zfsctl_sep_free(sep
);
750 error
= SET_ERROR(ENOENT
);
753 mutex_exit(&zsb
->z_ctldir_lock
);
754 ASSERT3S(error
, >=, 0);
760 * Traverse all mounted snapshots and attempt to unmount them. This
761 * is best effort, on failure EEXIST is returned and count will be set
762 * to the number of file snapshots which could not be unmounted.
765 zfsctl_unmount_snapshots(zfs_sb_t
*zsb
, int flags
, int *count
)
767 zfs_snapentry_t
*sep
, *next
;
772 ASSERT(zsb
->z_ctldir
!= NULL
);
773 mutex_enter(&zsb
->z_ctldir_lock
);
775 sep
= avl_first(&zsb
->z_ctldir_snaps
);
776 while (sep
!= NULL
) {
777 next
= AVL_NEXT(&zsb
->z_ctldir_snaps
, sep
);
778 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
779 mutex_exit(&zsb
->z_ctldir_lock
);
781 error
= __zfsctl_unmount_snapshot(sep
, flags
);
783 mutex_enter(&zsb
->z_ctldir_lock
);
784 if (error
== EBUSY
) {
785 avl_add(&zsb
->z_ctldir_snaps
, sep
);
788 zfsctl_sep_free(sep
);
794 mutex_exit(&zsb
->z_ctldir_lock
);
796 return ((*count
> 0) ? EEXIST
: 0);
799 #define SET_MOUNT_CMD \
800 "exec 0</dev/null " \
803 "mount -t zfs -n '%s' '%s'"
806 zfsctl_mount_snapshot(struct path
*path
, int flags
)
808 struct dentry
*dentry
= path
->dentry
;
809 struct inode
*ip
= dentry
->d_inode
;
810 zfs_sb_t
*zsb
= ITOZSB(ip
);
811 char *full_name
, *full_path
;
812 zfs_snapentry_t
*sep
;
813 zfs_snapentry_t search
;
814 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
815 char *envp
[] = { NULL
};
820 full_name
= kmem_zalloc(MAXNAMELEN
, KM_SLEEP
);
821 full_path
= kmem_zalloc(PATH_MAX
, KM_SLEEP
);
823 error
= zfsctl_snapshot_zname(ip
, dname(dentry
), MAXNAMELEN
, full_name
);
827 error
= zfsctl_snapshot_zpath(path
, PATH_MAX
, full_path
);
832 * Attempt to mount the snapshot from user space. Normally this
833 * would be done using the vfs_kern_mount() function, however that
834 * function is marked GPL-only and cannot be used. On error we
835 * careful to log the real error to the console and return EISDIR
836 * to safely abort the automount. This should be very rare.
838 argv
[2] = kmem_asprintf(SET_MOUNT_CMD
, full_name
, full_path
);
839 error
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
842 printk("ZFS: Unable to automount %s at %s: %d\n",
843 full_name
, full_path
, error
);
844 error
= SET_ERROR(EISDIR
);
848 mutex_enter(&zsb
->z_ctldir_lock
);
851 * Ensure a previous entry does not exist, if it does safely remove
852 * it any cancel the outstanding expiration. This can occur when a
853 * snapshot is manually unmounted and then an automount is triggered.
855 search
.se_name
= full_name
;
856 sep
= avl_find(&zsb
->z_ctldir_snaps
, &search
, NULL
);
858 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
859 taskq_cancel_id(zfs_expire_taskq
, sep
->se_taskqid
);
860 zfsctl_sep_free(sep
);
863 sep
= zfsctl_sep_alloc();
864 sep
->se_name
= full_name
;
865 sep
->se_path
= full_path
;
867 avl_add(&zsb
->z_ctldir_snaps
, sep
);
869 sep
->se_taskqid
= taskq_dispatch_delay(zfs_expire_taskq
,
870 zfsctl_expire_snapshot
, sep
, TQ_SLEEP
,
871 ddi_get_lbolt() + zfs_expire_snapshot
* HZ
);
873 mutex_exit(&zsb
->z_ctldir_lock
);
876 kmem_free(full_name
, MAXNAMELEN
);
877 kmem_free(full_path
, PATH_MAX
);
886 * Check if this super block has a matching objset id.
889 zfsctl_test_super(struct super_block
*sb
, void *objsetidp
)
891 zfs_sb_t
*zsb
= sb
->s_fs_info
;
892 uint64_t objsetid
= *(uint64_t *)objsetidp
;
894 return (dmu_objset_id(zsb
->z_os
) == objsetid
);
898 * Prevent a new super block from being allocated if an existing one
899 * could not be located. We only want to preform a lookup operation.
902 zfsctl_set_super(struct super_block
*sb
, void *objsetidp
)
908 zfsctl_lookup_objset(struct super_block
*sb
, uint64_t objsetid
, zfs_sb_t
**zsbp
)
910 zfs_sb_t
*zsb
= sb
->s_fs_info
;
911 struct super_block
*sbp
;
912 zfs_snapentry_t
*sep
;
916 ASSERT(zsb
->z_ctldir
!= NULL
);
918 mutex_enter(&zsb
->z_ctldir_lock
);
921 * Verify that the snapshot is mounted.
923 sep
= avl_first(&zsb
->z_ctldir_snaps
);
924 while (sep
!= NULL
) {
925 error
= dmu_snapshot_lookup(zsb
->z_os
, sep
->se_name
, &id
);
932 sep
= AVL_NEXT(&zsb
->z_ctldir_snaps
, sep
);
937 * Lookup the mounted root rather than the covered mount
938 * point. This may fail if the snapshot has just been
939 * unmounted by an unrelated user space process. This
940 * race cannot occur to an expired mount point because
941 * we hold the zsb->z_ctldir_lock to prevent the race.
943 sbp
= zpl_sget(&zpl_fs_type
, zfsctl_test_super
,
944 zfsctl_set_super
, 0, &id
);
946 error
= -PTR_ERR(sbp
);
948 *zsbp
= sbp
->s_fs_info
;
949 deactivate_super(sbp
);
952 error
= SET_ERROR(EINVAL
);
955 mutex_exit(&zsb
->z_ctldir_lock
);
956 ASSERT3S(error
, >=, 0);
963 zfsctl_shares_lookup(struct inode
*dip
, char *name
, struct inode
**ipp
,
964 int flags
, cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
966 zfs_sb_t
*zsb
= ITOZSB(dip
);
973 if (zsb
->z_shares_dir
== 0) {
975 return (SET_ERROR(ENOTSUP
));
978 error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
984 error
= zfs_lookup(ZTOI(dzp
), name
, &ip
, 0, cr
, NULL
, NULL
);
994 * Initialize the various pieces we'll need to create and manipulate .zfs
995 * directories. Currently this is unused but available.
1000 zfs_expire_taskq
= taskq_create("z_unmount", 1, maxclsyspri
,
1001 1, 8, TASKQ_PREPOPULATE
);
1005 * Cleanup the various pieces we needed for .zfs directories. In particular
1006 * ensure the expiry timer is canceled safely.
1011 taskq_destroy(zfs_expire_taskq
);
1014 module_param(zfs_expire_snapshot
, int, 0644);
1015 MODULE_PARM_DESC(zfs_expire_snapshot
, "Seconds to expire .zfs/snapshot");