]> git.sesse.net Git - bcachefs-tools-debian/blob - bcachefs.c
Merge commit '780de81b36'
[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              "\n"
60              "Commands for managing filesystem data:\n"
61              "  data rereplicate     Rereplicate degraded data\n"
62              "\n"
63              "Encryption:\n"
64              "  unlock               Unlock an encrypted filesystem prior to running/mounting\n"
65              "  set-passphrase       Change passphrase on an existing (unmounted) filesystem\n"
66              "  remove-passphrase    Remove passphrase on an existing (unmounted) filesystem\n"
67              "\n"
68              "Migrate:\n"
69              "  migrate              Migrate an existing filesystem to bcachefs, in place\n"
70              "  migrate-superblock   Add default superblock, after bcachefs migrate\n"
71              "\n"
72              "Commands for operating on files in a bcachefs filesystem:\n"
73              "  setattr              Set various per file attributes\n"
74              "Debug:\n"
75              "These commands work on offline, unmounted filesystems\n"
76              "  dump                 Dump filesystem metadata to a qcow2 image\n"
77              "  list                 List filesystem metadata in textual form\n"
78              "\n"
79              "Miscellaneous:\n"
80              "  version              Display the version of the invoked bcachefs tool\n");
81 }
82
83 static char *full_cmd;
84
85 static char *pop_cmd(int *argc, char *argv[])
86 {
87         if (*argc < 2) {
88                 printf("%s: missing command\n", argv[0]);
89                 usage();
90                 exit(EXIT_FAILURE);
91         }
92
93         char *cmd = argv[1];
94         memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
95         (*argc)--;
96
97         full_cmd = mprintf("%s %s", full_cmd, cmd);
98         return cmd;
99 }
100
101 static int fs_cmds(int argc, char *argv[])
102 {
103         char *cmd = pop_cmd(&argc, argv);
104
105         if (!strcmp(cmd, "usage"))
106                 return cmd_fs_usage(argc, argv);
107
108         usage();
109         return 0;
110 }
111
112 static int device_cmds(int argc, char *argv[])
113 {
114         char *cmd = pop_cmd(&argc, argv);
115
116         if (!strcmp(cmd, "add"))
117                 return cmd_device_add(argc, argv);
118         if (!strcmp(cmd, "remove"))
119                 return cmd_device_remove(argc, argv);
120         if (!strcmp(cmd, "online"))
121                 return cmd_device_online(argc, argv);
122         if (!strcmp(cmd, "offline"))
123                 return cmd_device_offline(argc, argv);
124         if (!strcmp(cmd, "evacuate"))
125                 return cmd_device_evacuate(argc, argv);
126         if (!strcmp(cmd, "set-state"))
127                 return cmd_device_set_state(argc, argv);
128         if (!strcmp(cmd, "resize"))
129                 return cmd_device_resize(argc, argv);
130
131         usage();
132         return 0;
133 }
134
135 static int data_cmds(int argc, char *argv[])
136 {
137         char *cmd = pop_cmd(&argc, argv);
138
139         if (!strcmp(cmd, "rereplicate"))
140                 return cmd_data_rereplicate(argc, argv);
141
142         usage();
143         return 0;
144 }
145
146 int main(int argc, char *argv[])
147 {
148         raid_init();
149
150         full_cmd = argv[0];
151
152         setvbuf(stdout, NULL, _IOLBF, 0);
153
154         char *cmd = pop_cmd(&argc, argv);
155
156         if (!strcmp(cmd, "version"))
157                 return cmd_version(argc, argv);
158         if (!strcmp(cmd, "format"))
159                 return cmd_format(argc, argv);
160         if (!strcmp(cmd, "show-super"))
161                 return cmd_show_super(argc, argv);
162
163         if (!strcmp(cmd, "fsck"))
164                 return cmd_fsck(argc, argv);
165
166 #if 0
167         if (!strcmp(cmd, "assemble"))
168                 return cmd_assemble(argc, argv);
169         if (!strcmp(cmd, "incremental"))
170                 return cmd_incremental(argc, argv);
171         if (!strcmp(cmd, "run"))
172                 return cmd_run(argc, argv);
173         if (!strcmp(cmd, "stop"))
174                 return cmd_stop(argc, argv);
175 #endif
176
177         if (!strcmp(cmd, "fs"))
178                 return fs_cmds(argc, argv);
179
180         if (!strcmp(cmd, "device"))
181                 return device_cmds(argc, argv);
182
183         if (!strcmp(cmd, "data"))
184                 return data_cmds(argc, argv);
185
186         if (!strcmp(cmd, "unlock"))
187                 return cmd_unlock(argc, argv);
188         if (!strcmp(cmd, "set-passphrase"))
189                 return cmd_set_passphrase(argc, argv);
190         if (!strcmp(cmd, "remove-passphrase"))
191                 return cmd_remove_passphrase(argc, argv);
192
193         if (!strcmp(cmd, "migrate"))
194                 return cmd_migrate(argc, argv);
195         if (!strcmp(cmd, "migrate-superblock"))
196                 return cmd_migrate_superblock(argc, argv);
197
198         if (!strcmp(cmd, "dump"))
199                 return cmd_dump(argc, argv);
200         if (!strcmp(cmd, "list"))
201                 return cmd_list(argc, argv);
202
203         if (!strcmp(cmd, "setattr"))
204                 return cmd_setattr(argc, argv);
205
206 #ifdef BCACHEFS_FUSE
207         if (!strcmp(cmd, "fusemount"))
208                 return cmd_fusemount(argc, argv);
209 #endif
210
211         if (!strcmp(cmd, "--help")) {
212                 usage();
213                 return 0;
214         }
215
216         printf("Unknown command %s\n", cmd);
217         usage();
218         exit(EXIT_FAILURE);
219 }