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.
21 #include <sys/types.h>
26 #include <uuid/uuid.h>
28 #include "kerncompat.h"
31 #include "transaction.h"
38 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
40 struct btrfs_trans_handle *trans;
41 struct btrfs_super_block *disk_super;
44 disk_super = root->fs_info->super_copy;
45 super_flags = btrfs_super_flags(disk_super);
47 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
51 fprintf(stderr, "seeding flag is already set on %s\n", device);
54 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
56 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
57 fprintf(stderr, "seeding flag is not set on %s\n",
61 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
62 fprintf(stderr, "Warning: Seeding flag cleared.\n");
65 trans = btrfs_start_transaction(root, 1);
66 btrfs_set_super_flags(disk_super, super_flags);
67 btrfs_commit_transaction(trans, root);
72 static int enable_extrefs_flag(struct btrfs_root *root)
74 struct btrfs_trans_handle *trans;
75 struct btrfs_super_block *disk_super;
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);
88 static int enable_skinny_metadata(struct btrfs_root *root)
90 struct btrfs_trans_handle *trans;
91 struct btrfs_super_block *disk_super;
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);
104 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
106 struct btrfs_fs_info *fs_info = root->fs_info;
108 int same_chunk_tree_uuid = 1;
111 /* Check for whether we need to change fs/chunk id */
112 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
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),
122 if (same_fsid && same_chunk_tree_uuid)
125 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
127 if (!same_chunk_tree_uuid)
128 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
129 btrfs_header_chunk_tree_uuid(eb),
131 ret = write_tree_block(NULL, root, eb);
136 static int change_extents_uuid(struct btrfs_fs_info *fs_info)
138 struct btrfs_root *root = fs_info->extent_root;
139 struct btrfs_path *path;
140 struct btrfs_key key = {0, 0, 0};
143 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
146 path = btrfs_alloc_path();
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
154 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
159 struct btrfs_extent_item *ei;
160 struct extent_buffer *eb;
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)
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))
174 bytenr = key.objectid;
175 eb = read_tree_block(root, bytenr, root->nodesize, 0);
177 fprintf(stderr, "Failed to read tree block: %llu\n",
182 ret = change_header_uuid(root, eb);
183 free_extent_buffer(eb);
185 fprintf(stderr, "Failed to change uuid of tree block: %llu\n",
190 ret = btrfs_next_item(root, path);
200 btrfs_free_path(path);
204 static int change_device_uuid(struct btrfs_root *root, struct extent_buffer *eb,
207 struct btrfs_fs_info *fs_info = root->fs_info;
208 struct btrfs_dev_item *di;
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),
217 write_extent_buffer(eb, fs_info->new_fsid,
218 (unsigned long)btrfs_device_fsid(di),
220 ret = write_tree_block(NULL, root, eb);
225 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
227 struct btrfs_root *root = fs_info->chunk_root;
228 struct btrfs_path *path;
229 struct btrfs_key key = {0, 0, 0};
233 * Unlike change_extents_uuid, we only need to change fsid in dev_item
235 if (!fs_info->new_fsid)
238 path = btrfs_alloc_path();
241 /* No transaction again */
242 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
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)
251 ret = change_device_uuid(root, path->nodes[0], path->slots[0]);
255 ret = btrfs_next_item(root, path);
264 btrfs_free_path(path);
268 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
270 u64 flags = btrfs_super_flags(fs_info->super_copy);
272 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
275 if (fs_info->new_fsid)
276 flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
277 btrfs_set_super_flags(fs_info->super_copy, flags);
279 return write_all_supers(fs_info->tree_root);
282 static int change_fsid_done(struct btrfs_fs_info *fs_info)
284 u64 flags = btrfs_super_flags(fs_info->super_copy);
286 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
289 if (fs_info->new_fsid)
290 flags &= ~BTRFS_SUPER_FLAG_CHANGING_FSID;
291 btrfs_set_super_flags(fs_info->super_copy, flags);
293 return write_all_supers(fs_info->tree_root);
297 * Change fsid of a given fs.
299 * If new_fsid_str is not given, use a random generated UUID.
301 static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
305 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
308 /* caller should do extra check on passed uuid */
310 uuid_parse(new_fsid_str, new_fsid);
312 uuid_generate(new_fsid);
314 uuid_generate(new_chunk_id);
315 fs_info->new_fsid = new_fsid;
316 fs_info->new_chunk_tree_uuid = new_chunk_id;
318 uuid_unparse_upper(new_fsid, uuid_buf);
319 printf("Changing fsid to %s\n", uuid_buf);
320 /* Now we can begin fsid change */
321 ret = change_fsid_prepare(fs_info);
325 /* Change extents first */
326 ret = change_extents_uuid(fs_info);
328 fprintf(stderr, "Failed to change UUID of metadata\n");
333 ret = change_devices_uuid(fs_info);
335 fprintf(stderr, "Failed to change UUID of devices\n");
339 /* Last, change fsid in super */
340 memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
342 memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
344 ret = write_all_supers(fs_info->tree_root);
348 /* Now fsid change is done */
349 ret = change_fsid_done(fs_info);
350 fs_info->new_fsid = NULL;
351 fs_info->new_chunk_tree_uuid = NULL;
352 printf("Fsid changed to %s\n", uuid_buf);
357 static void print_usage(void)
359 fprintf(stderr, "usage: btrfstune [options] device\n");
360 fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
361 fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
362 fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
363 fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
366 int main(int argc, char *argv[])
368 struct btrfs_root *root;
371 int extrefs_flag = 0;
372 int seeding_flag = 0;
373 u64 seeding_value = 0;
379 int c = getopt(argc, argv, "S:rxf");
385 seeding_value = arg_strtou64(optarg);
403 argc = argc - optind;
404 device = argv[optind];
405 if (check_argc_exact(argc, 1)) {
410 if (!(seeding_flag + extrefs_flag + skinny_flag)) {
412 "ERROR: At least one option should be assigned.\n");
417 ret = check_mounted(device);
419 fprintf(stderr, "Could not check mount status: %s\n",
423 fprintf(stderr, "%s is mounted\n", device);
427 root = open_ctree(device, 0, OPEN_CTREE_WRITES);
430 fprintf(stderr, "Open ctree failed\n");
435 if (!seeding_value && !force) {
436 fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
437 ret = ask_user("We are going to clear the seeding flag, are you sure?");
439 fprintf(stderr, "Clear seeding flag canceled\n");
444 ret = update_seeding_flag(root, seeding_value);
451 enable_extrefs_flag(root);
457 enable_skinny_metadata(root);
462 if (success == total) {
465 root->fs_info->readonly = 1;
467 fprintf(stderr, "btrfstune failed\n");