super.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  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->full_dirty_stripes))
  572. vfree(d->full_dirty_stripes);
  573. else
  574. kfree(d->full_dirty_stripes);
  575. if (is_vmalloc_addr(d->stripe_sectors_dirty))
  576. vfree(d->stripe_sectors_dirty);
  577. else
  578. kfree(d->stripe_sectors_dirty);
  579. closure_debug_destroy(&d->cl);
  580. }
  581. static int bcache_device_init(struct bcache_device *d, unsigned block_size,
  582. sector_t sectors)
  583. {
  584. struct request_queue *q;
  585. size_t n;
  586. if (!d->stripe_size)
  587. d->stripe_size = 1 << 31;
  588. d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
  589. if (!d->nr_stripes ||
  590. d->nr_stripes > INT_MAX ||
  591. d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
  592. pr_err("nr_stripes too large");
  593. return -ENOMEM;
  594. }
  595. n = d->nr_stripes * sizeof(atomic_t);
  596. d->stripe_sectors_dirty = n < PAGE_SIZE << 6
  597. ? kzalloc(n, GFP_KERNEL)
  598. : vzalloc(n);
  599. if (!d->stripe_sectors_dirty)
  600. return -ENOMEM;
  601. n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
  602. d->full_dirty_stripes = n < PAGE_SIZE << 6
  603. ? kzalloc(n, GFP_KERNEL)
  604. : vzalloc(n);
  605. if (!d->full_dirty_stripes)
  606. return -ENOMEM;
  607. if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
  608. !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
  609. sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
  610. bio_split_pool_init(&d->bio_split_hook) ||
  611. !(d->disk = alloc_disk(1)) ||
  612. !(q = blk_alloc_queue(GFP_KERNEL)))
  613. return -ENOMEM;
  614. set_capacity(d->disk, sectors);
  615. snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
  616. d->disk->major = bcache_major;
  617. d->disk->first_minor = bcache_minor++;
  618. d->disk->fops = &bcache_ops;
  619. d->disk->private_data = d;
  620. blk_queue_make_request(q, NULL);
  621. d->disk->queue = q;
  622. q->queuedata = d;
  623. q->backing_dev_info.congested_data = d;
  624. q->limits.max_hw_sectors = UINT_MAX;
  625. q->limits.max_sectors = UINT_MAX;
  626. q->limits.max_segment_size = UINT_MAX;
  627. q->limits.max_segments = BIO_MAX_PAGES;
  628. q->limits.max_discard_sectors = UINT_MAX;
  629. q->limits.io_min = block_size;
  630. q->limits.logical_block_size = block_size;
  631. q->limits.physical_block_size = block_size;
  632. set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
  633. set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
  634. blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
  635. return 0;
  636. }
  637. /* Cached device */
  638. static void calc_cached_dev_sectors(struct cache_set *c)
  639. {
  640. uint64_t sectors = 0;
  641. struct cached_dev *dc;
  642. list_for_each_entry(dc, &c->cached_devs, list)
  643. sectors += bdev_sectors(dc->bdev);
  644. c->cached_dev_sectors = sectors;
  645. }
  646. void bch_cached_dev_run(struct cached_dev *dc)
  647. {
  648. struct bcache_device *d = &dc->disk;
  649. char buf[SB_LABEL_SIZE + 1];
  650. char *env[] = {
  651. "DRIVER=bcache",
  652. kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
  653. NULL,
  654. NULL,
  655. };
  656. memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
  657. buf[SB_LABEL_SIZE] = '\0';
  658. env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
  659. if (atomic_xchg(&dc->running, 1))
  660. return;
  661. if (!d->c &&
  662. BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
  663. struct closure cl;
  664. closure_init_stack(&cl);
  665. SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
  666. bch_write_bdev_super(dc, &cl);
  667. closure_sync(&cl);
  668. }
  669. add_disk(d->disk);
  670. bd_link_disk_holder(dc->bdev, dc->disk.disk);
  671. /* won't show up in the uevent file, use udevadm monitor -e instead
  672. * only class / kset properties are persistent */
  673. kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
  674. kfree(env[1]);
  675. kfree(env[2]);
  676. if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
  677. sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
  678. pr_debug("error creating sysfs link");
  679. }
  680. static void cached_dev_detach_finish(struct work_struct *w)
  681. {
  682. struct cached_dev *dc = container_of(w, struct cached_dev, detach);
  683. char buf[BDEVNAME_SIZE];
  684. struct closure cl;
  685. closure_init_stack(&cl);
  686. BUG_ON(!atomic_read(&dc->disk.detaching));
  687. BUG_ON(atomic_read(&dc->count));
  688. mutex_lock(&bch_register_lock);
  689. memset(&dc->sb.set_uuid, 0, 16);
  690. SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
  691. bch_write_bdev_super(dc, &cl);
  692. closure_sync(&cl);
  693. bcache_device_detach(&dc->disk);
  694. list_move(&dc->list, &uncached_devices);
  695. mutex_unlock(&bch_register_lock);
  696. pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
  697. /* Drop ref we took in cached_dev_detach() */
  698. closure_put(&dc->disk.cl);
  699. }
  700. void bch_cached_dev_detach(struct cached_dev *dc)
  701. {
  702. lockdep_assert_held(&bch_register_lock);
  703. if (atomic_read(&dc->disk.closing))
  704. return;
  705. if (atomic_xchg(&dc->disk.detaching, 1))
  706. return;
  707. /*
  708. * Block the device from being closed and freed until we're finished
  709. * detaching
  710. */
  711. closure_get(&dc->disk.cl);
  712. bch_writeback_queue(dc);
  713. cached_dev_put(dc);
  714. }
  715. int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
  716. {
  717. uint32_t rtime = cpu_to_le32(get_seconds());
  718. struct uuid_entry *u;
  719. char buf[BDEVNAME_SIZE];
  720. bdevname(dc->bdev, buf);
  721. if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
  722. return -ENOENT;
  723. if (dc->disk.c) {
  724. pr_err("Can't attach %s: already attached", buf);
  725. return -EINVAL;
  726. }
  727. if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
  728. pr_err("Can't attach %s: shutting down", buf);
  729. return -EINVAL;
  730. }
  731. if (dc->sb.block_size < c->sb.block_size) {
  732. /* Will die */
  733. pr_err("Couldn't attach %s: block size less than set's block size",
  734. buf);
  735. return -EINVAL;
  736. }
  737. u = uuid_find(c, dc->sb.uuid);
  738. if (u &&
  739. (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
  740. BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
  741. memcpy(u->uuid, invalid_uuid, 16);
  742. u->invalidated = cpu_to_le32(get_seconds());
  743. u = NULL;
  744. }
  745. if (!u) {
  746. if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
  747. pr_err("Couldn't find uuid for %s in set", buf);
  748. return -ENOENT;
  749. }
  750. u = uuid_find_empty(c);
  751. if (!u) {
  752. pr_err("Not caching %s, no room for UUID", buf);
  753. return -EINVAL;
  754. }
  755. }
  756. /* Deadlocks since we're called via sysfs...
  757. sysfs_remove_file(&dc->kobj, &sysfs_attach);
  758. */
  759. if (bch_is_zero(u->uuid, 16)) {
  760. struct closure cl;
  761. closure_init_stack(&cl);
  762. memcpy(u->uuid, dc->sb.uuid, 16);
  763. memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
  764. u->first_reg = u->last_reg = rtime;
  765. bch_uuid_write(c);
  766. memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
  767. SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
  768. bch_write_bdev_super(dc, &cl);
  769. closure_sync(&cl);
  770. } else {
  771. u->last_reg = rtime;
  772. bch_uuid_write(c);
  773. }
  774. bcache_device_attach(&dc->disk, c, u - c->uuids);
  775. list_move(&dc->list, &c->cached_devs);
  776. calc_cached_dev_sectors(c);
  777. smp_wmb();
  778. /*
  779. * dc->c must be set before dc->count != 0 - paired with the mb in
  780. * cached_dev_get()
  781. */
  782. atomic_set(&dc->count, 1);
  783. if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
  784. bch_sectors_dirty_init(dc);
  785. atomic_set(&dc->has_dirty, 1);
  786. atomic_inc(&dc->count);
  787. bch_writeback_queue(dc);
  788. }
  789. bch_cached_dev_run(dc);
  790. bcache_device_link(&dc->disk, c, "bdev");
  791. pr_info("Caching %s as %s on set %pU",
  792. bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
  793. dc->disk.c->sb.set_uuid);
  794. return 0;
  795. }
  796. void bch_cached_dev_release(struct kobject *kobj)
  797. {
  798. struct cached_dev *dc = container_of(kobj, struct cached_dev,
  799. disk.kobj);
  800. kfree(dc);
  801. module_put(THIS_MODULE);
  802. }
  803. static void cached_dev_free(struct closure *cl)
  804. {
  805. struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
  806. cancel_delayed_work_sync(&dc->writeback_rate_update);
  807. kthread_stop(dc->writeback_thread);
  808. mutex_lock(&bch_register_lock);
  809. if (atomic_read(&dc->running))
  810. bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
  811. bcache_device_free(&dc->disk);
  812. list_del(&dc->list);
  813. mutex_unlock(&bch_register_lock);
  814. if (!IS_ERR_OR_NULL(dc->bdev)) {
  815. if (dc->bdev->bd_disk)
  816. blk_sync_queue(bdev_get_queue(dc->bdev));
  817. blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  818. }
  819. wake_up(&unregister_wait);
  820. kobject_put(&dc->disk.kobj);
  821. }
  822. static void cached_dev_flush(struct closure *cl)
  823. {
  824. struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
  825. struct bcache_device *d = &dc->disk;
  826. mutex_lock(&bch_register_lock);
  827. d->flush_done = 1;
  828. if (d->c)
  829. bcache_device_unlink(d);
  830. mutex_unlock(&bch_register_lock);
  831. bch_cache_accounting_destroy(&dc->accounting);
  832. kobject_del(&d->kobj);
  833. continue_at(cl, cached_dev_free, system_wq);
  834. }
  835. static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
  836. {
  837. int ret;
  838. struct io *io;
  839. struct request_queue *q = bdev_get_queue(dc->bdev);
  840. __module_get(THIS_MODULE);
  841. INIT_LIST_HEAD(&dc->list);
  842. closure_init(&dc->disk.cl, NULL);
  843. set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
  844. kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
  845. INIT_WORK(&dc->detach, cached_dev_detach_finish);
  846. closure_init_unlocked(&dc->sb_write);
  847. INIT_LIST_HEAD(&dc->io_lru);
  848. spin_lock_init(&dc->io_lock);
  849. bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
  850. dc->sequential_cutoff = 4 << 20;
  851. for (io = dc->io; io < dc->io + RECENT_IO; io++) {
  852. list_add(&io->lru, &dc->io_lru);
  853. hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
  854. }
  855. ret = bcache_device_init(&dc->disk, block_size,
  856. dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
  857. if (ret)
  858. return ret;
  859. set_capacity(dc->disk.disk,
  860. dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
  861. dc->disk.disk->queue->backing_dev_info.ra_pages =
  862. max(dc->disk.disk->queue->backing_dev_info.ra_pages,
  863. q->backing_dev_info.ra_pages);
  864. bch_cached_dev_request_init(dc);
  865. bch_cached_dev_writeback_init(dc);
  866. return 0;
  867. }
  868. /* Cached device - bcache superblock */
  869. static void register_bdev(struct cache_sb *sb, struct page *sb_page,
  870. struct block_device *bdev,
  871. struct cached_dev *dc)
  872. {
  873. char name[BDEVNAME_SIZE];
  874. const char *err = "cannot allocate memory";
  875. struct cache_set *c;
  876. memcpy(&dc->sb, sb, sizeof(struct cache_sb));
  877. dc->bdev = bdev;
  878. dc->bdev->bd_holder = dc;
  879. bio_init(&dc->sb_bio);
  880. dc->sb_bio.bi_max_vecs = 1;
  881. dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
  882. dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
  883. get_page(sb_page);
  884. if (cached_dev_init(dc, sb->block_size << 9))
  885. goto err;
  886. err = "error creating kobject";
  887. if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
  888. "bcache"))
  889. goto err;
  890. if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
  891. goto err;
  892. pr_info("registered backing device %s", bdevname(bdev, name));
  893. list_add(&dc->list, &uncached_devices);
  894. list_for_each_entry(c, &bch_cache_sets, list)
  895. bch_cached_dev_attach(dc, c);
  896. if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
  897. BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
  898. bch_cached_dev_run(dc);
  899. return;
  900. err:
  901. pr_notice("error opening %s: %s", bdevname(bdev, name), err);
  902. bcache_device_stop(&dc->disk);
  903. }
  904. /* Flash only volumes */
  905. void bch_flash_dev_release(struct kobject *kobj)
  906. {
  907. struct bcache_device *d = container_of(kobj, struct bcache_device,
  908. kobj);
  909. kfree(d);
  910. }
  911. static void flash_dev_free(struct closure *cl)
  912. {
  913. struct bcache_device *d = container_of(cl, struct bcache_device, cl);
  914. bcache_device_free(d);
  915. kobject_put(&d->kobj);
  916. }
  917. static void flash_dev_flush(struct closure *cl)
  918. {
  919. struct bcache_device *d = container_of(cl, struct bcache_device, cl);
  920. bcache_device_unlink(d);
  921. kobject_del(&d->kobj);
  922. continue_at(cl, flash_dev_free, system_wq);
  923. }
  924. static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
  925. {
  926. struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
  927. GFP_KERNEL);
  928. if (!d)
  929. return -ENOMEM;
  930. closure_init(&d->cl, NULL);
  931. set_closure_fn(&d->cl, flash_dev_flush, system_wq);
  932. kobject_init(&d->kobj, &bch_flash_dev_ktype);
  933. if (bcache_device_init(d, block_bytes(c), u->sectors))
  934. goto err;
  935. bcache_device_attach(d, c, u - c->uuids);
  936. bch_flash_dev_request_init(d);
  937. add_disk(d->disk);
  938. if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
  939. goto err;
  940. bcache_device_link(d, c, "volume");
  941. return 0;
  942. err:
  943. kobject_put(&d->kobj);
  944. return -ENOMEM;
  945. }
  946. static int flash_devs_run(struct cache_set *c)
  947. {
  948. int ret = 0;
  949. struct uuid_entry *u;
  950. for (u = c->uuids;
  951. u < c->uuids + c->nr_uuids && !ret;
  952. u++)
  953. if (UUID_FLASH_ONLY(u))
  954. ret = flash_dev_run(c, u);
  955. return ret;
  956. }
  957. int bch_flash_dev_create(struct cache_set *c, uint64_t size)
  958. {
  959. struct uuid_entry *u;
  960. if (test_bit(CACHE_SET_STOPPING, &c->flags))
  961. return -EINTR;
  962. u = uuid_find_empty(c);
  963. if (!u) {
  964. pr_err("Can't create volume, no room for UUID");
  965. return -EINVAL;
  966. }
  967. get_random_bytes(u->uuid, 16);
  968. memset(u->label, 0, 32);
  969. u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
  970. SET_UUID_FLASH_ONLY(u, 1);
  971. u->sectors = size >> 9;
  972. bch_uuid_write(c);
  973. return flash_dev_run(c, u);
  974. }
  975. /* Cache set */
  976. __printf(2, 3)
  977. bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
  978. {
  979. va_list args;
  980. if (c->on_error != ON_ERROR_PANIC &&
  981. test_bit(CACHE_SET_STOPPING, &c->flags))
  982. return false;
  983. /* XXX: we can be called from atomic context
  984. acquire_console_sem();
  985. */
  986. printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
  987. va_start(args, fmt);
  988. vprintk(fmt, args);
  989. va_end(args);
  990. printk(", disabling caching\n");
  991. if (c->on_error == ON_ERROR_PANIC)
  992. panic("panic forced after error\n");
  993. bch_cache_set_unregister(c);
  994. return true;
  995. }
  996. void bch_cache_set_release(struct kobject *kobj)
  997. {
  998. struct cache_set *c = container_of(kobj, struct cache_set, kobj);
  999. kfree(c);
  1000. module_put(THIS_MODULE);
  1001. }
  1002. static void cache_set_free(struct closure *cl)
  1003. {
  1004. struct cache_set *c = container_of(cl, struct cache_set, cl);
  1005. struct cache *ca;
  1006. unsigned i;
  1007. if (!IS_ERR_OR_NULL(c->debug))
  1008. debugfs_remove(c->debug);
  1009. bch_open_buckets_free(c);
  1010. bch_btree_cache_free(c);
  1011. bch_journal_free(c);
  1012. for_each_cache(ca, c, i)
  1013. if (ca)
  1014. kobject_put(&ca->kobj);
  1015. free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
  1016. free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
  1017. if (c->bio_split)
  1018. bioset_free(c->bio_split);
  1019. if (c->fill_iter)
  1020. mempool_destroy(c->fill_iter);
  1021. if (c->bio_meta)
  1022. mempool_destroy(c->bio_meta);
  1023. if (c->search)
  1024. mempool_destroy(c->search);
  1025. kfree(c->devices);
  1026. mutex_lock(&bch_register_lock);
  1027. list_del(&c->list);
  1028. mutex_unlock(&bch_register_lock);
  1029. pr_info("Cache set %pU unregistered", c->sb.set_uuid);
  1030. wake_up(&unregister_wait);
  1031. closure_debug_destroy(&c->cl);
  1032. kobject_put(&c->kobj);
  1033. }
  1034. static void cache_set_flush(struct closure *cl)
  1035. {
  1036. struct cache_set *c = container_of(cl, struct cache_set, caching);
  1037. struct cache *ca;
  1038. struct btree *b;
  1039. unsigned i;
  1040. bch_cache_accounting_destroy(&c->accounting);
  1041. kobject_put(&c->internal);
  1042. kobject_del(&c->kobj);
  1043. if (c->gc_thread)
  1044. kthread_stop(c->gc_thread);
  1045. if (!IS_ERR_OR_NULL(c->root))
  1046. list_add(&c->root->list, &c->btree_cache);
  1047. /* Should skip this if we're unregistering because of an error */
  1048. list_for_each_entry(b, &c->btree_cache, list)
  1049. if (btree_node_dirty(b))
  1050. bch_btree_node_write(b, NULL);
  1051. for_each_cache(ca, c, i)
  1052. if (ca->alloc_thread)
  1053. kthread_stop(ca->alloc_thread);
  1054. closure_return(cl);
  1055. }
  1056. static void __cache_set_unregister(struct closure *cl)
  1057. {
  1058. struct cache_set *c = container_of(cl, struct cache_set, caching);
  1059. struct cached_dev *dc;
  1060. size_t i;
  1061. mutex_lock(&bch_register_lock);
  1062. for (i = 0; i < c->nr_uuids; i++)
  1063. if (c->devices[i]) {
  1064. if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
  1065. test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
  1066. dc = container_of(c->devices[i],
  1067. struct cached_dev, disk);
  1068. bch_cached_dev_detach(dc);
  1069. } else {
  1070. bcache_device_stop(c->devices[i]);
  1071. }
  1072. }
  1073. mutex_unlock(&bch_register_lock);
  1074. continue_at(cl, cache_set_flush, system_wq);
  1075. }
  1076. void bch_cache_set_stop(struct cache_set *c)
  1077. {
  1078. if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
  1079. closure_queue(&c->caching);
  1080. }
  1081. void bch_cache_set_unregister(struct cache_set *c)
  1082. {
  1083. set_bit(CACHE_SET_UNREGISTERING, &c->flags);
  1084. bch_cache_set_stop(c);
  1085. }
  1086. #define alloc_bucket_pages(gfp, c) \
  1087. ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
  1088. struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
  1089. {
  1090. int iter_size;
  1091. struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
  1092. if (!c)
  1093. return NULL;
  1094. __module_get(THIS_MODULE);
  1095. closure_init(&c->cl, NULL);
  1096. set_closure_fn(&c->cl, cache_set_free, system_wq);
  1097. closure_init(&c->caching, &c->cl);
  1098. set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
  1099. /* Maybe create continue_at_noreturn() and use it here? */
  1100. closure_set_stopped(&c->cl);
  1101. closure_put(&c->cl);
  1102. kobject_init(&c->kobj, &bch_cache_set_ktype);
  1103. kobject_init(&c->internal, &bch_cache_set_internal_ktype);
  1104. bch_cache_accounting_init(&c->accounting, &c->cl);
  1105. memcpy(c->sb.set_uuid, sb->set_uuid, 16);
  1106. c->sb.block_size = sb->block_size;
  1107. c->sb.bucket_size = sb->bucket_size;
  1108. c->sb.nr_in_set = sb->nr_in_set;
  1109. c->sb.last_mount = sb->last_mount;
  1110. c->bucket_bits = ilog2(sb->bucket_size);
  1111. c->block_bits = ilog2(sb->block_size);
  1112. c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
  1113. c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
  1114. if (c->btree_pages > BTREE_MAX_PAGES)
  1115. c->btree_pages = max_t(int, c->btree_pages / 4,
  1116. BTREE_MAX_PAGES);
  1117. c->sort_crit_factor = int_sqrt(c->btree_pages);
  1118. closure_init_unlocked(&c->sb_write);
  1119. mutex_init(&c->bucket_lock);
  1120. init_waitqueue_head(&c->try_wait);
  1121. init_waitqueue_head(&c->bucket_wait);
  1122. closure_init_unlocked(&c->uuid_write);
  1123. mutex_init(&c->sort_lock);
  1124. spin_lock_init(&c->sort_time.lock);
  1125. spin_lock_init(&c->btree_gc_time.lock);
  1126. spin_lock_init(&c->btree_split_time.lock);
  1127. spin_lock_init(&c->btree_read_time.lock);
  1128. spin_lock_init(&c->try_harder_time.lock);
  1129. bch_moving_init_cache_set(c);
  1130. INIT_LIST_HEAD(&c->list);
  1131. INIT_LIST_HEAD(&c->cached_devs);
  1132. INIT_LIST_HEAD(&c->btree_cache);
  1133. INIT_LIST_HEAD(&c->btree_cache_freeable);
  1134. INIT_LIST_HEAD(&c->btree_cache_freed);
  1135. INIT_LIST_HEAD(&c->data_buckets);
  1136. c->search = mempool_create_slab_pool(32, bch_search_cache);
  1137. if (!c->search)
  1138. goto err;
  1139. iter_size = (sb->bucket_size / sb->block_size + 1) *
  1140. sizeof(struct btree_iter_set);
  1141. if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
  1142. !(c->bio_meta = mempool_create_kmalloc_pool(2,
  1143. sizeof(struct bbio) + sizeof(struct bio_vec) *
  1144. bucket_pages(c))) ||
  1145. !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
  1146. !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
  1147. !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
  1148. !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
  1149. bch_journal_alloc(c) ||
  1150. bch_btree_cache_alloc(c) ||
  1151. bch_open_buckets_alloc(c))
  1152. goto err;
  1153. c->congested_read_threshold_us = 2000;
  1154. c->congested_write_threshold_us = 20000;
  1155. c->error_limit = 8 << IO_ERROR_SHIFT;
  1156. return c;
  1157. err:
  1158. bch_cache_set_unregister(c);
  1159. return NULL;
  1160. }
  1161. static void run_cache_set(struct cache_set *c)
  1162. {
  1163. const char *err = "cannot allocate memory";
  1164. struct cached_dev *dc, *t;
  1165. struct cache *ca;
  1166. struct closure cl;
  1167. unsigned i;
  1168. closure_init_stack(&cl);
  1169. for_each_cache(ca, c, i)
  1170. c->nbuckets += ca->sb.nbuckets;
  1171. if (CACHE_SYNC(&c->sb)) {
  1172. LIST_HEAD(journal);
  1173. struct bkey *k;
  1174. struct jset *j;
  1175. err = "cannot allocate memory for journal";
  1176. if (bch_journal_read(c, &journal))
  1177. goto err;
  1178. pr_debug("btree_journal_read() done");
  1179. err = "no journal entries found";
  1180. if (list_empty(&journal))
  1181. goto err;
  1182. j = &list_entry(journal.prev, struct journal_replay, list)->j;
  1183. err = "IO error reading priorities";
  1184. for_each_cache(ca, c, i)
  1185. prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
  1186. /*
  1187. * If prio_read() fails it'll call cache_set_error and we'll
  1188. * tear everything down right away, but if we perhaps checked
  1189. * sooner we could avoid journal replay.
  1190. */
  1191. k = &j->btree_root;
  1192. err = "bad btree root";
  1193. if (bch_btree_ptr_invalid(c, k))
  1194. goto err;
  1195. err = "error reading btree root";
  1196. c->root = bch_btree_node_get(c, k, j->btree_level, true);
  1197. if (IS_ERR_OR_NULL(c->root))
  1198. goto err;
  1199. list_del_init(&c->root->list);
  1200. rw_unlock(true, c->root);
  1201. err = uuid_read(c, j, &cl);
  1202. if (err)
  1203. goto err;
  1204. err = "error in recovery";
  1205. if (bch_btree_check(c))
  1206. goto err;
  1207. bch_journal_mark(c, &journal);
  1208. bch_btree_gc_finish(c);
  1209. pr_debug("btree_check() done");
  1210. /*
  1211. * bcache_journal_next() can't happen sooner, or
  1212. * btree_gc_finish() will give spurious errors about last_gc >
  1213. * gc_gen - this is a hack but oh well.
  1214. */
  1215. bch_journal_next(&c->journal);
  1216. err = "error starting allocator thread";
  1217. for_each_cache(ca, c, i)
  1218. if (bch_cache_allocator_start(ca))
  1219. goto err;
  1220. /*
  1221. * First place it's safe to allocate: btree_check() and
  1222. * btree_gc_finish() have to run before we have buckets to
  1223. * allocate, and bch_bucket_alloc_set() might cause a journal
  1224. * entry to be written so bcache_journal_next() has to be called
  1225. * first.
  1226. *
  1227. * If the uuids were in the old format we have to rewrite them
  1228. * before the next journal entry is written:
  1229. */
  1230. if (j->version < BCACHE_JSET_VERSION_UUID)
  1231. __uuid_write(c);
  1232. bch_journal_replay(c, &journal);
  1233. } else {
  1234. pr_notice("invalidating existing data");
  1235. for_each_cache(ca, c, i) {
  1236. unsigned j;
  1237. ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
  1238. 2, SB_JOURNAL_BUCKETS);
  1239. for (j = 0; j < ca->sb.keys; j++)
  1240. ca->sb.d[j] = ca->sb.first_bucket + j;
  1241. }
  1242. bch_btree_gc_finish(c);
  1243. err = "error starting allocator thread";
  1244. for_each_cache(ca, c, i)
  1245. if (bch_cache_allocator_start(ca))
  1246. goto err;
  1247. mutex_lock(&c->bucket_lock);
  1248. for_each_cache(ca, c, i)
  1249. bch_prio_write(ca);
  1250. mutex_unlock(&c->bucket_lock);
  1251. err = "cannot allocate new UUID bucket";
  1252. if (__uuid_write(c))
  1253. goto err;
  1254. err = "cannot allocate new btree root";
  1255. c->root = bch_btree_node_alloc(c, 0, true);
  1256. if (IS_ERR_OR_NULL(c->root))
  1257. goto err;
  1258. bkey_copy_key(&c->root->key, &MAX_KEY);
  1259. bch_btree_node_write(c->root, &cl);
  1260. bch_btree_set_root(c->root);
  1261. rw_unlock(true, c->root);
  1262. /*
  1263. * We don't want to write the first journal entry until
  1264. * everything is set up - fortunately journal entries won't be
  1265. * written until the SET_CACHE_SYNC() here:
  1266. */
  1267. SET_CACHE_SYNC(&c->sb, true);
  1268. bch_journal_next(&c->journal);
  1269. bch_journal_meta(c, &cl);
  1270. }
  1271. err = "error starting gc thread";
  1272. if (bch_gc_thread_start(c))
  1273. goto err;
  1274. closure_sync(&cl);
  1275. c->sb.last_mount = get_seconds();
  1276. bcache_write_super(c);
  1277. list_for_each_entry_safe(dc, t, &uncached_devices, list)
  1278. bch_cached_dev_attach(dc, c);
  1279. flash_devs_run(c);
  1280. return;
  1281. err:
  1282. closure_sync(&cl);
  1283. /* XXX: test this, it's broken */
  1284. bch_cache_set_error(c, err);
  1285. }
  1286. static bool can_attach_cache(struct cache *ca, struct cache_set *c)
  1287. {
  1288. return ca->sb.block_size == c->sb.block_size &&
  1289. ca->sb.bucket_size == c->sb.block_size &&
  1290. ca->sb.nr_in_set == c->sb.nr_in_set;
  1291. }
  1292. static const char *register_cache_set(struct cache *ca)
  1293. {
  1294. char buf[12];
  1295. const char *err = "cannot allocate memory";
  1296. struct cache_set *c;
  1297. list_for_each_entry(c, &bch_cache_sets, list)
  1298. if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
  1299. if (c->cache[ca->sb.nr_this_dev])
  1300. return "duplicate cache set member";
  1301. if (!can_attach_cache(ca, c))
  1302. return "cache sb does not match set";
  1303. if (!CACHE_SYNC(&ca->sb))
  1304. SET_CACHE_SYNC(&c->sb, false);
  1305. goto found;
  1306. }
  1307. c = bch_cache_set_alloc(&ca->sb);
  1308. if (!c)
  1309. return err;
  1310. err = "error creating kobject";
  1311. if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
  1312. kobject_add(&c->internal, &c->kobj, "internal"))
  1313. goto err;
  1314. if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
  1315. goto err;
  1316. bch_debug_init_cache_set(c);
  1317. list_add(&c->list, &bch_cache_sets);
  1318. found:
  1319. sprintf(buf, "cache%i", ca->sb.nr_this_dev);
  1320. if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
  1321. sysfs_create_link(&c->kobj, &ca->kobj, buf))
  1322. goto err;
  1323. if (ca->sb.seq > c->sb.seq) {
  1324. c->sb.version = ca->sb.version;
  1325. memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
  1326. c->sb.flags = ca->sb.flags;
  1327. c->sb.seq = ca->sb.seq;
  1328. pr_debug("set version = %llu", c->sb.version);
  1329. }
  1330. ca->set = c;
  1331. ca->set->cache[ca->sb.nr_this_dev] = ca;
  1332. c->cache_by_alloc[c->caches_loaded++] = ca;
  1333. if (c->caches_loaded == c->sb.nr_in_set)
  1334. run_cache_set(c);
  1335. return NULL;
  1336. err:
  1337. bch_cache_set_unregister(c);
  1338. return err;
  1339. }
  1340. /* Cache device */
  1341. void bch_cache_release(struct kobject *kobj)
  1342. {
  1343. struct cache *ca = container_of(kobj, struct cache, kobj);
  1344. if (ca->set)
  1345. ca->set->cache[ca->sb.nr_this_dev] = NULL;
  1346. bio_split_pool_free(&ca->bio_split_hook);
  1347. free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
  1348. kfree(ca->prio_buckets);
  1349. vfree(ca->buckets);
  1350. free_heap(&ca->heap);
  1351. free_fifo(&ca->unused);
  1352. free_fifo(&ca->free_inc);
  1353. free_fifo(&ca->free);
  1354. if (ca->sb_bio.bi_inline_vecs[0].bv_page)
  1355. put_page(ca->sb_bio.bi_io_vec[0].bv_page);
  1356. if (!IS_ERR_OR_NULL(ca->bdev)) {
  1357. blk_sync_queue(bdev_get_queue(ca->bdev));
  1358. blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1359. }
  1360. kfree(ca);
  1361. module_put(THIS_MODULE);
  1362. }
  1363. static int cache_alloc(struct cache_sb *sb, struct cache *ca)
  1364. {
  1365. size_t free;
  1366. struct bucket *b;
  1367. __module_get(THIS_MODULE);
  1368. kobject_init(&ca->kobj, &bch_cache_ktype);
  1369. bio_init(&ca->journal.bio);
  1370. ca->journal.bio.bi_max_vecs = 8;
  1371. ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
  1372. free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
  1373. free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
  1374. if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
  1375. !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
  1376. !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
  1377. !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
  1378. !(ca->buckets = vzalloc(sizeof(struct bucket) *
  1379. ca->sb.nbuckets)) ||
  1380. !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
  1381. 2, GFP_KERNEL)) ||
  1382. !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
  1383. bio_split_pool_init(&ca->bio_split_hook))
  1384. return -ENOMEM;
  1385. ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
  1386. for_each_bucket(b, ca)
  1387. atomic_set(&b->pin, 0);
  1388. if (bch_cache_allocator_init(ca))
  1389. goto err;
  1390. return 0;
  1391. err:
  1392. kobject_put(&ca->kobj);
  1393. return -ENOMEM;
  1394. }
  1395. static void register_cache(struct cache_sb *sb, struct page *sb_page,
  1396. struct block_device *bdev, struct cache *ca)
  1397. {
  1398. char name[BDEVNAME_SIZE];
  1399. const char *err = "cannot allocate memory";
  1400. memcpy(&ca->sb, sb, sizeof(struct cache_sb));
  1401. ca->bdev = bdev;
  1402. ca->bdev->bd_holder = ca;
  1403. bio_init(&ca->sb_bio);
  1404. ca->sb_bio.bi_max_vecs = 1;
  1405. ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
  1406. ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
  1407. get_page(sb_page);
  1408. if (blk_queue_discard(bdev_get_queue(ca->bdev)))
  1409. ca->discard = CACHE_DISCARD(&ca->sb);
  1410. if (cache_alloc(sb, ca) != 0)
  1411. goto err;
  1412. err = "error creating kobject";
  1413. if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
  1414. goto err;
  1415. err = register_cache_set(ca);
  1416. if (err)
  1417. goto err;
  1418. pr_info("registered cache device %s", bdevname(bdev, name));
  1419. return;
  1420. err:
  1421. pr_notice("error opening %s: %s", bdevname(bdev, name), err);
  1422. kobject_put(&ca->kobj);
  1423. }
  1424. /* Global interfaces/init */
  1425. static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
  1426. const char *, size_t);
  1427. kobj_attribute_write(register, register_bcache);
  1428. kobj_attribute_write(register_quiet, register_bcache);
  1429. static bool bch_is_open_backing(struct block_device *bdev) {
  1430. struct cache_set *c, *tc;
  1431. struct cached_dev *dc, *t;
  1432. list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
  1433. list_for_each_entry_safe(dc, t, &c->cached_devs, list)
  1434. if (dc->bdev == bdev)
  1435. return true;
  1436. list_for_each_entry_safe(dc, t, &uncached_devices, list)
  1437. if (dc->bdev == bdev)
  1438. return true;
  1439. return false;
  1440. }
  1441. static bool bch_is_open_cache(struct block_device *bdev) {
  1442. struct cache_set *c, *tc;
  1443. struct cache *ca;
  1444. unsigned i;
  1445. list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
  1446. for_each_cache(ca, c, i)
  1447. if (ca->bdev == bdev)
  1448. return true;
  1449. return false;
  1450. }
  1451. static bool bch_is_open(struct block_device *bdev) {
  1452. return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
  1453. }
  1454. static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
  1455. const char *buffer, size_t size)
  1456. {
  1457. ssize_t ret = size;
  1458. const char *err = "cannot allocate memory";
  1459. char *path = NULL;
  1460. struct cache_sb *sb = NULL;
  1461. struct block_device *bdev = NULL;
  1462. struct page *sb_page = NULL;
  1463. if (!try_module_get(THIS_MODULE))
  1464. return -EBUSY;
  1465. mutex_lock(&bch_register_lock);
  1466. if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
  1467. !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
  1468. goto err;
  1469. err = "failed to open device";
  1470. bdev = blkdev_get_by_path(strim(path),
  1471. FMODE_READ|FMODE_WRITE|FMODE_EXCL,
  1472. sb);
  1473. if (IS_ERR(bdev)) {
  1474. if (bdev == ERR_PTR(-EBUSY)) {
  1475. bdev = lookup_bdev(strim(path));
  1476. if (!IS_ERR(bdev) && bch_is_open(bdev))
  1477. err = "device already registered";
  1478. else
  1479. err = "device busy";
  1480. }
  1481. goto err;
  1482. }
  1483. err = "failed to set blocksize";
  1484. if (set_blocksize(bdev, 4096))
  1485. goto err_close;
  1486. err = read_super(sb, bdev, &sb_page);
  1487. if (err)
  1488. goto err_close;
  1489. if (SB_IS_BDEV(sb)) {
  1490. struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
  1491. if (!dc)
  1492. goto err_close;
  1493. register_bdev(sb, sb_page, bdev, dc);
  1494. } else {
  1495. struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  1496. if (!ca)
  1497. goto err_close;
  1498. register_cache(sb, sb_page, bdev, ca);
  1499. }
  1500. out:
  1501. if (sb_page)
  1502. put_page(sb_page);
  1503. kfree(sb);
  1504. kfree(path);
  1505. mutex_unlock(&bch_register_lock);
  1506. module_put(THIS_MODULE);
  1507. return ret;
  1508. err_close:
  1509. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1510. err:
  1511. if (attr != &ksysfs_register_quiet)
  1512. pr_info("error opening %s: %s", path, err);
  1513. ret = -EINVAL;
  1514. goto out;
  1515. }
  1516. static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
  1517. {
  1518. if (code == SYS_DOWN ||
  1519. code == SYS_HALT ||
  1520. code == SYS_POWER_OFF) {
  1521. DEFINE_WAIT(wait);
  1522. unsigned long start = jiffies;
  1523. bool stopped = false;
  1524. struct cache_set *c, *tc;
  1525. struct cached_dev *dc, *tdc;
  1526. mutex_lock(&bch_register_lock);
  1527. if (list_empty(&bch_cache_sets) &&
  1528. list_empty(&uncached_devices))
  1529. goto out;
  1530. pr_info("Stopping all devices:");
  1531. list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
  1532. bch_cache_set_stop(c);
  1533. list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
  1534. bcache_device_stop(&dc->disk);
  1535. /* What's a condition variable? */
  1536. while (1) {
  1537. long timeout = start + 2 * HZ - jiffies;
  1538. stopped = list_empty(&bch_cache_sets) &&
  1539. list_empty(&uncached_devices);
  1540. if (timeout < 0 || stopped)
  1541. break;
  1542. prepare_to_wait(&unregister_wait, &wait,
  1543. TASK_UNINTERRUPTIBLE);
  1544. mutex_unlock(&bch_register_lock);
  1545. schedule_timeout(timeout);
  1546. mutex_lock(&bch_register_lock);
  1547. }
  1548. finish_wait(&unregister_wait, &wait);
  1549. if (stopped)
  1550. pr_info("All devices stopped");
  1551. else
  1552. pr_notice("Timeout waiting for devices to be closed");
  1553. out:
  1554. mutex_unlock(&bch_register_lock);
  1555. }
  1556. return NOTIFY_DONE;
  1557. }
  1558. static struct notifier_block reboot = {
  1559. .notifier_call = bcache_reboot,
  1560. .priority = INT_MAX, /* before any real devices */
  1561. };
  1562. static void bcache_exit(void)
  1563. {
  1564. bch_debug_exit();
  1565. bch_request_exit();
  1566. bch_btree_exit();
  1567. if (bcache_kobj)
  1568. kobject_put(bcache_kobj);
  1569. if (bcache_wq)
  1570. destroy_workqueue(bcache_wq);
  1571. unregister_blkdev(bcache_major, "bcache");
  1572. unregister_reboot_notifier(&reboot);
  1573. }
  1574. static int __init bcache_init(void)
  1575. {
  1576. static const struct attribute *files[] = {
  1577. &ksysfs_register.attr,
  1578. &ksysfs_register_quiet.attr,
  1579. NULL
  1580. };
  1581. mutex_init(&bch_register_lock);
  1582. init_waitqueue_head(&unregister_wait);
  1583. register_reboot_notifier(&reboot);
  1584. closure_debug_init();
  1585. bcache_major = register_blkdev(0, "bcache");
  1586. if (bcache_major < 0)
  1587. return bcache_major;
  1588. if (!(bcache_wq = create_workqueue("bcache")) ||
  1589. !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
  1590. sysfs_create_files(bcache_kobj, files) ||
  1591. bch_btree_init() ||
  1592. bch_request_init() ||
  1593. bch_debug_init(bcache_kobj))
  1594. goto err;
  1595. return 0;
  1596. err:
  1597. bcache_exit();
  1598. return -ENOMEM;
  1599. }
  1600. module_exit(bcache_exit);
  1601. module_init(bcache_init);