]> git.sesse.net Git - bcachefs-tools-debian/blob - bcachefs.c
New subcommand: set-option
[bcachefs-tools-debian] / bcachefs.c
1 /*
2  * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3  *          Gabriel de Perthuis <g2p.code@gmail.com>
4  *          Jacob Malevich <jam@datera.io>
5  *
6  * GPLv2
7  */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <limits.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include <raid/raid.h>
25
26 #include "cmds.h"
27
28 static void usage(void)
29 {
30         puts("bcachefs - tool for managing bcachefs filesystems\n"
31              "usage: bcachefs <command> [<args>]\n"
32              "\n"
33              "Superblock commands:\n"
34              "  format                   Format a new filesystem\n"
35              "  show-super               Dump superblock information to stdout\n"
36              "  set-option               Set a filesystem option\n"
37              "\n"
38              "Repair:\n"
39              "  fsck                     Check an existing filesystem for errors\n"
40              "\n"
41 #if 0
42              "Startup/shutdown, assembly of multi device filesystems:\n"
43              "  assemble                 Assemble an existing multi device filesystem\n"
44              "  incremental              Incrementally assemble an existing multi device filesystem\n"
45              "  run                      Start a partially assembled filesystem\n"
46              "  stop                     Stop a running filesystem\n"
47              "\n"
48 #endif
49              "Commands for managing a running filesystem:\n"
50              "  fs usage                 Show disk usage\n"
51              "\n"
52              "Commands for managing devices within a running filesystem:\n"
53              "  device add               Add a new device to an existing filesystem\n"
54              "  device remove            Remove a device from an existing filesystem\n"
55              "  device online            Re-add an existing member to a filesystem\n"
56              "  device offline           Take a device offline, without removing it\n"
57              "  device evacuate          Migrate data off of a specific device\n"
58              "  device set-state         Mark a device as failed\n"
59              "  device resize            Resize filesystem on a device\n"
60              "  device resize-journal    Resize journal on a device\n"
61              "\n"
62              "Commands for managing subvolumes and snapshots:\n"
63              "  subvolume create         Create a new subvolume\n"
64              "  subvolume delete         Delete an existing subvolume\n"
65              "  subvolume snapshot       Create a snapshot\n"
66              "\n"
67              "Commands for managing filesystem data:\n"
68              "  data rereplicate         Rereplicate degraded data\n"
69              "  data job                 Kick off low level data jobs\n"
70              "\n"
71              "Encryption:\n"
72              "  unlock                   Unlock an encrypted filesystem prior to running/mounting\n"
73              "  set-passphrase           Change passphrase on an existing (unmounted) filesystem\n"
74              "  remove-passphrase        Remove passphrase on an existing (unmounted) filesystem\n"
75              "\n"
76              "Migrate:\n"
77              "  migrate                  Migrate an existing filesystem to bcachefs, in place\n"
78              "  migrate-superblock       Add default superblock, after bcachefs migrate\n"
79              "\n"
80              "Commands for operating on files in a bcachefs filesystem:\n"
81              "  setattr                  Set various per file attributes\n"
82              "Debug:\n"
83              "These commands work on offline, unmounted filesystems\n"
84              "  dump                     Dump filesystem metadata to a qcow2 image\n"
85              "  list                     List filesystem metadata in textual form\n"
86              "  list_journal             List contents of journal\n"
87              "\n"
88              "Miscellaneous:\n"
89              "  version                  Display the version of the invoked bcachefs tool\n");
90 }
91
92 static char *full_cmd;
93
94 static char *pop_cmd(int *argc, char *argv[])
95 {
96         char *cmd = argv[1];
97         if (!(*argc < 2))
98                 memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
99         (*argc)--;
100
101         full_cmd = mprintf("%s %s", full_cmd, cmd);
102         return cmd;
103 }
104
105 static int fs_cmds(int argc, char *argv[])
106 {
107         char *cmd = pop_cmd(&argc, argv);
108
109         if (argc < 1)
110                 return fs_usage();
111         if (!strcmp(cmd, "usage"))
112                 return cmd_fs_usage(argc, argv);
113
114         return 0;
115 }
116
117 static int device_cmds(int argc, char *argv[])
118 {
119         char *cmd = pop_cmd(&argc, argv);
120
121         if (argc < 1)
122                 return device_usage();
123         if (!strcmp(cmd, "add"))
124                 return cmd_device_add(argc, argv);
125         if (!strcmp(cmd, "remove"))
126                 return cmd_device_remove(argc, argv);
127         if (!strcmp(cmd, "online"))
128                 return cmd_device_online(argc, argv);
129         if (!strcmp(cmd, "offline"))
130                 return cmd_device_offline(argc, argv);
131         if (!strcmp(cmd, "evacuate"))
132                 return cmd_device_evacuate(argc, argv);
133         if (!strcmp(cmd, "set-state"))
134                 return cmd_device_set_state(argc, argv);
135         if (!strcmp(cmd, "resize"))
136                 return cmd_device_resize(argc, argv);
137         if (!strcmp(cmd, "resize-journal"))
138                 return cmd_device_resize_journal(argc, argv);
139
140         return 0;
141 }
142
143 static int data_cmds(int argc, char *argv[])
144 {
145         char *cmd = pop_cmd(&argc, argv);
146
147         if (argc < 1)
148                 return data_usage();
149         if (!strcmp(cmd, "rereplicate"))
150                 return cmd_data_rereplicate(argc, argv);
151         if (!strcmp(cmd, "job"))
152                 return cmd_data_job(argc, argv);
153
154         return 0;
155 }
156
157 static int subvolume_cmds(int argc, char *argv[])
158 {
159         char *cmd = pop_cmd(&argc, argv);
160         if (argc < 1)
161                 return subvolume_usage();
162         if (!strcmp(cmd, "create"))
163                 return cmd_subvolume_create(argc, argv);
164         if (!strcmp(cmd, "delete"))
165                 return cmd_subvolume_delete(argc, argv);
166         if (!strcmp(cmd, "snapshot"))
167                 return cmd_subvolume_snapshot(argc, argv);
168
169         return 0;
170 }
171
172 int main(int argc, char *argv[])
173 {
174         raid_init();
175
176         full_cmd = argv[0];
177
178         setvbuf(stdout, NULL, _IOLBF, 0);
179
180         char *cmd = pop_cmd(&argc, argv);
181         if (argc < 1) {
182                 puts("missing command\n");
183                 goto usage;
184         }
185
186         /* these subcommands display usage when argc < 2 */
187         if (!strcmp(cmd, "device"))
188                 return device_cmds(argc, argv);
189         if (!strcmp(cmd, "fs"))
190                 return fs_cmds(argc, argv);
191         if (!strcmp(cmd, "data"))
192                 return data_cmds(argc, argv);
193         if (!strcmp(cmd, "subvolume"))
194                 return subvolume_cmds(argc, argv);
195         if (!strcmp(cmd, "format"))
196                 return cmd_format(argc, argv);
197         if (!strcmp(cmd, "fsck"))
198                 return cmd_fsck(argc, argv);
199         if (!strcmp(cmd, "version"))
200                 return cmd_version(argc, argv);
201         if (!strcmp(cmd, "show-super"))
202                 return cmd_show_super(argc, argv);
203         if (!strcmp(cmd, "set-option"))
204                 return cmd_set_option(argc, argv);
205
206         if (argc < 2) {
207                 printf("%s: missing command\n", argv[0]);
208                 usage();
209                 exit(EXIT_FAILURE);
210         }
211
212 #if 0
213         if (!strcmp(cmd, "assemble"))
214                 return cmd_assemble(argc, argv);
215         if (!strcmp(cmd, "incremental"))
216                 return cmd_incremental(argc, argv);
217         if (!strcmp(cmd, "run"))
218                 return cmd_run(argc, argv);
219         if (!strcmp(cmd, "stop"))
220                 return cmd_stop(argc, argv);
221 #endif
222
223         if (!strcmp(cmd, "unlock"))
224                 return cmd_unlock(argc, argv);
225         if (!strcmp(cmd, "set-passphrase"))
226                 return cmd_set_passphrase(argc, argv);
227         if (!strcmp(cmd, "remove-passphrase"))
228                 return cmd_remove_passphrase(argc, argv);
229
230         if (!strcmp(cmd, "migrate"))
231                 return cmd_migrate(argc, argv);
232         if (!strcmp(cmd, "migrate-superblock"))
233                 return cmd_migrate_superblock(argc, argv);
234
235         if (!strcmp(cmd, "dump"))
236                 return cmd_dump(argc, argv);
237         if (!strcmp(cmd, "list"))
238                 return cmd_list(argc, argv);
239         if (!strcmp(cmd, "list_journal"))
240                 return cmd_list_journal(argc, argv);
241         if (!strcmp(cmd, "kill_btree_node"))
242                 return cmd_kill_btree_node(argc, argv);
243
244         if (!strcmp(cmd, "setattr"))
245                 return cmd_setattr(argc, argv);
246
247 #ifdef BCACHEFS_FUSE
248         if (!strcmp(cmd, "fusemount"))
249                 return cmd_fusemount(argc, argv);
250 #endif
251
252         if (!strcmp(cmd, "--help")) {
253                 usage();
254                 return 0;
255         }
256
257         printf("Unknown command %s\n", cmd);
258 usage:
259         usage();
260         exit(EXIT_FAILURE);
261 }