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);
101 static int __ext2_add_one_block(ext2_filsys fs, char *bitmap,
102 unsigned long group_nr, struct cache_tree *used)
104 unsigned long offset;
108 offset = fs->super->s_first_data_block;
109 offset /= EXT2FS_CLUSTER_RATIO(fs);
110 offset += group_nr * EXT2_CLUSTERS_PER_GROUP(fs->super);
111 for (i = 0; i < EXT2_CLUSTERS_PER_GROUP(fs->super); i++) {
112 if ((i + offset) >= ext2fs_blocks_count(fs->super))
115 if (ext2fs_test_bit(i, bitmap)) {
118 start = (i + offset) * EXT2FS_CLUSTER_RATIO(fs);
119 start *= fs->blocksize;
120 ret = add_merge_cache_extent(used, start,
130 * Read all used ext2 space into cctx->used cache tree
132 static int ext2_read_used_space(struct btrfs_convert_context *cctx)
134 ext2_filsys fs = (ext2_filsys)cctx->fs_data;
135 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
136 struct cache_tree *used_tree = &cctx->used_space;
137 char *block_bitmap = NULL;
142 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
144 error("EXT2_CLUSTERS_PER_GROUP too small: %llu",
145 (unsigned long long)(EXT2_CLUSTERS_PER_GROUP(fs->super)));
149 block_bitmap = malloc(block_nbytes);
153 for (i = 0; i < fs->group_desc_count; i++) {
154 ret = ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr,
155 block_nbytes * 8, block_bitmap);
157 error("fail to get bitmap from ext2, %s",
161 ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree);
163 error("fail to build used space tree, %s",
167 blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super);
174 static void ext2_close_fs(struct btrfs_convert_context *cctx)
176 if (cctx->volume_name) {
177 free(cctx->volume_name);
178 cctx->volume_name = NULL;
180 ext2fs_close(cctx->fs_data);
183 static u8 ext2_filetype_conversion_table[EXT2_FT_MAX] = {
184 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
185 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
186 [EXT2_FT_DIR] = BTRFS_FT_DIR,
187 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
188 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
189 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
190 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
191 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
194 static int ext2_dir_iterate_proc(ext2_ino_t dir, int entry,
195 struct ext2_dir_entry *dirent,
196 int offset, int blocksize,
197 char *buf,void *priv_data)
202 char dotdot[] = "..";
203 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
206 name_len = dirent->name_len & 0xFF;
208 objectid = dirent->inode + INO_OFFSET;
209 if (!strncmp(dirent->name, dotdot, name_len)) {
211 BUG_ON(idata->parent != 0);
212 idata->parent = objectid;
216 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
219 file_type = dirent->name_len >> 8;
220 BUG_ON(file_type > EXT2_FT_SYMLINK);
222 ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
223 name_len, idata->objectid, objectid,
224 ext2_filetype_conversion_table[file_type],
225 idata->index_cnt, idata->inode);
227 idata->errcode = ret;
235 static int ext2_create_dir_entries(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root, u64 objectid,
237 struct btrfs_inode_item *btrfs_inode,
238 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
242 struct dir_iterate_data data = {
245 .inode = btrfs_inode,
246 .objectid = objectid,
252 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
253 ext2_dir_iterate_proc, &data);
257 if (ret == 0 && data.parent == objectid) {
258 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
259 objectid, objectid, 0);
263 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
267 static int ext2_block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
268 e2_blkcnt_t blockcnt, blk_t ref_block,
269 int ref_offset, void *priv_data)
272 struct blk_iterate_data *idata;
273 idata = (struct blk_iterate_data *)priv_data;
274 ret = block_iterate_proc(*blocknr, blockcnt, idata);
276 idata->errcode = ret;
283 * traverse file's data blocks, record these data blocks as file extents.
285 static int ext2_create_file_extents(struct btrfs_trans_handle *trans,
286 struct btrfs_root *root, u64 objectid,
287 struct btrfs_inode_item *btrfs_inode,
288 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
295 u32 sectorsize = root->fs_info->sectorsize;
296 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
297 struct blk_iterate_data data;
299 init_blk_iterate_data(&data, trans, root, btrfs_inode, objectid,
300 convert_flags & CONVERT_FLAG_DATACSUM);
302 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
303 NULL, ext2_block_iterate_proc, &data);
309 if ((convert_flags & CONVERT_FLAG_INLINE_DATA) && data.first_block == 0
310 && data.num_blocks > 0
311 && inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
312 u64 num_bytes = data.num_blocks * sectorsize;
313 u64 disk_bytenr = data.disk_block * sectorsize;
316 buffer = malloc(num_bytes);
319 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
322 if (num_bytes > inode_size)
323 num_bytes = inode_size;
324 ret = btrfs_insert_inline_extent(trans, root, objectid,
325 0, buffer, num_bytes);
328 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
329 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
330 } else if (data.num_blocks > 0) {
331 ret = record_file_blocks(&data, data.first_block,
332 data.disk_block, data.num_blocks);
336 data.first_block += data.num_blocks;
337 last_block = (inode_size + sectorsize - 1) / sectorsize;
338 if (last_block > data.first_block) {
339 ret = record_file_blocks(&data, data.first_block, 0,
340 last_block - data.first_block);
346 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
350 static int ext2_create_symlink(struct btrfs_trans_handle *trans,
351 struct btrfs_root *root, u64 objectid,
352 struct btrfs_inode_item *btrfs_inode,
353 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
354 struct ext2_inode *ext2_inode)
358 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
359 if (ext2fs_inode_data_blocks2(ext2_fs, ext2_inode)) {
360 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
361 ret = ext2_create_file_extents(trans, root, objectid,
362 btrfs_inode, ext2_fs, ext2_ino,
363 CONVERT_FLAG_DATACSUM |
364 CONVERT_FLAG_INLINE_DATA);
365 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
369 pathname = (char *)&(ext2_inode->i_block[0]);
370 BUG_ON(pathname[inode_size] != 0);
371 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
372 pathname, inode_size + 1);
373 btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
378 * Following xattr/acl related codes are based on codes in
379 * fs/ext3/xattr.c and fs/ext3/acl.c
381 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
382 #define EXT2_XATTR_BFIRST(ptr) \
383 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
384 #define EXT2_XATTR_IHDR(inode) \
385 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
386 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
387 #define EXT2_XATTR_IFIRST(inode) \
388 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
389 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
391 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
394 struct ext2_ext_attr_entry *next;
396 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
397 next = EXT2_EXT_ATTR_NEXT(entry);
398 if ((void *)next >= end)
405 static int ext2_xattr_check_block(const char *buf, size_t size)
408 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
410 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
411 header->h_blocks != 1)
413 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
417 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
420 size_t value_size = entry->e_value_size;
422 if (entry->e_value_block != 0 || value_size > size ||
423 entry->e_value_offs + value_size > size)
428 static int ext2_acl_to_xattr(void *dst, const void *src,
429 size_t dst_size, size_t src_size)
432 const void *end = src + src_size;
433 acl_ea_header *ext_acl = (acl_ea_header *)dst;
434 acl_ea_entry *dst_entry = ext_acl->a_entries;
435 ext2_acl_entry *src_entry;
437 if (src_size < sizeof(ext2_acl_header))
439 if (((ext2_acl_header *)src)->a_version !=
440 cpu_to_le32(EXT2_ACL_VERSION))
442 src += sizeof(ext2_acl_header);
443 count = ext2_acl_count(src_size);
447 BUG_ON(dst_size < acl_ea_size(count));
448 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
449 for (i = 0; i < count; i++, dst_entry++) {
450 src_entry = (ext2_acl_entry *)src;
451 if (src + sizeof(ext2_acl_entry_short) > end)
453 dst_entry->e_tag = src_entry->e_tag;
454 dst_entry->e_perm = src_entry->e_perm;
455 switch (le16_to_cpu(src_entry->e_tag)) {
460 src += sizeof(ext2_acl_entry_short);
461 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
465 src += sizeof(ext2_acl_entry);
468 dst_entry->e_id = src_entry->e_id;
481 static char *xattr_prefix_table[] = {
483 [2] = "system.posix_acl_access",
484 [3] = "system.posix_acl_default",
489 static int ext2_copy_single_xattr(struct btrfs_trans_handle *trans,
490 struct btrfs_root *root, u64 objectid,
491 struct ext2_ext_attr_entry *entry,
492 const void *data, u32 datalen)
497 void *databuf = NULL;
498 char namebuf[XATTR_NAME_MAX + 1];
500 name_index = entry->e_name_index;
501 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
502 xattr_prefix_table[name_index] == NULL)
504 name_len = strlen(xattr_prefix_table[name_index]) +
506 if (name_len >= sizeof(namebuf))
509 if (name_index == 2 || name_index == 3) {
510 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
511 databuf = malloc(bufsize);
514 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
520 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
521 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
522 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
523 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
524 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
525 objectid - INO_OFFSET, name_len, namebuf);
528 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
529 data, datalen, objectid);
535 static int ext2_copy_extended_attrs(struct btrfs_trans_handle *trans,
536 struct btrfs_root *root, u64 objectid,
537 struct btrfs_inode_item *btrfs_inode,
538 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
544 u32 block_size = ext2_fs->blocksize;
545 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
546 struct ext2_inode_large *ext2_inode;
547 struct ext2_ext_attr_entry *entry;
550 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
552 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
553 ext2_inode = (struct ext2_inode_large *)inode_buf;
555 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
559 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
562 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
568 if (ext2_ino > ext2_fs->super->s_first_ino &&
569 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
570 if (EXT2_GOOD_OLD_INODE_SIZE +
571 ext2_inode->i_extra_isize > inode_size) {
575 if (ext2_inode->i_extra_isize != 0 &&
576 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
577 EXT2_EXT_ATTR_MAGIC) {
583 void *end = (void *)ext2_inode + inode_size;
584 entry = EXT2_XATTR_IFIRST(ext2_inode);
585 total = end - (void *)entry;
586 ret = ext2_xattr_check_names(entry, end);
589 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
590 ret = ext2_xattr_check_entry(entry, total);
593 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
595 datalen = entry->e_value_size;
596 ret = ext2_copy_single_xattr(trans, root, objectid,
597 entry, data, datalen);
600 entry = EXT2_EXT_ATTR_NEXT(entry);
604 if (ext2_inode->i_file_acl == 0)
607 buffer = malloc(block_size);
612 err = ext2fs_read_ext_attr2(ext2_fs, ext2_inode->i_file_acl, buffer);
614 fprintf(stderr, "ext2fs_read_ext_attr2: %s\n",
619 ret = ext2_xattr_check_block(buffer, block_size);
623 entry = EXT2_XATTR_BFIRST(buffer);
624 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
625 ret = ext2_xattr_check_entry(entry, block_size);
628 data = buffer + entry->e_value_offs;
629 datalen = entry->e_value_size;
630 ret = ext2_copy_single_xattr(trans, root, objectid,
631 entry, data, datalen);
634 entry = EXT2_EXT_ATTR_NEXT(entry);
638 if ((void *)ext2_inode != inode_buf)
643 static inline dev_t old_decode_dev(u16 val)
645 return MKDEV((val >> 8) & 255, val & 255);
648 static void ext2_copy_inode_item(struct btrfs_inode_item *dst,
649 struct ext2_inode *src, u32 blocksize)
651 btrfs_set_stack_inode_generation(dst, 1);
652 btrfs_set_stack_inode_sequence(dst, 0);
653 btrfs_set_stack_inode_transid(dst, 1);
654 btrfs_set_stack_inode_size(dst, src->i_size);
655 btrfs_set_stack_inode_nbytes(dst, 0);
656 btrfs_set_stack_inode_block_group(dst, 0);
657 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
658 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
659 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
660 btrfs_set_stack_inode_mode(dst, src->i_mode);
661 btrfs_set_stack_inode_rdev(dst, 0);
662 btrfs_set_stack_inode_flags(dst, 0);
663 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
664 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
665 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
666 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
667 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
668 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
669 btrfs_set_stack_timespec_sec(&dst->otime, 0);
670 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
672 if (S_ISDIR(src->i_mode)) {
673 btrfs_set_stack_inode_size(dst, 0);
674 btrfs_set_stack_inode_nlink(dst, 1);
676 if (S_ISREG(src->i_mode)) {
677 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
680 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
681 !S_ISLNK(src->i_mode)) {
682 if (src->i_block[0]) {
683 btrfs_set_stack_inode_rdev(dst,
684 old_decode_dev(src->i_block[0]));
686 btrfs_set_stack_inode_rdev(dst,
687 decode_dev(src->i_block[1]));
690 memset(&dst->reserved, 0, sizeof(dst->reserved));
692 static int ext2_check_state(struct btrfs_convert_context *cctx)
694 ext2_filsys fs = cctx->fs_data;
696 if (!(fs->super->s_state & EXT2_VALID_FS))
698 else if (fs->super->s_state & EXT2_ERROR_FS)
704 /* EXT2_*_FL to BTRFS_INODE_FLAG_* stringification helper */
705 #define COPY_ONE_EXT2_FLAG(flags, ext2_inode, name) ({ \
706 if (ext2_inode->i_flags & EXT2_##name##_FL) \
707 flags |= BTRFS_INODE_##name; \
711 * Convert EXT2_*_FL to corresponding BTRFS_INODE_* flags
713 * Only a subset of EXT_*_FL is supported in btrfs.
715 static void ext2_convert_inode_flags(struct btrfs_inode_item *dst,
716 struct ext2_inode *src)
718 u64 flags = btrfs_stack_inode_flags(dst);
720 COPY_ONE_EXT2_FLAG(flags, src, APPEND);
721 COPY_ONE_EXT2_FLAG(flags, src, SYNC);
722 COPY_ONE_EXT2_FLAG(flags, src, IMMUTABLE);
723 COPY_ONE_EXT2_FLAG(flags, src, NODUMP);
724 COPY_ONE_EXT2_FLAG(flags, src, NOATIME);
725 COPY_ONE_EXT2_FLAG(flags, src, DIRSYNC);
726 btrfs_set_stack_inode_flags(dst, flags);
730 * copy a single inode. do all the required works, such as cloning
731 * inode item, creating file extents and creating directory entries.
733 static int ext2_copy_single_inode(struct btrfs_trans_handle *trans,
734 struct btrfs_root *root, u64 objectid,
735 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
736 struct ext2_inode *ext2_inode,
740 struct btrfs_inode_item btrfs_inode;
742 if (ext2_inode->i_links_count == 0)
745 ext2_copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
746 if (!(convert_flags & CONVERT_FLAG_DATACSUM)
747 && S_ISREG(ext2_inode->i_mode)) {
748 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
749 BTRFS_INODE_NODATASUM;
750 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
752 ext2_convert_inode_flags(&btrfs_inode, ext2_inode);
754 switch (ext2_inode->i_mode & S_IFMT) {
756 ret = ext2_create_file_extents(trans, root, objectid,
757 &btrfs_inode, ext2_fs, ext2_ino, convert_flags);
760 ret = ext2_create_dir_entries(trans, root, objectid,
761 &btrfs_inode, ext2_fs, ext2_ino);
764 ret = ext2_create_symlink(trans, root, objectid,
765 &btrfs_inode, ext2_fs, ext2_ino, ext2_inode);
774 if (convert_flags & CONVERT_FLAG_XATTR) {
775 ret = ext2_copy_extended_attrs(trans, root, objectid,
776 &btrfs_inode, ext2_fs, ext2_ino);
780 return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
783 static int ext2_is_special_inode(ext2_ino_t ino)
785 if (ino < EXT2_GOOD_OLD_FIRST_INO && ino != EXT2_ROOT_INO)
791 * scan ext2's inode bitmap and copy all used inodes.
793 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
794 struct btrfs_root *root,
795 u32 convert_flags, struct task_ctx *p)
797 ext2_filsys ext2_fs = cctx->fs_data;
800 ext2_inode_scan ext2_scan;
801 struct ext2_inode ext2_inode;
804 struct btrfs_trans_handle *trans;
806 trans = btrfs_start_transaction(root, 1);
808 return PTR_ERR(trans);
809 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
811 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
814 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
819 if (ext2_is_special_inode(ext2_ino))
821 objectid = ext2_ino + INO_OFFSET;
822 ret = ext2_copy_single_inode(trans, root,
823 objectid, ext2_fs, ext2_ino,
824 &ext2_inode, convert_flags);
825 pthread_mutex_lock(&p->mutex);
826 p->cur_copy_inodes++;
827 pthread_mutex_unlock(&p->mutex);
830 if (trans->blocks_used >= 4096) {
831 ret = btrfs_commit_transaction(trans, root);
833 trans = btrfs_start_transaction(root, 1);
834 BUG_ON(IS_ERR(trans));
838 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
841 ret = btrfs_commit_transaction(trans, root);
843 ext2fs_close_inode_scan(ext2_scan);
848 const struct btrfs_convert_operations ext2_convert_ops = {
850 .open_fs = ext2_open_fs,
851 .read_used_space = ext2_read_used_space,
852 .copy_inodes = ext2_copy_inodes,
853 .close_fs = ext2_close_fs,
854 .check_state = ext2_check_state,
857 #endif /* BTRFSCONVERT_EXT2 */