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 static void init_convert_context(struct btrfs_convert_context *cctx)
113 cache_tree_init(&cctx->used);
114 cache_tree_init(&cctx->data_chunks);
115 cache_tree_init(&cctx->free);
118 static void clean_convert_context(struct btrfs_convert_context *cctx)
120 free_extent_cache_tree(&cctx->used);
121 free_extent_cache_tree(&cctx->data_chunks);
122 free_extent_cache_tree(&cctx->free);
125 static inline int convert_alloc_block(struct btrfs_convert_context *cctx,
128 return cctx->convert_ops->alloc_block(cctx, goal, ret);
131 static inline int convert_alloc_block_range(struct btrfs_convert_context *cctx,
132 u64 goal, int num, u64 *ret)
134 return cctx->convert_ops->alloc_block_range(cctx, goal, num, ret);
137 static inline int convert_test_block(struct btrfs_convert_context *cctx,
140 return cctx->convert_ops->test_block(cctx, block);
143 static inline void convert_free_block(struct btrfs_convert_context *cctx,
146 cctx->convert_ops->free_block(cctx, block);
149 static inline void convert_free_block_range(struct btrfs_convert_context *cctx,
152 cctx->convert_ops->free_block_range(cctx, block, num);
155 static inline int copy_inodes(struct btrfs_convert_context *cctx,
156 struct btrfs_root *root, int datacsum,
157 int packing, int noxattr, struct task_ctx *p)
159 return cctx->convert_ops->copy_inodes(cctx, root, datacsum, packing,
163 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
165 cctx->convert_ops->close_fs(cctx);
169 * Open Ext2fs in readonly mode, read block allocation bitmap and
170 * inode bitmap into memory.
172 static int ext2_open_fs(struct btrfs_convert_context *cctx, const char *name)
179 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
181 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
185 * We need to know exactly the used space, some RO compat flags like
186 * BIGALLOC will affect how used space is present.
187 * So we need manuall check any unsupported RO compat flags
189 ro_feature = ext2_fs->super->s_feature_ro_compat;
190 if (ro_feature & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
192 "unsupported RO features detected: %x, abort convert to avoid possible corruption",
193 ro_feature & ~EXT2_LIB_FEATURE_COMPAT_SUPP);
196 ret = ext2fs_read_inode_bitmap(ext2_fs);
198 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
202 ret = ext2fs_read_block_bitmap(ext2_fs);
204 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
209 * search each block group for a free inode. this set up
210 * uninit block/inode bitmaps appropriately.
213 while (ino <= ext2_fs->super->s_inodes_count) {
215 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
216 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
219 if (!(ext2_fs->super->s_feature_incompat &
220 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
221 fprintf(stderr, "filetype feature is missing\n");
225 cctx->fs_data = ext2_fs;
226 cctx->blocksize = ext2_fs->blocksize;
227 cctx->block_count = ext2_fs->super->s_blocks_count;
228 cctx->total_bytes = ext2_fs->blocksize * ext2_fs->super->s_blocks_count;
229 cctx->volume_name = strndup(ext2_fs->super->s_volume_name, 16);
230 cctx->first_data_block = ext2_fs->super->s_first_data_block;
231 cctx->inodes_count = ext2_fs->super->s_inodes_count;
232 cctx->free_inodes_count = ext2_fs->super->s_free_inodes_count;
235 ext2fs_close(ext2_fs);
239 static int __ext2_add_one_block(ext2_filsys fs, char *bitmap,
240 unsigned long group_nr, struct cache_tree *used)
242 unsigned long offset;
246 offset = fs->super->s_first_data_block;
247 offset /= EXT2FS_CLUSTER_RATIO(fs);
248 offset += group_nr * EXT2_CLUSTERS_PER_GROUP(fs->super);
249 for (i = 0; i < EXT2_CLUSTERS_PER_GROUP(fs->super); i++) {
250 if (ext2fs_test_bit(i, bitmap)) {
253 start = (i + offset) * EXT2FS_CLUSTER_RATIO(fs);
254 start *= fs->blocksize;
255 ret = add_merge_cache_extent(used, start,
265 * Read all used ext2 space into cctx->used cache tree
267 static int ext2_read_used_space(struct btrfs_convert_context *cctx)
269 ext2_filsys fs = (ext2_filsys)cctx->fs_data;
270 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
271 struct cache_tree *used_tree = &cctx->used;
272 char *block_bitmap = NULL;
277 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
278 /* Shouldn't happen */
279 BUG_ON(!fs->block_map);
281 block_bitmap = malloc(block_nbytes);
285 for (i = 0; i < fs->group_desc_count; i++) {
286 ret = ext2fs_get_block_bitmap_range(fs->block_map, blk_itr,
287 block_nbytes * 8, block_bitmap);
289 error("fail to get bitmap from ext2, %s",
293 ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree);
295 error("fail to build used space tree, %s",
299 blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super);
306 static void ext2_close_fs(struct btrfs_convert_context *cctx)
308 if (cctx->volume_name) {
309 free(cctx->volume_name);
310 cctx->volume_name = NULL;
312 ext2fs_close(cctx->fs_data);
315 static int ext2_alloc_block(struct btrfs_convert_context *cctx,
316 u64 goal, u64 *block_ret)
318 ext2_filsys fs = cctx->fs_data;
321 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
322 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
329 static int ext2_alloc_block_range(struct btrfs_convert_context *cctx, u64 goal,
330 int num, u64 *block_ret)
332 ext2_filsys fs = cctx->fs_data;
334 ext2fs_block_bitmap bitmap = fs->block_map;
335 blk_t start = ext2fs_get_block_bitmap_start(bitmap);
336 blk_t end = ext2fs_get_block_bitmap_end(bitmap);
338 for (block = max_t(u64, goal, start); block + num < end; block++) {
339 if (ext2fs_fast_test_block_bitmap_range(bitmap, block, num)) {
340 ext2fs_fast_mark_block_bitmap_range(bitmap, block,
349 static void ext2_free_block(struct btrfs_convert_context *cctx, u64 block)
351 ext2_filsys fs = cctx->fs_data;
353 BUG_ON(block != (blk_t)block);
354 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
357 static void ext2_free_block_range(struct btrfs_convert_context *cctx, u64 block, int num)
359 ext2_filsys fs = cctx->fs_data;
361 BUG_ON(block != (blk_t)block);
362 ext2fs_fast_unmark_block_bitmap_range(fs->block_map, block, num);
365 static int cache_free_extents(struct btrfs_root *root,
366 struct btrfs_convert_context *cctx)
372 u64 blocksize = cctx->blocksize;
374 block = cctx->first_data_block;
375 for (; block < cctx->block_count; block++) {
376 if (convert_test_block(cctx, block))
378 bytenr = block * blocksize;
379 ret = set_extent_dirty(&root->fs_info->free_space_cache,
380 bytenr, bytenr + blocksize - 1, 0);
384 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
385 bytenr = btrfs_sb_offset(i);
386 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
387 if (bytenr >= blocksize * cctx->block_count)
389 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
390 bytenr + BTRFS_STRIPE_LEN - 1, 0);
393 clear_extent_dirty(&root->fs_info->free_space_cache,
394 0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
399 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
400 u64 hint_byte, struct btrfs_key *ins,
405 u64 last = hint_byte;
408 struct btrfs_block_group_cache *cache;
411 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
412 last, &start, &end, EXTENT_DIRTY);
414 if (wrapped++ == 0) {
422 start = max(last, start);
424 if (last - start < num_bytes)
427 last = start + num_bytes;
428 if (test_range_bit(&root->fs_info->pinned_extents,
429 start, last - 1, EXTENT_DIRTY, 0))
432 cache = btrfs_lookup_block_group(root->fs_info, start);
434 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
435 last > cache->key.objectid + cache->key.offset) {
436 last = cache->key.objectid + cache->key.offset;
441 BUG_ON(num_bytes != root->nodesize);
442 if (check_crossing_stripes(start, num_bytes)) {
443 last = round_down(start + num_bytes,
448 clear_extent_dirty(&root->fs_info->free_space_cache,
449 start, start + num_bytes - 1, 0);
451 ins->objectid = start;
452 ins->offset = num_bytes;
453 ins->type = BTRFS_EXTENT_ITEM_KEY;
457 fprintf(stderr, "not enough free space\n");
461 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
466 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
467 offset = btrfs_sb_offset(i);
468 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
470 if (bytenr < offset + BTRFS_STRIPE_LEN &&
471 bytenr + num_bytes > offset)
477 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
480 return intersect_with_sb(bytenr, num_bytes);
483 static struct btrfs_extent_ops extent_ops = {
484 .alloc_extent = custom_alloc_extent,
485 .free_extent = custom_free_extent,
488 static int convert_insert_dirent(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 const char *name, size_t name_len,
491 u64 dir, u64 objectid,
492 u8 file_type, u64 index_cnt,
493 struct btrfs_inode_item *inode)
497 struct btrfs_key location = {
498 .objectid = objectid,
500 .type = BTRFS_INODE_ITEM_KEY,
503 ret = btrfs_insert_dir_item(trans, root, name, name_len,
504 dir, &location, file_type, index_cnt);
507 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
508 objectid, dir, index_cnt);
511 inode_size = btrfs_stack_inode_size(inode) + name_len * 2;
512 btrfs_set_stack_inode_size(inode, inode_size);
517 struct dir_iterate_data {
518 struct btrfs_trans_handle *trans;
519 struct btrfs_root *root;
520 struct btrfs_inode_item *inode;
527 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
528 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
529 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
530 [EXT2_FT_DIR] = BTRFS_FT_DIR,
531 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
532 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
533 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
534 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
535 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
538 static int dir_iterate_proc(ext2_ino_t dir, int entry,
539 struct ext2_dir_entry *dirent,
540 int offset, int blocksize,
541 char *buf,void *priv_data)
546 char dotdot[] = "..";
547 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
550 name_len = dirent->name_len & 0xFF;
552 objectid = dirent->inode + INO_OFFSET;
553 if (!strncmp(dirent->name, dotdot, name_len)) {
555 BUG_ON(idata->parent != 0);
556 idata->parent = objectid;
560 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
563 file_type = dirent->name_len >> 8;
564 BUG_ON(file_type > EXT2_FT_SYMLINK);
566 ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
567 name_len, idata->objectid, objectid,
568 filetype_conversion_table[file_type],
569 idata->index_cnt, idata->inode);
571 idata->errcode = ret;
579 static int create_dir_entries(struct btrfs_trans_handle *trans,
580 struct btrfs_root *root, u64 objectid,
581 struct btrfs_inode_item *btrfs_inode,
582 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
586 struct dir_iterate_data data = {
589 .inode = btrfs_inode,
590 .objectid = objectid,
596 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
597 dir_iterate_proc, &data);
601 if (ret == 0 && data.parent == objectid) {
602 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
603 objectid, objectid, 0);
607 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
611 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
612 u32 num_bytes, char *buffer)
615 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
617 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
618 if (ret != num_bytes)
627 static int csum_disk_extent(struct btrfs_trans_handle *trans,
628 struct btrfs_root *root,
629 u64 disk_bytenr, u64 num_bytes)
631 u32 blocksize = root->sectorsize;
636 buffer = malloc(blocksize);
639 for (offset = 0; offset < num_bytes; offset += blocksize) {
640 ret = read_disk_extent(root, disk_bytenr + offset,
644 ret = btrfs_csum_file_block(trans,
645 root->fs_info->csum_root,
646 disk_bytenr + num_bytes,
647 disk_bytenr + offset,
656 struct blk_iterate_data {
657 struct btrfs_trans_handle *trans;
658 struct btrfs_root *root;
659 struct btrfs_root *convert_root;
660 struct btrfs_inode_item *inode;
671 static void init_blk_iterate_data(struct blk_iterate_data *data,
672 struct btrfs_trans_handle *trans,
673 struct btrfs_root *root,
674 struct btrfs_inode_item *inode,
675 u64 objectid, int checksum)
677 struct btrfs_key key;
682 data->objectid = objectid;
683 data->first_block = 0;
684 data->disk_block = 0;
685 data->num_blocks = 0;
686 data->boundary = (u64)-1;
687 data->checksum = checksum;
690 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
691 key.type = BTRFS_ROOT_ITEM_KEY;
692 key.offset = (u64)-1;
693 data->convert_root = btrfs_read_fs_root(root->fs_info, &key);
694 /* Impossible as we just opened it before */
695 BUG_ON(!data->convert_root || IS_ERR(data->convert_root));
696 data->convert_ino = BTRFS_FIRST_FREE_OBJECTID + 1;
700 * Record a file extent in original filesystem into btrfs one.
701 * The special point is, old disk_block can point to a reserved range.
702 * So here, we don't use disk_block directly but search convert_root
703 * to get the real disk_bytenr.
705 static int record_file_blocks(struct blk_iterate_data *data,
706 u64 file_block, u64 disk_block, u64 num_blocks)
709 struct btrfs_root *root = data->root;
710 struct btrfs_root *convert_root = data->convert_root;
711 struct btrfs_path *path;
712 u64 file_pos = file_block * root->sectorsize;
713 u64 old_disk_bytenr = disk_block * root->sectorsize;
714 u64 num_bytes = num_blocks * root->sectorsize;
715 u64 cur_off = old_disk_bytenr;
717 /* Hole, pass it to record_file_extent directly */
718 if (old_disk_bytenr == 0)
719 return btrfs_record_file_extent(data->trans, root,
720 data->objectid, data->inode, file_pos, 0,
723 path = btrfs_alloc_path();
728 * Search real disk bytenr from convert root
730 while (cur_off < old_disk_bytenr + num_bytes) {
731 struct btrfs_key key;
732 struct btrfs_file_extent_item *fi;
733 struct extent_buffer *node;
735 u64 extent_disk_bytenr;
736 u64 extent_num_bytes;
737 u64 real_disk_bytenr;
740 key.objectid = data->convert_ino;
741 key.type = BTRFS_EXTENT_DATA_KEY;
742 key.offset = cur_off;
744 ret = btrfs_search_slot(NULL, convert_root, &key, path, 0, 0);
748 ret = btrfs_previous_item(convert_root, path,
750 BTRFS_EXTENT_DATA_KEY);
758 node = path->nodes[0];
759 slot = path->slots[0];
760 btrfs_item_key_to_cpu(node, &key, slot);
761 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY ||
762 key.objectid != data->convert_ino ||
763 key.offset > cur_off);
764 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
765 extent_disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
766 extent_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
767 BUG_ON(cur_off - key.offset >= extent_num_bytes);
768 btrfs_release_path(path);
770 real_disk_bytenr = cur_off - key.offset + extent_disk_bytenr;
771 cur_len = min(key.offset + extent_num_bytes,
772 old_disk_bytenr + num_bytes) - cur_off;
773 ret = btrfs_record_file_extent(data->trans, data->root,
774 data->objectid, data->inode, file_pos,
775 real_disk_bytenr, cur_len);
782 * No need to care about csum
783 * As every byte of old fs image is calculated for csum, no
784 * need to waste CPU cycles now.
787 btrfs_free_path(path);
791 static int block_iterate_proc(u64 disk_block, u64 file_block,
792 struct blk_iterate_data *idata)
797 struct btrfs_root *root = idata->root;
798 struct btrfs_block_group_cache *cache;
799 u64 bytenr = disk_block * root->sectorsize;
801 sb_region = intersect_with_sb(bytenr, root->sectorsize);
802 do_barrier = sb_region || disk_block >= idata->boundary;
803 if ((idata->num_blocks > 0 && do_barrier) ||
804 (file_block > idata->first_block + idata->num_blocks) ||
805 (disk_block != idata->disk_block + idata->num_blocks)) {
806 if (idata->num_blocks > 0) {
807 ret = record_file_blocks(idata, idata->first_block,
812 idata->first_block += idata->num_blocks;
813 idata->num_blocks = 0;
815 if (file_block > idata->first_block) {
816 ret = record_file_blocks(idata, idata->first_block,
817 0, file_block - idata->first_block);
823 bytenr += BTRFS_STRIPE_LEN - 1;
824 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
826 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
828 bytenr = cache->key.objectid + cache->key.offset;
831 idata->first_block = file_block;
832 idata->disk_block = disk_block;
833 idata->boundary = bytenr / root->sectorsize;
840 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
841 e2_blkcnt_t blockcnt, blk_t ref_block,
842 int ref_offset, void *priv_data)
845 struct blk_iterate_data *idata;
846 idata = (struct blk_iterate_data *)priv_data;
847 ret = block_iterate_proc(*blocknr, blockcnt, idata);
849 idata->errcode = ret;
856 * traverse file's data blocks, record these data blocks as file extents.
858 static int create_file_extents(struct btrfs_trans_handle *trans,
859 struct btrfs_root *root, u64 objectid,
860 struct btrfs_inode_item *btrfs_inode,
861 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
862 int datacsum, int packing)
868 u32 sectorsize = root->sectorsize;
869 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
870 struct blk_iterate_data data;
872 init_blk_iterate_data(&data, trans, root, btrfs_inode, objectid,
875 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
876 NULL, __block_iterate_proc, &data);
882 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
883 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
884 u64 num_bytes = data.num_blocks * sectorsize;
885 u64 disk_bytenr = data.disk_block * sectorsize;
888 buffer = malloc(num_bytes);
891 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
894 if (num_bytes > inode_size)
895 num_bytes = inode_size;
896 ret = btrfs_insert_inline_extent(trans, root, objectid,
897 0, buffer, num_bytes);
900 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
901 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
902 } else if (data.num_blocks > 0) {
903 ret = record_file_blocks(&data, data.first_block,
904 data.disk_block, data.num_blocks);
908 data.first_block += data.num_blocks;
909 last_block = (inode_size + sectorsize - 1) / sectorsize;
910 if (last_block > data.first_block) {
911 ret = record_file_blocks(&data, data.first_block, 0,
912 last_block - data.first_block);
918 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
922 static int create_symbol_link(struct btrfs_trans_handle *trans,
923 struct btrfs_root *root, u64 objectid,
924 struct btrfs_inode_item *btrfs_inode,
925 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
926 struct ext2_inode *ext2_inode)
930 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
931 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
932 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
933 ret = create_file_extents(trans, root, objectid, btrfs_inode,
934 ext2_fs, ext2_ino, 1, 1);
935 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
939 pathname = (char *)&(ext2_inode->i_block[0]);
940 BUG_ON(pathname[inode_size] != 0);
941 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
942 pathname, inode_size + 1);
943 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
948 * Following xattr/acl related codes are based on codes in
949 * fs/ext3/xattr.c and fs/ext3/acl.c
951 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
952 #define EXT2_XATTR_BFIRST(ptr) \
953 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
954 #define EXT2_XATTR_IHDR(inode) \
955 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
956 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
957 #define EXT2_XATTR_IFIRST(inode) \
958 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
959 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
961 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
964 struct ext2_ext_attr_entry *next;
966 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
967 next = EXT2_EXT_ATTR_NEXT(entry);
968 if ((void *)next >= end)
975 static int ext2_xattr_check_block(const char *buf, size_t size)
978 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
980 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
981 header->h_blocks != 1)
983 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
987 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
990 size_t value_size = entry->e_value_size;
992 if (entry->e_value_block != 0 || value_size > size ||
993 entry->e_value_offs + value_size > size)
998 #define EXT2_ACL_VERSION 0x0001
1000 /* 23.2.5 acl_tag_t values */
1002 #define ACL_UNDEFINED_TAG (0x00)
1003 #define ACL_USER_OBJ (0x01)
1004 #define ACL_USER (0x02)
1005 #define ACL_GROUP_OBJ (0x04)
1006 #define ACL_GROUP (0x08)
1007 #define ACL_MASK (0x10)
1008 #define ACL_OTHER (0x20)
1010 /* 23.2.7 ACL qualifier constants */
1012 #define ACL_UNDEFINED_ID ((id_t)-1)
1023 } ext2_acl_entry_short;
1029 static inline int ext2_acl_count(size_t size)
1032 size -= sizeof(ext2_acl_header);
1033 s = size - 4 * sizeof(ext2_acl_entry_short);
1035 if (size % sizeof(ext2_acl_entry_short))
1037 return size / sizeof(ext2_acl_entry_short);
1039 if (s % sizeof(ext2_acl_entry))
1041 return s / sizeof(ext2_acl_entry) + 4;
1045 #define ACL_EA_VERSION 0x0002
1055 acl_ea_entry a_entries[0];
1058 static inline size_t acl_ea_size(int count)
1060 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
1063 static int ext2_acl_to_xattr(void *dst, const void *src,
1064 size_t dst_size, size_t src_size)
1067 const void *end = src + src_size;
1068 acl_ea_header *ext_acl = (acl_ea_header *)dst;
1069 acl_ea_entry *dst_entry = ext_acl->a_entries;
1070 ext2_acl_entry *src_entry;
1072 if (src_size < sizeof(ext2_acl_header))
1074 if (((ext2_acl_header *)src)->a_version !=
1075 cpu_to_le32(EXT2_ACL_VERSION))
1077 src += sizeof(ext2_acl_header);
1078 count = ext2_acl_count(src_size);
1082 BUG_ON(dst_size < acl_ea_size(count));
1083 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
1084 for (i = 0; i < count; i++, dst_entry++) {
1085 src_entry = (ext2_acl_entry *)src;
1086 if (src + sizeof(ext2_acl_entry_short) > end)
1088 dst_entry->e_tag = src_entry->e_tag;
1089 dst_entry->e_perm = src_entry->e_perm;
1090 switch (le16_to_cpu(src_entry->e_tag)) {
1095 src += sizeof(ext2_acl_entry_short);
1096 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1100 src += sizeof(ext2_acl_entry);
1103 dst_entry->e_id = src_entry->e_id;
1116 static char *xattr_prefix_table[] = {
1118 [2] = "system.posix_acl_access",
1119 [3] = "system.posix_acl_default",
1124 static int copy_single_xattr(struct btrfs_trans_handle *trans,
1125 struct btrfs_root *root, u64 objectid,
1126 struct ext2_ext_attr_entry *entry,
1127 const void *data, u32 datalen)
1132 void *databuf = NULL;
1133 char namebuf[XATTR_NAME_MAX + 1];
1135 name_index = entry->e_name_index;
1136 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
1137 xattr_prefix_table[name_index] == NULL)
1139 name_len = strlen(xattr_prefix_table[name_index]) +
1141 if (name_len >= sizeof(namebuf))
1144 if (name_index == 2 || name_index == 3) {
1145 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
1146 databuf = malloc(bufsize);
1149 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
1155 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
1156 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
1157 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
1158 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
1159 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
1160 objectid - INO_OFFSET, name_len, namebuf);
1163 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
1164 data, datalen, objectid);
1170 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
1171 struct btrfs_root *root, u64 objectid,
1172 struct btrfs_inode_item *btrfs_inode,
1173 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
1179 u32 block_size = ext2_fs->blocksize;
1180 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
1181 struct ext2_inode_large *ext2_inode;
1182 struct ext2_ext_attr_entry *entry;
1184 char *buffer = NULL;
1185 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
1187 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
1188 ext2_inode = (struct ext2_inode_large *)inode_buf;
1190 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
1194 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
1197 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
1198 error_message(err));
1203 if (ext2_ino > ext2_fs->super->s_first_ino &&
1204 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1205 if (EXT2_GOOD_OLD_INODE_SIZE +
1206 ext2_inode->i_extra_isize > inode_size) {
1210 if (ext2_inode->i_extra_isize != 0 &&
1211 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
1212 EXT2_EXT_ATTR_MAGIC) {
1218 void *end = (void *)ext2_inode + inode_size;
1219 entry = EXT2_XATTR_IFIRST(ext2_inode);
1220 total = end - (void *)entry;
1221 ret = ext2_xattr_check_names(entry, end);
1224 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1225 ret = ext2_xattr_check_entry(entry, total);
1228 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
1229 entry->e_value_offs;
1230 datalen = entry->e_value_size;
1231 ret = copy_single_xattr(trans, root, objectid,
1232 entry, data, datalen);
1235 entry = EXT2_EXT_ATTR_NEXT(entry);
1239 if (ext2_inode->i_file_acl == 0)
1242 buffer = malloc(block_size);
1247 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
1249 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
1250 error_message(err));
1254 ret = ext2_xattr_check_block(buffer, block_size);
1258 entry = EXT2_XATTR_BFIRST(buffer);
1259 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1260 ret = ext2_xattr_check_entry(entry, block_size);
1263 data = buffer + entry->e_value_offs;
1264 datalen = entry->e_value_size;
1265 ret = copy_single_xattr(trans, root, objectid,
1266 entry, data, datalen);
1269 entry = EXT2_EXT_ATTR_NEXT(entry);
1273 if ((void *)ext2_inode != inode_buf)
1277 #define MINORBITS 20
1278 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
1280 static inline dev_t old_decode_dev(u16 val)
1282 return MKDEV((val >> 8) & 255, val & 255);
1285 static inline dev_t new_decode_dev(u32 dev)
1287 unsigned major = (dev & 0xfff00) >> 8;
1288 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
1289 return MKDEV(major, minor);
1292 static int copy_inode_item(struct btrfs_inode_item *dst,
1293 struct ext2_inode *src, u32 blocksize)
1295 btrfs_set_stack_inode_generation(dst, 1);
1296 btrfs_set_stack_inode_sequence(dst, 0);
1297 btrfs_set_stack_inode_transid(dst, 1);
1298 btrfs_set_stack_inode_size(dst, src->i_size);
1299 btrfs_set_stack_inode_nbytes(dst, 0);
1300 btrfs_set_stack_inode_block_group(dst, 0);
1301 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
1302 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
1303 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
1304 btrfs_set_stack_inode_mode(dst, src->i_mode);
1305 btrfs_set_stack_inode_rdev(dst, 0);
1306 btrfs_set_stack_inode_flags(dst, 0);
1307 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
1308 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
1309 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
1310 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
1311 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
1312 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
1313 btrfs_set_stack_timespec_sec(&dst->otime, 0);
1314 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
1316 if (S_ISDIR(src->i_mode)) {
1317 btrfs_set_stack_inode_size(dst, 0);
1318 btrfs_set_stack_inode_nlink(dst, 1);
1320 if (S_ISREG(src->i_mode)) {
1321 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1324 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1325 !S_ISLNK(src->i_mode)) {
1326 if (src->i_block[0]) {
1327 btrfs_set_stack_inode_rdev(dst,
1328 old_decode_dev(src->i_block[0]));
1330 btrfs_set_stack_inode_rdev(dst,
1331 new_decode_dev(src->i_block[1]));
1334 memset(&dst->reserved, 0, sizeof(dst->reserved));
1340 * copy a single inode. do all the required works, such as cloning
1341 * inode item, creating file extents and creating directory entries.
1343 static int copy_single_inode(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root, u64 objectid,
1345 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1346 struct ext2_inode *ext2_inode,
1347 int datacsum, int packing, int noxattr)
1350 struct btrfs_inode_item btrfs_inode;
1352 if (ext2_inode->i_links_count == 0)
1355 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1356 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1357 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1358 BTRFS_INODE_NODATASUM;
1359 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1362 switch (ext2_inode->i_mode & S_IFMT) {
1364 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1365 ext2_fs, ext2_ino, datacsum, packing);
1368 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1372 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1373 ext2_fs, ext2_ino, ext2_inode);
1383 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1388 return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1391 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1392 u64 src_bytenr, u32 num_bytes)
1396 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1398 buffer = malloc(num_bytes);
1401 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1402 if (ret != num_bytes)
1404 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1405 if (ret != num_bytes)
1415 * scan ext2's inode bitmap and copy all used inodes.
1417 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
1418 struct btrfs_root *root,
1419 int datacsum, int packing, int noxattr, struct task_ctx *p)
1421 ext2_filsys ext2_fs = cctx->fs_data;
1424 ext2_inode_scan ext2_scan;
1425 struct ext2_inode ext2_inode;
1426 ext2_ino_t ext2_ino;
1428 struct btrfs_trans_handle *trans;
1430 trans = btrfs_start_transaction(root, 1);
1433 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1435 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1438 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1440 /* no more inodes */
1443 /* skip special inode in ext2fs */
1444 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1445 ext2_ino != EXT2_ROOT_INO)
1447 objectid = ext2_ino + INO_OFFSET;
1448 ret = copy_single_inode(trans, root,
1449 objectid, ext2_fs, ext2_ino,
1450 &ext2_inode, datacsum, packing,
1452 p->cur_copy_inodes++;
1455 if (trans->blocks_used >= 4096) {
1456 ret = btrfs_commit_transaction(trans, root);
1458 trans = btrfs_start_transaction(root, 1);
1463 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1466 ret = btrfs_commit_transaction(trans, root);
1468 ext2fs_close_inode_scan(ext2_scan);
1473 static int ext2_test_block(struct btrfs_convert_context *cctx, u64 block)
1475 ext2_filsys ext2_fs = cctx->fs_data;
1477 BUG_ON(block != (u32)block);
1478 return ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block);
1482 * Construct a range of ext2fs image file.
1483 * scan block allocation bitmap, find all blocks used by the ext2fs
1484 * in this range and create file extents that point to these blocks.
1486 * Note: Before calling the function, no file extent points to blocks
1489 static int create_image_file_range(struct btrfs_trans_handle *trans,
1490 struct btrfs_root *root, u64 objectid,
1491 struct btrfs_inode_item *inode,
1492 u64 start_byte, u64 end_byte,
1493 struct btrfs_convert_context *cctx, int datacsum)
1495 u32 blocksize = cctx->blocksize;
1496 u32 block = start_byte / blocksize;
1497 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1499 struct blk_iterate_data data;
1501 init_blk_iterate_data(&data, trans, root, inode, objectid, datacsum);
1502 data.first_block = block;
1504 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1505 if (!convert_test_block(cctx, block))
1507 ret = block_iterate_proc(block, block, &data);
1511 if (data.num_blocks > 0) {
1512 ret = record_file_blocks(&data, data.first_block,
1513 data.disk_block, data.num_blocks);
1516 data.first_block += data.num_blocks;
1518 if (last_block > data.first_block) {
1519 ret = record_file_blocks(&data, data.first_block, 0,
1520 last_block - data.first_block);
1529 * Create the fs image file.
1531 static int create_image(struct btrfs_convert_context *cctx,
1532 struct btrfs_root *root, const char *name, int datacsum)
1535 struct btrfs_key key;
1536 struct btrfs_key location;
1537 struct btrfs_path path;
1538 struct btrfs_inode_item btrfs_inode;
1539 struct btrfs_inode_item *inode_item;
1540 struct extent_buffer *leaf;
1541 struct btrfs_fs_info *fs_info = root->fs_info;
1542 struct btrfs_root *extent_root = fs_info->extent_root;
1543 struct btrfs_trans_handle *trans;
1544 struct btrfs_extent_item *ei;
1545 struct btrfs_extent_inline_ref *iref;
1546 struct btrfs_extent_data_ref *dref;
1553 u64 flags = BTRFS_INODE_READONLY;
1554 u32 sectorsize = root->sectorsize;
1556 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1557 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1558 first_free &= ~((u64)sectorsize - 1);
1560 flags |= BTRFS_INODE_NODATASUM;
1562 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1563 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1564 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1565 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1566 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1567 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1568 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1569 btrfs_init_path(&path);
1570 trans = btrfs_start_transaction(root, 1);
1573 objectid = btrfs_root_dirid(&root->root_item);
1574 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1579 * copy blocks covered by extent #0 to new positions. extent #0 is
1580 * special, we can't rely on relocate_extents_range to relocate it.
1582 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1583 ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
1586 ret = copy_disk_extent(root, key.objectid, last_byte,
1590 ret = btrfs_record_file_extent(trans, root, objectid,
1591 &btrfs_inode, last_byte,
1592 key.objectid, sectorsize);
1596 ret = csum_disk_extent(trans, root, key.objectid,
1604 key.objectid = last_byte;
1606 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1607 ret = btrfs_search_slot(trans, fs_info->extent_root,
1612 leaf = path.nodes[0];
1613 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1614 ret = btrfs_next_leaf(extent_root, &path);
1619 leaf = path.nodes[0];
1621 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1622 if (last_byte > key.objectid ||
1623 key.type != BTRFS_EXTENT_ITEM_KEY) {
1628 bytenr = key.objectid;
1629 num_bytes = key.offset;
1630 ei = btrfs_item_ptr(leaf, path.slots[0],
1631 struct btrfs_extent_item);
1632 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1637 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1638 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1640 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1641 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1642 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1643 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1644 if (btrfs_extent_data_ref_root(leaf, dref) !=
1645 BTRFS_FS_TREE_OBJECTID) {
1650 if (bytenr > last_byte) {
1651 ret = create_image_file_range(trans, root, objectid,
1652 &btrfs_inode, last_byte,
1658 ret = btrfs_record_file_extent(trans, root, objectid,
1659 &btrfs_inode, bytenr, bytenr,
1663 last_byte = bytenr + num_bytes;
1664 btrfs_release_path(&path);
1666 if (trans->blocks_used >= 4096) {
1667 ret = btrfs_commit_transaction(trans, root);
1669 trans = btrfs_start_transaction(root, 1);
1673 btrfs_release_path(&path);
1674 if (total_bytes > last_byte) {
1675 ret = create_image_file_range(trans, root, objectid,
1676 &btrfs_inode, last_byte,
1683 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1687 location.objectid = objectid;
1688 location.offset = 0;
1689 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1690 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1691 btrfs_root_dirid(&root->root_item),
1692 &location, BTRFS_FT_REG_FILE, objectid);
1695 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1697 btrfs_root_dirid(&root->root_item),
1701 location.objectid = btrfs_root_dirid(&root->root_item);
1702 location.offset = 0;
1703 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1704 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1707 leaf = path.nodes[0];
1708 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1709 struct btrfs_inode_item);
1710 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1711 btrfs_inode_size(leaf, inode_item));
1712 btrfs_mark_buffer_dirty(leaf);
1713 btrfs_release_path(&path);
1714 ret = btrfs_commit_transaction(trans, root);
1717 btrfs_release_path(&path);
1721 static int create_image_file_range_v2(struct btrfs_trans_handle *trans,
1722 struct btrfs_root *root,
1723 struct cache_tree *used,
1724 struct btrfs_inode_item *inode,
1725 u64 ino, u64 bytenr, u64 *ret_len,
1728 struct cache_extent *cache;
1729 struct btrfs_block_group_cache *bg_cache;
1734 BUG_ON(bytenr != round_down(bytenr, root->sectorsize));
1735 BUG_ON(len != round_down(len, root->sectorsize));
1736 len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE);
1738 cache = search_cache_extent(used, bytenr);
1740 if (cache->start <= bytenr) {
1742 * |///////Used///////|
1746 len = min_t(u64, len, cache->start + cache->size -
1748 disk_bytenr = bytenr;
1755 len = min(len, cache->start - bytenr);
1770 /* Check if the range is in a data block group */
1771 bg_cache = btrfs_lookup_block_group(root->fs_info, bytenr);
1774 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
1777 /* The extent should never cross block group boundary */
1778 len = min_t(u64, len, bg_cache->key.objectid +
1779 bg_cache->key.offset - bytenr);
1782 BUG_ON(len != round_down(len, root->sectorsize));
1783 ret = btrfs_record_file_extent(trans, root, ino, inode, bytenr,
1789 ret = csum_disk_extent(trans, root, bytenr, len);
1796 * Relocate old fs data in one reserved ranges
1798 * Since all old fs data in reserved range is not covered by any chunk nor
1799 * data extent, we don't need to handle any reference but add new
1800 * extent/reference, which makes codes more clear
1802 static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
1803 struct btrfs_root *root,
1804 struct cache_tree *used,
1805 struct btrfs_inode_item *inode, int fd,
1806 u64 ino, u64 start, u64 len, int datacsum)
1808 u64 cur_off = start;
1810 struct cache_extent *cache;
1811 struct btrfs_key key;
1812 struct extent_buffer *eb;
1815 while (cur_off < start + len) {
1816 cache = lookup_cache_extent(used, cur_off, cur_len);
1819 cur_off = max(cache->start, cur_off);
1820 cur_len = min(cache->start + cache->size, start + len) -
1822 BUG_ON(cur_len < root->sectorsize);
1824 /* reserve extent for the data */
1825 ret = btrfs_reserve_extent(trans, root, cur_len, 0, 0, (u64)-1,
1830 eb = malloc(sizeof(*eb) + cur_len);
1836 ret = pread(fd, eb->data, cur_len, cur_off);
1837 if (ret < cur_len) {
1838 ret = (ret < 0 ? ret : -EIO);
1842 eb->start = key.objectid;
1843 eb->len = key.offset;
1845 /* Write the data */
1846 ret = write_and_map_eb(trans, root, eb);
1851 /* Now handle extent item and file extent things */
1852 ret = btrfs_record_file_extent(trans, root, ino, inode, cur_off,
1853 key.objectid, key.offset);
1856 /* Finally, insert csum items */
1858 ret = csum_disk_extent(trans, root, key.objectid,
1861 cur_off += key.offset;
1862 cur_len = start + len - cur_off;
1868 * Relocate the used ext2 data in reserved ranges
1870 * [btrfs_sb_offset(1), +BTRFS_STRIPE_LEN)
1871 * [btrfs_sb_offset(2), +BTRFS_STRIPE_LEN)
1873 static int migrate_reserved_ranges(struct btrfs_trans_handle *trans,
1874 struct btrfs_root *root,
1875 struct cache_tree *used,
1876 struct btrfs_inode_item *inode, int fd,
1877 u64 ino, u64 total_bytes, int datacsum)
1885 cur_len = 1024 * 1024;
1886 ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
1887 cur_off, cur_len, datacsum);
1891 /* second sb(fisrt sb is included in 0~1M) */
1892 cur_off = btrfs_sb_offset(1);
1893 cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
1894 if (cur_off < total_bytes)
1896 ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
1897 cur_off, cur_len, datacsum);
1902 cur_off = btrfs_sb_offset(2);
1903 cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
1904 if (cur_off < total_bytes)
1906 ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
1907 cur_off, cur_len, datacsum);
1911 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
1915 * Create the fs image file of old filesystem.
1917 * This is completely fs independent as we have cctx->used, only
1918 * need to create file extents pointing to all the positions.
1920 static int create_image_v2(struct btrfs_root *root,
1921 struct btrfs_mkfs_config *cfg,
1922 struct btrfs_convert_context *cctx, int fd,
1923 u64 size, char *name, int datacsum)
1925 struct btrfs_inode_item buf;
1926 struct btrfs_trans_handle *trans;
1927 struct btrfs_path *path = NULL;
1928 struct btrfs_key key;
1929 struct cache_extent *cache;
1930 struct cache_tree used_tmp;
1935 trans = btrfs_start_transaction(root, 1);
1939 cache_tree_init(&used_tmp);
1941 ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
1945 ret = btrfs_new_inode(trans, root, ino, 0600 | S_IFREG);
1948 ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
1949 strlen(name), BTRFS_FT_REG_FILE, NULL, 1);
1953 path = btrfs_alloc_path();
1959 key.type = BTRFS_INODE_ITEM_KEY;
1962 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1964 ret = (ret > 0 ? -ENOENT : ret);
1967 read_extent_buffer(path->nodes[0], &buf,
1968 btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
1970 btrfs_release_path(path);
1973 * Create a new used space cache, which doesn't contain the reserved
1976 for (cache = first_cache_extent(&cctx->used); cache;
1977 cache = next_cache_extent(cache)) {
1978 ret = add_cache_extent(&used_tmp, cache->start, cache->size);
1982 ret = wipe_reserved_ranges(&used_tmp, 0, 0);
1987 * Start from 1M, as 0~1M is reserved, and create_image_file_range_v2()
1988 * can't handle bytenr 0(will consider it as a hole)
1991 while (cur < size) {
1992 u64 len = size - cur;
1994 ret = create_image_file_range_v2(trans, root, &used_tmp,
1995 &buf, ino, cur, &len, datacsum);
2000 /* Handle the reserved ranges */
2001 ret = migrate_reserved_ranges(trans, root, &cctx->used, &buf, fd, ino,
2002 cfg->num_bytes, datacsum);
2006 key.type = BTRFS_INODE_ITEM_KEY;
2008 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2010 ret = (ret > 0 ? -ENOENT : ret);
2013 btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
2014 write_extent_buffer(path->nodes[0], &buf,
2015 btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
2018 free_extent_cache_tree(&used_tmp);
2019 btrfs_free_path(path);
2020 btrfs_commit_transaction(trans, root);
2024 static struct btrfs_root * link_subvol(struct btrfs_root *root,
2025 const char *base, u64 root_objectid)
2027 struct btrfs_trans_handle *trans;
2028 struct btrfs_fs_info *fs_info = root->fs_info;
2029 struct btrfs_root *tree_root = fs_info->tree_root;
2030 struct btrfs_root *new_root = NULL;
2031 struct btrfs_path *path;
2032 struct btrfs_inode_item *inode_item;
2033 struct extent_buffer *leaf;
2034 struct btrfs_key key;
2035 u64 dirid = btrfs_root_dirid(&root->root_item);
2037 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
2043 if (len == 0 || len > BTRFS_NAME_LEN)
2046 path = btrfs_alloc_path();
2049 key.objectid = dirid;
2050 key.type = BTRFS_DIR_INDEX_KEY;
2051 key.offset = (u64)-1;
2053 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2056 if (path->slots[0] > 0) {
2058 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2059 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
2060 index = key.offset + 1;
2062 btrfs_release_path(path);
2064 trans = btrfs_start_transaction(root, 1);
2067 key.objectid = dirid;
2069 key.type = BTRFS_INODE_ITEM_KEY;
2071 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
2073 leaf = path->nodes[0];
2074 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2075 struct btrfs_inode_item);
2077 key.objectid = root_objectid;
2078 key.offset = (u64)-1;
2079 key.type = BTRFS_ROOT_ITEM_KEY;
2081 memcpy(buf, base, len);
2082 for (i = 0; i < 1024; i++) {
2083 ret = btrfs_insert_dir_item(trans, root, buf, len,
2084 dirid, &key, BTRFS_FT_DIR, index);
2087 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
2088 if (len < 1 || len > BTRFS_NAME_LEN) {
2096 btrfs_set_inode_size(leaf, inode_item, len * 2 +
2097 btrfs_inode_size(leaf, inode_item));
2098 btrfs_mark_buffer_dirty(leaf);
2099 btrfs_release_path(path);
2101 /* add the backref first */
2102 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
2103 BTRFS_ROOT_BACKREF_KEY,
2104 root->root_key.objectid,
2105 dirid, index, buf, len);
2108 /* now add the forward ref */
2109 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
2110 BTRFS_ROOT_REF_KEY, root_objectid,
2111 dirid, index, buf, len);
2113 ret = btrfs_commit_transaction(trans, root);
2116 new_root = btrfs_read_fs_root(fs_info, &key);
2117 if (IS_ERR(new_root))
2120 btrfs_free_path(path);
2124 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
2125 struct btrfs_root *root)
2127 struct btrfs_fs_info *info = root->fs_info;
2128 struct btrfs_root *chunk_root = info->chunk_root;
2129 struct btrfs_root *extent_root = info->extent_root;
2130 struct btrfs_device *device;
2131 struct btrfs_block_group_cache *cache;
2132 struct btrfs_dev_extent *extent;
2133 struct extent_buffer *leaf;
2134 struct btrfs_chunk chunk;
2135 struct btrfs_key key;
2136 struct btrfs_path path;
2142 btrfs_init_path(&path);
2144 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2145 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2147 BUG_ON(list_empty(&info->fs_devices->devices));
2148 device = list_entry(info->fs_devices->devices.next,
2149 struct btrfs_device, dev_list);
2150 BUG_ON(device->devid != info->fs_devices->latest_devid);
2152 /* delete device extent created by make_btrfs */
2153 key.objectid = device->devid;
2155 key.type = BTRFS_DEV_EXTENT_KEY;
2156 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
2161 ret = btrfs_del_item(trans, device->dev_root, &path);
2164 btrfs_release_path(&path);
2166 /* delete chunk item created by make_btrfs */
2167 key.objectid = chunk_objectid;
2169 key.type = BTRFS_CHUNK_ITEM_KEY;
2170 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
2175 ret = btrfs_del_item(trans, chunk_root, &path);
2178 btrfs_release_path(&path);
2180 /* for each block group, create device extent and chunk item */
2182 while (cur_start < total_bytes) {
2183 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2186 /* insert device extent */
2187 key.objectid = device->devid;
2188 key.offset = cache->key.objectid;
2189 key.type = BTRFS_DEV_EXTENT_KEY;
2190 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
2191 &key, sizeof(*extent));
2195 leaf = path.nodes[0];
2196 extent = btrfs_item_ptr(leaf, path.slots[0],
2197 struct btrfs_dev_extent);
2199 btrfs_set_dev_extent_chunk_tree(leaf, extent,
2200 chunk_root->root_key.objectid);
2201 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
2203 btrfs_set_dev_extent_chunk_offset(leaf, extent,
2204 cache->key.objectid);
2205 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
2206 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
2207 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
2209 btrfs_mark_buffer_dirty(leaf);
2210 btrfs_release_path(&path);
2212 /* insert chunk item */
2213 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
2214 btrfs_set_stack_chunk_owner(&chunk,
2215 extent_root->root_key.objectid);
2216 btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
2217 btrfs_set_stack_chunk_type(&chunk, cache->flags);
2218 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
2219 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
2220 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
2221 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
2222 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
2223 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
2224 btrfs_set_stack_stripe_offset(&chunk.stripe,
2225 cache->key.objectid);
2226 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2228 key.objectid = chunk_objectid;
2229 key.offset = cache->key.objectid;
2230 key.type = BTRFS_CHUNK_ITEM_KEY;
2232 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
2233 btrfs_chunk_item_size(1));
2237 cur_start = cache->key.objectid + cache->key.offset;
2240 device->bytes_used = total_bytes;
2241 ret = btrfs_update_device(trans, device);
2243 btrfs_release_path(&path);
2247 static int create_subvol(struct btrfs_trans_handle *trans,
2248 struct btrfs_root *root, u64 root_objectid)
2250 struct extent_buffer *tmp;
2251 struct btrfs_root *new_root;
2252 struct btrfs_key key;
2253 struct btrfs_root_item root_item;
2256 ret = btrfs_copy_root(trans, root, root->node, &tmp,
2260 memcpy(&root_item, &root->root_item, sizeof(root_item));
2261 btrfs_set_root_bytenr(&root_item, tmp->start);
2262 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
2263 btrfs_set_root_generation(&root_item, trans->transid);
2264 free_extent_buffer(tmp);
2266 key.objectid = root_objectid;
2267 key.type = BTRFS_ROOT_ITEM_KEY;
2268 key.offset = trans->transid;
2269 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
2272 key.offset = (u64)-1;
2273 new_root = btrfs_read_fs_root(root->fs_info, &key);
2274 BUG_ON(!new_root || IS_ERR(new_root));
2276 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
2283 * New make_btrfs_v2() has handle system and meta chunks quite well.
2284 * So only need to add remaining data chunks.
2286 static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
2287 struct btrfs_fs_info *fs_info,
2288 struct btrfs_mkfs_config *cfg,
2289 struct btrfs_convert_context *cctx)
2291 struct btrfs_root *extent_root = fs_info->extent_root;
2292 struct cache_tree *data_chunks = &cctx->data_chunks;
2293 struct cache_extent *cache;
2298 * Don't create data chunk over 10% of the convert device
2299 * And for single chunk, don't create chunk larger than 1G.
2301 max_chunk_size = cfg->num_bytes / 10;
2302 max_chunk_size = min((u64)(1024 * 1024 * 1024), max_chunk_size);
2303 max_chunk_size = round_down(max_chunk_size, extent_root->sectorsize);
2305 for (cache = first_cache_extent(data_chunks); cache;
2306 cache = next_cache_extent(cache)) {
2307 u64 cur = cache->start;
2309 while (cur < cache->start + cache->size) {
2311 u64 cur_backup = cur;
2313 len = min(max_chunk_size,
2314 cache->start + cache->size - cur);
2315 ret = btrfs_alloc_data_chunk(trans, extent_root,
2317 BTRFS_BLOCK_GROUP_DATA, 1);
2320 ret = btrfs_make_block_group(trans, extent_root, 0,
2321 BTRFS_BLOCK_GROUP_DATA,
2322 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2333 * Init the temp btrfs to a operational status.
2335 * It will fix the extent usage accounting(XXX: Do we really need?) and
2336 * insert needed data chunks, to ensure all old fs data extents are covered
2337 * by DATA chunks, preventing wrong chunks are allocated.
2339 * And also create convert image subvolume and relocation tree.
2340 * (XXX: Not need again?)
2341 * But the convert image subvolume is *NOT* linked to fs tree yet.
2343 static int init_btrfs_v2(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
2344 struct btrfs_convert_context *cctx, int datacsum,
2345 int packing, int noxattr)
2347 struct btrfs_key location;
2348 struct btrfs_trans_handle *trans;
2349 struct btrfs_fs_info *fs_info = root->fs_info;
2352 trans = btrfs_start_transaction(root, 1);
2354 ret = btrfs_fix_block_accounting(trans, root);
2357 ret = make_convert_data_block_groups(trans, fs_info, cfg, cctx);
2360 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
2361 BTRFS_ROOT_TREE_DIR_OBJECTID);
2364 memcpy(&location, &root->root_key, sizeof(location));
2365 location.offset = (u64)-1;
2366 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
2367 btrfs_super_root_dir(fs_info->super_copy),
2368 &location, BTRFS_FT_DIR, 0);
2371 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
2373 btrfs_super_root_dir(fs_info->super_copy), 0);
2376 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
2377 BTRFS_FIRST_FREE_OBJECTID);
2379 /* subvol for fs image file */
2380 ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
2383 /* subvol for data relocation tree */
2384 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
2388 ret = btrfs_commit_transaction(trans, root);
2393 static int init_btrfs(struct btrfs_root *root)
2396 struct btrfs_key location;
2397 struct btrfs_trans_handle *trans;
2398 struct btrfs_fs_info *fs_info = root->fs_info;
2399 struct extent_buffer *tmp;
2401 trans = btrfs_start_transaction(root, 1);
2403 ret = btrfs_make_block_groups(trans, root);
2406 ret = btrfs_fix_block_accounting(trans, root);
2409 ret = create_chunk_mapping(trans, root);
2412 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
2413 BTRFS_ROOT_TREE_DIR_OBJECTID);
2416 memcpy(&location, &root->root_key, sizeof(location));
2417 location.offset = (u64)-1;
2418 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
2419 btrfs_super_root_dir(fs_info->super_copy),
2420 &location, BTRFS_FT_DIR, 0);
2423 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
2425 btrfs_super_root_dir(fs_info->super_copy), 0);
2428 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
2429 BTRFS_FIRST_FREE_OBJECTID);
2431 /* subvol for fs image file */
2432 ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
2434 /* subvol for data relocation */
2435 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
2438 extent_buffer_get(fs_info->csum_root->node);
2439 ret = __btrfs_cow_block(trans, fs_info->csum_root,
2440 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
2442 free_extent_buffer(tmp);
2444 ret = btrfs_commit_transaction(trans, root);
2451 * Migrate super block to its default position and zero 0 ~ 16k
2453 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
2456 struct extent_buffer *buf;
2457 struct btrfs_super_block *super;
2461 BUG_ON(sectorsize < sizeof(*super));
2462 buf = malloc(sizeof(*buf) + sectorsize);
2466 buf->len = sectorsize;
2467 ret = pread(fd, buf->data, sectorsize, old_bytenr);
2468 if (ret != sectorsize)
2471 super = (struct btrfs_super_block *)buf->data;
2472 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
2473 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
2475 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2476 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2477 if (ret != sectorsize)
2484 memset(buf->data, 0, sectorsize);
2485 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
2486 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
2487 if (len > sectorsize)
2489 ret = pwrite(fd, buf->data, len, bytenr);
2491 fprintf(stderr, "unable to zero fill device\n");
2505 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
2507 struct btrfs_chunk *chunk;
2508 struct btrfs_disk_key *key;
2509 u32 sectorsize = btrfs_super_sectorsize(super);
2511 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
2512 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
2513 sizeof(struct btrfs_disk_key));
2515 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2516 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
2517 btrfs_set_disk_key_offset(key, 0);
2519 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
2520 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
2521 btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
2522 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
2523 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
2524 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
2525 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
2526 btrfs_set_stack_chunk_num_stripes(chunk, 1);
2527 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
2528 chunk->stripe.devid = super->dev_item.devid;
2529 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
2530 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
2531 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
2535 static int prepare_system_chunk(int fd, u64 sb_bytenr)
2538 struct extent_buffer *buf;
2539 struct btrfs_super_block *super;
2541 BUG_ON(BTRFS_SUPER_INFO_SIZE < sizeof(*super));
2542 buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
2546 buf->len = BTRFS_SUPER_INFO_SIZE;
2547 ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2548 if (ret != BTRFS_SUPER_INFO_SIZE)
2551 super = (struct btrfs_super_block *)buf->data;
2552 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
2553 BUG_ON(btrfs_super_num_devices(super) != 1);
2555 ret = prepare_system_chunk_sb(super);
2559 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2560 ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2561 if (ret != BTRFS_SUPER_INFO_SIZE)
2572 static int relocate_one_reference(struct btrfs_trans_handle *trans,
2573 struct btrfs_root *root,
2574 u64 extent_start, u64 extent_size,
2575 struct btrfs_key *extent_key,
2576 struct extent_io_tree *reloc_tree)
2578 struct extent_buffer *leaf;
2579 struct btrfs_file_extent_item *fi;
2580 struct btrfs_key key;
2581 struct btrfs_path path;
2582 struct btrfs_inode_item inode;
2583 struct blk_iterate_data data;
2590 u32 sectorsize = root->sectorsize;
2596 btrfs_init_path(&path);
2597 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
2601 leaf = path.nodes[0];
2602 fi = btrfs_item_ptr(leaf, path.slots[0],
2603 struct btrfs_file_extent_item);
2604 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
2605 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
2606 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
2611 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
2612 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2614 ret = btrfs_del_item(trans, root, &path);
2618 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
2619 root->root_key.objectid,
2620 extent_key->objectid, extent_key->offset);
2624 btrfs_release_path(&path);
2626 key.objectid = extent_key->objectid;
2628 key.type = BTRFS_INODE_ITEM_KEY;
2629 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
2633 leaf = path.nodes[0];
2634 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2635 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2636 btrfs_release_path(&path);
2638 BUG_ON(num_bytes & (sectorsize - 1));
2639 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
2640 btrfs_set_stack_inode_nbytes(&inode, nbytes);
2641 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
2643 init_blk_iterate_data(&data, trans, root, &inode, extent_key->objectid,
2645 data.first_block = extent_key->offset;
2647 cur_offset = extent_key->offset;
2648 while (num_bytes > 0) {
2649 sector_end = bytenr + sectorsize - 1;
2650 if (test_range_bit(reloc_tree, bytenr, sector_end,
2651 EXTENT_LOCKED, 1)) {
2652 ret = get_state_private(reloc_tree, bytenr, &new_pos);
2655 ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
2658 new_pos = key.objectid;
2660 if (cur_offset == extent_key->offset) {
2661 fd = root->fs_info->fs_devices->latest_bdev;
2662 readahead(fd, bytenr, num_bytes);
2664 ret = copy_disk_extent(root, new_pos, bytenr,
2668 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
2669 EXTENT_LOCKED, GFP_NOFS);
2671 ret = set_state_private(reloc_tree, bytenr, new_pos);
2675 ret = block_iterate_proc(new_pos / sectorsize,
2676 cur_offset / sectorsize, &data);
2680 cur_offset += sectorsize;
2681 bytenr += sectorsize;
2682 num_bytes -= sectorsize;
2685 if (data.num_blocks > 0) {
2686 ret = record_file_blocks(&data, data.first_block,
2687 data.disk_block, data.num_blocks);
2692 key.objectid = extent_key->objectid;
2694 key.type = BTRFS_INODE_ITEM_KEY;
2695 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
2699 leaf = path.nodes[0];
2700 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2701 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2702 btrfs_mark_buffer_dirty(leaf);
2703 btrfs_release_path(&path);
2706 btrfs_release_path(&path);
2710 static int relocate_extents_range(struct btrfs_root *fs_root,
2711 struct btrfs_root *image_root,
2712 u64 start_byte, u64 end_byte)
2714 struct btrfs_fs_info *info = fs_root->fs_info;
2715 struct btrfs_root *extent_root = info->extent_root;
2716 struct btrfs_root *cur_root = NULL;
2717 struct btrfs_trans_handle *trans;
2718 struct btrfs_extent_data_ref *dref;
2719 struct btrfs_extent_inline_ref *iref;
2720 struct btrfs_extent_item *ei;
2721 struct extent_buffer *leaf;
2722 struct btrfs_key key;
2723 struct btrfs_key extent_key;
2724 struct btrfs_path path;
2725 struct extent_io_tree reloc_tree;
2735 btrfs_init_path(&path);
2736 extent_io_tree_init(&reloc_tree);
2738 key.objectid = start_byte;
2740 key.type = BTRFS_EXTENT_ITEM_KEY;
2741 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2745 ret = btrfs_previous_item(extent_root, &path, 0,
2746 BTRFS_EXTENT_ITEM_KEY);
2750 leaf = path.nodes[0];
2751 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2752 if (key.objectid + key.offset > start_byte)
2753 start_byte = key.objectid;
2756 btrfs_release_path(&path);
2758 cur_root = (pass % 2 == 0) ? image_root : fs_root;
2761 trans = btrfs_start_transaction(cur_root, 1);
2764 cur_byte = start_byte;
2766 key.objectid = cur_byte;
2768 key.type = BTRFS_EXTENT_ITEM_KEY;
2769 ret = btrfs_search_slot(trans, extent_root,
2774 leaf = path.nodes[0];
2775 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2776 ret = btrfs_next_leaf(extent_root, &path);
2781 leaf = path.nodes[0];
2784 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2785 if (key.objectid < cur_byte ||
2786 key.type != BTRFS_EXTENT_ITEM_KEY) {
2790 if (key.objectid >= end_byte)
2795 cur_byte = key.objectid;
2796 num_bytes = key.offset;
2797 ei = btrfs_item_ptr(leaf, path.slots[0],
2798 struct btrfs_extent_item);
2799 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2800 BTRFS_EXTENT_FLAG_DATA));
2802 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2803 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2805 ptr += sizeof(struct btrfs_extent_item);
2808 iref = (struct btrfs_extent_inline_ref *)ptr;
2809 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2810 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2811 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2812 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2813 extent_key.objectid =
2814 btrfs_extent_data_ref_objectid(leaf, dref);
2816 btrfs_extent_data_ref_offset(leaf, dref);
2817 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2818 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2820 if (ref_root == cur_root->root_key.objectid)
2823 ptr += btrfs_extent_inline_ref_size(key.type);
2831 ret = relocate_one_reference(trans, cur_root, cur_byte,
2832 num_bytes, &extent_key,
2837 cur_byte += num_bytes;
2838 btrfs_release_path(&path);
2840 if (trans->blocks_used >= 4096) {
2841 ret = btrfs_commit_transaction(trans, cur_root);
2843 trans = btrfs_start_transaction(cur_root, 1);
2847 btrfs_release_path(&path);
2849 ret = btrfs_commit_transaction(trans, cur_root);
2852 if (num_extents > 0 && pass++ < 16)
2855 ret = (num_extents > 0) ? -1 : 0;
2857 btrfs_release_path(&path);
2858 extent_io_tree_cleanup(&reloc_tree);
2863 * relocate data in system chunk
2865 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2866 struct btrfs_root *image_root)
2868 struct btrfs_block_group_cache *cache;
2874 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2878 end_byte = cache->key.objectid + cache->key.offset;
2879 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2880 ret = relocate_extents_range(fs_root, image_root,
2881 cache->key.objectid,
2888 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2889 offset = btrfs_sb_offset(i);
2890 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2892 ret = relocate_extents_range(fs_root, image_root,
2893 offset, offset + BTRFS_STRIPE_LEN);
2902 static int fixup_chunk_mapping(struct btrfs_root *root)
2904 struct btrfs_trans_handle *trans;
2905 struct btrfs_fs_info *info = root->fs_info;
2906 struct btrfs_root *chunk_root = info->chunk_root;
2907 struct extent_buffer *leaf;
2908 struct btrfs_key key;
2909 struct btrfs_path path;
2910 struct btrfs_chunk chunk;
2916 btrfs_init_path(&path);
2918 trans = btrfs_start_transaction(root, 1);
2922 * recow the whole chunk tree. this will move all chunk tree blocks
2923 * into system block group.
2925 memset(&key, 0, sizeof(key));
2927 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2931 ret = btrfs_next_leaf(chunk_root, &path);
2937 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2938 btrfs_release_path(&path);
2940 btrfs_release_path(&path);
2942 /* fixup the system chunk array in super block */
2943 btrfs_set_super_sys_array_size(info->super_copy, 0);
2945 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2947 key.type = BTRFS_CHUNK_ITEM_KEY;
2949 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2954 leaf = path.nodes[0];
2955 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2956 ret = btrfs_next_leaf(chunk_root, &path);
2961 leaf = path.nodes[0];
2963 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2964 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2967 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2968 size = btrfs_item_size_nr(leaf, path.slots[0]);
2969 BUG_ON(size != sizeof(chunk));
2970 read_extent_buffer(leaf, &chunk, ptr, size);
2971 type = btrfs_stack_chunk_type(&chunk);
2973 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2976 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2984 ret = btrfs_commit_transaction(trans, root);
2987 btrfs_release_path(&path);
2991 static const struct btrfs_convert_operations ext2_convert_ops = {
2993 .open_fs = ext2_open_fs,
2994 .read_used_space = ext2_read_used_space,
2995 .alloc_block = ext2_alloc_block,
2996 .alloc_block_range = ext2_alloc_block_range,
2997 .copy_inodes = ext2_copy_inodes,
2998 .test_block = ext2_test_block,
2999 .free_block = ext2_free_block,
3000 .free_block_range = ext2_free_block_range,
3001 .close_fs = ext2_close_fs,
3004 static const struct btrfs_convert_operations *convert_operations[] = {
3008 static int convert_open_fs(const char *devname,
3009 struct btrfs_convert_context *cctx)
3013 memset(cctx, 0, sizeof(*cctx));
3015 for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
3016 int ret = convert_operations[i]->open_fs(cctx, devname);
3019 cctx->convert_ops = convert_operations[i];
3024 fprintf(stderr, "No file system found to convert.\n");
3029 * Remove one reserve range from given cache tree
3030 * if min_stripe_size is non-zero, it will ensure for split case,
3031 * all its split cache extent is no smaller than @min_strip_size / 2.
3033 static int wipe_one_reserved_range(struct cache_tree *tree,
3034 u64 start, u64 len, u64 min_stripe_size,
3037 struct cache_extent *cache;
3040 BUG_ON(ensure_size && min_stripe_size == 0);
3042 * The logical here is simplified to handle special cases only
3043 * So we don't need to consider merge case for ensure_size
3045 BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
3046 min_stripe_size / 2 < BTRFS_STRIPE_LEN));
3048 /* Also, wipe range should already be aligned */
3049 BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
3050 start + len != round_up(start + len, BTRFS_STRIPE_LEN));
3052 min_stripe_size /= 2;
3054 cache = lookup_cache_extent(tree, start, len);
3058 if (start <= cache->start) {
3060 * |--------cache---------|
3063 BUG_ON(start + len <= cache->start);
3066 * The wipe size is smaller than min_stripe_size / 2,
3067 * so the result length should still meet min_stripe_size
3068 * And no need to do alignment
3070 cache->size -= (start + len - cache->start);
3071 if (cache->size == 0) {
3072 remove_cache_extent(tree, cache);
3077 BUG_ON(ensure_size && cache->size < min_stripe_size);
3079 cache->start = start + len;
3081 } else if (start > cache->start && start + len < cache->start +
3084 * |-------cache-----|
3087 u64 old_len = cache->size;
3088 u64 insert_start = start + len;
3091 cache->size = start - cache->start;
3093 cache->size = max(cache->size, min_stripe_size);
3094 cache->start = start - cache->size;
3096 /* And insert the new one */
3097 insert_len = old_len - start - len;
3099 insert_len = max(insert_len, min_stripe_size);
3101 ret = add_merge_cache_extent(tree, insert_start, insert_len);
3107 * Wipe len should be small enough and no need to expand the
3110 cache->size = start - cache->start;
3111 BUG_ON(ensure_size && cache->size < min_stripe_size);
3116 * Remove reserved ranges from given cache_tree
3118 * It will remove the following ranges
3120 * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
3121 * 3) 3rd superblock, +64K
3123 * @min_stripe must be given for safety check
3124 * and if @ensure_size is given, it will ensure affected cache_extent will be
3125 * larger than min_stripe_size
3127 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
3132 ret = wipe_one_reserved_range(tree, 0, 1024 * 1024, min_stripe_size,
3136 ret = wipe_one_reserved_range(tree, btrfs_sb_offset(1),
3137 BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
3140 ret = wipe_one_reserved_range(tree, btrfs_sb_offset(2),
3141 BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
3145 static int calculate_available_space(struct btrfs_convert_context *cctx)
3147 struct cache_tree *used = &cctx->used;
3148 struct cache_tree *data_chunks = &cctx->data_chunks;
3149 struct cache_tree *free = &cctx->free;
3150 struct cache_extent *cache;
3153 * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
3154 * works without need to consider overlap
3156 u64 min_stripe_size = 2 * 16 * 1024 * 1024;
3159 /* Calculate data_chunks */
3160 for (cache = first_cache_extent(used); cache;
3161 cache = next_cache_extent(cache)) {
3164 if (cache->start + cache->size < cur_off)
3166 if (cache->start > cur_off + min_stripe_size)
3167 cur_off = cache->start;
3168 cur_len = max(cache->start + cache->size - cur_off,
3170 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
3176 * remove reserved ranges, so we won't ever bother relocating an old
3177 * filesystem extent to other place.
3179 ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
3185 * Calculate free space
3186 * Always round up the start bytenr, to avoid metadata extent corss
3187 * stripe boundary, as later mkfs_convert() won't have all the extent
3190 for (cache = first_cache_extent(data_chunks); cache;
3191 cache = next_cache_extent(cache)) {
3192 if (cache->start < cur_off)
3194 if (cache->start > cur_off) {
3198 len = cache->start - round_up(cur_off,
3200 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
3202 ret = add_merge_cache_extent(free, insert_start, len);
3206 cur_off = cache->start + cache->size;
3208 /* Don't forget the last range */
3209 if (cctx->total_bytes > cur_off) {
3210 u64 len = cctx->total_bytes - cur_off;
3213 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
3215 ret = add_merge_cache_extent(free, insert_start, len);
3220 /* Remove reserved bytes */
3221 ret = wipe_reserved_ranges(free, min_stripe_size, 0);
3226 * Read used space, and since we have the used space,
3227 * calcuate data_chunks and free for later mkfs
3229 static int convert_read_used_space(struct btrfs_convert_context *cctx)
3233 ret = cctx->convert_ops->read_used_space(cctx);
3237 ret = calculate_available_space(cctx);
3241 static int do_convert_v2(const char *devname, int datacsum, int packing,
3242 int noxattr, u32 nodesize, int copylabel, const char *fslabel,
3243 int progress, u64 features)
3250 struct btrfs_root *root;
3251 struct btrfs_root *image_root;
3252 struct btrfs_convert_context cctx;
3253 struct btrfs_key key;
3254 char *subvol_name = NULL;
3255 struct task_ctx ctx;
3256 char features_buf[64];
3257 struct btrfs_mkfs_config mkfs_cfg;
3259 init_convert_context(&cctx);
3260 ret = convert_open_fs(devname, &cctx);
3263 ret = convert_read_used_space(&cctx);
3267 blocksize = cctx.blocksize;
3268 total_bytes = (u64)blocksize * (u64)cctx.block_count;
3269 if (blocksize < 4096) {
3270 fprintf(stderr, "block size is too small\n");
3273 if (btrfs_check_nodesize(nodesize, blocksize, features))
3275 fd = open(devname, O_RDWR);
3277 fprintf(stderr, "unable to open %s\n", devname);
3280 btrfs_parse_features_to_string(features_buf, features);
3281 if (features == BTRFS_MKFS_DEFAULT_FEATURES)
3282 strcat(features_buf, " (default)");
3284 printf("create btrfs filesystem:\n");
3285 printf("\tblocksize: %u\n", blocksize);
3286 printf("\tnodesize: %u\n", nodesize);
3287 printf("\tfeatures: %s\n", features_buf);
3289 mkfs_cfg.label = cctx.volume_name;
3290 mkfs_cfg.num_bytes = total_bytes;
3291 mkfs_cfg.nodesize = nodesize;
3292 mkfs_cfg.sectorsize = blocksize;
3293 mkfs_cfg.stripesize = blocksize;
3294 mkfs_cfg.features = features;
3295 /* New convert need these space */
3296 mkfs_cfg.fs_uuid = malloc(BTRFS_UUID_UNPARSED_SIZE);
3297 mkfs_cfg.chunk_uuid = malloc(BTRFS_UUID_UNPARSED_SIZE);
3298 *(mkfs_cfg.fs_uuid) = '\0';
3299 *(mkfs_cfg.chunk_uuid) = '\0';
3301 ret = make_btrfs(fd, &mkfs_cfg, &cctx);
3303 fprintf(stderr, "unable to create initial ctree: %s\n",
3308 root = open_ctree_fd(fd, devname, mkfs_cfg.super_bytenr,
3311 fprintf(stderr, "unable to open ctree\n");
3314 ret = init_btrfs_v2(&mkfs_cfg, root, &cctx, datacsum, packing, noxattr);
3316 fprintf(stderr, "unable to setup the root tree\n");
3320 printf("creating %s image file.\n", cctx.convert_ops->name);
3321 ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
3323 fprintf(stderr, "error allocating subvolume name: %s_saved\n",
3324 cctx.convert_ops->name);
3327 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3328 key.offset = (u64)-1;
3329 key.type = BTRFS_ROOT_ITEM_KEY;
3330 image_root = btrfs_read_fs_root(root->fs_info, &key);
3332 fprintf(stderr, "unable to create subvol\n");
3335 ret = create_image_v2(image_root, &mkfs_cfg, &cctx, fd,
3336 mkfs_cfg.num_bytes, "image", datacsum);
3338 fprintf(stderr, "error during create_image %d\n", ret);
3342 printf("creating btrfs metadata.\n");
3343 ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
3344 ctx.cur_copy_inodes = 0;
3347 ctx.info = task_init(print_copied_inodes, after_copied_inodes,
3349 task_start(ctx.info);
3351 ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
3353 fprintf(stderr, "error during copy_inodes %d\n", ret);
3357 task_stop(ctx.info);
3358 task_deinit(ctx.info);
3361 image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
3365 memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
3366 if (copylabel == 1) {
3367 __strncpy_null(root->fs_info->super_copy->label,
3368 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
3369 fprintf(stderr, "copy label '%s'\n",
3370 root->fs_info->super_copy->label);
3371 } else if (copylabel == -1) {
3372 strcpy(root->fs_info->super_copy->label, fslabel);
3373 fprintf(stderr, "set label to '%s'\n", fslabel);
3376 ret = close_ctree(root);
3378 fprintf(stderr, "error during close_ctree %d\n", ret);
3381 convert_close_fs(&cctx);
3382 clean_convert_context(&cctx);
3385 * If this step succeed, we get a mountable btrfs. Otherwise
3386 * the source fs is left unchanged.
3388 ret = migrate_super_block(fd, mkfs_cfg.super_bytenr, blocksize);
3390 fprintf(stderr, "unable to migrate super block\n");
3395 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3397 fprintf(stderr, "unable to open ctree\n");
3402 printf("conversion complete.\n");
3405 clean_convert_context(&cctx);
3410 "WARNING: an error occurred during chunk mapping fixup, filesystem mountable but not finalized\n");
3412 fprintf(stderr, "conversion aborted\n");
3416 static int do_convert(const char *devname, int datacsum, int packing, int noxattr,
3417 u32 nodesize, int copylabel, const char *fslabel, int progress,
3420 int i, ret, blocks_per_node;
3427 struct btrfs_root *root;
3428 struct btrfs_root *image_root;
3429 struct btrfs_convert_context cctx;
3430 char *subvol_name = NULL;
3431 struct task_ctx ctx;
3432 char features_buf[64];
3433 struct btrfs_mkfs_config mkfs_cfg;
3435 init_convert_context(&cctx);
3436 ret = convert_open_fs(devname, &cctx);
3439 ret = convert_read_used_space(&cctx);
3443 blocksize = cctx.blocksize;
3444 total_bytes = (u64)blocksize * (u64)cctx.block_count;
3445 if (blocksize < 4096) {
3446 fprintf(stderr, "block size is too small\n");
3449 if (btrfs_check_nodesize(nodesize, blocksize, features))
3451 blocks_per_node = nodesize / blocksize;
3452 ret = -blocks_per_node;
3453 for (i = 0; i < 7; i++) {
3454 if (nodesize == blocksize)
3455 ret = convert_alloc_block(&cctx, 0, blocks + i);
3457 ret = convert_alloc_block_range(&cctx,
3458 ret + blocks_per_node, blocks_per_node,
3461 fprintf(stderr, "not enough free space\n");
3464 blocks[i] *= blocksize;
3466 super_bytenr = blocks[0];
3467 fd = open(devname, O_RDWR);
3469 fprintf(stderr, "unable to open %s\n", devname);
3472 btrfs_parse_features_to_string(features_buf, features);
3473 if (features == BTRFS_MKFS_DEFAULT_FEATURES)
3474 strcat(features_buf, " (default)");
3476 printf("create btrfs filesystem:\n");
3477 printf("\tblocksize: %u\n", blocksize);
3478 printf("\tnodesize: %u\n", nodesize);
3479 printf("\tfeatures: %s\n", features_buf);
3481 mkfs_cfg.label = cctx.volume_name;
3482 mkfs_cfg.fs_uuid = NULL;
3483 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
3484 mkfs_cfg.num_bytes = total_bytes;
3485 mkfs_cfg.nodesize = nodesize;
3486 mkfs_cfg.sectorsize = blocksize;
3487 mkfs_cfg.stripesize = blocksize;
3488 mkfs_cfg.features = features;
3490 ret = make_btrfs(fd, &mkfs_cfg, NULL);
3492 fprintf(stderr, "unable to create initial ctree: %s\n",
3496 /* create a system chunk that maps the whole device */
3497 ret = prepare_system_chunk(fd, super_bytenr);
3499 fprintf(stderr, "unable to update system chunk\n");
3502 root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
3504 fprintf(stderr, "unable to open ctree\n");
3507 ret = cache_free_extents(root, &cctx);
3509 fprintf(stderr, "error during cache_free_extents %d\n", ret);
3512 root->fs_info->extent_ops = &extent_ops;
3513 /* recover block allocation bitmap */
3514 for (i = 0; i < 7; i++) {
3515 blocks[i] /= blocksize;
3516 if (nodesize == blocksize)
3517 convert_free_block(&cctx, blocks[i]);
3519 convert_free_block_range(&cctx, blocks[i],
3522 ret = init_btrfs(root);
3524 fprintf(stderr, "unable to setup the root tree\n");
3527 printf("creating btrfs metadata.\n");
3528 ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
3529 ctx.cur_copy_inodes = 0;
3532 ctx.info = task_init(print_copied_inodes, after_copied_inodes, &ctx);
3533 task_start(ctx.info);
3535 ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
3537 fprintf(stderr, "error during copy_inodes %d\n", ret);
3541 task_stop(ctx.info);
3542 task_deinit(ctx.info);
3545 printf("creating %s image file.\n", cctx.convert_ops->name);
3546 ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
3548 fprintf(stderr, "error allocating subvolume name: %s_saved\n",
3549 cctx.convert_ops->name);
3553 image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
3558 fprintf(stderr, "unable to create subvol\n");
3561 ret = create_image(&cctx, image_root, "image", datacsum);
3563 fprintf(stderr, "error during create_image %d\n", ret);
3566 memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
3567 if (copylabel == 1) {
3568 __strncpy_null(root->fs_info->super_copy->label,
3569 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
3570 fprintf(stderr, "copy label '%s'\n",
3571 root->fs_info->super_copy->label);
3572 } else if (copylabel == -1) {
3573 strcpy(root->fs_info->super_copy->label, fslabel);
3574 fprintf(stderr, "set label to '%s'\n", fslabel);
3577 printf("cleaning up system chunk.\n");
3578 ret = cleanup_sys_chunk(root, image_root);
3580 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
3583 ret = close_ctree(root);
3585 fprintf(stderr, "error during close_ctree %d\n", ret);
3588 convert_close_fs(&cctx);
3589 clean_convert_context(&cctx);
3592 * If this step succeed, we get a mountable btrfs. Otherwise
3593 * the source fs is left unchanged.
3595 ret = migrate_super_block(fd, super_bytenr, blocksize);
3597 fprintf(stderr, "unable to migrate super block\n");
3602 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3604 fprintf(stderr, "unable to open ctree\n");
3607 /* move chunk tree into system chunk. */
3608 ret = fixup_chunk_mapping(root);
3610 fprintf(stderr, "error during fixup_chunk_tree\n");
3613 ret = close_ctree(root);
3616 printf("conversion complete.\n");
3619 clean_convert_context(&cctx);
3624 "WARNING: an error occured during chunk mapping fixup, filesystem mountable but not finalized\n");
3626 fprintf(stderr, "conversion aborted\n");
3630 static int may_rollback(struct btrfs_root *root)
3632 struct btrfs_fs_info *info = root->fs_info;
3633 struct btrfs_multi_bio *multi = NULL;
3641 if (btrfs_super_num_devices(info->super_copy) != 1)
3644 bytenr = BTRFS_SUPER_INFO_OFFSET;
3645 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3648 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
3649 &length, &multi, 0, NULL);
3651 if (ret == -ENOENT) {
3652 /* removed block group at the tail */
3653 if (length == (u64)-1)
3656 /* removed block group in the middle */
3662 num_stripes = multi->num_stripes;
3663 physical = multi->stripes[0].physical;
3666 if (num_stripes != 1 || physical != bytenr)
3670 if (bytenr >= total_bytes)
3678 static int do_rollback(const char *devname)
3683 struct btrfs_root *root;
3684 struct btrfs_root *image_root;
3685 struct btrfs_root *chunk_root;
3686 struct btrfs_dir_item *dir;
3687 struct btrfs_inode_item *inode;
3688 struct btrfs_file_extent_item *fi;
3689 struct btrfs_trans_handle *trans;
3690 struct extent_buffer *leaf;
3691 struct btrfs_block_group_cache *cache1;
3692 struct btrfs_block_group_cache *cache2;
3693 struct btrfs_key key;
3694 struct btrfs_path path;
3695 struct extent_io_tree io_tree;
3710 extent_io_tree_init(&io_tree);
3712 fd = open(devname, O_RDWR);
3714 fprintf(stderr, "unable to open %s\n", devname);
3717 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3719 fprintf(stderr, "unable to open ctree\n");
3722 ret = may_rollback(root);
3724 fprintf(stderr, "unable to do rollback\n");
3728 sectorsize = root->sectorsize;
3729 buf = malloc(sectorsize);
3731 fprintf(stderr, "unable to allocate memory\n");
3735 btrfs_init_path(&path);
3737 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3738 key.type = BTRFS_ROOT_BACKREF_KEY;
3739 key.offset = BTRFS_FS_TREE_OBJECTID;
3740 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path, 0,
3742 btrfs_release_path(&path);
3745 "ERROR: unable to convert ext2 image subvolume, is it deleted?\n");
3747 } else if (ret < 0) {
3749 "ERROR: unable to open ext2_saved, id=%llu: %s\n",
3750 (unsigned long long)key.objectid, strerror(-ret));
3754 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3755 key.type = BTRFS_ROOT_ITEM_KEY;
3756 key.offset = (u64)-1;
3757 image_root = btrfs_read_fs_root(root->fs_info, &key);
3758 if (!image_root || IS_ERR(image_root)) {
3759 fprintf(stderr, "unable to open subvol %llu\n",
3760 (unsigned long long)key.objectid);
3765 root_dir = btrfs_root_dirid(&root->root_item);
3766 dir = btrfs_lookup_dir_item(NULL, image_root, &path,
3767 root_dir, name, strlen(name), 0);
3768 if (!dir || IS_ERR(dir)) {
3769 fprintf(stderr, "unable to find file %s\n", name);
3772 leaf = path.nodes[0];
3773 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
3774 btrfs_release_path(&path);
3776 objectid = key.objectid;
3778 ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
3780 fprintf(stderr, "unable to find inode item\n");
3783 leaf = path.nodes[0];
3784 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
3785 total_bytes = btrfs_inode_size(leaf, inode);
3786 btrfs_release_path(&path);
3788 key.objectid = objectid;
3790 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3791 ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
3793 fprintf(stderr, "unable to find first file extent\n");
3794 btrfs_release_path(&path);
3798 /* build mapping tree for the relocated blocks */
3799 for (offset = 0; offset < total_bytes; ) {
3800 leaf = path.nodes[0];
3801 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
3802 ret = btrfs_next_leaf(root, &path);
3808 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
3809 if (key.objectid != objectid || key.offset != offset ||
3810 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3813 fi = btrfs_item_ptr(leaf, path.slots[0],
3814 struct btrfs_file_extent_item);
3815 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
3817 if (btrfs_file_extent_compression(leaf, fi) ||
3818 btrfs_file_extent_encryption(leaf, fi) ||
3819 btrfs_file_extent_other_encoding(leaf, fi))
3822 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3823 /* skip holes and direct mapped extents */
3824 if (bytenr == 0 || bytenr == offset)
3827 bytenr += btrfs_file_extent_offset(leaf, fi);
3828 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3830 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3831 cache2 = btrfs_lookup_block_group(root->fs_info,
3832 offset + num_bytes - 1);
3834 * Here we must take consideration of old and new convert
3836 * For old convert case, sign, there is no consist chunk type
3837 * that will cover the extent. META/DATA/SYS are all possible.
3838 * Just ensure relocate one is in SYS chunk.
3839 * For new convert case, they are all covered by DATA chunk.
3841 * So, there is not valid chunk type check for it now.
3843 if (cache1 != cache2)
3846 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
3847 EXTENT_LOCKED, GFP_NOFS);
3848 set_state_private(&io_tree, offset, bytenr);
3850 offset += btrfs_file_extent_num_bytes(leaf, fi);
3853 btrfs_release_path(&path);
3855 if (offset < total_bytes) {
3856 fprintf(stderr, "unable to build extent mapping\n");
3857 fprintf(stderr, "converted filesystem after balance is unable to rollback\n");
3861 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
3862 first_free &= ~((u64)sectorsize - 1);
3863 /* backup for extent #0 should exist */
3864 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
3865 fprintf(stderr, "no backup for the first extent\n");
3868 /* force no allocation from system block group */
3869 root->fs_info->system_allocs = -1;
3870 trans = btrfs_start_transaction(root, 1);
3873 * recow the whole chunk tree, this will remove all chunk tree blocks
3874 * from system block group
3876 chunk_root = root->fs_info->chunk_root;
3877 memset(&key, 0, sizeof(key));
3879 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
3883 ret = btrfs_next_leaf(chunk_root, &path);
3887 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3888 btrfs_release_path(&path);
3890 btrfs_release_path(&path);
3895 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3899 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
3900 num_bytes += btrfs_block_group_used(&cache1->item);
3902 offset = cache1->key.objectid + cache1->key.offset;
3904 /* only extent #0 left in system block group? */
3905 if (num_bytes > first_free) {
3906 fprintf(stderr, "unable to empty system block group\n");
3909 /* create a system chunk that maps the whole device */
3910 ret = prepare_system_chunk_sb(root->fs_info->super_copy);
3912 fprintf(stderr, "unable to update system chunk\n");
3916 ret = btrfs_commit_transaction(trans, root);
3919 ret = close_ctree(root);
3921 fprintf(stderr, "error during close_ctree %d\n", ret);
3925 /* zero btrfs super block mirrors */
3926 memset(buf, 0, sectorsize);
3927 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3928 bytenr = btrfs_sb_offset(i);
3929 if (bytenr >= total_bytes)
3931 ret = pwrite(fd, buf, sectorsize, bytenr);
3932 if (ret != sectorsize) {
3934 "error during zeroing superblock %d: %d\n",
3940 sb_bytenr = (u64)-1;
3941 /* copy all relocated blocks back */
3943 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
3948 ret = get_state_private(&io_tree, start, &bytenr);
3951 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
3954 while (start <= end) {
3955 if (start == BTRFS_SUPER_INFO_OFFSET) {
3959 ret = pread(fd, buf, sectorsize, bytenr);
3961 fprintf(stderr, "error during pread %d\n", ret);
3964 BUG_ON(ret != sectorsize);
3965 ret = pwrite(fd, buf, sectorsize, start);
3967 fprintf(stderr, "error during pwrite %d\n", ret);
3970 BUG_ON(ret != sectorsize);
3972 start += sectorsize;
3973 bytenr += sectorsize;
3979 fprintf(stderr, "error during fsync %d\n", ret);
3983 * finally, overwrite btrfs super block.
3985 ret = pread(fd, buf, sectorsize, sb_bytenr);
3987 fprintf(stderr, "error during pread %d\n", ret);
3990 BUG_ON(ret != sectorsize);
3991 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
3993 fprintf(stderr, "error during pwrite %d\n", ret);
3996 BUG_ON(ret != sectorsize);
3999 fprintf(stderr, "error during fsync %d\n", ret);
4005 extent_io_tree_cleanup(&io_tree);
4006 printf("rollback complete.\n");
4013 fprintf(stderr, "rollback aborted.\n");
4017 static void print_usage(void)
4019 printf("usage: btrfs-convert [options] device\n");
4020 printf("options:\n");
4021 printf("\t-d|--no-datasum disable data checksum, sets NODATASUM\n");
4022 printf("\t-i|--no-xattr ignore xattrs and ACLs\n");
4023 printf("\t-n|--no-inline disable inlining of small files to metadata\n");
4024 printf("\t-N|--nodesize SIZE set filesystem metadata nodesize\n");
4025 printf("\t-r|--rollback roll back to the original filesystem\n");
4026 printf("\t-l|--label LABEL set filesystem label\n");
4027 printf("\t-L|--copy-label use label from converted filesystem\n");
4028 printf("\t-p|--progress show converting progress (default)\n");
4029 printf("\t-O|--features LIST comma separated list of filesystem features\n");
4030 printf("\t--no-progress show only overview, not the detailed progress\n");
4033 int main(int argc, char *argv[])
4039 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
4040 BTRFS_MKFS_DEFAULT_NODE_SIZE);
4043 int usage_error = 0;
4046 char fslabel[BTRFS_LABEL_SIZE];
4047 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
4050 enum { GETOPT_VAL_NO_PROGRESS = 256 };
4051 static const struct option long_options[] = {
4052 { "no-progress", no_argument, NULL,
4053 GETOPT_VAL_NO_PROGRESS },
4054 { "no-datasum", no_argument, NULL, 'd' },
4055 { "no-inline", no_argument, NULL, 'n' },
4056 { "no-xattr", no_argument, NULL, 'i' },
4057 { "rollback", no_argument, NULL, 'r' },
4058 { "features", required_argument, NULL, 'O' },
4059 { "progress", no_argument, NULL, 'p' },
4060 { "label", required_argument, NULL, 'l' },
4061 { "copy-label", no_argument, NULL, 'L' },
4062 { "nodesize", required_argument, NULL, 'N' },
4063 { "help", no_argument, NULL, GETOPT_VAL_HELP},
4064 { NULL, 0, NULL, 0 }
4066 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
4081 nodesize = parse_size(optarg);
4088 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
4090 "WARNING: label too long, trimmed to %d bytes\n",
4091 BTRFS_LABEL_SIZE - 1);
4093 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
4102 char *orig = strdup(optarg);
4105 tmp = btrfs_parse_fs_features(tmp, &features);
4108 "Unrecognized filesystem feature '%s'\n",
4114 if (features & BTRFS_FEATURE_LIST_ALL) {
4115 btrfs_list_all_fs_features(
4116 ~BTRFS_CONVERT_ALLOWED_FEATURES);
4119 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
4122 btrfs_parse_features_to_string(buf,
4123 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
4125 "ERROR: features not allowed for convert: %s\n",
4132 case GETOPT_VAL_NO_PROGRESS:
4135 case GETOPT_VAL_HELP:
4138 return c != GETOPT_VAL_HELP;
4142 if (check_argc_exact(argc - optind, 1)) {
4147 if (rollback && (!datacsum || noxattr || !packing)) {
4149 "Usage error: -d, -i, -n options do not apply to rollback\n");
4158 file = argv[optind];
4159 ret = check_mounted(file);
4161 fprintf(stderr, "Could not check mount status: %s\n",
4165 fprintf(stderr, "%s is mounted\n", file);
4170 ret = do_rollback(file);
4172 ret = do_convert_v2(file, datacsum, packing, noxattr, nodesize,
4173 copylabel, fslabel, progress, features);