super.c 50 KB

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