super.c 52 KB

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