2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
13 #include "writeback.h"
15 #include <linux/blkdev.h>
16 #include <linux/buffer_head.h>
17 #include <linux/debugfs.h>
18 #include <linux/genhd.h>
19 #include <linux/kthread.h>
20 #include <linux/module.h>
21 #include <linux/random.h>
22 #include <linux/reboot.h>
23 #include <linux/sysfs.h>
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
28 static const char bcache_magic
[] = {
29 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
30 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
33 static const char invalid_uuid
[] = {
34 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
35 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
38 /* Default is -1; we skip past it for struct cached_dev's cache mode */
39 const char * const bch_cache_modes
[] = {
48 struct uuid_entry_v0
{
57 static struct kobject
*bcache_kobj
;
58 struct mutex bch_register_lock
;
59 LIST_HEAD(bch_cache_sets
);
60 static LIST_HEAD(uncached_devices
);
62 static int bcache_major
, bcache_minor
;
63 static wait_queue_head_t unregister_wait
;
64 struct workqueue_struct
*bcache_wq
;
66 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
68 static void bio_split_pool_free(struct bio_split_pool
*p
)
70 if (p
->bio_split_hook
)
71 mempool_destroy(p
->bio_split_hook
);
74 bioset_free(p
->bio_split
);
77 static int bio_split_pool_init(struct bio_split_pool
*p
)
79 p
->bio_split
= bioset_create(4, 0);
83 p
->bio_split_hook
= mempool_create_kmalloc_pool(4,
84 sizeof(struct bio_split_hook
));
85 if (!p
->bio_split_hook
)
93 static const char *read_super(struct cache_sb
*sb
, struct block_device
*bdev
,
98 struct buffer_head
*bh
= __bread(bdev
, 1, SB_SIZE
);
104 s
= (struct cache_sb
*) bh
->b_data
;
106 sb
->offset
= le64_to_cpu(s
->offset
);
107 sb
->version
= le64_to_cpu(s
->version
);
109 memcpy(sb
->magic
, s
->magic
, 16);
110 memcpy(sb
->uuid
, s
->uuid
, 16);
111 memcpy(sb
->set_uuid
, s
->set_uuid
, 16);
112 memcpy(sb
->label
, s
->label
, SB_LABEL_SIZE
);
114 sb
->flags
= le64_to_cpu(s
->flags
);
115 sb
->seq
= le64_to_cpu(s
->seq
);
116 sb
->last_mount
= le32_to_cpu(s
->last_mount
);
117 sb
->first_bucket
= le16_to_cpu(s
->first_bucket
);
118 sb
->keys
= le16_to_cpu(s
->keys
);
120 for (i
= 0; i
< SB_JOURNAL_BUCKETS
; i
++)
121 sb
->d
[i
] = le64_to_cpu(s
->d
[i
]);
123 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
124 sb
->version
, sb
->flags
, sb
->seq
, sb
->keys
);
126 err
= "Not a bcache superblock";
127 if (sb
->offset
!= SB_SECTOR
)
130 if (memcmp(sb
->magic
, bcache_magic
, 16))
133 err
= "Too many journal buckets";
134 if (sb
->keys
> SB_JOURNAL_BUCKETS
)
137 err
= "Bad checksum";
138 if (s
->csum
!= csum_set(s
))
142 if (bch_is_zero(sb
->uuid
, 16))
145 sb
->block_size
= le16_to_cpu(s
->block_size
);
147 err
= "Superblock block size smaller than device block size";
148 if (sb
->block_size
<< 9 < bdev_logical_block_size(bdev
))
151 switch (sb
->version
) {
152 case BCACHE_SB_VERSION_BDEV
:
153 sb
->data_offset
= BDEV_DATA_START_DEFAULT
;
155 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET
:
156 sb
->data_offset
= le64_to_cpu(s
->data_offset
);
158 err
= "Bad data offset";
159 if (sb
->data_offset
< BDEV_DATA_START_DEFAULT
)
163 case BCACHE_SB_VERSION_CDEV
:
164 case BCACHE_SB_VERSION_CDEV_WITH_UUID
:
165 sb
->nbuckets
= le64_to_cpu(s
->nbuckets
);
166 sb
->block_size
= le16_to_cpu(s
->block_size
);
167 sb
->bucket_size
= le16_to_cpu(s
->bucket_size
);
169 sb
->nr_in_set
= le16_to_cpu(s
->nr_in_set
);
170 sb
->nr_this_dev
= le16_to_cpu(s
->nr_this_dev
);
172 err
= "Too many buckets";
173 if (sb
->nbuckets
> LONG_MAX
)
176 err
= "Not enough buckets";
177 if (sb
->nbuckets
< 1 << 7)
180 err
= "Bad block/bucket size";
181 if (!is_power_of_2(sb
->block_size
) ||
182 sb
->block_size
> PAGE_SECTORS
||
183 !is_power_of_2(sb
->bucket_size
) ||
184 sb
->bucket_size
< PAGE_SECTORS
)
187 err
= "Invalid superblock: device too small";
188 if (get_capacity(bdev
->bd_disk
) < sb
->bucket_size
* sb
->nbuckets
)
192 if (bch_is_zero(sb
->set_uuid
, 16))
195 err
= "Bad cache device number in set";
196 if (!sb
->nr_in_set
||
197 sb
->nr_in_set
<= sb
->nr_this_dev
||
198 sb
->nr_in_set
> MAX_CACHES_PER_SET
)
201 err
= "Journal buckets not sequential";
202 for (i
= 0; i
< sb
->keys
; i
++)
203 if (sb
->d
[i
] != sb
->first_bucket
+ i
)
206 err
= "Too many journal buckets";
207 if (sb
->first_bucket
+ sb
->keys
> sb
->nbuckets
)
210 err
= "Invalid superblock: first bucket comes before end of super";
211 if (sb
->first_bucket
* sb
->bucket_size
< 16)
216 err
= "Unsupported superblock version";
220 sb
->last_mount
= get_seconds();
223 get_page(bh
->b_page
);
230 static void write_bdev_super_endio(struct bio
*bio
, int error
)
232 struct cached_dev
*dc
= bio
->bi_private
;
233 /* XXX: error checking */
235 closure_put(&dc
->sb_write
.cl
);
238 static void __write_super(struct cache_sb
*sb
, struct bio
*bio
)
240 struct cache_sb
*out
= page_address(bio
->bi_io_vec
[0].bv_page
);
243 bio
->bi_sector
= SB_SECTOR
;
244 bio
->bi_rw
= REQ_SYNC
|REQ_META
;
245 bio
->bi_size
= SB_SIZE
;
246 bch_bio_map(bio
, NULL
);
248 out
->offset
= cpu_to_le64(sb
->offset
);
249 out
->version
= cpu_to_le64(sb
->version
);
251 memcpy(out
->uuid
, sb
->uuid
, 16);
252 memcpy(out
->set_uuid
, sb
->set_uuid
, 16);
253 memcpy(out
->label
, sb
->label
, SB_LABEL_SIZE
);
255 out
->flags
= cpu_to_le64(sb
->flags
);
256 out
->seq
= cpu_to_le64(sb
->seq
);
258 out
->last_mount
= cpu_to_le32(sb
->last_mount
);
259 out
->first_bucket
= cpu_to_le16(sb
->first_bucket
);
260 out
->keys
= cpu_to_le16(sb
->keys
);
262 for (i
= 0; i
< sb
->keys
; i
++)
263 out
->d
[i
] = cpu_to_le64(sb
->d
[i
]);
265 out
->csum
= csum_set(out
);
267 pr_debug("ver %llu, flags %llu, seq %llu",
268 sb
->version
, sb
->flags
, sb
->seq
);
270 submit_bio(REQ_WRITE
, bio
);
273 void bch_write_bdev_super(struct cached_dev
*dc
, struct closure
*parent
)
275 struct closure
*cl
= &dc
->sb_write
.cl
;
276 struct bio
*bio
= &dc
->sb_bio
;
278 closure_lock(&dc
->sb_write
, parent
);
281 bio
->bi_bdev
= dc
->bdev
;
282 bio
->bi_end_io
= write_bdev_super_endio
;
283 bio
->bi_private
= dc
;
286 __write_super(&dc
->sb
, bio
);
291 static void write_super_endio(struct bio
*bio
, int error
)
293 struct cache
*ca
= bio
->bi_private
;
295 bch_count_io_errors(ca
, error
, "writing superblock");
296 closure_put(&ca
->set
->sb_write
.cl
);
299 void bcache_write_super(struct cache_set
*c
)
301 struct closure
*cl
= &c
->sb_write
.cl
;
305 closure_lock(&c
->sb_write
, &c
->cl
);
309 for_each_cache(ca
, c
, i
) {
310 struct bio
*bio
= &ca
->sb_bio
;
312 ca
->sb
.version
= BCACHE_SB_VERSION_CDEV_WITH_UUID
;
313 ca
->sb
.seq
= c
->sb
.seq
;
314 ca
->sb
.last_mount
= c
->sb
.last_mount
;
316 SET_CACHE_SYNC(&ca
->sb
, CACHE_SYNC(&c
->sb
));
319 bio
->bi_bdev
= ca
->bdev
;
320 bio
->bi_end_io
= write_super_endio
;
321 bio
->bi_private
= ca
;
324 __write_super(&ca
->sb
, bio
);
332 static void uuid_endio(struct bio
*bio
, int error
)
334 struct closure
*cl
= bio
->bi_private
;
335 struct cache_set
*c
= container_of(cl
, struct cache_set
, uuid_write
.cl
);
337 cache_set_err_on(error
, c
, "accessing uuids");
338 bch_bbio_free(bio
, c
);
342 static void uuid_io(struct cache_set
*c
, unsigned long rw
,
343 struct bkey
*k
, struct closure
*parent
)
345 struct closure
*cl
= &c
->uuid_write
.cl
;
346 struct uuid_entry
*u
;
351 closure_lock(&c
->uuid_write
, parent
);
353 for (i
= 0; i
< KEY_PTRS(k
); i
++) {
354 struct bio
*bio
= bch_bbio_alloc(c
);
356 bio
->bi_rw
= REQ_SYNC
|REQ_META
|rw
;
357 bio
->bi_size
= KEY_SIZE(k
) << 9;
359 bio
->bi_end_io
= uuid_endio
;
360 bio
->bi_private
= cl
;
361 bch_bio_map(bio
, c
->uuids
);
363 bch_submit_bbio(bio
, c
, k
, i
);
369 bch_bkey_to_text(buf
, sizeof(buf
), k
);
370 pr_debug("%s UUIDs at %s", rw
& REQ_WRITE
? "wrote" : "read", buf
);
372 for (u
= c
->uuids
; u
< c
->uuids
+ c
->nr_uuids
; u
++)
373 if (!bch_is_zero(u
->uuid
, 16))
374 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
375 u
- c
->uuids
, u
->uuid
, u
->label
,
376 u
->first_reg
, u
->last_reg
, u
->invalidated
);
381 static char *uuid_read(struct cache_set
*c
, struct jset
*j
, struct closure
*cl
)
383 struct bkey
*k
= &j
->uuid_bucket
;
385 if (__bch_ptr_invalid(c
, 1, k
))
386 return "bad uuid pointer";
388 bkey_copy(&c
->uuid_bucket
, k
);
389 uuid_io(c
, READ_SYNC
, k
, cl
);
391 if (j
->version
< BCACHE_JSET_VERSION_UUIDv1
) {
392 struct uuid_entry_v0
*u0
= (void *) c
->uuids
;
393 struct uuid_entry
*u1
= (void *) c
->uuids
;
399 * Since the new uuid entry is bigger than the old, we have to
400 * convert starting at the highest memory address and work down
401 * in order to do it in place
404 for (i
= c
->nr_uuids
- 1;
407 memcpy(u1
[i
].uuid
, u0
[i
].uuid
, 16);
408 memcpy(u1
[i
].label
, u0
[i
].label
, 32);
410 u1
[i
].first_reg
= u0
[i
].first_reg
;
411 u1
[i
].last_reg
= u0
[i
].last_reg
;
412 u1
[i
].invalidated
= u0
[i
].invalidated
;
422 static int __uuid_write(struct cache_set
*c
)
426 closure_init_stack(&cl
);
428 lockdep_assert_held(&bch_register_lock
);
430 if (bch_bucket_alloc_set(c
, WATERMARK_METADATA
, &k
.key
, 1, &cl
))
433 SET_KEY_SIZE(&k
.key
, c
->sb
.bucket_size
);
434 uuid_io(c
, REQ_WRITE
, &k
.key
, &cl
);
437 bkey_copy(&c
->uuid_bucket
, &k
.key
);
438 __bkey_put(c
, &k
.key
);
442 int bch_uuid_write(struct cache_set
*c
)
444 int ret
= __uuid_write(c
);
447 bch_journal_meta(c
, NULL
);
452 static struct uuid_entry
*uuid_find(struct cache_set
*c
, const char *uuid
)
454 struct uuid_entry
*u
;
457 u
< c
->uuids
+ c
->nr_uuids
; u
++)
458 if (!memcmp(u
->uuid
, uuid
, 16))
464 static struct uuid_entry
*uuid_find_empty(struct cache_set
*c
)
466 static const char zero_uuid
[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
467 return uuid_find(c
, zero_uuid
);
471 * Bucket priorities/gens:
473 * For each bucket, we store on disk its
477 * See alloc.c for an explanation of the gen. The priority is used to implement
478 * lru (and in the future other) cache replacement policies; for most purposes
479 * it's just an opaque integer.
481 * The gens and the priorities don't have a whole lot to do with each other, and
482 * it's actually the gens that must be written out at specific times - it's no
483 * big deal if the priorities don't get written, if we lose them we just reuse
484 * buckets in suboptimal order.
486 * On disk they're stored in a packed array, and in as many buckets are required
487 * to fit them all. The buckets we use to store them form a list; the journal
488 * header points to the first bucket, the first bucket points to the second
491 * This code is used by the allocation code; periodically (whenever it runs out
492 * of buckets to allocate from) the allocation code will invalidate some
493 * buckets, but it can't use those buckets until their new gens are safely on
497 static void prio_endio(struct bio
*bio
, int error
)
499 struct cache
*ca
= bio
->bi_private
;
501 cache_set_err_on(error
, ca
->set
, "accessing priorities");
502 bch_bbio_free(bio
, ca
->set
);
503 closure_put(&ca
->prio
);
506 static void prio_io(struct cache
*ca
, uint64_t bucket
, unsigned long rw
)
508 struct closure
*cl
= &ca
->prio
;
509 struct bio
*bio
= bch_bbio_alloc(ca
->set
);
511 closure_init_stack(cl
);
513 bio
->bi_sector
= bucket
* ca
->sb
.bucket_size
;
514 bio
->bi_bdev
= ca
->bdev
;
515 bio
->bi_rw
= REQ_SYNC
|REQ_META
|rw
;
516 bio
->bi_size
= bucket_bytes(ca
);
518 bio
->bi_end_io
= prio_endio
;
519 bio
->bi_private
= ca
;
520 bch_bio_map(bio
, ca
->disk_buckets
);
522 closure_bio_submit(bio
, &ca
->prio
, ca
);
526 #define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
527 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
529 void bch_prio_write(struct cache
*ca
)
535 closure_init_stack(&cl
);
537 lockdep_assert_held(&ca
->set
->bucket_lock
);
539 for (b
= ca
->buckets
;
540 b
< ca
->buckets
+ ca
->sb
.nbuckets
; b
++)
541 b
->disk_gen
= b
->gen
;
543 ca
->disk_buckets
->seq
++;
545 atomic_long_add(ca
->sb
.bucket_size
* prio_buckets(ca
),
546 &ca
->meta_sectors_written
);
548 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca
->free
),
549 fifo_used(&ca
->free_inc
), fifo_used(&ca
->unused
));
551 for (i
= prio_buckets(ca
) - 1; i
>= 0; --i
) {
553 struct prio_set
*p
= ca
->disk_buckets
;
554 struct bucket_disk
*d
= p
->data
;
555 struct bucket_disk
*end
= d
+ prios_per_bucket(ca
);
557 for (b
= ca
->buckets
+ i
* prios_per_bucket(ca
);
558 b
< ca
->buckets
+ ca
->sb
.nbuckets
&& d
< end
;
560 d
->prio
= cpu_to_le16(b
->prio
);
564 p
->next_bucket
= ca
->prio_buckets
[i
+ 1];
565 p
->magic
= pset_magic(ca
);
566 p
->csum
= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8);
568 bucket
= bch_bucket_alloc(ca
, WATERMARK_PRIO
, &cl
);
569 BUG_ON(bucket
== -1);
571 mutex_unlock(&ca
->set
->bucket_lock
);
572 prio_io(ca
, bucket
, REQ_WRITE
);
573 mutex_lock(&ca
->set
->bucket_lock
);
575 ca
->prio_buckets
[i
] = bucket
;
576 atomic_dec_bug(&ca
->buckets
[bucket
].pin
);
579 mutex_unlock(&ca
->set
->bucket_lock
);
581 bch_journal_meta(ca
->set
, &cl
);
584 mutex_lock(&ca
->set
->bucket_lock
);
586 ca
->need_save_prio
= 0;
589 * Don't want the old priorities to get garbage collected until after we
590 * finish writing the new ones, and they're journalled
592 for (i
= 0; i
< prio_buckets(ca
); i
++)
593 ca
->prio_last_buckets
[i
] = ca
->prio_buckets
[i
];
596 static void prio_read(struct cache
*ca
, uint64_t bucket
)
598 struct prio_set
*p
= ca
->disk_buckets
;
599 struct bucket_disk
*d
= p
->data
+ prios_per_bucket(ca
), *end
= d
;
601 unsigned bucket_nr
= 0;
603 for (b
= ca
->buckets
;
604 b
< ca
->buckets
+ ca
->sb
.nbuckets
;
607 ca
->prio_buckets
[bucket_nr
] = bucket
;
608 ca
->prio_last_buckets
[bucket_nr
] = bucket
;
611 prio_io(ca
, bucket
, READ_SYNC
);
613 if (p
->csum
!= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8))
614 pr_warn("bad csum reading priorities");
616 if (p
->magic
!= pset_magic(ca
))
617 pr_warn("bad magic reading priorities");
619 bucket
= p
->next_bucket
;
623 b
->prio
= le16_to_cpu(d
->prio
);
624 b
->gen
= b
->disk_gen
= b
->last_gc
= b
->gc_gen
= d
->gen
;
630 static int open_dev(struct block_device
*b
, fmode_t mode
)
632 struct bcache_device
*d
= b
->bd_disk
->private_data
;
633 if (atomic_read(&d
->closing
))
640 static void release_dev(struct gendisk
*b
, fmode_t mode
)
642 struct bcache_device
*d
= b
->private_data
;
646 static int ioctl_dev(struct block_device
*b
, fmode_t mode
,
647 unsigned int cmd
, unsigned long arg
)
649 struct bcache_device
*d
= b
->bd_disk
->private_data
;
650 return d
->ioctl(d
, mode
, cmd
, arg
);
653 static const struct block_device_operations bcache_ops
= {
655 .release
= release_dev
,
657 .owner
= THIS_MODULE
,
660 void bcache_device_stop(struct bcache_device
*d
)
662 if (!atomic_xchg(&d
->closing
, 1))
663 closure_queue(&d
->cl
);
666 static void bcache_device_unlink(struct bcache_device
*d
)
671 sysfs_remove_link(&d
->c
->kobj
, d
->name
);
672 sysfs_remove_link(&d
->kobj
, "cache");
674 for_each_cache(ca
, d
->c
, i
)
675 bd_unlink_disk_holder(ca
->bdev
, d
->disk
);
678 static void bcache_device_link(struct bcache_device
*d
, struct cache_set
*c
,
684 for_each_cache(ca
, d
->c
, i
)
685 bd_link_disk_holder(ca
->bdev
, d
->disk
);
687 snprintf(d
->name
, BCACHEDEVNAME_SIZE
,
688 "%s%u", name
, d
->id
);
690 WARN(sysfs_create_link(&d
->kobj
, &c
->kobj
, "cache") ||
691 sysfs_create_link(&c
->kobj
, &d
->kobj
, d
->name
),
692 "Couldn't create device <-> cache set symlinks");
695 static void bcache_device_detach(struct bcache_device
*d
)
697 lockdep_assert_held(&bch_register_lock
);
699 if (atomic_read(&d
->detaching
)) {
700 struct uuid_entry
*u
= d
->c
->uuids
+ d
->id
;
702 SET_UUID_FLASH_ONLY(u
, 0);
703 memcpy(u
->uuid
, invalid_uuid
, 16);
704 u
->invalidated
= cpu_to_le32(get_seconds());
705 bch_uuid_write(d
->c
);
707 atomic_set(&d
->detaching
, 0);
711 bcache_device_unlink(d
);
713 d
->c
->devices
[d
->id
] = NULL
;
714 closure_put(&d
->c
->caching
);
718 static void bcache_device_attach(struct bcache_device
*d
, struct cache_set
*c
,
721 BUG_ON(test_bit(CACHE_SET_STOPPING
, &c
->flags
));
727 closure_get(&c
->caching
);
730 static void bcache_device_free(struct bcache_device
*d
)
732 lockdep_assert_held(&bch_register_lock
);
734 pr_info("%s stopped", d
->disk
->disk_name
);
737 bcache_device_detach(d
);
738 if (d
->disk
&& d
->disk
->flags
& GENHD_FL_UP
)
739 del_gendisk(d
->disk
);
740 if (d
->disk
&& d
->disk
->queue
)
741 blk_cleanup_queue(d
->disk
->queue
);
745 bio_split_pool_free(&d
->bio_split_hook
);
746 if (d
->unaligned_bvec
)
747 mempool_destroy(d
->unaligned_bvec
);
749 bioset_free(d
->bio_split
);
750 if (is_vmalloc_addr(d
->stripe_sectors_dirty
))
751 vfree(d
->stripe_sectors_dirty
);
753 kfree(d
->stripe_sectors_dirty
);
755 closure_debug_destroy(&d
->cl
);
758 static int bcache_device_init(struct bcache_device
*d
, unsigned block_size
,
761 struct request_queue
*q
;
764 if (!d
->stripe_size_bits
)
765 d
->stripe_size_bits
= 31;
767 d
->nr_stripes
= round_up(sectors
, 1 << d
->stripe_size_bits
) >>
770 if (!d
->nr_stripes
|| d
->nr_stripes
> SIZE_MAX
/ sizeof(atomic_t
))
773 n
= d
->nr_stripes
* sizeof(atomic_t
);
774 d
->stripe_sectors_dirty
= n
< PAGE_SIZE
<< 6
775 ? kzalloc(n
, GFP_KERNEL
)
777 if (!d
->stripe_sectors_dirty
)
780 if (!(d
->bio_split
= bioset_create(4, offsetof(struct bbio
, bio
))) ||
781 !(d
->unaligned_bvec
= mempool_create_kmalloc_pool(1,
782 sizeof(struct bio_vec
) * BIO_MAX_PAGES
)) ||
783 bio_split_pool_init(&d
->bio_split_hook
) ||
784 !(d
->disk
= alloc_disk(1)) ||
785 !(q
= blk_alloc_queue(GFP_KERNEL
)))
788 set_capacity(d
->disk
, sectors
);
789 snprintf(d
->disk
->disk_name
, DISK_NAME_LEN
, "bcache%i", bcache_minor
);
791 d
->disk
->major
= bcache_major
;
792 d
->disk
->first_minor
= bcache_minor
++;
793 d
->disk
->fops
= &bcache_ops
;
794 d
->disk
->private_data
= d
;
796 blk_queue_make_request(q
, NULL
);
799 q
->backing_dev_info
.congested_data
= d
;
800 q
->limits
.max_hw_sectors
= UINT_MAX
;
801 q
->limits
.max_sectors
= UINT_MAX
;
802 q
->limits
.max_segment_size
= UINT_MAX
;
803 q
->limits
.max_segments
= BIO_MAX_PAGES
;
804 q
->limits
.max_discard_sectors
= UINT_MAX
;
805 q
->limits
.io_min
= block_size
;
806 q
->limits
.logical_block_size
= block_size
;
807 q
->limits
.physical_block_size
= block_size
;
808 set_bit(QUEUE_FLAG_NONROT
, &d
->disk
->queue
->queue_flags
);
809 set_bit(QUEUE_FLAG_DISCARD
, &d
->disk
->queue
->queue_flags
);
811 blk_queue_flush(q
, REQ_FLUSH
|REQ_FUA
);
818 static void calc_cached_dev_sectors(struct cache_set
*c
)
820 uint64_t sectors
= 0;
821 struct cached_dev
*dc
;
823 list_for_each_entry(dc
, &c
->cached_devs
, list
)
824 sectors
+= bdev_sectors(dc
->bdev
);
826 c
->cached_dev_sectors
= sectors
;
829 void bch_cached_dev_run(struct cached_dev
*dc
)
831 struct bcache_device
*d
= &dc
->disk
;
832 char buf
[SB_LABEL_SIZE
+ 1];
835 kasprintf(GFP_KERNEL
, "CACHED_UUID=%pU", dc
->sb
.uuid
),
840 memcpy(buf
, dc
->sb
.label
, SB_LABEL_SIZE
);
841 buf
[SB_LABEL_SIZE
] = '\0';
842 env
[2] = kasprintf(GFP_KERNEL
, "CACHED_LABEL=%s", buf
);
844 if (atomic_xchg(&dc
->running
, 1))
848 BDEV_STATE(&dc
->sb
) != BDEV_STATE_NONE
) {
850 closure_init_stack(&cl
);
852 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_STALE
);
853 bch_write_bdev_super(dc
, &cl
);
858 bd_link_disk_holder(dc
->bdev
, dc
->disk
.disk
);
859 /* won't show up in the uevent file, use udevadm monitor -e instead
860 * only class / kset properties are persistent */
861 kobject_uevent_env(&disk_to_dev(d
->disk
)->kobj
, KOBJ_CHANGE
, env
);
865 if (sysfs_create_link(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "dev") ||
866 sysfs_create_link(&disk_to_dev(d
->disk
)->kobj
, &d
->kobj
, "bcache"))
867 pr_debug("error creating sysfs link");
870 static void cached_dev_detach_finish(struct work_struct
*w
)
872 struct cached_dev
*dc
= container_of(w
, struct cached_dev
, detach
);
873 char buf
[BDEVNAME_SIZE
];
875 closure_init_stack(&cl
);
877 BUG_ON(!atomic_read(&dc
->disk
.detaching
));
878 BUG_ON(atomic_read(&dc
->count
));
880 mutex_lock(&bch_register_lock
);
882 memset(&dc
->sb
.set_uuid
, 0, 16);
883 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_NONE
);
885 bch_write_bdev_super(dc
, &cl
);
888 bcache_device_detach(&dc
->disk
);
889 list_move(&dc
->list
, &uncached_devices
);
891 mutex_unlock(&bch_register_lock
);
893 pr_info("Caching disabled for %s", bdevname(dc
->bdev
, buf
));
895 /* Drop ref we took in cached_dev_detach() */
896 closure_put(&dc
->disk
.cl
);
899 void bch_cached_dev_detach(struct cached_dev
*dc
)
901 lockdep_assert_held(&bch_register_lock
);
903 if (atomic_read(&dc
->disk
.closing
))
906 if (atomic_xchg(&dc
->disk
.detaching
, 1))
910 * Block the device from being closed and freed until we're finished
913 closure_get(&dc
->disk
.cl
);
915 bch_writeback_queue(dc
);
919 int bch_cached_dev_attach(struct cached_dev
*dc
, struct cache_set
*c
)
921 uint32_t rtime
= cpu_to_le32(get_seconds());
922 struct uuid_entry
*u
;
923 char buf
[BDEVNAME_SIZE
];
925 bdevname(dc
->bdev
, buf
);
927 if (memcmp(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16))
931 pr_err("Can't attach %s: already attached", buf
);
935 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
)) {
936 pr_err("Can't attach %s: shutting down", buf
);
940 if (dc
->sb
.block_size
< c
->sb
.block_size
) {
942 pr_err("Couldn't attach %s: block size less than set's block size",
947 u
= uuid_find(c
, dc
->sb
.uuid
);
950 (BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
||
951 BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
)) {
952 memcpy(u
->uuid
, invalid_uuid
, 16);
953 u
->invalidated
= cpu_to_le32(get_seconds());
958 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
959 pr_err("Couldn't find uuid for %s in set", buf
);
963 u
= uuid_find_empty(c
);
965 pr_err("Not caching %s, no room for UUID", buf
);
970 /* Deadlocks since we're called via sysfs...
971 sysfs_remove_file(&dc->kobj, &sysfs_attach);
974 if (bch_is_zero(u
->uuid
, 16)) {
976 closure_init_stack(&cl
);
978 memcpy(u
->uuid
, dc
->sb
.uuid
, 16);
979 memcpy(u
->label
, dc
->sb
.label
, SB_LABEL_SIZE
);
980 u
->first_reg
= u
->last_reg
= rtime
;
983 memcpy(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16);
984 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_CLEAN
);
986 bch_write_bdev_super(dc
, &cl
);
993 bcache_device_attach(&dc
->disk
, c
, u
- c
->uuids
);
994 list_move(&dc
->list
, &c
->cached_devs
);
995 calc_cached_dev_sectors(c
);
999 * dc->c must be set before dc->count != 0 - paired with the mb in
1002 atomic_set(&dc
->count
, 1);
1004 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
1005 bch_sectors_dirty_init(dc
);
1006 atomic_set(&dc
->has_dirty
, 1);
1007 atomic_inc(&dc
->count
);
1008 bch_writeback_queue(dc
);
1011 bch_cached_dev_run(dc
);
1012 bcache_device_link(&dc
->disk
, c
, "bdev");
1014 pr_info("Caching %s as %s on set %pU",
1015 bdevname(dc
->bdev
, buf
), dc
->disk
.disk
->disk_name
,
1016 dc
->disk
.c
->sb
.set_uuid
);
1020 void bch_cached_dev_release(struct kobject
*kobj
)
1022 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
1025 module_put(THIS_MODULE
);
1028 static void cached_dev_free(struct closure
*cl
)
1030 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
1032 cancel_delayed_work_sync(&dc
->writeback_rate_update
);
1034 mutex_lock(&bch_register_lock
);
1036 if (atomic_read(&dc
->running
))
1037 bd_unlink_disk_holder(dc
->bdev
, dc
->disk
.disk
);
1038 bcache_device_free(&dc
->disk
);
1039 list_del(&dc
->list
);
1041 mutex_unlock(&bch_register_lock
);
1043 if (!IS_ERR_OR_NULL(dc
->bdev
)) {
1044 if (dc
->bdev
->bd_disk
)
1045 blk_sync_queue(bdev_get_queue(dc
->bdev
));
1047 blkdev_put(dc
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1050 wake_up(&unregister_wait
);
1052 kobject_put(&dc
->disk
.kobj
);
1055 static void cached_dev_flush(struct closure
*cl
)
1057 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
1058 struct bcache_device
*d
= &dc
->disk
;
1060 mutex_lock(&bch_register_lock
);
1064 bcache_device_unlink(d
);
1066 mutex_unlock(&bch_register_lock
);
1068 bch_cache_accounting_destroy(&dc
->accounting
);
1069 kobject_del(&d
->kobj
);
1071 continue_at(cl
, cached_dev_free
, system_wq
);
1074 static int cached_dev_init(struct cached_dev
*dc
, unsigned block_size
)
1078 struct request_queue
*q
= bdev_get_queue(dc
->bdev
);
1080 __module_get(THIS_MODULE
);
1081 INIT_LIST_HEAD(&dc
->list
);
1082 closure_init(&dc
->disk
.cl
, NULL
);
1083 set_closure_fn(&dc
->disk
.cl
, cached_dev_flush
, system_wq
);
1084 kobject_init(&dc
->disk
.kobj
, &bch_cached_dev_ktype
);
1085 INIT_WORK(&dc
->detach
, cached_dev_detach_finish
);
1086 closure_init_unlocked(&dc
->sb_write
);
1087 INIT_LIST_HEAD(&dc
->io_lru
);
1088 spin_lock_init(&dc
->io_lock
);
1089 bch_cache_accounting_init(&dc
->accounting
, &dc
->disk
.cl
);
1091 dc
->sequential_merge
= true;
1092 dc
->sequential_cutoff
= 4 << 20;
1094 for (io
= dc
->io
; io
< dc
->io
+ RECENT_IO
; io
++) {
1095 list_add(&io
->lru
, &dc
->io_lru
);
1096 hlist_add_head(&io
->hash
, dc
->io_hash
+ RECENT_IO
);
1099 ret
= bcache_device_init(&dc
->disk
, block_size
,
1100 dc
->bdev
->bd_part
->nr_sects
- dc
->sb
.data_offset
);
1104 set_capacity(dc
->disk
.disk
,
1105 dc
->bdev
->bd_part
->nr_sects
- dc
->sb
.data_offset
);
1107 dc
->disk
.disk
->queue
->backing_dev_info
.ra_pages
=
1108 max(dc
->disk
.disk
->queue
->backing_dev_info
.ra_pages
,
1109 q
->backing_dev_info
.ra_pages
);
1111 bch_cached_dev_request_init(dc
);
1112 bch_cached_dev_writeback_init(dc
);
1116 /* Cached device - bcache superblock */
1118 static void register_bdev(struct cache_sb
*sb
, struct page
*sb_page
,
1119 struct block_device
*bdev
,
1120 struct cached_dev
*dc
)
1122 char name
[BDEVNAME_SIZE
];
1123 const char *err
= "cannot allocate memory";
1124 struct cache_set
*c
;
1126 memcpy(&dc
->sb
, sb
, sizeof(struct cache_sb
));
1128 dc
->bdev
->bd_holder
= dc
;
1130 bio_init(&dc
->sb_bio
);
1131 dc
->sb_bio
.bi_max_vecs
= 1;
1132 dc
->sb_bio
.bi_io_vec
= dc
->sb_bio
.bi_inline_vecs
;
1133 dc
->sb_bio
.bi_io_vec
[0].bv_page
= sb_page
;
1136 if (cached_dev_init(dc
, sb
->block_size
<< 9))
1139 err
= "error creating kobject";
1140 if (kobject_add(&dc
->disk
.kobj
, &part_to_dev(bdev
->bd_part
)->kobj
,
1143 if (bch_cache_accounting_add_kobjs(&dc
->accounting
, &dc
->disk
.kobj
))
1146 pr_info("registered backing device %s", bdevname(bdev
, name
));
1148 list_add(&dc
->list
, &uncached_devices
);
1149 list_for_each_entry(c
, &bch_cache_sets
, list
)
1150 bch_cached_dev_attach(dc
, c
);
1152 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
||
1153 BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
)
1154 bch_cached_dev_run(dc
);
1158 pr_notice("error opening %s: %s", bdevname(bdev
, name
), err
);
1159 bcache_device_stop(&dc
->disk
);
1162 /* Flash only volumes */
1164 void bch_flash_dev_release(struct kobject
*kobj
)
1166 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
1171 static void flash_dev_free(struct closure
*cl
)
1173 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1174 bcache_device_free(d
);
1175 kobject_put(&d
->kobj
);
1178 static void flash_dev_flush(struct closure
*cl
)
1180 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1182 bcache_device_unlink(d
);
1183 kobject_del(&d
->kobj
);
1184 continue_at(cl
, flash_dev_free
, system_wq
);
1187 static int flash_dev_run(struct cache_set
*c
, struct uuid_entry
*u
)
1189 struct bcache_device
*d
= kzalloc(sizeof(struct bcache_device
),
1194 closure_init(&d
->cl
, NULL
);
1195 set_closure_fn(&d
->cl
, flash_dev_flush
, system_wq
);
1197 kobject_init(&d
->kobj
, &bch_flash_dev_ktype
);
1199 if (bcache_device_init(d
, block_bytes(c
), u
->sectors
))
1202 bcache_device_attach(d
, c
, u
- c
->uuids
);
1203 bch_flash_dev_request_init(d
);
1206 if (kobject_add(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "bcache"))
1209 bcache_device_link(d
, c
, "volume");
1213 kobject_put(&d
->kobj
);
1217 static int flash_devs_run(struct cache_set
*c
)
1220 struct uuid_entry
*u
;
1223 u
< c
->uuids
+ c
->nr_uuids
&& !ret
;
1225 if (UUID_FLASH_ONLY(u
))
1226 ret
= flash_dev_run(c
, u
);
1231 int bch_flash_dev_create(struct cache_set
*c
, uint64_t size
)
1233 struct uuid_entry
*u
;
1235 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1238 u
= uuid_find_empty(c
);
1240 pr_err("Can't create volume, no room for UUID");
1244 get_random_bytes(u
->uuid
, 16);
1245 memset(u
->label
, 0, 32);
1246 u
->first_reg
= u
->last_reg
= cpu_to_le32(get_seconds());
1248 SET_UUID_FLASH_ONLY(u
, 1);
1249 u
->sectors
= size
>> 9;
1253 return flash_dev_run(c
, u
);
1259 bool bch_cache_set_error(struct cache_set
*c
, const char *fmt
, ...)
1263 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1266 /* XXX: we can be called from atomic context
1267 acquire_console_sem();
1270 printk(KERN_ERR
"bcache: error on %pU: ", c
->sb
.set_uuid
);
1272 va_start(args
, fmt
);
1276 printk(", disabling caching\n");
1278 bch_cache_set_unregister(c
);
1282 void bch_cache_set_release(struct kobject
*kobj
)
1284 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
1286 module_put(THIS_MODULE
);
1289 static void cache_set_free(struct closure
*cl
)
1291 struct cache_set
*c
= container_of(cl
, struct cache_set
, cl
);
1295 if (!IS_ERR_OR_NULL(c
->debug
))
1296 debugfs_remove(c
->debug
);
1298 bch_open_buckets_free(c
);
1299 bch_btree_cache_free(c
);
1300 bch_journal_free(c
);
1302 for_each_cache(ca
, c
, i
)
1304 kobject_put(&ca
->kobj
);
1306 free_pages((unsigned long) c
->uuids
, ilog2(bucket_pages(c
)));
1307 free_pages((unsigned long) c
->sort
, ilog2(bucket_pages(c
)));
1310 bioset_free(c
->bio_split
);
1312 mempool_destroy(c
->fill_iter
);
1314 mempool_destroy(c
->bio_meta
);
1316 mempool_destroy(c
->search
);
1319 mutex_lock(&bch_register_lock
);
1321 mutex_unlock(&bch_register_lock
);
1323 pr_info("Cache set %pU unregistered", c
->sb
.set_uuid
);
1324 wake_up(&unregister_wait
);
1326 closure_debug_destroy(&c
->cl
);
1327 kobject_put(&c
->kobj
);
1330 static void cache_set_flush(struct closure
*cl
)
1332 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1337 bch_cache_accounting_destroy(&c
->accounting
);
1339 kobject_put(&c
->internal
);
1340 kobject_del(&c
->kobj
);
1342 if (!IS_ERR_OR_NULL(c
->root
))
1343 list_add(&c
->root
->list
, &c
->btree_cache
);
1345 /* Should skip this if we're unregistering because of an error */
1346 list_for_each_entry(b
, &c
->btree_cache
, list
)
1347 if (btree_node_dirty(b
))
1348 bch_btree_node_write(b
, NULL
);
1350 for_each_cache(ca
, c
, i
)
1351 if (ca
->alloc_thread
)
1352 kthread_stop(ca
->alloc_thread
);
1357 static void __cache_set_unregister(struct closure
*cl
)
1359 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1360 struct cached_dev
*dc
;
1363 mutex_lock(&bch_register_lock
);
1365 for (i
= 0; i
< c
->nr_uuids
; i
++)
1366 if (c
->devices
[i
]) {
1367 if (!UUID_FLASH_ONLY(&c
->uuids
[i
]) &&
1368 test_bit(CACHE_SET_UNREGISTERING
, &c
->flags
)) {
1369 dc
= container_of(c
->devices
[i
],
1370 struct cached_dev
, disk
);
1371 bch_cached_dev_detach(dc
);
1373 bcache_device_stop(c
->devices
[i
]);
1377 mutex_unlock(&bch_register_lock
);
1379 continue_at(cl
, cache_set_flush
, system_wq
);
1382 void bch_cache_set_stop(struct cache_set
*c
)
1384 if (!test_and_set_bit(CACHE_SET_STOPPING
, &c
->flags
))
1385 closure_queue(&c
->caching
);
1388 void bch_cache_set_unregister(struct cache_set
*c
)
1390 set_bit(CACHE_SET_UNREGISTERING
, &c
->flags
);
1391 bch_cache_set_stop(c
);
1394 #define alloc_bucket_pages(gfp, c) \
1395 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1397 struct cache_set
*bch_cache_set_alloc(struct cache_sb
*sb
)
1400 struct cache_set
*c
= kzalloc(sizeof(struct cache_set
), GFP_KERNEL
);
1404 __module_get(THIS_MODULE
);
1405 closure_init(&c
->cl
, NULL
);
1406 set_closure_fn(&c
->cl
, cache_set_free
, system_wq
);
1408 closure_init(&c
->caching
, &c
->cl
);
1409 set_closure_fn(&c
->caching
, __cache_set_unregister
, system_wq
);
1411 /* Maybe create continue_at_noreturn() and use it here? */
1412 closure_set_stopped(&c
->cl
);
1413 closure_put(&c
->cl
);
1415 kobject_init(&c
->kobj
, &bch_cache_set_ktype
);
1416 kobject_init(&c
->internal
, &bch_cache_set_internal_ktype
);
1418 bch_cache_accounting_init(&c
->accounting
, &c
->cl
);
1420 memcpy(c
->sb
.set_uuid
, sb
->set_uuid
, 16);
1421 c
->sb
.block_size
= sb
->block_size
;
1422 c
->sb
.bucket_size
= sb
->bucket_size
;
1423 c
->sb
.nr_in_set
= sb
->nr_in_set
;
1424 c
->sb
.last_mount
= sb
->last_mount
;
1425 c
->bucket_bits
= ilog2(sb
->bucket_size
);
1426 c
->block_bits
= ilog2(sb
->block_size
);
1427 c
->nr_uuids
= bucket_bytes(c
) / sizeof(struct uuid_entry
);
1429 c
->btree_pages
= c
->sb
.bucket_size
/ PAGE_SECTORS
;
1430 if (c
->btree_pages
> BTREE_MAX_PAGES
)
1431 c
->btree_pages
= max_t(int, c
->btree_pages
/ 4,
1434 c
->sort_crit_factor
= int_sqrt(c
->btree_pages
);
1436 mutex_init(&c
->bucket_lock
);
1437 mutex_init(&c
->sort_lock
);
1438 spin_lock_init(&c
->sort_time_lock
);
1439 closure_init_unlocked(&c
->sb_write
);
1440 closure_init_unlocked(&c
->uuid_write
);
1441 spin_lock_init(&c
->btree_read_time_lock
);
1442 bch_moving_init_cache_set(c
);
1444 INIT_LIST_HEAD(&c
->list
);
1445 INIT_LIST_HEAD(&c
->cached_devs
);
1446 INIT_LIST_HEAD(&c
->btree_cache
);
1447 INIT_LIST_HEAD(&c
->btree_cache_freeable
);
1448 INIT_LIST_HEAD(&c
->btree_cache_freed
);
1449 INIT_LIST_HEAD(&c
->data_buckets
);
1451 c
->search
= mempool_create_slab_pool(32, bch_search_cache
);
1455 iter_size
= (sb
->bucket_size
/ sb
->block_size
+ 1) *
1456 sizeof(struct btree_iter_set
);
1458 if (!(c
->devices
= kzalloc(c
->nr_uuids
* sizeof(void *), GFP_KERNEL
)) ||
1459 !(c
->bio_meta
= mempool_create_kmalloc_pool(2,
1460 sizeof(struct bbio
) + sizeof(struct bio_vec
) *
1461 bucket_pages(c
))) ||
1462 !(c
->fill_iter
= mempool_create_kmalloc_pool(1, iter_size
)) ||
1463 !(c
->bio_split
= bioset_create(4, offsetof(struct bbio
, bio
))) ||
1464 !(c
->sort
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1465 !(c
->uuids
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1466 bch_journal_alloc(c
) ||
1467 bch_btree_cache_alloc(c
) ||
1468 bch_open_buckets_alloc(c
))
1471 c
->congested_read_threshold_us
= 2000;
1472 c
->congested_write_threshold_us
= 20000;
1473 c
->error_limit
= 8 << IO_ERROR_SHIFT
;
1477 bch_cache_set_unregister(c
);
1481 static void run_cache_set(struct cache_set
*c
)
1483 const char *err
= "cannot allocate memory";
1484 struct cached_dev
*dc
, *t
;
1489 bch_btree_op_init_stack(&op
);
1492 for_each_cache(ca
, c
, i
)
1493 c
->nbuckets
+= ca
->sb
.nbuckets
;
1495 if (CACHE_SYNC(&c
->sb
)) {
1500 err
= "cannot allocate memory for journal";
1501 if (bch_journal_read(c
, &journal
, &op
))
1504 pr_debug("btree_journal_read() done");
1506 err
= "no journal entries found";
1507 if (list_empty(&journal
))
1510 j
= &list_entry(journal
.prev
, struct journal_replay
, list
)->j
;
1512 err
= "IO error reading priorities";
1513 for_each_cache(ca
, c
, i
)
1514 prio_read(ca
, j
->prio_bucket
[ca
->sb
.nr_this_dev
]);
1517 * If prio_read() fails it'll call cache_set_error and we'll
1518 * tear everything down right away, but if we perhaps checked
1519 * sooner we could avoid journal replay.
1524 err
= "bad btree root";
1525 if (__bch_ptr_invalid(c
, j
->btree_level
+ 1, k
))
1528 err
= "error reading btree root";
1529 c
->root
= bch_btree_node_get(c
, k
, j
->btree_level
, &op
);
1530 if (IS_ERR_OR_NULL(c
->root
))
1533 list_del_init(&c
->root
->list
);
1534 rw_unlock(true, c
->root
);
1536 err
= uuid_read(c
, j
, &op
.cl
);
1540 err
= "error in recovery";
1541 if (bch_btree_check(c
, &op
))
1544 bch_journal_mark(c
, &journal
);
1545 bch_btree_gc_finish(c
);
1546 pr_debug("btree_check() done");
1549 * bcache_journal_next() can't happen sooner, or
1550 * btree_gc_finish() will give spurious errors about last_gc >
1551 * gc_gen - this is a hack but oh well.
1553 bch_journal_next(&c
->journal
);
1555 err
= "error starting allocator thread";
1556 for_each_cache(ca
, c
, i
)
1557 if (bch_cache_allocator_start(ca
))
1561 * First place it's safe to allocate: btree_check() and
1562 * btree_gc_finish() have to run before we have buckets to
1563 * allocate, and bch_bucket_alloc_set() might cause a journal
1564 * entry to be written so bcache_journal_next() has to be called
1567 * If the uuids were in the old format we have to rewrite them
1568 * before the next journal entry is written:
1570 if (j
->version
< BCACHE_JSET_VERSION_UUID
)
1573 bch_journal_replay(c
, &journal
, &op
);
1575 pr_notice("invalidating existing data");
1576 /* Don't want invalidate_buckets() to queue a gc yet */
1577 closure_lock(&c
->gc
, NULL
);
1579 for_each_cache(ca
, c
, i
) {
1582 ca
->sb
.keys
= clamp_t(int, ca
->sb
.nbuckets
>> 7,
1583 2, SB_JOURNAL_BUCKETS
);
1585 for (j
= 0; j
< ca
->sb
.keys
; j
++)
1586 ca
->sb
.d
[j
] = ca
->sb
.first_bucket
+ j
;
1589 bch_btree_gc_finish(c
);
1591 err
= "error starting allocator thread";
1592 for_each_cache(ca
, c
, i
)
1593 if (bch_cache_allocator_start(ca
))
1596 mutex_lock(&c
->bucket_lock
);
1597 for_each_cache(ca
, c
, i
)
1599 mutex_unlock(&c
->bucket_lock
);
1601 err
= "cannot allocate new UUID bucket";
1602 if (__uuid_write(c
))
1605 err
= "cannot allocate new btree root";
1606 c
->root
= bch_btree_node_alloc(c
, 0, &op
.cl
);
1607 if (IS_ERR_OR_NULL(c
->root
))
1610 bkey_copy_key(&c
->root
->key
, &MAX_KEY
);
1611 bch_btree_node_write(c
->root
, &op
.cl
);
1613 bch_btree_set_root(c
->root
);
1614 rw_unlock(true, c
->root
);
1617 * We don't want to write the first journal entry until
1618 * everything is set up - fortunately journal entries won't be
1619 * written until the SET_CACHE_SYNC() here:
1621 SET_CACHE_SYNC(&c
->sb
, true);
1623 bch_journal_next(&c
->journal
);
1624 bch_journal_meta(c
, &op
.cl
);
1627 closure_set_stopped(&c
->gc
.cl
);
1628 closure_put(&c
->gc
.cl
);
1631 closure_sync(&op
.cl
);
1632 c
->sb
.last_mount
= get_seconds();
1633 bcache_write_super(c
);
1635 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
1636 bch_cached_dev_attach(dc
, c
);
1642 closure_set_stopped(&c
->gc
.cl
);
1643 closure_put(&c
->gc
.cl
);
1645 closure_sync(&op
.cl
);
1646 /* XXX: test this, it's broken */
1647 bch_cache_set_error(c
, err
);
1650 static bool can_attach_cache(struct cache
*ca
, struct cache_set
*c
)
1652 return ca
->sb
.block_size
== c
->sb
.block_size
&&
1653 ca
->sb
.bucket_size
== c
->sb
.block_size
&&
1654 ca
->sb
.nr_in_set
== c
->sb
.nr_in_set
;
1657 static const char *register_cache_set(struct cache
*ca
)
1660 const char *err
= "cannot allocate memory";
1661 struct cache_set
*c
;
1663 list_for_each_entry(c
, &bch_cache_sets
, list
)
1664 if (!memcmp(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16)) {
1665 if (c
->cache
[ca
->sb
.nr_this_dev
])
1666 return "duplicate cache set member";
1668 if (!can_attach_cache(ca
, c
))
1669 return "cache sb does not match set";
1671 if (!CACHE_SYNC(&ca
->sb
))
1672 SET_CACHE_SYNC(&c
->sb
, false);
1677 c
= bch_cache_set_alloc(&ca
->sb
);
1681 err
= "error creating kobject";
1682 if (kobject_add(&c
->kobj
, bcache_kobj
, "%pU", c
->sb
.set_uuid
) ||
1683 kobject_add(&c
->internal
, &c
->kobj
, "internal"))
1686 if (bch_cache_accounting_add_kobjs(&c
->accounting
, &c
->kobj
))
1689 bch_debug_init_cache_set(c
);
1691 list_add(&c
->list
, &bch_cache_sets
);
1693 sprintf(buf
, "cache%i", ca
->sb
.nr_this_dev
);
1694 if (sysfs_create_link(&ca
->kobj
, &c
->kobj
, "set") ||
1695 sysfs_create_link(&c
->kobj
, &ca
->kobj
, buf
))
1698 if (ca
->sb
.seq
> c
->sb
.seq
) {
1699 c
->sb
.version
= ca
->sb
.version
;
1700 memcpy(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16);
1701 c
->sb
.flags
= ca
->sb
.flags
;
1702 c
->sb
.seq
= ca
->sb
.seq
;
1703 pr_debug("set version = %llu", c
->sb
.version
);
1707 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = ca
;
1708 c
->cache_by_alloc
[c
->caches_loaded
++] = ca
;
1710 if (c
->caches_loaded
== c
->sb
.nr_in_set
)
1715 bch_cache_set_unregister(c
);
1721 void bch_cache_release(struct kobject
*kobj
)
1723 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
1726 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = NULL
;
1728 bch_cache_allocator_exit(ca
);
1730 bio_split_pool_free(&ca
->bio_split_hook
);
1732 free_pages((unsigned long) ca
->disk_buckets
, ilog2(bucket_pages(ca
)));
1733 kfree(ca
->prio_buckets
);
1736 free_heap(&ca
->heap
);
1737 free_fifo(&ca
->unused
);
1738 free_fifo(&ca
->free_inc
);
1739 free_fifo(&ca
->free
);
1741 if (ca
->sb_bio
.bi_inline_vecs
[0].bv_page
)
1742 put_page(ca
->sb_bio
.bi_io_vec
[0].bv_page
);
1744 if (!IS_ERR_OR_NULL(ca
->bdev
)) {
1745 blk_sync_queue(bdev_get_queue(ca
->bdev
));
1746 blkdev_put(ca
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1750 module_put(THIS_MODULE
);
1753 static int cache_alloc(struct cache_sb
*sb
, struct cache
*ca
)
1758 __module_get(THIS_MODULE
);
1759 kobject_init(&ca
->kobj
, &bch_cache_ktype
);
1761 INIT_LIST_HEAD(&ca
->discards
);
1763 bio_init(&ca
->journal
.bio
);
1764 ca
->journal
.bio
.bi_max_vecs
= 8;
1765 ca
->journal
.bio
.bi_io_vec
= ca
->journal
.bio
.bi_inline_vecs
;
1767 free
= roundup_pow_of_two(ca
->sb
.nbuckets
) >> 9;
1768 free
= max_t(size_t, free
, (prio_buckets(ca
) + 8) * 2);
1770 if (!init_fifo(&ca
->free
, free
, GFP_KERNEL
) ||
1771 !init_fifo(&ca
->free_inc
, free
<< 2, GFP_KERNEL
) ||
1772 !init_fifo(&ca
->unused
, free
<< 2, GFP_KERNEL
) ||
1773 !init_heap(&ca
->heap
, free
<< 3, GFP_KERNEL
) ||
1774 !(ca
->buckets
= vzalloc(sizeof(struct bucket
) *
1775 ca
->sb
.nbuckets
)) ||
1776 !(ca
->prio_buckets
= kzalloc(sizeof(uint64_t) * prio_buckets(ca
) *
1778 !(ca
->disk_buckets
= alloc_bucket_pages(GFP_KERNEL
, ca
)) ||
1779 bio_split_pool_init(&ca
->bio_split_hook
))
1782 ca
->prio_last_buckets
= ca
->prio_buckets
+ prio_buckets(ca
);
1784 for_each_bucket(b
, ca
)
1785 atomic_set(&b
->pin
, 0);
1787 if (bch_cache_allocator_init(ca
))
1792 kobject_put(&ca
->kobj
);
1796 static void register_cache(struct cache_sb
*sb
, struct page
*sb_page
,
1797 struct block_device
*bdev
, struct cache
*ca
)
1799 char name
[BDEVNAME_SIZE
];
1800 const char *err
= "cannot allocate memory";
1802 memcpy(&ca
->sb
, sb
, sizeof(struct cache_sb
));
1804 ca
->bdev
->bd_holder
= ca
;
1806 bio_init(&ca
->sb_bio
);
1807 ca
->sb_bio
.bi_max_vecs
= 1;
1808 ca
->sb_bio
.bi_io_vec
= ca
->sb_bio
.bi_inline_vecs
;
1809 ca
->sb_bio
.bi_io_vec
[0].bv_page
= sb_page
;
1812 if (blk_queue_discard(bdev_get_queue(ca
->bdev
)))
1813 ca
->discard
= CACHE_DISCARD(&ca
->sb
);
1815 if (cache_alloc(sb
, ca
) != 0)
1818 err
= "error creating kobject";
1819 if (kobject_add(&ca
->kobj
, &part_to_dev(bdev
->bd_part
)->kobj
, "bcache"))
1822 err
= register_cache_set(ca
);
1826 pr_info("registered cache device %s", bdevname(bdev
, name
));
1829 pr_notice("error opening %s: %s", bdevname(bdev
, name
), err
);
1830 kobject_put(&ca
->kobj
);
1833 /* Global interfaces/init */
1835 static ssize_t
register_bcache(struct kobject
*, struct kobj_attribute
*,
1836 const char *, size_t);
1838 kobj_attribute_write(register, register_bcache
);
1839 kobj_attribute_write(register_quiet
, register_bcache
);
1841 static bool bch_is_open_backing(struct block_device
*bdev
) {
1842 struct cache_set
*c
, *tc
;
1843 struct cached_dev
*dc
, *t
;
1845 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1846 list_for_each_entry_safe(dc
, t
, &c
->cached_devs
, list
)
1847 if (dc
->bdev
== bdev
)
1849 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
1850 if (dc
->bdev
== bdev
)
1855 static bool bch_is_open_cache(struct block_device
*bdev
) {
1856 struct cache_set
*c
, *tc
;
1860 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1861 for_each_cache(ca
, c
, i
)
1862 if (ca
->bdev
== bdev
)
1867 static bool bch_is_open(struct block_device
*bdev
) {
1868 return bch_is_open_cache(bdev
) || bch_is_open_backing(bdev
);
1871 static ssize_t
register_bcache(struct kobject
*k
, struct kobj_attribute
*attr
,
1872 const char *buffer
, size_t size
)
1875 const char *err
= "cannot allocate memory";
1877 struct cache_sb
*sb
= NULL
;
1878 struct block_device
*bdev
= NULL
;
1879 struct page
*sb_page
= NULL
;
1881 if (!try_module_get(THIS_MODULE
))
1884 mutex_lock(&bch_register_lock
);
1886 if (!(path
= kstrndup(buffer
, size
, GFP_KERNEL
)) ||
1887 !(sb
= kmalloc(sizeof(struct cache_sb
), GFP_KERNEL
)))
1890 err
= "failed to open device";
1891 bdev
= blkdev_get_by_path(strim(path
),
1892 FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1895 if (bdev
== ERR_PTR(-EBUSY
)) {
1896 bdev
= lookup_bdev(strim(path
));
1897 if (!IS_ERR(bdev
) && bch_is_open(bdev
))
1898 err
= "device already registered";
1900 err
= "device busy";
1905 err
= "failed to set blocksize";
1906 if (set_blocksize(bdev
, 4096))
1909 err
= read_super(sb
, bdev
, &sb_page
);
1913 if (SB_IS_BDEV(sb
)) {
1914 struct cached_dev
*dc
= kzalloc(sizeof(*dc
), GFP_KERNEL
);
1918 register_bdev(sb
, sb_page
, bdev
, dc
);
1920 struct cache
*ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
1924 register_cache(sb
, sb_page
, bdev
, ca
);
1931 mutex_unlock(&bch_register_lock
);
1932 module_put(THIS_MODULE
);
1936 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1938 if (attr
!= &ksysfs_register_quiet
)
1939 pr_info("error opening %s: %s", path
, err
);
1944 static int bcache_reboot(struct notifier_block
*n
, unsigned long code
, void *x
)
1946 if (code
== SYS_DOWN
||
1948 code
== SYS_POWER_OFF
) {
1950 unsigned long start
= jiffies
;
1951 bool stopped
= false;
1953 struct cache_set
*c
, *tc
;
1954 struct cached_dev
*dc
, *tdc
;
1956 mutex_lock(&bch_register_lock
);
1958 if (list_empty(&bch_cache_sets
) &&
1959 list_empty(&uncached_devices
))
1962 pr_info("Stopping all devices:");
1964 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1965 bch_cache_set_stop(c
);
1967 list_for_each_entry_safe(dc
, tdc
, &uncached_devices
, list
)
1968 bcache_device_stop(&dc
->disk
);
1970 /* What's a condition variable? */
1972 long timeout
= start
+ 2 * HZ
- jiffies
;
1974 stopped
= list_empty(&bch_cache_sets
) &&
1975 list_empty(&uncached_devices
);
1977 if (timeout
< 0 || stopped
)
1980 prepare_to_wait(&unregister_wait
, &wait
,
1981 TASK_UNINTERRUPTIBLE
);
1983 mutex_unlock(&bch_register_lock
);
1984 schedule_timeout(timeout
);
1985 mutex_lock(&bch_register_lock
);
1988 finish_wait(&unregister_wait
, &wait
);
1991 pr_info("All devices stopped");
1993 pr_notice("Timeout waiting for devices to be closed");
1995 mutex_unlock(&bch_register_lock
);
2001 static struct notifier_block reboot
= {
2002 .notifier_call
= bcache_reboot
,
2003 .priority
= INT_MAX
, /* before any real devices */
2006 static void bcache_exit(void)
2009 bch_writeback_exit();
2013 kobject_put(bcache_kobj
);
2015 destroy_workqueue(bcache_wq
);
2016 unregister_blkdev(bcache_major
, "bcache");
2017 unregister_reboot_notifier(&reboot
);
2020 static int __init
bcache_init(void)
2022 static const struct attribute
*files
[] = {
2023 &ksysfs_register
.attr
,
2024 &ksysfs_register_quiet
.attr
,
2028 mutex_init(&bch_register_lock
);
2029 init_waitqueue_head(&unregister_wait
);
2030 register_reboot_notifier(&reboot
);
2031 closure_debug_init();
2033 bcache_major
= register_blkdev(0, "bcache");
2034 if (bcache_major
< 0)
2035 return bcache_major
;
2037 if (!(bcache_wq
= create_workqueue("bcache")) ||
2038 !(bcache_kobj
= kobject_create_and_add("bcache", fs_kobj
)) ||
2039 sysfs_create_files(bcache_kobj
, files
) ||
2041 bch_request_init() ||
2042 bch_writeback_init() ||
2043 bch_debug_init(bcache_kobj
))
2052 module_exit(bcache_exit
);
2053 module_init(bcache_init
);