btrfs-progs: convert: properly handle reserved ranges while iterating files
Subsystems (2)
PATCH 1/3 btrfs-progs: convert: properly handle reserved ranges while iterating files
From: Jeff Mahoney <[email protected]>
Commit 522ef705e38 (btrfs-progs: convert: Introduce function to calculate the available space) changed how we handle migrating file data so that we never have btrfs space associated with the reserved ranges. This works pretty well and when we iterate over the file blocks, the associations are redirected to the migrated locations.
This commit missed the case in block_iterate_proc where we just check for intersection with a superblock location before looking up a block group. intersect_with_sb checks to see if the range intersects with a stripe containing a superblock but, in fact, we've reserved the full 0-1MB range at the start of the disk. So a file block located at e.g. 160kB will fall in the reserved region but won't be excepted in block_iterate_block. We ultimately hit a BUG_ON when we fail to look up the block group for that location.
This is reproducible using convert-tests/003-ext4-basic.
The fix is to have intersect_with_sb and block_iterate_proc understand the full size of the reserved ranges. Since we use the range to determine the boundary for the block iterator, let's just return the boundary. 0 isn't a valid boundary and means that we proceed normally with block group lookup.
--- convert/source-fs.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
convert/source-fs.c +11 -14
@@ -28,18 +28,16 @@ const struct simple_range btrfs_reserved_ranges[3] = { { BTRFS_SB_MIRROR_OFFSET(2), SZ_64K } }; -static int intersect_with_sb (u64 bytenr,u64 num_bytes) +static u64 intersect_with_reserved (u64 bytenr,u64 num_bytes) {int i; -u64 offset; -for (i =0 ; i <BTRFS_SUPER_MIRROR_MAX ; i++) { - offset =btrfs_sb_offset (i); - offset &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +for (i =0 ; i <ARRAY_SIZE (btrfs_reserved_ranges); i++) { +const struct simple_range *range = &btrfs_reserved_ranges[i]; -if (bytenr < offset +BTRFS_STRIPE_LEN && - bytenr + num_bytes > offset) -return 1 ; +if (bytenr <range_end (range) && + bytenr + num_bytes >= range->start) +return range_end (range); }return 0 ; }
@@ -64,14 +62,14 @@ int block_iterate_proc(u64 disk_block, u64 file_block, struct blk_iterate_data *idata) {int ret =0 ; -int sb_region; +u64 reserved_boundary;int do_barrier;struct btrfs_root *root = idata->root;struct btrfs_block_group_cache *cache;u64 bytenr = disk_block * root->sectorsize; - sb_region =intersect_with_sb (bytenr, root->sectorsize); - do_barrier = sb_region || disk_block >= idata->boundary; + reserved_boundary =intersect_with_reserved (bytenr, root->sectorsize); + do_barrier = reserved_boundary || disk_block >= idata->boundary;if ((idata->num_blocks >0 && do_barrier) || (file_block > idata->first_block + idata->num_blocks) || (disk_block != idata->disk_block + idata->num_blocks)) {
@@ -91,9 +89,8 @@ int block_iterate_proc(u64 disk_block, u64 file_block, goto fail; } -if (sb_region) { - bytenr +=BTRFS_STRIPE_LEN -1 ; - bytenr &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +if (reserved_boundary) { + bytenr = reserved_boundary; }else { cache =btrfs_lookup_block_group (root->fs_info, bytenr);BUG_ON (!cache);
Signature · 1 line
2.11.0
PATCH 2/3 btrfs-progs: convert: add missing newlines for printfs
From: Jeff Mahoney <[email protected]>
There are two printfs with missing newlines that end up making the output wonky.
--- convert/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
convert/main.c +2 -2
@@ -1285,7 +1285,7 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize, goto fail; } -printf ("creating btrfs metadata" ); +printf ("creating btrfs metadata\n" ); ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count); ctx.cur_copy_inodes =0 ;
@@ -1348,7 +1348,7 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,close_ctree (root);close (fd); -printf ("conversion complete" ); +printf ("conversion complete\n" );return 0 ; fail:clean_convert_context (&cctx);
Signature · 1 line
2.11.0
PATCH 3/3 btrfs-progs: convert: add support for converting reiserfs
In reply to [email protected]From: Jeff Mahoney <[email protected]>
This patch adds support to convert reiserfs file systems in-place to btrfs.
It will convert extended attribute files to btrfs extended attributes, translate ACLs, coalesce tails that consist of multiple items into one item, and convert tails that are too big into indirect files.
This requires that libreiserfscore 3.6.27 be available.
Many of the test cases for convert apply regardless of what the source file system is and using ext4 is sufficient. I've included several test cases that are reiserfs-specific.
--- Makefile | 3 +- Makefile.inc.in | 3 +- configure.ac | 10 +- convert/main.c | 13 +- convert/source-reiserfs.c | 1011 ++++++++++++++++++++ convert/source-reiserfs.h | 105 ++ tests/common.convert | 14 +- tests/convert-tests/010-reiserfs-basic/test.sh | 16 + .../011-reiserfs-delete-all-rollback/test.sh | 67 ++ .../012-reiserfs-large-hole-extent/test.sh | 23 + .../013-reiserfs-common-inode-flags/test.sh | 35 + 11 files changed, 1290 insertions(+), 10 deletions(-) create mode 100644 convert/source-reiserfs.c create mode 100644 convert/source-reiserfs.h create mode 100755 tests/convert-tests/010-reiserfs-basic/test.sh create mode 100755 tests/convert-tests/011-reiserfs-delete-all-rollback/test.sh create mode 100755 tests/convert-tests/012-reiserfs-large-hole-extent/test.sh create mode 100755 tests/convert-tests/013-reiserfs-common-inode-flags/test.sh
Makefile +2 -1
@@ -111,7 +111,7 @@ libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-l kernel-lib/radix-tree.h kernel-lib/sizes.h extent-cache.h \ extent_io.h ioctl.h ctree.h btrfsck.h version.h convert_objects = convert/main.o convert/common.o convert/source-fs.o \ - convert/source-ext2.o + convert/source-ext2.o convert/source-reiserfs.o mkfs_objects = mkfs/main.o mkfs/common.o TESTS = fsck-tests.sh convert-tests.sh
@@ -188,6 +188,7 @@ endif # external libs required by various binaries; for btrfs-foo, # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2) +btrfs_convert_cflags += -DBTRFSCONVERT_REISERFS=$(BTRFSCONVERT_REISERFS) btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype btrfs_debug_tree_objects = cmds-inspect-dump-tree.o btrfs_show_super_objects = cmds-inspect-dump-super.o
Makefile.inc.in +2 -1
@@ -12,6 +12,7 @@ INSTALL = @INSTALL@ DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@ DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@ BTRFSCONVERT_EXT2 = @BTRFSCONVERT_EXT2@ +BTRFSCONVERT_REISERFS = @BTRFSCONVERT_REISERFS@ SUBST_CFLAGS = @CFLAGS@ SUBST_LDFLAGS = @LDFLAGS@
@@ -31,6 +32,6 @@ udevruledir = ${udevdir}/rules.d # external libs required by various binaries; for btrfs-foo, # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules in Makefile -btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@ +btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@ @REISERFS_LIBS@ MAKEFILE_INC_INCLUDED = yes
configure.ac +9 -1
@@ -120,6 +120,7 @@ fi convertfs= BTRFSCONVERT_EXT2=0 +BTRFSCONVERT_REISERFS=0 if test "x$enable_convert" = xyes; then if test "x$with_convert" = "xauto" || echo "$with_convert" | grep -q "ext2"; then PKG_CHECK_MODULES(EXT2FS, [ext2fs >= 1.42],,
@@ -131,11 +132,18 @@ if test "x$enable_convert" = xyes; then convertfs="${convertfs:+$convertfs,}ext2" BTRFSCONVERT_EXT2=1 fi + if test "x$with_convert" = "xauto" || echo "$with_convert" | grep -q "reiserfs"; then + PKG_CHECK_MODULES(REISERFS, [reiserfscore >= 3.6.27], + [BTRFSCONVERT_REISERFS=1],[]) + convertfs="${convertfs:+$convertfs,}reiserfs" + BTRFSCONVERT_REISERFS=1 + fi fi AC_SUBST([BTRFSCONVERT_EXT2]) +AC_SUBST([BTRFSCONVERT_REISERFS]) # catch typos -tmp=$(echo "$with_convert" | sed -e 's/auto//' | sed -e 's/ext2//' | sed -e 's/,\+/,/') +tmp=$(echo "$with_convert" | sed -e 's/auto//' | sed -e 's/ext2//' | sed -e 's/reiserfs//' | sed -e 's/,\+/,/') if ! test "x$tmp" = "x"; then AC_MSG_ERROR([unknown tokens for --with-convert: $tmp]) fi
convert/main.c +9 -4
@@ -102,12 +102,16 @@#include "convert/source-fs.h" #include "fsfeatures.h" -const struct btrfs_convert_operations ext2_convert_ops; +extern const struct btrfs_convert_operations ext2_convert_ops; +extern const struct btrfs_convert_operations reiserfs_convert_ops;static const struct btrfs_convert_operations *convert_operations[] = { #if BTRFSCONVERT_EXT2 &ext2_convert_ops, #endif +#if BTRFSCONVERT_REISERFS + &reiserfs_convert_ops, +#endif };static void *print_copied_inodes (void *p)
@@ -411,7 +415,7 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans, }/* -* Relocate the used ext2 data in reserved ranges +* Relocate the used source fs data in reserved ranges */ static int migrate_reserved_ranges(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -1655,11 +1659,11 @@ static int do_rollback(const char *devname) ret =btrfs_search_slot (NULL , fs_info->tree_root, &key, &path,0 ,0 );btrfs_release_path (&path);if (ret >0 ) { -error ("unable to find ext2 image subvolume, is it deleted?" ); +error ("unable to find source fs image subvolume, is it deleted?" ); ret = -ENOENT ; goto close_fs; }else if (ret <0 ) { -error ("failed to find ext2 image subvolume: %s" , +error ("failed to find source fs image subvolume: %s" ,strerror (-ret)); goto close_fs; }
@@ -1777,6 +1781,7 @@ static void print_usage(void)printf ("\n" );printf ("Supported filesystems:\n" );printf ("\text2/3/4: %s\n" ,BTRFSCONVERT_EXT2 ?"yes" :"no" ); +printf ("\treiserfs: %s\n" ,BTRFSCONVERT_REISERFS ?"yes" :"no" ); }int main (int argc,char *argv[])
convert/source-reiserfs.c new file +1011 -0
1,011 lines not on this page · raw message
convert/source-reiserfs.h new file +105 -0
105 lines not on this page · raw message
tests/common.convert +11 -3
@@ -17,7 +17,7 @@ convert_test_preamble() { msg="$2" shift 3 echo " [TEST/conv] $msg, btrfs" "${features:-defaults}" - echo "creating ext image with: $@" >> "$RESULTS" + echo "creating test image with: $@" >> "$RESULTS" } # prepare TEST_DEV before conversion, create filesystem and mount it, image
@@ -29,10 +29,18 @@ convert_test_prep_fs() { run_check truncate -s 0 "$TEST_DEV" # 256MB is the smallest acceptable btrfs image. run_check truncate -s 512M "$TEST_DEV" - run_check "$@" -F "$TEST_DEV" + force= + mountopts= + case "$@" in + *mke2fs*) + force=-F ;; + *mkreiserfs*) + force=-ff; mountopts="-o acl,user_xattr,attrs" ;; + esac + run_check "$@" $force "$TEST_DEV" # create a file to check btrfs-convert can convert regular file correct - run_check_mount_test_dev + run_check_mount_test_dev $mountopts # create a file inside the fs before convert, to make sure there is # data covering btrfs backup superblock range (64M)
tests/convert-tests/010-reiserfs-basic/test.sh new file +16 -0
@@ -0,0 +1,16 @@ +#!/bin/bash + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +for feature in '' 'extref' 'skinny-metadata' 'no-holes'; do + convert_test "$feature" "reiserfs 4k nodesize" 4096 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 8k nodesize" 8192 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 16k nodesize" 16384 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 32k nodesize" 32768 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 64k nodesize" 65536 mkreiserfs -b 4096 +done
tests/convert-tests/011-reiserfs-delete-all-rollback/test.sh new file +67 -0
67 lines not on this page · raw message
tests/convert-tests/012-reiserfs-large-hole-extent/test.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/bash +# Create a base image with large hole extent, then convert to btrfs, +# check the converted image. +# Check if btrfs-convert can handle such large hole. +# Fast pinpoint regression test. No options combination nor checksum +# verification + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +default_mkfs="mkreiserfs -b 4096" +convert_test_preamble '' 'large hole extent test' 16k "$default_mkfs" +convert_test_prep_fs $default_mkfs + +run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/file" bs=1M \ + count=1 seek=1024 > /dev/null 2>&1 + +run_check_umount_test_dev +convert_test_do_convert
tests/convert-tests/013-reiserfs-common-inode-flags/test.sh new file +35 -0
@@ -0,0 +1,35 @@ +#!/bin/bash +# Check if btrfs-convert can copy common inode flags like SYNC/IMMUTABLE + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +fail=0 +default_mkfs="mkreiserfs -b 4096" +convert_test_preamble '' 'common inode flags test' 16k "$default_mkfs" +convert_test_prep_fs $default_mkfs + +# create file with specific flags +run_check $SUDO_HELPER touch "$TEST_MNT/flag_test" +run_check $SUDO_HELPER chattr +aAdSi "$TEST_MNT/flag_test" +run_check lsattr "$TEST_MNT/flag_test" + +run_check_umount_test_dev +convert_test_do_convert +run_check_mount_test_dev + +# Log the status +run_check lsattr "$TEST_MNT/flag_test" +# Above flags should be copied to btrfs flags, and lsattr should get them +run_check_stdout lsattr "$TEST_MNT/flag_test" | cut -f1 -d\ | grep "[aAdiS]" -q +if [ $? -ne 0 ]; then + rm tmp_output + _fail "no common inode flags are copied after convert" +fi + +run_check_umount_test_dev +convert_test_post_rollback
Signature · 1 line
2.11.0
Re: [PATCH 3/3] btrfs-progs: convert: add support for converting reiserfs
Quoted 281 lines · On 7/25/17 4:54 PM, [email protected] wrote:
From: Jeff Mahoney <[email protected]>
This patch adds support to convert reiserfs file systems in-place to btrfs.
It will convert extended attribute files to btrfs extended attributes, translate ACLs, coalesce tails that consist of multiple items into one item, and convert tails that are too big into indirect files.
This requires that libreiserfscore 3.6.27 be available.
Many of the test cases for convert apply regardless of what the source file system is and using ext4 is sufficient. I've included several test cases that are reiserfs-specific.
Signed-off-by Jeff Mahoney <[email protected]>--- Makefile | 3 +- Makefile.inc.in | 3 +- configure.ac | 10 +- convert/main.c | 13 +- convert/source-reiserfs.c | 1011 ++++++++++++++++++++ convert/source-reiserfs.h | 105 ++ tests/common.convert | 14 +- tests/convert-tests/010-reiserfs-basic/test.sh | 16 + .../011-reiserfs-delete-all-rollback/test.sh | 67 ++ .../012-reiserfs-large-hole-extent/test.sh | 23 + .../013-reiserfs-common-inode-flags/test.sh | 35 + 11 files changed, 1290 insertions(+), 10 deletions(-) create mode 100644 convert/source-reiserfs.c create mode 100644 convert/source-reiserfs.h create mode 100755 tests/convert-tests/010-reiserfs-basic/test.sh create mode 100755 tests/convert-tests/011-reiserfs-delete-all-rollback/test.sh create mode 100755 tests/convert-tests/012-reiserfs-large-hole-extent/test.sh create mode 100755 tests/convert-tests/013-reiserfs-common-inode-flags/test.shMakefile +2
-1@@ -111,7 +111,7 @@ libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-l kernel-lib/radix-tree.h kernel-lib/sizes.h extent-cache.h \ extent_io.h ioctl.h ctree.h btrfsck.h version.h convert_objects = convert/main.o convert/common.o convert/source-fs.o \ - convert/source-ext2.o + convert/source-ext2.o convert/source-reiserfs.o mkfs_objects = mkfs/main.o mkfs/common.o TESTS = fsck-tests.sh convert-tests.sh@@ -188,6 +188,7 @@ endif # external libs required by various binaries; for btrfs-foo, # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2) +btrfs_convert_cflags += -DBTRFSCONVERT_REISERFS=$(BTRFSCONVERT_REISERFS) btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype btrfs_debug_tree_objects = cmds-inspect-dump-tree.o btrfs_show_super_objects = cmds-inspect-dump-super.oMakefile.inc.in +2
-1@@ -12,6 +12,7 @@ INSTALL = @INSTALL@ DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@ DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@ BTRFSCONVERT_EXT2 = @BTRFSCONVERT_EXT2@ +BTRFSCONVERT_REISERFS = @BTRFSCONVERT_REISERFS@ SUBST_CFLAGS = @CFLAGS@ SUBST_LDFLAGS = @LDFLAGS@@@ -31,6 +32,6 @@ udevruledir = ${udevdir}/rules.d # external libs required by various binaries; for btrfs-foo, # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules in Makefile -btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@ +btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@ @REISERFS_LIBS@ MAKEFILE_INC_INCLUDED = yesconfigure.ac +9
-1@@ -120,6 +120,7 @@ fi convertfs= BTRFSCONVERT_EXT2=0 +BTRFSCONVERT_REISERFS=0 if test "x$enable_convert" = xyes; then if test "x$with_convert" = "xauto" || echo "$with_convert" | grep -q "ext2"; then PKG_CHECK_MODULES(EXT2FS, [ext2fs >= 1.42],,@@ -131,11 +132,18 @@ if test "x$enable_convert" = xyes; then convertfs="${convertfs:+$convertfs,}ext2" BTRFSCONVERT_EXT2=1 fi + if test "x$with_convert" = "xauto" || echo "$with_convert" | grep -q "reiserfs"; then + PKG_CHECK_MODULES(REISERFS, [reiserfscore >= 3.6.27], + [BTRFSCONVERT_REISERFS=1],[]) + convertfs="${convertfs:+$convertfs,}reiserfs" + BTRFSCONVERT_REISERFS=1 + fi fi AC_SUBST([BTRFSCONVERT_EXT2]) +AC_SUBST([BTRFSCONVERT_REISERFS]) # catch typos -tmp=$(echo "$with_convert" | sed -e 's/auto//' | sed -e 's/ext2//' | sed -e 's/,\+/,/') +tmp=$(echo "$with_convert" | sed -e 's/auto//' | sed -e 's/ext2//' | sed -e 's/reiserfs//' | sed -e 's/,\+/,/') if ! test "x$tmp" = "x"; then AC_MSG_ERROR([unknown tokens for --with-convert: $tmp]) ficonvert/main.c +9
-4@@ -102,12 +102,16 @@#include "convert/source-fs.h" #include "fsfeatures.h" -const struct btrfs_convert_operations ext2_convert_ops; +extern const struct btrfs_convert_operations ext2_convert_ops; +extern const struct btrfs_convert_operations reiserfs_convert_ops;static const struct btrfs_convert_operations *convert_operations[] = { #if BTRFSCONVERT_EXT2 &ext2_convert_ops, #endif +#if BTRFSCONVERT_REISERFS + &reiserfs_convert_ops, +#endif };static void *print_copied_inodes (void *p)@@ -411,7 +415,7 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans, }/* -* Relocate the used ext2 data in reserved ranges +* Relocate the used source fs data in reserved ranges */ static int migrate_reserved_ranges(struct btrfs_trans_handle *trans, struct btrfs_root *root,@@ -1655,11 +1659,11 @@ static int do_rollback(const char *devname) ret =btrfs_search_slot (NULL , fs_info->tree_root, &key, &path,0 ,0 );btrfs_release_path (&path);if (ret >0 ) { -error ("unable to find ext2 image subvolume, is it deleted?" ); +error ("unable to find source fs image subvolume, is it deleted?" ); ret = -ENOENT ; goto close_fs; }else if (ret <0 ) { -error ("failed to find ext2 image subvolume: %s" , +error ("failed to find source fs image subvolume: %s" ,strerror (-ret)); goto close_fs; }@@ -1777,6 +1781,7 @@ static void print_usage(void)printf ("\n" );printf ("Supported filesystems:\n" );printf ("\text2/3/4: %s\n" ,BTRFSCONVERT_EXT2 ?"yes" :"no" ); +printf ("\treiserfs: %s\n" ,BTRFSCONVERT_REISERFS ?"yes" :"no" ); }int main (int argc,char *argv[])convert/source-reiserfs.c new file +114
-0@@ -0,0 +1,1011 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public +* License v2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public +* License along with this program; if not, write to the +* Free Software Foundation, Inc., 59 Temple Place - Suite 330, +* Boston, MA 021110-1307, USA. +*/ + +#if BTRFSCONVERT_REISERFS + +#include "kerncompat.h" +#include <linux/limits.h> +#include <linux/fs.h> +#include <limits.h> +#include <sys/stat.h> +#include "disk-io.h" +#include "transaction.h" +#include "utils.h" +#include "bitops.h" +#include "convert/common.h" +#include "convert/source-reiserfs.h" + +static inline u8 mode_to_file_type (u32 mode) +{ +switch (mode &S_IFMT ) { +case S_IFREG :return BTRFS_FT_REG_FILE ; +case S_IFDIR :return BTRFS_FT_DIR ; +case S_IFCHR :return BTRFS_FT_CHRDEV ; +case S_IFBLK :return BTRFS_FT_BLKDEV ; +case S_IFIFO :return BTRFS_FT_FIFO ; +case S_IFSOCK :return BTRFS_FT_SOCK ; +case S_IFLNK :return BTRFS_FT_SYMLINK ; + }; + +return BTRFS_FT_UNKNOWN ; +} + + +struct reiserfs_convert_info { +int privroot_found; +struct reiserfs_key privroot_key; +struct reiserfs_key xattr_key; +int copy_attrs; + +struct task_ctx *progress; + +/* used to track hardlinks */ +unsigned used_slots; +unsigned alloced_slots; +u64 *objectids; +}; + +static u32 reiserfs_count_objectids (reiserfs_filsys_t fs) +{ +struct reiserfs_super_block *sb = fs->fs_ondisk_sb; +u32 count =0 ; +u32 *map; +int i; + +if (fs->fs_format ==REISERFS_FORMAT_3_6 ) + map = (u32 *) (sb +1 ); +else + map = (u32 *)((struct reiserfs_super_block_v1 *)sb +1 ); + +for (i =0 ; i <get_sb_oid_cursize (sb); i +=2 ) + count +=le32_to_cpu (map[i +1 ]) - (le32_to_cpu (map[i]) +1 ); + +return count; +} + + +static int reiserfs_open_fs (struct btrfs_convert_context *cxt,const char *name) +{ +struct reiserfs_convert_info *info; +reiserfs_filsys_t fs; +long error; + + fs =reiserfs_open (name,O_RDONLY , &error,NULL ,0 ); +if (!fs) +return -1 ; + + error =reiserfs_open_ondisk_bitmap (fs); +if (error) { +reiserfs_close (fs); +return -1 ; + } + + cxt->fs_data = fs; + cxt->blocksize = fs->fs_blocksize; + cxt->block_count =get_sb_block_count (fs->fs_ondisk_sb); + cxt->total_bytes = cxt->blocksize * cxt->block_count; + cxt->volume_name =strndup (fs->fs_ondisk_sb->s_label,16 ); + cxt->first_data_block =0 ; + cxt->inodes_count =reiserfs_count_objectids (fs); + cxt->free_inodes_count =0 ; + info =calloc (1 ,sizeof (*info)); +if (!info) { +reiserfs_close (fs); +return -1 ; + } + +/* +* Inode attributes are somewhat of a hack on reiserfs and it was +* once possible to have garbage in the flags field. A superblock +* field now indicates that the field has been cleared and can +* be considered valid, but only on v3.6 format file systems.
convert/source-reiserfs.c line 124
Quoted 10 lines
convert/source-reiserfs.c new file +10
-0+*/ +if (fs->fs_format ==REISERFS_FORMAT_3_6 && +get_sb_v2_flag (fs->fs_ondisk_sb, reiserfs_attrs_cleared)) + info->copy_attrs =1 ; + + fs->fs_vp = info; +return 0 ; +} + +#if 0
Whoops. This will be removed as part of the next submission after review.
-Jeff
Quoted 1,198 lines
convert/source-reiserfs.c new file +887
-0+static int reiserfs_alloc_block (struct btrfs_convert_context *cxt,u64 goal, +u64 *block_ret) +{ +int ret; +reiserfs_filsys_t fs = cxt->fs_data; +unsigned long block = goal; + + ret =reiserfs_bitmap_find_zero_bit (fs->fs_bitmap2, &block); + +if (ret !=0 ) +return -1 ; + +reiserfs_bitmap_set_bit (fs->fs_bitmap2, block); + + *block_ret = block; +return 0 ; +} + +static void bc_reiserfs_free_block (struct btrfs_convert_context *cxt,u64 block) +{ +reiserfs_filsys_t fs = cxt->fs_data; +reiserfs_bitmap_clear_bit (fs->fs_bitmap2, block); +} + +static int reiserfs_test_block (struct btrfs_convert_context *cxt,u64 block64) +{ +reiserfs_filsys_t fs = cxt->fs_data; +u32 block = block64; + +BUG_ON (block != block64); +return reiserfs_bitmap_test_bit (fs->fs_bitmap2, block); +} +#endif + +static void reiserfs_close_fs (struct btrfs_convert_context *cxt) +{ +reiserfs_filsys_t fs = cxt->fs_data; +struct reiserfs_convert_info *info = fs->fs_vp; + +if (info) { +if (info->objectids) +free (info->objectids); +free (info); + fs->fs_vp =NULL ; + } + +/* We don't want changes to be persistent */ + fs->fs_bitmap2->bm_dirty =0 ; + +reiserfs_close (fs); +} + +static int compare_objectids (const void *p1,const void *p2) +{ +u64 v1 = *(u64 *)p1; +u64 v2 = *(u64 *)p2; + +if (v1 > v2) +return 1 ; +else if (v1 < v2) +return -1 ; +return 0 ; +} + +static int lookup_cached_objectid (reiserfs_filsys_t fs,u64 objectid) +{ +struct reiserfs_convert_info *info = fs->fs_vp; +u64 *result; + +if (!info->objectids) +return 0 ; + result =bsearch (&objectid, info->objectids, info->used_slots, +sizeof (u64), compare_objectids); +return result !=NULL ; +} + +static int insert_cached_objectid (reiserfs_filsys_t fs,u64 objectid) +{ +struct reiserfs_convert_info *info = fs->fs_vp; + +if (info->used_slots +1 >= info->alloced_slots) { +u64 *objectids =realloc (info->objectids, + (info->alloced_slots +1000 ) *sizeof (u64)); +if (!objectids) +return -ENOMEM ; + info->objectids = objectids; + info->alloced_slots +=1000 ; + } + info->objectids[info->used_slots++] = objectid; + +qsort (info->objectids, info->used_slots,sizeof (u64), + compare_objectids); +return 0 ; +} + +static int reiserfs_locate_privroot (reiserfs_filsys_t fs) +{ +int err; +unsigned generation; +struct reiserfs_convert_info *info = fs->fs_vp; +struct reiserfs_key key = root_dir_key; + + err =reiserfs_find_entry (fs, &key,".reiserfs_priv" , + &generation, &info->privroot_key); +if (err ==1 ) { + err =reiserfs_find_entry (fs, &info->privroot_key,"xattrs" , + &generation, &info->xattr_key); +if (err !=1 ) +memset (&info->xattr_key,0 ,sizeof (info->xattr_key)); + } + +return 0 ; +} + +static void reiserfs_convert_inode_flags (struct btrfs_inode_item *inode, +const struct stat_data *sd) +{ +u16 attrs =sd_v2_sd_attrs (sd); +u64 new_flags =0 ; + +if (attrs &FS_IMMUTABLE_FL ) + new_flags |=BTRFS_INODE_IMMUTABLE ; + +if (attrs &FS_APPEND_FL ) + new_flags |=BTRFS_INODE_APPEND ; + +if (attrs &FS_SYNC_FL ) + new_flags |=BTRFS_INODE_SYNC ; + +if (attrs &FS_NOATIME_FL ) + new_flags |=BTRFS_INODE_NOATIME ; + +if (attrs &FS_NODUMP_FL ) + new_flags |=BTRFS_INODE_NODUMP ; + +if (attrs &FS_NODUMP_FL ) + new_flags |=BTRFS_INODE_NODUMP ; + +btrfs_set_stack_inode_flags (inode, new_flags); + +} + +static void reiserfs_copy_inode_item (struct btrfs_inode_item *inode, +struct item_head *ih,void *stat_data, +int copy_inode_flags) +{ +u32 mode; +u32 rdev =0 ; + +memset (inode,0 ,sizeof (*inode)); +btrfs_set_stack_inode_generation (inode,1 ); +if (get_ih_key_format (ih) ==KEY_FORMAT_1 ) { +struct stat_data_v1 *sd = stat_data; + + mode =sd_v1_mode (sd); +btrfs_set_stack_inode_size (inode,sd_v1_size (sd)); +btrfs_set_stack_inode_nlink (inode,sd_v1_nlink (sd)); +btrfs_set_stack_inode_uid (inode,sd_v1_uid (sd)); +btrfs_set_stack_inode_gid (inode,sd_v1_gid (sd)); +btrfs_set_stack_timespec_sec (&inode->atime,sd_v1_atime (sd)); +btrfs_set_stack_timespec_sec (&inode->ctime,sd_v1_ctime (sd)); +btrfs_set_stack_timespec_sec (&inode->mtime,sd_v1_mtime (sd)); + +if (!S_ISREG (mode) && !S_ISDIR (mode) && !S_ISLNK (mode)) + rdev =new_decode_dev (sd_v1_rdev (sd)); + }else { +struct stat_data *sd = stat_data; + + mode =sd_v2_mode (sd); +btrfs_set_stack_inode_size (inode,sd_v2_size (sd)); +btrfs_set_stack_inode_nlink (inode,sd_v2_nlink (sd)); +btrfs_set_stack_inode_uid (inode,sd_v2_uid (sd)); +btrfs_set_stack_inode_gid (inode,sd_v2_gid (sd)); +btrfs_set_stack_timespec_sec (&inode->atime,sd_v2_atime (sd)); +btrfs_set_stack_timespec_sec (&inode->ctime,sd_v2_ctime (sd)); +btrfs_set_stack_timespec_sec (&inode->mtime,sd_v2_mtime (sd)); + +if (!S_ISREG (mode) && !S_ISDIR (mode) && !S_ISLNK (mode)) + rdev =new_decode_dev (sd_v2_rdev (sd)); + +if (copy_inode_flags) +reiserfs_convert_inode_flags (inode, sd); + + } +if (S_ISDIR (mode)) { +btrfs_set_stack_inode_size (inode,0 ); +btrfs_set_stack_inode_nlink (inode,1 ); + } +btrfs_set_stack_inode_mode (inode, mode); +btrfs_set_stack_inode_rdev (inode, rdev); + +} + +struct reiserfs_blk_iterate_data { +struct blk_iterate_data blk_data; +int indirect; +char *inline_data; +u64 inline_offset; +u32 inline_length; +}; + +static void init_reiserfs_blk_iterate_data ( +struct reiserfs_blk_iterate_data *data, +struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct btrfs_inode_item *inode, +u64 objectid,u32 convert_flags) +{ +init_blk_iterate_data (&data->blk_data, trans, root, inode, objectid, + convert_flags &CONVERT_FLAG_DATACSUM ); + data->indirect =0 ; + data->inline_data =NULL ; + data->inline_offset = (u64)-1 ; + data->inline_length =0 ; +} + +static int reiserfs_record_indirect_extent (reiserfs_filsys_t fs,u64 position, +u64 size,int num_ptrs, +u32 *ptrs,void *data) +{ +struct reiserfs_blk_iterate_data *bdata = data; +u32 file_block = position / fs->fs_blocksize; +int i; +int ret =0 ; + + bdata->indirect =1 ; + +for (i =0 ; i < num_ptrs; i++, file_block++) { +u32 block =d32_get (ptrs, i); + +if (!block) +continue ; + + ret =block_iterate_proc (block, file_block, &bdata->blk_data); +if (ret) +break ; + } + +return ret; +} + +static int reiserfs_record_direct_extent (reiserfs_filsys_t fs,__u64 position, +__u64 size,const char *body, +size_t len,void *data) +{ +struct reiserfs_blk_iterate_data *bdata = data; +char *inline_data; + +if (bdata->inline_offset == (u64)-1 ) + bdata->inline_offset = position; + + inline_data =realloc (bdata->inline_data, bdata->inline_length + len); +if (!inline_data) +return -ENOMEM ; + bdata->inline_data = inline_data; + +memcpy (bdata->inline_data + bdata->inline_length, body, len); + bdata->inline_length += len; + +return 0 ; +} + +static int convert_direct (struct btrfs_trans_handle *trans, +struct btrfs_root *root,u64 objectid, +struct btrfs_inode_item *inode,const char *body, +u32 length,u64 offset,u32 convert_flags) +{ +struct btrfs_key key; +u32 sectorsize = root->sectorsize; +int ret; +struct extent_buffer *eb; + +BUG_ON (length > sectorsize); + ret =btrfs_reserve_extent (trans, root, sectorsize, +0 ,0 ,-1ULL , &key,1 ); +if (ret) +return ret; + + eb =alloc_extent_buffer (&root->fs_info->extent_cache, key.objectid, + sectorsize); + +if (!eb) +return -ENOMEM ; + +write_extent_buffer (eb, body,0 , length); + ret =write_and_map_eb (root, eb); +free_extent_buffer (eb); +if (ret) +return ret; + +return btrfs_record_file_extent (trans, root, objectid, inode, offset, + key.objectid, sectorsize); +} + +static int reiserfs_record_file_extents (reiserfs_filsys_t fs, +struct btrfs_trans_handle *trans, +struct btrfs_root *root, +u64 objectid, +struct btrfs_inode_item *inode, +struct reiserfs_key *sd_key, +u32 convert_flags) + +{ +int ret; +u32 last_block; +u32 sectorsize = root->sectorsize; +u64 inode_size =btrfs_stack_inode_size (inode); +struct reiserfs_blk_iterate_data data; + +init_reiserfs_blk_iterate_data (&data, trans, root, inode, + objectid, convert_flags); + + ret =reiserfs_iterate_file_data (fs, sd_key, + reiserfs_record_indirect_extent, + reiserfs_record_direct_extent, &data); +if (ret) +return ret; + +if (data.indirect && data.blk_data.num_blocks) { + ret =record_file_blocks (&data.blk_data, + data.blk_data.first_block, + data.blk_data.disk_block, + data.blk_data.num_blocks); +if (ret) + goto fail; + } + + data.blk_data.first_block += data.blk_data.num_blocks; + last_block = (inode_size + sectorsize -1 ) / sectorsize; +if (last_block > data.blk_data.first_block) { + ret =record_file_blocks (&data.blk_data, + data.blk_data.first_block,0 , + last_block - data.blk_data.first_block); +if (ret) + goto fail; + } + +if (data.inline_length) { +if (data.inline_length <BTRFS_MAX_INLINE_DATA_SIZE (root)) { +int isize; + ret =btrfs_insert_inline_extent (trans, root, + objectid, + data.inline_offset, + data.inline_data, + data.inline_length); +if (ret) + goto fail; + + isize =btrfs_stack_inode_nbytes (inode); +btrfs_set_stack_inode_nbytes (inode, + isize + data.inline_length); + }else { + ret =convert_direct (trans, root, objectid, inode, + data.inline_data, + data.inline_length, + data.inline_offset, convert_flags); +if (ret) + goto fail; + } + } + + ret =0 ; +fail: +return ret; +} + +#define OID_OFFSET (BTRFS_FIRST_FREE_OBJECTID - REISERFS_ROOT_OBJECTID) +static int reiserfs_copy_meta (reiserfs_filsys_t fs,struct btrfs_root *root, +u32 convert_flags,u32 deh_dirid, +u32 deh_objectid,u8 *type); + +struct reiserfs_dirent_data { +u64 index; +u32 convert_flags; +struct btrfs_inode_item *inode; +struct btrfs_root *root; +}; + +static int reiserfs_copy_dirent (reiserfs_filsys_t fs, +const struct reiserfs_key *dir_short_key, +const char *name,size_t len, +__u32 deh_dirid,__u32 deh_objectid, +void *cb_data) +{ +int ret; +u8 type; +struct btrfs_trans_handle *trans; +u64 objectid = deh_objectid +OID_OFFSET ; +struct reiserfs_convert_info *info = fs->fs_vp; +struct reiserfs_dirent_data *dirent_data = cb_data; +struct btrfs_root *root = dirent_data->root; +__u32 dir_objectid =get_key_objectid (dir_short_key) +OID_OFFSET ; + +/* +* These are the extended attributes and shouldn't appear as files +* in the converted file systems. +*/ +if (deh_objectid ==get_key_objectid (&info->privroot_key)) +return 0 ; + + ret =reiserfs_copy_meta (fs, root, dirent_data->convert_flags, + deh_dirid, deh_objectid, &type); +if (ret) +return ret; + trans =btrfs_start_transaction (root,1 ); +if (!trans) +return -ENOMEM ; + + ret =convert_insert_dirent (trans, root, name, len, dir_objectid, + objectid, type, dirent_data->index++, + dirent_data->inode); +return btrfs_commit_transaction (trans, root); +} + +static int reiserfs_copy_symlink (struct btrfs_trans_handle *trans, +struct btrfs_root *root,u64 objectid, +struct btrfs_inode_item *btrfs_inode, +reiserfs_filsys_t fs, +struct reiserfs_path *sd_path) +{ +INITIALIZE_REISERFS_PATH (path); +struct item_head *ih =tp_item_head (sd_path); +struct reiserfs_key key = ih->ih_key; +int ret; +char *symlink; +int len; + +set_key_uniqueness (&key,type2uniqueness (TYPE_DIRECT )); +set_key_offset_v1 (&key,1 ); + + ret =reiserfs_search_by_key_3 (fs, &key, &path); +if (ret !=ITEM_FOUND ) { + ret = -ENOENT ; + goto fail; + } + + symlink =tp_item_body (&path); + len =get_ih_item_len (tp_item_head (&path)); + + ret =btrfs_insert_inline_extent (trans, root, objectid,0 , + symlink, len +1 ); +btrfs_set_stack_inode_nbytes (btrfs_inode, len +1 ); +fail: +pathrelse (&path); +return ret; +} + +static int reiserfs_copy_meta (reiserfs_filsys_t fs,struct btrfs_root *root, +u32 convert_flags,u32 deh_dirid, +u32 deh_objectid,u8 *type) +{ +INITIALIZE_REISERFS_PATH (path); +int ret =0 ; +struct item_head *ih; +struct reiserfs_key key; +struct btrfs_inode_item btrfs_inode; +struct btrfs_trans_handle *trans =NULL ; +struct reiserfs_convert_info *info = fs->fs_vp; +u32 mode; +u64 objectid = deh_objectid +OID_OFFSET ; +u64 parent = deh_dirid +OID_OFFSET ; +struct reiserfs_dirent_data dirent_data = { + .index =2 , + .convert_flags = convert_flags, + .inode = &btrfs_inode, + .root = root, + }; + +/* The root directory's dirid in reiserfs points to an object +* that does't exist. In btrfs it's self-referential. +*/ +if (deh_dirid ==REISERFS_ROOT_PARENT_OBJECTID ) + parent = objectid; + +set_key_dirid (&key, deh_dirid); +set_key_objectid (&key, deh_objectid); +set_key_offset_v2 (&key,0 ); +set_key_type_v2 (&key,TYPE_STAT_DATA ); + + ret =reiserfs_search_by_key_3 (fs, &key, &path); +if (ret !=ITEM_FOUND ) { + ret = -ENOENT ; + goto fail; + } + + ih =tp_item_head (&path); +if (!is_stat_data_ih (ih)) { + ret = -EINVAL ; + goto fail; + } + +reiserfs_copy_inode_item (&btrfs_inode, ih,tp_item_body (&path), + info->copy_attrs); + mode =btrfs_stack_inode_mode (&btrfs_inode); + *type =mode_to_file_type (mode); + +if (S_ISREG (mode)) { +/* Inodes with hardlinks should only be inserted once */ +if (btrfs_stack_inode_nlink (&btrfs_inode) >1 ) { +if (lookup_cached_objectid (fs, deh_objectid)) { + ret =0 ; + goto fail;/* Not a failure */ + } + ret =insert_cached_objectid (fs, deh_objectid); +if (ret) + goto fail; + } + + } + +if (!(convert_flags &CONVERT_FLAG_DATACSUM )) { +u32 flags =btrfs_stack_inode_flags (&btrfs_inode) | +BTRFS_INODE_NODATASUM ; +btrfs_set_stack_inode_flags (&btrfs_inode, flags); + } + +switch (mode &S_IFMT ) { +case S_IFREG : + trans =btrfs_start_transaction (root,1 ); +if (!trans) { + ret = -ENOMEM ; + goto fail; + } + ret =reiserfs_record_file_extents (fs, trans, root, objectid, + &btrfs_inode, &ih->ih_key, + convert_flags); +if (ret) + goto fail; +break ; +case S_IFDIR : + ret =reiserfs_iterate_dir (fs, &ih->ih_key, + reiserfs_copy_dirent, &dirent_data); +if (ret) + goto fail; + trans =btrfs_start_transaction (root,1 ); +if (!trans) { + ret = -ENOMEM ; + goto fail; + } + + ret =btrfs_insert_inode_ref (trans, root,".." ,2 , parent, + objectid,0 ); +break ; +case S_IFLNK : + trans =btrfs_start_transaction (root,1 ); +if (!trans) { + ret = -ENOMEM ; + goto fail; + } + ret =reiserfs_copy_symlink (trans, root, objectid, + &btrfs_inode, fs, &path); +if (ret) + goto fail; +break ; +default : + trans =btrfs_start_transaction (root,1 ); +if (!trans) { + ret = -ENOMEM ; + goto fail; + } + } + + ret =btrfs_insert_inode (trans, root, objectid, &btrfs_inode); +if (ret) + goto fail; + ret =btrfs_commit_transaction (trans, root); + info->progress->cur_copy_inodes++; + +fail: +pathrelse (&path); +return ret; +} + +struct reiserfs_xattr_data { +struct btrfs_root *root; +struct btrfs_trans_handle *trans; +u64 target_oid; +const char *name; +size_t namelen; +void *body; +size_t len; +}; + +static int reiserfs_xattr_indirect_fn (reiserfs_filsys_t fs,u64 position, +u64 size,int num_blocks, +u32 *blocks,void *data) +{ +int i; +struct reiserfs_xattr_data *xa_data = data; +size_t alloc =min (position + num_blocks * fs->fs_blocksize, size); +char *body; + +if (size >BTRFS_LEAF_DATA_SIZE (xa_data->root) - +sizeof (struct btrfs_item ) -sizeof (struct btrfs_dir_item )) { +fprintf (stderr,"skip large xattr on objectid %llu name %.*s\n" , + xa_data->target_oid, (int )xa_data->namelen, + xa_data->name); +return -E2BIG ; + } + + body =realloc (xa_data->body, alloc); +if (!body) +return -ENOMEM ; + + xa_data->body = body; + xa_data->len = alloc; + +for (i =0 ; i < num_blocks; i++) { +int ret; +u32 block =d32_get (blocks, i); +u64 offset = (u64 )block * fs->fs_blocksize; +size_t chunk =min_t (u64, size - position, fs->fs_blocksize); +char *buffer = xa_data->body + position; + ret =read_disk_extent (xa_data->root, offset, chunk, buffer); +if (ret) +return ret; + position += chunk; + } + +return 0 ; +} + +static int reiserfs_xattr_direct_fn (reiserfs_filsys_t fs,__u64 position, +__u64 size,const char *body,size_t len, +void *data) +{ +struct reiserfs_xattr_data *xa_data = data; +char *newbody; + +if (size >BTRFS_LEAF_DATA_SIZE (xa_data->root) - +sizeof (struct btrfs_item ) -sizeof (struct btrfs_dir_item )) { +fprintf (stderr,"skip large xattr on objectid %llu name %.*s\n" , + xa_data->target_oid, (int )xa_data->namelen, + xa_data->name); +return -E2BIG ; + } + + newbody =realloc (xa_data->body, position + len); +if (!newbody) +return -ENOMEM ; + xa_data->body = newbody; + xa_data->len = position + len; +memcpy (xa_data->body + position, body, len); +return 0 ; +} + +static int reiserfs_acl_to_xattr (void *dst,const void *src, +size_t dst_size,size_t src_size) +{ +int i, count; +const void *end = src + src_size; +acl_ea_header *ext_acl = (acl_ea_header *)dst; +acl_ea_entry *dst_entry = ext_acl->a_entries; +struct reiserfs_acl_entry *src_entry; + +if (src_size <sizeof (struct reiserfs_acl_header )) + goto fail; +if (((struct reiserfs_acl_header *)src)->a_version != +cpu_to_le32 (REISERFS_ACL_VERSION )) + goto fail; + src +=sizeof (struct reiserfs_acl_header ); + count =reiserfs_acl_count (src_size); +if (count <=0 ) + goto fail; + +BUG_ON (dst_size <acl_ea_size (count)); + ext_acl->a_version =cpu_to_le32 (ACL_EA_VERSION ); +for (i =0 ; i < count; i++, dst_entry++) { + src_entry = (struct reiserfs_acl_entry *)src; +if (src +sizeof (struct reiserfs_acl_entry_short ) > end) + goto fail; + dst_entry->e_tag = src_entry->e_tag; + dst_entry->e_perm = src_entry->e_perm; +switch (le16_to_cpu (src_entry->e_tag)) { +case ACL_USER_OBJ : +case ACL_GROUP_OBJ : +case ACL_MASK : +case ACL_OTHER : + src +=sizeof (struct reiserfs_acl_entry_short ); + dst_entry->e_id =cpu_to_le32 (ACL_UNDEFINED_ID ); +break ; +case ACL_USER : +case ACL_GROUP : + src +=sizeof (struct reiserfs_acl_entry ); +if (src > end) + goto fail; + dst_entry->e_id = src_entry->e_id; +break ; +default : + goto fail; + } + } +if (src != end) + goto fail; +return 0 ; +fail: +return -EINVAL ; +} + +static int reiserfs_copy_one_xattr (reiserfs_filsys_t fs, +const struct reiserfs_key *dir_short_key, +const char *name,size_t namelen, +__u32 deh_dirid, +__u32 deh_objectid,void *cb_data) +{ +struct reiserfs_convert_info *info = fs->fs_vp; +struct reiserfs_xattr_data *xa_data = cb_data; +struct reiserfs_key key = { + .k2_dir_id = deh_dirid, + .k2_objectid = deh_objectid, + }; +void *body =NULL ; +int len; +int ret; + + xa_data->name = name; + xa_data->namelen = namelen; + + ret =reiserfs_iterate_file_data (fs, &key, reiserfs_xattr_indirect_fn, + reiserfs_xattr_direct_fn, cb_data); +if (ret) + goto out; + +if (!reiserfs_check_xattr (xa_data->body, xa_data->len)) { +fprintf (stderr, +"skip corrupted xattr on objectid %u name %.*s\n" , + deh_objectid, (int )xa_data->namelen, + xa_data->name); + goto out; + } + + body = xa_data->body +sizeof (struct reiserfs_xattr_header ); + len = xa_data->len -sizeof (struct reiserfs_xattr_header ); + +if (!strncmp ("system.posix_acl_default" , name, namelen) || + !strncmp ("system.posix_acl_access" , name, namelen)) { +size_t bufsize =acl_ea_size (ext2_acl_count (len)); +char *databuf =malloc (bufsize); +if (!databuf) + goto out; + ret =reiserfs_acl_to_xattr (databuf, body, bufsize, len); +if (ret) + goto out; + body = databuf; + len = bufsize; + } + + ret =btrfs_insert_xattr_item (xa_data->trans, xa_data->root, + name, namelen, body, len, + xa_data->target_oid); + + info->progress->cur_copy_inodes++; +out: +if (body && + body != xa_data->body +sizeof (struct reiserfs_xattr_header )) +free (body); +if (xa_data->body) +free (xa_data->body); + xa_data->body =NULL ; + xa_data->len =0 ; + +return ret; +} + +static int reiserfs_copy_xattr_dir (reiserfs_filsys_t fs, +const struct reiserfs_key *dir_short_key, +const char *name,size_t len, +__u32 deh_dirid,__u32 deh_objectid, +void *cb_data) +{ +struct reiserfs_convert_info *info = fs->fs_vp; +struct reiserfs_xattr_data *xa_data = cb_data; +struct reiserfs_key dir_key = { + .k2_dir_id = deh_dirid, + .k2_objectid = deh_objectid, + }; +int ret, err; + + errno =0 ; + xa_data->target_oid =strtoull (name,NULL ,16 ); +if (xa_data->target_oid ==ULLONG_MAX && errno) +return -errno; + + xa_data->target_oid +=OID_OFFSET ; + + xa_data->trans =btrfs_start_transaction (xa_data->root,1 ); +if (!xa_data->trans) +return -ENOMEM ; + + ret =reiserfs_iterate_dir (fs, &dir_key, + reiserfs_copy_one_xattr, xa_data); + + err =btrfs_commit_transaction (xa_data->trans, xa_data->root); + info->progress->cur_copy_inodes++; + xa_data->trans =NULL ; +return ret ?: err; +} + +static int reiserfs_copy_xattrs (reiserfs_filsys_t fs,struct btrfs_root *root) +{ +struct reiserfs_convert_info *info = fs->fs_vp; +struct reiserfs_xattr_data data = { + .root = root, + }; + +if (info->xattr_key.k2_objectid ==0 ) +return 0 ; + +return reiserfs_iterate_dir (fs, &info->xattr_key, + reiserfs_copy_xattr_dir, &data); +} + +static int reiserfs_copy_inodes (struct btrfs_convert_context *cxt, +struct btrfs_root *root, +u32 convert_flags, +struct task_ctx *p) +{ +reiserfs_filsys_t fs = cxt->fs_data; +struct reiserfs_convert_info *info = fs->fs_vp; +int ret; +u8 type; + + info->progress = p; + + ret =reiserfs_locate_privroot (fs); +if (ret) + goto out; + + ret =reiserfs_copy_meta (fs, root, convert_flags, +REISERFS_ROOT_PARENT_OBJECTID , +REISERFS_ROOT_OBJECTID , &type); +if (ret) + goto out; + +if (convert_flags &CONVERT_FLAG_XATTR ) + ret =reiserfs_copy_xattrs (fs, root); + +out: + info->progress =NULL ; +return ret; +} + +static int read_used_space (struct btrfs_convert_context *cxt) +{ +reiserfs_filsys_t fs = cxt->fs_data; +u64 start, end =0 ; +unsigned int size =get_sb_block_count (fs->fs_ondisk_sb); +unsigned long *bitmap = (unsigned long *)fs->fs_bitmap2->bm_map; +int ret =0 ; + +/* We have the entire bitmap loaded so we can just ping pong with +* ffz and ffs +*/ + +while (end < size) { +u64 offset, length; + start =find_next_bit (bitmap, size, end); +if (start >= size) +break ; + end =find_next_zero_bit (bitmap, size, start); +if (end > size) + end = size; + offset = start * fs->fs_blocksize; + length = (end - start) * fs->fs_blocksize; + ret =add_merge_cache_extent (&cxt->used_space, offset, length); +if (ret <0 ) +break ; + } + +return ret; +} + +static int reiserfs_check_state (struct btrfs_convert_context *cxt) +{ +return 0 ; +} + +const struct btrfs_convert_operations reiserfs_convert_ops = { + .name ="reiserfs" , + .open_fs = reiserfs_open_fs, + .read_used_space = read_used_space, + .copy_inodes = reiserfs_copy_inodes, + .close_fs = reiserfs_close_fs, + .check_state = reiserfs_check_state, +}; + +#endif /* BTRFSCONVERT_REISERFS */ convert/source-reiserfs.h new file +105
-0@@ -0,0 +1,105 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public +* License v2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public +* License along with this program; if not, write to the +* Free Software Foundation, Inc., 59 Temple Place - Suite 330, +* Boston, MA 021110-1307, USA. +*/ + +#ifndef __BTRFS_CONVERT_SOURCE_REISERFS_H__ +#define __BTRFS_CONVERT_SOURCE_REISERFS_H__ + +#if BTRFSCONVERT_REISERFS + +#include "kerncompat.h" +#include <reiserfs/misc.h> +#include <reiserfs/io.h> +#include <reiserfs/reiserfs_lib.h> +#include <reiserfs/reiserfs_fs.h> +#include <linux/kdev_t.h> +#include "convert/source-fs.h" + +#define REISERFS_ACL_VERSION 0x0001 + +/* 23.2.5 acl_tag_t values */ + +#define ACL_UNDEFINED_TAG (0x00) +#define ACL_USER_OBJ (0x01) +#define ACL_USER (0x02) +#define ACL_GROUP_OBJ (0x04) +#define ACL_GROUP (0x08) +#define ACL_MASK (0x10) +#define ACL_OTHER (0x20) + +/* 23.2.7 ACL qualifier constants */ + +#define ACL_UNDEFINED_ID ((id_t)-1) + +typedef struct { +__le16 e_tag; +__le16 e_perm; +__le32 e_id; +}ext2_acl_entry ; + +typedef struct { +__le16 e_tag; +__le16 e_perm; +}ext2_acl_entry_short ; + +typedef struct { +__le32 a_version; +}ext2_acl_header ; + +#define ACL_EA_VERSION 0x0002 + +typedef struct { +__le16 e_tag; +__le16 e_perm; +__le32 e_id; +}acl_ea_entry ; + +typedef struct { +__le32 a_version; +acl_ea_entry a_entries[0 ]; +}acl_ea_header ; + +static inline int ext2_acl_count (size_t size) +{ +ssize_t s; + size -=sizeof (ext2_acl_header); + s = size -4 *sizeof (ext2_acl_entry_short); +if (s <0 ) { +if (size %sizeof (ext2_acl_entry_short)) +return -1 ; +return size /sizeof (ext2_acl_entry_short); + }else { +if (s %sizeof (ext2_acl_entry)) +return -1 ; +return s /sizeof (ext2_acl_entry) +4 ; + } +} + + +static inline size_t acl_ea_size (int count) +{ +return sizeof (acl_ea_header) + count *sizeof (acl_ea_entry); +} + +static inline dev_t new_decode_dev (u32 dev) +{ +unsigned major = (dev &0xfff00 ) >>8 ; +unsigned minor = (dev &0xff ) | ((dev >>12 ) &0xfff00 ); + +return MKDEV (major, minor); +} + +#endif /* BTRFSCONVERT_REISERFS */ +#endif tests/common.convert +11
-3@@ -17,7 +17,7 @@ convert_test_preamble() { msg="$2" shift 3 echo " [TEST/conv] $msg, btrfs" "${features:-defaults}" - echo "creating ext image with: $@" >> "$RESULTS" + echo "creating test image with: $@" >> "$RESULTS" } # prepare TEST_DEV before conversion, create filesystem and mount it, image@@ -29,10 +29,18 @@ convert_test_prep_fs() { run_check truncate -s 0 "$TEST_DEV" # 256MB is the smallest acceptable btrfs image. run_check truncate -s 512M "$TEST_DEV" - run_check "$@" -F "$TEST_DEV" + force= + mountopts= + case "$@" in + *mke2fs*) + force=-F ;; + *mkreiserfs*) + force=-ff; mountopts="-o acl,user_xattr,attrs" ;; + esac + run_check "$@" $force "$TEST_DEV" # create a file to check btrfs-convert can convert regular file correct - run_check_mount_test_dev + run_check_mount_test_dev $mountopts # create a file inside the fs before convert, to make sure there is # data covering btrfs backup superblock range (64M)tests/convert-tests/010-reiserfs-basic/test.sh new file +16
-0@@ -0,0 +1,16 @@ +#!/bin/bash + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +for feature in '' 'extref' 'skinny-metadata' 'no-holes'; do + convert_test "$feature" "reiserfs 4k nodesize" 4096 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 8k nodesize" 8192 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 16k nodesize" 16384 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 32k nodesize" 32768 mkreiserfs -b 4096 + convert_test "$feature" "reiserfs 64k nodesize" 65536 mkreiserfs -b 4096 +donetests/convert-tests/011-reiserfs-delete-all-rollback/test.sh new file +67
-0@@ -0,0 +1,67 @@ +#!/bin/bash +# create a base image, convert to btrfs, remove all files, rollback the reiserfs image + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +# simple wrapper for a convert test +# $1: btrfs features, argument to -O +# $2: message +# $3: nodesize value +# $4 + rest: command to create the reiserfs image +do_test() { + local features + local msg + local nodesize + local CHECKSUMTMP + local here + + features="$1" + msg="$2" + nodesize="$3" + shift 3 + convert_test_preamble "$features" "$msg" "$nodesize" "$@" + convert_test_prep_fs "$@" + populate_fs + CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX) + convert_test_gen_checksums "$CHECKSUMTMP" + + run_check_umount_test_dev + + convert_test_do_convert "$features" "$nodesize" + + run_check_mount_test_dev + convert_test_post_check_checksums "$CHECKSUMTMP" + + here=$(pwd) + cd "$TEST_MNT" || _fail "cannot cd to TEST_MNT" + # reiserfs_saved/image must not be deleted + run_mayfail $SUDO_HELPER find "$TEST_MNT"/ -mindepth 1 -path '*reiserfs_saved' -prune -o -exec rm -vrf "{}" \; + cd "$here" + run_check "$TOP/btrfs" filesystem sync "$TEST_MNT" + run_check_umount_test_dev + convert_test_post_rollback + + run_check_mount_test_dev + convert_test_post_check_checksums "$CHECKSUMTMP" + run_check_umount_test_dev + + # mount again and verify checksums + run_check_mount_test_dev + convert_test_post_check_checksums "$CHECKSUMTMP" + run_check_umount_test_dev + + rm "$CHECKSUMTMP" +} + +for feature in '' 'extref' 'skinny-metadata' 'no-holes'; do + do_test "$feature" "reiserfs 4k nodesize" 4096 mkreiserfs -b 4096 + do_test "$feature" "reiserfs 8k nodesize" 8192 mkreiserfs -b 4096 + do_test "$feature" "reiserfs 16k nodesize" 16384 mkreiserfs -b 4096 + do_test "$feature" "reiserfs 32k nodesize" 32768 mkreiserfs -b 4096 + do_test "$feature" "reiserfs 64k nodesize" 65536 mkreiserfs -b 4096 +donetests/convert-tests/012-reiserfs-large-hole-extent/test.sh new file +23
-0@@ -0,0 +1,23 @@ +#!/bin/bash +# Create a base image with large hole extent, then convert to btrfs, +# check the converted image. +# Check if btrfs-convert can handle such large hole. +# Fast pinpoint regression test. No options combination nor checksum +# verification + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +default_mkfs="mkreiserfs -b 4096" +convert_test_preamble '' 'large hole extent test' 16k "$default_mkfs" +convert_test_prep_fs $default_mkfs + +run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/file" bs=1M \ + count=1 seek=1024 > /dev/null 2>&1 + +run_check_umount_test_dev +convert_test_do_converttests/convert-tests/013-reiserfs-common-inode-flags/test.sh new file +35
-0@@ -0,0 +1,35 @@ +#!/bin/bash +# Check if btrfs-convert can copy common inode flags like SYNC/IMMUTABLE + +source "$TOP/tests/common" +source "$TOP/tests/common.convert" + +setup_root_helper +prepare_test_dev 512M +check_prereq btrfs-convert + +fail=0 +default_mkfs="mkreiserfs -b 4096" +convert_test_preamble '' 'common inode flags test' 16k "$default_mkfs" +convert_test_prep_fs $default_mkfs + +# create file with specific flags +run_check $SUDO_HELPER touch "$TEST_MNT/flag_test" +run_check $SUDO_HELPER chattr +aAdSi "$TEST_MNT/flag_test" +run_check lsattr "$TEST_MNT/flag_test" + +run_check_umount_test_dev +convert_test_do_convert +run_check_mount_test_dev + +# Log the status +run_check lsattr "$TEST_MNT/flag_test" +# Above flags should be copied to btrfs flags, and lsattr should get them +run_check_stdout lsattr "$TEST_MNT/flag_test" | cut -f1 -d\ | grep "[aAdiS]" -q +if [ $? -ne 0 ]; then + rm tmp_output + _fail "no common inode flags are copied after convert" +fi + +run_check_umount_test_dev +convert_test_post_rollback
Signature · 2 lines
Jeff Mahoney SUSE Labs
Quoted 8 lines · On 2017年07月26日 04:54, [email protected] wrote:
From: Jeff Mahoney <[email protected]>
Commit
522ef705e38(btrfs-progs: convert: Introduce function to calculate the available space) changed how we handle migrating file data so that we never have btrfs space associated with the reserved ranges. This works pretty well and when we iterate over the file blocks, the associations are redirected to the migrated locations.
Quoted 8 lines
This commit missed the case in
block_iterate_procwhere we just check for intersection with a superblock location before looking up a block group.intersect_with_sbchecks to see if the range intersects with a stripe containing a superblock but, in fact, we've reserved the full 0-1MB range at the start of the disk. So a file block located at e.g. 160kB will fall in the reserved region but won't be excepted inblock_iterate_block. We ultimately hit aBUG_ONwhen we fail to look up the block group for that location.
The description of the problem is indeed correct.
Quoted 2 lines
This is reproducible using convert-tests/003-ext4-basic.
Thanks for pointing this out, I also reproduced it.
While it would be nicer if you could upload a special crafted image as indicated test case.
IIRC the test passed without problem several versions ago, so there may be some factors preventing the bug from being exposed.
Quoted 6 lines
The fix is to have
intersect_with_sbandblock_iterate_procunderstand the full size of the reserved ranges. Since we use the range to determine the boundary for the block iterator, let's just return the boundary. 0 isn't a valid boundary and means that we proceed normally with block group lookup.
I'm OK with current fix as it indeed fix the bug and has minimal impact on current code.
So feel free to add:
While I think there is a better way to solve it more completely.
As when we run into block_iterate_proc(), we have already created ext2_save/image.
So we can use the the image as ext2 <-> btrfs position mapping, just as we have already done in record_file_blocks().
That's to say, we don't need too much care about the intersection with reserved range, but just letting record_file_blocks() to handle it will be good enough.
What do you think about this idea?
Thanks,
Qu
Quoted 67 lines
Cc Qu Wenruo <[email protected]>Signed-off-by Jeff Mahoney <[email protected]>--- convert/source-fs.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)convert/source-fs.c +11
-14@@ -28,18 +28,16 @@ const struct simple_range btrfs_reserved_ranges[3] = { { BTRFS_SB_MIRROR_OFFSET(2), SZ_64K } }; -static int intersect_with_sb (u64 bytenr,u64 num_bytes) +static u64 intersect_with_reserved (u64 bytenr,u64 num_bytes) {int i; -u64 offset; -for (i =0 ; i <BTRFS_SUPER_MIRROR_MAX ; i++) { - offset =btrfs_sb_offset (i); - offset &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +for (i =0 ; i <ARRAY_SIZE (btrfs_reserved_ranges); i++) { +const struct simple_range *range = &btrfs_reserved_ranges[i]; -if (bytenr < offset +BTRFS_STRIPE_LEN && - bytenr + num_bytes > offset) -return 1 ; +if (bytenr <range_end (range) && + bytenr + num_bytes >= range->start) +return range_end (range); }return 0 ; }@@ -64,14 +62,14 @@ int block_iterate_proc(u64 disk_block, u64 file_block, struct blk_iterate_data *idata) {int ret =0 ; -int sb_region; +u64 reserved_boundary;int do_barrier;struct btrfs_root *root = idata->root;struct btrfs_block_group_cache *cache;u64 bytenr = disk_block * root->sectorsize; - sb_region =intersect_with_sb (bytenr, root->sectorsize); - do_barrier = sb_region || disk_block >= idata->boundary; + reserved_boundary =intersect_with_reserved (bytenr, root->sectorsize); + do_barrier = reserved_boundary || disk_block >= idata->boundary;if ((idata->num_blocks >0 && do_barrier) || (file_block > idata->first_block + idata->num_blocks) || (disk_block != idata->disk_block + idata->num_blocks)) {@@ -91,9 +89,8 @@ int block_iterate_proc(u64 disk_block, u64 file_block, goto fail; } -if (sb_region) { - bytenr +=BTRFS_STRIPE_LEN -1 ; - bytenr &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +if (reserved_boundary) { + bytenr = reserved_boundary; }else { cache =btrfs_lookup_block_group (root->fs_info, bytenr);BUG_ON (!cache);
Quoted 52 lines · Qu Wenruo
Quoted 8 lines · On 2017年07月26日 04:54, [email protected] wrote:
From: Jeff Mahoney <[email protected]>
Commit
522ef705e38(btrfs-progs: convert: Introduce function to calculate the available space) changed how we handle migrating file data so that we never have btrfs space associated with the reserved ranges. This works pretty well and when we iterate over the file blocks, the associations are redirected to the migrated locations.Quoted 8 lines
This commit missed the case in
block_iterate_procwhere we just check for intersection with a superblock location before looking up a block group.intersect_with_sbchecks to see if the range intersects with a stripe containing a superblock but, in fact, we've reserved the full 0-1MB range at the start of the disk. So a file block located at e.g. 160kB will fall in the reserved region but won't be excepted inblock_iterate_block. We ultimately hit aBUG_ONwhen we fail to look up the block group for that location.The description of the problem is indeed correct.
Quoted 2 lines
This is reproducible using convert-tests/003-ext4-basic.
Thanks for pointing this out, I also reproduced it.
While it would be nicer if you could upload a special crafted image as indicated test case.
IIRC the test passed without problem several versions ago, so there may be some factors preventing the bug from being exposed.Quoted 6 lines
The fix is to have
intersect_with_sbandblock_iterate_procunderstand the full size of the reserved ranges. Since we use the range to determine the boundary for the block iterator, let's just return the boundary. 0 isn't a valid boundary and means that we proceed normally with block group lookup.I'm OK with current fix as it indeed fix the bug and has minimal impact on current code.
So feel free to add:
Reviewed-by Qu Wenruo <[email protected]>While I think there is a better way to solve it more completely.
As when we run into
block_iterate_proc(), we have already created ext2_save/image.
So we can use the the image as ext2 <-> btrfs position mapping, just as we have already done inrecord_file_blocks().
Quoted 5 lines
That's to say, we don't need too much care about the intersection with reserved range, but just letting
record_file_blocks()to handle it will be good enough.What do you think about this idea?
I think you're right. It should do the mapping already so we don't need to do anything special in block_iterate_proc. I can test that in a bit.
-Jeff
Quoted 73 lines
Thanks,
QuQuoted 65 lines
Cc Qu Wenruo <[email protected]>Signed-off-by Jeff Mahoney <[email protected]>--- convert/source-fs.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)convert/source-fs.c +11
-9@@ -28,18 +28,16 @@ const struct simple_range btrfs_reserved_ranges[3] = { {BTRFS_SB_MIRROR_OFFSET (2 ),SZ_64K } }; -static int intersect_with_sb(u64 bytenr, u64 num_bytes) +static u64 intersect_with_reserved(u64 bytenr, u64 num_bytes) {int i; -u64 offset; - for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { - offset =btrfs_sb_offset (i); - offset &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +for (i =0 ; i <ARRAY_SIZE (btrfs_reserved_ranges); i++) { +const struct simple_range *range = &btrfs_reserved_ranges[i]; - if (bytenr < offset + BTRFS_STRIPE_LEN && - bytenr + num_bytes > offset) -return 1 ; + if (bytenr < range_end(range) && + bytenr + num_bytes >= range->start) + return range_end(range); }return 0 ; }@@ -64,14 +62,14 @@ int block_iterate_proc(u64 disk_block, u64 file_block, struct blk_iterate_data *idata) {int ret =0 ; -int sb_region; +u64 reserved_boundary;int do_barrier;struct btrfs_root *root = idata->root;struct btrfs_block_group_cache *cache;u64 bytenr = disk_block * root->sectorsize; - sb_region =intersect_with_sb (bytenr, root->sectorsize); - do_barrier = sb_region || disk_block >= idata->boundary; + reserved_boundary =intersect_with_reserved (bytenr, root->sectorsize); + do_barrier = reserved_boundary || disk_block >= idata->boundary;if ((idata->num_blocks >0 && do_barrier) || (file_block > idata->first_block + idata->num_blocks) || (disk_block != idata->disk_block + idata->num_blocks)) {@@ -91,9 +89,8 @@ int block_iterate_proc(u64 disk_block, u64 file_block, goto fail; } - if (sb_region) { - bytenr +=BTRFS_STRIPE_LEN -1 ; - bytenr &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +if (reserved_boundary) { + bytenr = reserved_boundary; }else { cache =btrfs_lookup_block_group (root->fs_info, bytenr);BUG_ON (!cache);Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Jeff Mahoney SUSE Labs
Quoted 53 lines · Jeff Mahoney
Quoted 52 lines · Qu Wenruo
Quoted 8 lines · On 2017年07月26日 04:54, [email protected] wrote:
From: Jeff Mahoney <[email protected]>
Commit
522ef705e38(btrfs-progs: convert: Introduce function to calculate the available space) changed how we handle migrating file data so that we never have btrfs space associated with the reserved ranges. This works pretty well and when we iterate over the file blocks, the associations are redirected to the migrated locations.Quoted 8 lines
This commit missed the case in
block_iterate_procwhere we just check for intersection with a superblock location before looking up a block group.intersect_with_sbchecks to see if the range intersects with a stripe containing a superblock but, in fact, we've reserved the full 0-1MB range at the start of the disk. So a file block located at e.g. 160kB will fall in the reserved region but won't be excepted inblock_iterate_block. We ultimately hit aBUG_ONwhen we fail to look up the block group for that location.The description of the problem is indeed correct.
Quoted 2 lines
This is reproducible using convert-tests/003-ext4-basic.
Thanks for pointing this out, I also reproduced it.
While it would be nicer if you could upload a special crafted image as indicated test case.
IIRC the test passed without problem several versions ago, so there may be some factors preventing the bug from being exposed.Quoted 6 lines
The fix is to have
intersect_with_sbandblock_iterate_procunderstand the full size of the reserved ranges. Since we use the range to determine the boundary for the block iterator, let's just return the boundary. 0 isn't a valid boundary and means that we proceed normally with block group lookup.I'm OK with current fix as it indeed fix the bug and has minimal impact on current code.
So feel free to add:
Reviewed-by Qu Wenruo <[email protected]>While I think there is a better way to solve it more completely.
As when we run into
block_iterate_proc(), we have already created ext2_save/image.
So we can use the the image as ext2 <-> btrfs position mapping, just as we have already done inrecord_file_blocks().
Quoted 8 lines
Quoted 5 lines
That's to say, we don't need too much care about the intersection with reserved range, but just letting
record_file_blocks()to handle it will be good enough.What do you think about this idea?
I think you're right. It should do the mapping already so we don't need to do anything special in
block_iterate_proc. I can test that in a bit.
So the idea works and, in fact, we could really get rid of most of block_iterate_proc and still get correct results. This code is an optimization so that we can quickly assemble larger extents and not have to grow on-disk extents repeatedly. The code as it is does the right thing most of the time but the boundary condition triggers the barrier for every block in a migrated range and record_file_blocks must do the growing. The end result works fine, it's just slower than it needs to be for that 0-1MB range. I'm not sure I care enough to invest the time to fix that.
-Jeff
Quoted 77 lines
-Jeff
Quoted 73 lines
Thanks,
QuQuoted 65 lines
Cc Qu Wenruo <[email protected]>Signed-off-by Jeff Mahoney <[email protected]>--- convert/source-fs.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)convert/source-fs.c +11
-9@@ -28,18 +28,16 @@ const struct simple_range btrfs_reserved_ranges[3] = { {BTRFS_SB_MIRROR_OFFSET (2 ),SZ_64K } }; -static int intersect_with_sb(u64 bytenr, u64 num_bytes) +static u64 intersect_with_reserved(u64 bytenr, u64 num_bytes) {int i; -u64 offset; - for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { - offset =btrfs_sb_offset (i); - offset &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +for (i =0 ; i <ARRAY_SIZE (btrfs_reserved_ranges); i++) { +const struct simple_range *range = &btrfs_reserved_ranges[i]; - if (bytenr < offset + BTRFS_STRIPE_LEN && - bytenr + num_bytes > offset) -return 1 ; + if (bytenr < range_end(range) && + bytenr + num_bytes >= range->start) + return range_end(range); }return 0 ; }@@ -64,14 +62,14 @@ int block_iterate_proc(u64 disk_block, u64 file_block, struct blk_iterate_data *idata) {int ret =0 ; -int sb_region; +u64 reserved_boundary;int do_barrier;struct btrfs_root *root = idata->root;struct btrfs_block_group_cache *cache;u64 bytenr = disk_block * root->sectorsize; - sb_region =intersect_with_sb (bytenr, root->sectorsize); - do_barrier = sb_region || disk_block >= idata->boundary; + reserved_boundary =intersect_with_reserved (bytenr, root->sectorsize); + do_barrier = reserved_boundary || disk_block >= idata->boundary;if ((idata->num_blocks >0 && do_barrier) || (file_block > idata->first_block + idata->num_blocks) || (disk_block != idata->disk_block + idata->num_blocks)) {@@ -91,9 +89,8 @@ int block_iterate_proc(u64 disk_block, u64 file_block, goto fail; } - if (sb_region) { - bytenr +=BTRFS_STRIPE_LEN -1 ; - bytenr &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +if (reserved_boundary) { + bytenr = reserved_boundary; }else { cache =btrfs_lookup_block_group (root->fs_info, bytenr);BUG_ON (!cache);Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Jeff Mahoney SUSE Labs
Quoted 63 lines · Jeff Mahoney
Quoted 53 lines · Jeff Mahoney
Quoted 52 lines · Qu Wenruo
Quoted 8 lines · On 2017年07月26日 04:54, [email protected] wrote:
From: Jeff Mahoney <[email protected]>
Commit
522ef705e38(btrfs-progs: convert: Introduce function to calculate the available space) changed how we handle migrating file data so that we never have btrfs space associated with the reserved ranges. This works pretty well and when we iterate over the file blocks, the associations are redirected to the migrated locations.Quoted 8 lines
This commit missed the case in
block_iterate_procwhere we just check for intersection with a superblock location before looking up a block group.intersect_with_sbchecks to see if the range intersects with a stripe containing a superblock but, in fact, we've reserved the full 0-1MB range at the start of the disk. So a file block located at e.g. 160kB will fall in the reserved region but won't be excepted inblock_iterate_block. We ultimately hit aBUG_ONwhen we fail to look up the block group for that location.The description of the problem is indeed correct.
Quoted 2 lines
This is reproducible using convert-tests/003-ext4-basic.
Thanks for pointing this out, I also reproduced it.
While it would be nicer if you could upload a special crafted image as indicated test case.
IIRC the test passed without problem several versions ago, so there may be some factors preventing the bug from being exposed.Quoted 6 lines
The fix is to have
intersect_with_sbandblock_iterate_procunderstand the full size of the reserved ranges. Since we use the range to determine the boundary for the block iterator, let's just return the boundary. 0 isn't a valid boundary and means that we proceed normally with block group lookup.I'm OK with current fix as it indeed fix the bug and has minimal impact on current code.
So feel free to add:
Reviewed-by Qu Wenruo <[email protected]>While I think there is a better way to solve it more completely.
As when we run into
block_iterate_proc(), we have already created ext2_save/image.
So we can use the the image as ext2 <-> btrfs position mapping, just as we have already done inrecord_file_blocks().Quoted 8 lines
Quoted 5 lines
That's to say, we don't need too much care about the intersection with reserved range, but just letting
record_file_blocks()to handle it will be good enough.What do you think about this idea?
I think you're right. It should do the mapping already so we don't need to do anything special in
block_iterate_proc. I can test that in a bit.
Quoted 9 lines
So the idea works and, in fact, we could really get rid of most of
block_iterate_procand still get correct results. This code is an optimization so that we can quickly assemble larger extents and not have to grow on-disk extents repeatedly. The code as it is does the right thing most of the time but the boundary condition triggers the barrier for every block in a migrated range andrecord_file_blocksmust do the growing. The end result works fine, it's just slower than it needs to be for that 0-1MB range. I'm not sure I care enough to invest the time to fix that.
Yes, the record_file_extent() handles the mapping in a slow way, by doing btrfs_search_slot() to do the mapping, no matter if it's in the reserved range. So performance is much slower.
Performance wise, I'm OK using the current code.
Thanks,
Qu
Quoted 82 lines
-Jeff
Quoted 77 lines
-Jeff
Quoted 73 lines
Thanks,
QuQuoted 65 lines
Cc Qu Wenruo <[email protected]>Signed-off-by Jeff Mahoney <[email protected]>--- convert/source-fs.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)convert/source-fs.c +11
-9@@ -28,18 +28,16 @@ const struct simple_range btrfs_reserved_ranges[3] = { {BTRFS_SB_MIRROR_OFFSET (2 ),SZ_64K } }; -static int intersect_with_sb(u64 bytenr, u64 num_bytes) +static u64 intersect_with_reserved(u64 bytenr, u64 num_bytes) {int i; -u64 offset; - for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { - offset =btrfs_sb_offset (i); - offset &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +for (i =0 ; i <ARRAY_SIZE (btrfs_reserved_ranges); i++) { +const struct simple_range *range = &btrfs_reserved_ranges[i]; - if (bytenr < offset + BTRFS_STRIPE_LEN && - bytenr + num_bytes > offset) -return 1 ; + if (bytenr < range_end(range) && + bytenr + num_bytes >= range->start) + return range_end(range); }return 0 ; }@@ -64,14 +62,14 @@ int block_iterate_proc(u64 disk_block, u64 file_block, struct blk_iterate_data *idata) {int ret =0 ; -int sb_region; +u64 reserved_boundary;int do_barrier;struct btrfs_root *root = idata->root;struct btrfs_block_group_cache *cache;u64 bytenr = disk_block * root->sectorsize; - sb_region =intersect_with_sb (bytenr, root->sectorsize); - do_barrier = sb_region || disk_block >= idata->boundary; + reserved_boundary =intersect_with_reserved (bytenr, root->sectorsize); + do_barrier = reserved_boundary || disk_block >= idata->boundary;if ((idata->num_blocks >0 && do_barrier) || (file_block > idata->first_block + idata->num_blocks) || (disk_block != idata->disk_block + idata->num_blocks)) {@@ -91,9 +89,8 @@ int block_iterate_proc(u64 disk_block, u64 file_block, goto fail; } - if (sb_region) { - bytenr +=BTRFS_STRIPE_LEN -1 ; - bytenr &= ~((u64 )BTRFS_STRIPE_LEN -1 ); +if (reserved_boundary) { + bytenr = reserved_boundary; }else { cache =btrfs_lookup_block_group (root->fs_info, bytenr);BUG_ON (!cache);Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 3/3] btrfs-progs: convert: add support for converting reiserfs
In reply to [email protected]Quoted 8 lines · On Tue, Jul 25, 2017 at 04:54:43PM -0400, [email protected] wrote:
From: Jeff Mahoney <[email protected]>
This patch adds support to convert reiserfs file systems in-place to btrfs.
It will convert extended attribute files to btrfs extended attributes, translate ACLs, coalesce tails that consist of multiple items into one item, and convert tails that are too big into indirect files.
Quoted 7 lines
This requires that libreiserfscore 3.6.27 be available.
Many of the test cases for convert apply regardless of what the source file system is and using ext4 is sufficient. I've included several test cases that are reiserfs-specific.
Signed-off-by Jeff Mahoney <[email protected]>
Patches merged, with quite a few small fixups here and there. It took me less time to fix them on the way than to take it through the mailinglist. The tests were split to separate patch. I'm fine with keeping the reiserfs bits in one patch as it's an isolated feature. The tests are now running, I'll let it finish. There's some code or test code duplication that can be cleaned up eventually, but now I consider reiserfs conversion support done. Thanks.