btrfs-progs: build: Do not use cp -a to install library links
[platform/upstream/btrfs-progs.git] / convert / source-reiserfs.c
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.
5  *
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.
10  *
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.
15  */
16
17 #if BTRFSCONVERT_REISERFS
18
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"
31
32 static inline u8 mode_to_file_type(u32 mode)
33 {
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;
42         };
43
44         return BTRFS_FT_UNKNOWN;
45 }
46
47 static u32 reiserfs_count_objectids(reiserfs_filsys_t fs)
48 {
49         struct reiserfs_super_block *sb = fs->fs_ondisk_sb;
50         u32 count = 0;
51         u32 *map;
52         int i;
53
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);
58
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);
61
62         return count;
63 }
64
65
66 static int reiserfs_open_fs(struct btrfs_convert_context *cxt, const char *name)
67 {
68         struct reiserfs_convert_info *info;
69         reiserfs_filsys_t fs;
70         long error;
71
72         fs = reiserfs_open(name, O_RDONLY, &error, NULL, 0);
73         if (!fs)
74                 return -1;
75
76         error = reiserfs_open_ondisk_bitmap(fs);
77         if (error) {
78                 reiserfs_close(fs);
79                 return -1;
80         }
81
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;
94         }
95
96         /*
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.
101          */
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;
105
106         fs->fs_vp = info;
107         return 0;
108 }
109
110 static void reiserfs_close_fs(struct btrfs_convert_context *cxt)
111 {
112         reiserfs_filsys_t fs = cxt->fs_data;
113         struct reiserfs_convert_info *info = fs->fs_vp;
114
115         if (info) {
116                 if (info->objectids)
117                         free(info->objectids);
118                 free(info);
119                 fs->fs_vp = NULL;
120         }
121
122         /* We don't want changes to be persistent */
123         fs->fs_bitmap2->bm_dirty = 0;
124
125         reiserfs_close(fs);
126 }
127
128 static int compare_objectids(const void *p1, const void *p2)
129 {
130         u64 v1 = *(u64 *)p1;
131         u64 v2 = *(u64 *)p2;
132
133         if (v1 > v2)
134                 return 1;
135         else if (v1 < v2)
136                 return -1;
137         return 0;
138 }
139
140 static int lookup_cached_objectid(reiserfs_filsys_t fs, u64 objectid)
141 {
142         struct reiserfs_convert_info *info = fs->fs_vp;
143         u64 *result;
144
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;
150 }
151
152 static int insert_cached_objectid(reiserfs_filsys_t fs, u64 objectid)
153 {
154         struct reiserfs_convert_info *info = fs->fs_vp;
155
156         if (info->used_slots + 1 >= info->alloced_slots) {
157                 u64 *objectids = realloc(info->objectids,
158                                     (info->alloced_slots + 1000) * sizeof(u64));
159
160                 if (!objectids)
161                         return -ENOMEM;
162                 info->objectids = objectids;
163                 info->alloced_slots += 1000;
164         }
165         info->objectids[info->used_slots++] = objectid;
166
167         qsort(info->objectids, info->used_slots, sizeof(u64), compare_objectids);
168         return 0;
169 }
170
171 static int reiserfs_locate_privroot(reiserfs_filsys_t fs)
172 {
173         int err;
174         unsigned generation;
175         struct reiserfs_convert_info *info = fs->fs_vp;
176         struct reiserfs_key key = root_dir_key;
177
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));
185         }
186
187         return 0;
188 }
189
190 static void reiserfs_convert_inode_flags(struct btrfs_inode_item *inode,
191                                          const struct stat_data *sd)
192 {
193         u16 attrs = sd_v2_sd_attrs(sd);
194         u64 new_flags = 0;
195
196         if (attrs & FS_IMMUTABLE_FL)
197                 new_flags |= BTRFS_INODE_IMMUTABLE;
198
199         if (attrs & FS_APPEND_FL)
200                 new_flags |= BTRFS_INODE_APPEND;
201
202         if (attrs & FS_SYNC_FL)
203                 new_flags |= BTRFS_INODE_SYNC;
204
205         if (attrs & FS_NOATIME_FL)
206                 new_flags |= BTRFS_INODE_NOATIME;
207
208         if (attrs & FS_NODUMP_FL)
209                 new_flags |= BTRFS_INODE_NODUMP;
210
211         if (attrs & FS_NODUMP_FL)
212                 new_flags |= BTRFS_INODE_NODUMP;
213
214         btrfs_set_stack_inode_flags(inode, new_flags);
215
216 }
217
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)
221 {
222         u32 mode;
223         u32 rdev = 0;
224
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;
229
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));
238
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;
243
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));
252
253                 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
254                         rdev = decode_dev(sd_v2_rdev(sd));
255
256                 if (copy_inode_flags)
257                         reiserfs_convert_inode_flags(inode, sd);
258
259         }
260         if (S_ISDIR(mode)) {
261                 btrfs_set_stack_inode_size(inode, 0);
262                 btrfs_set_stack_inode_nlink(inode, 1);
263         }
264         btrfs_set_stack_inode_mode(inode, mode);
265         btrfs_set_stack_inode_rdev(inode, rdev);
266 }
267
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)
274 {
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;
280 }
281
282 static int reiserfs_record_indirect_extent(reiserfs_filsys_t fs, u64 position,
283                                            u64 size, int num_ptrs,
284                                            u32 *ptrs, void *data)
285 {
286         struct reiserfs_blk_iterate_data *bdata = data;
287         u32 file_block = position / fs->fs_blocksize;
288         int i;
289         int ret = 0;
290
291         for (i = 0; i < num_ptrs; i++, file_block++) {
292                 u32 block = d32_get(ptrs, i);
293
294                 ret = block_iterate_proc(block, file_block, &bdata->blk_data);
295                 if (ret)
296                         break;
297         }
298
299         return ret;
300 }
301
302 /*
303  * Unlike btrfs inline extents, reiserfs can have multiple inline extents.
304  * This handles concatanating multiple tails into one inline extent
305  * for insertion.
306  */
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)
310 {
311         struct reiserfs_blk_iterate_data *bdata = data;
312         char *inline_data;
313
314         if (bdata->inline_offset == (u64)-1)
315                 bdata->inline_offset = position;
316         else if (bdata->inline_offset + bdata->inline_length != position) {
317                 /*
318                  * This condition shouldn't actually happen, but better to
319                  * catch it than break silently.
320                  */
321                 error(
322 "source fs contains file with multiple tails but they are not contiguous");
323                 return -EINVAL;
324         }
325
326         inline_data = realloc(bdata->inline_data, bdata->inline_length + len);
327         if (!inline_data)
328                 return -ENOMEM;
329
330         bdata->inline_data = inline_data;
331         memcpy(bdata->inline_data + bdata->inline_length, body, len);
332         bdata->inline_length += len;
333
334         return 0;
335 }
336
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)
341 {
342         struct btrfs_key key;
343         u32 sectorsize = root->fs_info->sectorsize;
344         int ret;
345         struct extent_buffer *eb;
346
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;
352
353         eb = alloc_extent_buffer(&root->fs_info->extent_cache, key.objectid,
354                                  sectorsize);
355
356         if (!eb)
357                 return -ENOMEM;
358
359         write_extent_buffer(eb, body, 0, length);
360         ret = write_and_map_eb(root->fs_info, eb);
361         free_extent_buffer(eb);
362         if (ret)
363                 return ret;
364
365         return btrfs_record_file_extent(trans, root, objectid, inode, offset,
366                                         key.objectid, sectorsize);
367 }
368
369 static int reiserfs_convert_tail(struct btrfs_trans_handle *trans,
370                                  struct btrfs_root *root,
371                                  struct btrfs_inode_item *inode,
372                                  u64 objectid, u64 offset,
373                                  const void *body, unsigned length,
374                                  u32 convert_flags)
375 {
376         u64 isize;
377         int ret;
378
379         if (length >= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info) ||
380             length >= root->fs_info->sectorsize)
381                 return convert_direct(trans, root, objectid, inode, body,
382                                       length, offset, convert_flags);
383
384         ret = btrfs_insert_inline_extent(trans, root, objectid,
385                                          offset, body, length);
386         if (ret)
387                 return ret;
388
389         isize = btrfs_stack_inode_nbytes(inode);
390         btrfs_set_stack_inode_nbytes(inode, isize + length);
391
392         return 0;
393 }
394
395 static inline u32 block_count(u64 size, u32 blocksize)
396 {
397         return round_up(size, blocksize) / blocksize;
398 }
399
400 static int reiserfs_record_file_extents(reiserfs_filsys_t fs,
401                                         struct btrfs_trans_handle *trans,
402                                         struct btrfs_root *root,
403                                         u64 objectid,
404                                         struct btrfs_inode_item *inode,
405                                         struct reiserfs_key *sd_key,
406                                         u32 convert_flags)
407
408 {
409         int ret;
410         u32 blocksize = fs->fs_blocksize;
411         u64 inode_size = btrfs_stack_inode_size(inode);
412         u32 last_block;
413         struct reiserfs_blk_iterate_data data;
414
415         init_reiserfs_blk_iterate_data(&data, trans, root, inode,
416                                        objectid, convert_flags);
417
418         ret = reiserfs_iterate_file_data(fs, sd_key,
419                                          reiserfs_record_indirect_extent,
420                                          reiserfs_record_direct_extent, &data);
421         if (ret)
422                 return ret;
423
424         /*
425          * blk_iterate_block has no idea that we're done iterating, so record
426          * the final range if any.  This range can end and still have a tail
427          * after it.
428          */
429         if (data.blk_data.num_blocks) {
430                 ret = record_file_blocks(&data.blk_data,
431                                          data.blk_data.first_block,
432                                          data.blk_data.disk_block,
433                                          data.blk_data.num_blocks);
434                 if (ret)
435                         goto fail;
436                 data.blk_data.first_block += data.blk_data.num_blocks;
437                 data.blk_data.num_blocks = 0;
438         }
439
440         /*
441          * Handle a hole at the end of the file.  ReiserFS will
442          * not write a tail followed by a hole but it will write a hole
443          * followed by a tail.
444          */
445         last_block = block_count(inode_size - data.inline_length, blocksize);
446         if (last_block > data.blk_data.first_block) {
447                 ret = record_file_blocks(&data.blk_data,
448                                          data.blk_data.first_block, 0,
449                                          last_block - data.blk_data.first_block);
450                 if (ret)
451                         goto fail;
452         }
453
454         if (data.inline_length) {
455                 ret = reiserfs_convert_tail(trans, root, inode, objectid,
456                                             data.inline_offset,
457                                             data.inline_data,
458                                             data.inline_length, convert_flags);
459                 if (ret)
460                         goto fail;
461         }
462
463         ret = 0;
464 fail:
465         return ret;
466 }
467
468 static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
469                               u32 convert_flags, u32 deh_dirid,
470                               u32 deh_objectid, u8 *type);
471
472 static int reiserfs_copy_dirent(reiserfs_filsys_t fs,
473                                 const struct reiserfs_key *dir_short_key,
474                                 const char *name, size_t len,
475                                 __u32 deh_dirid, __u32 deh_objectid,
476                                 void *cb_data)
477 {
478         int ret;
479         u8 type;
480         struct btrfs_trans_handle *trans;
481         u64 objectid = deh_objectid + OID_OFFSET;
482         struct reiserfs_convert_info *info = fs->fs_vp;
483         struct reiserfs_dirent_data *dirent_data = cb_data;
484         struct btrfs_root *root = dirent_data->root;
485         __u32 dir_objectid = get_key_objectid(dir_short_key) + OID_OFFSET;
486
487         /*
488          * These are the extended attributes and shouldn't appear as files
489          * in the converted file systems.
490          */
491         if (deh_objectid == get_key_objectid(&info->privroot_key))
492                 return 0;
493
494         ret = reiserfs_copy_meta(fs, root, dirent_data->convert_flags,
495                                  deh_dirid, deh_objectid, &type);
496         if (ret) {
497                 error(
498         "an error occured while converting \"%.*s\", reiserfs key [%u %u]: %s",
499                         (int)len, name, deh_dirid, deh_objectid,
500                         strerror(-ret));
501                 return ret;
502         }
503         trans = btrfs_start_transaction(root, 1);
504         if (IS_ERR(trans))
505                 return PTR_ERR(trans);
506
507         ret = convert_insert_dirent(trans, root, name, len, dir_objectid,
508                                     objectid, type, dirent_data->index++,
509                                     dirent_data->inode);
510         return btrfs_commit_transaction(trans, root);
511 }
512
513 static int reiserfs_copy_symlink(struct btrfs_trans_handle *trans,
514                                  struct btrfs_root *root, u64 objectid,
515                                  struct btrfs_inode_item *btrfs_inode,
516                                  reiserfs_filsys_t fs,
517                                  struct reiserfs_path *sd_path)
518 {
519         INITIALIZE_REISERFS_PATH(path);
520         struct item_head *ih = tp_item_head(sd_path);
521         struct reiserfs_key key = ih->ih_key;
522         int ret;
523         char *symlink;
524         int len;
525
526         set_key_uniqueness(&key, type2uniqueness(TYPE_DIRECT));
527         set_key_offset_v1(&key, 1);
528
529         ret = reiserfs_search_by_key_3(fs, &key, &path);
530         if (ret != ITEM_FOUND) {
531                 ret = -ENOENT;
532                 goto fail;
533         }
534
535         symlink = tp_item_body(&path);
536         len = get_ih_item_len(tp_item_head(&path));
537
538         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
539                                          symlink, len + 1);
540         btrfs_set_stack_inode_nbytes(btrfs_inode, len + 1);
541 fail:
542         pathrelse(&path);
543         return ret;
544 }
545
546 static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
547                               u32 convert_flags, u32 deh_dirid,
548                               u32 deh_objectid, u8 *type)
549 {
550         INITIALIZE_REISERFS_PATH(path);
551         int ret = 0;
552         struct item_head *ih;
553         struct reiserfs_key key;
554         struct btrfs_inode_item btrfs_inode;
555         struct btrfs_trans_handle *trans = NULL;
556         struct reiserfs_convert_info *info = fs->fs_vp;
557         u32 mode;
558         u64 objectid = deh_objectid + OID_OFFSET;
559         u64 parent = deh_dirid + OID_OFFSET;
560         struct reiserfs_dirent_data dirent_data = {
561                 .index = 2,
562                 .convert_flags = convert_flags,
563                 .inode = &btrfs_inode,
564                 .root = root,
565         };
566
567         /* The root directory's dirid in reiserfs points to an object
568          * that does't exist.  In btrfs it's self-referential.
569          */
570         if (deh_dirid == REISERFS_ROOT_PARENT_OBJECTID)
571                 parent = objectid;
572
573         set_key_dirid(&key, deh_dirid);
574         set_key_objectid(&key, deh_objectid);
575         set_key_offset_v2(&key, 0);
576         set_key_type_v2(&key, TYPE_STAT_DATA);
577
578         ret = reiserfs_search_by_key_3(fs, &key, &path);
579         if (ret != ITEM_FOUND) {
580                 ret = -ENOENT;
581                 goto fail;
582         }
583
584         ih = tp_item_head(&path);
585         if (!is_stat_data_ih(ih)) {
586                 ret = -EINVAL;
587                 goto fail;
588         }
589
590         reiserfs_copy_inode_item(&btrfs_inode, ih, tp_item_body(&path),
591                                  info->copy_attrs);
592         mode = btrfs_stack_inode_mode(&btrfs_inode);
593         *type = mode_to_file_type(mode);
594
595         if (S_ISREG(mode)) {
596                 /* Inodes with hardlinks should only be inserted once */
597                 if (btrfs_stack_inode_nlink(&btrfs_inode) > 1) {
598                         if (lookup_cached_objectid(fs, deh_objectid)) {
599                                 ret = 0;
600                                 goto fail; /* Not a failure */
601                         }
602                         ret = insert_cached_objectid(fs, deh_objectid);
603                         if (ret)
604                                 goto fail;
605                 }
606         }
607
608         if (!(convert_flags & CONVERT_FLAG_DATACSUM)) {
609                 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
610                             BTRFS_INODE_NODATASUM;
611                 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
612         }
613
614         switch (mode & S_IFMT) {
615         case S_IFREG:
616                 trans = btrfs_start_transaction(root, 1);
617                 if (IS_ERR(trans)) {
618                         ret = PTR_ERR(trans);
619                         goto fail;
620                 }
621                 ret = reiserfs_record_file_extents(fs, trans, root, objectid,
622                                                    &btrfs_inode, &ih->ih_key,
623                                                    convert_flags);
624                 if (ret)
625                         goto fail;
626                 break;
627         case S_IFDIR:
628                 ret = reiserfs_iterate_dir(fs, &ih->ih_key,
629                                            reiserfs_copy_dirent, &dirent_data);
630                 if (ret)
631                         goto fail;
632                 trans = btrfs_start_transaction(root, 1);
633                 if (IS_ERR(trans)) {
634                         ret = PTR_ERR(trans);
635                         goto fail;
636                 }
637
638                 ret = btrfs_insert_inode_ref(trans, root, "..", 2, parent,
639                                              objectid, 0);
640                 break;
641         case S_IFLNK:
642                 trans = btrfs_start_transaction(root, 1);
643                 if (IS_ERR(trans)) {
644                         ret = PTR_ERR(trans);
645                         goto fail;
646                 }
647                 ret = reiserfs_copy_symlink(trans, root, objectid,
648                                             &btrfs_inode, fs, &path);
649                 if (ret)
650                         goto fail;
651                 break;
652         default:
653                 trans = btrfs_start_transaction(root, 1);
654                 if (IS_ERR(trans)) {
655                         ret = PTR_ERR(trans);
656                         goto fail;
657                 }
658         }
659
660         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
661         if (ret)
662                 goto fail;
663         ret = btrfs_commit_transaction(trans, root);
664         info->progress->cur_copy_inodes++;
665
666 fail:
667         pathrelse(&path);
668         return ret;
669 }
670
671 static int reiserfs_xattr_indirect_fn(reiserfs_filsys_t fs, u64 position,
672                                       u64 size, int num_blocks,
673                                       u32 *blocks, void *data)
674 {
675         int i;
676         struct reiserfs_xattr_data *xa_data = data;
677         size_t alloc = min(position + num_blocks * fs->fs_blocksize, size);
678         char *body;
679
680         if (size > BTRFS_LEAF_DATA_SIZE(xa_data->root->fs_info) -
681             sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
682                 fprintf(stderr, "skip large xattr on objectid %llu name %.*s\n",
683                         xa_data->target_oid, (int)xa_data->namelen,
684                         xa_data->name);
685                 return -E2BIG;
686         }
687
688         body = realloc(xa_data->body, alloc);
689         if (!body)
690                 return -ENOMEM;
691
692         xa_data->body = body;
693         xa_data->len = alloc;
694
695         for (i = 0; i < num_blocks; i++) {
696                 int ret;
697                 u32 block = d32_get(blocks, i);
698                 u64 offset = (u64)block * fs->fs_blocksize;
699                 size_t chunk = min_t(u64, size - position, fs->fs_blocksize);
700                 char *buffer = xa_data->body + position;
701
702                 ret = read_disk_extent(xa_data->root, offset, chunk, buffer);
703                 if (ret)
704                         return ret;
705                 position += chunk;
706         }
707
708         return 0;
709 }
710
711 static int reiserfs_xattr_direct_fn(reiserfs_filsys_t fs, __u64 position,
712                                     __u64 size, const char *body, size_t len,
713                                     void *data)
714 {
715         struct reiserfs_xattr_data *xa_data = data;
716         char *newbody;
717
718         if (size > BTRFS_LEAF_DATA_SIZE(xa_data->root->fs_info) -
719             sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
720                 fprintf(stderr, "skip large xattr on objectid %llu name %.*s\n",
721                         xa_data->target_oid, (int)xa_data->namelen,
722                         xa_data->name);
723                 return -E2BIG;
724         }
725
726         newbody = realloc(xa_data->body, position + len);
727         if (!newbody)
728                 return -ENOMEM;
729         xa_data->body = newbody;
730         xa_data->len = position + len;
731         memcpy(xa_data->body + position, body, len);
732         return 0;
733 }
734
735 static int reiserfs_acl_to_xattr(void *dst, const void *src,
736                                  size_t dst_size, size_t src_size)
737 {
738         int i, count;
739         const void *end = src + src_size;
740         acl_ea_header *ext_acl = (acl_ea_header *)dst;
741         acl_ea_entry *dst_entry = ext_acl->a_entries;
742         struct reiserfs_acl_entry *src_entry;
743
744         if (src_size < sizeof(struct reiserfs_acl_header))
745                 goto fail;
746         if (((struct reiserfs_acl_header *)src)->a_version !=
747             cpu_to_le32(REISERFS_ACL_VERSION))
748                 goto fail;
749         src += sizeof(struct reiserfs_acl_header);
750         count = reiserfs_acl_count(src_size);
751         if (count <= 0)
752                 goto fail;
753
754         BUG_ON(dst_size < acl_ea_size(count));
755         ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
756         for (i = 0; i < count; i++, dst_entry++) {
757                 src_entry = (struct reiserfs_acl_entry *)src;
758                 if (src + sizeof(struct reiserfs_acl_entry_short) > end)
759                         goto fail;
760                 dst_entry->e_tag = src_entry->e_tag;
761                 dst_entry->e_perm = src_entry->e_perm;
762                 switch (le16_to_cpu(src_entry->e_tag)) {
763                 case ACL_USER_OBJ:
764                 case ACL_GROUP_OBJ:
765                 case ACL_MASK:
766                 case ACL_OTHER:
767                         src += sizeof(struct reiserfs_acl_entry_short);
768                         dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
769                         break;
770                 case ACL_USER:
771                 case ACL_GROUP:
772                         src += sizeof(struct reiserfs_acl_entry);
773                         if (src > end)
774                                 goto fail;
775                         dst_entry->e_id = src_entry->e_id;
776                         break;
777                 default:
778                         goto fail;
779                 }
780         }
781         if (src != end)
782                 goto fail;
783         return 0;
784 fail:
785         return -EINVAL;
786 }
787
788 static int reiserfs_copy_one_xattr(reiserfs_filsys_t fs,
789                                    const struct reiserfs_key *dir_short_key,
790                                    const char *name, size_t namelen,
791                                    __u32 deh_dirid,
792                                    __u32 deh_objectid, void *cb_data)
793 {
794         struct reiserfs_convert_info *info = fs->fs_vp;
795         struct reiserfs_xattr_data *xa_data = cb_data;
796         struct reiserfs_key key = {
797                 .k2_dir_id = deh_dirid,
798                 .k2_objectid = deh_objectid,
799         };
800         void *body = NULL;
801         int len;
802         int ret;
803
804         xa_data->name = name;
805         xa_data->namelen = namelen;
806
807         ret = reiserfs_iterate_file_data(fs, &key, reiserfs_xattr_indirect_fn,
808                                          reiserfs_xattr_direct_fn, cb_data);
809         if (ret)
810                 goto out;
811
812         if (!reiserfs_check_xattr(xa_data->body, xa_data->len)) {
813                 fprintf(stderr,
814                         "skip corrupted xattr on objectid %u name %.*s\n",
815                         deh_objectid, (int)xa_data->namelen,
816                         xa_data->name);
817                 goto out;
818         }
819
820         body = xa_data->body + sizeof(struct reiserfs_xattr_header);
821         len = xa_data->len - sizeof(struct reiserfs_xattr_header);
822
823         if (!strncmp("system.posix_acl_default", name, namelen) ||
824             !strncmp("system.posix_acl_access", name, namelen)) {
825                 size_t bufsize = acl_ea_size(ext2_acl_count(len));
826                 char *databuf = malloc(bufsize);
827
828                 if (!databuf)
829                         goto out;
830                 ret = reiserfs_acl_to_xattr(databuf, body, bufsize, len);
831                 if (ret)
832                         goto out;
833                 body = databuf;
834                 len = bufsize;
835         }
836
837         ret = btrfs_insert_xattr_item(xa_data->trans, xa_data->root,
838                                       name, namelen, body, len,
839                                       xa_data->target_oid);
840
841         info->progress->cur_copy_inodes++;
842 out:
843         if (body &&
844             body != xa_data->body + sizeof(struct reiserfs_xattr_header))
845                 free(body);
846         if (xa_data->body)
847                 free(xa_data->body);
848         xa_data->body = NULL;
849         xa_data->len = 0;
850
851         return ret;
852 }
853
854 static int reiserfs_copy_xattr_dir(reiserfs_filsys_t fs,
855                                    const struct reiserfs_key *dir_short_key,
856                                    const char *name, size_t len,
857                                    __u32 deh_dirid, __u32 deh_objectid,
858                                    void *cb_data)
859 {
860         struct reiserfs_convert_info *info = fs->fs_vp;
861         struct reiserfs_xattr_data *xa_data = cb_data;
862         struct reiserfs_key dir_key = {
863                 .k2_dir_id = deh_dirid,
864                 .k2_objectid = deh_objectid,
865         };
866         int ret, err;
867
868         errno = 0;
869         xa_data->target_oid = strtoull(name, NULL, 16);
870         if (xa_data->target_oid == ULLONG_MAX && errno)
871                 return -errno;
872
873         xa_data->target_oid += OID_OFFSET;
874
875         xa_data->trans = btrfs_start_transaction(xa_data->root, 1);
876         if (IS_ERR(xa_data->trans))
877                 return PTR_ERR(xa_data->trans);
878
879         ret = reiserfs_iterate_dir(fs, &dir_key,
880                                     reiserfs_copy_one_xattr, xa_data);
881
882         err = btrfs_commit_transaction(xa_data->trans, xa_data->root);
883         info->progress->cur_copy_inodes++;
884         xa_data->trans = NULL;
885         return ret ?: err;
886 }
887
888 static int reiserfs_copy_xattrs(reiserfs_filsys_t fs, struct btrfs_root *root)
889 {
890         struct reiserfs_convert_info *info = fs->fs_vp;
891         struct reiserfs_xattr_data data = {
892                 .root = root,
893         };
894
895         if (get_key_objectid(&info->xattr_key) == 0)
896                 return 0;
897
898         return reiserfs_iterate_dir(fs, &info->xattr_key,
899                                     reiserfs_copy_xattr_dir, &data);
900 }
901
902 static int reiserfs_copy_inodes(struct btrfs_convert_context *cxt,
903                                 struct btrfs_root *root,
904                                 u32 convert_flags,
905                                 struct task_ctx *p)
906 {
907         reiserfs_filsys_t fs = cxt->fs_data;
908         struct reiserfs_convert_info *info = fs->fs_vp;
909         int ret;
910         u8 type;
911
912         info->progress = p;
913
914         ret = reiserfs_locate_privroot(fs);
915         if (ret)
916                 goto out;
917
918         ret = reiserfs_copy_meta(fs, root, convert_flags,
919                                  REISERFS_ROOT_PARENT_OBJECTID,
920                                  REISERFS_ROOT_OBJECTID, &type);
921         if (ret)
922                 goto out;
923
924         if (convert_flags & CONVERT_FLAG_XATTR)
925                 ret = reiserfs_copy_xattrs(fs, root);
926
927 out:
928         info->progress = NULL;
929         return ret;
930 }
931
932 static int reiserfs_read_used_space(struct btrfs_convert_context *cxt)
933 {
934         reiserfs_filsys_t fs = cxt->fs_data;
935         u64 start, end = 0;
936         unsigned int size = get_sb_block_count(fs->fs_ondisk_sb);
937         unsigned long *bitmap = (unsigned long *)fs->fs_bitmap2->bm_map;
938         int ret = 0;
939
940         /*
941          * We have the entire bitmap loaded so we can just ping pong with
942          * ffz and ffs
943          */
944         while (end < size) {
945                 u64 offset, length;
946
947                 start = find_next_bit(bitmap, size, end);
948                 if (start >= size)
949                         break;
950                 end = find_next_zero_bit(bitmap, size, start);
951                 if (end > size)
952                         end = size;
953                 offset = start * fs->fs_blocksize;
954                 length = (end - start) * fs->fs_blocksize;
955                 ret = add_merge_cache_extent(&cxt->used_space, offset, length);
956                 if (ret < 0)
957                         break;
958         }
959
960         return ret;
961 }
962
963 static int reiserfs_check_state(struct btrfs_convert_context *cxt)
964 {
965         return 0;
966 }
967
968 const struct btrfs_convert_operations reiserfs_convert_ops = {
969         .name           = "reiserfs",
970         .open_fs        = reiserfs_open_fs,
971         .read_used_space = reiserfs_read_used_space,
972         .copy_inodes    = reiserfs_copy_inodes,
973         .close_fs       = reiserfs_close_fs,
974         .check_state    = reiserfs_check_state,
975 };
976
977 #endif /* BTRFSCONVERT_REISERFS */