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