btrfs-progs: convert: Enhance record_file_blocks to handle reserved ranges
[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 static int init_btrfs(struct btrfs_root *root)
2333 {
2334         int ret;
2335         struct btrfs_key location;
2336         struct btrfs_trans_handle *trans;
2337         struct btrfs_fs_info *fs_info = root->fs_info;
2338         struct extent_buffer *tmp;
2339
2340         trans = btrfs_start_transaction(root, 1);
2341         BUG_ON(!trans);
2342         ret = btrfs_make_block_groups(trans, root);
2343         if (ret)
2344                 goto err;
2345         ret = btrfs_fix_block_accounting(trans, root);
2346         if (ret)
2347                 goto err;
2348         ret = create_chunk_mapping(trans, root);
2349         if (ret)
2350                 goto err;
2351         ret = btrfs_make_root_dir(trans, fs_info->tree_root,
2352                                   BTRFS_ROOT_TREE_DIR_OBJECTID);
2353         if (ret)
2354                 goto err;
2355         memcpy(&location, &root->root_key, sizeof(location));
2356         location.offset = (u64)-1;
2357         ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
2358                                 btrfs_super_root_dir(fs_info->super_copy),
2359                                 &location, BTRFS_FT_DIR, 0);
2360         if (ret)
2361                 goto err;
2362         ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
2363                                 location.objectid,
2364                                 btrfs_super_root_dir(fs_info->super_copy), 0);
2365         if (ret)
2366                 goto err;
2367         btrfs_set_root_dirid(&fs_info->fs_root->root_item,
2368                              BTRFS_FIRST_FREE_OBJECTID);
2369
2370         /* subvol for fs image file */
2371         ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
2372         BUG_ON(ret);
2373         /* subvol for data relocation */
2374         ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
2375         BUG_ON(ret);
2376
2377         extent_buffer_get(fs_info->csum_root->node);
2378         ret = __btrfs_cow_block(trans, fs_info->csum_root,
2379                                 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
2380         BUG_ON(ret);
2381         free_extent_buffer(tmp);
2382
2383         ret = btrfs_commit_transaction(trans, root);
2384         BUG_ON(ret);
2385 err:
2386         return ret;
2387 }
2388
2389 /*
2390  * Migrate super block to its default position and zero 0 ~ 16k
2391  */
2392 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
2393 {
2394         int ret;
2395         struct extent_buffer *buf;
2396         struct btrfs_super_block *super;
2397         u32 len;
2398         u32 bytenr;
2399
2400         BUG_ON(sectorsize < sizeof(*super));
2401         buf = malloc(sizeof(*buf) + sectorsize);
2402         if (!buf)
2403                 return -ENOMEM;
2404
2405         buf->len = sectorsize;
2406         ret = pread(fd, buf->data, sectorsize, old_bytenr);
2407         if (ret != sectorsize)
2408                 goto fail;
2409
2410         super = (struct btrfs_super_block *)buf->data;
2411         BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
2412         btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
2413
2414         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2415         ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2416         if (ret != sectorsize)
2417                 goto fail;
2418
2419         ret = fsync(fd);
2420         if (ret)
2421                 goto fail;
2422
2423         memset(buf->data, 0, sectorsize);
2424         for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
2425                 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
2426                 if (len > sectorsize)
2427                         len = sectorsize;
2428                 ret = pwrite(fd, buf->data, len, bytenr);
2429                 if (ret != len) {
2430                         fprintf(stderr, "unable to zero fill device\n");
2431                         break;
2432                 }
2433                 bytenr += len;
2434         }
2435         ret = 0;
2436         fsync(fd);
2437 fail:
2438         free(buf);
2439         if (ret > 0)
2440                 ret = -1;
2441         return ret;
2442 }
2443
2444 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
2445 {
2446         struct btrfs_chunk *chunk;
2447         struct btrfs_disk_key *key;
2448         u32 sectorsize = btrfs_super_sectorsize(super);
2449
2450         key = (struct btrfs_disk_key *)(super->sys_chunk_array);
2451         chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
2452                                        sizeof(struct btrfs_disk_key));
2453
2454         btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2455         btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
2456         btrfs_set_disk_key_offset(key, 0);
2457
2458         btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
2459         btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
2460         btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
2461         btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
2462         btrfs_set_stack_chunk_io_align(chunk, sectorsize);
2463         btrfs_set_stack_chunk_io_width(chunk, sectorsize);
2464         btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
2465         btrfs_set_stack_chunk_num_stripes(chunk, 1);
2466         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
2467         chunk->stripe.devid = super->dev_item.devid;
2468         btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
2469         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
2470         btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
2471         return 0;
2472 }
2473
2474 static int prepare_system_chunk(int fd, u64 sb_bytenr)
2475 {
2476         int ret;
2477         struct extent_buffer *buf;
2478         struct btrfs_super_block *super;
2479
2480         BUG_ON(BTRFS_SUPER_INFO_SIZE < sizeof(*super));
2481         buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
2482         if (!buf)
2483                 return -ENOMEM;
2484
2485         buf->len = BTRFS_SUPER_INFO_SIZE;
2486         ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2487         if (ret != BTRFS_SUPER_INFO_SIZE)
2488                 goto fail;
2489
2490         super = (struct btrfs_super_block *)buf->data;
2491         BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
2492         BUG_ON(btrfs_super_num_devices(super) != 1);
2493
2494         ret = prepare_system_chunk_sb(super);
2495         if (ret)
2496                 goto fail;
2497
2498         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
2499         ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
2500         if (ret != BTRFS_SUPER_INFO_SIZE)
2501                 goto fail;
2502
2503         ret = 0;
2504 fail:
2505         free(buf);
2506         if (ret > 0)
2507                 ret = -1;
2508         return ret;
2509 }
2510
2511 static int relocate_one_reference(struct btrfs_trans_handle *trans,
2512                                   struct btrfs_root *root,
2513                                   u64 extent_start, u64 extent_size,
2514                                   struct btrfs_key *extent_key,
2515                                   struct extent_io_tree *reloc_tree)
2516 {
2517         struct extent_buffer *leaf;
2518         struct btrfs_file_extent_item *fi;
2519         struct btrfs_key key;
2520         struct btrfs_path path;
2521         struct btrfs_inode_item inode;
2522         struct blk_iterate_data data;
2523         u64 bytenr;
2524         u64 num_bytes;
2525         u64 cur_offset;
2526         u64 new_pos;
2527         u64 nbytes;
2528         u64 sector_end;
2529         u32 sectorsize = root->sectorsize;
2530         unsigned long ptr;
2531         int datacsum;
2532         int fd;
2533         int ret;
2534
2535         btrfs_init_path(&path);
2536         ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
2537         if (ret)
2538                 goto fail;
2539
2540         leaf = path.nodes[0];
2541         fi = btrfs_item_ptr(leaf, path.slots[0],
2542                             struct btrfs_file_extent_item);
2543         BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
2544         if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
2545             extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
2546                 ret = 1;
2547                 goto fail;
2548         }
2549
2550         bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
2551         num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2552
2553         ret = btrfs_del_item(trans, root, &path);
2554         if (ret)
2555                 goto fail;
2556
2557         ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
2558                                 root->root_key.objectid,
2559                                 extent_key->objectid, extent_key->offset);
2560         if (ret)
2561                 goto fail;
2562
2563         btrfs_release_path(&path);
2564
2565         key.objectid = extent_key->objectid;
2566         key.offset = 0;
2567         key.type =  BTRFS_INODE_ITEM_KEY;
2568         ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
2569         if (ret)
2570                 goto fail;
2571
2572         leaf = path.nodes[0];
2573         ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2574         read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2575         btrfs_release_path(&path);
2576
2577         BUG_ON(num_bytes & (sectorsize - 1));
2578         nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
2579         btrfs_set_stack_inode_nbytes(&inode, nbytes);
2580         datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
2581
2582         init_blk_iterate_data(&data, trans, root, &inode, extent_key->objectid,
2583                               datacsum);
2584         data.first_block = extent_key->offset;
2585
2586         cur_offset = extent_key->offset;
2587         while (num_bytes > 0) {
2588                 sector_end = bytenr + sectorsize - 1;
2589                 if (test_range_bit(reloc_tree, bytenr, sector_end,
2590                                    EXTENT_LOCKED, 1)) {
2591                         ret = get_state_private(reloc_tree, bytenr, &new_pos);
2592                         BUG_ON(ret);
2593                 } else {
2594                         ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
2595                         if (ret)
2596                                 goto fail;
2597                         new_pos = key.objectid;
2598
2599                         if (cur_offset == extent_key->offset) {
2600                                 fd = root->fs_info->fs_devices->latest_bdev;
2601                                 readahead(fd, bytenr, num_bytes);
2602                         }
2603                         ret = copy_disk_extent(root, new_pos, bytenr,
2604                                                sectorsize);
2605                         if (ret)
2606                                 goto fail;
2607                         ret = set_extent_bits(reloc_tree, bytenr, sector_end,
2608                                               EXTENT_LOCKED, GFP_NOFS);
2609                         BUG_ON(ret);
2610                         ret = set_state_private(reloc_tree, bytenr, new_pos);
2611                         BUG_ON(ret);
2612                 }
2613
2614                 ret = block_iterate_proc(new_pos / sectorsize,
2615                                          cur_offset / sectorsize, &data);
2616                 if (ret < 0)
2617                         goto fail;
2618
2619                 cur_offset += sectorsize;
2620                 bytenr += sectorsize;
2621                 num_bytes -= sectorsize;
2622         }
2623
2624         if (data.num_blocks > 0) {
2625                 ret = record_file_blocks(&data, data.first_block,
2626                                          data.disk_block, data.num_blocks);
2627                 if (ret)
2628                         goto fail;
2629         }
2630
2631         key.objectid = extent_key->objectid;
2632         key.offset = 0;
2633         key.type =  BTRFS_INODE_ITEM_KEY;
2634         ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
2635         if (ret)
2636                 goto fail;
2637
2638         leaf = path.nodes[0];
2639         ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2640         write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
2641         btrfs_mark_buffer_dirty(leaf);
2642         btrfs_release_path(&path);
2643
2644 fail:
2645         btrfs_release_path(&path);
2646         return ret;
2647 }
2648
2649 static int relocate_extents_range(struct btrfs_root *fs_root,
2650                                   struct btrfs_root *image_root,
2651                                   u64 start_byte, u64 end_byte)
2652 {
2653         struct btrfs_fs_info *info = fs_root->fs_info;
2654         struct btrfs_root *extent_root = info->extent_root;
2655         struct btrfs_root *cur_root = NULL;
2656         struct btrfs_trans_handle *trans;
2657         struct btrfs_extent_data_ref *dref;
2658         struct btrfs_extent_inline_ref *iref;
2659         struct btrfs_extent_item *ei;
2660         struct extent_buffer *leaf;
2661         struct btrfs_key key;
2662         struct btrfs_key extent_key;
2663         struct btrfs_path path;
2664         struct extent_io_tree reloc_tree;
2665         unsigned long ptr;
2666         unsigned long end;
2667         u64 cur_byte;
2668         u64 num_bytes;
2669         u64 ref_root;
2670         u64 num_extents;
2671         int pass = 0;
2672         int ret;
2673
2674         btrfs_init_path(&path);
2675         extent_io_tree_init(&reloc_tree);
2676
2677         key.objectid = start_byte;
2678         key.offset = 0;
2679         key.type = BTRFS_EXTENT_ITEM_KEY;
2680         ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2681         if (ret < 0)
2682                 goto fail;
2683         if (ret > 0) {
2684                 ret = btrfs_previous_item(extent_root, &path, 0,
2685                                           BTRFS_EXTENT_ITEM_KEY);
2686                 if (ret < 0)
2687                         goto fail;
2688                 if (ret == 0) {
2689                         leaf = path.nodes[0];
2690                         btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2691                         if (key.objectid + key.offset > start_byte)
2692                                 start_byte = key.objectid;
2693                 }
2694         }
2695         btrfs_release_path(&path);
2696 again:
2697         cur_root = (pass % 2 == 0) ? image_root : fs_root;
2698         num_extents = 0;
2699
2700         trans = btrfs_start_transaction(cur_root, 1);
2701         BUG_ON(!trans);
2702
2703         cur_byte = start_byte;
2704         while (1) {
2705                 key.objectid = cur_byte;
2706                 key.offset = 0;
2707                 key.type = BTRFS_EXTENT_ITEM_KEY;
2708                 ret = btrfs_search_slot(trans, extent_root,
2709                                         &key, &path, 0, 0);
2710                 if (ret < 0)
2711                         goto fail;
2712 next:
2713                 leaf = path.nodes[0];
2714                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2715                         ret = btrfs_next_leaf(extent_root, &path);
2716                         if (ret < 0)
2717                                 goto fail;
2718                         if (ret > 0)
2719                                 break;
2720                         leaf = path.nodes[0];
2721                 }
2722
2723                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2724                 if (key.objectid < cur_byte ||
2725                     key.type != BTRFS_EXTENT_ITEM_KEY) {
2726                         path.slots[0]++;
2727                         goto next;
2728                 }
2729                 if (key.objectid >= end_byte)
2730                         break;
2731
2732                 num_extents++;
2733
2734                 cur_byte = key.objectid;
2735                 num_bytes = key.offset;
2736                 ei = btrfs_item_ptr(leaf, path.slots[0],
2737                                     struct btrfs_extent_item);
2738                 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2739                          BTRFS_EXTENT_FLAG_DATA));
2740
2741                 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2742                 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2743
2744                 ptr += sizeof(struct btrfs_extent_item);
2745
2746                 while (ptr < end) {
2747                         iref = (struct btrfs_extent_inline_ref *)ptr;
2748                         key.type = btrfs_extent_inline_ref_type(leaf, iref);
2749                         BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2750                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2751                         ref_root = btrfs_extent_data_ref_root(leaf, dref);
2752                         extent_key.objectid =
2753                                 btrfs_extent_data_ref_objectid(leaf, dref);
2754                         extent_key.offset =
2755                                 btrfs_extent_data_ref_offset(leaf, dref);
2756                         extent_key.type = BTRFS_EXTENT_DATA_KEY;
2757                         BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2758
2759                         if (ref_root == cur_root->root_key.objectid)
2760                                 break;
2761
2762                         ptr += btrfs_extent_inline_ref_size(key.type);
2763                 }
2764
2765                 if (ptr >= end) {
2766                         path.slots[0]++;
2767                         goto next;
2768                 }
2769
2770                 ret = relocate_one_reference(trans, cur_root, cur_byte,
2771                                              num_bytes, &extent_key,
2772                                              &reloc_tree);
2773                 if (ret < 0)
2774                         goto fail;
2775
2776                 cur_byte += num_bytes;
2777                 btrfs_release_path(&path);
2778
2779                 if (trans->blocks_used >= 4096) {
2780                         ret = btrfs_commit_transaction(trans, cur_root);
2781                         BUG_ON(ret);
2782                         trans = btrfs_start_transaction(cur_root, 1);
2783                         BUG_ON(!trans);
2784                 }
2785         }
2786         btrfs_release_path(&path);
2787
2788         ret = btrfs_commit_transaction(trans, cur_root);
2789         BUG_ON(ret);
2790
2791         if (num_extents > 0 && pass++ < 16)
2792                 goto again;
2793
2794         ret = (num_extents > 0) ? -1 : 0;
2795 fail:
2796         btrfs_release_path(&path);
2797         extent_io_tree_cleanup(&reloc_tree);
2798         return ret;
2799 }
2800
2801 /*
2802  * relocate data in system chunk
2803  */
2804 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2805                              struct btrfs_root *image_root)
2806 {
2807         struct btrfs_block_group_cache *cache;
2808         int i, ret = 0;
2809         u64 offset = 0;
2810         u64 end_byte;
2811
2812         while(1) {
2813                 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2814                 if (!cache)
2815                         break;
2816
2817                 end_byte = cache->key.objectid + cache->key.offset;
2818                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2819                         ret = relocate_extents_range(fs_root, image_root,
2820                                                      cache->key.objectid,
2821                                                      end_byte);
2822                         if (ret)
2823                                 goto fail;
2824                 }
2825                 offset = end_byte;
2826         }
2827         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2828                 offset = btrfs_sb_offset(i);
2829                 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2830
2831                 ret = relocate_extents_range(fs_root, image_root,
2832                                              offset, offset + BTRFS_STRIPE_LEN);
2833                 if (ret)
2834                         goto fail;
2835         }
2836         ret = 0;
2837 fail:
2838         return ret;
2839 }
2840
2841 static int fixup_chunk_mapping(struct btrfs_root *root)
2842 {
2843         struct btrfs_trans_handle *trans;
2844         struct btrfs_fs_info *info = root->fs_info;
2845         struct btrfs_root *chunk_root = info->chunk_root;
2846         struct extent_buffer *leaf;
2847         struct btrfs_key key;
2848         struct btrfs_path path;
2849         struct btrfs_chunk chunk;
2850         unsigned long ptr;
2851         u32 size;
2852         u64 type;
2853         int ret;
2854
2855         btrfs_init_path(&path);
2856
2857         trans = btrfs_start_transaction(root, 1);
2858         BUG_ON(!trans);
2859
2860         /*
2861          * recow the whole chunk tree. this will move all chunk tree blocks
2862          * into system block group.
2863          */
2864         memset(&key, 0, sizeof(key));
2865         while (1) {
2866                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2867                 if (ret < 0)
2868                         goto err;
2869
2870                 ret = btrfs_next_leaf(chunk_root, &path);
2871                 if (ret < 0)
2872                         goto err;
2873                 if (ret > 0)
2874                         break;
2875
2876                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2877                 btrfs_release_path(&path);
2878         }
2879         btrfs_release_path(&path);
2880
2881         /* fixup the system chunk array in super block */
2882         btrfs_set_super_sys_array_size(info->super_copy, 0);
2883
2884         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2885         key.offset = 0;
2886         key.type = BTRFS_CHUNK_ITEM_KEY;
2887
2888         ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2889         if (ret < 0)
2890                 goto err;
2891         BUG_ON(ret != 0);
2892         while(1) {
2893                 leaf = path.nodes[0];
2894                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2895                         ret = btrfs_next_leaf(chunk_root, &path);
2896                         if (ret < 0)
2897                                 goto err;
2898                         if (ret > 0)
2899                                 break;
2900                         leaf = path.nodes[0];
2901                 }
2902                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2903                 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2904                         goto next;
2905
2906                 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2907                 size = btrfs_item_size_nr(leaf, path.slots[0]);
2908                 BUG_ON(size != sizeof(chunk));
2909                 read_extent_buffer(leaf, &chunk, ptr, size);
2910                 type = btrfs_stack_chunk_type(&chunk);
2911
2912                 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2913                         goto next;
2914
2915                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2916                                              &chunk, size);
2917                 if (ret)
2918                         goto err;
2919 next:
2920                 path.slots[0]++;
2921         }
2922
2923         ret = btrfs_commit_transaction(trans, root);
2924         BUG_ON(ret);
2925 err:
2926         btrfs_release_path(&path);
2927         return ret;
2928 }
2929
2930 static const struct btrfs_convert_operations ext2_convert_ops = {
2931         .name                   = "ext2",
2932         .open_fs                = ext2_open_fs,
2933         .read_used_space        = ext2_read_used_space,
2934         .alloc_block            = ext2_alloc_block,
2935         .alloc_block_range      = ext2_alloc_block_range,
2936         .copy_inodes            = ext2_copy_inodes,
2937         .test_block             = ext2_test_block,
2938         .free_block             = ext2_free_block,
2939         .free_block_range       = ext2_free_block_range,
2940         .close_fs               = ext2_close_fs,
2941 };
2942
2943 static const struct btrfs_convert_operations *convert_operations[] = {
2944         &ext2_convert_ops,
2945 };
2946
2947 static int convert_open_fs(const char *devname,
2948                            struct btrfs_convert_context *cctx)
2949 {
2950         int i;
2951
2952         memset(cctx, 0, sizeof(*cctx));
2953
2954         for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
2955                 int ret = convert_operations[i]->open_fs(cctx, devname);
2956
2957                 if (ret == 0) {
2958                         cctx->convert_ops = convert_operations[i];
2959                         return ret;
2960                 }
2961         }
2962
2963         fprintf(stderr, "No file system found to convert.\n");
2964         return -1;
2965 }
2966
2967 /*
2968  * Remove one reserve range from given cache tree
2969  * if min_stripe_size is non-zero, it will ensure for split case,
2970  * all its split cache extent is no smaller than @min_strip_size / 2.
2971  */
2972 static int wipe_one_reserved_range(struct cache_tree *tree,
2973                                    u64 start, u64 len, u64 min_stripe_size,
2974                                    int ensure_size)
2975 {
2976         struct cache_extent *cache;
2977         int ret;
2978
2979         BUG_ON(ensure_size && min_stripe_size == 0);
2980         /*
2981          * The logical here is simplified to handle special cases only
2982          * So we don't need to consider merge case for ensure_size
2983          */
2984         BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
2985                min_stripe_size / 2 < BTRFS_STRIPE_LEN));
2986
2987         /* Also, wipe range should already be aligned */
2988         BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
2989                start + len != round_up(start + len, BTRFS_STRIPE_LEN));
2990
2991         min_stripe_size /= 2;
2992
2993         cache = lookup_cache_extent(tree, start, len);
2994         if (!cache)
2995                 return 0;
2996
2997         if (start <= cache->start) {
2998                 /*
2999                  *      |--------cache---------|
3000                  * |-wipe-|
3001                  */
3002                 BUG_ON(start + len <= cache->start);
3003
3004                 /*
3005                  * The wipe size is smaller than min_stripe_size / 2,
3006                  * so the result length should still meet min_stripe_size
3007                  * And no need to do alignment
3008                  */
3009                 cache->size -= (start + len - cache->start);
3010                 if (cache->size == 0) {
3011                         remove_cache_extent(tree, cache);
3012                         free(cache);
3013                         return 0;
3014                 }
3015
3016                 BUG_ON(ensure_size && cache->size < min_stripe_size);
3017
3018                 cache->start = start + len;
3019                 return 0;
3020         } else if (start > cache->start && start + len < cache->start +
3021                    cache->size) {
3022                 /*
3023                  * |-------cache-----|
3024                  *      |-wipe-|
3025                  */
3026                 u64 old_len = cache->size;
3027                 u64 insert_start = start + len;
3028                 u64 insert_len;
3029
3030                 cache->size = start - cache->start;
3031                 if (ensure_size)
3032                         cache->size = max(cache->size, min_stripe_size);
3033                 cache->start = start - cache->size;
3034
3035                 /* And insert the new one */
3036                 insert_len = old_len - start - len;
3037                 if (ensure_size)
3038                         insert_len = max(insert_len, min_stripe_size);
3039
3040                 ret = add_merge_cache_extent(tree, insert_start, insert_len);
3041                 return ret;
3042         }
3043         /*
3044          * |----cache-----|
3045          *              |--wipe-|
3046          * Wipe len should be small enough and no need to expand the
3047          * remaining extent
3048          */
3049         cache->size = start - cache->start;
3050         BUG_ON(ensure_size && cache->size < min_stripe_size);
3051         return 0;
3052 }
3053
3054 /*
3055  * Remove reserved ranges from given cache_tree
3056  *
3057  * It will remove the following ranges
3058  * 1) 0~1M
3059  * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
3060  * 3) 3rd superblock, +64K
3061  *
3062  * @min_stripe must be given for safety check
3063  * and if @ensure_size is given, it will ensure affected cache_extent will be
3064  * larger than min_stripe_size
3065  */
3066 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
3067                                 int ensure_size)
3068 {
3069         int ret;
3070
3071         ret = wipe_one_reserved_range(tree, 0, 1024 * 1024, min_stripe_size,
3072                                       ensure_size);
3073         if (ret < 0)
3074                 return ret;
3075         ret = wipe_one_reserved_range(tree, btrfs_sb_offset(1),
3076                         BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
3077         if (ret < 0)
3078                 return ret;
3079         ret = wipe_one_reserved_range(tree, btrfs_sb_offset(2),
3080                         BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
3081         return ret;
3082 }
3083
3084 static int calculate_available_space(struct btrfs_convert_context *cctx)
3085 {
3086         struct cache_tree *used = &cctx->used;
3087         struct cache_tree *data_chunks = &cctx->data_chunks;
3088         struct cache_tree *free = &cctx->free;
3089         struct cache_extent *cache;
3090         u64 cur_off = 0;
3091         /*
3092          * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
3093          * works without need to consider overlap
3094          */
3095         u64 min_stripe_size = 2 * 16 * 1024 * 1024;
3096         int ret;
3097
3098         /* Calculate data_chunks */
3099         for (cache = first_cache_extent(used); cache;
3100              cache = next_cache_extent(cache)) {
3101                 u64 cur_len;
3102
3103                 if (cache->start + cache->size < cur_off)
3104                         continue;
3105                 if (cache->start > cur_off + min_stripe_size)
3106                         cur_off = cache->start;
3107                 cur_len = max(cache->start + cache->size - cur_off,
3108                               min_stripe_size);
3109                 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
3110                 if (ret < 0)
3111                         goto out;
3112                 cur_off += cur_len;
3113         }
3114         /*
3115          * remove reserved ranges, so we won't ever bother relocating an old
3116          * filesystem extent to other place.
3117          */
3118         ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
3119         if (ret < 0)
3120                 goto out;
3121
3122         cur_off = 0;
3123         /*
3124          * Calculate free space
3125          * Always round up the start bytenr, to avoid metadata extent corss
3126          * stripe boundary, as later mkfs_convert() won't have all the extent
3127          * allocation check
3128          */
3129         for (cache = first_cache_extent(data_chunks); cache;
3130              cache = next_cache_extent(cache)) {
3131                 if (cache->start < cur_off)
3132                         continue;
3133                 if (cache->start > cur_off) {
3134                         u64 insert_start;
3135                         u64 len;
3136
3137                         len = cache->start - round_up(cur_off,
3138                                                       BTRFS_STRIPE_LEN);
3139                         insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
3140
3141                         ret = add_merge_cache_extent(free, insert_start, len);
3142                         if (ret < 0)
3143                                 goto out;
3144                 }
3145                 cur_off = cache->start + cache->size;
3146         }
3147         /* Don't forget the last range */
3148         if (cctx->total_bytes > cur_off) {
3149                 u64 len = cctx->total_bytes - cur_off;
3150                 u64 insert_start;
3151
3152                 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
3153
3154                 ret = add_merge_cache_extent(free, insert_start, len);
3155                 if (ret < 0)
3156                         goto out;
3157         }
3158
3159         /* Remove reserved bytes */
3160         ret = wipe_reserved_ranges(free, min_stripe_size, 0);
3161 out:
3162         return ret;
3163 }
3164 /*
3165  * Read used space, and since we have the used space,
3166  * calcuate data_chunks and free for later mkfs
3167  */
3168 static int convert_read_used_space(struct btrfs_convert_context *cctx)
3169 {
3170         int ret;
3171
3172         ret = cctx->convert_ops->read_used_space(cctx);
3173         if (ret)
3174                 return ret;
3175
3176         ret = calculate_available_space(cctx);
3177         return ret;
3178 }
3179
3180 static int do_convert(const char *devname, int datacsum, int packing, int noxattr,
3181                 u32 nodesize, int copylabel, const char *fslabel, int progress,
3182                 u64 features)
3183 {
3184         int i, ret, blocks_per_node;
3185         int fd = -1;
3186         int is_btrfs = 0;
3187         u32 blocksize;
3188         u64 blocks[7];
3189         u64 total_bytes;
3190         u64 super_bytenr;
3191         struct btrfs_root *root;
3192         struct btrfs_root *image_root;
3193         struct btrfs_convert_context cctx;
3194         char *subvol_name = NULL;
3195         struct task_ctx ctx;
3196         char features_buf[64];
3197         struct btrfs_mkfs_config mkfs_cfg;
3198
3199         init_convert_context(&cctx);
3200         ret = convert_open_fs(devname, &cctx);
3201         if (ret)
3202                 goto fail;
3203         ret = convert_read_used_space(&cctx);
3204         if (ret)
3205                 goto fail;
3206
3207         blocksize = cctx.blocksize;
3208         total_bytes = (u64)blocksize * (u64)cctx.block_count;
3209         if (blocksize < 4096) {
3210                 fprintf(stderr, "block size is too small\n");
3211                 goto fail;
3212         }
3213         if (btrfs_check_nodesize(nodesize, blocksize, features))
3214                 goto fail;
3215         blocks_per_node = nodesize / blocksize;
3216         ret = -blocks_per_node;
3217         for (i = 0; i < 7; i++) {
3218                 if (nodesize == blocksize)
3219                         ret = convert_alloc_block(&cctx, 0, blocks + i);
3220                 else
3221                         ret = convert_alloc_block_range(&cctx,
3222                                         ret + blocks_per_node, blocks_per_node,
3223                                         blocks + i);
3224                 if (ret) {
3225                         fprintf(stderr, "not enough free space\n");
3226                         goto fail;
3227                 }
3228                 blocks[i] *= blocksize;
3229         }
3230         super_bytenr = blocks[0];
3231         fd = open(devname, O_RDWR);
3232         if (fd < 0) {
3233                 fprintf(stderr, "unable to open %s\n", devname);
3234                 goto fail;
3235         }
3236         btrfs_parse_features_to_string(features_buf, features);
3237         if (features == BTRFS_MKFS_DEFAULT_FEATURES)
3238                 strcat(features_buf, " (default)");
3239
3240         printf("create btrfs filesystem:\n");
3241         printf("\tblocksize: %u\n", blocksize);
3242         printf("\tnodesize:  %u\n", nodesize);
3243         printf("\tfeatures:  %s\n", features_buf);
3244
3245         mkfs_cfg.label = cctx.volume_name;
3246         mkfs_cfg.fs_uuid = NULL;
3247         memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
3248         mkfs_cfg.num_bytes = total_bytes;
3249         mkfs_cfg.nodesize = nodesize;
3250         mkfs_cfg.sectorsize = blocksize;
3251         mkfs_cfg.stripesize = blocksize;
3252         mkfs_cfg.features = features;
3253
3254         ret = make_btrfs(fd, &mkfs_cfg, NULL);
3255         if (ret) {
3256                 fprintf(stderr, "unable to create initial ctree: %s\n",
3257                         strerror(-ret));
3258                 goto fail;
3259         }
3260         /* create a system chunk that maps the whole device */
3261         ret = prepare_system_chunk(fd, super_bytenr);
3262         if (ret) {
3263                 fprintf(stderr, "unable to update system chunk\n");
3264                 goto fail;
3265         }
3266         root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
3267         if (!root) {
3268                 fprintf(stderr, "unable to open ctree\n");
3269                 goto fail;
3270         }
3271         ret = cache_free_extents(root, &cctx);
3272         if (ret) {
3273                 fprintf(stderr, "error during cache_free_extents %d\n", ret);
3274                 goto fail;
3275         }
3276         root->fs_info->extent_ops = &extent_ops;
3277         /* recover block allocation bitmap */
3278         for (i = 0; i < 7; i++) {
3279                 blocks[i] /= blocksize;
3280                 if (nodesize == blocksize)
3281                         convert_free_block(&cctx, blocks[i]);
3282                 else
3283                         convert_free_block_range(&cctx, blocks[i],
3284                                         blocks_per_node);
3285         }
3286         ret = init_btrfs(root);
3287         if (ret) {
3288                 fprintf(stderr, "unable to setup the root tree\n");
3289                 goto fail;
3290         }
3291         printf("creating btrfs metadata.\n");
3292         ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
3293         ctx.cur_copy_inodes = 0;
3294
3295         if (progress) {
3296                 ctx.info = task_init(print_copied_inodes, after_copied_inodes, &ctx);
3297                 task_start(ctx.info);
3298         }
3299         ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
3300         if (ret) {
3301                 fprintf(stderr, "error during copy_inodes %d\n", ret);
3302                 goto fail;
3303         }
3304         if (progress) {
3305                 task_stop(ctx.info);
3306                 task_deinit(ctx.info);
3307         }
3308
3309         printf("creating %s image file.\n", cctx.convert_ops->name);
3310         ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
3311         if (ret < 0) {
3312                 fprintf(stderr, "error allocating subvolume name: %s_saved\n",
3313                         cctx.convert_ops->name);
3314                 goto fail;
3315         }
3316
3317         image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
3318
3319         free(subvol_name);
3320
3321         if (!image_root) {
3322                 fprintf(stderr, "unable to create subvol\n");
3323                 goto fail;
3324         }
3325         ret = create_image(&cctx, image_root, "image", datacsum);
3326         if (ret) {
3327                 fprintf(stderr, "error during create_image %d\n", ret);
3328                 goto fail;
3329         }
3330         memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
3331         if (copylabel == 1) {
3332                 __strncpy_null(root->fs_info->super_copy->label,
3333                                 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
3334                 fprintf(stderr, "copy label '%s'\n",
3335                                 root->fs_info->super_copy->label);
3336         } else if (copylabel == -1) {
3337                 strcpy(root->fs_info->super_copy->label, fslabel);
3338                 fprintf(stderr, "set label to '%s'\n", fslabel);
3339         }
3340
3341         printf("cleaning up system chunk.\n");
3342         ret = cleanup_sys_chunk(root, image_root);
3343         if (ret) {
3344                 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
3345                 goto fail;
3346         }
3347         ret = close_ctree(root);
3348         if (ret) {
3349                 fprintf(stderr, "error during close_ctree %d\n", ret);
3350                 goto fail;
3351         }
3352         convert_close_fs(&cctx);
3353         clean_convert_context(&cctx);
3354
3355         /*
3356          * If this step succeed, we get a mountable btrfs. Otherwise
3357          * the source fs is left unchanged.
3358          */
3359         ret = migrate_super_block(fd, super_bytenr, blocksize);
3360         if (ret) {
3361                 fprintf(stderr, "unable to migrate super block\n");
3362                 goto fail;
3363         }
3364         is_btrfs = 1;
3365
3366         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3367         if (!root) {
3368                 fprintf(stderr, "unable to open ctree\n");
3369                 goto fail;
3370         }
3371         /* move chunk tree into system chunk. */
3372         ret = fixup_chunk_mapping(root);
3373         if (ret) {
3374                 fprintf(stderr, "error during fixup_chunk_tree\n");
3375                 goto fail;
3376         }
3377         ret = close_ctree(root);
3378         close(fd);
3379
3380         printf("conversion complete.\n");
3381         return 0;
3382 fail:
3383         clean_convert_context(&cctx);
3384         if (fd != -1)
3385                 close(fd);
3386         if (is_btrfs)
3387                 fprintf(stderr,
3388                         "WARNING: an error occured during chunk mapping fixup, filesystem mountable but not finalized\n");
3389         else
3390                 fprintf(stderr, "conversion aborted\n");
3391         return -1;
3392 }
3393
3394 static int may_rollback(struct btrfs_root *root)
3395 {
3396         struct btrfs_fs_info *info = root->fs_info;
3397         struct btrfs_multi_bio *multi = NULL;
3398         u64 bytenr;
3399         u64 length;
3400         u64 physical;
3401         u64 total_bytes;
3402         int num_stripes;
3403         int ret;
3404
3405         if (btrfs_super_num_devices(info->super_copy) != 1)
3406                 goto fail;
3407
3408         bytenr = BTRFS_SUPER_INFO_OFFSET;
3409         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3410
3411         while (1) {
3412                 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
3413                                       &length, &multi, 0, NULL);
3414                 if (ret) {
3415                         if (ret == -ENOENT) {
3416                                 /* removed block group at the tail */
3417                                 if (length == (u64)-1)
3418                                         break;
3419
3420                                 /* removed block group in the middle */
3421                                 goto next;
3422                         }
3423                         goto fail;
3424                 }
3425
3426                 num_stripes = multi->num_stripes;
3427                 physical = multi->stripes[0].physical;
3428                 kfree(multi);
3429
3430                 if (num_stripes != 1 || physical != bytenr)
3431                         goto fail;
3432 next:
3433                 bytenr += length;
3434                 if (bytenr >= total_bytes)
3435                         break;
3436         }
3437         return 0;
3438 fail:
3439         return -1;
3440 }
3441
3442 static int do_rollback(const char *devname)
3443 {
3444         int fd = -1;
3445         int ret;
3446         int i;
3447         struct btrfs_root *root;
3448         struct btrfs_root *image_root;
3449         struct btrfs_root *chunk_root;
3450         struct btrfs_dir_item *dir;
3451         struct btrfs_inode_item *inode;
3452         struct btrfs_file_extent_item *fi;
3453         struct btrfs_trans_handle *trans;
3454         struct extent_buffer *leaf;
3455         struct btrfs_block_group_cache *cache1;
3456         struct btrfs_block_group_cache *cache2;
3457         struct btrfs_key key;
3458         struct btrfs_path path;
3459         struct extent_io_tree io_tree;
3460         char *buf = NULL;
3461         char *name;
3462         u64 bytenr;
3463         u64 num_bytes;
3464         u64 root_dir;
3465         u64 objectid;
3466         u64 offset;
3467         u64 start;
3468         u64 end;
3469         u64 sb_bytenr;
3470         u64 first_free;
3471         u64 total_bytes;
3472         u32 sectorsize;
3473
3474         extent_io_tree_init(&io_tree);
3475
3476         fd = open(devname, O_RDWR);
3477         if (fd < 0) {
3478                 fprintf(stderr, "unable to open %s\n", devname);
3479                 goto fail;
3480         }
3481         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
3482         if (!root) {
3483                 fprintf(stderr, "unable to open ctree\n");
3484                 goto fail;
3485         }
3486         ret = may_rollback(root);
3487         if (ret < 0) {
3488                 fprintf(stderr, "unable to do rollback\n");
3489                 goto fail;
3490         }
3491
3492         sectorsize = root->sectorsize;
3493         buf = malloc(sectorsize);
3494         if (!buf) {
3495                 fprintf(stderr, "unable to allocate memory\n");
3496                 goto fail;
3497         }
3498
3499         btrfs_init_path(&path);
3500
3501         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3502         key.type = BTRFS_ROOT_BACKREF_KEY;
3503         key.offset = BTRFS_FS_TREE_OBJECTID;
3504         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path, 0,
3505                                 0);
3506         btrfs_release_path(&path);
3507         if (ret > 0) {
3508                 fprintf(stderr,
3509                 "ERROR: unable to convert ext2 image subvolume, is it deleted?\n");
3510                 goto fail;
3511         } else if (ret < 0) {
3512                 fprintf(stderr,
3513                         "ERROR: unable to open ext2_saved, id=%llu: %s\n",
3514                         (unsigned long long)key.objectid, strerror(-ret));
3515                 goto fail;
3516         }
3517
3518         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
3519         key.type = BTRFS_ROOT_ITEM_KEY;
3520         key.offset = (u64)-1;
3521         image_root = btrfs_read_fs_root(root->fs_info, &key);
3522         if (!image_root || IS_ERR(image_root)) {
3523                 fprintf(stderr, "unable to open subvol %llu\n",
3524                         (unsigned long long)key.objectid);
3525                 goto fail;
3526         }
3527
3528         name = "image";
3529         root_dir = btrfs_root_dirid(&root->root_item);
3530         dir = btrfs_lookup_dir_item(NULL, image_root, &path,
3531                                    root_dir, name, strlen(name), 0);
3532         if (!dir || IS_ERR(dir)) {
3533                 fprintf(stderr, "unable to find file %s\n", name);
3534                 goto fail;
3535         }
3536         leaf = path.nodes[0];
3537         btrfs_dir_item_key_to_cpu(leaf, dir, &key);
3538         btrfs_release_path(&path);
3539
3540         objectid = key.objectid;
3541
3542         ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
3543         if (ret) {
3544                 fprintf(stderr, "unable to find inode item\n");
3545                 goto fail;
3546         }
3547         leaf = path.nodes[0];
3548         inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
3549         total_bytes = btrfs_inode_size(leaf, inode);
3550         btrfs_release_path(&path);
3551
3552         key.objectid = objectid;
3553         key.offset = 0;
3554         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3555         ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
3556         if (ret != 0) {
3557                 fprintf(stderr, "unable to find first file extent\n");
3558                 btrfs_release_path(&path);
3559                 goto fail;
3560         }
3561
3562         /* build mapping tree for the relocated blocks */
3563         for (offset = 0; offset < total_bytes; ) {
3564                 leaf = path.nodes[0];
3565                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
3566                         ret = btrfs_next_leaf(root, &path);
3567                         if (ret != 0)
3568                                 break;  
3569                         continue;
3570                 }
3571
3572                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
3573                 if (key.objectid != objectid || key.offset != offset ||
3574                     btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3575                         break;
3576
3577                 fi = btrfs_item_ptr(leaf, path.slots[0],
3578                                     struct btrfs_file_extent_item);
3579                 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
3580                         break;
3581                 if (btrfs_file_extent_compression(leaf, fi) ||
3582                     btrfs_file_extent_encryption(leaf, fi) ||
3583                     btrfs_file_extent_other_encoding(leaf, fi))
3584                         break;
3585
3586                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3587                 /* skip holes and direct mapped extents */
3588                 if (bytenr == 0 || bytenr == offset)
3589                         goto next_extent;
3590
3591                 bytenr += btrfs_file_extent_offset(leaf, fi);
3592                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3593
3594                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3595                 cache2 =  btrfs_lookup_block_group(root->fs_info,
3596                                                    offset + num_bytes - 1);
3597                 if (!cache1 || cache1 != cache2 ||
3598                     (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
3599                      !intersect_with_sb(offset, num_bytes)))
3600                         break;
3601
3602                 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
3603                                 EXTENT_LOCKED, GFP_NOFS);
3604                 set_state_private(&io_tree, offset, bytenr);
3605 next_extent:
3606                 offset += btrfs_file_extent_num_bytes(leaf, fi);
3607                 path.slots[0]++;
3608         }
3609         btrfs_release_path(&path);
3610
3611         if (offset < total_bytes) {
3612                 fprintf(stderr, "unable to build extent mapping\n");
3613                 goto fail;
3614         }
3615
3616         first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
3617         first_free &= ~((u64)sectorsize - 1);
3618         /* backup for extent #0 should exist */
3619         if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
3620                 fprintf(stderr, "no backup for the first extent\n");
3621                 goto fail;
3622         }
3623         /* force no allocation from system block group */
3624         root->fs_info->system_allocs = -1;
3625         trans = btrfs_start_transaction(root, 1);
3626         BUG_ON(!trans);
3627         /*
3628          * recow the whole chunk tree, this will remove all chunk tree blocks
3629          * from system block group
3630          */
3631         chunk_root = root->fs_info->chunk_root;
3632         memset(&key, 0, sizeof(key));
3633         while (1) {
3634                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
3635                 if (ret < 0)
3636                         break;
3637
3638                 ret = btrfs_next_leaf(chunk_root, &path);
3639                 if (ret)
3640                         break;
3641
3642                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
3643                 btrfs_release_path(&path);
3644         }
3645         btrfs_release_path(&path);
3646
3647         offset = 0;
3648         num_bytes = 0;
3649         while(1) {
3650                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
3651                 if (!cache1)
3652                         break;
3653
3654                 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
3655                         num_bytes += btrfs_block_group_used(&cache1->item);
3656
3657                 offset = cache1->key.objectid + cache1->key.offset;
3658         }
3659         /* only extent #0 left in system block group? */
3660         if (num_bytes > first_free) {
3661                 fprintf(stderr, "unable to empty system block group\n");
3662                 goto fail;
3663         }
3664         /* create a system chunk that maps the whole device */
3665         ret = prepare_system_chunk_sb(root->fs_info->super_copy);
3666         if (ret) {
3667                 fprintf(stderr, "unable to update system chunk\n");
3668                 goto fail;
3669         }
3670
3671         ret = btrfs_commit_transaction(trans, root);
3672         BUG_ON(ret);
3673
3674         ret = close_ctree(root);
3675         if (ret) {
3676                 fprintf(stderr, "error during close_ctree %d\n", ret);
3677                 goto fail;
3678         }
3679
3680         /* zero btrfs super block mirrors */
3681         memset(buf, 0, sectorsize);
3682         for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3683                 bytenr = btrfs_sb_offset(i);
3684                 if (bytenr >= total_bytes)
3685                         break;
3686                 ret = pwrite(fd, buf, sectorsize, bytenr);
3687                 if (ret != sectorsize) {
3688                         fprintf(stderr,
3689                                 "error during zeroing superblock %d: %d\n",
3690                                 i, ret);
3691                         goto fail;
3692                 }
3693         }
3694
3695         sb_bytenr = (u64)-1;
3696         /* copy all relocated blocks back */
3697         while(1) {
3698                 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
3699                                             EXTENT_LOCKED);
3700                 if (ret)
3701                         break;
3702
3703                 ret = get_state_private(&io_tree, start, &bytenr);
3704                 BUG_ON(ret);
3705
3706                 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
3707                                   GFP_NOFS);
3708
3709                 while (start <= end) {
3710                         if (start == BTRFS_SUPER_INFO_OFFSET) {
3711                                 sb_bytenr = bytenr;
3712                                 goto next_sector;
3713                         }
3714                         ret = pread(fd, buf, sectorsize, bytenr);
3715                         if (ret < 0) {
3716                                 fprintf(stderr, "error during pread %d\n", ret);
3717                                 goto fail;
3718                         }
3719                         BUG_ON(ret != sectorsize);
3720                         ret = pwrite(fd, buf, sectorsize, start);
3721                         if (ret < 0) {
3722                                 fprintf(stderr, "error during pwrite %d\n", ret);
3723                                 goto fail;
3724                         }
3725                         BUG_ON(ret != sectorsize);
3726 next_sector:
3727                         start += sectorsize;
3728                         bytenr += sectorsize;
3729                 }
3730         }
3731
3732         ret = fsync(fd);
3733         if (ret) {
3734                 fprintf(stderr, "error during fsync %d\n", ret);
3735                 goto fail;
3736         }
3737         /*
3738          * finally, overwrite btrfs super block.
3739          */
3740         ret = pread(fd, buf, sectorsize, sb_bytenr);
3741         if (ret < 0) {
3742                 fprintf(stderr, "error during pread %d\n", ret);
3743                 goto fail;
3744         }
3745         BUG_ON(ret != sectorsize);
3746         ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
3747         if (ret < 0) {
3748                 fprintf(stderr, "error during pwrite %d\n", ret);
3749                 goto fail;
3750         }
3751         BUG_ON(ret != sectorsize);
3752         ret = fsync(fd);
3753         if (ret) {
3754                 fprintf(stderr, "error during fsync %d\n", ret);
3755                 goto fail;
3756         }
3757
3758         close(fd);
3759         free(buf);
3760         extent_io_tree_cleanup(&io_tree);
3761         printf("rollback complete.\n");
3762         return 0;
3763
3764 fail:
3765         if (fd != -1)
3766                 close(fd);
3767         free(buf);
3768         fprintf(stderr, "rollback aborted.\n");
3769         return -1;
3770 }
3771
3772 static void print_usage(void)
3773 {
3774         printf("usage: btrfs-convert [options] device\n");
3775         printf("options:\n");
3776         printf("\t-d|--no-datasum        disable data checksum, sets NODATASUM\n");
3777         printf("\t-i|--no-xattr          ignore xattrs and ACLs\n");
3778         printf("\t-n|--no-inline         disable inlining of small files to metadata\n");
3779         printf("\t-N|--nodesize SIZE     set filesystem metadata nodesize\n");
3780         printf("\t-r|--rollback          roll back to the original filesystem\n");
3781         printf("\t-l|--label LABEL       set filesystem label\n");
3782         printf("\t-L|--copy-label        use label from converted filesystem\n");
3783         printf("\t-p|--progress          show converting progress (default)\n");
3784         printf("\t-O|--features LIST     comma separated list of filesystem features\n");
3785         printf("\t--no-progress          show only overview, not the detailed progress\n");
3786 }
3787
3788 int main(int argc, char *argv[])
3789 {
3790         int ret;
3791         int packing = 1;
3792         int noxattr = 0;
3793         int datacsum = 1;
3794         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
3795                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
3796         int rollback = 0;
3797         int copylabel = 0;
3798         int usage_error = 0;
3799         int progress = 1;
3800         char *file;
3801         char fslabel[BTRFS_LABEL_SIZE];
3802         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
3803
3804         while(1) {
3805                 enum { GETOPT_VAL_NO_PROGRESS = 256 };
3806                 static const struct option long_options[] = {
3807                         { "no-progress", no_argument, NULL,
3808                                 GETOPT_VAL_NO_PROGRESS },
3809                         { "no-datasum", no_argument, NULL, 'd' },
3810                         { "no-inline", no_argument, NULL, 'n' },
3811                         { "no-xattr", no_argument, NULL, 'i' },
3812                         { "rollback", no_argument, NULL, 'r' },
3813                         { "features", required_argument, NULL, 'O' },
3814                         { "progress", no_argument, NULL, 'p' },
3815                         { "label", required_argument, NULL, 'l' },
3816                         { "copy-label", no_argument, NULL, 'L' },
3817                         { "nodesize", required_argument, NULL, 'N' },
3818                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
3819                         { NULL, 0, NULL, 0 }
3820                 };
3821                 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
3822
3823                 if (c < 0)
3824                         break;
3825                 switch(c) {
3826                         case 'd':
3827                                 datacsum = 0;
3828                                 break;
3829                         case 'i':
3830                                 noxattr = 1;
3831                                 break;
3832                         case 'n':
3833                                 packing = 0;
3834                                 break;
3835                         case 'N':
3836                                 nodesize = parse_size(optarg);
3837                                 break;
3838                         case 'r':
3839                                 rollback = 1;
3840                                 break;
3841                         case 'l':
3842                                 copylabel = -1;
3843                                 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
3844                                         fprintf(stderr,
3845                                 "WARNING: label too long, trimmed to %d bytes\n",
3846                                                 BTRFS_LABEL_SIZE - 1);
3847                                 }
3848                                 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
3849                                 break;
3850                         case 'L':
3851                                 copylabel = 1;
3852                                 break;
3853                         case 'p':
3854                                 progress = 1;
3855                                 break;
3856                         case 'O': {
3857                                 char *orig = strdup(optarg);
3858                                 char *tmp = orig;
3859
3860                                 tmp = btrfs_parse_fs_features(tmp, &features);
3861                                 if (tmp) {
3862                                         fprintf(stderr,
3863                                                 "Unrecognized filesystem feature '%s'\n",
3864                                                         tmp);
3865                                         free(orig);
3866                                         exit(1);
3867                                 }
3868                                 free(orig);
3869                                 if (features & BTRFS_FEATURE_LIST_ALL) {
3870                                         btrfs_list_all_fs_features(
3871                                                 ~BTRFS_CONVERT_ALLOWED_FEATURES);
3872                                         exit(0);
3873                                 }
3874                                 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
3875                                         char buf[64];
3876
3877                                         btrfs_parse_features_to_string(buf,
3878                                                 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
3879                                         fprintf(stderr,
3880                                                 "ERROR: features not allowed for convert: %s\n",
3881                                                 buf);
3882                                         exit(1);
3883                                 }
3884
3885                                 break;
3886                                 }
3887                         case GETOPT_VAL_NO_PROGRESS:
3888                                 progress = 0;
3889                                 break;
3890                         case GETOPT_VAL_HELP:
3891                         default:
3892                                 print_usage();
3893                                 return c != GETOPT_VAL_HELP;
3894                 }
3895         }
3896         set_argv0(argv);
3897         if (check_argc_exact(argc - optind, 1)) {
3898                 print_usage();
3899                 return 1;
3900         }
3901
3902         if (rollback && (!datacsum || noxattr || !packing)) {
3903                 fprintf(stderr,
3904                         "Usage error: -d, -i, -n options do not apply to rollback\n");
3905                 usage_error++;
3906         }
3907
3908         if (usage_error) {
3909                 print_usage();
3910                 return 1;
3911         }
3912
3913         file = argv[optind];
3914         ret = check_mounted(file);
3915         if (ret < 0) {
3916                 fprintf(stderr, "Could not check mount status: %s\n",
3917                         strerror(-ret));
3918                 return 1;
3919         } else if (ret) {
3920                 fprintf(stderr, "%s is mounted\n", file);
3921                 return 1;
3922         }
3923
3924         if (rollback) {
3925                 ret = do_rollback(file);
3926         } else {
3927                 ret = do_convert(file, datacsum, packing, noxattr, nodesize,
3928                                 copylabel, fslabel, progress, features);
3929         }
3930         if (ret)
3931                 return 1;
3932         return 0;
3933 }