77a8b7ac54850f3d745ed218987eab6e1b8eb9ed
[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 int change_device_uuid(struct btrfs_root *root, struct extent_buffer *eb,
202                               int slot)
203 {
204         struct btrfs_fs_info *fs_info = root->fs_info;
205         struct btrfs_dev_item *di;
206         int ret = 0;
207
208         di = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
209         if (fs_info->new_fsid) {
210                 if (!memcmp_extent_buffer(eb, fs_info->new_fsid,
211                                           (unsigned long)btrfs_device_fsid(di),
212                                           BTRFS_FSID_SIZE))
213                         return ret;
214                 write_extent_buffer(eb, fs_info->new_fsid,
215                                     (unsigned long)btrfs_device_fsid(di),
216                                     BTRFS_FSID_SIZE);
217                 ret = write_tree_block(NULL, root, eb);
218         }
219         return ret;
220 }
221
222 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
223 {
224         struct btrfs_root *root = fs_info->chunk_root;
225         struct btrfs_path *path;
226         struct btrfs_key key = {0, 0, 0};
227         int ret = 0;
228
229         /*
230          * Unlike change_extents_uuid, we only need to change fsid in dev_item
231          */
232         if (!fs_info->new_fsid)
233                 return 0;
234
235         path = btrfs_alloc_path();
236         if (!path)
237                 return -ENOMEM;
238         /* No transaction again */
239         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
240         if (ret < 0)
241                 goto out;
242
243         while (1) {
244                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
245                 if (key.type != BTRFS_DEV_ITEM_KEY ||
246                     key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
247                         goto next;
248                 ret = change_device_uuid(root, path->nodes[0], path->slots[0]);
249                 if (ret < 0)
250                         goto out;
251 next:
252                 ret = btrfs_next_item(root, path);
253                 if (ret < 0)
254                         goto out;
255                 if (ret > 0) {
256                         ret = 0;
257                         goto out;
258                 }
259         }
260 out:
261         btrfs_free_path(path);
262         return ret;
263 }
264
265 static void print_usage(void)
266 {
267         fprintf(stderr, "usage: btrfstune [options] device\n");
268         fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
269         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
270         fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
271         fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
272 }
273
274 int main(int argc, char *argv[])
275 {
276         struct btrfs_root *root;
277         int success = 0;
278         int total = 0;
279         int extrefs_flag = 0;
280         int seeding_flag = 0;
281         u64 seeding_value = 0;
282         int skinny_flag = 0;
283         int ret;
284
285         optind = 1;
286         while(1) {
287                 int c = getopt(argc, argv, "S:rxf");
288                 if (c < 0)
289                         break;
290                 switch(c) {
291                 case 'S':
292                         seeding_flag = 1;
293                         seeding_value = arg_strtou64(optarg);
294                         break;
295                 case 'r':
296                         extrefs_flag = 1;
297                         break;
298                 case 'x':
299                         skinny_flag = 1;
300                         break;
301                 case 'f':
302                         force = 1;
303                         break;
304                 default:
305                         print_usage();
306                         return 1;
307                 }
308         }
309
310         set_argv0(argv);
311         argc = argc - optind;
312         device = argv[optind];
313         if (check_argc_exact(argc, 1)) {
314                 print_usage();
315                 return 1;
316         }
317
318         if (!(seeding_flag + extrefs_flag + skinny_flag)) {
319                 fprintf(stderr,
320                         "ERROR: At least one option should be assigned.\n");
321                 print_usage();
322                 return 1;
323         }
324
325         ret = check_mounted(device);
326         if (ret < 0) {
327                 fprintf(stderr, "Could not check mount status: %s\n",
328                         strerror(-ret));
329                 return 1;
330         } else if (ret) {
331                 fprintf(stderr, "%s is mounted\n", device);
332                 return 1;
333         }
334
335         root = open_ctree(device, 0, OPEN_CTREE_WRITES);
336
337         if (!root) {
338                 fprintf(stderr, "Open ctree failed\n");
339                 return 1;
340         }
341
342         if (seeding_flag) {
343                 if (!seeding_value && !force) {
344                         fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
345                         ret = ask_user("We are going to clear the seeding flag, are you sure?");
346                         if (!ret) {
347                                 fprintf(stderr, "Clear seeding flag canceled\n");
348                                 return 1;
349                         }
350                 }
351
352                 ret = update_seeding_flag(root, seeding_value);
353                 if (!ret)
354                         success++;
355                 total++;
356         }
357
358         if (extrefs_flag) {
359                 enable_extrefs_flag(root);
360                 success++;
361                 total++;
362         }
363
364         if (skinny_flag) {
365                 enable_skinny_metadata(root);
366                 success++;
367                 total++;
368         }
369
370         if (success == total) {
371                 ret = 0;
372         } else {
373                 root->fs_info->readonly = 1;
374                 ret = 1;
375                 fprintf(stderr, "btrfstune failed\n");
376         }
377         close_ctree(root);
378
379         return ret;
380 }