blob: 87796f840777067e8594a2fb52ec6a4241ed83b7 [file] [log] [blame]
Dan Williams4a826c82015-06-09 16:09:36 -04001/*
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>
Dan Williamsb3fde742017-06-04 10:18:39 +090015#include <linux/uuid.h>
Dan Williamsf524bf22015-05-30 12:36:02 -040016#include <linux/slab.h>
Dan Williams4a826c82015-06-09 16:09:36 -040017#include <linux/io.h>
18#include <linux/nd.h>
19#include "nd-core.h"
20#include "label.h"
21#include "nd.h"
22
Dan Williamsb3fde742017-06-04 10:18:39 +090023static guid_t nvdimm_btt_guid;
Vishal Verma14e49452017-06-28 14:25:00 -060024static guid_t nvdimm_btt2_guid;
Dan Williamsb3fde742017-06-04 10:18:39 +090025static guid_t nvdimm_pfn_guid;
26static guid_t nvdimm_dax_guid;
27
Dan Williams4a826c82015-06-09 16:09:36 -040028static u32 best_seq(u32 a, u32 b)
29{
30 a &= NSINDEX_SEQ_MASK;
31 b &= NSINDEX_SEQ_MASK;
32
33 if (a == 0 || a == b)
34 return b;
35 else if (b == 0)
36 return a;
37 else if (nd_inc_seq(a) == b)
38 return b;
39 else
40 return a;
41}
42
Dan Williams564e8712017-06-03 18:30:43 +090043unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
44{
45 return ndd->nslabel_size;
46}
47
Dan Williams4a826c82015-06-09 16:09:36 -040048size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
49{
50 u32 index_span;
51
52 if (ndd->nsindex_size)
53 return ndd->nsindex_size;
54
55 /*
56 * The minimum index space is 512 bytes, with that amount of
57 * index we can describe ~1400 labels which is less than a byte
58 * of overhead per label. Round up to a byte of overhead per
59 * label and determine the size of the index region. Yes, this
60 * starts to waste space at larger config_sizes, but it's
61 * unlikely we'll ever see anything but 128K.
62 */
Dan Williams564e8712017-06-03 18:30:43 +090063 index_span = ndd->nsarea.config_size / (sizeof_namespace_label(ndd) + 1);
Dan Williams4a826c82015-06-09 16:09:36 -040064 index_span /= NSINDEX_ALIGN * 2;
65 ndd->nsindex_size = index_span * NSINDEX_ALIGN;
66
67 return ndd->nsindex_size;
68}
69
Dan Williams0ba1c632015-05-30 12:35:36 -040070int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
Dan Williamsf524bf22015-05-30 12:36:02 -040071{
Dan Williams564e8712017-06-03 18:30:43 +090072 return ndd->nsarea.config_size / (sizeof_namespace_label(ndd) + 1);
Dan Williamsf524bf22015-05-30 12:36:02 -040073}
74
Dan Williams564e8712017-06-03 18:30:43 +090075static int __nd_label_validate(struct nvdimm_drvdata *ndd)
Dan Williams4a826c82015-06-09 16:09:36 -040076{
77 /*
78 * On media label format consists of two index blocks followed
79 * by an array of labels. None of these structures are ever
80 * updated in place. A sequence number tracks the current
81 * active index and the next one to write, while labels are
82 * written to free slots.
83 *
84 * +------------+
85 * | |
86 * | nsindex0 |
87 * | |
88 * +------------+
89 * | |
90 * | nsindex1 |
91 * | |
92 * +------------+
93 * | label0 |
94 * +------------+
95 * | label1 |
96 * +------------+
97 * | |
98 * ....nslot...
99 * | |
100 * +------------+
101 * | labelN |
102 * +------------+
103 */
104 struct nd_namespace_index *nsindex[] = {
105 to_namespace_index(ndd, 0),
106 to_namespace_index(ndd, 1),
107 };
108 const int num_index = ARRAY_SIZE(nsindex);
109 struct device *dev = ndd->dev;
110 bool valid[2] = { 0 };
111 int i, num_valid = 0;
112 u32 seq;
113
114 for (i = 0; i < num_index; i++) {
115 u32 nslot;
116 u8 sig[NSINDEX_SIG_LEN];
117 u64 sum_save, sum, size;
Dan Williams564e8712017-06-03 18:30:43 +0900118 unsigned int version, labelsize;
Dan Williams4a826c82015-06-09 16:09:36 -0400119
120 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
121 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
122 dev_dbg(dev, "%s: nsindex%d signature invalid\n",
123 __func__, i);
124 continue;
125 }
Dan Williams564e8712017-06-03 18:30:43 +0900126
127 /* label sizes larger than 128 arrived with v1.2 */
128 version = __le16_to_cpu(nsindex[i]->major) * 100
129 + __le16_to_cpu(nsindex[i]->minor);
130 if (version >= 102)
131 labelsize = 1 << (7 + nsindex[i]->labelsize);
132 else
133 labelsize = 128;
134
135 if (labelsize != sizeof_namespace_label(ndd)) {
136 dev_dbg(dev, "%s: nsindex%d labelsize %d invalid\n",
137 __func__, i, nsindex[i]->labelsize);
138 continue;
139 }
140
Dan Williams4a826c82015-06-09 16:09:36 -0400141 sum_save = __le64_to_cpu(nsindex[i]->checksum);
142 nsindex[i]->checksum = __cpu_to_le64(0);
143 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
144 nsindex[i]->checksum = __cpu_to_le64(sum_save);
145 if (sum != sum_save) {
146 dev_dbg(dev, "%s: nsindex%d checksum invalid\n",
147 __func__, i);
148 continue;
149 }
150
151 seq = __le32_to_cpu(nsindex[i]->seq);
152 if ((seq & NSINDEX_SEQ_MASK) == 0) {
153 dev_dbg(dev, "%s: nsindex%d sequence: %#x invalid\n",
154 __func__, i, seq);
155 continue;
156 }
157
158 /* sanity check the index against expected values */
159 if (__le64_to_cpu(nsindex[i]->myoff)
160 != i * sizeof_namespace_index(ndd)) {
161 dev_dbg(dev, "%s: nsindex%d myoff: %#llx invalid\n",
162 __func__, i, (unsigned long long)
163 __le64_to_cpu(nsindex[i]->myoff));
164 continue;
165 }
166 if (__le64_to_cpu(nsindex[i]->otheroff)
167 != (!i) * sizeof_namespace_index(ndd)) {
168 dev_dbg(dev, "%s: nsindex%d otheroff: %#llx invalid\n",
169 __func__, i, (unsigned long long)
170 __le64_to_cpu(nsindex[i]->otheroff));
171 continue;
172 }
173
174 size = __le64_to_cpu(nsindex[i]->mysize);
175 if (size > sizeof_namespace_index(ndd)
176 || size < sizeof(struct nd_namespace_index)) {
177 dev_dbg(dev, "%s: nsindex%d mysize: %#llx invalid\n",
178 __func__, i, size);
179 continue;
180 }
181
182 nslot = __le32_to_cpu(nsindex[i]->nslot);
Dan Williams564e8712017-06-03 18:30:43 +0900183 if (nslot * sizeof_namespace_label(ndd)
Dan Williams4a826c82015-06-09 16:09:36 -0400184 + 2 * sizeof_namespace_index(ndd)
185 > ndd->nsarea.config_size) {
186 dev_dbg(dev, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
187 __func__, i, nslot,
188 ndd->nsarea.config_size);
189 continue;
190 }
191 valid[i] = true;
192 num_valid++;
193 }
194
195 switch (num_valid) {
196 case 0:
197 break;
198 case 1:
199 for (i = 0; i < num_index; i++)
200 if (valid[i])
201 return i;
202 /* can't have num_valid > 0 but valid[] = { false, false } */
203 WARN_ON(1);
204 break;
205 default:
206 /* pick the best index... */
207 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
208 __le32_to_cpu(nsindex[1]->seq));
209 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
210 return 1;
211 else
212 return 0;
213 break;
214 }
215
216 return -1;
217}
218
Dan Williams564e8712017-06-03 18:30:43 +0900219int nd_label_validate(struct nvdimm_drvdata *ndd)
220{
221 /*
222 * In order to probe for and validate namespace index blocks we
223 * need to know the size of the labels, and we can't trust the
224 * size of the labels until we validate the index blocks.
225 * Resolve this dependency loop by probing for known label
Dan Williams8990cdf2017-06-07 10:19:46 -0700226 * sizes, but default to v1.2 256-byte namespace labels if
227 * discovery fails.
Dan Williams564e8712017-06-03 18:30:43 +0900228 */
Dan Williams8990cdf2017-06-07 10:19:46 -0700229 int label_size[] = { 128, 256 };
Dan Williams564e8712017-06-03 18:30:43 +0900230 int i, rc;
231
232 for (i = 0; i < ARRAY_SIZE(label_size); i++) {
233 ndd->nslabel_size = label_size[i];
234 rc = __nd_label_validate(ndd);
235 if (rc >= 0)
236 return rc;
237 }
238
239 return -1;
240}
241
Dan Williams4a826c82015-06-09 16:09:36 -0400242void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
243 struct nd_namespace_index *src)
244{
245 if (dst && src)
246 /* pass */;
247 else
248 return;
249
250 memcpy(dst, src, sizeof_namespace_index(ndd));
251}
252
253static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
254{
255 void *base = to_namespace_index(ndd, 0);
256
257 return base + 2 * sizeof_namespace_index(ndd);
258}
259
Dan Williamsf524bf22015-05-30 12:36:02 -0400260static int to_slot(struct nvdimm_drvdata *ndd,
261 struct nd_namespace_label *nd_label)
262{
Dan Williams564e8712017-06-03 18:30:43 +0900263 unsigned long label, base;
264
265 label = (unsigned long) nd_label;
266 base = (unsigned long) nd_label_base(ndd);
267
268 return (label - base) / sizeof_namespace_label(ndd);
269}
270
271static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
272{
273 unsigned long label, base;
274
275 base = (unsigned long) nd_label_base(ndd);
276 label = base + sizeof_namespace_label(ndd) * slot;
277
278 return (struct nd_namespace_label *) label;
Dan Williamsf524bf22015-05-30 12:36:02 -0400279}
280
Dan Williams4a826c82015-06-09 16:09:36 -0400281#define for_each_clear_bit_le(bit, addr, size) \
282 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
283 (bit) < (size); \
284 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
285
286/**
Dan Williamsf524bf22015-05-30 12:36:02 -0400287 * preamble_index - common variable initialization for nd_label_* routines
Dan Williams4a826c82015-06-09 16:09:36 -0400288 * @ndd: dimm container for the relevant label set
Dan Williamsf524bf22015-05-30 12:36:02 -0400289 * @idx: namespace_index index
Dan Williams4a826c82015-06-09 16:09:36 -0400290 * @nsindex_out: on return set to the currently active namespace index
291 * @free: on return set to the free label bitmap in the index
292 * @nslot: on return set to the number of slots in the label space
293 */
Dan Williamsf524bf22015-05-30 12:36:02 -0400294static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
Dan Williams4a826c82015-06-09 16:09:36 -0400295 struct nd_namespace_index **nsindex_out,
296 unsigned long **free, u32 *nslot)
297{
298 struct nd_namespace_index *nsindex;
299
Dan Williamsf524bf22015-05-30 12:36:02 -0400300 nsindex = to_namespace_index(ndd, idx);
Dan Williams4a826c82015-06-09 16:09:36 -0400301 if (nsindex == NULL)
302 return false;
303
304 *free = (unsigned long *) nsindex->free;
305 *nslot = __le32_to_cpu(nsindex->nslot);
306 *nsindex_out = nsindex;
307
308 return true;
309}
310
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400311char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
Dan Williams4a826c82015-06-09 16:09:36 -0400312{
313 if (!label_id || !uuid)
314 return NULL;
315 snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
316 flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
317 return label_id->id;
318}
319
Dan Williamsf524bf22015-05-30 12:36:02 -0400320static bool preamble_current(struct nvdimm_drvdata *ndd,
321 struct nd_namespace_index **nsindex,
322 unsigned long **free, u32 *nslot)
323{
324 return preamble_index(ndd, ndd->ns_current, nsindex,
325 free, nslot);
326}
327
328static bool preamble_next(struct nvdimm_drvdata *ndd,
329 struct nd_namespace_index **nsindex,
330 unsigned long **free, u32 *nslot)
331{
332 return preamble_index(ndd, ndd->ns_next, nsindex,
333 free, nslot);
334}
335
Dan Williams355d8382017-06-06 14:56:43 -0700336static bool slot_valid(struct nvdimm_drvdata *ndd,
337 struct nd_namespace_label *nd_label, u32 slot)
Dan Williams4a826c82015-06-09 16:09:36 -0400338{
339 /* check that we are written where we expect to be written */
340 if (slot != __le32_to_cpu(nd_label->slot))
341 return false;
342
343 /* check that DPA allocations are page aligned */
344 if ((__le64_to_cpu(nd_label->dpa)
345 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
346 return false;
347
Dan Williams355d8382017-06-06 14:56:43 -0700348 /* check checksum */
349 if (namespace_label_has(ndd, checksum)) {
350 u64 sum, sum_save;
351
352 sum_save = __le64_to_cpu(nd_label->checksum);
353 nd_label->checksum = __cpu_to_le64(0);
354 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
355 nd_label->checksum = __cpu_to_le64(sum_save);
356 if (sum != sum_save) {
357 dev_dbg(ndd->dev, "%s fail checksum. slot: %d expect: %#llx\n",
358 __func__, slot, sum);
359 return false;
360 }
361 }
362
Dan Williams4a826c82015-06-09 16:09:36 -0400363 return true;
364}
365
366int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
367{
368 struct nd_namespace_index *nsindex;
369 unsigned long *free;
370 u32 nslot, slot;
371
372 if (!preamble_current(ndd, &nsindex, &free, &nslot))
373 return 0; /* no label, nothing to reserve */
374
375 for_each_clear_bit_le(slot, free, nslot) {
376 struct nd_namespace_label *nd_label;
377 struct nd_region *nd_region = NULL;
378 u8 label_uuid[NSLABEL_UUID_LEN];
379 struct nd_label_id label_id;
380 struct resource *res;
381 u32 flags;
382
Dan Williams564e8712017-06-03 18:30:43 +0900383 nd_label = to_label(ndd, slot);
Dan Williams4a826c82015-06-09 16:09:36 -0400384
Dan Williams355d8382017-06-06 14:56:43 -0700385 if (!slot_valid(ndd, nd_label, slot))
Dan Williams4a826c82015-06-09 16:09:36 -0400386 continue;
387
388 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
389 flags = __le32_to_cpu(nd_label->flags);
390 nd_label_gen_id(&label_id, label_uuid, flags);
391 res = nvdimm_allocate_dpa(ndd, &label_id,
392 __le64_to_cpu(nd_label->dpa),
393 __le64_to_cpu(nd_label->rawsize));
394 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
395 if (!res)
396 return -EBUSY;
397 }
398
399 return 0;
400}
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400401
402int nd_label_active_count(struct nvdimm_drvdata *ndd)
403{
404 struct nd_namespace_index *nsindex;
405 unsigned long *free;
406 u32 nslot, slot;
407 int count = 0;
408
409 if (!preamble_current(ndd, &nsindex, &free, &nslot))
410 return 0;
411
412 for_each_clear_bit_le(slot, free, nslot) {
413 struct nd_namespace_label *nd_label;
414
Dan Williams564e8712017-06-03 18:30:43 +0900415 nd_label = to_label(ndd, slot);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400416
Dan Williams355d8382017-06-06 14:56:43 -0700417 if (!slot_valid(ndd, nd_label, slot)) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400418 u32 label_slot = __le32_to_cpu(nd_label->slot);
419 u64 size = __le64_to_cpu(nd_label->rawsize);
420 u64 dpa = __le64_to_cpu(nd_label->dpa);
421
422 dev_dbg(ndd->dev,
423 "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
424 __func__, slot, label_slot, dpa, size);
425 continue;
426 }
427 count++;
428 }
429 return count;
430}
431
432struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
433{
434 struct nd_namespace_index *nsindex;
435 unsigned long *free;
436 u32 nslot, slot;
437
438 if (!preamble_current(ndd, &nsindex, &free, &nslot))
439 return NULL;
440
441 for_each_clear_bit_le(slot, free, nslot) {
442 struct nd_namespace_label *nd_label;
443
Dan Williams564e8712017-06-03 18:30:43 +0900444 nd_label = to_label(ndd, slot);
Dan Williams355d8382017-06-06 14:56:43 -0700445 if (!slot_valid(ndd, nd_label, slot))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400446 continue;
447
448 if (n-- == 0)
Dan Williams564e8712017-06-03 18:30:43 +0900449 return to_label(ndd, slot);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400450 }
451
452 return NULL;
453}
Dan Williamsf524bf22015-05-30 12:36:02 -0400454
Dan Williams0ba1c632015-05-30 12:35:36 -0400455u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
Dan Williamsf524bf22015-05-30 12:36:02 -0400456{
457 struct nd_namespace_index *nsindex;
458 unsigned long *free;
459 u32 nslot, slot;
460
461 if (!preamble_next(ndd, &nsindex, &free, &nslot))
462 return UINT_MAX;
463
464 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
465
466 slot = find_next_bit_le(free, nslot, 0);
467 if (slot == nslot)
468 return UINT_MAX;
469
470 clear_bit_le(slot, free);
471
472 return slot;
473}
474
Dan Williams0ba1c632015-05-30 12:35:36 -0400475bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
Dan Williamsf524bf22015-05-30 12:36:02 -0400476{
477 struct nd_namespace_index *nsindex;
478 unsigned long *free;
479 u32 nslot;
480
481 if (!preamble_next(ndd, &nsindex, &free, &nslot))
482 return false;
483
484 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
485
486 if (slot < nslot)
487 return !test_and_set_bit_le(slot, free);
488 return false;
489}
490
491u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
492{
493 struct nd_namespace_index *nsindex;
494 unsigned long *free;
495 u32 nslot;
496
497 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
498
499 if (!preamble_next(ndd, &nsindex, &free, &nslot))
Dan Williams0ba1c632015-05-30 12:35:36 -0400500 return nvdimm_num_label_slots(ndd);
Dan Williamsf524bf22015-05-30 12:36:02 -0400501
502 return bitmap_weight(free, nslot);
503}
504
505static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
506 unsigned long flags)
507{
508 struct nd_namespace_index *nsindex;
509 unsigned long offset;
510 u64 checksum;
511 u32 nslot;
512 int rc;
513
514 nsindex = to_namespace_index(ndd, index);
515 if (flags & ND_NSINDEX_INIT)
516 nslot = nvdimm_num_label_slots(ndd);
517 else
518 nslot = __le32_to_cpu(nsindex->nslot);
519
520 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
Dan Williams564e8712017-06-03 18:30:43 +0900521 memset(&nsindex->flags, 0, 3);
522 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
Dan Williamsf524bf22015-05-30 12:36:02 -0400523 nsindex->seq = __cpu_to_le32(seq);
524 offset = (unsigned long) nsindex
525 - (unsigned long) to_namespace_index(ndd, 0);
526 nsindex->myoff = __cpu_to_le64(offset);
527 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
528 offset = (unsigned long) to_namespace_index(ndd,
529 nd_label_next_nsindex(index))
530 - (unsigned long) to_namespace_index(ndd, 0);
531 nsindex->otheroff = __cpu_to_le64(offset);
532 offset = (unsigned long) nd_label_base(ndd)
533 - (unsigned long) to_namespace_index(ndd, 0);
534 nsindex->labeloff = __cpu_to_le64(offset);
535 nsindex->nslot = __cpu_to_le32(nslot);
536 nsindex->major = __cpu_to_le16(1);
Dan Williams8990cdf2017-06-07 10:19:46 -0700537 if (sizeof_namespace_label(ndd) < 256)
538 nsindex->minor = __cpu_to_le16(1);
539 else
540 nsindex->minor = __cpu_to_le16(2);
Dan Williamsf524bf22015-05-30 12:36:02 -0400541 nsindex->checksum = __cpu_to_le64(0);
542 if (flags & ND_NSINDEX_INIT) {
543 unsigned long *free = (unsigned long *) nsindex->free;
544 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
545 int last_bits, i;
546
547 memset(nsindex->free, 0xff, nfree / 8);
548 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
549 clear_bit_le(nslot + i, free);
550 }
551 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
552 nsindex->checksum = __cpu_to_le64(checksum);
553 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
554 nsindex, sizeof_namespace_index(ndd));
555 if (rc < 0)
556 return rc;
557
558 if (flags & ND_NSINDEX_INIT)
559 return 0;
560
561 /* copy the index we just wrote to the new 'next' */
562 WARN_ON(index != ndd->ns_next);
563 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
564 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
565 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
566 WARN_ON(ndd->ns_current == ndd->ns_next);
567
568 return 0;
569}
570
571static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
572 struct nd_namespace_label *nd_label)
573{
574 return (unsigned long) nd_label
575 - (unsigned long) to_namespace_index(ndd, 0);
576}
577
Dan Williamsb3fde742017-06-04 10:18:39 +0900578enum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid)
579{
580 if (guid_equal(guid, &nvdimm_btt_guid))
581 return NVDIMM_CCLASS_BTT;
Vishal Verma14e49452017-06-28 14:25:00 -0600582 else if (guid_equal(guid, &nvdimm_btt2_guid))
583 return NVDIMM_CCLASS_BTT2;
Dan Williamsb3fde742017-06-04 10:18:39 +0900584 else if (guid_equal(guid, &nvdimm_pfn_guid))
585 return NVDIMM_CCLASS_PFN;
586 else if (guid_equal(guid, &nvdimm_dax_guid))
587 return NVDIMM_CCLASS_DAX;
588 else if (guid_equal(guid, &guid_null))
589 return NVDIMM_CCLASS_NONE;
590
591 return NVDIMM_CCLASS_UNKNOWN;
592}
593
594static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
595 guid_t *target)
596{
597 if (claim_class == NVDIMM_CCLASS_BTT)
598 return &nvdimm_btt_guid;
Vishal Verma14e49452017-06-28 14:25:00 -0600599 else if (claim_class == NVDIMM_CCLASS_BTT2)
600 return &nvdimm_btt2_guid;
Dan Williamsb3fde742017-06-04 10:18:39 +0900601 else if (claim_class == NVDIMM_CCLASS_PFN)
602 return &nvdimm_pfn_guid;
603 else if (claim_class == NVDIMM_CCLASS_DAX)
604 return &nvdimm_dax_guid;
605 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
606 /*
607 * If we're modifying a namespace for which we don't
608 * know the claim_class, don't touch the existing guid.
609 */
610 return target;
611 } else
612 return &guid_null;
613}
614
Dan Williamsf524bf22015-05-30 12:36:02 -0400615static int __pmem_label_update(struct nd_region *nd_region,
616 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
617 int pos)
618{
Dan Williamsb3fde742017-06-04 10:18:39 +0900619 struct nd_namespace_common *ndns = &nspm->nsio.common;
Dan Williamsfaec6f82017-06-06 11:10:51 -0700620 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williamsf524bf22015-05-30 12:36:02 -0400621 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams16660ea2016-10-05 21:13:23 -0700622 struct nd_label_ent *label_ent, *victim = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -0400623 struct nd_namespace_label *nd_label;
624 struct nd_namespace_index *nsindex;
Dan Williams16660ea2016-10-05 21:13:23 -0700625 struct nd_label_id label_id;
626 struct resource *res;
Dan Williamsf524bf22015-05-30 12:36:02 -0400627 unsigned long *free;
628 u32 nslot, slot;
629 size_t offset;
Dan Williamsc12c48c2017-06-04 10:59:15 +0900630 u64 cookie;
Dan Williamsf524bf22015-05-30 12:36:02 -0400631 int rc;
632
633 if (!preamble_next(ndd, &nsindex, &free, &nslot))
634 return -ENXIO;
635
Dan Williamsc12c48c2017-06-04 10:59:15 +0900636 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
Dan Williams16660ea2016-10-05 21:13:23 -0700637 nd_label_gen_id(&label_id, nspm->uuid, 0);
638 for_each_dpa_resource(ndd, res)
639 if (strcmp(res->name, label_id.id) == 0)
640 break;
641
642 if (!res) {
643 WARN_ON_ONCE(1);
644 return -ENXIO;
645 }
646
Dan Williamsf524bf22015-05-30 12:36:02 -0400647 /* allocate and write the label to the staging (next) index */
648 slot = nd_label_alloc_slot(ndd);
649 if (slot == UINT_MAX)
650 return -ENXIO;
651 dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
652
Dan Williams564e8712017-06-03 18:30:43 +0900653 nd_label = to_label(ndd, slot);
654 memset(nd_label, 0, sizeof_namespace_label(ndd));
Dan Williamsf524bf22015-05-30 12:36:02 -0400655 memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
656 if (nspm->alt_name)
657 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
658 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
659 nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
660 nd_label->position = __cpu_to_le16(pos);
661 nd_label->isetcookie = __cpu_to_le64(cookie);
Dan Williams16660ea2016-10-05 21:13:23 -0700662 nd_label->rawsize = __cpu_to_le64(resource_size(res));
Dan Williams2de51482017-07-03 16:30:44 -0700663 nd_label->lbasize = __cpu_to_le64(nspm->lbasize);
Dan Williams16660ea2016-10-05 21:13:23 -0700664 nd_label->dpa = __cpu_to_le64(res->start);
Dan Williamsf524bf22015-05-30 12:36:02 -0400665 nd_label->slot = __cpu_to_le32(slot);
Dan Williamsfaec6f82017-06-06 11:10:51 -0700666 if (namespace_label_has(ndd, type_guid))
667 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
Dan Williamsb3fde742017-06-04 10:18:39 +0900668 if (namespace_label_has(ndd, abstraction_guid))
669 guid_copy(&nd_label->abstraction_guid,
670 to_abstraction_guid(ndns->claim_class,
671 &nd_label->abstraction_guid));
Dan Williams355d8382017-06-06 14:56:43 -0700672 if (namespace_label_has(ndd, checksum)) {
673 u64 sum;
674
675 nd_label->checksum = __cpu_to_le64(0);
676 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
677 nd_label->checksum = __cpu_to_le64(sum);
678 }
Dan Williams16660ea2016-10-05 21:13:23 -0700679 nd_dbg_dpa(nd_region, ndd, res, "%s\n", __func__);
Dan Williamsf524bf22015-05-30 12:36:02 -0400680
681 /* update label */
682 offset = nd_label_offset(ndd, nd_label);
683 rc = nvdimm_set_config_data(ndd, offset, nd_label,
Dan Williams564e8712017-06-03 18:30:43 +0900684 sizeof_namespace_label(ndd));
Dan Williamsf524bf22015-05-30 12:36:02 -0400685 if (rc < 0)
686 return rc;
687
688 /* Garbage collect the previous label */
Dan Williamsae8219f2016-09-19 16:04:21 -0700689 mutex_lock(&nd_mapping->lock);
Dan Williams16660ea2016-10-05 21:13:23 -0700690 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
691 if (!label_ent->label)
692 continue;
693 if (memcmp(nspm->uuid, label_ent->label->uuid,
694 NSLABEL_UUID_LEN) != 0)
695 continue;
696 victim = label_ent;
697 list_move_tail(&victim->list, &nd_mapping->labels);
698 break;
699 }
700 if (victim) {
Dan Williamsf524bf22015-05-30 12:36:02 -0400701 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
Dan Williams16660ea2016-10-05 21:13:23 -0700702 slot = to_slot(ndd, victim->label);
703 nd_label_free_slot(ndd, slot);
704 victim->label = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -0400705 }
706
707 /* update index */
708 rc = nd_label_write_index(ndd, ndd->ns_next,
709 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
Dan Williams16660ea2016-10-05 21:13:23 -0700710 if (rc == 0) {
711 list_for_each_entry(label_ent, &nd_mapping->labels, list)
712 if (!label_ent->label) {
713 label_ent->label = nd_label;
714 nd_label = NULL;
715 break;
716 }
717 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
718 "failed to track label: %d\n",
719 to_slot(ndd, nd_label));
720 if (nd_label)
721 rc = -ENXIO;
722 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700723 mutex_unlock(&nd_mapping->lock);
Dan Williamsf524bf22015-05-30 12:36:02 -0400724
Dan Williamsae8219f2016-09-19 16:04:21 -0700725 return rc;
Dan Williams0ba1c632015-05-30 12:35:36 -0400726}
727
728static bool is_old_resource(struct resource *res, struct resource **list, int n)
Dan Williamsf524bf22015-05-30 12:36:02 -0400729{
730 int i;
Dan Williams0ba1c632015-05-30 12:35:36 -0400731
732 if (res->flags & DPA_RESOURCE_ADJUSTED)
733 return false;
734 for (i = 0; i < n; i++)
735 if (res == list[i])
736 return true;
737 return false;
738}
739
740static struct resource *to_resource(struct nvdimm_drvdata *ndd,
741 struct nd_namespace_label *nd_label)
742{
743 struct resource *res;
744
745 for_each_dpa_resource(ndd, res) {
746 if (res->start != __le64_to_cpu(nd_label->dpa))
747 continue;
748 if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
749 continue;
750 return res;
751 }
752
753 return NULL;
754}
755
756/*
757 * 1/ Account all the labels that can be freed after this update
758 * 2/ Allocate and write the label to the staging (next) index
759 * 3/ Record the resources in the namespace device
760 */
761static int __blk_label_update(struct nd_region *nd_region,
762 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
763 int num_labels)
764{
Dan Williamsae8219f2016-09-19 16:04:21 -0700765 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
Dan Williamsfaec6f82017-06-06 11:10:51 -0700766 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williamsb3fde742017-06-04 10:18:39 +0900767 struct nd_namespace_common *ndns = &nsblk->common;
Dan Williamsf524bf22015-05-30 12:36:02 -0400768 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams0ba1c632015-05-30 12:35:36 -0400769 struct nd_namespace_label *nd_label;
Dan Williamsae8219f2016-09-19 16:04:21 -0700770 struct nd_label_ent *label_ent, *e;
Dan Williams0ba1c632015-05-30 12:35:36 -0400771 struct nd_namespace_index *nsindex;
772 unsigned long *free, *victim_map = NULL;
773 struct resource *res, **old_res_list;
774 struct nd_label_id label_id;
775 u8 uuid[NSLABEL_UUID_LEN];
Dan Williams3934d842017-06-06 14:59:04 -0700776 int min_dpa_idx = 0;
Dan Williamsae8219f2016-09-19 16:04:21 -0700777 LIST_HEAD(list);
Dan Williams0ba1c632015-05-30 12:35:36 -0400778 u32 nslot, slot;
Dan Williamsf524bf22015-05-30 12:36:02 -0400779
Dan Williams0ba1c632015-05-30 12:35:36 -0400780 if (!preamble_next(ndd, &nsindex, &free, &nslot))
781 return -ENXIO;
Dan Williamsf524bf22015-05-30 12:36:02 -0400782
Dan Williams0ba1c632015-05-30 12:35:36 -0400783 old_res_list = nsblk->res;
784 nfree = nd_label_nfree(ndd);
785 old_num_resources = nsblk->num_resources;
786 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
787
788 /*
789 * We need to loop over the old resources a few times, which seems a
790 * bit inefficient, but we need to know that we have the label
791 * space before we start mutating the tracking structures.
792 * Otherwise the recovery method of last resort for userspace is
793 * disable and re-enable the parent region.
794 */
795 alloc = 0;
796 for_each_dpa_resource(ndd, res) {
797 if (strcmp(res->name, label_id.id) != 0)
798 continue;
799 if (!is_old_resource(res, old_res_list, old_num_resources))
800 alloc++;
801 }
802
803 victims = 0;
804 if (old_num_resources) {
805 /* convert old local-label-map to dimm-slot victim-map */
806 victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
807 GFP_KERNEL);
808 if (!victim_map)
809 return -ENOMEM;
810
811 /* mark unused labels for garbage collection */
812 for_each_clear_bit_le(slot, free, nslot) {
Dan Williams564e8712017-06-03 18:30:43 +0900813 nd_label = to_label(ndd, slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400814 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
815 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
816 continue;
817 res = to_resource(ndd, nd_label);
818 if (res && is_old_resource(res, old_res_list,
819 old_num_resources))
820 continue;
821 slot = to_slot(ndd, nd_label);
822 set_bit(slot, victim_map);
823 victims++;
824 }
825 }
826
827 /* don't allow updates that consume the last label */
828 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400829 dev_info(&nsblk->common.dev, "insufficient label space\n");
Dan Williams0ba1c632015-05-30 12:35:36 -0400830 kfree(victim_map);
831 return -ENOSPC;
832 }
833 /* from here on we need to abort on error */
834
835
836 /* assign all resources to the namespace before writing the labels */
837 nsblk->res = NULL;
838 nsblk->num_resources = 0;
839 for_each_dpa_resource(ndd, res) {
840 if (strcmp(res->name, label_id.id) != 0)
841 continue;
842 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
843 rc = -ENOMEM;
844 goto abort;
845 }
846 }
847
Dan Williams3934d842017-06-06 14:59:04 -0700848 /*
849 * Find the resource associated with the first label in the set
850 * per the v1.2 namespace specification.
851 */
852 for (i = 0; i < nsblk->num_resources; i++) {
853 struct resource *min = nsblk->res[min_dpa_idx];
854
855 res = nsblk->res[i];
856 if (res->start < min->start)
857 min_dpa_idx = i;
858 }
859
Dan Williams0ba1c632015-05-30 12:35:36 -0400860 for (i = 0; i < nsblk->num_resources; i++) {
861 size_t offset;
862
863 res = nsblk->res[i];
864 if (is_old_resource(res, old_res_list, old_num_resources))
865 continue; /* carry-over */
866 slot = nd_label_alloc_slot(ndd);
867 if (slot == UINT_MAX)
868 goto abort;
869 dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
870
Dan Williams564e8712017-06-03 18:30:43 +0900871 nd_label = to_label(ndd, slot);
872 memset(nd_label, 0, sizeof_namespace_label(ndd));
Dan Williams0ba1c632015-05-30 12:35:36 -0400873 memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
874 if (nsblk->alt_name)
875 memcpy(nd_label->name, nsblk->alt_name,
876 NSLABEL_NAME_LEN);
877 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
Dan Williams8f2bc242017-06-06 11:39:30 -0700878
879 /*
880 * Use the presence of the type_guid as a flag to
Dan Williams3934d842017-06-06 14:59:04 -0700881 * determine isetcookie usage and nlabel + position
882 * policy for blk-aperture namespaces.
Dan Williams8f2bc242017-06-06 11:39:30 -0700883 */
Dan Williams3934d842017-06-06 14:59:04 -0700884 if (namespace_label_has(ndd, type_guid)) {
885 if (i == min_dpa_idx) {
886 nd_label->nlabel = __cpu_to_le16(nsblk->num_resources);
887 nd_label->position = __cpu_to_le16(0);
888 } else {
889 nd_label->nlabel = __cpu_to_le16(0xffff);
890 nd_label->position = __cpu_to_le16(0xffff);
891 }
Dan Williams8f2bc242017-06-06 11:39:30 -0700892 nd_label->isetcookie = __cpu_to_le64(nd_set->cookie2);
Dan Williams3934d842017-06-06 14:59:04 -0700893 } else {
894 nd_label->nlabel = __cpu_to_le16(0); /* N/A */
895 nd_label->position = __cpu_to_le16(0); /* N/A */
Dan Williams8f2bc242017-06-06 11:39:30 -0700896 nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
Dan Williams3934d842017-06-06 14:59:04 -0700897 }
Dan Williams8f2bc242017-06-06 11:39:30 -0700898
Dan Williams0ba1c632015-05-30 12:35:36 -0400899 nd_label->dpa = __cpu_to_le64(res->start);
900 nd_label->rawsize = __cpu_to_le64(resource_size(res));
901 nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
902 nd_label->slot = __cpu_to_le32(slot);
Dan Williamsfaec6f82017-06-06 11:10:51 -0700903 if (namespace_label_has(ndd, type_guid))
904 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
Dan Williamsb3fde742017-06-04 10:18:39 +0900905 if (namespace_label_has(ndd, abstraction_guid))
906 guid_copy(&nd_label->abstraction_guid,
907 to_abstraction_guid(ndns->claim_class,
908 &nd_label->abstraction_guid));
909
Dan Williams355d8382017-06-06 14:56:43 -0700910 if (namespace_label_has(ndd, checksum)) {
911 u64 sum;
912
913 nd_label->checksum = __cpu_to_le64(0);
914 sum = nd_fletcher64(nd_label,
915 sizeof_namespace_label(ndd), 1);
916 nd_label->checksum = __cpu_to_le64(sum);
917 }
Dan Williams0ba1c632015-05-30 12:35:36 -0400918
919 /* update label */
920 offset = nd_label_offset(ndd, nd_label);
921 rc = nvdimm_set_config_data(ndd, offset, nd_label,
Dan Williams564e8712017-06-03 18:30:43 +0900922 sizeof_namespace_label(ndd));
Dan Williams0ba1c632015-05-30 12:35:36 -0400923 if (rc < 0)
924 goto abort;
925 }
926
927 /* free up now unused slots in the new index */
928 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
929 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
930 nd_label_free_slot(ndd, slot);
931 }
932
933 /* update index */
934 rc = nd_label_write_index(ndd, ndd->ns_next,
935 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
936 if (rc)
937 goto abort;
938
939 /*
940 * Now that the on-dimm labels are up to date, fix up the tracking
941 * entries in nd_mapping->labels
942 */
943 nlabel = 0;
Dan Williamsae8219f2016-09-19 16:04:21 -0700944 mutex_lock(&nd_mapping->lock);
945 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
946 nd_label = label_ent->label;
947 if (!nd_label)
948 continue;
Dan Williams0ba1c632015-05-30 12:35:36 -0400949 nlabel++;
950 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
951 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
952 continue;
953 nlabel--;
Dan Williamsae8219f2016-09-19 16:04:21 -0700954 list_move(&label_ent->list, &list);
955 label_ent->label = NULL;
Dan Williams0ba1c632015-05-30 12:35:36 -0400956 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700957 list_splice_tail_init(&list, &nd_mapping->labels);
958 mutex_unlock(&nd_mapping->lock);
959
Dan Williams0ba1c632015-05-30 12:35:36 -0400960 if (nlabel + nsblk->num_resources > num_labels) {
961 /*
962 * Bug, we can't end up with more resources than
963 * available labels
964 */
965 WARN_ON_ONCE(1);
966 rc = -ENXIO;
967 goto out;
968 }
969
Dan Williamsae8219f2016-09-19 16:04:21 -0700970 mutex_lock(&nd_mapping->lock);
971 label_ent = list_first_entry_or_null(&nd_mapping->labels,
972 typeof(*label_ent), list);
973 if (!label_ent) {
974 WARN_ON(1);
975 mutex_unlock(&nd_mapping->lock);
976 rc = -ENXIO;
977 goto out;
978 }
Dan Williams0ba1c632015-05-30 12:35:36 -0400979 for_each_clear_bit_le(slot, free, nslot) {
Dan Williams564e8712017-06-03 18:30:43 +0900980 nd_label = to_label(ndd, slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400981 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
982 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
983 continue;
984 res = to_resource(ndd, nd_label);
985 res->flags &= ~DPA_RESOURCE_ADJUSTED;
Dan Williamsae8219f2016-09-19 16:04:21 -0700986 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
987 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
988 if (label_ent->label)
989 continue;
990 label_ent->label = nd_label;
991 nd_label = NULL;
992 break;
993 }
994 if (nd_label)
995 dev_WARN(&nsblk->common.dev,
996 "failed to track label slot%d\n", slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400997 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700998 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400999
1000 out:
1001 kfree(old_res_list);
1002 kfree(victim_map);
1003 return rc;
1004
1005 abort:
1006 /*
1007 * 1/ repair the allocated label bitmap in the index
1008 * 2/ restore the resource list
1009 */
1010 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
1011 kfree(nsblk->res);
1012 nsblk->res = old_res_list;
1013 nsblk->num_resources = old_num_resources;
1014 old_res_list = NULL;
1015 goto out;
1016}
1017
1018static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
1019{
Dan Williamsae8219f2016-09-19 16:04:21 -07001020 int i, old_num_labels = 0;
1021 struct nd_label_ent *label_ent;
Dan Williams0ba1c632015-05-30 12:35:36 -04001022 struct nd_namespace_index *nsindex;
Dan Williams0ba1c632015-05-30 12:35:36 -04001023 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams0ba1c632015-05-30 12:35:36 -04001024
Dan Williamsae8219f2016-09-19 16:04:21 -07001025 mutex_lock(&nd_mapping->lock);
1026 list_for_each_entry(label_ent, &nd_mapping->labels, list)
Dan Williams0ba1c632015-05-30 12:35:36 -04001027 old_num_labels++;
Dan Williamsae8219f2016-09-19 16:04:21 -07001028 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -04001029
1030 /*
1031 * We need to preserve all the old labels for the mapping so
1032 * they can be garbage collected after writing the new labels.
1033 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001034 for (i = old_num_labels; i < num_labels; i++) {
1035 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
1036 if (!label_ent)
Dan Williams0ba1c632015-05-30 12:35:36 -04001037 return -ENOMEM;
Dan Williamsae8219f2016-09-19 16:04:21 -07001038 mutex_lock(&nd_mapping->lock);
1039 list_add_tail(&label_ent->list, &nd_mapping->labels);
1040 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -04001041 }
Dan Williams0ba1c632015-05-30 12:35:36 -04001042
Dan Williamsf524bf22015-05-30 12:36:02 -04001043 if (ndd->ns_current == -1 || ndd->ns_next == -1)
1044 /* pass */;
1045 else
Dan Williams0ba1c632015-05-30 12:35:36 -04001046 return max(num_labels, old_num_labels);
Dan Williamsf524bf22015-05-30 12:36:02 -04001047
1048 nsindex = to_namespace_index(ndd, 0);
1049 memset(nsindex, 0, ndd->nsarea.config_size);
1050 for (i = 0; i < 2; i++) {
1051 int rc = nd_label_write_index(ndd, i, i*2, ND_NSINDEX_INIT);
1052
1053 if (rc)
1054 return rc;
1055 }
1056 ndd->ns_next = 1;
1057 ndd->ns_current = 0;
1058
Dan Williams0ba1c632015-05-30 12:35:36 -04001059 return max(num_labels, old_num_labels);
Dan Williamsf524bf22015-05-30 12:36:02 -04001060}
1061
1062static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
1063{
1064 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001065 struct nd_label_ent *label_ent, *e;
Dan Williamsf524bf22015-05-30 12:36:02 -04001066 struct nd_namespace_index *nsindex;
1067 u8 label_uuid[NSLABEL_UUID_LEN];
Dan Williamsf524bf22015-05-30 12:36:02 -04001068 unsigned long *free;
Dan Williamsae8219f2016-09-19 16:04:21 -07001069 LIST_HEAD(list);
Dan Williamsf524bf22015-05-30 12:36:02 -04001070 u32 nslot, slot;
Dan Williamsae8219f2016-09-19 16:04:21 -07001071 int active = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -04001072
1073 if (!uuid)
1074 return 0;
1075
1076 /* no index || no labels == nothing to delete */
Dan Williamsae8219f2016-09-19 16:04:21 -07001077 if (!preamble_next(ndd, &nsindex, &free, &nslot))
Dan Williamsf524bf22015-05-30 12:36:02 -04001078 return 0;
1079
Dan Williamsae8219f2016-09-19 16:04:21 -07001080 mutex_lock(&nd_mapping->lock);
1081 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1082 struct nd_namespace_label *nd_label = label_ent->label;
1083
1084 if (!nd_label)
1085 continue;
1086 active++;
Dan Williamsf524bf22015-05-30 12:36:02 -04001087 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1088 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
1089 continue;
Dan Williamsae8219f2016-09-19 16:04:21 -07001090 active--;
Dan Williamsf524bf22015-05-30 12:36:02 -04001091 slot = to_slot(ndd, nd_label);
1092 nd_label_free_slot(ndd, slot);
1093 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
Dan Williamsae8219f2016-09-19 16:04:21 -07001094 list_move_tail(&label_ent->list, &list);
1095 label_ent->label = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -04001096 }
Dan Williamsae8219f2016-09-19 16:04:21 -07001097 list_splice_tail_init(&list, &nd_mapping->labels);
Dan Williamsf524bf22015-05-30 12:36:02 -04001098
Dan Williamsae8219f2016-09-19 16:04:21 -07001099 if (active == 0) {
1100 nd_mapping_free_labels(nd_mapping);
1101 dev_dbg(ndd->dev, "%s: no more active labels\n", __func__);
Dan Williamsf524bf22015-05-30 12:36:02 -04001102 }
Dan Williamsae8219f2016-09-19 16:04:21 -07001103 mutex_unlock(&nd_mapping->lock);
Dan Williamsf524bf22015-05-30 12:36:02 -04001104
1105 return nd_label_write_index(ndd, ndd->ns_next,
1106 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1107}
1108
1109int nd_pmem_namespace_label_update(struct nd_region *nd_region,
1110 struct nd_namespace_pmem *nspm, resource_size_t size)
1111{
1112 int i;
1113
1114 for (i = 0; i < nd_region->ndr_mappings; i++) {
1115 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams16660ea2016-10-05 21:13:23 -07001116 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1117 struct resource *res;
1118 int rc, count = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -04001119
1120 if (size == 0) {
1121 rc = del_labels(nd_mapping, nspm->uuid);
1122 if (rc)
1123 return rc;
1124 continue;
1125 }
1126
Dan Williams16660ea2016-10-05 21:13:23 -07001127 for_each_dpa_resource(ndd, res)
Nicolas Iooss2d9a0272016-10-29 13:28:52 +02001128 if (strncmp(res->name, "pmem", 4) == 0)
Dan Williams16660ea2016-10-05 21:13:23 -07001129 count++;
1130 WARN_ON_ONCE(!count);
1131
1132 rc = init_labels(nd_mapping, count);
Dan Williams0ba1c632015-05-30 12:35:36 -04001133 if (rc < 0)
Dan Williamsf524bf22015-05-30 12:36:02 -04001134 return rc;
1135
1136 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
1137 if (rc)
1138 return rc;
1139 }
1140
1141 return 0;
1142}
Dan Williams0ba1c632015-05-30 12:35:36 -04001143
1144int nd_blk_namespace_label_update(struct nd_region *nd_region,
1145 struct nd_namespace_blk *nsblk, resource_size_t size)
1146{
1147 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1148 struct resource *res;
1149 int count = 0;
1150
1151 if (size == 0)
1152 return del_labels(nd_mapping, nsblk->uuid);
1153
1154 for_each_dpa_resource(to_ndd(nd_mapping), res)
1155 count++;
1156
1157 count = init_labels(nd_mapping, count);
1158 if (count < 0)
1159 return count;
1160
1161 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
1162}
Dan Williamsb3fde742017-06-04 10:18:39 +09001163
1164int __init nd_label_init(void)
1165{
1166 WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
Vishal Verma14e49452017-06-28 14:25:00 -06001167 WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
Dan Williamsb3fde742017-06-04 10:18:39 +09001168 WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
1169 WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
1170
1171 return 0;
1172}