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"
36 " set-option Set a filesystem option\n"
39 " fsck Check an existing filesystem for errors\n"
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"
49 "Commands for managing a running filesystem:\n"
50 " fs usage Show disk usage\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"
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"
67 "Commands for managing filesystem data:\n"
68 " data rereplicate Rereplicate degraded data\n"
69 " data job Kick off low level data jobs\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"
77 " migrate Migrate an existing filesystem to bcachefs, in place\n"
78 " migrate-superblock Add default superblock, after bcachefs migrate\n"
80 "Commands for operating on files in a bcachefs filesystem:\n"
81 " setattr Set various per file attributes\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"
89 " version Display the version of the invoked bcachefs tool\n");
92 static char *full_cmd;
94 static char *pop_cmd(int *argc, char *argv[])
98 memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
101 full_cmd = mprintf("%s %s", full_cmd, cmd);
105 static int fs_cmds(int argc, char *argv[])
107 char *cmd = pop_cmd(&argc, argv);
111 if (!strcmp(cmd, "usage"))
112 return cmd_fs_usage(argc, argv);
117 static int device_cmds(int argc, char *argv[])
119 char *cmd = pop_cmd(&argc, argv);
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);
143 static int data_cmds(int argc, char *argv[])
145 char *cmd = pop_cmd(&argc, argv);
149 if (!strcmp(cmd, "rereplicate"))
150 return cmd_data_rereplicate(argc, argv);
151 if (!strcmp(cmd, "job"))
152 return cmd_data_job(argc, argv);
157 static int subvolume_cmds(int argc, char *argv[])
159 char *cmd = pop_cmd(&argc, argv);
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);
172 int main(int argc, char *argv[])
178 setvbuf(stdout, NULL, _IOLBF, 0);
180 char *cmd = pop_cmd(&argc, argv);
182 puts("missing command\n");
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);
207 if (!strcmp(cmd, "assemble"))
208 return cmd_assemble(argc, argv);
209 if (!strcmp(cmd, "incremental"))
210 return cmd_incremental(argc, argv);
211 if (!strcmp(cmd, "run"))
212 return cmd_run(argc, argv);
213 if (!strcmp(cmd, "stop"))
214 return cmd_stop(argc, argv);
217 if (!strcmp(cmd, "unlock"))
218 return cmd_unlock(argc, argv);
219 if (!strcmp(cmd, "set-passphrase"))
220 return cmd_set_passphrase(argc, argv);
221 if (!strcmp(cmd, "remove-passphrase"))
222 return cmd_remove_passphrase(argc, argv);
224 if (!strcmp(cmd, "migrate"))
225 return cmd_migrate(argc, argv);
226 if (!strcmp(cmd, "migrate-superblock"))
227 return cmd_migrate_superblock(argc, argv);
229 if (!strcmp(cmd, "dump"))
230 return cmd_dump(argc, argv);
231 if (!strcmp(cmd, "list"))
232 return cmd_list(argc, argv);
233 if (!strcmp(cmd, "list_journal"))
234 return cmd_list_journal(argc, argv);
235 if (!strcmp(cmd, "kill_btree_node"))
236 return cmd_kill_btree_node(argc, argv);
238 if (!strcmp(cmd, "setattr"))
239 return cmd_setattr(argc, argv);
241 if (!strcmp(cmd, "mount")) {
242 cmd_mount(argc, argv);
247 if (!strcmp(cmd, "fusemount"))
248 return cmd_fusemount(argc, argv);
251 if (!strcmp(cmd, "--help")) {
256 printf("Unknown command %s\n", cmd);