request.c 35 KB

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