super.c 49 KB

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