bcache: Fix sysfs splat on shutdown with flash only devs
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / md / bcache / super.c
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 #include "writeback.h"
14
15 #include <linux/blkdev.h>
16 #include <linux/buffer_head.h>
17 #include <linux/debugfs.h>
18 #include <linux/genhd.h>
19 #include <linux/kthread.h>
20 #include <linux/module.h>
21 #include <linux/random.h>
22 #include <linux/reboot.h>
23 #include <linux/sysfs.h>
24
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
27
28 static const char bcache_magic[] = {
29 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
30 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
31 };
32
33 static 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 */
39 const char * const bch_cache_modes[] = {
40 "default",
41 "writethrough",
42 "writeback",
43 "writearound",
44 "none",
45 NULL
46 };
47
48 static struct kobject *bcache_kobj;
49 struct mutex bch_register_lock;
50 LIST_HEAD(bch_cache_sets);
51 static LIST_HEAD(uncached_devices);
52
53 static int bcache_major, bcache_minor;
54 static wait_queue_head_t unregister_wait;
55 struct workqueue_struct *bcache_wq;
56
57 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
58
59 static void bio_split_pool_free(struct bio_split_pool *p)
60 {
61 if (p->bio_split_hook)
62 mempool_destroy(p->bio_split_hook);
63
64 if (p->bio_split)
65 bioset_free(p->bio_split);
66 }
67
68 static 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
84 static 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);
107 sb->last_mount = le32_to_cpu(s->last_mount);
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";
133 if (bch_is_zero(sb->uuid, 16))
134 goto err;
135
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
142 switch (sb->version) {
143 case BCACHE_SB_VERSION_BDEV:
144 sb->data_offset = BDEV_DATA_START_DEFAULT;
145 break;
146 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
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;
152
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);
159
160 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
161 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
162
163 err = "Too many buckets";
164 if (sb->nbuckets > LONG_MAX)
165 goto err;
166
167 err = "Not enough buckets";
168 if (sb->nbuckets < 1 << 7)
169 goto err;
170
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;
177
178 err = "Invalid superblock: device too small";
179 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
180 goto err;
181
182 err = "Bad UUID";
183 if (bch_is_zero(sb->set_uuid, 16))
184 goto err;
185
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)
190 goto err;
191
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;
196
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";
208 goto err;
209 }
210
211 sb->last_mount = get_seconds();
212 err = NULL;
213
214 get_page(bh->b_page);
215 *res = bh->b_page;
216 err:
217 put_bh(bh);
218 return err;
219 }
220
221 static 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
229 static 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;
237 bch_bio_map(bio, NULL);
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
264 void 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
282 static 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
290 void 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
303 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
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
323 static 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
333 static 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;
339 char buf[80];
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;
352 bch_bio_map(bio, c->uuids);
353
354 bch_submit_bbio(bio, c, k, i);
355
356 if (!(rw & WRITE))
357 break;
358 }
359
360 bch_bkey_to_text(buf, sizeof(buf), k);
361 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
362
363 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
364 if (!bch_is_zero(u->uuid, 16))
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
372 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
373 {
374 struct bkey *k = &j->uuid_bucket;
375
376 if (bch_btree_ptr_invalid(c, k))
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
413 static 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
421 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true))
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);
429 bkey_put(c, &k.key);
430 return 0;
431 }
432
433 int 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
443 static 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
455 static 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
488 static 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
497 static 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;
511 bch_bio_map(bio, ca->disk_buckets);
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
520 void 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));
541
542 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
543 long bucket;
544 struct prio_set *p = ca->disk_buckets;
545 struct bucket_disk *d = p->data;
546 struct bucket_disk *end = d + prios_per_bucket(ca);
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];
556 p->magic = pset_magic(&ca->sb);
557 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
558
559 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, true);
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
587 static 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
604 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
605 pr_warn("bad csum reading priorities");
606
607 if (p->magic != pset_magic(&ca->sb))
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
621 static int open_dev(struct block_device *b, fmode_t mode)
622 {
623 struct bcache_device *d = b->bd_disk->private_data;
624 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
625 return -ENXIO;
626
627 closure_get(&d->cl);
628 return 0;
629 }
630
631 static void release_dev(struct gendisk *b, fmode_t mode)
632 {
633 struct bcache_device *d = b->private_data;
634 closure_put(&d->cl);
635 }
636
637 static 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
644 static 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
651 void bcache_device_stop(struct bcache_device *d)
652 {
653 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
654 closure_queue(&d->cl);
655 }
656
657 static void bcache_device_unlink(struct bcache_device *d)
658 {
659 lockdep_assert_held(&bch_register_lock);
660
661 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
662 unsigned i;
663 struct cache *ca;
664
665 sysfs_remove_link(&d->c->kobj, d->name);
666 sysfs_remove_link(&d->kobj, "cache");
667
668 for_each_cache(ca, d->c, i)
669 bd_unlink_disk_holder(ca->bdev, d->disk);
670 }
671 }
672
673 static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
674 const char *name)
675 {
676 unsigned i;
677 struct cache *ca;
678
679 for_each_cache(ca, d->c, i)
680 bd_link_disk_holder(ca->bdev, d->disk);
681
682 snprintf(d->name, BCACHEDEVNAME_SIZE,
683 "%s%u", name, d->id);
684
685 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
686 sysfs_create_link(&c->kobj, &d->kobj, d->name),
687 "Couldn't create device <-> cache set symlinks");
688 }
689
690 static void bcache_device_detach(struct bcache_device *d)
691 {
692 lockdep_assert_held(&bch_register_lock);
693
694 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
695 struct uuid_entry *u = d->c->uuids + d->id;
696
697 SET_UUID_FLASH_ONLY(u, 0);
698 memcpy(u->uuid, invalid_uuid, 16);
699 u->invalidated = cpu_to_le32(get_seconds());
700 bch_uuid_write(d->c);
701 }
702
703 bcache_device_unlink(d);
704
705 d->c->devices[d->id] = NULL;
706 closure_put(&d->c->caching);
707 d->c = NULL;
708 }
709
710 static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
711 unsigned id)
712 {
713 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
714
715 d->id = id;
716 d->c = c;
717 c->devices[id] = d;
718
719 closure_get(&c->caching);
720 }
721
722 static void bcache_device_free(struct bcache_device *d)
723 {
724 lockdep_assert_held(&bch_register_lock);
725
726 pr_info("%s stopped", d->disk->disk_name);
727
728 if (d->c)
729 bcache_device_detach(d);
730 if (d->disk && d->disk->flags & GENHD_FL_UP)
731 del_gendisk(d->disk);
732 if (d->disk && d->disk->queue)
733 blk_cleanup_queue(d->disk->queue);
734 if (d->disk)
735 put_disk(d->disk);
736
737 bio_split_pool_free(&d->bio_split_hook);
738 if (d->unaligned_bvec)
739 mempool_destroy(d->unaligned_bvec);
740 if (d->bio_split)
741 bioset_free(d->bio_split);
742 if (is_vmalloc_addr(d->full_dirty_stripes))
743 vfree(d->full_dirty_stripes);
744 else
745 kfree(d->full_dirty_stripes);
746 if (is_vmalloc_addr(d->stripe_sectors_dirty))
747 vfree(d->stripe_sectors_dirty);
748 else
749 kfree(d->stripe_sectors_dirty);
750
751 closure_debug_destroy(&d->cl);
752 }
753
754 static int bcache_device_init(struct bcache_device *d, unsigned block_size,
755 sector_t sectors)
756 {
757 struct request_queue *q;
758 size_t n;
759
760 if (!d->stripe_size)
761 d->stripe_size = 1 << 31;
762
763 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
764
765 if (!d->nr_stripes ||
766 d->nr_stripes > INT_MAX ||
767 d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
768 pr_err("nr_stripes too large");
769 return -ENOMEM;
770 }
771
772 n = d->nr_stripes * sizeof(atomic_t);
773 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
774 ? kzalloc(n, GFP_KERNEL)
775 : vzalloc(n);
776 if (!d->stripe_sectors_dirty)
777 return -ENOMEM;
778
779 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
780 d->full_dirty_stripes = n < PAGE_SIZE << 6
781 ? kzalloc(n, GFP_KERNEL)
782 : vzalloc(n);
783 if (!d->full_dirty_stripes)
784 return -ENOMEM;
785
786 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
787 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
788 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
789 bio_split_pool_init(&d->bio_split_hook) ||
790 !(d->disk = alloc_disk(1)) ||
791 !(q = blk_alloc_queue(GFP_KERNEL)))
792 return -ENOMEM;
793
794 set_capacity(d->disk, sectors);
795 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
796
797 d->disk->major = bcache_major;
798 d->disk->first_minor = bcache_minor++;
799 d->disk->fops = &bcache_ops;
800 d->disk->private_data = d;
801
802 blk_queue_make_request(q, NULL);
803 d->disk->queue = q;
804 q->queuedata = d;
805 q->backing_dev_info.congested_data = d;
806 q->limits.max_hw_sectors = UINT_MAX;
807 q->limits.max_sectors = UINT_MAX;
808 q->limits.max_segment_size = UINT_MAX;
809 q->limits.max_segments = BIO_MAX_PAGES;
810 q->limits.max_discard_sectors = UINT_MAX;
811 q->limits.io_min = block_size;
812 q->limits.logical_block_size = block_size;
813 q->limits.physical_block_size = block_size;
814 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
815 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
816
817 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
818
819 return 0;
820 }
821
822 /* Cached device */
823
824 static void calc_cached_dev_sectors(struct cache_set *c)
825 {
826 uint64_t sectors = 0;
827 struct cached_dev *dc;
828
829 list_for_each_entry(dc, &c->cached_devs, list)
830 sectors += bdev_sectors(dc->bdev);
831
832 c->cached_dev_sectors = sectors;
833 }
834
835 void bch_cached_dev_run(struct cached_dev *dc)
836 {
837 struct bcache_device *d = &dc->disk;
838 char buf[SB_LABEL_SIZE + 1];
839 char *env[] = {
840 "DRIVER=bcache",
841 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
842 NULL,
843 NULL,
844 };
845
846 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
847 buf[SB_LABEL_SIZE] = '\0';
848 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
849
850 if (atomic_xchg(&dc->running, 1))
851 return;
852
853 if (!d->c &&
854 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
855 struct closure cl;
856 closure_init_stack(&cl);
857
858 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
859 bch_write_bdev_super(dc, &cl);
860 closure_sync(&cl);
861 }
862
863 add_disk(d->disk);
864 bd_link_disk_holder(dc->bdev, dc->disk.disk);
865 /* won't show up in the uevent file, use udevadm monitor -e instead
866 * only class / kset properties are persistent */
867 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
868 kfree(env[1]);
869 kfree(env[2]);
870
871 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
872 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
873 pr_debug("error creating sysfs link");
874 }
875
876 static void cached_dev_detach_finish(struct work_struct *w)
877 {
878 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
879 char buf[BDEVNAME_SIZE];
880 struct closure cl;
881 closure_init_stack(&cl);
882
883 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
884 BUG_ON(atomic_read(&dc->count));
885
886 mutex_lock(&bch_register_lock);
887
888 memset(&dc->sb.set_uuid, 0, 16);
889 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
890
891 bch_write_bdev_super(dc, &cl);
892 closure_sync(&cl);
893
894 bcache_device_detach(&dc->disk);
895 list_move(&dc->list, &uncached_devices);
896
897 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
898
899 mutex_unlock(&bch_register_lock);
900
901 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
902
903 /* Drop ref we took in cached_dev_detach() */
904 closure_put(&dc->disk.cl);
905 }
906
907 void bch_cached_dev_detach(struct cached_dev *dc)
908 {
909 lockdep_assert_held(&bch_register_lock);
910
911 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
912 return;
913
914 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
915 return;
916
917 /*
918 * Block the device from being closed and freed until we're finished
919 * detaching
920 */
921 closure_get(&dc->disk.cl);
922
923 bch_writeback_queue(dc);
924 cached_dev_put(dc);
925 }
926
927 int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
928 {
929 uint32_t rtime = cpu_to_le32(get_seconds());
930 struct uuid_entry *u;
931 char buf[BDEVNAME_SIZE];
932
933 bdevname(dc->bdev, buf);
934
935 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
936 return -ENOENT;
937
938 if (dc->disk.c) {
939 pr_err("Can't attach %s: already attached", buf);
940 return -EINVAL;
941 }
942
943 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
944 pr_err("Can't attach %s: shutting down", buf);
945 return -EINVAL;
946 }
947
948 if (dc->sb.block_size < c->sb.block_size) {
949 /* Will die */
950 pr_err("Couldn't attach %s: block size less than set's block size",
951 buf);
952 return -EINVAL;
953 }
954
955 u = uuid_find(c, dc->sb.uuid);
956
957 if (u &&
958 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
959 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
960 memcpy(u->uuid, invalid_uuid, 16);
961 u->invalidated = cpu_to_le32(get_seconds());
962 u = NULL;
963 }
964
965 if (!u) {
966 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
967 pr_err("Couldn't find uuid for %s in set", buf);
968 return -ENOENT;
969 }
970
971 u = uuid_find_empty(c);
972 if (!u) {
973 pr_err("Not caching %s, no room for UUID", buf);
974 return -EINVAL;
975 }
976 }
977
978 /* Deadlocks since we're called via sysfs...
979 sysfs_remove_file(&dc->kobj, &sysfs_attach);
980 */
981
982 if (bch_is_zero(u->uuid, 16)) {
983 struct closure cl;
984 closure_init_stack(&cl);
985
986 memcpy(u->uuid, dc->sb.uuid, 16);
987 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
988 u->first_reg = u->last_reg = rtime;
989 bch_uuid_write(c);
990
991 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
992 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
993
994 bch_write_bdev_super(dc, &cl);
995 closure_sync(&cl);
996 } else {
997 u->last_reg = rtime;
998 bch_uuid_write(c);
999 }
1000
1001 bcache_device_attach(&dc->disk, c, u - c->uuids);
1002 list_move(&dc->list, &c->cached_devs);
1003 calc_cached_dev_sectors(c);
1004
1005 smp_wmb();
1006 /*
1007 * dc->c must be set before dc->count != 0 - paired with the mb in
1008 * cached_dev_get()
1009 */
1010 atomic_set(&dc->count, 1);
1011
1012 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1013 bch_sectors_dirty_init(dc);
1014 atomic_set(&dc->has_dirty, 1);
1015 atomic_inc(&dc->count);
1016 bch_writeback_queue(dc);
1017 }
1018
1019 bch_cached_dev_run(dc);
1020 bcache_device_link(&dc->disk, c, "bdev");
1021
1022 pr_info("Caching %s as %s on set %pU",
1023 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
1024 dc->disk.c->sb.set_uuid);
1025 return 0;
1026 }
1027
1028 void bch_cached_dev_release(struct kobject *kobj)
1029 {
1030 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1031 disk.kobj);
1032 kfree(dc);
1033 module_put(THIS_MODULE);
1034 }
1035
1036 static void cached_dev_free(struct closure *cl)
1037 {
1038 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1039
1040 cancel_delayed_work_sync(&dc->writeback_rate_update);
1041 kthread_stop(dc->writeback_thread);
1042
1043 mutex_lock(&bch_register_lock);
1044
1045 if (atomic_read(&dc->running))
1046 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
1047 bcache_device_free(&dc->disk);
1048 list_del(&dc->list);
1049
1050 mutex_unlock(&bch_register_lock);
1051
1052 if (!IS_ERR_OR_NULL(dc->bdev)) {
1053 if (dc->bdev->bd_disk)
1054 blk_sync_queue(bdev_get_queue(dc->bdev));
1055
1056 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1057 }
1058
1059 wake_up(&unregister_wait);
1060
1061 kobject_put(&dc->disk.kobj);
1062 }
1063
1064 static void cached_dev_flush(struct closure *cl)
1065 {
1066 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1067 struct bcache_device *d = &dc->disk;
1068
1069 mutex_lock(&bch_register_lock);
1070 bcache_device_unlink(d);
1071 mutex_unlock(&bch_register_lock);
1072
1073 bch_cache_accounting_destroy(&dc->accounting);
1074 kobject_del(&d->kobj);
1075
1076 continue_at(cl, cached_dev_free, system_wq);
1077 }
1078
1079 static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1080 {
1081 int ret;
1082 struct io *io;
1083 struct request_queue *q = bdev_get_queue(dc->bdev);
1084
1085 __module_get(THIS_MODULE);
1086 INIT_LIST_HEAD(&dc->list);
1087 closure_init(&dc->disk.cl, NULL);
1088 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1089 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1090 INIT_WORK(&dc->detach, cached_dev_detach_finish);
1091 closure_init_unlocked(&dc->sb_write);
1092 INIT_LIST_HEAD(&dc->io_lru);
1093 spin_lock_init(&dc->io_lock);
1094 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1095
1096 dc->sequential_cutoff = 4 << 20;
1097
1098 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1099 list_add(&io->lru, &dc->io_lru);
1100 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1101 }
1102
1103 ret = bcache_device_init(&dc->disk, block_size,
1104 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1105 if (ret)
1106 return ret;
1107
1108 set_capacity(dc->disk.disk,
1109 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1110
1111 dc->disk.disk->queue->backing_dev_info.ra_pages =
1112 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1113 q->backing_dev_info.ra_pages);
1114
1115 bch_cached_dev_request_init(dc);
1116 bch_cached_dev_writeback_init(dc);
1117 return 0;
1118 }
1119
1120 /* Cached device - bcache superblock */
1121
1122 static void register_bdev(struct cache_sb *sb, struct page *sb_page,
1123 struct block_device *bdev,
1124 struct cached_dev *dc)
1125 {
1126 char name[BDEVNAME_SIZE];
1127 const char *err = "cannot allocate memory";
1128 struct cache_set *c;
1129
1130 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1131 dc->bdev = bdev;
1132 dc->bdev->bd_holder = dc;
1133
1134 bio_init(&dc->sb_bio);
1135 dc->sb_bio.bi_max_vecs = 1;
1136 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1137 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1138 get_page(sb_page);
1139
1140 if (cached_dev_init(dc, sb->block_size << 9))
1141 goto err;
1142
1143 err = "error creating kobject";
1144 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1145 "bcache"))
1146 goto err;
1147 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1148 goto err;
1149
1150 pr_info("registered backing device %s", bdevname(bdev, name));
1151
1152 list_add(&dc->list, &uncached_devices);
1153 list_for_each_entry(c, &bch_cache_sets, list)
1154 bch_cached_dev_attach(dc, c);
1155
1156 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1157 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1158 bch_cached_dev_run(dc);
1159
1160 return;
1161 err:
1162 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1163 bcache_device_stop(&dc->disk);
1164 }
1165
1166 /* Flash only volumes */
1167
1168 void bch_flash_dev_release(struct kobject *kobj)
1169 {
1170 struct bcache_device *d = container_of(kobj, struct bcache_device,
1171 kobj);
1172 kfree(d);
1173 }
1174
1175 static void flash_dev_free(struct closure *cl)
1176 {
1177 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1178 bcache_device_free(d);
1179 kobject_put(&d->kobj);
1180 }
1181
1182 static void flash_dev_flush(struct closure *cl)
1183 {
1184 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1185
1186 bcache_device_unlink(d);
1187 kobject_del(&d->kobj);
1188 continue_at(cl, flash_dev_free, system_wq);
1189 }
1190
1191 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1192 {
1193 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1194 GFP_KERNEL);
1195 if (!d)
1196 return -ENOMEM;
1197
1198 closure_init(&d->cl, NULL);
1199 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1200
1201 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1202
1203 if (bcache_device_init(d, block_bytes(c), u->sectors))
1204 goto err;
1205
1206 bcache_device_attach(d, c, u - c->uuids);
1207 bch_flash_dev_request_init(d);
1208 add_disk(d->disk);
1209
1210 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1211 goto err;
1212
1213 bcache_device_link(d, c, "volume");
1214
1215 return 0;
1216 err:
1217 kobject_put(&d->kobj);
1218 return -ENOMEM;
1219 }
1220
1221 static int flash_devs_run(struct cache_set *c)
1222 {
1223 int ret = 0;
1224 struct uuid_entry *u;
1225
1226 for (u = c->uuids;
1227 u < c->uuids + c->nr_uuids && !ret;
1228 u++)
1229 if (UUID_FLASH_ONLY(u))
1230 ret = flash_dev_run(c, u);
1231
1232 return ret;
1233 }
1234
1235 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1236 {
1237 struct uuid_entry *u;
1238
1239 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1240 return -EINTR;
1241
1242 u = uuid_find_empty(c);
1243 if (!u) {
1244 pr_err("Can't create volume, no room for UUID");
1245 return -EINVAL;
1246 }
1247
1248 get_random_bytes(u->uuid, 16);
1249 memset(u->label, 0, 32);
1250 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1251
1252 SET_UUID_FLASH_ONLY(u, 1);
1253 u->sectors = size >> 9;
1254
1255 bch_uuid_write(c);
1256
1257 return flash_dev_run(c, u);
1258 }
1259
1260 /* Cache set */
1261
1262 __printf(2, 3)
1263 bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1264 {
1265 va_list args;
1266
1267 if (c->on_error != ON_ERROR_PANIC &&
1268 test_bit(CACHE_SET_STOPPING, &c->flags))
1269 return false;
1270
1271 /* XXX: we can be called from atomic context
1272 acquire_console_sem();
1273 */
1274
1275 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1276
1277 va_start(args, fmt);
1278 vprintk(fmt, args);
1279 va_end(args);
1280
1281 printk(", disabling caching\n");
1282
1283 if (c->on_error == ON_ERROR_PANIC)
1284 panic("panic forced after error\n");
1285
1286 bch_cache_set_unregister(c);
1287 return true;
1288 }
1289
1290 void bch_cache_set_release(struct kobject *kobj)
1291 {
1292 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1293 kfree(c);
1294 module_put(THIS_MODULE);
1295 }
1296
1297 static void cache_set_free(struct closure *cl)
1298 {
1299 struct cache_set *c = container_of(cl, struct cache_set, cl);
1300 struct cache *ca;
1301 unsigned i;
1302
1303 if (!IS_ERR_OR_NULL(c->debug))
1304 debugfs_remove(c->debug);
1305
1306 bch_open_buckets_free(c);
1307 bch_btree_cache_free(c);
1308 bch_journal_free(c);
1309
1310 for_each_cache(ca, c, i)
1311 if (ca)
1312 kobject_put(&ca->kobj);
1313
1314 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1315 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1316
1317 if (c->bio_split)
1318 bioset_free(c->bio_split);
1319 if (c->fill_iter)
1320 mempool_destroy(c->fill_iter);
1321 if (c->bio_meta)
1322 mempool_destroy(c->bio_meta);
1323 if (c->search)
1324 mempool_destroy(c->search);
1325 kfree(c->devices);
1326
1327 mutex_lock(&bch_register_lock);
1328 list_del(&c->list);
1329 mutex_unlock(&bch_register_lock);
1330
1331 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1332 wake_up(&unregister_wait);
1333
1334 closure_debug_destroy(&c->cl);
1335 kobject_put(&c->kobj);
1336 }
1337
1338 static void cache_set_flush(struct closure *cl)
1339 {
1340 struct cache_set *c = container_of(cl, struct cache_set, caching);
1341 struct cache *ca;
1342 struct btree *b;
1343 unsigned i;
1344
1345 bch_cache_accounting_destroy(&c->accounting);
1346
1347 kobject_put(&c->internal);
1348 kobject_del(&c->kobj);
1349
1350 if (c->gc_thread)
1351 kthread_stop(c->gc_thread);
1352
1353 if (!IS_ERR_OR_NULL(c->root))
1354 list_add(&c->root->list, &c->btree_cache);
1355
1356 /* Should skip this if we're unregistering because of an error */
1357 list_for_each_entry(b, &c->btree_cache, list)
1358 if (btree_node_dirty(b))
1359 bch_btree_node_write(b, NULL);
1360
1361 for_each_cache(ca, c, i)
1362 if (ca->alloc_thread)
1363 kthread_stop(ca->alloc_thread);
1364
1365 closure_return(cl);
1366 }
1367
1368 static void __cache_set_unregister(struct closure *cl)
1369 {
1370 struct cache_set *c = container_of(cl, struct cache_set, caching);
1371 struct cached_dev *dc;
1372 size_t i;
1373
1374 mutex_lock(&bch_register_lock);
1375
1376 for (i = 0; i < c->nr_uuids; i++)
1377 if (c->devices[i]) {
1378 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1379 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1380 dc = container_of(c->devices[i],
1381 struct cached_dev, disk);
1382 bch_cached_dev_detach(dc);
1383 } else {
1384 bcache_device_stop(c->devices[i]);
1385 }
1386 }
1387
1388 mutex_unlock(&bch_register_lock);
1389
1390 continue_at(cl, cache_set_flush, system_wq);
1391 }
1392
1393 void bch_cache_set_stop(struct cache_set *c)
1394 {
1395 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1396 closure_queue(&c->caching);
1397 }
1398
1399 void bch_cache_set_unregister(struct cache_set *c)
1400 {
1401 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1402 bch_cache_set_stop(c);
1403 }
1404
1405 #define alloc_bucket_pages(gfp, c) \
1406 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1407
1408 struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1409 {
1410 int iter_size;
1411 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1412 if (!c)
1413 return NULL;
1414
1415 __module_get(THIS_MODULE);
1416 closure_init(&c->cl, NULL);
1417 set_closure_fn(&c->cl, cache_set_free, system_wq);
1418
1419 closure_init(&c->caching, &c->cl);
1420 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1421
1422 /* Maybe create continue_at_noreturn() and use it here? */
1423 closure_set_stopped(&c->cl);
1424 closure_put(&c->cl);
1425
1426 kobject_init(&c->kobj, &bch_cache_set_ktype);
1427 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1428
1429 bch_cache_accounting_init(&c->accounting, &c->cl);
1430
1431 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1432 c->sb.block_size = sb->block_size;
1433 c->sb.bucket_size = sb->bucket_size;
1434 c->sb.nr_in_set = sb->nr_in_set;
1435 c->sb.last_mount = sb->last_mount;
1436 c->bucket_bits = ilog2(sb->bucket_size);
1437 c->block_bits = ilog2(sb->block_size);
1438 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1439
1440 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1441 if (c->btree_pages > BTREE_MAX_PAGES)
1442 c->btree_pages = max_t(int, c->btree_pages / 4,
1443 BTREE_MAX_PAGES);
1444
1445 c->sort_crit_factor = int_sqrt(c->btree_pages);
1446
1447 closure_init_unlocked(&c->sb_write);
1448 mutex_init(&c->bucket_lock);
1449 init_waitqueue_head(&c->try_wait);
1450 init_waitqueue_head(&c->bucket_wait);
1451 closure_init_unlocked(&c->uuid_write);
1452 mutex_init(&c->sort_lock);
1453
1454 spin_lock_init(&c->sort_time.lock);
1455 spin_lock_init(&c->btree_gc_time.lock);
1456 spin_lock_init(&c->btree_split_time.lock);
1457 spin_lock_init(&c->btree_read_time.lock);
1458 spin_lock_init(&c->try_harder_time.lock);
1459
1460 bch_moving_init_cache_set(c);
1461
1462 INIT_LIST_HEAD(&c->list);
1463 INIT_LIST_HEAD(&c->cached_devs);
1464 INIT_LIST_HEAD(&c->btree_cache);
1465 INIT_LIST_HEAD(&c->btree_cache_freeable);
1466 INIT_LIST_HEAD(&c->btree_cache_freed);
1467 INIT_LIST_HEAD(&c->data_buckets);
1468
1469 c->search = mempool_create_slab_pool(32, bch_search_cache);
1470 if (!c->search)
1471 goto err;
1472
1473 iter_size = (sb->bucket_size / sb->block_size + 1) *
1474 sizeof(struct btree_iter_set);
1475
1476 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1477 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1478 sizeof(struct bbio) + sizeof(struct bio_vec) *
1479 bucket_pages(c))) ||
1480 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
1481 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
1482 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1483 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1484 bch_journal_alloc(c) ||
1485 bch_btree_cache_alloc(c) ||
1486 bch_open_buckets_alloc(c))
1487 goto err;
1488
1489 c->congested_read_threshold_us = 2000;
1490 c->congested_write_threshold_us = 20000;
1491 c->error_limit = 8 << IO_ERROR_SHIFT;
1492
1493 return c;
1494 err:
1495 bch_cache_set_unregister(c);
1496 return NULL;
1497 }
1498
1499 static void run_cache_set(struct cache_set *c)
1500 {
1501 const char *err = "cannot allocate memory";
1502 struct cached_dev *dc, *t;
1503 struct cache *ca;
1504 struct closure cl;
1505 unsigned i;
1506
1507 closure_init_stack(&cl);
1508
1509 for_each_cache(ca, c, i)
1510 c->nbuckets += ca->sb.nbuckets;
1511
1512 if (CACHE_SYNC(&c->sb)) {
1513 LIST_HEAD(journal);
1514 struct bkey *k;
1515 struct jset *j;
1516
1517 err = "cannot allocate memory for journal";
1518 if (bch_journal_read(c, &journal))
1519 goto err;
1520
1521 pr_debug("btree_journal_read() done");
1522
1523 err = "no journal entries found";
1524 if (list_empty(&journal))
1525 goto err;
1526
1527 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1528
1529 err = "IO error reading priorities";
1530 for_each_cache(ca, c, i)
1531 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1532
1533 /*
1534 * If prio_read() fails it'll call cache_set_error and we'll
1535 * tear everything down right away, but if we perhaps checked
1536 * sooner we could avoid journal replay.
1537 */
1538
1539 k = &j->btree_root;
1540
1541 err = "bad btree root";
1542 if (bch_btree_ptr_invalid(c, k))
1543 goto err;
1544
1545 err = "error reading btree root";
1546 c->root = bch_btree_node_get(c, k, j->btree_level, true);
1547 if (IS_ERR_OR_NULL(c->root))
1548 goto err;
1549
1550 list_del_init(&c->root->list);
1551 rw_unlock(true, c->root);
1552
1553 err = uuid_read(c, j, &cl);
1554 if (err)
1555 goto err;
1556
1557 err = "error in recovery";
1558 if (bch_btree_check(c))
1559 goto err;
1560
1561 bch_journal_mark(c, &journal);
1562 bch_btree_gc_finish(c);
1563 pr_debug("btree_check() done");
1564
1565 /*
1566 * bcache_journal_next() can't happen sooner, or
1567 * btree_gc_finish() will give spurious errors about last_gc >
1568 * gc_gen - this is a hack but oh well.
1569 */
1570 bch_journal_next(&c->journal);
1571
1572 err = "error starting allocator thread";
1573 for_each_cache(ca, c, i)
1574 if (bch_cache_allocator_start(ca))
1575 goto err;
1576
1577 /*
1578 * First place it's safe to allocate: btree_check() and
1579 * btree_gc_finish() have to run before we have buckets to
1580 * allocate, and bch_bucket_alloc_set() might cause a journal
1581 * entry to be written so bcache_journal_next() has to be called
1582 * first.
1583 *
1584 * If the uuids were in the old format we have to rewrite them
1585 * before the next journal entry is written:
1586 */
1587 if (j->version < BCACHE_JSET_VERSION_UUID)
1588 __uuid_write(c);
1589
1590 bch_journal_replay(c, &journal);
1591 } else {
1592 pr_notice("invalidating existing data");
1593
1594 for_each_cache(ca, c, i) {
1595 unsigned j;
1596
1597 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1598 2, SB_JOURNAL_BUCKETS);
1599
1600 for (j = 0; j < ca->sb.keys; j++)
1601 ca->sb.d[j] = ca->sb.first_bucket + j;
1602 }
1603
1604 bch_btree_gc_finish(c);
1605
1606 err = "error starting allocator thread";
1607 for_each_cache(ca, c, i)
1608 if (bch_cache_allocator_start(ca))
1609 goto err;
1610
1611 mutex_lock(&c->bucket_lock);
1612 for_each_cache(ca, c, i)
1613 bch_prio_write(ca);
1614 mutex_unlock(&c->bucket_lock);
1615
1616 err = "cannot allocate new UUID bucket";
1617 if (__uuid_write(c))
1618 goto err;
1619
1620 err = "cannot allocate new btree root";
1621 c->root = bch_btree_node_alloc(c, 0, true);
1622 if (IS_ERR_OR_NULL(c->root))
1623 goto err;
1624
1625 bkey_copy_key(&c->root->key, &MAX_KEY);
1626 bch_btree_node_write(c->root, &cl);
1627
1628 bch_btree_set_root(c->root);
1629 rw_unlock(true, c->root);
1630
1631 /*
1632 * We don't want to write the first journal entry until
1633 * everything is set up - fortunately journal entries won't be
1634 * written until the SET_CACHE_SYNC() here:
1635 */
1636 SET_CACHE_SYNC(&c->sb, true);
1637
1638 bch_journal_next(&c->journal);
1639 bch_journal_meta(c, &cl);
1640 }
1641
1642 err = "error starting gc thread";
1643 if (bch_gc_thread_start(c))
1644 goto err;
1645
1646 closure_sync(&cl);
1647 c->sb.last_mount = get_seconds();
1648 bcache_write_super(c);
1649
1650 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1651 bch_cached_dev_attach(dc, c);
1652
1653 flash_devs_run(c);
1654
1655 return;
1656 err:
1657 closure_sync(&cl);
1658 /* XXX: test this, it's broken */
1659 bch_cache_set_error(c, err);
1660 }
1661
1662 static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1663 {
1664 return ca->sb.block_size == c->sb.block_size &&
1665 ca->sb.bucket_size == c->sb.block_size &&
1666 ca->sb.nr_in_set == c->sb.nr_in_set;
1667 }
1668
1669 static const char *register_cache_set(struct cache *ca)
1670 {
1671 char buf[12];
1672 const char *err = "cannot allocate memory";
1673 struct cache_set *c;
1674
1675 list_for_each_entry(c, &bch_cache_sets, list)
1676 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1677 if (c->cache[ca->sb.nr_this_dev])
1678 return "duplicate cache set member";
1679
1680 if (!can_attach_cache(ca, c))
1681 return "cache sb does not match set";
1682
1683 if (!CACHE_SYNC(&ca->sb))
1684 SET_CACHE_SYNC(&c->sb, false);
1685
1686 goto found;
1687 }
1688
1689 c = bch_cache_set_alloc(&ca->sb);
1690 if (!c)
1691 return err;
1692
1693 err = "error creating kobject";
1694 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1695 kobject_add(&c->internal, &c->kobj, "internal"))
1696 goto err;
1697
1698 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1699 goto err;
1700
1701 bch_debug_init_cache_set(c);
1702
1703 list_add(&c->list, &bch_cache_sets);
1704 found:
1705 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1706 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1707 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1708 goto err;
1709
1710 if (ca->sb.seq > c->sb.seq) {
1711 c->sb.version = ca->sb.version;
1712 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1713 c->sb.flags = ca->sb.flags;
1714 c->sb.seq = ca->sb.seq;
1715 pr_debug("set version = %llu", c->sb.version);
1716 }
1717
1718 ca->set = c;
1719 ca->set->cache[ca->sb.nr_this_dev] = ca;
1720 c->cache_by_alloc[c->caches_loaded++] = ca;
1721
1722 if (c->caches_loaded == c->sb.nr_in_set)
1723 run_cache_set(c);
1724
1725 return NULL;
1726 err:
1727 bch_cache_set_unregister(c);
1728 return err;
1729 }
1730
1731 /* Cache device */
1732
1733 void bch_cache_release(struct kobject *kobj)
1734 {
1735 struct cache *ca = container_of(kobj, struct cache, kobj);
1736
1737 if (ca->set)
1738 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1739
1740 bio_split_pool_free(&ca->bio_split_hook);
1741
1742 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1743 kfree(ca->prio_buckets);
1744 vfree(ca->buckets);
1745
1746 free_heap(&ca->heap);
1747 free_fifo(&ca->unused);
1748 free_fifo(&ca->free_inc);
1749 free_fifo(&ca->free);
1750
1751 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1752 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1753
1754 if (!IS_ERR_OR_NULL(ca->bdev)) {
1755 blk_sync_queue(bdev_get_queue(ca->bdev));
1756 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1757 }
1758
1759 kfree(ca);
1760 module_put(THIS_MODULE);
1761 }
1762
1763 static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1764 {
1765 size_t free;
1766 struct bucket *b;
1767
1768 __module_get(THIS_MODULE);
1769 kobject_init(&ca->kobj, &bch_cache_ktype);
1770
1771 bio_init(&ca->journal.bio);
1772 ca->journal.bio.bi_max_vecs = 8;
1773 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1774
1775 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1776 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1777
1778 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1779 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1780 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1781 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
1782 !(ca->buckets = vzalloc(sizeof(struct bucket) *
1783 ca->sb.nbuckets)) ||
1784 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1785 2, GFP_KERNEL)) ||
1786 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
1787 bio_split_pool_init(&ca->bio_split_hook))
1788 return -ENOMEM;
1789
1790 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1791
1792 for_each_bucket(b, ca)
1793 atomic_set(&b->pin, 0);
1794
1795 if (bch_cache_allocator_init(ca))
1796 goto err;
1797
1798 return 0;
1799 err:
1800 kobject_put(&ca->kobj);
1801 return -ENOMEM;
1802 }
1803
1804 static void register_cache(struct cache_sb *sb, struct page *sb_page,
1805 struct block_device *bdev, struct cache *ca)
1806 {
1807 char name[BDEVNAME_SIZE];
1808 const char *err = "cannot allocate memory";
1809
1810 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
1811 ca->bdev = bdev;
1812 ca->bdev->bd_holder = ca;
1813
1814 bio_init(&ca->sb_bio);
1815 ca->sb_bio.bi_max_vecs = 1;
1816 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1817 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1818 get_page(sb_page);
1819
1820 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1821 ca->discard = CACHE_DISCARD(&ca->sb);
1822
1823 if (cache_alloc(sb, ca) != 0)
1824 goto err;
1825
1826 err = "error creating kobject";
1827 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1828 goto err;
1829
1830 err = register_cache_set(ca);
1831 if (err)
1832 goto err;
1833
1834 pr_info("registered cache device %s", bdevname(bdev, name));
1835 return;
1836 err:
1837 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1838 kobject_put(&ca->kobj);
1839 }
1840
1841 /* Global interfaces/init */
1842
1843 static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1844 const char *, size_t);
1845
1846 kobj_attribute_write(register, register_bcache);
1847 kobj_attribute_write(register_quiet, register_bcache);
1848
1849 static bool bch_is_open_backing(struct block_device *bdev) {
1850 struct cache_set *c, *tc;
1851 struct cached_dev *dc, *t;
1852
1853 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1854 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1855 if (dc->bdev == bdev)
1856 return true;
1857 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1858 if (dc->bdev == bdev)
1859 return true;
1860 return false;
1861 }
1862
1863 static bool bch_is_open_cache(struct block_device *bdev) {
1864 struct cache_set *c, *tc;
1865 struct cache *ca;
1866 unsigned i;
1867
1868 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1869 for_each_cache(ca, c, i)
1870 if (ca->bdev == bdev)
1871 return true;
1872 return false;
1873 }
1874
1875 static bool bch_is_open(struct block_device *bdev) {
1876 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1877 }
1878
1879 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1880 const char *buffer, size_t size)
1881 {
1882 ssize_t ret = size;
1883 const char *err = "cannot allocate memory";
1884 char *path = NULL;
1885 struct cache_sb *sb = NULL;
1886 struct block_device *bdev = NULL;
1887 struct page *sb_page = NULL;
1888
1889 if (!try_module_get(THIS_MODULE))
1890 return -EBUSY;
1891
1892 mutex_lock(&bch_register_lock);
1893
1894 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1895 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1896 goto err;
1897
1898 err = "failed to open device";
1899 bdev = blkdev_get_by_path(strim(path),
1900 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1901 sb);
1902 if (IS_ERR(bdev)) {
1903 if (bdev == ERR_PTR(-EBUSY)) {
1904 bdev = lookup_bdev(strim(path));
1905 if (!IS_ERR(bdev) && bch_is_open(bdev))
1906 err = "device already registered";
1907 else
1908 err = "device busy";
1909 }
1910 goto err;
1911 }
1912
1913 err = "failed to set blocksize";
1914 if (set_blocksize(bdev, 4096))
1915 goto err_close;
1916
1917 err = read_super(sb, bdev, &sb_page);
1918 if (err)
1919 goto err_close;
1920
1921 if (SB_IS_BDEV(sb)) {
1922 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1923 if (!dc)
1924 goto err_close;
1925
1926 register_bdev(sb, sb_page, bdev, dc);
1927 } else {
1928 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1929 if (!ca)
1930 goto err_close;
1931
1932 register_cache(sb, sb_page, bdev, ca);
1933 }
1934 out:
1935 if (sb_page)
1936 put_page(sb_page);
1937 kfree(sb);
1938 kfree(path);
1939 mutex_unlock(&bch_register_lock);
1940 module_put(THIS_MODULE);
1941 return ret;
1942
1943 err_close:
1944 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1945 err:
1946 if (attr != &ksysfs_register_quiet)
1947 pr_info("error opening %s: %s", path, err);
1948 ret = -EINVAL;
1949 goto out;
1950 }
1951
1952 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1953 {
1954 if (code == SYS_DOWN ||
1955 code == SYS_HALT ||
1956 code == SYS_POWER_OFF) {
1957 DEFINE_WAIT(wait);
1958 unsigned long start = jiffies;
1959 bool stopped = false;
1960
1961 struct cache_set *c, *tc;
1962 struct cached_dev *dc, *tdc;
1963
1964 mutex_lock(&bch_register_lock);
1965
1966 if (list_empty(&bch_cache_sets) &&
1967 list_empty(&uncached_devices))
1968 goto out;
1969
1970 pr_info("Stopping all devices:");
1971
1972 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1973 bch_cache_set_stop(c);
1974
1975 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1976 bcache_device_stop(&dc->disk);
1977
1978 /* What's a condition variable? */
1979 while (1) {
1980 long timeout = start + 2 * HZ - jiffies;
1981
1982 stopped = list_empty(&bch_cache_sets) &&
1983 list_empty(&uncached_devices);
1984
1985 if (timeout < 0 || stopped)
1986 break;
1987
1988 prepare_to_wait(&unregister_wait, &wait,
1989 TASK_UNINTERRUPTIBLE);
1990
1991 mutex_unlock(&bch_register_lock);
1992 schedule_timeout(timeout);
1993 mutex_lock(&bch_register_lock);
1994 }
1995
1996 finish_wait(&unregister_wait, &wait);
1997
1998 if (stopped)
1999 pr_info("All devices stopped");
2000 else
2001 pr_notice("Timeout waiting for devices to be closed");
2002 out:
2003 mutex_unlock(&bch_register_lock);
2004 }
2005
2006 return NOTIFY_DONE;
2007 }
2008
2009 static struct notifier_block reboot = {
2010 .notifier_call = bcache_reboot,
2011 .priority = INT_MAX, /* before any real devices */
2012 };
2013
2014 static void bcache_exit(void)
2015 {
2016 bch_debug_exit();
2017 bch_request_exit();
2018 bch_btree_exit();
2019 if (bcache_kobj)
2020 kobject_put(bcache_kobj);
2021 if (bcache_wq)
2022 destroy_workqueue(bcache_wq);
2023 unregister_blkdev(bcache_major, "bcache");
2024 unregister_reboot_notifier(&reboot);
2025 }
2026
2027 static int __init bcache_init(void)
2028 {
2029 static const struct attribute *files[] = {
2030 &ksysfs_register.attr,
2031 &ksysfs_register_quiet.attr,
2032 NULL
2033 };
2034
2035 mutex_init(&bch_register_lock);
2036 init_waitqueue_head(&unregister_wait);
2037 register_reboot_notifier(&reboot);
2038 closure_debug_init();
2039
2040 bcache_major = register_blkdev(0, "bcache");
2041 if (bcache_major < 0)
2042 return bcache_major;
2043
2044 if (!(bcache_wq = create_workqueue("bcache")) ||
2045 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2046 sysfs_create_files(bcache_kobj, files) ||
2047 bch_btree_init() ||
2048 bch_request_init() ||
2049 bch_debug_init(bcache_kobj))
2050 goto err;
2051
2052 return 0;
2053 err:
2054 bcache_exit();
2055 return -ENOMEM;
2056 }
2057
2058 module_exit(bcache_exit);
2059 module_init(bcache_init);