label.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/nd.h>
  18. #include "nd-core.h"
  19. #include "label.h"
  20. #include "nd.h"
  21. static u32 best_seq(u32 a, u32 b)
  22. {
  23. a &= NSINDEX_SEQ_MASK;
  24. b &= NSINDEX_SEQ_MASK;
  25. if (a == 0 || a == b)
  26. return b;
  27. else if (b == 0)
  28. return a;
  29. else if (nd_inc_seq(a) == b)
  30. return b;
  31. else
  32. return a;
  33. }
  34. size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
  35. {
  36. u32 index_span;
  37. if (ndd->nsindex_size)
  38. return ndd->nsindex_size;
  39. /*
  40. * The minimum index space is 512 bytes, with that amount of
  41. * index we can describe ~1400 labels which is less than a byte
  42. * of overhead per label. Round up to a byte of overhead per
  43. * label and determine the size of the index region. Yes, this
  44. * starts to waste space at larger config_sizes, but it's
  45. * unlikely we'll ever see anything but 128K.
  46. */
  47. index_span = ndd->nsarea.config_size / 129;
  48. index_span /= NSINDEX_ALIGN * 2;
  49. ndd->nsindex_size = index_span * NSINDEX_ALIGN;
  50. return ndd->nsindex_size;
  51. }
  52. int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
  53. {
  54. return ndd->nsarea.config_size / 129;
  55. }
  56. int nd_label_validate(struct nvdimm_drvdata *ndd)
  57. {
  58. /*
  59. * On media label format consists of two index blocks followed
  60. * by an array of labels. None of these structures are ever
  61. * updated in place. A sequence number tracks the current
  62. * active index and the next one to write, while labels are
  63. * written to free slots.
  64. *
  65. * +------------+
  66. * | |
  67. * | nsindex0 |
  68. * | |
  69. * +------------+
  70. * | |
  71. * | nsindex1 |
  72. * | |
  73. * +------------+
  74. * | label0 |
  75. * +------------+
  76. * | label1 |
  77. * +------------+
  78. * | |
  79. * ....nslot...
  80. * | |
  81. * +------------+
  82. * | labelN |
  83. * +------------+
  84. */
  85. struct nd_namespace_index *nsindex[] = {
  86. to_namespace_index(ndd, 0),
  87. to_namespace_index(ndd, 1),
  88. };
  89. const int num_index = ARRAY_SIZE(nsindex);
  90. struct device *dev = ndd->dev;
  91. bool valid[2] = { 0 };
  92. int i, num_valid = 0;
  93. u32 seq;
  94. for (i = 0; i < num_index; i++) {
  95. u32 nslot;
  96. u8 sig[NSINDEX_SIG_LEN];
  97. u64 sum_save, sum, size;
  98. memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
  99. if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
  100. dev_dbg(dev, "%s: nsindex%d signature invalid\n",
  101. __func__, i);
  102. continue;
  103. }
  104. sum_save = __le64_to_cpu(nsindex[i]->checksum);
  105. nsindex[i]->checksum = __cpu_to_le64(0);
  106. sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
  107. nsindex[i]->checksum = __cpu_to_le64(sum_save);
  108. if (sum != sum_save) {
  109. dev_dbg(dev, "%s: nsindex%d checksum invalid\n",
  110. __func__, i);
  111. continue;
  112. }
  113. seq = __le32_to_cpu(nsindex[i]->seq);
  114. if ((seq & NSINDEX_SEQ_MASK) == 0) {
  115. dev_dbg(dev, "%s: nsindex%d sequence: %#x invalid\n",
  116. __func__, i, seq);
  117. continue;
  118. }
  119. /* sanity check the index against expected values */
  120. if (__le64_to_cpu(nsindex[i]->myoff)
  121. != i * sizeof_namespace_index(ndd)) {
  122. dev_dbg(dev, "%s: nsindex%d myoff: %#llx invalid\n",
  123. __func__, i, (unsigned long long)
  124. __le64_to_cpu(nsindex[i]->myoff));
  125. continue;
  126. }
  127. if (__le64_to_cpu(nsindex[i]->otheroff)
  128. != (!i) * sizeof_namespace_index(ndd)) {
  129. dev_dbg(dev, "%s: nsindex%d otheroff: %#llx invalid\n",
  130. __func__, i, (unsigned long long)
  131. __le64_to_cpu(nsindex[i]->otheroff));
  132. continue;
  133. }
  134. size = __le64_to_cpu(nsindex[i]->mysize);
  135. if (size > sizeof_namespace_index(ndd)
  136. || size < sizeof(struct nd_namespace_index)) {
  137. dev_dbg(dev, "%s: nsindex%d mysize: %#llx invalid\n",
  138. __func__, i, size);
  139. continue;
  140. }
  141. nslot = __le32_to_cpu(nsindex[i]->nslot);
  142. if (nslot * sizeof(struct nd_namespace_label)
  143. + 2 * sizeof_namespace_index(ndd)
  144. > ndd->nsarea.config_size) {
  145. dev_dbg(dev, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
  146. __func__, i, nslot,
  147. ndd->nsarea.config_size);
  148. continue;
  149. }
  150. valid[i] = true;
  151. num_valid++;
  152. }
  153. switch (num_valid) {
  154. case 0:
  155. break;
  156. case 1:
  157. for (i = 0; i < num_index; i++)
  158. if (valid[i])
  159. return i;
  160. /* can't have num_valid > 0 but valid[] = { false, false } */
  161. WARN_ON(1);
  162. break;
  163. default:
  164. /* pick the best index... */
  165. seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
  166. __le32_to_cpu(nsindex[1]->seq));
  167. if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
  168. return 1;
  169. else
  170. return 0;
  171. break;
  172. }
  173. return -1;
  174. }
  175. void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
  176. struct nd_namespace_index *src)
  177. {
  178. if (dst && src)
  179. /* pass */;
  180. else
  181. return;
  182. memcpy(dst, src, sizeof_namespace_index(ndd));
  183. }
  184. static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
  185. {
  186. void *base = to_namespace_index(ndd, 0);
  187. return base + 2 * sizeof_namespace_index(ndd);
  188. }
  189. static int to_slot(struct nvdimm_drvdata *ndd,
  190. struct nd_namespace_label *nd_label)
  191. {
  192. return nd_label - nd_label_base(ndd);
  193. }
  194. #define for_each_clear_bit_le(bit, addr, size) \
  195. for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
  196. (bit) < (size); \
  197. (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
  198. /**
  199. * preamble_index - common variable initialization for nd_label_* routines
  200. * @ndd: dimm container for the relevant label set
  201. * @idx: namespace_index index
  202. * @nsindex_out: on return set to the currently active namespace index
  203. * @free: on return set to the free label bitmap in the index
  204. * @nslot: on return set to the number of slots in the label space
  205. */
  206. static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
  207. struct nd_namespace_index **nsindex_out,
  208. unsigned long **free, u32 *nslot)
  209. {
  210. struct nd_namespace_index *nsindex;
  211. nsindex = to_namespace_index(ndd, idx);
  212. if (nsindex == NULL)
  213. return false;
  214. *free = (unsigned long *) nsindex->free;
  215. *nslot = __le32_to_cpu(nsindex->nslot);
  216. *nsindex_out = nsindex;
  217. return true;
  218. }
  219. char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
  220. {
  221. if (!label_id || !uuid)
  222. return NULL;
  223. snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
  224. flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
  225. return label_id->id;
  226. }
  227. static bool preamble_current(struct nvdimm_drvdata *ndd,
  228. struct nd_namespace_index **nsindex,
  229. unsigned long **free, u32 *nslot)
  230. {
  231. return preamble_index(ndd, ndd->ns_current, nsindex,
  232. free, nslot);
  233. }
  234. static bool preamble_next(struct nvdimm_drvdata *ndd,
  235. struct nd_namespace_index **nsindex,
  236. unsigned long **free, u32 *nslot)
  237. {
  238. return preamble_index(ndd, ndd->ns_next, nsindex,
  239. free, nslot);
  240. }
  241. static bool slot_valid(struct nd_namespace_label *nd_label, u32 slot)
  242. {
  243. /* check that we are written where we expect to be written */
  244. if (slot != __le32_to_cpu(nd_label->slot))
  245. return false;
  246. /* check that DPA allocations are page aligned */
  247. if ((__le64_to_cpu(nd_label->dpa)
  248. | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
  249. return false;
  250. return true;
  251. }
  252. int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
  253. {
  254. struct nd_namespace_index *nsindex;
  255. unsigned long *free;
  256. u32 nslot, slot;
  257. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  258. return 0; /* no label, nothing to reserve */
  259. for_each_clear_bit_le(slot, free, nslot) {
  260. struct nd_namespace_label *nd_label;
  261. struct nd_region *nd_region = NULL;
  262. u8 label_uuid[NSLABEL_UUID_LEN];
  263. struct nd_label_id label_id;
  264. struct resource *res;
  265. u32 flags;
  266. nd_label = nd_label_base(ndd) + slot;
  267. if (!slot_valid(nd_label, slot))
  268. continue;
  269. memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  270. flags = __le32_to_cpu(nd_label->flags);
  271. nd_label_gen_id(&label_id, label_uuid, flags);
  272. res = nvdimm_allocate_dpa(ndd, &label_id,
  273. __le64_to_cpu(nd_label->dpa),
  274. __le64_to_cpu(nd_label->rawsize));
  275. nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
  276. if (!res)
  277. return -EBUSY;
  278. }
  279. return 0;
  280. }
  281. int nd_label_active_count(struct nvdimm_drvdata *ndd)
  282. {
  283. struct nd_namespace_index *nsindex;
  284. unsigned long *free;
  285. u32 nslot, slot;
  286. int count = 0;
  287. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  288. return 0;
  289. for_each_clear_bit_le(slot, free, nslot) {
  290. struct nd_namespace_label *nd_label;
  291. nd_label = nd_label_base(ndd) + slot;
  292. if (!slot_valid(nd_label, slot)) {
  293. u32 label_slot = __le32_to_cpu(nd_label->slot);
  294. u64 size = __le64_to_cpu(nd_label->rawsize);
  295. u64 dpa = __le64_to_cpu(nd_label->dpa);
  296. dev_dbg(ndd->dev,
  297. "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
  298. __func__, slot, label_slot, dpa, size);
  299. continue;
  300. }
  301. count++;
  302. }
  303. return count;
  304. }
  305. struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
  306. {
  307. struct nd_namespace_index *nsindex;
  308. unsigned long *free;
  309. u32 nslot, slot;
  310. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  311. return NULL;
  312. for_each_clear_bit_le(slot, free, nslot) {
  313. struct nd_namespace_label *nd_label;
  314. nd_label = nd_label_base(ndd) + slot;
  315. if (!slot_valid(nd_label, slot))
  316. continue;
  317. if (n-- == 0)
  318. return nd_label_base(ndd) + slot;
  319. }
  320. return NULL;
  321. }
  322. u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
  323. {
  324. struct nd_namespace_index *nsindex;
  325. unsigned long *free;
  326. u32 nslot, slot;
  327. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  328. return UINT_MAX;
  329. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  330. slot = find_next_bit_le(free, nslot, 0);
  331. if (slot == nslot)
  332. return UINT_MAX;
  333. clear_bit_le(slot, free);
  334. return slot;
  335. }
  336. bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
  337. {
  338. struct nd_namespace_index *nsindex;
  339. unsigned long *free;
  340. u32 nslot;
  341. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  342. return false;
  343. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  344. if (slot < nslot)
  345. return !test_and_set_bit_le(slot, free);
  346. return false;
  347. }
  348. u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
  349. {
  350. struct nd_namespace_index *nsindex;
  351. unsigned long *free;
  352. u32 nslot;
  353. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  354. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  355. return nvdimm_num_label_slots(ndd);
  356. return bitmap_weight(free, nslot);
  357. }
  358. static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
  359. unsigned long flags)
  360. {
  361. struct nd_namespace_index *nsindex;
  362. unsigned long offset;
  363. u64 checksum;
  364. u32 nslot;
  365. int rc;
  366. nsindex = to_namespace_index(ndd, index);
  367. if (flags & ND_NSINDEX_INIT)
  368. nslot = nvdimm_num_label_slots(ndd);
  369. else
  370. nslot = __le32_to_cpu(nsindex->nslot);
  371. memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
  372. nsindex->flags = __cpu_to_le32(0);
  373. nsindex->seq = __cpu_to_le32(seq);
  374. offset = (unsigned long) nsindex
  375. - (unsigned long) to_namespace_index(ndd, 0);
  376. nsindex->myoff = __cpu_to_le64(offset);
  377. nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
  378. offset = (unsigned long) to_namespace_index(ndd,
  379. nd_label_next_nsindex(index))
  380. - (unsigned long) to_namespace_index(ndd, 0);
  381. nsindex->otheroff = __cpu_to_le64(offset);
  382. offset = (unsigned long) nd_label_base(ndd)
  383. - (unsigned long) to_namespace_index(ndd, 0);
  384. nsindex->labeloff = __cpu_to_le64(offset);
  385. nsindex->nslot = __cpu_to_le32(nslot);
  386. nsindex->major = __cpu_to_le16(1);
  387. nsindex->minor = __cpu_to_le16(1);
  388. nsindex->checksum = __cpu_to_le64(0);
  389. if (flags & ND_NSINDEX_INIT) {
  390. unsigned long *free = (unsigned long *) nsindex->free;
  391. u32 nfree = ALIGN(nslot, BITS_PER_LONG);
  392. int last_bits, i;
  393. memset(nsindex->free, 0xff, nfree / 8);
  394. for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
  395. clear_bit_le(nslot + i, free);
  396. }
  397. checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
  398. nsindex->checksum = __cpu_to_le64(checksum);
  399. rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
  400. nsindex, sizeof_namespace_index(ndd));
  401. if (rc < 0)
  402. return rc;
  403. if (flags & ND_NSINDEX_INIT)
  404. return 0;
  405. /* copy the index we just wrote to the new 'next' */
  406. WARN_ON(index != ndd->ns_next);
  407. nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
  408. ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
  409. ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
  410. WARN_ON(ndd->ns_current == ndd->ns_next);
  411. return 0;
  412. }
  413. static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
  414. struct nd_namespace_label *nd_label)
  415. {
  416. return (unsigned long) nd_label
  417. - (unsigned long) to_namespace_index(ndd, 0);
  418. }
  419. static int __pmem_label_update(struct nd_region *nd_region,
  420. struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
  421. int pos)
  422. {
  423. u64 cookie = nd_region_interleave_set_cookie(nd_region), rawsize;
  424. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  425. struct nd_namespace_label *victim_label;
  426. struct nd_namespace_label *nd_label;
  427. struct nd_namespace_index *nsindex;
  428. unsigned long *free;
  429. u32 nslot, slot;
  430. size_t offset;
  431. int rc;
  432. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  433. return -ENXIO;
  434. /* allocate and write the label to the staging (next) index */
  435. slot = nd_label_alloc_slot(ndd);
  436. if (slot == UINT_MAX)
  437. return -ENXIO;
  438. dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
  439. nd_label = nd_label_base(ndd) + slot;
  440. memset(nd_label, 0, sizeof(struct nd_namespace_label));
  441. memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
  442. if (nspm->alt_name)
  443. memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
  444. nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
  445. nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
  446. nd_label->position = __cpu_to_le16(pos);
  447. nd_label->isetcookie = __cpu_to_le64(cookie);
  448. rawsize = div_u64(resource_size(&nspm->nsio.res),
  449. nd_region->ndr_mappings);
  450. nd_label->rawsize = __cpu_to_le64(rawsize);
  451. nd_label->dpa = __cpu_to_le64(nd_mapping->start);
  452. nd_label->slot = __cpu_to_le32(slot);
  453. /* update label */
  454. offset = nd_label_offset(ndd, nd_label);
  455. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  456. sizeof(struct nd_namespace_label));
  457. if (rc < 0)
  458. return rc;
  459. /* Garbage collect the previous label */
  460. victim_label = nd_mapping->labels[0];
  461. if (victim_label) {
  462. slot = to_slot(ndd, victim_label);
  463. nd_label_free_slot(ndd, slot);
  464. dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
  465. }
  466. /* update index */
  467. rc = nd_label_write_index(ndd, ndd->ns_next,
  468. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  469. if (rc < 0)
  470. return rc;
  471. nd_mapping->labels[0] = nd_label;
  472. return 0;
  473. }
  474. static void del_label(struct nd_mapping *nd_mapping, int l)
  475. {
  476. struct nd_namespace_label *next_label, *nd_label;
  477. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  478. unsigned int slot;
  479. int j;
  480. nd_label = nd_mapping->labels[l];
  481. slot = to_slot(ndd, nd_label);
  482. dev_vdbg(ndd->dev, "%s: clear: %d\n", __func__, slot);
  483. for (j = l; (next_label = nd_mapping->labels[j + 1]); j++)
  484. nd_mapping->labels[j] = next_label;
  485. nd_mapping->labels[j] = NULL;
  486. }
  487. static bool is_old_resource(struct resource *res, struct resource **list, int n)
  488. {
  489. int i;
  490. if (res->flags & DPA_RESOURCE_ADJUSTED)
  491. return false;
  492. for (i = 0; i < n; i++)
  493. if (res == list[i])
  494. return true;
  495. return false;
  496. }
  497. static struct resource *to_resource(struct nvdimm_drvdata *ndd,
  498. struct nd_namespace_label *nd_label)
  499. {
  500. struct resource *res;
  501. for_each_dpa_resource(ndd, res) {
  502. if (res->start != __le64_to_cpu(nd_label->dpa))
  503. continue;
  504. if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
  505. continue;
  506. return res;
  507. }
  508. return NULL;
  509. }
  510. /*
  511. * 1/ Account all the labels that can be freed after this update
  512. * 2/ Allocate and write the label to the staging (next) index
  513. * 3/ Record the resources in the namespace device
  514. */
  515. static int __blk_label_update(struct nd_region *nd_region,
  516. struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
  517. int num_labels)
  518. {
  519. int i, l, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
  520. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  521. struct nd_namespace_label *nd_label;
  522. struct nd_namespace_index *nsindex;
  523. unsigned long *free, *victim_map = NULL;
  524. struct resource *res, **old_res_list;
  525. struct nd_label_id label_id;
  526. u8 uuid[NSLABEL_UUID_LEN];
  527. u32 nslot, slot;
  528. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  529. return -ENXIO;
  530. old_res_list = nsblk->res;
  531. nfree = nd_label_nfree(ndd);
  532. old_num_resources = nsblk->num_resources;
  533. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  534. /*
  535. * We need to loop over the old resources a few times, which seems a
  536. * bit inefficient, but we need to know that we have the label
  537. * space before we start mutating the tracking structures.
  538. * Otherwise the recovery method of last resort for userspace is
  539. * disable and re-enable the parent region.
  540. */
  541. alloc = 0;
  542. for_each_dpa_resource(ndd, res) {
  543. if (strcmp(res->name, label_id.id) != 0)
  544. continue;
  545. if (!is_old_resource(res, old_res_list, old_num_resources))
  546. alloc++;
  547. }
  548. victims = 0;
  549. if (old_num_resources) {
  550. /* convert old local-label-map to dimm-slot victim-map */
  551. victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
  552. GFP_KERNEL);
  553. if (!victim_map)
  554. return -ENOMEM;
  555. /* mark unused labels for garbage collection */
  556. for_each_clear_bit_le(slot, free, nslot) {
  557. nd_label = nd_label_base(ndd) + slot;
  558. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  559. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  560. continue;
  561. res = to_resource(ndd, nd_label);
  562. if (res && is_old_resource(res, old_res_list,
  563. old_num_resources))
  564. continue;
  565. slot = to_slot(ndd, nd_label);
  566. set_bit(slot, victim_map);
  567. victims++;
  568. }
  569. }
  570. /* don't allow updates that consume the last label */
  571. if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
  572. dev_info(&nsblk->common.dev, "insufficient label space\n");
  573. kfree(victim_map);
  574. return -ENOSPC;
  575. }
  576. /* from here on we need to abort on error */
  577. /* assign all resources to the namespace before writing the labels */
  578. nsblk->res = NULL;
  579. nsblk->num_resources = 0;
  580. for_each_dpa_resource(ndd, res) {
  581. if (strcmp(res->name, label_id.id) != 0)
  582. continue;
  583. if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
  584. rc = -ENOMEM;
  585. goto abort;
  586. }
  587. }
  588. for (i = 0; i < nsblk->num_resources; i++) {
  589. size_t offset;
  590. res = nsblk->res[i];
  591. if (is_old_resource(res, old_res_list, old_num_resources))
  592. continue; /* carry-over */
  593. slot = nd_label_alloc_slot(ndd);
  594. if (slot == UINT_MAX)
  595. goto abort;
  596. dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
  597. nd_label = nd_label_base(ndd) + slot;
  598. memset(nd_label, 0, sizeof(struct nd_namespace_label));
  599. memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
  600. if (nsblk->alt_name)
  601. memcpy(nd_label->name, nsblk->alt_name,
  602. NSLABEL_NAME_LEN);
  603. nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
  604. nd_label->nlabel = __cpu_to_le16(0); /* N/A */
  605. nd_label->position = __cpu_to_le16(0); /* N/A */
  606. nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
  607. nd_label->dpa = __cpu_to_le64(res->start);
  608. nd_label->rawsize = __cpu_to_le64(resource_size(res));
  609. nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
  610. nd_label->slot = __cpu_to_le32(slot);
  611. /* update label */
  612. offset = nd_label_offset(ndd, nd_label);
  613. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  614. sizeof(struct nd_namespace_label));
  615. if (rc < 0)
  616. goto abort;
  617. }
  618. /* free up now unused slots in the new index */
  619. for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
  620. dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
  621. nd_label_free_slot(ndd, slot);
  622. }
  623. /* update index */
  624. rc = nd_label_write_index(ndd, ndd->ns_next,
  625. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  626. if (rc)
  627. goto abort;
  628. /*
  629. * Now that the on-dimm labels are up to date, fix up the tracking
  630. * entries in nd_mapping->labels
  631. */
  632. nlabel = 0;
  633. for_each_label(l, nd_label, nd_mapping->labels) {
  634. nlabel++;
  635. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  636. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  637. continue;
  638. nlabel--;
  639. del_label(nd_mapping, l);
  640. l--; /* retry with the new label at this index */
  641. }
  642. if (nlabel + nsblk->num_resources > num_labels) {
  643. /*
  644. * Bug, we can't end up with more resources than
  645. * available labels
  646. */
  647. WARN_ON_ONCE(1);
  648. rc = -ENXIO;
  649. goto out;
  650. }
  651. for_each_clear_bit_le(slot, free, nslot) {
  652. nd_label = nd_label_base(ndd) + slot;
  653. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  654. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  655. continue;
  656. res = to_resource(ndd, nd_label);
  657. res->flags &= ~DPA_RESOURCE_ADJUSTED;
  658. dev_vdbg(&nsblk->common.dev, "assign label[%d] slot: %d\n",
  659. l, slot);
  660. nd_mapping->labels[l++] = nd_label;
  661. }
  662. nd_mapping->labels[l] = NULL;
  663. out:
  664. kfree(old_res_list);
  665. kfree(victim_map);
  666. return rc;
  667. abort:
  668. /*
  669. * 1/ repair the allocated label bitmap in the index
  670. * 2/ restore the resource list
  671. */
  672. nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
  673. kfree(nsblk->res);
  674. nsblk->res = old_res_list;
  675. nsblk->num_resources = old_num_resources;
  676. old_res_list = NULL;
  677. goto out;
  678. }
  679. static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
  680. {
  681. int i, l, old_num_labels = 0;
  682. struct nd_namespace_index *nsindex;
  683. struct nd_namespace_label *nd_label;
  684. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  685. size_t size = (num_labels + 1) * sizeof(struct nd_namespace_label *);
  686. for_each_label(l, nd_label, nd_mapping->labels)
  687. old_num_labels++;
  688. /*
  689. * We need to preserve all the old labels for the mapping so
  690. * they can be garbage collected after writing the new labels.
  691. */
  692. if (num_labels > old_num_labels) {
  693. struct nd_namespace_label **labels;
  694. labels = krealloc(nd_mapping->labels, size, GFP_KERNEL);
  695. if (!labels)
  696. return -ENOMEM;
  697. nd_mapping->labels = labels;
  698. }
  699. if (!nd_mapping->labels)
  700. return -ENOMEM;
  701. for (i = old_num_labels; i <= num_labels; i++)
  702. nd_mapping->labels[i] = NULL;
  703. if (ndd->ns_current == -1 || ndd->ns_next == -1)
  704. /* pass */;
  705. else
  706. return max(num_labels, old_num_labels);
  707. nsindex = to_namespace_index(ndd, 0);
  708. memset(nsindex, 0, ndd->nsarea.config_size);
  709. for (i = 0; i < 2; i++) {
  710. int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
  711. if (rc)
  712. return rc;
  713. }
  714. ndd->ns_next = 1;
  715. ndd->ns_current = 0;
  716. return max(num_labels, old_num_labels);
  717. }
  718. static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
  719. {
  720. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  721. struct nd_namespace_label *nd_label;
  722. struct nd_namespace_index *nsindex;
  723. u8 label_uuid[NSLABEL_UUID_LEN];
  724. int l, num_freed = 0;
  725. unsigned long *free;
  726. u32 nslot, slot;
  727. if (!uuid)
  728. return 0;
  729. /* no index || no labels == nothing to delete */
  730. if (!preamble_next(ndd, &nsindex, &free, &nslot)
  731. || !nd_mapping->labels)
  732. return 0;
  733. for_each_label(l, nd_label, nd_mapping->labels) {
  734. memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  735. if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
  736. continue;
  737. slot = to_slot(ndd, nd_label);
  738. nd_label_free_slot(ndd, slot);
  739. dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
  740. del_label(nd_mapping, l);
  741. num_freed++;
  742. l--; /* retry with new label at this index */
  743. }
  744. if (num_freed > l) {
  745. /*
  746. * num_freed will only ever be > l when we delete the last
  747. * label
  748. */
  749. kfree(nd_mapping->labels);
  750. nd_mapping->labels = NULL;
  751. dev_dbg(ndd->dev, "%s: no more labels\n", __func__);
  752. }
  753. return nd_label_write_index(ndd, ndd->ns_next,
  754. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  755. }
  756. int nd_pmem_namespace_label_update(struct nd_region *nd_region,
  757. struct nd_namespace_pmem *nspm, resource_size_t size)
  758. {
  759. int i;
  760. for (i = 0; i < nd_region->ndr_mappings; i++) {
  761. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  762. int rc;
  763. if (size == 0) {
  764. rc = del_labels(nd_mapping, nspm->uuid);
  765. if (rc)
  766. return rc;
  767. continue;
  768. }
  769. rc = init_labels(nd_mapping, 1);
  770. if (rc < 0)
  771. return rc;
  772. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
  773. if (rc)
  774. return rc;
  775. }
  776. return 0;
  777. }
  778. int nd_blk_namespace_label_update(struct nd_region *nd_region,
  779. struct nd_namespace_blk *nsblk, resource_size_t size)
  780. {
  781. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  782. struct resource *res;
  783. int count = 0;
  784. if (size == 0)
  785. return del_labels(nd_mapping, nsblk->uuid);
  786. for_each_dpa_resource(to_ndd(nd_mapping), res)
  787. count++;
  788. count = init_labels(nd_mapping, count);
  789. if (count < 0)
  790. return count;
  791. return __blk_label_update(nd_region, nd_mapping, nsblk, count);
  792. }