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.
20 * Btrfs convert design:
22 * The overall design of btrfs convert is like the following:
24 * |<------------------Old fs----------------------------->|
25 * |<- used ->| |<- used ->| |<- used ->|
28 * |<---------------Btrfs fs------------------------------>|
29 * |<- Old data chunk ->|< new chunk (D/M/S)>|<- ODC ->|
30 * |<-Old-FE->| |<-Old-FE->|<- Btrfs extents ->|<-Old-FE->|
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.
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
41 * We must reserve and relocate 3 ranges for btrfs:
42 * * [0, 1M) - area never used for any data except the first
44 * * [btrfs_sb_offset(1), +64K) - 1st superblock backup copy
45 * * [btrfs_sb_offset(2), +64K) - 2nd, dtto
47 * Most work is spent handling corner cases around these reserved ranges.
49 * Detailed workflow is:
50 * 1) Scan old fs used space and calculate data chunk layout
52 * We can a map used space of old fs
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
60 * NOTE: This can be simplified if we don't need to handle backup supers
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
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
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
78 * 4) Iterate old fs to create files
79 * We just reflink file extents from old fs to newly created files on
83 #include "kerncompat.h"
87 #include <sys/types.h>
96 #include "transaction.h"
98 #include "task-utils.h"
100 #include "mkfs/common.h"
101 #include "convert/common.h"
102 #include "convert/source-fs.h"
103 #include "fsfeatures.h"
105 const struct btrfs_convert_operations ext2_convert_ops;
107 static const struct btrfs_convert_operations *convert_operations[] = {
108 #if BTRFSCONVERT_EXT2
113 static void *print_copied_inodes(void *p)
115 struct task_ctx *priv = p;
116 const char work_indicator[] = { '.', 'o', 'O', 'o' };
119 task_period_start(priv->info, 1000 /* 1s */);
122 printf("copy inodes [%c] [%10llu/%10llu]\r",
123 work_indicator[count % 4],
124 (unsigned long long)priv->cur_copy_inodes,
125 (unsigned long long)priv->max_copy_inodes);
127 task_period_wait(priv->info);
133 static int after_copied_inodes(void *p)
141 static inline int copy_inodes(struct btrfs_convert_context *cctx,
142 struct btrfs_root *root, u32 convert_flags,
145 return cctx->convert_ops->copy_inodes(cctx, root, convert_flags, p);
148 static inline void convert_close_fs(struct btrfs_convert_context *cctx)
150 cctx->convert_ops->close_fs(cctx);
153 static inline int convert_check_state(struct btrfs_convert_context *cctx)
155 return cctx->convert_ops->check_state(cctx);
158 static int csum_disk_extent(struct btrfs_trans_handle *trans,
159 struct btrfs_root *root,
160 u64 disk_bytenr, u64 num_bytes)
162 u32 blocksize = root->sectorsize;
167 buffer = malloc(blocksize);
170 for (offset = 0; offset < num_bytes; offset += blocksize) {
171 ret = read_disk_extent(root, disk_bytenr + offset,
175 ret = btrfs_csum_file_block(trans,
176 root->fs_info->csum_root,
177 disk_bytenr + num_bytes,
178 disk_bytenr + offset,
187 static int create_image_file_range(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root,
189 struct cache_tree *used,
190 struct btrfs_inode_item *inode,
191 u64 ino, u64 bytenr, u64 *ret_len,
194 struct cache_extent *cache;
195 struct btrfs_block_group_cache *bg_cache;
200 u32 datacsum = convert_flags & CONVERT_FLAG_DATACSUM;
202 if (bytenr != round_down(bytenr, root->sectorsize)) {
203 error("bytenr not sectorsize aligned: %llu",
204 (unsigned long long)bytenr);
207 if (len != round_down(len, root->sectorsize)) {
208 error("length not sectorsize aligned: %llu",
209 (unsigned long long)len);
212 len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE);
215 * Skip reserved ranges first
217 * Or we will insert a hole into current image file, and later
218 * migrate block will fail as there is already a file extent.
220 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
221 struct simple_range *reserved = &btrfs_reserved_ranges[i];
227 * |---- reserved ----|
229 * Skip to reserved range end
231 if (bytenr >= reserved->start && bytenr < range_end(reserved)) {
232 *ret_len = range_end(reserved) - bytenr;
239 * Leading part may still create a file extent
241 if (bytenr < reserved->start &&
242 bytenr + len >= range_end(reserved)) {
243 len = min_t(u64, len, reserved->start - bytenr);
248 /* Check if we are going to insert regular file extent, or hole */
249 cache = search_cache_extent(used, bytenr);
251 if (cache->start <= bytenr) {
253 * |///////Used///////|
256 * Insert one real file extent
258 len = min_t(u64, len, cache->start + cache->size -
260 disk_bytenr = bytenr;
268 len = min(len, cache->start - bytenr);
284 /* Check if the range is in a data block group */
285 bg_cache = btrfs_lookup_block_group(root->fs_info, bytenr);
288 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
291 /* The extent should never cross block group boundary */
292 len = min_t(u64, len, bg_cache->key.objectid +
293 bg_cache->key.offset - bytenr);
296 if (len != round_down(len, root->sectorsize)) {
297 error("remaining length not sectorsize aligned: %llu",
298 (unsigned long long)len);
301 ret = btrfs_record_file_extent(trans, root, ino, inode, bytenr,
307 ret = csum_disk_extent(trans, root, bytenr, len);
313 * Relocate old fs data in one reserved ranges
315 * Since all old fs data in reserved range is not covered by any chunk nor
316 * data extent, we don't need to handle any reference but add new
317 * extent/reference, which makes codes more clear
319 static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
320 struct btrfs_root *root,
321 struct cache_tree *used,
322 struct btrfs_inode_item *inode, int fd,
323 u64 ino, struct simple_range *range,
326 u64 cur_off = range->start;
327 u64 cur_len = range->len;
328 u64 hole_start = range->start;
330 struct cache_extent *cache;
331 struct btrfs_key key;
332 struct extent_buffer *eb;
336 * It's possible that there are holes in reserved range:
337 * |<---------------- Reserved range ---------------------->|
338 * |<- Old fs data ->| |<- Old fs data ->|
339 * So here we need to iterate through old fs used space and only
340 * migrate ranges that covered by old fs data.
342 while (cur_off < range_end(range)) {
343 cache = lookup_cache_extent(used, cur_off, cur_len);
346 cur_off = max(cache->start, cur_off);
347 cur_len = min(cache->start + cache->size, range_end(range)) -
349 BUG_ON(cur_len < root->sectorsize);
351 /* reserve extent for the data */
352 ret = btrfs_reserve_extent(trans, root, cur_len, 0, 0, (u64)-1,
357 eb = malloc(sizeof(*eb) + cur_len);
363 ret = pread(fd, eb->data, cur_len, cur_off);
365 ret = (ret < 0 ? ret : -EIO);
369 eb->start = key.objectid;
370 eb->len = key.offset;
373 ret = write_and_map_eb(root, eb);
378 /* Now handle extent item and file extent things */
379 ret = btrfs_record_file_extent(trans, root, ino, inode, cur_off,
380 key.objectid, key.offset);
383 /* Finally, insert csum items */
384 if (convert_flags & CONVERT_FLAG_DATACSUM)
385 ret = csum_disk_extent(trans, root, key.objectid,
388 /* Don't forget to insert hole */
389 hole_len = cur_off - hole_start;
391 ret = btrfs_record_file_extent(trans, root, ino, inode,
392 hole_start, 0, hole_len);
397 cur_off += key.offset;
398 hole_start = cur_off;
399 cur_len = range_end(range) - cur_off;
403 * |<---- reserved -------->|
404 * |<- Old fs data ->| |
407 if (range_end(range) - hole_start > 0)
408 ret = btrfs_record_file_extent(trans, root, ino, inode,
409 hole_start, 0, range_end(range) - hole_start);
414 * Relocate the used ext2 data in reserved ranges
416 static int migrate_reserved_ranges(struct btrfs_trans_handle *trans,
417 struct btrfs_root *root,
418 struct cache_tree *used,
419 struct btrfs_inode_item *inode, int fd,
420 u64 ino, u64 total_bytes, u32 convert_flags)
425 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
426 struct simple_range *range = &btrfs_reserved_ranges[i];
428 if (range->start > total_bytes)
430 ret = migrate_one_reserved_range(trans, root, used, inode, fd,
431 ino, range, convert_flags);
440 * Helper for expand and merge extent_cache for wipe_one_reserved_range() to
441 * handle wiping a range that exists in cache.
443 static int _expand_extent_cache(struct cache_tree *tree,
444 struct cache_extent *entry,
445 u64 min_stripe_size, int backward)
447 struct cache_extent *ce;
450 if (entry->size >= min_stripe_size)
452 diff = min_stripe_size - entry->size;
455 ce = prev_cache_extent(entry);
458 if (ce->start + ce->size >= entry->start - diff) {
459 /* Directly merge with previous extent */
460 ce->size = entry->start + entry->size - ce->start;
461 remove_cache_extent(tree, entry);
466 /* No overlap, normal extent */
467 if (entry->start < diff) {
468 error("cannot find space for data chunk layout");
471 entry->start -= diff;
475 ce = next_cache_extent(entry);
478 if (entry->start + entry->size + diff >= ce->start) {
479 /* Directly merge with next extent */
480 entry->size = ce->start + ce->size - entry->start;
481 remove_cache_extent(tree, ce);
491 * Remove one reserve range from given cache tree
492 * if min_stripe_size is non-zero, it will ensure for split case,
493 * all its split cache extent is no smaller than @min_strip_size / 2.
495 static int wipe_one_reserved_range(struct cache_tree *tree,
496 u64 start, u64 len, u64 min_stripe_size,
499 struct cache_extent *cache;
502 BUG_ON(ensure_size && min_stripe_size == 0);
504 * The logical here is simplified to handle special cases only
505 * So we don't need to consider merge case for ensure_size
507 BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
508 min_stripe_size / 2 < BTRFS_STRIPE_LEN));
510 /* Also, wipe range should already be aligned */
511 BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
512 start + len != round_up(start + len, BTRFS_STRIPE_LEN));
514 min_stripe_size /= 2;
516 cache = lookup_cache_extent(tree, start, len);
520 if (start <= cache->start) {
522 * |--------cache---------|
525 BUG_ON(start + len <= cache->start);
528 * The wipe size is smaller than min_stripe_size / 2,
529 * so the result length should still meet min_stripe_size
530 * And no need to do alignment
532 cache->size -= (start + len - cache->start);
533 if (cache->size == 0) {
534 remove_cache_extent(tree, cache);
539 BUG_ON(ensure_size && cache->size < min_stripe_size);
541 cache->start = start + len;
543 } else if (start > cache->start && start + len < cache->start +
546 * |-------cache-----|
549 u64 old_start = cache->start;
550 u64 old_len = cache->size;
551 u64 insert_start = start + len;
554 cache->size = start - cache->start;
555 /* Expand the leading half part if needed */
556 if (ensure_size && cache->size < min_stripe_size) {
557 ret = _expand_extent_cache(tree, cache,
563 /* And insert the new one */
564 insert_len = old_start + old_len - start - len;
565 ret = add_merge_cache_extent(tree, insert_start, insert_len);
569 /* Expand the last half part if needed */
570 if (ensure_size && insert_len < min_stripe_size) {
571 cache = lookup_cache_extent(tree, insert_start,
573 if (!cache || cache->start != insert_start ||
574 cache->size != insert_len)
576 ret = _expand_extent_cache(tree, cache,
585 * Wipe len should be small enough and no need to expand the
588 cache->size = start - cache->start;
589 BUG_ON(ensure_size && cache->size < min_stripe_size);
594 * Remove reserved ranges from given cache_tree
596 * It will remove the following ranges
598 * 2) 2nd superblock, +64K (make sure chunks are 64K aligned)
599 * 3) 3rd superblock, +64K
601 * @min_stripe must be given for safety check
602 * and if @ensure_size is given, it will ensure affected cache_extent will be
603 * larger than min_stripe_size
605 static int wipe_reserved_ranges(struct cache_tree *tree, u64 min_stripe_size,
611 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
612 struct simple_range *range = &btrfs_reserved_ranges[i];
614 ret = wipe_one_reserved_range(tree, range->start, range->len,
615 min_stripe_size, ensure_size);
622 static int calculate_available_space(struct btrfs_convert_context *cctx)
624 struct cache_tree *used = &cctx->used_space;
625 struct cache_tree *data_chunks = &cctx->data_chunks;
626 struct cache_tree *free = &cctx->free_space;
627 struct cache_extent *cache;
630 * Twice the minimal chunk size, to allow later wipe_reserved_ranges()
631 * works without need to consider overlap
633 u64 min_stripe_size = 2 * 16 * 1024 * 1024;
636 /* Calculate data_chunks */
637 for (cache = first_cache_extent(used); cache;
638 cache = next_cache_extent(cache)) {
641 if (cache->start + cache->size < cur_off)
643 if (cache->start > cur_off + min_stripe_size)
644 cur_off = cache->start;
645 cur_len = max(cache->start + cache->size - cur_off,
647 ret = add_merge_cache_extent(data_chunks, cur_off, cur_len);
653 * remove reserved ranges, so we won't ever bother relocating an old
654 * filesystem extent to other place.
656 ret = wipe_reserved_ranges(data_chunks, min_stripe_size, 1);
662 * Calculate free space
663 * Always round up the start bytenr, to avoid metadata extent corss
664 * stripe boundary, as later mkfs_convert() won't have all the extent
667 for (cache = first_cache_extent(data_chunks); cache;
668 cache = next_cache_extent(cache)) {
669 if (cache->start < cur_off)
671 if (cache->start > cur_off) {
675 len = cache->start - round_up(cur_off,
677 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
679 ret = add_merge_cache_extent(free, insert_start, len);
683 cur_off = cache->start + cache->size;
685 /* Don't forget the last range */
686 if (cctx->total_bytes > cur_off) {
687 u64 len = cctx->total_bytes - cur_off;
690 insert_start = round_up(cur_off, BTRFS_STRIPE_LEN);
692 ret = add_merge_cache_extent(free, insert_start, len);
697 /* Remove reserved bytes */
698 ret = wipe_reserved_ranges(free, min_stripe_size, 0);
704 * Read used space, and since we have the used space,
705 * calcuate data_chunks and free for later mkfs
707 static int convert_read_used_space(struct btrfs_convert_context *cctx)
711 ret = cctx->convert_ops->read_used_space(cctx);
715 ret = calculate_available_space(cctx);
720 * Create the fs image file of old filesystem.
722 * This is completely fs independent as we have cctx->used, only
723 * need to create file extents pointing to all the positions.
725 static int create_image(struct btrfs_root *root,
726 struct btrfs_mkfs_config *cfg,
727 struct btrfs_convert_context *cctx, int fd,
728 u64 size, char *name, u32 convert_flags)
730 struct btrfs_inode_item buf;
731 struct btrfs_trans_handle *trans;
732 struct btrfs_path path;
733 struct btrfs_key key;
734 struct cache_extent *cache;
735 struct cache_tree used_tmp;
738 u64 flags = BTRFS_INODE_READONLY;
741 if (!(convert_flags & CONVERT_FLAG_DATACSUM))
742 flags |= BTRFS_INODE_NODATASUM;
744 trans = btrfs_start_transaction(root, 1);
748 cache_tree_init(&used_tmp);
749 btrfs_init_path(&path);
751 ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
755 ret = btrfs_new_inode(trans, root, ino, 0400 | S_IFREG);
758 ret = btrfs_change_inode_flags(trans, root, ino, flags);
761 ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
762 strlen(name), BTRFS_FT_REG_FILE, NULL, 1);
767 key.type = BTRFS_INODE_ITEM_KEY;
770 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
772 ret = (ret > 0 ? -ENOENT : ret);
775 read_extent_buffer(path.nodes[0], &buf,
776 btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
778 btrfs_release_path(&path);
781 * Create a new used space cache, which doesn't contain the reserved
784 for (cache = first_cache_extent(&cctx->used_space); cache;
785 cache = next_cache_extent(cache)) {
786 ret = add_cache_extent(&used_tmp, cache->start, cache->size);
790 ret = wipe_reserved_ranges(&used_tmp, 0, 0);
795 * Start from 1M, as 0~1M is reserved, and create_image_file_range()
796 * can't handle bytenr 0(will consider it as a hole)
800 u64 len = size - cur;
802 ret = create_image_file_range(trans, root, &used_tmp,
803 &buf, ino, cur, &len,
809 /* Handle the reserved ranges */
810 ret = migrate_reserved_ranges(trans, root, &cctx->used_space, &buf, fd,
811 ino, cfg->num_bytes, convert_flags);
814 key.type = BTRFS_INODE_ITEM_KEY;
816 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
818 ret = (ret > 0 ? -ENOENT : ret);
821 btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
822 write_extent_buffer(path.nodes[0], &buf,
823 btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
826 free_extent_cache_tree(&used_tmp);
827 btrfs_release_path(&path);
828 btrfs_commit_transaction(trans, root);
832 static struct btrfs_root* link_subvol(struct btrfs_root *root,
833 const char *base, u64 root_objectid)
835 struct btrfs_trans_handle *trans;
836 struct btrfs_fs_info *fs_info = root->fs_info;
837 struct btrfs_root *tree_root = fs_info->tree_root;
838 struct btrfs_root *new_root = NULL;
839 struct btrfs_path path;
840 struct btrfs_inode_item *inode_item;
841 struct extent_buffer *leaf;
842 struct btrfs_key key;
843 u64 dirid = btrfs_root_dirid(&root->root_item);
845 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
851 if (len == 0 || len > BTRFS_NAME_LEN)
854 btrfs_init_path(&path);
855 key.objectid = dirid;
856 key.type = BTRFS_DIR_INDEX_KEY;
857 key.offset = (u64)-1;
859 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
861 error("search for DIR_INDEX dirid %llu failed: %d",
862 (unsigned long long)dirid, ret);
866 if (path.slots[0] > 0) {
868 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
869 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
870 index = key.offset + 1;
872 btrfs_release_path(&path);
874 trans = btrfs_start_transaction(root, 1);
876 error("unable to start transaction");
880 key.objectid = dirid;
882 key.type = BTRFS_INODE_ITEM_KEY;
884 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
886 error("search for INODE_ITEM %llu failed: %d",
887 (unsigned long long)dirid, ret);
890 leaf = path.nodes[0];
891 inode_item = btrfs_item_ptr(leaf, path.slots[0],
892 struct btrfs_inode_item);
894 key.objectid = root_objectid;
895 key.offset = (u64)-1;
896 key.type = BTRFS_ROOT_ITEM_KEY;
898 memcpy(buf, base, len);
899 for (i = 0; i < 1024; i++) {
900 ret = btrfs_insert_dir_item(trans, root, buf, len,
901 dirid, &key, BTRFS_FT_DIR, index);
904 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
905 if (len < 1 || len > BTRFS_NAME_LEN) {
913 btrfs_set_inode_size(leaf, inode_item, len * 2 +
914 btrfs_inode_size(leaf, inode_item));
915 btrfs_mark_buffer_dirty(leaf);
916 btrfs_release_path(&path);
918 /* add the backref first */
919 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
920 BTRFS_ROOT_BACKREF_KEY,
921 root->root_key.objectid,
922 dirid, index, buf, len);
924 error("unable to add root backref for %llu: %d",
925 root->root_key.objectid, ret);
929 /* now add the forward ref */
930 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
931 BTRFS_ROOT_REF_KEY, root_objectid,
932 dirid, index, buf, len);
934 error("unable to add root ref for %llu: %d",
935 root->root_key.objectid, ret);
939 ret = btrfs_commit_transaction(trans, root);
941 error("transaction commit failed: %d", ret);
945 new_root = btrfs_read_fs_root(fs_info, &key);
946 if (IS_ERR(new_root)) {
947 error("unable to fs read root: %lu", PTR_ERR(new_root));
951 btrfs_init_path(&path);
955 static int create_subvol(struct btrfs_trans_handle *trans,
956 struct btrfs_root *root, u64 root_objectid)
958 struct extent_buffer *tmp;
959 struct btrfs_root *new_root;
960 struct btrfs_key key;
961 struct btrfs_root_item root_item;
964 ret = btrfs_copy_root(trans, root, root->node, &tmp,
969 memcpy(&root_item, &root->root_item, sizeof(root_item));
970 btrfs_set_root_bytenr(&root_item, tmp->start);
971 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
972 btrfs_set_root_generation(&root_item, trans->transid);
973 free_extent_buffer(tmp);
975 key.objectid = root_objectid;
976 key.type = BTRFS_ROOT_ITEM_KEY;
977 key.offset = trans->transid;
978 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
981 key.offset = (u64)-1;
982 new_root = btrfs_read_fs_root(root->fs_info, &key);
983 if (!new_root || IS_ERR(new_root)) {
984 error("unable to fs read root: %lu", PTR_ERR(new_root));
985 return PTR_ERR(new_root);
988 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
994 * New make_btrfs() has handle system and meta chunks quite well.
995 * So only need to add remaining data chunks.
997 static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
998 struct btrfs_fs_info *fs_info,
999 struct btrfs_mkfs_config *cfg,
1000 struct btrfs_convert_context *cctx)
1002 struct btrfs_root *extent_root = fs_info->extent_root;
1003 struct cache_tree *data_chunks = &cctx->data_chunks;
1004 struct cache_extent *cache;
1009 * Don't create data chunk over 10% of the convert device
1010 * And for single chunk, don't create chunk larger than 1G.
1012 max_chunk_size = cfg->num_bytes / 10;
1013 max_chunk_size = min((u64)(1024 * 1024 * 1024), max_chunk_size);
1014 max_chunk_size = round_down(max_chunk_size, extent_root->sectorsize);
1016 for (cache = first_cache_extent(data_chunks); cache;
1017 cache = next_cache_extent(cache)) {
1018 u64 cur = cache->start;
1020 while (cur < cache->start + cache->size) {
1022 u64 cur_backup = cur;
1024 len = min(max_chunk_size,
1025 cache->start + cache->size - cur);
1026 ret = btrfs_alloc_data_chunk(trans, extent_root,
1028 BTRFS_BLOCK_GROUP_DATA, 1);
1031 ret = btrfs_make_block_group(trans, extent_root, 0,
1032 BTRFS_BLOCK_GROUP_DATA,
1033 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1044 * Init the temp btrfs to a operational status.
1046 * It will fix the extent usage accounting(XXX: Do we really need?) and
1047 * insert needed data chunks, to ensure all old fs data extents are covered
1048 * by DATA chunks, preventing wrong chunks are allocated.
1050 * And also create convert image subvolume and relocation tree.
1051 * (XXX: Not need again?)
1052 * But the convert image subvolume is *NOT* linked to fs tree yet.
1054 static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
1055 struct btrfs_convert_context *cctx, u32 convert_flags)
1057 struct btrfs_key location;
1058 struct btrfs_trans_handle *trans;
1059 struct btrfs_fs_info *fs_info = root->fs_info;
1063 * Don't alloc any metadata/system chunk, as we don't want
1064 * any meta/sys chunk allcated before all data chunks are inserted.
1065 * Or we screw up the chunk layout just like the old implement.
1067 fs_info->avoid_sys_chunk_alloc = 1;
1068 fs_info->avoid_meta_chunk_alloc = 1;
1069 trans = btrfs_start_transaction(root, 1);
1071 error("unable to start transaction");
1075 ret = btrfs_fix_block_accounting(trans, root);
1078 ret = make_convert_data_block_groups(trans, fs_info, cfg, cctx);
1081 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1082 BTRFS_ROOT_TREE_DIR_OBJECTID);
1085 memcpy(&location, &root->root_key, sizeof(location));
1086 location.offset = (u64)-1;
1087 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1088 btrfs_super_root_dir(fs_info->super_copy),
1089 &location, BTRFS_FT_DIR, 0);
1092 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1094 btrfs_super_root_dir(fs_info->super_copy), 0);
1097 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1098 BTRFS_FIRST_FREE_OBJECTID);
1100 /* subvol for fs image file */
1101 ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
1103 error("failed to create subvolume image root: %d", ret);
1106 /* subvol for data relocation tree */
1107 ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1109 error("failed to create DATA_RELOC root: %d", ret);
1113 ret = btrfs_commit_transaction(trans, root);
1114 fs_info->avoid_sys_chunk_alloc = 0;
1115 fs_info->avoid_meta_chunk_alloc = 0;
1121 * Migrate super block to its default position and zero 0 ~ 16k
1123 static int migrate_super_block(int fd, u64 old_bytenr)
1126 struct extent_buffer *buf;
1127 struct btrfs_super_block *super;
1131 buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
1135 buf->len = BTRFS_SUPER_INFO_SIZE;
1136 ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, old_bytenr);
1137 if (ret != BTRFS_SUPER_INFO_SIZE)
1140 super = (struct btrfs_super_block *)buf->data;
1141 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1142 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1144 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1145 ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
1146 BTRFS_SUPER_INFO_OFFSET);
1147 if (ret != BTRFS_SUPER_INFO_SIZE)
1154 memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
1155 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1156 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1157 if (len > BTRFS_SUPER_INFO_SIZE)
1158 len = BTRFS_SUPER_INFO_SIZE;
1159 ret = pwrite(fd, buf->data, len, bytenr);
1161 fprintf(stderr, "unable to zero fill device\n");
1175 static int convert_open_fs(const char *devname,
1176 struct btrfs_convert_context *cctx)
1180 for (i = 0; i < ARRAY_SIZE(convert_operations); i++) {
1181 int ret = convert_operations[i]->open_fs(cctx, devname);
1184 cctx->convert_ops = convert_operations[i];
1189 error("no file system found to convert");
1193 static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
1194 const char *fslabel, int progress, u64 features)
1200 struct btrfs_root *root;
1201 struct btrfs_root *image_root;
1202 struct btrfs_convert_context cctx;
1203 struct btrfs_key key;
1204 char subvol_name[SOURCE_FS_NAME_LEN + 8];
1205 struct task_ctx ctx;
1206 char features_buf[64];
1207 struct btrfs_mkfs_config mkfs_cfg;
1209 init_convert_context(&cctx);
1210 ret = convert_open_fs(devname, &cctx);
1213 ret = convert_check_state(&cctx);
1216 "source filesystem is not clean, running filesystem check is recommended");
1217 ret = convert_read_used_space(&cctx);
1221 blocksize = cctx.blocksize;
1222 total_bytes = (u64)blocksize * (u64)cctx.block_count;
1223 if (blocksize < 4096) {
1224 error("block size is too small: %u < 4096", blocksize);
1227 if (btrfs_check_nodesize(nodesize, blocksize, features))
1229 fd = open(devname, O_RDWR);
1231 error("unable to open %s: %s", devname, strerror(errno));
1234 btrfs_parse_features_to_string(features_buf, features);
1235 if (features == BTRFS_MKFS_DEFAULT_FEATURES)
1236 strcat(features_buf, " (default)");
1238 printf("create btrfs filesystem:\n");
1239 printf("\tblocksize: %u\n", blocksize);
1240 printf("\tnodesize: %u\n", nodesize);
1241 printf("\tfeatures: %s\n", features_buf);
1243 memset(&mkfs_cfg, 0, sizeof(mkfs_cfg));
1244 mkfs_cfg.label = cctx.volume_name;
1245 mkfs_cfg.num_bytes = total_bytes;
1246 mkfs_cfg.nodesize = nodesize;
1247 mkfs_cfg.sectorsize = blocksize;
1248 mkfs_cfg.stripesize = blocksize;
1249 mkfs_cfg.features = features;
1251 ret = make_convert_btrfs(fd, &mkfs_cfg, &cctx);
1253 error("unable to create initial ctree: %s", strerror(-ret));
1257 root = open_ctree_fd(fd, devname, mkfs_cfg.super_bytenr,
1258 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1260 error("unable to open ctree");
1263 ret = init_btrfs(&mkfs_cfg, root, &cctx, convert_flags);
1265 error("unable to setup the root tree: %d", ret);
1269 printf("creating %s image file\n", cctx.convert_ops->name);
1270 snprintf(subvol_name, sizeof(subvol_name), "%s_saved",
1271 cctx.convert_ops->name);
1272 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1273 key.offset = (u64)-1;
1274 key.type = BTRFS_ROOT_ITEM_KEY;
1275 image_root = btrfs_read_fs_root(root->fs_info, &key);
1277 error("unable to create image subvolume");
1280 ret = create_image(image_root, &mkfs_cfg, &cctx, fd,
1281 mkfs_cfg.num_bytes, "image",
1284 error("failed to create %s/image: %d", subvol_name, ret);
1288 printf("creating btrfs metadata");
1289 ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count);
1290 ctx.cur_copy_inodes = 0;
1293 ctx.info = task_init(print_copied_inodes, after_copied_inodes,
1295 task_start(ctx.info);
1297 ret = copy_inodes(&cctx, root, convert_flags, &ctx);
1299 error("error during copy_inodes %d", ret);
1303 task_stop(ctx.info);
1304 task_deinit(ctx.info);
1307 image_root = link_subvol(root, subvol_name, CONV_IMAGE_SUBVOL_OBJECTID);
1309 error("unable to link subvolume %s", subvol_name);
1313 memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
1314 if (convert_flags & CONVERT_FLAG_COPY_LABEL) {
1315 __strncpy_null(root->fs_info->super_copy->label,
1316 cctx.volume_name, BTRFS_LABEL_SIZE - 1);
1317 printf("copy label '%s'\n", root->fs_info->super_copy->label);
1318 } else if (convert_flags & CONVERT_FLAG_SET_LABEL) {
1319 strcpy(root->fs_info->super_copy->label, fslabel);
1320 printf("set label to '%s'\n", fslabel);
1323 ret = close_ctree(root);
1325 error("close_ctree failed: %d", ret);
1328 convert_close_fs(&cctx);
1329 clean_convert_context(&cctx);
1332 * If this step succeed, we get a mountable btrfs. Otherwise
1333 * the source fs is left unchanged.
1335 ret = migrate_super_block(fd, mkfs_cfg.super_bytenr);
1337 error("unable to migrate super block: %d", ret);
1341 root = open_ctree_fd(fd, devname, 0,
1342 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1344 error("unable to open ctree for finalization");
1347 root->fs_info->finalize_on_close = 1;
1351 printf("conversion complete");
1354 clean_convert_context(&cctx);
1358 "an error occurred during conversion, filesystem is partially created but not finalized and not mountable");
1363 * Read out data of convert image which is in btrfs reserved ranges so we can
1364 * use them to overwrite the ranges during rollback.
1366 static int read_reserved_ranges(struct btrfs_root *root, u64 ino,
1367 u64 total_bytes, char *reserved_ranges[])
1372 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
1373 struct simple_range *range = &btrfs_reserved_ranges[i];
1375 if (range->start + range->len >= total_bytes)
1377 ret = btrfs_read_file(root, ino, range->start, range->len,
1378 reserved_ranges[i]);
1379 if (ret < range->len) {
1381 "failed to read data of convert image, offset=%llu len=%llu ret=%d",
1382 range->start, range->len, ret);
1392 static bool is_subset_of_reserved_ranges(u64 start, u64 len)
1397 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
1398 struct simple_range *range = &btrfs_reserved_ranges[i];
1400 if (start >= range->start && start + len <= range_end(range)) {
1408 static bool is_chunk_direct_mapped(struct btrfs_fs_info *fs_info, u64 start)
1410 struct cache_extent *ce;
1411 struct map_lookup *map;
1414 ce = search_cache_extent(&fs_info->mapping_tree.cache_tree, start);
1417 if (ce->start > start || ce->start + ce->size < start)
1420 map = container_of(ce, struct map_lookup, ce);
1422 /* Not SINGLE chunk */
1423 if (map->num_stripes != 1)
1426 /* Chunk's logical doesn't match with phisical, not 1:1 mapped */
1427 if (map->ce.start != map->stripes[0].physical)
1435 * Iterate all file extents of the convert image.
1437 * All file extents except ones in btrfs_reserved_ranges must be mapped 1:1
1438 * on disk. (Means thier file_offset must match their on disk bytenr)
1440 * File extents in reserved ranges can be relocated to other place, and in
1441 * that case we will read them out for later use.
1443 static int check_convert_image(struct btrfs_root *image_root, u64 ino,
1444 u64 total_size, char *reserved_ranges[])
1446 struct btrfs_key key;
1447 struct btrfs_path path;
1448 struct btrfs_fs_info *fs_info = image_root->fs_info;
1449 u64 checked_bytes = 0;
1454 key.type = BTRFS_EXTENT_DATA_KEY;
1456 btrfs_init_path(&path);
1457 ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
1459 * It's possible that some fs doesn't store any (including sb)
1460 * data into 0~1M range, and NO_HOLES is enabled.
1462 * So we only need to check if ret < 0
1465 error("failed to iterate file extents at offset 0: %s",
1467 btrfs_release_path(&path);
1471 /* Loop from the first file extents */
1473 struct btrfs_file_extent_item *fi;
1474 struct extent_buffer *leaf = path.nodes[0];
1478 int slot = path.slots[0];
1480 if (slot >= btrfs_header_nritems(leaf))
1482 btrfs_item_key_to_cpu(leaf, &key, slot);
1485 * Iteration is done, exit normally, we have extra check out of
1488 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
1492 file_offset = key.offset;
1493 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1494 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) {
1497 "ino %llu offset %llu doesn't have a regular file extent",
1501 if (btrfs_file_extent_compression(leaf, fi) ||
1502 btrfs_file_extent_encryption(leaf, fi) ||
1503 btrfs_file_extent_other_encoding(leaf, fi)) {
1506 "ino %llu offset %llu doesn't have a plain file extent",
1511 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1512 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1514 checked_bytes += ram_bytes;
1516 if (disk_bytenr == 0)
1520 * Most file extents must be 1:1 mapped, which means 2 things:
1521 * 1) File extent file offset == disk_bytenr
1522 * 2) That data chunk's logical == chunk's physical
1524 * So file extent's file offset == physical position on disk.
1526 * And after rolling back btrfs reserved range, other part
1527 * remains what old fs used to be.
1529 if (file_offset != disk_bytenr ||
1530 !is_chunk_direct_mapped(fs_info, disk_bytenr)) {
1532 * Only file extent in btrfs reserved ranges are
1533 * allowed to be non-1:1 mapped
1535 if (!is_subset_of_reserved_ranges(file_offset,
1539 "ino %llu offset %llu file extent should not be relocated",
1545 ret = btrfs_next_item(image_root, &path);
1552 btrfs_release_path(&path);
1554 * For HOLES mode (without NO_HOLES), we must ensure file extents
1555 * cover the whole range of the image
1557 if (!ret && !btrfs_fs_incompat(fs_info, NO_HOLES)) {
1558 if (checked_bytes != total_size) {
1560 error("inode %llu has some file extents not checked",
1565 /* So far so good, read old data located in btrfs reserved ranges */
1566 ret = read_reserved_ranges(image_root, ino, total_size,
1572 * btrfs rollback is just reverted convert:
1573 * |<---------------Btrfs fs------------------------------>|
1574 * |<- Old data chunk ->|< new chunk (D/M/S)>|<- ODC ->|
1575 * |<-Old-FE->| |<-Old-FE->|<- Btrfs extents ->|<-Old-FE->|
1578 * |<------------------Old fs----------------------------->|
1579 * |<- used ->| |<- used ->| |<- used ->|
1581 * However things are much easier than convert, we don't really need to
1582 * do the complex space calculation, but only to handle btrfs reserved space
1584 * |<---------------------------Btrfs fs----------------------------->|
1585 * | RSV 1 | | Old | | RSV 2 | | Old | | RSV 3 |
1586 * | 0~1M | | Fs | | SB2 + 64K | | Fs | | SB3 + 64K |
1588 * On the other hande, the converted fs image in btrfs is a completely
1591 * |<-----------------Converted fs image in btrfs-------------------->|
1592 * | RSV 1 | | Old | | RSV 2 | | Old | | RSV 3 |
1593 * | Relocated | | Fs | | Relocated | | Fs | | Relocated |
1595 * Used space in fs image should be at the same physical position on disk.
1596 * We only need to recover the data in reserved ranges, so the whole
1599 * The idea to rollback is also straightforward, we just "read" out the data
1600 * of reserved ranges, and write them back to there they should be.
1601 * Then the old fs is back.
1603 static int do_rollback(const char *devname)
1605 struct btrfs_root *root;
1606 struct btrfs_root *image_root;
1607 struct btrfs_fs_info *fs_info;
1608 struct btrfs_key key;
1609 struct btrfs_path path;
1610 struct btrfs_dir_item *dir;
1611 struct btrfs_inode_item *inode_item;
1612 char *image_name = "image";
1613 char *reserved_ranges[ARRAY_SIZE(btrfs_reserved_ranges)] = { NULL };
1622 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
1623 struct simple_range *range = &btrfs_reserved_ranges[i];
1625 reserved_ranges[i] = calloc(1, range->len);
1626 if (!reserved_ranges[i]) {
1631 fd = open(devname, O_RDWR);
1633 error("unable to open %s: %s", devname, strerror(errno));
1637 fsize = lseek(fd, 0, SEEK_END);
1638 root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
1640 error("unable to open ctree");
1644 fs_info = root->fs_info;
1647 * Search root backref first, or after subvolume deletion (orphan),
1648 * we can still rollback the image.
1650 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1651 key.type = BTRFS_ROOT_BACKREF_KEY;
1652 key.offset = BTRFS_FS_TREE_OBJECTID;
1653 btrfs_init_path(&path);
1654 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, &path, 0, 0);
1655 btrfs_release_path(&path);
1657 error("unable to find ext2 image subvolume, is it deleted?");
1660 } else if (ret < 0) {
1661 error("failed to find ext2 image subvolume: %s",
1666 /* Search convert subvolume */
1667 key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
1668 key.type = BTRFS_ROOT_ITEM_KEY;
1669 key.offset = (u64)-1;
1670 image_root = btrfs_read_fs_root(fs_info, &key);
1671 if (IS_ERR(image_root)) {
1672 ret = PTR_ERR(image_root);
1673 error("failed to open convert image subvolume: %s",
1678 /* Search the image file */
1679 root_dir = btrfs_root_dirid(&image_root->root_item);
1680 dir = btrfs_lookup_dir_item(NULL, image_root, &path, root_dir,
1681 image_name, strlen(image_name), 0);
1683 if (!dir || IS_ERR(dir)) {
1684 btrfs_release_path(&path);
1689 error("failed to locate file %s: %s", image_name,
1693 btrfs_dir_item_key_to_cpu(path.nodes[0], dir, &key);
1694 btrfs_release_path(&path);
1696 /* Get total size of the original image */
1699 ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
1702 btrfs_release_path(&path);
1703 error("unable to find inode %llu: %s", ino, strerror(-ret));
1706 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
1707 struct btrfs_inode_item);
1708 total_bytes = btrfs_inode_size(path.nodes[0], inode_item);
1709 btrfs_release_path(&path);
1711 /* Check if we can rollback the image */
1712 ret = check_convert_image(image_root, ino, total_bytes, reserved_ranges);
1714 error("old fs image can't be rolled back");
1718 btrfs_release_path(&path);
1719 close_ctree_fs_info(fs_info);
1724 * Everything is OK, just write back old fs data into btrfs reserved
1727 * Here, we starts from the backup blocks first, so if something goes
1728 * wrong, the fs is still mountable
1731 for (i = ARRAY_SIZE(btrfs_reserved_ranges) - 1; i >= 0; i--) {
1733 struct simple_range *range = &btrfs_reserved_ranges[i];
1735 if (range_end(range) >= fsize)
1738 real_size = min(range_end(range), fsize) - range->start;
1739 ret = pwrite(fd, reserved_ranges[i], real_size, range->start);
1740 if (ret < real_size) {
1745 error("failed to recover range [%llu, %llu): %s",
1746 range->start, real_size, strerror(-ret));
1753 for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++)
1754 free(reserved_ranges[i]);
1756 error("rollback failed");
1758 printf("rollback succeeded\n");
1762 static void print_usage(void)
1764 printf("usage: btrfs-convert [options] device\n");
1765 printf("options:\n");
1766 printf("\t-d|--no-datasum disable data checksum, sets NODATASUM\n");
1767 printf("\t-i|--no-xattr ignore xattrs and ACLs\n");
1768 printf("\t-n|--no-inline disable inlining of small files to metadata\n");
1769 printf("\t-N|--nodesize SIZE set filesystem metadata nodesize\n");
1770 printf("\t-r|--rollback roll back to the original filesystem\n");
1771 printf("\t-l|--label LABEL set filesystem label\n");
1772 printf("\t-L|--copy-label use label from converted filesystem\n");
1773 printf("\t-p|--progress show converting progress (default)\n");
1774 printf("\t-O|--features LIST comma separated list of filesystem features\n");
1775 printf("\t--no-progress show only overview, not the detailed progress\n");
1777 printf("Supported filesystems:\n");
1778 printf("\text2/3/4: %s\n", BTRFSCONVERT_EXT2 ? "yes" : "no");
1781 int main(int argc, char *argv[])
1787 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1788 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1791 int usage_error = 0;
1794 char fslabel[BTRFS_LABEL_SIZE];
1795 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1798 enum { GETOPT_VAL_NO_PROGRESS = 256 };
1799 static const struct option long_options[] = {
1800 { "no-progress", no_argument, NULL,
1801 GETOPT_VAL_NO_PROGRESS },
1802 { "no-datasum", no_argument, NULL, 'd' },
1803 { "no-inline", no_argument, NULL, 'n' },
1804 { "no-xattr", no_argument, NULL, 'i' },
1805 { "rollback", no_argument, NULL, 'r' },
1806 { "features", required_argument, NULL, 'O' },
1807 { "progress", no_argument, NULL, 'p' },
1808 { "label", required_argument, NULL, 'l' },
1809 { "copy-label", no_argument, NULL, 'L' },
1810 { "nodesize", required_argument, NULL, 'N' },
1811 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1812 { NULL, 0, NULL, 0 }
1814 int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
1829 nodesize = parse_size(optarg);
1835 copylabel = CONVERT_FLAG_SET_LABEL;
1836 if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
1838 "label too long, trimmed to %d bytes",
1839 BTRFS_LABEL_SIZE - 1);
1841 __strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
1844 copylabel = CONVERT_FLAG_COPY_LABEL;
1850 char *orig = strdup(optarg);
1853 tmp = btrfs_parse_fs_features(tmp, &features);
1855 error("unrecognized filesystem feature: %s",
1861 if (features & BTRFS_FEATURE_LIST_ALL) {
1862 btrfs_list_all_fs_features(
1863 ~BTRFS_CONVERT_ALLOWED_FEATURES);
1866 if (features & ~BTRFS_CONVERT_ALLOWED_FEATURES) {
1869 btrfs_parse_features_to_string(buf,
1870 features & ~BTRFS_CONVERT_ALLOWED_FEATURES);
1871 error("features not allowed for convert: %s",
1878 case GETOPT_VAL_NO_PROGRESS:
1881 case GETOPT_VAL_HELP:
1884 return c != GETOPT_VAL_HELP;
1888 if (check_argc_exact(argc - optind, 1)) {
1893 if (rollback && (!datacsum || noxattr || !packing)) {
1895 "Usage error: -d, -i, -n options do not apply to rollback\n");
1904 file = argv[optind];
1905 ret = check_mounted(file);
1907 error("could not check mount status: %s", strerror(-ret));
1910 error("%s is mounted", file);
1915 ret = do_rollback(file);
1919 cf |= datacsum ? CONVERT_FLAG_DATACSUM : 0;
1920 cf |= packing ? CONVERT_FLAG_INLINE_DATA : 0;
1921 cf |= noxattr ? 0 : CONVERT_FLAG_XATTR;
1923 ret = do_convert(file, cf, nodesize, fslabel, progress, features);