2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
24 #include <raid/raid.h>
28 static void usage(void)
30 puts("bcachefs - tool for managing bcachefs filesystems\n"
31 "usage: bcachefs <command> [<args>]\n"
33 "Superblock commands:\n"
34 " format Format a new filesystem\n"
35 " show-super Dump superblock information to stdout\n"
38 " fsck Check an existing filesystem for errors\n"
41 "Startup/shutdown, assembly of multi device filesystems:\n"
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"
48 "Commands for managing a running filesystem:\n"
49 " fs usage Show disk usage\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"
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"
66 "Commands for managing filesystem data:\n"
67 " data rereplicate Rereplicate degraded data\n"
68 " data job Kick off low level data jobs\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"
76 " migrate Migrate an existing filesystem to bcachefs, in place\n"
77 " migrate-superblock Add default superblock, after bcachefs migrate\n"
79 "Commands for operating on files in a bcachefs filesystem:\n"
80 " setattr Set various per file attributes\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"
88 " version Display the version of the invoked bcachefs tool\n");
91 static char *full_cmd;
93 static char *pop_cmd(int *argc, char *argv[])
97 memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
100 full_cmd = mprintf("%s %s", full_cmd, cmd);
104 static int fs_cmds(int argc, char *argv[])
106 char *cmd = pop_cmd(&argc, argv);
110 if (!strcmp(cmd, "usage"))
111 return cmd_fs_usage(argc, argv);
116 static int device_cmds(int argc, char *argv[])
118 char *cmd = pop_cmd(&argc, argv);
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);
142 static int data_cmds(int argc, char *argv[])
144 char *cmd = pop_cmd(&argc, argv);
148 if (!strcmp(cmd, "rereplicate"))
149 return cmd_data_rereplicate(argc, argv);
150 if (!strcmp(cmd, "job"))
151 return cmd_data_job(argc, argv);
156 static int subvolume_cmds(int argc, char *argv[])
158 char *cmd = pop_cmd(&argc, argv);
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);
171 int main(int argc, char *argv[])
177 setvbuf(stdout, NULL, _IOLBF, 0);
179 char *cmd = pop_cmd(&argc, argv);
181 puts("missing command\n");
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 if (!strcmp(cmd, "version"))
199 return cmd_version(argc, argv);
200 if (!strcmp(cmd, "show-super"))
201 return cmd_show_super(argc, argv);
204 printf("%s: missing command\n", argv[0]);
210 if (!strcmp(cmd, "assemble"))
211 return cmd_assemble(argc, argv);
212 if (!strcmp(cmd, "incremental"))
213 return cmd_incremental(argc, argv);
214 if (!strcmp(cmd, "run"))
215 return cmd_run(argc, argv);
216 if (!strcmp(cmd, "stop"))
217 return cmd_stop(argc, argv);
220 if (!strcmp(cmd, "unlock"))
221 return cmd_unlock(argc, argv);
222 if (!strcmp(cmd, "set-passphrase"))
223 return cmd_set_passphrase(argc, argv);
224 if (!strcmp(cmd, "remove-passphrase"))
225 return cmd_remove_passphrase(argc, argv);
227 if (!strcmp(cmd, "migrate"))
228 return cmd_migrate(argc, argv);
229 if (!strcmp(cmd, "migrate-superblock"))
230 return cmd_migrate_superblock(argc, argv);
232 if (!strcmp(cmd, "dump"))
233 return cmd_dump(argc, argv);
234 if (!strcmp(cmd, "list"))
235 return cmd_list(argc, argv);
236 if (!strcmp(cmd, "list_journal"))
237 return cmd_list_journal(argc, argv);
238 if (!strcmp(cmd, "kill_btree_node"))
239 return cmd_kill_btree_node(argc, argv);
241 if (!strcmp(cmd, "setattr"))
242 return cmd_setattr(argc, argv);
245 if (!strcmp(cmd, "fusemount"))
246 return cmd_fusemount(argc, argv);
249 if (!strcmp(cmd, "--help")) {
254 printf("Unknown command %s\n", cmd);