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