xref: /openbmc/linux/drivers/md/bcache/super.c (revision 5e2421ce)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bcache setup/teardown code, and some metadata io - read a superblock and
4  * figure out what to do with it.
5  *
6  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
7  * Copyright 2012 Google, Inc.
8  */
9 
10 #include "bcache.h"
11 #include "btree.h"
12 #include "debug.h"
13 #include "extents.h"
14 #include "request.h"
15 #include "writeback.h"
16 #include "features.h"
17 
18 #include <linux/blkdev.h>
19 #include <linux/pagemap.h>
20 #include <linux/debugfs.h>
21 #include <linux/genhd.h>
22 #include <linux/idr.h>
23 #include <linux/kthread.h>
24 #include <linux/workqueue.h>
25 #include <linux/module.h>
26 #include <linux/random.h>
27 #include <linux/reboot.h>
28 #include <linux/sysfs.h>
29 
30 unsigned int bch_cutoff_writeback;
31 unsigned int bch_cutoff_writeback_sync;
32 
33 static const char bcache_magic[] = {
34 	0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
35 	0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
36 };
37 
38 static const char invalid_uuid[] = {
39 	0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
40 	0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
41 };
42 
43 static struct kobject *bcache_kobj;
44 struct mutex bch_register_lock;
45 bool bcache_is_reboot;
46 LIST_HEAD(bch_cache_sets);
47 static LIST_HEAD(uncached_devices);
48 
49 static int bcache_major;
50 static DEFINE_IDA(bcache_device_idx);
51 static wait_queue_head_t unregister_wait;
52 struct workqueue_struct *bcache_wq;
53 struct workqueue_struct *bch_flush_wq;
54 struct workqueue_struct *bch_journal_wq;
55 
56 
57 #define BTREE_MAX_PAGES		(256 * 1024 / PAGE_SIZE)
58 /* limitation of partitions number on single bcache device */
59 #define BCACHE_MINORS		128
60 /* limitation of bcache devices number on single system */
61 #define BCACHE_DEVICE_IDX_MAX	((1U << MINORBITS)/BCACHE_MINORS)
62 
63 /* Superblock */
64 
65 static unsigned int get_bucket_size(struct cache_sb *sb, struct cache_sb_disk *s)
66 {
67 	unsigned int bucket_size = le16_to_cpu(s->bucket_size);
68 
69 	if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
70 		if (bch_has_feature_large_bucket(sb)) {
71 			unsigned int max, order;
72 
73 			max = sizeof(unsigned int) * BITS_PER_BYTE - 1;
74 			order = le16_to_cpu(s->bucket_size);
75 			/*
76 			 * bcache tool will make sure the overflow won't
77 			 * happen, an error message here is enough.
78 			 */
79 			if (order > max)
80 				pr_err("Bucket size (1 << %u) overflows\n",
81 					order);
82 			bucket_size = 1 << order;
83 		} else if (bch_has_feature_obso_large_bucket(sb)) {
84 			bucket_size +=
85 				le16_to_cpu(s->obso_bucket_size_hi) << 16;
86 		}
87 	}
88 
89 	return bucket_size;
90 }
91 
92 static const char *read_super_common(struct cache_sb *sb,  struct block_device *bdev,
93 				     struct cache_sb_disk *s)
94 {
95 	const char *err;
96 	unsigned int i;
97 
98 	sb->first_bucket= le16_to_cpu(s->first_bucket);
99 	sb->nbuckets	= le64_to_cpu(s->nbuckets);
100 	sb->bucket_size	= get_bucket_size(sb, s);
101 
102 	sb->nr_in_set	= le16_to_cpu(s->nr_in_set);
103 	sb->nr_this_dev	= le16_to_cpu(s->nr_this_dev);
104 
105 	err = "Too many journal buckets";
106 	if (sb->keys > SB_JOURNAL_BUCKETS)
107 		goto err;
108 
109 	err = "Too many buckets";
110 	if (sb->nbuckets > LONG_MAX)
111 		goto err;
112 
113 	err = "Not enough buckets";
114 	if (sb->nbuckets < 1 << 7)
115 		goto err;
116 
117 	err = "Bad block size (not power of 2)";
118 	if (!is_power_of_2(sb->block_size))
119 		goto err;
120 
121 	err = "Bad block size (larger than page size)";
122 	if (sb->block_size > PAGE_SECTORS)
123 		goto err;
124 
125 	err = "Bad bucket size (not power of 2)";
126 	if (!is_power_of_2(sb->bucket_size))
127 		goto err;
128 
129 	err = "Bad bucket size (smaller than page size)";
130 	if (sb->bucket_size < PAGE_SECTORS)
131 		goto err;
132 
133 	err = "Invalid superblock: device too small";
134 	if (get_capacity(bdev->bd_disk) <
135 	    sb->bucket_size * sb->nbuckets)
136 		goto err;
137 
138 	err = "Bad UUID";
139 	if (bch_is_zero(sb->set_uuid, 16))
140 		goto err;
141 
142 	err = "Bad cache device number in set";
143 	if (!sb->nr_in_set ||
144 	    sb->nr_in_set <= sb->nr_this_dev ||
145 	    sb->nr_in_set > MAX_CACHES_PER_SET)
146 		goto err;
147 
148 	err = "Journal buckets not sequential";
149 	for (i = 0; i < sb->keys; i++)
150 		if (sb->d[i] != sb->first_bucket + i)
151 			goto err;
152 
153 	err = "Too many journal buckets";
154 	if (sb->first_bucket + sb->keys > sb->nbuckets)
155 		goto err;
156 
157 	err = "Invalid superblock: first bucket comes before end of super";
158 	if (sb->first_bucket * sb->bucket_size < 16)
159 		goto err;
160 
161 	err = NULL;
162 err:
163 	return err;
164 }
165 
166 
167 static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
168 			      struct cache_sb_disk **res)
169 {
170 	const char *err;
171 	struct cache_sb_disk *s;
172 	struct page *page;
173 	unsigned int i;
174 
175 	page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
176 				   SB_OFFSET >> PAGE_SHIFT, GFP_KERNEL);
177 	if (IS_ERR(page))
178 		return "IO error";
179 	s = page_address(page) + offset_in_page(SB_OFFSET);
180 
181 	sb->offset		= le64_to_cpu(s->offset);
182 	sb->version		= le64_to_cpu(s->version);
183 
184 	memcpy(sb->magic,	s->magic, 16);
185 	memcpy(sb->uuid,	s->uuid, 16);
186 	memcpy(sb->set_uuid,	s->set_uuid, 16);
187 	memcpy(sb->label,	s->label, SB_LABEL_SIZE);
188 
189 	sb->flags		= le64_to_cpu(s->flags);
190 	sb->seq			= le64_to_cpu(s->seq);
191 	sb->last_mount		= le32_to_cpu(s->last_mount);
192 	sb->keys		= le16_to_cpu(s->keys);
193 
194 	for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
195 		sb->d[i] = le64_to_cpu(s->d[i]);
196 
197 	pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u\n",
198 		 sb->version, sb->flags, sb->seq, sb->keys);
199 
200 	err = "Not a bcache superblock (bad offset)";
201 	if (sb->offset != SB_SECTOR)
202 		goto err;
203 
204 	err = "Not a bcache superblock (bad magic)";
205 	if (memcmp(sb->magic, bcache_magic, 16))
206 		goto err;
207 
208 	err = "Bad checksum";
209 	if (s->csum != csum_set(s))
210 		goto err;
211 
212 	err = "Bad UUID";
213 	if (bch_is_zero(sb->uuid, 16))
214 		goto err;
215 
216 	sb->block_size	= le16_to_cpu(s->block_size);
217 
218 	err = "Superblock block size smaller than device block size";
219 	if (sb->block_size << 9 < bdev_logical_block_size(bdev))
220 		goto err;
221 
222 	switch (sb->version) {
223 	case BCACHE_SB_VERSION_BDEV:
224 		sb->data_offset	= BDEV_DATA_START_DEFAULT;
225 		break;
226 	case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
227 	case BCACHE_SB_VERSION_BDEV_WITH_FEATURES:
228 		sb->data_offset	= le64_to_cpu(s->data_offset);
229 
230 		err = "Bad data offset";
231 		if (sb->data_offset < BDEV_DATA_START_DEFAULT)
232 			goto err;
233 
234 		break;
235 	case BCACHE_SB_VERSION_CDEV:
236 	case BCACHE_SB_VERSION_CDEV_WITH_UUID:
237 		err = read_super_common(sb, bdev, s);
238 		if (err)
239 			goto err;
240 		break;
241 	case BCACHE_SB_VERSION_CDEV_WITH_FEATURES:
242 		/*
243 		 * Feature bits are needed in read_super_common(),
244 		 * convert them firstly.
245 		 */
246 		sb->feature_compat = le64_to_cpu(s->feature_compat);
247 		sb->feature_incompat = le64_to_cpu(s->feature_incompat);
248 		sb->feature_ro_compat = le64_to_cpu(s->feature_ro_compat);
249 
250 		/* Check incompatible features */
251 		err = "Unsupported compatible feature found";
252 		if (bch_has_unknown_compat_features(sb))
253 			goto err;
254 
255 		err = "Unsupported read-only compatible feature found";
256 		if (bch_has_unknown_ro_compat_features(sb))
257 			goto err;
258 
259 		err = "Unsupported incompatible feature found";
260 		if (bch_has_unknown_incompat_features(sb))
261 			goto err;
262 
263 		err = read_super_common(sb, bdev, s);
264 		if (err)
265 			goto err;
266 		break;
267 	default:
268 		err = "Unsupported superblock version";
269 		goto err;
270 	}
271 
272 	sb->last_mount = (u32)ktime_get_real_seconds();
273 	*res = s;
274 	return NULL;
275 err:
276 	put_page(page);
277 	return err;
278 }
279 
280 static void write_bdev_super_endio(struct bio *bio)
281 {
282 	struct cached_dev *dc = bio->bi_private;
283 
284 	if (bio->bi_status)
285 		bch_count_backing_io_errors(dc, bio);
286 
287 	closure_put(&dc->sb_write);
288 }
289 
290 static void __write_super(struct cache_sb *sb, struct cache_sb_disk *out,
291 		struct bio *bio)
292 {
293 	unsigned int i;
294 
295 	bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META;
296 	bio->bi_iter.bi_sector	= SB_SECTOR;
297 	__bio_add_page(bio, virt_to_page(out), SB_SIZE,
298 			offset_in_page(out));
299 
300 	out->offset		= cpu_to_le64(sb->offset);
301 
302 	memcpy(out->uuid,	sb->uuid, 16);
303 	memcpy(out->set_uuid,	sb->set_uuid, 16);
304 	memcpy(out->label,	sb->label, SB_LABEL_SIZE);
305 
306 	out->flags		= cpu_to_le64(sb->flags);
307 	out->seq		= cpu_to_le64(sb->seq);
308 
309 	out->last_mount		= cpu_to_le32(sb->last_mount);
310 	out->first_bucket	= cpu_to_le16(sb->first_bucket);
311 	out->keys		= cpu_to_le16(sb->keys);
312 
313 	for (i = 0; i < sb->keys; i++)
314 		out->d[i] = cpu_to_le64(sb->d[i]);
315 
316 	if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
317 		out->feature_compat    = cpu_to_le64(sb->feature_compat);
318 		out->feature_incompat  = cpu_to_le64(sb->feature_incompat);
319 		out->feature_ro_compat = cpu_to_le64(sb->feature_ro_compat);
320 	}
321 
322 	out->version		= cpu_to_le64(sb->version);
323 	out->csum = csum_set(out);
324 
325 	pr_debug("ver %llu, flags %llu, seq %llu\n",
326 		 sb->version, sb->flags, sb->seq);
327 
328 	submit_bio(bio);
329 }
330 
331 static void bch_write_bdev_super_unlock(struct closure *cl)
332 {
333 	struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
334 
335 	up(&dc->sb_write_mutex);
336 }
337 
338 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
339 {
340 	struct closure *cl = &dc->sb_write;
341 	struct bio *bio = &dc->sb_bio;
342 
343 	down(&dc->sb_write_mutex);
344 	closure_init(cl, parent);
345 
346 	bio_init(bio, dc->sb_bv, 1);
347 	bio_set_dev(bio, dc->bdev);
348 	bio->bi_end_io	= write_bdev_super_endio;
349 	bio->bi_private = dc;
350 
351 	closure_get(cl);
352 	/* I/O request sent to backing device */
353 	__write_super(&dc->sb, dc->sb_disk, bio);
354 
355 	closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
356 }
357 
358 static void write_super_endio(struct bio *bio)
359 {
360 	struct cache *ca = bio->bi_private;
361 
362 	/* is_read = 0 */
363 	bch_count_io_errors(ca, bio->bi_status, 0,
364 			    "writing superblock");
365 	closure_put(&ca->set->sb_write);
366 }
367 
368 static void bcache_write_super_unlock(struct closure *cl)
369 {
370 	struct cache_set *c = container_of(cl, struct cache_set, sb_write);
371 
372 	up(&c->sb_write_mutex);
373 }
374 
375 void bcache_write_super(struct cache_set *c)
376 {
377 	struct closure *cl = &c->sb_write;
378 	struct cache *ca = c->cache;
379 	struct bio *bio = &ca->sb_bio;
380 	unsigned int version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
381 
382 	down(&c->sb_write_mutex);
383 	closure_init(cl, &c->cl);
384 
385 	ca->sb.seq++;
386 
387 	if (ca->sb.version < version)
388 		ca->sb.version = version;
389 
390 	bio_init(bio, ca->sb_bv, 1);
391 	bio_set_dev(bio, ca->bdev);
392 	bio->bi_end_io	= write_super_endio;
393 	bio->bi_private = ca;
394 
395 	closure_get(cl);
396 	__write_super(&ca->sb, ca->sb_disk, bio);
397 
398 	closure_return_with_destructor(cl, bcache_write_super_unlock);
399 }
400 
401 /* UUID io */
402 
403 static void uuid_endio(struct bio *bio)
404 {
405 	struct closure *cl = bio->bi_private;
406 	struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
407 
408 	cache_set_err_on(bio->bi_status, c, "accessing uuids");
409 	bch_bbio_free(bio, c);
410 	closure_put(cl);
411 }
412 
413 static void uuid_io_unlock(struct closure *cl)
414 {
415 	struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
416 
417 	up(&c->uuid_write_mutex);
418 }
419 
420 static void uuid_io(struct cache_set *c, int op, unsigned long op_flags,
421 		    struct bkey *k, struct closure *parent)
422 {
423 	struct closure *cl = &c->uuid_write;
424 	struct uuid_entry *u;
425 	unsigned int i;
426 	char buf[80];
427 
428 	BUG_ON(!parent);
429 	down(&c->uuid_write_mutex);
430 	closure_init(cl, parent);
431 
432 	for (i = 0; i < KEY_PTRS(k); i++) {
433 		struct bio *bio = bch_bbio_alloc(c);
434 
435 		bio->bi_opf = REQ_SYNC | REQ_META | op_flags;
436 		bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
437 
438 		bio->bi_end_io	= uuid_endio;
439 		bio->bi_private = cl;
440 		bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
441 		bch_bio_map(bio, c->uuids);
442 
443 		bch_submit_bbio(bio, c, k, i);
444 
445 		if (op != REQ_OP_WRITE)
446 			break;
447 	}
448 
449 	bch_extent_to_text(buf, sizeof(buf), k);
450 	pr_debug("%s UUIDs at %s\n", op == REQ_OP_WRITE ? "wrote" : "read", buf);
451 
452 	for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
453 		if (!bch_is_zero(u->uuid, 16))
454 			pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u\n",
455 				 u - c->uuids, u->uuid, u->label,
456 				 u->first_reg, u->last_reg, u->invalidated);
457 
458 	closure_return_with_destructor(cl, uuid_io_unlock);
459 }
460 
461 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
462 {
463 	struct bkey *k = &j->uuid_bucket;
464 
465 	if (__bch_btree_ptr_invalid(c, k))
466 		return "bad uuid pointer";
467 
468 	bkey_copy(&c->uuid_bucket, k);
469 	uuid_io(c, REQ_OP_READ, 0, k, cl);
470 
471 	if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
472 		struct uuid_entry_v0	*u0 = (void *) c->uuids;
473 		struct uuid_entry	*u1 = (void *) c->uuids;
474 		int i;
475 
476 		closure_sync(cl);
477 
478 		/*
479 		 * Since the new uuid entry is bigger than the old, we have to
480 		 * convert starting at the highest memory address and work down
481 		 * in order to do it in place
482 		 */
483 
484 		for (i = c->nr_uuids - 1;
485 		     i >= 0;
486 		     --i) {
487 			memcpy(u1[i].uuid,	u0[i].uuid, 16);
488 			memcpy(u1[i].label,	u0[i].label, 32);
489 
490 			u1[i].first_reg		= u0[i].first_reg;
491 			u1[i].last_reg		= u0[i].last_reg;
492 			u1[i].invalidated	= u0[i].invalidated;
493 
494 			u1[i].flags	= 0;
495 			u1[i].sectors	= 0;
496 		}
497 	}
498 
499 	return NULL;
500 }
501 
502 static int __uuid_write(struct cache_set *c)
503 {
504 	BKEY_PADDED(key) k;
505 	struct closure cl;
506 	struct cache *ca = c->cache;
507 	unsigned int size;
508 
509 	closure_init_stack(&cl);
510 	lockdep_assert_held(&bch_register_lock);
511 
512 	if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, true))
513 		return 1;
514 
515 	size =  meta_bucket_pages(&ca->sb) * PAGE_SECTORS;
516 	SET_KEY_SIZE(&k.key, size);
517 	uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl);
518 	closure_sync(&cl);
519 
520 	/* Only one bucket used for uuid write */
521 	atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written);
522 
523 	bkey_copy(&c->uuid_bucket, &k.key);
524 	bkey_put(c, &k.key);
525 	return 0;
526 }
527 
528 int bch_uuid_write(struct cache_set *c)
529 {
530 	int ret = __uuid_write(c);
531 
532 	if (!ret)
533 		bch_journal_meta(c, NULL);
534 
535 	return ret;
536 }
537 
538 static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
539 {
540 	struct uuid_entry *u;
541 
542 	for (u = c->uuids;
543 	     u < c->uuids + c->nr_uuids; u++)
544 		if (!memcmp(u->uuid, uuid, 16))
545 			return u;
546 
547 	return NULL;
548 }
549 
550 static struct uuid_entry *uuid_find_empty(struct cache_set *c)
551 {
552 	static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
553 
554 	return uuid_find(c, zero_uuid);
555 }
556 
557 /*
558  * Bucket priorities/gens:
559  *
560  * For each bucket, we store on disk its
561  *   8 bit gen
562  *  16 bit priority
563  *
564  * See alloc.c for an explanation of the gen. The priority is used to implement
565  * lru (and in the future other) cache replacement policies; for most purposes
566  * it's just an opaque integer.
567  *
568  * The gens and the priorities don't have a whole lot to do with each other, and
569  * it's actually the gens that must be written out at specific times - it's no
570  * big deal if the priorities don't get written, if we lose them we just reuse
571  * buckets in suboptimal order.
572  *
573  * On disk they're stored in a packed array, and in as many buckets are required
574  * to fit them all. The buckets we use to store them form a list; the journal
575  * header points to the first bucket, the first bucket points to the second
576  * bucket, et cetera.
577  *
578  * This code is used by the allocation code; periodically (whenever it runs out
579  * of buckets to allocate from) the allocation code will invalidate some
580  * buckets, but it can't use those buckets until their new gens are safely on
581  * disk.
582  */
583 
584 static void prio_endio(struct bio *bio)
585 {
586 	struct cache *ca = bio->bi_private;
587 
588 	cache_set_err_on(bio->bi_status, ca->set, "accessing priorities");
589 	bch_bbio_free(bio, ca->set);
590 	closure_put(&ca->prio);
591 }
592 
593 static void prio_io(struct cache *ca, uint64_t bucket, int op,
594 		    unsigned long op_flags)
595 {
596 	struct closure *cl = &ca->prio;
597 	struct bio *bio = bch_bbio_alloc(ca->set);
598 
599 	closure_init_stack(cl);
600 
601 	bio->bi_iter.bi_sector	= bucket * ca->sb.bucket_size;
602 	bio_set_dev(bio, ca->bdev);
603 	bio->bi_iter.bi_size	= meta_bucket_bytes(&ca->sb);
604 
605 	bio->bi_end_io	= prio_endio;
606 	bio->bi_private = ca;
607 	bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
608 	bch_bio_map(bio, ca->disk_buckets);
609 
610 	closure_bio_submit(ca->set, bio, &ca->prio);
611 	closure_sync(cl);
612 }
613 
614 int bch_prio_write(struct cache *ca, bool wait)
615 {
616 	int i;
617 	struct bucket *b;
618 	struct closure cl;
619 
620 	pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu\n",
621 		 fifo_used(&ca->free[RESERVE_PRIO]),
622 		 fifo_used(&ca->free[RESERVE_NONE]),
623 		 fifo_used(&ca->free_inc));
624 
625 	/*
626 	 * Pre-check if there are enough free buckets. In the non-blocking
627 	 * scenario it's better to fail early rather than starting to allocate
628 	 * buckets and do a cleanup later in case of failure.
629 	 */
630 	if (!wait) {
631 		size_t avail = fifo_used(&ca->free[RESERVE_PRIO]) +
632 			       fifo_used(&ca->free[RESERVE_NONE]);
633 		if (prio_buckets(ca) > avail)
634 			return -ENOMEM;
635 	}
636 
637 	closure_init_stack(&cl);
638 
639 	lockdep_assert_held(&ca->set->bucket_lock);
640 
641 	ca->disk_buckets->seq++;
642 
643 	atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
644 			&ca->meta_sectors_written);
645 
646 	for (i = prio_buckets(ca) - 1; i >= 0; --i) {
647 		long bucket;
648 		struct prio_set *p = ca->disk_buckets;
649 		struct bucket_disk *d = p->data;
650 		struct bucket_disk *end = d + prios_per_bucket(ca);
651 
652 		for (b = ca->buckets + i * prios_per_bucket(ca);
653 		     b < ca->buckets + ca->sb.nbuckets && d < end;
654 		     b++, d++) {
655 			d->prio = cpu_to_le16(b->prio);
656 			d->gen = b->gen;
657 		}
658 
659 		p->next_bucket	= ca->prio_buckets[i + 1];
660 		p->magic	= pset_magic(&ca->sb);
661 		p->csum		= bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8);
662 
663 		bucket = bch_bucket_alloc(ca, RESERVE_PRIO, wait);
664 		BUG_ON(bucket == -1);
665 
666 		mutex_unlock(&ca->set->bucket_lock);
667 		prio_io(ca, bucket, REQ_OP_WRITE, 0);
668 		mutex_lock(&ca->set->bucket_lock);
669 
670 		ca->prio_buckets[i] = bucket;
671 		atomic_dec_bug(&ca->buckets[bucket].pin);
672 	}
673 
674 	mutex_unlock(&ca->set->bucket_lock);
675 
676 	bch_journal_meta(ca->set, &cl);
677 	closure_sync(&cl);
678 
679 	mutex_lock(&ca->set->bucket_lock);
680 
681 	/*
682 	 * Don't want the old priorities to get garbage collected until after we
683 	 * finish writing the new ones, and they're journalled
684 	 */
685 	for (i = 0; i < prio_buckets(ca); i++) {
686 		if (ca->prio_last_buckets[i])
687 			__bch_bucket_free(ca,
688 				&ca->buckets[ca->prio_last_buckets[i]]);
689 
690 		ca->prio_last_buckets[i] = ca->prio_buckets[i];
691 	}
692 	return 0;
693 }
694 
695 static int prio_read(struct cache *ca, uint64_t bucket)
696 {
697 	struct prio_set *p = ca->disk_buckets;
698 	struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
699 	struct bucket *b;
700 	unsigned int bucket_nr = 0;
701 	int ret = -EIO;
702 
703 	for (b = ca->buckets;
704 	     b < ca->buckets + ca->sb.nbuckets;
705 	     b++, d++) {
706 		if (d == end) {
707 			ca->prio_buckets[bucket_nr] = bucket;
708 			ca->prio_last_buckets[bucket_nr] = bucket;
709 			bucket_nr++;
710 
711 			prio_io(ca, bucket, REQ_OP_READ, 0);
712 
713 			if (p->csum !=
714 			    bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8)) {
715 				pr_warn("bad csum reading priorities\n");
716 				goto out;
717 			}
718 
719 			if (p->magic != pset_magic(&ca->sb)) {
720 				pr_warn("bad magic reading priorities\n");
721 				goto out;
722 			}
723 
724 			bucket = p->next_bucket;
725 			d = p->data;
726 		}
727 
728 		b->prio = le16_to_cpu(d->prio);
729 		b->gen = b->last_gc = d->gen;
730 	}
731 
732 	ret = 0;
733 out:
734 	return ret;
735 }
736 
737 /* Bcache device */
738 
739 static int open_dev(struct block_device *b, fmode_t mode)
740 {
741 	struct bcache_device *d = b->bd_disk->private_data;
742 
743 	if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
744 		return -ENXIO;
745 
746 	closure_get(&d->cl);
747 	return 0;
748 }
749 
750 static void release_dev(struct gendisk *b, fmode_t mode)
751 {
752 	struct bcache_device *d = b->private_data;
753 
754 	closure_put(&d->cl);
755 }
756 
757 static int ioctl_dev(struct block_device *b, fmode_t mode,
758 		     unsigned int cmd, unsigned long arg)
759 {
760 	struct bcache_device *d = b->bd_disk->private_data;
761 
762 	return d->ioctl(d, mode, cmd, arg);
763 }
764 
765 static const struct block_device_operations bcache_cached_ops = {
766 	.submit_bio	= cached_dev_submit_bio,
767 	.open		= open_dev,
768 	.release	= release_dev,
769 	.ioctl		= ioctl_dev,
770 	.owner		= THIS_MODULE,
771 };
772 
773 static const struct block_device_operations bcache_flash_ops = {
774 	.submit_bio	= flash_dev_submit_bio,
775 	.open		= open_dev,
776 	.release	= release_dev,
777 	.ioctl		= ioctl_dev,
778 	.owner		= THIS_MODULE,
779 };
780 
781 void bcache_device_stop(struct bcache_device *d)
782 {
783 	if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
784 		/*
785 		 * closure_fn set to
786 		 * - cached device: cached_dev_flush()
787 		 * - flash dev: flash_dev_flush()
788 		 */
789 		closure_queue(&d->cl);
790 }
791 
792 static void bcache_device_unlink(struct bcache_device *d)
793 {
794 	lockdep_assert_held(&bch_register_lock);
795 
796 	if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
797 		struct cache *ca = d->c->cache;
798 
799 		sysfs_remove_link(&d->c->kobj, d->name);
800 		sysfs_remove_link(&d->kobj, "cache");
801 
802 		bd_unlink_disk_holder(ca->bdev, d->disk);
803 	}
804 }
805 
806 static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
807 			       const char *name)
808 {
809 	struct cache *ca = c->cache;
810 	int ret;
811 
812 	bd_link_disk_holder(ca->bdev, d->disk);
813 
814 	snprintf(d->name, BCACHEDEVNAME_SIZE,
815 		 "%s%u", name, d->id);
816 
817 	ret = sysfs_create_link(&d->kobj, &c->kobj, "cache");
818 	if (ret < 0)
819 		pr_err("Couldn't create device -> cache set symlink\n");
820 
821 	ret = sysfs_create_link(&c->kobj, &d->kobj, d->name);
822 	if (ret < 0)
823 		pr_err("Couldn't create cache set -> device symlink\n");
824 
825 	clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags);
826 }
827 
828 static void bcache_device_detach(struct bcache_device *d)
829 {
830 	lockdep_assert_held(&bch_register_lock);
831 
832 	atomic_dec(&d->c->attached_dev_nr);
833 
834 	if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
835 		struct uuid_entry *u = d->c->uuids + d->id;
836 
837 		SET_UUID_FLASH_ONLY(u, 0);
838 		memcpy(u->uuid, invalid_uuid, 16);
839 		u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
840 		bch_uuid_write(d->c);
841 	}
842 
843 	bcache_device_unlink(d);
844 
845 	d->c->devices[d->id] = NULL;
846 	closure_put(&d->c->caching);
847 	d->c = NULL;
848 }
849 
850 static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
851 				 unsigned int id)
852 {
853 	d->id = id;
854 	d->c = c;
855 	c->devices[id] = d;
856 
857 	if (id >= c->devices_max_used)
858 		c->devices_max_used = id + 1;
859 
860 	closure_get(&c->caching);
861 }
862 
863 static inline int first_minor_to_idx(int first_minor)
864 {
865 	return (first_minor/BCACHE_MINORS);
866 }
867 
868 static inline int idx_to_first_minor(int idx)
869 {
870 	return (idx * BCACHE_MINORS);
871 }
872 
873 static void bcache_device_free(struct bcache_device *d)
874 {
875 	struct gendisk *disk = d->disk;
876 
877 	lockdep_assert_held(&bch_register_lock);
878 
879 	if (disk)
880 		pr_info("%s stopped\n", disk->disk_name);
881 	else
882 		pr_err("bcache device (NULL gendisk) stopped\n");
883 
884 	if (d->c)
885 		bcache_device_detach(d);
886 
887 	if (disk) {
888 		ida_simple_remove(&bcache_device_idx,
889 				  first_minor_to_idx(disk->first_minor));
890 		blk_cleanup_disk(disk);
891 	}
892 
893 	bioset_exit(&d->bio_split);
894 	kvfree(d->full_dirty_stripes);
895 	kvfree(d->stripe_sectors_dirty);
896 
897 	closure_debug_destroy(&d->cl);
898 }
899 
900 static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
901 		sector_t sectors, struct block_device *cached_bdev,
902 		const struct block_device_operations *ops)
903 {
904 	struct request_queue *q;
905 	const size_t max_stripes = min_t(size_t, INT_MAX,
906 					 SIZE_MAX / sizeof(atomic_t));
907 	uint64_t n;
908 	int idx;
909 
910 	if (!d->stripe_size)
911 		d->stripe_size = 1 << 31;
912 
913 	n = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
914 	if (!n || n > max_stripes) {
915 		pr_err("nr_stripes too large or invalid: %llu (start sector beyond end of disk?)\n",
916 			n);
917 		return -ENOMEM;
918 	}
919 	d->nr_stripes = n;
920 
921 	n = d->nr_stripes * sizeof(atomic_t);
922 	d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL);
923 	if (!d->stripe_sectors_dirty)
924 		return -ENOMEM;
925 
926 	n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
927 	d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL);
928 	if (!d->full_dirty_stripes)
929 		goto out_free_stripe_sectors_dirty;
930 
931 	idx = ida_simple_get(&bcache_device_idx, 0,
932 				BCACHE_DEVICE_IDX_MAX, GFP_KERNEL);
933 	if (idx < 0)
934 		goto out_free_full_dirty_stripes;
935 
936 	if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio),
937 			BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER))
938 		goto out_ida_remove;
939 
940 	d->disk = blk_alloc_disk(NUMA_NO_NODE);
941 	if (!d->disk)
942 		goto out_bioset_exit;
943 
944 	set_capacity(d->disk, sectors);
945 	snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx);
946 
947 	d->disk->major		= bcache_major;
948 	d->disk->first_minor	= idx_to_first_minor(idx);
949 	d->disk->minors		= BCACHE_MINORS;
950 	d->disk->fops		= ops;
951 	d->disk->private_data	= d;
952 
953 	q = d->disk->queue;
954 	q->limits.max_hw_sectors	= UINT_MAX;
955 	q->limits.max_sectors		= UINT_MAX;
956 	q->limits.max_segment_size	= UINT_MAX;
957 	q->limits.max_segments		= BIO_MAX_VECS;
958 	blk_queue_max_discard_sectors(q, UINT_MAX);
959 	q->limits.discard_granularity	= 512;
960 	q->limits.io_min		= block_size;
961 	q->limits.logical_block_size	= block_size;
962 	q->limits.physical_block_size	= block_size;
963 
964 	if (q->limits.logical_block_size > PAGE_SIZE && cached_bdev) {
965 		/*
966 		 * This should only happen with BCACHE_SB_VERSION_BDEV.
967 		 * Block/page size is checked for BCACHE_SB_VERSION_CDEV.
968 		 */
969 		pr_info("%s: sb/logical block size (%u) greater than page size (%lu) falling back to device logical block size (%u)\n",
970 			d->disk->disk_name, q->limits.logical_block_size,
971 			PAGE_SIZE, bdev_logical_block_size(cached_bdev));
972 
973 		/* This also adjusts physical block size/min io size if needed */
974 		blk_queue_logical_block_size(q, bdev_logical_block_size(cached_bdev));
975 	}
976 
977 	blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue);
978 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue);
979 	blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue);
980 
981 	blk_queue_write_cache(q, true, true);
982 
983 	return 0;
984 
985 out_bioset_exit:
986 	bioset_exit(&d->bio_split);
987 out_ida_remove:
988 	ida_simple_remove(&bcache_device_idx, idx);
989 out_free_full_dirty_stripes:
990 	kvfree(d->full_dirty_stripes);
991 out_free_stripe_sectors_dirty:
992 	kvfree(d->stripe_sectors_dirty);
993 	return -ENOMEM;
994 
995 }
996 
997 /* Cached device */
998 
999 static void calc_cached_dev_sectors(struct cache_set *c)
1000 {
1001 	uint64_t sectors = 0;
1002 	struct cached_dev *dc;
1003 
1004 	list_for_each_entry(dc, &c->cached_devs, list)
1005 		sectors += bdev_nr_sectors(dc->bdev);
1006 
1007 	c->cached_dev_sectors = sectors;
1008 }
1009 
1010 #define BACKING_DEV_OFFLINE_TIMEOUT 5
1011 static int cached_dev_status_update(void *arg)
1012 {
1013 	struct cached_dev *dc = arg;
1014 	struct request_queue *q;
1015 
1016 	/*
1017 	 * If this delayed worker is stopping outside, directly quit here.
1018 	 * dc->io_disable might be set via sysfs interface, so check it
1019 	 * here too.
1020 	 */
1021 	while (!kthread_should_stop() && !dc->io_disable) {
1022 		q = bdev_get_queue(dc->bdev);
1023 		if (blk_queue_dying(q))
1024 			dc->offline_seconds++;
1025 		else
1026 			dc->offline_seconds = 0;
1027 
1028 		if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) {
1029 			pr_err("%pg: device offline for %d seconds\n",
1030 			       dc->bdev,
1031 			       BACKING_DEV_OFFLINE_TIMEOUT);
1032 			pr_err("%s: disable I/O request due to backing device offline\n",
1033 			       dc->disk.name);
1034 			dc->io_disable = true;
1035 			/* let others know earlier that io_disable is true */
1036 			smp_mb();
1037 			bcache_device_stop(&dc->disk);
1038 			break;
1039 		}
1040 		schedule_timeout_interruptible(HZ);
1041 	}
1042 
1043 	wait_for_kthread_stop();
1044 	return 0;
1045 }
1046 
1047 
1048 int bch_cached_dev_run(struct cached_dev *dc)
1049 {
1050 	int ret = 0;
1051 	struct bcache_device *d = &dc->disk;
1052 	char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL);
1053 	char *env[] = {
1054 		"DRIVER=bcache",
1055 		kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
1056 		kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""),
1057 		NULL,
1058 	};
1059 
1060 	if (dc->io_disable) {
1061 		pr_err("I/O disabled on cached dev %pg\n", dc->bdev);
1062 		ret = -EIO;
1063 		goto out;
1064 	}
1065 
1066 	if (atomic_xchg(&dc->running, 1)) {
1067 		pr_info("cached dev %pg is running already\n", dc->bdev);
1068 		ret = -EBUSY;
1069 		goto out;
1070 	}
1071 
1072 	if (!d->c &&
1073 	    BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
1074 		struct closure cl;
1075 
1076 		closure_init_stack(&cl);
1077 
1078 		SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
1079 		bch_write_bdev_super(dc, &cl);
1080 		closure_sync(&cl);
1081 	}
1082 
1083 	ret = add_disk(d->disk);
1084 	if (ret)
1085 		goto out;
1086 	bd_link_disk_holder(dc->bdev, dc->disk.disk);
1087 	/*
1088 	 * won't show up in the uevent file, use udevadm monitor -e instead
1089 	 * only class / kset properties are persistent
1090 	 */
1091 	kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
1092 
1093 	if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
1094 	    sysfs_create_link(&disk_to_dev(d->disk)->kobj,
1095 			      &d->kobj, "bcache")) {
1096 		pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n");
1097 		ret = -ENOMEM;
1098 		goto out;
1099 	}
1100 
1101 	dc->status_update_thread = kthread_run(cached_dev_status_update,
1102 					       dc, "bcache_status_update");
1103 	if (IS_ERR(dc->status_update_thread)) {
1104 		pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n");
1105 	}
1106 
1107 out:
1108 	kfree(env[1]);
1109 	kfree(env[2]);
1110 	kfree(buf);
1111 	return ret;
1112 }
1113 
1114 /*
1115  * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed
1116  * work dc->writeback_rate_update is running. Wait until the routine
1117  * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to
1118  * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out
1119  * seconds, give up waiting here and continue to cancel it too.
1120  */
1121 static void cancel_writeback_rate_update_dwork(struct cached_dev *dc)
1122 {
1123 	int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ;
1124 
1125 	do {
1126 		if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING,
1127 			      &dc->disk.flags))
1128 			break;
1129 		time_out--;
1130 		schedule_timeout_interruptible(1);
1131 	} while (time_out > 0);
1132 
1133 	if (time_out == 0)
1134 		pr_warn("give up waiting for dc->writeback_write_update to quit\n");
1135 
1136 	cancel_delayed_work_sync(&dc->writeback_rate_update);
1137 }
1138 
1139 static void cached_dev_detach_finish(struct work_struct *w)
1140 {
1141 	struct cached_dev *dc = container_of(w, struct cached_dev, detach);
1142 	struct cache_set *c = dc->disk.c;
1143 
1144 	BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
1145 	BUG_ON(refcount_read(&dc->count));
1146 
1147 
1148 	if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
1149 		cancel_writeback_rate_update_dwork(dc);
1150 
1151 	if (!IS_ERR_OR_NULL(dc->writeback_thread)) {
1152 		kthread_stop(dc->writeback_thread);
1153 		dc->writeback_thread = NULL;
1154 	}
1155 
1156 	mutex_lock(&bch_register_lock);
1157 
1158 	bcache_device_detach(&dc->disk);
1159 	list_move(&dc->list, &uncached_devices);
1160 	calc_cached_dev_sectors(c);
1161 
1162 	clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
1163 	clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags);
1164 
1165 	mutex_unlock(&bch_register_lock);
1166 
1167 	pr_info("Caching disabled for %pg\n", dc->bdev);
1168 
1169 	/* Drop ref we took in cached_dev_detach() */
1170 	closure_put(&dc->disk.cl);
1171 }
1172 
1173 void bch_cached_dev_detach(struct cached_dev *dc)
1174 {
1175 	lockdep_assert_held(&bch_register_lock);
1176 
1177 	if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
1178 		return;
1179 
1180 	if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
1181 		return;
1182 
1183 	/*
1184 	 * Block the device from being closed and freed until we're finished
1185 	 * detaching
1186 	 */
1187 	closure_get(&dc->disk.cl);
1188 
1189 	bch_writeback_queue(dc);
1190 
1191 	cached_dev_put(dc);
1192 }
1193 
1194 int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
1195 			  uint8_t *set_uuid)
1196 {
1197 	uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds());
1198 	struct uuid_entry *u;
1199 	struct cached_dev *exist_dc, *t;
1200 	int ret = 0;
1201 
1202 	if ((set_uuid && memcmp(set_uuid, c->set_uuid, 16)) ||
1203 	    (!set_uuid && memcmp(dc->sb.set_uuid, c->set_uuid, 16)))
1204 		return -ENOENT;
1205 
1206 	if (dc->disk.c) {
1207 		pr_err("Can't attach %pg: already attached\n", dc->bdev);
1208 		return -EINVAL;
1209 	}
1210 
1211 	if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
1212 		pr_err("Can't attach %pg: shutting down\n", dc->bdev);
1213 		return -EINVAL;
1214 	}
1215 
1216 	if (dc->sb.block_size < c->cache->sb.block_size) {
1217 		/* Will die */
1218 		pr_err("Couldn't attach %pg: block size less than set's block size\n",
1219 		       dc->bdev);
1220 		return -EINVAL;
1221 	}
1222 
1223 	/* Check whether already attached */
1224 	list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) {
1225 		if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) {
1226 			pr_err("Tried to attach %pg but duplicate UUID already attached\n",
1227 				dc->bdev);
1228 
1229 			return -EINVAL;
1230 		}
1231 	}
1232 
1233 	u = uuid_find(c, dc->sb.uuid);
1234 
1235 	if (u &&
1236 	    (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
1237 	     BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
1238 		memcpy(u->uuid, invalid_uuid, 16);
1239 		u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
1240 		u = NULL;
1241 	}
1242 
1243 	if (!u) {
1244 		if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1245 			pr_err("Couldn't find uuid for %pg in set\n", dc->bdev);
1246 			return -ENOENT;
1247 		}
1248 
1249 		u = uuid_find_empty(c);
1250 		if (!u) {
1251 			pr_err("Not caching %pg, no room for UUID\n", dc->bdev);
1252 			return -EINVAL;
1253 		}
1254 	}
1255 
1256 	/*
1257 	 * Deadlocks since we're called via sysfs...
1258 	 * sysfs_remove_file(&dc->kobj, &sysfs_attach);
1259 	 */
1260 
1261 	if (bch_is_zero(u->uuid, 16)) {
1262 		struct closure cl;
1263 
1264 		closure_init_stack(&cl);
1265 
1266 		memcpy(u->uuid, dc->sb.uuid, 16);
1267 		memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1268 		u->first_reg = u->last_reg = rtime;
1269 		bch_uuid_write(c);
1270 
1271 		memcpy(dc->sb.set_uuid, c->set_uuid, 16);
1272 		SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1273 
1274 		bch_write_bdev_super(dc, &cl);
1275 		closure_sync(&cl);
1276 	} else {
1277 		u->last_reg = rtime;
1278 		bch_uuid_write(c);
1279 	}
1280 
1281 	bcache_device_attach(&dc->disk, c, u - c->uuids);
1282 	list_move(&dc->list, &c->cached_devs);
1283 	calc_cached_dev_sectors(c);
1284 
1285 	/*
1286 	 * dc->c must be set before dc->count != 0 - paired with the mb in
1287 	 * cached_dev_get()
1288 	 */
1289 	smp_wmb();
1290 	refcount_set(&dc->count, 1);
1291 
1292 	/* Block writeback thread, but spawn it */
1293 	down_write(&dc->writeback_lock);
1294 	if (bch_cached_dev_writeback_start(dc)) {
1295 		up_write(&dc->writeback_lock);
1296 		pr_err("Couldn't start writeback facilities for %s\n",
1297 		       dc->disk.disk->disk_name);
1298 		return -ENOMEM;
1299 	}
1300 
1301 	if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1302 		atomic_set(&dc->has_dirty, 1);
1303 		bch_writeback_queue(dc);
1304 	}
1305 
1306 	bch_sectors_dirty_init(&dc->disk);
1307 
1308 	ret = bch_cached_dev_run(dc);
1309 	if (ret && (ret != -EBUSY)) {
1310 		up_write(&dc->writeback_lock);
1311 		/*
1312 		 * bch_register_lock is held, bcache_device_stop() is not
1313 		 * able to be directly called. The kthread and kworker
1314 		 * created previously in bch_cached_dev_writeback_start()
1315 		 * have to be stopped manually here.
1316 		 */
1317 		kthread_stop(dc->writeback_thread);
1318 		cancel_writeback_rate_update_dwork(dc);
1319 		pr_err("Couldn't run cached device %pg\n", dc->bdev);
1320 		return ret;
1321 	}
1322 
1323 	bcache_device_link(&dc->disk, c, "bdev");
1324 	atomic_inc(&c->attached_dev_nr);
1325 
1326 	if (bch_has_feature_obso_large_bucket(&(c->cache->sb))) {
1327 		pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n");
1328 		pr_err("Please update to the latest bcache-tools to create the cache device\n");
1329 		set_disk_ro(dc->disk.disk, 1);
1330 	}
1331 
1332 	/* Allow the writeback thread to proceed */
1333 	up_write(&dc->writeback_lock);
1334 
1335 	pr_info("Caching %pg as %s on set %pU\n",
1336 		dc->bdev,
1337 		dc->disk.disk->disk_name,
1338 		dc->disk.c->set_uuid);
1339 	return 0;
1340 }
1341 
1342 /* when dc->disk.kobj released */
1343 void bch_cached_dev_release(struct kobject *kobj)
1344 {
1345 	struct cached_dev *dc = container_of(kobj, struct cached_dev,
1346 					     disk.kobj);
1347 	kfree(dc);
1348 	module_put(THIS_MODULE);
1349 }
1350 
1351 static void cached_dev_free(struct closure *cl)
1352 {
1353 	struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1354 
1355 	if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
1356 		cancel_writeback_rate_update_dwork(dc);
1357 
1358 	if (!IS_ERR_OR_NULL(dc->writeback_thread))
1359 		kthread_stop(dc->writeback_thread);
1360 	if (!IS_ERR_OR_NULL(dc->status_update_thread))
1361 		kthread_stop(dc->status_update_thread);
1362 
1363 	mutex_lock(&bch_register_lock);
1364 
1365 	if (atomic_read(&dc->running)) {
1366 		bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
1367 		del_gendisk(dc->disk.disk);
1368 	}
1369 	bcache_device_free(&dc->disk);
1370 	list_del(&dc->list);
1371 
1372 	mutex_unlock(&bch_register_lock);
1373 
1374 	if (dc->sb_disk)
1375 		put_page(virt_to_page(dc->sb_disk));
1376 
1377 	if (!IS_ERR_OR_NULL(dc->bdev))
1378 		blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1379 
1380 	wake_up(&unregister_wait);
1381 
1382 	kobject_put(&dc->disk.kobj);
1383 }
1384 
1385 static void cached_dev_flush(struct closure *cl)
1386 {
1387 	struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1388 	struct bcache_device *d = &dc->disk;
1389 
1390 	mutex_lock(&bch_register_lock);
1391 	bcache_device_unlink(d);
1392 	mutex_unlock(&bch_register_lock);
1393 
1394 	bch_cache_accounting_destroy(&dc->accounting);
1395 	kobject_del(&d->kobj);
1396 
1397 	continue_at(cl, cached_dev_free, system_wq);
1398 }
1399 
1400 static int cached_dev_init(struct cached_dev *dc, unsigned int block_size)
1401 {
1402 	int ret;
1403 	struct io *io;
1404 	struct request_queue *q = bdev_get_queue(dc->bdev);
1405 
1406 	__module_get(THIS_MODULE);
1407 	INIT_LIST_HEAD(&dc->list);
1408 	closure_init(&dc->disk.cl, NULL);
1409 	set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1410 	kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1411 	INIT_WORK(&dc->detach, cached_dev_detach_finish);
1412 	sema_init(&dc->sb_write_mutex, 1);
1413 	INIT_LIST_HEAD(&dc->io_lru);
1414 	spin_lock_init(&dc->io_lock);
1415 	bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1416 
1417 	dc->sequential_cutoff		= 4 << 20;
1418 
1419 	for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1420 		list_add(&io->lru, &dc->io_lru);
1421 		hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1422 	}
1423 
1424 	dc->disk.stripe_size = q->limits.io_opt >> 9;
1425 
1426 	if (dc->disk.stripe_size)
1427 		dc->partial_stripes_expensive =
1428 			q->limits.raid_partial_stripes_expensive;
1429 
1430 	ret = bcache_device_init(&dc->disk, block_size,
1431 			 bdev_nr_sectors(dc->bdev) - dc->sb.data_offset,
1432 			 dc->bdev, &bcache_cached_ops);
1433 	if (ret)
1434 		return ret;
1435 
1436 	blk_queue_io_opt(dc->disk.disk->queue,
1437 		max(queue_io_opt(dc->disk.disk->queue), queue_io_opt(q)));
1438 
1439 	atomic_set(&dc->io_errors, 0);
1440 	dc->io_disable = false;
1441 	dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT;
1442 	/* default to auto */
1443 	dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO;
1444 
1445 	bch_cached_dev_request_init(dc);
1446 	bch_cached_dev_writeback_init(dc);
1447 	return 0;
1448 }
1449 
1450 /* Cached device - bcache superblock */
1451 
1452 static int register_bdev(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
1453 				 struct block_device *bdev,
1454 				 struct cached_dev *dc)
1455 {
1456 	const char *err = "cannot allocate memory";
1457 	struct cache_set *c;
1458 	int ret = -ENOMEM;
1459 
1460 	memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1461 	dc->bdev = bdev;
1462 	dc->bdev->bd_holder = dc;
1463 	dc->sb_disk = sb_disk;
1464 
1465 	if (cached_dev_init(dc, sb->block_size << 9))
1466 		goto err;
1467 
1468 	err = "error creating kobject";
1469 	if (kobject_add(&dc->disk.kobj, bdev_kobj(bdev), "bcache"))
1470 		goto err;
1471 	if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1472 		goto err;
1473 
1474 	pr_info("registered backing device %pg\n", dc->bdev);
1475 
1476 	list_add(&dc->list, &uncached_devices);
1477 	/* attach to a matched cache set if it exists */
1478 	list_for_each_entry(c, &bch_cache_sets, list)
1479 		bch_cached_dev_attach(dc, c, NULL);
1480 
1481 	if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1482 	    BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) {
1483 		err = "failed to run cached device";
1484 		ret = bch_cached_dev_run(dc);
1485 		if (ret)
1486 			goto err;
1487 	}
1488 
1489 	return 0;
1490 err:
1491 	pr_notice("error %pg: %s\n", dc->bdev, err);
1492 	bcache_device_stop(&dc->disk);
1493 	return ret;
1494 }
1495 
1496 /* Flash only volumes */
1497 
1498 /* When d->kobj released */
1499 void bch_flash_dev_release(struct kobject *kobj)
1500 {
1501 	struct bcache_device *d = container_of(kobj, struct bcache_device,
1502 					       kobj);
1503 	kfree(d);
1504 }
1505 
1506 static void flash_dev_free(struct closure *cl)
1507 {
1508 	struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1509 
1510 	mutex_lock(&bch_register_lock);
1511 	atomic_long_sub(bcache_dev_sectors_dirty(d),
1512 			&d->c->flash_dev_dirty_sectors);
1513 	del_gendisk(d->disk);
1514 	bcache_device_free(d);
1515 	mutex_unlock(&bch_register_lock);
1516 	kobject_put(&d->kobj);
1517 }
1518 
1519 static void flash_dev_flush(struct closure *cl)
1520 {
1521 	struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1522 
1523 	mutex_lock(&bch_register_lock);
1524 	bcache_device_unlink(d);
1525 	mutex_unlock(&bch_register_lock);
1526 	kobject_del(&d->kobj);
1527 	continue_at(cl, flash_dev_free, system_wq);
1528 }
1529 
1530 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1531 {
1532 	int err = -ENOMEM;
1533 	struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1534 					  GFP_KERNEL);
1535 	if (!d)
1536 		goto err_ret;
1537 
1538 	closure_init(&d->cl, NULL);
1539 	set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1540 
1541 	kobject_init(&d->kobj, &bch_flash_dev_ktype);
1542 
1543 	if (bcache_device_init(d, block_bytes(c->cache), u->sectors,
1544 			NULL, &bcache_flash_ops))
1545 		goto err;
1546 
1547 	bcache_device_attach(d, c, u - c->uuids);
1548 	bch_sectors_dirty_init(d);
1549 	bch_flash_dev_request_init(d);
1550 	err = add_disk(d->disk);
1551 	if (err)
1552 		goto err;
1553 
1554 	err = kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache");
1555 	if (err)
1556 		goto err;
1557 
1558 	bcache_device_link(d, c, "volume");
1559 
1560 	if (bch_has_feature_obso_large_bucket(&c->cache->sb)) {
1561 		pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n");
1562 		pr_err("Please update to the latest bcache-tools to create the cache device\n");
1563 		set_disk_ro(d->disk, 1);
1564 	}
1565 
1566 	return 0;
1567 err:
1568 	kobject_put(&d->kobj);
1569 err_ret:
1570 	return err;
1571 }
1572 
1573 static int flash_devs_run(struct cache_set *c)
1574 {
1575 	int ret = 0;
1576 	struct uuid_entry *u;
1577 
1578 	for (u = c->uuids;
1579 	     u < c->uuids + c->nr_uuids && !ret;
1580 	     u++)
1581 		if (UUID_FLASH_ONLY(u))
1582 			ret = flash_dev_run(c, u);
1583 
1584 	return ret;
1585 }
1586 
1587 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1588 {
1589 	struct uuid_entry *u;
1590 
1591 	if (test_bit(CACHE_SET_STOPPING, &c->flags))
1592 		return -EINTR;
1593 
1594 	if (!test_bit(CACHE_SET_RUNNING, &c->flags))
1595 		return -EPERM;
1596 
1597 	u = uuid_find_empty(c);
1598 	if (!u) {
1599 		pr_err("Can't create volume, no room for UUID\n");
1600 		return -EINVAL;
1601 	}
1602 
1603 	get_random_bytes(u->uuid, 16);
1604 	memset(u->label, 0, 32);
1605 	u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
1606 
1607 	SET_UUID_FLASH_ONLY(u, 1);
1608 	u->sectors = size >> 9;
1609 
1610 	bch_uuid_write(c);
1611 
1612 	return flash_dev_run(c, u);
1613 }
1614 
1615 bool bch_cached_dev_error(struct cached_dev *dc)
1616 {
1617 	if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
1618 		return false;
1619 
1620 	dc->io_disable = true;
1621 	/* make others know io_disable is true earlier */
1622 	smp_mb();
1623 
1624 	pr_err("stop %s: too many IO errors on backing device %pg\n",
1625 	       dc->disk.disk->disk_name, dc->bdev);
1626 
1627 	bcache_device_stop(&dc->disk);
1628 	return true;
1629 }
1630 
1631 /* Cache set */
1632 
1633 __printf(2, 3)
1634 bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1635 {
1636 	struct va_format vaf;
1637 	va_list args;
1638 
1639 	if (c->on_error != ON_ERROR_PANIC &&
1640 	    test_bit(CACHE_SET_STOPPING, &c->flags))
1641 		return false;
1642 
1643 	if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags))
1644 		pr_info("CACHE_SET_IO_DISABLE already set\n");
1645 
1646 	/*
1647 	 * XXX: we can be called from atomic context
1648 	 * acquire_console_sem();
1649 	 */
1650 
1651 	va_start(args, fmt);
1652 
1653 	vaf.fmt = fmt;
1654 	vaf.va = &args;
1655 
1656 	pr_err("error on %pU: %pV, disabling caching\n",
1657 	       c->set_uuid, &vaf);
1658 
1659 	va_end(args);
1660 
1661 	if (c->on_error == ON_ERROR_PANIC)
1662 		panic("panic forced after error\n");
1663 
1664 	bch_cache_set_unregister(c);
1665 	return true;
1666 }
1667 
1668 /* When c->kobj released */
1669 void bch_cache_set_release(struct kobject *kobj)
1670 {
1671 	struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1672 
1673 	kfree(c);
1674 	module_put(THIS_MODULE);
1675 }
1676 
1677 static void cache_set_free(struct closure *cl)
1678 {
1679 	struct cache_set *c = container_of(cl, struct cache_set, cl);
1680 	struct cache *ca;
1681 
1682 	debugfs_remove(c->debug);
1683 
1684 	bch_open_buckets_free(c);
1685 	bch_btree_cache_free(c);
1686 	bch_journal_free(c);
1687 
1688 	mutex_lock(&bch_register_lock);
1689 	bch_bset_sort_state_free(&c->sort);
1690 	free_pages((unsigned long) c->uuids, ilog2(meta_bucket_pages(&c->cache->sb)));
1691 
1692 	ca = c->cache;
1693 	if (ca) {
1694 		ca->set = NULL;
1695 		c->cache = NULL;
1696 		kobject_put(&ca->kobj);
1697 	}
1698 
1699 
1700 	if (c->moving_gc_wq)
1701 		destroy_workqueue(c->moving_gc_wq);
1702 	bioset_exit(&c->bio_split);
1703 	mempool_exit(&c->fill_iter);
1704 	mempool_exit(&c->bio_meta);
1705 	mempool_exit(&c->search);
1706 	kfree(c->devices);
1707 
1708 	list_del(&c->list);
1709 	mutex_unlock(&bch_register_lock);
1710 
1711 	pr_info("Cache set %pU unregistered\n", c->set_uuid);
1712 	wake_up(&unregister_wait);
1713 
1714 	closure_debug_destroy(&c->cl);
1715 	kobject_put(&c->kobj);
1716 }
1717 
1718 static void cache_set_flush(struct closure *cl)
1719 {
1720 	struct cache_set *c = container_of(cl, struct cache_set, caching);
1721 	struct cache *ca = c->cache;
1722 	struct btree *b;
1723 
1724 	bch_cache_accounting_destroy(&c->accounting);
1725 
1726 	kobject_put(&c->internal);
1727 	kobject_del(&c->kobj);
1728 
1729 	if (!IS_ERR_OR_NULL(c->gc_thread))
1730 		kthread_stop(c->gc_thread);
1731 
1732 	if (!IS_ERR_OR_NULL(c->root))
1733 		list_add(&c->root->list, &c->btree_cache);
1734 
1735 	/*
1736 	 * Avoid flushing cached nodes if cache set is retiring
1737 	 * due to too many I/O errors detected.
1738 	 */
1739 	if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags))
1740 		list_for_each_entry(b, &c->btree_cache, list) {
1741 			mutex_lock(&b->write_lock);
1742 			if (btree_node_dirty(b))
1743 				__bch_btree_node_write(b, NULL);
1744 			mutex_unlock(&b->write_lock);
1745 		}
1746 
1747 	if (ca->alloc_thread)
1748 		kthread_stop(ca->alloc_thread);
1749 
1750 	if (c->journal.cur) {
1751 		cancel_delayed_work_sync(&c->journal.work);
1752 		/* flush last journal entry if needed */
1753 		c->journal.work.work.func(&c->journal.work.work);
1754 	}
1755 
1756 	closure_return(cl);
1757 }
1758 
1759 /*
1760  * This function is only called when CACHE_SET_IO_DISABLE is set, which means
1761  * cache set is unregistering due to too many I/O errors. In this condition,
1762  * the bcache device might be stopped, it depends on stop_when_cache_set_failed
1763  * value and whether the broken cache has dirty data:
1764  *
1765  * dc->stop_when_cache_set_failed    dc->has_dirty   stop bcache device
1766  *  BCH_CACHED_STOP_AUTO               0               NO
1767  *  BCH_CACHED_STOP_AUTO               1               YES
1768  *  BCH_CACHED_DEV_STOP_ALWAYS         0               YES
1769  *  BCH_CACHED_DEV_STOP_ALWAYS         1               YES
1770  *
1771  * The expected behavior is, if stop_when_cache_set_failed is configured to
1772  * "auto" via sysfs interface, the bcache device will not be stopped if the
1773  * backing device is clean on the broken cache device.
1774  */
1775 static void conditional_stop_bcache_device(struct cache_set *c,
1776 					   struct bcache_device *d,
1777 					   struct cached_dev *dc)
1778 {
1779 	if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) {
1780 		pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.\n",
1781 			d->disk->disk_name, c->set_uuid);
1782 		bcache_device_stop(d);
1783 	} else if (atomic_read(&dc->has_dirty)) {
1784 		/*
1785 		 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1786 		 * and dc->has_dirty == 1
1787 		 */
1788 		pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.\n",
1789 			d->disk->disk_name);
1790 		/*
1791 		 * There might be a small time gap that cache set is
1792 		 * released but bcache device is not. Inside this time
1793 		 * gap, regular I/O requests will directly go into
1794 		 * backing device as no cache set attached to. This
1795 		 * behavior may also introduce potential inconsistence
1796 		 * data in writeback mode while cache is dirty.
1797 		 * Therefore before calling bcache_device_stop() due
1798 		 * to a broken cache device, dc->io_disable should be
1799 		 * explicitly set to true.
1800 		 */
1801 		dc->io_disable = true;
1802 		/* make others know io_disable is true earlier */
1803 		smp_mb();
1804 		bcache_device_stop(d);
1805 	} else {
1806 		/*
1807 		 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1808 		 * and dc->has_dirty == 0
1809 		 */
1810 		pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.\n",
1811 			d->disk->disk_name);
1812 	}
1813 }
1814 
1815 static void __cache_set_unregister(struct closure *cl)
1816 {
1817 	struct cache_set *c = container_of(cl, struct cache_set, caching);
1818 	struct cached_dev *dc;
1819 	struct bcache_device *d;
1820 	size_t i;
1821 
1822 	mutex_lock(&bch_register_lock);
1823 
1824 	for (i = 0; i < c->devices_max_used; i++) {
1825 		d = c->devices[i];
1826 		if (!d)
1827 			continue;
1828 
1829 		if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1830 		    test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1831 			dc = container_of(d, struct cached_dev, disk);
1832 			bch_cached_dev_detach(dc);
1833 			if (test_bit(CACHE_SET_IO_DISABLE, &c->flags))
1834 				conditional_stop_bcache_device(c, d, dc);
1835 		} else {
1836 			bcache_device_stop(d);
1837 		}
1838 	}
1839 
1840 	mutex_unlock(&bch_register_lock);
1841 
1842 	continue_at(cl, cache_set_flush, system_wq);
1843 }
1844 
1845 void bch_cache_set_stop(struct cache_set *c)
1846 {
1847 	if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1848 		/* closure_fn set to __cache_set_unregister() */
1849 		closure_queue(&c->caching);
1850 }
1851 
1852 void bch_cache_set_unregister(struct cache_set *c)
1853 {
1854 	set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1855 	bch_cache_set_stop(c);
1856 }
1857 
1858 #define alloc_meta_bucket_pages(gfp, sb)		\
1859 	((void *) __get_free_pages(__GFP_ZERO|__GFP_COMP|gfp, ilog2(meta_bucket_pages(sb))))
1860 
1861 struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1862 {
1863 	int iter_size;
1864 	struct cache *ca = container_of(sb, struct cache, sb);
1865 	struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1866 
1867 	if (!c)
1868 		return NULL;
1869 
1870 	__module_get(THIS_MODULE);
1871 	closure_init(&c->cl, NULL);
1872 	set_closure_fn(&c->cl, cache_set_free, system_wq);
1873 
1874 	closure_init(&c->caching, &c->cl);
1875 	set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1876 
1877 	/* Maybe create continue_at_noreturn() and use it here? */
1878 	closure_set_stopped(&c->cl);
1879 	closure_put(&c->cl);
1880 
1881 	kobject_init(&c->kobj, &bch_cache_set_ktype);
1882 	kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1883 
1884 	bch_cache_accounting_init(&c->accounting, &c->cl);
1885 
1886 	memcpy(c->set_uuid, sb->set_uuid, 16);
1887 
1888 	c->cache		= ca;
1889 	c->cache->set		= c;
1890 	c->bucket_bits		= ilog2(sb->bucket_size);
1891 	c->block_bits		= ilog2(sb->block_size);
1892 	c->nr_uuids		= meta_bucket_bytes(sb) / sizeof(struct uuid_entry);
1893 	c->devices_max_used	= 0;
1894 	atomic_set(&c->attached_dev_nr, 0);
1895 	c->btree_pages		= meta_bucket_pages(sb);
1896 	if (c->btree_pages > BTREE_MAX_PAGES)
1897 		c->btree_pages = max_t(int, c->btree_pages / 4,
1898 				       BTREE_MAX_PAGES);
1899 
1900 	sema_init(&c->sb_write_mutex, 1);
1901 	mutex_init(&c->bucket_lock);
1902 	init_waitqueue_head(&c->btree_cache_wait);
1903 	spin_lock_init(&c->btree_cannibalize_lock);
1904 	init_waitqueue_head(&c->bucket_wait);
1905 	init_waitqueue_head(&c->gc_wait);
1906 	sema_init(&c->uuid_write_mutex, 1);
1907 
1908 	spin_lock_init(&c->btree_gc_time.lock);
1909 	spin_lock_init(&c->btree_split_time.lock);
1910 	spin_lock_init(&c->btree_read_time.lock);
1911 
1912 	bch_moving_init_cache_set(c);
1913 
1914 	INIT_LIST_HEAD(&c->list);
1915 	INIT_LIST_HEAD(&c->cached_devs);
1916 	INIT_LIST_HEAD(&c->btree_cache);
1917 	INIT_LIST_HEAD(&c->btree_cache_freeable);
1918 	INIT_LIST_HEAD(&c->btree_cache_freed);
1919 	INIT_LIST_HEAD(&c->data_buckets);
1920 
1921 	iter_size = ((meta_bucket_pages(sb) * PAGE_SECTORS) / sb->block_size + 1) *
1922 		sizeof(struct btree_iter_set);
1923 
1924 	c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL);
1925 	if (!c->devices)
1926 		goto err;
1927 
1928 	if (mempool_init_slab_pool(&c->search, 32, bch_search_cache))
1929 		goto err;
1930 
1931 	if (mempool_init_kmalloc_pool(&c->bio_meta, 2,
1932 			sizeof(struct bbio) +
1933 			sizeof(struct bio_vec) * meta_bucket_pages(sb)))
1934 		goto err;
1935 
1936 	if (mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size))
1937 		goto err;
1938 
1939 	if (bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio),
1940 			BIOSET_NEED_RESCUER))
1941 		goto err;
1942 
1943 	c->uuids = alloc_meta_bucket_pages(GFP_KERNEL, sb);
1944 	if (!c->uuids)
1945 		goto err;
1946 
1947 	c->moving_gc_wq = alloc_workqueue("bcache_gc", WQ_MEM_RECLAIM, 0);
1948 	if (!c->moving_gc_wq)
1949 		goto err;
1950 
1951 	if (bch_journal_alloc(c))
1952 		goto err;
1953 
1954 	if (bch_btree_cache_alloc(c))
1955 		goto err;
1956 
1957 	if (bch_open_buckets_alloc(c))
1958 		goto err;
1959 
1960 	if (bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages)))
1961 		goto err;
1962 
1963 	c->congested_read_threshold_us	= 2000;
1964 	c->congested_write_threshold_us	= 20000;
1965 	c->error_limit	= DEFAULT_IO_ERROR_LIMIT;
1966 	c->idle_max_writeback_rate_enabled = 1;
1967 	WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags));
1968 
1969 	return c;
1970 err:
1971 	bch_cache_set_unregister(c);
1972 	return NULL;
1973 }
1974 
1975 static int run_cache_set(struct cache_set *c)
1976 {
1977 	const char *err = "cannot allocate memory";
1978 	struct cached_dev *dc, *t;
1979 	struct cache *ca = c->cache;
1980 	struct closure cl;
1981 	LIST_HEAD(journal);
1982 	struct journal_replay *l;
1983 
1984 	closure_init_stack(&cl);
1985 
1986 	c->nbuckets = ca->sb.nbuckets;
1987 	set_gc_sectors(c);
1988 
1989 	if (CACHE_SYNC(&c->cache->sb)) {
1990 		struct bkey *k;
1991 		struct jset *j;
1992 
1993 		err = "cannot allocate memory for journal";
1994 		if (bch_journal_read(c, &journal))
1995 			goto err;
1996 
1997 		pr_debug("btree_journal_read() done\n");
1998 
1999 		err = "no journal entries found";
2000 		if (list_empty(&journal))
2001 			goto err;
2002 
2003 		j = &list_entry(journal.prev, struct journal_replay, list)->j;
2004 
2005 		err = "IO error reading priorities";
2006 		if (prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]))
2007 			goto err;
2008 
2009 		/*
2010 		 * If prio_read() fails it'll call cache_set_error and we'll
2011 		 * tear everything down right away, but if we perhaps checked
2012 		 * sooner we could avoid journal replay.
2013 		 */
2014 
2015 		k = &j->btree_root;
2016 
2017 		err = "bad btree root";
2018 		if (__bch_btree_ptr_invalid(c, k))
2019 			goto err;
2020 
2021 		err = "error reading btree root";
2022 		c->root = bch_btree_node_get(c, NULL, k,
2023 					     j->btree_level,
2024 					     true, NULL);
2025 		if (IS_ERR_OR_NULL(c->root))
2026 			goto err;
2027 
2028 		list_del_init(&c->root->list);
2029 		rw_unlock(true, c->root);
2030 
2031 		err = uuid_read(c, j, &cl);
2032 		if (err)
2033 			goto err;
2034 
2035 		err = "error in recovery";
2036 		if (bch_btree_check(c))
2037 			goto err;
2038 
2039 		bch_journal_mark(c, &journal);
2040 		bch_initial_gc_finish(c);
2041 		pr_debug("btree_check() done\n");
2042 
2043 		/*
2044 		 * bcache_journal_next() can't happen sooner, or
2045 		 * btree_gc_finish() will give spurious errors about last_gc >
2046 		 * gc_gen - this is a hack but oh well.
2047 		 */
2048 		bch_journal_next(&c->journal);
2049 
2050 		err = "error starting allocator thread";
2051 		if (bch_cache_allocator_start(ca))
2052 			goto err;
2053 
2054 		/*
2055 		 * First place it's safe to allocate: btree_check() and
2056 		 * btree_gc_finish() have to run before we have buckets to
2057 		 * allocate, and bch_bucket_alloc_set() might cause a journal
2058 		 * entry to be written so bcache_journal_next() has to be called
2059 		 * first.
2060 		 *
2061 		 * If the uuids were in the old format we have to rewrite them
2062 		 * before the next journal entry is written:
2063 		 */
2064 		if (j->version < BCACHE_JSET_VERSION_UUID)
2065 			__uuid_write(c);
2066 
2067 		err = "bcache: replay journal failed";
2068 		if (bch_journal_replay(c, &journal))
2069 			goto err;
2070 	} else {
2071 		unsigned int j;
2072 
2073 		pr_notice("invalidating existing data\n");
2074 		ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
2075 					2, SB_JOURNAL_BUCKETS);
2076 
2077 		for (j = 0; j < ca->sb.keys; j++)
2078 			ca->sb.d[j] = ca->sb.first_bucket + j;
2079 
2080 		bch_initial_gc_finish(c);
2081 
2082 		err = "error starting allocator thread";
2083 		if (bch_cache_allocator_start(ca))
2084 			goto err;
2085 
2086 		mutex_lock(&c->bucket_lock);
2087 		bch_prio_write(ca, true);
2088 		mutex_unlock(&c->bucket_lock);
2089 
2090 		err = "cannot allocate new UUID bucket";
2091 		if (__uuid_write(c))
2092 			goto err;
2093 
2094 		err = "cannot allocate new btree root";
2095 		c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL);
2096 		if (IS_ERR_OR_NULL(c->root))
2097 			goto err;
2098 
2099 		mutex_lock(&c->root->write_lock);
2100 		bkey_copy_key(&c->root->key, &MAX_KEY);
2101 		bch_btree_node_write(c->root, &cl);
2102 		mutex_unlock(&c->root->write_lock);
2103 
2104 		bch_btree_set_root(c->root);
2105 		rw_unlock(true, c->root);
2106 
2107 		/*
2108 		 * We don't want to write the first journal entry until
2109 		 * everything is set up - fortunately journal entries won't be
2110 		 * written until the SET_CACHE_SYNC() here:
2111 		 */
2112 		SET_CACHE_SYNC(&c->cache->sb, true);
2113 
2114 		bch_journal_next(&c->journal);
2115 		bch_journal_meta(c, &cl);
2116 	}
2117 
2118 	err = "error starting gc thread";
2119 	if (bch_gc_thread_start(c))
2120 		goto err;
2121 
2122 	closure_sync(&cl);
2123 	c->cache->sb.last_mount = (u32)ktime_get_real_seconds();
2124 	bcache_write_super(c);
2125 
2126 	if (bch_has_feature_obso_large_bucket(&c->cache->sb))
2127 		pr_err("Detect obsoleted large bucket layout, all attached bcache device will be read-only\n");
2128 
2129 	list_for_each_entry_safe(dc, t, &uncached_devices, list)
2130 		bch_cached_dev_attach(dc, c, NULL);
2131 
2132 	flash_devs_run(c);
2133 
2134 	set_bit(CACHE_SET_RUNNING, &c->flags);
2135 	return 0;
2136 err:
2137 	while (!list_empty(&journal)) {
2138 		l = list_first_entry(&journal, struct journal_replay, list);
2139 		list_del(&l->list);
2140 		kfree(l);
2141 	}
2142 
2143 	closure_sync(&cl);
2144 
2145 	bch_cache_set_error(c, "%s", err);
2146 
2147 	return -EIO;
2148 }
2149 
2150 static const char *register_cache_set(struct cache *ca)
2151 {
2152 	char buf[12];
2153 	const char *err = "cannot allocate memory";
2154 	struct cache_set *c;
2155 
2156 	list_for_each_entry(c, &bch_cache_sets, list)
2157 		if (!memcmp(c->set_uuid, ca->sb.set_uuid, 16)) {
2158 			if (c->cache)
2159 				return "duplicate cache set member";
2160 
2161 			goto found;
2162 		}
2163 
2164 	c = bch_cache_set_alloc(&ca->sb);
2165 	if (!c)
2166 		return err;
2167 
2168 	err = "error creating kobject";
2169 	if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->set_uuid) ||
2170 	    kobject_add(&c->internal, &c->kobj, "internal"))
2171 		goto err;
2172 
2173 	if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
2174 		goto err;
2175 
2176 	bch_debug_init_cache_set(c);
2177 
2178 	list_add(&c->list, &bch_cache_sets);
2179 found:
2180 	sprintf(buf, "cache%i", ca->sb.nr_this_dev);
2181 	if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
2182 	    sysfs_create_link(&c->kobj, &ca->kobj, buf))
2183 		goto err;
2184 
2185 	kobject_get(&ca->kobj);
2186 	ca->set = c;
2187 	ca->set->cache = ca;
2188 
2189 	err = "failed to run cache set";
2190 	if (run_cache_set(c) < 0)
2191 		goto err;
2192 
2193 	return NULL;
2194 err:
2195 	bch_cache_set_unregister(c);
2196 	return err;
2197 }
2198 
2199 /* Cache device */
2200 
2201 /* When ca->kobj released */
2202 void bch_cache_release(struct kobject *kobj)
2203 {
2204 	struct cache *ca = container_of(kobj, struct cache, kobj);
2205 	unsigned int i;
2206 
2207 	if (ca->set) {
2208 		BUG_ON(ca->set->cache != ca);
2209 		ca->set->cache = NULL;
2210 	}
2211 
2212 	free_pages((unsigned long) ca->disk_buckets, ilog2(meta_bucket_pages(&ca->sb)));
2213 	kfree(ca->prio_buckets);
2214 	vfree(ca->buckets);
2215 
2216 	free_heap(&ca->heap);
2217 	free_fifo(&ca->free_inc);
2218 
2219 	for (i = 0; i < RESERVE_NR; i++)
2220 		free_fifo(&ca->free[i]);
2221 
2222 	if (ca->sb_disk)
2223 		put_page(virt_to_page(ca->sb_disk));
2224 
2225 	if (!IS_ERR_OR_NULL(ca->bdev))
2226 		blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2227 
2228 	kfree(ca);
2229 	module_put(THIS_MODULE);
2230 }
2231 
2232 static int cache_alloc(struct cache *ca)
2233 {
2234 	size_t free;
2235 	size_t btree_buckets;
2236 	struct bucket *b;
2237 	int ret = -ENOMEM;
2238 	const char *err = NULL;
2239 
2240 	__module_get(THIS_MODULE);
2241 	kobject_init(&ca->kobj, &bch_cache_ktype);
2242 
2243 	bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8);
2244 
2245 	/*
2246 	 * when ca->sb.njournal_buckets is not zero, journal exists,
2247 	 * and in bch_journal_replay(), tree node may split,
2248 	 * so bucket of RESERVE_BTREE type is needed,
2249 	 * the worst situation is all journal buckets are valid journal,
2250 	 * and all the keys need to replay,
2251 	 * so the number of  RESERVE_BTREE type buckets should be as much
2252 	 * as journal buckets
2253 	 */
2254 	btree_buckets = ca->sb.njournal_buckets ?: 8;
2255 	free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
2256 	if (!free) {
2257 		ret = -EPERM;
2258 		err = "ca->sb.nbuckets is too small";
2259 		goto err_free;
2260 	}
2261 
2262 	if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets,
2263 						GFP_KERNEL)) {
2264 		err = "ca->free[RESERVE_BTREE] alloc failed";
2265 		goto err_btree_alloc;
2266 	}
2267 
2268 	if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca),
2269 							GFP_KERNEL)) {
2270 		err = "ca->free[RESERVE_PRIO] alloc failed";
2271 		goto err_prio_alloc;
2272 	}
2273 
2274 	if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) {
2275 		err = "ca->free[RESERVE_MOVINGGC] alloc failed";
2276 		goto err_movinggc_alloc;
2277 	}
2278 
2279 	if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) {
2280 		err = "ca->free[RESERVE_NONE] alloc failed";
2281 		goto err_none_alloc;
2282 	}
2283 
2284 	if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) {
2285 		err = "ca->free_inc alloc failed";
2286 		goto err_free_inc_alloc;
2287 	}
2288 
2289 	if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) {
2290 		err = "ca->heap alloc failed";
2291 		goto err_heap_alloc;
2292 	}
2293 
2294 	ca->buckets = vzalloc(array_size(sizeof(struct bucket),
2295 			      ca->sb.nbuckets));
2296 	if (!ca->buckets) {
2297 		err = "ca->buckets alloc failed";
2298 		goto err_buckets_alloc;
2299 	}
2300 
2301 	ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t),
2302 				   prio_buckets(ca), 2),
2303 				   GFP_KERNEL);
2304 	if (!ca->prio_buckets) {
2305 		err = "ca->prio_buckets alloc failed";
2306 		goto err_prio_buckets_alloc;
2307 	}
2308 
2309 	ca->disk_buckets = alloc_meta_bucket_pages(GFP_KERNEL, &ca->sb);
2310 	if (!ca->disk_buckets) {
2311 		err = "ca->disk_buckets alloc failed";
2312 		goto err_disk_buckets_alloc;
2313 	}
2314 
2315 	ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
2316 
2317 	for_each_bucket(b, ca)
2318 		atomic_set(&b->pin, 0);
2319 	return 0;
2320 
2321 err_disk_buckets_alloc:
2322 	kfree(ca->prio_buckets);
2323 err_prio_buckets_alloc:
2324 	vfree(ca->buckets);
2325 err_buckets_alloc:
2326 	free_heap(&ca->heap);
2327 err_heap_alloc:
2328 	free_fifo(&ca->free_inc);
2329 err_free_inc_alloc:
2330 	free_fifo(&ca->free[RESERVE_NONE]);
2331 err_none_alloc:
2332 	free_fifo(&ca->free[RESERVE_MOVINGGC]);
2333 err_movinggc_alloc:
2334 	free_fifo(&ca->free[RESERVE_PRIO]);
2335 err_prio_alloc:
2336 	free_fifo(&ca->free[RESERVE_BTREE]);
2337 err_btree_alloc:
2338 err_free:
2339 	module_put(THIS_MODULE);
2340 	if (err)
2341 		pr_notice("error %pg: %s\n", ca->bdev, err);
2342 	return ret;
2343 }
2344 
2345 static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
2346 				struct block_device *bdev, struct cache *ca)
2347 {
2348 	const char *err = NULL; /* must be set for any error case */
2349 	int ret = 0;
2350 
2351 	memcpy(&ca->sb, sb, sizeof(struct cache_sb));
2352 	ca->bdev = bdev;
2353 	ca->bdev->bd_holder = ca;
2354 	ca->sb_disk = sb_disk;
2355 
2356 	if (blk_queue_discard(bdev_get_queue(bdev)))
2357 		ca->discard = CACHE_DISCARD(&ca->sb);
2358 
2359 	ret = cache_alloc(ca);
2360 	if (ret != 0) {
2361 		/*
2362 		 * If we failed here, it means ca->kobj is not initialized yet,
2363 		 * kobject_put() won't be called and there is no chance to
2364 		 * call blkdev_put() to bdev in bch_cache_release(). So we
2365 		 * explicitly call blkdev_put() here.
2366 		 */
2367 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2368 		if (ret == -ENOMEM)
2369 			err = "cache_alloc(): -ENOMEM";
2370 		else if (ret == -EPERM)
2371 			err = "cache_alloc(): cache device is too small";
2372 		else
2373 			err = "cache_alloc(): unknown error";
2374 		goto err;
2375 	}
2376 
2377 	if (kobject_add(&ca->kobj, bdev_kobj(bdev), "bcache")) {
2378 		err = "error calling kobject_add";
2379 		ret = -ENOMEM;
2380 		goto out;
2381 	}
2382 
2383 	mutex_lock(&bch_register_lock);
2384 	err = register_cache_set(ca);
2385 	mutex_unlock(&bch_register_lock);
2386 
2387 	if (err) {
2388 		ret = -ENODEV;
2389 		goto out;
2390 	}
2391 
2392 	pr_info("registered cache device %pg\n", ca->bdev);
2393 
2394 out:
2395 	kobject_put(&ca->kobj);
2396 
2397 err:
2398 	if (err)
2399 		pr_notice("error %pg: %s\n", ca->bdev, err);
2400 
2401 	return ret;
2402 }
2403 
2404 /* Global interfaces/init */
2405 
2406 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
2407 			       const char *buffer, size_t size);
2408 static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
2409 					 struct kobj_attribute *attr,
2410 					 const char *buffer, size_t size);
2411 
2412 kobj_attribute_write(register,		register_bcache);
2413 kobj_attribute_write(register_quiet,	register_bcache);
2414 kobj_attribute_write(pendings_cleanup,	bch_pending_bdevs_cleanup);
2415 
2416 static bool bch_is_open_backing(dev_t dev)
2417 {
2418 	struct cache_set *c, *tc;
2419 	struct cached_dev *dc, *t;
2420 
2421 	list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2422 		list_for_each_entry_safe(dc, t, &c->cached_devs, list)
2423 			if (dc->bdev->bd_dev == dev)
2424 				return true;
2425 	list_for_each_entry_safe(dc, t, &uncached_devices, list)
2426 		if (dc->bdev->bd_dev == dev)
2427 			return true;
2428 	return false;
2429 }
2430 
2431 static bool bch_is_open_cache(dev_t dev)
2432 {
2433 	struct cache_set *c, *tc;
2434 
2435 	list_for_each_entry_safe(c, tc, &bch_cache_sets, list) {
2436 		struct cache *ca = c->cache;
2437 
2438 		if (ca->bdev->bd_dev == dev)
2439 			return true;
2440 	}
2441 
2442 	return false;
2443 }
2444 
2445 static bool bch_is_open(dev_t dev)
2446 {
2447 	return bch_is_open_cache(dev) || bch_is_open_backing(dev);
2448 }
2449 
2450 struct async_reg_args {
2451 	struct delayed_work reg_work;
2452 	char *path;
2453 	struct cache_sb *sb;
2454 	struct cache_sb_disk *sb_disk;
2455 	struct block_device *bdev;
2456 };
2457 
2458 static void register_bdev_worker(struct work_struct *work)
2459 {
2460 	int fail = false;
2461 	struct async_reg_args *args =
2462 		container_of(work, struct async_reg_args, reg_work.work);
2463 	struct cached_dev *dc;
2464 
2465 	dc = kzalloc(sizeof(*dc), GFP_KERNEL);
2466 	if (!dc) {
2467 		fail = true;
2468 		put_page(virt_to_page(args->sb_disk));
2469 		blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2470 		goto out;
2471 	}
2472 
2473 	mutex_lock(&bch_register_lock);
2474 	if (register_bdev(args->sb, args->sb_disk, args->bdev, dc) < 0)
2475 		fail = true;
2476 	mutex_unlock(&bch_register_lock);
2477 
2478 out:
2479 	if (fail)
2480 		pr_info("error %s: fail to register backing device\n",
2481 			args->path);
2482 	kfree(args->sb);
2483 	kfree(args->path);
2484 	kfree(args);
2485 	module_put(THIS_MODULE);
2486 }
2487 
2488 static void register_cache_worker(struct work_struct *work)
2489 {
2490 	int fail = false;
2491 	struct async_reg_args *args =
2492 		container_of(work, struct async_reg_args, reg_work.work);
2493 	struct cache *ca;
2494 
2495 	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2496 	if (!ca) {
2497 		fail = true;
2498 		put_page(virt_to_page(args->sb_disk));
2499 		blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2500 		goto out;
2501 	}
2502 
2503 	/* blkdev_put() will be called in bch_cache_release() */
2504 	if (register_cache(args->sb, args->sb_disk, args->bdev, ca) != 0)
2505 		fail = true;
2506 
2507 out:
2508 	if (fail)
2509 		pr_info("error %s: fail to register cache device\n",
2510 			args->path);
2511 	kfree(args->sb);
2512 	kfree(args->path);
2513 	kfree(args);
2514 	module_put(THIS_MODULE);
2515 }
2516 
2517 static void register_device_async(struct async_reg_args *args)
2518 {
2519 	if (SB_IS_BDEV(args->sb))
2520 		INIT_DELAYED_WORK(&args->reg_work, register_bdev_worker);
2521 	else
2522 		INIT_DELAYED_WORK(&args->reg_work, register_cache_worker);
2523 
2524 	/* 10 jiffies is enough for a delay */
2525 	queue_delayed_work(system_wq, &args->reg_work, 10);
2526 }
2527 
2528 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
2529 			       const char *buffer, size_t size)
2530 {
2531 	const char *err;
2532 	char *path = NULL;
2533 	struct cache_sb *sb;
2534 	struct cache_sb_disk *sb_disk;
2535 	struct block_device *bdev;
2536 	ssize_t ret;
2537 	bool async_registration = false;
2538 
2539 #ifdef CONFIG_BCACHE_ASYNC_REGISTRATION
2540 	async_registration = true;
2541 #endif
2542 
2543 	ret = -EBUSY;
2544 	err = "failed to reference bcache module";
2545 	if (!try_module_get(THIS_MODULE))
2546 		goto out;
2547 
2548 	/* For latest state of bcache_is_reboot */
2549 	smp_mb();
2550 	err = "bcache is in reboot";
2551 	if (bcache_is_reboot)
2552 		goto out_module_put;
2553 
2554 	ret = -ENOMEM;
2555 	err = "cannot allocate memory";
2556 	path = kstrndup(buffer, size, GFP_KERNEL);
2557 	if (!path)
2558 		goto out_module_put;
2559 
2560 	sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL);
2561 	if (!sb)
2562 		goto out_free_path;
2563 
2564 	ret = -EINVAL;
2565 	err = "failed to open device";
2566 	bdev = blkdev_get_by_path(strim(path),
2567 				  FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2568 				  sb);
2569 	if (IS_ERR(bdev)) {
2570 		if (bdev == ERR_PTR(-EBUSY)) {
2571 			dev_t dev;
2572 
2573 			mutex_lock(&bch_register_lock);
2574 			if (lookup_bdev(strim(path), &dev) == 0 &&
2575 			    bch_is_open(dev))
2576 				err = "device already registered";
2577 			else
2578 				err = "device busy";
2579 			mutex_unlock(&bch_register_lock);
2580 			if (attr == &ksysfs_register_quiet)
2581 				goto done;
2582 		}
2583 		goto out_free_sb;
2584 	}
2585 
2586 	err = "failed to set blocksize";
2587 	if (set_blocksize(bdev, 4096))
2588 		goto out_blkdev_put;
2589 
2590 	err = read_super(sb, bdev, &sb_disk);
2591 	if (err)
2592 		goto out_blkdev_put;
2593 
2594 	err = "failed to register device";
2595 
2596 	if (async_registration) {
2597 		/* register in asynchronous way */
2598 		struct async_reg_args *args =
2599 			kzalloc(sizeof(struct async_reg_args), GFP_KERNEL);
2600 
2601 		if (!args) {
2602 			ret = -ENOMEM;
2603 			err = "cannot allocate memory";
2604 			goto out_put_sb_page;
2605 		}
2606 
2607 		args->path	= path;
2608 		args->sb	= sb;
2609 		args->sb_disk	= sb_disk;
2610 		args->bdev	= bdev;
2611 		register_device_async(args);
2612 		/* No wait and returns to user space */
2613 		goto async_done;
2614 	}
2615 
2616 	if (SB_IS_BDEV(sb)) {
2617 		struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
2618 
2619 		if (!dc) {
2620 			ret = -ENOMEM;
2621 			err = "cannot allocate memory";
2622 			goto out_put_sb_page;
2623 		}
2624 
2625 		mutex_lock(&bch_register_lock);
2626 		ret = register_bdev(sb, sb_disk, bdev, dc);
2627 		mutex_unlock(&bch_register_lock);
2628 		/* blkdev_put() will be called in cached_dev_free() */
2629 		if (ret < 0)
2630 			goto out_free_sb;
2631 	} else {
2632 		struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2633 
2634 		if (!ca) {
2635 			ret = -ENOMEM;
2636 			err = "cannot allocate memory";
2637 			goto out_put_sb_page;
2638 		}
2639 
2640 		/* blkdev_put() will be called in bch_cache_release() */
2641 		ret = register_cache(sb, sb_disk, bdev, ca);
2642 		if (ret)
2643 			goto out_free_sb;
2644 	}
2645 
2646 done:
2647 	kfree(sb);
2648 	kfree(path);
2649 	module_put(THIS_MODULE);
2650 async_done:
2651 	return size;
2652 
2653 out_put_sb_page:
2654 	put_page(virt_to_page(sb_disk));
2655 out_blkdev_put:
2656 	blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2657 out_free_sb:
2658 	kfree(sb);
2659 out_free_path:
2660 	kfree(path);
2661 	path = NULL;
2662 out_module_put:
2663 	module_put(THIS_MODULE);
2664 out:
2665 	pr_info("error %s: %s\n", path?path:"", err);
2666 	return ret;
2667 }
2668 
2669 
2670 struct pdev {
2671 	struct list_head list;
2672 	struct cached_dev *dc;
2673 };
2674 
2675 static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
2676 					 struct kobj_attribute *attr,
2677 					 const char *buffer,
2678 					 size_t size)
2679 {
2680 	LIST_HEAD(pending_devs);
2681 	ssize_t ret = size;
2682 	struct cached_dev *dc, *tdc;
2683 	struct pdev *pdev, *tpdev;
2684 	struct cache_set *c, *tc;
2685 
2686 	mutex_lock(&bch_register_lock);
2687 	list_for_each_entry_safe(dc, tdc, &uncached_devices, list) {
2688 		pdev = kmalloc(sizeof(struct pdev), GFP_KERNEL);
2689 		if (!pdev)
2690 			break;
2691 		pdev->dc = dc;
2692 		list_add(&pdev->list, &pending_devs);
2693 	}
2694 
2695 	list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
2696 		char *pdev_set_uuid = pdev->dc->sb.set_uuid;
2697 		list_for_each_entry_safe(c, tc, &bch_cache_sets, list) {
2698 			char *set_uuid = c->set_uuid;
2699 
2700 			if (!memcmp(pdev_set_uuid, set_uuid, 16)) {
2701 				list_del(&pdev->list);
2702 				kfree(pdev);
2703 				break;
2704 			}
2705 		}
2706 	}
2707 	mutex_unlock(&bch_register_lock);
2708 
2709 	list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
2710 		pr_info("delete pdev %p\n", pdev);
2711 		list_del(&pdev->list);
2712 		bcache_device_stop(&pdev->dc->disk);
2713 		kfree(pdev);
2714 	}
2715 
2716 	return ret;
2717 }
2718 
2719 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
2720 {
2721 	if (bcache_is_reboot)
2722 		return NOTIFY_DONE;
2723 
2724 	if (code == SYS_DOWN ||
2725 	    code == SYS_HALT ||
2726 	    code == SYS_POWER_OFF) {
2727 		DEFINE_WAIT(wait);
2728 		unsigned long start = jiffies;
2729 		bool stopped = false;
2730 
2731 		struct cache_set *c, *tc;
2732 		struct cached_dev *dc, *tdc;
2733 
2734 		mutex_lock(&bch_register_lock);
2735 
2736 		if (bcache_is_reboot)
2737 			goto out;
2738 
2739 		/* New registration is rejected since now */
2740 		bcache_is_reboot = true;
2741 		/*
2742 		 * Make registering caller (if there is) on other CPU
2743 		 * core know bcache_is_reboot set to true earlier
2744 		 */
2745 		smp_mb();
2746 
2747 		if (list_empty(&bch_cache_sets) &&
2748 		    list_empty(&uncached_devices))
2749 			goto out;
2750 
2751 		mutex_unlock(&bch_register_lock);
2752 
2753 		pr_info("Stopping all devices:\n");
2754 
2755 		/*
2756 		 * The reason bch_register_lock is not held to call
2757 		 * bch_cache_set_stop() and bcache_device_stop() is to
2758 		 * avoid potential deadlock during reboot, because cache
2759 		 * set or bcache device stopping process will acquire
2760 		 * bch_register_lock too.
2761 		 *
2762 		 * We are safe here because bcache_is_reboot sets to
2763 		 * true already, register_bcache() will reject new
2764 		 * registration now. bcache_is_reboot also makes sure
2765 		 * bcache_reboot() won't be re-entered on by other thread,
2766 		 * so there is no race in following list iteration by
2767 		 * list_for_each_entry_safe().
2768 		 */
2769 		list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2770 			bch_cache_set_stop(c);
2771 
2772 		list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
2773 			bcache_device_stop(&dc->disk);
2774 
2775 
2776 		/*
2777 		 * Give an early chance for other kthreads and
2778 		 * kworkers to stop themselves
2779 		 */
2780 		schedule();
2781 
2782 		/* What's a condition variable? */
2783 		while (1) {
2784 			long timeout = start + 10 * HZ - jiffies;
2785 
2786 			mutex_lock(&bch_register_lock);
2787 			stopped = list_empty(&bch_cache_sets) &&
2788 				list_empty(&uncached_devices);
2789 
2790 			if (timeout < 0 || stopped)
2791 				break;
2792 
2793 			prepare_to_wait(&unregister_wait, &wait,
2794 					TASK_UNINTERRUPTIBLE);
2795 
2796 			mutex_unlock(&bch_register_lock);
2797 			schedule_timeout(timeout);
2798 		}
2799 
2800 		finish_wait(&unregister_wait, &wait);
2801 
2802 		if (stopped)
2803 			pr_info("All devices stopped\n");
2804 		else
2805 			pr_notice("Timeout waiting for devices to be closed\n");
2806 out:
2807 		mutex_unlock(&bch_register_lock);
2808 	}
2809 
2810 	return NOTIFY_DONE;
2811 }
2812 
2813 static struct notifier_block reboot = {
2814 	.notifier_call	= bcache_reboot,
2815 	.priority	= INT_MAX, /* before any real devices */
2816 };
2817 
2818 static void bcache_exit(void)
2819 {
2820 	bch_debug_exit();
2821 	bch_request_exit();
2822 	if (bcache_kobj)
2823 		kobject_put(bcache_kobj);
2824 	if (bcache_wq)
2825 		destroy_workqueue(bcache_wq);
2826 	if (bch_journal_wq)
2827 		destroy_workqueue(bch_journal_wq);
2828 	if (bch_flush_wq)
2829 		destroy_workqueue(bch_flush_wq);
2830 	bch_btree_exit();
2831 
2832 	if (bcache_major)
2833 		unregister_blkdev(bcache_major, "bcache");
2834 	unregister_reboot_notifier(&reboot);
2835 	mutex_destroy(&bch_register_lock);
2836 }
2837 
2838 /* Check and fixup module parameters */
2839 static void check_module_parameters(void)
2840 {
2841 	if (bch_cutoff_writeback_sync == 0)
2842 		bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC;
2843 	else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) {
2844 		pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u\n",
2845 			bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX);
2846 		bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX;
2847 	}
2848 
2849 	if (bch_cutoff_writeback == 0)
2850 		bch_cutoff_writeback = CUTOFF_WRITEBACK;
2851 	else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) {
2852 		pr_warn("set bch_cutoff_writeback (%u) to max value %u\n",
2853 			bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX);
2854 		bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX;
2855 	}
2856 
2857 	if (bch_cutoff_writeback > bch_cutoff_writeback_sync) {
2858 		pr_warn("set bch_cutoff_writeback (%u) to %u\n",
2859 			bch_cutoff_writeback, bch_cutoff_writeback_sync);
2860 		bch_cutoff_writeback = bch_cutoff_writeback_sync;
2861 	}
2862 }
2863 
2864 static int __init bcache_init(void)
2865 {
2866 	static const struct attribute *files[] = {
2867 		&ksysfs_register.attr,
2868 		&ksysfs_register_quiet.attr,
2869 		&ksysfs_pendings_cleanup.attr,
2870 		NULL
2871 	};
2872 
2873 	check_module_parameters();
2874 
2875 	mutex_init(&bch_register_lock);
2876 	init_waitqueue_head(&unregister_wait);
2877 	register_reboot_notifier(&reboot);
2878 
2879 	bcache_major = register_blkdev(0, "bcache");
2880 	if (bcache_major < 0) {
2881 		unregister_reboot_notifier(&reboot);
2882 		mutex_destroy(&bch_register_lock);
2883 		return bcache_major;
2884 	}
2885 
2886 	if (bch_btree_init())
2887 		goto err;
2888 
2889 	bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0);
2890 	if (!bcache_wq)
2891 		goto err;
2892 
2893 	/*
2894 	 * Let's not make this `WQ_MEM_RECLAIM` for the following reasons:
2895 	 *
2896 	 * 1. It used `system_wq` before which also does no memory reclaim.
2897 	 * 2. With `WQ_MEM_RECLAIM` desktop stalls, increased boot times, and
2898 	 *    reduced throughput can be observed.
2899 	 *
2900 	 * We still want to user our own queue to not congest the `system_wq`.
2901 	 */
2902 	bch_flush_wq = alloc_workqueue("bch_flush", 0, 0);
2903 	if (!bch_flush_wq)
2904 		goto err;
2905 
2906 	bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0);
2907 	if (!bch_journal_wq)
2908 		goto err;
2909 
2910 	bcache_kobj = kobject_create_and_add("bcache", fs_kobj);
2911 	if (!bcache_kobj)
2912 		goto err;
2913 
2914 	if (bch_request_init() ||
2915 	    sysfs_create_files(bcache_kobj, files))
2916 		goto err;
2917 
2918 	bch_debug_init();
2919 	closure_debug_init();
2920 
2921 	bcache_is_reboot = false;
2922 
2923 	return 0;
2924 err:
2925 	bcache_exit();
2926 	return -ENOMEM;
2927 }
2928 
2929 /*
2930  * Module hooks
2931  */
2932 module_exit(bcache_exit);
2933 module_init(bcache_init);
2934 
2935 module_param(bch_cutoff_writeback, uint, 0);
2936 MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback");
2937 
2938 module_param(bch_cutoff_writeback_sync, uint, 0);
2939 MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback");
2940 
2941 MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
2942 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
2943 MODULE_LICENSE("GPL");
2944