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