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 #include "kerncompat.h"
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
25 #include <sys/types.h>
29 #include <uuid/uuid.h>
30 #include <linux/limits.h>
36 #include "transaction.h"
39 #include "task-utils.h"
40 #include <ext2fs/ext2_fs.h>
41 #include <ext2fs/ext2fs.h>
42 #include <ext2fs/ext2_ext_attr.h>
44 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
45 #define CONV_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
48 uint32_t max_copy_inodes
;
49 uint32_t cur_copy_inodes
;
50 struct task_info
*info
;
53 static void *print_copied_inodes(void *p
)
55 struct task_ctx
*priv
= p
;
56 const char work_indicator
[] = { '.', 'o', 'O', 'o' };
59 task_period_start(priv
->info
, 1000 /* 1s */);
62 printf("copy inodes [%c] [%10d/%10d]\r",
63 work_indicator
[count
% 4], priv
->cur_copy_inodes
,
64 priv
->max_copy_inodes
);
66 task_period_wait(priv
->info
);
72 static int after_copied_inodes(void *p
)
81 * Open Ext2fs in readonly mode, read block allocation bitmap and
82 * inode bitmap into memory.
84 static int open_ext2fs(const char *name
, ext2_filsys
*ret_fs
)
89 ret
= ext2fs_open(name
, 0, 0, 0, unix_io_manager
, &ext2_fs
);
91 fprintf(stderr
, "ext2fs_open: %s\n", error_message(ret
));
94 ret
= ext2fs_read_inode_bitmap(ext2_fs
);
96 fprintf(stderr
, "ext2fs_read_inode_bitmap: %s\n",
100 ret
= ext2fs_read_block_bitmap(ext2_fs
);
102 fprintf(stderr
, "ext2fs_read_block_bitmap: %s\n",
107 * search each block group for a free inode. this set up
108 * uninit block/inode bitmaps appropriately.
111 while (ino
<= ext2_fs
->super
->s_inodes_count
) {
113 ext2fs_new_inode(ext2_fs
, ino
, 0, NULL
, &foo
);
114 ino
+= EXT2_INODES_PER_GROUP(ext2_fs
->super
);
123 static int close_ext2fs(ext2_filsys fs
)
129 static int ext2_alloc_block(ext2_filsys fs
, u64 goal
, u64
*block_ret
)
133 if (!ext2fs_new_block(fs
, goal
, NULL
, &block
)) {
134 ext2fs_fast_mark_block_bitmap(fs
->block_map
, block
);
141 static int ext2_alloc_block_range(ext2_filsys fs
, u64 goal
, int num
,
145 ext2fs_block_bitmap bitmap
= fs
->block_map
;
146 blk_t start
= ext2fs_get_block_bitmap_start(bitmap
);
147 blk_t end
= ext2fs_get_block_bitmap_end(bitmap
);
149 for (block
= max_t(u64
, goal
, start
); block
+ num
< end
; block
++) {
150 if (ext2fs_fast_test_block_bitmap_range(bitmap
, block
, num
)) {
151 ext2fs_fast_mark_block_bitmap_range(bitmap
, block
,
160 static int ext2_free_block(ext2_filsys fs
, u64 block
)
162 BUG_ON(block
!= (blk_t
)block
);
163 ext2fs_fast_unmark_block_bitmap(fs
->block_map
, block
);
167 static int ext2_free_block_range(ext2_filsys fs
, u64 block
, int num
)
169 BUG_ON(block
!= (blk_t
)block
);
170 ext2fs_fast_unmark_block_bitmap_range(fs
->block_map
, block
, num
);
174 static int cache_free_extents(struct btrfs_root
*root
, ext2_filsys ext2_fs
)
180 u64 blocksize
= ext2_fs
->blocksize
;
182 block
= ext2_fs
->super
->s_first_data_block
;
183 for (; block
< ext2_fs
->super
->s_blocks_count
; block
++) {
184 if (ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
186 bytenr
= block
* blocksize
;
187 ret
= set_extent_dirty(&root
->fs_info
->free_space_cache
,
188 bytenr
, bytenr
+ blocksize
- 1, 0);
192 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
193 bytenr
= btrfs_sb_offset(i
);
194 bytenr
&= ~((u64
)BTRFS_STRIPE_LEN
- 1);
195 if (bytenr
>= blocksize
* ext2_fs
->super
->s_blocks_count
)
197 clear_extent_dirty(&root
->fs_info
->free_space_cache
, bytenr
,
198 bytenr
+ BTRFS_STRIPE_LEN
- 1, 0);
201 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
202 0, BTRFS_SUPER_INFO_OFFSET
- 1, 0);
207 static int custom_alloc_extent(struct btrfs_root
*root
, u64 num_bytes
,
208 u64 hint_byte
, struct btrfs_key
*ins
,
213 u64 last
= hint_byte
;
216 struct btrfs_block_group_cache
*cache
;
219 ret
= find_first_extent_bit(&root
->fs_info
->free_space_cache
,
220 last
, &start
, &end
, EXTENT_DIRTY
);
222 if (wrapped
++ == 0) {
230 start
= max(last
, start
);
232 if (last
- start
< num_bytes
)
235 last
= start
+ num_bytes
;
236 if (test_range_bit(&root
->fs_info
->pinned_extents
,
237 start
, last
- 1, EXTENT_DIRTY
, 0))
240 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
242 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
||
243 last
> cache
->key
.objectid
+ cache
->key
.offset
) {
244 last
= cache
->key
.objectid
+ cache
->key
.offset
;
249 BUG_ON(num_bytes
!= root
->nodesize
);
250 if (check_crossing_stripes(start
, num_bytes
)) {
251 last
= round_down(start
+ num_bytes
,
256 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
257 start
, start
+ num_bytes
- 1, 0);
259 ins
->objectid
= start
;
260 ins
->offset
= num_bytes
;
261 ins
->type
= BTRFS_EXTENT_ITEM_KEY
;
265 fprintf(stderr
, "not enough free space\n");
269 static int intersect_with_sb(u64 bytenr
, u64 num_bytes
)
274 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
275 offset
= btrfs_sb_offset(i
);
276 offset
&= ~((u64
)BTRFS_STRIPE_LEN
- 1);
278 if (bytenr
< offset
+ BTRFS_STRIPE_LEN
&&
279 bytenr
+ num_bytes
> offset
)
285 static int custom_free_extent(struct btrfs_root
*root
, u64 bytenr
,
288 return intersect_with_sb(bytenr
, num_bytes
);
291 static struct btrfs_extent_ops extent_ops
= {
292 .alloc_extent
= custom_alloc_extent
,
293 .free_extent
= custom_free_extent
,
296 static int convert_insert_dirent(struct btrfs_trans_handle
*trans
,
297 struct btrfs_root
*root
,
298 const char *name
, size_t name_len
,
299 u64 dir
, u64 objectid
,
300 u8 file_type
, u64 index_cnt
,
301 struct btrfs_inode_item
*inode
)
305 struct btrfs_key location
= {
306 .objectid
= objectid
,
308 .type
= BTRFS_INODE_ITEM_KEY
,
311 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
312 dir
, &location
, file_type
, index_cnt
);
315 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
,
316 objectid
, dir
, index_cnt
);
319 inode_size
= btrfs_stack_inode_size(inode
) + name_len
* 2;
320 btrfs_set_stack_inode_size(inode
, inode_size
);
325 struct dir_iterate_data
{
326 struct btrfs_trans_handle
*trans
;
327 struct btrfs_root
*root
;
328 struct btrfs_inode_item
*inode
;
335 static u8 filetype_conversion_table
[EXT2_FT_MAX
] = {
336 [EXT2_FT_UNKNOWN
] = BTRFS_FT_UNKNOWN
,
337 [EXT2_FT_REG_FILE
] = BTRFS_FT_REG_FILE
,
338 [EXT2_FT_DIR
] = BTRFS_FT_DIR
,
339 [EXT2_FT_CHRDEV
] = BTRFS_FT_CHRDEV
,
340 [EXT2_FT_BLKDEV
] = BTRFS_FT_BLKDEV
,
341 [EXT2_FT_FIFO
] = BTRFS_FT_FIFO
,
342 [EXT2_FT_SOCK
] = BTRFS_FT_SOCK
,
343 [EXT2_FT_SYMLINK
] = BTRFS_FT_SYMLINK
,
346 static int dir_iterate_proc(ext2_ino_t dir
, int entry
,
347 struct ext2_dir_entry
*dirent
,
348 int offset
, int blocksize
,
349 char *buf
,void *priv_data
)
354 char dotdot
[] = "..";
355 struct dir_iterate_data
*idata
= (struct dir_iterate_data
*)priv_data
;
358 name_len
= dirent
->name_len
& 0xFF;
360 objectid
= dirent
->inode
+ INO_OFFSET
;
361 if (!strncmp(dirent
->name
, dotdot
, name_len
)) {
363 BUG_ON(idata
->parent
!= 0);
364 idata
->parent
= objectid
;
368 if (dirent
->inode
< EXT2_GOOD_OLD_FIRST_INO
)
371 file_type
= dirent
->name_len
>> 8;
372 BUG_ON(file_type
> EXT2_FT_SYMLINK
);
374 ret
= convert_insert_dirent(idata
->trans
, idata
->root
, dirent
->name
,
375 name_len
, idata
->objectid
, objectid
,
376 filetype_conversion_table
[file_type
],
377 idata
->index_cnt
, idata
->inode
);
379 idata
->errcode
= ret
;
387 static int create_dir_entries(struct btrfs_trans_handle
*trans
,
388 struct btrfs_root
*root
, u64 objectid
,
389 struct btrfs_inode_item
*btrfs_inode
,
390 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
394 struct dir_iterate_data data
= {
397 .inode
= btrfs_inode
,
398 .objectid
= objectid
,
404 err
= ext2fs_dir_iterate2(ext2_fs
, ext2_ino
, 0, NULL
,
405 dir_iterate_proc
, &data
);
409 if (ret
== 0 && data
.parent
== objectid
) {
410 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2,
411 objectid
, objectid
, 0);
415 fprintf(stderr
, "ext2fs_dir_iterate2: %s\n", error_message(err
));
419 static int read_disk_extent(struct btrfs_root
*root
, u64 bytenr
,
420 u32 num_bytes
, char *buffer
)
423 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
425 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, bytenr
);
426 if (ret
!= num_bytes
)
435 static int csum_disk_extent(struct btrfs_trans_handle
*trans
,
436 struct btrfs_root
*root
,
437 u64 disk_bytenr
, u64 num_bytes
)
439 u32 blocksize
= root
->sectorsize
;
444 buffer
= malloc(blocksize
);
447 for (offset
= 0; offset
< num_bytes
; offset
+= blocksize
) {
448 ret
= read_disk_extent(root
, disk_bytenr
+ offset
,
452 ret
= btrfs_csum_file_block(trans
,
453 root
->fs_info
->csum_root
,
454 disk_bytenr
+ num_bytes
,
455 disk_bytenr
+ offset
,
464 struct blk_iterate_data
{
465 struct btrfs_trans_handle
*trans
;
466 struct btrfs_root
*root
;
467 struct btrfs_inode_item
*inode
;
477 static void init_blk_iterate_data(struct blk_iterate_data
*data
,
478 struct btrfs_trans_handle
*trans
,
479 struct btrfs_root
*root
,
480 struct btrfs_inode_item
*inode
,
481 u64 objectid
, int checksum
)
486 data
->objectid
= objectid
;
487 data
->first_block
= 0;
488 data
->disk_block
= 0;
489 data
->num_blocks
= 0;
490 data
->boundary
= (u64
)-1;
491 data
->checksum
= checksum
;
495 static int record_file_blocks(struct blk_iterate_data
*data
,
496 u64 file_block
, u64 disk_block
, u64 num_blocks
)
499 struct btrfs_root
*root
= data
->root
;
500 u64 file_pos
= file_block
* root
->sectorsize
;
501 u64 disk_bytenr
= disk_block
* root
->sectorsize
;
502 u64 num_bytes
= num_blocks
* root
->sectorsize
;
503 ret
= btrfs_record_file_extent(data
->trans
, data
->root
,
504 data
->objectid
, data
->inode
, file_pos
,
505 disk_bytenr
, num_bytes
);
507 if (ret
|| !data
->checksum
|| disk_bytenr
== 0)
510 return csum_disk_extent(data
->trans
, data
->root
, disk_bytenr
,
514 static int block_iterate_proc(u64 disk_block
, u64 file_block
,
515 struct blk_iterate_data
*idata
)
520 struct btrfs_root
*root
= idata
->root
;
521 struct btrfs_block_group_cache
*cache
;
522 u64 bytenr
= disk_block
* root
->sectorsize
;
524 sb_region
= intersect_with_sb(bytenr
, root
->sectorsize
);
525 do_barrier
= sb_region
|| disk_block
>= idata
->boundary
;
526 if ((idata
->num_blocks
> 0 && do_barrier
) ||
527 (file_block
> idata
->first_block
+ idata
->num_blocks
) ||
528 (disk_block
!= idata
->disk_block
+ idata
->num_blocks
)) {
529 if (idata
->num_blocks
> 0) {
530 ret
= record_file_blocks(idata
, idata
->first_block
,
535 idata
->first_block
+= idata
->num_blocks
;
536 idata
->num_blocks
= 0;
538 if (file_block
> idata
->first_block
) {
539 ret
= record_file_blocks(idata
, idata
->first_block
,
540 0, file_block
- idata
->first_block
);
546 bytenr
+= BTRFS_STRIPE_LEN
- 1;
547 bytenr
&= ~((u64
)BTRFS_STRIPE_LEN
- 1);
549 cache
= btrfs_lookup_block_group(root
->fs_info
, bytenr
);
551 bytenr
= cache
->key
.objectid
+ cache
->key
.offset
;
554 idata
->first_block
= file_block
;
555 idata
->disk_block
= disk_block
;
556 idata
->boundary
= bytenr
/ root
->sectorsize
;
563 static int __block_iterate_proc(ext2_filsys fs
, blk_t
*blocknr
,
564 e2_blkcnt_t blockcnt
, blk_t ref_block
,
565 int ref_offset
, void *priv_data
)
568 struct blk_iterate_data
*idata
;
569 idata
= (struct blk_iterate_data
*)priv_data
;
570 ret
= block_iterate_proc(*blocknr
, blockcnt
, idata
);
572 idata
->errcode
= ret
;
579 * traverse file's data blocks, record these data blocks as file extents.
581 static int create_file_extents(struct btrfs_trans_handle
*trans
,
582 struct btrfs_root
*root
, u64 objectid
,
583 struct btrfs_inode_item
*btrfs_inode
,
584 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
585 int datacsum
, int packing
)
591 u32 sectorsize
= root
->sectorsize
;
592 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
593 struct blk_iterate_data data
;
595 init_blk_iterate_data(&data
, trans
, root
, btrfs_inode
, objectid
,
598 err
= ext2fs_block_iterate2(ext2_fs
, ext2_ino
, BLOCK_FLAG_DATA_ONLY
,
599 NULL
, __block_iterate_proc
, &data
);
605 if (packing
&& data
.first_block
== 0 && data
.num_blocks
> 0 &&
606 inode_size
<= BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
607 u64 num_bytes
= data
.num_blocks
* sectorsize
;
608 u64 disk_bytenr
= data
.disk_block
* sectorsize
;
611 buffer
= malloc(num_bytes
);
614 ret
= read_disk_extent(root
, disk_bytenr
, num_bytes
, buffer
);
617 if (num_bytes
> inode_size
)
618 num_bytes
= inode_size
;
619 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
,
620 0, buffer
, num_bytes
);
623 nbytes
= btrfs_stack_inode_nbytes(btrfs_inode
) + num_bytes
;
624 btrfs_set_stack_inode_nbytes(btrfs_inode
, nbytes
);
625 } else if (data
.num_blocks
> 0) {
626 ret
= record_file_blocks(&data
, data
.first_block
,
627 data
.disk_block
, data
.num_blocks
);
631 data
.first_block
+= data
.num_blocks
;
632 last_block
= (inode_size
+ sectorsize
- 1) / sectorsize
;
633 if (last_block
> data
.first_block
) {
634 ret
= record_file_blocks(&data
, data
.first_block
, 0,
635 last_block
- data
.first_block
);
641 fprintf(stderr
, "ext2fs_block_iterate2: %s\n", error_message(err
));
645 static int create_symbol_link(struct btrfs_trans_handle
*trans
,
646 struct btrfs_root
*root
, u64 objectid
,
647 struct btrfs_inode_item
*btrfs_inode
,
648 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
649 struct ext2_inode
*ext2_inode
)
653 u64 inode_size
= btrfs_stack_inode_size(btrfs_inode
);
654 if (ext2fs_inode_data_blocks(ext2_fs
, ext2_inode
)) {
655 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
+ 1);
656 ret
= create_file_extents(trans
, root
, objectid
, btrfs_inode
,
657 ext2_fs
, ext2_ino
, 1, 1);
658 btrfs_set_stack_inode_size(btrfs_inode
, inode_size
);
662 pathname
= (char *)&(ext2_inode
->i_block
[0]);
663 BUG_ON(pathname
[inode_size
] != 0);
664 ret
= btrfs_insert_inline_extent(trans
, root
, objectid
, 0,
665 pathname
, inode_size
+ 1);
666 btrfs_set_stack_inode_nbytes(btrfs_inode
, inode_size
+ 1);
671 * Following xattr/acl related codes are based on codes in
672 * fs/ext3/xattr.c and fs/ext3/acl.c
674 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
675 #define EXT2_XATTR_BFIRST(ptr) \
676 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
677 #define EXT2_XATTR_IHDR(inode) \
678 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
679 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
680 #define EXT2_XATTR_IFIRST(inode) \
681 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
682 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
684 static int ext2_xattr_check_names(struct ext2_ext_attr_entry
*entry
,
687 struct ext2_ext_attr_entry
*next
;
689 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
690 next
= EXT2_EXT_ATTR_NEXT(entry
);
691 if ((void *)next
>= end
)
698 static int ext2_xattr_check_block(const char *buf
, size_t size
)
701 struct ext2_ext_attr_header
*header
= EXT2_XATTR_BHDR(buf
);
703 if (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC
||
704 header
->h_blocks
!= 1)
706 error
= ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf
), buf
+ size
);
710 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry
*entry
,
713 size_t value_size
= entry
->e_value_size
;
715 if (entry
->e_value_block
!= 0 || value_size
> size
||
716 entry
->e_value_offs
+ value_size
> size
)
721 #define EXT2_ACL_VERSION 0x0001
723 /* 23.2.5 acl_tag_t values */
725 #define ACL_UNDEFINED_TAG (0x00)
726 #define ACL_USER_OBJ (0x01)
727 #define ACL_USER (0x02)
728 #define ACL_GROUP_OBJ (0x04)
729 #define ACL_GROUP (0x08)
730 #define ACL_MASK (0x10)
731 #define ACL_OTHER (0x20)
733 /* 23.2.7 ACL qualifier constants */
735 #define ACL_UNDEFINED_ID ((id_t)-1)
746 } ext2_acl_entry_short
;
752 static inline int ext2_acl_count(size_t size
)
755 size
-= sizeof(ext2_acl_header
);
756 s
= size
- 4 * sizeof(ext2_acl_entry_short
);
758 if (size
% sizeof(ext2_acl_entry_short
))
760 return size
/ sizeof(ext2_acl_entry_short
);
762 if (s
% sizeof(ext2_acl_entry
))
764 return s
/ sizeof(ext2_acl_entry
) + 4;
768 #define ACL_EA_VERSION 0x0002
778 acl_ea_entry a_entries
[0];
781 static inline size_t acl_ea_size(int count
)
783 return sizeof(acl_ea_header
) + count
* sizeof(acl_ea_entry
);
786 static int ext2_acl_to_xattr(void *dst
, const void *src
,
787 size_t dst_size
, size_t src_size
)
790 const void *end
= src
+ src_size
;
791 acl_ea_header
*ext_acl
= (acl_ea_header
*)dst
;
792 acl_ea_entry
*dst_entry
= ext_acl
->a_entries
;
793 ext2_acl_entry
*src_entry
;
795 if (src_size
< sizeof(ext2_acl_header
))
797 if (((ext2_acl_header
*)src
)->a_version
!=
798 cpu_to_le32(EXT2_ACL_VERSION
))
800 src
+= sizeof(ext2_acl_header
);
801 count
= ext2_acl_count(src_size
);
805 BUG_ON(dst_size
< acl_ea_size(count
));
806 ext_acl
->a_version
= cpu_to_le32(ACL_EA_VERSION
);
807 for (i
= 0; i
< count
; i
++, dst_entry
++) {
808 src_entry
= (ext2_acl_entry
*)src
;
809 if (src
+ sizeof(ext2_acl_entry_short
) > end
)
811 dst_entry
->e_tag
= src_entry
->e_tag
;
812 dst_entry
->e_perm
= src_entry
->e_perm
;
813 switch (le16_to_cpu(src_entry
->e_tag
)) {
818 src
+= sizeof(ext2_acl_entry_short
);
819 dst_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
823 src
+= sizeof(ext2_acl_entry
);
826 dst_entry
->e_id
= src_entry
->e_id
;
839 static char *xattr_prefix_table
[] = {
841 [2] = "system.posix_acl_access",
842 [3] = "system.posix_acl_default",
847 static int copy_single_xattr(struct btrfs_trans_handle
*trans
,
848 struct btrfs_root
*root
, u64 objectid
,
849 struct ext2_ext_attr_entry
*entry
,
850 const void *data
, u32 datalen
)
855 void *databuf
= NULL
;
856 char namebuf
[XATTR_NAME_MAX
+ 1];
858 name_index
= entry
->e_name_index
;
859 if (name_index
>= ARRAY_SIZE(xattr_prefix_table
) ||
860 xattr_prefix_table
[name_index
] == NULL
)
862 name_len
= strlen(xattr_prefix_table
[name_index
]) +
864 if (name_len
>= sizeof(namebuf
))
867 if (name_index
== 2 || name_index
== 3) {
868 size_t bufsize
= acl_ea_size(ext2_acl_count(datalen
));
869 databuf
= malloc(bufsize
);
872 ret
= ext2_acl_to_xattr(databuf
, data
, bufsize
, datalen
);
878 strncpy(namebuf
, xattr_prefix_table
[name_index
], XATTR_NAME_MAX
);
879 strncat(namebuf
, EXT2_EXT_ATTR_NAME(entry
), entry
->e_name_len
);
880 if (name_len
+ datalen
> BTRFS_LEAF_DATA_SIZE(root
) -
881 sizeof(struct btrfs_item
) - sizeof(struct btrfs_dir_item
)) {
882 fprintf(stderr
, "skip large xattr on inode %Lu name %.*s\n",
883 objectid
- INO_OFFSET
, name_len
, namebuf
);
886 ret
= btrfs_insert_xattr_item(trans
, root
, namebuf
, name_len
,
887 data
, datalen
, objectid
);
893 static int copy_extended_attrs(struct btrfs_trans_handle
*trans
,
894 struct btrfs_root
*root
, u64 objectid
,
895 struct btrfs_inode_item
*btrfs_inode
,
896 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
)
902 u32 block_size
= ext2_fs
->blocksize
;
903 u32 inode_size
= EXT2_INODE_SIZE(ext2_fs
->super
);
904 struct ext2_inode_large
*ext2_inode
;
905 struct ext2_ext_attr_entry
*entry
;
908 char inode_buf
[EXT2_GOOD_OLD_INODE_SIZE
];
910 if (inode_size
<= EXT2_GOOD_OLD_INODE_SIZE
) {
911 ext2_inode
= (struct ext2_inode_large
*)inode_buf
;
913 ext2_inode
= (struct ext2_inode_large
*)malloc(inode_size
);
917 err
= ext2fs_read_inode_full(ext2_fs
, ext2_ino
, (void *)ext2_inode
,
920 fprintf(stderr
, "ext2fs_read_inode_full: %s\n",
926 if (ext2_ino
> ext2_fs
->super
->s_first_ino
&&
927 inode_size
> EXT2_GOOD_OLD_INODE_SIZE
) {
928 if (EXT2_GOOD_OLD_INODE_SIZE
+
929 ext2_inode
->i_extra_isize
> inode_size
) {
933 if (ext2_inode
->i_extra_isize
!= 0 &&
934 EXT2_XATTR_IHDR(ext2_inode
)->h_magic
==
935 EXT2_EXT_ATTR_MAGIC
) {
941 void *end
= (void *)ext2_inode
+ inode_size
;
942 entry
= EXT2_XATTR_IFIRST(ext2_inode
);
943 total
= end
- (void *)entry
;
944 ret
= ext2_xattr_check_names(entry
, end
);
947 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
948 ret
= ext2_xattr_check_entry(entry
, total
);
951 data
= (void *)EXT2_XATTR_IFIRST(ext2_inode
) +
953 datalen
= entry
->e_value_size
;
954 ret
= copy_single_xattr(trans
, root
, objectid
,
955 entry
, data
, datalen
);
958 entry
= EXT2_EXT_ATTR_NEXT(entry
);
962 if (ext2_inode
->i_file_acl
== 0)
965 buffer
= malloc(block_size
);
970 err
= ext2fs_read_ext_attr(ext2_fs
, ext2_inode
->i_file_acl
, buffer
);
972 fprintf(stderr
, "ext2fs_read_ext_attr: %s\n",
977 ret
= ext2_xattr_check_block(buffer
, block_size
);
981 entry
= EXT2_XATTR_BFIRST(buffer
);
982 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
983 ret
= ext2_xattr_check_entry(entry
, block_size
);
986 data
= buffer
+ entry
->e_value_offs
;
987 datalen
= entry
->e_value_size
;
988 ret
= copy_single_xattr(trans
, root
, objectid
,
989 entry
, data
, datalen
);
992 entry
= EXT2_EXT_ATTR_NEXT(entry
);
996 if ((void *)ext2_inode
!= inode_buf
)
1000 #define MINORBITS 20
1001 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
1003 static inline dev_t
old_decode_dev(u16 val
)
1005 return MKDEV((val
>> 8) & 255, val
& 255);
1008 static inline dev_t
new_decode_dev(u32 dev
)
1010 unsigned major
= (dev
& 0xfff00) >> 8;
1011 unsigned minor
= (dev
& 0xff) | ((dev
>> 12) & 0xfff00);
1012 return MKDEV(major
, minor
);
1015 static int copy_inode_item(struct btrfs_inode_item
*dst
,
1016 struct ext2_inode
*src
, u32 blocksize
)
1018 btrfs_set_stack_inode_generation(dst
, 1);
1019 btrfs_set_stack_inode_sequence(dst
, 0);
1020 btrfs_set_stack_inode_transid(dst
, 1);
1021 btrfs_set_stack_inode_size(dst
, src
->i_size
);
1022 btrfs_set_stack_inode_nbytes(dst
, 0);
1023 btrfs_set_stack_inode_block_group(dst
, 0);
1024 btrfs_set_stack_inode_nlink(dst
, src
->i_links_count
);
1025 btrfs_set_stack_inode_uid(dst
, src
->i_uid
| (src
->i_uid_high
<< 16));
1026 btrfs_set_stack_inode_gid(dst
, src
->i_gid
| (src
->i_gid_high
<< 16));
1027 btrfs_set_stack_inode_mode(dst
, src
->i_mode
);
1028 btrfs_set_stack_inode_rdev(dst
, 0);
1029 btrfs_set_stack_inode_flags(dst
, 0);
1030 btrfs_set_stack_timespec_sec(&dst
->atime
, src
->i_atime
);
1031 btrfs_set_stack_timespec_nsec(&dst
->atime
, 0);
1032 btrfs_set_stack_timespec_sec(&dst
->ctime
, src
->i_ctime
);
1033 btrfs_set_stack_timespec_nsec(&dst
->ctime
, 0);
1034 btrfs_set_stack_timespec_sec(&dst
->mtime
, src
->i_mtime
);
1035 btrfs_set_stack_timespec_nsec(&dst
->mtime
, 0);
1036 btrfs_set_stack_timespec_sec(&dst
->otime
, 0);
1037 btrfs_set_stack_timespec_nsec(&dst
->otime
, 0);
1039 if (S_ISDIR(src
->i_mode
)) {
1040 btrfs_set_stack_inode_size(dst
, 0);
1041 btrfs_set_stack_inode_nlink(dst
, 1);
1043 if (S_ISREG(src
->i_mode
)) {
1044 btrfs_set_stack_inode_size(dst
, (u64
)src
->i_size_high
<< 32 |
1047 if (!S_ISREG(src
->i_mode
) && !S_ISDIR(src
->i_mode
) &&
1048 !S_ISLNK(src
->i_mode
)) {
1049 if (src
->i_block
[0]) {
1050 btrfs_set_stack_inode_rdev(dst
,
1051 old_decode_dev(src
->i_block
[0]));
1053 btrfs_set_stack_inode_rdev(dst
,
1054 new_decode_dev(src
->i_block
[1]));
1057 memset(&dst
->reserved
, 0, sizeof(dst
->reserved
));
1063 * copy a single inode. do all the required works, such as cloning
1064 * inode item, creating file extents and creating directory entries.
1066 static int copy_single_inode(struct btrfs_trans_handle
*trans
,
1067 struct btrfs_root
*root
, u64 objectid
,
1068 ext2_filsys ext2_fs
, ext2_ino_t ext2_ino
,
1069 struct ext2_inode
*ext2_inode
,
1070 int datacsum
, int packing
, int noxattr
)
1073 struct btrfs_inode_item btrfs_inode
;
1075 if (ext2_inode
->i_links_count
== 0)
1078 copy_inode_item(&btrfs_inode
, ext2_inode
, ext2_fs
->blocksize
);
1079 if (!datacsum
&& S_ISREG(ext2_inode
->i_mode
)) {
1080 u32 flags
= btrfs_stack_inode_flags(&btrfs_inode
) |
1081 BTRFS_INODE_NODATASUM
;
1082 btrfs_set_stack_inode_flags(&btrfs_inode
, flags
);
1085 switch (ext2_inode
->i_mode
& S_IFMT
) {
1087 ret
= create_file_extents(trans
, root
, objectid
, &btrfs_inode
,
1088 ext2_fs
, ext2_ino
, datacsum
, packing
);
1091 ret
= create_dir_entries(trans
, root
, objectid
, &btrfs_inode
,
1095 ret
= create_symbol_link(trans
, root
, objectid
, &btrfs_inode
,
1096 ext2_fs
, ext2_ino
, ext2_inode
);
1106 ret
= copy_extended_attrs(trans
, root
, objectid
, &btrfs_inode
,
1111 return btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1114 static int copy_disk_extent(struct btrfs_root
*root
, u64 dst_bytenr
,
1115 u64 src_bytenr
, u32 num_bytes
)
1119 struct btrfs_fs_devices
*fs_devs
= root
->fs_info
->fs_devices
;
1121 buffer
= malloc(num_bytes
);
1124 ret
= pread(fs_devs
->latest_bdev
, buffer
, num_bytes
, src_bytenr
);
1125 if (ret
!= num_bytes
)
1127 ret
= pwrite(fs_devs
->latest_bdev
, buffer
, num_bytes
, dst_bytenr
);
1128 if (ret
!= num_bytes
)
1138 * scan ext2's inode bitmap and copy all used inodes.
1140 static int copy_inodes(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1141 int datacsum
, int packing
, int noxattr
, struct task_ctx
*p
)
1145 ext2_inode_scan ext2_scan
;
1146 struct ext2_inode ext2_inode
;
1147 ext2_ino_t ext2_ino
;
1149 struct btrfs_trans_handle
*trans
;
1151 trans
= btrfs_start_transaction(root
, 1);
1154 err
= ext2fs_open_inode_scan(ext2_fs
, 0, &ext2_scan
);
1156 fprintf(stderr
, "ext2fs_open_inode_scan: %s\n", error_message(err
));
1159 while (!(err
= ext2fs_get_next_inode(ext2_scan
, &ext2_ino
,
1161 /* no more inodes */
1164 /* skip special inode in ext2fs */
1165 if (ext2_ino
< EXT2_GOOD_OLD_FIRST_INO
&&
1166 ext2_ino
!= EXT2_ROOT_INO
)
1168 objectid
= ext2_ino
+ INO_OFFSET
;
1169 ret
= copy_single_inode(trans
, root
,
1170 objectid
, ext2_fs
, ext2_ino
,
1171 &ext2_inode
, datacsum
, packing
,
1173 p
->cur_copy_inodes
++;
1176 if (trans
->blocks_used
>= 4096) {
1177 ret
= btrfs_commit_transaction(trans
, root
);
1179 trans
= btrfs_start_transaction(root
, 1);
1184 fprintf(stderr
, "ext2fs_get_next_inode: %s\n", error_message(err
));
1187 ret
= btrfs_commit_transaction(trans
, root
);
1189 ext2fs_close_inode_scan(ext2_scan
);
1195 * Construct a range of ext2fs image file.
1196 * scan block allocation bitmap, find all blocks used by the ext2fs
1197 * in this range and create file extents that point to these blocks.
1199 * Note: Before calling the function, no file extent points to blocks
1202 static int create_image_file_range(struct btrfs_trans_handle
*trans
,
1203 struct btrfs_root
*root
, u64 objectid
,
1204 struct btrfs_inode_item
*inode
,
1205 u64 start_byte
, u64 end_byte
,
1206 ext2_filsys ext2_fs
, int datacsum
)
1208 u32 blocksize
= ext2_fs
->blocksize
;
1209 u32 block
= start_byte
/ blocksize
;
1210 u32 last_block
= (end_byte
+ blocksize
- 1) / blocksize
;
1212 struct blk_iterate_data data
;
1214 init_blk_iterate_data(&data
, trans
, root
, inode
, objectid
, datacsum
);
1215 data
.first_block
= block
;
1217 for (; start_byte
< end_byte
; block
++, start_byte
+= blocksize
) {
1218 if (!ext2fs_fast_test_block_bitmap(ext2_fs
->block_map
, block
))
1220 ret
= block_iterate_proc(block
, block
, &data
);
1224 if (data
.num_blocks
> 0) {
1225 ret
= record_file_blocks(&data
, data
.first_block
,
1226 data
.disk_block
, data
.num_blocks
);
1229 data
.first_block
+= data
.num_blocks
;
1231 if (last_block
> data
.first_block
) {
1232 ret
= record_file_blocks(&data
, data
.first_block
, 0,
1233 last_block
- data
.first_block
);
1241 * Create the ext2fs image file.
1243 static int create_ext2_image(struct btrfs_root
*root
, ext2_filsys ext2_fs
,
1244 const char *name
, int datacsum
)
1247 struct btrfs_key key
;
1248 struct btrfs_key location
;
1249 struct btrfs_path path
;
1250 struct btrfs_inode_item btrfs_inode
;
1251 struct btrfs_inode_item
*inode_item
;
1252 struct extent_buffer
*leaf
;
1253 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1254 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
1255 struct btrfs_trans_handle
*trans
;
1256 struct btrfs_extent_item
*ei
;
1257 struct btrfs_extent_inline_ref
*iref
;
1258 struct btrfs_extent_data_ref
*dref
;
1265 u64 flags
= BTRFS_INODE_READONLY
;
1266 u32 sectorsize
= root
->sectorsize
;
1268 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
1269 first_free
= BTRFS_SUPER_INFO_OFFSET
+ sectorsize
* 2 - 1;
1270 first_free
&= ~((u64
)sectorsize
- 1);
1272 flags
|= BTRFS_INODE_NODATASUM
;
1274 memset(&btrfs_inode
, 0, sizeof(btrfs_inode
));
1275 btrfs_set_stack_inode_generation(&btrfs_inode
, 1);
1276 btrfs_set_stack_inode_size(&btrfs_inode
, total_bytes
);
1277 btrfs_set_stack_inode_nlink(&btrfs_inode
, 1);
1278 btrfs_set_stack_inode_nbytes(&btrfs_inode
, 0);
1279 btrfs_set_stack_inode_mode(&btrfs_inode
, S_IFREG
| 0400);
1280 btrfs_set_stack_inode_flags(&btrfs_inode
, flags
);
1281 btrfs_init_path(&path
);
1282 trans
= btrfs_start_transaction(root
, 1);
1285 objectid
= btrfs_root_dirid(&root
->root_item
);
1286 ret
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
1291 * copy blocks covered by extent #0 to new positions. extent #0 is
1292 * special, we can't rely on relocate_extents_range to relocate it.
1294 for (last_byte
= 0; last_byte
< first_free
; last_byte
+= sectorsize
) {
1295 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
, 0);
1298 ret
= copy_disk_extent(root
, key
.objectid
, last_byte
,
1302 ret
= btrfs_record_file_extent(trans
, root
, objectid
,
1303 &btrfs_inode
, last_byte
,
1304 key
.objectid
, sectorsize
);
1308 ret
= csum_disk_extent(trans
, root
, key
.objectid
,
1316 key
.objectid
= last_byte
;
1318 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1319 ret
= btrfs_search_slot(trans
, fs_info
->extent_root
,
1324 leaf
= path
.nodes
[0];
1325 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
1326 ret
= btrfs_next_leaf(extent_root
, &path
);
1331 leaf
= path
.nodes
[0];
1333 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
1334 if (last_byte
> key
.objectid
||
1335 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
1340 bytenr
= key
.objectid
;
1341 num_bytes
= key
.offset
;
1342 ei
= btrfs_item_ptr(leaf
, path
.slots
[0],
1343 struct btrfs_extent_item
);
1344 if (!(btrfs_extent_flags(leaf
, ei
) & BTRFS_EXTENT_FLAG_DATA
)) {
1349 BUG_ON(btrfs_item_size_nr(leaf
, path
.slots
[0]) != sizeof(*ei
) +
1350 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY
));
1352 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
1353 key
.type
= btrfs_extent_inline_ref_type(leaf
, iref
);
1354 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
);
1355 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
1356 if (btrfs_extent_data_ref_root(leaf
, dref
) !=
1357 BTRFS_FS_TREE_OBJECTID
) {
1362 if (bytenr
> last_byte
) {
1363 ret
= create_image_file_range(trans
, root
, objectid
,
1364 &btrfs_inode
, last_byte
,
1370 ret
= btrfs_record_file_extent(trans
, root
, objectid
,
1371 &btrfs_inode
, bytenr
, bytenr
,
1375 last_byte
= bytenr
+ num_bytes
;
1376 btrfs_release_path(&path
);
1378 if (trans
->blocks_used
>= 4096) {
1379 ret
= btrfs_commit_transaction(trans
, root
);
1381 trans
= btrfs_start_transaction(root
, 1);
1385 btrfs_release_path(&path
);
1386 if (total_bytes
> last_byte
) {
1387 ret
= create_image_file_range(trans
, root
, objectid
,
1388 &btrfs_inode
, last_byte
,
1389 total_bytes
, ext2_fs
,
1395 ret
= btrfs_insert_inode(trans
, root
, objectid
, &btrfs_inode
);
1399 location
.objectid
= objectid
;
1400 location
.offset
= 0;
1401 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1402 ret
= btrfs_insert_dir_item(trans
, root
, name
, strlen(name
),
1403 btrfs_root_dirid(&root
->root_item
),
1404 &location
, BTRFS_FT_REG_FILE
, objectid
);
1407 ret
= btrfs_insert_inode_ref(trans
, root
, name
, strlen(name
),
1409 btrfs_root_dirid(&root
->root_item
),
1413 location
.objectid
= btrfs_root_dirid(&root
->root_item
);
1414 location
.offset
= 0;
1415 btrfs_set_key_type(&location
, BTRFS_INODE_ITEM_KEY
);
1416 ret
= btrfs_lookup_inode(trans
, root
, &path
, &location
, 1);
1419 leaf
= path
.nodes
[0];
1420 inode_item
= btrfs_item_ptr(leaf
, path
.slots
[0],
1421 struct btrfs_inode_item
);
1422 btrfs_set_inode_size(leaf
, inode_item
, strlen(name
) * 2 +
1423 btrfs_inode_size(leaf
, inode_item
));
1424 btrfs_mark_buffer_dirty(leaf
);
1425 btrfs_release_path(&path
);
1426 ret
= btrfs_commit_transaction(trans
, root
);
1429 btrfs_release_path(&path
);
1433 static struct btrfs_root
* link_subvol(struct btrfs_root
*root
,
1434 const char *base
, u64 root_objectid
)
1436 struct btrfs_trans_handle
*trans
;
1437 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1438 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1439 struct btrfs_root
*new_root
= NULL
;
1440 struct btrfs_path
*path
;
1441 struct btrfs_inode_item
*inode_item
;
1442 struct extent_buffer
*leaf
;
1443 struct btrfs_key key
;
1444 u64 dirid
= btrfs_root_dirid(&root
->root_item
);
1446 char buf
[BTRFS_NAME_LEN
+ 1]; /* for snprintf null */
1452 if (len
< 1 || len
> BTRFS_NAME_LEN
)
1455 path
= btrfs_alloc_path();
1458 key
.objectid
= dirid
;
1459 key
.type
= BTRFS_DIR_INDEX_KEY
;
1460 key
.offset
= (u64
)-1;
1462 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1465 if (path
->slots
[0] > 0) {
1467 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1468 if (key
.objectid
== dirid
&& key
.type
== BTRFS_DIR_INDEX_KEY
)
1469 index
= key
.offset
+ 1;
1471 btrfs_release_path(path
);
1473 trans
= btrfs_start_transaction(root
, 1);
1476 key
.objectid
= dirid
;
1478 key
.type
= BTRFS_INODE_ITEM_KEY
;
1480 ret
= btrfs_lookup_inode(trans
, root
, path
, &key
, 1);
1482 leaf
= path
->nodes
[0];
1483 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1484 struct btrfs_inode_item
);
1486 key
.objectid
= root_objectid
;
1487 key
.offset
= (u64
)-1;
1488 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1490 memcpy(buf
, base
, len
);
1491 for (i
= 0; i
< 1024; i
++) {
1492 ret
= btrfs_insert_dir_item(trans
, root
, buf
, len
,
1493 dirid
, &key
, BTRFS_FT_DIR
, index
);
1496 len
= snprintf(buf
, ARRAY_SIZE(buf
), "%s%d", base
, i
);
1497 if (len
< 1 || len
> BTRFS_NAME_LEN
) {
1505 btrfs_set_inode_size(leaf
, inode_item
, len
* 2 +
1506 btrfs_inode_size(leaf
, inode_item
));
1507 btrfs_mark_buffer_dirty(leaf
);
1508 btrfs_release_path(path
);
1510 /* add the backref first */
1511 ret
= btrfs_add_root_ref(trans
, tree_root
, root_objectid
,
1512 BTRFS_ROOT_BACKREF_KEY
,
1513 root
->root_key
.objectid
,
1514 dirid
, index
, buf
, len
);
1517 /* now add the forward ref */
1518 ret
= btrfs_add_root_ref(trans
, tree_root
, root
->root_key
.objectid
,
1519 BTRFS_ROOT_REF_KEY
, root_objectid
,
1520 dirid
, index
, buf
, len
);
1522 ret
= btrfs_commit_transaction(trans
, root
);
1525 new_root
= btrfs_read_fs_root(fs_info
, &key
);
1526 if (IS_ERR(new_root
))
1529 btrfs_free_path(path
);
1533 static int create_chunk_mapping(struct btrfs_trans_handle
*trans
,
1534 struct btrfs_root
*root
)
1536 struct btrfs_fs_info
*info
= root
->fs_info
;
1537 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1538 struct btrfs_root
*extent_root
= info
->extent_root
;
1539 struct btrfs_device
*device
;
1540 struct btrfs_block_group_cache
*cache
;
1541 struct btrfs_dev_extent
*extent
;
1542 struct extent_buffer
*leaf
;
1543 struct btrfs_chunk chunk
;
1544 struct btrfs_key key
;
1545 struct btrfs_path path
;
1551 btrfs_init_path(&path
);
1553 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1554 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1556 BUG_ON(list_empty(&info
->fs_devices
->devices
));
1557 device
= list_entry(info
->fs_devices
->devices
.next
,
1558 struct btrfs_device
, dev_list
);
1559 BUG_ON(device
->devid
!= info
->fs_devices
->latest_devid
);
1561 /* delete device extent created by make_btrfs */
1562 key
.objectid
= device
->devid
;
1564 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1565 ret
= btrfs_search_slot(trans
, device
->dev_root
, &key
, &path
, -1, 1);
1570 ret
= btrfs_del_item(trans
, device
->dev_root
, &path
);
1573 btrfs_release_path(&path
);
1575 /* delete chunk item created by make_btrfs */
1576 key
.objectid
= chunk_objectid
;
1578 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1579 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, -1, 1);
1584 ret
= btrfs_del_item(trans
, chunk_root
, &path
);
1587 btrfs_release_path(&path
);
1589 /* for each block group, create device extent and chunk item */
1591 while (cur_start
< total_bytes
) {
1592 cache
= btrfs_lookup_block_group(root
->fs_info
, cur_start
);
1595 /* insert device extent */
1596 key
.objectid
= device
->devid
;
1597 key
.offset
= cache
->key
.objectid
;
1598 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1599 ret
= btrfs_insert_empty_item(trans
, device
->dev_root
, &path
,
1600 &key
, sizeof(*extent
));
1604 leaf
= path
.nodes
[0];
1605 extent
= btrfs_item_ptr(leaf
, path
.slots
[0],
1606 struct btrfs_dev_extent
);
1608 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1609 chunk_root
->root_key
.objectid
);
1610 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1612 btrfs_set_dev_extent_chunk_offset(leaf
, extent
,
1613 cache
->key
.objectid
);
1614 btrfs_set_dev_extent_length(leaf
, extent
, cache
->key
.offset
);
1615 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1616 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1618 btrfs_mark_buffer_dirty(leaf
);
1619 btrfs_release_path(&path
);
1621 /* insert chunk item */
1622 btrfs_set_stack_chunk_length(&chunk
, cache
->key
.offset
);
1623 btrfs_set_stack_chunk_owner(&chunk
,
1624 extent_root
->root_key
.objectid
);
1625 btrfs_set_stack_chunk_stripe_len(&chunk
, BTRFS_STRIPE_LEN
);
1626 btrfs_set_stack_chunk_type(&chunk
, cache
->flags
);
1627 btrfs_set_stack_chunk_io_align(&chunk
, device
->io_align
);
1628 btrfs_set_stack_chunk_io_width(&chunk
, device
->io_width
);
1629 btrfs_set_stack_chunk_sector_size(&chunk
, device
->sector_size
);
1630 btrfs_set_stack_chunk_num_stripes(&chunk
, 1);
1631 btrfs_set_stack_chunk_sub_stripes(&chunk
, 0);
1632 btrfs_set_stack_stripe_devid(&chunk
.stripe
, device
->devid
);
1633 btrfs_set_stack_stripe_offset(&chunk
.stripe
,
1634 cache
->key
.objectid
);
1635 memcpy(&chunk
.stripe
.dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1637 key
.objectid
= chunk_objectid
;
1638 key
.offset
= cache
->key
.objectid
;
1639 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1641 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, &chunk
,
1642 btrfs_chunk_item_size(1));
1646 cur_start
= cache
->key
.objectid
+ cache
->key
.offset
;
1649 device
->bytes_used
= total_bytes
;
1650 ret
= btrfs_update_device(trans
, device
);
1652 btrfs_release_path(&path
);
1656 static int create_subvol(struct btrfs_trans_handle
*trans
,
1657 struct btrfs_root
*root
, u64 root_objectid
)
1659 struct extent_buffer
*tmp
;
1660 struct btrfs_root
*new_root
;
1661 struct btrfs_key key
;
1662 struct btrfs_root_item root_item
;
1665 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
,
1669 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
1670 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
1671 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
1672 btrfs_set_root_generation(&root_item
, trans
->transid
);
1673 free_extent_buffer(tmp
);
1675 key
.objectid
= root_objectid
;
1676 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1677 key
.offset
= trans
->transid
;
1678 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1681 key
.offset
= (u64
)-1;
1682 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
1683 BUG_ON(!new_root
|| IS_ERR(new_root
));
1685 ret
= btrfs_make_root_dir(trans
, new_root
, BTRFS_FIRST_FREE_OBJECTID
);
1691 static int init_btrfs(struct btrfs_root
*root
)
1694 struct btrfs_key location
;
1695 struct btrfs_trans_handle
*trans
;
1696 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1697 struct extent_buffer
*tmp
;
1699 trans
= btrfs_start_transaction(root
, 1);
1701 ret
= btrfs_make_block_groups(trans
, root
);
1704 ret
= btrfs_fix_block_accounting(trans
, root
);
1707 ret
= create_chunk_mapping(trans
, root
);
1710 ret
= btrfs_make_root_dir(trans
, fs_info
->tree_root
,
1711 BTRFS_ROOT_TREE_DIR_OBJECTID
);
1714 memcpy(&location
, &root
->root_key
, sizeof(location
));
1715 location
.offset
= (u64
)-1;
1716 ret
= btrfs_insert_dir_item(trans
, fs_info
->tree_root
, "default", 7,
1717 btrfs_super_root_dir(fs_info
->super_copy
),
1718 &location
, BTRFS_FT_DIR
, 0);
1721 ret
= btrfs_insert_inode_ref(trans
, fs_info
->tree_root
, "default", 7,
1723 btrfs_super_root_dir(fs_info
->super_copy
), 0);
1726 btrfs_set_root_dirid(&fs_info
->fs_root
->root_item
,
1727 BTRFS_FIRST_FREE_OBJECTID
);
1729 /* subvol for ext2 image file */
1730 ret
= create_subvol(trans
, root
, CONV_IMAGE_SUBVOL_OBJECTID
);
1732 /* subvol for data relocation */
1733 ret
= create_subvol(trans
, root
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
1736 extent_buffer_get(fs_info
->csum_root
->node
);
1737 ret
= __btrfs_cow_block(trans
, fs_info
->csum_root
,
1738 fs_info
->csum_root
->node
, NULL
, 0, &tmp
, 0, 0);
1740 free_extent_buffer(tmp
);
1742 ret
= btrfs_commit_transaction(trans
, root
);
1749 * Migrate super block to its default position and zero 0 ~ 16k
1751 static int migrate_super_block(int fd
, u64 old_bytenr
, u32 sectorsize
)
1754 struct extent_buffer
*buf
;
1755 struct btrfs_super_block
*super
;
1759 BUG_ON(sectorsize
< sizeof(*super
));
1760 buf
= malloc(sizeof(*buf
) + sectorsize
);
1764 buf
->len
= sectorsize
;
1765 ret
= pread(fd
, buf
->data
, sectorsize
, old_bytenr
);
1766 if (ret
!= sectorsize
)
1769 super
= (struct btrfs_super_block
*)buf
->data
;
1770 BUG_ON(btrfs_super_bytenr(super
) != old_bytenr
);
1771 btrfs_set_super_bytenr(super
, BTRFS_SUPER_INFO_OFFSET
);
1773 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
1774 ret
= pwrite(fd
, buf
->data
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
1775 if (ret
!= sectorsize
)
1782 memset(buf
->data
, 0, sectorsize
);
1783 for (bytenr
= 0; bytenr
< BTRFS_SUPER_INFO_OFFSET
; ) {
1784 len
= BTRFS_SUPER_INFO_OFFSET
- bytenr
;
1785 if (len
> sectorsize
)
1787 ret
= pwrite(fd
, buf
->data
, len
, bytenr
);
1789 fprintf(stderr
, "unable to zero fill device\n");
1803 static int prepare_system_chunk_sb(struct btrfs_super_block
*super
)
1805 struct btrfs_chunk
*chunk
;
1806 struct btrfs_disk_key
*key
;
1807 u32 sectorsize
= btrfs_super_sectorsize(super
);
1809 key
= (struct btrfs_disk_key
*)(super
->sys_chunk_array
);
1810 chunk
= (struct btrfs_chunk
*)(super
->sys_chunk_array
+
1811 sizeof(struct btrfs_disk_key
));
1813 btrfs_set_disk_key_objectid(key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1814 btrfs_set_disk_key_type(key
, BTRFS_CHUNK_ITEM_KEY
);
1815 btrfs_set_disk_key_offset(key
, 0);
1817 btrfs_set_stack_chunk_length(chunk
, btrfs_super_total_bytes(super
));
1818 btrfs_set_stack_chunk_owner(chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
1819 btrfs_set_stack_chunk_stripe_len(chunk
, BTRFS_STRIPE_LEN
);
1820 btrfs_set_stack_chunk_type(chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
1821 btrfs_set_stack_chunk_io_align(chunk
, sectorsize
);
1822 btrfs_set_stack_chunk_io_width(chunk
, sectorsize
);
1823 btrfs_set_stack_chunk_sector_size(chunk
, sectorsize
);
1824 btrfs_set_stack_chunk_num_stripes(chunk
, 1);
1825 btrfs_set_stack_chunk_sub_stripes(chunk
, 0);
1826 chunk
->stripe
.devid
= super
->dev_item
.devid
;
1827 btrfs_set_stack_stripe_offset(&chunk
->stripe
, 0);
1828 memcpy(chunk
->stripe
.dev_uuid
, super
->dev_item
.uuid
, BTRFS_UUID_SIZE
);
1829 btrfs_set_super_sys_array_size(super
, sizeof(*key
) + sizeof(*chunk
));
1833 static int prepare_system_chunk(int fd
, u64 sb_bytenr
)
1836 struct extent_buffer
*buf
;
1837 struct btrfs_super_block
*super
;
1839 BUG_ON(BTRFS_SUPER_INFO_SIZE
< sizeof(*super
));
1840 buf
= malloc(sizeof(*buf
) + BTRFS_SUPER_INFO_SIZE
);
1844 buf
->len
= BTRFS_SUPER_INFO_SIZE
;
1845 ret
= pread(fd
, buf
->data
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
1846 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1849 super
= (struct btrfs_super_block
*)buf
->data
;
1850 BUG_ON(btrfs_super_bytenr(super
) != sb_bytenr
);
1851 BUG_ON(btrfs_super_num_devices(super
) != 1);
1853 ret
= prepare_system_chunk_sb(super
);
1857 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
1858 ret
= pwrite(fd
, buf
->data
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
1859 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1870 static int relocate_one_reference(struct btrfs_trans_handle
*trans
,
1871 struct btrfs_root
*root
,
1872 u64 extent_start
, u64 extent_size
,
1873 struct btrfs_key
*extent_key
,
1874 struct extent_io_tree
*reloc_tree
)
1876 struct extent_buffer
*leaf
;
1877 struct btrfs_file_extent_item
*fi
;
1878 struct btrfs_key key
;
1879 struct btrfs_path path
;
1880 struct btrfs_inode_item inode
;
1881 struct blk_iterate_data data
;
1888 u32 sectorsize
= root
->sectorsize
;
1894 btrfs_init_path(&path
);
1895 ret
= btrfs_search_slot(trans
, root
, extent_key
, &path
, -1, 1);
1899 leaf
= path
.nodes
[0];
1900 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
1901 struct btrfs_file_extent_item
);
1902 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) > 0);
1903 if (extent_start
!= btrfs_file_extent_disk_bytenr(leaf
, fi
) ||
1904 extent_size
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1909 bytenr
= extent_start
+ btrfs_file_extent_offset(leaf
, fi
);
1910 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
1912 ret
= btrfs_del_item(trans
, root
, &path
);
1916 ret
= btrfs_free_extent(trans
, root
, extent_start
, extent_size
, 0,
1917 root
->root_key
.objectid
,
1918 extent_key
->objectid
, extent_key
->offset
);
1922 btrfs_release_path(&path
);
1924 key
.objectid
= extent_key
->objectid
;
1926 key
.type
= BTRFS_INODE_ITEM_KEY
;
1927 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 0);
1931 leaf
= path
.nodes
[0];
1932 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1933 read_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
1934 btrfs_release_path(&path
);
1936 BUG_ON(num_bytes
& (sectorsize
- 1));
1937 nbytes
= btrfs_stack_inode_nbytes(&inode
) - num_bytes
;
1938 btrfs_set_stack_inode_nbytes(&inode
, nbytes
);
1939 datacsum
= !(btrfs_stack_inode_flags(&inode
) & BTRFS_INODE_NODATASUM
);
1941 init_blk_iterate_data(&data
, trans
, root
, &inode
, extent_key
->objectid
,
1943 data
.first_block
= extent_key
->offset
;
1945 cur_offset
= extent_key
->offset
;
1946 while (num_bytes
> 0) {
1947 sector_end
= bytenr
+ sectorsize
- 1;
1948 if (test_range_bit(reloc_tree
, bytenr
, sector_end
,
1949 EXTENT_LOCKED
, 1)) {
1950 ret
= get_state_private(reloc_tree
, bytenr
, &new_pos
);
1953 ret
= custom_alloc_extent(root
, sectorsize
, 0, &key
, 0);
1956 new_pos
= key
.objectid
;
1958 if (cur_offset
== extent_key
->offset
) {
1959 fd
= root
->fs_info
->fs_devices
->latest_bdev
;
1960 readahead(fd
, bytenr
, num_bytes
);
1962 ret
= copy_disk_extent(root
, new_pos
, bytenr
,
1966 ret
= set_extent_bits(reloc_tree
, bytenr
, sector_end
,
1967 EXTENT_LOCKED
, GFP_NOFS
);
1969 ret
= set_state_private(reloc_tree
, bytenr
, new_pos
);
1973 ret
= block_iterate_proc(new_pos
/ sectorsize
,
1974 cur_offset
/ sectorsize
, &data
);
1978 cur_offset
+= sectorsize
;
1979 bytenr
+= sectorsize
;
1980 num_bytes
-= sectorsize
;
1983 if (data
.num_blocks
> 0) {
1984 ret
= record_file_blocks(&data
, data
.first_block
,
1985 data
.disk_block
, data
.num_blocks
);
1990 key
.objectid
= extent_key
->objectid
;
1992 key
.type
= BTRFS_INODE_ITEM_KEY
;
1993 ret
= btrfs_lookup_inode(trans
, root
, &path
, &key
, 1);
1997 leaf
= path
.nodes
[0];
1998 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
1999 write_extent_buffer(leaf
, &inode
, ptr
, sizeof(inode
));
2000 btrfs_mark_buffer_dirty(leaf
);
2001 btrfs_release_path(&path
);
2004 btrfs_release_path(&path
);
2008 static int relocate_extents_range(struct btrfs_root
*fs_root
,
2009 struct btrfs_root
*image_root
,
2010 u64 start_byte
, u64 end_byte
)
2012 struct btrfs_fs_info
*info
= fs_root
->fs_info
;
2013 struct btrfs_root
*extent_root
= info
->extent_root
;
2014 struct btrfs_root
*cur_root
= NULL
;
2015 struct btrfs_trans_handle
*trans
;
2016 struct btrfs_extent_data_ref
*dref
;
2017 struct btrfs_extent_inline_ref
*iref
;
2018 struct btrfs_extent_item
*ei
;
2019 struct extent_buffer
*leaf
;
2020 struct btrfs_key key
;
2021 struct btrfs_key extent_key
;
2022 struct btrfs_path path
;
2023 struct extent_io_tree reloc_tree
;
2033 btrfs_init_path(&path
);
2034 extent_io_tree_init(&reloc_tree
);
2036 key
.objectid
= start_byte
;
2038 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2039 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
2043 ret
= btrfs_previous_item(extent_root
, &path
, 0,
2044 BTRFS_EXTENT_ITEM_KEY
);
2048 leaf
= path
.nodes
[0];
2049 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2050 if (key
.objectid
+ key
.offset
> start_byte
)
2051 start_byte
= key
.objectid
;
2054 btrfs_release_path(&path
);
2056 cur_root
= (pass
% 2 == 0) ? image_root
: fs_root
;
2059 trans
= btrfs_start_transaction(cur_root
, 1);
2062 cur_byte
= start_byte
;
2064 key
.objectid
= cur_byte
;
2066 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2067 ret
= btrfs_search_slot(trans
, extent_root
,
2072 leaf
= path
.nodes
[0];
2073 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2074 ret
= btrfs_next_leaf(extent_root
, &path
);
2079 leaf
= path
.nodes
[0];
2082 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2083 if (key
.objectid
< cur_byte
||
2084 key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
2088 if (key
.objectid
>= end_byte
)
2093 cur_byte
= key
.objectid
;
2094 num_bytes
= key
.offset
;
2095 ei
= btrfs_item_ptr(leaf
, path
.slots
[0],
2096 struct btrfs_extent_item
);
2097 BUG_ON(!(btrfs_extent_flags(leaf
, ei
) &
2098 BTRFS_EXTENT_FLAG_DATA
));
2100 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
2101 end
= ptr
+ btrfs_item_size_nr(leaf
, path
.slots
[0]);
2103 ptr
+= sizeof(struct btrfs_extent_item
);
2106 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
2107 key
.type
= btrfs_extent_inline_ref_type(leaf
, iref
);
2108 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
);
2109 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
2110 ref_root
= btrfs_extent_data_ref_root(leaf
, dref
);
2111 extent_key
.objectid
=
2112 btrfs_extent_data_ref_objectid(leaf
, dref
);
2114 btrfs_extent_data_ref_offset(leaf
, dref
);
2115 extent_key
.type
= BTRFS_EXTENT_DATA_KEY
;
2116 BUG_ON(btrfs_extent_data_ref_count(leaf
, dref
) != 1);
2118 if (ref_root
== cur_root
->root_key
.objectid
)
2121 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
2129 ret
= relocate_one_reference(trans
, cur_root
, cur_byte
,
2130 num_bytes
, &extent_key
,
2135 cur_byte
+= num_bytes
;
2136 btrfs_release_path(&path
);
2138 if (trans
->blocks_used
>= 4096) {
2139 ret
= btrfs_commit_transaction(trans
, cur_root
);
2141 trans
= btrfs_start_transaction(cur_root
, 1);
2145 btrfs_release_path(&path
);
2147 ret
= btrfs_commit_transaction(trans
, cur_root
);
2150 if (num_extents
> 0 && pass
++ < 16)
2153 ret
= (num_extents
> 0) ? -1 : 0;
2155 btrfs_release_path(&path
);
2156 extent_io_tree_cleanup(&reloc_tree
);
2161 * relocate data in system chunk
2163 static int cleanup_sys_chunk(struct btrfs_root
*fs_root
,
2164 struct btrfs_root
*image_root
)
2166 struct btrfs_block_group_cache
*cache
;
2172 cache
= btrfs_lookup_block_group(fs_root
->fs_info
, offset
);
2176 end_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
2177 if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2178 ret
= relocate_extents_range(fs_root
, image_root
,
2179 cache
->key
.objectid
,
2186 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
2187 offset
= btrfs_sb_offset(i
);
2188 offset
&= ~((u64
)BTRFS_STRIPE_LEN
- 1);
2190 ret
= relocate_extents_range(fs_root
, image_root
,
2191 offset
, offset
+ BTRFS_STRIPE_LEN
);
2200 static int fixup_chunk_mapping(struct btrfs_root
*root
)
2202 struct btrfs_trans_handle
*trans
;
2203 struct btrfs_fs_info
*info
= root
->fs_info
;
2204 struct btrfs_root
*chunk_root
= info
->chunk_root
;
2205 struct extent_buffer
*leaf
;
2206 struct btrfs_key key
;
2207 struct btrfs_path path
;
2208 struct btrfs_chunk chunk
;
2214 btrfs_init_path(&path
);
2216 trans
= btrfs_start_transaction(root
, 1);
2220 * recow the whole chunk tree. this will move all chunk tree blocks
2221 * into system block group.
2223 memset(&key
, 0, sizeof(key
));
2225 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2229 ret
= btrfs_next_leaf(chunk_root
, &path
);
2235 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2236 btrfs_release_path(&path
);
2238 btrfs_release_path(&path
);
2240 /* fixup the system chunk array in super block */
2241 btrfs_set_super_sys_array_size(info
->super_copy
, 0);
2243 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2245 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2247 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 0);
2252 leaf
= path
.nodes
[0];
2253 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2254 ret
= btrfs_next_leaf(chunk_root
, &path
);
2259 leaf
= path
.nodes
[0];
2261 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2262 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
)
2265 ptr
= btrfs_item_ptr_offset(leaf
, path
.slots
[0]);
2266 size
= btrfs_item_size_nr(leaf
, path
.slots
[0]);
2267 BUG_ON(size
!= sizeof(chunk
));
2268 read_extent_buffer(leaf
, &chunk
, ptr
, size
);
2269 type
= btrfs_stack_chunk_type(&chunk
);
2271 if (!(type
& BTRFS_BLOCK_GROUP_SYSTEM
))
2274 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
2282 ret
= btrfs_commit_transaction(trans
, root
);
2285 btrfs_release_path(&path
);
2289 static int do_convert(const char *devname
, int datacsum
, int packing
, int noxattr
,
2290 u32 nodesize
, int copylabel
, const char *fslabel
, int progress
,
2293 int i
, ret
, blocks_per_node
;
2300 ext2_filsys ext2_fs
;
2301 struct btrfs_root
*root
;
2302 struct btrfs_root
*image_root
;
2303 struct task_ctx ctx
;
2304 char features_buf
[64];
2305 struct btrfs_mkfs_config mkfs_cfg
;
2307 ret
= open_ext2fs(devname
, &ext2_fs
);
2309 fprintf(stderr
, "unable to open the Ext2fs\n");
2312 blocksize
= ext2_fs
->blocksize
;
2313 total_bytes
= (u64
)ext2_fs
->super
->s_blocks_count
* blocksize
;
2314 if (blocksize
< 4096) {
2315 fprintf(stderr
, "block size is too small\n");
2318 if (!(ext2_fs
->super
->s_feature_incompat
&
2319 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
2320 fprintf(stderr
, "filetype feature is missing\n");
2323 if (btrfs_check_nodesize(nodesize
, blocksize
, features
))
2325 blocks_per_node
= nodesize
/ blocksize
;
2326 ret
= -blocks_per_node
;
2327 for (i
= 0; i
< 7; i
++) {
2328 if (nodesize
== blocksize
)
2329 ret
= ext2_alloc_block(ext2_fs
, 0, blocks
+ i
);
2331 ret
= ext2_alloc_block_range(ext2_fs
,
2332 ret
+ blocks_per_node
, blocks_per_node
,
2335 fprintf(stderr
, "not enough free space\n");
2338 blocks
[i
] *= blocksize
;
2340 super_bytenr
= blocks
[0];
2341 fd
= open(devname
, O_RDWR
);
2343 fprintf(stderr
, "unable to open %s\n", devname
);
2346 btrfs_parse_features_to_string(features_buf
, features
);
2347 if (features
== BTRFS_MKFS_DEFAULT_FEATURES
)
2348 strcat(features_buf
, " (default)");
2350 printf("create btrfs filesystem:\n");
2351 printf("\tblocksize: %u\n", blocksize
);
2352 printf("\tnodesize: %u\n", nodesize
);
2353 printf("\tfeatures: %s\n", features_buf
);
2355 mkfs_cfg
.label
= ext2_fs
->super
->s_volume_name
;
2356 mkfs_cfg
.fs_uuid
= NULL
;
2357 memcpy(mkfs_cfg
.blocks
, blocks
, sizeof(blocks
));
2358 mkfs_cfg
.num_bytes
= total_bytes
;
2359 mkfs_cfg
.nodesize
= nodesize
;
2360 mkfs_cfg
.sectorsize
= blocksize
;
2361 mkfs_cfg
.stripesize
= blocksize
;
2362 mkfs_cfg
.features
= features
;
2364 ret
= make_btrfs(fd
, &mkfs_cfg
);
2366 fprintf(stderr
, "unable to create initial ctree: %s\n",
2370 /* create a system chunk that maps the whole device */
2371 ret
= prepare_system_chunk(fd
, super_bytenr
);
2373 fprintf(stderr
, "unable to update system chunk\n");
2376 root
= open_ctree_fd(fd
, devname
, super_bytenr
, OPEN_CTREE_WRITES
);
2378 fprintf(stderr
, "unable to open ctree\n");
2381 ret
= cache_free_extents(root
, ext2_fs
);
2383 fprintf(stderr
, "error during cache_free_extents %d\n", ret
);
2386 root
->fs_info
->extent_ops
= &extent_ops
;
2387 /* recover block allocation bitmap */
2388 for (i
= 0; i
< 7; i
++) {
2389 blocks
[i
] /= blocksize
;
2390 if (nodesize
== blocksize
)
2391 ext2_free_block(ext2_fs
, blocks
[i
]);
2393 ext2_free_block_range(ext2_fs
, blocks
[i
],
2396 ret
= init_btrfs(root
);
2398 fprintf(stderr
, "unable to setup the root tree\n");
2401 printf("creating btrfs metadata.\n");
2402 ctx
.max_copy_inodes
= (ext2_fs
->super
->s_inodes_count
2403 - ext2_fs
->super
->s_free_inodes_count
);
2404 ctx
.cur_copy_inodes
= 0;
2407 ctx
.info
= task_init(print_copied_inodes
, after_copied_inodes
, &ctx
);
2408 task_start(ctx
.info
);
2410 ret
= copy_inodes(root
, ext2_fs
, datacsum
, packing
, noxattr
, &ctx
);
2412 fprintf(stderr
, "error during copy_inodes %d\n", ret
);
2416 task_stop(ctx
.info
);
2417 task_deinit(ctx
.info
);
2419 printf("creating ext2fs image file.\n");
2420 image_root
= link_subvol(root
, "ext2_saved", CONV_IMAGE_SUBVOL_OBJECTID
);
2422 fprintf(stderr
, "unable to create subvol\n");
2425 ret
= create_ext2_image(image_root
, ext2_fs
, "image", datacsum
);
2427 fprintf(stderr
, "error during create_ext2_image %d\n", ret
);
2430 memset(root
->fs_info
->super_copy
->label
, 0, BTRFS_LABEL_SIZE
);
2431 if (copylabel
== 1) {
2432 strncpy(root
->fs_info
->super_copy
->label
,
2433 ext2_fs
->super
->s_volume_name
, 16);
2434 fprintf(stderr
, "copy label '%s'\n",
2435 root
->fs_info
->super_copy
->label
);
2436 } else if (copylabel
== -1) {
2437 strcpy(root
->fs_info
->super_copy
->label
, fslabel
);
2438 fprintf(stderr
, "set label to '%s'\n", fslabel
);
2441 printf("cleaning up system chunk.\n");
2442 ret
= cleanup_sys_chunk(root
, image_root
);
2444 fprintf(stderr
, "error during cleanup_sys_chunk %d\n", ret
);
2447 ret
= close_ctree(root
);
2449 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2452 close_ext2fs(ext2_fs
);
2455 * If this step succeed, we get a mountable btrfs. Otherwise
2456 * the ext2fs is left unchanged.
2458 ret
= migrate_super_block(fd
, super_bytenr
, blocksize
);
2460 fprintf(stderr
, "unable to migrate super block\n");
2465 root
= open_ctree_fd(fd
, devname
, 0, OPEN_CTREE_WRITES
);
2467 fprintf(stderr
, "unable to open ctree\n");
2470 /* move chunk tree into system chunk. */
2471 ret
= fixup_chunk_mapping(root
);
2473 fprintf(stderr
, "error during fixup_chunk_tree\n");
2476 ret
= close_ctree(root
);
2479 printf("conversion complete.\n");
2486 "WARNING: an error occured during chunk mapping fixup, filesystem mountable but not finalized\n");
2488 fprintf(stderr
, "conversion aborted\n");
2492 static int may_rollback(struct btrfs_root
*root
)
2494 struct btrfs_fs_info
*info
= root
->fs_info
;
2495 struct btrfs_multi_bio
*multi
= NULL
;
2503 if (btrfs_super_num_devices(info
->super_copy
) != 1)
2506 bytenr
= BTRFS_SUPER_INFO_OFFSET
;
2507 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2510 ret
= btrfs_map_block(&info
->mapping_tree
, WRITE
, bytenr
,
2511 &length
, &multi
, 0, NULL
);
2513 if (ret
== -ENOENT
) {
2514 /* removed block group at the tail */
2515 if (length
== (u64
)-1)
2518 /* removed block group in the middle */
2524 num_stripes
= multi
->num_stripes
;
2525 physical
= multi
->stripes
[0].physical
;
2528 if (num_stripes
!= 1 || physical
!= bytenr
)
2532 if (bytenr
>= total_bytes
)
2540 static int do_rollback(const char *devname
)
2545 struct btrfs_root
*root
;
2546 struct btrfs_root
*image_root
;
2547 struct btrfs_root
*chunk_root
;
2548 struct btrfs_dir_item
*dir
;
2549 struct btrfs_inode_item
*inode
;
2550 struct btrfs_file_extent_item
*fi
;
2551 struct btrfs_trans_handle
*trans
;
2552 struct extent_buffer
*leaf
;
2553 struct btrfs_block_group_cache
*cache1
;
2554 struct btrfs_block_group_cache
*cache2
;
2555 struct btrfs_key key
;
2556 struct btrfs_path path
;
2557 struct extent_io_tree io_tree
;
2572 extent_io_tree_init(&io_tree
);
2574 fd
= open(devname
, O_RDWR
);
2576 fprintf(stderr
, "unable to open %s\n", devname
);
2579 root
= open_ctree_fd(fd
, devname
, 0, OPEN_CTREE_WRITES
);
2581 fprintf(stderr
, "unable to open ctree\n");
2584 ret
= may_rollback(root
);
2586 fprintf(stderr
, "unable to do rollback\n");
2590 sectorsize
= root
->sectorsize
;
2591 buf
= malloc(sectorsize
);
2593 fprintf(stderr
, "unable to allocate memory\n");
2597 btrfs_init_path(&path
);
2599 key
.objectid
= CONV_IMAGE_SUBVOL_OBJECTID
;
2600 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2601 key
.offset
= (u64
)-1;
2602 image_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2603 if (!image_root
|| IS_ERR(image_root
)) {
2604 fprintf(stderr
, "unable to open subvol %llu\n",
2605 (unsigned long long)key
.objectid
);
2610 root_dir
= btrfs_root_dirid(&root
->root_item
);
2611 dir
= btrfs_lookup_dir_item(NULL
, image_root
, &path
,
2612 root_dir
, name
, strlen(name
), 0);
2613 if (!dir
|| IS_ERR(dir
)) {
2614 fprintf(stderr
, "unable to find file %s\n", name
);
2617 leaf
= path
.nodes
[0];
2618 btrfs_dir_item_key_to_cpu(leaf
, dir
, &key
);
2619 btrfs_release_path(&path
);
2621 objectid
= key
.objectid
;
2623 ret
= btrfs_lookup_inode(NULL
, image_root
, &path
, &key
, 0);
2625 fprintf(stderr
, "unable to find inode item\n");
2628 leaf
= path
.nodes
[0];
2629 inode
= btrfs_item_ptr(leaf
, path
.slots
[0], struct btrfs_inode_item
);
2630 total_bytes
= btrfs_inode_size(leaf
, inode
);
2631 btrfs_release_path(&path
);
2633 key
.objectid
= objectid
;
2635 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2636 ret
= btrfs_search_slot(NULL
, image_root
, &key
, &path
, 0, 0);
2638 fprintf(stderr
, "unable to find first file extent\n");
2639 btrfs_release_path(&path
);
2643 /* build mapping tree for the relocated blocks */
2644 for (offset
= 0; offset
< total_bytes
; ) {
2645 leaf
= path
.nodes
[0];
2646 if (path
.slots
[0] >= btrfs_header_nritems(leaf
)) {
2647 ret
= btrfs_next_leaf(root
, &path
);
2653 btrfs_item_key_to_cpu(leaf
, &key
, path
.slots
[0]);
2654 if (key
.objectid
!= objectid
|| key
.offset
!= offset
||
2655 btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
2658 fi
= btrfs_item_ptr(leaf
, path
.slots
[0],
2659 struct btrfs_file_extent_item
);
2660 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2662 if (btrfs_file_extent_compression(leaf
, fi
) ||
2663 btrfs_file_extent_encryption(leaf
, fi
) ||
2664 btrfs_file_extent_other_encoding(leaf
, fi
))
2667 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2668 /* skip holes and direct mapped extents */
2669 if (bytenr
== 0 || bytenr
== offset
)
2672 bytenr
+= btrfs_file_extent_offset(leaf
, fi
);
2673 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
2675 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2676 cache2
= btrfs_lookup_block_group(root
->fs_info
,
2677 offset
+ num_bytes
- 1);
2678 if (!cache1
|| cache1
!= cache2
||
2679 (!(cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
2680 !intersect_with_sb(offset
, num_bytes
)))
2683 set_extent_bits(&io_tree
, offset
, offset
+ num_bytes
- 1,
2684 EXTENT_LOCKED
, GFP_NOFS
);
2685 set_state_private(&io_tree
, offset
, bytenr
);
2687 offset
+= btrfs_file_extent_num_bytes(leaf
, fi
);
2690 btrfs_release_path(&path
);
2692 if (offset
< total_bytes
) {
2693 fprintf(stderr
, "unable to build extent mapping\n");
2697 first_free
= BTRFS_SUPER_INFO_OFFSET
+ 2 * sectorsize
- 1;
2698 first_free
&= ~((u64
)sectorsize
- 1);
2699 /* backup for extent #0 should exist */
2700 if(!test_range_bit(&io_tree
, 0, first_free
- 1, EXTENT_LOCKED
, 1)) {
2701 fprintf(stderr
, "no backup for the first extent\n");
2704 /* force no allocation from system block group */
2705 root
->fs_info
->system_allocs
= -1;
2706 trans
= btrfs_start_transaction(root
, 1);
2709 * recow the whole chunk tree, this will remove all chunk tree blocks
2710 * from system block group
2712 chunk_root
= root
->fs_info
->chunk_root
;
2713 memset(&key
, 0, sizeof(key
));
2715 ret
= btrfs_search_slot(trans
, chunk_root
, &key
, &path
, 0, 1);
2719 ret
= btrfs_next_leaf(chunk_root
, &path
);
2723 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
2724 btrfs_release_path(&path
);
2726 btrfs_release_path(&path
);
2731 cache1
= btrfs_lookup_block_group(root
->fs_info
, offset
);
2735 if (cache1
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
2736 num_bytes
+= btrfs_block_group_used(&cache1
->item
);
2738 offset
= cache1
->key
.objectid
+ cache1
->key
.offset
;
2740 /* only extent #0 left in system block group? */
2741 if (num_bytes
> first_free
) {
2742 fprintf(stderr
, "unable to empty system block group\n");
2745 /* create a system chunk that maps the whole device */
2746 ret
= prepare_system_chunk_sb(root
->fs_info
->super_copy
);
2748 fprintf(stderr
, "unable to update system chunk\n");
2752 ret
= btrfs_commit_transaction(trans
, root
);
2755 ret
= close_ctree(root
);
2757 fprintf(stderr
, "error during close_ctree %d\n", ret
);
2761 /* zero btrfs super block mirrors */
2762 memset(buf
, 0, sectorsize
);
2763 for (i
= 1 ; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
2764 bytenr
= btrfs_sb_offset(i
);
2765 if (bytenr
>= total_bytes
)
2767 ret
= pwrite(fd
, buf
, sectorsize
, bytenr
);
2768 if (ret
!= sectorsize
) {
2770 "error during zeroing supreblock %d: %d\n",
2776 sb_bytenr
= (u64
)-1;
2777 /* copy all relocated blocks back */
2779 ret
= find_first_extent_bit(&io_tree
, 0, &start
, &end
,
2784 ret
= get_state_private(&io_tree
, start
, &bytenr
);
2787 clear_extent_bits(&io_tree
, start
, end
, EXTENT_LOCKED
,
2790 while (start
<= end
) {
2791 if (start
== BTRFS_SUPER_INFO_OFFSET
) {
2795 ret
= pread(fd
, buf
, sectorsize
, bytenr
);
2797 fprintf(stderr
, "error during pread %d\n", ret
);
2800 BUG_ON(ret
!= sectorsize
);
2801 ret
= pwrite(fd
, buf
, sectorsize
, start
);
2803 fprintf(stderr
, "error during pwrite %d\n", ret
);
2806 BUG_ON(ret
!= sectorsize
);
2808 start
+= sectorsize
;
2809 bytenr
+= sectorsize
;
2815 fprintf(stderr
, "error during fsync %d\n", ret
);
2819 * finally, overwrite btrfs super block.
2821 ret
= pread(fd
, buf
, sectorsize
, sb_bytenr
);
2823 fprintf(stderr
, "error during pread %d\n", ret
);
2826 BUG_ON(ret
!= sectorsize
);
2827 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
2829 fprintf(stderr
, "error during pwrite %d\n", ret
);
2832 BUG_ON(ret
!= sectorsize
);
2835 fprintf(stderr
, "error during fsync %d\n", ret
);
2841 extent_io_tree_cleanup(&io_tree
);
2842 printf("rollback complete.\n");
2849 fprintf(stderr
, "rollback aborted.\n");
2853 static void print_usage(void)
2855 printf("usage: btrfs-convert [options] device\n");
2856 printf("options:\n");
2857 printf("\t-d|--no-datasum disable data checksum, sets NODATASUM\n");
2858 printf("\t-i|--no-xattr ignore xattrs and ACLs\n");
2859 printf("\t-n|--no-inline disable inlining of small files to metadata\n");
2860 printf("\t-N|--nodesize SIZE set filesystem metadata nodesize\n");
2861 printf("\t-r|--rollback roll back to ext2fs\n");
2862 printf("\t-l|--label LABEL set filesystem label\n");
2863 printf("\t-L|--copy-label use label from converted filesystem\n");
2864 printf("\t-p|--progress show converting progress (default)\n");
2865 printf("\t-O|--features LIST comma separated list of filesystem features\n");
2866 printf("\t--no-progress show only overview, not the detailed progress\n");
2869 int main(int argc
, char *argv
[])
2875 u32 nodesize
= max_t(u32
, sysconf(_SC_PAGESIZE
),
2876 BTRFS_MKFS_DEFAULT_NODE_SIZE
);
2879 int usage_error
= 0;
2882 char fslabel
[BTRFS_LABEL_SIZE
];
2883 u64 features
= BTRFS_MKFS_DEFAULT_FEATURES
;
2886 enum { GETOPT_VAL_NO_PROGRESS
= 256 };
2887 static const struct option long_options
[] = {
2888 { "no-progress", no_argument
, NULL
,
2889 GETOPT_VAL_NO_PROGRESS
},
2890 { "no-datasum", no_argument
, NULL
, 'd' },
2891 { "no-inline", no_argument
, NULL
, 'n' },
2892 { "no-xattr", no_argument
, NULL
, 'i' },
2893 { "rollback", no_argument
, NULL
, 'r' },
2894 { "features", required_argument
, NULL
, 'O' },
2895 { "progress", no_argument
, NULL
, 'p' },
2896 { "label", required_argument
, NULL
, 'l' },
2897 { "copy-label", no_argument
, NULL
, 'L' },
2898 { "nodesize", required_argument
, NULL
, 'N' },
2899 { "help", no_argument
, NULL
, GETOPT_VAL_HELP
},
2900 { NULL
, 0, NULL
, 0 }
2902 int c
= getopt_long(argc
, argv
, "dinN:rl:LpO:", long_options
, NULL
);
2917 nodesize
= parse_size(optarg
);
2924 if (strlen(optarg
) >= BTRFS_LABEL_SIZE
) {
2926 "WARNING: label too long, trimmed to %d bytes\n",
2927 BTRFS_LABEL_SIZE
- 1);
2929 strncpy(fslabel
, optarg
, BTRFS_LABEL_SIZE
- 1);
2930 fslabel
[BTRFS_LABEL_SIZE
- 1] = 0;
2939 char *orig
= strdup(optarg
);
2942 tmp
= btrfs_parse_fs_features(tmp
, &features
);
2945 "Unrecognized filesystem feature '%s'\n",
2951 if (features
& BTRFS_FEATURE_LIST_ALL
) {
2952 btrfs_list_all_fs_features(
2953 ~BTRFS_CONVERT_ALLOWED_FEATURES
);
2956 if (features
& ~BTRFS_CONVERT_ALLOWED_FEATURES
) {
2959 btrfs_parse_features_to_string(buf
,
2960 features
& ~BTRFS_CONVERT_ALLOWED_FEATURES
);
2962 "ERROR: features not allowed for convert: %s\n",
2969 case GETOPT_VAL_NO_PROGRESS
:
2972 case GETOPT_VAL_HELP
:
2975 return c
!= GETOPT_VAL_HELP
;
2978 argc
= argc
- optind
;
2980 if (check_argc_exact(argc
, 1)) {
2985 if (rollback
&& (!datacsum
|| noxattr
|| !packing
)) {
2987 "Usage error: -d, -i, -n options do not apply to rollback\n");
2996 file
= argv
[optind
];
2997 ret
= check_mounted(file
);
2999 fprintf(stderr
, "Could not check mount status: %s\n",
3003 fprintf(stderr
, "%s is mounted\n", file
);
3008 ret
= do_rollback(file
);
3010 ret
= do_convert(file
, datacsum
, packing
, noxattr
, nodesize
,
3011 copylabel
, fslabel
, progress
, features
);