c2616aeee157caf55c327d029ab6875ffdf76a85
[platform/upstream/btrfs-progs.git] / btrfs-convert.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include "kerncompat.h"
20
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <uuid/uuid.h>
30 #include <linux/limits.h>
31 #include <getopt.h>
32
33 #include "ctree.h"
34 #include "disk-io.h"
35 #include "volumes.h"
36 #include "transaction.h"
37 #include "crc32c.h"
38 #include "utils.h"
39 #include "task-utils.h"
40 #include <ext2fs/ext2_fs.h>
41 #include <ext2fs/ext2fs.h>
42 #include <ext2fs/ext2_ext_attr.h>
43
44 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
45 #define CONV_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
46
47 /*
48  * Compatibility code for e2fsprogs 1.41 which doesn't support RO compat flag
49  * BIGALLOC.
50  * Unlike normal RO compat flag, BIGALLOC affects how e2fsprogs check used
51  * space, and btrfs-convert heavily relies on it.
52  */
53 #ifdef HAVE_OLD_E2FSPROGS
54 #define EXT2FS_CLUSTER_RATIO(fs)        (1)
55 #define EXT2_CLUSTERS_PER_GROUP(s)      (EXT2_BLOCKS_PER_GROUP(s))
56 #define EXT2FS_B2C(fs, blk)             (blk)
57 #endif
58
59 struct task_ctx {
60         uint32_t max_copy_inodes;
61         uint32_t cur_copy_inodes;
62         struct task_info *info;
63 };
64
65 static void *print_copied_inodes(void *p)
66 {
67         struct task_ctx *priv = p;
68         const char work_indicator[] = { '.', 'o', 'O', 'o' };
69         uint32_t count = 0;
70
71         task_period_start(priv->info, 1000 /* 1s */);
72         while (1) {
73                 count++;
74                 printf("copy inodes [%c] [%10d/%10d]\r",
75                        work_indicator[count % 4], priv->cur_copy_inodes,
76                        priv->max_copy_inodes);
77                 fflush(stdout);
78                 task_period_wait(priv->info);
79         }
80
81         return NULL;
82 }
83
84 static int after_copied_inodes(void *p)
85 {
86         printf("\n");
87         fflush(stdout);
88
89         return 0;
90 }
91
92 struct btrfs_convert_context;
93 struct btrfs_convert_operations {
94         const char *name;
95         int (*open_fs)(struct btrfs_convert_context *cctx, const char *devname);
96         int (*read_used_space)(struct btrfs_convert_context *cctx);
97         int (*alloc_block)(struct btrfs_convert_context *cctx, u64 goal,
98                            u64 *block_ret);
99         int (*alloc_block_range)(struct btrfs_convert_context *cctx, u64 goal,
100                            int num, u64 *block_ret);
101         int (*test_block)(struct btrfs_convert_context *cctx, u64 block);
102         void (*free_block)(struct btrfs_convert_context *cctx, u64 block);
103         void (*free_block_range)(struct btrfs_convert_context *cctx, u64 block,
104                            int num);
105         int (*copy_inodes)(struct btrfs_convert_context *cctx,
106                          struct btrfs_root *root, int datacsum,
107                          int packing, int noxattr, struct task_ctx *p);
108         void (*close_fs)(struct btrfs_convert_context *cctx);
109 };
110
111 static void init_convert_context(struct btrfs_convert_context *cctx)
112 {
113         cache_tree_init(&cctx->used);
114         cache_tree_init(&cctx->data_chunks);
115         cache_tree_init(&cctx->free);
116 }
117
118 static void clean_convert_context(struct btrfs_convert_context *cctx)
119 {
120         free_extent_cache_tree(&cctx->used);
121         free_extent_cache_tree(&cctx->data_chunks);
122         free_extent_cache_tree(&cctx->free);
123 }
124
125 static inline int convert_alloc_block(struct btrfs_convert_context *cctx,
126                                       u64 goal, u64 *ret)
127 {
128         return  cctx->convert_ops->alloc_block(cctx, goal, ret);
129 }
130
131 static inline int convert_alloc_block_range(struct btrfs_convert_context *cctx,
132                                       u64 goal, int num, u64 *ret)
133 {
134         return  cctx->convert_ops->alloc_block_range(cctx, goal, num, ret);
135 }
136
137 static inline int convert_test_block(struct btrfs_convert_context *cctx,
138                                      u64 block)
139 {
140         return cctx->convert_ops->test_block(cctx, block);
141 }
142
143 static inline void convert_free_block(struct btrfs_convert_context *cctx,
144                                       u64 block)
145 {
146         cctx->convert_ops->free_block(cctx, block);
147 }
148
149 static inline void convert_free_block_range(struct btrfs_convert_context *cctx,
150                                       u64 block, int num)
151 {
152         cctx->convert_ops->free_block_range(cctx, block, num);
153 }
154
155 static inline int copy_inodes(struct btrfs_convert_context *cctx,
156                               struct btrfs_root *root, int datacsum,
157                               int packing, int noxattr, struct task_ctx *p)
158 {
159         return cctx->convert_ops->copy_inodes(cctx, root, datacsum, packing,
160                                              noxattr, p);
161 }
162
163 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
164 {
165         cctx->convert_ops->close_fs(cctx);
166 }
167
168 /*
169  * Open Ext2fs in readonly mode, read block allocation bitmap and
170  * inode bitmap into memory.
171  */
172 static int ext2_open_fs(struct btrfs_convert_context *cctx, const char *name)
173 {
174         errcode_t ret;
175         ext2_filsys ext2_fs;
176         ext2_ino_t ino;
177         u32 ro_feature;
178
179         ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
180         if (ret) {
181                 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
182                 return -1;
183         }
184         /*
185          * We need to know exactly the used space, some RO compat flags like
186          * BIGALLOC will affect how used space is present.
187          * So we need manuall check any unsupported RO compat flags
188          */
189         ro_feature = ext2_fs->super->s_feature_ro_compat;
190         if (ro_feature & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
191                 error(
192 "unsupported RO features detected: %x, abort convert to avoid possible corruption",
193                       ro_feature & ~EXT2_LIB_FEATURE_COMPAT_SUPP);
194                 goto fail;
195         }
196         ret = ext2fs_read_inode_bitmap(ext2_fs);
197         if (ret) {
198                 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
199                         error_message(ret));
200                 goto fail;
201         }
202         ret = ext2fs_read_block_bitmap(ext2_fs);
203         if (ret) {
204                 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
205                         error_message(ret));
206                 goto fail;
207         }
208         /*
209          * search each block group for a free inode. this set up
210          * uninit block/inode bitmaps appropriately.
211          */
212         ino = 1;
213         while (ino <= ext2_fs->super->s_inodes_count) {
214                 ext2_ino_t foo;
215                 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
216                 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
217         }
218
219         if (!(ext2_fs->super->s_feature_incompat &
220               EXT2_FEATURE_INCOMPAT_FILETYPE)) {
221                 fprintf(stderr, "filetype feature is missing\n");
222                 goto fail;
223         }
224
225         cctx->fs_data = ext2_fs;
226         cctx->blocksize = ext2_fs->blocksize;
227         cctx->block_count = ext2_fs->super->s_blocks_count;
228         cctx->total_bytes = ext2_fs->blocksize * ext2_fs->super->s_blocks_count;
229         cctx->volume_name = strndup(ext2_fs->super->s_volume_name, 16);
230         cctx->first_data_block = ext2_fs->super->s_first_data_block;
231         cctx->inodes_count = ext2_fs->super->s_inodes_count;
232         cctx->free_inodes_count = ext2_fs->super->s_free_inodes_count;
233         return 0;
234 fail:
235         ext2fs_close(ext2_fs);
236         return -1;
237 }
238
239 static int __ext2_add_one_block(ext2_filsys fs, char *bitmap,
240                                 unsigned long group_nr, struct cache_tree *used)
241 {
242         unsigned long offset;
243         unsigned i;
244         int ret = 0;
245
246         offset = fs->super->s_first_data_block;
247         offset /= EXT2FS_CLUSTER_RATIO(fs);
248         offset += group_nr * EXT2_CLUSTERS_PER_GROUP(fs->super);
249         for (i = 0; i < EXT2_CLUSTERS_PER_GROUP(fs->super); i++) {
250                 if (ext2fs_test_bit(i, bitmap)) {
251                         u64 start;
252
253                         start = (i + offset) * EXT2FS_CLUSTER_RATIO(fs);
254                         start *= fs->blocksize;
255                         ret = add_merge_cache_extent(used, start,
256                                                      fs->blocksize);
257                         if (ret < 0)
258                                 break;
259                 }
260         }
261         return ret;
262 }
263
264 /*
265  * Read all used ext2 space into cctx->used cache tree
266  */
267 static int ext2_read_used_space(struct btrfs_convert_context *cctx)
268 {
269         ext2_filsys fs = (ext2_filsys)cctx->fs_data;
270         blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
271         struct cache_tree *used_tree = &cctx->used;
272         char *block_bitmap = NULL;
273         unsigned long i;
274         int block_nbytes;
275         int ret = 0;
276
277         block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
278         /* Shouldn't happen */
279         BUG_ON(!fs->block_map);
280
281         block_bitmap = malloc(block_nbytes);
282         if (!block_bitmap)
283                 return -ENOMEM;
284
285         for (i = 0; i < fs->group_desc_count; i++) {
286                 ret = ext2fs_get_block_bitmap_range(fs->block_map, blk_itr,
287                                                 block_nbytes * 8, block_bitmap);
288                 if (ret) {
289                         error("fail to get bitmap from ext2, %s",
290                               strerror(-ret));
291                         break;
292                 }
293                 ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree);
294                 if (ret < 0) {
295                         error("fail to build used space tree, %s",
296                               strerror(-ret));
297                         break;
298                 }
299                 blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super);
300         }
301
302         free(block_bitmap);
303         return ret;
304 }
305
306 static void ext2_close_fs(struct btrfs_convert_context *cctx)
307 {
308         if (cctx->volume_name) {
309                 free(cctx->volume_name);
310                 cctx->volume_name = NULL;
311         }
312         ext2fs_close(cctx->fs_data);
313 }
314
315 static int ext2_alloc_block(struct btrfs_convert_context *cctx,
316                             u64 goal, u64 *block_ret)
317 {
318         ext2_filsys fs = cctx->fs_data;
319         blk_t block;
320
321         if (!ext2fs_new_block(fs, goal, NULL, &block)) {
322                 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
323                 *block_ret = block;
324                 return 0;
325         }
326         return -ENOSPC;
327 }
328
329 static int ext2_alloc_block_range(struct btrfs_convert_context *cctx, u64 goal,
330                 int num, u64 *block_ret)
331 {
332         ext2_filsys fs = cctx->fs_data;
333         blk_t block;
334         ext2fs_block_bitmap bitmap = fs->block_map;
335         blk_t start = ext2fs_get_block_bitmap_start(bitmap);
336         blk_t end = ext2fs_get_block_bitmap_end(bitmap);
337
338         for (block = max_t(u64, goal, start); block + num < end; block++) {
339                 if (ext2fs_fast_test_block_bitmap_range(bitmap, block, num)) {
340                         ext2fs_fast_mark_block_bitmap_range(bitmap, block,
341                                         num);
342                         *block_ret = block;
343                         return 0;
344                 }
345         }
346         return -ENOSPC;
347 }
348
349 static void ext2_free_block(struct btrfs_convert_context *cctx, u64 block)
350 {
351         ext2_filsys fs = cctx->fs_data;
352
353         BUG_ON(block != (blk_t)block);
354         ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
355 }
356
357 static void ext2_free_block_range(struct btrfs_convert_context *cctx, u64 block, int num)
358 {
359         ext2_filsys fs = cctx->fs_data;
360
361         BUG_ON(block != (blk_t)block);
362         ext2fs_fast_unmark_block_bitmap_range(fs->block_map, block, num);
363 }
364
365 static int cache_free_extents(struct btrfs_root *root,
366                               struct btrfs_convert_context *cctx)
367
368 {
369         int i, ret = 0;
370         blk_t block;
371         u64 bytenr;
372         u64 blocksize = cctx->blocksize;
373
374         block = cctx->first_data_block;
375         for (; block < cctx->block_count; block++) {
376                 if (convert_test_block(cctx, block))
377                         continue;
378                 bytenr = block * blocksize;
379                 ret = set_extent_dirty(&root->fs_info->free_space_cache,
380                                        bytenr, bytenr + blocksize - 1, 0);
381                 BUG_ON(ret);
382         }
383
384         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
385                 bytenr = btrfs_sb_offset(i);
386                 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
387                 if (bytenr >= blocksize * cctx->block_count)
388                         break;
389                 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
390                                    bytenr + BTRFS_STRIPE_LEN - 1, 0);
391         }
392
393         clear_extent_dirty(&root->fs_info->free_space_cache,
394                            0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
395
396         return 0;
397 }
398
399 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
400                                u64 hint_byte, struct btrfs_key *ins,
401                                int metadata)
402 {
403         u64 start;
404         u64 end;
405         u64 last = hint_byte;
406         int ret;
407         int wrapped = 0;
408         struct btrfs_block_group_cache *cache;
409
410         while(1) {
411                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
412                                             last, &start, &end, EXTENT_DIRTY);
413                 if (ret) {
414                         if (wrapped++ == 0) {
415                                 last = 0;
416                                 continue;
417                         } else {
418                                 goto fail;
419                         }
420                 }
421
422                 start = max(last, start);
423                 last = end + 1;
424                 if (last - start < num_bytes)
425                         continue;
426
427                 last = start + num_bytes;
428                 if (test_range_bit(&root->fs_info->pinned_extents,
429                                    start, last - 1, EXTENT_DIRTY, 0))
430                         continue;
431
432                 cache = btrfs_lookup_block_group(root->fs_info, start);
433                 BUG_ON(!cache);
434                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
435                     last > cache->key.objectid + cache->key.offset) {
436                         last = cache->key.objectid + cache->key.offset;
437                         continue;
438                 }
439
440                 if (metadata) {
441                         BUG_ON(num_bytes != root->nodesize);
442                         if (check_crossing_stripes(start, num_bytes)) {
443                                 last = round_down(start + num_bytes,
444                                                   BTRFS_STRIPE_LEN);
445                                 continue;
446                         }
447                 }
448                 clear_extent_dirty(&root->fs_info->free_space_cache,
449                                    start, start + num_bytes - 1, 0);
450
451                 ins->objectid = start;
452                 ins->offset = num_bytes;
453                 ins->type = BTRFS_EXTENT_ITEM_KEY;
454                 return 0;
455         }
456 fail:
457         fprintf(stderr, "not enough free space\n");
458         return -ENOSPC;
459 }
460
461 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
462 {
463         int i;
464         u64 offset;
465
466         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
467                 offset = btrfs_sb_offset(i);
468                 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
469
470                 if (bytenr < offset + BTRFS_STRIPE_LEN &&
471                     bytenr + num_bytes > offset)
472                         return 1;
473         }
474         return 0;
475 }
476
477 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
478                               u64 num_bytes)
479 {
480         return intersect_with_sb(bytenr, num_bytes);
481 }
482
483 static struct btrfs_extent_ops extent_ops = {
484         .alloc_extent = custom_alloc_extent,
485         .free_extent = custom_free_extent,
486 };
487
488 static int convert_insert_dirent(struct btrfs_trans_handle *trans,
489                                  struct btrfs_root *root,
490                                  const char *name, size_t name_len,
491                                  u64 dir, u64 objectid,
492                                  u8 file_type, u64 index_cnt,
493                                  struct btrfs_inode_item *inode)
494 {
495         int ret;
496         u64 inode_size;
497         struct btrfs_key location = {
498                 .objectid = objectid,
499                 .offset = 0,
500                 .type = BTRFS_INODE_ITEM_KEY,
501         };
502
503         ret = btrfs_insert_dir_item(trans, root, name, name_len,
504                                     dir, &location, file_type, index_cnt);
505         if (ret)
506                 return ret;
507         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
508                                      objectid, dir, index_cnt);
509         if (ret)
510                 return ret;
511         inode_size = btrfs_stack_inode_size(inode) + name_len * 2;
512         btrfs_set_stack_inode_size(inode, inode_size);
513
514         return 0;
515 }
516
517 struct dir_iterate_data {
518         struct btrfs_trans_handle *trans;
519         struct btrfs_root *root;
520         struct btrfs_inode_item *inode;
521         u64 objectid;
522         u64 index_cnt;
523         u64 parent;
524         int errcode;
525 };
526
527 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
528         [EXT2_FT_UNKNOWN]       = BTRFS_FT_UNKNOWN,
529         [EXT2_FT_REG_FILE]      = BTRFS_FT_REG_FILE,
530         [EXT2_FT_DIR]           = BTRFS_FT_DIR,
531         [EXT2_FT_CHRDEV]        = BTRFS_FT_CHRDEV,
532         [EXT2_FT_BLKDEV]        = BTRFS_FT_BLKDEV,
533         [EXT2_FT_FIFO]          = BTRFS_FT_FIFO,
534         [EXT2_FT_SOCK]          = BTRFS_FT_SOCK,
535         [EXT2_FT_SYMLINK]       = BTRFS_FT_SYMLINK,
536 };
537
538 static int dir_iterate_proc(ext2_ino_t dir, int entry,
539                             struct ext2_dir_entry *dirent,
540                             int offset, int blocksize,
541                             char *buf,void *priv_data)
542 {
543         int ret;
544         int file_type;
545         u64 objectid;
546         char dotdot[] = "..";
547         struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
548         int name_len;
549
550         name_len = dirent->name_len & 0xFF;
551
552         objectid = dirent->inode + INO_OFFSET;
553         if (!strncmp(dirent->name, dotdot, name_len)) {
554                 if (name_len == 2) {
555                         BUG_ON(idata->parent != 0);
556                         idata->parent = objectid;
557                 }
558                 return 0;
559         }
560         if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
561                 return 0;
562
563         file_type = dirent->name_len >> 8;
564         BUG_ON(file_type > EXT2_FT_SYMLINK);
565
566         ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
567                                     name_len, idata->objectid, objectid,
568                                     filetype_conversion_table[file_type],
569                                     idata->index_cnt, idata->inode);
570         if (ret < 0) {
571                 idata->errcode = ret;
572                 return BLOCK_ABORT;
573         }
574
575         idata->index_cnt++;
576         return 0;
577 }
578
579 static int create_dir_entries(struct btrfs_trans_handle *trans,
580                               struct btrfs_root *root, u64 objectid,
581                               struct btrfs_inode_item *btrfs_inode,
582                               ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
583 {
584         int ret;
585         errcode_t err;
586         struct dir_iterate_data data = {
587                 .trans          = trans,
588                 .root           = root,
589                 .inode          = btrfs_inode,
590                 .objectid       = objectid,
591                 .index_cnt      = 2,
592                 .parent         = 0,
593                 .errcode        = 0,
594         };
595
596         err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
597                                   dir_iterate_proc, &data);
598         if (err)
599                 goto error;
600         ret = data.errcode;
601         if (ret == 0 && data.parent == objectid) {
602                 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
603                                              objectid, objectid, 0);
604         }
605         return ret;
606 error:
607         fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
608         return -1;
609 }
610
611 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
612                             u32 num_bytes, char *buffer)
613 {
614         int ret;
615         struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
616
617         ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
618         if (ret != num_bytes)
619                 goto fail;
620         ret = 0;
621 fail:
622         if (ret > 0)
623                 ret = -1;
624         return ret;
625 }
626
627 static int csum_disk_extent(struct btrfs_trans_handle *trans,
628                             struct btrfs_root *root,
629                             u64 disk_bytenr, u64 num_bytes)
630 {
631         u32 blocksize = root->sectorsize;
632         u64 offset;
633         char *buffer;
634         int ret = 0;
635
636         buffer = malloc(blocksize);
637         if (!buffer)
638                 return -ENOMEM;
639         for (offset = 0; offset < num_bytes; offset += blocksize) {
640                 ret = read_disk_extent(root, disk_bytenr + offset,
641                                         blocksize, buffer);
642                 if (ret)
643                         break;
644                 ret = btrfs_csum_file_block(trans,
645                                             root->fs_info->csum_root,
646                                             disk_bytenr + num_bytes,
647                                             disk_bytenr + offset,
648                                             buffer, blocksize);
649                 if (ret)
650                         break;
651         }
652         free(buffer);
653         return ret;
654 }
655
656 struct blk_iterate_data {
657         struct btrfs_trans_handle *trans;
658         struct btrfs_root *root;
659         struct btrfs_root *convert_root;
660         struct btrfs_inode_item *inode;
661         u64 convert_ino;
662         u64 objectid;
663         u64 first_block;
664         u64 disk_block;
665         u64 num_blocks;
666         u64 boundary;
667         int checksum;
668         int errcode;
669 };
670
671 static void init_blk_iterate_data(struct blk_iterate_data *data,
672                                   struct btrfs_trans_handle *trans,
673                                   struct btrfs_root *root,
674                                   struct btrfs_inode_item *inode,
675                                   u64 objectid, int checksum)
676 {
677         struct btrfs_key key;
678
679         data->trans             = trans;
680         data->root              = root;
681         data->inode             = inode;
682         data->objectid          = objectid;
683         data->first_block       = 0;
684         data->disk_block        = 0;
685         data->num_blocks        = 0;
686         data->boundary          = (u64)-1;
687         data->checksum          = checksum;
688         data->errcode           = 0;
689
690         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
691         key.type = BTRFS_ROOT_ITEM_KEY;
692         key.offset = (u64)-1;
693         data->convert_root = btrfs_read_fs_root(root->fs_info, &key);
694         /* Impossible as we just opened it before */
695         BUG_ON(!data->convert_root || IS_ERR(data->convert_root));
696         data->convert_ino = BTRFS_FIRST_FREE_OBJECTID + 1;
697 }
698
699 /*
700  * Record a file extent in original filesystem into btrfs one.
701  * The special point is, old disk_block can point to a reserved range.
702  * So here, we don't use disk_block directly but search convert_root
703  * to get the real disk_bytenr.
704  */
705 static int record_file_blocks(struct blk_iterate_data *data,
706                               u64 file_block, u64 disk_block, u64 num_blocks)
707 {
708         int ret = 0;
709         struct btrfs_root *root = data->root;
710         struct btrfs_root *convert_root = data->convert_root;
711         struct btrfs_path *path;
712         u64 file_pos = file_block * root->sectorsize;
713         u64 old_disk_bytenr = disk_block * root->sectorsize;
714         u64 num_bytes = num_blocks * root->sectorsize;
715         u64 cur_off = old_disk_bytenr;
716
717         /* Hole, pass it to record_file_extent directly */
718         if (old_disk_bytenr == 0)
719                 return btrfs_record_file_extent(data->trans, root,
720                                 data->objectid, data->inode, file_pos, 0,
721                                 num_bytes);
722
723         path = btrfs_alloc_path();
724         if (!path)
725                 return -ENOMEM;
726
727         /*
728          * Search real disk bytenr from convert root
729          */
730         while (cur_off < old_disk_bytenr + num_bytes) {
731                 struct btrfs_key key;
732                 struct btrfs_file_extent_item *fi;
733                 struct extent_buffer *node;
734                 int slot;
735                 u64 extent_disk_bytenr;
736                 u64 extent_num_bytes;
737                 u64 real_disk_bytenr;
738                 u64 cur_len;
739
740                 key.objectid = data->convert_ino;
741                 key.type = BTRFS_EXTENT_DATA_KEY;
742                 key.offset = cur_off;
743
744                 ret = btrfs_search_slot(NULL, convert_root, &key, path, 0, 0);
745                 if (ret < 0)
746                         break;
747                 if (ret > 0) {
748                         ret = btrfs_previous_item(convert_root, path,
749                                                   data->convert_ino,
750                                                   BTRFS_EXTENT_DATA_KEY);
751                         if (ret < 0)
752                                 break;
753                         if (ret > 0) {
754                                 ret = -ENOENT;
755                                 break;
756                         }
757                 }
758                 node = path->nodes[0];
759                 slot = path->slots[0];
760                 btrfs_item_key_to_cpu(node, &key, slot);
761                 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY ||
762                        key.objectid != data->convert_ino ||
763                        key.offset > cur_off);
764                 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
765                 extent_disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
766                 extent_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
767                 BUG_ON(cur_off - key.offset >= extent_num_bytes);
768                 btrfs_release_path(path);
769
770                 real_disk_bytenr = cur_off - key.offset + extent_disk_bytenr;
771                 cur_len = min(key.offset + extent_num_bytes,
772                               old_disk_bytenr + num_bytes) - cur_off;
773                 ret = btrfs_record_file_extent(data->trans, data->root,
774                                         data->objectid, data->inode, file_pos,
775                                         real_disk_bytenr, cur_len);
776                 if (ret < 0)
777                         break;
778                 cur_off += cur_len;
779                 file_pos += cur_len;
780
781                 /*
782                  * No need to care about csum
783                  * As every byte of old fs image is calculated for csum, no
784                  * need to waste CPU cycles now.
785                  */
786         }
787         btrfs_free_path(path);
788         return ret;
789 }
790
791 static int block_iterate_proc(u64 disk_block, u64 file_block,
792                               struct blk_iterate_data *idata)
793 {
794         int ret = 0;
795         int sb_region;
796         int do_barrier;
797         struct btrfs_root *root = idata->root;
798         struct btrfs_block_group_cache *cache;
799         u64 bytenr = disk_block * root->sectorsize;
800
801         sb_region = intersect_with_sb(bytenr, root->sectorsize);
802         do_barrier = sb_region || disk_block >= idata->boundary;
803         if ((idata->num_blocks > 0 && do_barrier) ||
804             (file_block > idata->first_block + idata->num_blocks) ||
805             (disk_block != idata->disk_block + idata->num_blocks)) {
806                 if (idata->num_blocks > 0) {
807                         ret = record_file_blocks(idata, idata->first_block,
808                                                  idata->disk_block,
809                                                  idata->num_blocks);
810                         if (ret)
811                                 goto fail;
812                         idata->first_block += idata->num_blocks;
813                         idata->num_blocks = 0;
814                 }
815                 if (file_block > idata->first_block) {
816                         ret = record_file_blocks(idata, idata->first_block,
817                                         0, file_block - idata->first_block);
818                         if (ret)
819                                 goto fail;
820                 }
821
822                 if (sb_region) {
823                         bytenr += BTRFS_STRIPE_LEN - 1;
824                         bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
825                 } else {
826                         cache = btrfs_lookup_block_group(root->fs_info, bytenr);
827                         BUG_ON(!cache);
828                         bytenr = cache->key.objectid + cache->key.offset;
829                 }
830
831                 idata->first_block = file_block;
832                 idata->disk_block = disk_block;
833                 idata->boundary = bytenr / root->sectorsize;
834         }
835         idata->num_blocks++;
836 fail:
837         return ret;
838 }
839
840 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
841                                 e2_blkcnt_t blockcnt, blk_t ref_block,
842                                 int ref_offset, void *priv_data)
843 {
844         int ret;
845         struct blk_iterate_data *idata;
846         idata = (struct blk_iterate_data *)priv_data;
847         ret = block_iterate_proc(*blocknr, blockcnt, idata);
848         if (ret) {
849                 idata->errcode = ret;
850                 return BLOCK_ABORT;
851         }
852         return 0;
853 }
854
855 /*
856  * traverse file's data blocks, record these data blocks as file extents.
857  */
858 static int create_file_extents(struct btrfs_trans_handle *trans,
859                                struct btrfs_root *root, u64 objectid,
860                                struct btrfs_inode_item *btrfs_inode,
861                                ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
862                                int datacsum, int packing)
863 {
864         int ret;
865         char *buffer = NULL;
866         errcode_t err;
867         u32 last_block;
868         u32 sectorsize = root->sectorsize;
869         u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
870         struct blk_iterate_data data;
871
872         init_blk_iterate_data(&data, trans, root, btrfs_inode, objectid,
873                               datacsum);
874
875         err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
876                                     NULL, __block_iterate_proc, &data);
877         if (err)
878                 goto error;
879         ret = data.errcode;
880         if (ret)
881                 goto fail;
882         if (packing && data.first_block == 0 && data.num_blocks > 0 &&
883             inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
884                 u64 num_bytes = data.num_blocks * sectorsize;
885                 u64 disk_bytenr = data.disk_block * sectorsize;
886                 u64 nbytes;
887
888                 buffer = malloc(num_bytes);
889                 if (!buffer)
890                         return -ENOMEM;
891                 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
892                 if (ret)
893                         goto fail;
894                 if (num_bytes > inode_size)
895                         num_bytes = inode_size;
896                 ret = btrfs_insert_inline_extent(trans, root, objectid,
897                                                  0, buffer, num_bytes);
898                 if (ret)
899                         goto fail;
900                 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
901                 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
902         } else if (data.num_blocks > 0) {
903                 ret = record_file_blocks(&data, data.first_block,
904                                          data.disk_block, data.num_blocks);
905                 if (ret)
906                         goto fail;
907         }
908         data.first_block += data.num_blocks;
909         last_block = (inode_size + sectorsize - 1) / sectorsize;
910         if (last_block > data.first_block) {
911                 ret = record_file_blocks(&data, data.first_block, 0,
912                                          last_block - data.first_block);
913         }
914 fail:
915         free(buffer);
916         return ret;
917 error:
918         fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
919         return -1;
920 }
921
922 static int create_symbol_link(struct btrfs_trans_handle *trans,
923                               struct btrfs_root *root, u64 objectid,
924                               struct btrfs_inode_item *btrfs_inode,
925                               ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
926                               struct ext2_inode *ext2_inode)
927 {
928         int ret;
929         char *pathname;
930         u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
931         if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
932                 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
933                 ret = create_file_extents(trans, root, objectid, btrfs_inode,
934                                           ext2_fs, ext2_ino, 1, 1);
935                 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
936                 return ret;
937         }
938
939         pathname = (char *)&(ext2_inode->i_block[0]);
940         BUG_ON(pathname[inode_size] != 0);
941         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
942                                          pathname, inode_size + 1);
943         btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
944         return ret;
945 }
946
947 /*
948  * Following xattr/acl related codes are based on codes in
949  * fs/ext3/xattr.c and fs/ext3/acl.c
950  */
951 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
952 #define EXT2_XATTR_BFIRST(ptr) \
953         ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
954 #define EXT2_XATTR_IHDR(inode) \
955         ((struct ext2_ext_attr_header *) ((void *)(inode) + \
956                 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
957 #define EXT2_XATTR_IFIRST(inode) \
958         ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
959                 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
960
961 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
962                                   const void *end)
963 {
964         struct ext2_ext_attr_entry *next;
965
966         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
967                 next = EXT2_EXT_ATTR_NEXT(entry);
968                 if ((void *)next >= end)
969                         return -EIO;
970                 entry = next;
971         }
972         return 0;
973 }
974
975 static int ext2_xattr_check_block(const char *buf, size_t size)
976 {
977         int error;
978         struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
979
980         if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
981             header->h_blocks != 1)
982                 return -EIO;
983         error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
984         return error;
985 }
986
987 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
988                                   size_t size)
989 {
990         size_t value_size = entry->e_value_size;
991
992         if (entry->e_value_block != 0 || value_size > size ||
993             entry->e_value_offs + value_size > size)
994                 return -EIO;
995         return 0;
996 }
997
998 #define EXT2_ACL_VERSION        0x0001
999
1000 /* 23.2.5 acl_tag_t values */
1001
1002 #define ACL_UNDEFINED_TAG       (0x00)
1003 #define ACL_USER_OBJ            (0x01)
1004 #define ACL_USER                (0x02)
1005 #define ACL_GROUP_OBJ           (0x04)
1006 #define ACL_GROUP               (0x08)
1007 #define ACL_MASK                (0x10)
1008 #define ACL_OTHER               (0x20)
1009
1010 /* 23.2.7 ACL qualifier constants */
1011
1012 #define ACL_UNDEFINED_ID        ((id_t)-1)
1013
1014 typedef struct {
1015         __le16          e_tag;
1016         __le16          e_perm;
1017         __le32          e_id;
1018 } ext2_acl_entry;
1019
1020 typedef struct {
1021         __le16          e_tag;
1022         __le16          e_perm;
1023 } ext2_acl_entry_short;
1024
1025 typedef struct {
1026         __le32          a_version;
1027 } ext2_acl_header;
1028
1029 static inline int ext2_acl_count(size_t size)
1030 {
1031         ssize_t s;
1032         size -= sizeof(ext2_acl_header);
1033         s = size - 4 * sizeof(ext2_acl_entry_short);
1034         if (s < 0) {
1035                 if (size % sizeof(ext2_acl_entry_short))
1036                         return -1;
1037                 return size / sizeof(ext2_acl_entry_short);
1038         } else {
1039                 if (s % sizeof(ext2_acl_entry))
1040                         return -1;
1041                 return s / sizeof(ext2_acl_entry) + 4;
1042         }
1043 }
1044
1045 #define ACL_EA_VERSION          0x0002
1046
1047 typedef struct {
1048         __le16          e_tag;
1049         __le16          e_perm;
1050         __le32          e_id;
1051 } acl_ea_entry;
1052
1053 typedef struct {
1054         __le32          a_version;
1055         acl_ea_entry    a_entries[0];
1056 } acl_ea_header;
1057
1058 static inline size_t acl_ea_size(int count)
1059 {
1060         return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
1061 }
1062
1063 static int ext2_acl_to_xattr(void *dst, const void *src,
1064                              size_t dst_size, size_t src_size)
1065 {
1066         int i, count;
1067         const void *end = src + src_size;
1068         acl_ea_header *ext_acl = (acl_ea_header *)dst;
1069         acl_ea_entry *dst_entry = ext_acl->a_entries;
1070         ext2_acl_entry *src_entry;
1071
1072         if (src_size < sizeof(ext2_acl_header))
1073                 goto fail;
1074         if (((ext2_acl_header *)src)->a_version !=
1075             cpu_to_le32(EXT2_ACL_VERSION))
1076                 goto fail;
1077         src += sizeof(ext2_acl_header);
1078         count = ext2_acl_count(src_size);
1079         if (count <= 0)
1080                 goto fail;
1081
1082         BUG_ON(dst_size < acl_ea_size(count));
1083         ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
1084         for (i = 0; i < count; i++, dst_entry++) {
1085                 src_entry = (ext2_acl_entry *)src;
1086                 if (src + sizeof(ext2_acl_entry_short) > end)
1087                         goto fail;
1088                 dst_entry->e_tag = src_entry->e_tag;
1089                 dst_entry->e_perm = src_entry->e_perm;
1090                 switch (le16_to_cpu(src_entry->e_tag)) {
1091                 case ACL_USER_OBJ:
1092                 case ACL_GROUP_OBJ:
1093                 case ACL_MASK:
1094                 case ACL_OTHER:
1095                         src += sizeof(ext2_acl_entry_short);
1096                         dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1097                         break;
1098                 case ACL_USER:
1099                 case ACL_GROUP:
1100                         src += sizeof(ext2_acl_entry);
1101                         if (src > end)
1102                                 goto fail;
1103                         dst_entry->e_id = src_entry->e_id;
1104                         break;
1105                 default:
1106                         goto fail;
1107                 }
1108         }
1109         if (src != end)
1110                 goto fail;
1111         return 0;
1112 fail:
1113         return -EINVAL;
1114 }
1115
1116 static char *xattr_prefix_table[] = {
1117         [1] =   "user.",
1118         [2] =   "system.posix_acl_access",
1119         [3] =   "system.posix_acl_default",
1120         [4] =   "trusted.",
1121         [6] =   "security.",
1122 };
1123
1124 static int copy_single_xattr(struct btrfs_trans_handle *trans,
1125                              struct btrfs_root *root, u64 objectid,
1126                              struct ext2_ext_attr_entry *entry,
1127                              const void *data, u32 datalen)
1128 {
1129         int ret = 0;
1130         int name_len;
1131         int name_index;
1132         void *databuf = NULL;
1133         char namebuf[XATTR_NAME_MAX + 1];
1134
1135         name_index = entry->e_name_index;
1136         if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
1137             xattr_prefix_table[name_index] == NULL)
1138                 return -EOPNOTSUPP;
1139         name_len = strlen(xattr_prefix_table[name_index]) +
1140                    entry->e_name_len;
1141         if (name_len >= sizeof(namebuf))
1142                 return -ERANGE;
1143
1144         if (name_index == 2 || name_index == 3) {
1145                 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
1146                 databuf = malloc(bufsize);
1147                 if (!databuf)
1148                        return -ENOMEM;
1149                 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
1150                 if (ret)
1151                         goto out;
1152                 data = databuf;
1153                 datalen = bufsize;
1154         }
1155         strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
1156         strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
1157         if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
1158             sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
1159                 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
1160                         objectid - INO_OFFSET, name_len, namebuf);
1161                 goto out;
1162         }
1163         ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
1164                                       data, datalen, objectid);
1165 out:
1166         free(databuf);
1167         return ret;
1168 }
1169
1170 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
1171                                struct btrfs_root *root, u64 objectid,
1172                                struct btrfs_inode_item *btrfs_inode,
1173                                ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
1174 {
1175         int ret = 0;
1176         int inline_ea = 0;
1177         errcode_t err;
1178         u32 datalen;
1179         u32 block_size = ext2_fs->blocksize;
1180         u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
1181         struct ext2_inode_large *ext2_inode;
1182         struct ext2_ext_attr_entry *entry;
1183         void *data;
1184         char *buffer = NULL;
1185         char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
1186
1187         if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
1188                 ext2_inode = (struct ext2_inode_large *)inode_buf;
1189         } else {
1190                 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
1191                 if (!ext2_inode)
1192                        return -ENOMEM;
1193         }
1194         err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
1195                                      inode_size);
1196         if (err) {
1197                 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
1198                         error_message(err));
1199                 ret = -1;
1200                 goto out;
1201         }
1202
1203         if (ext2_ino > ext2_fs->super->s_first_ino &&
1204             inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1205                 if (EXT2_GOOD_OLD_INODE_SIZE +
1206                     ext2_inode->i_extra_isize > inode_size) {
1207                         ret = -EIO;
1208                         goto out;
1209                 }
1210                 if (ext2_inode->i_extra_isize != 0 &&
1211                     EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
1212                     EXT2_EXT_ATTR_MAGIC) {
1213                         inline_ea = 1;
1214                 }
1215         }
1216         if (inline_ea) {
1217                 int total;
1218                 void *end = (void *)ext2_inode + inode_size;
1219                 entry = EXT2_XATTR_IFIRST(ext2_inode);
1220                 total = end - (void *)entry;
1221                 ret = ext2_xattr_check_names(entry, end);
1222                 if (ret)
1223                         goto out;
1224                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1225                         ret = ext2_xattr_check_entry(entry, total);
1226                         if (ret)
1227                                 goto out;
1228                         data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
1229                                 entry->e_value_offs;
1230                         datalen = entry->e_value_size;
1231                         ret = copy_single_xattr(trans, root, objectid,
1232                                                 entry, data, datalen);
1233                         if (ret)
1234                                 goto out;
1235                         entry = EXT2_EXT_ATTR_NEXT(entry);
1236                 }
1237         }
1238
1239         if (ext2_inode->i_file_acl == 0)
1240                 goto out;
1241
1242         buffer = malloc(block_size);
1243         if (!buffer) {
1244                 ret = -ENOMEM;
1245                 goto out;
1246         }
1247         err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
1248         if (err) {
1249                 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
1250                         error_message(err));
1251                 ret = -1;
1252                 goto out;
1253         }
1254         ret = ext2_xattr_check_block(buffer, block_size);
1255         if (ret)
1256                 goto out;
1257
1258         entry = EXT2_XATTR_BFIRST(buffer);
1259         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1260                 ret = ext2_xattr_check_entry(entry, block_size);
1261                 if (ret)
1262                         goto out;
1263                 data = buffer + entry->e_value_offs;
1264                 datalen = entry->e_value_size;
1265                 ret = copy_single_xattr(trans, root, objectid,
1266                                         entry, data, datalen);
1267                 if (ret)
1268                         goto out;
1269                 entry = EXT2_EXT_ATTR_NEXT(entry);
1270         }
1271 out:
1272         free(buffer);
1273         if ((void *)ext2_inode != inode_buf)
1274                 free(ext2_inode);
1275         return ret;
1276 }
1277 #define MINORBITS       20
1278 #define MKDEV(ma, mi)   (((ma) << MINORBITS) | (mi))
1279
1280 static inline dev_t old_decode_dev(u16 val)
1281 {
1282         return MKDEV((val >> 8) & 255, val & 255);
1283 }
1284
1285 static inline dev_t new_decode_dev(u32 dev)
1286 {
1287         unsigned major = (dev & 0xfff00) >> 8;
1288         unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
1289         return MKDEV(major, minor);
1290 }
1291
1292 static int copy_inode_item(struct btrfs_inode_item *dst,
1293                            struct ext2_inode *src, u32 blocksize)
1294 {
1295         btrfs_set_stack_inode_generation(dst, 1);
1296         btrfs_set_stack_inode_sequence(dst, 0);
1297         btrfs_set_stack_inode_transid(dst, 1);
1298         btrfs_set_stack_inode_size(dst, src->i_size);
1299         btrfs_set_stack_inode_nbytes(dst, 0);
1300         btrfs_set_stack_inode_block_group(dst, 0);
1301         btrfs_set_stack_inode_nlink(dst, src->i_links_count);
1302         btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
1303         btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
1304         btrfs_set_stack_inode_mode(dst, src->i_mode);
1305         btrfs_set_stack_inode_rdev(dst, 0);
1306         btrfs_set_stack_inode_flags(dst, 0);
1307         btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
1308         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
1309         btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
1310         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
1311         btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
1312         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
1313         btrfs_set_stack_timespec_sec(&dst->otime, 0);
1314         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
1315
1316         if (S_ISDIR(src->i_mode)) {
1317                 btrfs_set_stack_inode_size(dst, 0);
1318                 btrfs_set_stack_inode_nlink(dst, 1);
1319         }
1320         if (S_ISREG(src->i_mode)) {
1321                 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
1322                                            (u64)src->i_size);
1323         }
1324         if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
1325             !S_ISLNK(src->i_mode)) {
1326                 if (src->i_block[0]) {
1327                         btrfs_set_stack_inode_rdev(dst,
1328                                 old_decode_dev(src->i_block[0]));
1329                 } else {
1330                         btrfs_set_stack_inode_rdev(dst,
1331                                 new_decode_dev(src->i_block[1]));
1332                 }
1333         }
1334         memset(&dst->reserved, 0, sizeof(dst->reserved));
1335
1336         return 0;
1337 }
1338
1339 /*
1340  * copy a single inode. do all the required works, such as cloning
1341  * inode item, creating file extents and creating directory entries.
1342  */
1343 static int copy_single_inode(struct btrfs_trans_handle *trans,
1344                              struct btrfs_root *root, u64 objectid,
1345                              ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
1346                              struct ext2_inode *ext2_inode,
1347                              int datacsum, int packing, int noxattr)
1348 {
1349         int ret;
1350         struct btrfs_inode_item btrfs_inode;
1351
1352         if (ext2_inode->i_links_count == 0)
1353                 return 0;
1354
1355         copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
1356         if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
1357                 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
1358                             BTRFS_INODE_NODATASUM;
1359                 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
1360         }
1361
1362         switch (ext2_inode->i_mode & S_IFMT) {
1363         case S_IFREG:
1364                 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
1365                                         ext2_fs, ext2_ino, datacsum, packing);
1366                 break;
1367         case S_IFDIR:
1368                 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1369                                          ext2_fs, ext2_ino);
1370                 break;
1371         case S_IFLNK:
1372                 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1373                                          ext2_fs, ext2_ino, ext2_inode);
1374                 break;
1375         default:
1376                 ret = 0;
1377                 break;
1378         }
1379         if (ret)
1380                 return ret;
1381
1382         if (!noxattr) {
1383                 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1384                                           ext2_fs, ext2_ino);
1385                 if (ret)
1386                         return ret;
1387         }
1388         return btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1389 }
1390
1391 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1392                             u64 src_bytenr, u32 num_bytes)
1393 {
1394         int ret;
1395         char *buffer;
1396         struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1397
1398         buffer = malloc(num_bytes);
1399         if (!buffer)
1400                 return -ENOMEM;
1401         ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1402         if (ret != num_bytes)
1403                 goto fail;
1404         ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1405         if (ret != num_bytes)
1406                 goto fail;
1407         ret = 0;
1408 fail:
1409         free(buffer);
1410         if (ret > 0)
1411                 ret = -1;
1412         return ret;
1413 }
1414 /*
1415  * scan ext2's inode bitmap and copy all used inodes.
1416  */
1417 static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
1418                             struct btrfs_root *root,
1419                             int datacsum, int packing, int noxattr, struct task_ctx *p)
1420 {
1421         ext2_filsys ext2_fs = cctx->fs_data;
1422         int ret;
1423         errcode_t err;
1424         ext2_inode_scan ext2_scan;
1425         struct ext2_inode ext2_inode;
1426         ext2_ino_t ext2_ino;
1427         u64 objectid;
1428         struct btrfs_trans_handle *trans;
1429
1430         trans = btrfs_start_transaction(root, 1);
1431         if (!trans)
1432                 return -ENOMEM;
1433         err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1434         if (err) {
1435                 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1436                 return -1;
1437         }
1438         while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1439                                              &ext2_inode))) {
1440                 /* no more inodes */
1441                 if (ext2_ino == 0)
1442                         break;
1443                 /* skip special inode in ext2fs */
1444                 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1445                     ext2_ino != EXT2_ROOT_INO)
1446                         continue;
1447                 objectid = ext2_ino + INO_OFFSET;
1448                 ret = copy_single_inode(trans, root,
1449                                         objectid, ext2_fs, ext2_ino,
1450                                         &ext2_inode, datacsum, packing,
1451                                         noxattr);
1452                 p->cur_copy_inodes++;
1453                 if (ret)
1454                         return ret;
1455                 if (trans->blocks_used >= 4096) {
1456                         ret = btrfs_commit_transaction(trans, root);
1457                         BUG_ON(ret);
1458                         trans = btrfs_start_transaction(root, 1);
1459                         BUG_ON(!trans);
1460                 }
1461         }
1462         if (err) {
1463                 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1464                 return -1;
1465         }
1466         ret = btrfs_commit_transaction(trans, root);
1467         BUG_ON(ret);
1468         ext2fs_close_inode_scan(ext2_scan);
1469
1470         return ret;
1471 }
1472
1473 static int ext2_test_block(struct btrfs_convert_context *cctx, u64 block)
1474 {
1475         ext2_filsys ext2_fs = cctx->fs_data;
1476
1477         BUG_ON(block != (u32)block);
1478         return ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block);
1479 }
1480
1481 /*
1482  * Construct a range of ext2fs image file.
1483  * scan block allocation bitmap, find all blocks used by the ext2fs
1484  * in this range and create file extents that point to these blocks.
1485  *
1486  * Note: Before calling the function, no file extent points to blocks
1487  *       in this range
1488  */
1489 static int create_image_file_range(struct btrfs_trans_handle *trans,
1490                                    struct btrfs_root *root, u64 objectid,
1491                                    struct btrfs_inode_item *inode,
1492                                    u64 start_byte, u64 end_byte,
1493                                    struct btrfs_convert_context *cctx, int datacsum)
1494 {
1495         u32 blocksize = cctx->blocksize;
1496         u32 block = start_byte / blocksize;
1497         u32 last_block = (end_byte + blocksize - 1) / blocksize;
1498         int ret = 0;
1499         struct blk_iterate_data data;
1500
1501         init_blk_iterate_data(&data, trans, root, inode, objectid, datacsum);
1502         data.first_block = block;
1503
1504         for (; start_byte < end_byte; block++, start_byte += blocksize) {
1505                 if (!convert_test_block(cctx, block))
1506                         continue;
1507                 ret = block_iterate_proc(block, block, &data);
1508                 if (ret < 0)
1509                         goto fail;
1510         }
1511         if (data.num_blocks > 0) {
1512                 ret = record_file_blocks(&data, data.first_block,
1513                                          data.disk_block, data.num_blocks);
1514                 if (ret)
1515                         goto fail;
1516                 data.first_block += data.num_blocks;
1517         }
1518         if (last_block > data.first_block) {
1519                 ret = record_file_blocks(&data, data.first_block, 0,
1520                                          last_block - data.first_block);
1521                 if (ret)
1522                         goto fail;
1523         }
1524 fail:
1525         return ret;
1526 }
1527
1528 /*
1529  * Create the fs image file.
1530  */
1531 static int create_image(struct btrfs_convert_context *cctx,
1532                         struct btrfs_root *root, const char *name, int datacsum)
1533 {
1534         int ret;
1535         struct btrfs_key key;
1536         struct btrfs_key location;
1537         struct btrfs_path path;
1538         struct btrfs_inode_item btrfs_inode;
1539         struct btrfs_inode_item *inode_item;
1540         struct extent_buffer *leaf;
1541         struct btrfs_fs_info *fs_info = root->fs_info;
1542         struct btrfs_root *extent_root = fs_info->extent_root;
1543         struct btrfs_trans_handle *trans;
1544         struct btrfs_extent_item *ei;
1545         struct btrfs_extent_inline_ref *iref;
1546         struct btrfs_extent_data_ref *dref;
1547         u64 bytenr;
1548         u64 num_bytes;
1549         u64 objectid;
1550         u64 last_byte;
1551         u64 first_free;
1552         u64 total_bytes;
1553         u64 flags = BTRFS_INODE_READONLY;
1554         u32 sectorsize = root->sectorsize;
1555
1556         total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1557         first_free =  BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1558         first_free &= ~((u64)sectorsize - 1);
1559         if (!datacsum)
1560                 flags |= BTRFS_INODE_NODATASUM;
1561
1562         memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1563         btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1564         btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1565         btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1566         btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1567         btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1568         btrfs_set_stack_inode_flags(&btrfs_inode,  flags);
1569         btrfs_init_path(&path);
1570         trans = btrfs_start_transaction(root, 1);
1571         BUG_ON(!trans);
1572
1573         objectid = btrfs_root_dirid(&root->root_item);
1574         ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1575         if (ret)
1576                 goto fail;
1577
1578         /*
1579          * copy blocks covered by extent #0 to new positions. extent #0 is
1580          * special, we can't rely on relocate_extents_range to relocate it.
1581          */
1582         for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1583                 ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
1584                 if (ret)
1585                         goto fail;
1586                 ret = copy_disk_extent(root, key.objectid, last_byte,
1587                                        sectorsize);
1588                 if (ret)
1589                         goto fail;
1590                 ret = btrfs_record_file_extent(trans, root, objectid,
1591                                                &btrfs_inode, last_byte,
1592                                                key.objectid, sectorsize);
1593                 if (ret)
1594                         goto fail;
1595                 if (datacsum) {
1596                         ret = csum_disk_extent(trans, root, key.objectid,
1597                                                sectorsize);
1598                         if (ret)
1599                                 goto fail;
1600                 }
1601         }
1602
1603         while(1) {
1604                 key.objectid = last_byte;
1605                 key.offset = 0;
1606                 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1607                 ret = btrfs_search_slot(trans, fs_info->extent_root,
1608                                         &key, &path, 0, 0);
1609                 if (ret < 0)
1610                         goto fail;
1611 next:
1612                 leaf = path.nodes[0];
1613                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1614                         ret = btrfs_next_leaf(extent_root, &path);
1615                         if (ret < 0)
1616                                 goto fail;
1617                         if (ret > 0)
1618                                 break;
1619                         leaf = path.nodes[0];
1620                 }
1621                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1622                 if (last_byte > key.objectid ||
1623                     key.type != BTRFS_EXTENT_ITEM_KEY) {
1624                         path.slots[0]++;
1625                         goto next;
1626                 }
1627
1628                 bytenr = key.objectid;
1629                 num_bytes = key.offset;
1630                 ei = btrfs_item_ptr(leaf, path.slots[0],
1631                                     struct btrfs_extent_item);
1632                 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1633                         path.slots[0]++;
1634                         goto next;
1635                 }
1636
1637                 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1638                        btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1639
1640                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1641                 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1642                 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1643                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1644                 if (btrfs_extent_data_ref_root(leaf, dref) !=
1645                     BTRFS_FS_TREE_OBJECTID) {
1646                         path.slots[0]++;
1647                         goto next;
1648                 }
1649
1650                 if (bytenr > last_byte) {
1651                         ret = create_image_file_range(trans, root, objectid,
1652                                                       &btrfs_inode, last_byte,
1653                                                       bytenr, cctx,
1654                                                       datacsum);
1655                         if (ret)
1656                                 goto fail;
1657                 }
1658                 ret = btrfs_record_file_extent(trans, root, objectid,
1659                                                &btrfs_inode, bytenr, bytenr,
1660                                                num_bytes);
1661                 if (ret)
1662                         goto fail;
1663                 last_byte = bytenr + num_bytes;
1664                 btrfs_release_path(&path);
1665
1666                 if (trans->blocks_used >= 4096) {
1667                         ret = btrfs_commit_transaction(trans, root);
1668                         BUG_ON(ret);
1669                         trans = btrfs_start_transaction(root, 1);
1670                         BUG_ON(!trans);
1671                 }
1672         }
1673         btrfs_release_path(&path);
1674         if (total_bytes > last_byte) {
1675                 ret = create_image_file_range(trans, root, objectid,
1676                                               &btrfs_inode, last_byte,
1677                                               total_bytes, cctx,
1678                                               datacsum);
1679                 if (ret)
1680                         goto fail;
1681         }
1682
1683         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1684         if (ret)
1685                 goto fail;
1686
1687         location.objectid = objectid;
1688         location.offset = 0;
1689         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1690         ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1691                                     btrfs_root_dirid(&root->root_item),
1692                                     &location, BTRFS_FT_REG_FILE, objectid);
1693         if (ret)
1694                 goto fail;
1695         ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1696                                      objectid,
1697                                      btrfs_root_dirid(&root->root_item),
1698                                      objectid);
1699         if (ret)
1700                 goto fail;
1701         location.objectid = btrfs_root_dirid(&root->root_item);
1702         location.offset = 0;
1703         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1704         ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1705         if (ret)
1706                 goto fail;
1707         leaf = path.nodes[0];
1708         inode_item = btrfs_item_ptr(leaf, path.slots[0],
1709                                     struct btrfs_inode_item);
1710         btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1711                              btrfs_inode_size(leaf, inode_item));
1712         btrfs_mark_buffer_dirty(leaf);
1713         btrfs_release_path(&path);
1714         ret = btrfs_commit_transaction(trans, root);
1715         BUG_ON(ret);
1716 fail:
1717         btrfs_release_path(&path);
1718         return ret;
1719 }
1720
1721 static int create_image_file_range_v2(struct btrfs_trans_handle *trans,
1722                                       struct btrfs_root *root,
1723                                       struct cache_tree *used,
1724                                       struct btrfs_inode_item *inode,
1725                                       u64 ino, u64 bytenr, u64 *ret_len,
1726                                       int datacsum)
1727 {
1728         struct cache_extent *cache;
1729         struct btrfs_block_group_cache *bg_cache;
1730         u64 len = *ret_len;
1731         u64 disk_bytenr;
1732         int ret;
1733
1734         BUG_ON(bytenr != round_down(bytenr, root->sectorsize));
1735         BUG_ON(len != round_down(len, root->sectorsize));
1736         len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE);
1737
1738         cache = search_cache_extent(used, bytenr);
1739         if (cache) {
1740                 if (cache->start <= bytenr) {
1741                         /*
1742                          * |///////Used///////|
1743                          *      |<--insert--->|
1744                          *      bytenr
1745                          */
1746                         len = min_t(u64, len, cache->start + cache->size -
1747                                     bytenr);
1748                         disk_bytenr = bytenr;
1749                 } else {
1750                         /*
1751                          *              |//Used//|
1752                          *  |<-insert-->|
1753                          *  bytenr
1754                          */
1755                         len = min(len, cache->start - bytenr);
1756                         disk_bytenr = 0;
1757                         datacsum = 0;
1758                 }
1759         } else {
1760                 /*
1761                  * |//Used//|           |EOF
1762                  *          |<-insert-->|
1763                  *          bytenr
1764                  */
1765                 disk_bytenr = 0;
1766                 datacsum = 0;
1767         }
1768
1769         if (disk_bytenr) {
1770                 /* Check if the range is in a data block group */
1771                 bg_cache = btrfs_lookup_block_group(root->fs_info, bytenr);
1772                 if (!bg_cache)
1773                         return -ENOENT;
1774                 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
1775                         return -EINVAL;
1776
1777                 /* The extent should never cross block group boundary */
1778                 len = min_t(u64, len, bg_cache->key.objectid +
1779                             bg_cache->key.offset - bytenr);
1780         }
1781
1782         BUG_ON(len != round_down(len, root->sectorsize));
1783         ret = btrfs_record_file_extent(trans, root, ino, inode, bytenr,
1784                                        disk_bytenr, len);
1785         if (ret < 0)
1786                 return ret;
1787
1788         if (datacsum)
1789                 ret = csum_disk_extent(trans, root, bytenr, len);
1790         *ret_len = len;
1791         return ret;
1792 }
1793
1794
1795 /*
1796  * Relocate old fs data in one reserved ranges
1797  *
1798  * Since all old fs data in reserved range is not covered by any chunk nor
1799  * data extent, we don't need to handle any reference but add new
1800  * extent/reference, which makes codes more clear
1801  */
1802 static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
1803                                       struct btrfs_root *root,
1804                                       struct cache_tree *used,
1805                                       struct btrfs_inode_item *inode, int fd,
1806                                       u64 ino, u64 start, u64 len, int datacsum)
1807 {
1808         u64 cur_off = start;
1809         u64 cur_len = len;
1810         struct cache_extent *cache;
1811         struct btrfs_key key;
1812         struct extent_buffer *eb;
1813         int ret = 0;
1814
1815         while (cur_off < start + len) {
1816                 cache = lookup_cache_extent(used, cur_off, cur_len);
1817                 if (!cache)
1818                         break;
1819                 cur_off = max(cache->start, cur_off);
1820                 cur_len = min(cache->start + cache->size, start + len) -
1821                           cur_off;
1822                 BUG_ON(cur_len < root->sectorsize);
1823
1824                 /* reserve extent for the data */
1825                 ret = btrfs_reserve_extent(trans, root, cur_len, 0, 0, (u64)-1,
1826                                            &key, 1);
1827                 if (ret < 0)
1828                         break;
1829
1830                 eb = malloc(sizeof(*eb) + cur_len);
1831                 if (!eb) {
1832                         ret = -ENOMEM;
1833                         break;
1834                 }
1835
1836                 ret = pread(fd, eb->data, cur_len, cur_off);
1837                 if (ret < cur_len) {
1838                         ret = (ret < 0 ? ret : -EIO);
1839                         free(eb);
1840                         break;
1841                 }
1842                 eb->start = key.objectid;
1843                 eb->len = key.offset;
1844
1845                 /* Write the data */
1846                 ret = write_and_map_eb(trans, root, eb);
1847                 free(eb);
1848                 if (ret < 0)
1849                         break;
1850
1851                 /* Now handle extent item and file extent things */
1852                 ret = btrfs_record_file_extent(trans, root, ino, inode, cur_off,
1853                                                key.objectid, key.offset);
1854                 if (ret < 0)
1855                         break;
1856                 /* Finally, insert csum items */
1857                 if (datacsum)
1858                         ret = csum_disk_extent(trans, root, key.objectid,
1859                                                key.offset);
1860
1861                 cur_off += key.offset;
1862                 cur_len = start + len - cur_off;
1863         }
1864         return ret;
1865 }
1866
1867 /*
1868  * Relocate the used ext2 data in reserved ranges
1869  * [0,1M)
1870  * [btrfs_sb_offset(1), +BTRFS_STRIPE_LEN)
1871  * [btrfs_sb_offset(2), +BTRFS_STRIPE_LEN)
1872  */
1873 static int migrate_reserved_ranges(struct btrfs_trans_handle *trans,
1874                                    struct btrfs_root *root,
1875                                    struct cache_tree *used,
1876                                    struct btrfs_inode_item *inode, int fd,
1877                                    u64 ino, u64 total_bytes, int datacsum)
1878 {
1879         u64 cur_off;
1880         u64 cur_len;
1881         int ret = 0;
1882
1883         /* 0 ~ 1M */
1884         cur_off = 0;
1885         cur_len = 1024 * 1024;
1886         ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
1887                                          cur_off, cur_len, datacsum);
1888         if (ret < 0)
1889                 return ret;
1890
1891         /* second sb(fisrt sb is included in 0~1M) */
1892         cur_off = btrfs_sb_offset(1);
1893         cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
1894         if (cur_off < total_bytes)
1895                 return ret;
1896         ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
1897                                          cur_off, cur_len, datacsum);
1898         if (ret < 0)
1899                 return ret;
1900
1901         /* Last sb */
1902         cur_off = btrfs_sb_offset(2);
1903         cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
1904         if (cur_off < total_bytes)
1905                 return ret;
1906         ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
1907                                          cur_off, cur_len, datacsum);
1908         return ret;
1909 }
1910
1911 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
1912                                 int ensure_size);
1913
1914 /*
1915  * Create the fs image file of old filesystem.
1916  *
1917  * This is completely fs independent as we have cctx->used, only
1918  * need to create file extents pointing to all the positions.
1919  */
1920 static int create_image_v2(struct btrfs_root *root,
1921                            struct btrfs_mkfs_config *cfg,
1922                            struct btrfs_convert_context *cctx, int fd,
1923                            u64 size, char *name, int datacsum)
1924 {
1925         struct btrfs_inode_item buf;
1926         struct btrfs_trans_handle *trans;
1927         struct btrfs_path *path = NULL;
1928         struct btrfs_key key;
1929         struct cache_extent *cache;
1930         struct cache_tree used_tmp;
1931         u64 cur;
1932         u64 ino;
1933         int ret;
1934
1935         trans = btrfs_start_transaction(root, 1);
1936         if (!trans)
1937                 return -ENOMEM;
1938
1939         cache_tree_init(&used_tmp);
1940
1941         ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
1942                                        &ino);
1943         if (ret < 0)
1944                 goto out;
1945         ret = btrfs_new_inode(trans, root, ino, 0600 | S_IFREG);
1946         if (ret < 0)
1947                 goto out;
1948         ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
1949                              strlen(name), BTRFS_FT_REG_FILE, NULL, 1);
1950         if (ret < 0)
1951                 goto out;
1952
1953         path = btrfs_alloc_path();
1954         if (!path) {
1955                 ret = -ENOMEM;
1956                 goto out;
1957         }
1958         key.objectid = ino;
1959         key.type = BTRFS_INODE_ITEM_KEY;
1960         key.offset = 0;
1961
1962         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1963         if (ret) {
1964                 ret = (ret > 0 ? -ENOENT : ret);
1965                 goto out;
1966         }
1967         read_extent_buffer(path->nodes[0], &buf,
1968                         btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
1969                         sizeof(buf));
1970         btrfs_release_path(path);
1971
1972         /*
1973          * Create a new used space cache, which doesn't contain the reserved
1974          * range
1975          */
1976         for (cache = first_cache_extent(&cctx->used); cache;
1977              cache = next_cache_extent(cache)) {
1978                 ret = add_cache_extent(&used_tmp, cache->start, cache->size);
1979                 if (ret < 0)
1980                         goto out;
1981         }
1982         ret = wipe_reserved_ranges(&used_tmp, 0, 0);
1983         if (ret < 0)
1984                 goto out;
1985
1986         /*
1987          * Start from 1M, as 0~1M is reserved, and create_image_file_range_v2()
1988          * can't handle bytenr 0(will consider it as a hole)
1989          */
1990         cur = 1024 * 1024;
1991         while (cur < size) {
1992                 u64 len = size - cur;
1993
1994                 ret = create_image_file_range_v2(trans, root, &used_tmp,
1995                                                 &buf, ino, cur, &len, datacsum);
1996                 if (ret < 0)
1997                         goto out;
1998                 cur += len;
1999         }
2000         /* Handle the reserved ranges */
2001         ret = migrate_reserved_ranges(trans, root, &cctx->used, &buf, fd, ino,
2002                                       cfg->num_bytes, datacsum);
2003
2004
2005         key.objectid = ino;
2006         key.type = BTRFS_INODE_ITEM_KEY;
2007         key.offset = 0;
2008         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2009         if (ret) {
2010                 ret = (ret > 0 ? -ENOENT : ret);
2011                 goto out;
2012         }
2013         btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
2014         write_extent_buffer(path->nodes[0], &buf,
2015                         btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
2016                         sizeof(buf));
2017 out:
2018         free_extent_cache_tree(&used_tmp);
2019         btrfs_free_path(path);
2020         btrfs_commit_transaction(trans, root);
2021         return ret;
2022 }
2023
2024 static struct btrfs_root * link_subvol(struct btrfs_root *root,
2025                 const char *base, u64 root_objectid)
2026 {
2027         struct btrfs_trans_handle *trans;
2028         struct btrfs_fs_info *fs_info = root->fs_info;
2029         struct btrfs_root *tree_root = fs_info->tree_root;
2030         struct btrfs_root *new_root = NULL;
2031         struct btrfs_path *path;
2032         struct btrfs_inode_item *inode_item;
2033         struct extent_buffer *leaf;
2034         struct btrfs_key key;
2035         u64 dirid = btrfs_root_dirid(&root->root_item);
2036         u64 index = 2;
2037         char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
2038         int len;
2039         int i;
2040         int ret;
2041
2042         len = strlen(base);
2043         if (len == 0 || len > BTRFS_NAME_LEN)
2044                 return NULL;
2045
2046         path = btrfs_alloc_path();
2047         BUG_ON(!path);
2048
2049         key.objectid = dirid;
2050         key.type = BTRFS_DIR_INDEX_KEY;
2051         key.offset = (u64)-1;
2052
2053         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2054         BUG_ON(ret <= 0);
2055
2056         if (path->slots[0] > 0) {
2057                 path->slots[0]--;
2058                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2059                 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
2060                         index = key.offset + 1;
2061         }
2062         btrfs_release_path(path);
2063
2064         trans = btrfs_start_transaction(root, 1);
2065         BUG_ON(!trans);
2066
2067         key.objectid = dirid;
2068         key.offset = 0;
2069         key.type =  BTRFS_INODE_ITEM_KEY;
2070
2071         ret = btrfs_lookup_inode(trans, root, path, &key, 1);
2072         BUG_ON(ret);
2073         leaf = path->nodes[0];
2074         inode_item = btrfs_item_ptr(leaf, path->slots[0],
2075                                     struct btrfs_inode_item);
2076
2077         key.objectid = root_objectid;
2078         key.offset = (u64)-1;
2079         key.type = BTRFS_ROOT_ITEM_KEY;
2080
2081         memcpy(buf, base, len);
2082         for (i = 0; i < 1024; i++) {
2083                 ret = btrfs_insert_dir_item(trans, root, buf, len,
2084                                             dirid, &key, BTRFS_FT_DIR, index);
2085                 if (ret != -EEXIST)
2086                         break;
2087                 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
2088                 if (len < 1 || len > BTRFS_NAME_LEN) {
2089                         ret = -EINVAL;
2090                         break;
2091                 }
2092         }
2093         if (ret)
2094                 goto fail;
2095
2096         btrfs_set_inode_size(leaf, inode_item, len * 2 +
2097                              btrfs_inode_size(leaf, inode_item));
2098         btrfs_mark_buffer_dirty(leaf);
2099         btrfs_release_path(path);
2100
2101         /* add the backref first */
2102         ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
2103                                  BTRFS_ROOT_BACKREF_KEY,
2104                                  root->root_key.objectid,
2105                                  dirid, index, buf, len);
2106         BUG_ON(ret);
2107
2108         /* now add the forward ref */
2109         ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
2110                                  BTRFS_ROOT_REF_KEY, root_objectid,
2111                                  dirid, index, buf, len);
2112
2113         ret = btrfs_commit_transaction(trans, root);
2114         BUG_ON(ret);
2115
2116         new_root = btrfs_read_fs_root(fs_info, &key);
2117         if (IS_ERR(new_root))
2118                 new_root = NULL;
2119 fail:
2120         btrfs_free_path(path);
2121         return new_root;
2122 }
2123
2124 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
2125                                 struct btrfs_root *root)
2126 {
2127         struct btrfs_fs_info *info = root->fs_info;
2128         struct btrfs_root *chunk_root = info->chunk_root;
2129         struct btrfs_root *extent_root = info->extent_root;
2130         struct btrfs_device *device;
2131         struct btrfs_block_group_cache *cache;
2132         struct btrfs_dev_extent *extent;
2133         struct extent_buffer *leaf;
2134         struct btrfs_chunk chunk;
2135         struct btrfs_key key;
2136         struct btrfs_path path;
2137         u64 cur_start;
2138         u64 total_bytes;
2139         u64 chunk_objectid;
2140         int ret;
2141
2142         btrfs_init_path(&path);
2143
2144         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2145         chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2146
2147         BUG_ON(list_empty(&info->fs_devices->devices));
2148         device = list_entry(info->fs_devices->devices.next,
2149                             struct btrfs_device, dev_list);
2150         BUG_ON(device->devid != info->fs_devices->latest_devid);
2151
2152         /* delete device extent created by make_btrfs */
2153         key.objectid = device->devid;
2154         key.offset = 0;
2155         key.type = BTRFS_DEV_EXTENT_KEY;
2156         ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
2157         if (ret < 0)
2158                 goto err;
2159
2160         BUG_ON(ret > 0);
2161         ret = btrfs_del_item(trans, device->dev_root, &path);
2162         if (ret)
2163                 goto err;
2164         btrfs_release_path(&path);
2165
2166         /* delete chunk item created by make_btrfs */
2167         key.objectid = chunk_objectid;
2168         key.offset = 0;
2169         key.type = BTRFS_CHUNK_ITEM_KEY;
2170         ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
2171         if (ret < 0)
2172                 goto err;
2173
2174         BUG_ON(ret > 0);
2175         ret = btrfs_del_item(trans, chunk_root, &path);
2176         if (ret)
2177                 goto err;
2178         btrfs_release_path(&path);
2179
2180         /* for each block group, create device extent and chunk item */
2181         cur_start = 0;
2182         while (cur_start < total_bytes) {
2183                 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2184                 BUG_ON(!cache);
2185
2186                 /* insert device extent */
2187                 key.objectid = device->devid;
2188                 key.offset = cache->key.objectid;
2189                 key.type = BTRFS_DEV_EXTENT_KEY;
2190                 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
2191                                               &key, sizeof(*extent));
2192                 if (ret)
2193                         goto err;
2194
2195                 leaf = path.nodes[0];
2196                 extent = btrfs_item_ptr(leaf, path.slots[0],
2197                                         struct btrfs_dev_extent);
2198
2199                 btrfs_set_dev_extent_chunk_tree(leaf, extent,
2200                                                 chunk_root->root_key.objectid);
2201                 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
2202                                                     chunk_objectid);
2203                 btrfs_set_dev_extent_chunk_offset(leaf, extent,
2204                                                   cache->key.objectid);
2205                 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
2206                 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
2207                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
2208                     BTRFS_UUID_SIZE);
2209                 btrfs_mark_buffer_dirty(leaf);
2210                 btrfs_release_path(&path);
2211
2212                 /* insert chunk item */
2213                 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
2214                 btrfs_set_stack_chunk_owner(&chunk,
2215                                             extent_root->root_key.objectid);
2216                 btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
2217                 btrfs_set_stack_chunk_type(&chunk, cache->flags);
2218                 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
2219                 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
2220                 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
2221                 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
2222                 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
2223                 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
2224                 btrfs_set_stack_stripe_offset(&chunk.stripe,
2225                                               cache->key.objectid);
2226                 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2227
2228                 key.objectid = chunk_objectid;
2229                 key.offset = cache->key.objectid;
2230                 key.type = BTRFS_CHUNK_ITEM_KEY;
2231
2232                 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
2233                                         btrfs_chunk_item_size(1));
2234                 if (ret)
2235                         goto err;
2236
2237                 cur_start = cache->key.objectid + cache->key.offset;
2238         }
2239
2240         device->bytes_used = total_bytes;
2241         ret = btrfs_update_device(trans, device);
2242 err:
2243         btrfs_release_path(&path);
2244         return ret;
2245 }
2246
2247 static int create_subvol(struct btrfs_trans_handle *trans,
2248                          struct btrfs_root *root, u64 root_objectid)
2249 {
2250         struct extent_buffer *tmp;
2251         struct btrfs_root *new_root;
2252         struct btrfs_key key;
2253         struct btrfs_root_item root_item;
2254         int ret;
2255
2256         ret = btrfs_copy_root(trans, root, root->node, &tmp,
2257                               root_objectid);
2258         BUG_ON(ret);
2259
2260         memcpy(&root_item, &root->root_item, sizeof(root_item));
2261         btrfs_set_root_bytenr(&root_item, tmp->start);
2262         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
2263         btrfs_set_root_generation(&root_item, trans->transid);
2264         free_extent_buffer(tmp);
2265
2266         key.objectid = root_objectid;
2267         key.type = BTRFS_ROOT_ITEM_KEY;
2268         key.offset = trans->transid;
2269         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
2270                                 &key, &root_item);
2271
2272         key.offset = (u64)-1;
2273         new_root = btrfs_read_fs_root(root->fs_info, &key);
2274         BUG_ON(!new_root || IS_ERR(new_root));
2275
2276         ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
2277         BUG_ON(ret);
2278
2279         return 0;
2280 }
2281
2282 /*
2283  * New make_btrfs_v2() has handle system and meta chunks quite well.
2284  * So only need to add remaining data chunks.
2285  */
2286 static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
2287                                           struct btrfs_fs_info *fs_info,
2288                                           struct btrfs_mkfs_config *cfg,
2289                                           struct btrfs_convert_context *cctx)
2290 {
2291         struct btrfs_root *extent_root = fs_info->extent_root;
2292         struct cache_tree *data_chunks = &cctx->data_chunks;
2293         struct cache_extent *cache;
2294         u64 max_chunk_size;
2295         int ret = 0;
2296
2297         /*
2298          * Don't create data chunk over 10% of the convert device
2299          * And for single chunk, don't create chunk larger than 1G.
2300          */
2301         max_chunk_size = cfg->num_bytes / 10;
2302         max_chunk_size = min((u64)(1024 * 1024 * 1024), max_chunk_size);
2303         max_chunk_size = round_down(max_chunk_size, extent_root->sectorsize);
2304
2305         for (cache = first_cache_extent(data_chunks); cache;
2306              cache = next_cache_extent(cache)) {
2307                 u64 cur = cache->start;
2308
2309                 while (cur < cache->start + cache->size) {
2310                         u64 len;
2311                         u64 cur_backup = cur;
2312
2313                         len = min(max_chunk_size,
2314                                   cache->start + cache->size - cur);
2315                         ret = btrfs_alloc_data_chunk(trans, extent_root,
2316                                         &cur_backup, len,
2317                                         BTRFS_BLOCK_GROUP_DATA, 1);
2318                         if (ret < 0)
2319                                 break;
2320                         ret = btrfs_make_block_group(trans, extent_root, 0,
2321                                         BTRFS_BLOCK_GROUP_DATA,
2322                                         BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2323                                         cur, len);
2324                         if (ret < 0)
2325                                 break;
2326                         cur += len;
2327                 }
2328         }
2329         return ret;
2330 }
2331
2332 /*
2333  * Init the temp btrfs to a operational status.
2334  *
2335  * It will fix the extent usage accounting(XXX: Do we really need?) and
2336  * insert needed data chunks, to ensure all old fs data extents are covered
2337  * by DATA chunks, preventing wrong chunks are allocated.
2338  *
2339  * And also create convert image subvolume and relocation tree.
2340  * (XXX: Not need again?)
2341  * But the convert image subvolume is *NOT* linked to fs tree yet.
2342  */
2343 static int init_btrfs_v2(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
2344                          struct btrfs_convert_context *cctx, int datacsum,
2345                          int packing, int noxattr)
2346 {
2347         struct btrfs_key location;
2348         struct btrfs_trans_handle *trans;
2349         struct btrfs_fs_info *fs_info = root->fs_info;
2350         int ret;
2351
2352         trans = btrfs_start_transaction(root, 1);
2353         BUG_ON(!trans);
2354         ret = btrfs_fix_block_accounting(trans, root);
2355         if (ret)
2356                 goto err;
2357         ret = make_convert_data_block_groups(trans, fs_info, cfg, cctx);
2358         if (ret)
2359                 goto err;
2360         ret = btrfs_make_root_dir(trans, fs_info->tree_root,
2361                                   BTRFS_ROOT_TREE_DIR_OBJECTID);
2362         if (ret)
2363                 goto err;
2364         memcpy(&location, &root->root_key, sizeof(location));
2365         location.offset = (u64)-1;
2366         ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
2367                                 btrfs_super_root_dir(fs_info->super_copy),
2368                                 &location, BTRFS_FT_DIR, 0);
2369         if (ret)
2370                 goto err;
2371         ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
2372                                 location.objectid,
2373                                 btrfs_super_root_dir(fs_info->super_copy), 0);
2374         if (ret)
2375                 goto err;
2376         btrfs_set_root_dirid(&fs_info->fs_root->root_item,
2377                              BTRFS_FIRST_FREE_OBJECTID);
2378
2379         /* subvol for fs image file */
2380         ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
2381         if (ret < 0)
2382                 goto err;
2383         /* subvol for data relocation tree */
2384         ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
2385         if (ret < 0)
2386                 goto err;
2387
2388         ret = btrfs_commit_transaction(trans, root);
2389 err:
2390         return ret;
2391 }
2392
2393 static int init_btrfs(struct btrfs_root *root)
2394 {
2395         int ret;
2396         struct btrfs_key location;
2397         struct btrfs_trans_handle *trans;
2398         struct btrfs_fs_info *fs_info = root->fs_info;
2399         struct extent_buffer *tmp;
2400
2401         trans = btrfs_start_transaction(root, 1);
2402         BUG_ON(!trans);
2403         ret = btrfs_make_block_groups(trans, root);
2404         if (ret)
2405                 goto err;
2406         ret = btrfs_fix_block_accounting(trans, root);
2407         if (ret)
2408                 goto err;
2409         ret = create_chunk_mapping(trans, root);
2410         if (ret)
2411                 goto err;
2412         ret = btrfs_make_root_dir(trans, fs_info->tree_root,
2413                                   BTRFS_ROOT_TREE_DIR_OBJECTID);
2414         if (ret)
2415                 goto err;
2416         memcpy(&location, &root->root_key, sizeof(location));
2417         location.offset = (u64)-1;
2418         ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
2419                                 btrfs_super_root_dir(fs_info->super_copy),
2420                                 &location, BTRFS_FT_DIR, 0);
2421         if (ret)
2422                 goto err;
2423         ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
2424                                 location.objectid,
2425                                 btrfs_super_root_dir(fs_info->super_copy), 0);
2426         if (ret)
2427                 goto err;
2428         btrfs_set_root_dirid(&fs_info->fs_root->root_item,
2429                              BTRFS_FIRST_FREE_OBJECTID);
2430
2431         /* subvol for fs image file */
2432         ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
2433         BUG_ON(ret);
2434         /* subvol for data relocation */
2435         ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
2436         BUG_ON(ret);
2437
2438         extent_buffer_get(fs_info->csum_root->node);
2439         ret = __btrfs_cow_block(trans, fs_info->csum_root,
2440                                 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
2441         BUG_ON(ret);
2442         free_extent_buffer(tmp);
2443
2444         ret = btrfs_commit_transaction(trans, root);
2445         BUG_ON(ret);
2446 err:
2447         return ret;
2448 }
2449
2450 /*
2451  * Migrate super block to its default position and zero 0 ~ 16k
2452  */
2453 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
2454 {
2455         int ret;
2456         struct extent_buffer *buf;
2457         struct btrfs_super_block *super;
2458         u32 len;
2459         u32 bytenr;
2460
2461         BUG_ON(sectorsize < sizeof(*super));
2462         buf = malloc(sizeof(*buf) + sectorsize);
2463         if (!buf)
2464                 return -ENOMEM;
2465
2466         buf->len = sectorsize;
2467         ret = pread(fd, buf->data, sectorsize, old_bytenr);
2468         if (ret != sectorsize)
2469                 goto fail;
2470
2471         super = (struct btrfs_super_block *)buf->data;
2472         BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
2473         btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
2474
2475         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2476         ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2477         if (ret != sectorsize)
2478                 goto fail;
2479
2480         ret = fsync(fd);
2481         if (ret)
2482                 goto fail;
2483
2484         memset(buf->data, 0, sectorsize);
2485         for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
2486                 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
2487                 if (len > sectorsize)
2488                         len = sectorsize;
2489                 ret = pwrite(fd, buf->data, len, bytenr);
2490                 if (ret != len) {
2491                         fprintf(stderr, "unable to zero fill device\n");
2492                         break;
2493                 }
2494                 bytenr += len;
2495         }
2496         ret = 0;
2497         fsync(fd);
2498 fail:
2499         free(buf);
2500         if (ret > 0)
2501                 ret = -1;
2502         return ret;
2503 }
2504
2505 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
2506 {
2507         struct btrfs_chunk *chunk;
2508         struct btrfs_disk_key *key;
2509         u32 sectorsize = btrfs_super_sectorsize(super);
2510
2511         key = (struct btrfs_disk_key *)(super->sys_chunk_array);
2512         chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
2513                                        sizeof(struct btrfs_disk_key));
2514
2515         btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2516         btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
2517         btrfs_set_disk_key_offset(key, 0);
2518
2519         btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
2520         btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
2521         btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
2522         btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
2523         btrfs_set_stack_chunk_io_align(chunk, sectorsize);
2524         btrfs_set_stack_chunk_io_width(chunk, sectorsize);
2525         btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
2526         btrfs_set_stack_chunk_num_stripes(chunk, 1);
2527         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
2528         chunk->stripe.devid = super->dev_item.devid;
2529         btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
2530         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
2531         btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
2532         return 0;
2533 }
2534
2535 static int prepare_system_chunk(int fd, u64 sb_bytenr)
2536 {
2537         int ret;
2538         struct extent_buffer *buf;
2539         struct btrfs_super_block *super;
2540
2541         BUG_ON(BTRFS_SUPER_INFO_SIZE < sizeof(*super));
2542         buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
2543         if (!buf)
2544                 return -ENOMEM;
2545
2546         buf->len = BTRFS_SUPER_INFO_SIZE;
2547         ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2548         if (ret != BTRFS_SUPER_INFO_SIZE)
2549                 goto fail;
2550
2551         super = (struct btrfs_super_block *)buf->data;
2552         BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
2553         BUG_ON(btrfs_super_num_devices(super) != 1);
2554
2555         ret = prepare_system_chunk_sb(super);
2556         if (ret)
2557                 goto fail;
2558
2559         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2560         ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2561         if (ret != BTRFS_SUPER_INFO_SIZE)
2562                 goto fail;
2563
2564         ret = 0;
2565 fail:
2566         free(buf);
2567         if (ret > 0)
2568                 ret = -1;
2569         return ret;
2570 }
2571
2572 static int relocate_one_reference(struct btrfs_trans_handle *trans,
2573                                   struct btrfs_root *root,
2574                                   u64 extent_start, u64 extent_size,
2575                                   struct btrfs_key *extent_key,
2576                                   struct extent_io_tree *reloc_tree)
2577 {
2578         struct extent_buffer *leaf;
2579         struct btrfs_file_extent_item *fi;
2580         struct btrfs_key key;
2581         struct btrfs_path path;
2582         struct btrfs_inode_item inode;
2583         struct blk_iterate_data data;
2584         u64 bytenr;
2585         u64 num_bytes;
2586         u64 cur_offset;
2587         u64 new_pos;
2588         u64 nbytes;
2589         u64 sector_end;
2590         u32 sectorsize = root->sectorsize;
2591         unsigned long ptr;
2592         int datacsum;
2593         int fd;
2594         int ret;
2595
2596         btrfs_init_path(&path);
2597         ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
2598         if (ret)
2599                 goto fail;
2600
2601         leaf = path.nodes[0];
2602         fi = btrfs_item_ptr(leaf, path.slots[0],
2603                             struct btrfs_file_extent_item);
2604         BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
2605         if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
2606             extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
2607                 ret = 1;
2608                 goto fail;
2609         }
2610
2611         bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
2612         num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2613
2614         ret = btrfs_del_item(trans, root, &path);
2615         if (ret)
2616                 goto fail;
2617
2618         ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
2619                                 root->root_key.objectid,
2620                                 extent_key->objectid, extent_key->offset);
2621         if (ret)
2622                 goto fail;
2623
2624         btrfs_release_path(&path);
2625
2626         key.objectid = extent_key->objectid;
2627         key.offset = 0;
2628         key.type =  BTRFS_INODE_ITEM_KEY;
2629         ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
2630         if (ret)
2631                 goto fail;
2632
2633         leaf = path.nodes[0];
2634         ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2635         read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2636         btrfs_release_path(&path);
2637
2638         BUG_ON(num_bytes & (sectorsize - 1));
2639         nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
2640         btrfs_set_stack_inode_nbytes(&inode, nbytes);
2641         datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
2642
2643         init_blk_iterate_data(&data, trans, root, &inode, extent_key->objectid,
2644                               datacsum);
2645         data.first_block = extent_key->offset;
2646
2647         cur_offset = extent_key->offset;
2648         while (num_bytes > 0) {
2649                 sector_end = bytenr + sectorsize - 1;
2650                 if (test_range_bit(reloc_tree, bytenr, sector_end,
2651                                    EXTENT_LOCKED, 1)) {
2652                         ret = get_state_private(reloc_tree, bytenr, &new_pos);
2653                         BUG_ON(ret);
2654                 } else {
2655                         ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
2656                         if (ret)
2657                                 goto fail;
2658                         new_pos = key.objectid;
2659
2660                         if (cur_offset == extent_key->offset) {
2661                                 fd = root->fs_info->fs_devices->latest_bdev;
2662                                 readahead(fd, bytenr, num_bytes);
2663                         }
2664                         ret = copy_disk_extent(root, new_pos, bytenr,
2665                                                sectorsize);
2666                         if (ret)
2667                                 goto fail;
2668                         ret = set_extent_bits(reloc_tree, bytenr, sector_end,
2669                                               EXTENT_LOCKED, GFP_NOFS);
2670                         BUG_ON(ret);
2671                         ret = set_state_private(reloc_tree, bytenr, new_pos);
2672                         BUG_ON(ret);
2673                 }
2674
2675                 ret = block_iterate_proc(new_pos / sectorsize,
2676                                          cur_offset / sectorsize, &data);
2677                 if (ret < 0)
2678                         goto fail;
2679
2680                 cur_offset += sectorsize;
2681                 bytenr += sectorsize;
2682                 num_bytes -= sectorsize;
2683         }
2684
2685         if (data.num_blocks > 0) {
2686                 ret = record_file_blocks(&data, data.first_block,
2687                                          data.disk_block, data.num_blocks);
2688                 if (ret)
2689                         goto fail;
2690         }
2691
2692         key.objectid = extent_key->objectid;
2693         key.offset = 0;
2694         key.type =  BTRFS_INODE_ITEM_KEY;
2695         ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
2696         if (ret)
2697                 goto fail;
2698
2699         leaf = path.nodes[0];
2700         ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2701         write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2702         btrfs_mark_buffer_dirty(leaf);
2703         btrfs_release_path(&path);
2704
2705 fail:
2706         btrfs_release_path(&path);
2707         return ret;
2708 }
2709
2710 static int relocate_extents_range(struct btrfs_root *fs_root,
2711                                   struct btrfs_root *image_root,
2712                                   u64 start_byte, u64 end_byte)
2713 {
2714         struct btrfs_fs_info *info = fs_root->fs_info;
2715         struct btrfs_root *extent_root = info->extent_root;
2716         struct btrfs_root *cur_root = NULL;
2717         struct btrfs_trans_handle *trans;
2718         struct btrfs_extent_data_ref *dref;
2719         struct btrfs_extent_inline_ref *iref;
2720         struct btrfs_extent_item *ei;
2721         struct extent_buffer *leaf;
2722         struct btrfs_key key;
2723         struct btrfs_key extent_key;
2724         struct btrfs_path path;
2725         struct extent_io_tree reloc_tree;
2726         unsigned long ptr;
2727         unsigned long end;
2728         u64 cur_byte;
2729         u64 num_bytes;
2730         u64 ref_root;
2731         u64 num_extents;
2732         int pass = 0;
2733         int ret;
2734
2735         btrfs_init_path(&path);
2736         extent_io_tree_init(&reloc_tree);
2737
2738         key.objectid = start_byte;
2739         key.offset = 0;
2740         key.type = BTRFS_EXTENT_ITEM_KEY;
2741         ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2742         if (ret < 0)
2743                 goto fail;
2744         if (ret > 0) {
2745                 ret = btrfs_previous_item(extent_root, &path, 0,
2746                                           BTRFS_EXTENT_ITEM_KEY);
2747                 if (ret < 0)
2748                         goto fail;
2749                 if (ret == 0) {
2750                         leaf = path.nodes[0];
2751                         btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2752                         if (key.objectid + key.offset > start_byte)
2753                                 start_byte = key.objectid;
2754                 }
2755         }
2756         btrfs_release_path(&path);
2757 again:
2758         cur_root = (pass % 2 == 0) ? image_root : fs_root;
2759         num_extents = 0;
2760
2761         trans = btrfs_start_transaction(cur_root, 1);
2762         BUG_ON(!trans);
2763
2764         cur_byte = start_byte;
2765         while (1) {
2766                 key.objectid = cur_byte;
2767                 key.offset = 0;
2768                 key.type = BTRFS_EXTENT_ITEM_KEY;
2769                 ret = btrfs_search_slot(trans, extent_root,
2770                                         &key, &path, 0, 0);
2771                 if (ret < 0)
2772                         goto fail;
2773 next:
2774                 leaf = path.nodes[0];
2775                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2776                         ret = btrfs_next_leaf(extent_root, &path);
2777                         if (ret < 0)
2778                                 goto fail;
2779                         if (ret > 0)
2780                                 break;
2781                         leaf = path.nodes[0];
2782                 }
2783
2784                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2785                 if (key.objectid < cur_byte ||
2786                     key.type != BTRFS_EXTENT_ITEM_KEY) {
2787                         path.slots[0]++;
2788                         goto next;
2789                 }
2790                 if (key.objectid >= end_byte)
2791                         break;
2792
2793                 num_extents++;
2794
2795                 cur_byte = key.objectid;
2796                 num_bytes = key.offset;
2797                 ei = btrfs_item_ptr(leaf, path.slots[0],
2798                                     struct btrfs_extent_item);
2799                 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2800                          BTRFS_EXTENT_FLAG_DATA));
2801
2802                 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2803                 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2804
2805                 ptr += sizeof(struct btrfs_extent_item);
2806
2807                 while (ptr < end) {
2808                         iref = (struct btrfs_extent_inline_ref *)ptr;
2809                         key.type = btrfs_extent_inline_ref_type(leaf, iref);
2810                         BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2811                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2812                         ref_root = btrfs_extent_data_ref_root(leaf, dref);
2813                         extent_key.objectid =
2814                                 btrfs_extent_data_ref_objectid(leaf, dref);
2815                         extent_key.offset =
2816                                 btrfs_extent_data_ref_offset(leaf, dref);
2817                         extent_key.type = BTRFS_EXTENT_DATA_KEY;
2818                         BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2819
2820                         if (ref_root == cur_root->root_key.objectid)
2821                                 break;
2822
2823                         ptr += btrfs_extent_inline_ref_size(key.type);
2824                 }
2825
2826                 if (ptr >= end) {
2827                         path.slots[0]++;
2828                         goto next;
2829                 }
2830
2831                 ret = relocate_one_reference(trans, cur_root, cur_byte,
2832                                              num_bytes, &extent_key,
2833                                              &reloc_tree);
2834                 if (ret < 0)
2835                         goto fail;
2836
2837                 cur_byte += num_bytes;
2838                 btrfs_release_path(&path);
2839
2840                 if (trans->blocks_used >= 4096) {
2841                         ret = btrfs_commit_transaction(trans, cur_root);
2842                         BUG_ON(ret);
2843                         trans = btrfs_start_transaction(cur_root, 1);
2844                         BUG_ON(!trans);
2845                 }
2846         }
2847         btrfs_release_path(&path);
2848
2849         ret = btrfs_commit_transaction(trans, cur_root);
2850         BUG_ON(ret);
2851
2852         if (num_extents > 0 && pass++ < 16)
2853                 goto again;
2854
2855         ret = (num_extents > 0) ? -1 : 0;
2856 fail:
2857         btrfs_release_path(&path);
2858         extent_io_tree_cleanup(&reloc_tree);
2859         return ret;
2860 }
2861
2862 /*
2863  * relocate data in system chunk
2864  */
2865 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2866                              struct btrfs_root *image_root)
2867 {
2868         struct btrfs_block_group_cache *cache;
2869         int i, ret = 0;
2870         u64 offset = 0;
2871         u64 end_byte;
2872
2873         while(1) {
2874                 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2875                 if (!cache)
2876                         break;
2877
2878                 end_byte = cache->key.objectid + cache->key.offset;
2879                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2880                         ret = relocate_extents_range(fs_root, image_root,
2881                                                      cache->key.objectid,
2882                                                      end_byte);
2883                         if (ret)
2884                                 goto fail;
2885                 }
2886                 offset = end_byte;
2887         }
2888         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2889                 offset = btrfs_sb_offset(i);
2890                 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2891
2892                 ret = relocate_extents_range(fs_root, image_root,
2893                                              offset, offset + BTRFS_STRIPE_LEN);
2894                 if (ret)
2895                         goto fail;
2896         }
2897         ret = 0;
2898 fail:
2899         return ret;
2900 }
2901
2902 static int fixup_chunk_mapping(struct btrfs_root *root)
2903 {
2904         struct btrfs_trans_handle *trans;
2905         struct btrfs_fs_info *info = root->fs_info;
2906         struct btrfs_root *chunk_root = info->chunk_root;
2907         struct extent_buffer *leaf;
2908         struct btrfs_key key;
2909         struct btrfs_path path;
2910         struct btrfs_chunk chunk;
2911         unsigned long ptr;
2912         u32 size;
2913         u64 type;
2914         int ret;
2915
2916         btrfs_init_path(&path);
2917
2918         trans = btrfs_start_transaction(root, 1);
2919         BUG_ON(!trans);
2920
2921         /*
2922          * recow the whole chunk tree. this will move all chunk tree blocks
2923          * into system block group.
2924          */
2925         memset(&key, 0, sizeof(key));
2926         while (1) {
2927                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2928                 if (ret < 0)
2929                         goto err;
2930
2931                 ret = btrfs_next_leaf(chunk_root, &path);
2932                 if (ret < 0)
2933                         goto err;
2934                 if (ret > 0)
2935                         break;
2936
2937                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2938                 btrfs_release_path(&path);
2939         }
2940         btrfs_release_path(&path);
2941
2942         /* fixup the system chunk array in super block */
2943         btrfs_set_super_sys_array_size(info->super_copy, 0);
2944
2945         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2946         key.offset = 0;
2947         key.type = BTRFS_CHUNK_ITEM_KEY;
2948
2949         ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2950         if (ret < 0)
2951                 goto err;
2952         BUG_ON(ret != 0);
2953         while(1) {
2954                 leaf = path.nodes[0];
2955                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2956                         ret = btrfs_next_leaf(chunk_root, &path);
2957                         if (ret < 0)
2958                                 goto err;
2959                         if (ret > 0)
2960                                 break;
2961                         leaf = path.nodes[0];
2962                 }
2963                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2964                 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2965                         goto next;
2966
2967                 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2968                 size = btrfs_item_size_nr(leaf, path.slots[0]);
2969                 BUG_ON(size != sizeof(chunk));
2970                 read_extent_buffer(leaf, &chunk, ptr, size);
2971                 type = btrfs_stack_chunk_type(&chunk);
2972
2973                 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2974                         goto next;
2975
2976                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2977                                              &chunk, size);
2978                 if (ret)
2979                         goto err;
2980 next:
2981                 path.slots[0]++;
2982         }
2983
2984         ret = btrfs_commit_transaction(trans, root);
2985         BUG_ON(ret);
2986 err:
2987         btrfs_release_path(&path);
2988         return ret;
2989 }
2990
2991 static const struct btrfs_convert_operations ext2_convert_ops = {
2992         .name                   = "ext2",
2993         .open_fs                = ext2_open_fs,
2994         .read_used_space        = ext2_read_used_space,
2995         .alloc_block            = ext2_alloc_block,
2996         .alloc_block_range      = ext2_alloc_block_range,
2997         .copy_inodes            = ext2_copy_inodes,
2998         .test_block             = ext2_test_block,
2999         .free_block             = ext2_free_block,
3000         .free_block_range       = ext2_free_block_range,
3001         .close_fs               = ext2_close_fs,
3002 };
3003
3004 static const struct btrfs_convert_operations *convert_operations[] = {
3005         &ext2_convert_ops,
3006 };
3007
3008 static int convert_open_fs(const char *devname,
3009                            struct btrfs_convert_context *cctx)
3010 {
3011         int i;
3012
3013         memset(cctx, 0, sizeof(*cctx));
3014
3015         for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
3016                 int ret = convert_operations[i]->open_fs(cctx, devname);
3017
3018                 if (ret == 0) {
3019                         cctx->convert_ops = convert_operations[i];
3020                         return ret;
3021                 }
3022         }
3023
3024         fprintf(stderr, "No file system found to convert.\n");
3025         return -1;
3026 }
3027
3028 /*
3029  * Remove one reserve range from given cache tree
3030  * if min_stripe_size is non-zero, it will ensure for split case,
3031  * all its split cache extent is no smaller than @min_strip_size / 2.
3032  */
3033 static int wipe_one_reserved_range(struct cache_tree *tree,
3034                                    u64 start, u64 len, u64 min_stripe_size,
3035                                    int ensure_size)
3036 {
3037         struct cache_extent *cache;
3038         int ret;
3039
3040         BUG_ON(ensure_size && min_stripe_size == 0);
3041         /*
3042          * The logical here is simplified to handle special cases only
3043          * So we don't need to consider merge case for ensure_size
3044          */
3045         BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
3046                min_stripe_size / 2 < BTRFS_STRIPE_LEN));
3047
3048         /* Also, wipe range should already be aligned */
3049         BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
3050                start + len != round_up(start + len, BTRFS_STRIPE_LEN));
3051
3052         min_stripe_size /= 2;
3053
3054         cache = lookup_cache_extent(tree, start, len);
3055         if (!cache)
3056                 return 0;
3057
3058         if (start <= cache->start) {
3059                 /*
3060                  *      |--------cache---------|
3061                  * |-wipe-|
3062                  */
3063                 BUG_ON(start + len <= cache->start);
3064
3065                 /*
3066                  * The wipe size is smaller than min_stripe_size / 2,
3067                  * so the result length should still meet min_stripe_size
3068                  * And no need to do alignment
3069                  */
3070                 cache->size -= (start + len - cache->start);
3071                 if (cache->size == 0) {
3072                         remove_cache_extent(tree, cache);
3073                         free(cache);
3074                         return 0;
3075                 }
3076
3077                 BUG_ON(ensure_size && cache->size < min_stripe_size);
3078
3079                 cache->start = start + len;
3080                 return 0;
3081         } else if (start > cache->start && start + len < cache->start +
3082                    cache->size) {
3083                 /*
3084                  * |-------cache-----|
3085                  *      |-wipe-|
3086                  */
3087                 u64 old_len = cache->size;
3088                 u64 insert_start = start + len;
3089                 u64 insert_len;
3090
3091                 cache->size = start - cache->start;
3092                 if (ensure_size)
3093                         cache->size = max(cache->size, min_stripe_size);
3094                 cache->start = start - cache->size;
3095
3096                 /* And insert the new one */
3097                 insert_len = old_len - start - len;
3098                 if (ensure_size)
3099                         insert_len = max(insert_len, min_stripe_size);
3100
3101                 ret = add_merge_cache_extent(tree, insert_start, insert_len);
3102                 return ret;
3103         }
3104         /*
3105          * |----cache-----|
3106          *              |--wipe-|
3107          * Wipe len should be small enough and no need to expand the
3108          * remaining extent
3109          */
3110         cache->size = start - cache->start;
3111         BUG_ON(ensure_size && cache->size < min_stripe_size);
3112         return 0;
3113 }
3114
3115 /*
3116  * Remove reserved ranges from given cache_tree
3117  *
3118  * It will remove the following ranges
3119  * 1) 0~1M
3120  * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
3121  * 3) 3rd superblock, +64K
3122  *
3123  * @min_stripe must be given for safety check
3124  * and if @ensure_size is given, it will ensure affected cache_extent will be
3125  * larger than min_stripe_size
3126  */
3127 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
3128                                 int ensure_size)
3129 {
3130         int ret;
3131
3132         ret = wipe_one_reserved_range(tree, 0, 1024 * 1024, min_stripe_size,
3133                                       ensure_size);
3134         if (ret < 0)
3135                 return ret;
3136         ret = wipe_one_reserved_range(tree, btrfs_sb_offset(1),
3137                         BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
3138         if (ret < 0)
3139                 return ret;
3140         ret = wipe_one_reserved_range(tree, btrfs_sb_offset(2),
3141                         BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
3142         return ret;
3143 }
3144
3145 static int calculate_available_space(struct btrfs_convert_context *cctx)
3146 {
3147         struct cache_tree *used = &cctx->used;
3148         struct cache_tree *data_chunks = &cctx->data_chunks;
3149         struct cache_tree *free = &cctx->free;
3150         struct cache_extent *cache;
3151         u64 cur_off = 0;
3152         /*
3153          * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
3154          * works without need to consider overlap
3155          */
3156         u64 min_stripe_size = 2 * 16 * 1024 * 1024;
3157         int ret;
3158
3159         /* Calculate data_chunks */
3160         for (cache = first_cache_extent(used); cache;
3161              cache = next_cache_extent(cache)) {
3162                 u64 cur_len;
3163
3164                 if (cache->start + cache->size < cur_off)
3165                         continue;
3166                 if (cache->start > cur_off + min_stripe_size)
3167                         cur_off = cache->start;
3168                 cur_len = max(cache->start + cache->size - cur_off,
3169                               min_stripe_size);
3170                 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
3171                 if (ret < 0)
3172                         goto out;
3173                 cur_off += cur_len;
3174         }
3175         /*
3176          * remove reserved ranges, so we won't ever bother relocating an old
3177          * filesystem extent to other place.
3178          */
3179         ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
3180         if (ret < 0)
3181                 goto out;
3182
3183         cur_off = 0;
3184         /*
3185          * Calculate free space
3186          * Always round up the start bytenr, to avoid metadata extent corss
3187          * stripe boundary, as later mkfs_convert() won't have all the extent
3188          * allocation check
3189          */
3190         for (cache = first_cache_extent(data_chunks); cache;
3191              cache = next_cache_extent(cache)) {
3192                 if (cache->start < cur_off)
3193                         continue;
3194                 if (cache->start > cur_off) {
3195                         u64 insert_start;
3196                         u64 len;
3197
3198                         len = cache->start - round_up(cur_off,
3199                                                       BTRFS_STRIPE_LEN);
3200                         insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
3201
3202                         ret = add_merge_cache_extent(free, insert_start, len);
3203                         if (ret < 0)
3204                                 goto out;
3205                 }
3206                 cur_off = cache->start + cache->size;
3207         }
3208         /* Don't forget the last range */
3209         if (cctx->total_bytes > cur_off) {
3210                 u64 len = cctx->total_bytes - cur_off;
3211                 u64 insert_start;
3212
3213                 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
3214
3215                 ret = add_merge_cache_extent(free, insert_start, len);
3216                 if (ret < 0)
3217                         goto out;
3218         }
3219
3220         /* Remove reserved bytes */
3221         ret = wipe_reserved_ranges(free, min_stripe_size, 0);
3222 out:
3223         return ret;
3224 }
3225 /*
3226  * Read used space, and since we have the used space,
3227  * calcuate data_chunks and free for later mkfs
3228  */
3229 static int convert_read_used_space(struct btrfs_convert_context *cctx)
3230 {
3231         int ret;
3232
3233         ret = cctx->convert_ops->read_used_space(cctx);
3234         if (ret)
3235                 return ret;
3236
3237         ret = calculate_available_space(cctx);
3238         return ret;
3239 }
3240
3241 static int do_convert_v2(const char *devname, int datacsum, int packing,
3242                 int noxattr, u32 nodesize, int copylabel, const char *fslabel,
3243                 int progress, u64 features)
3244 {
3245         int ret;
3246         int fd = -1;
3247         int is_btrfs = 0;
3248         u32 blocksize;
3249         u64 total_bytes;
3250         struct btrfs_root *root;
3251         struct btrfs_root *image_root;
3252         struct btrfs_convert_context cctx;
3253         struct btrfs_key key;
3254         char *subvol_name = NULL;
3255         struct task_ctx ctx;
3256         char features_buf[64];
3257         struct btrfs_mkfs_config mkfs_cfg;
3258
3259         init_convert_context(&cctx);
3260         ret = convert_open_fs(devname, &cctx);
3261         if (ret)
3262                 goto fail;
3263         ret = convert_read_used_space(&cctx);
3264         if (ret)
3265                 goto fail;
3266
3267         blocksize = cctx.blocksize;
3268         total_bytes = (u64)blocksize * (u64)cctx.block_count;
3269         if (blocksize < 4096) {
3270                 fprintf(stderr, "block size is too small\n");
3271                 goto fail;
3272         }
3273         if (btrfs_check_nodesize(nodesize, blocksize, features))
3274                 goto fail;
3275         fd = open(devname, O_RDWR);
3276         if (fd < 0) {
3277                 fprintf(stderr, "unable to open %s\n", devname);
3278                 goto fail;
3279         }
3280         btrfs_parse_features_to_string(features_buf, features);
3281         if (features == BTRFS_MKFS_DEFAULT_FEATURES)
3282                 strcat(features_buf, " (default)");
3283
3284         printf("create btrfs filesystem:\n");
3285         printf("\tblocksize: %u\n", blocksize);
3286         printf("\tnodesize:  %u\n", nodesize);
3287         printf("\tfeatures:  %s\n", features_buf);
3288
3289         mkfs_cfg.label = cctx.volume_name;
3290         mkfs_cfg.num_bytes = total_bytes;
3291         mkfs_cfg.nodesize = nodesize;
3292         mkfs_cfg.sectorsize = blocksize;
3293         mkfs_cfg.stripesize = blocksize;
3294         mkfs_cfg.features = features;
3295         /* New convert need these space */
3296         mkfs_cfg.fs_uuid = malloc(BTRFS_UUID_UNPARSED_SIZE);
3297         mkfs_cfg.chunk_uuid = malloc(BTRFS_UUID_UNPARSED_SIZE);
3298         *(mkfs_cfg.fs_uuid) = '\0';
3299         *(mkfs_cfg.chunk_uuid) = '\0';
3300
3301         ret = make_btrfs(fd, &mkfs_cfg, &cctx);
3302         if (ret) {
3303                 fprintf(stderr, "unable to create initial ctree: %s\n",
3304                         strerror(-ret));
3305                 goto fail;
3306         }
3307
3308         root = open_ctree_fd(fd, devname, mkfs_cfg.super_bytenr,
3309                              OPEN_CTREE_WRITES);
3310         if (!root) {
3311                 fprintf(stderr, "unable to open ctree\n");
3312                 goto fail;
3313         }
3314         ret = init_btrfs_v2(&mkfs_cfg, root, &cctx, datacsum, packing, noxattr);
3315         if (ret) {
3316                 fprintf(stderr, "unable to setup the root tree\n");
3317                 goto fail;
3318         }
3319
3320         printf("creating %s image file.\n", cctx.convert_ops->name);
3321         ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
3322         if (ret < 0) {
3323                 fprintf(stderr, "error allocating subvolume name: %s_saved\n",
3324                         cctx.convert_ops->name);
3325                 goto fail;
3326         }
3327         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3328         key.offset = (u64)-1;
3329         key.type = BTRFS_ROOT_ITEM_KEY;
3330         image_root = btrfs_read_fs_root(root->fs_info, &key);
3331         if (!image_root) {
3332                 fprintf(stderr, "unable to create subvol\n");
3333                 goto fail;
3334         }
3335         ret = create_image_v2(image_root, &mkfs_cfg, &cctx, fd,
3336                               mkfs_cfg.num_bytes, "image", datacsum);
3337         if (ret) {
3338                 fprintf(stderr, "error during create_image %d\n", ret);
3339                 goto fail;
3340         }
3341
3342         printf("creating btrfs metadata.\n");
3343         ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
3344         ctx.cur_copy_inodes = 0;
3345
3346         if (progress) {
3347                 ctx.info = task_init(print_copied_inodes, after_copied_inodes,
3348                                      &ctx);
3349                 task_start(ctx.info);
3350         }
3351         ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
3352         if (ret) {
3353                 fprintf(stderr, "error during copy_inodes %d\n", ret);
3354                 goto fail;
3355         }
3356         if (progress) {
3357                 task_stop(ctx.info);
3358                 task_deinit(ctx.info);
3359         }
3360
3361         image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
3362
3363         free(subvol_name);
3364
3365         memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
3366         if (copylabel == 1) {
3367                 __strncpy_null(root->fs_info->super_copy->label,
3368                                 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
3369                 fprintf(stderr, "copy label '%s'\n",
3370                                 root->fs_info->super_copy->label);
3371         } else if (copylabel == -1) {
3372                 strcpy(root->fs_info->super_copy->label, fslabel);
3373                 fprintf(stderr, "set label to '%s'\n", fslabel);
3374         }
3375
3376         ret = close_ctree(root);
3377         if (ret) {
3378                 fprintf(stderr, "error during close_ctree %d\n", ret);
3379                 goto fail;
3380         }
3381         convert_close_fs(&cctx);
3382         clean_convert_context(&cctx);
3383
3384         /*
3385          * If this step succeed, we get a mountable btrfs. Otherwise
3386          * the source fs is left unchanged.
3387          */
3388         ret = migrate_super_block(fd, mkfs_cfg.super_bytenr, blocksize);
3389         if (ret) {
3390                 fprintf(stderr, "unable to migrate super block\n");
3391                 goto fail;
3392         }
3393         is_btrfs = 1;
3394
3395         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3396         if (!root) {
3397                 fprintf(stderr, "unable to open ctree\n");
3398                 goto fail;
3399         }
3400         close(fd);
3401
3402         printf("conversion complete.\n");
3403         return 0;
3404 fail:
3405         clean_convert_context(&cctx);
3406         if (fd != -1)
3407                 close(fd);
3408         if (is_btrfs)
3409                 fprintf(stderr,
3410                         "WARNING: an error occurred during chunk mapping fixup, filesystem mountable but not finalized\n");
3411         else
3412                 fprintf(stderr, "conversion aborted\n");
3413         return -1;
3414 }
3415
3416 static int do_convert(const char *devname, int datacsum, int packing, int noxattr,
3417                 u32 nodesize, int copylabel, const char *fslabel, int progress,
3418                 u64 features)
3419 {
3420         int i, ret, blocks_per_node;
3421         int fd = -1;
3422         int is_btrfs = 0;
3423         u32 blocksize;
3424         u64 blocks[7];
3425         u64 total_bytes;
3426         u64 super_bytenr;
3427         struct btrfs_root *root;
3428         struct btrfs_root *image_root;
3429         struct btrfs_convert_context cctx;
3430         char *subvol_name = NULL;
3431         struct task_ctx ctx;
3432         char features_buf[64];
3433         struct btrfs_mkfs_config mkfs_cfg;
3434
3435         init_convert_context(&cctx);
3436         ret = convert_open_fs(devname, &cctx);
3437         if (ret)
3438                 goto fail;
3439         ret = convert_read_used_space(&cctx);
3440         if (ret)
3441                 goto fail;
3442
3443         blocksize = cctx.blocksize;
3444         total_bytes = (u64)blocksize * (u64)cctx.block_count;
3445         if (blocksize < 4096) {
3446                 fprintf(stderr, "block size is too small\n");
3447                 goto fail;
3448         }
3449         if (btrfs_check_nodesize(nodesize, blocksize, features))
3450                 goto fail;
3451         blocks_per_node = nodesize / blocksize;
3452         ret = -blocks_per_node;
3453         for (i = 0; i < 7; i++) {
3454                 if (nodesize == blocksize)
3455                         ret = convert_alloc_block(&cctx, 0, blocks + i);
3456                 else
3457                         ret = convert_alloc_block_range(&cctx,
3458                                         ret + blocks_per_node, blocks_per_node,
3459                                         blocks + i);
3460                 if (ret) {
3461                         fprintf(stderr, "not enough free space\n");
3462                         goto fail;
3463                 }
3464                 blocks[i] *= blocksize;
3465         }
3466         super_bytenr = blocks[0];
3467         fd = open(devname, O_RDWR);
3468         if (fd < 0) {
3469                 fprintf(stderr, "unable to open %s\n", devname);
3470                 goto fail;
3471         }
3472         btrfs_parse_features_to_string(features_buf, features);
3473         if (features == BTRFS_MKFS_DEFAULT_FEATURES)
3474                 strcat(features_buf, " (default)");
3475
3476         printf("create btrfs filesystem:\n");
3477         printf("\tblocksize: %u\n", blocksize);
3478         printf("\tnodesize:  %u\n", nodesize);
3479         printf("\tfeatures:  %s\n", features_buf);
3480
3481         mkfs_cfg.label = cctx.volume_name;
3482         mkfs_cfg.fs_uuid = NULL;
3483         memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
3484         mkfs_cfg.num_bytes = total_bytes;
3485         mkfs_cfg.nodesize = nodesize;
3486         mkfs_cfg.sectorsize = blocksize;
3487         mkfs_cfg.stripesize = blocksize;
3488         mkfs_cfg.features = features;
3489
3490         ret = make_btrfs(fd, &mkfs_cfg, NULL);
3491         if (ret) {
3492                 fprintf(stderr, "unable to create initial ctree: %s\n",
3493                         strerror(-ret));
3494                 goto fail;
3495         }
3496         /* create a system chunk that maps the whole device */
3497         ret = prepare_system_chunk(fd, super_bytenr);
3498         if (ret) {
3499                 fprintf(stderr, "unable to update system chunk\n");
3500                 goto fail;
3501         }
3502         root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
3503         if (!root) {
3504                 fprintf(stderr, "unable to open ctree\n");
3505                 goto fail;
3506         }
3507         ret = cache_free_extents(root, &cctx);
3508         if (ret) {
3509                 fprintf(stderr, "error during cache_free_extents %d\n", ret);
3510                 goto fail;
3511         }
3512         root->fs_info->extent_ops = &extent_ops;
3513         /* recover block allocation bitmap */
3514         for (i = 0; i < 7; i++) {
3515                 blocks[i] /= blocksize;
3516                 if (nodesize == blocksize)
3517                         convert_free_block(&cctx, blocks[i]);
3518                 else
3519                         convert_free_block_range(&cctx, blocks[i],
3520                                         blocks_per_node);
3521         }
3522         ret = init_btrfs(root);
3523         if (ret) {
3524                 fprintf(stderr, "unable to setup the root tree\n");
3525                 goto fail;
3526         }
3527         printf("creating btrfs metadata.\n");
3528         ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
3529         ctx.cur_copy_inodes = 0;
3530
3531         if (progress) {
3532                 ctx.info = task_init(print_copied_inodes, after_copied_inodes, &ctx);
3533                 task_start(ctx.info);
3534         }
3535         ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
3536         if (ret) {
3537                 fprintf(stderr, "error during copy_inodes %d\n", ret);
3538                 goto fail;
3539         }
3540         if (progress) {
3541                 task_stop(ctx.info);
3542                 task_deinit(ctx.info);
3543         }
3544
3545         printf("creating %s image file.\n", cctx.convert_ops->name);
3546         ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
3547         if (ret < 0) {
3548                 fprintf(stderr, "error allocating subvolume name: %s_saved\n",
3549                         cctx.convert_ops->name);
3550                 goto fail;
3551         }
3552
3553         image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
3554
3555         free(subvol_name);
3556
3557         if (!image_root) {
3558                 fprintf(stderr, "unable to create subvol\n");
3559                 goto fail;
3560         }
3561         ret = create_image(&cctx, image_root, "image", datacsum);
3562         if (ret) {
3563                 fprintf(stderr, "error during create_image %d\n", ret);
3564                 goto fail;
3565         }
3566         memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
3567         if (copylabel == 1) {
3568                 __strncpy_null(root->fs_info->super_copy->label,
3569                                 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
3570                 fprintf(stderr, "copy label '%s'\n",
3571                                 root->fs_info->super_copy->label);
3572         } else if (copylabel == -1) {
3573                 strcpy(root->fs_info->super_copy->label, fslabel);
3574                 fprintf(stderr, "set label to '%s'\n", fslabel);
3575         }
3576
3577         printf("cleaning up system chunk.\n");
3578         ret = cleanup_sys_chunk(root, image_root);
3579         if (ret) {
3580                 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
3581                 goto fail;
3582         }
3583         ret = close_ctree(root);
3584         if (ret) {
3585                 fprintf(stderr, "error during close_ctree %d\n", ret);
3586                 goto fail;
3587         }
3588         convert_close_fs(&cctx);
3589         clean_convert_context(&cctx);
3590
3591         /*
3592          * If this step succeed, we get a mountable btrfs. Otherwise
3593          * the source fs is left unchanged.
3594          */
3595         ret = migrate_super_block(fd, super_bytenr, blocksize);
3596         if (ret) {
3597                 fprintf(stderr, "unable to migrate super block\n");
3598                 goto fail;
3599         }
3600         is_btrfs = 1;
3601
3602         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3603         if (!root) {
3604                 fprintf(stderr, "unable to open ctree\n");
3605                 goto fail;
3606         }
3607         /* move chunk tree into system chunk. */
3608         ret = fixup_chunk_mapping(root);
3609         if (ret) {
3610                 fprintf(stderr, "error during fixup_chunk_tree\n");
3611                 goto fail;
3612         }
3613         ret = close_ctree(root);
3614         close(fd);
3615
3616         printf("conversion complete.\n");
3617         return 0;
3618 fail:
3619         clean_convert_context(&cctx);
3620         if (fd != -1)
3621                 close(fd);
3622         if (is_btrfs)
3623                 fprintf(stderr,
3624                         "WARNING: an error occured during chunk mapping fixup, filesystem mountable but not finalized\n");
3625         else
3626                 fprintf(stderr, "conversion aborted\n");
3627         return -1;
3628 }
3629
3630 static int may_rollback(struct btrfs_root *root)
3631 {
3632         struct btrfs_fs_info *info = root->fs_info;
3633         struct btrfs_multi_bio *multi = NULL;
3634         u64 bytenr;
3635         u64 length;
3636         u64 physical;
3637         u64 total_bytes;
3638         int num_stripes;
3639         int ret;
3640
3641         if (btrfs_super_num_devices(info->super_copy) != 1)
3642                 goto fail;
3643
3644         bytenr = BTRFS_SUPER_INFO_OFFSET;
3645         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3646
3647         while (1) {
3648                 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
3649                                       &length, &multi, 0, NULL);
3650                 if (ret) {
3651                         if (ret == -ENOENT) {
3652                                 /* removed block group at the tail */
3653                                 if (length == (u64)-1)
3654                                         break;
3655
3656                                 /* removed block group in the middle */
3657                                 goto next;
3658                         }
3659                         goto fail;
3660                 }
3661
3662                 num_stripes = multi->num_stripes;
3663                 physical = multi->stripes[0].physical;
3664                 kfree(multi);
3665
3666                 if (num_stripes != 1 || physical != bytenr)
3667                         goto fail;
3668 next:
3669                 bytenr += length;
3670                 if (bytenr >= total_bytes)
3671                         break;
3672         }
3673         return 0;
3674 fail:
3675         return -1;
3676 }
3677
3678 static int do_rollback(const char *devname)
3679 {
3680         int fd = -1;
3681         int ret;
3682         int i;
3683         struct btrfs_root *root;
3684         struct btrfs_root *image_root;
3685         struct btrfs_root *chunk_root;
3686         struct btrfs_dir_item *dir;
3687         struct btrfs_inode_item *inode;
3688         struct btrfs_file_extent_item *fi;
3689         struct btrfs_trans_handle *trans;
3690         struct extent_buffer *leaf;
3691         struct btrfs_block_group_cache *cache1;
3692         struct btrfs_block_group_cache *cache2;
3693         struct btrfs_key key;
3694         struct btrfs_path path;
3695         struct extent_io_tree io_tree;
3696         char *buf = NULL;
3697         char *name;
3698         u64 bytenr;
3699         u64 num_bytes;
3700         u64 root_dir;
3701         u64 objectid;
3702         u64 offset;
3703         u64 start;
3704         u64 end;
3705         u64 sb_bytenr;
3706         u64 first_free;
3707         u64 total_bytes;
3708         u32 sectorsize;
3709
3710         extent_io_tree_init(&io_tree);
3711
3712         fd = open(devname, O_RDWR);
3713         if (fd < 0) {
3714                 fprintf(stderr, "unable to open %s\n", devname);
3715                 goto fail;
3716         }
3717         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3718         if (!root) {
3719                 fprintf(stderr, "unable to open ctree\n");
3720                 goto fail;
3721         }
3722         ret = may_rollback(root);
3723         if (ret < 0) {
3724                 fprintf(stderr, "unable to do rollback\n");
3725                 goto fail;
3726         }
3727
3728         sectorsize = root->sectorsize;
3729         buf = malloc(sectorsize);
3730         if (!buf) {
3731                 fprintf(stderr, "unable to allocate memory\n");
3732                 goto fail;
3733         }
3734
3735         btrfs_init_path(&path);
3736
3737         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3738         key.type = BTRFS_ROOT_BACKREF_KEY;
3739         key.offset = BTRFS_FS_TREE_OBJECTID;
3740         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path, 0,
3741                                 0);
3742         btrfs_release_path(&path);
3743         if (ret > 0) {
3744                 fprintf(stderr,
3745                 "ERROR: unable to convert ext2 image subvolume, is it deleted?\n");
3746                 goto fail;
3747         } else if (ret < 0) {
3748                 fprintf(stderr,
3749                         "ERROR: unable to open ext2_saved, id=%llu: %s\n",
3750                         (unsigned long long)key.objectid, strerror(-ret));
3751                 goto fail;
3752         }
3753
3754         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3755         key.type = BTRFS_ROOT_ITEM_KEY;
3756         key.offset = (u64)-1;
3757         image_root = btrfs_read_fs_root(root->fs_info, &key);
3758         if (!image_root || IS_ERR(image_root)) {
3759                 fprintf(stderr, "unable to open subvol %llu\n",
3760                         (unsigned long long)key.objectid);
3761                 goto fail;
3762         }
3763
3764         name = "image";
3765         root_dir = btrfs_root_dirid(&root->root_item);
3766         dir = btrfs_lookup_dir_item(NULL, image_root, &path,
3767                                    root_dir, name, strlen(name), 0);
3768         if (!dir || IS_ERR(dir)) {
3769                 fprintf(stderr, "unable to find file %s\n", name);
3770                 goto fail;
3771         }
3772         leaf = path.nodes[0];
3773         btrfs_dir_item_key_to_cpu(leaf, dir, &key);
3774         btrfs_release_path(&path);
3775
3776         objectid = key.objectid;
3777
3778         ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
3779         if (ret) {
3780                 fprintf(stderr, "unable to find inode item\n");
3781                 goto fail;
3782         }
3783         leaf = path.nodes[0];
3784         inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
3785         total_bytes = btrfs_inode_size(leaf, inode);
3786         btrfs_release_path(&path);
3787
3788         key.objectid = objectid;
3789         key.offset = 0;
3790         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3791         ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
3792         if (ret != 0) {
3793                 fprintf(stderr, "unable to find first file extent\n");
3794                 btrfs_release_path(&path);
3795                 goto fail;
3796         }
3797
3798         /* build mapping tree for the relocated blocks */
3799         for (offset = 0; offset < total_bytes; ) {
3800                 leaf = path.nodes[0];
3801                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
3802                         ret = btrfs_next_leaf(root, &path);
3803                         if (ret != 0)
3804                                 break;  
3805                         continue;
3806                 }
3807
3808                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
3809                 if (key.objectid != objectid || key.offset != offset ||
3810                     btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3811                         break;
3812
3813                 fi = btrfs_item_ptr(leaf, path.slots[0],
3814                                     struct btrfs_file_extent_item);
3815                 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
3816                         break;
3817                 if (btrfs_file_extent_compression(leaf, fi) ||
3818                     btrfs_file_extent_encryption(leaf, fi) ||
3819                     btrfs_file_extent_other_encoding(leaf, fi))
3820                         break;
3821
3822                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3823                 /* skip holes and direct mapped extents */
3824                 if (bytenr == 0 || bytenr == offset)
3825                         goto next_extent;
3826
3827                 bytenr += btrfs_file_extent_offset(leaf, fi);
3828                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3829
3830                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3831                 cache2 = btrfs_lookup_block_group(root->fs_info,
3832                                                   offset + num_bytes - 1);
3833                 /*
3834                  * Here we must take consideration of old and new convert
3835                  * behavior.
3836                  * For old convert case, sign, there is no consist chunk type
3837                  * that will cover the extent. META/DATA/SYS are all possible.
3838                  * Just ensure relocate one is in SYS chunk.
3839                  * For new convert case, they are all covered by DATA chunk.
3840                  *
3841                  * So, there is not valid chunk type check for it now.
3842                  */
3843                 if (cache1 != cache2)
3844                         break;
3845
3846                 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
3847                                 EXTENT_LOCKED, GFP_NOFS);
3848                 set_state_private(&io_tree, offset, bytenr);
3849 next_extent:
3850                 offset += btrfs_file_extent_num_bytes(leaf, fi);
3851                 path.slots[0]++;
3852         }
3853         btrfs_release_path(&path);
3854
3855         if (offset < total_bytes) {
3856                 fprintf(stderr, "unable to build extent mapping\n");
3857                 fprintf(stderr, "converted filesystem after balance is unable to rollback\n");
3858                 goto fail;
3859         }
3860
3861         first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
3862         first_free &= ~((u64)sectorsize - 1);
3863         /* backup for extent #0 should exist */
3864         if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
3865                 fprintf(stderr, "no backup for the first extent\n");
3866                 goto fail;
3867         }
3868         /* force no allocation from system block group */
3869         root->fs_info->system_allocs = -1;
3870         trans = btrfs_start_transaction(root, 1);
3871         BUG_ON(!trans);
3872         /*
3873          * recow the whole chunk tree, this will remove all chunk tree blocks
3874          * from system block group
3875          */
3876         chunk_root = root->fs_info->chunk_root;
3877         memset(&key, 0, sizeof(key));
3878         while (1) {
3879                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
3880                 if (ret < 0)
3881                         break;
3882
3883                 ret = btrfs_next_leaf(chunk_root, &path);
3884                 if (ret)
3885                         break;
3886
3887                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3888                 btrfs_release_path(&path);
3889         }
3890         btrfs_release_path(&path);
3891
3892         offset = 0;
3893         num_bytes = 0;
3894         while(1) {
3895                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3896                 if (!cache1)
3897                         break;
3898
3899                 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
3900                         num_bytes += btrfs_block_group_used(&cache1->item);
3901
3902                 offset = cache1->key.objectid + cache1->key.offset;
3903         }
3904         /* only extent #0 left in system block group? */
3905         if (num_bytes > first_free) {
3906                 fprintf(stderr, "unable to empty system block group\n");
3907                 goto fail;
3908         }
3909         /* create a system chunk that maps the whole device */
3910         ret = prepare_system_chunk_sb(root->fs_info->super_copy);
3911         if (ret) {
3912                 fprintf(stderr, "unable to update system chunk\n");
3913                 goto fail;
3914         }
3915
3916         ret = btrfs_commit_transaction(trans, root);
3917         BUG_ON(ret);
3918
3919         ret = close_ctree(root);
3920         if (ret) {
3921                 fprintf(stderr, "error during close_ctree %d\n", ret);
3922                 goto fail;
3923         }
3924
3925         /* zero btrfs super block mirrors */
3926         memset(buf, 0, sectorsize);
3927         for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3928                 bytenr = btrfs_sb_offset(i);
3929                 if (bytenr >= total_bytes)
3930                         break;
3931                 ret = pwrite(fd, buf, sectorsize, bytenr);
3932                 if (ret != sectorsize) {
3933                         fprintf(stderr,
3934                                 "error during zeroing superblock %d: %d\n",
3935                                 i, ret);
3936                         goto fail;
3937                 }
3938         }
3939
3940         sb_bytenr = (u64)-1;
3941         /* copy all relocated blocks back */
3942         while(1) {
3943                 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
3944                                             EXTENT_LOCKED);
3945                 if (ret)
3946                         break;
3947
3948                 ret = get_state_private(&io_tree, start, &bytenr);
3949                 BUG_ON(ret);
3950
3951                 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
3952                                   GFP_NOFS);
3953
3954                 while (start <= end) {
3955                         if (start == BTRFS_SUPER_INFO_OFFSET) {
3956                                 sb_bytenr = bytenr;
3957                                 goto next_sector;
3958                         }
3959                         ret = pread(fd, buf, sectorsize, bytenr);
3960                         if (ret < 0) {
3961                                 fprintf(stderr, "error during pread %d\n", ret);
3962                                 goto fail;
3963                         }
3964                         BUG_ON(ret != sectorsize);
3965                         ret = pwrite(fd, buf, sectorsize, start);
3966                         if (ret < 0) {
3967                                 fprintf(stderr, "error during pwrite %d\n", ret);
3968                                 goto fail;
3969                         }
3970                         BUG_ON(ret != sectorsize);
3971 next_sector:
3972                         start += sectorsize;
3973                         bytenr += sectorsize;
3974                 }
3975         }
3976
3977         ret = fsync(fd);
3978         if (ret) {
3979                 fprintf(stderr, "error during fsync %d\n", ret);
3980                 goto fail;
3981         }
3982         /*
3983          * finally, overwrite btrfs super block.
3984          */
3985         ret = pread(fd, buf, sectorsize, sb_bytenr);
3986         if (ret < 0) {
3987                 fprintf(stderr, "error during pread %d\n", ret);
3988                 goto fail;
3989         }
3990         BUG_ON(ret != sectorsize);
3991         ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
3992         if (ret < 0) {
3993                 fprintf(stderr, "error during pwrite %d\n", ret);
3994                 goto fail;
3995         }
3996         BUG_ON(ret != sectorsize);
3997         ret = fsync(fd);
3998         if (ret) {
3999                 fprintf(stderr, "error during fsync %d\n", ret);
4000                 goto fail;
4001         }
4002
4003         close(fd);
4004         free(buf);
4005         extent_io_tree_cleanup(&io_tree);
4006         printf("rollback complete.\n");
4007         return 0;
4008
4009 fail:
4010         if (fd != -1)
4011                 close(fd);
4012         free(buf);
4013         fprintf(stderr, "rollback aborted.\n");
4014         return -1;
4015 }
4016
4017 static void print_usage(void)
4018 {
4019         printf("usage: btrfs-convert [options] device\n");
4020         printf("options:\n");
4021         printf("\t-d|--no-datasum        disable data checksum, sets NODATASUM\n");
4022         printf("\t-i|--no-xattr          ignore xattrs and ACLs\n");
4023         printf("\t-n|--no-inline         disable inlining of small files to metadata\n");
4024         printf("\t-N|--nodesize SIZE     set filesystem metadata nodesize\n");
4025         printf("\t-r|--rollback          roll back to the original filesystem\n");
4026         printf("\t-l|--label LABEL       set filesystem label\n");
4027         printf("\t-L|--copy-label        use label from converted filesystem\n");
4028         printf("\t-p|--progress          show converting progress (default)\n");
4029         printf("\t-O|--features LIST     comma separated list of filesystem features\n");
4030         printf("\t--no-progress          show only overview, not the detailed progress\n");
4031 }
4032
4033 int main(int argc, char *argv[])
4034 {
4035         int ret;
4036         int packing = 1;
4037         int noxattr = 0;
4038         int datacsum = 1;
4039         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
4040                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
4041         int rollback = 0;
4042         int copylabel = 0;
4043         int usage_error = 0;
4044         int progress = 1;
4045         char *file;
4046         char fslabel[BTRFS_LABEL_SIZE];
4047         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
4048
4049         while(1) {
4050                 enum { GETOPT_VAL_NO_PROGRESS = 256 };
4051                 static const struct option long_options[] = {
4052                         { "no-progress", no_argument, NULL,
4053                                 GETOPT_VAL_NO_PROGRESS },
4054                         { "no-datasum", no_argument, NULL, 'd' },
4055                         { "no-inline", no_argument, NULL, 'n' },
4056                         { "no-xattr", no_argument, NULL, 'i' },
4057                         { "rollback", no_argument, NULL, 'r' },
4058                         { "features", required_argument, NULL, 'O' },
4059                         { "progress", no_argument, NULL, 'p' },
4060                         { "label", required_argument, NULL, 'l' },
4061                         { "copy-label", no_argument, NULL, 'L' },
4062                         { "nodesize", required_argument, NULL, 'N' },
4063                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
4064                         { NULL, 0, NULL, 0 }
4065                 };
4066                 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
4067
4068                 if (c < 0)
4069                         break;
4070                 switch(c) {
4071                         case 'd':
4072                                 datacsum = 0;
4073                                 break;
4074                         case 'i':
4075                                 noxattr = 1;
4076                                 break;
4077                         case 'n':
4078                                 packing = 0;
4079                                 break;
4080                         case 'N':
4081                                 nodesize = parse_size(optarg);
4082                                 break;
4083                         case 'r':
4084                                 rollback = 1;
4085                                 break;
4086                         case 'l':
4087                                 copylabel = -1;
4088                                 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
4089                                         fprintf(stderr,
4090                                 "WARNING: label too long, trimmed to %d bytes\n",
4091                                                 BTRFS_LABEL_SIZE - 1);
4092                                 }
4093                                 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
4094                                 break;
4095                         case 'L':
4096                                 copylabel = 1;
4097                                 break;
4098                         case 'p':
4099                                 progress = 1;
4100                                 break;
4101                         case 'O': {
4102                                 char *orig = strdup(optarg);
4103                                 char *tmp = orig;
4104
4105                                 tmp = btrfs_parse_fs_features(tmp, &features);
4106                                 if (tmp) {
4107                                         fprintf(stderr,
4108                                                 "Unrecognized filesystem feature '%s'\n",
4109                                                         tmp);
4110                                         free(orig);
4111                                         exit(1);
4112                                 }
4113                                 free(orig);
4114                                 if (features & BTRFS_FEATURE_LIST_ALL) {
4115                                         btrfs_list_all_fs_features(
4116                                                 ~BTRFS_CONVERT_ALLOWED_FEATURES);
4117                                         exit(0);
4118                                 }
4119                                 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
4120                                         char buf[64];
4121
4122                                         btrfs_parse_features_to_string(buf,
4123                                                 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
4124                                         fprintf(stderr,
4125                                                 "ERROR: features not allowed for convert: %s\n",
4126                                                 buf);
4127                                         exit(1);
4128                                 }
4129
4130                                 break;
4131                                 }
4132                         case GETOPT_VAL_NO_PROGRESS:
4133                                 progress = 0;
4134                                 break;
4135                         case GETOPT_VAL_HELP:
4136                         default:
4137                                 print_usage();
4138                                 return c != GETOPT_VAL_HELP;
4139                 }
4140         }
4141         set_argv0(argv);
4142         if (check_argc_exact(argc - optind, 1)) {
4143                 print_usage();
4144                 return 1;
4145         }
4146
4147         if (rollback && (!datacsum || noxattr || !packing)) {
4148                 fprintf(stderr,
4149                         "Usage error: -d, -i, -n options do not apply to rollback\n");
4150                 usage_error++;
4151         }
4152
4153         if (usage_error) {
4154                 print_usage();
4155                 return 1;
4156         }
4157
4158         file = argv[optind];
4159         ret = check_mounted(file);
4160         if (ret < 0) {
4161                 fprintf(stderr, "Could not check mount status: %s\n",
4162                         strerror(-ret));
4163                 return 1;
4164         } else if (ret) {
4165                 fprintf(stderr, "%s is mounted\n", file);
4166                 return 1;
4167         }
4168
4169         if (rollback) {
4170                 ret = do_rollback(file);
4171         } else {
4172                 ret = do_convert_v2(file, datacsum, packing, noxattr, nodesize,
4173                                 copylabel, fslabel, progress, features);
4174         }
4175         if (ret)
4176                 return 1;
4177         return 0;
4178 }