request.c 34 KB

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