btt.c 34 KB

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