btrfs-progs: mkfs: add --seed option to sprout a seed device at mkfs time
Subsystems (3)
cover btrfs-progs: mkfs: add --seed option to sprout a seed device at mkfs time
This branch can be fetched from github:
https://github.com/adam900710/btrfs-progs/tree/mkfs_seed
Which contains all the fixes submitted to the mailing list, along with the patchset.
Sometime ago I purposed to make seed sprout done in user space, now here comes the working prototype.
To support such seemingly easy feature, it's way more complex than I thought:
- Delay chunk/dev extent items insertion
- Add a way to track above delayed items for chunk allocation This allows btrfs to allocate multiple chunks in the same transaction, and should reduce possible false ENOSPC bugs in progs.
Although further cleanup is not in the patchset.
- Implement quirks for sprout
* Update device item for seed device
* Force metadata/system chunk allocation during sprout
* Relocate system chunks to avoid seed device in sys_chunk_array
* Remove empty seed chunks
Those quirks all have specific helpers implemented in mkfs/sprout.c.
Personally speaking we can enhance kernel/progs to handle seed devices in sys_chunk_array, but since it's the long existing behavior, I choose to keep the same behavior to avoid compatibility problems.
With all these solved, I intentionally add some limitation on the new --seed option:
- Ignore -m/-d profiles specification
Just like kernel, we inherit the old profile from the seed device. So if those options are set, we just output a warning and continue.
- Limit the source seed device to be a single device fs
In fact, I don't even think multi-device seed fs is sane. Like a two-disks RAID1 seed fs, adding a device will force we allocate SINGLE chunk, as we have no way to directly add two device in one go.
- Only accept one sprout device
This is completely an artificial limit. Just because I don't see much usefulness to have an completely empty device added.
- Reject --rootdir
This is a preventive behavior, as --rootdir can easily conflicts with the existing context from seed device.
Currently there is only one usecase that can not be replace by the user space sprout:
Read-only mount of the seed device and sprout without unmount.
Which can be useful for certain liveCD usage.
However even liveCD is no longer providing old-school RO fs as root fs. Nowadays most liveCD uses memory block device, combined with overlayfs to provide a RW root fs.
Thus if we go that way, the user space sprout can still be a very useful feature.
Although the feature works fine locally, and I have already done the patch split to contain the changeset, the main point for the patchset is still to evaluate whether this is a solid idea.
Thus any feedback on the feature (including rejection) is welcomed.
Qu Wenruo (10):
btrfs-progs: refactor find_free_dev_extent_start() for later expansion
btrfs-progs: delay chunk and device extent items insertion
btrfs-progs: mkfs: introduce helper to set seed flag
btrfs-progs: mkfs: avoid error out if some trees exist
btrfs-progs: extract btrfs_fs_devices structure allocation into a
helper
btrfs-progs: mkfs/sprout: add a helper to update generation for seed
device
btrfs-progs: mkfs/sprout: introduce helper to force allocating a chunk
btrfs-progs: mkfs/sprout: introduce a helper to relocate system chunks
btrfs-progs: mkfs/sprout: introduce a helper to remove empty system
chunks from seed device
btrfs-progs: mkfs: add support for seed sprout
Documentation/mkfs.btrfs.rst | 13 + Makefile | 2 +- kernel-shared/ctree.h | 2 + kernel-shared/extent-tree.c | 17 +- kernel-shared/transaction.c | 77 ++++++ kernel-shared/transaction.h | 12 + kernel-shared/volumes.c | 280 ++++++++++++--------- kernel-shared/volumes.h | 12 + mkfs/main.c | 133 +++++++++- mkfs/sprout.c | 465 +++++++++++++++++++++++++++++++++++ mkfs/sprout.h | 13 + 11 files changed, 900 insertions(+), 126 deletions(-) create mode 100644 mkfs/sprout.c create mode 100644 mkfs/sprout.h
Signature · 1 line
2.35.1
RFC PATCH 1/10 btrfs-progs: refactor find_free_dev_extent_start() for later expansion
[CURRENT BEHAVIOR]
We iterate through all dev extents, and calcluate the hole immediately.
This is fine, but for the later incoming delayed chunk allocation, it's pretty hard to handle both dev extents in dev tree and in delayed chunks.
[REFACTOR]
This patch will split the search into two part:
1. Populate @used_root cache tree
That tree will record all used device extents
2. Iterate through each cache extent to calculate holes
With such separate loops, we can easily handle multiple dev extents source, just queue them all into @used_root.
The 2nd part will handle everything well.
Since we're here, also add a comment on why we may want to re-search after iterating all dev extents.
(Which is for zoned device boundary)
--- kernel-shared/volumes.c | 88 ++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 37 deletions(-)
kernel-shared/volumes.c +51 -37
@@ -653,58 +653,59 @@ static int find_free_dev_extent_start(struct btrfs_device *device, u64 num_bytes, u64 search_start, u64 *start, u64 *len) { - struct btrfs_key key; struct btrfs_root *root = device->dev_root; -struct btrfs_dev_extent *dev_extent; -struct btrfs_path *path; -u64 hole_size; +struct btrfs_path path = {0 }; +struct btrfs_key key; +struct cache_tree used_root; +struct cache_extent *pe;u64 max_hole_start;u64 max_hole_size; -u64 extent_end;u64 search_end = device->total_bytes; +u64 hole_size;int ret; -int slot; -struct extent_buffer *l;u64 zone_size =0 ; +cache_tree_init (&used_root);if (device->zone_info) zone_size = device->zone_info->zone_size; search_start =dev_extent_search_start (device, search_start); - path =btrfs_alloc_path (); -if (!path) -return -ENOMEM ; +btrfs_init_path (&path); + path.reada =READA_FORWARD ; max_hole_start = search_start; max_hole_size =0 ; -again:if (search_start >= search_end) { ret = -ENOSPC ; goto out; } - path->reada =READA_FORWARD ; - key.objectid = device->devid; key.offset = search_start; key.type =BTRFS_DEV_EXTENT_KEY ; - ret =btrfs_search_slot (NULL , root, &key, path,0 ,0 ); + ret =btrfs_search_slot (NULL , root, &key, &path,0 ,0 );if (ret <0 ) goto out;if (ret >0 ) { - ret =btrfs_previous_item (root, path, key.objectid, key.type); + ret =btrfs_previous_item (root, &path, key.objectid, key.type);if (ret <0 ) goto out; } +/* +* Iterate through all the dev extents in the device tree, and add them +* into the used tree +*/ while (1 ) { - l = path->nodes[0 ]; - slot = path->slots[0 ]; +struct extent_buffer *l = path.nodes[0 ]; +int slot = path.slots[0 ]; +struct btrfs_dev_extent *de; +if (slot >=btrfs_header_nritems (l)) { - ret =btrfs_next_leaf (root, path); + ret =btrfs_next_leaf (root, &path);if (ret ==0 )continue ;if (ret <0 )
@@ -723,8 +724,27 @@ again:if (key.type !=BTRFS_DEV_EXTENT_KEY ) goto next; -if (key.offset > search_start) { - hole_size = key.offset - search_start; +/* Got a dev extent item, add it to used_root */ + de = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); + ret =add_merge_cache_extent (&used_root, key.offset, +btrfs_dev_extent_length (l, de)); +if (ret <0 ) + goto out; +next: + path.slots[0 ]++; +cond_resched (); + } + +again: +/* +* Now used_root contains all the dev extents. Iterate through the tree +* to grab holes. +*/ +for (pe =first_cache_extent (&used_root); pe; + pe =next_cache_extent (pe)) { +if (pe->start > search_start) { + + hole_size = pe->start - search_start;dev_extent_hole_check (device, &search_start, &hole_size, num_bytes);
@@ -732,7 +752,6 @@ again: max_hole_start = search_start; max_hole_size = hole_size; } -/* * If this free space is greater than which we need, * it must be the max free space that we have found
@@ -747,15 +766,7 @@ again: goto out; } } - - dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); - extent_end = key.offset +btrfs_dev_extent_length (l, - dev_extent); -if (extent_end > search_start) - search_start = extent_end; -next: - path->slots[0 ]++; -cond_resched (); + search_start =max (search_start, pe->start + pe->size); }/*
@@ -765,15 +776,17 @@ next:*/ if (search_end > search_start) { hole_size = search_end - search_start; + +/* +* Our hole crossed zone boundary, need to re-do the search +* from the zone boundary. +*/ if (dev_extent_hole_check (device, &search_start, &hole_size, - num_bytes)) { -btrfs_release_path (path); + num_bytes)) goto again; - } - -if (hole_size > max_hole_size) { - max_hole_start = search_start; +if (hole_size > max_hole_start) { max_hole_size = hole_size; + max_hole_start = search_start; } }
@@ -784,8 +797,9 @@ next: ret =0 ; out: +btrfs_release_path (&path); +free_extent_cache_tree (&used_root);ASSERT (zone_size ==0 ||IS_ALIGNED (max_hole_start, zone_size)); -btrfs_free_path (path); *start = max_hole_start;if (len) *len = max_hole_size;
Signature · 1 line
2.35.1
RFC PATCH 2/10 btrfs-progs: delay chunk and device extent items insertion
In reply to Qu WenruoCurrently btrfs-progs always insert chunk and device extent items at btrfs_chunk_alloc() time.
This behavior has one limitation, if we don't have enough space for even CoWing the chunk and device trees, then we can not allocate new chunks to fulfill our btrfs_reserve_extent() call.
This is not a problem so far as we always make sure we have enough space.
But it's going to cause problem for the incoming sprout support at mkfs time.
As when sprouting the seed fs, initially there is no RW block group at all, we must allocate new chunks to do anything.
To resolve the problem, we need to delay chunk item insertion, so that in do_chunk_alloc() we can create new chunk mapping with new block group cache without triggering tree block CoW.
With block group cache inserted, then we're able to call btrfs_reserve_extent() and do regular tree block CoW or whatever.
--- kernel-shared/extent-tree.c | 1 - kernel-shared/transaction.c | 77 ++++++++++++++++++ kernel-shared/transaction.h | 12 +++ kernel-shared/volumes.c | 154 ++++++++++++++++++++---------------- kernel-shared/volumes.h | 10 +++ 5 files changed, 187 insertions(+), 67 deletions(-)
kernel-shared/extent-tree.c +0 -1
@@ -1741,7 +1741,6 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans, trans->allocating_chunk =0 ;return 0 ; } -BUG_ON (ret); ret = btrfs_make_block_group(trans, fs_info, 0, flags, start,
kernel-shared/transaction.c +77 -0
105 lines not on this page · raw message
kernel-shared/transaction.h +12 -0
@@ -37,6 +37,15 @@ struct btrfs_trans_handle {struct btrfs_block_group *block_group;struct btrfs_delayed_ref_root delayed_refs;struct list_head dirty_bgs; +struct list_head new_chunks; +struct list_head reserved_dev_extents; +}; + +struct btrfs_reserved_dev_extent { +struct list_head list; +struct btrfs_device *dev; +u64 physical; +u64 length; }; struct btrfs_trans_handle* btrfs_start_transaction(struct btrfs_root *root,
@@ -48,5 +57,8 @@ int commit_tree_roots(struct btrfs_trans_handle *trans,int btrfs_commit_transaction (struct btrfs_trans_handle *trans,struct btrfs_root *root);void btrfs_abort_transaction (struct btrfs_trans_handle *trans,int error); +int btrfs_add_reserved_device_extent (struct btrfs_trans_handle *trans, +struct btrfs_device *dev,u64 physical, +u64 length);#endif
kernel-shared/volumes.c +88 -66
252 lines not on this page · raw message
kernel-shared/volumes.h +10 -0
@@ -113,6 +113,14 @@ struct btrfs_multi_bio {struct map_lookup {struct cache_extent ce; + +/* +* Newly allocated chunk map will be added to trans::new_chunks, +* and its chunk/dev_extent/block_group items will be inserted into +* the trees at transaction commit time. +*/ +struct list_head list; +u64 type;int io_align;int io_width;
@@ -264,6 +272,8 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 *num_bytes, u64 type);int btrfs_alloc_data_chunk (struct btrfs_trans_handle *trans,struct btrfs_fs_info *fs_info,u64 *start,u64 num_bytes); +int btrfs_insert_chunk_item (struct btrfs_trans_handle *trans, +struct map_lookup *map);int btrfs_open_devices (struct btrfs_fs_info *fs_info,struct btrfs_fs_devices *fs_devices,int flags);int btrfs_close_devices (struct btrfs_fs_devices *fs_devices);
Signature · 1 line
2.35.1
RFC PATCH 3/10 btrfs-progs: mkfs: introduce helper to set seed flag
In reply to Qu WenruoThe new helper, prepare_seed_device(), will be used for later mkfs time seed sprouting.
Although it has way more checks than btrfstune:
- csum_type/sectorsize/nodesize/features checks
Any mismatch means we can not use that seed device Normally it should not be a problem for default mkfs profiles, but since we're going to do the sprout at mkfs time, we must do these checks.
- Device number check
I see no reason nor use-case to support nested/multi-device seed at mkfs time.
Currently mkfs.btrfs will accept --seed (undocumented, and experimental) and call the helper to set seed flag on the target, but will not really do the sprout yet.
--- Makefile | 2 +- mkfs/main.c | 17 ++++++++ mkfs/sprout.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ mkfs/sprout.h | 10 +++++ 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 mkfs/sprout.c create mode 100644 mkfs/sprout.h
Makefile +1 -1
@@ -226,7 +226,7 @@ libbtrfsutil_objects = libbtrfsutil/errors.o libbtrfsutil/filesystem.o \ convert_objects = convert/main.o convert/common.o convert/source-fs.o \ convert/source-ext2.o convert/source-reiserfs.o \ mkfs/common.o -mkfs_objects = mkfs/main.o mkfs/common.o mkfs/rootdir.o +mkfs_objects = mkfs/main.o mkfs/common.o mkfs/rootdir.o mkfs/sprout.o image_objects = image/main.o image/sanitize.o all_objects = $(objects) $(cmds_objects) $(libbtrfs_objects) $(convert_objects) \ $(mkfs_objects) $(image_objects) $(libbtrfsutil_objects)
mkfs/main.c +17 -0
@@ -48,6 +48,7 @@#include "common/parse-utils.h" #include "mkfs/common.h" #include "mkfs/rootdir.h" +#include "mkfs/sprout.h" #include "common/fsfeatures.h" #include "common/box.h" #include "common/units.h"
@@ -999,6 +1000,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)bool force_overwrite = false;int oflags;char *source_dir =NULL ; +char *seed_dev =NULL ;bool source_dir_set = false;bool shrink_rootdir = false;u64 source_dir_size =0 ;
@@ -1024,6 +1026,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)GETOPT_VAL_SHRINK =257 ,GETOPT_VAL_CHECKSUM ,GETOPT_VAL_GLOBAL_ROOTS , + GETOPT_VAL_SEED_DEV, };static const struct option long_options[] = { { "byte-count", required_argument, NULL, 'b' },
@@ -1050,6 +1053,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv) { "shrink", no_argument, NULL, GETOPT_VAL_SHRINK }, #if EXPERIMENTAL { "num-global-roots", required_argument, NULL, GETOPT_VAL_GLOBAL_ROOTS }, + { "seed", required_argument, NULL, GETOPT_VAL_SEED_DEV }, #endif { "help", no_argument, NULL, GETOPT_VAL_HELP }, { NULL, 0, NULL, 0}
@@ -1158,6 +1162,9 @@ int BOX_MAIN(mkfs)(int argc, char **argv)case GETOPT_VAL_GLOBAL_ROOTS : nr_global_roots = (int )arg_strtou64 (optarg);break ; +case GETOPT_VAL_SEED_DEV : + seed_dev = optarg; +break ;case GETOPT_VAL_HELP :default :print_usage (c !=GETOPT_VAL_HELP );
@@ -1207,6 +1214,16 @@ int BOX_MAIN(mkfs)(int argc, char **argv) } } +if (seed_dev) { + ret =prepare_seed_device (seed_dev, features, csum_type, + sectorsize, nodesize); +if (ret <0 ) { + errno = -ret; +error ("faield to set seed flag on %s: %m" , seed_dev); + goto error; + } + } +while (dev_cnt-- >0 ) { file = argv[optind++];if (source_dir_set &&path_exists (file) ==0 )
mkfs/sprout.c new file +114 -0
114 lines not on this page · raw message
mkfs/sprout.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* Functions only used for seed sprout at mkfs time. */ +#ifndef __BTRFS_MKFS_SPROUT_H__ +#define __BTRFS_MKFS_SPROUT_H__ + +int prepare_seed_device (const char *path,u64 features,u32 csum_type, +u32 sectorsize,u32 nodesize); + +#endif
Signature · 1 line
2.35.1
RFC PATCH 4/10 btrfs-progs: mkfs: avoid error out if some trees exist
In reply to Qu WenruoWith the incoming seed sprout support at mkfs time, we can have quite some trees already exist, those trees includes:
- data reloc tree
- uuid tree
- quota tree
- root dir
Handle the existing tress properly so we won't error out just because the seed device already have the same tree.
--- mkfs/main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-)
mkfs/main.c +72 -2
@@ -712,6 +712,33 @@ static void update_chunk_allocation(struct btrfs_fs_info *fs_info, } } +/* +* Check if we already have an existing tree from seed device. +* +* Return >0 if we have seed device and already have the tree. +* Return 0 if we don't have seed device or the tree doesn't exist. +* Return <0 for error. +*/ +static int check_seed_existing_tree (struct btrfs_fs_info *fs_info, +struct btrfs_key *root_key) +{ +struct btrfs_path path; +int ret; + +if (!fs_info->fs_devices->seed) +return 0 ; + +btrfs_init_path (&path); + ret =btrfs_search_slot (NULL , fs_info->tree_root, root_key, &path,0 ,0 ); +btrfs_release_path (&path); +if (ret <0 ) +return ret; + +if (ret ==0 ) +return 1 ; +return 0 ; +} +static int create_data_reloc_tree (struct btrfs_trans_handle *trans) {struct btrfs_fs_info *fs_info = trans->fs_info;
@@ -726,6 +753,12 @@ static int create_data_reloc_tree(struct btrfs_trans_handle *trans)char *name =".." ;int ret; + ret =check_seed_existing_tree (fs_info, &key); +if (ret <0 ) +return ret; +if (ret >0 ) +return 0 ; + root =btrfs_create_tree (trans, fs_info, &key);if (IS_ERR (root)) { ret =PTR_ERR (root);
@@ -792,7 +825,12 @@ static int create_uuid_tree(struct btrfs_trans_handle *trans) };int ret =0 ; -ASSERT (fs_info->uuid_root ==NULL ); + ret =check_seed_existing_tree (fs_info, &key); +if (ret <0 ) +return ret; +if (ret >0 ) +return 0 ; + root =btrfs_create_tree (trans, fs_info, &key);if (IS_ERR (root)) { ret =PTR_ERR (root);
@@ -896,7 +934,7 @@ static int setup_quota_root(struct btrfs_fs_info *fs_info) {struct btrfs_trans_handle *trans;struct btrfs_qgroup_status_item *qsi; -struct btrfs_root *quota_root; +struct btrfs_root *quota_root = fs_info->quota_root;struct btrfs_path path;struct btrfs_key key;int qgroup_repaired =0 ;
@@ -909,6 +947,15 @@ static int setup_quota_root(struct btrfs_fs_info *fs_info)error ("failed to start transaction: %d (%m)" , ret);return ret; } + key.objectid =BTRFS_QUOTA_TREE_OBJECTID ; + key.type =BTRFS_ROOT_ITEM_KEY ; + key.offset =0 ; + ret =check_seed_existing_tree (fs_info, &key); +if (ret <0 ) + goto fail; +if (ret >0 ) + goto insert_status; + ret =btrfs_create_root (trans, fs_info,BTRFS_QUOTA_TREE_OBJECTID );if (ret <0 ) {error ("failed to create quota root: %d (%m)" , ret);
@@ -927,6 +974,25 @@ static int setup_quota_root(struct btrfs_fs_info *fs_info)error ("failed to insert qgroup status item: %d (%m)" , ret); goto fail; } +btrfs_release_path (&path); + +insert_status: +/* +* We reach here either we're creating a new quota root, or using +* the existing quota root from seed. +* So here we intentionally do a search, other than reusing the +* inserted item, to handle both cases well. +*/ + key.objectid =0 ; + key.type =BTRFS_QGROUP_STATUS_KEY ; + key.offset =0 ; + ret =btrfs_search_slot (trans, quota_root, &key, &path,1 ,0 ); +if (ret >0 ) + ret = -ENOENT ; +if (ret <0 ) { +btrfs_release_path (&path); + goto fail; + } qsi =btrfs_item_ptr (path.nodes[0 ], path.slots[0 ], struct btrfs_qgroup_status_item);
@@ -941,6 +1007,8 @@ static int setup_quota_root(struct btrfs_fs_info *fs_info)/* Currently mkfs will only create one subvolume */ ret =insert_qgroup_items (trans, fs_info,BTRFS_FS_TREE_OBJECTID ); +if (ret == -EEXIST ) + ret =0 ;if (ret <0 ) {error ("failed to insert qgroup items: %d (%m)" , ret); goto fail;
@@ -1554,6 +1622,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv) } ret =make_root_dir (trans, root); +if (ret == -EEXIST && root->fs_info->fs_devices->seed) +return 0 ;if (ret) {error ("failed to setup the root directory: %d" , ret); goto error;
Signature · 1 line
2.35.1
RFC PATCH 5/10 btrfs-progs: extract btrfs_fs_devices structure allocation into a helper
In reply to Qu WenruoThe new helper function, btrfs_alloc_fs_devices() will allocate and initialize a btrfs_fs_devices structure.
This helper will be later used by seed sprout, as we will need to create a new fs_devices for the sproted fs.
--- kernel-shared/volumes.c | 38 +++++++++++++++++++++++++++----------- kernel-shared/volumes.h | 2 ++ 2 files changed, 29 insertions(+), 11 deletions(-)
kernel-shared/volumes.c +27 -11
@@ -331,6 +331,31 @@ static struct btrfs_fs_devices *find_fsid(u8 *fsid, u8 *metadata_uuid)return NULL ; } +struct btrfs_fs_devices *btrfs_alloc_fs_devices (const u8 *fsid, +const u8 *metadata_uuid) +{ +struct btrfs_fs_devices *fs_devices; + + fs_devices =kzalloc (sizeof (*fs_devices),GFP_NOFS ); +if (!fs_devices) +return NULL ; + +INIT_LIST_HEAD (&fs_devices->devices); +list_add (&fs_devices->list, &fs_uuids); +memcpy (fs_devices->fsid, fsid,BTRFS_FSID_SIZE ); + +if (metadata_uuid) +memcpy (fs_devices->metadata_uuid, metadata_uuid, +BTRFS_FSID_SIZE ); +else +memcpy (fs_devices->metadata_uuid, fsid,BTRFS_FSID_SIZE ); + + fs_devices->latest_trans =0 ; + fs_devices->latest_devid = (u64)-1 ; + fs_devices->lowest_devid = (u64)-1 ; +return fs_devices; +} +static int device_list_add (const char *path,struct btrfs_super_block *disk_super,u64 devid,struct btrfs_fs_devices **fs_devices_ret)
@@ -348,22 +373,13 @@ static int device_list_add(const char *path, fs_devices =find_fsid (disk_super->fsid,NULL );if (!fs_devices) { - fs_devices =kzalloc (sizeof (*fs_devices),GFP_NOFS ); + fs_devices =btrfs_alloc_fs_devices (disk_super->fsid, + metadata_uuid ? disk_super->metadata_uuid :NULL );if (!fs_devices)return -ENOMEM ; -INIT_LIST_HEAD (&fs_devices->devices); -list_add (&fs_devices->list, &fs_uuids); -memcpy (fs_devices->fsid, disk_super->fsid,BTRFS_FSID_SIZE ); -if (metadata_uuid) -memcpy (fs_devices->metadata_uuid, - disk_super->metadata_uuid,BTRFS_FSID_SIZE ); -else -memcpy (fs_devices->metadata_uuid, fs_devices->fsid, -BTRFS_FSID_SIZE ); fs_devices->latest_devid = devid; fs_devices->latest_trans = found_transid; - fs_devices->lowest_devid = (u64)-1 ; fs_devices->chunk_alloc_policy =BTRFS_CHUNK_ALLOC_REGULAR ; device =NULL ; }else {
kernel-shared/volumes.h +2 -0
@@ -276,6 +276,8 @@ int btrfs_insert_chunk_item(struct btrfs_trans_handle *trans, struct map_lookup *map);int btrfs_open_devices (struct btrfs_fs_info *fs_info,struct btrfs_fs_devices *fs_devices,int flags); +struct btrfs_fs_devices *btrfs_alloc_fs_devices (const u8 *fsid, +const u8 *metadata_uuid);int btrfs_close_devices (struct btrfs_fs_devices *fs_devices);void btrfs_close_all_devices (void ); int btrfs_insert_dev_extent(struct btrfs_trans_handle *trans,
Signature · 1 line
2.35.1
RFC PATCH 6/10 btrfs-progs: mkfs/sprout: add a helper to update generation for seed device
In reply to Qu WenruoFor non-sprouted fs, generation of a device item is always 0.
But for sprouted fs, we use btrfs_dev_item::generation to store the generation of expected seed device generation.
Here we introduce a helper to update the generation of seed device, for the incoming seed sprout at mkfs time.
--- mkfs/sprout.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
mkfs/sprout.c +37 -0
@@ -112,3 +112,40 @@ out:close_ctree (fs_info->tree_root);return ret; } + +static int update_seed_dev_geneartion (struct btrfs_trans_handle *trans) +{ +struct btrfs_fs_info *fs_info = trans->fs_info; +struct btrfs_fs_devices *seed_devs = fs_info->fs_devices->seed; +struct btrfs_device *dev; +struct btrfs_path path; +int ret; + +ASSERT (seed_devs); + +btrfs_init_path (&path); +list_for_each_entry (dev, &seed_devs->devices, dev_list) { +struct btrfs_dev_item *di; +struct btrfs_key key; + + key.objectid =BTRFS_DEV_ITEMS_OBJECTID ; + key.type =BTRFS_DEV_ITEM_KEY ; + key.offset = dev->devid; + + ret =btrfs_search_slot (trans, fs_info->chunk_root, &key, &path, +0 ,1 ); +if (ret >0 ) + ret = -ENOENT ; +if (ret <0 ) +break ; + + di =btrfs_item_ptr (path.nodes[0 ], path.slots[0 ], + struct btrfs_dev_item); +btrfs_set_device_generation (path.nodes[0 ], di, dev->generation); +btrfs_mark_buffer_dirty (path.nodes[0 ]); +btrfs_release_path (&path); + } +btrfs_release_path (&path); +return ret; +} +
Signature · 1 line
2.35.1
RFC PATCH 7/10 btrfs-progs: mkfs/sprout: introduce helper to force allocating a chunk
In reply to Qu WenruoThe new helper, sprout_alloc_one_chunk(), will force allocating a chunk, mostly using the profile from the seed device.
With the delayed chunk allocation, we really only need to grab the target profile, and call btrfs_alloc_chunk() followed by btrfs_make_block_group().
For mixed block group, we need btrfs_space_info to determine if a profile needs extra types, so here we expoert btrfs_find_space_info().
--- kernel-shared/ctree.h | 2 ++ kernel-shared/extent-tree.c | 16 ++++++++-------- mkfs/sprout.c | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-)
kernel-shared/ctree.h +2 -0
@@ -2614,6 +2614,8 @@ u64 btrfs_name_hash(const char *name, int len);u64 btrfs_extref_hash (u64 parent_objectid,const char *name,int len);/* extent-tree.c */ +struct btrfs_space_info *btrfs_find_space_info (struct btrfs_fs_info *info, +u64 flags); int btrfs_reserve_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 num_bytes, u64 empty_size,
kernel-shared/extent-tree.c +8 -8
@@ -1592,8 +1592,8 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)return ret; } -static struct btrfs_space_info *__find_space_info (struct btrfs_fs_info *info, -u64 flags) +struct btrfs_space_info *btrfs_find_space_info (struct btrfs_fs_info *info, +u64 flags) {struct btrfs_space_info *found;
@@ -1617,7 +1617,7 @@ static int free_space_info(struct btrfs_fs_info *fs_info, u64 flags,if (bytes_used)return -ENOTEMPTY ; - found =__find_space_info (fs_info, flags); + found =btrfs_find_space_info (fs_info, flags);if (!found)return -ENOENT ;if (found->total_bytes < total_bytes) {
@@ -1638,7 +1638,7 @@ int update_space_info(struct btrfs_fs_info *info, u64 flags, {struct btrfs_space_info *found; - found =__find_space_info (info, flags); + found =btrfs_find_space_info (info, flags);if (found) { found->total_bytes += total_bytes; found->bytes_used += bytes_used;
@@ -1694,7 +1694,7 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,u64 num_bytes;int ret; - space_info =__find_space_info (fs_info, flags); + space_info =btrfs_find_space_info (fs_info, flags);if (!space_info) { ret =update_space_info (fs_info, flags,0 ,0 , &space_info);BUG_ON (ret);
@@ -2381,7 +2381,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,u64 start, end;int ret; - sinfo =__find_space_info (fs_info,BTRFS_BLOCK_GROUP_METADATA ); + sinfo =btrfs_find_space_info (fs_info,BTRFS_BLOCK_GROUP_METADATA );ASSERT (sinfo); ins.objectid = node->bytenr;
@@ -2478,7 +2478,7 @@ static int alloc_tree_block(struct btrfs_trans_handle *trans,if (!extent_op)return -ENOMEM ; - sinfo =__find_space_info (fs_info,BTRFS_BLOCK_GROUP_METADATA ); + sinfo =btrfs_find_space_info (fs_info,BTRFS_BLOCK_GROUP_METADATA );if (!sinfo) {error ("Corrupted fs, no valid METADATA block group found" );return -EUCLEAN ;
@@ -3657,7 +3657,7 @@ int cleanup_ref_head(struct btrfs_trans_handle *trans,if (!head->is_data) {struct btrfs_space_info *sinfo; - sinfo =__find_space_info (trans->fs_info, + sinfo =btrfs_find_space_info (trans->fs_info,BTRFS_BLOCK_GROUP_METADATA );ASSERT (sinfo); sinfo->bytes_reserved -= head->num_bytes;
mkfs/sprout.c +38 -0
@@ -149,3 +149,41 @@ static int update_seed_dev_geneartion(struct btrfs_trans_handle *trans)return ret; } +static int sprout_alloc_one_chunk (struct btrfs_trans_handle *trans,u64 type) +{ +struct btrfs_fs_info *fs_info = trans->fs_info; +struct btrfs_space_info *space_info; +u64 chunk_start; +u64 chunk_size; +u64 chunk_flags; +int ret; + + space_info =btrfs_find_space_info (fs_info, type); +if (!space_info) +return -ENOENT ; + +if (type &BTRFS_BLOCK_GROUP_METADATA ) + chunk_flags = fs_info->avail_metadata_alloc_bits & + fs_info->metadata_alloc_profile; +if (type &BTRFS_BLOCK_GROUP_SYSTEM ) + chunk_flags = fs_info->avail_system_alloc_bits & + fs_info->system_alloc_profile; +if (type &BTRFS_BLOCK_GROUP_DATA ) + chunk_flags = fs_info->avail_data_alloc_bits & + fs_info->data_alloc_profile; +/* This is for mixed profile */ + chunk_flags |= space_info->flags; + + ret =btrfs_alloc_chunk (trans, fs_info, &chunk_start, &chunk_size, + chunk_flags); +if (ret <0 ) + goto error; + ret =btrfs_make_block_group (trans, fs_info,0 , chunk_flags, + chunk_start, chunk_size); +if (ret <0 ) + goto error; +return ret; +error: +btrfs_abort_transaction (trans, ret); +return ret; +}
Signature · 1 line
2.35.1
RFC PATCH 8/10 btrfs-progs: mkfs/sprout: introduce a helper to relocate system chunks
In reply to Qu WenruoIn kernel, for seed sprout we always relocate all system chunks.
The reason is a little complex, at mount time, especially for sys_chunk_array processing, we don't have any idea which device is seed device.
And all we can access is just all deviecs with the same fsid, not even knowing if that fsid has any seed device.
Thus kernel choose to relocate all system chunks, and remove the empty seed system chunks to allow a proper mount.
Here we do the same thing, but since in progs we don't have chunk relocation ability, here we just CoW every leaf, then all nodes will also be CoWed, thus the whole chunk tree will be relocated.
--- mkfs/sprout.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
mkfs/sprout.c +54 -0
@@ -187,3 +187,57 @@ error:btrfs_abort_transaction (trans, ret);return ret; } + +/* +* Relocate all chunk tree blocks by CoWing every leaf. +* +* Kernel and btrfs-progs requires sys_chunk_array to only contain devices from +* current fsid. +* +* As btrfs_stripe only contains devid and dev uuid, no fsid to determine if +* the current device is a seed. +* +* And at sys_chunk_array read time, btrfs doesn't have seed devices setup, +* thus if we have a chunk with seed device in it, kernel and progs will +* treat it as missing directly. +* +* So we need this function to relocate all system chunks from seed device, +* so later we can cleanup those system chunks. +*/ +static int sprout_relocate_chunk_tree (struct btrfs_trans_handle *trans) +{ +struct btrfs_fs_info *fs_info = trans->fs_info; +struct btrfs_root *chunk_root = fs_info->chunk_root; +struct btrfs_key key = {0 }; +struct btrfs_path path; +int ret; + +while (true) { +btrfs_init_path (&path); + ret =btrfs_search_slot (trans, chunk_root, &key, &path,0 ,1 ); +if (ret <0 ) +break ; +/* +* This is for the first search, we should be at the first item +* of chunk tree. That's expected. +*/ +if (ret >0 ) { +ASSERT (key.offset ==0 && key.type ==0 && + key.objectid ==0 ); + ret =0 ; + } + + ret =btrfs_next_leaf (chunk_root, &path); +if (ret <0 ) +break ; +if (ret >0 ) { + ret =0 ; +break ; + } +/* Save the key for next search */ +btrfs_item_key_to_cpu (path.nodes[0 ], &key,0 ); +btrfs_release_path (&path); + } +btrfs_release_path (&path); +return ret; +}
Signature · 1 line
2.35.1
RFC PATCH 9/10 btrfs-progs: mkfs/sprout: introduce a helper to remove empty system chunks from seed device
In reply to Qu WenruoEven if an empty system chunk is still in sys_chunk_array, kernel will reject the mount.
So here we introduce the helper to do the removal, by iterating through all block groups, and remove the block group if it's a system chunk and contains stripe which points to the seed device.
--- mkfs/sprout.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
mkfs/sprout.c +53 -0
@@ -241,3 +241,56 @@ static int sprout_relocate_chunk_tree(struct btrfs_trans_handle *trans)btrfs_release_path (&path);return ret; } + +static int sprout_remove_seed_sys_chunk (struct btrfs_trans_handle *trans) +{ +struct btrfs_fs_info *fs_info = trans->fs_info; +struct btrfs_block_group *bg; +struct btrfs_device *seed_dev; +int ret; + +/* We should have exacly one seed device */ +ASSERT (fs_info->fs_devices->seed); +ASSERT (fs_info->fs_devices->seed->devices.next == + fs_info->fs_devices->seed->devices.prev); + seed_dev =list_entry (fs_info->fs_devices->seed->devices.next, + struct btrfs_device, dev_list); + bg =btrfs_lookup_first_block_group (fs_info,0 ); +while (bg) { +const u64 cur = bg->start + bg->length; + +struct cache_extent *ce; +struct map_lookup *map; +bool delete = false; +int i; + +if (!(bg->flags &BTRFS_BLOCK_GROUP_SYSTEM )) + goto next; + + ce =search_cache_extent (&fs_info->mapping_tree.cache_tree, + bg->start); +if (!ce) { +/* No chunk map for an bg, a big problem */ +error ("no chunk map for block group at %llu" , bg->start); +return -EUCLEAN ; + } + map = container_of(ce, struct map_lookup, ce); + +for (i =0 ; i < map->num_stripes; i++) { +if (map->stripes[i].dev == seed_dev) { + delete = true; +break ; + } + } +if (!delete) + goto next; + + ret =btrfs_remove_block_group (trans, bg->start, bg->length); +if (ret <0 ) +return ret; +next: +/* Has to using @cur, as the current bg may has been deleted */ + bg =btrfs_lookup_first_block_group (fs_info, cur); + } +return 0 ; +}
Signature · 1 line
2.35.1
RFC PATCH 10/10 btrfs-progs: mkfs: add support for seed sprout
In reply to Qu WenruoThis patch will add a new option "--seed <device>" to mkfs, allowing us to mark specified device as seed (if not yet a seed device), then sprouting it with the extra rw device.
The new --seed option will be hidden underneath the experimental feature.
Currently, --seed has extra limitations, including:
- csum type/sectorsize/nodesize/features must match seeding device The very basic sanity checks.
- Ignoreing -m/-d options
As we will inherit the profiles from the seed device.
- Only accept single deviced seed
This is not a technical limit, but purely to make seed usage less flex, and less error prone.
- Only accept single rw device
We can already add as many deviecs as regular mkfs, I just didn't see much usefulness from it.
- Rejects --rootdir option
It's super easy to have --rootdir to conflict with the existing files inherited from the seed fs.
With the new "--seed" option, we can replace the following workflow:
mkfs.btrfs -f $seed
mount $seed $mnt
# Populate $mnt with contents we want
umount $mnt
btrfstune -S1 $seed
mount $seed $mnt
btrfs dev add -f $rw_dev $mnt
mount -o remount,rw $mnt
With much less commands:
mkfs.btrfs -f $seed
mount $seed $mnt
# Populate $mnt with contents we want
umount $mnt
mkfs.btrfs --seed $seed $rw_dev
mount $rw_dev $mnt
--- Documentation/mkfs.btrfs.rst | 13 +++ mkfs/main.c | 42 +++++++++ mkfs/sprout.c | 169 +++++++++++++++++++++++++++++++++++ mkfs/sprout.h | 3 + 4 files changed, 227 insertions(+)
Documentation/mkfs.btrfs.rst +13 -0
@@ -136,6 +136,19 @@ OPTIONS contain the files from *rootdir*. Since version 4.14.1 the filesystem size is not minimized. Please see option *--shrink* if you need that functionality. +--seed <seed> + Use *seed* as the seed device, then sprout it with the extra *device*. + If *seed* is not a seed device yet, mkfs will mark it with seed flag, + other than that, no other write will reach *seed* device. + + Check *SEEDING DEVICE* section of ``btrfs(5)`` for more details and the + alternative way of sprouting a seed device. + + .. note:: + With this option, *seed* must only contain a single device + filesystem, and we only accept one single device to be added + to sprout the seed device. + --shrink Shrink the filesystem to its minimal size, only works with *--rootdir* option.
mkfs/main.c +42 -0
66 lines not on this page · raw message
mkfs/sprout.c +169 -0
182 lines not on this page · raw message
mkfs/sprout.h +3 -0
@@ -6,5 +6,8 @@int prepare_seed_device (const char *path,u64 features,u32 csum_type,u32 sectorsize,u32 nodesize); +int sprout_seed_fs (struct btrfs_fs_info *fs_info, +struct btrfs_mkfs_config *cfg,const char *path, +u64 dev_block_count);#endif
Signature · 1 line
2.35.1