super.c 56 KB

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