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>
44 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
46 * Open Ext2fs in readonly mode, read block allocation bitmap and
47 * inode bitmap into memory.
49 static int open_ext2fs(const char *name
, ext2_filsys
*ret_fs
)
53 ret
= ext2fs_open(name
, 0, 0, 0, unix_io_manager
, &ext2_fs
);
55 fprintf(stderr
, "ext2fs_open: %s\n", error_message(ret
));
58 ret
= ext2fs_read_inode_bitmap(ext2_fs
);
60 fprintf(stderr
, "ext2fs_read_inode_bitmap: %s\n",
64 ret
= ext2fs_read_block_bitmap(ext2_fs
);
66 fprintf(stderr
, "ext2fs_read_block_bitmap: %s\n",
76 static int close_ext2fs(ext2_filsys fs
)
82 static int ext2_alloc_block(ext2_filsys fs
, u64 goal
, u64
*block_ret
)
86 if (!ext2fs_new_block(fs
, goal
, NULL
, &block
)) {
87 ext2fs_fast_mark_block_bitmap(fs
->block_map
, block
);
94 static int ext2_free_block(ext2_filsys fs
, u64 block
)
96 BUG_ON(block
!= (blk_t
)block
);
97 ext2fs_fast_unmark_block_bitmap(fs
->block_map
, block
);
101 static int cache_free_extents(struct btrfs_root
*root
, ext2_filsys ext2_fs
)
107 u64 blocksize
= ext2_fs
->blocksize
;
109 block
= ext2_fs
->super
->s_first_data_block
;
110 for (; block
< ext2_fs
->super
->s_blocks_count
; block
++) {
111 if (ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
113 bytenr
= block
* blocksize
;
114 ret
= set_extent_dirty(&root
->fs_info
->free_space_cache
,
115 bytenr
, bytenr
+ blocksize
- 1, 0);
122 static int custom_alloc_extent(struct btrfs_root
*root
, u64 num_bytes
,
123 u64 hint_byte
, struct btrfs_key
*ins
)
127 u64 last
= hint_byte
;
130 struct btrfs_block_group_cache
*cache
;
133 ret
= find_first_extent_bit(&root
->fs_info
->free_space_cache
,
134 last
, &start
, &end
, EXTENT_DIRTY
);
136 if (wrapped
++ == 0) {
144 start
= max(last
, start
);
146 if (last
- start
< num_bytes
)
149 last
= start
+ num_bytes
;
150 if (test_range_bit(&root
->fs_info
->pinned_extents
,
151 start
, last
- 1, EXTENT_DIRTY
, 0))
154 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
156 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
||
157 last
> cache
->key
.objectid
+ cache
->key
.offset
) {
158 last
= cache
->key
.objectid
+ cache
->key
.offset
;
162 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
163 start
, start
+ num_bytes
- 1, 0);
165 ins
->objectid
= start
;
166 ins
->offset
= num_bytes
;
167 ins
->type
= BTRFS_EXTENT_ITEM_KEY
;
171 fprintf(stderr
, "not enough free space\n");
175 static int custom_free_extent(struct btrfs_root
*root
, u64 bytenr
,
181 struct btrfs_extent_ops extent_ops
= {
182 .alloc_extent
= custom_alloc_extent
,
183 .free_extent
= custom_free_extent
,
186 struct dir_iterate_data
{
187 struct btrfs_trans_handle
*trans
;
188 struct btrfs_root
*root
;
189 struct btrfs_inode_item
*inode
;
195 static u8 filetype_conversion_table
[EXT2_FT_MAX
] = {
196 [EXT2_FT_UNKNOWN
] = BTRFS_FT_UNKNOWN
,
197 [EXT2_FT_REG_FILE
] = BTRFS_FT_REG_FILE
,
198 [EXT2_FT_DIR
] = BTRFS_FT_DIR
,
199 [EXT2_FT_CHRDEV
] = BTRFS_FT_CHRDEV
,
200 [EXT2_FT_BLKDEV
] = BTRFS_FT_BLKDEV
,
201 [EXT2_FT_FIFO
] = BTRFS_FT_FIFO
,
202 [EXT2_FT_SOCK
] = BTRFS_FT_SOCK
,
203 [EXT2_FT_SYMLINK
] = BTRFS_FT_SYMLINK
,
206 static int dir_iterate_proc(ext2_ino_t dir
, int entry
,
207 struct ext2_dir_entry
*old
,
208 int offset
, int blocksize
,
209 char *buf
,void *priv_data
)
215 char dotdot
[] = "..";
216 struct btrfs_key location
;
217 struct ext2_dir_entry_2
*dirent
= (struct ext2_dir_entry_2
*)old
;
218 struct dir_iterate_data
*idata
= (struct dir_iterate_data
*)priv_data
;
220 objectid
= dirent
->inode
+ INO_OFFSET
;
221 if (!strncmp(dirent
->name
, dotdot
, dirent
->name_len
)) {
222 if (dirent
->name_len
== 2) {
223 BUG_ON(idata
->parent
!= 0);
224 idata
->parent
= objectid
;
228 if (dirent
->inode
< EXT2_GOOD_OLD_FIRST_INO
)
231 location
.objectid
= objectid
;
233 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
235 file_type
= dirent
->file_type
;
236 BUG_ON(file_type
> EXT2_FT_SYMLINK
);
237 ret
= btrfs_insert_dir_item(idata
->trans
, idata
->root
,
238 dirent
->name
, dirent
->name_len
,
239 idata
->objectid
, &location
,
240 filetype_conversion_table
[file_type
]);
243 ret
= btrfs_insert_inode_ref(idata
->trans
, idata
->root
,
244 dirent
->name
, dirent
->name_len
,
245 objectid
, idata
->objectid
);
248 inode_size
= btrfs_stack_inode_size(idata
->inode
) +
249 dirent
->name_len
* 2;
250 btrfs_set_stack_inode_size(idata
->inode
, inode_size
);
253 idata
->errcode
= ret
;
257 static int create_dir_entries(struct btrfs_trans_handle
*trans
,
258 struct btrfs_root
*root
, u64 objectid
,
259 struct btrfs_inode_item
*btrfs_inode
,
260 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
264 struct dir_iterate_data data
= {
267 .inode
= btrfs_inode
,
268 .objectid
= objectid
,
273 err
= ext2fs_dir_iterate2(ext2_fs
, ext2_ino
, 0, NULL
,
274 dir_iterate_proc
, &data
);
278 if (ret
== 0 && data
.parent
== objectid
) {
279 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2,
284 fprintf(stderr
, "ext2fs_dir_iterate2: %s\n", error_message(err
));
288 static int read_disk_extent(struct btrfs_root
*root
, u64 bytenr
,
289 u32 num_bytes
, char *buffer
)
292 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
294 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, bytenr
);
295 if (ret
!= num_bytes
)
304 * Record a file extent. Do all the required works, such as inserting
305 * file extent item, inserting extent item and backref item into extent
306 * tree and updating block accounting.
308 static int record_file_extent(struct btrfs_trans_handle
*trans
,
309 struct btrfs_root
*root
, u64 objectid
,
310 struct btrfs_inode_item
*inode
,
311 u64 file_pos
, u64 disk_bytenr
,
312 u64 num_bytes
, int checksum
)
315 struct btrfs_fs_info
*info
= root
->fs_info
;
316 struct btrfs_root
*extent_root
= info
->extent_root
;
317 struct btrfs_key ins_key
;
318 struct btrfs_path path
;
319 struct btrfs_extent_item extent_item
;
320 u32 blocksize
= root
->sectorsize
;
324 ret
= btrfs_insert_file_extent(trans
, root
, objectid
, file_pos
,
325 disk_bytenr
, num_bytes
, num_bytes
);
326 if (ret
|| disk_bytenr
== 0)
329 nblocks
= btrfs_stack_inode_nblocks(inode
) + num_bytes
/ 512;
330 btrfs_set_stack_inode_nblocks(inode
, nblocks
);
336 buffer
= malloc(blocksize
);
339 for (offset
= 0; offset
< num_bytes
; offset
+= blocksize
) {
340 ret
= read_disk_extent(root
, disk_bytenr
+ offset
,
344 ret
= btrfs_csum_file_block(trans
, root
, inode
,
345 objectid
, file_pos
+ offset
,
355 bytes_used
= btrfs_root_used(&root
->root_item
);
356 btrfs_set_root_used(&root
->root_item
, bytes_used
+ num_bytes
);
357 ins_key
.objectid
= disk_bytenr
;
358 ins_key
.offset
= num_bytes
;
359 btrfs_set_key_type(&ins_key
, BTRFS_EXTENT_ITEM_KEY
);
360 btrfs_set_stack_extent_refs(&extent_item
, 1);
361 ret
= btrfs_insert_item(trans
, extent_root
, &ins_key
,
362 &extent_item
, sizeof(extent_item
));
364 bytes_used
= btrfs_super_bytes_used(&info
->super_copy
);
365 btrfs_set_super_bytes_used(&info
->super_copy
, bytes_used
+
367 btrfs_init_path(&path
);
368 ret
= btrfs_insert_extent_backref(trans
, extent_root
, &path
,
369 disk_bytenr
, root
->root_key
.objectid
,
370 trans
->transid
, objectid
, file_pos
);
373 ret
= btrfs_update_block_group(trans
, root
, disk_bytenr
,
375 } else if (ret
== -EEXIST
) {
376 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
, num_bytes
,
377 root
->root_key
.objectid
,
378 trans
->transid
, objectid
, file_pos
);
382 btrfs_extent_post_op(trans
, extent_root
);
388 static int record_file_blocks(struct btrfs_trans_handle
*trans
,
389 struct btrfs_root
*root
, u64 objectid
,
390 struct btrfs_inode_item
*inode
,
391 u64 file_block
, u64 disk_block
,
392 u64 num_blocks
, int checksum
)
394 u64 file_pos
= file_block
* root
->sectorsize
;
395 u64 disk_bytenr
= disk_block
* root
->sectorsize
;
396 u64 num_bytes
= num_blocks
* root
->sectorsize
;
397 return record_file_extent(trans
, root
, objectid
, inode
, file_pos
,
398 disk_bytenr
, num_bytes
, checksum
);
401 struct blk_iterate_data
{
402 struct btrfs_trans_handle
*trans
;
403 struct btrfs_root
*root
;
404 struct btrfs_inode_item
*inode
;
414 static int block_iterate_proc(ext2_filsys ext2_fs
,
415 u64 disk_block
, u64 file_block
,
416 struct blk_iterate_data
*idata
)
419 struct btrfs_root
*root
= idata
->root
;
420 struct btrfs_trans_handle
*trans
= idata
->trans
;
421 struct btrfs_block_group_cache
*cache
;
424 BUG_ON(disk_block
== 0);
425 if ((idata
->num_blocks
> 0 && disk_block
>= idata
->boundary
) ||
426 (file_block
> idata
->first_block
+ idata
->num_blocks
) ||
427 (disk_block
!= idata
->disk_block
+ idata
->num_blocks
)) {
428 if (idata
->num_blocks
> 0) {
429 ret
= record_file_blocks(trans
, root
, idata
->objectid
,
430 idata
->inode
, idata
->first_block
,
431 idata
->disk_block
, idata
->num_blocks
,
435 idata
->first_block
+= idata
->num_blocks
;
436 idata
->num_blocks
= 0;
438 if (file_block
> idata
->first_block
) {
439 ret
= record_file_blocks(trans
, root
, idata
->objectid
,
440 idata
->inode
, idata
->first_block
,
441 0, file_block
- idata
->first_block
,
447 bytenr
= disk_block
* root
->sectorsize
;
448 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
450 bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
452 idata
->first_block
= file_block
;
453 idata
->disk_block
= disk_block
;
454 idata
->boundary
= bytenr
/ root
->sectorsize
;
459 idata
->errcode
= ret
;
463 static int __block_iterate_proc(ext2_filsys fs
, blk_t
*blocknr
,
464 e2_blkcnt_t blockcnt
, blk_t ref_block
,
465 int ref_offset
, void *priv_data
)
467 struct blk_iterate_data
*idata
;
468 idata
= (struct blk_iterate_data
*)priv_data
;
469 return block_iterate_proc(fs
, *blocknr
, blockcnt
, idata
);
473 * traverse file's data blocks, record these data blocks as file extents.
475 static int create_file_extents(struct btrfs_trans_handle
*trans
,
476 struct btrfs_root
*root
, u64 objectid
,
477 struct btrfs_inode_item
*btrfs_inode
,
478 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
479 int datacsum
, int packing
)
485 u32 sectorsize
= root
->sectorsize
;
486 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
487 struct blk_iterate_data data
= {
490 .inode
= btrfs_inode
,
491 .objectid
= objectid
,
496 .checksum
= datacsum
,
499 err
= ext2fs_block_iterate2(ext2_fs
, ext2_ino
, BLOCK_FLAG_DATA_ONLY
,
500 NULL
, __block_iterate_proc
, &data
);
506 if (packing
&& data
.first_block
== 0 && data
.num_blocks
> 0 &&
507 inode_size
<= BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
508 u64 num_bytes
= data
.num_blocks
* sectorsize
;
509 u64 disk_bytenr
= data
.disk_block
* sectorsize
;
511 buffer
= malloc(num_bytes
);
514 ret
= read_disk_extent(root
, disk_bytenr
, num_bytes
, buffer
);
517 if (num_bytes
> inode_size
)
518 num_bytes
= inode_size
;
519 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
,
520 0, buffer
, num_bytes
);
523 } else if (data
.num_blocks
> 0) {
524 ret
= record_file_blocks(trans
, root
, objectid
, btrfs_inode
,
525 data
.first_block
, data
.disk_block
,
526 data
.num_blocks
, data
.checksum
);
530 data
.first_block
+= data
.num_blocks
;
531 last_block
= (inode_size
+ sectorsize
- 1) / sectorsize
;
532 if (last_block
> data
.first_block
) {
533 ret
= record_file_blocks(trans
, root
, objectid
, btrfs_inode
,
534 data
.first_block
, 0, last_block
-
535 data
.first_block
, data
.checksum
);
542 fprintf(stderr
, "ext2fs_block_iterate2: %s\n", error_message(err
));
546 static int create_symbol_link(struct btrfs_trans_handle
*trans
,
547 struct btrfs_root
*root
, u64 objectid
,
548 struct btrfs_inode_item
*btrfs_inode
,
549 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
550 struct ext2_inode
*ext2_inode
)
554 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
555 if (ext2fs_inode_data_blocks(ext2_fs
, ext2_inode
)) {
556 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
+ 1);
557 ret
= create_file_extents(trans
, root
, objectid
, btrfs_inode
,
558 ext2_fs
, ext2_ino
, 1, 1);
559 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
);
563 pathname
= (char *)&(ext2_inode
->i_block
[0]);
564 BUG_ON(pathname
[inode_size
] != 0);
565 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
, 0,
566 pathname
, inode_size
+ 1);
571 * Following xattr/acl related codes are based on codes in
572 * fs/ext3/xattr.c and fs/ext3/acl.c
574 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
575 #define EXT2_XATTR_BFIRST(ptr) \
576 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
577 #define EXT2_XATTR_IHDR(inode) \
578 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
579 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
580 #define EXT2_XATTR_IFIRST(inode) \
581 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
582 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
584 static int ext2_xattr_check_names(struct ext2_ext_attr_entry
*entry
,
587 struct ext2_ext_attr_entry
*next
;
589 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
590 next
= EXT2_EXT_ATTR_NEXT(entry
);
591 if ((void *)next
>= end
)
598 static int ext2_xattr_check_block(const char *buf
, size_t size
)
601 struct ext2_ext_attr_header
*header
= EXT2_XATTR_BHDR(buf
);
603 if (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC
||
604 header
->h_blocks
!= 1)
606 error
= ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf
), buf
+ size
);
610 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry
*entry
,
613 size_t value_size
= entry
->e_value_size
;
615 if (entry
->e_value_block
!= 0 || value_size
> size
||
616 entry
->e_value_offs
+ value_size
> size
)
621 #define EXT2_ACL_VERSION 0x0001
632 } ext2_acl_entry_short
;
638 static inline int ext2_acl_count(size_t size
)
641 size
-= sizeof(ext2_acl_header
);
642 s
= size
- 4 * sizeof(ext2_acl_entry_short
);
644 if (size
% sizeof(ext2_acl_entry_short
))
646 return size
/ sizeof(ext2_acl_entry_short
);
648 if (s
% sizeof(ext2_acl_entry
))
650 return s
/ sizeof(ext2_acl_entry
) + 4;
654 #define ACL_EA_VERSION 0x0002
664 acl_ea_entry a_entries
[0];
667 static inline size_t acl_ea_size(int count
)
669 return sizeof(acl_ea_header
) + count
* sizeof(acl_ea_entry
);
672 static int ext2_acl_to_xattr(void *dst
, const void *src
,
673 size_t dst_size
, size_t src_size
)
676 const void *end
= src
+ src_size
;
677 acl_ea_header
*ext_acl
= (acl_ea_header
*)dst
;
678 acl_ea_entry
*dst_entry
= ext_acl
->a_entries
;
679 ext2_acl_entry
*src_entry
;
681 if (src_size
< sizeof(ext2_acl_header
))
683 if (((ext2_acl_header
*)src
)->a_version
!=
684 cpu_to_le32(EXT2_ACL_VERSION
))
686 src
+= sizeof(ext2_acl_header
);
687 count
= ext2_acl_count(src_size
);
691 BUG_ON(dst_size
< acl_ea_size(count
));
692 ext_acl
->a_version
= cpu_to_le32(ACL_EA_VERSION
);
693 for (i
= 0; i
< count
; i
++, dst_entry
++) {
694 src_entry
= (ext2_acl_entry
*)src
;
695 if (src
+ sizeof(ext2_acl_entry_short
) > end
)
697 dst_entry
->e_tag
= src_entry
->e_tag
;
698 dst_entry
->e_perm
= src_entry
->e_perm
;
699 switch (le16_to_cpu(src_entry
->e_tag
)) {
704 src
+= sizeof(ext2_acl_entry_short
);
705 dst_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
709 src
+= sizeof(ext2_acl_entry
);
712 dst_entry
->e_id
= src_entry
->e_id
;
725 static char *xattr_prefix_table
[] = {
727 [2] = "system.posix_acl_access",
728 [3] = "system.posix_acl_default",
733 static int copy_single_xattr(struct btrfs_trans_handle
*trans
,
734 struct btrfs_root
*root
, u64 objectid
,
735 struct ext2_ext_attr_entry
*entry
,
736 const void *data
, u32 datalen
)
741 void *databuf
= NULL
;
742 char namebuf
[XATTR_NAME_MAX
+ 1];
744 name_index
= entry
->e_name_index
;
745 if (name_index
>= ARRAY_SIZE(xattr_prefix_table
) ||
746 xattr_prefix_table
[name_index
] == NULL
)
748 name_len
= strlen(xattr_prefix_table
[name_index
]) +
750 if (name_len
>= sizeof(namebuf
))
753 if (name_index
== 2 || name_index
== 3) {
754 size_t bufsize
= acl_ea_size(ext2_acl_count(datalen
));
755 databuf
= malloc(bufsize
);
758 ret
= ext2_acl_to_xattr(databuf
, data
, bufsize
, datalen
);
764 strcpy(namebuf
, xattr_prefix_table
[name_index
]);
765 strncat(namebuf
, EXT2_EXT_ATTR_NAME(entry
), entry
->e_name_len
);
766 if (name_len
+ datalen
> BTRFS_LEAF_DATA_SIZE(root
) -
767 sizeof(struct btrfs_item
) - sizeof(struct btrfs_dir_item
)) {
768 fprintf(stderr
, "skip large xattr on inode %Lu name %.*s\n",
769 objectid
- INO_OFFSET
, name_len
, namebuf
);
772 ret
= btrfs_insert_xattr_item(trans
, root
, namebuf
, name_len
,
773 data
, datalen
, objectid
);
780 static int copy_extended_attrs(struct btrfs_trans_handle
*trans
,
781 struct btrfs_root
*root
, u64 objectid
,
782 struct btrfs_inode_item
*btrfs_inode
,
783 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
789 u32 block_size
= ext2_fs
->blocksize
;
790 u32 inode_size
= EXT2_INODE_SIZE(ext2_fs
->super
);
791 struct ext2_inode_large
*ext2_inode
;
792 struct ext2_ext_attr_entry
*entry
;
795 char inode_buf
[EXT2_GOOD_OLD_INODE_SIZE
];
797 if (inode_size
<= EXT2_GOOD_OLD_INODE_SIZE
) {
798 ext2_inode
= (struct ext2_inode_large
*)inode_buf
;
800 ext2_inode
= (struct ext2_inode_large
*)malloc(inode_size
);
804 err
= ext2fs_read_inode_full(ext2_fs
, ext2_ino
, (void *)ext2_inode
,
807 fprintf(stderr
, "ext2fs_read_inode_full: %s\n",
813 if (ext2_ino
> ext2_fs
->super
->s_first_ino
&&
814 inode_size
> EXT2_GOOD_OLD_INODE_SIZE
) {
815 if (EXT2_GOOD_OLD_INODE_SIZE
+
816 ext2_inode
->i_extra_isize
> inode_size
) {
820 if (ext2_inode
->i_extra_isize
!= 0 &&
821 EXT2_XATTR_IHDR(ext2_inode
)->h_magic
==
822 EXT2_EXT_ATTR_MAGIC
) {
828 void *end
= (void *)ext2_inode
+ inode_size
;
829 entry
= EXT2_XATTR_IFIRST(ext2_inode
);
830 total
= end
- (void *)entry
;
831 ret
= ext2_xattr_check_names(entry
, end
);
834 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
835 ret
= ext2_xattr_check_entry(entry
, total
);
838 data
= (void *)EXT2_XATTR_IFIRST(ext2_inode
) +
840 datalen
= entry
->e_value_size
;
841 ret
= copy_single_xattr(trans
, root
, objectid
,
842 entry
, data
, datalen
);
845 entry
= EXT2_EXT_ATTR_NEXT(entry
);
849 if (ext2_inode
->i_file_acl
== 0)
852 buffer
= malloc(block_size
);
857 err
= ext2fs_read_ext_attr(ext2_fs
, ext2_inode
->i_file_acl
, buffer
);
859 fprintf(stderr
, "ext2fs_read_ext_attr: %s\n",
864 ret
= ext2_xattr_check_block(buffer
, block_size
);
868 entry
= EXT2_XATTR_BFIRST(buffer
);
869 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
870 ret
= ext2_xattr_check_entry(entry
, block_size
);
873 data
= buffer
+ entry
->e_value_offs
;
874 datalen
= entry
->e_value_size
;
875 ret
= copy_single_xattr(trans
, root
, objectid
,
876 entry
, data
, datalen
);
879 entry
= EXT2_EXT_ATTR_NEXT(entry
);
884 if ((void *)ext2_inode
!= inode_buf
)
889 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
891 static inline dev_t
old_decode_dev(u16 val
)
893 return MKDEV((val
>> 8) & 255, val
& 255);
896 static inline dev_t
new_decode_dev(u32 dev
)
898 unsigned major
= (dev
& 0xfff00) >> 8;
899 unsigned minor
= (dev
& 0xff) | ((dev
>> 12) & 0xfff00);
900 return MKDEV(major
, minor
);
903 static int copy_inode_item(struct btrfs_inode_item
*dst
,
904 struct ext2_inode
*src
)
906 btrfs_set_stack_inode_generation(dst
, 1);
907 btrfs_set_stack_inode_size(dst
, src
->i_size
);
908 btrfs_set_stack_inode_nblocks(dst
, src
->i_blocks
);
909 btrfs_set_stack_inode_block_group(dst
, 0);
910 btrfs_set_stack_inode_nblocks(dst
, 0);
911 btrfs_set_stack_inode_nlink(dst
, src
->i_links_count
);
912 btrfs_set_stack_inode_uid(dst
, src
->i_uid
| (src
->i_uid_high
<< 16));
913 btrfs_set_stack_inode_gid(dst
, src
->i_gid
| (src
->i_gid_high
<< 16));
914 btrfs_set_stack_inode_mode(dst
, src
->i_mode
);
915 btrfs_set_stack_inode_rdev(dst
, 0);
916 btrfs_set_stack_inode_flags(dst
, 0);
917 btrfs_set_stack_inode_compat_flags(dst
, 0);
918 btrfs_set_stack_timespec_sec(&dst
->atime
, src
->i_atime
);
919 btrfs_set_stack_timespec_nsec(&dst
->atime
, 0);
920 btrfs_set_stack_timespec_sec(&dst
->ctime
, src
->i_ctime
);
921 btrfs_set_stack_timespec_nsec(&dst
->ctime
, 0);
922 btrfs_set_stack_timespec_sec(&dst
->mtime
, src
->i_mtime
);
923 btrfs_set_stack_timespec_nsec(&dst
->mtime
, 0);
924 btrfs_set_stack_timespec_sec(&dst
->otime
, 0);
925 btrfs_set_stack_timespec_nsec(&dst
->otime
, 0);
927 if (S_ISDIR(src
->i_mode
)) {
928 btrfs_set_stack_inode_size(dst
, 0);
929 btrfs_set_stack_inode_nlink(dst
, 1);
931 if (S_ISREG(src
->i_mode
)) {
932 btrfs_set_stack_inode_size(dst
, (u64
)src
->i_size_high
<< 32 |
935 if (!S_ISREG(src
->i_mode
) && !S_ISDIR(src
->i_mode
) &&
936 !S_ISLNK(src
->i_mode
)) {
937 if (src
->i_block
[0]) {
938 btrfs_set_stack_inode_rdev(dst
,
939 old_decode_dev(src
->i_block
[0]));
941 btrfs_set_stack_inode_rdev(dst
,
942 new_decode_dev(src
->i_block
[1]));
949 * copy a single inode. do all the required works, such as cloning
950 * inode item, creating file extents and creating directory entries.
952 static int copy_single_inode(struct btrfs_trans_handle
*trans
,
953 struct btrfs_root
*root
, u64 objectid
,
954 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
955 int datacsum
, int packing
, int noxattr
)
959 struct ext2_inode ext2_inode
;
960 struct btrfs_key inode_key
;
961 struct btrfs_inode_item btrfs_inode
;
963 err
= ext2fs_read_inode(ext2_fs
, ext2_ino
, &ext2_inode
);
967 if (!ext2_inode
.i_links_count
&&
968 (!ext2_inode
.i_mode
|| ext2_inode
.i_dtime
)) {
969 printf("skip inode %u\n", ext2_ino
);
972 copy_inode_item(&btrfs_inode
, &ext2_inode
);
973 if (!datacsum
&& S_ISREG(ext2_inode
.i_mode
)) {
974 u32 flags
= btrfs_stack_inode_flags(&btrfs_inode
) |
975 BTRFS_INODE_NODATASUM
;
976 btrfs_set_stack_inode_flags(&btrfs_inode
, flags
);
979 switch (ext2_inode
.i_mode
& S_IFMT
) {
981 ret
= create_file_extents(trans
, root
, objectid
, &btrfs_inode
,
982 ext2_fs
, ext2_ino
, datacsum
, packing
);
985 ret
= create_dir_entries(trans
, root
, objectid
, &btrfs_inode
,
989 ret
= create_symbol_link(trans
, root
, objectid
, &btrfs_inode
,
990 ext2_fs
, ext2_ino
, &ext2_inode
);
1000 ret
= copy_extended_attrs(trans
, root
, objectid
, &btrfs_inode
,
1005 inode_key
.objectid
= objectid
;
1006 inode_key
.offset
= 0;
1007 btrfs_set_key_type(&inode_key
, BTRFS_INODE_ITEM_KEY
);
1008 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1011 fprintf(stderr
, "ext2fs_read_inode: %s\n", error_message(err
));
1015 static int copy_disk_extent(struct btrfs_root
*root
, u64 dst_bytenr
,
1016 u64 src_bytenr
, u32 num_bytes
)
1020 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
1022 buffer
= malloc(num_bytes
);
1025 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, src_bytenr
);
1026 if (ret
!= num_bytes
)
1028 ret
= pwrite(fs_devs
->latest_bdev
, buffer
, num_bytes
, dst_bytenr
);
1029 if (ret
!= num_bytes
)
1039 * scan ext2's inode bitmap and copy all used inode.
1041 static int copy_inodes(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1042 int datacsum
, int packing
, int noxattr
)
1045 ext2_ino_t ext2_ino
;
1047 struct btrfs_trans_handle
*trans
;
1049 trans
= btrfs_start_transaction(root
, 1);
1052 ext2_ino
= ext2_fs
->inode_map
->start
;
1053 for (; ext2_ino
<= ext2_fs
->inode_map
->end
; ext2_ino
++) {
1054 if (ext2fs_fast_test_inode_bitmap(ext2_fs
->inode_map
,
1056 /* skip special inode in ext2fs */
1057 if (ext2_ino
< EXT2_GOOD_OLD_FIRST_INO
&&
1058 ext2_ino
!= EXT2_ROOT_INO
)
1060 objectid
= ext2_ino
+ INO_OFFSET
;
1061 ret
= copy_single_inode(trans
, root
,
1062 objectid
, ext2_fs
, ext2_ino
,
1063 datacsum
, packing
, noxattr
);
1067 if (trans
->blocks_used
>= 4096) {
1068 ret
= btrfs_commit_transaction(trans
, root
);
1070 trans
= btrfs_start_transaction(root
, 1);
1074 ret
= btrfs_commit_transaction(trans
, root
);
1081 * Construct a range of ext2fs image file.
1082 * scan block allocation bitmap, find all blocks used by the ext2fs
1083 * in this range and create file extents that point to these blocks.
1085 * Note: Before calling the function, no file extent points to blocks
1088 static int create_image_file_range(struct btrfs_trans_handle
*trans
,
1089 struct btrfs_root
*root
, u64 objectid
,
1090 struct btrfs_inode_item
*inode
,
1091 u64 start_byte
, u64 end_byte
,
1092 ext2_filsys ext2_fs
)
1094 u32 blocksize
= ext2_fs
->blocksize
;
1095 u32 block
= start_byte
/ blocksize
;
1096 u32 last_block
= (end_byte
+ blocksize
- 1) / blocksize
;
1098 struct blk_iterate_data data
= {
1102 .objectid
= objectid
,
1103 .first_block
= block
,
1106 .boundary
= (u64
)-1,
1110 for (; start_byte
< end_byte
; block
++, start_byte
+= blocksize
) {
1111 if (!ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
1113 ret
= block_iterate_proc(NULL
, block
, block
, &data
);
1114 if (ret
& BLOCK_ABORT
) {
1119 if (data
.num_blocks
> 0) {
1120 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1121 data
.first_block
, data
.disk_block
,
1122 data
.num_blocks
, 0);
1125 data
.first_block
+= data
.num_blocks
;
1127 if (last_block
> data
.first_block
) {
1128 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1129 data
.first_block
, 0, last_block
-
1130 data
.first_block
, 0);
1138 * Create the ext2fs image file.
1140 static int create_ext2_image(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1144 struct btrfs_key key
;
1145 struct btrfs_key location
;
1146 struct btrfs_path path
;
1147 struct btrfs_inode_item btrfs_inode
;
1148 struct btrfs_inode_item
*inode_item
;
1149 struct extent_buffer
*leaf
;
1150 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1151 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
1152 struct btrfs_trans_handle
*trans
;
1153 struct btrfs_extent_ref
*ref_item
;
1162 u32 sectorsize
= root
->sectorsize
;
1165 total_bytes
= btrfs_super_total_bytes(&fs_info
->super_copy
);
1166 first_free
= BTRFS_SUPER_INFO_OFFSET
+ sectorsize
* 2 - 1;
1167 first_free
&= ~((u64
)sectorsize
- 1);
1169 memset(&btrfs_inode
, 0, sizeof(btrfs_inode
));
1170 btrfs_set_stack_inode_generation(&btrfs_inode
, 1);
1171 btrfs_set_stack_inode_size(&btrfs_inode
, total_bytes
);
1172 btrfs_set_stack_inode_nlink(&btrfs_inode
, 1);
1173 btrfs_set_stack_inode_nblocks(&btrfs_inode
, 0);
1174 btrfs_set_stack_inode_mode(&btrfs_inode
, S_IFREG
| 0400);
1175 btrfs_set_stack_inode_flags(&btrfs_inode
, BTRFS_INODE_NODATASUM
|
1176 BTRFS_INODE_READONLY
);
1177 btrfs_init_path(&path
);
1178 trans
= btrfs_start_transaction(root
, 1);
1181 objectid
= btrfs_root_dirid(&root
->root_item
);
1182 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1187 * copy blocks covered by extent #0 to new positions. extent #0 is
1188 * special, we can't rely on relocate_extents_range to relocate it.
1190 for (last_byte
= 0; last_byte
< first_free
; last_byte
+= sectorsize
) {
1191 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1194 ret
= copy_disk_extent(root
, key
.objectid
, last_byte
,
1198 ret
= record_file_extent(trans
, root
, objectid
,
1199 &btrfs_inode
, last_byte
,
1200 key
.objectid
, sectorsize
, 0);
1206 key
.objectid
= last_byte
;
1208 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1209 ret
= btrfs_search_slot(trans
, fs_info
->extent_root
,
1214 leaf
= path
.nodes
[0];
1215 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1216 ret
= btrfs_next_leaf(extent_root
, &path
);
1221 leaf
= path
.nodes
[0];
1223 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1224 if (last_byte
> key
.objectid
||
1225 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1230 * Check backref to distinguish extent items for normal
1231 * files (files that correspond to files in Ext2fs) from
1232 * extent items for ctree blocks.
1234 bytenr
= key
.objectid
;
1235 num_bytes
= key
.offset
;
1238 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1239 ret
= btrfs_next_leaf(extent_root
, &path
);
1244 leaf
= path
.nodes
[0];
1246 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1247 if (key
.objectid
!= bytenr
)
1249 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1253 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1254 struct btrfs_extent_ref
);
1255 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1256 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1257 if ((ref_root
== BTRFS_FS_TREE_OBJECTID
) &&
1258 (ref_owner
>= BTRFS_FIRST_FREE_OBJECTID
)) {
1267 if (bytenr
> last_byte
) {
1268 ret
= create_image_file_range(trans
, root
, objectid
,
1269 &btrfs_inode
, last_byte
,
1274 ret
= record_file_extent(trans
, root
, objectid
, &btrfs_inode
,
1275 bytenr
, bytenr
, num_bytes
, 0);
1278 last_byte
= bytenr
+ num_bytes
;
1279 btrfs_release_path(extent_root
, &path
);
1281 if (trans
->blocks_used
>= 4096) {
1282 ret
= btrfs_commit_transaction(trans
, root
);
1284 trans
= btrfs_start_transaction(root
, 1);
1288 btrfs_release_path(root
, &path
);
1289 if (total_bytes
> last_byte
) {
1290 ret
= create_image_file_range(trans
, root
, objectid
,
1291 &btrfs_inode
, last_byte
,
1292 total_bytes
, ext2_fs
);
1297 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1301 location
.objectid
= objectid
;
1302 location
.offset
= 0;
1303 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1304 ret
= btrfs_insert_dir_item(trans
, root
, name
, strlen(name
),
1305 btrfs_root_dirid(&root
->root_item
),
1306 &location
, EXT2_FT_REG_FILE
);
1309 ret
= btrfs_insert_inode_ref(trans
, root
, name
, strlen(name
),
1311 btrfs_root_dirid(&root
->root_item
));
1314 location
.objectid
= btrfs_root_dirid(&root
->root_item
);
1315 location
.offset
= 0;
1316 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1317 ret
= btrfs_lookup_inode(trans
, root
, &path
, &location
, 1);
1320 leaf
= path
.nodes
[0];
1321 inode_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1322 struct btrfs_inode_item
);
1323 btrfs_set_inode_size(leaf
, inode_item
, strlen(name
) * 2 +
1324 btrfs_inode_size(leaf
, inode_item
));
1325 btrfs_mark_buffer_dirty(leaf
);
1326 btrfs_release_path(root
, &path
);
1327 ret
= btrfs_commit_transaction(trans
, root
);
1330 btrfs_release_path(root
, &path
);
1334 struct btrfs_root
*create_subvol(struct btrfs_root
*root
, const char *name
)
1338 struct btrfs_key location
;
1339 struct btrfs_root_item root_item
;
1340 struct btrfs_trans_handle
*trans
;
1341 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1342 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1343 struct btrfs_root
*new_root
;
1344 struct extent_buffer
*tmp
;
1346 trans
= btrfs_start_transaction(root
, 1);
1349 objectid
= btrfs_super_root_dir(&fs_info
->super_copy
);
1350 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1353 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
1356 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
1357 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
1358 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
1359 free_extent_buffer(tmp
);
1361 location
.objectid
= objectid
;
1362 location
.offset
= 1;
1363 btrfs_set_key_type(&location
, BTRFS_ROOT_ITEM_KEY
);
1364 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1365 &location
, &root_item
);
1368 location
.offset
= (u64
)-1;
1369 ret
= btrfs_insert_dir_item(trans
, tree_root
, name
, strlen(name
),
1370 btrfs_super_root_dir(&fs_info
->super_copy
),
1371 &location
, BTRFS_FT_DIR
);
1374 ret
= btrfs_insert_inode_ref(trans
, tree_root
, name
, strlen(name
),
1376 btrfs_super_root_dir(&fs_info
->super_copy
));
1379 ret
= btrfs_commit_transaction(trans
, root
);
1381 new_root
= btrfs_read_fs_root(fs_info
, &location
);
1382 if (!new_root
|| IS_ERR(new_root
))
1384 trans
= btrfs_start_transaction(new_root
, 1);
1386 ret
= btrfs_make_root_dir(trans
, new_root
, BTRFS_FIRST_FREE_OBJECTID
);
1389 ret
= btrfs_commit_transaction(trans
, new_root
);
1396 * Fixup block accounting. The initial block accounting created by
1397 * make_block_groups isn't accuracy in this case.
1399 static int fixup_block_accounting(struct btrfs_trans_handle
*trans
,
1400 struct btrfs_root
*root
)
1406 struct btrfs_path path
;
1407 struct btrfs_key key
;
1408 struct extent_buffer
*leaf
;
1409 struct btrfs_block_group_cache
*cache
;
1410 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1413 cache
= btrfs_lookup_block_group(fs_info
, start
);
1416 start
= cache
->key
.objectid
+ cache
->key
.offset
;
1417 btrfs_set_block_group_used(&cache
->item
, 0);
1418 cache
->space_info
->bytes_used
= 0;
1421 btrfs_init_path(&path
);
1424 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1425 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
1430 leaf
= path
.nodes
[0];
1431 slot
= path
.slots
[0];
1432 if (slot
>= btrfs_header_nritems(leaf
)) {
1433 ret
= btrfs_next_leaf(root
, &path
);
1438 leaf
= path
.nodes
[0];
1439 slot
= path
.slots
[0];
1441 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1442 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
1443 bytes_used
+= key
.offset
;
1444 ret
= btrfs_update_block_group(trans
, root
,
1445 key
.objectid
, key
.offset
, 1, 0);
1450 btrfs_set_super_bytes_used(&root
->fs_info
->super_copy
, bytes_used
);
1451 btrfs_release_path(root
, &path
);
1455 static int create_chunk_mapping(struct btrfs_trans_handle
*trans
,
1456 struct btrfs_root
*root
)
1458 struct btrfs_fs_info
*info
= root
->fs_info
;
1459 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1460 struct btrfs_root
*extent_root
= info
->extent_root
;
1461 struct btrfs_device
*device
;
1462 struct btrfs_block_group_cache
*cache
;
1463 struct btrfs_dev_extent
*extent
;
1464 struct extent_buffer
*leaf
;
1465 struct btrfs_chunk chunk
;
1466 struct btrfs_key key
;
1467 struct btrfs_path path
;
1473 btrfs_init_path(&path
);
1475 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1476 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1478 BUG_ON(list_empty(&info
->fs_devices
->devices
));
1479 device
= list_entry(info
->fs_devices
->devices
.next
,
1480 struct btrfs_device
, dev_list
);
1481 BUG_ON(device
->devid
!= info
->fs_devices
->latest_devid
);
1483 /* delete device extent created by make_btrfs */
1484 key
.objectid
= device
->devid
;
1486 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1487 ret
= btrfs_search_slot(trans
, device
->dev_root
, &key
, &path
, -1, 1);
1492 ret
= btrfs_del_item(trans
, device
->dev_root
, &path
);
1495 btrfs_release_path(device
->dev_root
, &path
);
1497 /* delete chunk item created by make_btrfs */
1498 key
.objectid
= chunk_objectid
;
1500 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1501 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, -1, 1);
1506 ret
= btrfs_del_item(trans
, chunk_root
, &path
);
1509 btrfs_release_path(chunk_root
, &path
);
1511 /* for each block group, create device extent and chunk item */
1513 while (cur_start
< total_bytes
) {
1514 cache
= btrfs_lookup_block_group(root
->fs_info
, cur_start
);
1517 /* insert device extent */
1518 key
.objectid
= device
->devid
;
1519 key
.offset
= cache
->key
.objectid
;
1520 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1521 ret
= btrfs_insert_empty_item(trans
, device
->dev_root
, &path
,
1522 &key
, sizeof(*extent
));
1526 leaf
= path
.nodes
[0];
1527 extent
= btrfs_item_ptr(leaf
, path
.slots
[0],
1528 struct btrfs_dev_extent
);
1530 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1531 chunk_root
->root_key
.objectid
);
1532 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1534 btrfs_set_dev_extent_chunk_offset(leaf
, extent
,
1535 cache
->key
.objectid
);
1536 btrfs_set_dev_extent_length(leaf
, extent
, cache
->key
.offset
);
1537 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1538 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1540 btrfs_mark_buffer_dirty(leaf
);
1541 btrfs_release_path(device
->dev_root
, &path
);
1543 /* insert chunk item */
1544 btrfs_set_stack_chunk_length(&chunk
, cache
->key
.offset
);
1545 btrfs_set_stack_chunk_owner(&chunk
,
1546 extent_root
->root_key
.objectid
);
1547 btrfs_set_stack_chunk_stripe_len(&chunk
, 64 * 1024);
1548 btrfs_set_stack_chunk_type(&chunk
, cache
->flags
);
1549 btrfs_set_stack_chunk_io_align(&chunk
, device
->io_align
);
1550 btrfs_set_stack_chunk_io_width(&chunk
, device
->io_width
);
1551 btrfs_set_stack_chunk_sector_size(&chunk
, device
->sector_size
);
1552 btrfs_set_stack_chunk_num_stripes(&chunk
, 1);
1553 btrfs_set_stack_chunk_sub_stripes(&chunk
, 0);
1554 btrfs_set_stack_stripe_devid(&chunk
.stripe
, device
->devid
);
1555 btrfs_set_stack_stripe_offset(&chunk
.stripe
,
1556 cache
->key
.objectid
);
1557 memcpy(&chunk
.stripe
.dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1559 key
.objectid
= chunk_objectid
;
1560 key
.offset
= cache
->key
.objectid
;
1561 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1563 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, &chunk
,
1564 btrfs_chunk_item_size(1));
1568 cur_start
= cache
->key
.objectid
+ cache
->key
.offset
;
1571 device
->bytes_used
= total_bytes
;
1572 ret
= btrfs_update_device(trans
, device
);
1574 btrfs_release_path(device
->dev_root
, &path
);
1578 static int init_btrfs(struct btrfs_root
*root
)
1581 struct btrfs_key location
;
1582 struct btrfs_trans_handle
*trans
;
1583 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1585 trans
= btrfs_start_transaction(root
, 1);
1587 ret
= btrfs_make_block_groups(trans
, root
);
1590 ret
= fixup_block_accounting(trans
, root
);
1593 ret
= create_chunk_mapping(trans
, root
);
1596 ret
= btrfs_make_root_dir(trans
, fs_info
->tree_root
,
1597 BTRFS_ROOT_TREE_DIR_OBJECTID
);
1600 memcpy(&location
, &root
->root_key
, sizeof(location
));
1601 location
.offset
= (u64
)-1;
1602 ret
= btrfs_insert_dir_item(trans
, fs_info
->tree_root
, "default", 7,
1603 btrfs_super_root_dir(&fs_info
->super_copy
),
1604 &location
, BTRFS_FT_DIR
);
1607 ret
= btrfs_insert_inode_ref(trans
, fs_info
->tree_root
, "default", 7,
1609 btrfs_super_root_dir(&fs_info
->super_copy
));
1612 btrfs_set_root_dirid(&fs_info
->fs_root
->root_item
,
1613 BTRFS_FIRST_FREE_OBJECTID
);
1614 ret
= btrfs_commit_transaction(trans
, root
);
1621 * Migrate super block to it's default position and zero 0 ~ 16k
1623 static int migrate_super_block(int fd
, u64 old_bytenr
, u32 sectorsize
)
1626 struct extent_buffer
*buf
;
1627 struct btrfs_super_block
*super
;
1631 BUG_ON(sectorsize
< sizeof(*super
));
1632 buf
= malloc(sizeof(*buf
) + sectorsize
);
1636 buf
->len
= sectorsize
;
1637 ret
= pread(fd
, buf
->data
, sectorsize
, old_bytenr
);
1638 if (ret
!= sectorsize
)
1641 super
= (struct btrfs_super_block
*)buf
->data
;
1642 BUG_ON(btrfs_super_bytenr(super
) != old_bytenr
);
1643 btrfs_set_super_bytenr(super
, BTRFS_SUPER_INFO_OFFSET
);
1645 csum_tree_block(NULL
, buf
, 0);
1646 ret
= pwrite(fd
, buf
->data
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
1647 if (ret
!= sectorsize
)
1654 memset(buf
->data
, 0, sectorsize
);
1655 for (bytenr
= 0; bytenr
< BTRFS_SUPER_INFO_OFFSET
; ) {
1656 len
= BTRFS_SUPER_INFO_OFFSET
- bytenr
;
1657 if (len
> sectorsize
)
1659 ret
= pwrite(fd
, buf
->data
, len
, bytenr
);
1661 fprintf(stderr
, "unable to zero fill device\n");
1675 static int prepare_system_chunk_sb(struct btrfs_super_block
*super
)
1677 struct btrfs_chunk
*chunk
;
1678 struct btrfs_disk_key
*key
;
1679 u32 sectorsize
= btrfs_super_sectorsize(super
);
1681 key
= (struct btrfs_disk_key
*)(super
->sys_chunk_array
);
1682 chunk
= (struct btrfs_chunk
*)(super
->sys_chunk_array
+
1683 sizeof(struct btrfs_disk_key
));
1685 btrfs_set_disk_key_objectid(key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1686 btrfs_set_disk_key_type(key
, BTRFS_CHUNK_ITEM_KEY
);
1687 btrfs_set_disk_key_offset(key
, 0);
1689 btrfs_set_stack_chunk_length(chunk
, btrfs_super_total_bytes(super
));
1690 btrfs_set_stack_chunk_owner(chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
1691 btrfs_set_stack_chunk_stripe_len(chunk
, 64 * 1024);
1692 btrfs_set_stack_chunk_type(chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
1693 btrfs_set_stack_chunk_io_align(chunk
, sectorsize
);
1694 btrfs_set_stack_chunk_io_width(chunk
, sectorsize
);
1695 btrfs_set_stack_chunk_sector_size(chunk
, sectorsize
);
1696 btrfs_set_stack_chunk_num_stripes(chunk
, 1);
1697 btrfs_set_stack_chunk_sub_stripes(chunk
, 0);
1698 chunk
->stripe
.devid
= super
->dev_item
.devid
;
1699 chunk
->stripe
.offset
= cpu_to_le64(0);
1700 memcpy(chunk
->stripe
.dev_uuid
, super
->dev_item
.uuid
, BTRFS_UUID_SIZE
);
1701 btrfs_set_super_sys_array_size(super
, sizeof(*key
) + sizeof(*chunk
));
1705 static int prepare_system_chunk(int fd
, u64 sb_bytenr
, u32 sectorsize
)
1708 struct extent_buffer
*buf
;
1709 struct btrfs_super_block
*super
;
1711 BUG_ON(sectorsize
< sizeof(*super
));
1712 buf
= malloc(sizeof(*buf
) + sectorsize
);
1716 buf
->len
= sectorsize
;
1717 ret
= pread(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1718 if (ret
!= sectorsize
)
1721 super
= (struct btrfs_super_block
*)buf
->data
;
1722 BUG_ON(btrfs_super_bytenr(super
) != sb_bytenr
);
1723 BUG_ON(btrfs_super_num_devices(super
) != 1);
1725 ret
= prepare_system_chunk_sb(super
);
1729 csum_tree_block(NULL
, buf
, 0);
1730 ret
= pwrite(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1731 if (ret
!= sectorsize
)
1742 static int relocate_one_reference(struct btrfs_trans_handle
*trans
,
1743 struct btrfs_root
*root
,
1744 u64 extent_start
, u64 extent_size
,
1745 u64 objectid
, u64 offset
,
1746 struct extent_io_tree
*reloc_tree
)
1748 struct extent_buffer
*leaf
;
1749 struct btrfs_file_extent_item
*fi
;
1750 struct btrfs_key key
;
1751 struct btrfs_path path
;
1752 struct btrfs_inode_item inode
;
1753 struct blk_iterate_data data
;
1762 u32 sectorsize
= root
->sectorsize
;
1767 btrfs_init_path(&path
);
1768 ret
= btrfs_lookup_file_extent(trans
, root
, &path
,
1769 objectid
, offset
, -1);
1773 leaf
= path
.nodes
[0];
1774 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
1775 struct btrfs_file_extent_item
);
1776 /* the extent may be referenced by old snapshot */
1777 if (extent_start
!= btrfs_file_extent_disk_bytenr(leaf
, fi
) ||
1778 extent_size
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1783 root_gen
= btrfs_header_generation(leaf
);
1784 root_owner
= btrfs_header_owner(leaf
);
1785 bytenr
= extent_start
+ btrfs_file_extent_offset(leaf
, fi
);
1786 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
1788 ret
= btrfs_del_item(trans
, root
, &path
);
1791 btrfs_release_path(root
, &path
);
1793 key
.objectid
= objectid
;
1795 key
.type
= BTRFS_INODE_ITEM_KEY
;
1796 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 0);
1800 leaf
= path
.nodes
[0];
1801 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1802 read_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1803 btrfs_release_path(root
, &path
);
1805 BUG_ON(num_bytes
& (sectorsize
- 1));
1806 nblocks
= btrfs_stack_inode_nblocks(&inode
) - num_bytes
/ 512;
1807 btrfs_set_stack_inode_nblocks(&inode
, nblocks
);
1809 data
= (struct blk_iterate_data
) {
1813 .objectid
= objectid
,
1814 .first_block
= offset
/ sectorsize
,
1817 .boundary
= (u64
)-1,
1822 cur_offset
= offset
;
1823 while (num_bytes
> 0) {
1824 sector_end
= bytenr
+ sectorsize
- 1;
1825 if (test_range_bit(reloc_tree
, bytenr
, sector_end
,
1826 EXTENT_LOCKED
, 1)) {
1827 ret
= get_state_private(reloc_tree
, bytenr
, &new_pos
);
1830 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1833 new_pos
= key
.objectid
;
1835 if (cur_offset
== offset
) {
1836 fd
= root
->fs_info
->fs_devices
->latest_bdev
;
1837 readahead(fd
, bytenr
, num_bytes
);
1839 ret
= copy_disk_extent(root
, new_pos
, bytenr
,
1843 ret
= set_extent_bits(reloc_tree
, bytenr
, sector_end
,
1844 EXTENT_LOCKED
, GFP_NOFS
);
1846 ret
= set_state_private(reloc_tree
, bytenr
, new_pos
);
1850 ret
= block_iterate_proc(NULL
, new_pos
/ sectorsize
,
1851 cur_offset
/ sectorsize
, &data
);
1852 if (ret
& BLOCK_ABORT
) {
1857 cur_offset
+= sectorsize
;
1858 bytenr
+= sectorsize
;
1859 num_bytes
-= sectorsize
;
1862 if (data
.num_blocks
> 0) {
1863 ret
= record_file_blocks(trans
, root
, objectid
, &inode
,
1864 data
.first_block
, data
.disk_block
,
1865 data
.num_blocks
, 0);
1870 key
.objectid
= objectid
;
1872 key
.type
= BTRFS_INODE_ITEM_KEY
;
1873 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 1);
1877 leaf
= path
.nodes
[0];
1878 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1879 write_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1880 btrfs_mark_buffer_dirty(leaf
);
1881 btrfs_release_path(root
, &path
);
1883 ret
= btrfs_free_extent(trans
, root
, extent_start
, extent_size
,
1884 root_owner
, root_gen
, objectid
, offset
, 0);
1886 btrfs_release_path(root
, &path
);
1890 static int relocate_extents_range(struct btrfs_root
*fs_root
,
1891 struct btrfs_root
*ext2_root
,
1892 u64 start_byte
, u64 end_byte
)
1894 struct btrfs_fs_info
*info
= fs_root
->fs_info
;
1895 struct btrfs_root
*extent_root
= info
->extent_root
;
1896 struct btrfs_root
*cur_root
;
1897 struct btrfs_trans_handle
*trans
;
1898 struct btrfs_extent_ref
*ref_item
;
1899 struct extent_buffer
*leaf
;
1900 struct btrfs_key key
;
1901 struct btrfs_path path
;
1902 struct extent_io_tree reloc_tree
;
1913 btrfs_init_path(&path
);
1914 extent_io_tree_init(&reloc_tree
);
1916 cur_root
= (pass
% 2 == 0) ? ext2_root
: fs_root
;
1919 trans
= btrfs_start_transaction(cur_root
, 1);
1922 cur_byte
= start_byte
;
1924 key
.objectid
= cur_byte
;
1926 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1927 ret
= btrfs_search_slot(trans
, extent_root
,
1932 leaf
= path
.nodes
[0];
1933 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1934 ret
= btrfs_next_leaf(extent_root
, &path
);
1939 leaf
= path
.nodes
[0];
1942 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1943 if (key
.objectid
< cur_byte
||
1944 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1948 if (key
.objectid
>= end_byte
)
1951 cur_byte
= key
.objectid
;
1952 num_bytes
= key
.offset
;
1955 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1956 ret
= btrfs_next_leaf(extent_root
, &path
);
1961 leaf
= path
.nodes
[0];
1964 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1965 if (key
.objectid
!= cur_byte
)
1967 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1971 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1972 struct btrfs_extent_ref
);
1973 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1974 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1975 ref_offset
= btrfs_ref_offset(leaf
, ref_item
);
1978 BUG_ON(ref_owner
< BTRFS_FIRST_FREE_OBJECTID
);
1979 if (ref_root
== cur_root
->root_key
.objectid
) {
1988 ret
= relocate_one_reference(trans
, cur_root
, cur_byte
,
1989 num_bytes
, ref_owner
,
1990 ref_offset
, &reloc_tree
);
1994 cur_byte
+= num_bytes
;
1995 btrfs_release_path(extent_root
, &path
);
1997 if (trans
->blocks_used
>= 4096) {
1998 ret
= btrfs_commit_transaction(trans
, cur_root
);
2000 trans
= btrfs_start_transaction(cur_root
, 1);
2004 btrfs_release_path(cur_root
, &path
);
2006 ret
= btrfs_commit_transaction(trans
, cur_root
);
2009 if (num_refs
> 0 && pass
++ < 4)
2012 ret
= (num_refs
> 0) ? -1 : 0;
2014 btrfs_release_path(cur_root
, &path
);
2015 extent_io_tree_cleanup(&reloc_tree
);
2020 * relocate data in system chunk
2022 static int cleanup_sys_chunk(struct btrfs_root
*fs_root
,
2023 struct btrfs_root
*ext2_root
)
2025 struct btrfs_block_group_cache
*cache
;
2031 cache
= btrfs_lookup_block_group(fs_root
->fs_info
, offset
);
2035 end_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
2036 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2037 ret
= relocate_extents_range(fs_root
, ext2_root
,
2038 cache
->key
.objectid
,
2049 static int fixup_chunk_mapping(struct btrfs_root
*root
)
2051 struct btrfs_trans_handle
*trans
;
2052 struct btrfs_fs_info
*info
= root
->fs_info
;
2053 struct btrfs_root
*chunk_root
= info
->chunk_root
;
2054 struct extent_buffer
*leaf
;
2055 struct btrfs_key key
;
2056 struct btrfs_path path
;
2057 struct btrfs_chunk chunk
;
2063 btrfs_init_path(&path
);
2065 trans
= btrfs_start_transaction(root
, 1);
2069 * recow the whole chunk tree. this will move all chunk tree blocks
2070 * into system block group.
2072 memset(&key
, 0, sizeof(key
));
2074 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2078 ret
= btrfs_next_leaf(chunk_root
, &path
);
2084 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2085 btrfs_release_path(chunk_root
, &path
);
2087 btrfs_release_path(chunk_root
, &path
);
2089 /* fixup the system chunk array in super block */
2090 btrfs_set_super_sys_array_size(&info
->super_copy
, 0);
2092 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2094 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2096 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 0);
2101 leaf
= path
.nodes
[0];
2102 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2103 ret
= btrfs_next_leaf(chunk_root
, &path
);
2108 leaf
= path
.nodes
[0];
2110 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2111 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
)
2114 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
2115 size
= btrfs_item_size_nr(leaf
, path
.slots
[0]);
2116 BUG_ON(size
!= sizeof(chunk
));
2117 read_extent_buffer(leaf
, &chunk
, ptr
, size
);
2118 type
= btrfs_stack_chunk_type(&chunk
);
2120 if (!(type
& BTRFS_BLOCK_GROUP_SYSTEM
))
2123 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
2131 ret
= btrfs_commit_transaction(trans
, root
);
2134 btrfs_release_path(chunk_root
, &path
);
2138 int do_convert(const char *devname
, int datacsum
, int packing
, int noxattr
)
2145 ext2_filsys ext2_fs
;
2146 struct btrfs_root
*root
;
2147 struct btrfs_root
*ext2_root
;
2149 ret
= open_ext2fs(devname
, &ext2_fs
);
2151 fprintf(stderr
, "unable to open the Ext2fs\n");
2154 blocksize
= ext2_fs
->blocksize
;
2155 total_bytes
= (u64
)ext2_fs
->super
->s_blocks_count
* blocksize
;
2156 if (blocksize
< 4096) {
2157 fprintf(stderr
, "block size is too small\n");
2160 if (!(ext2_fs
->super
->s_feature_incompat
&
2161 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
2162 fprintf(stderr
, "filetype feature is missing\n");
2165 for (i
= 0; i
< 6; i
++) {
2166 ret
= ext2_alloc_block(ext2_fs
, 0, blocks
+ i
);
2168 fprintf(stderr
, "not enough free space\n");
2171 blocks
[i
] *= blocksize
;
2173 super_bytenr
= blocks
[0];
2174 fd
= open(devname
, O_RDWR
);
2176 fprintf(stderr
, "unable to open %s\n", devname
);
2179 ret
= make_btrfs(fd
, devname
, ext2_fs
->super
->s_volume_name
,
2180 blocks
, total_bytes
, blocksize
, blocksize
,
2181 blocksize
, blocksize
);
2183 fprintf(stderr
, "unable to create initial ctree\n");
2186 /* create a system chunk that maps the whole device */
2187 ret
= prepare_system_chunk(fd
, super_bytenr
, blocksize
);
2189 fprintf(stderr
, "unable to update system chunk\n");
2192 root
= open_ctree_fd(fd
, devname
, super_bytenr
, O_RDWR
);
2194 fprintf(stderr
, "unable to open ctree\n");
2197 ret
= cache_free_extents(root
, ext2_fs
);
2199 fprintf(stderr
, "error during cache_free_extents %d\n", ret
);
2202 root
->fs_info
->extent_ops
= &extent_ops
;
2203 /* recover block allocation bitmap */
2204 for (i
= 0; i
< 6; i
++) {
2205 blocks
[i
] /= blocksize
;
2206 ext2_free_block(ext2_fs
, blocks
[i
]);
2208 ret
= init_btrfs(root
);
2210 fprintf(stderr
, "unable to setup the root tree\n");
2213 ext2_root
= create_subvol(root
, "ext2_saved");
2215 fprintf(stderr
, "unable to create subvol\n");
2218 printf("creating btrfs metadata.\n");
2219 ret
= copy_inodes(root
, ext2_fs
, datacsum
, packing
, noxattr
);
2221 fprintf(stderr
, "error during copy_inodes %d\n", ret
);
2224 printf("creating ext2fs image file.\n");
2225 ret
= create_ext2_image(ext2_root
, ext2_fs
, "image");
2227 fprintf(stderr
, "error during create_ext2_image %d\n", ret
);
2230 printf("cleaning up system chunk.\n");
2231 ret
= cleanup_sys_chunk(root
, ext2_root
);
2233 fprintf(stderr
, "error during cleanup_sys_chunk %d\n", ret
);
2236 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2237 ret
= close_ctree(root
);
2239 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2242 close_ext2fs(ext2_fs
);
2245 * If this step succeed, we get a mountable btrfs. Otherwise
2246 * the ext2fs is left unchanged.
2248 ret
= migrate_super_block(fd
, super_bytenr
, blocksize
);
2250 fprintf(stderr
, "unable to migrate super block\n");
2254 root
= open_ctree_fd(fd
, devname
, 0, O_RDWR
);
2256 fprintf(stderr
, "unable to open ctree\n");
2259 /* move chunk tree into system chunk. */
2260 ret
= fixup_chunk_mapping(root
);
2262 fprintf(stderr
, "error during fixup_chunk_tree\n");
2265 ret
= close_ctree(root
);
2268 printf("conversion complete.\n");
2271 fprintf(stderr
, "conversion aborted.\n");
2275 static int may_rollback(struct btrfs_root
*root
)
2277 struct btrfs_fs_info
*info
= root
->fs_info
;
2278 struct btrfs_multi_bio
*multi
= NULL
;
2286 if (btrfs_super_num_devices(&info
->super_copy
) != 1)
2289 bytenr
= BTRFS_SUPER_INFO_OFFSET
;
2290 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2293 ret
= btrfs_map_block(&info
->mapping_tree
, WRITE
, bytenr
,
2294 &length
, &multi
, 0);
2298 num_stripes
= multi
->num_stripes
;
2299 physical
= multi
->stripes
[0].physical
;
2302 if (num_stripes
!= 1 || physical
!= bytenr
)
2306 if (bytenr
>= total_bytes
)
2314 int do_rollback(const char *devname
, int force
)
2318 struct btrfs_root
*root
;
2319 struct btrfs_root
*ext2_root
;
2320 struct btrfs_root
*chunk_root
;
2321 struct btrfs_dir_item
*dir
;
2322 struct btrfs_inode_item
*inode
;
2323 struct btrfs_file_extent_item
*fi
;
2324 struct btrfs_trans_handle
*trans
;
2325 struct extent_buffer
*leaf
;
2326 struct btrfs_block_group_cache
*cache1
;
2327 struct btrfs_block_group_cache
*cache2
;
2328 struct btrfs_key key
;
2329 struct btrfs_path path
;
2330 struct extent_io_tree io_tree
;
2345 extent_io_tree_init(&io_tree
);
2347 fd
= open(devname
, O_RDWR
);
2349 fprintf(stderr
, "unable to open %s\n", devname
);
2352 root
= open_ctree_fd(fd
, devname
, 0, O_RDWR
);
2354 fprintf(stderr
, "unable to open ctree\n");
2357 ret
= may_rollback(root
);
2359 fprintf(stderr
, "unable to do rollback\n");
2363 sectorsize
= root
->sectorsize
;
2364 buf
= malloc(sectorsize
);
2366 fprintf(stderr
, "unable to allocate memory\n");
2370 btrfs_init_path(&path
);
2371 name
= "ext2_saved";
2372 root_dir
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2373 dir
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, &path
,
2374 root_dir
, name
, strlen(name
), 0);
2375 if (!dir
|| IS_ERR(dir
)) {
2376 fprintf(stderr
, "unable to find subvol %s\n", name
);
2379 leaf
= path
.nodes
[0];
2380 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2381 btrfs_release_path(root
->fs_info
->tree_root
, &path
);
2383 ext2_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2384 if (!ext2_root
|| IS_ERR(ext2_root
)) {
2385 fprintf(stderr
, "unable to open subvol %s\n", name
);
2390 root_dir
= btrfs_root_dirid(&root
->root_item
);
2391 dir
= btrfs_lookup_dir_item(NULL
, ext2_root
, &path
,
2392 root_dir
, name
, strlen(name
), 0);
2393 if (!dir
|| IS_ERR(dir
)) {
2394 fprintf(stderr
, "unable to find file %s\n", name
);
2397 leaf
= path
.nodes
[0];
2398 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2399 btrfs_release_path(ext2_root
, &path
);
2401 objectid
= key
.objectid
;
2403 ret
= btrfs_lookup_inode(NULL
, ext2_root
, &path
, &key
, 0);
2405 fprintf(stderr
, "unable to find inode item\n");
2408 leaf
= path
.nodes
[0];
2409 inode
= btrfs_item_ptr(leaf
, path
.slots
[0], struct btrfs_inode_item
);
2410 total_bytes
= btrfs_inode_size(leaf
, inode
);
2411 btrfs_release_path(ext2_root
, &path
);
2413 key
.objectid
= objectid
;
2415 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2416 ret
= btrfs_search_slot(NULL
, ext2_root
, &key
, &path
, 0, 0);
2418 fprintf(stderr
, "unable to find first file extent\n");
2419 btrfs_release_path(ext2_root
, &path
);
2423 /* build mapping tree for the relocated blocks */
2424 for (offset
= 0; offset
< total_bytes
; ) {
2425 leaf
= path
.nodes
[0];
2426 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2427 ret
= btrfs_next_leaf(root
, &path
);
2433 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2434 if (key
.objectid
!= objectid
|| key
.offset
!= offset
||
2435 btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2438 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
2439 struct btrfs_file_extent_item
);
2440 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2443 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2444 /* skip holes and direct mapped extents */
2445 if (bytenr
== 0 || bytenr
== offset
)
2448 bytenr
+= btrfs_file_extent_offset(leaf
, fi
);
2449 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
2451 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2452 cache2
= btrfs_lookup_block_group(root
->fs_info
,
2453 offset
+ num_bytes
- 1);
2454 if (!cache1
|| cache1
!= cache2
||
2455 !(cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
))
2458 set_extent_bits(&io_tree
, offset
, offset
+ num_bytes
- 1,
2459 EXTENT_LOCKED
, GFP_NOFS
);
2460 set_state_private(&io_tree
, offset
, bytenr
);
2462 offset
+= btrfs_file_extent_num_bytes(leaf
, fi
);
2465 btrfs_release_path(ext2_root
, &path
);
2466 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2468 if (offset
< total_bytes
) {
2469 fprintf(stderr
, "unable to build extent mapping\n");
2473 first_free
= BTRFS_SUPER_INFO_OFFSET
+ 2 * sectorsize
- 1;
2474 first_free
&= ~((u64
)sectorsize
- 1);
2475 /* backup for extent #0 should exist */
2476 if(!test_range_bit(&io_tree
, 0, first_free
- 1, EXTENT_LOCKED
, 1)) {
2477 fprintf(stderr
, "no backup for the first extent\n");
2480 /* force no allocation from system block group */
2481 root
->fs_info
->system_allocs
= -1;
2482 trans
= btrfs_start_transaction(root
, 1);
2485 * recow the whole chunk tree, this will remove all chunk tree blocks
2486 * from system block group
2488 chunk_root
= root
->fs_info
->chunk_root
;
2489 memset(&key
, 0, sizeof(key
));
2491 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2495 ret
= btrfs_next_leaf(chunk_root
, &path
);
2499 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2500 btrfs_release_path(chunk_root
, &path
);
2502 btrfs_release_path(chunk_root
, &path
);
2507 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2511 if (cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2512 num_bytes
+= btrfs_block_group_used(&cache1
->item
);
2514 offset
= cache1
->key
.objectid
+ cache1
->key
.offset
;
2516 /* only extent #0 left in system block group? */
2517 if (num_bytes
> first_free
) {
2518 fprintf(stderr
, "unable to empty system block group\n");
2521 /* create a system chunk that maps the whole device */
2522 ret
= prepare_system_chunk_sb(&root
->fs_info
->super_copy
);
2524 fprintf(stderr
, "unable to update system chunk\n");
2528 ret
= btrfs_commit_transaction(trans
, root
);
2531 ret
= close_ctree(root
);
2533 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2537 sb_bytenr
= (u64
)-1;
2538 /* copy all relocated blocks back */
2540 ret
= find_first_extent_bit(&io_tree
, 0, &start
, &end
,
2545 ret
= get_state_private(&io_tree
, start
, &bytenr
);
2548 clear_extent_bits(&io_tree
, start
, end
, EXTENT_LOCKED
,
2551 while (start
<= end
) {
2552 if (start
== BTRFS_SUPER_INFO_OFFSET
) {
2556 ret
= pread(fd
, buf
, sectorsize
, bytenr
);
2558 fprintf(stderr
, "error during pread %d\n", ret
);
2561 BUG_ON(ret
!= sectorsize
);
2562 ret
= pwrite(fd
, buf
, sectorsize
, start
);
2564 fprintf(stderr
, "error during pwrite %d\n", ret
);
2567 BUG_ON(ret
!= sectorsize
);
2569 start
+= sectorsize
;
2570 bytenr
+= sectorsize
;
2576 fprintf(stderr
, "error during fsync %d\n", ret
);
2580 * finally, overwrite btrfs super block.
2582 ret
= pread(fd
, buf
, sectorsize
, sb_bytenr
);
2584 fprintf(stderr
, "error during pread %d\n", ret
);
2587 BUG_ON(ret
!= sectorsize
);
2588 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
2590 fprintf(stderr
, "error during pwrite %d\n", ret
);
2593 BUG_ON(ret
!= sectorsize
);
2596 fprintf(stderr
, "error during fsync %d\n", ret
);
2602 extent_io_tree_cleanup(&io_tree
);
2603 printf("rollback complete.\n");
2606 fprintf(stderr
, "rollback aborted.\n");
2610 static void print_usage(void)
2612 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2613 printf("\t-d disable data checksum\n");
2614 printf("\t-i ignore xattrs and ACLs\n");
2615 printf("\t-n disable packing of small files\n");
2616 printf("\t-r roll back to ext2fs\n");
2619 int main(int argc
, char *argv
[])
2628 int c
= getopt(argc
, argv
, "dinr");
2649 argc
= argc
- optind
;
2655 file
= argv
[optind
];
2656 if (check_mounted(file
)) {
2657 fprintf(stderr
, "%s is mounted\n", file
);
2662 ret
= do_rollback(file
, 0);
2664 ret
= do_convert(file
, datacsum
, packing
, noxattr
);