btree.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
  4. *
  5. * Uses a block device as cache for other block devices; optimized for SSDs.
  6. * All allocation is done in buckets, which should match the erase block size
  7. * of the device.
  8. *
  9. * Buckets containing cached data are kept on a heap sorted by priority;
  10. * bucket priority is increased on cache hit, and periodically all the buckets
  11. * on the heap have their priority scaled down. This currently is just used as
  12. * an LRU but in the future should allow for more intelligent heuristics.
  13. *
  14. * Buckets have an 8 bit counter; freeing is accomplished by incrementing the
  15. * counter. Garbage collection is used to remove stale pointers.
  16. *
  17. * Indexing is done via a btree; nodes are not necessarily fully sorted, rather
  18. * as keys are inserted we only sort the pages that have not yet been written.
  19. * When garbage collection is run, we resort the entire node.
  20. *
  21. * All configuration is done via sysfs; see Documentation/admin-guide/bcache.rst.
  22. */
  23. #include "bcache.h"
  24. #include "btree.h"
  25. #include "debug.h"
  26. #include "extents.h"
  27. #include <linux/slab.h>
  28. #include <linux/bitops.h>
  29. #include <linux/hash.h>
  30. #include <linux/kthread.h>
  31. #include <linux/prefetch.h>
  32. #include <linux/random.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/sched/clock.h>
  35. #include <linux/rculist.h>
  36. #include <linux/delay.h>
  37. #include <trace/events/bcache.h>
  38. /*
  39. * Todo:
  40. * register_bcache: Return errors out to userspace correctly
  41. *
  42. * Writeback: don't undirty key until after a cache flush
  43. *
  44. * Create an iterator for key pointers
  45. *
  46. * On btree write error, mark bucket such that it won't be freed from the cache
  47. *
  48. * Journalling:
  49. * Check for bad keys in replay
  50. * Propagate barriers
  51. * Refcount journal entries in journal_replay
  52. *
  53. * Garbage collection:
  54. * Finish incremental gc
  55. * Gc should free old UUIDs, data for invalid UUIDs
  56. *
  57. * Provide a way to list backing device UUIDs we have data cached for, and
  58. * probably how long it's been since we've seen them, and a way to invalidate
  59. * dirty data for devices that will never be attached again
  60. *
  61. * Keep 1 min/5 min/15 min statistics of how busy a block device has been, so
  62. * that based on that and how much dirty data we have we can keep writeback
  63. * from being starved
  64. *
  65. * Add a tracepoint or somesuch to watch for writeback starvation
  66. *
  67. * When btree depth > 1 and splitting an interior node, we have to make sure
  68. * alloc_bucket() cannot fail. This should be true but is not completely
  69. * obvious.
  70. *
  71. * Plugging?
  72. *
  73. * If data write is less than hard sector size of ssd, round up offset in open
  74. * bucket to the next whole sector
  75. *
  76. * Superblock needs to be fleshed out for multiple cache devices
  77. *
  78. * Add a sysfs tunable for the number of writeback IOs in flight
  79. *
  80. * Add a sysfs tunable for the number of open data buckets
  81. *
  82. * IO tracking: Can we track when one process is doing io on behalf of another?
  83. * IO tracking: Don't use just an average, weigh more recent stuff higher
  84. *
  85. * Test module load/unload
  86. */
  87. #define MAX_NEED_GC 64
  88. #define MAX_SAVE_PRIO 72
  89. #define MAX_GC_TIMES 100
  90. #define MIN_GC_NODES 100
  91. #define GC_SLEEP_MS 100
  92. #define PTR_DIRTY_BIT (((uint64_t) 1 << 36))
  93. #define PTR_HASH(c, k) \
  94. (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0))
  95. static struct workqueue_struct *btree_io_wq;
  96. #define insert_lock(s, b) ((b)->level <= (s)->lock)
  97. /*
  98. * These macros are for recursing down the btree - they handle the details of
  99. * locking and looking up nodes in the cache for you. They're best treated as
  100. * mere syntax when reading code that uses them.
  101. *
  102. * op->lock determines whether we take a read or a write lock at a given depth.
  103. * If you've got a read lock and find that you need a write lock (i.e. you're
  104. * going to have to split), set op->lock and return -EINTR; btree_root() will
  105. * call you again and you'll have the correct lock.
  106. */
  107. /**
  108. * btree - recurse down the btree on a specified key
  109. * @fn: function to call, which will be passed the child node
  110. * @key: key to recurse on
  111. * @b: parent btree node
  112. * @op: pointer to struct btree_op
  113. */
  114. #define btree(fn, key, b, op, ...) \
  115. ({ \
  116. int _r, l = (b)->level - 1; \
  117. bool _w = l <= (op)->lock; \
  118. struct btree *_child = bch_btree_node_get((b)->c, op, key, l, \
  119. _w, b); \
  120. if (!IS_ERR(_child)) { \
  121. _r = bch_btree_ ## fn(_child, op, ##__VA_ARGS__); \
  122. rw_unlock(_w, _child); \
  123. } else \
  124. _r = PTR_ERR(_child); \
  125. _r; \
  126. })
  127. /**
  128. * btree_root - call a function on the root of the btree
  129. * @fn: function to call, which will be passed the child node
  130. * @c: cache set
  131. * @op: pointer to struct btree_op
  132. */
  133. #define btree_root(fn, c, op, ...) \
  134. ({ \
  135. int _r = -EINTR; \
  136. do { \
  137. struct btree *_b = (c)->root; \
  138. bool _w = insert_lock(op, _b); \
  139. rw_lock(_w, _b, _b->level); \
  140. if (_b == (c)->root && \
  141. _w == insert_lock(op, _b)) { \
  142. _r = bch_btree_ ## fn(_b, op, ##__VA_ARGS__); \
  143. } \
  144. rw_unlock(_w, _b); \
  145. bch_cannibalize_unlock(c); \
  146. if (_r == -EINTR) \
  147. schedule(); \
  148. } while (_r == -EINTR); \
  149. \
  150. finish_wait(&(c)->btree_cache_wait, &(op)->wait); \
  151. _r; \
  152. })
  153. static inline struct bset *write_block(struct btree *b)
  154. {
  155. return ((void *) btree_bset_first(b)) + b->written * block_bytes(b->c);
  156. }
  157. static void bch_btree_init_next(struct btree *b)
  158. {
  159. /* If not a leaf node, always sort */
  160. if (b->level && b->keys.nsets)
  161. bch_btree_sort(&b->keys, &b->c->sort);
  162. else
  163. bch_btree_sort_lazy(&b->keys, &b->c->sort);
  164. if (b->written < btree_blocks(b))
  165. bch_bset_init_next(&b->keys, write_block(b),
  166. bset_magic(&b->c->sb));
  167. }
  168. /* Btree key manipulation */
  169. void bkey_put(struct cache_set *c, struct bkey *k)
  170. {
  171. unsigned int i;
  172. for (i = 0; i < KEY_PTRS(k); i++)
  173. if (ptr_available(c, k, i))
  174. atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin);
  175. }
  176. /* Btree IO */
  177. static uint64_t btree_csum_set(struct btree *b, struct bset *i)
  178. {
  179. uint64_t crc = b->key.ptr[0];
  180. void *data = (void *) i + 8, *end = bset_bkey_last(i);
  181. crc = bch_crc64_update(crc, data, end - data);
  182. return crc ^ 0xffffffffffffffffULL;
  183. }
  184. void bch_btree_node_read_done(struct btree *b)
  185. {
  186. const char *err = "bad btree header";
  187. struct bset *i = btree_bset_first(b);
  188. struct btree_iter *iter;
  189. /*
  190. * c->fill_iter can allocate an iterator with more memory space
  191. * than static MAX_BSETS.
  192. * See the comment arount cache_set->fill_iter.
  193. */
  194. iter = mempool_alloc(&b->c->fill_iter, GFP_NOIO);
  195. iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
  196. iter->used = 0;
  197. #ifdef CONFIG_BCACHE_DEBUG
  198. iter->b = &b->keys;
  199. #endif
  200. if (!i->seq)
  201. goto err;
  202. for (;
  203. b->written < btree_blocks(b) && i->seq == b->keys.set[0].data->seq;
  204. i = write_block(b)) {
  205. err = "unsupported bset version";
  206. if (i->version > BCACHE_BSET_VERSION)
  207. goto err;
  208. err = "bad btree header";
  209. if (b->written + set_blocks(i, block_bytes(b->c)) >
  210. btree_blocks(b))
  211. goto err;
  212. err = "bad magic";
  213. if (i->magic != bset_magic(&b->c->sb))
  214. goto err;
  215. err = "bad checksum";
  216. switch (i->version) {
  217. case 0:
  218. if (i->csum != csum_set(i))
  219. goto err;
  220. break;
  221. case BCACHE_BSET_VERSION:
  222. if (i->csum != btree_csum_set(b, i))
  223. goto err;
  224. break;
  225. }
  226. err = "empty set";
  227. if (i != b->keys.set[0].data && !i->keys)
  228. goto err;
  229. bch_btree_iter_push(iter, i->start, bset_bkey_last(i));
  230. b->written += set_blocks(i, block_bytes(b->c));
  231. }
  232. err = "corrupted btree";
  233. for (i = write_block(b);
  234. bset_sector_offset(&b->keys, i) < KEY_SIZE(&b->key);
  235. i = ((void *) i) + block_bytes(b->c))
  236. if (i->seq == b->keys.set[0].data->seq)
  237. goto err;
  238. bch_btree_sort_and_fix_extents(&b->keys, iter, &b->c->sort);
  239. i = b->keys.set[0].data;
  240. err = "short btree key";
  241. if (b->keys.set[0].size &&
  242. bkey_cmp(&b->key, &b->keys.set[0].end) < 0)
  243. goto err;
  244. if (b->written < btree_blocks(b))
  245. bch_bset_init_next(&b->keys, write_block(b),
  246. bset_magic(&b->c->sb));
  247. out:
  248. mempool_free(iter, &b->c->fill_iter);
  249. return;
  250. err:
  251. set_btree_node_io_error(b);
  252. bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys",
  253. err, PTR_BUCKET_NR(b->c, &b->key, 0),
  254. bset_block_offset(b, i), i->keys);
  255. goto out;
  256. }
  257. static void btree_node_read_endio(struct bio *bio)
  258. {
  259. struct closure *cl = bio->bi_private;
  260. closure_put(cl);
  261. }
  262. static void bch_btree_node_read(struct btree *b)
  263. {
  264. uint64_t start_time = local_clock();
  265. struct closure cl;
  266. struct bio *bio;
  267. trace_bcache_btree_read(b);
  268. closure_init_stack(&cl);
  269. bio = bch_bbio_alloc(b->c);
  270. bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
  271. bio->bi_end_io = btree_node_read_endio;
  272. bio->bi_private = &cl;
  273. bio->bi_opf = REQ_OP_READ | REQ_META;
  274. bch_bio_map(bio, b->keys.set[0].data);
  275. bch_submit_bbio(bio, b->c, &b->key, 0);
  276. closure_sync(&cl);
  277. if (bio->bi_status)
  278. set_btree_node_io_error(b);
  279. bch_bbio_free(bio, b->c);
  280. if (btree_node_io_error(b))
  281. goto err;
  282. bch_btree_node_read_done(b);
  283. bch_time_stats_update(&b->c->btree_read_time, start_time);
  284. return;
  285. err:
  286. bch_cache_set_error(b->c, "io error reading bucket %zu",
  287. PTR_BUCKET_NR(b->c, &b->key, 0));
  288. }
  289. static void btree_complete_write(struct btree *b, struct btree_write *w)
  290. {
  291. if (w->prio_blocked &&
  292. !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
  293. wake_up_allocators(b->c);
  294. if (w->journal) {
  295. atomic_dec_bug(w->journal);
  296. __closure_wake_up(&b->c->journal.wait);
  297. }
  298. w->prio_blocked = 0;
  299. w->journal = NULL;
  300. }
  301. static void btree_node_write_unlock(struct closure *cl)
  302. {
  303. struct btree *b = container_of(cl, struct btree, io);
  304. up(&b->io_mutex);
  305. }
  306. static void __btree_node_write_done(struct closure *cl)
  307. {
  308. struct btree *b = container_of(cl, struct btree, io);
  309. struct btree_write *w = btree_prev_write(b);
  310. bch_bbio_free(b->bio, b->c);
  311. b->bio = NULL;
  312. btree_complete_write(b, w);
  313. if (btree_node_dirty(b))
  314. queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
  315. closure_return_with_destructor(cl, btree_node_write_unlock);
  316. }
  317. static void btree_node_write_done(struct closure *cl)
  318. {
  319. struct btree *b = container_of(cl, struct btree, io);
  320. bio_free_pages(b->bio);
  321. __btree_node_write_done(cl);
  322. }
  323. static void btree_node_write_endio(struct bio *bio)
  324. {
  325. struct closure *cl = bio->bi_private;
  326. struct btree *b = container_of(cl, struct btree, io);
  327. if (bio->bi_status)
  328. set_btree_node_io_error(b);
  329. bch_bbio_count_io_errors(b->c, bio, bio->bi_status, "writing btree");
  330. closure_put(cl);
  331. }
  332. static void do_btree_node_write(struct btree *b)
  333. {
  334. struct closure *cl = &b->io;
  335. struct bset *i = btree_bset_last(b);
  336. BKEY_PADDED(key) k;
  337. i->version = BCACHE_BSET_VERSION;
  338. i->csum = btree_csum_set(b, i);
  339. BUG_ON(b->bio);
  340. b->bio = bch_bbio_alloc(b->c);
  341. b->bio->bi_end_io = btree_node_write_endio;
  342. b->bio->bi_private = cl;
  343. b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c));
  344. b->bio->bi_opf = REQ_OP_WRITE | REQ_META | REQ_FUA;
  345. bch_bio_map(b->bio, i);
  346. /*
  347. * If we're appending to a leaf node, we don't technically need FUA -
  348. * this write just needs to be persisted before the next journal write,
  349. * which will be marked FLUSH|FUA.
  350. *
  351. * Similarly if we're writing a new btree root - the pointer is going to
  352. * be in the next journal entry.
  353. *
  354. * But if we're writing a new btree node (that isn't a root) or
  355. * appending to a non leaf btree node, we need either FUA or a flush
  356. * when we write the parent with the new pointer. FUA is cheaper than a
  357. * flush, and writes appending to leaf nodes aren't blocking anything so
  358. * just make all btree node writes FUA to keep things sane.
  359. */
  360. bkey_copy(&k.key, &b->key);
  361. SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) +
  362. bset_sector_offset(&b->keys, i));
  363. if (!bch_bio_alloc_pages(b->bio, __GFP_NOWARN|GFP_NOWAIT)) {
  364. struct bio_vec *bv;
  365. void *addr = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
  366. struct bvec_iter_all iter_all;
  367. bio_for_each_segment_all(bv, b->bio, iter_all) {
  368. memcpy(page_address(bv->bv_page), addr, PAGE_SIZE);
  369. addr += PAGE_SIZE;
  370. }
  371. bch_submit_bbio(b->bio, b->c, &k.key, 0);
  372. continue_at(cl, btree_node_write_done, NULL);
  373. } else {
  374. /*
  375. * No problem for multipage bvec since the bio is
  376. * just allocated
  377. */
  378. b->bio->bi_vcnt = 0;
  379. bch_bio_map(b->bio, i);
  380. bch_submit_bbio(b->bio, b->c, &k.key, 0);
  381. closure_sync(cl);
  382. continue_at_nobarrier(cl, __btree_node_write_done, NULL);
  383. }
  384. }
  385. void __bch_btree_node_write(struct btree *b, struct closure *parent)
  386. {
  387. struct bset *i = btree_bset_last(b);
  388. lockdep_assert_held(&b->write_lock);
  389. trace_bcache_btree_write(b);
  390. BUG_ON(current->bio_list);
  391. BUG_ON(b->written >= btree_blocks(b));
  392. BUG_ON(b->written && !i->keys);
  393. BUG_ON(btree_bset_first(b)->seq != i->seq);
  394. bch_check_keys(&b->keys, "writing");
  395. cancel_delayed_work(&b->work);
  396. /* If caller isn't waiting for write, parent refcount is cache set */
  397. down(&b->io_mutex);
  398. closure_init(&b->io, parent ?: &b->c->cl);
  399. clear_bit(BTREE_NODE_dirty, &b->flags);
  400. change_bit(BTREE_NODE_write_idx, &b->flags);
  401. do_btree_node_write(b);
  402. atomic_long_add(set_blocks(i, block_bytes(b->c)) * b->c->sb.block_size,
  403. &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
  404. b->written += set_blocks(i, block_bytes(b->c));
  405. }
  406. void bch_btree_node_write(struct btree *b, struct closure *parent)
  407. {
  408. unsigned int nsets = b->keys.nsets;
  409. lockdep_assert_held(&b->lock);
  410. __bch_btree_node_write(b, parent);
  411. /*
  412. * do verify if there was more than one set initially (i.e. we did a
  413. * sort) and we sorted down to a single set:
  414. */
  415. if (nsets && !b->keys.nsets)
  416. bch_btree_verify(b);
  417. bch_btree_init_next(b);
  418. }
  419. static void bch_btree_node_write_sync(struct btree *b)
  420. {
  421. struct closure cl;
  422. closure_init_stack(&cl);
  423. mutex_lock(&b->write_lock);
  424. bch_btree_node_write(b, &cl);
  425. mutex_unlock(&b->write_lock);
  426. closure_sync(&cl);
  427. }
  428. static void btree_node_write_work(struct work_struct *w)
  429. {
  430. struct btree *b = container_of(to_delayed_work(w), struct btree, work);
  431. mutex_lock(&b->write_lock);
  432. if (btree_node_dirty(b))
  433. __bch_btree_node_write(b, NULL);
  434. mutex_unlock(&b->write_lock);
  435. }
  436. static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
  437. {
  438. struct bset *i = btree_bset_last(b);
  439. struct btree_write *w = btree_current_write(b);
  440. lockdep_assert_held(&b->write_lock);
  441. BUG_ON(!b->written);
  442. BUG_ON(!i->keys);
  443. if (!btree_node_dirty(b))
  444. queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
  445. set_btree_node_dirty(b);
  446. if (journal_ref) {
  447. if (w->journal &&
  448. journal_pin_cmp(b->c, w->journal, journal_ref)) {
  449. atomic_dec_bug(w->journal);
  450. w->journal = NULL;
  451. }
  452. if (!w->journal) {
  453. w->journal = journal_ref;
  454. atomic_inc(w->journal);
  455. }
  456. }
  457. /* Force write if set is too big */
  458. if (set_bytes(i) > PAGE_SIZE - 48 &&
  459. !current->bio_list)
  460. bch_btree_node_write(b, NULL);
  461. }
  462. /*
  463. * Btree in memory cache - allocation/freeing
  464. * mca -> memory cache
  465. */
  466. #define mca_reserve(c) (((c->root && c->root->level) \
  467. ? c->root->level : 1) * 8 + 16)
  468. #define mca_can_free(c) \
  469. max_t(int, 0, c->btree_cache_used - mca_reserve(c))
  470. static void mca_data_free(struct btree *b)
  471. {
  472. BUG_ON(b->io_mutex.count != 1);
  473. bch_btree_keys_free(&b->keys);
  474. b->c->btree_cache_used--;
  475. list_move(&b->list, &b->c->btree_cache_freed);
  476. }
  477. static void mca_bucket_free(struct btree *b)
  478. {
  479. BUG_ON(btree_node_dirty(b));
  480. b->key.ptr[0] = 0;
  481. hlist_del_init_rcu(&b->hash);
  482. list_move(&b->list, &b->c->btree_cache_freeable);
  483. }
  484. static unsigned int btree_order(struct bkey *k)
  485. {
  486. return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
  487. }
  488. static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
  489. {
  490. if (!bch_btree_keys_alloc(&b->keys,
  491. max_t(unsigned int,
  492. ilog2(b->c->btree_pages),
  493. btree_order(k)),
  494. gfp)) {
  495. b->c->btree_cache_used++;
  496. list_move(&b->list, &b->c->btree_cache);
  497. } else {
  498. list_move(&b->list, &b->c->btree_cache_freed);
  499. }
  500. }
  501. static struct btree *mca_bucket_alloc(struct cache_set *c,
  502. struct bkey *k, gfp_t gfp)
  503. {
  504. /*
  505. * kzalloc() is necessary here for initialization,
  506. * see code comments in bch_btree_keys_init().
  507. */
  508. struct btree *b = kzalloc(sizeof(struct btree), gfp);
  509. if (!b)
  510. return NULL;
  511. init_rwsem(&b->lock);
  512. lockdep_set_novalidate_class(&b->lock);
  513. mutex_init(&b->write_lock);
  514. lockdep_set_novalidate_class(&b->write_lock);
  515. INIT_LIST_HEAD(&b->list);
  516. INIT_DELAYED_WORK(&b->work, btree_node_write_work);
  517. b->c = c;
  518. sema_init(&b->io_mutex, 1);
  519. mca_data_alloc(b, k, gfp);
  520. return b;
  521. }
  522. static int mca_reap(struct btree *b, unsigned int min_order, bool flush)
  523. {
  524. struct closure cl;
  525. closure_init_stack(&cl);
  526. lockdep_assert_held(&b->c->bucket_lock);
  527. if (!down_write_trylock(&b->lock))
  528. return -ENOMEM;
  529. BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data);
  530. if (b->keys.page_order < min_order)
  531. goto out_unlock;
  532. if (!flush) {
  533. if (btree_node_dirty(b))
  534. goto out_unlock;
  535. if (down_trylock(&b->io_mutex))
  536. goto out_unlock;
  537. up(&b->io_mutex);
  538. }
  539. retry:
  540. /*
  541. * BTREE_NODE_dirty might be cleared in btree_flush_btree() by
  542. * __bch_btree_node_write(). To avoid an extra flush, acquire
  543. * b->write_lock before checking BTREE_NODE_dirty bit.
  544. */
  545. mutex_lock(&b->write_lock);
  546. /*
  547. * If this btree node is selected in btree_flush_write() by journal
  548. * code, delay and retry until the node is flushed by journal code
  549. * and BTREE_NODE_journal_flush bit cleared by btree_flush_write().
  550. */
  551. if (btree_node_journal_flush(b)) {
  552. pr_debug("bnode %p is flushing by journal, retry", b);
  553. mutex_unlock(&b->write_lock);
  554. udelay(1);
  555. goto retry;
  556. }
  557. if (btree_node_dirty(b))
  558. __bch_btree_node_write(b, &cl);
  559. mutex_unlock(&b->write_lock);
  560. closure_sync(&cl);
  561. /* wait for any in flight btree write */
  562. down(&b->io_mutex);
  563. up(&b->io_mutex);
  564. return 0;
  565. out_unlock:
  566. rw_unlock(true, b);
  567. return -ENOMEM;
  568. }
  569. static unsigned long bch_mca_scan(struct shrinker *shrink,
  570. struct shrink_control *sc)
  571. {
  572. struct cache_set *c = container_of(shrink, struct cache_set, shrink);
  573. struct btree *b, *t;
  574. unsigned long i, nr = sc->nr_to_scan;
  575. unsigned long freed = 0;
  576. unsigned int btree_cache_used;
  577. if (c->shrinker_disabled)
  578. return SHRINK_STOP;
  579. if (c->btree_cache_alloc_lock)
  580. return SHRINK_STOP;
  581. /* Return -1 if we can't do anything right now */
  582. if (sc->gfp_mask & __GFP_IO)
  583. mutex_lock(&c->bucket_lock);
  584. else if (!mutex_trylock(&c->bucket_lock))
  585. return -1;
  586. /*
  587. * It's _really_ critical that we don't free too many btree nodes - we
  588. * have to always leave ourselves a reserve. The reserve is how we
  589. * guarantee that allocating memory for a new btree node can always
  590. * succeed, so that inserting keys into the btree can always succeed and
  591. * IO can always make forward progress:
  592. */
  593. nr /= c->btree_pages;
  594. if (nr == 0)
  595. nr = 1;
  596. nr = min_t(unsigned long, nr, mca_can_free(c));
  597. i = 0;
  598. btree_cache_used = c->btree_cache_used;
  599. list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
  600. if (nr <= 0)
  601. goto out;
  602. if (++i > 3 &&
  603. !mca_reap(b, 0, false)) {
  604. mca_data_free(b);
  605. rw_unlock(true, b);
  606. freed++;
  607. }
  608. nr--;
  609. }
  610. for (; (nr--) && i < btree_cache_used; i++) {
  611. if (list_empty(&c->btree_cache))
  612. goto out;
  613. b = list_first_entry(&c->btree_cache, struct btree, list);
  614. list_rotate_left(&c->btree_cache);
  615. if (!b->accessed &&
  616. !mca_reap(b, 0, false)) {
  617. mca_bucket_free(b);
  618. mca_data_free(b);
  619. rw_unlock(true, b);
  620. freed++;
  621. } else
  622. b->accessed = 0;
  623. }
  624. out:
  625. mutex_unlock(&c->bucket_lock);
  626. return freed * c->btree_pages;
  627. }
  628. static unsigned long bch_mca_count(struct shrinker *shrink,
  629. struct shrink_control *sc)
  630. {
  631. struct cache_set *c = container_of(shrink, struct cache_set, shrink);
  632. if (c->shrinker_disabled)
  633. return 0;
  634. if (c->btree_cache_alloc_lock)
  635. return 0;
  636. return mca_can_free(c) * c->btree_pages;
  637. }
  638. void bch_btree_cache_free(struct cache_set *c)
  639. {
  640. struct btree *b;
  641. struct closure cl;
  642. closure_init_stack(&cl);
  643. if (c->shrink.list.next)
  644. unregister_shrinker(&c->shrink);
  645. mutex_lock(&c->bucket_lock);
  646. #ifdef CONFIG_BCACHE_DEBUG
  647. if (c->verify_data)
  648. list_move(&c->verify_data->list, &c->btree_cache);
  649. free_pages((unsigned long) c->verify_ondisk, ilog2(bucket_pages(c)));
  650. #endif
  651. list_splice(&c->btree_cache_freeable,
  652. &c->btree_cache);
  653. while (!list_empty(&c->btree_cache)) {
  654. b = list_first_entry(&c->btree_cache, struct btree, list);
  655. /*
  656. * This function is called by cache_set_free(), no I/O
  657. * request on cache now, it is unnecessary to acquire
  658. * b->write_lock before clearing BTREE_NODE_dirty anymore.
  659. */
  660. if (btree_node_dirty(b)) {
  661. btree_complete_write(b, btree_current_write(b));
  662. clear_bit(BTREE_NODE_dirty, &b->flags);
  663. }
  664. mca_data_free(b);
  665. }
  666. while (!list_empty(&c->btree_cache_freed)) {
  667. b = list_first_entry(&c->btree_cache_freed,
  668. struct btree, list);
  669. list_del(&b->list);
  670. cancel_delayed_work_sync(&b->work);
  671. kfree(b);
  672. }
  673. mutex_unlock(&c->bucket_lock);
  674. }
  675. int bch_btree_cache_alloc(struct cache_set *c)
  676. {
  677. unsigned int i;
  678. for (i = 0; i < mca_reserve(c); i++)
  679. if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
  680. return -ENOMEM;
  681. list_splice_init(&c->btree_cache,
  682. &c->btree_cache_freeable);
  683. #ifdef CONFIG_BCACHE_DEBUG
  684. mutex_init(&c->verify_lock);
  685. c->verify_ondisk = (void *)
  686. __get_free_pages(GFP_KERNEL|__GFP_COMP, ilog2(bucket_pages(c)));
  687. c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
  688. if (c->verify_data &&
  689. c->verify_data->keys.set->data)
  690. list_del_init(&c->verify_data->list);
  691. else
  692. c->verify_data = NULL;
  693. #endif
  694. c->shrink.count_objects = bch_mca_count;
  695. c->shrink.scan_objects = bch_mca_scan;
  696. c->shrink.seeks = 4;
  697. c->shrink.batch = c->btree_pages * 2;
  698. if (register_shrinker(&c->shrink))
  699. pr_warn("bcache: %s: could not register shrinker",
  700. __func__);
  701. return 0;
  702. }
  703. /* Btree in memory cache - hash table */
  704. static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
  705. {
  706. return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
  707. }
  708. static struct btree *mca_find(struct cache_set *c, struct bkey *k)
  709. {
  710. struct btree *b;
  711. rcu_read_lock();
  712. hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
  713. if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
  714. goto out;
  715. b = NULL;
  716. out:
  717. rcu_read_unlock();
  718. return b;
  719. }
  720. static int mca_cannibalize_lock(struct cache_set *c, struct btree_op *op)
  721. {
  722. spin_lock(&c->btree_cannibalize_lock);
  723. if (likely(c->btree_cache_alloc_lock == NULL)) {
  724. c->btree_cache_alloc_lock = current;
  725. } else if (c->btree_cache_alloc_lock != current) {
  726. if (op)
  727. prepare_to_wait(&c->btree_cache_wait, &op->wait,
  728. TASK_UNINTERRUPTIBLE);
  729. spin_unlock(&c->btree_cannibalize_lock);
  730. return -EINTR;
  731. }
  732. spin_unlock(&c->btree_cannibalize_lock);
  733. return 0;
  734. }
  735. static struct btree *mca_cannibalize(struct cache_set *c, struct btree_op *op,
  736. struct bkey *k)
  737. {
  738. struct btree *b;
  739. trace_bcache_btree_cache_cannibalize(c);
  740. if (mca_cannibalize_lock(c, op))
  741. return ERR_PTR(-EINTR);
  742. list_for_each_entry_reverse(b, &c->btree_cache, list)
  743. if (!mca_reap(b, btree_order(k), false))
  744. return b;
  745. list_for_each_entry_reverse(b, &c->btree_cache, list)
  746. if (!mca_reap(b, btree_order(k), true))
  747. return b;
  748. WARN(1, "btree cache cannibalize failed\n");
  749. return ERR_PTR(-ENOMEM);
  750. }
  751. /*
  752. * We can only have one thread cannibalizing other cached btree nodes at a time,
  753. * or we'll deadlock. We use an open coded mutex to ensure that, which a
  754. * cannibalize_bucket() will take. This means every time we unlock the root of
  755. * the btree, we need to release this lock if we have it held.
  756. */
  757. static void bch_cannibalize_unlock(struct cache_set *c)
  758. {
  759. spin_lock(&c->btree_cannibalize_lock);
  760. if (c->btree_cache_alloc_lock == current) {
  761. c->btree_cache_alloc_lock = NULL;
  762. wake_up(&c->btree_cache_wait);
  763. }
  764. spin_unlock(&c->btree_cannibalize_lock);
  765. }
  766. static struct btree *mca_alloc(struct cache_set *c, struct btree_op *op,
  767. struct bkey *k, int level)
  768. {
  769. struct btree *b;
  770. BUG_ON(current->bio_list);
  771. lockdep_assert_held(&c->bucket_lock);
  772. if (mca_find(c, k))
  773. return NULL;
  774. /* btree_free() doesn't free memory; it sticks the node on the end of
  775. * the list. Check if there's any freed nodes there:
  776. */
  777. list_for_each_entry(b, &c->btree_cache_freeable, list)
  778. if (!mca_reap(b, btree_order(k), false))
  779. goto out;
  780. /* We never free struct btree itself, just the memory that holds the on
  781. * disk node. Check the freed list before allocating a new one:
  782. */
  783. list_for_each_entry(b, &c->btree_cache_freed, list)
  784. if (!mca_reap(b, 0, false)) {
  785. mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
  786. if (!b->keys.set[0].data)
  787. goto err;
  788. else
  789. goto out;
  790. }
  791. b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
  792. if (!b)
  793. goto err;
  794. BUG_ON(!down_write_trylock(&b->lock));
  795. if (!b->keys.set->data)
  796. goto err;
  797. out:
  798. BUG_ON(b->io_mutex.count != 1);
  799. bkey_copy(&b->key, k);
  800. list_move(&b->list, &c->btree_cache);
  801. hlist_del_init_rcu(&b->hash);
  802. hlist_add_head_rcu(&b->hash, mca_hash(c, k));
  803. lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
  804. b->parent = (void *) ~0UL;
  805. b->flags = 0;
  806. b->written = 0;
  807. b->level = level;
  808. if (!b->level)
  809. bch_btree_keys_init(&b->keys, &bch_extent_keys_ops,
  810. &b->c->expensive_debug_checks);
  811. else
  812. bch_btree_keys_init(&b->keys, &bch_btree_keys_ops,
  813. &b->c->expensive_debug_checks);
  814. return b;
  815. err:
  816. if (b)
  817. rw_unlock(true, b);
  818. b = mca_cannibalize(c, op, k);
  819. if (!IS_ERR(b))
  820. goto out;
  821. return b;
  822. }
  823. /*
  824. * bch_btree_node_get - find a btree node in the cache and lock it, reading it
  825. * in from disk if necessary.
  826. *
  827. * If IO is necessary and running under generic_make_request, returns -EAGAIN.
  828. *
  829. * The btree node will have either a read or a write lock held, depending on
  830. * level and op->lock.
  831. */
  832. struct btree *bch_btree_node_get(struct cache_set *c, struct btree_op *op,
  833. struct bkey *k, int level, bool write,
  834. struct btree *parent)
  835. {
  836. int i = 0;
  837. struct btree *b;
  838. BUG_ON(level < 0);
  839. retry:
  840. b = mca_find(c, k);
  841. if (!b) {
  842. if (current->bio_list)
  843. return ERR_PTR(-EAGAIN);
  844. mutex_lock(&c->bucket_lock);
  845. b = mca_alloc(c, op, k, level);
  846. mutex_unlock(&c->bucket_lock);
  847. if (!b)
  848. goto retry;
  849. if (IS_ERR(b))
  850. return b;
  851. bch_btree_node_read(b);
  852. if (!write)
  853. downgrade_write(&b->lock);
  854. } else {
  855. rw_lock(write, b, level);
  856. if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
  857. rw_unlock(write, b);
  858. goto retry;
  859. }
  860. BUG_ON(b->level != level);
  861. }
  862. if (btree_node_io_error(b)) {
  863. rw_unlock(write, b);
  864. return ERR_PTR(-EIO);
  865. }
  866. BUG_ON(!b->written);
  867. b->parent = parent;
  868. b->accessed = 1;
  869. for (; i <= b->keys.nsets && b->keys.set[i].size; i++) {
  870. prefetch(b->keys.set[i].tree);
  871. prefetch(b->keys.set[i].data);
  872. }
  873. for (; i <= b->keys.nsets; i++)
  874. prefetch(b->keys.set[i].data);
  875. return b;
  876. }
  877. static void btree_node_prefetch(struct btree *parent, struct bkey *k)
  878. {
  879. struct btree *b;
  880. mutex_lock(&parent->c->bucket_lock);
  881. b = mca_alloc(parent->c, NULL, k, parent->level - 1);
  882. mutex_unlock(&parent->c->bucket_lock);
  883. if (!IS_ERR_OR_NULL(b)) {
  884. b->parent = parent;
  885. bch_btree_node_read(b);
  886. rw_unlock(true, b);
  887. }
  888. }
  889. /* Btree alloc */
  890. static void btree_node_free(struct btree *b)
  891. {
  892. trace_bcache_btree_node_free(b);
  893. BUG_ON(b == b->c->root);
  894. retry:
  895. mutex_lock(&b->write_lock);
  896. /*
  897. * If the btree node is selected and flushing in btree_flush_write(),
  898. * delay and retry until the BTREE_NODE_journal_flush bit cleared,
  899. * then it is safe to free the btree node here. Otherwise this btree
  900. * node will be in race condition.
  901. */
  902. if (btree_node_journal_flush(b)) {
  903. mutex_unlock(&b->write_lock);
  904. pr_debug("bnode %p journal_flush set, retry", b);
  905. udelay(1);
  906. goto retry;
  907. }
  908. if (btree_node_dirty(b)) {
  909. btree_complete_write(b, btree_current_write(b));
  910. clear_bit(BTREE_NODE_dirty, &b->flags);
  911. }
  912. mutex_unlock(&b->write_lock);
  913. cancel_delayed_work(&b->work);
  914. mutex_lock(&b->c->bucket_lock);
  915. bch_bucket_free(b->c, &b->key);
  916. mca_bucket_free(b);
  917. mutex_unlock(&b->c->bucket_lock);
  918. }
  919. struct btree *__bch_btree_node_alloc(struct cache_set *c, struct btree_op *op,
  920. int level, bool wait,
  921. struct btree *parent)
  922. {
  923. BKEY_PADDED(key) k;
  924. struct btree *b = ERR_PTR(-EAGAIN);
  925. mutex_lock(&c->bucket_lock);
  926. retry:
  927. if (__bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, wait))
  928. goto err;
  929. bkey_put(c, &k.key);
  930. SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
  931. b = mca_alloc(c, op, &k.key, level);
  932. if (IS_ERR(b))
  933. goto err_free;
  934. if (!b) {
  935. cache_bug(c,
  936. "Tried to allocate bucket that was in btree cache");
  937. goto retry;
  938. }
  939. b->accessed = 1;
  940. b->parent = parent;
  941. bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->sb));
  942. mutex_unlock(&c->bucket_lock);
  943. trace_bcache_btree_node_alloc(b);
  944. return b;
  945. err_free:
  946. bch_bucket_free(c, &k.key);
  947. err:
  948. mutex_unlock(&c->bucket_lock);
  949. trace_bcache_btree_node_alloc_fail(c);
  950. return b;
  951. }
  952. static struct btree *bch_btree_node_alloc(struct cache_set *c,
  953. struct btree_op *op, int level,
  954. struct btree *parent)
  955. {
  956. return __bch_btree_node_alloc(c, op, level, op != NULL, parent);
  957. }
  958. static struct btree *btree_node_alloc_replacement(struct btree *b,
  959. struct btree_op *op)
  960. {
  961. struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent);
  962. if (!IS_ERR_OR_NULL(n)) {
  963. mutex_lock(&n->write_lock);
  964. bch_btree_sort_into(&b->keys, &n->keys, &b->c->sort);
  965. bkey_copy_key(&n->key, &b->key);
  966. mutex_unlock(&n->write_lock);
  967. }
  968. return n;
  969. }
  970. static void make_btree_freeing_key(struct btree *b, struct bkey *k)
  971. {
  972. unsigned int i;
  973. mutex_lock(&b->c->bucket_lock);
  974. atomic_inc(&b->c->prio_blocked);
  975. bkey_copy(k, &b->key);
  976. bkey_copy_key(k, &ZERO_KEY);
  977. for (i = 0; i < KEY_PTRS(k); i++)
  978. SET_PTR_GEN(k, i,
  979. bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
  980. PTR_BUCKET(b->c, &b->key, i)));
  981. mutex_unlock(&b->c->bucket_lock);
  982. }
  983. static int btree_check_reserve(struct btree *b, struct btree_op *op)
  984. {
  985. struct cache_set *c = b->c;
  986. struct cache *ca;
  987. unsigned int i, reserve = (c->root->level - b->level) * 2 + 1;
  988. mutex_lock(&c->bucket_lock);
  989. for_each_cache(ca, c, i)
  990. if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) {
  991. if (op)
  992. prepare_to_wait(&c->btree_cache_wait, &op->wait,
  993. TASK_UNINTERRUPTIBLE);
  994. mutex_unlock(&c->bucket_lock);
  995. return -EINTR;
  996. }
  997. mutex_unlock(&c->bucket_lock);
  998. return mca_cannibalize_lock(b->c, op);
  999. }
  1000. /* Garbage collection */
  1001. static uint8_t __bch_btree_mark_key(struct cache_set *c, int level,
  1002. struct bkey *k)
  1003. {
  1004. uint8_t stale = 0;
  1005. unsigned int i;
  1006. struct bucket *g;
  1007. /*
  1008. * ptr_invalid() can't return true for the keys that mark btree nodes as
  1009. * freed, but since ptr_bad() returns true we'll never actually use them
  1010. * for anything and thus we don't want mark their pointers here
  1011. */
  1012. if (!bkey_cmp(k, &ZERO_KEY))
  1013. return stale;
  1014. for (i = 0; i < KEY_PTRS(k); i++) {
  1015. if (!ptr_available(c, k, i))
  1016. continue;
  1017. g = PTR_BUCKET(c, k, i);
  1018. if (gen_after(g->last_gc, PTR_GEN(k, i)))
  1019. g->last_gc = PTR_GEN(k, i);
  1020. if (ptr_stale(c, k, i)) {
  1021. stale = max(stale, ptr_stale(c, k, i));
  1022. continue;
  1023. }
  1024. cache_bug_on(GC_MARK(g) &&
  1025. (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
  1026. c, "inconsistent ptrs: mark = %llu, level = %i",
  1027. GC_MARK(g), level);
  1028. if (level)
  1029. SET_GC_MARK(g, GC_MARK_METADATA);
  1030. else if (KEY_DIRTY(k))
  1031. SET_GC_MARK(g, GC_MARK_DIRTY);
  1032. else if (!GC_MARK(g))
  1033. SET_GC_MARK(g, GC_MARK_RECLAIMABLE);
  1034. /* guard against overflow */
  1035. SET_GC_SECTORS_USED(g, min_t(unsigned int,
  1036. GC_SECTORS_USED(g) + KEY_SIZE(k),
  1037. MAX_GC_SECTORS_USED));
  1038. BUG_ON(!GC_SECTORS_USED(g));
  1039. }
  1040. return stale;
  1041. }
  1042. #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
  1043. void bch_initial_mark_key(struct cache_set *c, int level, struct bkey *k)
  1044. {
  1045. unsigned int i;
  1046. for (i = 0; i < KEY_PTRS(k); i++)
  1047. if (ptr_available(c, k, i) &&
  1048. !ptr_stale(c, k, i)) {
  1049. struct bucket *b = PTR_BUCKET(c, k, i);
  1050. b->gen = PTR_GEN(k, i);
  1051. if (level && bkey_cmp(k, &ZERO_KEY))
  1052. b->prio = BTREE_PRIO;
  1053. else if (!level && b->prio == BTREE_PRIO)
  1054. b->prio = INITIAL_PRIO;
  1055. }
  1056. __bch_btree_mark_key(c, level, k);
  1057. }
  1058. void bch_update_bucket_in_use(struct cache_set *c, struct gc_stat *stats)
  1059. {
  1060. stats->in_use = (c->nbuckets - c->avail_nbuckets) * 100 / c->nbuckets;
  1061. }
  1062. static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)
  1063. {
  1064. uint8_t stale = 0;
  1065. unsigned int keys = 0, good_keys = 0;
  1066. struct bkey *k;
  1067. struct btree_iter iter;
  1068. struct bset_tree *t;
  1069. gc->nodes++;
  1070. for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) {
  1071. stale = max(stale, btree_mark_key(b, k));
  1072. keys++;
  1073. if (bch_ptr_bad(&b->keys, k))
  1074. continue;
  1075. gc->key_bytes += bkey_u64s(k);
  1076. gc->nkeys++;
  1077. good_keys++;
  1078. gc->data += KEY_SIZE(k);
  1079. }
  1080. for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++)
  1081. btree_bug_on(t->size &&
  1082. bset_written(&b->keys, t) &&
  1083. bkey_cmp(&b->key, &t->end) < 0,
  1084. b, "found short btree key in gc");
  1085. if (b->c->gc_always_rewrite)
  1086. return true;
  1087. if (stale > 10)
  1088. return true;
  1089. if ((keys - good_keys) * 2 > keys)
  1090. return true;
  1091. return false;
  1092. }
  1093. #define GC_MERGE_NODES 4U
  1094. struct gc_merge_info {
  1095. struct btree *b;
  1096. unsigned int keys;
  1097. };
  1098. static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
  1099. struct keylist *insert_keys,
  1100. atomic_t *journal_ref,
  1101. struct bkey *replace_key);
  1102. static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
  1103. struct gc_stat *gc, struct gc_merge_info *r)
  1104. {
  1105. unsigned int i, nodes = 0, keys = 0, blocks;
  1106. struct btree *new_nodes[GC_MERGE_NODES];
  1107. struct keylist keylist;
  1108. struct closure cl;
  1109. struct bkey *k;
  1110. bch_keylist_init(&keylist);
  1111. if (btree_check_reserve(b, NULL))
  1112. return 0;
  1113. memset(new_nodes, 0, sizeof(new_nodes));
  1114. closure_init_stack(&cl);
  1115. while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b))
  1116. keys += r[nodes++].keys;
  1117. blocks = btree_default_blocks(b->c) * 2 / 3;
  1118. if (nodes < 2 ||
  1119. __set_blocks(b->keys.set[0].data, keys,
  1120. block_bytes(b->c)) > blocks * (nodes - 1))
  1121. return 0;
  1122. for (i = 0; i < nodes; i++) {
  1123. new_nodes[i] = btree_node_alloc_replacement(r[i].b, NULL);
  1124. if (IS_ERR_OR_NULL(new_nodes[i]))
  1125. goto out_nocoalesce;
  1126. }
  1127. /*
  1128. * We have to check the reserve here, after we've allocated our new
  1129. * nodes, to make sure the insert below will succeed - we also check
  1130. * before as an optimization to potentially avoid a bunch of expensive
  1131. * allocs/sorts
  1132. */
  1133. if (btree_check_reserve(b, NULL))
  1134. goto out_nocoalesce;
  1135. for (i = 0; i < nodes; i++)
  1136. mutex_lock(&new_nodes[i]->write_lock);
  1137. for (i = nodes - 1; i > 0; --i) {
  1138. struct bset *n1 = btree_bset_first(new_nodes[i]);
  1139. struct bset *n2 = btree_bset_first(new_nodes[i - 1]);
  1140. struct bkey *k, *last = NULL;
  1141. keys = 0;
  1142. if (i > 1) {
  1143. for (k = n2->start;
  1144. k < bset_bkey_last(n2);
  1145. k = bkey_next(k)) {
  1146. if (__set_blocks(n1, n1->keys + keys +
  1147. bkey_u64s(k),
  1148. block_bytes(b->c)) > blocks)
  1149. break;
  1150. last = k;
  1151. keys += bkey_u64s(k);
  1152. }
  1153. } else {
  1154. /*
  1155. * Last node we're not getting rid of - we're getting
  1156. * rid of the node at r[0]. Have to try and fit all of
  1157. * the remaining keys into this node; we can't ensure
  1158. * they will always fit due to rounding and variable
  1159. * length keys (shouldn't be possible in practice,
  1160. * though)
  1161. */
  1162. if (__set_blocks(n1, n1->keys + n2->keys,
  1163. block_bytes(b->c)) >
  1164. btree_blocks(new_nodes[i]))
  1165. goto out_unlock_nocoalesce;
  1166. keys = n2->keys;
  1167. /* Take the key of the node we're getting rid of */
  1168. last = &r->b->key;
  1169. }
  1170. BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c)) >
  1171. btree_blocks(new_nodes[i]));
  1172. if (last)
  1173. bkey_copy_key(&new_nodes[i]->key, last);
  1174. memcpy(bset_bkey_last(n1),
  1175. n2->start,
  1176. (void *) bset_bkey_idx(n2, keys) - (void *) n2->start);
  1177. n1->keys += keys;
  1178. r[i].keys = n1->keys;
  1179. memmove(n2->start,
  1180. bset_bkey_idx(n2, keys),
  1181. (void *) bset_bkey_last(n2) -
  1182. (void *) bset_bkey_idx(n2, keys));
  1183. n2->keys -= keys;
  1184. if (__bch_keylist_realloc(&keylist,
  1185. bkey_u64s(&new_nodes[i]->key)))
  1186. goto out_unlock_nocoalesce;
  1187. bch_btree_node_write(new_nodes[i], &cl);
  1188. bch_keylist_add(&keylist, &new_nodes[i]->key);
  1189. }
  1190. for (i = 0; i < nodes; i++)
  1191. mutex_unlock(&new_nodes[i]->write_lock);
  1192. closure_sync(&cl);
  1193. /* We emptied out this node */
  1194. BUG_ON(btree_bset_first(new_nodes[0])->keys);
  1195. btree_node_free(new_nodes[0]);
  1196. rw_unlock(true, new_nodes[0]);
  1197. new_nodes[0] = NULL;
  1198. for (i = 0; i < nodes; i++) {
  1199. if (__bch_keylist_realloc(&keylist, bkey_u64s(&r[i].b->key)))
  1200. goto out_nocoalesce;
  1201. make_btree_freeing_key(r[i].b, keylist.top);
  1202. bch_keylist_push(&keylist);
  1203. }
  1204. bch_btree_insert_node(b, op, &keylist, NULL, NULL);
  1205. BUG_ON(!bch_keylist_empty(&keylist));
  1206. for (i = 0; i < nodes; i++) {
  1207. btree_node_free(r[i].b);
  1208. rw_unlock(true, r[i].b);
  1209. r[i].b = new_nodes[i];
  1210. }
  1211. memmove(r, r + 1, sizeof(r[0]) * (nodes - 1));
  1212. r[nodes - 1].b = ERR_PTR(-EINTR);
  1213. trace_bcache_btree_gc_coalesce(nodes);
  1214. gc->nodes--;
  1215. bch_keylist_free(&keylist);
  1216. /* Invalidated our iterator */
  1217. return -EINTR;
  1218. out_unlock_nocoalesce:
  1219. for (i = 0; i < nodes; i++)
  1220. mutex_unlock(&new_nodes[i]->write_lock);
  1221. out_nocoalesce:
  1222. closure_sync(&cl);
  1223. while ((k = bch_keylist_pop(&keylist)))
  1224. if (!bkey_cmp(k, &ZERO_KEY))
  1225. atomic_dec(&b->c->prio_blocked);
  1226. bch_keylist_free(&keylist);
  1227. for (i = 0; i < nodes; i++)
  1228. if (!IS_ERR_OR_NULL(new_nodes[i])) {
  1229. btree_node_free(new_nodes[i]);
  1230. rw_unlock(true, new_nodes[i]);
  1231. }
  1232. return 0;
  1233. }
  1234. static int btree_gc_rewrite_node(struct btree *b, struct btree_op *op,
  1235. struct btree *replace)
  1236. {
  1237. struct keylist keys;
  1238. struct btree *n;
  1239. if (btree_check_reserve(b, NULL))
  1240. return 0;
  1241. n = btree_node_alloc_replacement(replace, NULL);
  1242. /* recheck reserve after allocating replacement node */
  1243. if (btree_check_reserve(b, NULL)) {
  1244. btree_node_free(n);
  1245. rw_unlock(true, n);
  1246. return 0;
  1247. }
  1248. bch_btree_node_write_sync(n);
  1249. bch_keylist_init(&keys);
  1250. bch_keylist_add(&keys, &n->key);
  1251. make_btree_freeing_key(replace, keys.top);
  1252. bch_keylist_push(&keys);
  1253. bch_btree_insert_node(b, op, &keys, NULL, NULL);
  1254. BUG_ON(!bch_keylist_empty(&keys));
  1255. btree_node_free(replace);
  1256. rw_unlock(true, n);
  1257. /* Invalidated our iterator */
  1258. return -EINTR;
  1259. }
  1260. static unsigned int btree_gc_count_keys(struct btree *b)
  1261. {
  1262. struct bkey *k;
  1263. struct btree_iter iter;
  1264. unsigned int ret = 0;
  1265. for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
  1266. ret += bkey_u64s(k);
  1267. return ret;
  1268. }
  1269. static size_t btree_gc_min_nodes(struct cache_set *c)
  1270. {
  1271. size_t min_nodes;
  1272. /*
  1273. * Since incremental GC would stop 100ms when front
  1274. * side I/O comes, so when there are many btree nodes,
  1275. * if GC only processes constant (100) nodes each time,
  1276. * GC would last a long time, and the front side I/Os
  1277. * would run out of the buckets (since no new bucket
  1278. * can be allocated during GC), and be blocked again.
  1279. * So GC should not process constant nodes, but varied
  1280. * nodes according to the number of btree nodes, which
  1281. * realized by dividing GC into constant(100) times,
  1282. * so when there are many btree nodes, GC can process
  1283. * more nodes each time, otherwise, GC will process less
  1284. * nodes each time (but no less than MIN_GC_NODES)
  1285. */
  1286. min_nodes = c->gc_stats.nodes / MAX_GC_TIMES;
  1287. if (min_nodes < MIN_GC_NODES)
  1288. min_nodes = MIN_GC_NODES;
  1289. return min_nodes;
  1290. }
  1291. static int btree_gc_recurse(struct btree *b, struct btree_op *op,
  1292. struct closure *writes, struct gc_stat *gc)
  1293. {
  1294. int ret = 0;
  1295. bool should_rewrite;
  1296. struct bkey *k;
  1297. struct btree_iter iter;
  1298. struct gc_merge_info r[GC_MERGE_NODES];
  1299. struct gc_merge_info *i, *last = r + ARRAY_SIZE(r) - 1;
  1300. bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done);
  1301. for (i = r; i < r + ARRAY_SIZE(r); i++)
  1302. i->b = ERR_PTR(-EINTR);
  1303. while (1) {
  1304. k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad);
  1305. if (k) {
  1306. r->b = bch_btree_node_get(b->c, op, k, b->level - 1,
  1307. true, b);
  1308. if (IS_ERR(r->b)) {
  1309. ret = PTR_ERR(r->b);
  1310. break;
  1311. }
  1312. r->keys = btree_gc_count_keys(r->b);
  1313. ret = btree_gc_coalesce(b, op, gc, r);
  1314. if (ret)
  1315. break;
  1316. }
  1317. if (!last->b)
  1318. break;
  1319. if (!IS_ERR(last->b)) {
  1320. should_rewrite = btree_gc_mark_node(last->b, gc);
  1321. if (should_rewrite) {
  1322. ret = btree_gc_rewrite_node(b, op, last->b);
  1323. if (ret)
  1324. break;
  1325. }
  1326. if (last->b->level) {
  1327. ret = btree_gc_recurse(last->b, op, writes, gc);
  1328. if (ret)
  1329. break;
  1330. }
  1331. bkey_copy_key(&b->c->gc_done, &last->b->key);
  1332. /*
  1333. * Must flush leaf nodes before gc ends, since replace
  1334. * operations aren't journalled
  1335. */
  1336. mutex_lock(&last->b->write_lock);
  1337. if (btree_node_dirty(last->b))
  1338. bch_btree_node_write(last->b, writes);
  1339. mutex_unlock(&last->b->write_lock);
  1340. rw_unlock(true, last->b);
  1341. }
  1342. memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1));
  1343. r->b = NULL;
  1344. if (atomic_read(&b->c->search_inflight) &&
  1345. gc->nodes >= gc->nodes_pre + btree_gc_min_nodes(b->c)) {
  1346. gc->nodes_pre = gc->nodes;
  1347. ret = -EAGAIN;
  1348. break;
  1349. }
  1350. if (need_resched()) {
  1351. ret = -EAGAIN;
  1352. break;
  1353. }
  1354. }
  1355. for (i = r; i < r + ARRAY_SIZE(r); i++)
  1356. if (!IS_ERR_OR_NULL(i->b)) {
  1357. mutex_lock(&i->b->write_lock);
  1358. if (btree_node_dirty(i->b))
  1359. bch_btree_node_write(i->b, writes);
  1360. mutex_unlock(&i->b->write_lock);
  1361. rw_unlock(true, i->b);
  1362. }
  1363. return ret;
  1364. }
  1365. static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
  1366. struct closure *writes, struct gc_stat *gc)
  1367. {
  1368. struct btree *n = NULL;
  1369. int ret = 0;
  1370. bool should_rewrite;
  1371. should_rewrite = btree_gc_mark_node(b, gc);
  1372. if (should_rewrite) {
  1373. n = btree_node_alloc_replacement(b, NULL);
  1374. if (!IS_ERR_OR_NULL(n)) {
  1375. bch_btree_node_write_sync(n);
  1376. bch_btree_set_root(n);
  1377. btree_node_free(b);
  1378. rw_unlock(true, n);
  1379. return -EINTR;
  1380. }
  1381. }
  1382. __bch_btree_mark_key(b->c, b->level + 1, &b->key);
  1383. if (b->level) {
  1384. ret = btree_gc_recurse(b, op, writes, gc);
  1385. if (ret)
  1386. return ret;
  1387. }
  1388. bkey_copy_key(&b->c->gc_done, &b->key);
  1389. return ret;
  1390. }
  1391. static void btree_gc_start(struct cache_set *c)
  1392. {
  1393. struct cache *ca;
  1394. struct bucket *b;
  1395. unsigned int i;
  1396. if (!c->gc_mark_valid)
  1397. return;
  1398. mutex_lock(&c->bucket_lock);
  1399. c->gc_mark_valid = 0;
  1400. c->gc_done = ZERO_KEY;
  1401. for_each_cache(ca, c, i)
  1402. for_each_bucket(b, ca) {
  1403. b->last_gc = b->gen;
  1404. if (!atomic_read(&b->pin)) {
  1405. SET_GC_MARK(b, 0);
  1406. SET_GC_SECTORS_USED(b, 0);
  1407. }
  1408. }
  1409. mutex_unlock(&c->bucket_lock);
  1410. }
  1411. static void bch_btree_gc_finish(struct cache_set *c)
  1412. {
  1413. struct bucket *b;
  1414. struct cache *ca;
  1415. unsigned int i;
  1416. mutex_lock(&c->bucket_lock);
  1417. set_gc_sectors(c);
  1418. c->gc_mark_valid = 1;
  1419. c->need_gc = 0;
  1420. for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
  1421. SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
  1422. GC_MARK_METADATA);
  1423. /* don't reclaim buckets to which writeback keys point */
  1424. rcu_read_lock();
  1425. for (i = 0; i < c->devices_max_used; i++) {
  1426. struct bcache_device *d = c->devices[i];
  1427. struct cached_dev *dc;
  1428. struct keybuf_key *w, *n;
  1429. unsigned int j;
  1430. if (!d || UUID_FLASH_ONLY(&c->uuids[i]))
  1431. continue;
  1432. dc = container_of(d, struct cached_dev, disk);
  1433. spin_lock(&dc->writeback_keys.lock);
  1434. rbtree_postorder_for_each_entry_safe(w, n,
  1435. &dc->writeback_keys.keys, node)
  1436. for (j = 0; j < KEY_PTRS(&w->key); j++)
  1437. SET_GC_MARK(PTR_BUCKET(c, &w->key, j),
  1438. GC_MARK_DIRTY);
  1439. spin_unlock(&dc->writeback_keys.lock);
  1440. }
  1441. rcu_read_unlock();
  1442. c->avail_nbuckets = 0;
  1443. for_each_cache(ca, c, i) {
  1444. uint64_t *i;
  1445. ca->invalidate_needs_gc = 0;
  1446. for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
  1447. SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
  1448. for (i = ca->prio_buckets;
  1449. i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
  1450. SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
  1451. for_each_bucket(b, ca) {
  1452. c->need_gc = max(c->need_gc, bucket_gc_gen(b));
  1453. if (atomic_read(&b->pin))
  1454. continue;
  1455. BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b));
  1456. if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE)
  1457. c->avail_nbuckets++;
  1458. }
  1459. }
  1460. mutex_unlock(&c->bucket_lock);
  1461. }
  1462. static void bch_btree_gc(struct cache_set *c)
  1463. {
  1464. int ret;
  1465. struct gc_stat stats;
  1466. struct closure writes;
  1467. struct btree_op op;
  1468. uint64_t start_time = local_clock();
  1469. trace_bcache_gc_start(c);
  1470. memset(&stats, 0, sizeof(struct gc_stat));
  1471. closure_init_stack(&writes);
  1472. bch_btree_op_init(&op, SHRT_MAX);
  1473. btree_gc_start(c);
  1474. /* if CACHE_SET_IO_DISABLE set, gc thread should stop too */
  1475. do {
  1476. ret = btree_root(gc_root, c, &op, &writes, &stats);
  1477. closure_sync(&writes);
  1478. cond_resched();
  1479. if (ret == -EAGAIN)
  1480. schedule_timeout_interruptible(msecs_to_jiffies
  1481. (GC_SLEEP_MS));
  1482. else if (ret)
  1483. pr_warn("gc failed!");
  1484. } while (ret && !test_bit(CACHE_SET_IO_DISABLE, &c->flags));
  1485. bch_btree_gc_finish(c);
  1486. wake_up_allocators(c);
  1487. bch_time_stats_update(&c->btree_gc_time, start_time);
  1488. stats.key_bytes *= sizeof(uint64_t);
  1489. stats.data <<= 9;
  1490. bch_update_bucket_in_use(c, &stats);
  1491. memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
  1492. trace_bcache_gc_end(c);
  1493. bch_moving_gc(c);
  1494. }
  1495. static bool gc_should_run(struct cache_set *c)
  1496. {
  1497. struct cache *ca;
  1498. unsigned int i;
  1499. for_each_cache(ca, c, i)
  1500. if (ca->invalidate_needs_gc)
  1501. return true;
  1502. if (atomic_read(&c->sectors_to_gc) < 0)
  1503. return true;
  1504. return false;
  1505. }
  1506. static int bch_gc_thread(void *arg)
  1507. {
  1508. struct cache_set *c = arg;
  1509. while (1) {
  1510. wait_event_interruptible(c->gc_wait,
  1511. kthread_should_stop() ||
  1512. test_bit(CACHE_SET_IO_DISABLE, &c->flags) ||
  1513. gc_should_run(c));
  1514. if (kthread_should_stop() ||
  1515. test_bit(CACHE_SET_IO_DISABLE, &c->flags))
  1516. break;
  1517. set_gc_sectors(c);
  1518. bch_btree_gc(c);
  1519. }
  1520. wait_for_kthread_stop();
  1521. return 0;
  1522. }
  1523. int bch_gc_thread_start(struct cache_set *c)
  1524. {
  1525. c->gc_thread = kthread_run(bch_gc_thread, c, "bcache_gc");
  1526. return PTR_ERR_OR_ZERO(c->gc_thread);
  1527. }
  1528. /* Initial partial gc */
  1529. static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)
  1530. {
  1531. int ret = 0;
  1532. struct bkey *k, *p = NULL;
  1533. struct btree_iter iter;
  1534. for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid)
  1535. bch_initial_mark_key(b->c, b->level, k);
  1536. bch_initial_mark_key(b->c, b->level + 1, &b->key);
  1537. if (b->level) {
  1538. bch_btree_iter_init(&b->keys, &iter, NULL);
  1539. do {
  1540. k = bch_btree_iter_next_filter(&iter, &b->keys,
  1541. bch_ptr_bad);
  1542. if (k) {
  1543. btree_node_prefetch(b, k);
  1544. /*
  1545. * initiallize c->gc_stats.nodes
  1546. * for incremental GC
  1547. */
  1548. b->c->gc_stats.nodes++;
  1549. }
  1550. if (p)
  1551. ret = btree(check_recurse, p, b, op);
  1552. p = k;
  1553. } while (p && !ret);
  1554. }
  1555. return ret;
  1556. }
  1557. int bch_btree_check(struct cache_set *c)
  1558. {
  1559. struct btree_op op;
  1560. bch_btree_op_init(&op, SHRT_MAX);
  1561. return btree_root(check_recurse, c, &op);
  1562. }
  1563. void bch_initial_gc_finish(struct cache_set *c)
  1564. {
  1565. struct cache *ca;
  1566. struct bucket *b;
  1567. unsigned int i;
  1568. bch_btree_gc_finish(c);
  1569. mutex_lock(&c->bucket_lock);
  1570. /*
  1571. * We need to put some unused buckets directly on the prio freelist in
  1572. * order to get the allocator thread started - it needs freed buckets in
  1573. * order to rewrite the prios and gens, and it needs to rewrite prios
  1574. * and gens in order to free buckets.
  1575. *
  1576. * This is only safe for buckets that have no live data in them, which
  1577. * there should always be some of.
  1578. */
  1579. for_each_cache(ca, c, i) {
  1580. for_each_bucket(b, ca) {
  1581. if (fifo_full(&ca->free[RESERVE_PRIO]) &&
  1582. fifo_full(&ca->free[RESERVE_BTREE]))
  1583. break;
  1584. if (bch_can_invalidate_bucket(ca, b) &&
  1585. !GC_MARK(b)) {
  1586. __bch_invalidate_one_bucket(ca, b);
  1587. if (!fifo_push(&ca->free[RESERVE_PRIO],
  1588. b - ca->buckets))
  1589. fifo_push(&ca->free[RESERVE_BTREE],
  1590. b - ca->buckets);
  1591. }
  1592. }
  1593. }
  1594. mutex_unlock(&c->bucket_lock);
  1595. }
  1596. /* Btree insertion */
  1597. static bool btree_insert_key(struct btree *b, struct bkey *k,
  1598. struct bkey *replace_key)
  1599. {
  1600. unsigned int status;
  1601. BUG_ON(bkey_cmp(k, &b->key) > 0);
  1602. status = bch_btree_insert_key(&b->keys, k, replace_key);
  1603. if (status != BTREE_INSERT_STATUS_NO_INSERT) {
  1604. bch_check_keys(&b->keys, "%u for %s", status,
  1605. replace_key ? "replace" : "insert");
  1606. trace_bcache_btree_insert_key(b, k, replace_key != NULL,
  1607. status);
  1608. return true;
  1609. } else
  1610. return false;
  1611. }
  1612. static size_t insert_u64s_remaining(struct btree *b)
  1613. {
  1614. long ret = bch_btree_keys_u64s_remaining(&b->keys);
  1615. /*
  1616. * Might land in the middle of an existing extent and have to split it
  1617. */
  1618. if (b->keys.ops->is_extents)
  1619. ret -= KEY_MAX_U64S;
  1620. return max(ret, 0L);
  1621. }
  1622. static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
  1623. struct keylist *insert_keys,
  1624. struct bkey *replace_key)
  1625. {
  1626. bool ret = false;
  1627. int oldsize = bch_count_data(&b->keys);
  1628. while (!bch_keylist_empty(insert_keys)) {
  1629. struct bkey *k = insert_keys->keys;
  1630. if (bkey_u64s(k) > insert_u64s_remaining(b))
  1631. break;
  1632. if (bkey_cmp(k, &b->key) <= 0) {
  1633. if (!b->level)
  1634. bkey_put(b->c, k);
  1635. ret |= btree_insert_key(b, k, replace_key);
  1636. bch_keylist_pop_front(insert_keys);
  1637. } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
  1638. BKEY_PADDED(key) temp;
  1639. bkey_copy(&temp.key, insert_keys->keys);
  1640. bch_cut_back(&b->key, &temp.key);
  1641. bch_cut_front(&b->key, insert_keys->keys);
  1642. ret |= btree_insert_key(b, &temp.key, replace_key);
  1643. break;
  1644. } else {
  1645. break;
  1646. }
  1647. }
  1648. if (!ret)
  1649. op->insert_collision = true;
  1650. BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
  1651. BUG_ON(bch_count_data(&b->keys) < oldsize);
  1652. return ret;
  1653. }
  1654. static int btree_split(struct btree *b, struct btree_op *op,
  1655. struct keylist *insert_keys,
  1656. struct bkey *replace_key)
  1657. {
  1658. bool split;
  1659. struct btree *n1, *n2 = NULL, *n3 = NULL;
  1660. uint64_t start_time = local_clock();
  1661. struct closure cl;
  1662. struct keylist parent_keys;
  1663. closure_init_stack(&cl);
  1664. bch_keylist_init(&parent_keys);
  1665. if (btree_check_reserve(b, op)) {
  1666. if (!b->level)
  1667. return -EINTR;
  1668. else
  1669. WARN(1, "insufficient reserve for split\n");
  1670. }
  1671. n1 = btree_node_alloc_replacement(b, op);
  1672. if (IS_ERR(n1))
  1673. goto err;
  1674. split = set_blocks(btree_bset_first(n1),
  1675. block_bytes(n1->c)) > (btree_blocks(b) * 4) / 5;
  1676. if (split) {
  1677. unsigned int keys = 0;
  1678. trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys);
  1679. n2 = bch_btree_node_alloc(b->c, op, b->level, b->parent);
  1680. if (IS_ERR(n2))
  1681. goto err_free1;
  1682. if (!b->parent) {
  1683. n3 = bch_btree_node_alloc(b->c, op, b->level + 1, NULL);
  1684. if (IS_ERR(n3))
  1685. goto err_free2;
  1686. }
  1687. mutex_lock(&n1->write_lock);
  1688. mutex_lock(&n2->write_lock);
  1689. bch_btree_insert_keys(n1, op, insert_keys, replace_key);
  1690. /*
  1691. * Has to be a linear search because we don't have an auxiliary
  1692. * search tree yet
  1693. */
  1694. while (keys < (btree_bset_first(n1)->keys * 3) / 5)
  1695. keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1),
  1696. keys));
  1697. bkey_copy_key(&n1->key,
  1698. bset_bkey_idx(btree_bset_first(n1), keys));
  1699. keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1), keys));
  1700. btree_bset_first(n2)->keys = btree_bset_first(n1)->keys - keys;
  1701. btree_bset_first(n1)->keys = keys;
  1702. memcpy(btree_bset_first(n2)->start,
  1703. bset_bkey_last(btree_bset_first(n1)),
  1704. btree_bset_first(n2)->keys * sizeof(uint64_t));
  1705. bkey_copy_key(&n2->key, &b->key);
  1706. bch_keylist_add(&parent_keys, &n2->key);
  1707. bch_btree_node_write(n2, &cl);
  1708. mutex_unlock(&n2->write_lock);
  1709. rw_unlock(true, n2);
  1710. } else {
  1711. trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys);
  1712. mutex_lock(&n1->write_lock);
  1713. bch_btree_insert_keys(n1, op, insert_keys, replace_key);
  1714. }
  1715. bch_keylist_add(&parent_keys, &n1->key);
  1716. bch_btree_node_write(n1, &cl);
  1717. mutex_unlock(&n1->write_lock);
  1718. if (n3) {
  1719. /* Depth increases, make a new root */
  1720. mutex_lock(&n3->write_lock);
  1721. bkey_copy_key(&n3->key, &MAX_KEY);
  1722. bch_btree_insert_keys(n3, op, &parent_keys, NULL);
  1723. bch_btree_node_write(n3, &cl);
  1724. mutex_unlock(&n3->write_lock);
  1725. closure_sync(&cl);
  1726. bch_btree_set_root(n3);
  1727. rw_unlock(true, n3);
  1728. } else if (!b->parent) {
  1729. /* Root filled up but didn't need to be split */
  1730. closure_sync(&cl);
  1731. bch_btree_set_root(n1);
  1732. } else {
  1733. /* Split a non root node */
  1734. closure_sync(&cl);
  1735. make_btree_freeing_key(b, parent_keys.top);
  1736. bch_keylist_push(&parent_keys);
  1737. bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL);
  1738. BUG_ON(!bch_keylist_empty(&parent_keys));
  1739. }
  1740. btree_node_free(b);
  1741. rw_unlock(true, n1);
  1742. bch_time_stats_update(&b->c->btree_split_time, start_time);
  1743. return 0;
  1744. err_free2:
  1745. bkey_put(b->c, &n2->key);
  1746. btree_node_free(n2);
  1747. rw_unlock(true, n2);
  1748. err_free1:
  1749. bkey_put(b->c, &n1->key);
  1750. btree_node_free(n1);
  1751. rw_unlock(true, n1);
  1752. err:
  1753. WARN(1, "bcache: btree split failed (level %u)", b->level);
  1754. if (n3 == ERR_PTR(-EAGAIN) ||
  1755. n2 == ERR_PTR(-EAGAIN) ||
  1756. n1 == ERR_PTR(-EAGAIN))
  1757. return -EAGAIN;
  1758. return -ENOMEM;
  1759. }
  1760. static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
  1761. struct keylist *insert_keys,
  1762. atomic_t *journal_ref,
  1763. struct bkey *replace_key)
  1764. {
  1765. struct closure cl;
  1766. BUG_ON(b->level && replace_key);
  1767. closure_init_stack(&cl);
  1768. mutex_lock(&b->write_lock);
  1769. if (write_block(b) != btree_bset_last(b) &&
  1770. b->keys.last_set_unwritten)
  1771. bch_btree_init_next(b); /* just wrote a set */
  1772. if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) {
  1773. mutex_unlock(&b->write_lock);
  1774. goto split;
  1775. }
  1776. BUG_ON(write_block(b) != btree_bset_last(b));
  1777. if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) {
  1778. if (!b->level)
  1779. bch_btree_leaf_dirty(b, journal_ref);
  1780. else
  1781. bch_btree_node_write(b, &cl);
  1782. }
  1783. mutex_unlock(&b->write_lock);
  1784. /* wait for btree node write if necessary, after unlock */
  1785. closure_sync(&cl);
  1786. return 0;
  1787. split:
  1788. if (current->bio_list) {
  1789. op->lock = b->c->root->level + 1;
  1790. return -EAGAIN;
  1791. } else if (op->lock <= b->c->root->level) {
  1792. op->lock = b->c->root->level + 1;
  1793. return -EINTR;
  1794. } else {
  1795. /* Invalidated all iterators */
  1796. int ret = btree_split(b, op, insert_keys, replace_key);
  1797. if (bch_keylist_empty(insert_keys))
  1798. return 0;
  1799. else if (!ret)
  1800. return -EINTR;
  1801. return ret;
  1802. }
  1803. }
  1804. int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
  1805. struct bkey *check_key)
  1806. {
  1807. int ret = -EINTR;
  1808. uint64_t btree_ptr = b->key.ptr[0];
  1809. unsigned long seq = b->seq;
  1810. struct keylist insert;
  1811. bool upgrade = op->lock == -1;
  1812. bch_keylist_init(&insert);
  1813. if (upgrade) {
  1814. rw_unlock(false, b);
  1815. rw_lock(true, b, b->level);
  1816. if (b->key.ptr[0] != btree_ptr ||
  1817. b->seq != seq + 1) {
  1818. op->lock = b->level;
  1819. goto out;
  1820. }
  1821. }
  1822. SET_KEY_PTRS(check_key, 1);
  1823. get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
  1824. SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
  1825. bch_keylist_add(&insert, check_key);
  1826. ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
  1827. BUG_ON(!ret && !bch_keylist_empty(&insert));
  1828. out:
  1829. if (upgrade)
  1830. downgrade_write(&b->lock);
  1831. return ret;
  1832. }
  1833. struct btree_insert_op {
  1834. struct btree_op op;
  1835. struct keylist *keys;
  1836. atomic_t *journal_ref;
  1837. struct bkey *replace_key;
  1838. };
  1839. static int btree_insert_fn(struct btree_op *b_op, struct btree *b)
  1840. {
  1841. struct btree_insert_op *op = container_of(b_op,
  1842. struct btree_insert_op, op);
  1843. int ret = bch_btree_insert_node(b, &op->op, op->keys,
  1844. op->journal_ref, op->replace_key);
  1845. if (ret && !bch_keylist_empty(op->keys))
  1846. return ret;
  1847. else
  1848. return MAP_DONE;
  1849. }
  1850. int bch_btree_insert(struct cache_set *c, struct keylist *keys,
  1851. atomic_t *journal_ref, struct bkey *replace_key)
  1852. {
  1853. struct btree_insert_op op;
  1854. int ret = 0;
  1855. BUG_ON(current->bio_list);
  1856. BUG_ON(bch_keylist_empty(keys));
  1857. bch_btree_op_init(&op.op, 0);
  1858. op.keys = keys;
  1859. op.journal_ref = journal_ref;
  1860. op.replace_key = replace_key;
  1861. while (!ret && !bch_keylist_empty(keys)) {
  1862. op.op.lock = 0;
  1863. ret = bch_btree_map_leaf_nodes(&op.op, c,
  1864. &START_KEY(keys->keys),
  1865. btree_insert_fn);
  1866. }
  1867. if (ret) {
  1868. struct bkey *k;
  1869. pr_err("error %i", ret);
  1870. while ((k = bch_keylist_pop(keys)))
  1871. bkey_put(c, k);
  1872. } else if (op.op.insert_collision)
  1873. ret = -ESRCH;
  1874. return ret;
  1875. }
  1876. void bch_btree_set_root(struct btree *b)
  1877. {
  1878. unsigned int i;
  1879. struct closure cl;
  1880. closure_init_stack(&cl);
  1881. trace_bcache_btree_set_root(b);
  1882. BUG_ON(!b->written);
  1883. for (i = 0; i < KEY_PTRS(&b->key); i++)
  1884. BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
  1885. mutex_lock(&b->c->bucket_lock);
  1886. list_del_init(&b->list);
  1887. mutex_unlock(&b->c->bucket_lock);
  1888. b->c->root = b;
  1889. bch_journal_meta(b->c, &cl);
  1890. closure_sync(&cl);
  1891. }
  1892. /* Map across nodes or keys */
  1893. static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
  1894. struct bkey *from,
  1895. btree_map_nodes_fn *fn, int flags)
  1896. {
  1897. int ret = MAP_CONTINUE;
  1898. if (b->level) {
  1899. struct bkey *k;
  1900. struct btree_iter iter;
  1901. bch_btree_iter_init(&b->keys, &iter, from);
  1902. while ((k = bch_btree_iter_next_filter(&iter, &b->keys,
  1903. bch_ptr_bad))) {
  1904. ret = btree(map_nodes_recurse, k, b,
  1905. op, from, fn, flags);
  1906. from = NULL;
  1907. if (ret != MAP_CONTINUE)
  1908. return ret;
  1909. }
  1910. }
  1911. if (!b->level || flags == MAP_ALL_NODES)
  1912. ret = fn(op, b);
  1913. return ret;
  1914. }
  1915. int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
  1916. struct bkey *from, btree_map_nodes_fn *fn, int flags)
  1917. {
  1918. return btree_root(map_nodes_recurse, c, op, from, fn, flags);
  1919. }
  1920. static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
  1921. struct bkey *from, btree_map_keys_fn *fn,
  1922. int flags)
  1923. {
  1924. int ret = MAP_CONTINUE;
  1925. struct bkey *k;
  1926. struct btree_iter iter;
  1927. bch_btree_iter_init(&b->keys, &iter, from);
  1928. while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) {
  1929. ret = !b->level
  1930. ? fn(op, b, k)
  1931. : btree(map_keys_recurse, k, b, op, from, fn, flags);
  1932. from = NULL;
  1933. if (ret != MAP_CONTINUE)
  1934. return ret;
  1935. }
  1936. if (!b->level && (flags & MAP_END_KEY))
  1937. ret = fn(op, b, &KEY(KEY_INODE(&b->key),
  1938. KEY_OFFSET(&b->key), 0));
  1939. return ret;
  1940. }
  1941. int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
  1942. struct bkey *from, btree_map_keys_fn *fn, int flags)
  1943. {
  1944. return btree_root(map_keys_recurse, c, op, from, fn, flags);
  1945. }
  1946. /* Keybuf code */
  1947. static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
  1948. {
  1949. /* Overlapping keys compare equal */
  1950. if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
  1951. return -1;
  1952. if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
  1953. return 1;
  1954. return 0;
  1955. }
  1956. static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
  1957. struct keybuf_key *r)
  1958. {
  1959. return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
  1960. }
  1961. struct refill {
  1962. struct btree_op op;
  1963. unsigned int nr_found;
  1964. struct keybuf *buf;
  1965. struct bkey *end;
  1966. keybuf_pred_fn *pred;
  1967. };
  1968. static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
  1969. struct bkey *k)
  1970. {
  1971. struct refill *refill = container_of(op, struct refill, op);
  1972. struct keybuf *buf = refill->buf;
  1973. int ret = MAP_CONTINUE;
  1974. if (bkey_cmp(k, refill->end) > 0) {
  1975. ret = MAP_DONE;
  1976. goto out;
  1977. }
  1978. if (!KEY_SIZE(k)) /* end key */
  1979. goto out;
  1980. if (refill->pred(buf, k)) {
  1981. struct keybuf_key *w;
  1982. spin_lock(&buf->lock);
  1983. w = array_alloc(&buf->freelist);
  1984. if (!w) {
  1985. spin_unlock(&buf->lock);
  1986. return MAP_DONE;
  1987. }
  1988. w->private = NULL;
  1989. bkey_copy(&w->key, k);
  1990. if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
  1991. array_free(&buf->freelist, w);
  1992. else
  1993. refill->nr_found++;
  1994. if (array_freelist_empty(&buf->freelist))
  1995. ret = MAP_DONE;
  1996. spin_unlock(&buf->lock);
  1997. }
  1998. out:
  1999. buf->last_scanned = *k;
  2000. return ret;
  2001. }
  2002. void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
  2003. struct bkey *end, keybuf_pred_fn *pred)
  2004. {
  2005. struct bkey start = buf->last_scanned;
  2006. struct refill refill;
  2007. cond_resched();
  2008. bch_btree_op_init(&refill.op, -1);
  2009. refill.nr_found = 0;
  2010. refill.buf = buf;
  2011. refill.end = end;
  2012. refill.pred = pred;
  2013. bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
  2014. refill_keybuf_fn, MAP_END_KEY);
  2015. trace_bcache_keyscan(refill.nr_found,
  2016. KEY_INODE(&start), KEY_OFFSET(&start),
  2017. KEY_INODE(&buf->last_scanned),
  2018. KEY_OFFSET(&buf->last_scanned));
  2019. spin_lock(&buf->lock);
  2020. if (!RB_EMPTY_ROOT(&buf->keys)) {
  2021. struct keybuf_key *w;
  2022. w = RB_FIRST(&buf->keys, struct keybuf_key, node);
  2023. buf->start = START_KEY(&w->key);
  2024. w = RB_LAST(&buf->keys, struct keybuf_key, node);
  2025. buf->end = w->key;
  2026. } else {
  2027. buf->start = MAX_KEY;
  2028. buf->end = MAX_KEY;
  2029. }
  2030. spin_unlock(&buf->lock);
  2031. }
  2032. static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
  2033. {
  2034. rb_erase(&w->node, &buf->keys);
  2035. array_free(&buf->freelist, w);
  2036. }
  2037. void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
  2038. {
  2039. spin_lock(&buf->lock);
  2040. __bch_keybuf_del(buf, w);
  2041. spin_unlock(&buf->lock);
  2042. }
  2043. bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
  2044. struct bkey *end)
  2045. {
  2046. bool ret = false;
  2047. struct keybuf_key *p, *w, s;
  2048. s.key = *start;
  2049. if (bkey_cmp(end, &buf->start) <= 0 ||
  2050. bkey_cmp(start, &buf->end) >= 0)
  2051. return false;
  2052. spin_lock(&buf->lock);
  2053. w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
  2054. while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
  2055. p = w;
  2056. w = RB_NEXT(w, node);
  2057. if (p->private)
  2058. ret = true;
  2059. else
  2060. __bch_keybuf_del(buf, p);
  2061. }
  2062. spin_unlock(&buf->lock);
  2063. return ret;
  2064. }
  2065. struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
  2066. {
  2067. struct keybuf_key *w;
  2068. spin_lock(&buf->lock);
  2069. w = RB_FIRST(&buf->keys, struct keybuf_key, node);
  2070. while (w && w->private)
  2071. w = RB_NEXT(w, node);
  2072. if (w)
  2073. w->private = ERR_PTR(-EINTR);
  2074. spin_unlock(&buf->lock);
  2075. return w;
  2076. }
  2077. struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
  2078. struct keybuf *buf,
  2079. struct bkey *end,
  2080. keybuf_pred_fn *pred)
  2081. {
  2082. struct keybuf_key *ret;
  2083. while (1) {
  2084. ret = bch_keybuf_next(buf);
  2085. if (ret)
  2086. break;
  2087. if (bkey_cmp(&buf->last_scanned, end) >= 0) {
  2088. pr_debug("scan finished");
  2089. break;
  2090. }
  2091. bch_refill_keybuf(c, buf, end, pred);
  2092. }
  2093. return ret;
  2094. }
  2095. void bch_keybuf_init(struct keybuf *buf)
  2096. {
  2097. buf->last_scanned = MAX_KEY;
  2098. buf->keys = RB_ROOT;
  2099. spin_lock_init(&buf->lock);
  2100. array_allocator_init(&buf->freelist);
  2101. }
  2102. void bch_btree_exit(void)
  2103. {
  2104. if (btree_io_wq)
  2105. destroy_workqueue(btree_io_wq);
  2106. }
  2107. int __init bch_btree_init(void)
  2108. {
  2109. btree_io_wq = alloc_workqueue("bch_btree_io", WQ_MEM_RECLAIM, 0);
  2110. if (!btree_io_wq)
  2111. return -ENOMEM;
  2112. return 0;
  2113. }