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 static struct kobject
*bcache_kobj
;
49 struct mutex bch_register_lock
;
50 LIST_HEAD(bch_cache_sets
);
51 static LIST_HEAD(uncached_devices
);
53 static int bcache_major
, bcache_minor
;
54 static wait_queue_head_t unregister_wait
;
55 struct workqueue_struct
*bcache_wq
;
57 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
59 static void bio_split_pool_free(struct bio_split_pool
*p
)
61 if (p
->bio_split_hook
)
62 mempool_destroy(p
->bio_split_hook
);
65 bioset_free(p
->bio_split
);
68 static int bio_split_pool_init(struct bio_split_pool
*p
)
70 p
->bio_split
= bioset_create(4, 0);
74 p
->bio_split_hook
= mempool_create_kmalloc_pool(4,
75 sizeof(struct bio_split_hook
));
76 if (!p
->bio_split_hook
)
84 static const char *read_super(struct cache_sb
*sb
, struct block_device
*bdev
,
89 struct buffer_head
*bh
= __bread(bdev
, 1, SB_SIZE
);
95 s
= (struct cache_sb
*) bh
->b_data
;
97 sb
->offset
= le64_to_cpu(s
->offset
);
98 sb
->version
= le64_to_cpu(s
->version
);
100 memcpy(sb
->magic
, s
->magic
, 16);
101 memcpy(sb
->uuid
, s
->uuid
, 16);
102 memcpy(sb
->set_uuid
, s
->set_uuid
, 16);
103 memcpy(sb
->label
, s
->label
, SB_LABEL_SIZE
);
105 sb
->flags
= le64_to_cpu(s
->flags
);
106 sb
->seq
= le64_to_cpu(s
->seq
);
107 sb
->last_mount
= le32_to_cpu(s
->last_mount
);
108 sb
->first_bucket
= le16_to_cpu(s
->first_bucket
);
109 sb
->keys
= le16_to_cpu(s
->keys
);
111 for (i
= 0; i
< SB_JOURNAL_BUCKETS
; i
++)
112 sb
->d
[i
] = le64_to_cpu(s
->d
[i
]);
114 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
115 sb
->version
, sb
->flags
, sb
->seq
, sb
->keys
);
117 err
= "Not a bcache superblock";
118 if (sb
->offset
!= SB_SECTOR
)
121 if (memcmp(sb
->magic
, bcache_magic
, 16))
124 err
= "Too many journal buckets";
125 if (sb
->keys
> SB_JOURNAL_BUCKETS
)
128 err
= "Bad checksum";
129 if (s
->csum
!= csum_set(s
))
133 if (bch_is_zero(sb
->uuid
, 16))
136 sb
->block_size
= le16_to_cpu(s
->block_size
);
138 err
= "Superblock block size smaller than device block size";
139 if (sb
->block_size
<< 9 < bdev_logical_block_size(bdev
))
142 switch (sb
->version
) {
143 case BCACHE_SB_VERSION_BDEV
:
144 sb
->data_offset
= BDEV_DATA_START_DEFAULT
;
146 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET
:
147 sb
->data_offset
= le64_to_cpu(s
->data_offset
);
149 err
= "Bad data offset";
150 if (sb
->data_offset
< BDEV_DATA_START_DEFAULT
)
154 case BCACHE_SB_VERSION_CDEV
:
155 case BCACHE_SB_VERSION_CDEV_WITH_UUID
:
156 sb
->nbuckets
= le64_to_cpu(s
->nbuckets
);
157 sb
->block_size
= le16_to_cpu(s
->block_size
);
158 sb
->bucket_size
= le16_to_cpu(s
->bucket_size
);
160 sb
->nr_in_set
= le16_to_cpu(s
->nr_in_set
);
161 sb
->nr_this_dev
= le16_to_cpu(s
->nr_this_dev
);
163 err
= "Too many buckets";
164 if (sb
->nbuckets
> LONG_MAX
)
167 err
= "Not enough buckets";
168 if (sb
->nbuckets
< 1 << 7)
171 err
= "Bad block/bucket size";
172 if (!is_power_of_2(sb
->block_size
) ||
173 sb
->block_size
> PAGE_SECTORS
||
174 !is_power_of_2(sb
->bucket_size
) ||
175 sb
->bucket_size
< PAGE_SECTORS
)
178 err
= "Invalid superblock: device too small";
179 if (get_capacity(bdev
->bd_disk
) < sb
->bucket_size
* sb
->nbuckets
)
183 if (bch_is_zero(sb
->set_uuid
, 16))
186 err
= "Bad cache device number in set";
187 if (!sb
->nr_in_set
||
188 sb
->nr_in_set
<= sb
->nr_this_dev
||
189 sb
->nr_in_set
> MAX_CACHES_PER_SET
)
192 err
= "Journal buckets not sequential";
193 for (i
= 0; i
< sb
->keys
; i
++)
194 if (sb
->d
[i
] != sb
->first_bucket
+ i
)
197 err
= "Too many journal buckets";
198 if (sb
->first_bucket
+ sb
->keys
> sb
->nbuckets
)
201 err
= "Invalid superblock: first bucket comes before end of super";
202 if (sb
->first_bucket
* sb
->bucket_size
< 16)
207 err
= "Unsupported superblock version";
211 sb
->last_mount
= get_seconds();
214 get_page(bh
->b_page
);
221 static void write_bdev_super_endio(struct bio
*bio
, int error
)
223 struct cached_dev
*dc
= bio
->bi_private
;
224 /* XXX: error checking */
226 closure_put(&dc
->sb_write
.cl
);
229 static void __write_super(struct cache_sb
*sb
, struct bio
*bio
)
231 struct cache_sb
*out
= page_address(bio
->bi_io_vec
[0].bv_page
);
234 bio
->bi_sector
= SB_SECTOR
;
235 bio
->bi_rw
= REQ_SYNC
|REQ_META
;
236 bio
->bi_size
= SB_SIZE
;
237 bch_bio_map(bio
, NULL
);
239 out
->offset
= cpu_to_le64(sb
->offset
);
240 out
->version
= cpu_to_le64(sb
->version
);
242 memcpy(out
->uuid
, sb
->uuid
, 16);
243 memcpy(out
->set_uuid
, sb
->set_uuid
, 16);
244 memcpy(out
->label
, sb
->label
, SB_LABEL_SIZE
);
246 out
->flags
= cpu_to_le64(sb
->flags
);
247 out
->seq
= cpu_to_le64(sb
->seq
);
249 out
->last_mount
= cpu_to_le32(sb
->last_mount
);
250 out
->first_bucket
= cpu_to_le16(sb
->first_bucket
);
251 out
->keys
= cpu_to_le16(sb
->keys
);
253 for (i
= 0; i
< sb
->keys
; i
++)
254 out
->d
[i
] = cpu_to_le64(sb
->d
[i
]);
256 out
->csum
= csum_set(out
);
258 pr_debug("ver %llu, flags %llu, seq %llu",
259 sb
->version
, sb
->flags
, sb
->seq
);
261 submit_bio(REQ_WRITE
, bio
);
264 void bch_write_bdev_super(struct cached_dev
*dc
, struct closure
*parent
)
266 struct closure
*cl
= &dc
->sb_write
.cl
;
267 struct bio
*bio
= &dc
->sb_bio
;
269 closure_lock(&dc
->sb_write
, parent
);
272 bio
->bi_bdev
= dc
->bdev
;
273 bio
->bi_end_io
= write_bdev_super_endio
;
274 bio
->bi_private
= dc
;
277 __write_super(&dc
->sb
, bio
);
282 static void write_super_endio(struct bio
*bio
, int error
)
284 struct cache
*ca
= bio
->bi_private
;
286 bch_count_io_errors(ca
, error
, "writing superblock");
287 closure_put(&ca
->set
->sb_write
.cl
);
290 void bcache_write_super(struct cache_set
*c
)
292 struct closure
*cl
= &c
->sb_write
.cl
;
296 closure_lock(&c
->sb_write
, &c
->cl
);
300 for_each_cache(ca
, c
, i
) {
301 struct bio
*bio
= &ca
->sb_bio
;
303 ca
->sb
.version
= BCACHE_SB_VERSION_CDEV_WITH_UUID
;
304 ca
->sb
.seq
= c
->sb
.seq
;
305 ca
->sb
.last_mount
= c
->sb
.last_mount
;
307 SET_CACHE_SYNC(&ca
->sb
, CACHE_SYNC(&c
->sb
));
310 bio
->bi_bdev
= ca
->bdev
;
311 bio
->bi_end_io
= write_super_endio
;
312 bio
->bi_private
= ca
;
315 __write_super(&ca
->sb
, bio
);
323 static void uuid_endio(struct bio
*bio
, int error
)
325 struct closure
*cl
= bio
->bi_private
;
326 struct cache_set
*c
= container_of(cl
, struct cache_set
, uuid_write
.cl
);
328 cache_set_err_on(error
, c
, "accessing uuids");
329 bch_bbio_free(bio
, c
);
333 static void uuid_io(struct cache_set
*c
, unsigned long rw
,
334 struct bkey
*k
, struct closure
*parent
)
336 struct closure
*cl
= &c
->uuid_write
.cl
;
337 struct uuid_entry
*u
;
342 closure_lock(&c
->uuid_write
, parent
);
344 for (i
= 0; i
< KEY_PTRS(k
); i
++) {
345 struct bio
*bio
= bch_bbio_alloc(c
);
347 bio
->bi_rw
= REQ_SYNC
|REQ_META
|rw
;
348 bio
->bi_size
= KEY_SIZE(k
) << 9;
350 bio
->bi_end_io
= uuid_endio
;
351 bio
->bi_private
= cl
;
352 bch_bio_map(bio
, c
->uuids
);
354 bch_submit_bbio(bio
, c
, k
, i
);
360 bch_bkey_to_text(buf
, sizeof(buf
), k
);
361 pr_debug("%s UUIDs at %s", rw
& REQ_WRITE
? "wrote" : "read", buf
);
363 for (u
= c
->uuids
; u
< c
->uuids
+ c
->nr_uuids
; u
++)
364 if (!bch_is_zero(u
->uuid
, 16))
365 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
366 u
- c
->uuids
, u
->uuid
, u
->label
,
367 u
->first_reg
, u
->last_reg
, u
->invalidated
);
372 static char *uuid_read(struct cache_set
*c
, struct jset
*j
, struct closure
*cl
)
374 struct bkey
*k
= &j
->uuid_bucket
;
376 if (__bch_ptr_invalid(c
, 1, k
))
377 return "bad uuid pointer";
379 bkey_copy(&c
->uuid_bucket
, k
);
380 uuid_io(c
, READ_SYNC
, k
, cl
);
382 if (j
->version
< BCACHE_JSET_VERSION_UUIDv1
) {
383 struct uuid_entry_v0
*u0
= (void *) c
->uuids
;
384 struct uuid_entry
*u1
= (void *) c
->uuids
;
390 * Since the new uuid entry is bigger than the old, we have to
391 * convert starting at the highest memory address and work down
392 * in order to do it in place
395 for (i
= c
->nr_uuids
- 1;
398 memcpy(u1
[i
].uuid
, u0
[i
].uuid
, 16);
399 memcpy(u1
[i
].label
, u0
[i
].label
, 32);
401 u1
[i
].first_reg
= u0
[i
].first_reg
;
402 u1
[i
].last_reg
= u0
[i
].last_reg
;
403 u1
[i
].invalidated
= u0
[i
].invalidated
;
413 static int __uuid_write(struct cache_set
*c
)
417 closure_init_stack(&cl
);
419 lockdep_assert_held(&bch_register_lock
);
421 if (bch_bucket_alloc_set(c
, WATERMARK_METADATA
, &k
.key
, 1, true))
424 SET_KEY_SIZE(&k
.key
, c
->sb
.bucket_size
);
425 uuid_io(c
, REQ_WRITE
, &k
.key
, &cl
);
428 bkey_copy(&c
->uuid_bucket
, &k
.key
);
429 __bkey_put(c
, &k
.key
);
433 int bch_uuid_write(struct cache_set
*c
)
435 int ret
= __uuid_write(c
);
438 bch_journal_meta(c
, NULL
);
443 static struct uuid_entry
*uuid_find(struct cache_set
*c
, const char *uuid
)
445 struct uuid_entry
*u
;
448 u
< c
->uuids
+ c
->nr_uuids
; u
++)
449 if (!memcmp(u
->uuid
, uuid
, 16))
455 static struct uuid_entry
*uuid_find_empty(struct cache_set
*c
)
457 static const char zero_uuid
[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
458 return uuid_find(c
, zero_uuid
);
462 * Bucket priorities/gens:
464 * For each bucket, we store on disk its
468 * See alloc.c for an explanation of the gen. The priority is used to implement
469 * lru (and in the future other) cache replacement policies; for most purposes
470 * it's just an opaque integer.
472 * The gens and the priorities don't have a whole lot to do with each other, and
473 * it's actually the gens that must be written out at specific times - it's no
474 * big deal if the priorities don't get written, if we lose them we just reuse
475 * buckets in suboptimal order.
477 * On disk they're stored in a packed array, and in as many buckets are required
478 * to fit them all. The buckets we use to store them form a list; the journal
479 * header points to the first bucket, the first bucket points to the second
482 * This code is used by the allocation code; periodically (whenever it runs out
483 * of buckets to allocate from) the allocation code will invalidate some
484 * buckets, but it can't use those buckets until their new gens are safely on
488 static void prio_endio(struct bio
*bio
, int error
)
490 struct cache
*ca
= bio
->bi_private
;
492 cache_set_err_on(error
, ca
->set
, "accessing priorities");
493 bch_bbio_free(bio
, ca
->set
);
494 closure_put(&ca
->prio
);
497 static void prio_io(struct cache
*ca
, uint64_t bucket
, unsigned long rw
)
499 struct closure
*cl
= &ca
->prio
;
500 struct bio
*bio
= bch_bbio_alloc(ca
->set
);
502 closure_init_stack(cl
);
504 bio
->bi_sector
= bucket
* ca
->sb
.bucket_size
;
505 bio
->bi_bdev
= ca
->bdev
;
506 bio
->bi_rw
= REQ_SYNC
|REQ_META
|rw
;
507 bio
->bi_size
= bucket_bytes(ca
);
509 bio
->bi_end_io
= prio_endio
;
510 bio
->bi_private
= ca
;
511 bch_bio_map(bio
, ca
->disk_buckets
);
513 closure_bio_submit(bio
, &ca
->prio
, ca
);
517 #define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
518 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
520 void bch_prio_write(struct cache
*ca
)
526 closure_init_stack(&cl
);
528 lockdep_assert_held(&ca
->set
->bucket_lock
);
530 for (b
= ca
->buckets
;
531 b
< ca
->buckets
+ ca
->sb
.nbuckets
; b
++)
532 b
->disk_gen
= b
->gen
;
534 ca
->disk_buckets
->seq
++;
536 atomic_long_add(ca
->sb
.bucket_size
* prio_buckets(ca
),
537 &ca
->meta_sectors_written
);
539 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca
->free
),
540 fifo_used(&ca
->free_inc
), fifo_used(&ca
->unused
));
542 for (i
= prio_buckets(ca
) - 1; i
>= 0; --i
) {
544 struct prio_set
*p
= ca
->disk_buckets
;
545 struct bucket_disk
*d
= p
->data
;
546 struct bucket_disk
*end
= d
+ prios_per_bucket(ca
);
548 for (b
= ca
->buckets
+ i
* prios_per_bucket(ca
);
549 b
< ca
->buckets
+ ca
->sb
.nbuckets
&& d
< end
;
551 d
->prio
= cpu_to_le16(b
->prio
);
555 p
->next_bucket
= ca
->prio_buckets
[i
+ 1];
556 p
->magic
= pset_magic(&ca
->sb
);
557 p
->csum
= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8);
559 bucket
= bch_bucket_alloc(ca
, WATERMARK_PRIO
, true);
560 BUG_ON(bucket
== -1);
562 mutex_unlock(&ca
->set
->bucket_lock
);
563 prio_io(ca
, bucket
, REQ_WRITE
);
564 mutex_lock(&ca
->set
->bucket_lock
);
566 ca
->prio_buckets
[i
] = bucket
;
567 atomic_dec_bug(&ca
->buckets
[bucket
].pin
);
570 mutex_unlock(&ca
->set
->bucket_lock
);
572 bch_journal_meta(ca
->set
, &cl
);
575 mutex_lock(&ca
->set
->bucket_lock
);
577 ca
->need_save_prio
= 0;
580 * Don't want the old priorities to get garbage collected until after we
581 * finish writing the new ones, and they're journalled
583 for (i
= 0; i
< prio_buckets(ca
); i
++)
584 ca
->prio_last_buckets
[i
] = ca
->prio_buckets
[i
];
587 static void prio_read(struct cache
*ca
, uint64_t bucket
)
589 struct prio_set
*p
= ca
->disk_buckets
;
590 struct bucket_disk
*d
= p
->data
+ prios_per_bucket(ca
), *end
= d
;
592 unsigned bucket_nr
= 0;
594 for (b
= ca
->buckets
;
595 b
< ca
->buckets
+ ca
->sb
.nbuckets
;
598 ca
->prio_buckets
[bucket_nr
] = bucket
;
599 ca
->prio_last_buckets
[bucket_nr
] = bucket
;
602 prio_io(ca
, bucket
, READ_SYNC
);
604 if (p
->csum
!= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8))
605 pr_warn("bad csum reading priorities");
607 if (p
->magic
!= pset_magic(&ca
->sb
))
608 pr_warn("bad magic reading priorities");
610 bucket
= p
->next_bucket
;
614 b
->prio
= le16_to_cpu(d
->prio
);
615 b
->gen
= b
->disk_gen
= b
->last_gc
= b
->gc_gen
= d
->gen
;
621 static int open_dev(struct block_device
*b
, fmode_t mode
)
623 struct bcache_device
*d
= b
->bd_disk
->private_data
;
624 if (atomic_read(&d
->closing
))
631 static void release_dev(struct gendisk
*b
, fmode_t mode
)
633 struct bcache_device
*d
= b
->private_data
;
637 static int ioctl_dev(struct block_device
*b
, fmode_t mode
,
638 unsigned int cmd
, unsigned long arg
)
640 struct bcache_device
*d
= b
->bd_disk
->private_data
;
641 return d
->ioctl(d
, mode
, cmd
, arg
);
644 static const struct block_device_operations bcache_ops
= {
646 .release
= release_dev
,
648 .owner
= THIS_MODULE
,
651 void bcache_device_stop(struct bcache_device
*d
)
653 if (!atomic_xchg(&d
->closing
, 1))
654 closure_queue(&d
->cl
);
657 static void bcache_device_unlink(struct bcache_device
*d
)
662 sysfs_remove_link(&d
->c
->kobj
, d
->name
);
663 sysfs_remove_link(&d
->kobj
, "cache");
665 for_each_cache(ca
, d
->c
, i
)
666 bd_unlink_disk_holder(ca
->bdev
, d
->disk
);
669 static void bcache_device_link(struct bcache_device
*d
, struct cache_set
*c
,
675 for_each_cache(ca
, d
->c
, i
)
676 bd_link_disk_holder(ca
->bdev
, d
->disk
);
678 snprintf(d
->name
, BCACHEDEVNAME_SIZE
,
679 "%s%u", name
, d
->id
);
681 WARN(sysfs_create_link(&d
->kobj
, &c
->kobj
, "cache") ||
682 sysfs_create_link(&c
->kobj
, &d
->kobj
, d
->name
),
683 "Couldn't create device <-> cache set symlinks");
686 static void bcache_device_detach(struct bcache_device
*d
)
688 lockdep_assert_held(&bch_register_lock
);
690 if (atomic_read(&d
->detaching
)) {
691 struct uuid_entry
*u
= d
->c
->uuids
+ d
->id
;
693 SET_UUID_FLASH_ONLY(u
, 0);
694 memcpy(u
->uuid
, invalid_uuid
, 16);
695 u
->invalidated
= cpu_to_le32(get_seconds());
696 bch_uuid_write(d
->c
);
698 atomic_set(&d
->detaching
, 0);
702 bcache_device_unlink(d
);
704 d
->c
->devices
[d
->id
] = NULL
;
705 closure_put(&d
->c
->caching
);
709 static void bcache_device_attach(struct bcache_device
*d
, struct cache_set
*c
,
712 BUG_ON(test_bit(CACHE_SET_STOPPING
, &c
->flags
));
718 closure_get(&c
->caching
);
721 static void bcache_device_free(struct bcache_device
*d
)
723 lockdep_assert_held(&bch_register_lock
);
725 pr_info("%s stopped", d
->disk
->disk_name
);
728 bcache_device_detach(d
);
729 if (d
->disk
&& d
->disk
->flags
& GENHD_FL_UP
)
730 del_gendisk(d
->disk
);
731 if (d
->disk
&& d
->disk
->queue
)
732 blk_cleanup_queue(d
->disk
->queue
);
736 bio_split_pool_free(&d
->bio_split_hook
);
737 if (d
->unaligned_bvec
)
738 mempool_destroy(d
->unaligned_bvec
);
740 bioset_free(d
->bio_split
);
741 if (is_vmalloc_addr(d
->stripe_sectors_dirty
))
742 vfree(d
->stripe_sectors_dirty
);
744 kfree(d
->stripe_sectors_dirty
);
746 closure_debug_destroy(&d
->cl
);
749 static int bcache_device_init(struct bcache_device
*d
, unsigned block_size
,
752 struct request_queue
*q
;
756 d
->stripe_size
= 1 << 31;
758 d
->nr_stripes
= DIV_ROUND_UP_ULL(sectors
, d
->stripe_size
);
760 if (!d
->nr_stripes
|| d
->nr_stripes
> SIZE_MAX
/ sizeof(atomic_t
))
763 n
= d
->nr_stripes
* sizeof(atomic_t
);
764 d
->stripe_sectors_dirty
= n
< PAGE_SIZE
<< 6
765 ? kzalloc(n
, GFP_KERNEL
)
767 if (!d
->stripe_sectors_dirty
)
770 if (!(d
->bio_split
= bioset_create(4, offsetof(struct bbio
, bio
))) ||
771 !(d
->unaligned_bvec
= mempool_create_kmalloc_pool(1,
772 sizeof(struct bio_vec
) * BIO_MAX_PAGES
)) ||
773 bio_split_pool_init(&d
->bio_split_hook
) ||
774 !(d
->disk
= alloc_disk(1)) ||
775 !(q
= blk_alloc_queue(GFP_KERNEL
)))
778 set_capacity(d
->disk
, sectors
);
779 snprintf(d
->disk
->disk_name
, DISK_NAME_LEN
, "bcache%i", bcache_minor
);
781 d
->disk
->major
= bcache_major
;
782 d
->disk
->first_minor
= bcache_minor
++;
783 d
->disk
->fops
= &bcache_ops
;
784 d
->disk
->private_data
= d
;
786 blk_queue_make_request(q
, NULL
);
789 q
->backing_dev_info
.congested_data
= d
;
790 q
->limits
.max_hw_sectors
= UINT_MAX
;
791 q
->limits
.max_sectors
= UINT_MAX
;
792 q
->limits
.max_segment_size
= UINT_MAX
;
793 q
->limits
.max_segments
= BIO_MAX_PAGES
;
794 q
->limits
.max_discard_sectors
= UINT_MAX
;
795 q
->limits
.io_min
= block_size
;
796 q
->limits
.logical_block_size
= block_size
;
797 q
->limits
.physical_block_size
= block_size
;
798 set_bit(QUEUE_FLAG_NONROT
, &d
->disk
->queue
->queue_flags
);
799 set_bit(QUEUE_FLAG_DISCARD
, &d
->disk
->queue
->queue_flags
);
801 blk_queue_flush(q
, REQ_FLUSH
|REQ_FUA
);
808 static void calc_cached_dev_sectors(struct cache_set
*c
)
810 uint64_t sectors
= 0;
811 struct cached_dev
*dc
;
813 list_for_each_entry(dc
, &c
->cached_devs
, list
)
814 sectors
+= bdev_sectors(dc
->bdev
);
816 c
->cached_dev_sectors
= sectors
;
819 void bch_cached_dev_run(struct cached_dev
*dc
)
821 struct bcache_device
*d
= &dc
->disk
;
822 char buf
[SB_LABEL_SIZE
+ 1];
825 kasprintf(GFP_KERNEL
, "CACHED_UUID=%pU", dc
->sb
.uuid
),
830 memcpy(buf
, dc
->sb
.label
, SB_LABEL_SIZE
);
831 buf
[SB_LABEL_SIZE
] = '\0';
832 env
[2] = kasprintf(GFP_KERNEL
, "CACHED_LABEL=%s", buf
);
834 if (atomic_xchg(&dc
->running
, 1))
838 BDEV_STATE(&dc
->sb
) != BDEV_STATE_NONE
) {
840 closure_init_stack(&cl
);
842 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_STALE
);
843 bch_write_bdev_super(dc
, &cl
);
848 bd_link_disk_holder(dc
->bdev
, dc
->disk
.disk
);
849 /* won't show up in the uevent file, use udevadm monitor -e instead
850 * only class / kset properties are persistent */
851 kobject_uevent_env(&disk_to_dev(d
->disk
)->kobj
, KOBJ_CHANGE
, env
);
855 if (sysfs_create_link(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "dev") ||
856 sysfs_create_link(&disk_to_dev(d
->disk
)->kobj
, &d
->kobj
, "bcache"))
857 pr_debug("error creating sysfs link");
860 static void cached_dev_detach_finish(struct work_struct
*w
)
862 struct cached_dev
*dc
= container_of(w
, struct cached_dev
, detach
);
863 char buf
[BDEVNAME_SIZE
];
865 closure_init_stack(&cl
);
867 BUG_ON(!atomic_read(&dc
->disk
.detaching
));
868 BUG_ON(atomic_read(&dc
->count
));
870 mutex_lock(&bch_register_lock
);
872 memset(&dc
->sb
.set_uuid
, 0, 16);
873 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_NONE
);
875 bch_write_bdev_super(dc
, &cl
);
878 bcache_device_detach(&dc
->disk
);
879 list_move(&dc
->list
, &uncached_devices
);
881 mutex_unlock(&bch_register_lock
);
883 pr_info("Caching disabled for %s", bdevname(dc
->bdev
, buf
));
885 /* Drop ref we took in cached_dev_detach() */
886 closure_put(&dc
->disk
.cl
);
889 void bch_cached_dev_detach(struct cached_dev
*dc
)
891 lockdep_assert_held(&bch_register_lock
);
893 if (atomic_read(&dc
->disk
.closing
))
896 if (atomic_xchg(&dc
->disk
.detaching
, 1))
900 * Block the device from being closed and freed until we're finished
903 closure_get(&dc
->disk
.cl
);
905 bch_writeback_queue(dc
);
909 int bch_cached_dev_attach(struct cached_dev
*dc
, struct cache_set
*c
)
911 uint32_t rtime
= cpu_to_le32(get_seconds());
912 struct uuid_entry
*u
;
913 char buf
[BDEVNAME_SIZE
];
915 bdevname(dc
->bdev
, buf
);
917 if (memcmp(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16))
921 pr_err("Can't attach %s: already attached", buf
);
925 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
)) {
926 pr_err("Can't attach %s: shutting down", buf
);
930 if (dc
->sb
.block_size
< c
->sb
.block_size
) {
932 pr_err("Couldn't attach %s: block size less than set's block size",
937 u
= uuid_find(c
, dc
->sb
.uuid
);
940 (BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
||
941 BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
)) {
942 memcpy(u
->uuid
, invalid_uuid
, 16);
943 u
->invalidated
= cpu_to_le32(get_seconds());
948 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
949 pr_err("Couldn't find uuid for %s in set", buf
);
953 u
= uuid_find_empty(c
);
955 pr_err("Not caching %s, no room for UUID", buf
);
960 /* Deadlocks since we're called via sysfs...
961 sysfs_remove_file(&dc->kobj, &sysfs_attach);
964 if (bch_is_zero(u
->uuid
, 16)) {
966 closure_init_stack(&cl
);
968 memcpy(u
->uuid
, dc
->sb
.uuid
, 16);
969 memcpy(u
->label
, dc
->sb
.label
, SB_LABEL_SIZE
);
970 u
->first_reg
= u
->last_reg
= rtime
;
973 memcpy(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16);
974 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_CLEAN
);
976 bch_write_bdev_super(dc
, &cl
);
983 bcache_device_attach(&dc
->disk
, c
, u
- c
->uuids
);
984 list_move(&dc
->list
, &c
->cached_devs
);
985 calc_cached_dev_sectors(c
);
989 * dc->c must be set before dc->count != 0 - paired with the mb in
992 atomic_set(&dc
->count
, 1);
994 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
995 bch_sectors_dirty_init(dc
);
996 atomic_set(&dc
->has_dirty
, 1);
997 atomic_inc(&dc
->count
);
998 bch_writeback_queue(dc
);
1001 bch_cached_dev_run(dc
);
1002 bcache_device_link(&dc
->disk
, c
, "bdev");
1004 pr_info("Caching %s as %s on set %pU",
1005 bdevname(dc
->bdev
, buf
), dc
->disk
.disk
->disk_name
,
1006 dc
->disk
.c
->sb
.set_uuid
);
1010 void bch_cached_dev_release(struct kobject
*kobj
)
1012 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
1015 module_put(THIS_MODULE
);
1018 static void cached_dev_free(struct closure
*cl
)
1020 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
1022 cancel_delayed_work_sync(&dc
->writeback_rate_update
);
1023 kthread_stop(dc
->writeback_thread
);
1025 mutex_lock(&bch_register_lock
);
1027 if (atomic_read(&dc
->running
))
1028 bd_unlink_disk_holder(dc
->bdev
, dc
->disk
.disk
);
1029 bcache_device_free(&dc
->disk
);
1030 list_del(&dc
->list
);
1032 mutex_unlock(&bch_register_lock
);
1034 if (!IS_ERR_OR_NULL(dc
->bdev
)) {
1035 if (dc
->bdev
->bd_disk
)
1036 blk_sync_queue(bdev_get_queue(dc
->bdev
));
1038 blkdev_put(dc
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1041 wake_up(&unregister_wait
);
1043 kobject_put(&dc
->disk
.kobj
);
1046 static void cached_dev_flush(struct closure
*cl
)
1048 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
1049 struct bcache_device
*d
= &dc
->disk
;
1051 mutex_lock(&bch_register_lock
);
1055 bcache_device_unlink(d
);
1057 mutex_unlock(&bch_register_lock
);
1059 bch_cache_accounting_destroy(&dc
->accounting
);
1060 kobject_del(&d
->kobj
);
1062 continue_at(cl
, cached_dev_free
, system_wq
);
1065 static int cached_dev_init(struct cached_dev
*dc
, unsigned block_size
)
1069 struct request_queue
*q
= bdev_get_queue(dc
->bdev
);
1071 __module_get(THIS_MODULE
);
1072 INIT_LIST_HEAD(&dc
->list
);
1073 closure_init(&dc
->disk
.cl
, NULL
);
1074 set_closure_fn(&dc
->disk
.cl
, cached_dev_flush
, system_wq
);
1075 kobject_init(&dc
->disk
.kobj
, &bch_cached_dev_ktype
);
1076 INIT_WORK(&dc
->detach
, cached_dev_detach_finish
);
1077 closure_init_unlocked(&dc
->sb_write
);
1078 INIT_LIST_HEAD(&dc
->io_lru
);
1079 spin_lock_init(&dc
->io_lock
);
1080 bch_cache_accounting_init(&dc
->accounting
, &dc
->disk
.cl
);
1082 dc
->sequential_merge
= true;
1083 dc
->sequential_cutoff
= 4 << 20;
1085 for (io
= dc
->io
; io
< dc
->io
+ RECENT_IO
; io
++) {
1086 list_add(&io
->lru
, &dc
->io_lru
);
1087 hlist_add_head(&io
->hash
, dc
->io_hash
+ RECENT_IO
);
1090 ret
= bcache_device_init(&dc
->disk
, block_size
,
1091 dc
->bdev
->bd_part
->nr_sects
- dc
->sb
.data_offset
);
1095 set_capacity(dc
->disk
.disk
,
1096 dc
->bdev
->bd_part
->nr_sects
- dc
->sb
.data_offset
);
1098 dc
->disk
.disk
->queue
->backing_dev_info
.ra_pages
=
1099 max(dc
->disk
.disk
->queue
->backing_dev_info
.ra_pages
,
1100 q
->backing_dev_info
.ra_pages
);
1102 bch_cached_dev_request_init(dc
);
1103 bch_cached_dev_writeback_init(dc
);
1107 /* Cached device - bcache superblock */
1109 static void register_bdev(struct cache_sb
*sb
, struct page
*sb_page
,
1110 struct block_device
*bdev
,
1111 struct cached_dev
*dc
)
1113 char name
[BDEVNAME_SIZE
];
1114 const char *err
= "cannot allocate memory";
1115 struct cache_set
*c
;
1117 memcpy(&dc
->sb
, sb
, sizeof(struct cache_sb
));
1119 dc
->bdev
->bd_holder
= dc
;
1121 bio_init(&dc
->sb_bio
);
1122 dc
->sb_bio
.bi_max_vecs
= 1;
1123 dc
->sb_bio
.bi_io_vec
= dc
->sb_bio
.bi_inline_vecs
;
1124 dc
->sb_bio
.bi_io_vec
[0].bv_page
= sb_page
;
1127 if (cached_dev_init(dc
, sb
->block_size
<< 9))
1130 err
= "error creating kobject";
1131 if (kobject_add(&dc
->disk
.kobj
, &part_to_dev(bdev
->bd_part
)->kobj
,
1134 if (bch_cache_accounting_add_kobjs(&dc
->accounting
, &dc
->disk
.kobj
))
1137 pr_info("registered backing device %s", bdevname(bdev
, name
));
1139 list_add(&dc
->list
, &uncached_devices
);
1140 list_for_each_entry(c
, &bch_cache_sets
, list
)
1141 bch_cached_dev_attach(dc
, c
);
1143 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
||
1144 BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
)
1145 bch_cached_dev_run(dc
);
1149 pr_notice("error opening %s: %s", bdevname(bdev
, name
), err
);
1150 bcache_device_stop(&dc
->disk
);
1153 /* Flash only volumes */
1155 void bch_flash_dev_release(struct kobject
*kobj
)
1157 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
1162 static void flash_dev_free(struct closure
*cl
)
1164 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1165 bcache_device_free(d
);
1166 kobject_put(&d
->kobj
);
1169 static void flash_dev_flush(struct closure
*cl
)
1171 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1173 bcache_device_unlink(d
);
1174 kobject_del(&d
->kobj
);
1175 continue_at(cl
, flash_dev_free
, system_wq
);
1178 static int flash_dev_run(struct cache_set
*c
, struct uuid_entry
*u
)
1180 struct bcache_device
*d
= kzalloc(sizeof(struct bcache_device
),
1185 closure_init(&d
->cl
, NULL
);
1186 set_closure_fn(&d
->cl
, flash_dev_flush
, system_wq
);
1188 kobject_init(&d
->kobj
, &bch_flash_dev_ktype
);
1190 if (bcache_device_init(d
, block_bytes(c
), u
->sectors
))
1193 bcache_device_attach(d
, c
, u
- c
->uuids
);
1194 bch_flash_dev_request_init(d
);
1197 if (kobject_add(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "bcache"))
1200 bcache_device_link(d
, c
, "volume");
1204 kobject_put(&d
->kobj
);
1208 static int flash_devs_run(struct cache_set
*c
)
1211 struct uuid_entry
*u
;
1214 u
< c
->uuids
+ c
->nr_uuids
&& !ret
;
1216 if (UUID_FLASH_ONLY(u
))
1217 ret
= flash_dev_run(c
, u
);
1222 int bch_flash_dev_create(struct cache_set
*c
, uint64_t size
)
1224 struct uuid_entry
*u
;
1226 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1229 u
= uuid_find_empty(c
);
1231 pr_err("Can't create volume, no room for UUID");
1235 get_random_bytes(u
->uuid
, 16);
1236 memset(u
->label
, 0, 32);
1237 u
->first_reg
= u
->last_reg
= cpu_to_le32(get_seconds());
1239 SET_UUID_FLASH_ONLY(u
, 1);
1240 u
->sectors
= size
>> 9;
1244 return flash_dev_run(c
, u
);
1250 bool bch_cache_set_error(struct cache_set
*c
, const char *fmt
, ...)
1254 if (c
->on_error
!= ON_ERROR_PANIC
&&
1255 test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1258 /* XXX: we can be called from atomic context
1259 acquire_console_sem();
1262 printk(KERN_ERR
"bcache: error on %pU: ", c
->sb
.set_uuid
);
1264 va_start(args
, fmt
);
1268 printk(", disabling caching\n");
1270 if (c
->on_error
== ON_ERROR_PANIC
)
1271 panic("panic forced after error\n");
1273 bch_cache_set_unregister(c
);
1277 void bch_cache_set_release(struct kobject
*kobj
)
1279 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
1281 module_put(THIS_MODULE
);
1284 static void cache_set_free(struct closure
*cl
)
1286 struct cache_set
*c
= container_of(cl
, struct cache_set
, cl
);
1290 if (!IS_ERR_OR_NULL(c
->debug
))
1291 debugfs_remove(c
->debug
);
1293 bch_open_buckets_free(c
);
1294 bch_btree_cache_free(c
);
1295 bch_journal_free(c
);
1297 for_each_cache(ca
, c
, i
)
1299 kobject_put(&ca
->kobj
);
1301 free_pages((unsigned long) c
->uuids
, ilog2(bucket_pages(c
)));
1302 free_pages((unsigned long) c
->sort
, ilog2(bucket_pages(c
)));
1305 bioset_free(c
->bio_split
);
1307 mempool_destroy(c
->fill_iter
);
1309 mempool_destroy(c
->bio_meta
);
1311 mempool_destroy(c
->search
);
1314 mutex_lock(&bch_register_lock
);
1316 mutex_unlock(&bch_register_lock
);
1318 pr_info("Cache set %pU unregistered", c
->sb
.set_uuid
);
1319 wake_up(&unregister_wait
);
1321 closure_debug_destroy(&c
->cl
);
1322 kobject_put(&c
->kobj
);
1325 static void cache_set_flush(struct closure
*cl
)
1327 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1332 bch_cache_accounting_destroy(&c
->accounting
);
1334 kobject_put(&c
->internal
);
1335 kobject_del(&c
->kobj
);
1338 kthread_stop(c
->gc_thread
);
1340 if (!IS_ERR_OR_NULL(c
->root
))
1341 list_add(&c
->root
->list
, &c
->btree_cache
);
1343 /* Should skip this if we're unregistering because of an error */
1344 list_for_each_entry(b
, &c
->btree_cache
, list
)
1345 if (btree_node_dirty(b
))
1346 bch_btree_node_write(b
, NULL
);
1348 for_each_cache(ca
, c
, i
)
1349 if (ca
->alloc_thread
)
1350 kthread_stop(ca
->alloc_thread
);
1355 static void __cache_set_unregister(struct closure
*cl
)
1357 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1358 struct cached_dev
*dc
;
1361 mutex_lock(&bch_register_lock
);
1363 for (i
= 0; i
< c
->nr_uuids
; i
++)
1364 if (c
->devices
[i
]) {
1365 if (!UUID_FLASH_ONLY(&c
->uuids
[i
]) &&
1366 test_bit(CACHE_SET_UNREGISTERING
, &c
->flags
)) {
1367 dc
= container_of(c
->devices
[i
],
1368 struct cached_dev
, disk
);
1369 bch_cached_dev_detach(dc
);
1371 bcache_device_stop(c
->devices
[i
]);
1375 mutex_unlock(&bch_register_lock
);
1377 continue_at(cl
, cache_set_flush
, system_wq
);
1380 void bch_cache_set_stop(struct cache_set
*c
)
1382 if (!test_and_set_bit(CACHE_SET_STOPPING
, &c
->flags
))
1383 closure_queue(&c
->caching
);
1386 void bch_cache_set_unregister(struct cache_set
*c
)
1388 set_bit(CACHE_SET_UNREGISTERING
, &c
->flags
);
1389 bch_cache_set_stop(c
);
1392 #define alloc_bucket_pages(gfp, c) \
1393 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1395 struct cache_set
*bch_cache_set_alloc(struct cache_sb
*sb
)
1398 struct cache_set
*c
= kzalloc(sizeof(struct cache_set
), GFP_KERNEL
);
1402 __module_get(THIS_MODULE
);
1403 closure_init(&c
->cl
, NULL
);
1404 set_closure_fn(&c
->cl
, cache_set_free
, system_wq
);
1406 closure_init(&c
->caching
, &c
->cl
);
1407 set_closure_fn(&c
->caching
, __cache_set_unregister
, system_wq
);
1409 /* Maybe create continue_at_noreturn() and use it here? */
1410 closure_set_stopped(&c
->cl
);
1411 closure_put(&c
->cl
);
1413 kobject_init(&c
->kobj
, &bch_cache_set_ktype
);
1414 kobject_init(&c
->internal
, &bch_cache_set_internal_ktype
);
1416 bch_cache_accounting_init(&c
->accounting
, &c
->cl
);
1418 memcpy(c
->sb
.set_uuid
, sb
->set_uuid
, 16);
1419 c
->sb
.block_size
= sb
->block_size
;
1420 c
->sb
.bucket_size
= sb
->bucket_size
;
1421 c
->sb
.nr_in_set
= sb
->nr_in_set
;
1422 c
->sb
.last_mount
= sb
->last_mount
;
1423 c
->bucket_bits
= ilog2(sb
->bucket_size
);
1424 c
->block_bits
= ilog2(sb
->block_size
);
1425 c
->nr_uuids
= bucket_bytes(c
) / sizeof(struct uuid_entry
);
1427 c
->btree_pages
= c
->sb
.bucket_size
/ PAGE_SECTORS
;
1428 if (c
->btree_pages
> BTREE_MAX_PAGES
)
1429 c
->btree_pages
= max_t(int, c
->btree_pages
/ 4,
1432 c
->sort_crit_factor
= int_sqrt(c
->btree_pages
);
1434 closure_init_unlocked(&c
->sb_write
);
1435 mutex_init(&c
->bucket_lock
);
1436 init_waitqueue_head(&c
->try_wait
);
1437 init_waitqueue_head(&c
->bucket_wait
);
1438 closure_init_unlocked(&c
->uuid_write
);
1439 spin_lock_init(&c
->sort_time_lock
);
1440 mutex_init(&c
->sort_lock
);
1441 spin_lock_init(&c
->btree_read_time_lock
);
1443 bch_moving_init_cache_set(c
);
1445 INIT_LIST_HEAD(&c
->list
);
1446 INIT_LIST_HEAD(&c
->cached_devs
);
1447 INIT_LIST_HEAD(&c
->btree_cache
);
1448 INIT_LIST_HEAD(&c
->btree_cache_freeable
);
1449 INIT_LIST_HEAD(&c
->btree_cache_freed
);
1450 INIT_LIST_HEAD(&c
->data_buckets
);
1452 c
->search
= mempool_create_slab_pool(32, bch_search_cache
);
1456 iter_size
= (sb
->bucket_size
/ sb
->block_size
+ 1) *
1457 sizeof(struct btree_iter_set
);
1459 if (!(c
->devices
= kzalloc(c
->nr_uuids
* sizeof(void *), GFP_KERNEL
)) ||
1460 !(c
->bio_meta
= mempool_create_kmalloc_pool(2,
1461 sizeof(struct bbio
) + sizeof(struct bio_vec
) *
1462 bucket_pages(c
))) ||
1463 !(c
->fill_iter
= mempool_create_kmalloc_pool(1, iter_size
)) ||
1464 !(c
->bio_split
= bioset_create(4, offsetof(struct bbio
, bio
))) ||
1465 !(c
->sort
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1466 !(c
->uuids
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1467 bch_journal_alloc(c
) ||
1468 bch_btree_cache_alloc(c
) ||
1469 bch_open_buckets_alloc(c
))
1472 c
->congested_read_threshold_us
= 2000;
1473 c
->congested_write_threshold_us
= 20000;
1474 c
->error_limit
= 8 << IO_ERROR_SHIFT
;
1478 bch_cache_set_unregister(c
);
1482 static void run_cache_set(struct cache_set
*c
)
1484 const char *err
= "cannot allocate memory";
1485 struct cached_dev
*dc
, *t
;
1490 closure_init_stack(&cl
);
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
))
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
, true);
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
, &cl
);
1540 err
= "error in recovery";
1541 if (bch_btree_check(c
))
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
);
1575 pr_notice("invalidating existing data");
1577 for_each_cache(ca
, c
, i
) {
1580 ca
->sb
.keys
= clamp_t(int, ca
->sb
.nbuckets
>> 7,
1581 2, SB_JOURNAL_BUCKETS
);
1583 for (j
= 0; j
< ca
->sb
.keys
; j
++)
1584 ca
->sb
.d
[j
] = ca
->sb
.first_bucket
+ j
;
1587 bch_btree_gc_finish(c
);
1589 err
= "error starting allocator thread";
1590 for_each_cache(ca
, c
, i
)
1591 if (bch_cache_allocator_start(ca
))
1594 mutex_lock(&c
->bucket_lock
);
1595 for_each_cache(ca
, c
, i
)
1597 mutex_unlock(&c
->bucket_lock
);
1599 err
= "cannot allocate new UUID bucket";
1600 if (__uuid_write(c
))
1603 err
= "cannot allocate new btree root";
1604 c
->root
= bch_btree_node_alloc(c
, 0);
1605 if (IS_ERR_OR_NULL(c
->root
))
1608 bkey_copy_key(&c
->root
->key
, &MAX_KEY
);
1609 bch_btree_node_write(c
->root
, &cl
);
1611 bch_btree_set_root(c
->root
);
1612 rw_unlock(true, c
->root
);
1615 * We don't want to write the first journal entry until
1616 * everything is set up - fortunately journal entries won't be
1617 * written until the SET_CACHE_SYNC() here:
1619 SET_CACHE_SYNC(&c
->sb
, true);
1621 bch_journal_next(&c
->journal
);
1622 bch_journal_meta(c
, &cl
);
1625 err
= "error starting gc thread";
1626 if (bch_gc_thread_start(c
))
1630 c
->sb
.last_mount
= get_seconds();
1631 bcache_write_super(c
);
1633 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
1634 bch_cached_dev_attach(dc
, c
);
1641 /* XXX: test this, it's broken */
1642 bch_cache_set_error(c
, err
);
1645 static bool can_attach_cache(struct cache
*ca
, struct cache_set
*c
)
1647 return ca
->sb
.block_size
== c
->sb
.block_size
&&
1648 ca
->sb
.bucket_size
== c
->sb
.block_size
&&
1649 ca
->sb
.nr_in_set
== c
->sb
.nr_in_set
;
1652 static const char *register_cache_set(struct cache
*ca
)
1655 const char *err
= "cannot allocate memory";
1656 struct cache_set
*c
;
1658 list_for_each_entry(c
, &bch_cache_sets
, list
)
1659 if (!memcmp(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16)) {
1660 if (c
->cache
[ca
->sb
.nr_this_dev
])
1661 return "duplicate cache set member";
1663 if (!can_attach_cache(ca
, c
))
1664 return "cache sb does not match set";
1666 if (!CACHE_SYNC(&ca
->sb
))
1667 SET_CACHE_SYNC(&c
->sb
, false);
1672 c
= bch_cache_set_alloc(&ca
->sb
);
1676 err
= "error creating kobject";
1677 if (kobject_add(&c
->kobj
, bcache_kobj
, "%pU", c
->sb
.set_uuid
) ||
1678 kobject_add(&c
->internal
, &c
->kobj
, "internal"))
1681 if (bch_cache_accounting_add_kobjs(&c
->accounting
, &c
->kobj
))
1684 bch_debug_init_cache_set(c
);
1686 list_add(&c
->list
, &bch_cache_sets
);
1688 sprintf(buf
, "cache%i", ca
->sb
.nr_this_dev
);
1689 if (sysfs_create_link(&ca
->kobj
, &c
->kobj
, "set") ||
1690 sysfs_create_link(&c
->kobj
, &ca
->kobj
, buf
))
1693 if (ca
->sb
.seq
> c
->sb
.seq
) {
1694 c
->sb
.version
= ca
->sb
.version
;
1695 memcpy(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16);
1696 c
->sb
.flags
= ca
->sb
.flags
;
1697 c
->sb
.seq
= ca
->sb
.seq
;
1698 pr_debug("set version = %llu", c
->sb
.version
);
1702 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = ca
;
1703 c
->cache_by_alloc
[c
->caches_loaded
++] = ca
;
1705 if (c
->caches_loaded
== c
->sb
.nr_in_set
)
1710 bch_cache_set_unregister(c
);
1716 void bch_cache_release(struct kobject
*kobj
)
1718 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
1721 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = NULL
;
1723 bio_split_pool_free(&ca
->bio_split_hook
);
1725 free_pages((unsigned long) ca
->disk_buckets
, ilog2(bucket_pages(ca
)));
1726 kfree(ca
->prio_buckets
);
1729 free_heap(&ca
->heap
);
1730 free_fifo(&ca
->unused
);
1731 free_fifo(&ca
->free_inc
);
1732 free_fifo(&ca
->free
);
1734 if (ca
->sb_bio
.bi_inline_vecs
[0].bv_page
)
1735 put_page(ca
->sb_bio
.bi_io_vec
[0].bv_page
);
1737 if (!IS_ERR_OR_NULL(ca
->bdev
)) {
1738 blk_sync_queue(bdev_get_queue(ca
->bdev
));
1739 blkdev_put(ca
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1743 module_put(THIS_MODULE
);
1746 static int cache_alloc(struct cache_sb
*sb
, struct cache
*ca
)
1751 __module_get(THIS_MODULE
);
1752 kobject_init(&ca
->kobj
, &bch_cache_ktype
);
1754 bio_init(&ca
->journal
.bio
);
1755 ca
->journal
.bio
.bi_max_vecs
= 8;
1756 ca
->journal
.bio
.bi_io_vec
= ca
->journal
.bio
.bi_inline_vecs
;
1758 free
= roundup_pow_of_two(ca
->sb
.nbuckets
) >> 9;
1759 free
= max_t(size_t, free
, (prio_buckets(ca
) + 8) * 2);
1761 if (!init_fifo(&ca
->free
, free
, GFP_KERNEL
) ||
1762 !init_fifo(&ca
->free_inc
, free
<< 2, GFP_KERNEL
) ||
1763 !init_fifo(&ca
->unused
, free
<< 2, GFP_KERNEL
) ||
1764 !init_heap(&ca
->heap
, free
<< 3, GFP_KERNEL
) ||
1765 !(ca
->buckets
= vzalloc(sizeof(struct bucket
) *
1766 ca
->sb
.nbuckets
)) ||
1767 !(ca
->prio_buckets
= kzalloc(sizeof(uint64_t) * prio_buckets(ca
) *
1769 !(ca
->disk_buckets
= alloc_bucket_pages(GFP_KERNEL
, ca
)) ||
1770 bio_split_pool_init(&ca
->bio_split_hook
))
1773 ca
->prio_last_buckets
= ca
->prio_buckets
+ prio_buckets(ca
);
1775 for_each_bucket(b
, ca
)
1776 atomic_set(&b
->pin
, 0);
1778 if (bch_cache_allocator_init(ca
))
1783 kobject_put(&ca
->kobj
);
1787 static void register_cache(struct cache_sb
*sb
, struct page
*sb_page
,
1788 struct block_device
*bdev
, struct cache
*ca
)
1790 char name
[BDEVNAME_SIZE
];
1791 const char *err
= "cannot allocate memory";
1793 memcpy(&ca
->sb
, sb
, sizeof(struct cache_sb
));
1795 ca
->bdev
->bd_holder
= ca
;
1797 bio_init(&ca
->sb_bio
);
1798 ca
->sb_bio
.bi_max_vecs
= 1;
1799 ca
->sb_bio
.bi_io_vec
= ca
->sb_bio
.bi_inline_vecs
;
1800 ca
->sb_bio
.bi_io_vec
[0].bv_page
= sb_page
;
1803 if (blk_queue_discard(bdev_get_queue(ca
->bdev
)))
1804 ca
->discard
= CACHE_DISCARD(&ca
->sb
);
1806 if (cache_alloc(sb
, ca
) != 0)
1809 err
= "error creating kobject";
1810 if (kobject_add(&ca
->kobj
, &part_to_dev(bdev
->bd_part
)->kobj
, "bcache"))
1813 err
= register_cache_set(ca
);
1817 pr_info("registered cache device %s", bdevname(bdev
, name
));
1820 pr_notice("error opening %s: %s", bdevname(bdev
, name
), err
);
1821 kobject_put(&ca
->kobj
);
1824 /* Global interfaces/init */
1826 static ssize_t
register_bcache(struct kobject
*, struct kobj_attribute
*,
1827 const char *, size_t);
1829 kobj_attribute_write(register, register_bcache
);
1830 kobj_attribute_write(register_quiet
, register_bcache
);
1832 static bool bch_is_open_backing(struct block_device
*bdev
) {
1833 struct cache_set
*c
, *tc
;
1834 struct cached_dev
*dc
, *t
;
1836 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1837 list_for_each_entry_safe(dc
, t
, &c
->cached_devs
, list
)
1838 if (dc
->bdev
== bdev
)
1840 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
1841 if (dc
->bdev
== bdev
)
1846 static bool bch_is_open_cache(struct block_device
*bdev
) {
1847 struct cache_set
*c
, *tc
;
1851 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1852 for_each_cache(ca
, c
, i
)
1853 if (ca
->bdev
== bdev
)
1858 static bool bch_is_open(struct block_device
*bdev
) {
1859 return bch_is_open_cache(bdev
) || bch_is_open_backing(bdev
);
1862 static ssize_t
register_bcache(struct kobject
*k
, struct kobj_attribute
*attr
,
1863 const char *buffer
, size_t size
)
1866 const char *err
= "cannot allocate memory";
1868 struct cache_sb
*sb
= NULL
;
1869 struct block_device
*bdev
= NULL
;
1870 struct page
*sb_page
= NULL
;
1872 if (!try_module_get(THIS_MODULE
))
1875 mutex_lock(&bch_register_lock
);
1877 if (!(path
= kstrndup(buffer
, size
, GFP_KERNEL
)) ||
1878 !(sb
= kmalloc(sizeof(struct cache_sb
), GFP_KERNEL
)))
1881 err
= "failed to open device";
1882 bdev
= blkdev_get_by_path(strim(path
),
1883 FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1886 if (bdev
== ERR_PTR(-EBUSY
)) {
1887 bdev
= lookup_bdev(strim(path
));
1888 if (!IS_ERR(bdev
) && bch_is_open(bdev
))
1889 err
= "device already registered";
1891 err
= "device busy";
1896 err
= "failed to set blocksize";
1897 if (set_blocksize(bdev
, 4096))
1900 err
= read_super(sb
, bdev
, &sb_page
);
1904 if (SB_IS_BDEV(sb
)) {
1905 struct cached_dev
*dc
= kzalloc(sizeof(*dc
), GFP_KERNEL
);
1909 register_bdev(sb
, sb_page
, bdev
, dc
);
1911 struct cache
*ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
1915 register_cache(sb
, sb_page
, bdev
, ca
);
1922 mutex_unlock(&bch_register_lock
);
1923 module_put(THIS_MODULE
);
1927 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1929 if (attr
!= &ksysfs_register_quiet
)
1930 pr_info("error opening %s: %s", path
, err
);
1935 static int bcache_reboot(struct notifier_block
*n
, unsigned long code
, void *x
)
1937 if (code
== SYS_DOWN
||
1939 code
== SYS_POWER_OFF
) {
1941 unsigned long start
= jiffies
;
1942 bool stopped
= false;
1944 struct cache_set
*c
, *tc
;
1945 struct cached_dev
*dc
, *tdc
;
1947 mutex_lock(&bch_register_lock
);
1949 if (list_empty(&bch_cache_sets
) &&
1950 list_empty(&uncached_devices
))
1953 pr_info("Stopping all devices:");
1955 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1956 bch_cache_set_stop(c
);
1958 list_for_each_entry_safe(dc
, tdc
, &uncached_devices
, list
)
1959 bcache_device_stop(&dc
->disk
);
1961 /* What's a condition variable? */
1963 long timeout
= start
+ 2 * HZ
- jiffies
;
1965 stopped
= list_empty(&bch_cache_sets
) &&
1966 list_empty(&uncached_devices
);
1968 if (timeout
< 0 || stopped
)
1971 prepare_to_wait(&unregister_wait
, &wait
,
1972 TASK_UNINTERRUPTIBLE
);
1974 mutex_unlock(&bch_register_lock
);
1975 schedule_timeout(timeout
);
1976 mutex_lock(&bch_register_lock
);
1979 finish_wait(&unregister_wait
, &wait
);
1982 pr_info("All devices stopped");
1984 pr_notice("Timeout waiting for devices to be closed");
1986 mutex_unlock(&bch_register_lock
);
1992 static struct notifier_block reboot
= {
1993 .notifier_call
= bcache_reboot
,
1994 .priority
= INT_MAX
, /* before any real devices */
1997 static void bcache_exit(void)
2003 kobject_put(bcache_kobj
);
2005 destroy_workqueue(bcache_wq
);
2006 unregister_blkdev(bcache_major
, "bcache");
2007 unregister_reboot_notifier(&reboot
);
2010 static int __init
bcache_init(void)
2012 static const struct attribute
*files
[] = {
2013 &ksysfs_register
.attr
,
2014 &ksysfs_register_quiet
.attr
,
2018 mutex_init(&bch_register_lock
);
2019 init_waitqueue_head(&unregister_wait
);
2020 register_reboot_notifier(&reboot
);
2021 closure_debug_init();
2023 bcache_major
= register_blkdev(0, "bcache");
2024 if (bcache_major
< 0)
2025 return bcache_major
;
2027 if (!(bcache_wq
= create_workqueue("bcache")) ||
2028 !(bcache_kobj
= kobject_create_and_add("bcache", fs_kobj
)) ||
2029 sysfs_create_files(bcache_kobj
, files
) ||
2031 bch_request_init() ||
2032 bch_debug_init(bcache_kobj
))
2041 module_exit(bcache_exit
);
2042 module_init(bcache_init
);