btrfs-progs: convert: Fix a regression that ext2_save/image is not readonly

The new convert treats the convert image as a normal file, without any
special flags and permissions.

This is different from original code:
1) Permission changed from 0400 to 0600
2) Inode lacks READONLY flag

This makes we can read-write mount the ext2 image and cause rollback
failure.

Follow old code behavior, use 0400 permission and add back READONLY
flag to fix it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2016-08-19 16:13:05 +08:00 committed by David Sterba
parent 55aa862ea2
commit bdadea75e4
3 changed files with 46 additions and 1 deletions

View file

@ -1527,8 +1527,12 @@ static int create_image(struct btrfs_root *root,
struct cache_tree used_tmp;
u64 cur;
u64 ino;
u64 flags = BTRFS_INODE_READONLY;
int ret;
if (!datacsum)
flags |= BTRFS_INODE_NODATASUM;
trans = btrfs_start_transaction(root, 1);
if (!trans)
return -ENOMEM;
@ -1539,7 +1543,10 @@ static int create_image(struct btrfs_root *root,
&ino);
if (ret < 0)
goto out;
ret = btrfs_new_inode(trans, root, ino, 0600 | S_IFREG);
ret = btrfs_new_inode(trans, root, ino, 0400 | S_IFREG);
if (ret < 0)
goto out;
ret = btrfs_change_inode_flags(trans, root, ino, flags);
if (ret < 0)
goto out;
ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,

View file

@ -2578,6 +2578,8 @@ int check_dir_conflict(struct btrfs_root *root, char *name, int namelen,
u64 dir, u64 index);
int btrfs_new_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root,
u64 ino, u32 mode);
int btrfs_change_inode_flags(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 ino, u64 flags);
int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
u64 ino, u64 parent_ino, char *name, int namelen,
u8 type, u64 *index, int add_backref);

36
inode.c
View file

@ -471,6 +471,42 @@ int btrfs_new_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root,
return ret;
}
/*
* Change inode flags to given value
*/
int btrfs_change_inode_flags(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 ino, u64 flags)
{
struct btrfs_inode_item *item;
struct btrfs_path *path;
struct btrfs_key key;
int ret;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
key.objectid = ino;
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
if (ret > 0) {
ret = -ENOENT;
goto out;
}
if (ret < 0)
goto out;
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item);
btrfs_set_inode_flags(path->nodes[0], item, flags);
btrfs_mark_buffer_dirty(path->nodes[0]);
out:
btrfs_free_path(path);
return ret;
}
/*
* Make a dir under the parent inode 'parent_ino' with 'name'
* and 'mode', The owner will be root/root.