92837e6f5e6106d33b45b1b35d3fa5d1191e6393
[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 void print_usage(void)
134 {
135         fprintf(stderr, "usage: btrfstune [options] device\n");
136         fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
137         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
138         fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
139         fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
140 }
141
142 int main(int argc, char *argv[])
143 {
144         struct btrfs_root *root;
145         int success = 0;
146         int total = 0;
147         int extrefs_flag = 0;
148         int seeding_flag = 0;
149         u64 seeding_value = 0;
150         int skinny_flag = 0;
151         int ret;
152
153         optind = 1;
154         while(1) {
155                 int c = getopt(argc, argv, "S:rxf");
156                 if (c < 0)
157                         break;
158                 switch(c) {
159                 case 'S':
160                         seeding_flag = 1;
161                         seeding_value = arg_strtou64(optarg);
162                         break;
163                 case 'r':
164                         extrefs_flag = 1;
165                         break;
166                 case 'x':
167                         skinny_flag = 1;
168                         break;
169                 case 'f':
170                         force = 1;
171                         break;
172                 default:
173                         print_usage();
174                         return 1;
175                 }
176         }
177
178         set_argv0(argv);
179         argc = argc - optind;
180         device = argv[optind];
181         if (check_argc_exact(argc, 1)) {
182                 print_usage();
183                 return 1;
184         }
185
186         if (!(seeding_flag + extrefs_flag + skinny_flag)) {
187                 fprintf(stderr,
188                         "ERROR: At least one option should be assigned.\n");
189                 print_usage();
190                 return 1;
191         }
192
193         ret = check_mounted(device);
194         if (ret < 0) {
195                 fprintf(stderr, "Could not check mount status: %s\n",
196                         strerror(-ret));
197                 return 1;
198         } else if (ret) {
199                 fprintf(stderr, "%s is mounted\n", device);
200                 return 1;
201         }
202
203         root = open_ctree(device, 0, OPEN_CTREE_WRITES);
204
205         if (!root) {
206                 fprintf(stderr, "Open ctree failed\n");
207                 return 1;
208         }
209
210         if (seeding_flag) {
211                 if (!seeding_value && !force) {
212                         fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
213                         ret = ask_user("We are going to clear the seeding flag, are you sure?");
214                         if (!ret) {
215                                 fprintf(stderr, "Clear seeding flag canceled\n");
216                                 return 1;
217                         }
218                 }
219
220                 ret = update_seeding_flag(root, seeding_value);
221                 if (!ret)
222                         success++;
223                 total++;
224         }
225
226         if (extrefs_flag) {
227                 enable_extrefs_flag(root);
228                 success++;
229                 total++;
230         }
231
232         if (skinny_flag) {
233                 enable_skinny_metadata(root);
234                 success++;
235                 total++;
236         }
237
238         if (success == total) {
239                 ret = 0;
240         } else {
241                 root->fs_info->readonly = 1;
242                 ret = 1;
243                 fprintf(stderr, "btrfstune failed\n");
244         }
245         close_ctree(root);
246
247         return ret;
248 }