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.
31 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
32 * Copyright (c) 2018 George Melikov. All Rights Reserved.
36 * ZFS control directory (a.k.a. ".zfs")
38 * This directory provides a common location for all ZFS meta-objects.
39 * Currently, this is only the 'snapshot' and 'shares' directory, but this may
40 * expand in the future. The elements are built dynamically, as the hierarchy
41 * does not actually exist on disk.
43 * For 'snapshot', we don't want to have all snapshots always mounted, because
44 * this would take up a huge amount of space in /etc/mnttab. We have three
47 * ctldir ------> snapshotdir -------> snapshot
53 * The 'snapshot' node contains just enough information to lookup '..' and act
54 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
55 * perform an automount of the underlying filesystem and return the
56 * corresponding inode.
58 * All mounts are handled automatically by an user mode helper which invokes
59 * the mount procedure. Unmounts are handled by allowing the mount
60 * point to expire so the kernel may automatically unmount it.
62 * The '.zfs', '.zfs/snapshot', and all directories created under
63 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') all share the same
64 * zfsvfs_t as the head filesystem (what '.zfs' lives under).
66 * File systems mounted on top of the '.zfs/snapshot/<snapname>' paths
67 * (ie: snapshots) are complete ZFS filesystems and have their own unique
68 * zfsvfs_t. However, the fsid reported by these mounts will be the same
69 * as that used by the parent zfsvfs_t to make NFS happy.
72 #include <sys/types.h>
73 #include <sys/param.h>
75 #include <sys/sysmacros.h>
76 #include <sys/pathname.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/dmu_objset.h>
85 #include <sys/dsl_destroy.h>
86 #include <sys/dsl_deleg.h>
88 #include <sys/mntent.h>
89 #include "zfs_namecheck.h"
92 * Two AVL trees are maintained which contain all currently automounted
93 * snapshots. Every automounted snapshots maps to a single zfs_snapentry_t
96 * - be attached to both trees, and
97 * - be unique, no duplicate entries are allowed.
99 * The zfs_snapshots_by_name tree is indexed by the full dataset name
100 * while the zfs_snapshots_by_objsetid tree is indexed by the unique
101 * objsetid. This allows for fast lookups either by name or objsetid.
103 static avl_tree_t zfs_snapshots_by_name;
104 static avl_tree_t zfs_snapshots_by_objsetid;
105 static krwlock_t zfs_snapshot_lock;
108 * Control Directory Tunables (.zfs)
110 int zfs_expire_snapshot = ZFSCTL_EXPIRE_SNAPSHOT;
111 int zfs_admin_snapshot = 0;
114 char *se_name; /* full snapshot name */
115 char *se_path; /* full mount path */
116 spa_t *se_spa; /* pool spa */
117 uint64_t se_objsetid; /* snapshot objset id */
118 struct dentry *se_root_dentry; /* snapshot root dentry */
119 taskqid_t se_taskqid; /* scheduled unmount taskqid */
120 avl_node_t se_node_name; /* zfs_snapshots_by_name link */
121 avl_node_t se_node_objsetid; /* zfs_snapshots_by_objsetid link */
122 zfs_refcount_t se_refcount; /* reference count */
125 static void zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay);
128 * Allocate a new zfs_snapentry_t being careful to make a copy of the
129 * the snapshot name and provided mount point. No reference is taken.
131 static zfs_snapentry_t *
132 zfsctl_snapshot_alloc(char *full_name, char *full_path, spa_t *spa,
133 uint64_t objsetid, struct dentry *root_dentry)
137 se = kmem_zalloc(sizeof (zfs_snapentry_t), KM_SLEEP);
139 se->se_name = strdup(full_name);
140 se->se_path = strdup(full_path);
142 se->se_objsetid = objsetid;
143 se->se_root_dentry = root_dentry;
144 se->se_taskqid = TASKQID_INVALID;
146 zfs_refcount_create(&se->se_refcount);
152 * Free a zfs_snapentry_t the caller must ensure there are no active
156 zfsctl_snapshot_free(zfs_snapentry_t *se)
158 zfs_refcount_destroy(&se->se_refcount);
159 strfree(se->se_name);
160 strfree(se->se_path);
162 kmem_free(se, sizeof (zfs_snapentry_t));
166 * Hold a reference on the zfs_snapentry_t.
169 zfsctl_snapshot_hold(zfs_snapentry_t *se)
171 zfs_refcount_add(&se->se_refcount, NULL);
175 * Release a reference on the zfs_snapentry_t. When the number of
176 * references drops to zero the structure will be freed.
179 zfsctl_snapshot_rele(zfs_snapentry_t *se)
181 if (zfs_refcount_remove(&se->se_refcount, NULL) == 0)
182 zfsctl_snapshot_free(se);
186 * Add a zfs_snapentry_t to both the zfs_snapshots_by_name and
187 * zfs_snapshots_by_objsetid trees. While the zfs_snapentry_t is part
188 * of the trees a reference is held.
191 zfsctl_snapshot_add(zfs_snapentry_t *se)
193 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
194 zfs_refcount_add(&se->se_refcount, NULL);
195 avl_add(&zfs_snapshots_by_name, se);
196 avl_add(&zfs_snapshots_by_objsetid, se);
200 * Remove a zfs_snapentry_t from both the zfs_snapshots_by_name and
201 * zfs_snapshots_by_objsetid trees. Upon removal a reference is dropped,
202 * this can result in the structure being freed if that was the last
203 * remaining reference.
206 zfsctl_snapshot_remove(zfs_snapentry_t *se)
208 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
209 avl_remove(&zfs_snapshots_by_name, se);
210 avl_remove(&zfs_snapshots_by_objsetid, se);
211 zfsctl_snapshot_rele(se);
215 * Snapshot name comparison function for the zfs_snapshots_by_name.
218 snapentry_compare_by_name(const void *a, const void *b)
220 const zfs_snapentry_t *se_a = a;
221 const zfs_snapentry_t *se_b = b;
224 ret = strcmp(se_a->se_name, se_b->se_name);
235 * Snapshot name comparison function for the zfs_snapshots_by_objsetid.
238 snapentry_compare_by_objsetid(const void *a, const void *b)
240 const zfs_snapentry_t *se_a = a;
241 const zfs_snapentry_t *se_b = b;
243 if (se_a->se_spa != se_b->se_spa)
244 return ((ulong_t)se_a->se_spa < (ulong_t)se_b->se_spa ? -1 : 1);
246 if (se_a->se_objsetid < se_b->se_objsetid)
248 else if (se_a->se_objsetid > se_b->se_objsetid)
255 * Find a zfs_snapentry_t in zfs_snapshots_by_name. If the snapname
256 * is found a pointer to the zfs_snapentry_t is returned and a reference
257 * taken on the structure. The caller is responsible for dropping the
258 * reference with zfsctl_snapshot_rele(). If the snapname is not found
259 * NULL will be returned.
261 static zfs_snapentry_t *
262 zfsctl_snapshot_find_by_name(char *snapname)
264 zfs_snapentry_t *se, search;
266 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
268 search.se_name = snapname;
269 se = avl_find(&zfs_snapshots_by_name, &search, NULL);
271 zfs_refcount_add(&se->se_refcount, NULL);
277 * Find a zfs_snapentry_t in zfs_snapshots_by_objsetid given the objset id
278 * rather than the snapname. In all other respects it behaves the same
279 * as zfsctl_snapshot_find_by_name().
281 static zfs_snapentry_t *
282 zfsctl_snapshot_find_by_objsetid(spa_t *spa, uint64_t objsetid)
284 zfs_snapentry_t *se, search;
286 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
289 search.se_objsetid = objsetid;
290 se = avl_find(&zfs_snapshots_by_objsetid, &search, NULL);
292 zfs_refcount_add(&se->se_refcount, NULL);
298 * Rename a zfs_snapentry_t in the zfs_snapshots_by_name. The structure is
299 * removed, renamed, and added back to the new correct location in the tree.
302 zfsctl_snapshot_rename(char *old_snapname, char *new_snapname)
306 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
308 se = zfsctl_snapshot_find_by_name(old_snapname);
310 return (SET_ERROR(ENOENT));
312 zfsctl_snapshot_remove(se);
313 strfree(se->se_name);
314 se->se_name = strdup(new_snapname);
315 zfsctl_snapshot_add(se);
316 zfsctl_snapshot_rele(se);
322 * Delayed task responsible for unmounting an expired automounted snapshot.
325 snapentry_expire(void *data)
327 zfs_snapentry_t *se = (zfs_snapentry_t *)data;
328 spa_t *spa = se->se_spa;
329 uint64_t objsetid = se->se_objsetid;
331 if (zfs_expire_snapshot <= 0) {
332 zfsctl_snapshot_rele(se);
336 se->se_taskqid = TASKQID_INVALID;
337 (void) zfsctl_snapshot_unmount(se->se_name, MNT_EXPIRE);
338 zfsctl_snapshot_rele(se);
341 * Reschedule the unmount if the zfs_snapentry_t wasn't removed.
342 * This can occur when the snapshot is busy.
344 rw_enter(&zfs_snapshot_lock, RW_READER);
345 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
346 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot);
347 zfsctl_snapshot_rele(se);
349 rw_exit(&zfs_snapshot_lock);
353 * Cancel an automatic unmount of a snapname. This callback is responsible
354 * for dropping the reference on the zfs_snapentry_t which was taken when
358 zfsctl_snapshot_unmount_cancel(zfs_snapentry_t *se)
360 if (taskq_cancel_id(system_delay_taskq, se->se_taskqid) == 0) {
361 se->se_taskqid = TASKQID_INVALID;
362 zfsctl_snapshot_rele(se);
367 * Dispatch the unmount task for delayed handling with a hold protecting it.
370 zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay)
372 ASSERT3S(se->se_taskqid, ==, TASKQID_INVALID);
377 zfsctl_snapshot_hold(se);
378 se->se_taskqid = taskq_dispatch_delay(system_delay_taskq,
379 snapentry_expire, se, TQ_SLEEP, ddi_get_lbolt() + delay * HZ);
383 * Schedule an automatic unmount of objset id to occur in delay seconds from
384 * now. Any previous delayed unmount will be cancelled in favor of the
385 * updated deadline. A reference is taken by zfsctl_snapshot_find_by_name()
386 * and held until the outstanding task is handled or cancelled.
389 zfsctl_snapshot_unmount_delay(spa_t *spa, uint64_t objsetid, int delay)
394 rw_enter(&zfs_snapshot_lock, RW_READER);
395 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
396 zfsctl_snapshot_unmount_cancel(se);
397 zfsctl_snapshot_unmount_delay_impl(se, delay);
398 zfsctl_snapshot_rele(se);
401 rw_exit(&zfs_snapshot_lock);
407 * Check if snapname is currently mounted. Returned non-zero when mounted
408 * and zero when unmounted.
411 zfsctl_snapshot_ismounted(char *snapname)
414 boolean_t ismounted = B_FALSE;
416 rw_enter(&zfs_snapshot_lock, RW_READER);
417 if ((se = zfsctl_snapshot_find_by_name(snapname)) != NULL) {
418 zfsctl_snapshot_rele(se);
421 rw_exit(&zfs_snapshot_lock);
427 * Check if the given inode is a part of the virtual .zfs directory.
430 zfsctl_is_node(struct inode *ip)
432 return (ITOZ(ip)->z_is_ctldir);
436 * Check if the given inode is a .zfs/snapshots/snapname directory.
439 zfsctl_is_snapdir(struct inode *ip)
441 return (zfsctl_is_node(ip) && (ip->i_ino <= ZFSCTL_INO_SNAPDIRS));
445 * Allocate a new inode with the passed id and ops.
447 static struct inode *
448 zfsctl_inode_alloc(zfsvfs_t *zfsvfs, uint64_t id,
449 const struct file_operations *fops, const struct inode_operations *ops)
451 inode_timespec_t now;
455 ip = new_inode(zfsvfs->z_sb);
459 now = current_time(ip);
461 ASSERT3P(zp->z_dirlocks, ==, NULL);
462 ASSERT3P(zp->z_acl_cached, ==, NULL);
463 ASSERT3P(zp->z_xattr_cached, ==, NULL);
466 zp->z_atime_dirty = 0;
467 zp->z_zn_prefetch = 0;
477 zp->z_is_mapped = B_FALSE;
478 zp->z_is_ctldir = B_TRUE;
479 zp->z_is_sa = B_FALSE;
480 zp->z_is_stale = B_FALSE;
481 ip->i_generation = 0;
483 ip->i_mode = (S_IFDIR | S_IRWXUGO);
484 ip->i_uid = SUID_TO_KUID(0);
485 ip->i_gid = SGID_TO_KGID(0);
486 ip->i_blkbits = SPA_MINBLOCKSHIFT;
492 #if defined(IOP_XATTR)
493 ip->i_opflags &= ~IOP_XATTR;
496 if (insert_inode_locked(ip)) {
497 unlock_new_inode(ip);
502 mutex_enter(&zfsvfs->z_znodes_lock);
503 list_insert_tail(&zfsvfs->z_all_znodes, zp);
504 zfsvfs->z_nr_znodes++;
506 mutex_exit(&zfsvfs->z_znodes_lock);
508 unlock_new_inode(ip);
514 * Lookup the inode with given id, it will be allocated if needed.
516 static struct inode *
517 zfsctl_inode_lookup(zfsvfs_t *zfsvfs, uint64_t id,
518 const struct file_operations *fops, const struct inode_operations *ops)
520 struct inode *ip = NULL;
523 ip = ilookup(zfsvfs->z_sb, (unsigned long)id);
527 /* May fail due to concurrent zfsctl_inode_alloc() */
528 ip = zfsctl_inode_alloc(zfsvfs, id, fops, ops);
535 * Create the '.zfs' directory. This directory is cached as part of the VFS
536 * structure. This results in a hold on the zfsvfs_t. The code in zfs_umount()
537 * therefore checks against a vfs_count of 2 instead of 1. This reference
538 * is removed when the ctldir is destroyed in the unmount. All other entities
539 * under the '.zfs' directory are created dynamically as needed.
541 * Because the dynamically created '.zfs' directory entries assume the use
542 * of 64-bit inode numbers this support must be disabled on 32-bit systems.
545 zfsctl_create(zfsvfs_t *zfsvfs)
547 ASSERT(zfsvfs->z_ctldir == NULL);
549 zfsvfs->z_ctldir = zfsctl_inode_alloc(zfsvfs, ZFSCTL_INO_ROOT,
550 &zpl_fops_root, &zpl_ops_root);
551 if (zfsvfs->z_ctldir == NULL)
552 return (SET_ERROR(ENOENT));
558 * Destroy the '.zfs' directory or remove a snapshot from zfs_snapshots_by_name.
559 * Only called when the filesystem is unmounted.
562 zfsctl_destroy(zfsvfs_t *zfsvfs)
564 if (zfsvfs->z_issnap) {
566 spa_t *spa = zfsvfs->z_os->os_spa;
567 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
569 rw_enter(&zfs_snapshot_lock, RW_WRITER);
570 se = zfsctl_snapshot_find_by_objsetid(spa, objsetid);
572 zfsctl_snapshot_remove(se);
573 rw_exit(&zfs_snapshot_lock);
575 zfsctl_snapshot_unmount_cancel(se);
576 zfsctl_snapshot_rele(se);
578 } else if (zfsvfs->z_ctldir) {
579 iput(zfsvfs->z_ctldir);
580 zfsvfs->z_ctldir = NULL;
585 * Given a root znode, retrieve the associated .zfs directory.
586 * Add a hold to the vnode and return it.
589 zfsctl_root(znode_t *zp)
591 ASSERT(zfs_has_ctldir(zp));
592 igrab(ZTOZSB(zp)->z_ctldir);
593 return (ZTOZSB(zp)->z_ctldir);
597 * Generate a long fid to indicate a snapdir. We encode whether snapdir is
598 * already monunted in gen field. We do this because nfsd lookup will not
599 * trigger automount. Next time the nfsd does fh_to_dentry, we will notice
600 * this and do automount and return ESTALE to force nfsd revalidate and follow
604 zfsctl_snapdir_fid(struct inode *ip, fid_t *fidp)
606 zfid_short_t *zfid = (zfid_short_t *)fidp;
607 zfid_long_t *zlfid = (zfid_long_t *)fidp;
612 struct dentry *dentry;
614 if (fidp->fid_len < LONG_FID_LEN) {
615 fidp->fid_len = LONG_FID_LEN;
616 return (SET_ERROR(ENOSPC));
620 objsetid = ZFSCTL_INO_SNAPDIRS - ip->i_ino;
621 zfid->zf_len = LONG_FID_LEN;
623 dentry = d_obtain_alias(igrab(ip));
624 if (!IS_ERR(dentry)) {
625 gen = !!d_mountpoint(dentry);
629 for (i = 0; i < sizeof (zfid->zf_object); i++)
630 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
632 for (i = 0; i < sizeof (zfid->zf_gen); i++)
633 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
635 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
636 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
638 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
639 zlfid->zf_setgen[i] = 0;
645 * Generate an appropriate fid for an entry in the .zfs directory.
648 zfsctl_fid(struct inode *ip, fid_t *fidp)
650 znode_t *zp = ITOZ(ip);
651 zfsvfs_t *zfsvfs = ITOZSB(ip);
652 uint64_t object = zp->z_id;
658 if (zfsctl_is_snapdir(ip)) {
660 return (zfsctl_snapdir_fid(ip, fidp));
663 if (fidp->fid_len < SHORT_FID_LEN) {
664 fidp->fid_len = SHORT_FID_LEN;
666 return (SET_ERROR(ENOSPC));
669 zfid = (zfid_short_t *)fidp;
671 zfid->zf_len = SHORT_FID_LEN;
673 for (i = 0; i < sizeof (zfid->zf_object); i++)
674 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
676 /* .zfs znodes always have a generation number of 0 */
677 for (i = 0; i < sizeof (zfid->zf_gen); i++)
685 * Construct a full dataset name in full_name: "pool/dataset@snap_name"
688 zfsctl_snapshot_name(zfsvfs_t *zfsvfs, const char *snap_name, int len,
691 objset_t *os = zfsvfs->z_os;
693 if (zfs_component_namecheck(snap_name, NULL, NULL) != 0)
694 return (SET_ERROR(EILSEQ));
696 dmu_objset_name(os, full_name);
697 if ((strlen(full_name) + 1 + strlen(snap_name)) >= len)
698 return (SET_ERROR(ENAMETOOLONG));
700 (void) strcat(full_name, "@");
701 (void) strcat(full_name, snap_name);
707 * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/"
710 zfsctl_snapshot_path_objset(zfsvfs_t *zfsvfs, uint64_t objsetid,
711 int path_len, char *full_path)
713 objset_t *os = zfsvfs->z_os;
714 fstrans_cookie_t cookie;
716 boolean_t case_conflict;
717 uint64_t id, pos = 0;
720 if (zfsvfs->z_vfs->vfs_mntpoint == NULL)
721 return (SET_ERROR(ENOENT));
723 cookie = spl_fstrans_mark();
724 snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
727 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
728 error = dmu_snapshot_list_next(zfsvfs->z_os,
729 ZFS_MAX_DATASET_NAME_LEN, snapname, &id, &pos,
731 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
739 snprintf(full_path, path_len, "%s/.zfs/snapshot/%s",
740 zfsvfs->z_vfs->vfs_mntpoint, snapname);
742 kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN);
743 spl_fstrans_unmark(cookie);
749 * Special case the handling of "..".
752 zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp,
753 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
755 zfsvfs_t *zfsvfs = ITOZSB(dip);
760 if (strcmp(name, "..") == 0) {
761 *ipp = dip->i_sb->s_root->d_inode;
762 } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) {
763 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SNAPDIR,
764 &zpl_fops_snapdir, &zpl_ops_snapdir);
765 } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) {
766 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SHARES,
767 &zpl_fops_shares, &zpl_ops_shares);
773 error = SET_ERROR(ENOENT);
781 * Lookup entry point for the 'snapshot' directory. Try to open the
782 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
785 zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp,
786 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
788 zfsvfs_t *zfsvfs = ITOZSB(dip);
794 error = dmu_snapshot_lookup(zfsvfs->z_os, name, &id);
800 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SNAPDIRS - id,
801 &simple_dir_operations, &simple_dir_inode_operations);
803 error = SET_ERROR(ENOENT);
811 * Renaming a directory under '.zfs/snapshot' will automatically trigger
812 * a rename of the snapshot to the new given name. The rename is confined
813 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
816 zfsctl_snapdir_rename(struct inode *sdip, char *snm,
817 struct inode *tdip, char *tnm, cred_t *cr, int flags)
819 zfsvfs_t *zfsvfs = ITOZSB(sdip);
820 char *to, *from, *real, *fsname;
823 if (!zfs_admin_snapshot)
824 return (SET_ERROR(EACCES));
828 to = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
829 from = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
830 real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
831 fsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
833 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
834 error = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
835 ZFS_MAX_DATASET_NAME_LEN, NULL);
838 } else if (error != ENOTSUP) {
843 dmu_objset_name(zfsvfs->z_os, fsname);
845 error = zfsctl_snapshot_name(ITOZSB(sdip), snm,
846 ZFS_MAX_DATASET_NAME_LEN, from);
848 error = zfsctl_snapshot_name(ITOZSB(tdip), tnm,
849 ZFS_MAX_DATASET_NAME_LEN, to);
851 error = zfs_secpolicy_rename_perms(from, to, cr);
856 * Cannot move snapshots out of the snapdir.
859 error = SET_ERROR(EINVAL);
864 * No-op when names are identical.
866 if (strcmp(snm, tnm) == 0) {
871 rw_enter(&zfs_snapshot_lock, RW_WRITER);
873 error = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
875 (void) zfsctl_snapshot_rename(snm, tnm);
877 rw_exit(&zfs_snapshot_lock);
879 kmem_free(from, ZFS_MAX_DATASET_NAME_LEN);
880 kmem_free(to, ZFS_MAX_DATASET_NAME_LEN);
881 kmem_free(real, ZFS_MAX_DATASET_NAME_LEN);
882 kmem_free(fsname, ZFS_MAX_DATASET_NAME_LEN);
890 * Removing a directory under '.zfs/snapshot' will automatically trigger
891 * the removal of the snapshot with the given name.
894 zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags)
896 zfsvfs_t *zfsvfs = ITOZSB(dip);
897 char *snapname, *real;
900 if (!zfs_admin_snapshot)
901 return (SET_ERROR(EACCES));
905 snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
906 real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
908 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
909 error = dmu_snapshot_realname(zfsvfs->z_os, name, real,
910 ZFS_MAX_DATASET_NAME_LEN, NULL);
913 } else if (error != ENOTSUP) {
918 error = zfsctl_snapshot_name(ITOZSB(dip), name,
919 ZFS_MAX_DATASET_NAME_LEN, snapname);
921 error = zfs_secpolicy_destroy_perms(snapname, cr);
925 error = zfsctl_snapshot_unmount(snapname, MNT_FORCE);
926 if ((error == 0) || (error == ENOENT))
927 error = dsl_destroy_snapshot(snapname, B_FALSE);
929 kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN);
930 kmem_free(real, ZFS_MAX_DATASET_NAME_LEN);
938 * Creating a directory under '.zfs/snapshot' will automatically trigger
939 * the creation of a new snapshot with the given name.
942 zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap,
943 struct inode **ipp, cred_t *cr, int flags)
945 zfsvfs_t *zfsvfs = ITOZSB(dip);
949 if (!zfs_admin_snapshot)
950 return (SET_ERROR(EACCES));
952 dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
954 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) {
955 error = SET_ERROR(EILSEQ);
959 dmu_objset_name(zfsvfs->z_os, dsname);
961 error = zfs_secpolicy_snapshot_perms(dsname, cr);
966 error = dmu_objset_snapshot_one(dsname, dirname);
970 error = zfsctl_snapdir_lookup(dip, dirname, ipp,
974 kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN);
980 * Attempt to unmount a snapshot by making a call to user space.
981 * There is no assurance that this can or will succeed, is just a
982 * best effort. In the case where it does fail, perhaps because
983 * it's in use, the unmount will fail harmlessly.
986 zfsctl_snapshot_unmount(char *snapname, int flags)
988 char *argv[] = { "/usr/bin/env", "umount", "-t", "zfs", "-n", NULL,
990 char *envp[] = { NULL };
994 rw_enter(&zfs_snapshot_lock, RW_READER);
995 if ((se = zfsctl_snapshot_find_by_name(snapname)) == NULL) {
996 rw_exit(&zfs_snapshot_lock);
997 return (SET_ERROR(ENOENT));
999 rw_exit(&zfs_snapshot_lock);
1001 if (flags & MNT_FORCE)
1003 argv[5] = se->se_path;
1004 dprintf("unmount; path=%s\n", se->se_path);
1005 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1006 zfsctl_snapshot_rele(se);
1010 * The umount system utility will return 256 on error. We must
1011 * assume this error is because the file system is busy so it is
1012 * converted to the more sensible EBUSY.
1015 error = SET_ERROR(EBUSY);
1021 zfsctl_snapshot_mount(struct path *path, int flags)
1023 struct dentry *dentry = path->dentry;
1024 struct inode *ip = dentry->d_inode;
1026 zfsvfs_t *snap_zfsvfs;
1027 zfs_snapentry_t *se;
1028 char *full_name, *full_path;
1029 char *argv[] = { "/usr/bin/env", "mount", "-t", "zfs", "-n", NULL, NULL,
1031 char *envp[] = { NULL };
1036 return (SET_ERROR(EISDIR));
1038 zfsvfs = ITOZSB(ip);
1041 full_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
1042 full_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1044 error = zfsctl_snapshot_name(zfsvfs, dname(dentry),
1045 ZFS_MAX_DATASET_NAME_LEN, full_name);
1050 * Construct a mount point path from sb of the ctldir inode and dirent
1051 * name, instead of from d_path(), so that chroot'd process doesn't fail
1054 snprintf(full_path, MAXPATHLEN, "%s/.zfs/snapshot/%s",
1055 zfsvfs->z_vfs->vfs_mntpoint, dname(dentry));
1058 * Multiple concurrent automounts of a snapshot are never allowed.
1059 * The snapshot may be manually mounted as many times as desired.
1061 if (zfsctl_snapshot_ismounted(full_name)) {
1067 * Attempt to mount the snapshot from user space. Normally this
1068 * would be done using the vfs_kern_mount() function, however that
1069 * function is marked GPL-only and cannot be used. On error we
1070 * careful to log the real error to the console and return EISDIR
1071 * to safely abort the automount. This should be very rare.
1073 * If the user mode helper happens to return EBUSY, a concurrent
1074 * mount is already in progress in which case the error is ignored.
1075 * Take note that if the program was executed successfully the return
1076 * value from call_usermodehelper() will be (exitcode << 8 + signal).
1078 dprintf("mount; name=%s path=%s\n", full_name, full_path);
1079 argv[5] = full_name;
1080 argv[6] = full_path;
1081 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1083 if (!(error & MOUNT_BUSY << 8)) {
1084 cmn_err(CE_WARN, "Unable to automount %s/%s: %d",
1085 full_path, full_name, error);
1086 error = SET_ERROR(EISDIR);
1089 * EBUSY, this could mean a concurrent mount, or the
1090 * snapshot has already been mounted at completely
1091 * different place. We return 0 so VFS will retry. For
1092 * the latter case the VFS will retry several times
1093 * and return ELOOP, which is probably not a very good
1102 * Follow down in to the mounted snapshot and set MNT_SHRINKABLE
1103 * to identify this as an automounted filesystem.
1107 if (zpl_follow_down_one(&spath)) {
1108 snap_zfsvfs = ITOZSB(spath.dentry->d_inode);
1109 snap_zfsvfs->z_parent = zfsvfs;
1110 dentry = spath.dentry;
1111 spath.mnt->mnt_flags |= MNT_SHRINKABLE;
1113 rw_enter(&zfs_snapshot_lock, RW_WRITER);
1114 se = zfsctl_snapshot_alloc(full_name, full_path,
1115 snap_zfsvfs->z_os->os_spa, dmu_objset_id(snap_zfsvfs->z_os),
1117 zfsctl_snapshot_add(se);
1118 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot);
1119 rw_exit(&zfs_snapshot_lock);
1123 kmem_free(full_name, ZFS_MAX_DATASET_NAME_LEN);
1124 kmem_free(full_path, MAXPATHLEN);
1132 * Get the snapdir inode from fid
1135 zfsctl_snapdir_vget(struct super_block *sb, uint64_t objsetid, int gen,
1141 struct dentry *dentry;
1143 mnt = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1145 error = zfsctl_snapshot_path_objset(sb->s_fs_info, objsetid,
1150 /* Trigger automount */
1151 error = -kern_path(mnt, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &path);
1157 * Get the snapdir inode. Note, we don't want to use the above
1158 * path because it contains the root of the snapshot rather
1161 *ipp = ilookup(sb, ZFSCTL_INO_SNAPDIRS - objsetid);
1163 error = SET_ERROR(ENOENT);
1167 /* check gen, see zfsctl_snapdir_fid */
1168 dentry = d_obtain_alias(igrab(*ipp));
1169 if (gen != (!IS_ERR(dentry) && d_mountpoint(dentry))) {
1172 error = SET_ERROR(ENOENT);
1174 if (!IS_ERR(dentry))
1177 kmem_free(mnt, MAXPATHLEN);
1182 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp,
1183 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
1185 zfsvfs_t *zfsvfs = ITOZSB(dip);
1192 if (zfsvfs->z_shares_dir == 0) {
1194 return (SET_ERROR(ENOTSUP));
1197 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1198 error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL);
1208 * Initialize the various pieces we'll need to create and manipulate .zfs
1209 * directories. Currently this is unused but available.
1214 avl_create(&zfs_snapshots_by_name, snapentry_compare_by_name,
1215 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t,
1217 avl_create(&zfs_snapshots_by_objsetid, snapentry_compare_by_objsetid,
1218 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t,
1220 rw_init(&zfs_snapshot_lock, NULL, RW_DEFAULT, NULL);
1224 * Cleanup the various pieces we needed for .zfs directories. In particular
1225 * ensure the expiry timer is canceled safely.
1230 avl_destroy(&zfs_snapshots_by_name);
1231 avl_destroy(&zfs_snapshots_by_objsetid);
1232 rw_destroy(&zfs_snapshot_lock);
1235 module_param(zfs_admin_snapshot, int, 0644);
1236 MODULE_PARM_DESC(zfs_admin_snapshot, "Enable mkdir/rmdir/mv in .zfs/snapshot");
1238 module_param(zfs_expire_snapshot, int, 0644);
1239 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot");