1 #define _FILE_OFFSET_BITS 64
2 #define __USE_FILE_OFFSET64
3 #define _XOPEN_SOURCE 600
12 #include <sys/ioctl.h>
13 #include <sys/types.h>
16 #include <uuid/uuid.h>
22 uint64_t getblocks(int fd)
26 if (fstat(fd, &statbuf)) {
27 perror("stat error\n");
30 ret = statbuf.st_size / 512;
31 if (S_ISBLK(statbuf.st_mode))
32 if (ioctl(fd, BLKGETSIZE, &ret)) {
33 perror("ioctl error");
39 uint64_t hatoi(const char *s)
42 long long i = strtoll(s, &e, 10);
62 printf("Usage: make-bcache [options] device\n"
63 " -C Format a cache device\n"
64 " -B Format a backing device\n"
70 int main(int argc, char **argv)
72 bool cache = false, backingdev = false;
73 int64_t nblocks, journal = 0;
75 char uuid[40], set_uuid[40];
78 memset(&sb, 0, sizeof(struct cache_sb));
80 uuid_generate(sb.uuid);
81 uuid_generate(sb.set_uuid);
83 while ((c = getopt(argc, argv, "CBU:w:b:j:")) != -1)
92 sb.bucket_size = hatoi(optarg) / 512;
95 sb.block_size = hatoi(optarg) / 512;
98 journal = atoi(optarg);
101 if (uuid_parse(optarg, sb.uuid)) {
102 printf("Bad uuid\n");
112 sb.bucket_size = cache ? 256 : 8192;
114 if (cache == backingdev) {
115 printf("Must specify one of -C or -B\n");
119 if (argc <= optind) {
120 printf("Please supply a device\n");
124 fd = open(argv[optind], O_RDWR);
126 perror("Can't open dev\n");
129 nblocks = getblocks(fd);
130 printf("device is %ju sectors\n", nblocks);
132 if (sb.bucket_size < sb.block_size ||
133 sb.bucket_size > nblocks / 8) {
134 printf("Bad bucket size %i\n", sb.bucket_size);
138 memcpy(sb.magic, bcache_magic, 16);
139 sb.version = backingdev ? CACHE_BACKING_DEV : 0;
140 sb.nbuckets = nblocks / sb.bucket_size;
142 uuid_unparse(sb.uuid, uuid);
143 uuid_unparse(sb.set_uuid, set_uuid);
145 sb.journal_start = ((sb.nbuckets * sizeof(struct bucket_disk)) + (24 << 9)) / (sb.bucket_size << 9) + 1;
146 sb.first_bucket = sb.journal_start + journal;
148 printf("block_size: %u\n"
150 "journal_start: %u\n"
162 /* Zero out priorities */
163 lseek(fd, 4096, SEEK_SET);
164 for (i = 8; i < sb.first_bucket * sb.bucket_size; i++)
165 if (write(fd, zero, 512) != 512)
168 if (pwrite(fd, &sb, sizeof(sb), 4096) != sizeof(sb))
174 perror("write error\n");