btrfs-progs: cleanup whitespace in is_same_blk_file
[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
37 static int update_seeding_flag(struct btrfs_root *root, int set_flag)
38 {
39         struct btrfs_trans_handle *trans;
40         struct btrfs_super_block *disk_super;
41         u64 super_flags;
42
43         disk_super = root->fs_info->super_copy;
44         super_flags = btrfs_super_flags(disk_super);
45         if (set_flag) {
46                 if (super_flags & BTRFS_SUPER_FLAG_SEEDING) {
47                         fprintf(stderr, "seeding flag is already set on %s\n",
48                                 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 void print_usage(void)
102 {
103         fprintf(stderr, "usage: btrfstune [options] device\n");
104         fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
105         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
106         fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
107         fprintf(stderr, "\t-f \t\tforce to clear flags, make sure that you are aware of the dangers\n");
108 }
109
110 int main(int argc, char *argv[])
111 {
112         struct btrfs_root *root;
113         int success = 0;
114         int extrefs_flag = 0;
115         int seeding_flag = 0;
116         u64 seeding_value = 0;
117         int skinny_flag = 0;
118         int force = 0;
119         int ret;
120
121         optind = 1;
122         while(1) {
123                 int c = getopt(argc, argv, "S:rxf");
124                 if (c < 0)
125                         break;
126                 switch(c) {
127                 case 'S':
128                         seeding_flag = 1;
129                         seeding_value = arg_strtou64(optarg);
130                         break;
131                 case 'r':
132                         extrefs_flag = 1;
133                         break;
134                 case 'x':
135                         skinny_flag = 1;
136                         break;
137                 case 'f':
138                         force = 1;
139                         break;
140                 default:
141                         print_usage();
142                         return 1;
143                 }
144         }
145
146         set_argv0(argv);
147         argc = argc - optind;
148         device = argv[optind];
149         if (check_argc_exact(argc, 1)) {
150                 print_usage();
151                 return 1;
152         }
153
154         if (!(seeding_flag + extrefs_flag + skinny_flag)) {
155                 fprintf(stderr,
156                         "ERROR: At least one option should be assigned.\n");
157                 print_usage();
158                 return 1;
159         }
160
161         ret = check_mounted(device);
162         if (ret < 0) {
163                 fprintf(stderr, "Could not check mount status: %s\n",
164                         strerror(-ret));
165                 return 1;
166         } else if (ret) {
167                 fprintf(stderr, "%s is mounted\n", device);
168                 return 1;
169         }
170
171         root = open_ctree(device, 0, OPEN_CTREE_WRITES);
172
173         if (!root) {
174                 fprintf(stderr, "Open ctree failed\n");
175                 return 1;
176         }
177
178         if (seeding_flag) {
179                 if (!seeding_value && !force) {
180                         fprintf(stderr, "Warning: This is dangerous, clearing the seeding flag may cause the derived device not to be mountable!\n");
181                         ret = ask_user("We are going to clear the seeding flag, are you sure?");
182                         if (!ret) {
183                                 fprintf(stderr, "Clear seeding flag canceled\n");
184                                 return 1;
185                         }
186                 }
187
188                 ret = update_seeding_flag(root, seeding_value);
189                 if (!ret)
190                         success++;
191         }
192
193         if (extrefs_flag) {
194                 enable_extrefs_flag(root);
195                 success++;
196         }
197
198         if (skinny_flag) {
199                 enable_skinny_metadata(root);
200                 success++;
201         }
202
203         if (success > 0) {
204                 ret = 0;
205         } else {
206                 root->fs_info->readonly = 1;
207                 ret = 1;
208                 fprintf(stderr, "btrfstune failed\n");
209         }
210         close_ctree(root);
211
212         return ret;
213 }