request.c 34 KB

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