btt.c 43 KB

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