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 inline int ext2_acl_count(size_t size)
430 size -= sizeof(ext2_acl_header);
431 s = size - 4 * sizeof(ext2_acl_entry_short);
433 if (size % sizeof(ext2_acl_entry_short))
435 return size / sizeof(ext2_acl_entry_short);
437 if (s % sizeof(ext2_acl_entry))
439 return s / sizeof(ext2_acl_entry) + 4;
443 static inline size_t acl_ea_size(int count)
445 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
448 static int ext2_acl_to_xattr(void *dst, const void *src,
449 size_t dst_size, size_t src_size)
452 const void *end = src + src_size;
453 acl_ea_header *ext_acl = (acl_ea_header *)dst;
454 acl_ea_entry *dst_entry = ext_acl->a_entries;
455 ext2_acl_entry *src_entry;
457 if (src_size < sizeof(ext2_acl_header))
459 if (((ext2_acl_header *)src)->a_version !=
460 cpu_to_le32(EXT2_ACL_VERSION))
462 src += sizeof(ext2_acl_header);
463 count = ext2_acl_count(src_size);
467 BUG_ON(dst_size < acl_ea_size(count));
468 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
469 for (i = 0; i < count; i++, dst_entry++) {
470 src_entry = (ext2_acl_entry *)src;
471 if (src + sizeof(ext2_acl_entry_short) > end)
473 dst_entry->e_tag = src_entry->e_tag;
474 dst_entry->e_perm = src_entry->e_perm;
475 switch (le16_to_cpu(src_entry->e_tag)) {
480 src += sizeof(ext2_acl_entry_short);
481 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
485 src += sizeof(ext2_acl_entry);
488 dst_entry->e_id = src_entry->e_id;
501 static char *xattr_prefix_table[] = {
503 [2] = "system.posix_acl_access",
504 [3] = "system.posix_acl_default",
509 static int ext2_copy_single_xattr(struct btrfs_trans_handle *trans,
510 struct btrfs_root *root, u64 objectid,
511 struct ext2_ext_attr_entry *entry,
512 const void *data, u32 datalen)
517 void *databuf = NULL;
518 char namebuf[XATTR_NAME_MAX + 1];
520 name_index = entry->e_name_index;
521 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
522 xattr_prefix_table[name_index] == NULL)
524 name_len = strlen(xattr_prefix_table[name_index]) +
526 if (name_len >= sizeof(namebuf))
529 if (name_index == 2 || name_index == 3) {
530 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
531 databuf = malloc(bufsize);
534 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
540 strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
541 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
542 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
543 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
544 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
545 objectid - INO_OFFSET, name_len, namebuf);
548 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
549 data, datalen, objectid);
555 static int ext2_copy_extended_attrs(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root, u64 objectid,
557 struct btrfs_inode_item *btrfs_inode,
558 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
564 u32 block_size = ext2_fs->blocksize;
565 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
566 struct ext2_inode_large *ext2_inode;
567 struct ext2_ext_attr_entry *entry;
570 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
572 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
573 ext2_inode = (struct ext2_inode_large *)inode_buf;
575 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
579 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
582 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
588 if (ext2_ino > ext2_fs->super->s_first_ino &&
589 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
590 if (EXT2_GOOD_OLD_INODE_SIZE +
591 ext2_inode->i_extra_isize > inode_size) {
595 if (ext2_inode->i_extra_isize != 0 &&
596 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
597 EXT2_EXT_ATTR_MAGIC) {
603 void *end = (void *)ext2_inode + inode_size;
604 entry = EXT2_XATTR_IFIRST(ext2_inode);
605 total = end - (void *)entry;
606 ret = ext2_xattr_check_names(entry, end);
609 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
610 ret = ext2_xattr_check_entry(entry, total);
613 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
615 datalen = entry->e_value_size;
616 ret = ext2_copy_single_xattr(trans, root, objectid,
617 entry, data, datalen);
620 entry = EXT2_EXT_ATTR_NEXT(entry);
624 if (ext2_inode->i_file_acl == 0)
627 buffer = malloc(block_size);
632 err = ext2fs_read_ext_attr2(ext2_fs, ext2_inode->i_file_acl, buffer);
634 fprintf(stderr, "ext2fs_read_ext_attr2: %s\n",
639 ret = ext2_xattr_check_block(buffer, block_size);
643 entry = EXT2_XATTR_BFIRST(buffer);
644 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
645 ret = ext2_xattr_check_entry(entry, block_size);
648 data = buffer + entry->e_value_offs;
649 datalen = entry->e_value_size;
650 ret = ext2_copy_single_xattr(trans, root, objectid,
651 entry, data, datalen);
654 entry = EXT2_EXT_ATTR_NEXT(entry);
658 if ((void *)ext2_inode != inode_buf)
663 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
665 static inline dev_t old_decode_dev(u16 val)
667 return MKDEV((val >> 8) & 255, val & 255);
670 static inline dev_t new_decode_dev(u32 dev)
672 unsigned major = (dev & 0xfff00) >> 8;
673 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
674 return MKDEV(major, minor);
677 static void ext2_copy_inode_item(struct btrfs_inode_item *dst,
678 struct ext2_inode *src, u32 blocksize)
680 btrfs_set_stack_inode_generation(dst, 1);
681 btrfs_set_stack_inode_sequence(dst, 0);
682 btrfs_set_stack_inode_transid(dst, 1);
683 btrfs_set_stack_inode_size(dst, src->i_size);
684 btrfs_set_stack_inode_nbytes(dst, 0);
685 btrfs_set_stack_inode_block_group(dst, 0);
686 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
687 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
688 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
689 btrfs_set_stack_inode_mode(dst, src->i_mode);
690 btrfs_set_stack_inode_rdev(dst, 0);
691 btrfs_set_stack_inode_flags(dst, 0);
692 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
693 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
694 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
695 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
696 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
697 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
698 btrfs_set_stack_timespec_sec(&dst->otime, 0);
699 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
701 if (S_ISDIR(src->i_mode)) {
702 btrfs_set_stack_inode_size(dst, 0);
703 btrfs_set_stack_inode_nlink(dst, 1);
705 if (S_ISREG(src->i_mode)) {
706 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
709 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
710 !S_ISLNK(src->i_mode)) {
711 if (src->i_block[0]) {
712 btrfs_set_stack_inode_rdev(dst,
713 old_decode_dev(src->i_block[0]));
715 btrfs_set_stack_inode_rdev(dst,
716 new_decode_dev(src->i_block[1]));
719 memset(&dst->reserved, 0, sizeof(dst->reserved));
721 static int ext2_check_state(struct btrfs_convert_context *cctx)
723 ext2_filsys fs = cctx->fs_data;
725 if (!(fs->super->s_state & EXT2_VALID_FS))
727 else if (fs->super->s_state & EXT2_ERROR_FS)
733 /* EXT2_*_FL to BTRFS_INODE_FLAG_* stringification helper */
734 #define COPY_ONE_EXT2_FLAG(flags, ext2_inode, name) ({ \
735 if (ext2_inode->i_flags & EXT2_##name##_FL) \
736 flags |= BTRFS_INODE_##name; \
740 * Convert EXT2_*_FL to corresponding BTRFS_INODE_* flags
742 * Only a subset of EXT_*_FL is supported in btrfs.
744 static void ext2_convert_inode_flags(struct btrfs_inode_item *dst,
745 struct ext2_inode *src)
747 u64 flags = btrfs_stack_inode_flags(dst);
749 COPY_ONE_EXT2_FLAG(flags, src, APPEND);
750 COPY_ONE_EXT2_FLAG(flags, src, SYNC);
751 COPY_ONE_EXT2_FLAG(flags, src, IMMUTABLE);
752 COPY_ONE_EXT2_FLAG(flags, src, NODUMP);
753 COPY_ONE_EXT2_FLAG(flags, src, NOATIME);
754 COPY_ONE_EXT2_FLAG(flags, src, DIRSYNC);
755 btrfs_set_stack_inode_flags(dst, flags);
759 * copy a single inode. do all the required works, such as cloning
760 * inode item, creating file extents and creating directory entries.
762 static int ext2_copy_single_inode(struct btrfs_trans_handle *trans,
763 struct btrfs_root *root, u64 objectid,
764 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
765 struct ext2_inode *ext2_inode,
769 struct btrfs_inode_item btrfs_inode;
771 if (ext2_inode->i_links_count == 0)
774 ext2_copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
775 if (!(convert_flags & CONVERT_FLAG_DATACSUM)
776 && S_ISREG(ext2_inode->i_mode)) {
777 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
778 BTRFS_INODE_NODATASUM;
779 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
781 ext2_convert_inode_flags(&btrfs_inode, ext2_inode);
783 switch (ext2_inode->i_mode & S_IFMT) {
785 ret = ext2_create_file_extents(trans, root, objectid,
786 &btrfs_inode, ext2_fs, ext2_ino, convert_flags);
789 ret = ext2_create_dir_entries(trans, root, objectid,
790 &btrfs_inode, ext2_fs, ext2_ino);
793 ret = ext2_create_symlink(trans, root, objectid,
794 &btrfs_inode, ext2_fs, ext2_ino, ext2_inode);
803 if (convert_flags & CONVERT_FLAG_XATTR) {
804 ret = ext2_copy_extended_attrs(trans, root, objectid,
805 &btrfs_inode, ext2_fs, ext2_ino);
809 return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
812 static int ext2_is_special_inode(ext2_ino_t ino)
814 if (ino < EXT2_GOOD_OLD_FIRST_INO && ino != EXT2_ROOT_INO)
820 * scan ext2's inode bitmap and copy all used inodes.
822 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
823 struct btrfs_root *root,
824 u32 convert_flags, struct task_ctx *p)
826 ext2_filsys ext2_fs = cctx->fs_data;
829 ext2_inode_scan ext2_scan;
830 struct ext2_inode ext2_inode;
833 struct btrfs_trans_handle *trans;
835 trans = btrfs_start_transaction(root, 1);
838 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
840 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
843 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
848 if (ext2_is_special_inode(ext2_ino))
850 objectid = ext2_ino + INO_OFFSET;
851 ret = ext2_copy_single_inode(trans, root,
852 objectid, ext2_fs, ext2_ino,
853 &ext2_inode, convert_flags);
854 pthread_mutex_lock(&p->mutex);
855 p->cur_copy_inodes++;
856 pthread_mutex_unlock(&p->mutex);
859 if (trans->blocks_used >= 4096) {
860 ret = btrfs_commit_transaction(trans, root);
862 trans = btrfs_start_transaction(root, 1);
867 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
870 ret = btrfs_commit_transaction(trans, root);
872 ext2fs_close_inode_scan(ext2_scan);
877 const struct btrfs_convert_operations ext2_convert_ops = {
879 .open_fs = ext2_open_fs,
880 .read_used_space = ext2_read_used_space,
881 .copy_inodes = ext2_copy_inodes,
882 .close_fs = ext2_close_fs,
883 .check_state = ext2_check_state,
886 #endif /* BTRFSCONVERT_EXT2 */