btrfs-progs: get/set allocation_hint for an unmounted filesystem
cover btrfs-progs: get/set allocation_hint for an unmounted filesystem
From: Goffredo Baroncelli <[email protected]>
This patches set allows to change the allocation_hint even for an unmounted filesystem. It does that using the user space btrfs code.
Because the userspace btrfs code is unaware of the allocation_hint if a new (metadata) chunk allocation is required during the changing of this property, the new chunk is allocated without considering the allocation_hint ... hint.
Goffredo Baroncelli (3):
Rename btrfs_device->type to flags
Rename dev_item.type in flags in the command output. Read/change the allocation_hint prop when unmounted
cmds/property.c | 115 ++++++++++++++++++++++++++++++++++++ cmds/rescue-chunk-recover.c | 2 +- common/device-scan.c | 4 +- convert/common.c | 2 +- image/main.c | 6 +- kernel-shared/ctree.h | 8 +-- kernel-shared/disk-io.c | 2 +- kernel-shared/print-tree.c | 8 +-- kernel-shared/volumes.c | 6 +- kernel-shared/volumes.h | 4 +- mkfs/common.c | 2 +- 11 files changed, 137 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 Rename btrfs_device->type to flags
From: Goffredo Baroncelli <[email protected]>
As did in the kernel, rename the device->type to device->flag.
--- cmds/rescue-chunk-recover.c | 2 +- common/device-scan.c | 4 ++-- convert/common.c | 2 +- image/main.c | 6 +++--- kernel-shared/ctree.h | 8 ++++---- kernel-shared/disk-io.c | 2 +- kernel-shared/print-tree.c | 4 ++-- kernel-shared/volumes.c | 6 +++--- kernel-shared/volumes.h | 4 ++-- mkfs/common.c | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-)
cmds/rescue-chunk-recover.c +1 -1
@@ -1187,7 +1187,7 @@ static int __rebuild_device_items(struct btrfs_trans_handle *trans, key.offset = dev->devid;btrfs_set_stack_device_generation (dev_item,0 ); -btrfs_set_stack_device_type (dev_item, dev->type); +btrfs_set_stack_device_flags (dev_item, dev->flags);btrfs_set_stack_device_id (dev_item, dev->devid);btrfs_set_stack_device_total_bytes (dev_item, dev->total_bytes);btrfs_set_stack_device_bytes_used (dev_item, dev->bytes_used);
common/device-scan.c +2 -2
@@ -155,7 +155,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,uuid_generate (device->uuid); device->fs_info = fs_info; device->devid =0 ; - device->type =0 ; + device->flags =0 ; device->io_width = io_width; device->io_align = io_align; device->sector_size = sectorsize;
@@ -193,7 +193,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,btrfs_set_super_bytenr (disk_super,BTRFS_SUPER_INFO_OFFSET );btrfs_set_stack_device_id (dev_item, device->devid); -btrfs_set_stack_device_type (dev_item, device->type); +btrfs_set_stack_device_flags (dev_item, device->flags);btrfs_set_stack_device_io_align (dev_item, device->io_align);btrfs_set_stack_device_io_width (dev_item, device->io_width);btrfs_set_stack_device_sector_size (dev_item, device->sector_size);
convert/common.c +1 -1
@@ -336,7 +336,7 @@ static int insert_temp_dev_item(int fd, struct extent_buffer *buf,btrfs_set_device_io_align (buf, dev_item, cfg->sectorsize);btrfs_set_device_io_width (buf, dev_item, cfg->sectorsize);btrfs_set_device_sector_size (buf, dev_item, cfg->sectorsize); -btrfs_set_device_type (buf, dev_item,0 ); +btrfs_set_device_flags (buf, dev_item,0 );/* Super dev_item is not complete, copy the complete one to sb */ read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
image/main.c +3 -3
@@ -2942,7 +2942,7 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,struct btrfs_super_block disk_super;char dev_uuid[BTRFS_UUID_SIZE ];char fs_uuid[BTRFS_UUID_SIZE ]; -u64 devid, type, io_align, io_width; +u64 devid, flags, io_align, io_width;u64 sector_size, total_bytes, bytes_used;int fp =-1 ;int ret;
@@ -2972,7 +2972,7 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info, goto out; } - type =btrfs_device_type (leaf, dev_item); + flags =btrfs_device_flags (leaf, dev_item); io_align =btrfs_device_io_align (leaf, dev_item); io_width =btrfs_device_io_width (leaf, dev_item); sector_size =btrfs_device_sector_size (leaf, dev_item);
@@ -2997,7 +2997,7 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info, dev_item = &disk_super.dev_item; -btrfs_set_stack_device_type (dev_item, type); +btrfs_set_stack_device_flags (dev_item, flags);btrfs_set_stack_device_id (dev_item, devid);btrfs_set_stack_device_total_bytes (dev_item, total_bytes);btrfs_set_stack_device_bytes_used (dev_item, bytes_used);
kernel-shared/ctree.h +4 -4
@@ -249,8 +249,8 @@ struct btrfs_dev_item {/* minimal io size for this device */ __le32 sector_size; -/* type and info about this device */ -__le64 type; +/* device flags (e.g. allocation hint) */ +__le64 flags;/* expected generation for this device */ __le64 generation;
@@ -1605,7 +1605,7 @@ static inline void btrfs_set_##name(type *s, u##bits val) \ s->member = cpu_to_le##bits(val); \ } -BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64); +BTRFS_SETGET_FUNCS(device_flags, struct btrfs_dev_item, flags, 64); BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64); BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64); BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
@@ -1619,7 +1619,7 @@ BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8); BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8); BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64); -BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64); +BTRFS_SETGET_STACK_FUNCS(stack_device_flags, struct btrfs_dev_item, flags, 64); BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item, total_bytes,64 ); BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
kernel-shared/disk-io.c +1 -1
@@ -2099,7 +2099,7 @@ int write_all_supers(struct btrfs_fs_info *fs_info)continue ;btrfs_set_stack_device_generation (dev_item,0 ); -btrfs_set_stack_device_type (dev_item, dev->type); +btrfs_set_stack_device_flags (dev_item, dev->flags);btrfs_set_stack_device_id (dev_item, dev->devid);btrfs_set_stack_device_total_bytes (dev_item, dev->total_bytes);btrfs_set_stack_device_bytes_used (dev_item, dev->bytes_used);
kernel-shared/print-tree.c +2 -2
@@ -304,7 +304,7 @@ static void print_dev_item(struct extent_buffer *eb,btrfs_device_io_align (eb, dev_item),btrfs_device_io_width (eb, dev_item),btrfs_device_sector_size (eb, dev_item), - (unsigned long long )btrfs_device_type (eb, dev_item), + (unsigned long long )btrfs_device_flags (eb, dev_item), (unsigned long long )btrfs_device_generation (eb, dev_item), (unsigned long long )btrfs_device_start_offset (eb, dev_item), btrfs_device_group(eb, dev_item),
@@ -2052,7 +2052,7 @@ void btrfs_print_superblock(struct btrfs_super_block *sb, int full) cmp_res ? "[match]" : "[DON'T MATCH]");printf ("dev_item.type\t\t%llu\n" , (unsigned long long ) -btrfs_stack_device_type (&sb->dev_item)); +btrfs_stack_device_flags (&sb->dev_item));printf ("dev_item.total_bytes\t%llu\n" , (unsigned long long )btrfs_stack_device_total_bytes (&sb->dev_item)); printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
kernel-shared/volumes.c +3 -3
@@ -992,7 +992,7 @@ int btrfs_add_device(struct btrfs_trans_handle *trans, device->devid = free_devid;btrfs_set_device_id (leaf, dev_item, device->devid);btrfs_set_device_generation (leaf, dev_item,0 ); -btrfs_set_device_type (leaf, dev_item, device->type); +btrfs_set_device_flags (leaf, dev_item, device->flags);btrfs_set_device_io_align (leaf, dev_item, device->io_align);btrfs_set_device_io_width (leaf, dev_item, device->io_width);btrfs_set_device_sector_size (leaf, dev_item, device->sector_size);
@@ -1050,7 +1050,7 @@ int btrfs_update_device(struct btrfs_trans_handle *trans, dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);btrfs_set_device_id (leaf, dev_item, device->devid); -btrfs_set_device_type (leaf, dev_item, device->type); +btrfs_set_device_flags (leaf, dev_item, device->flags);btrfs_set_device_io_align (leaf, dev_item, device->io_align);btrfs_set_device_io_width (leaf, dev_item, device->io_width);btrfs_set_device_sector_size (leaf, dev_item, device->sector_size);
@@ -2302,7 +2302,7 @@ static int fill_device_from_item(struct extent_buffer *leaf, device->devid =btrfs_device_id (leaf, dev_item); device->total_bytes =btrfs_device_total_bytes (leaf, dev_item); device->bytes_used =btrfs_device_bytes_used (leaf, dev_item); - device->type =btrfs_device_type (leaf, dev_item); + device->flags =btrfs_device_flags (leaf, dev_item); device->io_align =btrfs_device_io_align (leaf, dev_item); device->io_width =btrfs_device_io_width (leaf, dev_item); device->sector_size =btrfs_device_sector_size (leaf, dev_item);
kernel-shared/volumes.h +2 -2
@@ -66,8 +66,8 @@ struct btrfs_device {/* minimal io size for this device */ u32 sector_size; -/* type and info about this device */ -u64 type; +/* device flags (e.g. allocation hint) */ +u64 flags;/* physical drive uuid (or lvm uuid) */ u8 uuid[BTRFS_UUID_SIZE ];
mkfs/common.c +1 -1
@@ -445,7 +445,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)btrfs_set_device_io_align (buf, dev_item, cfg->sectorsize);btrfs_set_device_io_width (buf, dev_item, cfg->sectorsize);btrfs_set_device_sector_size (buf, dev_item, cfg->sectorsize); -btrfs_set_device_type (buf, dev_item,0 ); +btrfs_set_device_flags (buf, dev_item,0 ); write_extent_buffer(buf, super.dev_item.uuid, (unsigned long)btrfs_device_uuid(dev_item),
Signature · 1 line
2.34.1
PATCH 2/3 Rename dev_item.type in flags in the command output.
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
As did in the kernel, rename the device->type to device->flags in the printf() functions.
--- kernel-shared/print-tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel-shared/print-tree.c +2 -2
@@ -293,7 +293,7 @@ static void print_dev_item(struct extent_buffer *eb, BTRFS_UUID_SIZE);uuid_unparse (fsid, fsid_str);printf ("\t\tdevid %llu total_bytes %llu bytes_used %llu\n" -"\t\tio_align %u io_width %u sector_size %u type %llu\n" +"\t\tio_align %u io_width %u sector_size %u flags %llu\n" "\t\tgeneration %llu start_offset %llu dev_group %u\n" "\t\tseek_speed %hhu bandwidth %hhu\n" "\t\tuuid %s\n"
@@ -2051,7 +2051,7 @@ void btrfs_print_superblock(struct btrfs_super_block *sb, int full)printf ("dev_item.fsid\t\t%s %s\n" , buf, cmp_res ?"[match]" :"[DON'T MATCH]" ); -printf ("dev_item.type\t\t%llu\n" , (unsigned long long ) +printf ("dev_item.flags\t\t%llu\n" , (unsigned long long )btrfs_stack_device_flags (&sb->dev_item));printf ("dev_item.total_bytes\t%llu\n" , (unsigned long long )btrfs_stack_device_total_bytes (&sb->dev_item));
Signature · 1 line
2.34.1
PATCH 3/3 Read/change the allocation_hint prop when unmounted
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
This patch enable the changing of the allocation_hint even when the filesystem is unmounted.
--- cmds/property.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)
cmds/property.c +115 -0
@@ -28,6 +28,9 @@#include "cmds/commands.h" #include "cmds/props.h" #include "kernel-shared/ctree.h" +#include "kernel-shared/volumes.h" +#include "kernel-shared/disk-io.h" +#include "kernel-shared/transaction.h" #include "common/open-utils.h" #include "common/utils.h" #include "common/help.h"
@@ -377,6 +380,115 @@ static struct ull_charp_pair_t { {0, NULL} }; +static int find_device (const char *object,struct btrfs_device **device_ret) +{ +struct btrfs_fs_devices *fs_devices; +struct list_head *fs_uuids; +struct stat sttarget, st; + +if (stat (object, &sttarget) <0 ) +return -EACCES ; + + fs_uuids =btrfs_scanned_uuids (); + +list_for_each_entry (fs_devices, fs_uuids, list) { +struct btrfs_device *device; + +list_for_each_entry (device, &fs_devices->devices, dev_list) { + +if (stat (device->name, &st) <0 ) +return -EACCES ; + +if (st.st_rdev == sttarget.st_rdev) { + *device_ret = device; +return 0 ; + } + } + } + +return -ENODEV ; +} + +static int prop_allocation_hint_unmounted (const char *object, +const char *name, +const char *val) +{ + +struct btrfs_device *device; +int ret; +struct btrfs_root *root =NULL ; + + root =open_ctree (object,btrfs_sb_offset (0 ),0 ); +if (!root) +return -ENODEV ; + + ret =find_device (object, &device); +if (ret) + goto out; + +if (!val) { +int i; +u64 v = device->flags &BTRFS_DEV_ALLOCATION_HINT_MASK ; + +for (i =0 ; allocation_hint_description[i].descr ; i++) +if (v == allocation_hint_description[i].value) +break ; + +if (allocation_hint_description[i].descr) +printf ("allocation_hint=%s\n" , + allocation_hint_description[i].descr); +else +printf ("allocation_hint=unknown:%llu\n" , v); + ret =0 ; + }else { +struct btrfs_trans_handle *trans; +int i; +u64 v; + +for (i =0 ; allocation_hint_description[i].descr ; i++) +if (!strcmp (val, allocation_hint_description[i].descr)) +break ; + +if (allocation_hint_description[i].descr) { + v = allocation_hint_description[i].value; + }else if (sscanf (val,"%llu" , &v) !=1 ) { +error ("Invalid value '%s'\n" , val); + ret =-3 ; + goto out; + } +if (v & ~BTRFS_DEV_ALLOCATION_HINT_MASK ) { +error ("Invalid value '%s'\n" , val); + ret =-3 ; + goto out; + } + + trans =btrfs_start_transaction (device->fs_info->chunk_root,1 ); +if (IS_ERR (trans)) { + ret =PTR_ERR (trans); + goto out; + } + +/* Manually update the device item in chunk tree */ + ret =btrfs_update_device (trans, device); +if (ret <0 ) { + errno = -ret; + goto out; + } + +/* +* Commit transaction not only to save the above change but also update +* the device item in super block. +*/ + ret =btrfs_commit_transaction (trans, device->fs_info->chunk_root); + + } +out: +if (root) +close_ctree (root); +return ret; + +} + static int prop_allocation_hint(enum prop_object_type type, const char *object, const char *name,
@@ -394,6 +506,9 @@ static int prop_allocation_hint(enum prop_object_type type, ret =btrfs_find_devid_uuid_by_dev (object, &devid, sysfs_file +sizeof (BTRFSYSFS ) -1 ); +if (ret == -ENODEV ) +return prop_allocation_hint_unmounted (object, name, val); +if (ret) goto out;
Signature · 1 line
2.34.1