ac9f3110888c5d8d6766e041aad7ba395f357b66
[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 #include "version.h"
32
33 static char *device;
34 static int force = 0;
35
36 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
37 {
38         struct btrfs_trans_handle *trans;
39         struct btrfs_super_block *disk_super;
40         u64 super_flags;
41
42         disk_super = root->fs_info->super_copy;
43         super_flags = btrfs_super_flags(disk_super);
44         if (set_flag) {
45                 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
46                         if (force)
47                                 return 0;
48                         else
49                                 fprintf(stderr, "seeding flag is already set on %s\n", device);
50                         return 1;
51                 }
52                 super_flags |= BTRFS_SUPER_FLAG_SEEDING;
53         } else {
54                 if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
55                         fprintf(stderr, "seeding flag is not set on %s\n",
56                                 device);
57                         return 1;
58                 }
59                 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
60                 fprintf(stderr, "Warning: Seeding flag cleared.\n");
61         }
62
63         trans = btrfs_start_transaction(root, 1);
64         btrfs_set_super_flags(disk_super, super_flags);
65         btrfs_commit_transaction(trans, root);
66
67         return 0;
68 }
69
70 static int enable_extrefs_flag(struct btrfs_root *root)
71 {
72         struct btrfs_trans_handle *trans;
73         struct btrfs_super_block *disk_super;
74         u64 super_flags;
75
76         disk_super = root->fs_info->super_copy;
77         super_flags = btrfs_super_incompat_flags(disk_super);
78         super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
79         trans = btrfs_start_transaction(root, 1);
80         btrfs_set_super_incompat_flags(disk_super, super_flags);
81         btrfs_commit_transaction(trans, root);
82
83         return 0;
84 }
85
86 static int enable_skinny_metadata(struct btrfs_root *root)
87 {
88         struct btrfs_trans_handle *trans;
89         struct btrfs_super_block *disk_super;
90         u64 super_flags;
91
92         disk_super = root->fs_info->super_copy;
93         super_flags = btrfs_super_incompat_flags(disk_super);
94         super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
95         trans = btrfs_start_transaction(root, 1);
96         btrfs_set_super_incompat_flags(disk_super, super_flags);
97         btrfs_commit_transaction(trans, root);
98
99         return 0;
100 }
101
102 static void print_usage(void)
103 {
104         fprintf(stderr, "usage: btrfstune [options] device\n");
105         fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
106         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
107         fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
108         fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
109 }
110
111 int main(int argc, char *argv[])
112 {
113         struct btrfs_root *root;
114         int success = 0;
115         int total = 0;
116         int extrefs_flag = 0;
117         int seeding_flag = 0;
118         u64 seeding_value = 0;
119         int skinny_flag = 0;
120         int ret;
121
122         optind = 1;
123         while(1) {
124                 int c = getopt(argc, argv, "S:rxf");
125                 if (c < 0)
126                         break;
127                 switch(c) {
128                 case 'S':
129                         seeding_flag = 1;
130                         seeding_value = arg_strtou64(optarg);
131                         break;
132                 case 'r':
133                         extrefs_flag = 1;
134                         break;
135                 case 'x':
136                         skinny_flag = 1;
137                         break;
138                 case 'f':
139                         force = 1;
140                         break;
141                 default:
142                         print_usage();
143                         return 1;
144                 }
145         }
146
147         set_argv0(argv);
148         argc = argc - optind;
149         device = argv[optind];
150         if (check_argc_exact(argc, 1)) {
151                 print_usage();
152                 return 1;
153         }
154
155         if (!(seeding_flag + extrefs_flag + skinny_flag)) {
156                 fprintf(stderr,
157                         "ERROR: At least one option should be assigned.\n");
158                 print_usage();
159                 return 1;
160         }
161
162         ret = check_mounted(device);
163         if (ret < 0) {
164                 fprintf(stderr, "Could not check mount status: %s\n",
165                         strerror(-ret));
166                 return 1;
167         } else if (ret) {
168                 fprintf(stderr, "%s is mounted\n", device);
169                 return 1;
170         }
171
172         root = open_ctree(device, 0, OPEN_CTREE_WRITES);
173
174         if (!root) {
175                 fprintf(stderr, "Open ctree failed\n");
176                 return 1;
177         }
178
179         if (seeding_flag) {
180                 if (!seeding_value && !force) {
181                         fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
182                         ret = ask_user("We are going to clear the seeding flag, are you sure?");
183                         if (!ret) {
184                                 fprintf(stderr, "Clear seeding flag canceled\n");
185                                 return 1;
186                         }
187                 }
188
189                 ret = update_seeding_flag(root, seeding_value);
190                 if (!ret)
191                         success++;
192                 total++;
193         }
194
195         if (extrefs_flag) {
196                 enable_extrefs_flag(root);
197                 success++;
198                 total++;
199         }
200
201         if (skinny_flag) {
202                 enable_skinny_metadata(root);
203                 success++;
204                 total++;
205         }
206
207         if (success == total) {
208                 ret = 0;
209         } else {
210                 root->fs_info->readonly = 1;
211                 ret = 1;
212                 fprintf(stderr, "btrfstune failed\n");
213         }
214         close_ctree(root);
215
216         return ret;
217 }