request.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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/cgroup.h>
  14. #include <linux/module.h>
  15. #include <linux/hash.h>
  16. #include <linux/random.h>
  17. #include "blk-cgroup.h"
  18. #include <trace/events/bcache.h>
  19. #define CUTOFF_CACHE_ADD 95
  20. #define CUTOFF_CACHE_READA 90
  21. struct kmem_cache *bch_search_cache;
  22. static void check_should_skip(struct cached_dev *, struct search *);
  23. /* Cgroup interface */
  24. #ifdef CONFIG_CGROUP_BCACHE
  25. static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 };
  26. static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup)
  27. {
  28. struct cgroup_subsys_state *css;
  29. return cgroup &&
  30. (css = cgroup_subsys_state(cgroup, bcache_subsys_id))
  31. ? container_of(css, struct bch_cgroup, css)
  32. : &bcache_default_cgroup;
  33. }
  34. struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio)
  35. {
  36. struct cgroup_subsys_state *css = bio->bi_css
  37. ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id)
  38. : task_subsys_state(current, bcache_subsys_id);
  39. return css
  40. ? container_of(css, struct bch_cgroup, css)
  41. : &bcache_default_cgroup;
  42. }
  43. static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft,
  44. struct file *file,
  45. char __user *buf, size_t nbytes, loff_t *ppos)
  46. {
  47. char tmp[1024];
  48. int len = bch_snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes,
  49. cgroup_to_bcache(cgrp)->cache_mode + 1);
  50. if (len < 0)
  51. return len;
  52. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  53. }
  54. static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft,
  55. const char *buf)
  56. {
  57. int v = bch_read_string_list(buf, bch_cache_modes);
  58. if (v < 0)
  59. return v;
  60. cgroup_to_bcache(cgrp)->cache_mode = v - 1;
  61. return 0;
  62. }
  63. static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft)
  64. {
  65. return cgroup_to_bcache(cgrp)->verify;
  66. }
  67. static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val)
  68. {
  69. cgroup_to_bcache(cgrp)->verify = val;
  70. return 0;
  71. }
  72. static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft)
  73. {
  74. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  75. return atomic_read(&bcachecg->stats.cache_hits);
  76. }
  77. static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft)
  78. {
  79. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  80. return atomic_read(&bcachecg->stats.cache_misses);
  81. }
  82. static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp,
  83. struct cftype *cft)
  84. {
  85. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  86. return atomic_read(&bcachecg->stats.cache_bypass_hits);
  87. }
  88. static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp,
  89. struct cftype *cft)
  90. {
  91. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  92. return atomic_read(&bcachecg->stats.cache_bypass_misses);
  93. }
  94. static struct cftype bch_files[] = {
  95. {
  96. .name = "cache_mode",
  97. .read = cache_mode_read,
  98. .write_string = cache_mode_write,
  99. },
  100. {
  101. .name = "verify",
  102. .read_u64 = bch_verify_read,
  103. .write_u64 = bch_verify_write,
  104. },
  105. {
  106. .name = "cache_hits",
  107. .read_u64 = bch_cache_hits_read,
  108. },
  109. {
  110. .name = "cache_misses",
  111. .read_u64 = bch_cache_misses_read,
  112. },
  113. {
  114. .name = "cache_bypass_hits",
  115. .read_u64 = bch_cache_bypass_hits_read,
  116. },
  117. {
  118. .name = "cache_bypass_misses",
  119. .read_u64 = bch_cache_bypass_misses_read,
  120. },
  121. { } /* terminate */
  122. };
  123. static void init_bch_cgroup(struct bch_cgroup *cg)
  124. {
  125. cg->cache_mode = -1;
  126. }
  127. static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup)
  128. {
  129. struct bch_cgroup *cg;
  130. cg = kzalloc(sizeof(*cg), GFP_KERNEL);
  131. if (!cg)
  132. return ERR_PTR(-ENOMEM);
  133. init_bch_cgroup(cg);
  134. return &cg->css;
  135. }
  136. static void bcachecg_destroy(struct cgroup *cgroup)
  137. {
  138. struct bch_cgroup *cg = cgroup_to_bcache(cgroup);
  139. free_css_id(&bcache_subsys, &cg->css);
  140. kfree(cg);
  141. }
  142. struct cgroup_subsys bcache_subsys = {
  143. .create = bcachecg_create,
  144. .destroy = bcachecg_destroy,
  145. .subsys_id = bcache_subsys_id,
  146. .name = "bcache",
  147. .module = THIS_MODULE,
  148. };
  149. EXPORT_SYMBOL_GPL(bcache_subsys);
  150. #endif
  151. static unsigned cache_mode(struct cached_dev *dc, struct bio *bio)
  152. {
  153. #ifdef CONFIG_CGROUP_BCACHE
  154. int r = bch_bio_to_cgroup(bio)->cache_mode;
  155. if (r >= 0)
  156. return r;
  157. #endif
  158. return BDEV_CACHE_MODE(&dc->sb);
  159. }
  160. static bool verify(struct cached_dev *dc, struct bio *bio)
  161. {
  162. #ifdef CONFIG_CGROUP_BCACHE
  163. if (bch_bio_to_cgroup(bio)->verify)
  164. return true;
  165. #endif
  166. return dc->verify;
  167. }
  168. static void bio_csum(struct bio *bio, struct bkey *k)
  169. {
  170. struct bio_vec *bv;
  171. uint64_t csum = 0;
  172. int i;
  173. bio_for_each_segment(bv, bio, i) {
  174. void *d = kmap(bv->bv_page) + bv->bv_offset;
  175. csum = bch_crc64_update(csum, d, bv->bv_len);
  176. kunmap(bv->bv_page);
  177. }
  178. k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
  179. }
  180. /* Insert data into cache */
  181. static void bio_invalidate(struct closure *cl)
  182. {
  183. struct btree_op *op = container_of(cl, struct btree_op, cl);
  184. struct bio *bio = op->cache_bio;
  185. pr_debug("invalidating %i sectors from %llu",
  186. bio_sectors(bio), (uint64_t) bio->bi_sector);
  187. while (bio_sectors(bio)) {
  188. unsigned len = min(bio_sectors(bio), 1U << 14);
  189. if (bch_keylist_realloc(&op->keys, 0, op->c))
  190. goto out;
  191. bio->bi_sector += len;
  192. bio->bi_size -= len << 9;
  193. bch_keylist_add(&op->keys,
  194. &KEY(op->inode, bio->bi_sector, len));
  195. }
  196. op->insert_data_done = true;
  197. bio_put(bio);
  198. out:
  199. continue_at(cl, bch_journal, bcache_wq);
  200. }
  201. struct open_bucket {
  202. struct list_head list;
  203. struct task_struct *last;
  204. unsigned sectors_free;
  205. BKEY_PADDED(key);
  206. };
  207. void bch_open_buckets_free(struct cache_set *c)
  208. {
  209. struct open_bucket *b;
  210. while (!list_empty(&c->data_buckets)) {
  211. b = list_first_entry(&c->data_buckets,
  212. struct open_bucket, list);
  213. list_del(&b->list);
  214. kfree(b);
  215. }
  216. }
  217. int bch_open_buckets_alloc(struct cache_set *c)
  218. {
  219. int i;
  220. spin_lock_init(&c->data_bucket_lock);
  221. for (i = 0; i < 6; i++) {
  222. struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
  223. if (!b)
  224. return -ENOMEM;
  225. list_add(&b->list, &c->data_buckets);
  226. }
  227. return 0;
  228. }
  229. /*
  230. * We keep multiple buckets open for writes, and try to segregate different
  231. * write streams for better cache utilization: first we look for a bucket where
  232. * the last write to it was sequential with the current write, and failing that
  233. * we look for a bucket that was last used by the same task.
  234. *
  235. * The ideas is if you've got multiple tasks pulling data into the cache at the
  236. * same time, you'll get better cache utilization if you try to segregate their
  237. * data and preserve locality.
  238. *
  239. * For example, say you've starting Firefox at the same time you're copying a
  240. * bunch of files. Firefox will likely end up being fairly hot and stay in the
  241. * cache awhile, but the data you copied might not be; if you wrote all that
  242. * data to the same buckets it'd get invalidated at the same time.
  243. *
  244. * Both of those tasks will be doing fairly random IO so we can't rely on
  245. * detecting sequential IO to segregate their data, but going off of the task
  246. * should be a sane heuristic.
  247. */
  248. static struct open_bucket *pick_data_bucket(struct cache_set *c,
  249. const struct bkey *search,
  250. struct task_struct *task,
  251. struct bkey *alloc)
  252. {
  253. struct open_bucket *ret, *ret_task = NULL;
  254. list_for_each_entry_reverse(ret, &c->data_buckets, list)
  255. if (!bkey_cmp(&ret->key, search))
  256. goto found;
  257. else if (ret->last == task)
  258. ret_task = ret;
  259. ret = ret_task ?: list_first_entry(&c->data_buckets,
  260. struct open_bucket, list);
  261. found:
  262. if (!ret->sectors_free && KEY_PTRS(alloc)) {
  263. ret->sectors_free = c->sb.bucket_size;
  264. bkey_copy(&ret->key, alloc);
  265. bkey_init(alloc);
  266. }
  267. if (!ret->sectors_free)
  268. ret = NULL;
  269. return ret;
  270. }
  271. /*
  272. * Allocates some space in the cache to write to, and k to point to the newly
  273. * allocated space, and updates KEY_SIZE(k) and KEY_OFFSET(k) (to point to the
  274. * end of the newly allocated space).
  275. *
  276. * May allocate fewer sectors than @sectors, KEY_SIZE(k) indicates how many
  277. * sectors were actually allocated.
  278. *
  279. * If s->writeback is true, will not fail.
  280. */
  281. static bool bch_alloc_sectors(struct bkey *k, unsigned sectors,
  282. struct search *s)
  283. {
  284. struct cache_set *c = s->op.c;
  285. struct open_bucket *b;
  286. BKEY_PADDED(key) alloc;
  287. struct closure cl, *w = NULL;
  288. unsigned i;
  289. if (s->writeback) {
  290. closure_init_stack(&cl);
  291. w = &cl;
  292. }
  293. /*
  294. * We might have to allocate a new bucket, which we can't do with a
  295. * spinlock held. So if we have to allocate, we drop the lock, allocate
  296. * and then retry. KEY_PTRS() indicates whether alloc points to
  297. * allocated bucket(s).
  298. */
  299. bkey_init(&alloc.key);
  300. spin_lock(&c->data_bucket_lock);
  301. while (!(b = pick_data_bucket(c, k, s->task, &alloc.key))) {
  302. unsigned watermark = s->op.write_prio
  303. ? WATERMARK_MOVINGGC
  304. : WATERMARK_NONE;
  305. spin_unlock(&c->data_bucket_lock);
  306. if (bch_bucket_alloc_set(c, watermark, &alloc.key, 1, w))
  307. return false;
  308. spin_lock(&c->data_bucket_lock);
  309. }
  310. /*
  311. * If we had to allocate, we might race and not need to allocate the
  312. * second time we call find_data_bucket(). If we allocated a bucket but
  313. * didn't use it, drop the refcount bch_bucket_alloc_set() took:
  314. */
  315. if (KEY_PTRS(&alloc.key))
  316. __bkey_put(c, &alloc.key);
  317. for (i = 0; i < KEY_PTRS(&b->key); i++)
  318. EBUG_ON(ptr_stale(c, &b->key, i));
  319. /* Set up the pointer to the space we're allocating: */
  320. for (i = 0; i < KEY_PTRS(&b->key); i++)
  321. k->ptr[i] = b->key.ptr[i];
  322. sectors = min(sectors, b->sectors_free);
  323. SET_KEY_OFFSET(k, KEY_OFFSET(k) + sectors);
  324. SET_KEY_SIZE(k, sectors);
  325. SET_KEY_PTRS(k, KEY_PTRS(&b->key));
  326. /*
  327. * Move b to the end of the lru, and keep track of what this bucket was
  328. * last used for:
  329. */
  330. list_move_tail(&b->list, &c->data_buckets);
  331. bkey_copy_key(&b->key, k);
  332. b->last = s->task;
  333. b->sectors_free -= sectors;
  334. for (i = 0; i < KEY_PTRS(&b->key); i++) {
  335. SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors);
  336. atomic_long_add(sectors,
  337. &PTR_CACHE(c, &b->key, i)->sectors_written);
  338. }
  339. if (b->sectors_free < c->sb.block_size)
  340. b->sectors_free = 0;
  341. /*
  342. * k takes refcounts on the buckets it points to until it's inserted
  343. * into the btree, but if we're done with this bucket we just transfer
  344. * get_data_bucket()'s refcount.
  345. */
  346. if (b->sectors_free)
  347. for (i = 0; i < KEY_PTRS(&b->key); i++)
  348. atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin);
  349. spin_unlock(&c->data_bucket_lock);
  350. return true;
  351. }
  352. static void bch_insert_data_error(struct closure *cl)
  353. {
  354. struct btree_op *op = container_of(cl, struct btree_op, cl);
  355. /*
  356. * Our data write just errored, which means we've got a bunch of keys to
  357. * insert that point to data that wasn't succesfully written.
  358. *
  359. * We don't have to insert those keys but we still have to invalidate
  360. * that region of the cache - so, if we just strip off all the pointers
  361. * from the keys we'll accomplish just that.
  362. */
  363. struct bkey *src = op->keys.bottom, *dst = op->keys.bottom;
  364. while (src != op->keys.top) {
  365. struct bkey *n = bkey_next(src);
  366. SET_KEY_PTRS(src, 0);
  367. bkey_copy(dst, src);
  368. dst = bkey_next(dst);
  369. src = n;
  370. }
  371. op->keys.top = dst;
  372. bch_journal(cl);
  373. }
  374. static void bch_insert_data_endio(struct bio *bio, int error)
  375. {
  376. struct closure *cl = bio->bi_private;
  377. struct btree_op *op = container_of(cl, struct btree_op, cl);
  378. struct search *s = container_of(op, struct search, op);
  379. if (error) {
  380. /* TODO: We could try to recover from this. */
  381. if (s->writeback)
  382. s->error = error;
  383. else if (s->write)
  384. set_closure_fn(cl, bch_insert_data_error, bcache_wq);
  385. else
  386. set_closure_fn(cl, NULL, NULL);
  387. }
  388. bch_bbio_endio(op->c, bio, error, "writing data to cache");
  389. }
  390. static void bch_insert_data_loop(struct closure *cl)
  391. {
  392. struct btree_op *op = container_of(cl, struct btree_op, cl);
  393. struct search *s = container_of(op, struct search, op);
  394. struct bio *bio = op->cache_bio, *n;
  395. if (op->skip)
  396. return bio_invalidate(cl);
  397. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) {
  398. set_gc_sectors(op->c);
  399. bch_queue_gc(op->c);
  400. }
  401. do {
  402. unsigned i;
  403. struct bkey *k;
  404. struct bio_set *split = s->d
  405. ? s->d->bio_split : op->c->bio_split;
  406. /* 1 for the device pointer and 1 for the chksum */
  407. if (bch_keylist_realloc(&op->keys,
  408. 1 + (op->csum ? 1 : 0),
  409. op->c))
  410. continue_at(cl, bch_journal, bcache_wq);
  411. k = op->keys.top;
  412. bkey_init(k);
  413. SET_KEY_INODE(k, op->inode);
  414. SET_KEY_OFFSET(k, bio->bi_sector);
  415. if (!bch_alloc_sectors(k, bio_sectors(bio), s))
  416. goto err;
  417. n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  418. if (!n) {
  419. __bkey_put(op->c, k);
  420. continue_at(cl, bch_insert_data_loop, bcache_wq);
  421. }
  422. n->bi_end_io = bch_insert_data_endio;
  423. n->bi_private = cl;
  424. if (s->writeback) {
  425. SET_KEY_DIRTY(k, true);
  426. for (i = 0; i < KEY_PTRS(k); i++)
  427. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  428. GC_MARK_DIRTY);
  429. }
  430. SET_KEY_CSUM(k, op->csum);
  431. if (KEY_CSUM(k))
  432. bio_csum(n, k);
  433. trace_bcache_cache_insert(k);
  434. bch_keylist_push(&op->keys);
  435. n->bi_rw |= REQ_WRITE;
  436. bch_submit_bbio(n, op->c, k, 0);
  437. } while (n != bio);
  438. op->insert_data_done = true;
  439. continue_at(cl, bch_journal, bcache_wq);
  440. err:
  441. /* bch_alloc_sectors() blocks if s->writeback = true */
  442. BUG_ON(s->writeback);
  443. /*
  444. * But if it's not a writeback write we'd rather just bail out if
  445. * there aren't any buckets ready to write to - it might take awhile and
  446. * we might be starving btree writes for gc or something.
  447. */
  448. if (s->write) {
  449. /*
  450. * Writethrough write: We can't complete the write until we've
  451. * updated the index. But we don't want to delay the write while
  452. * we wait for buckets to be freed up, so just invalidate the
  453. * rest of the write.
  454. */
  455. op->skip = true;
  456. return bio_invalidate(cl);
  457. } else {
  458. /*
  459. * From a cache miss, we can just insert the keys for the data
  460. * we have written or bail out if we didn't do anything.
  461. */
  462. op->insert_data_done = true;
  463. bio_put(bio);
  464. if (!bch_keylist_empty(&op->keys))
  465. continue_at(cl, bch_journal, bcache_wq);
  466. else
  467. closure_return(cl);
  468. }
  469. }
  470. /**
  471. * bch_insert_data - stick some data in the cache
  472. *
  473. * This is the starting point for any data to end up in a cache device; it could
  474. * be from a normal write, or a writeback write, or a write to a flash only
  475. * volume - it's also used by the moving garbage collector to compact data in
  476. * mostly empty buckets.
  477. *
  478. * It first writes the data to the cache, creating a list of keys to be inserted
  479. * (if the data had to be fragmented there will be multiple keys); after the
  480. * data is written it calls bch_journal, and after the keys have been added to
  481. * the next journal write they're inserted into the btree.
  482. *
  483. * It inserts the data in op->cache_bio; bi_sector is used for the key offset,
  484. * and op->inode is used for the key inode.
  485. *
  486. * If op->skip is true, instead of inserting the data it invalidates the region
  487. * of the cache represented by op->cache_bio and op->inode.
  488. */
  489. void bch_insert_data(struct closure *cl)
  490. {
  491. struct btree_op *op = container_of(cl, struct btree_op, cl);
  492. bch_keylist_init(&op->keys);
  493. bio_get(op->cache_bio);
  494. bch_insert_data_loop(cl);
  495. }
  496. void bch_btree_insert_async(struct closure *cl)
  497. {
  498. struct btree_op *op = container_of(cl, struct btree_op, cl);
  499. struct search *s = container_of(op, struct search, op);
  500. if (bch_btree_insert(op, op->c)) {
  501. s->error = -ENOMEM;
  502. op->insert_data_done = true;
  503. }
  504. if (op->insert_data_done) {
  505. bch_keylist_free(&op->keys);
  506. closure_return(cl);
  507. } else
  508. continue_at(cl, bch_insert_data_loop, bcache_wq);
  509. }
  510. /* Common code for the make_request functions */
  511. static void request_endio(struct bio *bio, int error)
  512. {
  513. struct closure *cl = bio->bi_private;
  514. if (error) {
  515. struct search *s = container_of(cl, struct search, cl);
  516. s->error = error;
  517. /* Only cache read errors are recoverable */
  518. s->recoverable = false;
  519. }
  520. bio_put(bio);
  521. closure_put(cl);
  522. }
  523. void bch_cache_read_endio(struct bio *bio, int error)
  524. {
  525. struct bbio *b = container_of(bio, struct bbio, bio);
  526. struct closure *cl = bio->bi_private;
  527. struct search *s = container_of(cl, struct search, cl);
  528. /*
  529. * If the bucket was reused while our bio was in flight, we might have
  530. * read the wrong data. Set s->error but not error so it doesn't get
  531. * counted against the cache device, but we'll still reread the data
  532. * from the backing device.
  533. */
  534. if (error)
  535. s->error = error;
  536. else if (ptr_stale(s->op.c, &b->key, 0)) {
  537. atomic_long_inc(&s->op.c->cache_read_races);
  538. s->error = -EINTR;
  539. }
  540. bch_bbio_endio(s->op.c, bio, error, "reading from cache");
  541. }
  542. static void bio_complete(struct search *s)
  543. {
  544. if (s->orig_bio) {
  545. int cpu, rw = bio_data_dir(s->orig_bio);
  546. unsigned long duration = jiffies - s->start_time;
  547. cpu = part_stat_lock();
  548. part_round_stats(cpu, &s->d->disk->part0);
  549. part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
  550. part_stat_unlock();
  551. trace_bcache_request_end(s, s->orig_bio);
  552. bio_endio(s->orig_bio, s->error);
  553. s->orig_bio = NULL;
  554. }
  555. }
  556. static void do_bio_hook(struct search *s)
  557. {
  558. struct bio *bio = &s->bio.bio;
  559. memcpy(bio, s->orig_bio, sizeof(struct bio));
  560. bio->bi_end_io = request_endio;
  561. bio->bi_private = &s->cl;
  562. atomic_set(&bio->bi_cnt, 3);
  563. }
  564. static void search_free(struct closure *cl)
  565. {
  566. struct search *s = container_of(cl, struct search, cl);
  567. bio_complete(s);
  568. if (s->op.cache_bio)
  569. bio_put(s->op.cache_bio);
  570. if (s->unaligned_bvec)
  571. mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec);
  572. closure_debug_destroy(cl);
  573. mempool_free(s, s->d->c->search);
  574. }
  575. static struct search *search_alloc(struct bio *bio, struct bcache_device *d)
  576. {
  577. struct bio_vec *bv;
  578. struct search *s = mempool_alloc(d->c->search, GFP_NOIO);
  579. memset(s, 0, offsetof(struct search, op.keys));
  580. __closure_init(&s->cl, NULL);
  581. s->op.inode = d->id;
  582. s->op.c = d->c;
  583. s->d = d;
  584. s->op.lock = -1;
  585. s->task = current;
  586. s->orig_bio = bio;
  587. s->write = (bio->bi_rw & REQ_WRITE) != 0;
  588. s->op.flush_journal = (bio->bi_rw & REQ_FLUSH) != 0;
  589. s->op.skip = (bio->bi_rw & REQ_DISCARD) != 0;
  590. s->recoverable = 1;
  591. s->start_time = jiffies;
  592. do_bio_hook(s);
  593. if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) {
  594. bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO);
  595. memcpy(bv, bio_iovec(bio),
  596. sizeof(struct bio_vec) * bio_segments(bio));
  597. s->bio.bio.bi_io_vec = bv;
  598. s->unaligned_bvec = 1;
  599. }
  600. return s;
  601. }
  602. static void btree_read_async(struct closure *cl)
  603. {
  604. struct btree_op *op = container_of(cl, struct btree_op, cl);
  605. int ret = btree_root(search_recurse, op->c, op);
  606. if (ret == -EAGAIN)
  607. continue_at(cl, btree_read_async, bcache_wq);
  608. closure_return(cl);
  609. }
  610. /* Cached devices */
  611. static void cached_dev_bio_complete(struct closure *cl)
  612. {
  613. struct search *s = container_of(cl, struct search, cl);
  614. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  615. search_free(cl);
  616. cached_dev_put(dc);
  617. }
  618. /* Process reads */
  619. static void cached_dev_read_complete(struct closure *cl)
  620. {
  621. struct search *s = container_of(cl, struct search, cl);
  622. if (s->op.insert_collision)
  623. bch_mark_cache_miss_collision(s);
  624. if (s->op.cache_bio) {
  625. int i;
  626. struct bio_vec *bv;
  627. __bio_for_each_segment(bv, s->op.cache_bio, i, 0)
  628. __free_page(bv->bv_page);
  629. }
  630. cached_dev_bio_complete(cl);
  631. }
  632. static void request_read_error(struct closure *cl)
  633. {
  634. struct search *s = container_of(cl, struct search, cl);
  635. struct bio_vec *bv;
  636. int i;
  637. if (s->recoverable) {
  638. /* Retry from the backing device: */
  639. trace_bcache_read_retry(s->orig_bio);
  640. s->error = 0;
  641. bv = s->bio.bio.bi_io_vec;
  642. do_bio_hook(s);
  643. s->bio.bio.bi_io_vec = bv;
  644. if (!s->unaligned_bvec)
  645. bio_for_each_segment(bv, s->orig_bio, i)
  646. bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
  647. else
  648. memcpy(s->bio.bio.bi_io_vec,
  649. bio_iovec(s->orig_bio),
  650. sizeof(struct bio_vec) *
  651. bio_segments(s->orig_bio));
  652. /* XXX: invalidate cache */
  653. closure_bio_submit(&s->bio.bio, &s->cl, s->d);
  654. }
  655. continue_at(cl, cached_dev_read_complete, NULL);
  656. }
  657. static void request_read_done(struct closure *cl)
  658. {
  659. struct search *s = container_of(cl, struct search, cl);
  660. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  661. /*
  662. * s->cache_bio != NULL implies that we had a cache miss; cache_bio now
  663. * contains data ready to be inserted into the cache.
  664. *
  665. * First, we copy the data we just read from cache_bio's bounce buffers
  666. * to the buffers the original bio pointed to:
  667. */
  668. if (s->op.cache_bio) {
  669. struct bio_vec *src, *dst;
  670. unsigned src_offset, dst_offset, bytes;
  671. void *dst_ptr;
  672. bio_reset(s->op.cache_bio);
  673. s->op.cache_bio->bi_sector = s->cache_miss->bi_sector;
  674. s->op.cache_bio->bi_bdev = s->cache_miss->bi_bdev;
  675. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  676. bch_bio_map(s->op.cache_bio, NULL);
  677. src = bio_iovec(s->op.cache_bio);
  678. dst = bio_iovec(s->cache_miss);
  679. src_offset = src->bv_offset;
  680. dst_offset = dst->bv_offset;
  681. dst_ptr = kmap(dst->bv_page);
  682. while (1) {
  683. if (dst_offset == dst->bv_offset + dst->bv_len) {
  684. kunmap(dst->bv_page);
  685. dst++;
  686. if (dst == bio_iovec_idx(s->cache_miss,
  687. s->cache_miss->bi_vcnt))
  688. break;
  689. dst_offset = dst->bv_offset;
  690. dst_ptr = kmap(dst->bv_page);
  691. }
  692. if (src_offset == src->bv_offset + src->bv_len) {
  693. src++;
  694. if (src == bio_iovec_idx(s->op.cache_bio,
  695. s->op.cache_bio->bi_vcnt))
  696. BUG();
  697. src_offset = src->bv_offset;
  698. }
  699. bytes = min(dst->bv_offset + dst->bv_len - dst_offset,
  700. src->bv_offset + src->bv_len - src_offset);
  701. memcpy(dst_ptr + dst_offset,
  702. page_address(src->bv_page) + src_offset,
  703. bytes);
  704. src_offset += bytes;
  705. dst_offset += bytes;
  706. }
  707. bio_put(s->cache_miss);
  708. s->cache_miss = NULL;
  709. }
  710. if (verify(dc, &s->bio.bio) && s->recoverable)
  711. bch_data_verify(s);
  712. bio_complete(s);
  713. if (s->op.cache_bio &&
  714. !test_bit(CACHE_SET_STOPPING, &s->op.c->flags)) {
  715. s->op.type = BTREE_REPLACE;
  716. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  717. }
  718. continue_at(cl, cached_dev_read_complete, NULL);
  719. }
  720. static void request_read_done_bh(struct closure *cl)
  721. {
  722. struct search *s = container_of(cl, struct search, cl);
  723. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  724. bch_mark_cache_accounting(s, !s->cache_miss, s->op.skip);
  725. trace_bcache_read(s->orig_bio, !s->cache_miss, s->op.skip);
  726. if (s->error)
  727. continue_at_nobarrier(cl, request_read_error, bcache_wq);
  728. else if (s->op.cache_bio || verify(dc, &s->bio.bio))
  729. continue_at_nobarrier(cl, request_read_done, bcache_wq);
  730. else
  731. continue_at_nobarrier(cl, cached_dev_read_complete, NULL);
  732. }
  733. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  734. struct bio *bio, unsigned sectors)
  735. {
  736. int ret = 0;
  737. unsigned reada;
  738. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  739. struct bio *miss;
  740. miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  741. if (!miss)
  742. return -EAGAIN;
  743. if (miss == bio)
  744. s->op.lookup_done = true;
  745. miss->bi_end_io = request_endio;
  746. miss->bi_private = &s->cl;
  747. if (s->cache_miss || s->op.skip)
  748. goto out_submit;
  749. if (miss != bio ||
  750. (bio->bi_rw & REQ_RAHEAD) ||
  751. (bio->bi_rw & REQ_META) ||
  752. s->op.c->gc_stats.in_use >= CUTOFF_CACHE_READA)
  753. reada = 0;
  754. else {
  755. reada = min(dc->readahead >> 9,
  756. sectors - bio_sectors(miss));
  757. if (bio_end(miss) + reada > bdev_sectors(miss->bi_bdev))
  758. reada = bdev_sectors(miss->bi_bdev) - bio_end(miss);
  759. }
  760. s->cache_bio_sectors = bio_sectors(miss) + reada;
  761. s->op.cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  762. DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS),
  763. dc->disk.bio_split);
  764. if (!s->op.cache_bio)
  765. goto out_submit;
  766. s->op.cache_bio->bi_sector = miss->bi_sector;
  767. s->op.cache_bio->bi_bdev = miss->bi_bdev;
  768. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  769. s->op.cache_bio->bi_end_io = request_endio;
  770. s->op.cache_bio->bi_private = &s->cl;
  771. /* btree_search_recurse()'s btree iterator is no good anymore */
  772. ret = -EINTR;
  773. if (!bch_btree_insert_check_key(b, &s->op, s->op.cache_bio))
  774. goto out_put;
  775. bch_bio_map(s->op.cache_bio, NULL);
  776. if (bch_bio_alloc_pages(s->op.cache_bio, __GFP_NOWARN|GFP_NOIO))
  777. goto out_put;
  778. s->cache_miss = miss;
  779. bio_get(s->op.cache_bio);
  780. closure_bio_submit(s->op.cache_bio, &s->cl, s->d);
  781. return ret;
  782. out_put:
  783. bio_put(s->op.cache_bio);
  784. s->op.cache_bio = NULL;
  785. out_submit:
  786. closure_bio_submit(miss, &s->cl, s->d);
  787. return ret;
  788. }
  789. static void request_read(struct cached_dev *dc, struct search *s)
  790. {
  791. struct closure *cl = &s->cl;
  792. check_should_skip(dc, s);
  793. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  794. continue_at(cl, request_read_done_bh, NULL);
  795. }
  796. /* Process writes */
  797. static void cached_dev_write_complete(struct closure *cl)
  798. {
  799. struct search *s = container_of(cl, struct search, cl);
  800. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  801. up_read_non_owner(&dc->writeback_lock);
  802. cached_dev_bio_complete(cl);
  803. }
  804. static void request_write(struct cached_dev *dc, struct search *s)
  805. {
  806. struct closure *cl = &s->cl;
  807. struct bio *bio = &s->bio.bio;
  808. struct bkey start, end;
  809. start = KEY(dc->disk.id, bio->bi_sector, 0);
  810. end = KEY(dc->disk.id, bio_end(bio), 0);
  811. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys, &start, &end);
  812. check_should_skip(dc, s);
  813. down_read_non_owner(&dc->writeback_lock);
  814. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  815. s->op.skip = false;
  816. s->writeback = true;
  817. }
  818. if (bio->bi_rw & REQ_DISCARD)
  819. goto skip;
  820. if (should_writeback(dc, s->orig_bio,
  821. cache_mode(dc, bio),
  822. s->op.skip)) {
  823. s->op.skip = false;
  824. s->writeback = true;
  825. }
  826. if (s->op.skip)
  827. goto skip;
  828. trace_bcache_write(s->orig_bio, s->writeback, s->op.skip);
  829. if (!s->writeback) {
  830. s->op.cache_bio = bio_clone_bioset(bio, GFP_NOIO,
  831. dc->disk.bio_split);
  832. closure_bio_submit(bio, cl, s->d);
  833. } else {
  834. s->op.cache_bio = bio;
  835. bch_writeback_add(dc);
  836. }
  837. out:
  838. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  839. continue_at(cl, cached_dev_write_complete, NULL);
  840. skip:
  841. s->op.skip = true;
  842. s->op.cache_bio = s->orig_bio;
  843. bio_get(s->op.cache_bio);
  844. if ((bio->bi_rw & REQ_DISCARD) &&
  845. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  846. goto out;
  847. closure_bio_submit(bio, cl, s->d);
  848. goto out;
  849. }
  850. static void request_nodata(struct cached_dev *dc, struct search *s)
  851. {
  852. struct closure *cl = &s->cl;
  853. struct bio *bio = &s->bio.bio;
  854. if (bio->bi_rw & REQ_DISCARD) {
  855. request_write(dc, s);
  856. return;
  857. }
  858. if (s->op.flush_journal)
  859. bch_journal_meta(s->op.c, cl);
  860. closure_bio_submit(bio, cl, s->d);
  861. continue_at(cl, cached_dev_bio_complete, NULL);
  862. }
  863. /* Cached devices - read & write stuff */
  864. unsigned bch_get_congested(struct cache_set *c)
  865. {
  866. int i;
  867. long rand;
  868. if (!c->congested_read_threshold_us &&
  869. !c->congested_write_threshold_us)
  870. return 0;
  871. i = (local_clock_us() - c->congested_last_us) / 1024;
  872. if (i < 0)
  873. return 0;
  874. i += atomic_read(&c->congested);
  875. if (i >= 0)
  876. return 0;
  877. i += CONGESTED_MAX;
  878. if (i > 0)
  879. i = fract_exp_two(i, 6);
  880. rand = get_random_int();
  881. i -= bitmap_weight(&rand, BITS_PER_LONG);
  882. return i > 0 ? i : 1;
  883. }
  884. static void add_sequential(struct task_struct *t)
  885. {
  886. ewma_add(t->sequential_io_avg,
  887. t->sequential_io, 8, 0);
  888. t->sequential_io = 0;
  889. }
  890. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  891. {
  892. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  893. }
  894. static void check_should_skip(struct cached_dev *dc, struct search *s)
  895. {
  896. struct cache_set *c = s->op.c;
  897. struct bio *bio = &s->bio.bio;
  898. unsigned mode = cache_mode(dc, bio);
  899. unsigned sectors, congested = bch_get_congested(c);
  900. if (atomic_read(&dc->disk.detaching) ||
  901. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  902. (bio->bi_rw & REQ_DISCARD))
  903. goto skip;
  904. if (mode == CACHE_MODE_NONE ||
  905. (mode == CACHE_MODE_WRITEAROUND &&
  906. (bio->bi_rw & REQ_WRITE)))
  907. goto skip;
  908. if (bio->bi_sector & (c->sb.block_size - 1) ||
  909. bio_sectors(bio) & (c->sb.block_size - 1)) {
  910. pr_debug("skipping unaligned io");
  911. goto skip;
  912. }
  913. if (!congested && !dc->sequential_cutoff)
  914. goto rescale;
  915. if (!congested &&
  916. mode == CACHE_MODE_WRITEBACK &&
  917. (bio->bi_rw & REQ_WRITE) &&
  918. (bio->bi_rw & REQ_SYNC))
  919. goto rescale;
  920. if (dc->sequential_merge) {
  921. struct io *i;
  922. spin_lock(&dc->io_lock);
  923. hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash)
  924. if (i->last == bio->bi_sector &&
  925. time_before(jiffies, i->jiffies))
  926. goto found;
  927. i = list_first_entry(&dc->io_lru, struct io, lru);
  928. add_sequential(s->task);
  929. i->sequential = 0;
  930. found:
  931. if (i->sequential + bio->bi_size > i->sequential)
  932. i->sequential += bio->bi_size;
  933. i->last = bio_end(bio);
  934. i->jiffies = jiffies + msecs_to_jiffies(5000);
  935. s->task->sequential_io = i->sequential;
  936. hlist_del(&i->hash);
  937. hlist_add_head(&i->hash, iohash(dc, i->last));
  938. list_move_tail(&i->lru, &dc->io_lru);
  939. spin_unlock(&dc->io_lock);
  940. } else {
  941. s->task->sequential_io = bio->bi_size;
  942. add_sequential(s->task);
  943. }
  944. sectors = max(s->task->sequential_io,
  945. s->task->sequential_io_avg) >> 9;
  946. if (dc->sequential_cutoff &&
  947. sectors >= dc->sequential_cutoff >> 9) {
  948. trace_bcache_bypass_sequential(s->orig_bio);
  949. goto skip;
  950. }
  951. if (congested && sectors >= congested) {
  952. trace_bcache_bypass_congested(s->orig_bio);
  953. goto skip;
  954. }
  955. rescale:
  956. bch_rescale_priorities(c, bio_sectors(bio));
  957. return;
  958. skip:
  959. bch_mark_sectors_bypassed(s, bio_sectors(bio));
  960. s->op.skip = true;
  961. }
  962. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  963. {
  964. struct search *s;
  965. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  966. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  967. int cpu, rw = bio_data_dir(bio);
  968. cpu = part_stat_lock();
  969. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  970. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  971. part_stat_unlock();
  972. bio->bi_bdev = dc->bdev;
  973. bio->bi_sector += dc->sb.data_offset;
  974. if (cached_dev_get(dc)) {
  975. s = search_alloc(bio, d);
  976. trace_bcache_request_start(s, bio);
  977. if (!bio_has_data(bio))
  978. request_nodata(dc, s);
  979. else if (rw)
  980. request_write(dc, s);
  981. else
  982. request_read(dc, s);
  983. } else {
  984. if ((bio->bi_rw & REQ_DISCARD) &&
  985. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  986. bio_endio(bio, 0);
  987. else
  988. bch_generic_make_request(bio, &d->bio_split_hook);
  989. }
  990. }
  991. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  992. unsigned int cmd, unsigned long arg)
  993. {
  994. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  995. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  996. }
  997. static int cached_dev_congested(void *data, int bits)
  998. {
  999. struct bcache_device *d = data;
  1000. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  1001. struct request_queue *q = bdev_get_queue(dc->bdev);
  1002. int ret = 0;
  1003. if (bdi_congested(&q->backing_dev_info, bits))
  1004. return 1;
  1005. if (cached_dev_get(dc)) {
  1006. unsigned i;
  1007. struct cache *ca;
  1008. for_each_cache(ca, d->c, i) {
  1009. q = bdev_get_queue(ca->bdev);
  1010. ret |= bdi_congested(&q->backing_dev_info, bits);
  1011. }
  1012. cached_dev_put(dc);
  1013. }
  1014. return ret;
  1015. }
  1016. void bch_cached_dev_request_init(struct cached_dev *dc)
  1017. {
  1018. struct gendisk *g = dc->disk.disk;
  1019. g->queue->make_request_fn = cached_dev_make_request;
  1020. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  1021. dc->disk.cache_miss = cached_dev_cache_miss;
  1022. dc->disk.ioctl = cached_dev_ioctl;
  1023. }
  1024. /* Flash backed devices */
  1025. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  1026. struct bio *bio, unsigned sectors)
  1027. {
  1028. /* Zero fill bio */
  1029. while (bio->bi_idx != bio->bi_vcnt) {
  1030. struct bio_vec *bv = bio_iovec(bio);
  1031. unsigned j = min(bv->bv_len >> 9, sectors);
  1032. void *p = kmap(bv->bv_page);
  1033. memset(p + bv->bv_offset, 0, j << 9);
  1034. kunmap(bv->bv_page);
  1035. bv->bv_len -= j << 9;
  1036. bv->bv_offset += j << 9;
  1037. if (bv->bv_len)
  1038. return 0;
  1039. bio->bi_sector += j;
  1040. bio->bi_size -= j << 9;
  1041. bio->bi_idx++;
  1042. sectors -= j;
  1043. }
  1044. s->op.lookup_done = true;
  1045. return 0;
  1046. }
  1047. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  1048. {
  1049. struct search *s;
  1050. struct closure *cl;
  1051. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  1052. int cpu, rw = bio_data_dir(bio);
  1053. cpu = part_stat_lock();
  1054. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  1055. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  1056. part_stat_unlock();
  1057. s = search_alloc(bio, d);
  1058. cl = &s->cl;
  1059. bio = &s->bio.bio;
  1060. trace_bcache_request_start(s, bio);
  1061. if (bio_has_data(bio) && !rw) {
  1062. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  1063. } else if (bio_has_data(bio) || s->op.skip) {
  1064. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
  1065. &KEY(d->id, bio->bi_sector, 0),
  1066. &KEY(d->id, bio_end(bio), 0));
  1067. s->writeback = true;
  1068. s->op.cache_bio = bio;
  1069. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  1070. } else {
  1071. /* No data - probably a cache flush */
  1072. if (s->op.flush_journal)
  1073. bch_journal_meta(s->op.c, cl);
  1074. }
  1075. continue_at(cl, search_free, NULL);
  1076. }
  1077. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1078. unsigned int cmd, unsigned long arg)
  1079. {
  1080. return -ENOTTY;
  1081. }
  1082. static int flash_dev_congested(void *data, int bits)
  1083. {
  1084. struct bcache_device *d = data;
  1085. struct request_queue *q;
  1086. struct cache *ca;
  1087. unsigned i;
  1088. int ret = 0;
  1089. for_each_cache(ca, d->c, i) {
  1090. q = bdev_get_queue(ca->bdev);
  1091. ret |= bdi_congested(&q->backing_dev_info, bits);
  1092. }
  1093. return ret;
  1094. }
  1095. void bch_flash_dev_request_init(struct bcache_device *d)
  1096. {
  1097. struct gendisk *g = d->disk;
  1098. g->queue->make_request_fn = flash_dev_make_request;
  1099. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  1100. d->cache_miss = flash_dev_cache_miss;
  1101. d->ioctl = flash_dev_ioctl;
  1102. }
  1103. void bch_request_exit(void)
  1104. {
  1105. #ifdef CONFIG_CGROUP_BCACHE
  1106. cgroup_unload_subsys(&bcache_subsys);
  1107. #endif
  1108. if (bch_search_cache)
  1109. kmem_cache_destroy(bch_search_cache);
  1110. }
  1111. int __init bch_request_init(void)
  1112. {
  1113. bch_search_cache = KMEM_CACHE(search, 0);
  1114. if (!bch_search_cache)
  1115. return -ENOMEM;
  1116. #ifdef CONFIG_CGROUP_BCACHE
  1117. cgroup_load_subsys(&bcache_subsys);
  1118. init_bch_cgroup(&bcache_default_cgroup);
  1119. cgroup_add_cftypes(&bcache_subsys, bch_files);
  1120. #endif
  1121. return 0;
  1122. }