btrfs-progs: check/original: Don't overwrite return value when we failed to repair
[btrfs-progs-unstable/devel.git] / convert / source-reiserfs.c
blobe0b3b685f764b4a39f04ec8a82e9082f30da05dd
1 /*
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.
17 #if BTRFSCONVERT_REISERFS
19 #include "kerncompat.h"
20 #include <linux/limits.h>
21 #include <linux/fs.h>
22 #include <limits.h>
23 #include <sys/stat.h>
24 #include <stdbool.h>
25 #include "disk-io.h"
26 #include "transaction.h"
27 #include "utils.h"
28 #include "bitops.h"
29 #include "convert/common.h"
30 #include "convert/source-reiserfs.h"
32 static inline u8 mode_to_file_type(u32 mode)
34 switch (mode & S_IFMT) {
35 case S_IFREG: return BTRFS_FT_REG_FILE;
36 case S_IFDIR: return BTRFS_FT_DIR;
37 case S_IFCHR: return BTRFS_FT_CHRDEV;
38 case S_IFBLK: return BTRFS_FT_BLKDEV;
39 case S_IFIFO: return BTRFS_FT_FIFO;
40 case S_IFSOCK: return BTRFS_FT_SOCK;
41 case S_IFLNK: return BTRFS_FT_SYMLINK;
44 return BTRFS_FT_UNKNOWN;
47 static u32 reiserfs_count_objectids(reiserfs_filsys_t fs)
49 struct reiserfs_super_block *sb = fs->fs_ondisk_sb;
50 u32 count = 0;
51 u32 *map;
52 int i;
54 if (fs->fs_format == REISERFS_FORMAT_3_6)
55 map = (u32 *) (sb + 1);
56 else
57 map = (u32 *)((struct reiserfs_super_block_v1 *)sb + 1);
59 for (i = 0; i < get_sb_oid_cursize(sb); i += 2)
60 count += le32_to_cpu(map[i + 1]) - (le32_to_cpu(map[i]) + 1);
62 return count;
66 static int reiserfs_open_fs(struct btrfs_convert_context *cxt, const char *name)
68 struct reiserfs_convert_info *info;
69 reiserfs_filsys_t fs;
70 long error;
72 fs = reiserfs_open(name, O_RDONLY, &error, NULL, 0);
73 if (!fs)
74 return -1;
76 error = reiserfs_open_ondisk_bitmap(fs);
77 if (error) {
78 reiserfs_close(fs);
79 return -1;
82 cxt->fs_data = fs;
83 cxt->blocksize = fs->fs_blocksize;
84 cxt->block_count = get_sb_block_count(fs->fs_ondisk_sb);
85 cxt->total_bytes = cxt->blocksize * cxt->block_count;
86 cxt->volume_name = strndup(fs->fs_ondisk_sb->s_label, 16);
87 cxt->first_data_block = 0;
88 cxt->inodes_count = reiserfs_count_objectids(fs);
89 cxt->free_inodes_count = 0;
90 info = calloc(1, sizeof(*info));
91 if (!info) {
92 reiserfs_close(fs);
93 return -1;
97 * Inode attributes are somewhat of a hack on reiserfs and it was
98 * once possible to have garbage in the flags field. A superblock
99 * field now indicates that the field has been cleared and can
100 * be considered valid, but only on v3.6 format file systems.
102 if (fs->fs_format == REISERFS_FORMAT_3_6 &&
103 get_sb_v2_flag(fs->fs_ondisk_sb, reiserfs_attrs_cleared))
104 info->copy_attrs = true;
106 fs->fs_vp = info;
107 return 0;
110 static void reiserfs_close_fs(struct btrfs_convert_context *cxt)
112 reiserfs_filsys_t fs = cxt->fs_data;
113 struct reiserfs_convert_info *info = fs->fs_vp;
115 if (info) {
116 if (info->objectids)
117 free(info->objectids);
118 free(info);
119 fs->fs_vp = NULL;
122 /* We don't want changes to be persistent */
123 fs->fs_bitmap2->bm_dirty = 0;
125 reiserfs_close(fs);
128 static int compare_objectids(const void *p1, const void *p2)
130 u64 v1 = *(u64 *)p1;
131 u64 v2 = *(u64 *)p2;
133 if (v1 > v2)
134 return 1;
135 else if (v1 < v2)
136 return -1;
137 return 0;
140 static int lookup_cached_objectid(reiserfs_filsys_t fs, u64 objectid)
142 struct reiserfs_convert_info *info = fs->fs_vp;
143 u64 *result;
145 if (!info->objectids)
146 return 0;
147 result = bsearch(&objectid, info->objectids, info->used_slots,
148 sizeof(u64), compare_objectids);
149 return result != NULL;
152 static int insert_cached_objectid(reiserfs_filsys_t fs, u64 objectid)
154 struct reiserfs_convert_info *info = fs->fs_vp;
156 if (info->used_slots + 1 >= info->alloced_slots) {
157 u64 *objectids = realloc(info->objectids,
158 (info->alloced_slots + 1000) * sizeof(u64));
160 if (!objectids)
161 return -ENOMEM;
162 info->objectids = objectids;
163 info->alloced_slots += 1000;
165 info->objectids[info->used_slots++] = objectid;
167 qsort(info->objectids, info->used_slots, sizeof(u64), compare_objectids);
168 return 0;
171 static int reiserfs_locate_privroot(reiserfs_filsys_t fs)
173 int err;
174 unsigned generation;
175 struct reiserfs_convert_info *info = fs->fs_vp;
176 struct reiserfs_key key = root_dir_key;
178 err = reiserfs_find_entry(fs, &key, ".reiserfs_priv",
179 &generation, &info->privroot_key);
180 if (err == 1) {
181 err = reiserfs_find_entry(fs, &info->privroot_key, "xattrs",
182 &generation, &info->xattr_key);
183 if (err != 1)
184 memset(&info->xattr_key, 0, sizeof(info->xattr_key));
187 return 0;
190 static void reiserfs_convert_inode_flags(struct btrfs_inode_item *inode,
191 const struct stat_data *sd)
193 u16 attrs = sd_v2_sd_attrs(sd);
194 u64 new_flags = 0;
196 if (attrs & FS_IMMUTABLE_FL)
197 new_flags |= BTRFS_INODE_IMMUTABLE;
199 if (attrs & FS_APPEND_FL)
200 new_flags |= BTRFS_INODE_APPEND;
202 if (attrs & FS_SYNC_FL)
203 new_flags |= BTRFS_INODE_SYNC;
205 if (attrs & FS_NOATIME_FL)
206 new_flags |= BTRFS_INODE_NOATIME;
208 if (attrs & FS_NODUMP_FL)
209 new_flags |= BTRFS_INODE_NODUMP;
211 if (attrs & FS_NODUMP_FL)
212 new_flags |= BTRFS_INODE_NODUMP;
214 btrfs_set_stack_inode_flags(inode, new_flags);
218 static void reiserfs_copy_inode_item(struct btrfs_inode_item *inode,
219 struct item_head *ih, void *stat_data,
220 bool copy_inode_flags)
222 u32 mode;
223 u32 rdev = 0;
225 memset(inode, 0, sizeof(*inode));
226 btrfs_set_stack_inode_generation(inode, 1);
227 if (get_ih_key_format(ih) == KEY_FORMAT_1) {
228 struct stat_data_v1 *sd = stat_data;
230 mode = sd_v1_mode(sd);
231 btrfs_set_stack_inode_size(inode, sd_v1_size(sd));
232 btrfs_set_stack_inode_nlink(inode, sd_v1_nlink(sd));
233 btrfs_set_stack_inode_uid(inode, sd_v1_uid(sd));
234 btrfs_set_stack_inode_gid(inode, sd_v1_gid(sd));
235 btrfs_set_stack_timespec_sec(&inode->atime, sd_v1_atime(sd));
236 btrfs_set_stack_timespec_sec(&inode->ctime, sd_v1_ctime(sd));
237 btrfs_set_stack_timespec_sec(&inode->mtime, sd_v1_mtime(sd));
239 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
240 rdev = decode_dev(sd_v1_rdev(sd));
241 } else {
242 struct stat_data *sd = stat_data;
244 mode = sd_v2_mode(sd);
245 btrfs_set_stack_inode_size(inode, sd_v2_size(sd));
246 btrfs_set_stack_inode_nlink(inode, sd_v2_nlink(sd));
247 btrfs_set_stack_inode_uid(inode, sd_v2_uid(sd));
248 btrfs_set_stack_inode_gid(inode, sd_v2_gid(sd));
249 btrfs_set_stack_timespec_sec(&inode->atime, sd_v2_atime(sd));
250 btrfs_set_stack_timespec_sec(&inode->ctime, sd_v2_ctime(sd));
251 btrfs_set_stack_timespec_sec(&inode->mtime, sd_v2_mtime(sd));
253 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
254 rdev = decode_dev(sd_v2_rdev(sd));
256 if (copy_inode_flags)
257 reiserfs_convert_inode_flags(inode, sd);
260 if (S_ISDIR(mode)) {
261 btrfs_set_stack_inode_size(inode, 0);
262 btrfs_set_stack_inode_nlink(inode, 1);
264 btrfs_set_stack_inode_mode(inode, mode);
265 btrfs_set_stack_inode_rdev(inode, rdev);
268 static void init_reiserfs_blk_iterate_data(
269 struct reiserfs_blk_iterate_data *data,
270 struct btrfs_trans_handle *trans,
271 struct btrfs_root *root,
272 struct btrfs_inode_item *inode,
273 u64 objectid, u32 convert_flags)
275 init_blk_iterate_data(&data->blk_data, trans, root, inode, objectid,
276 convert_flags & CONVERT_FLAG_DATACSUM);
277 data->inline_data = NULL;
278 data->inline_offset = (u64)-1;
279 data->inline_length = 0;
282 static int reiserfs_record_indirect_extent(reiserfs_filsys_t fs, u64 position,
283 u64 size, int num_ptrs,
284 u32 *ptrs, void *data)
286 struct reiserfs_blk_iterate_data *bdata = data;
287 u32 file_block = position / fs->fs_blocksize;
288 int i;
289 int ret = 0;
291 for (i = 0; i < num_ptrs; i++, file_block++) {
292 u32 block = d32_get(ptrs, i);
294 ret = block_iterate_proc(block, file_block, &bdata->blk_data);
295 if (ret)
296 break;
299 return ret;
303 * Unlike btrfs inline extents, reiserfs can have multiple inline extents.
304 * This handles concatanating multiple tails into one inline extent
305 * for insertion.
307 static int reiserfs_record_direct_extent(reiserfs_filsys_t fs, __u64 position,
308 __u64 size, const char *body,
309 size_t len, void *data)
311 struct reiserfs_blk_iterate_data *bdata = data;
312 char *inline_data;
314 if (bdata->inline_offset == (u64)-1)
315 bdata->inline_offset = position;
316 else if (bdata->inline_offset + bdata->inline_length != position) {
318 * This condition shouldn't actually happen, but better to
319 * catch it than break silently.
321 error(
322 "source fs contains file with multiple tails but they are not contiguous");
323 return -EINVAL;
326 inline_data = realloc(bdata->inline_data, bdata->inline_length + len);
327 if (!inline_data)
328 return -ENOMEM;
330 bdata->inline_data = inline_data;
331 memcpy(bdata->inline_data + bdata->inline_length, body, len);
332 bdata->inline_length += len;
334 return 0;
337 static int convert_direct(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root, u64 objectid,
339 struct btrfs_inode_item *inode, const char *body,
340 u32 length, u64 offset, u32 convert_flags)
342 struct btrfs_key key;
343 u32 sectorsize = root->fs_info->sectorsize;
344 int ret;
345 struct extent_buffer *eb;
347 BUG_ON(length > sectorsize);
348 ret = btrfs_reserve_extent(trans, root, sectorsize,
349 0, 0, -1ULL, &key, 1);
350 if (ret)
351 return ret;
353 eb = alloc_extent_buffer(root->fs_info, key.objectid, sectorsize);
355 if (!eb)
356 return -ENOMEM;
358 write_extent_buffer(eb, body, 0, length);
359 ret = write_and_map_eb(root->fs_info, eb);
360 free_extent_buffer(eb);
361 if (ret)
362 return ret;
364 return btrfs_record_file_extent(trans, root, objectid, inode, offset,
365 key.objectid, sectorsize);
368 static int reiserfs_convert_tail(struct btrfs_trans_handle *trans,
369 struct btrfs_root *root,
370 struct btrfs_inode_item *inode,
371 u64 objectid, u64 offset,
372 const void *body, unsigned length,
373 u32 convert_flags)
375 u64 isize;
376 int ret;
378 if (length >= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info) ||
379 length >= root->fs_info->sectorsize)
380 return convert_direct(trans, root, objectid, inode, body,
381 length, offset, convert_flags);
383 ret = btrfs_insert_inline_extent(trans, root, objectid,
384 offset, body, length);
385 if (ret)
386 return ret;
388 isize = btrfs_stack_inode_nbytes(inode);
389 btrfs_set_stack_inode_nbytes(inode, isize + length);
391 return 0;
394 static inline u32 block_count(u64 size, u32 blocksize)
396 return round_up(size, blocksize) / blocksize;
399 static int reiserfs_record_file_extents(reiserfs_filsys_t fs,
400 struct btrfs_trans_handle *trans,
401 struct btrfs_root *root,
402 u64 objectid,
403 struct btrfs_inode_item *inode,
404 struct reiserfs_key *sd_key,
405 u32 convert_flags)
408 int ret;
409 u32 blocksize = fs->fs_blocksize;
410 u64 inode_size = btrfs_stack_inode_size(inode);
411 u32 last_block;
412 struct reiserfs_blk_iterate_data data;
414 init_reiserfs_blk_iterate_data(&data, trans, root, inode,
415 objectid, convert_flags);
417 ret = reiserfs_iterate_file_data(fs, sd_key,
418 reiserfs_record_indirect_extent,
419 reiserfs_record_direct_extent, &data);
420 if (ret)
421 return ret;
424 * blk_iterate_block has no idea that we're done iterating, so record
425 * the final range if any. This range can end and still have a tail
426 * after it.
428 if (data.blk_data.num_blocks) {
429 ret = record_file_blocks(&data.blk_data,
430 data.blk_data.first_block,
431 data.blk_data.disk_block,
432 data.blk_data.num_blocks);
433 if (ret)
434 goto fail;
435 data.blk_data.first_block += data.blk_data.num_blocks;
436 data.blk_data.num_blocks = 0;
440 * Handle a hole at the end of the file. ReiserFS will
441 * not write a tail followed by a hole but it will write a hole
442 * followed by a tail.
444 last_block = block_count(inode_size - data.inline_length, blocksize);
445 if (last_block > data.blk_data.first_block) {
446 ret = record_file_blocks(&data.blk_data,
447 data.blk_data.first_block, 0,
448 last_block - data.blk_data.first_block);
449 if (ret)
450 goto fail;
453 if (data.inline_length) {
454 ret = reiserfs_convert_tail(trans, root, inode, objectid,
455 data.inline_offset,
456 data.inline_data,
457 data.inline_length, convert_flags);
458 if (ret)
459 goto fail;
462 ret = 0;
463 fail:
464 return ret;
467 static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
468 u32 convert_flags, u32 deh_dirid,
469 u32 deh_objectid, u8 *type);
471 static int reiserfs_copy_dirent(reiserfs_filsys_t fs,
472 const struct reiserfs_key *dir_short_key,
473 const char *name, size_t len,
474 __u32 deh_dirid, __u32 deh_objectid,
475 void *cb_data)
477 int ret;
478 u8 type;
479 struct btrfs_trans_handle *trans;
480 u64 objectid = deh_objectid + OID_OFFSET;
481 struct reiserfs_convert_info *info = fs->fs_vp;
482 struct reiserfs_dirent_data *dirent_data = cb_data;
483 struct btrfs_root *root = dirent_data->root;
484 __u32 dir_objectid = get_key_objectid(dir_short_key) + OID_OFFSET;
487 * These are the extended attributes and shouldn't appear as files
488 * in the converted file systems.
490 if (deh_objectid == get_key_objectid(&info->privroot_key))
491 return 0;
493 ret = reiserfs_copy_meta(fs, root, dirent_data->convert_flags,
494 deh_dirid, deh_objectid, &type);
495 if (ret) {
496 error(
497 "an error occured while converting \"%.*s\", reiserfs key [%u %u]: %s",
498 (int)len, name, deh_dirid, deh_objectid,
499 strerror(-ret));
500 return ret;
502 trans = btrfs_start_transaction(root, 1);
503 if (IS_ERR(trans))
504 return PTR_ERR(trans);
506 ret = convert_insert_dirent(trans, root, name, len, dir_objectid,
507 objectid, type, dirent_data->index++,
508 dirent_data->inode);
509 return btrfs_commit_transaction(trans, root);
512 static int reiserfs_copy_symlink(struct btrfs_trans_handle *trans,
513 struct btrfs_root *root, u64 objectid,
514 struct btrfs_inode_item *btrfs_inode,
515 reiserfs_filsys_t fs,
516 struct reiserfs_path *sd_path)
518 INITIALIZE_REISERFS_PATH(path);
519 struct item_head *ih = tp_item_head(sd_path);
520 struct reiserfs_key key = ih->ih_key;
521 int ret;
522 char *symlink;
523 int len;
525 set_key_uniqueness(&key, type2uniqueness(TYPE_DIRECT));
526 set_key_offset_v1(&key, 1);
528 ret = reiserfs_search_by_key_3(fs, &key, &path);
529 if (ret != ITEM_FOUND) {
530 ret = -ENOENT;
531 goto fail;
534 symlink = tp_item_body(&path);
535 len = get_ih_item_len(tp_item_head(&path));
537 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
538 symlink, len + 1);
539 btrfs_set_stack_inode_nbytes(btrfs_inode, len + 1);
540 fail:
541 pathrelse(&path);
542 return ret;
545 static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
546 u32 convert_flags, u32 deh_dirid,
547 u32 deh_objectid, u8 *type)
549 INITIALIZE_REISERFS_PATH(path);
550 int ret = 0;
551 struct item_head *ih;
552 struct reiserfs_key key;
553 struct btrfs_inode_item btrfs_inode;
554 struct btrfs_trans_handle *trans = NULL;
555 struct reiserfs_convert_info *info = fs->fs_vp;
556 u32 mode;
557 u64 objectid = deh_objectid + OID_OFFSET;
558 u64 parent = deh_dirid + OID_OFFSET;
559 struct reiserfs_dirent_data dirent_data = {
560 .index = 2,
561 .convert_flags = convert_flags,
562 .inode = &btrfs_inode,
563 .root = root,
566 /* The root directory's dirid in reiserfs points to an object
567 * that does't exist. In btrfs it's self-referential.
569 if (deh_dirid == REISERFS_ROOT_PARENT_OBJECTID)
570 parent = objectid;
572 set_key_dirid(&key, deh_dirid);
573 set_key_objectid(&key, deh_objectid);
574 set_key_offset_v2(&key, 0);
575 set_key_type_v2(&key, TYPE_STAT_DATA);
577 ret = reiserfs_search_by_key_3(fs, &key, &path);
578 if (ret != ITEM_FOUND) {
579 ret = -ENOENT;
580 goto fail;
583 ih = tp_item_head(&path);
584 if (!is_stat_data_ih(ih)) {
585 ret = -EINVAL;
586 goto fail;
589 reiserfs_copy_inode_item(&btrfs_inode, ih, tp_item_body(&path),
590 info->copy_attrs);
591 mode = btrfs_stack_inode_mode(&btrfs_inode);
592 *type = mode_to_file_type(mode);
594 if (S_ISREG(mode)) {
595 /* Inodes with hardlinks should only be inserted once */
596 if (btrfs_stack_inode_nlink(&btrfs_inode) > 1) {
597 if (lookup_cached_objectid(fs, deh_objectid)) {
598 ret = 0;
599 goto fail; /* Not a failure */
601 ret = insert_cached_objectid(fs, deh_objectid);
602 if (ret)
603 goto fail;
607 if (!(convert_flags & CONVERT_FLAG_DATACSUM)) {
608 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
609 BTRFS_INODE_NODATASUM;
610 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
613 switch (mode & S_IFMT) {
614 case S_IFREG:
615 trans = btrfs_start_transaction(root, 1);
616 if (IS_ERR(trans)) {
617 ret = PTR_ERR(trans);
618 goto fail;
620 ret = reiserfs_record_file_extents(fs, trans, root, objectid,
621 &btrfs_inode, &ih->ih_key,
622 convert_flags);
623 if (ret)
624 goto fail;
625 break;
626 case S_IFDIR:
627 ret = reiserfs_iterate_dir(fs, &ih->ih_key,
628 reiserfs_copy_dirent, &dirent_data);
629 if (ret)
630 goto fail;
631 trans = btrfs_start_transaction(root, 1);
632 if (IS_ERR(trans)) {
633 ret = PTR_ERR(trans);
634 goto fail;
637 ret = btrfs_insert_inode_ref(trans, root, "..", 2, parent,
638 objectid, 0);
639 break;
640 case S_IFLNK:
641 trans = btrfs_start_transaction(root, 1);
642 if (IS_ERR(trans)) {
643 ret = PTR_ERR(trans);
644 goto fail;
646 ret = reiserfs_copy_symlink(trans, root, objectid,
647 &btrfs_inode, fs, &path);
648 if (ret)
649 goto fail;
650 break;
651 default:
652 trans = btrfs_start_transaction(root, 1);
653 if (IS_ERR(trans)) {
654 ret = PTR_ERR(trans);
655 goto fail;
659 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
660 if (ret)
661 goto fail;
662 ret = btrfs_commit_transaction(trans, root);
663 info->progress->cur_copy_inodes++;
665 fail:
666 pathrelse(&path);
667 return ret;
670 static int reiserfs_xattr_indirect_fn(reiserfs_filsys_t fs, u64 position,
671 u64 size, int num_blocks,
672 u32 *blocks, void *data)
674 int i;
675 struct reiserfs_xattr_data *xa_data = data;
676 size_t alloc = min(position + num_blocks * fs->fs_blocksize, size);
677 char *body;
679 if (size > BTRFS_LEAF_DATA_SIZE(xa_data->root->fs_info) -
680 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
681 fprintf(stderr, "skip large xattr on objectid %llu name %.*s\n",
682 xa_data->target_oid, (int)xa_data->namelen,
683 xa_data->name);
684 return -E2BIG;
687 body = realloc(xa_data->body, alloc);
688 if (!body)
689 return -ENOMEM;
691 xa_data->body = body;
692 xa_data->len = alloc;
694 for (i = 0; i < num_blocks; i++) {
695 int ret;
696 u32 block = d32_get(blocks, i);
697 u64 offset = (u64)block * fs->fs_blocksize;
698 size_t chunk = min_t(u64, size - position, fs->fs_blocksize);
699 char *buffer = xa_data->body + position;
701 ret = read_disk_extent(xa_data->root, offset, chunk, buffer);
702 if (ret)
703 return ret;
704 position += chunk;
707 return 0;
710 static int reiserfs_xattr_direct_fn(reiserfs_filsys_t fs, __u64 position,
711 __u64 size, const char *body, size_t len,
712 void *data)
714 struct reiserfs_xattr_data *xa_data = data;
715 char *newbody;
717 if (size > BTRFS_LEAF_DATA_SIZE(xa_data->root->fs_info) -
718 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
719 fprintf(stderr, "skip large xattr on objectid %llu name %.*s\n",
720 xa_data->target_oid, (int)xa_data->namelen,
721 xa_data->name);
722 return -E2BIG;
725 newbody = realloc(xa_data->body, position + len);
726 if (!newbody)
727 return -ENOMEM;
728 xa_data->body = newbody;
729 xa_data->len = position + len;
730 memcpy(xa_data->body + position, body, len);
731 return 0;
734 static int reiserfs_acl_to_xattr(void *dst, const void *src,
735 size_t dst_size, size_t src_size)
737 int i, count;
738 const void *end = src + src_size;
739 acl_ea_header *ext_acl = (acl_ea_header *)dst;
740 acl_ea_entry *dst_entry = ext_acl->a_entries;
741 struct reiserfs_acl_entry *src_entry;
743 if (src_size < sizeof(struct reiserfs_acl_header))
744 goto fail;
745 if (((struct reiserfs_acl_header *)src)->a_version !=
746 cpu_to_le32(REISERFS_ACL_VERSION))
747 goto fail;
748 src += sizeof(struct reiserfs_acl_header);
749 count = reiserfs_acl_count(src_size);
750 if (count <= 0)
751 goto fail;
753 BUG_ON(dst_size < acl_ea_size(count));
754 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
755 for (i = 0; i < count; i++, dst_entry++) {
756 src_entry = (struct reiserfs_acl_entry *)src;
757 if (src + sizeof(struct reiserfs_acl_entry_short) > end)
758 goto fail;
759 dst_entry->e_tag = src_entry->e_tag;
760 dst_entry->e_perm = src_entry->e_perm;
761 switch (le16_to_cpu(src_entry->e_tag)) {
762 case ACL_USER_OBJ:
763 case ACL_GROUP_OBJ:
764 case ACL_MASK:
765 case ACL_OTHER:
766 src += sizeof(struct reiserfs_acl_entry_short);
767 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
768 break;
769 case ACL_USER:
770 case ACL_GROUP:
771 src += sizeof(struct reiserfs_acl_entry);
772 if (src > end)
773 goto fail;
774 dst_entry->e_id = src_entry->e_id;
775 break;
776 default:
777 goto fail;
780 if (src != end)
781 goto fail;
782 return 0;
783 fail:
784 return -EINVAL;
787 static int reiserfs_copy_one_xattr(reiserfs_filsys_t fs,
788 const struct reiserfs_key *dir_short_key,
789 const char *name, size_t namelen,
790 __u32 deh_dirid,
791 __u32 deh_objectid, void *cb_data)
793 struct reiserfs_convert_info *info = fs->fs_vp;
794 struct reiserfs_xattr_data *xa_data = cb_data;
795 struct reiserfs_key key = {
796 .k2_dir_id = deh_dirid,
797 .k2_objectid = deh_objectid,
799 void *body = NULL;
800 int len;
801 int ret;
803 xa_data->name = name;
804 xa_data->namelen = namelen;
806 ret = reiserfs_iterate_file_data(fs, &key, reiserfs_xattr_indirect_fn,
807 reiserfs_xattr_direct_fn, cb_data);
808 if (ret)
809 goto out;
811 if (!reiserfs_check_xattr(xa_data->body, xa_data->len)) {
812 fprintf(stderr,
813 "skip corrupted xattr on objectid %u name %.*s\n",
814 deh_objectid, (int)xa_data->namelen,
815 xa_data->name);
816 goto out;
819 body = xa_data->body + sizeof(struct reiserfs_xattr_header);
820 len = xa_data->len - sizeof(struct reiserfs_xattr_header);
822 if (!strncmp("system.posix_acl_default", name, namelen) ||
823 !strncmp("system.posix_acl_access", name, namelen)) {
824 size_t bufsize = acl_ea_size(ext2_acl_count(len));
825 char *databuf = malloc(bufsize);
827 if (!databuf)
828 goto out;
829 ret = reiserfs_acl_to_xattr(databuf, body, bufsize, len);
830 if (ret)
831 goto out;
832 body = databuf;
833 len = bufsize;
836 ret = btrfs_insert_xattr_item(xa_data->trans, xa_data->root,
837 name, namelen, body, len,
838 xa_data->target_oid);
840 info->progress->cur_copy_inodes++;
841 out:
842 if (body &&
843 body != xa_data->body + sizeof(struct reiserfs_xattr_header))
844 free(body);
845 if (xa_data->body)
846 free(xa_data->body);
847 xa_data->body = NULL;
848 xa_data->len = 0;
850 return ret;
853 static int reiserfs_copy_xattr_dir(reiserfs_filsys_t fs,
854 const struct reiserfs_key *dir_short_key,
855 const char *name, size_t len,
856 __u32 deh_dirid, __u32 deh_objectid,
857 void *cb_data)
859 struct reiserfs_convert_info *info = fs->fs_vp;
860 struct reiserfs_xattr_data *xa_data = cb_data;
861 struct reiserfs_key dir_key = {
862 .k2_dir_id = deh_dirid,
863 .k2_objectid = deh_objectid,
865 int ret, err;
867 errno = 0;
868 xa_data->target_oid = strtoull(name, NULL, 16);
869 if (xa_data->target_oid == ULLONG_MAX && errno)
870 return -errno;
872 xa_data->target_oid += OID_OFFSET;
874 xa_data->trans = btrfs_start_transaction(xa_data->root, 1);
875 if (IS_ERR(xa_data->trans))
876 return PTR_ERR(xa_data->trans);
878 ret = reiserfs_iterate_dir(fs, &dir_key,
879 reiserfs_copy_one_xattr, xa_data);
881 err = btrfs_commit_transaction(xa_data->trans, xa_data->root);
882 info->progress->cur_copy_inodes++;
883 xa_data->trans = NULL;
884 return ret ?: err;
887 static int reiserfs_copy_xattrs(reiserfs_filsys_t fs, struct btrfs_root *root)
889 struct reiserfs_convert_info *info = fs->fs_vp;
890 struct reiserfs_xattr_data data = {
891 .root = root,
894 if (get_key_objectid(&info->xattr_key) == 0)
895 return 0;
897 return reiserfs_iterate_dir(fs, &info->xattr_key,
898 reiserfs_copy_xattr_dir, &data);
901 static int reiserfs_copy_inodes(struct btrfs_convert_context *cxt,
902 struct btrfs_root *root,
903 u32 convert_flags,
904 struct task_ctx *p)
906 reiserfs_filsys_t fs = cxt->fs_data;
907 struct reiserfs_convert_info *info = fs->fs_vp;
908 int ret;
909 u8 type;
911 info->progress = p;
913 ret = reiserfs_locate_privroot(fs);
914 if (ret)
915 goto out;
917 ret = reiserfs_copy_meta(fs, root, convert_flags,
918 REISERFS_ROOT_PARENT_OBJECTID,
919 REISERFS_ROOT_OBJECTID, &type);
920 if (ret)
921 goto out;
923 if (convert_flags & CONVERT_FLAG_XATTR)
924 ret = reiserfs_copy_xattrs(fs, root);
926 out:
927 info->progress = NULL;
928 return ret;
931 static int reiserfs_read_used_space(struct btrfs_convert_context *cxt)
933 reiserfs_filsys_t fs = cxt->fs_data;
934 u64 start, end = 0;
935 unsigned int size = get_sb_block_count(fs->fs_ondisk_sb);
936 unsigned long *bitmap = (unsigned long *)fs->fs_bitmap2->bm_map;
937 int ret = 0;
940 * We have the entire bitmap loaded so we can just ping pong with
941 * ffz and ffs
943 while (end < size) {
944 u64 offset, length;
946 start = find_next_bit(bitmap, size, end);
947 if (start >= size)
948 break;
949 end = find_next_zero_bit(bitmap, size, start);
950 if (end > size)
951 end = size;
952 offset = start * fs->fs_blocksize;
953 length = (end - start) * fs->fs_blocksize;
954 ret = add_merge_cache_extent(&cxt->used_space, offset, length);
955 if (ret < 0)
956 break;
959 return ret;
962 static int reiserfs_check_state(struct btrfs_convert_context *cxt)
964 return 0;
967 const struct btrfs_convert_operations reiserfs_convert_ops = {
968 .name = "reiserfs",
969 .open_fs = reiserfs_open_fs,
970 .read_used_space = reiserfs_read_used_space,
971 .copy_inodes = reiserfs_copy_inodes,
972 .close_fs = reiserfs_close_fs,
973 .check_state = reiserfs_check_state,
976 #endif /* BTRFSCONVERT_REISERFS */