super.c 49 KB

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