blob: be01fd3c87f18d80d750be1eb9a6609d83789b12 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "btree.h"
11#include "debug.h"
Kent Overstreet65d45232013-12-20 17:22:05 -080012#include "extents.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070013#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070014#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070015
Kent Overstreetc37511b2013-04-26 15:39:55 -070016#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070017#include <linux/buffer_head.h>
18#include <linux/debugfs.h>
19#include <linux/genhd.h>
Kent Overstreet28935ab2013-07-31 01:12:02 -070020#include <linux/idr.h>
Kent Overstreet79826c32013-07-10 18:31:58 -070021#include <linux/kthread.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070022#include <linux/module.h>
23#include <linux/random.h>
24#include <linux/reboot.h>
25#include <linux/sysfs.h>
26
27MODULE_LICENSE("GPL");
28MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
29
30static const char bcache_magic[] = {
31 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
32 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
33};
34
35static const char invalid_uuid[] = {
36 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
37 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
38};
39
40/* Default is -1; we skip past it for struct cached_dev's cache mode */
41const char * const bch_cache_modes[] = {
42 "default",
43 "writethrough",
44 "writeback",
45 "writearound",
46 "none",
47 NULL
48};
49
Kent Overstreetcafe5632013-03-23 16:11:31 -070050static struct kobject *bcache_kobj;
51struct mutex bch_register_lock;
52LIST_HEAD(bch_cache_sets);
53static LIST_HEAD(uncached_devices);
54
Kent Overstreet28935ab2013-07-31 01:12:02 -070055static int bcache_major;
56static DEFINE_IDA(bcache_minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -070057static wait_queue_head_t unregister_wait;
58struct workqueue_struct *bcache_wq;
59
60#define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
61
62static void bio_split_pool_free(struct bio_split_pool *p)
63{
Kent Overstreet8ef74792013-04-05 13:46:13 -070064 if (p->bio_split_hook)
65 mempool_destroy(p->bio_split_hook);
66
Kent Overstreetcafe5632013-03-23 16:11:31 -070067 if (p->bio_split)
68 bioset_free(p->bio_split);
Kent Overstreetcafe5632013-03-23 16:11:31 -070069}
70
71static int bio_split_pool_init(struct bio_split_pool *p)
72{
73 p->bio_split = bioset_create(4, 0);
74 if (!p->bio_split)
75 return -ENOMEM;
76
77 p->bio_split_hook = mempool_create_kmalloc_pool(4,
78 sizeof(struct bio_split_hook));
79 if (!p->bio_split_hook)
80 return -ENOMEM;
81
82 return 0;
83}
84
85/* Superblock */
86
87static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
88 struct page **res)
89{
90 const char *err;
91 struct cache_sb *s;
92 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
93 unsigned i;
94
95 if (!bh)
96 return "IO error";
97
98 s = (struct cache_sb *) bh->b_data;
99
100 sb->offset = le64_to_cpu(s->offset);
101 sb->version = le64_to_cpu(s->version);
102
103 memcpy(sb->magic, s->magic, 16);
104 memcpy(sb->uuid, s->uuid, 16);
105 memcpy(sb->set_uuid, s->set_uuid, 16);
106 memcpy(sb->label, s->label, SB_LABEL_SIZE);
107
108 sb->flags = le64_to_cpu(s->flags);
109 sb->seq = le64_to_cpu(s->seq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700110 sb->last_mount = le32_to_cpu(s->last_mount);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700111 sb->first_bucket = le16_to_cpu(s->first_bucket);
112 sb->keys = le16_to_cpu(s->keys);
113
114 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
115 sb->d[i] = le64_to_cpu(s->d[i]);
116
117 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
118 sb->version, sb->flags, sb->seq, sb->keys);
119
120 err = "Not a bcache superblock";
121 if (sb->offset != SB_SECTOR)
122 goto err;
123
124 if (memcmp(sb->magic, bcache_magic, 16))
125 goto err;
126
127 err = "Too many journal buckets";
128 if (sb->keys > SB_JOURNAL_BUCKETS)
129 goto err;
130
131 err = "Bad checksum";
132 if (s->csum != csum_set(s))
133 goto err;
134
135 err = "Bad UUID";
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600136 if (bch_is_zero(sb->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700137 goto err;
138
Kent Overstreet8abb2a52013-04-23 21:51:48 -0700139 sb->block_size = le16_to_cpu(s->block_size);
140
141 err = "Superblock block size smaller than device block size";
142 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
143 goto err;
144
Kent Overstreet29033812013-04-11 15:14:35 -0700145 switch (sb->version) {
146 case BCACHE_SB_VERSION_BDEV:
Kent Overstreet29033812013-04-11 15:14:35 -0700147 sb->data_offset = BDEV_DATA_START_DEFAULT;
148 break;
149 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
Kent Overstreet29033812013-04-11 15:14:35 -0700150 sb->data_offset = le64_to_cpu(s->data_offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700151
Kent Overstreet29033812013-04-11 15:14:35 -0700152 err = "Bad data offset";
153 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700154 goto err;
155
Kent Overstreet29033812013-04-11 15:14:35 -0700156 break;
157 case BCACHE_SB_VERSION_CDEV:
158 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
159 sb->nbuckets = le64_to_cpu(s->nbuckets);
160 sb->block_size = le16_to_cpu(s->block_size);
161 sb->bucket_size = le16_to_cpu(s->bucket_size);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700162
Kent Overstreet29033812013-04-11 15:14:35 -0700163 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
164 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
165
166 err = "Too many buckets";
167 if (sb->nbuckets > LONG_MAX)
168 goto err;
169
170 err = "Not enough buckets";
171 if (sb->nbuckets < 1 << 7)
172 goto err;
173
174 err = "Bad block/bucket size";
175 if (!is_power_of_2(sb->block_size) ||
176 sb->block_size > PAGE_SECTORS ||
177 !is_power_of_2(sb->bucket_size) ||
178 sb->bucket_size < PAGE_SECTORS)
179 goto err;
180
181 err = "Invalid superblock: device too small";
182 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
183 goto err;
184
185 err = "Bad UUID";
186 if (bch_is_zero(sb->set_uuid, 16))
187 goto err;
188
189 err = "Bad cache device number in set";
190 if (!sb->nr_in_set ||
191 sb->nr_in_set <= sb->nr_this_dev ||
192 sb->nr_in_set > MAX_CACHES_PER_SET)
193 goto err;
194
195 err = "Journal buckets not sequential";
196 for (i = 0; i < sb->keys; i++)
197 if (sb->d[i] != sb->first_bucket + i)
198 goto err;
199
200 err = "Too many journal buckets";
201 if (sb->first_bucket + sb->keys > sb->nbuckets)
202 goto err;
203
204 err = "Invalid superblock: first bucket comes before end of super";
205 if (sb->first_bucket * sb->bucket_size < 16)
206 goto err;
207
208 break;
209 default:
210 err = "Unsupported superblock version";
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211 goto err;
Kent Overstreet29033812013-04-11 15:14:35 -0700212 }
213
Kent Overstreetcafe5632013-03-23 16:11:31 -0700214 sb->last_mount = get_seconds();
215 err = NULL;
216
217 get_page(bh->b_page);
218 *res = bh->b_page;
219err:
220 put_bh(bh);
221 return err;
222}
223
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200224static void write_bdev_super_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700225{
226 struct cached_dev *dc = bio->bi_private;
227 /* XXX: error checking */
228
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800229 closure_put(&dc->sb_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700230}
231
232static void __write_super(struct cache_sb *sb, struct bio *bio)
233{
234 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page);
235 unsigned i;
236
Kent Overstreet4f024f32013-10-11 15:44:27 -0700237 bio->bi_iter.bi_sector = SB_SECTOR;
238 bio->bi_rw = REQ_SYNC|REQ_META;
239 bio->bi_iter.bi_size = SB_SIZE;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600240 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700241
242 out->offset = cpu_to_le64(sb->offset);
243 out->version = cpu_to_le64(sb->version);
244
245 memcpy(out->uuid, sb->uuid, 16);
246 memcpy(out->set_uuid, sb->set_uuid, 16);
247 memcpy(out->label, sb->label, SB_LABEL_SIZE);
248
249 out->flags = cpu_to_le64(sb->flags);
250 out->seq = cpu_to_le64(sb->seq);
251
252 out->last_mount = cpu_to_le32(sb->last_mount);
253 out->first_bucket = cpu_to_le16(sb->first_bucket);
254 out->keys = cpu_to_le16(sb->keys);
255
256 for (i = 0; i < sb->keys; i++)
257 out->d[i] = cpu_to_le64(sb->d[i]);
258
259 out->csum = csum_set(out);
260
261 pr_debug("ver %llu, flags %llu, seq %llu",
262 sb->version, sb->flags, sb->seq);
263
264 submit_bio(REQ_WRITE, bio);
265}
266
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800267static void bch_write_bdev_super_unlock(struct closure *cl)
268{
269 struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
270
271 up(&dc->sb_write_mutex);
272}
273
Kent Overstreetcafe5632013-03-23 16:11:31 -0700274void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
275{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800276 struct closure *cl = &dc->sb_write;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700277 struct bio *bio = &dc->sb_bio;
278
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800279 down(&dc->sb_write_mutex);
280 closure_init(cl, parent);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700281
282 bio_reset(bio);
283 bio->bi_bdev = dc->bdev;
284 bio->bi_end_io = write_bdev_super_endio;
285 bio->bi_private = dc;
286
287 closure_get(cl);
288 __write_super(&dc->sb, bio);
289
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800290 closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700291}
292
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200293static void write_super_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700294{
295 struct cache *ca = bio->bi_private;
296
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200297 bch_count_io_errors(ca, bio->bi_error, "writing superblock");
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800298 closure_put(&ca->set->sb_write);
299}
300
301static void bcache_write_super_unlock(struct closure *cl)
302{
303 struct cache_set *c = container_of(cl, struct cache_set, sb_write);
304
305 up(&c->sb_write_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700306}
307
308void bcache_write_super(struct cache_set *c)
309{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800310 struct closure *cl = &c->sb_write;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700311 struct cache *ca;
312 unsigned i;
313
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800314 down(&c->sb_write_mutex);
315 closure_init(cl, &c->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700316
317 c->sb.seq++;
318
319 for_each_cache(ca, c, i) {
320 struct bio *bio = &ca->sb_bio;
321
Kent Overstreet29033812013-04-11 15:14:35 -0700322 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700323 ca->sb.seq = c->sb.seq;
324 ca->sb.last_mount = c->sb.last_mount;
325
326 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
327
328 bio_reset(bio);
329 bio->bi_bdev = ca->bdev;
330 bio->bi_end_io = write_super_endio;
331 bio->bi_private = ca;
332
333 closure_get(cl);
334 __write_super(&ca->sb, bio);
335 }
336
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800337 closure_return_with_destructor(cl, bcache_write_super_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700338}
339
340/* UUID io */
341
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200342static void uuid_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700343{
344 struct closure *cl = bio->bi_private;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800345 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700346
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200347 cache_set_err_on(bio->bi_error, c, "accessing uuids");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700348 bch_bbio_free(bio, c);
349 closure_put(cl);
350}
351
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800352static void uuid_io_unlock(struct closure *cl)
353{
354 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
355
356 up(&c->uuid_write_mutex);
357}
358
Kent Overstreetcafe5632013-03-23 16:11:31 -0700359static void uuid_io(struct cache_set *c, unsigned long rw,
360 struct bkey *k, struct closure *parent)
361{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800362 struct closure *cl = &c->uuid_write;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700363 struct uuid_entry *u;
364 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -0700365 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700366
367 BUG_ON(!parent);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800368 down(&c->uuid_write_mutex);
369 closure_init(cl, parent);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700370
371 for (i = 0; i < KEY_PTRS(k); i++) {
372 struct bio *bio = bch_bbio_alloc(c);
373
374 bio->bi_rw = REQ_SYNC|REQ_META|rw;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700375 bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700376
377 bio->bi_end_io = uuid_endio;
378 bio->bi_private = cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600379 bch_bio_map(bio, c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700380
381 bch_submit_bbio(bio, c, k, i);
382
383 if (!(rw & WRITE))
384 break;
385 }
386
Kent Overstreetdc9d98d2013-12-17 23:47:33 -0800387 bch_extent_to_text(buf, sizeof(buf), k);
Kent Overstreet85b14922013-05-14 20:33:16 -0700388 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700389
390 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600391 if (!bch_is_zero(u->uuid, 16))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700392 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
393 u - c->uuids, u->uuid, u->label,
394 u->first_reg, u->last_reg, u->invalidated);
395
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800396 closure_return_with_destructor(cl, uuid_io_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700397}
398
399static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
400{
401 struct bkey *k = &j->uuid_bucket;
402
Kent Overstreet65d45232013-12-20 17:22:05 -0800403 if (__bch_btree_ptr_invalid(c, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700404 return "bad uuid pointer";
405
406 bkey_copy(&c->uuid_bucket, k);
407 uuid_io(c, READ_SYNC, k, cl);
408
409 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
410 struct uuid_entry_v0 *u0 = (void *) c->uuids;
411 struct uuid_entry *u1 = (void *) c->uuids;
412 int i;
413
414 closure_sync(cl);
415
416 /*
417 * Since the new uuid entry is bigger than the old, we have to
418 * convert starting at the highest memory address and work down
419 * in order to do it in place
420 */
421
422 for (i = c->nr_uuids - 1;
423 i >= 0;
424 --i) {
425 memcpy(u1[i].uuid, u0[i].uuid, 16);
426 memcpy(u1[i].label, u0[i].label, 32);
427
428 u1[i].first_reg = u0[i].first_reg;
429 u1[i].last_reg = u0[i].last_reg;
430 u1[i].invalidated = u0[i].invalidated;
431
432 u1[i].flags = 0;
433 u1[i].sectors = 0;
434 }
435 }
436
437 return NULL;
438}
439
440static int __uuid_write(struct cache_set *c)
441{
442 BKEY_PADDED(key) k;
443 struct closure cl;
444 closure_init_stack(&cl);
445
446 lockdep_assert_held(&bch_register_lock);
447
Kent Overstreet78365412013-12-17 01:29:34 -0800448 if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, true))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700449 return 1;
450
451 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
452 uuid_io(c, REQ_WRITE, &k.key, &cl);
453 closure_sync(&cl);
454
455 bkey_copy(&c->uuid_bucket, &k.key);
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700456 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700457 return 0;
458}
459
460int bch_uuid_write(struct cache_set *c)
461{
462 int ret = __uuid_write(c);
463
464 if (!ret)
465 bch_journal_meta(c, NULL);
466
467 return ret;
468}
469
470static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
471{
472 struct uuid_entry *u;
473
474 for (u = c->uuids;
475 u < c->uuids + c->nr_uuids; u++)
476 if (!memcmp(u->uuid, uuid, 16))
477 return u;
478
479 return NULL;
480}
481
482static struct uuid_entry *uuid_find_empty(struct cache_set *c)
483{
484 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
485 return uuid_find(c, zero_uuid);
486}
487
488/*
489 * Bucket priorities/gens:
490 *
491 * For each bucket, we store on disk its
492 * 8 bit gen
493 * 16 bit priority
494 *
495 * See alloc.c for an explanation of the gen. The priority is used to implement
496 * lru (and in the future other) cache replacement policies; for most purposes
497 * it's just an opaque integer.
498 *
499 * The gens and the priorities don't have a whole lot to do with each other, and
500 * it's actually the gens that must be written out at specific times - it's no
501 * big deal if the priorities don't get written, if we lose them we just reuse
502 * buckets in suboptimal order.
503 *
504 * On disk they're stored in a packed array, and in as many buckets are required
505 * to fit them all. The buckets we use to store them form a list; the journal
506 * header points to the first bucket, the first bucket points to the second
507 * bucket, et cetera.
508 *
509 * This code is used by the allocation code; periodically (whenever it runs out
510 * of buckets to allocate from) the allocation code will invalidate some
511 * buckets, but it can't use those buckets until their new gens are safely on
512 * disk.
513 */
514
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200515static void prio_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700516{
517 struct cache *ca = bio->bi_private;
518
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200519 cache_set_err_on(bio->bi_error, ca->set, "accessing priorities");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700520 bch_bbio_free(bio, ca->set);
521 closure_put(&ca->prio);
522}
523
524static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
525{
526 struct closure *cl = &ca->prio;
527 struct bio *bio = bch_bbio_alloc(ca->set);
528
529 closure_init_stack(cl);
530
Kent Overstreet4f024f32013-10-11 15:44:27 -0700531 bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size;
532 bio->bi_bdev = ca->bdev;
533 bio->bi_rw = REQ_SYNC|REQ_META|rw;
534 bio->bi_iter.bi_size = bucket_bytes(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700535
536 bio->bi_end_io = prio_endio;
537 bio->bi_private = ca;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600538 bch_bio_map(bio, ca->disk_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700539
540 closure_bio_submit(bio, &ca->prio, ca);
541 closure_sync(cl);
542}
543
Kent Overstreetcafe5632013-03-23 16:11:31 -0700544void bch_prio_write(struct cache *ca)
545{
546 int i;
547 struct bucket *b;
548 struct closure cl;
549
550 closure_init_stack(&cl);
551
552 lockdep_assert_held(&ca->set->bucket_lock);
553
Kent Overstreetcafe5632013-03-23 16:11:31 -0700554 ca->disk_buckets->seq++;
555
556 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
557 &ca->meta_sectors_written);
558
Kent Overstreet78365412013-12-17 01:29:34 -0800559 //pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
560 // fifo_used(&ca->free_inc), fifo_used(&ca->unused));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700561
562 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
563 long bucket;
564 struct prio_set *p = ca->disk_buckets;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700565 struct bucket_disk *d = p->data;
566 struct bucket_disk *end = d + prios_per_bucket(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700567
568 for (b = ca->buckets + i * prios_per_bucket(ca);
569 b < ca->buckets + ca->sb.nbuckets && d < end;
570 b++, d++) {
571 d->prio = cpu_to_le16(b->prio);
572 d->gen = b->gen;
573 }
574
575 p->next_bucket = ca->prio_buckets[i + 1];
Kent Overstreet81ab4192013-10-31 15:46:42 -0700576 p->magic = pset_magic(&ca->sb);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600577 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700578
Kent Overstreet78365412013-12-17 01:29:34 -0800579 bucket = bch_bucket_alloc(ca, RESERVE_PRIO, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700580 BUG_ON(bucket == -1);
581
582 mutex_unlock(&ca->set->bucket_lock);
583 prio_io(ca, bucket, REQ_WRITE);
584 mutex_lock(&ca->set->bucket_lock);
585
586 ca->prio_buckets[i] = bucket;
587 atomic_dec_bug(&ca->buckets[bucket].pin);
588 }
589
590 mutex_unlock(&ca->set->bucket_lock);
591
592 bch_journal_meta(ca->set, &cl);
593 closure_sync(&cl);
594
595 mutex_lock(&ca->set->bucket_lock);
596
Kent Overstreetcafe5632013-03-23 16:11:31 -0700597 /*
598 * Don't want the old priorities to get garbage collected until after we
599 * finish writing the new ones, and they're journalled
600 */
Kent Overstreet2531d9ee2014-03-17 16:55:55 -0700601 for (i = 0; i < prio_buckets(ca); i++) {
602 if (ca->prio_last_buckets[i])
603 __bch_bucket_free(ca,
604 &ca->buckets[ca->prio_last_buckets[i]]);
605
Kent Overstreetcafe5632013-03-23 16:11:31 -0700606 ca->prio_last_buckets[i] = ca->prio_buckets[i];
Kent Overstreet2531d9ee2014-03-17 16:55:55 -0700607 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700608}
609
610static void prio_read(struct cache *ca, uint64_t bucket)
611{
612 struct prio_set *p = ca->disk_buckets;
613 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
614 struct bucket *b;
615 unsigned bucket_nr = 0;
616
617 for (b = ca->buckets;
618 b < ca->buckets + ca->sb.nbuckets;
619 b++, d++) {
620 if (d == end) {
621 ca->prio_buckets[bucket_nr] = bucket;
622 ca->prio_last_buckets[bucket_nr] = bucket;
623 bucket_nr++;
624
625 prio_io(ca, bucket, READ_SYNC);
626
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600627 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700628 pr_warn("bad csum reading priorities");
629
Kent Overstreet81ab4192013-10-31 15:46:42 -0700630 if (p->magic != pset_magic(&ca->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700631 pr_warn("bad magic reading priorities");
632
633 bucket = p->next_bucket;
634 d = p->data;
635 }
636
637 b->prio = le16_to_cpu(d->prio);
Kent Overstreet3a2fd9d2014-02-27 17:51:12 -0800638 b->gen = b->last_gc = d->gen;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700639 }
640}
641
642/* Bcache device */
643
644static int open_dev(struct block_device *b, fmode_t mode)
645{
646 struct bcache_device *d = b->bd_disk->private_data;
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700647 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700648 return -ENXIO;
649
650 closure_get(&d->cl);
651 return 0;
652}
653
Emil Goode867e1162013-05-09 22:39:26 +0200654static void release_dev(struct gendisk *b, fmode_t mode)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700655{
656 struct bcache_device *d = b->private_data;
657 closure_put(&d->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700658}
659
660static int ioctl_dev(struct block_device *b, fmode_t mode,
661 unsigned int cmd, unsigned long arg)
662{
663 struct bcache_device *d = b->bd_disk->private_data;
664 return d->ioctl(d, mode, cmd, arg);
665}
666
667static const struct block_device_operations bcache_ops = {
668 .open = open_dev,
669 .release = release_dev,
670 .ioctl = ioctl_dev,
671 .owner = THIS_MODULE,
672};
673
674void bcache_device_stop(struct bcache_device *d)
675{
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700676 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700677 closure_queue(&d->cl);
678}
679
Kent Overstreetee668502013-02-01 07:29:41 -0800680static void bcache_device_unlink(struct bcache_device *d)
681{
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700682 lockdep_assert_held(&bch_register_lock);
Kent Overstreetee668502013-02-01 07:29:41 -0800683
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700684 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
685 unsigned i;
686 struct cache *ca;
Kent Overstreetee668502013-02-01 07:29:41 -0800687
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700688 sysfs_remove_link(&d->c->kobj, d->name);
689 sysfs_remove_link(&d->kobj, "cache");
690
691 for_each_cache(ca, d->c, i)
692 bd_unlink_disk_holder(ca->bdev, d->disk);
693 }
Kent Overstreetee668502013-02-01 07:29:41 -0800694}
695
696static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
697 const char *name)
698{
699 unsigned i;
700 struct cache *ca;
701
702 for_each_cache(ca, d->c, i)
703 bd_link_disk_holder(ca->bdev, d->disk);
704
705 snprintf(d->name, BCACHEDEVNAME_SIZE,
706 "%s%u", name, d->id);
707
708 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
709 sysfs_create_link(&c->kobj, &d->kobj, d->name),
710 "Couldn't create device <-> cache set symlinks");
711}
712
Kent Overstreetcafe5632013-03-23 16:11:31 -0700713static void bcache_device_detach(struct bcache_device *d)
714{
715 lockdep_assert_held(&bch_register_lock);
716
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700717 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700718 struct uuid_entry *u = d->c->uuids + d->id;
719
720 SET_UUID_FLASH_ONLY(u, 0);
721 memcpy(u->uuid, invalid_uuid, 16);
722 u->invalidated = cpu_to_le32(get_seconds());
723 bch_uuid_write(d->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700724 }
725
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700726 bcache_device_unlink(d);
Kent Overstreetee668502013-02-01 07:29:41 -0800727
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728 d->c->devices[d->id] = NULL;
729 closure_put(&d->c->caching);
730 d->c = NULL;
731}
732
733static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
734 unsigned id)
735{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700736 d->id = id;
737 d->c = c;
738 c->devices[id] = d;
739
740 closure_get(&c->caching);
741}
742
Kent Overstreetcafe5632013-03-23 16:11:31 -0700743static void bcache_device_free(struct bcache_device *d)
744{
745 lockdep_assert_held(&bch_register_lock);
746
747 pr_info("%s stopped", d->disk->disk_name);
748
749 if (d->c)
750 bcache_device_detach(d);
Kent Overstreetf59fce82013-05-15 00:11:26 -0700751 if (d->disk && d->disk->flags & GENHD_FL_UP)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700752 del_gendisk(d->disk);
753 if (d->disk && d->disk->queue)
754 blk_cleanup_queue(d->disk->queue);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700755 if (d->disk) {
756 ida_simple_remove(&bcache_minor, d->disk->first_minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700757 put_disk(d->disk);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700758 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700759
760 bio_split_pool_free(&d->bio_split_hook);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700761 if (d->bio_split)
762 bioset_free(d->bio_split);
Pekka Enberg958b4332015-06-30 14:59:30 -0700763 kvfree(d->full_dirty_stripes);
764 kvfree(d->stripe_sectors_dirty);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700765
766 closure_debug_destroy(&d->cl);
767}
768
Kent Overstreet279afba2013-06-05 06:21:07 -0700769static int bcache_device_init(struct bcache_device *d, unsigned block_size,
770 sector_t sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700771{
772 struct request_queue *q;
Kent Overstreet279afba2013-06-05 06:21:07 -0700773 size_t n;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700774 int minor;
Kent Overstreet279afba2013-06-05 06:21:07 -0700775
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700776 if (!d->stripe_size)
777 d->stripe_size = 1 << 31;
Kent Overstreet279afba2013-06-05 06:21:07 -0700778
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700779 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
Kent Overstreet279afba2013-06-05 06:21:07 -0700780
Kent Overstreet48a915a2013-10-31 15:43:22 -0700781 if (!d->nr_stripes ||
782 d->nr_stripes > INT_MAX ||
783 d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
784 pr_err("nr_stripes too large");
Kent Overstreet279afba2013-06-05 06:21:07 -0700785 return -ENOMEM;
Kent Overstreet48a915a2013-10-31 15:43:22 -0700786 }
Kent Overstreet279afba2013-06-05 06:21:07 -0700787
788 n = d->nr_stripes * sizeof(atomic_t);
789 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
790 ? kzalloc(n, GFP_KERNEL)
791 : vzalloc(n);
792 if (!d->stripe_sectors_dirty)
793 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700794
Kent Overstreet48a915a2013-10-31 15:43:22 -0700795 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
796 d->full_dirty_stripes = n < PAGE_SIZE << 6
797 ? kzalloc(n, GFP_KERNEL)
798 : vzalloc(n);
799 if (!d->full_dirty_stripes)
800 return -ENOMEM;
801
Kent Overstreet28935ab2013-07-31 01:12:02 -0700802 minor = ida_simple_get(&bcache_minor, 0, MINORMASK + 1, GFP_KERNEL);
803 if (minor < 0)
804 return minor;
805
Kent Overstreetcafe5632013-03-23 16:11:31 -0700806 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -0700807 bio_split_pool_init(&d->bio_split_hook) ||
Kent Overstreet28935ab2013-07-31 01:12:02 -0700808 !(d->disk = alloc_disk(1))) {
809 ida_simple_remove(&bcache_minor, minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700810 return -ENOMEM;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700811 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700812
Kent Overstreet279afba2013-06-05 06:21:07 -0700813 set_capacity(d->disk, sectors);
Kent Overstreet28935ab2013-07-31 01:12:02 -0700814 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", minor);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700815
816 d->disk->major = bcache_major;
Kent Overstreet28935ab2013-07-31 01:12:02 -0700817 d->disk->first_minor = minor;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700818 d->disk->fops = &bcache_ops;
819 d->disk->private_data = d;
820
Kent Overstreet28935ab2013-07-31 01:12:02 -0700821 q = blk_alloc_queue(GFP_KERNEL);
822 if (!q)
823 return -ENOMEM;
824
Kent Overstreetcafe5632013-03-23 16:11:31 -0700825 blk_queue_make_request(q, NULL);
826 d->disk->queue = q;
827 q->queuedata = d;
828 q->backing_dev_info.congested_data = d;
829 q->limits.max_hw_sectors = UINT_MAX;
830 q->limits.max_sectors = UINT_MAX;
831 q->limits.max_segment_size = UINT_MAX;
832 q->limits.max_segments = BIO_MAX_PAGES;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600833 blk_queue_max_discard_sectors(q, UINT_MAX);
Kent Overstreet90db6912014-02-10 17:26:40 -0800834 q->limits.discard_granularity = 512;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700835 q->limits.io_min = block_size;
836 q->limits.logical_block_size = block_size;
837 q->limits.physical_block_size = block_size;
838 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -0600839 clear_bit(QUEUE_FLAG_ADD_RANDOM, &d->disk->queue->queue_flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700840 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
841
Kent Overstreet54d12f22013-07-10 18:44:40 -0700842 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
843
Kent Overstreetcafe5632013-03-23 16:11:31 -0700844 return 0;
845}
846
847/* Cached device */
848
849static void calc_cached_dev_sectors(struct cache_set *c)
850{
851 uint64_t sectors = 0;
852 struct cached_dev *dc;
853
854 list_for_each_entry(dc, &c->cached_devs, list)
855 sectors += bdev_sectors(dc->bdev);
856
857 c->cached_dev_sectors = sectors;
858}
859
860void bch_cached_dev_run(struct cached_dev *dc)
861{
862 struct bcache_device *d = &dc->disk;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200863 char buf[SB_LABEL_SIZE + 1];
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200864 char *env[] = {
865 "DRIVER=bcache",
866 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200867 NULL,
868 NULL,
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200869 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700870
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200871 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
872 buf[SB_LABEL_SIZE] = '\0';
873 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
874
Kent Overstreetcafe5632013-03-23 16:11:31 -0700875 if (atomic_xchg(&dc->running, 1))
876 return;
877
878 if (!d->c &&
879 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
880 struct closure cl;
881 closure_init_stack(&cl);
882
883 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
884 bch_write_bdev_super(dc, &cl);
885 closure_sync(&cl);
886 }
887
888 add_disk(d->disk);
Kent Overstreetee668502013-02-01 07:29:41 -0800889 bd_link_disk_holder(dc->bdev, dc->disk.disk);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200890 /* won't show up in the uevent file, use udevadm monitor -e instead
891 * only class / kset properties are persistent */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700892 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200893 kfree(env[1]);
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200894 kfree(env[2]);
Gabriel de Perthuisa25c32b2013-06-07 23:27:01 +0200895
Kent Overstreetcafe5632013-03-23 16:11:31 -0700896 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
897 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
898 pr_debug("error creating sysfs link");
899}
900
901static void cached_dev_detach_finish(struct work_struct *w)
902{
903 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
904 char buf[BDEVNAME_SIZE];
905 struct closure cl;
906 closure_init_stack(&cl);
907
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700908 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700909 BUG_ON(atomic_read(&dc->count));
910
Kent Overstreetcafe5632013-03-23 16:11:31 -0700911 mutex_lock(&bch_register_lock);
912
913 memset(&dc->sb.set_uuid, 0, 16);
914 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
915
916 bch_write_bdev_super(dc, &cl);
917 closure_sync(&cl);
918
919 bcache_device_detach(&dc->disk);
920 list_move(&dc->list, &uncached_devices);
921
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700922 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
Kent Overstreet5b1016e2014-03-19 17:49:37 -0700923 clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags);
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700924
Kent Overstreetcafe5632013-03-23 16:11:31 -0700925 mutex_unlock(&bch_register_lock);
926
927 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
928
929 /* Drop ref we took in cached_dev_detach() */
930 closure_put(&dc->disk.cl);
931}
932
933void bch_cached_dev_detach(struct cached_dev *dc)
934{
935 lockdep_assert_held(&bch_register_lock);
936
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700937 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700938 return;
939
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700940 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700941 return;
942
943 /*
944 * Block the device from being closed and freed until we're finished
945 * detaching
946 */
947 closure_get(&dc->disk.cl);
948
949 bch_writeback_queue(dc);
950 cached_dev_put(dc);
951}
952
953int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
954{
955 uint32_t rtime = cpu_to_le32(get_seconds());
956 struct uuid_entry *u;
957 char buf[BDEVNAME_SIZE];
958
959 bdevname(dc->bdev, buf);
960
961 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
962 return -ENOENT;
963
964 if (dc->disk.c) {
965 pr_err("Can't attach %s: already attached", buf);
966 return -EINVAL;
967 }
968
969 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
970 pr_err("Can't attach %s: shutting down", buf);
971 return -EINVAL;
972 }
973
974 if (dc->sb.block_size < c->sb.block_size) {
975 /* Will die */
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700976 pr_err("Couldn't attach %s: block size less than set's block size",
977 buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700978 return -EINVAL;
979 }
980
981 u = uuid_find(c, dc->sb.uuid);
982
983 if (u &&
984 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
985 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
986 memcpy(u->uuid, invalid_uuid, 16);
987 u->invalidated = cpu_to_le32(get_seconds());
988 u = NULL;
989 }
990
991 if (!u) {
992 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
993 pr_err("Couldn't find uuid for %s in set", buf);
994 return -ENOENT;
995 }
996
997 u = uuid_find_empty(c);
998 if (!u) {
999 pr_err("Not caching %s, no room for UUID", buf);
1000 return -EINVAL;
1001 }
1002 }
1003
1004 /* Deadlocks since we're called via sysfs...
1005 sysfs_remove_file(&dc->kobj, &sysfs_attach);
1006 */
1007
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001008 if (bch_is_zero(u->uuid, 16)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001009 struct closure cl;
1010 closure_init_stack(&cl);
1011
1012 memcpy(u->uuid, dc->sb.uuid, 16);
1013 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1014 u->first_reg = u->last_reg = rtime;
1015 bch_uuid_write(c);
1016
1017 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
1018 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1019
1020 bch_write_bdev_super(dc, &cl);
1021 closure_sync(&cl);
1022 } else {
1023 u->last_reg = rtime;
1024 bch_uuid_write(c);
1025 }
1026
1027 bcache_device_attach(&dc->disk, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028 list_move(&dc->list, &c->cached_devs);
1029 calc_cached_dev_sectors(c);
1030
1031 smp_wmb();
1032 /*
1033 * dc->c must be set before dc->count != 0 - paired with the mb in
1034 * cached_dev_get()
1035 */
1036 atomic_set(&dc->count, 1);
1037
Slava Pestov9e5c3532014-05-01 13:48:57 -07001038 if (bch_cached_dev_writeback_start(dc))
1039 return -ENOMEM;
1040
Kent Overstreetcafe5632013-03-23 16:11:31 -07001041 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001042 bch_sectors_dirty_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001043 atomic_set(&dc->has_dirty, 1);
1044 atomic_inc(&dc->count);
1045 bch_writeback_queue(dc);
1046 }
1047
1048 bch_cached_dev_run(dc);
Kent Overstreetee668502013-02-01 07:29:41 -08001049 bcache_device_link(&dc->disk, c, "bdev");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001050
1051 pr_info("Caching %s as %s on set %pU",
1052 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
1053 dc->disk.c->sb.set_uuid);
1054 return 0;
1055}
1056
1057void bch_cached_dev_release(struct kobject *kobj)
1058{
1059 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1060 disk.kobj);
1061 kfree(dc);
1062 module_put(THIS_MODULE);
1063}
1064
1065static void cached_dev_free(struct closure *cl)
1066{
1067 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1068
1069 cancel_delayed_work_sync(&dc->writeback_rate_update);
Slava Pestova664d0f2014-05-20 12:20:28 -07001070 if (!IS_ERR_OR_NULL(dc->writeback_thread))
1071 kthread_stop(dc->writeback_thread);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001072
1073 mutex_lock(&bch_register_lock);
1074
Kent Overstreetf59fce82013-05-15 00:11:26 -07001075 if (atomic_read(&dc->running))
1076 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001077 bcache_device_free(&dc->disk);
1078 list_del(&dc->list);
1079
1080 mutex_unlock(&bch_register_lock);
1081
Kent Overstreet0781c872014-07-07 13:03:36 -07001082 if (!IS_ERR_OR_NULL(dc->bdev))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001083 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001084
1085 wake_up(&unregister_wait);
1086
1087 kobject_put(&dc->disk.kobj);
1088}
1089
1090static void cached_dev_flush(struct closure *cl)
1091{
1092 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1093 struct bcache_device *d = &dc->disk;
1094
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001095 mutex_lock(&bch_register_lock);
Kent Overstreetc4d951d2013-08-21 17:49:09 -07001096 bcache_device_unlink(d);
Kent Overstreetc9502ea2013-07-10 21:25:02 -07001097 mutex_unlock(&bch_register_lock);
1098
Kent Overstreetcafe5632013-03-23 16:11:31 -07001099 bch_cache_accounting_destroy(&dc->accounting);
1100 kobject_del(&d->kobj);
1101
1102 continue_at(cl, cached_dev_free, system_wq);
1103}
1104
1105static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1106{
Kent Overstreetf59fce82013-05-15 00:11:26 -07001107 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001108 struct io *io;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001109 struct request_queue *q = bdev_get_queue(dc->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001110
1111 __module_get(THIS_MODULE);
1112 INIT_LIST_HEAD(&dc->list);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001113 closure_init(&dc->disk.cl, NULL);
1114 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001115 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001116 INIT_WORK(&dc->detach, cached_dev_detach_finish);
Kent Overstreetcb7a5832013-12-16 15:27:25 -08001117 sema_init(&dc->sb_write_mutex, 1);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001118 INIT_LIST_HEAD(&dc->io_lru);
1119 spin_lock_init(&dc->io_lock);
1120 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001121
Kent Overstreetcafe5632013-03-23 16:11:31 -07001122 dc->sequential_cutoff = 4 << 20;
1123
Kent Overstreetcafe5632013-03-23 16:11:31 -07001124 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1125 list_add(&io->lru, &dc->io_lru);
1126 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1127 }
1128
Kent Overstreetc78afc62013-07-11 22:39:53 -07001129 dc->disk.stripe_size = q->limits.io_opt >> 9;
1130
1131 if (dc->disk.stripe_size)
1132 dc->partial_stripes_expensive =
1133 q->limits.raid_partial_stripes_expensive;
1134
Kent Overstreet279afba2013-06-05 06:21:07 -07001135 ret = bcache_device_init(&dc->disk, block_size,
1136 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001137 if (ret)
1138 return ret;
1139
1140 set_capacity(dc->disk.disk,
1141 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1142
1143 dc->disk.disk->queue->backing_dev_info.ra_pages =
1144 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1145 q->backing_dev_info.ra_pages);
1146
1147 bch_cached_dev_request_init(dc);
1148 bch_cached_dev_writeback_init(dc);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001149 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001150}
1151
1152/* Cached device - bcache superblock */
1153
Kent Overstreetf59fce82013-05-15 00:11:26 -07001154static void register_bdev(struct cache_sb *sb, struct page *sb_page,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001155 struct block_device *bdev,
1156 struct cached_dev *dc)
1157{
1158 char name[BDEVNAME_SIZE];
1159 const char *err = "cannot allocate memory";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001160 struct cache_set *c;
1161
Kent Overstreetcafe5632013-03-23 16:11:31 -07001162 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001163 dc->bdev = bdev;
1164 dc->bdev->bd_holder = dc;
1165
Kent Overstreetf59fce82013-05-15 00:11:26 -07001166 bio_init(&dc->sb_bio);
1167 dc->sb_bio.bi_max_vecs = 1;
1168 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1169 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1170 get_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001171
Kent Overstreetf59fce82013-05-15 00:11:26 -07001172 if (cached_dev_init(dc, sb->block_size << 9))
1173 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001174
1175 err = "error creating kobject";
1176 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1177 "bcache"))
1178 goto err;
1179 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1180 goto err;
1181
Kent Overstreetf59fce82013-05-15 00:11:26 -07001182 pr_info("registered backing device %s", bdevname(bdev, name));
1183
Kent Overstreetcafe5632013-03-23 16:11:31 -07001184 list_add(&dc->list, &uncached_devices);
1185 list_for_each_entry(c, &bch_cache_sets, list)
1186 bch_cached_dev_attach(dc, c);
1187
1188 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1189 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1190 bch_cached_dev_run(dc);
1191
Kent Overstreetf59fce82013-05-15 00:11:26 -07001192 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001193err:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001194 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001195 bcache_device_stop(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001196}
1197
1198/* Flash only volumes */
1199
1200void bch_flash_dev_release(struct kobject *kobj)
1201{
1202 struct bcache_device *d = container_of(kobj, struct bcache_device,
1203 kobj);
1204 kfree(d);
1205}
1206
1207static void flash_dev_free(struct closure *cl)
1208{
1209 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
Slava Pestove5112202014-04-29 15:39:27 -07001210 mutex_lock(&bch_register_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001211 bcache_device_free(d);
Slava Pestove5112202014-04-29 15:39:27 -07001212 mutex_unlock(&bch_register_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001213 kobject_put(&d->kobj);
1214}
1215
1216static void flash_dev_flush(struct closure *cl)
1217{
1218 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1219
Slava Pestove5112202014-04-29 15:39:27 -07001220 mutex_lock(&bch_register_lock);
Kent Overstreetee668502013-02-01 07:29:41 -08001221 bcache_device_unlink(d);
Slava Pestove5112202014-04-29 15:39:27 -07001222 mutex_unlock(&bch_register_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001223 kobject_del(&d->kobj);
1224 continue_at(cl, flash_dev_free, system_wq);
1225}
1226
1227static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1228{
1229 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1230 GFP_KERNEL);
1231 if (!d)
1232 return -ENOMEM;
1233
1234 closure_init(&d->cl, NULL);
1235 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1236
1237 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1238
Kent Overstreet279afba2013-06-05 06:21:07 -07001239 if (bcache_device_init(d, block_bytes(c), u->sectors))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001240 goto err;
1241
1242 bcache_device_attach(d, c, u - c->uuids);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001243 bch_flash_dev_request_init(d);
1244 add_disk(d->disk);
1245
1246 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1247 goto err;
1248
1249 bcache_device_link(d, c, "volume");
1250
1251 return 0;
1252err:
1253 kobject_put(&d->kobj);
1254 return -ENOMEM;
1255}
1256
1257static int flash_devs_run(struct cache_set *c)
1258{
1259 int ret = 0;
1260 struct uuid_entry *u;
1261
1262 for (u = c->uuids;
1263 u < c->uuids + c->nr_uuids && !ret;
1264 u++)
1265 if (UUID_FLASH_ONLY(u))
1266 ret = flash_dev_run(c, u);
1267
1268 return ret;
1269}
1270
1271int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1272{
1273 struct uuid_entry *u;
1274
1275 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1276 return -EINTR;
1277
Slava Pestovbf0c55c2014-07-11 12:17:41 -07001278 if (!test_bit(CACHE_SET_RUNNING, &c->flags))
1279 return -EPERM;
1280
Kent Overstreetcafe5632013-03-23 16:11:31 -07001281 u = uuid_find_empty(c);
1282 if (!u) {
1283 pr_err("Can't create volume, no room for UUID");
1284 return -EINVAL;
1285 }
1286
1287 get_random_bytes(u->uuid, 16);
1288 memset(u->label, 0, 32);
1289 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1290
1291 SET_UUID_FLASH_ONLY(u, 1);
1292 u->sectors = size >> 9;
1293
1294 bch_uuid_write(c);
1295
1296 return flash_dev_run(c, u);
1297}
1298
1299/* Cache set */
1300
1301__printf(2, 3)
1302bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1303{
1304 va_list args;
1305
Kent Overstreet77c320e2013-07-11 19:42:51 -07001306 if (c->on_error != ON_ERROR_PANIC &&
1307 test_bit(CACHE_SET_STOPPING, &c->flags))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001308 return false;
1309
1310 /* XXX: we can be called from atomic context
1311 acquire_console_sem();
1312 */
1313
1314 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1315
1316 va_start(args, fmt);
1317 vprintk(fmt, args);
1318 va_end(args);
1319
1320 printk(", disabling caching\n");
1321
Kent Overstreet77c320e2013-07-11 19:42:51 -07001322 if (c->on_error == ON_ERROR_PANIC)
1323 panic("panic forced after error\n");
1324
Kent Overstreetcafe5632013-03-23 16:11:31 -07001325 bch_cache_set_unregister(c);
1326 return true;
1327}
1328
1329void bch_cache_set_release(struct kobject *kobj)
1330{
1331 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1332 kfree(c);
1333 module_put(THIS_MODULE);
1334}
1335
1336static void cache_set_free(struct closure *cl)
1337{
1338 struct cache_set *c = container_of(cl, struct cache_set, cl);
1339 struct cache *ca;
1340 unsigned i;
1341
1342 if (!IS_ERR_OR_NULL(c->debug))
1343 debugfs_remove(c->debug);
1344
1345 bch_open_buckets_free(c);
1346 bch_btree_cache_free(c);
1347 bch_journal_free(c);
1348
1349 for_each_cache(ca, c, i)
Slava Pestovc9a78332014-06-19 15:05:59 -07001350 if (ca) {
1351 ca->set = NULL;
1352 c->cache[ca->sb.nr_this_dev] = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001353 kobject_put(&ca->kobj);
Slava Pestovc9a78332014-06-19 15:05:59 -07001354 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001355
Kent Overstreet67539e82013-09-10 22:53:34 -07001356 bch_bset_sort_state_free(&c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001357 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001358
Nicholas Swensonda415a02014-01-09 16:03:04 -08001359 if (c->moving_gc_wq)
1360 destroy_workqueue(c->moving_gc_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001361 if (c->bio_split)
1362 bioset_free(c->bio_split);
Kent Overstreet57943512013-04-25 13:58:35 -07001363 if (c->fill_iter)
1364 mempool_destroy(c->fill_iter);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001365 if (c->bio_meta)
1366 mempool_destroy(c->bio_meta);
1367 if (c->search)
1368 mempool_destroy(c->search);
1369 kfree(c->devices);
1370
1371 mutex_lock(&bch_register_lock);
1372 list_del(&c->list);
1373 mutex_unlock(&bch_register_lock);
1374
1375 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1376 wake_up(&unregister_wait);
1377
1378 closure_debug_destroy(&c->cl);
1379 kobject_put(&c->kobj);
1380}
1381
1382static void cache_set_flush(struct closure *cl)
1383{
1384 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet79826c32013-07-10 18:31:58 -07001385 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001386 struct btree *b;
Kent Overstreet79826c32013-07-10 18:31:58 -07001387 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001388
1389 bch_cache_accounting_destroy(&c->accounting);
1390
1391 kobject_put(&c->internal);
1392 kobject_del(&c->kobj);
1393
Kent Overstreet72a44512013-10-24 17:19:26 -07001394 if (c->gc_thread)
1395 kthread_stop(c->gc_thread);
1396
Kent Overstreetcafe5632013-03-23 16:11:31 -07001397 if (!IS_ERR_OR_NULL(c->root))
1398 list_add(&c->root->list, &c->btree_cache);
1399
1400 /* Should skip this if we're unregistering because of an error */
Kent Overstreet2a285682014-03-04 16:42:42 -08001401 list_for_each_entry(b, &c->btree_cache, list) {
1402 mutex_lock(&b->write_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001403 if (btree_node_dirty(b))
Kent Overstreet2a285682014-03-04 16:42:42 -08001404 __bch_btree_node_write(b, NULL);
1405 mutex_unlock(&b->write_lock);
1406 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001407
Kent Overstreet79826c32013-07-10 18:31:58 -07001408 for_each_cache(ca, c, i)
1409 if (ca->alloc_thread)
1410 kthread_stop(ca->alloc_thread);
1411
Kent Overstreet5b1016e2014-03-19 17:49:37 -07001412 if (c->journal.cur) {
1413 cancel_delayed_work_sync(&c->journal.work);
1414 /* flush last journal entry if needed */
1415 c->journal.work.work.func(&c->journal.work.work);
1416 }
Kent Overstreetdabb4432014-02-19 19:48:26 -08001417
Kent Overstreetcafe5632013-03-23 16:11:31 -07001418 closure_return(cl);
1419}
1420
1421static void __cache_set_unregister(struct closure *cl)
1422{
1423 struct cache_set *c = container_of(cl, struct cache_set, caching);
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001424 struct cached_dev *dc;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001425 size_t i;
1426
1427 mutex_lock(&bch_register_lock);
1428
Kent Overstreetcafe5632013-03-23 16:11:31 -07001429 for (i = 0; i < c->nr_uuids; i++)
Kent Overstreet5caa52a2013-07-10 21:03:25 -07001430 if (c->devices[i]) {
1431 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1432 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1433 dc = container_of(c->devices[i],
1434 struct cached_dev, disk);
1435 bch_cached_dev_detach(dc);
1436 } else {
1437 bcache_device_stop(c->devices[i]);
1438 }
1439 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001440
1441 mutex_unlock(&bch_register_lock);
1442
1443 continue_at(cl, cache_set_flush, system_wq);
1444}
1445
1446void bch_cache_set_stop(struct cache_set *c)
1447{
1448 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1449 closure_queue(&c->caching);
1450}
1451
1452void bch_cache_set_unregister(struct cache_set *c)
1453{
1454 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1455 bch_cache_set_stop(c);
1456}
1457
1458#define alloc_bucket_pages(gfp, c) \
1459 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1460
1461struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1462{
1463 int iter_size;
1464 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1465 if (!c)
1466 return NULL;
1467
1468 __module_get(THIS_MODULE);
1469 closure_init(&c->cl, NULL);
1470 set_closure_fn(&c->cl, cache_set_free, system_wq);
1471
1472 closure_init(&c->caching, &c->cl);
1473 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1474
1475 /* Maybe create continue_at_noreturn() and use it here? */
1476 closure_set_stopped(&c->cl);
1477 closure_put(&c->cl);
1478
1479 kobject_init(&c->kobj, &bch_cache_set_ktype);
1480 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1481
1482 bch_cache_accounting_init(&c->accounting, &c->cl);
1483
1484 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1485 c->sb.block_size = sb->block_size;
1486 c->sb.bucket_size = sb->bucket_size;
1487 c->sb.nr_in_set = sb->nr_in_set;
1488 c->sb.last_mount = sb->last_mount;
1489 c->bucket_bits = ilog2(sb->bucket_size);
1490 c->block_bits = ilog2(sb->block_size);
1491 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1492
Kent Overstreetee811282013-12-17 23:49:49 -08001493 c->btree_pages = bucket_pages(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001494 if (c->btree_pages > BTREE_MAX_PAGES)
1495 c->btree_pages = max_t(int, c->btree_pages / 4,
1496 BTREE_MAX_PAGES);
1497
Kent Overstreetcb7a5832013-12-16 15:27:25 -08001498 sema_init(&c->sb_write_mutex, 1);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001499 mutex_init(&c->bucket_lock);
Kent Overstreet0a63b662014-03-17 17:15:53 -07001500 init_waitqueue_head(&c->btree_cache_wait);
Kent Overstreet35fcd842013-07-24 17:29:09 -07001501 init_waitqueue_head(&c->bucket_wait);
Kent Overstreetcb7a5832013-12-16 15:27:25 -08001502 sema_init(&c->uuid_write_mutex, 1);
Kent Overstreet65d22e92013-07-31 00:03:54 -07001503
Kent Overstreet65d22e92013-07-31 00:03:54 -07001504 spin_lock_init(&c->btree_gc_time.lock);
1505 spin_lock_init(&c->btree_split_time.lock);
1506 spin_lock_init(&c->btree_read_time.lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001507
Kent Overstreetcafe5632013-03-23 16:11:31 -07001508 bch_moving_init_cache_set(c);
1509
1510 INIT_LIST_HEAD(&c->list);
1511 INIT_LIST_HEAD(&c->cached_devs);
1512 INIT_LIST_HEAD(&c->btree_cache);
1513 INIT_LIST_HEAD(&c->btree_cache_freeable);
1514 INIT_LIST_HEAD(&c->btree_cache_freed);
1515 INIT_LIST_HEAD(&c->data_buckets);
1516
1517 c->search = mempool_create_slab_pool(32, bch_search_cache);
1518 if (!c->search)
1519 goto err;
1520
1521 iter_size = (sb->bucket_size / sb->block_size + 1) *
1522 sizeof(struct btree_iter_set);
1523
1524 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1525 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1526 sizeof(struct bbio) + sizeof(struct bio_vec) *
1527 bucket_pages(c))) ||
Kent Overstreet57943512013-04-25 13:58:35 -07001528 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001529 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001530 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
Nicholas Swensonda415a02014-01-09 16:03:04 -08001531 !(c->moving_gc_wq = create_workqueue("bcache_gc")) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001532 bch_journal_alloc(c) ||
1533 bch_btree_cache_alloc(c) ||
Kent Overstreet67539e82013-09-10 22:53:34 -07001534 bch_open_buckets_alloc(c) ||
1535 bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages)))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001536 goto err;
1537
Kent Overstreetcafe5632013-03-23 16:11:31 -07001538 c->congested_read_threshold_us = 2000;
1539 c->congested_write_threshold_us = 20000;
1540 c->error_limit = 8 << IO_ERROR_SHIFT;
1541
1542 return c;
1543err:
1544 bch_cache_set_unregister(c);
1545 return NULL;
1546}
1547
1548static void run_cache_set(struct cache_set *c)
1549{
1550 const char *err = "cannot allocate memory";
1551 struct cached_dev *dc, *t;
1552 struct cache *ca;
Kent Overstreetc18536a2013-07-24 17:44:17 -07001553 struct closure cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001554 unsigned i;
1555
Kent Overstreetc18536a2013-07-24 17:44:17 -07001556 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001557
1558 for_each_cache(ca, c, i)
1559 c->nbuckets += ca->sb.nbuckets;
1560
1561 if (CACHE_SYNC(&c->sb)) {
1562 LIST_HEAD(journal);
1563 struct bkey *k;
1564 struct jset *j;
1565
1566 err = "cannot allocate memory for journal";
Kent Overstreetc18536a2013-07-24 17:44:17 -07001567 if (bch_journal_read(c, &journal))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001568 goto err;
1569
1570 pr_debug("btree_journal_read() done");
1571
1572 err = "no journal entries found";
1573 if (list_empty(&journal))
1574 goto err;
1575
1576 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1577
1578 err = "IO error reading priorities";
1579 for_each_cache(ca, c, i)
1580 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1581
1582 /*
1583 * If prio_read() fails it'll call cache_set_error and we'll
1584 * tear everything down right away, but if we perhaps checked
1585 * sooner we could avoid journal replay.
1586 */
1587
1588 k = &j->btree_root;
1589
1590 err = "bad btree root";
Kent Overstreet65d45232013-12-20 17:22:05 -08001591 if (__bch_btree_ptr_invalid(c, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001592 goto err;
1593
1594 err = "error reading btree root";
Slava Pestov2452cc82014-07-12 00:22:53 -07001595 c->root = bch_btree_node_get(c, NULL, k, j->btree_level, true, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001596 if (IS_ERR_OR_NULL(c->root))
1597 goto err;
1598
1599 list_del_init(&c->root->list);
1600 rw_unlock(true, c->root);
1601
Kent Overstreetc18536a2013-07-24 17:44:17 -07001602 err = uuid_read(c, j, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001603 if (err)
1604 goto err;
1605
1606 err = "error in recovery";
Kent Overstreetc18536a2013-07-24 17:44:17 -07001607 if (bch_btree_check(c))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001608 goto err;
1609
1610 bch_journal_mark(c, &journal);
Kent Overstreet2531d9ee2014-03-17 16:55:55 -07001611 bch_initial_gc_finish(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001612 pr_debug("btree_check() done");
1613
1614 /*
1615 * bcache_journal_next() can't happen sooner, or
1616 * btree_gc_finish() will give spurious errors about last_gc >
1617 * gc_gen - this is a hack but oh well.
1618 */
1619 bch_journal_next(&c->journal);
1620
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001621 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001622 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001623 if (bch_cache_allocator_start(ca))
1624 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001625
1626 /*
1627 * First place it's safe to allocate: btree_check() and
1628 * btree_gc_finish() have to run before we have buckets to
1629 * allocate, and bch_bucket_alloc_set() might cause a journal
1630 * entry to be written so bcache_journal_next() has to be called
1631 * first.
1632 *
1633 * If the uuids were in the old format we have to rewrite them
1634 * before the next journal entry is written:
1635 */
1636 if (j->version < BCACHE_JSET_VERSION_UUID)
1637 __uuid_write(c);
1638
Kent Overstreetc18536a2013-07-24 17:44:17 -07001639 bch_journal_replay(c, &journal);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001640 } else {
1641 pr_notice("invalidating existing data");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001642
1643 for_each_cache(ca, c, i) {
1644 unsigned j;
1645
1646 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1647 2, SB_JOURNAL_BUCKETS);
1648
1649 for (j = 0; j < ca->sb.keys; j++)
1650 ca->sb.d[j] = ca->sb.first_bucket + j;
1651 }
1652
Kent Overstreet2531d9ee2014-03-17 16:55:55 -07001653 bch_initial_gc_finish(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001654
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001655 err = "error starting allocator thread";
Kent Overstreetcafe5632013-03-23 16:11:31 -07001656 for_each_cache(ca, c, i)
Kent Overstreet119ba0f2013-04-24 19:01:12 -07001657 if (bch_cache_allocator_start(ca))
1658 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001659
1660 mutex_lock(&c->bucket_lock);
1661 for_each_cache(ca, c, i)
1662 bch_prio_write(ca);
1663 mutex_unlock(&c->bucket_lock);
1664
Kent Overstreetcafe5632013-03-23 16:11:31 -07001665 err = "cannot allocate new UUID bucket";
1666 if (__uuid_write(c))
Kent Overstreet72a44512013-10-24 17:19:26 -07001667 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001668
1669 err = "cannot allocate new btree root";
Slava Pestov2452cc82014-07-12 00:22:53 -07001670 c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001671 if (IS_ERR_OR_NULL(c->root))
Kent Overstreet72a44512013-10-24 17:19:26 -07001672 goto err;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001673
Kent Overstreet2a285682014-03-04 16:42:42 -08001674 mutex_lock(&c->root->write_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001675 bkey_copy_key(&c->root->key, &MAX_KEY);
Kent Overstreetc18536a2013-07-24 17:44:17 -07001676 bch_btree_node_write(c->root, &cl);
Kent Overstreet2a285682014-03-04 16:42:42 -08001677 mutex_unlock(&c->root->write_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001678
1679 bch_btree_set_root(c->root);
1680 rw_unlock(true, c->root);
1681
1682 /*
1683 * We don't want to write the first journal entry until
1684 * everything is set up - fortunately journal entries won't be
1685 * written until the SET_CACHE_SYNC() here:
1686 */
1687 SET_CACHE_SYNC(&c->sb, true);
1688
1689 bch_journal_next(&c->journal);
Kent Overstreetc18536a2013-07-24 17:44:17 -07001690 bch_journal_meta(c, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001691 }
1692
Kent Overstreet72a44512013-10-24 17:19:26 -07001693 err = "error starting gc thread";
1694 if (bch_gc_thread_start(c))
1695 goto err;
1696
Kent Overstreetc18536a2013-07-24 17:44:17 -07001697 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001698 c->sb.last_mount = get_seconds();
1699 bcache_write_super(c);
1700
1701 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1702 bch_cached_dev_attach(dc, c);
1703
1704 flash_devs_run(c);
1705
Slava Pestovbf0c55c2014-07-11 12:17:41 -07001706 set_bit(CACHE_SET_RUNNING, &c->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001707 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001708err:
Kent Overstreetc18536a2013-07-24 17:44:17 -07001709 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001710 /* XXX: test this, it's broken */
Kees Cookc8694942013-09-10 21:41:34 -07001711 bch_cache_set_error(c, "%s", err);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001712}
1713
1714static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1715{
1716 return ca->sb.block_size == c->sb.block_size &&
Nicholas Swenson9eb8ebe2013-10-22 13:19:23 -07001717 ca->sb.bucket_size == c->sb.bucket_size &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001718 ca->sb.nr_in_set == c->sb.nr_in_set;
1719}
1720
1721static const char *register_cache_set(struct cache *ca)
1722{
1723 char buf[12];
1724 const char *err = "cannot allocate memory";
1725 struct cache_set *c;
1726
1727 list_for_each_entry(c, &bch_cache_sets, list)
1728 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1729 if (c->cache[ca->sb.nr_this_dev])
1730 return "duplicate cache set member";
1731
1732 if (!can_attach_cache(ca, c))
1733 return "cache sb does not match set";
1734
1735 if (!CACHE_SYNC(&ca->sb))
1736 SET_CACHE_SYNC(&c->sb, false);
1737
1738 goto found;
1739 }
1740
1741 c = bch_cache_set_alloc(&ca->sb);
1742 if (!c)
1743 return err;
1744
1745 err = "error creating kobject";
1746 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1747 kobject_add(&c->internal, &c->kobj, "internal"))
1748 goto err;
1749
1750 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1751 goto err;
1752
1753 bch_debug_init_cache_set(c);
1754
1755 list_add(&c->list, &bch_cache_sets);
1756found:
1757 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1758 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1759 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1760 goto err;
1761
1762 if (ca->sb.seq > c->sb.seq) {
1763 c->sb.version = ca->sb.version;
1764 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1765 c->sb.flags = ca->sb.flags;
1766 c->sb.seq = ca->sb.seq;
1767 pr_debug("set version = %llu", c->sb.version);
1768 }
1769
Kent Overstreetd83353b2014-06-11 19:44:49 -07001770 kobject_get(&ca->kobj);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001771 ca->set = c;
1772 ca->set->cache[ca->sb.nr_this_dev] = ca;
1773 c->cache_by_alloc[c->caches_loaded++] = ca;
1774
1775 if (c->caches_loaded == c->sb.nr_in_set)
1776 run_cache_set(c);
1777
1778 return NULL;
1779err:
1780 bch_cache_set_unregister(c);
1781 return err;
1782}
1783
1784/* Cache device */
1785
1786void bch_cache_release(struct kobject *kobj)
1787{
1788 struct cache *ca = container_of(kobj, struct cache, kobj);
Kent Overstreet78365412013-12-17 01:29:34 -08001789 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001790
Slava Pestovc9a78332014-06-19 15:05:59 -07001791 if (ca->set) {
1792 BUG_ON(ca->set->cache[ca->sb.nr_this_dev] != ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001793 ca->set->cache[ca->sb.nr_this_dev] = NULL;
Slava Pestovc9a78332014-06-19 15:05:59 -07001794 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001795
Kent Overstreetcafe5632013-03-23 16:11:31 -07001796 bio_split_pool_free(&ca->bio_split_hook);
1797
Kent Overstreetcafe5632013-03-23 16:11:31 -07001798 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1799 kfree(ca->prio_buckets);
1800 vfree(ca->buckets);
1801
1802 free_heap(&ca->heap);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001803 free_fifo(&ca->free_inc);
Kent Overstreet78365412013-12-17 01:29:34 -08001804
1805 for (i = 0; i < RESERVE_NR; i++)
1806 free_fifo(&ca->free[i]);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001807
1808 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1809 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1810
Kent Overstreet0781c872014-07-07 13:03:36 -07001811 if (!IS_ERR_OR_NULL(ca->bdev))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001812 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001813
1814 kfree(ca);
1815 module_put(THIS_MODULE);
1816}
1817
1818static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1819{
1820 size_t free;
1821 struct bucket *b;
1822
Kent Overstreetcafe5632013-03-23 16:11:31 -07001823 __module_get(THIS_MODULE);
1824 kobject_init(&ca->kobj, &bch_cache_ktype);
1825
Kent Overstreetcafe5632013-03-23 16:11:31 -07001826 bio_init(&ca->journal.bio);
1827 ca->journal.bio.bi_max_vecs = 8;
1828 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1829
Kent Overstreet78365412013-12-17 01:29:34 -08001830 free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001831
Kent Overstreet78365412013-12-17 01:29:34 -08001832 if (!init_fifo(&ca->free[RESERVE_BTREE], 8, GFP_KERNEL) ||
1833 !init_fifo(&ca->free[RESERVE_PRIO], prio_buckets(ca), GFP_KERNEL) ||
1834 !init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL) ||
1835 !init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001836 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001837 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
Kent Overstreetf59fce82013-05-15 00:11:26 -07001838 !(ca->buckets = vzalloc(sizeof(struct bucket) *
Kent Overstreetcafe5632013-03-23 16:11:31 -07001839 ca->sb.nbuckets)) ||
1840 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1841 2, GFP_KERNEL)) ||
1842 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07001843 bio_split_pool_init(&ca->bio_split_hook))
Kent Overstreetf59fce82013-05-15 00:11:26 -07001844 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001845
1846 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1847
Kent Overstreetcafe5632013-03-23 16:11:31 -07001848 for_each_bucket(b, ca)
1849 atomic_set(&b->pin, 0);
1850
Kent Overstreetcafe5632013-03-23 16:11:31 -07001851 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001852}
1853
Kent Overstreetf59fce82013-05-15 00:11:26 -07001854static void register_cache(struct cache_sb *sb, struct page *sb_page,
Slava Pestovc9a78332014-06-19 15:05:59 -07001855 struct block_device *bdev, struct cache *ca)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001856{
1857 char name[BDEVNAME_SIZE];
1858 const char *err = "cannot allocate memory";
1859
Kent Overstreetf59fce82013-05-15 00:11:26 -07001860 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001861 ca->bdev = bdev;
1862 ca->bdev->bd_holder = ca;
1863
Kent Overstreetf59fce82013-05-15 00:11:26 -07001864 bio_init(&ca->sb_bio);
1865 ca->sb_bio.bi_max_vecs = 1;
1866 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1867 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1868 get_page(sb_page);
1869
Kent Overstreetcafe5632013-03-23 16:11:31 -07001870 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1871 ca->discard = CACHE_DISCARD(&ca->sb);
1872
Kent Overstreetf59fce82013-05-15 00:11:26 -07001873 if (cache_alloc(sb, ca) != 0)
1874 goto err;
1875
Kent Overstreetcafe5632013-03-23 16:11:31 -07001876 err = "error creating kobject";
1877 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1878 goto err;
1879
Kent Overstreet4fa03402014-03-17 18:58:55 -07001880 mutex_lock(&bch_register_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001881 err = register_cache_set(ca);
Kent Overstreet4fa03402014-03-17 18:58:55 -07001882 mutex_unlock(&bch_register_lock);
1883
Kent Overstreetcafe5632013-03-23 16:11:31 -07001884 if (err)
1885 goto err;
1886
1887 pr_info("registered cache device %s", bdevname(bdev, name));
Kent Overstreetd83353b2014-06-11 19:44:49 -07001888out:
1889 kobject_put(&ca->kobj);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001890 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001891err:
Kent Overstreetf59fce82013-05-15 00:11:26 -07001892 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
Kent Overstreetd83353b2014-06-11 19:44:49 -07001893 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001894}
1895
1896/* Global interfaces/init */
1897
1898static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1899 const char *, size_t);
1900
1901kobj_attribute_write(register, register_bcache);
1902kobj_attribute_write(register_quiet, register_bcache);
1903
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001904static bool bch_is_open_backing(struct block_device *bdev) {
1905 struct cache_set *c, *tc;
1906 struct cached_dev *dc, *t;
1907
1908 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1909 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1910 if (dc->bdev == bdev)
1911 return true;
1912 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1913 if (dc->bdev == bdev)
1914 return true;
1915 return false;
1916}
1917
1918static bool bch_is_open_cache(struct block_device *bdev) {
1919 struct cache_set *c, *tc;
1920 struct cache *ca;
1921 unsigned i;
1922
1923 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1924 for_each_cache(ca, c, i)
1925 if (ca->bdev == bdev)
1926 return true;
1927 return false;
1928}
1929
1930static bool bch_is_open(struct block_device *bdev) {
1931 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1932}
1933
Kent Overstreetcafe5632013-03-23 16:11:31 -07001934static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1935 const char *buffer, size_t size)
1936{
1937 ssize_t ret = size;
1938 const char *err = "cannot allocate memory";
1939 char *path = NULL;
1940 struct cache_sb *sb = NULL;
1941 struct block_device *bdev = NULL;
1942 struct page *sb_page = NULL;
1943
1944 if (!try_module_get(THIS_MODULE))
1945 return -EBUSY;
1946
Kent Overstreetcafe5632013-03-23 16:11:31 -07001947 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1948 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1949 goto err;
1950
1951 err = "failed to open device";
1952 bdev = blkdev_get_by_path(strim(path),
1953 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1954 sb);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001955 if (IS_ERR(bdev)) {
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001956 if (bdev == ERR_PTR(-EBUSY)) {
1957 bdev = lookup_bdev(strim(path));
Jianjian Huo789d21d2014-07-13 09:08:59 -07001958 mutex_lock(&bch_register_lock);
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001959 if (!IS_ERR(bdev) && bch_is_open(bdev))
1960 err = "device already registered";
1961 else
1962 err = "device busy";
Jianjian Huo789d21d2014-07-13 09:08:59 -07001963 mutex_unlock(&bch_register_lock);
Gabriel de Perthuisa9dd53a2013-05-04 12:19:41 +02001964 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001965 goto err;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001966 }
1967
1968 err = "failed to set blocksize";
1969 if (set_blocksize(bdev, 4096))
1970 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001971
1972 err = read_super(sb, bdev, &sb_page);
1973 if (err)
1974 goto err_close;
1975
Kent Overstreet29033812013-04-11 15:14:35 -07001976 if (SB_IS_BDEV(sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001977 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001978 if (!dc)
1979 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001980
Kent Overstreet4fa03402014-03-17 18:58:55 -07001981 mutex_lock(&bch_register_lock);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001982 register_bdev(sb, sb_page, bdev, dc);
Kent Overstreet4fa03402014-03-17 18:58:55 -07001983 mutex_unlock(&bch_register_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001984 } else {
1985 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Kent Overstreetf59fce82013-05-15 00:11:26 -07001986 if (!ca)
1987 goto err_close;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001988
Kent Overstreetf59fce82013-05-15 00:11:26 -07001989 register_cache(sb, sb_page, bdev, ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001990 }
Kent Overstreetf59fce82013-05-15 00:11:26 -07001991out:
1992 if (sb_page)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001993 put_page(sb_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001994 kfree(sb);
1995 kfree(path);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001996 module_put(THIS_MODULE);
1997 return ret;
Kent Overstreetf59fce82013-05-15 00:11:26 -07001998
1999err_close:
2000 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2001err:
2002 if (attr != &ksysfs_register_quiet)
2003 pr_info("error opening %s: %s", path, err);
2004 ret = -EINVAL;
2005 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002006}
2007
2008static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
2009{
2010 if (code == SYS_DOWN ||
2011 code == SYS_HALT ||
2012 code == SYS_POWER_OFF) {
2013 DEFINE_WAIT(wait);
2014 unsigned long start = jiffies;
2015 bool stopped = false;
2016
2017 struct cache_set *c, *tc;
2018 struct cached_dev *dc, *tdc;
2019
2020 mutex_lock(&bch_register_lock);
2021
2022 if (list_empty(&bch_cache_sets) &&
2023 list_empty(&uncached_devices))
2024 goto out;
2025
2026 pr_info("Stopping all devices:");
2027
2028 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2029 bch_cache_set_stop(c);
2030
2031 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
2032 bcache_device_stop(&dc->disk);
2033
2034 /* What's a condition variable? */
2035 while (1) {
2036 long timeout = start + 2 * HZ - jiffies;
2037
2038 stopped = list_empty(&bch_cache_sets) &&
2039 list_empty(&uncached_devices);
2040
2041 if (timeout < 0 || stopped)
2042 break;
2043
2044 prepare_to_wait(&unregister_wait, &wait,
2045 TASK_UNINTERRUPTIBLE);
2046
2047 mutex_unlock(&bch_register_lock);
2048 schedule_timeout(timeout);
2049 mutex_lock(&bch_register_lock);
2050 }
2051
2052 finish_wait(&unregister_wait, &wait);
2053
2054 if (stopped)
2055 pr_info("All devices stopped");
2056 else
2057 pr_notice("Timeout waiting for devices to be closed");
2058out:
2059 mutex_unlock(&bch_register_lock);
2060 }
2061
2062 return NOTIFY_DONE;
2063}
2064
2065static struct notifier_block reboot = {
2066 .notifier_call = bcache_reboot,
2067 .priority = INT_MAX, /* before any real devices */
2068};
2069
2070static void bcache_exit(void)
2071{
2072 bch_debug_exit();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002073 bch_request_exit();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002074 if (bcache_kobj)
2075 kobject_put(bcache_kobj);
2076 if (bcache_wq)
2077 destroy_workqueue(bcache_wq);
Kent Overstreet5c41c8a2013-07-08 17:53:26 -07002078 if (bcache_major)
2079 unregister_blkdev(bcache_major, "bcache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07002080 unregister_reboot_notifier(&reboot);
2081}
2082
2083static int __init bcache_init(void)
2084{
2085 static const struct attribute *files[] = {
2086 &ksysfs_register.attr,
2087 &ksysfs_register_quiet.attr,
2088 NULL
2089 };
2090
2091 mutex_init(&bch_register_lock);
2092 init_waitqueue_head(&unregister_wait);
2093 register_reboot_notifier(&reboot);
Kent Overstreet07e86cc2013-03-25 11:46:43 -07002094 closure_debug_init();
Kent Overstreetcafe5632013-03-23 16:11:31 -07002095
2096 bcache_major = register_blkdev(0, "bcache");
2097 if (bcache_major < 0)
2098 return bcache_major;
2099
2100 if (!(bcache_wq = create_workqueue("bcache")) ||
2101 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2102 sysfs_create_files(bcache_kobj, files) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07002103 bch_request_init() ||
Kent Overstreetcafe5632013-03-23 16:11:31 -07002104 bch_debug_init(bcache_kobj))
2105 goto err;
2106
2107 return 0;
2108err:
2109 bcache_exit();
2110 return -ENOMEM;
2111}
2112
2113module_exit(bcache_exit);
2114module_init(bcache_init);