request.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Main bcache entry point - handle a read or a write request and decide what to
  4. * do with it; the make_request functions are called by the block layer.
  5. *
  6. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  7. * Copyright 2012 Google, Inc.
  8. */
  9. #include "bcache.h"
  10. #include "btree.h"
  11. #include "debug.h"
  12. #include "request.h"
  13. #include "writeback.h"
  14. #include <linux/module.h>
  15. #include <linux/hash.h>
  16. #include <linux/random.h>
  17. #include <linux/backing-dev.h>
  18. #include <trace/events/bcache.h>
  19. #define CUTOFF_CACHE_ADD 95
  20. #define CUTOFF_CACHE_READA 90
  21. struct kmem_cache *bch_search_cache;
  22. static void bch_data_insert_start(struct closure *cl);
  23. static unsigned int cache_mode(struct cached_dev *dc)
  24. {
  25. return BDEV_CACHE_MODE(&dc->sb);
  26. }
  27. static bool verify(struct cached_dev *dc)
  28. {
  29. return dc->verify;
  30. }
  31. static void bio_csum(struct bio *bio, struct bkey *k)
  32. {
  33. struct bio_vec bv;
  34. struct bvec_iter iter;
  35. uint64_t csum = 0;
  36. bio_for_each_segment(bv, bio, iter) {
  37. void *d = kmap(bv.bv_page) + bv.bv_offset;
  38. csum = bch_crc64_update(csum, d, bv.bv_len);
  39. kunmap(bv.bv_page);
  40. }
  41. k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
  42. }
  43. /* Insert data into cache */
  44. static void bch_data_insert_keys(struct closure *cl)
  45. {
  46. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  47. atomic_t *journal_ref = NULL;
  48. struct bkey *replace_key = op->replace ? &op->replace_key : NULL;
  49. int ret;
  50. /*
  51. * If we're looping, might already be waiting on
  52. * another journal write - can't wait on more than one journal write at
  53. * a time
  54. *
  55. * XXX: this looks wrong
  56. */
  57. #if 0
  58. while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING)
  59. closure_sync(&s->cl);
  60. #endif
  61. if (!op->replace)
  62. journal_ref = bch_journal(op->c, &op->insert_keys,
  63. op->flush_journal ? cl : NULL);
  64. ret = bch_btree_insert(op->c, &op->insert_keys,
  65. journal_ref, replace_key);
  66. if (ret == -ESRCH) {
  67. op->replace_collision = true;
  68. } else if (ret) {
  69. op->status = BLK_STS_RESOURCE;
  70. op->insert_data_done = true;
  71. }
  72. if (journal_ref)
  73. atomic_dec_bug(journal_ref);
  74. if (!op->insert_data_done) {
  75. continue_at(cl, bch_data_insert_start, op->wq);
  76. return;
  77. }
  78. bch_keylist_free(&op->insert_keys);
  79. closure_return(cl);
  80. }
  81. static int bch_keylist_realloc(struct keylist *l, unsigned int u64s,
  82. struct cache_set *c)
  83. {
  84. size_t oldsize = bch_keylist_nkeys(l);
  85. size_t newsize = oldsize + u64s;
  86. /*
  87. * The journalling code doesn't handle the case where the keys to insert
  88. * is bigger than an empty write: If we just return -ENOMEM here,
  89. * bch_data_insert_keys() will insert the keys created so far
  90. * and finish the rest when the keylist is empty.
  91. */
  92. if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
  93. return -ENOMEM;
  94. return __bch_keylist_realloc(l, u64s);
  95. }
  96. static void bch_data_invalidate(struct closure *cl)
  97. {
  98. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  99. struct bio *bio = op->bio;
  100. pr_debug("invalidating %i sectors from %llu",
  101. bio_sectors(bio), (uint64_t) bio->bi_iter.bi_sector);
  102. while (bio_sectors(bio)) {
  103. unsigned int sectors = min(bio_sectors(bio),
  104. 1U << (KEY_SIZE_BITS - 1));
  105. if (bch_keylist_realloc(&op->insert_keys, 2, op->c))
  106. goto out;
  107. bio->bi_iter.bi_sector += sectors;
  108. bio->bi_iter.bi_size -= sectors << 9;
  109. bch_keylist_add(&op->insert_keys,
  110. &KEY(op->inode,
  111. bio->bi_iter.bi_sector,
  112. sectors));
  113. }
  114. op->insert_data_done = true;
  115. /* get in bch_data_insert() */
  116. bio_put(bio);
  117. out:
  118. continue_at(cl, bch_data_insert_keys, op->wq);
  119. }
  120. static void bch_data_insert_error(struct closure *cl)
  121. {
  122. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  123. /*
  124. * Our data write just errored, which means we've got a bunch of keys to
  125. * insert that point to data that wasn't successfully written.
  126. *
  127. * We don't have to insert those keys but we still have to invalidate
  128. * that region of the cache - so, if we just strip off all the pointers
  129. * from the keys we'll accomplish just that.
  130. */
  131. struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys;
  132. while (src != op->insert_keys.top) {
  133. struct bkey *n = bkey_next(src);
  134. SET_KEY_PTRS(src, 0);
  135. memmove(dst, src, bkey_bytes(src));
  136. dst = bkey_next(dst);
  137. src = n;
  138. }
  139. op->insert_keys.top = dst;
  140. bch_data_insert_keys(cl);
  141. }
  142. static void bch_data_insert_endio(struct bio *bio)
  143. {
  144. struct closure *cl = bio->bi_private;
  145. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  146. if (bio->bi_status) {
  147. /* TODO: We could try to recover from this. */
  148. if (op->writeback)
  149. op->status = bio->bi_status;
  150. else if (!op->replace)
  151. set_closure_fn(cl, bch_data_insert_error, op->wq);
  152. else
  153. set_closure_fn(cl, NULL, NULL);
  154. }
  155. bch_bbio_endio(op->c, bio, bio->bi_status, "writing data to cache");
  156. }
  157. static void bch_data_insert_start(struct closure *cl)
  158. {
  159. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  160. struct bio *bio = op->bio, *n;
  161. if (op->bypass)
  162. return bch_data_invalidate(cl);
  163. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0)
  164. wake_up_gc(op->c);
  165. /*
  166. * Journal writes are marked REQ_PREFLUSH; if the original write was a
  167. * flush, it'll wait on the journal write.
  168. */
  169. bio->bi_opf &= ~(REQ_PREFLUSH|REQ_FUA);
  170. do {
  171. unsigned int i;
  172. struct bkey *k;
  173. struct bio_set *split = &op->c->bio_split;
  174. /* 1 for the device pointer and 1 for the chksum */
  175. if (bch_keylist_realloc(&op->insert_keys,
  176. 3 + (op->csum ? 1 : 0),
  177. op->c)) {
  178. continue_at(cl, bch_data_insert_keys, op->wq);
  179. return;
  180. }
  181. k = op->insert_keys.top;
  182. bkey_init(k);
  183. SET_KEY_INODE(k, op->inode);
  184. SET_KEY_OFFSET(k, bio->bi_iter.bi_sector);
  185. if (!bch_alloc_sectors(op->c, k, bio_sectors(bio),
  186. op->write_point, op->write_prio,
  187. op->writeback))
  188. goto err;
  189. n = bio_next_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  190. n->bi_end_io = bch_data_insert_endio;
  191. n->bi_private = cl;
  192. if (op->writeback) {
  193. SET_KEY_DIRTY(k, true);
  194. for (i = 0; i < KEY_PTRS(k); i++)
  195. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  196. GC_MARK_DIRTY);
  197. }
  198. SET_KEY_CSUM(k, op->csum);
  199. if (KEY_CSUM(k))
  200. bio_csum(n, k);
  201. trace_bcache_cache_insert(k);
  202. bch_keylist_push(&op->insert_keys);
  203. bio_set_op_attrs(n, REQ_OP_WRITE, 0);
  204. bch_submit_bbio(n, op->c, k, 0);
  205. } while (n != bio);
  206. op->insert_data_done = true;
  207. continue_at(cl, bch_data_insert_keys, op->wq);
  208. return;
  209. err:
  210. /* bch_alloc_sectors() blocks if s->writeback = true */
  211. BUG_ON(op->writeback);
  212. /*
  213. * But if it's not a writeback write we'd rather just bail out if
  214. * there aren't any buckets ready to write to - it might take awhile and
  215. * we might be starving btree writes for gc or something.
  216. */
  217. if (!op->replace) {
  218. /*
  219. * Writethrough write: We can't complete the write until we've
  220. * updated the index. But we don't want to delay the write while
  221. * we wait for buckets to be freed up, so just invalidate the
  222. * rest of the write.
  223. */
  224. op->bypass = true;
  225. return bch_data_invalidate(cl);
  226. } else {
  227. /*
  228. * From a cache miss, we can just insert the keys for the data
  229. * we have written or bail out if we didn't do anything.
  230. */
  231. op->insert_data_done = true;
  232. bio_put(bio);
  233. if (!bch_keylist_empty(&op->insert_keys))
  234. continue_at(cl, bch_data_insert_keys, op->wq);
  235. else
  236. closure_return(cl);
  237. }
  238. }
  239. /**
  240. * bch_data_insert - stick some data in the cache
  241. * @cl: closure pointer.
  242. *
  243. * This is the starting point for any data to end up in a cache device; it could
  244. * be from a normal write, or a writeback write, or a write to a flash only
  245. * volume - it's also used by the moving garbage collector to compact data in
  246. * mostly empty buckets.
  247. *
  248. * It first writes the data to the cache, creating a list of keys to be inserted
  249. * (if the data had to be fragmented there will be multiple keys); after the
  250. * data is written it calls bch_journal, and after the keys have been added to
  251. * the next journal write they're inserted into the btree.
  252. *
  253. * It inserts the data in s->cache_bio; bi_sector is used for the key offset,
  254. * and op->inode is used for the key inode.
  255. *
  256. * If s->bypass is true, instead of inserting the data it invalidates the
  257. * region of the cache represented by s->cache_bio and op->inode.
  258. */
  259. void bch_data_insert(struct closure *cl)
  260. {
  261. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  262. trace_bcache_write(op->c, op->inode, op->bio,
  263. op->writeback, op->bypass);
  264. bch_keylist_init(&op->insert_keys);
  265. bio_get(op->bio);
  266. bch_data_insert_start(cl);
  267. }
  268. /* Congested? */
  269. unsigned int bch_get_congested(struct cache_set *c)
  270. {
  271. int i;
  272. long rand;
  273. if (!c->congested_read_threshold_us &&
  274. !c->congested_write_threshold_us)
  275. return 0;
  276. i = (local_clock_us() - c->congested_last_us) / 1024;
  277. if (i < 0)
  278. return 0;
  279. i += atomic_read(&c->congested);
  280. if (i >= 0)
  281. return 0;
  282. i += CONGESTED_MAX;
  283. if (i > 0)
  284. i = fract_exp_two(i, 6);
  285. rand = get_random_int();
  286. i -= bitmap_weight(&rand, BITS_PER_LONG);
  287. return i > 0 ? i : 1;
  288. }
  289. static void add_sequential(struct task_struct *t)
  290. {
  291. ewma_add(t->sequential_io_avg,
  292. t->sequential_io, 8, 0);
  293. t->sequential_io = 0;
  294. }
  295. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  296. {
  297. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  298. }
  299. static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
  300. {
  301. struct cache_set *c = dc->disk.c;
  302. unsigned int mode = cache_mode(dc);
  303. unsigned int sectors, congested = bch_get_congested(c);
  304. struct task_struct *task = current;
  305. struct io *i;
  306. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  307. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  308. (bio_op(bio) == REQ_OP_DISCARD))
  309. goto skip;
  310. if (mode == CACHE_MODE_NONE ||
  311. (mode == CACHE_MODE_WRITEAROUND &&
  312. op_is_write(bio_op(bio))))
  313. goto skip;
  314. /*
  315. * If the bio is for read-ahead or background IO, bypass it or
  316. * not depends on the following situations,
  317. * - If the IO is for meta data, always cache it and no bypass
  318. * - If the IO is not meta data, check dc->cache_reada_policy,
  319. * BCH_CACHE_READA_ALL: cache it and not bypass
  320. * BCH_CACHE_READA_META_ONLY: not cache it and bypass
  321. * That is, read-ahead request for metadata always get cached
  322. * (eg, for gfs2 or xfs).
  323. */
  324. if ((bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND))) {
  325. if (!(bio->bi_opf & (REQ_META|REQ_PRIO)) &&
  326. (dc->cache_readahead_policy != BCH_CACHE_READA_ALL))
  327. goto skip;
  328. }
  329. if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||
  330. bio_sectors(bio) & (c->sb.block_size - 1)) {
  331. pr_debug("skipping unaligned io");
  332. goto skip;
  333. }
  334. if (bypass_torture_test(dc)) {
  335. if ((get_random_int() & 3) == 3)
  336. goto skip;
  337. else
  338. goto rescale;
  339. }
  340. if (!congested && !dc->sequential_cutoff)
  341. goto rescale;
  342. spin_lock(&dc->io_lock);
  343. hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash)
  344. if (i->last == bio->bi_iter.bi_sector &&
  345. time_before(jiffies, i->jiffies))
  346. goto found;
  347. i = list_first_entry(&dc->io_lru, struct io, lru);
  348. add_sequential(task);
  349. i->sequential = 0;
  350. found:
  351. if (i->sequential + bio->bi_iter.bi_size > i->sequential)
  352. i->sequential += bio->bi_iter.bi_size;
  353. i->last = bio_end_sector(bio);
  354. i->jiffies = jiffies + msecs_to_jiffies(5000);
  355. task->sequential_io = i->sequential;
  356. hlist_del(&i->hash);
  357. hlist_add_head(&i->hash, iohash(dc, i->last));
  358. list_move_tail(&i->lru, &dc->io_lru);
  359. spin_unlock(&dc->io_lock);
  360. sectors = max(task->sequential_io,
  361. task->sequential_io_avg) >> 9;
  362. if (dc->sequential_cutoff &&
  363. sectors >= dc->sequential_cutoff >> 9) {
  364. trace_bcache_bypass_sequential(bio);
  365. goto skip;
  366. }
  367. if (congested && sectors >= congested) {
  368. trace_bcache_bypass_congested(bio);
  369. goto skip;
  370. }
  371. rescale:
  372. bch_rescale_priorities(c, bio_sectors(bio));
  373. return false;
  374. skip:
  375. bch_mark_sectors_bypassed(c, dc, bio_sectors(bio));
  376. return true;
  377. }
  378. /* Cache lookup */
  379. struct search {
  380. /* Stack frame for bio_complete */
  381. struct closure cl;
  382. struct bbio bio;
  383. struct bio *orig_bio;
  384. struct bio *cache_miss;
  385. struct bcache_device *d;
  386. unsigned int insert_bio_sectors;
  387. unsigned int recoverable:1;
  388. unsigned int write:1;
  389. unsigned int read_dirty_data:1;
  390. unsigned int cache_missed:1;
  391. unsigned long start_time;
  392. struct btree_op op;
  393. struct data_insert_op iop;
  394. };
  395. static void bch_cache_read_endio(struct bio *bio)
  396. {
  397. struct bbio *b = container_of(bio, struct bbio, bio);
  398. struct closure *cl = bio->bi_private;
  399. struct search *s = container_of(cl, struct search, cl);
  400. /*
  401. * If the bucket was reused while our bio was in flight, we might have
  402. * read the wrong data. Set s->error but not error so it doesn't get
  403. * counted against the cache device, but we'll still reread the data
  404. * from the backing device.
  405. */
  406. if (bio->bi_status)
  407. s->iop.status = bio->bi_status;
  408. else if (!KEY_DIRTY(&b->key) &&
  409. ptr_stale(s->iop.c, &b->key, 0)) {
  410. atomic_long_inc(&s->iop.c->cache_read_races);
  411. s->iop.status = BLK_STS_IOERR;
  412. }
  413. bch_bbio_endio(s->iop.c, bio, bio->bi_status, "reading from cache");
  414. }
  415. /*
  416. * Read from a single key, handling the initial cache miss if the key starts in
  417. * the middle of the bio
  418. */
  419. static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
  420. {
  421. struct search *s = container_of(op, struct search, op);
  422. struct bio *n, *bio = &s->bio.bio;
  423. struct bkey *bio_key;
  424. unsigned int ptr;
  425. if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0)
  426. return MAP_CONTINUE;
  427. if (KEY_INODE(k) != s->iop.inode ||
  428. KEY_START(k) > bio->bi_iter.bi_sector) {
  429. unsigned int bio_sectors = bio_sectors(bio);
  430. unsigned int sectors = KEY_INODE(k) == s->iop.inode
  431. ? min_t(uint64_t, INT_MAX,
  432. KEY_START(k) - bio->bi_iter.bi_sector)
  433. : INT_MAX;
  434. int ret = s->d->cache_miss(b, s, bio, sectors);
  435. if (ret != MAP_CONTINUE)
  436. return ret;
  437. /* if this was a complete miss we shouldn't get here */
  438. BUG_ON(bio_sectors <= sectors);
  439. }
  440. if (!KEY_SIZE(k))
  441. return MAP_CONTINUE;
  442. /* XXX: figure out best pointer - for multiple cache devices */
  443. ptr = 0;
  444. PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
  445. if (KEY_DIRTY(k))
  446. s->read_dirty_data = true;
  447. n = bio_next_split(bio, min_t(uint64_t, INT_MAX,
  448. KEY_OFFSET(k) - bio->bi_iter.bi_sector),
  449. GFP_NOIO, &s->d->bio_split);
  450. bio_key = &container_of(n, struct bbio, bio)->key;
  451. bch_bkey_copy_single_ptr(bio_key, k, ptr);
  452. bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key);
  453. bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
  454. n->bi_end_io = bch_cache_read_endio;
  455. n->bi_private = &s->cl;
  456. /*
  457. * The bucket we're reading from might be reused while our bio
  458. * is in flight, and we could then end up reading the wrong
  459. * data.
  460. *
  461. * We guard against this by checking (in cache_read_endio()) if
  462. * the pointer is stale again; if so, we treat it as an error
  463. * and reread from the backing device (but we don't pass that
  464. * error up anywhere).
  465. */
  466. __bch_submit_bbio(n, b->c);
  467. return n == bio ? MAP_DONE : MAP_CONTINUE;
  468. }
  469. static void cache_lookup(struct closure *cl)
  470. {
  471. struct search *s = container_of(cl, struct search, iop.cl);
  472. struct bio *bio = &s->bio.bio;
  473. struct cached_dev *dc;
  474. int ret;
  475. bch_btree_op_init(&s->op, -1);
  476. ret = bch_btree_map_keys(&s->op, s->iop.c,
  477. &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0),
  478. cache_lookup_fn, MAP_END_KEY);
  479. if (ret == -EAGAIN) {
  480. continue_at(cl, cache_lookup, bcache_wq);
  481. return;
  482. }
  483. /*
  484. * We might meet err when searching the btree, If that happens, we will
  485. * get negative ret, in this scenario we should not recover data from
  486. * backing device (when cache device is dirty) because we don't know
  487. * whether bkeys the read request covered are all clean.
  488. *
  489. * And after that happened, s->iop.status is still its initial value
  490. * before we submit s->bio.bio
  491. */
  492. if (ret < 0) {
  493. BUG_ON(ret == -EINTR);
  494. if (s->d && s->d->c &&
  495. !UUID_FLASH_ONLY(&s->d->c->uuids[s->d->id])) {
  496. dc = container_of(s->d, struct cached_dev, disk);
  497. if (dc && atomic_read(&dc->has_dirty))
  498. s->recoverable = false;
  499. }
  500. if (!s->iop.status)
  501. s->iop.status = BLK_STS_IOERR;
  502. }
  503. closure_return(cl);
  504. }
  505. /* Common code for the make_request functions */
  506. static void request_endio(struct bio *bio)
  507. {
  508. struct closure *cl = bio->bi_private;
  509. if (bio->bi_status) {
  510. struct search *s = container_of(cl, struct search, cl);
  511. s->iop.status = bio->bi_status;
  512. /* Only cache read errors are recoverable */
  513. s->recoverable = false;
  514. }
  515. bio_put(bio);
  516. closure_put(cl);
  517. }
  518. static void backing_request_endio(struct bio *bio)
  519. {
  520. struct closure *cl = bio->bi_private;
  521. if (bio->bi_status) {
  522. struct search *s = container_of(cl, struct search, cl);
  523. struct cached_dev *dc = container_of(s->d,
  524. struct cached_dev, disk);
  525. /*
  526. * If a bio has REQ_PREFLUSH for writeback mode, it is
  527. * speically assembled in cached_dev_write() for a non-zero
  528. * write request which has REQ_PREFLUSH. we don't set
  529. * s->iop.status by this failure, the status will be decided
  530. * by result of bch_data_insert() operation.
  531. */
  532. if (unlikely(s->iop.writeback &&
  533. bio->bi_opf & REQ_PREFLUSH)) {
  534. pr_err("Can't flush %s: returned bi_status %i",
  535. dc->backing_dev_name, bio->bi_status);
  536. } else {
  537. /* set to orig_bio->bi_status in bio_complete() */
  538. s->iop.status = bio->bi_status;
  539. }
  540. s->recoverable = false;
  541. /* should count I/O error for backing device here */
  542. bch_count_backing_io_errors(dc, bio);
  543. }
  544. bio_put(bio);
  545. closure_put(cl);
  546. }
  547. static void bio_complete(struct search *s)
  548. {
  549. if (s->orig_bio) {
  550. generic_end_io_acct(s->d->disk->queue, bio_op(s->orig_bio),
  551. &s->d->disk->part0, s->start_time);
  552. trace_bcache_request_end(s->d, s->orig_bio);
  553. s->orig_bio->bi_status = s->iop.status;
  554. bio_endio(s->orig_bio);
  555. s->orig_bio = NULL;
  556. }
  557. }
  558. static void do_bio_hook(struct search *s,
  559. struct bio *orig_bio,
  560. bio_end_io_t *end_io_fn)
  561. {
  562. struct bio *bio = &s->bio.bio;
  563. bio_init(bio, NULL, 0);
  564. __bio_clone_fast(bio, orig_bio);
  565. /*
  566. * bi_end_io can be set separately somewhere else, e.g. the
  567. * variants in,
  568. * - cache_bio->bi_end_io from cached_dev_cache_miss()
  569. * - n->bi_end_io from cache_lookup_fn()
  570. */
  571. bio->bi_end_io = end_io_fn;
  572. bio->bi_private = &s->cl;
  573. bio_cnt_set(bio, 3);
  574. }
  575. static void search_free(struct closure *cl)
  576. {
  577. struct search *s = container_of(cl, struct search, cl);
  578. atomic_dec(&s->d->c->search_inflight);
  579. if (s->iop.bio)
  580. bio_put(s->iop.bio);
  581. bio_complete(s);
  582. closure_debug_destroy(cl);
  583. mempool_free(s, &s->d->c->search);
  584. }
  585. static inline struct search *search_alloc(struct bio *bio,
  586. struct bcache_device *d)
  587. {
  588. struct search *s;
  589. s = mempool_alloc(&d->c->search, GFP_NOIO);
  590. closure_init(&s->cl, NULL);
  591. do_bio_hook(s, bio, request_endio);
  592. atomic_inc(&d->c->search_inflight);
  593. s->orig_bio = bio;
  594. s->cache_miss = NULL;
  595. s->cache_missed = 0;
  596. s->d = d;
  597. s->recoverable = 1;
  598. s->write = op_is_write(bio_op(bio));
  599. s->read_dirty_data = 0;
  600. s->start_time = jiffies;
  601. s->iop.c = d->c;
  602. s->iop.bio = NULL;
  603. s->iop.inode = d->id;
  604. s->iop.write_point = hash_long((unsigned long) current, 16);
  605. s->iop.write_prio = 0;
  606. s->iop.status = 0;
  607. s->iop.flags = 0;
  608. s->iop.flush_journal = op_is_flush(bio->bi_opf);
  609. s->iop.wq = bcache_wq;
  610. return s;
  611. }
  612. /* Cached devices */
  613. static void cached_dev_bio_complete(struct closure *cl)
  614. {
  615. struct search *s = container_of(cl, struct search, cl);
  616. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  617. search_free(cl);
  618. cached_dev_put(dc);
  619. }
  620. /* Process reads */
  621. static void cached_dev_cache_miss_done(struct closure *cl)
  622. {
  623. struct search *s = container_of(cl, struct search, cl);
  624. if (s->iop.replace_collision)
  625. bch_mark_cache_miss_collision(s->iop.c, s->d);
  626. if (s->iop.bio)
  627. bio_free_pages(s->iop.bio);
  628. cached_dev_bio_complete(cl);
  629. }
  630. static void cached_dev_read_error(struct closure *cl)
  631. {
  632. struct search *s = container_of(cl, struct search, cl);
  633. struct bio *bio = &s->bio.bio;
  634. /*
  635. * If read request hit dirty data (s->read_dirty_data is true),
  636. * then recovery a failed read request from cached device may
  637. * get a stale data back. So read failure recovery is only
  638. * permitted when read request hit clean data in cache device,
  639. * or when cache read race happened.
  640. */
  641. if (s->recoverable && !s->read_dirty_data) {
  642. /* Retry from the backing device: */
  643. trace_bcache_read_retry(s->orig_bio);
  644. s->iop.status = 0;
  645. do_bio_hook(s, s->orig_bio, backing_request_endio);
  646. /* XXX: invalidate cache */
  647. /* I/O request sent to backing device */
  648. closure_bio_submit(s->iop.c, bio, cl);
  649. }
  650. continue_at(cl, cached_dev_cache_miss_done, NULL);
  651. }
  652. static void cached_dev_read_done(struct closure *cl)
  653. {
  654. struct search *s = container_of(cl, struct search, cl);
  655. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  656. /*
  657. * We had a cache miss; cache_bio now contains data ready to be inserted
  658. * into the cache.
  659. *
  660. * First, we copy the data we just read from cache_bio's bounce buffers
  661. * to the buffers the original bio pointed to:
  662. */
  663. if (s->iop.bio) {
  664. bio_reset(s->iop.bio);
  665. s->iop.bio->bi_iter.bi_sector =
  666. s->cache_miss->bi_iter.bi_sector;
  667. bio_copy_dev(s->iop.bio, s->cache_miss);
  668. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  669. bch_bio_map(s->iop.bio, NULL);
  670. bio_copy_data(s->cache_miss, s->iop.bio);
  671. bio_put(s->cache_miss);
  672. s->cache_miss = NULL;
  673. }
  674. if (verify(dc) && s->recoverable && !s->read_dirty_data)
  675. bch_data_verify(dc, s->orig_bio);
  676. bio_complete(s);
  677. if (s->iop.bio &&
  678. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  679. BUG_ON(!s->iop.replace);
  680. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  681. }
  682. continue_at(cl, cached_dev_cache_miss_done, NULL);
  683. }
  684. static void cached_dev_read_done_bh(struct closure *cl)
  685. {
  686. struct search *s = container_of(cl, struct search, cl);
  687. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  688. bch_mark_cache_accounting(s->iop.c, s->d,
  689. !s->cache_missed, s->iop.bypass);
  690. trace_bcache_read(s->orig_bio, !s->cache_missed, s->iop.bypass);
  691. if (s->iop.status)
  692. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  693. else if (s->iop.bio || verify(dc))
  694. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  695. else
  696. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  697. }
  698. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  699. struct bio *bio, unsigned int sectors)
  700. {
  701. int ret = MAP_CONTINUE;
  702. unsigned int reada = 0;
  703. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  704. struct bio *miss, *cache_bio;
  705. s->cache_missed = 1;
  706. if (s->cache_miss || s->iop.bypass) {
  707. miss = bio_next_split(bio, sectors, GFP_NOIO, &s->d->bio_split);
  708. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  709. goto out_submit;
  710. }
  711. if (!(bio->bi_opf & REQ_RAHEAD) &&
  712. !(bio->bi_opf & (REQ_META|REQ_PRIO)) &&
  713. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  714. reada = min_t(sector_t, dc->readahead >> 9,
  715. get_capacity(bio->bi_disk) - bio_end_sector(bio));
  716. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  717. s->iop.replace_key = KEY(s->iop.inode,
  718. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  719. s->insert_bio_sectors);
  720. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  721. if (ret)
  722. return ret;
  723. s->iop.replace = true;
  724. miss = bio_next_split(bio, sectors, GFP_NOIO, &s->d->bio_split);
  725. /* btree_search_recurse()'s btree iterator is no good anymore */
  726. ret = miss == bio ? MAP_DONE : -EINTR;
  727. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  728. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  729. &dc->disk.bio_split);
  730. if (!cache_bio)
  731. goto out_submit;
  732. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  733. bio_copy_dev(cache_bio, miss);
  734. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  735. cache_bio->bi_end_io = backing_request_endio;
  736. cache_bio->bi_private = &s->cl;
  737. bch_bio_map(cache_bio, NULL);
  738. if (bch_bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  739. goto out_put;
  740. if (reada)
  741. bch_mark_cache_readahead(s->iop.c, s->d);
  742. s->cache_miss = miss;
  743. s->iop.bio = cache_bio;
  744. bio_get(cache_bio);
  745. /* I/O request sent to backing device */
  746. closure_bio_submit(s->iop.c, cache_bio, &s->cl);
  747. return ret;
  748. out_put:
  749. bio_put(cache_bio);
  750. out_submit:
  751. miss->bi_end_io = backing_request_endio;
  752. miss->bi_private = &s->cl;
  753. /* I/O request sent to backing device */
  754. closure_bio_submit(s->iop.c, miss, &s->cl);
  755. return ret;
  756. }
  757. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  758. {
  759. struct closure *cl = &s->cl;
  760. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  761. continue_at(cl, cached_dev_read_done_bh, NULL);
  762. }
  763. /* Process writes */
  764. static void cached_dev_write_complete(struct closure *cl)
  765. {
  766. struct search *s = container_of(cl, struct search, cl);
  767. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  768. up_read_non_owner(&dc->writeback_lock);
  769. cached_dev_bio_complete(cl);
  770. }
  771. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  772. {
  773. struct closure *cl = &s->cl;
  774. struct bio *bio = &s->bio.bio;
  775. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  776. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  777. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  778. down_read_non_owner(&dc->writeback_lock);
  779. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  780. /*
  781. * We overlap with some dirty data undergoing background
  782. * writeback, force this write to writeback
  783. */
  784. s->iop.bypass = false;
  785. s->iop.writeback = true;
  786. }
  787. /*
  788. * Discards aren't _required_ to do anything, so skipping if
  789. * check_overlapping returned true is ok
  790. *
  791. * But check_overlapping drops dirty keys for which io hasn't started,
  792. * so we still want to call it.
  793. */
  794. if (bio_op(bio) == REQ_OP_DISCARD)
  795. s->iop.bypass = true;
  796. if (should_writeback(dc, s->orig_bio,
  797. cache_mode(dc),
  798. s->iop.bypass)) {
  799. s->iop.bypass = false;
  800. s->iop.writeback = true;
  801. }
  802. if (s->iop.bypass) {
  803. s->iop.bio = s->orig_bio;
  804. bio_get(s->iop.bio);
  805. if (bio_op(bio) == REQ_OP_DISCARD &&
  806. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  807. goto insert_data;
  808. /* I/O request sent to backing device */
  809. bio->bi_end_io = backing_request_endio;
  810. closure_bio_submit(s->iop.c, bio, cl);
  811. } else if (s->iop.writeback) {
  812. bch_writeback_add(dc);
  813. s->iop.bio = bio;
  814. if (bio->bi_opf & REQ_PREFLUSH) {
  815. /*
  816. * Also need to send a flush to the backing
  817. * device.
  818. */
  819. struct bio *flush;
  820. flush = bio_alloc_bioset(GFP_NOIO, 0,
  821. &dc->disk.bio_split);
  822. if (!flush) {
  823. s->iop.status = BLK_STS_RESOURCE;
  824. goto insert_data;
  825. }
  826. bio_copy_dev(flush, bio);
  827. flush->bi_end_io = backing_request_endio;
  828. flush->bi_private = cl;
  829. flush->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  830. /* I/O request sent to backing device */
  831. closure_bio_submit(s->iop.c, flush, cl);
  832. }
  833. } else {
  834. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, &dc->disk.bio_split);
  835. /* I/O request sent to backing device */
  836. bio->bi_end_io = backing_request_endio;
  837. closure_bio_submit(s->iop.c, bio, cl);
  838. }
  839. insert_data:
  840. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  841. continue_at(cl, cached_dev_write_complete, NULL);
  842. }
  843. static void cached_dev_nodata(struct closure *cl)
  844. {
  845. struct search *s = container_of(cl, struct search, cl);
  846. struct bio *bio = &s->bio.bio;
  847. if (s->iop.flush_journal)
  848. bch_journal_meta(s->iop.c, cl);
  849. /* If it's a flush, we send the flush to the backing device too */
  850. bio->bi_end_io = backing_request_endio;
  851. closure_bio_submit(s->iop.c, bio, cl);
  852. continue_at(cl, cached_dev_bio_complete, NULL);
  853. }
  854. struct detached_dev_io_private {
  855. struct bcache_device *d;
  856. unsigned long start_time;
  857. bio_end_io_t *bi_end_io;
  858. void *bi_private;
  859. };
  860. static void detached_dev_end_io(struct bio *bio)
  861. {
  862. struct detached_dev_io_private *ddip;
  863. ddip = bio->bi_private;
  864. bio->bi_end_io = ddip->bi_end_io;
  865. bio->bi_private = ddip->bi_private;
  866. generic_end_io_acct(ddip->d->disk->queue, bio_op(bio),
  867. &ddip->d->disk->part0, ddip->start_time);
  868. if (bio->bi_status) {
  869. struct cached_dev *dc = container_of(ddip->d,
  870. struct cached_dev, disk);
  871. /* should count I/O error for backing device here */
  872. bch_count_backing_io_errors(dc, bio);
  873. }
  874. kfree(ddip);
  875. bio->bi_end_io(bio);
  876. }
  877. static void detached_dev_do_request(struct bcache_device *d, struct bio *bio)
  878. {
  879. struct detached_dev_io_private *ddip;
  880. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  881. /*
  882. * no need to call closure_get(&dc->disk.cl),
  883. * because upper layer had already opened bcache device,
  884. * which would call closure_get(&dc->disk.cl)
  885. */
  886. ddip = kzalloc(sizeof(struct detached_dev_io_private), GFP_NOIO);
  887. ddip->d = d;
  888. ddip->start_time = jiffies;
  889. ddip->bi_end_io = bio->bi_end_io;
  890. ddip->bi_private = bio->bi_private;
  891. bio->bi_end_io = detached_dev_end_io;
  892. bio->bi_private = ddip;
  893. if ((bio_op(bio) == REQ_OP_DISCARD) &&
  894. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  895. bio->bi_end_io(bio);
  896. else
  897. generic_make_request(bio);
  898. }
  899. static void quit_max_writeback_rate(struct cache_set *c,
  900. struct cached_dev *this_dc)
  901. {
  902. int i;
  903. struct bcache_device *d;
  904. struct cached_dev *dc;
  905. /*
  906. * mutex bch_register_lock may compete with other parallel requesters,
  907. * or attach/detach operations on other backing device. Waiting to
  908. * the mutex lock may increase I/O request latency for seconds or more.
  909. * To avoid such situation, if mutext_trylock() failed, only writeback
  910. * rate of current cached device is set to 1, and __update_write_back()
  911. * will decide writeback rate of other cached devices (remember now
  912. * c->idle_counter is 0 already).
  913. */
  914. if (mutex_trylock(&bch_register_lock)) {
  915. for (i = 0; i < c->devices_max_used; i++) {
  916. if (!c->devices[i])
  917. continue;
  918. if (UUID_FLASH_ONLY(&c->uuids[i]))
  919. continue;
  920. d = c->devices[i];
  921. dc = container_of(d, struct cached_dev, disk);
  922. /*
  923. * set writeback rate to default minimum value,
  924. * then let update_writeback_rate() to decide the
  925. * upcoming rate.
  926. */
  927. atomic_long_set(&dc->writeback_rate.rate, 1);
  928. }
  929. mutex_unlock(&bch_register_lock);
  930. } else
  931. atomic_long_set(&this_dc->writeback_rate.rate, 1);
  932. }
  933. /* Cached devices - read & write stuff */
  934. static blk_qc_t cached_dev_make_request(struct request_queue *q,
  935. struct bio *bio)
  936. {
  937. struct search *s;
  938. struct bcache_device *d = bio->bi_disk->private_data;
  939. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  940. int rw = bio_data_dir(bio);
  941. if (unlikely((d->c && test_bit(CACHE_SET_IO_DISABLE, &d->c->flags)) ||
  942. dc->io_disable)) {
  943. bio->bi_status = BLK_STS_IOERR;
  944. bio_endio(bio);
  945. return BLK_QC_T_NONE;
  946. }
  947. if (likely(d->c)) {
  948. if (atomic_read(&d->c->idle_counter))
  949. atomic_set(&d->c->idle_counter, 0);
  950. /*
  951. * If at_max_writeback_rate of cache set is true and new I/O
  952. * comes, quit max writeback rate of all cached devices
  953. * attached to this cache set, and set at_max_writeback_rate
  954. * to false.
  955. */
  956. if (unlikely(atomic_read(&d->c->at_max_writeback_rate) == 1)) {
  957. atomic_set(&d->c->at_max_writeback_rate, 0);
  958. quit_max_writeback_rate(d->c, dc);
  959. }
  960. }
  961. generic_start_io_acct(q,
  962. bio_op(bio),
  963. bio_sectors(bio),
  964. &d->disk->part0);
  965. bio_set_dev(bio, dc->bdev);
  966. bio->bi_iter.bi_sector += dc->sb.data_offset;
  967. if (cached_dev_get(dc)) {
  968. s = search_alloc(bio, d);
  969. trace_bcache_request_start(s->d, bio);
  970. if (!bio->bi_iter.bi_size) {
  971. /*
  972. * can't call bch_journal_meta from under
  973. * generic_make_request
  974. */
  975. continue_at_nobarrier(&s->cl,
  976. cached_dev_nodata,
  977. bcache_wq);
  978. } else {
  979. s->iop.bypass = check_should_bypass(dc, bio);
  980. if (rw)
  981. cached_dev_write(dc, s);
  982. else
  983. cached_dev_read(dc, s);
  984. }
  985. } else
  986. /* I/O request sent to backing device */
  987. detached_dev_do_request(d, bio);
  988. return BLK_QC_T_NONE;
  989. }
  990. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  991. unsigned int cmd, unsigned long arg)
  992. {
  993. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  994. if (dc->io_disable)
  995. return -EIO;
  996. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  997. }
  998. static int cached_dev_congested(void *data, int bits)
  999. {
  1000. struct bcache_device *d = data;
  1001. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  1002. struct request_queue *q = bdev_get_queue(dc->bdev);
  1003. int ret = 0;
  1004. if (bdi_congested(q->backing_dev_info, bits))
  1005. return 1;
  1006. if (cached_dev_get(dc)) {
  1007. unsigned int i;
  1008. struct cache *ca;
  1009. for_each_cache(ca, d->c, i) {
  1010. q = bdev_get_queue(ca->bdev);
  1011. ret |= bdi_congested(q->backing_dev_info, bits);
  1012. }
  1013. cached_dev_put(dc);
  1014. }
  1015. return ret;
  1016. }
  1017. void bch_cached_dev_request_init(struct cached_dev *dc)
  1018. {
  1019. struct gendisk *g = dc->disk.disk;
  1020. g->queue->make_request_fn = cached_dev_make_request;
  1021. g->queue->backing_dev_info->congested_fn = cached_dev_congested;
  1022. dc->disk.cache_miss = cached_dev_cache_miss;
  1023. dc->disk.ioctl = cached_dev_ioctl;
  1024. }
  1025. /* Flash backed devices */
  1026. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  1027. struct bio *bio, unsigned int sectors)
  1028. {
  1029. unsigned int bytes = min(sectors, bio_sectors(bio)) << 9;
  1030. swap(bio->bi_iter.bi_size, bytes);
  1031. zero_fill_bio(bio);
  1032. swap(bio->bi_iter.bi_size, bytes);
  1033. bio_advance(bio, bytes);
  1034. if (!bio->bi_iter.bi_size)
  1035. return MAP_DONE;
  1036. return MAP_CONTINUE;
  1037. }
  1038. static void flash_dev_nodata(struct closure *cl)
  1039. {
  1040. struct search *s = container_of(cl, struct search, cl);
  1041. if (s->iop.flush_journal)
  1042. bch_journal_meta(s->iop.c, cl);
  1043. continue_at(cl, search_free, NULL);
  1044. }
  1045. static blk_qc_t flash_dev_make_request(struct request_queue *q,
  1046. struct bio *bio)
  1047. {
  1048. struct search *s;
  1049. struct closure *cl;
  1050. struct bcache_device *d = bio->bi_disk->private_data;
  1051. if (unlikely(d->c && test_bit(CACHE_SET_IO_DISABLE, &d->c->flags))) {
  1052. bio->bi_status = BLK_STS_IOERR;
  1053. bio_endio(bio);
  1054. return BLK_QC_T_NONE;
  1055. }
  1056. generic_start_io_acct(q, bio_op(bio), bio_sectors(bio), &d->disk->part0);
  1057. s = search_alloc(bio, d);
  1058. cl = &s->cl;
  1059. bio = &s->bio.bio;
  1060. trace_bcache_request_start(s->d, bio);
  1061. if (!bio->bi_iter.bi_size) {
  1062. /*
  1063. * can't call bch_journal_meta from under
  1064. * generic_make_request
  1065. */
  1066. continue_at_nobarrier(&s->cl,
  1067. flash_dev_nodata,
  1068. bcache_wq);
  1069. return BLK_QC_T_NONE;
  1070. } else if (bio_data_dir(bio)) {
  1071. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  1072. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  1073. &KEY(d->id, bio_end_sector(bio), 0));
  1074. s->iop.bypass = (bio_op(bio) == REQ_OP_DISCARD) != 0;
  1075. s->iop.writeback = true;
  1076. s->iop.bio = bio;
  1077. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  1078. } else {
  1079. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  1080. }
  1081. continue_at(cl, search_free, NULL);
  1082. return BLK_QC_T_NONE;
  1083. }
  1084. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1085. unsigned int cmd, unsigned long arg)
  1086. {
  1087. return -ENOTTY;
  1088. }
  1089. static int flash_dev_congested(void *data, int bits)
  1090. {
  1091. struct bcache_device *d = data;
  1092. struct request_queue *q;
  1093. struct cache *ca;
  1094. unsigned int i;
  1095. int ret = 0;
  1096. for_each_cache(ca, d->c, i) {
  1097. q = bdev_get_queue(ca->bdev);
  1098. ret |= bdi_congested(q->backing_dev_info, bits);
  1099. }
  1100. return ret;
  1101. }
  1102. void bch_flash_dev_request_init(struct bcache_device *d)
  1103. {
  1104. struct gendisk *g = d->disk;
  1105. g->queue->make_request_fn = flash_dev_make_request;
  1106. g->queue->backing_dev_info->congested_fn = flash_dev_congested;
  1107. d->cache_miss = flash_dev_cache_miss;
  1108. d->ioctl = flash_dev_ioctl;
  1109. }
  1110. void bch_request_exit(void)
  1111. {
  1112. kmem_cache_destroy(bch_search_cache);
  1113. }
  1114. int __init bch_request_init(void)
  1115. {
  1116. bch_search_cache = KMEM_CACHE(search, 0);
  1117. if (!bch_search_cache)
  1118. return -ENOMEM;
  1119. return 0;
  1120. }