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"
25 #include <sys/ioctl.h>
26 #include <sys/mount.h>
30 #include <sys/types.h>
35 #include <uuid/uuid.h>
41 #include "transaction.h"
44 #include <ext2fs/ext2_fs.h>
45 #include <ext2fs/ext2fs.h>
46 #include <ext2fs/ext2_ext_attr.h>
48 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
49 #define STRIPE_LEN (64 * 1024)
50 #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
53 * Open Ext2fs in readonly mode, read block allocation bitmap and
54 * inode bitmap into memory.
56 static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
61 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
63 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
66 ret = ext2fs_read_inode_bitmap(ext2_fs);
68 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
72 ret = ext2fs_read_block_bitmap(ext2_fs);
74 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
79 * search each block group for a free inode. this set up
80 * uninit block/inode bitmaps appropriately.
83 while (ino <= ext2_fs->super->s_inodes_count) {
85 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
86 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
95 static int close_ext2fs(ext2_filsys fs)
101 static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
105 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
106 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
113 static int ext2_free_block(ext2_filsys fs, u64 block)
115 BUG_ON(block != (blk_t)block);
116 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
120 static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
126 u64 blocksize = ext2_fs->blocksize;
128 block = ext2_fs->super->s_first_data_block;
129 for (; block < ext2_fs->super->s_blocks_count; block++) {
130 if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
132 bytenr = block * blocksize;
133 ret = set_extent_dirty(&root->fs_info->free_space_cache,
134 bytenr, bytenr + blocksize - 1, 0);
138 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
139 bytenr = btrfs_sb_offset(i);
140 bytenr &= ~((u64)STRIPE_LEN - 1);
141 if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
143 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
144 bytenr + STRIPE_LEN - 1, 0);
147 clear_extent_dirty(&root->fs_info->free_space_cache,
148 0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
153 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
154 u64 hint_byte, struct btrfs_key *ins)
158 u64 last = hint_byte;
161 struct btrfs_block_group_cache *cache;
164 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
165 last, &start, &end, EXTENT_DIRTY);
167 if (wrapped++ == 0) {
175 start = max(last, start);
177 if (last - start < num_bytes)
180 last = start + num_bytes;
181 if (test_range_bit(&root->fs_info->pinned_extents,
182 start, last - 1, EXTENT_DIRTY, 0))
185 cache = btrfs_lookup_block_group(root->fs_info, start);
187 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
188 last > cache->key.objectid + cache->key.offset) {
189 last = cache->key.objectid + cache->key.offset;
193 clear_extent_dirty(&root->fs_info->free_space_cache,
194 start, start + num_bytes - 1, 0);
196 ins->objectid = start;
197 ins->offset = num_bytes;
198 ins->type = BTRFS_EXTENT_ITEM_KEY;
202 fprintf(stderr, "not enough free space\n");
206 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
211 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
212 offset = btrfs_sb_offset(i);
213 offset &= ~((u64)STRIPE_LEN - 1);
215 if (bytenr < offset + STRIPE_LEN &&
216 bytenr + num_bytes > offset)
222 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
225 return intersect_with_sb(bytenr, num_bytes);
228 struct btrfs_extent_ops extent_ops = {
229 .alloc_extent = custom_alloc_extent,
230 .free_extent = custom_free_extent,
233 struct dir_iterate_data {
234 struct btrfs_trans_handle *trans;
235 struct btrfs_root *root;
236 struct btrfs_inode_item *inode;
243 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
244 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
245 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
246 [EXT2_FT_DIR] = BTRFS_FT_DIR,
247 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
248 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
249 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
250 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
251 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
254 static int dir_iterate_proc(ext2_ino_t dir, int entry,
255 struct ext2_dir_entry *dirent,
256 int offset, int blocksize,
257 char *buf,void *priv_data)
263 char dotdot[] = "..";
264 struct btrfs_key location;
265 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
268 name_len = dirent->name_len & 0xFF;
270 objectid = dirent->inode + INO_OFFSET;
271 if (!strncmp(dirent->name, dotdot, name_len)) {
273 BUG_ON(idata->parent != 0);
274 idata->parent = objectid;
278 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
281 location.objectid = objectid;
283 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
285 file_type = dirent->name_len >> 8;
286 BUG_ON(file_type > EXT2_FT_SYMLINK);
287 ret = btrfs_insert_dir_item(idata->trans, idata->root,
288 dirent->name, name_len,
289 idata->objectid, &location,
290 filetype_conversion_table[file_type],
294 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
295 dirent->name, name_len,
296 objectid, idata->objectid,
301 inode_size = btrfs_stack_inode_size(idata->inode) +
303 btrfs_set_stack_inode_size(idata->inode, inode_size);
306 idata->errcode = ret;
310 static int create_dir_entries(struct btrfs_trans_handle *trans,
311 struct btrfs_root *root, u64 objectid,
312 struct btrfs_inode_item *btrfs_inode,
313 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
317 struct dir_iterate_data data = {
320 .inode = btrfs_inode,
321 .objectid = objectid,
327 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
328 dir_iterate_proc, &data);
332 if (ret == 0 && data.parent == objectid) {
333 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
334 objectid, objectid, 0);
338 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
342 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
343 u32 num_bytes, char *buffer)
346 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
348 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
349 if (ret != num_bytes)
358 * Record a file extent. Do all the required works, such as inserting
359 * file extent item, inserting extent item and backref item into extent
360 * tree and updating block accounting.
362 static int record_file_extent(struct btrfs_trans_handle *trans,
363 struct btrfs_root *root, u64 objectid,
364 struct btrfs_inode_item *inode,
365 u64 file_pos, u64 disk_bytenr,
366 u64 num_bytes, int checksum)
369 struct btrfs_fs_info *info = root->fs_info;
370 struct btrfs_root *extent_root = info->extent_root;
371 struct extent_buffer *leaf;
372 struct btrfs_file_extent_item *fi;
373 struct btrfs_key ins_key;
374 struct btrfs_path path;
375 struct btrfs_extent_item *ei;
376 u32 blocksize = root->sectorsize;
379 if (disk_bytenr == 0) {
380 ret = btrfs_insert_file_extent(trans, root, objectid,
381 file_pos, disk_bytenr,
382 num_bytes, num_bytes);
386 btrfs_init_path(&path);
393 buffer = malloc(blocksize);
396 for (offset = 0; offset < num_bytes; offset += blocksize) {
397 ret = read_disk_extent(root, disk_bytenr + offset,
401 ret = btrfs_csum_file_block(trans,
402 root->fs_info->csum_root,
403 disk_bytenr + num_bytes,
404 disk_bytenr + offset,
414 ins_key.objectid = objectid;
415 ins_key.offset = file_pos;
416 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
417 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
421 leaf = path.nodes[0];
422 fi = btrfs_item_ptr(leaf, path.slots[0],
423 struct btrfs_file_extent_item);
424 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
425 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
426 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
427 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
428 btrfs_set_file_extent_offset(leaf, fi, 0);
429 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
430 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
431 btrfs_set_file_extent_compression(leaf, fi, 0);
432 btrfs_set_file_extent_encryption(leaf, fi, 0);
433 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
434 btrfs_mark_buffer_dirty(leaf);
436 nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
437 btrfs_set_stack_inode_nbytes(inode, nbytes);
439 btrfs_release_path(root, &path);
441 ins_key.objectid = disk_bytenr;
442 ins_key.offset = num_bytes;
443 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
445 ret = btrfs_insert_empty_item(trans, extent_root, &path,
446 &ins_key, sizeof(*ei));
448 leaf = path.nodes[0];
449 ei = btrfs_item_ptr(leaf, path.slots[0],
450 struct btrfs_extent_item);
452 btrfs_set_extent_refs(leaf, ei, 0);
453 btrfs_set_extent_generation(leaf, ei, 0);
454 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
456 btrfs_mark_buffer_dirty(leaf);
458 ret = btrfs_update_block_group(trans, root, disk_bytenr,
462 } else if (ret != -EEXIST) {
465 btrfs_extent_post_op(trans, extent_root);
467 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
468 root->root_key.objectid,
474 btrfs_release_path(root, &path);
478 static int record_file_blocks(struct btrfs_trans_handle *trans,
479 struct btrfs_root *root, u64 objectid,
480 struct btrfs_inode_item *inode,
481 u64 file_block, u64 disk_block,
482 u64 num_blocks, int checksum)
484 u64 file_pos = file_block * root->sectorsize;
485 u64 disk_bytenr = disk_block * root->sectorsize;
486 u64 num_bytes = num_blocks * root->sectorsize;
487 return record_file_extent(trans, root, objectid, inode, file_pos,
488 disk_bytenr, num_bytes, checksum);
491 struct blk_iterate_data {
492 struct btrfs_trans_handle *trans;
493 struct btrfs_root *root;
494 struct btrfs_inode_item *inode;
504 static int block_iterate_proc(ext2_filsys ext2_fs,
505 u64 disk_block, u64 file_block,
506 struct blk_iterate_data *idata)
511 struct btrfs_root *root = idata->root;
512 struct btrfs_trans_handle *trans = idata->trans;
513 struct btrfs_block_group_cache *cache;
514 u64 bytenr = disk_block * root->sectorsize;
516 sb_region = intersect_with_sb(bytenr, root->sectorsize);
517 do_barrier = sb_region || disk_block >= idata->boundary;
518 if ((idata->num_blocks > 0 && do_barrier) ||
519 (file_block > idata->first_block + idata->num_blocks) ||
520 (disk_block != idata->disk_block + idata->num_blocks)) {
521 if (idata->num_blocks > 0) {
522 ret = record_file_blocks(trans, root, idata->objectid,
523 idata->inode, idata->first_block,
524 idata->disk_block, idata->num_blocks,
528 idata->first_block += idata->num_blocks;
529 idata->num_blocks = 0;
531 if (file_block > idata->first_block) {
532 ret = record_file_blocks(trans, root, idata->objectid,
533 idata->inode, idata->first_block,
534 0, file_block - idata->first_block,
541 bytenr += STRIPE_LEN - 1;
542 bytenr &= ~((u64)STRIPE_LEN - 1);
544 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
546 bytenr = cache->key.objectid + cache->key.offset;
549 idata->first_block = file_block;
550 idata->disk_block = disk_block;
551 idata->boundary = bytenr / root->sectorsize;
556 idata->errcode = ret;
560 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
561 e2_blkcnt_t blockcnt, blk_t ref_block,
562 int ref_offset, void *priv_data)
564 struct blk_iterate_data *idata;
565 idata = (struct blk_iterate_data *)priv_data;
566 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
570 * traverse file's data blocks, record these data blocks as file extents.
572 static int create_file_extents(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root, u64 objectid,
574 struct btrfs_inode_item *btrfs_inode,
575 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
576 int datacsum, int packing)
582 u32 sectorsize = root->sectorsize;
583 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
584 struct blk_iterate_data data = {
587 .inode = btrfs_inode,
588 .objectid = objectid,
593 .checksum = datacsum,
596 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
597 NULL, __block_iterate_proc, &data);
603 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
604 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
605 u64 num_bytes = data.num_blocks * sectorsize;
606 u64 disk_bytenr = data.disk_block * sectorsize;
609 buffer = malloc(num_bytes);
612 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
615 if (num_bytes > inode_size)
616 num_bytes = inode_size;
617 ret = btrfs_insert_inline_extent(trans, root, objectid,
618 0, buffer, num_bytes);
621 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
622 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
623 } else if (data.num_blocks > 0) {
624 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
625 data.first_block, data.disk_block,
626 data.num_blocks, data.checksum);
630 data.first_block += data.num_blocks;
631 last_block = (inode_size + sectorsize - 1) / sectorsize;
632 if (last_block > data.first_block) {
633 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
634 data.first_block, 0, last_block -
635 data.first_block, data.checksum);
642 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
646 static int create_symbol_link(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root, u64 objectid,
648 struct btrfs_inode_item *btrfs_inode,
649 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
650 struct ext2_inode *ext2_inode)
654 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
655 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
656 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
657 ret = create_file_extents(trans, root, objectid, btrfs_inode,
658 ext2_fs, ext2_ino, 1, 1);
659 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
663 pathname = (char *)&(ext2_inode->i_block[0]);
664 BUG_ON(pathname[inode_size] != 0);
665 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
666 pathname, inode_size + 1);
667 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
672 * Following xattr/acl related codes are based on codes in
673 * fs/ext3/xattr.c and fs/ext3/acl.c
675 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
676 #define EXT2_XATTR_BFIRST(ptr) \
677 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
678 #define EXT2_XATTR_IHDR(inode) \
679 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
680 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
681 #define EXT2_XATTR_IFIRST(inode) \
682 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
683 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
685 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
688 struct ext2_ext_attr_entry *next;
690 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
691 next = EXT2_EXT_ATTR_NEXT(entry);
692 if ((void *)next >= end)
699 static int ext2_xattr_check_block(const char *buf, size_t size)
702 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
704 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
705 header->h_blocks != 1)
707 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
711 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
714 size_t value_size = entry->e_value_size;
716 if (entry->e_value_block != 0 || value_size > size ||
717 entry->e_value_offs + value_size > size)
722 #define EXT2_ACL_VERSION 0x0001
733 } ext2_acl_entry_short;
739 static inline int ext2_acl_count(size_t size)
742 size -= sizeof(ext2_acl_header);
743 s = size - 4 * sizeof(ext2_acl_entry_short);
745 if (size % sizeof(ext2_acl_entry_short))
747 return size / sizeof(ext2_acl_entry_short);
749 if (s % sizeof(ext2_acl_entry))
751 return s / sizeof(ext2_acl_entry) + 4;
755 #define ACL_EA_VERSION 0x0002
765 acl_ea_entry a_entries[0];
768 static inline size_t acl_ea_size(int count)
770 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
773 static int ext2_acl_to_xattr(void *dst, const void *src,
774 size_t dst_size, size_t src_size)
777 const void *end = src + src_size;
778 acl_ea_header *ext_acl = (acl_ea_header *)dst;
779 acl_ea_entry *dst_entry = ext_acl->a_entries;
780 ext2_acl_entry *src_entry;
782 if (src_size < sizeof(ext2_acl_header))
784 if (((ext2_acl_header *)src)->a_version !=
785 cpu_to_le32(EXT2_ACL_VERSION))
787 src += sizeof(ext2_acl_header);
788 count = ext2_acl_count(src_size);
792 BUG_ON(dst_size < acl_ea_size(count));
793 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
794 for (i = 0; i < count; i++, dst_entry++) {
795 src_entry = (ext2_acl_entry *)src;
796 if (src + sizeof(ext2_acl_entry_short) > end)
798 dst_entry->e_tag = src_entry->e_tag;
799 dst_entry->e_perm = src_entry->e_perm;
800 switch (le16_to_cpu(src_entry->e_tag)) {
805 src += sizeof(ext2_acl_entry_short);
806 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
810 src += sizeof(ext2_acl_entry);
813 dst_entry->e_id = src_entry->e_id;
826 static char *xattr_prefix_table[] = {
828 [2] = "system.posix_acl_access",
829 [3] = "system.posix_acl_default",
834 static int copy_single_xattr(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root, u64 objectid,
836 struct ext2_ext_attr_entry *entry,
837 const void *data, u32 datalen)
842 void *databuf = NULL;
843 char namebuf[XATTR_NAME_MAX + 1];
845 name_index = entry->e_name_index;
846 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
847 xattr_prefix_table[name_index] == NULL)
849 name_len = strlen(xattr_prefix_table[name_index]) +
851 if (name_len >= sizeof(namebuf))
854 if (name_index == 2 || name_index == 3) {
855 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
856 databuf = malloc(bufsize);
859 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
865 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
866 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
867 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
868 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
869 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
870 objectid - INO_OFFSET, name_len, namebuf);
873 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
874 data, datalen, objectid);
881 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
882 struct btrfs_root *root, u64 objectid,
883 struct btrfs_inode_item *btrfs_inode,
884 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
890 u32 block_size = ext2_fs->blocksize;
891 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
892 struct ext2_inode_large *ext2_inode;
893 struct ext2_ext_attr_entry *entry;
896 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
898 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
899 ext2_inode = (struct ext2_inode_large *)inode_buf;
901 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
905 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
908 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
914 if (ext2_ino > ext2_fs->super->s_first_ino &&
915 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
916 if (EXT2_GOOD_OLD_INODE_SIZE +
917 ext2_inode->i_extra_isize > inode_size) {
921 if (ext2_inode->i_extra_isize != 0 &&
922 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
923 EXT2_EXT_ATTR_MAGIC) {
929 void *end = (void *)ext2_inode + inode_size;
930 entry = EXT2_XATTR_IFIRST(ext2_inode);
931 total = end - (void *)entry;
932 ret = ext2_xattr_check_names(entry, end);
935 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
936 ret = ext2_xattr_check_entry(entry, total);
939 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
941 datalen = entry->e_value_size;
942 ret = copy_single_xattr(trans, root, objectid,
943 entry, data, datalen);
946 entry = EXT2_EXT_ATTR_NEXT(entry);
950 if (ext2_inode->i_file_acl == 0)
953 buffer = malloc(block_size);
958 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
960 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
965 ret = ext2_xattr_check_block(buffer, block_size);
969 entry = EXT2_XATTR_BFIRST(buffer);
970 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
971 ret = ext2_xattr_check_entry(entry, block_size);
974 data = buffer + entry->e_value_offs;
975 datalen = entry->e_value_size;
976 ret = copy_single_xattr(trans, root, objectid,
977 entry, data, datalen);
980 entry = EXT2_EXT_ATTR_NEXT(entry);
985 if ((void *)ext2_inode != inode_buf)
990 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
992 static inline dev_t old_decode_dev(u16 val)
994 return MKDEV((val >> 8) & 255, val & 255);
997 static inline dev_t new_decode_dev(u32 dev)
999 unsigned major = (dev & 0xfff00) >> 8;
1000 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
1001 return MKDEV(major, minor);
1004 static int copy_inode_item(struct btrfs_inode_item *dst,
1005 struct ext2_inode *src, u32 blocksize)
1007 btrfs_set_stack_inode_generation(dst, 1);
1008 btrfs_set_stack_inode_size(dst, src->i_size);
1009 btrfs_set_stack_inode_nbytes(dst, 0);
1010 btrfs_set_stack_inode_block_group(dst, 0);
1011 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
1012 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
1013 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
1014 btrfs_set_stack_inode_mode(dst, src->i_mode);
1015 btrfs_set_stack_inode_rdev(dst, 0);
1016 btrfs_set_stack_inode_flags(dst, 0);
1017 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
1018 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
1019 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
1020 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
1021 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
1022 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
1023 btrfs_set_stack_timespec_sec(&dst->otime, 0);
1024 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
1026 if (S_ISDIR(src->i_mode)) {
1027 btrfs_set_stack_inode_size(dst, 0);
1028 btrfs_set_stack_inode_nlink(dst, 1);
1030 if (S_ISREG(src->i_mode)) {
1031 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1034 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1035 !S_ISLNK(src->i_mode)) {
1036 if (src->i_block[0]) {
1037 btrfs_set_stack_inode_rdev(dst,
1038 old_decode_dev(src->i_block[0]));
1040 btrfs_set_stack_inode_rdev(dst,
1041 new_decode_dev(src->i_block[1]));
1048 * copy a single inode. do all the required works, such as cloning
1049 * inode item, creating file extents and creating directory entries.
1051 static int copy_single_inode(struct btrfs_trans_handle *trans,
1052 struct btrfs_root *root, u64 objectid,
1053 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1054 struct ext2_inode *ext2_inode,
1055 int datacsum, int packing, int noxattr)
1058 struct btrfs_key inode_key;
1059 struct btrfs_inode_item btrfs_inode;
1061 if (ext2_inode->i_links_count == 0)
1064 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1065 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1066 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1067 BTRFS_INODE_NODATASUM;
1068 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1071 switch (ext2_inode->i_mode & S_IFMT) {
1073 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1074 ext2_fs, ext2_ino, datacsum, packing);
1077 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1081 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1082 ext2_fs, ext2_ino, ext2_inode);
1092 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1097 inode_key.objectid = objectid;
1098 inode_key.offset = 0;
1099 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1100 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1104 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1105 u64 src_bytenr, u32 num_bytes)
1109 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1111 buffer = malloc(num_bytes);
1114 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1115 if (ret != num_bytes)
1117 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1118 if (ret != num_bytes)
1128 * scan ext2's inode bitmap and copy all used inodes.
1130 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1131 int datacsum, int packing, int noxattr)
1135 ext2_inode_scan ext2_scan;
1136 struct ext2_inode ext2_inode;
1137 ext2_ino_t ext2_ino;
1139 struct btrfs_trans_handle *trans;
1141 trans = btrfs_start_transaction(root, 1);
1144 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1146 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1149 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1151 /* no more inodes */
1154 /* skip special inode in ext2fs */
1155 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1156 ext2_ino != EXT2_ROOT_INO)
1158 objectid = ext2_ino + INO_OFFSET;
1159 ret = copy_single_inode(trans, root,
1160 objectid, ext2_fs, ext2_ino,
1161 &ext2_inode, datacsum, packing,
1165 if (trans->blocks_used >= 4096) {
1166 ret = btrfs_commit_transaction(trans, root);
1168 trans = btrfs_start_transaction(root, 1);
1173 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1176 ret = btrfs_commit_transaction(trans, root);
1183 * Construct a range of ext2fs image file.
1184 * scan block allocation bitmap, find all blocks used by the ext2fs
1185 * in this range and create file extents that point to these blocks.
1187 * Note: Before calling the function, no file extent points to blocks
1190 static int create_image_file_range(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root, u64 objectid,
1192 struct btrfs_inode_item *inode,
1193 u64 start_byte, u64 end_byte,
1194 ext2_filsys ext2_fs)
1196 u32 blocksize = ext2_fs->blocksize;
1197 u32 block = start_byte / blocksize;
1198 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1200 struct blk_iterate_data data = {
1204 .objectid = objectid,
1205 .first_block = block,
1208 .boundary = (u64)-1,
1212 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1213 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1215 ret = block_iterate_proc(NULL, block, block, &data);
1216 if (ret & BLOCK_ABORT) {
1221 if (data.num_blocks > 0) {
1222 ret = record_file_blocks(trans, root, objectid, inode,
1223 data.first_block, data.disk_block,
1224 data.num_blocks, 0);
1227 data.first_block += data.num_blocks;
1229 if (last_block > data.first_block) {
1230 ret = record_file_blocks(trans, root, objectid, inode,
1231 data.first_block, 0, last_block -
1232 data.first_block, 0);
1240 * Create the ext2fs image file.
1242 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1246 struct btrfs_key key;
1247 struct btrfs_key location;
1248 struct btrfs_path path;
1249 struct btrfs_inode_item btrfs_inode;
1250 struct btrfs_inode_item *inode_item;
1251 struct extent_buffer *leaf;
1252 struct btrfs_fs_info *fs_info = root->fs_info;
1253 struct btrfs_root *extent_root = fs_info->extent_root;
1254 struct btrfs_trans_handle *trans;
1255 struct btrfs_extent_item *ei;
1256 struct btrfs_extent_inline_ref *iref;
1257 struct btrfs_extent_data_ref *dref;
1264 u32 sectorsize = root->sectorsize;
1266 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1267 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1268 first_free &= ~((u64)sectorsize - 1);
1270 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1271 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1272 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1273 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1274 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1275 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1276 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1277 BTRFS_INODE_READONLY);
1278 btrfs_init_path(&path);
1279 trans = btrfs_start_transaction(root, 1);
1282 objectid = btrfs_root_dirid(&root->root_item);
1283 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1288 * copy blocks covered by extent #0 to new positions. extent #0 is
1289 * special, we can't rely on relocate_extents_range to relocate it.
1291 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1292 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1295 ret = copy_disk_extent(root, key.objectid, last_byte,
1299 ret = record_file_extent(trans, root, objectid,
1300 &btrfs_inode, last_byte,
1301 key.objectid, sectorsize, 0);
1307 key.objectid = last_byte;
1309 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1310 ret = btrfs_search_slot(trans, fs_info->extent_root,
1315 leaf = path.nodes[0];
1316 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1317 ret = btrfs_next_leaf(extent_root, &path);
1322 leaf = path.nodes[0];
1324 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1325 if (last_byte > key.objectid ||
1326 key.type != BTRFS_EXTENT_ITEM_KEY) {
1331 bytenr = key.objectid;
1332 num_bytes = key.offset;
1333 ei = btrfs_item_ptr(leaf, path.slots[0],
1334 struct btrfs_extent_item);
1335 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1340 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1341 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1343 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1344 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1345 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1346 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1347 if (btrfs_extent_data_ref_root(leaf, dref) !=
1348 BTRFS_FS_TREE_OBJECTID) {
1353 if (bytenr > last_byte) {
1354 ret = create_image_file_range(trans, root, objectid,
1355 &btrfs_inode, last_byte,
1360 ret = record_file_extent(trans, root, objectid, &btrfs_inode,
1361 bytenr, bytenr, num_bytes, 0);
1364 last_byte = bytenr + num_bytes;
1365 btrfs_release_path(extent_root, &path);
1367 if (trans->blocks_used >= 4096) {
1368 ret = btrfs_commit_transaction(trans, root);
1370 trans = btrfs_start_transaction(root, 1);
1374 btrfs_release_path(root, &path);
1375 if (total_bytes > last_byte) {
1376 ret = create_image_file_range(trans, root, objectid,
1377 &btrfs_inode, last_byte,
1378 total_bytes, ext2_fs);
1383 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1387 location.objectid = objectid;
1388 location.offset = 0;
1389 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1390 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1391 btrfs_root_dirid(&root->root_item),
1392 &location, EXT2_FT_REG_FILE, objectid);
1395 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1397 btrfs_root_dirid(&root->root_item),
1401 location.objectid = btrfs_root_dirid(&root->root_item);
1402 location.offset = 0;
1403 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1404 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1407 leaf = path.nodes[0];
1408 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1409 struct btrfs_inode_item);
1410 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1411 btrfs_inode_size(leaf, inode_item));
1412 btrfs_mark_buffer_dirty(leaf);
1413 btrfs_release_path(root, &path);
1414 ret = btrfs_commit_transaction(trans, root);
1417 btrfs_release_path(root, &path);
1421 struct btrfs_root *link_subvol(struct btrfs_root *root, const char *base,
1424 struct btrfs_trans_handle *trans;
1425 struct btrfs_fs_info *fs_info = root->fs_info;
1426 struct btrfs_root *tree_root = fs_info->tree_root;
1427 struct btrfs_root *new_root = NULL;
1428 struct btrfs_path *path;
1429 struct btrfs_inode_item *inode_item;
1430 struct extent_buffer *leaf;
1431 struct btrfs_key key;
1432 u64 dirid = btrfs_root_dirid(&root->root_item);
1438 path = btrfs_alloc_path();
1441 key.objectid = dirid;
1442 key.type = BTRFS_DIR_INDEX_KEY;
1443 key.offset = (u64)-1;
1445 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1448 if (path->slots[0] > 0) {
1450 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1451 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1452 index = key.offset + 1;
1454 btrfs_release_path(root, path);
1456 trans = btrfs_start_transaction(root, 1);
1459 key.objectid = dirid;
1461 key.type = BTRFS_INODE_ITEM_KEY;
1463 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1465 leaf = path->nodes[0];
1466 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1467 struct btrfs_inode_item);
1469 key.objectid = root_objectid;
1470 key.offset = (u64)-1;
1471 key.type = BTRFS_ROOT_ITEM_KEY;
1474 for (i = 0; i < 1024; i++) {
1475 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf),
1476 dirid, &key, BTRFS_FT_DIR, index);
1479 sprintf(buf, "%s%d", base, i);
1484 btrfs_set_inode_size(leaf, inode_item, strlen(buf) * 2 +
1485 btrfs_inode_size(leaf, inode_item));
1486 btrfs_mark_buffer_dirty(leaf);
1487 btrfs_release_path(root, path);
1489 /* add the backref first */
1490 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1491 BTRFS_ROOT_BACKREF_KEY,
1492 root->root_key.objectid,
1493 dirid, index, buf, strlen(buf));
1496 /* now add the forward ref */
1497 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1498 BTRFS_ROOT_REF_KEY, root_objectid,
1499 dirid, index, buf, strlen(buf));
1501 ret = btrfs_commit_transaction(trans, root);
1504 new_root = btrfs_read_fs_root(fs_info, &key);
1505 if (IS_ERR(new_root))
1508 btrfs_free_path(path);
1512 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1513 struct btrfs_root *root)
1515 struct btrfs_fs_info *info = root->fs_info;
1516 struct btrfs_root *chunk_root = info->chunk_root;
1517 struct btrfs_root *extent_root = info->extent_root;
1518 struct btrfs_device *device;
1519 struct btrfs_block_group_cache *cache;
1520 struct btrfs_dev_extent *extent;
1521 struct extent_buffer *leaf;
1522 struct btrfs_chunk chunk;
1523 struct btrfs_key key;
1524 struct btrfs_path path;
1530 btrfs_init_path(&path);
1532 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1533 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1535 BUG_ON(list_empty(&info->fs_devices->devices));
1536 device = list_entry(info->fs_devices->devices.next,
1537 struct btrfs_device, dev_list);
1538 BUG_ON(device->devid != info->fs_devices->latest_devid);
1540 /* delete device extent created by make_btrfs */
1541 key.objectid = device->devid;
1543 key.type = BTRFS_DEV_EXTENT_KEY;
1544 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1549 ret = btrfs_del_item(trans, device->dev_root, &path);
1552 btrfs_release_path(device->dev_root, &path);
1554 /* delete chunk item created by make_btrfs */
1555 key.objectid = chunk_objectid;
1557 key.type = BTRFS_CHUNK_ITEM_KEY;
1558 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1563 ret = btrfs_del_item(trans, chunk_root, &path);
1566 btrfs_release_path(chunk_root, &path);
1568 /* for each block group, create device extent and chunk item */
1570 while (cur_start < total_bytes) {
1571 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1574 /* insert device extent */
1575 key.objectid = device->devid;
1576 key.offset = cache->key.objectid;
1577 key.type = BTRFS_DEV_EXTENT_KEY;
1578 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1579 &key, sizeof(*extent));
1583 leaf = path.nodes[0];
1584 extent = btrfs_item_ptr(leaf, path.slots[0],
1585 struct btrfs_dev_extent);
1587 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1588 chunk_root->root_key.objectid);
1589 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1591 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1592 cache->key.objectid);
1593 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1594 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1595 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1597 btrfs_mark_buffer_dirty(leaf);
1598 btrfs_release_path(device->dev_root, &path);
1600 /* insert chunk item */
1601 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1602 btrfs_set_stack_chunk_owner(&chunk,
1603 extent_root->root_key.objectid);
1604 btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN);
1605 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1606 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1607 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1608 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1609 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1610 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1611 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1612 btrfs_set_stack_stripe_offset(&chunk.stripe,
1613 cache->key.objectid);
1614 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1616 key.objectid = chunk_objectid;
1617 key.offset = cache->key.objectid;
1618 key.type = BTRFS_CHUNK_ITEM_KEY;
1620 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1621 btrfs_chunk_item_size(1));
1625 cur_start = cache->key.objectid + cache->key.offset;
1628 device->bytes_used = total_bytes;
1629 ret = btrfs_update_device(trans, device);
1631 btrfs_release_path(device->dev_root, &path);
1635 static int create_subvol(struct btrfs_trans_handle *trans,
1636 struct btrfs_root *root, u64 root_objectid)
1638 struct extent_buffer *tmp;
1639 struct btrfs_root *new_root;
1640 struct btrfs_key key;
1641 struct btrfs_root_item root_item;
1644 ret = btrfs_copy_root(trans, root, root->node, &tmp,
1648 memcpy(&root_item, &root->root_item, sizeof(root_item));
1649 btrfs_set_root_bytenr(&root_item, tmp->start);
1650 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1651 btrfs_set_root_generation(&root_item, trans->transid);
1652 free_extent_buffer(tmp);
1654 key.objectid = root_objectid;
1655 key.type = BTRFS_ROOT_ITEM_KEY;
1656 key.offset = trans->transid;
1657 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1660 key.offset = (u64)-1;
1661 new_root = btrfs_read_fs_root(root->fs_info, &key);
1662 BUG_ON(!new_root || IS_ERR(new_root));
1664 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1670 static int init_btrfs(struct btrfs_root *root)
1673 struct btrfs_key location;
1674 struct btrfs_trans_handle *trans;
1675 struct btrfs_fs_info *fs_info = root->fs_info;
1676 struct extent_buffer *tmp;
1678 trans = btrfs_start_transaction(root, 1);
1680 ret = btrfs_make_block_groups(trans, root);
1683 ret = btrfs_fix_block_accounting(trans, root);
1686 ret = create_chunk_mapping(trans, root);
1689 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1690 BTRFS_ROOT_TREE_DIR_OBJECTID);
1693 memcpy(&location, &root->root_key, sizeof(location));
1694 location.offset = (u64)-1;
1695 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1696 btrfs_super_root_dir(fs_info->super_copy),
1697 &location, BTRFS_FT_DIR, 0);
1700 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1702 btrfs_super_root_dir(fs_info->super_copy), 0);
1705 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1706 BTRFS_FIRST_FREE_OBJECTID);
1708 /* subvol for ext2 image file */
1709 ret = create_subvol(trans, root, EXT2_IMAGE_SUBVOL_OBJECTID);
1711 /* subvol for data relocation */
1712 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1715 ret = __btrfs_cow_block(trans, fs_info->csum_root,
1716 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1718 free_extent_buffer(tmp);
1720 ret = btrfs_commit_transaction(trans, root);
1727 * Migrate super block to it's default position and zero 0 ~ 16k
1729 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1732 struct extent_buffer *buf;
1733 struct btrfs_super_block *super;
1737 BUG_ON(sectorsize < sizeof(*super));
1738 buf = malloc(sizeof(*buf) + sectorsize);
1742 buf->len = sectorsize;
1743 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1744 if (ret != sectorsize)
1747 super = (struct btrfs_super_block *)buf->data;
1748 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1749 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1751 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1752 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1753 if (ret != sectorsize)
1760 memset(buf->data, 0, sectorsize);
1761 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1762 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1763 if (len > sectorsize)
1765 ret = pwrite(fd, buf->data, len, bytenr);
1767 fprintf(stderr, "unable to zero fill device\n");
1781 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1783 struct btrfs_chunk *chunk;
1784 struct btrfs_disk_key *key;
1785 u32 sectorsize = btrfs_super_sectorsize(super);
1787 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1788 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1789 sizeof(struct btrfs_disk_key));
1791 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1792 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1793 btrfs_set_disk_key_offset(key, 0);
1795 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1796 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1797 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1798 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1799 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1800 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1801 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1802 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1803 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1804 chunk->stripe.devid = super->dev_item.devid;
1805 chunk->stripe.offset = cpu_to_le64(0);
1806 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1807 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1811 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1814 struct extent_buffer *buf;
1815 struct btrfs_super_block *super;
1817 BUG_ON(sectorsize < sizeof(*super));
1818 buf = malloc(sizeof(*buf) + sectorsize);
1822 buf->len = sectorsize;
1823 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1824 if (ret != sectorsize)
1827 super = (struct btrfs_super_block *)buf->data;
1828 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1829 BUG_ON(btrfs_super_num_devices(super) != 1);
1831 ret = prepare_system_chunk_sb(super);
1835 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1836 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1837 if (ret != sectorsize)
1848 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1849 struct btrfs_root *root,
1850 u64 extent_start, u64 extent_size,
1851 struct btrfs_key *extent_key,
1852 struct extent_io_tree *reloc_tree)
1854 struct extent_buffer *leaf;
1855 struct btrfs_file_extent_item *fi;
1856 struct btrfs_key key;
1857 struct btrfs_path path;
1858 struct btrfs_inode_item inode;
1859 struct blk_iterate_data data;
1866 u32 sectorsize = root->sectorsize;
1872 btrfs_init_path(&path);
1873 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
1877 leaf = path.nodes[0];
1878 fi = btrfs_item_ptr(leaf, path.slots[0],
1879 struct btrfs_file_extent_item);
1880 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
1881 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1882 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1887 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1888 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1890 ret = btrfs_del_item(trans, root, &path);
1894 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
1895 root->root_key.objectid,
1896 extent_key->objectid, extent_key->offset);
1900 btrfs_release_path(root, &path);
1902 key.objectid = extent_key->objectid;
1904 key.type = BTRFS_INODE_ITEM_KEY;
1905 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1909 leaf = path.nodes[0];
1910 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1911 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1912 btrfs_release_path(root, &path);
1914 BUG_ON(num_bytes & (sectorsize - 1));
1915 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
1916 btrfs_set_stack_inode_nbytes(&inode, nbytes);
1917 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
1919 data = (struct blk_iterate_data) {
1923 .objectid = extent_key->objectid,
1924 .first_block = extent_key->offset / sectorsize,
1927 .boundary = (u64)-1,
1928 .checksum = datacsum,
1932 cur_offset = extent_key->offset;
1933 while (num_bytes > 0) {
1934 sector_end = bytenr + sectorsize - 1;
1935 if (test_range_bit(reloc_tree, bytenr, sector_end,
1936 EXTENT_LOCKED, 1)) {
1937 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1940 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1943 new_pos = key.objectid;
1945 if (cur_offset == extent_key->offset) {
1946 fd = root->fs_info->fs_devices->latest_bdev;
1947 readahead(fd, bytenr, num_bytes);
1949 ret = copy_disk_extent(root, new_pos, bytenr,
1953 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1954 EXTENT_LOCKED, GFP_NOFS);
1956 ret = set_state_private(reloc_tree, bytenr, new_pos);
1960 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1961 cur_offset / sectorsize, &data);
1962 if (ret & BLOCK_ABORT) {
1967 cur_offset += sectorsize;
1968 bytenr += sectorsize;
1969 num_bytes -= sectorsize;
1972 if (data.num_blocks > 0) {
1973 ret = record_file_blocks(trans, root,
1974 extent_key->objectid, &inode,
1975 data.first_block, data.disk_block,
1976 data.num_blocks, datacsum);
1981 key.objectid = extent_key->objectid;
1983 key.type = BTRFS_INODE_ITEM_KEY;
1984 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1988 leaf = path.nodes[0];
1989 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1990 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1991 btrfs_mark_buffer_dirty(leaf);
1992 btrfs_release_path(root, &path);
1995 btrfs_release_path(root, &path);
1999 static int relocate_extents_range(struct btrfs_root *fs_root,
2000 struct btrfs_root *ext2_root,
2001 u64 start_byte, u64 end_byte)
2003 struct btrfs_fs_info *info = fs_root->fs_info;
2004 struct btrfs_root *extent_root = info->extent_root;
2005 struct btrfs_root *cur_root = NULL;
2006 struct btrfs_trans_handle *trans;
2007 struct btrfs_extent_data_ref *dref;
2008 struct btrfs_extent_inline_ref *iref;
2009 struct btrfs_extent_item *ei;
2010 struct extent_buffer *leaf;
2011 struct btrfs_key key;
2012 struct btrfs_key extent_key;
2013 struct btrfs_path path;
2014 struct extent_io_tree reloc_tree;
2024 btrfs_init_path(&path);
2025 extent_io_tree_init(&reloc_tree);
2027 key.objectid = start_byte;
2029 key.type = BTRFS_EXTENT_ITEM_KEY;
2030 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2034 ret = btrfs_previous_item(extent_root, &path, 0,
2035 BTRFS_EXTENT_ITEM_KEY);
2039 leaf = path.nodes[0];
2040 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2041 if (key.objectid + key.offset > start_byte)
2042 start_byte = key.objectid;
2045 btrfs_release_path(extent_root, &path);
2047 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
2050 trans = btrfs_start_transaction(cur_root, 1);
2053 cur_byte = start_byte;
2055 key.objectid = cur_byte;
2057 key.type = BTRFS_EXTENT_ITEM_KEY;
2058 ret = btrfs_search_slot(trans, extent_root,
2063 leaf = path.nodes[0];
2064 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2065 ret = btrfs_next_leaf(extent_root, &path);
2070 leaf = path.nodes[0];
2073 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2074 if (key.objectid < cur_byte ||
2075 key.type != BTRFS_EXTENT_ITEM_KEY) {
2079 if (key.objectid >= end_byte)
2084 cur_byte = key.objectid;
2085 num_bytes = key.offset;
2086 ei = btrfs_item_ptr(leaf, path.slots[0],
2087 struct btrfs_extent_item);
2088 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2089 BTRFS_EXTENT_FLAG_DATA));
2091 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2092 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2094 ptr += sizeof(struct btrfs_extent_item);
2097 iref = (struct btrfs_extent_inline_ref *)ptr;
2098 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2099 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2100 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2101 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2102 extent_key.objectid =
2103 btrfs_extent_data_ref_objectid(leaf, dref);
2105 btrfs_extent_data_ref_offset(leaf, dref);
2106 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2107 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2109 if (ref_root == cur_root->root_key.objectid)
2112 ptr += btrfs_extent_inline_ref_size(key.type);
2120 ret = relocate_one_reference(trans, cur_root, cur_byte,
2121 num_bytes, &extent_key,
2126 cur_byte += num_bytes;
2127 btrfs_release_path(extent_root, &path);
2129 if (trans->blocks_used >= 4096) {
2130 ret = btrfs_commit_transaction(trans, cur_root);
2132 trans = btrfs_start_transaction(cur_root, 1);
2136 btrfs_release_path(cur_root, &path);
2138 ret = btrfs_commit_transaction(trans, cur_root);
2141 if (num_extents > 0 && pass++ < 16)
2144 ret = (num_extents > 0) ? -1 : 0;
2146 btrfs_release_path(cur_root, &path);
2147 extent_io_tree_cleanup(&reloc_tree);
2152 * relocate data in system chunk
2154 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2155 struct btrfs_root *ext2_root)
2157 struct btrfs_block_group_cache *cache;
2163 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2167 end_byte = cache->key.objectid + cache->key.offset;
2168 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2169 ret = relocate_extents_range(fs_root, ext2_root,
2170 cache->key.objectid,
2177 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2178 offset = btrfs_sb_offset(i);
2179 offset &= ~((u64)STRIPE_LEN - 1);
2181 ret = relocate_extents_range(fs_root, ext2_root,
2182 offset, offset + STRIPE_LEN);
2191 static int fixup_chunk_mapping(struct btrfs_root *root)
2193 struct btrfs_trans_handle *trans;
2194 struct btrfs_fs_info *info = root->fs_info;
2195 struct btrfs_root *chunk_root = info->chunk_root;
2196 struct extent_buffer *leaf;
2197 struct btrfs_key key;
2198 struct btrfs_path path;
2199 struct btrfs_chunk chunk;
2205 btrfs_init_path(&path);
2207 trans = btrfs_start_transaction(root, 1);
2211 * recow the whole chunk tree. this will move all chunk tree blocks
2212 * into system block group.
2214 memset(&key, 0, sizeof(key));
2216 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2220 ret = btrfs_next_leaf(chunk_root, &path);
2226 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2227 btrfs_release_path(chunk_root, &path);
2229 btrfs_release_path(chunk_root, &path);
2231 /* fixup the system chunk array in super block */
2232 btrfs_set_super_sys_array_size(info->super_copy, 0);
2234 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2236 key.type = BTRFS_CHUNK_ITEM_KEY;
2238 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2243 leaf = path.nodes[0];
2244 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2245 ret = btrfs_next_leaf(chunk_root, &path);
2250 leaf = path.nodes[0];
2252 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2253 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2256 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2257 size = btrfs_item_size_nr(leaf, path.slots[0]);
2258 BUG_ON(size != sizeof(chunk));
2259 read_extent_buffer(leaf, &chunk, ptr, size);
2260 type = btrfs_stack_chunk_type(&chunk);
2262 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2265 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2273 ret = btrfs_commit_transaction(trans, root);
2276 btrfs_release_path(chunk_root, &path);
2280 int do_convert(const char *devname, int datacsum, int packing, int noxattr)
2288 ext2_filsys ext2_fs;
2289 struct btrfs_root *root;
2290 struct btrfs_root *ext2_root;
2292 ret = open_ext2fs(devname, &ext2_fs);
2294 fprintf(stderr, "unable to open the Ext2fs\n");
2297 blocksize = ext2_fs->blocksize;
2298 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2299 if (blocksize < 4096) {
2300 fprintf(stderr, "block size is too small\n");
2303 if (!(ext2_fs->super->s_feature_incompat &
2304 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2305 fprintf(stderr, "filetype feature is missing\n");
2308 for (i = 0; i < 7; i++) {
2309 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2311 fprintf(stderr, "not enough free space\n");
2314 blocks[i] *= blocksize;
2316 super_bytenr = blocks[0];
2317 fd = open(devname, O_RDWR);
2319 fprintf(stderr, "unable to open %s\n", devname);
2322 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2323 blocks, total_bytes, blocksize, blocksize,
2324 blocksize, blocksize);
2326 fprintf(stderr, "unable to create initial ctree\n");
2329 /* create a system chunk that maps the whole device */
2330 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2332 fprintf(stderr, "unable to update system chunk\n");
2335 root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
2337 fprintf(stderr, "unable to open ctree\n");
2340 ret = cache_free_extents(root, ext2_fs);
2342 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2345 root->fs_info->extent_ops = &extent_ops;
2346 /* recover block allocation bitmap */
2347 for (i = 0; i < 7; i++) {
2348 blocks[i] /= blocksize;
2349 ext2_free_block(ext2_fs, blocks[i]);
2351 ret = init_btrfs(root);
2353 fprintf(stderr, "unable to setup the root tree\n");
2356 printf("creating btrfs metadata.\n");
2357 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2359 fprintf(stderr, "error during copy_inodes %d\n", ret);
2362 printf("creating ext2fs image file.\n");
2363 ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
2365 fprintf(stderr, "unable to create subvol\n");
2368 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2370 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2373 printf("cleaning up system chunk.\n");
2374 ret = cleanup_sys_chunk(root, ext2_root);
2376 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2379 ret = close_ctree(root);
2381 fprintf(stderr, "error during close_ctree %d\n", ret);
2384 close_ext2fs(ext2_fs);
2387 * If this step succeed, we get a mountable btrfs. Otherwise
2388 * the ext2fs is left unchanged.
2390 ret = migrate_super_block(fd, super_bytenr, blocksize);
2392 fprintf(stderr, "unable to migrate super block\n");
2396 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2398 fprintf(stderr, "unable to open ctree\n");
2401 /* move chunk tree into system chunk. */
2402 ret = fixup_chunk_mapping(root);
2404 fprintf(stderr, "error during fixup_chunk_tree\n");
2407 ret = close_ctree(root);
2410 printf("conversion complete.\n");
2415 fprintf(stderr, "conversion aborted.\n");
2419 static int may_rollback(struct btrfs_root *root)
2421 struct btrfs_fs_info *info = root->fs_info;
2422 struct btrfs_multi_bio *multi = NULL;
2430 if (btrfs_super_num_devices(info->super_copy) != 1)
2433 bytenr = BTRFS_SUPER_INFO_OFFSET;
2434 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2437 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2438 &length, &multi, 0, NULL);
2442 num_stripes = multi->num_stripes;
2443 physical = multi->stripes[0].physical;
2446 if (num_stripes != 1 || physical != bytenr)
2450 if (bytenr >= total_bytes)
2458 int do_rollback(const char *devname, int force)
2463 struct btrfs_root *root;
2464 struct btrfs_root *ext2_root;
2465 struct btrfs_root *chunk_root;
2466 struct btrfs_dir_item *dir;
2467 struct btrfs_inode_item *inode;
2468 struct btrfs_file_extent_item *fi;
2469 struct btrfs_trans_handle *trans;
2470 struct extent_buffer *leaf;
2471 struct btrfs_block_group_cache *cache1;
2472 struct btrfs_block_group_cache *cache2;
2473 struct btrfs_key key;
2474 struct btrfs_path path;
2475 struct extent_io_tree io_tree;
2490 extent_io_tree_init(&io_tree);
2492 fd = open(devname, O_RDWR);
2494 fprintf(stderr, "unable to open %s\n", devname);
2497 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2499 fprintf(stderr, "unable to open ctree\n");
2502 ret = may_rollback(root);
2504 fprintf(stderr, "unable to do rollback\n");
2508 sectorsize = root->sectorsize;
2509 buf = malloc(sectorsize);
2511 fprintf(stderr, "unable to allocate memory\n");
2515 btrfs_init_path(&path);
2517 key.objectid = EXT2_IMAGE_SUBVOL_OBJECTID;
2518 key.type = BTRFS_ROOT_ITEM_KEY;
2519 key.offset = (u64)-1;
2520 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2521 if (!ext2_root || IS_ERR(ext2_root)) {
2522 fprintf(stderr, "unable to open subvol %llu\n",
2528 root_dir = btrfs_root_dirid(&root->root_item);
2529 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2530 root_dir, name, strlen(name), 0);
2531 if (!dir || IS_ERR(dir)) {
2532 fprintf(stderr, "unable to find file %s\n", name);
2535 leaf = path.nodes[0];
2536 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2537 btrfs_release_path(ext2_root, &path);
2539 objectid = key.objectid;
2541 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2543 fprintf(stderr, "unable to find inode item\n");
2546 leaf = path.nodes[0];
2547 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2548 total_bytes = btrfs_inode_size(leaf, inode);
2549 btrfs_release_path(ext2_root, &path);
2551 key.objectid = objectid;
2553 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2554 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2556 fprintf(stderr, "unable to find first file extent\n");
2557 btrfs_release_path(ext2_root, &path);
2561 /* build mapping tree for the relocated blocks */
2562 for (offset = 0; offset < total_bytes; ) {
2563 leaf = path.nodes[0];
2564 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2565 ret = btrfs_next_leaf(root, &path);
2571 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2572 if (key.objectid != objectid || key.offset != offset ||
2573 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2576 fi = btrfs_item_ptr(leaf, path.slots[0],
2577 struct btrfs_file_extent_item);
2578 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2580 if (btrfs_file_extent_compression(leaf, fi) ||
2581 btrfs_file_extent_encryption(leaf, fi) ||
2582 btrfs_file_extent_other_encoding(leaf, fi))
2585 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2586 /* skip holes and direct mapped extents */
2587 if (bytenr == 0 || bytenr == offset)
2590 bytenr += btrfs_file_extent_offset(leaf, fi);
2591 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2593 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2594 cache2 = btrfs_lookup_block_group(root->fs_info,
2595 offset + num_bytes - 1);
2596 if (!cache1 || cache1 != cache2 ||
2597 (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
2598 !intersect_with_sb(offset, num_bytes)))
2601 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2602 EXTENT_LOCKED, GFP_NOFS);
2603 set_state_private(&io_tree, offset, bytenr);
2605 offset += btrfs_file_extent_num_bytes(leaf, fi);
2608 btrfs_release_path(ext2_root, &path);
2610 if (offset < total_bytes) {
2611 fprintf(stderr, "unable to build extent mapping\n");
2615 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2616 first_free &= ~((u64)sectorsize - 1);
2617 /* backup for extent #0 should exist */
2618 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2619 fprintf(stderr, "no backup for the first extent\n");
2622 /* force no allocation from system block group */
2623 root->fs_info->system_allocs = -1;
2624 trans = btrfs_start_transaction(root, 1);
2627 * recow the whole chunk tree, this will remove all chunk tree blocks
2628 * from system block group
2630 chunk_root = root->fs_info->chunk_root;
2631 memset(&key, 0, sizeof(key));
2633 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2637 ret = btrfs_next_leaf(chunk_root, &path);
2641 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2642 btrfs_release_path(chunk_root, &path);
2644 btrfs_release_path(chunk_root, &path);
2649 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2653 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2654 num_bytes += btrfs_block_group_used(&cache1->item);
2656 offset = cache1->key.objectid + cache1->key.offset;
2658 /* only extent #0 left in system block group? */
2659 if (num_bytes > first_free) {
2660 fprintf(stderr, "unable to empty system block group\n");
2663 /* create a system chunk that maps the whole device */
2664 ret = prepare_system_chunk_sb(root->fs_info->super_copy);
2666 fprintf(stderr, "unable to update system chunk\n");
2670 ret = btrfs_commit_transaction(trans, root);
2673 ret = close_ctree(root);
2675 fprintf(stderr, "error during close_ctree %d\n", ret);
2679 /* zero btrfs super block mirrors */
2680 memset(buf, 0, sectorsize);
2681 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2682 bytenr = btrfs_sb_offset(i);
2683 if (bytenr >= total_bytes)
2685 ret = pwrite(fd, buf, sectorsize, bytenr);
2688 sb_bytenr = (u64)-1;
2689 /* copy all relocated blocks back */
2691 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2696 ret = get_state_private(&io_tree, start, &bytenr);
2699 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2702 while (start <= end) {
2703 if (start == BTRFS_SUPER_INFO_OFFSET) {
2707 ret = pread(fd, buf, sectorsize, bytenr);
2709 fprintf(stderr, "error during pread %d\n", ret);
2712 BUG_ON(ret != sectorsize);
2713 ret = pwrite(fd, buf, sectorsize, start);
2715 fprintf(stderr, "error during pwrite %d\n", ret);
2718 BUG_ON(ret != sectorsize);
2720 start += sectorsize;
2721 bytenr += sectorsize;
2727 fprintf(stderr, "error during fsync %d\n", ret);
2731 * finally, overwrite btrfs super block.
2733 ret = pread(fd, buf, sectorsize, sb_bytenr);
2735 fprintf(stderr, "error during pread %d\n", ret);
2738 BUG_ON(ret != sectorsize);
2739 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2741 fprintf(stderr, "error during pwrite %d\n", ret);
2744 BUG_ON(ret != sectorsize);
2747 fprintf(stderr, "error during fsync %d\n", ret);
2753 extent_io_tree_cleanup(&io_tree);
2754 printf("rollback complete.\n");
2761 fprintf(stderr, "rollback aborted.\n");
2765 static void print_usage(void)
2767 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2768 printf("\t-d disable data checksum\n");
2769 printf("\t-i ignore xattrs and ACLs\n");
2770 printf("\t-n disable packing of small files\n");
2771 printf("\t-r roll back to ext2fs\n");
2774 int main(int argc, char *argv[])
2783 int c = getopt(argc, argv, "dinr");
2804 argc = argc - optind;
2810 file = argv[optind];
2811 if (check_mounted(file)) {
2812 fprintf(stderr, "%s is mounted\n", file);
2817 ret = do_rollback(file, 0);
2819 ret = do_convert(file, datacsum, packing, noxattr);