super.c 52 KB

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