label.c 32 KB

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