btrfs-progs: judge the return value of check_mounted more accurately
[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         }
60
61         trans = btrfs_start_transaction(root, 1);
62         btrfs_set_super_flags(disk_super, super_flags);
63         btrfs_commit_transaction(trans, root);
64
65         return 0;
66 }
67
68 static int enable_extrefs_flag(struct btrfs_root *root)
69 {
70         struct btrfs_trans_handle *trans;
71         struct btrfs_super_block *disk_super;
72         u64 super_flags;
73
74         disk_super = root->fs_info->super_copy;
75         super_flags = btrfs_super_incompat_flags(disk_super);
76         super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
77         trans = btrfs_start_transaction(root, 1);
78         btrfs_set_super_incompat_flags(disk_super, super_flags);
79         btrfs_commit_transaction(trans, root);
80
81         return 0;
82 }
83
84 static int enable_skinny_metadata(struct btrfs_root *root)
85 {
86         struct btrfs_trans_handle *trans;
87         struct btrfs_super_block *disk_super;
88         u64 super_flags;
89
90         disk_super = root->fs_info->super_copy;
91         super_flags = btrfs_super_incompat_flags(disk_super);
92         super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA;
93         trans = btrfs_start_transaction(root, 1);
94         btrfs_set_super_incompat_flags(disk_super, super_flags);
95         btrfs_commit_transaction(trans, root);
96
97         return 0;
98 }
99
100 static void print_usage(void)
101 {
102         fprintf(stderr, "usage: btrfstune [options] device\n");
103         fprintf(stderr, "\t-S value\tenable/disable seeding\n");
104         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
105         fprintf(stderr, "\t-x enable skinny metadata extent refs\n");
106 }
107
108 int main(int argc, char *argv[])
109 {
110         struct btrfs_root *root;
111         int success = 0;
112         int extrefs_flag = 0;
113         int seeding_flag = 0;
114         u64 seeding_value = 0;
115         int skinny_flag = 0;
116         int ret;
117
118         optind = 1;
119         while(1) {
120                 int c = getopt(argc, argv, "S:rx");
121                 if (c < 0)
122                         break;
123                 switch(c) {
124                 case 'S':
125                         seeding_flag = 1;
126                         seeding_value = arg_strtou64(optarg);
127                         break;
128                 case 'r':
129                         extrefs_flag = 1;
130                         break;
131                 case 'x':
132                         skinny_flag = 1;
133                         break;
134                 default:
135                         print_usage();
136                         return 1;
137                 }
138         }
139
140         argc = argc - optind;
141         device = argv[optind];
142         if (argc != 1) {
143                 print_usage();
144                 return 1;
145         }
146
147         if (!(seeding_flag + extrefs_flag + skinny_flag)) {
148                 fprintf(stderr,
149                         "ERROR: At least one option should be assigned.\n");
150                 print_usage();
151                 return 1;
152         }
153
154         ret = check_mounted(device);
155         if (ret < 0) {
156                 fprintf(stderr, "Could not check mount status: %s\n",
157                         strerror(-ret));
158                 return 1;
159         } else if (ret) {
160                 fprintf(stderr, "%s is mounted\n", device);
161                 return 1;
162         }
163
164         root = open_ctree(device, 0, OPEN_CTREE_WRITES);
165
166         if (!root) {
167                 fprintf(stderr, "Open ctree failed\n");
168                 return 1;
169         }
170
171         if (seeding_flag) {
172                 ret = update_seeding_flag(root, seeding_value);
173                 if (!ret)
174                         success++;
175         }
176
177         if (extrefs_flag) {
178                 enable_extrefs_flag(root);
179                 success++;
180         }
181
182         if (skinny_flag) {
183                 enable_skinny_metadata(root);
184                 success++;
185         }
186
187         if (success > 0) {
188                 ret = 0;
189         } else {
190                 root->fs_info->readonly = 1;
191                 ret = 1;
192                 fprintf(stderr, "btrfstune failed\n");
193         }
194         close_ctree(root);
195
196         return ret;
197 }