3324bbef2ab5f9f20c6d8dc119db5298f128b700
[platform/upstream/btrfs-progs.git] / btrfstune.c
1 /*
2  * Copyright (C) 2008 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
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
28 #include "kerncompat.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "transaction.h"
32 #include "utils.h"
33 #include "volumes.h"
34
35 static char *device;
36 static int force = 0;
37
38 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
39 {
40         struct btrfs_trans_handle *trans;
41         struct btrfs_super_block *disk_super;
42         u64 super_flags;
43
44         disk_super = root->fs_info->super_copy;
45         super_flags = btrfs_super_flags(disk_super);
46         if (set_flag) {
47                 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
48                         if (force)
49                                 return 0;
50                         else
51                                 fprintf(stderr, "seeding flag is already set on %s\n", device);
52                         return 1;
53                 }
54                 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
55         } else {
56                 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
57                         fprintf(stderr, "seeding flag is not set on %s\n",
58                                 device);
59                         return 1;
60                 }
61                 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
62                 fprintf(stderr, "Warning: Seeding flag cleared.\n");
63         }
64
65         trans = btrfs_start_transaction(root, 1);
66         btrfs_set_super_flags(disk_super, super_flags);
67         btrfs_commit_transaction(trans, root);
68
69         return 0;
70 }
71
72 static int enable_extrefs_flag(struct btrfs_root *root)
73 {
74         struct btrfs_trans_handle *trans;
75         struct btrfs_super_block *disk_super;
76         u64 super_flags;
77
78         disk_super = root->fs_info->super_copy;
79         super_flags = btrfs_super_incompat_flags(disk_super);
80         super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
81         trans = btrfs_start_transaction(root, 1);
82         btrfs_set_super_incompat_flags(disk_super, super_flags);
83         btrfs_commit_transaction(trans, root);
84
85         return 0;
86 }
87
88 static int enable_skinny_metadata(struct btrfs_root *root)
89 {
90         struct btrfs_trans_handle *trans;
91         struct btrfs_super_block *disk_super;
92         u64 super_flags;
93
94         disk_super = root->fs_info->super_copy;
95         super_flags = btrfs_super_incompat_flags(disk_super);
96         super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
97         trans = btrfs_start_transaction(root, 1);
98         btrfs_set_super_incompat_flags(disk_super, super_flags);
99         btrfs_commit_transaction(trans, root);
100
101         return 0;
102 }
103
104 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
105 {
106         struct btrfs_fs_info *fs_info = root->fs_info;
107         int same_fsid = 1;
108         int same_chunk_tree_uuid = 1;
109         int ret;
110
111         /* Check for whether we need to change fs/chunk id */
112         if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
113                 return 0;
114         if (fs_info->new_fsid)
115                 same_fsid = !memcmp_extent_buffer(eb, fs_info->new_fsid,
116                                           btrfs_header_fsid(), BTRFS_FSID_SIZE);
117         if (fs_info->new_chunk_tree_uuid)
118                 same_chunk_tree_uuid =
119                         !memcmp_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
120                                               btrfs_header_chunk_tree_uuid(eb),
121                                               BTRFS_UUID_SIZE);
122         if (same_fsid && same_chunk_tree_uuid)
123                 return 0;
124         if (!same_fsid)
125                 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
126                                     BTRFS_FSID_SIZE);
127         if (!same_chunk_tree_uuid)
128                 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
129                                     btrfs_header_chunk_tree_uuid(eb),
130                                     BTRFS_UUID_SIZE);
131         ret = write_tree_block(NULL, root, eb);
132
133         return ret;
134 }
135
136 static int change_extents_uuid(struct btrfs_fs_info *fs_info)
137 {
138         struct btrfs_root *root = fs_info->extent_root;
139         struct btrfs_path *path;
140         struct btrfs_key key = {0, 0, 0};
141         int ret = 0;
142
143         if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
144                 return 0;
145
146         path = btrfs_alloc_path();
147         if (!path)
148                 return -ENOMEM;
149
150         /*
151          * Here we don't use transaction as it will takes a lot of reserve
152          * space, and that will make a near-full btrfs unable to change uuid
153          */
154         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
155         if (ret < 0)
156                 goto out;
157
158         while (1) {
159                 struct btrfs_extent_item *ei;
160                 struct extent_buffer *eb;
161                 u64 flags;
162                 u64 bytenr;
163
164                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
165                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
166                     key.type != BTRFS_METADATA_ITEM_KEY)
167                         goto next;
168                 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
169                                     struct btrfs_extent_item);
170                 flags = btrfs_extent_flags(path->nodes[0], ei);
171                 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
172                         goto next;
173
174                 bytenr = key.objectid;
175                 eb = read_tree_block(root, bytenr, root->nodesize, 0);
176                 if (IS_ERR(eb)) {
177                         fprintf(stderr, "Failed to read tree block: %llu\n",
178                                 bytenr);
179                         ret = PTR_ERR(eb);
180                         goto out;
181                 }
182                 ret = change_header_uuid(root, eb);
183                 free_extent_buffer(eb);
184                 if (ret < 0) {
185                         fprintf(stderr, "Failed to change uuid of tree block: %llu\n",
186                                 bytenr);
187                         goto out;
188                 }
189 next:
190                 ret = btrfs_next_item(root, path);
191                 if (ret < 0)
192                         goto out;
193                 if (ret > 0) {
194                         ret = 0;
195                         goto out;
196                 }
197         }
198
199 out:
200         btrfs_free_path(path);
201         return ret;
202 }
203
204 static int change_device_uuid(struct btrfs_root *root, struct extent_buffer *eb,
205                               int slot)
206 {
207         struct btrfs_fs_info *fs_info = root->fs_info;
208         struct btrfs_dev_item *di;
209         int ret = 0;
210
211         di = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
212         if (fs_info->new_fsid) {
213                 if (!memcmp_extent_buffer(eb, fs_info->new_fsid,
214                                           (unsigned long)btrfs_device_fsid(di),
215                                           BTRFS_FSID_SIZE))
216                         return ret;
217                 write_extent_buffer(eb, fs_info->new_fsid,
218                                     (unsigned long)btrfs_device_fsid(di),
219                                     BTRFS_FSID_SIZE);
220                 ret = write_tree_block(NULL, root, eb);
221         }
222         return ret;
223 }
224
225 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
226 {
227         struct btrfs_root *root = fs_info->chunk_root;
228         struct btrfs_path *path;
229         struct btrfs_key key = {0, 0, 0};
230         int ret = 0;
231
232         /*
233          * Unlike change_extents_uuid, we only need to change fsid in dev_item
234          */
235         if (!fs_info->new_fsid)
236                 return 0;
237
238         path = btrfs_alloc_path();
239         if (!path)
240                 return -ENOMEM;
241         /* No transaction again */
242         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
243         if (ret < 0)
244                 goto out;
245
246         while (1) {
247                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
248                 if (key.type != BTRFS_DEV_ITEM_KEY ||
249                     key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
250                         goto next;
251                 ret = change_device_uuid(root, path->nodes[0], path->slots[0]);
252                 if (ret < 0)
253                         goto out;
254 next:
255                 ret = btrfs_next_item(root, path);
256                 if (ret < 0)
257                         goto out;
258                 if (ret > 0) {
259                         ret = 0;
260                         goto out;
261                 }
262         }
263 out:
264         btrfs_free_path(path);
265         return ret;
266 }
267
268 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
269 {
270         struct btrfs_root *tree_root = fs_info->tree_root;
271         u64 flags = btrfs_super_flags(fs_info->super_copy);
272         int ret = 0;
273
274         if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
275                 return 0;
276
277         if (fs_info->new_fsid)
278                 flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
279         btrfs_set_super_flags(fs_info->super_copy, flags);
280
281         memcpy(fs_info->super_copy->fsid, fs_info->new_fsid, BTRFS_FSID_SIZE);
282         ret = write_all_supers(tree_root);
283         if (ret < 0)
284                 return ret;
285
286         /* also restore new chunk_tree_id into tree_root for restore */
287         write_extent_buffer(tree_root->node, fs_info->new_chunk_tree_uuid,
288                             btrfs_header_chunk_tree_uuid(tree_root->node),
289                             BTRFS_UUID_SIZE);
290         return write_tree_block(NULL, tree_root, tree_root->node);
291 }
292
293 static int change_fsid_done(struct btrfs_fs_info *fs_info)
294 {
295         u64 flags = btrfs_super_flags(fs_info->super_copy);
296
297         if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
298                 return 0;
299
300         if (fs_info->new_fsid)
301                 flags &= ~BTRFS_SUPER_FLAG_CHANGING_FSID;
302         btrfs_set_super_flags(fs_info->super_copy, flags);
303
304         return write_all_supers(fs_info->tree_root);
305 }
306
307 /*
308  * Return 0 for no unfinished fsid change.
309  * Return >0 for unfinished fsid change, and restore unfinished fsid/
310  * chunk_tree_id into fsid_ret/chunk_id_ret.
311  */
312 static int check_unfinished_fsid_change(struct btrfs_fs_info *fs_info,
313                                         uuid_t fsid_ret, uuid_t chunk_id_ret)
314 {
315         struct btrfs_root *tree_root = fs_info->tree_root;
316         u64 flags = btrfs_super_flags(fs_info->super_copy);
317
318         if (flags & BTRFS_SUPER_FLAG_CHANGING_FSID) {
319                 memcpy(fsid_ret, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
320                 read_extent_buffer(tree_root->node, chunk_id_ret,
321                                 btrfs_header_chunk_tree_uuid(tree_root->node),
322                                 BTRFS_UUID_SIZE);
323                 return 1;
324         }
325         return 0;
326 }
327
328 /*
329  * Change fsid of a given fs.
330  *
331  * If new_fsid_str is not given, use a random generated UUID.
332  * Caller should check new_fsid_str is valid
333  */
334 static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
335 {
336         uuid_t new_fsid;
337         uuid_t new_chunk_id;
338         char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
339         int ret = 0;
340
341         if (check_unfinished_fsid_change(fs_info, new_fsid, new_chunk_id)) {
342                 if (new_fsid_str) {
343                         uuid_t tmp;
344
345                         uuid_parse(new_fsid_str, tmp);
346                         if (memcmp(tmp, new_fsid, BTRFS_FSID_SIZE)) {
347                                 fprintf(stderr,
348                 "ERROR: New fsid %s is not the same with unfinished fsid change\n",
349                                         new_fsid_str);
350                                 return -EINVAL;
351                         }
352                 }
353         } else {
354                 if (new_fsid_str)
355                         uuid_parse(new_fsid_str, new_fsid);
356                 else
357                         uuid_generate(new_fsid);
358
359                 uuid_generate(new_chunk_id);
360         }
361         fs_info->new_fsid = new_fsid;
362         fs_info->new_chunk_tree_uuid = new_chunk_id;
363
364         uuid_unparse_upper(new_fsid, uuid_buf);
365         printf("Changing fsid to %s\n", uuid_buf);
366         /* Now we can begin fsid change */
367         ret = change_fsid_prepare(fs_info);
368         if (ret < 0)
369                 goto out;
370
371         /* Change extents first */
372         ret = change_extents_uuid(fs_info);
373         if (ret < 0) {
374                 fprintf(stderr, "Failed to change UUID of metadata\n");
375                 goto out;
376         }
377
378         /* Then devices */
379         ret = change_devices_uuid(fs_info);
380         if (ret < 0) {
381                 fprintf(stderr, "Failed to change UUID of devices\n");
382                 goto out;
383         }
384
385         /* Last, change fsid in super */
386         memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
387                BTRFS_FSID_SIZE);
388         memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
389                BTRFS_FSID_SIZE);
390         ret = write_all_supers(fs_info->tree_root);
391         if (ret < 0)
392                 goto out;
393
394         /* Now fsid change is done */
395         ret = change_fsid_done(fs_info);
396         fs_info->new_fsid = NULL;
397         fs_info->new_chunk_tree_uuid = NULL;
398         printf("Fsid changed to %s\n", uuid_buf);
399 out:
400         return ret;
401 }
402
403 static void print_usage(void)
404 {
405         fprintf(stderr, "usage: btrfstune [options] device\n");
406         fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
407         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
408         fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
409         fprintf(stderr, "\t-f \t\tforce to do dangerous operation, make sure that you are aware of the dangers\n");
410         fprintf(stderr, "\t-u \t\tchange fsid, use a random one\n");
411         fprintf(stderr, "\t-U UUID\t\tchange fsid to UUID\n");
412 }
413
414 int main(int argc, char *argv[])
415 {
416         struct btrfs_root *root;
417         enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_WRITES;
418         int success = 0;
419         int total = 0;
420         int extrefs_flag = 0;
421         int seeding_flag = 0;
422         u64 seeding_value = 0;
423         int skinny_flag = 0;
424         int random_fsid = 0;
425         char *new_fsid_str = NULL;
426         int ret;
427
428         optind = 1;
429         while(1) {
430                 int c = getopt(argc, argv, "S:rxfuU:");
431                 if (c < 0)
432                         break;
433                 switch(c) {
434                 case 'S':
435                         seeding_flag = 1;
436                         seeding_value = arg_strtou64(optarg);
437                         break;
438                 case 'r':
439                         extrefs_flag = 1;
440                         break;
441                 case 'x':
442                         skinny_flag = 1;
443                         break;
444                 case 'f':
445                         force = 1;
446                         break;
447                 case 'U':
448                         ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
449                         new_fsid_str = optarg;
450                         break;
451                 case 'u':
452                         ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
453                         random_fsid = 1;
454                         break;
455                 default:
456                         print_usage();
457                         return 1;
458                 }
459         }
460
461         set_argv0(argv);
462         argc = argc - optind;
463         device = argv[optind];
464         if (check_argc_exact(argc, 1)) {
465                 print_usage();
466                 return 1;
467         }
468
469         if (random_fsid && new_fsid_str) {
470                 fprintf(stderr,
471                         "ERROR: Random fsid can't be used with specified fsid\n");
472                 return 1;
473         }
474         if (!(seeding_flag + extrefs_flag + skinny_flag) &&
475             !(random_fsid || new_fsid_str)) {
476                 fprintf(stderr,
477                         "ERROR: At least one option should be assigned.\n");
478                 print_usage();
479                 return 1;
480         }
481
482         if (new_fsid_str) {
483                 uuid_t tmp;
484
485                 ret = uuid_parse(new_fsid_str, tmp);
486                 if (ret < 0) {
487                         fprintf(stderr,
488                                 "ERROR: Could not parse UUID: %s\n",
489                                 new_fsid_str);
490                         return 1;
491                 }
492                 if (!test_uuid_unique(new_fsid_str)) {
493                         fprintf(stderr,
494                                 "ERROR: Fsid %s is not unique\n",
495                                 new_fsid_str);
496                         return 1;
497                 }
498         }
499
500         ret = check_mounted(device);
501         if (ret < 0) {
502                 fprintf(stderr, "Could not check mount status: %s\n",
503                         strerror(-ret));
504                 return 1;
505         } else if (ret) {
506                 fprintf(stderr, "%s is mounted\n", device);
507                 return 1;
508         }
509
510         root = open_ctree(device, 0, ctree_flags);
511
512         if (!root) {
513                 fprintf(stderr, "Open ctree failed\n");
514                 return 1;
515         }
516
517         if (seeding_flag) {
518                 if (!seeding_value && !force) {
519                         fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
520                         ret = ask_user("We are going to clear the seeding flag, are you sure?");
521                         if (!ret) {
522                                 fprintf(stderr, "Clear seeding flag canceled\n");
523                                 ret = 1;
524                                 goto out;
525                         }
526                 }
527
528                 ret = update_seeding_flag(root, seeding_value);
529                 if (!ret)
530                         success++;
531                 total++;
532         }
533
534         if (extrefs_flag) {
535                 enable_extrefs_flag(root);
536                 success++;
537                 total++;
538         }
539
540         if (skinny_flag) {
541                 enable_skinny_metadata(root);
542                 success++;
543                 total++;
544         }
545
546         if (random_fsid || new_fsid_str) {
547                 if (!force) {
548                         fprintf(stderr,
549                                 "Warning: It's highly recommended to run 'btrfs check' before this operation\n");
550                         fprintf(stderr,
551                                 "Also canceling running UUID change progress may cause corruption\n");
552                         ret = ask_user("We are going to change UUID, are your sure?");
553                         if (!ret) {
554                                 fprintf(stderr, "UUID change canceled\n");
555                                 ret = 1;
556                                 goto out;
557                         }
558                 }
559                 ret = change_uuid(root->fs_info, new_fsid_str);
560                 if (!ret)
561                         success++;
562                 total++;
563         }
564
565         if (success == total) {
566                 ret = 0;
567         } else {
568                 root->fs_info->readonly = 1;
569                 ret = 1;
570                 fprintf(stderr, "btrfstune failed\n");
571         }
572 out:
573         close_ctree(root);
574
575         return ret;
576 }