super.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bcache setup/teardown code, and some metadata io - read a superblock and
  4. * figure out what to do with it.
  5. *
  6. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  7. * Copyright 2012 Google, Inc.
  8. */
  9. #include "bcache.h"
  10. #include "btree.h"
  11. #include "debug.h"
  12. #include "extents.h"
  13. #include "request.h"
  14. #include "writeback.h"
  15. #include <linux/blkdev.h>
  16. #include <linux/buffer_head.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/genhd.h>
  19. #include <linux/idr.h>
  20. #include <linux/kthread.h>
  21. #include <linux/module.h>
  22. #include <linux/random.h>
  23. #include <linux/reboot.h>
  24. #include <linux/sysfs.h>
  25. unsigned int bch_cutoff_writeback;
  26. unsigned int bch_cutoff_writeback_sync;
  27. static const char bcache_magic[] = {
  28. 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
  29. 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
  30. };
  31. static const char invalid_uuid[] = {
  32. 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
  33. 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
  34. };
  35. static struct kobject *bcache_kobj;
  36. struct mutex bch_register_lock;
  37. bool bcache_is_reboot;
  38. LIST_HEAD(bch_cache_sets);
  39. static LIST_HEAD(uncached_devices);
  40. static int bcache_major;
  41. static DEFINE_IDA(bcache_device_idx);
  42. static wait_queue_head_t unregister_wait;
  43. struct workqueue_struct *bcache_wq;
  44. struct workqueue_struct *bch_flush_wq;
  45. struct workqueue_struct *bch_journal_wq;
  46. #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
  47. /* limitation of partitions number on single bcache device */
  48. #define BCACHE_MINORS 128
  49. /* limitation of bcache devices number on single system */
  50. #define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS)
  51. /* Superblock */
  52. static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
  53. struct page **res)
  54. {
  55. const char *err;
  56. struct cache_sb *s;
  57. struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
  58. unsigned int i;
  59. if (!bh)
  60. return "IO error";
  61. s = (struct cache_sb *) bh->b_data;
  62. sb->offset = le64_to_cpu(s->offset);
  63. sb->version = le64_to_cpu(s->version);
  64. memcpy(sb->magic, s->magic, 16);
  65. memcpy(sb->uuid, s->uuid, 16);
  66. memcpy(sb->set_uuid, s->set_uuid, 16);
  67. memcpy(sb->label, s->label, SB_LABEL_SIZE);
  68. sb->flags = le64_to_cpu(s->flags);
  69. sb->seq = le64_to_cpu(s->seq);
  70. sb->last_mount = le32_to_cpu(s->last_mount);
  71. sb->first_bucket = le16_to_cpu(s->first_bucket);
  72. sb->keys = le16_to_cpu(s->keys);
  73. for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
  74. sb->d[i] = le64_to_cpu(s->d[i]);
  75. pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
  76. sb->version, sb->flags, sb->seq, sb->keys);
  77. err = "Not a bcache superblock";
  78. if (sb->offset != SB_SECTOR)
  79. goto err;
  80. if (memcmp(sb->magic, bcache_magic, 16))
  81. goto err;
  82. err = "Too many journal buckets";
  83. if (sb->keys > SB_JOURNAL_BUCKETS)
  84. goto err;
  85. err = "Bad checksum";
  86. if (s->csum != csum_set(s))
  87. goto err;
  88. err = "Bad UUID";
  89. if (bch_is_zero(sb->uuid, 16))
  90. goto err;
  91. sb->block_size = le16_to_cpu(s->block_size);
  92. err = "Superblock block size smaller than device block size";
  93. if (sb->block_size << 9 < bdev_logical_block_size(bdev))
  94. goto err;
  95. switch (sb->version) {
  96. case BCACHE_SB_VERSION_BDEV:
  97. sb->data_offset = BDEV_DATA_START_DEFAULT;
  98. break;
  99. case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
  100. sb->data_offset = le64_to_cpu(s->data_offset);
  101. err = "Bad data offset";
  102. if (sb->data_offset < BDEV_DATA_START_DEFAULT)
  103. goto err;
  104. break;
  105. case BCACHE_SB_VERSION_CDEV:
  106. case BCACHE_SB_VERSION_CDEV_WITH_UUID:
  107. sb->nbuckets = le64_to_cpu(s->nbuckets);
  108. sb->bucket_size = le16_to_cpu(s->bucket_size);
  109. sb->nr_in_set = le16_to_cpu(s->nr_in_set);
  110. sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
  111. err = "Too many buckets";
  112. if (sb->nbuckets > LONG_MAX)
  113. goto err;
  114. err = "Not enough buckets";
  115. if (sb->nbuckets < 1 << 7)
  116. goto err;
  117. err = "Bad block/bucket size";
  118. if (!is_power_of_2(sb->block_size) ||
  119. sb->block_size > PAGE_SECTORS ||
  120. !is_power_of_2(sb->bucket_size) ||
  121. sb->bucket_size < PAGE_SECTORS)
  122. goto err;
  123. err = "Invalid superblock: device too small";
  124. if (get_capacity(bdev->bd_disk) <
  125. sb->bucket_size * sb->nbuckets)
  126. goto err;
  127. err = "Bad UUID";
  128. if (bch_is_zero(sb->set_uuid, 16))
  129. goto err;
  130. err = "Bad cache device number in set";
  131. if (!sb->nr_in_set ||
  132. sb->nr_in_set <= sb->nr_this_dev ||
  133. sb->nr_in_set > MAX_CACHES_PER_SET)
  134. goto err;
  135. err = "Journal buckets not sequential";
  136. for (i = 0; i < sb->keys; i++)
  137. if (sb->d[i] != sb->first_bucket + i)
  138. goto err;
  139. err = "Too many journal buckets";
  140. if (sb->first_bucket + sb->keys > sb->nbuckets)
  141. goto err;
  142. err = "Invalid superblock: first bucket comes before end of super";
  143. if (sb->first_bucket * sb->bucket_size < 16)
  144. goto err;
  145. break;
  146. default:
  147. err = "Unsupported superblock version";
  148. goto err;
  149. }
  150. sb->last_mount = (u32)ktime_get_real_seconds();
  151. err = NULL;
  152. get_page(bh->b_page);
  153. *res = bh->b_page;
  154. err:
  155. put_bh(bh);
  156. return err;
  157. }
  158. static void write_bdev_super_endio(struct bio *bio)
  159. {
  160. struct cached_dev *dc = bio->bi_private;
  161. if (bio->bi_status)
  162. bch_count_backing_io_errors(dc, bio);
  163. closure_put(&dc->sb_write);
  164. }
  165. static void __write_super(struct cache_sb *sb, struct bio *bio)
  166. {
  167. struct cache_sb *out = page_address(bio_first_page_all(bio));
  168. unsigned int i;
  169. bio->bi_iter.bi_sector = SB_SECTOR;
  170. bio->bi_iter.bi_size = SB_SIZE;
  171. bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
  172. bch_bio_map(bio, NULL);
  173. out->offset = cpu_to_le64(sb->offset);
  174. out->version = cpu_to_le64(sb->version);
  175. memcpy(out->uuid, sb->uuid, 16);
  176. memcpy(out->set_uuid, sb->set_uuid, 16);
  177. memcpy(out->label, sb->label, SB_LABEL_SIZE);
  178. out->flags = cpu_to_le64(sb->flags);
  179. out->seq = cpu_to_le64(sb->seq);
  180. out->last_mount = cpu_to_le32(sb->last_mount);
  181. out->first_bucket = cpu_to_le16(sb->first_bucket);
  182. out->keys = cpu_to_le16(sb->keys);
  183. for (i = 0; i < sb->keys; i++)
  184. out->d[i] = cpu_to_le64(sb->d[i]);
  185. out->csum = csum_set(out);
  186. pr_debug("ver %llu, flags %llu, seq %llu",
  187. sb->version, sb->flags, sb->seq);
  188. submit_bio(bio);
  189. }
  190. static void bch_write_bdev_super_unlock(struct closure *cl)
  191. {
  192. struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
  193. up(&dc->sb_write_mutex);
  194. }
  195. void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
  196. {
  197. struct closure *cl = &dc->sb_write;
  198. struct bio *bio = &dc->sb_bio;
  199. down(&dc->sb_write_mutex);
  200. closure_init(cl, parent);
  201. bio_reset(bio);
  202. bio_set_dev(bio, dc->bdev);
  203. bio->bi_end_io = write_bdev_super_endio;
  204. bio->bi_private = dc;
  205. closure_get(cl);
  206. /* I/O request sent to backing device */
  207. __write_super(&dc->sb, bio);
  208. closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
  209. }
  210. static void write_super_endio(struct bio *bio)
  211. {
  212. struct cache *ca = bio->bi_private;
  213. /* is_read = 0 */
  214. bch_count_io_errors(ca, bio->bi_status, 0,
  215. "writing superblock");
  216. closure_put(&ca->set->sb_write);
  217. }
  218. static void bcache_write_super_unlock(struct closure *cl)
  219. {
  220. struct cache_set *c = container_of(cl, struct cache_set, sb_write);
  221. up(&c->sb_write_mutex);
  222. }
  223. void bcache_write_super(struct cache_set *c)
  224. {
  225. struct closure *cl = &c->sb_write;
  226. struct cache *ca;
  227. unsigned int i;
  228. down(&c->sb_write_mutex);
  229. closure_init(cl, &c->cl);
  230. c->sb.seq++;
  231. for_each_cache(ca, c, i) {
  232. struct bio *bio = &ca->sb_bio;
  233. ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
  234. ca->sb.seq = c->sb.seq;
  235. ca->sb.last_mount = c->sb.last_mount;
  236. SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
  237. bio_reset(bio);
  238. bio_set_dev(bio, ca->bdev);
  239. bio->bi_end_io = write_super_endio;
  240. bio->bi_private = ca;
  241. closure_get(cl);
  242. __write_super(&ca->sb, bio);
  243. }
  244. closure_return_with_destructor(cl, bcache_write_super_unlock);
  245. }
  246. /* UUID io */
  247. static void uuid_endio(struct bio *bio)
  248. {
  249. struct closure *cl = bio->bi_private;
  250. struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
  251. cache_set_err_on(bio->bi_status, c, "accessing uuids");
  252. bch_bbio_free(bio, c);
  253. closure_put(cl);
  254. }
  255. static void uuid_io_unlock(struct closure *cl)
  256. {
  257. struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
  258. up(&c->uuid_write_mutex);
  259. }
  260. static void uuid_io(struct cache_set *c, int op, unsigned long op_flags,
  261. struct bkey *k, struct closure *parent)
  262. {
  263. struct closure *cl = &c->uuid_write;
  264. struct uuid_entry *u;
  265. unsigned int i;
  266. char buf[80];
  267. BUG_ON(!parent);
  268. down(&c->uuid_write_mutex);
  269. closure_init(cl, parent);
  270. for (i = 0; i < KEY_PTRS(k); i++) {
  271. struct bio *bio = bch_bbio_alloc(c);
  272. bio->bi_opf = REQ_SYNC | REQ_META | op_flags;
  273. bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
  274. bio->bi_end_io = uuid_endio;
  275. bio->bi_private = cl;
  276. bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
  277. bch_bio_map(bio, c->uuids);
  278. bch_submit_bbio(bio, c, k, i);
  279. if (op != REQ_OP_WRITE)
  280. break;
  281. }
  282. bch_extent_to_text(buf, sizeof(buf), k);
  283. pr_debug("%s UUIDs at %s", op == REQ_OP_WRITE ? "wrote" : "read", buf);
  284. for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
  285. if (!bch_is_zero(u->uuid, 16))
  286. pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
  287. u - c->uuids, u->uuid, u->label,
  288. u->first_reg, u->last_reg, u->invalidated);
  289. closure_return_with_destructor(cl, uuid_io_unlock);
  290. }
  291. static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
  292. {
  293. struct bkey *k = &j->uuid_bucket;
  294. if (__bch_btree_ptr_invalid(c, k))
  295. return "bad uuid pointer";
  296. bkey_copy(&c->uuid_bucket, k);
  297. uuid_io(c, REQ_OP_READ, 0, k, cl);
  298. if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
  299. struct uuid_entry_v0 *u0 = (void *) c->uuids;
  300. struct uuid_entry *u1 = (void *) c->uuids;
  301. int i;
  302. closure_sync(cl);
  303. /*
  304. * Since the new uuid entry is bigger than the old, we have to
  305. * convert starting at the highest memory address and work down
  306. * in order to do it in place
  307. */
  308. for (i = c->nr_uuids - 1;
  309. i >= 0;
  310. --i) {
  311. memcpy(u1[i].uuid, u0[i].uuid, 16);
  312. memcpy(u1[i].label, u0[i].label, 32);
  313. u1[i].first_reg = u0[i].first_reg;
  314. u1[i].last_reg = u0[i].last_reg;
  315. u1[i].invalidated = u0[i].invalidated;
  316. u1[i].flags = 0;
  317. u1[i].sectors = 0;
  318. }
  319. }
  320. return NULL;
  321. }
  322. static int __uuid_write(struct cache_set *c)
  323. {
  324. BKEY_PADDED(key) k;
  325. struct closure cl;
  326. struct cache *ca;
  327. closure_init_stack(&cl);
  328. lockdep_assert_held(&bch_register_lock);
  329. if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, true))
  330. return 1;
  331. SET_KEY_SIZE(&k.key, c->sb.bucket_size);
  332. uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl);
  333. closure_sync(&cl);
  334. /* Only one bucket used for uuid write */
  335. ca = PTR_CACHE(c, &k.key, 0);
  336. atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written);
  337. bkey_copy(&c->uuid_bucket, &k.key);
  338. bkey_put(c, &k.key);
  339. return 0;
  340. }
  341. int bch_uuid_write(struct cache_set *c)
  342. {
  343. int ret = __uuid_write(c);
  344. if (!ret)
  345. bch_journal_meta(c, NULL);
  346. return ret;
  347. }
  348. static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
  349. {
  350. struct uuid_entry *u;
  351. for (u = c->uuids;
  352. u < c->uuids + c->nr_uuids; u++)
  353. if (!memcmp(u->uuid, uuid, 16))
  354. return u;
  355. return NULL;
  356. }
  357. static struct uuid_entry *uuid_find_empty(struct cache_set *c)
  358. {
  359. static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  360. return uuid_find(c, zero_uuid);
  361. }
  362. /*
  363. * Bucket priorities/gens:
  364. *
  365. * For each bucket, we store on disk its
  366. * 8 bit gen
  367. * 16 bit priority
  368. *
  369. * See alloc.c for an explanation of the gen. The priority is used to implement
  370. * lru (and in the future other) cache replacement policies; for most purposes
  371. * it's just an opaque integer.
  372. *
  373. * The gens and the priorities don't have a whole lot to do with each other, and
  374. * it's actually the gens that must be written out at specific times - it's no
  375. * big deal if the priorities don't get written, if we lose them we just reuse
  376. * buckets in suboptimal order.
  377. *
  378. * On disk they're stored in a packed array, and in as many buckets are required
  379. * to fit them all. The buckets we use to store them form a list; the journal
  380. * header points to the first bucket, the first bucket points to the second
  381. * bucket, et cetera.
  382. *
  383. * This code is used by the allocation code; periodically (whenever it runs out
  384. * of buckets to allocate from) the allocation code will invalidate some
  385. * buckets, but it can't use those buckets until their new gens are safely on
  386. * disk.
  387. */
  388. static void prio_endio(struct bio *bio)
  389. {
  390. struct cache *ca = bio->bi_private;
  391. cache_set_err_on(bio->bi_status, ca->set, "accessing priorities");
  392. bch_bbio_free(bio, ca->set);
  393. closure_put(&ca->prio);
  394. }
  395. static void prio_io(struct cache *ca, uint64_t bucket, int op,
  396. unsigned long op_flags)
  397. {
  398. struct closure *cl = &ca->prio;
  399. struct bio *bio = bch_bbio_alloc(ca->set);
  400. closure_init_stack(cl);
  401. bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size;
  402. bio_set_dev(bio, ca->bdev);
  403. bio->bi_iter.bi_size = bucket_bytes(ca);
  404. bio->bi_end_io = prio_endio;
  405. bio->bi_private = ca;
  406. bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
  407. bch_bio_map(bio, ca->disk_buckets);
  408. closure_bio_submit(ca->set, bio, &ca->prio);
  409. closure_sync(cl);
  410. }
  411. int bch_prio_write(struct cache *ca, bool wait)
  412. {
  413. int i;
  414. struct bucket *b;
  415. struct closure cl;
  416. pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu",
  417. fifo_used(&ca->free[RESERVE_PRIO]),
  418. fifo_used(&ca->free[RESERVE_NONE]),
  419. fifo_used(&ca->free_inc));
  420. /*
  421. * Pre-check if there are enough free buckets. In the non-blocking
  422. * scenario it's better to fail early rather than starting to allocate
  423. * buckets and do a cleanup later in case of failure.
  424. */
  425. if (!wait) {
  426. size_t avail = fifo_used(&ca->free[RESERVE_PRIO]) +
  427. fifo_used(&ca->free[RESERVE_NONE]);
  428. if (prio_buckets(ca) > avail)
  429. return -ENOMEM;
  430. }
  431. closure_init_stack(&cl);
  432. lockdep_assert_held(&ca->set->bucket_lock);
  433. ca->disk_buckets->seq++;
  434. atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
  435. &ca->meta_sectors_written);
  436. for (i = prio_buckets(ca) - 1; i >= 0; --i) {
  437. long bucket;
  438. struct prio_set *p = ca->disk_buckets;
  439. struct bucket_disk *d = p->data;
  440. struct bucket_disk *end = d + prios_per_bucket(ca);
  441. for (b = ca->buckets + i * prios_per_bucket(ca);
  442. b < ca->buckets + ca->sb.nbuckets && d < end;
  443. b++, d++) {
  444. d->prio = cpu_to_le16(b->prio);
  445. d->gen = b->gen;
  446. }
  447. p->next_bucket = ca->prio_buckets[i + 1];
  448. p->magic = pset_magic(&ca->sb);
  449. p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
  450. bucket = bch_bucket_alloc(ca, RESERVE_PRIO, wait);
  451. BUG_ON(bucket == -1);
  452. mutex_unlock(&ca->set->bucket_lock);
  453. prio_io(ca, bucket, REQ_OP_WRITE, 0);
  454. mutex_lock(&ca->set->bucket_lock);
  455. ca->prio_buckets[i] = bucket;
  456. atomic_dec_bug(&ca->buckets[bucket].pin);
  457. }
  458. mutex_unlock(&ca->set->bucket_lock);
  459. bch_journal_meta(ca->set, &cl);
  460. closure_sync(&cl);
  461. mutex_lock(&ca->set->bucket_lock);
  462. /*
  463. * Don't want the old priorities to get garbage collected until after we
  464. * finish writing the new ones, and they're journalled
  465. */
  466. for (i = 0; i < prio_buckets(ca); i++) {
  467. if (ca->prio_last_buckets[i])
  468. __bch_bucket_free(ca,
  469. &ca->buckets[ca->prio_last_buckets[i]]);
  470. ca->prio_last_buckets[i] = ca->prio_buckets[i];
  471. }
  472. return 0;
  473. }
  474. static void prio_read(struct cache *ca, uint64_t bucket)
  475. {
  476. struct prio_set *p = ca->disk_buckets;
  477. struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
  478. struct bucket *b;
  479. unsigned int bucket_nr = 0;
  480. for (b = ca->buckets;
  481. b < ca->buckets + ca->sb.nbuckets;
  482. b++, d++) {
  483. if (d == end) {
  484. ca->prio_buckets[bucket_nr] = bucket;
  485. ca->prio_last_buckets[bucket_nr] = bucket;
  486. bucket_nr++;
  487. prio_io(ca, bucket, REQ_OP_READ, 0);
  488. if (p->csum !=
  489. bch_crc64(&p->magic, bucket_bytes(ca) - 8))
  490. pr_warn("bad csum reading priorities");
  491. if (p->magic != pset_magic(&ca->sb))
  492. pr_warn("bad magic reading priorities");
  493. bucket = p->next_bucket;
  494. d = p->data;
  495. }
  496. b->prio = le16_to_cpu(d->prio);
  497. b->gen = b->last_gc = d->gen;
  498. }
  499. }
  500. /* Bcache device */
  501. static int open_dev(struct block_device *b, fmode_t mode)
  502. {
  503. struct bcache_device *d = b->bd_disk->private_data;
  504. if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
  505. return -ENXIO;
  506. closure_get(&d->cl);
  507. return 0;
  508. }
  509. static void release_dev(struct gendisk *b, fmode_t mode)
  510. {
  511. struct bcache_device *d = b->private_data;
  512. closure_put(&d->cl);
  513. }
  514. static int ioctl_dev(struct block_device *b, fmode_t mode,
  515. unsigned int cmd, unsigned long arg)
  516. {
  517. struct bcache_device *d = b->bd_disk->private_data;
  518. return d->ioctl(d, mode, cmd, arg);
  519. }
  520. static const struct block_device_operations bcache_ops = {
  521. .open = open_dev,
  522. .release = release_dev,
  523. .ioctl = ioctl_dev,
  524. .owner = THIS_MODULE,
  525. };
  526. void bcache_device_stop(struct bcache_device *d)
  527. {
  528. if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
  529. /*
  530. * closure_fn set to
  531. * - cached device: cached_dev_flush()
  532. * - flash dev: flash_dev_flush()
  533. */
  534. closure_queue(&d->cl);
  535. }
  536. static void bcache_device_unlink(struct bcache_device *d)
  537. {
  538. lockdep_assert_held(&bch_register_lock);
  539. if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
  540. unsigned int i;
  541. struct cache *ca;
  542. sysfs_remove_link(&d->c->kobj, d->name);
  543. sysfs_remove_link(&d->kobj, "cache");
  544. for_each_cache(ca, d->c, i)
  545. bd_unlink_disk_holder(ca->bdev, d->disk);
  546. }
  547. }
  548. static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
  549. const char *name)
  550. {
  551. unsigned int i;
  552. struct cache *ca;
  553. int ret;
  554. for_each_cache(ca, d->c, i)
  555. bd_link_disk_holder(ca->bdev, d->disk);
  556. snprintf(d->name, BCACHEDEVNAME_SIZE,
  557. "%s%u", name, d->id);
  558. ret = sysfs_create_link(&d->kobj, &c->kobj, "cache");
  559. if (ret < 0)
  560. pr_err("Couldn't create device -> cache set symlink");
  561. ret = sysfs_create_link(&c->kobj, &d->kobj, d->name);
  562. if (ret < 0)
  563. pr_err("Couldn't create cache set -> device symlink");
  564. clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags);
  565. }
  566. static void bcache_device_detach(struct bcache_device *d)
  567. {
  568. lockdep_assert_held(&bch_register_lock);
  569. atomic_dec(&d->c->attached_dev_nr);
  570. if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
  571. struct uuid_entry *u = d->c->uuids + d->id;
  572. SET_UUID_FLASH_ONLY(u, 0);
  573. memcpy(u->uuid, invalid_uuid, 16);
  574. u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
  575. bch_uuid_write(d->c);
  576. }
  577. bcache_device_unlink(d);
  578. d->c->devices[d->id] = NULL;
  579. closure_put(&d->c->caching);
  580. d->c = NULL;
  581. }
  582. static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
  583. unsigned int id)
  584. {
  585. d->id = id;
  586. d->c = c;
  587. c->devices[id] = d;
  588. if (id >= c->devices_max_used)
  589. c->devices_max_used = id + 1;
  590. closure_get(&c->caching);
  591. }
  592. static inline int first_minor_to_idx(int first_minor)
  593. {
  594. return (first_minor/BCACHE_MINORS);
  595. }
  596. static inline int idx_to_first_minor(int idx)
  597. {
  598. return (idx * BCACHE_MINORS);
  599. }
  600. static void bcache_device_free(struct bcache_device *d)
  601. {
  602. struct gendisk *disk = d->disk;
  603. lockdep_assert_held(&bch_register_lock);
  604. if (disk)
  605. pr_info("%s stopped", disk->disk_name);
  606. else
  607. pr_err("bcache device (NULL gendisk) stopped");
  608. if (d->c)
  609. bcache_device_detach(d);
  610. if (disk) {
  611. bool disk_added = (disk->flags & GENHD_FL_UP) != 0;
  612. if (disk_added)
  613. del_gendisk(disk);
  614. if (disk->queue)
  615. blk_cleanup_queue(disk->queue);
  616. ida_simple_remove(&bcache_device_idx,
  617. first_minor_to_idx(disk->first_minor));
  618. if (disk_added)
  619. put_disk(disk);
  620. }
  621. bioset_exit(&d->bio_split);
  622. kvfree(d->full_dirty_stripes);
  623. kvfree(d->stripe_sectors_dirty);
  624. closure_debug_destroy(&d->cl);
  625. }
  626. static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
  627. sector_t sectors)
  628. {
  629. struct request_queue *q;
  630. const size_t max_stripes = min_t(size_t, INT_MAX,
  631. SIZE_MAX / sizeof(atomic_t));
  632. uint64_t n;
  633. int idx;
  634. if (!d->stripe_size)
  635. d->stripe_size = 1 << 31;
  636. n = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
  637. if (!n || n > max_stripes) {
  638. pr_err("nr_stripes too large or invalid: %llu (start sector beyond end of disk?)\n",
  639. n);
  640. return -ENOMEM;
  641. }
  642. d->nr_stripes = n;
  643. n = d->nr_stripes * sizeof(atomic_t);
  644. d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL);
  645. if (!d->stripe_sectors_dirty)
  646. return -ENOMEM;
  647. n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
  648. d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL);
  649. if (!d->full_dirty_stripes)
  650. goto out_free_stripe_sectors_dirty;
  651. idx = ida_simple_get(&bcache_device_idx, 0,
  652. BCACHE_DEVICE_IDX_MAX, GFP_KERNEL);
  653. if (idx < 0)
  654. goto out_free_full_dirty_stripes;
  655. if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio),
  656. BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER))
  657. goto out_ida_remove;
  658. d->disk = alloc_disk(BCACHE_MINORS);
  659. if (!d->disk)
  660. goto out_bioset_exit;
  661. set_capacity(d->disk, sectors);
  662. snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx);
  663. d->disk->major = bcache_major;
  664. d->disk->first_minor = idx_to_first_minor(idx);
  665. d->disk->fops = &bcache_ops;
  666. d->disk->private_data = d;
  667. q = blk_alloc_queue(GFP_KERNEL);
  668. if (!q)
  669. return -ENOMEM;
  670. blk_queue_make_request(q, NULL);
  671. d->disk->queue = q;
  672. q->queuedata = d;
  673. q->backing_dev_info->congested_data = d;
  674. q->limits.max_hw_sectors = UINT_MAX;
  675. q->limits.max_sectors = UINT_MAX;
  676. q->limits.max_segment_size = UINT_MAX;
  677. q->limits.max_segments = BIO_MAX_PAGES;
  678. blk_queue_max_discard_sectors(q, UINT_MAX);
  679. q->limits.discard_granularity = 512;
  680. q->limits.io_min = block_size;
  681. q->limits.logical_block_size = block_size;
  682. q->limits.physical_block_size = block_size;
  683. blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue);
  684. blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue);
  685. blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue);
  686. blk_queue_write_cache(q, true, true);
  687. return 0;
  688. out_bioset_exit:
  689. bioset_exit(&d->bio_split);
  690. out_ida_remove:
  691. ida_simple_remove(&bcache_device_idx, idx);
  692. out_free_full_dirty_stripes:
  693. kvfree(d->full_dirty_stripes);
  694. out_free_stripe_sectors_dirty:
  695. kvfree(d->stripe_sectors_dirty);
  696. return -ENOMEM;
  697. }
  698. /* Cached device */
  699. static void calc_cached_dev_sectors(struct cache_set *c)
  700. {
  701. uint64_t sectors = 0;
  702. struct cached_dev *dc;
  703. list_for_each_entry(dc, &c->cached_devs, list)
  704. sectors += bdev_sectors(dc->bdev);
  705. c->cached_dev_sectors = sectors;
  706. }
  707. #define BACKING_DEV_OFFLINE_TIMEOUT 5
  708. static int cached_dev_status_update(void *arg)
  709. {
  710. struct cached_dev *dc = arg;
  711. struct request_queue *q;
  712. /*
  713. * If this delayed worker is stopping outside, directly quit here.
  714. * dc->io_disable might be set via sysfs interface, so check it
  715. * here too.
  716. */
  717. while (!kthread_should_stop() && !dc->io_disable) {
  718. q = bdev_get_queue(dc->bdev);
  719. if (blk_queue_dying(q))
  720. dc->offline_seconds++;
  721. else
  722. dc->offline_seconds = 0;
  723. if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) {
  724. pr_err("%s: device offline for %d seconds",
  725. dc->backing_dev_name,
  726. BACKING_DEV_OFFLINE_TIMEOUT);
  727. pr_err("%s: disable I/O request due to backing "
  728. "device offline", dc->disk.name);
  729. dc->io_disable = true;
  730. /* let others know earlier that io_disable is true */
  731. smp_mb();
  732. bcache_device_stop(&dc->disk);
  733. break;
  734. }
  735. schedule_timeout_interruptible(HZ);
  736. }
  737. wait_for_kthread_stop();
  738. return 0;
  739. }
  740. int bch_cached_dev_run(struct cached_dev *dc)
  741. {
  742. struct bcache_device *d = &dc->disk;
  743. char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL);
  744. char *env[] = {
  745. "DRIVER=bcache",
  746. kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
  747. kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""),
  748. NULL,
  749. };
  750. if (dc->io_disable) {
  751. pr_err("I/O disabled on cached dev %s",
  752. dc->backing_dev_name);
  753. kfree(env[1]);
  754. kfree(env[2]);
  755. kfree(buf);
  756. return -EIO;
  757. }
  758. if (atomic_xchg(&dc->running, 1)) {
  759. kfree(env[1]);
  760. kfree(env[2]);
  761. kfree(buf);
  762. pr_info("cached dev %s is running already",
  763. dc->backing_dev_name);
  764. return -EBUSY;
  765. }
  766. if (!d->c &&
  767. BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
  768. struct closure cl;
  769. closure_init_stack(&cl);
  770. SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
  771. bch_write_bdev_super(dc, &cl);
  772. closure_sync(&cl);
  773. }
  774. add_disk(d->disk);
  775. bd_link_disk_holder(dc->bdev, dc->disk.disk);
  776. /*
  777. * won't show up in the uevent file, use udevadm monitor -e instead
  778. * only class / kset properties are persistent
  779. */
  780. kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
  781. kfree(env[1]);
  782. kfree(env[2]);
  783. kfree(buf);
  784. if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
  785. sysfs_create_link(&disk_to_dev(d->disk)->kobj,
  786. &d->kobj, "bcache")) {
  787. pr_err("Couldn't create bcache dev <-> disk sysfs symlinks");
  788. return -ENOMEM;
  789. }
  790. dc->status_update_thread = kthread_run(cached_dev_status_update,
  791. dc, "bcache_status_update");
  792. if (IS_ERR(dc->status_update_thread)) {
  793. pr_warn("failed to create bcache_status_update kthread, "
  794. "continue to run without monitoring backing "
  795. "device status");
  796. }
  797. return 0;
  798. }
  799. /*
  800. * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed
  801. * work dc->writeback_rate_update is running. Wait until the routine
  802. * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to
  803. * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out
  804. * seconds, give up waiting here and continue to cancel it too.
  805. */
  806. static void cancel_writeback_rate_update_dwork(struct cached_dev *dc)
  807. {
  808. int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ;
  809. do {
  810. if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING,
  811. &dc->disk.flags))
  812. break;
  813. time_out--;
  814. schedule_timeout_interruptible(1);
  815. } while (time_out > 0);
  816. if (time_out == 0)
  817. pr_warn("give up waiting for dc->writeback_write_update to quit");
  818. cancel_delayed_work_sync(&dc->writeback_rate_update);
  819. }
  820. static void cached_dev_detach_finish(struct work_struct *w)
  821. {
  822. struct cached_dev *dc = container_of(w, struct cached_dev, detach);
  823. struct closure cl;
  824. closure_init_stack(&cl);
  825. BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
  826. BUG_ON(refcount_read(&dc->count));
  827. if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
  828. cancel_writeback_rate_update_dwork(dc);
  829. if (!IS_ERR_OR_NULL(dc->writeback_thread)) {
  830. kthread_stop(dc->writeback_thread);
  831. dc->writeback_thread = NULL;
  832. }
  833. memset(&dc->sb.set_uuid, 0, 16);
  834. SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
  835. bch_write_bdev_super(dc, &cl);
  836. closure_sync(&cl);
  837. mutex_lock(&bch_register_lock);
  838. calc_cached_dev_sectors(dc->disk.c);
  839. bcache_device_detach(&dc->disk);
  840. list_move(&dc->list, &uncached_devices);
  841. clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
  842. clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags);
  843. mutex_unlock(&bch_register_lock);
  844. pr_info("Caching disabled for %s", dc->backing_dev_name);
  845. /* Drop ref we took in cached_dev_detach() */
  846. closure_put(&dc->disk.cl);
  847. }
  848. void bch_cached_dev_detach(struct cached_dev *dc)
  849. {
  850. lockdep_assert_held(&bch_register_lock);
  851. if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
  852. return;
  853. if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
  854. return;
  855. /*
  856. * Block the device from being closed and freed until we're finished
  857. * detaching
  858. */
  859. closure_get(&dc->disk.cl);
  860. bch_writeback_queue(dc);
  861. cached_dev_put(dc);
  862. }
  863. int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
  864. uint8_t *set_uuid)
  865. {
  866. uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds());
  867. struct uuid_entry *u;
  868. struct cached_dev *exist_dc, *t;
  869. int ret = 0;
  870. if ((set_uuid && memcmp(set_uuid, c->sb.set_uuid, 16)) ||
  871. (!set_uuid && memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)))
  872. return -ENOENT;
  873. if (dc->disk.c) {
  874. pr_err("Can't attach %s: already attached",
  875. dc->backing_dev_name);
  876. return -EINVAL;
  877. }
  878. if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
  879. pr_err("Can't attach %s: shutting down",
  880. dc->backing_dev_name);
  881. return -EINVAL;
  882. }
  883. if (dc->sb.block_size < c->sb.block_size) {
  884. /* Will die */
  885. pr_err("Couldn't attach %s: block size less than set's block size",
  886. dc->backing_dev_name);
  887. return -EINVAL;
  888. }
  889. /* Check whether already attached */
  890. list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) {
  891. if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) {
  892. pr_err("Tried to attach %s but duplicate UUID already attached",
  893. dc->backing_dev_name);
  894. return -EINVAL;
  895. }
  896. }
  897. u = uuid_find(c, dc->sb.uuid);
  898. if (u &&
  899. (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
  900. BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
  901. memcpy(u->uuid, invalid_uuid, 16);
  902. u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
  903. u = NULL;
  904. }
  905. if (!u) {
  906. if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
  907. pr_err("Couldn't find uuid for %s in set",
  908. dc->backing_dev_name);
  909. return -ENOENT;
  910. }
  911. u = uuid_find_empty(c);
  912. if (!u) {
  913. pr_err("Not caching %s, no room for UUID",
  914. dc->backing_dev_name);
  915. return -EINVAL;
  916. }
  917. }
  918. /*
  919. * Deadlocks since we're called via sysfs...
  920. * sysfs_remove_file(&dc->kobj, &sysfs_attach);
  921. */
  922. if (bch_is_zero(u->uuid, 16)) {
  923. struct closure cl;
  924. closure_init_stack(&cl);
  925. memcpy(u->uuid, dc->sb.uuid, 16);
  926. memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
  927. u->first_reg = u->last_reg = rtime;
  928. bch_uuid_write(c);
  929. memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
  930. SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
  931. bch_write_bdev_super(dc, &cl);
  932. closure_sync(&cl);
  933. } else {
  934. u->last_reg = rtime;
  935. bch_uuid_write(c);
  936. }
  937. bcache_device_attach(&dc->disk, c, u - c->uuids);
  938. list_move(&dc->list, &c->cached_devs);
  939. calc_cached_dev_sectors(c);
  940. /*
  941. * dc->c must be set before dc->count != 0 - paired with the mb in
  942. * cached_dev_get()
  943. */
  944. smp_wmb();
  945. refcount_set(&dc->count, 1);
  946. /* Block writeback thread, but spawn it */
  947. down_write(&dc->writeback_lock);
  948. if (bch_cached_dev_writeback_start(dc)) {
  949. up_write(&dc->writeback_lock);
  950. pr_err("Couldn't start writeback facilities for %s",
  951. dc->disk.disk->disk_name);
  952. return -ENOMEM;
  953. }
  954. if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
  955. atomic_set(&dc->has_dirty, 1);
  956. bch_writeback_queue(dc);
  957. }
  958. bch_sectors_dirty_init(&dc->disk);
  959. ret = bch_cached_dev_run(dc);
  960. if (ret && (ret != -EBUSY)) {
  961. up_write(&dc->writeback_lock);
  962. /*
  963. * bch_register_lock is held, bcache_device_stop() is not
  964. * able to be directly called. The kthread and kworker
  965. * created previously in bch_cached_dev_writeback_start()
  966. * have to be stopped manually here.
  967. */
  968. kthread_stop(dc->writeback_thread);
  969. cancel_writeback_rate_update_dwork(dc);
  970. pr_err("Couldn't run cached device %s",
  971. dc->backing_dev_name);
  972. return ret;
  973. }
  974. bcache_device_link(&dc->disk, c, "bdev");
  975. atomic_inc(&c->attached_dev_nr);
  976. /* Allow the writeback thread to proceed */
  977. up_write(&dc->writeback_lock);
  978. pr_info("Caching %s as %s on set %pU",
  979. dc->backing_dev_name,
  980. dc->disk.disk->disk_name,
  981. dc->disk.c->sb.set_uuid);
  982. return 0;
  983. }
  984. /* when dc->disk.kobj released */
  985. void bch_cached_dev_release(struct kobject *kobj)
  986. {
  987. struct cached_dev *dc = container_of(kobj, struct cached_dev,
  988. disk.kobj);
  989. kfree(dc);
  990. module_put(THIS_MODULE);
  991. }
  992. static void cached_dev_free(struct closure *cl)
  993. {
  994. struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
  995. if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
  996. cancel_writeback_rate_update_dwork(dc);
  997. if (!IS_ERR_OR_NULL(dc->writeback_thread))
  998. kthread_stop(dc->writeback_thread);
  999. if (!IS_ERR_OR_NULL(dc->status_update_thread))
  1000. kthread_stop(dc->status_update_thread);
  1001. mutex_lock(&bch_register_lock);
  1002. if (atomic_read(&dc->running))
  1003. bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
  1004. bcache_device_free(&dc->disk);
  1005. list_del(&dc->list);
  1006. mutex_unlock(&bch_register_lock);
  1007. if (dc->sb_bio.bi_inline_vecs[0].bv_page)
  1008. put_page(bio_first_page_all(&dc->sb_bio));
  1009. if (!IS_ERR_OR_NULL(dc->bdev))
  1010. blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1011. wake_up(&unregister_wait);
  1012. kobject_put(&dc->disk.kobj);
  1013. }
  1014. static void cached_dev_flush(struct closure *cl)
  1015. {
  1016. struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
  1017. struct bcache_device *d = &dc->disk;
  1018. mutex_lock(&bch_register_lock);
  1019. bcache_device_unlink(d);
  1020. mutex_unlock(&bch_register_lock);
  1021. bch_cache_accounting_destroy(&dc->accounting);
  1022. kobject_del(&d->kobj);
  1023. continue_at(cl, cached_dev_free, system_wq);
  1024. }
  1025. static int cached_dev_init(struct cached_dev *dc, unsigned int block_size)
  1026. {
  1027. int ret;
  1028. struct io *io;
  1029. struct request_queue *q = bdev_get_queue(dc->bdev);
  1030. __module_get(THIS_MODULE);
  1031. INIT_LIST_HEAD(&dc->list);
  1032. closure_init(&dc->disk.cl, NULL);
  1033. set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
  1034. kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
  1035. INIT_WORK(&dc->detach, cached_dev_detach_finish);
  1036. sema_init(&dc->sb_write_mutex, 1);
  1037. INIT_LIST_HEAD(&dc->io_lru);
  1038. spin_lock_init(&dc->io_lock);
  1039. bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
  1040. dc->sequential_cutoff = 4 << 20;
  1041. for (io = dc->io; io < dc->io + RECENT_IO; io++) {
  1042. list_add(&io->lru, &dc->io_lru);
  1043. hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
  1044. }
  1045. dc->disk.stripe_size = q->limits.io_opt >> 9;
  1046. if (dc->disk.stripe_size)
  1047. dc->partial_stripes_expensive =
  1048. q->limits.raid_partial_stripes_expensive;
  1049. ret = bcache_device_init(&dc->disk, block_size,
  1050. dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
  1051. if (ret)
  1052. return ret;
  1053. dc->disk.disk->queue->backing_dev_info->ra_pages =
  1054. max(dc->disk.disk->queue->backing_dev_info->ra_pages,
  1055. q->backing_dev_info->ra_pages);
  1056. atomic_set(&dc->io_errors, 0);
  1057. dc->io_disable = false;
  1058. dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT;
  1059. /* default to auto */
  1060. dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO;
  1061. bch_cached_dev_request_init(dc);
  1062. bch_cached_dev_writeback_init(dc);
  1063. return 0;
  1064. }
  1065. /* Cached device - bcache superblock */
  1066. static int register_bdev(struct cache_sb *sb, struct page *sb_page,
  1067. struct block_device *bdev,
  1068. struct cached_dev *dc)
  1069. {
  1070. const char *err = "cannot allocate memory";
  1071. struct cache_set *c;
  1072. int ret = -ENOMEM;
  1073. bdevname(bdev, dc->backing_dev_name);
  1074. memcpy(&dc->sb, sb, sizeof(struct cache_sb));
  1075. dc->bdev = bdev;
  1076. dc->bdev->bd_holder = dc;
  1077. bio_init(&dc->sb_bio, dc->sb_bio.bi_inline_vecs, 1);
  1078. bio_first_bvec_all(&dc->sb_bio)->bv_page = sb_page;
  1079. get_page(sb_page);
  1080. if (cached_dev_init(dc, sb->block_size << 9))
  1081. goto err;
  1082. err = "error creating kobject";
  1083. if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
  1084. "bcache"))
  1085. goto err;
  1086. if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
  1087. goto err;
  1088. pr_info("registered backing device %s", dc->backing_dev_name);
  1089. list_add(&dc->list, &uncached_devices);
  1090. /* attach to a matched cache set if it exists */
  1091. list_for_each_entry(c, &bch_cache_sets, list)
  1092. bch_cached_dev_attach(dc, c, NULL);
  1093. if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
  1094. BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) {
  1095. err = "failed to run cached device";
  1096. ret = bch_cached_dev_run(dc);
  1097. if (ret)
  1098. goto err;
  1099. }
  1100. return 0;
  1101. err:
  1102. pr_notice("error %s: %s", dc->backing_dev_name, err);
  1103. bcache_device_stop(&dc->disk);
  1104. return ret;
  1105. }
  1106. /* Flash only volumes */
  1107. /* When d->kobj released */
  1108. void bch_flash_dev_release(struct kobject *kobj)
  1109. {
  1110. struct bcache_device *d = container_of(kobj, struct bcache_device,
  1111. kobj);
  1112. kfree(d);
  1113. }
  1114. static void flash_dev_free(struct closure *cl)
  1115. {
  1116. struct bcache_device *d = container_of(cl, struct bcache_device, cl);
  1117. mutex_lock(&bch_register_lock);
  1118. atomic_long_sub(bcache_dev_sectors_dirty(d),
  1119. &d->c->flash_dev_dirty_sectors);
  1120. bcache_device_free(d);
  1121. mutex_unlock(&bch_register_lock);
  1122. kobject_put(&d->kobj);
  1123. }
  1124. static void flash_dev_flush(struct closure *cl)
  1125. {
  1126. struct bcache_device *d = container_of(cl, struct bcache_device, cl);
  1127. mutex_lock(&bch_register_lock);
  1128. bcache_device_unlink(d);
  1129. mutex_unlock(&bch_register_lock);
  1130. kobject_del(&d->kobj);
  1131. continue_at(cl, flash_dev_free, system_wq);
  1132. }
  1133. static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
  1134. {
  1135. struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
  1136. GFP_KERNEL);
  1137. if (!d)
  1138. return -ENOMEM;
  1139. closure_init(&d->cl, NULL);
  1140. set_closure_fn(&d->cl, flash_dev_flush, system_wq);
  1141. kobject_init(&d->kobj, &bch_flash_dev_ktype);
  1142. if (bcache_device_init(d, block_bytes(c), u->sectors))
  1143. goto err;
  1144. bcache_device_attach(d, c, u - c->uuids);
  1145. bch_sectors_dirty_init(d);
  1146. bch_flash_dev_request_init(d);
  1147. add_disk(d->disk);
  1148. if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
  1149. goto err;
  1150. bcache_device_link(d, c, "volume");
  1151. return 0;
  1152. err:
  1153. kobject_put(&d->kobj);
  1154. return -ENOMEM;
  1155. }
  1156. static int flash_devs_run(struct cache_set *c)
  1157. {
  1158. int ret = 0;
  1159. struct uuid_entry *u;
  1160. for (u = c->uuids;
  1161. u < c->uuids + c->nr_uuids && !ret;
  1162. u++)
  1163. if (UUID_FLASH_ONLY(u))
  1164. ret = flash_dev_run(c, u);
  1165. return ret;
  1166. }
  1167. int bch_flash_dev_create(struct cache_set *c, uint64_t size)
  1168. {
  1169. struct uuid_entry *u;
  1170. if (test_bit(CACHE_SET_STOPPING, &c->flags))
  1171. return -EINTR;
  1172. if (!test_bit(CACHE_SET_RUNNING, &c->flags))
  1173. return -EPERM;
  1174. u = uuid_find_empty(c);
  1175. if (!u) {
  1176. pr_err("Can't create volume, no room for UUID");
  1177. return -EINVAL;
  1178. }
  1179. get_random_bytes(u->uuid, 16);
  1180. memset(u->label, 0, 32);
  1181. u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
  1182. SET_UUID_FLASH_ONLY(u, 1);
  1183. u->sectors = size >> 9;
  1184. bch_uuid_write(c);
  1185. return flash_dev_run(c, u);
  1186. }
  1187. bool bch_cached_dev_error(struct cached_dev *dc)
  1188. {
  1189. if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
  1190. return false;
  1191. dc->io_disable = true;
  1192. /* make others know io_disable is true earlier */
  1193. smp_mb();
  1194. pr_err("stop %s: too many IO errors on backing device %s\n",
  1195. dc->disk.disk->disk_name, dc->backing_dev_name);
  1196. bcache_device_stop(&dc->disk);
  1197. return true;
  1198. }
  1199. /* Cache set */
  1200. __printf(2, 3)
  1201. bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
  1202. {
  1203. va_list args;
  1204. if (c->on_error != ON_ERROR_PANIC &&
  1205. test_bit(CACHE_SET_STOPPING, &c->flags))
  1206. return false;
  1207. if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags))
  1208. pr_info("CACHE_SET_IO_DISABLE already set");
  1209. /*
  1210. * XXX: we can be called from atomic context
  1211. * acquire_console_sem();
  1212. */
  1213. pr_err("bcache: error on %pU: ", c->sb.set_uuid);
  1214. va_start(args, fmt);
  1215. vprintk(fmt, args);
  1216. va_end(args);
  1217. pr_err(", disabling caching\n");
  1218. if (c->on_error == ON_ERROR_PANIC)
  1219. panic("panic forced after error\n");
  1220. bch_cache_set_unregister(c);
  1221. return true;
  1222. }
  1223. /* When c->kobj released */
  1224. void bch_cache_set_release(struct kobject *kobj)
  1225. {
  1226. struct cache_set *c = container_of(kobj, struct cache_set, kobj);
  1227. kfree(c);
  1228. module_put(THIS_MODULE);
  1229. }
  1230. static void cache_set_free(struct closure *cl)
  1231. {
  1232. struct cache_set *c = container_of(cl, struct cache_set, cl);
  1233. struct cache *ca;
  1234. unsigned int i;
  1235. debugfs_remove(c->debug);
  1236. bch_open_buckets_free(c);
  1237. bch_btree_cache_free(c);
  1238. bch_journal_free(c);
  1239. mutex_lock(&bch_register_lock);
  1240. for_each_cache(ca, c, i)
  1241. if (ca) {
  1242. ca->set = NULL;
  1243. c->cache[ca->sb.nr_this_dev] = NULL;
  1244. kobject_put(&ca->kobj);
  1245. }
  1246. bch_bset_sort_state_free(&c->sort);
  1247. free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
  1248. if (c->moving_gc_wq)
  1249. destroy_workqueue(c->moving_gc_wq);
  1250. bioset_exit(&c->bio_split);
  1251. mempool_exit(&c->fill_iter);
  1252. mempool_exit(&c->bio_meta);
  1253. mempool_exit(&c->search);
  1254. kfree(c->devices);
  1255. list_del(&c->list);
  1256. mutex_unlock(&bch_register_lock);
  1257. pr_info("Cache set %pU unregistered", c->sb.set_uuid);
  1258. wake_up(&unregister_wait);
  1259. closure_debug_destroy(&c->cl);
  1260. kobject_put(&c->kobj);
  1261. }
  1262. static void cache_set_flush(struct closure *cl)
  1263. {
  1264. struct cache_set *c = container_of(cl, struct cache_set, caching);
  1265. struct cache *ca;
  1266. struct btree *b;
  1267. unsigned int i;
  1268. bch_cache_accounting_destroy(&c->accounting);
  1269. kobject_put(&c->internal);
  1270. kobject_del(&c->kobj);
  1271. if (!IS_ERR_OR_NULL(c->gc_thread))
  1272. kthread_stop(c->gc_thread);
  1273. if (!IS_ERR_OR_NULL(c->root))
  1274. list_add(&c->root->list, &c->btree_cache);
  1275. /*
  1276. * Avoid flushing cached nodes if cache set is retiring
  1277. * due to too many I/O errors detected.
  1278. */
  1279. if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags))
  1280. list_for_each_entry(b, &c->btree_cache, list) {
  1281. mutex_lock(&b->write_lock);
  1282. if (btree_node_dirty(b))
  1283. __bch_btree_node_write(b, NULL);
  1284. mutex_unlock(&b->write_lock);
  1285. }
  1286. for_each_cache(ca, c, i)
  1287. if (ca->alloc_thread)
  1288. kthread_stop(ca->alloc_thread);
  1289. if (c->journal.cur) {
  1290. cancel_delayed_work_sync(&c->journal.work);
  1291. /* flush last journal entry if needed */
  1292. c->journal.work.work.func(&c->journal.work.work);
  1293. }
  1294. closure_return(cl);
  1295. }
  1296. /*
  1297. * This function is only called when CACHE_SET_IO_DISABLE is set, which means
  1298. * cache set is unregistering due to too many I/O errors. In this condition,
  1299. * the bcache device might be stopped, it depends on stop_when_cache_set_failed
  1300. * value and whether the broken cache has dirty data:
  1301. *
  1302. * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device
  1303. * BCH_CACHED_STOP_AUTO 0 NO
  1304. * BCH_CACHED_STOP_AUTO 1 YES
  1305. * BCH_CACHED_DEV_STOP_ALWAYS 0 YES
  1306. * BCH_CACHED_DEV_STOP_ALWAYS 1 YES
  1307. *
  1308. * The expected behavior is, if stop_when_cache_set_failed is configured to
  1309. * "auto" via sysfs interface, the bcache device will not be stopped if the
  1310. * backing device is clean on the broken cache device.
  1311. */
  1312. static void conditional_stop_bcache_device(struct cache_set *c,
  1313. struct bcache_device *d,
  1314. struct cached_dev *dc)
  1315. {
  1316. if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) {
  1317. pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.",
  1318. d->disk->disk_name, c->sb.set_uuid);
  1319. bcache_device_stop(d);
  1320. } else if (atomic_read(&dc->has_dirty)) {
  1321. /*
  1322. * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
  1323. * and dc->has_dirty == 1
  1324. */
  1325. pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.",
  1326. d->disk->disk_name);
  1327. /*
  1328. * There might be a small time gap that cache set is
  1329. * released but bcache device is not. Inside this time
  1330. * gap, regular I/O requests will directly go into
  1331. * backing device as no cache set attached to. This
  1332. * behavior may also introduce potential inconsistence
  1333. * data in writeback mode while cache is dirty.
  1334. * Therefore before calling bcache_device_stop() due
  1335. * to a broken cache device, dc->io_disable should be
  1336. * explicitly set to true.
  1337. */
  1338. dc->io_disable = true;
  1339. /* make others know io_disable is true earlier */
  1340. smp_mb();
  1341. bcache_device_stop(d);
  1342. } else {
  1343. /*
  1344. * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
  1345. * and dc->has_dirty == 0
  1346. */
  1347. pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.",
  1348. d->disk->disk_name);
  1349. }
  1350. }
  1351. static void __cache_set_unregister(struct closure *cl)
  1352. {
  1353. struct cache_set *c = container_of(cl, struct cache_set, caching);
  1354. struct cached_dev *dc;
  1355. struct bcache_device *d;
  1356. size_t i;
  1357. mutex_lock(&bch_register_lock);
  1358. for (i = 0; i < c->devices_max_used; i++) {
  1359. d = c->devices[i];
  1360. if (!d)
  1361. continue;
  1362. if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
  1363. test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
  1364. dc = container_of(d, struct cached_dev, disk);
  1365. bch_cached_dev_detach(dc);
  1366. if (test_bit(CACHE_SET_IO_DISABLE, &c->flags))
  1367. conditional_stop_bcache_device(c, d, dc);
  1368. } else {
  1369. bcache_device_stop(d);
  1370. }
  1371. }
  1372. mutex_unlock(&bch_register_lock);
  1373. continue_at(cl, cache_set_flush, system_wq);
  1374. }
  1375. void bch_cache_set_stop(struct cache_set *c)
  1376. {
  1377. if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
  1378. /* closure_fn set to __cache_set_unregister() */
  1379. closure_queue(&c->caching);
  1380. }
  1381. void bch_cache_set_unregister(struct cache_set *c)
  1382. {
  1383. set_bit(CACHE_SET_UNREGISTERING, &c->flags);
  1384. bch_cache_set_stop(c);
  1385. }
  1386. #define alloc_bucket_pages(gfp, c) \
  1387. ((void *) __get_free_pages(__GFP_ZERO|__GFP_COMP|gfp, ilog2(bucket_pages(c))))
  1388. struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
  1389. {
  1390. int iter_size;
  1391. struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
  1392. if (!c)
  1393. return NULL;
  1394. __module_get(THIS_MODULE);
  1395. closure_init(&c->cl, NULL);
  1396. set_closure_fn(&c->cl, cache_set_free, system_wq);
  1397. closure_init(&c->caching, &c->cl);
  1398. set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
  1399. /* Maybe create continue_at_noreturn() and use it here? */
  1400. closure_set_stopped(&c->cl);
  1401. closure_put(&c->cl);
  1402. kobject_init(&c->kobj, &bch_cache_set_ktype);
  1403. kobject_init(&c->internal, &bch_cache_set_internal_ktype);
  1404. bch_cache_accounting_init(&c->accounting, &c->cl);
  1405. memcpy(c->sb.set_uuid, sb->set_uuid, 16);
  1406. c->sb.block_size = sb->block_size;
  1407. c->sb.bucket_size = sb->bucket_size;
  1408. c->sb.nr_in_set = sb->nr_in_set;
  1409. c->sb.last_mount = sb->last_mount;
  1410. c->bucket_bits = ilog2(sb->bucket_size);
  1411. c->block_bits = ilog2(sb->block_size);
  1412. c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
  1413. c->devices_max_used = 0;
  1414. atomic_set(&c->attached_dev_nr, 0);
  1415. c->btree_pages = bucket_pages(c);
  1416. if (c->btree_pages > BTREE_MAX_PAGES)
  1417. c->btree_pages = max_t(int, c->btree_pages / 4,
  1418. BTREE_MAX_PAGES);
  1419. sema_init(&c->sb_write_mutex, 1);
  1420. mutex_init(&c->bucket_lock);
  1421. init_waitqueue_head(&c->btree_cache_wait);
  1422. spin_lock_init(&c->btree_cannibalize_lock);
  1423. init_waitqueue_head(&c->bucket_wait);
  1424. init_waitqueue_head(&c->gc_wait);
  1425. sema_init(&c->uuid_write_mutex, 1);
  1426. spin_lock_init(&c->btree_gc_time.lock);
  1427. spin_lock_init(&c->btree_split_time.lock);
  1428. spin_lock_init(&c->btree_read_time.lock);
  1429. bch_moving_init_cache_set(c);
  1430. INIT_LIST_HEAD(&c->list);
  1431. INIT_LIST_HEAD(&c->cached_devs);
  1432. INIT_LIST_HEAD(&c->btree_cache);
  1433. INIT_LIST_HEAD(&c->btree_cache_freeable);
  1434. INIT_LIST_HEAD(&c->btree_cache_freed);
  1435. INIT_LIST_HEAD(&c->data_buckets);
  1436. iter_size = (sb->bucket_size / sb->block_size + 1) *
  1437. sizeof(struct btree_iter_set);
  1438. if (!(c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL)) ||
  1439. mempool_init_slab_pool(&c->search, 32, bch_search_cache) ||
  1440. mempool_init_kmalloc_pool(&c->bio_meta, 2,
  1441. sizeof(struct bbio) + sizeof(struct bio_vec) *
  1442. bucket_pages(c)) ||
  1443. mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
  1444. bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio),
  1445. BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER) ||
  1446. !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
  1447. !(c->moving_gc_wq = alloc_workqueue("bcache_gc",
  1448. WQ_MEM_RECLAIM, 0)) ||
  1449. bch_journal_alloc(c) ||
  1450. bch_btree_cache_alloc(c) ||
  1451. bch_open_buckets_alloc(c) ||
  1452. bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages)))
  1453. goto err;
  1454. c->congested_read_threshold_us = 2000;
  1455. c->congested_write_threshold_us = 20000;
  1456. c->error_limit = DEFAULT_IO_ERROR_LIMIT;
  1457. WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags));
  1458. return c;
  1459. err:
  1460. bch_cache_set_unregister(c);
  1461. return NULL;
  1462. }
  1463. static int run_cache_set(struct cache_set *c)
  1464. {
  1465. const char *err = "cannot allocate memory";
  1466. struct cached_dev *dc, *t;
  1467. struct cache *ca;
  1468. struct closure cl;
  1469. unsigned int i;
  1470. LIST_HEAD(journal);
  1471. struct journal_replay *l;
  1472. closure_init_stack(&cl);
  1473. for_each_cache(ca, c, i)
  1474. c->nbuckets += ca->sb.nbuckets;
  1475. set_gc_sectors(c);
  1476. if (CACHE_SYNC(&c->sb)) {
  1477. struct bkey *k;
  1478. struct jset *j;
  1479. err = "cannot allocate memory for journal";
  1480. if (bch_journal_read(c, &journal))
  1481. goto err;
  1482. pr_debug("btree_journal_read() done");
  1483. err = "no journal entries found";
  1484. if (list_empty(&journal))
  1485. goto err;
  1486. j = &list_entry(journal.prev, struct journal_replay, list)->j;
  1487. err = "IO error reading priorities";
  1488. for_each_cache(ca, c, i)
  1489. prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
  1490. /*
  1491. * If prio_read() fails it'll call cache_set_error and we'll
  1492. * tear everything down right away, but if we perhaps checked
  1493. * sooner we could avoid journal replay.
  1494. */
  1495. k = &j->btree_root;
  1496. err = "bad btree root";
  1497. if (__bch_btree_ptr_invalid(c, k))
  1498. goto err;
  1499. err = "error reading btree root";
  1500. c->root = bch_btree_node_get(c, NULL, k,
  1501. j->btree_level,
  1502. true, NULL);
  1503. if (IS_ERR_OR_NULL(c->root))
  1504. goto err;
  1505. list_del_init(&c->root->list);
  1506. rw_unlock(true, c->root);
  1507. err = uuid_read(c, j, &cl);
  1508. if (err)
  1509. goto err;
  1510. err = "error in recovery";
  1511. if (bch_btree_check(c))
  1512. goto err;
  1513. /*
  1514. * bch_btree_check() may occupy too much system memory which
  1515. * has negative effects to user space application (e.g. data
  1516. * base) performance. Shrink the mca cache memory proactively
  1517. * here to avoid competing memory with user space workloads..
  1518. */
  1519. if (!c->shrinker_disabled) {
  1520. struct shrink_control sc;
  1521. sc.gfp_mask = GFP_KERNEL;
  1522. sc.nr_to_scan = c->btree_cache_used * c->btree_pages;
  1523. /* first run to clear b->accessed tag */
  1524. c->shrink.scan_objects(&c->shrink, &sc);
  1525. /* second run to reap non-accessed nodes */
  1526. c->shrink.scan_objects(&c->shrink, &sc);
  1527. }
  1528. bch_journal_mark(c, &journal);
  1529. bch_initial_gc_finish(c);
  1530. pr_debug("btree_check() done");
  1531. /*
  1532. * bcache_journal_next() can't happen sooner, or
  1533. * btree_gc_finish() will give spurious errors about last_gc >
  1534. * gc_gen - this is a hack but oh well.
  1535. */
  1536. bch_journal_next(&c->journal);
  1537. err = "error starting allocator thread";
  1538. for_each_cache(ca, c, i)
  1539. if (bch_cache_allocator_start(ca))
  1540. goto err;
  1541. /*
  1542. * First place it's safe to allocate: btree_check() and
  1543. * btree_gc_finish() have to run before we have buckets to
  1544. * allocate, and bch_bucket_alloc_set() might cause a journal
  1545. * entry to be written so bcache_journal_next() has to be called
  1546. * first.
  1547. *
  1548. * If the uuids were in the old format we have to rewrite them
  1549. * before the next journal entry is written:
  1550. */
  1551. if (j->version < BCACHE_JSET_VERSION_UUID)
  1552. __uuid_write(c);
  1553. err = "bcache: replay journal failed";
  1554. if (bch_journal_replay(c, &journal))
  1555. goto err;
  1556. } else {
  1557. pr_notice("invalidating existing data");
  1558. for_each_cache(ca, c, i) {
  1559. unsigned int j;
  1560. ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
  1561. 2, SB_JOURNAL_BUCKETS);
  1562. for (j = 0; j < ca->sb.keys; j++)
  1563. ca->sb.d[j] = ca->sb.first_bucket + j;
  1564. }
  1565. bch_initial_gc_finish(c);
  1566. err = "error starting allocator thread";
  1567. for_each_cache(ca, c, i)
  1568. if (bch_cache_allocator_start(ca))
  1569. goto err;
  1570. mutex_lock(&c->bucket_lock);
  1571. for_each_cache(ca, c, i)
  1572. bch_prio_write(ca, true);
  1573. mutex_unlock(&c->bucket_lock);
  1574. err = "cannot allocate new UUID bucket";
  1575. if (__uuid_write(c))
  1576. goto err;
  1577. err = "cannot allocate new btree root";
  1578. c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL);
  1579. if (IS_ERR_OR_NULL(c->root))
  1580. goto err;
  1581. mutex_lock(&c->root->write_lock);
  1582. bkey_copy_key(&c->root->key, &MAX_KEY);
  1583. bch_btree_node_write(c->root, &cl);
  1584. mutex_unlock(&c->root->write_lock);
  1585. bch_btree_set_root(c->root);
  1586. rw_unlock(true, c->root);
  1587. /*
  1588. * We don't want to write the first journal entry until
  1589. * everything is set up - fortunately journal entries won't be
  1590. * written until the SET_CACHE_SYNC() here:
  1591. */
  1592. SET_CACHE_SYNC(&c->sb, true);
  1593. bch_journal_next(&c->journal);
  1594. bch_journal_meta(c, &cl);
  1595. }
  1596. err = "error starting gc thread";
  1597. if (bch_gc_thread_start(c))
  1598. goto err;
  1599. closure_sync(&cl);
  1600. c->sb.last_mount = (u32)ktime_get_real_seconds();
  1601. bcache_write_super(c);
  1602. list_for_each_entry_safe(dc, t, &uncached_devices, list)
  1603. bch_cached_dev_attach(dc, c, NULL);
  1604. flash_devs_run(c);
  1605. set_bit(CACHE_SET_RUNNING, &c->flags);
  1606. return 0;
  1607. err:
  1608. while (!list_empty(&journal)) {
  1609. l = list_first_entry(&journal, struct journal_replay, list);
  1610. list_del(&l->list);
  1611. kfree(l);
  1612. }
  1613. closure_sync(&cl);
  1614. bch_cache_set_error(c, "%s", err);
  1615. return -EIO;
  1616. }
  1617. static bool can_attach_cache(struct cache *ca, struct cache_set *c)
  1618. {
  1619. return ca->sb.block_size == c->sb.block_size &&
  1620. ca->sb.bucket_size == c->sb.bucket_size &&
  1621. ca->sb.nr_in_set == c->sb.nr_in_set;
  1622. }
  1623. static const char *register_cache_set(struct cache *ca)
  1624. {
  1625. char buf[12];
  1626. const char *err = "cannot allocate memory";
  1627. struct cache_set *c;
  1628. list_for_each_entry(c, &bch_cache_sets, list)
  1629. if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
  1630. if (c->cache[ca->sb.nr_this_dev])
  1631. return "duplicate cache set member";
  1632. if (!can_attach_cache(ca, c))
  1633. return "cache sb does not match set";
  1634. if (!CACHE_SYNC(&ca->sb))
  1635. SET_CACHE_SYNC(&c->sb, false);
  1636. goto found;
  1637. }
  1638. c = bch_cache_set_alloc(&ca->sb);
  1639. if (!c)
  1640. return err;
  1641. err = "error creating kobject";
  1642. if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
  1643. kobject_add(&c->internal, &c->kobj, "internal"))
  1644. goto err;
  1645. if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
  1646. goto err;
  1647. bch_debug_init_cache_set(c);
  1648. list_add(&c->list, &bch_cache_sets);
  1649. found:
  1650. sprintf(buf, "cache%i", ca->sb.nr_this_dev);
  1651. if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
  1652. sysfs_create_link(&c->kobj, &ca->kobj, buf))
  1653. goto err;
  1654. /*
  1655. * A special case is both ca->sb.seq and c->sb.seq are 0,
  1656. * such condition happens on a new created cache device whose
  1657. * super block is never flushed yet. In this case c->sb.version
  1658. * and other members should be updated too, otherwise we will
  1659. * have a mistaken super block version in cache set.
  1660. */
  1661. if (ca->sb.seq > c->sb.seq || c->sb.seq == 0) {
  1662. c->sb.version = ca->sb.version;
  1663. memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
  1664. c->sb.flags = ca->sb.flags;
  1665. c->sb.seq = ca->sb.seq;
  1666. pr_debug("set version = %llu", c->sb.version);
  1667. }
  1668. kobject_get(&ca->kobj);
  1669. ca->set = c;
  1670. ca->set->cache[ca->sb.nr_this_dev] = ca;
  1671. c->cache_by_alloc[c->caches_loaded++] = ca;
  1672. if (c->caches_loaded == c->sb.nr_in_set) {
  1673. err = "failed to run cache set";
  1674. if (run_cache_set(c) < 0)
  1675. goto err;
  1676. }
  1677. return NULL;
  1678. err:
  1679. bch_cache_set_unregister(c);
  1680. return err;
  1681. }
  1682. /* Cache device */
  1683. /* When ca->kobj released */
  1684. void bch_cache_release(struct kobject *kobj)
  1685. {
  1686. struct cache *ca = container_of(kobj, struct cache, kobj);
  1687. unsigned int i;
  1688. if (ca->set) {
  1689. BUG_ON(ca->set->cache[ca->sb.nr_this_dev] != ca);
  1690. ca->set->cache[ca->sb.nr_this_dev] = NULL;
  1691. }
  1692. free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
  1693. kfree(ca->prio_buckets);
  1694. vfree(ca->buckets);
  1695. free_heap(&ca->heap);
  1696. free_fifo(&ca->free_inc);
  1697. for (i = 0; i < RESERVE_NR; i++)
  1698. free_fifo(&ca->free[i]);
  1699. if (ca->sb_bio.bi_inline_vecs[0].bv_page)
  1700. put_page(bio_first_page_all(&ca->sb_bio));
  1701. if (!IS_ERR_OR_NULL(ca->bdev))
  1702. blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1703. kfree(ca);
  1704. module_put(THIS_MODULE);
  1705. }
  1706. static int cache_alloc(struct cache *ca)
  1707. {
  1708. size_t free;
  1709. size_t btree_buckets;
  1710. struct bucket *b;
  1711. int ret = -ENOMEM;
  1712. const char *err = NULL;
  1713. __module_get(THIS_MODULE);
  1714. kobject_init(&ca->kobj, &bch_cache_ktype);
  1715. bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8);
  1716. /*
  1717. * when ca->sb.njournal_buckets is not zero, journal exists,
  1718. * and in bch_journal_replay(), tree node may split,
  1719. * so bucket of RESERVE_BTREE type is needed,
  1720. * the worst situation is all journal buckets are valid journal,
  1721. * and all the keys need to replay,
  1722. * so the number of RESERVE_BTREE type buckets should be as much
  1723. * as journal buckets
  1724. */
  1725. btree_buckets = ca->sb.njournal_buckets ?: 8;
  1726. free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
  1727. if (!free) {
  1728. ret = -EPERM;
  1729. err = "ca->sb.nbuckets is too small";
  1730. goto err_free;
  1731. }
  1732. if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets,
  1733. GFP_KERNEL)) {
  1734. err = "ca->free[RESERVE_BTREE] alloc failed";
  1735. goto err_btree_alloc;
  1736. }
  1737. if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca),
  1738. GFP_KERNEL)) {
  1739. err = "ca->free[RESERVE_PRIO] alloc failed";
  1740. goto err_prio_alloc;
  1741. }
  1742. if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) {
  1743. err = "ca->free[RESERVE_MOVINGGC] alloc failed";
  1744. goto err_movinggc_alloc;
  1745. }
  1746. if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) {
  1747. err = "ca->free[RESERVE_NONE] alloc failed";
  1748. goto err_none_alloc;
  1749. }
  1750. if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) {
  1751. err = "ca->free_inc alloc failed";
  1752. goto err_free_inc_alloc;
  1753. }
  1754. if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) {
  1755. err = "ca->heap alloc failed";
  1756. goto err_heap_alloc;
  1757. }
  1758. ca->buckets = vzalloc(array_size(sizeof(struct bucket),
  1759. ca->sb.nbuckets));
  1760. if (!ca->buckets) {
  1761. err = "ca->buckets alloc failed";
  1762. goto err_buckets_alloc;
  1763. }
  1764. ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t),
  1765. prio_buckets(ca), 2),
  1766. GFP_KERNEL);
  1767. if (!ca->prio_buckets) {
  1768. err = "ca->prio_buckets alloc failed";
  1769. goto err_prio_buckets_alloc;
  1770. }
  1771. ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca);
  1772. if (!ca->disk_buckets) {
  1773. err = "ca->disk_buckets alloc failed";
  1774. goto err_disk_buckets_alloc;
  1775. }
  1776. ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
  1777. for_each_bucket(b, ca)
  1778. atomic_set(&b->pin, 0);
  1779. return 0;
  1780. err_disk_buckets_alloc:
  1781. kfree(ca->prio_buckets);
  1782. err_prio_buckets_alloc:
  1783. vfree(ca->buckets);
  1784. err_buckets_alloc:
  1785. free_heap(&ca->heap);
  1786. err_heap_alloc:
  1787. free_fifo(&ca->free_inc);
  1788. err_free_inc_alloc:
  1789. free_fifo(&ca->free[RESERVE_NONE]);
  1790. err_none_alloc:
  1791. free_fifo(&ca->free[RESERVE_MOVINGGC]);
  1792. err_movinggc_alloc:
  1793. free_fifo(&ca->free[RESERVE_PRIO]);
  1794. err_prio_alloc:
  1795. free_fifo(&ca->free[RESERVE_BTREE]);
  1796. err_btree_alloc:
  1797. err_free:
  1798. module_put(THIS_MODULE);
  1799. if (err)
  1800. pr_notice("error %s: %s", ca->cache_dev_name, err);
  1801. return ret;
  1802. }
  1803. static int register_cache(struct cache_sb *sb, struct page *sb_page,
  1804. struct block_device *bdev, struct cache *ca)
  1805. {
  1806. const char *err = NULL; /* must be set for any error case */
  1807. int ret = 0;
  1808. bdevname(bdev, ca->cache_dev_name);
  1809. memcpy(&ca->sb, sb, sizeof(struct cache_sb));
  1810. ca->bdev = bdev;
  1811. ca->bdev->bd_holder = ca;
  1812. bio_init(&ca->sb_bio, ca->sb_bio.bi_inline_vecs, 1);
  1813. bio_first_bvec_all(&ca->sb_bio)->bv_page = sb_page;
  1814. get_page(sb_page);
  1815. if (blk_queue_discard(bdev_get_queue(bdev)))
  1816. ca->discard = CACHE_DISCARD(&ca->sb);
  1817. ret = cache_alloc(ca);
  1818. if (ret != 0) {
  1819. /*
  1820. * If we failed here, it means ca->kobj is not initialized yet,
  1821. * kobject_put() won't be called and there is no chance to
  1822. * call blkdev_put() to bdev in bch_cache_release(). So we
  1823. * explicitly call blkdev_put() here.
  1824. */
  1825. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1826. if (ret == -ENOMEM)
  1827. err = "cache_alloc(): -ENOMEM";
  1828. else if (ret == -EPERM)
  1829. err = "cache_alloc(): cache device is too small";
  1830. else
  1831. err = "cache_alloc(): unknown error";
  1832. goto err;
  1833. }
  1834. if (kobject_add(&ca->kobj,
  1835. &part_to_dev(bdev->bd_part)->kobj,
  1836. "bcache")) {
  1837. err = "error calling kobject_add";
  1838. ret = -ENOMEM;
  1839. goto out;
  1840. }
  1841. mutex_lock(&bch_register_lock);
  1842. err = register_cache_set(ca);
  1843. mutex_unlock(&bch_register_lock);
  1844. if (err) {
  1845. ret = -ENODEV;
  1846. goto out;
  1847. }
  1848. pr_info("registered cache device %s", ca->cache_dev_name);
  1849. out:
  1850. kobject_put(&ca->kobj);
  1851. err:
  1852. if (err)
  1853. pr_notice("error %s: %s", ca->cache_dev_name, err);
  1854. return ret;
  1855. }
  1856. /* Global interfaces/init */
  1857. static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
  1858. const char *buffer, size_t size);
  1859. static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
  1860. struct kobj_attribute *attr,
  1861. const char *buffer, size_t size);
  1862. kobj_attribute_write(register, register_bcache);
  1863. kobj_attribute_write(register_quiet, register_bcache);
  1864. kobj_attribute_write(pendings_cleanup, bch_pending_bdevs_cleanup);
  1865. static bool bch_is_open_backing(struct block_device *bdev)
  1866. {
  1867. struct cache_set *c, *tc;
  1868. struct cached_dev *dc, *t;
  1869. list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
  1870. list_for_each_entry_safe(dc, t, &c->cached_devs, list)
  1871. if (dc->bdev == bdev)
  1872. return true;
  1873. list_for_each_entry_safe(dc, t, &uncached_devices, list)
  1874. if (dc->bdev == bdev)
  1875. return true;
  1876. return false;
  1877. }
  1878. static bool bch_is_open_cache(struct block_device *bdev)
  1879. {
  1880. struct cache_set *c, *tc;
  1881. struct cache *ca;
  1882. unsigned int i;
  1883. list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
  1884. for_each_cache(ca, c, i)
  1885. if (ca->bdev == bdev)
  1886. return true;
  1887. return false;
  1888. }
  1889. static bool bch_is_open(struct block_device *bdev)
  1890. {
  1891. return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
  1892. }
  1893. static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
  1894. const char *buffer, size_t size)
  1895. {
  1896. const char *err;
  1897. char *path = NULL;
  1898. struct cache_sb *sb;
  1899. struct block_device *bdev = NULL;
  1900. struct page *sb_page;
  1901. ssize_t ret;
  1902. ret = -EBUSY;
  1903. err = "failed to reference bcache module";
  1904. if (!try_module_get(THIS_MODULE))
  1905. goto out;
  1906. /* For latest state of bcache_is_reboot */
  1907. smp_mb();
  1908. err = "bcache is in reboot";
  1909. if (bcache_is_reboot)
  1910. goto out_module_put;
  1911. ret = -ENOMEM;
  1912. err = "cannot allocate memory";
  1913. path = kstrndup(buffer, size, GFP_KERNEL);
  1914. if (!path)
  1915. goto out_module_put;
  1916. sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL);
  1917. if (!sb)
  1918. goto out_free_path;
  1919. ret = -EINVAL;
  1920. err = "failed to open device";
  1921. bdev = blkdev_get_by_path(strim(path),
  1922. FMODE_READ|FMODE_WRITE|FMODE_EXCL,
  1923. sb);
  1924. if (IS_ERR(bdev)) {
  1925. if (bdev == ERR_PTR(-EBUSY)) {
  1926. bdev = lookup_bdev(strim(path));
  1927. mutex_lock(&bch_register_lock);
  1928. if (!IS_ERR(bdev) && bch_is_open(bdev))
  1929. err = "device already registered";
  1930. else
  1931. err = "device busy";
  1932. mutex_unlock(&bch_register_lock);
  1933. if (!IS_ERR(bdev))
  1934. bdput(bdev);
  1935. if (attr == &ksysfs_register_quiet)
  1936. goto done;
  1937. }
  1938. goto out_free_sb;
  1939. }
  1940. err = "failed to set blocksize";
  1941. if (set_blocksize(bdev, 4096))
  1942. goto out_blkdev_put;
  1943. err = read_super(sb, bdev, &sb_page);
  1944. if (err)
  1945. goto out_blkdev_put;
  1946. err = "failed to register device";
  1947. if (SB_IS_BDEV(sb)) {
  1948. struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
  1949. if (!dc)
  1950. goto out_put_sb_page;
  1951. mutex_lock(&bch_register_lock);
  1952. ret = register_bdev(sb, sb_page, bdev, dc);
  1953. mutex_unlock(&bch_register_lock);
  1954. /* blkdev_put() will be called in cached_dev_free() */
  1955. if (ret < 0) {
  1956. bdev = NULL;
  1957. goto out_put_sb_page;
  1958. }
  1959. } else {
  1960. struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  1961. if (!ca)
  1962. goto out_put_sb_page;
  1963. /* blkdev_put() will be called in bch_cache_release() */
  1964. if (register_cache(sb, sb_page, bdev, ca) != 0) {
  1965. bdev = NULL;
  1966. goto out_put_sb_page;
  1967. }
  1968. }
  1969. put_page(sb_page);
  1970. done:
  1971. kfree(sb);
  1972. kfree(path);
  1973. module_put(THIS_MODULE);
  1974. return size;
  1975. out_put_sb_page:
  1976. put_page(sb_page);
  1977. out_blkdev_put:
  1978. if (bdev)
  1979. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1980. out_free_sb:
  1981. kfree(sb);
  1982. out_free_path:
  1983. kfree(path);
  1984. path = NULL;
  1985. out_module_put:
  1986. module_put(THIS_MODULE);
  1987. out:
  1988. pr_info("error %s: %s", path?path:"", err);
  1989. return ret;
  1990. }
  1991. struct pdev {
  1992. struct list_head list;
  1993. struct cached_dev *dc;
  1994. };
  1995. static ssize_t bch_pending_bdevs_cleanup(struct kobject *k,
  1996. struct kobj_attribute *attr,
  1997. const char *buffer,
  1998. size_t size)
  1999. {
  2000. LIST_HEAD(pending_devs);
  2001. ssize_t ret = size;
  2002. struct cached_dev *dc, *tdc;
  2003. struct pdev *pdev, *tpdev;
  2004. struct cache_set *c, *tc;
  2005. mutex_lock(&bch_register_lock);
  2006. list_for_each_entry_safe(dc, tdc, &uncached_devices, list) {
  2007. pdev = kmalloc(sizeof(struct pdev), GFP_KERNEL);
  2008. if (!pdev)
  2009. break;
  2010. pdev->dc = dc;
  2011. list_add(&pdev->list, &pending_devs);
  2012. }
  2013. list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
  2014. list_for_each_entry_safe(c, tc, &bch_cache_sets, list) {
  2015. char *pdev_set_uuid = pdev->dc->sb.set_uuid;
  2016. char *set_uuid = c->sb.uuid;
  2017. if (!memcmp(pdev_set_uuid, set_uuid, 16)) {
  2018. list_del(&pdev->list);
  2019. kfree(pdev);
  2020. break;
  2021. }
  2022. }
  2023. }
  2024. mutex_unlock(&bch_register_lock);
  2025. list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) {
  2026. pr_info("delete pdev %p", pdev);
  2027. list_del(&pdev->list);
  2028. bcache_device_stop(&pdev->dc->disk);
  2029. kfree(pdev);
  2030. }
  2031. return ret;
  2032. }
  2033. static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
  2034. {
  2035. if (bcache_is_reboot)
  2036. return NOTIFY_DONE;
  2037. if (code == SYS_DOWN ||
  2038. code == SYS_HALT ||
  2039. code == SYS_POWER_OFF) {
  2040. DEFINE_WAIT(wait);
  2041. unsigned long start = jiffies;
  2042. bool stopped = false;
  2043. struct cache_set *c, *tc;
  2044. struct cached_dev *dc, *tdc;
  2045. mutex_lock(&bch_register_lock);
  2046. if (bcache_is_reboot)
  2047. goto out;
  2048. /* New registration is rejected since now */
  2049. bcache_is_reboot = true;
  2050. /*
  2051. * Make registering caller (if there is) on other CPU
  2052. * core know bcache_is_reboot set to true earlier
  2053. */
  2054. smp_mb();
  2055. if (list_empty(&bch_cache_sets) &&
  2056. list_empty(&uncached_devices))
  2057. goto out;
  2058. mutex_unlock(&bch_register_lock);
  2059. pr_info("Stopping all devices:");
  2060. /*
  2061. * The reason bch_register_lock is not held to call
  2062. * bch_cache_set_stop() and bcache_device_stop() is to
  2063. * avoid potential deadlock during reboot, because cache
  2064. * set or bcache device stopping process will acqurie
  2065. * bch_register_lock too.
  2066. *
  2067. * We are safe here because bcache_is_reboot sets to
  2068. * true already, register_bcache() will reject new
  2069. * registration now. bcache_is_reboot also makes sure
  2070. * bcache_reboot() won't be re-entered on by other thread,
  2071. * so there is no race in following list iteration by
  2072. * list_for_each_entry_safe().
  2073. */
  2074. list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
  2075. bch_cache_set_stop(c);
  2076. list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
  2077. bcache_device_stop(&dc->disk);
  2078. /*
  2079. * Give an early chance for other kthreads and
  2080. * kworkers to stop themselves
  2081. */
  2082. schedule();
  2083. /* What's a condition variable? */
  2084. while (1) {
  2085. long timeout = start + 10 * HZ - jiffies;
  2086. mutex_lock(&bch_register_lock);
  2087. stopped = list_empty(&bch_cache_sets) &&
  2088. list_empty(&uncached_devices);
  2089. if (timeout < 0 || stopped)
  2090. break;
  2091. prepare_to_wait(&unregister_wait, &wait,
  2092. TASK_UNINTERRUPTIBLE);
  2093. mutex_unlock(&bch_register_lock);
  2094. schedule_timeout(timeout);
  2095. }
  2096. finish_wait(&unregister_wait, &wait);
  2097. if (stopped)
  2098. pr_info("All devices stopped");
  2099. else
  2100. pr_notice("Timeout waiting for devices to be closed");
  2101. out:
  2102. mutex_unlock(&bch_register_lock);
  2103. }
  2104. return NOTIFY_DONE;
  2105. }
  2106. static struct notifier_block reboot = {
  2107. .notifier_call = bcache_reboot,
  2108. .priority = INT_MAX, /* before any real devices */
  2109. };
  2110. static void bcache_exit(void)
  2111. {
  2112. bch_debug_exit();
  2113. bch_request_exit();
  2114. if (bcache_kobj)
  2115. kobject_put(bcache_kobj);
  2116. if (bcache_wq)
  2117. destroy_workqueue(bcache_wq);
  2118. if (bch_journal_wq)
  2119. destroy_workqueue(bch_journal_wq);
  2120. if (bch_flush_wq)
  2121. destroy_workqueue(bch_flush_wq);
  2122. bch_btree_exit();
  2123. if (bcache_major)
  2124. unregister_blkdev(bcache_major, "bcache");
  2125. unregister_reboot_notifier(&reboot);
  2126. mutex_destroy(&bch_register_lock);
  2127. }
  2128. /* Check and fixup module parameters */
  2129. static void check_module_parameters(void)
  2130. {
  2131. if (bch_cutoff_writeback_sync == 0)
  2132. bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC;
  2133. else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) {
  2134. pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u",
  2135. bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX);
  2136. bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX;
  2137. }
  2138. if (bch_cutoff_writeback == 0)
  2139. bch_cutoff_writeback = CUTOFF_WRITEBACK;
  2140. else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) {
  2141. pr_warn("set bch_cutoff_writeback (%u) to max value %u",
  2142. bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX);
  2143. bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX;
  2144. }
  2145. if (bch_cutoff_writeback > bch_cutoff_writeback_sync) {
  2146. pr_warn("set bch_cutoff_writeback (%u) to %u",
  2147. bch_cutoff_writeback, bch_cutoff_writeback_sync);
  2148. bch_cutoff_writeback = bch_cutoff_writeback_sync;
  2149. }
  2150. }
  2151. static int __init bcache_init(void)
  2152. {
  2153. static const struct attribute *files[] = {
  2154. &ksysfs_register.attr,
  2155. &ksysfs_register_quiet.attr,
  2156. &ksysfs_pendings_cleanup.attr,
  2157. NULL
  2158. };
  2159. check_module_parameters();
  2160. mutex_init(&bch_register_lock);
  2161. init_waitqueue_head(&unregister_wait);
  2162. register_reboot_notifier(&reboot);
  2163. bcache_major = register_blkdev(0, "bcache");
  2164. if (bcache_major < 0) {
  2165. unregister_reboot_notifier(&reboot);
  2166. mutex_destroy(&bch_register_lock);
  2167. return bcache_major;
  2168. }
  2169. if (bch_btree_init())
  2170. goto err;
  2171. bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0);
  2172. if (!bcache_wq)
  2173. goto err;
  2174. /*
  2175. * Let's not make this `WQ_MEM_RECLAIM` for the following reasons:
  2176. *
  2177. * 1. It used `system_wq` before which also does no memory reclaim.
  2178. * 2. With `WQ_MEM_RECLAIM` desktop stalls, increased boot times, and
  2179. * reduced throughput can be observed.
  2180. *
  2181. * We still want to user our own queue to not congest the `system_wq`.
  2182. */
  2183. bch_flush_wq = alloc_workqueue("bch_flush", 0, 0);
  2184. if (!bch_flush_wq)
  2185. goto err;
  2186. bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0);
  2187. if (!bch_journal_wq)
  2188. goto err;
  2189. bcache_kobj = kobject_create_and_add("bcache", fs_kobj);
  2190. if (!bcache_kobj)
  2191. goto err;
  2192. if (bch_request_init() ||
  2193. sysfs_create_files(bcache_kobj, files))
  2194. goto err;
  2195. bch_debug_init();
  2196. closure_debug_init();
  2197. bcache_is_reboot = false;
  2198. return 0;
  2199. err:
  2200. bcache_exit();
  2201. return -ENOMEM;
  2202. }
  2203. /*
  2204. * Module hooks
  2205. */
  2206. module_exit(bcache_exit);
  2207. module_init(bcache_init);
  2208. module_param(bch_cutoff_writeback, uint, 0);
  2209. MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback");
  2210. module_param(bch_cutoff_writeback_sync, uint, 0);
  2211. MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback");
  2212. MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
  2213. MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
  2214. MODULE_LICENSE("GPL");