2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include "kerncompat.h"
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
25 #include <sys/types.h>
29 #include <uuid/uuid.h>
30 #include <linux/limits.h>
36 #include "transaction.h"
39 #include "task-utils.h"
41 #include "mkfs/common.h"
42 #include "convert/common.h"
43 #include "convert/source-fs.h"
44 #include "fsfeatures.h"
46 const struct btrfs_convert_operations ext2_convert_ops;
48 static const struct btrfs_convert_operations *convert_operations[] = {
54 static void *print_copied_inodes(void *p)
56 struct task_ctx *priv = p;
57 const char work_indicator[] = { '.', 'o', 'O', 'o' };
60 task_period_start(priv->info, 1000 /* 1s */);
63 printf("copy inodes [%c] [%10llu/%10llu]\r",
64 work_indicator[count % 4],
65 (unsigned long long)priv->cur_copy_inodes,
66 (unsigned long long)priv->max_copy_inodes);
68 task_period_wait(priv->info);
74 static int after_copied_inodes(void *p)
82 static inline int copy_inodes(struct btrfs_convert_context *cctx,
83 struct btrfs_root *root, int datacsum,
84 int packing, int noxattr, struct task_ctx *p)
86 return cctx->convert_ops->copy_inodes(cctx, root, datacsum, packing,
90 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
92 cctx->convert_ops->close_fs(cctx);
95 static inline int convert_check_state(struct btrfs_convert_context *cctx)
97 return cctx->convert_ops->check_state(cctx);
100 static int csum_disk_extent(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 u64 disk_bytenr, u64 num_bytes)
104 u32 blocksize = root->sectorsize;
109 buffer = malloc(blocksize);
112 for (offset = 0; offset < num_bytes; offset += blocksize) {
113 ret = read_disk_extent(root, disk_bytenr + offset,
117 ret = btrfs_csum_file_block(trans,
118 root->fs_info->csum_root,
119 disk_bytenr + num_bytes,
120 disk_bytenr + offset,
129 static int create_image_file_range(struct btrfs_trans_handle *trans,
130 struct btrfs_root *root,
131 struct cache_tree *used,
132 struct btrfs_inode_item *inode,
133 u64 ino, u64 bytenr, u64 *ret_len,
136 struct cache_extent *cache;
137 struct btrfs_block_group_cache *bg_cache;
143 if (bytenr != round_down(bytenr, root->sectorsize)) {
144 error("bytenr not sectorsize aligned: %llu",
145 (unsigned long long)bytenr);
148 if (len != round_down(len, root->sectorsize)) {
149 error("length not sectorsize aligned: %llu",
150 (unsigned long long)len);
153 len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE);
156 * Skip sb ranges first
157 * [0, 1M), [sb_offset(1), +64K), [sb_offset(2), +64K].
159 * Or we will insert a hole into current image file, and later
160 * migrate block will fail as there is already a file extent.
162 if (bytenr < 1024 * 1024) {
163 *ret_len = 1024 * 1024 - bytenr;
166 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
167 u64 cur = btrfs_sb_offset(i);
169 if (bytenr >= cur && bytenr < cur + BTRFS_STRIPE_LEN) {
170 *ret_len = cur + BTRFS_STRIPE_LEN - bytenr;
174 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
175 u64 cur = btrfs_sb_offset(i);
180 * May still need to go through file extent inserts
182 if (bytenr < cur && bytenr + len >= cur) {
183 len = min_t(u64, len, cur - bytenr);
189 * Drop out, no need to insert anything
191 if (bytenr >= cur && bytenr < cur + BTRFS_STRIPE_LEN) {
192 *ret_len = cur + BTRFS_STRIPE_LEN - bytenr;
197 cache = search_cache_extent(used, bytenr);
199 if (cache->start <= bytenr) {
201 * |///////Used///////|
205 len = min_t(u64, len, cache->start + cache->size -
207 disk_bytenr = bytenr;
214 len = min(len, cache->start - bytenr);
229 /* Check if the range is in a data block group */
230 bg_cache = btrfs_lookup_block_group(root->fs_info, bytenr);
233 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
236 /* The extent should never cross block group boundary */
237 len = min_t(u64, len, bg_cache->key.objectid +
238 bg_cache->key.offset - bytenr);
241 if (len != round_down(len, root->sectorsize)) {
242 error("remaining length not sectorsize aligned: %llu",
243 (unsigned long long)len);
246 ret = btrfs_record_file_extent(trans, root, ino, inode, bytenr,
252 ret = csum_disk_extent(trans, root, bytenr, len);
258 * Relocate old fs data in one reserved ranges
260 * Since all old fs data in reserved range is not covered by any chunk nor
261 * data extent, we don't need to handle any reference but add new
262 * extent/reference, which makes codes more clear
264 static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root,
266 struct cache_tree *used,
267 struct btrfs_inode_item *inode, int fd,
268 u64 ino, u64 start, u64 len, int datacsum)
272 u64 hole_start = start;
274 struct cache_extent *cache;
275 struct btrfs_key key;
276 struct extent_buffer *eb;
279 while (cur_off < start + len) {
280 cache = lookup_cache_extent(used, cur_off, cur_len);
283 cur_off = max(cache->start, cur_off);
284 cur_len = min(cache->start + cache->size, start + len) -
286 BUG_ON(cur_len < root->sectorsize);
288 /* reserve extent for the data */
289 ret = btrfs_reserve_extent(trans, root, cur_len, 0, 0, (u64)-1,
294 eb = malloc(sizeof(*eb) + cur_len);
300 ret = pread(fd, eb->data, cur_len, cur_off);
302 ret = (ret < 0 ? ret : -EIO);
306 eb->start = key.objectid;
307 eb->len = key.offset;
310 ret = write_and_map_eb(trans, root, eb);
315 /* Now handle extent item and file extent things */
316 ret = btrfs_record_file_extent(trans, root, ino, inode, cur_off,
317 key.objectid, key.offset);
320 /* Finally, insert csum items */
322 ret = csum_disk_extent(trans, root, key.objectid,
325 /* Don't forget to insert hole */
326 hole_len = cur_off - hole_start;
328 ret = btrfs_record_file_extent(trans, root, ino, inode,
329 hole_start, 0, hole_len);
334 cur_off += key.offset;
335 hole_start = cur_off;
336 cur_len = start + len - cur_off;
339 if (start + len - hole_start > 0)
340 ret = btrfs_record_file_extent(trans, root, ino, inode,
341 hole_start, 0, start + len - hole_start);
346 * Relocate the used ext2 data in reserved ranges
348 * [btrfs_sb_offset(1), +BTRFS_STRIPE_LEN)
349 * [btrfs_sb_offset(2), +BTRFS_STRIPE_LEN)
351 static int migrate_reserved_ranges(struct btrfs_trans_handle *trans,
352 struct btrfs_root *root,
353 struct cache_tree *used,
354 struct btrfs_inode_item *inode, int fd,
355 u64 ino, u64 total_bytes, int datacsum)
363 cur_len = 1024 * 1024;
364 ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
365 cur_off, cur_len, datacsum);
369 /* second sb(fisrt sb is included in 0~1M) */
370 cur_off = btrfs_sb_offset(1);
371 cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
372 if (cur_off > total_bytes)
374 ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
375 cur_off, cur_len, datacsum);
380 cur_off = btrfs_sb_offset(2);
381 cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off;
382 if (cur_off > total_bytes)
384 ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino,
385 cur_off, cur_len, datacsum);
390 * Helper for expand and merge extent_cache for wipe_one_reserved_range() to
391 * handle wiping a range that exists in cache.
393 static int _expand_extent_cache(struct cache_tree *tree,
394 struct cache_extent *entry,
395 u64 min_stripe_size, int backward)
397 struct cache_extent *ce;
400 if (entry->size >= min_stripe_size)
402 diff = min_stripe_size - entry->size;
405 ce = prev_cache_extent(entry);
408 if (ce->start + ce->size >= entry->start - diff) {
409 /* Directly merge with previous extent */
410 ce->size = entry->start + entry->size - ce->start;
411 remove_cache_extent(tree, entry);
416 /* No overlap, normal extent */
417 if (entry->start < diff) {
418 error("cannot find space for data chunk layout");
421 entry->start -= diff;
425 ce = next_cache_extent(entry);
428 if (entry->start + entry->size + diff >= ce->start) {
429 /* Directly merge with next extent */
430 entry->size = ce->start + ce->size - entry->start;
431 remove_cache_extent(tree, ce);
441 * Remove one reserve range from given cache tree
442 * if min_stripe_size is non-zero, it will ensure for split case,
443 * all its split cache extent is no smaller than @min_strip_size / 2.
445 static int wipe_one_reserved_range(struct cache_tree *tree,
446 u64 start, u64 len, u64 min_stripe_size,
449 struct cache_extent *cache;
452 BUG_ON(ensure_size && min_stripe_size == 0);
454 * The logical here is simplified to handle special cases only
455 * So we don't need to consider merge case for ensure_size
457 BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
458 min_stripe_size / 2 < BTRFS_STRIPE_LEN));
460 /* Also, wipe range should already be aligned */
461 BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
462 start + len != round_up(start + len, BTRFS_STRIPE_LEN));
464 min_stripe_size /= 2;
466 cache = lookup_cache_extent(tree, start, len);
470 if (start <= cache->start) {
472 * |--------cache---------|
475 BUG_ON(start + len <= cache->start);
478 * The wipe size is smaller than min_stripe_size / 2,
479 * so the result length should still meet min_stripe_size
480 * And no need to do alignment
482 cache->size -= (start + len - cache->start);
483 if (cache->size == 0) {
484 remove_cache_extent(tree, cache);
489 BUG_ON(ensure_size && cache->size < min_stripe_size);
491 cache->start = start + len;
493 } else if (start > cache->start && start + len < cache->start +
496 * |-------cache-----|
499 u64 old_start = cache->start;
500 u64 old_len = cache->size;
501 u64 insert_start = start + len;
504 cache->size = start - cache->start;
505 /* Expand the leading half part if needed */
506 if (ensure_size && cache->size < min_stripe_size) {
507 ret = _expand_extent_cache(tree, cache,
513 /* And insert the new one */
514 insert_len = old_start + old_len - start - len;
515 ret = add_merge_cache_extent(tree, insert_start, insert_len);
519 /* Expand the last half part if needed */
520 if (ensure_size && insert_len < min_stripe_size) {
521 cache = lookup_cache_extent(tree, insert_start,
523 if (!cache || cache->start != insert_start ||
524 cache->size != insert_len)
526 ret = _expand_extent_cache(tree, cache,
535 * Wipe len should be small enough and no need to expand the
538 cache->size = start - cache->start;
539 BUG_ON(ensure_size && cache->size < min_stripe_size);
544 * Remove reserved ranges from given cache_tree
546 * It will remove the following ranges
548 * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
549 * 3) 3rd superblock, +64K
551 * @min_stripe must be given for safety check
552 * and if @ensure_size is given, it will ensure affected cache_extent will be
553 * larger than min_stripe_size
555 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
560 ret = wipe_one_reserved_range(tree, 0, 1024 * 1024, min_stripe_size,
564 ret = wipe_one_reserved_range(tree, btrfs_sb_offset(1),
565 BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
568 ret = wipe_one_reserved_range(tree, btrfs_sb_offset(2),
569 BTRFS_STRIPE_LEN, min_stripe_size, ensure_size);
573 static int calculate_available_space(struct btrfs_convert_context *cctx)
575 struct cache_tree *used = &cctx->used;
576 struct cache_tree *data_chunks = &cctx->data_chunks;
577 struct cache_tree *free = &cctx->free;
578 struct cache_extent *cache;
581 * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
582 * works without need to consider overlap
584 u64 min_stripe_size = 2 * 16 * 1024 * 1024;
587 /* Calculate data_chunks */
588 for (cache = first_cache_extent(used); cache;
589 cache = next_cache_extent(cache)) {
592 if (cache->start + cache->size < cur_off)
594 if (cache->start > cur_off + min_stripe_size)
595 cur_off = cache->start;
596 cur_len = max(cache->start + cache->size - cur_off,
598 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
604 * remove reserved ranges, so we won't ever bother relocating an old
605 * filesystem extent to other place.
607 ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
613 * Calculate free space
614 * Always round up the start bytenr, to avoid metadata extent corss
615 * stripe boundary, as later mkfs_convert() won't have all the extent
618 for (cache = first_cache_extent(data_chunks); cache;
619 cache = next_cache_extent(cache)) {
620 if (cache->start < cur_off)
622 if (cache->start > cur_off) {
626 len = cache->start - round_up(cur_off,
628 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
630 ret = add_merge_cache_extent(free, insert_start, len);
634 cur_off = cache->start + cache->size;
636 /* Don't forget the last range */
637 if (cctx->total_bytes > cur_off) {
638 u64 len = cctx->total_bytes - cur_off;
641 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
643 ret = add_merge_cache_extent(free, insert_start, len);
648 /* Remove reserved bytes */
649 ret = wipe_reserved_ranges(free, min_stripe_size, 0);
655 * Read used space, and since we have the used space,
656 * calcuate data_chunks and free for later mkfs
658 static int convert_read_used_space(struct btrfs_convert_context *cctx)
662 ret = cctx->convert_ops->read_used_space(cctx);
666 ret = calculate_available_space(cctx);
671 * Create the fs image file of old filesystem.
673 * This is completely fs independent as we have cctx->used, only
674 * need to create file extents pointing to all the positions.
676 static int create_image(struct btrfs_root *root,
677 struct btrfs_mkfs_config *cfg,
678 struct btrfs_convert_context *cctx, int fd,
679 u64 size, char *name, int datacsum)
681 struct btrfs_inode_item buf;
682 struct btrfs_trans_handle *trans;
683 struct btrfs_path path;
684 struct btrfs_key key;
685 struct cache_extent *cache;
686 struct cache_tree used_tmp;
689 u64 flags = BTRFS_INODE_READONLY;
693 flags |= BTRFS_INODE_NODATASUM;
695 trans = btrfs_start_transaction(root, 1);
699 cache_tree_init(&used_tmp);
700 btrfs_init_path(&path);
702 ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
706 ret = btrfs_new_inode(trans, root, ino, 0400 | S_IFREG);
709 ret = btrfs_change_inode_flags(trans, root, ino, flags);
712 ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
713 strlen(name), BTRFS_FT_REG_FILE, NULL, 1);
718 key.type = BTRFS_INODE_ITEM_KEY;
721 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
723 ret = (ret > 0 ? -ENOENT : ret);
726 read_extent_buffer(path.nodes[0], &buf,
727 btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
729 btrfs_release_path(&path);
732 * Create a new used space cache, which doesn't contain the reserved
735 for (cache = first_cache_extent(&cctx->used); cache;
736 cache = next_cache_extent(cache)) {
737 ret = add_cache_extent(&used_tmp, cache->start, cache->size);
741 ret = wipe_reserved_ranges(&used_tmp, 0, 0);
746 * Start from 1M, as 0~1M is reserved, and create_image_file_range()
747 * can't handle bytenr 0(will consider it as a hole)
751 u64 len = size - cur;
753 ret = create_image_file_range(trans, root, &used_tmp,
754 &buf, ino, cur, &len, datacsum);
759 /* Handle the reserved ranges */
760 ret = migrate_reserved_ranges(trans, root, &cctx->used, &buf, fd, ino,
761 cfg->num_bytes, datacsum);
765 key.type = BTRFS_INODE_ITEM_KEY;
767 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
769 ret = (ret > 0 ? -ENOENT : ret);
772 btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
773 write_extent_buffer(path.nodes[0], &buf,
774 btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
777 free_extent_cache_tree(&used_tmp);
778 btrfs_release_path(&path);
779 btrfs_commit_transaction(trans, root);
783 static struct btrfs_root* link_subvol(struct btrfs_root *root,
784 const char *base, u64 root_objectid)
786 struct btrfs_trans_handle *trans;
787 struct btrfs_fs_info *fs_info = root->fs_info;
788 struct btrfs_root *tree_root = fs_info->tree_root;
789 struct btrfs_root *new_root = NULL;
790 struct btrfs_path path;
791 struct btrfs_inode_item *inode_item;
792 struct extent_buffer *leaf;
793 struct btrfs_key key;
794 u64 dirid = btrfs_root_dirid(&root->root_item);
796 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
802 if (len == 0 || len > BTRFS_NAME_LEN)
805 btrfs_init_path(&path);
806 key.objectid = dirid;
807 key.type = BTRFS_DIR_INDEX_KEY;
808 key.offset = (u64)-1;
810 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
812 error("search for DIR_INDEX dirid %llu failed: %d",
813 (unsigned long long)dirid, ret);
817 if (path.slots[0] > 0) {
819 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
820 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
821 index = key.offset + 1;
823 btrfs_release_path(&path);
825 trans = btrfs_start_transaction(root, 1);
827 error("unable to start transaction");
831 key.objectid = dirid;
833 key.type = BTRFS_INODE_ITEM_KEY;
835 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
837 error("search for INODE_ITEM %llu failed: %d",
838 (unsigned long long)dirid, ret);
841 leaf = path.nodes[0];
842 inode_item = btrfs_item_ptr(leaf, path.slots[0],
843 struct btrfs_inode_item);
845 key.objectid = root_objectid;
846 key.offset = (u64)-1;
847 key.type = BTRFS_ROOT_ITEM_KEY;
849 memcpy(buf, base, len);
850 for (i = 0; i < 1024; i++) {
851 ret = btrfs_insert_dir_item(trans, root, buf, len,
852 dirid, &key, BTRFS_FT_DIR, index);
855 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
856 if (len < 1 || len > BTRFS_NAME_LEN) {
864 btrfs_set_inode_size(leaf, inode_item, len * 2 +
865 btrfs_inode_size(leaf, inode_item));
866 btrfs_mark_buffer_dirty(leaf);
867 btrfs_release_path(&path);
869 /* add the backref first */
870 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
871 BTRFS_ROOT_BACKREF_KEY,
872 root->root_key.objectid,
873 dirid, index, buf, len);
875 error("unable to add root backref for %llu: %d",
876 root->root_key.objectid, ret);
880 /* now add the forward ref */
881 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
882 BTRFS_ROOT_REF_KEY, root_objectid,
883 dirid, index, buf, len);
885 error("unable to add root ref for %llu: %d",
886 root->root_key.objectid, ret);
890 ret = btrfs_commit_transaction(trans, root);
892 error("transaction commit failed: %d", ret);
896 new_root = btrfs_read_fs_root(fs_info, &key);
897 if (IS_ERR(new_root)) {
898 error("unable to fs read root: %lu", PTR_ERR(new_root));
902 btrfs_init_path(&path);
906 static int create_subvol(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root, u64 root_objectid)
909 struct extent_buffer *tmp;
910 struct btrfs_root *new_root;
911 struct btrfs_key key;
912 struct btrfs_root_item root_item;
915 ret = btrfs_copy_root(trans, root, root->node, &tmp,
920 memcpy(&root_item, &root->root_item, sizeof(root_item));
921 btrfs_set_root_bytenr(&root_item, tmp->start);
922 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
923 btrfs_set_root_generation(&root_item, trans->transid);
924 free_extent_buffer(tmp);
926 key.objectid = root_objectid;
927 key.type = BTRFS_ROOT_ITEM_KEY;
928 key.offset = trans->transid;
929 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
932 key.offset = (u64)-1;
933 new_root = btrfs_read_fs_root(root->fs_info, &key);
934 if (!new_root || IS_ERR(new_root)) {
935 error("unable to fs read root: %lu", PTR_ERR(new_root));
936 return PTR_ERR(new_root);
939 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
945 * New make_btrfs() has handle system and meta chunks quite well.
946 * So only need to add remaining data chunks.
948 static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
949 struct btrfs_fs_info *fs_info,
950 struct btrfs_mkfs_config *cfg,
951 struct btrfs_convert_context *cctx)
953 struct btrfs_root *extent_root = fs_info->extent_root;
954 struct cache_tree *data_chunks = &cctx->data_chunks;
955 struct cache_extent *cache;
960 * Don't create data chunk over 10% of the convert device
961 * And for single chunk, don't create chunk larger than 1G.
963 max_chunk_size = cfg->num_bytes / 10;
964 max_chunk_size = min((u64)(1024 * 1024 * 1024), max_chunk_size);
965 max_chunk_size = round_down(max_chunk_size, extent_root->sectorsize);
967 for (cache = first_cache_extent(data_chunks); cache;
968 cache = next_cache_extent(cache)) {
969 u64 cur = cache->start;
971 while (cur < cache->start + cache->size) {
973 u64 cur_backup = cur;
975 len = min(max_chunk_size,
976 cache->start + cache->size - cur);
977 ret = btrfs_alloc_data_chunk(trans, extent_root,
979 BTRFS_BLOCK_GROUP_DATA, 1);
982 ret = btrfs_make_block_group(trans, extent_root, 0,
983 BTRFS_BLOCK_GROUP_DATA,
984 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
995 * Init the temp btrfs to a operational status.
997 * It will fix the extent usage accounting(XXX: Do we really need?) and
998 * insert needed data chunks, to ensure all old fs data extents are covered
999 * by DATA chunks, preventing wrong chunks are allocated.
1001 * And also create convert image subvolume and relocation tree.
1002 * (XXX: Not need again?)
1003 * But the convert image subvolume is *NOT* linked to fs tree yet.
1005 static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
1006 struct btrfs_convert_context *cctx, int datacsum,
1007 int packing, int noxattr)
1009 struct btrfs_key location;
1010 struct btrfs_trans_handle *trans;
1011 struct btrfs_fs_info *fs_info = root->fs_info;
1015 * Don't alloc any metadata/system chunk, as we don't want
1016 * any meta/sys chunk allcated before all data chunks are inserted.
1017 * Or we screw up the chunk layout just like the old implement.
1019 fs_info->avoid_sys_chunk_alloc = 1;
1020 fs_info->avoid_meta_chunk_alloc = 1;
1021 trans = btrfs_start_transaction(root, 1);
1023 error("unable to start transaction");
1027 ret = btrfs_fix_block_accounting(trans, root);
1030 ret = make_convert_data_block_groups(trans, fs_info, cfg, cctx);
1033 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1034 BTRFS_ROOT_TREE_DIR_OBJECTID);
1037 memcpy(&location, &root->root_key, sizeof(location));
1038 location.offset = (u64)-1;
1039 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1040 btrfs_super_root_dir(fs_info->super_copy),
1041 &location, BTRFS_FT_DIR, 0);
1044 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1046 btrfs_super_root_dir(fs_info->super_copy), 0);
1049 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1050 BTRFS_FIRST_FREE_OBJECTID);
1052 /* subvol for fs image file */
1053 ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
1055 error("failed to create subvolume image root: %d", ret);
1058 /* subvol for data relocation tree */
1059 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1061 error("failed to create DATA_RELOC root: %d", ret);
1065 ret = btrfs_commit_transaction(trans, root);
1066 fs_info->avoid_sys_chunk_alloc = 0;
1067 fs_info->avoid_meta_chunk_alloc = 0;
1073 * Migrate super block to its default position and zero 0 ~ 16k
1075 static int migrate_super_block(int fd, u64 old_bytenr)
1078 struct extent_buffer *buf;
1079 struct btrfs_super_block *super;
1083 buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
1087 buf->len = BTRFS_SUPER_INFO_SIZE;
1088 ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, old_bytenr);
1089 if (ret != BTRFS_SUPER_INFO_SIZE)
1092 super = (struct btrfs_super_block *)buf->data;
1093 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1094 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1096 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1097 ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
1098 BTRFS_SUPER_INFO_OFFSET);
1099 if (ret != BTRFS_SUPER_INFO_SIZE)
1106 memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
1107 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1108 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1109 if (len > BTRFS_SUPER_INFO_SIZE)
1110 len = BTRFS_SUPER_INFO_SIZE;
1111 ret = pwrite(fd, buf->data, len, bytenr);
1113 fprintf(stderr, "unable to zero fill device\n");
1127 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1129 struct btrfs_chunk *chunk;
1130 struct btrfs_disk_key *key;
1131 u32 sectorsize = btrfs_super_sectorsize(super);
1133 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1134 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1135 sizeof(struct btrfs_disk_key));
1137 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1138 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1139 btrfs_set_disk_key_offset(key, 0);
1141 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1142 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1143 btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1144 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1145 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1146 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1147 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1148 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1149 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1150 chunk->stripe.devid = super->dev_item.devid;
1151 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1152 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1153 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1157 static int convert_open_fs(const char *devname,
1158 struct btrfs_convert_context *cctx)
1162 for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
1163 int ret = convert_operations[i]->open_fs(cctx, devname);
1166 cctx->convert_ops = convert_operations[i];
1171 error("no file system found to convert");
1175 static int do_convert(const char *devname, int datacsum, int packing,
1176 int noxattr, u32 nodesize, int copylabel, const char *fslabel,
1177 int progress, u64 features)
1183 struct btrfs_root *root;
1184 struct btrfs_root *image_root;
1185 struct btrfs_convert_context cctx;
1186 struct btrfs_key key;
1187 char subvol_name[SOURCE_FS_NAME_LEN + 8];
1188 struct task_ctx ctx;
1189 char features_buf[64];
1190 struct btrfs_mkfs_config mkfs_cfg;
1192 init_convert_context(&cctx);
1193 ret = convert_open_fs(devname, &cctx);
1196 ret = convert_check_state(&cctx);
1199 "source filesystem is not clean, running filesystem check is recommended");
1200 ret = convert_read_used_space(&cctx);
1204 blocksize = cctx.blocksize;
1205 total_bytes = (u64)blocksize * (u64)cctx.block_count;
1206 if (blocksize < 4096) {
1207 error("block size is too small: %u < 4096", blocksize);
1210 if (btrfs_check_nodesize(nodesize, blocksize, features))
1212 fd = open(devname, O_RDWR);
1214 error("unable to open %s: %s", devname, strerror(errno));
1217 btrfs_parse_features_to_string(features_buf, features);
1218 if (features == BTRFS_MKFS_DEFAULT_FEATURES)
1219 strcat(features_buf, " (default)");
1221 printf("create btrfs filesystem:\n");
1222 printf("\tblocksize: %u\n", blocksize);
1223 printf("\tnodesize: %u\n", nodesize);
1224 printf("\tfeatures: %s\n", features_buf);
1226 mkfs_cfg.label = cctx.volume_name;
1227 mkfs_cfg.num_bytes = total_bytes;
1228 mkfs_cfg.nodesize = nodesize;
1229 mkfs_cfg.sectorsize = blocksize;
1230 mkfs_cfg.stripesize = blocksize;
1231 mkfs_cfg.features = features;
1232 /* New convert need these space */
1233 memset(mkfs_cfg.chunk_uuid, 0, BTRFS_UUID_UNPARSED_SIZE);
1234 memset(mkfs_cfg.fs_uuid, 0, BTRFS_UUID_UNPARSED_SIZE);
1236 ret = make_convert_btrfs(fd, &mkfs_cfg, &cctx);
1238 error("unable to create initial ctree: %s", strerror(-ret));
1242 root = open_ctree_fd(fd, devname, mkfs_cfg.super_bytenr,
1243 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1245 error("unable to open ctree");
1248 ret = init_btrfs(&mkfs_cfg, root, &cctx, datacsum, packing, noxattr);
1250 error("unable to setup the root tree: %d", ret);
1254 printf("creating %s image file\n", cctx.convert_ops->name);
1255 snprintf(subvol_name, sizeof(subvol_name), "%s_saved",
1256 cctx.convert_ops->name);
1257 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1258 key.offset = (u64)-1;
1259 key.type = BTRFS_ROOT_ITEM_KEY;
1260 image_root = btrfs_read_fs_root(root->fs_info, &key);
1262 error("unable to create image subvolume");
1265 ret = create_image(image_root, &mkfs_cfg, &cctx, fd,
1266 mkfs_cfg.num_bytes, "image", datacsum);
1268 error("failed to create %s/image: %d", subvol_name, ret);
1272 printf("creating btrfs metadata");
1273 ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
1274 ctx.cur_copy_inodes = 0;
1277 ctx.info = task_init(print_copied_inodes, after_copied_inodes,
1279 task_start(ctx.info);
1281 ret = copy_inodes(&cctx, root, datacsum, packing, noxattr, &ctx);
1283 error("error during copy_inodes %d", ret);
1287 task_stop(ctx.info);
1288 task_deinit(ctx.info);
1291 image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
1293 error("unable to link subvolume %s", subvol_name);
1297 memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
1298 if (copylabel == 1) {
1299 __strncpy_null(root->fs_info->super_copy->label,
1300 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
1301 printf("copy label '%s'\n", root->fs_info->super_copy->label);
1302 } else if (copylabel == -1) {
1303 strcpy(root->fs_info->super_copy->label, fslabel);
1304 printf("set label to '%s'\n", fslabel);
1307 ret = close_ctree(root);
1309 error("close_ctree failed: %d", ret);
1312 convert_close_fs(&cctx);
1313 clean_convert_context(&cctx);
1316 * If this step succeed, we get a mountable btrfs. Otherwise
1317 * the source fs is left unchanged.
1319 ret = migrate_super_block(fd, mkfs_cfg.super_bytenr);
1321 error("unable to migrate super block: %d", ret);
1325 root = open_ctree_fd(fd, devname, 0,
1326 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1328 error("unable to open ctree for finalization");
1331 root->fs_info->finalize_on_close = 1;
1335 printf("conversion complete");
1338 clean_convert_context(&cctx);
1342 "an error occurred during conversion, filesystem is partially created but not finalized and not mountable");
1347 * Check if a non 1:1 mapped chunk can be rolled back.
1348 * For new convert, it's OK while for old convert it's not.
1350 static int may_rollback_chunk(struct btrfs_fs_info *fs_info, u64 bytenr)
1352 struct btrfs_block_group_cache *bg;
1353 struct btrfs_key key;
1354 struct btrfs_path path;
1355 struct btrfs_root *extent_root = fs_info->extent_root;
1360 bg = btrfs_lookup_first_block_group(fs_info, bytenr);
1363 bg_start = bg->key.objectid;
1364 bg_end = bg->key.objectid + bg->key.offset;
1366 key.objectid = bg_end;
1367 key.type = BTRFS_METADATA_ITEM_KEY;
1369 btrfs_init_path(&path);
1371 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
1376 struct btrfs_extent_item *ei;
1378 ret = btrfs_previous_extent_item(extent_root, &path, bg_start);
1386 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
1387 if (key.type == BTRFS_METADATA_ITEM_KEY)
1389 /* Now it's EXTENT_ITEM_KEY only */
1390 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
1391 struct btrfs_extent_item);
1393 * Found data extent, means this is old convert must follow 1:1
1396 if (btrfs_extent_flags(path.nodes[0], ei)
1397 & BTRFS_EXTENT_FLAG_DATA) {
1402 btrfs_release_path(&path);
1406 static int may_rollback(struct btrfs_root *root)
1408 struct btrfs_fs_info *info = root->fs_info;
1409 struct btrfs_multi_bio *multi = NULL;
1417 if (btrfs_super_num_devices(info->super_copy) != 1)
1420 bytenr = BTRFS_SUPER_INFO_OFFSET;
1421 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1424 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
1425 &length, &multi, 0, NULL);
1427 if (ret == -ENOENT) {
1428 /* removed block group at the tail */
1429 if (length == (u64)-1)
1432 /* removed block group in the middle */
1438 num_stripes = multi->num_stripes;
1439 physical = multi->stripes[0].physical;
1442 if (num_stripes != 1) {
1443 error("num stripes for bytenr %llu is not 1", bytenr);
1448 * Extra check for new convert, as metadata chunk from new
1449 * convert is much more free than old convert, it doesn't need
1450 * to do 1:1 mapping.
1452 if (physical != bytenr) {
1454 * Check if it's a metadata chunk and has only metadata
1457 ret = may_rollback_chunk(info, bytenr);
1463 if (bytenr >= total_bytes)
1471 static int do_rollback(const char *devname)
1476 struct btrfs_root *root;
1477 struct btrfs_root *image_root;
1478 struct btrfs_root *chunk_root;
1479 struct btrfs_dir_item *dir;
1480 struct btrfs_inode_item *inode;
1481 struct btrfs_file_extent_item *fi;
1482 struct btrfs_trans_handle *trans;
1483 struct extent_buffer *leaf;
1484 struct btrfs_block_group_cache *cache1;
1485 struct btrfs_block_group_cache *cache2;
1486 struct btrfs_key key;
1487 struct btrfs_path path;
1488 struct extent_io_tree io_tree;
1503 extent_io_tree_init(&io_tree);
1505 fd = open(devname, O_RDWR);
1507 error("unable to open %s: %s", devname, strerror(errno));
1510 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
1512 error("unable to open ctree");
1515 ret = may_rollback(root);
1517 error("unable to do rollback: %d", ret);
1521 sectorsize = root->sectorsize;
1522 buf = malloc(sectorsize);
1524 error("unable to allocate memory");
1528 btrfs_init_path(&path);
1530 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1531 key.type = BTRFS_ROOT_BACKREF_KEY;
1532 key.offset = BTRFS_FS_TREE_OBJECTID;
1533 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path, 0,
1535 btrfs_release_path(&path);
1537 error("unable to convert ext2 image subvolume, is it deleted?");
1539 } else if (ret < 0) {
1540 error("unable to open ext2_saved, id %llu: %s",
1541 (unsigned long long)key.objectid, strerror(-ret));
1545 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1546 key.type = BTRFS_ROOT_ITEM_KEY;
1547 key.offset = (u64)-1;
1548 image_root = btrfs_read_fs_root(root->fs_info, &key);
1549 if (!image_root || IS_ERR(image_root)) {
1550 error("unable to open subvolume %llu: %ld",
1551 (unsigned long long)key.objectid, PTR_ERR(image_root));
1556 root_dir = btrfs_root_dirid(&root->root_item);
1557 dir = btrfs_lookup_dir_item(NULL, image_root, &path,
1558 root_dir, name, strlen(name), 0);
1559 if (!dir || IS_ERR(dir)) {
1560 error("unable to find file %s: %ld", name, PTR_ERR(dir));
1563 leaf = path.nodes[0];
1564 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
1565 btrfs_release_path(&path);
1567 objectid = key.objectid;
1569 ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
1571 error("unable to find inode item: %d", ret);
1574 leaf = path.nodes[0];
1575 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
1576 total_bytes = btrfs_inode_size(leaf, inode);
1577 btrfs_release_path(&path);
1579 key.objectid = objectid;
1581 key.type = BTRFS_EXTENT_DATA_KEY;
1582 ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
1584 error("unable to find first file extent: %d", ret);
1585 btrfs_release_path(&path);
1589 /* build mapping tree for the relocated blocks */
1590 for (offset = 0; offset < total_bytes; ) {
1591 leaf = path.nodes[0];
1592 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1593 ret = btrfs_next_leaf(root, &path);
1599 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1600 if (key.objectid != objectid || key.offset != offset ||
1601 key.type != BTRFS_EXTENT_DATA_KEY)
1604 fi = btrfs_item_ptr(leaf, path.slots[0],
1605 struct btrfs_file_extent_item);
1606 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1608 if (btrfs_file_extent_compression(leaf, fi) ||
1609 btrfs_file_extent_encryption(leaf, fi) ||
1610 btrfs_file_extent_other_encoding(leaf, fi))
1613 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1614 /* skip holes and direct mapped extents */
1615 if (bytenr == 0 || bytenr == offset)
1618 bytenr += btrfs_file_extent_offset(leaf, fi);
1619 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1621 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
1622 cache2 = btrfs_lookup_block_group(root->fs_info,
1623 offset + num_bytes - 1);
1625 * Here we must take consideration of old and new convert
1627 * For old convert case, sign, there is no consist chunk type
1628 * that will cover the extent. META/DATA/SYS are all possible.
1629 * Just ensure relocate one is in SYS chunk.
1630 * For new convert case, they are all covered by DATA chunk.
1632 * So, there is not valid chunk type check for it now.
1634 if (cache1 != cache2)
1637 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
1638 EXTENT_LOCKED, GFP_NOFS);
1639 set_state_private(&io_tree, offset, bytenr);
1641 offset += btrfs_file_extent_num_bytes(leaf, fi);
1644 btrfs_release_path(&path);
1646 if (offset < total_bytes) {
1647 error("unable to build extent mapping (offset %llu, total_bytes %llu)",
1648 (unsigned long long)offset,
1649 (unsigned long long)total_bytes);
1650 error("converted filesystem after balance is unable to rollback");
1654 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
1655 first_free &= ~((u64)sectorsize - 1);
1656 /* backup for extent #0 should exist */
1657 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
1658 error("no backup for the first extent");
1661 /* force no allocation from system block group */
1662 root->fs_info->system_allocs = -1;
1663 trans = btrfs_start_transaction(root, 1);
1665 error("unable to start transaction");
1669 * recow the whole chunk tree, this will remove all chunk tree blocks
1670 * from system block group
1672 chunk_root = root->fs_info->chunk_root;
1673 memset(&key, 0, sizeof(key));
1675 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
1679 ret = btrfs_next_leaf(chunk_root, &path);
1683 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
1684 btrfs_release_path(&path);
1686 btrfs_release_path(&path);
1691 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
1695 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
1696 num_bytes += btrfs_block_group_used(&cache1->item);
1698 offset = cache1->key.objectid + cache1->key.offset;
1700 /* only extent #0 left in system block group? */
1701 if (num_bytes > first_free) {
1703 "unable to empty system block group (num_bytes %llu, first_free %llu",
1704 (unsigned long long)num_bytes,
1705 (unsigned long long)first_free);
1708 /* create a system chunk that maps the whole device */
1709 ret = prepare_system_chunk_sb(root->fs_info->super_copy);
1711 error("unable to update system chunk: %d", ret);
1715 ret = btrfs_commit_transaction(trans, root);
1717 error("transaction commit failed: %d", ret);
1721 ret = close_ctree(root);
1723 error("close_ctree failed: %d", ret);
1727 /* zero btrfs super block mirrors */
1728 memset(buf, 0, sectorsize);
1729 for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1730 bytenr = btrfs_sb_offset(i);
1731 if (bytenr >= total_bytes)
1733 ret = pwrite(fd, buf, sectorsize, bytenr);
1734 if (ret != sectorsize) {
1735 error("zeroing superblock mirror %d failed: %d",
1741 sb_bytenr = (u64)-1;
1742 /* copy all relocated blocks back */
1744 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
1749 ret = get_state_private(&io_tree, start, &bytenr);
1752 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
1755 while (start <= end) {
1756 if (start == BTRFS_SUPER_INFO_OFFSET) {
1760 ret = pread(fd, buf, sectorsize, bytenr);
1762 error("reading superblock at %llu failed: %d",
1763 (unsigned long long)bytenr, ret);
1766 BUG_ON(ret != sectorsize);
1767 ret = pwrite(fd, buf, sectorsize, start);
1769 error("writing superblock at %llu failed: %d",
1770 (unsigned long long)start, ret);
1773 BUG_ON(ret != sectorsize);
1775 start += sectorsize;
1776 bytenr += sectorsize;
1782 error("fsync failed: %s", strerror(errno));
1786 * finally, overwrite btrfs super block.
1788 ret = pread(fd, buf, sectorsize, sb_bytenr);
1790 error("reading primary superblock failed: %s",
1794 BUG_ON(ret != sectorsize);
1795 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1797 error("writing primary superblock failed: %s",
1801 BUG_ON(ret != sectorsize);
1804 error("fsync failed: %s", strerror(errno));
1810 extent_io_tree_cleanup(&io_tree);
1811 printf("rollback complete\n");
1818 error("rollback aborted");
1822 static void print_usage(void)
1824 printf("usage: btrfs-convert [options] device\n");
1825 printf("options:\n");
1826 printf("\t-d|--no-datasum disable data checksum, sets NODATASUM\n");
1827 printf("\t-i|--no-xattr ignore xattrs and ACLs\n");
1828 printf("\t-n|--no-inline disable inlining of small files to metadata\n");
1829 printf("\t-N|--nodesize SIZE set filesystem metadata nodesize\n");
1830 printf("\t-r|--rollback roll back to the original filesystem\n");
1831 printf("\t-l|--label LABEL set filesystem label\n");
1832 printf("\t-L|--copy-label use label from converted filesystem\n");
1833 printf("\t-p|--progress show converting progress (default)\n");
1834 printf("\t-O|--features LIST comma separated list of filesystem features\n");
1835 printf("\t--no-progress show only overview, not the detailed progress\n");
1837 printf("Supported filesystems:\n");
1838 printf("\text2/3/4: %s\n", BTRFSCONVERT_EXT2 ? "yes" : "no");
1841 int main(int argc, char *argv[])
1847 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1848 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1851 int usage_error = 0;
1854 char fslabel[BTRFS_LABEL_SIZE];
1855 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1858 enum { GETOPT_VAL_NO_PROGRESS = 256 };
1859 static const struct option long_options[] = {
1860 { "no-progress", no_argument, NULL,
1861 GETOPT_VAL_NO_PROGRESS },
1862 { "no-datasum", no_argument, NULL, 'd' },
1863 { "no-inline", no_argument, NULL, 'n' },
1864 { "no-xattr", no_argument, NULL, 'i' },
1865 { "rollback", no_argument, NULL, 'r' },
1866 { "features", required_argument, NULL, 'O' },
1867 { "progress", no_argument, NULL, 'p' },
1868 { "label", required_argument, NULL, 'l' },
1869 { "copy-label", no_argument, NULL, 'L' },
1870 { "nodesize", required_argument, NULL, 'N' },
1871 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1872 { NULL, 0, NULL, 0 }
1874 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
1889 nodesize = parse_size(optarg);
1896 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
1898 "label too long, trimmed to %d bytes",
1899 BTRFS_LABEL_SIZE - 1);
1901 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
1910 char *orig = strdup(optarg);
1913 tmp = btrfs_parse_fs_features(tmp, &features);
1915 error("unrecognized filesystem feature: %s",
1921 if (features & BTRFS_FEATURE_LIST_ALL) {
1922 btrfs_list_all_fs_features(
1923 ~BTRFS_CONVERT_ALLOWED_FEATURES);
1926 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
1929 btrfs_parse_features_to_string(buf,
1930 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
1931 error("features not allowed for convert: %s",
1938 case GETOPT_VAL_NO_PROGRESS:
1941 case GETOPT_VAL_HELP:
1944 return c != GETOPT_VAL_HELP;
1948 if (check_argc_exact(argc - optind, 1)) {
1953 if (rollback && (!datacsum || noxattr || !packing)) {
1955 "Usage error: -d, -i, -n options do not apply to rollback\n");
1964 file = argv[optind];
1965 ret = check_mounted(file);
1967 error("could not check mount status: %s", strerror(-ret));
1970 error("%s is mounted", file);
1975 ret = do_rollback(file);
1977 ret = do_convert(file, datacsum, packing, noxattr, nodesize,
1978 copylabel, fslabel, progress, features);