bcache: Rip out pkey()/pbtree()
[deliverable/linux.git] / drivers / md / bcache / super.c
CommitLineData
cafe5635
KO
1/*
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
c37511b8 14#include <linux/blkdev.h>
cafe5635
KO
15#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{
8ef74790
KO
68 if (p->bio_split_hook)
69 mempool_destroy(p->bio_split_hook);
70
cafe5635
KO
71 if (p->bio_split)
72 bioset_free(p->bio_split);
cafe5635
KO
73}
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);
cafe5635 114 sb->last_mount = le32_to_cpu(s->last_mount);
cafe5635
KO
115 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";
169ef1cf 140 if (bch_is_zero(sb->uuid, 16))
cafe5635
KO
141 goto err;
142
8abb2a5d
KO
143 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
2903381f
KO
149 switch (sb->version) {
150 case BCACHE_SB_VERSION_BDEV:
2903381f
KO
151 sb->data_offset = BDEV_DATA_START_DEFAULT;
152 break;
153 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
2903381f
KO
154 sb->data_offset = le64_to_cpu(s->data_offset);
155
156 err = "Bad data offset";
157 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
158 goto err;
cafe5635 159
2903381f
KO
160 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);
cafe5635 166
2903381f
KO
167 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
168 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
cafe5635 169
2903381f
KO
170 err = "Too many buckets";
171 if (sb->nbuckets > LONG_MAX)
172 goto err;
cafe5635 173
2903381f
KO
174 err = "Not enough buckets";
175 if (sb->nbuckets < 1 << 7)
176 goto err;
cafe5635 177
2903381f
KO
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;
cafe5635 184
2903381f
KO
185 err = "Invalid superblock: device too small";
186 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
187 goto err;
cafe5635 188
2903381f
KO
189 err = "Bad UUID";
190 if (bch_is_zero(sb->set_uuid, 16))
191 goto err;
cafe5635 192
2903381f
KO
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)
cafe5635
KO
197 goto err;
198
2903381f
KO
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;
cafe5635 203
2903381f
KO
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";
cafe5635 215 goto err;
2903381f
KO
216 }
217
cafe5635
KO
218 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;
169ef1cf 244 bch_bio_map(bio, NULL);
cafe5635
KO
245
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
2903381f 310 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
cafe5635
KO
311 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;
85b1492e 346 char buf[80];
cafe5635
KO
347
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;
169ef1cf 359 bch_bio_map(bio, c->uuids);
cafe5635
KO
360
361 bch_submit_bbio(bio, c, k, i);
362
363 if (!(rw & WRITE))
364 break;
365 }
366
85b1492e
KO
367 bch_bkey_to_text(buf, sizeof(buf), k);
368 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
cafe5635
KO
369
370 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
169ef1cf 371 if (!bch_is_zero(u->uuid, 16))
cafe5635
KO
372 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;
169ef1cf 518 bch_bio_map(bio, ca->disk_buckets);
cafe5635
KO
519
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));
cafe5635
KO
548
549 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
550 long bucket;
551 struct prio_set *p = ca->disk_buckets;
b1a67b0f
KO
552 struct bucket_disk *d = p->data;
553 struct bucket_disk *end = d + prios_per_bucket(ca);
cafe5635
KO
554
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);
169ef1cf 564 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
cafe5635
KO
565
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
169ef1cf 611 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
cafe5635
KO
612 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
867e1162 638static void release_dev(struct gendisk *b, fmode_t mode)
cafe5635
KO
639{
640 struct bcache_device *d = b->private_data;
641 closure_put(&d->cl);
cafe5635
KO
642}
643
644static int ioctl_dev(struct block_device *b, fmode_t mode,
645 unsigned int cmd, unsigned long arg)
646{
647 struct bcache_device *d = b->bd_disk->private_data;
648 return d->ioctl(d, mode, cmd, arg);
649}
650
651static const struct block_device_operations bcache_ops = {
652 .open = open_dev,
653 .release = release_dev,
654 .ioctl = ioctl_dev,
655 .owner = THIS_MODULE,
656};
657
658void bcache_device_stop(struct bcache_device *d)
659{
660 if (!atomic_xchg(&d->closing, 1))
661 closure_queue(&d->cl);
662}
663
ee668506
KO
664static 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
cafe5635
KO
693static 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
ee668506
KO
708 bcache_device_unlink(d);
709
cafe5635
KO
710 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
cafe5635
KO
727static 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);
f59fce84 735 if (d->disk && d->disk->flags & GENHD_FL_UP)
cafe5635
KO
736 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)) ||
f59fce84
KO
758 bio_split_pool_init(&d->bio_split_hook) ||
759 !(d->disk = alloc_disk(1)) ||
760 !(q = blk_alloc_queue(GFP_KERNEL)))
cafe5635
KO
761 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
cafe5635
KO
770 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);
ee668506 819 bd_link_disk_holder(dc->bdev, dc->disk.disk);
cafe5635
KO
820#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
cafe5635
KO
839 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 */
b1a67b0f
KO
901 pr_err("Couldn't attach %s: block size less than set's block size",
902 buf);
cafe5635
KO
903 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
169ef1cf 933 if (bch_is_zero(u->uuid, 16)) {
cafe5635
KO
934 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);
cafe5635
KO
953 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) {
964 atomic_set(&dc->has_dirty, 1);
965 atomic_inc(&dc->count);
966 bch_writeback_queue(dc);
967 }
968
969 bch_cached_dev_run(dc);
ee668506 970 bcache_device_link(&dc->disk, c, "bdev");
cafe5635
KO
971
972 pr_info("Caching %s as %s on set %pU",
973 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
974 dc->disk.c->sb.set_uuid);
975 return 0;
976}
977
978void bch_cached_dev_release(struct kobject *kobj)
979{
980 struct cached_dev *dc = container_of(kobj, struct cached_dev,
981 disk.kobj);
982 kfree(dc);
983 module_put(THIS_MODULE);
984}
985
986static void cached_dev_free(struct closure *cl)
987{
988 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
989
990 cancel_delayed_work_sync(&dc->writeback_rate_update);
991
992 mutex_lock(&bch_register_lock);
993
f59fce84
KO
994 if (atomic_read(&dc->running))
995 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
cafe5635
KO
996 bcache_device_free(&dc->disk);
997 list_del(&dc->list);
998
999 mutex_unlock(&bch_register_lock);
1000
1001 if (!IS_ERR_OR_NULL(dc->bdev)) {
f59fce84
KO
1002 if (dc->bdev->bd_disk)
1003 blk_sync_queue(bdev_get_queue(dc->bdev));
1004
cafe5635
KO
1005 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1006 }
1007
1008 wake_up(&unregister_wait);
1009
1010 kobject_put(&dc->disk.kobj);
1011}
1012
1013static void cached_dev_flush(struct closure *cl)
1014{
1015 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1016 struct bcache_device *d = &dc->disk;
1017
1018 bch_cache_accounting_destroy(&dc->accounting);
1019 kobject_del(&d->kobj);
1020
1021 continue_at(cl, cached_dev_free, system_wq);
1022}
1023
1024static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1025{
f59fce84 1026 int ret;
cafe5635 1027 struct io *io;
f59fce84 1028 struct request_queue *q = bdev_get_queue(dc->bdev);
cafe5635
KO
1029
1030 __module_get(THIS_MODULE);
1031 INIT_LIST_HEAD(&dc->list);
f59fce84
KO
1032 closure_init(&dc->disk.cl, NULL);
1033 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
cafe5635 1034 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
cafe5635 1035 INIT_WORK(&dc->detach, cached_dev_detach_finish);
f59fce84
KO
1036 closure_init_unlocked(&dc->sb_write);
1037 INIT_LIST_HEAD(&dc->io_lru);
1038 spin_lock_init(&dc->io_lock);
1039 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
cafe5635
KO
1040
1041 dc->sequential_merge = true;
1042 dc->sequential_cutoff = 4 << 20;
1043
cafe5635
KO
1044 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1045 list_add(&io->lru, &dc->io_lru);
1046 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1047 }
1048
f59fce84
KO
1049 ret = bcache_device_init(&dc->disk, block_size);
1050 if (ret)
1051 return ret;
1052
1053 set_capacity(dc->disk.disk,
1054 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1055
1056 dc->disk.disk->queue->backing_dev_info.ra_pages =
1057 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1058 q->backing_dev_info.ra_pages);
1059
1060 bch_cached_dev_request_init(dc);
1061 bch_cached_dev_writeback_init(dc);
cafe5635 1062 return 0;
cafe5635
KO
1063}
1064
1065/* Cached device - bcache superblock */
1066
f59fce84 1067static void register_bdev(struct cache_sb *sb, struct page *sb_page,
cafe5635
KO
1068 struct block_device *bdev,
1069 struct cached_dev *dc)
1070{
1071 char name[BDEVNAME_SIZE];
1072 const char *err = "cannot allocate memory";
cafe5635
KO
1073 struct cache_set *c;
1074
cafe5635 1075 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
cafe5635
KO
1076 dc->bdev = bdev;
1077 dc->bdev->bd_holder = dc;
1078
f59fce84
KO
1079 bio_init(&dc->sb_bio);
1080 dc->sb_bio.bi_max_vecs = 1;
1081 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1082 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1083 get_page(sb_page);
4f0fd955 1084
f59fce84
KO
1085 if (cached_dev_init(dc, sb->block_size << 9))
1086 goto err;
cafe5635
KO
1087
1088 err = "error creating kobject";
1089 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1090 "bcache"))
1091 goto err;
1092 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1093 goto err;
1094
f59fce84
KO
1095 pr_info("registered backing device %s", bdevname(bdev, name));
1096
cafe5635
KO
1097 list_add(&dc->list, &uncached_devices);
1098 list_for_each_entry(c, &bch_cache_sets, list)
1099 bch_cached_dev_attach(dc, c);
1100
1101 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1102 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1103 bch_cached_dev_run(dc);
1104
f59fce84 1105 return;
cafe5635 1106err:
cafe5635 1107 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
f59fce84 1108 bcache_device_stop(&dc->disk);
cafe5635
KO
1109}
1110
1111/* Flash only volumes */
1112
1113void bch_flash_dev_release(struct kobject *kobj)
1114{
1115 struct bcache_device *d = container_of(kobj, struct bcache_device,
1116 kobj);
1117 kfree(d);
1118}
1119
1120static void flash_dev_free(struct closure *cl)
1121{
1122 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1123 bcache_device_free(d);
1124 kobject_put(&d->kobj);
1125}
1126
1127static void flash_dev_flush(struct closure *cl)
1128{
1129 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1130
ee668506 1131 bcache_device_unlink(d);
cafe5635
KO
1132 kobject_del(&d->kobj);
1133 continue_at(cl, flash_dev_free, system_wq);
1134}
1135
1136static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1137{
1138 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1139 GFP_KERNEL);
1140 if (!d)
1141 return -ENOMEM;
1142
1143 closure_init(&d->cl, NULL);
1144 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1145
1146 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1147
1148 if (bcache_device_init(d, block_bytes(c)))
1149 goto err;
1150
1151 bcache_device_attach(d, c, u - c->uuids);
1152 set_capacity(d->disk, u->sectors);
1153 bch_flash_dev_request_init(d);
1154 add_disk(d->disk);
1155
1156 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1157 goto err;
1158
1159 bcache_device_link(d, c, "volume");
1160
1161 return 0;
1162err:
1163 kobject_put(&d->kobj);
1164 return -ENOMEM;
1165}
1166
1167static int flash_devs_run(struct cache_set *c)
1168{
1169 int ret = 0;
1170 struct uuid_entry *u;
1171
1172 for (u = c->uuids;
1173 u < c->uuids + c->nr_uuids && !ret;
1174 u++)
1175 if (UUID_FLASH_ONLY(u))
1176 ret = flash_dev_run(c, u);
1177
1178 return ret;
1179}
1180
1181int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1182{
1183 struct uuid_entry *u;
1184
1185 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1186 return -EINTR;
1187
1188 u = uuid_find_empty(c);
1189 if (!u) {
1190 pr_err("Can't create volume, no room for UUID");
1191 return -EINVAL;
1192 }
1193
1194 get_random_bytes(u->uuid, 16);
1195 memset(u->label, 0, 32);
1196 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1197
1198 SET_UUID_FLASH_ONLY(u, 1);
1199 u->sectors = size >> 9;
1200
1201 bch_uuid_write(c);
1202
1203 return flash_dev_run(c, u);
1204}
1205
1206/* Cache set */
1207
1208__printf(2, 3)
1209bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1210{
1211 va_list args;
1212
1213 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1214 return false;
1215
1216 /* XXX: we can be called from atomic context
1217 acquire_console_sem();
1218 */
1219
1220 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1221
1222 va_start(args, fmt);
1223 vprintk(fmt, args);
1224 va_end(args);
1225
1226 printk(", disabling caching\n");
1227
1228 bch_cache_set_unregister(c);
1229 return true;
1230}
1231
1232void bch_cache_set_release(struct kobject *kobj)
1233{
1234 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1235 kfree(c);
1236 module_put(THIS_MODULE);
1237}
1238
1239static void cache_set_free(struct closure *cl)
1240{
1241 struct cache_set *c = container_of(cl, struct cache_set, cl);
1242 struct cache *ca;
1243 unsigned i;
1244
1245 if (!IS_ERR_OR_NULL(c->debug))
1246 debugfs_remove(c->debug);
1247
1248 bch_open_buckets_free(c);
1249 bch_btree_cache_free(c);
1250 bch_journal_free(c);
1251
1252 for_each_cache(ca, c, i)
1253 if (ca)
1254 kobject_put(&ca->kobj);
1255
1256 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1257 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1258
cafe5635
KO
1259 if (c->bio_split)
1260 bioset_free(c->bio_split);
57943511
KO
1261 if (c->fill_iter)
1262 mempool_destroy(c->fill_iter);
cafe5635
KO
1263 if (c->bio_meta)
1264 mempool_destroy(c->bio_meta);
1265 if (c->search)
1266 mempool_destroy(c->search);
1267 kfree(c->devices);
1268
1269 mutex_lock(&bch_register_lock);
1270 list_del(&c->list);
1271 mutex_unlock(&bch_register_lock);
1272
1273 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1274 wake_up(&unregister_wait);
1275
1276 closure_debug_destroy(&c->cl);
1277 kobject_put(&c->kobj);
1278}
1279
1280static void cache_set_flush(struct closure *cl)
1281{
1282 struct cache_set *c = container_of(cl, struct cache_set, caching);
1283 struct btree *b;
1284
1285 /* Shut down allocator threads */
1286 set_bit(CACHE_SET_STOPPING_2, &c->flags);
119ba0f8 1287 wake_up_allocators(c);
cafe5635
KO
1288
1289 bch_cache_accounting_destroy(&c->accounting);
1290
1291 kobject_put(&c->internal);
1292 kobject_del(&c->kobj);
1293
1294 if (!IS_ERR_OR_NULL(c->root))
1295 list_add(&c->root->list, &c->btree_cache);
1296
1297 /* Should skip this if we're unregistering because of an error */
1298 list_for_each_entry(b, &c->btree_cache, list)
1299 if (btree_node_dirty(b))
57943511 1300 bch_btree_node_write(b, NULL);
cafe5635
KO
1301
1302 closure_return(cl);
1303}
1304
1305static void __cache_set_unregister(struct closure *cl)
1306{
1307 struct cache_set *c = container_of(cl, struct cache_set, caching);
1308 struct cached_dev *dc, *t;
1309 size_t i;
1310
1311 mutex_lock(&bch_register_lock);
1312
1313 if (test_bit(CACHE_SET_UNREGISTERING, &c->flags))
1314 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1315 bch_cached_dev_detach(dc);
1316
1317 for (i = 0; i < c->nr_uuids; i++)
1318 if (c->devices[i] && UUID_FLASH_ONLY(&c->uuids[i]))
1319 bcache_device_stop(c->devices[i]);
1320
1321 mutex_unlock(&bch_register_lock);
1322
1323 continue_at(cl, cache_set_flush, system_wq);
1324}
1325
1326void bch_cache_set_stop(struct cache_set *c)
1327{
1328 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1329 closure_queue(&c->caching);
1330}
1331
1332void bch_cache_set_unregister(struct cache_set *c)
1333{
1334 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1335 bch_cache_set_stop(c);
1336}
1337
1338#define alloc_bucket_pages(gfp, c) \
1339 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1340
1341struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1342{
1343 int iter_size;
1344 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1345 if (!c)
1346 return NULL;
1347
1348 __module_get(THIS_MODULE);
1349 closure_init(&c->cl, NULL);
1350 set_closure_fn(&c->cl, cache_set_free, system_wq);
1351
1352 closure_init(&c->caching, &c->cl);
1353 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1354
1355 /* Maybe create continue_at_noreturn() and use it here? */
1356 closure_set_stopped(&c->cl);
1357 closure_put(&c->cl);
1358
1359 kobject_init(&c->kobj, &bch_cache_set_ktype);
1360 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1361
1362 bch_cache_accounting_init(&c->accounting, &c->cl);
1363
1364 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1365 c->sb.block_size = sb->block_size;
1366 c->sb.bucket_size = sb->bucket_size;
1367 c->sb.nr_in_set = sb->nr_in_set;
1368 c->sb.last_mount = sb->last_mount;
1369 c->bucket_bits = ilog2(sb->bucket_size);
1370 c->block_bits = ilog2(sb->block_size);
1371 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1372
1373 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1374 if (c->btree_pages > BTREE_MAX_PAGES)
1375 c->btree_pages = max_t(int, c->btree_pages / 4,
1376 BTREE_MAX_PAGES);
1377
cafe5635 1378 mutex_init(&c->bucket_lock);
cafe5635
KO
1379 mutex_init(&c->sort_lock);
1380 spin_lock_init(&c->sort_time_lock);
1381 closure_init_unlocked(&c->sb_write);
1382 closure_init_unlocked(&c->uuid_write);
1383 spin_lock_init(&c->btree_read_time_lock);
1384 bch_moving_init_cache_set(c);
1385
1386 INIT_LIST_HEAD(&c->list);
1387 INIT_LIST_HEAD(&c->cached_devs);
1388 INIT_LIST_HEAD(&c->btree_cache);
1389 INIT_LIST_HEAD(&c->btree_cache_freeable);
1390 INIT_LIST_HEAD(&c->btree_cache_freed);
1391 INIT_LIST_HEAD(&c->data_buckets);
1392
1393 c->search = mempool_create_slab_pool(32, bch_search_cache);
1394 if (!c->search)
1395 goto err;
1396
1397 iter_size = (sb->bucket_size / sb->block_size + 1) *
1398 sizeof(struct btree_iter_set);
1399
1400 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1401 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1402 sizeof(struct bbio) + sizeof(struct bio_vec) *
1403 bucket_pages(c))) ||
57943511 1404 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
cafe5635 1405 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
cafe5635
KO
1406 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1407 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1408 bch_journal_alloc(c) ||
1409 bch_btree_cache_alloc(c) ||
1410 bch_open_buckets_alloc(c))
1411 goto err;
1412
cafe5635
KO
1413 c->congested_read_threshold_us = 2000;
1414 c->congested_write_threshold_us = 20000;
1415 c->error_limit = 8 << IO_ERROR_SHIFT;
1416
1417 return c;
1418err:
1419 bch_cache_set_unregister(c);
1420 return NULL;
1421}
1422
1423static void run_cache_set(struct cache_set *c)
1424{
1425 const char *err = "cannot allocate memory";
1426 struct cached_dev *dc, *t;
1427 struct cache *ca;
1428 unsigned i;
1429
1430 struct btree_op op;
1431 bch_btree_op_init_stack(&op);
1432 op.lock = SHRT_MAX;
1433
1434 for_each_cache(ca, c, i)
1435 c->nbuckets += ca->sb.nbuckets;
1436
1437 if (CACHE_SYNC(&c->sb)) {
1438 LIST_HEAD(journal);
1439 struct bkey *k;
1440 struct jset *j;
1441
1442 err = "cannot allocate memory for journal";
1443 if (bch_journal_read(c, &journal, &op))
1444 goto err;
1445
1446 pr_debug("btree_journal_read() done");
1447
1448 err = "no journal entries found";
1449 if (list_empty(&journal))
1450 goto err;
1451
1452 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1453
1454 err = "IO error reading priorities";
1455 for_each_cache(ca, c, i)
1456 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1457
1458 /*
1459 * If prio_read() fails it'll call cache_set_error and we'll
1460 * tear everything down right away, but if we perhaps checked
1461 * sooner we could avoid journal replay.
1462 */
1463
1464 k = &j->btree_root;
1465
1466 err = "bad btree root";
1467 if (__bch_ptr_invalid(c, j->btree_level + 1, k))
1468 goto err;
1469
1470 err = "error reading btree root";
1471 c->root = bch_btree_node_get(c, k, j->btree_level, &op);
1472 if (IS_ERR_OR_NULL(c->root))
1473 goto err;
1474
1475 list_del_init(&c->root->list);
1476 rw_unlock(true, c->root);
1477
1478 err = uuid_read(c, j, &op.cl);
1479 if (err)
1480 goto err;
1481
1482 err = "error in recovery";
1483 if (bch_btree_check(c, &op))
1484 goto err;
1485
1486 bch_journal_mark(c, &journal);
1487 bch_btree_gc_finish(c);
1488 pr_debug("btree_check() done");
1489
1490 /*
1491 * bcache_journal_next() can't happen sooner, or
1492 * btree_gc_finish() will give spurious errors about last_gc >
1493 * gc_gen - this is a hack but oh well.
1494 */
1495 bch_journal_next(&c->journal);
1496
119ba0f8 1497 err = "error starting allocator thread";
cafe5635 1498 for_each_cache(ca, c, i)
119ba0f8
KO
1499 if (bch_cache_allocator_start(ca))
1500 goto err;
cafe5635
KO
1501
1502 /*
1503 * First place it's safe to allocate: btree_check() and
1504 * btree_gc_finish() have to run before we have buckets to
1505 * allocate, and bch_bucket_alloc_set() might cause a journal
1506 * entry to be written so bcache_journal_next() has to be called
1507 * first.
1508 *
1509 * If the uuids were in the old format we have to rewrite them
1510 * before the next journal entry is written:
1511 */
1512 if (j->version < BCACHE_JSET_VERSION_UUID)
1513 __uuid_write(c);
1514
1515 bch_journal_replay(c, &journal, &op);
1516 } else {
1517 pr_notice("invalidating existing data");
1518 /* Don't want invalidate_buckets() to queue a gc yet */
1519 closure_lock(&c->gc, NULL);
1520
1521 for_each_cache(ca, c, i) {
1522 unsigned j;
1523
1524 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1525 2, SB_JOURNAL_BUCKETS);
1526
1527 for (j = 0; j < ca->sb.keys; j++)
1528 ca->sb.d[j] = ca->sb.first_bucket + j;
1529 }
1530
1531 bch_btree_gc_finish(c);
1532
119ba0f8 1533 err = "error starting allocator thread";
cafe5635 1534 for_each_cache(ca, c, i)
119ba0f8
KO
1535 if (bch_cache_allocator_start(ca))
1536 goto err;
cafe5635
KO
1537
1538 mutex_lock(&c->bucket_lock);
1539 for_each_cache(ca, c, i)
1540 bch_prio_write(ca);
1541 mutex_unlock(&c->bucket_lock);
1542
cafe5635
KO
1543 err = "cannot allocate new UUID bucket";
1544 if (__uuid_write(c))
1545 goto err_unlock_gc;
1546
1547 err = "cannot allocate new btree root";
1548 c->root = bch_btree_node_alloc(c, 0, &op.cl);
1549 if (IS_ERR_OR_NULL(c->root))
1550 goto err_unlock_gc;
1551
1552 bkey_copy_key(&c->root->key, &MAX_KEY);
57943511 1553 bch_btree_node_write(c->root, &op.cl);
cafe5635
KO
1554
1555 bch_btree_set_root(c->root);
1556 rw_unlock(true, c->root);
1557
1558 /*
1559 * We don't want to write the first journal entry until
1560 * everything is set up - fortunately journal entries won't be
1561 * written until the SET_CACHE_SYNC() here:
1562 */
1563 SET_CACHE_SYNC(&c->sb, true);
1564
1565 bch_journal_next(&c->journal);
1566 bch_journal_meta(c, &op.cl);
1567
1568 /* Unlock */
1569 closure_set_stopped(&c->gc.cl);
1570 closure_put(&c->gc.cl);
1571 }
1572
1573 closure_sync(&op.cl);
1574 c->sb.last_mount = get_seconds();
1575 bcache_write_super(c);
1576
1577 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1578 bch_cached_dev_attach(dc, c);
1579
1580 flash_devs_run(c);
1581
1582 return;
1583err_unlock_gc:
1584 closure_set_stopped(&c->gc.cl);
1585 closure_put(&c->gc.cl);
1586err:
1587 closure_sync(&op.cl);
1588 /* XXX: test this, it's broken */
1589 bch_cache_set_error(c, err);
1590}
1591
1592static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1593{
1594 return ca->sb.block_size == c->sb.block_size &&
1595 ca->sb.bucket_size == c->sb.block_size &&
1596 ca->sb.nr_in_set == c->sb.nr_in_set;
1597}
1598
1599static const char *register_cache_set(struct cache *ca)
1600{
1601 char buf[12];
1602 const char *err = "cannot allocate memory";
1603 struct cache_set *c;
1604
1605 list_for_each_entry(c, &bch_cache_sets, list)
1606 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1607 if (c->cache[ca->sb.nr_this_dev])
1608 return "duplicate cache set member";
1609
1610 if (!can_attach_cache(ca, c))
1611 return "cache sb does not match set";
1612
1613 if (!CACHE_SYNC(&ca->sb))
1614 SET_CACHE_SYNC(&c->sb, false);
1615
1616 goto found;
1617 }
1618
1619 c = bch_cache_set_alloc(&ca->sb);
1620 if (!c)
1621 return err;
1622
1623 err = "error creating kobject";
1624 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1625 kobject_add(&c->internal, &c->kobj, "internal"))
1626 goto err;
1627
1628 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1629 goto err;
1630
1631 bch_debug_init_cache_set(c);
1632
1633 list_add(&c->list, &bch_cache_sets);
1634found:
1635 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1636 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1637 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1638 goto err;
1639
1640 if (ca->sb.seq > c->sb.seq) {
1641 c->sb.version = ca->sb.version;
1642 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1643 c->sb.flags = ca->sb.flags;
1644 c->sb.seq = ca->sb.seq;
1645 pr_debug("set version = %llu", c->sb.version);
1646 }
1647
1648 ca->set = c;
1649 ca->set->cache[ca->sb.nr_this_dev] = ca;
1650 c->cache_by_alloc[c->caches_loaded++] = ca;
1651
1652 if (c->caches_loaded == c->sb.nr_in_set)
1653 run_cache_set(c);
1654
1655 return NULL;
1656err:
1657 bch_cache_set_unregister(c);
1658 return err;
1659}
1660
1661/* Cache device */
1662
1663void bch_cache_release(struct kobject *kobj)
1664{
1665 struct cache *ca = container_of(kobj, struct cache, kobj);
1666
1667 if (ca->set)
1668 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1669
1670 bch_cache_allocator_exit(ca);
1671
1672 bio_split_pool_free(&ca->bio_split_hook);
1673
cafe5635
KO
1674 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1675 kfree(ca->prio_buckets);
1676 vfree(ca->buckets);
1677
1678 free_heap(&ca->heap);
1679 free_fifo(&ca->unused);
1680 free_fifo(&ca->free_inc);
1681 free_fifo(&ca->free);
1682
1683 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1684 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1685
1686 if (!IS_ERR_OR_NULL(ca->bdev)) {
1687 blk_sync_queue(bdev_get_queue(ca->bdev));
1688 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1689 }
1690
1691 kfree(ca);
1692 module_put(THIS_MODULE);
1693}
1694
1695static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1696{
1697 size_t free;
1698 struct bucket *b;
1699
cafe5635
KO
1700 __module_get(THIS_MODULE);
1701 kobject_init(&ca->kobj, &bch_cache_ktype);
1702
cafe5635
KO
1703 INIT_LIST_HEAD(&ca->discards);
1704
cafe5635
KO
1705 bio_init(&ca->journal.bio);
1706 ca->journal.bio.bi_max_vecs = 8;
1707 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1708
1709 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1710 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1711
1712 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1713 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1714 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1715 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
f59fce84 1716 !(ca->buckets = vzalloc(sizeof(struct bucket) *
cafe5635
KO
1717 ca->sb.nbuckets)) ||
1718 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1719 2, GFP_KERNEL)) ||
1720 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
cafe5635 1721 bio_split_pool_init(&ca->bio_split_hook))
f59fce84 1722 return -ENOMEM;
cafe5635
KO
1723
1724 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1725
cafe5635
KO
1726 for_each_bucket(b, ca)
1727 atomic_set(&b->pin, 0);
1728
1729 if (bch_cache_allocator_init(ca))
1730 goto err;
1731
1732 return 0;
1733err:
1734 kobject_put(&ca->kobj);
1735 return -ENOMEM;
1736}
1737
f59fce84 1738static void register_cache(struct cache_sb *sb, struct page *sb_page,
cafe5635
KO
1739 struct block_device *bdev, struct cache *ca)
1740{
1741 char name[BDEVNAME_SIZE];
1742 const char *err = "cannot allocate memory";
1743
f59fce84 1744 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
cafe5635
KO
1745 ca->bdev = bdev;
1746 ca->bdev->bd_holder = ca;
1747
f59fce84
KO
1748 bio_init(&ca->sb_bio);
1749 ca->sb_bio.bi_max_vecs = 1;
1750 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1751 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1752 get_page(sb_page);
1753
cafe5635
KO
1754 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1755 ca->discard = CACHE_DISCARD(&ca->sb);
1756
f59fce84
KO
1757 if (cache_alloc(sb, ca) != 0)
1758 goto err;
1759
cafe5635
KO
1760 err = "error creating kobject";
1761 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1762 goto err;
1763
1764 err = register_cache_set(ca);
1765 if (err)
1766 goto err;
1767
1768 pr_info("registered cache device %s", bdevname(bdev, name));
f59fce84 1769 return;
cafe5635 1770err:
f59fce84 1771 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
cafe5635 1772 kobject_put(&ca->kobj);
cafe5635
KO
1773}
1774
1775/* Global interfaces/init */
1776
1777static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1778 const char *, size_t);
1779
1780kobj_attribute_write(register, register_bcache);
1781kobj_attribute_write(register_quiet, register_bcache);
1782
a9dd53ad
GP
1783static bool bch_is_open_backing(struct block_device *bdev) {
1784 struct cache_set *c, *tc;
1785 struct cached_dev *dc, *t;
1786
1787 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1788 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1789 if (dc->bdev == bdev)
1790 return true;
1791 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1792 if (dc->bdev == bdev)
1793 return true;
1794 return false;
1795}
1796
1797static bool bch_is_open_cache(struct block_device *bdev) {
1798 struct cache_set *c, *tc;
1799 struct cache *ca;
1800 unsigned i;
1801
1802 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1803 for_each_cache(ca, c, i)
1804 if (ca->bdev == bdev)
1805 return true;
1806 return false;
1807}
1808
1809static bool bch_is_open(struct block_device *bdev) {
1810 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1811}
1812
cafe5635
KO
1813static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1814 const char *buffer, size_t size)
1815{
1816 ssize_t ret = size;
1817 const char *err = "cannot allocate memory";
1818 char *path = NULL;
1819 struct cache_sb *sb = NULL;
1820 struct block_device *bdev = NULL;
1821 struct page *sb_page = NULL;
1822
1823 if (!try_module_get(THIS_MODULE))
1824 return -EBUSY;
1825
1826 mutex_lock(&bch_register_lock);
1827
1828 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1829 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1830 goto err;
1831
1832 err = "failed to open device";
1833 bdev = blkdev_get_by_path(strim(path),
1834 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1835 sb);
f59fce84 1836 if (IS_ERR(bdev)) {
a9dd53ad
GP
1837 if (bdev == ERR_PTR(-EBUSY)) {
1838 bdev = lookup_bdev(strim(path));
1839 if (!IS_ERR(bdev) && bch_is_open(bdev))
1840 err = "device already registered";
1841 else
1842 err = "device busy";
1843 }
cafe5635 1844 goto err;
f59fce84
KO
1845 }
1846
1847 err = "failed to set blocksize";
1848 if (set_blocksize(bdev, 4096))
1849 goto err_close;
cafe5635
KO
1850
1851 err = read_super(sb, bdev, &sb_page);
1852 if (err)
1853 goto err_close;
1854
2903381f 1855 if (SB_IS_BDEV(sb)) {
cafe5635 1856 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
f59fce84
KO
1857 if (!dc)
1858 goto err_close;
cafe5635 1859
f59fce84 1860 register_bdev(sb, sb_page, bdev, dc);
cafe5635
KO
1861 } else {
1862 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
f59fce84
KO
1863 if (!ca)
1864 goto err_close;
cafe5635 1865
f59fce84 1866 register_cache(sb, sb_page, bdev, ca);
cafe5635 1867 }
f59fce84
KO
1868out:
1869 if (sb_page)
cafe5635 1870 put_page(sb_page);
cafe5635
KO
1871 kfree(sb);
1872 kfree(path);
1873 mutex_unlock(&bch_register_lock);
1874 module_put(THIS_MODULE);
1875 return ret;
f59fce84
KO
1876
1877err_close:
1878 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1879err:
1880 if (attr != &ksysfs_register_quiet)
1881 pr_info("error opening %s: %s", path, err);
1882 ret = -EINVAL;
1883 goto out;
cafe5635
KO
1884}
1885
1886static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1887{
1888 if (code == SYS_DOWN ||
1889 code == SYS_HALT ||
1890 code == SYS_POWER_OFF) {
1891 DEFINE_WAIT(wait);
1892 unsigned long start = jiffies;
1893 bool stopped = false;
1894
1895 struct cache_set *c, *tc;
1896 struct cached_dev *dc, *tdc;
1897
1898 mutex_lock(&bch_register_lock);
1899
1900 if (list_empty(&bch_cache_sets) &&
1901 list_empty(&uncached_devices))
1902 goto out;
1903
1904 pr_info("Stopping all devices:");
1905
1906 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1907 bch_cache_set_stop(c);
1908
1909 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1910 bcache_device_stop(&dc->disk);
1911
1912 /* What's a condition variable? */
1913 while (1) {
1914 long timeout = start + 2 * HZ - jiffies;
1915
1916 stopped = list_empty(&bch_cache_sets) &&
1917 list_empty(&uncached_devices);
1918
1919 if (timeout < 0 || stopped)
1920 break;
1921
1922 prepare_to_wait(&unregister_wait, &wait,
1923 TASK_UNINTERRUPTIBLE);
1924
1925 mutex_unlock(&bch_register_lock);
1926 schedule_timeout(timeout);
1927 mutex_lock(&bch_register_lock);
1928 }
1929
1930 finish_wait(&unregister_wait, &wait);
1931
1932 if (stopped)
1933 pr_info("All devices stopped");
1934 else
1935 pr_notice("Timeout waiting for devices to be closed");
1936out:
1937 mutex_unlock(&bch_register_lock);
1938 }
1939
1940 return NOTIFY_DONE;
1941}
1942
1943static struct notifier_block reboot = {
1944 .notifier_call = bcache_reboot,
1945 .priority = INT_MAX, /* before any real devices */
1946};
1947
1948static void bcache_exit(void)
1949{
1950 bch_debug_exit();
1951 bch_writeback_exit();
1952 bch_request_exit();
1953 bch_btree_exit();
1954 if (bcache_kobj)
1955 kobject_put(bcache_kobj);
1956 if (bcache_wq)
1957 destroy_workqueue(bcache_wq);
1958 unregister_blkdev(bcache_major, "bcache");
1959 unregister_reboot_notifier(&reboot);
1960}
1961
1962static int __init bcache_init(void)
1963{
1964 static const struct attribute *files[] = {
1965 &ksysfs_register.attr,
1966 &ksysfs_register_quiet.attr,
1967 NULL
1968 };
1969
1970 mutex_init(&bch_register_lock);
1971 init_waitqueue_head(&unregister_wait);
1972 register_reboot_notifier(&reboot);
07e86ccb 1973 closure_debug_init();
cafe5635
KO
1974
1975 bcache_major = register_blkdev(0, "bcache");
1976 if (bcache_major < 0)
1977 return bcache_major;
1978
1979 if (!(bcache_wq = create_workqueue("bcache")) ||
1980 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
1981 sysfs_create_files(bcache_kobj, files) ||
1982 bch_btree_init() ||
1983 bch_request_init() ||
1984 bch_writeback_init() ||
1985 bch_debug_init(bcache_kobj))
1986 goto err;
1987
1988 return 0;
1989err:
1990 bcache_exit();
1991 return -ENOMEM;
1992}
1993
1994module_exit(bcache_exit);
1995module_init(bcache_init);
This page took 0.110224 seconds and 5 git commands to generate.