btt.c 35 KB

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