btt.c 43 KB

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