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