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 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
46 * We need to know exactly the used space, some RO compat flags like
47 * BIGALLOC will affect how used space is present.
48 * So we need manuall check any unsupported RO compat flags
50 ro_feature = ext2_fs->super->s_feature_ro_compat;
51 if (ro_feature & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
53 "unsupported RO features detected: %x, abort convert to avoid possible corruption",
54 ro_feature & ~EXT2_LIB_FEATURE_COMPAT_SUPP);
57 ret = ext2fs_read_inode_bitmap(ext2_fs);
59 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
63 ret = ext2fs_read_block_bitmap(ext2_fs);
65 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
70 * search each block group for a free inode. this set up
71 * uninit block/inode bitmaps appropriately.
74 while (ino <= ext2_fs->super->s_inodes_count) {
76 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
77 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
80 if (!(ext2_fs->super->s_feature_incompat &
81 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
82 error("filetype feature is missing");
86 cctx->fs_data = ext2_fs;
87 cctx->blocksize = ext2_fs->blocksize;
88 cctx->block_count = ext2_fs->super->s_blocks_count;
89 cctx->total_bytes = ext2_fs->blocksize * ext2_fs->super->s_blocks_count;
90 cctx->volume_name = strndup(ext2_fs->super->s_volume_name, 16);
91 cctx->first_data_block = ext2_fs->super->s_first_data_block;
92 cctx->inodes_count = ext2_fs->super->s_inodes_count;
93 cctx->free_inodes_count = ext2_fs->super->s_free_inodes_count;
96 ext2fs_close(ext2_fs);
100 static int __ext2_add_one_block(ext2_filsys fs, char *bitmap,
101 unsigned long group_nr, struct cache_tree *used)
103 unsigned long offset;
107 offset = fs->super->s_first_data_block;
108 offset /= EXT2FS_CLUSTER_RATIO(fs);
109 offset += group_nr * EXT2_CLUSTERS_PER_GROUP(fs->super);
110 for (i = 0; i < EXT2_CLUSTERS_PER_GROUP(fs->super); i++) {
111 if ((i + offset) >= ext2fs_blocks_count(fs->super))
114 if (ext2fs_test_bit(i, bitmap)) {
117 start = (i + offset) * EXT2FS_CLUSTER_RATIO(fs);
118 start *= fs->blocksize;
119 ret = add_merge_cache_extent(used, start,
129 * Read all used ext2 space into cctx->used cache tree
131 static int ext2_read_used_space(struct btrfs_convert_context *cctx)
133 ext2_filsys fs = (ext2_filsys)cctx->fs_data;
134 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
135 struct cache_tree *used_tree = &cctx->used_space;
136 char *block_bitmap = NULL;
141 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
143 error("EXT2_CLUSTERS_PER_GROUP too small: %llu",
144 (unsigned long long)(EXT2_CLUSTERS_PER_GROUP(fs->super)));
148 block_bitmap = malloc(block_nbytes);
152 for (i = 0; i < fs->group_desc_count; i++) {
153 ret = ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr,
154 block_nbytes * 8, block_bitmap);
156 error("fail to get bitmap from ext2, %s",
160 ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree);
162 error("fail to build used space tree, %s",
166 blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super);
173 static void ext2_close_fs(struct btrfs_convert_context *cctx)
175 if (cctx->volume_name) {
176 free(cctx->volume_name);
177 cctx->volume_name = NULL;
179 ext2fs_close(cctx->fs_data);
182 static u8 ext2_filetype_conversion_table[EXT2_FT_MAX] = {
183 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
184 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
185 [EXT2_FT_DIR] = BTRFS_FT_DIR,
186 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
187 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
188 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
189 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
190 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
193 static int ext2_dir_iterate_proc(ext2_ino_t dir, int entry,
194 struct ext2_dir_entry *dirent,
195 int offset, int blocksize,
196 char *buf,void *priv_data)
201 char dotdot[] = "..";
202 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
205 name_len = dirent->name_len & 0xFF;
207 objectid = dirent->inode + INO_OFFSET;
208 if (!strncmp(dirent->name, dotdot, name_len)) {
210 BUG_ON(idata->parent != 0);
211 idata->parent = objectid;
215 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
218 file_type = dirent->name_len >> 8;
219 BUG_ON(file_type > EXT2_FT_SYMLINK);
221 ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
222 name_len, idata->objectid, objectid,
223 ext2_filetype_conversion_table[file_type],
224 idata->index_cnt, idata->inode);
226 idata->errcode = ret;
234 static int ext2_create_dir_entries(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root, u64 objectid,
236 struct btrfs_inode_item *btrfs_inode,
237 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
241 struct dir_iterate_data data = {
244 .inode = btrfs_inode,
245 .objectid = objectid,
251 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
252 ext2_dir_iterate_proc, &data);
256 if (ret == 0 && data.parent == objectid) {
257 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
258 objectid, objectid, 0);
262 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
266 static int ext2_block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
267 e2_blkcnt_t blockcnt, blk_t ref_block,
268 int ref_offset, void *priv_data)
271 struct blk_iterate_data *idata;
272 idata = (struct blk_iterate_data *)priv_data;
273 ret = block_iterate_proc(*blocknr, blockcnt, idata);
275 idata->errcode = ret;
282 * traverse file's data blocks, record these data blocks as file extents.
284 static int ext2_create_file_extents(struct btrfs_trans_handle *trans,
285 struct btrfs_root *root, u64 objectid,
286 struct btrfs_inode_item *btrfs_inode,
287 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
294 u32 sectorsize = root->fs_info->sectorsize;
295 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
296 struct blk_iterate_data data;
298 init_blk_iterate_data(&data, trans, root, btrfs_inode, objectid,
299 convert_flags & CONVERT_FLAG_DATACSUM);
301 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
302 NULL, ext2_block_iterate_proc, &data);
308 if ((convert_flags & CONVERT_FLAG_INLINE_DATA) && data.first_block == 0
309 && data.num_blocks > 0
310 && inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
311 u64 num_bytes = data.num_blocks * sectorsize;
312 u64 disk_bytenr = data.disk_block * sectorsize;
315 buffer = malloc(num_bytes);
318 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
321 if (num_bytes > inode_size)
322 num_bytes = inode_size;
323 ret = btrfs_insert_inline_extent(trans, root, objectid,
324 0, buffer, num_bytes);
327 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
328 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
329 } else if (data.num_blocks > 0) {
330 ret = record_file_blocks(&data, data.first_block,
331 data.disk_block, data.num_blocks);
335 data.first_block += data.num_blocks;
336 last_block = (inode_size + sectorsize - 1) / sectorsize;
337 if (last_block > data.first_block) {
338 ret = record_file_blocks(&data, data.first_block, 0,
339 last_block - data.first_block);
345 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
349 static int ext2_create_symlink(struct btrfs_trans_handle *trans,
350 struct btrfs_root *root, u64 objectid,
351 struct btrfs_inode_item *btrfs_inode,
352 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
353 struct ext2_inode *ext2_inode)
357 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
358 if (ext2fs_inode_data_blocks2(ext2_fs, ext2_inode)) {
359 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
360 ret = ext2_create_file_extents(trans, root, objectid,
361 btrfs_inode, ext2_fs, ext2_ino,
362 CONVERT_FLAG_DATACSUM |
363 CONVERT_FLAG_INLINE_DATA);
364 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
368 pathname = (char *)&(ext2_inode->i_block[0]);
369 BUG_ON(pathname[inode_size] != 0);
370 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
371 pathname, inode_size + 1);
372 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
377 * Following xattr/acl related codes are based on codes in
378 * fs/ext3/xattr.c and fs/ext3/acl.c
380 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
381 #define EXT2_XATTR_BFIRST(ptr) \
382 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
383 #define EXT2_XATTR_IHDR(inode) \
384 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
385 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
386 #define EXT2_XATTR_IFIRST(inode) \
387 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
388 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
390 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
393 struct ext2_ext_attr_entry *next;
395 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
396 next = EXT2_EXT_ATTR_NEXT(entry);
397 if ((void *)next >= end)
404 static int ext2_xattr_check_block(const char *buf, size_t size)
407 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
409 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
410 header->h_blocks != 1)
412 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
416 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
419 size_t value_size = entry->e_value_size;
421 if (entry->e_value_block != 0 || value_size > size ||
422 entry->e_value_offs + value_size > size)
427 static int ext2_acl_to_xattr(void *dst, const void *src,
428 size_t dst_size, size_t src_size)
431 const void *end = src + src_size;
432 acl_ea_header *ext_acl = (acl_ea_header *)dst;
433 acl_ea_entry *dst_entry = ext_acl->a_entries;
434 ext2_acl_entry *src_entry;
436 if (src_size < sizeof(ext2_acl_header))
438 if (((ext2_acl_header *)src)->a_version !=
439 cpu_to_le32(EXT2_ACL_VERSION))
441 src += sizeof(ext2_acl_header);
442 count = ext2_acl_count(src_size);
446 BUG_ON(dst_size < acl_ea_size(count));
447 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
448 for (i = 0; i < count; i++, dst_entry++) {
449 src_entry = (ext2_acl_entry *)src;
450 if (src + sizeof(ext2_acl_entry_short) > end)
452 dst_entry->e_tag = src_entry->e_tag;
453 dst_entry->e_perm = src_entry->e_perm;
454 switch (le16_to_cpu(src_entry->e_tag)) {
459 src += sizeof(ext2_acl_entry_short);
460 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
464 src += sizeof(ext2_acl_entry);
467 dst_entry->e_id = src_entry->e_id;
480 static char *xattr_prefix_table[] = {
482 [2] = "system.posix_acl_access",
483 [3] = "system.posix_acl_default",
488 static int ext2_copy_single_xattr(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root, u64 objectid,
490 struct ext2_ext_attr_entry *entry,
491 const void *data, u32 datalen)
496 void *databuf = NULL;
497 char namebuf[XATTR_NAME_MAX + 1];
499 name_index = entry->e_name_index;
500 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
501 xattr_prefix_table[name_index] == NULL)
503 name_len = strlen(xattr_prefix_table[name_index]) +
505 if (name_len >= sizeof(namebuf))
508 if (name_index == 2 || name_index == 3) {
509 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
510 databuf = malloc(bufsize);
513 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
519 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
520 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
521 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
522 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
523 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
524 objectid - INO_OFFSET, name_len, namebuf);
527 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
528 data, datalen, objectid);
534 static int ext2_copy_extended_attrs(struct btrfs_trans_handle *trans,
535 struct btrfs_root *root, u64 objectid,
536 struct btrfs_inode_item *btrfs_inode,
537 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
543 u32 block_size = ext2_fs->blocksize;
544 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
545 struct ext2_inode_large *ext2_inode;
546 struct ext2_ext_attr_entry *entry;
549 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
551 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
552 ext2_inode = (struct ext2_inode_large *)inode_buf;
554 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
558 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
561 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
567 if (ext2_ino > ext2_fs->super->s_first_ino &&
568 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
569 if (EXT2_GOOD_OLD_INODE_SIZE +
570 ext2_inode->i_extra_isize > inode_size) {
574 if (ext2_inode->i_extra_isize != 0 &&
575 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
576 EXT2_EXT_ATTR_MAGIC) {
582 void *end = (void *)ext2_inode + inode_size;
583 entry = EXT2_XATTR_IFIRST(ext2_inode);
584 total = end - (void *)entry;
585 ret = ext2_xattr_check_names(entry, end);
588 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
589 ret = ext2_xattr_check_entry(entry, total);
592 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
594 datalen = entry->e_value_size;
595 ret = ext2_copy_single_xattr(trans, root, objectid,
596 entry, data, datalen);
599 entry = EXT2_EXT_ATTR_NEXT(entry);
603 if (ext2_inode->i_file_acl == 0)
606 buffer = malloc(block_size);
611 err = ext2fs_read_ext_attr2(ext2_fs, ext2_inode->i_file_acl, buffer);
613 fprintf(stderr, "ext2fs_read_ext_attr2: %s\n",
618 ret = ext2_xattr_check_block(buffer, block_size);
622 entry = EXT2_XATTR_BFIRST(buffer);
623 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
624 ret = ext2_xattr_check_entry(entry, block_size);
627 data = buffer + entry->e_value_offs;
628 datalen = entry->e_value_size;
629 ret = ext2_copy_single_xattr(trans, root, objectid,
630 entry, data, datalen);
633 entry = EXT2_EXT_ATTR_NEXT(entry);
637 if ((void *)ext2_inode != inode_buf)
642 static inline dev_t old_decode_dev(u16 val)
644 return MKDEV((val >> 8) & 255, val & 255);
647 static void ext2_copy_inode_item(struct btrfs_inode_item *dst,
648 struct ext2_inode *src, u32 blocksize)
650 btrfs_set_stack_inode_generation(dst, 1);
651 btrfs_set_stack_inode_sequence(dst, 0);
652 btrfs_set_stack_inode_transid(dst, 1);
653 btrfs_set_stack_inode_size(dst, src->i_size);
654 btrfs_set_stack_inode_nbytes(dst, 0);
655 btrfs_set_stack_inode_block_group(dst, 0);
656 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
657 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
658 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
659 btrfs_set_stack_inode_mode(dst, src->i_mode);
660 btrfs_set_stack_inode_rdev(dst, 0);
661 btrfs_set_stack_inode_flags(dst, 0);
662 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
663 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
664 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
665 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
666 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
667 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
668 btrfs_set_stack_timespec_sec(&dst->otime, 0);
669 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
671 if (S_ISDIR(src->i_mode)) {
672 btrfs_set_stack_inode_size(dst, 0);
673 btrfs_set_stack_inode_nlink(dst, 1);
675 if (S_ISREG(src->i_mode)) {
676 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
679 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
680 !S_ISLNK(src->i_mode)) {
681 if (src->i_block[0]) {
682 btrfs_set_stack_inode_rdev(dst,
683 old_decode_dev(src->i_block[0]));
685 btrfs_set_stack_inode_rdev(dst,
686 decode_dev(src->i_block[1]));
689 memset(&dst->reserved, 0, sizeof(dst->reserved));
691 static int ext2_check_state(struct btrfs_convert_context *cctx)
693 ext2_filsys fs = cctx->fs_data;
695 if (!(fs->super->s_state & EXT2_VALID_FS))
697 else if (fs->super->s_state & EXT2_ERROR_FS)
703 /* EXT2_*_FL to BTRFS_INODE_FLAG_* stringification helper */
704 #define COPY_ONE_EXT2_FLAG(flags, ext2_inode, name) ({ \
705 if (ext2_inode->i_flags & EXT2_##name##_FL) \
706 flags |= BTRFS_INODE_##name; \
710 * Convert EXT2_*_FL to corresponding BTRFS_INODE_* flags
712 * Only a subset of EXT_*_FL is supported in btrfs.
714 static void ext2_convert_inode_flags(struct btrfs_inode_item *dst,
715 struct ext2_inode *src)
717 u64 flags = btrfs_stack_inode_flags(dst);
719 COPY_ONE_EXT2_FLAG(flags, src, APPEND);
720 COPY_ONE_EXT2_FLAG(flags, src, SYNC);
721 COPY_ONE_EXT2_FLAG(flags, src, IMMUTABLE);
722 COPY_ONE_EXT2_FLAG(flags, src, NODUMP);
723 COPY_ONE_EXT2_FLAG(flags, src, NOATIME);
724 COPY_ONE_EXT2_FLAG(flags, src, DIRSYNC);
725 btrfs_set_stack_inode_flags(dst, flags);
729 * copy a single inode. do all the required works, such as cloning
730 * inode item, creating file extents and creating directory entries.
732 static int ext2_copy_single_inode(struct btrfs_trans_handle *trans,
733 struct btrfs_root *root, u64 objectid,
734 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
735 struct ext2_inode *ext2_inode,
739 struct btrfs_inode_item btrfs_inode;
741 if (ext2_inode->i_links_count == 0)
744 ext2_copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
745 if (!(convert_flags & CONVERT_FLAG_DATACSUM)
746 && S_ISREG(ext2_inode->i_mode)) {
747 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
748 BTRFS_INODE_NODATASUM;
749 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
751 ext2_convert_inode_flags(&btrfs_inode, ext2_inode);
753 switch (ext2_inode->i_mode & S_IFMT) {
755 ret = ext2_create_file_extents(trans, root, objectid,
756 &btrfs_inode, ext2_fs, ext2_ino, convert_flags);
759 ret = ext2_create_dir_entries(trans, root, objectid,
760 &btrfs_inode, ext2_fs, ext2_ino);
763 ret = ext2_create_symlink(trans, root, objectid,
764 &btrfs_inode, ext2_fs, ext2_ino, ext2_inode);
773 if (convert_flags & CONVERT_FLAG_XATTR) {
774 ret = ext2_copy_extended_attrs(trans, root, objectid,
775 &btrfs_inode, ext2_fs, ext2_ino);
779 return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
782 static int ext2_is_special_inode(ext2_ino_t ino)
784 if (ino < EXT2_GOOD_OLD_FIRST_INO && ino != EXT2_ROOT_INO)
790 * scan ext2's inode bitmap and copy all used inodes.
792 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
793 struct btrfs_root *root,
794 u32 convert_flags, struct task_ctx *p)
796 ext2_filsys ext2_fs = cctx->fs_data;
799 ext2_inode_scan ext2_scan;
800 struct ext2_inode ext2_inode;
803 struct btrfs_trans_handle *trans;
805 trans = btrfs_start_transaction(root, 1);
807 return PTR_ERR(trans);
808 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
810 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
813 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
818 if (ext2_is_special_inode(ext2_ino))
820 objectid = ext2_ino + INO_OFFSET;
821 ret = ext2_copy_single_inode(trans, root,
822 objectid, ext2_fs, ext2_ino,
823 &ext2_inode, convert_flags);
824 pthread_mutex_lock(&p->mutex);
825 p->cur_copy_inodes++;
826 pthread_mutex_unlock(&p->mutex);
829 if (trans->blocks_used >= 4096) {
830 ret = btrfs_commit_transaction(trans, root);
832 trans = btrfs_start_transaction(root, 1);
833 BUG_ON(IS_ERR(trans));
837 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
840 ret = btrfs_commit_transaction(trans, root);
842 ext2fs_close_inode_scan(ext2_scan);
847 const struct btrfs_convert_operations ext2_convert_ops = {
849 .open_fs = ext2_open_fs,
850 .read_used_space = ext2_read_used_space,
851 .copy_inodes = ext2_copy_inodes,
852 .close_fs = ext2_close_fs,
853 .check_state = ext2_check_state,
856 #endif /* BTRFSCONVERT_EXT2 */