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.
14 #include <linux/buffer_head.h>
15 #include <linux/debugfs.h>
16 #include <linux/genhd.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/reboot.h>
20 #include <linux/sysfs.h>
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
25 static const char bcache_magic
[] = {
26 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
27 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
30 static const char invalid_uuid
[] = {
31 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
32 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
35 /* Default is -1; we skip past it for struct cached_dev's cache mode */
36 const char * const bch_cache_modes
[] = {
45 struct uuid_entry_v0
{
54 static struct kobject
*bcache_kobj
;
55 struct mutex bch_register_lock
;
56 LIST_HEAD(bch_cache_sets
);
57 static LIST_HEAD(uncached_devices
);
59 static int bcache_major
, bcache_minor
;
60 static wait_queue_head_t unregister_wait
;
61 struct workqueue_struct
*bcache_wq
;
63 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
65 static void bio_split_pool_free(struct bio_split_pool
*p
)
67 if (p
->bio_split_hook
)
68 mempool_destroy(p
->bio_split_hook
);
71 bioset_free(p
->bio_split
);
74 static int bio_split_pool_init(struct bio_split_pool
*p
)
76 p
->bio_split
= bioset_create(4, 0);
80 p
->bio_split_hook
= mempool_create_kmalloc_pool(4,
81 sizeof(struct bio_split_hook
));
82 if (!p
->bio_split_hook
)
90 static const char *read_super(struct cache_sb
*sb
, struct block_device
*bdev
,
95 struct buffer_head
*bh
= __bread(bdev
, 1, SB_SIZE
);
101 s
= (struct cache_sb
*) bh
->b_data
;
103 sb
->offset
= le64_to_cpu(s
->offset
);
104 sb
->version
= le64_to_cpu(s
->version
);
106 memcpy(sb
->magic
, s
->magic
, 16);
107 memcpy(sb
->uuid
, s
->uuid
, 16);
108 memcpy(sb
->set_uuid
, s
->set_uuid
, 16);
109 memcpy(sb
->label
, s
->label
, SB_LABEL_SIZE
);
111 sb
->flags
= le64_to_cpu(s
->flags
);
112 sb
->seq
= le64_to_cpu(s
->seq
);
113 sb
->last_mount
= le32_to_cpu(s
->last_mount
);
114 sb
->first_bucket
= le16_to_cpu(s
->first_bucket
);
115 sb
->keys
= le16_to_cpu(s
->keys
);
117 for (i
= 0; i
< SB_JOURNAL_BUCKETS
; i
++)
118 sb
->d
[i
] = le64_to_cpu(s
->d
[i
]);
120 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
121 sb
->version
, sb
->flags
, sb
->seq
, sb
->keys
);
123 err
= "Not a bcache superblock";
124 if (sb
->offset
!= SB_SECTOR
)
127 if (memcmp(sb
->magic
, bcache_magic
, 16))
130 err
= "Too many journal buckets";
131 if (sb
->keys
> SB_JOURNAL_BUCKETS
)
134 err
= "Bad checksum";
135 if (s
->csum
!= csum_set(s
))
139 if (bch_is_zero(sb
->uuid
, 16))
142 switch (sb
->version
) {
143 case BCACHE_SB_VERSION_BDEV
:
144 sb
->block_size
= le16_to_cpu(s
->block_size
);
145 sb
->data_offset
= BDEV_DATA_START_DEFAULT
;
147 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET
:
148 sb
->block_size
= le16_to_cpu(s
->block_size
);
149 sb
->data_offset
= le64_to_cpu(s
->data_offset
);
151 err
= "Bad data offset";
152 if (sb
->data_offset
< BDEV_DATA_START_DEFAULT
)
156 case BCACHE_SB_VERSION_CDEV
:
157 case BCACHE_SB_VERSION_CDEV_WITH_UUID
:
158 sb
->nbuckets
= le64_to_cpu(s
->nbuckets
);
159 sb
->block_size
= le16_to_cpu(s
->block_size
);
160 sb
->bucket_size
= le16_to_cpu(s
->bucket_size
);
162 sb
->nr_in_set
= le16_to_cpu(s
->nr_in_set
);
163 sb
->nr_this_dev
= le16_to_cpu(s
->nr_this_dev
);
165 err
= "Too many buckets";
166 if (sb
->nbuckets
> LONG_MAX
)
169 err
= "Not enough buckets";
170 if (sb
->nbuckets
< 1 << 7)
173 err
= "Bad block/bucket size";
174 if (!is_power_of_2(sb
->block_size
) ||
175 sb
->block_size
> PAGE_SECTORS
||
176 !is_power_of_2(sb
->bucket_size
) ||
177 sb
->bucket_size
< PAGE_SECTORS
)
180 err
= "Invalid superblock: device too small";
181 if (get_capacity(bdev
->bd_disk
) < sb
->bucket_size
* sb
->nbuckets
)
185 if (bch_is_zero(sb
->set_uuid
, 16))
188 err
= "Bad cache device number in set";
189 if (!sb
->nr_in_set
||
190 sb
->nr_in_set
<= sb
->nr_this_dev
||
191 sb
->nr_in_set
> MAX_CACHES_PER_SET
)
194 err
= "Journal buckets not sequential";
195 for (i
= 0; i
< sb
->keys
; i
++)
196 if (sb
->d
[i
] != sb
->first_bucket
+ i
)
199 err
= "Too many journal buckets";
200 if (sb
->first_bucket
+ sb
->keys
> sb
->nbuckets
)
203 err
= "Invalid superblock: first bucket comes before end of super";
204 if (sb
->first_bucket
* sb
->bucket_size
< 16)
209 err
= "Unsupported superblock version";
213 sb
->last_mount
= get_seconds();
216 get_page(bh
->b_page
);
223 static void write_bdev_super_endio(struct bio
*bio
, int error
)
225 struct cached_dev
*dc
= bio
->bi_private
;
226 /* XXX: error checking */
228 closure_put(&dc
->sb_write
.cl
);
231 static void __write_super(struct cache_sb
*sb
, struct bio
*bio
)
233 struct cache_sb
*out
= page_address(bio
->bi_io_vec
[0].bv_page
);
236 bio
->bi_sector
= SB_SECTOR
;
237 bio
->bi_rw
= REQ_SYNC
|REQ_META
;
238 bio
->bi_size
= SB_SIZE
;
239 bch_bio_map(bio
, NULL
);
241 out
->offset
= cpu_to_le64(sb
->offset
);
242 out
->version
= cpu_to_le64(sb
->version
);
244 memcpy(out
->uuid
, sb
->uuid
, 16);
245 memcpy(out
->set_uuid
, sb
->set_uuid
, 16);
246 memcpy(out
->label
, sb
->label
, SB_LABEL_SIZE
);
248 out
->flags
= cpu_to_le64(sb
->flags
);
249 out
->seq
= cpu_to_le64(sb
->seq
);
251 out
->last_mount
= cpu_to_le32(sb
->last_mount
);
252 out
->first_bucket
= cpu_to_le16(sb
->first_bucket
);
253 out
->keys
= cpu_to_le16(sb
->keys
);
255 for (i
= 0; i
< sb
->keys
; i
++)
256 out
->d
[i
] = cpu_to_le64(sb
->d
[i
]);
258 out
->csum
= csum_set(out
);
260 pr_debug("ver %llu, flags %llu, seq %llu",
261 sb
->version
, sb
->flags
, sb
->seq
);
263 submit_bio(REQ_WRITE
, bio
);
266 void bch_write_bdev_super(struct cached_dev
*dc
, struct closure
*parent
)
268 struct closure
*cl
= &dc
->sb_write
.cl
;
269 struct bio
*bio
= &dc
->sb_bio
;
271 closure_lock(&dc
->sb_write
, parent
);
274 bio
->bi_bdev
= dc
->bdev
;
275 bio
->bi_end_io
= write_bdev_super_endio
;
276 bio
->bi_private
= dc
;
279 __write_super(&dc
->sb
, bio
);
284 static void write_super_endio(struct bio
*bio
, int error
)
286 struct cache
*ca
= bio
->bi_private
;
288 bch_count_io_errors(ca
, error
, "writing superblock");
289 closure_put(&ca
->set
->sb_write
.cl
);
292 void bcache_write_super(struct cache_set
*c
)
294 struct closure
*cl
= &c
->sb_write
.cl
;
298 closure_lock(&c
->sb_write
, &c
->cl
);
302 for_each_cache(ca
, c
, i
) {
303 struct bio
*bio
= &ca
->sb_bio
;
305 ca
->sb
.version
= BCACHE_SB_VERSION_CDEV_WITH_UUID
;
306 ca
->sb
.seq
= c
->sb
.seq
;
307 ca
->sb
.last_mount
= c
->sb
.last_mount
;
309 SET_CACHE_SYNC(&ca
->sb
, CACHE_SYNC(&c
->sb
));
312 bio
->bi_bdev
= ca
->bdev
;
313 bio
->bi_end_io
= write_super_endio
;
314 bio
->bi_private
= ca
;
317 __write_super(&ca
->sb
, bio
);
325 static void uuid_endio(struct bio
*bio
, int error
)
327 struct closure
*cl
= bio
->bi_private
;
328 struct cache_set
*c
= container_of(cl
, struct cache_set
, uuid_write
.cl
);
330 cache_set_err_on(error
, c
, "accessing uuids");
331 bch_bbio_free(bio
, c
);
335 static void uuid_io(struct cache_set
*c
, unsigned long rw
,
336 struct bkey
*k
, struct closure
*parent
)
338 struct closure
*cl
= &c
->uuid_write
.cl
;
339 struct uuid_entry
*u
;
343 closure_lock(&c
->uuid_write
, parent
);
345 for (i
= 0; i
< KEY_PTRS(k
); i
++) {
346 struct bio
*bio
= bch_bbio_alloc(c
);
348 bio
->bi_rw
= REQ_SYNC
|REQ_META
|rw
;
349 bio
->bi_size
= KEY_SIZE(k
) << 9;
351 bio
->bi_end_io
= uuid_endio
;
352 bio
->bi_private
= cl
;
353 bch_bio_map(bio
, c
->uuids
);
355 bch_submit_bbio(bio
, c
, k
, i
);
361 pr_debug("%s UUIDs at %s", rw
& REQ_WRITE
? "wrote" : "read",
362 pkey(&c
->uuid_bucket
));
364 for (u
= c
->uuids
; u
< c
->uuids
+ c
->nr_uuids
; u
++)
365 if (!bch_is_zero(u
->uuid
, 16))
366 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
367 u
- c
->uuids
, u
->uuid
, u
->label
,
368 u
->first_reg
, u
->last_reg
, u
->invalidated
);
373 static char *uuid_read(struct cache_set
*c
, struct jset
*j
, struct closure
*cl
)
375 struct bkey
*k
= &j
->uuid_bucket
;
377 if (__bch_ptr_invalid(c
, 1, k
))
378 return "bad uuid pointer";
380 bkey_copy(&c
->uuid_bucket
, k
);
381 uuid_io(c
, READ_SYNC
, k
, cl
);
383 if (j
->version
< BCACHE_JSET_VERSION_UUIDv1
) {
384 struct uuid_entry_v0
*u0
= (void *) c
->uuids
;
385 struct uuid_entry
*u1
= (void *) c
->uuids
;
391 * Since the new uuid entry is bigger than the old, we have to
392 * convert starting at the highest memory address and work down
393 * in order to do it in place
396 for (i
= c
->nr_uuids
- 1;
399 memcpy(u1
[i
].uuid
, u0
[i
].uuid
, 16);
400 memcpy(u1
[i
].label
, u0
[i
].label
, 32);
402 u1
[i
].first_reg
= u0
[i
].first_reg
;
403 u1
[i
].last_reg
= u0
[i
].last_reg
;
404 u1
[i
].invalidated
= u0
[i
].invalidated
;
414 static int __uuid_write(struct cache_set
*c
)
418 closure_init_stack(&cl
);
420 lockdep_assert_held(&bch_register_lock
);
422 if (bch_bucket_alloc_set(c
, WATERMARK_METADATA
, &k
.key
, 1, &cl
))
425 SET_KEY_SIZE(&k
.key
, c
->sb
.bucket_size
);
426 uuid_io(c
, REQ_WRITE
, &k
.key
, &cl
);
429 bkey_copy(&c
->uuid_bucket
, &k
.key
);
430 __bkey_put(c
, &k
.key
);
434 int bch_uuid_write(struct cache_set
*c
)
436 int ret
= __uuid_write(c
);
439 bch_journal_meta(c
, NULL
);
444 static struct uuid_entry
*uuid_find(struct cache_set
*c
, const char *uuid
)
446 struct uuid_entry
*u
;
449 u
< c
->uuids
+ c
->nr_uuids
; u
++)
450 if (!memcmp(u
->uuid
, uuid
, 16))
456 static struct uuid_entry
*uuid_find_empty(struct cache_set
*c
)
458 static const char zero_uuid
[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
459 return uuid_find(c
, zero_uuid
);
463 * Bucket priorities/gens:
465 * For each bucket, we store on disk its
469 * See alloc.c for an explanation of the gen. The priority is used to implement
470 * lru (and in the future other) cache replacement policies; for most purposes
471 * it's just an opaque integer.
473 * The gens and the priorities don't have a whole lot to do with each other, and
474 * it's actually the gens that must be written out at specific times - it's no
475 * big deal if the priorities don't get written, if we lose them we just reuse
476 * buckets in suboptimal order.
478 * On disk they're stored in a packed array, and in as many buckets are required
479 * to fit them all. The buckets we use to store them form a list; the journal
480 * header points to the first bucket, the first bucket points to the second
483 * This code is used by the allocation code; periodically (whenever it runs out
484 * of buckets to allocate from) the allocation code will invalidate some
485 * buckets, but it can't use those buckets until their new gens are safely on
489 static void prio_endio(struct bio
*bio
, int error
)
491 struct cache
*ca
= bio
->bi_private
;
493 cache_set_err_on(error
, ca
->set
, "accessing priorities");
494 bch_bbio_free(bio
, ca
->set
);
495 closure_put(&ca
->prio
);
498 static void prio_io(struct cache
*ca
, uint64_t bucket
, unsigned long rw
)
500 struct closure
*cl
= &ca
->prio
;
501 struct bio
*bio
= bch_bbio_alloc(ca
->set
);
503 closure_init_stack(cl
);
505 bio
->bi_sector
= bucket
* ca
->sb
.bucket_size
;
506 bio
->bi_bdev
= ca
->bdev
;
507 bio
->bi_rw
= REQ_SYNC
|REQ_META
|rw
;
508 bio
->bi_size
= bucket_bytes(ca
);
510 bio
->bi_end_io
= prio_endio
;
511 bio
->bi_private
= ca
;
512 bch_bio_map(bio
, ca
->disk_buckets
);
514 closure_bio_submit(bio
, &ca
->prio
, ca
);
518 #define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
519 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
521 void bch_prio_write(struct cache
*ca
)
527 closure_init_stack(&cl
);
529 lockdep_assert_held(&ca
->set
->bucket_lock
);
531 for (b
= ca
->buckets
;
532 b
< ca
->buckets
+ ca
->sb
.nbuckets
; b
++)
533 b
->disk_gen
= b
->gen
;
535 ca
->disk_buckets
->seq
++;
537 atomic_long_add(ca
->sb
.bucket_size
* prio_buckets(ca
),
538 &ca
->meta_sectors_written
);
540 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca
->free
),
541 fifo_used(&ca
->free_inc
), fifo_used(&ca
->unused
));
542 blktrace_msg(ca
, "Starting priorities: " buckets_free(ca
));
544 for (i
= prio_buckets(ca
) - 1; i
>= 0; --i
) {
546 struct prio_set
*p
= ca
->disk_buckets
;
547 struct bucket_disk
*d
= p
->data
;
548 struct bucket_disk
*end
= d
+ prios_per_bucket(ca
);
550 for (b
= ca
->buckets
+ i
* prios_per_bucket(ca
);
551 b
< ca
->buckets
+ ca
->sb
.nbuckets
&& d
< end
;
553 d
->prio
= cpu_to_le16(b
->prio
);
557 p
->next_bucket
= ca
->prio_buckets
[i
+ 1];
558 p
->magic
= pset_magic(ca
);
559 p
->csum
= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8);
561 bucket
= bch_bucket_alloc(ca
, WATERMARK_PRIO
, &cl
);
562 BUG_ON(bucket
== -1);
564 mutex_unlock(&ca
->set
->bucket_lock
);
565 prio_io(ca
, bucket
, REQ_WRITE
);
566 mutex_lock(&ca
->set
->bucket_lock
);
568 ca
->prio_buckets
[i
] = bucket
;
569 atomic_dec_bug(&ca
->buckets
[bucket
].pin
);
572 mutex_unlock(&ca
->set
->bucket_lock
);
574 bch_journal_meta(ca
->set
, &cl
);
577 mutex_lock(&ca
->set
->bucket_lock
);
579 ca
->need_save_prio
= 0;
582 * Don't want the old priorities to get garbage collected until after we
583 * finish writing the new ones, and they're journalled
585 for (i
= 0; i
< prio_buckets(ca
); i
++)
586 ca
->prio_last_buckets
[i
] = ca
->prio_buckets
[i
];
589 static void prio_read(struct cache
*ca
, uint64_t bucket
)
591 struct prio_set
*p
= ca
->disk_buckets
;
592 struct bucket_disk
*d
= p
->data
+ prios_per_bucket(ca
), *end
= d
;
594 unsigned bucket_nr
= 0;
596 for (b
= ca
->buckets
;
597 b
< ca
->buckets
+ ca
->sb
.nbuckets
;
600 ca
->prio_buckets
[bucket_nr
] = bucket
;
601 ca
->prio_last_buckets
[bucket_nr
] = bucket
;
604 prio_io(ca
, bucket
, READ_SYNC
);
606 if (p
->csum
!= bch_crc64(&p
->magic
, bucket_bytes(ca
) - 8))
607 pr_warn("bad csum reading priorities");
609 if (p
->magic
!= pset_magic(ca
))
610 pr_warn("bad magic reading priorities");
612 bucket
= p
->next_bucket
;
616 b
->prio
= le16_to_cpu(d
->prio
);
617 b
->gen
= b
->disk_gen
= b
->last_gc
= b
->gc_gen
= d
->gen
;
623 static int open_dev(struct block_device
*b
, fmode_t mode
)
625 struct bcache_device
*d
= b
->bd_disk
->private_data
;
626 if (atomic_read(&d
->closing
))
633 static int release_dev(struct gendisk
*b
, fmode_t mode
)
635 struct bcache_device
*d
= b
->private_data
;
640 static int ioctl_dev(struct block_device
*b
, fmode_t mode
,
641 unsigned int cmd
, unsigned long arg
)
643 struct bcache_device
*d
= b
->bd_disk
->private_data
;
644 return d
->ioctl(d
, mode
, cmd
, arg
);
647 static const struct block_device_operations bcache_ops
= {
649 .release
= release_dev
,
651 .owner
= THIS_MODULE
,
654 void bcache_device_stop(struct bcache_device
*d
)
656 if (!atomic_xchg(&d
->closing
, 1))
657 closure_queue(&d
->cl
);
660 static void bcache_device_detach(struct bcache_device
*d
)
662 lockdep_assert_held(&bch_register_lock
);
664 if (atomic_read(&d
->detaching
)) {
665 struct uuid_entry
*u
= d
->c
->uuids
+ d
->id
;
667 SET_UUID_FLASH_ONLY(u
, 0);
668 memcpy(u
->uuid
, invalid_uuid
, 16);
669 u
->invalidated
= cpu_to_le32(get_seconds());
670 bch_uuid_write(d
->c
);
672 atomic_set(&d
->detaching
, 0);
675 d
->c
->devices
[d
->id
] = NULL
;
676 closure_put(&d
->c
->caching
);
680 static void bcache_device_attach(struct bcache_device
*d
, struct cache_set
*c
,
683 BUG_ON(test_bit(CACHE_SET_STOPPING
, &c
->flags
));
689 closure_get(&c
->caching
);
692 static void bcache_device_link(struct bcache_device
*d
, struct cache_set
*c
,
695 snprintf(d
->name
, BCACHEDEVNAME_SIZE
,
696 "%s%u", name
, d
->id
);
698 WARN(sysfs_create_link(&d
->kobj
, &c
->kobj
, "cache") ||
699 sysfs_create_link(&c
->kobj
, &d
->kobj
, d
->name
),
700 "Couldn't create device <-> cache set symlinks");
703 static void bcache_device_free(struct bcache_device
*d
)
705 lockdep_assert_held(&bch_register_lock
);
707 pr_info("%s stopped", d
->disk
->disk_name
);
710 bcache_device_detach(d
);
713 del_gendisk(d
->disk
);
714 if (d
->disk
&& d
->disk
->queue
)
715 blk_cleanup_queue(d
->disk
->queue
);
719 bio_split_pool_free(&d
->bio_split_hook
);
720 if (d
->unaligned_bvec
)
721 mempool_destroy(d
->unaligned_bvec
);
723 bioset_free(d
->bio_split
);
725 closure_debug_destroy(&d
->cl
);
728 static int bcache_device_init(struct bcache_device
*d
, unsigned block_size
)
730 struct request_queue
*q
;
732 if (!(d
->bio_split
= bioset_create(4, offsetof(struct bbio
, bio
))) ||
733 !(d
->unaligned_bvec
= mempool_create_kmalloc_pool(1,
734 sizeof(struct bio_vec
) * BIO_MAX_PAGES
)) ||
735 bio_split_pool_init(&d
->bio_split_hook
))
739 d
->disk
= alloc_disk(1);
743 snprintf(d
->disk
->disk_name
, DISK_NAME_LEN
, "bcache%i", bcache_minor
);
745 d
->disk
->major
= bcache_major
;
746 d
->disk
->first_minor
= bcache_minor
++;
747 d
->disk
->fops
= &bcache_ops
;
748 d
->disk
->private_data
= d
;
750 q
= blk_alloc_queue(GFP_KERNEL
);
754 blk_queue_make_request(q
, NULL
);
757 q
->backing_dev_info
.congested_data
= d
;
758 q
->limits
.max_hw_sectors
= UINT_MAX
;
759 q
->limits
.max_sectors
= UINT_MAX
;
760 q
->limits
.max_segment_size
= UINT_MAX
;
761 q
->limits
.max_segments
= BIO_MAX_PAGES
;
762 q
->limits
.max_discard_sectors
= UINT_MAX
;
763 q
->limits
.io_min
= block_size
;
764 q
->limits
.logical_block_size
= block_size
;
765 q
->limits
.physical_block_size
= block_size
;
766 set_bit(QUEUE_FLAG_NONROT
, &d
->disk
->queue
->queue_flags
);
767 set_bit(QUEUE_FLAG_DISCARD
, &d
->disk
->queue
->queue_flags
);
774 static void calc_cached_dev_sectors(struct cache_set
*c
)
776 uint64_t sectors
= 0;
777 struct cached_dev
*dc
;
779 list_for_each_entry(dc
, &c
->cached_devs
, list
)
780 sectors
+= bdev_sectors(dc
->bdev
);
782 c
->cached_dev_sectors
= sectors
;
785 void bch_cached_dev_run(struct cached_dev
*dc
)
787 struct bcache_device
*d
= &dc
->disk
;
789 if (atomic_xchg(&dc
->running
, 1))
793 BDEV_STATE(&dc
->sb
) != BDEV_STATE_NONE
) {
795 closure_init_stack(&cl
);
797 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_STALE
);
798 bch_write_bdev_super(dc
, &cl
);
804 char *env
[] = { "SYMLINK=label" , NULL
};
805 kobject_uevent_env(&disk_to_dev(d
->disk
)->kobj
, KOBJ_CHANGE
, env
);
807 if (sysfs_create_link(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "dev") ||
808 sysfs_create_link(&disk_to_dev(d
->disk
)->kobj
, &d
->kobj
, "bcache"))
809 pr_debug("error creating sysfs link");
812 static void cached_dev_detach_finish(struct work_struct
*w
)
814 struct cached_dev
*dc
= container_of(w
, struct cached_dev
, detach
);
815 char buf
[BDEVNAME_SIZE
];
817 closure_init_stack(&cl
);
819 BUG_ON(!atomic_read(&dc
->disk
.detaching
));
820 BUG_ON(atomic_read(&dc
->count
));
822 sysfs_remove_link(&dc
->disk
.c
->kobj
, dc
->disk
.name
);
823 sysfs_remove_link(&dc
->disk
.kobj
, "cache");
825 mutex_lock(&bch_register_lock
);
827 memset(&dc
->sb
.set_uuid
, 0, 16);
828 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_NONE
);
830 bch_write_bdev_super(dc
, &cl
);
833 bcache_device_detach(&dc
->disk
);
834 list_move(&dc
->list
, &uncached_devices
);
836 mutex_unlock(&bch_register_lock
);
838 pr_info("Caching disabled for %s", bdevname(dc
->bdev
, buf
));
840 /* Drop ref we took in cached_dev_detach() */
841 closure_put(&dc
->disk
.cl
);
844 void bch_cached_dev_detach(struct cached_dev
*dc
)
846 lockdep_assert_held(&bch_register_lock
);
848 if (atomic_read(&dc
->disk
.closing
))
851 if (atomic_xchg(&dc
->disk
.detaching
, 1))
855 * Block the device from being closed and freed until we're finished
858 closure_get(&dc
->disk
.cl
);
860 bch_writeback_queue(dc
);
864 int bch_cached_dev_attach(struct cached_dev
*dc
, struct cache_set
*c
)
866 uint32_t rtime
= cpu_to_le32(get_seconds());
867 struct uuid_entry
*u
;
868 char buf
[BDEVNAME_SIZE
];
870 bdevname(dc
->bdev
, buf
);
872 if (memcmp(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16))
876 pr_err("Can't attach %s: already attached", buf
);
880 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
)) {
881 pr_err("Can't attach %s: shutting down", buf
);
885 if (dc
->sb
.block_size
< c
->sb
.block_size
) {
887 pr_err("Couldn't attach %s: block size less than set's block size",
892 u
= uuid_find(c
, dc
->sb
.uuid
);
895 (BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
||
896 BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
)) {
897 memcpy(u
->uuid
, invalid_uuid
, 16);
898 u
->invalidated
= cpu_to_le32(get_seconds());
903 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
904 pr_err("Couldn't find uuid for %s in set", buf
);
908 u
= uuid_find_empty(c
);
910 pr_err("Not caching %s, no room for UUID", buf
);
915 /* Deadlocks since we're called via sysfs...
916 sysfs_remove_file(&dc->kobj, &sysfs_attach);
919 if (bch_is_zero(u
->uuid
, 16)) {
921 closure_init_stack(&cl
);
923 memcpy(u
->uuid
, dc
->sb
.uuid
, 16);
924 memcpy(u
->label
, dc
->sb
.label
, SB_LABEL_SIZE
);
925 u
->first_reg
= u
->last_reg
= rtime
;
928 memcpy(dc
->sb
.set_uuid
, c
->sb
.set_uuid
, 16);
929 SET_BDEV_STATE(&dc
->sb
, BDEV_STATE_CLEAN
);
931 bch_write_bdev_super(dc
, &cl
);
938 bcache_device_attach(&dc
->disk
, c
, u
- c
->uuids
);
939 bcache_device_link(&dc
->disk
, c
, "bdev");
940 list_move(&dc
->list
, &c
->cached_devs
);
941 calc_cached_dev_sectors(c
);
945 * dc->c must be set before dc->count != 0 - paired with the mb in
948 atomic_set(&dc
->count
, 1);
950 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_DIRTY
) {
951 atomic_set(&dc
->has_dirty
, 1);
952 atomic_inc(&dc
->count
);
953 bch_writeback_queue(dc
);
956 bch_cached_dev_run(dc
);
958 pr_info("Caching %s as %s on set %pU",
959 bdevname(dc
->bdev
, buf
), dc
->disk
.disk
->disk_name
,
960 dc
->disk
.c
->sb
.set_uuid
);
964 void bch_cached_dev_release(struct kobject
*kobj
)
966 struct cached_dev
*dc
= container_of(kobj
, struct cached_dev
,
969 module_put(THIS_MODULE
);
972 static void cached_dev_free(struct closure
*cl
)
974 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
976 cancel_delayed_work_sync(&dc
->writeback_rate_update
);
978 mutex_lock(&bch_register_lock
);
980 bcache_device_free(&dc
->disk
);
983 mutex_unlock(&bch_register_lock
);
985 if (!IS_ERR_OR_NULL(dc
->bdev
)) {
986 blk_sync_queue(bdev_get_queue(dc
->bdev
));
987 blkdev_put(dc
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
990 wake_up(&unregister_wait
);
992 kobject_put(&dc
->disk
.kobj
);
995 static void cached_dev_flush(struct closure
*cl
)
997 struct cached_dev
*dc
= container_of(cl
, struct cached_dev
, disk
.cl
);
998 struct bcache_device
*d
= &dc
->disk
;
1000 bch_cache_accounting_destroy(&dc
->accounting
);
1001 kobject_del(&d
->kobj
);
1003 continue_at(cl
, cached_dev_free
, system_wq
);
1006 static int cached_dev_init(struct cached_dev
*dc
, unsigned block_size
)
1011 closure_init(&dc
->disk
.cl
, NULL
);
1012 set_closure_fn(&dc
->disk
.cl
, cached_dev_flush
, system_wq
);
1014 __module_get(THIS_MODULE
);
1015 INIT_LIST_HEAD(&dc
->list
);
1016 kobject_init(&dc
->disk
.kobj
, &bch_cached_dev_ktype
);
1018 bch_cache_accounting_init(&dc
->accounting
, &dc
->disk
.cl
);
1020 err
= bcache_device_init(&dc
->disk
, block_size
);
1024 spin_lock_init(&dc
->io_lock
);
1025 closure_init_unlocked(&dc
->sb_write
);
1026 INIT_WORK(&dc
->detach
, cached_dev_detach_finish
);
1028 dc
->sequential_merge
= true;
1029 dc
->sequential_cutoff
= 4 << 20;
1031 INIT_LIST_HEAD(&dc
->io_lru
);
1032 dc
->sb_bio
.bi_max_vecs
= 1;
1033 dc
->sb_bio
.bi_io_vec
= dc
->sb_bio
.bi_inline_vecs
;
1035 for (io
= dc
->io
; io
< dc
->io
+ RECENT_IO
; io
++) {
1036 list_add(&io
->lru
, &dc
->io_lru
);
1037 hlist_add_head(&io
->hash
, dc
->io_hash
+ RECENT_IO
);
1040 bch_writeback_init_cached_dev(dc
);
1043 bcache_device_stop(&dc
->disk
);
1047 /* Cached device - bcache superblock */
1049 static const char *register_bdev(struct cache_sb
*sb
, struct page
*sb_page
,
1050 struct block_device
*bdev
,
1051 struct cached_dev
*dc
)
1053 char name
[BDEVNAME_SIZE
];
1054 const char *err
= "cannot allocate memory";
1056 struct cache_set
*c
;
1058 if (!dc
|| cached_dev_init(dc
, sb
->block_size
<< 9) != 0)
1061 memcpy(&dc
->sb
, sb
, sizeof(struct cache_sb
));
1062 dc
->sb_bio
.bi_io_vec
[0].bv_page
= sb_page
;
1064 dc
->bdev
->bd_holder
= dc
;
1068 set_capacity(g
, dc
->bdev
->bd_part
->nr_sects
- dc
->sb
.data_offset
);
1070 g
->queue
->backing_dev_info
.ra_pages
=
1071 max(g
->queue
->backing_dev_info
.ra_pages
,
1072 bdev
->bd_queue
->backing_dev_info
.ra_pages
);
1074 bch_cached_dev_request_init(dc
);
1076 err
= "error creating kobject";
1077 if (kobject_add(&dc
->disk
.kobj
, &part_to_dev(bdev
->bd_part
)->kobj
,
1080 if (bch_cache_accounting_add_kobjs(&dc
->accounting
, &dc
->disk
.kobj
))
1083 list_add(&dc
->list
, &uncached_devices
);
1084 list_for_each_entry(c
, &bch_cache_sets
, list
)
1085 bch_cached_dev_attach(dc
, c
);
1087 if (BDEV_STATE(&dc
->sb
) == BDEV_STATE_NONE
||
1088 BDEV_STATE(&dc
->sb
) == BDEV_STATE_STALE
)
1089 bch_cached_dev_run(dc
);
1093 kobject_put(&dc
->disk
.kobj
);
1094 pr_notice("error opening %s: %s", bdevname(bdev
, name
), err
);
1096 * Return NULL instead of an error because kobject_put() cleans
1102 /* Flash only volumes */
1104 void bch_flash_dev_release(struct kobject
*kobj
)
1106 struct bcache_device
*d
= container_of(kobj
, struct bcache_device
,
1111 static void flash_dev_free(struct closure
*cl
)
1113 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1114 bcache_device_free(d
);
1115 kobject_put(&d
->kobj
);
1118 static void flash_dev_flush(struct closure
*cl
)
1120 struct bcache_device
*d
= container_of(cl
, struct bcache_device
, cl
);
1122 sysfs_remove_link(&d
->c
->kobj
, d
->name
);
1123 sysfs_remove_link(&d
->kobj
, "cache");
1124 kobject_del(&d
->kobj
);
1125 continue_at(cl
, flash_dev_free
, system_wq
);
1128 static int flash_dev_run(struct cache_set
*c
, struct uuid_entry
*u
)
1130 struct bcache_device
*d
= kzalloc(sizeof(struct bcache_device
),
1135 closure_init(&d
->cl
, NULL
);
1136 set_closure_fn(&d
->cl
, flash_dev_flush
, system_wq
);
1138 kobject_init(&d
->kobj
, &bch_flash_dev_ktype
);
1140 if (bcache_device_init(d
, block_bytes(c
)))
1143 bcache_device_attach(d
, c
, u
- c
->uuids
);
1144 set_capacity(d
->disk
, u
->sectors
);
1145 bch_flash_dev_request_init(d
);
1148 if (kobject_add(&d
->kobj
, &disk_to_dev(d
->disk
)->kobj
, "bcache"))
1151 bcache_device_link(d
, c
, "volume");
1155 kobject_put(&d
->kobj
);
1159 static int flash_devs_run(struct cache_set
*c
)
1162 struct uuid_entry
*u
;
1165 u
< c
->uuids
+ c
->nr_uuids
&& !ret
;
1167 if (UUID_FLASH_ONLY(u
))
1168 ret
= flash_dev_run(c
, u
);
1173 int bch_flash_dev_create(struct cache_set
*c
, uint64_t size
)
1175 struct uuid_entry
*u
;
1177 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1180 u
= uuid_find_empty(c
);
1182 pr_err("Can't create volume, no room for UUID");
1186 get_random_bytes(u
->uuid
, 16);
1187 memset(u
->label
, 0, 32);
1188 u
->first_reg
= u
->last_reg
= cpu_to_le32(get_seconds());
1190 SET_UUID_FLASH_ONLY(u
, 1);
1191 u
->sectors
= size
>> 9;
1195 return flash_dev_run(c
, u
);
1201 bool bch_cache_set_error(struct cache_set
*c
, const char *fmt
, ...)
1205 if (test_bit(CACHE_SET_STOPPING
, &c
->flags
))
1208 /* XXX: we can be called from atomic context
1209 acquire_console_sem();
1212 printk(KERN_ERR
"bcache: error on %pU: ", c
->sb
.set_uuid
);
1214 va_start(args
, fmt
);
1218 printk(", disabling caching\n");
1220 bch_cache_set_unregister(c
);
1224 void bch_cache_set_release(struct kobject
*kobj
)
1226 struct cache_set
*c
= container_of(kobj
, struct cache_set
, kobj
);
1228 module_put(THIS_MODULE
);
1231 static void cache_set_free(struct closure
*cl
)
1233 struct cache_set
*c
= container_of(cl
, struct cache_set
, cl
);
1237 if (!IS_ERR_OR_NULL(c
->debug
))
1238 debugfs_remove(c
->debug
);
1240 bch_open_buckets_free(c
);
1241 bch_btree_cache_free(c
);
1242 bch_journal_free(c
);
1244 for_each_cache(ca
, c
, i
)
1246 kobject_put(&ca
->kobj
);
1248 free_pages((unsigned long) c
->uuids
, ilog2(bucket_pages(c
)));
1249 free_pages((unsigned long) c
->sort
, ilog2(bucket_pages(c
)));
1251 kfree(c
->fill_iter
);
1253 bioset_free(c
->bio_split
);
1255 mempool_destroy(c
->bio_meta
);
1257 mempool_destroy(c
->search
);
1260 mutex_lock(&bch_register_lock
);
1262 mutex_unlock(&bch_register_lock
);
1264 pr_info("Cache set %pU unregistered", c
->sb
.set_uuid
);
1265 wake_up(&unregister_wait
);
1267 closure_debug_destroy(&c
->cl
);
1268 kobject_put(&c
->kobj
);
1271 static void cache_set_flush(struct closure
*cl
)
1273 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1276 /* Shut down allocator threads */
1277 set_bit(CACHE_SET_STOPPING_2
, &c
->flags
);
1278 wake_up(&c
->alloc_wait
);
1280 bch_cache_accounting_destroy(&c
->accounting
);
1282 kobject_put(&c
->internal
);
1283 kobject_del(&c
->kobj
);
1285 if (!IS_ERR_OR_NULL(c
->root
))
1286 list_add(&c
->root
->list
, &c
->btree_cache
);
1288 /* Should skip this if we're unregistering because of an error */
1289 list_for_each_entry(b
, &c
->btree_cache
, list
)
1290 if (btree_node_dirty(b
))
1291 bch_btree_write(b
, true, NULL
);
1296 static void __cache_set_unregister(struct closure
*cl
)
1298 struct cache_set
*c
= container_of(cl
, struct cache_set
, caching
);
1299 struct cached_dev
*dc
, *t
;
1302 mutex_lock(&bch_register_lock
);
1304 if (test_bit(CACHE_SET_UNREGISTERING
, &c
->flags
))
1305 list_for_each_entry_safe(dc
, t
, &c
->cached_devs
, list
)
1306 bch_cached_dev_detach(dc
);
1308 for (i
= 0; i
< c
->nr_uuids
; i
++)
1309 if (c
->devices
[i
] && UUID_FLASH_ONLY(&c
->uuids
[i
]))
1310 bcache_device_stop(c
->devices
[i
]);
1312 mutex_unlock(&bch_register_lock
);
1314 continue_at(cl
, cache_set_flush
, system_wq
);
1317 void bch_cache_set_stop(struct cache_set
*c
)
1319 if (!test_and_set_bit(CACHE_SET_STOPPING
, &c
->flags
))
1320 closure_queue(&c
->caching
);
1323 void bch_cache_set_unregister(struct cache_set
*c
)
1325 set_bit(CACHE_SET_UNREGISTERING
, &c
->flags
);
1326 bch_cache_set_stop(c
);
1329 #define alloc_bucket_pages(gfp, c) \
1330 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1332 struct cache_set
*bch_cache_set_alloc(struct cache_sb
*sb
)
1335 struct cache_set
*c
= kzalloc(sizeof(struct cache_set
), GFP_KERNEL
);
1339 __module_get(THIS_MODULE
);
1340 closure_init(&c
->cl
, NULL
);
1341 set_closure_fn(&c
->cl
, cache_set_free
, system_wq
);
1343 closure_init(&c
->caching
, &c
->cl
);
1344 set_closure_fn(&c
->caching
, __cache_set_unregister
, system_wq
);
1346 /* Maybe create continue_at_noreturn() and use it here? */
1347 closure_set_stopped(&c
->cl
);
1348 closure_put(&c
->cl
);
1350 kobject_init(&c
->kobj
, &bch_cache_set_ktype
);
1351 kobject_init(&c
->internal
, &bch_cache_set_internal_ktype
);
1353 bch_cache_accounting_init(&c
->accounting
, &c
->cl
);
1355 memcpy(c
->sb
.set_uuid
, sb
->set_uuid
, 16);
1356 c
->sb
.block_size
= sb
->block_size
;
1357 c
->sb
.bucket_size
= sb
->bucket_size
;
1358 c
->sb
.nr_in_set
= sb
->nr_in_set
;
1359 c
->sb
.last_mount
= sb
->last_mount
;
1360 c
->bucket_bits
= ilog2(sb
->bucket_size
);
1361 c
->block_bits
= ilog2(sb
->block_size
);
1362 c
->nr_uuids
= bucket_bytes(c
) / sizeof(struct uuid_entry
);
1364 c
->btree_pages
= c
->sb
.bucket_size
/ PAGE_SECTORS
;
1365 if (c
->btree_pages
> BTREE_MAX_PAGES
)
1366 c
->btree_pages
= max_t(int, c
->btree_pages
/ 4,
1369 init_waitqueue_head(&c
->alloc_wait
);
1370 mutex_init(&c
->bucket_lock
);
1371 mutex_init(&c
->fill_lock
);
1372 mutex_init(&c
->sort_lock
);
1373 spin_lock_init(&c
->sort_time_lock
);
1374 closure_init_unlocked(&c
->sb_write
);
1375 closure_init_unlocked(&c
->uuid_write
);
1376 spin_lock_init(&c
->btree_read_time_lock
);
1377 bch_moving_init_cache_set(c
);
1379 INIT_LIST_HEAD(&c
->list
);
1380 INIT_LIST_HEAD(&c
->cached_devs
);
1381 INIT_LIST_HEAD(&c
->btree_cache
);
1382 INIT_LIST_HEAD(&c
->btree_cache_freeable
);
1383 INIT_LIST_HEAD(&c
->btree_cache_freed
);
1384 INIT_LIST_HEAD(&c
->data_buckets
);
1386 c
->search
= mempool_create_slab_pool(32, bch_search_cache
);
1390 iter_size
= (sb
->bucket_size
/ sb
->block_size
+ 1) *
1391 sizeof(struct btree_iter_set
);
1393 if (!(c
->devices
= kzalloc(c
->nr_uuids
* sizeof(void *), GFP_KERNEL
)) ||
1394 !(c
->bio_meta
= mempool_create_kmalloc_pool(2,
1395 sizeof(struct bbio
) + sizeof(struct bio_vec
) *
1396 bucket_pages(c
))) ||
1397 !(c
->bio_split
= bioset_create(4, offsetof(struct bbio
, bio
))) ||
1398 !(c
->fill_iter
= kmalloc(iter_size
, GFP_KERNEL
)) ||
1399 !(c
->sort
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1400 !(c
->uuids
= alloc_bucket_pages(GFP_KERNEL
, c
)) ||
1401 bch_journal_alloc(c
) ||
1402 bch_btree_cache_alloc(c
) ||
1403 bch_open_buckets_alloc(c
))
1406 c
->fill_iter
->size
= sb
->bucket_size
/ sb
->block_size
;
1408 c
->congested_read_threshold_us
= 2000;
1409 c
->congested_write_threshold_us
= 20000;
1410 c
->error_limit
= 8 << IO_ERROR_SHIFT
;
1414 bch_cache_set_unregister(c
);
1418 static void run_cache_set(struct cache_set
*c
)
1420 const char *err
= "cannot allocate memory";
1421 struct cached_dev
*dc
, *t
;
1426 bch_btree_op_init_stack(&op
);
1429 for_each_cache(ca
, c
, i
)
1430 c
->nbuckets
+= ca
->sb
.nbuckets
;
1432 if (CACHE_SYNC(&c
->sb
)) {
1437 err
= "cannot allocate memory for journal";
1438 if (bch_journal_read(c
, &journal
, &op
))
1441 pr_debug("btree_journal_read() done");
1443 err
= "no journal entries found";
1444 if (list_empty(&journal
))
1447 j
= &list_entry(journal
.prev
, struct journal_replay
, list
)->j
;
1449 err
= "IO error reading priorities";
1450 for_each_cache(ca
, c
, i
)
1451 prio_read(ca
, j
->prio_bucket
[ca
->sb
.nr_this_dev
]);
1454 * If prio_read() fails it'll call cache_set_error and we'll
1455 * tear everything down right away, but if we perhaps checked
1456 * sooner we could avoid journal replay.
1461 err
= "bad btree root";
1462 if (__bch_ptr_invalid(c
, j
->btree_level
+ 1, k
))
1465 err
= "error reading btree root";
1466 c
->root
= bch_btree_node_get(c
, k
, j
->btree_level
, &op
);
1467 if (IS_ERR_OR_NULL(c
->root
))
1470 list_del_init(&c
->root
->list
);
1471 rw_unlock(true, c
->root
);
1473 err
= uuid_read(c
, j
, &op
.cl
);
1477 err
= "error in recovery";
1478 if (bch_btree_check(c
, &op
))
1481 bch_journal_mark(c
, &journal
);
1482 bch_btree_gc_finish(c
);
1483 pr_debug("btree_check() done");
1486 * bcache_journal_next() can't happen sooner, or
1487 * btree_gc_finish() will give spurious errors about last_gc >
1488 * gc_gen - this is a hack but oh well.
1490 bch_journal_next(&c
->journal
);
1492 for_each_cache(ca
, c
, i
)
1493 closure_call(&ca
->alloc
, bch_allocator_thread
,
1497 * First place it's safe to allocate: btree_check() and
1498 * btree_gc_finish() have to run before we have buckets to
1499 * allocate, and bch_bucket_alloc_set() might cause a journal
1500 * entry to be written so bcache_journal_next() has to be called
1503 * If the uuids were in the old format we have to rewrite them
1504 * before the next journal entry is written:
1506 if (j
->version
< BCACHE_JSET_VERSION_UUID
)
1509 bch_journal_replay(c
, &journal
, &op
);
1511 pr_notice("invalidating existing data");
1512 /* Don't want invalidate_buckets() to queue a gc yet */
1513 closure_lock(&c
->gc
, NULL
);
1515 for_each_cache(ca
, c
, i
) {
1518 ca
->sb
.keys
= clamp_t(int, ca
->sb
.nbuckets
>> 7,
1519 2, SB_JOURNAL_BUCKETS
);
1521 for (j
= 0; j
< ca
->sb
.keys
; j
++)
1522 ca
->sb
.d
[j
] = ca
->sb
.first_bucket
+ j
;
1525 bch_btree_gc_finish(c
);
1527 for_each_cache(ca
, c
, i
)
1528 closure_call(&ca
->alloc
, bch_allocator_thread
,
1529 ca
->alloc_workqueue
, &c
->cl
);
1531 mutex_lock(&c
->bucket_lock
);
1532 for_each_cache(ca
, c
, i
)
1534 mutex_unlock(&c
->bucket_lock
);
1536 wake_up(&c
->alloc_wait
);
1538 err
= "cannot allocate new UUID bucket";
1539 if (__uuid_write(c
))
1542 err
= "cannot allocate new btree root";
1543 c
->root
= bch_btree_node_alloc(c
, 0, &op
.cl
);
1544 if (IS_ERR_OR_NULL(c
->root
))
1547 bkey_copy_key(&c
->root
->key
, &MAX_KEY
);
1548 bch_btree_write(c
->root
, true, &op
);
1550 bch_btree_set_root(c
->root
);
1551 rw_unlock(true, c
->root
);
1554 * We don't want to write the first journal entry until
1555 * everything is set up - fortunately journal entries won't be
1556 * written until the SET_CACHE_SYNC() here:
1558 SET_CACHE_SYNC(&c
->sb
, true);
1560 bch_journal_next(&c
->journal
);
1561 bch_journal_meta(c
, &op
.cl
);
1564 closure_set_stopped(&c
->gc
.cl
);
1565 closure_put(&c
->gc
.cl
);
1568 closure_sync(&op
.cl
);
1569 c
->sb
.last_mount
= get_seconds();
1570 bcache_write_super(c
);
1572 list_for_each_entry_safe(dc
, t
, &uncached_devices
, list
)
1573 bch_cached_dev_attach(dc
, c
);
1579 closure_set_stopped(&c
->gc
.cl
);
1580 closure_put(&c
->gc
.cl
);
1582 closure_sync(&op
.cl
);
1583 /* XXX: test this, it's broken */
1584 bch_cache_set_error(c
, err
);
1587 static bool can_attach_cache(struct cache
*ca
, struct cache_set
*c
)
1589 return ca
->sb
.block_size
== c
->sb
.block_size
&&
1590 ca
->sb
.bucket_size
== c
->sb
.block_size
&&
1591 ca
->sb
.nr_in_set
== c
->sb
.nr_in_set
;
1594 static const char *register_cache_set(struct cache
*ca
)
1597 const char *err
= "cannot allocate memory";
1598 struct cache_set
*c
;
1600 list_for_each_entry(c
, &bch_cache_sets
, list
)
1601 if (!memcmp(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16)) {
1602 if (c
->cache
[ca
->sb
.nr_this_dev
])
1603 return "duplicate cache set member";
1605 if (!can_attach_cache(ca
, c
))
1606 return "cache sb does not match set";
1608 if (!CACHE_SYNC(&ca
->sb
))
1609 SET_CACHE_SYNC(&c
->sb
, false);
1614 c
= bch_cache_set_alloc(&ca
->sb
);
1618 err
= "error creating kobject";
1619 if (kobject_add(&c
->kobj
, bcache_kobj
, "%pU", c
->sb
.set_uuid
) ||
1620 kobject_add(&c
->internal
, &c
->kobj
, "internal"))
1623 if (bch_cache_accounting_add_kobjs(&c
->accounting
, &c
->kobj
))
1626 bch_debug_init_cache_set(c
);
1628 list_add(&c
->list
, &bch_cache_sets
);
1630 sprintf(buf
, "cache%i", ca
->sb
.nr_this_dev
);
1631 if (sysfs_create_link(&ca
->kobj
, &c
->kobj
, "set") ||
1632 sysfs_create_link(&c
->kobj
, &ca
->kobj
, buf
))
1635 if (ca
->sb
.seq
> c
->sb
.seq
) {
1636 c
->sb
.version
= ca
->sb
.version
;
1637 memcpy(c
->sb
.set_uuid
, ca
->sb
.set_uuid
, 16);
1638 c
->sb
.flags
= ca
->sb
.flags
;
1639 c
->sb
.seq
= ca
->sb
.seq
;
1640 pr_debug("set version = %llu", c
->sb
.version
);
1644 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = ca
;
1645 c
->cache_by_alloc
[c
->caches_loaded
++] = ca
;
1647 if (c
->caches_loaded
== c
->sb
.nr_in_set
)
1652 bch_cache_set_unregister(c
);
1658 void bch_cache_release(struct kobject
*kobj
)
1660 struct cache
*ca
= container_of(kobj
, struct cache
, kobj
);
1663 ca
->set
->cache
[ca
->sb
.nr_this_dev
] = NULL
;
1665 bch_cache_allocator_exit(ca
);
1667 bio_split_pool_free(&ca
->bio_split_hook
);
1669 if (ca
->alloc_workqueue
)
1670 destroy_workqueue(ca
->alloc_workqueue
);
1672 free_pages((unsigned long) ca
->disk_buckets
, ilog2(bucket_pages(ca
)));
1673 kfree(ca
->prio_buckets
);
1676 free_heap(&ca
->heap
);
1677 free_fifo(&ca
->unused
);
1678 free_fifo(&ca
->free_inc
);
1679 free_fifo(&ca
->free
);
1681 if (ca
->sb_bio
.bi_inline_vecs
[0].bv_page
)
1682 put_page(ca
->sb_bio
.bi_io_vec
[0].bv_page
);
1684 if (!IS_ERR_OR_NULL(ca
->bdev
)) {
1685 blk_sync_queue(bdev_get_queue(ca
->bdev
));
1686 blkdev_put(ca
->bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1690 module_put(THIS_MODULE
);
1693 static int cache_alloc(struct cache_sb
*sb
, struct cache
*ca
)
1701 __module_get(THIS_MODULE
);
1702 kobject_init(&ca
->kobj
, &bch_cache_ktype
);
1704 memcpy(&ca
->sb
, sb
, sizeof(struct cache_sb
));
1706 INIT_LIST_HEAD(&ca
->discards
);
1708 bio_init(&ca
->sb_bio
);
1709 ca
->sb_bio
.bi_max_vecs
= 1;
1710 ca
->sb_bio
.bi_io_vec
= ca
->sb_bio
.bi_inline_vecs
;
1712 bio_init(&ca
->journal
.bio
);
1713 ca
->journal
.bio
.bi_max_vecs
= 8;
1714 ca
->journal
.bio
.bi_io_vec
= ca
->journal
.bio
.bi_inline_vecs
;
1716 free
= roundup_pow_of_two(ca
->sb
.nbuckets
) >> 9;
1717 free
= max_t(size_t, free
, (prio_buckets(ca
) + 8) * 2);
1719 if (!init_fifo(&ca
->free
, free
, GFP_KERNEL
) ||
1720 !init_fifo(&ca
->free_inc
, free
<< 2, GFP_KERNEL
) ||
1721 !init_fifo(&ca
->unused
, free
<< 2, GFP_KERNEL
) ||
1722 !init_heap(&ca
->heap
, free
<< 3, GFP_KERNEL
) ||
1723 !(ca
->buckets
= vmalloc(sizeof(struct bucket
) *
1724 ca
->sb
.nbuckets
)) ||
1725 !(ca
->prio_buckets
= kzalloc(sizeof(uint64_t) * prio_buckets(ca
) *
1727 !(ca
->disk_buckets
= alloc_bucket_pages(GFP_KERNEL
, ca
)) ||
1728 !(ca
->alloc_workqueue
= alloc_workqueue("bch_allocator", 0, 1)) ||
1729 bio_split_pool_init(&ca
->bio_split_hook
))
1732 ca
->prio_last_buckets
= ca
->prio_buckets
+ prio_buckets(ca
);
1734 memset(ca
->buckets
, 0, ca
->sb
.nbuckets
* sizeof(struct bucket
));
1735 for_each_bucket(b
, ca
)
1736 atomic_set(&b
->pin
, 0);
1738 if (bch_cache_allocator_init(ca
))
1743 kobject_put(&ca
->kobj
);
1747 static const char *register_cache(struct cache_sb
*sb
, struct page
*sb_page
,
1748 struct block_device
*bdev
, struct cache
*ca
)
1750 char name
[BDEVNAME_SIZE
];
1751 const char *err
= "cannot allocate memory";
1753 if (cache_alloc(sb
, ca
) != 0)
1756 ca
->sb_bio
.bi_io_vec
[0].bv_page
= sb_page
;
1758 ca
->bdev
->bd_holder
= ca
;
1760 if (blk_queue_discard(bdev_get_queue(ca
->bdev
)))
1761 ca
->discard
= CACHE_DISCARD(&ca
->sb
);
1763 err
= "error creating kobject";
1764 if (kobject_add(&ca
->kobj
, &part_to_dev(bdev
->bd_part
)->kobj
, "bcache"))
1767 err
= register_cache_set(ca
);
1771 pr_info("registered cache device %s", bdevname(bdev
, name
));
1775 kobject_put(&ca
->kobj
);
1776 pr_info("error opening %s: %s", bdevname(bdev
, name
), err
);
1777 /* Return NULL instead of an error because kobject_put() cleans
1783 /* Global interfaces/init */
1785 static ssize_t
register_bcache(struct kobject
*, struct kobj_attribute
*,
1786 const char *, size_t);
1788 kobj_attribute_write(register, register_bcache
);
1789 kobj_attribute_write(register_quiet
, register_bcache
);
1791 static ssize_t
register_bcache(struct kobject
*k
, struct kobj_attribute
*attr
,
1792 const char *buffer
, size_t size
)
1795 const char *err
= "cannot allocate memory";
1797 struct cache_sb
*sb
= NULL
;
1798 struct block_device
*bdev
= NULL
;
1799 struct page
*sb_page
= NULL
;
1801 if (!try_module_get(THIS_MODULE
))
1804 mutex_lock(&bch_register_lock
);
1806 if (!(path
= kstrndup(buffer
, size
, GFP_KERNEL
)) ||
1807 !(sb
= kmalloc(sizeof(struct cache_sb
), GFP_KERNEL
)))
1810 err
= "failed to open device";
1811 bdev
= blkdev_get_by_path(strim(path
),
1812 FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1814 if (bdev
== ERR_PTR(-EBUSY
))
1815 err
= "device busy";
1818 set_blocksize(bdev
, 4096))
1821 err
= read_super(sb
, bdev
, &sb_page
);
1825 if (SB_IS_BDEV(sb
)) {
1826 struct cached_dev
*dc
= kzalloc(sizeof(*dc
), GFP_KERNEL
);
1828 err
= register_bdev(sb
, sb_page
, bdev
, dc
);
1830 struct cache
*ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
1832 err
= register_cache(sb
, sb_page
, bdev
, ca
);
1836 /* register_(bdev|cache) will only return an error if they
1837 * didn't get far enough to create the kobject - if they did,
1838 * the kobject destructor will do this cleanup.
1842 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1844 if (attr
!= &ksysfs_register_quiet
)
1845 pr_info("error opening %s: %s", path
, err
);
1851 mutex_unlock(&bch_register_lock
);
1852 module_put(THIS_MODULE
);
1856 static int bcache_reboot(struct notifier_block
*n
, unsigned long code
, void *x
)
1858 if (code
== SYS_DOWN
||
1860 code
== SYS_POWER_OFF
) {
1862 unsigned long start
= jiffies
;
1863 bool stopped
= false;
1865 struct cache_set
*c
, *tc
;
1866 struct cached_dev
*dc
, *tdc
;
1868 mutex_lock(&bch_register_lock
);
1870 if (list_empty(&bch_cache_sets
) &&
1871 list_empty(&uncached_devices
))
1874 pr_info("Stopping all devices:");
1876 list_for_each_entry_safe(c
, tc
, &bch_cache_sets
, list
)
1877 bch_cache_set_stop(c
);
1879 list_for_each_entry_safe(dc
, tdc
, &uncached_devices
, list
)
1880 bcache_device_stop(&dc
->disk
);
1882 /* What's a condition variable? */
1884 long timeout
= start
+ 2 * HZ
- jiffies
;
1886 stopped
= list_empty(&bch_cache_sets
) &&
1887 list_empty(&uncached_devices
);
1889 if (timeout
< 0 || stopped
)
1892 prepare_to_wait(&unregister_wait
, &wait
,
1893 TASK_UNINTERRUPTIBLE
);
1895 mutex_unlock(&bch_register_lock
);
1896 schedule_timeout(timeout
);
1897 mutex_lock(&bch_register_lock
);
1900 finish_wait(&unregister_wait
, &wait
);
1903 pr_info("All devices stopped");
1905 pr_notice("Timeout waiting for devices to be closed");
1907 mutex_unlock(&bch_register_lock
);
1913 static struct notifier_block reboot
= {
1914 .notifier_call
= bcache_reboot
,
1915 .priority
= INT_MAX
, /* before any real devices */
1918 static void bcache_exit(void)
1921 bch_writeback_exit();
1925 kobject_put(bcache_kobj
);
1927 destroy_workqueue(bcache_wq
);
1928 unregister_blkdev(bcache_major
, "bcache");
1929 unregister_reboot_notifier(&reboot
);
1932 static int __init
bcache_init(void)
1934 static const struct attribute
*files
[] = {
1935 &ksysfs_register
.attr
,
1936 &ksysfs_register_quiet
.attr
,
1940 mutex_init(&bch_register_lock
);
1941 init_waitqueue_head(&unregister_wait
);
1942 register_reboot_notifier(&reboot
);
1943 closure_debug_init();
1945 bcache_major
= register_blkdev(0, "bcache");
1946 if (bcache_major
< 0)
1947 return bcache_major
;
1949 if (!(bcache_wq
= create_workqueue("bcache")) ||
1950 !(bcache_kobj
= kobject_create_and_add("bcache", fs_kobj
)) ||
1951 sysfs_create_files(bcache_kobj
, files
) ||
1953 bch_request_init() ||
1954 bch_writeback_init() ||
1955 bch_debug_init(bcache_kobj
))
1964 module_exit(bcache_exit
);
1965 module_init(bcache_init
);