btt.c 43 KB

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