request.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 *);
  23. static unsigned 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 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. * bio_insert() and bio_invalidate() 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 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, bio->bi_iter.bi_sector, sectors));
  111. }
  112. op->insert_data_done = true;
  113. bio_put(bio);
  114. out:
  115. continue_at(cl, bch_data_insert_keys, op->wq);
  116. }
  117. static void bch_data_insert_error(struct closure *cl)
  118. {
  119. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  120. /*
  121. * Our data write just errored, which means we've got a bunch of keys to
  122. * insert that point to data that wasn't succesfully written.
  123. *
  124. * We don't have to insert those keys but we still have to invalidate
  125. * that region of the cache - so, if we just strip off all the pointers
  126. * from the keys we'll accomplish just that.
  127. */
  128. struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys;
  129. while (src != op->insert_keys.top) {
  130. struct bkey *n = bkey_next(src);
  131. SET_KEY_PTRS(src, 0);
  132. memmove(dst, src, bkey_bytes(src));
  133. dst = bkey_next(dst);
  134. src = n;
  135. }
  136. op->insert_keys.top = dst;
  137. bch_data_insert_keys(cl);
  138. }
  139. static void bch_data_insert_endio(struct bio *bio)
  140. {
  141. struct closure *cl = bio->bi_private;
  142. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  143. if (bio->bi_status) {
  144. /* TODO: We could try to recover from this. */
  145. if (op->writeback)
  146. op->status = bio->bi_status;
  147. else if (!op->replace)
  148. set_closure_fn(cl, bch_data_insert_error, op->wq);
  149. else
  150. set_closure_fn(cl, NULL, NULL);
  151. }
  152. bch_bbio_endio(op->c, bio, bio->bi_status, "writing data to cache");
  153. }
  154. static void bch_data_insert_start(struct closure *cl)
  155. {
  156. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  157. struct bio *bio = op->bio, *n;
  158. if (op->bypass)
  159. return bch_data_invalidate(cl);
  160. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0)
  161. wake_up_gc(op->c);
  162. /*
  163. * Journal writes are marked REQ_PREFLUSH; if the original write was a
  164. * flush, it'll wait on the journal write.
  165. */
  166. bio->bi_opf &= ~(REQ_PREFLUSH|REQ_FUA);
  167. do {
  168. unsigned i;
  169. struct bkey *k;
  170. struct bio_set *split = op->c->bio_split;
  171. /* 1 for the device pointer and 1 for the chksum */
  172. if (bch_keylist_realloc(&op->insert_keys,
  173. 3 + (op->csum ? 1 : 0),
  174. op->c)) {
  175. continue_at(cl, bch_data_insert_keys, op->wq);
  176. return;
  177. }
  178. k = op->insert_keys.top;
  179. bkey_init(k);
  180. SET_KEY_INODE(k, op->inode);
  181. SET_KEY_OFFSET(k, bio->bi_iter.bi_sector);
  182. if (!bch_alloc_sectors(op->c, k, bio_sectors(bio),
  183. op->write_point, op->write_prio,
  184. op->writeback))
  185. goto err;
  186. n = bio_next_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  187. n->bi_end_io = bch_data_insert_endio;
  188. n->bi_private = cl;
  189. if (op->writeback) {
  190. SET_KEY_DIRTY(k, true);
  191. for (i = 0; i < KEY_PTRS(k); i++)
  192. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  193. GC_MARK_DIRTY);
  194. }
  195. SET_KEY_CSUM(k, op->csum);
  196. if (KEY_CSUM(k))
  197. bio_csum(n, k);
  198. trace_bcache_cache_insert(k);
  199. bch_keylist_push(&op->insert_keys);
  200. bio_set_op_attrs(n, REQ_OP_WRITE, 0);
  201. bch_submit_bbio(n, op->c, k, 0);
  202. } while (n != bio);
  203. op->insert_data_done = true;
  204. continue_at(cl, bch_data_insert_keys, op->wq);
  205. return;
  206. err:
  207. /* bch_alloc_sectors() blocks if s->writeback = true */
  208. BUG_ON(op->writeback);
  209. /*
  210. * But if it's not a writeback write we'd rather just bail out if
  211. * there aren't any buckets ready to write to - it might take awhile and
  212. * we might be starving btree writes for gc or something.
  213. */
  214. if (!op->replace) {
  215. /*
  216. * Writethrough write: We can't complete the write until we've
  217. * updated the index. But we don't want to delay the write while
  218. * we wait for buckets to be freed up, so just invalidate the
  219. * rest of the write.
  220. */
  221. op->bypass = true;
  222. return bch_data_invalidate(cl);
  223. } else {
  224. /*
  225. * From a cache miss, we can just insert the keys for the data
  226. * we have written or bail out if we didn't do anything.
  227. */
  228. op->insert_data_done = true;
  229. bio_put(bio);
  230. if (!bch_keylist_empty(&op->insert_keys))
  231. continue_at(cl, bch_data_insert_keys, op->wq);
  232. else
  233. closure_return(cl);
  234. }
  235. }
  236. /**
  237. * bch_data_insert - stick some data in the cache
  238. *
  239. * This is the starting point for any data to end up in a cache device; it could
  240. * be from a normal write, or a writeback write, or a write to a flash only
  241. * volume - it's also used by the moving garbage collector to compact data in
  242. * mostly empty buckets.
  243. *
  244. * It first writes the data to the cache, creating a list of keys to be inserted
  245. * (if the data had to be fragmented there will be multiple keys); after the
  246. * data is written it calls bch_journal, and after the keys have been added to
  247. * the next journal write they're inserted into the btree.
  248. *
  249. * It inserts the data in s->cache_bio; bi_sector is used for the key offset,
  250. * and op->inode is used for the key inode.
  251. *
  252. * If s->bypass is true, instead of inserting the data it invalidates the
  253. * region of the cache represented by s->cache_bio and op->inode.
  254. */
  255. void bch_data_insert(struct closure *cl)
  256. {
  257. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  258. trace_bcache_write(op->c, op->inode, op->bio,
  259. op->writeback, op->bypass);
  260. bch_keylist_init(&op->insert_keys);
  261. bio_get(op->bio);
  262. bch_data_insert_start(cl);
  263. }
  264. /* Congested? */
  265. unsigned bch_get_congested(struct cache_set *c)
  266. {
  267. int i;
  268. long rand;
  269. if (!c->congested_read_threshold_us &&
  270. !c->congested_write_threshold_us)
  271. return 0;
  272. i = (local_clock_us() - c->congested_last_us) / 1024;
  273. if (i < 0)
  274. return 0;
  275. i += atomic_read(&c->congested);
  276. if (i >= 0)
  277. return 0;
  278. i += CONGESTED_MAX;
  279. if (i > 0)
  280. i = fract_exp_two(i, 6);
  281. rand = get_random_int();
  282. i -= bitmap_weight(&rand, BITS_PER_LONG);
  283. return i > 0 ? i : 1;
  284. }
  285. static void add_sequential(struct task_struct *t)
  286. {
  287. ewma_add(t->sequential_io_avg,
  288. t->sequential_io, 8, 0);
  289. t->sequential_io = 0;
  290. }
  291. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  292. {
  293. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  294. }
  295. static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
  296. {
  297. struct cache_set *c = dc->disk.c;
  298. unsigned mode = cache_mode(dc);
  299. unsigned sectors, congested = bch_get_congested(c);
  300. struct task_struct *task = current;
  301. struct io *i;
  302. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  303. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  304. (bio_op(bio) == REQ_OP_DISCARD))
  305. goto skip;
  306. if (mode == CACHE_MODE_NONE ||
  307. (mode == CACHE_MODE_WRITEAROUND &&
  308. op_is_write(bio_op(bio))))
  309. goto skip;
  310. /*
  311. * Flag for bypass if the IO is for read-ahead or background,
  312. * unless the read-ahead request is for metadata (eg, for gfs2).
  313. */
  314. if (bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND) &&
  315. !(bio->bi_opf & REQ_META))
  316. goto skip;
  317. if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||
  318. bio_sectors(bio) & (c->sb.block_size - 1)) {
  319. pr_debug("skipping unaligned io");
  320. goto skip;
  321. }
  322. if (bypass_torture_test(dc)) {
  323. if ((get_random_int() & 3) == 3)
  324. goto skip;
  325. else
  326. goto rescale;
  327. }
  328. if (!congested && !dc->sequential_cutoff)
  329. goto rescale;
  330. spin_lock(&dc->io_lock);
  331. hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash)
  332. if (i->last == bio->bi_iter.bi_sector &&
  333. time_before(jiffies, i->jiffies))
  334. goto found;
  335. i = list_first_entry(&dc->io_lru, struct io, lru);
  336. add_sequential(task);
  337. i->sequential = 0;
  338. found:
  339. if (i->sequential + bio->bi_iter.bi_size > i->sequential)
  340. i->sequential += bio->bi_iter.bi_size;
  341. i->last = bio_end_sector(bio);
  342. i->jiffies = jiffies + msecs_to_jiffies(5000);
  343. task->sequential_io = i->sequential;
  344. hlist_del(&i->hash);
  345. hlist_add_head(&i->hash, iohash(dc, i->last));
  346. list_move_tail(&i->lru, &dc->io_lru);
  347. spin_unlock(&dc->io_lock);
  348. sectors = max(task->sequential_io,
  349. task->sequential_io_avg) >> 9;
  350. if (dc->sequential_cutoff &&
  351. sectors >= dc->sequential_cutoff >> 9) {
  352. trace_bcache_bypass_sequential(bio);
  353. goto skip;
  354. }
  355. if (congested && sectors >= congested) {
  356. trace_bcache_bypass_congested(bio);
  357. goto skip;
  358. }
  359. rescale:
  360. bch_rescale_priorities(c, bio_sectors(bio));
  361. return false;
  362. skip:
  363. bch_mark_sectors_bypassed(c, dc, bio_sectors(bio));
  364. return true;
  365. }
  366. /* Cache lookup */
  367. struct search {
  368. /* Stack frame for bio_complete */
  369. struct closure cl;
  370. struct bbio bio;
  371. struct bio *orig_bio;
  372. struct bio *cache_miss;
  373. struct bcache_device *d;
  374. unsigned insert_bio_sectors;
  375. unsigned recoverable:1;
  376. unsigned write:1;
  377. unsigned read_dirty_data:1;
  378. unsigned cache_missed:1;
  379. unsigned long start_time;
  380. struct btree_op op;
  381. struct data_insert_op iop;
  382. };
  383. static void bch_cache_read_endio(struct bio *bio)
  384. {
  385. struct bbio *b = container_of(bio, struct bbio, bio);
  386. struct closure *cl = bio->bi_private;
  387. struct search *s = container_of(cl, struct search, cl);
  388. /*
  389. * If the bucket was reused while our bio was in flight, we might have
  390. * read the wrong data. Set s->error but not error so it doesn't get
  391. * counted against the cache device, but we'll still reread the data
  392. * from the backing device.
  393. */
  394. if (bio->bi_status)
  395. s->iop.status = bio->bi_status;
  396. else if (!KEY_DIRTY(&b->key) &&
  397. ptr_stale(s->iop.c, &b->key, 0)) {
  398. atomic_long_inc(&s->iop.c->cache_read_races);
  399. s->iop.status = BLK_STS_IOERR;
  400. }
  401. bch_bbio_endio(s->iop.c, bio, bio->bi_status, "reading from cache");
  402. }
  403. /*
  404. * Read from a single key, handling the initial cache miss if the key starts in
  405. * the middle of the bio
  406. */
  407. static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
  408. {
  409. struct search *s = container_of(op, struct search, op);
  410. struct bio *n, *bio = &s->bio.bio;
  411. struct bkey *bio_key;
  412. unsigned ptr;
  413. if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0)
  414. return MAP_CONTINUE;
  415. if (KEY_INODE(k) != s->iop.inode ||
  416. KEY_START(k) > bio->bi_iter.bi_sector) {
  417. unsigned bio_sectors = bio_sectors(bio);
  418. unsigned sectors = KEY_INODE(k) == s->iop.inode
  419. ? min_t(uint64_t, INT_MAX,
  420. KEY_START(k) - bio->bi_iter.bi_sector)
  421. : INT_MAX;
  422. int ret = s->d->cache_miss(b, s, bio, sectors);
  423. if (ret != MAP_CONTINUE)
  424. return ret;
  425. /* if this was a complete miss we shouldn't get here */
  426. BUG_ON(bio_sectors <= sectors);
  427. }
  428. if (!KEY_SIZE(k))
  429. return MAP_CONTINUE;
  430. /* XXX: figure out best pointer - for multiple cache devices */
  431. ptr = 0;
  432. PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
  433. if (KEY_DIRTY(k))
  434. s->read_dirty_data = true;
  435. n = bio_next_split(bio, min_t(uint64_t, INT_MAX,
  436. KEY_OFFSET(k) - bio->bi_iter.bi_sector),
  437. GFP_NOIO, s->d->bio_split);
  438. bio_key = &container_of(n, struct bbio, bio)->key;
  439. bch_bkey_copy_single_ptr(bio_key, k, ptr);
  440. bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key);
  441. bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
  442. n->bi_end_io = bch_cache_read_endio;
  443. n->bi_private = &s->cl;
  444. /*
  445. * The bucket we're reading from might be reused while our bio
  446. * is in flight, and we could then end up reading the wrong
  447. * data.
  448. *
  449. * We guard against this by checking (in cache_read_endio()) if
  450. * the pointer is stale again; if so, we treat it as an error
  451. * and reread from the backing device (but we don't pass that
  452. * error up anywhere).
  453. */
  454. __bch_submit_bbio(n, b->c);
  455. return n == bio ? MAP_DONE : MAP_CONTINUE;
  456. }
  457. static void cache_lookup(struct closure *cl)
  458. {
  459. struct search *s = container_of(cl, struct search, iop.cl);
  460. struct bio *bio = &s->bio.bio;
  461. struct cached_dev *dc;
  462. int ret;
  463. bch_btree_op_init(&s->op, -1);
  464. ret = bch_btree_map_keys(&s->op, s->iop.c,
  465. &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0),
  466. cache_lookup_fn, MAP_END_KEY);
  467. if (ret == -EAGAIN) {
  468. continue_at(cl, cache_lookup, bcache_wq);
  469. return;
  470. }
  471. /*
  472. * We might meet err when searching the btree, If that happens, we will
  473. * get negative ret, in this scenario we should not recover data from
  474. * backing device (when cache device is dirty) because we don't know
  475. * whether bkeys the read request covered are all clean.
  476. *
  477. * And after that happened, s->iop.status is still its initial value
  478. * before we submit s->bio.bio
  479. */
  480. if (ret < 0) {
  481. BUG_ON(ret == -EINTR);
  482. if (s->d && s->d->c &&
  483. !UUID_FLASH_ONLY(&s->d->c->uuids[s->d->id])) {
  484. dc = container_of(s->d, struct cached_dev, disk);
  485. if (dc && atomic_read(&dc->has_dirty))
  486. s->recoverable = false;
  487. }
  488. if (!s->iop.status)
  489. s->iop.status = BLK_STS_IOERR;
  490. }
  491. closure_return(cl);
  492. }
  493. /* Common code for the make_request functions */
  494. static void request_endio(struct bio *bio)
  495. {
  496. struct closure *cl = bio->bi_private;
  497. if (bio->bi_status) {
  498. struct search *s = container_of(cl, struct search, cl);
  499. s->iop.status = bio->bi_status;
  500. /* Only cache read errors are recoverable */
  501. s->recoverable = false;
  502. }
  503. bio_put(bio);
  504. closure_put(cl);
  505. }
  506. static void bio_complete(struct search *s)
  507. {
  508. if (s->orig_bio) {
  509. generic_end_io_acct(s->d->disk->queue,
  510. bio_data_dir(s->orig_bio),
  511. &s->d->disk->part0, s->start_time);
  512. trace_bcache_request_end(s->d, s->orig_bio);
  513. s->orig_bio->bi_status = s->iop.status;
  514. bio_endio(s->orig_bio);
  515. s->orig_bio = NULL;
  516. }
  517. }
  518. static void do_bio_hook(struct search *s, struct bio *orig_bio)
  519. {
  520. struct bio *bio = &s->bio.bio;
  521. bio_init(bio, NULL, 0);
  522. __bio_clone_fast(bio, orig_bio);
  523. bio->bi_end_io = request_endio;
  524. bio->bi_private = &s->cl;
  525. bio_cnt_set(bio, 3);
  526. }
  527. static void search_free(struct closure *cl)
  528. {
  529. struct search *s = container_of(cl, struct search, cl);
  530. bio_complete(s);
  531. if (s->iop.bio)
  532. bio_put(s->iop.bio);
  533. closure_debug_destroy(cl);
  534. mempool_free(s, s->d->c->search);
  535. }
  536. static inline struct search *search_alloc(struct bio *bio,
  537. struct bcache_device *d)
  538. {
  539. struct search *s;
  540. s = mempool_alloc(d->c->search, GFP_NOIO);
  541. closure_init(&s->cl, NULL);
  542. do_bio_hook(s, bio);
  543. s->orig_bio = bio;
  544. s->cache_miss = NULL;
  545. s->cache_missed = 0;
  546. s->d = d;
  547. s->recoverable = 1;
  548. s->write = op_is_write(bio_op(bio));
  549. s->read_dirty_data = 0;
  550. s->start_time = jiffies;
  551. s->iop.c = d->c;
  552. s->iop.bio = NULL;
  553. s->iop.inode = d->id;
  554. s->iop.write_point = hash_long((unsigned long) current, 16);
  555. s->iop.write_prio = 0;
  556. s->iop.status = 0;
  557. s->iop.flags = 0;
  558. s->iop.flush_journal = op_is_flush(bio->bi_opf);
  559. s->iop.wq = bcache_wq;
  560. return s;
  561. }
  562. /* Cached devices */
  563. static void cached_dev_bio_complete(struct closure *cl)
  564. {
  565. struct search *s = container_of(cl, struct search, cl);
  566. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  567. search_free(cl);
  568. cached_dev_put(dc);
  569. }
  570. /* Process reads */
  571. static void cached_dev_cache_miss_done(struct closure *cl)
  572. {
  573. struct search *s = container_of(cl, struct search, cl);
  574. if (s->iop.replace_collision)
  575. bch_mark_cache_miss_collision(s->iop.c, s->d);
  576. if (s->iop.bio)
  577. bio_free_pages(s->iop.bio);
  578. cached_dev_bio_complete(cl);
  579. }
  580. static void cached_dev_read_error(struct closure *cl)
  581. {
  582. struct search *s = container_of(cl, struct search, cl);
  583. struct bio *bio = &s->bio.bio;
  584. /*
  585. * If read request hit dirty data (s->read_dirty_data is true),
  586. * then recovery a failed read request from cached device may
  587. * get a stale data back. So read failure recovery is only
  588. * permitted when read request hit clean data in cache device,
  589. * or when cache read race happened.
  590. */
  591. if (s->recoverable && !s->read_dirty_data) {
  592. /* Retry from the backing device: */
  593. trace_bcache_read_retry(s->orig_bio);
  594. s->iop.status = 0;
  595. do_bio_hook(s, s->orig_bio);
  596. /* XXX: invalidate cache */
  597. closure_bio_submit(bio, cl);
  598. }
  599. continue_at(cl, cached_dev_cache_miss_done, NULL);
  600. }
  601. static void cached_dev_read_done(struct closure *cl)
  602. {
  603. struct search *s = container_of(cl, struct search, cl);
  604. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  605. /*
  606. * We had a cache miss; cache_bio now contains data ready to be inserted
  607. * into the cache.
  608. *
  609. * First, we copy the data we just read from cache_bio's bounce buffers
  610. * to the buffers the original bio pointed to:
  611. */
  612. if (s->iop.bio) {
  613. bio_reset(s->iop.bio);
  614. s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector;
  615. bio_copy_dev(s->iop.bio, s->cache_miss);
  616. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  617. bch_bio_map(s->iop.bio, NULL);
  618. bio_copy_data(s->cache_miss, s->iop.bio);
  619. bio_put(s->cache_miss);
  620. s->cache_miss = NULL;
  621. }
  622. if (verify(dc) && s->recoverable && !s->read_dirty_data)
  623. bch_data_verify(dc, s->orig_bio);
  624. bio_complete(s);
  625. if (s->iop.bio &&
  626. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  627. BUG_ON(!s->iop.replace);
  628. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  629. }
  630. continue_at(cl, cached_dev_cache_miss_done, NULL);
  631. }
  632. static void cached_dev_read_done_bh(struct closure *cl)
  633. {
  634. struct search *s = container_of(cl, struct search, cl);
  635. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  636. bch_mark_cache_accounting(s->iop.c, s->d,
  637. !s->cache_missed, s->iop.bypass);
  638. trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
  639. if (s->iop.status)
  640. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  641. else if (s->iop.bio || verify(dc))
  642. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  643. else
  644. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  645. }
  646. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  647. struct bio *bio, unsigned sectors)
  648. {
  649. int ret = MAP_CONTINUE;
  650. unsigned reada = 0;
  651. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  652. struct bio *miss, *cache_bio;
  653. s->cache_missed = 1;
  654. if (s->cache_miss || s->iop.bypass) {
  655. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  656. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  657. goto out_submit;
  658. }
  659. if (!(bio->bi_opf & REQ_RAHEAD) &&
  660. !(bio->bi_opf & REQ_META) &&
  661. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  662. reada = min_t(sector_t, dc->readahead >> 9,
  663. get_capacity(bio->bi_disk) - bio_end_sector(bio));
  664. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  665. s->iop.replace_key = KEY(s->iop.inode,
  666. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  667. s->insert_bio_sectors);
  668. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  669. if (ret)
  670. return ret;
  671. s->iop.replace = true;
  672. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  673. /* btree_search_recurse()'s btree iterator is no good anymore */
  674. ret = miss == bio ? MAP_DONE : -EINTR;
  675. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  676. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  677. dc->disk.bio_split);
  678. if (!cache_bio)
  679. goto out_submit;
  680. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  681. bio_copy_dev(cache_bio, miss);
  682. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  683. cache_bio->bi_end_io = request_endio;
  684. cache_bio->bi_private = &s->cl;
  685. bch_bio_map(cache_bio, NULL);
  686. if (bch_bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  687. goto out_put;
  688. if (reada)
  689. bch_mark_cache_readahead(s->iop.c, s->d);
  690. s->cache_miss = miss;
  691. s->iop.bio = cache_bio;
  692. bio_get(cache_bio);
  693. closure_bio_submit(cache_bio, &s->cl);
  694. return ret;
  695. out_put:
  696. bio_put(cache_bio);
  697. out_submit:
  698. miss->bi_end_io = request_endio;
  699. miss->bi_private = &s->cl;
  700. closure_bio_submit(miss, &s->cl);
  701. return ret;
  702. }
  703. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  704. {
  705. struct closure *cl = &s->cl;
  706. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  707. continue_at(cl, cached_dev_read_done_bh, NULL);
  708. }
  709. /* Process writes */
  710. static void cached_dev_write_complete(struct closure *cl)
  711. {
  712. struct search *s = container_of(cl, struct search, cl);
  713. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  714. up_read_non_owner(&dc->writeback_lock);
  715. cached_dev_bio_complete(cl);
  716. }
  717. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  718. {
  719. struct closure *cl = &s->cl;
  720. struct bio *bio = &s->bio.bio;
  721. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  722. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  723. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  724. down_read_non_owner(&dc->writeback_lock);
  725. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  726. /*
  727. * We overlap with some dirty data undergoing background
  728. * writeback, force this write to writeback
  729. */
  730. s->iop.bypass = false;
  731. s->iop.writeback = true;
  732. }
  733. /*
  734. * Discards aren't _required_ to do anything, so skipping if
  735. * check_overlapping returned true is ok
  736. *
  737. * But check_overlapping drops dirty keys for which io hasn't started,
  738. * so we still want to call it.
  739. */
  740. if (bio_op(bio) == REQ_OP_DISCARD)
  741. s->iop.bypass = true;
  742. if (should_writeback(dc, s->orig_bio,
  743. cache_mode(dc),
  744. s->iop.bypass)) {
  745. s->iop.bypass = false;
  746. s->iop.writeback = true;
  747. }
  748. if (s->iop.bypass) {
  749. s->iop.bio = s->orig_bio;
  750. bio_get(s->iop.bio);
  751. if ((bio_op(bio) != REQ_OP_DISCARD) ||
  752. blk_queue_discard(bdev_get_queue(dc->bdev)))
  753. closure_bio_submit(bio, cl);
  754. } else if (s->iop.writeback) {
  755. bch_writeback_add(dc);
  756. s->iop.bio = bio;
  757. if (bio->bi_opf & REQ_PREFLUSH) {
  758. /* Also need to send a flush to the backing device */
  759. struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
  760. dc->disk.bio_split);
  761. bio_copy_dev(flush, bio);
  762. flush->bi_end_io = request_endio;
  763. flush->bi_private = cl;
  764. flush->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  765. closure_bio_submit(flush, cl);
  766. }
  767. } else {
  768. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split);
  769. closure_bio_submit(bio, cl);
  770. }
  771. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  772. continue_at(cl, cached_dev_write_complete, NULL);
  773. }
  774. static void cached_dev_nodata(struct closure *cl)
  775. {
  776. struct search *s = container_of(cl, struct search, cl);
  777. struct bio *bio = &s->bio.bio;
  778. if (s->iop.flush_journal)
  779. bch_journal_meta(s->iop.c, cl);
  780. /* If it's a flush, we send the flush to the backing device too */
  781. closure_bio_submit(bio, cl);
  782. continue_at(cl, cached_dev_bio_complete, NULL);
  783. }
  784. /* Cached devices - read & write stuff */
  785. static blk_qc_t cached_dev_make_request(struct request_queue *q,
  786. struct bio *bio)
  787. {
  788. struct search *s;
  789. struct bcache_device *d = bio->bi_disk->private_data;
  790. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  791. int rw = bio_data_dir(bio);
  792. atomic_set(&dc->backing_idle, 0);
  793. generic_start_io_acct(q, rw, bio_sectors(bio), &d->disk->part0);
  794. bio_set_dev(bio, dc->bdev);
  795. bio->bi_iter.bi_sector += dc->sb.data_offset;
  796. if (cached_dev_get(dc)) {
  797. s = search_alloc(bio, d);
  798. trace_bcache_request_start(s->d, bio);
  799. if (!bio->bi_iter.bi_size) {
  800. /*
  801. * can't call bch_journal_meta from under
  802. * generic_make_request
  803. */
  804. continue_at_nobarrier(&s->cl,
  805. cached_dev_nodata,
  806. bcache_wq);
  807. } else {
  808. s->iop.bypass = check_should_bypass(dc, bio);
  809. if (rw)
  810. cached_dev_write(dc, s);
  811. else
  812. cached_dev_read(dc, s);
  813. }
  814. } else {
  815. if ((bio_op(bio) == REQ_OP_DISCARD) &&
  816. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  817. bio_endio(bio);
  818. else
  819. generic_make_request(bio);
  820. }
  821. return BLK_QC_T_NONE;
  822. }
  823. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  824. unsigned int cmd, unsigned long arg)
  825. {
  826. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  827. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  828. }
  829. static int cached_dev_congested(void *data, int bits)
  830. {
  831. struct bcache_device *d = data;
  832. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  833. struct request_queue *q = bdev_get_queue(dc->bdev);
  834. int ret = 0;
  835. if (bdi_congested(q->backing_dev_info, bits))
  836. return 1;
  837. if (cached_dev_get(dc)) {
  838. unsigned i;
  839. struct cache *ca;
  840. for_each_cache(ca, d->c, i) {
  841. q = bdev_get_queue(ca->bdev);
  842. ret |= bdi_congested(q->backing_dev_info, bits);
  843. }
  844. cached_dev_put(dc);
  845. }
  846. return ret;
  847. }
  848. void bch_cached_dev_request_init(struct cached_dev *dc)
  849. {
  850. struct gendisk *g = dc->disk.disk;
  851. g->queue->make_request_fn = cached_dev_make_request;
  852. g->queue->backing_dev_info->congested_fn = cached_dev_congested;
  853. dc->disk.cache_miss = cached_dev_cache_miss;
  854. dc->disk.ioctl = cached_dev_ioctl;
  855. }
  856. /* Flash backed devices */
  857. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  858. struct bio *bio, unsigned sectors)
  859. {
  860. unsigned bytes = min(sectors, bio_sectors(bio)) << 9;
  861. swap(bio->bi_iter.bi_size, bytes);
  862. zero_fill_bio(bio);
  863. swap(bio->bi_iter.bi_size, bytes);
  864. bio_advance(bio, bytes);
  865. if (!bio->bi_iter.bi_size)
  866. return MAP_DONE;
  867. return MAP_CONTINUE;
  868. }
  869. static void flash_dev_nodata(struct closure *cl)
  870. {
  871. struct search *s = container_of(cl, struct search, cl);
  872. if (s->iop.flush_journal)
  873. bch_journal_meta(s->iop.c, cl);
  874. continue_at(cl, search_free, NULL);
  875. }
  876. static blk_qc_t flash_dev_make_request(struct request_queue *q,
  877. struct bio *bio)
  878. {
  879. struct search *s;
  880. struct closure *cl;
  881. struct bcache_device *d = bio->bi_disk->private_data;
  882. int rw = bio_data_dir(bio);
  883. generic_start_io_acct(q, rw, bio_sectors(bio), &d->disk->part0);
  884. s = search_alloc(bio, d);
  885. cl = &s->cl;
  886. bio = &s->bio.bio;
  887. trace_bcache_request_start(s->d, bio);
  888. if (!bio->bi_iter.bi_size) {
  889. /*
  890. * can't call bch_journal_meta from under
  891. * generic_make_request
  892. */
  893. continue_at_nobarrier(&s->cl,
  894. flash_dev_nodata,
  895. bcache_wq);
  896. return BLK_QC_T_NONE;
  897. } else if (rw) {
  898. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  899. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  900. &KEY(d->id, bio_end_sector(bio), 0));
  901. s->iop.bypass = (bio_op(bio) == REQ_OP_DISCARD) != 0;
  902. s->iop.writeback = true;
  903. s->iop.bio = bio;
  904. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  905. } else {
  906. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  907. }
  908. continue_at(cl, search_free, NULL);
  909. return BLK_QC_T_NONE;
  910. }
  911. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  912. unsigned int cmd, unsigned long arg)
  913. {
  914. return -ENOTTY;
  915. }
  916. static int flash_dev_congested(void *data, int bits)
  917. {
  918. struct bcache_device *d = data;
  919. struct request_queue *q;
  920. struct cache *ca;
  921. unsigned i;
  922. int ret = 0;
  923. for_each_cache(ca, d->c, i) {
  924. q = bdev_get_queue(ca->bdev);
  925. ret |= bdi_congested(q->backing_dev_info, bits);
  926. }
  927. return ret;
  928. }
  929. void bch_flash_dev_request_init(struct bcache_device *d)
  930. {
  931. struct gendisk *g = d->disk;
  932. g->queue->make_request_fn = flash_dev_make_request;
  933. g->queue->backing_dev_info->congested_fn = flash_dev_congested;
  934. d->cache_miss = flash_dev_cache_miss;
  935. d->ioctl = flash_dev_ioctl;
  936. }
  937. void bch_request_exit(void)
  938. {
  939. if (bch_search_cache)
  940. kmem_cache_destroy(bch_search_cache);
  941. }
  942. int __init bch_request_init(void)
  943. {
  944. bch_search_cache = KMEM_CACHE(search, 0);
  945. if (!bch_search_cache)
  946. return -ENOMEM;
  947. return 0;
  948. }