blob: cfac75d0644856e778bedf12026ce469ad2bd5c7 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * bcache setup/teardown code, and some metadata io - read a superblock and
4 * figure out what to do with it.
5 *
6 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
7 * Copyright 2012 Google, Inc.
8 */
9
10#include "bcache.h"
11#include "btree.h"
12#include "debug.h"
13#include "extents.h"
14#include "request.h"
15#include "writeback.h"
16
17#include <linux/blkdev.h>
18#include <linux/buffer_head.h>
19#include <linux/debugfs.h>
20#include <linux/genhd.h>
21#include <linux/idr.h>
22#include <linux/kthread.h>
23#include <linux/module.h>
24#include <linux/random.h>
25#include <linux/reboot.h>
26#include <linux/sysfs.h>
27
28unsigned int bch_cutoff_writeback;
29unsigned int bch_cutoff_writeback_sync;
30
31static const char bcache_magic[] = {
32 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
33 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
34};
35
36static const char invalid_uuid[] = {
37 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
38 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
39};
40
41static struct kobject *bcache_kobj;
42struct mutex bch_register_lock;
43bool bcache_is_reboot;
44LIST_HEAD(bch_cache_sets);
45static LIST_HEAD(uncached_devices);
46
47static int bcache_major;
48static DEFINE_IDA(bcache_device_idx);
49static wait_queue_head_t unregister_wait;
50struct workqueue_struct *bcache_wq;
51struct workqueue_struct *bch_flush_wq;
52struct workqueue_struct *bch_journal_wq;
53
54
55#define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
56/* limitation of partitions number on single bcache device */
57#define BCACHE_MINORS 128
58/* limitation of bcache devices number on single system */
59#define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS)
60
61/* Superblock */
62
63static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
64 struct page **res)
65{
66 const char *err;
67 struct cache_sb *s;
68 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
69 unsigned int i;
70
71 if (!bh)
72 return "IO error";
73
74 s = (struct cache_sb *) bh->b_data;
75
76 sb->offset = le64_to_cpu(s->offset);
77 sb->version = le64_to_cpu(s->version);
78
79 memcpy(sb->magic, s->magic, 16);
80 memcpy(sb->uuid, s->uuid, 16);
81 memcpy(sb->set_uuid, s->set_uuid, 16);
82 memcpy(sb->label, s->label, SB_LABEL_SIZE);
83
84 sb->flags = le64_to_cpu(s->flags);
85 sb->seq = le64_to_cpu(s->seq);
86 sb->last_mount = le32_to_cpu(s->last_mount);
87 sb->first_bucket = le16_to_cpu(s->first_bucket);
88 sb->keys = le16_to_cpu(s->keys);
89
90 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
91 sb->d[i] = le64_to_cpu(s->d[i]);
92
93 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
94 sb->version, sb->flags, sb->seq, sb->keys);
95
96 err = "Not a bcache superblock";
97 if (sb->offset != SB_SECTOR)
98 goto err;
99
100 if (memcmp(sb->magic, bcache_magic, 16))
101 goto err;
102
103 err = "Too many journal buckets";
104 if (sb->keys > SB_JOURNAL_BUCKETS)
105 goto err;
106
107 err = "Bad checksum";
108 if (s->csum != csum_set(s))
109 goto err;
110
111 err = "Bad UUID";
112 if (bch_is_zero(sb->uuid, 16))
113 goto err;
114
115 sb->block_size = le16_to_cpu(s->block_size);
116
117 err = "Superblock block size smaller than device block size";
118 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
119 goto err;
120
121 switch (sb->version) {
122 case BCACHE_SB_VERSION_BDEV:
123 sb->data_offset = BDEV_DATA_START_DEFAULT;
124 break;
125 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
126 sb->data_offset = le64_to_cpu(s->data_offset);
127
128 err = "Bad data offset";
129 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
130 goto err;
131
132 break;
133 case BCACHE_SB_VERSION_CDEV:
134 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
135 sb->nbuckets = le64_to_cpu(s->nbuckets);
136 sb->bucket_size = le16_to_cpu(s->bucket_size);
137
138 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
139 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
140
141 err = "Too many buckets";
142 if (sb->nbuckets > LONG_MAX)
143 goto err;
144
145 err = "Not enough buckets";
146 if (sb->nbuckets < 1 << 7)
147 goto err;
148
149 err = "Bad block/bucket size";
150 if (!is_power_of_2(sb->block_size) ||
151 sb->block_size > PAGE_SECTORS ||
152 !is_power_of_2(sb->bucket_size) ||
153 sb->bucket_size < PAGE_SECTORS)
154 goto err;
155
156 err = "Invalid superblock: device too small";
157 if (get_capacity(bdev->bd_disk) <
158 sb->bucket_size * sb->nbuckets)
159 goto err;
160
161 err = "Bad UUID";
162 if (bch_is_zero(sb->set_uuid, 16))
163 goto err;
164
165 err = "Bad cache device number in set";
166 if (!sb->nr_in_set ||
167 sb->nr_in_set <= sb->nr_this_dev ||
168 sb->nr_in_set > MAX_CACHES_PER_SET)
169 goto err;
170
171 err = "Journal buckets not sequential";
172 for (i = 0; i < sb->keys; i++)
173 if (sb->d[i] != sb->first_bucket + i)
174 goto err;
175
176 err = "Too many journal buckets";
177 if (sb->first_bucket + sb->keys > sb->nbuckets)
178 goto err;
179
180 err = "Invalid superblock: first bucket comes before end of super";
181 if (sb->first_bucket * sb->bucket_size < 16)
182 goto err;
183
184 break;
185 default:
186 err = "Unsupported superblock version";
187 goto err;
188 }
189
190 sb->last_mount = (u32)ktime_get_real_seconds();
191 err = NULL;
192
193 get_page(bh->b_page);
194 *res = bh->b_page;
195err:
196 put_bh(bh);
197 return err;
198}
199
200static void write_bdev_super_endio(struct bio *bio)
201{
202 struct cached_dev *dc = bio->bi_private;
203
204 if (bio->bi_status)
205 bch_count_backing_io_errors(dc, bio);
206
207 closure_put(&dc->sb_write);
208}
209
210static void __write_super(struct cache_sb *sb, struct bio *bio)
211{
212 struct cache_sb *out = page_address(bio_first_page_all(bio));
213 unsigned int i;
214
215 bio->bi_iter.bi_sector = SB_SECTOR;
216 bio->bi_iter.bi_size = SB_SIZE;
217 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
218 bch_bio_map(bio, NULL);
219
220 out->offset = cpu_to_le64(sb->offset);
221 out->version = cpu_to_le64(sb->version);
222
223 memcpy(out->uuid, sb->uuid, 16);
224 memcpy(out->set_uuid, sb->set_uuid, 16);
225 memcpy(out->label, sb->label, SB_LABEL_SIZE);
226
227 out->flags = cpu_to_le64(sb->flags);
228 out->seq = cpu_to_le64(sb->seq);
229
230 out->last_mount = cpu_to_le32(sb->last_mount);
231 out->first_bucket = cpu_to_le16(sb->first_bucket);
232 out->keys = cpu_to_le16(sb->keys);
233
234 for (i = 0; i < sb->keys; i++)
235 out->d[i] = cpu_to_le64(sb->d[i]);
236
237 out->csum = csum_set(out);
238
239 pr_debug("ver %llu, flags %llu, seq %llu",
240 sb->version, sb->flags, sb->seq);
241
242 submit_bio(bio);
243}
244
245static void bch_write_bdev_super_unlock(struct closure *cl)
246{
247 struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
248
249 up(&dc->sb_write_mutex);
250}
251
252void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
253{
254 struct closure *cl = &dc->sb_write;
255 struct bio *bio = &dc->sb_bio;
256
257 down(&dc->sb_write_mutex);
258 closure_init(cl, parent);
259
260 bio_reset(bio);
261 bio_set_dev(bio, dc->bdev);
262 bio->bi_end_io = write_bdev_super_endio;
263 bio->bi_private = dc;
264
265 closure_get(cl);
266 /* I/O request sent to backing device */
267 __write_super(&dc->sb, bio);
268
269 closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
270}
271
272static void write_super_endio(struct bio *bio)
273{
274 struct cache *ca = bio->bi_private;
275
276 /* is_read = 0 */
277 bch_count_io_errors(ca, bio->bi_status, 0,
278 "writing superblock");
279 closure_put(&ca->set->sb_write);
280}
281
282static void bcache_write_super_unlock(struct closure *cl)
283{
284 struct cache_set *c = container_of(cl, struct cache_set, sb_write);
285
286 up(&c->sb_write_mutex);
287}
288
289void bcache_write_super(struct cache_set *c)
290{
291 struct closure *cl = &c->sb_write;
292 struct cache *ca;
293 unsigned int i;
294
295 down(&c->sb_write_mutex);
296 closure_init(cl, &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_set_dev(bio, 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_with_destructor(cl, bcache_write_super_unlock);
319}
320
321/* UUID io */
322
323static void uuid_endio(struct bio *bio)
324{
325 struct closure *cl = bio->bi_private;
326 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
327
328 cache_set_err_on(bio->bi_status, c, "accessing uuids");
329 bch_bbio_free(bio, c);
330 closure_put(cl);
331}
332
333static void uuid_io_unlock(struct closure *cl)
334{
335 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
336
337 up(&c->uuid_write_mutex);
338}
339
340static void uuid_io(struct cache_set *c, int op, unsigned long op_flags,
341 struct bkey *k, struct closure *parent)
342{
343 struct closure *cl = &c->uuid_write;
344 struct uuid_entry *u;
345 unsigned int i;
346 char buf[80];
347
348 BUG_ON(!parent);
349 down(&c->uuid_write_mutex);
350 closure_init(cl, parent);
351
352 for (i = 0; i < KEY_PTRS(k); i++) {
353 struct bio *bio = bch_bbio_alloc(c);
354
355 bio->bi_opf = REQ_SYNC | REQ_META | op_flags;
356 bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
357
358 bio->bi_end_io = uuid_endio;
359 bio->bi_private = cl;
360 bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
361 bch_bio_map(bio, c->uuids);
362
363 bch_submit_bbio(bio, c, k, i);
364
365 if (op != REQ_OP_WRITE)
366 break;
367 }
368
369 bch_extent_to_text(buf, sizeof(buf), k);
370 pr_debug("%s UUIDs at %s", op == REQ_OP_WRITE ? "wrote" : "read", buf);
371
372 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
373 if (!bch_is_zero(u->uuid, 16))
374 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
375 u - c->uuids, u->uuid, u->label,
376 u->first_reg, u->last_reg, u->invalidated);
377
378 closure_return_with_destructor(cl, uuid_io_unlock);
379}
380
381static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
382{
383 struct bkey *k = &j->uuid_bucket;
384
385 if (__bch_btree_ptr_invalid(c, k))
386 return "bad uuid pointer";
387
388 bkey_copy(&c->uuid_bucket, k);
389 uuid_io(c, REQ_OP_READ, 0, k, cl);
390
391 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
392 struct uuid_entry_v0 *u0 = (void *) c->uuids;
393 struct uuid_entry *u1 = (void *) c->uuids;
394 int i;
395
396 closure_sync(cl);
397
398 /*
399 * Since the new uuid entry is bigger than the old, we have to
400 * convert starting at the highest memory address and work down
401 * in order to do it in place
402 */
403
404 for (i = c->nr_uuids - 1;
405 i >= 0;
406 --i) {
407 memcpy(u1[i].uuid, u0[i].uuid, 16);
408 memcpy(u1[i].label, u0[i].label, 32);
409
410 u1[i].first_reg = u0[i].first_reg;
411 u1[i].last_reg = u0[i].last_reg;
412 u1[i].invalidated = u0[i].invalidated;
413
414 u1[i].flags = 0;
415 u1[i].sectors = 0;
416 }
417 }
418
419 return NULL;
420}
421
422static int __uuid_write(struct cache_set *c)
423{
424 BKEY_PADDED(key) k;
425 struct closure cl;
426 struct cache *ca;
427
428 closure_init_stack(&cl);
429 lockdep_assert_held(&bch_register_lock);
430
431 if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, true))
432 return 1;
433
434 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
435 uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl);
436 closure_sync(&cl);
437
438 /* Only one bucket used for uuid write */
439 ca = PTR_CACHE(c, &k.key, 0);
440 atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written);
441
442 bkey_copy(&c->uuid_bucket, &k.key);
443 bkey_put(c, &k.key);
444 return 0;
445}
446
447int bch_uuid_write(struct cache_set *c)
448{
449 int ret = __uuid_write(c);
450
451 if (!ret)
452 bch_journal_meta(c, NULL);
453
454 return ret;
455}
456
457static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
458{
459 struct uuid_entry *u;
460
461 for (u = c->uuids;
462 u < c->uuids + c->nr_uuids; u++)
463 if (!memcmp(u->uuid, uuid, 16))
464 return u;
465
466 return NULL;
467}
468
469static struct uuid_entry *uuid_find_empty(struct cache_set *c)
470{
471 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
472
473 return uuid_find(c, zero_uuid);
474}
475
476/*
477 * Bucket priorities/gens:
478 *
479 * For each bucket, we store on disk its
480 * 8 bit gen
481 * 16 bit priority
482 *
483 * See alloc.c for an explanation of the gen. The priority is used to implement
484 * lru (and in the future other) cache replacement policies; for most purposes
485 * it's just an opaque integer.
486 *
487 * The gens and the priorities don't have a whole lot to do with each other, and
488 * it's actually the gens that must be written out at specific times - it's no
489 * big deal if the priorities don't get written, if we lose them we just reuse
490 * buckets in suboptimal order.
491 *
492 * On disk they're stored in a packed array, and in as many buckets are required
493 * to fit them all. The buckets we use to store them form a list; the journal
494 * header points to the first bucket, the first bucket points to the second
495 * bucket, et cetera.
496 *
497 * This code is used by the allocation code; periodically (whenever it runs out
498 * of buckets to allocate from) the allocation code will invalidate some
499 * buckets, but it can't use those buckets until their new gens are safely on
500 * disk.
501 */
502
503static void prio_endio(struct bio *bio)
504{
505 struct cache *ca = bio->bi_private;
506
507 cache_set_err_on(bio->bi_status, ca->set, "accessing priorities");
508 bch_bbio_free(bio, ca->set);
509 closure_put(&ca->prio);
510}
511
512static void prio_io(struct cache *ca, uint64_t bucket, int op,
513 unsigned long op_flags)
514{
515 struct closure *cl = &ca->prio;
516 struct bio *bio = bch_bbio_alloc(ca->set);
517
518 closure_init_stack(cl);
519
520 bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size;
521 bio_set_dev(bio, ca->bdev);
522 bio->bi_iter.bi_size = bucket_bytes(ca);
523
524 bio->bi_end_io = prio_endio;
525 bio->bi_private = ca;
526 bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
527 bch_bio_map(bio, ca->disk_buckets);
528
529 closure_bio_submit(ca->set, bio, &ca->prio);
530 closure_sync(cl);
531}
532
533int bch_prio_write(struct cache *ca, bool wait)
534{
535 int i;
536 struct bucket *b;
537 struct closure cl;
538
539 pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu",
540 fifo_used(&ca->free[RESERVE_PRIO]),
541 fifo_used(&ca->free[RESERVE_NONE]),
542 fifo_used(&ca->free_inc));
543
544 /*
545 * Pre-check if there are enough free buckets. In the non-blocking
546 * scenario it's better to fail early rather than starting to allocate
547 * buckets and do a cleanup later in case of failure.
548 */
549 if (!wait) {
550 size_t avail = fifo_used(&ca->free[RESERVE_PRIO]) +
551 fifo_used(&ca->free[RESERVE_NONE]);
552 if (prio_buckets(ca) > avail)
553 return -ENOMEM;
554 }
555
556 closure_init_stack(&cl);
557
558 lockdep_assert_held(&ca->set->bucket_lock);
559
560 ca->disk_buckets->seq++;
561
562 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
563 &ca->meta_sectors_written);
564
565 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
566 long bucket;
567 struct prio_set *p = ca->disk_buckets;
568 struct bucket_disk *d = p->data;
569 struct bucket_disk *end = d + prios_per_bucket(ca);
570
571 for (b = ca->buckets + i * prios_per_bucket(ca);
572 b < ca->buckets + ca->sb.nbuckets && d < end;
573 b++, d++) {
574 d->prio = cpu_to_le16(b->prio);
575 d->gen = b->gen;
576 }
577
578 p->next_bucket = ca->prio_buckets[i + 1];
579 p->magic = pset_magic(&ca->sb);
580 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
581
582 bucket = bch_bucket_alloc(ca, RESERVE_PRIO, wait);
583 BUG_ON(bucket == -1);
584
585 mutex_unlock(&ca->set->bucket_lock);
586 prio_io(ca, bucket, REQ_OP_WRITE, 0);
587 mutex_lock(&ca->set->bucket_lock);
588
589 ca->prio_buckets[i] = bucket;
590 atomic_dec_bug(&ca->buckets[bucket].pin);
591 }
592
593 mutex_unlock(&ca->set->bucket_lock);
594
595 bch_journal_meta(ca->set, &cl);
596 closure_sync(&cl);
597
598 mutex_lock(&ca->set->bucket_lock);
599
600 /*
601 * Don't want the old priorities to get garbage collected until after we
602 * finish writing the new ones, and they're journalled
603 */
604 for (i = 0; i < prio_buckets(ca); i++) {
605 if (ca->prio_last_buckets[i])
606 __bch_bucket_free(ca,
607 &ca->buckets[ca->prio_last_buckets[i]]);
608
609 ca->prio_last_buckets[i] = ca->prio_buckets[i];
610 }
611 return 0;
612}
613
614static void prio_read(struct cache *ca, uint64_t bucket)
615{
616 struct prio_set *p = ca->disk_buckets;
617 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
618 struct bucket *b;
619 unsigned int bucket_nr = 0;
620
621 for (b = ca->buckets;
622 b < ca->buckets + ca->sb.nbuckets;
623 b++, d++) {
624 if (d == end) {
625 ca->prio_buckets[bucket_nr] = bucket;
626 ca->prio_last_buckets[bucket_nr] = bucket;
627 bucket_nr++;
628
629 prio_io(ca, bucket, REQ_OP_READ, 0);
630
631 if (p->csum !=
632 bch_crc64(&p->magic, bucket_bytes(ca) - 8))
633 pr_warn("bad csum reading priorities");
634
635 if (p->magic != pset_magic(&ca->sb))
636 pr_warn("bad magic reading priorities");
637
638 bucket = p->next_bucket;
639 d = p->data;
640 }
641
642 b->prio = le16_to_cpu(d->prio);
643 b->gen = b->last_gc = d->gen;
644 }
645}
646
647/* Bcache device */
648
649static int open_dev(struct block_device *b, fmode_t mode)
650{
651 struct bcache_device *d = b->bd_disk->private_data;
652
653 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
654 return -ENXIO;
655
656 closure_get(&d->cl);
657 return 0;
658}
659
660static void release_dev(struct gendisk *b, fmode_t mode)
661{
662 struct bcache_device *d = b->private_data;
663
664 closure_put(&d->cl);
665}
666
667static int ioctl_dev(struct block_device *b, fmode_t mode,
668 unsigned int cmd, unsigned long arg)
669{
670 struct bcache_device *d = b->bd_disk->private_data;
671
672 return d->ioctl(d, mode, cmd, arg);
673}
674
675static const struct block_device_operations bcache_ops = {
676 .open = open_dev,
677 .release = release_dev,
678 .ioctl = ioctl_dev,
679 .owner = THIS_MODULE,
680};
681
682void bcache_device_stop(struct bcache_device *d)
683{
684 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
685 /*
686 * closure_fn set to
687 * - cached device: cached_dev_flush()
688 * - flash dev: flash_dev_flush()
689 */
690 closure_queue(&d->cl);
691}
692
693static void bcache_device_unlink(struct bcache_device *d)
694{
695 lockdep_assert_held(&bch_register_lock);
696
697 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
698 unsigned int i;
699 struct cache *ca;
700
701 sysfs_remove_link(&d->c->kobj, d->name);
702 sysfs_remove_link(&d->kobj, "cache");
703
704 for_each_cache(ca, d->c, i)
705 bd_unlink_disk_holder(ca->bdev, d->disk);
706 }
707}
708
709static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
710 const char *name)
711{
712 unsigned int i;
713 struct cache *ca;
714 int ret;
715
716 for_each_cache(ca, d->c, i)
717 bd_link_disk_holder(ca->bdev, d->disk);
718
719 snprintf(d->name, BCACHEDEVNAME_SIZE,
720 "%s%u", name, d->id);
721
722 ret = sysfs_create_link(&d->kobj, &c->kobj, "cache");
723 if (ret < 0)
724 pr_err("Couldn't create device -> cache set symlink");
725
726 ret = sysfs_create_link(&c->kobj, &d->kobj, d->name);
727 if (ret < 0)
728 pr_err("Couldn't create cache set -> device symlink");
729
730 clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags);
731}
732
733static void bcache_device_detach(struct bcache_device *d)
734{
735 lockdep_assert_held(&bch_register_lock);
736
737 atomic_dec(&d->c->attached_dev_nr);
738
739 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
740 struct uuid_entry *u = d->c->uuids + d->id;
741
742 SET_UUID_FLASH_ONLY(u, 0);
743 memcpy(u->uuid, invalid_uuid, 16);
744 u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
745 bch_uuid_write(d->c);
746 }
747
748 bcache_device_unlink(d);
749
750 d->c->devices[d->id] = NULL;
751 closure_put(&d->c->caching);
752 d->c = NULL;
753}
754
755static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
756 unsigned int id)
757{
758 d->id = id;
759 d->c = c;
760 c->devices[id] = d;
761
762 if (id >= c->devices_max_used)
763 c->devices_max_used = id + 1;
764
765 closure_get(&c->caching);
766}
767
768static inline int first_minor_to_idx(int first_minor)
769{
770 return (first_minor/BCACHE_MINORS);
771}
772
773static inline int idx_to_first_minor(int idx)
774{
775 return (idx * BCACHE_MINORS);
776}
777
778static void bcache_device_free(struct bcache_device *d)
779{
780 struct gendisk *disk = d->disk;
781
782 lockdep_assert_held(&bch_register_lock);
783
784 if (disk)
785 pr_info("%s stopped", disk->disk_name);
786 else
787 pr_err("bcache device (NULL gendisk) stopped");
788
789 if (d->c)
790 bcache_device_detach(d);
791
792 if (disk) {
793 bool disk_added = (disk->flags & GENHD_FL_UP) != 0;
794
795 if (disk_added)
796 del_gendisk(disk);
797
798 if (disk->queue)
799 blk_cleanup_queue(disk->queue);
800
801 ida_simple_remove(&bcache_device_idx,
802 first_minor_to_idx(disk->first_minor));
803 if (disk_added)
804 put_disk(disk);
805 }
806
807 bioset_exit(&d->bio_split);
808 kvfree(d->full_dirty_stripes);
809 kvfree(d->stripe_sectors_dirty);
810
811 closure_debug_destroy(&d->cl);
812}
813
814static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
815 sector_t sectors)
816{
817 struct request_queue *q;
818 const size_t max_stripes = min_t(size_t, INT_MAX,
819 SIZE_MAX / sizeof(atomic_t));
820 uint64_t n;
821 int idx;
822
823 if (!d->stripe_size)
824 d->stripe_size = 1 << 31;
825 else if (d->stripe_size < BCH_MIN_STRIPE_SZ)
826 d->stripe_size = roundup(BCH_MIN_STRIPE_SZ, d->stripe_size);
827
828 n = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
829 if (!n || n > max_stripes) {
830 pr_err("nr_stripes too large or invalid: %llu (start sector beyond end of disk?)\n",
831 n);
832 return -ENOMEM;
833 }
834 d->nr_stripes = n;
835
836 n = d->nr_stripes * sizeof(atomic_t);
837 d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL);
838 if (!d->stripe_sectors_dirty)
839 return -ENOMEM;
840
841 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
842 d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL);
843 if (!d->full_dirty_stripes)
844 goto out_free_stripe_sectors_dirty;
845
846 idx = ida_simple_get(&bcache_device_idx, 0,
847 BCACHE_DEVICE_IDX_MAX, GFP_KERNEL);
848 if (idx < 0)
849 goto out_free_full_dirty_stripes;
850
851 if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio),
852 BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER))
853 goto out_ida_remove;
854
855 d->disk = alloc_disk(BCACHE_MINORS);
856 if (!d->disk)
857 goto out_bioset_exit;
858
859 set_capacity(d->disk, sectors);
860 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx);
861
862 d->disk->major = bcache_major;
863 d->disk->first_minor = idx_to_first_minor(idx);
864 d->disk->fops = &bcache_ops;
865 d->disk->private_data = d;
866
867 q = blk_alloc_queue(GFP_KERNEL);
868 if (!q)
869 return -ENOMEM;
870
871 blk_queue_make_request(q, NULL);
872 d->disk->queue = q;
873 q->queuedata = d;
874 q->backing_dev_info->congested_data = d;
875 q->limits.max_hw_sectors = UINT_MAX;
876 q->limits.max_sectors = UINT_MAX;
877 q->limits.max_segment_size = UINT_MAX;
878 q->limits.max_segments = BIO_MAX_PAGES;
879 blk_queue_max_discard_sectors(q, UINT_MAX);
880 q->limits.discard_granularity = 512;
881 q->limits.io_min = block_size;
882 q->limits.logical_block_size = block_size;
883 q->limits.physical_block_size = block_size;
884 blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue);
885 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue);
886 blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue);
887
888 blk_queue_write_cache(q, true, true);
889
890 return 0;
891
892out_bioset_exit:
893 bioset_exit(&d->bio_split);
894out_ida_remove:
895 ida_simple_remove(&bcache_device_idx, idx);
896out_free_full_dirty_stripes:
897 kvfree(d->full_dirty_stripes);
898out_free_stripe_sectors_dirty:
899 kvfree(d->stripe_sectors_dirty);
900 return -ENOMEM;
901
902}
903
904/* Cached device */
905
906static void calc_cached_dev_sectors(struct cache_set *c)
907{
908 uint64_t sectors = 0;
909 struct cached_dev *dc;
910
911 list_for_each_entry(dc, &c->cached_devs, list)
912 sectors += bdev_sectors(dc->bdev);
913
914 c->cached_dev_sectors = sectors;
915}
916
917#define BACKING_DEV_OFFLINE_TIMEOUT 5
918static int cached_dev_status_update(void *arg)
919{
920 struct cached_dev *dc = arg;
921 struct request_queue *q;
922
923 /*
924 * If this delayed worker is stopping outside, directly quit here.
925 * dc->io_disable might be set via sysfs interface, so check it
926 * here too.
927 */
928 while (!kthread_should_stop() && !dc->io_disable) {
929 q = bdev_get_queue(dc->bdev);
930 if (blk_queue_dying(q))
931 dc->offline_seconds++;
932 else
933 dc->offline_seconds = 0;
934
935 if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) {
936 pr_err("%s: device offline for %d seconds",
937 dc->backing_dev_name,
938 BACKING_DEV_OFFLINE_TIMEOUT);
939 pr_err("%s: disable I/O request due to backing "
940 "device offline", dc->disk.name);
941 dc->io_disable = true;
942 /* let others know earlier that io_disable is true */
943 smp_mb();
944 bcache_device_stop(&dc->disk);
945 break;
946 }
947 schedule_timeout_interruptible(HZ);
948 }
949
950 wait_for_kthread_stop();
951 return 0;
952}
953
954
955int bch_cached_dev_run(struct cached_dev *dc)
956{
957 struct bcache_device *d = &dc->disk;
958 char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL);
959 char *env[] = {
960 "DRIVER=bcache",
961 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
962 kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""),
963 NULL,
964 };
965
966 if (dc->io_disable) {
967 pr_err("I/O disabled on cached dev %s",
968 dc->backing_dev_name);
969 kfree(env[1]);
970 kfree(env[2]);
971 kfree(buf);
972 return -EIO;
973 }
974
975 if (atomic_xchg(&dc->running, 1)) {
976 kfree(env[1]);
977 kfree(env[2]);
978 kfree(buf);
979 pr_info("cached dev %s is running already",
980 dc->backing_dev_name);
981 return -EBUSY;
982 }
983
984 if (!d->c &&
985 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
986 struct closure cl;
987
988 closure_init_stack(&cl);
989
990 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
991 bch_write_bdev_super(dc, &cl);
992 closure_sync(&cl);
993 }
994
995 add_disk(d->disk);
996 bd_link_disk_holder(dc->bdev, dc->disk.disk);
997 /*
998 * won't show up in the uevent file, use udevadm monitor -e instead
999 * only class / kset properties are persistent
1000 */
1001 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
1002 kfree(env[1]);
1003 kfree(env[2]);
1004 kfree(buf);
1005
1006 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
1007 sysfs_create_link(&disk_to_dev(d->disk)->kobj,
1008 &d->kobj, "bcache")) {
1009 pr_err("Couldn't create bcache dev <-> disk sysfs symlinks");
1010 return -ENOMEM;
1011 }
1012
1013 dc->status_update_thread = kthread_run(cached_dev_status_update,
1014 dc, "bcache_status_update");
1015 if (IS_ERR(dc->status_update_thread)) {
1016 pr_warn("failed to create bcache_status_update kthread, "
1017 "continue to run without monitoring backing "
1018 "device status");
1019 }
1020
1021 return 0;
1022}
1023
1024/*
1025 * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed
1026 * work dc->writeback_rate_update is running. Wait until the routine
1027 * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to
1028 * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out
1029 * seconds, give up waiting here and continue to cancel it too.
1030 */
1031static void cancel_writeback_rate_update_dwork(struct cached_dev *dc)
1032{
1033 int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ;
1034
1035 do {
1036 if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING,
1037 &dc->disk.flags))
1038 break;
1039 time_out--;
1040 schedule_timeout_interruptible(1);
1041 } while (time_out > 0);
1042
1043 if (time_out == 0)
1044 pr_warn("give up waiting for dc->writeback_write_update to quit");
1045
1046 cancel_delayed_work_sync(&dc->writeback_rate_update);
1047}
1048
1049static void cached_dev_detach_finish(struct work_struct *w)
1050{
1051 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
1052 struct closure cl;
1053
1054 closure_init_stack(&cl);
1055
1056 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
1057 BUG_ON(refcount_read(&dc->count));
1058
1059
1060 if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
1061 cancel_writeback_rate_update_dwork(dc);
1062
1063 if (!IS_ERR_OR_NULL(dc->writeback_thread)) {
1064 kthread_stop(dc->writeback_thread);
1065 dc->writeback_thread = NULL;
1066 }
1067
1068 memset(&dc->sb.set_uuid, 0, 16);
1069 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
1070
1071 bch_write_bdev_super(dc, &cl);
1072 closure_sync(&cl);
1073
1074 mutex_lock(&bch_register_lock);
1075
1076 calc_cached_dev_sectors(dc->disk.c);
1077 bcache_device_detach(&dc->disk);
1078 list_move(&dc->list, &uncached_devices);
1079
1080 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
1081 clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags);
1082
1083 mutex_unlock(&bch_register_lock);
1084
1085 pr_info("Caching disabled for %s", dc->backing_dev_name);
1086
1087 /* Drop ref we took in cached_dev_detach() */
1088 closure_put(&dc->disk.cl);
1089}
1090
1091void bch_cached_dev_detach(struct cached_dev *dc)
1092{
1093 lockdep_assert_held(&bch_register_lock);
1094
1095 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
1096 return;
1097
1098 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
1099 return;
1100
1101 /*
1102 * Block the device from being closed and freed until we're finished
1103 * detaching
1104 */
1105 closure_get(&dc->disk.cl);
1106
1107 bch_writeback_queue(dc);
1108
1109 cached_dev_put(dc);
1110}
1111
1112int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
1113 uint8_t *set_uuid)
1114{
1115 uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds());
1116 struct uuid_entry *u;
1117 struct cached_dev *exist_dc, *t;
1118 int ret = 0;
1119
1120 if ((set_uuid && memcmp(set_uuid, c->sb.set_uuid, 16)) ||
1121 (!set_uuid && memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)))
1122 return -ENOENT;
1123
1124 if (dc->disk.c) {
1125 pr_err("Can't attach %s: already attached",
1126 dc->backing_dev_name);
1127 return -EINVAL;
1128 }
1129
1130 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
1131 pr_err("Can't attach %s: shutting down",
1132 dc->backing_dev_name);
1133 return -EINVAL;
1134 }
1135
1136 if (dc->sb.block_size < c->sb.block_size) {
1137 /* Will die */
1138 pr_err("Couldn't attach %s: block size less than set's block size",
1139 dc->backing_dev_name);
1140 return -EINVAL;
1141 }
1142
1143 /* Check whether already attached */
1144 list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) {
1145 if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) {
1146 pr_err("Tried to attach %s but duplicate UUID already attached",
1147 dc->backing_dev_name);
1148
1149 return -EINVAL;
1150 }
1151 }
1152
1153 u = uuid_find(c, dc->sb.uuid);
1154
1155 if (u &&
1156 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
1157 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
1158 memcpy(u->uuid, invalid_uuid, 16);
1159 u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
1160 u = NULL;
1161 }
1162
1163 if (!u) {
1164 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1165 pr_err("Couldn't find uuid for %s in set",
1166 dc->backing_dev_name);
1167 return -ENOENT;
1168 }
1169
1170 u = uuid_find_empty(c);
1171 if (!u) {
1172 pr_err("Not caching %s, no room for UUID",
1173 dc->backing_dev_name);
1174 return -EINVAL;
1175 }
1176 }
1177
1178 /*
1179 * Deadlocks since we're called via sysfs...
1180 * sysfs_remove_file(&dc->kobj, &sysfs_attach);
1181 */
1182
1183 if (bch_is_zero(u->uuid, 16)) {
1184 struct closure cl;
1185
1186 closure_init_stack(&cl);
1187
1188 memcpy(u->uuid, dc->sb.uuid, 16);
1189 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1190 u->first_reg = u->last_reg = rtime;
1191 bch_uuid_write(c);
1192
1193 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
1194 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1195
1196 bch_write_bdev_super(dc, &cl);
1197 closure_sync(&cl);
1198 } else {
1199 u->last_reg = rtime;
1200 bch_uuid_write(c);
1201 }
1202
1203 bcache_device_attach(&dc->disk, c, u - c->uuids);
1204 list_move(&dc->list, &c->cached_devs);
1205 calc_cached_dev_sectors(c);
1206
1207 /*
1208 * dc->c must be set before dc->count != 0 - paired with the mb in
1209 * cached_dev_get()
1210 */
1211 smp_wmb();
1212 refcount_set(&dc->count, 1);
1213
1214 /* Block writeback thread, but spawn it */
1215 down_write(&dc->writeback_lock);
1216 if (bch_cached_dev_writeback_start(dc)) {
1217 up_write(&dc->writeback_lock);
1218 pr_err("Couldn't start writeback facilities for %s",
1219 dc->disk.disk->disk_name);
1220 return -ENOMEM;
1221 }
1222
1223 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1224 atomic_set(&dc->has_dirty, 1);
1225 bch_writeback_queue(dc);
1226 }
1227
1228 bch_sectors_dirty_init(&dc->disk);
1229
1230 ret = bch_cached_dev_run(dc);
1231 if (ret && (ret != -EBUSY)) {
1232 up_write(&dc->writeback_lock);
1233 /*
1234 * bch_register_lock is held, bcache_device_stop() is not
1235 * able to be directly called. The kthread and kworker
1236 * created previously in bch_cached_dev_writeback_start()
1237 * have to be stopped manually here.
1238 */
1239 kthread_stop(dc->writeback_thread);
1240 cancel_writeback_rate_update_dwork(dc);
1241 pr_err("Couldn't run cached device %s",
1242 dc->backing_dev_name);
1243 return ret;
1244 }
1245
1246 bcache_device_link(&dc->disk, c, "bdev");
1247 atomic_inc(&c->attached_dev_nr);
1248
1249 /* Allow the writeback thread to proceed */
1250 up_write(&dc->writeback_lock);
1251
1252 pr_info("Caching %s as %s on set %pU",
1253 dc->backing_dev_name,
1254 dc->disk.disk->disk_name,
1255 dc->disk.c->sb.set_uuid);
1256 return 0;
1257}
1258
1259/* when dc->disk.kobj released */
1260void bch_cached_dev_release(struct kobject *kobj)
1261{
1262 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1263 disk.kobj);
1264 kfree(dc);
1265 module_put(THIS_MODULE);
1266}
1267
1268static void cached_dev_free(struct closure *cl)
1269{
1270 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1271
1272 if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
1273 cancel_writeback_rate_update_dwork(dc);
1274
1275 if (!IS_ERR_OR_NULL(dc->writeback_thread))
1276 kthread_stop(dc->writeback_thread);
1277 if (!IS_ERR_OR_NULL(dc->status_update_thread))
1278 kthread_stop(dc->status_update_thread);
1279
1280 mutex_lock(&bch_register_lock);
1281
1282 if (atomic_read(&dc->running))
1283 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
1284 bcache_device_free(&dc->disk);
1285 list_del(&dc->list);
1286
1287 mutex_unlock(&bch_register_lock);
1288
1289 if (dc->sb_bio.bi_inline_vecs[0].bv_page)
1290 put_page(bio_first_page_all(&dc->sb_bio));
1291
1292 if (!IS_ERR_OR_NULL(dc->bdev))
1293 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1294
1295 wake_up(&unregister_wait);
1296
1297 kobject_put(&dc->disk.kobj);
1298}
1299
1300static void cached_dev_flush(struct closure *cl)
1301{
1302 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1303 struct bcache_device *d = &dc->disk;
1304
1305 mutex_lock(&bch_register_lock);
1306 bcache_device_unlink(d);
1307 mutex_unlock(&bch_register_lock);
1308
1309 bch_cache_accounting_destroy(&dc->accounting);
1310 kobject_del(&d->kobj);
1311
1312 continue_at(cl, cached_dev_free, system_wq);
1313}
1314
1315static int cached_dev_init(struct cached_dev *dc, unsigned int block_size)
1316{
1317 int ret;
1318 struct io *io;
1319 struct request_queue *q = bdev_get_queue(dc->bdev);
1320
1321 __module_get(THIS_MODULE);
1322 INIT_LIST_HEAD(&dc->list);
1323 closure_init(&dc->disk.cl, NULL);
1324 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1325 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1326 INIT_WORK(&dc->detach, cached_dev_detach_finish);
1327 sema_init(&dc->sb_write_mutex, 1);
1328 INIT_LIST_HEAD(&dc->io_lru);
1329 spin_lock_init(&dc->io_lock);
1330 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1331
1332 dc->sequential_cutoff = 4 << 20;
1333
1334 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1335 list_add(&io->lru, &dc->io_lru);
1336 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1337 }
1338
1339 dc->disk.stripe_size = q->limits.io_opt >> 9;
1340
1341 if (dc->disk.stripe_size)
1342 dc->partial_stripes_expensive =
1343 q->limits.raid_partial_stripes_expensive;
1344
1345 ret = bcache_device_init(&dc->disk, block_size,
1346 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1347 if (ret)
1348 return ret;
1349
1350 dc->disk.disk->queue->backing_dev_info->ra_pages =
1351 max(dc->disk.disk->queue->backing_dev_info->ra_pages,
1352 q->backing_dev_info->ra_pages);
1353
1354 atomic_set(&dc->io_errors, 0);
1355 dc->io_disable = false;
1356 dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT;
1357 /* default to auto */
1358 dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO;
1359
1360 bch_cached_dev_request_init(dc);
1361 bch_cached_dev_writeback_init(dc);
1362 return 0;
1363}
1364
1365/* Cached device - bcache superblock */
1366
1367static int register_bdev(struct cache_sb *sb, struct page *sb_page,
1368 struct block_device *bdev,
1369 struct cached_dev *dc)
1370{
1371 const char *err = "cannot allocate memory";
1372 struct cache_set *c;
1373 int ret = -ENOMEM;
1374
1375 bdevname(bdev, dc->backing_dev_name);
1376 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1377 dc->bdev = bdev;
1378 dc->bdev->bd_holder = dc;
1379
1380 bio_init(&dc->sb_bio, dc->sb_bio.bi_inline_vecs, 1);
1381 bio_first_bvec_all(&dc->sb_bio)->bv_page = sb_page;
1382 get_page(sb_page);
1383
1384
1385 if (cached_dev_init(dc, sb->block_size << 9))
1386 goto err;
1387
1388 err = "error creating kobject";
1389 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1390 "bcache"))
1391 goto err;
1392 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1393 goto err;
1394
1395 pr_info("registered backing device %s", dc->backing_dev_name);
1396
1397 list_add(&dc->list, &uncached_devices);
1398 /* attach to a matched cache set if it exists */
1399 list_for_each_entry(c, &bch_cache_sets, list)
1400 bch_cached_dev_attach(dc, c, NULL);
1401
1402 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1403 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) {
1404 err = "failed to run cached device";
1405 ret = bch_cached_dev_run(dc);
1406 if (ret)
1407 goto err;
1408 }
1409
1410 return 0;
1411err:
1412 pr_notice("error %s: %s", dc->backing_dev_name, err);
1413 bcache_device_stop(&dc->disk);
1414 return ret;
1415}
1416
1417/* Flash only volumes */
1418
1419/* When d->kobj released */
1420void bch_flash_dev_release(struct kobject *kobj)
1421{
1422 struct bcache_device *d = container_of(kobj, struct bcache_device,
1423 kobj);
1424 kfree(d);
1425}
1426
1427static void flash_dev_free(struct closure *cl)
1428{
1429 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1430
1431 mutex_lock(&bch_register_lock);
1432 atomic_long_sub(bcache_dev_sectors_dirty(d),
1433 &d->c->flash_dev_dirty_sectors);
1434 bcache_device_free(d);
1435 mutex_unlock(&bch_register_lock);
1436 kobject_put(&d->kobj);
1437}
1438
1439static void flash_dev_flush(struct closure *cl)
1440{
1441 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1442
1443 mutex_lock(&bch_register_lock);
1444 bcache_device_unlink(d);
1445 mutex_unlock(&bch_register_lock);
1446 kobject_del(&d->kobj);
1447 continue_at(cl, flash_dev_free, system_wq);
1448}
1449
1450static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1451{
1452 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1453 GFP_KERNEL);
1454 if (!d)
1455 return -ENOMEM;
1456
1457 closure_init(&d->cl, NULL);
1458 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1459
1460 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1461
1462 if (bcache_device_init(d, block_bytes(c), u->sectors))
1463 goto err;
1464
1465 bcache_device_attach(d, c, u - c->uuids);
1466 bch_sectors_dirty_init(d);
1467 bch_flash_dev_request_init(d);
1468 add_disk(d->disk);
1469
1470 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1471 goto err;
1472
1473 bcache_device_link(d, c, "volume");
1474
1475 return 0;
1476err:
1477 kobject_put(&d->kobj);
1478 return -ENOMEM;
1479}
1480
1481static int flash_devs_run(struct cache_set *c)
1482{
1483 int ret = 0;
1484 struct uuid_entry *u;
1485
1486 for (u = c->uuids;
1487 u < c->uuids + c->nr_uuids && !ret;
1488 u++)
1489 if (UUID_FLASH_ONLY(u))
1490 ret = flash_dev_run(c, u);
1491
1492 return ret;
1493}
1494
1495int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1496{
1497 struct uuid_entry *u;
1498
1499 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1500 return -EINTR;
1501
1502 if (!test_bit(CACHE_SET_RUNNING, &c->flags))
1503 return -EPERM;
1504
1505 u = uuid_find_empty(c);
1506 if (!u) {
1507 pr_err("Can't create volume, no room for UUID");
1508 return -EINVAL;
1509 }
1510
1511 get_random_bytes(u->uuid, 16);
1512 memset(u->label, 0, 32);
1513 u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
1514
1515 SET_UUID_FLASH_ONLY(u, 1);
1516 u->sectors = size >> 9;
1517
1518 bch_uuid_write(c);
1519
1520 return flash_dev_run(c, u);
1521}
1522
1523bool bch_cached_dev_error(struct cached_dev *dc)
1524{
1525 if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
1526 return false;
1527
1528 dc->io_disable = true;
1529 /* make others know io_disable is true earlier */
1530 smp_mb();
1531
1532 pr_err("stop %s: too many IO errors on backing device %s\n",
1533 dc->disk.disk->disk_name, dc->backing_dev_name);
1534
1535 bcache_device_stop(&dc->disk);
1536 return true;
1537}
1538
1539/* Cache set */
1540
1541__printf(2, 3)
1542bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1543{
1544 va_list args;
1545
1546 if (c->on_error != ON_ERROR_PANIC &&
1547 test_bit(CACHE_SET_STOPPING, &c->flags))
1548 return false;
1549
1550 if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags))
1551 pr_info("CACHE_SET_IO_DISABLE already set");
1552
1553 /*
1554 * XXX: we can be called from atomic context
1555 * acquire_console_sem();
1556 */
1557
1558 pr_err("bcache: error on %pU: ", c->sb.set_uuid);
1559
1560 va_start(args, fmt);
1561 vprintk(fmt, args);
1562 va_end(args);
1563
1564 pr_err(", disabling caching\n");
1565
1566 if (c->on_error == ON_ERROR_PANIC)
1567 panic("panic forced after error\n");
1568
1569 bch_cache_set_unregister(c);
1570 return true;
1571}
1572
1573/* When c->kobj released */
1574void bch_cache_set_release(struct kobject *kobj)
1575{
1576 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1577
1578 kfree(c);
1579 module_put(THIS_MODULE);
1580}
1581
1582static void cache_set_free(struct closure *cl)
1583{
1584 struct cache_set *c = container_of(cl, struct cache_set, cl);
1585 struct cache *ca;
1586 unsigned int i;
1587
1588 debugfs_remove(c->debug);
1589
1590 bch_open_buckets_free(c);
1591 bch_btree_cache_free(c);
1592 bch_journal_free(c);
1593
1594 mutex_lock(&bch_register_lock);
1595 for_each_cache(ca, c, i)
1596 if (ca) {
1597 ca->set = NULL;
1598 c->cache[ca->sb.nr_this_dev] = NULL;
1599 kobject_put(&ca->kobj);
1600 }
1601
1602 bch_bset_sort_state_free(&c->sort);
1603 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1604
1605 if (c->moving_gc_wq)
1606 destroy_workqueue(c->moving_gc_wq);
1607 bioset_exit(&c->bio_split);
1608 mempool_exit(&c->fill_iter);
1609 mempool_exit(&c->bio_meta);
1610 mempool_exit(&c->search);
1611 kfree(c->devices);
1612
1613 list_del(&c->list);
1614 mutex_unlock(&bch_register_lock);
1615
1616 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1617 wake_up(&unregister_wait);
1618
1619 closure_debug_destroy(&c->cl);
1620 kobject_put(&c->kobj);
1621}
1622
1623static void cache_set_flush(struct closure *cl)
1624{
1625 struct cache_set *c = container_of(cl, struct cache_set, caching);
1626 struct cache *ca;
1627 struct btree *b;
1628 unsigned int i;
1629
1630 bch_cache_accounting_destroy(&c->accounting);
1631
1632 kobject_put(&c->internal);
1633 kobject_del(&c->kobj);
1634
1635 if (!IS_ERR_OR_NULL(c->gc_thread))
1636 kthread_stop(c->gc_thread);
1637
1638 if (!IS_ERR_OR_NULL(c->root))
1639 list_add(&c->root->list, &c->btree_cache);
1640
1641 /*
1642 * Avoid flushing cached nodes if cache set is retiring
1643 * due to too many I/O errors detected.
1644 */
1645 if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags))
1646 list_for_each_entry(b, &c->btree_cache, list) {
1647 mutex_lock(&b->write_lock);
1648 if (btree_node_dirty(b))
1649 __bch_btree_node_write(b, NULL);
1650 mutex_unlock(&b->write_lock);
1651 }
1652
1653 for_each_cache(ca, c, i)
1654 if (ca->alloc_thread)
1655 kthread_stop(ca->alloc_thread);
1656
1657 if (c->journal.cur) {
1658 cancel_delayed_work_sync(&c->journal.work);
1659 /* flush last journal entry if needed */
1660 c->journal.work.work.func(&c->journal.work.work);
1661 }
1662
1663 closure_return(cl);
1664}
1665
1666/*
1667 * This function is only called when CACHE_SET_IO_DISABLE is set, which means
1668 * cache set is unregistering due to too many I/O errors. In this condition,
1669 * the bcache device might be stopped, it depends on stop_when_cache_set_failed
1670 * value and whether the broken cache has dirty data:
1671 *
1672 * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device
1673 * BCH_CACHED_STOP_AUTO 0 NO
1674 * BCH_CACHED_STOP_AUTO 1 YES
1675 * BCH_CACHED_DEV_STOP_ALWAYS 0 YES
1676 * BCH_CACHED_DEV_STOP_ALWAYS 1 YES
1677 *
1678 * The expected behavior is, if stop_when_cache_set_failed is configured to
1679 * "auto" via sysfs interface, the bcache device will not be stopped if the
1680 * backing device is clean on the broken cache device.
1681 */
1682static void conditional_stop_bcache_device(struct cache_set *c,
1683 struct bcache_device *d,
1684 struct cached_dev *dc)
1685{
1686 if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) {
1687 pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.",
1688 d->disk->disk_name, c->sb.set_uuid);
1689 bcache_device_stop(d);
1690 } else if (atomic_read(&dc->has_dirty)) {
1691 /*
1692 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1693 * and dc->has_dirty == 1
1694 */
1695 pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.",
1696 d->disk->disk_name);
1697 /*
1698 * There might be a small time gap that cache set is
1699 * released but bcache device is not. Inside this time
1700 * gap, regular I/O requests will directly go into
1701 * backing device as no cache set attached to. This
1702 * behavior may also introduce potential inconsistence
1703 * data in writeback mode while cache is dirty.
1704 * Therefore before calling bcache_device_stop() due
1705 * to a broken cache device, dc->io_disable should be
1706 * explicitly set to true.
1707 */
1708 dc->io_disable = true;
1709 /* make others know io_disable is true earlier */
1710 smp_mb();
1711 bcache_device_stop(d);
1712 } else {
1713 /*
1714 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1715 * and dc->has_dirty == 0
1716 */
1717 pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.",
1718 d->disk->disk_name);
1719 }
1720}
1721
1722static void __cache_set_unregister(struct closure *cl)
1723{
1724 struct cache_set *c = container_of(cl, struct cache_set, caching);
1725 struct cached_dev *dc;
1726 struct bcache_device *d;
1727 size_t i;
1728
1729 mutex_lock(&bch_register_lock);
1730
1731 for (i = 0; i < c->devices_max_used; i++) {
1732 d = c->devices[i];
1733 if (!d)
1734 continue;
1735
1736 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1737 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1738 dc = container_of(d, struct cached_dev, disk);
1739 bch_cached_dev_detach(dc);
1740 if (test_bit(CACHE_SET_IO_DISABLE, &c->flags))
1741 conditional_stop_bcache_device(c, d, dc);
1742 } else {
1743 bcache_device_stop(d);
1744 }
1745 }
1746
1747 mutex_unlock(&bch_register_lock);
1748
1749 continue_at(cl, cache_set_flush, system_wq);
1750}
1751
1752void bch_cache_set_stop(struct cache_set *c)
1753{
1754 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1755 /* closure_fn set to __cache_set_unregister() */
1756 closure_queue(&c->caching);
1757}
1758
1759void bch_cache_set_unregister(struct cache_set *c)
1760{
1761 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1762 bch_cache_set_stop(c);
1763}
1764
1765#define alloc_bucket_pages(gfp, c) \
1766 ((void *) __get_free_pages(__GFP_ZERO|__GFP_COMP|gfp, ilog2(bucket_pages(c))))
1767
1768struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1769{
1770 int iter_size;
1771 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1772
1773 if (!c)
1774 return NULL;
1775
1776 __module_get(THIS_MODULE);
1777 closure_init(&c->cl, NULL);
1778 set_closure_fn(&c->cl, cache_set_free, system_wq);
1779
1780 closure_init(&c->caching, &c->cl);
1781 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1782
1783 /* Maybe create continue_at_noreturn() and use it here? */
1784 closure_set_stopped(&c->cl);
1785 closure_put(&c->cl);
1786
1787 kobject_init(&c->kobj, &bch_cache_set_ktype);
1788 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1789
1790 bch_cache_accounting_init(&c->accounting, &c->cl);
1791
1792 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1793 c->sb.block_size = sb->block_size;
1794 c->sb.bucket_size = sb->bucket_size;
1795 c->sb.nr_in_set = sb->nr_in_set;
1796 c->sb.last_mount = sb->last_mount;
1797 c->bucket_bits = ilog2(sb->bucket_size);
1798 c->block_bits = ilog2(sb->block_size);
1799 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1800 c->devices_max_used = 0;
1801 atomic_set(&c->attached_dev_nr, 0);
1802 c->btree_pages = bucket_pages(c);
1803 if (c->btree_pages > BTREE_MAX_PAGES)
1804 c->btree_pages = max_t(int, c->btree_pages / 4,
1805 BTREE_MAX_PAGES);
1806
1807 sema_init(&c->sb_write_mutex, 1);
1808 mutex_init(&c->bucket_lock);
1809 init_waitqueue_head(&c->btree_cache_wait);
1810 spin_lock_init(&c->btree_cannibalize_lock);
1811 init_waitqueue_head(&c->bucket_wait);
1812 init_waitqueue_head(&c->gc_wait);
1813 sema_init(&c->uuid_write_mutex, 1);
1814
1815 spin_lock_init(&c->btree_gc_time.lock);
1816 spin_lock_init(&c->btree_split_time.lock);
1817 spin_lock_init(&c->btree_read_time.lock);
1818
1819 bch_moving_init_cache_set(c);
1820
1821 INIT_LIST_HEAD(&c->list);
1822 INIT_LIST_HEAD(&c->cached_devs);
1823 INIT_LIST_HEAD(&c->btree_cache);
1824 INIT_LIST_HEAD(&c->btree_cache_freeable);
1825 INIT_LIST_HEAD(&c->btree_cache_freed);
1826 INIT_LIST_HEAD(&c->data_buckets);
1827
1828 iter_size = (sb->bucket_size / sb->block_size + 1) *
1829 sizeof(struct btree_iter_set);
1830
1831 if (!(c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL)) ||
1832 mempool_init_slab_pool(&c->search, 32, bch_search_cache) ||
1833 mempool_init_kmalloc_pool(&c->bio_meta, 2,
1834 sizeof(struct bbio) + sizeof(struct bio_vec) *
1835 bucket_pages(c)) ||
1836 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
1837 bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio),
1838 BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER) ||
1839 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1840 !(c->moving_gc_wq = alloc_workqueue("bcache_gc",
1841 WQ_MEM_RECLAIM, 0)) ||
1842 bch_journal_alloc(c) ||
1843 bch_btree_cache_alloc(c) ||
1844 bch_open_buckets_alloc(c) ||
1845 bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages)))
1846 goto err;
1847
1848 c->congested_read_threshold_us = 2000;
1849 c->congested_write_threshold_us = 20000;
1850 c->error_limit = DEFAULT_IO_ERROR_LIMIT;
1851 WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags));
1852
1853 return c;
1854err:
1855 bch_cache_set_unregister(c);
1856 return NULL;
1857}
1858
1859static int run_cache_set(struct cache_set *c)
1860{
1861 const char *err = "cannot allocate memory";
1862 struct cached_dev *dc, *t;
1863 struct cache *ca;
1864 struct closure cl;
1865 unsigned int i;
1866 LIST_HEAD(journal);
1867 struct journal_replay *l;
1868
1869 closure_init_stack(&cl);
1870
1871 for_each_cache(ca, c, i)
1872 c->nbuckets += ca->sb.nbuckets;
1873 set_gc_sectors(c);
1874
1875 if (CACHE_SYNC(&c->sb)) {
1876 struct bkey *k;
1877 struct jset *j;
1878
1879 err = "cannot allocate memory for journal";
1880 if (bch_journal_read(c, &journal))
1881 goto err;
1882
1883 pr_debug("btree_journal_read() done");
1884
1885 err = "no journal entries found";
1886 if (list_empty(&journal))
1887 goto err;
1888
1889 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1890
1891 err = "IO error reading priorities";
1892 for_each_cache(ca, c, i)
1893 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1894
1895 /*
1896 * If prio_read() fails it'll call cache_set_error and we'll
1897 * tear everything down right away, but if we perhaps checked
1898 * sooner we could avoid journal replay.
1899 */
1900
1901 k = &j->btree_root;
1902
1903 err = "bad btree root";
1904 if (__bch_btree_ptr_invalid(c, k))
1905 goto err;
1906
1907 err = "error reading btree root";
1908 c->root = bch_btree_node_get(c, NULL, k,
1909 j->btree_level,
1910 true, NULL);
1911 if (IS_ERR(c->root))
1912 goto err;
1913
1914 list_del_init(&c->root->list);
1915 rw_unlock(true, c->root);
1916
1917 err = uuid_read(c, j, &cl);
1918 if (err)
1919 goto err;
1920
1921 err = "error in recovery";
1922 if (bch_btree_check(c))
1923 goto err;
1924
1925 /*
1926 * bch_btree_check() may occupy too much system memory which
1927 * has negative effects to user space application (e.g. data
1928 * base) performance. Shrink the mca cache memory proactively
1929 * here to avoid competing memory with user space workloads..
1930 */
1931 if (!c->shrinker_disabled) {
1932 struct shrink_control sc;
1933
1934 sc.gfp_mask = GFP_KERNEL;
1935 sc.nr_to_scan = c->btree_cache_used * c->btree_pages;
1936 /* first run to clear b->accessed tag */
1937 c->shrink.scan_objects(&c->shrink, &sc);
1938 /* second run to reap non-accessed nodes */
1939 c->shrink.scan_objects(&c->shrink, &sc);
1940 }
1941
1942 bch_journal_mark(c, &journal);
1943 bch_initial_gc_finish(c);
1944 pr_debug("btree_check() done");
1945
1946 /*
1947 * bcache_journal_next() can't happen sooner, or
1948 * btree_gc_finish() will give spurious errors about last_gc >
1949 * gc_gen - this is a hack but oh well.
1950 */
1951 bch_journal_next(&c->journal);
1952
1953 err = "error starting allocator thread";
1954 for_each_cache(ca, c, i)
1955 if (bch_cache_allocator_start(ca))
1956 goto err;
1957
1958 /*
1959 * First place it's safe to allocate: btree_check() and
1960 * btree_gc_finish() have to run before we have buckets to
1961 * allocate, and bch_bucket_alloc_set() might cause a journal
1962 * entry to be written so bcache_journal_next() has to be called
1963 * first.
1964 *
1965 * If the uuids were in the old format we have to rewrite them
1966 * before the next journal entry is written:
1967 */
1968 if (j->version < BCACHE_JSET_VERSION_UUID)
1969 __uuid_write(c);
1970
1971 err = "bcache: replay journal failed";
1972 if (bch_journal_replay(c, &journal))
1973 goto err;
1974 } else {
1975 pr_notice("invalidating existing data");
1976
1977 for_each_cache(ca, c, i) {
1978 unsigned int j;
1979
1980 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1981 2, SB_JOURNAL_BUCKETS);
1982
1983 for (j = 0; j < ca->sb.keys; j++)
1984 ca->sb.d[j] = ca->sb.first_bucket + j;
1985 }
1986
1987 bch_initial_gc_finish(c);
1988
1989 err = "error starting allocator thread";
1990 for_each_cache(ca, c, i)
1991 if (bch_cache_allocator_start(ca))
1992 goto err;
1993
1994 mutex_lock(&c->bucket_lock);
1995 for_each_cache(ca, c, i)
1996 bch_prio_write(ca, true);
1997 mutex_unlock(&c->bucket_lock);
1998
1999 err = "cannot allocate new UUID bucket";
2000 if (__uuid_write(c))
2001 goto err;
2002
2003 err = "cannot allocate new btree root";
2004 c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL);
2005 if (IS_ERR(c->root))
2006 goto err;
2007
2008 mutex_lock(&c->root->write_lock);
2009 bkey_copy_key(&c->root->key, &MAX_KEY);
2010 bch_btree_node_write(c->root, &cl);
2011 mutex_unlock(&c->root->write_lock);
2012
2013 bch_btree_set_root(c->root);
2014 rw_unlock(true, c->root);
2015
2016 /*
2017 * We don't want to write the first journal entry until
2018 * everything is set up - fortunately journal entries won't be
2019 * written until the SET_CACHE_SYNC() here:
2020 */
2021 SET_CACHE_SYNC(&c->sb, true);
2022
2023 bch_journal_next(&c->journal);
2024 bch_journal_meta(c, &cl);
2025 }
2026
2027 err = "error starting gc thread";
2028 if (bch_gc_thread_start(c))
2029 goto err;
2030
2031 closure_sync(&cl);
2032 c->sb.last_mount = (u32)ktime_get_real_seconds();
2033 bcache_write_super(c);
2034
2035 list_for_each_entry_safe(dc, t, &uncached_devices, list)
2036 bch_cached_dev_attach(dc, c, NULL);
2037
2038 flash_devs_run(c);
2039
2040 set_bit(CACHE_SET_RUNNING, &c->flags);
2041 return 0;
2042err:
2043 while (!list_empty(&journal)) {
2044 l = list_first_entry(&journal, struct journal_replay, list);
2045 list_del(&l->list);
2046 kfree(l);
2047 }
2048
2049 closure_sync(&cl);
2050
2051 bch_cache_set_error(c, "%s", err);
2052
2053 return -EIO;
2054}
2055
2056static bool can_attach_cache(struct cache *ca, struct cache_set *c)
2057{
2058 return ca->sb.block_size == c->sb.block_size &&
2059 ca->sb.bucket_size == c->sb.bucket_size &&
2060 ca->sb.nr_in_set == c->sb.nr_in_set;
2061}
2062
2063static const char *register_cache_set(struct cache *ca)
2064{
2065 char buf[12];
2066 const char *err = "cannot allocate memory";
2067 struct cache_set *c;
2068
2069 list_for_each_entry(c, &bch_cache_sets, list)
2070 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
2071 if (c->cache[ca->sb.nr_this_dev])
2072 return "duplicate cache set member";
2073
2074 if (!can_attach_cache(ca, c))
2075 return "cache sb does not match set";
2076
2077 if (!CACHE_SYNC(&ca->sb))
2078 SET_CACHE_SYNC(&c->sb, false);
2079
2080 goto found;
2081 }
2082
2083 c = bch_cache_set_alloc(&ca->sb);
2084 if (!c)
2085 return err;
2086
2087 err = "error creating kobject";
2088 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
2089 kobject_add(&c->internal, &c->kobj, "internal"))
2090 goto err;
2091
2092 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
2093 goto err;
2094
2095 bch_debug_init_cache_set(c);
2096
2097 list_add(&c->list, &bch_cache_sets);
2098found:
2099 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
2100 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
2101 sysfs_create_link(&c->kobj, &ca->kobj, buf))
2102 goto err;
2103
2104 /*
2105 * A special case is both ca->sb.seq and c->sb.seq are 0,
2106 * such condition happens on a new created cache device whose
2107 * super block is never flushed yet. In this case c->sb.version
2108 * and other members should be updated too, otherwise we will
2109 * have a mistaken super block version in cache set.
2110 */
2111 if (ca->sb.seq > c->sb.seq || c->sb.seq == 0) {
2112 c->sb.version = ca->sb.version;
2113 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
2114 c->sb.flags = ca->sb.flags;
2115 c->sb.seq = ca->sb.seq;
2116 pr_debug("set version = %llu", c->sb.version);
2117 }
2118
2119 kobject_get(&ca->kobj);
2120 ca->set = c;
2121 ca->set->cache[ca->sb.nr_this_dev] = ca;
2122 c->cache_by_alloc[c->caches_loaded++] = ca;
2123
2124 if (c->caches_loaded == c->sb.nr_in_set) {
2125 err = "failed to run cache set";
2126 if (run_cache_set(c) < 0)
2127 goto err;
2128 }
2129
2130 return NULL;
2131err:
2132 bch_cache_set_unregister(c);
2133 return err;
2134}
2135
2136/* Cache device */
2137
2138/* When ca->kobj released */
2139void bch_cache_release(struct kobject *kobj)
2140{
2141 struct cache *ca = container_of(kobj, struct cache, kobj);
2142 unsigned int i;
2143
2144 if (ca->set) {
2145 BUG_ON(ca->set->cache[ca->sb.nr_this_dev] != ca);
2146 ca->set->cache[ca->sb.nr_this_dev] = NULL;
2147 }
2148
2149 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
2150 kfree(ca->prio_buckets);
2151 vfree(ca->buckets);
2152
2153 free_heap(&ca->heap);
2154 free_fifo(&ca->free_inc);
2155
2156 for (i = 0; i < RESERVE_NR; i++)
2157 free_fifo(&ca->free[i]);
2158
2159 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
2160 put_page(bio_first_page_all(&ca->sb_bio));
2161
2162 if (!IS_ERR_OR_NULL(ca->bdev))
2163 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2164
2165 kfree(ca);
2166 module_put(THIS_MODULE);
2167}
2168
2169static int cache_alloc(struct cache *ca)
2170{
2171 size_t free;
2172 size_t btree_buckets;
2173 struct bucket *b;
2174 int ret = -ENOMEM;
2175 const char *err = NULL;
2176
2177 __module_get(THIS_MODULE);
2178 kobject_init(&ca->kobj, &bch_cache_ktype);
2179
2180 bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8);
2181
2182 /*
2183 * when ca->sb.njournal_buckets is not zero, journal exists,
2184 * and in bch_journal_replay(), tree node may split,
2185 * so bucket of RESERVE_BTREE type is needed,
2186 * the worst situation is all journal buckets are valid journal,
2187 * and all the keys need to replay,
2188 * so the number of RESERVE_BTREE type buckets should be as much
2189 * as journal buckets
2190 */
2191 btree_buckets = ca->sb.njournal_buckets ?: 8;
2192 free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
2193 if (!free) {
2194 ret = -EPERM;
2195 err = "ca->sb.nbuckets is too small";
2196 goto err_free;
2197 }
2198
2199 if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets,
2200 GFP_KERNEL)) {
2201 err = "ca->free[RESERVE_BTREE] alloc failed";
2202 goto err_btree_alloc;
2203 }
2204
2205 if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca),
2206 GFP_KERNEL)) {
2207 err = "ca->free[RESERVE_PRIO] alloc failed";
2208 goto err_prio_alloc;
2209 }
2210
2211 if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) {
2212 err = "ca->free[RESERVE_MOVINGGC] alloc failed";
2213 goto err_movinggc_alloc;
2214 }
2215
2216 if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) {
2217 err = "ca->free[RESERVE_NONE] alloc failed";
2218 goto err_none_alloc;
2219 }
2220
2221 if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) {
2222 err = "ca->free_inc alloc failed";
2223 goto err_free_inc_alloc;
2224 }
2225
2226 if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) {
2227 err = "ca->heap alloc failed";
2228 goto err_heap_alloc;
2229 }
2230
2231 ca->buckets = vzalloc(array_size(sizeof(struct bucket),
2232 ca->sb.nbuckets));
2233 if (!ca->buckets) {
2234 err = "ca->buckets alloc failed";
2235 goto err_buckets_alloc;
2236 }
2237
2238 ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t),
2239 prio_buckets(ca), 2),
2240 GFP_KERNEL);
2241 if (!ca->prio_buckets) {
2242 err = "ca->prio_buckets alloc failed";
2243 goto err_prio_buckets_alloc;
2244 }
2245
2246 ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca);
2247 if (!ca->disk_buckets) {
2248 err = "ca->disk_buckets alloc failed";
2249 goto err_disk_buckets_alloc;
2250 }
2251
2252 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
2253
2254 for_each_bucket(b, ca)
2255 atomic_set(&b->pin, 0);
2256 return 0;
2257
2258err_disk_buckets_alloc:
2259 kfree(ca->prio_buckets);
2260err_prio_buckets_alloc:
2261 vfree(ca->buckets);
2262err_buckets_alloc:
2263 free_heap(&ca->heap);
2264err_heap_alloc:
2265 free_fifo(&ca->free_inc);
2266err_free_inc_alloc:
2267 free_fifo(&ca->free[RESERVE_NONE]);
2268err_none_alloc:
2269 free_fifo(&ca->free[RESERVE_MOVINGGC]);
2270err_movinggc_alloc:
2271 free_fifo(&ca->free[RESERVE_PRIO]);
2272err_prio_alloc:
2273 free_fifo(&ca->free[RESERVE_BTREE]);
2274err_btree_alloc:
2275err_free:
2276 module_put(THIS_MODULE);
2277 if (err)
2278 pr_notice("error %s: %s", ca->cache_dev_name, err);
2279 return ret;
2280}
2281
2282static int register_cache(struct cache_sb *sb, struct page *sb_page,
2283 struct block_device *bdev, struct cache *ca)
2284{
2285 const char *err = NULL; /* must be set for any error case */
2286 int ret = 0;
2287
2288 bdevname(bdev, ca->cache_dev_name);
2289 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
2290 ca->bdev = bdev;
2291 ca->bdev->bd_holder = ca;
2292
2293 bio_init(&ca->sb_bio, ca->sb_bio.bi_inline_vecs, 1);
2294 bio_first_bvec_all(&ca->sb_bio)->bv_page = sb_page;
2295 get_page(sb_page);
2296
2297 if (blk_queue_discard(bdev_get_queue(bdev)))
2298 ca->discard = CACHE_DISCARD(&ca->sb);
2299
2300 ret = cache_alloc(ca);
2301 if (ret != 0) {
2302 /*
2303 * If we failed here, it means ca->kobj is not initialized yet,
2304 * kobject_put() won't be called and there is no chance to
2305 * call blkdev_put() to bdev in bch_cache_release(). So we
2306 * explicitly call blkdev_put() here.
2307 */
2308 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2309 if (ret == -ENOMEM)
2310 err = "cache_alloc(): -ENOMEM";
2311 else if (ret == -EPERM)
2312 err = "cache_alloc(): cache device is too small";
2313 else
2314 err = "cache_alloc(): unknown error";
2315 goto err;
2316 }
2317
2318 if (kobject_add(&ca->kobj,
2319 &part_to_dev(bdev->bd_part)->kobj,
2320 "bcache")) {
2321 err = "error calling kobject_add";
2322 ret = -ENOMEM;
2323 goto out;
2324 }
2325
2326 mutex_lock(&bch_register_lock);
2327 err = register_cache_set(ca);
2328 mutex_unlock(&bch_register_lock);
2329
2330 if (err) {
2331 ret = -ENODEV;
2332 goto out;
2333 }
2334
2335 pr_info("registered cache device %s", ca->cache_dev_name);
2336
2337out:
2338 kobject_put(&ca->kobj);
2339
2340err:
2341 if (err)
2342 pr_notice("error %s: %s", ca->cache_dev_name, err);
2343
2344 return ret;
2345}
2346
2347/* Global interfaces/init */
2348
2349static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
2350 const char *buffer, size_t size);
2351static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
2352 struct kobj_attribute *attr,
2353 const char *buffer, size_t size);
2354
2355kobj_attribute_write(register, register_bcache);
2356kobj_attribute_write(register_quiet, register_bcache);
2357kobj_attribute_write(pendings_cleanup, bch_pending_bdevs_cleanup);
2358
2359static bool bch_is_open_backing(struct block_device *bdev)
2360{
2361 struct cache_set *c, *tc;
2362 struct cached_dev *dc, *t;
2363
2364 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2365 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
2366 if (dc->bdev == bdev)
2367 return true;
2368 list_for_each_entry_safe(dc, t, &uncached_devices, list)
2369 if (dc->bdev == bdev)
2370 return true;
2371 return false;
2372}
2373
2374static bool bch_is_open_cache(struct block_device *bdev)
2375{
2376 struct cache_set *c, *tc;
2377 struct cache *ca;
2378 unsigned int i;
2379
2380 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2381 for_each_cache(ca, c, i)
2382 if (ca->bdev == bdev)
2383 return true;
2384 return false;
2385}
2386
2387static bool bch_is_open(struct block_device *bdev)
2388{
2389 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
2390}
2391
2392static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
2393 const char *buffer, size_t size)
2394{
2395 const char *err;
2396 char *path = NULL;
2397 struct cache_sb *sb;
2398 struct block_device *bdev = NULL;
2399 struct page *sb_page;
2400 ssize_t ret;
2401
2402 ret = -EBUSY;
2403 err = "failed to reference bcache module";
2404 if (!try_module_get(THIS_MODULE))
2405 goto out;
2406
2407 /* For latest state of bcache_is_reboot */
2408 smp_mb();
2409 err = "bcache is in reboot";
2410 if (bcache_is_reboot)
2411 goto out_module_put;
2412
2413 ret = -ENOMEM;
2414 err = "cannot allocate memory";
2415 path = kstrndup(buffer, size, GFP_KERNEL);
2416 if (!path)
2417 goto out_module_put;
2418
2419 sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL);
2420 if (!sb)
2421 goto out_free_path;
2422
2423 ret = -EINVAL;
2424 err = "failed to open device";
2425 bdev = blkdev_get_by_path(strim(path),
2426 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2427 sb);
2428 if (IS_ERR(bdev)) {
2429 if (bdev == ERR_PTR(-EBUSY)) {
2430 bdev = lookup_bdev(strim(path));
2431 mutex_lock(&bch_register_lock);
2432 if (!IS_ERR(bdev) && bch_is_open(bdev))
2433 err = "device already registered";
2434 else
2435 err = "device busy";
2436 mutex_unlock(&bch_register_lock);
2437 if (!IS_ERR(bdev))
2438 bdput(bdev);
2439 if (attr == &ksysfs_register_quiet)
2440 goto done;
2441 }
2442 goto out_free_sb;
2443 }
2444
2445 err = "failed to set blocksize";
2446 if (set_blocksize(bdev, 4096))
2447 goto out_blkdev_put;
2448
2449 err = read_super(sb, bdev, &sb_page);
2450 if (err)
2451 goto out_blkdev_put;
2452
2453 err = "failed to register device";
2454 if (SB_IS_BDEV(sb)) {
2455 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
2456
2457 if (!dc)
2458 goto out_put_sb_page;
2459
2460 mutex_lock(&bch_register_lock);
2461 ret = register_bdev(sb, sb_page, bdev, dc);
2462 mutex_unlock(&bch_register_lock);
2463 /* blkdev_put() will be called in cached_dev_free() */
2464 if (ret < 0) {
2465 bdev = NULL;
2466 goto out_put_sb_page;
2467 }
2468 } else {
2469 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2470
2471 if (!ca)
2472 goto out_put_sb_page;
2473
2474 /* blkdev_put() will be called in bch_cache_release() */
2475 if (register_cache(sb, sb_page, bdev, ca) != 0) {
2476 bdev = NULL;
2477 goto out_put_sb_page;
2478 }
2479 }
2480
2481 put_page(sb_page);
2482done:
2483 kfree(sb);
2484 kfree(path);
2485 module_put(THIS_MODULE);
2486 return size;
2487
2488out_put_sb_page:
2489 put_page(sb_page);
2490out_blkdev_put:
2491 if (bdev)
2492 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2493out_free_sb:
2494 kfree(sb);
2495out_free_path:
2496 kfree(path);
2497 path = NULL;
2498out_module_put:
2499 module_put(THIS_MODULE);
2500out:
2501 pr_info("error %s: %s", path?path:"", err);
2502 return ret;
2503}
2504
2505
2506struct pdev {
2507 struct list_head list;
2508 struct cached_dev *dc;
2509};
2510
2511static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
2512 struct kobj_attribute *attr,
2513 const char *buffer,
2514 size_t size)
2515{
2516 LIST_HEAD(pending_devs);
2517 ssize_t ret = size;
2518 struct cached_dev *dc, *tdc;
2519 struct pdev *pdev, *tpdev;
2520 struct cache_set *c, *tc;
2521
2522 mutex_lock(&bch_register_lock);
2523 list_for_each_entry_safe(dc, tdc, &uncached_devices, list) {
2524 pdev = kmalloc(sizeof(struct pdev), GFP_KERNEL);
2525 if (!pdev)
2526 break;
2527 pdev->dc = dc;
2528 list_add(&pdev->list, &pending_devs);
2529 }
2530
2531 list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
2532 list_for_each_entry_safe(c, tc, &bch_cache_sets, list) {
2533 char *pdev_set_uuid = pdev->dc->sb.set_uuid;
2534 char *set_uuid = c->sb.uuid;
2535
2536 if (!memcmp(pdev_set_uuid, set_uuid, 16)) {
2537 list_del(&pdev->list);
2538 kfree(pdev);
2539 break;
2540 }
2541 }
2542 }
2543 mutex_unlock(&bch_register_lock);
2544
2545 list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
2546 pr_info("delete pdev %p", pdev);
2547 list_del(&pdev->list);
2548 bcache_device_stop(&pdev->dc->disk);
2549 kfree(pdev);
2550 }
2551
2552 return ret;
2553}
2554
2555static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
2556{
2557 if (bcache_is_reboot)
2558 return NOTIFY_DONE;
2559
2560 if (code == SYS_DOWN ||
2561 code == SYS_HALT ||
2562 code == SYS_POWER_OFF) {
2563 DEFINE_WAIT(wait);
2564 unsigned long start = jiffies;
2565 bool stopped = false;
2566
2567 struct cache_set *c, *tc;
2568 struct cached_dev *dc, *tdc;
2569
2570 mutex_lock(&bch_register_lock);
2571
2572 if (bcache_is_reboot)
2573 goto out;
2574
2575 /* New registration is rejected since now */
2576 bcache_is_reboot = true;
2577 /*
2578 * Make registering caller (if there is) on other CPU
2579 * core know bcache_is_reboot set to true earlier
2580 */
2581 smp_mb();
2582
2583 if (list_empty(&bch_cache_sets) &&
2584 list_empty(&uncached_devices))
2585 goto out;
2586
2587 mutex_unlock(&bch_register_lock);
2588
2589 pr_info("Stopping all devices:");
2590
2591 /*
2592 * The reason bch_register_lock is not held to call
2593 * bch_cache_set_stop() and bcache_device_stop() is to
2594 * avoid potential deadlock during reboot, because cache
2595 * set or bcache device stopping process will acqurie
2596 * bch_register_lock too.
2597 *
2598 * We are safe here because bcache_is_reboot sets to
2599 * true already, register_bcache() will reject new
2600 * registration now. bcache_is_reboot also makes sure
2601 * bcache_reboot() won't be re-entered on by other thread,
2602 * so there is no race in following list iteration by
2603 * list_for_each_entry_safe().
2604 */
2605 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2606 bch_cache_set_stop(c);
2607
2608 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
2609 bcache_device_stop(&dc->disk);
2610
2611
2612 /*
2613 * Give an early chance for other kthreads and
2614 * kworkers to stop themselves
2615 */
2616 schedule();
2617
2618 /* What's a condition variable? */
2619 while (1) {
2620 long timeout = start + 10 * HZ - jiffies;
2621
2622 mutex_lock(&bch_register_lock);
2623 stopped = list_empty(&bch_cache_sets) &&
2624 list_empty(&uncached_devices);
2625
2626 if (timeout < 0 || stopped)
2627 break;
2628
2629 prepare_to_wait(&unregister_wait, &wait,
2630 TASK_UNINTERRUPTIBLE);
2631
2632 mutex_unlock(&bch_register_lock);
2633 schedule_timeout(timeout);
2634 }
2635
2636 finish_wait(&unregister_wait, &wait);
2637
2638 if (stopped)
2639 pr_info("All devices stopped");
2640 else
2641 pr_notice("Timeout waiting for devices to be closed");
2642out:
2643 mutex_unlock(&bch_register_lock);
2644 }
2645
2646 return NOTIFY_DONE;
2647}
2648
2649static struct notifier_block reboot = {
2650 .notifier_call = bcache_reboot,
2651 .priority = INT_MAX, /* before any real devices */
2652};
2653
2654static void bcache_exit(void)
2655{
2656 bch_debug_exit();
2657 bch_request_exit();
2658 if (bcache_kobj)
2659 kobject_put(bcache_kobj);
2660 if (bcache_wq)
2661 destroy_workqueue(bcache_wq);
2662 if (bch_journal_wq)
2663 destroy_workqueue(bch_journal_wq);
2664 if (bch_flush_wq)
2665 destroy_workqueue(bch_flush_wq);
2666 bch_btree_exit();
2667
2668 if (bcache_major)
2669 unregister_blkdev(bcache_major, "bcache");
2670 unregister_reboot_notifier(&reboot);
2671 mutex_destroy(&bch_register_lock);
2672}
2673
2674/* Check and fixup module parameters */
2675static void check_module_parameters(void)
2676{
2677 if (bch_cutoff_writeback_sync == 0)
2678 bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC;
2679 else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) {
2680 pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u",
2681 bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX);
2682 bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX;
2683 }
2684
2685 if (bch_cutoff_writeback == 0)
2686 bch_cutoff_writeback = CUTOFF_WRITEBACK;
2687 else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) {
2688 pr_warn("set bch_cutoff_writeback (%u) to max value %u",
2689 bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX);
2690 bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX;
2691 }
2692
2693 if (bch_cutoff_writeback > bch_cutoff_writeback_sync) {
2694 pr_warn("set bch_cutoff_writeback (%u) to %u",
2695 bch_cutoff_writeback, bch_cutoff_writeback_sync);
2696 bch_cutoff_writeback = bch_cutoff_writeback_sync;
2697 }
2698}
2699
2700static int __init bcache_init(void)
2701{
2702 static const struct attribute *files[] = {
2703 &ksysfs_register.attr,
2704 &ksysfs_register_quiet.attr,
2705 &ksysfs_pendings_cleanup.attr,
2706 NULL
2707 };
2708
2709 check_module_parameters();
2710
2711 mutex_init(&bch_register_lock);
2712 init_waitqueue_head(&unregister_wait);
2713 register_reboot_notifier(&reboot);
2714
2715 bcache_major = register_blkdev(0, "bcache");
2716 if (bcache_major < 0) {
2717 unregister_reboot_notifier(&reboot);
2718 mutex_destroy(&bch_register_lock);
2719 return bcache_major;
2720 }
2721
2722 if (bch_btree_init())
2723 goto err;
2724
2725 bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0);
2726 if (!bcache_wq)
2727 goto err;
2728
2729 /*
2730 * Let's not make this `WQ_MEM_RECLAIM` for the following reasons:
2731 *
2732 * 1. It used `system_wq` before which also does no memory reclaim.
2733 * 2. With `WQ_MEM_RECLAIM` desktop stalls, increased boot times, and
2734 * reduced throughput can be observed.
2735 *
2736 * We still want to user our own queue to not congest the `system_wq`.
2737 */
2738 bch_flush_wq = alloc_workqueue("bch_flush", 0, 0);
2739 if (!bch_flush_wq)
2740 goto err;
2741
2742 bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0);
2743 if (!bch_journal_wq)
2744 goto err;
2745
2746 bcache_kobj = kobject_create_and_add("bcache", fs_kobj);
2747 if (!bcache_kobj)
2748 goto err;
2749
2750 if (bch_request_init() ||
2751 sysfs_create_files(bcache_kobj, files))
2752 goto err;
2753
2754 bch_debug_init();
2755 closure_debug_init();
2756
2757 bcache_is_reboot = false;
2758
2759 return 0;
2760err:
2761 bcache_exit();
2762 return -ENOMEM;
2763}
2764
2765/*
2766 * Module hooks
2767 */
2768module_exit(bcache_exit);
2769module_init(bcache_init);
2770
2771module_param(bch_cutoff_writeback, uint, 0);
2772MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback");
2773
2774module_param(bch_cutoff_writeback_sync, uint, 0);
2775MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback");
2776
2777MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
2778MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
2779MODULE_LICENSE("GPL");