btrfs-progs: convert: move ext2 conversion out of main.c
[platform/upstream/btrfs-progs.git] / convert / main.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 "help.h"
41 #include "mkfs/common.h"
42 #include "convert/common.h"
43 #include "convert/source-fs.h"
44 #include "fsfeatures.h"
45
46 static void *print_copied_inodes(void *p)
47 {
48         struct task_ctx *priv = p;
49         const char work_indicator[] = { '.', 'o', 'O', 'o' };
50         uint32_t count = 0;
51
52         task_period_start(priv->info, 1000 /* 1s */);
53         while (1) {
54                 count++;
55                 printf("copy inodes [%c] [%10d/%10d]\r",
56                        work_indicator[count % 4], priv->cur_copy_inodes,
57                        priv->max_copy_inodes);
58                 fflush(stdout);
59                 task_period_wait(priv->info);
60         }
61
62         return NULL;
63 }
64
65 static int after_copied_inodes(void *p)
66 {
67         printf("\n");
68         fflush(stdout);
69
70         return 0;
71 }
72
73 void init_convert_context(struct btrfs_convert_context *cctx)
74 {
75         cache_tree_init(&cctx->used);
76         cache_tree_init(&cctx->data_chunks);
77         cache_tree_init(&cctx->free);
78 }
79
80 void clean_convert_context(struct btrfs_convert_context *cctx)
81 {
82         free_extent_cache_tree(&cctx->used);
83         free_extent_cache_tree(&cctx->data_chunks);
84         free_extent_cache_tree(&cctx->free);
85 }
86
87 static inline int copy_inodes(struct btrfs_convert_context *cctx,
88                               struct btrfs_root *root, int datacsum,
89                               int packing, int noxattr, struct task_ctx *p)
90 {
91         return cctx->convert_ops->copy_inodes(cctx, root, datacsum, packing,
92                                              noxattr, p);
93 }
94
95 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
96 {
97         cctx->convert_ops->close_fs(cctx);
98 }
99
100 static inline int convert_check_state(struct btrfs_convert_context *cctx)
101 {
102         return cctx->convert_ops->check_state(cctx);
103 }
104
105 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
106 {
107         int i;
108         u64 offset;
109
110         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
111                 offset = btrfs_sb_offset(i);
112                 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
113
114                 if (bytenr < offset + BTRFS_STRIPE_LEN &&
115                     bytenr + num_bytes > offset)
116                         return 1;
117         }
118         return 0;
119 }
120
121 int convert_insert_dirent(struct btrfs_trans_handle *trans,
122                                  struct btrfs_root *root,
123                                  const char *name, size_t name_len,
124                                  u64 dir, u64 objectid,
125                                  u8 file_type, u64 index_cnt,
126                                  struct btrfs_inode_item *inode)
127 {
128         int ret;
129         u64 inode_size;
130         struct btrfs_key location = {
131                 .objectid = objectid,
132                 .offset = 0,
133                 .type = BTRFS_INODE_ITEM_KEY,
134         };
135
136         ret = btrfs_insert_dir_item(trans, root, name, name_len,
137                                     dir, &location, file_type, index_cnt);
138         if (ret)
139                 return ret;
140         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
141                                      objectid, dir, index_cnt);
142         if (ret)
143                 return ret;
144         inode_size = btrfs_stack_inode_size(inode) + name_len * 2;
145         btrfs_set_stack_inode_size(inode, inode_size);
146
147         return 0;
148 }
149
150 int read_disk_extent(struct btrfs_root *root, u64 bytenr,
151                             u32 num_bytes, char *buffer)
152 {
153         int ret;
154         struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
155
156         ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
157         if (ret != num_bytes)
158                 goto fail;
159         ret = 0;
160 fail:
161         if (ret > 0)
162                 ret = -1;
163         return ret;
164 }
165
166 static int csum_disk_extent(struct btrfs_trans_handle *trans,
167                             struct btrfs_root *root,
168                             u64 disk_bytenr, u64 num_bytes)
169 {
170         u32 blocksize = root->sectorsize;
171         u64 offset;
172         char *buffer;
173         int ret = 0;
174
175         buffer = malloc(blocksize);
176         if (!buffer)
177                 return -ENOMEM;
178         for (offset = 0; offset < num_bytes; offset += blocksize) {
179                 ret = read_disk_extent(root, disk_bytenr + offset,
180                                         blocksize, buffer);
181                 if (ret)
182                         break;
183                 ret = btrfs_csum_file_block(trans,
184                                             root->fs_info->csum_root,
185                                             disk_bytenr + num_bytes,
186                                             disk_bytenr + offset,
187                                             buffer, blocksize);
188                 if (ret)
189                         break;
190         }
191         free(buffer);
192         return ret;
193 }
194
195 void init_blk_iterate_data(struct blk_iterate_data *data,
196                                   struct btrfs_trans_handle *trans,
197                                   struct btrfs_root *root,
198                                   struct btrfs_inode_item *inode,
199                                   u64 objectid, int checksum)
200 {
201         struct btrfs_key key;
202
203         data->trans             = trans;
204         data->root              = root;
205         data->inode             = inode;
206         data->objectid          = objectid;
207         data->first_block       = 0;
208         data->disk_block        = 0;
209         data->num_blocks        = 0;
210         data->boundary          = (u64)-1;
211         data->checksum          = checksum;
212         data->errcode           = 0;
213
214         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
215         key.type = BTRFS_ROOT_ITEM_KEY;
216         key.offset = (u64)-1;
217         data->convert_root = btrfs_read_fs_root(root->fs_info, &key);
218         /* Impossible as we just opened it before */
219         BUG_ON(!data->convert_root || IS_ERR(data->convert_root));
220         data->convert_ino = BTRFS_FIRST_FREE_OBJECTID + 1;
221 }
222
223 /*
224  * Record a file extent in original filesystem into btrfs one.
225  * The special point is, old disk_block can point to a reserved range.
226  * So here, we don't use disk_block directly but search convert_root
227  * to get the real disk_bytenr.
228  */
229 int record_file_blocks(struct blk_iterate_data *data,
230                               u64 file_block, u64 disk_block, u64 num_blocks)
231 {
232         int ret = 0;
233         struct btrfs_root *root = data->root;
234         struct btrfs_root *convert_root = data->convert_root;
235         struct btrfs_path path;
236         u64 file_pos = file_block * root->sectorsize;
237         u64 old_disk_bytenr = disk_block * root->sectorsize;
238         u64 num_bytes = num_blocks * root->sectorsize;
239         u64 cur_off = old_disk_bytenr;
240
241         /* Hole, pass it to record_file_extent directly */
242         if (old_disk_bytenr == 0)
243                 return btrfs_record_file_extent(data->trans, root,
244                                 data->objectid, data->inode, file_pos, 0,
245                                 num_bytes);
246
247         btrfs_init_path(&path);
248
249         /*
250          * Search real disk bytenr from convert root
251          */
252         while (cur_off < old_disk_bytenr + num_bytes) {
253                 struct btrfs_key key;
254                 struct btrfs_file_extent_item *fi;
255                 struct extent_buffer *node;
256                 int slot;
257                 u64 extent_disk_bytenr;
258                 u64 extent_num_bytes;
259                 u64 real_disk_bytenr;
260                 u64 cur_len;
261
262                 key.objectid = data->convert_ino;
263                 key.type = BTRFS_EXTENT_DATA_KEY;
264                 key.offset = cur_off;
265
266                 ret = btrfs_search_slot(NULL, convert_root, &key, &path, 0, 0);
267                 if (ret < 0)
268                         break;
269                 if (ret > 0) {
270                         ret = btrfs_previous_item(convert_root, &path,
271                                                   data->convert_ino,
272                                                   BTRFS_EXTENT_DATA_KEY);
273                         if (ret < 0)
274                                 break;
275                         if (ret > 0) {
276                                 ret = -ENOENT;
277                                 break;
278                         }
279                 }
280                 node = path.nodes[0];
281                 slot = path.slots[0];
282                 btrfs_item_key_to_cpu(node, &key, slot);
283                 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY ||
284                        key.objectid != data->convert_ino ||
285                        key.offset > cur_off);
286                 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
287                 extent_disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
288                 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
289                 BUG_ON(cur_off - key.offset >= extent_num_bytes);
290                 btrfs_release_path(&path);
291
292                 if (extent_disk_bytenr)
293                         real_disk_bytenr = cur_off - key.offset +
294                                            extent_disk_bytenr;
295                 else
296                         real_disk_bytenr = 0;
297                 cur_len = min(key.offset + extent_num_bytes,
298                               old_disk_bytenr + num_bytes) - cur_off;
299                 ret = btrfs_record_file_extent(data->trans, data->root,
300                                         data->objectid, data->inode, file_pos,
301                                         real_disk_bytenr, cur_len);
302                 if (ret < 0)
303                         break;
304                 cur_off += cur_len;
305                 file_pos += cur_len;
306
307                 /*
308                  * No need to care about csum
309                  * As every byte of old fs image is calculated for csum, no
310                  * need to waste CPU cycles now.
311                  */
312         }
313         btrfs_release_path(&path);
314         return ret;
315 }
316
317 int block_iterate_proc(u64 disk_block, u64 file_block,
318                               struct blk_iterate_data *idata)
319 {
320         int ret = 0;
321         int sb_region;
322         int do_barrier;
323         struct btrfs_root *root = idata->root;
324         struct btrfs_block_group_cache *cache;
325         u64 bytenr = disk_block * root->sectorsize;
326
327         sb_region = intersect_with_sb(bytenr, root->sectorsize);
328         do_barrier = sb_region || disk_block >= idata->boundary;
329         if ((idata->num_blocks > 0 && do_barrier) ||
330             (file_block > idata->first_block + idata->num_blocks) ||
331             (disk_block != idata->disk_block + idata->num_blocks)) {
332                 if (idata->num_blocks > 0) {
333                         ret = record_file_blocks(idata, idata->first_block,
334                                                  idata->disk_block,
335                                                  idata->num_blocks);
336                         if (ret)
337                                 goto fail;
338                         idata->first_block += idata->num_blocks;
339                         idata->num_blocks = 0;
340                 }
341                 if (file_block > idata->first_block) {
342                         ret = record_file_blocks(idata, idata->first_block,
343                                         0, file_block - idata->first_block);
344                         if (ret)
345                                 goto fail;
346                 }
347
348                 if (sb_region) {
349                         bytenr += BTRFS_STRIPE_LEN - 1;
350                         bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
351                 } else {
352                         cache = btrfs_lookup_block_group(root->fs_info, bytenr);
353                         BUG_ON(!cache);
354                         bytenr = cache->key.objectid + cache->key.offset;
355                 }
356
357                 idata->first_block = file_block;
358                 idata->disk_block = disk_block;
359                 idata->boundary = bytenr / root->sectorsize;
360         }
361         idata->num_blocks++;
362 fail:
363         return ret;
364 }
365
366 static int create_image_file_range(struct btrfs_trans_handle *trans,
367                                       struct btrfs_root *root,
368                                       struct cache_tree *used,
369                                       struct btrfs_inode_item *inode,
370                                       u64 ino, u64 bytenr, u64 *ret_len,
371                                       int datacsum)
372 {
373         struct cache_extent *cache;
374         struct btrfs_block_group_cache *bg_cache;
375         u64 len = *ret_len;
376         u64 disk_bytenr;
377         int i;
378         int ret;
379
380         if (bytenr != round_down(bytenr, root->sectorsize)) {
381                 error("bytenr not sectorsize aligned: %llu",
382                                 (unsigned long long)bytenr);
383                 return -EINVAL;
384         }
385         if (len != round_down(len, root->sectorsize)) {
386                 error("length not sectorsize aligned: %llu",
387                                 (unsigned long long)len);
388                 return -EINVAL;
389         }
390         len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE);
391
392         /*
393          * Skip sb ranges first
394          * [0, 1M), [sb_offset(1), +64K), [sb_offset(2), +64K].
395          *
396          * Or we will insert a hole into current image file, and later
397          * migrate block will fail as there is already a file extent.
398          */
399         if (bytenr < 1024 * 1024) {
400                 *ret_len = 1024 * 1024 - bytenr;
401                 return 0;
402         }
403         for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
404                 u64 cur = btrfs_sb_offset(i);
405
406                 if (bytenr >= cur && bytenr < cur + BTRFS_STRIPE_LEN) {
407                         *ret_len = cur + BTRFS_STRIPE_LEN - bytenr;
408                         return 0;
409                 }
410         }
411         for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
412                 u64 cur = btrfs_sb_offset(i);
413
414                 /*
415                  *      |--reserved--|
416                  * |----range-------|
417                  * May still need to go through file extent inserts
418                  */
419                 if (bytenr < cur && bytenr + len >= cur) {
420                         len = min_t(u64, len, cur - bytenr);
421                         break;
422                 }
423                 /*
424                  * |--reserved--|
425                  *      |---range---|
426                  * Drop out, no need to insert anything
427                  */
428                 if (bytenr >= cur && bytenr < cur + BTRFS_STRIPE_LEN) {
429                         *ret_len = cur + BTRFS_STRIPE_LEN - bytenr;
430                         return 0;
431                 }
432         }
433
434         cache = search_cache_extent(used, bytenr);
435         if (cache) {
436                 if (cache->start <= bytenr) {
437                         /*
438                          * |///////Used///////|
439                          *      |<--insert--->|
440                          *      bytenr
441                          */
442                         len = min_t(u64, len, cache->start + cache->size -
443                                     bytenr);
444                         disk_bytenr = bytenr;
445                 } else {
446                         /*
447                          *              |//Used//|
448                          *  |<-insert-->|
449                          *  bytenr
450                          */
451                         len = min(len, cache->start - bytenr);
452                         disk_bytenr = 0;
453                         datacsum = 0;
454                 }
455         } else {
456                 /*
457                  * |//Used//|           |EOF
458                  *          |<-insert-->|
459                  *          bytenr
460                  */
461                 disk_bytenr = 0;
462                 datacsum = 0;
463         }
464
465         if (disk_bytenr) {
466                 /* Check if the range is in a data block group */
467                 bg_cache = btrfs_lookup_block_group(root->fs_info, bytenr);
468                 if (!bg_cache)
469                         return -ENOENT;
470                 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
471                         return -EINVAL;
472
473                 /* The extent should never cross block group boundary */
474                 len = min_t(u64, len, bg_cache->key.objectid +
475                             bg_cache->key.offset - bytenr);
476         }
477
478         if (len != round_down(len, root->sectorsize)) {
479                 error("remaining length not sectorsize aligned: %llu",
480                                 (unsigned long long)len);
481                 return -EINVAL;
482         }
483         ret = btrfs_record_file_extent(trans, root, ino, inode, bytenr,
484                                        disk_bytenr, len);
485         if (ret < 0)
486                 return ret;
487
488         if (datacsum)
489                 ret = csum_disk_extent(trans, root, bytenr, len);
490         *ret_len = len;
491         return ret;
492 }
493
494 /*
495  * Relocate old fs data in one reserved ranges
496  *
497  * Since all old fs data in reserved range is not covered by any chunk nor
498  * data extent, we don't need to handle any reference but add new
499  * extent/reference, which makes codes more clear
500  */
501 static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
502                                       struct btrfs_root *root,
503                                       struct cache_tree *used,
504                                       struct btrfs_inode_item *inode, int fd,
505                                       u64 ino, u64 start, u64 len, int datacsum)
506 {
507         u64 cur_off = start;
508         u64 cur_len = len;
509         u64 hole_start = start;
510         u64 hole_len;
511         struct cache_extent *cache;
512         struct btrfs_key key;
513         struct extent_buffer *eb;
514         int ret = 0;
515
516         while (cur_off < start + len) {
517                 cache = lookup_cache_extent(used, cur_off, cur_len);
518                 if (!cache)
519                         break;
520                 cur_off = max(cache->start, cur_off);
521                 cur_len = min(cache->start + cache->size, start + len) -
522                           cur_off;
523                 BUG_ON(cur_len < root->sectorsize);
524
525                 /* reserve extent for the data */
526                 ret = btrfs_reserve_extent(trans, root, cur_len, 0, 0, (u64)-1,
527                                            &key, 1);
528                 if (ret < 0)
529                         break;
530
531                 eb = malloc(sizeof(*eb) + cur_len);
532                 if (!eb) {
533                         ret = -ENOMEM;
534                         break;
535                 }
536
537                 ret = pread(fd, eb->data, cur_len, cur_off);
538                 if (ret < cur_len) {
539                         ret = (ret < 0 ? ret : -EIO);
540                         free(eb);
541                         break;
542                 }
543                 eb->start = key.objectid;
544                 eb->len = key.offset;
545
546                 /* Write the data */
547                 ret = write_and_map_eb(trans, root, eb);
548                 free(eb);
549                 if (ret < 0)
550                         break;
551
552                 /* Now handle extent item and file extent things */
553                 ret = btrfs_record_file_extent(trans, root, ino, inode, cur_off,
554                                                key.objectid, key.offset);
555                 if (ret < 0)
556                         break;
557                 /* Finally, insert csum items */
558                 if (datacsum)
559                         ret = csum_disk_extent(trans, root, key.objectid,
560                                                key.offset);
561
562                 /* Don't forget to insert hole */
563                 hole_len = cur_off - hole_start;
564                 if (hole_len) {
565                         ret = btrfs_record_file_extent(trans, root, ino, inode,
566                                         hole_start, 0, hole_len);
567                         if (ret < 0)
568                                 break;
569                 }
570
571                 cur_off += key.offset;
572                 hole_start = cur_off;
573                 cur_len = start + len - cur_off;
574         }
575         /* Last hole */
576         if (start + len - hole_start > 0)
577                 ret = btrfs_record_file_extent(trans, root, ino, inode,
578                                 hole_start, 0, start + len - hole_start);
579         return ret;
580 }
581
582 /*
583  * Relocate the used ext2 data in reserved ranges
584  * [0,1M)
585  * [btrfs_sb_offset(1), +BTRFS_STRIPE_LEN)
586  * [btrfs_sb_offset(2), +BTRFS_STRIPE_LEN)
587  */
588 static int migrate_reserved_ranges(struct btrfs_trans_handle *trans,
589                                    struct btrfs_root *root,
590                                    struct cache_tree *used,
591                                    struct btrfs_inode_item *inode, int fd,
592                                    u64 ino, u64 total_bytes, int datacsum)
593 {
594         u64 cur_off;
595         u64 cur_len;
596         int ret = 0;
597
598         /* 0 ~ 1M */
599         cur_off = 0;
600         cur_len = 1024 * 1024;
601         ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
602                                          cur_off, cur_len, datacsum);
603         if (ret < 0)
604                 return ret;
605
606         /* second sb(fisrt sb is included in 0~1M) */
607         cur_off = btrfs_sb_offset(1);
608         cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
609         if (cur_off > total_bytes)
610                 return ret;
611         ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
612                                          cur_off, cur_len, datacsum);
613         if (ret < 0)
614                 return ret;
615
616         /* Last sb */
617         cur_off = btrfs_sb_offset(2);
618         cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
619         if (cur_off > total_bytes)
620                 return ret;
621         ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
622                                          cur_off, cur_len, datacsum);
623         return ret;
624 }
625
626 /*
627  * Helper for expand and merge extent_cache for wipe_one_reserved_range() to
628  * handle wiping a range that exists in cache.
629  */
630 static int _expand_extent_cache(struct cache_tree *tree,
631                                 struct cache_extent *entry,
632                                 u64 min_stripe_size, int backward)
633 {
634         struct cache_extent *ce;
635         int diff;
636
637         if (entry->size >= min_stripe_size)
638                 return 0;
639         diff = min_stripe_size - entry->size;
640
641         if (backward) {
642                 ce = prev_cache_extent(entry);
643                 if (!ce)
644                         goto expand_back;
645                 if (ce->start + ce->size >= entry->start - diff) {
646                         /* Directly merge with previous extent */
647                         ce->size = entry->start + entry->size - ce->start;
648                         remove_cache_extent(tree, entry);
649                         free(entry);
650                         return 0;
651                 }
652 expand_back:
653                 /* No overlap, normal extent */
654                 if (entry->start < diff) {
655                         error("cannot find space for data chunk layout");
656                         return -ENOSPC;
657                 }
658                 entry->start -= diff;
659                 entry->size += diff;
660                 return 0;
661         }
662         ce = next_cache_extent(entry);
663         if (!ce)
664                 goto expand_after;
665         if (entry->start + entry->size + diff >= ce->start) {
666                 /* Directly merge with next extent */
667                 entry->size = ce->start + ce->size - entry->start;
668                 remove_cache_extent(tree, ce);
669                 free(ce);
670                 return 0;
671         }
672 expand_after:
673         entry->size += diff;
674         return 0;
675 }
676
677 /*
678  * Remove one reserve range from given cache tree
679  * if min_stripe_size is non-zero, it will ensure for split case,
680  * all its split cache extent is no smaller than @min_strip_size / 2.
681  */
682 static int wipe_one_reserved_range(struct cache_tree *tree,
683                                    u64 start, u64 len, u64 min_stripe_size,
684                                    int ensure_size)
685 {
686         struct cache_extent *cache;
687         int ret;
688
689         BUG_ON(ensure_size && min_stripe_size == 0);
690         /*
691          * The logical here is simplified to handle special cases only
692          * So we don't need to consider merge case for ensure_size
693          */
694         BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
695                min_stripe_size / 2 < BTRFS_STRIPE_LEN));
696
697         /* Also, wipe range should already be aligned */
698         BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
699                start + len != round_up(start + len, BTRFS_STRIPE_LEN));
700
701         min_stripe_size /= 2;
702
703         cache = lookup_cache_extent(tree, start, len);
704         if (!cache)
705                 return 0;
706
707         if (start <= cache->start) {
708                 /*
709                  *      |--------cache---------|
710                  * |-wipe-|
711                  */
712                 BUG_ON(start + len <= cache->start);
713
714                 /*
715                  * The wipe size is smaller than min_stripe_size / 2,
716                  * so the result length should still meet min_stripe_size
717                  * And no need to do alignment
718                  */
719                 cache->size -= (start + len - cache->start);
720                 if (cache->size == 0) {
721                         remove_cache_extent(tree, cache);
722                         free(cache);
723                         return 0;
724                 }
725
726                 BUG_ON(ensure_size && cache->size < min_stripe_size);
727
728                 cache->start = start + len;
729                 return 0;
730         } else if (start > cache->start && start + len < cache->start +
731                    cache->size) {
732                 /*
733                  * |-------cache-----|
734                  *      |-wipe-|
735                  */
736                 u64 old_start = cache->start;
737                 u64 old_len = cache->size;
738                 u64 insert_start = start + len;
739                 u64 insert_len;
740
741                 cache->size = start - cache->start;
742                 /* Expand the leading half part if needed */
743                 if (ensure_size && cache->size < min_stripe_size) {
744                         ret = _expand_extent_cache(tree, cache,
745                                         min_stripe_size, 1);
746                         if (ret < 0)
747                                 return ret;
748                 }
749
750                 /* And insert the new one */
751                 insert_len = old_start + old_len - start - len;
752                 ret = add_merge_cache_extent(tree, insert_start, insert_len);
753                 if (ret < 0)
754                         return ret;
755
756                 /* Expand the last half part if needed */
757                 if (ensure_size && insert_len < min_stripe_size) {
758                         cache = lookup_cache_extent(tree, insert_start,
759                                                     insert_len);
760                         if (!cache || cache->start != insert_start ||
761                             cache->size != insert_len)
762                                 return -ENOENT;
763                         ret = _expand_extent_cache(tree, cache,
764                                         min_stripe_size, 0);
765                 }
766
767                 return ret;
768         }
769         /*
770          * |----cache-----|
771          *              |--wipe-|
772          * Wipe len should be small enough and no need to expand the
773          * remaining extent
774          */
775         cache->size = start - cache->start;
776         BUG_ON(ensure_size && cache->size < min_stripe_size);
777         return 0;
778 }
779
780 /*
781  * Remove reserved ranges from given cache_tree
782  *
783  * It will remove the following ranges
784  * 1) 0~1M
785  * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
786  * 3) 3rd superblock, +64K
787  *
788  * @min_stripe must be given for safety check
789  * and if @ensure_size is given, it will ensure affected cache_extent will be
790  * larger than min_stripe_size
791  */
792 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
793                                 int ensure_size)
794 {
795         int ret;
796
797         ret = wipe_one_reserved_range(tree, 0, 1024 * 1024, min_stripe_size,
798                                       ensure_size);
799         if (ret < 0)
800                 return ret;
801         ret = wipe_one_reserved_range(tree, btrfs_sb_offset(1),
802                         BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
803         if (ret < 0)
804                 return ret;
805         ret = wipe_one_reserved_range(tree, btrfs_sb_offset(2),
806                         BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
807         return ret;
808 }
809
810 static int calculate_available_space(struct btrfs_convert_context *cctx)
811 {
812         struct cache_tree *used = &cctx->used;
813         struct cache_tree *data_chunks = &cctx->data_chunks;
814         struct cache_tree *free = &cctx->free;
815         struct cache_extent *cache;
816         u64 cur_off = 0;
817         /*
818          * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
819          * works without need to consider overlap
820          */
821         u64 min_stripe_size = 2 * 16 * 1024 * 1024;
822         int ret;
823
824         /* Calculate data_chunks */
825         for (cache = first_cache_extent(used); cache;
826              cache = next_cache_extent(cache)) {
827                 u64 cur_len;
828
829                 if (cache->start + cache->size < cur_off)
830                         continue;
831                 if (cache->start > cur_off + min_stripe_size)
832                         cur_off = cache->start;
833                 cur_len = max(cache->start + cache->size - cur_off,
834                               min_stripe_size);
835                 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
836                 if (ret < 0)
837                         goto out;
838                 cur_off += cur_len;
839         }
840         /*
841          * remove reserved ranges, so we won't ever bother relocating an old
842          * filesystem extent to other place.
843          */
844         ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
845         if (ret < 0)
846                 goto out;
847
848         cur_off = 0;
849         /*
850          * Calculate free space
851          * Always round up the start bytenr, to avoid metadata extent corss
852          * stripe boundary, as later mkfs_convert() won't have all the extent
853          * allocation check
854          */
855         for (cache = first_cache_extent(data_chunks); cache;
856              cache = next_cache_extent(cache)) {
857                 if (cache->start < cur_off)
858                         continue;
859                 if (cache->start > cur_off) {
860                         u64 insert_start;
861                         u64 len;
862
863                         len = cache->start - round_up(cur_off,
864                                                       BTRFS_STRIPE_LEN);
865                         insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
866
867                         ret = add_merge_cache_extent(free, insert_start, len);
868                         if (ret < 0)
869                                 goto out;
870                 }
871                 cur_off = cache->start + cache->size;
872         }
873         /* Don't forget the last range */
874         if (cctx->total_bytes > cur_off) {
875                 u64 len = cctx->total_bytes - cur_off;
876                 u64 insert_start;
877
878                 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
879
880                 ret = add_merge_cache_extent(free, insert_start, len);
881                 if (ret < 0)
882                         goto out;
883         }
884
885         /* Remove reserved bytes */
886         ret = wipe_reserved_ranges(free, min_stripe_size, 0);
887 out:
888         return ret;
889 }
890
891 /*
892  * Read used space, and since we have the used space,
893  * calcuate data_chunks and free for later mkfs
894  */
895 static int convert_read_used_space(struct btrfs_convert_context *cctx)
896 {
897         int ret;
898
899         ret = cctx->convert_ops->read_used_space(cctx);
900         if (ret)
901                 return ret;
902
903         ret = calculate_available_space(cctx);
904         return ret;
905 }
906
907 /*
908  * Create the fs image file of old filesystem.
909  *
910  * This is completely fs independent as we have cctx->used, only
911  * need to create file extents pointing to all the positions.
912  */
913 static int create_image(struct btrfs_root *root,
914                            struct btrfs_mkfs_config *cfg,
915                            struct btrfs_convert_context *cctx, int fd,
916                            u64 size, char *name, int datacsum)
917 {
918         struct btrfs_inode_item buf;
919         struct btrfs_trans_handle *trans;
920         struct btrfs_path path;
921         struct btrfs_key key;
922         struct cache_extent *cache;
923         struct cache_tree used_tmp;
924         u64 cur;
925         u64 ino;
926         u64 flags = BTRFS_INODE_READONLY;
927         int ret;
928
929         if (!datacsum)
930                 flags |= BTRFS_INODE_NODATASUM;
931
932         trans = btrfs_start_transaction(root, 1);
933         if (!trans)
934                 return -ENOMEM;
935
936         cache_tree_init(&used_tmp);
937         btrfs_init_path(&path);
938
939         ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
940                                        &ino);
941         if (ret < 0)
942                 goto out;
943         ret = btrfs_new_inode(trans, root, ino, 0400 | S_IFREG);
944         if (ret < 0)
945                 goto out;
946         ret = btrfs_change_inode_flags(trans, root, ino, flags);
947         if (ret < 0)
948                 goto out;
949         ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
950                              strlen(name), BTRFS_FT_REG_FILE, NULL, 1);
951         if (ret < 0)
952                 goto out;
953
954         key.objectid = ino;
955         key.type = BTRFS_INODE_ITEM_KEY;
956         key.offset = 0;
957
958         ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
959         if (ret) {
960                 ret = (ret > 0 ? -ENOENT : ret);
961                 goto out;
962         }
963         read_extent_buffer(path.nodes[0], &buf,
964                         btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
965                         sizeof(buf));
966         btrfs_release_path(&path);
967
968         /*
969          * Create a new used space cache, which doesn't contain the reserved
970          * range
971          */
972         for (cache = first_cache_extent(&cctx->used); cache;
973              cache = next_cache_extent(cache)) {
974                 ret = add_cache_extent(&used_tmp, cache->start, cache->size);
975                 if (ret < 0)
976                         goto out;
977         }
978         ret = wipe_reserved_ranges(&used_tmp, 0, 0);
979         if (ret < 0)
980                 goto out;
981
982         /*
983          * Start from 1M, as 0~1M is reserved, and create_image_file_range()
984          * can't handle bytenr 0(will consider it as a hole)
985          */
986         cur = 1024 * 1024;
987         while (cur < size) {
988                 u64 len = size - cur;
989
990                 ret = create_image_file_range(trans, root, &used_tmp,
991                                                 &buf, ino, cur, &len, datacsum);
992                 if (ret < 0)
993                         goto out;
994                 cur += len;
995         }
996         /* Handle the reserved ranges */
997         ret = migrate_reserved_ranges(trans, root, &cctx->used, &buf, fd, ino,
998                                       cfg->num_bytes, datacsum);
999
1000
1001         key.objectid = ino;
1002         key.type = BTRFS_INODE_ITEM_KEY;
1003         key.offset = 0;
1004         ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
1005         if (ret) {
1006                 ret = (ret > 0 ? -ENOENT : ret);
1007                 goto out;
1008         }
1009         btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
1010         write_extent_buffer(path.nodes[0], &buf,
1011                         btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
1012                         sizeof(buf));
1013 out:
1014         free_extent_cache_tree(&used_tmp);
1015         btrfs_release_path(&path);
1016         btrfs_commit_transaction(trans, root);
1017         return ret;
1018 }
1019
1020 static struct btrfs_root* link_subvol(struct btrfs_root *root,
1021                 const char *base, u64 root_objectid)
1022 {
1023         struct btrfs_trans_handle *trans;
1024         struct btrfs_fs_info *fs_info = root->fs_info;
1025         struct btrfs_root *tree_root = fs_info->tree_root;
1026         struct btrfs_root *new_root = NULL;
1027         struct btrfs_path path;
1028         struct btrfs_inode_item *inode_item;
1029         struct extent_buffer *leaf;
1030         struct btrfs_key key;
1031         u64 dirid = btrfs_root_dirid(&root->root_item);
1032         u64 index = 2;
1033         char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
1034         int len;
1035         int i;
1036         int ret;
1037
1038         len = strlen(base);
1039         if (len == 0 || len > BTRFS_NAME_LEN)
1040                 return NULL;
1041
1042         btrfs_init_path(&path);
1043         key.objectid = dirid;
1044         key.type = BTRFS_DIR_INDEX_KEY;
1045         key.offset = (u64)-1;
1046
1047         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1048         if (ret <= 0) {
1049                 error("search for DIR_INDEX dirid %llu failed: %d",
1050                                 (unsigned long long)dirid, ret);
1051                 goto fail;
1052         }
1053
1054         if (path.slots[0] > 0) {
1055                 path.slots[0]--;
1056                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
1057                 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1058                         index = key.offset + 1;
1059         }
1060         btrfs_release_path(&path);
1061
1062         trans = btrfs_start_transaction(root, 1);
1063         if (!trans) {
1064                 error("unable to start transaction");
1065                 goto fail;
1066         }
1067
1068         key.objectid = dirid;
1069         key.offset = 0;
1070         key.type =  BTRFS_INODE_ITEM_KEY;
1071
1072         ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1073         if (ret) {
1074                 error("search for INODE_ITEM %llu failed: %d",
1075                                 (unsigned long long)dirid, ret);
1076                 goto fail;
1077         }
1078         leaf = path.nodes[0];
1079         inode_item = btrfs_item_ptr(leaf, path.slots[0],
1080                                     struct btrfs_inode_item);
1081
1082         key.objectid = root_objectid;
1083         key.offset = (u64)-1;
1084         key.type = BTRFS_ROOT_ITEM_KEY;
1085
1086         memcpy(buf, base, len);
1087         for (i = 0; i < 1024; i++) {
1088                 ret = btrfs_insert_dir_item(trans, root, buf, len,
1089                                             dirid, &key, BTRFS_FT_DIR, index);
1090                 if (ret != -EEXIST)
1091                         break;
1092                 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
1093                 if (len < 1 || len > BTRFS_NAME_LEN) {
1094                         ret = -EINVAL;
1095                         break;
1096                 }
1097         }
1098         if (ret)
1099                 goto fail;
1100
1101         btrfs_set_inode_size(leaf, inode_item, len * 2 +
1102                              btrfs_inode_size(leaf, inode_item));
1103         btrfs_mark_buffer_dirty(leaf);
1104         btrfs_release_path(&path);
1105
1106         /* add the backref first */
1107         ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1108                                  BTRFS_ROOT_BACKREF_KEY,
1109                                  root->root_key.objectid,
1110                                  dirid, index, buf, len);
1111         if (ret) {
1112                 error("unable to add root backref for %llu: %d",
1113                                 root->root_key.objectid, ret);
1114                 goto fail;
1115         }
1116
1117         /* now add the forward ref */
1118         ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1119                                  BTRFS_ROOT_REF_KEY, root_objectid,
1120                                  dirid, index, buf, len);
1121         if (ret) {
1122                 error("unable to add root ref for %llu: %d",
1123                                 root->root_key.objectid, ret);
1124                 goto fail;
1125         }
1126
1127         ret = btrfs_commit_transaction(trans, root);
1128         if (ret) {
1129                 error("transaction commit failed: %d", ret);
1130                 goto fail;
1131         }
1132
1133         new_root = btrfs_read_fs_root(fs_info, &key);
1134         if (IS_ERR(new_root)) {
1135                 error("unable to fs read root: %lu", PTR_ERR(new_root));
1136                 new_root = NULL;
1137         }
1138 fail:
1139         btrfs_init_path(&path);
1140         return new_root;
1141 }
1142
1143 static int create_subvol(struct btrfs_trans_handle *trans,
1144                          struct btrfs_root *root, u64 root_objectid)
1145 {
1146         struct extent_buffer *tmp;
1147         struct btrfs_root *new_root;
1148         struct btrfs_key key;
1149         struct btrfs_root_item root_item;
1150         int ret;
1151
1152         ret = btrfs_copy_root(trans, root, root->node, &tmp,
1153                               root_objectid);
1154         if (ret)
1155                 return ret;
1156
1157         memcpy(&root_item, &root->root_item, sizeof(root_item));
1158         btrfs_set_root_bytenr(&root_item, tmp->start);
1159         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1160         btrfs_set_root_generation(&root_item, trans->transid);
1161         free_extent_buffer(tmp);
1162
1163         key.objectid = root_objectid;
1164         key.type = BTRFS_ROOT_ITEM_KEY;
1165         key.offset = trans->transid;
1166         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1167                                 &key, &root_item);
1168
1169         key.offset = (u64)-1;
1170         new_root = btrfs_read_fs_root(root->fs_info, &key);
1171         if (!new_root || IS_ERR(new_root)) {
1172                 error("unable to fs read root: %lu", PTR_ERR(new_root));
1173                 return PTR_ERR(new_root);
1174         }
1175
1176         ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1177
1178         return ret;
1179 }
1180
1181 /*
1182  * New make_btrfs() has handle system and meta chunks quite well.
1183  * So only need to add remaining data chunks.
1184  */
1185 static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
1186                                           struct btrfs_fs_info *fs_info,
1187                                           struct btrfs_mkfs_config *cfg,
1188                                           struct btrfs_convert_context *cctx)
1189 {
1190         struct btrfs_root *extent_root = fs_info->extent_root;
1191         struct cache_tree *data_chunks = &cctx->data_chunks;
1192         struct cache_extent *cache;
1193         u64 max_chunk_size;
1194         int ret = 0;
1195
1196         /*
1197          * Don't create data chunk over 10% of the convert device
1198          * And for single chunk, don't create chunk larger than 1G.
1199          */
1200         max_chunk_size = cfg->num_bytes / 10;
1201         max_chunk_size = min((u64)(1024 * 1024 * 1024), max_chunk_size);
1202         max_chunk_size = round_down(max_chunk_size, extent_root->sectorsize);
1203
1204         for (cache = first_cache_extent(data_chunks); cache;
1205              cache = next_cache_extent(cache)) {
1206                 u64 cur = cache->start;
1207
1208                 while (cur < cache->start + cache->size) {
1209                         u64 len;
1210                         u64 cur_backup = cur;
1211
1212                         len = min(max_chunk_size,
1213                                   cache->start + cache->size - cur);
1214                         ret = btrfs_alloc_data_chunk(trans, extent_root,
1215                                         &cur_backup, len,
1216                                         BTRFS_BLOCK_GROUP_DATA, 1);
1217                         if (ret < 0)
1218                                 break;
1219                         ret = btrfs_make_block_group(trans, extent_root, 0,
1220                                         BTRFS_BLOCK_GROUP_DATA,
1221                                         BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1222                                         cur, len);
1223                         if (ret < 0)
1224                                 break;
1225                         cur += len;
1226                 }
1227         }
1228         return ret;
1229 }
1230
1231 /*
1232  * Init the temp btrfs to a operational status.
1233  *
1234  * It will fix the extent usage accounting(XXX: Do we really need?) and
1235  * insert needed data chunks, to ensure all old fs data extents are covered
1236  * by DATA chunks, preventing wrong chunks are allocated.
1237  *
1238  * And also create convert image subvolume and relocation tree.
1239  * (XXX: Not need again?)
1240  * But the convert image subvolume is *NOT* linked to fs tree yet.
1241  */
1242 static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
1243                          struct btrfs_convert_context *cctx, int datacsum,
1244                          int packing, int noxattr)
1245 {
1246         struct btrfs_key location;
1247         struct btrfs_trans_handle *trans;
1248         struct btrfs_fs_info *fs_info = root->fs_info;
1249         int ret;
1250
1251         /*
1252          * Don't alloc any metadata/system chunk, as we don't want
1253          * any meta/sys chunk allcated before all data chunks are inserted.
1254          * Or we screw up the chunk layout just like the old implement.
1255          */
1256         fs_info->avoid_sys_chunk_alloc = 1;
1257         fs_info->avoid_meta_chunk_alloc = 1;
1258         trans = btrfs_start_transaction(root, 1);
1259         if (!trans) {
1260                 error("unable to start transaction");
1261                 ret = -EINVAL;
1262                 goto err;
1263         }
1264         ret = btrfs_fix_block_accounting(trans, root);
1265         if (ret)
1266                 goto err;
1267         ret = make_convert_data_block_groups(trans, fs_info, cfg, cctx);
1268         if (ret)
1269                 goto err;
1270         ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1271                                   BTRFS_ROOT_TREE_DIR_OBJECTID);
1272         if (ret)
1273                 goto err;
1274         memcpy(&location, &root->root_key, sizeof(location));
1275         location.offset = (u64)-1;
1276         ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1277                                 btrfs_super_root_dir(fs_info->super_copy),
1278                                 &location, BTRFS_FT_DIR, 0);
1279         if (ret)
1280                 goto err;
1281         ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1282                                 location.objectid,
1283                                 btrfs_super_root_dir(fs_info->super_copy), 0);
1284         if (ret)
1285                 goto err;
1286         btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1287                              BTRFS_FIRST_FREE_OBJECTID);
1288
1289         /* subvol for fs image file */
1290         ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
1291         if (ret < 0) {
1292                 error("failed to create subvolume image root: %d", ret);
1293                 goto err;
1294         }
1295         /* subvol for data relocation tree */
1296         ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1297         if (ret < 0) {
1298                 error("failed to create DATA_RELOC root: %d", ret);
1299                 goto err;
1300         }
1301
1302         ret = btrfs_commit_transaction(trans, root);
1303         fs_info->avoid_sys_chunk_alloc = 0;
1304         fs_info->avoid_meta_chunk_alloc = 0;
1305 err:
1306         return ret;
1307 }
1308
1309 /*
1310  * Migrate super block to its default position and zero 0 ~ 16k
1311  */
1312 static int migrate_super_block(int fd, u64 old_bytenr)
1313 {
1314         int ret;
1315         struct extent_buffer *buf;
1316         struct btrfs_super_block *super;
1317         u32 len;
1318         u32 bytenr;
1319
1320         buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
1321         if (!buf)
1322                 return -ENOMEM;
1323
1324         buf->len = BTRFS_SUPER_INFO_SIZE;
1325         ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, old_bytenr);
1326         if (ret != BTRFS_SUPER_INFO_SIZE)
1327                 goto fail;
1328
1329         super = (struct btrfs_super_block *)buf->data;
1330         BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1331         btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1332
1333         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1334         ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
1335                 BTRFS_SUPER_INFO_OFFSET);
1336         if (ret != BTRFS_SUPER_INFO_SIZE)
1337                 goto fail;
1338
1339         ret = fsync(fd);
1340         if (ret)
1341                 goto fail;
1342
1343         memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
1344         for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1345                 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1346                 if (len > BTRFS_SUPER_INFO_SIZE)
1347                         len = BTRFS_SUPER_INFO_SIZE;
1348                 ret = pwrite(fd, buf->data, len, bytenr);
1349                 if (ret != len) {
1350                         fprintf(stderr, "unable to zero fill device\n");
1351                         break;
1352                 }
1353                 bytenr += len;
1354         }
1355         ret = 0;
1356         fsync(fd);
1357 fail:
1358         free(buf);
1359         if (ret > 0)
1360                 ret = -1;
1361         return ret;
1362 }
1363
1364 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1365 {
1366         struct btrfs_chunk *chunk;
1367         struct btrfs_disk_key *key;
1368         u32 sectorsize = btrfs_super_sectorsize(super);
1369
1370         key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1371         chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1372                                        sizeof(struct btrfs_disk_key));
1373
1374         btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1375         btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1376         btrfs_set_disk_key_offset(key, 0);
1377
1378         btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1379         btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1380         btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1381         btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1382         btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1383         btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1384         btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1385         btrfs_set_stack_chunk_num_stripes(chunk, 1);
1386         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1387         chunk->stripe.devid = super->dev_item.devid;
1388         btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1389         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1390         btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1391         return 0;
1392 }
1393
1394 const struct btrfs_convert_operations ext2_convert_ops;
1395
1396 static const struct btrfs_convert_operations *convert_operations[] = {
1397 #if BTRFSCONVERT_EXT2
1398         &ext2_convert_ops,
1399 #endif
1400 };
1401
1402 static int convert_open_fs(const char *devname,
1403                            struct btrfs_convert_context *cctx)
1404 {
1405         int i;
1406
1407         memset(cctx, 0, sizeof(*cctx));
1408
1409         for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
1410                 int ret = convert_operations[i]->open_fs(cctx, devname);
1411
1412                 if (ret == 0) {
1413                         cctx->convert_ops = convert_operations[i];
1414                         return ret;
1415                 }
1416         }
1417
1418         error("no file system found to convert");
1419         return -1;
1420 }
1421
1422 static int do_convert(const char *devname, int datacsum, int packing,
1423                 int noxattr, u32 nodesize, int copylabel, const char *fslabel,
1424                 int progress, u64 features)
1425 {
1426         int ret;
1427         int fd = -1;
1428         u32 blocksize;
1429         u64 total_bytes;
1430         struct btrfs_root *root;
1431         struct btrfs_root *image_root;
1432         struct btrfs_convert_context cctx;
1433         struct btrfs_key key;
1434         char *subvol_name = NULL;
1435         struct task_ctx ctx;
1436         char features_buf[64];
1437         struct btrfs_mkfs_config mkfs_cfg;
1438
1439         init_convert_context(&cctx);
1440         ret = convert_open_fs(devname, &cctx);
1441         if (ret)
1442                 goto fail;
1443         ret = convert_check_state(&cctx);
1444         if (ret)
1445                 warning(
1446                 "source filesystem is not clean, running filesystem check is recommended");
1447         ret = convert_read_used_space(&cctx);
1448         if (ret)
1449                 goto fail;
1450
1451         blocksize = cctx.blocksize;
1452         total_bytes = (u64)blocksize * (u64)cctx.block_count;
1453         if (blocksize < 4096) {
1454                 error("block size is too small: %u < 4096", blocksize);
1455                 goto fail;
1456         }
1457         if (btrfs_check_nodesize(nodesize, blocksize, features))
1458                 goto fail;
1459         fd = open(devname, O_RDWR);
1460         if (fd < 0) {
1461                 error("unable to open %s: %s", devname, strerror(errno));
1462                 goto fail;
1463         }
1464         btrfs_parse_features_to_string(features_buf, features);
1465         if (features == BTRFS_MKFS_DEFAULT_FEATURES)
1466                 strcat(features_buf, " (default)");
1467
1468         printf("create btrfs filesystem:\n");
1469         printf("\tblocksize: %u\n", blocksize);
1470         printf("\tnodesize:  %u\n", nodesize);
1471         printf("\tfeatures:  %s\n", features_buf);
1472
1473         mkfs_cfg.label = cctx.volume_name;
1474         mkfs_cfg.num_bytes = total_bytes;
1475         mkfs_cfg.nodesize = nodesize;
1476         mkfs_cfg.sectorsize = blocksize;
1477         mkfs_cfg.stripesize = blocksize;
1478         mkfs_cfg.features = features;
1479         /* New convert need these space */
1480         memset(mkfs_cfg.chunk_uuid, 0, BTRFS_UUID_UNPARSED_SIZE);
1481         memset(mkfs_cfg.fs_uuid, 0, BTRFS_UUID_UNPARSED_SIZE);
1482
1483         ret = make_convert_btrfs(fd, &mkfs_cfg, &cctx);
1484         if (ret) {
1485                 error("unable to create initial ctree: %s", strerror(-ret));
1486                 goto fail;
1487         }
1488
1489         root = open_ctree_fd(fd, devname, mkfs_cfg.super_bytenr,
1490                              OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1491         if (!root) {
1492                 error("unable to open ctree");
1493                 goto fail;
1494         }
1495         ret = init_btrfs(&mkfs_cfg, root, &cctx, datacsum, packing, noxattr);
1496         if (ret) {
1497                 error("unable to setup the root tree: %d", ret);
1498                 goto fail;
1499         }
1500
1501         printf("creating %s image file\n", cctx.convert_ops->name);
1502         ret = asprintf(&subvol_name, "%s_saved", cctx.convert_ops->name);
1503         if (ret < 0) {
1504                 error("memory allocation failure for subvolume name: %s_saved",
1505                         cctx.convert_ops->name);
1506                 goto fail;
1507         }
1508         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1509         key.offset = (u64)-1;
1510         key.type = BTRFS_ROOT_ITEM_KEY;
1511         image_root = btrfs_read_fs_root(root->fs_info, &key);
1512         if (!image_root) {
1513                 error("unable to create image subvolume");
1514                 goto fail;
1515         }
1516         ret = create_image(image_root, &mkfs_cfg, &cctx, fd,
1517                               mkfs_cfg.num_bytes, "image", datacsum);
1518         if (ret) {
1519                 error("failed to create %s/image: %d", subvol_name, ret);
1520                 goto fail;
1521         }
1522
1523         printf("creating btrfs metadata");
1524         ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
1525         ctx.cur_copy_inodes = 0;
1526
1527         if (progress) {
1528                 ctx.info = task_init(print_copied_inodes, after_copied_inodes,
1529                                      &ctx);
1530                 task_start(ctx.info);
1531         }
1532         ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
1533         if (ret) {
1534                 error("error during copy_inodes %d", ret);
1535                 goto fail;
1536         }
1537         if (progress) {
1538                 task_stop(ctx.info);
1539                 task_deinit(ctx.info);
1540         }
1541
1542         image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
1543         if (!image_root) {
1544                 error("unable to link subvolume %s", subvol_name);
1545                 goto fail;
1546         }
1547
1548         free(subvol_name);
1549
1550         memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
1551         if (copylabel == 1) {
1552                 __strncpy_null(root->fs_info->super_copy->label,
1553                                 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
1554                 printf("copy label '%s'\n", root->fs_info->super_copy->label);
1555         } else if (copylabel == -1) {
1556                 strcpy(root->fs_info->super_copy->label, fslabel);
1557                 printf("set label to '%s'\n", fslabel);
1558         }
1559
1560         ret = close_ctree(root);
1561         if (ret) {
1562                 error("close_ctree failed: %d", ret);
1563                 goto fail;
1564         }
1565         convert_close_fs(&cctx);
1566         clean_convert_context(&cctx);
1567
1568         /*
1569          * If this step succeed, we get a mountable btrfs. Otherwise
1570          * the source fs is left unchanged.
1571          */
1572         ret = migrate_super_block(fd, mkfs_cfg.super_bytenr);
1573         if (ret) {
1574                 error("unable to migrate super block: %d", ret);
1575                 goto fail;
1576         }
1577
1578         root = open_ctree_fd(fd, devname, 0,
1579                         OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1580         if (!root) {
1581                 error("unable to open ctree for finalization");
1582                 goto fail;
1583         }
1584         root->fs_info->finalize_on_close = 1;
1585         close_ctree(root);
1586         close(fd);
1587
1588         printf("conversion complete");
1589         return 0;
1590 fail:
1591         clean_convert_context(&cctx);
1592         if (fd != -1)
1593                 close(fd);
1594         warning(
1595 "an error occurred during conversion, filesystem is partially created but not finalized and not mountable");
1596         return -1;
1597 }
1598
1599 /*
1600  * Check if a non 1:1 mapped chunk can be rolled back.
1601  * For new convert, it's OK while for old convert it's not.
1602  */
1603 static int may_rollback_chunk(struct btrfs_fs_info *fs_info, u64 bytenr)
1604 {
1605         struct btrfs_block_group_cache *bg;
1606         struct btrfs_key key;
1607         struct btrfs_path path;
1608         struct btrfs_root *extent_root = fs_info->extent_root;
1609         u64 bg_start;
1610         u64 bg_end;
1611         int ret;
1612
1613         bg = btrfs_lookup_first_block_group(fs_info, bytenr);
1614         if (!bg)
1615                 return -ENOENT;
1616         bg_start = bg->key.objectid;
1617         bg_end = bg->key.objectid + bg->key.offset;
1618
1619         key.objectid = bg_end;
1620         key.type = BTRFS_METADATA_ITEM_KEY;
1621         key.offset = 0;
1622         btrfs_init_path(&path);
1623
1624         ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
1625         if (ret < 0)
1626                 return ret;
1627
1628         while (1) {
1629                 struct btrfs_extent_item *ei;
1630
1631                 ret = btrfs_previous_extent_item(extent_root, &path, bg_start);
1632                 if (ret > 0) {
1633                         ret = 0;
1634                         break;
1635                 }
1636                 if (ret < 0)
1637                         break;
1638
1639                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
1640                 if (key.type == BTRFS_METADATA_ITEM_KEY)
1641                         continue;
1642                 /* Now it's EXTENT_ITEM_KEY only */
1643                 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
1644                                     struct btrfs_extent_item);
1645                 /*
1646                  * Found data extent, means this is old convert must follow 1:1
1647                  * mapping.
1648                  */
1649                 if (btrfs_extent_flags(path.nodes[0], ei)
1650                                 & BTRFS_EXTENT_FLAG_DATA) {
1651                         ret = -EINVAL;
1652                         break;
1653                 }
1654         }
1655         btrfs_release_path(&path);
1656         return ret;
1657 }
1658
1659 static int may_rollback(struct btrfs_root *root)
1660 {
1661         struct btrfs_fs_info *info = root->fs_info;
1662         struct btrfs_multi_bio *multi = NULL;
1663         u64 bytenr;
1664         u64 length;
1665         u64 physical;
1666         u64 total_bytes;
1667         int num_stripes;
1668         int ret;
1669
1670         if (btrfs_super_num_devices(info->super_copy) != 1)
1671                 goto fail;
1672
1673         bytenr = BTRFS_SUPER_INFO_OFFSET;
1674         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1675
1676         while (1) {
1677                 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
1678                                       &length, &multi, 0, NULL);
1679                 if (ret) {
1680                         if (ret == -ENOENT) {
1681                                 /* removed block group at the tail */
1682                                 if (length == (u64)-1)
1683                                         break;
1684
1685                                 /* removed block group in the middle */
1686                                 goto next;
1687                         }
1688                         goto fail;
1689                 }
1690
1691                 num_stripes = multi->num_stripes;
1692                 physical = multi->stripes[0].physical;
1693                 free(multi);
1694
1695                 if (num_stripes != 1) {
1696                         error("num stripes for bytenr %llu is not 1", bytenr);
1697                         goto fail;
1698                 }
1699
1700                 /*
1701                  * Extra check for new convert, as metadata chunk from new
1702                  * convert is much more free than old convert, it doesn't need
1703                  * to do 1:1 mapping.
1704                  */
1705                 if (physical != bytenr) {
1706                         /*
1707                          * Check if it's a metadata chunk and has only metadata
1708                          * extent.
1709                          */
1710                         ret = may_rollback_chunk(info, bytenr);
1711                         if (ret < 0)
1712                                 goto fail;
1713                 }
1714 next:
1715                 bytenr += length;
1716                 if (bytenr >= total_bytes)
1717                         break;
1718         }
1719         return 0;
1720 fail:
1721         return -1;
1722 }
1723
1724 static int do_rollback(const char *devname)
1725 {
1726         int fd = -1;
1727         int ret;
1728         int i;
1729         struct btrfs_root *root;
1730         struct btrfs_root *image_root;
1731         struct btrfs_root *chunk_root;
1732         struct btrfs_dir_item *dir;
1733         struct btrfs_inode_item *inode;
1734         struct btrfs_file_extent_item *fi;
1735         struct btrfs_trans_handle *trans;
1736         struct extent_buffer *leaf;
1737         struct btrfs_block_group_cache *cache1;
1738         struct btrfs_block_group_cache *cache2;
1739         struct btrfs_key key;
1740         struct btrfs_path path;
1741         struct extent_io_tree io_tree;
1742         char *buf = NULL;
1743         char *name;
1744         u64 bytenr;
1745         u64 num_bytes;
1746         u64 root_dir;
1747         u64 objectid;
1748         u64 offset;
1749         u64 start;
1750         u64 end;
1751         u64 sb_bytenr;
1752         u64 first_free;
1753         u64 total_bytes;
1754         u32 sectorsize;
1755
1756         extent_io_tree_init(&io_tree);
1757
1758         fd = open(devname, O_RDWR);
1759         if (fd < 0) {
1760                 error("unable to open %s: %s", devname, strerror(errno));
1761                 goto fail;
1762         }
1763         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
1764         if (!root) {
1765                 error("unable to open ctree");
1766                 goto fail;
1767         }
1768         ret = may_rollback(root);
1769         if (ret < 0) {
1770                 error("unable to do rollback: %d", ret);
1771                 goto fail;
1772         }
1773
1774         sectorsize = root->sectorsize;
1775         buf = malloc(sectorsize);
1776         if (!buf) {
1777                 error("unable to allocate memory");
1778                 goto fail;
1779         }
1780
1781         btrfs_init_path(&path);
1782
1783         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1784         key.type = BTRFS_ROOT_BACKREF_KEY;
1785         key.offset = BTRFS_FS_TREE_OBJECTID;
1786         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path, 0,
1787                                 0);
1788         btrfs_release_path(&path);
1789         if (ret > 0) {
1790                 error("unable to convert ext2 image subvolume, is it deleted?");
1791                 goto fail;
1792         } else if (ret < 0) {
1793                 error("unable to open ext2_saved, id %llu: %s",
1794                         (unsigned long long)key.objectid, strerror(-ret));
1795                 goto fail;
1796         }
1797
1798         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1799         key.type = BTRFS_ROOT_ITEM_KEY;
1800         key.offset = (u64)-1;
1801         image_root = btrfs_read_fs_root(root->fs_info, &key);
1802         if (!image_root || IS_ERR(image_root)) {
1803                 error("unable to open subvolume %llu: %ld",
1804                         (unsigned long long)key.objectid, PTR_ERR(image_root));
1805                 goto fail;
1806         }
1807
1808         name = "image";
1809         root_dir = btrfs_root_dirid(&root->root_item);
1810         dir = btrfs_lookup_dir_item(NULL, image_root, &path,
1811                                    root_dir, name, strlen(name), 0);
1812         if (!dir || IS_ERR(dir)) {
1813                 error("unable to find file %s: %ld", name, PTR_ERR(dir));
1814                 goto fail;
1815         }
1816         leaf = path.nodes[0];
1817         btrfs_dir_item_key_to_cpu(leaf, dir, &key);
1818         btrfs_release_path(&path);
1819
1820         objectid = key.objectid;
1821
1822         ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
1823         if (ret) {
1824                 error("unable to find inode item: %d", ret);
1825                 goto fail;
1826         }
1827         leaf = path.nodes[0];
1828         inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
1829         total_bytes = btrfs_inode_size(leaf, inode);
1830         btrfs_release_path(&path);
1831
1832         key.objectid = objectid;
1833         key.offset = 0;
1834         key.type = BTRFS_EXTENT_DATA_KEY;
1835         ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
1836         if (ret != 0) {
1837                 error("unable to find first file extent: %d", ret);
1838                 btrfs_release_path(&path);
1839                 goto fail;
1840         }
1841
1842         /* build mapping tree for the relocated blocks */
1843         for (offset = 0; offset < total_bytes; ) {
1844                 leaf = path.nodes[0];
1845                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1846                         ret = btrfs_next_leaf(root, &path);
1847                         if (ret != 0)
1848                                 break;  
1849                         continue;
1850                 }
1851
1852                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1853                 if (key.objectid != objectid || key.offset != offset ||
1854                     key.type != BTRFS_EXTENT_DATA_KEY)
1855                         break;
1856
1857                 fi = btrfs_item_ptr(leaf, path.slots[0],
1858                                     struct btrfs_file_extent_item);
1859                 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1860                         break;
1861                 if (btrfs_file_extent_compression(leaf, fi) ||
1862                     btrfs_file_extent_encryption(leaf, fi) ||
1863                     btrfs_file_extent_other_encoding(leaf, fi))
1864                         break;
1865
1866                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1867                 /* skip holes and direct mapped extents */
1868                 if (bytenr == 0 || bytenr == offset)
1869                         goto next_extent;
1870
1871                 bytenr += btrfs_file_extent_offset(leaf, fi);
1872                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1873
1874                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
1875                 cache2 = btrfs_lookup_block_group(root->fs_info,
1876                                                   offset + num_bytes - 1);
1877                 /*
1878                  * Here we must take consideration of old and new convert
1879                  * behavior.
1880                  * For old convert case, sign, there is no consist chunk type
1881                  * that will cover the extent. META/DATA/SYS are all possible.
1882                  * Just ensure relocate one is in SYS chunk.
1883                  * For new convert case, they are all covered by DATA chunk.
1884                  *
1885                  * So, there is not valid chunk type check for it now.
1886                  */
1887                 if (cache1 != cache2)
1888                         break;
1889
1890                 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
1891                                 EXTENT_LOCKED, GFP_NOFS);
1892                 set_state_private(&io_tree, offset, bytenr);
1893 next_extent:
1894                 offset += btrfs_file_extent_num_bytes(leaf, fi);
1895                 path.slots[0]++;
1896         }
1897         btrfs_release_path(&path);
1898
1899         if (offset < total_bytes) {
1900                 error("unable to build extent mapping (offset %llu, total_bytes %llu)",
1901                                 (unsigned long long)offset,
1902                                 (unsigned long long)total_bytes);
1903                 error("converted filesystem after balance is unable to rollback");
1904                 goto fail;
1905         }
1906
1907         first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
1908         first_free &= ~((u64)sectorsize - 1);
1909         /* backup for extent #0 should exist */
1910         if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
1911                 error("no backup for the first extent");
1912                 goto fail;
1913         }
1914         /* force no allocation from system block group */
1915         root->fs_info->system_allocs = -1;
1916         trans = btrfs_start_transaction(root, 1);
1917         if (!trans) {
1918                 error("unable to start transaction");
1919                 goto fail;
1920         }
1921         /*
1922          * recow the whole chunk tree, this will remove all chunk tree blocks
1923          * from system block group
1924          */
1925         chunk_root = root->fs_info->chunk_root;
1926         memset(&key, 0, sizeof(key));
1927         while (1) {
1928                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
1929                 if (ret < 0)
1930                         break;
1931
1932                 ret = btrfs_next_leaf(chunk_root, &path);
1933                 if (ret)
1934                         break;
1935
1936                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
1937                 btrfs_release_path(&path);
1938         }
1939         btrfs_release_path(&path);
1940
1941         offset = 0;
1942         num_bytes = 0;
1943         while(1) {
1944                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
1945                 if (!cache1)
1946                         break;
1947
1948                 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
1949                         num_bytes += btrfs_block_group_used(&cache1->item);
1950
1951                 offset = cache1->key.objectid + cache1->key.offset;
1952         }
1953         /* only extent #0 left in system block group? */
1954         if (num_bytes > first_free) {
1955                 error(
1956         "unable to empty system block group (num_bytes %llu, first_free %llu",
1957                                 (unsigned long long)num_bytes,
1958                                 (unsigned long long)first_free);
1959                 goto fail;
1960         }
1961         /* create a system chunk that maps the whole device */
1962         ret = prepare_system_chunk_sb(root->fs_info->super_copy);
1963         if (ret) {
1964                 error("unable to update system chunk: %d", ret);
1965                 goto fail;
1966         }
1967
1968         ret = btrfs_commit_transaction(trans, root);
1969         if (ret) {
1970                 error("transaction commit failed: %d", ret);
1971                 goto fail;
1972         }
1973
1974         ret = close_ctree(root);
1975         if (ret) {
1976                 error("close_ctree failed: %d", ret);
1977                 goto fail;
1978         }
1979
1980         /* zero btrfs super block mirrors */
1981         memset(buf, 0, sectorsize);
1982         for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1983                 bytenr = btrfs_sb_offset(i);
1984                 if (bytenr >= total_bytes)
1985                         break;
1986                 ret = pwrite(fd, buf, sectorsize, bytenr);
1987                 if (ret != sectorsize) {
1988                         error("zeroing superblock mirror %d failed: %d",
1989                                         i, ret);
1990                         goto fail;
1991                 }
1992         }
1993
1994         sb_bytenr = (u64)-1;
1995         /* copy all relocated blocks back */
1996         while(1) {
1997                 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
1998                                             EXTENT_LOCKED);
1999                 if (ret)
2000                         break;
2001
2002                 ret = get_state_private(&io_tree, start, &bytenr);
2003                 BUG_ON(ret);
2004
2005                 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2006                                   GFP_NOFS);
2007
2008                 while (start <= end) {
2009                         if (start == BTRFS_SUPER_INFO_OFFSET) {
2010                                 sb_bytenr = bytenr;
2011                                 goto next_sector;
2012                         }
2013                         ret = pread(fd, buf, sectorsize, bytenr);
2014                         if (ret < 0) {
2015                                 error("reading superblock at %llu failed: %d",
2016                                                 (unsigned long long)bytenr, ret);
2017                                 goto fail;
2018                         }
2019                         BUG_ON(ret != sectorsize);
2020                         ret = pwrite(fd, buf, sectorsize, start);
2021                         if (ret < 0) {
2022                                 error("writing superblock at %llu failed: %d",
2023                                                 (unsigned long long)start, ret);
2024                                 goto fail;
2025                         }
2026                         BUG_ON(ret != sectorsize);
2027 next_sector:
2028                         start += sectorsize;
2029                         bytenr += sectorsize;
2030                 }
2031         }
2032
2033         ret = fsync(fd);
2034         if (ret < 0) {
2035                 error("fsync failed: %s", strerror(errno));
2036                 goto fail;
2037         }
2038         /*
2039          * finally, overwrite btrfs super block.
2040          */
2041         ret = pread(fd, buf, sectorsize, sb_bytenr);
2042         if (ret < 0) {
2043                 error("reading primary superblock failed: %s",
2044                                 strerror(errno));
2045                 goto fail;
2046         }
2047         BUG_ON(ret != sectorsize);
2048         ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2049         if (ret < 0) {
2050                 error("writing primary superblock failed: %s",
2051                                 strerror(errno));
2052                 goto fail;
2053         }
2054         BUG_ON(ret != sectorsize);
2055         ret = fsync(fd);
2056         if (ret < 0) {
2057                 error("fsync failed: %s", strerror(errno));
2058                 goto fail;
2059         }
2060
2061         close(fd);
2062         free(buf);
2063         extent_io_tree_cleanup(&io_tree);
2064         printf("rollback complete\n");
2065         return 0;
2066
2067 fail:
2068         if (fd != -1)
2069                 close(fd);
2070         free(buf);
2071         error("rollback aborted");
2072         return -1;
2073 }
2074
2075 static void print_usage(void)
2076 {
2077         printf("usage: btrfs-convert [options] device\n");
2078         printf("options:\n");
2079         printf("\t-d|--no-datasum        disable data checksum, sets NODATASUM\n");
2080         printf("\t-i|--no-xattr          ignore xattrs and ACLs\n");
2081         printf("\t-n|--no-inline         disable inlining of small files to metadata\n");
2082         printf("\t-N|--nodesize SIZE     set filesystem metadata nodesize\n");
2083         printf("\t-r|--rollback          roll back to the original filesystem\n");
2084         printf("\t-l|--label LABEL       set filesystem label\n");
2085         printf("\t-L|--copy-label        use label from converted filesystem\n");
2086         printf("\t-p|--progress          show converting progress (default)\n");
2087         printf("\t-O|--features LIST     comma separated list of filesystem features\n");
2088         printf("\t--no-progress          show only overview, not the detailed progress\n");
2089         printf("\n");
2090         printf("Supported filesystems:\n");
2091         printf("\text2/3/4: %s\n", BTRFSCONVERT_EXT2 ? "yes" : "no");
2092 }
2093
2094 int main(int argc, char *argv[])
2095 {
2096         int ret;
2097         int packing = 1;
2098         int noxattr = 0;
2099         int datacsum = 1;
2100         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
2101                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
2102         int rollback = 0;
2103         int copylabel = 0;
2104         int usage_error = 0;
2105         int progress = 1;
2106         char *file;
2107         char fslabel[BTRFS_LABEL_SIZE];
2108         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
2109
2110         while(1) {
2111                 enum { GETOPT_VAL_NO_PROGRESS = 256 };
2112                 static const struct option long_options[] = {
2113                         { "no-progress", no_argument, NULL,
2114                                 GETOPT_VAL_NO_PROGRESS },
2115                         { "no-datasum", no_argument, NULL, 'd' },
2116                         { "no-inline", no_argument, NULL, 'n' },
2117                         { "no-xattr", no_argument, NULL, 'i' },
2118                         { "rollback", no_argument, NULL, 'r' },
2119                         { "features", required_argument, NULL, 'O' },
2120                         { "progress", no_argument, NULL, 'p' },
2121                         { "label", required_argument, NULL, 'l' },
2122                         { "copy-label", no_argument, NULL, 'L' },
2123                         { "nodesize", required_argument, NULL, 'N' },
2124                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
2125                         { NULL, 0, NULL, 0 }
2126                 };
2127                 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
2128
2129                 if (c < 0)
2130                         break;
2131                 switch(c) {
2132                         case 'd':
2133                                 datacsum = 0;
2134                                 break;
2135                         case 'i':
2136                                 noxattr = 1;
2137                                 break;
2138                         case 'n':
2139                                 packing = 0;
2140                                 break;
2141                         case 'N':
2142                                 nodesize = parse_size(optarg);
2143                                 break;
2144                         case 'r':
2145                                 rollback = 1;
2146                                 break;
2147                         case 'l':
2148                                 copylabel = -1;
2149                                 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
2150                                         warning(
2151                                         "label too long, trimmed to %d bytes",
2152                                                 BTRFS_LABEL_SIZE - 1);
2153                                 }
2154                                 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
2155                                 break;
2156                         case 'L':
2157                                 copylabel = 1;
2158                                 break;
2159                         case 'p':
2160                                 progress = 1;
2161                                 break;
2162                         case 'O': {
2163                                 char *orig = strdup(optarg);
2164                                 char *tmp = orig;
2165
2166                                 tmp = btrfs_parse_fs_features(tmp, &features);
2167                                 if (tmp) {
2168                                         error("unrecognized filesystem feature: %s",
2169                                                         tmp);
2170                                         free(orig);
2171                                         exit(1);
2172                                 }
2173                                 free(orig);
2174                                 if (features & BTRFS_FEATURE_LIST_ALL) {
2175                                         btrfs_list_all_fs_features(
2176                                                 ~BTRFS_CONVERT_ALLOWED_FEATURES);
2177                                         exit(0);
2178                                 }
2179                                 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
2180                                         char buf[64];
2181
2182                                         btrfs_parse_features_to_string(buf,
2183                                                 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
2184                                         error("features not allowed for convert: %s",
2185                                                 buf);
2186                                         exit(1);
2187                                 }
2188
2189                                 break;
2190                                 }
2191                         case GETOPT_VAL_NO_PROGRESS:
2192                                 progress = 0;
2193                                 break;
2194                         case GETOPT_VAL_HELP:
2195                         default:
2196                                 print_usage();
2197                                 return c != GETOPT_VAL_HELP;
2198                 }
2199         }
2200         set_argv0(argv);
2201         if (check_argc_exact(argc - optind, 1)) {
2202                 print_usage();
2203                 return 1;
2204         }
2205
2206         if (rollback && (!datacsum || noxattr || !packing)) {
2207                 fprintf(stderr,
2208                         "Usage error: -d, -i, -n options do not apply to rollback\n");
2209                 usage_error++;
2210         }
2211
2212         if (usage_error) {
2213                 print_usage();
2214                 return 1;
2215         }
2216
2217         file = argv[optind];
2218         ret = check_mounted(file);
2219         if (ret < 0) {
2220                 error("could not check mount status: %s", strerror(-ret));
2221                 return 1;
2222         } else if (ret) {
2223                 error("%s is mounted", file);
2224                 return 1;
2225         }
2226
2227         if (rollback) {
2228                 ret = do_rollback(file);
2229         } else {
2230                 ret = do_convert(file, datacsum, packing, noxattr, nodesize,
2231                                 copylabel, fslabel, progress, features);
2232         }
2233         if (ret)
2234                 return 1;
2235         return 0;
2236 }