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 "kerncompat.h"
29 #include "transaction.h"
35 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
37 struct btrfs_trans_handle *trans;
38 struct btrfs_super_block *disk_super;
41 disk_super = root->fs_info->super_copy;
42 super_flags = btrfs_super_flags(disk_super);
44 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
48 fprintf(stderr, "seeding flag is already set on %s\n", device);
51 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
53 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
54 fprintf(stderr, "seeding flag is not set on %s\n",
58 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
59 fprintf(stderr, "Warning: Seeding flag cleared.\n");
62 trans = btrfs_start_transaction(root, 1);
63 btrfs_set_super_flags(disk_super, super_flags);
64 btrfs_commit_transaction(trans, root);
69 static int enable_extrefs_flag(struct btrfs_root *root)
71 struct btrfs_trans_handle *trans;
72 struct btrfs_super_block *disk_super;
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);
85 static int enable_skinny_metadata(struct btrfs_root *root)
87 struct btrfs_trans_handle *trans;
88 struct btrfs_super_block *disk_super;
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);
101 static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
103 struct btrfs_fs_info *fs_info = root->fs_info;
105 int same_chunk_tree_uuid = 1;
108 /* Check for whether we need to change fs/chunk id */
109 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
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),
119 if (same_fsid && same_chunk_tree_uuid)
122 write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
124 if (!same_chunk_tree_uuid)
125 write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
126 btrfs_header_chunk_tree_uuid(eb),
128 ret = write_tree_block(NULL, root, eb);
133 static int change_extents_uuid(struct btrfs_fs_info *fs_info)
135 struct btrfs_root *root = fs_info->extent_root;
136 struct btrfs_path *path;
137 struct btrfs_key key = {0, 0, 0};
140 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
143 path = btrfs_alloc_path();
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
151 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
156 struct btrfs_extent_item *ei;
157 struct extent_buffer *eb;
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)
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))
171 bytenr = key.objectid;
172 eb = read_tree_block(root, bytenr, root->nodesize, 0);
174 fprintf(stderr, "Failed to read tree block: %llu\n",
179 ret = change_header_uuid(root, eb);
180 free_extent_buffer(eb);
182 fprintf(stderr, "Failed to change uuid of tree block: %llu\n",
187 ret = btrfs_next_item(root, path);
197 btrfs_free_path(path);
201 static int change_device_uuid(struct btrfs_root *root, struct extent_buffer *eb,
204 struct btrfs_fs_info *fs_info = root->fs_info;
205 struct btrfs_dev_item *di;
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),
214 write_extent_buffer(eb, fs_info->new_fsid,
215 (unsigned long)btrfs_device_fsid(di),
217 ret = write_tree_block(NULL, root, eb);
222 static int change_devices_uuid(struct btrfs_fs_info *fs_info)
224 struct btrfs_root *root = fs_info->chunk_root;
225 struct btrfs_path *path;
226 struct btrfs_key key = {0, 0, 0};
230 * Unlike change_extents_uuid, we only need to change fsid in dev_item
232 if (!fs_info->new_fsid)
235 path = btrfs_alloc_path();
238 /* No transaction again */
239 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
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)
248 ret = change_device_uuid(root, path->nodes[0], path->slots[0]);
252 ret = btrfs_next_item(root, path);
261 btrfs_free_path(path);
265 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
267 u64 flags = btrfs_super_flags(fs_info->super_copy);
269 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
272 if (fs_info->new_fsid)
273 flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
274 btrfs_set_super_flags(fs_info->super_copy, flags);
276 return write_all_supers(fs_info->tree_root);
279 static int change_fsid_done(struct btrfs_fs_info *fs_info)
281 u64 flags = btrfs_super_flags(fs_info->super_copy);
283 if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
286 if (fs_info->new_fsid)
287 flags &= ~BTRFS_SUPER_FLAG_CHANGING_FSID;
288 btrfs_set_super_flags(fs_info->super_copy, flags);
290 return write_all_supers(fs_info->tree_root);
293 static void print_usage(void)
295 fprintf(stderr, "usage: btrfstune [options] device\n");
296 fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
297 fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
298 fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
299 fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
302 int main(int argc, char *argv[])
304 struct btrfs_root *root;
307 int extrefs_flag = 0;
308 int seeding_flag = 0;
309 u64 seeding_value = 0;
315 int c = getopt(argc, argv, "S:rxf");
321 seeding_value = arg_strtou64(optarg);
339 argc = argc - optind;
340 device = argv[optind];
341 if (check_argc_exact(argc, 1)) {
346 if (!(seeding_flag + extrefs_flag + skinny_flag)) {
348 "ERROR: At least one option should be assigned.\n");
353 ret = check_mounted(device);
355 fprintf(stderr, "Could not check mount status: %s\n",
359 fprintf(stderr, "%s is mounted\n", device);
363 root = open_ctree(device, 0, OPEN_CTREE_WRITES);
366 fprintf(stderr, "Open ctree failed\n");
371 if (!seeding_value && !force) {
372 fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
373 ret = ask_user("We are going to clear the seeding flag, are you sure?");
375 fprintf(stderr, "Clear seeding flag canceled\n");
380 ret = update_seeding_flag(root, seeding_value);
387 enable_extrefs_flag(root);
393 enable_skinny_metadata(root);
398 if (success == total) {
401 root->fs_info->readonly = 1;
403 fprintf(stderr, "btrfstune failed\n");