2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include "kerncompat.h"
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
25 #include <sys/types.h>
29 #include <uuid/uuid.h>
30 #include <linux/limits.h>
36 #include "transaction.h"
39 #include "task-utils.h"
40 #include <ext2fs/ext2_fs.h>
41 #include <ext2fs/ext2fs.h>
42 #include <ext2fs/ext2_ext_attr.h>
44 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
45 #define CONV_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
48 * Compatibility code for e2fsprogs 1.41 which doesn't support RO compat flag
50 * Unlike normal RO compat flag, BIGALLOC affects how e2fsprogs check used
51 * space, and btrfs-convert heavily relies on it.
53 #ifdef HAVE_OLD_E2FSPROGS
54 #define EXT2FS_CLUSTER_RATIO(fs) (1)
55 #define EXT2_CLUSTERS_PER_GROUP(s) (EXT2_BLOCKS_PER_GROUP(s))
56 #define EXT2FS_B2C(fs, blk) (blk)
60 uint32_t max_copy_inodes;
61 uint32_t cur_copy_inodes;
62 struct task_info *info;
65 static void *print_copied_inodes(void *p)
67 struct task_ctx *priv = p;
68 const char work_indicator[] = { '.', 'o', 'O', 'o' };
71 task_period_start(priv->info, 1000 /* 1s */);
74 printf("copy inodes [%c] [%10d/%10d]\r",
75 work_indicator[count % 4], priv->cur_copy_inodes,
76 priv->max_copy_inodes);
78 task_period_wait(priv->info);
84 static int after_copied_inodes(void *p)
92 struct btrfs_convert_context;
93 struct btrfs_convert_operations {
95 int (*open_fs)(struct btrfs_convert_context *cctx, const char *devname);
96 int (*read_used_space)(struct btrfs_convert_context *cctx);
97 int (*alloc_block)(struct btrfs_convert_context *cctx, u64 goal,
99 int (*alloc_block_range)(struct btrfs_convert_context *cctx, u64 goal,
100 int num, u64 *block_ret);
101 int (*test_block)(struct btrfs_convert_context *cctx, u64 block);
102 void (*free_block)(struct btrfs_convert_context *cctx, u64 block);
103 void (*free_block_range)(struct btrfs_convert_context *cctx, u64 block,
105 int (*copy_inodes)(struct btrfs_convert_context *cctx,
106 struct btrfs_root *root, int datacsum,
107 int packing, int noxattr, struct task_ctx *p);
108 void (*close_fs)(struct btrfs_convert_context *cctx);
111 struct btrfs_convert_context {
113 u32 first_data_block;
116 u32 free_inodes_count;
119 const struct btrfs_convert_operations *convert_ops;
121 /* The accurate used space of old filesystem */
122 struct cache_tree used;
124 /* Batched ranges which must be covered by data chunks */
125 struct cache_tree data_chunks;
127 /* Free space which is not covered by data_chunks */
128 struct cache_tree free;
133 static void init_convert_context(struct btrfs_convert_context *cctx)
135 cache_tree_init(&cctx->used);
136 cache_tree_init(&cctx->data_chunks);
137 cache_tree_init(&cctx->free);
140 static void clean_convert_context(struct btrfs_convert_context *cctx)
142 free_extent_cache_tree(&cctx->used);
143 free_extent_cache_tree(&cctx->data_chunks);
144 free_extent_cache_tree(&cctx->free);
147 static inline int convert_alloc_block(struct btrfs_convert_context *cctx,
150 return cctx->convert_ops->alloc_block(cctx, goal, ret);
153 static inline int convert_alloc_block_range(struct btrfs_convert_context *cctx,
154 u64 goal, int num, u64 *ret)
156 return cctx->convert_ops->alloc_block_range(cctx, goal, num, ret);
159 static inline int convert_test_block(struct btrfs_convert_context *cctx,
162 return cctx->convert_ops->test_block(cctx, block);
165 static inline void convert_free_block(struct btrfs_convert_context *cctx,
168 cctx->convert_ops->free_block(cctx, block);
171 static inline void convert_free_block_range(struct btrfs_convert_context *cctx,
174 cctx->convert_ops->free_block_range(cctx, block, num);
177 static inline int copy_inodes(struct btrfs_convert_context *cctx,
178 struct btrfs_root *root, int datacsum,
179 int packing, int noxattr, struct task_ctx *p)
181 return cctx->convert_ops->copy_inodes(cctx, root, datacsum, packing,
185 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
187 cctx->convert_ops->close_fs(cctx);
191 * Open Ext2fs in readonly mode, read block allocation bitmap and
192 * inode bitmap into memory.
194 static int ext2_open_fs(struct btrfs_convert_context *cctx, const char *name)
201 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
203 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
207 * We need to know exactly the used space, some RO compat flags like
208 * BIGALLOC will affect how used space is present.
209 * So we need manuall check any unsupported RO compat flags
211 ro_feature = ext2_fs->super->s_feature_ro_compat;
212 if (ro_feature & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
214 "unsupported RO features detected: %x, abort convert to avoid possible corruption",
215 ro_feature & ~EXT2_LIB_FEATURE_COMPAT_SUPP);
218 ret = ext2fs_read_inode_bitmap(ext2_fs);
220 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
224 ret = ext2fs_read_block_bitmap(ext2_fs);
226 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
231 * search each block group for a free inode. this set up
232 * uninit block/inode bitmaps appropriately.
235 while (ino <= ext2_fs->super->s_inodes_count) {
237 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
238 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
241 if (!(ext2_fs->super->s_feature_incompat &
242 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
243 fprintf(stderr, "filetype feature is missing\n");
247 cctx->fs_data = ext2_fs;
248 cctx->blocksize = ext2_fs->blocksize;
249 cctx->block_count = ext2_fs->super->s_blocks_count;
250 cctx->total_bytes = ext2_fs->blocksize * ext2_fs->super->s_blocks_count;
251 cctx->volume_name = strndup(ext2_fs->super->s_volume_name, 16);
252 cctx->first_data_block = ext2_fs->super->s_first_data_block;
253 cctx->inodes_count = ext2_fs->super->s_inodes_count;
254 cctx->free_inodes_count = ext2_fs->super->s_free_inodes_count;
257 ext2fs_close(ext2_fs);
261 static int __ext2_add_one_block(ext2_filsys fs, char *bitmap,
262 unsigned long group_nr, struct cache_tree *used)
264 unsigned long offset;
268 offset = fs->super->s_first_data_block;
269 offset /= EXT2FS_CLUSTER_RATIO(fs);
270 offset += group_nr * EXT2_CLUSTERS_PER_GROUP(fs->super);
271 for (i = 0; i < EXT2_CLUSTERS_PER_GROUP(fs->super); i++) {
272 if (ext2fs_test_bit(i, bitmap)) {
275 start = (i + offset) * EXT2FS_CLUSTER_RATIO(fs);
276 start *= fs->blocksize;
277 ret = add_merge_cache_extent(used, start,
287 * Read all used ext2 space into cctx->used cache tree
289 static int ext2_read_used_space(struct btrfs_convert_context *cctx)
291 ext2_filsys fs = (ext2_filsys)cctx->fs_data;
292 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
293 struct cache_tree *used_tree = &cctx->used;
294 char *block_bitmap = NULL;
299 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
300 /* Shouldn't happen */
301 BUG_ON(!fs->block_map);
303 block_bitmap = malloc(block_nbytes);
307 for (i = 0; i < fs->group_desc_count; i++) {
308 ret = ext2fs_get_block_bitmap_range(fs->block_map, blk_itr,
309 block_nbytes * 8, block_bitmap);
311 error("fail to get bitmap from ext2, %s",
315 ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree);
317 error("fail to build used space tree, %s",
321 blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super);
328 static void ext2_close_fs(struct btrfs_convert_context *cctx)
330 if (cctx->volume_name) {
331 free(cctx->volume_name);
332 cctx->volume_name = NULL;
334 ext2fs_close(cctx->fs_data);
337 static int ext2_alloc_block(struct btrfs_convert_context *cctx,
338 u64 goal, u64 *block_ret)
340 ext2_filsys fs = cctx->fs_data;
343 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
344 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
351 static int ext2_alloc_block_range(struct btrfs_convert_context *cctx, u64 goal,
352 int num, u64 *block_ret)
354 ext2_filsys fs = cctx->fs_data;
356 ext2fs_block_bitmap bitmap = fs->block_map;
357 blk_t start = ext2fs_get_block_bitmap_start(bitmap);
358 blk_t end = ext2fs_get_block_bitmap_end(bitmap);
360 for (block = max_t(u64, goal, start); block + num < end; block++) {
361 if (ext2fs_fast_test_block_bitmap_range(bitmap, block, num)) {
362 ext2fs_fast_mark_block_bitmap_range(bitmap, block,
371 static void ext2_free_block(struct btrfs_convert_context *cctx, u64 block)
373 ext2_filsys fs = cctx->fs_data;
375 BUG_ON(block != (blk_t)block);
376 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
379 static void ext2_free_block_range(struct btrfs_convert_context *cctx, u64 block, int num)
381 ext2_filsys fs = cctx->fs_data;
383 BUG_ON(block != (blk_t)block);
384 ext2fs_fast_unmark_block_bitmap_range(fs->block_map, block, num);
387 static int cache_free_extents(struct btrfs_root *root,
388 struct btrfs_convert_context *cctx)
394 u64 blocksize = cctx->blocksize;
396 block = cctx->first_data_block;
397 for (; block < cctx->block_count; block++) {
398 if (convert_test_block(cctx, block))
400 bytenr = block * blocksize;
401 ret = set_extent_dirty(&root->fs_info->free_space_cache,
402 bytenr, bytenr + blocksize - 1, 0);
406 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
407 bytenr = btrfs_sb_offset(i);
408 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
409 if (bytenr >= blocksize * cctx->block_count)
411 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
412 bytenr + BTRFS_STRIPE_LEN - 1, 0);
415 clear_extent_dirty(&root->fs_info->free_space_cache,
416 0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
421 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
422 u64 hint_byte, struct btrfs_key *ins,
427 u64 last = hint_byte;
430 struct btrfs_block_group_cache *cache;
433 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
434 last, &start, &end, EXTENT_DIRTY);
436 if (wrapped++ == 0) {
444 start = max(last, start);
446 if (last - start < num_bytes)
449 last = start + num_bytes;
450 if (test_range_bit(&root->fs_info->pinned_extents,
451 start, last - 1, EXTENT_DIRTY, 0))
454 cache = btrfs_lookup_block_group(root->fs_info, start);
456 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
457 last > cache->key.objectid + cache->key.offset) {
458 last = cache->key.objectid + cache->key.offset;
463 BUG_ON(num_bytes != root->nodesize);
464 if (check_crossing_stripes(start, num_bytes)) {
465 last = round_down(start + num_bytes,
470 clear_extent_dirty(&root->fs_info->free_space_cache,
471 start, start + num_bytes - 1, 0);
473 ins->objectid = start;
474 ins->offset = num_bytes;
475 ins->type = BTRFS_EXTENT_ITEM_KEY;
479 fprintf(stderr, "not enough free space\n");
483 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
488 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
489 offset = btrfs_sb_offset(i);
490 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
492 if (bytenr < offset + BTRFS_STRIPE_LEN &&
493 bytenr + num_bytes > offset)
499 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
502 return intersect_with_sb(bytenr, num_bytes);
505 static struct btrfs_extent_ops extent_ops = {
506 .alloc_extent = custom_alloc_extent,
507 .free_extent = custom_free_extent,
510 static int convert_insert_dirent(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root,
512 const char *name, size_t name_len,
513 u64 dir, u64 objectid,
514 u8 file_type, u64 index_cnt,
515 struct btrfs_inode_item *inode)
519 struct btrfs_key location = {
520 .objectid = objectid,
522 .type = BTRFS_INODE_ITEM_KEY,
525 ret = btrfs_insert_dir_item(trans, root, name, name_len,
526 dir, &location, file_type, index_cnt);
529 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
530 objectid, dir, index_cnt);
533 inode_size = btrfs_stack_inode_size(inode) + name_len * 2;
534 btrfs_set_stack_inode_size(inode, inode_size);
539 struct dir_iterate_data {
540 struct btrfs_trans_handle *trans;
541 struct btrfs_root *root;
542 struct btrfs_inode_item *inode;
549 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
550 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
551 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
552 [EXT2_FT_DIR] = BTRFS_FT_DIR,
553 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
554 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
555 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
556 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
557 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
560 static int dir_iterate_proc(ext2_ino_t dir, int entry,
561 struct ext2_dir_entry *dirent,
562 int offset, int blocksize,
563 char *buf,void *priv_data)
568 char dotdot[] = "..";
569 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
572 name_len = dirent->name_len & 0xFF;
574 objectid = dirent->inode + INO_OFFSET;
575 if (!strncmp(dirent->name, dotdot, name_len)) {
577 BUG_ON(idata->parent != 0);
578 idata->parent = objectid;
582 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
585 file_type = dirent->name_len >> 8;
586 BUG_ON(file_type > EXT2_FT_SYMLINK);
588 ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
589 name_len, idata->objectid, objectid,
590 filetype_conversion_table[file_type],
591 idata->index_cnt, idata->inode);
593 idata->errcode = ret;
601 static int create_dir_entries(struct btrfs_trans_handle *trans,
602 struct btrfs_root *root, u64 objectid,
603 struct btrfs_inode_item *btrfs_inode,
604 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
608 struct dir_iterate_data data = {
611 .inode = btrfs_inode,
612 .objectid = objectid,
618 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
619 dir_iterate_proc, &data);
623 if (ret == 0 && data.parent == objectid) {
624 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
625 objectid, objectid, 0);
629 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
633 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
634 u32 num_bytes, char *buffer)
637 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
639 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
640 if (ret != num_bytes)
649 static int csum_disk_extent(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root,
651 u64 disk_bytenr, u64 num_bytes)
653 u32 blocksize = root->sectorsize;
658 buffer = malloc(blocksize);
661 for (offset = 0; offset < num_bytes; offset += blocksize) {
662 ret = read_disk_extent(root, disk_bytenr + offset,
666 ret = btrfs_csum_file_block(trans,
667 root->fs_info->csum_root,
668 disk_bytenr + num_bytes,
669 disk_bytenr + offset,
678 struct blk_iterate_data {
679 struct btrfs_trans_handle *trans;
680 struct btrfs_root *root;
681 struct btrfs_inode_item *inode;
691 static void init_blk_iterate_data(struct blk_iterate_data *data,
692 struct btrfs_trans_handle *trans,
693 struct btrfs_root *root,
694 struct btrfs_inode_item *inode,
695 u64 objectid, int checksum)
700 data->objectid = objectid;
701 data->first_block = 0;
702 data->disk_block = 0;
703 data->num_blocks = 0;
704 data->boundary = (u64)-1;
705 data->checksum = checksum;
709 static int record_file_blocks(struct blk_iterate_data *data,
710 u64 file_block, u64 disk_block, u64 num_blocks)
713 struct btrfs_root *root = data->root;
714 u64 file_pos = file_block * root->sectorsize;
715 u64 disk_bytenr = disk_block * root->sectorsize;
716 u64 num_bytes = num_blocks * root->sectorsize;
717 ret = btrfs_record_file_extent(data->trans, data->root,
718 data->objectid, data->inode, file_pos,
719 disk_bytenr, num_bytes);
721 if (ret || !data->checksum || disk_bytenr == 0)
724 return csum_disk_extent(data->trans, data->root, disk_bytenr,
728 static int block_iterate_proc(u64 disk_block, u64 file_block,
729 struct blk_iterate_data *idata)
734 struct btrfs_root *root = idata->root;
735 struct btrfs_block_group_cache *cache;
736 u64 bytenr = disk_block * root->sectorsize;
738 sb_region = intersect_with_sb(bytenr, root->sectorsize);
739 do_barrier = sb_region || disk_block >= idata->boundary;
740 if ((idata->num_blocks > 0 && do_barrier) ||
741 (file_block > idata->first_block + idata->num_blocks) ||
742 (disk_block != idata->disk_block + idata->num_blocks)) {
743 if (idata->num_blocks > 0) {
744 ret = record_file_blocks(idata, idata->first_block,
749 idata->first_block += idata->num_blocks;
750 idata->num_blocks = 0;
752 if (file_block > idata->first_block) {
753 ret = record_file_blocks(idata, idata->first_block,
754 0, file_block - idata->first_block);
760 bytenr += BTRFS_STRIPE_LEN - 1;
761 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
763 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
765 bytenr = cache->key.objectid + cache->key.offset;
768 idata->first_block = file_block;
769 idata->disk_block = disk_block;
770 idata->boundary = bytenr / root->sectorsize;
777 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
778 e2_blkcnt_t blockcnt, blk_t ref_block,
779 int ref_offset, void *priv_data)
782 struct blk_iterate_data *idata;
783 idata = (struct blk_iterate_data *)priv_data;
784 ret = block_iterate_proc(*blocknr, blockcnt, idata);
786 idata->errcode = ret;
793 * traverse file's data blocks, record these data blocks as file extents.
795 static int create_file_extents(struct btrfs_trans_handle *trans,
796 struct btrfs_root *root, u64 objectid,
797 struct btrfs_inode_item *btrfs_inode,
798 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
799 int datacsum, int packing)
805 u32 sectorsize = root->sectorsize;
806 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
807 struct blk_iterate_data data;
809 init_blk_iterate_data(&data, trans, root, btrfs_inode, objectid,
812 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
813 NULL, __block_iterate_proc, &data);
819 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
820 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
821 u64 num_bytes = data.num_blocks * sectorsize;
822 u64 disk_bytenr = data.disk_block * sectorsize;
825 buffer = malloc(num_bytes);
828 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
831 if (num_bytes > inode_size)
832 num_bytes = inode_size;
833 ret = btrfs_insert_inline_extent(trans, root, objectid,
834 0, buffer, num_bytes);
837 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
838 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
839 } else if (data.num_blocks > 0) {
840 ret = record_file_blocks(&data, data.first_block,
841 data.disk_block, data.num_blocks);
845 data.first_block += data.num_blocks;
846 last_block = (inode_size + sectorsize - 1) / sectorsize;
847 if (last_block > data.first_block) {
848 ret = record_file_blocks(&data, data.first_block, 0,
849 last_block - data.first_block);
855 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
859 static int create_symbol_link(struct btrfs_trans_handle *trans,
860 struct btrfs_root *root, u64 objectid,
861 struct btrfs_inode_item *btrfs_inode,
862 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
863 struct ext2_inode *ext2_inode)
867 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
868 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
869 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
870 ret = create_file_extents(trans, root, objectid, btrfs_inode,
871 ext2_fs, ext2_ino, 1, 1);
872 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
876 pathname = (char *)&(ext2_inode->i_block[0]);
877 BUG_ON(pathname[inode_size] != 0);
878 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
879 pathname, inode_size + 1);
880 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
885 * Following xattr/acl related codes are based on codes in
886 * fs/ext3/xattr.c and fs/ext3/acl.c
888 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
889 #define EXT2_XATTR_BFIRST(ptr) \
890 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
891 #define EXT2_XATTR_IHDR(inode) \
892 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
893 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
894 #define EXT2_XATTR_IFIRST(inode) \
895 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
896 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
898 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
901 struct ext2_ext_attr_entry *next;
903 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
904 next = EXT2_EXT_ATTR_NEXT(entry);
905 if ((void *)next >= end)
912 static int ext2_xattr_check_block(const char *buf, size_t size)
915 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
917 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
918 header->h_blocks != 1)
920 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
924 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
927 size_t value_size = entry->e_value_size;
929 if (entry->e_value_block != 0 || value_size > size ||
930 entry->e_value_offs + value_size > size)
935 #define EXT2_ACL_VERSION 0x0001
937 /* 23.2.5 acl_tag_t values */
939 #define ACL_UNDEFINED_TAG (0x00)
940 #define ACL_USER_OBJ (0x01)
941 #define ACL_USER (0x02)
942 #define ACL_GROUP_OBJ (0x04)
943 #define ACL_GROUP (0x08)
944 #define ACL_MASK (0x10)
945 #define ACL_OTHER (0x20)
947 /* 23.2.7 ACL qualifier constants */
949 #define ACL_UNDEFINED_ID ((id_t)-1)
960 } ext2_acl_entry_short;
966 static inline int ext2_acl_count(size_t size)
969 size -= sizeof(ext2_acl_header);
970 s = size - 4 * sizeof(ext2_acl_entry_short);
972 if (size % sizeof(ext2_acl_entry_short))
974 return size / sizeof(ext2_acl_entry_short);
976 if (s % sizeof(ext2_acl_entry))
978 return s / sizeof(ext2_acl_entry) + 4;
982 #define ACL_EA_VERSION 0x0002
992 acl_ea_entry a_entries[0];
995 static inline size_t acl_ea_size(int count)
997 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
1000 static int ext2_acl_to_xattr(void *dst, const void *src,
1001 size_t dst_size, size_t src_size)
1004 const void *end = src + src_size;
1005 acl_ea_header *ext_acl = (acl_ea_header *)dst;
1006 acl_ea_entry *dst_entry = ext_acl->a_entries;
1007 ext2_acl_entry *src_entry;
1009 if (src_size < sizeof(ext2_acl_header))
1011 if (((ext2_acl_header *)src)->a_version !=
1012 cpu_to_le32(EXT2_ACL_VERSION))
1014 src += sizeof(ext2_acl_header);
1015 count = ext2_acl_count(src_size);
1019 BUG_ON(dst_size < acl_ea_size(count));
1020 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
1021 for (i = 0; i < count; i++, dst_entry++) {
1022 src_entry = (ext2_acl_entry *)src;
1023 if (src + sizeof(ext2_acl_entry_short) > end)
1025 dst_entry->e_tag = src_entry->e_tag;
1026 dst_entry->e_perm = src_entry->e_perm;
1027 switch (le16_to_cpu(src_entry->e_tag)) {
1032 src += sizeof(ext2_acl_entry_short);
1033 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1037 src += sizeof(ext2_acl_entry);
1040 dst_entry->e_id = src_entry->e_id;
1053 static char *xattr_prefix_table[] = {
1055 [2] = "system.posix_acl_access",
1056 [3] = "system.posix_acl_default",
1061 static int copy_single_xattr(struct btrfs_trans_handle *trans,
1062 struct btrfs_root *root, u64 objectid,
1063 struct ext2_ext_attr_entry *entry,
1064 const void *data, u32 datalen)
1069 void *databuf = NULL;
1070 char namebuf[XATTR_NAME_MAX + 1];
1072 name_index = entry->e_name_index;
1073 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
1074 xattr_prefix_table[name_index] == NULL)
1076 name_len = strlen(xattr_prefix_table[name_index]) +
1078 if (name_len >= sizeof(namebuf))
1081 if (name_index == 2 || name_index == 3) {
1082 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
1083 databuf = malloc(bufsize);
1086 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
1092 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
1093 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
1094 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
1095 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
1096 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
1097 objectid - INO_OFFSET, name_len, namebuf);
1100 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
1101 data, datalen, objectid);
1107 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
1108 struct btrfs_root *root, u64 objectid,
1109 struct btrfs_inode_item *btrfs_inode,
1110 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
1116 u32 block_size = ext2_fs->blocksize;
1117 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
1118 struct ext2_inode_large *ext2_inode;
1119 struct ext2_ext_attr_entry *entry;
1121 char *buffer = NULL;
1122 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
1124 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
1125 ext2_inode = (struct ext2_inode_large *)inode_buf;
1127 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
1131 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
1134 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
1135 error_message(err));
1140 if (ext2_ino > ext2_fs->super->s_first_ino &&
1141 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1142 if (EXT2_GOOD_OLD_INODE_SIZE +
1143 ext2_inode->i_extra_isize > inode_size) {
1147 if (ext2_inode->i_extra_isize != 0 &&
1148 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
1149 EXT2_EXT_ATTR_MAGIC) {
1155 void *end = (void *)ext2_inode + inode_size;
1156 entry = EXT2_XATTR_IFIRST(ext2_inode);
1157 total = end - (void *)entry;
1158 ret = ext2_xattr_check_names(entry, end);
1161 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1162 ret = ext2_xattr_check_entry(entry, total);
1165 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
1166 entry->e_value_offs;
1167 datalen = entry->e_value_size;
1168 ret = copy_single_xattr(trans, root, objectid,
1169 entry, data, datalen);
1172 entry = EXT2_EXT_ATTR_NEXT(entry);
1176 if (ext2_inode->i_file_acl == 0)
1179 buffer = malloc(block_size);
1184 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
1186 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
1187 error_message(err));
1191 ret = ext2_xattr_check_block(buffer, block_size);
1195 entry = EXT2_XATTR_BFIRST(buffer);
1196 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1197 ret = ext2_xattr_check_entry(entry, block_size);
1200 data = buffer + entry->e_value_offs;
1201 datalen = entry->e_value_size;
1202 ret = copy_single_xattr(trans, root, objectid,
1203 entry, data, datalen);
1206 entry = EXT2_EXT_ATTR_NEXT(entry);
1210 if ((void *)ext2_inode != inode_buf)
1214 #define MINORBITS 20
1215 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
1217 static inline dev_t old_decode_dev(u16 val)
1219 return MKDEV((val >> 8) & 255, val & 255);
1222 static inline dev_t new_decode_dev(u32 dev)
1224 unsigned major = (dev & 0xfff00) >> 8;
1225 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
1226 return MKDEV(major, minor);
1229 static int copy_inode_item(struct btrfs_inode_item *dst,
1230 struct ext2_inode *src, u32 blocksize)
1232 btrfs_set_stack_inode_generation(dst, 1);
1233 btrfs_set_stack_inode_sequence(dst, 0);
1234 btrfs_set_stack_inode_transid(dst, 1);
1235 btrfs_set_stack_inode_size(dst, src->i_size);
1236 btrfs_set_stack_inode_nbytes(dst, 0);
1237 btrfs_set_stack_inode_block_group(dst, 0);
1238 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
1239 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
1240 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
1241 btrfs_set_stack_inode_mode(dst, src->i_mode);
1242 btrfs_set_stack_inode_rdev(dst, 0);
1243 btrfs_set_stack_inode_flags(dst, 0);
1244 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
1245 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
1246 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
1247 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
1248 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
1249 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
1250 btrfs_set_stack_timespec_sec(&dst->otime, 0);
1251 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
1253 if (S_ISDIR(src->i_mode)) {
1254 btrfs_set_stack_inode_size(dst, 0);
1255 btrfs_set_stack_inode_nlink(dst, 1);
1257 if (S_ISREG(src->i_mode)) {
1258 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1261 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1262 !S_ISLNK(src->i_mode)) {
1263 if (src->i_block[0]) {
1264 btrfs_set_stack_inode_rdev(dst,
1265 old_decode_dev(src->i_block[0]));
1267 btrfs_set_stack_inode_rdev(dst,
1268 new_decode_dev(src->i_block[1]));
1271 memset(&dst->reserved, 0, sizeof(dst->reserved));
1277 * copy a single inode. do all the required works, such as cloning
1278 * inode item, creating file extents and creating directory entries.
1280 static int copy_single_inode(struct btrfs_trans_handle *trans,
1281 struct btrfs_root *root, u64 objectid,
1282 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1283 struct ext2_inode *ext2_inode,
1284 int datacsum, int packing, int noxattr)
1287 struct btrfs_inode_item btrfs_inode;
1289 if (ext2_inode->i_links_count == 0)
1292 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1293 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1294 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1295 BTRFS_INODE_NODATASUM;
1296 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1299 switch (ext2_inode->i_mode & S_IFMT) {
1301 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1302 ext2_fs, ext2_ino, datacsum, packing);
1305 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1309 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1310 ext2_fs, ext2_ino, ext2_inode);
1320 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1325 return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1328 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1329 u64 src_bytenr, u32 num_bytes)
1333 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1335 buffer = malloc(num_bytes);
1338 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1339 if (ret != num_bytes)
1341 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1342 if (ret != num_bytes)
1352 * scan ext2's inode bitmap and copy all used inodes.
1354 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
1355 struct btrfs_root *root,
1356 int datacsum, int packing, int noxattr, struct task_ctx *p)
1358 ext2_filsys ext2_fs = cctx->fs_data;
1361 ext2_inode_scan ext2_scan;
1362 struct ext2_inode ext2_inode;
1363 ext2_ino_t ext2_ino;
1365 struct btrfs_trans_handle *trans;
1367 trans = btrfs_start_transaction(root, 1);
1370 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1372 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1375 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1377 /* no more inodes */
1380 /* skip special inode in ext2fs */
1381 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1382 ext2_ino != EXT2_ROOT_INO)
1384 objectid = ext2_ino + INO_OFFSET;
1385 ret = copy_single_inode(trans, root,
1386 objectid, ext2_fs, ext2_ino,
1387 &ext2_inode, datacsum, packing,
1389 p->cur_copy_inodes++;
1392 if (trans->blocks_used >= 4096) {
1393 ret = btrfs_commit_transaction(trans, root);
1395 trans = btrfs_start_transaction(root, 1);
1400 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1403 ret = btrfs_commit_transaction(trans, root);
1405 ext2fs_close_inode_scan(ext2_scan);
1410 static int ext2_test_block(struct btrfs_convert_context *cctx, u64 block)
1412 ext2_filsys ext2_fs = cctx->fs_data;
1414 BUG_ON(block != (u32)block);
1415 return ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block);
1419 * Construct a range of ext2fs image file.
1420 * scan block allocation bitmap, find all blocks used by the ext2fs
1421 * in this range and create file extents that point to these blocks.
1423 * Note: Before calling the function, no file extent points to blocks
1426 static int create_image_file_range(struct btrfs_trans_handle *trans,
1427 struct btrfs_root *root, u64 objectid,
1428 struct btrfs_inode_item *inode,
1429 u64 start_byte, u64 end_byte,
1430 struct btrfs_convert_context *cctx, int datacsum)
1432 u32 blocksize = cctx->blocksize;
1433 u32 block = start_byte / blocksize;
1434 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1436 struct blk_iterate_data data;
1438 init_blk_iterate_data(&data, trans, root, inode, objectid, datacsum);
1439 data.first_block = block;
1441 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1442 if (!convert_test_block(cctx, block))
1444 ret = block_iterate_proc(block, block, &data);
1448 if (data.num_blocks > 0) {
1449 ret = record_file_blocks(&data, data.first_block,
1450 data.disk_block, data.num_blocks);
1453 data.first_block += data.num_blocks;
1455 if (last_block > data.first_block) {
1456 ret = record_file_blocks(&data, data.first_block, 0,
1457 last_block - data.first_block);
1465 * Create the fs image file.
1467 static int create_image(struct btrfs_convert_context *cctx,
1468 struct btrfs_root *root, const char *name, int datacsum)
1471 struct btrfs_key key;
1472 struct btrfs_key location;
1473 struct btrfs_path path;
1474 struct btrfs_inode_item btrfs_inode;
1475 struct btrfs_inode_item *inode_item;
1476 struct extent_buffer *leaf;
1477 struct btrfs_fs_info *fs_info = root->fs_info;
1478 struct btrfs_root *extent_root = fs_info->extent_root;
1479 struct btrfs_trans_handle *trans;
1480 struct btrfs_extent_item *ei;
1481 struct btrfs_extent_inline_ref *iref;
1482 struct btrfs_extent_data_ref *dref;
1489 u64 flags = BTRFS_INODE_READONLY;
1490 u32 sectorsize = root->sectorsize;
1492 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1493 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1494 first_free &= ~((u64)sectorsize - 1);
1496 flags |= BTRFS_INODE_NODATASUM;
1498 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1499 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1500 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1501 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1502 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1503 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1504 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1505 btrfs_init_path(&path);
1506 trans = btrfs_start_transaction(root, 1);
1509 objectid = btrfs_root_dirid(&root->root_item);
1510 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1515 * copy blocks covered by extent #0 to new positions. extent #0 is
1516 * special, we can't rely on relocate_extents_range to relocate it.
1518 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1519 ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
1522 ret = copy_disk_extent(root, key.objectid, last_byte,
1526 ret = btrfs_record_file_extent(trans, root, objectid,
1527 &btrfs_inode, last_byte,
1528 key.objectid, sectorsize);
1532 ret = csum_disk_extent(trans, root, key.objectid,
1540 key.objectid = last_byte;
1542 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1543 ret = btrfs_search_slot(trans, fs_info->extent_root,
1548 leaf = path.nodes[0];
1549 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1550 ret = btrfs_next_leaf(extent_root, &path);
1555 leaf = path.nodes[0];
1557 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1558 if (last_byte > key.objectid ||
1559 key.type != BTRFS_EXTENT_ITEM_KEY) {
1564 bytenr = key.objectid;
1565 num_bytes = key.offset;
1566 ei = btrfs_item_ptr(leaf, path.slots[0],
1567 struct btrfs_extent_item);
1568 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1573 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1574 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1576 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1577 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1578 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1579 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1580 if (btrfs_extent_data_ref_root(leaf, dref) !=
1581 BTRFS_FS_TREE_OBJECTID) {
1586 if (bytenr > last_byte) {
1587 ret = create_image_file_range(trans, root, objectid,
1588 &btrfs_inode, last_byte,
1594 ret = btrfs_record_file_extent(trans, root, objectid,
1595 &btrfs_inode, bytenr, bytenr,
1599 last_byte = bytenr + num_bytes;
1600 btrfs_release_path(&path);
1602 if (trans->blocks_used >= 4096) {
1603 ret = btrfs_commit_transaction(trans, root);
1605 trans = btrfs_start_transaction(root, 1);
1609 btrfs_release_path(&path);
1610 if (total_bytes > last_byte) {
1611 ret = create_image_file_range(trans, root, objectid,
1612 &btrfs_inode, last_byte,
1619 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1623 location.objectid = objectid;
1624 location.offset = 0;
1625 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1626 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1627 btrfs_root_dirid(&root->root_item),
1628 &location, BTRFS_FT_REG_FILE, objectid);
1631 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1633 btrfs_root_dirid(&root->root_item),
1637 location.objectid = btrfs_root_dirid(&root->root_item);
1638 location.offset = 0;
1639 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1640 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1643 leaf = path.nodes[0];
1644 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1645 struct btrfs_inode_item);
1646 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1647 btrfs_inode_size(leaf, inode_item));
1648 btrfs_mark_buffer_dirty(leaf);
1649 btrfs_release_path(&path);
1650 ret = btrfs_commit_transaction(trans, root);
1653 btrfs_release_path(&path);
1657 static struct btrfs_root * link_subvol(struct btrfs_root *root,
1658 const char *base, u64 root_objectid)
1660 struct btrfs_trans_handle *trans;
1661 struct btrfs_fs_info *fs_info = root->fs_info;
1662 struct btrfs_root *tree_root = fs_info->tree_root;
1663 struct btrfs_root *new_root = NULL;
1664 struct btrfs_path *path;
1665 struct btrfs_inode_item *inode_item;
1666 struct extent_buffer *leaf;
1667 struct btrfs_key key;
1668 u64 dirid = btrfs_root_dirid(&root->root_item);
1670 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
1676 if (len == 0 || len > BTRFS_NAME_LEN)
1679 path = btrfs_alloc_path();
1682 key.objectid = dirid;
1683 key.type = BTRFS_DIR_INDEX_KEY;
1684 key.offset = (u64)-1;
1686 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1689 if (path->slots[0] > 0) {
1691 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1692 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1693 index = key.offset + 1;
1695 btrfs_release_path(path);
1697 trans = btrfs_start_transaction(root, 1);
1700 key.objectid = dirid;
1702 key.type = BTRFS_INODE_ITEM_KEY;
1704 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1706 leaf = path->nodes[0];
1707 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1708 struct btrfs_inode_item);
1710 key.objectid = root_objectid;
1711 key.offset = (u64)-1;
1712 key.type = BTRFS_ROOT_ITEM_KEY;
1714 memcpy(buf, base, len);
1715 for (i = 0; i < 1024; i++) {
1716 ret = btrfs_insert_dir_item(trans, root, buf, len,
1717 dirid, &key, BTRFS_FT_DIR, index);
1720 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
1721 if (len < 1 || len > BTRFS_NAME_LEN) {
1729 btrfs_set_inode_size(leaf, inode_item, len * 2 +
1730 btrfs_inode_size(leaf, inode_item));
1731 btrfs_mark_buffer_dirty(leaf);
1732 btrfs_release_path(path);
1734 /* add the backref first */
1735 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1736 BTRFS_ROOT_BACKREF_KEY,
1737 root->root_key.objectid,
1738 dirid, index, buf, len);
1741 /* now add the forward ref */
1742 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1743 BTRFS_ROOT_REF_KEY, root_objectid,
1744 dirid, index, buf, len);
1746 ret = btrfs_commit_transaction(trans, root);
1749 new_root = btrfs_read_fs_root(fs_info, &key);
1750 if (IS_ERR(new_root))
1753 btrfs_free_path(path);
1757 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1758 struct btrfs_root *root)
1760 struct btrfs_fs_info *info = root->fs_info;
1761 struct btrfs_root *chunk_root = info->chunk_root;
1762 struct btrfs_root *extent_root = info->extent_root;
1763 struct btrfs_device *device;
1764 struct btrfs_block_group_cache *cache;
1765 struct btrfs_dev_extent *extent;
1766 struct extent_buffer *leaf;
1767 struct btrfs_chunk chunk;
1768 struct btrfs_key key;
1769 struct btrfs_path path;
1775 btrfs_init_path(&path);
1777 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1778 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1780 BUG_ON(list_empty(&info->fs_devices->devices));
1781 device = list_entry(info->fs_devices->devices.next,
1782 struct btrfs_device, dev_list);
1783 BUG_ON(device->devid != info->fs_devices->latest_devid);
1785 /* delete device extent created by make_btrfs */
1786 key.objectid = device->devid;
1788 key.type = BTRFS_DEV_EXTENT_KEY;
1789 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1794 ret = btrfs_del_item(trans, device->dev_root, &path);
1797 btrfs_release_path(&path);
1799 /* delete chunk item created by make_btrfs */
1800 key.objectid = chunk_objectid;
1802 key.type = BTRFS_CHUNK_ITEM_KEY;
1803 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1808 ret = btrfs_del_item(trans, chunk_root, &path);
1811 btrfs_release_path(&path);
1813 /* for each block group, create device extent and chunk item */
1815 while (cur_start < total_bytes) {
1816 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1819 /* insert device extent */
1820 key.objectid = device->devid;
1821 key.offset = cache->key.objectid;
1822 key.type = BTRFS_DEV_EXTENT_KEY;
1823 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1824 &key, sizeof(*extent));
1828 leaf = path.nodes[0];
1829 extent = btrfs_item_ptr(leaf, path.slots[0],
1830 struct btrfs_dev_extent);
1832 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1833 chunk_root->root_key.objectid);
1834 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1836 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1837 cache->key.objectid);
1838 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1839 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1840 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1842 btrfs_mark_buffer_dirty(leaf);
1843 btrfs_release_path(&path);
1845 /* insert chunk item */
1846 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1847 btrfs_set_stack_chunk_owner(&chunk,
1848 extent_root->root_key.objectid);
1849 btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
1850 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1851 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1852 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1853 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1854 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1855 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1856 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1857 btrfs_set_stack_stripe_offset(&chunk.stripe,
1858 cache->key.objectid);
1859 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1861 key.objectid = chunk_objectid;
1862 key.offset = cache->key.objectid;
1863 key.type = BTRFS_CHUNK_ITEM_KEY;
1865 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1866 btrfs_chunk_item_size(1));
1870 cur_start = cache->key.objectid + cache->key.offset;
1873 device->bytes_used = total_bytes;
1874 ret = btrfs_update_device(trans, device);
1876 btrfs_release_path(&path);
1880 static int create_subvol(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *root, u64 root_objectid)
1883 struct extent_buffer *tmp;
1884 struct btrfs_root *new_root;
1885 struct btrfs_key key;
1886 struct btrfs_root_item root_item;
1889 ret = btrfs_copy_root(trans, root, root->node, &tmp,
1893 memcpy(&root_item, &root->root_item, sizeof(root_item));
1894 btrfs_set_root_bytenr(&root_item, tmp->start);
1895 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1896 btrfs_set_root_generation(&root_item, trans->transid);
1897 free_extent_buffer(tmp);
1899 key.objectid = root_objectid;
1900 key.type = BTRFS_ROOT_ITEM_KEY;
1901 key.offset = trans->transid;
1902 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1905 key.offset = (u64)-1;
1906 new_root = btrfs_read_fs_root(root->fs_info, &key);
1907 BUG_ON(!new_root || IS_ERR(new_root));
1909 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1915 static int init_btrfs(struct btrfs_root *root)
1918 struct btrfs_key location;
1919 struct btrfs_trans_handle *trans;
1920 struct btrfs_fs_info *fs_info = root->fs_info;
1921 struct extent_buffer *tmp;
1923 trans = btrfs_start_transaction(root, 1);
1925 ret = btrfs_make_block_groups(trans, root);
1928 ret = btrfs_fix_block_accounting(trans, root);
1931 ret = create_chunk_mapping(trans, root);
1934 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1935 BTRFS_ROOT_TREE_DIR_OBJECTID);
1938 memcpy(&location, &root->root_key, sizeof(location));
1939 location.offset = (u64)-1;
1940 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1941 btrfs_super_root_dir(fs_info->super_copy),
1942 &location, BTRFS_FT_DIR, 0);
1945 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1947 btrfs_super_root_dir(fs_info->super_copy), 0);
1950 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1951 BTRFS_FIRST_FREE_OBJECTID);
1953 /* subvol for fs image file */
1954 ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
1956 /* subvol for data relocation */
1957 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1960 extent_buffer_get(fs_info->csum_root->node);
1961 ret = __btrfs_cow_block(trans, fs_info->csum_root,
1962 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1964 free_extent_buffer(tmp);
1966 ret = btrfs_commit_transaction(trans, root);
1973 * Migrate super block to its default position and zero 0 ~ 16k
1975 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1978 struct extent_buffer *buf;
1979 struct btrfs_super_block *super;
1983 BUG_ON(sectorsize < sizeof(*super));
1984 buf = malloc(sizeof(*buf) + sectorsize);
1988 buf->len = sectorsize;
1989 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1990 if (ret != sectorsize)
1993 super = (struct btrfs_super_block *)buf->data;
1994 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1995 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1997 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1998 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1999 if (ret != sectorsize)
2006 memset(buf->data, 0, sectorsize);
2007 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
2008 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
2009 if (len > sectorsize)
2011 ret = pwrite(fd, buf->data, len, bytenr);
2013 fprintf(stderr, "unable to zero fill device\n");
2027 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
2029 struct btrfs_chunk *chunk;
2030 struct btrfs_disk_key *key;
2031 u32 sectorsize = btrfs_super_sectorsize(super);
2033 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
2034 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
2035 sizeof(struct btrfs_disk_key));
2037 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2038 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
2039 btrfs_set_disk_key_offset(key, 0);
2041 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
2042 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
2043 btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
2044 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
2045 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
2046 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
2047 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
2048 btrfs_set_stack_chunk_num_stripes(chunk, 1);
2049 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
2050 chunk->stripe.devid = super->dev_item.devid;
2051 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
2052 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
2053 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
2057 static int prepare_system_chunk(int fd, u64 sb_bytenr)
2060 struct extent_buffer *buf;
2061 struct btrfs_super_block *super;
2063 BUG_ON(BTRFS_SUPER_INFO_SIZE < sizeof(*super));
2064 buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
2068 buf->len = BTRFS_SUPER_INFO_SIZE;
2069 ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2070 if (ret != BTRFS_SUPER_INFO_SIZE)
2073 super = (struct btrfs_super_block *)buf->data;
2074 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
2075 BUG_ON(btrfs_super_num_devices(super) != 1);
2077 ret = prepare_system_chunk_sb(super);
2081 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2082 ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2083 if (ret != BTRFS_SUPER_INFO_SIZE)
2094 static int relocate_one_reference(struct btrfs_trans_handle *trans,
2095 struct btrfs_root *root,
2096 u64 extent_start, u64 extent_size,
2097 struct btrfs_key *extent_key,
2098 struct extent_io_tree *reloc_tree)
2100 struct extent_buffer *leaf;
2101 struct btrfs_file_extent_item *fi;
2102 struct btrfs_key key;
2103 struct btrfs_path path;
2104 struct btrfs_inode_item inode;
2105 struct blk_iterate_data data;
2112 u32 sectorsize = root->sectorsize;
2118 btrfs_init_path(&path);
2119 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
2123 leaf = path.nodes[0];
2124 fi = btrfs_item_ptr(leaf, path.slots[0],
2125 struct btrfs_file_extent_item);
2126 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
2127 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
2128 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
2133 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
2134 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2136 ret = btrfs_del_item(trans, root, &path);
2140 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
2141 root->root_key.objectid,
2142 extent_key->objectid, extent_key->offset);
2146 btrfs_release_path(&path);
2148 key.objectid = extent_key->objectid;
2150 key.type = BTRFS_INODE_ITEM_KEY;
2151 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
2155 leaf = path.nodes[0];
2156 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2157 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2158 btrfs_release_path(&path);
2160 BUG_ON(num_bytes & (sectorsize - 1));
2161 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
2162 btrfs_set_stack_inode_nbytes(&inode, nbytes);
2163 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
2165 init_blk_iterate_data(&data, trans, root, &inode, extent_key->objectid,
2167 data.first_block = extent_key->offset;
2169 cur_offset = extent_key->offset;
2170 while (num_bytes > 0) {
2171 sector_end = bytenr + sectorsize - 1;
2172 if (test_range_bit(reloc_tree, bytenr, sector_end,
2173 EXTENT_LOCKED, 1)) {
2174 ret = get_state_private(reloc_tree, bytenr, &new_pos);
2177 ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
2180 new_pos = key.objectid;
2182 if (cur_offset == extent_key->offset) {
2183 fd = root->fs_info->fs_devices->latest_bdev;
2184 readahead(fd, bytenr, num_bytes);
2186 ret = copy_disk_extent(root, new_pos, bytenr,
2190 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
2191 EXTENT_LOCKED, GFP_NOFS);
2193 ret = set_state_private(reloc_tree, bytenr, new_pos);
2197 ret = block_iterate_proc(new_pos / sectorsize,
2198 cur_offset / sectorsize, &data);
2202 cur_offset += sectorsize;
2203 bytenr += sectorsize;
2204 num_bytes -= sectorsize;
2207 if (data.num_blocks > 0) {
2208 ret = record_file_blocks(&data, data.first_block,
2209 data.disk_block, data.num_blocks);
2214 key.objectid = extent_key->objectid;
2216 key.type = BTRFS_INODE_ITEM_KEY;
2217 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
2221 leaf = path.nodes[0];
2222 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2223 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2224 btrfs_mark_buffer_dirty(leaf);
2225 btrfs_release_path(&path);
2228 btrfs_release_path(&path);
2232 static int relocate_extents_range(struct btrfs_root *fs_root,
2233 struct btrfs_root *image_root,
2234 u64 start_byte, u64 end_byte)
2236 struct btrfs_fs_info *info = fs_root->fs_info;
2237 struct btrfs_root *extent_root = info->extent_root;
2238 struct btrfs_root *cur_root = NULL;
2239 struct btrfs_trans_handle *trans;
2240 struct btrfs_extent_data_ref *dref;
2241 struct btrfs_extent_inline_ref *iref;
2242 struct btrfs_extent_item *ei;
2243 struct extent_buffer *leaf;
2244 struct btrfs_key key;
2245 struct btrfs_key extent_key;
2246 struct btrfs_path path;
2247 struct extent_io_tree reloc_tree;
2257 btrfs_init_path(&path);
2258 extent_io_tree_init(&reloc_tree);
2260 key.objectid = start_byte;
2262 key.type = BTRFS_EXTENT_ITEM_KEY;
2263 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2267 ret = btrfs_previous_item(extent_root, &path, 0,
2268 BTRFS_EXTENT_ITEM_KEY);
2272 leaf = path.nodes[0];
2273 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2274 if (key.objectid + key.offset > start_byte)
2275 start_byte = key.objectid;
2278 btrfs_release_path(&path);
2280 cur_root = (pass % 2 == 0) ? image_root : fs_root;
2283 trans = btrfs_start_transaction(cur_root, 1);
2286 cur_byte = start_byte;
2288 key.objectid = cur_byte;
2290 key.type = BTRFS_EXTENT_ITEM_KEY;
2291 ret = btrfs_search_slot(trans, extent_root,
2296 leaf = path.nodes[0];
2297 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2298 ret = btrfs_next_leaf(extent_root, &path);
2303 leaf = path.nodes[0];
2306 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2307 if (key.objectid < cur_byte ||
2308 key.type != BTRFS_EXTENT_ITEM_KEY) {
2312 if (key.objectid >= end_byte)
2317 cur_byte = key.objectid;
2318 num_bytes = key.offset;
2319 ei = btrfs_item_ptr(leaf, path.slots[0],
2320 struct btrfs_extent_item);
2321 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2322 BTRFS_EXTENT_FLAG_DATA));
2324 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2325 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2327 ptr += sizeof(struct btrfs_extent_item);
2330 iref = (struct btrfs_extent_inline_ref *)ptr;
2331 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2332 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2333 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2334 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2335 extent_key.objectid =
2336 btrfs_extent_data_ref_objectid(leaf, dref);
2338 btrfs_extent_data_ref_offset(leaf, dref);
2339 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2340 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2342 if (ref_root == cur_root->root_key.objectid)
2345 ptr += btrfs_extent_inline_ref_size(key.type);
2353 ret = relocate_one_reference(trans, cur_root, cur_byte,
2354 num_bytes, &extent_key,
2359 cur_byte += num_bytes;
2360 btrfs_release_path(&path);
2362 if (trans->blocks_used >= 4096) {
2363 ret = btrfs_commit_transaction(trans, cur_root);
2365 trans = btrfs_start_transaction(cur_root, 1);
2369 btrfs_release_path(&path);
2371 ret = btrfs_commit_transaction(trans, cur_root);
2374 if (num_extents > 0 && pass++ < 16)
2377 ret = (num_extents > 0) ? -1 : 0;
2379 btrfs_release_path(&path);
2380 extent_io_tree_cleanup(&reloc_tree);
2385 * relocate data in system chunk
2387 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2388 struct btrfs_root *image_root)
2390 struct btrfs_block_group_cache *cache;
2396 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2400 end_byte = cache->key.objectid + cache->key.offset;
2401 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2402 ret = relocate_extents_range(fs_root, image_root,
2403 cache->key.objectid,
2410 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2411 offset = btrfs_sb_offset(i);
2412 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2414 ret = relocate_extents_range(fs_root, image_root,
2415 offset, offset + BTRFS_STRIPE_LEN);
2424 static int fixup_chunk_mapping(struct btrfs_root *root)
2426 struct btrfs_trans_handle *trans;
2427 struct btrfs_fs_info *info = root->fs_info;
2428 struct btrfs_root *chunk_root = info->chunk_root;
2429 struct extent_buffer *leaf;
2430 struct btrfs_key key;
2431 struct btrfs_path path;
2432 struct btrfs_chunk chunk;
2438 btrfs_init_path(&path);
2440 trans = btrfs_start_transaction(root, 1);
2444 * recow the whole chunk tree. this will move all chunk tree blocks
2445 * into system block group.
2447 memset(&key, 0, sizeof(key));
2449 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2453 ret = btrfs_next_leaf(chunk_root, &path);
2459 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2460 btrfs_release_path(&path);
2462 btrfs_release_path(&path);
2464 /* fixup the system chunk array in super block */
2465 btrfs_set_super_sys_array_size(info->super_copy, 0);
2467 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2469 key.type = BTRFS_CHUNK_ITEM_KEY;
2471 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2476 leaf = path.nodes[0];
2477 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2478 ret = btrfs_next_leaf(chunk_root, &path);
2483 leaf = path.nodes[0];
2485 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2486 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2489 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2490 size = btrfs_item_size_nr(leaf, path.slots[0]);
2491 BUG_ON(size != sizeof(chunk));
2492 read_extent_buffer(leaf, &chunk, ptr, size);
2493 type = btrfs_stack_chunk_type(&chunk);
2495 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2498 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2506 ret = btrfs_commit_transaction(trans, root);
2509 btrfs_release_path(&path);
2513 static const struct btrfs_convert_operations ext2_convert_ops = {
2515 .open_fs = ext2_open_fs,
2516 .read_used_space = ext2_read_used_space,
2517 .alloc_block = ext2_alloc_block,
2518 .alloc_block_range = ext2_alloc_block_range,
2519 .copy_inodes = ext2_copy_inodes,
2520 .test_block = ext2_test_block,
2521 .free_block = ext2_free_block,
2522 .free_block_range = ext2_free_block_range,
2523 .close_fs = ext2_close_fs,
2526 static const struct btrfs_convert_operations *convert_operations[] = {
2530 static int convert_open_fs(const char *devname,
2531 struct btrfs_convert_context *cctx)
2535 memset(cctx, 0, sizeof(*cctx));
2537 for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
2538 int ret = convert_operations[i]->open_fs(cctx, devname);
2541 cctx->convert_ops = convert_operations[i];
2546 fprintf(stderr, "No file system found to convert.\n");
2551 * Remove one reserve range from given cache tree
2552 * if min_stripe_size is non-zero, it will ensure for split case,
2553 * all its split cache extent is no smaller than @min_strip_size / 2.
2555 static int wipe_one_reserved_range(struct cache_tree *tree,
2556 u64 start, u64 len, u64 min_stripe_size,
2559 struct cache_extent *cache;
2562 BUG_ON(ensure_size && min_stripe_size == 0);
2564 * The logical here is simplified to handle special cases only
2565 * So we don't need to consider merge case for ensure_size
2567 BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
2568 min_stripe_size / 2 < BTRFS_STRIPE_LEN));
2570 /* Also, wipe range should already be aligned */
2571 BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
2572 start + len != round_up(start + len, BTRFS_STRIPE_LEN));
2574 min_stripe_size /= 2;
2576 cache = lookup_cache_extent(tree, start, len);
2580 if (start <= cache->start) {
2582 * |--------cache---------|
2585 BUG_ON(start + len <= cache->start);
2588 * The wipe size is smaller than min_stripe_size / 2,
2589 * so the result length should still meet min_stripe_size
2590 * And no need to do alignment
2592 cache->size -= (start + len - cache->start);
2593 if (cache->size == 0) {
2594 remove_cache_extent(tree, cache);
2599 BUG_ON(ensure_size && cache->size < min_stripe_size);
2601 cache->start = start + len;
2603 } else if (start > cache->start && start + len < cache->start +
2606 * |-------cache-----|
2609 u64 old_len = cache->size;
2610 u64 insert_start = start + len;
2613 cache->size = start - cache->start;
2615 cache->size = max(cache->size, min_stripe_size);
2616 cache->start = start - cache->size;
2618 /* And insert the new one */
2619 insert_len = old_len - start - len;
2621 insert_len = max(insert_len, min_stripe_size);
2623 ret = add_merge_cache_extent(tree, insert_start, insert_len);
2629 * Wipe len should be small enough and no need to expand the
2632 cache->size = start - cache->start;
2633 BUG_ON(ensure_size && cache->size < min_stripe_size);
2638 * Remove reserved ranges from given cache_tree
2640 * It will remove the following ranges
2642 * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
2643 * 3) 3rd superblock, +64K
2645 * @min_stripe must be given for safety check
2646 * and if @ensure_size is given, it will ensure affected cache_extent will be
2647 * larger than min_stripe_size
2649 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
2654 ret = wipe_one_reserved_range(tree, 0, 1024 * 1024, min_stripe_size,
2658 ret = wipe_one_reserved_range(tree, btrfs_sb_offset(1),
2659 BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
2662 ret = wipe_one_reserved_range(tree, btrfs_sb_offset(2),
2663 BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
2670 static int convert_read_used_space(struct btrfs_convert_context *cctx)
2672 return cctx->convert_ops->read_used_space(cctx);
2675 static int do_convert(const char *devname, int datacsum, int packing, int noxattr,
2676 u32 nodesize, int copylabel, const char *fslabel, int progress,
2679 int i, ret, blocks_per_node;
2686 struct btrfs_root *root;
2687 struct btrfs_root *image_root;
2688 struct btrfs_convert_context cctx;
2689 char *subvol_name = NULL;
2690 struct task_ctx ctx;
2691 char features_buf[64];
2692 struct btrfs_mkfs_config mkfs_cfg;
2694 init_convert_context(&cctx);
2695 ret = convert_open_fs(devname, &cctx);
2698 ret = convert_read_used_space(&cctx);
2702 blocksize = cctx.blocksize;
2703 total_bytes = (u64)blocksize * (u64)cctx.block_count;
2704 if (blocksize < 4096) {
2705 fprintf(stderr, "block size is too small\n");
2708 if (btrfs_check_nodesize(nodesize, blocksize, features))
2710 blocks_per_node = nodesize / blocksize;
2711 ret = -blocks_per_node;
2712 for (i = 0; i < 7; i++) {
2713 if (nodesize == blocksize)
2714 ret = convert_alloc_block(&cctx, 0, blocks + i);
2716 ret = convert_alloc_block_range(&cctx,
2717 ret + blocks_per_node, blocks_per_node,
2720 fprintf(stderr, "not enough free space\n");
2723 blocks[i] *= blocksize;
2725 super_bytenr = blocks[0];
2726 fd = open(devname, O_RDWR);
2728 fprintf(stderr, "unable to open %s\n", devname);
2731 btrfs_parse_features_to_string(features_buf, features);
2732 if (features == BTRFS_MKFS_DEFAULT_FEATURES)
2733 strcat(features_buf, " (default)");
2735 printf("create btrfs filesystem:\n");
2736 printf("\tblocksize: %u\n", blocksize);
2737 printf("\tnodesize: %u\n", nodesize);
2738 printf("\tfeatures: %s\n", features_buf);
2740 mkfs_cfg.label = cctx.volume_name;
2741 mkfs_cfg.fs_uuid = NULL;
2742 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
2743 mkfs_cfg.num_bytes = total_bytes;
2744 mkfs_cfg.nodesize = nodesize;
2745 mkfs_cfg.sectorsize = blocksize;
2746 mkfs_cfg.stripesize = blocksize;
2747 mkfs_cfg.features = features;
2749 ret = make_btrfs(fd, &mkfs_cfg);
2751 fprintf(stderr, "unable to create initial ctree: %s\n",
2755 /* create a system chunk that maps the whole device */
2756 ret = prepare_system_chunk(fd, super_bytenr);
2758 fprintf(stderr, "unable to update system chunk\n");
2761 root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
2763 fprintf(stderr, "unable to open ctree\n");
2766 ret = cache_free_extents(root, &cctx);
2768 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2771 root->fs_info->extent_ops = &extent_ops;
2772 /* recover block allocation bitmap */
2773 for (i = 0; i < 7; i++) {
2774 blocks[i] /= blocksize;
2775 if (nodesize == blocksize)
2776 convert_free_block(&cctx, blocks[i]);
2778 convert_free_block_range(&cctx, blocks[i],
2781 ret = init_btrfs(root);
2783 fprintf(stderr, "unable to setup the root tree\n");
2786 printf("creating btrfs metadata.\n");
2787 ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
2788 ctx.cur_copy_inodes = 0;
2791 ctx.info = task_init(print_copied_inodes, after_copied_inodes, &ctx);
2792 task_start(ctx.info);
2794 ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
2796 fprintf(stderr, "error during copy_inodes %d\n", ret);
2800 task_stop(ctx.info);
2801 task_deinit(ctx.info);
2804 printf("creating %s image file.\n", cctx.convert_ops->name);
2805 ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
2807 fprintf(stderr, "error allocating subvolume name: %s_saved\n",
2808 cctx.convert_ops->name);
2812 image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
2817 fprintf(stderr, "unable to create subvol\n");
2820 ret = create_image(&cctx, image_root, "image", datacsum);
2822 fprintf(stderr, "error during create_image %d\n", ret);
2825 memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
2826 if (copylabel == 1) {
2827 __strncpy_null(root->fs_info->super_copy->label,
2828 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
2829 fprintf(stderr, "copy label '%s'\n",
2830 root->fs_info->super_copy->label);
2831 } else if (copylabel == -1) {
2832 strcpy(root->fs_info->super_copy->label, fslabel);
2833 fprintf(stderr, "set label to '%s'\n", fslabel);
2836 printf("cleaning up system chunk.\n");
2837 ret = cleanup_sys_chunk(root, image_root);
2839 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2842 ret = close_ctree(root);
2844 fprintf(stderr, "error during close_ctree %d\n", ret);
2847 convert_close_fs(&cctx);
2848 clean_convert_context(&cctx);
2851 * If this step succeed, we get a mountable btrfs. Otherwise
2852 * the source fs is left unchanged.
2854 ret = migrate_super_block(fd, super_bytenr, blocksize);
2856 fprintf(stderr, "unable to migrate super block\n");
2861 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2863 fprintf(stderr, "unable to open ctree\n");
2866 /* move chunk tree into system chunk. */
2867 ret = fixup_chunk_mapping(root);
2869 fprintf(stderr, "error during fixup_chunk_tree\n");
2872 ret = close_ctree(root);
2875 printf("conversion complete.\n");
2878 clean_convert_context(&cctx);
2883 "WARNING: an error occured during chunk mapping fixup, filesystem mountable but not finalized\n");
2885 fprintf(stderr, "conversion aborted\n");
2889 static int may_rollback(struct btrfs_root *root)
2891 struct btrfs_fs_info *info = root->fs_info;
2892 struct btrfs_multi_bio *multi = NULL;
2900 if (btrfs_super_num_devices(info->super_copy) != 1)
2903 bytenr = BTRFS_SUPER_INFO_OFFSET;
2904 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2907 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2908 &length, &multi, 0, NULL);
2910 if (ret == -ENOENT) {
2911 /* removed block group at the tail */
2912 if (length == (u64)-1)
2915 /* removed block group in the middle */
2921 num_stripes = multi->num_stripes;
2922 physical = multi->stripes[0].physical;
2925 if (num_stripes != 1 || physical != bytenr)
2929 if (bytenr >= total_bytes)
2937 static int do_rollback(const char *devname)
2942 struct btrfs_root *root;
2943 struct btrfs_root *image_root;
2944 struct btrfs_root *chunk_root;
2945 struct btrfs_dir_item *dir;
2946 struct btrfs_inode_item *inode;
2947 struct btrfs_file_extent_item *fi;
2948 struct btrfs_trans_handle *trans;
2949 struct extent_buffer *leaf;
2950 struct btrfs_block_group_cache *cache1;
2951 struct btrfs_block_group_cache *cache2;
2952 struct btrfs_key key;
2953 struct btrfs_path path;
2954 struct extent_io_tree io_tree;
2969 extent_io_tree_init(&io_tree);
2971 fd = open(devname, O_RDWR);
2973 fprintf(stderr, "unable to open %s\n", devname);
2976 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2978 fprintf(stderr, "unable to open ctree\n");
2981 ret = may_rollback(root);
2983 fprintf(stderr, "unable to do rollback\n");
2987 sectorsize = root->sectorsize;
2988 buf = malloc(sectorsize);
2990 fprintf(stderr, "unable to allocate memory\n");
2994 btrfs_init_path(&path);
2996 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
2997 key.type = BTRFS_ROOT_BACKREF_KEY;
2998 key.offset = BTRFS_FS_TREE_OBJECTID;
2999 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path, 0,
3001 btrfs_release_path(&path);
3004 "ERROR: unable to convert ext2 image subvolume, is it deleted?\n");
3006 } else if (ret < 0) {
3008 "ERROR: unable to open ext2_saved, id=%llu: %s\n",
3009 (unsigned long long)key.objectid, strerror(-ret));
3013 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3014 key.type = BTRFS_ROOT_ITEM_KEY;
3015 key.offset = (u64)-1;
3016 image_root = btrfs_read_fs_root(root->fs_info, &key);
3017 if (!image_root || IS_ERR(image_root)) {
3018 fprintf(stderr, "unable to open subvol %llu\n",
3019 (unsigned long long)key.objectid);
3024 root_dir = btrfs_root_dirid(&root->root_item);
3025 dir = btrfs_lookup_dir_item(NULL, image_root, &path,
3026 root_dir, name, strlen(name), 0);
3027 if (!dir || IS_ERR(dir)) {
3028 fprintf(stderr, "unable to find file %s\n", name);
3031 leaf = path.nodes[0];
3032 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
3033 btrfs_release_path(&path);
3035 objectid = key.objectid;
3037 ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
3039 fprintf(stderr, "unable to find inode item\n");
3042 leaf = path.nodes[0];
3043 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
3044 total_bytes = btrfs_inode_size(leaf, inode);
3045 btrfs_release_path(&path);
3047 key.objectid = objectid;
3049 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3050 ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
3052 fprintf(stderr, "unable to find first file extent\n");
3053 btrfs_release_path(&path);
3057 /* build mapping tree for the relocated blocks */
3058 for (offset = 0; offset < total_bytes; ) {
3059 leaf = path.nodes[0];
3060 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
3061 ret = btrfs_next_leaf(root, &path);
3067 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
3068 if (key.objectid != objectid || key.offset != offset ||
3069 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3072 fi = btrfs_item_ptr(leaf, path.slots[0],
3073 struct btrfs_file_extent_item);
3074 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
3076 if (btrfs_file_extent_compression(leaf, fi) ||
3077 btrfs_file_extent_encryption(leaf, fi) ||
3078 btrfs_file_extent_other_encoding(leaf, fi))
3081 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3082 /* skip holes and direct mapped extents */
3083 if (bytenr == 0 || bytenr == offset)
3086 bytenr += btrfs_file_extent_offset(leaf, fi);
3087 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3089 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3090 cache2 = btrfs_lookup_block_group(root->fs_info,
3091 offset + num_bytes - 1);
3092 if (!cache1 || cache1 != cache2 ||
3093 (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
3094 !intersect_with_sb(offset, num_bytes)))
3097 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
3098 EXTENT_LOCKED, GFP_NOFS);
3099 set_state_private(&io_tree, offset, bytenr);
3101 offset += btrfs_file_extent_num_bytes(leaf, fi);
3104 btrfs_release_path(&path);
3106 if (offset < total_bytes) {
3107 fprintf(stderr, "unable to build extent mapping\n");
3111 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
3112 first_free &= ~((u64)sectorsize - 1);
3113 /* backup for extent #0 should exist */
3114 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
3115 fprintf(stderr, "no backup for the first extent\n");
3118 /* force no allocation from system block group */
3119 root->fs_info->system_allocs = -1;
3120 trans = btrfs_start_transaction(root, 1);
3123 * recow the whole chunk tree, this will remove all chunk tree blocks
3124 * from system block group
3126 chunk_root = root->fs_info->chunk_root;
3127 memset(&key, 0, sizeof(key));
3129 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
3133 ret = btrfs_next_leaf(chunk_root, &path);
3137 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3138 btrfs_release_path(&path);
3140 btrfs_release_path(&path);
3145 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3149 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
3150 num_bytes += btrfs_block_group_used(&cache1->item);
3152 offset = cache1->key.objectid + cache1->key.offset;
3154 /* only extent #0 left in system block group? */
3155 if (num_bytes > first_free) {
3156 fprintf(stderr, "unable to empty system block group\n");
3159 /* create a system chunk that maps the whole device */
3160 ret = prepare_system_chunk_sb(root->fs_info->super_copy);
3162 fprintf(stderr, "unable to update system chunk\n");
3166 ret = btrfs_commit_transaction(trans, root);
3169 ret = close_ctree(root);
3171 fprintf(stderr, "error during close_ctree %d\n", ret);
3175 /* zero btrfs super block mirrors */
3176 memset(buf, 0, sectorsize);
3177 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3178 bytenr = btrfs_sb_offset(i);
3179 if (bytenr >= total_bytes)
3181 ret = pwrite(fd, buf, sectorsize, bytenr);
3182 if (ret != sectorsize) {
3184 "error during zeroing superblock %d: %d\n",
3190 sb_bytenr = (u64)-1;
3191 /* copy all relocated blocks back */
3193 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
3198 ret = get_state_private(&io_tree, start, &bytenr);
3201 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
3204 while (start <= end) {
3205 if (start == BTRFS_SUPER_INFO_OFFSET) {
3209 ret = pread(fd, buf, sectorsize, bytenr);
3211 fprintf(stderr, "error during pread %d\n", ret);
3214 BUG_ON(ret != sectorsize);
3215 ret = pwrite(fd, buf, sectorsize, start);
3217 fprintf(stderr, "error during pwrite %d\n", ret);
3220 BUG_ON(ret != sectorsize);
3222 start += sectorsize;
3223 bytenr += sectorsize;
3229 fprintf(stderr, "error during fsync %d\n", ret);
3233 * finally, overwrite btrfs super block.
3235 ret = pread(fd, buf, sectorsize, sb_bytenr);
3237 fprintf(stderr, "error during pread %d\n", ret);
3240 BUG_ON(ret != sectorsize);
3241 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
3243 fprintf(stderr, "error during pwrite %d\n", ret);
3246 BUG_ON(ret != sectorsize);
3249 fprintf(stderr, "error during fsync %d\n", ret);
3255 extent_io_tree_cleanup(&io_tree);
3256 printf("rollback complete.\n");
3263 fprintf(stderr, "rollback aborted.\n");
3267 static void print_usage(void)
3269 printf("usage: btrfs-convert [options] device\n");
3270 printf("options:\n");
3271 printf("\t-d|--no-datasum disable data checksum, sets NODATASUM\n");
3272 printf("\t-i|--no-xattr ignore xattrs and ACLs\n");
3273 printf("\t-n|--no-inline disable inlining of small files to metadata\n");
3274 printf("\t-N|--nodesize SIZE set filesystem metadata nodesize\n");
3275 printf("\t-r|--rollback roll back to the original filesystem\n");
3276 printf("\t-l|--label LABEL set filesystem label\n");
3277 printf("\t-L|--copy-label use label from converted filesystem\n");
3278 printf("\t-p|--progress show converting progress (default)\n");
3279 printf("\t-O|--features LIST comma separated list of filesystem features\n");
3280 printf("\t--no-progress show only overview, not the detailed progress\n");
3283 int main(int argc, char *argv[])
3289 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
3290 BTRFS_MKFS_DEFAULT_NODE_SIZE);
3293 int usage_error = 0;
3296 char fslabel[BTRFS_LABEL_SIZE];
3297 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
3300 enum { GETOPT_VAL_NO_PROGRESS = 256 };
3301 static const struct option long_options[] = {
3302 { "no-progress", no_argument, NULL,
3303 GETOPT_VAL_NO_PROGRESS },
3304 { "no-datasum", no_argument, NULL, 'd' },
3305 { "no-inline", no_argument, NULL, 'n' },
3306 { "no-xattr", no_argument, NULL, 'i' },
3307 { "rollback", no_argument, NULL, 'r' },
3308 { "features", required_argument, NULL, 'O' },
3309 { "progress", no_argument, NULL, 'p' },
3310 { "label", required_argument, NULL, 'l' },
3311 { "copy-label", no_argument, NULL, 'L' },
3312 { "nodesize", required_argument, NULL, 'N' },
3313 { "help", no_argument, NULL, GETOPT_VAL_HELP},
3314 { NULL, 0, NULL, 0 }
3316 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
3331 nodesize = parse_size(optarg);
3338 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
3340 "WARNING: label too long, trimmed to %d bytes\n",
3341 BTRFS_LABEL_SIZE - 1);
3343 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
3352 char *orig = strdup(optarg);
3355 tmp = btrfs_parse_fs_features(tmp, &features);
3358 "Unrecognized filesystem feature '%s'\n",
3364 if (features & BTRFS_FEATURE_LIST_ALL) {
3365 btrfs_list_all_fs_features(
3366 ~BTRFS_CONVERT_ALLOWED_FEATURES);
3369 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
3372 btrfs_parse_features_to_string(buf,
3373 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
3375 "ERROR: features not allowed for convert: %s\n",
3382 case GETOPT_VAL_NO_PROGRESS:
3385 case GETOPT_VAL_HELP:
3388 return c != GETOPT_VAL_HELP;
3392 if (check_argc_exact(argc - optind, 1)) {
3397 if (rollback && (!datacsum || noxattr || !packing)) {
3399 "Usage error: -d, -i, -n options do not apply to rollback\n");
3408 file = argv[optind];
3409 ret = check_mounted(file);
3411 fprintf(stderr, "Could not check mount status: %s\n",
3415 fprintf(stderr, "%s is mounted\n", file);
3420 ret = do_rollback(file);
3422 ret = do_convert(file, datacsum, packing, noxattr, nodesize,
3423 copylabel, fslabel, progress, features);