* [PATCH v4 1/4] btrfs-progs: image: Introduce framework for more dump versions
2020-03-18 7:54 [PATCH v4 0/4] btrfs-progs: image: Enhancement with new data dump feature Qu Wenruo
@ 2020-03-18 7:54 ` Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 2/4] btrfs-progs: image: Introduce -d option to dump data Qu Wenruo
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2020-03-18 7:54 UTC (permalink / raw)
To: linux-btrfs
The original dump format only contains a @magic member to verify the
format, this means if we want to introduce new on-disk format or change
certain size limit, we can only introduce new magic as kind of version.
This patch will introduce the framework to allow multiple magic to
co-exist for further functions.
This patch will introduce the following members for each dump version.
- max_pending_size
The threshold size for an cluster. It's not a hard limit but a soft
one. One cluster can go larger than max_pending_size for one item, but
next item would go to next cluster.
- magic_cpu
The magic number in CPU endian.
- extra_sb_flags
If the super block of this restore needs extra super block flags like
BTRFS_SUPER_FLAG_METADUMP_V2.
For incoming data dump feature, we don't need any extra super block
flags.
This change also implies that all image dumps will use the same magic
for all clusters. No mixing is allowed, as we will use the first cluster
to determine the dump version.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
image/main.c | 72 ++++++++++++++++++++++++++++++++++++++++++------
image/metadump.h | 13 +++++++--
2 files changed, 74 insertions(+), 11 deletions(-)
diff --git a/image/main.c b/image/main.c
index 95eb3cc3d4de..12faf5350566 100644
--- a/image/main.c
+++ b/image/main.c
@@ -44,6 +44,19 @@
#define MAX_WORKER_THREADS (32)
+const struct dump_version dump_versions[NR_DUMP_VERSIONS] = {
+ /*
+ * The original format, which only supports tree blocks and
+ * free space cache dump.
+ */
+ { .version = 0,
+ .max_pending_size = SZ_256K,
+ .magic_cpu = 0xbd5c25e27295668bULL,
+ .extra_sb_flags = 1 }
+};
+
+const struct dump_version *current_version = &dump_versions[0];
+
struct async_work {
struct list_head list;
struct list_head ordered;
@@ -404,7 +417,7 @@ static void meta_cluster_init(struct metadump_struct *md, u64 start)
md->num_items = 0;
md->num_ready = 0;
header = &md->cluster.header;
- header->magic = cpu_to_le64(HEADER_MAGIC);
+ header->magic = cpu_to_le64(current_version->magic_cpu);
header->bytenr = cpu_to_le64(start);
header->nritems = cpu_to_le32(0);
header->compress = md->compress_level > 0 ?
@@ -716,7 +729,7 @@ static int add_extent(u64 start, u64 size, struct metadump_struct *md,
{
int ret;
if (md->data != data ||
- md->pending_size + size > MAX_PENDING_SIZE ||
+ md->pending_size + size > current_version->max_pending_size ||
md->pending_start + md->pending_size != start) {
ret = flush_pending(md, 0);
if (ret)
@@ -1045,7 +1058,8 @@ static void update_super_old(u8 *buffer)
u32 sectorsize = btrfs_super_sectorsize(super);
u64 flags = btrfs_super_flags(super);
- flags |= BTRFS_SUPER_FLAG_METADUMP;
+ if (current_version->extra_sb_flags)
+ flags |= BTRFS_SUPER_FLAG_METADUMP;
btrfs_set_super_flags(super, flags);
key = (struct btrfs_disk_key *)(super->sys_chunk_array);
@@ -1138,7 +1152,8 @@ static int update_super(struct mdrestore_struct *mdres, u8 *buffer)
if (mdres->clear_space_cache)
btrfs_set_super_cache_generation(super, 0);
- flags |= BTRFS_SUPER_FLAG_METADUMP_V2;
+ if (current_version->extra_sb_flags)
+ flags |= BTRFS_SUPER_FLAG_METADUMP_V2;
btrfs_set_super_flags(super, flags);
btrfs_set_super_sys_array_size(super, new_array_size);
btrfs_set_super_num_devices(super, 1);
@@ -1326,7 +1341,7 @@ static void *restore_worker(void *data)
u8 *outbuf;
int outfd;
int ret;
- int compress_size = MAX_PENDING_SIZE * 4;
+ int compress_size = current_version->max_pending_size * 4;
outfd = fileno(mdres->out);
buffer = malloc(compress_size);
@@ -1476,6 +1491,42 @@ static void mdrestore_destroy(struct mdrestore_struct *mdres, int num_threads)
pthread_mutex_destroy(&mdres->mutex);
}
+static int detect_version(FILE *in)
+{
+ struct meta_cluster *cluster;
+ u8 buf[BLOCK_SIZE];
+ bool found = false;
+ int i;
+ int ret;
+
+ if (fseek(in, 0, SEEK_SET) < 0) {
+ error("seek failed: %m");
+ return -errno;
+ }
+ ret = fread(buf, BLOCK_SIZE, 1, in);
+ if (!ret) {
+ error("failed to read header");
+ return -EIO;
+ }
+
+ fseek(in, 0, SEEK_SET);
+ cluster = (struct meta_cluster *)buf;
+ for (i = 0; i < NR_DUMP_VERSIONS; i++) {
+ if (le64_to_cpu(cluster->header.magic) ==
+ dump_versions[i].magic_cpu) {
+ found = true;
+ current_version = &dump_versions[i];
+ break;
+ }
+ }
+
+ if (!found) {
+ error("unrecognized header format");
+ return -EINVAL;
+ }
+ return 0;
+}
+
static int mdrestore_init(struct mdrestore_struct *mdres,
FILE *in, FILE *out, int old_restore,
int num_threads, int fixup_offset,
@@ -1483,6 +1534,9 @@ static int mdrestore_init(struct mdrestore_struct *mdres,
{
int i, ret = 0;
+ ret = detect_version(in);
+ if (ret < 0)
+ return ret;
memset(mdres, 0, sizeof(*mdres));
pthread_cond_init(&mdres->cond, NULL);
pthread_mutex_init(&mdres->mutex, NULL);
@@ -1832,7 +1886,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres)
u64 current_cluster = 0, bytenr;
u64 item_bytenr;
u32 bufsize, nritems, i;
- u32 max_size = MAX_PENDING_SIZE * 2;
+ u32 max_size = current_version->max_pending_size * 2;
u8 *buffer, *tmp = NULL;
int ret = 0;
@@ -1885,7 +1939,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres)
ret = 0;
header = &cluster->header;
- if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
+ if (le64_to_cpu(header->magic) != current_version->magic_cpu ||
le64_to_cpu(header->bytenr) != current_cluster) {
error("bad header in metadump image");
ret = -EIO;
@@ -2084,7 +2138,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
ret = 0;
header = &cluster->header;
- if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
+ if (le64_to_cpu(header->magic) != current_version->magic_cpu ||
le64_to_cpu(header->bytenr) != 0) {
error("bad header in metadump image");
return -EIO;
@@ -2592,7 +2646,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
break;
header = &cluster->header;
- if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
+ if (le64_to_cpu(header->magic) != current_version->magic_cpu ||
le64_to_cpu(header->bytenr) != bytenr) {
error("bad header in metadump image");
ret = -EIO;
diff --git a/image/metadump.h b/image/metadump.h
index f85c9bcfb813..941d4b827a24 100644
--- a/image/metadump.h
+++ b/image/metadump.h
@@ -22,8 +22,6 @@
#include "kernel-lib/list.h"
#include "ctree.h"
-#define HEADER_MAGIC 0xbd5c25e27295668bULL
-#define MAX_PENDING_SIZE SZ_256K
#define BLOCK_SIZE SZ_1K
#define BLOCK_MASK (BLOCK_SIZE - 1)
@@ -33,6 +31,17 @@
#define COMPRESS_NONE 0
#define COMPRESS_ZLIB 1
+struct dump_version {
+ u64 magic_cpu;
+ int version;
+ int max_pending_size;
+ unsigned int extra_sb_flags:1;
+};
+
+#define NR_DUMP_VERSIONS 1
+extern const struct dump_version dump_versions[NR_DUMP_VERSIONS];
+const extern struct dump_version *current_version;
+
struct meta_cluster_item {
__le64 bytenr;
__le32 size;
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v4 2/4] btrfs-progs: image: Introduce -d option to dump data
2020-03-18 7:54 [PATCH v4 0/4] btrfs-progs: image: Enhancement with new data dump feature Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 1/4] btrfs-progs: image: Introduce framework for more dump versions Qu Wenruo
@ 2020-03-18 7:54 ` Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 3/4] btrfs-progs: image: Reduce memory requirement for decompression Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 4/4] btrfs: image: Fix restored image size misalignment Qu Wenruo
3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2020-03-18 7:54 UTC (permalink / raw)
To: linux-btrfs
This new data dump feature will dump the whole image, not long the
existing tree blocks but also all its data extents(*).
This feature will rely on the new dump format (_DUmP_v1), as it needs
extra large extent size limit, and older btrfs-image dump can't handle
such large item/cluster size.
Since we're dumping all extents including data extents, for the restored
image there is no need to use any extra super block flags to inform
kernel.
Kernel should just treat the restored image as any ordinary btrfs.
*: The data extents will be dumped as is, that's to say, even for
preallocated extent, its (meaningless) data will be read out and
dumpped.
This behavior will cause extra space usage for the image, but we can
skip all the complex partially shared preallocated extent check.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
image/main.c | 53 +++++++++++++++++++++++++++++++++++++-----------
image/metadump.h | 2 +-
2 files changed, 42 insertions(+), 13 deletions(-)
diff --git a/image/main.c b/image/main.c
index 12faf5350566..3dadaf1a8617 100644
--- a/image/main.c
+++ b/image/main.c
@@ -52,7 +52,15 @@ const struct dump_version dump_versions[NR_DUMP_VERSIONS] = {
{ .version = 0,
.max_pending_size = SZ_256K,
.magic_cpu = 0xbd5c25e27295668bULL,
- .extra_sb_flags = 1 }
+ .extra_sb_flags = 1 },
+ /*
+ * The newer format, with much larger item size to contain
+ * any data extent.
+ */
+ { .version = 1,
+ .max_pending_size = SZ_256M,
+ .magic_cpu = 0x31765f506d55445fULL, /* ascii _DUmP_v1, no null */
+ .extra_sb_flags = 0 },
};
const struct dump_version *current_version = &dump_versions[0];
@@ -453,10 +461,14 @@ static void metadump_destroy(struct metadump_struct *md, int num_threads)
static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
FILE *out, int num_threads, int compress_level,
- enum sanitize_mode sanitize_names)
+ bool dump_data, enum sanitize_mode sanitize_names)
{
int i, ret = 0;
+ /* We need larger item/cluster limit for data extents */
+ if (dump_data)
+ current_version = &dump_versions[1];
+
memset(md, 0, sizeof(*md));
INIT_LIST_HEAD(&md->list);
INIT_LIST_HEAD(&md->ordered);
@@ -884,7 +896,7 @@ static int copy_space_cache(struct btrfs_root *root,
}
static int copy_from_extent_tree(struct metadump_struct *metadump,
- struct btrfs_path *path)
+ struct btrfs_path *path, bool dump_data)
{
struct btrfs_root *extent_root;
struct extent_buffer *leaf;
@@ -949,9 +961,15 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
ei = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item);
if (btrfs_extent_flags(leaf, ei) &
- BTRFS_EXTENT_FLAG_TREE_BLOCK) {
+ BTRFS_EXTENT_FLAG_TREE_BLOCK ||
+ (dump_data && (btrfs_extent_flags(leaf, ei) &
+ BTRFS_EXTENT_FLAG_DATA))) {
+ bool is_data;
+
+ is_data = btrfs_extent_flags(leaf, ei) &
+ BTRFS_EXTENT_FLAG_DATA;
ret = add_extent(bytenr, num_bytes, metadump,
- 0);
+ is_data);
if (ret) {
error("unable to add block %llu: %d",
(unsigned long long)bytenr, ret);
@@ -974,7 +992,7 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
static int create_metadump(const char *input, FILE *out, int num_threads,
int compress_level, enum sanitize_mode sanitize,
- int walk_trees)
+ int walk_trees, bool dump_data)
{
struct btrfs_root *root;
struct btrfs_path path;
@@ -989,7 +1007,7 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
}
ret = metadump_init(&metadump, root, out, num_threads,
- compress_level, sanitize);
+ compress_level, dump_data, sanitize);
if (ret) {
error("failed to initialize metadump: %d", ret);
close_ctree(root);
@@ -1021,7 +1039,7 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
goto out;
}
} else {
- ret = copy_from_extent_tree(&metadump, &path);
+ ret = copy_from_extent_tree(&metadump, &path, dump_data);
if (ret) {
err = ret;
goto out;
@@ -2808,6 +2826,7 @@ static void print_usage(int ret)
printf("\t-s \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
printf("\t-w \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
printf("\t-m \trestore for multiple devices\n");
+ printf("\t-d \talso dump data, conflicts with -w\n");
printf("\n");
printf("\tIn the dump mode, source is the btrfs device and target is the output file (use '-' for stdout).\n");
printf("\tIn the restore mode, source is the dumped image and target is the btrfs device/file.\n");
@@ -2827,6 +2846,7 @@ int BOX_MAIN(image)(int argc, char *argv[])
int ret;
enum sanitize_mode sanitize = SANITIZE_NONE;
int dev_cnt = 0;
+ bool dump_data = false;
int usage_error = 0;
FILE *out;
@@ -2835,7 +2855,7 @@ int BOX_MAIN(image)(int argc, char *argv[])
{ "help", no_argument, NULL, GETOPT_VAL_HELP},
{ NULL, 0, NULL, 0 }
};
- int c = getopt_long(argc, argv, "rc:t:oswm", long_options, NULL);
+ int c = getopt_long(argc, argv, "rc:t:oswmd", long_options, NULL);
if (c < 0)
break;
switch (c) {
@@ -2875,6 +2895,9 @@ int BOX_MAIN(image)(int argc, char *argv[])
create = 0;
multi_devices = 1;
break;
+ case 'd':
+ dump_data = true;
+ break;
case GETOPT_VAL_HELP:
default:
print_usage(c != GETOPT_VAL_HELP);
@@ -2893,10 +2916,15 @@ int BOX_MAIN(image)(int argc, char *argv[])
"create and restore cannot be used at the same time");
usage_error++;
}
+ if (dump_data && walk_trees) {
+ error("-d conflicts with -w option");
+ usage_error++;
+ }
} else {
- if (walk_trees || sanitize != SANITIZE_NONE || compress_level) {
+ if (walk_trees || sanitize != SANITIZE_NONE || compress_level ||
+ dump_data) {
error(
- "using -w, -s, -c options for restore makes no sense");
+ "using -w, -s, -c, -d options for restore makes no sense");
usage_error++;
}
if (multi_devices && dev_cnt < 2) {
@@ -2949,7 +2977,8 @@ int BOX_MAIN(image)(int argc, char *argv[])
}
ret = create_metadump(source, out, num_threads,
- compress_level, sanitize, walk_trees);
+ compress_level, sanitize, walk_trees,
+ dump_data);
} else {
ret = restore_metadump(source, out, old_restore, num_threads,
0, target, multi_devices);
diff --git a/image/metadump.h b/image/metadump.h
index 941d4b827a24..a04f63a910d6 100644
--- a/image/metadump.h
+++ b/image/metadump.h
@@ -38,7 +38,7 @@ struct dump_version {
unsigned int extra_sb_flags:1;
};
-#define NR_DUMP_VERSIONS 1
+#define NR_DUMP_VERSIONS 2
extern const struct dump_version dump_versions[NR_DUMP_VERSIONS];
const extern struct dump_version *current_version;
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v4 3/4] btrfs-progs: image: Reduce memory requirement for decompression
2020-03-18 7:54 [PATCH v4 0/4] btrfs-progs: image: Enhancement with new data dump feature Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 1/4] btrfs-progs: image: Introduce framework for more dump versions Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 2/4] btrfs-progs: image: Introduce -d option to dump data Qu Wenruo
@ 2020-03-18 7:54 ` Qu Wenruo
2020-03-18 7:54 ` [PATCH v4 4/4] btrfs: image: Fix restored image size misalignment Qu Wenruo
3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2020-03-18 7:54 UTC (permalink / raw)
To: linux-btrfs
With recent change to enlarge max_pending_size to 256M for data dump,
the decompress code requires quite a lot of memory space. (256M * 4).
The main reason behind it is, we're using wrapped uncompress() function
call, which needs the buffer to be large enough to contain the
decompressed data.
This patch will re-work the decompress work to use inflate() which can
resume it decompression so that we can use a much smaller buffer size.
This patch choose to use 512K buffer size.
Now the memory consumption for restore is reduced to
Cluster data size + 512K * nr_running_threads
Instead of the original one:
Cluster data size + 1G * nr_running_threads
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
image/main.c | 220 +++++++++++++++++++++++++++++++++------------------
1 file changed, 145 insertions(+), 75 deletions(-)
diff --git a/image/main.c b/image/main.c
index 3dadaf1a8617..47d30584e0f1 100644
--- a/image/main.c
+++ b/image/main.c
@@ -1350,128 +1350,198 @@ static void write_backup_supers(int fd, u8 *buf)
}
}
-static void *restore_worker(void *data)
+/*
+ * Restore one item.
+ *
+ * For uncompressed data, it's just reading from work->buf then write to output.
+ * For compressed data, since we can have very large decompressed data
+ * (up to 256M), we need to consider memory usage. So here we will fill buffer
+ * then write the decompressed buffer to output.
+ */
+static int restore_one_work(struct mdrestore_struct *mdres,
+ struct async_work *async, u8 *buffer, int bufsize)
{
- struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
- struct async_work *async;
- size_t size;
- u8 *buffer;
- u8 *outbuf;
- int outfd;
+ z_stream strm;
+ int buf_offset = 0; /* offset inside work->buffer */
+ int out_offset = 0; /* offset for output */
+ int out_len;
+ int outfd = fileno(mdres->out);
+ int compress_method = mdres->compress_method;
int ret;
- int compress_size = current_version->max_pending_size * 4;
- outfd = fileno(mdres->out);
- buffer = malloc(compress_size);
- if (!buffer) {
- error("not enough memory for restore worker buffer");
- pthread_mutex_lock(&mdres->mutex);
- if (!mdres->error)
- mdres->error = -ENOMEM;
- pthread_mutex_unlock(&mdres->mutex);
- pthread_exit(NULL);
+ ASSERT(is_power_of_2(bufsize));
+
+ if (compress_method == COMPRESS_ZLIB) {
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = async->bufsize;
+ strm.next_in = async->buffer;
+ strm.avail_out = 0;
+ strm.next_out = Z_NULL;
+ ret = inflateInit(&strm);
+ if (ret != Z_OK) {
+ error("failed to initialize decompress parameters: %d",
+ ret);
+ return ret;
+ }
}
+ while (buf_offset < async->bufsize) {
+ bool compress_end = false;
+ int read_size = min_t(u64, async->bufsize - buf_offset,
+ bufsize);
- while (1) {
- u64 bytenr, physical_dup;
- off_t offset = 0;
- int err = 0;
-
- pthread_mutex_lock(&mdres->mutex);
- while (!mdres->nodesize || list_empty(&mdres->list)) {
- if (mdres->done) {
- pthread_mutex_unlock(&mdres->mutex);
- goto out;
+ /* Read part */
+ if (compress_method == COMPRESS_ZLIB) {
+ if (strm.avail_out == 0) {
+ strm.avail_out = bufsize;
+ strm.next_out = buffer;
}
- pthread_cond_wait(&mdres->cond, &mdres->mutex);
- }
- async = list_entry(mdres->list.next, struct async_work, list);
- list_del_init(&async->list);
-
- if (mdres->compress_method == COMPRESS_ZLIB) {
- size = compress_size;
pthread_mutex_unlock(&mdres->mutex);
- ret = uncompress(buffer, (unsigned long *)&size,
- async->buffer, async->bufsize);
+ ret = inflate(&strm, Z_NO_FLUSH);
pthread_mutex_lock(&mdres->mutex);
- if (ret != Z_OK) {
- error("decompression failed with %d", ret);
- err = -EIO;
+ switch (ret) {
+ case Z_NEED_DICT:
+ ret = Z_DATA_ERROR; /* fallthrough */
+ __attribute__ ((fallthrough));
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ goto out;
+ }
+ if (ret == Z_STREAM_END) {
+ ret = 0;
+ compress_end = true;
}
- outbuf = buffer;
+ out_len = bufsize - strm.avail_out;
} else {
- outbuf = async->buffer;
- size = async->bufsize;
+ /* No compress, read as many data as possible */
+ memcpy(buffer, async->buffer + buf_offset, read_size);
+
+ buf_offset += read_size;
+ out_len = read_size;
}
+ /* Fixup part */
if (!mdres->multi_devices) {
if (async->start == BTRFS_SUPER_INFO_OFFSET) {
if (mdres->old_restore) {
- update_super_old(outbuf);
+ update_super_old(buffer);
} else {
- ret = update_super(mdres, outbuf);
- if (ret)
- err = ret;
+ ret = update_super(mdres, buffer);
+ if (ret < 0)
+ goto out;
}
} else if (!mdres->old_restore) {
- ret = fixup_chunk_tree_block(mdres, async, outbuf, size);
+ ret = fixup_chunk_tree_block(mdres, async,
+ buffer, out_len);
if (ret)
- err = ret;
+ goto out;
}
}
+ /* Write part */
if (!mdres->fixup_offset) {
+ int size = out_len;
+ off_t offset = 0;
+
while (size) {
+ u64 logical = async->start + out_offset + offset;
u64 chunk_size = size;
- physical_dup = 0;
+ u64 physical_dup = 0;
+ u64 bytenr;
+
if (!mdres->multi_devices && !mdres->old_restore)
bytenr = logical_to_physical(mdres,
- async->start + offset,
- &chunk_size,
- &physical_dup);
+ logical, &chunk_size,
+ &physical_dup);
else
- bytenr = async->start + offset;
+ bytenr = logical;
- ret = pwrite64(outfd, outbuf+offset, chunk_size,
- bytenr);
+ ret = pwrite64(outfd, buffer + offset, chunk_size, bytenr);
if (ret != chunk_size)
- goto error;
+ goto write_error;
if (physical_dup)
- ret = pwrite64(outfd, outbuf+offset,
- chunk_size,
- physical_dup);
+ ret = pwrite64(outfd, buffer + offset,
+ chunk_size, physical_dup);
if (ret != chunk_size)
- goto error;
+ goto write_error;
size -= chunk_size;
offset += chunk_size;
continue;
-
-error:
- if (ret < 0) {
- error("unable to write to device: %m");
- err = errno;
- } else {
- error("short write");
- err = -EIO;
- }
}
} else if (async->start != BTRFS_SUPER_INFO_OFFSET) {
- ret = write_data_to_disk(mdres->info, outbuf, async->start, size, 0);
+ ret = write_data_to_disk(mdres->info, buffer,
+ async->start, out_len, 0);
if (ret) {
error("failed to write data");
exit(1);
}
}
-
/* backup super blocks are already there at fixup_offset stage */
- if (!mdres->multi_devices && async->start == BTRFS_SUPER_INFO_OFFSET)
- write_backup_supers(outfd, outbuf);
+ if (async->start == BTRFS_SUPER_INFO_OFFSET &&
+ !mdres->multi_devices)
+ write_backup_supers(outfd, buffer);
+ out_offset += out_len;
+ if (compress_end) {
+ inflateEnd(&strm);
+ break;
+ }
+ }
+ return ret;
+
+write_error:
+ if (ret < 0) {
+ error("unable to write to device: %m");
+ ret = -errno;
+ } else {
+ error("short write");
+ ret = -EIO;
+ }
+out:
+ if (compress_method == COMPRESS_ZLIB)
+ inflateEnd(&strm);
+ return ret;
+}
+
+static void *restore_worker(void *data)
+{
+ struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
+ struct async_work *async;
+ u8 *buffer;
+ int ret;
+ int buffer_size = SZ_512K;
+
+ buffer = malloc(buffer_size);
+ if (!buffer) {
+ error("not enough memory for restore worker buffer");
+ pthread_mutex_lock(&mdres->mutex);
+ if (!mdres->error)
+ mdres->error = -ENOMEM;
+ pthread_mutex_unlock(&mdres->mutex);
+ pthread_exit(NULL);
+ }
+
+ while (1) {
+ pthread_mutex_lock(&mdres->mutex);
+ while (!mdres->nodesize || list_empty(&mdres->list)) {
+ if (mdres->done) {
+ pthread_mutex_unlock(&mdres->mutex);
+ goto out;
+ }
+ pthread_cond_wait(&mdres->cond, &mdres->mutex);
+ }
+ async = list_entry(mdres->list.next, struct async_work, list);
+ list_del_init(&async->list);
- if (err && !mdres->error)
- mdres->error = err;
+ ret = restore_one_work(mdres, async, buffer, buffer_size);
+ if (ret < 0) {
+ mdres->error = ret;
+ pthread_mutex_unlock(&mdres->mutex);
+ goto out;
+ }
mdres->num_items--;
pthread_mutex_unlock(&mdres->mutex);
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v4 4/4] btrfs: image: Fix restored image size misalignment
2020-03-18 7:54 [PATCH v4 0/4] btrfs-progs: image: Enhancement with new data dump feature Qu Wenruo
` (2 preceding siblings ...)
2020-03-18 7:54 ` [PATCH v4 3/4] btrfs-progs: image: Reduce memory requirement for decompression Qu Wenruo
@ 2020-03-18 7:54 ` Qu Wenruo
3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2020-03-18 7:54 UTC (permalink / raw)
To: linux-btrfs
[BUG]
There is a small device size misalignment between the super block device
size and the device extent size:
total_bytes 10737418240 <<<
bytes_used 15097856
dev_item.total_bytes 10737418240
dev_item.bytes_used 1094713344
item 0 key (DEV_ITEMS DEV_ITEM 1) itemoff 16185 itemsize 98
devid 1 total_bytes 1095761920 bytes_used 1094713344
^^^^^^^^^^
[CAUSE]
In fixup_device_size(), we only reset superblock device item size, which
will be overwritten in write_dev_supers() using
btrfs_device::total_bytes.
And it doesn't touch btrfs_superblock::total_bytes either.
[FIX]
So fix the small mismatch by also resetting btrfs_device::total_bytes,
btrfs_device::bytes_used and btrfs_superblock::total_bytes.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
image/main.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/image/main.c b/image/main.c
index 47d30584e0f1..04e347afe11d 100644
--- a/image/main.c
+++ b/image/main.c
@@ -2357,6 +2357,7 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_dev_item *dev_item;
struct btrfs_dev_extent *dev_ext;
+ struct btrfs_device *dev;
struct btrfs_path path;
struct extent_buffer *leaf;
struct btrfs_root *root = fs_info->chunk_root;
@@ -2375,6 +2376,8 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,
key.type = BTRFS_DEV_EXTENT_KEY;
key.offset = (u64)-1;
+ dev = list_first_entry(&fs_info->fs_devices->devices,
+ struct btrfs_device, dev_list);
ret = btrfs_search_slot(NULL, fs_info->dev_root, &key, &path, 0, 0);
if (ret < 0) {
errno = -ret;
@@ -2408,6 +2411,9 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,
btrfs_set_stack_device_total_bytes(dev_item, dev_size);
btrfs_set_stack_device_bytes_used(dev_item, mdres->alloced_chunks);
+ dev->total_bytes = dev_size;
+ dev->bytes_used = mdres->alloced_chunks;
+ btrfs_set_super_total_bytes(fs_info->super_copy, dev_size);
ret = fstat(out_fd, &buf);
if (ret < 0) {
error("failed to stat result image: %m");
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread