super.c 50 KB

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