blob: dbfa1c38e85e331a3ecd9e3d1f5985158c9b1abf [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "btree.h"
11#include "debug.h"
12#include "request.h"
13
Kent Overstreetc37511b2013-04-26 15:39:55 -070014#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070015#include <linux/buffer_head.h>
16#include <linux/debugfs.h>
17#include <linux/genhd.h>
18#include <linux/module.h>
19#include <linux/random.h>
20#include <linux/reboot.h>
21#include <linux/sysfs.h>
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
25
26static const char bcache_magic[] = {
27 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
28 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
29};
30
31static const char invalid_uuid[] = {
32 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
33 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
34};
35
36/* Default is -1; we skip past it for struct cached_dev's cache mode */
37const char * const bch_cache_modes[] = {
38 "default",
39 "writethrough",
40 "writeback",
41 "writearound",
42 "none",
43 NULL
44};
45
46struct uuid_entry_v0 {
47 uint8_t uuid[16];
48 uint8_t label[32];
49 uint32_t first_reg;
50 uint32_t last_reg;
51 uint32_t invalidated;
52 uint32_t pad;
53};
54
55static struct kobject *bcache_kobj;
56struct mutex bch_register_lock;
57LIST_HEAD(bch_cache_sets);
58static LIST_HEAD(uncached_devices);
59
60static int bcache_major, bcache_minor;
61static wait_queue_head_t unregister_wait;
62struct workqueue_struct *bcache_wq;
63
64#define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
65
66static void bio_split_pool_free(struct bio_split_pool *p)
67{
Kent Overstreet8ef74792013-04-05 13:46:13 -070068 if (p->bio_split_hook)
69 mempool_destroy(p->bio_split_hook);
70
Kent Overstreetcafe5632013-03-23 16:11:31 -070071 if (p->bio_split)
72 bioset_free(p->bio_split);
Kent Overstreetcafe5632013-03-23 16:11:31 -070073}
74
75static int bio_split_pool_init(struct bio_split_pool *p)
76{
77 p->bio_split = bioset_create(4, 0);
78 if (!p->bio_split)
79 return -ENOMEM;
80
81 p->bio_split_hook = mempool_create_kmalloc_pool(4,
82 sizeof(struct bio_split_hook));
83 if (!p->bio_split_hook)
84 return -ENOMEM;
85
86 return 0;
87}
88
89/* Superblock */
90
91static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
92 struct page **res)
93{
94 const char *err;
95 struct cache_sb *s;
96 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
97 unsigned i;
98
99 if (!bh)
100 return "IO error";
101
102 s = (struct cache_sb *) bh->b_data;
103
104 sb->offset = le64_to_cpu(s->offset);
105 sb->version = le64_to_cpu(s->version);
106
107 memcpy(sb->magic, s->magic, 16);
108 memcpy(sb->uuid, s->uuid, 16);
109 memcpy(sb->set_uuid, s->set_uuid, 16);
110 memcpy(sb->label, s->label, SB_LABEL_SIZE);
111
112 sb->flags = le64_to_cpu(s->flags);
113 sb->seq = le64_to_cpu(s->seq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700114 sb->last_mount = le32_to_cpu(s->last_mount);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700115 sb->first_bucket = le16_to_cpu(s->first_bucket);
116 sb->keys = le16_to_cpu(s->keys);
117
118 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
119 sb->d[i] = le64_to_cpu(s->d[i]);
120
121 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
122 sb->version, sb->flags, sb->seq, sb->keys);
123
124 err = "Not a bcache superblock";
125 if (sb->offset != SB_SECTOR)
126 goto err;
127
128 if (memcmp(sb->magic, bcache_magic, 16))
129 goto err;
130
131 err = "Too many journal buckets";
132 if (sb->keys > SB_JOURNAL_BUCKETS)
133 goto err;
134
135 err = "Bad checksum";
136 if (s->csum != csum_set(s))
137 goto err;
138
139 err = "Bad UUID";
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600140 if (bch_is_zero(sb->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141 goto err;
142
Kent Overstreet8abb2a52013-04-23 21:51:48 -0700143 sb->block_size = le16_to_cpu(s->block_size);
144
145 err = "Superblock block size smaller than device block size";
146 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
147 goto err;
148
Kent Overstreet29033812013-04-11 15:14:35 -0700149 switch (sb->version) {
150 case BCACHE_SB_VERSION_BDEV:
Kent Overstreet29033812013-04-11 15:14:35 -0700151 sb->data_offset = BDEV_DATA_START_DEFAULT;
152 break;
153 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
Kent Overstreet29033812013-04-11 15:14:35 -0700154 sb->data_offset = le64_to_cpu(s->data_offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700155
Kent Overstreet29033812013-04-11 15:14:35 -0700156 err = "Bad data offset";
157 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700158 goto err;
159
Kent Overstreet29033812013-04-11 15:14:35 -0700160 break;
161 case BCACHE_SB_VERSION_CDEV:
162 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
163 sb->nbuckets = le64_to_cpu(s->nbuckets);
164 sb->block_size = le16_to_cpu(s->block_size);
165 sb->bucket_size = le16_to_cpu(s->bucket_size);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700166
Kent Overstreet29033812013-04-11 15:14:35 -0700167 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
168 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
169
170 err = "Too many buckets";
171 if (sb->nbuckets > LONG_MAX)
172 goto err;
173
174 err = "Not enough buckets";
175 if (sb->nbuckets < 1 << 7)
176 goto err;
177
178 err = "Bad block/bucket size";
179 if (!is_power_of_2(sb->block_size) ||
180 sb->block_size > PAGE_SECTORS ||
181 !is_power_of_2(sb->bucket_size) ||
182 sb->bucket_size < PAGE_SECTORS)
183 goto err;
184
185 err = "Invalid superblock: device too small";
186 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
187 goto err;
188
189 err = "Bad UUID";
190 if (bch_is_zero(sb->set_uuid, 16))
191 goto err;
192
193 err = "Bad cache device number in set";
194 if (!sb->nr_in_set ||
195 sb->nr_in_set <= sb->nr_this_dev ||
196 sb->nr_in_set > MAX_CACHES_PER_SET)
197 goto err;
198
199 err = "Journal buckets not sequential";
200 for (i = 0; i < sb->keys; i++)
201 if (sb->d[i] != sb->first_bucket + i)
202 goto err;
203
204 err = "Too many journal buckets";
205 if (sb->first_bucket + sb->keys > sb->nbuckets)
206 goto err;
207
208 err = "Invalid superblock: first bucket comes before end of super";
209 if (sb->first_bucket * sb->bucket_size < 16)
210 goto err;
211
212 break;
213 default:
214 err = "Unsupported superblock version";
Kent Overstreetcafe5632013-03-23 16:11:31 -0700215 goto err;
Kent Overstreet29033812013-04-11 15:14:35 -0700216 }
217
Kent Overstreetcafe5632013-03-23 16:11:31 -0700218 sb->last_mount = get_seconds();
219 err = NULL;
220
221 get_page(bh->b_page);
222 *res = bh->b_page;
223err:
224 put_bh(bh);
225 return err;
226}
227
228static void write_bdev_super_endio(struct bio *bio, int error)
229{
230 struct cached_dev *dc = bio->bi_private;
231 /* XXX: error checking */
232
233 closure_put(&dc->sb_write.cl);
234}
235
236static void __write_super(struct cache_sb *sb, struct bio *bio)
237{
238 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page);
239 unsigned i;
240
241 bio->bi_sector = SB_SECTOR;
242 bio->bi_rw = REQ_SYNC|REQ_META;
243 bio->bi_size = SB_SIZE;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600244 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700245
246 out->offset = cpu_to_le64(sb->offset);
247 out->version = cpu_to_le64(sb->version);
248
249 memcpy(out->uuid, sb->uuid, 16);
250 memcpy(out->set_uuid, sb->set_uuid, 16);
251 memcpy(out->label, sb->label, SB_LABEL_SIZE);
252
253 out->flags = cpu_to_le64(sb->flags);
254 out->seq = cpu_to_le64(sb->seq);
255
256 out->last_mount = cpu_to_le32(sb->last_mount);
257 out->first_bucket = cpu_to_le16(sb->first_bucket);
258 out->keys = cpu_to_le16(sb->keys);
259
260 for (i = 0; i < sb->keys; i++)
261 out->d[i] = cpu_to_le64(sb->d[i]);
262
263 out->csum = csum_set(out);
264
265 pr_debug("ver %llu, flags %llu, seq %llu",
266 sb->version, sb->flags, sb->seq);
267
268 submit_bio(REQ_WRITE, bio);
269}
270
271void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
272{
273 struct closure *cl = &dc->sb_write.cl;
274 struct bio *bio = &dc->sb_bio;
275
276 closure_lock(&dc->sb_write, parent);
277
278 bio_reset(bio);
279 bio->bi_bdev = dc->bdev;
280 bio->bi_end_io = write_bdev_super_endio;
281 bio->bi_private = dc;
282
283 closure_get(cl);
284 __write_super(&dc->sb, bio);
285
286 closure_return(cl);
287}
288
289static void write_super_endio(struct bio *bio, int error)
290{
291 struct cache *ca = bio->bi_private;
292
293 bch_count_io_errors(ca, error, "writing superblock");
294 closure_put(&ca->set->sb_write.cl);
295}
296
297void bcache_write_super(struct cache_set *c)
298{
299 struct closure *cl = &c->sb_write.cl;
300 struct cache *ca;
301 unsigned i;
302
303 closure_lock(&c->sb_write, &c->cl);
304
305 c->sb.seq++;
306
307 for_each_cache(ca, c, i) {
308 struct bio *bio = &ca->sb_bio;
309
Kent Overstreet29033812013-04-11 15:14:35 -0700310 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700311 ca->sb.seq = c->sb.seq;
312 ca->sb.last_mount = c->sb.last_mount;
313
314 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
315
316 bio_reset(bio);
317 bio->bi_bdev = ca->bdev;
318 bio->bi_end_io = write_super_endio;
319 bio->bi_private = ca;
320
321 closure_get(cl);
322 __write_super(&ca->sb, bio);
323 }
324
325 closure_return(cl);
326}
327
328/* UUID io */
329
330static void uuid_endio(struct bio *bio, int error)
331{
332 struct closure *cl = bio->bi_private;
333 struct cache_set *c = container_of(cl, struct cache_set, uuid_write.cl);
334
335 cache_set_err_on(error, c, "accessing uuids");
336 bch_bbio_free(bio, c);
337 closure_put(cl);
338}
339
340static void uuid_io(struct cache_set *c, unsigned long rw,
341 struct bkey *k, struct closure *parent)
342{
343 struct closure *cl = &c->uuid_write.cl;
344 struct uuid_entry *u;
345 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -0700346 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700347
348 BUG_ON(!parent);
349 closure_lock(&c->uuid_write, parent);
350
351 for (i = 0; i < KEY_PTRS(k); i++) {
352 struct bio *bio = bch_bbio_alloc(c);
353
354 bio->bi_rw = REQ_SYNC|REQ_META|rw;
355 bio->bi_size = KEY_SIZE(k) << 9;
356
357 bio->bi_end_io = uuid_endio;
358 bio->bi_private = cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600359 bch_bio_map(bio, c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700360
361 bch_submit_bbio(bio, c, k, i);
362
363 if (!(rw & WRITE))
364 break;
365 }
366
Kent Overstreet85b14922013-05-14 20:33:16 -0700367 bch_bkey_to_text(buf, sizeof(buf), k);
368 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700369
370 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600371 if (!bch_is_zero(u->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700372 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
373 u - c->uuids, u->uuid, u->label,
374 u->first_reg, u->last_reg, u->invalidated);
375
376 closure_return(cl);
377}
378
379static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
380{
381 struct bkey *k = &j->uuid_bucket;
382
383 if (__bch_ptr_invalid(c, 1, k))
384 return "bad uuid pointer";
385
386 bkey_copy(&c->uuid_bucket, k);
387 uuid_io(c, READ_SYNC, k, cl);
388
389 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
390 struct uuid_entry_v0 *u0 = (void *) c->uuids;
391 struct uuid_entry *u1 = (void *) c->uuids;
392 int i;
393
394 closure_sync(cl);
395
396 /*
397 * Since the new uuid entry is bigger than the old, we have to
398 * convert starting at the highest memory address and work down
399 * in order to do it in place
400 */
401
402 for (i = c->nr_uuids - 1;
403 i >= 0;
404 --i) {
405 memcpy(u1[i].uuid, u0[i].uuid, 16);
406 memcpy(u1[i].label, u0[i].label, 32);
407
408 u1[i].first_reg = u0[i].first_reg;
409 u1[i].last_reg = u0[i].last_reg;
410 u1[i].invalidated = u0[i].invalidated;
411
412 u1[i].flags = 0;
413 u1[i].sectors = 0;
414 }
415 }
416
417 return NULL;
418}
419
420static int __uuid_write(struct cache_set *c)
421{
422 BKEY_PADDED(key) k;
423 struct closure cl;
424 closure_init_stack(&cl);
425
426 lockdep_assert_held(&bch_register_lock);
427
428 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, &cl))
429 return 1;
430
431 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
432 uuid_io(c, REQ_WRITE, &k.key, &cl);
433 closure_sync(&cl);
434
435 bkey_copy(&c->uuid_bucket, &k.key);
436 __bkey_put(c, &k.key);
437 return 0;
438}
439
440int bch_uuid_write(struct cache_set *c)
441{
442 int ret = __uuid_write(c);
443
444 if (!ret)
445 bch_journal_meta(c, NULL);
446
447 return ret;
448}
449
450static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
451{
452 struct uuid_entry *u;
453
454 for (u = c->uuids;
455 u < c->uuids + c->nr_uuids; u++)
456 if (!memcmp(u->uuid, uuid, 16))
457 return u;
458
459 return NULL;
460}
461
462static struct uuid_entry *uuid_find_empty(struct cache_set *c)
463{
464 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
465 return uuid_find(c, zero_uuid);
466}
467
468/*
469 * Bucket priorities/gens:
470 *
471 * For each bucket, we store on disk its
472 * 8 bit gen
473 * 16 bit priority
474 *
475 * See alloc.c for an explanation of the gen. The priority is used to implement
476 * lru (and in the future other) cache replacement policies; for most purposes
477 * it's just an opaque integer.
478 *
479 * The gens and the priorities don't have a whole lot to do with each other, and
480 * it's actually the gens that must be written out at specific times - it's no
481 * big deal if the priorities don't get written, if we lose them we just reuse
482 * buckets in suboptimal order.
483 *
484 * On disk they're stored in a packed array, and in as many buckets are required
485 * to fit them all. The buckets we use to store them form a list; the journal
486 * header points to the first bucket, the first bucket points to the second
487 * bucket, et cetera.
488 *
489 * This code is used by the allocation code; periodically (whenever it runs out
490 * of buckets to allocate from) the allocation code will invalidate some
491 * buckets, but it can't use those buckets until their new gens are safely on
492 * disk.
493 */
494
495static void prio_endio(struct bio *bio, int error)
496{
497 struct cache *ca = bio->bi_private;
498
499 cache_set_err_on(error, ca->set, "accessing priorities");
500 bch_bbio_free(bio, ca->set);
501 closure_put(&ca->prio);
502}
503
504static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
505{
506 struct closure *cl = &ca->prio;
507 struct bio *bio = bch_bbio_alloc(ca->set);
508
509 closure_init_stack(cl);
510
511 bio->bi_sector = bucket * ca->sb.bucket_size;
512 bio->bi_bdev = ca->bdev;
513 bio->bi_rw = REQ_SYNC|REQ_META|rw;
514 bio->bi_size = bucket_bytes(ca);
515
516 bio->bi_end_io = prio_endio;
517 bio->bi_private = ca;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600518 bch_bio_map(bio, ca->disk_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700519
520 closure_bio_submit(bio, &ca->prio, ca);
521 closure_sync(cl);
522}
523
524#define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
525 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
526
527void bch_prio_write(struct cache *ca)
528{
529 int i;
530 struct bucket *b;
531 struct closure cl;
532
533 closure_init_stack(&cl);
534
535 lockdep_assert_held(&ca->set->bucket_lock);
536
537 for (b = ca->buckets;
538 b < ca->buckets + ca->sb.nbuckets; b++)
539 b->disk_gen = b->gen;
540
541 ca->disk_buckets->seq++;
542
543 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
544 &ca->meta_sectors_written);
545
546 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
547 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700548
549 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
550 long bucket;
551 struct prio_set *p = ca->disk_buckets;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700552 struct bucket_disk *d = p->data;
553 struct bucket_disk *end = d + prios_per_bucket(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700554
555 for (b = ca->buckets + i * prios_per_bucket(ca);
556 b < ca->buckets + ca->sb.nbuckets && d < end;
557 b++, d++) {
558 d->prio = cpu_to_le16(b->prio);
559 d->gen = b->gen;
560 }
561
562 p->next_bucket = ca->prio_buckets[i + 1];
563 p->magic = pset_magic(ca);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600564 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700565
566 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, &cl);
567 BUG_ON(bucket == -1);
568
569 mutex_unlock(&ca->set->bucket_lock);
570 prio_io(ca, bucket, REQ_WRITE);
571 mutex_lock(&ca->set->bucket_lock);
572
573 ca->prio_buckets[i] = bucket;
574 atomic_dec_bug(&ca->buckets[bucket].pin);
575 }
576
577 mutex_unlock(&ca->set->bucket_lock);
578
579 bch_journal_meta(ca->set, &cl);
580 closure_sync(&cl);
581
582 mutex_lock(&ca->set->bucket_lock);
583
584 ca->need_save_prio = 0;
585
586 /*
587 * Don't want the old priorities to get garbage collected until after we
588 * finish writing the new ones, and they're journalled
589 */
590 for (i = 0; i < prio_buckets(ca); i++)
591 ca->prio_last_buckets[i] = ca->prio_buckets[i];
592}
593
594static void prio_read(struct cache *ca, uint64_t bucket)
595{
596 struct prio_set *p = ca->disk_buckets;
597 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
598 struct bucket *b;
599 unsigned bucket_nr = 0;
600
601 for (b = ca->buckets;
602 b < ca->buckets + ca->sb.nbuckets;
603 b++, d++) {
604 if (d == end) {
605 ca->prio_buckets[bucket_nr] = bucket;
606 ca->prio_last_buckets[bucket_nr] = bucket;
607 bucket_nr++;
608
609 prio_io(ca, bucket, READ_SYNC);
610
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600611 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700612 pr_warn("bad csum reading priorities");
613
614 if (p->magic != pset_magic(ca))
615 pr_warn("bad magic reading priorities");
616
617 bucket = p->next_bucket;
618 d = p->data;
619 }
620
621 b->prio = le16_to_cpu(d->prio);
622 b->gen = b->disk_gen = b->last_gc = b->gc_gen = d->gen;
623 }
624}
625
626/* Bcache device */
627
628static int open_dev(struct block_device *b, fmode_t mode)
629{
630 struct bcache_device *d = b->bd_disk->private_data;
631 if (atomic_read(&d->closing))
632 return -ENXIO;
633
634 closure_get(&d->cl);
635 return 0;
636}
637
Emil Goode867e1162013-05-09 22:39:26 +0200638static void release_dev(struct gendisk *b, fmode_t mode)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700639{
640 struct bcache_device *d = b->private_data;
641 closure_put(&d->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700642}
643
644static int ioctl_dev(struct block_device *b, fmode_t mode,
645 unsigned int cmd, unsigned long arg)
646{
647 struct bcache_device *d = b->bd_disk->private_data;
648 return d->ioctl(d, mode, cmd, arg);
649}
650
651static const struct block_device_operations bcache_ops = {
652 .open = open_dev,
653 .release = release_dev,
654 .ioctl = ioctl_dev,
655 .owner = THIS_MODULE,
656};
657
658void bcache_device_stop(struct bcache_device *d)
659{
660 if (!atomic_xchg(&d->closing, 1))
661 closure_queue(&d->cl);
662}
663
Kent Overstreetee668502013-02-01 07:29:41 -0800664static void bcache_device_unlink(struct bcache_device *d)
665{
666 unsigned i;
667 struct cache *ca;
668
669 sysfs_remove_link(&d->c->kobj, d->name);
670 sysfs_remove_link(&d->kobj, "cache");
671
672 for_each_cache(ca, d->c, i)
673 bd_unlink_disk_holder(ca->bdev, d->disk);
674}
675
676static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
677 const char *name)
678{
679 unsigned i;
680 struct cache *ca;
681
682 for_each_cache(ca, d->c, i)
683 bd_link_disk_holder(ca->bdev, d->disk);
684
685 snprintf(d->name, BCACHEDEVNAME_SIZE,
686 "%s%u", name, d->id);
687
688 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
689 sysfs_create_link(&c->kobj, &d->kobj, d->name),
690 "Couldn't create device <-> cache set symlinks");
691}
692
Kent Overstreetcafe5632013-03-23 16:11:31 -0700693static void bcache_device_detach(struct bcache_device *d)
694{
695 lockdep_assert_held(&bch_register_lock);
696
697 if (atomic_read(&d->detaching)) {
698 struct uuid_entry *u = d->c->uuids + d->id;
699
700 SET_UUID_FLASH_ONLY(u, 0);
701 memcpy(u->uuid, invalid_uuid, 16);
702 u->invalidated = cpu_to_le32(get_seconds());
703 bch_uuid_write(d->c);
704
705 atomic_set(&d->detaching, 0);
706 }
707
Kent Overstreetee668502013-02-01 07:29:41 -0800708 bcache_device_unlink(d);
709
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710 d->c->devices[d->id] = NULL;
711 closure_put(&d->c->caching);
712 d->c = NULL;
713}
714
715static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
716 unsigned id)
717{
718 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
719
720 d->id = id;
721 d->c = c;
722 c->devices[id] = d;
723
724 closure_get(&c->caching);
725}
726
Kent Overstreetcafe5632013-03-23 16:11:31 -0700727static void bcache_device_free(struct bcache_device *d)
728{
729 lockdep_assert_held(&bch_register_lock);
730
731 pr_info("%s stopped", d->disk->disk_name);
732
733 if (d->c)
734 bcache_device_detach(d);
Kent Overstreetf59fce82013-05-15 00:11:26 -0700735 if (d->disk && d->disk->flags & GENHD_FL_UP)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700736 del_gendisk(d->disk);
737 if (d->disk && d->disk->queue)
738 blk_cleanup_queue(d->disk->queue);
739 if (d->disk)
740 put_disk(d->disk);
741
742 bio_split_pool_free(&d->bio_split_hook);
743 if (d->unaligned_bvec)
744 mempool_destroy(d->unaligned_bvec);
745 if (d->bio_split)
746 bioset_free(d->bio_split);
747
748 closure_debug_destroy(&d->cl);
749}
750
751static int bcache_device_init(struct bcache_device *d, unsigned block_size)
752{
753 struct request_queue *q;
754
755 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
756 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
757 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -0700758 bio_split_pool_init(&d->bio_split_hook) ||
759 !(d->disk = alloc_disk(1)) ||
760 !(q = blk_alloc_queue(GFP_KERNEL)))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700761 return -ENOMEM;
762
763 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
764
765 d->disk->major = bcache_major;
766 d->disk->first_minor = bcache_minor++;
767 d->disk->fops = &bcache_ops;
768 d->disk->private_data = d;
769
Kent Overstreetcafe5632013-03-23 16:11:31 -0700770 blk_queue_make_request(q, NULL);
771 d->disk->queue = q;
772 q->queuedata = d;
773 q->backing_dev_info.congested_data = d;
774 q->limits.max_hw_sectors = UINT_MAX;
775 q->limits.max_sectors = UINT_MAX;
776 q->limits.max_segment_size = UINT_MAX;
777 q->limits.max_segments = BIO_MAX_PAGES;
778 q->limits.max_discard_sectors = UINT_MAX;
779 q->limits.io_min = block_size;
780 q->limits.logical_block_size = block_size;
781 q->limits.physical_block_size = block_size;
782 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
783 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
784
785 return 0;
786}
787
788/* Cached device */
789
790static void calc_cached_dev_sectors(struct cache_set *c)
791{
792 uint64_t sectors = 0;
793 struct cached_dev *dc;
794
795 list_for_each_entry(dc, &c->cached_devs, list)
796 sectors += bdev_sectors(dc->bdev);
797
798 c->cached_dev_sectors = sectors;
799}
800
801void bch_cached_dev_run(struct cached_dev *dc)
802{
803 struct bcache_device *d = &dc->disk;
804
805 if (atomic_xchg(&dc->running, 1))
806 return;
807
808 if (!d->c &&
809 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
810 struct closure cl;
811 closure_init_stack(&cl);
812
813 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
814 bch_write_bdev_super(dc, &cl);
815 closure_sync(&cl);
816 }
817
818 add_disk(d->disk);
Kent Overstreetee668502013-02-01 07:29:41 -0800819 bd_link_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700820#if 0
821 char *env[] = { "SYMLINK=label" , NULL };
822 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
823#endif
824 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
825 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
826 pr_debug("error creating sysfs link");
827}
828
829static void cached_dev_detach_finish(struct work_struct *w)
830{
831 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
832 char buf[BDEVNAME_SIZE];
833 struct closure cl;
834 closure_init_stack(&cl);
835
836 BUG_ON(!atomic_read(&dc->disk.detaching));
837 BUG_ON(atomic_read(&dc->count));
838
Kent Overstreetcafe5632013-03-23 16:11:31 -0700839 mutex_lock(&bch_register_lock);
840
841 memset(&dc->sb.set_uuid, 0, 16);
842 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
843
844 bch_write_bdev_super(dc, &cl);
845 closure_sync(&cl);
846
847 bcache_device_detach(&dc->disk);
848 list_move(&dc->list, &uncached_devices);
849
850 mutex_unlock(&bch_register_lock);
851
852 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
853
854 /* Drop ref we took in cached_dev_detach() */
855 closure_put(&dc->disk.cl);
856}
857
858void bch_cached_dev_detach(struct cached_dev *dc)
859{
860 lockdep_assert_held(&bch_register_lock);
861
862 if (atomic_read(&dc->disk.closing))
863 return;
864
865 if (atomic_xchg(&dc->disk.detaching, 1))
866 return;
867
868 /*
869 * Block the device from being closed and freed until we're finished
870 * detaching
871 */
872 closure_get(&dc->disk.cl);
873
874 bch_writeback_queue(dc);
875 cached_dev_put(dc);
876}
877
878int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
879{
880 uint32_t rtime = cpu_to_le32(get_seconds());
881 struct uuid_entry *u;
882 char buf[BDEVNAME_SIZE];
883
884 bdevname(dc->bdev, buf);
885
886 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
887 return -ENOENT;
888
889 if (dc->disk.c) {
890 pr_err("Can't attach %s: already attached", buf);
891 return -EINVAL;
892 }
893
894 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
895 pr_err("Can't attach %s: shutting down", buf);
896 return -EINVAL;
897 }
898
899 if (dc->sb.block_size < c->sb.block_size) {
900 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700901 pr_err("Couldn't attach %s: block size less than set's block size",
902 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700903 return -EINVAL;
904 }
905
906 u = uuid_find(c, dc->sb.uuid);
907
908 if (u &&
909 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
910 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
911 memcpy(u->uuid, invalid_uuid, 16);
912 u->invalidated = cpu_to_le32(get_seconds());
913 u = NULL;
914 }
915
916 if (!u) {
917 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
918 pr_err("Couldn't find uuid for %s in set", buf);
919 return -ENOENT;
920 }
921
922 u = uuid_find_empty(c);
923 if (!u) {
924 pr_err("Not caching %s, no room for UUID", buf);
925 return -EINVAL;
926 }
927 }
928
929 /* Deadlocks since we're called via sysfs...
930 sysfs_remove_file(&dc->kobj, &sysfs_attach);
931 */
932
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600933 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700934 struct closure cl;
935 closure_init_stack(&cl);
936
937 memcpy(u->uuid, dc->sb.uuid, 16);
938 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
939 u->first_reg = u->last_reg = rtime;
940 bch_uuid_write(c);
941
942 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
943 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
944
945 bch_write_bdev_super(dc, &cl);
946 closure_sync(&cl);
947 } else {
948 u->last_reg = rtime;
949 bch_uuid_write(c);
950 }
951
952 bcache_device_attach(&dc->disk, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700953 list_move(&dc->list, &c->cached_devs);
954 calc_cached_dev_sectors(c);
955
956 smp_wmb();
957 /*
958 * dc->c must be set before dc->count != 0 - paired with the mb in
959 * cached_dev_get()
960 */
961 atomic_set(&dc->count, 1);
962
963 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700964 bch_sectors_dirty_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700965 atomic_set(&dc->has_dirty, 1);
966 atomic_inc(&dc->count);
967 bch_writeback_queue(dc);
968 }
969
970 bch_cached_dev_run(dc);
Kent Overstreetee668502013-02-01 07:29:41 -0800971 bcache_device_link(&dc->disk, c, "bdev");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700972
973 pr_info("Caching %s as %s on set %pU",
974 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
975 dc->disk.c->sb.set_uuid);
976 return 0;
977}
978
979void bch_cached_dev_release(struct kobject *kobj)
980{
981 struct cached_dev *dc = container_of(kobj, struct cached_dev,
982 disk.kobj);
983 kfree(dc);
984 module_put(THIS_MODULE);
985}
986
987static void cached_dev_free(struct closure *cl)
988{
989 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
990
991 cancel_delayed_work_sync(&dc->writeback_rate_update);
992
993 mutex_lock(&bch_register_lock);
994
Kent Overstreetf59fce82013-05-15 00:11:26 -0700995 if (atomic_read(&dc->running))
996 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700997 bcache_device_free(&dc->disk);
998 list_del(&dc->list);
999
1000 mutex_unlock(&bch_register_lock);
1001
1002 if (!IS_ERR_OR_NULL(dc->bdev)) {
Kent Overstreetf59fce82013-05-15 00:11:26 -07001003 if (dc->bdev->bd_disk)
1004 blk_sync_queue(bdev_get_queue(dc->bdev));
1005
Kent Overstreetcafe5632013-03-23 16:11:31 -07001006 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1007 }
1008
1009 wake_up(&unregister_wait);
1010
1011 kobject_put(&dc->disk.kobj);
1012}
1013
1014static void cached_dev_flush(struct closure *cl)
1015{
1016 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1017 struct bcache_device *d = &dc->disk;
1018
1019 bch_cache_accounting_destroy(&dc->accounting);
1020 kobject_del(&d->kobj);
1021
1022 continue_at(cl, cached_dev_free, system_wq);
1023}
1024
1025static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1026{
Kent Overstreetf59fce82013-05-15 00:11:26 -07001027 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028 struct io *io;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001029 struct request_queue *q = bdev_get_queue(dc->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001030
1031 __module_get(THIS_MODULE);
1032 INIT_LIST_HEAD(&dc->list);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001033 closure_init(&dc->disk.cl, NULL);
1034 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001035 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001036 INIT_WORK(&dc->detach, cached_dev_detach_finish);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001037 closure_init_unlocked(&dc->sb_write);
1038 INIT_LIST_HEAD(&dc->io_lru);
1039 spin_lock_init(&dc->io_lock);
1040 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001041
1042 dc->sequential_merge = true;
1043 dc->sequential_cutoff = 4 << 20;
1044
Kent Overstreetcafe5632013-03-23 16:11:31 -07001045 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1046 list_add(&io->lru, &dc->io_lru);
1047 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1048 }
1049
Kent Overstreetf59fce82013-05-15 00:11:26 -07001050 ret = bcache_device_init(&dc->disk, block_size);
1051 if (ret)
1052 return ret;
1053
1054 set_capacity(dc->disk.disk,
1055 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1056
1057 dc->disk.disk->queue->backing_dev_info.ra_pages =
1058 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1059 q->backing_dev_info.ra_pages);
1060
1061 bch_cached_dev_request_init(dc);
1062 bch_cached_dev_writeback_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001063 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001064}
1065
1066/* Cached device - bcache superblock */
1067
Kent Overstreetf59fce82013-05-15 00:11:26 -07001068static void register_bdev(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001069 struct block_device *bdev,
1070 struct cached_dev *dc)
1071{
1072 char name[BDEVNAME_SIZE];
1073 const char *err = "cannot allocate memory";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001074 struct cache_set *c;
1075
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001077 dc->bdev = bdev;
1078 dc->bdev->bd_holder = dc;
1079
Kent Overstreetf59fce82013-05-15 00:11:26 -07001080 bio_init(&dc->sb_bio);
1081 dc->sb_bio.bi_max_vecs = 1;
1082 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1083 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1084 get_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001085
Kent Overstreetf59fce82013-05-15 00:11:26 -07001086 if (cached_dev_init(dc, sb->block_size << 9))
1087 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001088
1089 err = "error creating kobject";
1090 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1091 "bcache"))
1092 goto err;
1093 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1094 goto err;
1095
Kent Overstreetf59fce82013-05-15 00:11:26 -07001096 pr_info("registered backing device %s", bdevname(bdev, name));
1097
Kent Overstreetcafe5632013-03-23 16:11:31 -07001098 list_add(&dc->list, &uncached_devices);
1099 list_for_each_entry(c, &bch_cache_sets, list)
1100 bch_cached_dev_attach(dc, c);
1101
1102 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1103 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1104 bch_cached_dev_run(dc);
1105
Kent Overstreetf59fce82013-05-15 00:11:26 -07001106 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001107err:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001108 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001109 bcache_device_stop(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001110}
1111
1112/* Flash only volumes */
1113
1114void bch_flash_dev_release(struct kobject *kobj)
1115{
1116 struct bcache_device *d = container_of(kobj, struct bcache_device,
1117 kobj);
1118 kfree(d);
1119}
1120
1121static void flash_dev_free(struct closure *cl)
1122{
1123 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1124 bcache_device_free(d);
1125 kobject_put(&d->kobj);
1126}
1127
1128static void flash_dev_flush(struct closure *cl)
1129{
1130 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1131
Kent Overstreetee668502013-02-01 07:29:41 -08001132 bcache_device_unlink(d);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001133 kobject_del(&d->kobj);
1134 continue_at(cl, flash_dev_free, system_wq);
1135}
1136
1137static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1138{
1139 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1140 GFP_KERNEL);
1141 if (!d)
1142 return -ENOMEM;
1143
1144 closure_init(&d->cl, NULL);
1145 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1146
1147 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1148
1149 if (bcache_device_init(d, block_bytes(c)))
1150 goto err;
1151
1152 bcache_device_attach(d, c, u - c->uuids);
1153 set_capacity(d->disk, u->sectors);
1154 bch_flash_dev_request_init(d);
1155 add_disk(d->disk);
1156
1157 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1158 goto err;
1159
1160 bcache_device_link(d, c, "volume");
1161
1162 return 0;
1163err:
1164 kobject_put(&d->kobj);
1165 return -ENOMEM;
1166}
1167
1168static int flash_devs_run(struct cache_set *c)
1169{
1170 int ret = 0;
1171 struct uuid_entry *u;
1172
1173 for (u = c->uuids;
1174 u < c->uuids + c->nr_uuids && !ret;
1175 u++)
1176 if (UUID_FLASH_ONLY(u))
1177 ret = flash_dev_run(c, u);
1178
1179 return ret;
1180}
1181
1182int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1183{
1184 struct uuid_entry *u;
1185
1186 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1187 return -EINTR;
1188
1189 u = uuid_find_empty(c);
1190 if (!u) {
1191 pr_err("Can't create volume, no room for UUID");
1192 return -EINVAL;
1193 }
1194
1195 get_random_bytes(u->uuid, 16);
1196 memset(u->label, 0, 32);
1197 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1198
1199 SET_UUID_FLASH_ONLY(u, 1);
1200 u->sectors = size >> 9;
1201
1202 bch_uuid_write(c);
1203
1204 return flash_dev_run(c, u);
1205}
1206
1207/* Cache set */
1208
1209__printf(2, 3)
1210bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1211{
1212 va_list args;
1213
1214 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1215 return false;
1216
1217 /* XXX: we can be called from atomic context
1218 acquire_console_sem();
1219 */
1220
1221 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1222
1223 va_start(args, fmt);
1224 vprintk(fmt, args);
1225 va_end(args);
1226
1227 printk(", disabling caching\n");
1228
1229 bch_cache_set_unregister(c);
1230 return true;
1231}
1232
1233void bch_cache_set_release(struct kobject *kobj)
1234{
1235 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1236 kfree(c);
1237 module_put(THIS_MODULE);
1238}
1239
1240static void cache_set_free(struct closure *cl)
1241{
1242 struct cache_set *c = container_of(cl, struct cache_set, cl);
1243 struct cache *ca;
1244 unsigned i;
1245
1246 if (!IS_ERR_OR_NULL(c->debug))
1247 debugfs_remove(c->debug);
1248
1249 bch_open_buckets_free(c);
1250 bch_btree_cache_free(c);
1251 bch_journal_free(c);
1252
1253 for_each_cache(ca, c, i)
1254 if (ca)
1255 kobject_put(&ca->kobj);
1256
1257 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1258 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1259
Kent Overstreetcafe5632013-03-23 16:11:31 -07001260 if (c->bio_split)
1261 bioset_free(c->bio_split);
Kent Overstreet57943512013-04-25 13:58:35 -07001262 if (c->fill_iter)
1263 mempool_destroy(c->fill_iter);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001264 if (c->bio_meta)
1265 mempool_destroy(c->bio_meta);
1266 if (c->search)
1267 mempool_destroy(c->search);
1268 kfree(c->devices);
1269
1270 mutex_lock(&bch_register_lock);
1271 list_del(&c->list);
1272 mutex_unlock(&bch_register_lock);
1273
1274 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1275 wake_up(&unregister_wait);
1276
1277 closure_debug_destroy(&c->cl);
1278 kobject_put(&c->kobj);
1279}
1280
1281static void cache_set_flush(struct closure *cl)
1282{
1283 struct cache_set *c = container_of(cl, struct cache_set, caching);
1284 struct btree *b;
1285
1286 /* Shut down allocator threads */
1287 set_bit(CACHE_SET_STOPPING_2, &c->flags);
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001288 wake_up_allocators(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001289
1290 bch_cache_accounting_destroy(&c->accounting);
1291
1292 kobject_put(&c->internal);
1293 kobject_del(&c->kobj);
1294
1295 if (!IS_ERR_OR_NULL(c->root))
1296 list_add(&c->root->list, &c->btree_cache);
1297
1298 /* Should skip this if we're unregistering because of an error */
1299 list_for_each_entry(b, &c->btree_cache, list)
1300 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -07001301 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001302
1303 closure_return(cl);
1304}
1305
1306static void __cache_set_unregister(struct closure *cl)
1307{
1308 struct cache_set *c = container_of(cl, struct cache_set, caching);
1309 struct cached_dev *dc, *t;
1310 size_t i;
1311
1312 mutex_lock(&bch_register_lock);
1313
1314 if (test_bit(CACHE_SET_UNREGISTERING, &c->flags))
1315 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1316 bch_cached_dev_detach(dc);
1317
1318 for (i = 0; i < c->nr_uuids; i++)
1319 if (c->devices[i] && UUID_FLASH_ONLY(&c->uuids[i]))
1320 bcache_device_stop(c->devices[i]);
1321
1322 mutex_unlock(&bch_register_lock);
1323
1324 continue_at(cl, cache_set_flush, system_wq);
1325}
1326
1327void bch_cache_set_stop(struct cache_set *c)
1328{
1329 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1330 closure_queue(&c->caching);
1331}
1332
1333void bch_cache_set_unregister(struct cache_set *c)
1334{
1335 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1336 bch_cache_set_stop(c);
1337}
1338
1339#define alloc_bucket_pages(gfp, c) \
1340 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1341
1342struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1343{
1344 int iter_size;
1345 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1346 if (!c)
1347 return NULL;
1348
1349 __module_get(THIS_MODULE);
1350 closure_init(&c->cl, NULL);
1351 set_closure_fn(&c->cl, cache_set_free, system_wq);
1352
1353 closure_init(&c->caching, &c->cl);
1354 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1355
1356 /* Maybe create continue_at_noreturn() and use it here? */
1357 closure_set_stopped(&c->cl);
1358 closure_put(&c->cl);
1359
1360 kobject_init(&c->kobj, &bch_cache_set_ktype);
1361 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1362
1363 bch_cache_accounting_init(&c->accounting, &c->cl);
1364
1365 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1366 c->sb.block_size = sb->block_size;
1367 c->sb.bucket_size = sb->bucket_size;
1368 c->sb.nr_in_set = sb->nr_in_set;
1369 c->sb.last_mount = sb->last_mount;
1370 c->bucket_bits = ilog2(sb->bucket_size);
1371 c->block_bits = ilog2(sb->block_size);
1372 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1373
1374 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1375 if (c->btree_pages > BTREE_MAX_PAGES)
1376 c->btree_pages = max_t(int, c->btree_pages / 4,
1377 BTREE_MAX_PAGES);
1378
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001379 c->sort_crit_factor = int_sqrt(c->btree_pages);
1380
Kent Overstreetcafe5632013-03-23 16:11:31 -07001381 mutex_init(&c->bucket_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001382 mutex_init(&c->sort_lock);
1383 spin_lock_init(&c->sort_time_lock);
1384 closure_init_unlocked(&c->sb_write);
1385 closure_init_unlocked(&c->uuid_write);
1386 spin_lock_init(&c->btree_read_time_lock);
1387 bch_moving_init_cache_set(c);
1388
1389 INIT_LIST_HEAD(&c->list);
1390 INIT_LIST_HEAD(&c->cached_devs);
1391 INIT_LIST_HEAD(&c->btree_cache);
1392 INIT_LIST_HEAD(&c->btree_cache_freeable);
1393 INIT_LIST_HEAD(&c->btree_cache_freed);
1394 INIT_LIST_HEAD(&c->data_buckets);
1395
1396 c->search = mempool_create_slab_pool(32, bch_search_cache);
1397 if (!c->search)
1398 goto err;
1399
1400 iter_size = (sb->bucket_size / sb->block_size + 1) *
1401 sizeof(struct btree_iter_set);
1402
1403 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1404 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1405 sizeof(struct bbio) + sizeof(struct bio_vec) *
1406 bucket_pages(c))) ||
Kent Overstreet57943512013-04-25 13:58:35 -07001407 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001408 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001409 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1410 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1411 bch_journal_alloc(c) ||
1412 bch_btree_cache_alloc(c) ||
1413 bch_open_buckets_alloc(c))
1414 goto err;
1415
Kent Overstreetcafe5632013-03-23 16:11:31 -07001416 c->congested_read_threshold_us = 2000;
1417 c->congested_write_threshold_us = 20000;
1418 c->error_limit = 8 << IO_ERROR_SHIFT;
1419
1420 return c;
1421err:
1422 bch_cache_set_unregister(c);
1423 return NULL;
1424}
1425
1426static void run_cache_set(struct cache_set *c)
1427{
1428 const char *err = "cannot allocate memory";
1429 struct cached_dev *dc, *t;
1430 struct cache *ca;
1431 unsigned i;
1432
1433 struct btree_op op;
1434 bch_btree_op_init_stack(&op);
1435 op.lock = SHRT_MAX;
1436
1437 for_each_cache(ca, c, i)
1438 c->nbuckets += ca->sb.nbuckets;
1439
1440 if (CACHE_SYNC(&c->sb)) {
1441 LIST_HEAD(journal);
1442 struct bkey *k;
1443 struct jset *j;
1444
1445 err = "cannot allocate memory for journal";
1446 if (bch_journal_read(c, &journal, &op))
1447 goto err;
1448
1449 pr_debug("btree_journal_read() done");
1450
1451 err = "no journal entries found";
1452 if (list_empty(&journal))
1453 goto err;
1454
1455 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1456
1457 err = "IO error reading priorities";
1458 for_each_cache(ca, c, i)
1459 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1460
1461 /*
1462 * If prio_read() fails it'll call cache_set_error and we'll
1463 * tear everything down right away, but if we perhaps checked
1464 * sooner we could avoid journal replay.
1465 */
1466
1467 k = &j->btree_root;
1468
1469 err = "bad btree root";
1470 if (__bch_ptr_invalid(c, j->btree_level + 1, k))
1471 goto err;
1472
1473 err = "error reading btree root";
1474 c->root = bch_btree_node_get(c, k, j->btree_level, &op);
1475 if (IS_ERR_OR_NULL(c->root))
1476 goto err;
1477
1478 list_del_init(&c->root->list);
1479 rw_unlock(true, c->root);
1480
1481 err = uuid_read(c, j, &op.cl);
1482 if (err)
1483 goto err;
1484
1485 err = "error in recovery";
1486 if (bch_btree_check(c, &op))
1487 goto err;
1488
1489 bch_journal_mark(c, &journal);
1490 bch_btree_gc_finish(c);
1491 pr_debug("btree_check() done");
1492
1493 /*
1494 * bcache_journal_next() can't happen sooner, or
1495 * btree_gc_finish() will give spurious errors about last_gc >
1496 * gc_gen - this is a hack but oh well.
1497 */
1498 bch_journal_next(&c->journal);
1499
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001500 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001501 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001502 if (bch_cache_allocator_start(ca))
1503 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001504
1505 /*
1506 * First place it's safe to allocate: btree_check() and
1507 * btree_gc_finish() have to run before we have buckets to
1508 * allocate, and bch_bucket_alloc_set() might cause a journal
1509 * entry to be written so bcache_journal_next() has to be called
1510 * first.
1511 *
1512 * If the uuids were in the old format we have to rewrite them
1513 * before the next journal entry is written:
1514 */
1515 if (j->version < BCACHE_JSET_VERSION_UUID)
1516 __uuid_write(c);
1517
1518 bch_journal_replay(c, &journal, &op);
1519 } else {
1520 pr_notice("invalidating existing data");
1521 /* Don't want invalidate_buckets() to queue a gc yet */
1522 closure_lock(&c->gc, NULL);
1523
1524 for_each_cache(ca, c, i) {
1525 unsigned j;
1526
1527 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1528 2, SB_JOURNAL_BUCKETS);
1529
1530 for (j = 0; j < ca->sb.keys; j++)
1531 ca->sb.d[j] = ca->sb.first_bucket + j;
1532 }
1533
1534 bch_btree_gc_finish(c);
1535
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001536 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001537 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001538 if (bch_cache_allocator_start(ca))
1539 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001540
1541 mutex_lock(&c->bucket_lock);
1542 for_each_cache(ca, c, i)
1543 bch_prio_write(ca);
1544 mutex_unlock(&c->bucket_lock);
1545
Kent Overstreetcafe5632013-03-23 16:11:31 -07001546 err = "cannot allocate new UUID bucket";
1547 if (__uuid_write(c))
1548 goto err_unlock_gc;
1549
1550 err = "cannot allocate new btree root";
1551 c->root = bch_btree_node_alloc(c, 0, &op.cl);
1552 if (IS_ERR_OR_NULL(c->root))
1553 goto err_unlock_gc;
1554
1555 bkey_copy_key(&c->root->key, &MAX_KEY);
Kent Overstreet57943512013-04-25 13:58:35 -07001556 bch_btree_node_write(c->root, &op.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001557
1558 bch_btree_set_root(c->root);
1559 rw_unlock(true, c->root);
1560
1561 /*
1562 * We don't want to write the first journal entry until
1563 * everything is set up - fortunately journal entries won't be
1564 * written until the SET_CACHE_SYNC() here:
1565 */
1566 SET_CACHE_SYNC(&c->sb, true);
1567
1568 bch_journal_next(&c->journal);
1569 bch_journal_meta(c, &op.cl);
1570
1571 /* Unlock */
1572 closure_set_stopped(&c->gc.cl);
1573 closure_put(&c->gc.cl);
1574 }
1575
1576 closure_sync(&op.cl);
1577 c->sb.last_mount = get_seconds();
1578 bcache_write_super(c);
1579
1580 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1581 bch_cached_dev_attach(dc, c);
1582
1583 flash_devs_run(c);
1584
1585 return;
1586err_unlock_gc:
1587 closure_set_stopped(&c->gc.cl);
1588 closure_put(&c->gc.cl);
1589err:
1590 closure_sync(&op.cl);
1591 /* XXX: test this, it's broken */
1592 bch_cache_set_error(c, err);
1593}
1594
1595static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1596{
1597 return ca->sb.block_size == c->sb.block_size &&
1598 ca->sb.bucket_size == c->sb.block_size &&
1599 ca->sb.nr_in_set == c->sb.nr_in_set;
1600}
1601
1602static const char *register_cache_set(struct cache *ca)
1603{
1604 char buf[12];
1605 const char *err = "cannot allocate memory";
1606 struct cache_set *c;
1607
1608 list_for_each_entry(c, &bch_cache_sets, list)
1609 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1610 if (c->cache[ca->sb.nr_this_dev])
1611 return "duplicate cache set member";
1612
1613 if (!can_attach_cache(ca, c))
1614 return "cache sb does not match set";
1615
1616 if (!CACHE_SYNC(&ca->sb))
1617 SET_CACHE_SYNC(&c->sb, false);
1618
1619 goto found;
1620 }
1621
1622 c = bch_cache_set_alloc(&ca->sb);
1623 if (!c)
1624 return err;
1625
1626 err = "error creating kobject";
1627 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1628 kobject_add(&c->internal, &c->kobj, "internal"))
1629 goto err;
1630
1631 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1632 goto err;
1633
1634 bch_debug_init_cache_set(c);
1635
1636 list_add(&c->list, &bch_cache_sets);
1637found:
1638 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1639 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1640 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1641 goto err;
1642
1643 if (ca->sb.seq > c->sb.seq) {
1644 c->sb.version = ca->sb.version;
1645 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1646 c->sb.flags = ca->sb.flags;
1647 c->sb.seq = ca->sb.seq;
1648 pr_debug("set version = %llu", c->sb.version);
1649 }
1650
1651 ca->set = c;
1652 ca->set->cache[ca->sb.nr_this_dev] = ca;
1653 c->cache_by_alloc[c->caches_loaded++] = ca;
1654
1655 if (c->caches_loaded == c->sb.nr_in_set)
1656 run_cache_set(c);
1657
1658 return NULL;
1659err:
1660 bch_cache_set_unregister(c);
1661 return err;
1662}
1663
1664/* Cache device */
1665
1666void bch_cache_release(struct kobject *kobj)
1667{
1668 struct cache *ca = container_of(kobj, struct cache, kobj);
1669
1670 if (ca->set)
1671 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1672
1673 bch_cache_allocator_exit(ca);
1674
1675 bio_split_pool_free(&ca->bio_split_hook);
1676
Kent Overstreetcafe5632013-03-23 16:11:31 -07001677 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1678 kfree(ca->prio_buckets);
1679 vfree(ca->buckets);
1680
1681 free_heap(&ca->heap);
1682 free_fifo(&ca->unused);
1683 free_fifo(&ca->free_inc);
1684 free_fifo(&ca->free);
1685
1686 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1687 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1688
1689 if (!IS_ERR_OR_NULL(ca->bdev)) {
1690 blk_sync_queue(bdev_get_queue(ca->bdev));
1691 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1692 }
1693
1694 kfree(ca);
1695 module_put(THIS_MODULE);
1696}
1697
1698static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1699{
1700 size_t free;
1701 struct bucket *b;
1702
Kent Overstreetcafe5632013-03-23 16:11:31 -07001703 __module_get(THIS_MODULE);
1704 kobject_init(&ca->kobj, &bch_cache_ktype);
1705
Kent Overstreetcafe5632013-03-23 16:11:31 -07001706 INIT_LIST_HEAD(&ca->discards);
1707
Kent Overstreetcafe5632013-03-23 16:11:31 -07001708 bio_init(&ca->journal.bio);
1709 ca->journal.bio.bi_max_vecs = 8;
1710 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1711
1712 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1713 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1714
1715 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1716 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1717 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1718 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -07001719 !(ca->buckets = vzalloc(sizeof(struct bucket) *
Kent Overstreetcafe5632013-03-23 16:11:31 -07001720 ca->sb.nbuckets)) ||
1721 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1722 2, GFP_KERNEL)) ||
1723 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001724 bio_split_pool_init(&ca->bio_split_hook))
Kent Overstreetf59fce82013-05-15 00:11:26 -07001725 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001726
1727 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1728
Kent Overstreetcafe5632013-03-23 16:11:31 -07001729 for_each_bucket(b, ca)
1730 atomic_set(&b->pin, 0);
1731
1732 if (bch_cache_allocator_init(ca))
1733 goto err;
1734
1735 return 0;
1736err:
1737 kobject_put(&ca->kobj);
1738 return -ENOMEM;
1739}
1740
Kent Overstreetf59fce82013-05-15 00:11:26 -07001741static void register_cache(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001742 struct block_device *bdev, struct cache *ca)
1743{
1744 char name[BDEVNAME_SIZE];
1745 const char *err = "cannot allocate memory";
1746
Kent Overstreetf59fce82013-05-15 00:11:26 -07001747 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001748 ca->bdev = bdev;
1749 ca->bdev->bd_holder = ca;
1750
Kent Overstreetf59fce82013-05-15 00:11:26 -07001751 bio_init(&ca->sb_bio);
1752 ca->sb_bio.bi_max_vecs = 1;
1753 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1754 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1755 get_page(sb_page);
1756
Kent Overstreetcafe5632013-03-23 16:11:31 -07001757 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1758 ca->discard = CACHE_DISCARD(&ca->sb);
1759
Kent Overstreetf59fce82013-05-15 00:11:26 -07001760 if (cache_alloc(sb, ca) != 0)
1761 goto err;
1762
Kent Overstreetcafe5632013-03-23 16:11:31 -07001763 err = "error creating kobject";
1764 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1765 goto err;
1766
1767 err = register_cache_set(ca);
1768 if (err)
1769 goto err;
1770
1771 pr_info("registered cache device %s", bdevname(bdev, name));
Kent Overstreetf59fce82013-05-15 00:11:26 -07001772 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001773err:
Kent Overstreetf59fce82013-05-15 00:11:26 -07001774 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001775 kobject_put(&ca->kobj);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001776}
1777
1778/* Global interfaces/init */
1779
1780static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1781 const char *, size_t);
1782
1783kobj_attribute_write(register, register_bcache);
1784kobj_attribute_write(register_quiet, register_bcache);
1785
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001786static bool bch_is_open_backing(struct block_device *bdev) {
1787 struct cache_set *c, *tc;
1788 struct cached_dev *dc, *t;
1789
1790 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1791 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1792 if (dc->bdev == bdev)
1793 return true;
1794 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1795 if (dc->bdev == bdev)
1796 return true;
1797 return false;
1798}
1799
1800static bool bch_is_open_cache(struct block_device *bdev) {
1801 struct cache_set *c, *tc;
1802 struct cache *ca;
1803 unsigned i;
1804
1805 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1806 for_each_cache(ca, c, i)
1807 if (ca->bdev == bdev)
1808 return true;
1809 return false;
1810}
1811
1812static bool bch_is_open(struct block_device *bdev) {
1813 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1814}
1815
Kent Overstreetcafe5632013-03-23 16:11:31 -07001816static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1817 const char *buffer, size_t size)
1818{
1819 ssize_t ret = size;
1820 const char *err = "cannot allocate memory";
1821 char *path = NULL;
1822 struct cache_sb *sb = NULL;
1823 struct block_device *bdev = NULL;
1824 struct page *sb_page = NULL;
1825
1826 if (!try_module_get(THIS_MODULE))
1827 return -EBUSY;
1828
1829 mutex_lock(&bch_register_lock);
1830
1831 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1832 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1833 goto err;
1834
1835 err = "failed to open device";
1836 bdev = blkdev_get_by_path(strim(path),
1837 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1838 sb);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001839 if (IS_ERR(bdev)) {
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001840 if (bdev == ERR_PTR(-EBUSY)) {
1841 bdev = lookup_bdev(strim(path));
1842 if (!IS_ERR(bdev) && bch_is_open(bdev))
1843 err = "device already registered";
1844 else
1845 err = "device busy";
1846 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001847 goto err;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001848 }
1849
1850 err = "failed to set blocksize";
1851 if (set_blocksize(bdev, 4096))
1852 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001853
1854 err = read_super(sb, bdev, &sb_page);
1855 if (err)
1856 goto err_close;
1857
Kent Overstreet29033812013-04-11 15:14:35 -07001858 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001859 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001860 if (!dc)
1861 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001862
Kent Overstreetf59fce82013-05-15 00:11:26 -07001863 register_bdev(sb, sb_page, bdev, dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001864 } else {
1865 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001866 if (!ca)
1867 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001868
Kent Overstreetf59fce82013-05-15 00:11:26 -07001869 register_cache(sb, sb_page, bdev, ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001870 }
Kent Overstreetf59fce82013-05-15 00:11:26 -07001871out:
1872 if (sb_page)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001873 put_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001874 kfree(sb);
1875 kfree(path);
1876 mutex_unlock(&bch_register_lock);
1877 module_put(THIS_MODULE);
1878 return ret;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001879
1880err_close:
1881 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1882err:
1883 if (attr != &ksysfs_register_quiet)
1884 pr_info("error opening %s: %s", path, err);
1885 ret = -EINVAL;
1886 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001887}
1888
1889static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1890{
1891 if (code == SYS_DOWN ||
1892 code == SYS_HALT ||
1893 code == SYS_POWER_OFF) {
1894 DEFINE_WAIT(wait);
1895 unsigned long start = jiffies;
1896 bool stopped = false;
1897
1898 struct cache_set *c, *tc;
1899 struct cached_dev *dc, *tdc;
1900
1901 mutex_lock(&bch_register_lock);
1902
1903 if (list_empty(&bch_cache_sets) &&
1904 list_empty(&uncached_devices))
1905 goto out;
1906
1907 pr_info("Stopping all devices:");
1908
1909 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1910 bch_cache_set_stop(c);
1911
1912 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1913 bcache_device_stop(&dc->disk);
1914
1915 /* What's a condition variable? */
1916 while (1) {
1917 long timeout = start + 2 * HZ - jiffies;
1918
1919 stopped = list_empty(&bch_cache_sets) &&
1920 list_empty(&uncached_devices);
1921
1922 if (timeout < 0 || stopped)
1923 break;
1924
1925 prepare_to_wait(&unregister_wait, &wait,
1926 TASK_UNINTERRUPTIBLE);
1927
1928 mutex_unlock(&bch_register_lock);
1929 schedule_timeout(timeout);
1930 mutex_lock(&bch_register_lock);
1931 }
1932
1933 finish_wait(&unregister_wait, &wait);
1934
1935 if (stopped)
1936 pr_info("All devices stopped");
1937 else
1938 pr_notice("Timeout waiting for devices to be closed");
1939out:
1940 mutex_unlock(&bch_register_lock);
1941 }
1942
1943 return NOTIFY_DONE;
1944}
1945
1946static struct notifier_block reboot = {
1947 .notifier_call = bcache_reboot,
1948 .priority = INT_MAX, /* before any real devices */
1949};
1950
1951static void bcache_exit(void)
1952{
1953 bch_debug_exit();
1954 bch_writeback_exit();
1955 bch_request_exit();
1956 bch_btree_exit();
1957 if (bcache_kobj)
1958 kobject_put(bcache_kobj);
1959 if (bcache_wq)
1960 destroy_workqueue(bcache_wq);
1961 unregister_blkdev(bcache_major, "bcache");
1962 unregister_reboot_notifier(&reboot);
1963}
1964
1965static int __init bcache_init(void)
1966{
1967 static const struct attribute *files[] = {
1968 &ksysfs_register.attr,
1969 &ksysfs_register_quiet.attr,
1970 NULL
1971 };
1972
1973 mutex_init(&bch_register_lock);
1974 init_waitqueue_head(&unregister_wait);
1975 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07001976 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07001977
1978 bcache_major = register_blkdev(0, "bcache");
1979 if (bcache_major < 0)
1980 return bcache_major;
1981
1982 if (!(bcache_wq = create_workqueue("bcache")) ||
1983 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
1984 sysfs_create_files(bcache_kobj, files) ||
1985 bch_btree_init() ||
1986 bch_request_init() ||
1987 bch_writeback_init() ||
1988 bch_debug_init(bcache_kobj))
1989 goto err;
1990
1991 return 0;
1992err:
1993 bcache_exit();
1994 return -ENOMEM;
1995}
1996
1997module_exit(bcache_exit);
1998module_init(bcache_init);