]> git.sesse.net Git - bcachefs-tools-debian/blob - bcachefs.c
Update bcachefs sources to 0906b1fb49 bcachefs: fixes for 32 bit/big endian machines
[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 "cmds.h"
25
26 static void usage(void)
27 {
28         puts("bcachefs - tool for managing bcachefs filesystems\n"
29              "usage: bcachefs <command> [<args>]\n"
30              "\n"
31              "Superblock commands:\n"
32              "  format               Format a new filesystem\n"
33              "  show-super           Dump superblock information to stdout\n"
34              "\n"
35              "Repair:\n"
36              "  fsck                 Check an existing filesystem for errors\n"
37              "\n"
38              "Startup/shutdown, assembly of multi device filesystems:\n"
39 #if 0
40              "  assemble             Assemble an existing multi device filesystem\n"
41              "  incremental          Incrementally assemble an existing multi device filesystem\n"
42              "  run                  Start a partially assembled filesystem\n"
43              "  stop                   Stop a running filesystem\n"
44 #endif
45              "\n"
46              "Commands for managing a running filesystem:\n"
47              "  fs usage             Show disk usage\n"
48              "\n"
49              "Commands for managing devices within a running filesystem:\n"
50              "  device add           Add a new device to an existing filesystem\n"
51              "  device remove        Remove a device from an existing filesystem\n"
52              "  device online        Re-add an existing member to a filesystem\n"
53              "  device offline       Take a device offline, without removing it\n"
54              "  device evacuate      Migrate data off of a specific device\n"
55              "  device set-state     Mark a device as failed\n"
56              "  device resize        Resize filesystem on a device\n"
57              "\n"
58              "Commands for managing filesystem data:\n"
59              "  data rereplicate     Rereplicate degraded data\n"
60              "\n"
61              "Encryption:\n"
62              "  unlock               Unlock an encrypted filesystem prior to running/mounting\n"
63              "  set-passphrase       Change passphrase on an existing (unmounted) filesystem\n"
64              "  remove-passphrase    Remove passphrase on an existing (unmounted) filesystem\n"
65              "\n"
66              "Migrate:\n"
67              "  migrate              Migrate an existing filesystem to bcachefs, in place\n"
68              "  migrate-superblock   Add default superblock, after bcachefs migrate\n"
69              "\n"
70              "Debug:\n"
71              "These commands work on offline, unmounted filesystems\n"
72              "  dump                 Dump filesystem metadata to a qcow2 image\n"
73              "  list                 List filesystem metadata in textual form\n");
74 }
75
76 static char *full_cmd;
77
78 static char *pop_cmd(int *argc, char *argv[])
79 {
80         if (*argc < 2) {
81                 printf("%s: missing command\n", argv[0]);
82                 usage();
83                 exit(EXIT_FAILURE);
84         }
85
86         char *cmd = argv[1];
87         memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
88         (*argc)--;
89
90         full_cmd = mprintf("%s %s", full_cmd, cmd);
91         return cmd;
92 }
93
94 static int fs_cmds(int argc, char *argv[])
95 {
96         char *cmd = pop_cmd(&argc, argv);
97
98         if (!strcmp(cmd, "usage"))
99                 return cmd_fs_usage(argc, argv);
100
101         usage();
102         return 0;
103 }
104
105 static int device_cmds(int argc, char *argv[])
106 {
107         char *cmd = pop_cmd(&argc, argv);
108
109         if (!strcmp(cmd, "add"))
110                 return cmd_device_add(argc, argv);
111         if (!strcmp(cmd, "remove"))
112                 return cmd_device_remove(argc, argv);
113         if (!strcmp(cmd, "online"))
114                 return cmd_device_online(argc, argv);
115         if (!strcmp(cmd, "offline"))
116                 return cmd_device_offline(argc, argv);
117         if (!strcmp(cmd, "evacuate"))
118                 return cmd_device_evacuate(argc, argv);
119         if (!strcmp(cmd, "set-state"))
120                 return cmd_device_set_state(argc, argv);
121         if (!strcmp(cmd, "resize"))
122                 return cmd_device_resize(argc, argv);
123
124         usage();
125         return 0;
126 }
127
128 static int data_cmds(int argc, char *argv[])
129 {
130         char *cmd = pop_cmd(&argc, argv);
131
132         if (!strcmp(cmd, "rereplicate"))
133                 return cmd_data_rereplicate(argc, argv);
134
135         usage();
136         return 0;
137 }
138
139 int main(int argc, char *argv[])
140 {
141         full_cmd = argv[0];
142
143         setvbuf(stdout, NULL, _IOLBF, 0);
144
145         char *cmd = pop_cmd(&argc, argv);
146
147         if (!strcmp(cmd, "format"))
148                 return cmd_format(argc, argv);
149         if (!strcmp(cmd, "show-super"))
150                 return cmd_show_super(argc, argv);
151
152         if (!strcmp(cmd, "fsck"))
153                 return cmd_fsck(argc, argv);
154
155 #if 0
156         if (!strcmp(cmd, "assemble"))
157                 return cmd_assemble(argc, argv);
158         if (!strcmp(cmd, "incremental"))
159                 return cmd_incremental(argc, argv);
160         if (!strcmp(cmd, "run"))
161                 return cmd_run(argc, argv);
162         if (!strcmp(cmd, "stop"))
163                 return cmd_stop(argc, argv);
164 #endif
165
166         if (!strcmp(cmd, "fs"))
167                 return fs_cmds(argc, argv);
168
169         if (!strcmp(cmd, "device"))
170                 return device_cmds(argc, argv);
171
172         if (!strcmp(cmd, "data"))
173                 return data_cmds(argc, argv);
174
175         if (!strcmp(cmd, "unlock"))
176                 return cmd_unlock(argc, argv);
177         if (!strcmp(cmd, "set-passphrase"))
178                 return cmd_set_passphrase(argc, argv);
179         if (!strcmp(cmd, "remove-passphrase"))
180                 return cmd_remove_passphrase(argc, argv);
181
182         if (!strcmp(cmd, "migrate"))
183                 return cmd_migrate(argc, argv);
184         if (!strcmp(cmd, "migrate-superblock"))
185                 return cmd_migrate_superblock(argc, argv);
186
187         if (!strcmp(cmd, "dump"))
188                 return cmd_dump(argc, argv);
189         if (!strcmp(cmd, "list"))
190                 return cmd_list(argc, argv);
191
192         printf("Unknown command %s\n", cmd);
193         usage();
194         exit(EXIT_FAILURE);
195 }