super.c 51 KB

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