btrfs-progs: add btrfs_clear_free_space_tree() from the kernel
[btrfs-progs-unstable/devel.git] / btrfstune.c
blobe8e3d00ac47df46c32c9fe424666c3d2aabd2d90
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"
36 static char *device;
37 static int force = 0;
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;
43 u64 super_flags;
44 int ret;
46 disk_super = root->fs_info->super_copy;
47 super_flags = btrfs_super_flags(disk_super);
48 if (set_flag) {
49 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
50 if (force)
51 return 0;
52 else
53 warning("seeding flag is already set on %s",
54 device);
55 return 1;
57 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
58 } else {
59 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
60 warning("seeding flag is not set on %s", device);
61 return 1;
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);
71 return ret;
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;
78 u64 super_flags;
79 int ret;
81 disk_super = root->fs_info->super_copy;
82 super_flags = btrfs_super_incompat_flags(disk_super);
83 super_flags |= flags;
84 trans = btrfs_start_transaction(root, 1);
85 btrfs_set_super_incompat_flags(disk_super, super_flags);
86 ret = btrfs_commit_transaction(trans, root);
88 return ret;
91 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
93 struct btrfs_fs_info *fs_info = root->fs_info;
94 int same_fsid = 1;
95 int same_chunk_tree_uuid = 1;
96 int ret;
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),
103 BTRFS_UUID_SIZE);
104 if (same_fsid && same_chunk_tree_uuid)
105 return 0;
106 if (!same_fsid)
107 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
108 BTRFS_FSID_SIZE);
109 if (!same_chunk_tree_uuid)
110 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
111 btrfs_header_chunk_tree_uuid(eb),
112 BTRFS_UUID_SIZE);
113 ret = write_tree_block(NULL, root, eb);
115 return ret;
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};
123 int ret = 0;
125 btrfs_init_path(&path);
127 * Here we don't use transaction as it will takes a lot of reserve
128 * space, and that will make a near-full btrfs unable to change uuid
130 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
131 if (ret < 0)
132 goto out;
134 while (1) {
135 struct btrfs_extent_item *ei;
136 struct extent_buffer *eb;
137 u64 flags;
138 u64 bytenr;
140 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
141 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
142 key.type != BTRFS_METADATA_ITEM_KEY)
143 goto next;
144 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
145 struct btrfs_extent_item);
146 flags = btrfs_extent_flags(path.nodes[0], ei);
147 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
148 goto next;
150 bytenr = key.objectid;
151 eb = read_tree_block(root, bytenr, root->nodesize, 0);
152 if (IS_ERR(eb)) {
153 error("failed to read tree block: %llu", bytenr);
154 ret = PTR_ERR(eb);
155 goto out;
157 ret = change_header_uuid(root, eb);
158 free_extent_buffer(eb);
159 if (ret < 0) {
160 error("failed to change uuid of tree block: %llu",
161 bytenr);
162 goto out;
164 next:
165 ret = btrfs_next_item(root, &path);
166 if (ret < 0)
167 goto out;
168 if (ret > 0) {
169 ret = 0;
170 goto out;
174 out:
175 btrfs_release_path(&path);
176 return ret;
179 static int change_device_uuid(struct btrfs_root *root, struct extent_buffer *eb,
180 int slot)
182 struct btrfs_fs_info *fs_info = root->fs_info;
183 struct btrfs_dev_item *di;
184 int ret = 0;
186 di = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
187 if (!memcmp_extent_buffer(eb, fs_info->new_fsid,
188 (unsigned long)btrfs_device_fsid(di),
189 BTRFS_FSID_SIZE))
190 return ret;
192 write_extent_buffer(eb, fs_info->new_fsid,
193 (unsigned long)btrfs_device_fsid(di),
194 BTRFS_FSID_SIZE);
195 ret = write_tree_block(NULL, root, eb);
197 return ret;
200 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
202 struct btrfs_root *root = fs_info->chunk_root;
203 struct btrfs_path path;
204 struct btrfs_key key = {0, 0, 0};
205 int ret = 0;
207 btrfs_init_path(&path);
208 /* No transaction again */
209 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
210 if (ret < 0)
211 goto out;
213 while (1) {
214 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
215 if (key.type != BTRFS_DEV_ITEM_KEY ||
216 key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
217 goto next;
218 ret = change_device_uuid(root, path.nodes[0], path.slots[0]);
219 if (ret < 0)
220 goto out;
221 next:
222 ret = btrfs_next_item(root, &path);
223 if (ret < 0)
224 goto out;
225 if (ret > 0) {
226 ret = 0;
227 goto out;
230 out:
231 btrfs_release_path(&path);
232 return ret;
235 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
237 struct btrfs_root *tree_root = fs_info->tree_root;
238 u64 flags = btrfs_super_flags(fs_info->super_copy);
239 int ret = 0;
241 flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
242 btrfs_set_super_flags(fs_info->super_copy, flags);
244 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid, BTRFS_FSID_SIZE);
245 ret = write_all_supers(tree_root);
246 if (ret < 0)
247 return ret;
249 /* also restore new chunk_tree_id into tree_root for restore */
250 write_extent_buffer(tree_root->node, fs_info->new_chunk_tree_uuid,
251 btrfs_header_chunk_tree_uuid(tree_root->node),
252 BTRFS_UUID_SIZE);
253 return write_tree_block(NULL, tree_root, tree_root->node);
256 static int change_fsid_done(struct btrfs_fs_info *fs_info)
258 u64 flags = btrfs_super_flags(fs_info->super_copy);
260 flags &= ~BTRFS_SUPER_FLAG_CHANGING_FSID;
261 btrfs_set_super_flags(fs_info->super_copy, flags);
263 return write_all_supers(fs_info->tree_root);
267 * Return 0 for no unfinished fsid change.
268 * Return >0 for unfinished fsid change, and restore unfinished fsid/
269 * chunk_tree_id into fsid_ret/chunk_id_ret.
271 static int check_unfinished_fsid_change(struct btrfs_fs_info *fs_info,
272 uuid_t fsid_ret, uuid_t chunk_id_ret)
274 struct btrfs_root *tree_root = fs_info->tree_root;
275 u64 flags = btrfs_super_flags(fs_info->super_copy);
277 if (flags & BTRFS_SUPER_FLAG_CHANGING_FSID) {
278 memcpy(fsid_ret, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
279 read_extent_buffer(tree_root->node, chunk_id_ret,
280 btrfs_header_chunk_tree_uuid(tree_root->node),
281 BTRFS_UUID_SIZE);
282 return 1;
284 return 0;
288 * Change fsid of a given fs.
290 * If new_fsid_str is not given, use a random generated UUID.
291 * Caller should check new_fsid_str is valid
293 static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
295 uuid_t new_fsid;
296 uuid_t new_chunk_id;
297 uuid_t old_fsid;
298 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
299 int ret = 0;
301 if (check_unfinished_fsid_change(fs_info, new_fsid, new_chunk_id)) {
302 if (new_fsid_str) {
303 uuid_t tmp;
305 uuid_parse(new_fsid_str, tmp);
306 if (memcmp(tmp, new_fsid, BTRFS_FSID_SIZE)) {
307 error(
308 "new fsid %s is not the same with unfinished fsid change",
309 new_fsid_str);
310 return -EINVAL;
313 } else {
314 if (new_fsid_str)
315 uuid_parse(new_fsid_str, new_fsid);
316 else
317 uuid_generate(new_fsid);
319 uuid_generate(new_chunk_id);
321 fs_info->new_fsid = new_fsid;
322 fs_info->new_chunk_tree_uuid = new_chunk_id;
324 memcpy(old_fsid, (const char*)fs_info->fsid, BTRFS_UUID_SIZE);
325 uuid_unparse(old_fsid, uuid_buf);
326 printf("Current fsid: %s\n", uuid_buf);
328 uuid_unparse(new_fsid, uuid_buf);
329 printf("New fsid: %s\n", uuid_buf);
330 /* Now we can begin fsid change */
331 printf("Set superblock flag CHANGING_FSID\n");
332 ret = change_fsid_prepare(fs_info);
333 if (ret < 0)
334 goto out;
336 /* Change extents first */
337 printf("Change fsid in extents\n");
338 ret = change_extents_uuid(fs_info);
339 if (ret < 0) {
340 error("failed to change UUID of metadata: %d", ret);
341 goto out;
344 /* Then devices */
345 printf("Change fsid on devices\n");
346 ret = change_devices_uuid(fs_info);
347 if (ret < 0) {
348 error("failed to change UUID of devices: %d", ret);
349 goto out;
352 /* Last, change fsid in super */
353 memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
354 BTRFS_FSID_SIZE);
355 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
356 BTRFS_FSID_SIZE);
357 ret = write_all_supers(fs_info->tree_root);
358 if (ret < 0)
359 goto out;
361 /* Now fsid change is done */
362 printf("Clear superblock flag CHANGING_FSID\n");
363 ret = change_fsid_done(fs_info);
364 fs_info->new_fsid = NULL;
365 fs_info->new_chunk_tree_uuid = NULL;
366 printf("Fsid change finished\n");
367 out:
368 return ret;
371 static void print_usage(void)
373 printf("usage: btrfstune [options] device\n");
374 printf("\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
375 printf("\t-r \t\tenable extended inode refs\n");
376 printf("\t-x \t\tenable skinny metadata extent refs\n");
377 printf("\t-n \t\tenable no-holes feature (more efficient sparse file representation)\n");
378 printf("\t-f \t\tforce to do dangerous operation, make sure that you are aware of the dangers\n");
379 printf("\t-u \t\tchange fsid, use a random one\n");
380 printf("\t-U UUID\t\tchange fsid to UUID\n");
383 int main(int argc, char *argv[])
385 struct btrfs_root *root;
386 unsigned ctree_flags = OPEN_CTREE_WRITES;
387 int success = 0;
388 int total = 0;
389 int seeding_flag = 0;
390 u64 seeding_value = 0;
391 int random_fsid = 0;
392 char *new_fsid_str = NULL;
393 int ret;
394 u64 super_flags = 0;
396 while(1) {
397 static const struct option long_options[] = {
398 { "help", no_argument, NULL, GETOPT_VAL_HELP},
399 { NULL, 0, NULL, 0 }
401 int c = getopt_long(argc, argv, "S:rxfuU:n", long_options, NULL);
403 if (c < 0)
404 break;
405 switch(c) {
406 case 'S':
407 seeding_flag = 1;
408 seeding_value = arg_strtou64(optarg);
409 break;
410 case 'r':
411 super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
412 break;
413 case 'x':
414 super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
415 break;
416 case 'n':
417 super_flags |= BTRFS_FEATURE_INCOMPAT_NO_HOLES;
418 break;
419 case 'f':
420 force = 1;
421 break;
422 case 'U':
423 ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
424 new_fsid_str = optarg;
425 break;
426 case 'u':
427 ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
428 random_fsid = 1;
429 break;
430 case GETOPT_VAL_HELP:
431 default:
432 print_usage();
433 return c != GETOPT_VAL_HELP;
437 set_argv0(argv);
438 device = argv[optind];
439 if (check_argc_exact(argc - optind, 1)) {
440 print_usage();
441 return 1;
444 if (random_fsid && new_fsid_str) {
445 error("random fsid can't be used with specified fsid");
446 return 1;
448 if (!super_flags && !seeding_flag && !(random_fsid || new_fsid_str)) {
449 error("at least one option should be specified");
450 print_usage();
451 return 1;
454 if (new_fsid_str) {
455 uuid_t tmp;
457 ret = uuid_parse(new_fsid_str, tmp);
458 if (ret < 0) {
459 error("could not parse UUID: %s", new_fsid_str);
460 return 1;
462 if (!test_uuid_unique(new_fsid_str)) {
463 error("fsid %s is not unique", new_fsid_str);
464 return 1;
468 ret = check_mounted(device);
469 if (ret < 0) {
470 error("could not check mount status of %s: %s", device,
471 strerror(-ret));
472 return 1;
473 } else if (ret) {
474 error("%s is mounted", device);
475 return 1;
478 root = open_ctree(device, 0, ctree_flags);
480 if (!root) {
481 error("open ctree failed");
482 return 1;
485 if (seeding_flag) {
486 if (!seeding_value && !force) {
487 warning(
488 "this is dangerous, clearing the seeding flag may cause the derived device not to be mountable!");
489 ret = ask_user("We are going to clear the seeding flag, are you sure?");
490 if (!ret) {
491 fprintf(stderr, "Clear seeding flag canceled\n");
492 ret = 1;
493 goto out;
497 ret = update_seeding_flag(root, seeding_value);
498 if (!ret)
499 success++;
500 total++;
503 if (super_flags) {
504 ret = set_super_incompat_flags(root, super_flags);
505 if (!ret)
506 success++;
507 total++;
510 if (random_fsid || new_fsid_str) {
511 if (!force) {
512 warning(
513 "it's highly recommended to run 'btrfs check' before this operation");
514 warning(
515 "also canceling running UUID change progress may cause corruption");
516 ret = ask_user("We are going to change UUID, are your sure?");
517 if (!ret) {
518 fprintf(stderr, "UUID change canceled\n");
519 ret = 1;
520 goto out;
523 ret = change_uuid(root->fs_info, new_fsid_str);
524 if (!ret)
525 success++;
526 total++;
529 if (success == total) {
530 ret = 0;
531 } else {
532 root->fs_info->readonly = 1;
533 ret = 1;
534 error("btrfstune failed");
536 out:
537 close_ctree(root);
538 btrfs_close_all_devices();
540 return ret;