super.c 50 KB

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