super.c 50 KB

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