summaryrefslogtreecommitdiff
path: root/bcache.c
blob: b3c8e468502cbb441516dcc195b7937465371d63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
 *	    Gabriel de Perthuis <g2p.code@gmail.com>
 *	    Jacob Malevich <jam@datera.io>
 *
 * GPLv2
 */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "cmds.h"

static void usage(void)
{
	puts("bcache - tool for managing bcache volumes/filesystems\n"
	     "usage: bcache <command> [<args>]\n"
	     "\n"
	     "Commands for formatting, startup and shutdown:\n"
	     "  format         Format a new filesystem\n"
	     "  unlock         Unlock an encrypted filesystem prior to running/mounting\n"
	     "  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"
	     "\n"
	     "Commands for managing a running filesystem:\n"
	     "  fs_show        Show various information about a filesystem\n"
	     "  fs_set         Modify filesystem options\n"
	     "\n"
	     "Commands for managing a specific device in a filesystem:\n"
	     "  device_show    Show information about a formatted device\n"
	     "  device_add     Add a device to an existing (running) filesystem\n"
	     "  device_fail    Mark a device as failed\n"
	     "  device_remove  Remove a device from an existing (running) filesystem\n"
	     "\n"
	     "Repair:\n"
	     "  bcache fsck    Check an existing filesystem for errors\n"
	     "\n"
	     "Debug:\n"
	     "  bcache dump    Dump filesystem metadata to a qcow2 image\n"
	     "  bcache list    List filesystem metadata in textual form\n"
	     "\n"
	     "Migrate:\n"
	     "  bcache migrate Migrate an existing filesystem to bcachefs, in place\n"
	     "  bcache migrate_superblock\n"
	     "                 Add default superblock, after bcache migrate\n");
}

int main(int argc, char *argv[])
{
	char *cmd;

	setvbuf(stdout, NULL, _IOLBF, 0);

	if (argc < 2) {
		printf("%s: missing command\n", argv[0]);
		usage();
		exit(EXIT_FAILURE);
	}

	cmd = argv[1];

	memmove(&argv[1], &argv[2], argc * sizeof(argv[0]));
	argc--;

	if (!strcmp(cmd, "format"))
		return cmd_format(argc, argv);
	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);

	if (!strcmp(cmd, "fs_show"))
		return cmd_fs_show(argc, argv);
	if (!strcmp(cmd, "fs_set"))
		return cmd_fs_set(argc, argv);

	if (!strcmp(cmd, "device_show"))
		return cmd_device_show(argc, argv);
	if (!strcmp(cmd, "device_add"))
		return cmd_device_add(argc, argv);
	if (!strcmp(cmd, "device_fail"))
		return cmd_device_fail(argc, argv);
	if (!strcmp(cmd, "device_remove"))
		return cmd_device_remove(argc, argv);

	if (!strcmp(cmd, "fsck"))
		return cmd_fsck(argc, argv);

	if (!strcmp(cmd, "unlock"))
		return cmd_unlock(argc, argv);

	if (!strcmp(cmd, "dump"))
		return cmd_dump(argc, argv);
	if (!strcmp(cmd, "list"))
		return cmd_list(argc, argv);

	if (!strcmp(cmd, "migrate"))
		return cmd_migrate(argc, argv);
	if (!strcmp(cmd, "migrate_superblock"))
		return cmd_migrate_superblock(argc, argv);

	usage();
	return 0;
}