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