super.c 59 KB

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