super.c 57 KB

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