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 #define _XOPEN_SOURCE 600
22 #include "kerncompat.h"
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
28 #include <sys/types.h>
33 #include <uuid/uuid.h>
38 #include "transaction.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 * Open Ext2fs in readonly mode, read block allocation bitmap and
50 * inode bitmap into memory.
52 static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
57 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
59 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
62 ret = ext2fs_read_inode_bitmap(ext2_fs);
64 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
68 ret = ext2fs_read_block_bitmap(ext2_fs);
70 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
75 * search each block group for a free inode. this set up
76 * uninit block/inode bitmaps appropriately.
79 while (ino <= ext2_fs->super->s_inodes_count) {
81 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
82 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
91 static int close_ext2fs(ext2_filsys fs)
97 static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
101 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
102 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
109 static int ext2_free_block(ext2_filsys fs, u64 block)
111 BUG_ON(block != (blk_t)block);
112 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
116 static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
122 u64 blocksize = ext2_fs->blocksize;
124 block = ext2_fs->super->s_first_data_block;
125 for (; block < ext2_fs->super->s_blocks_count; block++) {
126 if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
128 bytenr = block * blocksize;
129 ret = set_extent_dirty(&root->fs_info->free_space_cache,
130 bytenr, bytenr + blocksize - 1, 0);
134 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
135 bytenr = btrfs_sb_offset(i);
136 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
137 if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
139 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
140 bytenr + BTRFS_STRIPE_LEN - 1, 0);
143 clear_extent_dirty(&root->fs_info->free_space_cache,
144 0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
149 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
150 u64 hint_byte, struct btrfs_key *ins)
154 u64 last = hint_byte;
157 struct btrfs_block_group_cache *cache;
160 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
161 last, &start, &end, EXTENT_DIRTY);
163 if (wrapped++ == 0) {
171 start = max(last, start);
173 if (last - start < num_bytes)
176 last = start + num_bytes;
177 if (test_range_bit(&root->fs_info->pinned_extents,
178 start, last - 1, EXTENT_DIRTY, 0))
181 cache = btrfs_lookup_block_group(root->fs_info, start);
183 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
184 last > cache->key.objectid + cache->key.offset) {
185 last = cache->key.objectid + cache->key.offset;
189 clear_extent_dirty(&root->fs_info->free_space_cache,
190 start, start + num_bytes - 1, 0);
192 ins->objectid = start;
193 ins->offset = num_bytes;
194 ins->type = BTRFS_EXTENT_ITEM_KEY;
198 fprintf(stderr, "not enough free space\n");
202 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
207 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
208 offset = btrfs_sb_offset(i);
209 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
211 if (bytenr < offset + BTRFS_STRIPE_LEN &&
212 bytenr + num_bytes > offset)
218 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
221 return intersect_with_sb(bytenr, num_bytes);
224 static struct btrfs_extent_ops extent_ops = {
225 .alloc_extent = custom_alloc_extent,
226 .free_extent = custom_free_extent,
229 struct dir_iterate_data {
230 struct btrfs_trans_handle *trans;
231 struct btrfs_root *root;
232 struct btrfs_inode_item *inode;
239 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
240 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
241 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
242 [EXT2_FT_DIR] = BTRFS_FT_DIR,
243 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
244 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
245 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
246 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
247 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
250 static int dir_iterate_proc(ext2_ino_t dir, int entry,
251 struct ext2_dir_entry *dirent,
252 int offset, int blocksize,
253 char *buf,void *priv_data)
259 char dotdot[] = "..";
260 struct btrfs_key location;
261 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
264 name_len = dirent->name_len & 0xFF;
266 objectid = dirent->inode + INO_OFFSET;
267 if (!strncmp(dirent->name, dotdot, name_len)) {
269 BUG_ON(idata->parent != 0);
270 idata->parent = objectid;
274 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
277 location.objectid = objectid;
279 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
281 file_type = dirent->name_len >> 8;
282 BUG_ON(file_type > EXT2_FT_SYMLINK);
283 ret = btrfs_insert_dir_item(idata->trans, idata->root,
284 dirent->name, name_len,
285 idata->objectid, &location,
286 filetype_conversion_table[file_type],
290 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
291 dirent->name, name_len,
292 objectid, idata->objectid,
297 inode_size = btrfs_stack_inode_size(idata->inode) +
299 btrfs_set_stack_inode_size(idata->inode, inode_size);
302 idata->errcode = ret;
306 static int create_dir_entries(struct btrfs_trans_handle *trans,
307 struct btrfs_root *root, u64 objectid,
308 struct btrfs_inode_item *btrfs_inode,
309 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
313 struct dir_iterate_data data = {
316 .inode = btrfs_inode,
317 .objectid = objectid,
323 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
324 dir_iterate_proc, &data);
328 if (ret == 0 && data.parent == objectid) {
329 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
330 objectid, objectid, 0);
334 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
338 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
339 u32 num_bytes, char *buffer)
342 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
344 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
345 if (ret != num_bytes)
354 static int csum_disk_extent(struct btrfs_trans_handle *trans,
355 struct btrfs_root *root,
356 u64 disk_bytenr, u64 num_bytes)
358 u32 blocksize = root->sectorsize;
363 buffer = malloc(blocksize);
366 for (offset = 0; offset < num_bytes; offset += blocksize) {
367 ret = read_disk_extent(root, disk_bytenr + offset,
371 ret = btrfs_csum_file_block(trans,
372 root->fs_info->csum_root,
373 disk_bytenr + num_bytes,
374 disk_bytenr + offset,
383 static int record_file_blocks(struct btrfs_trans_handle *trans,
384 struct btrfs_root *root, u64 objectid,
385 struct btrfs_inode_item *inode,
386 u64 file_block, u64 disk_block,
387 u64 num_blocks, int checksum)
390 u64 file_pos = file_block * root->sectorsize;
391 u64 disk_bytenr = disk_block * root->sectorsize;
392 u64 num_bytes = num_blocks * root->sectorsize;
393 ret = btrfs_record_file_extent(trans, root, objectid, inode, file_pos,
394 disk_bytenr, num_bytes);
396 if (ret || !checksum)
399 return csum_disk_extent(trans, root, disk_bytenr, num_bytes);
402 struct blk_iterate_data {
403 struct btrfs_trans_handle *trans;
404 struct btrfs_root *root;
405 struct btrfs_inode_item *inode;
415 static int block_iterate_proc(ext2_filsys ext2_fs,
416 u64 disk_block, u64 file_block,
417 struct blk_iterate_data *idata)
422 struct btrfs_root *root = idata->root;
423 struct btrfs_trans_handle *trans = idata->trans;
424 struct btrfs_block_group_cache *cache;
425 u64 bytenr = disk_block * root->sectorsize;
427 sb_region = intersect_with_sb(bytenr, root->sectorsize);
428 do_barrier = sb_region || disk_block >= idata->boundary;
429 if ((idata->num_blocks > 0 && do_barrier) ||
430 (file_block > idata->first_block + idata->num_blocks) ||
431 (disk_block != idata->disk_block + idata->num_blocks)) {
432 if (idata->num_blocks > 0) {
433 ret = record_file_blocks(trans, root, idata->objectid,
434 idata->inode, idata->first_block,
435 idata->disk_block, idata->num_blocks,
439 idata->first_block += idata->num_blocks;
440 idata->num_blocks = 0;
442 if (file_block > idata->first_block) {
443 ret = record_file_blocks(trans, root, idata->objectid,
444 idata->inode, idata->first_block,
445 0, file_block - idata->first_block,
452 bytenr += BTRFS_STRIPE_LEN - 1;
453 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
455 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
457 bytenr = cache->key.objectid + cache->key.offset;
460 idata->first_block = file_block;
461 idata->disk_block = disk_block;
462 idata->boundary = bytenr / root->sectorsize;
467 idata->errcode = ret;
471 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
472 e2_blkcnt_t blockcnt, blk_t ref_block,
473 int ref_offset, void *priv_data)
475 struct blk_iterate_data *idata;
476 idata = (struct blk_iterate_data *)priv_data;
477 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
481 * traverse file's data blocks, record these data blocks as file extents.
483 static int create_file_extents(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root, u64 objectid,
485 struct btrfs_inode_item *btrfs_inode,
486 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
487 int datacsum, int packing)
493 u32 sectorsize = root->sectorsize;
494 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
495 struct blk_iterate_data data = {
498 .inode = btrfs_inode,
499 .objectid = objectid,
504 .checksum = datacsum,
507 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
508 NULL, __block_iterate_proc, &data);
514 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
515 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
516 u64 num_bytes = data.num_blocks * sectorsize;
517 u64 disk_bytenr = data.disk_block * sectorsize;
520 buffer = malloc(num_bytes);
523 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
526 if (num_bytes > inode_size)
527 num_bytes = inode_size;
528 ret = btrfs_insert_inline_extent(trans, root, objectid,
529 0, buffer, num_bytes);
532 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
533 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
534 } else if (data.num_blocks > 0) {
535 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
536 data.first_block, data.disk_block,
537 data.num_blocks, data.checksum);
541 data.first_block += data.num_blocks;
542 last_block = (inode_size + sectorsize - 1) / sectorsize;
543 if (last_block > data.first_block) {
544 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
545 data.first_block, 0, last_block -
546 data.first_block, data.checksum);
552 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
556 static int create_symbol_link(struct btrfs_trans_handle *trans,
557 struct btrfs_root *root, u64 objectid,
558 struct btrfs_inode_item *btrfs_inode,
559 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
560 struct ext2_inode *ext2_inode)
564 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
565 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
566 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
567 ret = create_file_extents(trans, root, objectid, btrfs_inode,
568 ext2_fs, ext2_ino, 1, 1);
569 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
573 pathname = (char *)&(ext2_inode->i_block[0]);
574 BUG_ON(pathname[inode_size] != 0);
575 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
576 pathname, inode_size + 1);
577 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
582 * Following xattr/acl related codes are based on codes in
583 * fs/ext3/xattr.c and fs/ext3/acl.c
585 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
586 #define EXT2_XATTR_BFIRST(ptr) \
587 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
588 #define EXT2_XATTR_IHDR(inode) \
589 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
590 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
591 #define EXT2_XATTR_IFIRST(inode) \
592 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
593 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
595 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
598 struct ext2_ext_attr_entry *next;
600 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
601 next = EXT2_EXT_ATTR_NEXT(entry);
602 if ((void *)next >= end)
609 static int ext2_xattr_check_block(const char *buf, size_t size)
612 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
614 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
615 header->h_blocks != 1)
617 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
621 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
624 size_t value_size = entry->e_value_size;
626 if (entry->e_value_block != 0 || value_size > size ||
627 entry->e_value_offs + value_size > size)
632 #define EXT2_ACL_VERSION 0x0001
643 } ext2_acl_entry_short;
649 static inline int ext2_acl_count(size_t size)
652 size -= sizeof(ext2_acl_header);
653 s = size - 4 * sizeof(ext2_acl_entry_short);
655 if (size % sizeof(ext2_acl_entry_short))
657 return size / sizeof(ext2_acl_entry_short);
659 if (s % sizeof(ext2_acl_entry))
661 return s / sizeof(ext2_acl_entry) + 4;
665 #define ACL_EA_VERSION 0x0002
675 acl_ea_entry a_entries[0];
678 static inline size_t acl_ea_size(int count)
680 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
683 static int ext2_acl_to_xattr(void *dst, const void *src,
684 size_t dst_size, size_t src_size)
687 const void *end = src + src_size;
688 acl_ea_header *ext_acl = (acl_ea_header *)dst;
689 acl_ea_entry *dst_entry = ext_acl->a_entries;
690 ext2_acl_entry *src_entry;
692 if (src_size < sizeof(ext2_acl_header))
694 if (((ext2_acl_header *)src)->a_version !=
695 cpu_to_le32(EXT2_ACL_VERSION))
697 src += sizeof(ext2_acl_header);
698 count = ext2_acl_count(src_size);
702 BUG_ON(dst_size < acl_ea_size(count));
703 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
704 for (i = 0; i < count; i++, dst_entry++) {
705 src_entry = (ext2_acl_entry *)src;
706 if (src + sizeof(ext2_acl_entry_short) > end)
708 dst_entry->e_tag = src_entry->e_tag;
709 dst_entry->e_perm = src_entry->e_perm;
710 switch (le16_to_cpu(src_entry->e_tag)) {
715 src += sizeof(ext2_acl_entry_short);
716 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
720 src += sizeof(ext2_acl_entry);
723 dst_entry->e_id = src_entry->e_id;
736 static char *xattr_prefix_table[] = {
738 [2] = "system.posix_acl_access",
739 [3] = "system.posix_acl_default",
744 static int copy_single_xattr(struct btrfs_trans_handle *trans,
745 struct btrfs_root *root, u64 objectid,
746 struct ext2_ext_attr_entry *entry,
747 const void *data, u32 datalen)
752 void *databuf = NULL;
753 char namebuf[XATTR_NAME_MAX + 1];
755 name_index = entry->e_name_index;
756 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
757 xattr_prefix_table[name_index] == NULL)
759 name_len = strlen(xattr_prefix_table[name_index]) +
761 if (name_len >= sizeof(namebuf))
764 if (name_index == 2 || name_index == 3) {
765 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
766 databuf = malloc(bufsize);
769 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
775 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
776 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
777 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
778 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
779 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
780 objectid - INO_OFFSET, name_len, namebuf);
783 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
784 data, datalen, objectid);
790 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
791 struct btrfs_root *root, u64 objectid,
792 struct btrfs_inode_item *btrfs_inode,
793 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
799 u32 block_size = ext2_fs->blocksize;
800 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
801 struct ext2_inode_large *ext2_inode;
802 struct ext2_ext_attr_entry *entry;
805 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
807 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
808 ext2_inode = (struct ext2_inode_large *)inode_buf;
810 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
814 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
817 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
823 if (ext2_ino > ext2_fs->super->s_first_ino &&
824 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
825 if (EXT2_GOOD_OLD_INODE_SIZE +
826 ext2_inode->i_extra_isize > inode_size) {
830 if (ext2_inode->i_extra_isize != 0 &&
831 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
832 EXT2_EXT_ATTR_MAGIC) {
838 void *end = (void *)ext2_inode + inode_size;
839 entry = EXT2_XATTR_IFIRST(ext2_inode);
840 total = end - (void *)entry;
841 ret = ext2_xattr_check_names(entry, end);
844 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
845 ret = ext2_xattr_check_entry(entry, total);
848 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
850 datalen = entry->e_value_size;
851 ret = copy_single_xattr(trans, root, objectid,
852 entry, data, datalen);
855 entry = EXT2_EXT_ATTR_NEXT(entry);
859 if (ext2_inode->i_file_acl == 0)
862 buffer = malloc(block_size);
867 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
869 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
874 ret = ext2_xattr_check_block(buffer, block_size);
878 entry = EXT2_XATTR_BFIRST(buffer);
879 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
880 ret = ext2_xattr_check_entry(entry, block_size);
883 data = buffer + entry->e_value_offs;
884 datalen = entry->e_value_size;
885 ret = copy_single_xattr(trans, root, objectid,
886 entry, data, datalen);
889 entry = EXT2_EXT_ATTR_NEXT(entry);
893 if ((void *)ext2_inode != inode_buf)
898 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
900 static inline dev_t old_decode_dev(u16 val)
902 return MKDEV((val >> 8) & 255, val & 255);
905 static inline dev_t new_decode_dev(u32 dev)
907 unsigned major = (dev & 0xfff00) >> 8;
908 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
909 return MKDEV(major, minor);
912 static int copy_inode_item(struct btrfs_inode_item *dst,
913 struct ext2_inode *src, u32 blocksize)
915 btrfs_set_stack_inode_generation(dst, 1);
916 btrfs_set_stack_inode_size(dst, src->i_size);
917 btrfs_set_stack_inode_nbytes(dst, 0);
918 btrfs_set_stack_inode_block_group(dst, 0);
919 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
920 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
921 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
922 btrfs_set_stack_inode_mode(dst, src->i_mode);
923 btrfs_set_stack_inode_rdev(dst, 0);
924 btrfs_set_stack_inode_flags(dst, 0);
925 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
926 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
927 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
928 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
929 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
930 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
931 btrfs_set_stack_timespec_sec(&dst->otime, 0);
932 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
934 if (S_ISDIR(src->i_mode)) {
935 btrfs_set_stack_inode_size(dst, 0);
936 btrfs_set_stack_inode_nlink(dst, 1);
938 if (S_ISREG(src->i_mode)) {
939 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
942 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
943 !S_ISLNK(src->i_mode)) {
944 if (src->i_block[0]) {
945 btrfs_set_stack_inode_rdev(dst,
946 old_decode_dev(src->i_block[0]));
948 btrfs_set_stack_inode_rdev(dst,
949 new_decode_dev(src->i_block[1]));
956 * copy a single inode. do all the required works, such as cloning
957 * inode item, creating file extents and creating directory entries.
959 static int copy_single_inode(struct btrfs_trans_handle *trans,
960 struct btrfs_root *root, u64 objectid,
961 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
962 struct ext2_inode *ext2_inode,
963 int datacsum, int packing, int noxattr)
966 struct btrfs_key inode_key;
967 struct btrfs_inode_item btrfs_inode;
969 if (ext2_inode->i_links_count == 0)
972 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
973 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
974 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
975 BTRFS_INODE_NODATASUM;
976 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
979 switch (ext2_inode->i_mode & S_IFMT) {
981 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
982 ext2_fs, ext2_ino, datacsum, packing);
985 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
989 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
990 ext2_fs, ext2_ino, ext2_inode);
1000 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1005 inode_key.objectid = objectid;
1006 inode_key.offset = 0;
1007 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1008 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1012 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1013 u64 src_bytenr, u32 num_bytes)
1017 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1019 buffer = malloc(num_bytes);
1022 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1023 if (ret != num_bytes)
1025 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1026 if (ret != num_bytes)
1036 * scan ext2's inode bitmap and copy all used inodes.
1038 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1039 int datacsum, int packing, int noxattr)
1043 ext2_inode_scan ext2_scan;
1044 struct ext2_inode ext2_inode;
1045 ext2_ino_t ext2_ino;
1047 struct btrfs_trans_handle *trans;
1049 trans = btrfs_start_transaction(root, 1);
1052 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1054 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1057 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1059 /* no more inodes */
1062 /* skip special inode in ext2fs */
1063 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1064 ext2_ino != EXT2_ROOT_INO)
1066 objectid = ext2_ino + INO_OFFSET;
1067 ret = copy_single_inode(trans, root,
1068 objectid, ext2_fs, ext2_ino,
1069 &ext2_inode, datacsum, packing,
1073 if (trans->blocks_used >= 4096) {
1074 ret = btrfs_commit_transaction(trans, root);
1076 trans = btrfs_start_transaction(root, 1);
1081 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1084 ret = btrfs_commit_transaction(trans, root);
1091 * Construct a range of ext2fs image file.
1092 * scan block allocation bitmap, find all blocks used by the ext2fs
1093 * in this range and create file extents that point to these blocks.
1095 * Note: Before calling the function, no file extent points to blocks
1098 static int create_image_file_range(struct btrfs_trans_handle *trans,
1099 struct btrfs_root *root, u64 objectid,
1100 struct btrfs_inode_item *inode,
1101 u64 start_byte, u64 end_byte,
1102 ext2_filsys ext2_fs)
1104 u32 blocksize = ext2_fs->blocksize;
1105 u32 block = start_byte / blocksize;
1106 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1108 struct blk_iterate_data data = {
1112 .objectid = objectid,
1113 .first_block = block,
1116 .boundary = (u64)-1,
1120 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1121 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1123 ret = block_iterate_proc(NULL, block, block, &data);
1124 if (ret & BLOCK_ABORT) {
1129 if (data.num_blocks > 0) {
1130 ret = record_file_blocks(trans, root, objectid, inode,
1131 data.first_block, data.disk_block,
1132 data.num_blocks, 0);
1135 data.first_block += data.num_blocks;
1137 if (last_block > data.first_block) {
1138 ret = record_file_blocks(trans, root, objectid, inode,
1139 data.first_block, 0, last_block -
1140 data.first_block, 0);
1148 * Create the ext2fs image file.
1150 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1154 struct btrfs_key key;
1155 struct btrfs_key location;
1156 struct btrfs_path path;
1157 struct btrfs_inode_item btrfs_inode;
1158 struct btrfs_inode_item *inode_item;
1159 struct extent_buffer *leaf;
1160 struct btrfs_fs_info *fs_info = root->fs_info;
1161 struct btrfs_root *extent_root = fs_info->extent_root;
1162 struct btrfs_trans_handle *trans;
1163 struct btrfs_extent_item *ei;
1164 struct btrfs_extent_inline_ref *iref;
1165 struct btrfs_extent_data_ref *dref;
1172 u32 sectorsize = root->sectorsize;
1174 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1175 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1176 first_free &= ~((u64)sectorsize - 1);
1178 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1179 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1180 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1181 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1182 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1183 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1184 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1185 BTRFS_INODE_READONLY);
1186 btrfs_init_path(&path);
1187 trans = btrfs_start_transaction(root, 1);
1190 objectid = btrfs_root_dirid(&root->root_item);
1191 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1196 * copy blocks covered by extent #0 to new positions. extent #0 is
1197 * special, we can't rely on relocate_extents_range to relocate it.
1199 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1200 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1203 ret = copy_disk_extent(root, key.objectid, last_byte,
1207 ret = btrfs_record_file_extent(trans, root, objectid,
1208 &btrfs_inode, last_byte,
1209 key.objectid, sectorsize);
1215 key.objectid = last_byte;
1217 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1218 ret = btrfs_search_slot(trans, fs_info->extent_root,
1223 leaf = path.nodes[0];
1224 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1225 ret = btrfs_next_leaf(extent_root, &path);
1230 leaf = path.nodes[0];
1232 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1233 if (last_byte > key.objectid ||
1234 key.type != BTRFS_EXTENT_ITEM_KEY) {
1239 bytenr = key.objectid;
1240 num_bytes = key.offset;
1241 ei = btrfs_item_ptr(leaf, path.slots[0],
1242 struct btrfs_extent_item);
1243 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1248 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1249 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1251 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1252 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1253 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1254 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1255 if (btrfs_extent_data_ref_root(leaf, dref) !=
1256 BTRFS_FS_TREE_OBJECTID) {
1261 if (bytenr > last_byte) {
1262 ret = create_image_file_range(trans, root, objectid,
1263 &btrfs_inode, last_byte,
1268 ret = btrfs_record_file_extent(trans, root, objectid,
1269 &btrfs_inode, bytenr, bytenr,
1273 last_byte = bytenr + num_bytes;
1274 btrfs_release_path(&path);
1276 if (trans->blocks_used >= 4096) {
1277 ret = btrfs_commit_transaction(trans, root);
1279 trans = btrfs_start_transaction(root, 1);
1283 btrfs_release_path(&path);
1284 if (total_bytes > last_byte) {
1285 ret = create_image_file_range(trans, root, objectid,
1286 &btrfs_inode, last_byte,
1287 total_bytes, ext2_fs);
1292 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1296 location.objectid = objectid;
1297 location.offset = 0;
1298 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1299 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1300 btrfs_root_dirid(&root->root_item),
1301 &location, EXT2_FT_REG_FILE, objectid);
1304 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1306 btrfs_root_dirid(&root->root_item),
1310 location.objectid = btrfs_root_dirid(&root->root_item);
1311 location.offset = 0;
1312 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1313 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1316 leaf = path.nodes[0];
1317 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1318 struct btrfs_inode_item);
1319 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1320 btrfs_inode_size(leaf, inode_item));
1321 btrfs_mark_buffer_dirty(leaf);
1322 btrfs_release_path(&path);
1323 ret = btrfs_commit_transaction(trans, root);
1326 btrfs_release_path(&path);
1330 static struct btrfs_root * link_subvol(struct btrfs_root *root,
1331 const char *base, u64 root_objectid)
1333 struct btrfs_trans_handle *trans;
1334 struct btrfs_fs_info *fs_info = root->fs_info;
1335 struct btrfs_root *tree_root = fs_info->tree_root;
1336 struct btrfs_root *new_root = NULL;
1337 struct btrfs_path *path;
1338 struct btrfs_inode_item *inode_item;
1339 struct extent_buffer *leaf;
1340 struct btrfs_key key;
1341 u64 dirid = btrfs_root_dirid(&root->root_item);
1343 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
1349 if (len < 1 || len > BTRFS_NAME_LEN)
1352 path = btrfs_alloc_path();
1355 key.objectid = dirid;
1356 key.type = BTRFS_DIR_INDEX_KEY;
1357 key.offset = (u64)-1;
1359 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1362 if (path->slots[0] > 0) {
1364 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1365 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1366 index = key.offset + 1;
1368 btrfs_release_path(path);
1370 trans = btrfs_start_transaction(root, 1);
1373 key.objectid = dirid;
1375 key.type = BTRFS_INODE_ITEM_KEY;
1377 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1379 leaf = path->nodes[0];
1380 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1381 struct btrfs_inode_item);
1383 key.objectid = root_objectid;
1384 key.offset = (u64)-1;
1385 key.type = BTRFS_ROOT_ITEM_KEY;
1387 memcpy(buf, base, len);
1388 for (i = 0; i < 1024; i++) {
1389 ret = btrfs_insert_dir_item(trans, root, buf, len,
1390 dirid, &key, BTRFS_FT_DIR, index);
1393 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
1394 if (len < 1 || len > BTRFS_NAME_LEN) {
1402 btrfs_set_inode_size(leaf, inode_item, len * 2 +
1403 btrfs_inode_size(leaf, inode_item));
1404 btrfs_mark_buffer_dirty(leaf);
1405 btrfs_release_path(path);
1407 /* add the backref first */
1408 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1409 BTRFS_ROOT_BACKREF_KEY,
1410 root->root_key.objectid,
1411 dirid, index, buf, len);
1414 /* now add the forward ref */
1415 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1416 BTRFS_ROOT_REF_KEY, root_objectid,
1417 dirid, index, buf, len);
1419 ret = btrfs_commit_transaction(trans, root);
1422 new_root = btrfs_read_fs_root(fs_info, &key);
1423 if (IS_ERR(new_root))
1426 btrfs_free_path(path);
1430 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1431 struct btrfs_root *root)
1433 struct btrfs_fs_info *info = root->fs_info;
1434 struct btrfs_root *chunk_root = info->chunk_root;
1435 struct btrfs_root *extent_root = info->extent_root;
1436 struct btrfs_device *device;
1437 struct btrfs_block_group_cache *cache;
1438 struct btrfs_dev_extent *extent;
1439 struct extent_buffer *leaf;
1440 struct btrfs_chunk chunk;
1441 struct btrfs_key key;
1442 struct btrfs_path path;
1448 btrfs_init_path(&path);
1450 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1451 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1453 BUG_ON(list_empty(&info->fs_devices->devices));
1454 device = list_entry(info->fs_devices->devices.next,
1455 struct btrfs_device, dev_list);
1456 BUG_ON(device->devid != info->fs_devices->latest_devid);
1458 /* delete device extent created by make_btrfs */
1459 key.objectid = device->devid;
1461 key.type = BTRFS_DEV_EXTENT_KEY;
1462 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1467 ret = btrfs_del_item(trans, device->dev_root, &path);
1470 btrfs_release_path(&path);
1472 /* delete chunk item created by make_btrfs */
1473 key.objectid = chunk_objectid;
1475 key.type = BTRFS_CHUNK_ITEM_KEY;
1476 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1481 ret = btrfs_del_item(trans, chunk_root, &path);
1484 btrfs_release_path(&path);
1486 /* for each block group, create device extent and chunk item */
1488 while (cur_start < total_bytes) {
1489 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1492 /* insert device extent */
1493 key.objectid = device->devid;
1494 key.offset = cache->key.objectid;
1495 key.type = BTRFS_DEV_EXTENT_KEY;
1496 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1497 &key, sizeof(*extent));
1501 leaf = path.nodes[0];
1502 extent = btrfs_item_ptr(leaf, path.slots[0],
1503 struct btrfs_dev_extent);
1505 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1506 chunk_root->root_key.objectid);
1507 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1509 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1510 cache->key.objectid);
1511 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1512 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1513 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1515 btrfs_mark_buffer_dirty(leaf);
1516 btrfs_release_path(&path);
1518 /* insert chunk item */
1519 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1520 btrfs_set_stack_chunk_owner(&chunk,
1521 extent_root->root_key.objectid);
1522 btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
1523 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1524 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1525 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1526 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1527 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1528 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1529 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1530 btrfs_set_stack_stripe_offset(&chunk.stripe,
1531 cache->key.objectid);
1532 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1534 key.objectid = chunk_objectid;
1535 key.offset = cache->key.objectid;
1536 key.type = BTRFS_CHUNK_ITEM_KEY;
1538 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1539 btrfs_chunk_item_size(1));
1543 cur_start = cache->key.objectid + cache->key.offset;
1546 device->bytes_used = total_bytes;
1547 ret = btrfs_update_device(trans, device);
1549 btrfs_release_path(&path);
1553 static int create_subvol(struct btrfs_trans_handle *trans,
1554 struct btrfs_root *root, u64 root_objectid)
1556 struct extent_buffer *tmp;
1557 struct btrfs_root *new_root;
1558 struct btrfs_key key;
1559 struct btrfs_root_item root_item;
1562 ret = btrfs_copy_root(trans, root, root->node, &tmp,
1566 memcpy(&root_item, &root->root_item, sizeof(root_item));
1567 btrfs_set_root_bytenr(&root_item, tmp->start);
1568 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1569 btrfs_set_root_generation(&root_item, trans->transid);
1570 free_extent_buffer(tmp);
1572 key.objectid = root_objectid;
1573 key.type = BTRFS_ROOT_ITEM_KEY;
1574 key.offset = trans->transid;
1575 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1578 key.offset = (u64)-1;
1579 new_root = btrfs_read_fs_root(root->fs_info, &key);
1580 BUG_ON(!new_root || IS_ERR(new_root));
1582 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1588 static int init_btrfs(struct btrfs_root *root)
1591 struct btrfs_key location;
1592 struct btrfs_trans_handle *trans;
1593 struct btrfs_fs_info *fs_info = root->fs_info;
1594 struct extent_buffer *tmp;
1596 trans = btrfs_start_transaction(root, 1);
1598 ret = btrfs_make_block_groups(trans, root);
1601 ret = btrfs_fix_block_accounting(trans, root);
1604 ret = create_chunk_mapping(trans, root);
1607 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1608 BTRFS_ROOT_TREE_DIR_OBJECTID);
1611 memcpy(&location, &root->root_key, sizeof(location));
1612 location.offset = (u64)-1;
1613 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1614 btrfs_super_root_dir(fs_info->super_copy),
1615 &location, BTRFS_FT_DIR, 0);
1618 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1620 btrfs_super_root_dir(fs_info->super_copy), 0);
1623 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1624 BTRFS_FIRST_FREE_OBJECTID);
1626 /* subvol for ext2 image file */
1627 ret = create_subvol(trans, root, EXT2_IMAGE_SUBVOL_OBJECTID);
1629 /* subvol for data relocation */
1630 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1633 extent_buffer_get(fs_info->csum_root->node);
1634 ret = __btrfs_cow_block(trans, fs_info->csum_root,
1635 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1637 free_extent_buffer(tmp);
1639 ret = btrfs_commit_transaction(trans, root);
1646 * Migrate super block to it's default position and zero 0 ~ 16k
1648 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1651 struct extent_buffer *buf;
1652 struct btrfs_super_block *super;
1656 BUG_ON(sectorsize < sizeof(*super));
1657 buf = malloc(sizeof(*buf) + sectorsize);
1661 buf->len = sectorsize;
1662 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1663 if (ret != sectorsize)
1666 super = (struct btrfs_super_block *)buf->data;
1667 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1668 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1670 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1671 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1672 if (ret != sectorsize)
1679 memset(buf->data, 0, sectorsize);
1680 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1681 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1682 if (len > sectorsize)
1684 ret = pwrite(fd, buf->data, len, bytenr);
1686 fprintf(stderr, "unable to zero fill device\n");
1700 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1702 struct btrfs_chunk *chunk;
1703 struct btrfs_disk_key *key;
1704 u32 sectorsize = btrfs_super_sectorsize(super);
1706 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1707 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1708 sizeof(struct btrfs_disk_key));
1710 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1711 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1712 btrfs_set_disk_key_offset(key, 0);
1714 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1715 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1716 btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1717 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1718 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1719 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1720 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1721 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1722 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1723 chunk->stripe.devid = super->dev_item.devid;
1724 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1725 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1726 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1730 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1733 struct extent_buffer *buf;
1734 struct btrfs_super_block *super;
1736 BUG_ON(sectorsize < sizeof(*super));
1737 buf = malloc(sizeof(*buf) + sectorsize);
1741 buf->len = sectorsize;
1742 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1743 if (ret != sectorsize)
1746 super = (struct btrfs_super_block *)buf->data;
1747 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1748 BUG_ON(btrfs_super_num_devices(super) != 1);
1750 ret = prepare_system_chunk_sb(super);
1754 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1755 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1756 if (ret != sectorsize)
1767 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *root,
1769 u64 extent_start, u64 extent_size,
1770 struct btrfs_key *extent_key,
1771 struct extent_io_tree *reloc_tree)
1773 struct extent_buffer *leaf;
1774 struct btrfs_file_extent_item *fi;
1775 struct btrfs_key key;
1776 struct btrfs_path path;
1777 struct btrfs_inode_item inode;
1778 struct blk_iterate_data data;
1785 u32 sectorsize = root->sectorsize;
1791 btrfs_init_path(&path);
1792 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
1796 leaf = path.nodes[0];
1797 fi = btrfs_item_ptr(leaf, path.slots[0],
1798 struct btrfs_file_extent_item);
1799 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
1800 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1801 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1806 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1807 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1809 ret = btrfs_del_item(trans, root, &path);
1813 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
1814 root->root_key.objectid,
1815 extent_key->objectid, extent_key->offset);
1819 btrfs_release_path(&path);
1821 key.objectid = extent_key->objectid;
1823 key.type = BTRFS_INODE_ITEM_KEY;
1824 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1828 leaf = path.nodes[0];
1829 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1830 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1831 btrfs_release_path(&path);
1833 BUG_ON(num_bytes & (sectorsize - 1));
1834 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
1835 btrfs_set_stack_inode_nbytes(&inode, nbytes);
1836 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
1838 data = (struct blk_iterate_data) {
1842 .objectid = extent_key->objectid,
1843 .first_block = extent_key->offset / sectorsize,
1846 .boundary = (u64)-1,
1847 .checksum = datacsum,
1851 cur_offset = extent_key->offset;
1852 while (num_bytes > 0) {
1853 sector_end = bytenr + sectorsize - 1;
1854 if (test_range_bit(reloc_tree, bytenr, sector_end,
1855 EXTENT_LOCKED, 1)) {
1856 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1859 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1862 new_pos = key.objectid;
1864 if (cur_offset == extent_key->offset) {
1865 fd = root->fs_info->fs_devices->latest_bdev;
1866 readahead(fd, bytenr, num_bytes);
1868 ret = copy_disk_extent(root, new_pos, bytenr,
1872 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1873 EXTENT_LOCKED, GFP_NOFS);
1875 ret = set_state_private(reloc_tree, bytenr, new_pos);
1879 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1880 cur_offset / sectorsize, &data);
1881 if (ret & BLOCK_ABORT) {
1886 cur_offset += sectorsize;
1887 bytenr += sectorsize;
1888 num_bytes -= sectorsize;
1891 if (data.num_blocks > 0) {
1892 ret = record_file_blocks(trans, root,
1893 extent_key->objectid, &inode,
1894 data.first_block, data.disk_block,
1895 data.num_blocks, datacsum);
1900 key.objectid = extent_key->objectid;
1902 key.type = BTRFS_INODE_ITEM_KEY;
1903 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1907 leaf = path.nodes[0];
1908 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1909 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1910 btrfs_mark_buffer_dirty(leaf);
1911 btrfs_release_path(&path);
1914 btrfs_release_path(&path);
1918 static int relocate_extents_range(struct btrfs_root *fs_root,
1919 struct btrfs_root *ext2_root,
1920 u64 start_byte, u64 end_byte)
1922 struct btrfs_fs_info *info = fs_root->fs_info;
1923 struct btrfs_root *extent_root = info->extent_root;
1924 struct btrfs_root *cur_root = NULL;
1925 struct btrfs_trans_handle *trans;
1926 struct btrfs_extent_data_ref *dref;
1927 struct btrfs_extent_inline_ref *iref;
1928 struct btrfs_extent_item *ei;
1929 struct extent_buffer *leaf;
1930 struct btrfs_key key;
1931 struct btrfs_key extent_key;
1932 struct btrfs_path path;
1933 struct extent_io_tree reloc_tree;
1943 btrfs_init_path(&path);
1944 extent_io_tree_init(&reloc_tree);
1946 key.objectid = start_byte;
1948 key.type = BTRFS_EXTENT_ITEM_KEY;
1949 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
1953 ret = btrfs_previous_item(extent_root, &path, 0,
1954 BTRFS_EXTENT_ITEM_KEY);
1958 leaf = path.nodes[0];
1959 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1960 if (key.objectid + key.offset > start_byte)
1961 start_byte = key.objectid;
1964 btrfs_release_path(&path);
1966 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
1969 trans = btrfs_start_transaction(cur_root, 1);
1972 cur_byte = start_byte;
1974 key.objectid = cur_byte;
1976 key.type = BTRFS_EXTENT_ITEM_KEY;
1977 ret = btrfs_search_slot(trans, extent_root,
1982 leaf = path.nodes[0];
1983 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1984 ret = btrfs_next_leaf(extent_root, &path);
1989 leaf = path.nodes[0];
1992 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1993 if (key.objectid < cur_byte ||
1994 key.type != BTRFS_EXTENT_ITEM_KEY) {
1998 if (key.objectid >= end_byte)
2003 cur_byte = key.objectid;
2004 num_bytes = key.offset;
2005 ei = btrfs_item_ptr(leaf, path.slots[0],
2006 struct btrfs_extent_item);
2007 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2008 BTRFS_EXTENT_FLAG_DATA));
2010 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2011 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2013 ptr += sizeof(struct btrfs_extent_item);
2016 iref = (struct btrfs_extent_inline_ref *)ptr;
2017 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2018 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2019 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2020 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2021 extent_key.objectid =
2022 btrfs_extent_data_ref_objectid(leaf, dref);
2024 btrfs_extent_data_ref_offset(leaf, dref);
2025 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2026 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2028 if (ref_root == cur_root->root_key.objectid)
2031 ptr += btrfs_extent_inline_ref_size(key.type);
2039 ret = relocate_one_reference(trans, cur_root, cur_byte,
2040 num_bytes, &extent_key,
2045 cur_byte += num_bytes;
2046 btrfs_release_path(&path);
2048 if (trans->blocks_used >= 4096) {
2049 ret = btrfs_commit_transaction(trans, cur_root);
2051 trans = btrfs_start_transaction(cur_root, 1);
2055 btrfs_release_path(&path);
2057 ret = btrfs_commit_transaction(trans, cur_root);
2060 if (num_extents > 0 && pass++ < 16)
2063 ret = (num_extents > 0) ? -1 : 0;
2065 btrfs_release_path(&path);
2066 extent_io_tree_cleanup(&reloc_tree);
2071 * relocate data in system chunk
2073 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2074 struct btrfs_root *ext2_root)
2076 struct btrfs_block_group_cache *cache;
2082 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2086 end_byte = cache->key.objectid + cache->key.offset;
2087 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2088 ret = relocate_extents_range(fs_root, ext2_root,
2089 cache->key.objectid,
2096 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2097 offset = btrfs_sb_offset(i);
2098 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2100 ret = relocate_extents_range(fs_root, ext2_root,
2101 offset, offset + BTRFS_STRIPE_LEN);
2110 static int fixup_chunk_mapping(struct btrfs_root *root)
2112 struct btrfs_trans_handle *trans;
2113 struct btrfs_fs_info *info = root->fs_info;
2114 struct btrfs_root *chunk_root = info->chunk_root;
2115 struct extent_buffer *leaf;
2116 struct btrfs_key key;
2117 struct btrfs_path path;
2118 struct btrfs_chunk chunk;
2124 btrfs_init_path(&path);
2126 trans = btrfs_start_transaction(root, 1);
2130 * recow the whole chunk tree. this will move all chunk tree blocks
2131 * into system block group.
2133 memset(&key, 0, sizeof(key));
2135 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2139 ret = btrfs_next_leaf(chunk_root, &path);
2145 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2146 btrfs_release_path(&path);
2148 btrfs_release_path(&path);
2150 /* fixup the system chunk array in super block */
2151 btrfs_set_super_sys_array_size(info->super_copy, 0);
2153 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2155 key.type = BTRFS_CHUNK_ITEM_KEY;
2157 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2162 leaf = path.nodes[0];
2163 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2164 ret = btrfs_next_leaf(chunk_root, &path);
2169 leaf = path.nodes[0];
2171 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2172 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2175 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2176 size = btrfs_item_size_nr(leaf, path.slots[0]);
2177 BUG_ON(size != sizeof(chunk));
2178 read_extent_buffer(leaf, &chunk, ptr, size);
2179 type = btrfs_stack_chunk_type(&chunk);
2181 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2184 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2192 ret = btrfs_commit_transaction(trans, root);
2195 btrfs_release_path(&path);
2199 static int do_convert(const char *devname, int datacsum, int packing,
2208 ext2_filsys ext2_fs;
2209 struct btrfs_root *root;
2210 struct btrfs_root *ext2_root;
2212 ret = open_ext2fs(devname, &ext2_fs);
2214 fprintf(stderr, "unable to open the Ext2fs\n");
2217 blocksize = ext2_fs->blocksize;
2218 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2219 if (blocksize < 4096) {
2220 fprintf(stderr, "block size is too small\n");
2223 if (!(ext2_fs->super->s_feature_incompat &
2224 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2225 fprintf(stderr, "filetype feature is missing\n");
2228 for (i = 0; i < 7; i++) {
2229 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2231 fprintf(stderr, "not enough free space\n");
2234 blocks[i] *= blocksize;
2236 super_bytenr = blocks[0];
2237 fd = open(devname, O_RDWR);
2239 fprintf(stderr, "unable to open %s\n", devname);
2242 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2243 blocks, total_bytes, blocksize, blocksize,
2244 blocksize, blocksize, 0);
2246 fprintf(stderr, "unable to create initial ctree: %s\n",
2250 /* create a system chunk that maps the whole device */
2251 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2253 fprintf(stderr, "unable to update system chunk\n");
2256 root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
2258 fprintf(stderr, "unable to open ctree\n");
2261 ret = cache_free_extents(root, ext2_fs);
2263 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2266 root->fs_info->extent_ops = &extent_ops;
2267 /* recover block allocation bitmap */
2268 for (i = 0; i < 7; i++) {
2269 blocks[i] /= blocksize;
2270 ext2_free_block(ext2_fs, blocks[i]);
2272 ret = init_btrfs(root);
2274 fprintf(stderr, "unable to setup the root tree\n");
2277 printf("creating btrfs metadata.\n");
2278 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2280 fprintf(stderr, "error during copy_inodes %d\n", ret);
2283 printf("creating ext2fs image file.\n");
2284 ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
2286 fprintf(stderr, "unable to create subvol\n");
2289 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2291 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2294 printf("cleaning up system chunk.\n");
2295 ret = cleanup_sys_chunk(root, ext2_root);
2297 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2300 ret = close_ctree(root);
2302 fprintf(stderr, "error during close_ctree %d\n", ret);
2305 close_ext2fs(ext2_fs);
2308 * If this step succeed, we get a mountable btrfs. Otherwise
2309 * the ext2fs is left unchanged.
2311 ret = migrate_super_block(fd, super_bytenr, blocksize);
2313 fprintf(stderr, "unable to migrate super block\n");
2317 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2319 fprintf(stderr, "unable to open ctree\n");
2322 /* move chunk tree into system chunk. */
2323 ret = fixup_chunk_mapping(root);
2325 fprintf(stderr, "error during fixup_chunk_tree\n");
2328 ret = close_ctree(root);
2331 printf("conversion complete.\n");
2336 fprintf(stderr, "conversion aborted.\n");
2340 static int may_rollback(struct btrfs_root *root)
2342 struct btrfs_fs_info *info = root->fs_info;
2343 struct btrfs_multi_bio *multi = NULL;
2351 if (btrfs_super_num_devices(info->super_copy) != 1)
2354 bytenr = BTRFS_SUPER_INFO_OFFSET;
2355 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2358 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2359 &length, &multi, 0, NULL);
2363 num_stripes = multi->num_stripes;
2364 physical = multi->stripes[0].physical;
2367 if (num_stripes != 1 || physical != bytenr)
2371 if (bytenr >= total_bytes)
2379 static int do_rollback(const char *devname, int force)
2384 struct btrfs_root *root;
2385 struct btrfs_root *ext2_root;
2386 struct btrfs_root *chunk_root;
2387 struct btrfs_dir_item *dir;
2388 struct btrfs_inode_item *inode;
2389 struct btrfs_file_extent_item *fi;
2390 struct btrfs_trans_handle *trans;
2391 struct extent_buffer *leaf;
2392 struct btrfs_block_group_cache *cache1;
2393 struct btrfs_block_group_cache *cache2;
2394 struct btrfs_key key;
2395 struct btrfs_path path;
2396 struct extent_io_tree io_tree;
2411 extent_io_tree_init(&io_tree);
2413 fd = open(devname, O_RDWR);
2415 fprintf(stderr, "unable to open %s\n", devname);
2418 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2420 fprintf(stderr, "unable to open ctree\n");
2423 ret = may_rollback(root);
2425 fprintf(stderr, "unable to do rollback\n");
2429 sectorsize = root->sectorsize;
2430 buf = malloc(sectorsize);
2432 fprintf(stderr, "unable to allocate memory\n");
2436 btrfs_init_path(&path);
2438 key.objectid = EXT2_IMAGE_SUBVOL_OBJECTID;
2439 key.type = BTRFS_ROOT_ITEM_KEY;
2440 key.offset = (u64)-1;
2441 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2442 if (!ext2_root || IS_ERR(ext2_root)) {
2443 fprintf(stderr, "unable to open subvol %llu\n",
2449 root_dir = btrfs_root_dirid(&root->root_item);
2450 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2451 root_dir, name, strlen(name), 0);
2452 if (!dir || IS_ERR(dir)) {
2453 fprintf(stderr, "unable to find file %s\n", name);
2456 leaf = path.nodes[0];
2457 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2458 btrfs_release_path(&path);
2460 objectid = key.objectid;
2462 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2464 fprintf(stderr, "unable to find inode item\n");
2467 leaf = path.nodes[0];
2468 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2469 total_bytes = btrfs_inode_size(leaf, inode);
2470 btrfs_release_path(&path);
2472 key.objectid = objectid;
2474 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2475 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2477 fprintf(stderr, "unable to find first file extent\n");
2478 btrfs_release_path(&path);
2482 /* build mapping tree for the relocated blocks */
2483 for (offset = 0; offset < total_bytes; ) {
2484 leaf = path.nodes[0];
2485 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2486 ret = btrfs_next_leaf(root, &path);
2492 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2493 if (key.objectid != objectid || key.offset != offset ||
2494 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2497 fi = btrfs_item_ptr(leaf, path.slots[0],
2498 struct btrfs_file_extent_item);
2499 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2501 if (btrfs_file_extent_compression(leaf, fi) ||
2502 btrfs_file_extent_encryption(leaf, fi) ||
2503 btrfs_file_extent_other_encoding(leaf, fi))
2506 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2507 /* skip holes and direct mapped extents */
2508 if (bytenr == 0 || bytenr == offset)
2511 bytenr += btrfs_file_extent_offset(leaf, fi);
2512 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2514 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2515 cache2 = btrfs_lookup_block_group(root->fs_info,
2516 offset + num_bytes - 1);
2517 if (!cache1 || cache1 != cache2 ||
2518 (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
2519 !intersect_with_sb(offset, num_bytes)))
2522 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2523 EXTENT_LOCKED, GFP_NOFS);
2524 set_state_private(&io_tree, offset, bytenr);
2526 offset += btrfs_file_extent_num_bytes(leaf, fi);
2529 btrfs_release_path(&path);
2531 if (offset < total_bytes) {
2532 fprintf(stderr, "unable to build extent mapping\n");
2536 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2537 first_free &= ~((u64)sectorsize - 1);
2538 /* backup for extent #0 should exist */
2539 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2540 fprintf(stderr, "no backup for the first extent\n");
2543 /* force no allocation from system block group */
2544 root->fs_info->system_allocs = -1;
2545 trans = btrfs_start_transaction(root, 1);
2548 * recow the whole chunk tree, this will remove all chunk tree blocks
2549 * from system block group
2551 chunk_root = root->fs_info->chunk_root;
2552 memset(&key, 0, sizeof(key));
2554 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2558 ret = btrfs_next_leaf(chunk_root, &path);
2562 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2563 btrfs_release_path(&path);
2565 btrfs_release_path(&path);
2570 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2574 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2575 num_bytes += btrfs_block_group_used(&cache1->item);
2577 offset = cache1->key.objectid + cache1->key.offset;
2579 /* only extent #0 left in system block group? */
2580 if (num_bytes > first_free) {
2581 fprintf(stderr, "unable to empty system block group\n");
2584 /* create a system chunk that maps the whole device */
2585 ret = prepare_system_chunk_sb(root->fs_info->super_copy);
2587 fprintf(stderr, "unable to update system chunk\n");
2591 ret = btrfs_commit_transaction(trans, root);
2594 ret = close_ctree(root);
2596 fprintf(stderr, "error during close_ctree %d\n", ret);
2600 /* zero btrfs super block mirrors */
2601 memset(buf, 0, sectorsize);
2602 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2603 bytenr = btrfs_sb_offset(i);
2604 if (bytenr >= total_bytes)
2606 ret = pwrite(fd, buf, sectorsize, bytenr);
2609 sb_bytenr = (u64)-1;
2610 /* copy all relocated blocks back */
2612 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2617 ret = get_state_private(&io_tree, start, &bytenr);
2620 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2623 while (start <= end) {
2624 if (start == BTRFS_SUPER_INFO_OFFSET) {
2628 ret = pread(fd, buf, sectorsize, bytenr);
2630 fprintf(stderr, "error during pread %d\n", ret);
2633 BUG_ON(ret != sectorsize);
2634 ret = pwrite(fd, buf, sectorsize, start);
2636 fprintf(stderr, "error during pwrite %d\n", ret);
2639 BUG_ON(ret != sectorsize);
2641 start += sectorsize;
2642 bytenr += sectorsize;
2648 fprintf(stderr, "error during fsync %d\n", ret);
2652 * finally, overwrite btrfs super block.
2654 ret = pread(fd, buf, sectorsize, sb_bytenr);
2656 fprintf(stderr, "error during pread %d\n", ret);
2659 BUG_ON(ret != sectorsize);
2660 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2662 fprintf(stderr, "error during pwrite %d\n", ret);
2665 BUG_ON(ret != sectorsize);
2668 fprintf(stderr, "error during fsync %d\n", ret);
2674 extent_io_tree_cleanup(&io_tree);
2675 printf("rollback complete.\n");
2682 fprintf(stderr, "rollback aborted.\n");
2686 static void print_usage(void)
2688 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2689 printf("\t-d disable data checksum\n");
2690 printf("\t-i ignore xattrs and ACLs\n");
2691 printf("\t-n disable packing of small files\n");
2692 printf("\t-r roll back to ext2fs\n");
2695 int main(int argc, char *argv[])
2704 int c = getopt(argc, argv, "dinr");
2725 argc = argc - optind;
2731 file = argv[optind];
2732 if (check_mounted(file)) {
2733 fprintf(stderr, "%s is mounted\n", file);
2738 ret = do_rollback(file, 0);
2740 ret = do_convert(file, datacsum, packing, noxattr);