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