super.c 48 KB

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