Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / btrfstune.c
blob889b931c55d8345f155bcf2b28a3add7f6c71b42
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <dirent.h>
26 #include <uuid/uuid.h>
27 #include <getopt.h>
29 #include "kerncompat.h"
30 #include "ctree.h"
31 #include "disk-io.h"
32 #include "transaction.h"
33 #include "utils.h"
34 #include "volumes.h"
35 #include "help.h"
37 static char *device;
38 static int force = 0;
40 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
42 struct btrfs_trans_handle *trans;
43 struct btrfs_super_block *disk_super;
44 u64 super_flags;
45 int ret;
47 disk_super = root->fs_info->super_copy;
48 super_flags = btrfs_super_flags(disk_super);
49 if (set_flag) {
50 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
51 if (force)
52 return 0;
53 else
54 warning("seeding flag is already set on %s",
55 device);
56 return 1;
58 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
59 } else {
60 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
61 warning("seeding flag is not set on %s", device);
62 return 1;
64 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
65 warning("seeding flag cleared on %s", device);
68 trans = btrfs_start_transaction(root, 1);
69 BUG_ON(IS_ERR(trans));
70 btrfs_set_super_flags(disk_super, super_flags);
71 ret = btrfs_commit_transaction(trans, root);
73 return ret;
76 static int set_super_incompat_flags(struct btrfs_root *root, u64 flags)
78 struct btrfs_trans_handle *trans;
79 struct btrfs_super_block *disk_super;
80 u64 super_flags;
81 int ret;
83 disk_super = root->fs_info->super_copy;
84 super_flags = btrfs_super_incompat_flags(disk_super);
85 super_flags |= flags;
86 trans = btrfs_start_transaction(root, 1);
87 BUG_ON(IS_ERR(trans));
88 btrfs_set_super_incompat_flags(disk_super, super_flags);
89 ret = btrfs_commit_transaction(trans, root);
91 return ret;
94 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
96 struct btrfs_fs_info *fs_info = root->fs_info;
97 int same_fsid = 1;
98 int same_chunk_tree_uuid = 1;
99 int ret;
101 same_fsid = !memcmp_extent_buffer(eb, fs_info->new_fsid,
102 btrfs_header_fsid(), BTRFS_FSID_SIZE);
103 same_chunk_tree_uuid =
104 !memcmp_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
105 btrfs_header_chunk_tree_uuid(eb),
106 BTRFS_UUID_SIZE);
107 if (same_fsid && same_chunk_tree_uuid)
108 return 0;
109 if (!same_fsid)
110 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
111 BTRFS_FSID_SIZE);
112 if (!same_chunk_tree_uuid)
113 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
114 btrfs_header_chunk_tree_uuid(eb),
115 BTRFS_UUID_SIZE);
116 ret = write_tree_block(NULL, fs_info, eb);
118 return ret;
121 static int change_extents_uuid(struct btrfs_fs_info *fs_info)
123 struct btrfs_root *root = fs_info->extent_root;
124 struct btrfs_path path;
125 struct btrfs_key key = {0, 0, 0};
126 int ret = 0;
128 btrfs_init_path(&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);
134 if (ret < 0)
135 goto out;
137 while (1) {
138 struct btrfs_extent_item *ei;
139 struct extent_buffer *eb;
140 u64 flags;
141 u64 bytenr;
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)
146 goto next;
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))
151 goto next;
153 bytenr = key.objectid;
154 eb = read_tree_block(fs_info, bytenr, 0);
155 if (IS_ERR(eb)) {
156 error("failed to read tree block: %llu", bytenr);
157 ret = PTR_ERR(eb);
158 goto out;
160 ret = change_header_uuid(root, eb);
161 free_extent_buffer(eb);
162 if (ret < 0) {
163 error("failed to change uuid of tree block: %llu",
164 bytenr);
165 goto out;
167 next:
168 ret = btrfs_next_item(root, &path);
169 if (ret < 0)
170 goto out;
171 if (ret > 0) {
172 ret = 0;
173 goto out;
177 out:
178 btrfs_release_path(&path);
179 return ret;
182 static int change_device_uuid(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
183 int slot)
185 struct btrfs_dev_item *di;
186 int ret = 0;
188 di = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
189 if (!memcmp_extent_buffer(eb, fs_info->new_fsid,
190 (unsigned long)btrfs_device_fsid(di),
191 BTRFS_FSID_SIZE))
192 return ret;
194 write_extent_buffer(eb, fs_info->new_fsid,
195 (unsigned long)btrfs_device_fsid(di),
196 BTRFS_FSID_SIZE);
197 ret = write_tree_block(NULL, fs_info, eb);
199 return ret;
202 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
204 struct btrfs_root *root = fs_info->chunk_root;
205 struct btrfs_path path;
206 struct btrfs_key key = {0, 0, 0};
207 int ret = 0;
209 btrfs_init_path(&path);
210 /* No transaction again */
211 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
212 if (ret < 0)
213 goto out;
215 while (1) {
216 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
217 if (key.type != BTRFS_DEV_ITEM_KEY ||
218 key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
219 goto next;
220 ret = change_device_uuid(fs_info, path.nodes[0], path.slots[0]);
221 if (ret < 0)
222 goto out;
223 next:
224 ret = btrfs_next_item(root, &path);
225 if (ret < 0)
226 goto out;
227 if (ret > 0) {
228 ret = 0;
229 goto out;
232 out:
233 btrfs_release_path(&path);
234 return ret;
237 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
239 struct btrfs_root *tree_root = fs_info->tree_root;
240 u64 flags = btrfs_super_flags(fs_info->super_copy);
241 int ret = 0;
243 flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
244 btrfs_set_super_flags(fs_info->super_copy, flags);
246 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid, BTRFS_FSID_SIZE);
247 ret = write_all_supers(fs_info);
248 if (ret < 0)
249 return ret;
251 /* also restore new chunk_tree_id into tree_root for restore */
252 write_extent_buffer(tree_root->node, fs_info->new_chunk_tree_uuid,
253 btrfs_header_chunk_tree_uuid(tree_root->node),
254 BTRFS_UUID_SIZE);
255 return write_tree_block(NULL, fs_info, tree_root->node);
258 static int change_fsid_done(struct btrfs_fs_info *fs_info)
260 u64 flags = btrfs_super_flags(fs_info->super_copy);
262 flags &= ~BTRFS_SUPER_FLAG_CHANGING_FSID;
263 btrfs_set_super_flags(fs_info->super_copy, flags);
265 return write_all_supers(fs_info);
269 * Return 0 for no unfinished fsid change.
270 * Return >0 for unfinished fsid change, and restore unfinished fsid/
271 * chunk_tree_id into fsid_ret/chunk_id_ret.
273 static int check_unfinished_fsid_change(struct btrfs_fs_info *fs_info,
274 uuid_t fsid_ret, uuid_t chunk_id_ret)
276 struct btrfs_root *tree_root = fs_info->tree_root;
277 u64 flags = btrfs_super_flags(fs_info->super_copy);
279 if (flags & BTRFS_SUPER_FLAG_CHANGING_FSID) {
280 memcpy(fsid_ret, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
281 read_extent_buffer(tree_root->node, chunk_id_ret,
282 btrfs_header_chunk_tree_uuid(tree_root->node),
283 BTRFS_UUID_SIZE);
284 return 1;
286 return 0;
290 * Change fsid of a given fs.
292 * If new_fsid_str is not given, use a random generated UUID.
293 * Caller should check new_fsid_str is valid
295 static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
297 uuid_t new_fsid;
298 uuid_t new_chunk_id;
299 uuid_t old_fsid;
300 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
301 int ret = 0;
303 if (check_unfinished_fsid_change(fs_info, new_fsid, new_chunk_id)) {
304 if (new_fsid_str) {
305 uuid_t tmp;
307 uuid_parse(new_fsid_str, tmp);
308 if (memcmp(tmp, new_fsid, BTRFS_FSID_SIZE)) {
309 error(
310 "new fsid %s is not the same with unfinished fsid change",
311 new_fsid_str);
312 return -EINVAL;
315 } else {
316 if (new_fsid_str)
317 uuid_parse(new_fsid_str, new_fsid);
318 else
319 uuid_generate(new_fsid);
321 uuid_generate(new_chunk_id);
323 fs_info->new_fsid = new_fsid;
324 fs_info->new_chunk_tree_uuid = new_chunk_id;
326 memcpy(old_fsid, (const char*)fs_info->fsid, BTRFS_UUID_SIZE);
327 uuid_unparse(old_fsid, uuid_buf);
328 printf("Current fsid: %s\n", uuid_buf);
330 uuid_unparse(new_fsid, uuid_buf);
331 printf("New fsid: %s\n", uuid_buf);
332 /* Now we can begin fsid change */
333 printf("Set superblock flag CHANGING_FSID\n");
334 ret = change_fsid_prepare(fs_info);
335 if (ret < 0)
336 goto out;
338 /* Change extents first */
339 printf("Change fsid in extents\n");
340 ret = change_extents_uuid(fs_info);
341 if (ret < 0) {
342 error("failed to change UUID of metadata: %d", ret);
343 goto out;
346 /* Then devices */
347 printf("Change fsid on devices\n");
348 ret = change_devices_uuid(fs_info);
349 if (ret < 0) {
350 error("failed to change UUID of devices: %d", ret);
351 goto out;
354 /* Last, change fsid in super */
355 memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
356 BTRFS_FSID_SIZE);
357 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
358 BTRFS_FSID_SIZE);
359 ret = write_all_supers(fs_info);
360 if (ret < 0)
361 goto out;
363 /* Now fsid change is done */
364 printf("Clear superblock flag CHANGING_FSID\n");
365 ret = change_fsid_done(fs_info);
366 fs_info->new_fsid = NULL;
367 fs_info->new_chunk_tree_uuid = NULL;
368 printf("Fsid change finished\n");
369 out:
370 return ret;
373 static void print_usage(void)
375 printf("usage: btrfstune [options] device\n");
376 printf("\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
377 printf("\t-r \t\tenable extended inode refs\n");
378 printf("\t-x \t\tenable skinny metadata extent refs\n");
379 printf("\t-n \t\tenable no-holes feature (more efficient sparse file representation)\n");
380 printf("\t-f \t\tforce to do dangerous operation, make sure that you are aware of the dangers\n");
381 printf("\t-u \t\tchange fsid, use a random one\n");
382 printf("\t-U UUID\t\tchange fsid to UUID\n");
385 int main(int argc, char *argv[])
387 struct btrfs_root *root;
388 unsigned ctree_flags = OPEN_CTREE_WRITES;
389 int success = 0;
390 int total = 0;
391 int seeding_flag = 0;
392 u64 seeding_value = 0;
393 int random_fsid = 0;
394 char *new_fsid_str = NULL;
395 int ret;
396 u64 super_flags = 0;
397 int fd = -1;
399 while(1) {
400 static const struct option long_options[] = {
401 { "help", no_argument, NULL, GETOPT_VAL_HELP},
402 { NULL, 0, NULL, 0 }
404 int c = getopt_long(argc, argv, "S:rxfuU:n", long_options, NULL);
406 if (c < 0)
407 break;
408 switch(c) {
409 case 'S':
410 seeding_flag = 1;
411 seeding_value = arg_strtou64(optarg);
412 break;
413 case 'r':
414 super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
415 break;
416 case 'x':
417 super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
418 break;
419 case 'n':
420 super_flags |= BTRFS_FEATURE_INCOMPAT_NO_HOLES;
421 break;
422 case 'f':
423 force = 1;
424 break;
425 case 'U':
426 ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
427 new_fsid_str = optarg;
428 break;
429 case 'u':
430 ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
431 random_fsid = 1;
432 break;
433 case GETOPT_VAL_HELP:
434 default:
435 print_usage();
436 return c != GETOPT_VAL_HELP;
440 set_argv0(argv);
441 device = argv[optind];
442 if (check_argc_exact(argc - optind, 1)) {
443 print_usage();
444 return 1;
447 if (random_fsid && new_fsid_str) {
448 error("random fsid can't be used with specified fsid");
449 return 1;
451 if (!super_flags && !seeding_flag && !(random_fsid || new_fsid_str)) {
452 error("at least one option should be specified");
453 print_usage();
454 return 1;
457 if (new_fsid_str) {
458 uuid_t tmp;
460 ret = uuid_parse(new_fsid_str, tmp);
461 if (ret < 0) {
462 error("could not parse UUID: %s", new_fsid_str);
463 return 1;
465 if (!test_uuid_unique(new_fsid_str)) {
466 error("fsid %s is not unique", new_fsid_str);
467 return 1;
471 fd = open(device, O_RDWR);
472 if (fd < 0) {
473 error("mount check: cannot open %s: %m", device);
474 return 1;
477 ret = check_mounted_where(fd, device, NULL, 0, NULL, SBREAD_DEFAULT);
478 if (ret < 0) {
479 error("could not check mount status of %s: %s", device,
480 strerror(-ret));
481 close(fd);
482 return 1;
483 } else if (ret) {
484 error("%s is mounted", device);
485 close(fd);
486 return 1;
489 root = open_ctree_fd(fd, device, 0, ctree_flags);
491 if (!root) {
492 error("open ctree failed");
493 return 1;
496 if (seeding_flag) {
497 if (!seeding_value && !force) {
498 warning(
499 "this is dangerous, clearing the seeding flag may cause the derived device not to be mountable!");
500 ret = ask_user("We are going to clear the seeding flag, are you sure?");
501 if (!ret) {
502 fprintf(stderr, "Clear seeding flag canceled\n");
503 ret = 1;
504 goto out;
508 ret = update_seeding_flag(root, seeding_value);
509 if (!ret)
510 success++;
511 total++;
514 if (super_flags) {
515 ret = set_super_incompat_flags(root, super_flags);
516 if (!ret)
517 success++;
518 total++;
521 if (random_fsid || new_fsid_str) {
522 if (!force) {
523 warning(
524 "it's highly recommended to run 'btrfs check' before this operation");
525 warning(
526 "also canceling running UUID change progress may cause corruption");
527 ret = ask_user("We are going to change UUID, are your sure?");
528 if (!ret) {
529 fprintf(stderr, "UUID change canceled\n");
530 ret = 1;
531 goto out;
534 ret = change_uuid(root->fs_info, new_fsid_str);
535 if (!ret)
536 success++;
537 total++;
540 if (success == total) {
541 ret = 0;
542 } else {
543 root->fs_info->readonly = 1;
544 ret = 1;
545 error("btrfstune failed");
547 out:
548 close_ctree(root);
549 btrfs_close_all_devices();
551 return ret;