btt.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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 = kzalloc_objs(struct free_entry, arena->nfree);
  466. if (!arena->freelist)
  467. return -ENOMEM;
  468. for (i = 0; i < arena->nfree; i++) {
  469. new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT);
  470. if (new < 0)
  471. return new;
  472. /* old and new map entries with any flags stripped out */
  473. log_oldmap = ent_lba(le32_to_cpu(log_new.old_map));
  474. log_newmap = ent_lba(le32_to_cpu(log_new.new_map));
  475. /* sub points to the next one to be overwritten */
  476. arena->freelist[i].sub = 1 - new;
  477. arena->freelist[i].seq = nd_inc_seq(le32_to_cpu(log_new.seq));
  478. arena->freelist[i].block = log_oldmap;
  479. /*
  480. * FIXME: if error clearing fails during init, we want to make
  481. * the BTT read-only
  482. */
  483. if (ent_e_flag(le32_to_cpu(log_new.old_map)) &&
  484. !ent_normal(le32_to_cpu(log_new.old_map))) {
  485. arena->freelist[i].has_err = 1;
  486. ret = arena_clear_freelist_error(arena, i);
  487. if (ret)
  488. dev_err_ratelimited(to_dev(arena),
  489. "Unable to clear known errors\n");
  490. }
  491. /* This implies a newly created or untouched flog entry */
  492. if (log_oldmap == log_newmap)
  493. continue;
  494. /* Check if map recovery is needed */
  495. ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry,
  496. NULL, NULL, 0);
  497. if (ret)
  498. return ret;
  499. /*
  500. * The map_entry from btt_read_map is stripped of any flag bits,
  501. * so use the stripped out versions from the log as well for
  502. * testing whether recovery is needed. For restoration, use the
  503. * 'raw' version of the log entries as that captured what we
  504. * were going to write originally.
  505. */
  506. if ((log_newmap != map_entry) && (log_oldmap == map_entry)) {
  507. /*
  508. * Last transaction wrote the flog, but wasn't able
  509. * to complete the map write. So fix up the map.
  510. */
  511. ret = btt_map_write(arena, le32_to_cpu(log_new.lba),
  512. le32_to_cpu(log_new.new_map), 0, 0, 0);
  513. if (ret)
  514. return ret;
  515. }
  516. }
  517. return 0;
  518. }
  519. static bool ent_is_padding(struct log_entry *ent)
  520. {
  521. return (ent->lba == 0) && (ent->old_map == 0) && (ent->new_map == 0)
  522. && (ent->seq == 0);
  523. }
  524. /*
  525. * Detecting valid log indices: We read a log group (see the comments in btt.h
  526. * for a description of a 'log_group' and its 'slots'), and iterate over its
  527. * four slots. We expect that a padding slot will be all-zeroes, and use this
  528. * to detect a padding slot vs. an actual entry.
  529. *
  530. * If a log_group is in the initial state, i.e. hasn't been used since the
  531. * creation of this BTT layout, it will have three of the four slots with
  532. * zeroes. We skip over these log_groups for the detection of log_index. If
  533. * all log_groups are in the initial state (i.e. the BTT has never been
  534. * written to), it is safe to assume the 'new format' of log entries in slots
  535. * (0, 1).
  536. */
  537. static int log_set_indices(struct arena_info *arena)
  538. {
  539. bool idx_set = false, initial_state = true;
  540. int ret, log_index[2] = {-1, -1};
  541. u32 i, j, next_idx = 0;
  542. struct log_group log;
  543. u32 pad_count = 0;
  544. for (i = 0; i < arena->nfree; i++) {
  545. ret = btt_log_group_read(arena, i, &log);
  546. if (ret < 0)
  547. return ret;
  548. for (j = 0; j < 4; j++) {
  549. if (!idx_set) {
  550. if (ent_is_padding(&log.ent[j])) {
  551. pad_count++;
  552. continue;
  553. } else {
  554. /* Skip if index has been recorded */
  555. if ((next_idx == 1) &&
  556. (j == log_index[0]))
  557. continue;
  558. /* valid entry, record index */
  559. log_index[next_idx] = j;
  560. next_idx++;
  561. }
  562. if (next_idx == 2) {
  563. /* two valid entries found */
  564. idx_set = true;
  565. } else if (next_idx > 2) {
  566. /* too many valid indices */
  567. return -ENXIO;
  568. }
  569. } else {
  570. /*
  571. * once the indices have been set, just verify
  572. * that all subsequent log groups are either in
  573. * their initial state or follow the same
  574. * indices.
  575. */
  576. if (j == log_index[0]) {
  577. /* entry must be 'valid' */
  578. if (ent_is_padding(&log.ent[j]))
  579. return -ENXIO;
  580. } else if (j == log_index[1]) {
  581. ;
  582. /*
  583. * log_index[1] can be padding if the
  584. * lane never got used and it is still
  585. * in the initial state (three 'padding'
  586. * entries)
  587. */
  588. } else {
  589. /* entry must be invalid (padding) */
  590. if (!ent_is_padding(&log.ent[j]))
  591. return -ENXIO;
  592. }
  593. }
  594. }
  595. /*
  596. * If any of the log_groups have more than one valid,
  597. * non-padding entry, then the we are no longer in the
  598. * initial_state
  599. */
  600. if (pad_count < 3)
  601. initial_state = false;
  602. pad_count = 0;
  603. }
  604. if (!initial_state && !idx_set)
  605. return -ENXIO;
  606. /*
  607. * If all the entries in the log were in the initial state,
  608. * assume new padding scheme
  609. */
  610. if (initial_state)
  611. log_index[1] = 1;
  612. /*
  613. * Only allow the known permutations of log/padding indices,
  614. * i.e. (0, 1), and (0, 2)
  615. */
  616. if ((log_index[0] == 0) && ((log_index[1] == 1) || (log_index[1] == 2)))
  617. ; /* known index possibilities */
  618. else {
  619. dev_err(to_dev(arena), "Found an unknown padding scheme\n");
  620. return -ENXIO;
  621. }
  622. arena->log_index[0] = log_index[0];
  623. arena->log_index[1] = log_index[1];
  624. dev_dbg(to_dev(arena), "log_index_0 = %d\n", log_index[0]);
  625. dev_dbg(to_dev(arena), "log_index_1 = %d\n", log_index[1]);
  626. return 0;
  627. }
  628. static int btt_rtt_init(struct arena_info *arena)
  629. {
  630. arena->rtt = kcalloc(arena->nfree, sizeof(u32), GFP_KERNEL);
  631. if (arena->rtt == NULL)
  632. return -ENOMEM;
  633. return 0;
  634. }
  635. static int btt_maplocks_init(struct arena_info *arena)
  636. {
  637. u32 i;
  638. arena->map_locks = kzalloc_objs(struct aligned_lock, arena->nfree);
  639. if (!arena->map_locks)
  640. return -ENOMEM;
  641. for (i = 0; i < arena->nfree; i++)
  642. spin_lock_init(&arena->map_locks[i].lock);
  643. return 0;
  644. }
  645. static struct arena_info *alloc_arena(struct btt *btt, size_t size,
  646. size_t start, size_t arena_off)
  647. {
  648. struct arena_info *arena;
  649. u64 logsize, mapsize, datasize;
  650. u64 available = size;
  651. arena = kzalloc_obj(*arena);
  652. if (!arena)
  653. return NULL;
  654. arena->nd_btt = btt->nd_btt;
  655. arena->sector_size = btt->sector_size;
  656. mutex_init(&arena->err_lock);
  657. if (!size)
  658. return arena;
  659. arena->size = size;
  660. arena->external_lba_start = start;
  661. arena->external_lbasize = btt->lbasize;
  662. arena->internal_lbasize = roundup(arena->external_lbasize,
  663. INT_LBASIZE_ALIGNMENT);
  664. arena->nfree = BTT_DEFAULT_NFREE;
  665. arena->version_major = btt->nd_btt->version_major;
  666. arena->version_minor = btt->nd_btt->version_minor;
  667. if (available % BTT_PG_SIZE)
  668. available -= (available % BTT_PG_SIZE);
  669. /* Two pages are reserved for the super block and its copy */
  670. available -= 2 * BTT_PG_SIZE;
  671. /* The log takes a fixed amount of space based on nfree */
  672. logsize = roundup(arena->nfree * LOG_GRP_SIZE, BTT_PG_SIZE);
  673. available -= logsize;
  674. /* Calculate optimal split between map and data area */
  675. arena->internal_nlba = div_u64(available - BTT_PG_SIZE,
  676. arena->internal_lbasize + MAP_ENT_SIZE);
  677. arena->external_nlba = arena->internal_nlba - arena->nfree;
  678. mapsize = roundup((arena->external_nlba * MAP_ENT_SIZE), BTT_PG_SIZE);
  679. datasize = available - mapsize;
  680. /* 'Absolute' values, relative to start of storage space */
  681. arena->infooff = arena_off;
  682. arena->dataoff = arena->infooff + BTT_PG_SIZE;
  683. arena->mapoff = arena->dataoff + datasize;
  684. arena->logoff = arena->mapoff + mapsize;
  685. arena->info2off = arena->logoff + logsize;
  686. /* Default log indices are (0,1) */
  687. arena->log_index[0] = 0;
  688. arena->log_index[1] = 1;
  689. return arena;
  690. }
  691. static void free_arenas(struct btt *btt)
  692. {
  693. struct arena_info *arena, *next;
  694. list_for_each_entry_safe(arena, next, &btt->arena_list, list) {
  695. list_del(&arena->list);
  696. kfree(arena->rtt);
  697. kfree(arena->map_locks);
  698. kfree(arena->freelist);
  699. debugfs_remove_recursive(arena->debugfs_dir);
  700. kfree(arena);
  701. }
  702. }
  703. /*
  704. * This function reads an existing valid btt superblock and
  705. * populates the corresponding arena_info struct
  706. */
  707. static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super,
  708. u64 arena_off)
  709. {
  710. arena->internal_nlba = le32_to_cpu(super->internal_nlba);
  711. arena->internal_lbasize = le32_to_cpu(super->internal_lbasize);
  712. arena->external_nlba = le32_to_cpu(super->external_nlba);
  713. arena->external_lbasize = le32_to_cpu(super->external_lbasize);
  714. arena->nfree = le32_to_cpu(super->nfree);
  715. arena->version_major = le16_to_cpu(super->version_major);
  716. arena->version_minor = le16_to_cpu(super->version_minor);
  717. arena->nextoff = (super->nextoff == 0) ? 0 : (arena_off +
  718. le64_to_cpu(super->nextoff));
  719. arena->infooff = arena_off;
  720. arena->dataoff = arena_off + le64_to_cpu(super->dataoff);
  721. arena->mapoff = arena_off + le64_to_cpu(super->mapoff);
  722. arena->logoff = arena_off + le64_to_cpu(super->logoff);
  723. arena->info2off = arena_off + le64_to_cpu(super->info2off);
  724. arena->size = (le64_to_cpu(super->nextoff) > 0)
  725. ? (le64_to_cpu(super->nextoff))
  726. : (arena->info2off - arena->infooff + BTT_PG_SIZE);
  727. arena->flags = le32_to_cpu(super->flags);
  728. }
  729. static int discover_arenas(struct btt *btt)
  730. {
  731. int ret = 0;
  732. struct arena_info *arena;
  733. size_t remaining = btt->rawsize;
  734. u64 cur_nlba = 0;
  735. size_t cur_off = 0;
  736. int num_arenas = 0;
  737. struct btt_sb *super __free(kfree) = kzalloc_obj(*super);
  738. if (!super)
  739. return -ENOMEM;
  740. while (remaining) {
  741. /* Alloc memory for arena */
  742. arena = alloc_arena(btt, 0, 0, 0);
  743. if (!arena)
  744. return -ENOMEM;
  745. arena->infooff = cur_off;
  746. ret = btt_info_read(arena, super);
  747. if (ret)
  748. goto out;
  749. if (!nd_btt_arena_is_valid(btt->nd_btt, super)) {
  750. if (remaining == btt->rawsize) {
  751. btt->init_state = INIT_NOTFOUND;
  752. dev_info(to_dev(arena), "No existing arenas\n");
  753. goto out;
  754. } else {
  755. dev_err(to_dev(arena),
  756. "Found corrupted metadata!\n");
  757. ret = -ENODEV;
  758. goto out;
  759. }
  760. }
  761. arena->external_lba_start = cur_nlba;
  762. parse_arena_meta(arena, super, cur_off);
  763. ret = log_set_indices(arena);
  764. if (ret) {
  765. dev_err(to_dev(arena),
  766. "Unable to deduce log/padding indices\n");
  767. goto out;
  768. }
  769. ret = btt_freelist_init(arena);
  770. if (ret)
  771. goto out;
  772. ret = btt_rtt_init(arena);
  773. if (ret)
  774. goto out;
  775. ret = btt_maplocks_init(arena);
  776. if (ret)
  777. goto out;
  778. list_add_tail(&arena->list, &btt->arena_list);
  779. remaining -= arena->size;
  780. cur_off += arena->size;
  781. cur_nlba += arena->external_nlba;
  782. num_arenas++;
  783. if (arena->nextoff == 0)
  784. break;
  785. }
  786. btt->num_arenas = num_arenas;
  787. btt->nlba = cur_nlba;
  788. btt->init_state = INIT_READY;
  789. return ret;
  790. out:
  791. kfree(arena);
  792. free_arenas(btt);
  793. return ret;
  794. }
  795. static int create_arenas(struct btt *btt)
  796. {
  797. size_t remaining = btt->rawsize;
  798. size_t cur_off = 0;
  799. while (remaining) {
  800. struct arena_info *arena;
  801. size_t arena_size = min_t(u64, ARENA_MAX_SIZE, remaining);
  802. remaining -= arena_size;
  803. if (arena_size < ARENA_MIN_SIZE)
  804. break;
  805. arena = alloc_arena(btt, arena_size, btt->nlba, cur_off);
  806. if (!arena) {
  807. free_arenas(btt);
  808. return -ENOMEM;
  809. }
  810. btt->nlba += arena->external_nlba;
  811. if (remaining >= ARENA_MIN_SIZE)
  812. arena->nextoff = arena->size;
  813. else
  814. arena->nextoff = 0;
  815. cur_off += arena_size;
  816. list_add_tail(&arena->list, &btt->arena_list);
  817. }
  818. return 0;
  819. }
  820. /*
  821. * This function completes arena initialization by writing
  822. * all the metadata.
  823. * It is only called for an uninitialized arena when a write
  824. * to that arena occurs for the first time.
  825. */
  826. static int btt_arena_write_layout(struct arena_info *arena)
  827. {
  828. int ret;
  829. u64 sum;
  830. struct btt_sb *super;
  831. struct nd_btt *nd_btt = arena->nd_btt;
  832. const uuid_t *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
  833. ret = btt_map_init(arena);
  834. if (ret)
  835. return ret;
  836. ret = btt_log_init(arena);
  837. if (ret)
  838. return ret;
  839. super = kzalloc_obj(*super, GFP_NOIO);
  840. if (!super)
  841. return -ENOMEM;
  842. strscpy(super->signature, BTT_SIG, sizeof(super->signature));
  843. export_uuid(super->uuid, nd_btt->uuid);
  844. export_uuid(super->parent_uuid, parent_uuid);
  845. super->flags = cpu_to_le32(arena->flags);
  846. super->version_major = cpu_to_le16(arena->version_major);
  847. super->version_minor = cpu_to_le16(arena->version_minor);
  848. super->external_lbasize = cpu_to_le32(arena->external_lbasize);
  849. super->external_nlba = cpu_to_le32(arena->external_nlba);
  850. super->internal_lbasize = cpu_to_le32(arena->internal_lbasize);
  851. super->internal_nlba = cpu_to_le32(arena->internal_nlba);
  852. super->nfree = cpu_to_le32(arena->nfree);
  853. super->infosize = cpu_to_le32(sizeof(struct btt_sb));
  854. super->nextoff = cpu_to_le64(arena->nextoff);
  855. /*
  856. * Subtract arena->infooff (arena start) so numbers are relative
  857. * to 'this' arena
  858. */
  859. super->dataoff = cpu_to_le64(arena->dataoff - arena->infooff);
  860. super->mapoff = cpu_to_le64(arena->mapoff - arena->infooff);
  861. super->logoff = cpu_to_le64(arena->logoff - arena->infooff);
  862. super->info2off = cpu_to_le64(arena->info2off - arena->infooff);
  863. super->flags = 0;
  864. sum = nd_sb_checksum((struct nd_gen_sb *) super);
  865. super->checksum = cpu_to_le64(sum);
  866. ret = btt_info_write(arena, super);
  867. kfree(super);
  868. return ret;
  869. }
  870. /*
  871. * This function completes the initialization for the BTT namespace
  872. * such that it is ready to accept IOs
  873. */
  874. static int btt_meta_init(struct btt *btt)
  875. {
  876. int ret = 0;
  877. struct arena_info *arena;
  878. mutex_lock(&btt->init_lock);
  879. list_for_each_entry(arena, &btt->arena_list, list) {
  880. ret = btt_arena_write_layout(arena);
  881. if (ret)
  882. goto unlock;
  883. ret = btt_freelist_init(arena);
  884. if (ret)
  885. goto unlock;
  886. ret = btt_rtt_init(arena);
  887. if (ret)
  888. goto unlock;
  889. ret = btt_maplocks_init(arena);
  890. if (ret)
  891. goto unlock;
  892. }
  893. btt->init_state = INIT_READY;
  894. unlock:
  895. mutex_unlock(&btt->init_lock);
  896. return ret;
  897. }
  898. static u32 btt_meta_size(struct btt *btt)
  899. {
  900. return btt->lbasize - btt->sector_size;
  901. }
  902. /*
  903. * This function calculates the arena in which the given LBA lies
  904. * by doing a linear walk. This is acceptable since we expect only
  905. * a few arenas. If we have backing devices that get much larger,
  906. * we can construct a balanced binary tree of arenas at init time
  907. * so that this range search becomes faster.
  908. */
  909. static int lba_to_arena(struct btt *btt, sector_t sector, __u32 *premap,
  910. struct arena_info **arena)
  911. {
  912. struct arena_info *arena_list;
  913. __u64 lba = div_u64(sector << SECTOR_SHIFT, btt->sector_size);
  914. list_for_each_entry(arena_list, &btt->arena_list, list) {
  915. if (lba < arena_list->external_nlba) {
  916. *arena = arena_list;
  917. *premap = lba;
  918. return 0;
  919. }
  920. lba -= arena_list->external_nlba;
  921. }
  922. return -EIO;
  923. }
  924. /*
  925. * The following (lock_map, unlock_map) are mostly just to improve
  926. * readability, since they index into an array of locks
  927. */
  928. static void lock_map(struct arena_info *arena, u32 premap)
  929. __acquires(&arena->map_locks[idx].lock)
  930. {
  931. u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  932. spin_lock(&arena->map_locks[idx].lock);
  933. }
  934. static void unlock_map(struct arena_info *arena, u32 premap)
  935. __releases(&arena->map_locks[idx].lock)
  936. {
  937. u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  938. spin_unlock(&arena->map_locks[idx].lock);
  939. }
  940. static int btt_data_read(struct arena_info *arena, struct page *page,
  941. unsigned int off, u32 lba, u32 len)
  942. {
  943. int ret;
  944. u64 nsoff = to_namespace_offset(arena, lba);
  945. void *mem = kmap_local_page(page);
  946. ret = arena_read_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC);
  947. kunmap_local(mem);
  948. return ret;
  949. }
  950. static int btt_data_write(struct arena_info *arena, u32 lba,
  951. struct page *page, unsigned int off, u32 len)
  952. {
  953. int ret;
  954. u64 nsoff = to_namespace_offset(arena, lba);
  955. void *mem = kmap_local_page(page);
  956. ret = arena_write_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC);
  957. kunmap_local(mem);
  958. return ret;
  959. }
  960. static void zero_fill_data(struct page *page, unsigned int off, u32 len)
  961. {
  962. void *mem = kmap_local_page(page);
  963. memset(mem + off, 0, len);
  964. kunmap_local(mem);
  965. }
  966. #ifdef CONFIG_BLK_DEV_INTEGRITY
  967. static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  968. struct arena_info *arena, u32 postmap, int rw)
  969. {
  970. unsigned int len = btt_meta_size(btt);
  971. u64 meta_nsoff;
  972. int ret = 0;
  973. if (bip == NULL)
  974. return 0;
  975. meta_nsoff = to_namespace_offset(arena, postmap) + btt->sector_size;
  976. while (len) {
  977. unsigned int cur_len;
  978. struct bio_vec bv;
  979. void *mem;
  980. bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  981. /*
  982. * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
  983. * .bv_offset already adjusted for iter->bi_bvec_done, and we
  984. * can use those directly
  985. */
  986. cur_len = min(len, bv.bv_len);
  987. mem = bvec_kmap_local(&bv);
  988. if (rw)
  989. ret = arena_write_bytes(arena, meta_nsoff, mem, cur_len,
  990. NVDIMM_IO_ATOMIC);
  991. else
  992. ret = arena_read_bytes(arena, meta_nsoff, mem, cur_len,
  993. NVDIMM_IO_ATOMIC);
  994. kunmap_local(mem);
  995. if (ret)
  996. return ret;
  997. len -= cur_len;
  998. meta_nsoff += cur_len;
  999. if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len))
  1000. return -EIO;
  1001. }
  1002. return ret;
  1003. }
  1004. #else /* CONFIG_BLK_DEV_INTEGRITY */
  1005. static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  1006. struct arena_info *arena, u32 postmap, int rw)
  1007. {
  1008. return 0;
  1009. }
  1010. #endif
  1011. static int btt_read_pg(struct btt *btt, struct bio_integrity_payload *bip,
  1012. struct page *page, unsigned int off, sector_t sector,
  1013. unsigned int len)
  1014. {
  1015. int ret = 0;
  1016. int t_flag, e_flag;
  1017. struct arena_info *arena = NULL;
  1018. u32 lane = 0, premap, postmap;
  1019. while (len) {
  1020. u32 cur_len;
  1021. lane = nd_region_acquire_lane(btt->nd_region);
  1022. ret = lba_to_arena(btt, sector, &premap, &arena);
  1023. if (ret)
  1024. goto out_lane;
  1025. cur_len = min(btt->sector_size, len);
  1026. ret = btt_map_read(arena, premap, &postmap, &t_flag, &e_flag,
  1027. NVDIMM_IO_ATOMIC);
  1028. if (ret)
  1029. goto out_lane;
  1030. /*
  1031. * We loop to make sure that the post map LBA didn't change
  1032. * from under us between writing the RTT and doing the actual
  1033. * read.
  1034. */
  1035. while (1) {
  1036. u32 new_map;
  1037. int new_t, new_e;
  1038. if (t_flag) {
  1039. zero_fill_data(page, off, cur_len);
  1040. goto out_lane;
  1041. }
  1042. if (e_flag) {
  1043. ret = -EIO;
  1044. goto out_lane;
  1045. }
  1046. arena->rtt[lane] = RTT_VALID | postmap;
  1047. /*
  1048. * Barrier to make sure this write is not reordered
  1049. * to do the verification map_read before the RTT store
  1050. */
  1051. barrier();
  1052. ret = btt_map_read(arena, premap, &new_map, &new_t,
  1053. &new_e, NVDIMM_IO_ATOMIC);
  1054. if (ret)
  1055. goto out_rtt;
  1056. if ((postmap == new_map) && (t_flag == new_t) &&
  1057. (e_flag == new_e))
  1058. break;
  1059. postmap = new_map;
  1060. t_flag = new_t;
  1061. e_flag = new_e;
  1062. }
  1063. ret = btt_data_read(arena, page, off, postmap, cur_len);
  1064. if (ret) {
  1065. /* Media error - set the e_flag */
  1066. if (btt_map_write(arena, premap, postmap, 0, 1, NVDIMM_IO_ATOMIC))
  1067. dev_warn_ratelimited(to_dev(arena),
  1068. "Error persistently tracking bad blocks at %#x\n",
  1069. premap);
  1070. goto out_rtt;
  1071. }
  1072. if (bip) {
  1073. ret = btt_rw_integrity(btt, bip, arena, postmap, READ);
  1074. if (ret)
  1075. goto out_rtt;
  1076. }
  1077. arena->rtt[lane] = RTT_INVALID;
  1078. nd_region_release_lane(btt->nd_region, lane);
  1079. len -= cur_len;
  1080. off += cur_len;
  1081. sector += btt->sector_size >> SECTOR_SHIFT;
  1082. }
  1083. return 0;
  1084. out_rtt:
  1085. arena->rtt[lane] = RTT_INVALID;
  1086. out_lane:
  1087. nd_region_release_lane(btt->nd_region, lane);
  1088. return ret;
  1089. }
  1090. /*
  1091. * Normally, arena_{read,write}_bytes will take care of the initial offset
  1092. * adjustment, but in the case of btt_is_badblock, where we query is_bad_pmem,
  1093. * we need the final, raw namespace offset here
  1094. */
  1095. static bool btt_is_badblock(struct btt *btt, struct arena_info *arena,
  1096. u32 postmap)
  1097. {
  1098. u64 nsoff = adjust_initial_offset(arena->nd_btt,
  1099. to_namespace_offset(arena, postmap));
  1100. sector_t phys_sector = nsoff >> 9;
  1101. return is_bad_pmem(btt->phys_bb, phys_sector, arena->internal_lbasize);
  1102. }
  1103. static int btt_write_pg(struct btt *btt, struct bio_integrity_payload *bip,
  1104. sector_t sector, struct page *page, unsigned int off,
  1105. unsigned int len)
  1106. {
  1107. int ret = 0;
  1108. struct arena_info *arena = NULL;
  1109. u32 premap = 0, old_postmap, new_postmap, lane = 0, i;
  1110. struct log_entry log;
  1111. int sub;
  1112. while (len) {
  1113. u32 cur_len;
  1114. int e_flag;
  1115. retry:
  1116. lane = nd_region_acquire_lane(btt->nd_region);
  1117. ret = lba_to_arena(btt, sector, &premap, &arena);
  1118. if (ret)
  1119. goto out_lane;
  1120. cur_len = min(btt->sector_size, len);
  1121. if ((arena->flags & IB_FLAG_ERROR_MASK) != 0) {
  1122. ret = -EIO;
  1123. goto out_lane;
  1124. }
  1125. if (btt_is_badblock(btt, arena, arena->freelist[lane].block))
  1126. arena->freelist[lane].has_err = 1;
  1127. if (mutex_is_locked(&arena->err_lock)
  1128. || arena->freelist[lane].has_err) {
  1129. nd_region_release_lane(btt->nd_region, lane);
  1130. ret = arena_clear_freelist_error(arena, lane);
  1131. if (ret)
  1132. return ret;
  1133. /* OK to acquire a different lane/free block */
  1134. goto retry;
  1135. }
  1136. new_postmap = arena->freelist[lane].block;
  1137. /* Wait if the new block is being read from */
  1138. for (i = 0; i < arena->nfree; i++)
  1139. while (arena->rtt[i] == (RTT_VALID | new_postmap))
  1140. cpu_relax();
  1141. if (new_postmap >= arena->internal_nlba) {
  1142. ret = -EIO;
  1143. goto out_lane;
  1144. }
  1145. ret = btt_data_write(arena, new_postmap, page, off, cur_len);
  1146. if (ret)
  1147. goto out_lane;
  1148. if (bip) {
  1149. ret = btt_rw_integrity(btt, bip, arena, new_postmap,
  1150. WRITE);
  1151. if (ret)
  1152. goto out_lane;
  1153. }
  1154. lock_map(arena, premap);
  1155. ret = btt_map_read(arena, premap, &old_postmap, NULL, &e_flag,
  1156. NVDIMM_IO_ATOMIC);
  1157. if (ret)
  1158. goto out_map;
  1159. if (old_postmap >= arena->internal_nlba) {
  1160. ret = -EIO;
  1161. goto out_map;
  1162. }
  1163. if (e_flag)
  1164. set_e_flag(old_postmap);
  1165. log.lba = cpu_to_le32(premap);
  1166. log.old_map = cpu_to_le32(old_postmap);
  1167. log.new_map = cpu_to_le32(new_postmap);
  1168. log.seq = cpu_to_le32(arena->freelist[lane].seq);
  1169. sub = arena->freelist[lane].sub;
  1170. ret = btt_flog_write(arena, lane, sub, &log);
  1171. if (ret)
  1172. goto out_map;
  1173. ret = btt_map_write(arena, premap, new_postmap, 0, 0,
  1174. NVDIMM_IO_ATOMIC);
  1175. if (ret)
  1176. goto out_map;
  1177. unlock_map(arena, premap);
  1178. nd_region_release_lane(btt->nd_region, lane);
  1179. if (e_flag) {
  1180. ret = arena_clear_freelist_error(arena, lane);
  1181. if (ret)
  1182. return ret;
  1183. }
  1184. len -= cur_len;
  1185. off += cur_len;
  1186. sector += btt->sector_size >> SECTOR_SHIFT;
  1187. }
  1188. return 0;
  1189. out_map:
  1190. unlock_map(arena, premap);
  1191. out_lane:
  1192. nd_region_release_lane(btt->nd_region, lane);
  1193. return ret;
  1194. }
  1195. static int btt_do_bvec(struct btt *btt, struct bio_integrity_payload *bip,
  1196. struct page *page, unsigned int len, unsigned int off,
  1197. enum req_op op, sector_t sector)
  1198. {
  1199. int ret;
  1200. if (!op_is_write(op)) {
  1201. ret = btt_read_pg(btt, bip, page, off, sector, len);
  1202. flush_dcache_page(page);
  1203. } else {
  1204. flush_dcache_page(page);
  1205. ret = btt_write_pg(btt, bip, sector, page, off, len);
  1206. }
  1207. return ret;
  1208. }
  1209. static void btt_submit_bio(struct bio *bio)
  1210. {
  1211. struct bio_integrity_payload *bip = bio_integrity(bio);
  1212. struct btt *btt = bio->bi_bdev->bd_disk->private_data;
  1213. struct bvec_iter iter;
  1214. unsigned long start;
  1215. struct bio_vec bvec;
  1216. int err = 0;
  1217. bool do_acct;
  1218. if (!bio_integrity_prep(bio))
  1219. return;
  1220. do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
  1221. if (do_acct)
  1222. start = bio_start_io_acct(bio);
  1223. bio_for_each_segment(bvec, bio, iter) {
  1224. unsigned int len = bvec.bv_len;
  1225. if (len > PAGE_SIZE || len < btt->sector_size ||
  1226. len % btt->sector_size) {
  1227. dev_err_ratelimited(&btt->nd_btt->dev,
  1228. "unaligned bio segment (len: %d)\n", len);
  1229. bio->bi_status = BLK_STS_IOERR;
  1230. break;
  1231. }
  1232. err = btt_do_bvec(btt, bip, bvec.bv_page, len, bvec.bv_offset,
  1233. bio_op(bio), iter.bi_sector);
  1234. if (err) {
  1235. dev_err(&btt->nd_btt->dev,
  1236. "io error in %s sector %lld, len %d,\n",
  1237. (op_is_write(bio_op(bio))) ? "WRITE" :
  1238. "READ",
  1239. (unsigned long long) iter.bi_sector, len);
  1240. bio->bi_status = errno_to_blk_status(err);
  1241. break;
  1242. }
  1243. }
  1244. if (do_acct)
  1245. bio_end_io_acct(bio, start);
  1246. bio_endio(bio);
  1247. }
  1248. static int btt_getgeo(struct gendisk *disk, struct hd_geometry *geo)
  1249. {
  1250. /* some standard values */
  1251. geo->heads = 1 << 6;
  1252. geo->sectors = 1 << 5;
  1253. geo->cylinders = get_capacity(disk) >> 11;
  1254. return 0;
  1255. }
  1256. static const struct block_device_operations btt_fops = {
  1257. .owner = THIS_MODULE,
  1258. .submit_bio = btt_submit_bio,
  1259. .getgeo = btt_getgeo,
  1260. };
  1261. static int btt_blk_init(struct btt *btt)
  1262. {
  1263. struct nd_btt *nd_btt = btt->nd_btt;
  1264. struct nd_namespace_common *ndns = nd_btt->ndns;
  1265. struct queue_limits lim = {
  1266. .logical_block_size = btt->sector_size,
  1267. .max_hw_sectors = UINT_MAX,
  1268. .max_integrity_segments = 1,
  1269. .features = BLK_FEAT_SYNCHRONOUS,
  1270. };
  1271. int rc;
  1272. if (btt_meta_size(btt) && IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) {
  1273. lim.integrity.metadata_size = btt_meta_size(btt);
  1274. lim.integrity.tag_size = btt_meta_size(btt);
  1275. }
  1276. btt->btt_disk = blk_alloc_disk(&lim, NUMA_NO_NODE);
  1277. if (IS_ERR(btt->btt_disk))
  1278. return PTR_ERR(btt->btt_disk);
  1279. nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
  1280. btt->btt_disk->first_minor = 0;
  1281. btt->btt_disk->fops = &btt_fops;
  1282. btt->btt_disk->private_data = btt;
  1283. set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
  1284. rc = device_add_disk(&btt->nd_btt->dev, btt->btt_disk, NULL);
  1285. if (rc)
  1286. goto out_cleanup_disk;
  1287. btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
  1288. nvdimm_check_and_set_ro(btt->btt_disk);
  1289. return 0;
  1290. out_cleanup_disk:
  1291. put_disk(btt->btt_disk);
  1292. return rc;
  1293. }
  1294. static void btt_blk_cleanup(struct btt *btt)
  1295. {
  1296. del_gendisk(btt->btt_disk);
  1297. put_disk(btt->btt_disk);
  1298. }
  1299. /**
  1300. * btt_init - initialize a block translation table for the given device
  1301. * @nd_btt: device with BTT geometry and backing device info
  1302. * @rawsize: raw size in bytes of the backing device
  1303. * @lbasize: lba size of the backing device
  1304. * @uuid: A uuid for the backing device - this is stored on media
  1305. * @nd_region: &struct nd_region for the REGION device
  1306. *
  1307. * Initialize a Block Translation Table on a backing device to provide
  1308. * single sector power fail atomicity.
  1309. *
  1310. * Context:
  1311. * Might sleep.
  1312. *
  1313. * Returns:
  1314. * Pointer to a new struct btt on success, NULL on failure.
  1315. */
  1316. static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize,
  1317. u32 lbasize, uuid_t *uuid,
  1318. struct nd_region *nd_region)
  1319. {
  1320. int ret;
  1321. struct btt *btt;
  1322. struct nd_namespace_io *nsio;
  1323. struct device *dev = &nd_btt->dev;
  1324. btt = devm_kzalloc(dev, sizeof(struct btt), GFP_KERNEL);
  1325. if (!btt)
  1326. return NULL;
  1327. btt->nd_btt = nd_btt;
  1328. btt->rawsize = rawsize;
  1329. btt->lbasize = lbasize;
  1330. btt->sector_size = ((lbasize >= 4096) ? 4096 : 512);
  1331. INIT_LIST_HEAD(&btt->arena_list);
  1332. mutex_init(&btt->init_lock);
  1333. btt->nd_region = nd_region;
  1334. nsio = to_nd_namespace_io(&nd_btt->ndns->dev);
  1335. btt->phys_bb = &nsio->bb;
  1336. ret = discover_arenas(btt);
  1337. if (ret) {
  1338. dev_err(dev, "init: error in arena_discover: %d\n", ret);
  1339. return NULL;
  1340. }
  1341. if (btt->init_state != INIT_READY && nd_region->ro) {
  1342. dev_warn(dev, "%s is read-only, unable to init btt metadata\n",
  1343. dev_name(&nd_region->dev));
  1344. return NULL;
  1345. } else if (btt->init_state != INIT_READY) {
  1346. btt->num_arenas = (rawsize / ARENA_MAX_SIZE) +
  1347. ((rawsize % ARENA_MAX_SIZE) ? 1 : 0);
  1348. dev_dbg(dev, "init: %d arenas for %llu rawsize\n",
  1349. btt->num_arenas, rawsize);
  1350. ret = create_arenas(btt);
  1351. if (ret) {
  1352. dev_info(dev, "init: create_arenas: %d\n", ret);
  1353. return NULL;
  1354. }
  1355. ret = btt_meta_init(btt);
  1356. if (ret) {
  1357. dev_err(dev, "init: error in meta_init: %d\n", ret);
  1358. return NULL;
  1359. }
  1360. }
  1361. ret = btt_blk_init(btt);
  1362. if (ret) {
  1363. dev_err(dev, "init: error in blk_init: %d\n", ret);
  1364. return NULL;
  1365. }
  1366. btt_debugfs_init(btt);
  1367. return btt;
  1368. }
  1369. /**
  1370. * btt_fini - de-initialize a BTT
  1371. * @btt: the BTT handle that was generated by btt_init
  1372. *
  1373. * De-initialize a Block Translation Table on device removal
  1374. *
  1375. * Context:
  1376. * Might sleep.
  1377. */
  1378. static void btt_fini(struct btt *btt)
  1379. {
  1380. if (btt) {
  1381. btt_blk_cleanup(btt);
  1382. free_arenas(btt);
  1383. debugfs_remove_recursive(btt->debugfs_dir);
  1384. }
  1385. }
  1386. int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
  1387. {
  1388. struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
  1389. struct nd_region *nd_region;
  1390. struct btt_sb *btt_sb;
  1391. struct btt *btt;
  1392. size_t size, rawsize;
  1393. int rc;
  1394. if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize) {
  1395. dev_dbg(&nd_btt->dev, "incomplete btt configuration\n");
  1396. return -ENODEV;
  1397. }
  1398. btt_sb = devm_kzalloc(&nd_btt->dev, sizeof(*btt_sb), GFP_KERNEL);
  1399. if (!btt_sb)
  1400. return -ENOMEM;
  1401. size = nvdimm_namespace_capacity(ndns);
  1402. rc = devm_namespace_enable(&nd_btt->dev, ndns, size);
  1403. if (rc)
  1404. return rc;
  1405. /*
  1406. * If this returns < 0, that is ok as it just means there wasn't
  1407. * an existing BTT, and we're creating a new one. We still need to
  1408. * call this as we need the version dependent fields in nd_btt to be
  1409. * set correctly based on the holder class
  1410. */
  1411. nd_btt_version(nd_btt, ndns, btt_sb);
  1412. rawsize = size - nd_btt->initial_offset;
  1413. if (rawsize < ARENA_MIN_SIZE) {
  1414. dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
  1415. dev_name(&ndns->dev),
  1416. ARENA_MIN_SIZE + nd_btt->initial_offset);
  1417. return -ENXIO;
  1418. }
  1419. nd_region = to_nd_region(nd_btt->dev.parent);
  1420. btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
  1421. nd_region);
  1422. if (!btt)
  1423. return -ENOMEM;
  1424. nd_btt->btt = btt;
  1425. return 0;
  1426. }
  1427. EXPORT_SYMBOL(nvdimm_namespace_attach_btt);
  1428. int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt)
  1429. {
  1430. struct btt *btt = nd_btt->btt;
  1431. btt_fini(btt);
  1432. nd_btt->btt = NULL;
  1433. return 0;
  1434. }
  1435. EXPORT_SYMBOL(nvdimm_namespace_detach_btt);
  1436. static int __init nd_btt_init(void)
  1437. {
  1438. int rc = 0;
  1439. debugfs_root = debugfs_create_dir("btt", NULL);
  1440. if (IS_ERR_OR_NULL(debugfs_root))
  1441. rc = -ENXIO;
  1442. return rc;
  1443. }
  1444. static void __exit nd_btt_exit(void)
  1445. {
  1446. debugfs_remove_recursive(debugfs_root);
  1447. }
  1448. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT);
  1449. MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>");
  1450. MODULE_DESCRIPTION("NVDIMM Block Translation Table");
  1451. MODULE_LICENSE("GPL v2");
  1452. module_init(nd_btt_init);
  1453. module_exit(nd_btt_exit);