#include <fcntl.h>
#include <unistd.h>
#include <uuid/uuid.h>
+#include <linux/limits.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
#include "crc32c.h"
#include "utils.h"
+#include "task-utils.h"
#include <ext2fs/ext2_fs.h>
#include <ext2fs/ext2fs.h>
#include <ext2fs/ext2_ext_attr.h>
#define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
-#define STRIPE_LEN (64 * 1024)
#define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
+struct task_ctx {
+ uint32_t max_copy_inodes;
+ uint32_t cur_copy_inodes;
+ struct task_info *info;
+};
+
+static void *print_copied_inodes(void *p)
+{
+ struct task_ctx *priv = p;
+ const char work_indicator[] = { '.', 'o', 'O', 'o' };
+ uint32_t count = 0;
+
+ task_period_start(priv->info, 1000 /* 1s */);
+ while (1) {
+ count++;
+ printf("copy inodes [%c] [%10d/%10d]\r",
+ work_indicator[count % 4], priv->cur_copy_inodes,
+ priv->max_copy_inodes);
+ fflush(stdout);
+ task_period_wait(priv->info);
+ }
+
+ return NULL;
+}
+
+static int after_copied_inodes(void *p)
+{
+ struct task_ctx *priv = p;
+
+ printf("\n");
+ task_period_stop(priv->info);
+
+ return 0;
+}
+
/*
* Open Ext2fs in readonly mode, read block allocation bitmap and
* inode bitmap into memory.
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
bytenr = btrfs_sb_offset(i);
- bytenr &= ~((u64)STRIPE_LEN - 1);
+ bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
break;
clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
- bytenr + STRIPE_LEN - 1, 0);
+ bytenr + BTRFS_STRIPE_LEN - 1, 0);
}
clear_extent_dirty(&root->fs_info->free_space_cache,
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
offset = btrfs_sb_offset(i);
- offset &= ~((u64)STRIPE_LEN - 1);
+ offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
- if (bytenr < offset + STRIPE_LEN &&
+ if (bytenr < offset + BTRFS_STRIPE_LEN &&
bytenr + num_bytes > offset)
return 1;
}
ret = btrfs_record_file_extent(trans, root, objectid, inode, file_pos,
disk_bytenr, num_bytes);
- if (ret || !checksum)
+ if (ret || !checksum || disk_bytenr == 0)
return ret;
return csum_disk_extent(trans, root, disk_bytenr, num_bytes);
}
if (sb_region) {
- bytenr += STRIPE_LEN - 1;
- bytenr &= ~((u64)STRIPE_LEN - 1);
+ bytenr += BTRFS_STRIPE_LEN - 1;
+ bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
} else {
cache = btrfs_lookup_block_group(root->fs_info, bytenr);
BUG_ON(!cache);
* scan ext2's inode bitmap and copy all used inodes.
*/
static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
- int datacsum, int packing, int noxattr)
+ int datacsum, int packing, int noxattr, struct task_ctx *p)
{
int ret;
errcode_t err;
objectid, ext2_fs, ext2_ino,
&ext2_inode, datacsum, packing,
noxattr);
+ p->cur_copy_inodes++;
if (ret)
return ret;
if (trans->blocks_used >= 4096) {
btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
btrfs_set_stack_chunk_owner(&chunk,
extent_root->root_key.objectid);
- btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN);
+ btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
btrfs_set_stack_chunk_type(&chunk, cache->flags);
btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
}
/*
- * Migrate super block to it's default position and zero 0 ~ 16k
+ * Migrate super block to its default position and zero 0 ~ 16k
*/
static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
{
btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
- btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
+ btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
btrfs_set_stack_chunk_io_align(chunk, sectorsize);
btrfs_set_stack_chunk_io_width(chunk, sectorsize);
}
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
offset = btrfs_sb_offset(i);
- offset &= ~((u64)STRIPE_LEN - 1);
+ offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
ret = relocate_extents_range(fs_root, ext2_root,
- offset, offset + STRIPE_LEN);
+ offset, offset + BTRFS_STRIPE_LEN);
if (ret)
goto fail;
}
return ret;
}
-static int do_convert(const char *devname, int datacsum, int packing,
- int noxattr)
+static int do_convert(const char *devname, int datacsum, int packing, int noxattr,
+ int copylabel, const char *fslabel, int progress)
{
int i, ret;
int fd = -1;
ext2_filsys ext2_fs;
struct btrfs_root *root;
struct btrfs_root *ext2_root;
+ struct task_ctx ctx;
ret = open_ext2fs(devname, &ext2_fs);
if (ret) {
goto fail;
}
ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
- blocks, total_bytes, blocksize, blocksize,
+ NULL, blocks, total_bytes, blocksize, blocksize,
blocksize, blocksize, 0);
if (ret) {
fprintf(stderr, "unable to create initial ctree: %s\n",
goto fail;
}
printf("creating btrfs metadata.\n");
- ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
+ ctx.max_copy_inodes = (ext2_fs->super->s_inodes_count
+ - ext2_fs->super->s_free_inodes_count);
+ ctx.cur_copy_inodes = 0;
+
+ if (progress) {
+ ctx.info = task_init(print_copied_inodes, after_copied_inodes, &ctx);
+ task_start(ctx.info);
+ }
+ ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr, &ctx);
if (ret) {
fprintf(stderr, "error during copy_inodes %d\n", ret);
goto fail;
}
+ if (progress) {
+ task_stop(ctx.info);
+ task_deinit(ctx.info);
+ }
printf("creating ext2fs image file.\n");
ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
if (!ext2_root) {
fprintf(stderr, "error during create_ext2_image %d\n", ret);
goto fail;
}
+ memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
+ if (copylabel == 1) {
+ strncpy(root->fs_info->super_copy->label,
+ ext2_fs->super->s_volume_name, 16);
+ fprintf(stderr, "copy label '%s'\n",
+ root->fs_info->super_copy->label);
+ } else if (copylabel == -1) {
+ strncpy(root->fs_info->super_copy->label, fslabel, BTRFS_LABEL_SIZE);
+ fprintf(stderr, "set label to '%s'\n", fslabel);
+ }
+
printf("cleaning up system chunk.\n");
ret = cleanup_sys_chunk(root, ext2_root);
if (ret) {
while (1) {
ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
&length, &multi, 0, NULL);
- if (ret)
+ if (ret) {
+ if (ret == -ENOENT) {
+ /* removed block group at the tail */
+ if (length == (u64)-1)
+ break;
+
+ /* removed block group in the middle */
+ goto next;
+ }
goto fail;
+ }
num_stripes = multi->num_stripes;
physical = multi->stripes[0].physical;
if (num_stripes != 1 || physical != bytenr)
goto fail;
-
+next:
bytenr += length;
if (bytenr >= total_bytes)
break;
return -1;
}
-static int do_rollback(const char *devname, int force)
+static int do_rollback(const char *devname)
{
int fd = -1;
int ret;
ext2_root = btrfs_read_fs_root(root->fs_info, &key);
if (!ext2_root || IS_ERR(ext2_root)) {
fprintf(stderr, "unable to open subvol %llu\n",
- key.objectid);
+ (unsigned long long)key.objectid);
goto fail;
}
if (bytenr >= total_bytes)
break;
ret = pwrite(fd, buf, sectorsize, bytenr);
+ if (ret != sectorsize) {
+ fprintf(stderr,
+ "error during zeroing supreblock %d: %d\n",
+ i, ret);
+ goto fail;
+ }
}
sb_bytenr = (u64)-1;
static void print_usage(void)
{
- printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
- printf("\t-d disable data checksum\n");
- printf("\t-i ignore xattrs and ACLs\n");
- printf("\t-n disable packing of small files\n");
- printf("\t-r roll back to ext2fs\n");
+ printf("usage: btrfs-convert [-d] [-i] [-n] [-r] [-l label] [-L] device\n");
+ printf("\t-d disable data checksum\n");
+ printf("\t-i ignore xattrs and ACLs\n");
+ printf("\t-n disable packing of small files\n");
+ printf("\t-r roll back to ext2fs\n");
+ printf("\t-l LABEL set filesystem label\n");
+ printf("\t-L use label from converted fs\n");
+ printf("\t-p show converting progress\n");
}
int main(int argc, char *argv[])
int noxattr = 0;
int datacsum = 1;
int rollback = 0;
+ int copylabel = 0;
+ int usage_error = 0;
+ int progress = 0;
char *file;
+ char *fslabel = NULL;
+
while(1) {
- int c = getopt(argc, argv, "dinr");
+ int c = getopt(argc, argv, "dinrl:Lp");
if (c < 0)
break;
switch(c) {
case 'r':
rollback = 1;
break;
+ case 'l':
+ copylabel = -1;
+ fslabel = strdup(optarg);
+ if (strlen(fslabel) > BTRFS_LABEL_SIZE) {
+ fprintf(stderr,
+ "warning: label too long, trimmed to %d bytes\n",
+ BTRFS_LABEL_SIZE);
+ fslabel[BTRFS_LABEL_SIZE] = 0;
+ }
+ break;
+ case 'L':
+ copylabel = 1;
+ break;
+ case 'p':
+ progress = 1;
+ break;
default:
print_usage();
return 1;
}
}
argc = argc - optind;
- if (argc != 1) {
+ set_argv0(argv);
+ if (check_argc_exact(argc, 1)) {
+ print_usage();
+ return 1;
+ }
+
+ if (rollback && (!datacsum || noxattr || !packing)) {
+ fprintf(stderr,
+ "Usage error: -d, -i, -n options do not apply to rollback\n");
+ usage_error++;
+ }
+
+ if (usage_error) {
print_usage();
return 1;
}
file = argv[optind];
- if (check_mounted(file)) {
+ ret = check_mounted(file);
+ if (ret < 0) {
+ fprintf(stderr, "Could not check mount status: %s\n",
+ strerror(-ret));
+ return 1;
+ } else if (ret) {
fprintf(stderr, "%s is mounted\n", file);
return 1;
}
if (rollback) {
- ret = do_rollback(file, 0);
+ ret = do_rollback(file);
} else {
- ret = do_convert(file, datacsum, packing, noxattr);
+ ret = do_convert(file, datacsum, packing, noxattr, copylabel, fslabel, progress);
}
if (ret)
return 1;