1 // SPDX-License-Identifier: GPL-2.0
3 * bcache setup/teardown code, and some metadata io - read a superblock and
4 * figure out what to do with it.
6 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
7 * Copyright 2012 Google, Inc.
15 #include "writeback.h"
17 #include <linux/blkdev.h>
18 #include <linux/debugfs.h>
19 #include <linux/genhd.h>
20 #include <linux/idr.h>
21 #include <linux/kthread.h>
22 #include <linux/module.h>
23 #include <linux/random.h>
24 #include <linux/reboot.h>
25 #include <linux/sysfs.h>
27 unsigned int bch_cutoff_writeback
;
28 unsigned int bch_cutoff_writeback_sync
;
30 static const char bcache_magic
[] = {
31 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
32 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
35 static const char invalid_uuid
[] = {
36 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
37 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
40 static struct kobject
*bcache_kobj
;
41 struct mutex bch_register_lock
;
42 bool bcache_is_reboot
;
43 LIST_HEAD(bch_cache_sets
);
44 static LIST_HEAD(uncached_devices
);
46 static int bcache_major
;
47 static DEFINE_IDA(bcache_device_idx
);
48 static wait_queue_head_t unregister_wait
;
49 struct workqueue_struct
*bcache_wq
;
50 struct workqueue_struct
*bch_journal_wq
;
53 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
54 /* limitation of partitions number on single bcache device */
55 #define BCACHE_MINORS 128
56 /* limitation of bcache devices number on single system */
57 #define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS)
61 static const char *read_super(struct cache_sb
*sb
, struct block_device
*bdev
,
62 struct cache_sb_disk
**res
)
65 struct cache_sb_disk
*s
;
69 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
70 SB_OFFSET
>> PAGE_SHIFT
, GFP_KERNEL
);
73 s
= page_address(page
) + offset_in_page(SB_OFFSET
);
75 sb
->offset
= le64_to_cpu(s
->offset
);
76 sb
->version
= le64_to_cpu(s
->version
);
78 memcpy(sb
->magic
, s
->magic
, 16);
79 memcpy(sb
->uuid
, s
->uuid
, 16);
80 memcpy(sb
->set_uuid
, s
->set_uuid
, 16);
81 memcpy(sb
->label
, s
->label
, SB_LABEL_SIZE
);
83 sb
->flags
= le64_to_cpu(s
->flags
);
84 sb
->seq
= le64_to_cpu(s
->seq
);
85 sb
->last_mount
= le32_to_cpu(s
->last_mount
);
86 sb
->first_bucket
= le16_to_cpu(s
->first_bucket
);
87 sb
->keys
= le16_to_cpu(s
->keys
);
89 for (i
= 0; i
< SB_JOURNAL_BUCKETS
; i
++)
90 sb
->d
[i
] = le64_to_cpu(s
->d
[i
]);
92 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
93 sb
->version
, sb
->flags
, sb
->seq
, sb
->keys
);
95 err
= "Not a bcache superblock (bad offset)";
96 if (sb
->offset
!= SB_SECTOR
)
99 err
= "Not a bcache superblock (bad magic)";
100 if (memcmp(sb
->magic
, bcache_magic
, 16))
103 err
= "Too many journal buckets";
104 if (sb
->keys
> SB_JOURNAL_BUCKETS
)
107 err
= "Bad checksum";
108 if (s
->csum
!= csum_set(s
))
112 if (bch_is_zero(sb
->uuid
, 16))
115 sb
->block_size
= le16_to_cpu(s
->block_size
);
117 err
= "Superblock block size smaller than device block size";
118 if (sb
->block_size
<< 9 < bdev_logical_block_size(bdev
))
121 switch (sb
->version
) {
122 case BCACHE_SB_VERSION_BDEV
:
123 sb
->data_offset
= BDEV_DATA_START_DEFAULT
;
125 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET
:
126 sb
->data_offset
= le64_to_cpu(s
->data_offset
);
128 err
= "Bad data offset";
129 if (sb
->data_offset
< BDEV_DATA_START_DEFAULT
)
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
);
138 sb
->nr_in_set
= le16_to_cpu(s
->nr_in_set
);
139 sb
->nr_this_dev
= le16_to_cpu(s
->nr_this_dev
);
141 err
= "Too many buckets";
142 if (sb
->nbuckets
> LONG_MAX
)
145 err
= "Not enough buckets";
146 if (sb
->nbuckets
< 1 << 7)
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
)
156 err
= "Invalid superblock: device too small";
157 if (get_capacity(bdev
->bd_disk
) <
158 sb
->bucket_size
* sb
->nbuckets
)
162 if (bch_is_zero(sb
->set_uuid
, 16))
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
)
171 err
= "Journal buckets not sequential";
172 for (i
= 0; i
< sb
->keys
; i
++)
173 if (sb
->d
[i
] != sb
->first_bucket
+ i
)
176 err
= "Too many journal buckets";
177 if (sb
->first_bucket
+ sb
->keys
> sb
->nbuckets
)
180 err
= "Invalid superblock: first bucket comes before end of super";
181 if (sb
->first_bucket
* sb
->bucket_size
< 16)
186 err
= "Unsupported superblock version";
190 sb
->last_mount
= (u32
)ktime_get_real_seconds();
198 static void write_bdev_super_endio(struct bio
*bio
)
200 struct cached_dev
*dc
= bio
->bi_private
;
203 bch_count_backing_io_errors(dc
, bio
);
205 closure_put(&dc
->sb_write
);
208 static void __write_super(struct cache_sb
*sb
, struct cache_sb_disk
*out
,
213 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_META
;
214 bio
->bi_iter
.bi_sector
= SB_SECTOR
;
215 __bio_add_page(bio
, virt_to_page(out
), SB_SIZE
,
216 offset_in_page(out
));
218 out
->offset
= cpu_to_le64(sb
->offset
);
219 out
->version
= cpu_to_le64(sb
->version
);
221 memcpy(out
->uuid
, sb
->uuid
, 16);
222 memcpy(out
->set_uuid
, sb
->set_uuid
, 16);
223 memcpy(out
->label
, sb
->label
, SB_LABEL_SIZE
);
225 out
->flags
= cpu_to_le64(sb
->flags
);
226 out
->seq
= cpu_to_le64(sb
->seq
);
228 out
->last_mount
= cpu_to_le32(sb
->last_mount
);
229 out
->first_bucket
= cpu_to_le16(sb
->first_bucket
);
230 out
->keys
= cpu_to_le16(sb
->keys
);
232 for (i
= 0; i
< sb
->keys
; i
++)
233 out
->d
[i
] = cpu_to_le64(sb
->d
[i
]);
235 out
->csum
= csum_set(out
);
237 pr_debug("ver %llu, flags %llu, seq %llu",
238 sb
->version
, sb
->flags
, sb
->seq
);
243 static void bch_write_bdev_super_unlock(struct closure
*cl
)
245 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, sb_write
);
247 up(&dc
->sb_write_mutex
);
250 void bch_write_bdev_super(struct cached_dev
*dc
, struct closure
*parent
)
252 struct closure
*cl
= &dc
->sb_write
;
253 struct bio
*bio
= &dc
->sb_bio
;
255 down(&dc
->sb_write_mutex
);
256 closure_init(cl
, parent
);
258 bio_init(bio
, dc
->sb_bv
, 1);
259 bio_set_dev(bio
, dc
->bdev
);
260 bio
->bi_end_io
= write_bdev_super_endio
;
261 bio
->bi_private
= dc
;
264 /* I/O request sent to backing device */
265 __write_super(&dc
->sb
, dc
->sb_disk
, bio
);
267 closure_return_with_destructor(cl
, bch_write_bdev_super_unlock
);
270 static void write_super_endio(struct bio
*bio
)
272 struct cache
*ca
= bio
->bi_private
;
275 bch_count_io_errors(ca
, bio
->bi_status
, 0,
276 "writing superblock");
277 closure_put(&ca
->set
->sb_write
);
280 static void bcache_write_super_unlock(struct closure
*cl
)
282 struct cache_set
*c
= container_of(cl
, struct cache_set
, sb_write
);
284 up(&c
->sb_write_mutex
);
287 void bcache_write_super(struct cache_set
*c
)
289 struct closure
*cl
= &c
->sb_write
;
293 down(&c
->sb_write_mutex
);
294 closure_init(cl
, &c
->cl
);
298 for_each_cache(ca
, c
, i
) {
299 struct bio
*bio
= &ca
->sb_bio
;
301 ca
->sb
.version
= BCACHE_SB_VERSION_CDEV_WITH_UUID
;
302 ca
->sb
.seq
= c
->sb
.seq
;
303 ca
->sb
.last_mount
= c
->sb
.last_mount
;
305 SET_CACHE_SYNC(&ca
->sb
, CACHE_SYNC(&c
->sb
));
307 bio_init(bio
, ca
->sb_bv
, 1);
308 bio_set_dev(bio
, ca
->bdev
);
309 bio
->bi_end_io
= write_super_endio
;
310 bio
->bi_private
= ca
;
313 __write_super(&ca
->sb
, ca
->sb_disk
, bio
);
316 closure_return_with_destructor(cl
, bcache_write_super_unlock
);
321 static void uuid_endio(struct bio
*bio
)
323 struct closure
*cl
= bio
->bi_private
;
324 struct cache_set
*c
= container_of(cl
, struct cache_set
, uuid_write
);
326 cache_set_err_on(bio
->bi_status
, c
, "accessing uuids");
327 bch_bbio_free(bio
, c
);
331 static void uuid_io_unlock(struct closure
*cl
)
333 struct cache_set
*c
= container_of(cl
, struct cache_set
, uuid_write
);
335 up(&c
->uuid_write_mutex
);
338 static void uuid_io(struct cache_set
*c
, int op
, unsigned long op_flags
,
339 struct bkey
*k
, struct closure
*parent
)
341 struct closure
*cl
= &c
->uuid_write
;
342 struct uuid_entry
*u
;
347 down(&c
->uuid_write_mutex
);
348 closure_init(cl
, parent
);
350 for (i
= 0; i
< KEY_PTRS(k
); i
++) {
351 struct bio
*bio
= bch_bbio_alloc(c
);
353 bio
->bi_opf
= REQ_SYNC
| REQ_META
| op_flags
;
354 bio
->bi_iter
.bi_size
= KEY_SIZE(k
) << 9;
356 bio
->bi_end_io
= uuid_endio
;
357 bio
->bi_private
= cl
;
358 bio_set_op_attrs(bio
, op
, REQ_SYNC
|REQ_META
|op_flags
);
359 bch_bio_map(bio
, c
->uuids
);
361 bch_submit_bbio(bio
, c
, k
, i
);
363 if (op
!= REQ_OP_WRITE
)
367 bch_extent_to_text(buf
, sizeof(buf
), k
);
368 pr_debug("%s UUIDs at %s", op
== REQ_OP_WRITE
? "wrote" : "read", buf
);
370 for (u
= c
->uuids
; u
< c
->uuids
+ c
->nr_uuids
; u
++)
371 if (!bch_is_zero(u
->uuid
, 16))
372 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
373 u
- c
->uuids
, u
->uuid
, u
->label
,
374 u
->first_reg
, u
->last_reg
, u
->invalidated
);
376 closure_return_with_destructor(cl
, uuid_io_unlock
);
379 static char *uuid_read(struct cache_set
*c
, struct jset
*j
, struct closure
*cl
)
381 struct bkey
*k
= &j
->uuid_bucket
;
383 if (__bch_btree_ptr_invalid(c
, k
))
384 return "bad uuid pointer";
386 bkey_copy(&c
->uuid_bucket
, k
);
387 uuid_io(c
, REQ_OP_READ
, 0, k
, cl
);
389 if (j
->version
< BCACHE_JSET_VERSION_UUIDv1
) {
390 struct uuid_entry_v0
*u0
= (void *) c
->uuids
;
391 struct uuid_entry
*u1
= (void *) c
->uuids
;
397 * Since the new uuid entry is bigger than the old, we have to
398 * convert starting at the highest memory address and work down
399 * in order to do it in place
402 for (i
= c
->nr_uuids
- 1;
405 memcpy(u1
[i
].uuid
, u0
[i
].uuid
, 16);
406 memcpy(u1
[i
].label
, u0
[i
].label
, 32);
408 u1
[i
].first_reg
= u0
[i
].first_reg
;
409 u1
[i
].last_reg
= u0
[i
].last_reg
;
410 u1
[i
].invalidated
= u0
[i
].invalidated
;
420 static int __uuid_write(struct cache_set
*c
)
426 closure_init_stack(&cl
);
427 lockdep_assert_held(&bch_register_lock
);
429 if (bch_bucket_alloc_set(c
, RESERVE_BTREE
, &k
.key
, 1, true))
432 SET_KEY_SIZE(&k
.key
, c
->sb
.bucket_size
);
433 uuid_io(c
, REQ_OP_WRITE
, 0, &k
.key
, &cl
);
436 /* Only one bucket used for uuid write */
437 ca
= PTR_CACHE(c
, &k
.key
, 0);
438 atomic_long_add(ca
->sb
.bucket_size
, &ca
->meta_sectors_written
);
440 bkey_copy(&c
->uuid_bucket
, &k
.key
);
445 int bch_uuid_write(struct cache_set
*c
)
447 int ret
= __uuid_write(c
);
450 bch_journal_meta(c
, NULL
);
455 static struct uuid_entry
*uuid_find(struct cache_set
*c
, const char *uuid
)
457 struct uuid_entry
*u
;
460 u
< c
->uuids
+ c
->nr_uuids
; u
++)
461 if (!memcmp(u
->uuid
, uuid
, 16))
467 static struct uuid_entry
*uuid_find_empty(struct cache_set
*c
)
469 static const char zero_uuid
[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
471 return uuid_find(c
, zero_uuid
);
475 * Bucket priorities/gens:
477 * For each bucket, we store on disk its
481 * See alloc.c for an explanation of the gen. The priority is used to implement
482 * lru (and in the future other) cache replacement policies; for most purposes
483 * it's just an opaque integer.
485 * The gens and the priorities don't have a whole lot to do with each other, and
486 * it's actually the gens that must be written out at specific times - it's no
487 * big deal if the priorities don't get written, if we lose them we just reuse
488 * buckets in suboptimal order.
490 * On disk they're stored in a packed array, and in as many buckets are required
491 * to fit them all. The buckets we use to store them form a list; the journal
492 * header points to the first bucket, the first bucket points to the second
495 * This code is used by the allocation code; periodically (whenever it runs out
496 * of buckets to allocate from) the allocation code will invalidate some
497 * buckets, but it can't use those buckets until their new gens are safely on
501 static void prio_endio(struct bio
*bio
)
503 struct cache
*ca
= bio
->bi_private
;
505 cache_set_err_on(bio
->bi_status
, ca
->set
, "accessing priorities");
506 bch_bbio_free(bio
, ca
->set
);
507 closure_put(&ca
->prio
);
510 static void prio_io(struct cache
*ca
, uint64_t bucket
, int op
,
511 unsigned long op_flags
)
513 struct closure
*cl
= &ca
->prio
;
514 struct bio
*bio
= bch_bbio_alloc(ca
->set
);
516 closure_init_stack(cl
);
518 bio
->bi_iter
.bi_sector
= bucket
* ca
->sb
.bucket_size
;
519 bio_set_dev(bio
, ca
->bdev
);
520 bio
->bi_iter
.bi_size
= bucket_bytes(ca
);
522 bio
->bi_end_io
= prio_endio
;
523 bio
->bi_private
= ca
;
524 bio_set_op_attrs(bio
, op
, REQ_SYNC
|REQ_META
|op_flags
);
525 bch_bio_map(bio
, ca
->disk_buckets
);
527 closure_bio_submit(ca
->set
, bio
, &ca
->prio
);
531 int bch_prio_write(struct cache
*ca
, bool wait
)
537 pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu",
538 fifo_used(&ca
->free
[RESERVE_PRIO
]),
539 fifo_used(&ca
->free
[RESERVE_NONE
]),
540 fifo_used(&ca
->free_inc
));
543 * Pre-check if there are enough free buckets. In the non-blocking
544 * scenario it's better to fail early rather than starting to allocate
545 * buckets and do a cleanup later in case of failure.
548 size_t avail
= fifo_used(&ca
->free
[RESERVE_PRIO
]) +
549 fifo_used(&ca
->free
[RESERVE_NONE
]);
550 if (prio_buckets(ca
) > avail
)
554 closure_init_stack(&cl
);
556 lockdep_assert_held(&ca
->set
->bucket_lock
);
558 ca
->disk_buckets
->seq
++;
560 atomic_long_add(ca
->sb
.bucket_size
* prio_buckets(ca
),
561 &ca
->meta_sectors_written
);
563 for (i
= prio_buckets(ca
) - 1; i
>= 0; --i
) {
565 struct prio_set
*p
= ca
->disk_buckets
;
566 struct bucket_disk
*d
= p
->data
;
567 struct bucket_disk
*end
= d
+ prios_per_bucket(ca
);
569 for (b
= ca
->buckets
+ i
* prios_per_bucket(ca
);
570 b
< ca
->buckets
+ ca
->sb
.nbuckets
&& d
< end
;
572 d
->prio
= cpu_to_le16(b
->prio
);
576 p
->next_bucket
= ca
->prio_buckets
[i
+ 1];
577 p
->magic
= pset_magic(&ca
->sb
);
578 p
->csum
= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8);
580 bucket
= bch_bucket_alloc(ca
, RESERVE_PRIO
, wait
);
581 BUG_ON(bucket
== -1);
583 mutex_unlock(&ca
->set
->bucket_lock
);
584 prio_io(ca
, bucket
, REQ_OP_WRITE
, 0);
585 mutex_lock(&ca
->set
->bucket_lock
);
587 ca
->prio_buckets
[i
] = bucket
;
588 atomic_dec_bug(&ca
->buckets
[bucket
].pin
);
591 mutex_unlock(&ca
->set
->bucket_lock
);
593 bch_journal_meta(ca
->set
, &cl
);
596 mutex_lock(&ca
->set
->bucket_lock
);
599 * Don't want the old priorities to get garbage collected until after we
600 * finish writing the new ones, and they're journalled
602 for (i
= 0; i
< prio_buckets(ca
); i
++) {
603 if (ca
->prio_last_buckets
[i
])
604 __bch_bucket_free(ca
,
605 &ca
->buckets
[ca
->prio_last_buckets
[i
]]);
607 ca
->prio_last_buckets
[i
] = ca
->prio_buckets
[i
];
612 static int prio_read(struct cache
*ca
, uint64_t bucket
)
614 struct prio_set
*p
= ca
->disk_buckets
;
615 struct bucket_disk
*d
= p
->data
+ prios_per_bucket(ca
), *end
= d
;
617 unsigned int bucket_nr
= 0;
620 for (b
= ca
->buckets
;
621 b
< ca
->buckets
+ ca
->sb
.nbuckets
;
624 ca
->prio_buckets
[bucket_nr
] = bucket
;
625 ca
->prio_last_buckets
[bucket_nr
] = bucket
;
628 prio_io(ca
, bucket
, REQ_OP_READ
, 0);
631 bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8)) {
632 pr_warn("bad csum reading priorities");
636 if (p
->magic
!= pset_magic(&ca
->sb
)) {
637 pr_warn("bad magic reading priorities");
641 bucket
= p
->next_bucket
;
645 b
->prio
= le16_to_cpu(d
->prio
);
646 b
->gen
= b
->last_gc
= d
->gen
;
656 static int open_dev(struct block_device
*b
, fmode_t mode
)
658 struct bcache_device
*d
= b
->bd_disk
->private_data
;
660 if (test_bit(BCACHE_DEV_CLOSING
, &d
->flags
))
667 static void release_dev(struct gendisk
*b
, fmode_t mode
)
669 struct bcache_device
*d
= b
->private_data
;
674 static int ioctl_dev(struct block_device
*b
, fmode_t mode
,
675 unsigned int cmd
, unsigned long arg
)
677 struct bcache_device
*d
= b
->bd_disk
->private_data
;
679 return d
->ioctl(d
, mode
, cmd
, arg
);
682 static const struct block_device_operations bcache_ops
= {
684 .release
= release_dev
,
686 .owner
= THIS_MODULE
,
689 void bcache_device_stop(struct bcache_device
*d
)
691 if (!test_and_set_bit(BCACHE_DEV_CLOSING
, &d
->flags
))
694 * - cached device: cached_dev_flush()
695 * - flash dev: flash_dev_flush()
697 closure_queue(&d
->cl
);
700 static void bcache_device_unlink(struct bcache_device
*d
)
702 lockdep_assert_held(&bch_register_lock
);
704 if (d
->c
&& !test_and_set_bit(BCACHE_DEV_UNLINK_DONE
, &d
->flags
)) {
708 sysfs_remove_link(&d
->c
->kobj
, d
->name
);
709 sysfs_remove_link(&d
->kobj
, "cache");
711 for_each_cache(ca
, d
->c
, i
)
712 bd_unlink_disk_holder(ca
->bdev
, d
->disk
);
716 static void bcache_device_link(struct bcache_device
*d
, struct cache_set
*c
,
723 for_each_cache(ca
, d
->c
, i
)
724 bd_link_disk_holder(ca
->bdev
, d
->disk
);
726 snprintf(d
->name
, BCACHEDEVNAME_SIZE
,
727 "%s%u", name
, d
->id
);
729 ret
= sysfs_create_link(&d
->kobj
, &c
->kobj
, "cache");
731 pr_err("Couldn't create device -> cache set symlink");
733 ret
= sysfs_create_link(&c
->kobj
, &d
->kobj
, d
->name
);
735 pr_err("Couldn't create cache set -> device symlink");
737 clear_bit(BCACHE_DEV_UNLINK_DONE
, &d
->flags
);
740 static void bcache_device_detach(struct bcache_device
*d
)
742 lockdep_assert_held(&bch_register_lock
);
744 atomic_dec(&d
->c
->attached_dev_nr
);
746 if (test_bit(BCACHE_DEV_DETACHING
, &d
->flags
)) {
747 struct uuid_entry
*u
= d
->c
->uuids
+ d
->id
;
749 SET_UUID_FLASH_ONLY(u
, 0);
750 memcpy(u
->uuid
, invalid_uuid
, 16);
751 u
->invalidated
= cpu_to_le32((u32
)ktime_get_real_seconds());
752 bch_uuid_write(d
->c
);
755 bcache_device_unlink(d
);
757 d
->c
->devices
[d
->id
] = NULL
;
758 closure_put(&d
->c
->caching
);
762 static void bcache_device_attach(struct bcache_device
*d
, struct cache_set
*c
,
769 if (id
>= c
->devices_max_used
)
770 c
->devices_max_used
= id
+ 1;
772 closure_get(&c
->caching
);
775 static inline int first_minor_to_idx(int first_minor
)
777 return (first_minor
/BCACHE_MINORS
);
780 static inline int idx_to_first_minor(int idx
)
782 return (idx
* BCACHE_MINORS
);
785 static void bcache_device_free(struct bcache_device
*d
)
787 struct gendisk
*disk
= d
->disk
;
789 lockdep_assert_held(&bch_register_lock
);
792 pr_info("%s stopped", disk
->disk_name
);
794 pr_err("bcache device (NULL gendisk) stopped");
797 bcache_device_detach(d
);
800 if (disk
->flags
& GENHD_FL_UP
)
804 blk_cleanup_queue(disk
->queue
);
806 ida_simple_remove(&bcache_device_idx
,
807 first_minor_to_idx(disk
->first_minor
));
811 bioset_exit(&d
->bio_split
);
812 kvfree(d
->full_dirty_stripes
);
813 kvfree(d
->stripe_sectors_dirty
);
815 closure_debug_destroy(&d
->cl
);
818 static int bcache_device_init(struct bcache_device
*d
, unsigned int block_size
,
819 sector_t sectors
, make_request_fn make_request_fn
)
821 struct request_queue
*q
;
822 const size_t max_stripes
= min_t(size_t, INT_MAX
,
823 SIZE_MAX
/ sizeof(atomic_t
));
828 d
->stripe_size
= 1 << 31;
830 d
->nr_stripes
= DIV_ROUND_UP_ULL(sectors
, d
->stripe_size
);
832 if (!d
->nr_stripes
|| d
->nr_stripes
> max_stripes
) {
833 pr_err("nr_stripes too large or invalid: %u (start sector beyond end of disk?)",
834 (unsigned int)d
->nr_stripes
);
838 n
= d
->nr_stripes
* sizeof(atomic_t
);
839 d
->stripe_sectors_dirty
= kvzalloc(n
, GFP_KERNEL
);
840 if (!d
->stripe_sectors_dirty
)
843 n
= BITS_TO_LONGS(d
->nr_stripes
) * sizeof(unsigned long);
844 d
->full_dirty_stripes
= kvzalloc(n
, GFP_KERNEL
);
845 if (!d
->full_dirty_stripes
)
848 idx
= ida_simple_get(&bcache_device_idx
, 0,
849 BCACHE_DEVICE_IDX_MAX
, GFP_KERNEL
);
853 if (bioset_init(&d
->bio_split
, 4, offsetof(struct bbio
, bio
),
854 BIOSET_NEED_BVECS
|BIOSET_NEED_RESCUER
))
857 d
->disk
= alloc_disk(BCACHE_MINORS
);
861 set_capacity(d
->disk
, sectors
);
862 snprintf(d
->disk
->disk_name
, DISK_NAME_LEN
, "bcache%i", idx
);
864 d
->disk
->major
= bcache_major
;
865 d
->disk
->first_minor
= idx_to_first_minor(idx
);
866 d
->disk
->fops
= &bcache_ops
;
867 d
->disk
->private_data
= d
;
869 q
= blk_alloc_queue(make_request_fn
, NUMA_NO_NODE
);
875 q
->backing_dev_info
->congested_data
= d
;
876 q
->limits
.max_hw_sectors
= UINT_MAX
;
877 q
->limits
.max_sectors
= UINT_MAX
;
878 q
->limits
.max_segment_size
= UINT_MAX
;
879 q
->limits
.max_segments
= BIO_MAX_PAGES
;
880 blk_queue_max_discard_sectors(q
, UINT_MAX
);
881 q
->limits
.discard_granularity
= 512;
882 q
->limits
.io_min
= block_size
;
883 q
->limits
.logical_block_size
= block_size
;
884 q
->limits
.physical_block_size
= block_size
;
885 blk_queue_flag_set(QUEUE_FLAG_NONROT
, d
->disk
->queue
);
886 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, d
->disk
->queue
);
887 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, d
->disk
->queue
);
889 blk_queue_write_cache(q
, true, true);
894 ida_simple_remove(&bcache_device_idx
, idx
);
901 static void calc_cached_dev_sectors(struct cache_set
*c
)
903 uint64_t sectors
= 0;
904 struct cached_dev
*dc
;
906 list_for_each_entry(dc
, &c
->cached_devs
, list
)
907 sectors
+= bdev_sectors(dc
->bdev
);
909 c
->cached_dev_sectors
= sectors
;
912 #define BACKING_DEV_OFFLINE_TIMEOUT 5
913 static int cached_dev_status_update(void *arg
)
915 struct cached_dev
*dc
= arg
;
916 struct request_queue
*q
;
919 * If this delayed worker is stopping outside, directly quit here.
920 * dc->io_disable might be set via sysfs interface, so check it
923 while (!kthread_should_stop() && !dc
->io_disable
) {
924 q
= bdev_get_queue(dc
->bdev
);
925 if (blk_queue_dying(q
))
926 dc
->offline_seconds
++;
928 dc
->offline_seconds
= 0;
930 if (dc
->offline_seconds
>= BACKING_DEV_OFFLINE_TIMEOUT
) {
931 pr_err("%s: device offline for %d seconds",
932 dc
->backing_dev_name
,
933 BACKING_DEV_OFFLINE_TIMEOUT
);
934 pr_err("%s: disable I/O request due to backing "
935 "device offline", dc
->disk
.name
);
936 dc
->io_disable
= true;
937 /* let others know earlier that io_disable is true */
939 bcache_device_stop(&dc
->disk
);
942 schedule_timeout_interruptible(HZ
);
945 wait_for_kthread_stop();
950 int bch_cached_dev_run(struct cached_dev
*dc
)
952 struct bcache_device
*d
= &dc
->disk
;
953 char *buf
= kmemdup_nul(dc
->sb
.label
, SB_LABEL_SIZE
, GFP_KERNEL
);
956 kasprintf(GFP_KERNEL
, "CACHED_UUID=%pU", dc
->sb
.uuid
),
957 kasprintf(GFP_KERNEL
, "CACHED_LABEL=%s", buf
? : ""),
961 if (dc
->io_disable
) {
962 pr_err("I/O disabled on cached dev %s",
963 dc
->backing_dev_name
);
970 if (atomic_xchg(&dc
->running
, 1)) {
974 pr_info("cached dev %s is running already",
975 dc
->backing_dev_name
);
980 BDEV_STATE(&dc
->sb
) != BDEV_STATE_NONE
) {
983 closure_init_stack(&cl
);
985 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_STALE
);
986 bch_write_bdev_super(dc
, &cl
);
991 bd_link_disk_holder(dc
->bdev
, dc
->disk
.disk
);
993 * won't show up in the uevent file, use udevadm monitor -e instead
994 * only class / kset properties are persistent
996 kobject_uevent_env(&disk_to_dev(d
->disk
)->kobj
, KOBJ_CHANGE
, env
);
1001 if (sysfs_create_link(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "dev") ||
1002 sysfs_create_link(&disk_to_dev(d
->disk
)->kobj
,
1003 &d
->kobj
, "bcache")) {
1004 pr_err("Couldn't create bcache dev <-> disk sysfs symlinks");
1008 dc
->status_update_thread
= kthread_run(cached_dev_status_update
,
1009 dc
, "bcache_status_update");
1010 if (IS_ERR(dc
->status_update_thread
)) {
1011 pr_warn("failed to create bcache_status_update kthread, "
1012 "continue to run without monitoring backing "
1020 * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed
1021 * work dc->writeback_rate_update is running. Wait until the routine
1022 * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to
1023 * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out
1024 * seconds, give up waiting here and continue to cancel it too.
1026 static void cancel_writeback_rate_update_dwork(struct cached_dev
*dc
)
1028 int time_out
= WRITEBACK_RATE_UPDATE_SECS_MAX
* HZ
;
1031 if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING
,
1035 schedule_timeout_interruptible(1);
1036 } while (time_out
> 0);
1039 pr_warn("give up waiting for dc->writeback_write_update to quit");
1041 cancel_delayed_work_sync(&dc
->writeback_rate_update
);
1044 static void cached_dev_detach_finish(struct work_struct
*w
)
1046 struct cached_dev
*dc
= container_of(w
, struct cached_dev
, detach
);
1049 closure_init_stack(&cl
);
1051 BUG_ON(!test_bit(BCACHE_DEV_DETACHING
, &dc
->disk
.flags
));
1052 BUG_ON(refcount_read(&dc
->count
));
1055 if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING
, &dc
->disk
.flags
))
1056 cancel_writeback_rate_update_dwork(dc
);
1058 if (!IS_ERR_OR_NULL(dc
->writeback_thread
)) {
1059 kthread_stop(dc
->writeback_thread
);
1060 dc
->writeback_thread
= NULL
;
1063 memset(&dc
->sb
.set_uuid
, 0, 16);
1064 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_NONE
);
1066 bch_write_bdev_super(dc
, &cl
);
1069 mutex_lock(&bch_register_lock
);
1071 calc_cached_dev_sectors(dc
->disk
.c
);
1072 bcache_device_detach(&dc
->disk
);
1073 list_move(&dc
->list
, &uncached_devices
);
1075 clear_bit(BCACHE_DEV_DETACHING
, &dc
->disk
.flags
);
1076 clear_bit(BCACHE_DEV_UNLINK_DONE
, &dc
->disk
.flags
);
1078 mutex_unlock(&bch_register_lock
);
1080 pr_info("Caching disabled for %s", dc
->backing_dev_name
);
1082 /* Drop ref we took in cached_dev_detach() */
1083 closure_put(&dc
->disk
.cl
);
1086 void bch_cached_dev_detach(struct cached_dev
*dc
)
1088 lockdep_assert_held(&bch_register_lock
);
1090 if (test_bit(BCACHE_DEV_CLOSING
, &dc
->disk
.flags
))
1093 if (test_and_set_bit(BCACHE_DEV_DETACHING
, &dc
->disk
.flags
))
1097 * Block the device from being closed and freed until we're finished
1100 closure_get(&dc
->disk
.cl
);
1102 bch_writeback_queue(dc
);
1107 int bch_cached_dev_attach(struct cached_dev
*dc
, struct cache_set
*c
,
1110 uint32_t rtime
= cpu_to_le32((u32
)ktime_get_real_seconds());
1111 struct uuid_entry
*u
;
1112 struct cached_dev
*exist_dc
, *t
;
1115 if ((set_uuid
&& memcmp(set_uuid
, c
->sb
.set_uuid
, 16)) ||
1116 (!set_uuid
&& memcmp(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16)))
1120 pr_err("Can't attach %s: already attached",
1121 dc
->backing_dev_name
);
1125 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
)) {
1126 pr_err("Can't attach %s: shutting down",
1127 dc
->backing_dev_name
);
1131 if (dc
->sb
.block_size
< c
->sb
.block_size
) {
1133 pr_err("Couldn't attach %s: block size less than set's block size",
1134 dc
->backing_dev_name
);
1138 /* Check whether already attached */
1139 list_for_each_entry_safe(exist_dc
, t
, &c
->cached_devs
, list
) {
1140 if (!memcmp(dc
->sb
.uuid
, exist_dc
->sb
.uuid
, 16)) {
1141 pr_err("Tried to attach %s but duplicate UUID already attached",
1142 dc
->backing_dev_name
);
1148 u
= uuid_find(c
, dc
->sb
.uuid
);
1151 (BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
||
1152 BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
)) {
1153 memcpy(u
->uuid
, invalid_uuid
, 16);
1154 u
->invalidated
= cpu_to_le32((u32
)ktime_get_real_seconds());
1159 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
1160 pr_err("Couldn't find uuid for %s in set",
1161 dc
->backing_dev_name
);
1165 u
= uuid_find_empty(c
);
1167 pr_err("Not caching %s, no room for UUID",
1168 dc
->backing_dev_name
);
1174 * Deadlocks since we're called via sysfs...
1175 * sysfs_remove_file(&dc->kobj, &sysfs_attach);
1178 if (bch_is_zero(u
->uuid
, 16)) {
1181 closure_init_stack(&cl
);
1183 memcpy(u
->uuid
, dc
->sb
.uuid
, 16);
1184 memcpy(u
->label
, dc
->sb
.label
, SB_LABEL_SIZE
);
1185 u
->first_reg
= u
->last_reg
= rtime
;
1188 memcpy(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16);
1189 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_CLEAN
);
1191 bch_write_bdev_super(dc
, &cl
);
1194 u
->last_reg
= rtime
;
1198 bcache_device_attach(&dc
->disk
, c
, u
- c
->uuids
);
1199 list_move(&dc
->list
, &c
->cached_devs
);
1200 calc_cached_dev_sectors(c
);
1203 * dc->c must be set before dc->count != 0 - paired with the mb in
1207 refcount_set(&dc
->count
, 1);
1209 /* Block writeback thread, but spawn it */
1210 down_write(&dc
->writeback_lock
);
1211 if (bch_cached_dev_writeback_start(dc
)) {
1212 up_write(&dc
->writeback_lock
);
1213 pr_err("Couldn't start writeback facilities for %s",
1214 dc
->disk
.disk
->disk_name
);
1218 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
1219 atomic_set(&dc
->has_dirty
, 1);
1220 bch_writeback_queue(dc
);
1223 bch_sectors_dirty_init(&dc
->disk
);
1225 ret
= bch_cached_dev_run(dc
);
1226 if (ret
&& (ret
!= -EBUSY
)) {
1227 up_write(&dc
->writeback_lock
);
1229 * bch_register_lock is held, bcache_device_stop() is not
1230 * able to be directly called. The kthread and kworker
1231 * created previously in bch_cached_dev_writeback_start()
1232 * have to be stopped manually here.
1234 kthread_stop(dc
->writeback_thread
);
1235 cancel_writeback_rate_update_dwork(dc
);
1236 pr_err("Couldn't run cached device %s",
1237 dc
->backing_dev_name
);
1241 bcache_device_link(&dc
->disk
, c
, "bdev");
1242 atomic_inc(&c
->attached_dev_nr
);
1244 /* Allow the writeback thread to proceed */
1245 up_write(&dc
->writeback_lock
);
1247 pr_info("Caching %s as %s on set %pU",
1248 dc
->backing_dev_name
,
1249 dc
->disk
.disk
->disk_name
,
1250 dc
->disk
.c
->sb
.set_uuid
);
1254 /* when dc->disk.kobj released */
1255 void bch_cached_dev_release(struct kobject
*kobj
)
1257 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
1260 module_put(THIS_MODULE
);
1263 static void cached_dev_free(struct closure
*cl
)
1265 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
1267 if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING
, &dc
->disk
.flags
))
1268 cancel_writeback_rate_update_dwork(dc
);
1270 if (!IS_ERR_OR_NULL(dc
->writeback_thread
))
1271 kthread_stop(dc
->writeback_thread
);
1272 if (!IS_ERR_OR_NULL(dc
->status_update_thread
))
1273 kthread_stop(dc
->status_update_thread
);
1275 mutex_lock(&bch_register_lock
);
1277 if (atomic_read(&dc
->running
))
1278 bd_unlink_disk_holder(dc
->bdev
, dc
->disk
.disk
);
1279 bcache_device_free(&dc
->disk
);
1280 list_del(&dc
->list
);
1282 mutex_unlock(&bch_register_lock
);
1285 put_page(virt_to_page(dc
->sb_disk
));
1287 if (!IS_ERR_OR_NULL(dc
->bdev
))
1288 blkdev_put(dc
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1290 wake_up(&unregister_wait
);
1292 kobject_put(&dc
->disk
.kobj
);
1295 static void cached_dev_flush(struct closure
*cl
)
1297 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
1298 struct bcache_device
*d
= &dc
->disk
;
1300 mutex_lock(&bch_register_lock
);
1301 bcache_device_unlink(d
);
1302 mutex_unlock(&bch_register_lock
);
1304 bch_cache_accounting_destroy(&dc
->accounting
);
1305 kobject_del(&d
->kobj
);
1307 continue_at(cl
, cached_dev_free
, system_wq
);
1310 static int cached_dev_init(struct cached_dev
*dc
, unsigned int block_size
)
1314 struct request_queue
*q
= bdev_get_queue(dc
->bdev
);
1316 __module_get(THIS_MODULE
);
1317 INIT_LIST_HEAD(&dc
->list
);
1318 closure_init(&dc
->disk
.cl
, NULL
);
1319 set_closure_fn(&dc
->disk
.cl
, cached_dev_flush
, system_wq
);
1320 kobject_init(&dc
->disk
.kobj
, &bch_cached_dev_ktype
);
1321 INIT_WORK(&dc
->detach
, cached_dev_detach_finish
);
1322 sema_init(&dc
->sb_write_mutex
, 1);
1323 INIT_LIST_HEAD(&dc
->io_lru
);
1324 spin_lock_init(&dc
->io_lock
);
1325 bch_cache_accounting_init(&dc
->accounting
, &dc
->disk
.cl
);
1327 dc
->sequential_cutoff
= 4 << 20;
1329 for (io
= dc
->io
; io
< dc
->io
+ RECENT_IO
; io
++) {
1330 list_add(&io
->lru
, &dc
->io_lru
);
1331 hlist_add_head(&io
->hash
, dc
->io_hash
+ RECENT_IO
);
1334 dc
->disk
.stripe_size
= q
->limits
.io_opt
>> 9;
1336 if (dc
->disk
.stripe_size
)
1337 dc
->partial_stripes_expensive
=
1338 q
->limits
.raid_partial_stripes_expensive
;
1340 ret
= bcache_device_init(&dc
->disk
, block_size
,
1341 dc
->bdev
->bd_part
->nr_sects
- dc
->sb
.data_offset
,
1342 cached_dev_make_request
);
1346 dc
->disk
.disk
->queue
->backing_dev_info
->ra_pages
=
1347 max(dc
->disk
.disk
->queue
->backing_dev_info
->ra_pages
,
1348 q
->backing_dev_info
->ra_pages
);
1350 atomic_set(&dc
->io_errors
, 0);
1351 dc
->io_disable
= false;
1352 dc
->error_limit
= DEFAULT_CACHED_DEV_ERROR_LIMIT
;
1353 /* default to auto */
1354 dc
->stop_when_cache_set_failed
= BCH_CACHED_DEV_STOP_AUTO
;
1356 bch_cached_dev_request_init(dc
);
1357 bch_cached_dev_writeback_init(dc
);
1361 /* Cached device - bcache superblock */
1363 static int register_bdev(struct cache_sb
*sb
, struct cache_sb_disk
*sb_disk
,
1364 struct block_device
*bdev
,
1365 struct cached_dev
*dc
)
1367 const char *err
= "cannot allocate memory";
1368 struct cache_set
*c
;
1371 bdevname(bdev
, dc
->backing_dev_name
);
1372 memcpy(&dc
->sb
, sb
, sizeof(struct cache_sb
));
1374 dc
->bdev
->bd_holder
= dc
;
1375 dc
->sb_disk
= sb_disk
;
1377 if (cached_dev_init(dc
, sb
->block_size
<< 9))
1380 err
= "error creating kobject";
1381 if (kobject_add(&dc
->disk
.kobj
, &part_to_dev(bdev
->bd_part
)->kobj
,
1384 if (bch_cache_accounting_add_kobjs(&dc
->accounting
, &dc
->disk
.kobj
))
1387 pr_info("registered backing device %s", dc
->backing_dev_name
);
1389 list_add(&dc
->list
, &uncached_devices
);
1390 /* attach to a matched cache set if it exists */
1391 list_for_each_entry(c
, &bch_cache_sets
, list
)
1392 bch_cached_dev_attach(dc
, c
, NULL
);
1394 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
||
1395 BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
) {
1396 err
= "failed to run cached device";
1397 ret
= bch_cached_dev_run(dc
);
1404 pr_notice("error %s: %s", dc
->backing_dev_name
, err
);
1405 bcache_device_stop(&dc
->disk
);
1409 /* Flash only volumes */
1411 /* When d->kobj released */
1412 void bch_flash_dev_release(struct kobject
*kobj
)
1414 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
1419 static void flash_dev_free(struct closure
*cl
)
1421 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1423 mutex_lock(&bch_register_lock
);
1424 atomic_long_sub(bcache_dev_sectors_dirty(d
),
1425 &d
->c
->flash_dev_dirty_sectors
);
1426 bcache_device_free(d
);
1427 mutex_unlock(&bch_register_lock
);
1428 kobject_put(&d
->kobj
);
1431 static void flash_dev_flush(struct closure
*cl
)
1433 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1435 mutex_lock(&bch_register_lock
);
1436 bcache_device_unlink(d
);
1437 mutex_unlock(&bch_register_lock
);
1438 kobject_del(&d
->kobj
);
1439 continue_at(cl
, flash_dev_free
, system_wq
);
1442 static int flash_dev_run(struct cache_set
*c
, struct uuid_entry
*u
)
1444 struct bcache_device
*d
= kzalloc(sizeof(struct bcache_device
),
1449 closure_init(&d
->cl
, NULL
);
1450 set_closure_fn(&d
->cl
, flash_dev_flush
, system_wq
);
1452 kobject_init(&d
->kobj
, &bch_flash_dev_ktype
);
1454 if (bcache_device_init(d
, block_bytes(c
), u
->sectors
,
1455 flash_dev_make_request
))
1458 bcache_device_attach(d
, c
, u
- c
->uuids
);
1459 bch_sectors_dirty_init(d
);
1460 bch_flash_dev_request_init(d
);
1463 if (kobject_add(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "bcache"))
1466 bcache_device_link(d
, c
, "volume");
1470 kobject_put(&d
->kobj
);
1474 static int flash_devs_run(struct cache_set
*c
)
1477 struct uuid_entry
*u
;
1480 u
< c
->uuids
+ c
->nr_uuids
&& !ret
;
1482 if (UUID_FLASH_ONLY(u
))
1483 ret
= flash_dev_run(c
, u
);
1488 int bch_flash_dev_create(struct cache_set
*c
, uint64_t size
)
1490 struct uuid_entry
*u
;
1492 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1495 if (!test_bit(CACHE_SET_RUNNING
, &c
->flags
))
1498 u
= uuid_find_empty(c
);
1500 pr_err("Can't create volume, no room for UUID");
1504 get_random_bytes(u
->uuid
, 16);
1505 memset(u
->label
, 0, 32);
1506 u
->first_reg
= u
->last_reg
= cpu_to_le32((u32
)ktime_get_real_seconds());
1508 SET_UUID_FLASH_ONLY(u
, 1);
1509 u
->sectors
= size
>> 9;
1513 return flash_dev_run(c
, u
);
1516 bool bch_cached_dev_error(struct cached_dev
*dc
)
1518 if (!dc
|| test_bit(BCACHE_DEV_CLOSING
, &dc
->disk
.flags
))
1521 dc
->io_disable
= true;
1522 /* make others know io_disable is true earlier */
1525 pr_err("stop %s: too many IO errors on backing device %s\n",
1526 dc
->disk
.disk
->disk_name
, dc
->backing_dev_name
);
1528 bcache_device_stop(&dc
->disk
);
1535 bool bch_cache_set_error(struct cache_set
*c
, const char *fmt
, ...)
1539 if (c
->on_error
!= ON_ERROR_PANIC
&&
1540 test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1543 if (test_and_set_bit(CACHE_SET_IO_DISABLE
, &c
->flags
))
1544 pr_info("CACHE_SET_IO_DISABLE already set");
1547 * XXX: we can be called from atomic context
1548 * acquire_console_sem();
1551 pr_err("bcache: error on %pU: ", c
->sb
.set_uuid
);
1553 va_start(args
, fmt
);
1557 pr_err(", disabling caching\n");
1559 if (c
->on_error
== ON_ERROR_PANIC
)
1560 panic("panic forced after error\n");
1562 bch_cache_set_unregister(c
);
1566 /* When c->kobj released */
1567 void bch_cache_set_release(struct kobject
*kobj
)
1569 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
1572 module_put(THIS_MODULE
);
1575 static void cache_set_free(struct closure
*cl
)
1577 struct cache_set
*c
= container_of(cl
, struct cache_set
, cl
);
1581 debugfs_remove(c
->debug
);
1583 bch_open_buckets_free(c
);
1584 bch_btree_cache_free(c
);
1585 bch_journal_free(c
);
1587 mutex_lock(&bch_register_lock
);
1588 for_each_cache(ca
, c
, i
)
1591 c
->cache
[ca
->sb
.nr_this_dev
] = NULL
;
1592 kobject_put(&ca
->kobj
);
1595 bch_bset_sort_state_free(&c
->sort
);
1596 free_pages((unsigned long) c
->uuids
, ilog2(bucket_pages(c
)));
1598 if (c
->moving_gc_wq
)
1599 destroy_workqueue(c
->moving_gc_wq
);
1600 bioset_exit(&c
->bio_split
);
1601 mempool_exit(&c
->fill_iter
);
1602 mempool_exit(&c
->bio_meta
);
1603 mempool_exit(&c
->search
);
1607 mutex_unlock(&bch_register_lock
);
1609 pr_info("Cache set %pU unregistered", c
->sb
.set_uuid
);
1610 wake_up(&unregister_wait
);
1612 closure_debug_destroy(&c
->cl
);
1613 kobject_put(&c
->kobj
);
1616 static void cache_set_flush(struct closure
*cl
)
1618 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1623 bch_cache_accounting_destroy(&c
->accounting
);
1625 kobject_put(&c
->internal
);
1626 kobject_del(&c
->kobj
);
1628 if (!IS_ERR_OR_NULL(c
->gc_thread
))
1629 kthread_stop(c
->gc_thread
);
1631 if (!IS_ERR_OR_NULL(c
->root
))
1632 list_add(&c
->root
->list
, &c
->btree_cache
);
1635 * Avoid flushing cached nodes if cache set is retiring
1636 * due to too many I/O errors detected.
1638 if (!test_bit(CACHE_SET_IO_DISABLE
, &c
->flags
))
1639 list_for_each_entry(b
, &c
->btree_cache
, list
) {
1640 mutex_lock(&b
->write_lock
);
1641 if (btree_node_dirty(b
))
1642 __bch_btree_node_write(b
, NULL
);
1643 mutex_unlock(&b
->write_lock
);
1646 for_each_cache(ca
, c
, i
)
1647 if (ca
->alloc_thread
)
1648 kthread_stop(ca
->alloc_thread
);
1650 if (c
->journal
.cur
) {
1651 cancel_delayed_work_sync(&c
->journal
.work
);
1652 /* flush last journal entry if needed */
1653 c
->journal
.work
.work
.func(&c
->journal
.work
.work
);
1660 * This function is only called when CACHE_SET_IO_DISABLE is set, which means
1661 * cache set is unregistering due to too many I/O errors. In this condition,
1662 * the bcache device might be stopped, it depends on stop_when_cache_set_failed
1663 * value and whether the broken cache has dirty data:
1665 * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device
1666 * BCH_CACHED_STOP_AUTO 0 NO
1667 * BCH_CACHED_STOP_AUTO 1 YES
1668 * BCH_CACHED_DEV_STOP_ALWAYS 0 YES
1669 * BCH_CACHED_DEV_STOP_ALWAYS 1 YES
1671 * The expected behavior is, if stop_when_cache_set_failed is configured to
1672 * "auto" via sysfs interface, the bcache device will not be stopped if the
1673 * backing device is clean on the broken cache device.
1675 static void conditional_stop_bcache_device(struct cache_set
*c
,
1676 struct bcache_device
*d
,
1677 struct cached_dev
*dc
)
1679 if (dc
->stop_when_cache_set_failed
== BCH_CACHED_DEV_STOP_ALWAYS
) {
1680 pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.",
1681 d
->disk
->disk_name
, c
->sb
.set_uuid
);
1682 bcache_device_stop(d
);
1683 } else if (atomic_read(&dc
->has_dirty
)) {
1685 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1686 * and dc->has_dirty == 1
1688 pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.",
1689 d
->disk
->disk_name
);
1691 * There might be a small time gap that cache set is
1692 * released but bcache device is not. Inside this time
1693 * gap, regular I/O requests will directly go into
1694 * backing device as no cache set attached to. This
1695 * behavior may also introduce potential inconsistence
1696 * data in writeback mode while cache is dirty.
1697 * Therefore before calling bcache_device_stop() due
1698 * to a broken cache device, dc->io_disable should be
1699 * explicitly set to true.
1701 dc
->io_disable
= true;
1702 /* make others know io_disable is true earlier */
1704 bcache_device_stop(d
);
1707 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1708 * and dc->has_dirty == 0
1710 pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.",
1711 d
->disk
->disk_name
);
1715 static void __cache_set_unregister(struct closure
*cl
)
1717 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1718 struct cached_dev
*dc
;
1719 struct bcache_device
*d
;
1722 mutex_lock(&bch_register_lock
);
1724 for (i
= 0; i
< c
->devices_max_used
; i
++) {
1729 if (!UUID_FLASH_ONLY(&c
->uuids
[i
]) &&
1730 test_bit(CACHE_SET_UNREGISTERING
, &c
->flags
)) {
1731 dc
= container_of(d
, struct cached_dev
, disk
);
1732 bch_cached_dev_detach(dc
);
1733 if (test_bit(CACHE_SET_IO_DISABLE
, &c
->flags
))
1734 conditional_stop_bcache_device(c
, d
, dc
);
1736 bcache_device_stop(d
);
1740 mutex_unlock(&bch_register_lock
);
1742 continue_at(cl
, cache_set_flush
, system_wq
);
1745 void bch_cache_set_stop(struct cache_set
*c
)
1747 if (!test_and_set_bit(CACHE_SET_STOPPING
, &c
->flags
))
1748 /* closure_fn set to __cache_set_unregister() */
1749 closure_queue(&c
->caching
);
1752 void bch_cache_set_unregister(struct cache_set
*c
)
1754 set_bit(CACHE_SET_UNREGISTERING
, &c
->flags
);
1755 bch_cache_set_stop(c
);
1758 #define alloc_bucket_pages(gfp, c) \
1759 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1761 struct cache_set
*bch_cache_set_alloc(struct cache_sb
*sb
)
1764 struct cache_set
*c
= kzalloc(sizeof(struct cache_set
), GFP_KERNEL
);
1769 __module_get(THIS_MODULE
);
1770 closure_init(&c
->cl
, NULL
);
1771 set_closure_fn(&c
->cl
, cache_set_free
, system_wq
);
1773 closure_init(&c
->caching
, &c
->cl
);
1774 set_closure_fn(&c
->caching
, __cache_set_unregister
, system_wq
);
1776 /* Maybe create continue_at_noreturn() and use it here? */
1777 closure_set_stopped(&c
->cl
);
1778 closure_put(&c
->cl
);
1780 kobject_init(&c
->kobj
, &bch_cache_set_ktype
);
1781 kobject_init(&c
->internal
, &bch_cache_set_internal_ktype
);
1783 bch_cache_accounting_init(&c
->accounting
, &c
->cl
);
1785 memcpy(c
->sb
.set_uuid
, sb
->set_uuid
, 16);
1786 c
->sb
.block_size
= sb
->block_size
;
1787 c
->sb
.bucket_size
= sb
->bucket_size
;
1788 c
->sb
.nr_in_set
= sb
->nr_in_set
;
1789 c
->sb
.last_mount
= sb
->last_mount
;
1790 c
->bucket_bits
= ilog2(sb
->bucket_size
);
1791 c
->block_bits
= ilog2(sb
->block_size
);
1792 c
->nr_uuids
= bucket_bytes(c
) / sizeof(struct uuid_entry
);
1793 c
->devices_max_used
= 0;
1794 atomic_set(&c
->attached_dev_nr
, 0);
1795 c
->btree_pages
= bucket_pages(c
);
1796 if (c
->btree_pages
> BTREE_MAX_PAGES
)
1797 c
->btree_pages
= max_t(int, c
->btree_pages
/ 4,
1800 sema_init(&c
->sb_write_mutex
, 1);
1801 mutex_init(&c
->bucket_lock
);
1802 init_waitqueue_head(&c
->btree_cache_wait
);
1803 spin_lock_init(&c
->btree_cannibalize_lock
);
1804 init_waitqueue_head(&c
->bucket_wait
);
1805 init_waitqueue_head(&c
->gc_wait
);
1806 sema_init(&c
->uuid_write_mutex
, 1);
1808 spin_lock_init(&c
->btree_gc_time
.lock
);
1809 spin_lock_init(&c
->btree_split_time
.lock
);
1810 spin_lock_init(&c
->btree_read_time
.lock
);
1812 bch_moving_init_cache_set(c
);
1814 INIT_LIST_HEAD(&c
->list
);
1815 INIT_LIST_HEAD(&c
->cached_devs
);
1816 INIT_LIST_HEAD(&c
->btree_cache
);
1817 INIT_LIST_HEAD(&c
->btree_cache_freeable
);
1818 INIT_LIST_HEAD(&c
->btree_cache_freed
);
1819 INIT_LIST_HEAD(&c
->data_buckets
);
1821 iter_size
= (sb
->bucket_size
/ sb
->block_size
+ 1) *
1822 sizeof(struct btree_iter_set
);
1824 if (!(c
->devices
= kcalloc(c
->nr_uuids
, sizeof(void *), GFP_KERNEL
)) ||
1825 mempool_init_slab_pool(&c
->search
, 32, bch_search_cache
) ||
1826 mempool_init_kmalloc_pool(&c
->bio_meta
, 2,
1827 sizeof(struct bbio
) + sizeof(struct bio_vec
) *
1829 mempool_init_kmalloc_pool(&c
->fill_iter
, 1, iter_size
) ||
1830 bioset_init(&c
->bio_split
, 4, offsetof(struct bbio
, bio
),
1831 BIOSET_NEED_BVECS
|BIOSET_NEED_RESCUER
) ||
1832 !(c
->uuids
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1833 !(c
->moving_gc_wq
= alloc_workqueue("bcache_gc",
1834 WQ_MEM_RECLAIM
, 0)) ||
1835 bch_journal_alloc(c
) ||
1836 bch_btree_cache_alloc(c
) ||
1837 bch_open_buckets_alloc(c
) ||
1838 bch_bset_sort_state_init(&c
->sort
, ilog2(c
->btree_pages
)))
1841 c
->congested_read_threshold_us
= 2000;
1842 c
->congested_write_threshold_us
= 20000;
1843 c
->error_limit
= DEFAULT_IO_ERROR_LIMIT
;
1844 c
->idle_max_writeback_rate_enabled
= 1;
1845 WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE
, &c
->flags
));
1849 bch_cache_set_unregister(c
);
1853 static int run_cache_set(struct cache_set
*c
)
1855 const char *err
= "cannot allocate memory";
1856 struct cached_dev
*dc
, *t
;
1861 struct journal_replay
*l
;
1863 closure_init_stack(&cl
);
1865 for_each_cache(ca
, c
, i
)
1866 c
->nbuckets
+= ca
->sb
.nbuckets
;
1869 if (CACHE_SYNC(&c
->sb
)) {
1873 err
= "cannot allocate memory for journal";
1874 if (bch_journal_read(c
, &journal
))
1877 pr_debug("btree_journal_read() done");
1879 err
= "no journal entries found";
1880 if (list_empty(&journal
))
1883 j
= &list_entry(journal
.prev
, struct journal_replay
, list
)->j
;
1885 err
= "IO error reading priorities";
1886 for_each_cache(ca
, c
, i
) {
1887 if (prio_read(ca
, j
->prio_bucket
[ca
->sb
.nr_this_dev
]))
1892 * If prio_read() fails it'll call cache_set_error and we'll
1893 * tear everything down right away, but if we perhaps checked
1894 * sooner we could avoid journal replay.
1899 err
= "bad btree root";
1900 if (__bch_btree_ptr_invalid(c
, k
))
1903 err
= "error reading btree root";
1904 c
->root
= bch_btree_node_get(c
, NULL
, k
,
1907 if (IS_ERR_OR_NULL(c
->root
))
1910 list_del_init(&c
->root
->list
);
1911 rw_unlock(true, c
->root
);
1913 err
= uuid_read(c
, j
, &cl
);
1917 err
= "error in recovery";
1918 if (bch_btree_check(c
))
1921 bch_journal_mark(c
, &journal
);
1922 bch_initial_gc_finish(c
);
1923 pr_debug("btree_check() done");
1926 * bcache_journal_next() can't happen sooner, or
1927 * btree_gc_finish() will give spurious errors about last_gc >
1928 * gc_gen - this is a hack but oh well.
1930 bch_journal_next(&c
->journal
);
1932 err
= "error starting allocator thread";
1933 for_each_cache(ca
, c
, i
)
1934 if (bch_cache_allocator_start(ca
))
1938 * First place it's safe to allocate: btree_check() and
1939 * btree_gc_finish() have to run before we have buckets to
1940 * allocate, and bch_bucket_alloc_set() might cause a journal
1941 * entry to be written so bcache_journal_next() has to be called
1944 * If the uuids were in the old format we have to rewrite them
1945 * before the next journal entry is written:
1947 if (j
->version
< BCACHE_JSET_VERSION_UUID
)
1950 err
= "bcache: replay journal failed";
1951 if (bch_journal_replay(c
, &journal
))
1954 pr_notice("invalidating existing data");
1956 for_each_cache(ca
, c
, i
) {
1959 ca
->sb
.keys
= clamp_t(int, ca
->sb
.nbuckets
>> 7,
1960 2, SB_JOURNAL_BUCKETS
);
1962 for (j
= 0; j
< ca
->sb
.keys
; j
++)
1963 ca
->sb
.d
[j
] = ca
->sb
.first_bucket
+ j
;
1966 bch_initial_gc_finish(c
);
1968 err
= "error starting allocator thread";
1969 for_each_cache(ca
, c
, i
)
1970 if (bch_cache_allocator_start(ca
))
1973 mutex_lock(&c
->bucket_lock
);
1974 for_each_cache(ca
, c
, i
)
1975 bch_prio_write(ca
, true);
1976 mutex_unlock(&c
->bucket_lock
);
1978 err
= "cannot allocate new UUID bucket";
1979 if (__uuid_write(c
))
1982 err
= "cannot allocate new btree root";
1983 c
->root
= __bch_btree_node_alloc(c
, NULL
, 0, true, NULL
);
1984 if (IS_ERR_OR_NULL(c
->root
))
1987 mutex_lock(&c
->root
->write_lock
);
1988 bkey_copy_key(&c
->root
->key
, &MAX_KEY
);
1989 bch_btree_node_write(c
->root
, &cl
);
1990 mutex_unlock(&c
->root
->write_lock
);
1992 bch_btree_set_root(c
->root
);
1993 rw_unlock(true, c
->root
);
1996 * We don't want to write the first journal entry until
1997 * everything is set up - fortunately journal entries won't be
1998 * written until the SET_CACHE_SYNC() here:
2000 SET_CACHE_SYNC(&c
->sb
, true);
2002 bch_journal_next(&c
->journal
);
2003 bch_journal_meta(c
, &cl
);
2006 err
= "error starting gc thread";
2007 if (bch_gc_thread_start(c
))
2011 c
->sb
.last_mount
= (u32
)ktime_get_real_seconds();
2012 bcache_write_super(c
);
2014 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
2015 bch_cached_dev_attach(dc
, c
, NULL
);
2019 set_bit(CACHE_SET_RUNNING
, &c
->flags
);
2022 while (!list_empty(&journal
)) {
2023 l
= list_first_entry(&journal
, struct journal_replay
, list
);
2030 bch_cache_set_error(c
, "%s", err
);
2035 static bool can_attach_cache(struct cache
*ca
, struct cache_set
*c
)
2037 return ca
->sb
.block_size
== c
->sb
.block_size
&&
2038 ca
->sb
.bucket_size
== c
->sb
.bucket_size
&&
2039 ca
->sb
.nr_in_set
== c
->sb
.nr_in_set
;
2042 static const char *register_cache_set(struct cache
*ca
)
2045 const char *err
= "cannot allocate memory";
2046 struct cache_set
*c
;
2048 list_for_each_entry(c
, &bch_cache_sets
, list
)
2049 if (!memcmp(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16)) {
2050 if (c
->cache
[ca
->sb
.nr_this_dev
])
2051 return "duplicate cache set member";
2053 if (!can_attach_cache(ca
, c
))
2054 return "cache sb does not match set";
2056 if (!CACHE_SYNC(&ca
->sb
))
2057 SET_CACHE_SYNC(&c
->sb
, false);
2062 c
= bch_cache_set_alloc(&ca
->sb
);
2066 err
= "error creating kobject";
2067 if (kobject_add(&c
->kobj
, bcache_kobj
, "%pU", c
->sb
.set_uuid
) ||
2068 kobject_add(&c
->internal
, &c
->kobj
, "internal"))
2071 if (bch_cache_accounting_add_kobjs(&c
->accounting
, &c
->kobj
))
2074 bch_debug_init_cache_set(c
);
2076 list_add(&c
->list
, &bch_cache_sets
);
2078 sprintf(buf
, "cache%i", ca
->sb
.nr_this_dev
);
2079 if (sysfs_create_link(&ca
->kobj
, &c
->kobj
, "set") ||
2080 sysfs_create_link(&c
->kobj
, &ca
->kobj
, buf
))
2083 if (ca
->sb
.seq
> c
->sb
.seq
) {
2084 c
->sb
.version
= ca
->sb
.version
;
2085 memcpy(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16);
2086 c
->sb
.flags
= ca
->sb
.flags
;
2087 c
->sb
.seq
= ca
->sb
.seq
;
2088 pr_debug("set version = %llu", c
->sb
.version
);
2091 kobject_get(&ca
->kobj
);
2093 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = ca
;
2094 c
->cache_by_alloc
[c
->caches_loaded
++] = ca
;
2096 if (c
->caches_loaded
== c
->sb
.nr_in_set
) {
2097 err
= "failed to run cache set";
2098 if (run_cache_set(c
) < 0)
2104 bch_cache_set_unregister(c
);
2110 /* When ca->kobj released */
2111 void bch_cache_release(struct kobject
*kobj
)
2113 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
2117 BUG_ON(ca
->set
->cache
[ca
->sb
.nr_this_dev
] != ca
);
2118 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = NULL
;
2121 free_pages((unsigned long) ca
->disk_buckets
, ilog2(bucket_pages(ca
)));
2122 kfree(ca
->prio_buckets
);
2125 free_heap(&ca
->heap
);
2126 free_fifo(&ca
->free_inc
);
2128 for (i
= 0; i
< RESERVE_NR
; i
++)
2129 free_fifo(&ca
->free
[i
]);
2132 put_page(virt_to_page(ca
->sb_disk
));
2134 if (!IS_ERR_OR_NULL(ca
->bdev
))
2135 blkdev_put(ca
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2138 module_put(THIS_MODULE
);
2141 static int cache_alloc(struct cache
*ca
)
2144 size_t btree_buckets
;
2147 const char *err
= NULL
;
2149 __module_get(THIS_MODULE
);
2150 kobject_init(&ca
->kobj
, &bch_cache_ktype
);
2152 bio_init(&ca
->journal
.bio
, ca
->journal
.bio
.bi_inline_vecs
, 8);
2155 * when ca->sb.njournal_buckets is not zero, journal exists,
2156 * and in bch_journal_replay(), tree node may split,
2157 * so bucket of RESERVE_BTREE type is needed,
2158 * the worst situation is all journal buckets are valid journal,
2159 * and all the keys need to replay,
2160 * so the number of RESERVE_BTREE type buckets should be as much
2161 * as journal buckets
2163 btree_buckets
= ca
->sb
.njournal_buckets
?: 8;
2164 free
= roundup_pow_of_two(ca
->sb
.nbuckets
) >> 10;
2167 err
= "ca->sb.nbuckets is too small";
2171 if (!init_fifo(&ca
->free
[RESERVE_BTREE
], btree_buckets
,
2173 err
= "ca->free[RESERVE_BTREE] alloc failed";
2174 goto err_btree_alloc
;
2177 if (!init_fifo_exact(&ca
->free
[RESERVE_PRIO
], prio_buckets(ca
),
2179 err
= "ca->free[RESERVE_PRIO] alloc failed";
2180 goto err_prio_alloc
;
2183 if (!init_fifo(&ca
->free
[RESERVE_MOVINGGC
], free
, GFP_KERNEL
)) {
2184 err
= "ca->free[RESERVE_MOVINGGC] alloc failed";
2185 goto err_movinggc_alloc
;
2188 if (!init_fifo(&ca
->free
[RESERVE_NONE
], free
, GFP_KERNEL
)) {
2189 err
= "ca->free[RESERVE_NONE] alloc failed";
2190 goto err_none_alloc
;
2193 if (!init_fifo(&ca
->free_inc
, free
<< 2, GFP_KERNEL
)) {
2194 err
= "ca->free_inc alloc failed";
2195 goto err_free_inc_alloc
;
2198 if (!init_heap(&ca
->heap
, free
<< 3, GFP_KERNEL
)) {
2199 err
= "ca->heap alloc failed";
2200 goto err_heap_alloc
;
2203 ca
->buckets
= vzalloc(array_size(sizeof(struct bucket
),
2206 err
= "ca->buckets alloc failed";
2207 goto err_buckets_alloc
;
2210 ca
->prio_buckets
= kzalloc(array3_size(sizeof(uint64_t),
2211 prio_buckets(ca
), 2),
2213 if (!ca
->prio_buckets
) {
2214 err
= "ca->prio_buckets alloc failed";
2215 goto err_prio_buckets_alloc
;
2218 ca
->disk_buckets
= alloc_bucket_pages(GFP_KERNEL
, ca
);
2219 if (!ca
->disk_buckets
) {
2220 err
= "ca->disk_buckets alloc failed";
2221 goto err_disk_buckets_alloc
;
2224 ca
->prio_last_buckets
= ca
->prio_buckets
+ prio_buckets(ca
);
2226 for_each_bucket(b
, ca
)
2227 atomic_set(&b
->pin
, 0);
2230 err_disk_buckets_alloc
:
2231 kfree(ca
->prio_buckets
);
2232 err_prio_buckets_alloc
:
2235 free_heap(&ca
->heap
);
2237 free_fifo(&ca
->free_inc
);
2239 free_fifo(&ca
->free
[RESERVE_NONE
]);
2241 free_fifo(&ca
->free
[RESERVE_MOVINGGC
]);
2243 free_fifo(&ca
->free
[RESERVE_PRIO
]);
2245 free_fifo(&ca
->free
[RESERVE_BTREE
]);
2248 module_put(THIS_MODULE
);
2250 pr_notice("error %s: %s", ca
->cache_dev_name
, err
);
2254 static int register_cache(struct cache_sb
*sb
, struct cache_sb_disk
*sb_disk
,
2255 struct block_device
*bdev
, struct cache
*ca
)
2257 const char *err
= NULL
; /* must be set for any error case */
2260 bdevname(bdev
, ca
->cache_dev_name
);
2261 memcpy(&ca
->sb
, sb
, sizeof(struct cache_sb
));
2263 ca
->bdev
->bd_holder
= ca
;
2264 ca
->sb_disk
= sb_disk
;
2266 if (blk_queue_discard(bdev_get_queue(bdev
)))
2267 ca
->discard
= CACHE_DISCARD(&ca
->sb
);
2269 ret
= cache_alloc(ca
);
2272 * If we failed here, it means ca->kobj is not initialized yet,
2273 * kobject_put() won't be called and there is no chance to
2274 * call blkdev_put() to bdev in bch_cache_release(). So we
2275 * explicitly call blkdev_put() here.
2277 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2279 err
= "cache_alloc(): -ENOMEM";
2280 else if (ret
== -EPERM
)
2281 err
= "cache_alloc(): cache device is too small";
2283 err
= "cache_alloc(): unknown error";
2287 if (kobject_add(&ca
->kobj
,
2288 &part_to_dev(bdev
->bd_part
)->kobj
,
2290 err
= "error calling kobject_add";
2295 mutex_lock(&bch_register_lock
);
2296 err
= register_cache_set(ca
);
2297 mutex_unlock(&bch_register_lock
);
2304 pr_info("registered cache device %s", ca
->cache_dev_name
);
2307 kobject_put(&ca
->kobj
);
2311 pr_notice("error %s: %s", ca
->cache_dev_name
, err
);
2316 /* Global interfaces/init */
2318 static ssize_t
register_bcache(struct kobject
*k
, struct kobj_attribute
*attr
,
2319 const char *buffer
, size_t size
);
2320 static ssize_t
bch_pending_bdevs_cleanup(struct kobject
*k
,
2321 struct kobj_attribute
*attr
,
2322 const char *buffer
, size_t size
);
2324 kobj_attribute_write(register, register_bcache
);
2325 kobj_attribute_write(register_quiet
, register_bcache
);
2326 kobj_attribute_write(pendings_cleanup
, bch_pending_bdevs_cleanup
);
2328 static bool bch_is_open_backing(struct block_device
*bdev
)
2330 struct cache_set
*c
, *tc
;
2331 struct cached_dev
*dc
, *t
;
2333 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
2334 list_for_each_entry_safe(dc
, t
, &c
->cached_devs
, list
)
2335 if (dc
->bdev
== bdev
)
2337 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
2338 if (dc
->bdev
== bdev
)
2343 static bool bch_is_open_cache(struct block_device
*bdev
)
2345 struct cache_set
*c
, *tc
;
2349 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
2350 for_each_cache(ca
, c
, i
)
2351 if (ca
->bdev
== bdev
)
2356 static bool bch_is_open(struct block_device
*bdev
)
2358 return bch_is_open_cache(bdev
) || bch_is_open_backing(bdev
);
2361 static ssize_t
register_bcache(struct kobject
*k
, struct kobj_attribute
*attr
,
2362 const char *buffer
, size_t size
)
2366 struct cache_sb
*sb
;
2367 struct cache_sb_disk
*sb_disk
;
2368 struct block_device
*bdev
;
2372 err
= "failed to reference bcache module";
2373 if (!try_module_get(THIS_MODULE
))
2376 /* For latest state of bcache_is_reboot */
2378 err
= "bcache is in reboot";
2379 if (bcache_is_reboot
)
2380 goto out_module_put
;
2383 err
= "cannot allocate memory";
2384 path
= kstrndup(buffer
, size
, GFP_KERNEL
);
2386 goto out_module_put
;
2388 sb
= kmalloc(sizeof(struct cache_sb
), GFP_KERNEL
);
2393 err
= "failed to open device";
2394 bdev
= blkdev_get_by_path(strim(path
),
2395 FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2398 if (bdev
== ERR_PTR(-EBUSY
)) {
2399 bdev
= lookup_bdev(strim(path
));
2400 mutex_lock(&bch_register_lock
);
2401 if (!IS_ERR(bdev
) && bch_is_open(bdev
))
2402 err
= "device already registered";
2404 err
= "device busy";
2405 mutex_unlock(&bch_register_lock
);
2408 if (attr
== &ksysfs_register_quiet
)
2414 err
= "failed to set blocksize";
2415 if (set_blocksize(bdev
, 4096))
2416 goto out_blkdev_put
;
2418 err
= read_super(sb
, bdev
, &sb_disk
);
2420 goto out_blkdev_put
;
2422 err
= "failed to register device";
2423 if (SB_IS_BDEV(sb
)) {
2424 struct cached_dev
*dc
= kzalloc(sizeof(*dc
), GFP_KERNEL
);
2427 goto out_put_sb_page
;
2429 mutex_lock(&bch_register_lock
);
2430 ret
= register_bdev(sb
, sb_disk
, bdev
, dc
);
2431 mutex_unlock(&bch_register_lock
);
2432 /* blkdev_put() will be called in cached_dev_free() */
2436 struct cache
*ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
2439 goto out_put_sb_page
;
2441 /* blkdev_put() will be called in bch_cache_release() */
2442 if (register_cache(sb
, sb_disk
, bdev
, ca
) != 0)
2449 module_put(THIS_MODULE
);
2453 put_page(virt_to_page(sb_disk
));
2455 blkdev_put(bdev
, FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
2462 module_put(THIS_MODULE
);
2464 pr_info("error %s: %s", path
?path
:"", err
);
2470 struct list_head list
;
2471 struct cached_dev
*dc
;
2474 static ssize_t
bch_pending_bdevs_cleanup(struct kobject
*k
,
2475 struct kobj_attribute
*attr
,
2479 LIST_HEAD(pending_devs
);
2481 struct cached_dev
*dc
, *tdc
;
2482 struct pdev
*pdev
, *tpdev
;
2483 struct cache_set
*c
, *tc
;
2485 mutex_lock(&bch_register_lock
);
2486 list_for_each_entry_safe(dc
, tdc
, &uncached_devices
, list
) {
2487 pdev
= kmalloc(sizeof(struct pdev
), GFP_KERNEL
);
2491 list_add(&pdev
->list
, &pending_devs
);
2494 list_for_each_entry_safe(pdev
, tpdev
, &pending_devs
, list
) {
2495 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
) {
2496 char *pdev_set_uuid
= pdev
->dc
->sb
.set_uuid
;
2497 char *set_uuid
= c
->sb
.uuid
;
2499 if (!memcmp(pdev_set_uuid
, set_uuid
, 16)) {
2500 list_del(&pdev
->list
);
2506 mutex_unlock(&bch_register_lock
);
2508 list_for_each_entry_safe(pdev
, tpdev
, &pending_devs
, list
) {
2509 pr_info("delete pdev %p", pdev
);
2510 list_del(&pdev
->list
);
2511 bcache_device_stop(&pdev
->dc
->disk
);
2518 static int bcache_reboot(struct notifier_block
*n
, unsigned long code
, void *x
)
2520 if (bcache_is_reboot
)
2523 if (code
== SYS_DOWN
||
2525 code
== SYS_POWER_OFF
) {
2527 unsigned long start
= jiffies
;
2528 bool stopped
= false;
2530 struct cache_set
*c
, *tc
;
2531 struct cached_dev
*dc
, *tdc
;
2533 mutex_lock(&bch_register_lock
);
2535 if (bcache_is_reboot
)
2538 /* New registration is rejected since now */
2539 bcache_is_reboot
= true;
2541 * Make registering caller (if there is) on other CPU
2542 * core know bcache_is_reboot set to true earlier
2546 if (list_empty(&bch_cache_sets
) &&
2547 list_empty(&uncached_devices
))
2550 mutex_unlock(&bch_register_lock
);
2552 pr_info("Stopping all devices:");
2555 * The reason bch_register_lock is not held to call
2556 * bch_cache_set_stop() and bcache_device_stop() is to
2557 * avoid potential deadlock during reboot, because cache
2558 * set or bcache device stopping process will acqurie
2559 * bch_register_lock too.
2561 * We are safe here because bcache_is_reboot sets to
2562 * true already, register_bcache() will reject new
2563 * registration now. bcache_is_reboot also makes sure
2564 * bcache_reboot() won't be re-entered on by other thread,
2565 * so there is no race in following list iteration by
2566 * list_for_each_entry_safe().
2568 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
2569 bch_cache_set_stop(c
);
2571 list_for_each_entry_safe(dc
, tdc
, &uncached_devices
, list
)
2572 bcache_device_stop(&dc
->disk
);
2576 * Give an early chance for other kthreads and
2577 * kworkers to stop themselves
2581 /* What's a condition variable? */
2583 long timeout
= start
+ 10 * HZ
- jiffies
;
2585 mutex_lock(&bch_register_lock
);
2586 stopped
= list_empty(&bch_cache_sets
) &&
2587 list_empty(&uncached_devices
);
2589 if (timeout
< 0 || stopped
)
2592 prepare_to_wait(&unregister_wait
, &wait
,
2593 TASK_UNINTERRUPTIBLE
);
2595 mutex_unlock(&bch_register_lock
);
2596 schedule_timeout(timeout
);
2599 finish_wait(&unregister_wait
, &wait
);
2602 pr_info("All devices stopped");
2604 pr_notice("Timeout waiting for devices to be closed");
2606 mutex_unlock(&bch_register_lock
);
2612 static struct notifier_block reboot
= {
2613 .notifier_call
= bcache_reboot
,
2614 .priority
= INT_MAX
, /* before any real devices */
2617 static void bcache_exit(void)
2622 kobject_put(bcache_kobj
);
2624 destroy_workqueue(bcache_wq
);
2626 destroy_workqueue(bch_journal_wq
);
2629 unregister_blkdev(bcache_major
, "bcache");
2630 unregister_reboot_notifier(&reboot
);
2631 mutex_destroy(&bch_register_lock
);
2634 /* Check and fixup module parameters */
2635 static void check_module_parameters(void)
2637 if (bch_cutoff_writeback_sync
== 0)
2638 bch_cutoff_writeback_sync
= CUTOFF_WRITEBACK_SYNC
;
2639 else if (bch_cutoff_writeback_sync
> CUTOFF_WRITEBACK_SYNC_MAX
) {
2640 pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u",
2641 bch_cutoff_writeback_sync
, CUTOFF_WRITEBACK_SYNC_MAX
);
2642 bch_cutoff_writeback_sync
= CUTOFF_WRITEBACK_SYNC_MAX
;
2645 if (bch_cutoff_writeback
== 0)
2646 bch_cutoff_writeback
= CUTOFF_WRITEBACK
;
2647 else if (bch_cutoff_writeback
> CUTOFF_WRITEBACK_MAX
) {
2648 pr_warn("set bch_cutoff_writeback (%u) to max value %u",
2649 bch_cutoff_writeback
, CUTOFF_WRITEBACK_MAX
);
2650 bch_cutoff_writeback
= CUTOFF_WRITEBACK_MAX
;
2653 if (bch_cutoff_writeback
> bch_cutoff_writeback_sync
) {
2654 pr_warn("set bch_cutoff_writeback (%u) to %u",
2655 bch_cutoff_writeback
, bch_cutoff_writeback_sync
);
2656 bch_cutoff_writeback
= bch_cutoff_writeback_sync
;
2660 static int __init
bcache_init(void)
2662 static const struct attribute
*files
[] = {
2663 &ksysfs_register
.attr
,
2664 &ksysfs_register_quiet
.attr
,
2665 &ksysfs_pendings_cleanup
.attr
,
2669 check_module_parameters();
2671 mutex_init(&bch_register_lock
);
2672 init_waitqueue_head(&unregister_wait
);
2673 register_reboot_notifier(&reboot
);
2675 bcache_major
= register_blkdev(0, "bcache");
2676 if (bcache_major
< 0) {
2677 unregister_reboot_notifier(&reboot
);
2678 mutex_destroy(&bch_register_lock
);
2679 return bcache_major
;
2682 bcache_wq
= alloc_workqueue("bcache", WQ_MEM_RECLAIM
, 0);
2686 bch_journal_wq
= alloc_workqueue("bch_journal", WQ_MEM_RECLAIM
, 0);
2687 if (!bch_journal_wq
)
2690 bcache_kobj
= kobject_create_and_add("bcache", fs_kobj
);
2694 if (bch_request_init() ||
2695 sysfs_create_files(bcache_kobj
, files
))
2699 closure_debug_init();
2701 bcache_is_reboot
= false;
2712 module_exit(bcache_exit
);
2713 module_init(bcache_init
);
2715 module_param(bch_cutoff_writeback
, uint
, 0);
2716 MODULE_PARM_DESC(bch_cutoff_writeback
, "threshold to cutoff writeback");
2718 module_param(bch_cutoff_writeback_sync
, uint
, 0);
2719 MODULE_PARM_DESC(bch_cutoff_writeback_sync
, "hard threshold to cutoff writeback");
2721 MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
2722 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
2723 MODULE_LICENSE("GPL");