btt.c 43 KB

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