blob: b057676fc67de31b2623a62fb4487976975909cc [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "btree.h"
11#include "debug.h"
12#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070013#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
Kent Overstreetc37511b2013-04-26 15:39:55 -070015#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070016#include <linux/buffer_head.h>
17#include <linux/debugfs.h>
18#include <linux/genhd.h>
Kent Overstreet28935ab2013-07-31 01:12:02 -070019#include <linux/idr.h>
Kent Overstreet79826c32013-07-10 18:31:58 -070020#include <linux/kthread.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070021#include <linux/module.h>
22#include <linux/random.h>
23#include <linux/reboot.h>
24#include <linux/sysfs.h>
25
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
28
29static const char bcache_magic[] = {
30 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
31 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
32};
33
34static const char invalid_uuid[] = {
35 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
36 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
37};
38
39/* Default is -1; we skip past it for struct cached_dev's cache mode */
40const char * const bch_cache_modes[] = {
41 "default",
42 "writethrough",
43 "writeback",
44 "writearound",
45 "none",
46 NULL
47};
48
Kent Overstreetcafe5632013-03-23 16:11:31 -070049static struct kobject *bcache_kobj;
50struct mutex bch_register_lock;
51LIST_HEAD(bch_cache_sets);
52static LIST_HEAD(uncached_devices);
53
Kent Overstreet28935ab2013-07-31 01:12:02 -070054static int bcache_major;
55static DEFINE_IDA(bcache_minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -070056static wait_queue_head_t unregister_wait;
57struct workqueue_struct *bcache_wq;
58
59#define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
60
61static void bio_split_pool_free(struct bio_split_pool *p)
62{
Kent Overstreet8ef74792013-04-05 13:46:13 -070063 if (p->bio_split_hook)
64 mempool_destroy(p->bio_split_hook);
65
Kent Overstreetcafe5632013-03-23 16:11:31 -070066 if (p->bio_split)
67 bioset_free(p->bio_split);
Kent Overstreetcafe5632013-03-23 16:11:31 -070068}
69
70static int bio_split_pool_init(struct bio_split_pool *p)
71{
72 p->bio_split = bioset_create(4, 0);
73 if (!p->bio_split)
74 return -ENOMEM;
75
76 p->bio_split_hook = mempool_create_kmalloc_pool(4,
77 sizeof(struct bio_split_hook));
78 if (!p->bio_split_hook)
79 return -ENOMEM;
80
81 return 0;
82}
83
84/* Superblock */
85
86static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
87 struct page **res)
88{
89 const char *err;
90 struct cache_sb *s;
91 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
92 unsigned i;
93
94 if (!bh)
95 return "IO error";
96
97 s = (struct cache_sb *) bh->b_data;
98
99 sb->offset = le64_to_cpu(s->offset);
100 sb->version = le64_to_cpu(s->version);
101
102 memcpy(sb->magic, s->magic, 16);
103 memcpy(sb->uuid, s->uuid, 16);
104 memcpy(sb->set_uuid, s->set_uuid, 16);
105 memcpy(sb->label, s->label, SB_LABEL_SIZE);
106
107 sb->flags = le64_to_cpu(s->flags);
108 sb->seq = le64_to_cpu(s->seq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700109 sb->last_mount = le32_to_cpu(s->last_mount);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700110 sb->first_bucket = le16_to_cpu(s->first_bucket);
111 sb->keys = le16_to_cpu(s->keys);
112
113 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
114 sb->d[i] = le64_to_cpu(s->d[i]);
115
116 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
117 sb->version, sb->flags, sb->seq, sb->keys);
118
119 err = "Not a bcache superblock";
120 if (sb->offset != SB_SECTOR)
121 goto err;
122
123 if (memcmp(sb->magic, bcache_magic, 16))
124 goto err;
125
126 err = "Too many journal buckets";
127 if (sb->keys > SB_JOURNAL_BUCKETS)
128 goto err;
129
130 err = "Bad checksum";
131 if (s->csum != csum_set(s))
132 goto err;
133
134 err = "Bad UUID";
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600135 if (bch_is_zero(sb->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700136 goto err;
137
Kent Overstreet8abb2a52013-04-23 21:51:48 -0700138 sb->block_size = le16_to_cpu(s->block_size);
139
140 err = "Superblock block size smaller than device block size";
141 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
142 goto err;
143
Kent Overstreet29033812013-04-11 15:14:35 -0700144 switch (sb->version) {
145 case BCACHE_SB_VERSION_BDEV:
Kent Overstreet29033812013-04-11 15:14:35 -0700146 sb->data_offset = BDEV_DATA_START_DEFAULT;
147 break;
148 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
Kent Overstreet29033812013-04-11 15:14:35 -0700149 sb->data_offset = le64_to_cpu(s->data_offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700150
Kent Overstreet29033812013-04-11 15:14:35 -0700151 err = "Bad data offset";
152 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700153 goto err;
154
Kent Overstreet29033812013-04-11 15:14:35 -0700155 break;
156 case BCACHE_SB_VERSION_CDEV:
157 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
158 sb->nbuckets = le64_to_cpu(s->nbuckets);
159 sb->block_size = le16_to_cpu(s->block_size);
160 sb->bucket_size = le16_to_cpu(s->bucket_size);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700161
Kent Overstreet29033812013-04-11 15:14:35 -0700162 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
163 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
164
165 err = "Too many buckets";
166 if (sb->nbuckets > LONG_MAX)
167 goto err;
168
169 err = "Not enough buckets";
170 if (sb->nbuckets < 1 << 7)
171 goto err;
172
173 err = "Bad block/bucket size";
174 if (!is_power_of_2(sb->block_size) ||
175 sb->block_size > PAGE_SECTORS ||
176 !is_power_of_2(sb->bucket_size) ||
177 sb->bucket_size < PAGE_SECTORS)
178 goto err;
179
180 err = "Invalid superblock: device too small";
181 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
182 goto err;
183
184 err = "Bad UUID";
185 if (bch_is_zero(sb->set_uuid, 16))
186 goto err;
187
188 err = "Bad cache device number in set";
189 if (!sb->nr_in_set ||
190 sb->nr_in_set <= sb->nr_this_dev ||
191 sb->nr_in_set > MAX_CACHES_PER_SET)
192 goto err;
193
194 err = "Journal buckets not sequential";
195 for (i = 0; i < sb->keys; i++)
196 if (sb->d[i] != sb->first_bucket + i)
197 goto err;
198
199 err = "Too many journal buckets";
200 if (sb->first_bucket + sb->keys > sb->nbuckets)
201 goto err;
202
203 err = "Invalid superblock: first bucket comes before end of super";
204 if (sb->first_bucket * sb->bucket_size < 16)
205 goto err;
206
207 break;
208 default:
209 err = "Unsupported superblock version";
Kent Overstreetcafe5632013-03-23 16:11:31 -0700210 goto err;
Kent Overstreet29033812013-04-11 15:14:35 -0700211 }
212
Kent Overstreetcafe5632013-03-23 16:11:31 -0700213 sb->last_mount = get_seconds();
214 err = NULL;
215
216 get_page(bh->b_page);
217 *res = bh->b_page;
218err:
219 put_bh(bh);
220 return err;
221}
222
223static void write_bdev_super_endio(struct bio *bio, int error)
224{
225 struct cached_dev *dc = bio->bi_private;
226 /* XXX: error checking */
227
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800228 closure_put(&dc->sb_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700229}
230
231static void __write_super(struct cache_sb *sb, struct bio *bio)
232{
233 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page);
234 unsigned i;
235
Kent Overstreet4f024f32013-10-11 15:44:27 -0700236 bio->bi_iter.bi_sector = SB_SECTOR;
237 bio->bi_rw = REQ_SYNC|REQ_META;
238 bio->bi_iter.bi_size = SB_SIZE;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600239 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700240
241 out->offset = cpu_to_le64(sb->offset);
242 out->version = cpu_to_le64(sb->version);
243
244 memcpy(out->uuid, sb->uuid, 16);
245 memcpy(out->set_uuid, sb->set_uuid, 16);
246 memcpy(out->label, sb->label, SB_LABEL_SIZE);
247
248 out->flags = cpu_to_le64(sb->flags);
249 out->seq = cpu_to_le64(sb->seq);
250
251 out->last_mount = cpu_to_le32(sb->last_mount);
252 out->first_bucket = cpu_to_le16(sb->first_bucket);
253 out->keys = cpu_to_le16(sb->keys);
254
255 for (i = 0; i < sb->keys; i++)
256 out->d[i] = cpu_to_le64(sb->d[i]);
257
258 out->csum = csum_set(out);
259
260 pr_debug("ver %llu, flags %llu, seq %llu",
261 sb->version, sb->flags, sb->seq);
262
263 submit_bio(REQ_WRITE, bio);
264}
265
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800266static void bch_write_bdev_super_unlock(struct closure *cl)
267{
268 struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
269
270 up(&dc->sb_write_mutex);
271}
272
Kent Overstreetcafe5632013-03-23 16:11:31 -0700273void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
274{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800275 struct closure *cl = &dc->sb_write;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700276 struct bio *bio = &dc->sb_bio;
277
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800278 down(&dc->sb_write_mutex);
279 closure_init(cl, parent);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700280
281 bio_reset(bio);
282 bio->bi_bdev = dc->bdev;
283 bio->bi_end_io = write_bdev_super_endio;
284 bio->bi_private = dc;
285
286 closure_get(cl);
287 __write_super(&dc->sb, bio);
288
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800289 closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700290}
291
292static void write_super_endio(struct bio *bio, int error)
293{
294 struct cache *ca = bio->bi_private;
295
296 bch_count_io_errors(ca, error, "writing superblock");
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800297 closure_put(&ca->set->sb_write);
298}
299
300static void bcache_write_super_unlock(struct closure *cl)
301{
302 struct cache_set *c = container_of(cl, struct cache_set, sb_write);
303
304 up(&c->sb_write_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700305}
306
307void bcache_write_super(struct cache_set *c)
308{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800309 struct closure *cl = &c->sb_write;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700310 struct cache *ca;
311 unsigned i;
312
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800313 down(&c->sb_write_mutex);
314 closure_init(cl, &c->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700315
316 c->sb.seq++;
317
318 for_each_cache(ca, c, i) {
319 struct bio *bio = &ca->sb_bio;
320
Kent Overstreet29033812013-04-11 15:14:35 -0700321 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700322 ca->sb.seq = c->sb.seq;
323 ca->sb.last_mount = c->sb.last_mount;
324
325 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
326
327 bio_reset(bio);
328 bio->bi_bdev = ca->bdev;
329 bio->bi_end_io = write_super_endio;
330 bio->bi_private = ca;
331
332 closure_get(cl);
333 __write_super(&ca->sb, bio);
334 }
335
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800336 closure_return_with_destructor(cl, bcache_write_super_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700337}
338
339/* UUID io */
340
341static void uuid_endio(struct bio *bio, int error)
342{
343 struct closure *cl = bio->bi_private;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800344 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700345
346 cache_set_err_on(error, c, "accessing uuids");
347 bch_bbio_free(bio, c);
348 closure_put(cl);
349}
350
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800351static void uuid_io_unlock(struct closure *cl)
352{
353 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
354
355 up(&c->uuid_write_mutex);
356}
357
Kent Overstreetcafe5632013-03-23 16:11:31 -0700358static void uuid_io(struct cache_set *c, unsigned long rw,
359 struct bkey *k, struct closure *parent)
360{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800361 struct closure *cl = &c->uuid_write;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700362 struct uuid_entry *u;
363 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -0700364 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700365
366 BUG_ON(!parent);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800367 down(&c->uuid_write_mutex);
368 closure_init(cl, parent);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700369
370 for (i = 0; i < KEY_PTRS(k); i++) {
371 struct bio *bio = bch_bbio_alloc(c);
372
373 bio->bi_rw = REQ_SYNC|REQ_META|rw;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700374 bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700375
376 bio->bi_end_io = uuid_endio;
377 bio->bi_private = cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600378 bch_bio_map(bio, c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700379
380 bch_submit_bbio(bio, c, k, i);
381
382 if (!(rw & WRITE))
383 break;
384 }
385
Kent Overstreet85b14922013-05-14 20:33:16 -0700386 bch_bkey_to_text(buf, sizeof(buf), k);
387 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700388
389 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600390 if (!bch_is_zero(u->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700391 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
392 u - c->uuids, u->uuid, u->label,
393 u->first_reg, u->last_reg, u->invalidated);
394
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800395 closure_return_with_destructor(cl, uuid_io_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700396}
397
398static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
399{
400 struct bkey *k = &j->uuid_bucket;
401
Kent Overstreetd5cc66e2013-07-24 23:06:40 -0700402 if (bch_btree_ptr_invalid(c, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700403 return "bad uuid pointer";
404
405 bkey_copy(&c->uuid_bucket, k);
406 uuid_io(c, READ_SYNC, k, cl);
407
408 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
409 struct uuid_entry_v0 *u0 = (void *) c->uuids;
410 struct uuid_entry *u1 = (void *) c->uuids;
411 int i;
412
413 closure_sync(cl);
414
415 /*
416 * Since the new uuid entry is bigger than the old, we have to
417 * convert starting at the highest memory address and work down
418 * in order to do it in place
419 */
420
421 for (i = c->nr_uuids - 1;
422 i >= 0;
423 --i) {
424 memcpy(u1[i].uuid, u0[i].uuid, 16);
425 memcpy(u1[i].label, u0[i].label, 32);
426
427 u1[i].first_reg = u0[i].first_reg;
428 u1[i].last_reg = u0[i].last_reg;
429 u1[i].invalidated = u0[i].invalidated;
430
431 u1[i].flags = 0;
432 u1[i].sectors = 0;
433 }
434 }
435
436 return NULL;
437}
438
439static int __uuid_write(struct cache_set *c)
440{
441 BKEY_PADDED(key) k;
442 struct closure cl;
443 closure_init_stack(&cl);
444
445 lockdep_assert_held(&bch_register_lock);
446
Kent Overstreet35fcd842013-07-24 17:29:09 -0700447 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700448 return 1;
449
450 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
451 uuid_io(c, REQ_WRITE, &k.key, &cl);
452 closure_sync(&cl);
453
454 bkey_copy(&c->uuid_bucket, &k.key);
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700455 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700456 return 0;
457}
458
459int bch_uuid_write(struct cache_set *c)
460{
461 int ret = __uuid_write(c);
462
463 if (!ret)
464 bch_journal_meta(c, NULL);
465
466 return ret;
467}
468
469static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
470{
471 struct uuid_entry *u;
472
473 for (u = c->uuids;
474 u < c->uuids + c->nr_uuids; u++)
475 if (!memcmp(u->uuid, uuid, 16))
476 return u;
477
478 return NULL;
479}
480
481static struct uuid_entry *uuid_find_empty(struct cache_set *c)
482{
483 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
484 return uuid_find(c, zero_uuid);
485}
486
487/*
488 * Bucket priorities/gens:
489 *
490 * For each bucket, we store on disk its
491 * 8 bit gen
492 * 16 bit priority
493 *
494 * See alloc.c for an explanation of the gen. The priority is used to implement
495 * lru (and in the future other) cache replacement policies; for most purposes
496 * it's just an opaque integer.
497 *
498 * The gens and the priorities don't have a whole lot to do with each other, and
499 * it's actually the gens that must be written out at specific times - it's no
500 * big deal if the priorities don't get written, if we lose them we just reuse
501 * buckets in suboptimal order.
502 *
503 * On disk they're stored in a packed array, and in as many buckets are required
504 * to fit them all. The buckets we use to store them form a list; the journal
505 * header points to the first bucket, the first bucket points to the second
506 * bucket, et cetera.
507 *
508 * This code is used by the allocation code; periodically (whenever it runs out
509 * of buckets to allocate from) the allocation code will invalidate some
510 * buckets, but it can't use those buckets until their new gens are safely on
511 * disk.
512 */
513
514static void prio_endio(struct bio *bio, int error)
515{
516 struct cache *ca = bio->bi_private;
517
518 cache_set_err_on(error, ca->set, "accessing priorities");
519 bch_bbio_free(bio, ca->set);
520 closure_put(&ca->prio);
521}
522
523static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
524{
525 struct closure *cl = &ca->prio;
526 struct bio *bio = bch_bbio_alloc(ca->set);
527
528 closure_init_stack(cl);
529
Kent Overstreet4f024f32013-10-11 15:44:27 -0700530 bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size;
531 bio->bi_bdev = ca->bdev;
532 bio->bi_rw = REQ_SYNC|REQ_META|rw;
533 bio->bi_iter.bi_size = bucket_bytes(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700534
535 bio->bi_end_io = prio_endio;
536 bio->bi_private = ca;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600537 bch_bio_map(bio, ca->disk_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700538
539 closure_bio_submit(bio, &ca->prio, ca);
540 closure_sync(cl);
541}
542
543#define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
544 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
545
546void bch_prio_write(struct cache *ca)
547{
548 int i;
549 struct bucket *b;
550 struct closure cl;
551
552 closure_init_stack(&cl);
553
554 lockdep_assert_held(&ca->set->bucket_lock);
555
556 for (b = ca->buckets;
557 b < ca->buckets + ca->sb.nbuckets; b++)
558 b->disk_gen = b->gen;
559
560 ca->disk_buckets->seq++;
561
562 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
563 &ca->meta_sectors_written);
564
565 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
566 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700567
568 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
569 long bucket;
570 struct prio_set *p = ca->disk_buckets;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700571 struct bucket_disk *d = p->data;
572 struct bucket_disk *end = d + prios_per_bucket(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700573
574 for (b = ca->buckets + i * prios_per_bucket(ca);
575 b < ca->buckets + ca->sb.nbuckets && d < end;
576 b++, d++) {
577 d->prio = cpu_to_le16(b->prio);
578 d->gen = b->gen;
579 }
580
581 p->next_bucket = ca->prio_buckets[i + 1];
Kent Overstreet81ab4192013-10-31 15:46:42 -0700582 p->magic = pset_magic(&ca->sb);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600583 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700584
Kent Overstreet35fcd842013-07-24 17:29:09 -0700585 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700586 BUG_ON(bucket == -1);
587
588 mutex_unlock(&ca->set->bucket_lock);
589 prio_io(ca, bucket, REQ_WRITE);
590 mutex_lock(&ca->set->bucket_lock);
591
592 ca->prio_buckets[i] = bucket;
593 atomic_dec_bug(&ca->buckets[bucket].pin);
594 }
595
596 mutex_unlock(&ca->set->bucket_lock);
597
598 bch_journal_meta(ca->set, &cl);
599 closure_sync(&cl);
600
601 mutex_lock(&ca->set->bucket_lock);
602
603 ca->need_save_prio = 0;
604
605 /*
606 * Don't want the old priorities to get garbage collected until after we
607 * finish writing the new ones, and they're journalled
608 */
609 for (i = 0; i < prio_buckets(ca); i++)
610 ca->prio_last_buckets[i] = ca->prio_buckets[i];
611}
612
613static void prio_read(struct cache *ca, uint64_t bucket)
614{
615 struct prio_set *p = ca->disk_buckets;
616 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
617 struct bucket *b;
618 unsigned bucket_nr = 0;
619
620 for (b = ca->buckets;
621 b < ca->buckets + ca->sb.nbuckets;
622 b++, d++) {
623 if (d == end) {
624 ca->prio_buckets[bucket_nr] = bucket;
625 ca->prio_last_buckets[bucket_nr] = bucket;
626 bucket_nr++;
627
628 prio_io(ca, bucket, READ_SYNC);
629
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600630 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700631 pr_warn("bad csum reading priorities");
632
Kent Overstreet81ab4192013-10-31 15:46:42 -0700633 if (p->magic != pset_magic(&ca->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700634 pr_warn("bad magic reading priorities");
635
636 bucket = p->next_bucket;
637 d = p->data;
638 }
639
640 b->prio = le16_to_cpu(d->prio);
641 b->gen = b->disk_gen = b->last_gc = b->gc_gen = d->gen;
642 }
643}
644
645/* Bcache device */
646
647static int open_dev(struct block_device *b, fmode_t mode)
648{
649 struct bcache_device *d = b->bd_disk->private_data;
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700650 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700651 return -ENXIO;
652
653 closure_get(&d->cl);
654 return 0;
655}
656
Emil Goode867e1162013-05-09 22:39:26 +0200657static void release_dev(struct gendisk *b, fmode_t mode)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700658{
659 struct bcache_device *d = b->private_data;
660 closure_put(&d->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700661}
662
663static int ioctl_dev(struct block_device *b, fmode_t mode,
664 unsigned int cmd, unsigned long arg)
665{
666 struct bcache_device *d = b->bd_disk->private_data;
667 return d->ioctl(d, mode, cmd, arg);
668}
669
670static const struct block_device_operations bcache_ops = {
671 .open = open_dev,
672 .release = release_dev,
673 .ioctl = ioctl_dev,
674 .owner = THIS_MODULE,
675};
676
677void bcache_device_stop(struct bcache_device *d)
678{
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700679 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700680 closure_queue(&d->cl);
681}
682
Kent Overstreetee668502013-02-01 07:29:41 -0800683static void bcache_device_unlink(struct bcache_device *d)
684{
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700685 lockdep_assert_held(&bch_register_lock);
Kent Overstreetee668502013-02-01 07:29:41 -0800686
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700687 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
688 unsigned i;
689 struct cache *ca;
Kent Overstreetee668502013-02-01 07:29:41 -0800690
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700691 sysfs_remove_link(&d->c->kobj, d->name);
692 sysfs_remove_link(&d->kobj, "cache");
693
694 for_each_cache(ca, d->c, i)
695 bd_unlink_disk_holder(ca->bdev, d->disk);
696 }
Kent Overstreetee668502013-02-01 07:29:41 -0800697}
698
699static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
700 const char *name)
701{
702 unsigned i;
703 struct cache *ca;
704
705 for_each_cache(ca, d->c, i)
706 bd_link_disk_holder(ca->bdev, d->disk);
707
708 snprintf(d->name, BCACHEDEVNAME_SIZE,
709 "%s%u", name, d->id);
710
711 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
712 sysfs_create_link(&c->kobj, &d->kobj, d->name),
713 "Couldn't create device <-> cache set symlinks");
714}
715
Kent Overstreetcafe5632013-03-23 16:11:31 -0700716static void bcache_device_detach(struct bcache_device *d)
717{
718 lockdep_assert_held(&bch_register_lock);
719
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700720 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700721 struct uuid_entry *u = d->c->uuids + d->id;
722
723 SET_UUID_FLASH_ONLY(u, 0);
724 memcpy(u->uuid, invalid_uuid, 16);
725 u->invalidated = cpu_to_le32(get_seconds());
726 bch_uuid_write(d->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700727 }
728
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700729 bcache_device_unlink(d);
Kent Overstreetee668502013-02-01 07:29:41 -0800730
Kent Overstreetcafe5632013-03-23 16:11:31 -0700731 d->c->devices[d->id] = NULL;
732 closure_put(&d->c->caching);
733 d->c = NULL;
734}
735
736static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
737 unsigned id)
738{
739 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
740
741 d->id = id;
742 d->c = c;
743 c->devices[id] = d;
744
745 closure_get(&c->caching);
746}
747
Kent Overstreetcafe5632013-03-23 16:11:31 -0700748static void bcache_device_free(struct bcache_device *d)
749{
750 lockdep_assert_held(&bch_register_lock);
751
752 pr_info("%s stopped", d->disk->disk_name);
753
754 if (d->c)
755 bcache_device_detach(d);
Kent Overstreetf59fce82013-05-15 00:11:26 -0700756 if (d->disk && d->disk->flags & GENHD_FL_UP)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700757 del_gendisk(d->disk);
758 if (d->disk && d->disk->queue)
759 blk_cleanup_queue(d->disk->queue);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700760 if (d->disk) {
761 ida_simple_remove(&bcache_minor, d->disk->first_minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700762 put_disk(d->disk);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700763 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700764
765 bio_split_pool_free(&d->bio_split_hook);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700766 if (d->bio_split)
767 bioset_free(d->bio_split);
Kent Overstreet48a915a2013-10-31 15:43:22 -0700768 if (is_vmalloc_addr(d->full_dirty_stripes))
769 vfree(d->full_dirty_stripes);
770 else
771 kfree(d->full_dirty_stripes);
Kent Overstreet279afba2013-06-05 06:21:07 -0700772 if (is_vmalloc_addr(d->stripe_sectors_dirty))
773 vfree(d->stripe_sectors_dirty);
774 else
775 kfree(d->stripe_sectors_dirty);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700776
777 closure_debug_destroy(&d->cl);
778}
779
Kent Overstreet279afba2013-06-05 06:21:07 -0700780static int bcache_device_init(struct bcache_device *d, unsigned block_size,
781 sector_t sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700782{
783 struct request_queue *q;
Kent Overstreet279afba2013-06-05 06:21:07 -0700784 size_t n;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700785 int minor;
Kent Overstreet279afba2013-06-05 06:21:07 -0700786
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700787 if (!d->stripe_size)
788 d->stripe_size = 1 << 31;
Kent Overstreet279afba2013-06-05 06:21:07 -0700789
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700790 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
Kent Overstreet279afba2013-06-05 06:21:07 -0700791
Kent Overstreet48a915a2013-10-31 15:43:22 -0700792 if (!d->nr_stripes ||
793 d->nr_stripes > INT_MAX ||
794 d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
795 pr_err("nr_stripes too large");
Kent Overstreet279afba2013-06-05 06:21:07 -0700796 return -ENOMEM;
Kent Overstreet48a915a2013-10-31 15:43:22 -0700797 }
Kent Overstreet279afba2013-06-05 06:21:07 -0700798
799 n = d->nr_stripes * sizeof(atomic_t);
800 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
801 ? kzalloc(n, GFP_KERNEL)
802 : vzalloc(n);
803 if (!d->stripe_sectors_dirty)
804 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700805
Kent Overstreet48a915a2013-10-31 15:43:22 -0700806 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
807 d->full_dirty_stripes = n < PAGE_SIZE << 6
808 ? kzalloc(n, GFP_KERNEL)
809 : vzalloc(n);
810 if (!d->full_dirty_stripes)
811 return -ENOMEM;
812
Kent Overstreet28935ab2013-07-31 01:12:02 -0700813 minor = ida_simple_get(&bcache_minor, 0, MINORMASK + 1, GFP_KERNEL);
814 if (minor < 0)
815 return minor;
816
Kent Overstreetcafe5632013-03-23 16:11:31 -0700817 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -0700818 bio_split_pool_init(&d->bio_split_hook) ||
Kent Overstreet28935ab2013-07-31 01:12:02 -0700819 !(d->disk = alloc_disk(1))) {
820 ida_simple_remove(&bcache_minor, minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700821 return -ENOMEM;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700822 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700823
Kent Overstreet279afba2013-06-05 06:21:07 -0700824 set_capacity(d->disk, sectors);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700825 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700826
827 d->disk->major = bcache_major;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700828 d->disk->first_minor = minor;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700829 d->disk->fops = &bcache_ops;
830 d->disk->private_data = d;
831
Kent Overstreet28935ab2013-07-31 01:12:02 -0700832 q = blk_alloc_queue(GFP_KERNEL);
833 if (!q)
834 return -ENOMEM;
835
Kent Overstreetcafe5632013-03-23 16:11:31 -0700836 blk_queue_make_request(q, NULL);
837 d->disk->queue = q;
838 q->queuedata = d;
839 q->backing_dev_info.congested_data = d;
840 q->limits.max_hw_sectors = UINT_MAX;
841 q->limits.max_sectors = UINT_MAX;
842 q->limits.max_segment_size = UINT_MAX;
843 q->limits.max_segments = BIO_MAX_PAGES;
844 q->limits.max_discard_sectors = UINT_MAX;
845 q->limits.io_min = block_size;
846 q->limits.logical_block_size = block_size;
847 q->limits.physical_block_size = block_size;
848 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
849 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
850
Kent Overstreet54d12f22013-07-10 18:44:40 -0700851 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
852
Kent Overstreetcafe5632013-03-23 16:11:31 -0700853 return 0;
854}
855
856/* Cached device */
857
858static void calc_cached_dev_sectors(struct cache_set *c)
859{
860 uint64_t sectors = 0;
861 struct cached_dev *dc;
862
863 list_for_each_entry(dc, &c->cached_devs, list)
864 sectors += bdev_sectors(dc->bdev);
865
866 c->cached_dev_sectors = sectors;
867}
868
869void bch_cached_dev_run(struct cached_dev *dc)
870{
871 struct bcache_device *d = &dc->disk;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200872 char buf[SB_LABEL_SIZE + 1];
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200873 char *env[] = {
874 "DRIVER=bcache",
875 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200876 NULL,
877 NULL,
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200878 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700879
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200880 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
881 buf[SB_LABEL_SIZE] = '\0';
882 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
883
Kent Overstreetcafe5632013-03-23 16:11:31 -0700884 if (atomic_xchg(&dc->running, 1))
885 return;
886
887 if (!d->c &&
888 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
889 struct closure cl;
890 closure_init_stack(&cl);
891
892 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
893 bch_write_bdev_super(dc, &cl);
894 closure_sync(&cl);
895 }
896
897 add_disk(d->disk);
Kent Overstreetee668502013-02-01 07:29:41 -0800898 bd_link_disk_holder(dc->bdev, dc->disk.disk);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200899 /* won't show up in the uevent file, use udevadm monitor -e instead
900 * only class / kset properties are persistent */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700901 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200902 kfree(env[1]);
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200903 kfree(env[2]);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200904
Kent Overstreetcafe5632013-03-23 16:11:31 -0700905 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
906 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
907 pr_debug("error creating sysfs link");
908}
909
910static void cached_dev_detach_finish(struct work_struct *w)
911{
912 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
913 char buf[BDEVNAME_SIZE];
914 struct closure cl;
915 closure_init_stack(&cl);
916
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700917 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700918 BUG_ON(atomic_read(&dc->count));
919
Kent Overstreetcafe5632013-03-23 16:11:31 -0700920 mutex_lock(&bch_register_lock);
921
922 memset(&dc->sb.set_uuid, 0, 16);
923 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
924
925 bch_write_bdev_super(dc, &cl);
926 closure_sync(&cl);
927
928 bcache_device_detach(&dc->disk);
929 list_move(&dc->list, &uncached_devices);
930
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700931 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
932
Kent Overstreetcafe5632013-03-23 16:11:31 -0700933 mutex_unlock(&bch_register_lock);
934
935 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
936
937 /* Drop ref we took in cached_dev_detach() */
938 closure_put(&dc->disk.cl);
939}
940
941void bch_cached_dev_detach(struct cached_dev *dc)
942{
943 lockdep_assert_held(&bch_register_lock);
944
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700945 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700946 return;
947
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700948 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700949 return;
950
951 /*
952 * Block the device from being closed and freed until we're finished
953 * detaching
954 */
955 closure_get(&dc->disk.cl);
956
957 bch_writeback_queue(dc);
958 cached_dev_put(dc);
959}
960
961int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
962{
963 uint32_t rtime = cpu_to_le32(get_seconds());
964 struct uuid_entry *u;
965 char buf[BDEVNAME_SIZE];
966
967 bdevname(dc->bdev, buf);
968
969 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
970 return -ENOENT;
971
972 if (dc->disk.c) {
973 pr_err("Can't attach %s: already attached", buf);
974 return -EINVAL;
975 }
976
977 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
978 pr_err("Can't attach %s: shutting down", buf);
979 return -EINVAL;
980 }
981
982 if (dc->sb.block_size < c->sb.block_size) {
983 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700984 pr_err("Couldn't attach %s: block size less than set's block size",
985 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700986 return -EINVAL;
987 }
988
989 u = uuid_find(c, dc->sb.uuid);
990
991 if (u &&
992 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
993 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
994 memcpy(u->uuid, invalid_uuid, 16);
995 u->invalidated = cpu_to_le32(get_seconds());
996 u = NULL;
997 }
998
999 if (!u) {
1000 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1001 pr_err("Couldn't find uuid for %s in set", buf);
1002 return -ENOENT;
1003 }
1004
1005 u = uuid_find_empty(c);
1006 if (!u) {
1007 pr_err("Not caching %s, no room for UUID", buf);
1008 return -EINVAL;
1009 }
1010 }
1011
1012 /* Deadlocks since we're called via sysfs...
1013 sysfs_remove_file(&dc->kobj, &sysfs_attach);
1014 */
1015
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001016 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001017 struct closure cl;
1018 closure_init_stack(&cl);
1019
1020 memcpy(u->uuid, dc->sb.uuid, 16);
1021 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1022 u->first_reg = u->last_reg = rtime;
1023 bch_uuid_write(c);
1024
1025 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
1026 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1027
1028 bch_write_bdev_super(dc, &cl);
1029 closure_sync(&cl);
1030 } else {
1031 u->last_reg = rtime;
1032 bch_uuid_write(c);
1033 }
1034
1035 bcache_device_attach(&dc->disk, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001036 list_move(&dc->list, &c->cached_devs);
1037 calc_cached_dev_sectors(c);
1038
1039 smp_wmb();
1040 /*
1041 * dc->c must be set before dc->count != 0 - paired with the mb in
1042 * cached_dev_get()
1043 */
1044 atomic_set(&dc->count, 1);
1045
1046 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001047 bch_sectors_dirty_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001048 atomic_set(&dc->has_dirty, 1);
1049 atomic_inc(&dc->count);
1050 bch_writeback_queue(dc);
1051 }
1052
1053 bch_cached_dev_run(dc);
Kent Overstreetee668502013-02-01 07:29:41 -08001054 bcache_device_link(&dc->disk, c, "bdev");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001055
1056 pr_info("Caching %s as %s on set %pU",
1057 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
1058 dc->disk.c->sb.set_uuid);
1059 return 0;
1060}
1061
1062void bch_cached_dev_release(struct kobject *kobj)
1063{
1064 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1065 disk.kobj);
1066 kfree(dc);
1067 module_put(THIS_MODULE);
1068}
1069
1070static void cached_dev_free(struct closure *cl)
1071{
1072 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1073
1074 cancel_delayed_work_sync(&dc->writeback_rate_update);
Kent Overstreet5e6926d2013-07-24 17:50:06 -07001075 kthread_stop(dc->writeback_thread);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076
1077 mutex_lock(&bch_register_lock);
1078
Kent Overstreetf59fce82013-05-15 00:11:26 -07001079 if (atomic_read(&dc->running))
1080 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001081 bcache_device_free(&dc->disk);
1082 list_del(&dc->list);
1083
1084 mutex_unlock(&bch_register_lock);
1085
1086 if (!IS_ERR_OR_NULL(dc->bdev)) {
Kent Overstreetf59fce82013-05-15 00:11:26 -07001087 if (dc->bdev->bd_disk)
1088 blk_sync_queue(bdev_get_queue(dc->bdev));
1089
Kent Overstreetcafe5632013-03-23 16:11:31 -07001090 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1091 }
1092
1093 wake_up(&unregister_wait);
1094
1095 kobject_put(&dc->disk.kobj);
1096}
1097
1098static void cached_dev_flush(struct closure *cl)
1099{
1100 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1101 struct bcache_device *d = &dc->disk;
1102
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001103 mutex_lock(&bch_register_lock);
Kent Overstreetc4d951d2013-08-21 17:49:09 -07001104 bcache_device_unlink(d);
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001105 mutex_unlock(&bch_register_lock);
1106
Kent Overstreetcafe5632013-03-23 16:11:31 -07001107 bch_cache_accounting_destroy(&dc->accounting);
1108 kobject_del(&d->kobj);
1109
1110 continue_at(cl, cached_dev_free, system_wq);
1111}
1112
1113static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1114{
Kent Overstreetf59fce82013-05-15 00:11:26 -07001115 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001116 struct io *io;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001117 struct request_queue *q = bdev_get_queue(dc->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001118
1119 __module_get(THIS_MODULE);
1120 INIT_LIST_HEAD(&dc->list);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001121 closure_init(&dc->disk.cl, NULL);
1122 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001123 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001124 INIT_WORK(&dc->detach, cached_dev_detach_finish);
Kent Overstreetcb7a5832013-12-16 15:27:25 -08001125 sema_init(&dc->sb_write_mutex, 1);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001126 INIT_LIST_HEAD(&dc->io_lru);
1127 spin_lock_init(&dc->io_lock);
1128 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001129
Kent Overstreetcafe5632013-03-23 16:11:31 -07001130 dc->sequential_cutoff = 4 << 20;
1131
Kent Overstreetcafe5632013-03-23 16:11:31 -07001132 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1133 list_add(&io->lru, &dc->io_lru);
1134 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1135 }
1136
Kent Overstreet279afba2013-06-05 06:21:07 -07001137 ret = bcache_device_init(&dc->disk, block_size,
1138 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001139 if (ret)
1140 return ret;
1141
1142 set_capacity(dc->disk.disk,
1143 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1144
1145 dc->disk.disk->queue->backing_dev_info.ra_pages =
1146 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1147 q->backing_dev_info.ra_pages);
1148
1149 bch_cached_dev_request_init(dc);
1150 bch_cached_dev_writeback_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001151 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001152}
1153
1154/* Cached device - bcache superblock */
1155
Kent Overstreetf59fce82013-05-15 00:11:26 -07001156static void register_bdev(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001157 struct block_device *bdev,
1158 struct cached_dev *dc)
1159{
1160 char name[BDEVNAME_SIZE];
1161 const char *err = "cannot allocate memory";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001162 struct cache_set *c;
1163
Kent Overstreetcafe5632013-03-23 16:11:31 -07001164 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001165 dc->bdev = bdev;
1166 dc->bdev->bd_holder = dc;
1167
Kent Overstreetf59fce82013-05-15 00:11:26 -07001168 bio_init(&dc->sb_bio);
1169 dc->sb_bio.bi_max_vecs = 1;
1170 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1171 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1172 get_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001173
Kent Overstreetf59fce82013-05-15 00:11:26 -07001174 if (cached_dev_init(dc, sb->block_size << 9))
1175 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001176
1177 err = "error creating kobject";
1178 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1179 "bcache"))
1180 goto err;
1181 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1182 goto err;
1183
Kent Overstreetf59fce82013-05-15 00:11:26 -07001184 pr_info("registered backing device %s", bdevname(bdev, name));
1185
Kent Overstreetcafe5632013-03-23 16:11:31 -07001186 list_add(&dc->list, &uncached_devices);
1187 list_for_each_entry(c, &bch_cache_sets, list)
1188 bch_cached_dev_attach(dc, c);
1189
1190 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1191 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1192 bch_cached_dev_run(dc);
1193
Kent Overstreetf59fce82013-05-15 00:11:26 -07001194 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001195err:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001196 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001197 bcache_device_stop(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001198}
1199
1200/* Flash only volumes */
1201
1202void bch_flash_dev_release(struct kobject *kobj)
1203{
1204 struct bcache_device *d = container_of(kobj, struct bcache_device,
1205 kobj);
1206 kfree(d);
1207}
1208
1209static void flash_dev_free(struct closure *cl)
1210{
1211 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1212 bcache_device_free(d);
1213 kobject_put(&d->kobj);
1214}
1215
1216static void flash_dev_flush(struct closure *cl)
1217{
1218 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1219
Kent Overstreetee668502013-02-01 07:29:41 -08001220 bcache_device_unlink(d);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001221 kobject_del(&d->kobj);
1222 continue_at(cl, flash_dev_free, system_wq);
1223}
1224
1225static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1226{
1227 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1228 GFP_KERNEL);
1229 if (!d)
1230 return -ENOMEM;
1231
1232 closure_init(&d->cl, NULL);
1233 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1234
1235 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1236
Kent Overstreet279afba2013-06-05 06:21:07 -07001237 if (bcache_device_init(d, block_bytes(c), u->sectors))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001238 goto err;
1239
1240 bcache_device_attach(d, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001241 bch_flash_dev_request_init(d);
1242 add_disk(d->disk);
1243
1244 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1245 goto err;
1246
1247 bcache_device_link(d, c, "volume");
1248
1249 return 0;
1250err:
1251 kobject_put(&d->kobj);
1252 return -ENOMEM;
1253}
1254
1255static int flash_devs_run(struct cache_set *c)
1256{
1257 int ret = 0;
1258 struct uuid_entry *u;
1259
1260 for (u = c->uuids;
1261 u < c->uuids + c->nr_uuids && !ret;
1262 u++)
1263 if (UUID_FLASH_ONLY(u))
1264 ret = flash_dev_run(c, u);
1265
1266 return ret;
1267}
1268
1269int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1270{
1271 struct uuid_entry *u;
1272
1273 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1274 return -EINTR;
1275
1276 u = uuid_find_empty(c);
1277 if (!u) {
1278 pr_err("Can't create volume, no room for UUID");
1279 return -EINVAL;
1280 }
1281
1282 get_random_bytes(u->uuid, 16);
1283 memset(u->label, 0, 32);
1284 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1285
1286 SET_UUID_FLASH_ONLY(u, 1);
1287 u->sectors = size >> 9;
1288
1289 bch_uuid_write(c);
1290
1291 return flash_dev_run(c, u);
1292}
1293
1294/* Cache set */
1295
1296__printf(2, 3)
1297bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1298{
1299 va_list args;
1300
Kent Overstreet77c320e2013-07-11 19:42:51 -07001301 if (c->on_error != ON_ERROR_PANIC &&
1302 test_bit(CACHE_SET_STOPPING, &c->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001303 return false;
1304
1305 /* XXX: we can be called from atomic context
1306 acquire_console_sem();
1307 */
1308
1309 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1310
1311 va_start(args, fmt);
1312 vprintk(fmt, args);
1313 va_end(args);
1314
1315 printk(", disabling caching\n");
1316
Kent Overstreet77c320e2013-07-11 19:42:51 -07001317 if (c->on_error == ON_ERROR_PANIC)
1318 panic("panic forced after error\n");
1319
Kent Overstreetcafe5632013-03-23 16:11:31 -07001320 bch_cache_set_unregister(c);
1321 return true;
1322}
1323
1324void bch_cache_set_release(struct kobject *kobj)
1325{
1326 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1327 kfree(c);
1328 module_put(THIS_MODULE);
1329}
1330
1331static void cache_set_free(struct closure *cl)
1332{
1333 struct cache_set *c = container_of(cl, struct cache_set, cl);
1334 struct cache *ca;
1335 unsigned i;
1336
1337 if (!IS_ERR_OR_NULL(c->debug))
1338 debugfs_remove(c->debug);
1339
1340 bch_open_buckets_free(c);
1341 bch_btree_cache_free(c);
1342 bch_journal_free(c);
1343
1344 for_each_cache(ca, c, i)
1345 if (ca)
1346 kobject_put(&ca->kobj);
1347
1348 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1349 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1350
Kent Overstreetcafe5632013-03-23 16:11:31 -07001351 if (c->bio_split)
1352 bioset_free(c->bio_split);
Kent Overstreet57943512013-04-25 13:58:35 -07001353 if (c->fill_iter)
1354 mempool_destroy(c->fill_iter);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001355 if (c->bio_meta)
1356 mempool_destroy(c->bio_meta);
1357 if (c->search)
1358 mempool_destroy(c->search);
1359 kfree(c->devices);
1360
1361 mutex_lock(&bch_register_lock);
1362 list_del(&c->list);
1363 mutex_unlock(&bch_register_lock);
1364
1365 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1366 wake_up(&unregister_wait);
1367
1368 closure_debug_destroy(&c->cl);
1369 kobject_put(&c->kobj);
1370}
1371
1372static void cache_set_flush(struct closure *cl)
1373{
1374 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet79826c32013-07-10 18:31:58 -07001375 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001376 struct btree *b;
Kent Overstreet79826c32013-07-10 18:31:58 -07001377 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001378
1379 bch_cache_accounting_destroy(&c->accounting);
1380
1381 kobject_put(&c->internal);
1382 kobject_del(&c->kobj);
1383
Kent Overstreet72a44512013-10-24 17:19:26 -07001384 if (c->gc_thread)
1385 kthread_stop(c->gc_thread);
1386
Kent Overstreetcafe5632013-03-23 16:11:31 -07001387 if (!IS_ERR_OR_NULL(c->root))
1388 list_add(&c->root->list, &c->btree_cache);
1389
1390 /* Should skip this if we're unregistering because of an error */
1391 list_for_each_entry(b, &c->btree_cache, list)
1392 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -07001393 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001394
Kent Overstreet79826c32013-07-10 18:31:58 -07001395 for_each_cache(ca, c, i)
1396 if (ca->alloc_thread)
1397 kthread_stop(ca->alloc_thread);
1398
Kent Overstreetcafe5632013-03-23 16:11:31 -07001399 closure_return(cl);
1400}
1401
1402static void __cache_set_unregister(struct closure *cl)
1403{
1404 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001405 struct cached_dev *dc;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001406 size_t i;
1407
1408 mutex_lock(&bch_register_lock);
1409
Kent Overstreetcafe5632013-03-23 16:11:31 -07001410 for (i = 0; i < c->nr_uuids; i++)
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001411 if (c->devices[i]) {
1412 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1413 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1414 dc = container_of(c->devices[i],
1415 struct cached_dev, disk);
1416 bch_cached_dev_detach(dc);
1417 } else {
1418 bcache_device_stop(c->devices[i]);
1419 }
1420 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001421
1422 mutex_unlock(&bch_register_lock);
1423
1424 continue_at(cl, cache_set_flush, system_wq);
1425}
1426
1427void bch_cache_set_stop(struct cache_set *c)
1428{
1429 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1430 closure_queue(&c->caching);
1431}
1432
1433void bch_cache_set_unregister(struct cache_set *c)
1434{
1435 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1436 bch_cache_set_stop(c);
1437}
1438
1439#define alloc_bucket_pages(gfp, c) \
1440 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1441
1442struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1443{
1444 int iter_size;
1445 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1446 if (!c)
1447 return NULL;
1448
1449 __module_get(THIS_MODULE);
1450 closure_init(&c->cl, NULL);
1451 set_closure_fn(&c->cl, cache_set_free, system_wq);
1452
1453 closure_init(&c->caching, &c->cl);
1454 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1455
1456 /* Maybe create continue_at_noreturn() and use it here? */
1457 closure_set_stopped(&c->cl);
1458 closure_put(&c->cl);
1459
1460 kobject_init(&c->kobj, &bch_cache_set_ktype);
1461 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1462
1463 bch_cache_accounting_init(&c->accounting, &c->cl);
1464
1465 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1466 c->sb.block_size = sb->block_size;
1467 c->sb.bucket_size = sb->bucket_size;
1468 c->sb.nr_in_set = sb->nr_in_set;
1469 c->sb.last_mount = sb->last_mount;
1470 c->bucket_bits = ilog2(sb->bucket_size);
1471 c->block_bits = ilog2(sb->block_size);
1472 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1473
1474 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1475 if (c->btree_pages > BTREE_MAX_PAGES)
1476 c->btree_pages = max_t(int, c->btree_pages / 4,
1477 BTREE_MAX_PAGES);
1478
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001479 c->sort_crit_factor = int_sqrt(c->btree_pages);
1480
Kent Overstreetcb7a5832013-12-16 15:27:25 -08001481 sema_init(&c->sb_write_mutex, 1);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001482 mutex_init(&c->bucket_lock);
1483 init_waitqueue_head(&c->try_wait);
Kent Overstreet35fcd842013-07-24 17:29:09 -07001484 init_waitqueue_head(&c->bucket_wait);
Kent Overstreetcb7a5832013-12-16 15:27:25 -08001485 sema_init(&c->uuid_write_mutex, 1);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001486 mutex_init(&c->sort_lock);
Kent Overstreet65d22e92013-07-31 00:03:54 -07001487
1488 spin_lock_init(&c->sort_time.lock);
1489 spin_lock_init(&c->btree_gc_time.lock);
1490 spin_lock_init(&c->btree_split_time.lock);
1491 spin_lock_init(&c->btree_read_time.lock);
1492 spin_lock_init(&c->try_harder_time.lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001493
Kent Overstreetcafe5632013-03-23 16:11:31 -07001494 bch_moving_init_cache_set(c);
1495
1496 INIT_LIST_HEAD(&c->list);
1497 INIT_LIST_HEAD(&c->cached_devs);
1498 INIT_LIST_HEAD(&c->btree_cache);
1499 INIT_LIST_HEAD(&c->btree_cache_freeable);
1500 INIT_LIST_HEAD(&c->btree_cache_freed);
1501 INIT_LIST_HEAD(&c->data_buckets);
1502
1503 c->search = mempool_create_slab_pool(32, bch_search_cache);
1504 if (!c->search)
1505 goto err;
1506
1507 iter_size = (sb->bucket_size / sb->block_size + 1) *
1508 sizeof(struct btree_iter_set);
1509
1510 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1511 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1512 sizeof(struct bbio) + sizeof(struct bio_vec) *
1513 bucket_pages(c))) ||
Kent Overstreet57943512013-04-25 13:58:35 -07001514 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001515 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001516 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1517 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1518 bch_journal_alloc(c) ||
1519 bch_btree_cache_alloc(c) ||
1520 bch_open_buckets_alloc(c))
1521 goto err;
1522
Kent Overstreetcafe5632013-03-23 16:11:31 -07001523 c->congested_read_threshold_us = 2000;
1524 c->congested_write_threshold_us = 20000;
1525 c->error_limit = 8 << IO_ERROR_SHIFT;
1526
1527 return c;
1528err:
1529 bch_cache_set_unregister(c);
1530 return NULL;
1531}
1532
1533static void run_cache_set(struct cache_set *c)
1534{
1535 const char *err = "cannot allocate memory";
1536 struct cached_dev *dc, *t;
1537 struct cache *ca;
Kent Overstreetc18536a2013-07-24 17:44:17 -07001538 struct closure cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001539 unsigned i;
1540
Kent Overstreetc18536a2013-07-24 17:44:17 -07001541 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001542
1543 for_each_cache(ca, c, i)
1544 c->nbuckets += ca->sb.nbuckets;
1545
1546 if (CACHE_SYNC(&c->sb)) {
1547 LIST_HEAD(journal);
1548 struct bkey *k;
1549 struct jset *j;
1550
1551 err = "cannot allocate memory for journal";
Kent Overstreetc18536a2013-07-24 17:44:17 -07001552 if (bch_journal_read(c, &journal))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001553 goto err;
1554
1555 pr_debug("btree_journal_read() done");
1556
1557 err = "no journal entries found";
1558 if (list_empty(&journal))
1559 goto err;
1560
1561 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1562
1563 err = "IO error reading priorities";
1564 for_each_cache(ca, c, i)
1565 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1566
1567 /*
1568 * If prio_read() fails it'll call cache_set_error and we'll
1569 * tear everything down right away, but if we perhaps checked
1570 * sooner we could avoid journal replay.
1571 */
1572
1573 k = &j->btree_root;
1574
1575 err = "bad btree root";
Kent Overstreetd5cc66e2013-07-24 23:06:40 -07001576 if (bch_btree_ptr_invalid(c, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001577 goto err;
1578
1579 err = "error reading btree root";
Kent Overstreete8e1d462013-07-24 17:27:07 -07001580 c->root = bch_btree_node_get(c, k, j->btree_level, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001581 if (IS_ERR_OR_NULL(c->root))
1582 goto err;
1583
1584 list_del_init(&c->root->list);
1585 rw_unlock(true, c->root);
1586
Kent Overstreetc18536a2013-07-24 17:44:17 -07001587 err = uuid_read(c, j, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001588 if (err)
1589 goto err;
1590
1591 err = "error in recovery";
Kent Overstreetc18536a2013-07-24 17:44:17 -07001592 if (bch_btree_check(c))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001593 goto err;
1594
1595 bch_journal_mark(c, &journal);
1596 bch_btree_gc_finish(c);
1597 pr_debug("btree_check() done");
1598
1599 /*
1600 * bcache_journal_next() can't happen sooner, or
1601 * btree_gc_finish() will give spurious errors about last_gc >
1602 * gc_gen - this is a hack but oh well.
1603 */
1604 bch_journal_next(&c->journal);
1605
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001606 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001607 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001608 if (bch_cache_allocator_start(ca))
1609 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001610
1611 /*
1612 * First place it's safe to allocate: btree_check() and
1613 * btree_gc_finish() have to run before we have buckets to
1614 * allocate, and bch_bucket_alloc_set() might cause a journal
1615 * entry to be written so bcache_journal_next() has to be called
1616 * first.
1617 *
1618 * If the uuids were in the old format we have to rewrite them
1619 * before the next journal entry is written:
1620 */
1621 if (j->version < BCACHE_JSET_VERSION_UUID)
1622 __uuid_write(c);
1623
Kent Overstreetc18536a2013-07-24 17:44:17 -07001624 bch_journal_replay(c, &journal);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001625 } else {
1626 pr_notice("invalidating existing data");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001627
1628 for_each_cache(ca, c, i) {
1629 unsigned j;
1630
1631 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1632 2, SB_JOURNAL_BUCKETS);
1633
1634 for (j = 0; j < ca->sb.keys; j++)
1635 ca->sb.d[j] = ca->sb.first_bucket + j;
1636 }
1637
1638 bch_btree_gc_finish(c);
1639
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001640 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001641 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001642 if (bch_cache_allocator_start(ca))
1643 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001644
1645 mutex_lock(&c->bucket_lock);
1646 for_each_cache(ca, c, i)
1647 bch_prio_write(ca);
1648 mutex_unlock(&c->bucket_lock);
1649
Kent Overstreetcafe5632013-03-23 16:11:31 -07001650 err = "cannot allocate new UUID bucket";
1651 if (__uuid_write(c))
Kent Overstreet72a44512013-10-24 17:19:26 -07001652 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001653
1654 err = "cannot allocate new btree root";
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001655 c->root = bch_btree_node_alloc(c, 0, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001656 if (IS_ERR_OR_NULL(c->root))
Kent Overstreet72a44512013-10-24 17:19:26 -07001657 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001658
1659 bkey_copy_key(&c->root->key, &MAX_KEY);
Kent Overstreetc18536a2013-07-24 17:44:17 -07001660 bch_btree_node_write(c->root, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001661
1662 bch_btree_set_root(c->root);
1663 rw_unlock(true, c->root);
1664
1665 /*
1666 * We don't want to write the first journal entry until
1667 * everything is set up - fortunately journal entries won't be
1668 * written until the SET_CACHE_SYNC() here:
1669 */
1670 SET_CACHE_SYNC(&c->sb, true);
1671
1672 bch_journal_next(&c->journal);
Kent Overstreetc18536a2013-07-24 17:44:17 -07001673 bch_journal_meta(c, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001674 }
1675
Kent Overstreet72a44512013-10-24 17:19:26 -07001676 err = "error starting gc thread";
1677 if (bch_gc_thread_start(c))
1678 goto err;
1679
Kent Overstreetc18536a2013-07-24 17:44:17 -07001680 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001681 c->sb.last_mount = get_seconds();
1682 bcache_write_super(c);
1683
1684 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1685 bch_cached_dev_attach(dc, c);
1686
1687 flash_devs_run(c);
1688
1689 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001690err:
Kent Overstreetc18536a2013-07-24 17:44:17 -07001691 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001692 /* XXX: test this, it's broken */
Kees Cookc8694942013-09-10 21:41:34 -07001693 bch_cache_set_error(c, "%s", err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001694}
1695
1696static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1697{
1698 return ca->sb.block_size == c->sb.block_size &&
Nicholas Swenson9eb8ebe2013-10-22 13:19:23 -07001699 ca->sb.bucket_size == c->sb.bucket_size &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001700 ca->sb.nr_in_set == c->sb.nr_in_set;
1701}
1702
1703static const char *register_cache_set(struct cache *ca)
1704{
1705 char buf[12];
1706 const char *err = "cannot allocate memory";
1707 struct cache_set *c;
1708
1709 list_for_each_entry(c, &bch_cache_sets, list)
1710 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1711 if (c->cache[ca->sb.nr_this_dev])
1712 return "duplicate cache set member";
1713
1714 if (!can_attach_cache(ca, c))
1715 return "cache sb does not match set";
1716
1717 if (!CACHE_SYNC(&ca->sb))
1718 SET_CACHE_SYNC(&c->sb, false);
1719
1720 goto found;
1721 }
1722
1723 c = bch_cache_set_alloc(&ca->sb);
1724 if (!c)
1725 return err;
1726
1727 err = "error creating kobject";
1728 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1729 kobject_add(&c->internal, &c->kobj, "internal"))
1730 goto err;
1731
1732 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1733 goto err;
1734
1735 bch_debug_init_cache_set(c);
1736
1737 list_add(&c->list, &bch_cache_sets);
1738found:
1739 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1740 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1741 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1742 goto err;
1743
1744 if (ca->sb.seq > c->sb.seq) {
1745 c->sb.version = ca->sb.version;
1746 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1747 c->sb.flags = ca->sb.flags;
1748 c->sb.seq = ca->sb.seq;
1749 pr_debug("set version = %llu", c->sb.version);
1750 }
1751
1752 ca->set = c;
1753 ca->set->cache[ca->sb.nr_this_dev] = ca;
1754 c->cache_by_alloc[c->caches_loaded++] = ca;
1755
1756 if (c->caches_loaded == c->sb.nr_in_set)
1757 run_cache_set(c);
1758
1759 return NULL;
1760err:
1761 bch_cache_set_unregister(c);
1762 return err;
1763}
1764
1765/* Cache device */
1766
1767void bch_cache_release(struct kobject *kobj)
1768{
1769 struct cache *ca = container_of(kobj, struct cache, kobj);
1770
1771 if (ca->set)
1772 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1773
Kent Overstreetcafe5632013-03-23 16:11:31 -07001774 bio_split_pool_free(&ca->bio_split_hook);
1775
Kent Overstreetcafe5632013-03-23 16:11:31 -07001776 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1777 kfree(ca->prio_buckets);
1778 vfree(ca->buckets);
1779
1780 free_heap(&ca->heap);
1781 free_fifo(&ca->unused);
1782 free_fifo(&ca->free_inc);
1783 free_fifo(&ca->free);
1784
1785 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1786 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1787
1788 if (!IS_ERR_OR_NULL(ca->bdev)) {
1789 blk_sync_queue(bdev_get_queue(ca->bdev));
1790 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1791 }
1792
1793 kfree(ca);
1794 module_put(THIS_MODULE);
1795}
1796
1797static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1798{
1799 size_t free;
1800 struct bucket *b;
1801
Kent Overstreetcafe5632013-03-23 16:11:31 -07001802 __module_get(THIS_MODULE);
1803 kobject_init(&ca->kobj, &bch_cache_ktype);
1804
Kent Overstreetcafe5632013-03-23 16:11:31 -07001805 bio_init(&ca->journal.bio);
1806 ca->journal.bio.bi_max_vecs = 8;
1807 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1808
1809 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1810 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1811
1812 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1813 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1814 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1815 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -07001816 !(ca->buckets = vzalloc(sizeof(struct bucket) *
Kent Overstreetcafe5632013-03-23 16:11:31 -07001817 ca->sb.nbuckets)) ||
1818 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1819 2, GFP_KERNEL)) ||
1820 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001821 bio_split_pool_init(&ca->bio_split_hook))
Kent Overstreetf59fce82013-05-15 00:11:26 -07001822 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001823
1824 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1825
Kent Overstreetcafe5632013-03-23 16:11:31 -07001826 for_each_bucket(b, ca)
1827 atomic_set(&b->pin, 0);
1828
1829 if (bch_cache_allocator_init(ca))
1830 goto err;
1831
1832 return 0;
1833err:
1834 kobject_put(&ca->kobj);
1835 return -ENOMEM;
1836}
1837
Kent Overstreetf59fce82013-05-15 00:11:26 -07001838static void register_cache(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001839 struct block_device *bdev, struct cache *ca)
1840{
1841 char name[BDEVNAME_SIZE];
1842 const char *err = "cannot allocate memory";
1843
Kent Overstreetf59fce82013-05-15 00:11:26 -07001844 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001845 ca->bdev = bdev;
1846 ca->bdev->bd_holder = ca;
1847
Kent Overstreetf59fce82013-05-15 00:11:26 -07001848 bio_init(&ca->sb_bio);
1849 ca->sb_bio.bi_max_vecs = 1;
1850 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1851 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1852 get_page(sb_page);
1853
Kent Overstreetcafe5632013-03-23 16:11:31 -07001854 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1855 ca->discard = CACHE_DISCARD(&ca->sb);
1856
Kent Overstreetf59fce82013-05-15 00:11:26 -07001857 if (cache_alloc(sb, ca) != 0)
1858 goto err;
1859
Kent Overstreetcafe5632013-03-23 16:11:31 -07001860 err = "error creating kobject";
1861 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1862 goto err;
1863
1864 err = register_cache_set(ca);
1865 if (err)
1866 goto err;
1867
1868 pr_info("registered cache device %s", bdevname(bdev, name));
Kent Overstreetf59fce82013-05-15 00:11:26 -07001869 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001870err:
Kent Overstreetf59fce82013-05-15 00:11:26 -07001871 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001872 kobject_put(&ca->kobj);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001873}
1874
1875/* Global interfaces/init */
1876
1877static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1878 const char *, size_t);
1879
1880kobj_attribute_write(register, register_bcache);
1881kobj_attribute_write(register_quiet, register_bcache);
1882
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001883static bool bch_is_open_backing(struct block_device *bdev) {
1884 struct cache_set *c, *tc;
1885 struct cached_dev *dc, *t;
1886
1887 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1888 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1889 if (dc->bdev == bdev)
1890 return true;
1891 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1892 if (dc->bdev == bdev)
1893 return true;
1894 return false;
1895}
1896
1897static bool bch_is_open_cache(struct block_device *bdev) {
1898 struct cache_set *c, *tc;
1899 struct cache *ca;
1900 unsigned i;
1901
1902 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1903 for_each_cache(ca, c, i)
1904 if (ca->bdev == bdev)
1905 return true;
1906 return false;
1907}
1908
1909static bool bch_is_open(struct block_device *bdev) {
1910 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1911}
1912
Kent Overstreetcafe5632013-03-23 16:11:31 -07001913static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1914 const char *buffer, size_t size)
1915{
1916 ssize_t ret = size;
1917 const char *err = "cannot allocate memory";
1918 char *path = NULL;
1919 struct cache_sb *sb = NULL;
1920 struct block_device *bdev = NULL;
1921 struct page *sb_page = NULL;
1922
1923 if (!try_module_get(THIS_MODULE))
1924 return -EBUSY;
1925
1926 mutex_lock(&bch_register_lock);
1927
1928 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1929 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1930 goto err;
1931
1932 err = "failed to open device";
1933 bdev = blkdev_get_by_path(strim(path),
1934 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1935 sb);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001936 if (IS_ERR(bdev)) {
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001937 if (bdev == ERR_PTR(-EBUSY)) {
1938 bdev = lookup_bdev(strim(path));
1939 if (!IS_ERR(bdev) && bch_is_open(bdev))
1940 err = "device already registered";
1941 else
1942 err = "device busy";
1943 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001944 goto err;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001945 }
1946
1947 err = "failed to set blocksize";
1948 if (set_blocksize(bdev, 4096))
1949 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001950
1951 err = read_super(sb, bdev, &sb_page);
1952 if (err)
1953 goto err_close;
1954
Kent Overstreet29033812013-04-11 15:14:35 -07001955 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001956 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001957 if (!dc)
1958 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001959
Kent Overstreetf59fce82013-05-15 00:11:26 -07001960 register_bdev(sb, sb_page, bdev, dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001961 } else {
1962 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001963 if (!ca)
1964 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001965
Kent Overstreetf59fce82013-05-15 00:11:26 -07001966 register_cache(sb, sb_page, bdev, ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001967 }
Kent Overstreetf59fce82013-05-15 00:11:26 -07001968out:
1969 if (sb_page)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001970 put_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001971 kfree(sb);
1972 kfree(path);
1973 mutex_unlock(&bch_register_lock);
1974 module_put(THIS_MODULE);
1975 return ret;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001976
1977err_close:
1978 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1979err:
1980 if (attr != &ksysfs_register_quiet)
1981 pr_info("error opening %s: %s", path, err);
1982 ret = -EINVAL;
1983 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001984}
1985
1986static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1987{
1988 if (code == SYS_DOWN ||
1989 code == SYS_HALT ||
1990 code == SYS_POWER_OFF) {
1991 DEFINE_WAIT(wait);
1992 unsigned long start = jiffies;
1993 bool stopped = false;
1994
1995 struct cache_set *c, *tc;
1996 struct cached_dev *dc, *tdc;
1997
1998 mutex_lock(&bch_register_lock);
1999
2000 if (list_empty(&bch_cache_sets) &&
2001 list_empty(&uncached_devices))
2002 goto out;
2003
2004 pr_info("Stopping all devices:");
2005
2006 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2007 bch_cache_set_stop(c);
2008
2009 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
2010 bcache_device_stop(&dc->disk);
2011
2012 /* What's a condition variable? */
2013 while (1) {
2014 long timeout = start + 2 * HZ - jiffies;
2015
2016 stopped = list_empty(&bch_cache_sets) &&
2017 list_empty(&uncached_devices);
2018
2019 if (timeout < 0 || stopped)
2020 break;
2021
2022 prepare_to_wait(&unregister_wait, &wait,
2023 TASK_UNINTERRUPTIBLE);
2024
2025 mutex_unlock(&bch_register_lock);
2026 schedule_timeout(timeout);
2027 mutex_lock(&bch_register_lock);
2028 }
2029
2030 finish_wait(&unregister_wait, &wait);
2031
2032 if (stopped)
2033 pr_info("All devices stopped");
2034 else
2035 pr_notice("Timeout waiting for devices to be closed");
2036out:
2037 mutex_unlock(&bch_register_lock);
2038 }
2039
2040 return NOTIFY_DONE;
2041}
2042
2043static struct notifier_block reboot = {
2044 .notifier_call = bcache_reboot,
2045 .priority = INT_MAX, /* before any real devices */
2046};
2047
2048static void bcache_exit(void)
2049{
2050 bch_debug_exit();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002051 bch_request_exit();
2052 bch_btree_exit();
2053 if (bcache_kobj)
2054 kobject_put(bcache_kobj);
2055 if (bcache_wq)
2056 destroy_workqueue(bcache_wq);
2057 unregister_blkdev(bcache_major, "bcache");
2058 unregister_reboot_notifier(&reboot);
2059}
2060
2061static int __init bcache_init(void)
2062{
2063 static const struct attribute *files[] = {
2064 &ksysfs_register.attr,
2065 &ksysfs_register_quiet.attr,
2066 NULL
2067 };
2068
2069 mutex_init(&bch_register_lock);
2070 init_waitqueue_head(&unregister_wait);
2071 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07002072 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002073
2074 bcache_major = register_blkdev(0, "bcache");
2075 if (bcache_major < 0)
2076 return bcache_major;
2077
2078 if (!(bcache_wq = create_workqueue("bcache")) ||
2079 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2080 sysfs_create_files(bcache_kobj, files) ||
2081 bch_btree_init() ||
2082 bch_request_init() ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07002083 bch_debug_init(bcache_kobj))
2084 goto err;
2085
2086 return 0;
2087err:
2088 bcache_exit();
2089 return -ENOMEM;
2090}
2091
2092module_exit(bcache_exit);
2093module_init(bcache_init);