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