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