btt.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Block Translation Table
  4. * Copyright (c) 2014-2015, Intel Corporation.
  5. */
  6. #include <linux/highmem.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/blk-integrity.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/mutex.h>
  14. #include <linux/hdreg.h>
  15. #include <linux/sizes.h>
  16. #include <linux/ndctl.h>
  17. #include <linux/fs.h>
  18. #include <linux/nd.h>
  19. #include <linux/backing-dev.h>
  20. #include <linux/cleanup.h>
  21. #include "btt.h"
  22. #include "nd.h"
  23. enum log_ent_request {
  24. LOG_NEW_ENT = 0,
  25. LOG_OLD_ENT
  26. };
  27. static struct device *to_dev(struct arena_info *arena)
  28. {
  29. return &arena->nd_btt->dev;
  30. }
  31. static u64 adjust_initial_offset(struct nd_btt *nd_btt, u64 offset)
  32. {
  33. return offset + nd_btt->initial_offset;
  34. }
  35. static int arena_read_bytes(struct arena_info *arena, resource_size_t offset,
  36. void *buf, size_t n, unsigned long flags)
  37. {
  38. struct nd_btt *nd_btt = arena->nd_btt;
  39. struct nd_namespace_common *ndns = nd_btt->ndns;
  40. /* arena offsets may be shifted from the base of the device */
  41. offset = adjust_initial_offset(nd_btt, offset);
  42. return nvdimm_read_bytes(ndns, offset, buf, n, flags);
  43. }
  44. static int arena_write_bytes(struct arena_info *arena, resource_size_t offset,
  45. void *buf, size_t n, unsigned long flags)
  46. {
  47. struct nd_btt *nd_btt = arena->nd_btt;
  48. struct nd_namespace_common *ndns = nd_btt->ndns;
  49. /* arena offsets may be shifted from the base of the device */
  50. offset = adjust_initial_offset(nd_btt, offset);
  51. return nvdimm_write_bytes(ndns, offset, buf, n, flags);
  52. }
  53. static int btt_info_write(struct arena_info *arena, struct btt_sb *super)
  54. {
  55. int ret;
  56. /*
  57. * infooff and info2off should always be at least 512B aligned.
  58. * We rely on that to make sure rw_bytes does error clearing
  59. * correctly, so make sure that is the case.
  60. */
  61. dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->infooff, 512),
  62. "arena->infooff: %#llx is unaligned\n", arena->infooff);
  63. dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->info2off, 512),
  64. "arena->info2off: %#llx is unaligned\n", arena->info2off);
  65. ret = arena_write_bytes(arena, arena->info2off, super,
  66. sizeof(struct btt_sb), 0);
  67. if (ret)
  68. return ret;
  69. return arena_write_bytes(arena, arena->infooff, super,
  70. sizeof(struct btt_sb), 0);
  71. }
  72. static int btt_info_read(struct arena_info *arena, struct btt_sb *super)
  73. {
  74. return arena_read_bytes(arena, arena->infooff, super,
  75. sizeof(struct btt_sb), 0);
  76. }
  77. /*
  78. * 'raw' version of btt_map write
  79. * Assumptions:
  80. * mapping is in little-endian
  81. * mapping contains 'E' and 'Z' flags as desired
  82. */
  83. static int __btt_map_write(struct arena_info *arena, u32 lba, __le32 mapping,
  84. unsigned long flags)
  85. {
  86. u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE);
  87. if (unlikely(lba >= arena->external_nlba))
  88. dev_err_ratelimited(to_dev(arena),
  89. "%s: lba %#x out of range (max: %#x)\n",
  90. __func__, lba, arena->external_nlba);
  91. return arena_write_bytes(arena, ns_off, &mapping, MAP_ENT_SIZE, flags);
  92. }
  93. static int btt_map_write(struct arena_info *arena, u32 lba, u32 mapping,
  94. u32 z_flag, u32 e_flag, unsigned long rwb_flags)
  95. {
  96. u32 ze;
  97. __le32 mapping_le;
  98. /*
  99. * This 'mapping' is supposed to be just the LBA mapping, without
  100. * any flags set, so strip the flag bits.
  101. */
  102. mapping = ent_lba(mapping);
  103. ze = (z_flag << 1) + e_flag;
  104. switch (ze) {
  105. case 0:
  106. /*
  107. * We want to set neither of the Z or E flags, and
  108. * in the actual layout, this means setting the bit
  109. * positions of both to '1' to indicate a 'normal'
  110. * map entry
  111. */
  112. mapping |= MAP_ENT_NORMAL;
  113. break;
  114. case 1:
  115. mapping |= (1 << MAP_ERR_SHIFT);
  116. break;
  117. case 2:
  118. mapping |= (1 << MAP_TRIM_SHIFT);
  119. break;
  120. default:
  121. /*
  122. * The case where Z and E are both sent in as '1' could be
  123. * construed as a valid 'normal' case, but we decide not to,
  124. * to avoid confusion
  125. */
  126. dev_err_ratelimited(to_dev(arena),
  127. "Invalid use of Z and E flags\n");
  128. return -EIO;
  129. }
  130. mapping_le = cpu_to_le32(mapping);
  131. return __btt_map_write(arena, lba, mapping_le, rwb_flags);
  132. }
  133. static int btt_map_read(struct arena_info *arena, u32 lba, u32 *mapping,
  134. int *trim, int *error, unsigned long rwb_flags)
  135. {
  136. int ret;
  137. __le32 in;
  138. u32 raw_mapping, postmap, ze, z_flag, e_flag;
  139. u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE);
  140. if (unlikely(lba >= arena->external_nlba))
  141. dev_err_ratelimited(to_dev(arena),
  142. "%s: lba %#x out of range (max: %#x)\n",
  143. __func__, lba, arena->external_nlba);
  144. ret = arena_read_bytes(arena, ns_off, &in, MAP_ENT_SIZE, rwb_flags);
  145. if (ret)
  146. return ret;
  147. raw_mapping = le32_to_cpu(in);
  148. z_flag = ent_z_flag(raw_mapping);
  149. e_flag = ent_e_flag(raw_mapping);
  150. ze = (z_flag << 1) + e_flag;
  151. postmap = ent_lba(raw_mapping);
  152. /* Reuse the {z,e}_flag variables for *trim and *error */
  153. z_flag = 0;
  154. e_flag = 0;
  155. switch (ze) {
  156. case 0:
  157. /* Initial state. Return postmap = premap */
  158. *mapping = lba;
  159. break;
  160. case 1:
  161. *mapping = postmap;
  162. e_flag = 1;
  163. break;
  164. case 2:
  165. *mapping = postmap;
  166. z_flag = 1;
  167. break;
  168. case 3:
  169. *mapping = postmap;
  170. break;
  171. default:
  172. return -EIO;
  173. }
  174. if (trim)
  175. *trim = z_flag;
  176. if (error)
  177. *error = e_flag;
  178. return ret;
  179. }
  180. static int btt_log_group_read(struct arena_info *arena, u32 lane,
  181. struct log_group *log)
  182. {
  183. return arena_read_bytes(arena,
  184. arena->logoff + (lane * LOG_GRP_SIZE), log,
  185. LOG_GRP_SIZE, 0);
  186. }
  187. static struct dentry *debugfs_root;
  188. static void arena_debugfs_init(struct arena_info *a, struct dentry *parent,
  189. int idx)
  190. {
  191. char dirname[32];
  192. struct dentry *d;
  193. /* If for some reason, parent bttN was not created, exit */
  194. if (!parent)
  195. return;
  196. snprintf(dirname, 32, "arena%d", idx);
  197. d = debugfs_create_dir(dirname, parent);
  198. if (IS_ERR_OR_NULL(d))
  199. return;
  200. a->debugfs_dir = d;
  201. debugfs_create_x64("size", S_IRUGO, d, &a->size);
  202. debugfs_create_x64("external_lba_start", S_IRUGO, d,
  203. &a->external_lba_start);
  204. debugfs_create_x32("internal_nlba", S_IRUGO, d, &a->internal_nlba);
  205. debugfs_create_u32("internal_lbasize", S_IRUGO, d,
  206. &a->internal_lbasize);
  207. debugfs_create_x32("external_nlba", S_IRUGO, d, &a->external_nlba);
  208. debugfs_create_u32("external_lbasize", S_IRUGO, d,
  209. &a->external_lbasize);
  210. debugfs_create_u32("nfree", S_IRUGO, d, &a->nfree);
  211. debugfs_create_u16("version_major", S_IRUGO, d, &a->version_major);
  212. debugfs_create_u16("version_minor", S_IRUGO, d, &a->version_minor);
  213. debugfs_create_x64("nextoff", S_IRUGO, d, &a->nextoff);
  214. debugfs_create_x64("infooff", S_IRUGO, d, &a->infooff);
  215. debugfs_create_x64("dataoff", S_IRUGO, d, &a->dataoff);
  216. debugfs_create_x64("mapoff", S_IRUGO, d, &a->mapoff);
  217. debugfs_create_x64("logoff", S_IRUGO, d, &a->logoff);
  218. debugfs_create_x64("info2off", S_IRUGO, d, &a->info2off);
  219. debugfs_create_x32("flags", S_IRUGO, d, &a->flags);
  220. debugfs_create_u32("log_index_0", S_IRUGO, d, &a->log_index[0]);
  221. debugfs_create_u32("log_index_1", S_IRUGO, d, &a->log_index[1]);
  222. }
  223. static void btt_debugfs_init(struct btt *btt)
  224. {
  225. int i = 0;
  226. struct arena_info *arena;
  227. btt->debugfs_dir = debugfs_create_dir(dev_name(&btt->nd_btt->dev),
  228. debugfs_root);
  229. if (IS_ERR_OR_NULL(btt->debugfs_dir))
  230. return;
  231. list_for_each_entry(arena, &btt->arena_list, list) {
  232. arena_debugfs_init(arena, btt->debugfs_dir, i);
  233. i++;
  234. }
  235. }
  236. static u32 log_seq(struct log_group *log, int log_idx)
  237. {
  238. return le32_to_cpu(log->ent[log_idx].seq);
  239. }
  240. /*
  241. * This function accepts two log entries, and uses the
  242. * sequence number to find the 'older' entry.
  243. * It also updates the sequence number in this old entry to
  244. * make it the 'new' one if the mark_flag is set.
  245. * Finally, it returns which of the entries was the older one.
  246. *
  247. * TODO The logic feels a bit kludge-y. make it better..
  248. */
  249. static int btt_log_get_old(struct arena_info *a, struct log_group *log)
  250. {
  251. int idx0 = a->log_index[0];
  252. int idx1 = a->log_index[1];
  253. int old;
  254. /*
  255. * the first ever time this is seen, the entry goes into [0]
  256. * the next time, the following logic works out to put this
  257. * (next) entry into [1]
  258. */
  259. if (log_seq(log, idx0) == 0) {
  260. log->ent[idx0].seq = cpu_to_le32(1);
  261. return 0;
  262. }
  263. if (log_seq(log, idx0) == log_seq(log, idx1))
  264. return -EINVAL;
  265. if (log_seq(log, idx0) + log_seq(log, idx1) > 5)
  266. return -EINVAL;
  267. if (log_seq(log, idx0) < log_seq(log, idx1)) {
  268. if ((log_seq(log, idx1) - log_seq(log, idx0)) == 1)
  269. old = 0;
  270. else
  271. old = 1;
  272. } else {
  273. if ((log_seq(log, idx0) - log_seq(log, idx1)) == 1)
  274. old = 1;
  275. else
  276. old = 0;
  277. }
  278. return old;
  279. }
  280. /*
  281. * This function copies the desired (old/new) log entry into ent if
  282. * it is not NULL. It returns the sub-slot number (0 or 1)
  283. * where the desired log entry was found. Negative return values
  284. * indicate errors.
  285. */
  286. static int btt_log_read(struct arena_info *arena, u32 lane,
  287. struct log_entry *ent, int old_flag)
  288. {
  289. int ret;
  290. int old_ent, ret_ent;
  291. struct log_group log;
  292. ret = btt_log_group_read(arena, lane, &log);
  293. if (ret)
  294. return -EIO;
  295. old_ent = btt_log_get_old(arena, &log);
  296. if (old_ent < 0 || old_ent > 1) {
  297. dev_err(to_dev(arena),
  298. "log corruption (%d): lane %d seq [%d, %d]\n",
  299. old_ent, lane, log.ent[arena->log_index[0]].seq,
  300. log.ent[arena->log_index[1]].seq);
  301. /* TODO set error state? */
  302. return -EIO;
  303. }
  304. ret_ent = (old_flag ? old_ent : (1 - old_ent));
  305. if (ent != NULL)
  306. memcpy(ent, &log.ent[arena->log_index[ret_ent]], LOG_ENT_SIZE);
  307. return ret_ent;
  308. }
  309. /*
  310. * This function commits a log entry to media
  311. * It does _not_ prepare the freelist entry for the next write
  312. * btt_flog_write is the wrapper for updating the freelist elements
  313. */
  314. static int __btt_log_write(struct arena_info *arena, u32 lane,
  315. u32 sub, struct log_entry *ent, unsigned long flags)
  316. {
  317. int ret;
  318. u32 group_slot = arena->log_index[sub];
  319. unsigned int log_half = LOG_ENT_SIZE / 2;
  320. void *src = ent;
  321. u64 ns_off;
  322. ns_off = arena->logoff + (lane * LOG_GRP_SIZE) +
  323. (group_slot * LOG_ENT_SIZE);
  324. /* split the 16B write into atomic, durable halves */
  325. ret = arena_write_bytes(arena, ns_off, src, log_half, flags);
  326. if (ret)
  327. return ret;
  328. ns_off += log_half;
  329. src += log_half;
  330. return arena_write_bytes(arena, ns_off, src, log_half, flags);
  331. }
  332. static int btt_flog_write(struct arena_info *arena, u32 lane, u32 sub,
  333. struct log_entry *ent)
  334. {
  335. int ret;
  336. ret = __btt_log_write(arena, lane, sub, ent, NVDIMM_IO_ATOMIC);
  337. if (ret)
  338. return ret;
  339. /* prepare the next free entry */
  340. arena->freelist[lane].sub = 1 - arena->freelist[lane].sub;
  341. if (++(arena->freelist[lane].seq) == 4)
  342. arena->freelist[lane].seq = 1;
  343. if (ent_e_flag(le32_to_cpu(ent->old_map)))
  344. arena->freelist[lane].has_err = 1;
  345. arena->freelist[lane].block = ent_lba(le32_to_cpu(ent->old_map));
  346. return ret;
  347. }
  348. /*
  349. * This function initializes the BTT map to the initial state, which is
  350. * all-zeroes, and indicates an identity mapping
  351. */
  352. static int btt_map_init(struct arena_info *arena)
  353. {
  354. int ret = -EINVAL;
  355. void *zerobuf;
  356. size_t offset = 0;
  357. size_t chunk_size = SZ_2M;
  358. size_t mapsize = arena->logoff - arena->mapoff;
  359. zerobuf = kzalloc(chunk_size, GFP_KERNEL);
  360. if (!zerobuf)
  361. return -ENOMEM;
  362. /*
  363. * mapoff should always be at least 512B aligned. We rely on that to
  364. * make sure rw_bytes does error clearing correctly, so make sure that
  365. * is the case.
  366. */
  367. dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->mapoff, 512),
  368. "arena->mapoff: %#llx is unaligned\n", arena->mapoff);
  369. while (mapsize) {
  370. size_t size = min(mapsize, chunk_size);
  371. dev_WARN_ONCE(to_dev(arena), size < 512,
  372. "chunk size: %#zx is unaligned\n", size);
  373. ret = arena_write_bytes(arena, arena->mapoff + offset, zerobuf,
  374. size, 0);
  375. if (ret)
  376. goto free;
  377. offset += size;
  378. mapsize -= size;
  379. cond_resched();
  380. }
  381. free:
  382. kfree(zerobuf);
  383. return ret;
  384. }
  385. /*
  386. * This function initializes the BTT log with 'fake' entries pointing
  387. * to the initial reserved set of blocks as being free
  388. */
  389. static int btt_log_init(struct arena_info *arena)
  390. {
  391. size_t logsize = arena->info2off - arena->logoff;
  392. size_t chunk_size = SZ_4K, offset = 0;
  393. struct log_entry ent;
  394. void *zerobuf;
  395. int ret;
  396. u32 i;
  397. zerobuf = kzalloc(chunk_size, GFP_KERNEL);
  398. if (!zerobuf)
  399. return -ENOMEM;
  400. /*
  401. * logoff should always be at least 512B aligned. We rely on that to
  402. * make sure rw_bytes does error clearing correctly, so make sure that
  403. * is the case.
  404. */
  405. dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->logoff, 512),
  406. "arena->logoff: %#llx is unaligned\n", arena->logoff);
  407. while (logsize) {
  408. size_t size = min(logsize, chunk_size);
  409. dev_WARN_ONCE(to_dev(arena), size < 512,
  410. "chunk size: %#zx is unaligned\n", size);
  411. ret = arena_write_bytes(arena, arena->logoff + offset, zerobuf,
  412. size, 0);
  413. if (ret)
  414. goto free;
  415. offset += size;
  416. logsize -= size;
  417. cond_resched();
  418. }
  419. for (i = 0; i < arena->nfree; i++) {
  420. ent.lba = cpu_to_le32(i);
  421. ent.old_map = cpu_to_le32(arena->external_nlba + i);
  422. ent.new_map = cpu_to_le32(arena->external_nlba + i);
  423. ent.seq = cpu_to_le32(LOG_SEQ_INIT);
  424. ret = __btt_log_write(arena, i, 0, &ent, 0);
  425. if (ret)
  426. goto free;
  427. }
  428. free:
  429. kfree(zerobuf);
  430. return ret;
  431. }
  432. static u64 to_namespace_offset(struct arena_info *arena, u64 lba)
  433. {
  434. return arena->dataoff + ((u64)lba * arena->internal_lbasize);
  435. }
  436. static int arena_clear_freelist_error(struct arena_info *arena, u32 lane)
  437. {
  438. int ret = 0;
  439. if (arena->freelist[lane].has_err) {
  440. void *zero_page = page_address(ZERO_PAGE(0));
  441. u32 lba = arena->freelist[lane].block;
  442. u64 nsoff = to_namespace_offset(arena, lba);
  443. unsigned long len = arena->sector_size;
  444. mutex_lock(&arena->err_lock);
  445. while (len) {
  446. unsigned long chunk = min(len, PAGE_SIZE);
  447. ret = arena_write_bytes(arena, nsoff, zero_page,
  448. chunk, 0);
  449. if (ret)
  450. break;
  451. len -= chunk;
  452. nsoff += chunk;
  453. if (len == 0)
  454. arena->freelist[lane].has_err = 0;
  455. }
  456. mutex_unlock(&arena->err_lock);
  457. }
  458. return ret;
  459. }
  460. static int btt_freelist_init(struct arena_info *arena)
  461. {
  462. int new, ret;
  463. struct log_entry log_new;
  464. u32 i, map_entry, log_oldmap, log_newmap;
  465. arena->freelist = kcalloc(arena->nfree, sizeof(struct free_entry),
  466. GFP_KERNEL);
  467. if (!arena->freelist)
  468. return -ENOMEM;
  469. for (i = 0; i < arena->nfree; i++) {
  470. new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT);
  471. if (new < 0)
  472. return new;
  473. /* old and new map entries with any flags stripped out */
  474. log_oldmap = ent_lba(le32_to_cpu(log_new.old_map));
  475. log_newmap = ent_lba(le32_to_cpu(log_new.new_map));
  476. /* sub points to the next one to be overwritten */
  477. arena->freelist[i].sub = 1 - new;
  478. arena->freelist[i].seq = nd_inc_seq(le32_to_cpu(log_new.seq));
  479. arena->freelist[i].block = log_oldmap;
  480. /*
  481. * FIXME: if error clearing fails during init, we want to make
  482. * the BTT read-only
  483. */
  484. if (ent_e_flag(le32_to_cpu(log_new.old_map)) &&
  485. !ent_normal(le32_to_cpu(log_new.old_map))) {
  486. arena->freelist[i].has_err = 1;
  487. ret = arena_clear_freelist_error(arena, i);
  488. if (ret)
  489. dev_err_ratelimited(to_dev(arena),
  490. "Unable to clear known errors\n");
  491. }
  492. /* This implies a newly created or untouched flog entry */
  493. if (log_oldmap == log_newmap)
  494. continue;
  495. /* Check if map recovery is needed */
  496. ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry,
  497. NULL, NULL, 0);
  498. if (ret)
  499. return ret;
  500. /*
  501. * The map_entry from btt_read_map is stripped of any flag bits,
  502. * so use the stripped out versions from the log as well for
  503. * testing whether recovery is needed. For restoration, use the
  504. * 'raw' version of the log entries as that captured what we
  505. * were going to write originally.
  506. */
  507. if ((log_newmap != map_entry) && (log_oldmap == map_entry)) {
  508. /*
  509. * Last transaction wrote the flog, but wasn't able
  510. * to complete the map write. So fix up the map.
  511. */
  512. ret = btt_map_write(arena, le32_to_cpu(log_new.lba),
  513. le32_to_cpu(log_new.new_map), 0, 0, 0);
  514. if (ret)
  515. return ret;
  516. }
  517. }
  518. return 0;
  519. }
  520. static bool ent_is_padding(struct log_entry *ent)
  521. {
  522. return (ent->lba == 0) && (ent->old_map == 0) && (ent->new_map == 0)
  523. && (ent->seq == 0);
  524. }
  525. /*
  526. * Detecting valid log indices: We read a log group (see the comments in btt.h
  527. * for a description of a 'log_group' and its 'slots'), and iterate over its
  528. * four slots. We expect that a padding slot will be all-zeroes, and use this
  529. * to detect a padding slot vs. an actual entry.
  530. *
  531. * If a log_group is in the initial state, i.e. hasn't been used since the
  532. * creation of this BTT layout, it will have three of the four slots with
  533. * zeroes. We skip over these log_groups for the detection of log_index. If
  534. * all log_groups are in the initial state (i.e. the BTT has never been
  535. * written to), it is safe to assume the 'new format' of log entries in slots
  536. * (0, 1).
  537. */
  538. static int log_set_indices(struct arena_info *arena)
  539. {
  540. bool idx_set = false, initial_state = true;
  541. int ret, log_index[2] = {-1, -1};
  542. u32 i, j, next_idx = 0;
  543. struct log_group log;
  544. u32 pad_count = 0;
  545. for (i = 0; i < arena->nfree; i++) {
  546. ret = btt_log_group_read(arena, i, &log);
  547. if (ret < 0)
  548. return ret;
  549. for (j = 0; j < 4; j++) {
  550. if (!idx_set) {
  551. if (ent_is_padding(&log.ent[j])) {
  552. pad_count++;
  553. continue;
  554. } else {
  555. /* Skip if index has been recorded */
  556. if ((next_idx == 1) &&
  557. (j == log_index[0]))
  558. continue;
  559. /* valid entry, record index */
  560. log_index[next_idx] = j;
  561. next_idx++;
  562. }
  563. if (next_idx == 2) {
  564. /* two valid entries found */
  565. idx_set = true;
  566. } else if (next_idx > 2) {
  567. /* too many valid indices */
  568. return -ENXIO;
  569. }
  570. } else {
  571. /*
  572. * once the indices have been set, just verify
  573. * that all subsequent log groups are either in
  574. * their initial state or follow the same
  575. * indices.
  576. */
  577. if (j == log_index[0]) {
  578. /* entry must be 'valid' */
  579. if (ent_is_padding(&log.ent[j]))
  580. return -ENXIO;
  581. } else if (j == log_index[1]) {
  582. ;
  583. /*
  584. * log_index[1] can be padding if the
  585. * lane never got used and it is still
  586. * in the initial state (three 'padding'
  587. * entries)
  588. */
  589. } else {
  590. /* entry must be invalid (padding) */
  591. if (!ent_is_padding(&log.ent[j]))
  592. return -ENXIO;
  593. }
  594. }
  595. }
  596. /*
  597. * If any of the log_groups have more than one valid,
  598. * non-padding entry, then the we are no longer in the
  599. * initial_state
  600. */
  601. if (pad_count < 3)
  602. initial_state = false;
  603. pad_count = 0;
  604. }
  605. if (!initial_state && !idx_set)
  606. return -ENXIO;
  607. /*
  608. * If all the entries in the log were in the initial state,
  609. * assume new padding scheme
  610. */
  611. if (initial_state)
  612. log_index[1] = 1;
  613. /*
  614. * Only allow the known permutations of log/padding indices,
  615. * i.e. (0, 1), and (0, 2)
  616. */
  617. if ((log_index[0] == 0) && ((log_index[1] == 1) || (log_index[1] == 2)))
  618. ; /* known index possibilities */
  619. else {
  620. dev_err(to_dev(arena), "Found an unknown padding scheme\n");
  621. return -ENXIO;
  622. }
  623. arena->log_index[0] = log_index[0];
  624. arena->log_index[1] = log_index[1];
  625. dev_dbg(to_dev(arena), "log_index_0 = %d\n", log_index[0]);
  626. dev_dbg(to_dev(arena), "log_index_1 = %d\n", log_index[1]);
  627. return 0;
  628. }
  629. static int btt_rtt_init(struct arena_info *arena)
  630. {
  631. arena->rtt = kcalloc(arena->nfree, sizeof(u32), GFP_KERNEL);
  632. if (arena->rtt == NULL)
  633. return -ENOMEM;
  634. return 0;
  635. }
  636. static int btt_maplocks_init(struct arena_info *arena)
  637. {
  638. u32 i;
  639. arena->map_locks = kcalloc(arena->nfree, sizeof(struct aligned_lock),
  640. GFP_KERNEL);
  641. if (!arena->map_locks)
  642. return -ENOMEM;
  643. for (i = 0; i < arena->nfree; i++)
  644. spin_lock_init(&arena->map_locks[i].lock);
  645. return 0;
  646. }
  647. static struct arena_info *alloc_arena(struct btt *btt, size_t size,
  648. size_t start, size_t arena_off)
  649. {
  650. struct arena_info *arena;
  651. u64 logsize, mapsize, datasize;
  652. u64 available = size;
  653. arena = kzalloc(sizeof(*arena), GFP_KERNEL);
  654. if (!arena)
  655. return NULL;
  656. arena->nd_btt = btt->nd_btt;
  657. arena->sector_size = btt->sector_size;
  658. mutex_init(&arena->err_lock);
  659. if (!size)
  660. return arena;
  661. arena->size = size;
  662. arena->external_lba_start = start;
  663. arena->external_lbasize = btt->lbasize;
  664. arena->internal_lbasize = roundup(arena->external_lbasize,
  665. INT_LBASIZE_ALIGNMENT);
  666. arena->nfree = BTT_DEFAULT_NFREE;
  667. arena->version_major = btt->nd_btt->version_major;
  668. arena->version_minor = btt->nd_btt->version_minor;
  669. if (available % BTT_PG_SIZE)
  670. available -= (available % BTT_PG_SIZE);
  671. /* Two pages are reserved for the super block and its copy */
  672. available -= 2 * BTT_PG_SIZE;
  673. /* The log takes a fixed amount of space based on nfree */
  674. logsize = roundup(arena->nfree * LOG_GRP_SIZE, BTT_PG_SIZE);
  675. available -= logsize;
  676. /* Calculate optimal split between map and data area */
  677. arena->internal_nlba = div_u64(available - BTT_PG_SIZE,
  678. arena->internal_lbasize + MAP_ENT_SIZE);
  679. arena->external_nlba = arena->internal_nlba - arena->nfree;
  680. mapsize = roundup((arena->external_nlba * MAP_ENT_SIZE), BTT_PG_SIZE);
  681. datasize = available - mapsize;
  682. /* 'Absolute' values, relative to start of storage space */
  683. arena->infooff = arena_off;
  684. arena->dataoff = arena->infooff + BTT_PG_SIZE;
  685. arena->mapoff = arena->dataoff + datasize;
  686. arena->logoff = arena->mapoff + mapsize;
  687. arena->info2off = arena->logoff + logsize;
  688. /* Default log indices are (0,1) */
  689. arena->log_index[0] = 0;
  690. arena->log_index[1] = 1;
  691. return arena;
  692. }
  693. static void free_arenas(struct btt *btt)
  694. {
  695. struct arena_info *arena, *next;
  696. list_for_each_entry_safe(arena, next, &btt->arena_list, list) {
  697. list_del(&arena->list);
  698. kfree(arena->rtt);
  699. kfree(arena->map_locks);
  700. kfree(arena->freelist);
  701. debugfs_remove_recursive(arena->debugfs_dir);
  702. kfree(arena);
  703. }
  704. }
  705. /*
  706. * This function reads an existing valid btt superblock and
  707. * populates the corresponding arena_info struct
  708. */
  709. static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super,
  710. u64 arena_off)
  711. {
  712. arena->internal_nlba = le32_to_cpu(super->internal_nlba);
  713. arena->internal_lbasize = le32_to_cpu(super->internal_lbasize);
  714. arena->external_nlba = le32_to_cpu(super->external_nlba);
  715. arena->external_lbasize = le32_to_cpu(super->external_lbasize);
  716. arena->nfree = le32_to_cpu(super->nfree);
  717. arena->version_major = le16_to_cpu(super->version_major);
  718. arena->version_minor = le16_to_cpu(super->version_minor);
  719. arena->nextoff = (super->nextoff == 0) ? 0 : (arena_off +
  720. le64_to_cpu(super->nextoff));
  721. arena->infooff = arena_off;
  722. arena->dataoff = arena_off + le64_to_cpu(super->dataoff);
  723. arena->mapoff = arena_off + le64_to_cpu(super->mapoff);
  724. arena->logoff = arena_off + le64_to_cpu(super->logoff);
  725. arena->info2off = arena_off + le64_to_cpu(super->info2off);
  726. arena->size = (le64_to_cpu(super->nextoff) > 0)
  727. ? (le64_to_cpu(super->nextoff))
  728. : (arena->info2off - arena->infooff + BTT_PG_SIZE);
  729. arena->flags = le32_to_cpu(super->flags);
  730. }
  731. static int discover_arenas(struct btt *btt)
  732. {
  733. int ret = 0;
  734. struct arena_info *arena;
  735. size_t remaining = btt->rawsize;
  736. u64 cur_nlba = 0;
  737. size_t cur_off = 0;
  738. int num_arenas = 0;
  739. struct btt_sb *super __free(kfree) = kzalloc(sizeof(*super), GFP_KERNEL);
  740. if (!super)
  741. return -ENOMEM;
  742. while (remaining) {
  743. /* Alloc memory for arena */
  744. arena = alloc_arena(btt, 0, 0, 0);
  745. if (!arena)
  746. return -ENOMEM;
  747. arena->infooff = cur_off;
  748. ret = btt_info_read(arena, super);
  749. if (ret)
  750. goto out;
  751. if (!nd_btt_arena_is_valid(btt->nd_btt, super)) {
  752. if (remaining == btt->rawsize) {
  753. btt->init_state = INIT_NOTFOUND;
  754. dev_info(to_dev(arena), "No existing arenas\n");
  755. goto out;
  756. } else {
  757. dev_err(to_dev(arena),
  758. "Found corrupted metadata!\n");
  759. ret = -ENODEV;
  760. goto out;
  761. }
  762. }
  763. arena->external_lba_start = cur_nlba;
  764. parse_arena_meta(arena, super, cur_off);
  765. ret = log_set_indices(arena);
  766. if (ret) {
  767. dev_err(to_dev(arena),
  768. "Unable to deduce log/padding indices\n");
  769. goto out;
  770. }
  771. ret = btt_freelist_init(arena);
  772. if (ret)
  773. goto out;
  774. ret = btt_rtt_init(arena);
  775. if (ret)
  776. goto out;
  777. ret = btt_maplocks_init(arena);
  778. if (ret)
  779. goto out;
  780. list_add_tail(&arena->list, &btt->arena_list);
  781. remaining -= arena->size;
  782. cur_off += arena->size;
  783. cur_nlba += arena->external_nlba;
  784. num_arenas++;
  785. if (arena->nextoff == 0)
  786. break;
  787. }
  788. btt->num_arenas = num_arenas;
  789. btt->nlba = cur_nlba;
  790. btt->init_state = INIT_READY;
  791. return ret;
  792. out:
  793. kfree(arena);
  794. free_arenas(btt);
  795. return ret;
  796. }
  797. static int create_arenas(struct btt *btt)
  798. {
  799. size_t remaining = btt->rawsize;
  800. size_t cur_off = 0;
  801. while (remaining) {
  802. struct arena_info *arena;
  803. size_t arena_size = min_t(u64, ARENA_MAX_SIZE, remaining);
  804. remaining -= arena_size;
  805. if (arena_size < ARENA_MIN_SIZE)
  806. break;
  807. arena = alloc_arena(btt, arena_size, btt->nlba, cur_off);
  808. if (!arena) {
  809. free_arenas(btt);
  810. return -ENOMEM;
  811. }
  812. btt->nlba += arena->external_nlba;
  813. if (remaining >= ARENA_MIN_SIZE)
  814. arena->nextoff = arena->size;
  815. else
  816. arena->nextoff = 0;
  817. cur_off += arena_size;
  818. list_add_tail(&arena->list, &btt->arena_list);
  819. }
  820. return 0;
  821. }
  822. /*
  823. * This function completes arena initialization by writing
  824. * all the metadata.
  825. * It is only called for an uninitialized arena when a write
  826. * to that arena occurs for the first time.
  827. */
  828. static int btt_arena_write_layout(struct arena_info *arena)
  829. {
  830. int ret;
  831. u64 sum;
  832. struct btt_sb *super;
  833. struct nd_btt *nd_btt = arena->nd_btt;
  834. const uuid_t *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
  835. ret = btt_map_init(arena);
  836. if (ret)
  837. return ret;
  838. ret = btt_log_init(arena);
  839. if (ret)
  840. return ret;
  841. super = kzalloc(sizeof(*super), GFP_NOIO);
  842. if (!super)
  843. return -ENOMEM;
  844. strscpy(super->signature, BTT_SIG, sizeof(super->signature));
  845. export_uuid(super->uuid, nd_btt->uuid);
  846. export_uuid(super->parent_uuid, parent_uuid);
  847. super->flags = cpu_to_le32(arena->flags);
  848. super->version_major = cpu_to_le16(arena->version_major);
  849. super->version_minor = cpu_to_le16(arena->version_minor);
  850. super->external_lbasize = cpu_to_le32(arena->external_lbasize);
  851. super->external_nlba = cpu_to_le32(arena->external_nlba);
  852. super->internal_lbasize = cpu_to_le32(arena->internal_lbasize);
  853. super->internal_nlba = cpu_to_le32(arena->internal_nlba);
  854. super->nfree = cpu_to_le32(arena->nfree);
  855. super->infosize = cpu_to_le32(sizeof(struct btt_sb));
  856. super->nextoff = cpu_to_le64(arena->nextoff);
  857. /*
  858. * Subtract arena->infooff (arena start) so numbers are relative
  859. * to 'this' arena
  860. */
  861. super->dataoff = cpu_to_le64(arena->dataoff - arena->infooff);
  862. super->mapoff = cpu_to_le64(arena->mapoff - arena->infooff);
  863. super->logoff = cpu_to_le64(arena->logoff - arena->infooff);
  864. super->info2off = cpu_to_le64(arena->info2off - arena->infooff);
  865. super->flags = 0;
  866. sum = nd_sb_checksum((struct nd_gen_sb *) super);
  867. super->checksum = cpu_to_le64(sum);
  868. ret = btt_info_write(arena, super);
  869. kfree(super);
  870. return ret;
  871. }
  872. /*
  873. * This function completes the initialization for the BTT namespace
  874. * such that it is ready to accept IOs
  875. */
  876. static int btt_meta_init(struct btt *btt)
  877. {
  878. int ret = 0;
  879. struct arena_info *arena;
  880. mutex_lock(&btt->init_lock);
  881. list_for_each_entry(arena, &btt->arena_list, list) {
  882. ret = btt_arena_write_layout(arena);
  883. if (ret)
  884. goto unlock;
  885. ret = btt_freelist_init(arena);
  886. if (ret)
  887. goto unlock;
  888. ret = btt_rtt_init(arena);
  889. if (ret)
  890. goto unlock;
  891. ret = btt_maplocks_init(arena);
  892. if (ret)
  893. goto unlock;
  894. }
  895. btt->init_state = INIT_READY;
  896. unlock:
  897. mutex_unlock(&btt->init_lock);
  898. return ret;
  899. }
  900. static u32 btt_meta_size(struct btt *btt)
  901. {
  902. return btt->lbasize - btt->sector_size;
  903. }
  904. /*
  905. * This function calculates the arena in which the given LBA lies
  906. * by doing a linear walk. This is acceptable since we expect only
  907. * a few arenas. If we have backing devices that get much larger,
  908. * we can construct a balanced binary tree of arenas at init time
  909. * so that this range search becomes faster.
  910. */
  911. static int lba_to_arena(struct btt *btt, sector_t sector, __u32 *premap,
  912. struct arena_info **arena)
  913. {
  914. struct arena_info *arena_list;
  915. __u64 lba = div_u64(sector << SECTOR_SHIFT, btt->sector_size);
  916. list_for_each_entry(arena_list, &btt->arena_list, list) {
  917. if (lba < arena_list->external_nlba) {
  918. *arena = arena_list;
  919. *premap = lba;
  920. return 0;
  921. }
  922. lba -= arena_list->external_nlba;
  923. }
  924. return -EIO;
  925. }
  926. /*
  927. * The following (lock_map, unlock_map) are mostly just to improve
  928. * readability, since they index into an array of locks
  929. */
  930. static void lock_map(struct arena_info *arena, u32 premap)
  931. __acquires(&arena->map_locks[idx].lock)
  932. {
  933. u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  934. spin_lock(&arena->map_locks[idx].lock);
  935. }
  936. static void unlock_map(struct arena_info *arena, u32 premap)
  937. __releases(&arena->map_locks[idx].lock)
  938. {
  939. u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  940. spin_unlock(&arena->map_locks[idx].lock);
  941. }
  942. static int btt_data_read(struct arena_info *arena, struct page *page,
  943. unsigned int off, u32 lba, u32 len)
  944. {
  945. int ret;
  946. u64 nsoff = to_namespace_offset(arena, lba);
  947. void *mem = kmap_atomic(page);
  948. ret = arena_read_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC);
  949. kunmap_atomic(mem);
  950. return ret;
  951. }
  952. static int btt_data_write(struct arena_info *arena, u32 lba,
  953. struct page *page, unsigned int off, u32 len)
  954. {
  955. int ret;
  956. u64 nsoff = to_namespace_offset(arena, lba);
  957. void *mem = kmap_atomic(page);
  958. ret = arena_write_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC);
  959. kunmap_atomic(mem);
  960. return ret;
  961. }
  962. static void zero_fill_data(struct page *page, unsigned int off, u32 len)
  963. {
  964. void *mem = kmap_atomic(page);
  965. memset(mem + off, 0, len);
  966. kunmap_atomic(mem);
  967. }
  968. #ifdef CONFIG_BLK_DEV_INTEGRITY
  969. static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  970. struct arena_info *arena, u32 postmap, int rw)
  971. {
  972. unsigned int len = btt_meta_size(btt);
  973. u64 meta_nsoff;
  974. int ret = 0;
  975. if (bip == NULL)
  976. return 0;
  977. meta_nsoff = to_namespace_offset(arena, postmap) + btt->sector_size;
  978. while (len) {
  979. unsigned int cur_len;
  980. struct bio_vec bv;
  981. void *mem;
  982. bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  983. /*
  984. * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
  985. * .bv_offset already adjusted for iter->bi_bvec_done, and we
  986. * can use those directly
  987. */
  988. cur_len = min(len, bv.bv_len);
  989. mem = bvec_kmap_local(&bv);
  990. if (rw)
  991. ret = arena_write_bytes(arena, meta_nsoff, mem, cur_len,
  992. NVDIMM_IO_ATOMIC);
  993. else
  994. ret = arena_read_bytes(arena, meta_nsoff, mem, cur_len,
  995. NVDIMM_IO_ATOMIC);
  996. kunmap_local(mem);
  997. if (ret)
  998. return ret;
  999. len -= cur_len;
  1000. meta_nsoff += cur_len;
  1001. if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len))
  1002. return -EIO;
  1003. }
  1004. return ret;
  1005. }
  1006. #else /* CONFIG_BLK_DEV_INTEGRITY */
  1007. static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  1008. struct arena_info *arena, u32 postmap, int rw)
  1009. {
  1010. return 0;
  1011. }
  1012. #endif
  1013. static int btt_read_pg(struct btt *btt, struct bio_integrity_payload *bip,
  1014. struct page *page, unsigned int off, sector_t sector,
  1015. unsigned int len)
  1016. {
  1017. int ret = 0;
  1018. int t_flag, e_flag;
  1019. struct arena_info *arena = NULL;
  1020. u32 lane = 0, premap, postmap;
  1021. while (len) {
  1022. u32 cur_len;
  1023. lane = nd_region_acquire_lane(btt->nd_region);
  1024. ret = lba_to_arena(btt, sector, &premap, &arena);
  1025. if (ret)
  1026. goto out_lane;
  1027. cur_len = min(btt->sector_size, len);
  1028. ret = btt_map_read(arena, premap, &postmap, &t_flag, &e_flag,
  1029. NVDIMM_IO_ATOMIC);
  1030. if (ret)
  1031. goto out_lane;
  1032. /*
  1033. * We loop to make sure that the post map LBA didn't change
  1034. * from under us between writing the RTT and doing the actual
  1035. * read.
  1036. */
  1037. while (1) {
  1038. u32 new_map;
  1039. int new_t, new_e;
  1040. if (t_flag) {
  1041. zero_fill_data(page, off, cur_len);
  1042. goto out_lane;
  1043. }
  1044. if (e_flag) {
  1045. ret = -EIO;
  1046. goto out_lane;
  1047. }
  1048. arena->rtt[lane] = RTT_VALID | postmap;
  1049. /*
  1050. * Barrier to make sure this write is not reordered
  1051. * to do the verification map_read before the RTT store
  1052. */
  1053. barrier();
  1054. ret = btt_map_read(arena, premap, &new_map, &new_t,
  1055. &new_e, NVDIMM_IO_ATOMIC);
  1056. if (ret)
  1057. goto out_rtt;
  1058. if ((postmap == new_map) && (t_flag == new_t) &&
  1059. (e_flag == new_e))
  1060. break;
  1061. postmap = new_map;
  1062. t_flag = new_t;
  1063. e_flag = new_e;
  1064. }
  1065. ret = btt_data_read(arena, page, off, postmap, cur_len);
  1066. if (ret) {
  1067. /* Media error - set the e_flag */
  1068. if (btt_map_write(arena, premap, postmap, 0, 1, NVDIMM_IO_ATOMIC))
  1069. dev_warn_ratelimited(to_dev(arena),
  1070. "Error persistently tracking bad blocks at %#x\n",
  1071. premap);
  1072. goto out_rtt;
  1073. }
  1074. if (bip) {
  1075. ret = btt_rw_integrity(btt, bip, arena, postmap, READ);
  1076. if (ret)
  1077. goto out_rtt;
  1078. }
  1079. arena->rtt[lane] = RTT_INVALID;
  1080. nd_region_release_lane(btt->nd_region, lane);
  1081. len -= cur_len;
  1082. off += cur_len;
  1083. sector += btt->sector_size >> SECTOR_SHIFT;
  1084. }
  1085. return 0;
  1086. out_rtt:
  1087. arena->rtt[lane] = RTT_INVALID;
  1088. out_lane:
  1089. nd_region_release_lane(btt->nd_region, lane);
  1090. return ret;
  1091. }
  1092. /*
  1093. * Normally, arena_{read,write}_bytes will take care of the initial offset
  1094. * adjustment, but in the case of btt_is_badblock, where we query is_bad_pmem,
  1095. * we need the final, raw namespace offset here
  1096. */
  1097. static bool btt_is_badblock(struct btt *btt, struct arena_info *arena,
  1098. u32 postmap)
  1099. {
  1100. u64 nsoff = adjust_initial_offset(arena->nd_btt,
  1101. to_namespace_offset(arena, postmap));
  1102. sector_t phys_sector = nsoff >> 9;
  1103. return is_bad_pmem(btt->phys_bb, phys_sector, arena->internal_lbasize);
  1104. }
  1105. static int btt_write_pg(struct btt *btt, struct bio_integrity_payload *bip,
  1106. sector_t sector, struct page *page, unsigned int off,
  1107. unsigned int len)
  1108. {
  1109. int ret = 0;
  1110. struct arena_info *arena = NULL;
  1111. u32 premap = 0, old_postmap, new_postmap, lane = 0, i;
  1112. struct log_entry log;
  1113. int sub;
  1114. while (len) {
  1115. u32 cur_len;
  1116. int e_flag;
  1117. retry:
  1118. lane = nd_region_acquire_lane(btt->nd_region);
  1119. ret = lba_to_arena(btt, sector, &premap, &arena);
  1120. if (ret)
  1121. goto out_lane;
  1122. cur_len = min(btt->sector_size, len);
  1123. if ((arena->flags & IB_FLAG_ERROR_MASK) != 0) {
  1124. ret = -EIO;
  1125. goto out_lane;
  1126. }
  1127. if (btt_is_badblock(btt, arena, arena->freelist[lane].block))
  1128. arena->freelist[lane].has_err = 1;
  1129. if (mutex_is_locked(&arena->err_lock)
  1130. || arena->freelist[lane].has_err) {
  1131. nd_region_release_lane(btt->nd_region, lane);
  1132. ret = arena_clear_freelist_error(arena, lane);
  1133. if (ret)
  1134. return ret;
  1135. /* OK to acquire a different lane/free block */
  1136. goto retry;
  1137. }
  1138. new_postmap = arena->freelist[lane].block;
  1139. /* Wait if the new block is being read from */
  1140. for (i = 0; i < arena->nfree; i++)
  1141. while (arena->rtt[i] == (RTT_VALID | new_postmap))
  1142. cpu_relax();
  1143. if (new_postmap >= arena->internal_nlba) {
  1144. ret = -EIO;
  1145. goto out_lane;
  1146. }
  1147. ret = btt_data_write(arena, new_postmap, page, off, cur_len);
  1148. if (ret)
  1149. goto out_lane;
  1150. if (bip) {
  1151. ret = btt_rw_integrity(btt, bip, arena, new_postmap,
  1152. WRITE);
  1153. if (ret)
  1154. goto out_lane;
  1155. }
  1156. lock_map(arena, premap);
  1157. ret = btt_map_read(arena, premap, &old_postmap, NULL, &e_flag,
  1158. NVDIMM_IO_ATOMIC);
  1159. if (ret)
  1160. goto out_map;
  1161. if (old_postmap >= arena->internal_nlba) {
  1162. ret = -EIO;
  1163. goto out_map;
  1164. }
  1165. if (e_flag)
  1166. set_e_flag(old_postmap);
  1167. log.lba = cpu_to_le32(premap);
  1168. log.old_map = cpu_to_le32(old_postmap);
  1169. log.new_map = cpu_to_le32(new_postmap);
  1170. log.seq = cpu_to_le32(arena->freelist[lane].seq);
  1171. sub = arena->freelist[lane].sub;
  1172. ret = btt_flog_write(arena, lane, sub, &log);
  1173. if (ret)
  1174. goto out_map;
  1175. ret = btt_map_write(arena, premap, new_postmap, 0, 0,
  1176. NVDIMM_IO_ATOMIC);
  1177. if (ret)
  1178. goto out_map;
  1179. unlock_map(arena, premap);
  1180. nd_region_release_lane(btt->nd_region, lane);
  1181. if (e_flag) {
  1182. ret = arena_clear_freelist_error(arena, lane);
  1183. if (ret)
  1184. return ret;
  1185. }
  1186. len -= cur_len;
  1187. off += cur_len;
  1188. sector += btt->sector_size >> SECTOR_SHIFT;
  1189. }
  1190. return 0;
  1191. out_map:
  1192. unlock_map(arena, premap);
  1193. out_lane:
  1194. nd_region_release_lane(btt->nd_region, lane);
  1195. return ret;
  1196. }
  1197. static int btt_do_bvec(struct btt *btt, struct bio_integrity_payload *bip,
  1198. struct page *page, unsigned int len, unsigned int off,
  1199. enum req_op op, sector_t sector)
  1200. {
  1201. int ret;
  1202. if (!op_is_write(op)) {
  1203. ret = btt_read_pg(btt, bip, page, off, sector, len);
  1204. flush_dcache_page(page);
  1205. } else {
  1206. flush_dcache_page(page);
  1207. ret = btt_write_pg(btt, bip, sector, page, off, len);
  1208. }
  1209. return ret;
  1210. }
  1211. static void btt_submit_bio(struct bio *bio)
  1212. {
  1213. struct bio_integrity_payload *bip = bio_integrity(bio);
  1214. struct btt *btt = bio->bi_bdev->bd_disk->private_data;
  1215. struct bvec_iter iter;
  1216. unsigned long start;
  1217. struct bio_vec bvec;
  1218. int err = 0;
  1219. bool do_acct;
  1220. if (!bio_integrity_prep(bio))
  1221. return;
  1222. do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
  1223. if (do_acct)
  1224. start = bio_start_io_acct(bio);
  1225. bio_for_each_segment(bvec, bio, iter) {
  1226. unsigned int len = bvec.bv_len;
  1227. if (len > PAGE_SIZE || len < btt->sector_size ||
  1228. len % btt->sector_size) {
  1229. dev_err_ratelimited(&btt->nd_btt->dev,
  1230. "unaligned bio segment (len: %d)\n", len);
  1231. bio->bi_status = BLK_STS_IOERR;
  1232. break;
  1233. }
  1234. err = btt_do_bvec(btt, bip, bvec.bv_page, len, bvec.bv_offset,
  1235. bio_op(bio), iter.bi_sector);
  1236. if (err) {
  1237. dev_err(&btt->nd_btt->dev,
  1238. "io error in %s sector %lld, len %d,\n",
  1239. (op_is_write(bio_op(bio))) ? "WRITE" :
  1240. "READ",
  1241. (unsigned long long) iter.bi_sector, len);
  1242. bio->bi_status = errno_to_blk_status(err);
  1243. break;
  1244. }
  1245. }
  1246. if (do_acct)
  1247. bio_end_io_acct(bio, start);
  1248. bio_endio(bio);
  1249. }
  1250. static int btt_getgeo(struct block_device *bd, struct hd_geometry *geo)
  1251. {
  1252. /* some standard values */
  1253. geo->heads = 1 << 6;
  1254. geo->sectors = 1 << 5;
  1255. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  1256. return 0;
  1257. }
  1258. static const struct block_device_operations btt_fops = {
  1259. .owner = THIS_MODULE,
  1260. .submit_bio = btt_submit_bio,
  1261. .getgeo = btt_getgeo,
  1262. };
  1263. static int btt_blk_init(struct btt *btt)
  1264. {
  1265. struct nd_btt *nd_btt = btt->nd_btt;
  1266. struct nd_namespace_common *ndns = nd_btt->ndns;
  1267. struct queue_limits lim = {
  1268. .logical_block_size = btt->sector_size,
  1269. .max_hw_sectors = UINT_MAX,
  1270. .max_integrity_segments = 1,
  1271. .features = BLK_FEAT_SYNCHRONOUS,
  1272. };
  1273. int rc;
  1274. if (btt_meta_size(btt) && IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) {
  1275. lim.integrity.tuple_size = btt_meta_size(btt);
  1276. lim.integrity.tag_size = btt_meta_size(btt);
  1277. }
  1278. btt->btt_disk = blk_alloc_disk(&lim, NUMA_NO_NODE);
  1279. if (IS_ERR(btt->btt_disk))
  1280. return PTR_ERR(btt->btt_disk);
  1281. nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
  1282. btt->btt_disk->first_minor = 0;
  1283. btt->btt_disk->fops = &btt_fops;
  1284. btt->btt_disk->private_data = btt;
  1285. set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
  1286. rc = device_add_disk(&btt->nd_btt->dev, btt->btt_disk, NULL);
  1287. if (rc)
  1288. goto out_cleanup_disk;
  1289. btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
  1290. nvdimm_check_and_set_ro(btt->btt_disk);
  1291. return 0;
  1292. out_cleanup_disk:
  1293. put_disk(btt->btt_disk);
  1294. return rc;
  1295. }
  1296. static void btt_blk_cleanup(struct btt *btt)
  1297. {
  1298. del_gendisk(btt->btt_disk);
  1299. put_disk(btt->btt_disk);
  1300. }
  1301. /**
  1302. * btt_init - initialize a block translation table for the given device
  1303. * @nd_btt: device with BTT geometry and backing device info
  1304. * @rawsize: raw size in bytes of the backing device
  1305. * @lbasize: lba size of the backing device
  1306. * @uuid: A uuid for the backing device - this is stored on media
  1307. * @nd_region: &struct nd_region for the REGION device
  1308. *
  1309. * Initialize a Block Translation Table on a backing device to provide
  1310. * single sector power fail atomicity.
  1311. *
  1312. * Context:
  1313. * Might sleep.
  1314. *
  1315. * Returns:
  1316. * Pointer to a new struct btt on success, NULL on failure.
  1317. */
  1318. static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize,
  1319. u32 lbasize, uuid_t *uuid,
  1320. struct nd_region *nd_region)
  1321. {
  1322. int ret;
  1323. struct btt *btt;
  1324. struct nd_namespace_io *nsio;
  1325. struct device *dev = &nd_btt->dev;
  1326. btt = devm_kzalloc(dev, sizeof(struct btt), GFP_KERNEL);
  1327. if (!btt)
  1328. return NULL;
  1329. btt->nd_btt = nd_btt;
  1330. btt->rawsize = rawsize;
  1331. btt->lbasize = lbasize;
  1332. btt->sector_size = ((lbasize >= 4096) ? 4096 : 512);
  1333. INIT_LIST_HEAD(&btt->arena_list);
  1334. mutex_init(&btt->init_lock);
  1335. btt->nd_region = nd_region;
  1336. nsio = to_nd_namespace_io(&nd_btt->ndns->dev);
  1337. btt->phys_bb = &nsio->bb;
  1338. ret = discover_arenas(btt);
  1339. if (ret) {
  1340. dev_err(dev, "init: error in arena_discover: %d\n", ret);
  1341. return NULL;
  1342. }
  1343. if (btt->init_state != INIT_READY && nd_region->ro) {
  1344. dev_warn(dev, "%s is read-only, unable to init btt metadata\n",
  1345. dev_name(&nd_region->dev));
  1346. return NULL;
  1347. } else if (btt->init_state != INIT_READY) {
  1348. btt->num_arenas = (rawsize / ARENA_MAX_SIZE) +
  1349. ((rawsize % ARENA_MAX_SIZE) ? 1 : 0);
  1350. dev_dbg(dev, "init: %d arenas for %llu rawsize\n",
  1351. btt->num_arenas, rawsize);
  1352. ret = create_arenas(btt);
  1353. if (ret) {
  1354. dev_info(dev, "init: create_arenas: %d\n", ret);
  1355. return NULL;
  1356. }
  1357. ret = btt_meta_init(btt);
  1358. if (ret) {
  1359. dev_err(dev, "init: error in meta_init: %d\n", ret);
  1360. return NULL;
  1361. }
  1362. }
  1363. ret = btt_blk_init(btt);
  1364. if (ret) {
  1365. dev_err(dev, "init: error in blk_init: %d\n", ret);
  1366. return NULL;
  1367. }
  1368. btt_debugfs_init(btt);
  1369. return btt;
  1370. }
  1371. /**
  1372. * btt_fini - de-initialize a BTT
  1373. * @btt: the BTT handle that was generated by btt_init
  1374. *
  1375. * De-initialize a Block Translation Table on device removal
  1376. *
  1377. * Context:
  1378. * Might sleep.
  1379. */
  1380. static void btt_fini(struct btt *btt)
  1381. {
  1382. if (btt) {
  1383. btt_blk_cleanup(btt);
  1384. free_arenas(btt);
  1385. debugfs_remove_recursive(btt->debugfs_dir);
  1386. }
  1387. }
  1388. int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
  1389. {
  1390. struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
  1391. struct nd_region *nd_region;
  1392. struct btt_sb *btt_sb;
  1393. struct btt *btt;
  1394. size_t size, rawsize;
  1395. int rc;
  1396. if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize) {
  1397. dev_dbg(&nd_btt->dev, "incomplete btt configuration\n");
  1398. return -ENODEV;
  1399. }
  1400. btt_sb = devm_kzalloc(&nd_btt->dev, sizeof(*btt_sb), GFP_KERNEL);
  1401. if (!btt_sb)
  1402. return -ENOMEM;
  1403. size = nvdimm_namespace_capacity(ndns);
  1404. rc = devm_namespace_enable(&nd_btt->dev, ndns, size);
  1405. if (rc)
  1406. return rc;
  1407. /*
  1408. * If this returns < 0, that is ok as it just means there wasn't
  1409. * an existing BTT, and we're creating a new one. We still need to
  1410. * call this as we need the version dependent fields in nd_btt to be
  1411. * set correctly based on the holder class
  1412. */
  1413. nd_btt_version(nd_btt, ndns, btt_sb);
  1414. rawsize = size - nd_btt->initial_offset;
  1415. if (rawsize < ARENA_MIN_SIZE) {
  1416. dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
  1417. dev_name(&ndns->dev),
  1418. ARENA_MIN_SIZE + nd_btt->initial_offset);
  1419. return -ENXIO;
  1420. }
  1421. nd_region = to_nd_region(nd_btt->dev.parent);
  1422. btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
  1423. nd_region);
  1424. if (!btt)
  1425. return -ENOMEM;
  1426. nd_btt->btt = btt;
  1427. return 0;
  1428. }
  1429. EXPORT_SYMBOL(nvdimm_namespace_attach_btt);
  1430. int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt)
  1431. {
  1432. struct btt *btt = nd_btt->btt;
  1433. btt_fini(btt);
  1434. nd_btt->btt = NULL;
  1435. return 0;
  1436. }
  1437. EXPORT_SYMBOL(nvdimm_namespace_detach_btt);
  1438. static int __init nd_btt_init(void)
  1439. {
  1440. int rc = 0;
  1441. debugfs_root = debugfs_create_dir("btt", NULL);
  1442. if (IS_ERR_OR_NULL(debugfs_root))
  1443. rc = -ENXIO;
  1444. return rc;
  1445. }
  1446. static void __exit nd_btt_exit(void)
  1447. {
  1448. debugfs_remove_recursive(debugfs_root);
  1449. }
  1450. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT);
  1451. MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>");
  1452. MODULE_DESCRIPTION("NVDIMM Block Translation Table");
  1453. MODULE_LICENSE("GPL v2");
  1454. module_init(nd_btt_init);
  1455. module_exit(nd_btt_exit);