request.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. int ret;
  462. bch_btree_op_init(&s->op, -1);
  463. ret = bch_btree_map_keys(&s->op, s->iop.c,
  464. &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0),
  465. cache_lookup_fn, MAP_END_KEY);
  466. if (ret == -EAGAIN) {
  467. continue_at(cl, cache_lookup, bcache_wq);
  468. return;
  469. }
  470. closure_return(cl);
  471. }
  472. /* Common code for the make_request functions */
  473. static void request_endio(struct bio *bio)
  474. {
  475. struct closure *cl = bio->bi_private;
  476. if (bio->bi_status) {
  477. struct search *s = container_of(cl, struct search, cl);
  478. s->iop.status = bio->bi_status;
  479. /* Only cache read errors are recoverable */
  480. s->recoverable = false;
  481. }
  482. bio_put(bio);
  483. closure_put(cl);
  484. }
  485. static void bio_complete(struct search *s)
  486. {
  487. if (s->orig_bio) {
  488. struct request_queue *q = s->orig_bio->bi_disk->queue;
  489. generic_end_io_acct(q, bio_data_dir(s->orig_bio),
  490. &s->d->disk->part0, s->start_time);
  491. trace_bcache_request_end(s->d, s->orig_bio);
  492. s->orig_bio->bi_status = s->iop.status;
  493. bio_endio(s->orig_bio);
  494. s->orig_bio = NULL;
  495. }
  496. }
  497. static void do_bio_hook(struct search *s, struct bio *orig_bio)
  498. {
  499. struct bio *bio = &s->bio.bio;
  500. bio_init(bio, NULL, 0);
  501. __bio_clone_fast(bio, orig_bio);
  502. bio->bi_end_io = request_endio;
  503. bio->bi_private = &s->cl;
  504. bio_cnt_set(bio, 3);
  505. }
  506. static void search_free(struct closure *cl)
  507. {
  508. struct search *s = container_of(cl, struct search, cl);
  509. bio_complete(s);
  510. if (s->iop.bio)
  511. bio_put(s->iop.bio);
  512. closure_debug_destroy(cl);
  513. mempool_free(s, s->d->c->search);
  514. }
  515. static inline struct search *search_alloc(struct bio *bio,
  516. struct bcache_device *d)
  517. {
  518. struct search *s;
  519. s = mempool_alloc(d->c->search, GFP_NOIO);
  520. closure_init(&s->cl, NULL);
  521. do_bio_hook(s, bio);
  522. s->orig_bio = bio;
  523. s->cache_miss = NULL;
  524. s->cache_missed = 0;
  525. s->d = d;
  526. s->recoverable = 1;
  527. s->write = op_is_write(bio_op(bio));
  528. s->read_dirty_data = 0;
  529. s->start_time = jiffies;
  530. s->iop.c = d->c;
  531. s->iop.bio = NULL;
  532. s->iop.inode = d->id;
  533. s->iop.write_point = hash_long((unsigned long) current, 16);
  534. s->iop.write_prio = 0;
  535. s->iop.status = 0;
  536. s->iop.flags = 0;
  537. s->iop.flush_journal = op_is_flush(bio->bi_opf);
  538. s->iop.wq = bcache_wq;
  539. return s;
  540. }
  541. /* Cached devices */
  542. static void cached_dev_bio_complete(struct closure *cl)
  543. {
  544. struct search *s = container_of(cl, struct search, cl);
  545. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  546. search_free(cl);
  547. cached_dev_put(dc);
  548. }
  549. /* Process reads */
  550. static void cached_dev_cache_miss_done(struct closure *cl)
  551. {
  552. struct search *s = container_of(cl, struct search, cl);
  553. if (s->iop.replace_collision)
  554. bch_mark_cache_miss_collision(s->iop.c, s->d);
  555. if (s->iop.bio)
  556. bio_free_pages(s->iop.bio);
  557. cached_dev_bio_complete(cl);
  558. }
  559. static void cached_dev_read_error(struct closure *cl)
  560. {
  561. struct search *s = container_of(cl, struct search, cl);
  562. struct bio *bio = &s->bio.bio;
  563. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  564. /*
  565. * If cache device is dirty (dc->has_dirty is non-zero), then
  566. * recovery a failed read request from cached device may get a
  567. * stale data back. So read failure recovery is only permitted
  568. * when cache device is clean.
  569. */
  570. if (s->recoverable &&
  571. (dc && !atomic_read(&dc->has_dirty))) {
  572. /* Retry from the backing device: */
  573. trace_bcache_read_retry(s->orig_bio);
  574. s->iop.status = 0;
  575. do_bio_hook(s, s->orig_bio);
  576. /* XXX: invalidate cache */
  577. closure_bio_submit(bio, cl);
  578. }
  579. continue_at(cl, cached_dev_cache_miss_done, NULL);
  580. }
  581. static void cached_dev_read_done(struct closure *cl)
  582. {
  583. struct search *s = container_of(cl, struct search, cl);
  584. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  585. /*
  586. * We had a cache miss; cache_bio now contains data ready to be inserted
  587. * into the cache.
  588. *
  589. * First, we copy the data we just read from cache_bio's bounce buffers
  590. * to the buffers the original bio pointed to:
  591. */
  592. if (s->iop.bio) {
  593. bio_reset(s->iop.bio);
  594. s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector;
  595. bio_copy_dev(s->iop.bio, s->cache_miss);
  596. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  597. bch_bio_map(s->iop.bio, NULL);
  598. bio_copy_data(s->cache_miss, s->iop.bio);
  599. bio_put(s->cache_miss);
  600. s->cache_miss = NULL;
  601. }
  602. if (verify(dc) && s->recoverable && !s->read_dirty_data)
  603. bch_data_verify(dc, s->orig_bio);
  604. bio_complete(s);
  605. if (s->iop.bio &&
  606. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  607. BUG_ON(!s->iop.replace);
  608. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  609. }
  610. continue_at(cl, cached_dev_cache_miss_done, NULL);
  611. }
  612. static void cached_dev_read_done_bh(struct closure *cl)
  613. {
  614. struct search *s = container_of(cl, struct search, cl);
  615. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  616. bch_mark_cache_accounting(s->iop.c, s->d,
  617. !s->cache_missed, s->iop.bypass);
  618. trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
  619. if (s->iop.status)
  620. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  621. else if (s->iop.bio || verify(dc))
  622. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  623. else
  624. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  625. }
  626. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  627. struct bio *bio, unsigned sectors)
  628. {
  629. int ret = MAP_CONTINUE;
  630. unsigned reada = 0;
  631. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  632. struct bio *miss, *cache_bio;
  633. s->cache_missed = 1;
  634. if (s->cache_miss || s->iop.bypass) {
  635. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  636. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  637. goto out_submit;
  638. }
  639. if (!(bio->bi_opf & REQ_RAHEAD) &&
  640. !(bio->bi_opf & REQ_META) &&
  641. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  642. reada = min_t(sector_t, dc->readahead >> 9,
  643. get_capacity(bio->bi_disk) - bio_end_sector(bio));
  644. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  645. s->iop.replace_key = KEY(s->iop.inode,
  646. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  647. s->insert_bio_sectors);
  648. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  649. if (ret)
  650. return ret;
  651. s->iop.replace = true;
  652. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  653. /* btree_search_recurse()'s btree iterator is no good anymore */
  654. ret = miss == bio ? MAP_DONE : -EINTR;
  655. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  656. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  657. dc->disk.bio_split);
  658. if (!cache_bio)
  659. goto out_submit;
  660. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  661. bio_copy_dev(cache_bio, miss);
  662. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  663. cache_bio->bi_end_io = request_endio;
  664. cache_bio->bi_private = &s->cl;
  665. bch_bio_map(cache_bio, NULL);
  666. if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  667. goto out_put;
  668. if (reada)
  669. bch_mark_cache_readahead(s->iop.c, s->d);
  670. s->cache_miss = miss;
  671. s->iop.bio = cache_bio;
  672. bio_get(cache_bio);
  673. closure_bio_submit(cache_bio, &s->cl);
  674. return ret;
  675. out_put:
  676. bio_put(cache_bio);
  677. out_submit:
  678. miss->bi_end_io = request_endio;
  679. miss->bi_private = &s->cl;
  680. closure_bio_submit(miss, &s->cl);
  681. return ret;
  682. }
  683. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  684. {
  685. struct closure *cl = &s->cl;
  686. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  687. continue_at(cl, cached_dev_read_done_bh, NULL);
  688. }
  689. /* Process writes */
  690. static void cached_dev_write_complete(struct closure *cl)
  691. {
  692. struct search *s = container_of(cl, struct search, cl);
  693. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  694. up_read_non_owner(&dc->writeback_lock);
  695. cached_dev_bio_complete(cl);
  696. }
  697. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  698. {
  699. struct closure *cl = &s->cl;
  700. struct bio *bio = &s->bio.bio;
  701. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  702. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  703. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  704. down_read_non_owner(&dc->writeback_lock);
  705. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  706. /*
  707. * We overlap with some dirty data undergoing background
  708. * writeback, force this write to writeback
  709. */
  710. s->iop.bypass = false;
  711. s->iop.writeback = true;
  712. }
  713. /*
  714. * Discards aren't _required_ to do anything, so skipping if
  715. * check_overlapping returned true is ok
  716. *
  717. * But check_overlapping drops dirty keys for which io hasn't started,
  718. * so we still want to call it.
  719. */
  720. if (bio_op(bio) == REQ_OP_DISCARD)
  721. s->iop.bypass = true;
  722. if (should_writeback(dc, s->orig_bio,
  723. cache_mode(dc),
  724. s->iop.bypass)) {
  725. s->iop.bypass = false;
  726. s->iop.writeback = true;
  727. }
  728. if (s->iop.bypass) {
  729. s->iop.bio = s->orig_bio;
  730. bio_get(s->iop.bio);
  731. if ((bio_op(bio) != REQ_OP_DISCARD) ||
  732. blk_queue_discard(bdev_get_queue(dc->bdev)))
  733. closure_bio_submit(bio, cl);
  734. } else if (s->iop.writeback) {
  735. bch_writeback_add(dc);
  736. s->iop.bio = bio;
  737. if (bio->bi_opf & REQ_PREFLUSH) {
  738. /* Also need to send a flush to the backing device */
  739. struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
  740. dc->disk.bio_split);
  741. bio_copy_dev(flush, bio);
  742. flush->bi_end_io = request_endio;
  743. flush->bi_private = cl;
  744. flush->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  745. closure_bio_submit(flush, cl);
  746. }
  747. } else {
  748. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split);
  749. closure_bio_submit(bio, cl);
  750. }
  751. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  752. continue_at(cl, cached_dev_write_complete, NULL);
  753. }
  754. static void cached_dev_nodata(struct closure *cl)
  755. {
  756. struct search *s = container_of(cl, struct search, cl);
  757. struct bio *bio = &s->bio.bio;
  758. if (s->iop.flush_journal)
  759. bch_journal_meta(s->iop.c, cl);
  760. /* If it's a flush, we send the flush to the backing device too */
  761. closure_bio_submit(bio, cl);
  762. continue_at(cl, cached_dev_bio_complete, NULL);
  763. }
  764. /* Cached devices - read & write stuff */
  765. static blk_qc_t cached_dev_make_request(struct request_queue *q,
  766. struct bio *bio)
  767. {
  768. struct search *s;
  769. struct bcache_device *d = bio->bi_disk->private_data;
  770. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  771. int rw = bio_data_dir(bio);
  772. generic_start_io_acct(q, rw, bio_sectors(bio), &d->disk->part0);
  773. bio_set_dev(bio, dc->bdev);
  774. bio->bi_iter.bi_sector += dc->sb.data_offset;
  775. if (cached_dev_get(dc)) {
  776. s = search_alloc(bio, d);
  777. trace_bcache_request_start(s->d, bio);
  778. if (!bio->bi_iter.bi_size) {
  779. /*
  780. * can't call bch_journal_meta from under
  781. * generic_make_request
  782. */
  783. continue_at_nobarrier(&s->cl,
  784. cached_dev_nodata,
  785. bcache_wq);
  786. } else {
  787. s->iop.bypass = check_should_bypass(dc, bio);
  788. if (rw)
  789. cached_dev_write(dc, s);
  790. else
  791. cached_dev_read(dc, s);
  792. }
  793. } else {
  794. if ((bio_op(bio) == REQ_OP_DISCARD) &&
  795. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  796. bio_endio(bio);
  797. else
  798. generic_make_request(bio);
  799. }
  800. return BLK_QC_T_NONE;
  801. }
  802. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  803. unsigned int cmd, unsigned long arg)
  804. {
  805. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  806. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  807. }
  808. static int cached_dev_congested(void *data, int bits)
  809. {
  810. struct bcache_device *d = data;
  811. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  812. struct request_queue *q = bdev_get_queue(dc->bdev);
  813. int ret = 0;
  814. if (bdi_congested(q->backing_dev_info, bits))
  815. return 1;
  816. if (cached_dev_get(dc)) {
  817. unsigned i;
  818. struct cache *ca;
  819. for_each_cache(ca, d->c, i) {
  820. q = bdev_get_queue(ca->bdev);
  821. ret |= bdi_congested(q->backing_dev_info, bits);
  822. }
  823. cached_dev_put(dc);
  824. }
  825. return ret;
  826. }
  827. void bch_cached_dev_request_init(struct cached_dev *dc)
  828. {
  829. struct gendisk *g = dc->disk.disk;
  830. g->queue->make_request_fn = cached_dev_make_request;
  831. g->queue->backing_dev_info->congested_fn = cached_dev_congested;
  832. dc->disk.cache_miss = cached_dev_cache_miss;
  833. dc->disk.ioctl = cached_dev_ioctl;
  834. }
  835. /* Flash backed devices */
  836. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  837. struct bio *bio, unsigned sectors)
  838. {
  839. unsigned bytes = min(sectors, bio_sectors(bio)) << 9;
  840. swap(bio->bi_iter.bi_size, bytes);
  841. zero_fill_bio(bio);
  842. swap(bio->bi_iter.bi_size, bytes);
  843. bio_advance(bio, bytes);
  844. if (!bio->bi_iter.bi_size)
  845. return MAP_DONE;
  846. return MAP_CONTINUE;
  847. }
  848. static void flash_dev_nodata(struct closure *cl)
  849. {
  850. struct search *s = container_of(cl, struct search, cl);
  851. if (s->iop.flush_journal)
  852. bch_journal_meta(s->iop.c, cl);
  853. continue_at(cl, search_free, NULL);
  854. }
  855. static blk_qc_t flash_dev_make_request(struct request_queue *q,
  856. struct bio *bio)
  857. {
  858. struct search *s;
  859. struct closure *cl;
  860. struct bcache_device *d = bio->bi_disk->private_data;
  861. int rw = bio_data_dir(bio);
  862. generic_start_io_acct(q, rw, bio_sectors(bio), &d->disk->part0);
  863. s = search_alloc(bio, d);
  864. cl = &s->cl;
  865. bio = &s->bio.bio;
  866. trace_bcache_request_start(s->d, bio);
  867. if (!bio->bi_iter.bi_size) {
  868. /*
  869. * can't call bch_journal_meta from under
  870. * generic_make_request
  871. */
  872. continue_at_nobarrier(&s->cl,
  873. flash_dev_nodata,
  874. bcache_wq);
  875. return BLK_QC_T_NONE;
  876. } else if (rw) {
  877. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  878. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  879. &KEY(d->id, bio_end_sector(bio), 0));
  880. s->iop.bypass = (bio_op(bio) == REQ_OP_DISCARD) != 0;
  881. s->iop.writeback = true;
  882. s->iop.bio = bio;
  883. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  884. } else {
  885. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  886. }
  887. continue_at(cl, search_free, NULL);
  888. return BLK_QC_T_NONE;
  889. }
  890. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  891. unsigned int cmd, unsigned long arg)
  892. {
  893. return -ENOTTY;
  894. }
  895. static int flash_dev_congested(void *data, int bits)
  896. {
  897. struct bcache_device *d = data;
  898. struct request_queue *q;
  899. struct cache *ca;
  900. unsigned i;
  901. int ret = 0;
  902. for_each_cache(ca, d->c, i) {
  903. q = bdev_get_queue(ca->bdev);
  904. ret |= bdi_congested(q->backing_dev_info, bits);
  905. }
  906. return ret;
  907. }
  908. void bch_flash_dev_request_init(struct bcache_device *d)
  909. {
  910. struct gendisk *g = d->disk;
  911. g->queue->make_request_fn = flash_dev_make_request;
  912. g->queue->backing_dev_info->congested_fn = flash_dev_congested;
  913. d->cache_miss = flash_dev_cache_miss;
  914. d->ioctl = flash_dev_ioctl;
  915. }
  916. void bch_request_exit(void)
  917. {
  918. if (bch_search_cache)
  919. kmem_cache_destroy(bch_search_cache);
  920. }
  921. int __init bch_request_init(void)
  922. {
  923. bch_search_cache = KMEM_CACHE(search, 0);
  924. if (!bch_search_cache)
  925. return -ENOMEM;
  926. return 0;
  927. }