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
) && !S_ISDIR(src
->i_mode
) &&
932 !S_ISLNK(src
->i_mode
)) {
933 if (src
->i_block
[0]) {
934 btrfs_set_stack_inode_rdev(dst
,
935 old_decode_dev(src
->i_block
[0]));
937 btrfs_set_stack_inode_rdev(dst
,
938 new_decode_dev(src
->i_block
[1]));
945 * copy a single inode. do all the required works, such as cloning
946 * inode item, creating file extents and creating directory entries.
948 static int copy_single_inode(struct btrfs_trans_handle
*trans
,
949 struct btrfs_root
*root
, u64 objectid
,
950 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
951 int datacsum
, int packing
, int noxattr
)
955 struct ext2_inode ext2_inode
;
956 struct btrfs_key inode_key
;
957 struct btrfs_inode_item btrfs_inode
;
959 err
= ext2fs_read_inode(ext2_fs
, ext2_ino
, &ext2_inode
);
963 if (!ext2_inode
.i_links_count
&&
964 (!ext2_inode
.i_mode
|| ext2_inode
.i_dtime
)) {
965 printf("skip inode %u\n", ext2_ino
);
968 copy_inode_item(&btrfs_inode
, &ext2_inode
);
969 if (!datacsum
&& S_ISREG(ext2_inode
.i_mode
)) {
970 u32 flags
= btrfs_stack_inode_flags(&btrfs_inode
) |
971 BTRFS_INODE_NODATASUM
;
972 btrfs_set_stack_inode_flags(&btrfs_inode
, flags
);
975 switch (ext2_inode
.i_mode
& S_IFMT
) {
977 ret
= create_file_extents(trans
, root
, objectid
, &btrfs_inode
,
978 ext2_fs
, ext2_ino
, datacsum
, packing
);
981 ret
= create_dir_entries(trans
, root
, objectid
, &btrfs_inode
,
985 ret
= create_symbol_link(trans
, root
, objectid
, &btrfs_inode
,
986 ext2_fs
, ext2_ino
, &ext2_inode
);
996 ret
= copy_extended_attrs(trans
, root
, objectid
, &btrfs_inode
,
1001 inode_key
.objectid
= objectid
;
1002 inode_key
.offset
= 0;
1003 btrfs_set_key_type(&inode_key
, BTRFS_INODE_ITEM_KEY
);
1004 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1007 fprintf(stderr
, "ext2fs_read_inode: %s\n", error_message(err
));
1011 static int copy_disk_extent(struct btrfs_root
*root
, u64 dst_bytenr
,
1012 u64 src_bytenr
, u32 num_bytes
)
1016 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
1018 buffer
= malloc(num_bytes
);
1021 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, src_bytenr
);
1022 if (ret
!= num_bytes
)
1024 ret
= pwrite(fs_devs
->latest_bdev
, buffer
, num_bytes
, dst_bytenr
);
1025 if (ret
!= num_bytes
)
1035 * scan ext2's inode bitmap and copy all used inode.
1037 static int copy_inodes(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1038 int datacsum
, int packing
, int noxattr
)
1041 ext2_ino_t ext2_ino
;
1043 struct btrfs_trans_handle
*trans
;
1045 trans
= btrfs_start_transaction(root
, 1);
1048 ext2_ino
= ext2_fs
->inode_map
->start
;
1049 for (; ext2_ino
<= ext2_fs
->inode_map
->end
; ext2_ino
++) {
1050 if (ext2fs_fast_test_inode_bitmap(ext2_fs
->inode_map
,
1052 /* skip special inode in ext2fs */
1053 if (ext2_ino
< EXT2_GOOD_OLD_FIRST_INO
&&
1054 ext2_ino
!= EXT2_ROOT_INO
)
1056 objectid
= ext2_ino
+ INO_OFFSET
;
1057 ret
= copy_single_inode(trans
, root
,
1058 objectid
, ext2_fs
, ext2_ino
,
1059 datacsum
, packing
, noxattr
);
1063 if (trans
->blocks_used
>= 4096) {
1064 ret
= btrfs_commit_transaction(trans
, root
);
1066 trans
= btrfs_start_transaction(root
, 1);
1070 ret
= btrfs_commit_transaction(trans
, root
);
1077 * Construct a range of ext2fs image file.
1078 * scan block allocation bitmap, find all blocks used by the ext2fs
1079 * in this range and create file extents that point to these blocks.
1081 * Note: Before calling the function, no file extent points to blocks
1084 static int create_image_file_range(struct btrfs_trans_handle
*trans
,
1085 struct btrfs_root
*root
, u64 objectid
,
1086 struct btrfs_inode_item
*inode
,
1087 u64 start_byte
, u64 end_byte
,
1088 ext2_filsys ext2_fs
)
1090 u32 blocksize
= ext2_fs
->blocksize
;
1091 u32 block
= start_byte
/ blocksize
;
1092 u32 last_block
= (end_byte
+ blocksize
- 1) / blocksize
;
1094 struct blk_iterate_data data
= {
1098 .objectid
= objectid
,
1099 .first_block
= block
,
1102 .boundary
= (u64
)-1,
1106 for (; start_byte
< end_byte
; block
++, start_byte
+= blocksize
) {
1107 if (!ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
1109 ret
= block_iterate_proc(NULL
, block
, block
, &data
);
1110 if (ret
& BLOCK_ABORT
) {
1115 if (data
.num_blocks
> 0) {
1116 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1117 data
.first_block
, data
.disk_block
,
1118 data
.num_blocks
, 0);
1121 data
.first_block
+= data
.num_blocks
;
1123 if (last_block
> data
.first_block
) {
1124 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1125 data
.first_block
, 0, last_block
-
1126 data
.first_block
, 0);
1134 * Create the ext2fs image file.
1136 static int create_ext2_image(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1140 struct btrfs_key key
;
1141 struct btrfs_key location
;
1142 struct btrfs_path path
;
1143 struct btrfs_inode_item btrfs_inode
;
1144 struct btrfs_inode_item
*inode_item
;
1145 struct extent_buffer
*leaf
;
1146 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1147 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
1148 struct btrfs_trans_handle
*trans
;
1149 struct btrfs_extent_ref
*ref_item
;
1158 u32 sectorsize
= root
->sectorsize
;
1161 total_bytes
= btrfs_super_total_bytes(&fs_info
->super_copy
);
1162 first_free
= BTRFS_SUPER_INFO_OFFSET
+ sectorsize
* 2 - 1;
1163 first_free
&= ~((u64
)sectorsize
- 1);
1165 memset(&btrfs_inode
, 0, sizeof(btrfs_inode
));
1166 btrfs_set_stack_inode_generation(&btrfs_inode
, 1);
1167 btrfs_set_stack_inode_size(&btrfs_inode
, total_bytes
);
1168 btrfs_set_stack_inode_nlink(&btrfs_inode
, 1);
1169 btrfs_set_stack_inode_nblocks(&btrfs_inode
, 0);
1170 btrfs_set_stack_inode_mode(&btrfs_inode
, S_IFREG
| 0400);
1171 btrfs_set_stack_inode_flags(&btrfs_inode
, BTRFS_INODE_NODATASUM
|
1172 BTRFS_INODE_READONLY
);
1173 btrfs_init_path(&path
);
1174 trans
= btrfs_start_transaction(root
, 1);
1177 objectid
= btrfs_root_dirid(&root
->root_item
);
1178 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1183 * copy blocks covered by extent #0 to new positions. extent #0 is
1184 * special, we can't rely on relocate_extents_range to relocate it.
1186 for (last_byte
= 0; last_byte
< first_free
; last_byte
+= sectorsize
) {
1187 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1190 ret
= copy_disk_extent(root
, key
.objectid
, last_byte
,
1194 ret
= record_file_extent(trans
, root
, objectid
,
1195 &btrfs_inode
, last_byte
,
1196 key
.objectid
, sectorsize
, 0);
1202 key
.objectid
= last_byte
;
1204 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1205 ret
= btrfs_search_slot(trans
, fs_info
->extent_root
,
1210 leaf
= path
.nodes
[0];
1211 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1212 ret
= btrfs_next_leaf(extent_root
, &path
);
1217 leaf
= path
.nodes
[0];
1219 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1220 if (last_byte
> key
.objectid
||
1221 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1226 * Check backref to distinguish extent items for normal
1227 * files (files that correspond to files in Ext2fs) from
1228 * extent items for ctree blocks.
1230 bytenr
= key
.objectid
;
1231 num_bytes
= key
.offset
;
1234 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1235 ret
= btrfs_next_leaf(extent_root
, &path
);
1240 leaf
= path
.nodes
[0];
1242 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1243 if (key
.objectid
!= bytenr
)
1245 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1249 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1250 struct btrfs_extent_ref
);
1251 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1252 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1253 if ((ref_root
== BTRFS_FS_TREE_OBJECTID
) &&
1254 (ref_owner
>= BTRFS_FIRST_FREE_OBJECTID
)) {
1263 if (bytenr
> last_byte
) {
1264 ret
= create_image_file_range(trans
, root
, objectid
,
1265 &btrfs_inode
, last_byte
,
1270 ret
= record_file_extent(trans
, root
, objectid
, &btrfs_inode
,
1271 bytenr
, bytenr
, num_bytes
, 0);
1274 last_byte
= bytenr
+ num_bytes
;
1275 btrfs_release_path(extent_root
, &path
);
1277 if (trans
->blocks_used
>= 4096) {
1278 ret
= btrfs_commit_transaction(trans
, root
);
1280 trans
= btrfs_start_transaction(root
, 1);
1284 btrfs_release_path(root
, &path
);
1285 if (total_bytes
> last_byte
) {
1286 ret
= create_image_file_range(trans
, root
, objectid
,
1287 &btrfs_inode
, last_byte
,
1288 total_bytes
, ext2_fs
);
1293 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1297 location
.objectid
= objectid
;
1298 location
.offset
= 0;
1299 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1300 ret
= btrfs_insert_dir_item(trans
, root
, name
, strlen(name
),
1301 btrfs_root_dirid(&root
->root_item
),
1302 &location
, EXT2_FT_REG_FILE
);
1305 ret
= btrfs_insert_inode_ref(trans
, root
, name
, strlen(name
),
1307 btrfs_root_dirid(&root
->root_item
));
1310 location
.objectid
= btrfs_root_dirid(&root
->root_item
);
1311 location
.offset
= 0;
1312 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1313 ret
= btrfs_lookup_inode(trans
, root
, &path
, &location
, 1);
1316 leaf
= path
.nodes
[0];
1317 inode_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1318 struct btrfs_inode_item
);
1319 btrfs_set_inode_size(leaf
, inode_item
, strlen(name
) * 2 +
1320 btrfs_inode_size(leaf
, inode_item
));
1321 btrfs_mark_buffer_dirty(leaf
);
1322 btrfs_release_path(root
, &path
);
1323 ret
= btrfs_commit_transaction(trans
, root
);
1326 btrfs_release_path(root
, &path
);
1330 struct btrfs_root
*create_subvol(struct btrfs_root
*root
, const char *name
)
1334 struct btrfs_key location
;
1335 struct btrfs_root_item root_item
;
1336 struct btrfs_trans_handle
*trans
;
1337 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1338 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1339 struct btrfs_root
*new_root
;
1340 struct extent_buffer
*tmp
;
1342 trans
= btrfs_start_transaction(root
, 1);
1345 objectid
= btrfs_super_root_dir(&fs_info
->super_copy
);
1346 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1349 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
1352 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
1353 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
1354 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
1355 free_extent_buffer(tmp
);
1357 location
.objectid
= objectid
;
1358 location
.offset
= 1;
1359 btrfs_set_key_type(&location
, BTRFS_ROOT_ITEM_KEY
);
1360 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1361 &location
, &root_item
);
1364 location
.offset
= (u64
)-1;
1365 ret
= btrfs_insert_dir_item(trans
, tree_root
, name
, strlen(name
),
1366 btrfs_super_root_dir(&fs_info
->super_copy
),
1367 &location
, BTRFS_FT_DIR
);
1370 ret
= btrfs_insert_inode_ref(trans
, tree_root
, name
, strlen(name
),
1372 btrfs_super_root_dir(&fs_info
->super_copy
));
1375 ret
= btrfs_commit_transaction(trans
, root
);
1377 new_root
= btrfs_read_fs_root(fs_info
, &location
);
1378 if (!new_root
|| IS_ERR(new_root
))
1380 trans
= btrfs_start_transaction(new_root
, 1);
1382 ret
= btrfs_make_root_dir(trans
, new_root
, BTRFS_FIRST_FREE_OBJECTID
);
1385 ret
= btrfs_commit_transaction(trans
, new_root
);
1392 * Fixup block accounting. The initial block accounting created by
1393 * make_block_groups isn't accuracy in this case.
1395 static int fixup_block_accounting(struct btrfs_trans_handle
*trans
,
1396 struct btrfs_root
*root
)
1402 struct btrfs_path path
;
1403 struct btrfs_key key
;
1404 struct extent_buffer
*leaf
;
1405 struct btrfs_block_group_cache
*cache
;
1406 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1409 cache
= btrfs_lookup_block_group(fs_info
, start
);
1412 start
= cache
->key
.objectid
+ cache
->key
.offset
;
1413 btrfs_set_block_group_used(&cache
->item
, 0);
1414 cache
->space_info
->bytes_used
= 0;
1417 btrfs_init_path(&path
);
1420 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1421 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
1426 leaf
= path
.nodes
[0];
1427 slot
= path
.slots
[0];
1428 if (slot
>= btrfs_header_nritems(leaf
)) {
1429 ret
= btrfs_next_leaf(root
, &path
);
1434 leaf
= path
.nodes
[0];
1435 slot
= path
.slots
[0];
1437 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1438 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
1439 bytes_used
+= key
.offset
;
1440 ret
= btrfs_update_block_group(trans
, root
,
1441 key
.objectid
, key
.offset
, 1, 0);
1446 btrfs_set_super_bytes_used(&root
->fs_info
->super_copy
, bytes_used
);
1447 btrfs_release_path(root
, &path
);
1451 static int create_chunk_mapping(struct btrfs_trans_handle
*trans
,
1452 struct btrfs_root
*root
)
1454 struct btrfs_fs_info
*info
= root
->fs_info
;
1455 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1456 struct btrfs_root
*extent_root
= info
->extent_root
;
1457 struct btrfs_device
*device
;
1458 struct btrfs_block_group_cache
*cache
;
1459 struct btrfs_dev_extent
*extent
;
1460 struct extent_buffer
*leaf
;
1461 struct btrfs_chunk chunk
;
1462 struct btrfs_key key
;
1463 struct btrfs_path path
;
1469 btrfs_init_path(&path
);
1471 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1472 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1474 BUG_ON(list_empty(&info
->fs_devices
->devices
));
1475 device
= list_entry(info
->fs_devices
->devices
.next
,
1476 struct btrfs_device
, dev_list
);
1477 BUG_ON(device
->devid
!= info
->fs_devices
->latest_devid
);
1479 /* delete device extent created by make_btrfs */
1480 key
.objectid
= device
->devid
;
1482 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1483 ret
= btrfs_search_slot(trans
, device
->dev_root
, &key
, &path
, -1, 1);
1488 ret
= btrfs_del_item(trans
, device
->dev_root
, &path
);
1491 btrfs_release_path(device
->dev_root
, &path
);
1493 /* delete chunk item created by make_btrfs */
1494 key
.objectid
= chunk_objectid
;
1496 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1497 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, -1, 1);
1502 ret
= btrfs_del_item(trans
, chunk_root
, &path
);
1505 btrfs_release_path(chunk_root
, &path
);
1507 /* for each block group, create device extent and chunk item */
1509 while (cur_start
< total_bytes
) {
1510 cache
= btrfs_lookup_block_group(root
->fs_info
, cur_start
);
1513 /* insert device extent */
1514 key
.objectid
= device
->devid
;
1515 key
.offset
= cache
->key
.objectid
;
1516 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1517 ret
= btrfs_insert_empty_item(trans
, device
->dev_root
, &path
,
1518 &key
, sizeof(*extent
));
1522 leaf
= path
.nodes
[0];
1523 extent
= btrfs_item_ptr(leaf
, path
.slots
[0],
1524 struct btrfs_dev_extent
);
1526 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1527 chunk_root
->root_key
.objectid
);
1528 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1530 btrfs_set_dev_extent_chunk_offset(leaf
, extent
,
1531 cache
->key
.objectid
);
1532 btrfs_set_dev_extent_length(leaf
, extent
, cache
->key
.offset
);
1533 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1534 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1536 btrfs_mark_buffer_dirty(leaf
);
1537 btrfs_release_path(device
->dev_root
, &path
);
1539 /* insert chunk item */
1540 btrfs_set_stack_chunk_length(&chunk
, cache
->key
.offset
);
1541 btrfs_set_stack_chunk_owner(&chunk
,
1542 extent_root
->root_key
.objectid
);
1543 btrfs_set_stack_chunk_stripe_len(&chunk
, 64 * 1024);
1544 btrfs_set_stack_chunk_type(&chunk
, cache
->flags
);
1545 btrfs_set_stack_chunk_io_align(&chunk
, device
->io_align
);
1546 btrfs_set_stack_chunk_io_width(&chunk
, device
->io_width
);
1547 btrfs_set_stack_chunk_sector_size(&chunk
, device
->sector_size
);
1548 btrfs_set_stack_chunk_num_stripes(&chunk
, 1);
1549 btrfs_set_stack_chunk_sub_stripes(&chunk
, 0);
1550 btrfs_set_stack_stripe_devid(&chunk
.stripe
, device
->devid
);
1551 btrfs_set_stack_stripe_offset(&chunk
.stripe
,
1552 cache
->key
.objectid
);
1553 memcpy(&chunk
.stripe
.dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1555 key
.objectid
= chunk_objectid
;
1556 key
.offset
= cache
->key
.objectid
;
1557 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1559 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, &chunk
,
1560 btrfs_chunk_item_size(1));
1564 cur_start
= cache
->key
.objectid
+ cache
->key
.offset
;
1567 device
->bytes_used
= total_bytes
;
1568 ret
= btrfs_update_device(trans
, device
);
1570 btrfs_release_path(device
->dev_root
, &path
);
1574 static int init_btrfs(struct btrfs_root
*root
)
1577 struct btrfs_key location
;
1578 struct btrfs_trans_handle
*trans
;
1579 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1581 trans
= btrfs_start_transaction(root
, 1);
1583 ret
= btrfs_make_block_groups(trans
, root
);
1586 ret
= fixup_block_accounting(trans
, root
);
1589 ret
= create_chunk_mapping(trans
, root
);
1592 ret
= btrfs_make_root_dir(trans
, fs_info
->tree_root
,
1593 BTRFS_ROOT_TREE_DIR_OBJECTID
);
1596 memcpy(&location
, &root
->root_key
, sizeof(location
));
1597 location
.offset
= (u64
)-1;
1598 ret
= btrfs_insert_dir_item(trans
, fs_info
->tree_root
, "default", 7,
1599 btrfs_super_root_dir(&fs_info
->super_copy
),
1600 &location
, BTRFS_FT_DIR
);
1603 ret
= btrfs_insert_inode_ref(trans
, fs_info
->tree_root
, "default", 7,
1605 btrfs_super_root_dir(&fs_info
->super_copy
));
1608 btrfs_set_root_dirid(&fs_info
->fs_root
->root_item
,
1609 BTRFS_FIRST_FREE_OBJECTID
);
1610 ret
= btrfs_commit_transaction(trans
, root
);
1617 * Migrate super block to it's default position and zero 0 ~ 16k
1619 static int migrate_super_block(int fd
, u64 old_bytenr
, u32 sectorsize
)
1622 struct extent_buffer
*buf
;
1623 struct btrfs_super_block
*super
;
1627 BUG_ON(sectorsize
< sizeof(*super
));
1628 buf
= malloc(sizeof(*buf
) + sectorsize
);
1632 buf
->len
= sectorsize
;
1633 ret
= pread(fd
, buf
->data
, sectorsize
, old_bytenr
);
1634 if (ret
!= sectorsize
)
1637 super
= (struct btrfs_super_block
*)buf
->data
;
1638 BUG_ON(btrfs_super_bytenr(super
) != old_bytenr
);
1639 btrfs_set_super_bytenr(super
, BTRFS_SUPER_INFO_OFFSET
);
1641 csum_tree_block(NULL
, buf
, 0);
1642 ret
= pwrite(fd
, buf
->data
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
1643 if (ret
!= sectorsize
)
1650 memset(buf
->data
, 0, sectorsize
);
1651 for (bytenr
= 0; bytenr
< BTRFS_SUPER_INFO_OFFSET
; ) {
1652 len
= BTRFS_SUPER_INFO_OFFSET
- bytenr
;
1653 if (len
> sectorsize
)
1655 ret
= pwrite(fd
, buf
->data
, len
, bytenr
);
1657 fprintf(stderr
, "unable to zero fill device\n");
1671 static int prepare_system_chunk_sb(struct btrfs_super_block
*super
)
1673 struct btrfs_chunk
*chunk
;
1674 struct btrfs_disk_key
*key
;
1675 u32 sectorsize
= btrfs_super_sectorsize(super
);
1677 key
= (struct btrfs_disk_key
*)(super
->sys_chunk_array
);
1678 chunk
= (struct btrfs_chunk
*)(super
->sys_chunk_array
+
1679 sizeof(struct btrfs_disk_key
));
1681 btrfs_set_disk_key_objectid(key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1682 btrfs_set_disk_key_type(key
, BTRFS_CHUNK_ITEM_KEY
);
1683 btrfs_set_disk_key_offset(key
, 0);
1685 btrfs_set_stack_chunk_length(chunk
, btrfs_super_total_bytes(super
));
1686 btrfs_set_stack_chunk_owner(chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
1687 btrfs_set_stack_chunk_stripe_len(chunk
, 64 * 1024);
1688 btrfs_set_stack_chunk_type(chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
1689 btrfs_set_stack_chunk_io_align(chunk
, sectorsize
);
1690 btrfs_set_stack_chunk_io_width(chunk
, sectorsize
);
1691 btrfs_set_stack_chunk_sector_size(chunk
, sectorsize
);
1692 btrfs_set_stack_chunk_num_stripes(chunk
, 1);
1693 btrfs_set_stack_chunk_sub_stripes(chunk
, 0);
1694 chunk
->stripe
.devid
= super
->dev_item
.devid
;
1695 chunk
->stripe
.offset
= cpu_to_le64(0);
1696 memcpy(chunk
->stripe
.dev_uuid
, super
->dev_item
.uuid
, BTRFS_UUID_SIZE
);
1697 btrfs_set_super_sys_array_size(super
, sizeof(*key
) + sizeof(*chunk
));
1701 static int prepare_system_chunk(int fd
, u64 sb_bytenr
, u32 sectorsize
)
1704 struct extent_buffer
*buf
;
1705 struct btrfs_super_block
*super
;
1707 BUG_ON(sectorsize
< sizeof(*super
));
1708 buf
= malloc(sizeof(*buf
) + sectorsize
);
1712 buf
->len
= sectorsize
;
1713 ret
= pread(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1714 if (ret
!= sectorsize
)
1717 super
= (struct btrfs_super_block
*)buf
->data
;
1718 BUG_ON(btrfs_super_bytenr(super
) != sb_bytenr
);
1719 BUG_ON(btrfs_super_num_devices(super
) != 1);
1721 ret
= prepare_system_chunk_sb(super
);
1725 csum_tree_block(NULL
, buf
, 0);
1726 ret
= pwrite(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1727 if (ret
!= sectorsize
)
1738 static int relocate_one_reference(struct btrfs_trans_handle
*trans
,
1739 struct btrfs_root
*root
,
1740 u64 extent_start
, u64 extent_size
,
1741 u64 objectid
, u64 offset
,
1742 struct extent_io_tree
*reloc_tree
)
1744 struct extent_buffer
*leaf
;
1745 struct btrfs_file_extent_item
*fi
;
1746 struct btrfs_key key
;
1747 struct btrfs_path path
;
1748 struct btrfs_inode_item inode
;
1749 struct blk_iterate_data data
;
1758 u32 sectorsize
= root
->sectorsize
;
1763 btrfs_init_path(&path
);
1764 ret
= btrfs_lookup_file_extent(trans
, root
, &path
,
1765 objectid
, offset
, -1);
1769 leaf
= path
.nodes
[0];
1770 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
1771 struct btrfs_file_extent_item
);
1772 /* the extent may be referenced by old snapshot */
1773 if (extent_start
!= btrfs_file_extent_disk_bytenr(leaf
, fi
) ||
1774 extent_size
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1779 root_gen
= btrfs_header_generation(leaf
);
1780 root_owner
= btrfs_header_owner(leaf
);
1781 bytenr
= extent_start
+ btrfs_file_extent_offset(leaf
, fi
);
1782 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
1784 ret
= btrfs_del_item(trans
, root
, &path
);
1787 btrfs_release_path(root
, &path
);
1789 key
.objectid
= objectid
;
1791 key
.type
= BTRFS_INODE_ITEM_KEY
;
1792 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 0);
1796 leaf
= path
.nodes
[0];
1797 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1798 read_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1799 btrfs_release_path(root
, &path
);
1801 BUG_ON(num_bytes
& (sectorsize
- 1));
1802 nblocks
= btrfs_stack_inode_nblocks(&inode
) - num_bytes
/ 512;
1803 btrfs_set_stack_inode_nblocks(&inode
, nblocks
);
1805 data
= (struct blk_iterate_data
) {
1809 .objectid
= objectid
,
1810 .first_block
= offset
/ sectorsize
,
1813 .boundary
= (u64
)-1,
1818 cur_offset
= offset
;
1819 while (num_bytes
> 0) {
1820 sector_end
= bytenr
+ sectorsize
- 1;
1821 if (test_range_bit(reloc_tree
, bytenr
, sector_end
,
1822 EXTENT_LOCKED
, 1)) {
1823 ret
= get_state_private(reloc_tree
, bytenr
, &new_pos
);
1826 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1829 new_pos
= key
.objectid
;
1831 if (cur_offset
== offset
) {
1832 fd
= root
->fs_info
->fs_devices
->latest_bdev
;
1833 readahead(fd
, bytenr
, num_bytes
);
1835 ret
= copy_disk_extent(root
, new_pos
, bytenr
,
1839 ret
= set_extent_bits(reloc_tree
, bytenr
, sector_end
,
1840 EXTENT_LOCKED
, GFP_NOFS
);
1842 ret
= set_state_private(reloc_tree
, bytenr
, new_pos
);
1846 ret
= block_iterate_proc(NULL
, new_pos
/ sectorsize
,
1847 cur_offset
/ sectorsize
, &data
);
1848 if (ret
& BLOCK_ABORT
) {
1853 cur_offset
+= sectorsize
;
1854 bytenr
+= sectorsize
;
1855 num_bytes
-= sectorsize
;
1858 if (data
.num_blocks
> 0) {
1859 ret
= record_file_blocks(trans
, root
, objectid
, &inode
,
1860 data
.first_block
, data
.disk_block
,
1861 data
.num_blocks
, 0);
1866 key
.objectid
= objectid
;
1868 key
.type
= BTRFS_INODE_ITEM_KEY
;
1869 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 1);
1873 leaf
= path
.nodes
[0];
1874 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1875 write_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1876 btrfs_mark_buffer_dirty(leaf
);
1877 btrfs_release_path(root
, &path
);
1879 ret
= btrfs_free_extent(trans
, root
, extent_start
, extent_size
,
1880 root_owner
, root_gen
, objectid
, offset
, 0);
1882 btrfs_release_path(root
, &path
);
1886 static int relocate_extents_range(struct btrfs_root
*fs_root
,
1887 struct btrfs_root
*ext2_root
,
1888 u64 start_byte
, u64 end_byte
)
1890 struct btrfs_fs_info
*info
= fs_root
->fs_info
;
1891 struct btrfs_root
*extent_root
= info
->extent_root
;
1892 struct btrfs_root
*cur_root
;
1893 struct btrfs_trans_handle
*trans
;
1894 struct btrfs_extent_ref
*ref_item
;
1895 struct extent_buffer
*leaf
;
1896 struct btrfs_key key
;
1897 struct btrfs_path path
;
1898 struct extent_io_tree reloc_tree
;
1909 btrfs_init_path(&path
);
1910 extent_io_tree_init(&reloc_tree
);
1912 cur_root
= (pass
% 2 == 0) ? ext2_root
: fs_root
;
1915 trans
= btrfs_start_transaction(cur_root
, 1);
1918 cur_byte
= start_byte
;
1920 key
.objectid
= cur_byte
;
1922 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1923 ret
= btrfs_search_slot(trans
, extent_root
,
1928 leaf
= path
.nodes
[0];
1929 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1930 ret
= btrfs_next_leaf(extent_root
, &path
);
1935 leaf
= path
.nodes
[0];
1938 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1939 if (key
.objectid
< cur_byte
||
1940 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1944 if (key
.objectid
>= end_byte
)
1947 cur_byte
= key
.objectid
;
1948 num_bytes
= key
.offset
;
1951 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1952 ret
= btrfs_next_leaf(extent_root
, &path
);
1957 leaf
= path
.nodes
[0];
1960 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1961 if (key
.objectid
!= cur_byte
)
1963 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1967 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1968 struct btrfs_extent_ref
);
1969 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1970 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1971 ref_offset
= btrfs_ref_offset(leaf
, ref_item
);
1974 BUG_ON(ref_owner
< BTRFS_FIRST_FREE_OBJECTID
);
1975 if (ref_root
== cur_root
->root_key
.objectid
) {
1984 ret
= relocate_one_reference(trans
, cur_root
, cur_byte
,
1985 num_bytes
, ref_owner
,
1986 ref_offset
, &reloc_tree
);
1990 cur_byte
+= num_bytes
;
1991 btrfs_release_path(extent_root
, &path
);
1993 if (trans
->blocks_used
>= 4096) {
1994 ret
= btrfs_commit_transaction(trans
, cur_root
);
1996 trans
= btrfs_start_transaction(cur_root
, 1);
2000 btrfs_release_path(cur_root
, &path
);
2002 ret
= btrfs_commit_transaction(trans
, cur_root
);
2005 if (num_refs
> 0 && pass
++ < 4)
2008 ret
= (num_refs
> 0) ? -1 : 0;
2010 btrfs_release_path(cur_root
, &path
);
2011 extent_io_tree_cleanup(&reloc_tree
);
2016 * relocate data in system chunk
2018 static int cleanup_sys_chunk(struct btrfs_root
*fs_root
,
2019 struct btrfs_root
*ext2_root
)
2021 struct btrfs_block_group_cache
*cache
;
2027 cache
= btrfs_lookup_block_group(fs_root
->fs_info
, offset
);
2031 end_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
2032 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2033 ret
= relocate_extents_range(fs_root
, ext2_root
,
2034 cache
->key
.objectid
,
2045 static int fixup_chunk_mapping(struct btrfs_root
*root
)
2047 struct btrfs_trans_handle
*trans
;
2048 struct btrfs_fs_info
*info
= root
->fs_info
;
2049 struct btrfs_root
*chunk_root
= info
->chunk_root
;
2050 struct extent_buffer
*leaf
;
2051 struct btrfs_key key
;
2052 struct btrfs_path path
;
2053 struct btrfs_chunk chunk
;
2059 btrfs_init_path(&path
);
2061 trans
= btrfs_start_transaction(root
, 1);
2065 * recow the whole chunk tree. this will move all chunk tree blocks
2066 * into system block group.
2068 memset(&key
, 0, sizeof(key
));
2070 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2074 ret
= btrfs_next_leaf(chunk_root
, &path
);
2080 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2081 btrfs_release_path(chunk_root
, &path
);
2083 btrfs_release_path(chunk_root
, &path
);
2085 /* fixup the system chunk array in super block */
2086 btrfs_set_super_sys_array_size(&info
->super_copy
, 0);
2088 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2090 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2092 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 0);
2097 leaf
= path
.nodes
[0];
2098 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2099 ret
= btrfs_next_leaf(chunk_root
, &path
);
2104 leaf
= path
.nodes
[0];
2106 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2107 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
)
2110 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
2111 size
= btrfs_item_size_nr(leaf
, path
.slots
[0]);
2112 BUG_ON(size
!= sizeof(chunk
));
2113 read_extent_buffer(leaf
, &chunk
, ptr
, size
);
2114 type
= btrfs_stack_chunk_type(&chunk
);
2116 if (!(type
& BTRFS_BLOCK_GROUP_SYSTEM
))
2119 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
2127 ret
= btrfs_commit_transaction(trans
, root
);
2130 btrfs_release_path(chunk_root
, &path
);
2134 int do_convert(const char *devname
, int datacsum
, int packing
, int noxattr
)
2141 ext2_filsys ext2_fs
;
2142 struct btrfs_root
*root
;
2143 struct btrfs_root
*ext2_root
;
2145 ret
= open_ext2fs(devname
, &ext2_fs
);
2147 fprintf(stderr
, "unable to open the Ext2fs\n");
2150 blocksize
= ext2_fs
->blocksize
;
2151 total_bytes
= (u64
)ext2_fs
->super
->s_blocks_count
* blocksize
;
2152 if (blocksize
< 4096) {
2153 fprintf(stderr
, "block size is too small\n");
2156 if (!(ext2_fs
->super
->s_feature_incompat
&
2157 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
2158 fprintf(stderr
, "filetype feature is missing\n");
2161 for (i
= 0; i
< 6; i
++) {
2162 ret
= ext2_alloc_block(ext2_fs
, 0, blocks
+ i
);
2164 fprintf(stderr
, "not enough free space\n");
2167 blocks
[i
] *= blocksize
;
2169 super_bytenr
= blocks
[0];
2170 fd
= open(devname
, O_RDWR
);
2172 fprintf(stderr
, "unable to open %s\n", devname
);
2175 ret
= make_btrfs(fd
, devname
, ext2_fs
->super
->s_volume_name
,
2176 blocks
, total_bytes
, blocksize
, blocksize
,
2177 blocksize
, blocksize
);
2179 fprintf(stderr
, "unable to create initial ctree\n");
2182 /* create a system chunk that maps the whole device */
2183 ret
= prepare_system_chunk(fd
, super_bytenr
, blocksize
);
2185 fprintf(stderr
, "unable to update system chunk\n");
2188 root
= open_ctree_fd(fd
, devname
, super_bytenr
);
2190 fprintf(stderr
, "unable to open ctree\n");
2193 ret
= cache_free_extents(root
, ext2_fs
);
2195 fprintf(stderr
, "error during cache_free_extents %d\n", ret
);
2198 root
->fs_info
->extent_ops
= &extent_ops
;
2199 /* recover block allocation bitmap */
2200 for (i
= 0; i
< 6; i
++) {
2201 blocks
[i
] /= blocksize
;
2202 ext2_free_block(ext2_fs
, blocks
[i
]);
2204 ret
= init_btrfs(root
);
2206 fprintf(stderr
, "unable to setup the root tree\n");
2209 ext2_root
= create_subvol(root
, "ext2_saved");
2211 fprintf(stderr
, "unable to create subvol\n");
2214 printf("creating btrfs metadata.\n");
2215 ret
= copy_inodes(root
, ext2_fs
, datacsum
, packing
, noxattr
);
2217 fprintf(stderr
, "error during copy_inodes %d\n", ret
);
2220 printf("creating ext2fs image file.\n");
2221 ret
= create_ext2_image(ext2_root
, ext2_fs
, "image");
2223 fprintf(stderr
, "error during create_ext2_image %d\n", ret
);
2226 printf("cleaning up system chunk.\n");
2227 ret
= cleanup_sys_chunk(root
, ext2_root
);
2229 fprintf(stderr
, "error during cleanup_sys_chunk %d\n", ret
);
2232 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2233 ret
= close_ctree(root
);
2235 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2238 close_ext2fs(ext2_fs
);
2241 * If this step succeed, we get a mountable btrfs. Otherwise
2242 * the ext2fs is left unchanged.
2244 ret
= migrate_super_block(fd
, super_bytenr
, blocksize
);
2246 fprintf(stderr
, "unable to migrate super block\n");
2250 root
= open_ctree_fd(fd
, devname
, 0);
2252 fprintf(stderr
, "unable to open ctree\n");
2255 /* move chunk tree into system chunk. */
2256 ret
= fixup_chunk_mapping(root
);
2258 fprintf(stderr
, "error during fixup_chunk_tree\n");
2261 ret
= close_ctree(root
);
2264 printf("conversion complete.\n");
2267 fprintf(stderr
, "conversion aborted.\n");
2271 static int may_rollback(struct btrfs_root
*root
)
2273 struct btrfs_fs_info
*info
= root
->fs_info
;
2274 struct btrfs_multi_bio
*multi
= NULL
;
2282 if (btrfs_super_num_devices(&info
->super_copy
) != 1)
2285 bytenr
= BTRFS_SUPER_INFO_OFFSET
;
2286 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2289 ret
= btrfs_map_block(&info
->mapping_tree
, WRITE
, bytenr
,
2290 &length
, &multi
, 0);
2294 num_stripes
= multi
->num_stripes
;
2295 physical
= multi
->stripes
[0].physical
;
2298 if (num_stripes
!= 1 || physical
!= bytenr
)
2302 if (bytenr
>= total_bytes
)
2310 int do_rollback(const char *devname
, int force
)
2314 struct btrfs_root
*root
;
2315 struct btrfs_root
*ext2_root
;
2316 struct btrfs_root
*chunk_root
;
2317 struct btrfs_dir_item
*dir
;
2318 struct btrfs_inode_item
*inode
;
2319 struct btrfs_file_extent_item
*fi
;
2320 struct btrfs_trans_handle
*trans
;
2321 struct extent_buffer
*leaf
;
2322 struct btrfs_block_group_cache
*cache1
;
2323 struct btrfs_block_group_cache
*cache2
;
2324 struct btrfs_key key
;
2325 struct btrfs_path path
;
2326 struct extent_io_tree io_tree
;
2341 extent_io_tree_init(&io_tree
);
2343 fd
= open(devname
, O_RDWR
);
2345 fprintf(stderr
, "unable to open %s\n", devname
);
2348 root
= open_ctree_fd(fd
, devname
, 0);
2350 fprintf(stderr
, "unable to open ctree\n");
2353 ret
= may_rollback(root
);
2355 fprintf(stderr
, "unable to do rollback\n");
2359 sectorsize
= root
->sectorsize
;
2360 buf
= malloc(sectorsize
);
2362 fprintf(stderr
, "unable to allocate memory\n");
2366 btrfs_init_path(&path
);
2367 name
= "ext2_saved";
2368 root_dir
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2369 dir
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, &path
,
2370 root_dir
, name
, strlen(name
), 0);
2371 if (!dir
|| IS_ERR(dir
)) {
2372 fprintf(stderr
, "unable to find subvol %s\n", name
);
2375 leaf
= path
.nodes
[0];
2376 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2377 btrfs_release_path(root
->fs_info
->tree_root
, &path
);
2379 ext2_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2380 if (!ext2_root
|| IS_ERR(ext2_root
)) {
2381 fprintf(stderr
, "unable to open subvol %s\n", name
);
2386 root_dir
= btrfs_root_dirid(&root
->root_item
);
2387 dir
= btrfs_lookup_dir_item(NULL
, ext2_root
, &path
,
2388 root_dir
, name
, strlen(name
), 0);
2389 if (!dir
|| IS_ERR(dir
)) {
2390 fprintf(stderr
, "unable to find file %s\n", name
);
2393 leaf
= path
.nodes
[0];
2394 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2395 btrfs_release_path(ext2_root
, &path
);
2397 objectid
= key
.objectid
;
2399 ret
= btrfs_lookup_inode(NULL
, ext2_root
, &path
, &key
, 0);
2401 fprintf(stderr
, "unable to find inode item\n");
2404 leaf
= path
.nodes
[0];
2405 inode
= btrfs_item_ptr(leaf
, path
.slots
[0], struct btrfs_inode_item
);
2406 total_bytes
= btrfs_inode_size(leaf
, inode
);
2407 btrfs_release_path(ext2_root
, &path
);
2409 key
.objectid
= objectid
;
2411 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2412 ret
= btrfs_search_slot(NULL
, ext2_root
, &key
, &path
, 0, 0);
2414 fprintf(stderr
, "unable to find first file extent\n");
2415 btrfs_release_path(ext2_root
, &path
);
2419 /* build mapping tree for the relocated blocks */
2420 for (offset
= 0; offset
< total_bytes
; ) {
2421 leaf
= path
.nodes
[0];
2422 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2423 ret
= btrfs_next_leaf(root
, &path
);
2429 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2430 if (key
.objectid
!= objectid
|| key
.offset
!= offset
||
2431 btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2434 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
2435 struct btrfs_file_extent_item
);
2436 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2439 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2440 /* skip holes and direct mapped extents */
2441 if (bytenr
== 0 || bytenr
== offset
)
2444 bytenr
+= btrfs_file_extent_offset(leaf
, fi
);
2445 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
2447 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2448 cache2
= btrfs_lookup_block_group(root
->fs_info
,
2449 offset
+ num_bytes
- 1);
2450 if (!cache1
|| cache1
!= cache2
||
2451 !(cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
))
2454 set_extent_bits(&io_tree
, offset
, offset
+ num_bytes
- 1,
2455 EXTENT_LOCKED
, GFP_NOFS
);
2456 set_state_private(&io_tree
, offset
, bytenr
);
2458 offset
+= btrfs_file_extent_num_bytes(leaf
, fi
);
2461 btrfs_release_path(ext2_root
, &path
);
2462 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2464 if (offset
< total_bytes
) {
2465 fprintf(stderr
, "unable to build extent mapping\n");
2469 first_free
= BTRFS_SUPER_INFO_OFFSET
+ 2 * sectorsize
- 1;
2470 first_free
&= ~((u64
)sectorsize
- 1);
2471 /* backup for extent #0 should exist */
2472 if(!test_range_bit(&io_tree
, 0, first_free
- 1, EXTENT_LOCKED
, 1)) {
2473 fprintf(stderr
, "no backup for the first extent\n");
2476 /* force no allocation from system block group */
2477 root
->fs_info
->system_allocs
= -1;
2478 trans
= btrfs_start_transaction(root
, 1);
2481 * recow the whole chunk tree, this will remove all chunk tree blocks
2482 * from system block group
2484 chunk_root
= root
->fs_info
->chunk_root
;
2485 memset(&key
, 0, sizeof(key
));
2487 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2491 ret
= btrfs_next_leaf(chunk_root
, &path
);
2495 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2496 btrfs_release_path(chunk_root
, &path
);
2498 btrfs_release_path(chunk_root
, &path
);
2503 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2507 if (cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2508 num_bytes
+= btrfs_block_group_used(&cache1
->item
);
2510 offset
= cache1
->key
.objectid
+ cache1
->key
.offset
;
2512 /* only extent #0 left in system block group? */
2513 if (num_bytes
> first_free
) {
2514 fprintf(stderr
, "unable to empty system block group\n");
2517 /* create a system chunk that maps the whole device */
2518 ret
= prepare_system_chunk_sb(&root
->fs_info
->super_copy
);
2520 fprintf(stderr
, "unable to update system chunk\n");
2524 ret
= btrfs_commit_transaction(trans
, root
);
2527 ret
= close_ctree(root
);
2529 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2533 sb_bytenr
= (u64
)-1;
2534 /* copy all relocated blocks back */
2536 ret
= find_first_extent_bit(&io_tree
, 0, &start
, &end
,
2541 ret
= get_state_private(&io_tree
, start
, &bytenr
);
2544 clear_extent_bits(&io_tree
, start
, end
, EXTENT_LOCKED
,
2547 while (start
<= end
) {
2548 if (start
== BTRFS_SUPER_INFO_OFFSET
) {
2552 ret
= pread(fd
, buf
, sectorsize
, bytenr
);
2554 fprintf(stderr
, "error during pread %d\n", ret
);
2557 BUG_ON(ret
!= sectorsize
);
2558 ret
= pwrite(fd
, buf
, sectorsize
, start
);
2560 fprintf(stderr
, "error during pwrite %d\n", ret
);
2563 BUG_ON(ret
!= sectorsize
);
2565 start
+= sectorsize
;
2566 bytenr
+= sectorsize
;
2572 fprintf(stderr
, "error during fsync %d\n", ret
);
2576 * finally, overwrite btrfs super block.
2578 ret
= pread(fd
, buf
, sectorsize
, sb_bytenr
);
2580 fprintf(stderr
, "error during pread %d\n", ret
);
2583 BUG_ON(ret
!= sectorsize
);
2584 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
2586 fprintf(stderr
, "error during pwrite %d\n", ret
);
2589 BUG_ON(ret
!= sectorsize
);
2592 fprintf(stderr
, "error during fsync %d\n", ret
);
2598 extent_io_tree_cleanup(&io_tree
);
2599 printf("rollback complete.\n");
2602 fprintf(stderr
, "rollback aborted.\n");
2606 static void print_usage(void)
2608 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2609 printf("\t-d disable data checksum\n");
2610 printf("\t-i ignore xattrs and ACLs\n");
2611 printf("\t-n disable packing of small files\n");
2612 printf("\t-r roll back to ext2fs\n");
2615 int main(int argc
, char *argv
[])
2624 int c
= getopt(argc
, argv
, "dinr");
2645 argc
= argc
- optind
;
2651 file
= argv
[optind
];
2652 if (check_mounted(file
)) {
2653 fprintf(stderr
, "%s is mounted\n", file
);
2658 ret
= do_rollback(file
, 0);
2660 ret
= do_convert(file
, datacsum
, packing
, noxattr
);