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>
30 #include <uuid/uuid.h>
31 #include <linux/limits.h>
37 #include "transaction.h"
40 #include "task-utils.h"
41 #include <ext2fs/ext2_fs.h>
42 #include <ext2fs/ext2fs.h>
43 #include <ext2fs/ext2_ext_attr.h>
45 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
46 #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
49 uint32_t max_copy_inodes;
50 uint32_t cur_copy_inodes;
51 struct task_info *info;
54 static void *print_copied_inodes(void *p)
56 struct task_ctx *priv = p;
57 const char work_indicator[] = { '.', 'o', 'O', 'o' };
60 task_period_start(priv->info, 1000 /* 1s */);
63 printf("copy inodes [%c] [%10d/%10d]\r",
64 work_indicator[count % 4], priv->cur_copy_inodes,
65 priv->max_copy_inodes);
67 task_period_wait(priv->info);
73 static int after_copied_inodes(void *p)
75 struct task_ctx *priv = p;
78 task_period_stop(priv->info);
84 * Open Ext2fs in readonly mode, read block allocation bitmap and
85 * inode bitmap into memory.
87 static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
92 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
94 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
97 ret = ext2fs_read_inode_bitmap(ext2_fs);
99 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
103 ret = ext2fs_read_block_bitmap(ext2_fs);
105 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
110 * search each block group for a free inode. this set up
111 * uninit block/inode bitmaps appropriately.
114 while (ino <= ext2_fs->super->s_inodes_count) {
116 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
117 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
126 static int close_ext2fs(ext2_filsys fs)
132 static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
136 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
137 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
144 static int ext2_alloc_block_range(ext2_filsys fs, u64 goal, int num,
148 ext2fs_block_bitmap bitmap = fs->block_map;
149 blk_t start = ext2fs_get_block_bitmap_start(bitmap);
150 blk_t end = ext2fs_get_block_bitmap_end(bitmap);
152 for (block = max_t(u64, goal, start); block + num < end; block++) {
153 if (ext2fs_fast_test_block_bitmap_range(bitmap, block, num)) {
154 ext2fs_fast_mark_block_bitmap_range(bitmap, block,
163 static int ext2_free_block(ext2_filsys fs, u64 block)
165 BUG_ON(block != (blk_t)block);
166 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
170 static int ext2_free_block_range(ext2_filsys fs, u64 block, int num)
172 BUG_ON(block != (blk_t)block);
173 ext2fs_fast_unmark_block_bitmap_range(fs->block_map, block, num);
177 static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
183 u64 blocksize = ext2_fs->blocksize;
185 block = ext2_fs->super->s_first_data_block;
186 for (; block < ext2_fs->super->s_blocks_count; block++) {
187 if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
189 bytenr = block * blocksize;
190 ret = set_extent_dirty(&root->fs_info->free_space_cache,
191 bytenr, bytenr + blocksize - 1, 0);
195 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
196 bytenr = btrfs_sb_offset(i);
197 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
198 if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
200 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
201 bytenr + BTRFS_STRIPE_LEN - 1, 0);
204 clear_extent_dirty(&root->fs_info->free_space_cache,
205 0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
210 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
211 u64 hint_byte, struct btrfs_key *ins)
215 u64 last = hint_byte;
218 struct btrfs_block_group_cache *cache;
221 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
222 last, &start, &end, EXTENT_DIRTY);
224 if (wrapped++ == 0) {
232 start = max(last, start);
234 if (last - start < num_bytes)
237 last = start + num_bytes;
238 if (test_range_bit(&root->fs_info->pinned_extents,
239 start, last - 1, EXTENT_DIRTY, 0))
242 cache = btrfs_lookup_block_group(root->fs_info, start);
244 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
245 last > cache->key.objectid + cache->key.offset) {
246 last = cache->key.objectid + cache->key.offset;
250 clear_extent_dirty(&root->fs_info->free_space_cache,
251 start, start + num_bytes - 1, 0);
253 ins->objectid = start;
254 ins->offset = num_bytes;
255 ins->type = BTRFS_EXTENT_ITEM_KEY;
259 fprintf(stderr, "not enough free space\n");
263 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
268 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
269 offset = btrfs_sb_offset(i);
270 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
272 if (bytenr < offset + BTRFS_STRIPE_LEN &&
273 bytenr + num_bytes > offset)
279 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
282 return intersect_with_sb(bytenr, num_bytes);
285 static struct btrfs_extent_ops extent_ops = {
286 .alloc_extent = custom_alloc_extent,
287 .free_extent = custom_free_extent,
290 struct dir_iterate_data {
291 struct btrfs_trans_handle *trans;
292 struct btrfs_root *root;
293 struct btrfs_inode_item *inode;
300 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
301 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
302 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
303 [EXT2_FT_DIR] = BTRFS_FT_DIR,
304 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
305 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
306 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
307 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
308 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
311 static int dir_iterate_proc(ext2_ino_t dir, int entry,
312 struct ext2_dir_entry *dirent,
313 int offset, int blocksize,
314 char *buf,void *priv_data)
320 char dotdot[] = "..";
321 struct btrfs_key location;
322 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
325 name_len = dirent->name_len & 0xFF;
327 objectid = dirent->inode + INO_OFFSET;
328 if (!strncmp(dirent->name, dotdot, name_len)) {
330 BUG_ON(idata->parent != 0);
331 idata->parent = objectid;
335 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
338 location.objectid = objectid;
340 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
342 file_type = dirent->name_len >> 8;
343 BUG_ON(file_type > EXT2_FT_SYMLINK);
344 ret = btrfs_insert_dir_item(idata->trans, idata->root,
345 dirent->name, name_len,
346 idata->objectid, &location,
347 filetype_conversion_table[file_type],
351 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
352 dirent->name, name_len,
353 objectid, idata->objectid,
358 inode_size = btrfs_stack_inode_size(idata->inode) +
360 btrfs_set_stack_inode_size(idata->inode, inode_size);
363 idata->errcode = ret;
367 static int create_dir_entries(struct btrfs_trans_handle *trans,
368 struct btrfs_root *root, u64 objectid,
369 struct btrfs_inode_item *btrfs_inode,
370 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
374 struct dir_iterate_data data = {
377 .inode = btrfs_inode,
378 .objectid = objectid,
384 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
385 dir_iterate_proc, &data);
389 if (ret == 0 && data.parent == objectid) {
390 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
391 objectid, objectid, 0);
395 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
399 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
400 u32 num_bytes, char *buffer)
403 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
405 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
406 if (ret != num_bytes)
415 static int csum_disk_extent(struct btrfs_trans_handle *trans,
416 struct btrfs_root *root,
417 u64 disk_bytenr, u64 num_bytes)
419 u32 blocksize = root->sectorsize;
424 buffer = malloc(blocksize);
427 for (offset = 0; offset < num_bytes; offset += blocksize) {
428 ret = read_disk_extent(root, disk_bytenr + offset,
432 ret = btrfs_csum_file_block(trans,
433 root->fs_info->csum_root,
434 disk_bytenr + num_bytes,
435 disk_bytenr + offset,
444 static int record_file_blocks(struct btrfs_trans_handle *trans,
445 struct btrfs_root *root, u64 objectid,
446 struct btrfs_inode_item *inode,
447 u64 file_block, u64 disk_block,
448 u64 num_blocks, int checksum)
451 u64 file_pos = file_block * root->sectorsize;
452 u64 disk_bytenr = disk_block * root->sectorsize;
453 u64 num_bytes = num_blocks * root->sectorsize;
454 ret = btrfs_record_file_extent(trans, root, objectid, inode, file_pos,
455 disk_bytenr, num_bytes);
457 if (ret || !checksum || disk_bytenr == 0)
460 return csum_disk_extent(trans, root, disk_bytenr, num_bytes);
463 struct blk_iterate_data {
464 struct btrfs_trans_handle *trans;
465 struct btrfs_root *root;
466 struct btrfs_inode_item *inode;
476 static int block_iterate_proc(ext2_filsys ext2_fs,
477 u64 disk_block, u64 file_block,
478 struct blk_iterate_data *idata)
483 struct btrfs_root *root = idata->root;
484 struct btrfs_trans_handle *trans = idata->trans;
485 struct btrfs_block_group_cache *cache;
486 u64 bytenr = disk_block * root->sectorsize;
488 sb_region = intersect_with_sb(bytenr, root->sectorsize);
489 do_barrier = sb_region || disk_block >= idata->boundary;
490 if ((idata->num_blocks > 0 && do_barrier) ||
491 (file_block > idata->first_block + idata->num_blocks) ||
492 (disk_block != idata->disk_block + idata->num_blocks)) {
493 if (idata->num_blocks > 0) {
494 ret = record_file_blocks(trans, root, idata->objectid,
495 idata->inode, idata->first_block,
496 idata->disk_block, idata->num_blocks,
500 idata->first_block += idata->num_blocks;
501 idata->num_blocks = 0;
503 if (file_block > idata->first_block) {
504 ret = record_file_blocks(trans, root, idata->objectid,
505 idata->inode, idata->first_block,
506 0, file_block - idata->first_block,
513 bytenr += BTRFS_STRIPE_LEN - 1;
514 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
516 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
518 bytenr = cache->key.objectid + cache->key.offset;
521 idata->first_block = file_block;
522 idata->disk_block = disk_block;
523 idata->boundary = bytenr / root->sectorsize;
528 idata->errcode = ret;
532 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
533 e2_blkcnt_t blockcnt, blk_t ref_block,
534 int ref_offset, void *priv_data)
536 struct blk_iterate_data *idata;
537 idata = (struct blk_iterate_data *)priv_data;
538 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
542 * traverse file's data blocks, record these data blocks as file extents.
544 static int create_file_extents(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root, u64 objectid,
546 struct btrfs_inode_item *btrfs_inode,
547 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
548 int datacsum, int packing)
554 u32 sectorsize = root->sectorsize;
555 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
556 struct blk_iterate_data data = {
559 .inode = btrfs_inode,
560 .objectid = objectid,
565 .checksum = datacsum,
568 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
569 NULL, __block_iterate_proc, &data);
575 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
576 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
577 u64 num_bytes = data.num_blocks * sectorsize;
578 u64 disk_bytenr = data.disk_block * sectorsize;
581 buffer = malloc(num_bytes);
584 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
587 if (num_bytes > inode_size)
588 num_bytes = inode_size;
589 ret = btrfs_insert_inline_extent(trans, root, objectid,
590 0, buffer, num_bytes);
593 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
594 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
595 } else if (data.num_blocks > 0) {
596 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
597 data.first_block, data.disk_block,
598 data.num_blocks, data.checksum);
602 data.first_block += data.num_blocks;
603 last_block = (inode_size + sectorsize - 1) / sectorsize;
604 if (last_block > data.first_block) {
605 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
606 data.first_block, 0, last_block -
607 data.first_block, data.checksum);
613 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
617 static int create_symbol_link(struct btrfs_trans_handle *trans,
618 struct btrfs_root *root, u64 objectid,
619 struct btrfs_inode_item *btrfs_inode,
620 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
621 struct ext2_inode *ext2_inode)
625 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
626 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
627 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
628 ret = create_file_extents(trans, root, objectid, btrfs_inode,
629 ext2_fs, ext2_ino, 1, 1);
630 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
634 pathname = (char *)&(ext2_inode->i_block[0]);
635 BUG_ON(pathname[inode_size] != 0);
636 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
637 pathname, inode_size + 1);
638 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
643 * Following xattr/acl related codes are based on codes in
644 * fs/ext3/xattr.c and fs/ext3/acl.c
646 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
647 #define EXT2_XATTR_BFIRST(ptr) \
648 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
649 #define EXT2_XATTR_IHDR(inode) \
650 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
651 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
652 #define EXT2_XATTR_IFIRST(inode) \
653 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
654 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
656 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
659 struct ext2_ext_attr_entry *next;
661 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
662 next = EXT2_EXT_ATTR_NEXT(entry);
663 if ((void *)next >= end)
670 static int ext2_xattr_check_block(const char *buf, size_t size)
673 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
675 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
676 header->h_blocks != 1)
678 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
682 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
685 size_t value_size = entry->e_value_size;
687 if (entry->e_value_block != 0 || value_size > size ||
688 entry->e_value_offs + value_size > size)
693 #define EXT2_ACL_VERSION 0x0001
704 } ext2_acl_entry_short;
710 static inline int ext2_acl_count(size_t size)
713 size -= sizeof(ext2_acl_header);
714 s = size - 4 * sizeof(ext2_acl_entry_short);
716 if (size % sizeof(ext2_acl_entry_short))
718 return size / sizeof(ext2_acl_entry_short);
720 if (s % sizeof(ext2_acl_entry))
722 return s / sizeof(ext2_acl_entry) + 4;
726 #define ACL_EA_VERSION 0x0002
736 acl_ea_entry a_entries[0];
739 static inline size_t acl_ea_size(int count)
741 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
744 static int ext2_acl_to_xattr(void *dst, const void *src,
745 size_t dst_size, size_t src_size)
748 const void *end = src + src_size;
749 acl_ea_header *ext_acl = (acl_ea_header *)dst;
750 acl_ea_entry *dst_entry = ext_acl->a_entries;
751 ext2_acl_entry *src_entry;
753 if (src_size < sizeof(ext2_acl_header))
755 if (((ext2_acl_header *)src)->a_version !=
756 cpu_to_le32(EXT2_ACL_VERSION))
758 src += sizeof(ext2_acl_header);
759 count = ext2_acl_count(src_size);
763 BUG_ON(dst_size < acl_ea_size(count));
764 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
765 for (i = 0; i < count; i++, dst_entry++) {
766 src_entry = (ext2_acl_entry *)src;
767 if (src + sizeof(ext2_acl_entry_short) > end)
769 dst_entry->e_tag = src_entry->e_tag;
770 dst_entry->e_perm = src_entry->e_perm;
771 switch (le16_to_cpu(src_entry->e_tag)) {
776 src += sizeof(ext2_acl_entry_short);
777 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
781 src += sizeof(ext2_acl_entry);
784 dst_entry->e_id = src_entry->e_id;
797 static char *xattr_prefix_table[] = {
799 [2] = "system.posix_acl_access",
800 [3] = "system.posix_acl_default",
805 static int copy_single_xattr(struct btrfs_trans_handle *trans,
806 struct btrfs_root *root, u64 objectid,
807 struct ext2_ext_attr_entry *entry,
808 const void *data, u32 datalen)
813 void *databuf = NULL;
814 char namebuf[XATTR_NAME_MAX + 1];
816 name_index = entry->e_name_index;
817 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
818 xattr_prefix_table[name_index] == NULL)
820 name_len = strlen(xattr_prefix_table[name_index]) +
822 if (name_len >= sizeof(namebuf))
825 if (name_index == 2 || name_index == 3) {
826 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
827 databuf = malloc(bufsize);
830 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
836 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
837 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
838 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
839 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
840 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
841 objectid - INO_OFFSET, name_len, namebuf);
844 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
845 data, datalen, objectid);
851 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
852 struct btrfs_root *root, u64 objectid,
853 struct btrfs_inode_item *btrfs_inode,
854 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
860 u32 block_size = ext2_fs->blocksize;
861 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
862 struct ext2_inode_large *ext2_inode;
863 struct ext2_ext_attr_entry *entry;
866 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
868 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
869 ext2_inode = (struct ext2_inode_large *)inode_buf;
871 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
875 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
878 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
884 if (ext2_ino > ext2_fs->super->s_first_ino &&
885 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
886 if (EXT2_GOOD_OLD_INODE_SIZE +
887 ext2_inode->i_extra_isize > inode_size) {
891 if (ext2_inode->i_extra_isize != 0 &&
892 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
893 EXT2_EXT_ATTR_MAGIC) {
899 void *end = (void *)ext2_inode + inode_size;
900 entry = EXT2_XATTR_IFIRST(ext2_inode);
901 total = end - (void *)entry;
902 ret = ext2_xattr_check_names(entry, end);
905 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
906 ret = ext2_xattr_check_entry(entry, total);
909 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
911 datalen = entry->e_value_size;
912 ret = copy_single_xattr(trans, root, objectid,
913 entry, data, datalen);
916 entry = EXT2_EXT_ATTR_NEXT(entry);
920 if (ext2_inode->i_file_acl == 0)
923 buffer = malloc(block_size);
928 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
930 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
935 ret = ext2_xattr_check_block(buffer, block_size);
939 entry = EXT2_XATTR_BFIRST(buffer);
940 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
941 ret = ext2_xattr_check_entry(entry, block_size);
944 data = buffer + entry->e_value_offs;
945 datalen = entry->e_value_size;
946 ret = copy_single_xattr(trans, root, objectid,
947 entry, data, datalen);
950 entry = EXT2_EXT_ATTR_NEXT(entry);
954 if ((void *)ext2_inode != inode_buf)
959 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
961 static inline dev_t old_decode_dev(u16 val)
963 return MKDEV((val >> 8) & 255, val & 255);
966 static inline dev_t new_decode_dev(u32 dev)
968 unsigned major = (dev & 0xfff00) >> 8;
969 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
970 return MKDEV(major, minor);
973 static int copy_inode_item(struct btrfs_inode_item *dst,
974 struct ext2_inode *src, u32 blocksize)
976 btrfs_set_stack_inode_generation(dst, 1);
977 btrfs_set_stack_inode_size(dst, src->i_size);
978 btrfs_set_stack_inode_nbytes(dst, 0);
979 btrfs_set_stack_inode_block_group(dst, 0);
980 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
981 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
982 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
983 btrfs_set_stack_inode_mode(dst, src->i_mode);
984 btrfs_set_stack_inode_rdev(dst, 0);
985 btrfs_set_stack_inode_flags(dst, 0);
986 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
987 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
988 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
989 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
990 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
991 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
992 btrfs_set_stack_timespec_sec(&dst->otime, 0);
993 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
995 if (S_ISDIR(src->i_mode)) {
996 btrfs_set_stack_inode_size(dst, 0);
997 btrfs_set_stack_inode_nlink(dst, 1);
999 if (S_ISREG(src->i_mode)) {
1000 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1003 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1004 !S_ISLNK(src->i_mode)) {
1005 if (src->i_block[0]) {
1006 btrfs_set_stack_inode_rdev(dst,
1007 old_decode_dev(src->i_block[0]));
1009 btrfs_set_stack_inode_rdev(dst,
1010 new_decode_dev(src->i_block[1]));
1017 * copy a single inode. do all the required works, such as cloning
1018 * inode item, creating file extents and creating directory entries.
1020 static int copy_single_inode(struct btrfs_trans_handle *trans,
1021 struct btrfs_root *root, u64 objectid,
1022 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1023 struct ext2_inode *ext2_inode,
1024 int datacsum, int packing, int noxattr)
1027 struct btrfs_key inode_key;
1028 struct btrfs_inode_item btrfs_inode;
1030 if (ext2_inode->i_links_count == 0)
1033 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1034 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1035 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1036 BTRFS_INODE_NODATASUM;
1037 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1040 switch (ext2_inode->i_mode & S_IFMT) {
1042 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1043 ext2_fs, ext2_ino, datacsum, packing);
1046 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1050 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1051 ext2_fs, ext2_ino, ext2_inode);
1061 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1066 inode_key.objectid = objectid;
1067 inode_key.offset = 0;
1068 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1069 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1073 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1074 u64 src_bytenr, u32 num_bytes)
1078 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1080 buffer = malloc(num_bytes);
1083 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1084 if (ret != num_bytes)
1086 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1087 if (ret != num_bytes)
1097 * scan ext2's inode bitmap and copy all used inodes.
1099 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1100 int datacsum, int packing, int noxattr, struct task_ctx *p)
1104 ext2_inode_scan ext2_scan;
1105 struct ext2_inode ext2_inode;
1106 ext2_ino_t ext2_ino;
1108 struct btrfs_trans_handle *trans;
1110 trans = btrfs_start_transaction(root, 1);
1113 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1115 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1118 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1120 /* no more inodes */
1123 /* skip special inode in ext2fs */
1124 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1125 ext2_ino != EXT2_ROOT_INO)
1127 objectid = ext2_ino + INO_OFFSET;
1128 ret = copy_single_inode(trans, root,
1129 objectid, ext2_fs, ext2_ino,
1130 &ext2_inode, datacsum, packing,
1132 p->cur_copy_inodes++;
1135 if (trans->blocks_used >= 4096) {
1136 ret = btrfs_commit_transaction(trans, root);
1138 trans = btrfs_start_transaction(root, 1);
1143 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1146 ret = btrfs_commit_transaction(trans, root);
1153 * Construct a range of ext2fs image file.
1154 * scan block allocation bitmap, find all blocks used by the ext2fs
1155 * in this range and create file extents that point to these blocks.
1157 * Note: Before calling the function, no file extent points to blocks
1160 static int create_image_file_range(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root, u64 objectid,
1162 struct btrfs_inode_item *inode,
1163 u64 start_byte, u64 end_byte,
1164 ext2_filsys ext2_fs)
1166 u32 blocksize = ext2_fs->blocksize;
1167 u32 block = start_byte / blocksize;
1168 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1170 struct blk_iterate_data data = {
1174 .objectid = objectid,
1175 .first_block = block,
1178 .boundary = (u64)-1,
1182 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1183 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1185 ret = block_iterate_proc(NULL, block, block, &data);
1186 if (ret & BLOCK_ABORT) {
1191 if (data.num_blocks > 0) {
1192 ret = record_file_blocks(trans, root, objectid, inode,
1193 data.first_block, data.disk_block,
1194 data.num_blocks, 0);
1197 data.first_block += data.num_blocks;
1199 if (last_block > data.first_block) {
1200 ret = record_file_blocks(trans, root, objectid, inode,
1201 data.first_block, 0, last_block -
1202 data.first_block, 0);
1210 * Create the ext2fs image file.
1212 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1216 struct btrfs_key key;
1217 struct btrfs_key location;
1218 struct btrfs_path path;
1219 struct btrfs_inode_item btrfs_inode;
1220 struct btrfs_inode_item *inode_item;
1221 struct extent_buffer *leaf;
1222 struct btrfs_fs_info *fs_info = root->fs_info;
1223 struct btrfs_root *extent_root = fs_info->extent_root;
1224 struct btrfs_trans_handle *trans;
1225 struct btrfs_extent_item *ei;
1226 struct btrfs_extent_inline_ref *iref;
1227 struct btrfs_extent_data_ref *dref;
1234 u32 sectorsize = root->sectorsize;
1236 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1237 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1238 first_free &= ~((u64)sectorsize - 1);
1240 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1241 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1242 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1243 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1244 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1245 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1246 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1247 BTRFS_INODE_READONLY);
1248 btrfs_init_path(&path);
1249 trans = btrfs_start_transaction(root, 1);
1252 objectid = btrfs_root_dirid(&root->root_item);
1253 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1258 * copy blocks covered by extent #0 to new positions. extent #0 is
1259 * special, we can't rely on relocate_extents_range to relocate it.
1261 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1262 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1265 ret = copy_disk_extent(root, key.objectid, last_byte,
1269 ret = btrfs_record_file_extent(trans, root, objectid,
1270 &btrfs_inode, last_byte,
1271 key.objectid, sectorsize);
1277 key.objectid = last_byte;
1279 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1280 ret = btrfs_search_slot(trans, fs_info->extent_root,
1285 leaf = path.nodes[0];
1286 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1287 ret = btrfs_next_leaf(extent_root, &path);
1292 leaf = path.nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1295 if (last_byte > key.objectid ||
1296 key.type != BTRFS_EXTENT_ITEM_KEY) {
1301 bytenr = key.objectid;
1302 num_bytes = key.offset;
1303 ei = btrfs_item_ptr(leaf, path.slots[0],
1304 struct btrfs_extent_item);
1305 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1310 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1311 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1313 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1314 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1315 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1316 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1317 if (btrfs_extent_data_ref_root(leaf, dref) !=
1318 BTRFS_FS_TREE_OBJECTID) {
1323 if (bytenr > last_byte) {
1324 ret = create_image_file_range(trans, root, objectid,
1325 &btrfs_inode, last_byte,
1330 ret = btrfs_record_file_extent(trans, root, objectid,
1331 &btrfs_inode, bytenr, bytenr,
1335 last_byte = bytenr + num_bytes;
1336 btrfs_release_path(&path);
1338 if (trans->blocks_used >= 4096) {
1339 ret = btrfs_commit_transaction(trans, root);
1341 trans = btrfs_start_transaction(root, 1);
1345 btrfs_release_path(&path);
1346 if (total_bytes > last_byte) {
1347 ret = create_image_file_range(trans, root, objectid,
1348 &btrfs_inode, last_byte,
1349 total_bytes, ext2_fs);
1354 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1358 location.objectid = objectid;
1359 location.offset = 0;
1360 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1361 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1362 btrfs_root_dirid(&root->root_item),
1363 &location, EXT2_FT_REG_FILE, objectid);
1366 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1368 btrfs_root_dirid(&root->root_item),
1372 location.objectid = btrfs_root_dirid(&root->root_item);
1373 location.offset = 0;
1374 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1375 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1378 leaf = path.nodes[0];
1379 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1380 struct btrfs_inode_item);
1381 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1382 btrfs_inode_size(leaf, inode_item));
1383 btrfs_mark_buffer_dirty(leaf);
1384 btrfs_release_path(&path);
1385 ret = btrfs_commit_transaction(trans, root);
1388 btrfs_release_path(&path);
1392 static struct btrfs_root * link_subvol(struct btrfs_root *root,
1393 const char *base, u64 root_objectid)
1395 struct btrfs_trans_handle *trans;
1396 struct btrfs_fs_info *fs_info = root->fs_info;
1397 struct btrfs_root *tree_root = fs_info->tree_root;
1398 struct btrfs_root *new_root = NULL;
1399 struct btrfs_path *path;
1400 struct btrfs_inode_item *inode_item;
1401 struct extent_buffer *leaf;
1402 struct btrfs_key key;
1403 u64 dirid = btrfs_root_dirid(&root->root_item);
1405 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
1411 if (len < 1 || len > BTRFS_NAME_LEN)
1414 path = btrfs_alloc_path();
1417 key.objectid = dirid;
1418 key.type = BTRFS_DIR_INDEX_KEY;
1419 key.offset = (u64)-1;
1421 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1424 if (path->slots[0] > 0) {
1426 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1427 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1428 index = key.offset + 1;
1430 btrfs_release_path(path);
1432 trans = btrfs_start_transaction(root, 1);
1435 key.objectid = dirid;
1437 key.type = BTRFS_INODE_ITEM_KEY;
1439 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1441 leaf = path->nodes[0];
1442 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1443 struct btrfs_inode_item);
1445 key.objectid = root_objectid;
1446 key.offset = (u64)-1;
1447 key.type = BTRFS_ROOT_ITEM_KEY;
1449 memcpy(buf, base, len);
1450 for (i = 0; i < 1024; i++) {
1451 ret = btrfs_insert_dir_item(trans, root, buf, len,
1452 dirid, &key, BTRFS_FT_DIR, index);
1455 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
1456 if (len < 1 || len > BTRFS_NAME_LEN) {
1464 btrfs_set_inode_size(leaf, inode_item, len * 2 +
1465 btrfs_inode_size(leaf, inode_item));
1466 btrfs_mark_buffer_dirty(leaf);
1467 btrfs_release_path(path);
1469 /* add the backref first */
1470 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1471 BTRFS_ROOT_BACKREF_KEY,
1472 root->root_key.objectid,
1473 dirid, index, buf, len);
1476 /* now add the forward ref */
1477 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1478 BTRFS_ROOT_REF_KEY, root_objectid,
1479 dirid, index, buf, len);
1481 ret = btrfs_commit_transaction(trans, root);
1484 new_root = btrfs_read_fs_root(fs_info, &key);
1485 if (IS_ERR(new_root))
1488 btrfs_free_path(path);
1492 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1493 struct btrfs_root *root)
1495 struct btrfs_fs_info *info = root->fs_info;
1496 struct btrfs_root *chunk_root = info->chunk_root;
1497 struct btrfs_root *extent_root = info->extent_root;
1498 struct btrfs_device *device;
1499 struct btrfs_block_group_cache *cache;
1500 struct btrfs_dev_extent *extent;
1501 struct extent_buffer *leaf;
1502 struct btrfs_chunk chunk;
1503 struct btrfs_key key;
1504 struct btrfs_path path;
1510 btrfs_init_path(&path);
1512 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1513 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1515 BUG_ON(list_empty(&info->fs_devices->devices));
1516 device = list_entry(info->fs_devices->devices.next,
1517 struct btrfs_device, dev_list);
1518 BUG_ON(device->devid != info->fs_devices->latest_devid);
1520 /* delete device extent created by make_btrfs */
1521 key.objectid = device->devid;
1523 key.type = BTRFS_DEV_EXTENT_KEY;
1524 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1529 ret = btrfs_del_item(trans, device->dev_root, &path);
1532 btrfs_release_path(&path);
1534 /* delete chunk item created by make_btrfs */
1535 key.objectid = chunk_objectid;
1537 key.type = BTRFS_CHUNK_ITEM_KEY;
1538 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1543 ret = btrfs_del_item(trans, chunk_root, &path);
1546 btrfs_release_path(&path);
1548 /* for each block group, create device extent and chunk item */
1550 while (cur_start < total_bytes) {
1551 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1554 /* insert device extent */
1555 key.objectid = device->devid;
1556 key.offset = cache->key.objectid;
1557 key.type = BTRFS_DEV_EXTENT_KEY;
1558 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1559 &key, sizeof(*extent));
1563 leaf = path.nodes[0];
1564 extent = btrfs_item_ptr(leaf, path.slots[0],
1565 struct btrfs_dev_extent);
1567 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1568 chunk_root->root_key.objectid);
1569 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1571 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1572 cache->key.objectid);
1573 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1574 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1575 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1577 btrfs_mark_buffer_dirty(leaf);
1578 btrfs_release_path(&path);
1580 /* insert chunk item */
1581 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1582 btrfs_set_stack_chunk_owner(&chunk,
1583 extent_root->root_key.objectid);
1584 btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
1585 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1586 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1587 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1588 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1589 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1590 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1591 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1592 btrfs_set_stack_stripe_offset(&chunk.stripe,
1593 cache->key.objectid);
1594 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1596 key.objectid = chunk_objectid;
1597 key.offset = cache->key.objectid;
1598 key.type = BTRFS_CHUNK_ITEM_KEY;
1600 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1601 btrfs_chunk_item_size(1));
1605 cur_start = cache->key.objectid + cache->key.offset;
1608 device->bytes_used = total_bytes;
1609 ret = btrfs_update_device(trans, device);
1611 btrfs_release_path(&path);
1615 static int create_subvol(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root, u64 root_objectid)
1618 struct extent_buffer *tmp;
1619 struct btrfs_root *new_root;
1620 struct btrfs_key key;
1621 struct btrfs_root_item root_item;
1624 ret = btrfs_copy_root(trans, root, root->node, &tmp,
1628 memcpy(&root_item, &root->root_item, sizeof(root_item));
1629 btrfs_set_root_bytenr(&root_item, tmp->start);
1630 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1631 btrfs_set_root_generation(&root_item, trans->transid);
1632 free_extent_buffer(tmp);
1634 key.objectid = root_objectid;
1635 key.type = BTRFS_ROOT_ITEM_KEY;
1636 key.offset = trans->transid;
1637 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1640 key.offset = (u64)-1;
1641 new_root = btrfs_read_fs_root(root->fs_info, &key);
1642 BUG_ON(!new_root || IS_ERR(new_root));
1644 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1650 static int init_btrfs(struct btrfs_root *root)
1653 struct btrfs_key location;
1654 struct btrfs_trans_handle *trans;
1655 struct btrfs_fs_info *fs_info = root->fs_info;
1656 struct extent_buffer *tmp;
1658 trans = btrfs_start_transaction(root, 1);
1660 ret = btrfs_make_block_groups(trans, root);
1663 ret = btrfs_fix_block_accounting(trans, root);
1666 ret = create_chunk_mapping(trans, root);
1669 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1670 BTRFS_ROOT_TREE_DIR_OBJECTID);
1673 memcpy(&location, &root->root_key, sizeof(location));
1674 location.offset = (u64)-1;
1675 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1676 btrfs_super_root_dir(fs_info->super_copy),
1677 &location, BTRFS_FT_DIR, 0);
1680 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1682 btrfs_super_root_dir(fs_info->super_copy), 0);
1685 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1686 BTRFS_FIRST_FREE_OBJECTID);
1688 /* subvol for ext2 image file */
1689 ret = create_subvol(trans, root, EXT2_IMAGE_SUBVOL_OBJECTID);
1691 /* subvol for data relocation */
1692 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1695 extent_buffer_get(fs_info->csum_root->node);
1696 ret = __btrfs_cow_block(trans, fs_info->csum_root,
1697 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1699 free_extent_buffer(tmp);
1701 ret = btrfs_commit_transaction(trans, root);
1708 * Migrate super block to its default position and zero 0 ~ 16k
1710 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1713 struct extent_buffer *buf;
1714 struct btrfs_super_block *super;
1718 BUG_ON(sectorsize < sizeof(*super));
1719 buf = malloc(sizeof(*buf) + sectorsize);
1723 buf->len = sectorsize;
1724 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1725 if (ret != sectorsize)
1728 super = (struct btrfs_super_block *)buf->data;
1729 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1730 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1732 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1733 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1734 if (ret != sectorsize)
1741 memset(buf->data, 0, sectorsize);
1742 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1743 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1744 if (len > sectorsize)
1746 ret = pwrite(fd, buf->data, len, bytenr);
1748 fprintf(stderr, "unable to zero fill device\n");
1762 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1764 struct btrfs_chunk *chunk;
1765 struct btrfs_disk_key *key;
1766 u32 sectorsize = btrfs_super_sectorsize(super);
1768 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1769 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1770 sizeof(struct btrfs_disk_key));
1772 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1773 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1774 btrfs_set_disk_key_offset(key, 0);
1776 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1777 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1778 btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1779 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1780 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1781 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1782 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1783 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1784 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1785 chunk->stripe.devid = super->dev_item.devid;
1786 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1787 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1788 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1792 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1795 struct extent_buffer *buf;
1796 struct btrfs_super_block *super;
1798 BUG_ON(sectorsize < sizeof(*super));
1799 buf = malloc(sizeof(*buf) + sectorsize);
1803 buf->len = sectorsize;
1804 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1805 if (ret != sectorsize)
1808 super = (struct btrfs_super_block *)buf->data;
1809 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1810 BUG_ON(btrfs_super_num_devices(super) != 1);
1812 ret = prepare_system_chunk_sb(super);
1816 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1817 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1818 if (ret != sectorsize)
1829 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
1831 u64 extent_start, u64 extent_size,
1832 struct btrfs_key *extent_key,
1833 struct extent_io_tree *reloc_tree)
1835 struct extent_buffer *leaf;
1836 struct btrfs_file_extent_item *fi;
1837 struct btrfs_key key;
1838 struct btrfs_path path;
1839 struct btrfs_inode_item inode;
1840 struct blk_iterate_data data;
1847 u32 sectorsize = root->sectorsize;
1853 btrfs_init_path(&path);
1854 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
1858 leaf = path.nodes[0];
1859 fi = btrfs_item_ptr(leaf, path.slots[0],
1860 struct btrfs_file_extent_item);
1861 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
1862 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1863 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1868 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1869 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1871 ret = btrfs_del_item(trans, root, &path);
1875 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
1876 root->root_key.objectid,
1877 extent_key->objectid, extent_key->offset);
1881 btrfs_release_path(&path);
1883 key.objectid = extent_key->objectid;
1885 key.type = BTRFS_INODE_ITEM_KEY;
1886 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1890 leaf = path.nodes[0];
1891 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1892 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1893 btrfs_release_path(&path);
1895 BUG_ON(num_bytes & (sectorsize - 1));
1896 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
1897 btrfs_set_stack_inode_nbytes(&inode, nbytes);
1898 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
1900 data = (struct blk_iterate_data) {
1904 .objectid = extent_key->objectid,
1905 .first_block = extent_key->offset / sectorsize,
1908 .boundary = (u64)-1,
1909 .checksum = datacsum,
1913 cur_offset = extent_key->offset;
1914 while (num_bytes > 0) {
1915 sector_end = bytenr + sectorsize - 1;
1916 if (test_range_bit(reloc_tree, bytenr, sector_end,
1917 EXTENT_LOCKED, 1)) {
1918 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1921 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1924 new_pos = key.objectid;
1926 if (cur_offset == extent_key->offset) {
1927 fd = root->fs_info->fs_devices->latest_bdev;
1928 readahead(fd, bytenr, num_bytes);
1930 ret = copy_disk_extent(root, new_pos, bytenr,
1934 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1935 EXTENT_LOCKED, GFP_NOFS);
1937 ret = set_state_private(reloc_tree, bytenr, new_pos);
1941 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1942 cur_offset / sectorsize, &data);
1943 if (ret & BLOCK_ABORT) {
1948 cur_offset += sectorsize;
1949 bytenr += sectorsize;
1950 num_bytes -= sectorsize;
1953 if (data.num_blocks > 0) {
1954 ret = record_file_blocks(trans, root,
1955 extent_key->objectid, &inode,
1956 data.first_block, data.disk_block,
1957 data.num_blocks, datacsum);
1962 key.objectid = extent_key->objectid;
1964 key.type = BTRFS_INODE_ITEM_KEY;
1965 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1969 leaf = path.nodes[0];
1970 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1971 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1972 btrfs_mark_buffer_dirty(leaf);
1973 btrfs_release_path(&path);
1976 btrfs_release_path(&path);
1980 static int relocate_extents_range(struct btrfs_root *fs_root,
1981 struct btrfs_root *ext2_root,
1982 u64 start_byte, u64 end_byte)
1984 struct btrfs_fs_info *info = fs_root->fs_info;
1985 struct btrfs_root *extent_root = info->extent_root;
1986 struct btrfs_root *cur_root = NULL;
1987 struct btrfs_trans_handle *trans;
1988 struct btrfs_extent_data_ref *dref;
1989 struct btrfs_extent_inline_ref *iref;
1990 struct btrfs_extent_item *ei;
1991 struct extent_buffer *leaf;
1992 struct btrfs_key key;
1993 struct btrfs_key extent_key;
1994 struct btrfs_path path;
1995 struct extent_io_tree reloc_tree;
2005 btrfs_init_path(&path);
2006 extent_io_tree_init(&reloc_tree);
2008 key.objectid = start_byte;
2010 key.type = BTRFS_EXTENT_ITEM_KEY;
2011 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2015 ret = btrfs_previous_item(extent_root, &path, 0,
2016 BTRFS_EXTENT_ITEM_KEY);
2020 leaf = path.nodes[0];
2021 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2022 if (key.objectid + key.offset > start_byte)
2023 start_byte = key.objectid;
2026 btrfs_release_path(&path);
2028 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
2031 trans = btrfs_start_transaction(cur_root, 1);
2034 cur_byte = start_byte;
2036 key.objectid = cur_byte;
2038 key.type = BTRFS_EXTENT_ITEM_KEY;
2039 ret = btrfs_search_slot(trans, extent_root,
2044 leaf = path.nodes[0];
2045 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2046 ret = btrfs_next_leaf(extent_root, &path);
2051 leaf = path.nodes[0];
2054 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2055 if (key.objectid < cur_byte ||
2056 key.type != BTRFS_EXTENT_ITEM_KEY) {
2060 if (key.objectid >= end_byte)
2065 cur_byte = key.objectid;
2066 num_bytes = key.offset;
2067 ei = btrfs_item_ptr(leaf, path.slots[0],
2068 struct btrfs_extent_item);
2069 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2070 BTRFS_EXTENT_FLAG_DATA));
2072 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2073 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2075 ptr += sizeof(struct btrfs_extent_item);
2078 iref = (struct btrfs_extent_inline_ref *)ptr;
2079 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2080 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2081 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2082 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2083 extent_key.objectid =
2084 btrfs_extent_data_ref_objectid(leaf, dref);
2086 btrfs_extent_data_ref_offset(leaf, dref);
2087 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2088 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2090 if (ref_root == cur_root->root_key.objectid)
2093 ptr += btrfs_extent_inline_ref_size(key.type);
2101 ret = relocate_one_reference(trans, cur_root, cur_byte,
2102 num_bytes, &extent_key,
2107 cur_byte += num_bytes;
2108 btrfs_release_path(&path);
2110 if (trans->blocks_used >= 4096) {
2111 ret = btrfs_commit_transaction(trans, cur_root);
2113 trans = btrfs_start_transaction(cur_root, 1);
2117 btrfs_release_path(&path);
2119 ret = btrfs_commit_transaction(trans, cur_root);
2122 if (num_extents > 0 && pass++ < 16)
2125 ret = (num_extents > 0) ? -1 : 0;
2127 btrfs_release_path(&path);
2128 extent_io_tree_cleanup(&reloc_tree);
2133 * relocate data in system chunk
2135 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2136 struct btrfs_root *ext2_root)
2138 struct btrfs_block_group_cache *cache;
2144 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2148 end_byte = cache->key.objectid + cache->key.offset;
2149 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2150 ret = relocate_extents_range(fs_root, ext2_root,
2151 cache->key.objectid,
2158 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2159 offset = btrfs_sb_offset(i);
2160 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2162 ret = relocate_extents_range(fs_root, ext2_root,
2163 offset, offset + BTRFS_STRIPE_LEN);
2172 static int fixup_chunk_mapping(struct btrfs_root *root)
2174 struct btrfs_trans_handle *trans;
2175 struct btrfs_fs_info *info = root->fs_info;
2176 struct btrfs_root *chunk_root = info->chunk_root;
2177 struct extent_buffer *leaf;
2178 struct btrfs_key key;
2179 struct btrfs_path path;
2180 struct btrfs_chunk chunk;
2186 btrfs_init_path(&path);
2188 trans = btrfs_start_transaction(root, 1);
2192 * recow the whole chunk tree. this will move all chunk tree blocks
2193 * into system block group.
2195 memset(&key, 0, sizeof(key));
2197 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2201 ret = btrfs_next_leaf(chunk_root, &path);
2207 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2208 btrfs_release_path(&path);
2210 btrfs_release_path(&path);
2212 /* fixup the system chunk array in super block */
2213 btrfs_set_super_sys_array_size(info->super_copy, 0);
2215 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2217 key.type = BTRFS_CHUNK_ITEM_KEY;
2219 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2224 leaf = path.nodes[0];
2225 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2226 ret = btrfs_next_leaf(chunk_root, &path);
2231 leaf = path.nodes[0];
2233 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2234 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2237 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2238 size = btrfs_item_size_nr(leaf, path.slots[0]);
2239 BUG_ON(size != sizeof(chunk));
2240 read_extent_buffer(leaf, &chunk, ptr, size);
2241 type = btrfs_stack_chunk_type(&chunk);
2243 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2246 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2254 ret = btrfs_commit_transaction(trans, root);
2257 btrfs_release_path(&path);
2261 static int do_convert(const char *devname, int datacsum, int packing, int noxattr,
2262 u32 nodesize, int copylabel, const char *fslabel, int progress)
2264 int i, ret, blocks_per_node;
2270 ext2_filsys ext2_fs;
2271 struct btrfs_root *root;
2272 struct btrfs_root *ext2_root;
2273 struct task_ctx ctx;
2275 ret = open_ext2fs(devname, &ext2_fs);
2277 fprintf(stderr, "unable to open the Ext2fs\n");
2280 blocksize = ext2_fs->blocksize;
2281 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2282 if (blocksize < 4096) {
2283 fprintf(stderr, "block size is too small\n");
2286 if (!(ext2_fs->super->s_feature_incompat &
2287 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2288 fprintf(stderr, "filetype feature is missing\n");
2291 if (btrfs_check_node_or_leaf_size(nodesize, blocksize))
2293 blocks_per_node = nodesize / blocksize;
2294 ret = -blocks_per_node;
2295 for (i = 0; i < 7; i++) {
2296 if (nodesize == blocksize)
2297 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2299 ret = ext2_alloc_block_range(ext2_fs,
2300 ret + blocks_per_node, blocks_per_node,
2303 fprintf(stderr, "not enough free space\n");
2306 blocks[i] *= blocksize;
2308 super_bytenr = blocks[0];
2309 fd = open(devname, O_RDWR);
2311 fprintf(stderr, "unable to open %s\n", devname);
2314 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2315 NULL, blocks, total_bytes, nodesize, nodesize,
2316 blocksize, blocksize, 0);
2318 fprintf(stderr, "unable to create initial ctree: %s\n",
2322 /* create a system chunk that maps the whole device */
2323 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2325 fprintf(stderr, "unable to update system chunk\n");
2328 root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
2330 fprintf(stderr, "unable to open ctree\n");
2333 ret = cache_free_extents(root, ext2_fs);
2335 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2338 root->fs_info->extent_ops = &extent_ops;
2339 /* recover block allocation bitmap */
2340 for (i = 0; i < 7; i++) {
2341 blocks[i] /= blocksize;
2342 if (nodesize == blocksize)
2343 ext2_free_block(ext2_fs, blocks[i]);
2345 ext2_free_block_range(ext2_fs, blocks[i],
2348 ret = init_btrfs(root);
2350 fprintf(stderr, "unable to setup the root tree\n");
2353 printf("creating btrfs metadata.\n");
2354 ctx.max_copy_inodes = (ext2_fs->super->s_inodes_count
2355 - ext2_fs->super->s_free_inodes_count);
2356 ctx.cur_copy_inodes = 0;
2359 ctx.info = task_init(print_copied_inodes, after_copied_inodes, &ctx);
2360 task_start(ctx.info);
2362 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr, &ctx);
2364 fprintf(stderr, "error during copy_inodes %d\n", ret);
2368 task_stop(ctx.info);
2369 task_deinit(ctx.info);
2371 printf("creating ext2fs image file.\n");
2372 ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
2374 fprintf(stderr, "unable to create subvol\n");
2377 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2379 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2382 memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
2383 if (copylabel == 1) {
2384 strncpy(root->fs_info->super_copy->label,
2385 ext2_fs->super->s_volume_name, 16);
2386 fprintf(stderr, "copy label '%s'\n",
2387 root->fs_info->super_copy->label);
2388 } else if (copylabel == -1) {
2389 strncpy(root->fs_info->super_copy->label, fslabel, BTRFS_LABEL_SIZE);
2390 fprintf(stderr, "set label to '%s'\n", fslabel);
2393 printf("cleaning up system chunk.\n");
2394 ret = cleanup_sys_chunk(root, ext2_root);
2396 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2399 ret = close_ctree(root);
2401 fprintf(stderr, "error during close_ctree %d\n", ret);
2404 close_ext2fs(ext2_fs);
2407 * If this step succeed, we get a mountable btrfs. Otherwise
2408 * the ext2fs is left unchanged.
2410 ret = migrate_super_block(fd, super_bytenr, blocksize);
2412 fprintf(stderr, "unable to migrate super block\n");
2416 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2418 fprintf(stderr, "unable to open ctree\n");
2421 /* move chunk tree into system chunk. */
2422 ret = fixup_chunk_mapping(root);
2424 fprintf(stderr, "error during fixup_chunk_tree\n");
2427 ret = close_ctree(root);
2430 printf("conversion complete.\n");
2435 fprintf(stderr, "conversion aborted.\n");
2439 static int may_rollback(struct btrfs_root *root)
2441 struct btrfs_fs_info *info = root->fs_info;
2442 struct btrfs_multi_bio *multi = NULL;
2450 if (btrfs_super_num_devices(info->super_copy) != 1)
2453 bytenr = BTRFS_SUPER_INFO_OFFSET;
2454 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2457 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2458 &length, &multi, 0, NULL);
2460 if (ret == -ENOENT) {
2461 /* removed block group at the tail */
2462 if (length == (u64)-1)
2465 /* removed block group in the middle */
2471 num_stripes = multi->num_stripes;
2472 physical = multi->stripes[0].physical;
2475 if (num_stripes != 1 || physical != bytenr)
2479 if (bytenr >= total_bytes)
2487 static int do_rollback(const char *devname)
2492 struct btrfs_root *root;
2493 struct btrfs_root *ext2_root;
2494 struct btrfs_root *chunk_root;
2495 struct btrfs_dir_item *dir;
2496 struct btrfs_inode_item *inode;
2497 struct btrfs_file_extent_item *fi;
2498 struct btrfs_trans_handle *trans;
2499 struct extent_buffer *leaf;
2500 struct btrfs_block_group_cache *cache1;
2501 struct btrfs_block_group_cache *cache2;
2502 struct btrfs_key key;
2503 struct btrfs_path path;
2504 struct extent_io_tree io_tree;
2519 extent_io_tree_init(&io_tree);
2521 fd = open(devname, O_RDWR);
2523 fprintf(stderr, "unable to open %s\n", devname);
2526 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2528 fprintf(stderr, "unable to open ctree\n");
2531 ret = may_rollback(root);
2533 fprintf(stderr, "unable to do rollback\n");
2537 sectorsize = root->sectorsize;
2538 buf = malloc(sectorsize);
2540 fprintf(stderr, "unable to allocate memory\n");
2544 btrfs_init_path(&path);
2546 key.objectid = EXT2_IMAGE_SUBVOL_OBJECTID;
2547 key.type = BTRFS_ROOT_ITEM_KEY;
2548 key.offset = (u64)-1;
2549 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2550 if (!ext2_root || IS_ERR(ext2_root)) {
2551 fprintf(stderr, "unable to open subvol %llu\n",
2552 (unsigned long long)key.objectid);
2557 root_dir = btrfs_root_dirid(&root->root_item);
2558 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2559 root_dir, name, strlen(name), 0);
2560 if (!dir || IS_ERR(dir)) {
2561 fprintf(stderr, "unable to find file %s\n", name);
2564 leaf = path.nodes[0];
2565 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2566 btrfs_release_path(&path);
2568 objectid = key.objectid;
2570 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2572 fprintf(stderr, "unable to find inode item\n");
2575 leaf = path.nodes[0];
2576 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2577 total_bytes = btrfs_inode_size(leaf, inode);
2578 btrfs_release_path(&path);
2580 key.objectid = objectid;
2582 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2583 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2585 fprintf(stderr, "unable to find first file extent\n");
2586 btrfs_release_path(&path);
2590 /* build mapping tree for the relocated blocks */
2591 for (offset = 0; offset < total_bytes; ) {
2592 leaf = path.nodes[0];
2593 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2594 ret = btrfs_next_leaf(root, &path);
2600 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2601 if (key.objectid != objectid || key.offset != offset ||
2602 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2605 fi = btrfs_item_ptr(leaf, path.slots[0],
2606 struct btrfs_file_extent_item);
2607 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2609 if (btrfs_file_extent_compression(leaf, fi) ||
2610 btrfs_file_extent_encryption(leaf, fi) ||
2611 btrfs_file_extent_other_encoding(leaf, fi))
2614 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2615 /* skip holes and direct mapped extents */
2616 if (bytenr == 0 || bytenr == offset)
2619 bytenr += btrfs_file_extent_offset(leaf, fi);
2620 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2622 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2623 cache2 = btrfs_lookup_block_group(root->fs_info,
2624 offset + num_bytes - 1);
2625 if (!cache1 || cache1 != cache2 ||
2626 (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
2627 !intersect_with_sb(offset, num_bytes)))
2630 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2631 EXTENT_LOCKED, GFP_NOFS);
2632 set_state_private(&io_tree, offset, bytenr);
2634 offset += btrfs_file_extent_num_bytes(leaf, fi);
2637 btrfs_release_path(&path);
2639 if (offset < total_bytes) {
2640 fprintf(stderr, "unable to build extent mapping\n");
2644 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2645 first_free &= ~((u64)sectorsize - 1);
2646 /* backup for extent #0 should exist */
2647 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2648 fprintf(stderr, "no backup for the first extent\n");
2651 /* force no allocation from system block group */
2652 root->fs_info->system_allocs = -1;
2653 trans = btrfs_start_transaction(root, 1);
2656 * recow the whole chunk tree, this will remove all chunk tree blocks
2657 * from system block group
2659 chunk_root = root->fs_info->chunk_root;
2660 memset(&key, 0, sizeof(key));
2662 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2666 ret = btrfs_next_leaf(chunk_root, &path);
2670 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2671 btrfs_release_path(&path);
2673 btrfs_release_path(&path);
2678 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2682 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2683 num_bytes += btrfs_block_group_used(&cache1->item);
2685 offset = cache1->key.objectid + cache1->key.offset;
2687 /* only extent #0 left in system block group? */
2688 if (num_bytes > first_free) {
2689 fprintf(stderr, "unable to empty system block group\n");
2692 /* create a system chunk that maps the whole device */
2693 ret = prepare_system_chunk_sb(root->fs_info->super_copy);
2695 fprintf(stderr, "unable to update system chunk\n");
2699 ret = btrfs_commit_transaction(trans, root);
2702 ret = close_ctree(root);
2704 fprintf(stderr, "error during close_ctree %d\n", ret);
2708 /* zero btrfs super block mirrors */
2709 memset(buf, 0, sectorsize);
2710 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2711 bytenr = btrfs_sb_offset(i);
2712 if (bytenr >= total_bytes)
2714 ret = pwrite(fd, buf, sectorsize, bytenr);
2715 if (ret != sectorsize) {
2717 "error during zeroing supreblock %d: %d\n",
2723 sb_bytenr = (u64)-1;
2724 /* copy all relocated blocks back */
2726 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2731 ret = get_state_private(&io_tree, start, &bytenr);
2734 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2737 while (start <= end) {
2738 if (start == BTRFS_SUPER_INFO_OFFSET) {
2742 ret = pread(fd, buf, sectorsize, bytenr);
2744 fprintf(stderr, "error during pread %d\n", ret);
2747 BUG_ON(ret != sectorsize);
2748 ret = pwrite(fd, buf, sectorsize, start);
2750 fprintf(stderr, "error during pwrite %d\n", ret);
2753 BUG_ON(ret != sectorsize);
2755 start += sectorsize;
2756 bytenr += sectorsize;
2762 fprintf(stderr, "error during fsync %d\n", ret);
2766 * finally, overwrite btrfs super block.
2768 ret = pread(fd, buf, sectorsize, sb_bytenr);
2770 fprintf(stderr, "error during pread %d\n", ret);
2773 BUG_ON(ret != sectorsize);
2774 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2776 fprintf(stderr, "error during pwrite %d\n", ret);
2779 BUG_ON(ret != sectorsize);
2782 fprintf(stderr, "error during fsync %d\n", ret);
2788 extent_io_tree_cleanup(&io_tree);
2789 printf("rollback complete.\n");
2796 fprintf(stderr, "rollback aborted.\n");
2800 static void print_usage(void)
2802 printf("usage: btrfs-convert [options] device\n");
2803 printf("options:\n");
2804 printf("\t-d|--no-datasum disable data checksum, sets NODATASUM\n");
2805 printf("\t-i|--no-xattr ignore xattrs and ACLs\n");
2806 printf("\t-n|--no-inline disable inlining of small files to metadata\n");
2807 printf("\t-N|--nodesize SIZE set filesystem metadata nodesize\n");
2808 printf("\t-r|--rollback roll back to ext2fs\n");
2809 printf("\t-l|--label LABEL set filesystem label\n");
2810 printf("\t-L|--copy-label use label from converted filesystem\n");
2811 printf("\t-p|--progress show converting progress (default)\n");
2812 printf("\t--no-progress show only overview, not the detailed progress\n");
2815 int main(int argc, char *argv[])
2821 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
2822 BTRFS_MKFS_DEFAULT_NODE_SIZE);
2825 int usage_error = 0;
2828 char *fslabel = NULL;
2832 enum { GETOPT_VAL_NO_PROGRESS = 256 };
2833 static const struct option long_options[] = {
2834 { "no-progress", no_argument, NULL,
2835 GETOPT_VAL_NO_PROGRESS },
2836 { "no-datasum", no_argument, NULL, 'd' },
2837 { "no-inline", no_argument, NULL, 'n' },
2838 { "no-xattr", no_argument, NULL, 'i' },
2839 { "rollback", no_argument, NULL, 'r' },
2840 { "progress", no_argument, NULL, 'p' },
2841 { "label", required_argument, NULL, 'l' },
2842 { "copy-label", no_argument, NULL, 'L' },
2843 { "nodesize", required_argument, NULL, 'N' },
2844 { NULL, 0, NULL, 0 }
2846 int c = getopt_long(argc, argv, "dinN:rl:Lp", long_options,
2862 nodesize = parse_size(optarg);
2869 fslabel = strdup(optarg);
2870 if (strlen(fslabel) > BTRFS_LABEL_SIZE) {
2872 "warning: label too long, trimmed to %d bytes\n",
2874 fslabel[BTRFS_LABEL_SIZE] = 0;
2883 case GETOPT_VAL_NO_PROGRESS:
2891 argc = argc - optind;
2893 if (check_argc_exact(argc, 1)) {
2898 if (rollback && (!datacsum || noxattr || !packing)) {
2900 "Usage error: -d, -i, -n options do not apply to rollback\n");
2909 file = argv[optind];
2910 ret = check_mounted(file);
2912 fprintf(stderr, "Could not check mount status: %s\n",
2916 fprintf(stderr, "%s is mounted\n", file);
2921 ret = do_rollback(file);
2923 ret = do_convert(file, datacsum, packing, noxattr, nodesize,
2924 copylabel, fslabel, progress);