blob: a104dad2c7fac7ce442d2006d72ecb6424087b8a [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 Overstreetee668502013-02-01 07:29:41 -0800709 bcache_device_unlink(d);
710
Kent Overstreetcafe5632013-03-23 16:11:31 -0700711 d->c->devices[d->id] = NULL;
712 closure_put(&d->c->caching);
713 d->c = NULL;
714}
715
716static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
717 unsigned id)
718{
719 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
720
721 d->id = id;
722 d->c = c;
723 c->devices[id] = d;
724
725 closure_get(&c->caching);
726}
727
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728static void bcache_device_free(struct bcache_device *d)
729{
730 lockdep_assert_held(&bch_register_lock);
731
732 pr_info("%s stopped", d->disk->disk_name);
733
734 if (d->c)
735 bcache_device_detach(d);
Kent Overstreetf59fce82013-05-15 00:11:26 -0700736 if (d->disk && d->disk->flags & GENHD_FL_UP)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700737 del_gendisk(d->disk);
738 if (d->disk && d->disk->queue)
739 blk_cleanup_queue(d->disk->queue);
740 if (d->disk)
741 put_disk(d->disk);
742
743 bio_split_pool_free(&d->bio_split_hook);
744 if (d->unaligned_bvec)
745 mempool_destroy(d->unaligned_bvec);
746 if (d->bio_split)
747 bioset_free(d->bio_split);
Kent Overstreet279afba2013-06-05 06:21:07 -0700748 if (is_vmalloc_addr(d->stripe_sectors_dirty))
749 vfree(d->stripe_sectors_dirty);
750 else
751 kfree(d->stripe_sectors_dirty);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700752
753 closure_debug_destroy(&d->cl);
754}
755
Kent Overstreet279afba2013-06-05 06:21:07 -0700756static int bcache_device_init(struct bcache_device *d, unsigned block_size,
757 sector_t sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700758{
759 struct request_queue *q;
Kent Overstreet279afba2013-06-05 06:21:07 -0700760 size_t n;
761
762 if (!d->stripe_size_bits)
763 d->stripe_size_bits = 31;
764
765 d->nr_stripes = round_up(sectors, 1 << d->stripe_size_bits) >>
766 d->stripe_size_bits;
767
768 if (!d->nr_stripes || d->nr_stripes > SIZE_MAX / sizeof(atomic_t))
769 return -ENOMEM;
770
771 n = d->nr_stripes * sizeof(atomic_t);
772 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
773 ? kzalloc(n, GFP_KERNEL)
774 : vzalloc(n);
775 if (!d->stripe_sectors_dirty)
776 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700777
778 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
779 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
780 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -0700781 bio_split_pool_init(&d->bio_split_hook) ||
782 !(d->disk = alloc_disk(1)) ||
783 !(q = blk_alloc_queue(GFP_KERNEL)))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700784 return -ENOMEM;
785
Kent Overstreet279afba2013-06-05 06:21:07 -0700786 set_capacity(d->disk, sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700787 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
788
789 d->disk->major = bcache_major;
790 d->disk->first_minor = bcache_minor++;
791 d->disk->fops = &bcache_ops;
792 d->disk->private_data = d;
793
Kent Overstreetcafe5632013-03-23 16:11:31 -0700794 blk_queue_make_request(q, NULL);
795 d->disk->queue = q;
796 q->queuedata = d;
797 q->backing_dev_info.congested_data = d;
798 q->limits.max_hw_sectors = UINT_MAX;
799 q->limits.max_sectors = UINT_MAX;
800 q->limits.max_segment_size = UINT_MAX;
801 q->limits.max_segments = BIO_MAX_PAGES;
802 q->limits.max_discard_sectors = UINT_MAX;
803 q->limits.io_min = block_size;
804 q->limits.logical_block_size = block_size;
805 q->limits.physical_block_size = block_size;
806 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
807 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
808
809 return 0;
810}
811
812/* Cached device */
813
814static void calc_cached_dev_sectors(struct cache_set *c)
815{
816 uint64_t sectors = 0;
817 struct cached_dev *dc;
818
819 list_for_each_entry(dc, &c->cached_devs, list)
820 sectors += bdev_sectors(dc->bdev);
821
822 c->cached_dev_sectors = sectors;
823}
824
825void bch_cached_dev_run(struct cached_dev *dc)
826{
827 struct bcache_device *d = &dc->disk;
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200828 char *env[] = {
829 "DRIVER=bcache",
830 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
831 NULL
832 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700833
834 if (atomic_xchg(&dc->running, 1))
835 return;
836
837 if (!d->c &&
838 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
839 struct closure cl;
840 closure_init_stack(&cl);
841
842 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
843 bch_write_bdev_super(dc, &cl);
844 closure_sync(&cl);
845 }
846
847 add_disk(d->disk);
Kent Overstreetee668502013-02-01 07:29:41 -0800848 bd_link_disk_holder(dc->bdev, dc->disk.disk);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200849 /* won't show up in the uevent file, use udevadm monitor -e instead
850 * only class / kset properties are persistent */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700851 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200852 kfree(env[1]);
853
Kent Overstreetcafe5632013-03-23 16:11:31 -0700854 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
855 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
856 pr_debug("error creating sysfs link");
857}
858
859static void cached_dev_detach_finish(struct work_struct *w)
860{
861 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
862 char buf[BDEVNAME_SIZE];
863 struct closure cl;
864 closure_init_stack(&cl);
865
866 BUG_ON(!atomic_read(&dc->disk.detaching));
867 BUG_ON(atomic_read(&dc->count));
868
Kent Overstreetcafe5632013-03-23 16:11:31 -0700869 mutex_lock(&bch_register_lock);
870
871 memset(&dc->sb.set_uuid, 0, 16);
872 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
873
874 bch_write_bdev_super(dc, &cl);
875 closure_sync(&cl);
876
877 bcache_device_detach(&dc->disk);
878 list_move(&dc->list, &uncached_devices);
879
880 mutex_unlock(&bch_register_lock);
881
882 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
883
884 /* Drop ref we took in cached_dev_detach() */
885 closure_put(&dc->disk.cl);
886}
887
888void bch_cached_dev_detach(struct cached_dev *dc)
889{
890 lockdep_assert_held(&bch_register_lock);
891
892 if (atomic_read(&dc->disk.closing))
893 return;
894
895 if (atomic_xchg(&dc->disk.detaching, 1))
896 return;
897
898 /*
899 * Block the device from being closed and freed until we're finished
900 * detaching
901 */
902 closure_get(&dc->disk.cl);
903
904 bch_writeback_queue(dc);
905 cached_dev_put(dc);
906}
907
908int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
909{
910 uint32_t rtime = cpu_to_le32(get_seconds());
911 struct uuid_entry *u;
912 char buf[BDEVNAME_SIZE];
913
914 bdevname(dc->bdev, buf);
915
916 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
917 return -ENOENT;
918
919 if (dc->disk.c) {
920 pr_err("Can't attach %s: already attached", buf);
921 return -EINVAL;
922 }
923
924 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
925 pr_err("Can't attach %s: shutting down", buf);
926 return -EINVAL;
927 }
928
929 if (dc->sb.block_size < c->sb.block_size) {
930 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700931 pr_err("Couldn't attach %s: block size less than set's block size",
932 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700933 return -EINVAL;
934 }
935
936 u = uuid_find(c, dc->sb.uuid);
937
938 if (u &&
939 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
940 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
941 memcpy(u->uuid, invalid_uuid, 16);
942 u->invalidated = cpu_to_le32(get_seconds());
943 u = NULL;
944 }
945
946 if (!u) {
947 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
948 pr_err("Couldn't find uuid for %s in set", buf);
949 return -ENOENT;
950 }
951
952 u = uuid_find_empty(c);
953 if (!u) {
954 pr_err("Not caching %s, no room for UUID", buf);
955 return -EINVAL;
956 }
957 }
958
959 /* Deadlocks since we're called via sysfs...
960 sysfs_remove_file(&dc->kobj, &sysfs_attach);
961 */
962
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600963 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700964 struct closure cl;
965 closure_init_stack(&cl);
966
967 memcpy(u->uuid, dc->sb.uuid, 16);
968 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
969 u->first_reg = u->last_reg = rtime;
970 bch_uuid_write(c);
971
972 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
973 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
974
975 bch_write_bdev_super(dc, &cl);
976 closure_sync(&cl);
977 } else {
978 u->last_reg = rtime;
979 bch_uuid_write(c);
980 }
981
982 bcache_device_attach(&dc->disk, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700983 list_move(&dc->list, &c->cached_devs);
984 calc_cached_dev_sectors(c);
985
986 smp_wmb();
987 /*
988 * dc->c must be set before dc->count != 0 - paired with the mb in
989 * cached_dev_get()
990 */
991 atomic_set(&dc->count, 1);
992
993 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700994 bch_sectors_dirty_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700995 atomic_set(&dc->has_dirty, 1);
996 atomic_inc(&dc->count);
997 bch_writeback_queue(dc);
998 }
999
1000 bch_cached_dev_run(dc);
Kent Overstreetee668502013-02-01 07:29:41 -08001001 bcache_device_link(&dc->disk, c, "bdev");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001002
1003 pr_info("Caching %s as %s on set %pU",
1004 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
1005 dc->disk.c->sb.set_uuid);
1006 return 0;
1007}
1008
1009void bch_cached_dev_release(struct kobject *kobj)
1010{
1011 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1012 disk.kobj);
1013 kfree(dc);
1014 module_put(THIS_MODULE);
1015}
1016
1017static void cached_dev_free(struct closure *cl)
1018{
1019 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1020
1021 cancel_delayed_work_sync(&dc->writeback_rate_update);
1022
1023 mutex_lock(&bch_register_lock);
1024
Kent Overstreetf59fce82013-05-15 00:11:26 -07001025 if (atomic_read(&dc->running))
1026 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001027 bcache_device_free(&dc->disk);
1028 list_del(&dc->list);
1029
1030 mutex_unlock(&bch_register_lock);
1031
1032 if (!IS_ERR_OR_NULL(dc->bdev)) {
Kent Overstreetf59fce82013-05-15 00:11:26 -07001033 if (dc->bdev->bd_disk)
1034 blk_sync_queue(bdev_get_queue(dc->bdev));
1035
Kent Overstreetcafe5632013-03-23 16:11:31 -07001036 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1037 }
1038
1039 wake_up(&unregister_wait);
1040
1041 kobject_put(&dc->disk.kobj);
1042}
1043
1044static void cached_dev_flush(struct closure *cl)
1045{
1046 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1047 struct bcache_device *d = &dc->disk;
1048
1049 bch_cache_accounting_destroy(&dc->accounting);
1050 kobject_del(&d->kobj);
1051
1052 continue_at(cl, cached_dev_free, system_wq);
1053}
1054
1055static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1056{
Kent Overstreetf59fce82013-05-15 00:11:26 -07001057 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001058 struct io *io;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001059 struct request_queue *q = bdev_get_queue(dc->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001060
1061 __module_get(THIS_MODULE);
1062 INIT_LIST_HEAD(&dc->list);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001063 closure_init(&dc->disk.cl, NULL);
1064 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001065 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001066 INIT_WORK(&dc->detach, cached_dev_detach_finish);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001067 closure_init_unlocked(&dc->sb_write);
1068 INIT_LIST_HEAD(&dc->io_lru);
1069 spin_lock_init(&dc->io_lock);
1070 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001071
1072 dc->sequential_merge = true;
1073 dc->sequential_cutoff = 4 << 20;
1074
Kent Overstreetcafe5632013-03-23 16:11:31 -07001075 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1076 list_add(&io->lru, &dc->io_lru);
1077 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1078 }
1079
Kent Overstreet279afba2013-06-05 06:21:07 -07001080 ret = bcache_device_init(&dc->disk, block_size,
1081 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001082 if (ret)
1083 return ret;
1084
1085 set_capacity(dc->disk.disk,
1086 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1087
1088 dc->disk.disk->queue->backing_dev_info.ra_pages =
1089 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1090 q->backing_dev_info.ra_pages);
1091
1092 bch_cached_dev_request_init(dc);
1093 bch_cached_dev_writeback_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001094 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001095}
1096
1097/* Cached device - bcache superblock */
1098
Kent Overstreetf59fce82013-05-15 00:11:26 -07001099static void register_bdev(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001100 struct block_device *bdev,
1101 struct cached_dev *dc)
1102{
1103 char name[BDEVNAME_SIZE];
1104 const char *err = "cannot allocate memory";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001105 struct cache_set *c;
1106
Kent Overstreetcafe5632013-03-23 16:11:31 -07001107 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001108 dc->bdev = bdev;
1109 dc->bdev->bd_holder = dc;
1110
Kent Overstreetf59fce82013-05-15 00:11:26 -07001111 bio_init(&dc->sb_bio);
1112 dc->sb_bio.bi_max_vecs = 1;
1113 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1114 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1115 get_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001116
Kent Overstreetf59fce82013-05-15 00:11:26 -07001117 if (cached_dev_init(dc, sb->block_size << 9))
1118 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001119
1120 err = "error creating kobject";
1121 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1122 "bcache"))
1123 goto err;
1124 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1125 goto err;
1126
Kent Overstreetf59fce82013-05-15 00:11:26 -07001127 pr_info("registered backing device %s", bdevname(bdev, name));
1128
Kent Overstreetcafe5632013-03-23 16:11:31 -07001129 list_add(&dc->list, &uncached_devices);
1130 list_for_each_entry(c, &bch_cache_sets, list)
1131 bch_cached_dev_attach(dc, c);
1132
1133 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1134 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1135 bch_cached_dev_run(dc);
1136
Kent Overstreetf59fce82013-05-15 00:11:26 -07001137 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001138err:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001139 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001140 bcache_device_stop(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001141}
1142
1143/* Flash only volumes */
1144
1145void bch_flash_dev_release(struct kobject *kobj)
1146{
1147 struct bcache_device *d = container_of(kobj, struct bcache_device,
1148 kobj);
1149 kfree(d);
1150}
1151
1152static void flash_dev_free(struct closure *cl)
1153{
1154 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1155 bcache_device_free(d);
1156 kobject_put(&d->kobj);
1157}
1158
1159static void flash_dev_flush(struct closure *cl)
1160{
1161 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1162
Kent Overstreetee668502013-02-01 07:29:41 -08001163 bcache_device_unlink(d);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001164 kobject_del(&d->kobj);
1165 continue_at(cl, flash_dev_free, system_wq);
1166}
1167
1168static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1169{
1170 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1171 GFP_KERNEL);
1172 if (!d)
1173 return -ENOMEM;
1174
1175 closure_init(&d->cl, NULL);
1176 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1177
1178 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1179
Kent Overstreet279afba2013-06-05 06:21:07 -07001180 if (bcache_device_init(d, block_bytes(c), u->sectors))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001181 goto err;
1182
1183 bcache_device_attach(d, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001184 bch_flash_dev_request_init(d);
1185 add_disk(d->disk);
1186
1187 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1188 goto err;
1189
1190 bcache_device_link(d, c, "volume");
1191
1192 return 0;
1193err:
1194 kobject_put(&d->kobj);
1195 return -ENOMEM;
1196}
1197
1198static int flash_devs_run(struct cache_set *c)
1199{
1200 int ret = 0;
1201 struct uuid_entry *u;
1202
1203 for (u = c->uuids;
1204 u < c->uuids + c->nr_uuids && !ret;
1205 u++)
1206 if (UUID_FLASH_ONLY(u))
1207 ret = flash_dev_run(c, u);
1208
1209 return ret;
1210}
1211
1212int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1213{
1214 struct uuid_entry *u;
1215
1216 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1217 return -EINTR;
1218
1219 u = uuid_find_empty(c);
1220 if (!u) {
1221 pr_err("Can't create volume, no room for UUID");
1222 return -EINVAL;
1223 }
1224
1225 get_random_bytes(u->uuid, 16);
1226 memset(u->label, 0, 32);
1227 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1228
1229 SET_UUID_FLASH_ONLY(u, 1);
1230 u->sectors = size >> 9;
1231
1232 bch_uuid_write(c);
1233
1234 return flash_dev_run(c, u);
1235}
1236
1237/* Cache set */
1238
1239__printf(2, 3)
1240bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1241{
1242 va_list args;
1243
1244 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1245 return false;
1246
1247 /* XXX: we can be called from atomic context
1248 acquire_console_sem();
1249 */
1250
1251 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1252
1253 va_start(args, fmt);
1254 vprintk(fmt, args);
1255 va_end(args);
1256
1257 printk(", disabling caching\n");
1258
1259 bch_cache_set_unregister(c);
1260 return true;
1261}
1262
1263void bch_cache_set_release(struct kobject *kobj)
1264{
1265 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1266 kfree(c);
1267 module_put(THIS_MODULE);
1268}
1269
1270static void cache_set_free(struct closure *cl)
1271{
1272 struct cache_set *c = container_of(cl, struct cache_set, cl);
1273 struct cache *ca;
1274 unsigned i;
1275
1276 if (!IS_ERR_OR_NULL(c->debug))
1277 debugfs_remove(c->debug);
1278
1279 bch_open_buckets_free(c);
1280 bch_btree_cache_free(c);
1281 bch_journal_free(c);
1282
1283 for_each_cache(ca, c, i)
1284 if (ca)
1285 kobject_put(&ca->kobj);
1286
1287 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1288 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1289
Kent Overstreetcafe5632013-03-23 16:11:31 -07001290 if (c->bio_split)
1291 bioset_free(c->bio_split);
Kent Overstreet57943512013-04-25 13:58:35 -07001292 if (c->fill_iter)
1293 mempool_destroy(c->fill_iter);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001294 if (c->bio_meta)
1295 mempool_destroy(c->bio_meta);
1296 if (c->search)
1297 mempool_destroy(c->search);
1298 kfree(c->devices);
1299
1300 mutex_lock(&bch_register_lock);
1301 list_del(&c->list);
1302 mutex_unlock(&bch_register_lock);
1303
1304 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1305 wake_up(&unregister_wait);
1306
1307 closure_debug_destroy(&c->cl);
1308 kobject_put(&c->kobj);
1309}
1310
1311static void cache_set_flush(struct closure *cl)
1312{
1313 struct cache_set *c = container_of(cl, struct cache_set, caching);
1314 struct btree *b;
1315
1316 /* Shut down allocator threads */
1317 set_bit(CACHE_SET_STOPPING_2, &c->flags);
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001318 wake_up_allocators(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001319
1320 bch_cache_accounting_destroy(&c->accounting);
1321
1322 kobject_put(&c->internal);
1323 kobject_del(&c->kobj);
1324
1325 if (!IS_ERR_OR_NULL(c->root))
1326 list_add(&c->root->list, &c->btree_cache);
1327
1328 /* Should skip this if we're unregistering because of an error */
1329 list_for_each_entry(b, &c->btree_cache, list)
1330 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -07001331 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001332
1333 closure_return(cl);
1334}
1335
1336static void __cache_set_unregister(struct closure *cl)
1337{
1338 struct cache_set *c = container_of(cl, struct cache_set, caching);
1339 struct cached_dev *dc, *t;
1340 size_t i;
1341
1342 mutex_lock(&bch_register_lock);
1343
1344 if (test_bit(CACHE_SET_UNREGISTERING, &c->flags))
1345 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1346 bch_cached_dev_detach(dc);
1347
1348 for (i = 0; i < c->nr_uuids; i++)
1349 if (c->devices[i] && UUID_FLASH_ONLY(&c->uuids[i]))
1350 bcache_device_stop(c->devices[i]);
1351
1352 mutex_unlock(&bch_register_lock);
1353
1354 continue_at(cl, cache_set_flush, system_wq);
1355}
1356
1357void bch_cache_set_stop(struct cache_set *c)
1358{
1359 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1360 closure_queue(&c->caching);
1361}
1362
1363void bch_cache_set_unregister(struct cache_set *c)
1364{
1365 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1366 bch_cache_set_stop(c);
1367}
1368
1369#define alloc_bucket_pages(gfp, c) \
1370 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1371
1372struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1373{
1374 int iter_size;
1375 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1376 if (!c)
1377 return NULL;
1378
1379 __module_get(THIS_MODULE);
1380 closure_init(&c->cl, NULL);
1381 set_closure_fn(&c->cl, cache_set_free, system_wq);
1382
1383 closure_init(&c->caching, &c->cl);
1384 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1385
1386 /* Maybe create continue_at_noreturn() and use it here? */
1387 closure_set_stopped(&c->cl);
1388 closure_put(&c->cl);
1389
1390 kobject_init(&c->kobj, &bch_cache_set_ktype);
1391 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1392
1393 bch_cache_accounting_init(&c->accounting, &c->cl);
1394
1395 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1396 c->sb.block_size = sb->block_size;
1397 c->sb.bucket_size = sb->bucket_size;
1398 c->sb.nr_in_set = sb->nr_in_set;
1399 c->sb.last_mount = sb->last_mount;
1400 c->bucket_bits = ilog2(sb->bucket_size);
1401 c->block_bits = ilog2(sb->block_size);
1402 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1403
1404 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1405 if (c->btree_pages > BTREE_MAX_PAGES)
1406 c->btree_pages = max_t(int, c->btree_pages / 4,
1407 BTREE_MAX_PAGES);
1408
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001409 c->sort_crit_factor = int_sqrt(c->btree_pages);
1410
Kent Overstreetcafe5632013-03-23 16:11:31 -07001411 mutex_init(&c->bucket_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001412 mutex_init(&c->sort_lock);
1413 spin_lock_init(&c->sort_time_lock);
1414 closure_init_unlocked(&c->sb_write);
1415 closure_init_unlocked(&c->uuid_write);
1416 spin_lock_init(&c->btree_read_time_lock);
1417 bch_moving_init_cache_set(c);
1418
1419 INIT_LIST_HEAD(&c->list);
1420 INIT_LIST_HEAD(&c->cached_devs);
1421 INIT_LIST_HEAD(&c->btree_cache);
1422 INIT_LIST_HEAD(&c->btree_cache_freeable);
1423 INIT_LIST_HEAD(&c->btree_cache_freed);
1424 INIT_LIST_HEAD(&c->data_buckets);
1425
1426 c->search = mempool_create_slab_pool(32, bch_search_cache);
1427 if (!c->search)
1428 goto err;
1429
1430 iter_size = (sb->bucket_size / sb->block_size + 1) *
1431 sizeof(struct btree_iter_set);
1432
1433 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1434 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1435 sizeof(struct bbio) + sizeof(struct bio_vec) *
1436 bucket_pages(c))) ||
Kent Overstreet57943512013-04-25 13:58:35 -07001437 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001438 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001439 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1440 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1441 bch_journal_alloc(c) ||
1442 bch_btree_cache_alloc(c) ||
1443 bch_open_buckets_alloc(c))
1444 goto err;
1445
Kent Overstreetcafe5632013-03-23 16:11:31 -07001446 c->congested_read_threshold_us = 2000;
1447 c->congested_write_threshold_us = 20000;
1448 c->error_limit = 8 << IO_ERROR_SHIFT;
1449
1450 return c;
1451err:
1452 bch_cache_set_unregister(c);
1453 return NULL;
1454}
1455
1456static void run_cache_set(struct cache_set *c)
1457{
1458 const char *err = "cannot allocate memory";
1459 struct cached_dev *dc, *t;
1460 struct cache *ca;
1461 unsigned i;
1462
1463 struct btree_op op;
1464 bch_btree_op_init_stack(&op);
1465 op.lock = SHRT_MAX;
1466
1467 for_each_cache(ca, c, i)
1468 c->nbuckets += ca->sb.nbuckets;
1469
1470 if (CACHE_SYNC(&c->sb)) {
1471 LIST_HEAD(journal);
1472 struct bkey *k;
1473 struct jset *j;
1474
1475 err = "cannot allocate memory for journal";
1476 if (bch_journal_read(c, &journal, &op))
1477 goto err;
1478
1479 pr_debug("btree_journal_read() done");
1480
1481 err = "no journal entries found";
1482 if (list_empty(&journal))
1483 goto err;
1484
1485 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1486
1487 err = "IO error reading priorities";
1488 for_each_cache(ca, c, i)
1489 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1490
1491 /*
1492 * If prio_read() fails it'll call cache_set_error and we'll
1493 * tear everything down right away, but if we perhaps checked
1494 * sooner we could avoid journal replay.
1495 */
1496
1497 k = &j->btree_root;
1498
1499 err = "bad btree root";
1500 if (__bch_ptr_invalid(c, j->btree_level + 1, k))
1501 goto err;
1502
1503 err = "error reading btree root";
1504 c->root = bch_btree_node_get(c, k, j->btree_level, &op);
1505 if (IS_ERR_OR_NULL(c->root))
1506 goto err;
1507
1508 list_del_init(&c->root->list);
1509 rw_unlock(true, c->root);
1510
1511 err = uuid_read(c, j, &op.cl);
1512 if (err)
1513 goto err;
1514
1515 err = "error in recovery";
1516 if (bch_btree_check(c, &op))
1517 goto err;
1518
1519 bch_journal_mark(c, &journal);
1520 bch_btree_gc_finish(c);
1521 pr_debug("btree_check() done");
1522
1523 /*
1524 * bcache_journal_next() can't happen sooner, or
1525 * btree_gc_finish() will give spurious errors about last_gc >
1526 * gc_gen - this is a hack but oh well.
1527 */
1528 bch_journal_next(&c->journal);
1529
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001530 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001531 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001532 if (bch_cache_allocator_start(ca))
1533 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001534
1535 /*
1536 * First place it's safe to allocate: btree_check() and
1537 * btree_gc_finish() have to run before we have buckets to
1538 * allocate, and bch_bucket_alloc_set() might cause a journal
1539 * entry to be written so bcache_journal_next() has to be called
1540 * first.
1541 *
1542 * If the uuids were in the old format we have to rewrite them
1543 * before the next journal entry is written:
1544 */
1545 if (j->version < BCACHE_JSET_VERSION_UUID)
1546 __uuid_write(c);
1547
1548 bch_journal_replay(c, &journal, &op);
1549 } else {
1550 pr_notice("invalidating existing data");
1551 /* Don't want invalidate_buckets() to queue a gc yet */
1552 closure_lock(&c->gc, NULL);
1553
1554 for_each_cache(ca, c, i) {
1555 unsigned j;
1556
1557 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1558 2, SB_JOURNAL_BUCKETS);
1559
1560 for (j = 0; j < ca->sb.keys; j++)
1561 ca->sb.d[j] = ca->sb.first_bucket + j;
1562 }
1563
1564 bch_btree_gc_finish(c);
1565
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001566 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001567 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001568 if (bch_cache_allocator_start(ca))
1569 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001570
1571 mutex_lock(&c->bucket_lock);
1572 for_each_cache(ca, c, i)
1573 bch_prio_write(ca);
1574 mutex_unlock(&c->bucket_lock);
1575
Kent Overstreetcafe5632013-03-23 16:11:31 -07001576 err = "cannot allocate new UUID bucket";
1577 if (__uuid_write(c))
1578 goto err_unlock_gc;
1579
1580 err = "cannot allocate new btree root";
1581 c->root = bch_btree_node_alloc(c, 0, &op.cl);
1582 if (IS_ERR_OR_NULL(c->root))
1583 goto err_unlock_gc;
1584
1585 bkey_copy_key(&c->root->key, &MAX_KEY);
Kent Overstreet57943512013-04-25 13:58:35 -07001586 bch_btree_node_write(c->root, &op.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001587
1588 bch_btree_set_root(c->root);
1589 rw_unlock(true, c->root);
1590
1591 /*
1592 * We don't want to write the first journal entry until
1593 * everything is set up - fortunately journal entries won't be
1594 * written until the SET_CACHE_SYNC() here:
1595 */
1596 SET_CACHE_SYNC(&c->sb, true);
1597
1598 bch_journal_next(&c->journal);
1599 bch_journal_meta(c, &op.cl);
1600
1601 /* Unlock */
1602 closure_set_stopped(&c->gc.cl);
1603 closure_put(&c->gc.cl);
1604 }
1605
1606 closure_sync(&op.cl);
1607 c->sb.last_mount = get_seconds();
1608 bcache_write_super(c);
1609
1610 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1611 bch_cached_dev_attach(dc, c);
1612
1613 flash_devs_run(c);
1614
1615 return;
1616err_unlock_gc:
1617 closure_set_stopped(&c->gc.cl);
1618 closure_put(&c->gc.cl);
1619err:
1620 closure_sync(&op.cl);
1621 /* XXX: test this, it's broken */
1622 bch_cache_set_error(c, err);
1623}
1624
1625static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1626{
1627 return ca->sb.block_size == c->sb.block_size &&
1628 ca->sb.bucket_size == c->sb.block_size &&
1629 ca->sb.nr_in_set == c->sb.nr_in_set;
1630}
1631
1632static const char *register_cache_set(struct cache *ca)
1633{
1634 char buf[12];
1635 const char *err = "cannot allocate memory";
1636 struct cache_set *c;
1637
1638 list_for_each_entry(c, &bch_cache_sets, list)
1639 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1640 if (c->cache[ca->sb.nr_this_dev])
1641 return "duplicate cache set member";
1642
1643 if (!can_attach_cache(ca, c))
1644 return "cache sb does not match set";
1645
1646 if (!CACHE_SYNC(&ca->sb))
1647 SET_CACHE_SYNC(&c->sb, false);
1648
1649 goto found;
1650 }
1651
1652 c = bch_cache_set_alloc(&ca->sb);
1653 if (!c)
1654 return err;
1655
1656 err = "error creating kobject";
1657 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1658 kobject_add(&c->internal, &c->kobj, "internal"))
1659 goto err;
1660
1661 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1662 goto err;
1663
1664 bch_debug_init_cache_set(c);
1665
1666 list_add(&c->list, &bch_cache_sets);
1667found:
1668 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1669 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1670 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1671 goto err;
1672
1673 if (ca->sb.seq > c->sb.seq) {
1674 c->sb.version = ca->sb.version;
1675 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1676 c->sb.flags = ca->sb.flags;
1677 c->sb.seq = ca->sb.seq;
1678 pr_debug("set version = %llu", c->sb.version);
1679 }
1680
1681 ca->set = c;
1682 ca->set->cache[ca->sb.nr_this_dev] = ca;
1683 c->cache_by_alloc[c->caches_loaded++] = ca;
1684
1685 if (c->caches_loaded == c->sb.nr_in_set)
1686 run_cache_set(c);
1687
1688 return NULL;
1689err:
1690 bch_cache_set_unregister(c);
1691 return err;
1692}
1693
1694/* Cache device */
1695
1696void bch_cache_release(struct kobject *kobj)
1697{
1698 struct cache *ca = container_of(kobj, struct cache, kobj);
1699
1700 if (ca->set)
1701 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1702
1703 bch_cache_allocator_exit(ca);
1704
1705 bio_split_pool_free(&ca->bio_split_hook);
1706
Kent Overstreetcafe5632013-03-23 16:11:31 -07001707 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1708 kfree(ca->prio_buckets);
1709 vfree(ca->buckets);
1710
1711 free_heap(&ca->heap);
1712 free_fifo(&ca->unused);
1713 free_fifo(&ca->free_inc);
1714 free_fifo(&ca->free);
1715
1716 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1717 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1718
1719 if (!IS_ERR_OR_NULL(ca->bdev)) {
1720 blk_sync_queue(bdev_get_queue(ca->bdev));
1721 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1722 }
1723
1724 kfree(ca);
1725 module_put(THIS_MODULE);
1726}
1727
1728static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1729{
1730 size_t free;
1731 struct bucket *b;
1732
Kent Overstreetcafe5632013-03-23 16:11:31 -07001733 __module_get(THIS_MODULE);
1734 kobject_init(&ca->kobj, &bch_cache_ktype);
1735
Kent Overstreetcafe5632013-03-23 16:11:31 -07001736 INIT_LIST_HEAD(&ca->discards);
1737
Kent Overstreetcafe5632013-03-23 16:11:31 -07001738 bio_init(&ca->journal.bio);
1739 ca->journal.bio.bi_max_vecs = 8;
1740 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1741
1742 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1743 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1744
1745 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1746 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1747 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1748 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -07001749 !(ca->buckets = vzalloc(sizeof(struct bucket) *
Kent Overstreetcafe5632013-03-23 16:11:31 -07001750 ca->sb.nbuckets)) ||
1751 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1752 2, GFP_KERNEL)) ||
1753 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001754 bio_split_pool_init(&ca->bio_split_hook))
Kent Overstreetf59fce82013-05-15 00:11:26 -07001755 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001756
1757 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1758
Kent Overstreetcafe5632013-03-23 16:11:31 -07001759 for_each_bucket(b, ca)
1760 atomic_set(&b->pin, 0);
1761
1762 if (bch_cache_allocator_init(ca))
1763 goto err;
1764
1765 return 0;
1766err:
1767 kobject_put(&ca->kobj);
1768 return -ENOMEM;
1769}
1770
Kent Overstreetf59fce82013-05-15 00:11:26 -07001771static void register_cache(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001772 struct block_device *bdev, struct cache *ca)
1773{
1774 char name[BDEVNAME_SIZE];
1775 const char *err = "cannot allocate memory";
1776
Kent Overstreetf59fce82013-05-15 00:11:26 -07001777 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001778 ca->bdev = bdev;
1779 ca->bdev->bd_holder = ca;
1780
Kent Overstreetf59fce82013-05-15 00:11:26 -07001781 bio_init(&ca->sb_bio);
1782 ca->sb_bio.bi_max_vecs = 1;
1783 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1784 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1785 get_page(sb_page);
1786
Kent Overstreetcafe5632013-03-23 16:11:31 -07001787 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1788 ca->discard = CACHE_DISCARD(&ca->sb);
1789
Kent Overstreetf59fce82013-05-15 00:11:26 -07001790 if (cache_alloc(sb, ca) != 0)
1791 goto err;
1792
Kent Overstreetcafe5632013-03-23 16:11:31 -07001793 err = "error creating kobject";
1794 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1795 goto err;
1796
1797 err = register_cache_set(ca);
1798 if (err)
1799 goto err;
1800
1801 pr_info("registered cache device %s", bdevname(bdev, name));
Kent Overstreetf59fce82013-05-15 00:11:26 -07001802 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001803err:
Kent Overstreetf59fce82013-05-15 00:11:26 -07001804 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001805 kobject_put(&ca->kobj);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001806}
1807
1808/* Global interfaces/init */
1809
1810static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1811 const char *, size_t);
1812
1813kobj_attribute_write(register, register_bcache);
1814kobj_attribute_write(register_quiet, register_bcache);
1815
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001816static bool bch_is_open_backing(struct block_device *bdev) {
1817 struct cache_set *c, *tc;
1818 struct cached_dev *dc, *t;
1819
1820 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1821 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1822 if (dc->bdev == bdev)
1823 return true;
1824 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1825 if (dc->bdev == bdev)
1826 return true;
1827 return false;
1828}
1829
1830static bool bch_is_open_cache(struct block_device *bdev) {
1831 struct cache_set *c, *tc;
1832 struct cache *ca;
1833 unsigned i;
1834
1835 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1836 for_each_cache(ca, c, i)
1837 if (ca->bdev == bdev)
1838 return true;
1839 return false;
1840}
1841
1842static bool bch_is_open(struct block_device *bdev) {
1843 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1844}
1845
Kent Overstreetcafe5632013-03-23 16:11:31 -07001846static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1847 const char *buffer, size_t size)
1848{
1849 ssize_t ret = size;
1850 const char *err = "cannot allocate memory";
1851 char *path = NULL;
1852 struct cache_sb *sb = NULL;
1853 struct block_device *bdev = NULL;
1854 struct page *sb_page = NULL;
1855
1856 if (!try_module_get(THIS_MODULE))
1857 return -EBUSY;
1858
1859 mutex_lock(&bch_register_lock);
1860
1861 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1862 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1863 goto err;
1864
1865 err = "failed to open device";
1866 bdev = blkdev_get_by_path(strim(path),
1867 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1868 sb);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001869 if (IS_ERR(bdev)) {
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001870 if (bdev == ERR_PTR(-EBUSY)) {
1871 bdev = lookup_bdev(strim(path));
1872 if (!IS_ERR(bdev) && bch_is_open(bdev))
1873 err = "device already registered";
1874 else
1875 err = "device busy";
1876 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001877 goto err;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001878 }
1879
1880 err = "failed to set blocksize";
1881 if (set_blocksize(bdev, 4096))
1882 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001883
1884 err = read_super(sb, bdev, &sb_page);
1885 if (err)
1886 goto err_close;
1887
Kent Overstreet29033812013-04-11 15:14:35 -07001888 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001889 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001890 if (!dc)
1891 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001892
Kent Overstreetf59fce82013-05-15 00:11:26 -07001893 register_bdev(sb, sb_page, bdev, dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001894 } else {
1895 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001896 if (!ca)
1897 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001898
Kent Overstreetf59fce82013-05-15 00:11:26 -07001899 register_cache(sb, sb_page, bdev, ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001900 }
Kent Overstreetf59fce82013-05-15 00:11:26 -07001901out:
1902 if (sb_page)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001903 put_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001904 kfree(sb);
1905 kfree(path);
1906 mutex_unlock(&bch_register_lock);
1907 module_put(THIS_MODULE);
1908 return ret;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001909
1910err_close:
1911 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1912err:
1913 if (attr != &ksysfs_register_quiet)
1914 pr_info("error opening %s: %s", path, err);
1915 ret = -EINVAL;
1916 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001917}
1918
1919static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1920{
1921 if (code == SYS_DOWN ||
1922 code == SYS_HALT ||
1923 code == SYS_POWER_OFF) {
1924 DEFINE_WAIT(wait);
1925 unsigned long start = jiffies;
1926 bool stopped = false;
1927
1928 struct cache_set *c, *tc;
1929 struct cached_dev *dc, *tdc;
1930
1931 mutex_lock(&bch_register_lock);
1932
1933 if (list_empty(&bch_cache_sets) &&
1934 list_empty(&uncached_devices))
1935 goto out;
1936
1937 pr_info("Stopping all devices:");
1938
1939 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1940 bch_cache_set_stop(c);
1941
1942 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1943 bcache_device_stop(&dc->disk);
1944
1945 /* What's a condition variable? */
1946 while (1) {
1947 long timeout = start + 2 * HZ - jiffies;
1948
1949 stopped = list_empty(&bch_cache_sets) &&
1950 list_empty(&uncached_devices);
1951
1952 if (timeout < 0 || stopped)
1953 break;
1954
1955 prepare_to_wait(&unregister_wait, &wait,
1956 TASK_UNINTERRUPTIBLE);
1957
1958 mutex_unlock(&bch_register_lock);
1959 schedule_timeout(timeout);
1960 mutex_lock(&bch_register_lock);
1961 }
1962
1963 finish_wait(&unregister_wait, &wait);
1964
1965 if (stopped)
1966 pr_info("All devices stopped");
1967 else
1968 pr_notice("Timeout waiting for devices to be closed");
1969out:
1970 mutex_unlock(&bch_register_lock);
1971 }
1972
1973 return NOTIFY_DONE;
1974}
1975
1976static struct notifier_block reboot = {
1977 .notifier_call = bcache_reboot,
1978 .priority = INT_MAX, /* before any real devices */
1979};
1980
1981static void bcache_exit(void)
1982{
1983 bch_debug_exit();
1984 bch_writeback_exit();
1985 bch_request_exit();
1986 bch_btree_exit();
1987 if (bcache_kobj)
1988 kobject_put(bcache_kobj);
1989 if (bcache_wq)
1990 destroy_workqueue(bcache_wq);
1991 unregister_blkdev(bcache_major, "bcache");
1992 unregister_reboot_notifier(&reboot);
1993}
1994
1995static int __init bcache_init(void)
1996{
1997 static const struct attribute *files[] = {
1998 &ksysfs_register.attr,
1999 &ksysfs_register_quiet.attr,
2000 NULL
2001 };
2002
2003 mutex_init(&bch_register_lock);
2004 init_waitqueue_head(&unregister_wait);
2005 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07002006 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002007
2008 bcache_major = register_blkdev(0, "bcache");
2009 if (bcache_major < 0)
2010 return bcache_major;
2011
2012 if (!(bcache_wq = create_workqueue("bcache")) ||
2013 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2014 sysfs_create_files(bcache_kobj, files) ||
2015 bch_btree_init() ||
2016 bch_request_init() ||
2017 bch_writeback_init() ||
2018 bch_debug_init(bcache_kobj))
2019 goto err;
2020
2021 return 0;
2022err:
2023 bcache_exit();
2024 return -ENOMEM;
2025}
2026
2027module_exit(bcache_exit);
2028module_init(bcache_init);