super.c 52 KB

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