2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
17 #include <sys/types.h>
21 #include <uuid/uuid.h>
23 #include "ccan/darray/darray.h"
26 #include "libbcache.h"
30 /* Open a block device, do magic blkid stuff: */
31 static int open_for_format(const char *dev, bool force)
34 const char *fs_type = NULL, *fs_label = NULL;
35 size_t fs_type_len, fs_label_len;
38 if ((fd = open(dev, O_RDWR|O_EXCL)) == -1)
39 die("Can't open dev %s: %s\n", dev, strerror(errno));
44 if (!(pr = blkid_new_probe()))
46 if (blkid_probe_set_device(pr, fd, 0, 0))
48 if (blkid_probe_enable_partitions(pr, true))
50 if (blkid_do_fullprobe(pr) < 0)
53 blkid_probe_lookup_value(pr, "TYPE", &fs_type, &fs_type_len);
54 blkid_probe_lookup_value(pr, "LABEL", &fs_label, &fs_label_len);
58 printf("%s contains a %s filesystem labelled '%s'\n",
59 dev, fs_type, fs_label);
61 printf("%s contains a %s filesystem\n",
63 fputs("Proceed anyway?", stdout);
72 static void usage(void)
74 puts("bcache format - create a new bcache filesystem on one or more devices\n"
75 "Usage: bcache format [OPTION]... <devices>\n"
79 " --btree_node=size Btree node size, default 256k\n"
80 " --metadata_checksum_type=(none|crc32c|crc64)\n"
81 " --data_checksum_type=(none|crc32c|crc64)\n"
82 " --compression_type=(none|lz4|gzip)\n"
83 " --error_action=(continue|readonly|panic)\n"
84 " Action to take on filesystem error\n"
85 " --max_journal_entry_size=size\n"
86 " -l, --label=label\n"
90 "Device specific options:\n"
91 " --fs_size=size Size of filesystem on device\n"
92 " --bucket=size bucket size\n"
93 " --discard Enable discards\n"
94 " -t, --tier=# tier of subsequent devices\n"
96 " -h, --help display this help and exit\n"
98 "Device specific options must come before corresponding devices, e.g.\n"
99 " bcache format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
101 "Report bugs to <linux-bcache@vger.kernel.org>");
105 OPT('b', block_size, required_argument) \
106 OPT(0, btree_node_size, required_argument) \
107 OPT(0, metadata_checksum_type, required_argument) \
108 OPT(0, data_checksum_type, required_argument) \
109 OPT(0, compression_type, required_argument) \
110 OPT('e', error_action, required_argument) \
111 OPT(0, max_journal_entry_size, required_argument) \
112 OPT('L', label, required_argument) \
113 OPT('U', uuid, required_argument) \
114 OPT('f', force, no_argument) \
115 OPT(0, fs_size, required_argument) \
116 OPT(0, bucket_size, required_argument) \
117 OPT('t', tier, required_argument) \
118 OPT(0, discard, no_argument) \
119 OPT('h', help, no_argument)
123 #define OPT(shortopt, longopt, has_arg) Opt_##longopt,
128 static const struct option format_opts[] = {
129 #define OPT(shortopt, longopt, has_arg) { \
130 #longopt, has_arg, NULL, Opt_##longopt \
137 static unsigned hatoi_validate(const char *s, const char *msg)
141 if (bch_strtoull_h(s, &v))
142 die("bad %s %s", msg, s);
145 die("%s must be a power of two", msg);
150 die("%s too large\n", msg);
153 die("%s too small\n", msg);
158 int cmd_format(int argc, char *argv[])
160 darray(struct dev_opts) devices;
161 struct dev_opts *dev;
162 unsigned block_size = 0;
163 unsigned btree_node_size = 0;
164 unsigned meta_csum_type = BCH_CSUM_CRC32C;
165 unsigned data_csum_type = BCH_CSUM_CRC32C;
166 unsigned compression_type = BCH_COMPRESSION_NONE;
167 unsigned on_error_action = BCH_ON_ERROR_RO;
172 /* Device specific options: */
173 u64 filesystem_size = 0;
174 unsigned bucket_size = 0;
176 bool discard = false;
177 unsigned max_journal_entry_size = 0;
178 char *passphrase = NULL;
181 darray_init(devices);
184 while ((opt = getopt_long(argc, argv,
191 block_size = hatoi_validate(optarg,
194 case Opt_btree_node_size:
195 btree_node_size = hatoi_validate(optarg,
198 case Opt_metadata_checksum_type:
199 meta_csum_type = read_string_list_or_die(optarg,
200 bch_csum_types, "checksum type");
202 case Opt_data_checksum_type:
203 data_csum_type = read_string_list_or_die(optarg,
204 bch_csum_types, "checksum type");
206 case Opt_compression_type:
207 compression_type = read_string_list_or_die(optarg,
208 bch_compression_types,
211 case Opt_error_action:
213 on_error_action = read_string_list_or_die(optarg,
214 bch_error_actions, "error action");
216 case Opt_max_journal_entry_size:
217 max_journal_entry_size = hatoi_validate(optarg,
218 "journal entry size");
222 label = strdup(optarg);
226 if (uuid_parse(optarg, uuid.b))
234 if (bch_strtoull_h(optarg, &filesystem_size))
235 die("invalid filesystem size");
237 filesystem_size >>= 9;
239 case Opt_bucket_size:
240 bucket_size = hatoi_validate(optarg, "bucket size");
244 if (kstrtouint(optarg, 10, &tier) ||
252 darray_append(devices, (struct dev_opts) {
253 .path = strdup(optarg),
254 .size = filesystem_size,
255 .bucket_size = bucket_size,
267 if (!darray_size(devices))
268 die("Please supply a device");
270 if (uuid_is_null(uuid.b))
271 uuid_generate(uuid.b);
273 darray_foreach(dev, devices)
274 dev->fd = open_for_format(dev->path, force);
276 bcache_format(devices.item, darray_size(devices),
285 max_journal_entry_size,
290 memzero_explicit(passphrase, strlen(passphrase));