btrfs-progs: Rename OPEN_CTREE_FS_PARTIAL to OPEN_CTREE_TEMPORARY_SUPER
[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 /*
20  * Btrfs convert design:
21  *
22  * The overall design of btrfs convert is like the following:
23  *
24  * |<------------------Old fs----------------------------->|
25  * |<- used ->| |<- used ->|                    |<- used ->|
26  *                           ||
27  *                           \/
28  * |<---------------Btrfs fs------------------------------>|
29  * |<-   Old data chunk  ->|< new chunk (D/M/S)>|<- ODC  ->|
30  * |<-Old-FE->| |<-Old-FE->|<- Btrfs extents  ->|<-Old-FE->|
31  *
32  * ODC    = Old data chunk, btrfs chunks containing old fs data
33  *          Mapped 1:1 (logical address == device offset)
34  * Old-FE = file extents pointing to old fs.
35  *
36  * So old fs used space is (mostly) kept as is, while btrfs will insert
37  * its chunk (Data/Meta/Sys) into large enough free space.
38  * In this way, we can create different profiles for metadata/data for
39  * converted fs.
40  *
41  * We must reserve and relocate 3 ranges for btrfs:
42  * * [0, 1M)                    - area never used for any data except the first
43  *                                superblock
44  * * [btrfs_sb_offset(1), +64K) - 1st superblock backup copy
45  * * [btrfs_sb_offset(2), +64K) - 2nd, dtto
46  *
47  * Most work is spent handling corner cases around these reserved ranges.
48  *
49  * Detailed workflow is:
50  * 1)   Scan old fs used space and calculate data chunk layout
51  * 1.1) Scan old fs
52  *      We can a map used space of old fs
53  *
54  * 1.2) Calculate data chunk layout - this is the hard part
55  *      New data chunks must meet 3 conditions using result fomr 1.1
56  *      a. Large enough to be a chunk
57  *      b. Doesn't intersect reserved ranges
58  *      c. Covers all the remaining old fs used space
59  *
60  *      NOTE: This can be simplified if we don't need to handle backup supers
61  *
62  * 1.3) Calculate usable space for new btrfs chunks
63  *      Btrfs chunk usable space must meet 3 conditions using result from 1.2
64  *      a. Large enough to be a chunk
65  *      b. Doesn't intersect reserved ranges
66  *      c. Doesn't cover any data chunks in 1.1
67  *
68  * 2)   Create basic btrfs filesystem structure
69  *      Initial metadata and sys chunks are inserted in the first availabe
70  *      space found in step 1.3
71  *      Then insert all data chunks into the basic btrfs
72  *
73  * 3)   Create convert image
74  *      We need to relocate reserved ranges here.
75  *      After this step, the convert image is done, and we can use the image
76  *      as reflink source to create old files
77  *
78  * 4)   Iterate old fs to create files
79  *      We just reflink file extents from old fs to newly created files on
80  *      btrfs.
81  */
82
83 #include "kerncompat.h"
84
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <sys/types.h>
88 #include <fcntl.h>
89 #include <unistd.h>
90 #include <getopt.h>
91 #include <pthread.h>
92 #include <stdbool.h>
93
94 #include "ctree.h"
95 #include "disk-io.h"
96 #include "volumes.h"
97 #include "transaction.h"
98 #include "utils.h"
99 #include "task-utils.h"
100 #include "help.h"
101 #include "mkfs/common.h"
102 #include "convert/common.h"
103 #include "convert/source-fs.h"
104 #include "fsfeatures.h"
105
106 extern const struct btrfs_convert_operations ext2_convert_ops;
107 extern const struct btrfs_convert_operations reiserfs_convert_ops;
108
109 static const struct btrfs_convert_operations *convert_operations[] = {
110 #if BTRFSCONVERT_EXT2
111         &ext2_convert_ops,
112 #endif
113 #if BTRFSCONVERT_REISERFS
114         &reiserfs_convert_ops,
115 #endif
116 };
117
118 static void *print_copied_inodes(void *p)
119 {
120         struct task_ctx *priv = p;
121         const char work_indicator[] = { '.', 'o', 'O', 'o' };
122         u64 count = 0;
123
124         task_period_start(priv->info, 1000 /* 1s */);
125         while (1) {
126                 count++;
127                 pthread_mutex_lock(&priv->mutex);
128                 printf("copy inodes [%c] [%10llu/%10llu]\r",
129                        work_indicator[count % 4],
130                        (unsigned long long)priv->cur_copy_inodes,
131                        (unsigned long long)priv->max_copy_inodes);
132                 pthread_mutex_unlock(&priv->mutex);
133                 fflush(stdout);
134                 task_period_wait(priv->info);
135         }
136
137         return NULL;
138 }
139
140 static int after_copied_inodes(void *p)
141 {
142         printf("\n");
143         fflush(stdout);
144
145         return 0;
146 }
147
148 static inline int copy_inodes(struct btrfs_convert_context *cctx,
149                               struct btrfs_root *root, u32 convert_flags,
150                               struct task_ctx *p)
151 {
152         return cctx->convert_ops->copy_inodes(cctx, root, convert_flags, p);
153 }
154
155 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
156 {
157         cctx->convert_ops->close_fs(cctx);
158 }
159
160 static inline int convert_check_state(struct btrfs_convert_context *cctx)
161 {
162         return cctx->convert_ops->check_state(cctx);
163 }
164
165 static int csum_disk_extent(struct btrfs_trans_handle *trans,
166                             struct btrfs_root *root,
167                             u64 disk_bytenr, u64 num_bytes)
168 {
169         u32 blocksize = root->fs_info->sectorsize;
170         u64 offset;
171         char *buffer;
172         int ret = 0;
173
174         buffer = malloc(blocksize);
175         if (!buffer)
176                 return -ENOMEM;
177         for (offset = 0; offset < num_bytes; offset += blocksize) {
178                 ret = read_disk_extent(root, disk_bytenr + offset,
179                                         blocksize, buffer);
180                 if (ret)
181                         break;
182                 ret = btrfs_csum_file_block(trans,
183                                             root->fs_info->csum_root,
184                                             disk_bytenr + num_bytes,
185                                             disk_bytenr + offset,
186                                             buffer, blocksize);
187                 if (ret)
188                         break;
189         }
190         free(buffer);
191         return ret;
192 }
193
194 static int create_image_file_range(struct btrfs_trans_handle *trans,
195                                       struct btrfs_root *root,
196                                       struct cache_tree *used,
197                                       struct btrfs_inode_item *inode,
198                                       u64 ino, u64 bytenr, u64 *ret_len,
199                                       u32 convert_flags)
200 {
201         struct cache_extent *cache;
202         struct btrfs_block_group_cache *bg_cache;
203         u64 len = *ret_len;
204         u64 disk_bytenr;
205         int i;
206         int ret;
207         u32 datacsum = convert_flags & CONVERT_FLAG_DATACSUM;
208
209         if (bytenr != round_down(bytenr, root->fs_info->sectorsize)) {
210                 error("bytenr not sectorsize aligned: %llu",
211                                 (unsigned long long)bytenr);
212                 return -EINVAL;
213         }
214         if (len != round_down(len, root->fs_info->sectorsize)) {
215                 error("length not sectorsize aligned: %llu",
216                                 (unsigned long long)len);
217                 return -EINVAL;
218         }
219         len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE);
220
221         /*
222          * Skip reserved ranges first
223          *
224          * Or we will insert a hole into current image file, and later
225          * migrate block will fail as there is already a file extent.
226          */
227         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
228                 const struct simple_range *reserved = &btrfs_reserved_ranges[i];
229
230                 /*
231                  * |-- reserved --|
232                  *         |--range---|
233                  * or
234                  * |---- reserved ----|
235                  *    |-- range --|
236                  * Skip to reserved range end
237                  */
238                 if (bytenr >= reserved->start && bytenr < range_end(reserved)) {
239                         *ret_len = range_end(reserved) - bytenr;
240                         return 0;
241                 }
242
243                 /*
244                  *      |---reserved---|
245                  * |----range-------|
246                  * Leading part may still create a file extent
247                  */
248                 if (bytenr < reserved->start &&
249                     bytenr + len >= range_end(reserved)) {
250                         len = min_t(u64, len, reserved->start - bytenr);
251                         break;
252                 }
253         }
254
255         /* Check if we are going to insert regular file extent, or hole */
256         cache = search_cache_extent(used, bytenr);
257         if (cache) {
258                 if (cache->start <= bytenr) {
259                         /*
260                          * |///////Used///////|
261                          *      |<--insert--->|
262                          *      bytenr
263                          * Insert one real file extent
264                          */
265                         len = min_t(u64, len, cache->start + cache->size -
266                                     bytenr);
267                         disk_bytenr = bytenr;
268                 } else {
269                         /*
270                          *              |//Used//|
271                          *  |<-insert-->|
272                          *  bytenr
273                          *  Insert one hole
274                          */
275                         len = min(len, cache->start - bytenr);
276                         disk_bytenr = 0;
277                         datacsum = 0;
278                 }
279         } else {
280                 /*
281                  * |//Used//|           |EOF
282                  *          |<-insert-->|
283                  *          bytenr
284                  * Insert one hole
285                  */
286                 disk_bytenr = 0;
287                 datacsum = 0;
288         }
289
290         if (disk_bytenr) {
291                 /* Check if the range is in a data block group */
292                 bg_cache = btrfs_lookup_block_group(root->fs_info, bytenr);
293                 if (!bg_cache)
294                         return -ENOENT;
295                 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
296                         return -EINVAL;
297
298                 /* The extent should never cross block group boundary */
299                 len = min_t(u64, len, bg_cache->key.objectid +
300                             bg_cache->key.offset - bytenr);
301         }
302
303         if (len != round_down(len, root->fs_info->sectorsize)) {
304                 error("remaining length not sectorsize aligned: %llu",
305                                 (unsigned long long)len);
306                 return -EINVAL;
307         }
308         ret = btrfs_record_file_extent(trans, root, ino, inode, bytenr,
309                                        disk_bytenr, len);
310         if (ret < 0)
311                 return ret;
312
313         if (datacsum)
314                 ret = csum_disk_extent(trans, root, bytenr, len);
315         *ret_len = len;
316         return ret;
317 }
318
319 /*
320  * Relocate old fs data in one reserved ranges
321  *
322  * Since all old fs data in reserved range is not covered by any chunk nor
323  * data extent, we don't need to handle any reference but add new
324  * extent/reference, which makes codes more clear
325  */
326 static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
327                                       struct btrfs_root *root,
328                                       struct cache_tree *used,
329                                       struct btrfs_inode_item *inode, int fd,
330                                       u64 ino, const struct simple_range *range,
331                                       u32 convert_flags)
332 {
333         u64 cur_off = range->start;
334         u64 cur_len = range->len;
335         u64 hole_start = range->start;
336         u64 hole_len;
337         struct cache_extent *cache;
338         struct btrfs_key key;
339         struct extent_buffer *eb;
340         int ret = 0;
341
342         /*
343          * It's possible that there are holes in reserved range:
344          * |<---------------- Reserved range ---------------------->|
345          *      |<- Old fs data ->|   |<- Old fs data ->|
346          * So here we need to iterate through old fs used space and only
347          * migrate ranges that covered by old fs data.
348          */
349         while (cur_off < range_end(range)) {
350                 cache = search_cache_extent(used, cur_off);
351                 if (!cache)
352                         break;
353                 cur_off = max(cache->start, cur_off);
354                 if (cur_off >= range_end(range))
355                         break;
356                 cur_len = min(cache->start + cache->size, range_end(range)) -
357                           cur_off;
358                 BUG_ON(cur_len < root->fs_info->sectorsize);
359
360                 /* reserve extent for the data */
361                 ret = btrfs_reserve_extent(trans, root, cur_len, 0, 0, (u64)-1,
362                                            &key, 1);
363                 if (ret < 0)
364                         break;
365
366                 eb = malloc(sizeof(*eb) + cur_len);
367                 if (!eb) {
368                         ret = -ENOMEM;
369                         break;
370                 }
371
372                 ret = pread(fd, eb->data, cur_len, cur_off);
373                 if (ret < cur_len) {
374                         ret = (ret < 0 ? ret : -EIO);
375                         free(eb);
376                         break;
377                 }
378                 eb->start = key.objectid;
379                 eb->len = key.offset;
380
381                 /* Write the data */
382                 ret = write_and_map_eb(root->fs_info, eb);
383                 free(eb);
384                 if (ret < 0)
385                         break;
386
387                 /* Now handle extent item and file extent things */
388                 ret = btrfs_record_file_extent(trans, root, ino, inode, cur_off,
389                                                key.objectid, key.offset);
390                 if (ret < 0)
391                         break;
392                 /* Finally, insert csum items */
393                 if (convert_flags & CONVERT_FLAG_DATACSUM)
394                         ret = csum_disk_extent(trans, root, key.objectid,
395                                                key.offset);
396
397                 /* Don't forget to insert hole */
398                 hole_len = cur_off - hole_start;
399                 if (hole_len) {
400                         ret = btrfs_record_file_extent(trans, root, ino, inode,
401                                         hole_start, 0, hole_len);
402                         if (ret < 0)
403                                 break;
404                 }
405
406                 cur_off += key.offset;
407                 hole_start = cur_off;
408                 cur_len = range_end(range) - cur_off;
409         }
410         /*
411          * Last hole
412          * |<---- reserved -------->|
413          * |<- Old fs data ->|      |
414          *                   | Hole |
415          */
416         if (range_end(range) - hole_start > 0)
417                 ret = btrfs_record_file_extent(trans, root, ino, inode,
418                                 hole_start, 0, range_end(range) - hole_start);
419         return ret;
420 }
421
422 /*
423  * Relocate the used source fs data in reserved ranges
424  */
425 static int migrate_reserved_ranges(struct btrfs_trans_handle *trans,
426                                    struct btrfs_root *root,
427                                    struct cache_tree *used,
428                                    struct btrfs_inode_item *inode, int fd,
429                                    u64 ino, u64 total_bytes, u32 convert_flags)
430 {
431         int i;
432         int ret = 0;
433
434         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
435                 const struct simple_range *range = &btrfs_reserved_ranges[i];
436
437                 if (range->start > total_bytes)
438                         return ret;
439                 ret = migrate_one_reserved_range(trans, root, used, inode, fd,
440                                                  ino, range, convert_flags);
441                 if (ret < 0)
442                         return ret;
443         }
444
445         return ret;
446 }
447
448 /*
449  * Helper for expand and merge extent_cache for wipe_one_reserved_range() to
450  * handle wiping a range that exists in cache.
451  */
452 static int _expand_extent_cache(struct cache_tree *tree,
453                                 struct cache_extent *entry,
454                                 u64 min_stripe_size, int backward)
455 {
456         struct cache_extent *ce;
457         int diff;
458
459         if (entry->size >= min_stripe_size)
460                 return 0;
461         diff = min_stripe_size - entry->size;
462
463         if (backward) {
464                 ce = prev_cache_extent(entry);
465                 if (!ce)
466                         goto expand_back;
467                 if (ce->start + ce->size >= entry->start - diff) {
468                         /* Directly merge with previous extent */
469                         ce->size = entry->start + entry->size - ce->start;
470                         remove_cache_extent(tree, entry);
471                         free(entry);
472                         return 0;
473                 }
474 expand_back:
475                 /* No overlap, normal extent */
476                 if (entry->start < diff) {
477                         error("cannot find space for data chunk layout");
478                         return -ENOSPC;
479                 }
480                 entry->start -= diff;
481                 entry->size += diff;
482                 return 0;
483         }
484         ce = next_cache_extent(entry);
485         if (!ce)
486                 goto expand_after;
487         if (entry->start + entry->size + diff >= ce->start) {
488                 /* Directly merge with next extent */
489                 entry->size = ce->start + ce->size - entry->start;
490                 remove_cache_extent(tree, ce);
491                 free(ce);
492                 return 0;
493         }
494 expand_after:
495         entry->size += diff;
496         return 0;
497 }
498
499 /*
500  * Remove one reserve range from given cache tree
501  * if min_stripe_size is non-zero, it will ensure for split case,
502  * all its split cache extent is no smaller than @min_strip_size / 2.
503  */
504 static int wipe_one_reserved_range(struct cache_tree *tree,
505                                    u64 start, u64 len, u64 min_stripe_size,
506                                    int ensure_size)
507 {
508         struct cache_extent *cache;
509         int ret;
510
511         BUG_ON(ensure_size && min_stripe_size == 0);
512         /*
513          * The logical here is simplified to handle special cases only
514          * So we don't need to consider merge case for ensure_size
515          */
516         BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
517                min_stripe_size / 2 < BTRFS_STRIPE_LEN));
518
519         /* Also, wipe range should already be aligned */
520         BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
521                start + len != round_up(start + len, BTRFS_STRIPE_LEN));
522
523         min_stripe_size /= 2;
524
525         cache = lookup_cache_extent(tree, start, len);
526         if (!cache)
527                 return 0;
528
529         if (start <= cache->start) {
530                 /*
531                  *      |--------cache---------|
532                  * |-wipe-|
533                  */
534                 BUG_ON(start + len <= cache->start);
535
536                 /*
537                  * The wipe size is smaller than min_stripe_size / 2,
538                  * so the result length should still meet min_stripe_size
539                  * And no need to do alignment
540                  */
541                 cache->size -= (start + len - cache->start);
542                 if (cache->size == 0) {
543                         remove_cache_extent(tree, cache);
544                         free(cache);
545                         return 0;
546                 }
547
548                 BUG_ON(ensure_size && cache->size < min_stripe_size);
549
550                 cache->start = start + len;
551                 return 0;
552         } else if (start > cache->start && start + len < cache->start +
553                    cache->size) {
554                 /*
555                  * |-------cache-----|
556                  *      |-wipe-|
557                  */
558                 u64 old_start = cache->start;
559                 u64 old_len = cache->size;
560                 u64 insert_start = start + len;
561                 u64 insert_len;
562
563                 cache->size = start - cache->start;
564                 /* Expand the leading half part if needed */
565                 if (ensure_size && cache->size < min_stripe_size) {
566                         ret = _expand_extent_cache(tree, cache,
567                                         min_stripe_size, 1);
568                         if (ret < 0)
569                                 return ret;
570                 }
571
572                 /* And insert the new one */
573                 insert_len = old_start + old_len - start - len;
574                 ret = add_merge_cache_extent(tree, insert_start, insert_len);
575                 if (ret < 0)
576                         return ret;
577
578                 /* Expand the last half part if needed */
579                 if (ensure_size && insert_len < min_stripe_size) {
580                         cache = lookup_cache_extent(tree, insert_start,
581                                                     insert_len);
582                         if (!cache || cache->start != insert_start ||
583                             cache->size != insert_len)
584                                 return -ENOENT;
585                         ret = _expand_extent_cache(tree, cache,
586                                         min_stripe_size, 0);
587                 }
588
589                 return ret;
590         }
591         /*
592          * |----cache-----|
593          *              |--wipe-|
594          * Wipe len should be small enough and no need to expand the
595          * remaining extent
596          */
597         cache->size = start - cache->start;
598         BUG_ON(ensure_size && cache->size < min_stripe_size);
599         return 0;
600 }
601
602 /*
603  * Remove reserved ranges from given cache_tree
604  *
605  * It will remove the following ranges
606  * 1) 0~1M
607  * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
608  * 3) 3rd superblock, +64K
609  *
610  * @min_stripe must be given for safety check
611  * and if @ensure_size is given, it will ensure affected cache_extent will be
612  * larger than min_stripe_size
613  */
614 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
615                                 int ensure_size)
616 {
617         int i;
618         int ret;
619
620         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
621                 const struct simple_range *range = &btrfs_reserved_ranges[i];
622
623                 ret = wipe_one_reserved_range(tree, range->start, range->len,
624                                               min_stripe_size, ensure_size);
625                 if (ret < 0)
626                         return ret;
627         }
628         return ret;
629 }
630
631 static int calculate_available_space(struct btrfs_convert_context *cctx)
632 {
633         struct cache_tree *used = &cctx->used_space;
634         struct cache_tree *data_chunks = &cctx->data_chunks;
635         struct cache_tree *free = &cctx->free_space;
636         struct cache_extent *cache;
637         u64 cur_off = 0;
638         /*
639          * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
640          * works without need to consider overlap
641          */
642         u64 min_stripe_size = SZ_32M;
643         int ret;
644
645         /* Calculate data_chunks */
646         for (cache = first_cache_extent(used); cache;
647              cache = next_cache_extent(cache)) {
648                 u64 cur_len;
649
650                 if (cache->start + cache->size < cur_off)
651                         continue;
652                 if (cache->start > cur_off + min_stripe_size)
653                         cur_off = cache->start;
654                 cur_len = max(cache->start + cache->size - cur_off,
655                               min_stripe_size);
656                 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
657                 if (ret < 0)
658                         goto out;
659                 cur_off += cur_len;
660         }
661         /*
662          * remove reserved ranges, so we won't ever bother relocating an old
663          * filesystem extent to other place.
664          */
665         ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
666         if (ret < 0)
667                 goto out;
668
669         cur_off = 0;
670         /*
671          * Calculate free space
672          * Always round up the start bytenr, to avoid metadata extent corss
673          * stripe boundary, as later mkfs_convert() won't have all the extent
674          * allocation check
675          */
676         for (cache = first_cache_extent(data_chunks); cache;
677              cache = next_cache_extent(cache)) {
678                 if (cache->start < cur_off)
679                         continue;
680                 if (cache->start > cur_off) {
681                         u64 insert_start;
682                         u64 len;
683
684                         len = cache->start - round_up(cur_off,
685                                                       BTRFS_STRIPE_LEN);
686                         insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
687
688                         ret = add_merge_cache_extent(free, insert_start, len);
689                         if (ret < 0)
690                                 goto out;
691                 }
692                 cur_off = cache->start + cache->size;
693         }
694         /* Don't forget the last range */
695         if (cctx->total_bytes > cur_off) {
696                 u64 len = cctx->total_bytes - cur_off;
697                 u64 insert_start;
698
699                 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
700
701                 ret = add_merge_cache_extent(free, insert_start, len);
702                 if (ret < 0)
703                         goto out;
704         }
705
706         /* Remove reserved bytes */
707         ret = wipe_reserved_ranges(free, min_stripe_size, 0);
708 out:
709         return ret;
710 }
711
712 /*
713  * Read used space, and since we have the used space,
714  * calcuate data_chunks and free for later mkfs
715  */
716 static int convert_read_used_space(struct btrfs_convert_context *cctx)
717 {
718         int ret;
719
720         ret = cctx->convert_ops->read_used_space(cctx);
721         if (ret)
722                 return ret;
723
724         ret = calculate_available_space(cctx);
725         return ret;
726 }
727
728 /*
729  * Create the fs image file of old filesystem.
730  *
731  * This is completely fs independent as we have cctx->used, only
732  * need to create file extents pointing to all the positions.
733  */
734 static int create_image(struct btrfs_root *root,
735                            struct btrfs_mkfs_config *cfg,
736                            struct btrfs_convert_context *cctx, int fd,
737                            u64 size, char *name, u32 convert_flags)
738 {
739         struct btrfs_inode_item buf;
740         struct btrfs_trans_handle *trans;
741         struct btrfs_path path;
742         struct btrfs_key key;
743         struct cache_extent *cache;
744         struct cache_tree used_tmp;
745         u64 cur;
746         u64 ino;
747         u64 flags = BTRFS_INODE_READONLY;
748         int ret;
749
750         if (!(convert_flags & CONVERT_FLAG_DATACSUM))
751                 flags |= BTRFS_INODE_NODATASUM;
752
753         trans = btrfs_start_transaction(root, 1);
754         if (IS_ERR(trans))
755                 return PTR_ERR(trans);
756
757         cache_tree_init(&used_tmp);
758         btrfs_init_path(&path);
759
760         ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
761                                        &ino);
762         if (ret < 0)
763                 goto out;
764         ret = btrfs_new_inode(trans, root, ino, 0400 | S_IFREG);
765         if (ret < 0)
766                 goto out;
767         ret = btrfs_change_inode_flags(trans, root, ino, flags);
768         if (ret < 0)
769                 goto out;
770         ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
771                              strlen(name), BTRFS_FT_REG_FILE, NULL, 1, 0);
772         if (ret < 0)
773                 goto out;
774
775         key.objectid = ino;
776         key.type = BTRFS_INODE_ITEM_KEY;
777         key.offset = 0;
778
779         ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
780         if (ret) {
781                 ret = (ret > 0 ? -ENOENT : ret);
782                 goto out;
783         }
784         read_extent_buffer(path.nodes[0], &buf,
785                         btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
786                         sizeof(buf));
787         btrfs_release_path(&path);
788
789         /*
790          * Create a new used space cache, which doesn't contain the reserved
791          * range
792          */
793         for (cache = first_cache_extent(&cctx->used_space); cache;
794              cache = next_cache_extent(cache)) {
795                 ret = add_cache_extent(&used_tmp, cache->start, cache->size);
796                 if (ret < 0)
797                         goto out;
798         }
799         ret = wipe_reserved_ranges(&used_tmp, 0, 0);
800         if (ret < 0)
801                 goto out;
802
803         /*
804          * Start from 1M, as 0~1M is reserved, and create_image_file_range()
805          * can't handle bytenr 0(will consider it as a hole)
806          */
807         cur = SZ_1M;
808         while (cur < size) {
809                 u64 len = size - cur;
810
811                 ret = create_image_file_range(trans, root, &used_tmp,
812                                                 &buf, ino, cur, &len,
813                                                 convert_flags);
814                 if (ret < 0)
815                         goto out;
816                 cur += len;
817         }
818         /* Handle the reserved ranges */
819         ret = migrate_reserved_ranges(trans, root, &cctx->used_space, &buf, fd,
820                         ino, cfg->num_bytes, convert_flags);
821
822         key.objectid = ino;
823         key.type = BTRFS_INODE_ITEM_KEY;
824         key.offset = 0;
825         ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
826         if (ret) {
827                 ret = (ret > 0 ? -ENOENT : ret);
828                 goto out;
829         }
830         btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
831         write_extent_buffer(path.nodes[0], &buf,
832                         btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
833                         sizeof(buf));
834 out:
835         free_extent_cache_tree(&used_tmp);
836         btrfs_release_path(&path);
837         btrfs_commit_transaction(trans, root);
838         return ret;
839 }
840
841 static int create_subvol(struct btrfs_trans_handle *trans,
842                          struct btrfs_root *root, u64 root_objectid)
843 {
844         struct extent_buffer *tmp;
845         struct btrfs_root *new_root;
846         struct btrfs_key key;
847         struct btrfs_root_item root_item;
848         int ret;
849
850         ret = btrfs_copy_root(trans, root, root->node, &tmp,
851                               root_objectid);
852         if (ret)
853                 return ret;
854
855         memcpy(&root_item, &root->root_item, sizeof(root_item));
856         btrfs_set_root_bytenr(&root_item, tmp->start);
857         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
858         btrfs_set_root_generation(&root_item, trans->transid);
859         free_extent_buffer(tmp);
860
861         key.objectid = root_objectid;
862         key.type = BTRFS_ROOT_ITEM_KEY;
863         key.offset = trans->transid;
864         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
865                                 &key, &root_item);
866
867         key.offset = (u64)-1;
868         new_root = btrfs_read_fs_root(root->fs_info, &key);
869         if (!new_root || IS_ERR(new_root)) {
870                 error("unable to fs read root: %lu", PTR_ERR(new_root));
871                 return PTR_ERR(new_root);
872         }
873
874         ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
875
876         return ret;
877 }
878
879 /*
880  * New make_btrfs() has handle system and meta chunks quite well.
881  * So only need to add remaining data chunks.
882  */
883 static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
884                                           struct btrfs_fs_info *fs_info,
885                                           struct btrfs_mkfs_config *cfg,
886                                           struct btrfs_convert_context *cctx)
887 {
888         struct btrfs_root *extent_root = fs_info->extent_root;
889         struct cache_tree *data_chunks = &cctx->data_chunks;
890         struct cache_extent *cache;
891         u64 max_chunk_size;
892         int ret = 0;
893
894         /*
895          * Don't create data chunk over 10% of the convert device
896          * And for single chunk, don't create chunk larger than 1G.
897          */
898         max_chunk_size = cfg->num_bytes / 10;
899         max_chunk_size = min((u64)(SZ_1G), max_chunk_size);
900         max_chunk_size = round_down(max_chunk_size,
901                                     extent_root->fs_info->sectorsize);
902
903         for (cache = first_cache_extent(data_chunks); cache;
904              cache = next_cache_extent(cache)) {
905                 u64 cur = cache->start;
906
907                 while (cur < cache->start + cache->size) {
908                         u64 len;
909                         u64 cur_backup = cur;
910
911                         len = min(max_chunk_size,
912                                   cache->start + cache->size - cur);
913                         ret = btrfs_alloc_data_chunk(trans, fs_info,
914                                         &cur_backup, len,
915                                         BTRFS_BLOCK_GROUP_DATA, 1);
916                         if (ret < 0)
917                                 break;
918                         ret = btrfs_make_block_group(trans, fs_info, 0,
919                                         BTRFS_BLOCK_GROUP_DATA, cur, len);
920                         if (ret < 0)
921                                 break;
922                         cur += len;
923                 }
924         }
925         return ret;
926 }
927
928 /*
929  * Init the temp btrfs to a operational status.
930  *
931  * It will fix the extent usage accounting(XXX: Do we really need?) and
932  * insert needed data chunks, to ensure all old fs data extents are covered
933  * by DATA chunks, preventing wrong chunks are allocated.
934  *
935  * And also create convert image subvolume and relocation tree.
936  * (XXX: Not need again?)
937  * But the convert image subvolume is *NOT* linked to fs tree yet.
938  */
939 static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
940                          struct btrfs_convert_context *cctx, u32 convert_flags)
941 {
942         struct btrfs_key location;
943         struct btrfs_trans_handle *trans;
944         struct btrfs_fs_info *fs_info = root->fs_info;
945         int ret;
946
947         /*
948          * Don't alloc any metadata/system chunk, as we don't want
949          * any meta/sys chunk allcated before all data chunks are inserted.
950          * Or we screw up the chunk layout just like the old implement.
951          */
952         fs_info->avoid_sys_chunk_alloc = 1;
953         fs_info->avoid_meta_chunk_alloc = 1;
954         trans = btrfs_start_transaction(root, 1);
955         if (IS_ERR(trans)) {
956                 error("unable to start transaction");
957                 ret = PTR_ERR(trans);
958                 goto err;
959         }
960         ret = btrfs_fix_block_accounting(trans, root);
961         if (ret)
962                 goto err;
963         ret = make_convert_data_block_groups(trans, fs_info, cfg, cctx);
964         if (ret)
965                 goto err;
966         ret = btrfs_make_root_dir(trans, fs_info->tree_root,
967                                   BTRFS_ROOT_TREE_DIR_OBJECTID);
968         if (ret)
969                 goto err;
970         memcpy(&location, &root->root_key, sizeof(location));
971         location.offset = (u64)-1;
972         ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
973                                 btrfs_super_root_dir(fs_info->super_copy),
974                                 &location, BTRFS_FT_DIR, 0);
975         if (ret)
976                 goto err;
977         ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
978                                 location.objectid,
979                                 btrfs_super_root_dir(fs_info->super_copy), 0);
980         if (ret)
981                 goto err;
982         btrfs_set_root_dirid(&fs_info->fs_root->root_item,
983                              BTRFS_FIRST_FREE_OBJECTID);
984
985         /* subvol for fs image file */
986         ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
987         if (ret < 0) {
988                 error("failed to create subvolume image root: %d", ret);
989                 goto err;
990         }
991         /* subvol for data relocation tree */
992         ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
993         if (ret < 0) {
994                 error("failed to create DATA_RELOC root: %d", ret);
995                 goto err;
996         }
997
998         ret = btrfs_commit_transaction(trans, root);
999         fs_info->avoid_sys_chunk_alloc = 0;
1000         fs_info->avoid_meta_chunk_alloc = 0;
1001 err:
1002         return ret;
1003 }
1004
1005 /*
1006  * Migrate super block to its default position and zero 0 ~ 16k
1007  */
1008 static int migrate_super_block(int fd, u64 old_bytenr)
1009 {
1010         int ret;
1011         struct extent_buffer *buf;
1012         struct btrfs_super_block *super;
1013         u32 len;
1014         u32 bytenr;
1015
1016         buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
1017         if (!buf)
1018                 return -ENOMEM;
1019
1020         buf->len = BTRFS_SUPER_INFO_SIZE;
1021         ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, old_bytenr);
1022         if (ret != BTRFS_SUPER_INFO_SIZE)
1023                 goto fail;
1024
1025         super = (struct btrfs_super_block *)buf->data;
1026         BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1027         btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1028
1029         csum_tree_block_size(buf, btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32], 0);
1030         ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
1031                 BTRFS_SUPER_INFO_OFFSET);
1032         if (ret != BTRFS_SUPER_INFO_SIZE)
1033                 goto fail;
1034
1035         ret = fsync(fd);
1036         if (ret)
1037                 goto fail;
1038
1039         memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
1040         for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1041                 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1042                 if (len > BTRFS_SUPER_INFO_SIZE)
1043                         len = BTRFS_SUPER_INFO_SIZE;
1044                 ret = pwrite(fd, buf->data, len, bytenr);
1045                 if (ret != len) {
1046                         fprintf(stderr, "unable to zero fill device\n");
1047                         break;
1048                 }
1049                 bytenr += len;
1050         }
1051         ret = 0;
1052         fsync(fd);
1053 fail:
1054         free(buf);
1055         if (ret > 0)
1056                 ret = -1;
1057         return ret;
1058 }
1059
1060 static int convert_open_fs(const char *devname,
1061                            struct btrfs_convert_context *cctx)
1062 {
1063         int i;
1064
1065         for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
1066                 int ret = convert_operations[i]->open_fs(cctx, devname);
1067
1068                 if (ret == 0) {
1069                         cctx->convert_ops = convert_operations[i];
1070                         return ret;
1071                 }
1072         }
1073
1074         error("no file system found to convert");
1075         return -1;
1076 }
1077
1078 static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
1079                 const char *fslabel, int progress, u64 features)
1080 {
1081         int ret;
1082         int fd = -1;
1083         u32 blocksize;
1084         u64 total_bytes;
1085         struct btrfs_root *root;
1086         struct btrfs_root *image_root;
1087         struct btrfs_convert_context cctx;
1088         struct btrfs_key key;
1089         char subvol_name[SOURCE_FS_NAME_LEN + 8];
1090         struct task_ctx ctx;
1091         char features_buf[64];
1092         struct btrfs_mkfs_config mkfs_cfg;
1093
1094         init_convert_context(&cctx);
1095         ret = convert_open_fs(devname, &cctx);
1096         if (ret)
1097                 goto fail;
1098         ret = convert_check_state(&cctx);
1099         if (ret)
1100                 warning(
1101                 "source filesystem is not clean, running filesystem check is recommended");
1102         ret = convert_read_used_space(&cctx);
1103         if (ret)
1104                 goto fail;
1105
1106         blocksize = cctx.blocksize;
1107         total_bytes = (u64)blocksize * (u64)cctx.block_count;
1108         if (blocksize < 4096) {
1109                 error("block size is too small: %u < 4096", blocksize);
1110                 goto fail;
1111         }
1112         if (btrfs_check_nodesize(nodesize, blocksize, features))
1113                 goto fail;
1114         fd = open(devname, O_RDWR);
1115         if (fd < 0) {
1116                 error("unable to open %s: %m", devname);
1117                 goto fail;
1118         }
1119         btrfs_parse_features_to_string(features_buf, features);
1120         if (features == BTRFS_MKFS_DEFAULT_FEATURES)
1121                 strcat(features_buf, " (default)");
1122
1123         printf("create btrfs filesystem:\n");
1124         printf("\tblocksize: %u\n", blocksize);
1125         printf("\tnodesize:  %u\n", nodesize);
1126         printf("\tfeatures:  %s\n", features_buf);
1127
1128         memset(&mkfs_cfg, 0, sizeof(mkfs_cfg));
1129         mkfs_cfg.label = cctx.volume_name;
1130         mkfs_cfg.num_bytes = total_bytes;
1131         mkfs_cfg.nodesize = nodesize;
1132         mkfs_cfg.sectorsize = blocksize;
1133         mkfs_cfg.stripesize = blocksize;
1134         mkfs_cfg.features = features;
1135
1136         ret = make_convert_btrfs(fd, &mkfs_cfg, &cctx);
1137         if (ret) {
1138                 error("unable to create initial ctree: %s", strerror(-ret));
1139                 goto fail;
1140         }
1141
1142         root = open_ctree_fd(fd, devname, mkfs_cfg.super_bytenr,
1143                              OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER);
1144         if (!root) {
1145                 error("unable to open ctree");
1146                 goto fail;
1147         }
1148         ret = init_btrfs(&mkfs_cfg, root, &cctx, convert_flags);
1149         if (ret) {
1150                 error("unable to setup the root tree: %d", ret);
1151                 goto fail;
1152         }
1153
1154         printf("creating %s image file\n", cctx.convert_ops->name);
1155         snprintf(subvol_name, sizeof(subvol_name), "%s_saved",
1156                         cctx.convert_ops->name);
1157         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1158         key.offset = (u64)-1;
1159         key.type = BTRFS_ROOT_ITEM_KEY;
1160         image_root = btrfs_read_fs_root(root->fs_info, &key);
1161         if (!image_root) {
1162                 error("unable to create image subvolume");
1163                 goto fail;
1164         }
1165         ret = create_image(image_root, &mkfs_cfg, &cctx, fd,
1166                               mkfs_cfg.num_bytes, "image",
1167                               convert_flags);
1168         if (ret) {
1169                 error("failed to create %s/image: %d", subvol_name, ret);
1170                 goto fail;
1171         }
1172
1173         printf("creating btrfs metadata\n");
1174         ret = pthread_mutex_init(&ctx.mutex, NULL);
1175         if (ret) {
1176                 error("failed to initialize mutex: %d", ret);
1177                 goto fail;
1178         }
1179         ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
1180         ctx.cur_copy_inodes = 0;
1181
1182         if (progress) {
1183                 ctx.info = task_init(print_copied_inodes, after_copied_inodes,
1184                                      &ctx);
1185                 task_start(ctx.info);
1186         }
1187         ret = copy_inodes(&cctx, root, convert_flags, &ctx);
1188         if (ret) {
1189                 error("error during copy_inodes %d", ret);
1190                 goto fail;
1191         }
1192         if (progress) {
1193                 task_stop(ctx.info);
1194                 task_deinit(ctx.info);
1195         }
1196
1197         image_root = btrfs_mksubvol(root, subvol_name,
1198                                     CONV_IMAGE_SUBVOL_OBJECTID, true);
1199         if (!image_root) {
1200                 error("unable to link subvolume %s", subvol_name);
1201                 goto fail;
1202         }
1203
1204         memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
1205         if (convert_flags & CONVERT_FLAG_COPY_LABEL) {
1206                 __strncpy_null(root->fs_info->super_copy->label,
1207                                 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
1208                 printf("copy label '%s'\n", root->fs_info->super_copy->label);
1209         } else if (convert_flags & CONVERT_FLAG_SET_LABEL) {
1210                 strcpy(root->fs_info->super_copy->label, fslabel);
1211                 printf("set label to '%s'\n", fslabel);
1212         }
1213
1214         ret = close_ctree(root);
1215         if (ret) {
1216                 error("close_ctree failed: %d", ret);
1217                 goto fail;
1218         }
1219         convert_close_fs(&cctx);
1220         clean_convert_context(&cctx);
1221
1222         /*
1223          * If this step succeed, we get a mountable btrfs. Otherwise
1224          * the source fs is left unchanged.
1225          */
1226         ret = migrate_super_block(fd, mkfs_cfg.super_bytenr);
1227         if (ret) {
1228                 error("unable to migrate super block: %d", ret);
1229                 goto fail;
1230         }
1231
1232         root = open_ctree_fd(fd, devname, 0,
1233                              OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER);
1234         if (!root) {
1235                 error("unable to open ctree for finalization");
1236                 goto fail;
1237         }
1238         root->fs_info->finalize_on_close = 1;
1239         close_ctree(root);
1240         close(fd);
1241
1242         printf("conversion complete\n");
1243         return 0;
1244 fail:
1245         clean_convert_context(&cctx);
1246         if (fd != -1)
1247                 close(fd);
1248         warning(
1249 "an error occurred during conversion, filesystem is partially created but not finalized and not mountable");
1250         return -1;
1251 }
1252
1253 /*
1254  * Read out data of convert image which is in btrfs reserved ranges so we can
1255  * use them to overwrite the ranges during rollback.
1256  */
1257 static int read_reserved_ranges(struct btrfs_root *root, u64 ino,
1258                                 u64 total_bytes, char *reserved_ranges[])
1259 {
1260         int i;
1261         int ret = 0;
1262
1263         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
1264                 const struct simple_range *range = &btrfs_reserved_ranges[i];
1265
1266                 if (range->start + range->len >= total_bytes)
1267                         break;
1268                 ret = btrfs_read_file(root, ino, range->start, range->len,
1269                                       reserved_ranges[i]);
1270                 if (ret < range->len) {
1271                         error(
1272         "failed to read data of convert image, offset=%llu len=%llu ret=%d",
1273                               range->start, range->len, ret);
1274                         if (ret >= 0)
1275                                 ret = -EIO;
1276                         break;
1277                 }
1278                 ret = 0;
1279         }
1280         return ret;
1281 }
1282
1283 static bool is_subset_of_reserved_ranges(u64 start, u64 len)
1284 {
1285         int i;
1286         bool ret = false;
1287
1288         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
1289                 const struct simple_range *range = &btrfs_reserved_ranges[i];
1290
1291                 if (start >= range->start && start + len <= range_end(range)) {
1292                         ret = true;
1293                         break;
1294                 }
1295         }
1296         return ret;
1297 }
1298
1299 static bool is_chunk_direct_mapped(struct btrfs_fs_info *fs_info, u64 start)
1300 {
1301         struct cache_extent *ce;
1302         struct map_lookup *map;
1303         bool ret = false;
1304
1305         ce = search_cache_extent(&fs_info->mapping_tree.cache_tree, start);
1306         if (!ce)
1307                 goto out;
1308         if (ce->start > start || ce->start + ce->size < start)
1309                 goto out;
1310
1311         map = container_of(ce, struct map_lookup, ce);
1312
1313         /* Not SINGLE chunk */
1314         if (map->num_stripes != 1)
1315                 goto out;
1316
1317         /* Chunk's logical doesn't match with phisical, not 1:1 mapped */
1318         if (map->ce.start != map->stripes[0].physical)
1319                 goto out;
1320         ret = true;
1321 out:
1322         return ret;
1323 }
1324
1325 /*
1326  * Iterate all file extents of the convert image.
1327  *
1328  * All file extents except ones in btrfs_reserved_ranges must be mapped 1:1
1329  * on disk. (Means thier file_offset must match their on disk bytenr)
1330  *
1331  * File extents in reserved ranges can be relocated to other place, and in
1332  * that case we will read them out for later use.
1333  */
1334 static int check_convert_image(struct btrfs_root *image_root, u64 ino,
1335                                u64 total_size, char *reserved_ranges[])
1336 {
1337         struct btrfs_key key;
1338         struct btrfs_path path;
1339         struct btrfs_fs_info *fs_info = image_root->fs_info;
1340         u64 checked_bytes = 0;
1341         int ret;
1342
1343         key.objectid = ino;
1344         key.offset = 0;
1345         key.type = BTRFS_EXTENT_DATA_KEY;
1346
1347         btrfs_init_path(&path);
1348         ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
1349         /*
1350          * It's possible that some fs doesn't store any (including sb)
1351          * data into 0~1M range, and NO_HOLES is enabled.
1352          *
1353          * So we only need to check if ret < 0
1354          */
1355         if (ret < 0) {
1356                 error("failed to iterate file extents at offset 0: %s",
1357                         strerror(-ret));
1358                 btrfs_release_path(&path);
1359                 return ret;
1360         }
1361
1362         /* Loop from the first file extents */
1363         while (1) {
1364                 struct btrfs_file_extent_item *fi;
1365                 struct extent_buffer *leaf = path.nodes[0];
1366                 u64 disk_bytenr;
1367                 u64 file_offset;
1368                 u64 ram_bytes;
1369                 int slot = path.slots[0];
1370
1371                 if (slot >= btrfs_header_nritems(leaf))
1372                         goto next;
1373                 btrfs_item_key_to_cpu(leaf, &key, slot);
1374
1375                 /*
1376                  * Iteration is done, exit normally, we have extra check out of
1377                  * the loop
1378                  */
1379                 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
1380                         ret = 0;
1381                         break;
1382                 }
1383                 file_offset = key.offset;
1384                 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1385                 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) {
1386                         ret = -EINVAL;
1387                         error(
1388                 "ino %llu offset %llu doesn't have a regular file extent",
1389                                 ino, file_offset);
1390                         break;
1391                 }
1392                 if (btrfs_file_extent_compression(leaf, fi) ||
1393                     btrfs_file_extent_encryption(leaf, fi) ||
1394                     btrfs_file_extent_other_encoding(leaf, fi)) {
1395                         ret = -EINVAL;
1396                         error(
1397                         "ino %llu offset %llu doesn't have a plain file extent",
1398                                 ino, file_offset);
1399                         break;
1400                 }
1401
1402                 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1403                 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1404
1405                 checked_bytes += ram_bytes;
1406                 /* Skip hole */
1407                 if (disk_bytenr == 0)
1408                         goto next;
1409
1410                 /*
1411                  * Most file extents must be 1:1 mapped, which means 2 things:
1412                  * 1) File extent file offset == disk_bytenr
1413                  * 2) That data chunk's logical == chunk's physical
1414                  *
1415                  * So file extent's file offset == physical position on disk.
1416                  *
1417                  * And after rolling back btrfs reserved range, other part
1418                  * remains what old fs used to be.
1419                  */
1420                 if (file_offset != disk_bytenr ||
1421                     !is_chunk_direct_mapped(fs_info, disk_bytenr)) {
1422                         /*
1423                          * Only file extent in btrfs reserved ranges are
1424                          * allowed to be non-1:1 mapped
1425                          */
1426                         if (!is_subset_of_reserved_ranges(file_offset,
1427                                                         ram_bytes)) {
1428                                 ret = -EINVAL;
1429                                 error(
1430                 "ino %llu offset %llu file extent should not be relocated",
1431                                         ino, file_offset);
1432                                 break;
1433                         }
1434                 }
1435 next:
1436                 ret = btrfs_next_item(image_root, &path);
1437                 if (ret) {
1438                         if (ret > 0)
1439                                 ret = 0;
1440                         break;
1441                 }
1442         }
1443         btrfs_release_path(&path);
1444         if (ret)
1445                 return ret;
1446         /*
1447          * For HOLES mode (without NO_HOLES), we must ensure file extents
1448          * cover the whole range of the image
1449          */
1450         if (!ret && !btrfs_fs_incompat(fs_info, NO_HOLES)) {
1451                 if (checked_bytes != total_size) {
1452                         ret = -EINVAL;
1453                         error("inode %llu has some file extents not checked",
1454                                 ino);
1455                         return ret;
1456                 }
1457         }
1458
1459         /* So far so good, read old data located in btrfs reserved ranges */
1460         ret = read_reserved_ranges(image_root, ino, total_size,
1461                                    reserved_ranges);
1462         return ret;
1463 }
1464
1465 /*
1466  * btrfs rollback is just reverted convert:
1467  * |<---------------Btrfs fs------------------------------>|
1468  * |<-   Old data chunk  ->|< new chunk (D/M/S)>|<- ODC  ->|
1469  * |<-Old-FE->| |<-Old-FE->|<- Btrfs extents  ->|<-Old-FE->|
1470  *                           ||
1471  *                           \/
1472  * |<------------------Old fs----------------------------->|
1473  * |<- used ->| |<- used ->|                    |<- used ->|
1474  *
1475  * However things are much easier than convert, we don't really need to
1476  * do the complex space calculation, but only to handle btrfs reserved space
1477  *
1478  * |<---------------------------Btrfs fs----------------------------->|
1479  * |   RSV 1   |  | Old  |   |    RSV 2  | | Old  | |   RSV 3   |
1480  * |   0~1M    |  | Fs   |   | SB2 + 64K | | Fs   | | SB3 + 64K |
1481  *
1482  * On the other hand, the converted fs image in btrfs is a completely
1483  * valid old fs.
1484  *
1485  * |<-----------------Converted fs image in btrfs-------------------->|
1486  * |   RSV 1   |  | Old  |   |    RSV 2  | | Old  | |   RSV 3   |
1487  * | Relocated |  | Fs   |   | Relocated | | Fs   | | Relocated |
1488  *
1489  * Used space in fs image should be at the same physical position on disk.
1490  * We only need to recover the data in reserved ranges, so the whole
1491  * old fs is back.
1492  *
1493  * The idea to rollback is also straightforward, we just "read" out the data
1494  * of reserved ranges, and write them back to there they should be.
1495  * Then the old fs is back.
1496  */
1497 static int do_rollback(const char *devname)
1498 {
1499         struct btrfs_root *root;
1500         struct btrfs_root *image_root;
1501         struct btrfs_fs_info *fs_info;
1502         struct btrfs_key key;
1503         struct btrfs_path path;
1504         struct btrfs_dir_item *dir;
1505         struct btrfs_inode_item *inode_item;
1506         char *image_name = "image";
1507         char *reserved_ranges[ARRAY_SIZE(btrfs_reserved_ranges)] = { NULL };
1508         u64 total_bytes;
1509         u64 fsize;
1510         u64 root_dir;
1511         u64 ino;
1512         int fd = -1;
1513         int ret;
1514         int i;
1515
1516         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
1517                 const struct simple_range *range = &btrfs_reserved_ranges[i];
1518
1519                 reserved_ranges[i] = calloc(1, range->len);
1520                 if (!reserved_ranges[i]) {
1521                         ret = -ENOMEM;
1522                         goto free_mem;
1523                 }
1524         }
1525         fd = open(devname, O_RDWR);
1526         if (fd < 0) {
1527                 error("unable to open %s: %m", devname);
1528                 ret = -EIO;
1529                 goto free_mem;
1530         }
1531         fsize = lseek(fd, 0, SEEK_END);
1532
1533         /*
1534          * For rollback, we don't really need to write anything so open it
1535          * read-only.  The write part will happen after we close the
1536          * filesystem.
1537          */
1538         root = open_ctree_fd(fd, devname, 0, 0);
1539         if (!root) {
1540                 error("unable to open ctree");
1541                 ret = -EIO;
1542                 goto free_mem;
1543         }
1544         fs_info = root->fs_info;
1545
1546         /*
1547          * Search root backref first, or after subvolume deletion (orphan),
1548          * we can still rollback the image.
1549          */
1550         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1551         key.type = BTRFS_ROOT_BACKREF_KEY;
1552         key.offset = BTRFS_FS_TREE_OBJECTID;
1553         btrfs_init_path(&path);
1554         ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, &path, 0, 0);
1555         btrfs_release_path(&path);
1556         if (ret > 0) {
1557                 error("unable to find source fs image subvolume, is it deleted?");
1558                 ret = -ENOENT;
1559                 goto close_fs;
1560         } else if (ret < 0) {
1561                 error("failed to find source fs image subvolume: %s",
1562                         strerror(-ret));
1563                 goto close_fs;
1564         }
1565
1566         /* Search convert subvolume */
1567         key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1568         key.type = BTRFS_ROOT_ITEM_KEY;
1569         key.offset = (u64)-1;
1570         image_root = btrfs_read_fs_root(fs_info, &key);
1571         if (IS_ERR(image_root)) {
1572                 ret = PTR_ERR(image_root);
1573                 error("failed to open convert image subvolume: %s",
1574                         strerror(-ret));
1575                 goto close_fs;
1576         }
1577
1578         /* Search the image file */
1579         root_dir = btrfs_root_dirid(&image_root->root_item);
1580         dir = btrfs_lookup_dir_item(NULL, image_root, &path, root_dir,
1581                         image_name, strlen(image_name), 0);
1582
1583         if (!dir || IS_ERR(dir)) {
1584                 btrfs_release_path(&path);
1585                 if (dir)
1586                         ret = PTR_ERR(dir);
1587                 else
1588                         ret = -ENOENT;
1589                 error("failed to locate file %s: %s", image_name,
1590                         strerror(-ret));
1591                 goto close_fs;
1592         }
1593         btrfs_dir_item_key_to_cpu(path.nodes[0], dir, &key);
1594         btrfs_release_path(&path);
1595
1596         /* Get total size of the original image */
1597         ino = key.objectid;
1598
1599         ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
1600
1601         if (ret < 0) {
1602                 btrfs_release_path(&path);
1603                 error("unable to find inode %llu: %s", ino, strerror(-ret));
1604                 goto close_fs;
1605         }
1606         inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
1607                                     struct btrfs_inode_item);
1608         total_bytes = btrfs_inode_size(path.nodes[0], inode_item);
1609         btrfs_release_path(&path);
1610
1611         /* Check if we can rollback the image */
1612         ret = check_convert_image(image_root, ino, total_bytes, reserved_ranges);
1613         if (ret < 0) {
1614                 error("old fs image can't be rolled back");
1615                 goto close_fs;
1616         }
1617 close_fs:
1618         btrfs_release_path(&path);
1619         close_ctree_fs_info(fs_info);
1620         if (ret)
1621                 goto free_mem;
1622
1623         /*
1624          * Everything is OK, just write back old fs data into btrfs reserved
1625          * ranges
1626          *
1627          * Here, we starts from the backup blocks first, so if something goes
1628          * wrong, the fs is still mountable
1629          */
1630
1631         for (i = ARRAY_SIZE(btrfs_reserved_ranges) - 1; i >= 0; i--) {
1632                 u64 real_size;
1633                 const struct simple_range *range = &btrfs_reserved_ranges[i];
1634
1635                 if (range_end(range) >= fsize)
1636                         continue;
1637
1638                 real_size = min(range_end(range), fsize) - range->start;
1639                 ret = pwrite(fd, reserved_ranges[i], real_size, range->start);
1640                 if (ret < real_size) {
1641                         if (ret < 0)
1642                                 ret = -errno;
1643                         else
1644                                 ret = -EIO;
1645                         error("failed to recover range [%llu, %llu): %s",
1646                               range->start, real_size, strerror(-ret));
1647                         goto free_mem;
1648                 }
1649                 ret = 0;
1650         }
1651
1652 free_mem:
1653         for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++)
1654                 free(reserved_ranges[i]);
1655         if (ret)
1656                 error("rollback failed");
1657         else
1658                 printf("rollback succeeded\n");
1659         return ret;
1660 }
1661
1662 static void print_usage(void)
1663 {
1664         printf("usage: btrfs-convert [options] device\n");
1665         printf("options:\n");
1666         printf("\t-d|--no-datasum        disable data checksum, sets NODATASUM\n");
1667         printf("\t-i|--no-xattr          ignore xattrs and ACLs\n");
1668         printf("\t-n|--no-inline         disable inlining of small files to metadata\n");
1669         printf("\t-N|--nodesize SIZE     set filesystem metadata nodesize\n");
1670         printf("\t-r|--rollback          roll back to the original filesystem\n");
1671         printf("\t-l|--label LABEL       set filesystem label\n");
1672         printf("\t-L|--copy-label        use label from converted filesystem\n");
1673         printf("\t-p|--progress          show converting progress (default)\n");
1674         printf("\t-O|--features LIST     comma separated list of filesystem features\n");
1675         printf("\t--no-progress          show only overview, not the detailed progress\n");
1676         printf("\n");
1677         printf("Supported filesystems:\n");
1678         printf("\text2/3/4: %s\n", BTRFSCONVERT_EXT2 ? "yes" : "no");
1679         printf("\treiserfs: %s\n", BTRFSCONVERT_REISERFS ? "yes" : "no");
1680 }
1681
1682 int main(int argc, char *argv[])
1683 {
1684         int ret;
1685         int packing = 1;
1686         int noxattr = 0;
1687         int datacsum = 1;
1688         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1689                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
1690         int rollback = 0;
1691         int copylabel = 0;
1692         int usage_error = 0;
1693         int progress = 1;
1694         char *file;
1695         char fslabel[BTRFS_LABEL_SIZE];
1696         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1697
1698         while(1) {
1699                 enum { GETOPT_VAL_NO_PROGRESS = 256 };
1700                 static const struct option long_options[] = {
1701                         { "no-progress", no_argument, NULL,
1702                                 GETOPT_VAL_NO_PROGRESS },
1703                         { "no-datasum", no_argument, NULL, 'd' },
1704                         { "no-inline", no_argument, NULL, 'n' },
1705                         { "no-xattr", no_argument, NULL, 'i' },
1706                         { "rollback", no_argument, NULL, 'r' },
1707                         { "features", required_argument, NULL, 'O' },
1708                         { "progress", no_argument, NULL, 'p' },
1709                         { "label", required_argument, NULL, 'l' },
1710                         { "copy-label", no_argument, NULL, 'L' },
1711                         { "nodesize", required_argument, NULL, 'N' },
1712                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
1713                         { NULL, 0, NULL, 0 }
1714                 };
1715                 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
1716
1717                 if (c < 0)
1718                         break;
1719                 switch(c) {
1720                         case 'd':
1721                                 datacsum = 0;
1722                                 break;
1723                         case 'i':
1724                                 noxattr = 1;
1725                                 break;
1726                         case 'n':
1727                                 packing = 0;
1728                                 break;
1729                         case 'N':
1730                                 nodesize = parse_size(optarg);
1731                                 break;
1732                         case 'r':
1733                                 rollback = 1;
1734                                 break;
1735                         case 'l':
1736                                 copylabel = CONVERT_FLAG_SET_LABEL;
1737                                 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
1738                                         warning(
1739                                         "label too long, trimmed to %d bytes",
1740                                                 BTRFS_LABEL_SIZE - 1);
1741                                 }
1742                                 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
1743                                 break;
1744                         case 'L':
1745                                 copylabel = CONVERT_FLAG_COPY_LABEL;
1746                                 break;
1747                         case 'p':
1748                                 progress = 1;
1749                                 break;
1750                         case 'O': {
1751                                 char *orig = strdup(optarg);
1752                                 char *tmp = orig;
1753
1754                                 tmp = btrfs_parse_fs_features(tmp, &features);
1755                                 if (tmp) {
1756                                         error("unrecognized filesystem feature: %s",
1757                                                         tmp);
1758                                         free(orig);
1759                                         exit(1);
1760                                 }
1761                                 free(orig);
1762                                 if (features & BTRFS_FEATURE_LIST_ALL) {
1763                                         btrfs_list_all_fs_features(
1764                                                 ~BTRFS_CONVERT_ALLOWED_FEATURES);
1765                                         exit(0);
1766                                 }
1767                                 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
1768                                         char buf[64];
1769
1770                                         btrfs_parse_features_to_string(buf,
1771                                                 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
1772                                         error("features not allowed for convert: %s",
1773                                                 buf);
1774                                         exit(1);
1775                                 }
1776
1777                                 break;
1778                                 }
1779                         case GETOPT_VAL_NO_PROGRESS:
1780                                 progress = 0;
1781                                 break;
1782                         case GETOPT_VAL_HELP:
1783                         default:
1784                                 print_usage();
1785                                 return c != GETOPT_VAL_HELP;
1786                 }
1787         }
1788         set_argv0(argv);
1789         if (check_argc_exact(argc - optind, 1)) {
1790                 print_usage();
1791                 return 1;
1792         }
1793
1794         if (rollback && (!datacsum || noxattr || !packing)) {
1795                 fprintf(stderr,
1796                         "Usage error: -d, -i, -n options do not apply to rollback\n");
1797                 usage_error++;
1798         }
1799
1800         if (usage_error) {
1801                 print_usage();
1802                 return 1;
1803         }
1804
1805         file = argv[optind];
1806         ret = check_mounted(file);
1807         if (ret < 0) {
1808                 error("could not check mount status: %s", strerror(-ret));
1809                 return 1;
1810         } else if (ret) {
1811                 error("%s is mounted", file);
1812                 return 1;
1813         }
1814
1815         if (rollback) {
1816                 ret = do_rollback(file);
1817         } else {
1818                 u32 cf = 0;
1819
1820                 cf |= datacsum ? CONVERT_FLAG_DATACSUM : 0;
1821                 cf |= packing ? CONVERT_FLAG_INLINE_DATA : 0;
1822                 cf |= noxattr ? 0 : CONVERT_FLAG_XATTR;
1823                 cf |= copylabel;
1824                 ret = do_convert(file, cf, nodesize, fslabel, progress, features);
1825         }
1826         if (ret)
1827                 return 1;
1828         return 0;
1829 }