label.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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);
  424. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  425. struct nd_label_ent *label_ent, *victim = NULL;
  426. struct nd_namespace_label *nd_label;
  427. struct nd_namespace_index *nsindex;
  428. struct nd_label_id label_id;
  429. struct resource *res;
  430. unsigned long *free;
  431. u32 nslot, slot;
  432. size_t offset;
  433. int rc;
  434. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  435. return -ENXIO;
  436. nd_label_gen_id(&label_id, nspm->uuid, 0);
  437. for_each_dpa_resource(ndd, res)
  438. if (strcmp(res->name, label_id.id) == 0)
  439. break;
  440. if (!res) {
  441. WARN_ON_ONCE(1);
  442. return -ENXIO;
  443. }
  444. /* allocate and write the label to the staging (next) index */
  445. slot = nd_label_alloc_slot(ndd);
  446. if (slot == UINT_MAX)
  447. return -ENXIO;
  448. dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
  449. nd_label = nd_label_base(ndd) + slot;
  450. memset(nd_label, 0, sizeof(struct nd_namespace_label));
  451. memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
  452. if (nspm->alt_name)
  453. memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
  454. nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
  455. nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
  456. nd_label->position = __cpu_to_le16(pos);
  457. nd_label->isetcookie = __cpu_to_le64(cookie);
  458. nd_label->rawsize = __cpu_to_le64(resource_size(res));
  459. nd_label->dpa = __cpu_to_le64(res->start);
  460. nd_label->slot = __cpu_to_le32(slot);
  461. nd_dbg_dpa(nd_region, ndd, res, "%s\n", __func__);
  462. /* update label */
  463. offset = nd_label_offset(ndd, nd_label);
  464. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  465. sizeof(struct nd_namespace_label));
  466. if (rc < 0)
  467. return rc;
  468. /* Garbage collect the previous label */
  469. mutex_lock(&nd_mapping->lock);
  470. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  471. if (!label_ent->label)
  472. continue;
  473. if (memcmp(nspm->uuid, label_ent->label->uuid,
  474. NSLABEL_UUID_LEN) != 0)
  475. continue;
  476. victim = label_ent;
  477. list_move_tail(&victim->list, &nd_mapping->labels);
  478. break;
  479. }
  480. if (victim) {
  481. dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
  482. slot = to_slot(ndd, victim->label);
  483. nd_label_free_slot(ndd, slot);
  484. victim->label = NULL;
  485. }
  486. /* update index */
  487. rc = nd_label_write_index(ndd, ndd->ns_next,
  488. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  489. if (rc == 0) {
  490. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  491. if (!label_ent->label) {
  492. label_ent->label = nd_label;
  493. nd_label = NULL;
  494. break;
  495. }
  496. dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
  497. "failed to track label: %d\n",
  498. to_slot(ndd, nd_label));
  499. if (nd_label)
  500. rc = -ENXIO;
  501. }
  502. mutex_unlock(&nd_mapping->lock);
  503. return rc;
  504. }
  505. static bool is_old_resource(struct resource *res, struct resource **list, int n)
  506. {
  507. int i;
  508. if (res->flags & DPA_RESOURCE_ADJUSTED)
  509. return false;
  510. for (i = 0; i < n; i++)
  511. if (res == list[i])
  512. return true;
  513. return false;
  514. }
  515. static struct resource *to_resource(struct nvdimm_drvdata *ndd,
  516. struct nd_namespace_label *nd_label)
  517. {
  518. struct resource *res;
  519. for_each_dpa_resource(ndd, res) {
  520. if (res->start != __le64_to_cpu(nd_label->dpa))
  521. continue;
  522. if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
  523. continue;
  524. return res;
  525. }
  526. return NULL;
  527. }
  528. /*
  529. * 1/ Account all the labels that can be freed after this update
  530. * 2/ Allocate and write the label to the staging (next) index
  531. * 3/ Record the resources in the namespace device
  532. */
  533. static int __blk_label_update(struct nd_region *nd_region,
  534. struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
  535. int num_labels)
  536. {
  537. int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
  538. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  539. struct nd_namespace_label *nd_label;
  540. struct nd_label_ent *label_ent, *e;
  541. struct nd_namespace_index *nsindex;
  542. unsigned long *free, *victim_map = NULL;
  543. struct resource *res, **old_res_list;
  544. struct nd_label_id label_id;
  545. u8 uuid[NSLABEL_UUID_LEN];
  546. LIST_HEAD(list);
  547. u32 nslot, slot;
  548. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  549. return -ENXIO;
  550. old_res_list = nsblk->res;
  551. nfree = nd_label_nfree(ndd);
  552. old_num_resources = nsblk->num_resources;
  553. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  554. /*
  555. * We need to loop over the old resources a few times, which seems a
  556. * bit inefficient, but we need to know that we have the label
  557. * space before we start mutating the tracking structures.
  558. * Otherwise the recovery method of last resort for userspace is
  559. * disable and re-enable the parent region.
  560. */
  561. alloc = 0;
  562. for_each_dpa_resource(ndd, res) {
  563. if (strcmp(res->name, label_id.id) != 0)
  564. continue;
  565. if (!is_old_resource(res, old_res_list, old_num_resources))
  566. alloc++;
  567. }
  568. victims = 0;
  569. if (old_num_resources) {
  570. /* convert old local-label-map to dimm-slot victim-map */
  571. victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
  572. GFP_KERNEL);
  573. if (!victim_map)
  574. return -ENOMEM;
  575. /* mark unused labels for garbage collection */
  576. for_each_clear_bit_le(slot, free, nslot) {
  577. nd_label = nd_label_base(ndd) + slot;
  578. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  579. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  580. continue;
  581. res = to_resource(ndd, nd_label);
  582. if (res && is_old_resource(res, old_res_list,
  583. old_num_resources))
  584. continue;
  585. slot = to_slot(ndd, nd_label);
  586. set_bit(slot, victim_map);
  587. victims++;
  588. }
  589. }
  590. /* don't allow updates that consume the last label */
  591. if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
  592. dev_info(&nsblk->common.dev, "insufficient label space\n");
  593. kfree(victim_map);
  594. return -ENOSPC;
  595. }
  596. /* from here on we need to abort on error */
  597. /* assign all resources to the namespace before writing the labels */
  598. nsblk->res = NULL;
  599. nsblk->num_resources = 0;
  600. for_each_dpa_resource(ndd, res) {
  601. if (strcmp(res->name, label_id.id) != 0)
  602. continue;
  603. if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
  604. rc = -ENOMEM;
  605. goto abort;
  606. }
  607. }
  608. for (i = 0; i < nsblk->num_resources; i++) {
  609. size_t offset;
  610. res = nsblk->res[i];
  611. if (is_old_resource(res, old_res_list, old_num_resources))
  612. continue; /* carry-over */
  613. slot = nd_label_alloc_slot(ndd);
  614. if (slot == UINT_MAX)
  615. goto abort;
  616. dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
  617. nd_label = nd_label_base(ndd) + slot;
  618. memset(nd_label, 0, sizeof(struct nd_namespace_label));
  619. memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
  620. if (nsblk->alt_name)
  621. memcpy(nd_label->name, nsblk->alt_name,
  622. NSLABEL_NAME_LEN);
  623. nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
  624. nd_label->nlabel = __cpu_to_le16(0); /* N/A */
  625. nd_label->position = __cpu_to_le16(0); /* N/A */
  626. nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
  627. nd_label->dpa = __cpu_to_le64(res->start);
  628. nd_label->rawsize = __cpu_to_le64(resource_size(res));
  629. nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
  630. nd_label->slot = __cpu_to_le32(slot);
  631. /* update label */
  632. offset = nd_label_offset(ndd, nd_label);
  633. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  634. sizeof(struct nd_namespace_label));
  635. if (rc < 0)
  636. goto abort;
  637. }
  638. /* free up now unused slots in the new index */
  639. for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
  640. dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
  641. nd_label_free_slot(ndd, slot);
  642. }
  643. /* update index */
  644. rc = nd_label_write_index(ndd, ndd->ns_next,
  645. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  646. if (rc)
  647. goto abort;
  648. /*
  649. * Now that the on-dimm labels are up to date, fix up the tracking
  650. * entries in nd_mapping->labels
  651. */
  652. nlabel = 0;
  653. mutex_lock(&nd_mapping->lock);
  654. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  655. nd_label = label_ent->label;
  656. if (!nd_label)
  657. continue;
  658. nlabel++;
  659. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  660. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  661. continue;
  662. nlabel--;
  663. list_move(&label_ent->list, &list);
  664. label_ent->label = NULL;
  665. }
  666. list_splice_tail_init(&list, &nd_mapping->labels);
  667. mutex_unlock(&nd_mapping->lock);
  668. if (nlabel + nsblk->num_resources > num_labels) {
  669. /*
  670. * Bug, we can't end up with more resources than
  671. * available labels
  672. */
  673. WARN_ON_ONCE(1);
  674. rc = -ENXIO;
  675. goto out;
  676. }
  677. mutex_lock(&nd_mapping->lock);
  678. label_ent = list_first_entry_or_null(&nd_mapping->labels,
  679. typeof(*label_ent), list);
  680. if (!label_ent) {
  681. WARN_ON(1);
  682. mutex_unlock(&nd_mapping->lock);
  683. rc = -ENXIO;
  684. goto out;
  685. }
  686. for_each_clear_bit_le(slot, free, nslot) {
  687. nd_label = nd_label_base(ndd) + slot;
  688. memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  689. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
  690. continue;
  691. res = to_resource(ndd, nd_label);
  692. res->flags &= ~DPA_RESOURCE_ADJUSTED;
  693. dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
  694. list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
  695. if (label_ent->label)
  696. continue;
  697. label_ent->label = nd_label;
  698. nd_label = NULL;
  699. break;
  700. }
  701. if (nd_label)
  702. dev_WARN(&nsblk->common.dev,
  703. "failed to track label slot%d\n", slot);
  704. }
  705. mutex_unlock(&nd_mapping->lock);
  706. out:
  707. kfree(old_res_list);
  708. kfree(victim_map);
  709. return rc;
  710. abort:
  711. /*
  712. * 1/ repair the allocated label bitmap in the index
  713. * 2/ restore the resource list
  714. */
  715. nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
  716. kfree(nsblk->res);
  717. nsblk->res = old_res_list;
  718. nsblk->num_resources = old_num_resources;
  719. old_res_list = NULL;
  720. goto out;
  721. }
  722. static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
  723. {
  724. int i, old_num_labels = 0;
  725. struct nd_label_ent *label_ent;
  726. struct nd_namespace_index *nsindex;
  727. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  728. mutex_lock(&nd_mapping->lock);
  729. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  730. old_num_labels++;
  731. mutex_unlock(&nd_mapping->lock);
  732. /*
  733. * We need to preserve all the old labels for the mapping so
  734. * they can be garbage collected after writing the new labels.
  735. */
  736. for (i = old_num_labels; i < num_labels; i++) {
  737. label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
  738. if (!label_ent)
  739. return -ENOMEM;
  740. mutex_lock(&nd_mapping->lock);
  741. list_add_tail(&label_ent->list, &nd_mapping->labels);
  742. mutex_unlock(&nd_mapping->lock);
  743. }
  744. if (ndd->ns_current == -1 || ndd->ns_next == -1)
  745. /* pass */;
  746. else
  747. return max(num_labels, old_num_labels);
  748. nsindex = to_namespace_index(ndd, 0);
  749. memset(nsindex, 0, ndd->nsarea.config_size);
  750. for (i = 0; i < 2; i++) {
  751. int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
  752. if (rc)
  753. return rc;
  754. }
  755. ndd->ns_next = 1;
  756. ndd->ns_current = 0;
  757. return max(num_labels, old_num_labels);
  758. }
  759. static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
  760. {
  761. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  762. struct nd_label_ent *label_ent, *e;
  763. struct nd_namespace_index *nsindex;
  764. u8 label_uuid[NSLABEL_UUID_LEN];
  765. unsigned long *free;
  766. LIST_HEAD(list);
  767. u32 nslot, slot;
  768. int active = 0;
  769. if (!uuid)
  770. return 0;
  771. /* no index || no labels == nothing to delete */
  772. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  773. return 0;
  774. mutex_lock(&nd_mapping->lock);
  775. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  776. struct nd_namespace_label *nd_label = label_ent->label;
  777. if (!nd_label)
  778. continue;
  779. active++;
  780. memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
  781. if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
  782. continue;
  783. active--;
  784. slot = to_slot(ndd, nd_label);
  785. nd_label_free_slot(ndd, slot);
  786. dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
  787. list_move_tail(&label_ent->list, &list);
  788. label_ent->label = NULL;
  789. }
  790. list_splice_tail_init(&list, &nd_mapping->labels);
  791. if (active == 0) {
  792. nd_mapping_free_labels(nd_mapping);
  793. dev_dbg(ndd->dev, "%s: no more active labels\n", __func__);
  794. }
  795. mutex_unlock(&nd_mapping->lock);
  796. return nd_label_write_index(ndd, ndd->ns_next,
  797. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  798. }
  799. int nd_pmem_namespace_label_update(struct nd_region *nd_region,
  800. struct nd_namespace_pmem *nspm, resource_size_t size)
  801. {
  802. int i;
  803. for (i = 0; i < nd_region->ndr_mappings; i++) {
  804. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  805. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  806. struct resource *res;
  807. int rc, count = 0;
  808. if (size == 0) {
  809. rc = del_labels(nd_mapping, nspm->uuid);
  810. if (rc)
  811. return rc;
  812. continue;
  813. }
  814. for_each_dpa_resource(ndd, res)
  815. if (strncmp(res->name, "pmem", 3) == 0)
  816. count++;
  817. WARN_ON_ONCE(!count);
  818. rc = init_labels(nd_mapping, count);
  819. if (rc < 0)
  820. return rc;
  821. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
  822. if (rc)
  823. return rc;
  824. }
  825. return 0;
  826. }
  827. int nd_blk_namespace_label_update(struct nd_region *nd_region,
  828. struct nd_namespace_blk *nsblk, resource_size_t size)
  829. {
  830. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  831. struct resource *res;
  832. int count = 0;
  833. if (size == 0)
  834. return del_labels(nd_mapping, nsblk->uuid);
  835. for_each_dpa_resource(to_ndd(nd_mapping), res)
  836. count++;
  837. count = init_labels(nd_mapping, count);
  838. if (count < 0)
  839. return count;
  840. return __blk_label_update(nd_region, nd_mapping, nsblk, count);
  841. }