super.c 45 KB

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