request.c 35 KB

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