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
)
261 * An inode is being evicted from the cache.
264 zfsctl_inode_inactive(struct inode
*ip
)
266 if (zfsctl_is_snapdir(ip
))
267 zfsctl_snapdir_inactive(ip
);
271 * Create the '.zfs' directory. This directory is cached as part of the VFS
272 * structure. This results in a hold on the zfs_sb_t. The code in zfs_umount()
273 * therefore checks against a vfs_count of 2 instead of 1. This reference
274 * is removed when the ctldir is destroyed in the unmount. All other entities
275 * under the '.zfs' directory are created dynamically as needed.
277 * Because the dynamically created '.zfs' directory entries assume the use
278 * of 64-bit inode numbers this support must be disabled on 32-bit systems.
281 zfsctl_create(zfs_sb_t
*zsb
)
283 #if defined(CONFIG_64BIT)
284 ASSERT(zsb
->z_ctldir
== NULL
);
286 zsb
->z_ctldir
= zfsctl_inode_alloc(zsb
, ZFSCTL_INO_ROOT
,
287 &zpl_fops_root
, &zpl_ops_root
);
288 if (zsb
->z_ctldir
== NULL
)
289 return (SET_ERROR(ENOENT
));
293 return (SET_ERROR(EOPNOTSUPP
));
294 #endif /* CONFIG_64BIT */
298 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
301 zfsctl_destroy(zfs_sb_t
*zsb
)
304 zsb
->z_ctldir
= NULL
;
308 * Given a root znode, retrieve the associated .zfs directory.
309 * Add a hold to the vnode and return it.
312 zfsctl_root(znode_t
*zp
)
314 ASSERT(zfs_has_ctldir(zp
));
315 igrab(ZTOZSB(zp
)->z_ctldir
);
316 return (ZTOZSB(zp
)->z_ctldir
);
321 zfsctl_fid(struct inode
*ip
, fid_t
*fidp
)
323 znode_t
*zp
= ITOZ(ip
);
324 zfs_sb_t
*zsb
= ITOZSB(ip
);
325 uint64_t object
= zp
->z_id
;
331 if (fidp
->fid_len
< SHORT_FID_LEN
) {
332 fidp
->fid_len
= SHORT_FID_LEN
;
334 return (SET_ERROR(ENOSPC
));
337 zfid
= (zfid_short_t
*)fidp
;
339 zfid
->zf_len
= SHORT_FID_LEN
;
341 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
342 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
344 /* .zfs znodes always have a generation number of 0 */
345 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
353 zfsctl_snapshot_zname(struct inode
*ip
, const char *name
, int len
, char *zname
)
355 objset_t
*os
= ITOZSB(ip
)->z_os
;
357 if (zfs_component_namecheck(name
, NULL
, NULL
) != 0)
358 return (SET_ERROR(EILSEQ
));
360 dmu_objset_name(os
, zname
);
361 if ((strlen(zname
) + 1 + strlen(name
)) >= len
)
362 return (SET_ERROR(ENAMETOOLONG
));
364 (void) strcat(zname
, "@");
365 (void) strcat(zname
, name
);
371 * Gets the full dataset name that corresponds to the given snapshot name
373 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
376 zfsctl_snapshot_zpath(struct path
*path
, int len
, char *zpath
)
378 char *path_buffer
, *path_ptr
;
379 int path_len
, error
= 0;
381 path_buffer
= kmem_alloc(len
, KM_SLEEP
);
383 path_ptr
= d_path(path
, path_buffer
, len
);
384 if (IS_ERR(path_ptr
)) {
385 error
= -PTR_ERR(path_ptr
);
389 path_len
= path_buffer
+ len
- 1 - path_ptr
;
390 if (path_len
> len
) {
391 error
= SET_ERROR(EFAULT
);
395 memcpy(zpath
, path_ptr
, path_len
);
396 zpath
[path_len
] = '\0';
398 kmem_free(path_buffer
, len
);
404 * Special case the handling of "..".
408 zfsctl_root_lookup(struct inode
*dip
, char *name
, struct inode
**ipp
,
409 int flags
, cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
411 zfs_sb_t
*zsb
= ITOZSB(dip
);
416 if (strcmp(name
, "..") == 0) {
417 *ipp
= dip
->i_sb
->s_root
->d_inode
;
418 } else if (strcmp(name
, ZFS_SNAPDIR_NAME
) == 0) {
419 *ipp
= zfsctl_inode_lookup(zsb
, ZFSCTL_INO_SNAPDIR
,
420 &zpl_fops_snapdir
, &zpl_ops_snapdir
);
421 } else if (strcmp(name
, ZFS_SHAREDIR_NAME
) == 0) {
422 *ipp
= zfsctl_inode_lookup(zsb
, ZFSCTL_INO_SHARES
,
423 &zpl_fops_shares
, &zpl_ops_shares
);
429 error
= SET_ERROR(ENOENT
);
437 * Lookup entry point for the 'snapshot' directory. Try to open the
438 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
439 * Perform a mount of the associated dataset on top of the inode.
443 zfsctl_snapdir_lookup(struct inode
*dip
, char *name
, struct inode
**ipp
,
444 int flags
, cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
446 zfs_sb_t
*zsb
= ITOZSB(dip
);
452 error
= dmu_snapshot_lookup(zsb
->z_os
, name
, &id
);
458 *ipp
= zfsctl_inode_lookup(zsb
, ZFSCTL_INO_SNAPDIRS
- id
,
459 &simple_dir_operations
, &simple_dir_inode_operations
);
461 #ifdef HAVE_AUTOMOUNT
462 (*ipp
)->i_flags
|= S_AUTOMOUNT
;
463 #endif /* HAVE_AUTOMOUNT */
465 error
= SET_ERROR(ENOENT
);
474 zfsctl_rename_snap(zfs_sb_t
*zsb
, zfs_snapentry_t
*sep
, const char *name
)
478 ASSERT(MUTEX_HELD(&zsb
->z_ctldir_lock
));
482 * Change the name in the AVL tree.
484 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
485 (void) strcpy(sep
->se_name
, name
);
486 VERIFY(avl_find(&zsb
->z_ctldir_snaps
, sep
, &where
) == NULL
);
487 avl_insert(&zsb
->z_ctldir_snaps
, sep
, where
);
491 * Renaming a directory under '.zfs/snapshot' will automatically trigger
492 * a rename of the snapshot to the new given name. The rename is confined
493 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
497 zfsctl_snapdir_rename(struct inode
*sdip
, char *snm
,
498 struct inode
*tdip
, char *tnm
, cred_t
*cr
, int flags
)
500 zfs_sb_t
*zsb
= ITOZSB(sdip
);
501 zfs_snapentry_t search
, *sep
;
503 char *to
, *from
, *real
, *fsname
;
508 to
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
509 from
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
510 real
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
511 fsname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
513 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
) {
514 error
= dmu_snapshot_realname(zsb
->z_os
, snm
, real
,
518 } else if (error
!= ENOTSUP
) {
523 dmu_objset_name(zsb
->z_os
, fsname
);
525 error
= zfsctl_snapshot_zname(sdip
, snm
, MAXNAMELEN
, from
);
527 error
= zfsctl_snapshot_zname(tdip
, tnm
, MAXNAMELEN
, to
);
529 error
= zfs_secpolicy_rename_perms(from
, to
, cr
);
534 * Cannot move snapshots out of the snapdir.
537 error
= SET_ERROR(EINVAL
);
542 * No-op when names are identical.
544 if (strcmp(snm
, tnm
) == 0) {
549 mutex_enter(&zsb
->z_ctldir_lock
);
551 error
= dsl_dataset_rename_snapshot(fsname
, snm
, tnm
, B_FALSE
);
555 search
.se_name
= (char *)snm
;
556 sep
= avl_find(&zsb
->z_ctldir_snaps
, &search
, &where
);
558 zfsctl_rename_snap(zsb
, sep
, tnm
);
561 mutex_exit(&zsb
->z_ctldir_lock
);
563 kmem_free(from
, MAXNAMELEN
);
564 kmem_free(to
, MAXNAMELEN
);
565 kmem_free(real
, MAXNAMELEN
);
566 kmem_free(fsname
, MAXNAMELEN
);
574 * Removing a directory under '.zfs/snapshot' will automatically trigger
575 * the removal of the snapshot with the given name.
579 zfsctl_snapdir_remove(struct inode
*dip
, char *name
, cred_t
*cr
, int flags
)
581 zfs_sb_t
*zsb
= ITOZSB(dip
);
582 char *snapname
, *real
;
587 snapname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
588 real
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
590 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
) {
591 error
= dmu_snapshot_realname(zsb
->z_os
, name
, real
,
595 } else if (error
!= ENOTSUP
) {
600 error
= zfsctl_snapshot_zname(dip
, name
, MAXNAMELEN
, snapname
);
602 error
= zfs_secpolicy_destroy_perms(snapname
, cr
);
606 error
= zfsctl_unmount_snapshot(zsb
, name
, MNT_FORCE
);
607 if ((error
== 0) || (error
== ENOENT
))
608 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
610 kmem_free(snapname
, MAXNAMELEN
);
611 kmem_free(real
, MAXNAMELEN
);
619 * Creating a directory under '.zfs/snapshot' will automatically trigger
620 * the creation of a new snapshot with the given name.
624 zfsctl_snapdir_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
,
625 struct inode
**ipp
, cred_t
*cr
, int flags
)
627 zfs_sb_t
*zsb
= ITOZSB(dip
);
631 dsname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
633 if (zfs_component_namecheck(dirname
, NULL
, NULL
) != 0) {
634 error
= SET_ERROR(EILSEQ
);
638 dmu_objset_name(zsb
->z_os
, dsname
);
640 error
= zfs_secpolicy_snapshot_perms(dsname
, cr
);
645 error
= dmu_objset_snapshot_one(dsname
, dirname
);
649 error
= zfsctl_snapdir_lookup(dip
, dirname
, ipp
,
653 kmem_free(dsname
, MAXNAMELEN
);
659 * When a .zfs/snapshot/<snapshot> inode is evicted they must be removed
660 * from the snapshot list. This will normally happen as part of the auto
661 * unmount, however in the case of a manual snapshot unmount this will be
662 * the only notification we receive.
665 zfsctl_snapdir_inactive(struct inode
*ip
)
667 zfs_sb_t
*zsb
= ITOZSB(ip
);
668 zfs_snapentry_t
*sep
, *next
;
670 mutex_enter(&zsb
->z_ctldir_lock
);
672 sep
= avl_first(&zsb
->z_ctldir_snaps
);
673 while (sep
!= NULL
) {
674 next
= AVL_NEXT(&zsb
->z_ctldir_snaps
, sep
);
676 if (sep
->se_inode
== ip
) {
677 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
678 taskq_cancel_id(zfs_expire_taskq
, sep
->se_taskqid
);
679 zfsctl_sep_free(sep
);
685 mutex_exit(&zsb
->z_ctldir_lock
);
689 * Attempt to unmount a snapshot by making a call to user space.
690 * There is no assurance that this can or will succeed, is just a
691 * best effort. In the case where it does fail, perhaps because
692 * it's in use, the unmount will fail harmlessly.
694 #define SET_UNMOUNT_CMD \
695 "exec 0</dev/null " \
698 "umount -t zfs -n %s'%s'"
701 __zfsctl_unmount_snapshot(zfs_snapentry_t
*sep
, int flags
)
703 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
704 char *envp
[] = { NULL
};
707 argv
[2] = kmem_asprintf(SET_UNMOUNT_CMD
,
708 flags
& MNT_FORCE
? "-f " : "", sep
->se_path
);
709 error
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
713 * The umount system utility will return 256 on error. We must
714 * assume this error is because the file system is busy so it is
715 * converted to the more sensible EBUSY.
718 error
= SET_ERROR(EBUSY
);
721 * This was the result of a manual unmount, cancel the delayed work
722 * to prevent zfsctl_expire_snapshot() from attempting a unmount.
724 if ((error
== 0) && !(flags
& MNT_EXPIRE
))
725 taskq_cancel_id(zfs_expire_taskq
, sep
->se_taskqid
);
732 zfsctl_unmount_snapshot(zfs_sb_t
*zsb
, char *name
, int flags
)
734 zfs_snapentry_t search
;
735 zfs_snapentry_t
*sep
;
738 mutex_enter(&zsb
->z_ctldir_lock
);
740 search
.se_name
= name
;
741 sep
= avl_find(&zsb
->z_ctldir_snaps
, &search
, NULL
);
743 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
744 mutex_exit(&zsb
->z_ctldir_lock
);
746 error
= __zfsctl_unmount_snapshot(sep
, flags
);
748 mutex_enter(&zsb
->z_ctldir_lock
);
750 avl_add(&zsb
->z_ctldir_snaps
, sep
);
752 zfsctl_sep_free(sep
);
754 error
= SET_ERROR(ENOENT
);
757 mutex_exit(&zsb
->z_ctldir_lock
);
758 ASSERT3S(error
, >=, 0);
764 * Traverse all mounted snapshots and attempt to unmount them. This
765 * is best effort, on failure EEXIST is returned and count will be set
766 * to the number of file snapshots which could not be unmounted.
769 zfsctl_unmount_snapshots(zfs_sb_t
*zsb
, int flags
, int *count
)
771 zfs_snapentry_t
*sep
, *next
;
776 ASSERT(zsb
->z_ctldir
!= NULL
);
777 mutex_enter(&zsb
->z_ctldir_lock
);
779 sep
= avl_first(&zsb
->z_ctldir_snaps
);
780 while (sep
!= NULL
) {
781 next
= AVL_NEXT(&zsb
->z_ctldir_snaps
, sep
);
782 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
783 mutex_exit(&zsb
->z_ctldir_lock
);
785 error
= __zfsctl_unmount_snapshot(sep
, flags
);
787 mutex_enter(&zsb
->z_ctldir_lock
);
788 if (error
== EBUSY
) {
789 avl_add(&zsb
->z_ctldir_snaps
, sep
);
792 zfsctl_sep_free(sep
);
798 mutex_exit(&zsb
->z_ctldir_lock
);
800 return ((*count
> 0) ? EEXIST
: 0);
803 #define MOUNT_BUSY 0x80 /* Mount failed due to EBUSY (from mntent.h) */
805 #define SET_MOUNT_CMD \
806 "exec 0</dev/null " \
809 "mount -t zfs -n '%s' '%s'"
812 zfsctl_mount_snapshot(struct path
*path
, int flags
)
814 struct dentry
*dentry
= path
->dentry
;
815 struct inode
*ip
= dentry
->d_inode
;
816 zfs_sb_t
*zsb
= ITOZSB(ip
);
817 char *full_name
, *full_path
;
818 zfs_snapentry_t
*sep
;
819 zfs_snapentry_t search
;
820 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
821 char *envp
[] = { NULL
};
826 full_name
= kmem_zalloc(MAXNAMELEN
, KM_SLEEP
);
827 full_path
= kmem_zalloc(PATH_MAX
, KM_SLEEP
);
829 error
= zfsctl_snapshot_zname(ip
, dname(dentry
), MAXNAMELEN
, full_name
);
833 error
= zfsctl_snapshot_zpath(path
, PATH_MAX
, full_path
);
838 * Attempt to mount the snapshot from user space. Normally this
839 * would be done using the vfs_kern_mount() function, however that
840 * function is marked GPL-only and cannot be used. On error we
841 * careful to log the real error to the console and return EISDIR
842 * to safely abort the automount. This should be very rare.
844 * If the user mode helper happens to return EBUSY, a concurrent
845 * mount is already in progress in which case the error is ignored.
846 * Take note that if the program was executed successfully the return
847 * value from call_usermodehelper() will be (exitcode << 8 + signal).
849 argv
[2] = kmem_asprintf(SET_MOUNT_CMD
, full_name
, full_path
);
850 error
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
852 if (error
&& !(error
& MOUNT_BUSY
<< 8)) {
853 printk("ZFS: Unable to automount %s at %s: %d\n",
854 full_name
, full_path
, error
);
855 error
= SET_ERROR(EISDIR
);
860 mutex_enter(&zsb
->z_ctldir_lock
);
863 * Ensure a previous entry does not exist, if it does safely remove
864 * it any cancel the outstanding expiration. This can occur when a
865 * snapshot is manually unmounted and then an automount is triggered.
867 search
.se_name
= full_name
;
868 sep
= avl_find(&zsb
->z_ctldir_snaps
, &search
, NULL
);
870 avl_remove(&zsb
->z_ctldir_snaps
, sep
);
871 taskq_cancel_id(zfs_expire_taskq
, sep
->se_taskqid
);
872 zfsctl_sep_free(sep
);
875 sep
= zfsctl_sep_alloc();
876 sep
->se_name
= full_name
;
877 sep
->se_path
= full_path
;
879 avl_add(&zsb
->z_ctldir_snaps
, sep
);
881 sep
->se_taskqid
= taskq_dispatch_delay(zfs_expire_taskq
,
882 zfsctl_expire_snapshot
, sep
, TQ_SLEEP
,
883 ddi_get_lbolt() + zfs_expire_snapshot
* HZ
);
885 mutex_exit(&zsb
->z_ctldir_lock
);
888 kmem_free(full_name
, MAXNAMELEN
);
889 kmem_free(full_path
, PATH_MAX
);
898 * Check if this super block has a matching objset id.
901 zfsctl_test_super(struct super_block
*sb
, void *objsetidp
)
903 zfs_sb_t
*zsb
= sb
->s_fs_info
;
904 uint64_t objsetid
= *(uint64_t *)objsetidp
;
906 return (dmu_objset_id(zsb
->z_os
) == objsetid
);
910 * Prevent a new super block from being allocated if an existing one
911 * could not be located. We only want to preform a lookup operation.
914 zfsctl_set_super(struct super_block
*sb
, void *objsetidp
)
920 zfsctl_lookup_objset(struct super_block
*sb
, uint64_t objsetid
, zfs_sb_t
**zsbp
)
922 zfs_sb_t
*zsb
= sb
->s_fs_info
;
923 struct super_block
*sbp
;
924 zfs_snapentry_t
*sep
;
928 ASSERT(zsb
->z_ctldir
!= NULL
);
930 mutex_enter(&zsb
->z_ctldir_lock
);
933 * Verify that the snapshot is mounted.
935 sep
= avl_first(&zsb
->z_ctldir_snaps
);
936 while (sep
!= NULL
) {
937 error
= dmu_snapshot_lookup(zsb
->z_os
, sep
->se_name
, &id
);
944 sep
= AVL_NEXT(&zsb
->z_ctldir_snaps
, sep
);
949 * Lookup the mounted root rather than the covered mount
950 * point. This may fail if the snapshot has just been
951 * unmounted by an unrelated user space process. This
952 * race cannot occur to an expired mount point because
953 * we hold the zsb->z_ctldir_lock to prevent the race.
955 sbp
= zpl_sget(&zpl_fs_type
, zfsctl_test_super
,
956 zfsctl_set_super
, 0, &id
);
958 error
= -PTR_ERR(sbp
);
960 *zsbp
= sbp
->s_fs_info
;
961 deactivate_super(sbp
);
964 error
= SET_ERROR(EINVAL
);
967 mutex_exit(&zsb
->z_ctldir_lock
);
968 ASSERT3S(error
, >=, 0);
975 zfsctl_shares_lookup(struct inode
*dip
, char *name
, struct inode
**ipp
,
976 int flags
, cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
978 zfs_sb_t
*zsb
= ITOZSB(dip
);
985 if (zsb
->z_shares_dir
== 0) {
987 return (SET_ERROR(ENOTSUP
));
990 error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
996 error
= zfs_lookup(ZTOI(dzp
), name
, &ip
, 0, cr
, NULL
, NULL
);
1006 * Initialize the various pieces we'll need to create and manipulate .zfs
1007 * directories. Currently this is unused but available.
1012 zfs_expire_taskq
= taskq_create("z_unmount", 1, maxclsyspri
,
1013 1, 8, TASKQ_PREPOPULATE
);
1017 * Cleanup the various pieces we needed for .zfs directories. In particular
1018 * ensure the expiry timer is canceled safely.
1023 taskq_destroy(zfs_expire_taskq
);
1026 module_param(zfs_expire_snapshot
, int, 0644);
1027 MODULE_PARM_DESC(zfs_expire_snapshot
, "Seconds to expire .zfs/snapshot");