btt.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. btt_do_bvec(btt, NULL, page, PAGE_SIZE, 0, is_write, sector);
  1006. page_endio(page, is_write, 0);
  1007. return 0;
  1008. }
  1009. static int btt_getgeo(struct block_device *bd, struct hd_geometry *geo)
  1010. {
  1011. /* some standard values */
  1012. geo->heads = 1 << 6;
  1013. geo->sectors = 1 << 5;
  1014. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  1015. return 0;
  1016. }
  1017. static const struct block_device_operations btt_fops = {
  1018. .owner = THIS_MODULE,
  1019. .rw_page = btt_rw_page,
  1020. .getgeo = btt_getgeo,
  1021. .revalidate_disk = nvdimm_revalidate_disk,
  1022. };
  1023. static int btt_blk_init(struct btt *btt)
  1024. {
  1025. struct nd_btt *nd_btt = btt->nd_btt;
  1026. struct nd_namespace_common *ndns = nd_btt->ndns;
  1027. /* create a new disk and request queue for btt */
  1028. btt->btt_queue = blk_alloc_queue(GFP_KERNEL);
  1029. if (!btt->btt_queue)
  1030. return -ENOMEM;
  1031. btt->btt_disk = alloc_disk(0);
  1032. if (!btt->btt_disk) {
  1033. blk_cleanup_queue(btt->btt_queue);
  1034. return -ENOMEM;
  1035. }
  1036. nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
  1037. btt->btt_disk->first_minor = 0;
  1038. btt->btt_disk->fops = &btt_fops;
  1039. btt->btt_disk->private_data = btt;
  1040. btt->btt_disk->queue = btt->btt_queue;
  1041. btt->btt_disk->flags = GENHD_FL_EXT_DEVT;
  1042. blk_queue_make_request(btt->btt_queue, btt_make_request);
  1043. blk_queue_logical_block_size(btt->btt_queue, btt->sector_size);
  1044. blk_queue_max_hw_sectors(btt->btt_queue, UINT_MAX);
  1045. blk_queue_bounce_limit(btt->btt_queue, BLK_BOUNCE_ANY);
  1046. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, btt->btt_queue);
  1047. btt->btt_queue->queuedata = btt;
  1048. set_capacity(btt->btt_disk, 0);
  1049. device_add_disk(&btt->nd_btt->dev, btt->btt_disk);
  1050. if (btt_meta_size(btt)) {
  1051. int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
  1052. if (rc) {
  1053. del_gendisk(btt->btt_disk);
  1054. put_disk(btt->btt_disk);
  1055. blk_cleanup_queue(btt->btt_queue);
  1056. return rc;
  1057. }
  1058. }
  1059. set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
  1060. revalidate_disk(btt->btt_disk);
  1061. return 0;
  1062. }
  1063. static void btt_blk_cleanup(struct btt *btt)
  1064. {
  1065. del_gendisk(btt->btt_disk);
  1066. put_disk(btt->btt_disk);
  1067. blk_cleanup_queue(btt->btt_queue);
  1068. }
  1069. /**
  1070. * btt_init - initialize a block translation table for the given device
  1071. * @nd_btt: device with BTT geometry and backing device info
  1072. * @rawsize: raw size in bytes of the backing device
  1073. * @lbasize: lba size of the backing device
  1074. * @uuid: A uuid for the backing device - this is stored on media
  1075. * @maxlane: maximum number of parallel requests the device can handle
  1076. *
  1077. * Initialize a Block Translation Table on a backing device to provide
  1078. * single sector power fail atomicity.
  1079. *
  1080. * Context:
  1081. * Might sleep.
  1082. *
  1083. * Returns:
  1084. * Pointer to a new struct btt on success, NULL on failure.
  1085. */
  1086. static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize,
  1087. u32 lbasize, u8 *uuid, struct nd_region *nd_region)
  1088. {
  1089. int ret;
  1090. struct btt *btt;
  1091. struct device *dev = &nd_btt->dev;
  1092. btt = devm_kzalloc(dev, sizeof(struct btt), GFP_KERNEL);
  1093. if (!btt)
  1094. return NULL;
  1095. btt->nd_btt = nd_btt;
  1096. btt->rawsize = rawsize;
  1097. btt->lbasize = lbasize;
  1098. btt->sector_size = ((lbasize >= 4096) ? 4096 : 512);
  1099. INIT_LIST_HEAD(&btt->arena_list);
  1100. mutex_init(&btt->init_lock);
  1101. btt->nd_region = nd_region;
  1102. ret = discover_arenas(btt);
  1103. if (ret) {
  1104. dev_err(dev, "init: error in arena_discover: %d\n", ret);
  1105. return NULL;
  1106. }
  1107. if (btt->init_state != INIT_READY && nd_region->ro) {
  1108. dev_info(dev, "%s is read-only, unable to init btt metadata\n",
  1109. dev_name(&nd_region->dev));
  1110. return NULL;
  1111. } else if (btt->init_state != INIT_READY) {
  1112. btt->num_arenas = (rawsize / ARENA_MAX_SIZE) +
  1113. ((rawsize % ARENA_MAX_SIZE) ? 1 : 0);
  1114. dev_dbg(dev, "init: %d arenas for %llu rawsize\n",
  1115. btt->num_arenas, rawsize);
  1116. ret = create_arenas(btt);
  1117. if (ret) {
  1118. dev_info(dev, "init: create_arenas: %d\n", ret);
  1119. return NULL;
  1120. }
  1121. ret = btt_meta_init(btt);
  1122. if (ret) {
  1123. dev_err(dev, "init: error in meta_init: %d\n", ret);
  1124. return NULL;
  1125. }
  1126. }
  1127. ret = btt_blk_init(btt);
  1128. if (ret) {
  1129. dev_err(dev, "init: error in blk_init: %d\n", ret);
  1130. return NULL;
  1131. }
  1132. btt_debugfs_init(btt);
  1133. return btt;
  1134. }
  1135. /**
  1136. * btt_fini - de-initialize a BTT
  1137. * @btt: the BTT handle that was generated by btt_init
  1138. *
  1139. * De-initialize a Block Translation Table on device removal
  1140. *
  1141. * Context:
  1142. * Might sleep.
  1143. */
  1144. static void btt_fini(struct btt *btt)
  1145. {
  1146. if (btt) {
  1147. btt_blk_cleanup(btt);
  1148. free_arenas(btt);
  1149. debugfs_remove_recursive(btt->debugfs_dir);
  1150. }
  1151. }
  1152. int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
  1153. {
  1154. struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
  1155. struct nd_region *nd_region;
  1156. struct btt *btt;
  1157. size_t rawsize;
  1158. if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize) {
  1159. dev_dbg(&nd_btt->dev, "incomplete btt configuration\n");
  1160. return -ENODEV;
  1161. }
  1162. rawsize = nvdimm_namespace_capacity(ndns) - SZ_4K;
  1163. if (rawsize < ARENA_MIN_SIZE) {
  1164. dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
  1165. dev_name(&ndns->dev), ARENA_MIN_SIZE + SZ_4K);
  1166. return -ENXIO;
  1167. }
  1168. nd_region = to_nd_region(nd_btt->dev.parent);
  1169. btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
  1170. nd_region);
  1171. if (!btt)
  1172. return -ENOMEM;
  1173. nd_btt->btt = btt;
  1174. return 0;
  1175. }
  1176. EXPORT_SYMBOL(nvdimm_namespace_attach_btt);
  1177. int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt)
  1178. {
  1179. struct btt *btt = nd_btt->btt;
  1180. btt_fini(btt);
  1181. nd_btt->btt = NULL;
  1182. return 0;
  1183. }
  1184. EXPORT_SYMBOL(nvdimm_namespace_detach_btt);
  1185. static int __init nd_btt_init(void)
  1186. {
  1187. int rc = 0;
  1188. debugfs_root = debugfs_create_dir("btt", NULL);
  1189. if (IS_ERR_OR_NULL(debugfs_root))
  1190. rc = -ENXIO;
  1191. return rc;
  1192. }
  1193. static void __exit nd_btt_exit(void)
  1194. {
  1195. debugfs_remove_recursive(debugfs_root);
  1196. }
  1197. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT);
  1198. MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>");
  1199. MODULE_LICENSE("GPL v2");
  1200. module_init(nd_btt_init);
  1201. module_exit(nd_btt_exit);