47830c5a94189452b6958e97fc17f605737ba90c
[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 static void print_usage(void)
69 {
70         fprintf(stderr, "usage: btrfstune [options] device\n");
71         fprintf(stderr, "\t-S value\tenable/disable seeding\n");
72 }
73
74 int main(int argc, char *argv[])
75 {
76         struct btrfs_root *root;
77         int success = 0;
78         int seeding_flag = 0;
79         int seeding_value = 0;
80         int ret;
81
82         while(1) {
83                 int c = getopt(argc, argv, "S:");
84                 if (c < 0)
85                         break;
86                 switch(c) {
87                 case 'S':
88                         seeding_flag = 1;
89                         seeding_value = atoi(optarg);
90                         break;
91                 default:
92                         print_usage();
93                         return 1;
94                 }
95         }
96
97         argc = argc - optind;
98         device = argv[optind];
99         if (argc != 1) {
100                 print_usage();
101                 return 1;
102         }
103
104         if (check_mounted(device)) {
105                 fprintf(stderr, "%s is mounted\n", device);
106                 return 1;
107         }
108
109         root = open_ctree(device, 0, 1);
110
111         if (seeding_flag) {
112                 ret = update_seeding_flag(root, seeding_value);
113                 if (!ret)
114                         success++;
115         }
116
117         if (success > 0) {
118                 ret = 0;
119         } else {
120                 root->fs_info->readonly = 1;
121                 ret = 1;
122         }
123         close_ctree(root);
124
125         return ret;
126 }