summaryrefslogtreecommitdiff
path: root/c_src
diff options
context:
space:
mode:
Diffstat (limited to 'c_src')
-rw-r--r--c_src/cmd_image.c6
-rw-r--r--c_src/cmd_strip_alloc.c3
-rw-r--r--c_src/posix_to_bcachefs.c104
-rw-r--r--c_src/posix_to_bcachefs.h1
4 files changed, 55 insertions, 59 deletions
diff --git a/c_src/cmd_image.c b/c_src/cmd_image.c
index 58268b4b..d00d85cf 100644
--- a/c_src/cmd_image.c
+++ b/c_src/cmd_image.c
@@ -477,7 +477,7 @@ static void image_create(struct bch_opt_strs fs_opt_strs,
if (ret)
goto err;
- struct copy_fs_state s = {};
+ struct copy_fs_state s = { .verbosity = verbosity };
ret = copy_fs(c, &s, src_fd, src_path) ?:
finish_image(c, keep_alloc, verbosity);
if (ret)
@@ -635,7 +635,7 @@ static int image_update(const char *src_path, const char *dst_image,
u64 input_bytes = count_input_size(src_fd);
- if (truncate(dst_image, input_bytes * 2))
+ if (truncate(dst_image, xstat(dst_image).st_size + input_bytes * 2))
die("truncate error: %m");
darray_const_str device_paths = {};
@@ -705,7 +705,7 @@ static int image_update(const char *src_path, const char *dst_image,
goto err_stop;
bch_verbose(c, "Syncing data");
- struct copy_fs_state s = {};
+ struct copy_fs_state s = { .verbosity = verbosity };
ret = copy_fs(c, &s, src_fd, src_path) ?:
finish_image(c, keep_alloc, verbosity);
diff --git a/c_src/cmd_strip_alloc.c b/c_src/cmd_strip_alloc.c
index c313b665..e16eb093 100644
--- a/c_src/cmd_strip_alloc.c
+++ b/c_src/cmd_strip_alloc.c
@@ -104,8 +104,9 @@ int cmd_strip_alloc(int argc, char *argv[])
struct bch_opts opts = bch2_opts_empty();
opt_set(opts, nostart, true);
+ struct bch_fs *c;
reopen:
- struct bch_fs *c = bch2_fs_open(&devs, &opts);
+ c = bch2_fs_open(&devs, &opts);
int ret = PTR_ERR_OR_ZERO(c);
if (ret)
die("Error opening filesystem: %s", bch2_err_str(ret));
diff --git a/c_src/posix_to_bcachefs.c b/c_src/posix_to_bcachefs.c
index 8cb1c7c8..ca44db32 100644
--- a/c_src/posix_to_bcachefs.c
+++ b/c_src/posix_to_bcachefs.c
@@ -282,7 +282,7 @@ static void write_data(struct bch_fs *c,
closure_call(&op.cl, bch2_write, NULL, NULL);
BUG_ON(!(op.flags & BCH_WRITE_submitted));
- dst_inode->bi_sectors += len >> 9;
+ dst_inode->bi_sectors += op.i_sectors_delta;
if (op.error)
die("write error: %s", bch2_err_str(op.error));
@@ -371,6 +371,8 @@ static void copy_link(struct bch_fs *c,
if (ret)
die("bch2_fpunch error: %s", bch2_err_str(ret));
+ dst->bi_sectors += i_sectors_delta;
+
ret = readlink(src, src_buf, sizeof(src_buf));
if (ret < 0)
die("readlink error: %m");
@@ -437,40 +439,38 @@ static void link_file_data(struct bch_fs *c,
fiemap_iter_exit(&iter);
}
-static struct range seek_data_aligned(int fd, u64 i_size, loff_t o, unsigned bs)
+static struct range align_range(struct range r, unsigned bs)
{
- struct range seek_data(int fd, loff_t o)
- {
- s64 s = lseek(fd, o, SEEK_DATA);
- if (s < 0 && errno == ENXIO)
- return (struct range) {};
- if (s < 0)
- die("lseek error: %m");
-
- s64 e = lseek(fd, s, SEEK_HOLE);
- if (e < 0 && errno == ENXIO)
- e = i_size;
- if (e < 0)
- die("lseek error: %m");
-
- return (struct range) { s, e };
- }
-
- struct range __seek_data_aligned(int fd, loff_t o, unsigned bs)
- {
- struct range r = seek_data(fd, o);
+ r.start = round_down(r.start, bs);
+ r.end = round_up(r.end, bs);
+ return r;
+}
- r.start = round_down(r.start, bs);
- r.end = round_up(r.end, bs);
- return r;
- }
+struct range seek_data(int fd, u64 i_size, loff_t o)
+{
+ s64 s = lseek(fd, o, SEEK_DATA);
+ if (s < 0 && errno == ENXIO)
+ return (struct range) {};
+ if (s < 0)
+ die("lseek error: %m");
+
+ s64 e = lseek(fd, s, SEEK_HOLE);
+ if (e < 0 && errno == ENXIO)
+ e = i_size;
+ if (e < 0)
+ die("lseek error: %m");
+
+ return (struct range) { s, e };
+}
- struct range r = __seek_data_aligned(fd, o, bs);
+static struct range seek_data_aligned(int fd, u64 i_size, loff_t o, unsigned bs)
+{
+ struct range r = align_range(seek_data(fd, i_size, o), bs);
if (!r.end)
return r;
while (true) {
- struct range n = __seek_data_aligned(fd, r.end, bs);
+ struct range n = align_range(seek_data(fd, i_size, r.end), bs);
if (!n.end || r.end < n.start)
break;
@@ -480,38 +480,30 @@ static struct range seek_data_aligned(int fd, u64 i_size, loff_t o, unsigned bs)
return r;
}
-static struct range seek_mismatch_aligned(const char *buf1, const char *buf2,
- unsigned offset, unsigned len,
- unsigned bs)
+struct range seek_mismatch(const char *buf1, const char *buf2,
+ unsigned o, unsigned len)
{
- struct range seek_mismatch(unsigned o)
- {
- while (o < len && buf1[o] == buf2[o])
- o++;
-
- if (o == len)
- return (struct range) {};
+ while (o < len && buf1[o] == buf2[o])
+ o++;
- unsigned s = o;
- while (o < len && buf1[o] != buf2[o])
- o++;
+ if (o == len)
+ return (struct range) {};
- return (struct range) { s, o };
- }
-
- struct range __seek_mismatch_aligned(unsigned o)
- {
- struct range r = seek_mismatch(o);
+ unsigned s = o;
+ while (o < len && buf1[o] != buf2[o])
+ o++;
- r.start = round_down(r.start, bs);
- r.end = round_up(r.end, bs);
- return r;
- }
+ return (struct range) { s, o };
+}
- struct range r = __seek_mismatch_aligned(offset);
+static struct range seek_mismatch_aligned(const char *buf1, const char *buf2,
+ unsigned offset, unsigned len,
+ unsigned bs)
+{
+ struct range r = align_range(seek_mismatch(buf1, buf2, offset, len), bs);
if (r.end)
while (true) {
- struct range n = __seek_mismatch_aligned(r.end);
+ struct range n = align_range(seek_mismatch(buf1, buf2, r.end, len), bs);
if (!n.end || r.end < n.start)
break;
@@ -669,6 +661,7 @@ static int recursive_remove(struct bch_fs *c,
}
static int delete_non_matching_dirents(struct bch_fs *c,
+ struct copy_fs_state *s,
subvol_inum dst_dir_inum,
struct bch_inode_unpacked *dst_dir,
dirents src_dirents)
@@ -692,7 +685,8 @@ static int delete_non_matching_dirents(struct bch_fs *c,
!strcmp(dst_d->d_name, "lost+found"))
continue;
- printf("deleting %s type %u\n", dst_d->d_name, dst_d->d_type);
+ if (s->verbosity > 1)
+ printf("deleting %s\n", dst_d->d_name);
ret = recursive_remove(c, dst_dir_inum, dst_dir, dst_d);
if (ret)
@@ -724,7 +718,7 @@ static int copy_dir(struct bch_fs *c,
sort(dirents.data, dirents.nr, sizeof(dirents.data[0]), dirent_cmp, NULL);
subvol_inum dir_inum = { 1, dst->bi_inum };
- int ret = delete_non_matching_dirents(c, dir_inum, dst, dirents);
+ int ret = delete_non_matching_dirents(c, s, dir_inum, dst, dirents);
if (ret)
goto err;
diff --git a/c_src/posix_to_bcachefs.h b/c_src/posix_to_bcachefs.h
index 542ae171..3fc586d8 100644
--- a/c_src/posix_to_bcachefs.h
+++ b/c_src/posix_to_bcachefs.h
@@ -36,6 +36,7 @@ struct copy_fs_state {
GENRADIX(u64) hardlinks;
ranges extents;
enum bch_migrate_type type;
+ unsigned verbosity;
u64 reserve_start;