request.c 29 KB

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