btt.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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 checks if the metadata layout is valid and error free
  496. */
  497. static int arena_is_valid(struct arena_info *arena, struct btt_sb *super,
  498. u8 *uuid, u32 lbasize)
  499. {
  500. u64 checksum;
  501. if (memcmp(super->uuid, uuid, 16))
  502. return 0;
  503. checksum = le64_to_cpu(super->checksum);
  504. super->checksum = 0;
  505. if (checksum != nd_btt_sb_checksum(super))
  506. return 0;
  507. super->checksum = cpu_to_le64(checksum);
  508. if (lbasize != le32_to_cpu(super->external_lbasize))
  509. return 0;
  510. /* TODO: figure out action for this */
  511. if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
  512. dev_info(to_dev(arena), "Found arena with an error flag\n");
  513. return 1;
  514. }
  515. /*
  516. * This function reads an existing valid btt superblock and
  517. * populates the corresponding arena_info struct
  518. */
  519. static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super,
  520. u64 arena_off)
  521. {
  522. arena->internal_nlba = le32_to_cpu(super->internal_nlba);
  523. arena->internal_lbasize = le32_to_cpu(super->internal_lbasize);
  524. arena->external_nlba = le32_to_cpu(super->external_nlba);
  525. arena->external_lbasize = le32_to_cpu(super->external_lbasize);
  526. arena->nfree = le32_to_cpu(super->nfree);
  527. arena->version_major = le16_to_cpu(super->version_major);
  528. arena->version_minor = le16_to_cpu(super->version_minor);
  529. arena->nextoff = (super->nextoff == 0) ? 0 : (arena_off +
  530. le64_to_cpu(super->nextoff));
  531. arena->infooff = arena_off;
  532. arena->dataoff = arena_off + le64_to_cpu(super->dataoff);
  533. arena->mapoff = arena_off + le64_to_cpu(super->mapoff);
  534. arena->logoff = arena_off + le64_to_cpu(super->logoff);
  535. arena->info2off = arena_off + le64_to_cpu(super->info2off);
  536. arena->size = (super->nextoff > 0) ? (le64_to_cpu(super->nextoff)) :
  537. (arena->info2off - arena->infooff + BTT_PG_SIZE);
  538. arena->flags = le32_to_cpu(super->flags);
  539. }
  540. static int discover_arenas(struct btt *btt)
  541. {
  542. int ret = 0;
  543. struct arena_info *arena;
  544. struct btt_sb *super;
  545. size_t remaining = btt->rawsize;
  546. u64 cur_nlba = 0;
  547. size_t cur_off = 0;
  548. int num_arenas = 0;
  549. super = kzalloc(sizeof(*super), GFP_KERNEL);
  550. if (!super)
  551. return -ENOMEM;
  552. while (remaining) {
  553. /* Alloc memory for arena */
  554. arena = alloc_arena(btt, 0, 0, 0);
  555. if (!arena) {
  556. ret = -ENOMEM;
  557. goto out_super;
  558. }
  559. arena->infooff = cur_off;
  560. ret = btt_info_read(arena, super);
  561. if (ret)
  562. goto out;
  563. if (!arena_is_valid(arena, super, btt->nd_btt->uuid,
  564. btt->lbasize)) {
  565. if (remaining == btt->rawsize) {
  566. btt->init_state = INIT_NOTFOUND;
  567. dev_info(to_dev(arena), "No existing arenas\n");
  568. goto out;
  569. } else {
  570. dev_info(to_dev(arena),
  571. "Found corrupted metadata!\n");
  572. ret = -ENODEV;
  573. goto out;
  574. }
  575. }
  576. arena->external_lba_start = cur_nlba;
  577. parse_arena_meta(arena, super, cur_off);
  578. ret = btt_freelist_init(arena);
  579. if (ret)
  580. goto out;
  581. ret = btt_rtt_init(arena);
  582. if (ret)
  583. goto out;
  584. ret = btt_maplocks_init(arena);
  585. if (ret)
  586. goto out;
  587. list_add_tail(&arena->list, &btt->arena_list);
  588. remaining -= arena->size;
  589. cur_off += arena->size;
  590. cur_nlba += arena->external_nlba;
  591. num_arenas++;
  592. if (arena->nextoff == 0)
  593. break;
  594. }
  595. btt->num_arenas = num_arenas;
  596. btt->nlba = cur_nlba;
  597. btt->init_state = INIT_READY;
  598. kfree(super);
  599. return ret;
  600. out:
  601. kfree(arena);
  602. free_arenas(btt);
  603. out_super:
  604. kfree(super);
  605. return ret;
  606. }
  607. static int create_arenas(struct btt *btt)
  608. {
  609. size_t remaining = btt->rawsize;
  610. size_t cur_off = 0;
  611. while (remaining) {
  612. struct arena_info *arena;
  613. size_t arena_size = min_t(u64, ARENA_MAX_SIZE, remaining);
  614. remaining -= arena_size;
  615. if (arena_size < ARENA_MIN_SIZE)
  616. break;
  617. arena = alloc_arena(btt, arena_size, btt->nlba, cur_off);
  618. if (!arena) {
  619. free_arenas(btt);
  620. return -ENOMEM;
  621. }
  622. btt->nlba += arena->external_nlba;
  623. if (remaining >= ARENA_MIN_SIZE)
  624. arena->nextoff = arena->size;
  625. else
  626. arena->nextoff = 0;
  627. cur_off += arena_size;
  628. list_add_tail(&arena->list, &btt->arena_list);
  629. }
  630. return 0;
  631. }
  632. /*
  633. * This function completes arena initialization by writing
  634. * all the metadata.
  635. * It is only called for an uninitialized arena when a write
  636. * to that arena occurs for the first time.
  637. */
  638. static int btt_arena_write_layout(struct arena_info *arena, u8 *uuid)
  639. {
  640. int ret;
  641. struct btt_sb *super;
  642. ret = btt_map_init(arena);
  643. if (ret)
  644. return ret;
  645. ret = btt_log_init(arena);
  646. if (ret)
  647. return ret;
  648. super = kzalloc(sizeof(struct btt_sb), GFP_NOIO);
  649. if (!super)
  650. return -ENOMEM;
  651. strncpy(super->signature, BTT_SIG, BTT_SIG_LEN);
  652. memcpy(super->uuid, uuid, 16);
  653. super->flags = cpu_to_le32(arena->flags);
  654. super->version_major = cpu_to_le16(arena->version_major);
  655. super->version_minor = cpu_to_le16(arena->version_minor);
  656. super->external_lbasize = cpu_to_le32(arena->external_lbasize);
  657. super->external_nlba = cpu_to_le32(arena->external_nlba);
  658. super->internal_lbasize = cpu_to_le32(arena->internal_lbasize);
  659. super->internal_nlba = cpu_to_le32(arena->internal_nlba);
  660. super->nfree = cpu_to_le32(arena->nfree);
  661. super->infosize = cpu_to_le32(sizeof(struct btt_sb));
  662. super->nextoff = cpu_to_le64(arena->nextoff);
  663. /*
  664. * Subtract arena->infooff (arena start) so numbers are relative
  665. * to 'this' arena
  666. */
  667. super->dataoff = cpu_to_le64(arena->dataoff - arena->infooff);
  668. super->mapoff = cpu_to_le64(arena->mapoff - arena->infooff);
  669. super->logoff = cpu_to_le64(arena->logoff - arena->infooff);
  670. super->info2off = cpu_to_le64(arena->info2off - arena->infooff);
  671. super->flags = 0;
  672. super->checksum = cpu_to_le64(nd_btt_sb_checksum(super));
  673. ret = btt_info_write(arena, super);
  674. kfree(super);
  675. return ret;
  676. }
  677. /*
  678. * This function completes the initialization for the BTT namespace
  679. * such that it is ready to accept IOs
  680. */
  681. static int btt_meta_init(struct btt *btt)
  682. {
  683. int ret = 0;
  684. struct arena_info *arena;
  685. mutex_lock(&btt->init_lock);
  686. list_for_each_entry(arena, &btt->arena_list, list) {
  687. ret = btt_arena_write_layout(arena, btt->nd_btt->uuid);
  688. if (ret)
  689. goto unlock;
  690. ret = btt_freelist_init(arena);
  691. if (ret)
  692. goto unlock;
  693. ret = btt_rtt_init(arena);
  694. if (ret)
  695. goto unlock;
  696. ret = btt_maplocks_init(arena);
  697. if (ret)
  698. goto unlock;
  699. }
  700. btt->init_state = INIT_READY;
  701. unlock:
  702. mutex_unlock(&btt->init_lock);
  703. return ret;
  704. }
  705. static u32 btt_meta_size(struct btt *btt)
  706. {
  707. return btt->lbasize - btt->sector_size;
  708. }
  709. /*
  710. * This function calculates the arena in which the given LBA lies
  711. * by doing a linear walk. This is acceptable since we expect only
  712. * a few arenas. If we have backing devices that get much larger,
  713. * we can construct a balanced binary tree of arenas at init time
  714. * so that this range search becomes faster.
  715. */
  716. static int lba_to_arena(struct btt *btt, sector_t sector, __u32 *premap,
  717. struct arena_info **arena)
  718. {
  719. struct arena_info *arena_list;
  720. __u64 lba = div_u64(sector << SECTOR_SHIFT, btt->sector_size);
  721. list_for_each_entry(arena_list, &btt->arena_list, list) {
  722. if (lba < arena_list->external_nlba) {
  723. *arena = arena_list;
  724. *premap = lba;
  725. return 0;
  726. }
  727. lba -= arena_list->external_nlba;
  728. }
  729. return -EIO;
  730. }
  731. /*
  732. * The following (lock_map, unlock_map) are mostly just to improve
  733. * readability, since they index into an array of locks
  734. */
  735. static void lock_map(struct arena_info *arena, u32 premap)
  736. __acquires(&arena->map_locks[idx].lock)
  737. {
  738. u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  739. spin_lock(&arena->map_locks[idx].lock);
  740. }
  741. static void unlock_map(struct arena_info *arena, u32 premap)
  742. __releases(&arena->map_locks[idx].lock)
  743. {
  744. u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  745. spin_unlock(&arena->map_locks[idx].lock);
  746. }
  747. static u64 to_namespace_offset(struct arena_info *arena, u64 lba)
  748. {
  749. return arena->dataoff + ((u64)lba * arena->internal_lbasize);
  750. }
  751. static int btt_data_read(struct arena_info *arena, struct page *page,
  752. unsigned int off, u32 lba, u32 len)
  753. {
  754. int ret;
  755. u64 nsoff = to_namespace_offset(arena, lba);
  756. void *mem = kmap_atomic(page);
  757. ret = arena_read_bytes(arena, nsoff, mem + off, len);
  758. kunmap_atomic(mem);
  759. return ret;
  760. }
  761. static int btt_data_write(struct arena_info *arena, u32 lba,
  762. struct page *page, unsigned int off, u32 len)
  763. {
  764. int ret;
  765. u64 nsoff = to_namespace_offset(arena, lba);
  766. void *mem = kmap_atomic(page);
  767. ret = arena_write_bytes(arena, nsoff, mem + off, len);
  768. kunmap_atomic(mem);
  769. return ret;
  770. }
  771. static void zero_fill_data(struct page *page, unsigned int off, u32 len)
  772. {
  773. void *mem = kmap_atomic(page);
  774. memset(mem + off, 0, len);
  775. kunmap_atomic(mem);
  776. }
  777. #ifdef CONFIG_BLK_DEV_INTEGRITY
  778. static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  779. struct arena_info *arena, u32 postmap, int rw)
  780. {
  781. unsigned int len = btt_meta_size(btt);
  782. u64 meta_nsoff;
  783. int ret = 0;
  784. if (bip == NULL)
  785. return 0;
  786. meta_nsoff = to_namespace_offset(arena, postmap) + btt->sector_size;
  787. while (len) {
  788. unsigned int cur_len;
  789. struct bio_vec bv;
  790. void *mem;
  791. bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  792. /*
  793. * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
  794. * .bv_offset already adjusted for iter->bi_bvec_done, and we
  795. * can use those directly
  796. */
  797. cur_len = min(len, bv.bv_len);
  798. mem = kmap_atomic(bv.bv_page);
  799. if (rw)
  800. ret = arena_write_bytes(arena, meta_nsoff,
  801. mem + bv.bv_offset, cur_len);
  802. else
  803. ret = arena_read_bytes(arena, meta_nsoff,
  804. mem + bv.bv_offset, cur_len);
  805. kunmap_atomic(mem);
  806. if (ret)
  807. return ret;
  808. len -= cur_len;
  809. meta_nsoff += cur_len;
  810. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len);
  811. }
  812. return ret;
  813. }
  814. #else /* CONFIG_BLK_DEV_INTEGRITY */
  815. static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  816. struct arena_info *arena, u32 postmap, int rw)
  817. {
  818. return 0;
  819. }
  820. #endif
  821. static int btt_read_pg(struct btt *btt, struct bio_integrity_payload *bip,
  822. struct page *page, unsigned int off, sector_t sector,
  823. unsigned int len)
  824. {
  825. int ret = 0;
  826. int t_flag, e_flag;
  827. struct arena_info *arena = NULL;
  828. u32 lane = 0, premap, postmap;
  829. while (len) {
  830. u32 cur_len;
  831. lane = nd_region_acquire_lane(btt->nd_region);
  832. ret = lba_to_arena(btt, sector, &premap, &arena);
  833. if (ret)
  834. goto out_lane;
  835. cur_len = min(btt->sector_size, len);
  836. ret = btt_map_read(arena, premap, &postmap, &t_flag, &e_flag);
  837. if (ret)
  838. goto out_lane;
  839. /*
  840. * We loop to make sure that the post map LBA didn't change
  841. * from under us between writing the RTT and doing the actual
  842. * read.
  843. */
  844. while (1) {
  845. u32 new_map;
  846. if (t_flag) {
  847. zero_fill_data(page, off, cur_len);
  848. goto out_lane;
  849. }
  850. if (e_flag) {
  851. ret = -EIO;
  852. goto out_lane;
  853. }
  854. arena->rtt[lane] = RTT_VALID | postmap;
  855. /*
  856. * Barrier to make sure this write is not reordered
  857. * to do the verification map_read before the RTT store
  858. */
  859. barrier();
  860. ret = btt_map_read(arena, premap, &new_map, &t_flag,
  861. &e_flag);
  862. if (ret)
  863. goto out_rtt;
  864. if (postmap == new_map)
  865. break;
  866. postmap = new_map;
  867. }
  868. ret = btt_data_read(arena, page, off, postmap, cur_len);
  869. if (ret)
  870. goto out_rtt;
  871. if (bip) {
  872. ret = btt_rw_integrity(btt, bip, arena, postmap, READ);
  873. if (ret)
  874. goto out_rtt;
  875. }
  876. arena->rtt[lane] = RTT_INVALID;
  877. nd_region_release_lane(btt->nd_region, lane);
  878. len -= cur_len;
  879. off += cur_len;
  880. sector += btt->sector_size >> SECTOR_SHIFT;
  881. }
  882. return 0;
  883. out_rtt:
  884. arena->rtt[lane] = RTT_INVALID;
  885. out_lane:
  886. nd_region_release_lane(btt->nd_region, lane);
  887. return ret;
  888. }
  889. static int btt_write_pg(struct btt *btt, struct bio_integrity_payload *bip,
  890. sector_t sector, struct page *page, unsigned int off,
  891. unsigned int len)
  892. {
  893. int ret = 0;
  894. struct arena_info *arena = NULL;
  895. u32 premap = 0, old_postmap, new_postmap, lane = 0, i;
  896. struct log_entry log;
  897. int sub;
  898. while (len) {
  899. u32 cur_len;
  900. lane = nd_region_acquire_lane(btt->nd_region);
  901. ret = lba_to_arena(btt, sector, &premap, &arena);
  902. if (ret)
  903. goto out_lane;
  904. cur_len = min(btt->sector_size, len);
  905. if ((arena->flags & IB_FLAG_ERROR_MASK) != 0) {
  906. ret = -EIO;
  907. goto out_lane;
  908. }
  909. new_postmap = arena->freelist[lane].block;
  910. /* Wait if the new block is being read from */
  911. for (i = 0; i < arena->nfree; i++)
  912. while (arena->rtt[i] == (RTT_VALID | new_postmap))
  913. cpu_relax();
  914. if (new_postmap >= arena->internal_nlba) {
  915. ret = -EIO;
  916. goto out_lane;
  917. }
  918. ret = btt_data_write(arena, new_postmap, page, off, cur_len);
  919. if (ret)
  920. goto out_lane;
  921. if (bip) {
  922. ret = btt_rw_integrity(btt, bip, arena, new_postmap,
  923. WRITE);
  924. if (ret)
  925. goto out_lane;
  926. }
  927. lock_map(arena, premap);
  928. ret = btt_map_read(arena, premap, &old_postmap, NULL, NULL);
  929. if (ret)
  930. goto out_map;
  931. if (old_postmap >= arena->internal_nlba) {
  932. ret = -EIO;
  933. goto out_map;
  934. }
  935. log.lba = cpu_to_le32(premap);
  936. log.old_map = cpu_to_le32(old_postmap);
  937. log.new_map = cpu_to_le32(new_postmap);
  938. log.seq = cpu_to_le32(arena->freelist[lane].seq);
  939. sub = arena->freelist[lane].sub;
  940. ret = btt_flog_write(arena, lane, sub, &log);
  941. if (ret)
  942. goto out_map;
  943. ret = btt_map_write(arena, premap, new_postmap, 0, 0);
  944. if (ret)
  945. goto out_map;
  946. unlock_map(arena, premap);
  947. nd_region_release_lane(btt->nd_region, lane);
  948. len -= cur_len;
  949. off += cur_len;
  950. sector += btt->sector_size >> SECTOR_SHIFT;
  951. }
  952. return 0;
  953. out_map:
  954. unlock_map(arena, premap);
  955. out_lane:
  956. nd_region_release_lane(btt->nd_region, lane);
  957. return ret;
  958. }
  959. static int btt_do_bvec(struct btt *btt, struct bio_integrity_payload *bip,
  960. struct page *page, unsigned int len, unsigned int off,
  961. int rw, sector_t sector)
  962. {
  963. int ret;
  964. if (rw == READ) {
  965. ret = btt_read_pg(btt, bip, page, off, sector, len);
  966. flush_dcache_page(page);
  967. } else {
  968. flush_dcache_page(page);
  969. ret = btt_write_pg(btt, bip, sector, page, off, len);
  970. }
  971. return ret;
  972. }
  973. static void btt_make_request(struct request_queue *q, struct bio *bio)
  974. {
  975. struct bio_integrity_payload *bip = bio_integrity(bio);
  976. struct btt *btt = q->queuedata;
  977. struct bvec_iter iter;
  978. unsigned long start;
  979. struct bio_vec bvec;
  980. int err = 0, rw;
  981. bool do_acct;
  982. /*
  983. * bio_integrity_enabled also checks if the bio already has an
  984. * integrity payload attached. If it does, we *don't* do a
  985. * bio_integrity_prep here - the payload has been generated by
  986. * another kernel subsystem, and we just pass it through.
  987. */
  988. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  989. err = -EIO;
  990. goto out;
  991. }
  992. do_acct = nd_iostat_start(bio, &start);
  993. rw = bio_data_dir(bio);
  994. bio_for_each_segment(bvec, bio, iter) {
  995. unsigned int len = bvec.bv_len;
  996. BUG_ON(len > PAGE_SIZE);
  997. /* Make sure len is in multiples of sector size. */
  998. /* XXX is this right? */
  999. BUG_ON(len < btt->sector_size);
  1000. BUG_ON(len % btt->sector_size);
  1001. err = btt_do_bvec(btt, bip, bvec.bv_page, len, bvec.bv_offset,
  1002. rw, iter.bi_sector);
  1003. if (err) {
  1004. dev_info(&btt->nd_btt->dev,
  1005. "io error in %s sector %lld, len %d,\n",
  1006. (rw == READ) ? "READ" : "WRITE",
  1007. (unsigned long long) iter.bi_sector, len);
  1008. break;
  1009. }
  1010. }
  1011. if (do_acct)
  1012. nd_iostat_end(bio, start);
  1013. out:
  1014. bio_endio(bio, err);
  1015. }
  1016. static int btt_rw_page(struct block_device *bdev, sector_t sector,
  1017. struct page *page, int rw)
  1018. {
  1019. struct btt *btt = bdev->bd_disk->private_data;
  1020. btt_do_bvec(btt, NULL, page, PAGE_CACHE_SIZE, 0, rw, sector);
  1021. page_endio(page, rw & WRITE, 0);
  1022. return 0;
  1023. }
  1024. static int btt_getgeo(struct block_device *bd, struct hd_geometry *geo)
  1025. {
  1026. /* some standard values */
  1027. geo->heads = 1 << 6;
  1028. geo->sectors = 1 << 5;
  1029. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  1030. return 0;
  1031. }
  1032. static const struct block_device_operations btt_fops = {
  1033. .owner = THIS_MODULE,
  1034. .rw_page = btt_rw_page,
  1035. .getgeo = btt_getgeo,
  1036. .revalidate_disk = nvdimm_revalidate_disk,
  1037. };
  1038. static int btt_blk_init(struct btt *btt)
  1039. {
  1040. struct nd_btt *nd_btt = btt->nd_btt;
  1041. struct nd_namespace_common *ndns = nd_btt->ndns;
  1042. /* create a new disk and request queue for btt */
  1043. btt->btt_queue = blk_alloc_queue(GFP_KERNEL);
  1044. if (!btt->btt_queue)
  1045. return -ENOMEM;
  1046. btt->btt_disk = alloc_disk(0);
  1047. if (!btt->btt_disk) {
  1048. blk_cleanup_queue(btt->btt_queue);
  1049. return -ENOMEM;
  1050. }
  1051. nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
  1052. btt->btt_disk->driverfs_dev = &btt->nd_btt->dev;
  1053. btt->btt_disk->major = btt_major;
  1054. btt->btt_disk->first_minor = 0;
  1055. btt->btt_disk->fops = &btt_fops;
  1056. btt->btt_disk->private_data = btt;
  1057. btt->btt_disk->queue = btt->btt_queue;
  1058. btt->btt_disk->flags = GENHD_FL_EXT_DEVT;
  1059. blk_queue_make_request(btt->btt_queue, btt_make_request);
  1060. blk_queue_logical_block_size(btt->btt_queue, btt->sector_size);
  1061. blk_queue_max_hw_sectors(btt->btt_queue, UINT_MAX);
  1062. blk_queue_bounce_limit(btt->btt_queue, BLK_BOUNCE_ANY);
  1063. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, btt->btt_queue);
  1064. btt->btt_queue->queuedata = btt;
  1065. set_capacity(btt->btt_disk, 0);
  1066. add_disk(btt->btt_disk);
  1067. if (btt_meta_size(btt)) {
  1068. int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
  1069. if (rc) {
  1070. del_gendisk(btt->btt_disk);
  1071. put_disk(btt->btt_disk);
  1072. blk_cleanup_queue(btt->btt_queue);
  1073. return rc;
  1074. }
  1075. }
  1076. set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
  1077. revalidate_disk(btt->btt_disk);
  1078. return 0;
  1079. }
  1080. static void btt_blk_cleanup(struct btt *btt)
  1081. {
  1082. blk_integrity_unregister(btt->btt_disk);
  1083. del_gendisk(btt->btt_disk);
  1084. put_disk(btt->btt_disk);
  1085. blk_cleanup_queue(btt->btt_queue);
  1086. }
  1087. /**
  1088. * btt_init - initialize a block translation table for the given device
  1089. * @nd_btt: device with BTT geometry and backing device info
  1090. * @rawsize: raw size in bytes of the backing device
  1091. * @lbasize: lba size of the backing device
  1092. * @uuid: A uuid for the backing device - this is stored on media
  1093. * @maxlane: maximum number of parallel requests the device can handle
  1094. *
  1095. * Initialize a Block Translation Table on a backing device to provide
  1096. * single sector power fail atomicity.
  1097. *
  1098. * Context:
  1099. * Might sleep.
  1100. *
  1101. * Returns:
  1102. * Pointer to a new struct btt on success, NULL on failure.
  1103. */
  1104. static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize,
  1105. u32 lbasize, u8 *uuid, struct nd_region *nd_region)
  1106. {
  1107. int ret;
  1108. struct btt *btt;
  1109. struct device *dev = &nd_btt->dev;
  1110. btt = kzalloc(sizeof(struct btt), GFP_KERNEL);
  1111. if (!btt)
  1112. return NULL;
  1113. btt->nd_btt = nd_btt;
  1114. btt->rawsize = rawsize;
  1115. btt->lbasize = lbasize;
  1116. btt->sector_size = ((lbasize >= 4096) ? 4096 : 512);
  1117. INIT_LIST_HEAD(&btt->arena_list);
  1118. mutex_init(&btt->init_lock);
  1119. btt->nd_region = nd_region;
  1120. ret = discover_arenas(btt);
  1121. if (ret) {
  1122. dev_err(dev, "init: error in arena_discover: %d\n", ret);
  1123. goto out_free;
  1124. }
  1125. if (btt->init_state != INIT_READY && nd_region->ro) {
  1126. dev_info(dev, "%s is read-only, unable to init btt metadata\n",
  1127. dev_name(&nd_region->dev));
  1128. goto out_free;
  1129. } else if (btt->init_state != INIT_READY) {
  1130. btt->num_arenas = (rawsize / ARENA_MAX_SIZE) +
  1131. ((rawsize % ARENA_MAX_SIZE) ? 1 : 0);
  1132. dev_dbg(dev, "init: %d arenas for %llu rawsize\n",
  1133. btt->num_arenas, rawsize);
  1134. ret = create_arenas(btt);
  1135. if (ret) {
  1136. dev_info(dev, "init: create_arenas: %d\n", ret);
  1137. goto out_free;
  1138. }
  1139. ret = btt_meta_init(btt);
  1140. if (ret) {
  1141. dev_err(dev, "init: error in meta_init: %d\n", ret);
  1142. goto out_free;
  1143. }
  1144. }
  1145. ret = btt_blk_init(btt);
  1146. if (ret) {
  1147. dev_err(dev, "init: error in blk_init: %d\n", ret);
  1148. goto out_free;
  1149. }
  1150. btt_debugfs_init(btt);
  1151. return btt;
  1152. out_free:
  1153. kfree(btt);
  1154. return NULL;
  1155. }
  1156. /**
  1157. * btt_fini - de-initialize a BTT
  1158. * @btt: the BTT handle that was generated by btt_init
  1159. *
  1160. * De-initialize a Block Translation Table on device removal
  1161. *
  1162. * Context:
  1163. * Might sleep.
  1164. */
  1165. static void btt_fini(struct btt *btt)
  1166. {
  1167. if (btt) {
  1168. btt_blk_cleanup(btt);
  1169. free_arenas(btt);
  1170. debugfs_remove_recursive(btt->debugfs_dir);
  1171. kfree(btt);
  1172. }
  1173. }
  1174. int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
  1175. {
  1176. struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
  1177. struct nd_region *nd_region;
  1178. struct btt *btt;
  1179. size_t rawsize;
  1180. if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize)
  1181. return -ENODEV;
  1182. rawsize = nvdimm_namespace_capacity(ndns) - SZ_4K;
  1183. if (rawsize < ARENA_MIN_SIZE) {
  1184. return -ENXIO;
  1185. }
  1186. nd_region = to_nd_region(nd_btt->dev.parent);
  1187. btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
  1188. nd_region);
  1189. if (!btt)
  1190. return -ENOMEM;
  1191. nd_btt->btt = btt;
  1192. return 0;
  1193. }
  1194. EXPORT_SYMBOL(nvdimm_namespace_attach_btt);
  1195. int nvdimm_namespace_detach_btt(struct nd_namespace_common *ndns)
  1196. {
  1197. struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
  1198. struct btt *btt = nd_btt->btt;
  1199. btt_fini(btt);
  1200. nd_btt->btt = NULL;
  1201. return 0;
  1202. }
  1203. EXPORT_SYMBOL(nvdimm_namespace_detach_btt);
  1204. static int __init nd_btt_init(void)
  1205. {
  1206. int rc;
  1207. BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
  1208. btt_major = register_blkdev(0, "btt");
  1209. if (btt_major < 0)
  1210. return btt_major;
  1211. debugfs_root = debugfs_create_dir("btt", NULL);
  1212. if (IS_ERR_OR_NULL(debugfs_root)) {
  1213. rc = -ENXIO;
  1214. goto err_debugfs;
  1215. }
  1216. return 0;
  1217. err_debugfs:
  1218. unregister_blkdev(btt_major, "btt");
  1219. return rc;
  1220. }
  1221. static void __exit nd_btt_exit(void)
  1222. {
  1223. debugfs_remove_recursive(debugfs_root);
  1224. unregister_blkdev(btt_major, "btt");
  1225. }
  1226. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT);
  1227. MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>");
  1228. MODULE_LICENSE("GPL v2");
  1229. module_init(nd_btt_init);
  1230. module_exit(nd_btt_exit);