Fix inode link count checks in btrfsck
[btrfs-progs-unstable.git] / btrfstune.c
blob47830c5a94189452b6958e97fc17f605737ba90c
1 /*
2 * Copyright (C) 2008 Oracle. All rights reserved.
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.
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.
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.
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"
35 static char *device;
37 int update_seeding_flag(struct btrfs_root *root, int set_flag)
39 struct btrfs_trans_handle *trans;
40 struct btrfs_super_block *disk_super;
41 u64 super_flags;
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;
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;
58 super_flags &= ~BTRFS_SUPER_FLAG_SEEDING;
61 trans = btrfs_start_transaction(root, 1);
62 btrfs_set_super_flags(disk_super, super_flags);
63 btrfs_commit_transaction(trans, root);
65 return 0;
68 static void print_usage(void)
70 fprintf(stderr, "usage: btrfstune [options] device\n");
71 fprintf(stderr, "\t-S value\tenable/disable seeding\n");
74 int main(int argc, char *argv[])
76 struct btrfs_root *root;
77 int success = 0;
78 int seeding_flag = 0;
79 int seeding_value = 0;
80 int ret;
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;
97 argc = argc - optind;
98 device = argv[optind];
99 if (argc != 1) {
100 print_usage();
101 return 1;
104 if (check_mounted(device)) {
105 fprintf(stderr, "%s is mounted\n", device);
106 return 1;
109 root = open_ctree(device, 0, 1);
111 if (seeding_flag) {
112 ret = update_seeding_flag(root, seeding_value);
113 if (!ret)
114 success++;
117 if (success > 0) {
118 ret = 0;
119 } else {
120 root->fs_info->readonly = 1;
121 ret = 1;
123 close_ctree(root);
125 return ret;