super.c 47 KB

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