summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2021-03-16 23:28:43 -0400
committerKent Overstreet <kent.overstreet@gmail.com>2021-04-27 17:05:19 -0400
commite6194111f218b21d2aaf0876dc5ac29a70cc7dd0 (patch)
treec5a2fde9077ec40a1482e62421a288fa2d8990e6
parent02ae4636e4ed743784403cbe1058b3cde65e5e26 (diff)
bcachefs: Snapshot creationbcachefs-v5.10-snapshots
-rw-r--r--fs/bcachefs/chardev.c142
-rw-r--r--fs/bcachefs/fs-common.c124
-rw-r--r--fs/bcachefs/fs-common.h5
-rw-r--r--fs/bcachefs/fs.c18
-rw-r--r--fs/bcachefs/fs.h2
-rw-r--r--fs/bcachefs/fsck.c3
-rw-r--r--fs/bcachefs/recovery.c2
7 files changed, 259 insertions, 37 deletions
diff --git a/fs/bcachefs/chardev.c b/fs/bcachefs/chardev.c
index c61601476c0d..0370640efb9f 100644
--- a/fs/bcachefs/chardev.c
+++ b/fs/bcachefs/chardev.c
@@ -5,6 +5,9 @@
#include "bcachefs_ioctl.h"
#include "buckets.h"
#include "chardev.h"
+#include "dirent.h"
+#include "fs.h"
+#include "fs-common.h"
#include "journal.h"
#include "move.h"
#include "replicas.h"
@@ -16,12 +19,16 @@
#include <linux/device.h>
#include <linux/file.h>
#include <linux/fs.h>
+#include <linux/fsnotify.h>
#include <linux/ioctl.h>
#include <linux/kthread.h>
#include <linux/major.h>
+#include <linux/namei.h>
#include <linux/sched/task.h>
+#include <linux/security.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
+#include <linux/writeback.h>
/* returns with ref on ca->ref */
static struct bch_dev *bch2_device_lookup(struct bch_fs *c, u64 dev,
@@ -585,6 +592,136 @@ static long bch2_ioctl_disk_resize_journal(struct bch_fs *c,
return ret;
}
+static long bch2_ioctl_subvolume_create(struct bch_fs *c,
+ struct bch_ioctl_subvolume arg)
+{
+ struct inode *dir;
+ struct bch_inode_info *inode;
+ struct user_namespace *s_user_ns;
+ struct dentry *dst_dentry;
+ struct path src_path, dst_path;
+ int how = LOOKUP_FOLLOW;
+ int error;
+ subvol_inum snapshot_src = { 0 };
+ unsigned lookup_flags = 0;
+ unsigned create_flags = BCH_CREATE_SUBVOL;
+
+ if (arg.flags & ~(BCH_SUBVOL_SNAPSHOT_CREATE|
+ BCH_SUBVOL_SNAPSHOT_RO))
+ return -EINVAL;
+
+ if (!(arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE) &&
+ (arg.src_ptr ||
+ (arg.flags & BCH_SUBVOL_SNAPSHOT_RO)))
+ return -EINVAL;
+
+ if (arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE)
+ create_flags |= BCH_CREATE_SNAPSHOT;
+
+ if (arg.flags & BCH_SUBVOL_SNAPSHOT_RO)
+ create_flags |= BCH_CREATE_SNAPSHOT_RO;
+
+ down_read(&c->vfs_sb->s_umount);
+
+ if (arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE)
+ sync_inodes_sb(c->vfs_sb);
+retry:
+ if (arg.src_ptr) {
+ error = user_path_at(arg.dirfd,
+ (const char __user *)(unsigned long)arg.src_ptr,
+ how, &src_path);
+ if (error)
+ goto err1;
+
+ if (src_path.dentry->d_sb->s_fs_info != c) {
+ path_put(&src_path);
+ error = -EXDEV;
+ goto err1;
+ }
+
+ snapshot_src = inode_inum(to_bch_ei(src_path.dentry->d_inode));
+ }
+
+ dst_dentry = user_path_create(arg.dirfd,
+ (const char __user *)(unsigned long)arg.dst_ptr,
+ &dst_path, lookup_flags);
+ error = PTR_ERR_OR_ZERO(dst_dentry);
+ if (error)
+ goto err2;
+
+ if (dst_dentry->d_sb->s_fs_info != c) {
+ error = -EXDEV;
+ goto err3;
+ }
+
+ if (dst_dentry->d_inode) {
+ error = -EEXIST;
+ goto err3;
+ }
+
+ dir = dst_path.dentry->d_inode;
+ if (IS_DEADDIR(dir)) {
+ error = -ENOENT;
+ goto err3;
+ }
+
+ s_user_ns = dir->i_sb->s_user_ns;
+ if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
+ !kgid_has_mapping(s_user_ns, current_fsgid())) {
+ error = -EOVERFLOW;
+ goto err3;
+ }
+
+ error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
+ if (error)
+ goto err3;
+
+ if (!IS_POSIXACL(dir))
+ arg.mode &= ~current_umask();
+
+ error = security_path_mkdir(&dst_path, dst_dentry, arg.mode);
+ if (error)
+ goto err3;
+
+ if ((arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE) &&
+ !arg.src_ptr)
+ snapshot_src.subvol = to_bch_ei(dir)->ei_inode.bi_subvol;
+
+ inode = __bch2_create(to_bch_ei(dir), dst_dentry, arg.mode|S_IFDIR,
+ 0, snapshot_src, create_flags);
+ error = PTR_ERR_OR_ZERO(inode);
+ if (error)
+ goto err3;
+
+ d_instantiate(dst_dentry, &inode->v);
+ fsnotify_mkdir(dir, dst_dentry);
+err3:
+ done_path_create(&dst_path, dst_dentry);
+err2:
+ if (arg.src_ptr)
+ path_put(&src_path);
+
+ if (retry_estale(error, lookup_flags)) {
+ lookup_flags |= LOOKUP_REVAL;
+ goto retry;
+ }
+err1:
+ up_read(&c->vfs_sb->s_umount);
+
+ return error;
+}
+
+static long bch2_ioctl_subvolume_destroy(struct bch_fs *c,
+ struct bch_ioctl_subvolume arg)
+{
+ int ret = 0;
+
+ if (arg.flags)
+ return -EINVAL;
+
+ return ret;
+}
+
#define BCH_IOCTL(_name, _argtype) \
do { \
_argtype i; \
@@ -644,6 +781,11 @@ long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
case BCH_IOCTL_DISK_RESIZE_JOURNAL:
BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal);
+ case BCH_IOCTL_SUBVOLUME_CREATE:
+ BCH_IOCTL(subvolume_create, struct bch_ioctl_subvolume);
+ case BCH_IOCTL_SUBVOLUME_DESTROY:
+ BCH_IOCTL(subvolume_destroy, struct bch_ioctl_subvolume);
+
default:
return -ENOTTY;
}
diff --git a/fs/bcachefs/fs-common.c b/fs/bcachefs/fs-common.c
index 310ec36c4df4..9d605df70c14 100644
--- a/fs/bcachefs/fs-common.c
+++ b/fs/bcachefs/fs-common.c
@@ -19,6 +19,7 @@ int bch2_create_trans(struct btree_trans *trans,
uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
struct posix_acl *default_acl,
struct posix_acl *acl,
+ subvol_inum snapshot_src,
unsigned flags)
{
struct bch_fs *c = trans->c;
@@ -26,10 +27,9 @@ int bch2_create_trans(struct btree_trans *trans,
struct btree_iter *inode_iter = NULL;
subvol_inum new_inum = dir;
u64 now = bch2_current_time(c);
- u64 dir_offset = 0;
u64 dir_target;
u32 snapshot;
- unsigned dir_type;
+ unsigned dir_type = mode_to_type(mode);
int ret;
ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
@@ -41,36 +41,116 @@ int bch2_create_trans(struct btree_trans *trans,
if (ret)
goto err;
- bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
+ if (!(flags & BCH_CREATE_SNAPSHOT)) {
+ /* Normal create path - allocate a new inode: */
+ bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
- if (!name)
- new_inode->bi_flags |= BCH_INODE_UNLINKED;
+ if (flags & BCH_CREATE_TMPFILE)
+ new_inode->bi_flags |= BCH_INODE_UNLINKED;
- inode_iter = bch2_inode_create(trans, new_inode, snapshot);
- ret = PTR_ERR_OR_ZERO(inode_iter);
- if (ret)
- goto err;
+ inode_iter = bch2_inode_create(trans, new_inode, snapshot);
+ ret = PTR_ERR_OR_ZERO(inode_iter);
+ if (ret)
+ goto err;
+
+ snapshot_src = (subvol_inum) { 0 };
+ } else {
+ /*
+ * Creating a snapshot - we're not allocating a new inode, but
+ * we do have to lookup the root inode of the subvolume we're
+ * snapshotting and update it (in the new snapshot):
+ */
+
+ if (!snapshot_src.inum) {
+ /* Inode wasn't specified, just snapshot: */
+ struct btree_iter *subvol_iter =
+ bch2_trans_get_iter(trans, BTREE_ID_subvolumes,
+ POS(0, snapshot_src.subvol), 0);
+ struct bkey_s_c k = bch2_btree_iter_peek_slot(subvol_iter);
+
+ ret = bkey_err(k);
+ if (!ret && k.k->type != KEY_TYPE_subvolume) {
+ bch_err(c, "subvolume %u not found",
+ snapshot_src.subvol);
+ ret = -ENOENT;
+ }
+
+ if (!ret)
+ snapshot_src.inum = le64_to_cpu(bkey_s_c_to_subvolume(k).v->inode);
+ bch2_trans_iter_put(trans, subvol_iter);
+
+ if (ret)
+ goto err;
+ }
+
+ inode_iter = bch2_inode_peek(trans, new_inode, snapshot_src,
+ BTREE_ITER_INTENT);
+ ret = PTR_ERR_OR_ZERO(inode_iter);
+ if (ret)
+ goto err;
+
+ if (new_inode->bi_subvol != snapshot_src.subvol) {
+ /* Not a subvolume root: */
+ ret = -EINVAL;
+ goto err;
+ }
+
+ /*
+ * If we're not root, we have to own the subvolume being
+ * snapshotted:
+ */
+ if (uid && new_inode->bi_uid != uid) {
+ ret = -EPERM;
+ goto err;
+ }
+
+ flags |= BCH_CREATE_SUBVOL;
+ }
new_inum.inum = new_inode->bi_inum;
dir_target = new_inode->bi_inum;
- dir_type = mode_to_type(new_inode->bi_mode);
- if (default_acl) {
- ret = bch2_set_acl_trans(trans, new_inum, new_inode,
- default_acl, ACL_TYPE_DEFAULT);
+ if (flags & BCH_CREATE_SUBVOL) {
+ u32 new_subvol, dir_snapshot;
+
+ ret = bch2_subvolume_create(trans, new_inode->bi_inum,
+ snapshot_src.subvol,
+ &new_subvol, &snapshot,
+ (flags & BCH_CREATE_SNAPSHOT_RO) != 0);
if (ret)
goto err;
- }
- if (acl) {
- ret = bch2_set_acl_trans(trans, new_inum, new_inode,
- acl, ACL_TYPE_ACCESS);
+ new_inode->bi_parent_subvol = dir.subvol;
+ new_inode->bi_subvol = new_subvol;
+ new_inum.subvol = new_subvol;
+ dir_target = new_subvol;
+ dir_type = DT_SUBVOL;
+
+ ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &dir_snapshot);
if (ret)
goto err;
+ bch2_btree_iter_set_snapshot(dir_iter, dir_snapshot);
}
- if (name) {
+ if (!(flags & BCH_CREATE_SNAPSHOT)) {
+ if (default_acl) {
+ ret = bch2_set_acl_trans(trans, new_inum, new_inode,
+ default_acl, ACL_TYPE_DEFAULT);
+ if (ret)
+ goto err;
+ }
+
+ if (acl) {
+ ret = bch2_set_acl_trans(trans, new_inum, new_inode,
+ acl, ACL_TYPE_ACCESS);
+ if (ret)
+ goto err;
+ }
+ }
+
+ if (!(flags & BCH_CREATE_TMPFILE)) {
struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
+ u64 dir_offset;
if (S_ISDIR(new_inode->bi_mode))
dir_u->bi_nlink++;
@@ -88,11 +168,11 @@ int bch2_create_trans(struct btree_trans *trans,
BCH_HASH_SET_MUST_CREATE);
if (ret)
goto err;
- }
- if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
- new_inode->bi_dir = dir_u->bi_inum;
- new_inode->bi_dir_offset = dir_offset;
+ if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
+ new_inode->bi_dir = dir_u->bi_inum;
+ new_inode->bi_dir_offset = dir_offset;
+ }
}
inode_iter->flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
diff --git a/fs/bcachefs/fs-common.h b/fs/bcachefs/fs-common.h
index 1bb2ac4dc13a..e25ef56147ac 100644
--- a/fs/bcachefs/fs-common.h
+++ b/fs/bcachefs/fs-common.h
@@ -5,6 +5,9 @@
struct posix_acl;
#define BCH_CREATE_TMPFILE (1U << 0)
+#define BCH_CREATE_SUBVOL (1U << 1)
+#define BCH_CREATE_SNAPSHOT (1U << 2)
+#define BCH_CREATE_SNAPSHOT_RO (1U << 3)
int bch2_create_trans(struct btree_trans *, subvol_inum,
struct bch_inode_unpacked *,
@@ -13,7 +16,7 @@ int bch2_create_trans(struct btree_trans *, subvol_inum,
uid_t, gid_t, umode_t, dev_t,
struct posix_acl *,
struct posix_acl *,
- unsigned);
+ subvol_inum, unsigned);
int bch2_link_trans(struct btree_trans *,
subvol_inum, struct bch_inode_unpacked *,
diff --git a/fs/bcachefs/fs.c b/fs/bcachefs/fs.c
index d944f2292663..73f93f1ac35f 100644
--- a/fs/bcachefs/fs.c
+++ b/fs/bcachefs/fs.c
@@ -239,12 +239,6 @@ struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
struct bch_inode_info *inode;
int ret;
- /*
- * debug assert, to be removed when we start creating
- * subvolumes/snapshots:
- */
- BUG_ON(inum.subvol != BCACHEFS_ROOT_SUBVOL);
-
inode = to_bch_ei(iget5_locked(c->vfs_sb,
bch2_inode_hash(inum),
bch2_iget5_test,
@@ -272,7 +266,8 @@ struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
struct bch_inode_info *
__bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
- umode_t mode, dev_t rdev, unsigned flags)
+ umode_t mode, dev_t rdev, subvol_inum snapshot_src,
+ unsigned flags)
{
struct bch_fs *c = dir->v.i_sb->s_fs_info;
struct user_namespace *ns = dir->v.i_sb->s_user_ns;
@@ -318,7 +313,7 @@ retry:
from_kuid(ns, current_fsuid()),
from_kgid(ns, current_fsgid()),
mode, rdev,
- default_acl, acl, flags) ?:
+ default_acl, acl, snapshot_src, flags) ?:
bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
KEY_TYPE_QUOTA_PREALLOC);
if (unlikely(ret))
@@ -425,7 +420,8 @@ static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
umode_t mode, dev_t rdev)
{
struct bch_inode_info *inode =
- __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, 0);
+ __bch2_create(to_bch_ei(vdir), dentry, mode, rdev,
+ (subvol_inum) { 0 }, 0);
if (IS_ERR(inode))
return PTR_ERR(inode);
@@ -535,7 +531,7 @@ static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
int ret;
inode = __bch2_create(dir, dentry, S_IFLNK|S_IRWXUGO, 0,
- BCH_CREATE_TMPFILE);
+ (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
if (unlikely(IS_ERR(inode)))
return PTR_ERR(inode);
@@ -846,7 +842,7 @@ static int bch2_tmpfile(struct inode *vdir, struct dentry *dentry, umode_t mode)
{
struct bch_inode_info *inode =
__bch2_create(to_bch_ei(vdir), dentry, mode, 0,
- BCH_CREATE_TMPFILE);
+ (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
if (IS_ERR(inode))
return PTR_ERR(inode);
diff --git a/fs/bcachefs/fs.h b/fs/bcachefs/fs.h
index 066fd1c68c3c..e65513c819f0 100644
--- a/fs/bcachefs/fs.h
+++ b/fs/bcachefs/fs.h
@@ -147,7 +147,7 @@ struct bch_inode_unpacked;
struct bch_inode_info *
__bch2_create(struct bch_inode_info *, struct dentry *,
- umode_t, dev_t, unsigned);
+ umode_t, dev_t, subvol_inum, unsigned);
int bch2_fs_quota_transfer(struct bch_fs *,
struct bch_inode_info *,
diff --git a/fs/bcachefs/fsck.c b/fs/bcachefs/fsck.c
index bfc27233d9c5..e286ae0b9a39 100644
--- a/fs/bcachefs/fsck.c
+++ b/fs/bcachefs/fsck.c
@@ -289,7 +289,8 @@ create_lostfound:
BTREE_INSERT_LAZY_RW,
bch2_create_trans(trans, root_inum, &root,
lostfound, &lostfound_str,
- 0, 0, S_IFDIR|0700, 0, NULL, NULL, 0));
+ 0, 0, S_IFDIR|0700, 0, NULL, NULL,
+ (subvol_inum) { }, 0));
if (ret)
bch_err(c, "error creating lost+found: %i", ret);
}
diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
index 072e45481c37..176ab38699dd 100644
--- a/fs/bcachefs/recovery.c
+++ b/fs/bcachefs/recovery.c
@@ -1407,7 +1407,7 @@ int bch2_fs_initialize(struct bch_fs *c)
&root_inode, &lostfound_inode,
&lostfound,
0, 0, S_IFDIR|0700, 0,
- NULL, NULL, 0));
+ NULL, NULL, (subvol_inum) { 0 }, 0));
if (ret) {
bch_err(c, "error creating lost+found");
goto err;