request.c 28 KB

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