2016-03-12 09:18:42 +03:00
|
|
|
/*
|
|
|
|
* Authors: Kent Overstreet <kent.overstreet@gmail.com>
|
|
|
|
* Gabriel de Perthuis <g2p.code@gmail.com>
|
|
|
|
* Jacob Malevich <jam@datera.io>
|
|
|
|
*
|
|
|
|
* GPLv2
|
|
|
|
*/
|
2011-07-26 21:02:15 +04:00
|
|
|
|
2016-03-12 09:18:42 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-08-26 06:11:59 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2014-09-26 05:34:24 +04:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
2016-03-12 09:18:42 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2014-09-26 05:34:24 +04:00
|
|
|
#include <stdbool.h>
|
2016-03-12 09:18:42 +03:00
|
|
|
#include <stdint.h>
|
2014-09-26 05:34:24 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2011-07-26 21:02:15 +04:00
|
|
|
|
2018-11-23 08:44:20 +03:00
|
|
|
#include <raid/raid.h>
|
|
|
|
|
2017-02-02 06:16:42 +03:00
|
|
|
#include "cmds.h"
|
2014-08-26 06:11:59 +04:00
|
|
|
|
2016-08-24 06:50:31 +03:00
|
|
|
static void usage(void)
|
|
|
|
{
|
2017-03-20 02:56:34 +03:00
|
|
|
puts("bcachefs - tool for managing bcachefs filesystems\n"
|
|
|
|
"usage: bcachefs <command> [<args>]\n"
|
2016-08-24 06:50:31 +03:00
|
|
|
"\n"
|
2017-03-12 17:53:43 +03:00
|
|
|
"Superblock commands:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" format Format a new filesystem\n"
|
|
|
|
" show-super Dump superblock information to stdout\n"
|
2022-05-26 23:26:53 +03:00
|
|
|
" set-option Set a filesystem option\n"
|
2016-08-24 06:50:31 +03:00
|
|
|
"\n"
|
2023-04-20 19:35:27 +03:00
|
|
|
#ifndef BCACHEFS_NO_RUST
|
2023-03-08 22:57:50 +03:00
|
|
|
"Mount:\n"
|
|
|
|
" mount Mount a filesystem\n"
|
|
|
|
"\n"
|
2023-04-20 19:35:27 +03:00
|
|
|
#endif
|
2017-03-12 17:53:43 +03:00
|
|
|
"Repair:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" fsck Check an existing filesystem for errors\n"
|
2017-03-12 17:53:43 +03:00
|
|
|
"\n"
|
2018-05-17 08:38:57 +03:00
|
|
|
#if 0
|
2021-12-14 01:14:44 +03:00
|
|
|
"Startup/shutdown, assembly of multi device filesystems:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" assemble Assemble an existing multi device filesystem\n"
|
|
|
|
" incremental Incrementally assemble an existing multi device filesystem\n"
|
|
|
|
" run Start a partially assembled filesystem\n"
|
|
|
|
" stop Stop a running filesystem\n"
|
2017-11-26 10:07:34 +03:00
|
|
|
"\n"
|
2021-12-14 01:14:44 +03:00
|
|
|
#endif
|
2017-01-08 12:13:18 +03:00
|
|
|
"Commands for managing a running filesystem:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" fs usage Show disk usage\n"
|
2016-08-24 06:50:31 +03:00
|
|
|
"\n"
|
2017-03-12 17:53:43 +03:00
|
|
|
"Commands for managing devices within a running filesystem:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" device add Add a new device to an existing filesystem\n"
|
|
|
|
" device remove Remove a device from an existing filesystem\n"
|
|
|
|
" device online Re-add an existing member to a filesystem\n"
|
|
|
|
" device offline Take a device offline, without removing it\n"
|
|
|
|
" device evacuate Migrate data off of a specific device\n"
|
|
|
|
" device set-state Mark a device as failed\n"
|
|
|
|
" device resize Resize filesystem on a device\n"
|
2021-10-24 15:52:32 +03:00
|
|
|
" device resize-journal Resize journal on a device\n"
|
2017-01-08 12:13:18 +03:00
|
|
|
"\n"
|
2021-03-04 22:48:21 +03:00
|
|
|
"Commands for managing subvolumes and snapshots:\n"
|
2022-05-26 23:26:53 +03:00
|
|
|
" subvolume create Create a new subvolume\n"
|
|
|
|
" subvolume delete Delete an existing subvolume\n"
|
|
|
|
" subvolume snapshot Create a snapshot\n"
|
2021-03-04 22:48:21 +03:00
|
|
|
"\n"
|
2018-02-08 23:30:19 +03:00
|
|
|
"Commands for managing filesystem data:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" data rereplicate Rereplicate degraded data\n"
|
|
|
|
" data job Kick off low level data jobs\n"
|
2018-02-08 23:30:19 +03:00
|
|
|
"\n"
|
2017-11-09 04:03:59 +03:00
|
|
|
"Encryption:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" unlock Unlock an encrypted filesystem prior to running/mounting\n"
|
|
|
|
" set-passphrase Change passphrase on an existing (unmounted) filesystem\n"
|
|
|
|
" remove-passphrase Remove passphrase on an existing (unmounted) filesystem\n"
|
2017-11-09 04:03:59 +03:00
|
|
|
"\n"
|
2017-03-12 17:53:43 +03:00
|
|
|
"Migrate:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" migrate Migrate an existing filesystem to bcachefs, in place\n"
|
|
|
|
" migrate-superblock Add default superblock, after bcachefs migrate\n"
|
2016-10-04 06:22:17 +03:00
|
|
|
"\n"
|
2018-12-19 23:41:47 +03:00
|
|
|
"Commands for operating on files in a bcachefs filesystem:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" setattr Set various per file attributes\n"
|
2017-02-03 09:45:08 +03:00
|
|
|
"Debug:\n"
|
2017-03-12 17:53:43 +03:00
|
|
|
"These commands work on offline, unmounted filesystems\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" dump Dump filesystem metadata to a qcow2 image\n"
|
2023-04-20 19:35:27 +03:00
|
|
|
#ifndef BCACHEFS_NO_RUST
|
2021-05-24 23:58:37 +03:00
|
|
|
" list List filesystem metadata in textual form\n"
|
2023-04-20 19:35:27 +03:00
|
|
|
#endif
|
2021-05-24 23:58:37 +03:00
|
|
|
" list_journal List contents of journal\n"
|
2018-02-14 12:28:24 +03:00
|
|
|
"\n"
|
|
|
|
"Miscellaneous:\n"
|
2021-05-24 23:58:37 +03:00
|
|
|
" version Display the version of the invoked bcachefs tool\n");
|
2016-03-12 09:18:42 +03:00
|
|
|
}
|
|
|
|
|
2017-03-12 17:53:43 +03:00
|
|
|
static char *full_cmd;
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-12 17:53:43 +03:00
|
|
|
static char *pop_cmd(int *argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *cmd = argv[1];
|
2021-11-01 07:30:13 +03:00
|
|
|
if (!(*argc < 2))
|
|
|
|
memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
|
2017-03-12 17:53:43 +03:00
|
|
|
(*argc)--;
|
|
|
|
|
|
|
|
full_cmd = mprintf("%s %s", full_cmd, cmd);
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fs_cmds(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *cmd = pop_cmd(&argc, argv);
|
|
|
|
|
2023-06-11 03:59:22 +03:00
|
|
|
if (argc < 1) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-12-23 08:50:55 +03:00
|
|
|
if (!strcmp(cmd, "usage"))
|
|
|
|
return cmd_fs_usage(argc, argv);
|
2016-08-24 06:50:31 +03:00
|
|
|
|
2017-03-12 17:53:43 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int device_cmds(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *cmd = pop_cmd(&argc, argv);
|
|
|
|
|
2021-11-16 02:41:28 +03:00
|
|
|
if (argc < 1)
|
2021-11-01 07:30:13 +03:00
|
|
|
return device_usage();
|
2017-03-12 17:53:43 +03:00
|
|
|
if (!strcmp(cmd, "add"))
|
|
|
|
return cmd_device_add(argc, argv);
|
|
|
|
if (!strcmp(cmd, "remove"))
|
|
|
|
return cmd_device_remove(argc, argv);
|
|
|
|
if (!strcmp(cmd, "online"))
|
|
|
|
return cmd_device_online(argc, argv);
|
|
|
|
if (!strcmp(cmd, "offline"))
|
|
|
|
return cmd_device_offline(argc, argv);
|
|
|
|
if (!strcmp(cmd, "evacuate"))
|
2017-12-30 11:36:12 +03:00
|
|
|
return cmd_device_evacuate(argc, argv);
|
2017-03-12 17:53:43 +03:00
|
|
|
if (!strcmp(cmd, "set-state"))
|
|
|
|
return cmd_device_set_state(argc, argv);
|
2017-12-28 09:01:16 +03:00
|
|
|
if (!strcmp(cmd, "resize"))
|
|
|
|
return cmd_device_resize(argc, argv);
|
2020-11-17 18:52:29 +03:00
|
|
|
if (!strcmp(cmd, "resize-journal"))
|
|
|
|
return cmd_device_resize_journal(argc, argv);
|
2017-03-12 17:53:43 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-08 23:30:19 +03:00
|
|
|
static int data_cmds(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *cmd = pop_cmd(&argc, argv);
|
|
|
|
|
2021-11-16 02:41:28 +03:00
|
|
|
if (argc < 1)
|
2021-11-01 07:30:13 +03:00
|
|
|
return data_usage();
|
2018-02-08 23:30:19 +03:00
|
|
|
if (!strcmp(cmd, "rereplicate"))
|
|
|
|
return cmd_data_rereplicate(argc, argv);
|
2021-03-17 04:37:23 +03:00
|
|
|
if (!strcmp(cmd, "job"))
|
|
|
|
return cmd_data_job(argc, argv);
|
2018-02-08 23:30:19 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:48:21 +03:00
|
|
|
static int subvolume_cmds(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *cmd = pop_cmd(&argc, argv);
|
2021-11-16 02:41:28 +03:00
|
|
|
if (argc < 1)
|
2021-11-01 07:30:13 +03:00
|
|
|
return subvolume_usage();
|
2021-03-04 22:48:21 +03:00
|
|
|
if (!strcmp(cmd, "create"))
|
|
|
|
return cmd_subvolume_create(argc, argv);
|
|
|
|
if (!strcmp(cmd, "delete"))
|
|
|
|
return cmd_subvolume_delete(argc, argv);
|
|
|
|
if (!strcmp(cmd, "snapshot"))
|
|
|
|
return cmd_subvolume_snapshot(argc, argv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:53:43 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-11-23 08:44:20 +03:00
|
|
|
raid_init();
|
|
|
|
|
2017-03-12 17:53:43 +03:00
|
|
|
full_cmd = argv[0];
|
|
|
|
|
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
|
2023-02-21 22:39:43 +03:00
|
|
|
char *cmd = pop_cmd(&argc, argv);
|
2023-01-04 06:31:36 +03:00
|
|
|
if (!cmd) {
|
2021-11-01 07:30:13 +03:00
|
|
|
puts("missing command\n");
|
|
|
|
goto usage;
|
|
|
|
}
|
2016-08-24 06:50:31 +03:00
|
|
|
|
2021-11-01 07:30:13 +03:00
|
|
|
/* these subcommands display usage when argc < 2 */
|
|
|
|
if (!strcmp(cmd, "device"))
|
|
|
|
return device_cmds(argc, argv);
|
|
|
|
if (!strcmp(cmd, "fs"))
|
|
|
|
return fs_cmds(argc, argv);
|
|
|
|
if (!strcmp(cmd, "data"))
|
|
|
|
return data_cmds(argc, argv);
|
|
|
|
if (!strcmp(cmd, "subvolume"))
|
|
|
|
return subvolume_cmds(argc, argv);
|
2016-08-24 06:50:31 +03:00
|
|
|
if (!strcmp(cmd, "format"))
|
|
|
|
return cmd_format(argc, argv);
|
2021-11-01 07:30:13 +03:00
|
|
|
if (!strcmp(cmd, "fsck"))
|
|
|
|
return cmd_fsck(argc, argv);
|
2021-11-04 07:38:58 +03:00
|
|
|
if (!strcmp(cmd, "version"))
|
|
|
|
return cmd_version(argc, argv);
|
|
|
|
if (!strcmp(cmd, "show-super"))
|
|
|
|
return cmd_show_super(argc, argv);
|
2022-05-26 23:26:53 +03:00
|
|
|
if (!strcmp(cmd, "set-option"))
|
|
|
|
return cmd_set_option(argc, argv);
|
2021-11-01 07:30:13 +03:00
|
|
|
|
2018-05-17 08:38:57 +03:00
|
|
|
#if 0
|
2016-08-24 06:50:31 +03:00
|
|
|
if (!strcmp(cmd, "assemble"))
|
|
|
|
return cmd_assemble(argc, argv);
|
|
|
|
if (!strcmp(cmd, "incremental"))
|
|
|
|
return cmd_incremental(argc, argv);
|
|
|
|
if (!strcmp(cmd, "run"))
|
|
|
|
return cmd_run(argc, argv);
|
|
|
|
if (!strcmp(cmd, "stop"))
|
|
|
|
return cmd_stop(argc, argv);
|
2018-05-17 08:38:57 +03:00
|
|
|
#endif
|
2016-08-24 06:50:31 +03:00
|
|
|
|
2017-11-09 04:03:59 +03:00
|
|
|
if (!strcmp(cmd, "unlock"))
|
|
|
|
return cmd_unlock(argc, argv);
|
|
|
|
if (!strcmp(cmd, "set-passphrase"))
|
|
|
|
return cmd_set_passphrase(argc, argv);
|
|
|
|
if (!strcmp(cmd, "remove-passphrase"))
|
|
|
|
return cmd_remove_passphrase(argc, argv);
|
|
|
|
|
2017-03-12 17:53:43 +03:00
|
|
|
if (!strcmp(cmd, "migrate"))
|
|
|
|
return cmd_migrate(argc, argv);
|
|
|
|
if (!strcmp(cmd, "migrate-superblock"))
|
|
|
|
return cmd_migrate_superblock(argc, argv);
|
2016-10-04 06:22:17 +03:00
|
|
|
|
2017-02-03 09:45:08 +03:00
|
|
|
if (!strcmp(cmd, "dump"))
|
|
|
|
return cmd_dump(argc, argv);
|
2023-04-20 19:35:27 +03:00
|
|
|
#ifndef BCACHEFS_NO_RUST
|
2017-02-03 09:45:08 +03:00
|
|
|
if (!strcmp(cmd, "list"))
|
|
|
|
return cmd_list(argc, argv);
|
2023-04-20 19:35:27 +03:00
|
|
|
#endif
|
2020-03-25 23:20:45 +03:00
|
|
|
if (!strcmp(cmd, "list_journal"))
|
|
|
|
return cmd_list_journal(argc, argv);
|
2022-04-08 02:25:30 +03:00
|
|
|
if (!strcmp(cmd, "kill_btree_node"))
|
|
|
|
return cmd_kill_btree_node(argc, argv);
|
2017-02-03 09:45:08 +03:00
|
|
|
|
2018-12-19 23:41:47 +03:00
|
|
|
if (!strcmp(cmd, "setattr"))
|
|
|
|
return cmd_setattr(argc, argv);
|
2023-04-20 19:35:27 +03:00
|
|
|
#ifndef BCACHEFS_NO_RUST
|
2023-02-21 22:39:43 +03:00
|
|
|
if (!strcmp(cmd, "mount")) {
|
|
|
|
cmd_mount(argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-04-20 19:35:27 +03:00
|
|
|
#endif
|
2023-02-21 22:39:43 +03:00
|
|
|
|
2019-11-11 23:00:08 +03:00
|
|
|
#ifdef BCACHEFS_FUSE
|
2019-10-01 21:06:58 +03:00
|
|
|
if (!strcmp(cmd, "fusemount"))
|
|
|
|
return cmd_fusemount(argc, argv);
|
2019-11-11 23:00:08 +03:00
|
|
|
#endif
|
2019-10-01 21:06:58 +03:00
|
|
|
|
2018-08-24 07:40:25 +03:00
|
|
|
if (!strcmp(cmd, "--help")) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-20 08:39:19 +03:00
|
|
|
printf("Unknown command %s\n", cmd);
|
2021-11-01 07:30:13 +03:00
|
|
|
usage:
|
2016-08-24 06:50:31 +03:00
|
|
|
usage();
|
2017-03-20 08:39:19 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2014-12-10 05:03:47 +03:00
|
|
|
}
|