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
;
196 static u8 filetype_conversion_table
[EXT2_FT_MAX
] = {
197 [EXT2_FT_UNKNOWN
] = BTRFS_FT_UNKNOWN
,
198 [EXT2_FT_REG_FILE
] = BTRFS_FT_REG_FILE
,
199 [EXT2_FT_DIR
] = BTRFS_FT_DIR
,
200 [EXT2_FT_CHRDEV
] = BTRFS_FT_CHRDEV
,
201 [EXT2_FT_BLKDEV
] = BTRFS_FT_BLKDEV
,
202 [EXT2_FT_FIFO
] = BTRFS_FT_FIFO
,
203 [EXT2_FT_SOCK
] = BTRFS_FT_SOCK
,
204 [EXT2_FT_SYMLINK
] = BTRFS_FT_SYMLINK
,
207 static int dir_iterate_proc(ext2_ino_t dir
, int entry
,
208 struct ext2_dir_entry
*old
,
209 int offset
, int blocksize
,
210 char *buf
,void *priv_data
)
216 char dotdot
[] = "..";
217 struct btrfs_key location
;
218 struct ext2_dir_entry_2
*dirent
= (struct ext2_dir_entry_2
*)old
;
219 struct dir_iterate_data
*idata
= (struct dir_iterate_data
*)priv_data
;
221 objectid
= dirent
->inode
+ INO_OFFSET
;
222 if (!strncmp(dirent
->name
, dotdot
, dirent
->name_len
)) {
223 if (dirent
->name_len
== 2) {
224 BUG_ON(idata
->parent
!= 0);
225 idata
->parent
= objectid
;
229 if (dirent
->inode
< EXT2_GOOD_OLD_FIRST_INO
)
232 location
.objectid
= objectid
;
234 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
236 file_type
= dirent
->file_type
;
237 BUG_ON(file_type
> EXT2_FT_SYMLINK
);
238 ret
= btrfs_insert_dir_item(idata
->trans
, idata
->root
,
239 dirent
->name
, dirent
->name_len
,
240 idata
->objectid
, &location
,
241 filetype_conversion_table
[file_type
]);
244 ret
= btrfs_insert_inode_ref(idata
->trans
, idata
->root
,
245 dirent
->name
, dirent
->name_len
,
246 objectid
, idata
->objectid
,
250 inode_size
= btrfs_stack_inode_size(idata
->inode
) +
251 dirent
->name_len
* 2;
252 btrfs_set_stack_inode_size(idata
->inode
, inode_size
);
255 idata
->errcode
= ret
;
259 static int create_dir_entries(struct btrfs_trans_handle
*trans
,
260 struct btrfs_root
*root
, u64 objectid
,
261 struct btrfs_inode_item
*btrfs_inode
,
262 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
266 struct dir_iterate_data data
= {
269 .inode
= btrfs_inode
,
270 .objectid
= objectid
,
276 err
= ext2fs_dir_iterate2(ext2_fs
, ext2_ino
, 0, NULL
,
277 dir_iterate_proc
, &data
);
281 if (ret
== 0 && data
.parent
== objectid
) {
282 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2,
283 objectid
, objectid
, 0);
287 fprintf(stderr
, "ext2fs_dir_iterate2: %s\n", error_message(err
));
291 static int read_disk_extent(struct btrfs_root
*root
, u64 bytenr
,
292 u32 num_bytes
, char *buffer
)
295 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
297 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, bytenr
);
298 if (ret
!= num_bytes
)
307 * Record a file extent. Do all the required works, such as inserting
308 * file extent item, inserting extent item and backref item into extent
309 * tree and updating block accounting.
311 static int record_file_extent(struct btrfs_trans_handle
*trans
,
312 struct btrfs_root
*root
, u64 objectid
,
313 struct btrfs_inode_item
*inode
,
314 u64 file_pos
, u64 disk_bytenr
,
315 u64 num_bytes
, int checksum
)
318 struct btrfs_fs_info
*info
= root
->fs_info
;
319 struct btrfs_root
*extent_root
= info
->extent_root
;
320 struct extent_buffer
*leaf
;
321 struct btrfs_file_extent_item
*fi
;
322 struct btrfs_key ins_key
;
323 struct btrfs_path path
;
324 struct btrfs_extent_item extent_item
;
325 u32 blocksize
= root
->sectorsize
;
329 if (disk_bytenr
== 0) {
330 ret
= btrfs_insert_file_extent(trans
, root
, objectid
,
331 file_pos
, disk_bytenr
,
332 num_bytes
, num_bytes
);
336 btrfs_init_path(&path
);
343 buffer
= malloc(blocksize
);
346 for (offset
= 0; offset
< num_bytes
; offset
+= blocksize
) {
347 ret
= read_disk_extent(root
, disk_bytenr
+ offset
,
351 ret
= btrfs_csum_file_block(trans
, root
, inode
,
352 objectid
, file_pos
+ offset
,
362 ins_key
.objectid
= objectid
;
363 ins_key
.offset
= file_pos
;
364 btrfs_set_key_type(&ins_key
, BTRFS_EXTENT_DATA_KEY
);
365 ret
= btrfs_insert_empty_item(trans
, root
, &path
, &ins_key
,
369 leaf
= path
.nodes
[0];
370 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
371 struct btrfs_file_extent_item
);
372 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
373 btrfs_set_file_extent_type(leaf
, fi
, BTRFS_FILE_EXTENT_REG
);
374 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
375 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, num_bytes
);
376 btrfs_set_file_extent_offset(leaf
, fi
, 0);
377 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
378 btrfs_mark_buffer_dirty(leaf
);
380 nblocks
= btrfs_stack_inode_nblocks(inode
) + num_bytes
/ 512;
381 btrfs_set_stack_inode_nblocks(inode
, nblocks
);
383 bytes_used
= btrfs_root_used(&root
->root_item
);
384 btrfs_set_root_used(&root
->root_item
, bytes_used
+ num_bytes
);
386 ins_key
.objectid
= disk_bytenr
;
387 ins_key
.offset
= num_bytes
;
388 btrfs_set_key_type(&ins_key
, BTRFS_EXTENT_ITEM_KEY
);
389 btrfs_set_stack_extent_refs(&extent_item
, 0);
390 ret
= btrfs_insert_item(trans
, extent_root
, &ins_key
,
391 &extent_item
, sizeof(extent_item
));
393 bytes_used
= btrfs_super_bytes_used(&info
->super_copy
);
394 btrfs_set_super_bytes_used(&info
->super_copy
, bytes_used
+
396 } else if (ret
!= -EEXIST
) {
399 btrfs_extent_post_op(trans
, extent_root
);
401 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
, num_bytes
,
402 leaf
->start
, root
->root_key
.objectid
,
403 trans
->transid
, objectid
, file_pos
);
408 btrfs_release_path(root
, &path
);
412 static int record_file_blocks(struct btrfs_trans_handle
*trans
,
413 struct btrfs_root
*root
, u64 objectid
,
414 struct btrfs_inode_item
*inode
,
415 u64 file_block
, u64 disk_block
,
416 u64 num_blocks
, int checksum
)
418 u64 file_pos
= file_block
* root
->sectorsize
;
419 u64 disk_bytenr
= disk_block
* root
->sectorsize
;
420 u64 num_bytes
= num_blocks
* root
->sectorsize
;
421 return record_file_extent(trans
, root
, objectid
, inode
, file_pos
,
422 disk_bytenr
, num_bytes
, checksum
);
425 struct blk_iterate_data
{
426 struct btrfs_trans_handle
*trans
;
427 struct btrfs_root
*root
;
428 struct btrfs_inode_item
*inode
;
438 static int block_iterate_proc(ext2_filsys ext2_fs
,
439 u64 disk_block
, u64 file_block
,
440 struct blk_iterate_data
*idata
)
443 struct btrfs_root
*root
= idata
->root
;
444 struct btrfs_trans_handle
*trans
= idata
->trans
;
445 struct btrfs_block_group_cache
*cache
;
448 BUG_ON(disk_block
== 0);
449 if ((idata
->num_blocks
> 0 && disk_block
>= idata
->boundary
) ||
450 (file_block
> idata
->first_block
+ idata
->num_blocks
) ||
451 (disk_block
!= idata
->disk_block
+ idata
->num_blocks
)) {
452 if (idata
->num_blocks
> 0) {
453 ret
= record_file_blocks(trans
, root
, idata
->objectid
,
454 idata
->inode
, idata
->first_block
,
455 idata
->disk_block
, idata
->num_blocks
,
459 idata
->first_block
+= idata
->num_blocks
;
460 idata
->num_blocks
= 0;
462 if (file_block
> idata
->first_block
) {
463 ret
= record_file_blocks(trans
, root
, idata
->objectid
,
464 idata
->inode
, idata
->first_block
,
465 0, file_block
- idata
->first_block
,
471 bytenr
= disk_block
* root
->sectorsize
;
472 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
474 bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
476 idata
->first_block
= file_block
;
477 idata
->disk_block
= disk_block
;
478 idata
->boundary
= bytenr
/ root
->sectorsize
;
483 idata
->errcode
= ret
;
487 static int __block_iterate_proc(ext2_filsys fs
, blk_t
*blocknr
,
488 e2_blkcnt_t blockcnt
, blk_t ref_block
,
489 int ref_offset
, void *priv_data
)
491 struct blk_iterate_data
*idata
;
492 idata
= (struct blk_iterate_data
*)priv_data
;
493 return block_iterate_proc(fs
, *blocknr
, blockcnt
, idata
);
497 * traverse file's data blocks, record these data blocks as file extents.
499 static int create_file_extents(struct btrfs_trans_handle
*trans
,
500 struct btrfs_root
*root
, u64 objectid
,
501 struct btrfs_inode_item
*btrfs_inode
,
502 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
503 int datacsum
, int packing
)
509 u32 sectorsize
= root
->sectorsize
;
510 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
511 struct blk_iterate_data data
= {
514 .inode
= btrfs_inode
,
515 .objectid
= objectid
,
520 .checksum
= datacsum
,
523 err
= ext2fs_block_iterate2(ext2_fs
, ext2_ino
, BLOCK_FLAG_DATA_ONLY
,
524 NULL
, __block_iterate_proc
, &data
);
530 if (packing
&& data
.first_block
== 0 && data
.num_blocks
> 0 &&
531 inode_size
<= BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
532 u64 num_bytes
= data
.num_blocks
* sectorsize
;
533 u64 disk_bytenr
= data
.disk_block
* sectorsize
;
535 buffer
= malloc(num_bytes
);
538 ret
= read_disk_extent(root
, disk_bytenr
, num_bytes
, buffer
);
541 if (num_bytes
> inode_size
)
542 num_bytes
= inode_size
;
543 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
,
544 0, buffer
, num_bytes
);
547 } else if (data
.num_blocks
> 0) {
548 ret
= record_file_blocks(trans
, root
, objectid
, btrfs_inode
,
549 data
.first_block
, data
.disk_block
,
550 data
.num_blocks
, data
.checksum
);
554 data
.first_block
+= data
.num_blocks
;
555 last_block
= (inode_size
+ sectorsize
- 1) / sectorsize
;
556 if (last_block
> data
.first_block
) {
557 ret
= record_file_blocks(trans
, root
, objectid
, btrfs_inode
,
558 data
.first_block
, 0, last_block
-
559 data
.first_block
, data
.checksum
);
566 fprintf(stderr
, "ext2fs_block_iterate2: %s\n", error_message(err
));
570 static int create_symbol_link(struct btrfs_trans_handle
*trans
,
571 struct btrfs_root
*root
, u64 objectid
,
572 struct btrfs_inode_item
*btrfs_inode
,
573 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
574 struct ext2_inode
*ext2_inode
)
578 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
579 if (ext2fs_inode_data_blocks(ext2_fs
, ext2_inode
)) {
580 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
+ 1);
581 ret
= create_file_extents(trans
, root
, objectid
, btrfs_inode
,
582 ext2_fs
, ext2_ino
, 1, 1);
583 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
);
587 pathname
= (char *)&(ext2_inode
->i_block
[0]);
588 BUG_ON(pathname
[inode_size
] != 0);
589 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
, 0,
590 pathname
, inode_size
+ 1);
595 * Following xattr/acl related codes are based on codes in
596 * fs/ext3/xattr.c and fs/ext3/acl.c
598 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
599 #define EXT2_XATTR_BFIRST(ptr) \
600 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
601 #define EXT2_XATTR_IHDR(inode) \
602 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
603 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
604 #define EXT2_XATTR_IFIRST(inode) \
605 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
606 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
608 static int ext2_xattr_check_names(struct ext2_ext_attr_entry
*entry
,
611 struct ext2_ext_attr_entry
*next
;
613 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
614 next
= EXT2_EXT_ATTR_NEXT(entry
);
615 if ((void *)next
>= end
)
622 static int ext2_xattr_check_block(const char *buf
, size_t size
)
625 struct ext2_ext_attr_header
*header
= EXT2_XATTR_BHDR(buf
);
627 if (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC
||
628 header
->h_blocks
!= 1)
630 error
= ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf
), buf
+ size
);
634 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry
*entry
,
637 size_t value_size
= entry
->e_value_size
;
639 if (entry
->e_value_block
!= 0 || value_size
> size
||
640 entry
->e_value_offs
+ value_size
> size
)
645 #define EXT2_ACL_VERSION 0x0001
656 } ext2_acl_entry_short
;
662 static inline int ext2_acl_count(size_t size
)
665 size
-= sizeof(ext2_acl_header
);
666 s
= size
- 4 * sizeof(ext2_acl_entry_short
);
668 if (size
% sizeof(ext2_acl_entry_short
))
670 return size
/ sizeof(ext2_acl_entry_short
);
672 if (s
% sizeof(ext2_acl_entry
))
674 return s
/ sizeof(ext2_acl_entry
) + 4;
678 #define ACL_EA_VERSION 0x0002
688 acl_ea_entry a_entries
[0];
691 static inline size_t acl_ea_size(int count
)
693 return sizeof(acl_ea_header
) + count
* sizeof(acl_ea_entry
);
696 static int ext2_acl_to_xattr(void *dst
, const void *src
,
697 size_t dst_size
, size_t src_size
)
700 const void *end
= src
+ src_size
;
701 acl_ea_header
*ext_acl
= (acl_ea_header
*)dst
;
702 acl_ea_entry
*dst_entry
= ext_acl
->a_entries
;
703 ext2_acl_entry
*src_entry
;
705 if (src_size
< sizeof(ext2_acl_header
))
707 if (((ext2_acl_header
*)src
)->a_version
!=
708 cpu_to_le32(EXT2_ACL_VERSION
))
710 src
+= sizeof(ext2_acl_header
);
711 count
= ext2_acl_count(src_size
);
715 BUG_ON(dst_size
< acl_ea_size(count
));
716 ext_acl
->a_version
= cpu_to_le32(ACL_EA_VERSION
);
717 for (i
= 0; i
< count
; i
++, dst_entry
++) {
718 src_entry
= (ext2_acl_entry
*)src
;
719 if (src
+ sizeof(ext2_acl_entry_short
) > end
)
721 dst_entry
->e_tag
= src_entry
->e_tag
;
722 dst_entry
->e_perm
= src_entry
->e_perm
;
723 switch (le16_to_cpu(src_entry
->e_tag
)) {
728 src
+= sizeof(ext2_acl_entry_short
);
729 dst_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
733 src
+= sizeof(ext2_acl_entry
);
736 dst_entry
->e_id
= src_entry
->e_id
;
749 static char *xattr_prefix_table
[] = {
751 [2] = "system.posix_acl_access",
752 [3] = "system.posix_acl_default",
757 static int copy_single_xattr(struct btrfs_trans_handle
*trans
,
758 struct btrfs_root
*root
, u64 objectid
,
759 struct ext2_ext_attr_entry
*entry
,
760 const void *data
, u32 datalen
)
765 void *databuf
= NULL
;
766 char namebuf
[XATTR_NAME_MAX
+ 1];
768 name_index
= entry
->e_name_index
;
769 if (name_index
>= ARRAY_SIZE(xattr_prefix_table
) ||
770 xattr_prefix_table
[name_index
] == NULL
)
772 name_len
= strlen(xattr_prefix_table
[name_index
]) +
774 if (name_len
>= sizeof(namebuf
))
777 if (name_index
== 2 || name_index
== 3) {
778 size_t bufsize
= acl_ea_size(ext2_acl_count(datalen
));
779 databuf
= malloc(bufsize
);
782 ret
= ext2_acl_to_xattr(databuf
, data
, bufsize
, datalen
);
788 strcpy(namebuf
, xattr_prefix_table
[name_index
]);
789 strncat(namebuf
, EXT2_EXT_ATTR_NAME(entry
), entry
->e_name_len
);
790 if (name_len
+ datalen
> BTRFS_LEAF_DATA_SIZE(root
) -
791 sizeof(struct btrfs_item
) - sizeof(struct btrfs_dir_item
)) {
792 fprintf(stderr
, "skip large xattr on inode %Lu name %.*s\n",
793 objectid
- INO_OFFSET
, name_len
, namebuf
);
796 ret
= btrfs_insert_xattr_item(trans
, root
, namebuf
, name_len
,
797 data
, datalen
, objectid
);
804 static int copy_extended_attrs(struct btrfs_trans_handle
*trans
,
805 struct btrfs_root
*root
, u64 objectid
,
806 struct btrfs_inode_item
*btrfs_inode
,
807 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
813 u32 block_size
= ext2_fs
->blocksize
;
814 u32 inode_size
= EXT2_INODE_SIZE(ext2_fs
->super
);
815 struct ext2_inode_large
*ext2_inode
;
816 struct ext2_ext_attr_entry
*entry
;
819 char inode_buf
[EXT2_GOOD_OLD_INODE_SIZE
];
821 if (inode_size
<= EXT2_GOOD_OLD_INODE_SIZE
) {
822 ext2_inode
= (struct ext2_inode_large
*)inode_buf
;
824 ext2_inode
= (struct ext2_inode_large
*)malloc(inode_size
);
828 err
= ext2fs_read_inode_full(ext2_fs
, ext2_ino
, (void *)ext2_inode
,
831 fprintf(stderr
, "ext2fs_read_inode_full: %s\n",
837 if (ext2_ino
> ext2_fs
->super
->s_first_ino
&&
838 inode_size
> EXT2_GOOD_OLD_INODE_SIZE
) {
839 if (EXT2_GOOD_OLD_INODE_SIZE
+
840 ext2_inode
->i_extra_isize
> inode_size
) {
844 if (ext2_inode
->i_extra_isize
!= 0 &&
845 EXT2_XATTR_IHDR(ext2_inode
)->h_magic
==
846 EXT2_EXT_ATTR_MAGIC
) {
852 void *end
= (void *)ext2_inode
+ inode_size
;
853 entry
= EXT2_XATTR_IFIRST(ext2_inode
);
854 total
= end
- (void *)entry
;
855 ret
= ext2_xattr_check_names(entry
, end
);
858 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
859 ret
= ext2_xattr_check_entry(entry
, total
);
862 data
= (void *)EXT2_XATTR_IFIRST(ext2_inode
) +
864 datalen
= entry
->e_value_size
;
865 ret
= copy_single_xattr(trans
, root
, objectid
,
866 entry
, data
, datalen
);
869 entry
= EXT2_EXT_ATTR_NEXT(entry
);
873 if (ext2_inode
->i_file_acl
== 0)
876 buffer
= malloc(block_size
);
881 err
= ext2fs_read_ext_attr(ext2_fs
, ext2_inode
->i_file_acl
, buffer
);
883 fprintf(stderr
, "ext2fs_read_ext_attr: %s\n",
888 ret
= ext2_xattr_check_block(buffer
, block_size
);
892 entry
= EXT2_XATTR_BFIRST(buffer
);
893 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
894 ret
= ext2_xattr_check_entry(entry
, block_size
);
897 data
= buffer
+ entry
->e_value_offs
;
898 datalen
= entry
->e_value_size
;
899 ret
= copy_single_xattr(trans
, root
, objectid
,
900 entry
, data
, datalen
);
903 entry
= EXT2_EXT_ATTR_NEXT(entry
);
908 if ((void *)ext2_inode
!= inode_buf
)
913 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
915 static inline dev_t
old_decode_dev(u16 val
)
917 return MKDEV((val
>> 8) & 255, val
& 255);
920 static inline dev_t
new_decode_dev(u32 dev
)
922 unsigned major
= (dev
& 0xfff00) >> 8;
923 unsigned minor
= (dev
& 0xff) | ((dev
>> 12) & 0xfff00);
924 return MKDEV(major
, minor
);
927 static int copy_inode_item(struct btrfs_inode_item
*dst
,
928 struct ext2_inode
*src
)
930 btrfs_set_stack_inode_generation(dst
, 1);
931 btrfs_set_stack_inode_size(dst
, src
->i_size
);
932 btrfs_set_stack_inode_nblocks(dst
, src
->i_blocks
);
933 btrfs_set_stack_inode_block_group(dst
, 0);
934 btrfs_set_stack_inode_nblocks(dst
, 0);
935 btrfs_set_stack_inode_nlink(dst
, src
->i_links_count
);
936 btrfs_set_stack_inode_uid(dst
, src
->i_uid
| (src
->i_uid_high
<< 16));
937 btrfs_set_stack_inode_gid(dst
, src
->i_gid
| (src
->i_gid_high
<< 16));
938 btrfs_set_stack_inode_mode(dst
, src
->i_mode
);
939 btrfs_set_stack_inode_rdev(dst
, 0);
940 btrfs_set_stack_inode_flags(dst
, 0);
941 btrfs_set_stack_inode_compat_flags(dst
, 0);
942 btrfs_set_stack_timespec_sec(&dst
->atime
, src
->i_atime
);
943 btrfs_set_stack_timespec_nsec(&dst
->atime
, 0);
944 btrfs_set_stack_timespec_sec(&dst
->ctime
, src
->i_ctime
);
945 btrfs_set_stack_timespec_nsec(&dst
->ctime
, 0);
946 btrfs_set_stack_timespec_sec(&dst
->mtime
, src
->i_mtime
);
947 btrfs_set_stack_timespec_nsec(&dst
->mtime
, 0);
948 btrfs_set_stack_timespec_sec(&dst
->otime
, 0);
949 btrfs_set_stack_timespec_nsec(&dst
->otime
, 0);
951 if (S_ISDIR(src
->i_mode
)) {
952 btrfs_set_stack_inode_size(dst
, 0);
953 btrfs_set_stack_inode_nlink(dst
, 1);
955 if (S_ISREG(src
->i_mode
)) {
956 btrfs_set_stack_inode_size(dst
, (u64
)src
->i_size_high
<< 32 |
959 if (!S_ISREG(src
->i_mode
) && !S_ISDIR(src
->i_mode
) &&
960 !S_ISLNK(src
->i_mode
)) {
961 if (src
->i_block
[0]) {
962 btrfs_set_stack_inode_rdev(dst
,
963 old_decode_dev(src
->i_block
[0]));
965 btrfs_set_stack_inode_rdev(dst
,
966 new_decode_dev(src
->i_block
[1]));
973 * copy a single inode. do all the required works, such as cloning
974 * inode item, creating file extents and creating directory entries.
976 static int copy_single_inode(struct btrfs_trans_handle
*trans
,
977 struct btrfs_root
*root
, u64 objectid
,
978 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
979 struct ext2_inode
*ext2_inode
,
980 int datacsum
, int packing
, int noxattr
)
983 struct btrfs_key inode_key
;
984 struct btrfs_inode_item btrfs_inode
;
986 if (ext2_inode
->i_links_count
== 0)
989 copy_inode_item(&btrfs_inode
, ext2_inode
);
990 if (!datacsum
&& S_ISREG(ext2_inode
->i_mode
)) {
991 u32 flags
= btrfs_stack_inode_flags(&btrfs_inode
) |
992 BTRFS_INODE_NODATASUM
;
993 btrfs_set_stack_inode_flags(&btrfs_inode
, flags
);
996 switch (ext2_inode
->i_mode
& S_IFMT
) {
998 ret
= create_file_extents(trans
, root
, objectid
, &btrfs_inode
,
999 ext2_fs
, ext2_ino
, datacsum
, packing
);
1002 ret
= create_dir_entries(trans
, root
, objectid
, &btrfs_inode
,
1006 ret
= create_symbol_link(trans
, root
, objectid
, &btrfs_inode
,
1007 ext2_fs
, ext2_ino
, ext2_inode
);
1017 ret
= copy_extended_attrs(trans
, root
, objectid
, &btrfs_inode
,
1022 inode_key
.objectid
= objectid
;
1023 inode_key
.offset
= 0;
1024 btrfs_set_key_type(&inode_key
, BTRFS_INODE_ITEM_KEY
);
1025 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1029 static int copy_disk_extent(struct btrfs_root
*root
, u64 dst_bytenr
,
1030 u64 src_bytenr
, u32 num_bytes
)
1034 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
1036 buffer
= malloc(num_bytes
);
1039 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, src_bytenr
);
1040 if (ret
!= num_bytes
)
1042 ret
= pwrite(fs_devs
->latest_bdev
, buffer
, num_bytes
, dst_bytenr
);
1043 if (ret
!= num_bytes
)
1053 * scan ext2's inode bitmap and copy all used inode.
1055 static int copy_inodes(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1056 int datacsum
, int packing
, int noxattr
)
1060 ext2_inode_scan ext2_scan
;
1061 struct ext2_inode ext2_inode
;
1062 ext2_ino_t ext2_ino
;
1064 struct btrfs_trans_handle
*trans
;
1066 trans
= btrfs_start_transaction(root
, 1);
1069 err
= ext2fs_open_inode_scan(ext2_fs
, 0, &ext2_scan
);
1071 fprintf(stderr
, "ext2fs_open_inode_scan: %s\n", error_message(err
));
1074 while (!(err
= ext2fs_get_next_inode(ext2_scan
, &ext2_ino
,
1076 /* no more inodes */
1079 /* skip special inode in ext2fs */
1080 if (ext2_ino
< EXT2_GOOD_OLD_FIRST_INO
&&
1081 ext2_ino
!= EXT2_ROOT_INO
)
1083 objectid
= ext2_ino
+ INO_OFFSET
;
1084 ret
= copy_single_inode(trans
, root
,
1085 objectid
, ext2_fs
, ext2_ino
,
1086 &ext2_inode
, datacsum
, packing
,
1090 if (trans
->blocks_used
>= 4096) {
1091 ret
= btrfs_commit_transaction(trans
, root
);
1093 trans
= btrfs_start_transaction(root
, 1);
1098 fprintf(stderr
, "ext2fs_get_next_inode: %s\n", error_message(err
));
1101 ret
= btrfs_commit_transaction(trans
, root
);
1108 * Construct a range of ext2fs image file.
1109 * scan block allocation bitmap, find all blocks used by the ext2fs
1110 * in this range and create file extents that point to these blocks.
1112 * Note: Before calling the function, no file extent points to blocks
1115 static int create_image_file_range(struct btrfs_trans_handle
*trans
,
1116 struct btrfs_root
*root
, u64 objectid
,
1117 struct btrfs_inode_item
*inode
,
1118 u64 start_byte
, u64 end_byte
,
1119 ext2_filsys ext2_fs
)
1121 u32 blocksize
= ext2_fs
->blocksize
;
1122 u32 block
= start_byte
/ blocksize
;
1123 u32 last_block
= (end_byte
+ blocksize
- 1) / blocksize
;
1125 struct blk_iterate_data data
= {
1129 .objectid
= objectid
,
1130 .first_block
= block
,
1133 .boundary
= (u64
)-1,
1137 for (; start_byte
< end_byte
; block
++, start_byte
+= blocksize
) {
1138 if (!ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
1140 ret
= block_iterate_proc(NULL
, block
, block
, &data
);
1141 if (ret
& BLOCK_ABORT
) {
1146 if (data
.num_blocks
> 0) {
1147 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1148 data
.first_block
, data
.disk_block
,
1149 data
.num_blocks
, 0);
1152 data
.first_block
+= data
.num_blocks
;
1154 if (last_block
> data
.first_block
) {
1155 ret
= record_file_blocks(trans
, root
, objectid
, inode
,
1156 data
.first_block
, 0, last_block
-
1157 data
.first_block
, 0);
1165 * Create the ext2fs image file.
1167 static int create_ext2_image(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1171 struct btrfs_key key
;
1172 struct btrfs_key location
;
1173 struct btrfs_path path
;
1174 struct btrfs_inode_item btrfs_inode
;
1175 struct btrfs_inode_item
*inode_item
;
1176 struct extent_buffer
*leaf
;
1177 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1178 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
1179 struct btrfs_trans_handle
*trans
;
1180 struct btrfs_extent_ref
*ref_item
;
1189 u32 sectorsize
= root
->sectorsize
;
1192 total_bytes
= btrfs_super_total_bytes(&fs_info
->super_copy
);
1193 first_free
= BTRFS_SUPER_INFO_OFFSET
+ sectorsize
* 2 - 1;
1194 first_free
&= ~((u64
)sectorsize
- 1);
1196 memset(&btrfs_inode
, 0, sizeof(btrfs_inode
));
1197 btrfs_set_stack_inode_generation(&btrfs_inode
, 1);
1198 btrfs_set_stack_inode_size(&btrfs_inode
, total_bytes
);
1199 btrfs_set_stack_inode_nlink(&btrfs_inode
, 1);
1200 btrfs_set_stack_inode_nblocks(&btrfs_inode
, 0);
1201 btrfs_set_stack_inode_mode(&btrfs_inode
, S_IFREG
| 0400);
1202 btrfs_set_stack_inode_flags(&btrfs_inode
, BTRFS_INODE_NODATASUM
|
1203 BTRFS_INODE_READONLY
);
1204 btrfs_init_path(&path
);
1205 trans
= btrfs_start_transaction(root
, 1);
1208 objectid
= btrfs_root_dirid(&root
->root_item
);
1209 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1214 * copy blocks covered by extent #0 to new positions. extent #0 is
1215 * special, we can't rely on relocate_extents_range to relocate it.
1217 for (last_byte
= 0; last_byte
< first_free
; last_byte
+= sectorsize
) {
1218 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1221 ret
= copy_disk_extent(root
, key
.objectid
, last_byte
,
1225 ret
= record_file_extent(trans
, root
, objectid
,
1226 &btrfs_inode
, last_byte
,
1227 key
.objectid
, sectorsize
, 0);
1233 key
.objectid
= last_byte
;
1235 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1236 ret
= btrfs_search_slot(trans
, fs_info
->extent_root
,
1241 leaf
= path
.nodes
[0];
1242 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1243 ret
= btrfs_next_leaf(extent_root
, &path
);
1248 leaf
= path
.nodes
[0];
1250 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1251 if (last_byte
> key
.objectid
||
1252 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1257 * Check backref to distinguish extent items for normal
1258 * files (files that correspond to files in Ext2fs) from
1259 * extent items for ctree blocks.
1261 bytenr
= key
.objectid
;
1262 num_bytes
= key
.offset
;
1265 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1266 ret
= btrfs_next_leaf(extent_root
, &path
);
1271 leaf
= path
.nodes
[0];
1273 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1274 if (key
.objectid
!= bytenr
)
1276 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
1280 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1281 struct btrfs_extent_ref
);
1282 ref_root
= btrfs_ref_root(leaf
, ref_item
);
1283 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
1284 if ((ref_root
== BTRFS_FS_TREE_OBJECTID
) &&
1285 (ref_owner
>= BTRFS_FIRST_FREE_OBJECTID
)) {
1294 if (bytenr
> last_byte
) {
1295 ret
= create_image_file_range(trans
, root
, objectid
,
1296 &btrfs_inode
, last_byte
,
1301 ret
= record_file_extent(trans
, root
, objectid
, &btrfs_inode
,
1302 bytenr
, bytenr
, num_bytes
, 0);
1305 last_byte
= bytenr
+ num_bytes
;
1306 btrfs_release_path(extent_root
, &path
);
1308 if (trans
->blocks_used
>= 4096) {
1309 ret
= btrfs_commit_transaction(trans
, root
);
1311 trans
= btrfs_start_transaction(root
, 1);
1315 btrfs_release_path(root
, &path
);
1316 if (total_bytes
> last_byte
) {
1317 ret
= create_image_file_range(trans
, root
, objectid
,
1318 &btrfs_inode
, last_byte
,
1319 total_bytes
, ext2_fs
);
1324 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1328 location
.objectid
= objectid
;
1329 location
.offset
= 0;
1330 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1331 ret
= btrfs_insert_dir_item(trans
, root
, name
, strlen(name
),
1332 btrfs_root_dirid(&root
->root_item
),
1333 &location
, EXT2_FT_REG_FILE
);
1336 ret
= btrfs_insert_inode_ref(trans
, root
, name
, strlen(name
),
1338 btrfs_root_dirid(&root
->root_item
), 0);
1341 location
.objectid
= btrfs_root_dirid(&root
->root_item
);
1342 location
.offset
= 0;
1343 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1344 ret
= btrfs_lookup_inode(trans
, root
, &path
, &location
, 1);
1347 leaf
= path
.nodes
[0];
1348 inode_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1349 struct btrfs_inode_item
);
1350 btrfs_set_inode_size(leaf
, inode_item
, strlen(name
) * 2 +
1351 btrfs_inode_size(leaf
, inode_item
));
1352 btrfs_mark_buffer_dirty(leaf
);
1353 btrfs_release_path(root
, &path
);
1354 ret
= btrfs_commit_transaction(trans
, root
);
1357 btrfs_release_path(root
, &path
);
1361 struct btrfs_root
*create_subvol(struct btrfs_root
*root
, const char *name
)
1365 struct btrfs_key location
;
1366 struct btrfs_root_item root_item
;
1367 struct btrfs_trans_handle
*trans
;
1368 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1369 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1370 struct btrfs_root
*new_root
;
1371 struct extent_buffer
*tmp
;
1373 trans
= btrfs_start_transaction(root
, 1);
1376 objectid
= btrfs_super_root_dir(&fs_info
->super_copy
);
1377 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1380 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
1383 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
1384 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
1385 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
1386 free_extent_buffer(tmp
);
1388 location
.objectid
= objectid
;
1389 location
.offset
= 1;
1390 btrfs_set_key_type(&location
, BTRFS_ROOT_ITEM_KEY
);
1391 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1392 &location
, &root_item
);
1395 location
.offset
= (u64
)-1;
1396 ret
= btrfs_insert_dir_item(trans
, tree_root
, name
, strlen(name
),
1397 btrfs_super_root_dir(&fs_info
->super_copy
),
1398 &location
, BTRFS_FT_DIR
);
1401 ret
= btrfs_insert_inode_ref(trans
, tree_root
, name
, strlen(name
),
1403 btrfs_super_root_dir(&fs_info
->super_copy
),
1407 ret
= btrfs_commit_transaction(trans
, root
);
1409 new_root
= btrfs_read_fs_root(fs_info
, &location
);
1410 if (!new_root
|| IS_ERR(new_root
))
1412 trans
= btrfs_start_transaction(new_root
, 1);
1414 ret
= btrfs_make_root_dir(trans
, new_root
, BTRFS_FIRST_FREE_OBJECTID
);
1417 ret
= btrfs_commit_transaction(trans
, new_root
);
1424 * Fixup block accounting. The initial block accounting created by
1425 * make_block_groups isn't accuracy in this case.
1427 static int fixup_block_accounting(struct btrfs_trans_handle
*trans
,
1428 struct btrfs_root
*root
)
1434 struct btrfs_path path
;
1435 struct btrfs_key key
;
1436 struct extent_buffer
*leaf
;
1437 struct btrfs_block_group_cache
*cache
;
1438 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1441 cache
= btrfs_lookup_block_group(fs_info
, start
);
1444 start
= cache
->key
.objectid
+ cache
->key
.offset
;
1445 btrfs_set_block_group_used(&cache
->item
, 0);
1446 cache
->space_info
->bytes_used
= 0;
1449 btrfs_init_path(&path
);
1452 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1453 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
1458 leaf
= path
.nodes
[0];
1459 slot
= path
.slots
[0];
1460 if (slot
>= btrfs_header_nritems(leaf
)) {
1461 ret
= btrfs_next_leaf(root
, &path
);
1466 leaf
= path
.nodes
[0];
1467 slot
= path
.slots
[0];
1469 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1470 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
1471 bytes_used
+= key
.offset
;
1472 ret
= btrfs_update_block_group(trans
, root
,
1473 key
.objectid
, key
.offset
, 1, 0);
1478 btrfs_set_super_bytes_used(&root
->fs_info
->super_copy
, bytes_used
);
1479 btrfs_release_path(root
, &path
);
1483 static int create_chunk_mapping(struct btrfs_trans_handle
*trans
,
1484 struct btrfs_root
*root
)
1486 struct btrfs_fs_info
*info
= root
->fs_info
;
1487 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1488 struct btrfs_root
*extent_root
= info
->extent_root
;
1489 struct btrfs_device
*device
;
1490 struct btrfs_block_group_cache
*cache
;
1491 struct btrfs_dev_extent
*extent
;
1492 struct extent_buffer
*leaf
;
1493 struct btrfs_chunk chunk
;
1494 struct btrfs_key key
;
1495 struct btrfs_path path
;
1501 btrfs_init_path(&path
);
1503 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1504 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1506 BUG_ON(list_empty(&info
->fs_devices
->devices
));
1507 device
= list_entry(info
->fs_devices
->devices
.next
,
1508 struct btrfs_device
, dev_list
);
1509 BUG_ON(device
->devid
!= info
->fs_devices
->latest_devid
);
1511 /* delete device extent created by make_btrfs */
1512 key
.objectid
= device
->devid
;
1514 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1515 ret
= btrfs_search_slot(trans
, device
->dev_root
, &key
, &path
, -1, 1);
1520 ret
= btrfs_del_item(trans
, device
->dev_root
, &path
);
1523 btrfs_release_path(device
->dev_root
, &path
);
1525 /* delete chunk item created by make_btrfs */
1526 key
.objectid
= chunk_objectid
;
1528 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1529 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, -1, 1);
1534 ret
= btrfs_del_item(trans
, chunk_root
, &path
);
1537 btrfs_release_path(chunk_root
, &path
);
1539 /* for each block group, create device extent and chunk item */
1541 while (cur_start
< total_bytes
) {
1542 cache
= btrfs_lookup_block_group(root
->fs_info
, cur_start
);
1545 /* insert device extent */
1546 key
.objectid
= device
->devid
;
1547 key
.offset
= cache
->key
.objectid
;
1548 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1549 ret
= btrfs_insert_empty_item(trans
, device
->dev_root
, &path
,
1550 &key
, sizeof(*extent
));
1554 leaf
= path
.nodes
[0];
1555 extent
= btrfs_item_ptr(leaf
, path
.slots
[0],
1556 struct btrfs_dev_extent
);
1558 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1559 chunk_root
->root_key
.objectid
);
1560 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1562 btrfs_set_dev_extent_chunk_offset(leaf
, extent
,
1563 cache
->key
.objectid
);
1564 btrfs_set_dev_extent_length(leaf
, extent
, cache
->key
.offset
);
1565 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1566 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1568 btrfs_mark_buffer_dirty(leaf
);
1569 btrfs_release_path(device
->dev_root
, &path
);
1571 /* insert chunk item */
1572 btrfs_set_stack_chunk_length(&chunk
, cache
->key
.offset
);
1573 btrfs_set_stack_chunk_owner(&chunk
,
1574 extent_root
->root_key
.objectid
);
1575 btrfs_set_stack_chunk_stripe_len(&chunk
, 64 * 1024);
1576 btrfs_set_stack_chunk_type(&chunk
, cache
->flags
);
1577 btrfs_set_stack_chunk_io_align(&chunk
, device
->io_align
);
1578 btrfs_set_stack_chunk_io_width(&chunk
, device
->io_width
);
1579 btrfs_set_stack_chunk_sector_size(&chunk
, device
->sector_size
);
1580 btrfs_set_stack_chunk_num_stripes(&chunk
, 1);
1581 btrfs_set_stack_chunk_sub_stripes(&chunk
, 0);
1582 btrfs_set_stack_stripe_devid(&chunk
.stripe
, device
->devid
);
1583 btrfs_set_stack_stripe_offset(&chunk
.stripe
,
1584 cache
->key
.objectid
);
1585 memcpy(&chunk
.stripe
.dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1587 key
.objectid
= chunk_objectid
;
1588 key
.offset
= cache
->key
.objectid
;
1589 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1591 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, &chunk
,
1592 btrfs_chunk_item_size(1));
1596 cur_start
= cache
->key
.objectid
+ cache
->key
.offset
;
1599 device
->bytes_used
= total_bytes
;
1600 ret
= btrfs_update_device(trans
, device
);
1602 btrfs_release_path(device
->dev_root
, &path
);
1606 static int init_btrfs(struct btrfs_root
*root
)
1609 struct btrfs_key location
;
1610 struct btrfs_trans_handle
*trans
;
1611 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1613 trans
= btrfs_start_transaction(root
, 1);
1615 ret
= btrfs_make_block_groups(trans
, root
);
1618 ret
= fixup_block_accounting(trans
, root
);
1621 ret
= create_chunk_mapping(trans
, root
);
1624 ret
= btrfs_make_root_dir(trans
, fs_info
->tree_root
,
1625 BTRFS_ROOT_TREE_DIR_OBJECTID
);
1628 memcpy(&location
, &root
->root_key
, sizeof(location
));
1629 location
.offset
= (u64
)-1;
1630 ret
= btrfs_insert_dir_item(trans
, fs_info
->tree_root
, "default", 7,
1631 btrfs_super_root_dir(&fs_info
->super_copy
),
1632 &location
, BTRFS_FT_DIR
);
1635 ret
= btrfs_insert_inode_ref(trans
, fs_info
->tree_root
, "default", 7,
1637 btrfs_super_root_dir(&fs_info
->super_copy
), 0);
1640 btrfs_set_root_dirid(&fs_info
->fs_root
->root_item
,
1641 BTRFS_FIRST_FREE_OBJECTID
);
1642 ret
= btrfs_commit_transaction(trans
, root
);
1649 * Migrate super block to it's default position and zero 0 ~ 16k
1651 static int migrate_super_block(int fd
, u64 old_bytenr
, u32 sectorsize
)
1654 struct extent_buffer
*buf
;
1655 struct btrfs_super_block
*super
;
1659 BUG_ON(sectorsize
< sizeof(*super
));
1660 buf
= malloc(sizeof(*buf
) + sectorsize
);
1664 buf
->len
= sectorsize
;
1665 ret
= pread(fd
, buf
->data
, sectorsize
, old_bytenr
);
1666 if (ret
!= sectorsize
)
1669 super
= (struct btrfs_super_block
*)buf
->data
;
1670 BUG_ON(btrfs_super_bytenr(super
) != old_bytenr
);
1671 btrfs_set_super_bytenr(super
, BTRFS_SUPER_INFO_OFFSET
);
1673 csum_tree_block(NULL
, buf
, 0);
1674 ret
= pwrite(fd
, buf
->data
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
1675 if (ret
!= sectorsize
)
1682 memset(buf
->data
, 0, sectorsize
);
1683 for (bytenr
= 0; bytenr
< BTRFS_SUPER_INFO_OFFSET
; ) {
1684 len
= BTRFS_SUPER_INFO_OFFSET
- bytenr
;
1685 if (len
> sectorsize
)
1687 ret
= pwrite(fd
, buf
->data
, len
, bytenr
);
1689 fprintf(stderr
, "unable to zero fill device\n");
1703 static int prepare_system_chunk_sb(struct btrfs_super_block
*super
)
1705 struct btrfs_chunk
*chunk
;
1706 struct btrfs_disk_key
*key
;
1707 u32 sectorsize
= btrfs_super_sectorsize(super
);
1709 key
= (struct btrfs_disk_key
*)(super
->sys_chunk_array
);
1710 chunk
= (struct btrfs_chunk
*)(super
->sys_chunk_array
+
1711 sizeof(struct btrfs_disk_key
));
1713 btrfs_set_disk_key_objectid(key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1714 btrfs_set_disk_key_type(key
, BTRFS_CHUNK_ITEM_KEY
);
1715 btrfs_set_disk_key_offset(key
, 0);
1717 btrfs_set_stack_chunk_length(chunk
, btrfs_super_total_bytes(super
));
1718 btrfs_set_stack_chunk_owner(chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
1719 btrfs_set_stack_chunk_stripe_len(chunk
, 64 * 1024);
1720 btrfs_set_stack_chunk_type(chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
1721 btrfs_set_stack_chunk_io_align(chunk
, sectorsize
);
1722 btrfs_set_stack_chunk_io_width(chunk
, sectorsize
);
1723 btrfs_set_stack_chunk_sector_size(chunk
, sectorsize
);
1724 btrfs_set_stack_chunk_num_stripes(chunk
, 1);
1725 btrfs_set_stack_chunk_sub_stripes(chunk
, 0);
1726 chunk
->stripe
.devid
= super
->dev_item
.devid
;
1727 chunk
->stripe
.offset
= cpu_to_le64(0);
1728 memcpy(chunk
->stripe
.dev_uuid
, super
->dev_item
.uuid
, BTRFS_UUID_SIZE
);
1729 btrfs_set_super_sys_array_size(super
, sizeof(*key
) + sizeof(*chunk
));
1733 static int prepare_system_chunk(int fd
, u64 sb_bytenr
, u32 sectorsize
)
1736 struct extent_buffer
*buf
;
1737 struct btrfs_super_block
*super
;
1739 BUG_ON(sectorsize
< sizeof(*super
));
1740 buf
= malloc(sizeof(*buf
) + sectorsize
);
1744 buf
->len
= sectorsize
;
1745 ret
= pread(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1746 if (ret
!= sectorsize
)
1749 super
= (struct btrfs_super_block
*)buf
->data
;
1750 BUG_ON(btrfs_super_bytenr(super
) != sb_bytenr
);
1751 BUG_ON(btrfs_super_num_devices(super
) != 1);
1753 ret
= prepare_system_chunk_sb(super
);
1757 csum_tree_block(NULL
, buf
, 0);
1758 ret
= pwrite(fd
, buf
->data
, sectorsize
, sb_bytenr
);
1759 if (ret
!= sectorsize
)
1770 static int relocate_one_reference(struct btrfs_trans_handle
*trans
,
1771 struct btrfs_root
*root
,
1772 u64 extent_start
, u64 extent_size
,
1773 u64 objectid
, u64 offset
,
1774 struct extent_io_tree
*reloc_tree
)
1776 struct extent_buffer
*leaf
;
1777 struct btrfs_file_extent_item
*fi
;
1778 struct btrfs_key key
;
1779 struct btrfs_path path
;
1780 struct btrfs_inode_item inode
;
1781 struct blk_iterate_data data
;
1790 u32 sectorsize
= root
->sectorsize
;
1795 btrfs_init_path(&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
);
1804 /* the extent may be referenced by old snapshot */
1805 if (extent_start
!= btrfs_file_extent_disk_bytenr(leaf
, fi
) ||
1806 extent_size
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1811 root_gen
= btrfs_header_generation(leaf
);
1812 root_owner
= btrfs_header_owner(leaf
);
1813 bytenr
= extent_start
+ btrfs_file_extent_offset(leaf
, fi
);
1814 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
1816 ret
= btrfs_del_item(trans
, root
, &path
);
1820 ret
= btrfs_free_extent(trans
, root
, extent_start
, extent_size
,
1821 leaf
->start
, root_owner
, root_gen
, objectid
,
1826 btrfs_release_path(root
, &path
);
1828 key
.objectid
= objectid
;
1830 key
.type
= BTRFS_INODE_ITEM_KEY
;
1831 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 0);
1835 leaf
= path
.nodes
[0];
1836 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1837 read_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1838 btrfs_release_path(root
, &path
);
1840 BUG_ON(num_bytes
& (sectorsize
- 1));
1841 nblocks
= btrfs_stack_inode_nblocks(&inode
) - num_bytes
/ 512;
1842 btrfs_set_stack_inode_nblocks(&inode
, nblocks
);
1844 data
= (struct blk_iterate_data
) {
1848 .objectid
= objectid
,
1849 .first_block
= offset
/ sectorsize
,
1852 .boundary
= (u64
)-1,
1857 cur_offset
= offset
;
1858 while (num_bytes
> 0) {
1859 sector_end
= bytenr
+ sectorsize
- 1;
1860 if (test_range_bit(reloc_tree
, bytenr
, sector_end
,
1861 EXTENT_LOCKED
, 1)) {
1862 ret
= get_state_private(reloc_tree
, bytenr
, &new_pos
);
1865 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
);
1868 new_pos
= key
.objectid
;
1870 if (cur_offset
== offset
) {
1871 fd
= root
->fs_info
->fs_devices
->latest_bdev
;
1872 readahead(fd
, bytenr
, num_bytes
);
1874 ret
= copy_disk_extent(root
, new_pos
, bytenr
,
1878 ret
= set_extent_bits(reloc_tree
, bytenr
, sector_end
,
1879 EXTENT_LOCKED
, GFP_NOFS
);
1881 ret
= set_state_private(reloc_tree
, bytenr
, new_pos
);
1885 ret
= block_iterate_proc(NULL
, new_pos
/ sectorsize
,
1886 cur_offset
/ sectorsize
, &data
);
1887 if (ret
& BLOCK_ABORT
) {
1892 cur_offset
+= sectorsize
;
1893 bytenr
+= sectorsize
;
1894 num_bytes
-= sectorsize
;
1897 if (data
.num_blocks
> 0) {
1898 ret
= record_file_blocks(trans
, root
, objectid
, &inode
,
1899 data
.first_block
, data
.disk_block
,
1900 data
.num_blocks
, 0);
1905 key
.objectid
= objectid
;
1907 key
.type
= BTRFS_INODE_ITEM_KEY
;
1908 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 1);
1912 leaf
= path
.nodes
[0];
1913 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1914 write_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1915 btrfs_mark_buffer_dirty(leaf
);
1916 btrfs_release_path(root
, &path
);
1919 btrfs_release_path(root
, &path
);
1923 static int relocate_extents_range(struct btrfs_root
*fs_root
,
1924 struct btrfs_root
*ext2_root
,
1925 u64 start_byte
, u64 end_byte
)
1927 struct btrfs_fs_info
*info
= fs_root
->fs_info
;
1928 struct btrfs_root
*extent_root
= info
->extent_root
;
1929 struct btrfs_root
*cur_root
;
1930 struct btrfs_trans_handle
*trans
;
1931 struct btrfs_extent_ref
*ref_item
;
1932 struct extent_buffer
*leaf
;
1933 struct btrfs_key key
;
1934 struct btrfs_path path
;
1935 struct extent_io_tree reloc_tree
;
1946 btrfs_init_path(&path
);
1947 extent_io_tree_init(&reloc_tree
);
1949 cur_root
= (pass
% 2 == 0) ? ext2_root
: fs_root
;
1952 trans
= btrfs_start_transaction(cur_root
, 1);
1955 cur_byte
= start_byte
;
1957 key
.objectid
= cur_byte
;
1959 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1960 ret
= btrfs_search_slot(trans
, extent_root
,
1965 leaf
= path
.nodes
[0];
1966 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1967 ret
= btrfs_next_leaf(extent_root
, &path
);
1972 leaf
= path
.nodes
[0];
1975 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1976 if (key
.objectid
< cur_byte
||
1977 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1981 if (key
.objectid
>= end_byte
)
1984 cur_byte
= key
.objectid
;
1985 num_bytes
= key
.offset
;
1988 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1989 ret
= btrfs_next_leaf(extent_root
, &path
);
1994 leaf
= path
.nodes
[0];
1997 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1998 if (key
.objectid
!= cur_byte
)
2000 if (key
.type
!= BTRFS_EXTENT_REF_KEY
) {
2004 ref_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
2005 struct btrfs_extent_ref
);
2006 ref_root
= btrfs_ref_root(leaf
, ref_item
);
2007 ref_owner
= btrfs_ref_objectid(leaf
, ref_item
);
2008 ref_offset
= btrfs_ref_offset(leaf
, ref_item
);
2011 BUG_ON(ref_owner
< BTRFS_FIRST_FREE_OBJECTID
);
2012 if (ref_root
== cur_root
->root_key
.objectid
) {
2021 ret
= relocate_one_reference(trans
, cur_root
, cur_byte
,
2022 num_bytes
, ref_owner
,
2023 ref_offset
, &reloc_tree
);
2027 cur_byte
+= num_bytes
;
2028 btrfs_release_path(extent_root
, &path
);
2030 if (trans
->blocks_used
>= 4096) {
2031 ret
= btrfs_commit_transaction(trans
, cur_root
);
2033 trans
= btrfs_start_transaction(cur_root
, 1);
2037 btrfs_release_path(cur_root
, &path
);
2039 ret
= btrfs_commit_transaction(trans
, cur_root
);
2042 if (num_refs
> 0 && pass
++ < 4)
2045 ret
= (num_refs
> 0) ? -1 : 0;
2047 btrfs_release_path(cur_root
, &path
);
2048 extent_io_tree_cleanup(&reloc_tree
);
2053 * relocate data in system chunk
2055 static int cleanup_sys_chunk(struct btrfs_root
*fs_root
,
2056 struct btrfs_root
*ext2_root
)
2058 struct btrfs_block_group_cache
*cache
;
2064 cache
= btrfs_lookup_block_group(fs_root
->fs_info
, offset
);
2068 end_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
2069 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2070 ret
= relocate_extents_range(fs_root
, ext2_root
,
2071 cache
->key
.objectid
,
2082 static int fixup_chunk_mapping(struct btrfs_root
*root
)
2084 struct btrfs_trans_handle
*trans
;
2085 struct btrfs_fs_info
*info
= root
->fs_info
;
2086 struct btrfs_root
*chunk_root
= info
->chunk_root
;
2087 struct extent_buffer
*leaf
;
2088 struct btrfs_key key
;
2089 struct btrfs_path path
;
2090 struct btrfs_chunk chunk
;
2096 btrfs_init_path(&path
);
2098 trans
= btrfs_start_transaction(root
, 1);
2102 * recow the whole chunk tree. this will move all chunk tree blocks
2103 * into system block group.
2105 memset(&key
, 0, sizeof(key
));
2107 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2111 ret
= btrfs_next_leaf(chunk_root
, &path
);
2117 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2118 btrfs_release_path(chunk_root
, &path
);
2120 btrfs_release_path(chunk_root
, &path
);
2122 /* fixup the system chunk array in super block */
2123 btrfs_set_super_sys_array_size(&info
->super_copy
, 0);
2125 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2127 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2129 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 0);
2134 leaf
= path
.nodes
[0];
2135 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2136 ret
= btrfs_next_leaf(chunk_root
, &path
);
2141 leaf
= path
.nodes
[0];
2143 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2144 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
)
2147 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
2148 size
= btrfs_item_size_nr(leaf
, path
.slots
[0]);
2149 BUG_ON(size
!= sizeof(chunk
));
2150 read_extent_buffer(leaf
, &chunk
, ptr
, size
);
2151 type
= btrfs_stack_chunk_type(&chunk
);
2153 if (!(type
& BTRFS_BLOCK_GROUP_SYSTEM
))
2156 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
2164 ret
= btrfs_commit_transaction(trans
, root
);
2167 btrfs_release_path(chunk_root
, &path
);
2171 int do_convert(const char *devname
, int datacsum
, int packing
, int noxattr
)
2178 ext2_filsys ext2_fs
;
2179 struct btrfs_root
*root
;
2180 struct btrfs_root
*ext2_root
;
2182 ret
= open_ext2fs(devname
, &ext2_fs
);
2184 fprintf(stderr
, "unable to open the Ext2fs\n");
2187 blocksize
= ext2_fs
->blocksize
;
2188 total_bytes
= (u64
)ext2_fs
->super
->s_blocks_count
* blocksize
;
2189 if (blocksize
< 4096) {
2190 fprintf(stderr
, "block size is too small\n");
2193 if (!(ext2_fs
->super
->s_feature_incompat
&
2194 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
2195 fprintf(stderr
, "filetype feature is missing\n");
2198 for (i
= 0; i
< 6; i
++) {
2199 ret
= ext2_alloc_block(ext2_fs
, 0, blocks
+ i
);
2201 fprintf(stderr
, "not enough free space\n");
2204 blocks
[i
] *= blocksize
;
2206 super_bytenr
= blocks
[0];
2207 fd
= open(devname
, O_RDWR
);
2209 fprintf(stderr
, "unable to open %s\n", devname
);
2212 ret
= make_btrfs(fd
, devname
, ext2_fs
->super
->s_volume_name
,
2213 blocks
, total_bytes
, blocksize
, blocksize
,
2214 blocksize
, blocksize
);
2216 fprintf(stderr
, "unable to create initial ctree\n");
2219 /* create a system chunk that maps the whole device */
2220 ret
= prepare_system_chunk(fd
, super_bytenr
, blocksize
);
2222 fprintf(stderr
, "unable to update system chunk\n");
2225 root
= open_ctree_fd(fd
, devname
, super_bytenr
, O_RDWR
);
2227 fprintf(stderr
, "unable to open ctree\n");
2230 ret
= cache_free_extents(root
, ext2_fs
);
2232 fprintf(stderr
, "error during cache_free_extents %d\n", ret
);
2235 root
->fs_info
->extent_ops
= &extent_ops
;
2236 /* recover block allocation bitmap */
2237 for (i
= 0; i
< 6; i
++) {
2238 blocks
[i
] /= blocksize
;
2239 ext2_free_block(ext2_fs
, blocks
[i
]);
2241 ret
= init_btrfs(root
);
2243 fprintf(stderr
, "unable to setup the root tree\n");
2246 ext2_root
= create_subvol(root
, "ext2_saved");
2248 fprintf(stderr
, "unable to create subvol\n");
2251 printf("creating btrfs metadata.\n");
2252 ret
= copy_inodes(root
, ext2_fs
, datacsum
, packing
, noxattr
);
2254 fprintf(stderr
, "error during copy_inodes %d\n", ret
);
2257 printf("creating ext2fs image file.\n");
2258 ret
= create_ext2_image(ext2_root
, ext2_fs
, "image");
2260 fprintf(stderr
, "error during create_ext2_image %d\n", ret
);
2263 printf("cleaning up system chunk.\n");
2264 ret
= cleanup_sys_chunk(root
, ext2_root
);
2266 fprintf(stderr
, "error during cleanup_sys_chunk %d\n", ret
);
2269 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2270 ret
= close_ctree(root
);
2272 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2275 close_ext2fs(ext2_fs
);
2278 * If this step succeed, we get a mountable btrfs. Otherwise
2279 * the ext2fs is left unchanged.
2281 ret
= migrate_super_block(fd
, super_bytenr
, blocksize
);
2283 fprintf(stderr
, "unable to migrate super block\n");
2287 root
= open_ctree_fd(fd
, devname
, 0, O_RDWR
);
2289 fprintf(stderr
, "unable to open ctree\n");
2292 /* move chunk tree into system chunk. */
2293 ret
= fixup_chunk_mapping(root
);
2295 fprintf(stderr
, "error during fixup_chunk_tree\n");
2298 ret
= close_ctree(root
);
2301 printf("conversion complete.\n");
2304 fprintf(stderr
, "conversion aborted.\n");
2308 static int may_rollback(struct btrfs_root
*root
)
2310 struct btrfs_fs_info
*info
= root
->fs_info
;
2311 struct btrfs_multi_bio
*multi
= NULL
;
2319 if (btrfs_super_num_devices(&info
->super_copy
) != 1)
2322 bytenr
= BTRFS_SUPER_INFO_OFFSET
;
2323 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2326 ret
= btrfs_map_block(&info
->mapping_tree
, WRITE
, bytenr
,
2327 &length
, &multi
, 0);
2331 num_stripes
= multi
->num_stripes
;
2332 physical
= multi
->stripes
[0].physical
;
2335 if (num_stripes
!= 1 || physical
!= bytenr
)
2339 if (bytenr
>= total_bytes
)
2347 int do_rollback(const char *devname
, int force
)
2351 struct btrfs_root
*root
;
2352 struct btrfs_root
*ext2_root
;
2353 struct btrfs_root
*chunk_root
;
2354 struct btrfs_dir_item
*dir
;
2355 struct btrfs_inode_item
*inode
;
2356 struct btrfs_file_extent_item
*fi
;
2357 struct btrfs_trans_handle
*trans
;
2358 struct extent_buffer
*leaf
;
2359 struct btrfs_block_group_cache
*cache1
;
2360 struct btrfs_block_group_cache
*cache2
;
2361 struct btrfs_key key
;
2362 struct btrfs_path path
;
2363 struct extent_io_tree io_tree
;
2378 extent_io_tree_init(&io_tree
);
2380 fd
= open(devname
, O_RDWR
);
2382 fprintf(stderr
, "unable to open %s\n", devname
);
2385 root
= open_ctree_fd(fd
, devname
, 0, O_RDWR
);
2387 fprintf(stderr
, "unable to open ctree\n");
2390 ret
= may_rollback(root
);
2392 fprintf(stderr
, "unable to do rollback\n");
2396 sectorsize
= root
->sectorsize
;
2397 buf
= malloc(sectorsize
);
2399 fprintf(stderr
, "unable to allocate memory\n");
2403 btrfs_init_path(&path
);
2404 name
= "ext2_saved";
2405 root_dir
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2406 dir
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, &path
,
2407 root_dir
, name
, strlen(name
), 0);
2408 if (!dir
|| IS_ERR(dir
)) {
2409 fprintf(stderr
, "unable to find subvol %s\n", name
);
2412 leaf
= path
.nodes
[0];
2413 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2414 btrfs_release_path(root
->fs_info
->tree_root
, &path
);
2416 ext2_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2417 if (!ext2_root
|| IS_ERR(ext2_root
)) {
2418 fprintf(stderr
, "unable to open subvol %s\n", name
);
2423 root_dir
= btrfs_root_dirid(&root
->root_item
);
2424 dir
= btrfs_lookup_dir_item(NULL
, ext2_root
, &path
,
2425 root_dir
, name
, strlen(name
), 0);
2426 if (!dir
|| IS_ERR(dir
)) {
2427 fprintf(stderr
, "unable to find file %s\n", name
);
2430 leaf
= path
.nodes
[0];
2431 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2432 btrfs_release_path(ext2_root
, &path
);
2434 objectid
= key
.objectid
;
2436 ret
= btrfs_lookup_inode(NULL
, ext2_root
, &path
, &key
, 0);
2438 fprintf(stderr
, "unable to find inode item\n");
2441 leaf
= path
.nodes
[0];
2442 inode
= btrfs_item_ptr(leaf
, path
.slots
[0], struct btrfs_inode_item
);
2443 total_bytes
= btrfs_inode_size(leaf
, inode
);
2444 btrfs_release_path(ext2_root
, &path
);
2446 key
.objectid
= objectid
;
2448 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2449 ret
= btrfs_search_slot(NULL
, ext2_root
, &key
, &path
, 0, 0);
2451 fprintf(stderr
, "unable to find first file extent\n");
2452 btrfs_release_path(ext2_root
, &path
);
2456 /* build mapping tree for the relocated blocks */
2457 for (offset
= 0; offset
< total_bytes
; ) {
2458 leaf
= path
.nodes
[0];
2459 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2460 ret
= btrfs_next_leaf(root
, &path
);
2466 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2467 if (key
.objectid
!= objectid
|| key
.offset
!= offset
||
2468 btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2471 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
2472 struct btrfs_file_extent_item
);
2473 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2476 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2477 /* skip holes and direct mapped extents */
2478 if (bytenr
== 0 || bytenr
== offset
)
2481 bytenr
+= btrfs_file_extent_offset(leaf
, fi
);
2482 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
2484 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2485 cache2
= btrfs_lookup_block_group(root
->fs_info
,
2486 offset
+ num_bytes
- 1);
2487 if (!cache1
|| cache1
!= cache2
||
2488 !(cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
))
2491 set_extent_bits(&io_tree
, offset
, offset
+ num_bytes
- 1,
2492 EXTENT_LOCKED
, GFP_NOFS
);
2493 set_state_private(&io_tree
, offset
, bytenr
);
2495 offset
+= btrfs_file_extent_num_bytes(leaf
, fi
);
2498 btrfs_release_path(ext2_root
, &path
);
2499 btrfs_free_fs_root(ext2_root
->fs_info
, ext2_root
);
2501 if (offset
< total_bytes
) {
2502 fprintf(stderr
, "unable to build extent mapping\n");
2506 first_free
= BTRFS_SUPER_INFO_OFFSET
+ 2 * sectorsize
- 1;
2507 first_free
&= ~((u64
)sectorsize
- 1);
2508 /* backup for extent #0 should exist */
2509 if(!test_range_bit(&io_tree
, 0, first_free
- 1, EXTENT_LOCKED
, 1)) {
2510 fprintf(stderr
, "no backup for the first extent\n");
2513 /* force no allocation from system block group */
2514 root
->fs_info
->system_allocs
= -1;
2515 trans
= btrfs_start_transaction(root
, 1);
2518 * recow the whole chunk tree, this will remove all chunk tree blocks
2519 * from system block group
2521 chunk_root
= root
->fs_info
->chunk_root
;
2522 memset(&key
, 0, sizeof(key
));
2524 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2528 ret
= btrfs_next_leaf(chunk_root
, &path
);
2532 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2533 btrfs_release_path(chunk_root
, &path
);
2535 btrfs_release_path(chunk_root
, &path
);
2540 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2544 if (cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2545 num_bytes
+= btrfs_block_group_used(&cache1
->item
);
2547 offset
= cache1
->key
.objectid
+ cache1
->key
.offset
;
2549 /* only extent #0 left in system block group? */
2550 if (num_bytes
> first_free
) {
2551 fprintf(stderr
, "unable to empty system block group\n");
2554 /* create a system chunk that maps the whole device */
2555 ret
= prepare_system_chunk_sb(&root
->fs_info
->super_copy
);
2557 fprintf(stderr
, "unable to update system chunk\n");
2561 ret
= btrfs_commit_transaction(trans
, root
);
2564 ret
= close_ctree(root
);
2566 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2570 sb_bytenr
= (u64
)-1;
2571 /* copy all relocated blocks back */
2573 ret
= find_first_extent_bit(&io_tree
, 0, &start
, &end
,
2578 ret
= get_state_private(&io_tree
, start
, &bytenr
);
2581 clear_extent_bits(&io_tree
, start
, end
, EXTENT_LOCKED
,
2584 while (start
<= end
) {
2585 if (start
== BTRFS_SUPER_INFO_OFFSET
) {
2589 ret
= pread(fd
, buf
, sectorsize
, bytenr
);
2591 fprintf(stderr
, "error during pread %d\n", ret
);
2594 BUG_ON(ret
!= sectorsize
);
2595 ret
= pwrite(fd
, buf
, sectorsize
, start
);
2597 fprintf(stderr
, "error during pwrite %d\n", ret
);
2600 BUG_ON(ret
!= sectorsize
);
2602 start
+= sectorsize
;
2603 bytenr
+= sectorsize
;
2609 fprintf(stderr
, "error during fsync %d\n", ret
);
2613 * finally, overwrite btrfs super block.
2615 ret
= pread(fd
, buf
, sectorsize
, sb_bytenr
);
2617 fprintf(stderr
, "error during pread %d\n", ret
);
2620 BUG_ON(ret
!= sectorsize
);
2621 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
2623 fprintf(stderr
, "error during pwrite %d\n", ret
);
2626 BUG_ON(ret
!= sectorsize
);
2629 fprintf(stderr
, "error during fsync %d\n", ret
);
2635 extent_io_tree_cleanup(&io_tree
);
2636 printf("rollback complete.\n");
2639 fprintf(stderr
, "rollback aborted.\n");
2643 static void print_usage(void)
2645 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2646 printf("\t-d disable data checksum\n");
2647 printf("\t-i ignore xattrs and ACLs\n");
2648 printf("\t-n disable packing of small files\n");
2649 printf("\t-r roll back to ext2fs\n");
2652 int main(int argc
, char *argv
[])
2661 int c
= getopt(argc
, argv
, "dinr");
2682 argc
= argc
- optind
;
2688 file
= argv
[optind
];
2689 if (check_mounted(file
)) {
2690 fprintf(stderr
, "%s is mounted\n", file
);
2695 ret
= do_rollback(file
, 0);
2697 ret
= do_convert(file
, datacsum
, packing
, noxattr
);