btt.c 40 KB

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