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