blob: f1e69f2fad37dcd219601b796ed5c46fb5385ef2 [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"
13
14#include <linux/buffer_head.h>
15#include <linux/debugfs.h>
16#include <linux/genhd.h>
17#include <linux/module.h>
18#include <linux/random.h>
19#include <linux/reboot.h>
20#include <linux/sysfs.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
24
25static const char bcache_magic[] = {
26 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
27 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
28};
29
30static const char invalid_uuid[] = {
31 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
32 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
33};
34
35/* Default is -1; we skip past it for struct cached_dev's cache mode */
36const char * const bch_cache_modes[] = {
37 "default",
38 "writethrough",
39 "writeback",
40 "writearound",
41 "none",
42 NULL
43};
44
45struct uuid_entry_v0 {
46 uint8_t uuid[16];
47 uint8_t label[32];
48 uint32_t first_reg;
49 uint32_t last_reg;
50 uint32_t invalidated;
51 uint32_t pad;
52};
53
54static struct kobject *bcache_kobj;
55struct mutex bch_register_lock;
56LIST_HEAD(bch_cache_sets);
57static LIST_HEAD(uncached_devices);
58
59static int bcache_major, bcache_minor;
60static wait_queue_head_t unregister_wait;
61struct workqueue_struct *bcache_wq;
62
63#define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
64
65static void bio_split_pool_free(struct bio_split_pool *p)
66{
Kent Overstreet8ef74792013-04-05 13:46:13 -070067 if (p->bio_split_hook)
68 mempool_destroy(p->bio_split_hook);
69
Kent Overstreetcafe5632013-03-23 16:11:31 -070070 if (p->bio_split)
71 bioset_free(p->bio_split);
Kent Overstreetcafe5632013-03-23 16:11:31 -070072}
73
74static int bio_split_pool_init(struct bio_split_pool *p)
75{
76 p->bio_split = bioset_create(4, 0);
77 if (!p->bio_split)
78 return -ENOMEM;
79
80 p->bio_split_hook = mempool_create_kmalloc_pool(4,
81 sizeof(struct bio_split_hook));
82 if (!p->bio_split_hook)
83 return -ENOMEM;
84
85 return 0;
86}
87
88/* Superblock */
89
90static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
91 struct page **res)
92{
93 const char *err;
94 struct cache_sb *s;
95 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
96 unsigned i;
97
98 if (!bh)
99 return "IO error";
100
101 s = (struct cache_sb *) bh->b_data;
102
103 sb->offset = le64_to_cpu(s->offset);
104 sb->version = le64_to_cpu(s->version);
105
106 memcpy(sb->magic, s->magic, 16);
107 memcpy(sb->uuid, s->uuid, 16);
108 memcpy(sb->set_uuid, s->set_uuid, 16);
109 memcpy(sb->label, s->label, SB_LABEL_SIZE);
110
111 sb->flags = le64_to_cpu(s->flags);
112 sb->seq = le64_to_cpu(s->seq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700113 sb->last_mount = le32_to_cpu(s->last_mount);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700114 sb->first_bucket = le16_to_cpu(s->first_bucket);
115 sb->keys = le16_to_cpu(s->keys);
116
117 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
118 sb->d[i] = le64_to_cpu(s->d[i]);
119
120 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
121 sb->version, sb->flags, sb->seq, sb->keys);
122
123 err = "Not a bcache superblock";
124 if (sb->offset != SB_SECTOR)
125 goto err;
126
127 if (memcmp(sb->magic, bcache_magic, 16))
128 goto err;
129
130 err = "Too many journal buckets";
131 if (sb->keys > SB_JOURNAL_BUCKETS)
132 goto err;
133
134 err = "Bad checksum";
135 if (s->csum != csum_set(s))
136 goto err;
137
138 err = "Bad UUID";
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600139 if (bch_is_zero(sb->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700140 goto err;
141
Kent Overstreet29033812013-04-11 15:14:35 -0700142 switch (sb->version) {
143 case BCACHE_SB_VERSION_BDEV:
144 sb->block_size = le16_to_cpu(s->block_size);
145 sb->data_offset = BDEV_DATA_START_DEFAULT;
146 break;
147 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
148 sb->block_size = le16_to_cpu(s->block_size);
149 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;
341
342 BUG_ON(!parent);
343 closure_lock(&c->uuid_write, parent);
344
345 for (i = 0; i < KEY_PTRS(k); i++) {
346 struct bio *bio = bch_bbio_alloc(c);
347
348 bio->bi_rw = REQ_SYNC|REQ_META|rw;
349 bio->bi_size = KEY_SIZE(k) << 9;
350
351 bio->bi_end_io = uuid_endio;
352 bio->bi_private = cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600353 bch_bio_map(bio, c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700354
355 bch_submit_bbio(bio, c, k, i);
356
357 if (!(rw & WRITE))
358 break;
359 }
360
361 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read",
362 pkey(&c->uuid_bucket));
363
364 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600365 if (!bch_is_zero(u->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700366 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
367 u - c->uuids, u->uuid, u->label,
368 u->first_reg, u->last_reg, u->invalidated);
369
370 closure_return(cl);
371}
372
373static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
374{
375 struct bkey *k = &j->uuid_bucket;
376
377 if (__bch_ptr_invalid(c, 1, k))
378 return "bad uuid pointer";
379
380 bkey_copy(&c->uuid_bucket, k);
381 uuid_io(c, READ_SYNC, k, cl);
382
383 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
384 struct uuid_entry_v0 *u0 = (void *) c->uuids;
385 struct uuid_entry *u1 = (void *) c->uuids;
386 int i;
387
388 closure_sync(cl);
389
390 /*
391 * Since the new uuid entry is bigger than the old, we have to
392 * convert starting at the highest memory address and work down
393 * in order to do it in place
394 */
395
396 for (i = c->nr_uuids - 1;
397 i >= 0;
398 --i) {
399 memcpy(u1[i].uuid, u0[i].uuid, 16);
400 memcpy(u1[i].label, u0[i].label, 32);
401
402 u1[i].first_reg = u0[i].first_reg;
403 u1[i].last_reg = u0[i].last_reg;
404 u1[i].invalidated = u0[i].invalidated;
405
406 u1[i].flags = 0;
407 u1[i].sectors = 0;
408 }
409 }
410
411 return NULL;
412}
413
414static int __uuid_write(struct cache_set *c)
415{
416 BKEY_PADDED(key) k;
417 struct closure cl;
418 closure_init_stack(&cl);
419
420 lockdep_assert_held(&bch_register_lock);
421
422 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, &cl))
423 return 1;
424
425 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
426 uuid_io(c, REQ_WRITE, &k.key, &cl);
427 closure_sync(&cl);
428
429 bkey_copy(&c->uuid_bucket, &k.key);
430 __bkey_put(c, &k.key);
431 return 0;
432}
433
434int bch_uuid_write(struct cache_set *c)
435{
436 int ret = __uuid_write(c);
437
438 if (!ret)
439 bch_journal_meta(c, NULL);
440
441 return ret;
442}
443
444static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
445{
446 struct uuid_entry *u;
447
448 for (u = c->uuids;
449 u < c->uuids + c->nr_uuids; u++)
450 if (!memcmp(u->uuid, uuid, 16))
451 return u;
452
453 return NULL;
454}
455
456static struct uuid_entry *uuid_find_empty(struct cache_set *c)
457{
458 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
459 return uuid_find(c, zero_uuid);
460}
461
462/*
463 * Bucket priorities/gens:
464 *
465 * For each bucket, we store on disk its
466 * 8 bit gen
467 * 16 bit priority
468 *
469 * See alloc.c for an explanation of the gen. The priority is used to implement
470 * lru (and in the future other) cache replacement policies; for most purposes
471 * it's just an opaque integer.
472 *
473 * The gens and the priorities don't have a whole lot to do with each other, and
474 * it's actually the gens that must be written out at specific times - it's no
475 * big deal if the priorities don't get written, if we lose them we just reuse
476 * buckets in suboptimal order.
477 *
478 * On disk they're stored in a packed array, and in as many buckets are required
479 * to fit them all. The buckets we use to store them form a list; the journal
480 * header points to the first bucket, the first bucket points to the second
481 * bucket, et cetera.
482 *
483 * This code is used by the allocation code; periodically (whenever it runs out
484 * of buckets to allocate from) the allocation code will invalidate some
485 * buckets, but it can't use those buckets until their new gens are safely on
486 * disk.
487 */
488
489static void prio_endio(struct bio *bio, int error)
490{
491 struct cache *ca = bio->bi_private;
492
493 cache_set_err_on(error, ca->set, "accessing priorities");
494 bch_bbio_free(bio, ca->set);
495 closure_put(&ca->prio);
496}
497
498static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
499{
500 struct closure *cl = &ca->prio;
501 struct bio *bio = bch_bbio_alloc(ca->set);
502
503 closure_init_stack(cl);
504
505 bio->bi_sector = bucket * ca->sb.bucket_size;
506 bio->bi_bdev = ca->bdev;
507 bio->bi_rw = REQ_SYNC|REQ_META|rw;
508 bio->bi_size = bucket_bytes(ca);
509
510 bio->bi_end_io = prio_endio;
511 bio->bi_private = ca;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600512 bch_bio_map(bio, ca->disk_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700513
514 closure_bio_submit(bio, &ca->prio, ca);
515 closure_sync(cl);
516}
517
518#define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
519 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
520
521void bch_prio_write(struct cache *ca)
522{
523 int i;
524 struct bucket *b;
525 struct closure cl;
526
527 closure_init_stack(&cl);
528
529 lockdep_assert_held(&ca->set->bucket_lock);
530
531 for (b = ca->buckets;
532 b < ca->buckets + ca->sb.nbuckets; b++)
533 b->disk_gen = b->gen;
534
535 ca->disk_buckets->seq++;
536
537 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
538 &ca->meta_sectors_written);
539
540 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
541 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
542 blktrace_msg(ca, "Starting priorities: " buckets_free(ca));
543
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];
558 p->magic = pset_magic(ca);
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
561 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, &cl);
562 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
609 if (p->magic != pset_magic(ca))
610 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;
626 if (atomic_read(&d->closing))
627 return -ENXIO;
628
629 closure_get(&d->cl);
630 return 0;
631}
632
633static int release_dev(struct gendisk *b, fmode_t mode)
634{
635 struct bcache_device *d = b->private_data;
636 closure_put(&d->cl);
637 return 0;
638}
639
640static int ioctl_dev(struct block_device *b, fmode_t mode,
641 unsigned int cmd, unsigned long arg)
642{
643 struct bcache_device *d = b->bd_disk->private_data;
644 return d->ioctl(d, mode, cmd, arg);
645}
646
647static const struct block_device_operations bcache_ops = {
648 .open = open_dev,
649 .release = release_dev,
650 .ioctl = ioctl_dev,
651 .owner = THIS_MODULE,
652};
653
654void bcache_device_stop(struct bcache_device *d)
655{
656 if (!atomic_xchg(&d->closing, 1))
657 closure_queue(&d->cl);
658}
659
660static void bcache_device_detach(struct bcache_device *d)
661{
662 lockdep_assert_held(&bch_register_lock);
663
664 if (atomic_read(&d->detaching)) {
665 struct uuid_entry *u = d->c->uuids + d->id;
666
667 SET_UUID_FLASH_ONLY(u, 0);
668 memcpy(u->uuid, invalid_uuid, 16);
669 u->invalidated = cpu_to_le32(get_seconds());
670 bch_uuid_write(d->c);
671
672 atomic_set(&d->detaching, 0);
673 }
674
675 d->c->devices[d->id] = NULL;
676 closure_put(&d->c->caching);
677 d->c = NULL;
678}
679
680static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
681 unsigned id)
682{
683 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
684
685 d->id = id;
686 d->c = c;
687 c->devices[id] = d;
688
689 closure_get(&c->caching);
690}
691
692static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
693 const char *name)
694{
695 snprintf(d->name, BCACHEDEVNAME_SIZE,
696 "%s%u", name, d->id);
697
698 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
699 sysfs_create_link(&c->kobj, &d->kobj, d->name),
700 "Couldn't create device <-> cache set symlinks");
701}
702
703static void bcache_device_free(struct bcache_device *d)
704{
705 lockdep_assert_held(&bch_register_lock);
706
707 pr_info("%s stopped", d->disk->disk_name);
708
709 if (d->c)
710 bcache_device_detach(d);
711
712 if (d->disk)
713 del_gendisk(d->disk);
714 if (d->disk && d->disk->queue)
715 blk_cleanup_queue(d->disk->queue);
716 if (d->disk)
717 put_disk(d->disk);
718
719 bio_split_pool_free(&d->bio_split_hook);
720 if (d->unaligned_bvec)
721 mempool_destroy(d->unaligned_bvec);
722 if (d->bio_split)
723 bioset_free(d->bio_split);
724
725 closure_debug_destroy(&d->cl);
726}
727
728static int bcache_device_init(struct bcache_device *d, unsigned block_size)
729{
730 struct request_queue *q;
731
732 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
733 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
734 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
735 bio_split_pool_init(&d->bio_split_hook))
736
737 return -ENOMEM;
738
739 d->disk = alloc_disk(1);
740 if (!d->disk)
741 return -ENOMEM;
742
743 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
744
745 d->disk->major = bcache_major;
746 d->disk->first_minor = bcache_minor++;
747 d->disk->fops = &bcache_ops;
748 d->disk->private_data = d;
749
750 q = blk_alloc_queue(GFP_KERNEL);
751 if (!q)
752 return -ENOMEM;
753
754 blk_queue_make_request(q, NULL);
755 d->disk->queue = q;
756 q->queuedata = d;
757 q->backing_dev_info.congested_data = d;
758 q->limits.max_hw_sectors = UINT_MAX;
759 q->limits.max_sectors = UINT_MAX;
760 q->limits.max_segment_size = UINT_MAX;
761 q->limits.max_segments = BIO_MAX_PAGES;
762 q->limits.max_discard_sectors = UINT_MAX;
763 q->limits.io_min = block_size;
764 q->limits.logical_block_size = block_size;
765 q->limits.physical_block_size = block_size;
766 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
767 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
768
769 return 0;
770}
771
772/* Cached device */
773
774static void calc_cached_dev_sectors(struct cache_set *c)
775{
776 uint64_t sectors = 0;
777 struct cached_dev *dc;
778
779 list_for_each_entry(dc, &c->cached_devs, list)
780 sectors += bdev_sectors(dc->bdev);
781
782 c->cached_dev_sectors = sectors;
783}
784
785void bch_cached_dev_run(struct cached_dev *dc)
786{
787 struct bcache_device *d = &dc->disk;
788
789 if (atomic_xchg(&dc->running, 1))
790 return;
791
792 if (!d->c &&
793 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
794 struct closure cl;
795 closure_init_stack(&cl);
796
797 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
798 bch_write_bdev_super(dc, &cl);
799 closure_sync(&cl);
800 }
801
802 add_disk(d->disk);
803#if 0
804 char *env[] = { "SYMLINK=label" , NULL };
805 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
806#endif
807 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
808 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
809 pr_debug("error creating sysfs link");
810}
811
812static void cached_dev_detach_finish(struct work_struct *w)
813{
814 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
815 char buf[BDEVNAME_SIZE];
816 struct closure cl;
817 closure_init_stack(&cl);
818
819 BUG_ON(!atomic_read(&dc->disk.detaching));
820 BUG_ON(atomic_read(&dc->count));
821
822 sysfs_remove_link(&dc->disk.c->kobj, dc->disk.name);
823 sysfs_remove_link(&dc->disk.kobj, "cache");
824
825 mutex_lock(&bch_register_lock);
826
827 memset(&dc->sb.set_uuid, 0, 16);
828 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
829
830 bch_write_bdev_super(dc, &cl);
831 closure_sync(&cl);
832
833 bcache_device_detach(&dc->disk);
834 list_move(&dc->list, &uncached_devices);
835
836 mutex_unlock(&bch_register_lock);
837
838 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
839
840 /* Drop ref we took in cached_dev_detach() */
841 closure_put(&dc->disk.cl);
842}
843
844void bch_cached_dev_detach(struct cached_dev *dc)
845{
846 lockdep_assert_held(&bch_register_lock);
847
848 if (atomic_read(&dc->disk.closing))
849 return;
850
851 if (atomic_xchg(&dc->disk.detaching, 1))
852 return;
853
854 /*
855 * Block the device from being closed and freed until we're finished
856 * detaching
857 */
858 closure_get(&dc->disk.cl);
859
860 bch_writeback_queue(dc);
861 cached_dev_put(dc);
862}
863
864int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
865{
866 uint32_t rtime = cpu_to_le32(get_seconds());
867 struct uuid_entry *u;
868 char buf[BDEVNAME_SIZE];
869
870 bdevname(dc->bdev, buf);
871
872 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
873 return -ENOENT;
874
875 if (dc->disk.c) {
876 pr_err("Can't attach %s: already attached", buf);
877 return -EINVAL;
878 }
879
880 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
881 pr_err("Can't attach %s: shutting down", buf);
882 return -EINVAL;
883 }
884
885 if (dc->sb.block_size < c->sb.block_size) {
886 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700887 pr_err("Couldn't attach %s: block size less than set's block size",
888 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700889 return -EINVAL;
890 }
891
892 u = uuid_find(c, dc->sb.uuid);
893
894 if (u &&
895 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
896 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
897 memcpy(u->uuid, invalid_uuid, 16);
898 u->invalidated = cpu_to_le32(get_seconds());
899 u = NULL;
900 }
901
902 if (!u) {
903 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
904 pr_err("Couldn't find uuid for %s in set", buf);
905 return -ENOENT;
906 }
907
908 u = uuid_find_empty(c);
909 if (!u) {
910 pr_err("Not caching %s, no room for UUID", buf);
911 return -EINVAL;
912 }
913 }
914
915 /* Deadlocks since we're called via sysfs...
916 sysfs_remove_file(&dc->kobj, &sysfs_attach);
917 */
918
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600919 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700920 struct closure cl;
921 closure_init_stack(&cl);
922
923 memcpy(u->uuid, dc->sb.uuid, 16);
924 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
925 u->first_reg = u->last_reg = rtime;
926 bch_uuid_write(c);
927
928 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
929 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
930
931 bch_write_bdev_super(dc, &cl);
932 closure_sync(&cl);
933 } else {
934 u->last_reg = rtime;
935 bch_uuid_write(c);
936 }
937
938 bcache_device_attach(&dc->disk, c, u - c->uuids);
939 bcache_device_link(&dc->disk, c, "bdev");
940 list_move(&dc->list, &c->cached_devs);
941 calc_cached_dev_sectors(c);
942
943 smp_wmb();
944 /*
945 * dc->c must be set before dc->count != 0 - paired with the mb in
946 * cached_dev_get()
947 */
948 atomic_set(&dc->count, 1);
949
950 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
951 atomic_set(&dc->has_dirty, 1);
952 atomic_inc(&dc->count);
953 bch_writeback_queue(dc);
954 }
955
956 bch_cached_dev_run(dc);
957
958 pr_info("Caching %s as %s on set %pU",
959 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
960 dc->disk.c->sb.set_uuid);
961 return 0;
962}
963
964void bch_cached_dev_release(struct kobject *kobj)
965{
966 struct cached_dev *dc = container_of(kobj, struct cached_dev,
967 disk.kobj);
968 kfree(dc);
969 module_put(THIS_MODULE);
970}
971
972static void cached_dev_free(struct closure *cl)
973{
974 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
975
976 cancel_delayed_work_sync(&dc->writeback_rate_update);
977
978 mutex_lock(&bch_register_lock);
979
980 bcache_device_free(&dc->disk);
981 list_del(&dc->list);
982
983 mutex_unlock(&bch_register_lock);
984
985 if (!IS_ERR_OR_NULL(dc->bdev)) {
986 blk_sync_queue(bdev_get_queue(dc->bdev));
987 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
988 }
989
990 wake_up(&unregister_wait);
991
992 kobject_put(&dc->disk.kobj);
993}
994
995static void cached_dev_flush(struct closure *cl)
996{
997 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
998 struct bcache_device *d = &dc->disk;
999
1000 bch_cache_accounting_destroy(&dc->accounting);
1001 kobject_del(&d->kobj);
1002
1003 continue_at(cl, cached_dev_free, system_wq);
1004}
1005
1006static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1007{
1008 int err;
1009 struct io *io;
1010
1011 closure_init(&dc->disk.cl, NULL);
1012 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1013
1014 __module_get(THIS_MODULE);
1015 INIT_LIST_HEAD(&dc->list);
1016 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1017
1018 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1019
1020 err = bcache_device_init(&dc->disk, block_size);
1021 if (err)
1022 goto err;
1023
1024 spin_lock_init(&dc->io_lock);
1025 closure_init_unlocked(&dc->sb_write);
1026 INIT_WORK(&dc->detach, cached_dev_detach_finish);
1027
1028 dc->sequential_merge = true;
1029 dc->sequential_cutoff = 4 << 20;
1030
1031 INIT_LIST_HEAD(&dc->io_lru);
1032 dc->sb_bio.bi_max_vecs = 1;
1033 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1034
1035 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1036 list_add(&io->lru, &dc->io_lru);
1037 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1038 }
1039
1040 bch_writeback_init_cached_dev(dc);
1041 return 0;
1042err:
1043 bcache_device_stop(&dc->disk);
1044 return err;
1045}
1046
1047/* Cached device - bcache superblock */
1048
1049static const char *register_bdev(struct cache_sb *sb, struct page *sb_page,
1050 struct block_device *bdev,
1051 struct cached_dev *dc)
1052{
1053 char name[BDEVNAME_SIZE];
1054 const char *err = "cannot allocate memory";
1055 struct gendisk *g;
1056 struct cache_set *c;
1057
1058 if (!dc || cached_dev_init(dc, sb->block_size << 9) != 0)
1059 return err;
1060
1061 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1062 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1063 dc->bdev = bdev;
1064 dc->bdev->bd_holder = dc;
1065
1066 g = dc->disk.disk;
1067
Kent Overstreet29033812013-04-11 15:14:35 -07001068 set_capacity(g, dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001069
1070 bch_cached_dev_request_init(dc);
1071
1072 err = "error creating kobject";
1073 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1074 "bcache"))
1075 goto err;
1076 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1077 goto err;
1078
1079 list_add(&dc->list, &uncached_devices);
1080 list_for_each_entry(c, &bch_cache_sets, list)
1081 bch_cached_dev_attach(dc, c);
1082
1083 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1084 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1085 bch_cached_dev_run(dc);
1086
1087 return NULL;
1088err:
1089 kobject_put(&dc->disk.kobj);
1090 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1091 /*
1092 * Return NULL instead of an error because kobject_put() cleans
1093 * everything up
1094 */
1095 return NULL;
1096}
1097
1098/* Flash only volumes */
1099
1100void bch_flash_dev_release(struct kobject *kobj)
1101{
1102 struct bcache_device *d = container_of(kobj, struct bcache_device,
1103 kobj);
1104 kfree(d);
1105}
1106
1107static void flash_dev_free(struct closure *cl)
1108{
1109 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1110 bcache_device_free(d);
1111 kobject_put(&d->kobj);
1112}
1113
1114static void flash_dev_flush(struct closure *cl)
1115{
1116 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1117
1118 sysfs_remove_link(&d->c->kobj, d->name);
1119 sysfs_remove_link(&d->kobj, "cache");
1120 kobject_del(&d->kobj);
1121 continue_at(cl, flash_dev_free, system_wq);
1122}
1123
1124static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1125{
1126 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1127 GFP_KERNEL);
1128 if (!d)
1129 return -ENOMEM;
1130
1131 closure_init(&d->cl, NULL);
1132 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1133
1134 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1135
1136 if (bcache_device_init(d, block_bytes(c)))
1137 goto err;
1138
1139 bcache_device_attach(d, c, u - c->uuids);
1140 set_capacity(d->disk, u->sectors);
1141 bch_flash_dev_request_init(d);
1142 add_disk(d->disk);
1143
1144 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1145 goto err;
1146
1147 bcache_device_link(d, c, "volume");
1148
1149 return 0;
1150err:
1151 kobject_put(&d->kobj);
1152 return -ENOMEM;
1153}
1154
1155static int flash_devs_run(struct cache_set *c)
1156{
1157 int ret = 0;
1158 struct uuid_entry *u;
1159
1160 for (u = c->uuids;
1161 u < c->uuids + c->nr_uuids && !ret;
1162 u++)
1163 if (UUID_FLASH_ONLY(u))
1164 ret = flash_dev_run(c, u);
1165
1166 return ret;
1167}
1168
1169int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1170{
1171 struct uuid_entry *u;
1172
1173 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1174 return -EINTR;
1175
1176 u = uuid_find_empty(c);
1177 if (!u) {
1178 pr_err("Can't create volume, no room for UUID");
1179 return -EINVAL;
1180 }
1181
1182 get_random_bytes(u->uuid, 16);
1183 memset(u->label, 0, 32);
1184 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1185
1186 SET_UUID_FLASH_ONLY(u, 1);
1187 u->sectors = size >> 9;
1188
1189 bch_uuid_write(c);
1190
1191 return flash_dev_run(c, u);
1192}
1193
1194/* Cache set */
1195
1196__printf(2, 3)
1197bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1198{
1199 va_list args;
1200
1201 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1202 return false;
1203
1204 /* XXX: we can be called from atomic context
1205 acquire_console_sem();
1206 */
1207
1208 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1209
1210 va_start(args, fmt);
1211 vprintk(fmt, args);
1212 va_end(args);
1213
1214 printk(", disabling caching\n");
1215
1216 bch_cache_set_unregister(c);
1217 return true;
1218}
1219
1220void bch_cache_set_release(struct kobject *kobj)
1221{
1222 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1223 kfree(c);
1224 module_put(THIS_MODULE);
1225}
1226
1227static void cache_set_free(struct closure *cl)
1228{
1229 struct cache_set *c = container_of(cl, struct cache_set, cl);
1230 struct cache *ca;
1231 unsigned i;
1232
1233 if (!IS_ERR_OR_NULL(c->debug))
1234 debugfs_remove(c->debug);
1235
1236 bch_open_buckets_free(c);
1237 bch_btree_cache_free(c);
1238 bch_journal_free(c);
1239
1240 for_each_cache(ca, c, i)
1241 if (ca)
1242 kobject_put(&ca->kobj);
1243
1244 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1245 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1246
1247 kfree(c->fill_iter);
1248 if (c->bio_split)
1249 bioset_free(c->bio_split);
1250 if (c->bio_meta)
1251 mempool_destroy(c->bio_meta);
1252 if (c->search)
1253 mempool_destroy(c->search);
1254 kfree(c->devices);
1255
1256 mutex_lock(&bch_register_lock);
1257 list_del(&c->list);
1258 mutex_unlock(&bch_register_lock);
1259
1260 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1261 wake_up(&unregister_wait);
1262
1263 closure_debug_destroy(&c->cl);
1264 kobject_put(&c->kobj);
1265}
1266
1267static void cache_set_flush(struct closure *cl)
1268{
1269 struct cache_set *c = container_of(cl, struct cache_set, caching);
1270 struct btree *b;
1271
1272 /* Shut down allocator threads */
1273 set_bit(CACHE_SET_STOPPING_2, &c->flags);
1274 wake_up(&c->alloc_wait);
1275
1276 bch_cache_accounting_destroy(&c->accounting);
1277
1278 kobject_put(&c->internal);
1279 kobject_del(&c->kobj);
1280
1281 if (!IS_ERR_OR_NULL(c->root))
1282 list_add(&c->root->list, &c->btree_cache);
1283
1284 /* Should skip this if we're unregistering because of an error */
1285 list_for_each_entry(b, &c->btree_cache, list)
1286 if (btree_node_dirty(b))
1287 bch_btree_write(b, true, NULL);
1288
1289 closure_return(cl);
1290}
1291
1292static void __cache_set_unregister(struct closure *cl)
1293{
1294 struct cache_set *c = container_of(cl, struct cache_set, caching);
1295 struct cached_dev *dc, *t;
1296 size_t i;
1297
1298 mutex_lock(&bch_register_lock);
1299
1300 if (test_bit(CACHE_SET_UNREGISTERING, &c->flags))
1301 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1302 bch_cached_dev_detach(dc);
1303
1304 for (i = 0; i < c->nr_uuids; i++)
1305 if (c->devices[i] && UUID_FLASH_ONLY(&c->uuids[i]))
1306 bcache_device_stop(c->devices[i]);
1307
1308 mutex_unlock(&bch_register_lock);
1309
1310 continue_at(cl, cache_set_flush, system_wq);
1311}
1312
1313void bch_cache_set_stop(struct cache_set *c)
1314{
1315 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1316 closure_queue(&c->caching);
1317}
1318
1319void bch_cache_set_unregister(struct cache_set *c)
1320{
1321 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1322 bch_cache_set_stop(c);
1323}
1324
1325#define alloc_bucket_pages(gfp, c) \
1326 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1327
1328struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1329{
1330 int iter_size;
1331 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1332 if (!c)
1333 return NULL;
1334
1335 __module_get(THIS_MODULE);
1336 closure_init(&c->cl, NULL);
1337 set_closure_fn(&c->cl, cache_set_free, system_wq);
1338
1339 closure_init(&c->caching, &c->cl);
1340 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1341
1342 /* Maybe create continue_at_noreturn() and use it here? */
1343 closure_set_stopped(&c->cl);
1344 closure_put(&c->cl);
1345
1346 kobject_init(&c->kobj, &bch_cache_set_ktype);
1347 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1348
1349 bch_cache_accounting_init(&c->accounting, &c->cl);
1350
1351 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1352 c->sb.block_size = sb->block_size;
1353 c->sb.bucket_size = sb->bucket_size;
1354 c->sb.nr_in_set = sb->nr_in_set;
1355 c->sb.last_mount = sb->last_mount;
1356 c->bucket_bits = ilog2(sb->bucket_size);
1357 c->block_bits = ilog2(sb->block_size);
1358 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1359
1360 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1361 if (c->btree_pages > BTREE_MAX_PAGES)
1362 c->btree_pages = max_t(int, c->btree_pages / 4,
1363 BTREE_MAX_PAGES);
1364
1365 init_waitqueue_head(&c->alloc_wait);
1366 mutex_init(&c->bucket_lock);
1367 mutex_init(&c->fill_lock);
1368 mutex_init(&c->sort_lock);
1369 spin_lock_init(&c->sort_time_lock);
1370 closure_init_unlocked(&c->sb_write);
1371 closure_init_unlocked(&c->uuid_write);
1372 spin_lock_init(&c->btree_read_time_lock);
1373 bch_moving_init_cache_set(c);
1374
1375 INIT_LIST_HEAD(&c->list);
1376 INIT_LIST_HEAD(&c->cached_devs);
1377 INIT_LIST_HEAD(&c->btree_cache);
1378 INIT_LIST_HEAD(&c->btree_cache_freeable);
1379 INIT_LIST_HEAD(&c->btree_cache_freed);
1380 INIT_LIST_HEAD(&c->data_buckets);
1381
1382 c->search = mempool_create_slab_pool(32, bch_search_cache);
1383 if (!c->search)
1384 goto err;
1385
1386 iter_size = (sb->bucket_size / sb->block_size + 1) *
1387 sizeof(struct btree_iter_set);
1388
1389 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1390 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1391 sizeof(struct bbio) + sizeof(struct bio_vec) *
1392 bucket_pages(c))) ||
1393 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
1394 !(c->fill_iter = kmalloc(iter_size, GFP_KERNEL)) ||
1395 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1396 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1397 bch_journal_alloc(c) ||
1398 bch_btree_cache_alloc(c) ||
1399 bch_open_buckets_alloc(c))
1400 goto err;
1401
1402 c->fill_iter->size = sb->bucket_size / sb->block_size;
1403
1404 c->congested_read_threshold_us = 2000;
1405 c->congested_write_threshold_us = 20000;
1406 c->error_limit = 8 << IO_ERROR_SHIFT;
1407
1408 return c;
1409err:
1410 bch_cache_set_unregister(c);
1411 return NULL;
1412}
1413
1414static void run_cache_set(struct cache_set *c)
1415{
1416 const char *err = "cannot allocate memory";
1417 struct cached_dev *dc, *t;
1418 struct cache *ca;
1419 unsigned i;
1420
1421 struct btree_op op;
1422 bch_btree_op_init_stack(&op);
1423 op.lock = SHRT_MAX;
1424
1425 for_each_cache(ca, c, i)
1426 c->nbuckets += ca->sb.nbuckets;
1427
1428 if (CACHE_SYNC(&c->sb)) {
1429 LIST_HEAD(journal);
1430 struct bkey *k;
1431 struct jset *j;
1432
1433 err = "cannot allocate memory for journal";
1434 if (bch_journal_read(c, &journal, &op))
1435 goto err;
1436
1437 pr_debug("btree_journal_read() done");
1438
1439 err = "no journal entries found";
1440 if (list_empty(&journal))
1441 goto err;
1442
1443 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1444
1445 err = "IO error reading priorities";
1446 for_each_cache(ca, c, i)
1447 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1448
1449 /*
1450 * If prio_read() fails it'll call cache_set_error and we'll
1451 * tear everything down right away, but if we perhaps checked
1452 * sooner we could avoid journal replay.
1453 */
1454
1455 k = &j->btree_root;
1456
1457 err = "bad btree root";
1458 if (__bch_ptr_invalid(c, j->btree_level + 1, k))
1459 goto err;
1460
1461 err = "error reading btree root";
1462 c->root = bch_btree_node_get(c, k, j->btree_level, &op);
1463 if (IS_ERR_OR_NULL(c->root))
1464 goto err;
1465
1466 list_del_init(&c->root->list);
1467 rw_unlock(true, c->root);
1468
1469 err = uuid_read(c, j, &op.cl);
1470 if (err)
1471 goto err;
1472
1473 err = "error in recovery";
1474 if (bch_btree_check(c, &op))
1475 goto err;
1476
1477 bch_journal_mark(c, &journal);
1478 bch_btree_gc_finish(c);
1479 pr_debug("btree_check() done");
1480
1481 /*
1482 * bcache_journal_next() can't happen sooner, or
1483 * btree_gc_finish() will give spurious errors about last_gc >
1484 * gc_gen - this is a hack but oh well.
1485 */
1486 bch_journal_next(&c->journal);
1487
1488 for_each_cache(ca, c, i)
1489 closure_call(&ca->alloc, bch_allocator_thread,
1490 system_wq, &c->cl);
1491
1492 /*
1493 * First place it's safe to allocate: btree_check() and
1494 * btree_gc_finish() have to run before we have buckets to
1495 * allocate, and bch_bucket_alloc_set() might cause a journal
1496 * entry to be written so bcache_journal_next() has to be called
1497 * first.
1498 *
1499 * If the uuids were in the old format we have to rewrite them
1500 * before the next journal entry is written:
1501 */
1502 if (j->version < BCACHE_JSET_VERSION_UUID)
1503 __uuid_write(c);
1504
1505 bch_journal_replay(c, &journal, &op);
1506 } else {
1507 pr_notice("invalidating existing data");
1508 /* Don't want invalidate_buckets() to queue a gc yet */
1509 closure_lock(&c->gc, NULL);
1510
1511 for_each_cache(ca, c, i) {
1512 unsigned j;
1513
1514 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1515 2, SB_JOURNAL_BUCKETS);
1516
1517 for (j = 0; j < ca->sb.keys; j++)
1518 ca->sb.d[j] = ca->sb.first_bucket + j;
1519 }
1520
1521 bch_btree_gc_finish(c);
1522
1523 for_each_cache(ca, c, i)
1524 closure_call(&ca->alloc, bch_allocator_thread,
1525 ca->alloc_workqueue, &c->cl);
1526
1527 mutex_lock(&c->bucket_lock);
1528 for_each_cache(ca, c, i)
1529 bch_prio_write(ca);
1530 mutex_unlock(&c->bucket_lock);
1531
1532 wake_up(&c->alloc_wait);
1533
1534 err = "cannot allocate new UUID bucket";
1535 if (__uuid_write(c))
1536 goto err_unlock_gc;
1537
1538 err = "cannot allocate new btree root";
1539 c->root = bch_btree_node_alloc(c, 0, &op.cl);
1540 if (IS_ERR_OR_NULL(c->root))
1541 goto err_unlock_gc;
1542
1543 bkey_copy_key(&c->root->key, &MAX_KEY);
1544 bch_btree_write(c->root, true, &op);
1545
1546 bch_btree_set_root(c->root);
1547 rw_unlock(true, c->root);
1548
1549 /*
1550 * We don't want to write the first journal entry until
1551 * everything is set up - fortunately journal entries won't be
1552 * written until the SET_CACHE_SYNC() here:
1553 */
1554 SET_CACHE_SYNC(&c->sb, true);
1555
1556 bch_journal_next(&c->journal);
1557 bch_journal_meta(c, &op.cl);
1558
1559 /* Unlock */
1560 closure_set_stopped(&c->gc.cl);
1561 closure_put(&c->gc.cl);
1562 }
1563
1564 closure_sync(&op.cl);
1565 c->sb.last_mount = get_seconds();
1566 bcache_write_super(c);
1567
1568 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1569 bch_cached_dev_attach(dc, c);
1570
1571 flash_devs_run(c);
1572
1573 return;
1574err_unlock_gc:
1575 closure_set_stopped(&c->gc.cl);
1576 closure_put(&c->gc.cl);
1577err:
1578 closure_sync(&op.cl);
1579 /* XXX: test this, it's broken */
1580 bch_cache_set_error(c, err);
1581}
1582
1583static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1584{
1585 return ca->sb.block_size == c->sb.block_size &&
1586 ca->sb.bucket_size == c->sb.block_size &&
1587 ca->sb.nr_in_set == c->sb.nr_in_set;
1588}
1589
1590static const char *register_cache_set(struct cache *ca)
1591{
1592 char buf[12];
1593 const char *err = "cannot allocate memory";
1594 struct cache_set *c;
1595
1596 list_for_each_entry(c, &bch_cache_sets, list)
1597 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1598 if (c->cache[ca->sb.nr_this_dev])
1599 return "duplicate cache set member";
1600
1601 if (!can_attach_cache(ca, c))
1602 return "cache sb does not match set";
1603
1604 if (!CACHE_SYNC(&ca->sb))
1605 SET_CACHE_SYNC(&c->sb, false);
1606
1607 goto found;
1608 }
1609
1610 c = bch_cache_set_alloc(&ca->sb);
1611 if (!c)
1612 return err;
1613
1614 err = "error creating kobject";
1615 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1616 kobject_add(&c->internal, &c->kobj, "internal"))
1617 goto err;
1618
1619 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1620 goto err;
1621
1622 bch_debug_init_cache_set(c);
1623
1624 list_add(&c->list, &bch_cache_sets);
1625found:
1626 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1627 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1628 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1629 goto err;
1630
1631 if (ca->sb.seq > c->sb.seq) {
1632 c->sb.version = ca->sb.version;
1633 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1634 c->sb.flags = ca->sb.flags;
1635 c->sb.seq = ca->sb.seq;
1636 pr_debug("set version = %llu", c->sb.version);
1637 }
1638
1639 ca->set = c;
1640 ca->set->cache[ca->sb.nr_this_dev] = ca;
1641 c->cache_by_alloc[c->caches_loaded++] = ca;
1642
1643 if (c->caches_loaded == c->sb.nr_in_set)
1644 run_cache_set(c);
1645
1646 return NULL;
1647err:
1648 bch_cache_set_unregister(c);
1649 return err;
1650}
1651
1652/* Cache device */
1653
1654void bch_cache_release(struct kobject *kobj)
1655{
1656 struct cache *ca = container_of(kobj, struct cache, kobj);
1657
1658 if (ca->set)
1659 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1660
1661 bch_cache_allocator_exit(ca);
1662
1663 bio_split_pool_free(&ca->bio_split_hook);
1664
1665 if (ca->alloc_workqueue)
1666 destroy_workqueue(ca->alloc_workqueue);
1667
1668 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1669 kfree(ca->prio_buckets);
1670 vfree(ca->buckets);
1671
1672 free_heap(&ca->heap);
1673 free_fifo(&ca->unused);
1674 free_fifo(&ca->free_inc);
1675 free_fifo(&ca->free);
1676
1677 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1678 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1679
1680 if (!IS_ERR_OR_NULL(ca->bdev)) {
1681 blk_sync_queue(bdev_get_queue(ca->bdev));
1682 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1683 }
1684
1685 kfree(ca);
1686 module_put(THIS_MODULE);
1687}
1688
1689static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1690{
1691 size_t free;
1692 struct bucket *b;
1693
1694 if (!ca)
1695 return -ENOMEM;
1696
1697 __module_get(THIS_MODULE);
1698 kobject_init(&ca->kobj, &bch_cache_ktype);
1699
1700 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
1701
1702 INIT_LIST_HEAD(&ca->discards);
1703
1704 bio_init(&ca->sb_bio);
1705 ca->sb_bio.bi_max_vecs = 1;
1706 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1707
1708 bio_init(&ca->journal.bio);
1709 ca->journal.bio.bi_max_vecs = 8;
1710 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1711
1712 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1713 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1714
1715 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1716 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1717 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1718 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
1719 !(ca->buckets = vmalloc(sizeof(struct bucket) *
1720 ca->sb.nbuckets)) ||
1721 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1722 2, GFP_KERNEL)) ||
1723 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
1724 !(ca->alloc_workqueue = alloc_workqueue("bch_allocator", 0, 1)) ||
1725 bio_split_pool_init(&ca->bio_split_hook))
1726 goto err;
1727
1728 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1729
1730 memset(ca->buckets, 0, ca->sb.nbuckets * sizeof(struct bucket));
1731 for_each_bucket(b, ca)
1732 atomic_set(&b->pin, 0);
1733
1734 if (bch_cache_allocator_init(ca))
1735 goto err;
1736
1737 return 0;
1738err:
1739 kobject_put(&ca->kobj);
1740 return -ENOMEM;
1741}
1742
1743static const char *register_cache(struct cache_sb *sb, struct page *sb_page,
1744 struct block_device *bdev, struct cache *ca)
1745{
1746 char name[BDEVNAME_SIZE];
1747 const char *err = "cannot allocate memory";
1748
1749 if (cache_alloc(sb, ca) != 0)
1750 return err;
1751
1752 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1753 ca->bdev = bdev;
1754 ca->bdev->bd_holder = ca;
1755
1756 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1757 ca->discard = CACHE_DISCARD(&ca->sb);
1758
1759 err = "error creating kobject";
1760 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1761 goto err;
1762
1763 err = register_cache_set(ca);
1764 if (err)
1765 goto err;
1766
1767 pr_info("registered cache device %s", bdevname(bdev, name));
1768
1769 return NULL;
1770err:
1771 kobject_put(&ca->kobj);
1772 pr_info("error opening %s: %s", bdevname(bdev, name), err);
1773 /* Return NULL instead of an error because kobject_put() cleans
1774 * everything up
1775 */
1776 return NULL;
1777}
1778
1779/* Global interfaces/init */
1780
1781static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1782 const char *, size_t);
1783
1784kobj_attribute_write(register, register_bcache);
1785kobj_attribute_write(register_quiet, register_bcache);
1786
1787static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1788 const char *buffer, size_t size)
1789{
1790 ssize_t ret = size;
1791 const char *err = "cannot allocate memory";
1792 char *path = NULL;
1793 struct cache_sb *sb = NULL;
1794 struct block_device *bdev = NULL;
1795 struct page *sb_page = NULL;
1796
1797 if (!try_module_get(THIS_MODULE))
1798 return -EBUSY;
1799
1800 mutex_lock(&bch_register_lock);
1801
1802 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1803 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1804 goto err;
1805
1806 err = "failed to open device";
1807 bdev = blkdev_get_by_path(strim(path),
1808 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1809 sb);
1810 if (bdev == ERR_PTR(-EBUSY))
1811 err = "device busy";
1812
1813 if (IS_ERR(bdev) ||
1814 set_blocksize(bdev, 4096))
1815 goto err;
1816
1817 err = read_super(sb, bdev, &sb_page);
1818 if (err)
1819 goto err_close;
1820
Kent Overstreet29033812013-04-11 15:14:35 -07001821 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001822 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1823
1824 err = register_bdev(sb, sb_page, bdev, dc);
1825 } else {
1826 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1827
1828 err = register_cache(sb, sb_page, bdev, ca);
1829 }
1830
1831 if (err) {
1832 /* register_(bdev|cache) will only return an error if they
1833 * didn't get far enough to create the kobject - if they did,
1834 * the kobject destructor will do this cleanup.
1835 */
1836 put_page(sb_page);
1837err_close:
1838 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1839err:
1840 if (attr != &ksysfs_register_quiet)
1841 pr_info("error opening %s: %s", path, err);
1842 ret = -EINVAL;
1843 }
1844
1845 kfree(sb);
1846 kfree(path);
1847 mutex_unlock(&bch_register_lock);
1848 module_put(THIS_MODULE);
1849 return ret;
1850}
1851
1852static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1853{
1854 if (code == SYS_DOWN ||
1855 code == SYS_HALT ||
1856 code == SYS_POWER_OFF) {
1857 DEFINE_WAIT(wait);
1858 unsigned long start = jiffies;
1859 bool stopped = false;
1860
1861 struct cache_set *c, *tc;
1862 struct cached_dev *dc, *tdc;
1863
1864 mutex_lock(&bch_register_lock);
1865
1866 if (list_empty(&bch_cache_sets) &&
1867 list_empty(&uncached_devices))
1868 goto out;
1869
1870 pr_info("Stopping all devices:");
1871
1872 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1873 bch_cache_set_stop(c);
1874
1875 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1876 bcache_device_stop(&dc->disk);
1877
1878 /* What's a condition variable? */
1879 while (1) {
1880 long timeout = start + 2 * HZ - jiffies;
1881
1882 stopped = list_empty(&bch_cache_sets) &&
1883 list_empty(&uncached_devices);
1884
1885 if (timeout < 0 || stopped)
1886 break;
1887
1888 prepare_to_wait(&unregister_wait, &wait,
1889 TASK_UNINTERRUPTIBLE);
1890
1891 mutex_unlock(&bch_register_lock);
1892 schedule_timeout(timeout);
1893 mutex_lock(&bch_register_lock);
1894 }
1895
1896 finish_wait(&unregister_wait, &wait);
1897
1898 if (stopped)
1899 pr_info("All devices stopped");
1900 else
1901 pr_notice("Timeout waiting for devices to be closed");
1902out:
1903 mutex_unlock(&bch_register_lock);
1904 }
1905
1906 return NOTIFY_DONE;
1907}
1908
1909static struct notifier_block reboot = {
1910 .notifier_call = bcache_reboot,
1911 .priority = INT_MAX, /* before any real devices */
1912};
1913
1914static void bcache_exit(void)
1915{
1916 bch_debug_exit();
1917 bch_writeback_exit();
1918 bch_request_exit();
1919 bch_btree_exit();
1920 if (bcache_kobj)
1921 kobject_put(bcache_kobj);
1922 if (bcache_wq)
1923 destroy_workqueue(bcache_wq);
1924 unregister_blkdev(bcache_major, "bcache");
1925 unregister_reboot_notifier(&reboot);
1926}
1927
1928static int __init bcache_init(void)
1929{
1930 static const struct attribute *files[] = {
1931 &ksysfs_register.attr,
1932 &ksysfs_register_quiet.attr,
1933 NULL
1934 };
1935
1936 mutex_init(&bch_register_lock);
1937 init_waitqueue_head(&unregister_wait);
1938 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07001939 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07001940
1941 bcache_major = register_blkdev(0, "bcache");
1942 if (bcache_major < 0)
1943 return bcache_major;
1944
1945 if (!(bcache_wq = create_workqueue("bcache")) ||
1946 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
1947 sysfs_create_files(bcache_kobj, files) ||
1948 bch_btree_init() ||
1949 bch_request_init() ||
1950 bch_writeback_init() ||
1951 bch_debug_init(bcache_kobj))
1952 goto err;
1953
1954 return 0;
1955err:
1956 bcache_exit();
1957 return -ENOMEM;
1958}
1959
1960module_exit(bcache_exit);
1961module_init(bcache_init);