label.c 31 KB

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