blob: dec15cd2d797eaaa7fa0585abf8867c9f70a4cef [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
228 closure_put(&dc->sb_write.cl);
229}
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
236 bio->bi_sector = SB_SECTOR;
237 bio->bi_rw = REQ_SYNC|REQ_META;
238 bio->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
266void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
267{
268 struct closure *cl = &dc->sb_write.cl;
269 struct bio *bio = &dc->sb_bio;
270
271 closure_lock(&dc->sb_write, parent);
272
273 bio_reset(bio);
274 bio->bi_bdev = dc->bdev;
275 bio->bi_end_io = write_bdev_super_endio;
276 bio->bi_private = dc;
277
278 closure_get(cl);
279 __write_super(&dc->sb, bio);
280
281 closure_return(cl);
282}
283
284static void write_super_endio(struct bio *bio, int error)
285{
286 struct cache *ca = bio->bi_private;
287
288 bch_count_io_errors(ca, error, "writing superblock");
289 closure_put(&ca->set->sb_write.cl);
290}
291
292void bcache_write_super(struct cache_set *c)
293{
294 struct closure *cl = &c->sb_write.cl;
295 struct cache *ca;
296 unsigned i;
297
298 closure_lock(&c->sb_write, &c->cl);
299
300 c->sb.seq++;
301
302 for_each_cache(ca, c, i) {
303 struct bio *bio = &ca->sb_bio;
304
Kent Overstreet29033812013-04-11 15:14:35 -0700305 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700306 ca->sb.seq = c->sb.seq;
307 ca->sb.last_mount = c->sb.last_mount;
308
309 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
310
311 bio_reset(bio);
312 bio->bi_bdev = ca->bdev;
313 bio->bi_end_io = write_super_endio;
314 bio->bi_private = ca;
315
316 closure_get(cl);
317 __write_super(&ca->sb, bio);
318 }
319
320 closure_return(cl);
321}
322
323/* UUID io */
324
325static void uuid_endio(struct bio *bio, int error)
326{
327 struct closure *cl = bio->bi_private;
328 struct cache_set *c = container_of(cl, struct cache_set, uuid_write.cl);
329
330 cache_set_err_on(error, c, "accessing uuids");
331 bch_bbio_free(bio, c);
332 closure_put(cl);
333}
334
335static void uuid_io(struct cache_set *c, unsigned long rw,
336 struct bkey *k, struct closure *parent)
337{
338 struct closure *cl = &c->uuid_write.cl;
339 struct uuid_entry *u;
340 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -0700341 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700342
343 BUG_ON(!parent);
344 closure_lock(&c->uuid_write, parent);
345
346 for (i = 0; i < KEY_PTRS(k); i++) {
347 struct bio *bio = bch_bbio_alloc(c);
348
349 bio->bi_rw = REQ_SYNC|REQ_META|rw;
350 bio->bi_size = KEY_SIZE(k) << 9;
351
352 bio->bi_end_io = uuid_endio;
353 bio->bi_private = cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600354 bch_bio_map(bio, c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700355
356 bch_submit_bbio(bio, c, k, i);
357
358 if (!(rw & WRITE))
359 break;
360 }
361
Kent Overstreet85b14922013-05-14 20:33:16 -0700362 bch_bkey_to_text(buf, sizeof(buf), k);
363 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700364
365 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600366 if (!bch_is_zero(u->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700367 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
368 u - c->uuids, u->uuid, u->label,
369 u->first_reg, u->last_reg, u->invalidated);
370
371 closure_return(cl);
372}
373
374static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
375{
376 struct bkey *k = &j->uuid_bucket;
377
Kent Overstreetd5cc66e2013-07-24 23:06:40 -0700378 if (bch_btree_ptr_invalid(c, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700379 return "bad uuid pointer";
380
381 bkey_copy(&c->uuid_bucket, k);
382 uuid_io(c, READ_SYNC, k, cl);
383
384 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
385 struct uuid_entry_v0 *u0 = (void *) c->uuids;
386 struct uuid_entry *u1 = (void *) c->uuids;
387 int i;
388
389 closure_sync(cl);
390
391 /*
392 * Since the new uuid entry is bigger than the old, we have to
393 * convert starting at the highest memory address and work down
394 * in order to do it in place
395 */
396
397 for (i = c->nr_uuids - 1;
398 i >= 0;
399 --i) {
400 memcpy(u1[i].uuid, u0[i].uuid, 16);
401 memcpy(u1[i].label, u0[i].label, 32);
402
403 u1[i].first_reg = u0[i].first_reg;
404 u1[i].last_reg = u0[i].last_reg;
405 u1[i].invalidated = u0[i].invalidated;
406
407 u1[i].flags = 0;
408 u1[i].sectors = 0;
409 }
410 }
411
412 return NULL;
413}
414
415static int __uuid_write(struct cache_set *c)
416{
417 BKEY_PADDED(key) k;
418 struct closure cl;
419 closure_init_stack(&cl);
420
421 lockdep_assert_held(&bch_register_lock);
422
Kent Overstreet35fcd842013-07-24 17:29:09 -0700423 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700424 return 1;
425
426 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
427 uuid_io(c, REQ_WRITE, &k.key, &cl);
428 closure_sync(&cl);
429
430 bkey_copy(&c->uuid_bucket, &k.key);
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700431 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700432 return 0;
433}
434
435int bch_uuid_write(struct cache_set *c)
436{
437 int ret = __uuid_write(c);
438
439 if (!ret)
440 bch_journal_meta(c, NULL);
441
442 return ret;
443}
444
445static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
446{
447 struct uuid_entry *u;
448
449 for (u = c->uuids;
450 u < c->uuids + c->nr_uuids; u++)
451 if (!memcmp(u->uuid, uuid, 16))
452 return u;
453
454 return NULL;
455}
456
457static struct uuid_entry *uuid_find_empty(struct cache_set *c)
458{
459 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
460 return uuid_find(c, zero_uuid);
461}
462
463/*
464 * Bucket priorities/gens:
465 *
466 * For each bucket, we store on disk its
467 * 8 bit gen
468 * 16 bit priority
469 *
470 * See alloc.c for an explanation of the gen. The priority is used to implement
471 * lru (and in the future other) cache replacement policies; for most purposes
472 * it's just an opaque integer.
473 *
474 * The gens and the priorities don't have a whole lot to do with each other, and
475 * it's actually the gens that must be written out at specific times - it's no
476 * big deal if the priorities don't get written, if we lose them we just reuse
477 * buckets in suboptimal order.
478 *
479 * On disk they're stored in a packed array, and in as many buckets are required
480 * to fit them all. The buckets we use to store them form a list; the journal
481 * header points to the first bucket, the first bucket points to the second
482 * bucket, et cetera.
483 *
484 * This code is used by the allocation code; periodically (whenever it runs out
485 * of buckets to allocate from) the allocation code will invalidate some
486 * buckets, but it can't use those buckets until their new gens are safely on
487 * disk.
488 */
489
490static void prio_endio(struct bio *bio, int error)
491{
492 struct cache *ca = bio->bi_private;
493
494 cache_set_err_on(error, ca->set, "accessing priorities");
495 bch_bbio_free(bio, ca->set);
496 closure_put(&ca->prio);
497}
498
499static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
500{
501 struct closure *cl = &ca->prio;
502 struct bio *bio = bch_bbio_alloc(ca->set);
503
504 closure_init_stack(cl);
505
506 bio->bi_sector = bucket * ca->sb.bucket_size;
507 bio->bi_bdev = ca->bdev;
508 bio->bi_rw = REQ_SYNC|REQ_META|rw;
509 bio->bi_size = bucket_bytes(ca);
510
511 bio->bi_end_io = prio_endio;
512 bio->bi_private = ca;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600513 bch_bio_map(bio, ca->disk_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700514
515 closure_bio_submit(bio, &ca->prio, ca);
516 closure_sync(cl);
517}
518
519#define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
520 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
521
522void bch_prio_write(struct cache *ca)
523{
524 int i;
525 struct bucket *b;
526 struct closure cl;
527
528 closure_init_stack(&cl);
529
530 lockdep_assert_held(&ca->set->bucket_lock);
531
532 for (b = ca->buckets;
533 b < ca->buckets + ca->sb.nbuckets; b++)
534 b->disk_gen = b->gen;
535
536 ca->disk_buckets->seq++;
537
538 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
539 &ca->meta_sectors_written);
540
541 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
542 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700543
544 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
545 long bucket;
546 struct prio_set *p = ca->disk_buckets;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700547 struct bucket_disk *d = p->data;
548 struct bucket_disk *end = d + prios_per_bucket(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700549
550 for (b = ca->buckets + i * prios_per_bucket(ca);
551 b < ca->buckets + ca->sb.nbuckets && d < end;
552 b++, d++) {
553 d->prio = cpu_to_le16(b->prio);
554 d->gen = b->gen;
555 }
556
557 p->next_bucket = ca->prio_buckets[i + 1];
Kent Overstreet81ab4192013-10-31 15:46:42 -0700558 p->magic = pset_magic(&ca->sb);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600559 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700560
Kent Overstreet35fcd842013-07-24 17:29:09 -0700561 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700562 BUG_ON(bucket == -1);
563
564 mutex_unlock(&ca->set->bucket_lock);
565 prio_io(ca, bucket, REQ_WRITE);
566 mutex_lock(&ca->set->bucket_lock);
567
568 ca->prio_buckets[i] = bucket;
569 atomic_dec_bug(&ca->buckets[bucket].pin);
570 }
571
572 mutex_unlock(&ca->set->bucket_lock);
573
574 bch_journal_meta(ca->set, &cl);
575 closure_sync(&cl);
576
577 mutex_lock(&ca->set->bucket_lock);
578
579 ca->need_save_prio = 0;
580
581 /*
582 * Don't want the old priorities to get garbage collected until after we
583 * finish writing the new ones, and they're journalled
584 */
585 for (i = 0; i < prio_buckets(ca); i++)
586 ca->prio_last_buckets[i] = ca->prio_buckets[i];
587}
588
589static void prio_read(struct cache *ca, uint64_t bucket)
590{
591 struct prio_set *p = ca->disk_buckets;
592 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
593 struct bucket *b;
594 unsigned bucket_nr = 0;
595
596 for (b = ca->buckets;
597 b < ca->buckets + ca->sb.nbuckets;
598 b++, d++) {
599 if (d == end) {
600 ca->prio_buckets[bucket_nr] = bucket;
601 ca->prio_last_buckets[bucket_nr] = bucket;
602 bucket_nr++;
603
604 prio_io(ca, bucket, READ_SYNC);
605
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600606 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700607 pr_warn("bad csum reading priorities");
608
Kent Overstreet81ab4192013-10-31 15:46:42 -0700609 if (p->magic != pset_magic(&ca->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700610 pr_warn("bad magic reading priorities");
611
612 bucket = p->next_bucket;
613 d = p->data;
614 }
615
616 b->prio = le16_to_cpu(d->prio);
617 b->gen = b->disk_gen = b->last_gc = b->gc_gen = d->gen;
618 }
619}
620
621/* Bcache device */
622
623static int open_dev(struct block_device *b, fmode_t mode)
624{
625 struct bcache_device *d = b->bd_disk->private_data;
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700626 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700627 return -ENXIO;
628
629 closure_get(&d->cl);
630 return 0;
631}
632
Emil Goode867e1162013-05-09 22:39:26 +0200633static void release_dev(struct gendisk *b, fmode_t mode)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700634{
635 struct bcache_device *d = b->private_data;
636 closure_put(&d->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700637}
638
639static int ioctl_dev(struct block_device *b, fmode_t mode,
640 unsigned int cmd, unsigned long arg)
641{
642 struct bcache_device *d = b->bd_disk->private_data;
643 return d->ioctl(d, mode, cmd, arg);
644}
645
646static const struct block_device_operations bcache_ops = {
647 .open = open_dev,
648 .release = release_dev,
649 .ioctl = ioctl_dev,
650 .owner = THIS_MODULE,
651};
652
653void bcache_device_stop(struct bcache_device *d)
654{
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700655 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700656 closure_queue(&d->cl);
657}
658
Kent Overstreetee668502013-02-01 07:29:41 -0800659static void bcache_device_unlink(struct bcache_device *d)
660{
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700661 lockdep_assert_held(&bch_register_lock);
Kent Overstreetee668502013-02-01 07:29:41 -0800662
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700663 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
664 unsigned i;
665 struct cache *ca;
Kent Overstreetee668502013-02-01 07:29:41 -0800666
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700667 sysfs_remove_link(&d->c->kobj, d->name);
668 sysfs_remove_link(&d->kobj, "cache");
669
670 for_each_cache(ca, d->c, i)
671 bd_unlink_disk_holder(ca->bdev, d->disk);
672 }
Kent Overstreetee668502013-02-01 07:29:41 -0800673}
674
675static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
676 const char *name)
677{
678 unsigned i;
679 struct cache *ca;
680
681 for_each_cache(ca, d->c, i)
682 bd_link_disk_holder(ca->bdev, d->disk);
683
684 snprintf(d->name, BCACHEDEVNAME_SIZE,
685 "%s%u", name, d->id);
686
687 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
688 sysfs_create_link(&c->kobj, &d->kobj, d->name),
689 "Couldn't create device <-> cache set symlinks");
690}
691
Kent Overstreetcafe5632013-03-23 16:11:31 -0700692static void bcache_device_detach(struct bcache_device *d)
693{
694 lockdep_assert_held(&bch_register_lock);
695
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700696 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700697 struct uuid_entry *u = d->c->uuids + d->id;
698
699 SET_UUID_FLASH_ONLY(u, 0);
700 memcpy(u->uuid, invalid_uuid, 16);
701 u->invalidated = cpu_to_le32(get_seconds());
702 bch_uuid_write(d->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700703 }
704
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700705 bcache_device_unlink(d);
Kent Overstreetee668502013-02-01 07:29:41 -0800706
Kent Overstreetcafe5632013-03-23 16:11:31 -0700707 d->c->devices[d->id] = NULL;
708 closure_put(&d->c->caching);
709 d->c = NULL;
710}
711
712static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
713 unsigned id)
714{
715 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
716
717 d->id = id;
718 d->c = c;
719 c->devices[id] = d;
720
721 closure_get(&c->caching);
722}
723
Kent Overstreetcafe5632013-03-23 16:11:31 -0700724static void bcache_device_free(struct bcache_device *d)
725{
726 lockdep_assert_held(&bch_register_lock);
727
728 pr_info("%s stopped", d->disk->disk_name);
729
730 if (d->c)
731 bcache_device_detach(d);
Kent Overstreetf59fce82013-05-15 00:11:26 -0700732 if (d->disk && d->disk->flags & GENHD_FL_UP)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700733 del_gendisk(d->disk);
734 if (d->disk && d->disk->queue)
735 blk_cleanup_queue(d->disk->queue);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700736 if (d->disk) {
737 ida_simple_remove(&bcache_minor, d->disk->first_minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700738 put_disk(d->disk);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700739 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700740
741 bio_split_pool_free(&d->bio_split_hook);
742 if (d->unaligned_bvec)
743 mempool_destroy(d->unaligned_bvec);
744 if (d->bio_split)
745 bioset_free(d->bio_split);
Kent Overstreet48a915a2013-10-31 15:43:22 -0700746 if (is_vmalloc_addr(d->full_dirty_stripes))
747 vfree(d->full_dirty_stripes);
748 else
749 kfree(d->full_dirty_stripes);
Kent Overstreet279afba2013-06-05 06:21:07 -0700750 if (is_vmalloc_addr(d->stripe_sectors_dirty))
751 vfree(d->stripe_sectors_dirty);
752 else
753 kfree(d->stripe_sectors_dirty);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700754
755 closure_debug_destroy(&d->cl);
756}
757
Kent Overstreet279afba2013-06-05 06:21:07 -0700758static int bcache_device_init(struct bcache_device *d, unsigned block_size,
759 sector_t sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700760{
761 struct request_queue *q;
Kent Overstreet279afba2013-06-05 06:21:07 -0700762 size_t n;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700763 int minor;
Kent Overstreet279afba2013-06-05 06:21:07 -0700764
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700765 if (!d->stripe_size)
766 d->stripe_size = 1 << 31;
Kent Overstreet279afba2013-06-05 06:21:07 -0700767
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700768 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
Kent Overstreet279afba2013-06-05 06:21:07 -0700769
Kent Overstreet48a915a2013-10-31 15:43:22 -0700770 if (!d->nr_stripes ||
771 d->nr_stripes > INT_MAX ||
772 d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
773 pr_err("nr_stripes too large");
Kent Overstreet279afba2013-06-05 06:21:07 -0700774 return -ENOMEM;
Kent Overstreet48a915a2013-10-31 15:43:22 -0700775 }
Kent Overstreet279afba2013-06-05 06:21:07 -0700776
777 n = d->nr_stripes * sizeof(atomic_t);
778 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
779 ? kzalloc(n, GFP_KERNEL)
780 : vzalloc(n);
781 if (!d->stripe_sectors_dirty)
782 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700783
Kent Overstreet48a915a2013-10-31 15:43:22 -0700784 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
785 d->full_dirty_stripes = n < PAGE_SIZE << 6
786 ? kzalloc(n, GFP_KERNEL)
787 : vzalloc(n);
788 if (!d->full_dirty_stripes)
789 return -ENOMEM;
790
Kent Overstreet28935ab2013-07-31 01:12:02 -0700791 minor = ida_simple_get(&bcache_minor, 0, MINORMASK + 1, GFP_KERNEL);
792 if (minor < 0)
793 return minor;
794
Kent Overstreetcafe5632013-03-23 16:11:31 -0700795 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
796 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
797 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -0700798 bio_split_pool_init(&d->bio_split_hook) ||
Kent Overstreet28935ab2013-07-31 01:12:02 -0700799 !(d->disk = alloc_disk(1))) {
800 ida_simple_remove(&bcache_minor, minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801 return -ENOMEM;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700802 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700803
Kent Overstreet279afba2013-06-05 06:21:07 -0700804 set_capacity(d->disk, sectors);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700805 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700806
807 d->disk->major = bcache_major;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700808 d->disk->first_minor = minor;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700809 d->disk->fops = &bcache_ops;
810 d->disk->private_data = d;
811
Kent Overstreet28935ab2013-07-31 01:12:02 -0700812 q = blk_alloc_queue(GFP_KERNEL);
813 if (!q)
814 return -ENOMEM;
815
Kent Overstreetcafe5632013-03-23 16:11:31 -0700816 blk_queue_make_request(q, NULL);
817 d->disk->queue = q;
818 q->queuedata = d;
819 q->backing_dev_info.congested_data = d;
820 q->limits.max_hw_sectors = UINT_MAX;
821 q->limits.max_sectors = UINT_MAX;
822 q->limits.max_segment_size = UINT_MAX;
823 q->limits.max_segments = BIO_MAX_PAGES;
824 q->limits.max_discard_sectors = UINT_MAX;
825 q->limits.io_min = block_size;
826 q->limits.logical_block_size = block_size;
827 q->limits.physical_block_size = block_size;
828 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
829 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
830
Kent Overstreet54d12f22013-07-10 18:44:40 -0700831 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
832
Kent Overstreetcafe5632013-03-23 16:11:31 -0700833 return 0;
834}
835
836/* Cached device */
837
838static void calc_cached_dev_sectors(struct cache_set *c)
839{
840 uint64_t sectors = 0;
841 struct cached_dev *dc;
842
843 list_for_each_entry(dc, &c->cached_devs, list)
844 sectors += bdev_sectors(dc->bdev);
845
846 c->cached_dev_sectors = sectors;
847}
848
849void bch_cached_dev_run(struct cached_dev *dc)
850{
851 struct bcache_device *d = &dc->disk;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200852 char buf[SB_LABEL_SIZE + 1];
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200853 char *env[] = {
854 "DRIVER=bcache",
855 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200856 NULL,
857 NULL,
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200858 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700859
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200860 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
861 buf[SB_LABEL_SIZE] = '\0';
862 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
863
Kent Overstreetcafe5632013-03-23 16:11:31 -0700864 if (atomic_xchg(&dc->running, 1))
865 return;
866
867 if (!d->c &&
868 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
869 struct closure cl;
870 closure_init_stack(&cl);
871
872 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
873 bch_write_bdev_super(dc, &cl);
874 closure_sync(&cl);
875 }
876
877 add_disk(d->disk);
Kent Overstreetee668502013-02-01 07:29:41 -0800878 bd_link_disk_holder(dc->bdev, dc->disk.disk);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200879 /* won't show up in the uevent file, use udevadm monitor -e instead
880 * only class / kset properties are persistent */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700881 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200882 kfree(env[1]);
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200883 kfree(env[2]);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200884
Kent Overstreetcafe5632013-03-23 16:11:31 -0700885 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
886 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
887 pr_debug("error creating sysfs link");
888}
889
890static void cached_dev_detach_finish(struct work_struct *w)
891{
892 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
893 char buf[BDEVNAME_SIZE];
894 struct closure cl;
895 closure_init_stack(&cl);
896
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700897 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700898 BUG_ON(atomic_read(&dc->count));
899
Kent Overstreetcafe5632013-03-23 16:11:31 -0700900 mutex_lock(&bch_register_lock);
901
902 memset(&dc->sb.set_uuid, 0, 16);
903 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
904
905 bch_write_bdev_super(dc, &cl);
906 closure_sync(&cl);
907
908 bcache_device_detach(&dc->disk);
909 list_move(&dc->list, &uncached_devices);
910
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700911 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
912
Kent Overstreetcafe5632013-03-23 16:11:31 -0700913 mutex_unlock(&bch_register_lock);
914
915 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
916
917 /* Drop ref we took in cached_dev_detach() */
918 closure_put(&dc->disk.cl);
919}
920
921void bch_cached_dev_detach(struct cached_dev *dc)
922{
923 lockdep_assert_held(&bch_register_lock);
924
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700925 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700926 return;
927
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700928 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700929 return;
930
931 /*
932 * Block the device from being closed and freed until we're finished
933 * detaching
934 */
935 closure_get(&dc->disk.cl);
936
937 bch_writeback_queue(dc);
938 cached_dev_put(dc);
939}
940
941int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
942{
943 uint32_t rtime = cpu_to_le32(get_seconds());
944 struct uuid_entry *u;
945 char buf[BDEVNAME_SIZE];
946
947 bdevname(dc->bdev, buf);
948
949 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
950 return -ENOENT;
951
952 if (dc->disk.c) {
953 pr_err("Can't attach %s: already attached", buf);
954 return -EINVAL;
955 }
956
957 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
958 pr_err("Can't attach %s: shutting down", buf);
959 return -EINVAL;
960 }
961
962 if (dc->sb.block_size < c->sb.block_size) {
963 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700964 pr_err("Couldn't attach %s: block size less than set's block size",
965 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700966 return -EINVAL;
967 }
968
969 u = uuid_find(c, dc->sb.uuid);
970
971 if (u &&
972 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
973 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
974 memcpy(u->uuid, invalid_uuid, 16);
975 u->invalidated = cpu_to_le32(get_seconds());
976 u = NULL;
977 }
978
979 if (!u) {
980 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
981 pr_err("Couldn't find uuid for %s in set", buf);
982 return -ENOENT;
983 }
984
985 u = uuid_find_empty(c);
986 if (!u) {
987 pr_err("Not caching %s, no room for UUID", buf);
988 return -EINVAL;
989 }
990 }
991
992 /* Deadlocks since we're called via sysfs...
993 sysfs_remove_file(&dc->kobj, &sysfs_attach);
994 */
995
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600996 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700997 struct closure cl;
998 closure_init_stack(&cl);
999
1000 memcpy(u->uuid, dc->sb.uuid, 16);
1001 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1002 u->first_reg = u->last_reg = rtime;
1003 bch_uuid_write(c);
1004
1005 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
1006 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1007
1008 bch_write_bdev_super(dc, &cl);
1009 closure_sync(&cl);
1010 } else {
1011 u->last_reg = rtime;
1012 bch_uuid_write(c);
1013 }
1014
1015 bcache_device_attach(&dc->disk, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001016 list_move(&dc->list, &c->cached_devs);
1017 calc_cached_dev_sectors(c);
1018
1019 smp_wmb();
1020 /*
1021 * dc->c must be set before dc->count != 0 - paired with the mb in
1022 * cached_dev_get()
1023 */
1024 atomic_set(&dc->count, 1);
1025
1026 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001027 bch_sectors_dirty_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028 atomic_set(&dc->has_dirty, 1);
1029 atomic_inc(&dc->count);
1030 bch_writeback_queue(dc);
1031 }
1032
1033 bch_cached_dev_run(dc);
Kent Overstreetee668502013-02-01 07:29:41 -08001034 bcache_device_link(&dc->disk, c, "bdev");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001035
1036 pr_info("Caching %s as %s on set %pU",
1037 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
1038 dc->disk.c->sb.set_uuid);
1039 return 0;
1040}
1041
1042void bch_cached_dev_release(struct kobject *kobj)
1043{
1044 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1045 disk.kobj);
1046 kfree(dc);
1047 module_put(THIS_MODULE);
1048}
1049
1050static void cached_dev_free(struct closure *cl)
1051{
1052 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1053
1054 cancel_delayed_work_sync(&dc->writeback_rate_update);
Kent Overstreet5e6926d2013-07-24 17:50:06 -07001055 kthread_stop(dc->writeback_thread);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001056
1057 mutex_lock(&bch_register_lock);
1058
Kent Overstreetf59fce82013-05-15 00:11:26 -07001059 if (atomic_read(&dc->running))
1060 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001061 bcache_device_free(&dc->disk);
1062 list_del(&dc->list);
1063
1064 mutex_unlock(&bch_register_lock);
1065
1066 if (!IS_ERR_OR_NULL(dc->bdev)) {
Kent Overstreetf59fce82013-05-15 00:11:26 -07001067 if (dc->bdev->bd_disk)
1068 blk_sync_queue(bdev_get_queue(dc->bdev));
1069
Kent Overstreetcafe5632013-03-23 16:11:31 -07001070 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1071 }
1072
1073 wake_up(&unregister_wait);
1074
1075 kobject_put(&dc->disk.kobj);
1076}
1077
1078static void cached_dev_flush(struct closure *cl)
1079{
1080 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1081 struct bcache_device *d = &dc->disk;
1082
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001083 mutex_lock(&bch_register_lock);
Kent Overstreetc4d951d2013-08-21 17:49:09 -07001084 bcache_device_unlink(d);
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001085 mutex_unlock(&bch_register_lock);
1086
Kent Overstreetcafe5632013-03-23 16:11:31 -07001087 bch_cache_accounting_destroy(&dc->accounting);
1088 kobject_del(&d->kobj);
1089
1090 continue_at(cl, cached_dev_free, system_wq);
1091}
1092
1093static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1094{
Kent Overstreetf59fce82013-05-15 00:11:26 -07001095 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001096 struct io *io;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001097 struct request_queue *q = bdev_get_queue(dc->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001098
1099 __module_get(THIS_MODULE);
1100 INIT_LIST_HEAD(&dc->list);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001101 closure_init(&dc->disk.cl, NULL);
1102 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001103 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001104 INIT_WORK(&dc->detach, cached_dev_detach_finish);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001105 closure_init_unlocked(&dc->sb_write);
1106 INIT_LIST_HEAD(&dc->io_lru);
1107 spin_lock_init(&dc->io_lock);
1108 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001109
Kent Overstreetcafe5632013-03-23 16:11:31 -07001110 dc->sequential_cutoff = 4 << 20;
1111
Kent Overstreetcafe5632013-03-23 16:11:31 -07001112 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1113 list_add(&io->lru, &dc->io_lru);
1114 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1115 }
1116
Kent Overstreet279afba2013-06-05 06:21:07 -07001117 ret = bcache_device_init(&dc->disk, block_size,
1118 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001119 if (ret)
1120 return ret;
1121
1122 set_capacity(dc->disk.disk,
1123 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1124
1125 dc->disk.disk->queue->backing_dev_info.ra_pages =
1126 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1127 q->backing_dev_info.ra_pages);
1128
1129 bch_cached_dev_request_init(dc);
1130 bch_cached_dev_writeback_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001131 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001132}
1133
1134/* Cached device - bcache superblock */
1135
Kent Overstreetf59fce82013-05-15 00:11:26 -07001136static void register_bdev(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001137 struct block_device *bdev,
1138 struct cached_dev *dc)
1139{
1140 char name[BDEVNAME_SIZE];
1141 const char *err = "cannot allocate memory";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001142 struct cache_set *c;
1143
Kent Overstreetcafe5632013-03-23 16:11:31 -07001144 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001145 dc->bdev = bdev;
1146 dc->bdev->bd_holder = dc;
1147
Kent Overstreetf59fce82013-05-15 00:11:26 -07001148 bio_init(&dc->sb_bio);
1149 dc->sb_bio.bi_max_vecs = 1;
1150 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1151 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1152 get_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001153
Kent Overstreetf59fce82013-05-15 00:11:26 -07001154 if (cached_dev_init(dc, sb->block_size << 9))
1155 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001156
1157 err = "error creating kobject";
1158 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1159 "bcache"))
1160 goto err;
1161 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1162 goto err;
1163
Kent Overstreetf59fce82013-05-15 00:11:26 -07001164 pr_info("registered backing device %s", bdevname(bdev, name));
1165
Kent Overstreetcafe5632013-03-23 16:11:31 -07001166 list_add(&dc->list, &uncached_devices);
1167 list_for_each_entry(c, &bch_cache_sets, list)
1168 bch_cached_dev_attach(dc, c);
1169
1170 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1171 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1172 bch_cached_dev_run(dc);
1173
Kent Overstreetf59fce82013-05-15 00:11:26 -07001174 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001175err:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001176 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001177 bcache_device_stop(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001178}
1179
1180/* Flash only volumes */
1181
1182void bch_flash_dev_release(struct kobject *kobj)
1183{
1184 struct bcache_device *d = container_of(kobj, struct bcache_device,
1185 kobj);
1186 kfree(d);
1187}
1188
1189static void flash_dev_free(struct closure *cl)
1190{
1191 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1192 bcache_device_free(d);
1193 kobject_put(&d->kobj);
1194}
1195
1196static void flash_dev_flush(struct closure *cl)
1197{
1198 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1199
Kent Overstreetee668502013-02-01 07:29:41 -08001200 bcache_device_unlink(d);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001201 kobject_del(&d->kobj);
1202 continue_at(cl, flash_dev_free, system_wq);
1203}
1204
1205static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1206{
1207 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1208 GFP_KERNEL);
1209 if (!d)
1210 return -ENOMEM;
1211
1212 closure_init(&d->cl, NULL);
1213 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1214
1215 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1216
Kent Overstreet279afba2013-06-05 06:21:07 -07001217 if (bcache_device_init(d, block_bytes(c), u->sectors))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001218 goto err;
1219
1220 bcache_device_attach(d, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001221 bch_flash_dev_request_init(d);
1222 add_disk(d->disk);
1223
1224 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1225 goto err;
1226
1227 bcache_device_link(d, c, "volume");
1228
1229 return 0;
1230err:
1231 kobject_put(&d->kobj);
1232 return -ENOMEM;
1233}
1234
1235static int flash_devs_run(struct cache_set *c)
1236{
1237 int ret = 0;
1238 struct uuid_entry *u;
1239
1240 for (u = c->uuids;
1241 u < c->uuids + c->nr_uuids && !ret;
1242 u++)
1243 if (UUID_FLASH_ONLY(u))
1244 ret = flash_dev_run(c, u);
1245
1246 return ret;
1247}
1248
1249int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1250{
1251 struct uuid_entry *u;
1252
1253 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1254 return -EINTR;
1255
1256 u = uuid_find_empty(c);
1257 if (!u) {
1258 pr_err("Can't create volume, no room for UUID");
1259 return -EINVAL;
1260 }
1261
1262 get_random_bytes(u->uuid, 16);
1263 memset(u->label, 0, 32);
1264 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1265
1266 SET_UUID_FLASH_ONLY(u, 1);
1267 u->sectors = size >> 9;
1268
1269 bch_uuid_write(c);
1270
1271 return flash_dev_run(c, u);
1272}
1273
1274/* Cache set */
1275
1276__printf(2, 3)
1277bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1278{
1279 va_list args;
1280
Kent Overstreet77c320e2013-07-11 19:42:51 -07001281 if (c->on_error != ON_ERROR_PANIC &&
1282 test_bit(CACHE_SET_STOPPING, &c->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001283 return false;
1284
1285 /* XXX: we can be called from atomic context
1286 acquire_console_sem();
1287 */
1288
1289 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1290
1291 va_start(args, fmt);
1292 vprintk(fmt, args);
1293 va_end(args);
1294
1295 printk(", disabling caching\n");
1296
Kent Overstreet77c320e2013-07-11 19:42:51 -07001297 if (c->on_error == ON_ERROR_PANIC)
1298 panic("panic forced after error\n");
1299
Kent Overstreetcafe5632013-03-23 16:11:31 -07001300 bch_cache_set_unregister(c);
1301 return true;
1302}
1303
1304void bch_cache_set_release(struct kobject *kobj)
1305{
1306 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1307 kfree(c);
1308 module_put(THIS_MODULE);
1309}
1310
1311static void cache_set_free(struct closure *cl)
1312{
1313 struct cache_set *c = container_of(cl, struct cache_set, cl);
1314 struct cache *ca;
1315 unsigned i;
1316
1317 if (!IS_ERR_OR_NULL(c->debug))
1318 debugfs_remove(c->debug);
1319
1320 bch_open_buckets_free(c);
1321 bch_btree_cache_free(c);
1322 bch_journal_free(c);
1323
1324 for_each_cache(ca, c, i)
1325 if (ca)
1326 kobject_put(&ca->kobj);
1327
1328 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1329 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1330
Kent Overstreetcafe5632013-03-23 16:11:31 -07001331 if (c->bio_split)
1332 bioset_free(c->bio_split);
Kent Overstreet57943512013-04-25 13:58:35 -07001333 if (c->fill_iter)
1334 mempool_destroy(c->fill_iter);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001335 if (c->bio_meta)
1336 mempool_destroy(c->bio_meta);
1337 if (c->search)
1338 mempool_destroy(c->search);
1339 kfree(c->devices);
1340
1341 mutex_lock(&bch_register_lock);
1342 list_del(&c->list);
1343 mutex_unlock(&bch_register_lock);
1344
1345 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1346 wake_up(&unregister_wait);
1347
1348 closure_debug_destroy(&c->cl);
1349 kobject_put(&c->kobj);
1350}
1351
1352static void cache_set_flush(struct closure *cl)
1353{
1354 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet79826c32013-07-10 18:31:58 -07001355 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001356 struct btree *b;
Kent Overstreet79826c32013-07-10 18:31:58 -07001357 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001358
1359 bch_cache_accounting_destroy(&c->accounting);
1360
1361 kobject_put(&c->internal);
1362 kobject_del(&c->kobj);
1363
Kent Overstreet72a44512013-10-24 17:19:26 -07001364 if (c->gc_thread)
1365 kthread_stop(c->gc_thread);
1366
Kent Overstreetcafe5632013-03-23 16:11:31 -07001367 if (!IS_ERR_OR_NULL(c->root))
1368 list_add(&c->root->list, &c->btree_cache);
1369
1370 /* Should skip this if we're unregistering because of an error */
1371 list_for_each_entry(b, &c->btree_cache, list)
1372 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -07001373 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001374
Kent Overstreet79826c32013-07-10 18:31:58 -07001375 for_each_cache(ca, c, i)
1376 if (ca->alloc_thread)
1377 kthread_stop(ca->alloc_thread);
1378
Kent Overstreetcafe5632013-03-23 16:11:31 -07001379 closure_return(cl);
1380}
1381
1382static void __cache_set_unregister(struct closure *cl)
1383{
1384 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001385 struct cached_dev *dc;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001386 size_t i;
1387
1388 mutex_lock(&bch_register_lock);
1389
Kent Overstreetcafe5632013-03-23 16:11:31 -07001390 for (i = 0; i < c->nr_uuids; i++)
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001391 if (c->devices[i]) {
1392 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1393 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1394 dc = container_of(c->devices[i],
1395 struct cached_dev, disk);
1396 bch_cached_dev_detach(dc);
1397 } else {
1398 bcache_device_stop(c->devices[i]);
1399 }
1400 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001401
1402 mutex_unlock(&bch_register_lock);
1403
1404 continue_at(cl, cache_set_flush, system_wq);
1405}
1406
1407void bch_cache_set_stop(struct cache_set *c)
1408{
1409 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1410 closure_queue(&c->caching);
1411}
1412
1413void bch_cache_set_unregister(struct cache_set *c)
1414{
1415 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1416 bch_cache_set_stop(c);
1417}
1418
1419#define alloc_bucket_pages(gfp, c) \
1420 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1421
1422struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1423{
1424 int iter_size;
1425 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1426 if (!c)
1427 return NULL;
1428
1429 __module_get(THIS_MODULE);
1430 closure_init(&c->cl, NULL);
1431 set_closure_fn(&c->cl, cache_set_free, system_wq);
1432
1433 closure_init(&c->caching, &c->cl);
1434 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1435
1436 /* Maybe create continue_at_noreturn() and use it here? */
1437 closure_set_stopped(&c->cl);
1438 closure_put(&c->cl);
1439
1440 kobject_init(&c->kobj, &bch_cache_set_ktype);
1441 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1442
1443 bch_cache_accounting_init(&c->accounting, &c->cl);
1444
1445 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1446 c->sb.block_size = sb->block_size;
1447 c->sb.bucket_size = sb->bucket_size;
1448 c->sb.nr_in_set = sb->nr_in_set;
1449 c->sb.last_mount = sb->last_mount;
1450 c->bucket_bits = ilog2(sb->bucket_size);
1451 c->block_bits = ilog2(sb->block_size);
1452 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1453
1454 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1455 if (c->btree_pages > BTREE_MAX_PAGES)
1456 c->btree_pages = max_t(int, c->btree_pages / 4,
1457 BTREE_MAX_PAGES);
1458
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001459 c->sort_crit_factor = int_sqrt(c->btree_pages);
1460
Kent Overstreetcafe5632013-03-23 16:11:31 -07001461 closure_init_unlocked(&c->sb_write);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001462 mutex_init(&c->bucket_lock);
1463 init_waitqueue_head(&c->try_wait);
Kent Overstreet35fcd842013-07-24 17:29:09 -07001464 init_waitqueue_head(&c->bucket_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001465 closure_init_unlocked(&c->uuid_write);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001466 mutex_init(&c->sort_lock);
Kent Overstreet65d22e92013-07-31 00:03:54 -07001467
1468 spin_lock_init(&c->sort_time.lock);
1469 spin_lock_init(&c->btree_gc_time.lock);
1470 spin_lock_init(&c->btree_split_time.lock);
1471 spin_lock_init(&c->btree_read_time.lock);
1472 spin_lock_init(&c->try_harder_time.lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001473
Kent Overstreetcafe5632013-03-23 16:11:31 -07001474 bch_moving_init_cache_set(c);
1475
1476 INIT_LIST_HEAD(&c->list);
1477 INIT_LIST_HEAD(&c->cached_devs);
1478 INIT_LIST_HEAD(&c->btree_cache);
1479 INIT_LIST_HEAD(&c->btree_cache_freeable);
1480 INIT_LIST_HEAD(&c->btree_cache_freed);
1481 INIT_LIST_HEAD(&c->data_buckets);
1482
1483 c->search = mempool_create_slab_pool(32, bch_search_cache);
1484 if (!c->search)
1485 goto err;
1486
1487 iter_size = (sb->bucket_size / sb->block_size + 1) *
1488 sizeof(struct btree_iter_set);
1489
1490 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1491 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1492 sizeof(struct bbio) + sizeof(struct bio_vec) *
1493 bucket_pages(c))) ||
Kent Overstreet57943512013-04-25 13:58:35 -07001494 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001495 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001496 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1497 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1498 bch_journal_alloc(c) ||
1499 bch_btree_cache_alloc(c) ||
1500 bch_open_buckets_alloc(c))
1501 goto err;
1502
Kent Overstreetcafe5632013-03-23 16:11:31 -07001503 c->congested_read_threshold_us = 2000;
1504 c->congested_write_threshold_us = 20000;
1505 c->error_limit = 8 << IO_ERROR_SHIFT;
1506
1507 return c;
1508err:
1509 bch_cache_set_unregister(c);
1510 return NULL;
1511}
1512
1513static void run_cache_set(struct cache_set *c)
1514{
1515 const char *err = "cannot allocate memory";
1516 struct cached_dev *dc, *t;
1517 struct cache *ca;
Kent Overstreetc18536a2013-07-24 17:44:17 -07001518 struct closure cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001519 unsigned i;
1520
Kent Overstreetc18536a2013-07-24 17:44:17 -07001521 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001522
1523 for_each_cache(ca, c, i)
1524 c->nbuckets += ca->sb.nbuckets;
1525
1526 if (CACHE_SYNC(&c->sb)) {
1527 LIST_HEAD(journal);
1528 struct bkey *k;
1529 struct jset *j;
1530
1531 err = "cannot allocate memory for journal";
Kent Overstreetc18536a2013-07-24 17:44:17 -07001532 if (bch_journal_read(c, &journal))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001533 goto err;
1534
1535 pr_debug("btree_journal_read() done");
1536
1537 err = "no journal entries found";
1538 if (list_empty(&journal))
1539 goto err;
1540
1541 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1542
1543 err = "IO error reading priorities";
1544 for_each_cache(ca, c, i)
1545 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1546
1547 /*
1548 * If prio_read() fails it'll call cache_set_error and we'll
1549 * tear everything down right away, but if we perhaps checked
1550 * sooner we could avoid journal replay.
1551 */
1552
1553 k = &j->btree_root;
1554
1555 err = "bad btree root";
Kent Overstreetd5cc66e2013-07-24 23:06:40 -07001556 if (bch_btree_ptr_invalid(c, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001557 goto err;
1558
1559 err = "error reading btree root";
Kent Overstreete8e1d462013-07-24 17:27:07 -07001560 c->root = bch_btree_node_get(c, k, j->btree_level, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001561 if (IS_ERR_OR_NULL(c->root))
1562 goto err;
1563
1564 list_del_init(&c->root->list);
1565 rw_unlock(true, c->root);
1566
Kent Overstreetc18536a2013-07-24 17:44:17 -07001567 err = uuid_read(c, j, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001568 if (err)
1569 goto err;
1570
1571 err = "error in recovery";
Kent Overstreetc18536a2013-07-24 17:44:17 -07001572 if (bch_btree_check(c))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001573 goto err;
1574
1575 bch_journal_mark(c, &journal);
1576 bch_btree_gc_finish(c);
1577 pr_debug("btree_check() done");
1578
1579 /*
1580 * bcache_journal_next() can't happen sooner, or
1581 * btree_gc_finish() will give spurious errors about last_gc >
1582 * gc_gen - this is a hack but oh well.
1583 */
1584 bch_journal_next(&c->journal);
1585
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001586 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001587 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001588 if (bch_cache_allocator_start(ca))
1589 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001590
1591 /*
1592 * First place it's safe to allocate: btree_check() and
1593 * btree_gc_finish() have to run before we have buckets to
1594 * allocate, and bch_bucket_alloc_set() might cause a journal
1595 * entry to be written so bcache_journal_next() has to be called
1596 * first.
1597 *
1598 * If the uuids were in the old format we have to rewrite them
1599 * before the next journal entry is written:
1600 */
1601 if (j->version < BCACHE_JSET_VERSION_UUID)
1602 __uuid_write(c);
1603
Kent Overstreetc18536a2013-07-24 17:44:17 -07001604 bch_journal_replay(c, &journal);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001605 } else {
1606 pr_notice("invalidating existing data");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001607
1608 for_each_cache(ca, c, i) {
1609 unsigned j;
1610
1611 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1612 2, SB_JOURNAL_BUCKETS);
1613
1614 for (j = 0; j < ca->sb.keys; j++)
1615 ca->sb.d[j] = ca->sb.first_bucket + j;
1616 }
1617
1618 bch_btree_gc_finish(c);
1619
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001620 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001621 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001622 if (bch_cache_allocator_start(ca))
1623 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001624
1625 mutex_lock(&c->bucket_lock);
1626 for_each_cache(ca, c, i)
1627 bch_prio_write(ca);
1628 mutex_unlock(&c->bucket_lock);
1629
Kent Overstreetcafe5632013-03-23 16:11:31 -07001630 err = "cannot allocate new UUID bucket";
1631 if (__uuid_write(c))
Kent Overstreet72a44512013-10-24 17:19:26 -07001632 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001633
1634 err = "cannot allocate new btree root";
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001635 c->root = bch_btree_node_alloc(c, 0, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001636 if (IS_ERR_OR_NULL(c->root))
Kent Overstreet72a44512013-10-24 17:19:26 -07001637 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001638
1639 bkey_copy_key(&c->root->key, &MAX_KEY);
Kent Overstreetc18536a2013-07-24 17:44:17 -07001640 bch_btree_node_write(c->root, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001641
1642 bch_btree_set_root(c->root);
1643 rw_unlock(true, c->root);
1644
1645 /*
1646 * We don't want to write the first journal entry until
1647 * everything is set up - fortunately journal entries won't be
1648 * written until the SET_CACHE_SYNC() here:
1649 */
1650 SET_CACHE_SYNC(&c->sb, true);
1651
1652 bch_journal_next(&c->journal);
Kent Overstreetc18536a2013-07-24 17:44:17 -07001653 bch_journal_meta(c, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001654 }
1655
Kent Overstreet72a44512013-10-24 17:19:26 -07001656 err = "error starting gc thread";
1657 if (bch_gc_thread_start(c))
1658 goto err;
1659
Kent Overstreetc18536a2013-07-24 17:44:17 -07001660 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001661 c->sb.last_mount = get_seconds();
1662 bcache_write_super(c);
1663
1664 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1665 bch_cached_dev_attach(dc, c);
1666
1667 flash_devs_run(c);
1668
1669 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001670err:
Kent Overstreetc18536a2013-07-24 17:44:17 -07001671 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001672 /* XXX: test this, it's broken */
Kees Cookc8694942013-09-10 21:41:34 -07001673 bch_cache_set_error(c, "%s", err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001674}
1675
1676static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1677{
1678 return ca->sb.block_size == c->sb.block_size &&
1679 ca->sb.bucket_size == c->sb.block_size &&
1680 ca->sb.nr_in_set == c->sb.nr_in_set;
1681}
1682
1683static const char *register_cache_set(struct cache *ca)
1684{
1685 char buf[12];
1686 const char *err = "cannot allocate memory";
1687 struct cache_set *c;
1688
1689 list_for_each_entry(c, &bch_cache_sets, list)
1690 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1691 if (c->cache[ca->sb.nr_this_dev])
1692 return "duplicate cache set member";
1693
1694 if (!can_attach_cache(ca, c))
1695 return "cache sb does not match set";
1696
1697 if (!CACHE_SYNC(&ca->sb))
1698 SET_CACHE_SYNC(&c->sb, false);
1699
1700 goto found;
1701 }
1702
1703 c = bch_cache_set_alloc(&ca->sb);
1704 if (!c)
1705 return err;
1706
1707 err = "error creating kobject";
1708 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1709 kobject_add(&c->internal, &c->kobj, "internal"))
1710 goto err;
1711
1712 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1713 goto err;
1714
1715 bch_debug_init_cache_set(c);
1716
1717 list_add(&c->list, &bch_cache_sets);
1718found:
1719 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1720 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1721 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1722 goto err;
1723
1724 if (ca->sb.seq > c->sb.seq) {
1725 c->sb.version = ca->sb.version;
1726 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1727 c->sb.flags = ca->sb.flags;
1728 c->sb.seq = ca->sb.seq;
1729 pr_debug("set version = %llu", c->sb.version);
1730 }
1731
1732 ca->set = c;
1733 ca->set->cache[ca->sb.nr_this_dev] = ca;
1734 c->cache_by_alloc[c->caches_loaded++] = ca;
1735
1736 if (c->caches_loaded == c->sb.nr_in_set)
1737 run_cache_set(c);
1738
1739 return NULL;
1740err:
1741 bch_cache_set_unregister(c);
1742 return err;
1743}
1744
1745/* Cache device */
1746
1747void bch_cache_release(struct kobject *kobj)
1748{
1749 struct cache *ca = container_of(kobj, struct cache, kobj);
1750
1751 if (ca->set)
1752 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1753
Kent Overstreetcafe5632013-03-23 16:11:31 -07001754 bio_split_pool_free(&ca->bio_split_hook);
1755
Kent Overstreetcafe5632013-03-23 16:11:31 -07001756 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1757 kfree(ca->prio_buckets);
1758 vfree(ca->buckets);
1759
1760 free_heap(&ca->heap);
1761 free_fifo(&ca->unused);
1762 free_fifo(&ca->free_inc);
1763 free_fifo(&ca->free);
1764
1765 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1766 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1767
1768 if (!IS_ERR_OR_NULL(ca->bdev)) {
1769 blk_sync_queue(bdev_get_queue(ca->bdev));
1770 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1771 }
1772
1773 kfree(ca);
1774 module_put(THIS_MODULE);
1775}
1776
1777static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1778{
1779 size_t free;
1780 struct bucket *b;
1781
Kent Overstreetcafe5632013-03-23 16:11:31 -07001782 __module_get(THIS_MODULE);
1783 kobject_init(&ca->kobj, &bch_cache_ktype);
1784
Kent Overstreetcafe5632013-03-23 16:11:31 -07001785 bio_init(&ca->journal.bio);
1786 ca->journal.bio.bi_max_vecs = 8;
1787 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1788
1789 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1790 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1791
1792 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1793 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1794 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1795 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -07001796 !(ca->buckets = vzalloc(sizeof(struct bucket) *
Kent Overstreetcafe5632013-03-23 16:11:31 -07001797 ca->sb.nbuckets)) ||
1798 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1799 2, GFP_KERNEL)) ||
1800 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001801 bio_split_pool_init(&ca->bio_split_hook))
Kent Overstreetf59fce82013-05-15 00:11:26 -07001802 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001803
1804 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1805
Kent Overstreetcafe5632013-03-23 16:11:31 -07001806 for_each_bucket(b, ca)
1807 atomic_set(&b->pin, 0);
1808
1809 if (bch_cache_allocator_init(ca))
1810 goto err;
1811
1812 return 0;
1813err:
1814 kobject_put(&ca->kobj);
1815 return -ENOMEM;
1816}
1817
Kent Overstreetf59fce82013-05-15 00:11:26 -07001818static void register_cache(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001819 struct block_device *bdev, struct cache *ca)
1820{
1821 char name[BDEVNAME_SIZE];
1822 const char *err = "cannot allocate memory";
1823
Kent Overstreetf59fce82013-05-15 00:11:26 -07001824 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001825 ca->bdev = bdev;
1826 ca->bdev->bd_holder = ca;
1827
Kent Overstreetf59fce82013-05-15 00:11:26 -07001828 bio_init(&ca->sb_bio);
1829 ca->sb_bio.bi_max_vecs = 1;
1830 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1831 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1832 get_page(sb_page);
1833
Kent Overstreetcafe5632013-03-23 16:11:31 -07001834 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1835 ca->discard = CACHE_DISCARD(&ca->sb);
1836
Kent Overstreetf59fce82013-05-15 00:11:26 -07001837 if (cache_alloc(sb, ca) != 0)
1838 goto err;
1839
Kent Overstreetcafe5632013-03-23 16:11:31 -07001840 err = "error creating kobject";
1841 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1842 goto err;
1843
1844 err = register_cache_set(ca);
1845 if (err)
1846 goto err;
1847
1848 pr_info("registered cache device %s", bdevname(bdev, name));
Kent Overstreetf59fce82013-05-15 00:11:26 -07001849 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001850err:
Kent Overstreetf59fce82013-05-15 00:11:26 -07001851 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001852 kobject_put(&ca->kobj);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001853}
1854
1855/* Global interfaces/init */
1856
1857static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1858 const char *, size_t);
1859
1860kobj_attribute_write(register, register_bcache);
1861kobj_attribute_write(register_quiet, register_bcache);
1862
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001863static bool bch_is_open_backing(struct block_device *bdev) {
1864 struct cache_set *c, *tc;
1865 struct cached_dev *dc, *t;
1866
1867 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1868 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1869 if (dc->bdev == bdev)
1870 return true;
1871 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1872 if (dc->bdev == bdev)
1873 return true;
1874 return false;
1875}
1876
1877static bool bch_is_open_cache(struct block_device *bdev) {
1878 struct cache_set *c, *tc;
1879 struct cache *ca;
1880 unsigned i;
1881
1882 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1883 for_each_cache(ca, c, i)
1884 if (ca->bdev == bdev)
1885 return true;
1886 return false;
1887}
1888
1889static bool bch_is_open(struct block_device *bdev) {
1890 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1891}
1892
Kent Overstreetcafe5632013-03-23 16:11:31 -07001893static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1894 const char *buffer, size_t size)
1895{
1896 ssize_t ret = size;
1897 const char *err = "cannot allocate memory";
1898 char *path = NULL;
1899 struct cache_sb *sb = NULL;
1900 struct block_device *bdev = NULL;
1901 struct page *sb_page = NULL;
1902
1903 if (!try_module_get(THIS_MODULE))
1904 return -EBUSY;
1905
1906 mutex_lock(&bch_register_lock);
1907
1908 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1909 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1910 goto err;
1911
1912 err = "failed to open device";
1913 bdev = blkdev_get_by_path(strim(path),
1914 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1915 sb);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001916 if (IS_ERR(bdev)) {
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001917 if (bdev == ERR_PTR(-EBUSY)) {
1918 bdev = lookup_bdev(strim(path));
1919 if (!IS_ERR(bdev) && bch_is_open(bdev))
1920 err = "device already registered";
1921 else
1922 err = "device busy";
1923 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001924 goto err;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001925 }
1926
1927 err = "failed to set blocksize";
1928 if (set_blocksize(bdev, 4096))
1929 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001930
1931 err = read_super(sb, bdev, &sb_page);
1932 if (err)
1933 goto err_close;
1934
Kent Overstreet29033812013-04-11 15:14:35 -07001935 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001936 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001937 if (!dc)
1938 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001939
Kent Overstreetf59fce82013-05-15 00:11:26 -07001940 register_bdev(sb, sb_page, bdev, dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001941 } else {
1942 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001943 if (!ca)
1944 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001945
Kent Overstreetf59fce82013-05-15 00:11:26 -07001946 register_cache(sb, sb_page, bdev, ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001947 }
Kent Overstreetf59fce82013-05-15 00:11:26 -07001948out:
1949 if (sb_page)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001950 put_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001951 kfree(sb);
1952 kfree(path);
1953 mutex_unlock(&bch_register_lock);
1954 module_put(THIS_MODULE);
1955 return ret;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001956
1957err_close:
1958 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1959err:
1960 if (attr != &ksysfs_register_quiet)
1961 pr_info("error opening %s: %s", path, err);
1962 ret = -EINVAL;
1963 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001964}
1965
1966static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1967{
1968 if (code == SYS_DOWN ||
1969 code == SYS_HALT ||
1970 code == SYS_POWER_OFF) {
1971 DEFINE_WAIT(wait);
1972 unsigned long start = jiffies;
1973 bool stopped = false;
1974
1975 struct cache_set *c, *tc;
1976 struct cached_dev *dc, *tdc;
1977
1978 mutex_lock(&bch_register_lock);
1979
1980 if (list_empty(&bch_cache_sets) &&
1981 list_empty(&uncached_devices))
1982 goto out;
1983
1984 pr_info("Stopping all devices:");
1985
1986 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1987 bch_cache_set_stop(c);
1988
1989 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1990 bcache_device_stop(&dc->disk);
1991
1992 /* What's a condition variable? */
1993 while (1) {
1994 long timeout = start + 2 * HZ - jiffies;
1995
1996 stopped = list_empty(&bch_cache_sets) &&
1997 list_empty(&uncached_devices);
1998
1999 if (timeout < 0 || stopped)
2000 break;
2001
2002 prepare_to_wait(&unregister_wait, &wait,
2003 TASK_UNINTERRUPTIBLE);
2004
2005 mutex_unlock(&bch_register_lock);
2006 schedule_timeout(timeout);
2007 mutex_lock(&bch_register_lock);
2008 }
2009
2010 finish_wait(&unregister_wait, &wait);
2011
2012 if (stopped)
2013 pr_info("All devices stopped");
2014 else
2015 pr_notice("Timeout waiting for devices to be closed");
2016out:
2017 mutex_unlock(&bch_register_lock);
2018 }
2019
2020 return NOTIFY_DONE;
2021}
2022
2023static struct notifier_block reboot = {
2024 .notifier_call = bcache_reboot,
2025 .priority = INT_MAX, /* before any real devices */
2026};
2027
2028static void bcache_exit(void)
2029{
2030 bch_debug_exit();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002031 bch_request_exit();
2032 bch_btree_exit();
2033 if (bcache_kobj)
2034 kobject_put(bcache_kobj);
2035 if (bcache_wq)
2036 destroy_workqueue(bcache_wq);
2037 unregister_blkdev(bcache_major, "bcache");
2038 unregister_reboot_notifier(&reboot);
2039}
2040
2041static int __init bcache_init(void)
2042{
2043 static const struct attribute *files[] = {
2044 &ksysfs_register.attr,
2045 &ksysfs_register_quiet.attr,
2046 NULL
2047 };
2048
2049 mutex_init(&bch_register_lock);
2050 init_waitqueue_head(&unregister_wait);
2051 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07002052 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002053
2054 bcache_major = register_blkdev(0, "bcache");
2055 if (bcache_major < 0)
2056 return bcache_major;
2057
2058 if (!(bcache_wq = create_workqueue("bcache")) ||
2059 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2060 sysfs_create_files(bcache_kobj, files) ||
2061 bch_btree_init() ||
2062 bch_request_init() ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07002063 bch_debug_init(bcache_kobj))
2064 goto err;
2065
2066 return 0;
2067err:
2068 bcache_exit();
2069 return -ENOMEM;
2070}
2071
2072module_exit(bcache_exit);
2073module_init(bcache_init);