Btrfs: Readonly snapshots
v3 cover Btrfs: Readonly snapshots
This patchset adds readonly-snapshots support. You can create a readonly snapshot, and you can also set a snapshot (subvolume also) readonly/writable on the fly.
A few readonly checks are added in setattr, permission, remove_xattr and set_xattr callbacks, as well as in some ioctls.
The patchset is also available in:
git://repo.or.cz/linux-btrfs-devel.git readonly-snapshots
--- fs/btrfs/ctree.h | 7 ++ fs/btrfs/inode.c | 8 ++ fs/btrfs/ioctl.c | 199 ++++++++++++++++++++++++++++++++++++------------ fs/btrfs/ioctl.h | 3 + fs/btrfs/transaction.c | 8 ++ fs/btrfs/transaction.h | 1 + fs/btrfs/xattr.c | 18 +++++ 7 files changed, 195 insertions(+), 49 deletions(-)
PATCH v3 1/3 Btrfs: Refactor btrfs_ioctl_snap_create()
Split it into two functions for two different ioctls, since they share no common code.
--- fs/btrfs/ioctl.c | 84 +++++++++++++++++++++++++---------------------------- 1 files changed, 40 insertions(+), 44 deletions(-)
fs/btrfs/ioctl.c +40 -44
@@ -946,58 +946,54 @@ out: } static noinline int btrfs_ioctl_snap_create(struct file *file, -void __user *arg,int subvol, -int v2) +void __user *arg,int subvol) { -struct btrfs_ioctl_vol_args *vol_args =NULL ; -struct btrfs_ioctl_vol_args_v2 *vol_args_v2 =NULL ; -char *name; -u64 fd; +struct btrfs_ioctl_vol_args *vol_args;int ret; -if (v2) { -u64 transid =0 ; -u64 *ptr =NULL ; + vol_args =memdup_user (arg,sizeof (*vol_args)); +if (IS_ERR (vol_args)) +return PTR_ERR (vol_args); + vol_args->name[BTRFS_PATH_NAME_MAX ] ='\0' ; - vol_args_v2 =memdup_user (arg,sizeof (*vol_args_v2)); -if (IS_ERR (vol_args_v2)) -return PTR_ERR (vol_args_v2); + ret =btrfs_ioctl_snap_create_transid (file, vol_args->name, + vol_args->fd, subvol,NULL ); -if (vol_args_v2->flags & ~BTRFS_SUBVOL_CREATE_ASYNC ) { - ret = -EINVAL ; - goto out; - } - - name = vol_args_v2->name; - fd = vol_args_v2->fd; - vol_args_v2->name[BTRFS_SUBVOL_NAME_MAX ] ='\0' ; +kfree (vol_args); +return ret; +} -if (vol_args_v2->flags &BTRFS_SUBVOL_CREATE_ASYNC ) - ptr = &transid; +static noinline int btrfs_ioctl_snap_create_v2(struct file *file, +void __user *arg,int subvol) +{ +struct btrfs_ioctl_vol_args_v2 *vol_args; +int ret; +u64 transid =0 ; +u64 *ptr =NULL ; - ret =btrfs_ioctl_snap_create_transid (file, name, fd, - subvol, ptr); + vol_args =memdup_user (arg,sizeof (*vol_args)); +if (IS_ERR (vol_args)) +return PTR_ERR (vol_args); + vol_args->name[BTRFS_SUBVOL_NAME_MAX ] ='\0' ; -if (ret ==0 && ptr && -copy_to_user (arg + - offsetof(struct btrfs_ioctl_vol_args_v2 , - transid), ptr,sizeof (*ptr))) - ret = -EFAULT ; - }else { - vol_args =memdup_user (arg,sizeof (*vol_args)); -if (IS_ERR (vol_args)) -return PTR_ERR (vol_args); - name = vol_args->name; - fd = vol_args->fd; - vol_args->name[BTRFS_PATH_NAME_MAX ] ='\0' ; - - ret =btrfs_ioctl_snap_create_transid (file, name, fd, - subvol,NULL ); +if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ASYNC ) { + ret = -EINVAL ; + goto out; } + +if (vol_args->flags &BTRFS_SUBVOL_CREATE_ASYNC ) + ptr = &transid; + + ret =btrfs_ioctl_snap_create_transid (file, vol_args->name, + vol_args->fd, subvol, ptr); + +if (ret ==0 && ptr && +copy_to_user (arg + + offsetof(struct btrfs_ioctl_vol_args_v2 , + transid), ptr,sizeof (*ptr))) + ret = -EFAULT ; out:kfree (vol_args); -kfree (vol_args_v2); -return ret; }
@@ -2257,11 +2253,11 @@ long btrfs_ioctl(struct file *file, unsigned intcase FS_IOC_GETVERSION :return btrfs_ioctl_getversion (file, argp);case BTRFS_IOC_SNAP_CREATE : -return btrfs_ioctl_snap_create (file, argp,0 ,0 ); +return btrfs_ioctl_snap_create (file, argp,0 );case BTRFS_IOC_SNAP_CREATE_V2 : -return btrfs_ioctl_snap_create (file, argp,0 ,1 ); +return btrfs_ioctl_snap_create_v2 (file, argp,0 );case BTRFS_IOC_SUBVOL_CREATE : -return btrfs_ioctl_snap_create (file, argp,1 ,0 ); +return btrfs_ioctl_snap_create (file, argp,1 );case BTRFS_IOC_SNAP_DESTROY :return btrfs_ioctl_snap_destroy (file, argp);case BTRFS_IOC_DEFAULT_SUBVOL :
Signature · 1 line
1.6.3
PATCH v3 2/3 Btrfs: Add readonly snapshots support
In reply to Li ZefanUsage:
Set BTRFS_SUBVOL_RDONLY of btrfs_ioctl_vol_arg_v2->flags, and call ioctl(BTRFS_I0CTL_SNAP_CREATE_V2).
Implementation:
- Set readonly bit of btrfs_root_item->flags.
- Add readonly checks in btrfs_permission (inode_permission), btrfs_setattr, btrfs_set/remove_xattr and some ioctls.
Changelog for v3:
- Eliminate btrfs_root->readonly, but check btrfs_root->root_item.flags.
- Rename BTRFS_ROOT_SNAP_RDONLY to BTRFS_ROOT_SUBVOL_RDONLY.
--- fs/btrfs/ctree.h | 7 +++++++ fs/btrfs/inode.c | 8 ++++++++ fs/btrfs/ioctl.c | 42 ++++++++++++++++++++++++++++++++---------- fs/btrfs/ioctl.h | 1 + fs/btrfs/transaction.c | 8 ++++++++ fs/btrfs/transaction.h | 1 + fs/btrfs/xattr.c | 18 ++++++++++++++++++ 7 files changed, 75 insertions(+), 10 deletions(-)
fs/btrfs/ctree.h +7 -0
@@ -597,6 +597,8 @@ struct btrfs_dir_item {u8 type; } __attribute__ ((__packed__)); +#define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0) +struct btrfs_root_item {struct btrfs_inode_item inode;__le64 generation;
@@ -1893,6 +1895,11 @@ BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64); BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item, last_snapshot,64 ); +static inline bool btrfs_root_readonly (struct btrfs_root *root) +{ +return root->root_item.flags &BTRFS_ROOT_SUBVOL_RDONLY ; +} +/* struct btrfs_super_block */ BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
fs/btrfs/inode.c +8 -0
@@ -3671,8 +3671,12 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)static int btrfs_setattr (struct dentry *dentry,struct iattr *attr) {struct inode *inode = dentry->d_inode; +struct btrfs_root *root =BTRFS_I (inode)->root;int err; +if (btrfs_root_readonly (root)) +return -EROFS ; + err =inode_change_ok (inode, attr);if (err)return err;
@@ -7206,6 +7210,10 @@ static int btrfs_set_page_dirty(struct page *page)static int btrfs_permission (struct inode *inode,int mask) { +struct btrfs_root *root =BTRFS_I (inode)->root; + +if (btrfs_root_readonly (root) && (mask &MAY_WRITE )) +return -EROFS ;if ((BTRFS_I (inode)->flags &BTRFS_INODE_READONLY ) && (mask &MAY_WRITE ))return -EACCES ;return generic_permission (inode, mask, btrfs_check_acl);
fs/btrfs/ioctl.c +32 -10
133 lines not on this page · raw message
fs/btrfs/ioctl.h +1 -0
@@ -31,6 +31,7 @@ struct btrfs_ioctl_vol_args { };#define BTRFS_SUBVOL_CREATE_ASYNC (1ULL << 0) +#define BTRFS_SUBVOL_RDONLY (1ULL << 1)#define BTRFS_SUBVOL_NAME_MAX 4039struct btrfs_ioctl_vol_args_v2 {
fs/btrfs/transaction.c +8 -0
@@ -910,6 +910,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,u64 to_reserve =0 ;u64 index =0 ;u64 objectid; +u64 root_flags; new_root_item =kmalloc (sizeof (*new_root_item),GFP_NOFS );if (!new_root_item) {
@@ -967,6 +968,13 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,btrfs_set_root_last_snapshot (&root->root_item, trans->transid);memcpy (new_root_item, &root->root_item,sizeof (*new_root_item)); + root_flags =btrfs_root_flags (new_root_item); +if (pending->readonly) + root_flags |=BTRFS_ROOT_SUBVOL_RDONLY ; +else + root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY ; +btrfs_set_root_flags (new_root_item, root_flags); + old =btrfs_lock_root_node (root);btrfs_cow_block (trans, root, old,NULL ,0 , &old);btrfs_set_lock_blocking (old);
fs/btrfs/transaction.h +1 -0
@@ -62,6 +62,7 @@ struct btrfs_pending_snapshot {struct btrfs_block_rsv block_rsv;/* extra metadata reseration for relocation */ int error; +bool readonly;struct list_head list; };
fs/btrfs/xattr.c +18 -0
@@ -316,6 +316,15 @@ ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,int btrfs_setxattr (struct dentry *dentry,const char *name,const void *value,size_t size,int flags) { +struct btrfs_root *root =BTRFS_I (dentry->d_inode)->root; + +/* +* The permission on security.* and system.* is not checked +* in permission(). +*/ +if (btrfs_root_readonly (root)) +return -EROFS ; +/* * If this is a request for a synthetic attribute in the system.* * namespace use the generic infrastructure to resolve a handler
@@ -336,6 +345,15 @@ int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,int btrfs_removexattr (struct dentry *dentry,const char *name) { +struct btrfs_root *root =BTRFS_I (dentry->d_inode)->root; + +/* +* The permission on security.* and system.* is not checked +* in permission(). +*/ +if (btrfs_root_readonly (root)) +return -EROFS ; +/* * If this is a request for a synthetic attribute in the system.* * namespace use the generic infrastructure to resolve a handler
Signature · 1 line
1.6.3
PATCH v3 3/3 Btrfs: Add BTRFS_IOC_SUBVOL_GETFLAGS/SETFLAGS ioctls
In reply to Li ZefanThis allows us to set a snapshot or a subvolume readonly or writable on the fly.
Usage:
Set BTRFS_SUBVOL_RDONLY of btrfs_ioctl_vol_arg_v2->flags, and then call ioctl(BTRFS_IOCTL_SUBVOL_SETFLAGS);
Changelog for v3:
- Change to pass __u64 as ioctl parameter.
Changelog for v2:
- Add _GETFLAGS ioctl.
- Check if the passed fd is the root of a subvolume.
- Change the name from _SNAP_SETFLAGS to _SUBVOL_SETFLAGS.
--- fs/btrfs/ioctl.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ioctl.h | 2 + 2 files changed, 85 insertions(+), 0 deletions(-)
fs/btrfs/ioctl.c +83 -0
@@ -1009,6 +1009,85 @@ out:return ret; } +static noinline int btrfs_ioctl_subvol_getflags(struct file *file, +void __user *arg) +{ +struct inode *inode =fdentry (file)->d_inode; +struct btrfs_root *root =BTRFS_I (inode)->root; +int ret =0 ; +u64 flags =0 ; + +if (inode->i_ino !=BTRFS_FIRST_FREE_OBJECTID ) +return -EINVAL ; + +down_read (&root->fs_info->subvol_sem); +if (btrfs_root_readonly (root)) + flags |=BTRFS_SUBVOL_RDONLY ; +up_read (&root->fs_info->subvol_sem); + +if (copy_to_user (arg, &flags,sizeof (flags))) + ret = -EFAULT ; + +return ret; +} + +static noinline int btrfs_ioctl_subvol_setflags(struct file *file, +void __user *arg) +{ +struct inode *inode =fdentry (file)->d_inode; +struct btrfs_root *root =BTRFS_I (inode)->root; +struct btrfs_trans_handle *trans; +u64 root_flags; +u64 flags; +int ret =0 ; + +if (root->fs_info->sb->s_flags &MS_RDONLY ) +return -EROFS ; + +if (inode->i_ino !=BTRFS_FIRST_FREE_OBJECTID ) +return -EINVAL ; + +if (copy_from_user (&flags, arg,sizeof (flags))) +return -EFAULT ; + +if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC ) +return -EINVAL ; + +if (flags & ~BTRFS_SUBVOL_RDONLY ) +return -EOPNOTSUPP ; + +down_write (&root->fs_info->subvol_sem); + +/* nothing to do */ +if (!!(flags &BTRFS_SUBVOL_RDONLY ) ==btrfs_root_readonly (root)) + goto out; + + root_flags =btrfs_root_flags (&root->root_item); +if (flags &BTRFS_SUBVOL_RDONLY ) +btrfs_set_root_flags (&root->root_item, + root_flags |BTRFS_ROOT_SUBVOL_RDONLY ); +else +btrfs_set_root_flags (&root->root_item, + root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY ); + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { + ret =PTR_ERR (trans); + goto out_reset; + } + + ret =btrfs_update_root (trans, root, + &root->root_key, &root->root_item); + +btrfs_commit_transaction (trans, root); +out_reset: +if (ret) +btrfs_set_root_flags (&root->root_item, root_flags); +out: +up_write (&root->fs_info->subvol_sem); +return ret; +} +/* * helper to check if the subvolume references other subvolumes */
@@ -2282,6 +2361,10 @@ long btrfs_ioctl(struct file *file, unsigned intreturn btrfs_ioctl_snap_create (file, argp,1 );case BTRFS_IOC_SNAP_DESTROY :return btrfs_ioctl_snap_destroy (file, argp); +case BTRFS_IOC_SUBVOL_GETFLAGS : +return btrfs_ioctl_subvol_getflags (file, argp); +case BTRFS_IOC_SUBVOL_SETFLAGS : +return btrfs_ioctl_subvol_setflags (file, argp);case BTRFS_IOC_DEFAULT_SUBVOL :return btrfs_ioctl_default_subvol (file, argp);case BTRFS_IOC_DEFRAG :
fs/btrfs/ioctl.h +2 -0
@@ -194,4 +194,6 @@ struct btrfs_ioctl_space_args {#define BTRFS_IOC_WAIT_SYNC _IOW(BTRFS_IOCTL_MAGIC, 22, __u64)#define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, \ struct btrfs_ioctl_vol_args_v2) +#define BTRFS_IOC_SUBVOL_GETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 25, __u64) +#define BTRFS_IOC_SUBVOL_SETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 26, __u64)#endif
Signature · 1 line
1.6.3