blob: f6a62174e8f6ac6e448e7f259a3bf74fe0392291 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "btree.h"
11#include "debug.h"
12#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070013#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
Kent Overstreetc37511b2013-04-26 15:39:55 -070015#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070016#include <linux/buffer_head.h>
17#include <linux/debugfs.h>
18#include <linux/genhd.h>
19#include <linux/module.h>
20#include <linux/random.h>
21#include <linux/reboot.h>
22#include <linux/sysfs.h>
23
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
26
27static const char bcache_magic[] = {
28 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
29 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
30};
31
32static const char invalid_uuid[] = {
33 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
34 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
35};
36
37/* Default is -1; we skip past it for struct cached_dev's cache mode */
38const char * const bch_cache_modes[] = {
39 "default",
40 "writethrough",
41 "writeback",
42 "writearound",
43 "none",
44 NULL
45};
46
47struct uuid_entry_v0 {
48 uint8_t uuid[16];
49 uint8_t label[32];
50 uint32_t first_reg;
51 uint32_t last_reg;
52 uint32_t invalidated;
53 uint32_t pad;
54};
55
56static struct kobject *bcache_kobj;
57struct mutex bch_register_lock;
58LIST_HEAD(bch_cache_sets);
59static LIST_HEAD(uncached_devices);
60
61static int bcache_major, bcache_minor;
62static wait_queue_head_t unregister_wait;
63struct workqueue_struct *bcache_wq;
64
65#define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
66
67static void bio_split_pool_free(struct bio_split_pool *p)
68{
Kent Overstreet8ef74792013-04-05 13:46:13 -070069 if (p->bio_split_hook)
70 mempool_destroy(p->bio_split_hook);
71
Kent Overstreetcafe5632013-03-23 16:11:31 -070072 if (p->bio_split)
73 bioset_free(p->bio_split);
Kent Overstreetcafe5632013-03-23 16:11:31 -070074}
75
76static int bio_split_pool_init(struct bio_split_pool *p)
77{
78 p->bio_split = bioset_create(4, 0);
79 if (!p->bio_split)
80 return -ENOMEM;
81
82 p->bio_split_hook = mempool_create_kmalloc_pool(4,
83 sizeof(struct bio_split_hook));
84 if (!p->bio_split_hook)
85 return -ENOMEM;
86
87 return 0;
88}
89
90/* Superblock */
91
92static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
93 struct page **res)
94{
95 const char *err;
96 struct cache_sb *s;
97 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
98 unsigned i;
99
100 if (!bh)
101 return "IO error";
102
103 s = (struct cache_sb *) bh->b_data;
104
105 sb->offset = le64_to_cpu(s->offset);
106 sb->version = le64_to_cpu(s->version);
107
108 memcpy(sb->magic, s->magic, 16);
109 memcpy(sb->uuid, s->uuid, 16);
110 memcpy(sb->set_uuid, s->set_uuid, 16);
111 memcpy(sb->label, s->label, SB_LABEL_SIZE);
112
113 sb->flags = le64_to_cpu(s->flags);
114 sb->seq = le64_to_cpu(s->seq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700115 sb->last_mount = le32_to_cpu(s->last_mount);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700116 sb->first_bucket = le16_to_cpu(s->first_bucket);
117 sb->keys = le16_to_cpu(s->keys);
118
119 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
120 sb->d[i] = le64_to_cpu(s->d[i]);
121
122 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
123 sb->version, sb->flags, sb->seq, sb->keys);
124
125 err = "Not a bcache superblock";
126 if (sb->offset != SB_SECTOR)
127 goto err;
128
129 if (memcmp(sb->magic, bcache_magic, 16))
130 goto err;
131
132 err = "Too many journal buckets";
133 if (sb->keys > SB_JOURNAL_BUCKETS)
134 goto err;
135
136 err = "Bad checksum";
137 if (s->csum != csum_set(s))
138 goto err;
139
140 err = "Bad UUID";
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600141 if (bch_is_zero(sb->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700142 goto err;
143
Kent Overstreet8abb2a52013-04-23 21:51:48 -0700144 sb->block_size = le16_to_cpu(s->block_size);
145
146 err = "Superblock block size smaller than device block size";
147 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
148 goto err;
149
Kent Overstreet29033812013-04-11 15:14:35 -0700150 switch (sb->version) {
151 case BCACHE_SB_VERSION_BDEV:
Kent Overstreet29033812013-04-11 15:14:35 -0700152 sb->data_offset = BDEV_DATA_START_DEFAULT;
153 break;
154 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
Kent Overstreet29033812013-04-11 15:14:35 -0700155 sb->data_offset = le64_to_cpu(s->data_offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156
Kent Overstreet29033812013-04-11 15:14:35 -0700157 err = "Bad data offset";
158 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700159 goto err;
160
Kent Overstreet29033812013-04-11 15:14:35 -0700161 break;
162 case BCACHE_SB_VERSION_CDEV:
163 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
164 sb->nbuckets = le64_to_cpu(s->nbuckets);
165 sb->block_size = le16_to_cpu(s->block_size);
166 sb->bucket_size = le16_to_cpu(s->bucket_size);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700167
Kent Overstreet29033812013-04-11 15:14:35 -0700168 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
169 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
170
171 err = "Too many buckets";
172 if (sb->nbuckets > LONG_MAX)
173 goto err;
174
175 err = "Not enough buckets";
176 if (sb->nbuckets < 1 << 7)
177 goto err;
178
179 err = "Bad block/bucket size";
180 if (!is_power_of_2(sb->block_size) ||
181 sb->block_size > PAGE_SECTORS ||
182 !is_power_of_2(sb->bucket_size) ||
183 sb->bucket_size < PAGE_SECTORS)
184 goto err;
185
186 err = "Invalid superblock: device too small";
187 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
188 goto err;
189
190 err = "Bad UUID";
191 if (bch_is_zero(sb->set_uuid, 16))
192 goto err;
193
194 err = "Bad cache device number in set";
195 if (!sb->nr_in_set ||
196 sb->nr_in_set <= sb->nr_this_dev ||
197 sb->nr_in_set > MAX_CACHES_PER_SET)
198 goto err;
199
200 err = "Journal buckets not sequential";
201 for (i = 0; i < sb->keys; i++)
202 if (sb->d[i] != sb->first_bucket + i)
203 goto err;
204
205 err = "Too many journal buckets";
206 if (sb->first_bucket + sb->keys > sb->nbuckets)
207 goto err;
208
209 err = "Invalid superblock: first bucket comes before end of super";
210 if (sb->first_bucket * sb->bucket_size < 16)
211 goto err;
212
213 break;
214 default:
215 err = "Unsupported superblock version";
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216 goto err;
Kent Overstreet29033812013-04-11 15:14:35 -0700217 }
218
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219 sb->last_mount = get_seconds();
220 err = NULL;
221
222 get_page(bh->b_page);
223 *res = bh->b_page;
224err:
225 put_bh(bh);
226 return err;
227}
228
229static void write_bdev_super_endio(struct bio *bio, int error)
230{
231 struct cached_dev *dc = bio->bi_private;
232 /* XXX: error checking */
233
234 closure_put(&dc->sb_write.cl);
235}
236
237static void __write_super(struct cache_sb *sb, struct bio *bio)
238{
239 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page);
240 unsigned i;
241
242 bio->bi_sector = SB_SECTOR;
243 bio->bi_rw = REQ_SYNC|REQ_META;
244 bio->bi_size = SB_SIZE;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600245 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700246
247 out->offset = cpu_to_le64(sb->offset);
248 out->version = cpu_to_le64(sb->version);
249
250 memcpy(out->uuid, sb->uuid, 16);
251 memcpy(out->set_uuid, sb->set_uuid, 16);
252 memcpy(out->label, sb->label, SB_LABEL_SIZE);
253
254 out->flags = cpu_to_le64(sb->flags);
255 out->seq = cpu_to_le64(sb->seq);
256
257 out->last_mount = cpu_to_le32(sb->last_mount);
258 out->first_bucket = cpu_to_le16(sb->first_bucket);
259 out->keys = cpu_to_le16(sb->keys);
260
261 for (i = 0; i < sb->keys; i++)
262 out->d[i] = cpu_to_le64(sb->d[i]);
263
264 out->csum = csum_set(out);
265
266 pr_debug("ver %llu, flags %llu, seq %llu",
267 sb->version, sb->flags, sb->seq);
268
269 submit_bio(REQ_WRITE, bio);
270}
271
272void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
273{
274 struct closure *cl = &dc->sb_write.cl;
275 struct bio *bio = &dc->sb_bio;
276
277 closure_lock(&dc->sb_write, parent);
278
279 bio_reset(bio);
280 bio->bi_bdev = dc->bdev;
281 bio->bi_end_io = write_bdev_super_endio;
282 bio->bi_private = dc;
283
284 closure_get(cl);
285 __write_super(&dc->sb, bio);
286
287 closure_return(cl);
288}
289
290static void write_super_endio(struct bio *bio, int error)
291{
292 struct cache *ca = bio->bi_private;
293
294 bch_count_io_errors(ca, error, "writing superblock");
295 closure_put(&ca->set->sb_write.cl);
296}
297
298void bcache_write_super(struct cache_set *c)
299{
300 struct closure *cl = &c->sb_write.cl;
301 struct cache *ca;
302 unsigned i;
303
304 closure_lock(&c->sb_write, &c->cl);
305
306 c->sb.seq++;
307
308 for_each_cache(ca, c, i) {
309 struct bio *bio = &ca->sb_bio;
310
Kent Overstreet29033812013-04-11 15:14:35 -0700311 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700312 ca->sb.seq = c->sb.seq;
313 ca->sb.last_mount = c->sb.last_mount;
314
315 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
316
317 bio_reset(bio);
318 bio->bi_bdev = ca->bdev;
319 bio->bi_end_io = write_super_endio;
320 bio->bi_private = ca;
321
322 closure_get(cl);
323 __write_super(&ca->sb, bio);
324 }
325
326 closure_return(cl);
327}
328
329/* UUID io */
330
331static void uuid_endio(struct bio *bio, int error)
332{
333 struct closure *cl = bio->bi_private;
334 struct cache_set *c = container_of(cl, struct cache_set, uuid_write.cl);
335
336 cache_set_err_on(error, c, "accessing uuids");
337 bch_bbio_free(bio, c);
338 closure_put(cl);
339}
340
341static void uuid_io(struct cache_set *c, unsigned long rw,
342 struct bkey *k, struct closure *parent)
343{
344 struct closure *cl = &c->uuid_write.cl;
345 struct uuid_entry *u;
346 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -0700347 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700348
349 BUG_ON(!parent);
350 closure_lock(&c->uuid_write, parent);
351
352 for (i = 0; i < KEY_PTRS(k); i++) {
353 struct bio *bio = bch_bbio_alloc(c);
354
355 bio->bi_rw = REQ_SYNC|REQ_META|rw;
356 bio->bi_size = KEY_SIZE(k) << 9;
357
358 bio->bi_end_io = uuid_endio;
359 bio->bi_private = cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600360 bch_bio_map(bio, c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700361
362 bch_submit_bbio(bio, c, k, i);
363
364 if (!(rw & WRITE))
365 break;
366 }
367
Kent Overstreet85b14922013-05-14 20:33:16 -0700368 bch_bkey_to_text(buf, sizeof(buf), k);
369 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700370
371 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600372 if (!bch_is_zero(u->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700373 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
374 u - c->uuids, u->uuid, u->label,
375 u->first_reg, u->last_reg, u->invalidated);
376
377 closure_return(cl);
378}
379
380static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
381{
382 struct bkey *k = &j->uuid_bucket;
383
384 if (__bch_ptr_invalid(c, 1, k))
385 return "bad uuid pointer";
386
387 bkey_copy(&c->uuid_bucket, k);
388 uuid_io(c, READ_SYNC, k, cl);
389
390 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
391 struct uuid_entry_v0 *u0 = (void *) c->uuids;
392 struct uuid_entry *u1 = (void *) c->uuids;
393 int i;
394
395 closure_sync(cl);
396
397 /*
398 * Since the new uuid entry is bigger than the old, we have to
399 * convert starting at the highest memory address and work down
400 * in order to do it in place
401 */
402
403 for (i = c->nr_uuids - 1;
404 i >= 0;
405 --i) {
406 memcpy(u1[i].uuid, u0[i].uuid, 16);
407 memcpy(u1[i].label, u0[i].label, 32);
408
409 u1[i].first_reg = u0[i].first_reg;
410 u1[i].last_reg = u0[i].last_reg;
411 u1[i].invalidated = u0[i].invalidated;
412
413 u1[i].flags = 0;
414 u1[i].sectors = 0;
415 }
416 }
417
418 return NULL;
419}
420
421static int __uuid_write(struct cache_set *c)
422{
423 BKEY_PADDED(key) k;
424 struct closure cl;
425 closure_init_stack(&cl);
426
427 lockdep_assert_held(&bch_register_lock);
428
429 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, &cl))
430 return 1;
431
432 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
433 uuid_io(c, REQ_WRITE, &k.key, &cl);
434 closure_sync(&cl);
435
436 bkey_copy(&c->uuid_bucket, &k.key);
437 __bkey_put(c, &k.key);
438 return 0;
439}
440
441int bch_uuid_write(struct cache_set *c)
442{
443 int ret = __uuid_write(c);
444
445 if (!ret)
446 bch_journal_meta(c, NULL);
447
448 return ret;
449}
450
451static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
452{
453 struct uuid_entry *u;
454
455 for (u = c->uuids;
456 u < c->uuids + c->nr_uuids; u++)
457 if (!memcmp(u->uuid, uuid, 16))
458 return u;
459
460 return NULL;
461}
462
463static struct uuid_entry *uuid_find_empty(struct cache_set *c)
464{
465 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
466 return uuid_find(c, zero_uuid);
467}
468
469/*
470 * Bucket priorities/gens:
471 *
472 * For each bucket, we store on disk its
473 * 8 bit gen
474 * 16 bit priority
475 *
476 * See alloc.c for an explanation of the gen. The priority is used to implement
477 * lru (and in the future other) cache replacement policies; for most purposes
478 * it's just an opaque integer.
479 *
480 * The gens and the priorities don't have a whole lot to do with each other, and
481 * it's actually the gens that must be written out at specific times - it's no
482 * big deal if the priorities don't get written, if we lose them we just reuse
483 * buckets in suboptimal order.
484 *
485 * On disk they're stored in a packed array, and in as many buckets are required
486 * to fit them all. The buckets we use to store them form a list; the journal
487 * header points to the first bucket, the first bucket points to the second
488 * bucket, et cetera.
489 *
490 * This code is used by the allocation code; periodically (whenever it runs out
491 * of buckets to allocate from) the allocation code will invalidate some
492 * buckets, but it can't use those buckets until their new gens are safely on
493 * disk.
494 */
495
496static void prio_endio(struct bio *bio, int error)
497{
498 struct cache *ca = bio->bi_private;
499
500 cache_set_err_on(error, ca->set, "accessing priorities");
501 bch_bbio_free(bio, ca->set);
502 closure_put(&ca->prio);
503}
504
505static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
506{
507 struct closure *cl = &ca->prio;
508 struct bio *bio = bch_bbio_alloc(ca->set);
509
510 closure_init_stack(cl);
511
512 bio->bi_sector = bucket * ca->sb.bucket_size;
513 bio->bi_bdev = ca->bdev;
514 bio->bi_rw = REQ_SYNC|REQ_META|rw;
515 bio->bi_size = bucket_bytes(ca);
516
517 bio->bi_end_io = prio_endio;
518 bio->bi_private = ca;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600519 bch_bio_map(bio, ca->disk_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700520
521 closure_bio_submit(bio, &ca->prio, ca);
522 closure_sync(cl);
523}
524
525#define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
526 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
527
528void bch_prio_write(struct cache *ca)
529{
530 int i;
531 struct bucket *b;
532 struct closure cl;
533
534 closure_init_stack(&cl);
535
536 lockdep_assert_held(&ca->set->bucket_lock);
537
538 for (b = ca->buckets;
539 b < ca->buckets + ca->sb.nbuckets; b++)
540 b->disk_gen = b->gen;
541
542 ca->disk_buckets->seq++;
543
544 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
545 &ca->meta_sectors_written);
546
547 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
548 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700549
550 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
551 long bucket;
552 struct prio_set *p = ca->disk_buckets;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700553 struct bucket_disk *d = p->data;
554 struct bucket_disk *end = d + prios_per_bucket(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700555
556 for (b = ca->buckets + i * prios_per_bucket(ca);
557 b < ca->buckets + ca->sb.nbuckets && d < end;
558 b++, d++) {
559 d->prio = cpu_to_le16(b->prio);
560 d->gen = b->gen;
561 }
562
563 p->next_bucket = ca->prio_buckets[i + 1];
564 p->magic = pset_magic(ca);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600565 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700566
567 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, &cl);
568 BUG_ON(bucket == -1);
569
570 mutex_unlock(&ca->set->bucket_lock);
571 prio_io(ca, bucket, REQ_WRITE);
572 mutex_lock(&ca->set->bucket_lock);
573
574 ca->prio_buckets[i] = bucket;
575 atomic_dec_bug(&ca->buckets[bucket].pin);
576 }
577
578 mutex_unlock(&ca->set->bucket_lock);
579
580 bch_journal_meta(ca->set, &cl);
581 closure_sync(&cl);
582
583 mutex_lock(&ca->set->bucket_lock);
584
585 ca->need_save_prio = 0;
586
587 /*
588 * Don't want the old priorities to get garbage collected until after we
589 * finish writing the new ones, and they're journalled
590 */
591 for (i = 0; i < prio_buckets(ca); i++)
592 ca->prio_last_buckets[i] = ca->prio_buckets[i];
593}
594
595static void prio_read(struct cache *ca, uint64_t bucket)
596{
597 struct prio_set *p = ca->disk_buckets;
598 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
599 struct bucket *b;
600 unsigned bucket_nr = 0;
601
602 for (b = ca->buckets;
603 b < ca->buckets + ca->sb.nbuckets;
604 b++, d++) {
605 if (d == end) {
606 ca->prio_buckets[bucket_nr] = bucket;
607 ca->prio_last_buckets[bucket_nr] = bucket;
608 bucket_nr++;
609
610 prio_io(ca, bucket, READ_SYNC);
611
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600612 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700613 pr_warn("bad csum reading priorities");
614
615 if (p->magic != pset_magic(ca))
616 pr_warn("bad magic reading priorities");
617
618 bucket = p->next_bucket;
619 d = p->data;
620 }
621
622 b->prio = le16_to_cpu(d->prio);
623 b->gen = b->disk_gen = b->last_gc = b->gc_gen = d->gen;
624 }
625}
626
627/* Bcache device */
628
629static int open_dev(struct block_device *b, fmode_t mode)
630{
631 struct bcache_device *d = b->bd_disk->private_data;
632 if (atomic_read(&d->closing))
633 return -ENXIO;
634
635 closure_get(&d->cl);
636 return 0;
637}
638
Emil Goode867e1162013-05-09 22:39:26 +0200639static void release_dev(struct gendisk *b, fmode_t mode)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700640{
641 struct bcache_device *d = b->private_data;
642 closure_put(&d->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700643}
644
645static int ioctl_dev(struct block_device *b, fmode_t mode,
646 unsigned int cmd, unsigned long arg)
647{
648 struct bcache_device *d = b->bd_disk->private_data;
649 return d->ioctl(d, mode, cmd, arg);
650}
651
652static const struct block_device_operations bcache_ops = {
653 .open = open_dev,
654 .release = release_dev,
655 .ioctl = ioctl_dev,
656 .owner = THIS_MODULE,
657};
658
659void bcache_device_stop(struct bcache_device *d)
660{
661 if (!atomic_xchg(&d->closing, 1))
662 closure_queue(&d->cl);
663}
664
Kent Overstreetee668502013-02-01 07:29:41 -0800665static void bcache_device_unlink(struct bcache_device *d)
666{
667 unsigned i;
668 struct cache *ca;
669
670 sysfs_remove_link(&d->c->kobj, d->name);
671 sysfs_remove_link(&d->kobj, "cache");
672
673 for_each_cache(ca, d->c, i)
674 bd_unlink_disk_holder(ca->bdev, d->disk);
675}
676
677static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
678 const char *name)
679{
680 unsigned i;
681 struct cache *ca;
682
683 for_each_cache(ca, d->c, i)
684 bd_link_disk_holder(ca->bdev, d->disk);
685
686 snprintf(d->name, BCACHEDEVNAME_SIZE,
687 "%s%u", name, d->id);
688
689 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
690 sysfs_create_link(&c->kobj, &d->kobj, d->name),
691 "Couldn't create device <-> cache set symlinks");
692}
693
Kent Overstreetcafe5632013-03-23 16:11:31 -0700694static void bcache_device_detach(struct bcache_device *d)
695{
696 lockdep_assert_held(&bch_register_lock);
697
698 if (atomic_read(&d->detaching)) {
699 struct uuid_entry *u = d->c->uuids + d->id;
700
701 SET_UUID_FLASH_ONLY(u, 0);
702 memcpy(u->uuid, invalid_uuid, 16);
703 u->invalidated = cpu_to_le32(get_seconds());
704 bch_uuid_write(d->c);
705
706 atomic_set(&d->detaching, 0);
707 }
708
Kent Overstreetc9502ea2013-07-10 21:25:02 -0700709 if (!d->flush_done)
710 bcache_device_unlink(d);
Kent Overstreetee668502013-02-01 07:29:41 -0800711
Kent Overstreetcafe5632013-03-23 16:11:31 -0700712 d->c->devices[d->id] = NULL;
713 closure_put(&d->c->caching);
714 d->c = NULL;
715}
716
717static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
718 unsigned id)
719{
720 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
721
722 d->id = id;
723 d->c = c;
724 c->devices[id] = d;
725
726 closure_get(&c->caching);
727}
728
Kent Overstreetcafe5632013-03-23 16:11:31 -0700729static void bcache_device_free(struct bcache_device *d)
730{
731 lockdep_assert_held(&bch_register_lock);
732
733 pr_info("%s stopped", d->disk->disk_name);
734
735 if (d->c)
736 bcache_device_detach(d);
Kent Overstreetf59fce82013-05-15 00:11:26 -0700737 if (d->disk && d->disk->flags & GENHD_FL_UP)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700738 del_gendisk(d->disk);
739 if (d->disk && d->disk->queue)
740 blk_cleanup_queue(d->disk->queue);
741 if (d->disk)
742 put_disk(d->disk);
743
744 bio_split_pool_free(&d->bio_split_hook);
745 if (d->unaligned_bvec)
746 mempool_destroy(d->unaligned_bvec);
747 if (d->bio_split)
748 bioset_free(d->bio_split);
Kent Overstreet279afba2013-06-05 06:21:07 -0700749 if (is_vmalloc_addr(d->stripe_sectors_dirty))
750 vfree(d->stripe_sectors_dirty);
751 else
752 kfree(d->stripe_sectors_dirty);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700753
754 closure_debug_destroy(&d->cl);
755}
756
Kent Overstreet279afba2013-06-05 06:21:07 -0700757static int bcache_device_init(struct bcache_device *d, unsigned block_size,
758 sector_t sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700759{
760 struct request_queue *q;
Kent Overstreet279afba2013-06-05 06:21:07 -0700761 size_t n;
762
763 if (!d->stripe_size_bits)
764 d->stripe_size_bits = 31;
765
766 d->nr_stripes = round_up(sectors, 1 << d->stripe_size_bits) >>
767 d->stripe_size_bits;
768
769 if (!d->nr_stripes || d->nr_stripes > SIZE_MAX / sizeof(atomic_t))
770 return -ENOMEM;
771
772 n = d->nr_stripes * sizeof(atomic_t);
773 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
774 ? kzalloc(n, GFP_KERNEL)
775 : vzalloc(n);
776 if (!d->stripe_sectors_dirty)
777 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700778
779 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
780 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
781 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -0700782 bio_split_pool_init(&d->bio_split_hook) ||
783 !(d->disk = alloc_disk(1)) ||
784 !(q = blk_alloc_queue(GFP_KERNEL)))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700785 return -ENOMEM;
786
Kent Overstreet279afba2013-06-05 06:21:07 -0700787 set_capacity(d->disk, sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700788 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
789
790 d->disk->major = bcache_major;
791 d->disk->first_minor = bcache_minor++;
792 d->disk->fops = &bcache_ops;
793 d->disk->private_data = d;
794
Kent Overstreetcafe5632013-03-23 16:11:31 -0700795 blk_queue_make_request(q, NULL);
796 d->disk->queue = q;
797 q->queuedata = d;
798 q->backing_dev_info.congested_data = d;
799 q->limits.max_hw_sectors = UINT_MAX;
800 q->limits.max_sectors = UINT_MAX;
801 q->limits.max_segment_size = UINT_MAX;
802 q->limits.max_segments = BIO_MAX_PAGES;
803 q->limits.max_discard_sectors = UINT_MAX;
804 q->limits.io_min = block_size;
805 q->limits.logical_block_size = block_size;
806 q->limits.physical_block_size = block_size;
807 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
808 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
809
Kent Overstreet54d12f22013-07-10 18:44:40 -0700810 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
811
Kent Overstreetcafe5632013-03-23 16:11:31 -0700812 return 0;
813}
814
815/* Cached device */
816
817static void calc_cached_dev_sectors(struct cache_set *c)
818{
819 uint64_t sectors = 0;
820 struct cached_dev *dc;
821
822 list_for_each_entry(dc, &c->cached_devs, list)
823 sectors += bdev_sectors(dc->bdev);
824
825 c->cached_dev_sectors = sectors;
826}
827
828void bch_cached_dev_run(struct cached_dev *dc)
829{
830 struct bcache_device *d = &dc->disk;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200831 char buf[SB_LABEL_SIZE + 1];
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200832 char *env[] = {
833 "DRIVER=bcache",
834 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200835 NULL,
836 NULL,
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200837 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700838
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200839 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
840 buf[SB_LABEL_SIZE] = '\0';
841 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
842
Kent Overstreetcafe5632013-03-23 16:11:31 -0700843 if (atomic_xchg(&dc->running, 1))
844 return;
845
846 if (!d->c &&
847 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
848 struct closure cl;
849 closure_init_stack(&cl);
850
851 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
852 bch_write_bdev_super(dc, &cl);
853 closure_sync(&cl);
854 }
855
856 add_disk(d->disk);
Kent Overstreetee668502013-02-01 07:29:41 -0800857 bd_link_disk_holder(dc->bdev, dc->disk.disk);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200858 /* won't show up in the uevent file, use udevadm monitor -e instead
859 * only class / kset properties are persistent */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700860 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200861 kfree(env[1]);
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200862 kfree(env[2]);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200863
Kent Overstreetcafe5632013-03-23 16:11:31 -0700864 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
865 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
866 pr_debug("error creating sysfs link");
867}
868
869static void cached_dev_detach_finish(struct work_struct *w)
870{
871 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
872 char buf[BDEVNAME_SIZE];
873 struct closure cl;
874 closure_init_stack(&cl);
875
876 BUG_ON(!atomic_read(&dc->disk.detaching));
877 BUG_ON(atomic_read(&dc->count));
878
Kent Overstreetcafe5632013-03-23 16:11:31 -0700879 mutex_lock(&bch_register_lock);
880
881 memset(&dc->sb.set_uuid, 0, 16);
882 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
883
884 bch_write_bdev_super(dc, &cl);
885 closure_sync(&cl);
886
887 bcache_device_detach(&dc->disk);
888 list_move(&dc->list, &uncached_devices);
889
890 mutex_unlock(&bch_register_lock);
891
892 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
893
894 /* Drop ref we took in cached_dev_detach() */
895 closure_put(&dc->disk.cl);
896}
897
898void bch_cached_dev_detach(struct cached_dev *dc)
899{
900 lockdep_assert_held(&bch_register_lock);
901
902 if (atomic_read(&dc->disk.closing))
903 return;
904
905 if (atomic_xchg(&dc->disk.detaching, 1))
906 return;
907
908 /*
909 * Block the device from being closed and freed until we're finished
910 * detaching
911 */
912 closure_get(&dc->disk.cl);
913
914 bch_writeback_queue(dc);
915 cached_dev_put(dc);
916}
917
918int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
919{
920 uint32_t rtime = cpu_to_le32(get_seconds());
921 struct uuid_entry *u;
922 char buf[BDEVNAME_SIZE];
923
924 bdevname(dc->bdev, buf);
925
926 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
927 return -ENOENT;
928
929 if (dc->disk.c) {
930 pr_err("Can't attach %s: already attached", buf);
931 return -EINVAL;
932 }
933
934 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
935 pr_err("Can't attach %s: shutting down", buf);
936 return -EINVAL;
937 }
938
939 if (dc->sb.block_size < c->sb.block_size) {
940 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700941 pr_err("Couldn't attach %s: block size less than set's block size",
942 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700943 return -EINVAL;
944 }
945
946 u = uuid_find(c, dc->sb.uuid);
947
948 if (u &&
949 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
950 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
951 memcpy(u->uuid, invalid_uuid, 16);
952 u->invalidated = cpu_to_le32(get_seconds());
953 u = NULL;
954 }
955
956 if (!u) {
957 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
958 pr_err("Couldn't find uuid for %s in set", buf);
959 return -ENOENT;
960 }
961
962 u = uuid_find_empty(c);
963 if (!u) {
964 pr_err("Not caching %s, no room for UUID", buf);
965 return -EINVAL;
966 }
967 }
968
969 /* Deadlocks since we're called via sysfs...
970 sysfs_remove_file(&dc->kobj, &sysfs_attach);
971 */
972
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600973 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700974 struct closure cl;
975 closure_init_stack(&cl);
976
977 memcpy(u->uuid, dc->sb.uuid, 16);
978 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
979 u->first_reg = u->last_reg = rtime;
980 bch_uuid_write(c);
981
982 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
983 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
984
985 bch_write_bdev_super(dc, &cl);
986 closure_sync(&cl);
987 } else {
988 u->last_reg = rtime;
989 bch_uuid_write(c);
990 }
991
992 bcache_device_attach(&dc->disk, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700993 list_move(&dc->list, &c->cached_devs);
994 calc_cached_dev_sectors(c);
995
996 smp_wmb();
997 /*
998 * dc->c must be set before dc->count != 0 - paired with the mb in
999 * cached_dev_get()
1000 */
1001 atomic_set(&dc->count, 1);
1002
1003 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001004 bch_sectors_dirty_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001005 atomic_set(&dc->has_dirty, 1);
1006 atomic_inc(&dc->count);
1007 bch_writeback_queue(dc);
1008 }
1009
1010 bch_cached_dev_run(dc);
Kent Overstreetee668502013-02-01 07:29:41 -08001011 bcache_device_link(&dc->disk, c, "bdev");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001012
1013 pr_info("Caching %s as %s on set %pU",
1014 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
1015 dc->disk.c->sb.set_uuid);
1016 return 0;
1017}
1018
1019void bch_cached_dev_release(struct kobject *kobj)
1020{
1021 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1022 disk.kobj);
1023 kfree(dc);
1024 module_put(THIS_MODULE);
1025}
1026
1027static void cached_dev_free(struct closure *cl)
1028{
1029 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1030
1031 cancel_delayed_work_sync(&dc->writeback_rate_update);
1032
1033 mutex_lock(&bch_register_lock);
1034
Kent Overstreetf59fce82013-05-15 00:11:26 -07001035 if (atomic_read(&dc->running))
1036 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001037 bcache_device_free(&dc->disk);
1038 list_del(&dc->list);
1039
1040 mutex_unlock(&bch_register_lock);
1041
1042 if (!IS_ERR_OR_NULL(dc->bdev)) {
Kent Overstreetf59fce82013-05-15 00:11:26 -07001043 if (dc->bdev->bd_disk)
1044 blk_sync_queue(bdev_get_queue(dc->bdev));
1045
Kent Overstreetcafe5632013-03-23 16:11:31 -07001046 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1047 }
1048
1049 wake_up(&unregister_wait);
1050
1051 kobject_put(&dc->disk.kobj);
1052}
1053
1054static void cached_dev_flush(struct closure *cl)
1055{
1056 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1057 struct bcache_device *d = &dc->disk;
1058
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001059 mutex_lock(&bch_register_lock);
1060 d->flush_done = 1;
1061
1062 if (d->c)
1063 bcache_device_unlink(d);
1064
1065 mutex_unlock(&bch_register_lock);
1066
Kent Overstreetcafe5632013-03-23 16:11:31 -07001067 bch_cache_accounting_destroy(&dc->accounting);
1068 kobject_del(&d->kobj);
1069
1070 continue_at(cl, cached_dev_free, system_wq);
1071}
1072
1073static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1074{
Kent Overstreetf59fce82013-05-15 00:11:26 -07001075 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076 struct io *io;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001077 struct request_queue *q = bdev_get_queue(dc->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001078
1079 __module_get(THIS_MODULE);
1080 INIT_LIST_HEAD(&dc->list);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001081 closure_init(&dc->disk.cl, NULL);
1082 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001083 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001084 INIT_WORK(&dc->detach, cached_dev_detach_finish);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001085 closure_init_unlocked(&dc->sb_write);
1086 INIT_LIST_HEAD(&dc->io_lru);
1087 spin_lock_init(&dc->io_lock);
1088 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001089
1090 dc->sequential_merge = true;
1091 dc->sequential_cutoff = 4 << 20;
1092
Kent Overstreetcafe5632013-03-23 16:11:31 -07001093 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1094 list_add(&io->lru, &dc->io_lru);
1095 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1096 }
1097
Kent Overstreet279afba2013-06-05 06:21:07 -07001098 ret = bcache_device_init(&dc->disk, block_size,
1099 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001100 if (ret)
1101 return ret;
1102
1103 set_capacity(dc->disk.disk,
1104 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1105
1106 dc->disk.disk->queue->backing_dev_info.ra_pages =
1107 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1108 q->backing_dev_info.ra_pages);
1109
1110 bch_cached_dev_request_init(dc);
1111 bch_cached_dev_writeback_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001112 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001113}
1114
1115/* Cached device - bcache superblock */
1116
Kent Overstreetf59fce82013-05-15 00:11:26 -07001117static void register_bdev(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001118 struct block_device *bdev,
1119 struct cached_dev *dc)
1120{
1121 char name[BDEVNAME_SIZE];
1122 const char *err = "cannot allocate memory";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001123 struct cache_set *c;
1124
Kent Overstreetcafe5632013-03-23 16:11:31 -07001125 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001126 dc->bdev = bdev;
1127 dc->bdev->bd_holder = dc;
1128
Kent Overstreetf59fce82013-05-15 00:11:26 -07001129 bio_init(&dc->sb_bio);
1130 dc->sb_bio.bi_max_vecs = 1;
1131 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1132 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1133 get_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001134
Kent Overstreetf59fce82013-05-15 00:11:26 -07001135 if (cached_dev_init(dc, sb->block_size << 9))
1136 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001137
1138 err = "error creating kobject";
1139 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1140 "bcache"))
1141 goto err;
1142 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1143 goto err;
1144
Kent Overstreetf59fce82013-05-15 00:11:26 -07001145 pr_info("registered backing device %s", bdevname(bdev, name));
1146
Kent Overstreetcafe5632013-03-23 16:11:31 -07001147 list_add(&dc->list, &uncached_devices);
1148 list_for_each_entry(c, &bch_cache_sets, list)
1149 bch_cached_dev_attach(dc, c);
1150
1151 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1152 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1153 bch_cached_dev_run(dc);
1154
Kent Overstreetf59fce82013-05-15 00:11:26 -07001155 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001156err:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001157 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001158 bcache_device_stop(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001159}
1160
1161/* Flash only volumes */
1162
1163void bch_flash_dev_release(struct kobject *kobj)
1164{
1165 struct bcache_device *d = container_of(kobj, struct bcache_device,
1166 kobj);
1167 kfree(d);
1168}
1169
1170static void flash_dev_free(struct closure *cl)
1171{
1172 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1173 bcache_device_free(d);
1174 kobject_put(&d->kobj);
1175}
1176
1177static void flash_dev_flush(struct closure *cl)
1178{
1179 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1180
Kent Overstreetee668502013-02-01 07:29:41 -08001181 bcache_device_unlink(d);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001182 kobject_del(&d->kobj);
1183 continue_at(cl, flash_dev_free, system_wq);
1184}
1185
1186static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1187{
1188 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1189 GFP_KERNEL);
1190 if (!d)
1191 return -ENOMEM;
1192
1193 closure_init(&d->cl, NULL);
1194 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1195
1196 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1197
Kent Overstreet279afba2013-06-05 06:21:07 -07001198 if (bcache_device_init(d, block_bytes(c), u->sectors))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199 goto err;
1200
1201 bcache_device_attach(d, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001202 bch_flash_dev_request_init(d);
1203 add_disk(d->disk);
1204
1205 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1206 goto err;
1207
1208 bcache_device_link(d, c, "volume");
1209
1210 return 0;
1211err:
1212 kobject_put(&d->kobj);
1213 return -ENOMEM;
1214}
1215
1216static int flash_devs_run(struct cache_set *c)
1217{
1218 int ret = 0;
1219 struct uuid_entry *u;
1220
1221 for (u = c->uuids;
1222 u < c->uuids + c->nr_uuids && !ret;
1223 u++)
1224 if (UUID_FLASH_ONLY(u))
1225 ret = flash_dev_run(c, u);
1226
1227 return ret;
1228}
1229
1230int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1231{
1232 struct uuid_entry *u;
1233
1234 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1235 return -EINTR;
1236
1237 u = uuid_find_empty(c);
1238 if (!u) {
1239 pr_err("Can't create volume, no room for UUID");
1240 return -EINVAL;
1241 }
1242
1243 get_random_bytes(u->uuid, 16);
1244 memset(u->label, 0, 32);
1245 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1246
1247 SET_UUID_FLASH_ONLY(u, 1);
1248 u->sectors = size >> 9;
1249
1250 bch_uuid_write(c);
1251
1252 return flash_dev_run(c, u);
1253}
1254
1255/* Cache set */
1256
1257__printf(2, 3)
1258bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1259{
1260 va_list args;
1261
1262 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1263 return false;
1264
1265 /* XXX: we can be called from atomic context
1266 acquire_console_sem();
1267 */
1268
1269 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1270
1271 va_start(args, fmt);
1272 vprintk(fmt, args);
1273 va_end(args);
1274
1275 printk(", disabling caching\n");
1276
1277 bch_cache_set_unregister(c);
1278 return true;
1279}
1280
1281void bch_cache_set_release(struct kobject *kobj)
1282{
1283 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1284 kfree(c);
1285 module_put(THIS_MODULE);
1286}
1287
1288static void cache_set_free(struct closure *cl)
1289{
1290 struct cache_set *c = container_of(cl, struct cache_set, cl);
1291 struct cache *ca;
1292 unsigned i;
1293
1294 if (!IS_ERR_OR_NULL(c->debug))
1295 debugfs_remove(c->debug);
1296
1297 bch_open_buckets_free(c);
1298 bch_btree_cache_free(c);
1299 bch_journal_free(c);
1300
1301 for_each_cache(ca, c, i)
1302 if (ca)
1303 kobject_put(&ca->kobj);
1304
1305 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1306 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1307
Kent Overstreetcafe5632013-03-23 16:11:31 -07001308 if (c->bio_split)
1309 bioset_free(c->bio_split);
Kent Overstreet57943512013-04-25 13:58:35 -07001310 if (c->fill_iter)
1311 mempool_destroy(c->fill_iter);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001312 if (c->bio_meta)
1313 mempool_destroy(c->bio_meta);
1314 if (c->search)
1315 mempool_destroy(c->search);
1316 kfree(c->devices);
1317
1318 mutex_lock(&bch_register_lock);
1319 list_del(&c->list);
1320 mutex_unlock(&bch_register_lock);
1321
1322 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1323 wake_up(&unregister_wait);
1324
1325 closure_debug_destroy(&c->cl);
1326 kobject_put(&c->kobj);
1327}
1328
1329static void cache_set_flush(struct closure *cl)
1330{
1331 struct cache_set *c = container_of(cl, struct cache_set, caching);
1332 struct btree *b;
1333
1334 /* Shut down allocator threads */
1335 set_bit(CACHE_SET_STOPPING_2, &c->flags);
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001336 wake_up_allocators(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001337
1338 bch_cache_accounting_destroy(&c->accounting);
1339
1340 kobject_put(&c->internal);
1341 kobject_del(&c->kobj);
1342
1343 if (!IS_ERR_OR_NULL(c->root))
1344 list_add(&c->root->list, &c->btree_cache);
1345
1346 /* Should skip this if we're unregistering because of an error */
1347 list_for_each_entry(b, &c->btree_cache, list)
1348 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -07001349 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001350
1351 closure_return(cl);
1352}
1353
1354static void __cache_set_unregister(struct closure *cl)
1355{
1356 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001357 struct cached_dev *dc;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001358 size_t i;
1359
1360 mutex_lock(&bch_register_lock);
1361
Kent Overstreetcafe5632013-03-23 16:11:31 -07001362 for (i = 0; i < c->nr_uuids; i++)
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001363 if (c->devices[i]) {
1364 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1365 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1366 dc = container_of(c->devices[i],
1367 struct cached_dev, disk);
1368 bch_cached_dev_detach(dc);
1369 } else {
1370 bcache_device_stop(c->devices[i]);
1371 }
1372 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001373
1374 mutex_unlock(&bch_register_lock);
1375
1376 continue_at(cl, cache_set_flush, system_wq);
1377}
1378
1379void bch_cache_set_stop(struct cache_set *c)
1380{
1381 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1382 closure_queue(&c->caching);
1383}
1384
1385void bch_cache_set_unregister(struct cache_set *c)
1386{
1387 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1388 bch_cache_set_stop(c);
1389}
1390
1391#define alloc_bucket_pages(gfp, c) \
1392 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1393
1394struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1395{
1396 int iter_size;
1397 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1398 if (!c)
1399 return NULL;
1400
1401 __module_get(THIS_MODULE);
1402 closure_init(&c->cl, NULL);
1403 set_closure_fn(&c->cl, cache_set_free, system_wq);
1404
1405 closure_init(&c->caching, &c->cl);
1406 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1407
1408 /* Maybe create continue_at_noreturn() and use it here? */
1409 closure_set_stopped(&c->cl);
1410 closure_put(&c->cl);
1411
1412 kobject_init(&c->kobj, &bch_cache_set_ktype);
1413 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1414
1415 bch_cache_accounting_init(&c->accounting, &c->cl);
1416
1417 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1418 c->sb.block_size = sb->block_size;
1419 c->sb.bucket_size = sb->bucket_size;
1420 c->sb.nr_in_set = sb->nr_in_set;
1421 c->sb.last_mount = sb->last_mount;
1422 c->bucket_bits = ilog2(sb->bucket_size);
1423 c->block_bits = ilog2(sb->block_size);
1424 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1425
1426 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1427 if (c->btree_pages > BTREE_MAX_PAGES)
1428 c->btree_pages = max_t(int, c->btree_pages / 4,
1429 BTREE_MAX_PAGES);
1430
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001431 c->sort_crit_factor = int_sqrt(c->btree_pages);
1432
Kent Overstreetcafe5632013-03-23 16:11:31 -07001433 mutex_init(&c->bucket_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001434 mutex_init(&c->sort_lock);
1435 spin_lock_init(&c->sort_time_lock);
1436 closure_init_unlocked(&c->sb_write);
1437 closure_init_unlocked(&c->uuid_write);
1438 spin_lock_init(&c->btree_read_time_lock);
1439 bch_moving_init_cache_set(c);
1440
1441 INIT_LIST_HEAD(&c->list);
1442 INIT_LIST_HEAD(&c->cached_devs);
1443 INIT_LIST_HEAD(&c->btree_cache);
1444 INIT_LIST_HEAD(&c->btree_cache_freeable);
1445 INIT_LIST_HEAD(&c->btree_cache_freed);
1446 INIT_LIST_HEAD(&c->data_buckets);
1447
1448 c->search = mempool_create_slab_pool(32, bch_search_cache);
1449 if (!c->search)
1450 goto err;
1451
1452 iter_size = (sb->bucket_size / sb->block_size + 1) *
1453 sizeof(struct btree_iter_set);
1454
1455 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1456 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1457 sizeof(struct bbio) + sizeof(struct bio_vec) *
1458 bucket_pages(c))) ||
Kent Overstreet57943512013-04-25 13:58:35 -07001459 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001460 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001461 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1462 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1463 bch_journal_alloc(c) ||
1464 bch_btree_cache_alloc(c) ||
1465 bch_open_buckets_alloc(c))
1466 goto err;
1467
Kent Overstreetcafe5632013-03-23 16:11:31 -07001468 c->congested_read_threshold_us = 2000;
1469 c->congested_write_threshold_us = 20000;
1470 c->error_limit = 8 << IO_ERROR_SHIFT;
1471
1472 return c;
1473err:
1474 bch_cache_set_unregister(c);
1475 return NULL;
1476}
1477
1478static void run_cache_set(struct cache_set *c)
1479{
1480 const char *err = "cannot allocate memory";
1481 struct cached_dev *dc, *t;
1482 struct cache *ca;
1483 unsigned i;
1484
1485 struct btree_op op;
1486 bch_btree_op_init_stack(&op);
1487 op.lock = SHRT_MAX;
1488
1489 for_each_cache(ca, c, i)
1490 c->nbuckets += ca->sb.nbuckets;
1491
1492 if (CACHE_SYNC(&c->sb)) {
1493 LIST_HEAD(journal);
1494 struct bkey *k;
1495 struct jset *j;
1496
1497 err = "cannot allocate memory for journal";
1498 if (bch_journal_read(c, &journal, &op))
1499 goto err;
1500
1501 pr_debug("btree_journal_read() done");
1502
1503 err = "no journal entries found";
1504 if (list_empty(&journal))
1505 goto err;
1506
1507 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1508
1509 err = "IO error reading priorities";
1510 for_each_cache(ca, c, i)
1511 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1512
1513 /*
1514 * If prio_read() fails it'll call cache_set_error and we'll
1515 * tear everything down right away, but if we perhaps checked
1516 * sooner we could avoid journal replay.
1517 */
1518
1519 k = &j->btree_root;
1520
1521 err = "bad btree root";
1522 if (__bch_ptr_invalid(c, j->btree_level + 1, k))
1523 goto err;
1524
1525 err = "error reading btree root";
1526 c->root = bch_btree_node_get(c, k, j->btree_level, &op);
1527 if (IS_ERR_OR_NULL(c->root))
1528 goto err;
1529
1530 list_del_init(&c->root->list);
1531 rw_unlock(true, c->root);
1532
1533 err = uuid_read(c, j, &op.cl);
1534 if (err)
1535 goto err;
1536
1537 err = "error in recovery";
1538 if (bch_btree_check(c, &op))
1539 goto err;
1540
1541 bch_journal_mark(c, &journal);
1542 bch_btree_gc_finish(c);
1543 pr_debug("btree_check() done");
1544
1545 /*
1546 * bcache_journal_next() can't happen sooner, or
1547 * btree_gc_finish() will give spurious errors about last_gc >
1548 * gc_gen - this is a hack but oh well.
1549 */
1550 bch_journal_next(&c->journal);
1551
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001552 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001553 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001554 if (bch_cache_allocator_start(ca))
1555 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001556
1557 /*
1558 * First place it's safe to allocate: btree_check() and
1559 * btree_gc_finish() have to run before we have buckets to
1560 * allocate, and bch_bucket_alloc_set() might cause a journal
1561 * entry to be written so bcache_journal_next() has to be called
1562 * first.
1563 *
1564 * If the uuids were in the old format we have to rewrite them
1565 * before the next journal entry is written:
1566 */
1567 if (j->version < BCACHE_JSET_VERSION_UUID)
1568 __uuid_write(c);
1569
1570 bch_journal_replay(c, &journal, &op);
1571 } else {
1572 pr_notice("invalidating existing data");
1573 /* Don't want invalidate_buckets() to queue a gc yet */
1574 closure_lock(&c->gc, NULL);
1575
1576 for_each_cache(ca, c, i) {
1577 unsigned j;
1578
1579 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1580 2, SB_JOURNAL_BUCKETS);
1581
1582 for (j = 0; j < ca->sb.keys; j++)
1583 ca->sb.d[j] = ca->sb.first_bucket + j;
1584 }
1585
1586 bch_btree_gc_finish(c);
1587
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001588 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001589 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001590 if (bch_cache_allocator_start(ca))
1591 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001592
1593 mutex_lock(&c->bucket_lock);
1594 for_each_cache(ca, c, i)
1595 bch_prio_write(ca);
1596 mutex_unlock(&c->bucket_lock);
1597
Kent Overstreetcafe5632013-03-23 16:11:31 -07001598 err = "cannot allocate new UUID bucket";
1599 if (__uuid_write(c))
1600 goto err_unlock_gc;
1601
1602 err = "cannot allocate new btree root";
1603 c->root = bch_btree_node_alloc(c, 0, &op.cl);
1604 if (IS_ERR_OR_NULL(c->root))
1605 goto err_unlock_gc;
1606
1607 bkey_copy_key(&c->root->key, &MAX_KEY);
Kent Overstreet57943512013-04-25 13:58:35 -07001608 bch_btree_node_write(c->root, &op.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001609
1610 bch_btree_set_root(c->root);
1611 rw_unlock(true, c->root);
1612
1613 /*
1614 * We don't want to write the first journal entry until
1615 * everything is set up - fortunately journal entries won't be
1616 * written until the SET_CACHE_SYNC() here:
1617 */
1618 SET_CACHE_SYNC(&c->sb, true);
1619
1620 bch_journal_next(&c->journal);
1621 bch_journal_meta(c, &op.cl);
1622
1623 /* Unlock */
1624 closure_set_stopped(&c->gc.cl);
1625 closure_put(&c->gc.cl);
1626 }
1627
1628 closure_sync(&op.cl);
1629 c->sb.last_mount = get_seconds();
1630 bcache_write_super(c);
1631
1632 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1633 bch_cached_dev_attach(dc, c);
1634
1635 flash_devs_run(c);
1636
1637 return;
1638err_unlock_gc:
1639 closure_set_stopped(&c->gc.cl);
1640 closure_put(&c->gc.cl);
1641err:
1642 closure_sync(&op.cl);
1643 /* XXX: test this, it's broken */
1644 bch_cache_set_error(c, err);
1645}
1646
1647static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1648{
1649 return ca->sb.block_size == c->sb.block_size &&
1650 ca->sb.bucket_size == c->sb.block_size &&
1651 ca->sb.nr_in_set == c->sb.nr_in_set;
1652}
1653
1654static const char *register_cache_set(struct cache *ca)
1655{
1656 char buf[12];
1657 const char *err = "cannot allocate memory";
1658 struct cache_set *c;
1659
1660 list_for_each_entry(c, &bch_cache_sets, list)
1661 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1662 if (c->cache[ca->sb.nr_this_dev])
1663 return "duplicate cache set member";
1664
1665 if (!can_attach_cache(ca, c))
1666 return "cache sb does not match set";
1667
1668 if (!CACHE_SYNC(&ca->sb))
1669 SET_CACHE_SYNC(&c->sb, false);
1670
1671 goto found;
1672 }
1673
1674 c = bch_cache_set_alloc(&ca->sb);
1675 if (!c)
1676 return err;
1677
1678 err = "error creating kobject";
1679 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1680 kobject_add(&c->internal, &c->kobj, "internal"))
1681 goto err;
1682
1683 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1684 goto err;
1685
1686 bch_debug_init_cache_set(c);
1687
1688 list_add(&c->list, &bch_cache_sets);
1689found:
1690 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1691 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1692 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1693 goto err;
1694
1695 if (ca->sb.seq > c->sb.seq) {
1696 c->sb.version = ca->sb.version;
1697 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1698 c->sb.flags = ca->sb.flags;
1699 c->sb.seq = ca->sb.seq;
1700 pr_debug("set version = %llu", c->sb.version);
1701 }
1702
1703 ca->set = c;
1704 ca->set->cache[ca->sb.nr_this_dev] = ca;
1705 c->cache_by_alloc[c->caches_loaded++] = ca;
1706
1707 if (c->caches_loaded == c->sb.nr_in_set)
1708 run_cache_set(c);
1709
1710 return NULL;
1711err:
1712 bch_cache_set_unregister(c);
1713 return err;
1714}
1715
1716/* Cache device */
1717
1718void bch_cache_release(struct kobject *kobj)
1719{
1720 struct cache *ca = container_of(kobj, struct cache, kobj);
1721
1722 if (ca->set)
1723 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1724
1725 bch_cache_allocator_exit(ca);
1726
1727 bio_split_pool_free(&ca->bio_split_hook);
1728
Kent Overstreetcafe5632013-03-23 16:11:31 -07001729 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1730 kfree(ca->prio_buckets);
1731 vfree(ca->buckets);
1732
1733 free_heap(&ca->heap);
1734 free_fifo(&ca->unused);
1735 free_fifo(&ca->free_inc);
1736 free_fifo(&ca->free);
1737
1738 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1739 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1740
1741 if (!IS_ERR_OR_NULL(ca->bdev)) {
1742 blk_sync_queue(bdev_get_queue(ca->bdev));
1743 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1744 }
1745
1746 kfree(ca);
1747 module_put(THIS_MODULE);
1748}
1749
1750static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1751{
1752 size_t free;
1753 struct bucket *b;
1754
Kent Overstreetcafe5632013-03-23 16:11:31 -07001755 __module_get(THIS_MODULE);
1756 kobject_init(&ca->kobj, &bch_cache_ktype);
1757
Kent Overstreetcafe5632013-03-23 16:11:31 -07001758 INIT_LIST_HEAD(&ca->discards);
1759
Kent Overstreetcafe5632013-03-23 16:11:31 -07001760 bio_init(&ca->journal.bio);
1761 ca->journal.bio.bi_max_vecs = 8;
1762 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1763
1764 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1765 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1766
1767 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1768 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1769 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1770 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -07001771 !(ca->buckets = vzalloc(sizeof(struct bucket) *
Kent Overstreetcafe5632013-03-23 16:11:31 -07001772 ca->sb.nbuckets)) ||
1773 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1774 2, GFP_KERNEL)) ||
1775 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001776 bio_split_pool_init(&ca->bio_split_hook))
Kent Overstreetf59fce82013-05-15 00:11:26 -07001777 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001778
1779 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1780
Kent Overstreetcafe5632013-03-23 16:11:31 -07001781 for_each_bucket(b, ca)
1782 atomic_set(&b->pin, 0);
1783
1784 if (bch_cache_allocator_init(ca))
1785 goto err;
1786
1787 return 0;
1788err:
1789 kobject_put(&ca->kobj);
1790 return -ENOMEM;
1791}
1792
Kent Overstreetf59fce82013-05-15 00:11:26 -07001793static void register_cache(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001794 struct block_device *bdev, struct cache *ca)
1795{
1796 char name[BDEVNAME_SIZE];
1797 const char *err = "cannot allocate memory";
1798
Kent Overstreetf59fce82013-05-15 00:11:26 -07001799 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001800 ca->bdev = bdev;
1801 ca->bdev->bd_holder = ca;
1802
Kent Overstreetf59fce82013-05-15 00:11:26 -07001803 bio_init(&ca->sb_bio);
1804 ca->sb_bio.bi_max_vecs = 1;
1805 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1806 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1807 get_page(sb_page);
1808
Kent Overstreetcafe5632013-03-23 16:11:31 -07001809 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1810 ca->discard = CACHE_DISCARD(&ca->sb);
1811
Kent Overstreetf59fce82013-05-15 00:11:26 -07001812 if (cache_alloc(sb, ca) != 0)
1813 goto err;
1814
Kent Overstreetcafe5632013-03-23 16:11:31 -07001815 err = "error creating kobject";
1816 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1817 goto err;
1818
1819 err = register_cache_set(ca);
1820 if (err)
1821 goto err;
1822
1823 pr_info("registered cache device %s", bdevname(bdev, name));
Kent Overstreetf59fce82013-05-15 00:11:26 -07001824 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001825err:
Kent Overstreetf59fce82013-05-15 00:11:26 -07001826 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001827 kobject_put(&ca->kobj);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001828}
1829
1830/* Global interfaces/init */
1831
1832static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1833 const char *, size_t);
1834
1835kobj_attribute_write(register, register_bcache);
1836kobj_attribute_write(register_quiet, register_bcache);
1837
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001838static bool bch_is_open_backing(struct block_device *bdev) {
1839 struct cache_set *c, *tc;
1840 struct cached_dev *dc, *t;
1841
1842 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1843 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1844 if (dc->bdev == bdev)
1845 return true;
1846 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1847 if (dc->bdev == bdev)
1848 return true;
1849 return false;
1850}
1851
1852static bool bch_is_open_cache(struct block_device *bdev) {
1853 struct cache_set *c, *tc;
1854 struct cache *ca;
1855 unsigned i;
1856
1857 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1858 for_each_cache(ca, c, i)
1859 if (ca->bdev == bdev)
1860 return true;
1861 return false;
1862}
1863
1864static bool bch_is_open(struct block_device *bdev) {
1865 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1866}
1867
Kent Overstreetcafe5632013-03-23 16:11:31 -07001868static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1869 const char *buffer, size_t size)
1870{
1871 ssize_t ret = size;
1872 const char *err = "cannot allocate memory";
1873 char *path = NULL;
1874 struct cache_sb *sb = NULL;
1875 struct block_device *bdev = NULL;
1876 struct page *sb_page = NULL;
1877
1878 if (!try_module_get(THIS_MODULE))
1879 return -EBUSY;
1880
1881 mutex_lock(&bch_register_lock);
1882
1883 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1884 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1885 goto err;
1886
1887 err = "failed to open device";
1888 bdev = blkdev_get_by_path(strim(path),
1889 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1890 sb);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001891 if (IS_ERR(bdev)) {
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001892 if (bdev == ERR_PTR(-EBUSY)) {
1893 bdev = lookup_bdev(strim(path));
1894 if (!IS_ERR(bdev) && bch_is_open(bdev))
1895 err = "device already registered";
1896 else
1897 err = "device busy";
1898 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001899 goto err;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001900 }
1901
1902 err = "failed to set blocksize";
1903 if (set_blocksize(bdev, 4096))
1904 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001905
1906 err = read_super(sb, bdev, &sb_page);
1907 if (err)
1908 goto err_close;
1909
Kent Overstreet29033812013-04-11 15:14:35 -07001910 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001911 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001912 if (!dc)
1913 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001914
Kent Overstreetf59fce82013-05-15 00:11:26 -07001915 register_bdev(sb, sb_page, bdev, dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001916 } else {
1917 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001918 if (!ca)
1919 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001920
Kent Overstreetf59fce82013-05-15 00:11:26 -07001921 register_cache(sb, sb_page, bdev, ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001922 }
Kent Overstreetf59fce82013-05-15 00:11:26 -07001923out:
1924 if (sb_page)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001925 put_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001926 kfree(sb);
1927 kfree(path);
1928 mutex_unlock(&bch_register_lock);
1929 module_put(THIS_MODULE);
1930 return ret;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001931
1932err_close:
1933 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1934err:
1935 if (attr != &ksysfs_register_quiet)
1936 pr_info("error opening %s: %s", path, err);
1937 ret = -EINVAL;
1938 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001939}
1940
1941static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1942{
1943 if (code == SYS_DOWN ||
1944 code == SYS_HALT ||
1945 code == SYS_POWER_OFF) {
1946 DEFINE_WAIT(wait);
1947 unsigned long start = jiffies;
1948 bool stopped = false;
1949
1950 struct cache_set *c, *tc;
1951 struct cached_dev *dc, *tdc;
1952
1953 mutex_lock(&bch_register_lock);
1954
1955 if (list_empty(&bch_cache_sets) &&
1956 list_empty(&uncached_devices))
1957 goto out;
1958
1959 pr_info("Stopping all devices:");
1960
1961 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1962 bch_cache_set_stop(c);
1963
1964 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1965 bcache_device_stop(&dc->disk);
1966
1967 /* What's a condition variable? */
1968 while (1) {
1969 long timeout = start + 2 * HZ - jiffies;
1970
1971 stopped = list_empty(&bch_cache_sets) &&
1972 list_empty(&uncached_devices);
1973
1974 if (timeout < 0 || stopped)
1975 break;
1976
1977 prepare_to_wait(&unregister_wait, &wait,
1978 TASK_UNINTERRUPTIBLE);
1979
1980 mutex_unlock(&bch_register_lock);
1981 schedule_timeout(timeout);
1982 mutex_lock(&bch_register_lock);
1983 }
1984
1985 finish_wait(&unregister_wait, &wait);
1986
1987 if (stopped)
1988 pr_info("All devices stopped");
1989 else
1990 pr_notice("Timeout waiting for devices to be closed");
1991out:
1992 mutex_unlock(&bch_register_lock);
1993 }
1994
1995 return NOTIFY_DONE;
1996}
1997
1998static struct notifier_block reboot = {
1999 .notifier_call = bcache_reboot,
2000 .priority = INT_MAX, /* before any real devices */
2001};
2002
2003static void bcache_exit(void)
2004{
2005 bch_debug_exit();
2006 bch_writeback_exit();
2007 bch_request_exit();
2008 bch_btree_exit();
2009 if (bcache_kobj)
2010 kobject_put(bcache_kobj);
2011 if (bcache_wq)
2012 destroy_workqueue(bcache_wq);
2013 unregister_blkdev(bcache_major, "bcache");
2014 unregister_reboot_notifier(&reboot);
2015}
2016
2017static int __init bcache_init(void)
2018{
2019 static const struct attribute *files[] = {
2020 &ksysfs_register.attr,
2021 &ksysfs_register_quiet.attr,
2022 NULL
2023 };
2024
2025 mutex_init(&bch_register_lock);
2026 init_waitqueue_head(&unregister_wait);
2027 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07002028 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002029
2030 bcache_major = register_blkdev(0, "bcache");
2031 if (bcache_major < 0)
2032 return bcache_major;
2033
2034 if (!(bcache_wq = create_workqueue("bcache")) ||
2035 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2036 sysfs_create_files(bcache_kobj, files) ||
2037 bch_btree_init() ||
2038 bch_request_init() ||
2039 bch_writeback_init() ||
2040 bch_debug_init(bcache_kobj))
2041 goto err;
2042
2043 return 0;
2044err:
2045 bcache_exit();
2046 return -ENOMEM;
2047}
2048
2049module_exit(bcache_exit);
2050module_init(bcache_init);