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 <sys/ioctl.h>
23 #include <sys/mount.h>
27 #include <sys/types.h>
32 #include <uuid/uuid.h>
34 #include "kerncompat.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 STRIPE_LEN (64 * 1024)
47 #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
50 * Open Ext2fs in readonly mode, read block allocation bitmap and
51 * inode bitmap into memory.
53 static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
58 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
60 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
63 ret = ext2fs_read_inode_bitmap(ext2_fs);
65 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
69 ret = ext2fs_read_block_bitmap(ext2_fs);
71 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
76 * search each block group for a free inode. this set up
77 * uninit block/inode bitmaps appropriately.
80 while (ino <= ext2_fs->super->s_inodes_count) {
82 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
83 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
92 static int close_ext2fs(ext2_filsys fs)
98 static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
102 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
103 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
110 static int ext2_free_block(ext2_filsys fs, u64 block)
112 BUG_ON(block != (blk_t)block);
113 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
117 static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
123 u64 blocksize = ext2_fs->blocksize;
125 block = ext2_fs->super->s_first_data_block;
126 for (; block < ext2_fs->super->s_blocks_count; block++) {
127 if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
129 bytenr = block * blocksize;
130 ret = set_extent_dirty(&root->fs_info->free_space_cache,
131 bytenr, bytenr + blocksize - 1, 0);
135 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
136 bytenr = btrfs_sb_offset(i);
137 bytenr &= ~((u64)STRIPE_LEN - 1);
138 if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
140 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
141 bytenr + STRIPE_LEN - 1, 0);
144 clear_extent_dirty(&root->fs_info->free_space_cache,
145 0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
150 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
151 u64 hint_byte, struct btrfs_key *ins)
155 u64 last = hint_byte;
158 struct btrfs_block_group_cache *cache;
161 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
162 last, &start, &end, EXTENT_DIRTY);
164 if (wrapped++ == 0) {
172 start = max(last, start);
174 if (last - start < num_bytes)
177 last = start + num_bytes;
178 if (test_range_bit(&root->fs_info->pinned_extents,
179 start, last - 1, EXTENT_DIRTY, 0))
182 cache = btrfs_lookup_block_group(root->fs_info, start);
184 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
185 last > cache->key.objectid + cache->key.offset) {
186 last = cache->key.objectid + cache->key.offset;
190 clear_extent_dirty(&root->fs_info->free_space_cache,
191 start, start + num_bytes - 1, 0);
193 ins->objectid = start;
194 ins->offset = num_bytes;
195 ins->type = BTRFS_EXTENT_ITEM_KEY;
199 fprintf(stderr, "not enough free space\n");
203 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
208 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
209 offset = btrfs_sb_offset(i);
210 offset &= ~((u64)STRIPE_LEN - 1);
212 if (bytenr < offset + STRIPE_LEN &&
213 bytenr + num_bytes > offset)
219 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
222 return intersect_with_sb(bytenr, num_bytes);
225 struct btrfs_extent_ops extent_ops = {
226 .alloc_extent = custom_alloc_extent,
227 .free_extent = custom_free_extent,
230 struct dir_iterate_data {
231 struct btrfs_trans_handle *trans;
232 struct btrfs_root *root;
233 struct btrfs_inode_item *inode;
240 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
241 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
242 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
243 [EXT2_FT_DIR] = BTRFS_FT_DIR,
244 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
245 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
246 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
247 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
248 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
251 static int dir_iterate_proc(ext2_ino_t dir, int entry,
252 struct ext2_dir_entry *old,
253 int offset, int blocksize,
254 char *buf,void *priv_data)
260 char dotdot[] = "..";
261 struct btrfs_key location;
262 struct ext2_dir_entry_2 *dirent = (struct ext2_dir_entry_2 *)old;
263 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
265 objectid = dirent->inode + INO_OFFSET;
266 if (!strncmp(dirent->name, dotdot, dirent->name_len)) {
267 if (dirent->name_len == 2) {
268 BUG_ON(idata->parent != 0);
269 idata->parent = objectid;
273 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
276 location.objectid = objectid;
278 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
280 file_type = dirent->file_type;
281 BUG_ON(file_type > EXT2_FT_SYMLINK);
282 ret = btrfs_insert_dir_item(idata->trans, idata->root,
283 dirent->name, dirent->name_len,
284 idata->objectid, &location,
285 filetype_conversion_table[file_type],
289 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
290 dirent->name, dirent->name_len,
291 objectid, idata->objectid,
296 inode_size = btrfs_stack_inode_size(idata->inode) +
297 dirent->name_len * 2;
298 btrfs_set_stack_inode_size(idata->inode, inode_size);
301 idata->errcode = ret;
305 static int create_dir_entries(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root, u64 objectid,
307 struct btrfs_inode_item *btrfs_inode,
308 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
312 struct dir_iterate_data data = {
315 .inode = btrfs_inode,
316 .objectid = objectid,
322 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
323 dir_iterate_proc, &data);
327 if (ret == 0 && data.parent == objectid) {
328 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
329 objectid, objectid, 0);
333 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
337 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
338 u32 num_bytes, char *buffer)
341 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
343 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
344 if (ret != num_bytes)
353 * Record a file extent. Do all the required works, such as inserting
354 * file extent item, inserting extent item and backref item into extent
355 * tree and updating block accounting.
357 static int record_file_extent(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root, u64 objectid,
359 struct btrfs_inode_item *inode,
360 u64 file_pos, u64 disk_bytenr,
361 u64 num_bytes, int checksum)
364 struct btrfs_fs_info *info = root->fs_info;
365 struct btrfs_root *extent_root = info->extent_root;
366 struct extent_buffer *leaf;
367 struct btrfs_file_extent_item *fi;
368 struct btrfs_key ins_key;
369 struct btrfs_path path;
370 struct btrfs_extent_item *ei;
371 u32 blocksize = root->sectorsize;
374 if (disk_bytenr == 0) {
375 ret = btrfs_insert_file_extent(trans, root, objectid,
376 file_pos, disk_bytenr,
377 num_bytes, num_bytes);
381 btrfs_init_path(&path);
388 buffer = malloc(blocksize);
391 for (offset = 0; offset < num_bytes; offset += blocksize) {
392 ret = read_disk_extent(root, disk_bytenr + offset,
396 ret = btrfs_csum_file_block(trans,
397 root->fs_info->csum_root,
398 disk_bytenr + num_bytes,
399 disk_bytenr + offset,
409 ins_key.objectid = objectid;
410 ins_key.offset = file_pos;
411 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
412 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
416 leaf = path.nodes[0];
417 fi = btrfs_item_ptr(leaf, path.slots[0],
418 struct btrfs_file_extent_item);
419 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
420 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
421 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
422 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
423 btrfs_set_file_extent_offset(leaf, fi, 0);
424 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
425 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
426 btrfs_set_file_extent_compression(leaf, fi, 0);
427 btrfs_set_file_extent_encryption(leaf, fi, 0);
428 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
429 btrfs_mark_buffer_dirty(leaf);
431 nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
432 btrfs_set_stack_inode_nbytes(inode, nbytes);
434 btrfs_release_path(root, &path);
436 ins_key.objectid = disk_bytenr;
437 ins_key.offset = num_bytes;
438 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
440 ret = btrfs_insert_empty_item(trans, extent_root, &path,
441 &ins_key, sizeof(*ei));
443 leaf = path.nodes[0];
444 ei = btrfs_item_ptr(leaf, path.slots[0],
445 struct btrfs_extent_item);
447 btrfs_set_extent_refs(leaf, ei, 0);
448 btrfs_set_extent_generation(leaf, ei, 0);
449 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
451 btrfs_mark_buffer_dirty(leaf);
453 ret = btrfs_update_block_group(trans, root, disk_bytenr,
457 } else if (ret != -EEXIST) {
460 btrfs_extent_post_op(trans, extent_root);
462 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
463 root->root_key.objectid,
469 btrfs_release_path(root, &path);
473 static int record_file_blocks(struct btrfs_trans_handle *trans,
474 struct btrfs_root *root, u64 objectid,
475 struct btrfs_inode_item *inode,
476 u64 file_block, u64 disk_block,
477 u64 num_blocks, int checksum)
479 u64 file_pos = file_block * root->sectorsize;
480 u64 disk_bytenr = disk_block * root->sectorsize;
481 u64 num_bytes = num_blocks * root->sectorsize;
482 return record_file_extent(trans, root, objectid, inode, file_pos,
483 disk_bytenr, num_bytes, checksum);
486 struct blk_iterate_data {
487 struct btrfs_trans_handle *trans;
488 struct btrfs_root *root;
489 struct btrfs_inode_item *inode;
499 static int block_iterate_proc(ext2_filsys ext2_fs,
500 u64 disk_block, u64 file_block,
501 struct blk_iterate_data *idata)
506 struct btrfs_root *root = idata->root;
507 struct btrfs_trans_handle *trans = idata->trans;
508 struct btrfs_block_group_cache *cache;
509 u64 bytenr = disk_block * root->sectorsize;
511 sb_region = intersect_with_sb(bytenr, root->sectorsize);
512 do_barrier = sb_region || disk_block >= idata->boundary;
513 if ((idata->num_blocks > 0 && do_barrier) ||
514 (file_block > idata->first_block + idata->num_blocks) ||
515 (disk_block != idata->disk_block + idata->num_blocks)) {
516 if (idata->num_blocks > 0) {
517 ret = record_file_blocks(trans, root, idata->objectid,
518 idata->inode, idata->first_block,
519 idata->disk_block, idata->num_blocks,
523 idata->first_block += idata->num_blocks;
524 idata->num_blocks = 0;
526 if (file_block > idata->first_block) {
527 ret = record_file_blocks(trans, root, idata->objectid,
528 idata->inode, idata->first_block,
529 0, file_block - idata->first_block,
536 bytenr += STRIPE_LEN - 1;
537 bytenr &= ~((u64)STRIPE_LEN - 1);
539 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
541 bytenr = cache->key.objectid + cache->key.offset;
544 idata->first_block = file_block;
545 idata->disk_block = disk_block;
546 idata->boundary = bytenr / root->sectorsize;
551 idata->errcode = ret;
555 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
556 e2_blkcnt_t blockcnt, blk_t ref_block,
557 int ref_offset, void *priv_data)
559 struct blk_iterate_data *idata;
560 idata = (struct blk_iterate_data *)priv_data;
561 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
565 * traverse file's data blocks, record these data blocks as file extents.
567 static int create_file_extents(struct btrfs_trans_handle *trans,
568 struct btrfs_root *root, u64 objectid,
569 struct btrfs_inode_item *btrfs_inode,
570 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
571 int datacsum, int packing)
577 u32 sectorsize = root->sectorsize;
578 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
579 struct blk_iterate_data data = {
582 .inode = btrfs_inode,
583 .objectid = objectid,
588 .checksum = datacsum,
591 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
592 NULL, __block_iterate_proc, &data);
598 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
599 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
600 u64 num_bytes = data.num_blocks * sectorsize;
601 u64 disk_bytenr = data.disk_block * sectorsize;
604 buffer = malloc(num_bytes);
607 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
610 if (num_bytes > inode_size)
611 num_bytes = inode_size;
612 ret = btrfs_insert_inline_extent(trans, root, objectid,
613 0, buffer, num_bytes);
616 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
617 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
618 } else if (data.num_blocks > 0) {
619 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
620 data.first_block, data.disk_block,
621 data.num_blocks, data.checksum);
625 data.first_block += data.num_blocks;
626 last_block = (inode_size + sectorsize - 1) / sectorsize;
627 if (last_block > data.first_block) {
628 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
629 data.first_block, 0, last_block -
630 data.first_block, data.checksum);
637 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
641 static int create_symbol_link(struct btrfs_trans_handle *trans,
642 struct btrfs_root *root, u64 objectid,
643 struct btrfs_inode_item *btrfs_inode,
644 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
645 struct ext2_inode *ext2_inode)
649 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
650 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
651 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
652 ret = create_file_extents(trans, root, objectid, btrfs_inode,
653 ext2_fs, ext2_ino, 1, 1);
654 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
658 pathname = (char *)&(ext2_inode->i_block[0]);
659 BUG_ON(pathname[inode_size] != 0);
660 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
661 pathname, inode_size + 1);
662 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
667 * Following xattr/acl related codes are based on codes in
668 * fs/ext3/xattr.c and fs/ext3/acl.c
670 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
671 #define EXT2_XATTR_BFIRST(ptr) \
672 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
673 #define EXT2_XATTR_IHDR(inode) \
674 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
675 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
676 #define EXT2_XATTR_IFIRST(inode) \
677 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
678 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
680 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
683 struct ext2_ext_attr_entry *next;
685 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
686 next = EXT2_EXT_ATTR_NEXT(entry);
687 if ((void *)next >= end)
694 static int ext2_xattr_check_block(const char *buf, size_t size)
697 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
699 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
700 header->h_blocks != 1)
702 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
706 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
709 size_t value_size = entry->e_value_size;
711 if (entry->e_value_block != 0 || value_size > size ||
712 entry->e_value_offs + value_size > size)
717 #define EXT2_ACL_VERSION 0x0001
728 } ext2_acl_entry_short;
734 static inline int ext2_acl_count(size_t size)
737 size -= sizeof(ext2_acl_header);
738 s = size - 4 * sizeof(ext2_acl_entry_short);
740 if (size % sizeof(ext2_acl_entry_short))
742 return size / sizeof(ext2_acl_entry_short);
744 if (s % sizeof(ext2_acl_entry))
746 return s / sizeof(ext2_acl_entry) + 4;
750 #define ACL_EA_VERSION 0x0002
760 acl_ea_entry a_entries[0];
763 static inline size_t acl_ea_size(int count)
765 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
768 static int ext2_acl_to_xattr(void *dst, const void *src,
769 size_t dst_size, size_t src_size)
772 const void *end = src + src_size;
773 acl_ea_header *ext_acl = (acl_ea_header *)dst;
774 acl_ea_entry *dst_entry = ext_acl->a_entries;
775 ext2_acl_entry *src_entry;
777 if (src_size < sizeof(ext2_acl_header))
779 if (((ext2_acl_header *)src)->a_version !=
780 cpu_to_le32(EXT2_ACL_VERSION))
782 src += sizeof(ext2_acl_header);
783 count = ext2_acl_count(src_size);
787 BUG_ON(dst_size < acl_ea_size(count));
788 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
789 for (i = 0; i < count; i++, dst_entry++) {
790 src_entry = (ext2_acl_entry *)src;
791 if (src + sizeof(ext2_acl_entry_short) > end)
793 dst_entry->e_tag = src_entry->e_tag;
794 dst_entry->e_perm = src_entry->e_perm;
795 switch (le16_to_cpu(src_entry->e_tag)) {
800 src += sizeof(ext2_acl_entry_short);
801 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
805 src += sizeof(ext2_acl_entry);
808 dst_entry->e_id = src_entry->e_id;
821 static char *xattr_prefix_table[] = {
823 [2] = "system.posix_acl_access",
824 [3] = "system.posix_acl_default",
829 static int copy_single_xattr(struct btrfs_trans_handle *trans,
830 struct btrfs_root *root, u64 objectid,
831 struct ext2_ext_attr_entry *entry,
832 const void *data, u32 datalen)
837 void *databuf = NULL;
838 char namebuf[XATTR_NAME_MAX + 1];
840 name_index = entry->e_name_index;
841 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
842 xattr_prefix_table[name_index] == NULL)
844 name_len = strlen(xattr_prefix_table[name_index]) +
846 if (name_len >= sizeof(namebuf))
849 if (name_index == 2 || name_index == 3) {
850 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
851 databuf = malloc(bufsize);
854 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
860 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
861 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
862 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
863 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
864 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
865 objectid - INO_OFFSET, name_len, namebuf);
868 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
869 data, datalen, objectid);
876 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
877 struct btrfs_root *root, u64 objectid,
878 struct btrfs_inode_item *btrfs_inode,
879 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
885 u32 block_size = ext2_fs->blocksize;
886 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
887 struct ext2_inode_large *ext2_inode;
888 struct ext2_ext_attr_entry *entry;
891 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
893 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
894 ext2_inode = (struct ext2_inode_large *)inode_buf;
896 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
900 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
903 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
909 if (ext2_ino > ext2_fs->super->s_first_ino &&
910 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
911 if (EXT2_GOOD_OLD_INODE_SIZE +
912 ext2_inode->i_extra_isize > inode_size) {
916 if (ext2_inode->i_extra_isize != 0 &&
917 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
918 EXT2_EXT_ATTR_MAGIC) {
924 void *end = (void *)ext2_inode + inode_size;
925 entry = EXT2_XATTR_IFIRST(ext2_inode);
926 total = end - (void *)entry;
927 ret = ext2_xattr_check_names(entry, end);
930 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
931 ret = ext2_xattr_check_entry(entry, total);
934 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
936 datalen = entry->e_value_size;
937 ret = copy_single_xattr(trans, root, objectid,
938 entry, data, datalen);
941 entry = EXT2_EXT_ATTR_NEXT(entry);
945 if (ext2_inode->i_file_acl == 0)
948 buffer = malloc(block_size);
953 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
955 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
960 ret = ext2_xattr_check_block(buffer, block_size);
964 entry = EXT2_XATTR_BFIRST(buffer);
965 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
966 ret = ext2_xattr_check_entry(entry, block_size);
969 data = buffer + entry->e_value_offs;
970 datalen = entry->e_value_size;
971 ret = copy_single_xattr(trans, root, objectid,
972 entry, data, datalen);
975 entry = EXT2_EXT_ATTR_NEXT(entry);
980 if ((void *)ext2_inode != inode_buf)
985 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
987 static inline dev_t old_decode_dev(u16 val)
989 return MKDEV((val >> 8) & 255, val & 255);
992 static inline dev_t new_decode_dev(u32 dev)
994 unsigned major = (dev & 0xfff00) >> 8;
995 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
996 return MKDEV(major, minor);
999 static int copy_inode_item(struct btrfs_inode_item *dst,
1000 struct ext2_inode *src, u32 blocksize)
1002 btrfs_set_stack_inode_generation(dst, 1);
1003 btrfs_set_stack_inode_size(dst, src->i_size);
1004 btrfs_set_stack_inode_nbytes(dst, 0);
1005 btrfs_set_stack_inode_block_group(dst, 0);
1006 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
1007 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
1008 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
1009 btrfs_set_stack_inode_mode(dst, src->i_mode);
1010 btrfs_set_stack_inode_rdev(dst, 0);
1011 btrfs_set_stack_inode_flags(dst, 0);
1012 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
1013 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
1014 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
1015 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
1016 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
1017 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
1018 btrfs_set_stack_timespec_sec(&dst->otime, 0);
1019 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
1021 if (S_ISDIR(src->i_mode)) {
1022 btrfs_set_stack_inode_size(dst, 0);
1023 btrfs_set_stack_inode_nlink(dst, 1);
1025 if (S_ISREG(src->i_mode)) {
1026 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1029 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1030 !S_ISLNK(src->i_mode)) {
1031 if (src->i_block[0]) {
1032 btrfs_set_stack_inode_rdev(dst,
1033 old_decode_dev(src->i_block[0]));
1035 btrfs_set_stack_inode_rdev(dst,
1036 new_decode_dev(src->i_block[1]));
1043 * copy a single inode. do all the required works, such as cloning
1044 * inode item, creating file extents and creating directory entries.
1046 static int copy_single_inode(struct btrfs_trans_handle *trans,
1047 struct btrfs_root *root, u64 objectid,
1048 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1049 struct ext2_inode *ext2_inode,
1050 int datacsum, int packing, int noxattr)
1053 struct btrfs_key inode_key;
1054 struct btrfs_inode_item btrfs_inode;
1056 if (ext2_inode->i_links_count == 0)
1059 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1060 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1061 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1062 BTRFS_INODE_NODATASUM;
1063 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1066 switch (ext2_inode->i_mode & S_IFMT) {
1068 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1069 ext2_fs, ext2_ino, datacsum, packing);
1072 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1076 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1077 ext2_fs, ext2_ino, ext2_inode);
1087 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1092 inode_key.objectid = objectid;
1093 inode_key.offset = 0;
1094 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1095 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1099 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1100 u64 src_bytenr, u32 num_bytes)
1104 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1106 buffer = malloc(num_bytes);
1109 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1110 if (ret != num_bytes)
1112 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1113 if (ret != num_bytes)
1123 * scan ext2's inode bitmap and copy all used inodes.
1125 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1126 int datacsum, int packing, int noxattr)
1130 ext2_inode_scan ext2_scan;
1131 struct ext2_inode ext2_inode;
1132 ext2_ino_t ext2_ino;
1134 struct btrfs_trans_handle *trans;
1136 trans = btrfs_start_transaction(root, 1);
1139 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1141 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1144 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1146 /* no more inodes */
1149 /* skip special inode in ext2fs */
1150 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1151 ext2_ino != EXT2_ROOT_INO)
1153 objectid = ext2_ino + INO_OFFSET;
1154 ret = copy_single_inode(trans, root,
1155 objectid, ext2_fs, ext2_ino,
1156 &ext2_inode, datacsum, packing,
1160 if (trans->blocks_used >= 4096) {
1161 ret = btrfs_commit_transaction(trans, root);
1163 trans = btrfs_start_transaction(root, 1);
1168 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1171 ret = btrfs_commit_transaction(trans, root);
1178 * Construct a range of ext2fs image file.
1179 * scan block allocation bitmap, find all blocks used by the ext2fs
1180 * in this range and create file extents that point to these blocks.
1182 * Note: Before calling the function, no file extent points to blocks
1185 static int create_image_file_range(struct btrfs_trans_handle *trans,
1186 struct btrfs_root *root, u64 objectid,
1187 struct btrfs_inode_item *inode,
1188 u64 start_byte, u64 end_byte,
1189 ext2_filsys ext2_fs)
1191 u32 blocksize = ext2_fs->blocksize;
1192 u32 block = start_byte / blocksize;
1193 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1195 struct blk_iterate_data data = {
1199 .objectid = objectid,
1200 .first_block = block,
1203 .boundary = (u64)-1,
1207 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1208 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1210 ret = block_iterate_proc(NULL, block, block, &data);
1211 if (ret & BLOCK_ABORT) {
1216 if (data.num_blocks > 0) {
1217 ret = record_file_blocks(trans, root, objectid, inode,
1218 data.first_block, data.disk_block,
1219 data.num_blocks, 0);
1222 data.first_block += data.num_blocks;
1224 if (last_block > data.first_block) {
1225 ret = record_file_blocks(trans, root, objectid, inode,
1226 data.first_block, 0, last_block -
1227 data.first_block, 0);
1235 * Create the ext2fs image file.
1237 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1241 struct btrfs_key key;
1242 struct btrfs_key location;
1243 struct btrfs_path path;
1244 struct btrfs_inode_item btrfs_inode;
1245 struct btrfs_inode_item *inode_item;
1246 struct extent_buffer *leaf;
1247 struct btrfs_fs_info *fs_info = root->fs_info;
1248 struct btrfs_root *extent_root = fs_info->extent_root;
1249 struct btrfs_trans_handle *trans;
1250 struct btrfs_extent_item *ei;
1251 struct btrfs_extent_inline_ref *iref;
1252 struct btrfs_extent_data_ref *dref;
1259 u32 sectorsize = root->sectorsize;
1261 total_bytes = btrfs_super_total_bytes(&fs_info->super_copy);
1262 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1263 first_free &= ~((u64)sectorsize - 1);
1265 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1266 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1267 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1268 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1269 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1270 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1271 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1272 BTRFS_INODE_READONLY);
1273 btrfs_init_path(&path);
1274 trans = btrfs_start_transaction(root, 1);
1277 objectid = btrfs_root_dirid(&root->root_item);
1278 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1283 * copy blocks covered by extent #0 to new positions. extent #0 is
1284 * special, we can't rely on relocate_extents_range to relocate it.
1286 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1287 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1290 ret = copy_disk_extent(root, key.objectid, last_byte,
1294 ret = record_file_extent(trans, root, objectid,
1295 &btrfs_inode, last_byte,
1296 key.objectid, sectorsize, 0);
1302 key.objectid = last_byte;
1304 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1305 ret = btrfs_search_slot(trans, fs_info->extent_root,
1310 leaf = path.nodes[0];
1311 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1312 ret = btrfs_next_leaf(extent_root, &path);
1317 leaf = path.nodes[0];
1319 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1320 if (last_byte > key.objectid ||
1321 key.type != BTRFS_EXTENT_ITEM_KEY) {
1326 bytenr = key.objectid;
1327 num_bytes = key.offset;
1328 ei = btrfs_item_ptr(leaf, path.slots[0],
1329 struct btrfs_extent_item);
1330 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1335 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1336 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1338 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1339 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1340 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1341 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1342 if (btrfs_extent_data_ref_root(leaf, dref) !=
1343 BTRFS_FS_TREE_OBJECTID) {
1348 if (bytenr > last_byte) {
1349 ret = create_image_file_range(trans, root, objectid,
1350 &btrfs_inode, last_byte,
1355 ret = record_file_extent(trans, root, objectid, &btrfs_inode,
1356 bytenr, bytenr, num_bytes, 0);
1359 last_byte = bytenr + num_bytes;
1360 btrfs_release_path(extent_root, &path);
1362 if (trans->blocks_used >= 4096) {
1363 ret = btrfs_commit_transaction(trans, root);
1365 trans = btrfs_start_transaction(root, 1);
1369 btrfs_release_path(root, &path);
1370 if (total_bytes > last_byte) {
1371 ret = create_image_file_range(trans, root, objectid,
1372 &btrfs_inode, last_byte,
1373 total_bytes, ext2_fs);
1378 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1382 location.objectid = objectid;
1383 location.offset = 0;
1384 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1385 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1386 btrfs_root_dirid(&root->root_item),
1387 &location, EXT2_FT_REG_FILE, objectid);
1390 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1392 btrfs_root_dirid(&root->root_item),
1396 location.objectid = btrfs_root_dirid(&root->root_item);
1397 location.offset = 0;
1398 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1399 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1402 leaf = path.nodes[0];
1403 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1404 struct btrfs_inode_item);
1405 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1406 btrfs_inode_size(leaf, inode_item));
1407 btrfs_mark_buffer_dirty(leaf);
1408 btrfs_release_path(root, &path);
1409 ret = btrfs_commit_transaction(trans, root);
1412 btrfs_release_path(root, &path);
1416 struct btrfs_root *link_subvol(struct btrfs_root *root, const char *base,
1419 struct btrfs_trans_handle *trans;
1420 struct btrfs_fs_info *fs_info = root->fs_info;
1421 struct btrfs_root *tree_root = fs_info->tree_root;
1422 struct btrfs_root *new_root = NULL;
1423 struct btrfs_path *path;
1424 struct btrfs_inode_item *inode_item;
1425 struct extent_buffer *leaf;
1426 struct btrfs_key key;
1427 u64 dirid = btrfs_root_dirid(&root->root_item);
1433 path = btrfs_alloc_path();
1436 key.objectid = dirid;
1437 key.type = BTRFS_DIR_INDEX_KEY;
1438 key.offset = (u64)-1;
1440 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1443 if (path->slots[0] > 0) {
1445 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1446 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1447 index = key.offset + 1;
1449 btrfs_release_path(root, path);
1451 trans = btrfs_start_transaction(root, 1);
1454 key.objectid = dirid;
1456 key.type = BTRFS_INODE_ITEM_KEY;
1458 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1460 leaf = path->nodes[0];
1461 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1462 struct btrfs_inode_item);
1464 key.objectid = root_objectid;
1465 key.offset = (u64)-1;
1466 key.type = BTRFS_ROOT_ITEM_KEY;
1469 for (i = 0; i < 1024; i++) {
1470 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf),
1471 dirid, &key, BTRFS_FT_DIR, index);
1474 sprintf(buf, "%s%d", base, i);
1479 btrfs_set_inode_size(leaf, inode_item, strlen(buf) * 2 +
1480 btrfs_inode_size(leaf, inode_item));
1481 btrfs_mark_buffer_dirty(leaf);
1482 btrfs_release_path(root, path);
1484 /* add the backref first */
1485 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1486 BTRFS_ROOT_BACKREF_KEY,
1487 root->root_key.objectid,
1488 dirid, index, buf, strlen(buf));
1491 /* now add the forward ref */
1492 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1493 BTRFS_ROOT_REF_KEY, root_objectid,
1494 dirid, index, buf, strlen(buf));
1496 ret = btrfs_commit_transaction(trans, root);
1499 new_root = btrfs_read_fs_root(fs_info, &key);
1500 if (IS_ERR(new_root))
1503 btrfs_free_path(path);
1507 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1508 struct btrfs_root *root)
1510 struct btrfs_fs_info *info = root->fs_info;
1511 struct btrfs_root *chunk_root = info->chunk_root;
1512 struct btrfs_root *extent_root = info->extent_root;
1513 struct btrfs_device *device;
1514 struct btrfs_block_group_cache *cache;
1515 struct btrfs_dev_extent *extent;
1516 struct extent_buffer *leaf;
1517 struct btrfs_chunk chunk;
1518 struct btrfs_key key;
1519 struct btrfs_path path;
1525 btrfs_init_path(&path);
1527 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1528 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1530 BUG_ON(list_empty(&info->fs_devices->devices));
1531 device = list_entry(info->fs_devices->devices.next,
1532 struct btrfs_device, dev_list);
1533 BUG_ON(device->devid != info->fs_devices->latest_devid);
1535 /* delete device extent created by make_btrfs */
1536 key.objectid = device->devid;
1538 key.type = BTRFS_DEV_EXTENT_KEY;
1539 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1544 ret = btrfs_del_item(trans, device->dev_root, &path);
1547 btrfs_release_path(device->dev_root, &path);
1549 /* delete chunk item created by make_btrfs */
1550 key.objectid = chunk_objectid;
1552 key.type = BTRFS_CHUNK_ITEM_KEY;
1553 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1558 ret = btrfs_del_item(trans, chunk_root, &path);
1561 btrfs_release_path(chunk_root, &path);
1563 /* for each block group, create device extent and chunk item */
1565 while (cur_start < total_bytes) {
1566 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1569 /* insert device extent */
1570 key.objectid = device->devid;
1571 key.offset = cache->key.objectid;
1572 key.type = BTRFS_DEV_EXTENT_KEY;
1573 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1574 &key, sizeof(*extent));
1578 leaf = path.nodes[0];
1579 extent = btrfs_item_ptr(leaf, path.slots[0],
1580 struct btrfs_dev_extent);
1582 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1583 chunk_root->root_key.objectid);
1584 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1586 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1587 cache->key.objectid);
1588 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1589 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1590 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1592 btrfs_mark_buffer_dirty(leaf);
1593 btrfs_release_path(device->dev_root, &path);
1595 /* insert chunk item */
1596 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1597 btrfs_set_stack_chunk_owner(&chunk,
1598 extent_root->root_key.objectid);
1599 btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN);
1600 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1601 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1602 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1603 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1604 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1605 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1606 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1607 btrfs_set_stack_stripe_offset(&chunk.stripe,
1608 cache->key.objectid);
1609 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1611 key.objectid = chunk_objectid;
1612 key.offset = cache->key.objectid;
1613 key.type = BTRFS_CHUNK_ITEM_KEY;
1615 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1616 btrfs_chunk_item_size(1));
1620 cur_start = cache->key.objectid + cache->key.offset;
1623 device->bytes_used = total_bytes;
1624 ret = btrfs_update_device(trans, device);
1626 btrfs_release_path(device->dev_root, &path);
1630 static int create_subvol(struct btrfs_trans_handle *trans,
1631 struct btrfs_root *root, u64 root_objectid)
1633 struct extent_buffer *tmp;
1634 struct btrfs_root *new_root;
1635 struct btrfs_key key;
1636 struct btrfs_root_item root_item;
1639 ret = btrfs_copy_root(trans, root, root->node, &tmp,
1643 memcpy(&root_item, &root->root_item, sizeof(root_item));
1644 btrfs_set_root_bytenr(&root_item, tmp->start);
1645 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1646 btrfs_set_root_generation(&root_item, trans->transid);
1647 free_extent_buffer(tmp);
1649 key.objectid = root_objectid;
1650 key.type = BTRFS_ROOT_ITEM_KEY;
1651 key.offset = trans->transid;
1652 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1655 key.offset = (u64)-1;
1656 new_root = btrfs_read_fs_root(root->fs_info, &key);
1657 BUG_ON(!new_root || IS_ERR(new_root));
1659 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1665 static int init_btrfs(struct btrfs_root *root)
1668 struct btrfs_key location;
1669 struct btrfs_trans_handle *trans;
1670 struct btrfs_fs_info *fs_info = root->fs_info;
1671 struct extent_buffer *tmp;
1673 trans = btrfs_start_transaction(root, 1);
1675 ret = btrfs_make_block_groups(trans, root);
1678 ret = btrfs_fix_block_accounting(trans, root);
1681 ret = create_chunk_mapping(trans, root);
1684 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1685 BTRFS_ROOT_TREE_DIR_OBJECTID);
1688 memcpy(&location, &root->root_key, sizeof(location));
1689 location.offset = (u64)-1;
1690 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1691 btrfs_super_root_dir(&fs_info->super_copy),
1692 &location, BTRFS_FT_DIR, 0);
1695 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1697 btrfs_super_root_dir(&fs_info->super_copy), 0);
1700 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1701 BTRFS_FIRST_FREE_OBJECTID);
1703 /* subvol for ext2 image file */
1704 ret = create_subvol(trans, root, EXT2_IMAGE_SUBVOL_OBJECTID);
1706 /* subvol for data relocation */
1707 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1710 ret = __btrfs_cow_block(trans, fs_info->csum_root,
1711 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1713 free_extent_buffer(tmp);
1715 ret = btrfs_commit_transaction(trans, root);
1722 * Migrate super block to it's default position and zero 0 ~ 16k
1724 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1727 struct extent_buffer *buf;
1728 struct btrfs_super_block *super;
1732 BUG_ON(sectorsize < sizeof(*super));
1733 buf = malloc(sizeof(*buf) + sectorsize);
1737 buf->len = sectorsize;
1738 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1739 if (ret != sectorsize)
1742 super = (struct btrfs_super_block *)buf->data;
1743 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1744 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1746 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1747 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1748 if (ret != sectorsize)
1755 memset(buf->data, 0, sectorsize);
1756 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1757 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1758 if (len > sectorsize)
1760 ret = pwrite(fd, buf->data, len, bytenr);
1762 fprintf(stderr, "unable to zero fill device\n");
1776 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1778 struct btrfs_chunk *chunk;
1779 struct btrfs_disk_key *key;
1780 u32 sectorsize = btrfs_super_sectorsize(super);
1782 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1783 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1784 sizeof(struct btrfs_disk_key));
1786 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1787 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1788 btrfs_set_disk_key_offset(key, 0);
1790 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1791 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1792 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1793 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1794 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1795 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1796 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1797 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1798 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1799 chunk->stripe.devid = super->dev_item.devid;
1800 chunk->stripe.offset = cpu_to_le64(0);
1801 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1802 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1806 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1809 struct extent_buffer *buf;
1810 struct btrfs_super_block *super;
1812 BUG_ON(sectorsize < sizeof(*super));
1813 buf = malloc(sizeof(*buf) + sectorsize);
1817 buf->len = sectorsize;
1818 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1819 if (ret != sectorsize)
1822 super = (struct btrfs_super_block *)buf->data;
1823 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1824 BUG_ON(btrfs_super_num_devices(super) != 1);
1826 ret = prepare_system_chunk_sb(super);
1830 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1831 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1832 if (ret != sectorsize)
1843 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1844 struct btrfs_root *root,
1845 u64 extent_start, u64 extent_size,
1846 struct btrfs_key *extent_key,
1847 struct extent_io_tree *reloc_tree)
1849 struct extent_buffer *leaf;
1850 struct btrfs_file_extent_item *fi;
1851 struct btrfs_key key;
1852 struct btrfs_path path;
1853 struct btrfs_inode_item inode;
1854 struct blk_iterate_data data;
1861 u32 sectorsize = root->sectorsize;
1867 btrfs_init_path(&path);
1868 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
1872 leaf = path.nodes[0];
1873 fi = btrfs_item_ptr(leaf, path.slots[0],
1874 struct btrfs_file_extent_item);
1875 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
1876 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1877 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1882 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1883 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1885 ret = btrfs_del_item(trans, root, &path);
1889 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
1890 root->root_key.objectid,
1891 extent_key->objectid, extent_key->offset);
1895 btrfs_release_path(root, &path);
1897 key.objectid = extent_key->objectid;
1899 key.type = BTRFS_INODE_ITEM_KEY;
1900 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1904 leaf = path.nodes[0];
1905 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1906 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1907 btrfs_release_path(root, &path);
1909 BUG_ON(num_bytes & (sectorsize - 1));
1910 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
1911 btrfs_set_stack_inode_nbytes(&inode, nbytes);
1912 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
1914 data = (struct blk_iterate_data) {
1918 .objectid = extent_key->objectid,
1919 .first_block = extent_key->offset / sectorsize,
1922 .boundary = (u64)-1,
1923 .checksum = datacsum,
1927 cur_offset = extent_key->offset;
1928 while (num_bytes > 0) {
1929 sector_end = bytenr + sectorsize - 1;
1930 if (test_range_bit(reloc_tree, bytenr, sector_end,
1931 EXTENT_LOCKED, 1)) {
1932 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1935 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1938 new_pos = key.objectid;
1940 if (cur_offset == extent_key->offset) {
1941 fd = root->fs_info->fs_devices->latest_bdev;
1942 readahead(fd, bytenr, num_bytes);
1944 ret = copy_disk_extent(root, new_pos, bytenr,
1948 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1949 EXTENT_LOCKED, GFP_NOFS);
1951 ret = set_state_private(reloc_tree, bytenr, new_pos);
1955 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1956 cur_offset / sectorsize, &data);
1957 if (ret & BLOCK_ABORT) {
1962 cur_offset += sectorsize;
1963 bytenr += sectorsize;
1964 num_bytes -= sectorsize;
1967 if (data.num_blocks > 0) {
1968 ret = record_file_blocks(trans, root,
1969 extent_key->objectid, &inode,
1970 data.first_block, data.disk_block,
1971 data.num_blocks, datacsum);
1976 key.objectid = extent_key->objectid;
1978 key.type = BTRFS_INODE_ITEM_KEY;
1979 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1983 leaf = path.nodes[0];
1984 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1985 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1986 btrfs_mark_buffer_dirty(leaf);
1987 btrfs_release_path(root, &path);
1990 btrfs_release_path(root, &path);
1994 static int relocate_extents_range(struct btrfs_root *fs_root,
1995 struct btrfs_root *ext2_root,
1996 u64 start_byte, u64 end_byte)
1998 struct btrfs_fs_info *info = fs_root->fs_info;
1999 struct btrfs_root *extent_root = info->extent_root;
2000 struct btrfs_root *cur_root = NULL;
2001 struct btrfs_trans_handle *trans;
2002 struct btrfs_extent_data_ref *dref;
2003 struct btrfs_extent_inline_ref *iref;
2004 struct btrfs_extent_item *ei;
2005 struct extent_buffer *leaf;
2006 struct btrfs_key key;
2007 struct btrfs_key extent_key;
2008 struct btrfs_path path;
2009 struct extent_io_tree reloc_tree;
2019 btrfs_init_path(&path);
2020 extent_io_tree_init(&reloc_tree);
2022 key.objectid = start_byte;
2024 key.type = BTRFS_EXTENT_ITEM_KEY;
2025 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2029 ret = btrfs_previous_item(extent_root, &path, 0,
2030 BTRFS_EXTENT_ITEM_KEY);
2034 leaf = path.nodes[0];
2035 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2036 if (key.objectid + key.offset > start_byte)
2037 start_byte = key.objectid;
2040 btrfs_release_path(extent_root, &path);
2042 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
2045 trans = btrfs_start_transaction(cur_root, 1);
2048 cur_byte = start_byte;
2050 key.objectid = cur_byte;
2052 key.type = BTRFS_EXTENT_ITEM_KEY;
2053 ret = btrfs_search_slot(trans, extent_root,
2058 leaf = path.nodes[0];
2059 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2060 ret = btrfs_next_leaf(extent_root, &path);
2065 leaf = path.nodes[0];
2068 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2069 if (key.objectid < cur_byte ||
2070 key.type != BTRFS_EXTENT_ITEM_KEY) {
2074 if (key.objectid >= end_byte)
2079 cur_byte = key.objectid;
2080 num_bytes = key.offset;
2081 ei = btrfs_item_ptr(leaf, path.slots[0],
2082 struct btrfs_extent_item);
2083 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2084 BTRFS_EXTENT_FLAG_DATA));
2086 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2087 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2089 ptr += sizeof(struct btrfs_extent_item);
2092 iref = (struct btrfs_extent_inline_ref *)ptr;
2093 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2094 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2095 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2096 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2097 extent_key.objectid =
2098 btrfs_extent_data_ref_objectid(leaf, dref);
2100 btrfs_extent_data_ref_offset(leaf, dref);
2101 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2102 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2104 if (ref_root == cur_root->root_key.objectid)
2107 ptr += btrfs_extent_inline_ref_size(key.type);
2115 ret = relocate_one_reference(trans, cur_root, cur_byte,
2116 num_bytes, &extent_key,
2121 cur_byte += num_bytes;
2122 btrfs_release_path(extent_root, &path);
2124 if (trans->blocks_used >= 4096) {
2125 ret = btrfs_commit_transaction(trans, cur_root);
2127 trans = btrfs_start_transaction(cur_root, 1);
2131 btrfs_release_path(cur_root, &path);
2133 ret = btrfs_commit_transaction(trans, cur_root);
2136 if (num_extents > 0 && pass++ < 16)
2139 ret = (num_extents > 0) ? -1 : 0;
2141 btrfs_release_path(cur_root, &path);
2142 extent_io_tree_cleanup(&reloc_tree);
2147 * relocate data in system chunk
2149 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2150 struct btrfs_root *ext2_root)
2152 struct btrfs_block_group_cache *cache;
2158 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2162 end_byte = cache->key.objectid + cache->key.offset;
2163 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2164 ret = relocate_extents_range(fs_root, ext2_root,
2165 cache->key.objectid,
2172 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2173 offset = btrfs_sb_offset(i);
2174 offset &= ~((u64)STRIPE_LEN - 1);
2176 ret = relocate_extents_range(fs_root, ext2_root,
2177 offset, offset + STRIPE_LEN);
2186 static int fixup_chunk_mapping(struct btrfs_root *root)
2188 struct btrfs_trans_handle *trans;
2189 struct btrfs_fs_info *info = root->fs_info;
2190 struct btrfs_root *chunk_root = info->chunk_root;
2191 struct extent_buffer *leaf;
2192 struct btrfs_key key;
2193 struct btrfs_path path;
2194 struct btrfs_chunk chunk;
2200 btrfs_init_path(&path);
2202 trans = btrfs_start_transaction(root, 1);
2206 * recow the whole chunk tree. this will move all chunk tree blocks
2207 * into system block group.
2209 memset(&key, 0, sizeof(key));
2211 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2215 ret = btrfs_next_leaf(chunk_root, &path);
2221 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2222 btrfs_release_path(chunk_root, &path);
2224 btrfs_release_path(chunk_root, &path);
2226 /* fixup the system chunk array in super block */
2227 btrfs_set_super_sys_array_size(&info->super_copy, 0);
2229 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2231 key.type = BTRFS_CHUNK_ITEM_KEY;
2233 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2238 leaf = path.nodes[0];
2239 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2240 ret = btrfs_next_leaf(chunk_root, &path);
2245 leaf = path.nodes[0];
2247 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2248 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2251 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2252 size = btrfs_item_size_nr(leaf, path.slots[0]);
2253 BUG_ON(size != sizeof(chunk));
2254 read_extent_buffer(leaf, &chunk, ptr, size);
2255 type = btrfs_stack_chunk_type(&chunk);
2257 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2260 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2268 ret = btrfs_commit_transaction(trans, root);
2271 btrfs_release_path(chunk_root, &path);
2275 int do_convert(const char *devname, int datacsum, int packing, int noxattr)
2282 ext2_filsys ext2_fs;
2283 struct btrfs_root *root;
2284 struct btrfs_root *ext2_root;
2286 ret = open_ext2fs(devname, &ext2_fs);
2288 fprintf(stderr, "unable to open the Ext2fs\n");
2291 blocksize = ext2_fs->blocksize;
2292 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2293 if (blocksize < 4096) {
2294 fprintf(stderr, "block size is too small\n");
2297 if (!(ext2_fs->super->s_feature_incompat &
2298 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2299 fprintf(stderr, "filetype feature is missing\n");
2302 for (i = 0; i < 7; i++) {
2303 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2305 fprintf(stderr, "not enough free space\n");
2308 blocks[i] *= blocksize;
2310 super_bytenr = blocks[0];
2311 fd = open(devname, O_RDWR);
2313 fprintf(stderr, "unable to open %s\n", devname);
2316 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2317 blocks, total_bytes, blocksize, blocksize,
2318 blocksize, blocksize);
2320 fprintf(stderr, "unable to create initial ctree\n");
2323 /* create a system chunk that maps the whole device */
2324 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2326 fprintf(stderr, "unable to update system chunk\n");
2329 root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
2331 fprintf(stderr, "unable to open ctree\n");
2334 ret = cache_free_extents(root, ext2_fs);
2336 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2339 root->fs_info->extent_ops = &extent_ops;
2340 /* recover block allocation bitmap */
2341 for (i = 0; i < 7; i++) {
2342 blocks[i] /= blocksize;
2343 ext2_free_block(ext2_fs, blocks[i]);
2345 ret = init_btrfs(root);
2347 fprintf(stderr, "unable to setup the root tree\n");
2350 printf("creating btrfs metadata.\n");
2351 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2353 fprintf(stderr, "error during copy_inodes %d\n", ret);
2356 printf("creating ext2fs image file.\n");
2357 ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
2359 fprintf(stderr, "unable to create subvol\n");
2362 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2364 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2367 printf("cleaning up system chunk.\n");
2368 ret = cleanup_sys_chunk(root, ext2_root);
2370 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2373 ret = close_ctree(root);
2375 fprintf(stderr, "error during close_ctree %d\n", ret);
2378 close_ext2fs(ext2_fs);
2381 * If this step succeed, we get a mountable btrfs. Otherwise
2382 * the ext2fs is left unchanged.
2384 ret = migrate_super_block(fd, super_bytenr, blocksize);
2386 fprintf(stderr, "unable to migrate super block\n");
2390 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2392 fprintf(stderr, "unable to open ctree\n");
2395 /* move chunk tree into system chunk. */
2396 ret = fixup_chunk_mapping(root);
2398 fprintf(stderr, "error during fixup_chunk_tree\n");
2401 ret = close_ctree(root);
2404 printf("conversion complete.\n");
2407 fprintf(stderr, "conversion aborted.\n");
2411 static int may_rollback(struct btrfs_root *root)
2413 struct btrfs_fs_info *info = root->fs_info;
2414 struct btrfs_multi_bio *multi = NULL;
2422 if (btrfs_super_num_devices(&info->super_copy) != 1)
2425 bytenr = BTRFS_SUPER_INFO_OFFSET;
2426 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2429 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2430 &length, &multi, 0);
2434 num_stripes = multi->num_stripes;
2435 physical = multi->stripes[0].physical;
2438 if (num_stripes != 1 || physical != bytenr)
2442 if (bytenr >= total_bytes)
2450 int do_rollback(const char *devname, int force)
2455 struct btrfs_root *root;
2456 struct btrfs_root *ext2_root;
2457 struct btrfs_root *chunk_root;
2458 struct btrfs_dir_item *dir;
2459 struct btrfs_inode_item *inode;
2460 struct btrfs_file_extent_item *fi;
2461 struct btrfs_trans_handle *trans;
2462 struct extent_buffer *leaf;
2463 struct btrfs_block_group_cache *cache1;
2464 struct btrfs_block_group_cache *cache2;
2465 struct btrfs_key key;
2466 struct btrfs_path path;
2467 struct extent_io_tree io_tree;
2482 extent_io_tree_init(&io_tree);
2484 fd = open(devname, O_RDWR);
2486 fprintf(stderr, "unable to open %s\n", devname);
2489 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2491 fprintf(stderr, "unable to open ctree\n");
2494 ret = may_rollback(root);
2496 fprintf(stderr, "unable to do rollback\n");
2500 sectorsize = root->sectorsize;
2501 buf = malloc(sectorsize);
2503 fprintf(stderr, "unable to allocate memory\n");
2507 btrfs_init_path(&path);
2509 key.objectid = EXT2_IMAGE_SUBVOL_OBJECTID;
2510 key.type = BTRFS_ROOT_ITEM_KEY;
2511 key.offset = (u64)-1;
2512 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2513 if (!ext2_root || IS_ERR(ext2_root)) {
2514 fprintf(stderr, "unable to open subvol %llu\n",
2520 root_dir = btrfs_root_dirid(&root->root_item);
2521 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2522 root_dir, name, strlen(name), 0);
2523 if (!dir || IS_ERR(dir)) {
2524 fprintf(stderr, "unable to find file %s\n", name);
2527 leaf = path.nodes[0];
2528 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2529 btrfs_release_path(ext2_root, &path);
2531 objectid = key.objectid;
2533 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2535 fprintf(stderr, "unable to find inode item\n");
2538 leaf = path.nodes[0];
2539 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2540 total_bytes = btrfs_inode_size(leaf, inode);
2541 btrfs_release_path(ext2_root, &path);
2543 key.objectid = objectid;
2545 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2546 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2548 fprintf(stderr, "unable to find first file extent\n");
2549 btrfs_release_path(ext2_root, &path);
2553 /* build mapping tree for the relocated blocks */
2554 for (offset = 0; offset < total_bytes; ) {
2555 leaf = path.nodes[0];
2556 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2557 ret = btrfs_next_leaf(root, &path);
2563 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2564 if (key.objectid != objectid || key.offset != offset ||
2565 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2568 fi = btrfs_item_ptr(leaf, path.slots[0],
2569 struct btrfs_file_extent_item);
2570 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2572 if (btrfs_file_extent_compression(leaf, fi) ||
2573 btrfs_file_extent_encryption(leaf, fi) ||
2574 btrfs_file_extent_other_encoding(leaf, fi))
2577 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2578 /* skip holes and direct mapped extents */
2579 if (bytenr == 0 || bytenr == offset)
2582 bytenr += btrfs_file_extent_offset(leaf, fi);
2583 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2585 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2586 cache2 = btrfs_lookup_block_group(root->fs_info,
2587 offset + num_bytes - 1);
2588 if (!cache1 || cache1 != cache2 ||
2589 (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
2590 !intersect_with_sb(offset, num_bytes)))
2593 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2594 EXTENT_LOCKED, GFP_NOFS);
2595 set_state_private(&io_tree, offset, bytenr);
2597 offset += btrfs_file_extent_num_bytes(leaf, fi);
2600 btrfs_release_path(ext2_root, &path);
2602 if (offset < total_bytes) {
2603 fprintf(stderr, "unable to build extent mapping\n");
2607 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2608 first_free &= ~((u64)sectorsize - 1);
2609 /* backup for extent #0 should exist */
2610 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2611 fprintf(stderr, "no backup for the first extent\n");
2614 /* force no allocation from system block group */
2615 root->fs_info->system_allocs = -1;
2616 trans = btrfs_start_transaction(root, 1);
2619 * recow the whole chunk tree, this will remove all chunk tree blocks
2620 * from system block group
2622 chunk_root = root->fs_info->chunk_root;
2623 memset(&key, 0, sizeof(key));
2625 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2629 ret = btrfs_next_leaf(chunk_root, &path);
2633 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2634 btrfs_release_path(chunk_root, &path);
2636 btrfs_release_path(chunk_root, &path);
2641 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2645 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2646 num_bytes += btrfs_block_group_used(&cache1->item);
2648 offset = cache1->key.objectid + cache1->key.offset;
2650 /* only extent #0 left in system block group? */
2651 if (num_bytes > first_free) {
2652 fprintf(stderr, "unable to empty system block group\n");
2655 /* create a system chunk that maps the whole device */
2656 ret = prepare_system_chunk_sb(&root->fs_info->super_copy);
2658 fprintf(stderr, "unable to update system chunk\n");
2662 ret = btrfs_commit_transaction(trans, root);
2665 ret = close_ctree(root);
2667 fprintf(stderr, "error during close_ctree %d\n", ret);
2671 /* zero btrfs super block mirrors */
2672 memset(buf, 0, sectorsize);
2673 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2674 bytenr = btrfs_sb_offset(i);
2675 if (bytenr >= total_bytes)
2677 ret = pwrite(fd, buf, sectorsize, bytenr);
2680 sb_bytenr = (u64)-1;
2681 /* copy all relocated blocks back */
2683 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2688 ret = get_state_private(&io_tree, start, &bytenr);
2691 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2694 while (start <= end) {
2695 if (start == BTRFS_SUPER_INFO_OFFSET) {
2699 ret = pread(fd, buf, sectorsize, bytenr);
2701 fprintf(stderr, "error during pread %d\n", ret);
2704 BUG_ON(ret != sectorsize);
2705 ret = pwrite(fd, buf, sectorsize, start);
2707 fprintf(stderr, "error during pwrite %d\n", ret);
2710 BUG_ON(ret != sectorsize);
2712 start += sectorsize;
2713 bytenr += sectorsize;
2719 fprintf(stderr, "error during fsync %d\n", ret);
2723 * finally, overwrite btrfs super block.
2725 ret = pread(fd, buf, sectorsize, sb_bytenr);
2727 fprintf(stderr, "error during pread %d\n", ret);
2730 BUG_ON(ret != sectorsize);
2731 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2733 fprintf(stderr, "error during pwrite %d\n", ret);
2736 BUG_ON(ret != sectorsize);
2739 fprintf(stderr, "error during fsync %d\n", ret);
2745 extent_io_tree_cleanup(&io_tree);
2746 printf("rollback complete.\n");
2749 fprintf(stderr, "rollback aborted.\n");
2753 static void print_usage(void)
2755 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2756 printf("\t-d disable data checksum\n");
2757 printf("\t-i ignore xattrs and ACLs\n");
2758 printf("\t-n disable packing of small files\n");
2759 printf("\t-r roll back to ext2fs\n");
2762 int main(int argc, char *argv[])
2771 int c = getopt(argc, argv, "dinr");
2792 argc = argc - optind;
2798 file = argv[optind];
2799 if (check_mounted(file)) {
2800 fprintf(stderr, "%s is mounted\n", file);
2805 ret = do_rollback(file, 0);
2807 ret = do_convert(file, datacsum, packing, noxattr);