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 500
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
26 #include <sys/types.h>
31 #include <uuid/uuid.h>
33 #include "kerncompat.h"
37 #include "transaction.h"
40 #include <ext2fs/ext2_fs.h>
41 #include <ext2fs/ext2fs.h>
42 #include <ext2fs/ext2_ext_attr.h>
43 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
45 * Open Ext2fs in readonly mode, read block allocation bitmap and
46 * inode bitmap into memory.
48 static int open_ext2fs(const char *name
, ext2_filsys
*ret_fs
)
52 ret
= ext2fs_open(name
, 0, 0, 0, unix_io_manager
, &ext2_fs
);
54 fprintf(stderr
, "ext2fs_open: %s\n", error_message(ret
));
57 ret
= ext2fs_read_inode_bitmap(ext2_fs
);
59 fprintf(stderr
, "ext2fs_read_inode_bitmap: %s\n",
63 ret
= ext2fs_read_block_bitmap(ext2_fs
);
65 fprintf(stderr
, "ext2fs_read_block_bitmap: %s\n",
75 static int close_ext2fs(ext2_filsys fs
)
81 static int ext2_alloc_block(ext2_filsys fs
, u64 goal
, u64
*block_ret
)
85 if (!ext2fs_new_block(fs
, goal
, NULL
, &block
)) {
86 ext2fs_fast_mark_block_bitmap(fs
->block_map
, block
);
93 static int ext2_free_block(ext2_filsys fs
, u64 block
)
95 BUG_ON(block
!= (blk_t
)block
);
96 ext2fs_fast_unmark_block_bitmap(fs
->block_map
, block
);
100 static int custom_alloc_extent(struct btrfs_root
*root
, u64 num_bytes
,
101 u64 hint_byte
, struct btrfs_key
*ins
)
103 struct btrfs_block_group_cache
*cache
;
104 ext2_filsys ext2_fs
= (ext2_filsys
)root
->fs_info
->priv_data
;
105 u32 blocksize
= ext2_fs
->blocksize
;
112 block
= hint_byte
/ blocksize
;
113 ret
= ext2fs_new_block(ext2_fs
, block
, NULL
, &block
);
117 bytenr
= (u64
)block
* blocksize
;
118 if (bytenr
< hint_byte
&& ++wrapped
> 1)
121 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
123 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
124 hint_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
128 if (test_range_bit(&root
->fs_info
->pinned_extents
, bytenr
,
129 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 0)) {
130 hint_byte
= bytenr
+ blocksize
;
134 ext2fs_fast_mark_block_bitmap(ext2_fs
->block_map
, block
);
137 ins
->objectid
= bytenr
;
138 ins
->offset
= blocksize
;
139 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
142 fprintf(stderr
, "not enough free space\n");
146 static int custom_free_extent(struct btrfs_root
*root
, u64 bytenr
,
150 ext2_filsys fs
= (ext2_filsys
)root
->fs_info
->priv_data
;
152 BUG_ON(bytenr
& (fs
->blocksize
- 1));
153 block
= bytenr
/ fs
->blocksize
;
154 while (num_bytes
> 0) {
155 ext2_free_block(fs
, block
);
157 num_bytes
-= fs
->blocksize
;
162 struct btrfs_extent_ops extent_ops
= {
163 .alloc_extent
= custom_alloc_extent
,
164 .free_extent
= custom_free_extent
,
167 struct dir_iterate_data
{
168 struct btrfs_trans_handle
*trans
;
169 struct btrfs_root
*root
;
170 struct btrfs_inode_item
*inode
;
176 static u8 filetype_conversion_table
[EXT2_FT_MAX
] = {
177 [EXT2_FT_UNKNOWN
] = BTRFS_FT_UNKNOWN
,
178 [EXT2_FT_REG_FILE
] = BTRFS_FT_REG_FILE
,
179 [EXT2_FT_DIR
] = BTRFS_FT_DIR
,
180 [EXT2_FT_CHRDEV
] = BTRFS_FT_CHRDEV
,
181 [EXT2_FT_BLKDEV
] = BTRFS_FT_BLKDEV
,
182 [EXT2_FT_FIFO
] = BTRFS_FT_FIFO
,
183 [EXT2_FT_SOCK
] = BTRFS_FT_SOCK
,
184 [EXT2_FT_SYMLINK
] = BTRFS_FT_SYMLINK
,
187 static int dir_iterate_proc(ext2_ino_t dir
, int entry
,
188 struct ext2_dir_entry
*old
,
189 int offset
, int blocksize
,
190 char *buf
,void *priv_data
)
196 char dotdot
[] = "..";
197 struct btrfs_key location
;
198 struct ext2_dir_entry_2
*dirent
= (struct ext2_dir_entry_2
*)old
;
199 struct dir_iterate_data
*idata
= (struct dir_iterate_data
*)priv_data
;
201 objectid
= dirent
->inode
+ INO_OFFSET
;
202 if (!strncmp(dirent
->name
, dotdot
, dirent
->name_len
)) {
203 if (dirent
->name_len
== 2) {
204 BUG_ON(idata
->parent
!= 0);
205 idata
->parent
= objectid
;
209 if (dirent
->inode
< EXT2_GOOD_OLD_FIRST_INO
)
212 location
.objectid
= objectid
;
214 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
216 file_type
= dirent
->file_type
;
217 BUG_ON(file_type
> EXT2_FT_SYMLINK
);
218 ret
= btrfs_insert_dir_item(idata
->trans
, idata
->root
,
219 dirent
->name
, dirent
->name_len
,
220 idata
->objectid
, &location
,
221 filetype_conversion_table
[file_type
]);
224 ret
= btrfs_insert_inode_ref(idata
->trans
, idata
->root
,
225 dirent
->name
, dirent
->name_len
,
226 objectid
, idata
->objectid
);
229 inode_size
= btrfs_stack_inode_size(idata
->inode
) +
230 dirent
->name_len
* 2;
231 btrfs_set_stack_inode_size(idata
->inode
, inode_size
);
234 idata
->errcode
= ret
;
238 static int create_dir_entries(struct btrfs_trans_handle
*trans
,
239 struct btrfs_root
*root
, u64 objectid
,
240 struct btrfs_inode_item
*btrfs_inode
,
241 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
245 struct dir_iterate_data data
= {
248 .inode
= btrfs_inode
,
249 .objectid
= objectid
,
254 err
= ext2fs_dir_iterate2(ext2_fs
, ext2_ino
, 0, NULL
,
255 dir_iterate_proc
, &data
);
259 if (ret
== 0 && data
.parent
== objectid
) {
260 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2,
265 fprintf(stderr
, "ext2fs_dir_iterate2: %s\n", error_message(err
));
269 static int read_disk_extent(struct btrfs_root
*root
, u64 bytenr
,
270 u32 num_bytes
, char *buffer
)
273 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
275 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, bytenr
);
276 if (ret
!= num_bytes
)
285 * Record a file extent. Do all the required works, such as inserting
286 * file extent item, inserting extent item and backref item into extent
287 * tree and updating block accounting.
289 static int record_file_extent(struct btrfs_trans_handle
*trans
,
290 struct btrfs_root
*root
, u64 objectid
,
291 struct btrfs_inode_item
*inode
,
292 u64 file_pos
, u64 disk_bytenr
,
293 u64 num_bytes
, int checksum
)
296 struct btrfs_fs_info
*info
= root
->fs_info
;
297 struct btrfs_root
*extent_root
= info
->extent_root
;
298 struct btrfs_key ins_key
;
299 struct btrfs_path path
;
300 struct btrfs_extent_item extent_item
;
301 u32 blocksize
= root
->sectorsize
;
305 ret
= btrfs_insert_file_extent(trans
, root
, objectid
, file_pos
,
306 disk_bytenr
, num_bytes
, num_bytes
);
307 if (ret
|| disk_bytenr
== 0)
310 nblocks
= btrfs_stack_inode_nblocks(inode
) + num_bytes
/ 512;
311 btrfs_set_stack_inode_nblocks(inode
, nblocks
);
317 buffer
= malloc(blocksize
);
320 for (offset
= 0; offset
< num_bytes
; offset
+= blocksize
) {
321 ret
= read_disk_extent(root
, disk_bytenr
+ offset
,
325 ret
= btrfs_csum_file_block(trans
, root
, inode
,
326 objectid
, file_pos
+ offset
,
336 bytes_used
= btrfs_root_used(&root
->root_item
);
337 btrfs_set_root_used(&root
->root_item
, bytes_used
+ num_bytes
);
338 ins_key
.objectid
= disk_bytenr
;
339 ins_key
.offset
= num_bytes
;
340 btrfs_set_key_type(&ins_key
, BTRFS_EXTENT_ITEM_KEY
);
341 btrfs_set_stack_extent_refs(&extent_item
, 1);
342 ret
= btrfs_insert_item(trans
, extent_root
, &ins_key
,
343 &extent_item
, sizeof(extent_item
));
345 bytes_used
= btrfs_super_bytes_used(&info
->super_copy
);
346 btrfs_set_super_bytes_used(&info
->super_copy
, bytes_used
+
348 btrfs_init_path(&path
);
349 ret
= btrfs_insert_extent_backref(trans
, extent_root
, &path
,
350 disk_bytenr
, root
->root_key
.objectid
,
351 trans
->transid
, objectid
, file_pos
);
354 ret
= btrfs_update_block_group(trans
, root
, disk_bytenr
,
356 } else if (ret
== -EEXIST
) {
357 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
, num_bytes
,
358 root
->root_key
.objectid
,
359 trans
->transid
, objectid
, file_pos
);
363 btrfs_extent_post_op(trans
, extent_root
);
369 static int record_file_blocks(struct btrfs_trans_handle
*trans
,
370 struct btrfs_root
*root
, u64 objectid
,
371 struct btrfs_inode_item
*inode
,
372 u64 file_block
, u64 disk_block
,
373 u64 num_blocks
, int checksum
)
375 u64 file_pos
= file_block
* root
->sectorsize
;
376 u64 disk_bytenr
= disk_block
* root
->sectorsize
;
377 u64 num_bytes
= num_blocks
* root
->sectorsize
;
378 return record_file_extent(trans
, root
, objectid
, inode
, file_pos
,
379 disk_bytenr
, num_bytes
, checksum
);
382 struct blk_iterate_data
{
383 struct btrfs_trans_handle
*trans
;
384 struct btrfs_root
*root
;
385 struct btrfs_inode_item
*inode
;
395 static int block_iterate_proc(ext2_filsys ext2_fs
,
396 u64 disk_block
, u64 file_block
,
397 struct blk_iterate_data
*idata
)
400 struct btrfs_root
*root
= idata
->root
;
401 struct btrfs_trans_handle
*trans
= idata
->trans
;
402 struct btrfs_block_group_cache
*cache
;
405 BUG_ON(disk_block
== 0);
406 if ((idata
->num_blocks
> 0 && disk_block
>= idata
->boundary
) ||
407 (file_block
> idata
->first_block
+ idata
->num_blocks
) ||
408 (disk_block
!= idata
->disk_block
+ idata
->num_blocks
)) {
409 if (idata
->num_blocks
> 0) {
410 ret
= record_file_blocks(trans
, root
, idata
->objectid
,
411 idata
->inode
, idata
->first_block
,
412 idata
->disk_block
, idata
->num_blocks
,
416 idata
->first_block
+= idata
->num_blocks
;
417 idata
->num_blocks
= 0;
419 if (file_block
> idata
->first_block
) {
420 ret
= record_file_blocks(trans
, root
, idata
->objectid
,
421 idata
->inode
, idata
->first_block
,
422 0, file_block
- idata
->first_block
,
428 bytenr
= disk_block
* root
->sectorsize
;
429 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
431 bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
433 idata
->first_block
= file_block
;
434 idata
->disk_block
= disk_block
;
435 idata
->boundary
= bytenr
/ root
->sectorsize
;
440 idata
->errcode
= ret
;
444 static int __block_iterate_proc(ext2_filsys fs
, blk_t
*blocknr
,
445 e2_blkcnt_t blockcnt
, blk_t ref_block
,
446 int ref_offset
, void *priv_data
)
448 struct blk_iterate_data
*idata
;
449 idata
= (struct blk_iterate_data
*)priv_data
;
450 return block_iterate_proc(fs
, *blocknr
, blockcnt
, idata
);
454 * traverse file's data blocks, record these data blocks as file extents.
456 static int create_file_extents(struct btrfs_trans_handle
*trans
,
457 struct btrfs_root
*root
, u64 objectid
,
458 struct btrfs_inode_item
*btrfs_inode
,
459 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
460 int datacsum
, int packing
)
466 u32 sectorsize
= root
->sectorsize
;
467 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
468 struct blk_iterate_data data
= {
471 .inode
= btrfs_inode
,
472 .objectid
= objectid
,
477 .checksum
= datacsum
,
480 err
= ext2fs_block_iterate2(ext2_fs
, ext2_ino
, BLOCK_FLAG_DATA_ONLY
,
481 NULL
, __block_iterate_proc
, &data
);
487 if (packing
&& data
.first_block
== 0 && data
.num_blocks
> 0 &&
488 inode_size
<= BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
489 u64 num_bytes
= data
.num_blocks
* sectorsize
;
490 u64 disk_bytenr
= data
.disk_block
* sectorsize
;
492 buffer
= malloc(num_bytes
);
495 ret
= read_disk_extent(root
, disk_bytenr
, num_bytes
, buffer
);
498 if (num_bytes
> inode_size
)
499 num_bytes
= inode_size
;
500 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
,
501 0, buffer
, num_bytes
);
504 } else if (data
.num_blocks
> 0) {
505 ret
= record_file_blocks(trans
, root
, objectid
, btrfs_inode
,
506 data
.first_block
, data
.disk_block
,
507 data
.num_blocks
, data
.checksum
);
511 data
.first_block
+= data
.num_blocks
;
512 last_block
= (inode_size
+ sectorsize
- 1) / sectorsize
;
513 if (last_block
> data
.first_block
) {
514 ret
= record_file_blocks(trans
, root
, objectid
, btrfs_inode
,
515 data
.first_block
, 0, last_block
-
516 data
.first_block
, data
.checksum
);
523 fprintf(stderr
, "ext2fs_block_iterate2: %s\n", error_message(err
));
527 static int create_symbol_link(struct btrfs_trans_handle
*trans
,
528 struct btrfs_root
*root
, u64 objectid
,
529 struct btrfs_inode_item
*btrfs_inode
,
530 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
531 struct ext2_inode
*ext2_inode
)
535 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
536 if (ext2fs_inode_data_blocks(ext2_fs
, ext2_inode
)) {
537 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
+ 1);
538 ret
= create_file_extents(trans
, root
, objectid
, btrfs_inode
,
539 ext2_fs
, ext2_ino
, 1, 1);
540 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
);
544 pathname
= (char *)&(ext2_inode
->i_block
[0]);
545 BUG_ON(pathname
[inode_size
] != 0);
546 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
, 0,
547 pathname
, inode_size
+ 1);
552 * Following xattr/acl related codes are based on codes in
553 * fs/ext3/xattr.c and fs/ext3/acl.c
555 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
556 #define EXT2_XATTR_BFIRST(ptr) \
557 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
558 #define EXT2_XATTR_IHDR(inode) \
559 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
560 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
561 #define EXT2_XATTR_IFIRST(inode) \
562 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
563 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
565 static int ext2_xattr_check_names(struct ext2_ext_attr_entry
*entry
,
568 struct ext2_ext_attr_entry
*next
;
570 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
571 next
= EXT2_EXT_ATTR_NEXT(entry
);
572 if ((void *)next
>= end
)
579 static int ext2_xattr_check_block(const char *buf
, size_t size
)
582 struct ext2_ext_attr_header
*header
= EXT2_XATTR_BHDR(buf
);
584 if (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC
||
585 header
->h_blocks
!= 1)
587 error
= ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf
), buf
+ size
);
591 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry
*entry
,
594 size_t value_size
= entry
->e_value_size
;
596 if (entry
->e_value_block
!= 0 || value_size
> size
||
597 entry
->e_value_offs
+ value_size
> size
)
602 #define EXT2_ACL_VERSION 0x0001
613 } ext2_acl_entry_short
;
619 static inline int ext2_acl_count(size_t size
)
622 size
-= sizeof(ext2_acl_header
);
623 s
= size
- 4 * sizeof(ext2_acl_entry_short
);
625 if (size
% sizeof(ext2_acl_entry_short
))
627 return size
/ sizeof(ext2_acl_entry_short
);
629 if (s
% sizeof(ext2_acl_entry
))
631 return s
/ sizeof(ext2_acl_entry
) + 4;
635 #define ACL_EA_VERSION 0x0002
645 acl_ea_entry a_entries
[0];
648 static inline size_t acl_ea_size(int count
)
650 return sizeof(acl_ea_header
) + count
* sizeof(acl_ea_entry
);
653 static int ext2_acl_to_xattr(void *dst
, const void *src
,
654 size_t dst_size
, size_t src_size
)
657 const void *end
= src
+ src_size
;
658 acl_ea_header
*ext_acl
= (acl_ea_header
*)dst
;
659 acl_ea_entry
*dst_entry
= ext_acl
->a_entries
;
660 ext2_acl_entry
*src_entry
;
662 if (src_size
< sizeof(ext2_acl_header
))
664 if (((ext2_acl_header
*)src
)->a_version
!=
665 cpu_to_le32(EXT2_ACL_VERSION
))
667 src
+= sizeof(ext2_acl_header
);
668 count
= ext2_acl_count(src_size
);
672 BUG_ON(dst_size
< acl_ea_size(count
));
673 ext_acl
->a_version
= cpu_to_le32(ACL_EA_VERSION
);
674 for (i
= 0; i
< count
; i
++, dst_entry
++) {
675 src_entry
= (ext2_acl_entry
*)src
;
676 if (src
+ sizeof(ext2_acl_entry_short
) > end
)
678 dst_entry
->e_tag
= src_entry
->e_tag
;
679 dst_entry
->e_perm
= src_entry
->e_perm
;
680 switch (le16_to_cpu(src_entry
->e_tag
)) {
685 src
+= sizeof(ext2_acl_entry_short
);
686 dst_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
690 src
+= sizeof(ext2_acl_entry
);
693 dst_entry
->e_id
= src_entry
->e_id
;
706 static char *xattr_prefix_table
[] = {
708 [2] = "system.posix_acl_access",
709 [3] = "system.posix_acl_default",
714 static int copy_single_xattr(struct btrfs_trans_handle
*trans
,
715 struct btrfs_root
*root
, u64 objectid
,
716 struct ext2_ext_attr_entry
*entry
,
717 const void *data
, u32 datalen
)
722 void *databuf
= NULL
;
723 char namebuf
[XATTR_NAME_MAX
+ 1];
725 name_index
= entry
->e_name_index
;
726 if (name_index
>= ARRAY_SIZE(xattr_prefix_table
) ||
727 xattr_prefix_table
[name_index
] == NULL
)
729 name_len
= strlen(xattr_prefix_table
[name_index
]) +
731 if (name_len
>= sizeof(namebuf
))
734 if (name_index
== 2 || name_index
== 3) {
735 size_t bufsize
= acl_ea_size(ext2_acl_count(datalen
));
736 databuf
= malloc(bufsize
);
739 ret
= ext2_acl_to_xattr(databuf
, data
, bufsize
, datalen
);
745 strcpy(namebuf
, xattr_prefix_table
[name_index
]);
746 strncat(namebuf
, EXT2_EXT_ATTR_NAME(entry
), entry
->e_name_len
);
747 if (name_len
+ datalen
> BTRFS_LEAF_DATA_SIZE(root
) -
748 sizeof(struct btrfs_item
) - sizeof(struct btrfs_dir_item
)) {
749 fprintf(stderr
, "skip large xattr on inode %Lu name %.*s\n",
750 objectid
- INO_OFFSET
, name_len
, namebuf
);
753 ret
= btrfs_insert_xattr_item(trans
, root
, namebuf
, name_len
,
754 data
, datalen
, objectid
);
761 static int copy_extended_attrs(struct btrfs_trans_handle
*trans
,
762 struct btrfs_root
*root
, u64 objectid
,
763 struct btrfs_inode_item
*btrfs_inode
,
764 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
770 u32 block_size
= ext2_fs
->blocksize
;
771 u32 inode_size
= EXT2_INODE_SIZE(ext2_fs
->super
);
772 struct ext2_inode_large
*ext2_inode
;
773 struct ext2_ext_attr_entry
*entry
;
776 char inode_buf
[EXT2_GOOD_OLD_INODE_SIZE
];
778 if (inode_size
<= EXT2_GOOD_OLD_INODE_SIZE
) {
779 ext2_inode
= (struct ext2_inode_large
*)inode_buf
;
781 ext2_inode
= (struct ext2_inode_large
*)malloc(inode_size
);
785 err
= ext2fs_read_inode_full(ext2_fs
, ext2_ino
, (void *)ext2_inode
,
788 fprintf(stderr
, "ext2fs_read_inode_full: %s\n",
794 if (ext2_ino
> ext2_fs
->super
->s_first_ino
&&
795 inode_size
> EXT2_GOOD_OLD_INODE_SIZE
) {
796 if (EXT2_GOOD_OLD_INODE_SIZE
+
797 ext2_inode
->i_extra_isize
> inode_size
) {
801 if (ext2_inode
->i_extra_isize
!= 0 &&
802 EXT2_XATTR_IHDR(ext2_inode
)->h_magic
==
803 EXT2_EXT_ATTR_MAGIC
) {
809 void *end
= (void *)ext2_inode
+ inode_size
;
810 entry
= EXT2_XATTR_IFIRST(ext2_inode
);
811 total
= end
- (void *)entry
;
812 ret
= ext2_xattr_check_names(entry
, end
);
815 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
816 ret
= ext2_xattr_check_entry(entry
, total
);
819 data
= (void *)EXT2_XATTR_IFIRST(ext2_inode
) +
821 datalen
= entry
->e_value_size
;
822 ret
= copy_single_xattr(trans
, root
, objectid
,
823 entry
, data
, datalen
);
826 entry
= EXT2_EXT_ATTR_NEXT(entry
);
830 if (ext2_inode
->i_file_acl
== 0)
833 buffer
= malloc(block_size
);
838 err
= ext2fs_read_ext_attr(ext2_fs
, ext2_inode
->i_file_acl
, buffer
);
840 fprintf(stderr
, "ext2fs_read_ext_attr: %s\n",
845 ret
= ext2_xattr_check_block(buffer
, block_size
);
849 entry
= EXT2_XATTR_BFIRST(buffer
);
850 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
851 ret
= ext2_xattr_check_entry(entry
, block_size
);
854 data
= buffer
+ entry
->e_value_offs
;
855 datalen
= entry
->e_value_size
;
856 ret
= copy_single_xattr(trans
, root
, objectid
,
857 entry
, data
, datalen
);
860 entry
= EXT2_EXT_ATTR_NEXT(entry
);
865 if ((void *)ext2_inode
!= inode_buf
)
870 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
872 static inline dev_t
old_decode_dev(u16 val
)
874 return MKDEV((val
>> 8) & 255, val
& 255);
877 static inline dev_t
new_decode_dev(u32 dev
)
879 unsigned major
= (dev
& 0xfff00) >> 8;
880 unsigned minor
= (dev
& 0xff) | ((dev
>> 12) & 0xfff00);
881 return MKDEV(major
, minor
);
884 static int copy_inode_item(struct btrfs_inode_item
*dst
,
885 struct ext2_inode
*src
)
887 btrfs_set_stack_inode_generation(dst
, 1);
888 btrfs_set_stack_inode_size(dst
, src
->i_size
);
889 btrfs_set_stack_inode_nblocks(dst
, src
->i_blocks
);
890 btrfs_set_stack_inode_block_group(dst
, 0);
891 btrfs_set_stack_inode_nblocks(dst
, 0);
892 btrfs_set_stack_inode_nlink(dst
, src
->i_links_count
);
893 btrfs_set_stack_inode_uid(dst
, src
->i_uid
| (src
->i_uid_high
<< 16));
894 btrfs_set_stack_inode_gid(dst
, src
->i_gid
| (src
->i_gid_high
<< 16));
895 btrfs_set_stack_inode_mode(dst
, src
->i_mode
);
896 btrfs_set_stack_inode_rdev(dst
, 0);
897 btrfs_set_stack_inode_flags(dst
, 0);
898 btrfs_set_stack_inode_compat_flags(dst
, 0);
899 btrfs_set_stack_timespec_sec(&dst
->atime
, src
->i_atime
);
900 btrfs_set_stack_timespec_nsec(&dst
->atime
, 0);
901 btrfs_set_stack_timespec_sec(&dst
->ctime
, src
->i_ctime
);
902 btrfs_set_stack_timespec_nsec(&dst
->ctime
, 0);
903 btrfs_set_stack_timespec_sec(&dst
->mtime
, src
->i_mtime
);
904 btrfs_set_stack_timespec_nsec(&dst
->mtime
, 0);
905 btrfs_set_stack_timespec_sec(&dst
->otime
, 0);
906 btrfs_set_stack_timespec_nsec(&dst
->otime
, 0);
908 if (S_ISDIR(src
->i_mode
)) {
909 btrfs_set_stack_inode_size(dst
, 0);
910 btrfs_set_stack_inode_nlink(dst
, 1);
912 if (!S_ISREG(src
->i_mode
) && !S_ISDIR(src
->i_mode
) &&
913 !S_ISLNK(src
->i_mode
)) {
914 if (src
->i_block
[0]) {
915 btrfs_set_stack_inode_rdev(dst
,
916 old_decode_dev(src
->i_block
[0]));
918 btrfs_set_stack_inode_rdev(dst
,
919 new_decode_dev(src
->i_block
[1]));
926 * copy a single inode. do all the required works, such as cloning
927 * inode item, creating file extents and creating directory entries.
929 static int copy_single_inode(struct btrfs_trans_handle
*trans
,
930 struct btrfs_root
*root
, u64 objectid
,
931 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
932 int datacsum
, int packing
, int noxattr
)
936 struct ext2_inode ext2_inode
;
937 struct btrfs_key inode_key
;
938 struct btrfs_inode_item btrfs_inode
;
940 err
= ext2fs_read_inode(ext2_fs
, ext2_ino
, &ext2_inode
);
944 if (!ext2_inode
.i_links_count
&&
945 (!ext2_inode
.i_mode
|| ext2_inode
.i_dtime
)) {
946 printf("skip inode %u\n", ext2_ino
);
949 copy_inode_item(&btrfs_inode
, &ext2_inode
);
950 if (!datacsum
&& S_ISREG(ext2_inode
.i_mode
)) {
951 u32 flags
= btrfs_stack_inode_flags(&btrfs_inode
) |
952 BTRFS_INODE_NODATASUM
;
953 btrfs_set_stack_inode_flags(&btrfs_inode
, flags
);
956 switch (ext2_inode
.i_mode
& S_IFMT
) {
958 ret
= create_file_extents(trans
, root
, objectid
, &btrfs_inode
,
959 ext2_fs
, ext2_ino
, datacsum
, packing
);
962 ret
= create_dir_entries(trans
, root
, objectid
, &btrfs_inode
,
966 ret
= create_symbol_link(trans
, root
, objectid
, &btrfs_inode
,
967 ext2_fs
, ext2_ino
, &ext2_inode
);
977 ret
= copy_extended_attrs(trans
, root
, objectid
, &btrfs_inode
,
982 inode_key
.objectid
= objectid
;
983 inode_key
.offset
= 0;
984 btrfs_set_key_type(&inode_key
, BTRFS_INODE_ITEM_KEY
);
985 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
988 fprintf(stderr
, "ext2fs_read_inode: %s\n", error_message(err
));
992 static int copy_disk_extent(struct btrfs_root
*root
, u64 dst_bytenr
,
993 u64 src_bytenr
, u32 num_bytes
)
997 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
999 buffer
= malloc(num_bytes
);
1002 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, src_bytenr
);
1003 if (ret
!= num_bytes
)
1005 ret
= pwrite(fs_devs
->latest_bdev
, buffer
, num_bytes
, dst_bytenr
);
1006 if (ret
!= num_bytes
)
1016 * scan ext2's inode bitmap and copy all used inode.
1018 static int copy_inodes(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1019 int datacsum
, int packing
, int noxattr
)
1022 ext2_ino_t ext2_ino
;
1024 struct btrfs_trans_handle
*trans
;
1026 trans
= btrfs_start_transaction(root
, 1);
1029 ext2_ino
= ext2_fs
->inode_map
->start
;
1030 for (; ext2_ino
<= ext2_fs
->inode_map
->end
; ext2_ino
++) {
1031 if (ext2fs_fast_test_inode_bitmap(ext2_fs
->inode_map
,
1033 /* skip special inode in ext2fs */
1034 if (ext2_ino
< EXT2_GOOD_OLD_FIRST_INO
&&
1035 ext2_ino
!= EXT2_ROOT_INO
)
1037 objectid
= ext2_ino
+ INO_OFFSET
;
1038 ret
= copy_single_inode(trans
, root
,
1039 objectid
, ext2_fs
, ext2_ino
,
1040 datacsum
, packing
, noxattr
);
1044 if (trans
->blocks_used
>= 4096) {
1045 ret
= btrfs_commit_transaction(trans
, root
);
1047 trans
= btrfs_start_transaction(root
, 1);
1051 ret
= btrfs_commit_transaction(trans
, root
);
1057 static int lookup_extent_item(struct btrfs_trans_handle
*trans
,
1058 struct btrfs_root
*root
,
1059 u64 bytenr
, u64 num_bytes
)
1062 struct btrfs_key key
;
1063 struct btrfs_path path
;
1065 btrfs_init_path(&path
);
1066 key
.objectid
= bytenr
;
1067 key
.offset
= (u64
)-1;
1068 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1069 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
1075 ret
= btrfs_previous_item(root
, &path
, 0, BTRFS_EXTENT_ITEM_KEY
);
1079 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
1080 if (key
.objectid
+ key
.offset
< bytenr
+ num_bytes
)
1083 btrfs_release_path(root
, &path
);
1087 * Construct a range of ext2fs image file.
1088 * scan block allocation bitmap, find all blocks used by the ext2fs
1089 * in this range and create file extents that point to these blocks.
1091 * Note: Before calling the function, no file extent points to blocks
1094 static int create_image_file_range(struct btrfs_trans_handle
*trans
,
1095 struct btrfs_root
*root
, u64 objectid
,
1096 struct btrfs_inode_item
*inode
,
1097 u64 start_byte
, u64 end_byte
,
1098 ext2_filsys ext2_fs
)
1101 u32 blocksize
= ext2_fs
->blocksize
;
1102 u32 block
= start_byte
/ blocksize
;
1103 u32 last_block
= (end_byte
+ blocksize
- 1) / blocksize
;
1105 struct blk_iterate_data data
= {
1109 .objectid
= objectid
,
1110 .first_block
= block
,
1113 .boundary
= (u64
)-1,
1117 for (; start_byte
< end_byte
; block
++, start_byte
+= blocksize
) {
1118 if (!ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
1120 /* the bit may be set by us, check extent tree */
1121 bytenr
= (u64
)block
* blocksize
;
1122 ret
= lookup_extent_item(trans
, root
, bytenr
, blocksize
);
1128 ret
= block_iterate_proc(NULL
, block
, block
, &data
);
1129 if (ret
& BLOCK_ABORT
) {
1134 if (data
.num_blocks
> 0) {
1135 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1136 data
.first_block
, data
.disk_block
,
1137 data
.num_blocks
, 0);
1140 data
.first_block
+= data
.num_blocks
;
1142 if (last_block
> data
.first_block
) {
1143 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1144 data
.first_block
, 0, last_block
-
1145 data
.first_block
, 0);
1153 * Create the ext2fs image file.
1155 static int create_ext2_image(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1159 struct btrfs_key key
;
1160 struct btrfs_key location
;
1161 struct btrfs_path path
;
1162 struct btrfs_inode_item btrfs_inode
;
1163 struct btrfs_inode_item
*inode_item
;
1164 struct extent_buffer
*leaf
;
1165 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1166 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
1167 struct btrfs_trans_handle
*trans
;
1168 struct btrfs_extent_ref
*ref_item
;
1177 u32 sectorsize
= root
->sectorsize
;
1180 total_bytes
= btrfs_super_total_bytes(&fs_info
->super_copy
);
1181 first_free
= BTRFS_SUPER_INFO_OFFSET
+ sectorsize
* 2 - 1;
1182 first_free
&= ~((u64
)sectorsize
- 1);
1184 memset(&btrfs_inode
, 0, sizeof(btrfs_inode
));
1185 btrfs_set_stack_inode_generation(&btrfs_inode
, 1);
1186 btrfs_set_stack_inode_size(&btrfs_inode
, total_bytes
);
1187 btrfs_set_stack_inode_nlink(&btrfs_inode
, 1);
1188 btrfs_set_stack_inode_nblocks(&btrfs_inode
, 0);
1189 btrfs_set_stack_inode_mode(&btrfs_inode
, S_IFREG
| 0400);
1190 btrfs_set_stack_inode_flags(&btrfs_inode
, BTRFS_INODE_NODATASUM
|
1191 BTRFS_INODE_READONLY
);
1192 btrfs_init_path(&path
);
1193 trans
= btrfs_start_transaction(root
, 1);
1196 objectid
= btrfs_root_dirid(&root
->root_item
);
1197 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1202 * copy blocks covered by extent #0 to new positions. extent #0 is
1203 * special, we can't rely on relocate_extents_range to relocate it.
1205 for (last_byte
= 0; last_byte
< first_free
; last_byte
+= sectorsize
) {
1206 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1209 ret
= copy_disk_extent(root
, key
.objectid
, last_byte
,
1213 ret
= record_file_extent(trans
, root
, objectid
,
1214 &btrfs_inode
, last_byte
,
1215 key
.objectid
, sectorsize
, 0);
1221 key
.objectid
= last_byte
;
1223 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1224 ret
= btrfs_search_slot(trans
, fs_info
->extent_root
,
1229 leaf
= path
.nodes
[0];
1230 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1231 ret
= btrfs_next_leaf(extent_root
, &path
);
1236 leaf
= path
.nodes
[0];
1238 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1239 if (last_byte
> key
.objectid
||
1240 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1245 * Check backref to distinguish extent items for normal
1246 * files (files that correspond to files in Ext2fs) from
1247 * extent items for ctree blocks.
1249 bytenr
= key
.objectid
;
1250 num_bytes
= key
.offset
;
1253 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1254 ret
= btrfs_next_leaf(extent_root
, &path
);
1259 leaf
= path
.nodes
[0];
1261 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1262 if (key
.objectid
!= bytenr
)
1264 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1268 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1269 struct btrfs_extent_ref
);
1270 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1271 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1272 if ((ref_root
== BTRFS_FS_TREE_OBJECTID
) &&
1273 (ref_owner
>= BTRFS_FIRST_FREE_OBJECTID
)) {
1282 if (bytenr
> last_byte
) {
1283 ret
= create_image_file_range(trans
, root
, objectid
,
1284 &btrfs_inode
, last_byte
,
1289 ret
= record_file_extent(trans
, root
, objectid
, &btrfs_inode
,
1290 bytenr
, bytenr
, num_bytes
, 0);
1293 last_byte
= bytenr
+ num_bytes
;
1294 btrfs_release_path(extent_root
, &path
);
1296 if (trans
->blocks_used
>= 4096) {
1297 ret
= btrfs_commit_transaction(trans
, root
);
1299 trans
= btrfs_start_transaction(root
, 1);
1303 btrfs_release_path(root
, &path
);
1304 if (total_bytes
> last_byte
) {
1305 ret
= create_image_file_range(trans
, root
, objectid
,
1306 &btrfs_inode
, last_byte
,
1307 total_bytes
, ext2_fs
);
1312 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1316 location
.objectid
= objectid
;
1317 location
.offset
= 0;
1318 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1319 ret
= btrfs_insert_dir_item(trans
, root
, name
, strlen(name
),
1320 btrfs_root_dirid(&root
->root_item
),
1321 &location
, EXT2_FT_REG_FILE
);
1324 ret
= btrfs_insert_inode_ref(trans
, root
, name
, strlen(name
),
1326 btrfs_root_dirid(&root
->root_item
));
1329 location
.objectid
= btrfs_root_dirid(&root
->root_item
);
1330 location
.offset
= 0;
1331 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1332 ret
= btrfs_lookup_inode(trans
, root
, &path
, &location
, 1);
1335 leaf
= path
.nodes
[0];
1336 inode_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1337 struct btrfs_inode_item
);
1338 btrfs_set_inode_size(leaf
, inode_item
, strlen(name
) * 2 +
1339 btrfs_inode_size(leaf
, inode_item
));
1340 btrfs_mark_buffer_dirty(leaf
);
1341 btrfs_release_path(root
, &path
);
1342 ret
= btrfs_commit_transaction(trans
, root
);
1345 btrfs_release_path(root
, &path
);
1349 struct btrfs_root
*create_subvol(struct btrfs_root
*root
, const char *name
)
1353 struct btrfs_key location
;
1354 struct btrfs_root_item root_item
;
1355 struct btrfs_trans_handle
*trans
;
1356 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1357 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1358 struct btrfs_root
*new_root
;
1359 struct extent_buffer
*tmp
;
1361 trans
= btrfs_start_transaction(root
, 1);
1364 objectid
= btrfs_super_root_dir(&fs_info
->super_copy
);
1365 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1368 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
1371 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
1372 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
1373 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
1374 free_extent_buffer(tmp
);
1376 location
.objectid
= objectid
;
1377 location
.offset
= 1;
1378 btrfs_set_key_type(&location
, BTRFS_ROOT_ITEM_KEY
);
1379 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1380 &location
, &root_item
);
1383 location
.offset
= (u64
)-1;
1384 ret
= btrfs_insert_dir_item(trans
, tree_root
, name
, strlen(name
),
1385 btrfs_super_root_dir(&fs_info
->super_copy
),
1386 &location
, BTRFS_FT_DIR
);
1389 ret
= btrfs_insert_inode_ref(trans
, tree_root
, name
, strlen(name
),
1391 btrfs_super_root_dir(&fs_info
->super_copy
));
1394 ret
= btrfs_commit_transaction(trans
, root
);
1396 new_root
= btrfs_read_fs_root(fs_info
, &location
);
1397 if (!new_root
|| IS_ERR(new_root
))
1399 trans
= btrfs_start_transaction(new_root
, 1);
1401 ret
= btrfs_make_root_dir(trans
, new_root
, BTRFS_FIRST_FREE_OBJECTID
);
1404 ret
= btrfs_commit_transaction(trans
, new_root
);
1411 * Fixup block accounting. The initial block accounting created by
1412 * make_block_groups isn't accuracy in this case.
1414 static int fixup_block_accounting(struct btrfs_trans_handle
*trans
,
1415 struct btrfs_root
*root
)
1421 struct btrfs_path path
;
1422 struct btrfs_key key
;
1423 struct extent_buffer
*leaf
;
1424 struct btrfs_block_group_cache
*cache
;
1425 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1428 cache
= btrfs_lookup_block_group(fs_info
, start
);
1431 start
= cache
->key
.objectid
+ cache
->key
.offset
;
1432 btrfs_set_block_group_used(&cache
->item
, 0);
1433 cache
->space_info
->bytes_used
= 0;
1436 btrfs_init_path(&path
);
1439 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1440 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
1445 leaf
= path
.nodes
[0];
1446 slot
= path
.slots
[0];
1447 if (slot
>= btrfs_header_nritems(leaf
)) {
1448 ret
= btrfs_next_leaf(root
, &path
);
1453 leaf
= path
.nodes
[0];
1454 slot
= path
.slots
[0];
1456 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1457 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
1458 bytes_used
+= key
.offset
;
1459 ret
= btrfs_update_block_group(trans
, root
,
1460 key
.objectid
, key
.offset
, 1, 0);
1465 btrfs_set_super_bytes_used(&root
->fs_info
->super_copy
, bytes_used
);
1466 btrfs_release_path(root
, &path
);
1470 static int create_chunk_mapping(struct btrfs_trans_handle
*trans
,
1471 struct btrfs_root
*root
)
1473 struct btrfs_fs_info
*info
= root
->fs_info
;
1474 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1475 struct btrfs_root
*extent_root
= info
->extent_root
;
1476 struct btrfs_device
*device
;
1477 struct btrfs_block_group_cache
*cache
;
1478 struct btrfs_dev_extent
*extent
;
1479 struct extent_buffer
*leaf
;
1480 struct btrfs_chunk chunk
;
1481 struct btrfs_key key
;
1482 struct btrfs_path path
;
1488 btrfs_init_path(&path
);
1490 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1491 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1493 BUG_ON(list_empty(&info
->fs_devices
->devices
));
1494 device
= list_entry(info
->fs_devices
->devices
.next
,
1495 struct btrfs_device
, dev_list
);
1496 BUG_ON(device
->devid
!= info
->fs_devices
->latest_devid
);
1498 /* delete device extent created by make_btrfs */
1499 key
.objectid
= device
->devid
;
1501 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1502 ret
= btrfs_search_slot(trans
, device
->dev_root
, &key
, &path
, -1, 1);
1507 ret
= btrfs_del_item(trans
, device
->dev_root
, &path
);
1510 btrfs_release_path(device
->dev_root
, &path
);
1512 /* delete chunk item created by make_btrfs */
1513 key
.objectid
= chunk_objectid
;
1515 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1516 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, -1, 1);
1521 ret
= btrfs_del_item(trans
, chunk_root
, &path
);
1524 btrfs_release_path(chunk_root
, &path
);
1526 /* for each block group, create device extent and chunk item */
1528 while (cur_start
< total_bytes
) {
1529 cache
= btrfs_lookup_block_group(root
->fs_info
, cur_start
);
1532 /* insert device extent */
1533 key
.objectid
= device
->devid
;
1534 key
.offset
= cache
->key
.objectid
;
1535 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1536 ret
= btrfs_insert_empty_item(trans
, device
->dev_root
, &path
,
1537 &key
, sizeof(*extent
));
1541 leaf
= path
.nodes
[0];
1542 extent
= btrfs_item_ptr(leaf
, path
.slots
[0],
1543 struct btrfs_dev_extent
);
1545 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1546 chunk_root
->root_key
.objectid
);
1547 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1549 btrfs_set_dev_extent_chunk_offset(leaf
, extent
,
1550 cache
->key
.objectid
);
1551 btrfs_set_dev_extent_length(leaf
, extent
, cache
->key
.offset
);
1552 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1553 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1555 btrfs_mark_buffer_dirty(leaf
);
1556 btrfs_release_path(device
->dev_root
, &path
);
1558 /* insert chunk item */
1559 btrfs_set_stack_chunk_length(&chunk
, cache
->key
.offset
);
1560 btrfs_set_stack_chunk_owner(&chunk
,
1561 extent_root
->root_key
.objectid
);
1562 btrfs_set_stack_chunk_stripe_len(&chunk
, 64 * 1024);
1563 btrfs_set_stack_chunk_type(&chunk
, cache
->flags
);
1564 btrfs_set_stack_chunk_io_align(&chunk
, device
->io_align
);
1565 btrfs_set_stack_chunk_io_width(&chunk
, device
->io_width
);
1566 btrfs_set_stack_chunk_sector_size(&chunk
, device
->sector_size
);
1567 btrfs_set_stack_chunk_num_stripes(&chunk
, 1);
1568 btrfs_set_stack_chunk_sub_stripes(&chunk
, 0);
1569 btrfs_set_stack_stripe_devid(&chunk
.stripe
, device
->devid
);
1570 btrfs_set_stack_stripe_offset(&chunk
.stripe
,
1571 cache
->key
.objectid
);
1572 memcpy(&chunk
.stripe
.dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1574 key
.objectid
= chunk_objectid
;
1575 key
.offset
= cache
->key
.objectid
;
1576 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1578 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, &chunk
,
1579 btrfs_chunk_item_size(1));
1583 cur_start
= cache
->key
.objectid
+ cache
->key
.offset
;
1586 device
->bytes_used
= total_bytes
;
1587 ret
= btrfs_update_device(trans
, device
);
1589 btrfs_release_path(device
->dev_root
, &path
);
1593 static int init_btrfs(struct btrfs_root
*root
)
1596 struct btrfs_key location
;
1597 struct btrfs_trans_handle
*trans
;
1598 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1600 trans
= btrfs_start_transaction(root
, 1);
1602 ret
= btrfs_make_block_groups(trans
, root
);
1605 ret
= fixup_block_accounting(trans
, root
);
1608 ret
= create_chunk_mapping(trans
, root
);
1611 ret
= btrfs_make_root_dir(trans
, fs_info
->tree_root
,
1612 BTRFS_ROOT_TREE_DIR_OBJECTID
);
1615 memcpy(&location
, &root
->root_key
, sizeof(location
));
1616 location
.offset
= (u64
)-1;
1617 ret
= btrfs_insert_dir_item(trans
, fs_info
->tree_root
, "default", 7,
1618 btrfs_super_root_dir(&fs_info
->super_copy
),
1619 &location
, BTRFS_FT_DIR
);
1622 ret
= btrfs_insert_inode_ref(trans
, fs_info
->tree_root
, "default", 7,
1624 btrfs_super_root_dir(&fs_info
->super_copy
));
1627 btrfs_set_root_dirid(&fs_info
->fs_root
->root_item
,
1628 BTRFS_FIRST_FREE_OBJECTID
);
1629 ret
= btrfs_commit_transaction(trans
, root
);
1636 * Migrate super block to it's default position and zero 0 ~ 16k
1638 static int migrate_super_block(int fd
, u64 old_bytenr
, u32 sectorsize
)
1641 struct extent_buffer
*buf
;
1642 struct btrfs_super_block
*super
;
1646 BUG_ON(sectorsize
< sizeof(*super
));
1647 buf
= malloc(sizeof(*buf
) + sectorsize
);
1651 buf
->len
= sectorsize
;
1652 ret
= pread(fd
, buf
->data
, sectorsize
, old_bytenr
);
1653 if (ret
!= sectorsize
)
1656 super
= (struct btrfs_super_block
*)buf
->data
;
1657 BUG_ON(btrfs_super_bytenr(super
) != old_bytenr
);
1658 btrfs_set_super_bytenr(super
, BTRFS_SUPER_INFO_OFFSET
);
1660 csum_tree_block(NULL
, buf
, 0);
1661 ret
= pwrite(fd
, buf
->data
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
1662 if (ret
!= sectorsize
)
1669 memset(buf
->data
, 0, sectorsize
);
1670 for (bytenr
= 0; bytenr
< BTRFS_SUPER_INFO_OFFSET
; ) {
1671 len
= BTRFS_SUPER_INFO_OFFSET
- bytenr
;
1672 if (len
> sectorsize
)
1674 ret
= pwrite(fd
, buf
->data
, len
, bytenr
);
1676 fprintf(stderr
, "unable to zero fill device\n");
1690 static int prepare_system_chunk_sb(struct btrfs_super_block
*super
)
1692 struct btrfs_chunk
*chunk
;
1693 struct btrfs_disk_key
*key
;
1694 u32 sectorsize
= btrfs_super_sectorsize(super
);
1696 key
= (struct btrfs_disk_key
*)(super
->sys_chunk_array
);
1697 chunk
= (struct btrfs_chunk
*)(super
->sys_chunk_array
+
1698 sizeof(struct btrfs_disk_key
));
1700 btrfs_set_disk_key_objectid(key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1701 btrfs_set_disk_key_type(key
, BTRFS_CHUNK_ITEM_KEY
);
1702 btrfs_set_disk_key_offset(key
, 0);
1704 btrfs_set_stack_chunk_length(chunk
, btrfs_super_total_bytes(super
));
1705 btrfs_set_stack_chunk_owner(chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
1706 btrfs_set_stack_chunk_stripe_len(chunk
, 64 * 1024);
1707 btrfs_set_stack_chunk_type(chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
1708 btrfs_set_stack_chunk_io_align(chunk
, sectorsize
);
1709 btrfs_set_stack_chunk_io_width(chunk
, sectorsize
);
1710 btrfs_set_stack_chunk_sector_size(chunk
, sectorsize
);
1711 btrfs_set_stack_chunk_num_stripes(chunk
, 1);
1712 btrfs_set_stack_chunk_sub_stripes(chunk
, 0);
1713 chunk
->stripe
.devid
= super
->dev_item
.devid
;
1714 chunk
->stripe
.offset
= cpu_to_le64(0);
1715 memcpy(chunk
->stripe
.dev_uuid
, super
->dev_item
.uuid
, BTRFS_UUID_SIZE
);
1716 btrfs_set_super_sys_array_size(super
, sizeof(*key
) + sizeof(*chunk
));
1720 static int prepare_system_chunk(int fd
, u64 sb_bytenr
, u32 sectorsize
)
1723 struct extent_buffer
*buf
;
1724 struct btrfs_super_block
*super
;
1726 BUG_ON(sectorsize
< sizeof(*super
));
1727 buf
= malloc(sizeof(*buf
) + sectorsize
);
1731 buf
->len
= sectorsize
;
1732 ret
= pread(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1733 if (ret
!= sectorsize
)
1736 super
= (struct btrfs_super_block
*)buf
->data
;
1737 BUG_ON(btrfs_super_bytenr(super
) != sb_bytenr
);
1738 BUG_ON(btrfs_super_num_devices(super
) != 1);
1740 ret
= prepare_system_chunk_sb(super
);
1744 csum_tree_block(NULL
, buf
, 0);
1745 ret
= pwrite(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1746 if (ret
!= sectorsize
)
1757 static int relocate_one_reference(struct btrfs_trans_handle
*trans
,
1758 struct btrfs_root
*root
,
1759 u64 objectid
, u64 offset
,
1760 struct extent_io_tree
*reloc_tree
)
1762 struct extent_buffer
*leaf
;
1763 struct btrfs_file_extent_item
*fi
;
1764 struct btrfs_key key
;
1765 struct btrfs_path path
;
1766 struct btrfs_inode_item inode
;
1767 struct blk_iterate_data data
;
1778 u32 sectorsize
= root
->sectorsize
;
1782 btrfs_init_path(&path
);
1784 key
.objectid
= objectid
;
1786 key
.type
= BTRFS_INODE_ITEM_KEY
;
1787 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 0);
1791 leaf
= path
.nodes
[0];
1792 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1793 read_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1794 btrfs_release_path(root
, &path
);
1796 ret
= btrfs_lookup_file_extent(trans
, root
, &path
,
1797 objectid
, offset
, -1);
1801 leaf
= path
.nodes
[0];
1802 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
1803 struct btrfs_file_extent_item
);
1805 root_gen
= btrfs_header_generation(leaf
);
1806 root_owner
= btrfs_header_owner(leaf
);
1808 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1809 extent_size
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1810 bytenr
= extent_start
+ btrfs_file_extent_offset(leaf
, fi
);
1811 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
1813 ret
= btrfs_del_item(trans
, root
, &path
);
1816 btrfs_release_path(root
, &path
);
1818 BUG_ON(num_bytes
& (sectorsize
- 1));
1819 nblocks
= btrfs_stack_inode_nblocks(&inode
) - num_bytes
/ 512;
1820 btrfs_set_stack_inode_nblocks(&inode
, nblocks
);
1822 data
= (struct blk_iterate_data
) {
1826 .objectid
= objectid
,
1827 .first_block
= offset
/ sectorsize
,
1830 .boundary
= (u64
)-1,
1835 cur_offset
= offset
;
1836 while (num_bytes
> 0) {
1837 sector_end
= bytenr
+ sectorsize
- 1;
1838 if (test_range_bit(reloc_tree
, bytenr
, sector_end
,
1839 EXTENT_LOCKED
, 1)) {
1840 ret
= get_state_private(reloc_tree
, bytenr
, &new_pos
);
1843 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1846 new_pos
= key
.objectid
;
1847 ret
= copy_disk_extent(root
, new_pos
, bytenr
,
1851 ret
= set_extent_bits(reloc_tree
, bytenr
, sector_end
,
1852 EXTENT_LOCKED
, GFP_NOFS
);
1854 ret
= set_state_private(reloc_tree
, bytenr
, new_pos
);
1858 ret
= block_iterate_proc(NULL
, new_pos
/ sectorsize
,
1859 cur_offset
/ sectorsize
, &data
);
1860 if (ret
& BLOCK_ABORT
) {
1865 cur_offset
+= sectorsize
;
1866 bytenr
+= sectorsize
;
1867 num_bytes
-= sectorsize
;
1870 if (data
.num_blocks
> 0) {
1871 ret
= record_file_blocks(trans
, root
, objectid
, &inode
,
1872 data
.first_block
, data
.disk_block
,
1873 data
.num_blocks
, 0);
1878 key
.objectid
= objectid
;
1880 key
.type
= BTRFS_INODE_ITEM_KEY
;
1881 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 1);
1885 leaf
= path
.nodes
[0];
1886 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1887 write_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1888 btrfs_mark_buffer_dirty(leaf
);
1889 btrfs_release_path(root
, &path
);
1891 ret
= btrfs_free_extent(trans
, root
, extent_start
, extent_size
,
1892 root_owner
, root_gen
, objectid
, offset
, 0);
1894 btrfs_release_path(root
, &path
);
1898 static int relocate_extents_range(struct btrfs_root
*fs_root
,
1899 struct btrfs_root
*ext2_root
,
1900 u64 start_byte
, u64 end_byte
)
1902 struct btrfs_fs_info
*info
= fs_root
->fs_info
;
1903 struct btrfs_root
*extent_root
= info
->extent_root
;
1904 struct btrfs_root
*cur_root
;
1905 struct btrfs_trans_handle
*trans
;
1906 struct btrfs_extent_ref
*ref_item
;
1907 struct extent_buffer
*leaf
;
1908 struct btrfs_key key
;
1909 struct btrfs_path path
;
1910 struct extent_io_tree reloc_tree
;
1921 btrfs_init_path(&path
);
1922 extent_io_tree_init(&reloc_tree
);
1924 cur_root
= (pass
% 2 == 0) ? ext2_root
: fs_root
;
1927 trans
= btrfs_start_transaction(cur_root
, 1);
1930 cur_byte
= start_byte
;
1932 key
.objectid
= cur_byte
;
1934 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1935 ret
= btrfs_search_slot(trans
, extent_root
,
1940 leaf
= path
.nodes
[0];
1941 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1942 ret
= btrfs_next_leaf(extent_root
, &path
);
1947 leaf
= path
.nodes
[0];
1950 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1951 if (key
.objectid
< cur_byte
||
1952 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1956 if (key
.objectid
>= end_byte
)
1959 cur_byte
= key
.objectid
;
1960 num_bytes
= key
.offset
;
1963 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1964 ret
= btrfs_next_leaf(extent_root
, &path
);
1969 leaf
= path
.nodes
[0];
1972 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1973 if (key
.objectid
!= cur_byte
)
1975 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1979 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1980 struct btrfs_extent_ref
);
1981 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1982 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1983 ref_offset
= btrfs_ref_offset(leaf
, ref_item
);
1986 BUG_ON(ref_owner
< BTRFS_FIRST_FREE_OBJECTID
);
1987 if (ref_root
== cur_root
->root_key
.objectid
)
1995 cur_byte
+= num_bytes
;
1996 btrfs_release_path(extent_root
, &path
);
1998 ret
= relocate_one_reference(trans
, cur_root
, ref_owner
,
1999 ref_offset
, &reloc_tree
);
2003 if (trans
->blocks_used
>= 4096) {
2004 ret
= btrfs_commit_transaction(trans
, cur_root
);
2006 trans
= btrfs_start_transaction(cur_root
, 1);
2010 btrfs_release_path(cur_root
, &path
);
2012 ret
= btrfs_commit_transaction(trans
, cur_root
);
2015 if (num_refs
> 0 && pass
++ < 4)
2018 ret
= (num_refs
> 0) ? -1 : 0;
2020 btrfs_release_path(cur_root
, &path
);
2021 extent_io_tree_cleanup(&reloc_tree
);
2026 * relocate data in system chunk
2028 static int cleanup_sys_chunk(struct btrfs_root
*fs_root
,
2029 struct btrfs_root
*ext2_root
)
2031 struct btrfs_block_group_cache
*cache
;
2037 cache
= btrfs_lookup_block_group(fs_root
->fs_info
, offset
);
2041 end_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
2042 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2043 ret
= relocate_extents_range(fs_root
, ext2_root
,
2044 cache
->key
.objectid
,
2055 static int fixup_chunk_mapping(struct btrfs_root
*root
)
2057 struct btrfs_trans_handle
*trans
;
2058 struct btrfs_fs_info
*info
= root
->fs_info
;
2059 struct btrfs_root
*chunk_root
= info
->chunk_root
;
2060 struct extent_buffer
*leaf
;
2061 struct btrfs_key key
;
2062 struct btrfs_path path
;
2063 struct btrfs_chunk chunk
;
2069 btrfs_init_path(&path
);
2071 trans
= btrfs_start_transaction(root
, 1);
2075 * recow the whole chunk tree. this will move all chunk tree blocks
2076 * into system block group.
2078 memset(&key
, 0, sizeof(key
));
2080 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2084 ret
= btrfs_next_leaf(chunk_root
, &path
);
2090 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2091 btrfs_release_path(chunk_root
, &path
);
2093 btrfs_release_path(chunk_root
, &path
);
2095 /* fixup the system chunk array in super block */
2096 btrfs_set_super_sys_array_size(&info
->super_copy
, 0);
2098 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2100 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2102 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 0);
2107 leaf
= path
.nodes
[0];
2108 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2109 ret
= btrfs_next_leaf(chunk_root
, &path
);
2114 leaf
= path
.nodes
[0];
2116 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2117 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
)
2120 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
2121 size
= btrfs_item_size_nr(leaf
, path
.slots
[0]);
2122 BUG_ON(size
!= sizeof(chunk
));
2123 read_extent_buffer(leaf
, &chunk
, ptr
, size
);
2124 type
= btrfs_stack_chunk_type(&chunk
);
2126 if (!(type
& BTRFS_BLOCK_GROUP_SYSTEM
))
2129 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
2137 ret
= btrfs_commit_transaction(trans
, root
);
2140 btrfs_release_path(chunk_root
, &path
);
2144 int do_convert(const char *devname
, int datacsum
, int packing
, int noxattr
)
2151 ext2_filsys ext2_fs
;
2152 struct btrfs_root
*root
;
2153 struct btrfs_root
*ext2_root
;
2155 ret
= open_ext2fs(devname
, &ext2_fs
);
2157 fprintf(stderr
, "unable to open the Ext2fs\n");
2160 blocksize
= ext2_fs
->blocksize
;
2161 total_bytes
= (u64
)ext2_fs
->super
->s_blocks_count
* blocksize
;
2162 if (blocksize
< 4096) {
2163 fprintf(stderr
, "block size is too small\n");
2166 if (!(ext2_fs
->super
->s_feature_incompat
&
2167 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
2168 fprintf(stderr
, "filetype feature is missing\n");
2171 for (i
= 0; i
< 6; i
++) {
2172 ret
= ext2_alloc_block(ext2_fs
, 0, blocks
+ i
);
2174 fprintf(stderr
, "not enough free space\n");
2177 blocks
[i
] *= blocksize
;
2179 super_bytenr
= blocks
[0];
2180 fd
= open(devname
, O_RDWR
);
2182 fprintf(stderr
, "unable to open %s\n", devname
);
2185 ret
= make_btrfs(fd
, devname
, ext2_fs
->super
->s_volume_name
,
2186 blocks
, total_bytes
, blocksize
, blocksize
,
2187 blocksize
, blocksize
);
2189 fprintf(stderr
, "unable to create initial ctree\n");
2192 /* create a system chunk that maps the whole device */
2193 ret
= prepare_system_chunk(fd
, super_bytenr
, blocksize
);
2195 fprintf(stderr
, "unable to update system chunk\n");
2198 root
= open_ctree_fd(fd
, devname
, super_bytenr
);
2200 fprintf(stderr
, "unable to open ctree\n");
2204 root
->fs_info
->priv_data
= ext2_fs
;
2205 root
->fs_info
->extent_ops
= &extent_ops
;
2206 ret
= init_btrfs(root
);
2208 fprintf(stderr
, "unable to setup the root tree\n");
2211 ext2_root
= create_subvol(root
, "ext2_saved");
2213 fprintf(stderr
, "unable to create subvol\n");
2216 printf("creating btrfs metadata.\n");
2217 ret
= copy_inodes(root
, ext2_fs
, datacsum
, packing
, noxattr
);
2219 fprintf(stderr
, "error during copy_inodes %d\n", ret
);
2222 printf("creating ext2fs image file.\n");
2223 ret
= create_ext2_image(ext2_root
, ext2_fs
, "image");
2225 fprintf(stderr
, "error during create_ext2_image %d\n", ret
);
2228 printf("cleaning up system chunk.\n");
2229 ret
= cleanup_sys_chunk(root
, ext2_root
);
2231 fprintf(stderr
, "error during cleanup_sys_chunk %d\n", ret
);
2234 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2235 ret
= close_ctree(root
);
2237 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2240 close_ext2fs(ext2_fs
);
2243 * If this step succeed, we get a mountable btrfs. Otherwise
2244 * the ext2fs is left unchanged.
2246 ret
= migrate_super_block(fd
, super_bytenr
, blocksize
);
2248 fprintf(stderr
, "unable to migrate super block\n");
2252 root
= open_ctree_fd(fd
, devname
, 0);
2254 fprintf(stderr
, "unable to open ctree\n");
2257 /* move chunk tree into system chunk. */
2258 ret
= fixup_chunk_mapping(root
);
2260 fprintf(stderr
, "error during fixup_chunk_tree\n");
2263 ret
= close_ctree(root
);
2266 printf("conversion complete.\n");
2269 fprintf(stderr
, "conversion aborted.\n");
2273 static int may_rollback(struct btrfs_root
*root
)
2275 struct btrfs_fs_info
*info
= root
->fs_info
;
2276 struct btrfs_multi_bio
*multi
= NULL
;
2284 if (btrfs_super_num_devices(&info
->super_copy
) != 1)
2287 bytenr
= BTRFS_SUPER_INFO_OFFSET
;
2288 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2291 ret
= btrfs_map_block(&info
->mapping_tree
, WRITE
, bytenr
,
2292 &length
, &multi
, 0);
2296 num_stripes
= multi
->num_stripes
;
2297 physical
= multi
->stripes
[0].physical
;
2300 if (num_stripes
!= 1 || physical
!= bytenr
)
2304 if (bytenr
>= total_bytes
)
2312 int do_rollback(const char *devname
, int force
)
2316 struct btrfs_root
*root
;
2317 struct btrfs_root
*ext2_root
;
2318 struct btrfs_root
*chunk_root
;
2319 struct btrfs_dir_item
*dir
;
2320 struct btrfs_inode_item
*inode
;
2321 struct btrfs_file_extent_item
*fi
;
2322 struct btrfs_trans_handle
*trans
;
2323 struct extent_buffer
*leaf
;
2324 struct btrfs_block_group_cache
*cache1
;
2325 struct btrfs_block_group_cache
*cache2
;
2326 struct btrfs_key key
;
2327 struct btrfs_path path
;
2328 struct extent_io_tree io_tree
;
2343 extent_io_tree_init(&io_tree
);
2345 fd
= open(devname
, O_RDWR
);
2347 fprintf(stderr
, "unable to open %s\n", devname
);
2350 root
= open_ctree_fd(fd
, devname
, 0);
2352 fprintf(stderr
, "unable to open ctree\n");
2355 ret
= may_rollback(root
);
2357 fprintf(stderr
, "unable to do rollback\n");
2361 sectorsize
= root
->sectorsize
;
2362 buf
= malloc(sectorsize
);
2364 fprintf(stderr
, "unable to allocate memory\n");
2368 btrfs_init_path(&path
);
2369 name
= "ext2_saved";
2370 root_dir
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2371 dir
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, &path
,
2372 root_dir
, name
, strlen(name
), 0);
2373 if (!dir
|| IS_ERR(dir
)) {
2374 fprintf(stderr
, "unable to find subvol %s\n", name
);
2377 leaf
= path
.nodes
[0];
2378 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2379 btrfs_release_path(root
->fs_info
->tree_root
, &path
);
2381 ext2_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2382 if (!ext2_root
|| IS_ERR(ext2_root
)) {
2383 fprintf(stderr
, "unable to open subvol %s\n", name
);
2388 root_dir
= btrfs_root_dirid(&root
->root_item
);
2389 dir
= btrfs_lookup_dir_item(NULL
, ext2_root
, &path
,
2390 root_dir
, name
, strlen(name
), 0);
2391 if (!dir
|| IS_ERR(dir
)) {
2392 fprintf(stderr
, "unable to find file %s\n", name
);
2395 leaf
= path
.nodes
[0];
2396 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2397 btrfs_release_path(ext2_root
, &path
);
2399 objectid
= key
.objectid
;
2401 ret
= btrfs_lookup_inode(NULL
, ext2_root
, &path
, &key
, 0);
2403 fprintf(stderr
, "unable to find inode item\n");
2406 leaf
= path
.nodes
[0];
2407 inode
= btrfs_item_ptr(leaf
, path
.slots
[0], struct btrfs_inode_item
);
2408 total_bytes
= btrfs_inode_size(leaf
, inode
);
2409 btrfs_release_path(ext2_root
, &path
);
2411 key
.objectid
= objectid
;
2413 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2414 ret
= btrfs_search_slot(NULL
, ext2_root
, &key
, &path
, 0, 0);
2416 fprintf(stderr
, "unable to find first file extent\n");
2417 btrfs_release_path(ext2_root
, &path
);
2421 /* build mapping tree for the relocated blocks */
2422 for (offset
= 0; offset
< total_bytes
; ) {
2423 leaf
= path
.nodes
[0];
2424 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2425 ret
= btrfs_next_leaf(root
, &path
);
2431 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2432 if (key
.objectid
!= objectid
|| key
.offset
!= offset
||
2433 btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2436 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
2437 struct btrfs_file_extent_item
);
2438 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2441 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2442 /* skip holes and direct mapped extents */
2443 if (bytenr
== 0 || bytenr
== offset
)
2446 bytenr
+= btrfs_file_extent_offset(leaf
, fi
);
2447 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
2449 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2450 cache2
= btrfs_lookup_block_group(root
->fs_info
,
2451 offset
+ num_bytes
- 1);
2452 if (!cache1
|| cache1
!= cache2
||
2453 !(cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
))
2456 set_extent_bits(&io_tree
, offset
, offset
+ num_bytes
- 1,
2457 EXTENT_LOCKED
, GFP_NOFS
);
2458 set_state_private(&io_tree
, offset
, bytenr
);
2460 offset
+= btrfs_file_extent_num_bytes(leaf
, fi
);
2463 btrfs_release_path(ext2_root
, &path
);
2464 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2466 if (offset
< total_bytes
) {
2467 fprintf(stderr
, "unable to build extent mapping\n");
2471 first_free
= BTRFS_SUPER_INFO_OFFSET
+ 2 * sectorsize
- 1;
2472 first_free
&= ~((u64
)sectorsize
- 1);
2473 /* backup for extent #0 should exist */
2474 if(!test_range_bit(&io_tree
, 0, first_free
- 1, EXTENT_LOCKED
, 1)) {
2475 fprintf(stderr
, "no backup for the first extent\n");
2478 /* force no allocation from system block group */
2479 root
->fs_info
->system_allocs
= -1;
2480 trans
= btrfs_start_transaction(root
, 1);
2483 * recow the whole chunk tree, this will remove all chunk tree blocks
2484 * from system block group
2486 chunk_root
= root
->fs_info
->chunk_root
;
2487 memset(&key
, 0, sizeof(key
));
2489 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2493 ret
= btrfs_next_leaf(chunk_root
, &path
);
2497 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2498 btrfs_release_path(chunk_root
, &path
);
2500 btrfs_release_path(chunk_root
, &path
);
2505 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2509 if (cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2510 num_bytes
+= btrfs_block_group_used(&cache1
->item
);
2512 offset
= cache1
->key
.objectid
+ cache1
->key
.offset
;
2514 /* only extent #0 left in system block group? */
2515 if (num_bytes
> first_free
) {
2516 fprintf(stderr
, "unable to empty system block group\n");
2519 /* create a system chunk that maps the whole device */
2520 ret
= prepare_system_chunk_sb(&root
->fs_info
->super_copy
);
2522 fprintf(stderr
, "unable to update system chunk\n");
2526 ret
= btrfs_commit_transaction(trans
, root
);
2529 ret
= close_ctree(root
);
2531 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2535 sb_bytenr
= (u64
)-1;
2536 /* copy all relocated blocks back */
2538 ret
= find_first_extent_bit(&io_tree
, 0, &start
, &end
,
2543 ret
= get_state_private(&io_tree
, start
, &bytenr
);
2546 clear_extent_bits(&io_tree
, start
, end
, EXTENT_LOCKED
,
2549 while (start
<= end
) {
2550 if (start
== BTRFS_SUPER_INFO_OFFSET
) {
2554 ret
= pread(fd
, buf
, sectorsize
, bytenr
);
2556 fprintf(stderr
, "error during pread %d\n", ret
);
2559 BUG_ON(ret
!= sectorsize
);
2560 ret
= pwrite(fd
, buf
, sectorsize
, start
);
2562 fprintf(stderr
, "error during pwrite %d\n", ret
);
2565 BUG_ON(ret
!= sectorsize
);
2567 start
+= sectorsize
;
2568 bytenr
+= sectorsize
;
2574 fprintf(stderr
, "error during fsync %d\n", ret
);
2578 * finally, overwrite btrfs super block.
2580 ret
= pread(fd
, buf
, sectorsize
, sb_bytenr
);
2582 fprintf(stderr
, "error during pread %d\n", ret
);
2585 BUG_ON(ret
!= sectorsize
);
2586 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
2588 fprintf(stderr
, "error during pwrite %d\n", ret
);
2591 BUG_ON(ret
!= sectorsize
);
2594 fprintf(stderr
, "error during fsync %d\n", ret
);
2600 extent_io_tree_cleanup(&io_tree
);
2601 printf("rollback complete.\n");
2604 fprintf(stderr
, "rollback aborted.\n");
2608 static void print_usage(void)
2610 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2611 printf("\t-d disable data checksum\n");
2612 printf("\t-i ignore xattrs and ACLs\n");
2613 printf("\t-n disable packing of small files\n");
2614 printf("\t-r roll back to ext2fs\n");
2617 int main(int argc
, char *argv
[])
2626 int c
= getopt(argc
, argv
, "dinr");
2646 argc
= argc
- optind
;
2652 file
= argv
[optind
];
2653 if (check_mounted(file
)) {
2654 fprintf(stderr
, "%s is mounted\n", file
);
2659 ret
= do_rollback(file
, 0);
2661 ret
= do_convert(file
, datacsum
, packing
, noxattr
);