bcachefs-tools/make-bcache.c

184 lines
3.5 KiB
C
Raw Normal View History

2010-05-08 22:31:53 +04:00
#define _FILE_OFFSET_BITS 64
#define __USE_FILE_OFFSET64
2011-05-20 07:25:14 +04:00
#define _XOPEN_SOURCE 600
2010-05-05 05:58:22 +04:00
2011-07-25 11:18:42 +04:00
#include <ctype.h>
#include <errno.h>
2010-05-05 05:58:22 +04:00
#include <fcntl.h>
#include <linux/fs.h>
2010-12-16 12:22:45 +03:00
#include <stdbool.h>
2010-05-05 05:58:22 +04:00
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
2010-05-08 22:31:53 +04:00
#include <unistd.h>
2010-10-08 18:04:49 +04:00
#include <uuid/uuid.h>
2010-05-05 05:58:22 +04:00
2010-10-08 18:04:49 +04:00
#include "bcache.h"
2010-05-05 05:58:22 +04:00
char zero[4096];
2011-05-20 07:25:14 +04:00
uint64_t getblocks(int fd)
2010-05-05 05:58:22 +04:00
{
2011-05-20 07:25:14 +04:00
uint64_t ret;
2010-05-05 05:58:22 +04:00
struct stat statbuf;
2010-05-08 22:31:53 +04:00
if (fstat(fd, &statbuf)) {
perror("stat error\n");
exit(EXIT_FAILURE);
}
2010-05-14 20:54:26 +04:00
ret = statbuf.st_size / 512;
2010-05-08 22:31:53 +04:00
if (S_ISBLK(statbuf.st_mode))
if (ioctl(fd, BLKGETSIZE, &ret)) {
perror("ioctl error");
exit(EXIT_FAILURE);
}
return ret;
}
2011-05-20 07:25:14 +04:00
uint64_t hatoi(const char *s)
2010-05-08 22:31:53 +04:00
{
char *e;
2011-05-20 07:25:14 +04:00
long long i = strtoll(s, &e, 10);
2010-05-08 22:31:53 +04:00
switch (*e) {
case 't':
case 'T':
i *= 1024;
case 'g':
case 'G':
i *= 1024;
case 'm':
case 'M':
i *= 1024;
case 'k':
case 'K':
i *= 1024;
}
return i;
}
2010-07-30 10:05:51 +04:00
void usage()
{
2010-12-16 12:22:45 +03:00
printf("Usage: make-bcache [options] device\n"
" -C Format a cache device\n"
" -B Format a backing device\n"
" -b bucket size\n"
" -w block size (hard sector size of SSD, often 2k)\n"
2011-07-13 02:42:37 +04:00
" -U UUID\n"
" -S Set UUID\n");
2010-07-30 10:05:51 +04:00
exit(EXIT_FAILURE);
}
2010-05-08 22:31:53 +04:00
int main(int argc, char **argv)
{
2010-12-16 12:22:45 +03:00
bool cache = false, backingdev = false;
2011-07-25 11:18:42 +04:00
int fd, c;
int64_t nblocks;
char uuid[40], set_uuid[40], *dev;
2011-05-20 07:25:14 +04:00
struct cache_sb sb;
memset(&sb, 0, sizeof(struct cache_sb));
2010-05-05 05:58:22 +04:00
2010-10-08 18:04:49 +04:00
uuid_generate(sb.uuid);
uuid_generate(sb.set_uuid);
2010-10-08 18:04:49 +04:00
2011-05-20 07:25:14 +04:00
while ((c = getopt(argc, argv, "CBU:w:b:j:")) != -1)
2010-05-08 22:31:53 +04:00
switch (c) {
2010-12-16 12:22:45 +03:00
case 'C':
cache = true;
break;
case 'B':
backingdev = true;
break;
2010-05-08 22:31:53 +04:00
case 'b':
2011-02-13 18:01:10 +03:00
sb.bucket_size = hatoi(optarg) / 512;
break;
case 'w':
sb.block_size = hatoi(optarg) / 512;
2010-05-08 22:31:53 +04:00
break;
2010-10-08 18:04:49 +04:00
case 'U':
if (uuid_parse(optarg, sb.uuid)) {
printf("Bad uuid\n");
exit(EXIT_FAILURE);
}
break;
2011-07-13 02:42:37 +04:00
case 'S':
if (uuid_parse(optarg, sb.set_uuid)) {
printf("Bad uuid\n");
exit(EXIT_FAILURE);
}
break;
2010-05-08 22:31:53 +04:00
}
2011-05-20 07:25:14 +04:00
if (!sb.block_size)
sb.block_size = 4;
2011-02-13 18:01:10 +03:00
if (!sb.bucket_size)
sb.bucket_size = cache ? 256 : 8192;
2010-12-16 12:22:45 +03:00
if (cache == backingdev) {
printf("Must specify one of -C or -B\n");
usage();
2010-12-16 12:22:45 +03:00
}
2010-05-08 22:31:53 +04:00
if (argc <= optind) {
2010-05-05 05:58:22 +04:00
printf("Please supply a device\n");
exit(EXIT_FAILURE);
}
2011-07-25 11:18:42 +04:00
dev = argv[optind];
fd = open(dev, O_RDWR);
2010-05-08 22:31:53 +04:00
if (fd == -1) {
2011-07-25 11:18:42 +04:00
printf("Can't open dev %s: %s\n", dev, strerror(errno));
2010-05-05 05:58:22 +04:00
exit(EXIT_FAILURE);
}
2010-05-08 22:31:53 +04:00
nblocks = getblocks(fd);
2011-05-20 07:25:14 +04:00
printf("device is %ju sectors\n", nblocks);
2010-05-05 05:58:22 +04:00
2011-07-25 11:18:42 +04:00
sb.offset_this_sb = 8;
2010-05-05 05:58:22 +04:00
memcpy(sb.magic, bcache_magic, 16);
2011-07-25 11:18:42 +04:00
sb.version = backingdev ? CACHE_BACKING_DEV : 0;
sb.nbuckets = nblocks / sb.bucket_size;
sb.nr_in_set = 1;
2010-10-08 18:04:49 +04:00
uuid_unparse(sb.uuid, uuid);
2011-05-20 07:25:14 +04:00
uuid_unparse(sb.set_uuid, set_uuid);
2010-05-05 05:58:22 +04:00
2011-07-25 11:18:42 +04:00
sb.first_bucket = (23 / sb.bucket_size) + 1;
if (cache)
if (sb.bucket_size < sb.block_size ||
sb.bucket_size > nblocks / 8) {
printf("Bad bucket size %i\n", sb.bucket_size);
exit(EXIT_FAILURE);
}
2010-05-05 05:58:22 +04:00
2011-07-25 11:18:42 +04:00
printf("UUID: %s\n"
"Set UUID: %s\n"
"nbuckets: %ju\n"
"block_size: %u\n"
2010-05-05 05:58:22 +04:00
"bucket_size: %u\n"
2011-07-25 11:18:42 +04:00
"nr_in_set: %u\n"
"nr_this_dev: %u\n"
2010-05-05 05:58:22 +04:00
"first_bucket: %u\n"
2011-07-25 11:18:42 +04:00
"sizeof sb: %lu\n",
uuid, set_uuid,
sb.nbuckets,
2010-05-05 05:58:22 +04:00
sb.block_size,
sb.bucket_size,
2011-07-25 11:18:42 +04:00
sb.nr_in_set,
sb.nr_this_dev,
2010-05-05 05:58:22 +04:00
sb.first_bucket,
2011-07-25 11:18:42 +04:00
sizeof(sb));
2010-05-05 05:58:22 +04:00
if (pwrite(fd, &sb, sizeof(sb), 4096) != sizeof(sb))
goto err;
2010-06-15 16:15:32 +04:00
fsync(fd);
2010-05-05 05:58:22 +04:00
exit(EXIT_SUCCESS);
err:
perror("write error\n");
return 1;
}