btrfs-progs: better option/error handling for btrfs-vol
[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 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 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 void print_usage(void)
85 {
86         fprintf(stderr, "usage: btrfstune [options] device\n");
87         fprintf(stderr, "\t-S value\tenable/disable seeding\n");
88         fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
89 }
90
91 int main(int argc, char *argv[])
92 {
93         struct btrfs_root *root;
94         int success = 0;
95         int extrefs_flag = 0;
96         int seeding_flag = 0;
97         int seeding_value = 0;
98         int ret;
99
100         while(1) {
101                 int c = getopt(argc, argv, "S:r");
102                 if (c < 0)
103                         break;
104                 switch(c) {
105                 case 'S':
106                         seeding_flag = 1;
107                         seeding_value = atoi(optarg);
108                         break;
109                 case 'r':
110                         extrefs_flag = 1;
111                         break;
112                 default:
113                         print_usage();
114                         return 1;
115                 }
116         }
117
118         argc = argc - optind;
119         device = argv[optind];
120         if (argc != 1) {
121                 print_usage();
122                 return 1;
123         }
124
125         if (check_mounted(device)) {
126                 fprintf(stderr, "%s is mounted\n", device);
127                 return 1;
128         }
129
130         root = open_ctree(device, 0, 1);
131
132         if (!root) {
133                 fprintf(stderr, "Open ctree failed\n");
134                 return 1;
135         }
136
137         if (seeding_flag) {
138                 ret = update_seeding_flag(root, seeding_value);
139                 if (!ret)
140                         success++;
141         }
142
143         if (extrefs_flag) {
144                 enable_extrefs_flag(root);
145                 success++;
146         }
147
148         if (success > 0) {
149                 ret = 0;
150         } else {
151                 root->fs_info->readonly = 1;
152                 ret = 1;
153         }
154         close_ctree(root);
155
156         return ret;
157 }