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