label.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/ndctl.h>
  15. #include <linux/uuid.h>
  16. #include <linux/slab.h>
  17. #include <linux/io.h>
  18. #include <linux/nd.h>
  19. #include "nd-core.h"
  20. #include "label.h"
  21. #include "nd.h"
  22. static guid_t nvdimm_btt_guid;
  23. static guid_t nvdimm_btt2_guid;
  24. static guid_t nvdimm_pfn_guid;
  25. static guid_t nvdimm_dax_guid;
  26. static u32 best_seq(u32 a, u32 b)
  27. {
  28. a &= NSINDEX_SEQ_MASK;
  29. b &= NSINDEX_SEQ_MASK;
  30. if (a == 0 || a == b)
  31. return b;
  32. else if (b == 0)
  33. return a;
  34. else if (nd_inc_seq(a) == b)
  35. return b;
  36. else
  37. return a;
  38. }
  39. unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
  40. {
  41. return ndd->nslabel_size;
  42. }
  43. static size_t __sizeof_namespace_index(u32 nslot)
  44. {
  45. return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8),
  46. NSINDEX_ALIGN);
  47. }
  48. static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd,
  49. size_t index_size)
  50. {
  51. return (ndd->nsarea.config_size - index_size * 2) /
  52. sizeof_namespace_label(ndd);
  53. }
  54. int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
  55. {
  56. u32 tmp_nslot, n;
  57. tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd);
  58. n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN;
  59. return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n);
  60. }
  61. size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
  62. {
  63. u32 nslot, space, size;
  64. /*
  65. * Per UEFI 2.7, the minimum size of the Label Storage Area is large
  66. * enough to hold 2 index blocks and 2 labels. The minimum index
  67. * block size is 256 bytes, and the minimum label size is 256 bytes.
  68. */
  69. nslot = nvdimm_num_label_slots(ndd);
  70. space = ndd->nsarea.config_size - nslot * sizeof_namespace_label(ndd);
  71. size = __sizeof_namespace_index(nslot) * 2;
  72. if (size <= space && nslot >= 2)
  73. return size / 2;
  74. dev_err(ndd->dev, "label area (%d) too small to host (%d byte) labels\n",
  75. ndd->nsarea.config_size, sizeof_namespace_label(ndd));
  76. return 0;
  77. }
  78. static int __nd_label_validate(struct nvdimm_drvdata *ndd)
  79. {
  80. /*
  81. * On media label format consists of two index blocks followed
  82. * by an array of labels. None of these structures are ever
  83. * updated in place. A sequence number tracks the current
  84. * active index and the next one to write, while labels are
  85. * written to free slots.
  86. *
  87. * +------------+
  88. * | |
  89. * | nsindex0 |
  90. * | |
  91. * +------------+
  92. * | |
  93. * | nsindex1 |
  94. * | |
  95. * +------------+
  96. * | label0 |
  97. * +------------+
  98. * | label1 |
  99. * +------------+
  100. * | |
  101. * ....nslot...
  102. * | |
  103. * +------------+
  104. * | labelN |
  105. * +------------+
  106. */
  107. struct nd_namespace_index *nsindex[] = {
  108. to_namespace_index(ndd, 0),
  109. to_namespace_index(ndd, 1),
  110. };
  111. const int num_index = ARRAY_SIZE(nsindex);
  112. struct device *dev = ndd->dev;
  113. bool valid[2] = { 0 };
  114. int i, num_valid = 0;
  115. u32 seq;
  116. for (i = 0; i < num_index; i++) {
  117. u32 nslot;
  118. u8 sig[NSINDEX_SIG_LEN];
  119. u64 sum_save, sum, size;
  120. unsigned int version, labelsize;
  121. memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
  122. if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
  123. dev_dbg(dev, "nsindex%d signature invalid\n", i);
  124. continue;
  125. }
  126. /* label sizes larger than 128 arrived with v1.2 */
  127. version = __le16_to_cpu(nsindex[i]->major) * 100
  128. + __le16_to_cpu(nsindex[i]->minor);
  129. if (version >= 102)
  130. labelsize = 1 << (7 + nsindex[i]->labelsize);
  131. else
  132. labelsize = 128;
  133. if (labelsize != sizeof_namespace_label(ndd)) {
  134. dev_dbg(dev, "nsindex%d labelsize %d invalid\n",
  135. i, nsindex[i]->labelsize);
  136. continue;
  137. }
  138. sum_save = __le64_to_cpu(nsindex[i]->checksum);
  139. nsindex[i]->checksum = __cpu_to_le64(0);
  140. sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
  141. nsindex[i]->checksum = __cpu_to_le64(sum_save);
  142. if (sum != sum_save) {
  143. dev_dbg(dev, "nsindex%d checksum invalid\n", i);
  144. continue;
  145. }
  146. seq = __le32_to_cpu(nsindex[i]->seq);
  147. if ((seq & NSINDEX_SEQ_MASK) == 0) {
  148. dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq);
  149. continue;
  150. }
  151. /* sanity check the index against expected values */
  152. if (__le64_to_cpu(nsindex[i]->myoff)
  153. != i * sizeof_namespace_index(ndd)) {
  154. dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n",
  155. i, (unsigned long long)
  156. __le64_to_cpu(nsindex[i]->myoff));
  157. continue;
  158. }
  159. if (__le64_to_cpu(nsindex[i]->otheroff)
  160. != (!i) * sizeof_namespace_index(ndd)) {
  161. dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n",
  162. i, (unsigned long long)
  163. __le64_to_cpu(nsindex[i]->otheroff));
  164. continue;
  165. }
  166. size = __le64_to_cpu(nsindex[i]->mysize);
  167. if (size > sizeof_namespace_index(ndd)
  168. || size < sizeof(struct nd_namespace_index)) {
  169. dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size);
  170. continue;
  171. }
  172. nslot = __le32_to_cpu(nsindex[i]->nslot);
  173. if (nslot * sizeof_namespace_label(ndd)
  174. + 2 * sizeof_namespace_index(ndd)
  175. > ndd->nsarea.config_size) {
  176. dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n",
  177. i, nslot, ndd->nsarea.config_size);
  178. continue;
  179. }
  180. valid[i] = true;
  181. num_valid++;
  182. }
  183. switch (num_valid) {
  184. case 0:
  185. break;
  186. case 1:
  187. for (i = 0; i < num_index; i++)
  188. if (valid[i])
  189. return i;
  190. /* can't have num_valid > 0 but valid[] = { false, false } */
  191. WARN_ON(1);
  192. break;
  193. default:
  194. /* pick the best index... */
  195. seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
  196. __le32_to_cpu(nsindex[1]->seq));
  197. if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
  198. return 1;
  199. else
  200. return 0;
  201. break;
  202. }
  203. return -1;
  204. }
  205. int nd_label_validate(struct nvdimm_drvdata *ndd)
  206. {
  207. /*
  208. * In order to probe for and validate namespace index blocks we
  209. * need to know the size of the labels, and we can't trust the
  210. * size of the labels until we validate the index blocks.
  211. * Resolve this dependency loop by probing for known label
  212. * sizes, but default to v1.2 256-byte namespace labels if
  213. * discovery fails.
  214. */
  215. int label_size[] = { 128, 256 };
  216. int i, rc;
  217. for (i = 0; i < ARRAY_SIZE(label_size); i++) {
  218. ndd->nslabel_size = label_size[i];
  219. rc = __nd_label_validate(ndd);
  220. if (rc >= 0)
  221. return rc;
  222. }
  223. return -1;
  224. }
  225. void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
  226. struct nd_namespace_index *src)
  227. {
  228. if (dst && src)
  229. /* pass */;
  230. else
  231. return;
  232. memcpy(dst, src, sizeof_namespace_index(ndd));
  233. }
  234. static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
  235. {
  236. void *base = to_namespace_index(ndd, 0);
  237. return base + 2 * sizeof_namespace_index(ndd);
  238. }
  239. static int to_slot(struct nvdimm_drvdata *ndd,
  240. struct nd_namespace_label *nd_label)
  241. {
  242. unsigned long label, base;
  243. label = (unsigned long) nd_label;
  244. base = (unsigned long) nd_label_base(ndd);
  245. return (label - base) / sizeof_namespace_label(ndd);
  246. }
  247. static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
  248. {
  249. unsigned long label, base;
  250. base = (unsigned long) nd_label_base(ndd);
  251. label = base + sizeof_namespace_label(ndd) * slot;
  252. return (struct nd_namespace_label *) label;
  253. }
  254. #define for_each_clear_bit_le(bit, addr, size) \
  255. for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
  256. (bit) < (size); \
  257. (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
  258. /**
  259. * preamble_index - common variable initialization for nd_label_* routines
  260. * @ndd: dimm container for the relevant label set
  261. * @idx: namespace_index index
  262. * @nsindex_out: on return set to the currently active namespace index
  263. * @free: on return set to the free label bitmap in the index
  264. * @nslot: on return set to the number of slots in the label space
  265. */
  266. static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
  267. struct nd_namespace_index **nsindex_out,
  268. unsigned long **free, u32 *nslot)
  269. {
  270. struct nd_namespace_index *nsindex;
  271. nsindex = to_namespace_index(ndd, idx);
  272. if (nsindex == NULL)
  273. return false;
  274. *free = (unsigned long *) nsindex->free;
  275. *nslot = __le32_to_cpu(nsindex->nslot);
  276. *nsindex_out = nsindex;
  277. return true;
  278. }
  279. char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
  280. {
  281. if (!label_id || !uuid)
  282. return NULL;
  283. snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
  284. flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
  285. return label_id->id;
  286. }
  287. static bool preamble_current(struct nvdimm_drvdata *ndd,
  288. struct nd_namespace_index **nsindex,
  289. unsigned long **free, u32 *nslot)
  290. {
  291. return preamble_index(ndd, ndd->ns_current, nsindex,
  292. free, nslot);
  293. }
  294. static bool preamble_next(struct nvdimm_drvdata *ndd,
  295. struct nd_namespace_index **nsindex,
  296. unsigned long **free, u32 *nslot)
  297. {
  298. return preamble_index(ndd, ndd->ns_next, nsindex,
  299. free, nslot);
  300. }
  301. static bool slot_valid(struct nvdimm_drvdata *ndd,
  302. struct nd_namespace_label *nd_label, u32 slot)
  303. {
  304. /* check that we are written where we expect to be written */
  305. if (slot != __le32_to_cpu(nd_label->slot))
  306. return false;
  307. /* check that DPA allocations are page aligned */
  308. if ((__le64_to_cpu(nd_label->dpa)
  309. | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
  310. return false;
  311. /* check checksum */
  312. if (namespace_label_has(ndd, checksum)) {
  313. u64 sum, sum_save;
  314. sum_save = __le64_to_cpu(nd_label->checksum);
  315. nd_label->checksum = __cpu_to_le64(0);
  316. sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
  317. nd_label->checksum = __cpu_to_le64(sum_save);
  318. if (sum != sum_save) {
  319. dev_dbg(ndd->dev, "fail checksum. slot: %d expect: %#llx\n",
  320. slot, sum);
  321. return false;
  322. }
  323. }
  324. return true;
  325. }
  326. int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
  327. {
  328. struct nd_namespace_index *nsindex;
  329. unsigned long *free;
  330. u32 nslot, slot;
  331. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  332. return 0; /* no label, nothing to reserve */
  333. for_each_clear_bit_le(slot, free, nslot) {
  334. struct nd_namespace_label *nd_label;
  335. struct nd_region *nd_region = NULL;
  336. u8 label_uuid[NSLABEL_UUID_LEN];
  337. struct nd_label_id label_id;
  338. struct resource *res;
  339. u32 flags;
  340. nd_label = to_label(ndd, slot);
  341. if (!slot_valid(ndd, nd_label, slot))
  342. continue;
  343. memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  344. flags = __le32_to_cpu(nd_label->flags);
  345. nd_label_gen_id(&label_id, label_uuid, flags);
  346. res = nvdimm_allocate_dpa(ndd, &label_id,
  347. __le64_to_cpu(nd_label->dpa),
  348. __le64_to_cpu(nd_label->rawsize));
  349. nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
  350. if (!res)
  351. return -EBUSY;
  352. }
  353. return 0;
  354. }
  355. int nd_label_active_count(struct nvdimm_drvdata *ndd)
  356. {
  357. struct nd_namespace_index *nsindex;
  358. unsigned long *free;
  359. u32 nslot, slot;
  360. int count = 0;
  361. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  362. return 0;
  363. for_each_clear_bit_le(slot, free, nslot) {
  364. struct nd_namespace_label *nd_label;
  365. nd_label = to_label(ndd, slot);
  366. if (!slot_valid(ndd, nd_label, slot)) {
  367. u32 label_slot = __le32_to_cpu(nd_label->slot);
  368. u64 size = __le64_to_cpu(nd_label->rawsize);
  369. u64 dpa = __le64_to_cpu(nd_label->dpa);
  370. dev_dbg(ndd->dev,
  371. "slot%d invalid slot: %d dpa: %llx size: %llx\n",
  372. slot, label_slot, dpa, size);
  373. continue;
  374. }
  375. count++;
  376. }
  377. return count;
  378. }
  379. struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
  380. {
  381. struct nd_namespace_index *nsindex;
  382. unsigned long *free;
  383. u32 nslot, slot;
  384. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  385. return NULL;
  386. for_each_clear_bit_le(slot, free, nslot) {
  387. struct nd_namespace_label *nd_label;
  388. nd_label = to_label(ndd, slot);
  389. if (!slot_valid(ndd, nd_label, slot))
  390. continue;
  391. if (n-- == 0)
  392. return to_label(ndd, slot);
  393. }
  394. return NULL;
  395. }
  396. u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
  397. {
  398. struct nd_namespace_index *nsindex;
  399. unsigned long *free;
  400. u32 nslot, slot;
  401. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  402. return UINT_MAX;
  403. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  404. slot = find_next_bit_le(free, nslot, 0);
  405. if (slot == nslot)
  406. return UINT_MAX;
  407. clear_bit_le(slot, free);
  408. return slot;
  409. }
  410. bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
  411. {
  412. struct nd_namespace_index *nsindex;
  413. unsigned long *free;
  414. u32 nslot;
  415. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  416. return false;
  417. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  418. if (slot < nslot)
  419. return !test_and_set_bit_le(slot, free);
  420. return false;
  421. }
  422. u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
  423. {
  424. struct nd_namespace_index *nsindex;
  425. unsigned long *free;
  426. u32 nslot;
  427. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  428. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  429. return nvdimm_num_label_slots(ndd);
  430. return bitmap_weight(free, nslot);
  431. }
  432. static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
  433. unsigned long flags)
  434. {
  435. struct nd_namespace_index *nsindex;
  436. unsigned long offset;
  437. u64 checksum;
  438. u32 nslot;
  439. int rc;
  440. nsindex = to_namespace_index(ndd, index);
  441. if (flags & ND_NSINDEX_INIT)
  442. nslot = nvdimm_num_label_slots(ndd);
  443. else
  444. nslot = __le32_to_cpu(nsindex->nslot);
  445. memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
  446. memset(&nsindex->flags, 0, 3);
  447. nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
  448. nsindex->seq = __cpu_to_le32(seq);
  449. offset = (unsigned long) nsindex
  450. - (unsigned long) to_namespace_index(ndd, 0);
  451. nsindex->myoff = __cpu_to_le64(offset);
  452. nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
  453. offset = (unsigned long) to_namespace_index(ndd,
  454. nd_label_next_nsindex(index))
  455. - (unsigned long) to_namespace_index(ndd, 0);
  456. nsindex->otheroff = __cpu_to_le64(offset);
  457. offset = (unsigned long) nd_label_base(ndd)
  458. - (unsigned long) to_namespace_index(ndd, 0);
  459. nsindex->labeloff = __cpu_to_le64(offset);
  460. nsindex->nslot = __cpu_to_le32(nslot);
  461. nsindex->major = __cpu_to_le16(1);
  462. if (sizeof_namespace_label(ndd) < 256)
  463. nsindex->minor = __cpu_to_le16(1);
  464. else
  465. nsindex->minor = __cpu_to_le16(2);
  466. nsindex->checksum = __cpu_to_le64(0);
  467. if (flags & ND_NSINDEX_INIT) {
  468. unsigned long *free = (unsigned long *) nsindex->free;
  469. u32 nfree = ALIGN(nslot, BITS_PER_LONG);
  470. int last_bits, i;
  471. memset(nsindex->free, 0xff, nfree / 8);
  472. for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
  473. clear_bit_le(nslot + i, free);
  474. }
  475. checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
  476. nsindex->checksum = __cpu_to_le64(checksum);
  477. rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
  478. nsindex, sizeof_namespace_index(ndd));
  479. if (rc < 0)
  480. return rc;
  481. if (flags & ND_NSINDEX_INIT)
  482. return 0;
  483. /* copy the index we just wrote to the new 'next' */
  484. WARN_ON(index != ndd->ns_next);
  485. nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
  486. ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
  487. ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
  488. WARN_ON(ndd->ns_current == ndd->ns_next);
  489. return 0;
  490. }
  491. static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
  492. struct nd_namespace_label *nd_label)
  493. {
  494. return (unsigned long) nd_label
  495. - (unsigned long) to_namespace_index(ndd, 0);
  496. }
  497. enum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid)
  498. {
  499. if (guid_equal(guid, &nvdimm_btt_guid))
  500. return NVDIMM_CCLASS_BTT;
  501. else if (guid_equal(guid, &nvdimm_btt2_guid))
  502. return NVDIMM_CCLASS_BTT2;
  503. else if (guid_equal(guid, &nvdimm_pfn_guid))
  504. return NVDIMM_CCLASS_PFN;
  505. else if (guid_equal(guid, &nvdimm_dax_guid))
  506. return NVDIMM_CCLASS_DAX;
  507. else if (guid_equal(guid, &guid_null))
  508. return NVDIMM_CCLASS_NONE;
  509. return NVDIMM_CCLASS_UNKNOWN;
  510. }
  511. static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
  512. guid_t *target)
  513. {
  514. if (claim_class == NVDIMM_CCLASS_BTT)
  515. return &nvdimm_btt_guid;
  516. else if (claim_class == NVDIMM_CCLASS_BTT2)
  517. return &nvdimm_btt2_guid;
  518. else if (claim_class == NVDIMM_CCLASS_PFN)
  519. return &nvdimm_pfn_guid;
  520. else if (claim_class == NVDIMM_CCLASS_DAX)
  521. return &nvdimm_dax_guid;
  522. else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
  523. /*
  524. * If we're modifying a namespace for which we don't
  525. * know the claim_class, don't touch the existing guid.
  526. */
  527. return target;
  528. } else
  529. return &guid_null;
  530. }
  531. static int __pmem_label_update(struct nd_region *nd_region,
  532. struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
  533. int pos)
  534. {
  535. struct nd_namespace_common *ndns = &nspm->nsio.common;
  536. struct nd_interleave_set *nd_set = nd_region->nd_set;
  537. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  538. struct nd_label_ent *label_ent, *victim = NULL;
  539. struct nd_namespace_label *nd_label;
  540. struct nd_namespace_index *nsindex;
  541. struct nd_label_id label_id;
  542. struct resource *res;
  543. unsigned long *free;
  544. u32 nslot, slot;
  545. size_t offset;
  546. u64 cookie;
  547. int rc;
  548. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  549. return -ENXIO;
  550. cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
  551. nd_label_gen_id(&label_id, nspm->uuid, 0);
  552. for_each_dpa_resource(ndd, res)
  553. if (strcmp(res->name, label_id.id) == 0)
  554. break;
  555. if (!res) {
  556. WARN_ON_ONCE(1);
  557. return -ENXIO;
  558. }
  559. /* allocate and write the label to the staging (next) index */
  560. slot = nd_label_alloc_slot(ndd);
  561. if (slot == UINT_MAX)
  562. return -ENXIO;
  563. dev_dbg(ndd->dev, "allocated: %d\n", slot);
  564. nd_label = to_label(ndd, slot);
  565. memset(nd_label, 0, sizeof_namespace_label(ndd));
  566. memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
  567. if (nspm->alt_name)
  568. memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
  569. nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
  570. nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
  571. nd_label->position = __cpu_to_le16(pos);
  572. nd_label->isetcookie = __cpu_to_le64(cookie);
  573. nd_label->rawsize = __cpu_to_le64(resource_size(res));
  574. nd_label->lbasize = __cpu_to_le64(nspm->lbasize);
  575. nd_label->dpa = __cpu_to_le64(res->start);
  576. nd_label->slot = __cpu_to_le32(slot);
  577. if (namespace_label_has(ndd, type_guid))
  578. guid_copy(&nd_label->type_guid, &nd_set->type_guid);
  579. if (namespace_label_has(ndd, abstraction_guid))
  580. guid_copy(&nd_label->abstraction_guid,
  581. to_abstraction_guid(ndns->claim_class,
  582. &nd_label->abstraction_guid));
  583. if (namespace_label_has(ndd, checksum)) {
  584. u64 sum;
  585. nd_label->checksum = __cpu_to_le64(0);
  586. sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
  587. nd_label->checksum = __cpu_to_le64(sum);
  588. }
  589. nd_dbg_dpa(nd_region, ndd, res, "\n");
  590. /* update label */
  591. offset = nd_label_offset(ndd, nd_label);
  592. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  593. sizeof_namespace_label(ndd));
  594. if (rc < 0)
  595. return rc;
  596. /* Garbage collect the previous label */
  597. mutex_lock(&nd_mapping->lock);
  598. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  599. if (!label_ent->label)
  600. continue;
  601. if (memcmp(nspm->uuid, label_ent->label->uuid,
  602. NSLABEL_UUID_LEN) != 0)
  603. continue;
  604. victim = label_ent;
  605. list_move_tail(&victim->list, &nd_mapping->labels);
  606. break;
  607. }
  608. if (victim) {
  609. dev_dbg(ndd->dev, "free: %d\n", slot);
  610. slot = to_slot(ndd, victim->label);
  611. nd_label_free_slot(ndd, slot);
  612. victim->label = NULL;
  613. }
  614. /* update index */
  615. rc = nd_label_write_index(ndd, ndd->ns_next,
  616. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  617. if (rc == 0) {
  618. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  619. if (!label_ent->label) {
  620. label_ent->label = nd_label;
  621. nd_label = NULL;
  622. break;
  623. }
  624. dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
  625. "failed to track label: %d\n",
  626. to_slot(ndd, nd_label));
  627. if (nd_label)
  628. rc = -ENXIO;
  629. }
  630. mutex_unlock(&nd_mapping->lock);
  631. return rc;
  632. }
  633. static bool is_old_resource(struct resource *res, struct resource **list, int n)
  634. {
  635. int i;
  636. if (res->flags & DPA_RESOURCE_ADJUSTED)
  637. return false;
  638. for (i = 0; i < n; i++)
  639. if (res == list[i])
  640. return true;
  641. return false;
  642. }
  643. static struct resource *to_resource(struct nvdimm_drvdata *ndd,
  644. struct nd_namespace_label *nd_label)
  645. {
  646. struct resource *res;
  647. for_each_dpa_resource(ndd, res) {
  648. if (res->start != __le64_to_cpu(nd_label->dpa))
  649. continue;
  650. if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
  651. continue;
  652. return res;
  653. }
  654. return NULL;
  655. }
  656. /*
  657. * 1/ Account all the labels that can be freed after this update
  658. * 2/ Allocate and write the label to the staging (next) index
  659. * 3/ Record the resources in the namespace device
  660. */
  661. static int __blk_label_update(struct nd_region *nd_region,
  662. struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
  663. int num_labels)
  664. {
  665. int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
  666. struct nd_interleave_set *nd_set = nd_region->nd_set;
  667. struct nd_namespace_common *ndns = &nsblk->common;
  668. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  669. struct nd_namespace_label *nd_label;
  670. struct nd_label_ent *label_ent, *e;
  671. struct nd_namespace_index *nsindex;
  672. unsigned long *free, *victim_map = NULL;
  673. struct resource *res, **old_res_list;
  674. struct nd_label_id label_id;
  675. u8 uuid[NSLABEL_UUID_LEN];
  676. int min_dpa_idx = 0;
  677. LIST_HEAD(list);
  678. u32 nslot, slot;
  679. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  680. return -ENXIO;
  681. old_res_list = nsblk->res;
  682. nfree = nd_label_nfree(ndd);
  683. old_num_resources = nsblk->num_resources;
  684. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  685. /*
  686. * We need to loop over the old resources a few times, which seems a
  687. * bit inefficient, but we need to know that we have the label
  688. * space before we start mutating the tracking structures.
  689. * Otherwise the recovery method of last resort for userspace is
  690. * disable and re-enable the parent region.
  691. */
  692. alloc = 0;
  693. for_each_dpa_resource(ndd, res) {
  694. if (strcmp(res->name, label_id.id) != 0)
  695. continue;
  696. if (!is_old_resource(res, old_res_list, old_num_resources))
  697. alloc++;
  698. }
  699. victims = 0;
  700. if (old_num_resources) {
  701. /* convert old local-label-map to dimm-slot victim-map */
  702. victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
  703. GFP_KERNEL);
  704. if (!victim_map)
  705. return -ENOMEM;
  706. /* mark unused labels for garbage collection */
  707. for_each_clear_bit_le(slot, free, nslot) {
  708. nd_label = to_label(ndd, slot);
  709. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  710. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  711. continue;
  712. res = to_resource(ndd, nd_label);
  713. if (res && is_old_resource(res, old_res_list,
  714. old_num_resources))
  715. continue;
  716. slot = to_slot(ndd, nd_label);
  717. set_bit(slot, victim_map);
  718. victims++;
  719. }
  720. }
  721. /* don't allow updates that consume the last label */
  722. if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
  723. dev_info(&nsblk->common.dev, "insufficient label space\n");
  724. kfree(victim_map);
  725. return -ENOSPC;
  726. }
  727. /* from here on we need to abort on error */
  728. /* assign all resources to the namespace before writing the labels */
  729. nsblk->res = NULL;
  730. nsblk->num_resources = 0;
  731. for_each_dpa_resource(ndd, res) {
  732. if (strcmp(res->name, label_id.id) != 0)
  733. continue;
  734. if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
  735. rc = -ENOMEM;
  736. goto abort;
  737. }
  738. }
  739. /*
  740. * Find the resource associated with the first label in the set
  741. * per the v1.2 namespace specification.
  742. */
  743. for (i = 0; i < nsblk->num_resources; i++) {
  744. struct resource *min = nsblk->res[min_dpa_idx];
  745. res = nsblk->res[i];
  746. if (res->start < min->start)
  747. min_dpa_idx = i;
  748. }
  749. for (i = 0; i < nsblk->num_resources; i++) {
  750. size_t offset;
  751. res = nsblk->res[i];
  752. if (is_old_resource(res, old_res_list, old_num_resources))
  753. continue; /* carry-over */
  754. slot = nd_label_alloc_slot(ndd);
  755. if (slot == UINT_MAX)
  756. goto abort;
  757. dev_dbg(ndd->dev, "allocated: %d\n", slot);
  758. nd_label = to_label(ndd, slot);
  759. memset(nd_label, 0, sizeof_namespace_label(ndd));
  760. memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
  761. if (nsblk->alt_name)
  762. memcpy(nd_label->name, nsblk->alt_name,
  763. NSLABEL_NAME_LEN);
  764. nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
  765. /*
  766. * Use the presence of the type_guid as a flag to
  767. * determine isetcookie usage and nlabel + position
  768. * policy for blk-aperture namespaces.
  769. */
  770. if (namespace_label_has(ndd, type_guid)) {
  771. if (i == min_dpa_idx) {
  772. nd_label->nlabel = __cpu_to_le16(nsblk->num_resources);
  773. nd_label->position = __cpu_to_le16(0);
  774. } else {
  775. nd_label->nlabel = __cpu_to_le16(0xffff);
  776. nd_label->position = __cpu_to_le16(0xffff);
  777. }
  778. nd_label->isetcookie = __cpu_to_le64(nd_set->cookie2);
  779. } else {
  780. nd_label->nlabel = __cpu_to_le16(0); /* N/A */
  781. nd_label->position = __cpu_to_le16(0); /* N/A */
  782. nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
  783. }
  784. nd_label->dpa = __cpu_to_le64(res->start);
  785. nd_label->rawsize = __cpu_to_le64(resource_size(res));
  786. nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
  787. nd_label->slot = __cpu_to_le32(slot);
  788. if (namespace_label_has(ndd, type_guid))
  789. guid_copy(&nd_label->type_guid, &nd_set->type_guid);
  790. if (namespace_label_has(ndd, abstraction_guid))
  791. guid_copy(&nd_label->abstraction_guid,
  792. to_abstraction_guid(ndns->claim_class,
  793. &nd_label->abstraction_guid));
  794. if (namespace_label_has(ndd, checksum)) {
  795. u64 sum;
  796. nd_label->checksum = __cpu_to_le64(0);
  797. sum = nd_fletcher64(nd_label,
  798. sizeof_namespace_label(ndd), 1);
  799. nd_label->checksum = __cpu_to_le64(sum);
  800. }
  801. /* update label */
  802. offset = nd_label_offset(ndd, nd_label);
  803. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  804. sizeof_namespace_label(ndd));
  805. if (rc < 0)
  806. goto abort;
  807. }
  808. /* free up now unused slots in the new index */
  809. for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
  810. dev_dbg(ndd->dev, "free: %d\n", slot);
  811. nd_label_free_slot(ndd, slot);
  812. }
  813. /* update index */
  814. rc = nd_label_write_index(ndd, ndd->ns_next,
  815. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  816. if (rc)
  817. goto abort;
  818. /*
  819. * Now that the on-dimm labels are up to date, fix up the tracking
  820. * entries in nd_mapping->labels
  821. */
  822. nlabel = 0;
  823. mutex_lock(&nd_mapping->lock);
  824. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  825. nd_label = label_ent->label;
  826. if (!nd_label)
  827. continue;
  828. nlabel++;
  829. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  830. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  831. continue;
  832. nlabel--;
  833. list_move(&label_ent->list, &list);
  834. label_ent->label = NULL;
  835. }
  836. list_splice_tail_init(&list, &nd_mapping->labels);
  837. mutex_unlock(&nd_mapping->lock);
  838. if (nlabel + nsblk->num_resources > num_labels) {
  839. /*
  840. * Bug, we can't end up with more resources than
  841. * available labels
  842. */
  843. WARN_ON_ONCE(1);
  844. rc = -ENXIO;
  845. goto out;
  846. }
  847. mutex_lock(&nd_mapping->lock);
  848. label_ent = list_first_entry_or_null(&nd_mapping->labels,
  849. typeof(*label_ent), list);
  850. if (!label_ent) {
  851. WARN_ON(1);
  852. mutex_unlock(&nd_mapping->lock);
  853. rc = -ENXIO;
  854. goto out;
  855. }
  856. for_each_clear_bit_le(slot, free, nslot) {
  857. nd_label = to_label(ndd, slot);
  858. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  859. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  860. continue;
  861. res = to_resource(ndd, nd_label);
  862. res->flags &= ~DPA_RESOURCE_ADJUSTED;
  863. dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
  864. list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
  865. if (label_ent->label)
  866. continue;
  867. label_ent->label = nd_label;
  868. nd_label = NULL;
  869. break;
  870. }
  871. if (nd_label)
  872. dev_WARN(&nsblk->common.dev,
  873. "failed to track label slot%d\n", slot);
  874. }
  875. mutex_unlock(&nd_mapping->lock);
  876. out:
  877. kfree(old_res_list);
  878. kfree(victim_map);
  879. return rc;
  880. abort:
  881. /*
  882. * 1/ repair the allocated label bitmap in the index
  883. * 2/ restore the resource list
  884. */
  885. nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
  886. kfree(nsblk->res);
  887. nsblk->res = old_res_list;
  888. nsblk->num_resources = old_num_resources;
  889. old_res_list = NULL;
  890. goto out;
  891. }
  892. static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
  893. {
  894. int i, old_num_labels = 0;
  895. struct nd_label_ent *label_ent;
  896. struct nd_namespace_index *nsindex;
  897. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  898. mutex_lock(&nd_mapping->lock);
  899. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  900. old_num_labels++;
  901. mutex_unlock(&nd_mapping->lock);
  902. /*
  903. * We need to preserve all the old labels for the mapping so
  904. * they can be garbage collected after writing the new labels.
  905. */
  906. for (i = old_num_labels; i < num_labels; i++) {
  907. label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
  908. if (!label_ent)
  909. return -ENOMEM;
  910. mutex_lock(&nd_mapping->lock);
  911. list_add_tail(&label_ent->list, &nd_mapping->labels);
  912. mutex_unlock(&nd_mapping->lock);
  913. }
  914. if (ndd->ns_current == -1 || ndd->ns_next == -1)
  915. /* pass */;
  916. else
  917. return max(num_labels, old_num_labels);
  918. nsindex = to_namespace_index(ndd, 0);
  919. memset(nsindex, 0, ndd->nsarea.config_size);
  920. for (i = 0; i < 2; i++) {
  921. int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
  922. if (rc)
  923. return rc;
  924. }
  925. ndd->ns_next = 1;
  926. ndd->ns_current = 0;
  927. return max(num_labels, old_num_labels);
  928. }
  929. static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
  930. {
  931. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  932. struct nd_label_ent *label_ent, *e;
  933. struct nd_namespace_index *nsindex;
  934. u8 label_uuid[NSLABEL_UUID_LEN];
  935. unsigned long *free;
  936. LIST_HEAD(list);
  937. u32 nslot, slot;
  938. int active = 0;
  939. if (!uuid)
  940. return 0;
  941. /* no index || no labels == nothing to delete */
  942. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  943. return 0;
  944. mutex_lock(&nd_mapping->lock);
  945. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  946. struct nd_namespace_label *nd_label = label_ent->label;
  947. if (!nd_label)
  948. continue;
  949. active++;
  950. memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  951. if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
  952. continue;
  953. active--;
  954. slot = to_slot(ndd, nd_label);
  955. nd_label_free_slot(ndd, slot);
  956. dev_dbg(ndd->dev, "free: %d\n", slot);
  957. list_move_tail(&label_ent->list, &list);
  958. label_ent->label = NULL;
  959. }
  960. list_splice_tail_init(&list, &nd_mapping->labels);
  961. if (active == 0) {
  962. nd_mapping_free_labels(nd_mapping);
  963. dev_dbg(ndd->dev, "no more active labels\n");
  964. }
  965. mutex_unlock(&nd_mapping->lock);
  966. return nd_label_write_index(ndd, ndd->ns_next,
  967. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  968. }
  969. int nd_pmem_namespace_label_update(struct nd_region *nd_region,
  970. struct nd_namespace_pmem *nspm, resource_size_t size)
  971. {
  972. int i;
  973. for (i = 0; i < nd_region->ndr_mappings; i++) {
  974. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  975. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  976. struct resource *res;
  977. int rc, count = 0;
  978. if (size == 0) {
  979. rc = del_labels(nd_mapping, nspm->uuid);
  980. if (rc)
  981. return rc;
  982. continue;
  983. }
  984. for_each_dpa_resource(ndd, res)
  985. if (strncmp(res->name, "pmem", 4) == 0)
  986. count++;
  987. WARN_ON_ONCE(!count);
  988. rc = init_labels(nd_mapping, count);
  989. if (rc < 0)
  990. return rc;
  991. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
  992. if (rc)
  993. return rc;
  994. }
  995. return 0;
  996. }
  997. int nd_blk_namespace_label_update(struct nd_region *nd_region,
  998. struct nd_namespace_blk *nsblk, resource_size_t size)
  999. {
  1000. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1001. struct resource *res;
  1002. int count = 0;
  1003. if (size == 0)
  1004. return del_labels(nd_mapping, nsblk->uuid);
  1005. for_each_dpa_resource(to_ndd(nd_mapping), res)
  1006. count++;
  1007. count = init_labels(nd_mapping, count);
  1008. if (count < 0)
  1009. return count;
  1010. return __blk_label_update(nd_region, nd_mapping, nsblk, count);
  1011. }
  1012. int __init nd_label_init(void)
  1013. {
  1014. WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
  1015. WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
  1016. WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
  1017. WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
  1018. return 0;
  1019. }