btrfs-progs: Introduce change_extents_uuid function
[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 "kerncompat.h"
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "utils.h"
31
32 static char *device;
33 static int force = 0;
34
35 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
36 {
37         struct btrfs_trans_handle *trans;
38         struct btrfs_super_block *disk_super;
39         u64 super_flags;
40
41         disk_super = root->fs_info->super_copy;
42         super_flags = btrfs_super_flags(disk_super);
43         if (set_flag) {
44                 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
45                         if (force)
46                                 return 0;
47                         else
48                                 fprintf(stderr, "seeding flag is already set on %s\n", device);
49                         return 1;
50                 }
51                 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
52         } else {
53                 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
54                         fprintf(stderr, "seeding flag is not set on %s\n",
55                                 device);
56                         return 1;
57                 }
58                 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
59                 fprintf(stderr, "Warning: Seeding flag cleared.\n");
60         }
61
62         trans = btrfs_start_transaction(root, 1);
63         btrfs_set_super_flags(disk_super, super_flags);
64         btrfs_commit_transaction(trans, root);
65
66         return 0;
67 }
68
69 static int enable_extrefs_flag(struct btrfs_root *root)
70 {
71         struct btrfs_trans_handle *trans;
72         struct btrfs_super_block *disk_super;
73         u64 super_flags;
74
75         disk_super = root->fs_info->super_copy;
76         super_flags = btrfs_super_incompat_flags(disk_super);
77         super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
78         trans = btrfs_start_transaction(root, 1);
79         btrfs_set_super_incompat_flags(disk_super, super_flags);
80         btrfs_commit_transaction(trans, root);
81
82         return 0;
83 }
84
85 static int enable_skinny_metadata(struct btrfs_root *root)
86 {
87         struct btrfs_trans_handle *trans;
88         struct btrfs_super_block *disk_super;
89         u64 super_flags;
90
91         disk_super = root->fs_info->super_copy;
92         super_flags = btrfs_super_incompat_flags(disk_super);
93         super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
94         trans = btrfs_start_transaction(root, 1);
95         btrfs_set_super_incompat_flags(disk_super, super_flags);
96         btrfs_commit_transaction(trans, root);
97
98         return 0;
99 }
100
101 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
102 {
103         struct btrfs_fs_info *fs_info = root->fs_info;
104         int same_fsid = 1;
105         int same_chunk_tree_uuid = 1;
106         int ret;
107
108         /* Check for whether we need to change fs/chunk id */
109         if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
110                 return 0;
111         if (fs_info->new_fsid)
112                 same_fsid = !memcmp_extent_buffer(eb, fs_info->new_fsid,
113                                           btrfs_header_fsid(), BTRFS_FSID_SIZE);
114         if (fs_info->new_chunk_tree_uuid)
115                 same_chunk_tree_uuid =
116                         !memcmp_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
117                                               btrfs_header_chunk_tree_uuid(eb),
118                                               BTRFS_UUID_SIZE);
119         if (same_fsid && same_chunk_tree_uuid)
120                 return 0;
121         if (!same_fsid)
122                 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
123                                     BTRFS_FSID_SIZE);
124         if (!same_chunk_tree_uuid)
125                 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
126                                     btrfs_header_chunk_tree_uuid(eb),
127                                     BTRFS_UUID_SIZE);
128         ret = write_tree_block(NULL, root, eb);
129
130         return ret;
131 }
132
133 static int change_extents_uuid(struct btrfs_fs_info *fs_info)
134 {
135         struct btrfs_root *root = fs_info->extent_root;
136         struct btrfs_path *path;
137         struct btrfs_key key = {0, 0, 0};
138         int ret = 0;
139
140         if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
141                 return 0;
142
143         path = btrfs_alloc_path();
144         if (!path)
145                 return -ENOMEM;
146
147         /*
148          * Here we don't use transaction as it will takes a lot of reserve
149          * space, and that will make a near-full btrfs unable to change uuid
150          */
151         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
152         if (ret < 0)
153                 goto out;
154
155         while (1) {
156                 struct btrfs_extent_item *ei;
157                 struct extent_buffer *eb;
158                 u64 flags;
159                 u64 bytenr;
160
161                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
162                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
163                     key.type != BTRFS_METADATA_ITEM_KEY)
164                         goto next;
165                 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
166                                     struct btrfs_extent_item);
167                 flags = btrfs_extent_flags(path->nodes[0], ei);
168                 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
169                         goto next;
170
171                 bytenr = key.objectid;
172                 eb = read_tree_block(root, bytenr, root->nodesize, 0);
173                 if (IS_ERR(eb)) {
174                         fprintf(stderr, "Failed to read tree block: %llu\n",
175                                 bytenr);
176                         ret = PTR_ERR(eb);
177                         goto out;
178                 }
179                 ret = change_header_uuid(root, eb);
180                 free_extent_buffer(eb);
181                 if (ret < 0) {
182                         fprintf(stderr, "Failed to change uuid of tree block: %llu\n",
183                                 bytenr);
184                         goto out;
185                 }
186 next:
187                 ret = btrfs_next_item(root, path);
188                 if (ret < 0)
189                         goto out;
190                 if (ret > 0) {
191                         ret = 0;
192                         goto out;
193                 }
194         }
195
196 out:
197         btrfs_free_path(path);
198         return ret;
199 }
200
201 static void print_usage(void)
202 {
203         fprintf(stderr, "usage: btrfstune [options] device\n");
204         fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
205         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
206         fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
207         fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
208 }
209
210 int main(int argc, char *argv[])
211 {
212         struct btrfs_root *root;
213         int success = 0;
214         int total = 0;
215         int extrefs_flag = 0;
216         int seeding_flag = 0;
217         u64 seeding_value = 0;
218         int skinny_flag = 0;
219         int ret;
220
221         optind = 1;
222         while(1) {
223                 int c = getopt(argc, argv, "S:rxf");
224                 if (c < 0)
225                         break;
226                 switch(c) {
227                 case 'S':
228                         seeding_flag = 1;
229                         seeding_value = arg_strtou64(optarg);
230                         break;
231                 case 'r':
232                         extrefs_flag = 1;
233                         break;
234                 case 'x':
235                         skinny_flag = 1;
236                         break;
237                 case 'f':
238                         force = 1;
239                         break;
240                 default:
241                         print_usage();
242                         return 1;
243                 }
244         }
245
246         set_argv0(argv);
247         argc = argc - optind;
248         device = argv[optind];
249         if (check_argc_exact(argc, 1)) {
250                 print_usage();
251                 return 1;
252         }
253
254         if (!(seeding_flag + extrefs_flag + skinny_flag)) {
255                 fprintf(stderr,
256                         "ERROR: At least one option should be assigned.\n");
257                 print_usage();
258                 return 1;
259         }
260
261         ret = check_mounted(device);
262         if (ret < 0) {
263                 fprintf(stderr, "Could not check mount status: %s\n",
264                         strerror(-ret));
265                 return 1;
266         } else if (ret) {
267                 fprintf(stderr, "%s is mounted\n", device);
268                 return 1;
269         }
270
271         root = open_ctree(device, 0, OPEN_CTREE_WRITES);
272
273         if (!root) {
274                 fprintf(stderr, "Open ctree failed\n");
275                 return 1;
276         }
277
278         if (seeding_flag) {
279                 if (!seeding_value && !force) {
280                         fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
281                         ret = ask_user("We are going to clear the seeding flag, are you sure?");
282                         if (!ret) {
283                                 fprintf(stderr, "Clear seeding flag canceled\n");
284                                 return 1;
285                         }
286                 }
287
288                 ret = update_seeding_flag(root, seeding_value);
289                 if (!ret)
290                         success++;
291                 total++;
292         }
293
294         if (extrefs_flag) {
295                 enable_extrefs_flag(root);
296                 success++;
297                 total++;
298         }
299
300         if (skinny_flag) {
301                 enable_skinny_metadata(root);
302                 success++;
303                 total++;
304         }
305
306         if (success == total) {
307                 ret = 0;
308         } else {
309                 root->fs_info->readonly = 1;
310                 ret = 1;
311                 fprintf(stderr, "btrfstune failed\n");
312         }
313         close_ctree(root);
314
315         return ret;
316 }