2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
19 #include "kerncompat.h"
20 #include <linux/limits.h>
23 #include "transaction.h"
25 #include "convert/common.h"
26 #include "convert/source-ext2.h"
29 * Open Ext2fs in readonly mode, read block allocation bitmap and
30 * inode bitmap into memory.
32 static int ext2_open_fs(struct btrfs_convert_context *cctx, const char *name)
38 int open_flag = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
40 ret = ext2fs_open(name, open_flag, 0, 0, unix_io_manager, &ext2_fs);
42 if (ret != EXT2_ET_BAD_MAGIC)
43 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
47 * We need to know exactly the used space, some RO compat flags like
48 * BIGALLOC will affect how used space is present.
49 * So we need manuall check any unsupported RO compat flags
51 ro_feature = ext2_fs->super->s_feature_ro_compat;
52 if (ro_feature & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
54 "unsupported RO features detected: %x, abort convert to avoid possible corruption",
55 ro_feature & ~EXT2_LIB_FEATURE_COMPAT_SUPP);
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",
71 * search each block group for a free inode. this set up
72 * uninit block/inode bitmaps appropriately.
75 while (ino <= ext2_fs->super->s_inodes_count) {
77 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
78 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
81 if (!(ext2_fs->super->s_feature_incompat &
82 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
83 error("filetype feature is missing");
87 cctx->fs_data = ext2_fs;
88 cctx->blocksize = ext2_fs->blocksize;
89 cctx->block_count = ext2_fs->super->s_blocks_count;
90 cctx->total_bytes = ext2_fs->blocksize * ext2_fs->super->s_blocks_count;
91 cctx->volume_name = strndup(ext2_fs->super->s_volume_name, 16);
92 cctx->first_data_block = ext2_fs->super->s_first_data_block;
93 cctx->inodes_count = ext2_fs->super->s_inodes_count;
94 cctx->free_inodes_count = ext2_fs->super->s_free_inodes_count;
97 ext2fs_close(ext2_fs);
102 static int __ext2_add_one_block(ext2_filsys fs, char *bitmap,
103 unsigned long group_nr, struct cache_tree *used)
105 unsigned long offset;
109 offset = fs->super->s_first_data_block;
110 offset /= EXT2FS_CLUSTER_RATIO(fs);
111 offset += group_nr * EXT2_CLUSTERS_PER_GROUP(fs->super);
112 for (i = 0; i < EXT2_CLUSTERS_PER_GROUP(fs->super); i++) {
113 if ((i + offset) >= ext2fs_blocks_count(fs->super))
116 if (ext2fs_test_bit(i, bitmap)) {
119 start = (i + offset) * EXT2FS_CLUSTER_RATIO(fs);
120 start *= fs->blocksize;
121 ret = add_merge_cache_extent(used, start,
131 * Read all used ext2 space into cctx->used cache tree
133 static int ext2_read_used_space(struct btrfs_convert_context *cctx)
135 ext2_filsys fs = (ext2_filsys)cctx->fs_data;
136 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
137 struct cache_tree *used_tree = &cctx->used_space;
138 char *block_bitmap = NULL;
143 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
145 error("EXT2_CLUSTERS_PER_GROUP too small: %llu",
146 (unsigned long long)(EXT2_CLUSTERS_PER_GROUP(fs->super)));
150 block_bitmap = malloc(block_nbytes);
154 for (i = 0; i < fs->group_desc_count; i++) {
155 ret = ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr,
156 block_nbytes * 8, block_bitmap);
158 error("fail to get bitmap from ext2, %s",
163 ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree);
165 error("fail to build used space tree, %s",
169 blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super);
176 static void ext2_close_fs(struct btrfs_convert_context *cctx)
178 if (cctx->volume_name) {
179 free(cctx->volume_name);
180 cctx->volume_name = NULL;
182 ext2fs_close(cctx->fs_data);
183 ext2fs_free(cctx->fs_data);
186 static u8 ext2_filetype_conversion_table[EXT2_FT_MAX] = {
187 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
188 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
189 [EXT2_FT_DIR] = BTRFS_FT_DIR,
190 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
191 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
192 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
193 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
194 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
197 static int ext2_dir_iterate_proc(ext2_ino_t dir, int entry,
198 struct ext2_dir_entry *dirent,
199 int offset, int blocksize,
200 char *buf,void *priv_data)
205 char dotdot[] = "..";
206 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
209 name_len = dirent->name_len & 0xFF;
211 objectid = dirent->inode + INO_OFFSET;
212 if (!strncmp(dirent->name, dotdot, name_len)) {
214 BUG_ON(idata->parent != 0);
215 idata->parent = objectid;
219 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
222 file_type = dirent->name_len >> 8;
223 BUG_ON(file_type > EXT2_FT_SYMLINK);
225 ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
226 name_len, idata->objectid, objectid,
227 ext2_filetype_conversion_table[file_type],
228 idata->index_cnt, idata->inode);
230 idata->errcode = ret;
238 static int ext2_create_dir_entries(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root, u64 objectid,
240 struct btrfs_inode_item *btrfs_inode,
241 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
245 struct dir_iterate_data data = {
248 .inode = btrfs_inode,
249 .objectid = objectid,
255 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
256 ext2_dir_iterate_proc, &data);
260 if (ret == 0 && data.parent == objectid) {
261 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
262 objectid, objectid, 0);
266 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
270 static int ext2_block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
271 e2_blkcnt_t blockcnt, blk_t ref_block,
272 int ref_offset, void *priv_data)
275 struct blk_iterate_data *idata;
276 idata = (struct blk_iterate_data *)priv_data;
277 ret = block_iterate_proc(*blocknr, blockcnt, idata);
279 idata->errcode = ret;
286 * traverse file's data blocks, record these data blocks as file extents.
288 static int ext2_create_file_extents(struct btrfs_trans_handle *trans,
289 struct btrfs_root *root, u64 objectid,
290 struct btrfs_inode_item *btrfs_inode,
291 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
298 u32 sectorsize = root->fs_info->sectorsize;
299 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
300 struct blk_iterate_data data;
302 init_blk_iterate_data(&data, trans, root, btrfs_inode, objectid,
303 convert_flags & CONVERT_FLAG_DATACSUM);
305 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
306 NULL, ext2_block_iterate_proc, &data);
312 if ((convert_flags & CONVERT_FLAG_INLINE_DATA) && data.first_block == 0
313 && data.num_blocks > 0
314 && inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info)) {
315 u64 num_bytes = data.num_blocks * sectorsize;
316 u64 disk_bytenr = data.disk_block * sectorsize;
319 buffer = malloc(num_bytes);
322 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
325 if (num_bytes > inode_size)
326 num_bytes = inode_size;
327 ret = btrfs_insert_inline_extent(trans, root, objectid,
328 0, buffer, num_bytes);
331 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
332 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
333 } else if (data.num_blocks > 0) {
334 ret = record_file_blocks(&data, data.first_block,
335 data.disk_block, data.num_blocks);
339 data.first_block += data.num_blocks;
340 last_block = (inode_size + sectorsize - 1) / sectorsize;
341 if (last_block > data.first_block) {
342 ret = record_file_blocks(&data, data.first_block, 0,
343 last_block - data.first_block);
349 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
353 static int ext2_create_symlink(struct btrfs_trans_handle *trans,
354 struct btrfs_root *root, u64 objectid,
355 struct btrfs_inode_item *btrfs_inode,
356 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
357 struct ext2_inode *ext2_inode)
361 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
362 if (ext2fs_inode_data_blocks2(ext2_fs, ext2_inode)) {
363 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
364 ret = ext2_create_file_extents(trans, root, objectid,
365 btrfs_inode, ext2_fs, ext2_ino,
366 CONVERT_FLAG_DATACSUM |
367 CONVERT_FLAG_INLINE_DATA);
368 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
372 pathname = (char *)&(ext2_inode->i_block[0]);
373 BUG_ON(pathname[inode_size] != 0);
374 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
375 pathname, inode_size + 1);
376 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
381 * Following xattr/acl related codes are based on codes in
382 * fs/ext3/xattr.c and fs/ext3/acl.c
384 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
385 #define EXT2_XATTR_BFIRST(ptr) \
386 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
387 #define EXT2_XATTR_IHDR(inode) \
388 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
389 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
390 #define EXT2_XATTR_IFIRST(inode) \
391 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
392 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
394 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
397 struct ext2_ext_attr_entry *next;
399 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
400 next = EXT2_EXT_ATTR_NEXT(entry);
401 if ((void *)next >= end)
408 static int ext2_xattr_check_block(const char *buf, size_t size)
411 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
413 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
414 header->h_blocks != 1)
416 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
420 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
423 size_t value_size = entry->e_value_size;
425 if (entry->e_value_block != 0 || value_size > size ||
426 entry->e_value_offs + value_size > size)
431 static int ext2_acl_to_xattr(void *dst, const void *src,
432 size_t dst_size, size_t src_size)
435 const void *end = src + src_size;
436 acl_ea_header *ext_acl = (acl_ea_header *)dst;
437 acl_ea_entry *dst_entry = ext_acl->a_entries;
438 ext2_acl_entry *src_entry;
440 if (src_size < sizeof(ext2_acl_header))
442 if (((ext2_acl_header *)src)->a_version !=
443 cpu_to_le32(EXT2_ACL_VERSION))
445 src += sizeof(ext2_acl_header);
446 count = ext2_acl_count(src_size);
450 BUG_ON(dst_size < acl_ea_size(count));
451 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
452 for (i = 0; i < count; i++, dst_entry++) {
453 src_entry = (ext2_acl_entry *)src;
454 if (src + sizeof(ext2_acl_entry_short) > end)
456 dst_entry->e_tag = src_entry->e_tag;
457 dst_entry->e_perm = src_entry->e_perm;
458 switch (le16_to_cpu(src_entry->e_tag)) {
463 src += sizeof(ext2_acl_entry_short);
464 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
468 src += sizeof(ext2_acl_entry);
471 dst_entry->e_id = src_entry->e_id;
484 static char *xattr_prefix_table[] = {
486 [2] = "system.posix_acl_access",
487 [3] = "system.posix_acl_default",
492 static int ext2_copy_single_xattr(struct btrfs_trans_handle *trans,
493 struct btrfs_root *root, u64 objectid,
494 struct ext2_ext_attr_entry *entry,
495 const void *data, u32 datalen)
500 void *databuf = NULL;
501 char namebuf[XATTR_NAME_MAX + 1];
503 name_index = entry->e_name_index;
504 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
505 xattr_prefix_table[name_index] == NULL)
507 name_len = strlen(xattr_prefix_table[name_index]) +
509 if (name_len >= sizeof(namebuf))
512 if (name_index == 2 || name_index == 3) {
513 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
514 databuf = malloc(bufsize);
517 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
523 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
524 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
525 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root->fs_info) -
526 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
527 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
528 objectid - INO_OFFSET, name_len, namebuf);
531 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
532 data, datalen, objectid);
538 static int ext2_copy_extended_attrs(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root, u64 objectid,
540 struct btrfs_inode_item *btrfs_inode,
541 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
547 u32 block_size = ext2_fs->blocksize;
548 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
549 struct ext2_inode_large *ext2_inode;
550 struct ext2_ext_attr_entry *entry;
553 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
555 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
556 ext2_inode = (struct ext2_inode_large *)inode_buf;
558 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
562 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
565 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
571 if (ext2_ino > ext2_fs->super->s_first_ino &&
572 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
573 if (EXT2_GOOD_OLD_INODE_SIZE +
574 ext2_inode->i_extra_isize > inode_size) {
578 if (ext2_inode->i_extra_isize != 0 &&
579 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
580 EXT2_EXT_ATTR_MAGIC) {
586 void *end = (void *)ext2_inode + inode_size;
587 entry = EXT2_XATTR_IFIRST(ext2_inode);
588 total = end - (void *)entry;
589 ret = ext2_xattr_check_names(entry, end);
592 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
593 ret = ext2_xattr_check_entry(entry, total);
596 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
598 datalen = entry->e_value_size;
599 ret = ext2_copy_single_xattr(trans, root, objectid,
600 entry, data, datalen);
603 entry = EXT2_EXT_ATTR_NEXT(entry);
607 if (ext2_inode->i_file_acl == 0)
610 buffer = malloc(block_size);
615 err = ext2fs_read_ext_attr2(ext2_fs, ext2_inode->i_file_acl, buffer);
617 fprintf(stderr, "ext2fs_read_ext_attr2: %s\n",
622 ret = ext2_xattr_check_block(buffer, block_size);
626 entry = EXT2_XATTR_BFIRST(buffer);
627 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
628 ret = ext2_xattr_check_entry(entry, block_size);
631 data = buffer + entry->e_value_offs;
632 datalen = entry->e_value_size;
633 ret = ext2_copy_single_xattr(trans, root, objectid,
634 entry, data, datalen);
637 entry = EXT2_EXT_ATTR_NEXT(entry);
641 if ((void *)ext2_inode != inode_buf)
646 static inline dev_t old_decode_dev(u16 val)
648 return MKDEV((val >> 8) & 255, val & 255);
651 static void ext2_copy_inode_item(struct btrfs_inode_item *dst,
652 struct ext2_inode *src, u32 blocksize)
654 btrfs_set_stack_inode_generation(dst, 1);
655 btrfs_set_stack_inode_sequence(dst, 0);
656 btrfs_set_stack_inode_transid(dst, 1);
657 btrfs_set_stack_inode_size(dst, src->i_size);
658 btrfs_set_stack_inode_nbytes(dst, 0);
659 btrfs_set_stack_inode_block_group(dst, 0);
660 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
661 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
662 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
663 btrfs_set_stack_inode_mode(dst, src->i_mode);
664 btrfs_set_stack_inode_rdev(dst, 0);
665 btrfs_set_stack_inode_flags(dst, 0);
666 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
667 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
668 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
669 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
670 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
671 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
672 btrfs_set_stack_timespec_sec(&dst->otime, 0);
673 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
675 if (S_ISDIR(src->i_mode)) {
676 btrfs_set_stack_inode_size(dst, 0);
677 btrfs_set_stack_inode_nlink(dst, 1);
679 if (S_ISREG(src->i_mode)) {
680 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
683 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
684 !S_ISLNK(src->i_mode)) {
685 if (src->i_block[0]) {
686 btrfs_set_stack_inode_rdev(dst,
687 old_decode_dev(src->i_block[0]));
689 btrfs_set_stack_inode_rdev(dst,
690 decode_dev(src->i_block[1]));
693 memset(&dst->reserved, 0, sizeof(dst->reserved));
695 static int ext2_check_state(struct btrfs_convert_context *cctx)
697 ext2_filsys fs = cctx->fs_data;
699 if (!(fs->super->s_state & EXT2_VALID_FS))
701 else if (fs->super->s_state & EXT2_ERROR_FS)
707 /* EXT2_*_FL to BTRFS_INODE_FLAG_* stringification helper */
708 #define COPY_ONE_EXT2_FLAG(flags, ext2_inode, name) ({ \
709 if (ext2_inode->i_flags & EXT2_##name##_FL) \
710 flags |= BTRFS_INODE_##name; \
714 * Convert EXT2_*_FL to corresponding BTRFS_INODE_* flags
716 * Only a subset of EXT_*_FL is supported in btrfs.
718 static void ext2_convert_inode_flags(struct btrfs_inode_item *dst,
719 struct ext2_inode *src)
721 u64 flags = btrfs_stack_inode_flags(dst);
723 COPY_ONE_EXT2_FLAG(flags, src, APPEND);
724 COPY_ONE_EXT2_FLAG(flags, src, SYNC);
725 COPY_ONE_EXT2_FLAG(flags, src, IMMUTABLE);
726 COPY_ONE_EXT2_FLAG(flags, src, NODUMP);
727 COPY_ONE_EXT2_FLAG(flags, src, NOATIME);
728 COPY_ONE_EXT2_FLAG(flags, src, DIRSYNC);
729 btrfs_set_stack_inode_flags(dst, flags);
733 * copy a single inode. do all the required works, such as cloning
734 * inode item, creating file extents and creating directory entries.
736 static int ext2_copy_single_inode(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root, u64 objectid,
738 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
739 struct ext2_inode *ext2_inode,
743 struct btrfs_inode_item btrfs_inode;
745 if (ext2_inode->i_links_count == 0)
748 ext2_copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
749 if (!(convert_flags & CONVERT_FLAG_DATACSUM)
750 && S_ISREG(ext2_inode->i_mode)) {
751 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
752 BTRFS_INODE_NODATASUM;
753 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
755 ext2_convert_inode_flags(&btrfs_inode, ext2_inode);
757 switch (ext2_inode->i_mode & S_IFMT) {
759 ret = ext2_create_file_extents(trans, root, objectid,
760 &btrfs_inode, ext2_fs, ext2_ino, convert_flags);
763 ret = ext2_create_dir_entries(trans, root, objectid,
764 &btrfs_inode, ext2_fs, ext2_ino);
767 ret = ext2_create_symlink(trans, root, objectid,
768 &btrfs_inode, ext2_fs, ext2_ino, ext2_inode);
777 if (convert_flags & CONVERT_FLAG_XATTR) {
778 ret = ext2_copy_extended_attrs(trans, root, objectid,
779 &btrfs_inode, ext2_fs, ext2_ino);
783 return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
786 static int ext2_is_special_inode(ext2_ino_t ino)
788 if (ino < EXT2_GOOD_OLD_FIRST_INO && ino != EXT2_ROOT_INO)
794 * scan ext2's inode bitmap and copy all used inodes.
796 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
797 struct btrfs_root *root,
798 u32 convert_flags, struct task_ctx *p)
800 ext2_filsys ext2_fs = cctx->fs_data;
803 ext2_inode_scan ext2_scan;
804 struct ext2_inode ext2_inode;
807 struct btrfs_trans_handle *trans;
809 trans = btrfs_start_transaction(root, 1);
811 return PTR_ERR(trans);
812 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
814 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
817 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
822 if (ext2_is_special_inode(ext2_ino))
824 objectid = ext2_ino + INO_OFFSET;
825 ret = ext2_copy_single_inode(trans, root,
826 objectid, ext2_fs, ext2_ino,
827 &ext2_inode, convert_flags);
828 pthread_mutex_lock(&p->mutex);
829 p->cur_copy_inodes++;
830 pthread_mutex_unlock(&p->mutex);
833 if (trans->blocks_used >= 4096) {
834 ret = btrfs_commit_transaction(trans, root);
836 trans = btrfs_start_transaction(root, 1);
837 BUG_ON(IS_ERR(trans));
841 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
844 ret = btrfs_commit_transaction(trans, root);
846 ext2fs_close_inode_scan(ext2_scan);
851 const struct btrfs_convert_operations ext2_convert_ops = {
853 .open_fs = ext2_open_fs,
854 .read_used_space = ext2_read_used_space,
855 .copy_inodes = ext2_copy_inodes,
856 .close_fs = ext2_close_fs,
857 .check_state = ext2_check_state,
860 #endif /* BTRFSCONVERT_EXT2 */