2 * Copyright (C) 2008 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.
21 #include <sys/types.h>
26 #include <uuid/uuid.h>
29 #include "kerncompat.h"
32 #include "transaction.h"
39 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
41 struct btrfs_trans_handle *trans;
42 struct btrfs_super_block *disk_super;
46 disk_super = root->fs_info->super_copy;
47 super_flags = btrfs_super_flags(disk_super);
49 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
53 warning("seeding flag is already set on %s",
57 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
59 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
60 warning("seeding flag is not set on %s", device);
63 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
64 warning("seeding flag cleared on %s", device);
67 trans = btrfs_start_transaction(root, 1);
68 btrfs_set_super_flags(disk_super, super_flags);
69 ret = btrfs_commit_transaction(trans, root);
74 static int set_super_incompat_flags(struct btrfs_root *root, u64 flags)
76 struct btrfs_trans_handle *trans;
77 struct btrfs_super_block *disk_super;
81 disk_super = root->fs_info->super_copy;
82 super_flags = btrfs_super_incompat_flags(disk_super);
84 trans = btrfs_start_transaction(root, 1);
85 btrfs_set_super_incompat_flags(disk_super, super_flags);
86 ret = btrfs_commit_transaction(trans, root);
91 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
93 struct btrfs_fs_info *fs_info = root->fs_info;
95 int same_chunk_tree_uuid = 1;
98 same_fsid = !memcmp_extent_buffer(eb, fs_info->new_fsid,
99 btrfs_header_fsid(), BTRFS_FSID_SIZE);
100 same_chunk_tree_uuid =
101 !memcmp_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
102 btrfs_header_chunk_tree_uuid(eb),
104 if (same_fsid && same_chunk_tree_uuid)
107 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
109 if (!same_chunk_tree_uuid)
110 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
111 btrfs_header_chunk_tree_uuid(eb),
113 ret = write_tree_block(NULL, root, eb);
118 static int change_extents_uuid(struct btrfs_fs_info *fs_info)
120 struct btrfs_root *root = fs_info->extent_root;
121 struct btrfs_path *path;
122 struct btrfs_key key = {0, 0, 0};
125 path = btrfs_alloc_path();
130 * Here we don't use transaction as it will takes a lot of reserve
131 * space, and that will make a near-full btrfs unable to change uuid
133 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
138 struct btrfs_extent_item *ei;
139 struct extent_buffer *eb;
143 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
144 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
145 key.type != BTRFS_METADATA_ITEM_KEY)
147 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
148 struct btrfs_extent_item);
149 flags = btrfs_extent_flags(path->nodes[0], ei);
150 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
153 bytenr = key.objectid;
154 eb = read_tree_block(root, bytenr, root->nodesize, 0);
156 error("failed to read tree block: %llu", bytenr);
160 ret = change_header_uuid(root, eb);
161 free_extent_buffer(eb);
163 error("failed to change uuid of tree block: %llu",
168 ret = btrfs_next_item(root, path);
178 btrfs_free_path(path);
182 static int change_device_uuid(struct btrfs_root *root, struct extent_buffer *eb,
185 struct btrfs_fs_info *fs_info = root->fs_info;
186 struct btrfs_dev_item *di;
189 di = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
190 if (!memcmp_extent_buffer(eb, fs_info->new_fsid,
191 (unsigned long)btrfs_device_fsid(di),
195 write_extent_buffer(eb, fs_info->new_fsid,
196 (unsigned long)btrfs_device_fsid(di),
198 ret = write_tree_block(NULL, root, eb);
203 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
205 struct btrfs_root *root = fs_info->chunk_root;
206 struct btrfs_path *path;
207 struct btrfs_key key = {0, 0, 0};
210 path = btrfs_alloc_path();
213 /* No transaction again */
214 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
219 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
220 if (key.type != BTRFS_DEV_ITEM_KEY ||
221 key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
223 ret = change_device_uuid(root, path->nodes[0], path->slots[0]);
227 ret = btrfs_next_item(root, path);
236 btrfs_free_path(path);
240 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
242 struct btrfs_root *tree_root = fs_info->tree_root;
243 u64 flags = btrfs_super_flags(fs_info->super_copy);
246 flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
247 btrfs_set_super_flags(fs_info->super_copy, flags);
249 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid, BTRFS_FSID_SIZE);
250 ret = write_all_supers(tree_root);
254 /* also restore new chunk_tree_id into tree_root for restore */
255 write_extent_buffer(tree_root->node, fs_info->new_chunk_tree_uuid,
256 btrfs_header_chunk_tree_uuid(tree_root->node),
258 return write_tree_block(NULL, tree_root, tree_root->node);
261 static int change_fsid_done(struct btrfs_fs_info *fs_info)
263 u64 flags = btrfs_super_flags(fs_info->super_copy);
265 flags &= ~BTRFS_SUPER_FLAG_CHANGING_FSID;
266 btrfs_set_super_flags(fs_info->super_copy, flags);
268 return write_all_supers(fs_info->tree_root);
272 * Return 0 for no unfinished fsid change.
273 * Return >0 for unfinished fsid change, and restore unfinished fsid/
274 * chunk_tree_id into fsid_ret/chunk_id_ret.
276 static int check_unfinished_fsid_change(struct btrfs_fs_info *fs_info,
277 uuid_t fsid_ret, uuid_t chunk_id_ret)
279 struct btrfs_root *tree_root = fs_info->tree_root;
280 u64 flags = btrfs_super_flags(fs_info->super_copy);
282 if (flags & BTRFS_SUPER_FLAG_CHANGING_FSID) {
283 memcpy(fsid_ret, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
284 read_extent_buffer(tree_root->node, chunk_id_ret,
285 btrfs_header_chunk_tree_uuid(tree_root->node),
293 * Change fsid of a given fs.
295 * If new_fsid_str is not given, use a random generated UUID.
296 * Caller should check new_fsid_str is valid
298 static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
303 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
306 if (check_unfinished_fsid_change(fs_info, new_fsid, new_chunk_id)) {
310 uuid_parse(new_fsid_str, tmp);
311 if (memcmp(tmp, new_fsid, BTRFS_FSID_SIZE)) {
313 "new fsid %s is not the same with unfinished fsid change",
320 uuid_parse(new_fsid_str, new_fsid);
322 uuid_generate(new_fsid);
324 uuid_generate(new_chunk_id);
326 fs_info->new_fsid = new_fsid;
327 fs_info->new_chunk_tree_uuid = new_chunk_id;
329 memcpy(old_fsid, (const char*)fs_info->fsid, BTRFS_UUID_SIZE);
330 uuid_unparse(old_fsid, uuid_buf);
331 printf("Current fsid: %s\n", uuid_buf);
333 uuid_unparse(new_fsid, uuid_buf);
334 printf("New fsid: %s\n", uuid_buf);
335 /* Now we can begin fsid change */
336 printf("Set superblock flag CHANGING_FSID\n");
337 ret = change_fsid_prepare(fs_info);
341 /* Change extents first */
342 printf("Change fsid in extents\n");
343 ret = change_extents_uuid(fs_info);
345 error("failed to change UUID of metadata: %d", ret);
350 printf("Change fsid on devices\n");
351 ret = change_devices_uuid(fs_info);
353 error("failed to change UUID of devices: %d", ret);
357 /* Last, change fsid in super */
358 memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
360 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
362 ret = write_all_supers(fs_info->tree_root);
366 /* Now fsid change is done */
367 printf("Clear superblock flag CHANGING_FSID\n");
368 ret = change_fsid_done(fs_info);
369 fs_info->new_fsid = NULL;
370 fs_info->new_chunk_tree_uuid = NULL;
371 printf("Fsid change finished\n");
376 static void print_usage(void)
378 printf("usage: btrfstune [options] device\n");
379 printf("\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
380 printf("\t-r \t\tenable extended inode refs\n");
381 printf("\t-x \t\tenable skinny metadata extent refs\n");
382 printf("\t-n \t\tenable no-holes feature (more efficient sparse file representation)\n");
383 printf("\t-f \t\tforce to do dangerous operation, make sure that you are aware of the dangers\n");
384 printf("\t-u \t\tchange fsid, use a random one\n");
385 printf("\t-U UUID\t\tchange fsid to UUID\n");
388 int main(int argc, char *argv[])
390 struct btrfs_root *root;
391 unsigned ctree_flags = OPEN_CTREE_WRITES;
394 int seeding_flag = 0;
395 u64 seeding_value = 0;
397 char *new_fsid_str = NULL;
402 static const struct option long_options[] = {
403 { "help", no_argument, NULL, GETOPT_VAL_HELP},
406 int c = getopt_long(argc, argv, "S:rxfuU:n", long_options, NULL);
413 seeding_value = arg_strtou64(optarg);
416 super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
419 super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
422 super_flags |= BTRFS_FEATURE_INCOMPAT_NO_HOLES;
428 ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
429 new_fsid_str = optarg;
432 ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
435 case GETOPT_VAL_HELP:
438 return c != GETOPT_VAL_HELP;
443 device = argv[optind];
444 if (check_argc_exact(argc - optind, 1)) {
449 if (random_fsid && new_fsid_str) {
450 error("random fsid can't be used with specified fsid");
453 if (!super_flags && !seeding_flag && !(random_fsid || new_fsid_str)) {
454 error("at least one option should be specified");
462 ret = uuid_parse(new_fsid_str, tmp);
464 error("could not parse UUID: %s", new_fsid_str);
467 if (!test_uuid_unique(new_fsid_str)) {
468 error("fsid %s is not unique", new_fsid_str);
473 ret = check_mounted(device);
475 error("could not check mount status of %s: %s", device,
479 error("%s is mounted", device);
483 root = open_ctree(device, 0, ctree_flags);
486 error("open ctree failed");
491 if (!seeding_value && !force) {
493 "this is dangerous, clearing the seeding flag may cause the derived device not to be mountable!");
494 ret = ask_user("We are going to clear the seeding flag, are you sure?");
496 fprintf(stderr, "Clear seeding flag canceled\n");
502 ret = update_seeding_flag(root, seeding_value);
509 ret = set_super_incompat_flags(root, super_flags);
515 if (random_fsid || new_fsid_str) {
518 "it's highly recommended to run 'btrfs check' before this operation");
520 "also canceling running UUID change progress may cause corruption");
521 ret = ask_user("We are going to change UUID, are your sure?");
523 fprintf(stderr, "UUID change canceled\n");
528 ret = change_uuid(root->fs_info, new_fsid_str);
534 if (success == total) {
537 root->fs_info->readonly = 1;
539 error("btrfstune failed");
543 btrfs_close_all_devices();