btt.c 39 KB

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