request.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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 CLOSURE_CALLBACK(bch_data_insert_start);
  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 CLOSURE_CALLBACK(bch_data_insert_keys)
  45. {
  46. closure_type(op, 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 CLOSURE_CALLBACK(bch_data_insert_error)
  110. {
  111. closure_type(op, 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->work);
  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 CLOSURE_CALLBACK(bch_data_insert_start)
  147. {
  148. closure_type(op, 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. n->bi_opf = REQ_OP_WRITE;
  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. CLOSURE_CALLBACK(bch_data_insert)
  249. {
  250. closure_type(op, 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->work);
  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. (bio_op(bio) == REQ_OP_DISCARD))
  298. goto skip;
  299. if (c->gc_stats.in_use > CUTOFF_CACHE_ADD) {
  300. /*
  301. * If cached buckets are all clean now, 'true' will be
  302. * returned and all requests will bypass the cache device.
  303. * Then c->sectors_to_gc has no chance to be negative, and
  304. * gc thread won't wake up and caching won't work forever.
  305. * Here call force_wake_up_gc() to avoid such aftermath.
  306. */
  307. if (BDEV_STATE(&dc->sb) == BDEV_STATE_CLEAN &&
  308. c->gc_mark_valid)
  309. force_wake_up_gc(c);
  310. goto skip;
  311. }
  312. if (mode == CACHE_MODE_NONE ||
  313. (mode == CACHE_MODE_WRITEAROUND &&
  314. op_is_write(bio_op(bio))))
  315. goto skip;
  316. /*
  317. * If the bio is for read-ahead or background IO, bypass it or
  318. * not depends on the following situations,
  319. * - If the IO is for meta data, always cache it and no bypass
  320. * - If the IO is not meta data, check dc->cache_reada_policy,
  321. * BCH_CACHE_READA_ALL: cache it and not bypass
  322. * BCH_CACHE_READA_META_ONLY: not cache it and bypass
  323. * That is, read-ahead request for metadata always get cached
  324. * (eg, for gfs2 or xfs).
  325. */
  326. if ((bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND))) {
  327. if (!(bio->bi_opf & (REQ_META|REQ_PRIO)) &&
  328. (dc->cache_readahead_policy != BCH_CACHE_READA_ALL))
  329. goto skip;
  330. }
  331. if (bio->bi_iter.bi_sector & (c->cache->sb.block_size - 1) ||
  332. bio_sectors(bio) & (c->cache->sb.block_size - 1)) {
  333. pr_debug("skipping unaligned io\n");
  334. goto skip;
  335. }
  336. if (bypass_torture_test(dc)) {
  337. if (get_random_u32_below(4) == 3)
  338. goto skip;
  339. else
  340. goto rescale;
  341. }
  342. congested = bch_get_congested(c);
  343. if (!congested && !dc->sequential_cutoff)
  344. goto rescale;
  345. spin_lock(&dc->io_lock);
  346. hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash)
  347. if (i->last == bio->bi_iter.bi_sector &&
  348. time_before(jiffies, i->jiffies))
  349. goto found;
  350. i = list_first_entry(&dc->io_lru, struct io, lru);
  351. add_sequential(task);
  352. i->sequential = 0;
  353. found:
  354. if (i->sequential + bio->bi_iter.bi_size > i->sequential)
  355. i->sequential += bio->bi_iter.bi_size;
  356. i->last = bio_end_sector(bio);
  357. i->jiffies = jiffies + msecs_to_jiffies(5000);
  358. task->sequential_io = i->sequential;
  359. hlist_del(&i->hash);
  360. hlist_add_head(&i->hash, iohash(dc, i->last));
  361. list_move_tail(&i->lru, &dc->io_lru);
  362. spin_unlock(&dc->io_lock);
  363. sectors = max(task->sequential_io,
  364. task->sequential_io_avg) >> 9;
  365. if (dc->sequential_cutoff &&
  366. sectors >= dc->sequential_cutoff >> 9) {
  367. trace_bcache_bypass_sequential(bio);
  368. goto skip;
  369. }
  370. if (congested && sectors >= congested) {
  371. trace_bcache_bypass_congested(bio);
  372. goto skip;
  373. }
  374. rescale:
  375. bch_rescale_priorities(c, bio_sectors(bio));
  376. return false;
  377. skip:
  378. bch_mark_sectors_bypassed(c, dc, bio_sectors(bio));
  379. return true;
  380. }
  381. /* Cache lookup */
  382. struct search {
  383. /* Stack frame for bio_complete */
  384. struct closure cl;
  385. struct bbio bio;
  386. struct bio *orig_bio;
  387. struct bio *cache_miss;
  388. struct bcache_device *d;
  389. unsigned int insert_bio_sectors;
  390. unsigned int recoverable:1;
  391. unsigned int write:1;
  392. unsigned int read_dirty_data:1;
  393. unsigned int cache_missed:1;
  394. struct block_device *orig_bdev;
  395. unsigned long start_time;
  396. struct btree_op op;
  397. struct data_insert_op iop;
  398. };
  399. static void bch_cache_read_endio(struct bio *bio)
  400. {
  401. struct bbio *b = container_of(bio, struct bbio, bio);
  402. struct closure *cl = bio->bi_private;
  403. struct search *s = container_of(cl, struct search, cl);
  404. /*
  405. * If the bucket was reused while our bio was in flight, we might have
  406. * read the wrong data. Set s->error but not error so it doesn't get
  407. * counted against the cache device, but we'll still reread the data
  408. * from the backing device.
  409. */
  410. if (bio->bi_status)
  411. s->iop.status = bio->bi_status;
  412. else if (!KEY_DIRTY(&b->key) &&
  413. ptr_stale(s->iop.c, &b->key, 0)) {
  414. atomic_long_inc(&s->iop.c->cache_read_races);
  415. s->iop.status = BLK_STS_IOERR;
  416. }
  417. bch_bbio_endio(s->iop.c, bio, bio->bi_status, "reading from cache");
  418. }
  419. /*
  420. * Read from a single key, handling the initial cache miss if the key starts in
  421. * the middle of the bio
  422. */
  423. static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
  424. {
  425. struct search *s = container_of(op, struct search, op);
  426. struct bio *n, *bio = &s->bio.bio;
  427. struct bkey *bio_key;
  428. unsigned int ptr;
  429. if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0)
  430. return MAP_CONTINUE;
  431. if (KEY_INODE(k) != s->iop.inode ||
  432. KEY_START(k) > bio->bi_iter.bi_sector) {
  433. unsigned int bio_sectors = bio_sectors(bio);
  434. unsigned int sectors = KEY_INODE(k) == s->iop.inode
  435. ? min_t(uint64_t, INT_MAX,
  436. KEY_START(k) - bio->bi_iter.bi_sector)
  437. : INT_MAX;
  438. int ret = s->d->cache_miss(b, s, bio, sectors);
  439. if (ret != MAP_CONTINUE)
  440. return ret;
  441. /* if this was a complete miss we shouldn't get here */
  442. BUG_ON(bio_sectors <= sectors);
  443. }
  444. if (!KEY_SIZE(k))
  445. return MAP_CONTINUE;
  446. /* XXX: figure out best pointer - for multiple cache devices */
  447. ptr = 0;
  448. PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
  449. if (KEY_DIRTY(k))
  450. s->read_dirty_data = true;
  451. n = bio_next_split(bio, min_t(uint64_t, INT_MAX,
  452. KEY_OFFSET(k) - bio->bi_iter.bi_sector),
  453. GFP_NOIO, &s->d->bio_split);
  454. bio_key = &container_of(n, struct bbio, bio)->key;
  455. bch_bkey_copy_single_ptr(bio_key, k, ptr);
  456. bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key);
  457. bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
  458. n->bi_end_io = bch_cache_read_endio;
  459. n->bi_private = &s->cl;
  460. /*
  461. * The bucket we're reading from might be reused while our bio
  462. * is in flight, and we could then end up reading the wrong
  463. * data.
  464. *
  465. * We guard against this by checking (in cache_read_endio()) if
  466. * the pointer is stale again; if so, we treat it as an error
  467. * and reread from the backing device (but we don't pass that
  468. * error up anywhere).
  469. */
  470. __bch_submit_bbio(n, b->c);
  471. return n == bio ? MAP_DONE : MAP_CONTINUE;
  472. }
  473. static CLOSURE_CALLBACK(cache_lookup)
  474. {
  475. closure_type(s, struct search, iop.cl);
  476. struct bio *bio = &s->bio.bio;
  477. struct cached_dev *dc;
  478. int ret;
  479. bch_btree_op_init(&s->op, -1);
  480. ret = bch_btree_map_keys(&s->op, s->iop.c,
  481. &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0),
  482. cache_lookup_fn, MAP_END_KEY);
  483. if (ret == -EAGAIN) {
  484. continue_at(cl, cache_lookup, bcache_wq);
  485. return;
  486. }
  487. /*
  488. * We might meet err when searching the btree, If that happens, we will
  489. * get negative ret, in this scenario we should not recover data from
  490. * backing device (when cache device is dirty) because we don't know
  491. * whether bkeys the read request covered are all clean.
  492. *
  493. * And after that happened, s->iop.status is still its initial value
  494. * before we submit s->bio.bio
  495. */
  496. if (ret < 0) {
  497. BUG_ON(ret == -EINTR);
  498. if (s->d && s->d->c &&
  499. !UUID_FLASH_ONLY(&s->d->c->uuids[s->d->id])) {
  500. dc = container_of(s->d, struct cached_dev, disk);
  501. if (dc && atomic_read(&dc->has_dirty))
  502. s->recoverable = false;
  503. }
  504. if (!s->iop.status)
  505. s->iop.status = BLK_STS_IOERR;
  506. }
  507. closure_return(cl);
  508. }
  509. /* Common code for the make_request functions */
  510. static void 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. s->iop.status = bio->bi_status;
  516. /* Only cache read errors are recoverable */
  517. s->recoverable = false;
  518. }
  519. bio_put(bio);
  520. closure_put(cl);
  521. }
  522. static void backing_request_endio(struct bio *bio)
  523. {
  524. struct closure *cl = bio->bi_private;
  525. if (bio->bi_status) {
  526. struct search *s = container_of(cl, struct search, cl);
  527. struct cached_dev *dc = container_of(s->d,
  528. struct cached_dev, disk);
  529. /*
  530. * If a bio has REQ_PREFLUSH for writeback mode, it is
  531. * speically assembled in cached_dev_write() for a non-zero
  532. * write request which has REQ_PREFLUSH. we don't set
  533. * s->iop.status by this failure, the status will be decided
  534. * by result of bch_data_insert() operation.
  535. */
  536. if (unlikely(s->iop.writeback &&
  537. bio->bi_opf & REQ_PREFLUSH)) {
  538. pr_err("Can't flush %pg: returned bi_status %i\n",
  539. dc->bdev, bio->bi_status);
  540. } else {
  541. /* set to orig_bio->bi_status in bio_complete() */
  542. s->iop.status = bio->bi_status;
  543. }
  544. s->recoverable = false;
  545. /* should count I/O error for backing device here */
  546. bch_count_backing_io_errors(dc, bio);
  547. }
  548. bio_put(bio);
  549. closure_put(cl);
  550. }
  551. static void bio_complete(struct search *s)
  552. {
  553. if (s->orig_bio) {
  554. /* Count on bcache device */
  555. bio_end_io_acct_remapped(s->orig_bio, s->start_time,
  556. s->orig_bdev);
  557. trace_bcache_request_end(s->d, s->orig_bio);
  558. s->orig_bio->bi_status = s->iop.status;
  559. bio_endio(s->orig_bio);
  560. s->orig_bio = NULL;
  561. }
  562. }
  563. static void do_bio_hook(struct search *s,
  564. struct bio *orig_bio,
  565. bio_end_io_t *end_io_fn)
  566. {
  567. struct bio *bio = &s->bio.bio;
  568. bio_init_clone(orig_bio->bi_bdev, bio, orig_bio, GFP_NOIO);
  569. /*
  570. * bi_end_io can be set separately somewhere else, e.g. the
  571. * variants in,
  572. * - cache_bio->bi_end_io from cached_dev_cache_miss()
  573. * - n->bi_end_io from cache_lookup_fn()
  574. */
  575. bio->bi_end_io = end_io_fn;
  576. bio->bi_private = &s->cl;
  577. bio_cnt_set(bio, 3);
  578. }
  579. static CLOSURE_CALLBACK(search_free)
  580. {
  581. closure_type(s, struct search, cl);
  582. atomic_dec(&s->iop.c->search_inflight);
  583. if (s->iop.bio)
  584. bio_put(s->iop.bio);
  585. bio_complete(s);
  586. closure_debug_destroy(cl);
  587. mempool_free(s, &s->iop.c->search);
  588. }
  589. static inline struct search *search_alloc(struct bio *bio,
  590. struct bcache_device *d, struct block_device *orig_bdev,
  591. unsigned long start_time)
  592. {
  593. struct search *s;
  594. s = mempool_alloc(&d->c->search, GFP_NOIO);
  595. closure_init(&s->cl, NULL);
  596. do_bio_hook(s, bio, request_endio);
  597. atomic_inc(&d->c->search_inflight);
  598. s->orig_bio = bio;
  599. s->cache_miss = NULL;
  600. s->cache_missed = 0;
  601. s->d = d;
  602. s->recoverable = 1;
  603. s->write = op_is_write(bio_op(bio));
  604. s->read_dirty_data = 0;
  605. /* Count on the bcache device */
  606. s->orig_bdev = orig_bdev;
  607. s->start_time = start_time;
  608. s->iop.c = d->c;
  609. s->iop.bio = NULL;
  610. s->iop.inode = d->id;
  611. s->iop.write_point = hash_long((unsigned long) current, 16);
  612. s->iop.write_prio = 0;
  613. s->iop.status = 0;
  614. s->iop.flags = 0;
  615. s->iop.flush_journal = op_is_flush(bio->bi_opf);
  616. s->iop.wq = bcache_wq;
  617. return s;
  618. }
  619. /* Cached devices */
  620. static CLOSURE_CALLBACK(cached_dev_bio_complete)
  621. {
  622. closure_type(s, struct search, cl);
  623. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  624. cached_dev_put(dc);
  625. search_free(&cl->work);
  626. }
  627. /* Process reads */
  628. static CLOSURE_CALLBACK(cached_dev_read_error_done)
  629. {
  630. closure_type(s, struct search, cl);
  631. if (s->iop.replace_collision)
  632. bch_mark_cache_miss_collision(s->iop.c, s->d);
  633. if (s->iop.bio)
  634. bio_free_pages(s->iop.bio);
  635. cached_dev_bio_complete(&cl->work);
  636. }
  637. static CLOSURE_CALLBACK(cached_dev_read_error)
  638. {
  639. closure_type(s, struct search, cl);
  640. struct bio *bio = &s->bio.bio;
  641. /*
  642. * If read request hit dirty data (s->read_dirty_data is true),
  643. * then recovery a failed read request from cached device may
  644. * get a stale data back. So read failure recovery is only
  645. * permitted when read request hit clean data in cache device,
  646. * or when cache read race happened.
  647. */
  648. if (s->recoverable && !s->read_dirty_data) {
  649. /* Retry from the backing device: */
  650. trace_bcache_read_retry(s->orig_bio);
  651. s->iop.status = 0;
  652. do_bio_hook(s, s->orig_bio, backing_request_endio);
  653. /* XXX: invalidate cache */
  654. /* I/O request sent to backing device */
  655. closure_bio_submit(s->iop.c, bio, cl);
  656. }
  657. continue_at(cl, cached_dev_read_error_done, NULL);
  658. }
  659. static CLOSURE_CALLBACK(cached_dev_cache_miss_done)
  660. {
  661. closure_type(s, struct search, cl);
  662. struct bcache_device *d = s->d;
  663. if (s->iop.replace_collision)
  664. bch_mark_cache_miss_collision(s->iop.c, s->d);
  665. if (s->iop.bio)
  666. bio_free_pages(s->iop.bio);
  667. cached_dev_bio_complete(&cl->work);
  668. closure_put(&d->cl);
  669. }
  670. static CLOSURE_CALLBACK(cached_dev_read_done)
  671. {
  672. closure_type(s, struct search, cl);
  673. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  674. /*
  675. * We had a cache miss; cache_bio now contains data ready to be inserted
  676. * into the cache.
  677. *
  678. * First, we copy the data we just read from cache_bio's bounce buffers
  679. * to the buffers the original bio pointed to:
  680. */
  681. if (s->iop.bio) {
  682. bio_reset(s->iop.bio, s->cache_miss->bi_bdev, REQ_OP_READ);
  683. s->iop.bio->bi_iter.bi_sector =
  684. s->cache_miss->bi_iter.bi_sector;
  685. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  686. bio_clone_blkg_association(s->iop.bio, s->cache_miss);
  687. bch_bio_map(s->iop.bio, NULL);
  688. bio_copy_data(s->cache_miss, s->iop.bio);
  689. bio_put(s->cache_miss);
  690. s->cache_miss = NULL;
  691. }
  692. if (verify(dc) && s->recoverable && !s->read_dirty_data)
  693. bch_data_verify(dc, s->orig_bio);
  694. closure_get(&dc->disk.cl);
  695. bio_complete(s);
  696. if (s->iop.bio &&
  697. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  698. BUG_ON(!s->iop.replace);
  699. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  700. }
  701. continue_at(cl, cached_dev_cache_miss_done, NULL);
  702. }
  703. static CLOSURE_CALLBACK(cached_dev_read_done_bh)
  704. {
  705. closure_type(s, struct search, cl);
  706. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  707. bch_mark_cache_accounting(s->iop.c, s->d,
  708. !s->cache_missed, s->iop.bypass);
  709. trace_bcache_read(s->orig_bio, !s->cache_missed, s->iop.bypass);
  710. if (s->iop.status)
  711. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  712. else if (s->iop.bio || verify(dc))
  713. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  714. else
  715. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  716. }
  717. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  718. struct bio *bio, unsigned int sectors)
  719. {
  720. int ret = MAP_CONTINUE;
  721. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  722. struct bio *miss, *cache_bio;
  723. unsigned int size_limit;
  724. s->cache_missed = 1;
  725. if (s->cache_miss || s->iop.bypass) {
  726. miss = bio_next_split(bio, sectors, GFP_NOIO, &s->d->bio_split);
  727. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  728. goto out_submit;
  729. }
  730. /* Limitation for valid replace key size and cache_bio bvecs number */
  731. size_limit = min_t(unsigned int, BIO_MAX_VECS * PAGE_SECTORS,
  732. (1 << KEY_SIZE_BITS) - 1);
  733. s->insert_bio_sectors = min3(size_limit, sectors, bio_sectors(bio));
  734. s->iop.replace_key = KEY(s->iop.inode,
  735. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  736. s->insert_bio_sectors);
  737. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  738. if (ret)
  739. return ret;
  740. s->iop.replace = true;
  741. miss = bio_next_split(bio, s->insert_bio_sectors, GFP_NOIO,
  742. &s->d->bio_split);
  743. /* btree_search_recurse()'s btree iterator is no good anymore */
  744. ret = miss == bio ? MAP_DONE : -EINTR;
  745. cache_bio = bio_alloc_bioset(miss->bi_bdev,
  746. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  747. 0, GFP_NOWAIT, &dc->disk.bio_split);
  748. if (!cache_bio)
  749. goto out_submit;
  750. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  751. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  752. cache_bio->bi_end_io = backing_request_endio;
  753. cache_bio->bi_private = &s->cl;
  754. bch_bio_map(cache_bio, NULL);
  755. if (bch_bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  756. goto out_put;
  757. s->cache_miss = miss;
  758. s->iop.bio = cache_bio;
  759. bio_get(cache_bio);
  760. /* I/O request sent to backing device */
  761. closure_bio_submit(s->iop.c, cache_bio, &s->cl);
  762. return ret;
  763. out_put:
  764. bio_put(cache_bio);
  765. out_submit:
  766. miss->bi_end_io = backing_request_endio;
  767. miss->bi_private = &s->cl;
  768. /* I/O request sent to backing device */
  769. closure_bio_submit(s->iop.c, miss, &s->cl);
  770. return ret;
  771. }
  772. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  773. {
  774. struct closure *cl = &s->cl;
  775. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  776. continue_at(cl, cached_dev_read_done_bh, NULL);
  777. }
  778. /* Process writes */
  779. static CLOSURE_CALLBACK(cached_dev_write_complete)
  780. {
  781. closure_type(s, struct search, cl);
  782. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  783. up_read_non_owner(&dc->writeback_lock);
  784. cached_dev_bio_complete(&cl->work);
  785. }
  786. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  787. {
  788. struct closure *cl = &s->cl;
  789. struct bio *bio = &s->bio.bio;
  790. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  791. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  792. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  793. down_read_non_owner(&dc->writeback_lock);
  794. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  795. /*
  796. * We overlap with some dirty data undergoing background
  797. * writeback, force this write to writeback
  798. */
  799. s->iop.bypass = false;
  800. s->iop.writeback = true;
  801. }
  802. /*
  803. * Discards aren't _required_ to do anything, so skipping if
  804. * check_overlapping returned true is ok
  805. *
  806. * But check_overlapping drops dirty keys for which io hasn't started,
  807. * so we still want to call it.
  808. */
  809. if (bio_op(bio) == REQ_OP_DISCARD)
  810. s->iop.bypass = true;
  811. if (should_writeback(dc, s->orig_bio,
  812. cache_mode(dc),
  813. s->iop.bypass)) {
  814. s->iop.bypass = false;
  815. s->iop.writeback = true;
  816. }
  817. if (s->iop.bypass) {
  818. s->iop.bio = s->orig_bio;
  819. bio_get(s->iop.bio);
  820. if (bio_op(bio) == REQ_OP_DISCARD &&
  821. !bdev_max_discard_sectors(dc->bdev))
  822. goto insert_data;
  823. /* I/O request sent to backing device */
  824. bio->bi_end_io = backing_request_endio;
  825. closure_bio_submit(s->iop.c, bio, cl);
  826. } else if (s->iop.writeback) {
  827. bch_writeback_add(dc);
  828. s->iop.bio = bio;
  829. if (bio->bi_opf & REQ_PREFLUSH) {
  830. /*
  831. * Also need to send a flush to the backing
  832. * device.
  833. */
  834. struct bio *flush;
  835. flush = bio_alloc_bioset(bio->bi_bdev, 0,
  836. REQ_OP_WRITE | REQ_PREFLUSH,
  837. GFP_NOIO, &dc->disk.bio_split);
  838. if (!flush) {
  839. s->iop.status = BLK_STS_RESOURCE;
  840. goto insert_data;
  841. }
  842. flush->bi_end_io = backing_request_endio;
  843. flush->bi_private = cl;
  844. /* I/O request sent to backing device */
  845. closure_bio_submit(s->iop.c, flush, cl);
  846. }
  847. } else {
  848. s->iop.bio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
  849. &dc->disk.bio_split);
  850. /* I/O request sent to backing device */
  851. bio->bi_end_io = backing_request_endio;
  852. closure_bio_submit(s->iop.c, bio, cl);
  853. }
  854. insert_data:
  855. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  856. continue_at(cl, cached_dev_write_complete, NULL);
  857. }
  858. static CLOSURE_CALLBACK(cached_dev_nodata)
  859. {
  860. closure_type(s, struct search, cl);
  861. struct bio *bio = &s->bio.bio;
  862. if (s->iop.flush_journal)
  863. bch_journal_meta(s->iop.c, cl);
  864. /* If it's a flush, we send the flush to the backing device too */
  865. bio->bi_end_io = backing_request_endio;
  866. closure_bio_submit(s->iop.c, bio, cl);
  867. continue_at(cl, cached_dev_bio_complete, NULL);
  868. }
  869. static void detached_dev_end_io(struct bio *bio)
  870. {
  871. struct detached_dev_io_private *ddip =
  872. container_of(bio, struct detached_dev_io_private, bio);
  873. struct bio *orig_bio = ddip->orig_bio;
  874. /* Count on the bcache device */
  875. bio_end_io_acct(orig_bio, ddip->start_time);
  876. if (bio->bi_status) {
  877. struct cached_dev *dc = bio->bi_private;
  878. /* should count I/O error for backing device here */
  879. bch_count_backing_io_errors(dc, bio);
  880. orig_bio->bi_status = bio->bi_status;
  881. }
  882. bio_put(bio);
  883. bio_endio(orig_bio);
  884. }
  885. static void detached_dev_do_request(struct bcache_device *d,
  886. struct bio *orig_bio, unsigned long start_time)
  887. {
  888. struct detached_dev_io_private *ddip;
  889. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  890. struct bio *clone_bio;
  891. if (bio_op(orig_bio) == REQ_OP_DISCARD &&
  892. !bdev_max_discard_sectors(dc->bdev)) {
  893. bio_end_io_acct(orig_bio, start_time);
  894. bio_endio(orig_bio);
  895. return;
  896. }
  897. clone_bio = bio_alloc_clone(dc->bdev, orig_bio, GFP_NOIO,
  898. &d->bio_detached);
  899. ddip = container_of(clone_bio, struct detached_dev_io_private, bio);
  900. /* Count on the bcache device */
  901. ddip->d = d;
  902. ddip->start_time = start_time;
  903. ddip->orig_bio = orig_bio;
  904. clone_bio->bi_end_io = detached_dev_end_io;
  905. clone_bio->bi_private = dc;
  906. submit_bio_noacct(clone_bio);
  907. }
  908. static void quit_max_writeback_rate(struct cache_set *c,
  909. struct cached_dev *this_dc)
  910. {
  911. int i;
  912. struct bcache_device *d;
  913. struct cached_dev *dc;
  914. /*
  915. * mutex bch_register_lock may compete with other parallel requesters,
  916. * or attach/detach operations on other backing device. Waiting to
  917. * the mutex lock may increase I/O request latency for seconds or more.
  918. * To avoid such situation, if mutext_trylock() failed, only writeback
  919. * rate of current cached device is set to 1, and __update_write_back()
  920. * will decide writeback rate of other cached devices (remember now
  921. * c->idle_counter is 0 already).
  922. */
  923. if (mutex_trylock(&bch_register_lock)) {
  924. for (i = 0; i < c->devices_max_used; i++) {
  925. if (!c->devices[i])
  926. continue;
  927. if (UUID_FLASH_ONLY(&c->uuids[i]))
  928. continue;
  929. d = c->devices[i];
  930. dc = container_of(d, struct cached_dev, disk);
  931. /*
  932. * set writeback rate to default minimum value,
  933. * then let update_writeback_rate() to decide the
  934. * upcoming rate.
  935. */
  936. atomic_long_set(&dc->writeback_rate.rate, 1);
  937. }
  938. mutex_unlock(&bch_register_lock);
  939. } else
  940. atomic_long_set(&this_dc->writeback_rate.rate, 1);
  941. }
  942. /* Cached devices - read & write stuff */
  943. void cached_dev_submit_bio(struct bio *bio)
  944. {
  945. struct search *s;
  946. struct block_device *orig_bdev = bio->bi_bdev;
  947. struct bcache_device *d = orig_bdev->bd_disk->private_data;
  948. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  949. unsigned long start_time;
  950. int rw = bio_data_dir(bio);
  951. if (unlikely((d->c && test_bit(CACHE_SET_IO_DISABLE, &d->c->flags)) ||
  952. dc->io_disable)) {
  953. bio->bi_status = BLK_STS_IOERR;
  954. bio_endio(bio);
  955. return;
  956. }
  957. if (likely(d->c)) {
  958. if (atomic_read(&d->c->idle_counter))
  959. atomic_set(&d->c->idle_counter, 0);
  960. /*
  961. * If at_max_writeback_rate of cache set is true and new I/O
  962. * comes, quit max writeback rate of all cached devices
  963. * attached to this cache set, and set at_max_writeback_rate
  964. * to false.
  965. */
  966. if (unlikely(atomic_read(&d->c->at_max_writeback_rate) == 1)) {
  967. atomic_set(&d->c->at_max_writeback_rate, 0);
  968. quit_max_writeback_rate(d->c, dc);
  969. }
  970. }
  971. start_time = bio_start_io_acct(bio);
  972. bio->bi_iter.bi_sector += dc->sb.data_offset;
  973. if (cached_dev_get(dc)) {
  974. bio_set_dev(bio, dc->bdev);
  975. s = search_alloc(bio, d, orig_bdev, start_time);
  976. trace_bcache_request_start(s->d, bio);
  977. if (!bio->bi_iter.bi_size) {
  978. /*
  979. * can't call bch_journal_meta from under
  980. * submit_bio_noacct
  981. */
  982. continue_at_nobarrier(&s->cl,
  983. cached_dev_nodata,
  984. bcache_wq);
  985. } else {
  986. s->iop.bypass = check_should_bypass(dc, bio);
  987. if (rw)
  988. cached_dev_write(dc, s);
  989. else
  990. cached_dev_read(dc, s);
  991. }
  992. } else {
  993. /* I/O request sent to backing device */
  994. detached_dev_do_request(d, bio, start_time);
  995. }
  996. }
  997. static int cached_dev_ioctl(struct bcache_device *d, blk_mode_t mode,
  998. unsigned int cmd, unsigned long arg)
  999. {
  1000. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  1001. if (dc->io_disable)
  1002. return -EIO;
  1003. if (!dc->bdev->bd_disk->fops->ioctl)
  1004. return -ENOTTY;
  1005. return dc->bdev->bd_disk->fops->ioctl(dc->bdev, mode, cmd, arg);
  1006. }
  1007. void bch_cached_dev_request_init(struct cached_dev *dc)
  1008. {
  1009. dc->disk.cache_miss = cached_dev_cache_miss;
  1010. dc->disk.ioctl = cached_dev_ioctl;
  1011. }
  1012. /* Flash backed devices */
  1013. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  1014. struct bio *bio, unsigned int sectors)
  1015. {
  1016. unsigned int bytes = min(sectors, bio_sectors(bio)) << 9;
  1017. swap(bio->bi_iter.bi_size, bytes);
  1018. zero_fill_bio(bio);
  1019. swap(bio->bi_iter.bi_size, bytes);
  1020. bio_advance(bio, bytes);
  1021. if (!bio->bi_iter.bi_size)
  1022. return MAP_DONE;
  1023. return MAP_CONTINUE;
  1024. }
  1025. static CLOSURE_CALLBACK(flash_dev_nodata)
  1026. {
  1027. closure_type(s, struct search, cl);
  1028. if (s->iop.flush_journal)
  1029. bch_journal_meta(s->iop.c, cl);
  1030. continue_at(cl, search_free, NULL);
  1031. }
  1032. void flash_dev_submit_bio(struct bio *bio)
  1033. {
  1034. struct search *s;
  1035. struct closure *cl;
  1036. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  1037. if (unlikely(d->c && test_bit(CACHE_SET_IO_DISABLE, &d->c->flags))) {
  1038. bio->bi_status = BLK_STS_IOERR;
  1039. bio_endio(bio);
  1040. return;
  1041. }
  1042. s = search_alloc(bio, d, bio->bi_bdev, bio_start_io_acct(bio));
  1043. cl = &s->cl;
  1044. bio = &s->bio.bio;
  1045. trace_bcache_request_start(s->d, bio);
  1046. if (!bio->bi_iter.bi_size) {
  1047. /*
  1048. * can't call bch_journal_meta from under submit_bio_noacct
  1049. */
  1050. continue_at_nobarrier(&s->cl,
  1051. flash_dev_nodata,
  1052. bcache_wq);
  1053. return;
  1054. } else if (bio_data_dir(bio)) {
  1055. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  1056. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  1057. &KEY(d->id, bio_end_sector(bio), 0));
  1058. s->iop.bypass = (bio_op(bio) == REQ_OP_DISCARD) != 0;
  1059. s->iop.writeback = true;
  1060. s->iop.bio = bio;
  1061. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  1062. } else {
  1063. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  1064. }
  1065. continue_at(cl, search_free, NULL);
  1066. }
  1067. static int flash_dev_ioctl(struct bcache_device *d, blk_mode_t mode,
  1068. unsigned int cmd, unsigned long arg)
  1069. {
  1070. return -ENOTTY;
  1071. }
  1072. void bch_flash_dev_request_init(struct bcache_device *d)
  1073. {
  1074. d->cache_miss = flash_dev_cache_miss;
  1075. d->ioctl = flash_dev_ioctl;
  1076. }
  1077. void bch_request_exit(void)
  1078. {
  1079. kmem_cache_destroy(bch_search_cache);
  1080. }
  1081. int __init bch_request_init(void)
  1082. {
  1083. bch_search_cache = KMEM_CACHE(search, 0);
  1084. if (!bch_search_cache)
  1085. return -ENOMEM;
  1086. return 0;
  1087. }