bcachefs-tools/make-bcache.c

188 lines
3.7 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
#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"
" -j journal size, in buckets\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-05-20 07:25:14 +04:00
int64_t nblocks, journal = 0;
2010-05-08 22:31:53 +04:00
int fd, i, c;
2011-05-20 07:25:14 +04:00
char uuid[40], set_uuid[40];
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;
2011-05-20 07:25:14 +04:00
case 'j':
journal = atoi(optarg);
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);
}
2010-05-08 22:31:53 +04:00
fd = open(argv[optind], O_RDWR);
if (fd == -1) {
2010-05-05 05:58:22 +04:00
perror("Can't open dev\n");
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-02-13 18:01:10 +03:00
if (sb.bucket_size < sb.block_size ||
sb.bucket_size > nblocks / 8) {
printf("Bad bucket size %i\n", sb.bucket_size);
2010-05-05 05:58:22 +04:00
exit(EXIT_FAILURE);
}
memcpy(sb.magic, bcache_magic, 16);
2011-05-20 07:25:14 +04:00
sb.version = backingdev ? CACHE_BACKING_DEV : 0;
2010-05-08 22:31:53 +04:00
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-05-20 07:25:14 +04:00
sb.journal_start = ((sb.nbuckets * sizeof(struct bucket_disk)) + (24 << 9)) / (sb.bucket_size << 9) + 1;
sb.first_bucket = sb.journal_start + journal;
2010-05-05 05:58:22 +04:00
printf("block_size: %u\n"
"bucket_size: %u\n"
"journal_start: %u\n"
"first_bucket: %u\n"
2010-10-08 18:04:49 +04:00
"nbuckets: %ju\n"
2011-05-20 07:25:14 +04:00
"UUID: %s\n"
"Set UUID: %s\n",
2010-05-05 05:58:22 +04:00
sb.block_size,
sb.bucket_size,
sb.journal_start,
sb.first_bucket,
2010-10-08 18:04:49 +04:00
sb.nbuckets,
2011-05-20 07:25:14 +04:00
uuid, set_uuid);
2010-05-05 05:58:22 +04:00
if (!backingdev) {
/* Zero out priorities */
lseek(fd, 4096, SEEK_SET);
for (i = 8; i < sb.first_bucket * sb.bucket_size; i++)
if (write(fd, zero, 512) != 512)
goto err;
}
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;
}