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