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 *old,
256 int offset, int blocksize,
257 char *buf,void *priv_data)
263 char dotdot[] = "..";
264 struct btrfs_key location;
265 struct ext2_dir_entry_2 *dirent = (struct ext2_dir_entry_2 *)old;
266 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
268 objectid = dirent->inode + INO_OFFSET;
269 if (!strncmp(dirent->name, dotdot, dirent->name_len)) {
270 if (dirent->name_len == 2) {
271 BUG_ON(idata->parent != 0);
272 idata->parent = objectid;
276 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
279 location.objectid = objectid;
281 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
283 file_type = dirent->file_type;
284 BUG_ON(file_type > EXT2_FT_SYMLINK);
285 ret = btrfs_insert_dir_item(idata->trans, idata->root,
286 dirent->name, dirent->name_len,
287 idata->objectid, &location,
288 filetype_conversion_table[file_type],
292 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
293 dirent->name, dirent->name_len,
294 objectid, idata->objectid,
299 inode_size = btrfs_stack_inode_size(idata->inode) +
300 dirent->name_len * 2;
301 btrfs_set_stack_inode_size(idata->inode, inode_size);
304 idata->errcode = ret;
308 static int create_dir_entries(struct btrfs_trans_handle *trans,
309 struct btrfs_root *root, u64 objectid,
310 struct btrfs_inode_item *btrfs_inode,
311 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
315 struct dir_iterate_data data = {
318 .inode = btrfs_inode,
319 .objectid = objectid,
325 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
326 dir_iterate_proc, &data);
330 if (ret == 0 && data.parent == objectid) {
331 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
332 objectid, objectid, 0);
336 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
340 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
341 u32 num_bytes, char *buffer)
344 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
346 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
347 if (ret != num_bytes)
356 * Record a file extent. Do all the required works, such as inserting
357 * file extent item, inserting extent item and backref item into extent
358 * tree and updating block accounting.
360 static int record_file_extent(struct btrfs_trans_handle *trans,
361 struct btrfs_root *root, u64 objectid,
362 struct btrfs_inode_item *inode,
363 u64 file_pos, u64 disk_bytenr,
364 u64 num_bytes, int checksum)
367 struct btrfs_fs_info *info = root->fs_info;
368 struct btrfs_root *extent_root = info->extent_root;
369 struct extent_buffer *leaf;
370 struct btrfs_file_extent_item *fi;
371 struct btrfs_key ins_key;
372 struct btrfs_path path;
373 struct btrfs_extent_item *ei;
374 u32 blocksize = root->sectorsize;
377 if (disk_bytenr == 0) {
378 ret = btrfs_insert_file_extent(trans, root, objectid,
379 file_pos, disk_bytenr,
380 num_bytes, num_bytes);
384 btrfs_init_path(&path);
391 buffer = malloc(blocksize);
394 for (offset = 0; offset < num_bytes; offset += blocksize) {
395 ret = read_disk_extent(root, disk_bytenr + offset,
399 ret = btrfs_csum_file_block(trans,
400 root->fs_info->csum_root,
401 disk_bytenr + num_bytes,
402 disk_bytenr + offset,
412 ins_key.objectid = objectid;
413 ins_key.offset = file_pos;
414 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
415 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
419 leaf = path.nodes[0];
420 fi = btrfs_item_ptr(leaf, path.slots[0],
421 struct btrfs_file_extent_item);
422 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
423 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
424 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
425 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
426 btrfs_set_file_extent_offset(leaf, fi, 0);
427 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
428 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
429 btrfs_set_file_extent_compression(leaf, fi, 0);
430 btrfs_set_file_extent_encryption(leaf, fi, 0);
431 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
432 btrfs_mark_buffer_dirty(leaf);
434 nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
435 btrfs_set_stack_inode_nbytes(inode, nbytes);
437 btrfs_release_path(root, &path);
439 ins_key.objectid = disk_bytenr;
440 ins_key.offset = num_bytes;
441 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
443 ret = btrfs_insert_empty_item(trans, extent_root, &path,
444 &ins_key, sizeof(*ei));
446 leaf = path.nodes[0];
447 ei = btrfs_item_ptr(leaf, path.slots[0],
448 struct btrfs_extent_item);
450 btrfs_set_extent_refs(leaf, ei, 0);
451 btrfs_set_extent_generation(leaf, ei, 0);
452 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
454 btrfs_mark_buffer_dirty(leaf);
456 ret = btrfs_update_block_group(trans, root, disk_bytenr,
460 } else if (ret != -EEXIST) {
463 btrfs_extent_post_op(trans, extent_root);
465 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
466 root->root_key.objectid,
472 btrfs_release_path(root, &path);
476 static int record_file_blocks(struct btrfs_trans_handle *trans,
477 struct btrfs_root *root, u64 objectid,
478 struct btrfs_inode_item *inode,
479 u64 file_block, u64 disk_block,
480 u64 num_blocks, int checksum)
482 u64 file_pos = file_block * root->sectorsize;
483 u64 disk_bytenr = disk_block * root->sectorsize;
484 u64 num_bytes = num_blocks * root->sectorsize;
485 return record_file_extent(trans, root, objectid, inode, file_pos,
486 disk_bytenr, num_bytes, checksum);
489 struct blk_iterate_data {
490 struct btrfs_trans_handle *trans;
491 struct btrfs_root *root;
492 struct btrfs_inode_item *inode;
502 static int block_iterate_proc(ext2_filsys ext2_fs,
503 u64 disk_block, u64 file_block,
504 struct blk_iterate_data *idata)
509 struct btrfs_root *root = idata->root;
510 struct btrfs_trans_handle *trans = idata->trans;
511 struct btrfs_block_group_cache *cache;
512 u64 bytenr = disk_block * root->sectorsize;
514 sb_region = intersect_with_sb(bytenr, root->sectorsize);
515 do_barrier = sb_region || disk_block >= idata->boundary;
516 if ((idata->num_blocks > 0 && do_barrier) ||
517 (file_block > idata->first_block + idata->num_blocks) ||
518 (disk_block != idata->disk_block + idata->num_blocks)) {
519 if (idata->num_blocks > 0) {
520 ret = record_file_blocks(trans, root, idata->objectid,
521 idata->inode, idata->first_block,
522 idata->disk_block, idata->num_blocks,
526 idata->first_block += idata->num_blocks;
527 idata->num_blocks = 0;
529 if (file_block > idata->first_block) {
530 ret = record_file_blocks(trans, root, idata->objectid,
531 idata->inode, idata->first_block,
532 0, file_block - idata->first_block,
539 bytenr += STRIPE_LEN - 1;
540 bytenr &= ~((u64)STRIPE_LEN - 1);
542 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
544 bytenr = cache->key.objectid + cache->key.offset;
547 idata->first_block = file_block;
548 idata->disk_block = disk_block;
549 idata->boundary = bytenr / root->sectorsize;
554 idata->errcode = ret;
558 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
559 e2_blkcnt_t blockcnt, blk_t ref_block,
560 int ref_offset, void *priv_data)
562 struct blk_iterate_data *idata;
563 idata = (struct blk_iterate_data *)priv_data;
564 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
568 * traverse file's data blocks, record these data blocks as file extents.
570 static int create_file_extents(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root, u64 objectid,
572 struct btrfs_inode_item *btrfs_inode,
573 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
574 int datacsum, int packing)
580 u32 sectorsize = root->sectorsize;
581 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
582 struct blk_iterate_data data = {
585 .inode = btrfs_inode,
586 .objectid = objectid,
591 .checksum = datacsum,
594 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
595 NULL, __block_iterate_proc, &data);
601 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
602 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
603 u64 num_bytes = data.num_blocks * sectorsize;
604 u64 disk_bytenr = data.disk_block * sectorsize;
607 buffer = malloc(num_bytes);
610 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
613 if (num_bytes > inode_size)
614 num_bytes = inode_size;
615 ret = btrfs_insert_inline_extent(trans, root, objectid,
616 0, buffer, num_bytes);
619 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
620 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
621 } else if (data.num_blocks > 0) {
622 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
623 data.first_block, data.disk_block,
624 data.num_blocks, data.checksum);
628 data.first_block += data.num_blocks;
629 last_block = (inode_size + sectorsize - 1) / sectorsize;
630 if (last_block > data.first_block) {
631 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
632 data.first_block, 0, last_block -
633 data.first_block, data.checksum);
640 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
644 static int create_symbol_link(struct btrfs_trans_handle *trans,
645 struct btrfs_root *root, u64 objectid,
646 struct btrfs_inode_item *btrfs_inode,
647 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
648 struct ext2_inode *ext2_inode)
652 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
653 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
654 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
655 ret = create_file_extents(trans, root, objectid, btrfs_inode,
656 ext2_fs, ext2_ino, 1, 1);
657 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
661 pathname = (char *)&(ext2_inode->i_block[0]);
662 BUG_ON(pathname[inode_size] != 0);
663 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
664 pathname, inode_size + 1);
665 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
670 * Following xattr/acl related codes are based on codes in
671 * fs/ext3/xattr.c and fs/ext3/acl.c
673 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
674 #define EXT2_XATTR_BFIRST(ptr) \
675 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
676 #define EXT2_XATTR_IHDR(inode) \
677 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
678 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
679 #define EXT2_XATTR_IFIRST(inode) \
680 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
681 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
683 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
686 struct ext2_ext_attr_entry *next;
688 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
689 next = EXT2_EXT_ATTR_NEXT(entry);
690 if ((void *)next >= end)
697 static int ext2_xattr_check_block(const char *buf, size_t size)
700 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
702 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
703 header->h_blocks != 1)
705 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
709 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
712 size_t value_size = entry->e_value_size;
714 if (entry->e_value_block != 0 || value_size > size ||
715 entry->e_value_offs + value_size > size)
720 #define EXT2_ACL_VERSION 0x0001
731 } ext2_acl_entry_short;
737 static inline int ext2_acl_count(size_t size)
740 size -= sizeof(ext2_acl_header);
741 s = size - 4 * sizeof(ext2_acl_entry_short);
743 if (size % sizeof(ext2_acl_entry_short))
745 return size / sizeof(ext2_acl_entry_short);
747 if (s % sizeof(ext2_acl_entry))
749 return s / sizeof(ext2_acl_entry) + 4;
753 #define ACL_EA_VERSION 0x0002
763 acl_ea_entry a_entries[0];
766 static inline size_t acl_ea_size(int count)
768 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
771 static int ext2_acl_to_xattr(void *dst, const void *src,
772 size_t dst_size, size_t src_size)
775 const void *end = src + src_size;
776 acl_ea_header *ext_acl = (acl_ea_header *)dst;
777 acl_ea_entry *dst_entry = ext_acl->a_entries;
778 ext2_acl_entry *src_entry;
780 if (src_size < sizeof(ext2_acl_header))
782 if (((ext2_acl_header *)src)->a_version !=
783 cpu_to_le32(EXT2_ACL_VERSION))
785 src += sizeof(ext2_acl_header);
786 count = ext2_acl_count(src_size);
790 BUG_ON(dst_size < acl_ea_size(count));
791 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
792 for (i = 0; i < count; i++, dst_entry++) {
793 src_entry = (ext2_acl_entry *)src;
794 if (src + sizeof(ext2_acl_entry_short) > end)
796 dst_entry->e_tag = src_entry->e_tag;
797 dst_entry->e_perm = src_entry->e_perm;
798 switch (le16_to_cpu(src_entry->e_tag)) {
803 src += sizeof(ext2_acl_entry_short);
804 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
808 src += sizeof(ext2_acl_entry);
811 dst_entry->e_id = src_entry->e_id;
824 static char *xattr_prefix_table[] = {
826 [2] = "system.posix_acl_access",
827 [3] = "system.posix_acl_default",
832 static int copy_single_xattr(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root, u64 objectid,
834 struct ext2_ext_attr_entry *entry,
835 const void *data, u32 datalen)
840 void *databuf = NULL;
841 char namebuf[XATTR_NAME_MAX + 1];
843 name_index = entry->e_name_index;
844 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
845 xattr_prefix_table[name_index] == NULL)
847 name_len = strlen(xattr_prefix_table[name_index]) +
849 if (name_len >= sizeof(namebuf))
852 if (name_index == 2 || name_index == 3) {
853 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
854 databuf = malloc(bufsize);
857 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
863 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
864 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
865 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
866 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
867 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
868 objectid - INO_OFFSET, name_len, namebuf);
871 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
872 data, datalen, objectid);
879 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root, u64 objectid,
881 struct btrfs_inode_item *btrfs_inode,
882 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
888 u32 block_size = ext2_fs->blocksize;
889 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
890 struct ext2_inode_large *ext2_inode;
891 struct ext2_ext_attr_entry *entry;
894 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
896 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
897 ext2_inode = (struct ext2_inode_large *)inode_buf;
899 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
903 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
906 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
912 if (ext2_ino > ext2_fs->super->s_first_ino &&
913 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
914 if (EXT2_GOOD_OLD_INODE_SIZE +
915 ext2_inode->i_extra_isize > inode_size) {
919 if (ext2_inode->i_extra_isize != 0 &&
920 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
921 EXT2_EXT_ATTR_MAGIC) {
927 void *end = (void *)ext2_inode + inode_size;
928 entry = EXT2_XATTR_IFIRST(ext2_inode);
929 total = end - (void *)entry;
930 ret = ext2_xattr_check_names(entry, end);
933 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
934 ret = ext2_xattr_check_entry(entry, total);
937 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
939 datalen = entry->e_value_size;
940 ret = copy_single_xattr(trans, root, objectid,
941 entry, data, datalen);
944 entry = EXT2_EXT_ATTR_NEXT(entry);
948 if (ext2_inode->i_file_acl == 0)
951 buffer = malloc(block_size);
956 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
958 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
963 ret = ext2_xattr_check_block(buffer, block_size);
967 entry = EXT2_XATTR_BFIRST(buffer);
968 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
969 ret = ext2_xattr_check_entry(entry, block_size);
972 data = buffer + entry->e_value_offs;
973 datalen = entry->e_value_size;
974 ret = copy_single_xattr(trans, root, objectid,
975 entry, data, datalen);
978 entry = EXT2_EXT_ATTR_NEXT(entry);
983 if ((void *)ext2_inode != inode_buf)
988 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
990 static inline dev_t old_decode_dev(u16 val)
992 return MKDEV((val >> 8) & 255, val & 255);
995 static inline dev_t new_decode_dev(u32 dev)
997 unsigned major = (dev & 0xfff00) >> 8;
998 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
999 return MKDEV(major, minor);
1002 static int copy_inode_item(struct btrfs_inode_item *dst,
1003 struct ext2_inode *src, u32 blocksize)
1005 btrfs_set_stack_inode_generation(dst, 1);
1006 btrfs_set_stack_inode_size(dst, src->i_size);
1007 btrfs_set_stack_inode_nbytes(dst, 0);
1008 btrfs_set_stack_inode_block_group(dst, 0);
1009 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
1010 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
1011 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
1012 btrfs_set_stack_inode_mode(dst, src->i_mode);
1013 btrfs_set_stack_inode_rdev(dst, 0);
1014 btrfs_set_stack_inode_flags(dst, 0);
1015 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
1016 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
1017 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
1018 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
1019 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
1020 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
1021 btrfs_set_stack_timespec_sec(&dst->otime, 0);
1022 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
1024 if (S_ISDIR(src->i_mode)) {
1025 btrfs_set_stack_inode_size(dst, 0);
1026 btrfs_set_stack_inode_nlink(dst, 1);
1028 if (S_ISREG(src->i_mode)) {
1029 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1032 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1033 !S_ISLNK(src->i_mode)) {
1034 if (src->i_block[0]) {
1035 btrfs_set_stack_inode_rdev(dst,
1036 old_decode_dev(src->i_block[0]));
1038 btrfs_set_stack_inode_rdev(dst,
1039 new_decode_dev(src->i_block[1]));
1046 * copy a single inode. do all the required works, such as cloning
1047 * inode item, creating file extents and creating directory entries.
1049 static int copy_single_inode(struct btrfs_trans_handle *trans,
1050 struct btrfs_root *root, u64 objectid,
1051 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1052 struct ext2_inode *ext2_inode,
1053 int datacsum, int packing, int noxattr)
1056 struct btrfs_key inode_key;
1057 struct btrfs_inode_item btrfs_inode;
1059 if (ext2_inode->i_links_count == 0)
1062 copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1063 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1064 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1065 BTRFS_INODE_NODATASUM;
1066 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1069 switch (ext2_inode->i_mode & S_IFMT) {
1071 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1072 ext2_fs, ext2_ino, datacsum, packing);
1075 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1079 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1080 ext2_fs, ext2_ino, ext2_inode);
1090 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1095 inode_key.objectid = objectid;
1096 inode_key.offset = 0;
1097 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1098 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1102 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1103 u64 src_bytenr, u32 num_bytes)
1107 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1109 buffer = malloc(num_bytes);
1112 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1113 if (ret != num_bytes)
1115 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1116 if (ret != num_bytes)
1126 * scan ext2's inode bitmap and copy all used inodes.
1128 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1129 int datacsum, int packing, int noxattr)
1133 ext2_inode_scan ext2_scan;
1134 struct ext2_inode ext2_inode;
1135 ext2_ino_t ext2_ino;
1137 struct btrfs_trans_handle *trans;
1139 trans = btrfs_start_transaction(root, 1);
1142 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1144 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1147 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1149 /* no more inodes */
1152 /* skip special inode in ext2fs */
1153 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1154 ext2_ino != EXT2_ROOT_INO)
1156 objectid = ext2_ino + INO_OFFSET;
1157 ret = copy_single_inode(trans, root,
1158 objectid, ext2_fs, ext2_ino,
1159 &ext2_inode, datacsum, packing,
1163 if (trans->blocks_used >= 4096) {
1164 ret = btrfs_commit_transaction(trans, root);
1166 trans = btrfs_start_transaction(root, 1);
1171 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1174 ret = btrfs_commit_transaction(trans, root);
1181 * Construct a range of ext2fs image file.
1182 * scan block allocation bitmap, find all blocks used by the ext2fs
1183 * in this range and create file extents that point to these blocks.
1185 * Note: Before calling the function, no file extent points to blocks
1188 static int create_image_file_range(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root, u64 objectid,
1190 struct btrfs_inode_item *inode,
1191 u64 start_byte, u64 end_byte,
1192 ext2_filsys ext2_fs)
1194 u32 blocksize = ext2_fs->blocksize;
1195 u32 block = start_byte / blocksize;
1196 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1198 struct blk_iterate_data data = {
1202 .objectid = objectid,
1203 .first_block = block,
1206 .boundary = (u64)-1,
1210 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1211 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1213 ret = block_iterate_proc(NULL, block, block, &data);
1214 if (ret & BLOCK_ABORT) {
1219 if (data.num_blocks > 0) {
1220 ret = record_file_blocks(trans, root, objectid, inode,
1221 data.first_block, data.disk_block,
1222 data.num_blocks, 0);
1225 data.first_block += data.num_blocks;
1227 if (last_block > data.first_block) {
1228 ret = record_file_blocks(trans, root, objectid, inode,
1229 data.first_block, 0, last_block -
1230 data.first_block, 0);
1238 * Create the ext2fs image file.
1240 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1244 struct btrfs_key key;
1245 struct btrfs_key location;
1246 struct btrfs_path path;
1247 struct btrfs_inode_item btrfs_inode;
1248 struct btrfs_inode_item *inode_item;
1249 struct extent_buffer *leaf;
1250 struct btrfs_fs_info *fs_info = root->fs_info;
1251 struct btrfs_root *extent_root = fs_info->extent_root;
1252 struct btrfs_trans_handle *trans;
1253 struct btrfs_extent_item *ei;
1254 struct btrfs_extent_inline_ref *iref;
1255 struct btrfs_extent_data_ref *dref;
1262 u32 sectorsize = root->sectorsize;
1264 total_bytes = btrfs_super_total_bytes(&fs_info->super_copy);
1265 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1266 first_free &= ~((u64)sectorsize - 1);
1268 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1269 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1270 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1271 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1272 btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1273 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1274 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1275 BTRFS_INODE_READONLY);
1276 btrfs_init_path(&path);
1277 trans = btrfs_start_transaction(root, 1);
1280 objectid = btrfs_root_dirid(&root->root_item);
1281 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1286 * copy blocks covered by extent #0 to new positions. extent #0 is
1287 * special, we can't rely on relocate_extents_range to relocate it.
1289 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1290 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1293 ret = copy_disk_extent(root, key.objectid, last_byte,
1297 ret = record_file_extent(trans, root, objectid,
1298 &btrfs_inode, last_byte,
1299 key.objectid, sectorsize, 0);
1305 key.objectid = last_byte;
1307 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1308 ret = btrfs_search_slot(trans, fs_info->extent_root,
1313 leaf = path.nodes[0];
1314 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1315 ret = btrfs_next_leaf(extent_root, &path);
1320 leaf = path.nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1323 if (last_byte > key.objectid ||
1324 key.type != BTRFS_EXTENT_ITEM_KEY) {
1329 bytenr = key.objectid;
1330 num_bytes = key.offset;
1331 ei = btrfs_item_ptr(leaf, path.slots[0],
1332 struct btrfs_extent_item);
1333 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1338 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1339 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1341 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1342 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1343 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1344 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1345 if (btrfs_extent_data_ref_root(leaf, dref) !=
1346 BTRFS_FS_TREE_OBJECTID) {
1351 if (bytenr > last_byte) {
1352 ret = create_image_file_range(trans, root, objectid,
1353 &btrfs_inode, last_byte,
1358 ret = record_file_extent(trans, root, objectid, &btrfs_inode,
1359 bytenr, bytenr, num_bytes, 0);
1362 last_byte = bytenr + num_bytes;
1363 btrfs_release_path(extent_root, &path);
1365 if (trans->blocks_used >= 4096) {
1366 ret = btrfs_commit_transaction(trans, root);
1368 trans = btrfs_start_transaction(root, 1);
1372 btrfs_release_path(root, &path);
1373 if (total_bytes > last_byte) {
1374 ret = create_image_file_range(trans, root, objectid,
1375 &btrfs_inode, last_byte,
1376 total_bytes, ext2_fs);
1381 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1385 location.objectid = objectid;
1386 location.offset = 0;
1387 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1388 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1389 btrfs_root_dirid(&root->root_item),
1390 &location, EXT2_FT_REG_FILE, objectid);
1393 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1395 btrfs_root_dirid(&root->root_item),
1399 location.objectid = btrfs_root_dirid(&root->root_item);
1400 location.offset = 0;
1401 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1402 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1405 leaf = path.nodes[0];
1406 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1407 struct btrfs_inode_item);
1408 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1409 btrfs_inode_size(leaf, inode_item));
1410 btrfs_mark_buffer_dirty(leaf);
1411 btrfs_release_path(root, &path);
1412 ret = btrfs_commit_transaction(trans, root);
1415 btrfs_release_path(root, &path);
1419 struct btrfs_root *link_subvol(struct btrfs_root *root, const char *base,
1422 struct btrfs_trans_handle *trans;
1423 struct btrfs_fs_info *fs_info = root->fs_info;
1424 struct btrfs_root *tree_root = fs_info->tree_root;
1425 struct btrfs_root *new_root = NULL;
1426 struct btrfs_path *path;
1427 struct btrfs_inode_item *inode_item;
1428 struct extent_buffer *leaf;
1429 struct btrfs_key key;
1430 u64 dirid = btrfs_root_dirid(&root->root_item);
1436 path = btrfs_alloc_path();
1439 key.objectid = dirid;
1440 key.type = BTRFS_DIR_INDEX_KEY;
1441 key.offset = (u64)-1;
1443 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1446 if (path->slots[0] > 0) {
1448 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1449 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1450 index = key.offset + 1;
1452 btrfs_release_path(root, path);
1454 trans = btrfs_start_transaction(root, 1);
1457 key.objectid = dirid;
1459 key.type = BTRFS_INODE_ITEM_KEY;
1461 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1463 leaf = path->nodes[0];
1464 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1465 struct btrfs_inode_item);
1467 key.objectid = root_objectid;
1468 key.offset = (u64)-1;
1469 key.type = BTRFS_ROOT_ITEM_KEY;
1472 for (i = 0; i < 1024; i++) {
1473 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf),
1474 dirid, &key, BTRFS_FT_DIR, index);
1477 sprintf(buf, "%s%d", base, i);
1482 btrfs_set_inode_size(leaf, inode_item, strlen(buf) * 2 +
1483 btrfs_inode_size(leaf, inode_item));
1484 btrfs_mark_buffer_dirty(leaf);
1485 btrfs_release_path(root, path);
1487 /* add the backref first */
1488 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1489 BTRFS_ROOT_BACKREF_KEY,
1490 root->root_key.objectid,
1491 dirid, index, buf, strlen(buf));
1494 /* now add the forward ref */
1495 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1496 BTRFS_ROOT_REF_KEY, root_objectid,
1497 dirid, index, buf, strlen(buf));
1499 ret = btrfs_commit_transaction(trans, root);
1502 new_root = btrfs_read_fs_root(fs_info, &key);
1503 if (IS_ERR(new_root))
1506 btrfs_free_path(path);
1510 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1511 struct btrfs_root *root)
1513 struct btrfs_fs_info *info = root->fs_info;
1514 struct btrfs_root *chunk_root = info->chunk_root;
1515 struct btrfs_root *extent_root = info->extent_root;
1516 struct btrfs_device *device;
1517 struct btrfs_block_group_cache *cache;
1518 struct btrfs_dev_extent *extent;
1519 struct extent_buffer *leaf;
1520 struct btrfs_chunk chunk;
1521 struct btrfs_key key;
1522 struct btrfs_path path;
1528 btrfs_init_path(&path);
1530 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1531 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1533 BUG_ON(list_empty(&info->fs_devices->devices));
1534 device = list_entry(info->fs_devices->devices.next,
1535 struct btrfs_device, dev_list);
1536 BUG_ON(device->devid != info->fs_devices->latest_devid);
1538 /* delete device extent created by make_btrfs */
1539 key.objectid = device->devid;
1541 key.type = BTRFS_DEV_EXTENT_KEY;
1542 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1547 ret = btrfs_del_item(trans, device->dev_root, &path);
1550 btrfs_release_path(device->dev_root, &path);
1552 /* delete chunk item created by make_btrfs */
1553 key.objectid = chunk_objectid;
1555 key.type = BTRFS_CHUNK_ITEM_KEY;
1556 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1561 ret = btrfs_del_item(trans, chunk_root, &path);
1564 btrfs_release_path(chunk_root, &path);
1566 /* for each block group, create device extent and chunk item */
1568 while (cur_start < total_bytes) {
1569 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1572 /* insert device extent */
1573 key.objectid = device->devid;
1574 key.offset = cache->key.objectid;
1575 key.type = BTRFS_DEV_EXTENT_KEY;
1576 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1577 &key, sizeof(*extent));
1581 leaf = path.nodes[0];
1582 extent = btrfs_item_ptr(leaf, path.slots[0],
1583 struct btrfs_dev_extent);
1585 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1586 chunk_root->root_key.objectid);
1587 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1589 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1590 cache->key.objectid);
1591 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1592 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1593 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1595 btrfs_mark_buffer_dirty(leaf);
1596 btrfs_release_path(device->dev_root, &path);
1598 /* insert chunk item */
1599 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1600 btrfs_set_stack_chunk_owner(&chunk,
1601 extent_root->root_key.objectid);
1602 btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN);
1603 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1604 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1605 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1606 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1607 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1608 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1609 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1610 btrfs_set_stack_stripe_offset(&chunk.stripe,
1611 cache->key.objectid);
1612 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1614 key.objectid = chunk_objectid;
1615 key.offset = cache->key.objectid;
1616 key.type = BTRFS_CHUNK_ITEM_KEY;
1618 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1619 btrfs_chunk_item_size(1));
1623 cur_start = cache->key.objectid + cache->key.offset;
1626 device->bytes_used = total_bytes;
1627 ret = btrfs_update_device(trans, device);
1629 btrfs_release_path(device->dev_root, &path);
1633 static int create_subvol(struct btrfs_trans_handle *trans,
1634 struct btrfs_root *root, u64 root_objectid)
1636 struct extent_buffer *tmp;
1637 struct btrfs_root *new_root;
1638 struct btrfs_key key;
1639 struct btrfs_root_item root_item;
1642 ret = btrfs_copy_root(trans, root, root->node, &tmp,
1646 memcpy(&root_item, &root->root_item, sizeof(root_item));
1647 btrfs_set_root_bytenr(&root_item, tmp->start);
1648 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1649 btrfs_set_root_generation(&root_item, trans->transid);
1650 free_extent_buffer(tmp);
1652 key.objectid = root_objectid;
1653 key.type = BTRFS_ROOT_ITEM_KEY;
1654 key.offset = trans->transid;
1655 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1658 key.offset = (u64)-1;
1659 new_root = btrfs_read_fs_root(root->fs_info, &key);
1660 BUG_ON(!new_root || IS_ERR(new_root));
1662 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1668 static int init_btrfs(struct btrfs_root *root)
1671 struct btrfs_key location;
1672 struct btrfs_trans_handle *trans;
1673 struct btrfs_fs_info *fs_info = root->fs_info;
1674 struct extent_buffer *tmp;
1676 trans = btrfs_start_transaction(root, 1);
1678 ret = btrfs_make_block_groups(trans, root);
1681 ret = btrfs_fix_block_accounting(trans, root);
1684 ret = create_chunk_mapping(trans, root);
1687 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1688 BTRFS_ROOT_TREE_DIR_OBJECTID);
1691 memcpy(&location, &root->root_key, sizeof(location));
1692 location.offset = (u64)-1;
1693 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1694 btrfs_super_root_dir(&fs_info->super_copy),
1695 &location, BTRFS_FT_DIR, 0);
1698 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1700 btrfs_super_root_dir(&fs_info->super_copy), 0);
1703 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1704 BTRFS_FIRST_FREE_OBJECTID);
1706 /* subvol for ext2 image file */
1707 ret = create_subvol(trans, root, EXT2_IMAGE_SUBVOL_OBJECTID);
1709 /* subvol for data relocation */
1710 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1713 ret = __btrfs_cow_block(trans, fs_info->csum_root,
1714 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1716 free_extent_buffer(tmp);
1718 ret = btrfs_commit_transaction(trans, root);
1725 * Migrate super block to it's default position and zero 0 ~ 16k
1727 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1730 struct extent_buffer *buf;
1731 struct btrfs_super_block *super;
1735 BUG_ON(sectorsize < sizeof(*super));
1736 buf = malloc(sizeof(*buf) + sectorsize);
1740 buf->len = sectorsize;
1741 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1742 if (ret != sectorsize)
1745 super = (struct btrfs_super_block *)buf->data;
1746 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1747 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1749 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1750 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1751 if (ret != sectorsize)
1758 memset(buf->data, 0, sectorsize);
1759 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1760 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1761 if (len > sectorsize)
1763 ret = pwrite(fd, buf->data, len, bytenr);
1765 fprintf(stderr, "unable to zero fill device\n");
1779 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1781 struct btrfs_chunk *chunk;
1782 struct btrfs_disk_key *key;
1783 u32 sectorsize = btrfs_super_sectorsize(super);
1785 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1786 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1787 sizeof(struct btrfs_disk_key));
1789 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1790 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1791 btrfs_set_disk_key_offset(key, 0);
1793 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1794 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1795 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1796 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1797 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1798 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1799 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1800 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1801 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1802 chunk->stripe.devid = super->dev_item.devid;
1803 chunk->stripe.offset = cpu_to_le64(0);
1804 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1805 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1809 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1812 struct extent_buffer *buf;
1813 struct btrfs_super_block *super;
1815 BUG_ON(sectorsize < sizeof(*super));
1816 buf = malloc(sizeof(*buf) + sectorsize);
1820 buf->len = sectorsize;
1821 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1822 if (ret != sectorsize)
1825 super = (struct btrfs_super_block *)buf->data;
1826 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1827 BUG_ON(btrfs_super_num_devices(super) != 1);
1829 ret = prepare_system_chunk_sb(super);
1833 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1834 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1835 if (ret != sectorsize)
1846 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1847 struct btrfs_root *root,
1848 u64 extent_start, u64 extent_size,
1849 struct btrfs_key *extent_key,
1850 struct extent_io_tree *reloc_tree)
1852 struct extent_buffer *leaf;
1853 struct btrfs_file_extent_item *fi;
1854 struct btrfs_key key;
1855 struct btrfs_path path;
1856 struct btrfs_inode_item inode;
1857 struct blk_iterate_data data;
1864 u32 sectorsize = root->sectorsize;
1870 btrfs_init_path(&path);
1871 ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
1875 leaf = path.nodes[0];
1876 fi = btrfs_item_ptr(leaf, path.slots[0],
1877 struct btrfs_file_extent_item);
1878 BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
1879 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1880 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1885 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1886 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1888 ret = btrfs_del_item(trans, root, &path);
1892 ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
1893 root->root_key.objectid,
1894 extent_key->objectid, extent_key->offset);
1898 btrfs_release_path(root, &path);
1900 key.objectid = extent_key->objectid;
1902 key.type = BTRFS_INODE_ITEM_KEY;
1903 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1907 leaf = path.nodes[0];
1908 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1909 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1910 btrfs_release_path(root, &path);
1912 BUG_ON(num_bytes & (sectorsize - 1));
1913 nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
1914 btrfs_set_stack_inode_nbytes(&inode, nbytes);
1915 datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
1917 data = (struct blk_iterate_data) {
1921 .objectid = extent_key->objectid,
1922 .first_block = extent_key->offset / sectorsize,
1925 .boundary = (u64)-1,
1926 .checksum = datacsum,
1930 cur_offset = extent_key->offset;
1931 while (num_bytes > 0) {
1932 sector_end = bytenr + sectorsize - 1;
1933 if (test_range_bit(reloc_tree, bytenr, sector_end,
1934 EXTENT_LOCKED, 1)) {
1935 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1938 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1941 new_pos = key.objectid;
1943 if (cur_offset == extent_key->offset) {
1944 fd = root->fs_info->fs_devices->latest_bdev;
1945 readahead(fd, bytenr, num_bytes);
1947 ret = copy_disk_extent(root, new_pos, bytenr,
1951 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1952 EXTENT_LOCKED, GFP_NOFS);
1954 ret = set_state_private(reloc_tree, bytenr, new_pos);
1958 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1959 cur_offset / sectorsize, &data);
1960 if (ret & BLOCK_ABORT) {
1965 cur_offset += sectorsize;
1966 bytenr += sectorsize;
1967 num_bytes -= sectorsize;
1970 if (data.num_blocks > 0) {
1971 ret = record_file_blocks(trans, root,
1972 extent_key->objectid, &inode,
1973 data.first_block, data.disk_block,
1974 data.num_blocks, datacsum);
1979 key.objectid = extent_key->objectid;
1981 key.type = BTRFS_INODE_ITEM_KEY;
1982 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1986 leaf = path.nodes[0];
1987 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1988 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1989 btrfs_mark_buffer_dirty(leaf);
1990 btrfs_release_path(root, &path);
1993 btrfs_release_path(root, &path);
1997 static int relocate_extents_range(struct btrfs_root *fs_root,
1998 struct btrfs_root *ext2_root,
1999 u64 start_byte, u64 end_byte)
2001 struct btrfs_fs_info *info = fs_root->fs_info;
2002 struct btrfs_root *extent_root = info->extent_root;
2003 struct btrfs_root *cur_root = NULL;
2004 struct btrfs_trans_handle *trans;
2005 struct btrfs_extent_data_ref *dref;
2006 struct btrfs_extent_inline_ref *iref;
2007 struct btrfs_extent_item *ei;
2008 struct extent_buffer *leaf;
2009 struct btrfs_key key;
2010 struct btrfs_key extent_key;
2011 struct btrfs_path path;
2012 struct extent_io_tree reloc_tree;
2022 btrfs_init_path(&path);
2023 extent_io_tree_init(&reloc_tree);
2025 key.objectid = start_byte;
2027 key.type = BTRFS_EXTENT_ITEM_KEY;
2028 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2032 ret = btrfs_previous_item(extent_root, &path, 0,
2033 BTRFS_EXTENT_ITEM_KEY);
2037 leaf = path.nodes[0];
2038 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2039 if (key.objectid + key.offset > start_byte)
2040 start_byte = key.objectid;
2043 btrfs_release_path(extent_root, &path);
2045 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
2048 trans = btrfs_start_transaction(cur_root, 1);
2051 cur_byte = start_byte;
2053 key.objectid = cur_byte;
2055 key.type = BTRFS_EXTENT_ITEM_KEY;
2056 ret = btrfs_search_slot(trans, extent_root,
2061 leaf = path.nodes[0];
2062 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2063 ret = btrfs_next_leaf(extent_root, &path);
2068 leaf = path.nodes[0];
2071 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2072 if (key.objectid < cur_byte ||
2073 key.type != BTRFS_EXTENT_ITEM_KEY) {
2077 if (key.objectid >= end_byte)
2082 cur_byte = key.objectid;
2083 num_bytes = key.offset;
2084 ei = btrfs_item_ptr(leaf, path.slots[0],
2085 struct btrfs_extent_item);
2086 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2087 BTRFS_EXTENT_FLAG_DATA));
2089 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2090 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2092 ptr += sizeof(struct btrfs_extent_item);
2095 iref = (struct btrfs_extent_inline_ref *)ptr;
2096 key.type = btrfs_extent_inline_ref_type(leaf, iref);
2097 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2098 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2099 ref_root = btrfs_extent_data_ref_root(leaf, dref);
2100 extent_key.objectid =
2101 btrfs_extent_data_ref_objectid(leaf, dref);
2103 btrfs_extent_data_ref_offset(leaf, dref);
2104 extent_key.type = BTRFS_EXTENT_DATA_KEY;
2105 BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2107 if (ref_root == cur_root->root_key.objectid)
2110 ptr += btrfs_extent_inline_ref_size(key.type);
2118 ret = relocate_one_reference(trans, cur_root, cur_byte,
2119 num_bytes, &extent_key,
2124 cur_byte += num_bytes;
2125 btrfs_release_path(extent_root, &path);
2127 if (trans->blocks_used >= 4096) {
2128 ret = btrfs_commit_transaction(trans, cur_root);
2130 trans = btrfs_start_transaction(cur_root, 1);
2134 btrfs_release_path(cur_root, &path);
2136 ret = btrfs_commit_transaction(trans, cur_root);
2139 if (num_extents > 0 && pass++ < 16)
2142 ret = (num_extents > 0) ? -1 : 0;
2144 btrfs_release_path(cur_root, &path);
2145 extent_io_tree_cleanup(&reloc_tree);
2150 * relocate data in system chunk
2152 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2153 struct btrfs_root *ext2_root)
2155 struct btrfs_block_group_cache *cache;
2161 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2165 end_byte = cache->key.objectid + cache->key.offset;
2166 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2167 ret = relocate_extents_range(fs_root, ext2_root,
2168 cache->key.objectid,
2175 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2176 offset = btrfs_sb_offset(i);
2177 offset &= ~((u64)STRIPE_LEN - 1);
2179 ret = relocate_extents_range(fs_root, ext2_root,
2180 offset, offset + STRIPE_LEN);
2189 static int fixup_chunk_mapping(struct btrfs_root *root)
2191 struct btrfs_trans_handle *trans;
2192 struct btrfs_fs_info *info = root->fs_info;
2193 struct btrfs_root *chunk_root = info->chunk_root;
2194 struct extent_buffer *leaf;
2195 struct btrfs_key key;
2196 struct btrfs_path path;
2197 struct btrfs_chunk chunk;
2203 btrfs_init_path(&path);
2205 trans = btrfs_start_transaction(root, 1);
2209 * recow the whole chunk tree. this will move all chunk tree blocks
2210 * into system block group.
2212 memset(&key, 0, sizeof(key));
2214 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2218 ret = btrfs_next_leaf(chunk_root, &path);
2224 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2225 btrfs_release_path(chunk_root, &path);
2227 btrfs_release_path(chunk_root, &path);
2229 /* fixup the system chunk array in super block */
2230 btrfs_set_super_sys_array_size(&info->super_copy, 0);
2232 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2234 key.type = BTRFS_CHUNK_ITEM_KEY;
2236 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2241 leaf = path.nodes[0];
2242 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2243 ret = btrfs_next_leaf(chunk_root, &path);
2248 leaf = path.nodes[0];
2250 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2251 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2254 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2255 size = btrfs_item_size_nr(leaf, path.slots[0]);
2256 BUG_ON(size != sizeof(chunk));
2257 read_extent_buffer(leaf, &chunk, ptr, size);
2258 type = btrfs_stack_chunk_type(&chunk);
2260 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2263 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2271 ret = btrfs_commit_transaction(trans, root);
2274 btrfs_release_path(chunk_root, &path);
2278 int do_convert(const char *devname, int datacsum, int packing, int noxattr)
2286 ext2_filsys ext2_fs;
2287 struct btrfs_root *root;
2288 struct btrfs_root *ext2_root;
2290 ret = open_ext2fs(devname, &ext2_fs);
2292 fprintf(stderr, "unable to open the Ext2fs\n");
2295 blocksize = ext2_fs->blocksize;
2296 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2297 if (blocksize < 4096) {
2298 fprintf(stderr, "block size is too small\n");
2301 if (!(ext2_fs->super->s_feature_incompat &
2302 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2303 fprintf(stderr, "filetype feature is missing\n");
2306 for (i = 0; i < 7; i++) {
2307 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2309 fprintf(stderr, "not enough free space\n");
2312 blocks[i] *= blocksize;
2314 super_bytenr = blocks[0];
2315 fd = open(devname, O_RDWR);
2317 fprintf(stderr, "unable to open %s\n", devname);
2320 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2321 blocks, total_bytes, blocksize, blocksize,
2322 blocksize, blocksize);
2324 fprintf(stderr, "unable to create initial ctree\n");
2327 /* create a system chunk that maps the whole device */
2328 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2330 fprintf(stderr, "unable to update system chunk\n");
2333 root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
2335 fprintf(stderr, "unable to open ctree\n");
2338 ret = cache_free_extents(root, ext2_fs);
2340 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2343 root->fs_info->extent_ops = &extent_ops;
2344 /* recover block allocation bitmap */
2345 for (i = 0; i < 7; i++) {
2346 blocks[i] /= blocksize;
2347 ext2_free_block(ext2_fs, blocks[i]);
2349 ret = init_btrfs(root);
2351 fprintf(stderr, "unable to setup the root tree\n");
2354 printf("creating btrfs metadata.\n");
2355 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2357 fprintf(stderr, "error during copy_inodes %d\n", ret);
2360 printf("creating ext2fs image file.\n");
2361 ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
2363 fprintf(stderr, "unable to create subvol\n");
2366 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2368 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2371 printf("cleaning up system chunk.\n");
2372 ret = cleanup_sys_chunk(root, ext2_root);
2374 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2377 ret = close_ctree(root);
2379 fprintf(stderr, "error during close_ctree %d\n", ret);
2382 close_ext2fs(ext2_fs);
2385 * If this step succeed, we get a mountable btrfs. Otherwise
2386 * the ext2fs is left unchanged.
2388 ret = migrate_super_block(fd, super_bytenr, blocksize);
2390 fprintf(stderr, "unable to migrate super block\n");
2394 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2396 fprintf(stderr, "unable to open ctree\n");
2399 /* move chunk tree into system chunk. */
2400 ret = fixup_chunk_mapping(root);
2402 fprintf(stderr, "error during fixup_chunk_tree\n");
2405 ret = close_ctree(root);
2408 printf("conversion complete.\n");
2413 fprintf(stderr, "conversion aborted.\n");
2417 static int may_rollback(struct btrfs_root *root)
2419 struct btrfs_fs_info *info = root->fs_info;
2420 struct btrfs_multi_bio *multi = NULL;
2428 if (btrfs_super_num_devices(&info->super_copy) != 1)
2431 bytenr = BTRFS_SUPER_INFO_OFFSET;
2432 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2435 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2436 &length, &multi, 0, NULL);
2440 num_stripes = multi->num_stripes;
2441 physical = multi->stripes[0].physical;
2444 if (num_stripes != 1 || physical != bytenr)
2448 if (bytenr >= total_bytes)
2456 int do_rollback(const char *devname, int force)
2461 struct btrfs_root *root;
2462 struct btrfs_root *ext2_root;
2463 struct btrfs_root *chunk_root;
2464 struct btrfs_dir_item *dir;
2465 struct btrfs_inode_item *inode;
2466 struct btrfs_file_extent_item *fi;
2467 struct btrfs_trans_handle *trans;
2468 struct extent_buffer *leaf;
2469 struct btrfs_block_group_cache *cache1;
2470 struct btrfs_block_group_cache *cache2;
2471 struct btrfs_key key;
2472 struct btrfs_path path;
2473 struct extent_io_tree io_tree;
2488 extent_io_tree_init(&io_tree);
2490 fd = open(devname, O_RDWR);
2492 fprintf(stderr, "unable to open %s\n", devname);
2495 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2497 fprintf(stderr, "unable to open ctree\n");
2500 ret = may_rollback(root);
2502 fprintf(stderr, "unable to do rollback\n");
2506 sectorsize = root->sectorsize;
2507 buf = malloc(sectorsize);
2509 fprintf(stderr, "unable to allocate memory\n");
2513 btrfs_init_path(&path);
2515 key.objectid = EXT2_IMAGE_SUBVOL_OBJECTID;
2516 key.type = BTRFS_ROOT_ITEM_KEY;
2517 key.offset = (u64)-1;
2518 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2519 if (!ext2_root || IS_ERR(ext2_root)) {
2520 fprintf(stderr, "unable to open subvol %llu\n",
2526 root_dir = btrfs_root_dirid(&root->root_item);
2527 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2528 root_dir, name, strlen(name), 0);
2529 if (!dir || IS_ERR(dir)) {
2530 fprintf(stderr, "unable to find file %s\n", name);
2533 leaf = path.nodes[0];
2534 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2535 btrfs_release_path(ext2_root, &path);
2537 objectid = key.objectid;
2539 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2541 fprintf(stderr, "unable to find inode item\n");
2544 leaf = path.nodes[0];
2545 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2546 total_bytes = btrfs_inode_size(leaf, inode);
2547 btrfs_release_path(ext2_root, &path);
2549 key.objectid = objectid;
2551 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2552 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2554 fprintf(stderr, "unable to find first file extent\n");
2555 btrfs_release_path(ext2_root, &path);
2559 /* build mapping tree for the relocated blocks */
2560 for (offset = 0; offset < total_bytes; ) {
2561 leaf = path.nodes[0];
2562 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2563 ret = btrfs_next_leaf(root, &path);
2569 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2570 if (key.objectid != objectid || key.offset != offset ||
2571 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2574 fi = btrfs_item_ptr(leaf, path.slots[0],
2575 struct btrfs_file_extent_item);
2576 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2578 if (btrfs_file_extent_compression(leaf, fi) ||
2579 btrfs_file_extent_encryption(leaf, fi) ||
2580 btrfs_file_extent_other_encoding(leaf, fi))
2583 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2584 /* skip holes and direct mapped extents */
2585 if (bytenr == 0 || bytenr == offset)
2588 bytenr += btrfs_file_extent_offset(leaf, fi);
2589 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2591 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2592 cache2 = btrfs_lookup_block_group(root->fs_info,
2593 offset + num_bytes - 1);
2594 if (!cache1 || cache1 != cache2 ||
2595 (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
2596 !intersect_with_sb(offset, num_bytes)))
2599 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2600 EXTENT_LOCKED, GFP_NOFS);
2601 set_state_private(&io_tree, offset, bytenr);
2603 offset += btrfs_file_extent_num_bytes(leaf, fi);
2606 btrfs_release_path(ext2_root, &path);
2608 if (offset < total_bytes) {
2609 fprintf(stderr, "unable to build extent mapping\n");
2613 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2614 first_free &= ~((u64)sectorsize - 1);
2615 /* backup for extent #0 should exist */
2616 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2617 fprintf(stderr, "no backup for the first extent\n");
2620 /* force no allocation from system block group */
2621 root->fs_info->system_allocs = -1;
2622 trans = btrfs_start_transaction(root, 1);
2625 * recow the whole chunk tree, this will remove all chunk tree blocks
2626 * from system block group
2628 chunk_root = root->fs_info->chunk_root;
2629 memset(&key, 0, sizeof(key));
2631 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2635 ret = btrfs_next_leaf(chunk_root, &path);
2639 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2640 btrfs_release_path(chunk_root, &path);
2642 btrfs_release_path(chunk_root, &path);
2647 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2651 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2652 num_bytes += btrfs_block_group_used(&cache1->item);
2654 offset = cache1->key.objectid + cache1->key.offset;
2656 /* only extent #0 left in system block group? */
2657 if (num_bytes > first_free) {
2658 fprintf(stderr, "unable to empty system block group\n");
2661 /* create a system chunk that maps the whole device */
2662 ret = prepare_system_chunk_sb(&root->fs_info->super_copy);
2664 fprintf(stderr, "unable to update system chunk\n");
2668 ret = btrfs_commit_transaction(trans, root);
2671 ret = close_ctree(root);
2673 fprintf(stderr, "error during close_ctree %d\n", ret);
2677 /* zero btrfs super block mirrors */
2678 memset(buf, 0, sectorsize);
2679 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2680 bytenr = btrfs_sb_offset(i);
2681 if (bytenr >= total_bytes)
2683 ret = pwrite(fd, buf, sectorsize, bytenr);
2686 sb_bytenr = (u64)-1;
2687 /* copy all relocated blocks back */
2689 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2694 ret = get_state_private(&io_tree, start, &bytenr);
2697 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2700 while (start <= end) {
2701 if (start == BTRFS_SUPER_INFO_OFFSET) {
2705 ret = pread(fd, buf, sectorsize, bytenr);
2707 fprintf(stderr, "error during pread %d\n", ret);
2710 BUG_ON(ret != sectorsize);
2711 ret = pwrite(fd, buf, sectorsize, start);
2713 fprintf(stderr, "error during pwrite %d\n", ret);
2716 BUG_ON(ret != sectorsize);
2718 start += sectorsize;
2719 bytenr += sectorsize;
2725 fprintf(stderr, "error during fsync %d\n", ret);
2729 * finally, overwrite btrfs super block.
2731 ret = pread(fd, buf, sectorsize, sb_bytenr);
2733 fprintf(stderr, "error during pread %d\n", ret);
2736 BUG_ON(ret != sectorsize);
2737 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2739 fprintf(stderr, "error during pwrite %d\n", ret);
2742 BUG_ON(ret != sectorsize);
2745 fprintf(stderr, "error during fsync %d\n", ret);
2751 extent_io_tree_cleanup(&io_tree);
2752 printf("rollback complete.\n");
2755 fprintf(stderr, "rollback aborted.\n");
2759 static void print_usage(void)
2761 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2762 printf("\t-d disable data checksum\n");
2763 printf("\t-i ignore xattrs and ACLs\n");
2764 printf("\t-n disable packing of small files\n");
2765 printf("\t-r roll back to ext2fs\n");
2768 int main(int argc, char *argv[])
2777 int c = getopt(argc, argv, "dinr");
2798 argc = argc - optind;
2804 file = argv[optind];
2805 if (check_mounted(file)) {
2806 fprintf(stderr, "%s is mounted\n", file);
2811 ret = do_rollback(file, 0);
2813 ret = do_convert(file, datacsum, packing, noxattr);