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