blob: d6233d220bfd72d9c6de8c682b041ac2da0c43c9 [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 Williamsf524bf22015-05-30 12:36:02 -040015#include <linux/slab.h>
Dan Williams4a826c82015-06-09 16:09:36 -040016#include <linux/io.h>
17#include <linux/nd.h>
18#include "nd-core.h"
19#include "label.h"
20#include "nd.h"
21
22static u32 best_seq(u32 a, u32 b)
23{
24 a &= NSINDEX_SEQ_MASK;
25 b &= NSINDEX_SEQ_MASK;
26
27 if (a == 0 || a == b)
28 return b;
29 else if (b == 0)
30 return a;
31 else if (nd_inc_seq(a) == b)
32 return b;
33 else
34 return a;
35}
36
Dan Williams564e8712017-06-03 18:30:43 +090037unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
38{
39 return ndd->nslabel_size;
40}
41
Dan Williams4a826c82015-06-09 16:09:36 -040042size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
43{
44 u32 index_span;
45
46 if (ndd->nsindex_size)
47 return ndd->nsindex_size;
48
49 /*
50 * The minimum index space is 512 bytes, with that amount of
51 * index we can describe ~1400 labels which is less than a byte
52 * of overhead per label. Round up to a byte of overhead per
53 * label and determine the size of the index region. Yes, this
54 * starts to waste space at larger config_sizes, but it's
55 * unlikely we'll ever see anything but 128K.
56 */
Dan Williams564e8712017-06-03 18:30:43 +090057 index_span = ndd->nsarea.config_size / (sizeof_namespace_label(ndd) + 1);
Dan Williams4a826c82015-06-09 16:09:36 -040058 index_span /= NSINDEX_ALIGN * 2;
59 ndd->nsindex_size = index_span * NSINDEX_ALIGN;
60
61 return ndd->nsindex_size;
62}
63
Dan Williams0ba1c632015-05-30 12:35:36 -040064int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
Dan Williamsf524bf22015-05-30 12:36:02 -040065{
Dan Williams564e8712017-06-03 18:30:43 +090066 return ndd->nsarea.config_size / (sizeof_namespace_label(ndd) + 1);
Dan Williamsf524bf22015-05-30 12:36:02 -040067}
68
Dan Williams564e8712017-06-03 18:30:43 +090069static int __nd_label_validate(struct nvdimm_drvdata *ndd)
Dan Williams4a826c82015-06-09 16:09:36 -040070{
71 /*
72 * On media label format consists of two index blocks followed
73 * by an array of labels. None of these structures are ever
74 * updated in place. A sequence number tracks the current
75 * active index and the next one to write, while labels are
76 * written to free slots.
77 *
78 * +------------+
79 * | |
80 * | nsindex0 |
81 * | |
82 * +------------+
83 * | |
84 * | nsindex1 |
85 * | |
86 * +------------+
87 * | label0 |
88 * +------------+
89 * | label1 |
90 * +------------+
91 * | |
92 * ....nslot...
93 * | |
94 * +------------+
95 * | labelN |
96 * +------------+
97 */
98 struct nd_namespace_index *nsindex[] = {
99 to_namespace_index(ndd, 0),
100 to_namespace_index(ndd, 1),
101 };
102 const int num_index = ARRAY_SIZE(nsindex);
103 struct device *dev = ndd->dev;
104 bool valid[2] = { 0 };
105 int i, num_valid = 0;
106 u32 seq;
107
108 for (i = 0; i < num_index; i++) {
109 u32 nslot;
110 u8 sig[NSINDEX_SIG_LEN];
111 u64 sum_save, sum, size;
Dan Williams564e8712017-06-03 18:30:43 +0900112 unsigned int version, labelsize;
Dan Williams4a826c82015-06-09 16:09:36 -0400113
114 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
115 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
116 dev_dbg(dev, "%s: nsindex%d signature invalid\n",
117 __func__, i);
118 continue;
119 }
Dan Williams564e8712017-06-03 18:30:43 +0900120
121 /* label sizes larger than 128 arrived with v1.2 */
122 version = __le16_to_cpu(nsindex[i]->major) * 100
123 + __le16_to_cpu(nsindex[i]->minor);
124 if (version >= 102)
125 labelsize = 1 << (7 + nsindex[i]->labelsize);
126 else
127 labelsize = 128;
128
129 if (labelsize != sizeof_namespace_label(ndd)) {
130 dev_dbg(dev, "%s: nsindex%d labelsize %d invalid\n",
131 __func__, i, nsindex[i]->labelsize);
132 continue;
133 }
134
Dan Williams4a826c82015-06-09 16:09:36 -0400135 sum_save = __le64_to_cpu(nsindex[i]->checksum);
136 nsindex[i]->checksum = __cpu_to_le64(0);
137 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
138 nsindex[i]->checksum = __cpu_to_le64(sum_save);
139 if (sum != sum_save) {
140 dev_dbg(dev, "%s: nsindex%d checksum invalid\n",
141 __func__, i);
142 continue;
143 }
144
145 seq = __le32_to_cpu(nsindex[i]->seq);
146 if ((seq & NSINDEX_SEQ_MASK) == 0) {
147 dev_dbg(dev, "%s: nsindex%d sequence: %#x invalid\n",
148 __func__, i, seq);
149 continue;
150 }
151
152 /* sanity check the index against expected values */
153 if (__le64_to_cpu(nsindex[i]->myoff)
154 != i * sizeof_namespace_index(ndd)) {
155 dev_dbg(dev, "%s: nsindex%d myoff: %#llx invalid\n",
156 __func__, i, (unsigned long long)
157 __le64_to_cpu(nsindex[i]->myoff));
158 continue;
159 }
160 if (__le64_to_cpu(nsindex[i]->otheroff)
161 != (!i) * sizeof_namespace_index(ndd)) {
162 dev_dbg(dev, "%s: nsindex%d otheroff: %#llx invalid\n",
163 __func__, i, (unsigned long long)
164 __le64_to_cpu(nsindex[i]->otheroff));
165 continue;
166 }
167
168 size = __le64_to_cpu(nsindex[i]->mysize);
169 if (size > sizeof_namespace_index(ndd)
170 || size < sizeof(struct nd_namespace_index)) {
171 dev_dbg(dev, "%s: nsindex%d mysize: %#llx invalid\n",
172 __func__, i, size);
173 continue;
174 }
175
176 nslot = __le32_to_cpu(nsindex[i]->nslot);
Dan Williams564e8712017-06-03 18:30:43 +0900177 if (nslot * sizeof_namespace_label(ndd)
Dan Williams4a826c82015-06-09 16:09:36 -0400178 + 2 * sizeof_namespace_index(ndd)
179 > ndd->nsarea.config_size) {
180 dev_dbg(dev, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
181 __func__, i, nslot,
182 ndd->nsarea.config_size);
183 continue;
184 }
185 valid[i] = true;
186 num_valid++;
187 }
188
189 switch (num_valid) {
190 case 0:
191 break;
192 case 1:
193 for (i = 0; i < num_index; i++)
194 if (valid[i])
195 return i;
196 /* can't have num_valid > 0 but valid[] = { false, false } */
197 WARN_ON(1);
198 break;
199 default:
200 /* pick the best index... */
201 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
202 __le32_to_cpu(nsindex[1]->seq));
203 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
204 return 1;
205 else
206 return 0;
207 break;
208 }
209
210 return -1;
211}
212
Dan Williams564e8712017-06-03 18:30:43 +0900213int nd_label_validate(struct nvdimm_drvdata *ndd)
214{
215 /*
216 * In order to probe for and validate namespace index blocks we
217 * need to know the size of the labels, and we can't trust the
218 * size of the labels until we validate the index blocks.
219 * Resolve this dependency loop by probing for known label
220 * sizes.
221 */
222 int label_size[] = { 256, 128 };
223 int i, rc;
224
225 for (i = 0; i < ARRAY_SIZE(label_size); i++) {
226 ndd->nslabel_size = label_size[i];
227 rc = __nd_label_validate(ndd);
228 if (rc >= 0)
229 return rc;
230 }
231
232 return -1;
233}
234
Dan Williams4a826c82015-06-09 16:09:36 -0400235void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
236 struct nd_namespace_index *src)
237{
238 if (dst && src)
239 /* pass */;
240 else
241 return;
242
243 memcpy(dst, src, sizeof_namespace_index(ndd));
244}
245
246static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
247{
248 void *base = to_namespace_index(ndd, 0);
249
250 return base + 2 * sizeof_namespace_index(ndd);
251}
252
Dan Williamsf524bf22015-05-30 12:36:02 -0400253static int to_slot(struct nvdimm_drvdata *ndd,
254 struct nd_namespace_label *nd_label)
255{
Dan Williams564e8712017-06-03 18:30:43 +0900256 unsigned long label, base;
257
258 label = (unsigned long) nd_label;
259 base = (unsigned long) nd_label_base(ndd);
260
261 return (label - base) / sizeof_namespace_label(ndd);
262}
263
264static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
265{
266 unsigned long label, base;
267
268 base = (unsigned long) nd_label_base(ndd);
269 label = base + sizeof_namespace_label(ndd) * slot;
270
271 return (struct nd_namespace_label *) label;
Dan Williamsf524bf22015-05-30 12:36:02 -0400272}
273
Dan Williams4a826c82015-06-09 16:09:36 -0400274#define for_each_clear_bit_le(bit, addr, size) \
275 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
276 (bit) < (size); \
277 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
278
279/**
Dan Williamsf524bf22015-05-30 12:36:02 -0400280 * preamble_index - common variable initialization for nd_label_* routines
Dan Williams4a826c82015-06-09 16:09:36 -0400281 * @ndd: dimm container for the relevant label set
Dan Williamsf524bf22015-05-30 12:36:02 -0400282 * @idx: namespace_index index
Dan Williams4a826c82015-06-09 16:09:36 -0400283 * @nsindex_out: on return set to the currently active namespace index
284 * @free: on return set to the free label bitmap in the index
285 * @nslot: on return set to the number of slots in the label space
286 */
Dan Williamsf524bf22015-05-30 12:36:02 -0400287static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
Dan Williams4a826c82015-06-09 16:09:36 -0400288 struct nd_namespace_index **nsindex_out,
289 unsigned long **free, u32 *nslot)
290{
291 struct nd_namespace_index *nsindex;
292
Dan Williamsf524bf22015-05-30 12:36:02 -0400293 nsindex = to_namespace_index(ndd, idx);
Dan Williams4a826c82015-06-09 16:09:36 -0400294 if (nsindex == NULL)
295 return false;
296
297 *free = (unsigned long *) nsindex->free;
298 *nslot = __le32_to_cpu(nsindex->nslot);
299 *nsindex_out = nsindex;
300
301 return true;
302}
303
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400304char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
Dan Williams4a826c82015-06-09 16:09:36 -0400305{
306 if (!label_id || !uuid)
307 return NULL;
308 snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
309 flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
310 return label_id->id;
311}
312
Dan Williamsf524bf22015-05-30 12:36:02 -0400313static bool preamble_current(struct nvdimm_drvdata *ndd,
314 struct nd_namespace_index **nsindex,
315 unsigned long **free, u32 *nslot)
316{
317 return preamble_index(ndd, ndd->ns_current, nsindex,
318 free, nslot);
319}
320
321static bool preamble_next(struct nvdimm_drvdata *ndd,
322 struct nd_namespace_index **nsindex,
323 unsigned long **free, u32 *nslot)
324{
325 return preamble_index(ndd, ndd->ns_next, nsindex,
326 free, nslot);
327}
328
Dan Williams4a826c82015-06-09 16:09:36 -0400329static bool slot_valid(struct nd_namespace_label *nd_label, u32 slot)
330{
331 /* check that we are written where we expect to be written */
332 if (slot != __le32_to_cpu(nd_label->slot))
333 return false;
334
335 /* check that DPA allocations are page aligned */
336 if ((__le64_to_cpu(nd_label->dpa)
337 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
338 return false;
339
340 return true;
341}
342
343int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
344{
345 struct nd_namespace_index *nsindex;
346 unsigned long *free;
347 u32 nslot, slot;
348
349 if (!preamble_current(ndd, &nsindex, &free, &nslot))
350 return 0; /* no label, nothing to reserve */
351
352 for_each_clear_bit_le(slot, free, nslot) {
353 struct nd_namespace_label *nd_label;
354 struct nd_region *nd_region = NULL;
355 u8 label_uuid[NSLABEL_UUID_LEN];
356 struct nd_label_id label_id;
357 struct resource *res;
358 u32 flags;
359
Dan Williams564e8712017-06-03 18:30:43 +0900360 nd_label = to_label(ndd, slot);
Dan Williams4a826c82015-06-09 16:09:36 -0400361
362 if (!slot_valid(nd_label, slot))
363 continue;
364
365 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
366 flags = __le32_to_cpu(nd_label->flags);
367 nd_label_gen_id(&label_id, label_uuid, flags);
368 res = nvdimm_allocate_dpa(ndd, &label_id,
369 __le64_to_cpu(nd_label->dpa),
370 __le64_to_cpu(nd_label->rawsize));
371 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
372 if (!res)
373 return -EBUSY;
374 }
375
376 return 0;
377}
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400378
379int nd_label_active_count(struct nvdimm_drvdata *ndd)
380{
381 struct nd_namespace_index *nsindex;
382 unsigned long *free;
383 u32 nslot, slot;
384 int count = 0;
385
386 if (!preamble_current(ndd, &nsindex, &free, &nslot))
387 return 0;
388
389 for_each_clear_bit_le(slot, free, nslot) {
390 struct nd_namespace_label *nd_label;
391
Dan Williams564e8712017-06-03 18:30:43 +0900392 nd_label = to_label(ndd, slot);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400393
394 if (!slot_valid(nd_label, slot)) {
395 u32 label_slot = __le32_to_cpu(nd_label->slot);
396 u64 size = __le64_to_cpu(nd_label->rawsize);
397 u64 dpa = __le64_to_cpu(nd_label->dpa);
398
399 dev_dbg(ndd->dev,
400 "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
401 __func__, slot, label_slot, dpa, size);
402 continue;
403 }
404 count++;
405 }
406 return count;
407}
408
409struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
410{
411 struct nd_namespace_index *nsindex;
412 unsigned long *free;
413 u32 nslot, slot;
414
415 if (!preamble_current(ndd, &nsindex, &free, &nslot))
416 return NULL;
417
418 for_each_clear_bit_le(slot, free, nslot) {
419 struct nd_namespace_label *nd_label;
420
Dan Williams564e8712017-06-03 18:30:43 +0900421 nd_label = to_label(ndd, slot);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400422 if (!slot_valid(nd_label, slot))
423 continue;
424
425 if (n-- == 0)
Dan Williams564e8712017-06-03 18:30:43 +0900426 return to_label(ndd, slot);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400427 }
428
429 return NULL;
430}
Dan Williamsf524bf22015-05-30 12:36:02 -0400431
Dan Williams0ba1c632015-05-30 12:35:36 -0400432u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
Dan Williamsf524bf22015-05-30 12:36:02 -0400433{
434 struct nd_namespace_index *nsindex;
435 unsigned long *free;
436 u32 nslot, slot;
437
438 if (!preamble_next(ndd, &nsindex, &free, &nslot))
439 return UINT_MAX;
440
441 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
442
443 slot = find_next_bit_le(free, nslot, 0);
444 if (slot == nslot)
445 return UINT_MAX;
446
447 clear_bit_le(slot, free);
448
449 return slot;
450}
451
Dan Williams0ba1c632015-05-30 12:35:36 -0400452bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
Dan Williamsf524bf22015-05-30 12:36:02 -0400453{
454 struct nd_namespace_index *nsindex;
455 unsigned long *free;
456 u32 nslot;
457
458 if (!preamble_next(ndd, &nsindex, &free, &nslot))
459 return false;
460
461 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
462
463 if (slot < nslot)
464 return !test_and_set_bit_le(slot, free);
465 return false;
466}
467
468u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
469{
470 struct nd_namespace_index *nsindex;
471 unsigned long *free;
472 u32 nslot;
473
474 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
475
476 if (!preamble_next(ndd, &nsindex, &free, &nslot))
Dan Williams0ba1c632015-05-30 12:35:36 -0400477 return nvdimm_num_label_slots(ndd);
Dan Williamsf524bf22015-05-30 12:36:02 -0400478
479 return bitmap_weight(free, nslot);
480}
481
482static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
483 unsigned long flags)
484{
485 struct nd_namespace_index *nsindex;
486 unsigned long offset;
487 u64 checksum;
488 u32 nslot;
489 int rc;
490
491 nsindex = to_namespace_index(ndd, index);
492 if (flags & ND_NSINDEX_INIT)
493 nslot = nvdimm_num_label_slots(ndd);
494 else
495 nslot = __le32_to_cpu(nsindex->nslot);
496
497 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
Dan Williams564e8712017-06-03 18:30:43 +0900498 memset(&nsindex->flags, 0, 3);
499 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
Dan Williamsf524bf22015-05-30 12:36:02 -0400500 nsindex->seq = __cpu_to_le32(seq);
501 offset = (unsigned long) nsindex
502 - (unsigned long) to_namespace_index(ndd, 0);
503 nsindex->myoff = __cpu_to_le64(offset);
504 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
505 offset = (unsigned long) to_namespace_index(ndd,
506 nd_label_next_nsindex(index))
507 - (unsigned long) to_namespace_index(ndd, 0);
508 nsindex->otheroff = __cpu_to_le64(offset);
509 offset = (unsigned long) nd_label_base(ndd)
510 - (unsigned long) to_namespace_index(ndd, 0);
511 nsindex->labeloff = __cpu_to_le64(offset);
512 nsindex->nslot = __cpu_to_le32(nslot);
513 nsindex->major = __cpu_to_le16(1);
514 nsindex->minor = __cpu_to_le16(1);
515 nsindex->checksum = __cpu_to_le64(0);
516 if (flags & ND_NSINDEX_INIT) {
517 unsigned long *free = (unsigned long *) nsindex->free;
518 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
519 int last_bits, i;
520
521 memset(nsindex->free, 0xff, nfree / 8);
522 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
523 clear_bit_le(nslot + i, free);
524 }
525 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
526 nsindex->checksum = __cpu_to_le64(checksum);
527 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
528 nsindex, sizeof_namespace_index(ndd));
529 if (rc < 0)
530 return rc;
531
532 if (flags & ND_NSINDEX_INIT)
533 return 0;
534
535 /* copy the index we just wrote to the new 'next' */
536 WARN_ON(index != ndd->ns_next);
537 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
538 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
539 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
540 WARN_ON(ndd->ns_current == ndd->ns_next);
541
542 return 0;
543}
544
545static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
546 struct nd_namespace_label *nd_label)
547{
548 return (unsigned long) nd_label
549 - (unsigned long) to_namespace_index(ndd, 0);
550}
551
552static int __pmem_label_update(struct nd_region *nd_region,
553 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
554 int pos)
555{
Dan Williams16660ea2016-10-05 21:13:23 -0700556 u64 cookie = nd_region_interleave_set_cookie(nd_region);
Dan Williamsf524bf22015-05-30 12:36:02 -0400557 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams16660ea2016-10-05 21:13:23 -0700558 struct nd_label_ent *label_ent, *victim = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -0400559 struct nd_namespace_label *nd_label;
560 struct nd_namespace_index *nsindex;
Dan Williams16660ea2016-10-05 21:13:23 -0700561 struct nd_label_id label_id;
562 struct resource *res;
Dan Williamsf524bf22015-05-30 12:36:02 -0400563 unsigned long *free;
564 u32 nslot, slot;
565 size_t offset;
566 int rc;
567
568 if (!preamble_next(ndd, &nsindex, &free, &nslot))
569 return -ENXIO;
570
Dan Williams16660ea2016-10-05 21:13:23 -0700571 nd_label_gen_id(&label_id, nspm->uuid, 0);
572 for_each_dpa_resource(ndd, res)
573 if (strcmp(res->name, label_id.id) == 0)
574 break;
575
576 if (!res) {
577 WARN_ON_ONCE(1);
578 return -ENXIO;
579 }
580
Dan Williamsf524bf22015-05-30 12:36:02 -0400581 /* allocate and write the label to the staging (next) index */
582 slot = nd_label_alloc_slot(ndd);
583 if (slot == UINT_MAX)
584 return -ENXIO;
585 dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
586
Dan Williams564e8712017-06-03 18:30:43 +0900587 nd_label = to_label(ndd, slot);
588 memset(nd_label, 0, sizeof_namespace_label(ndd));
Dan Williamsf524bf22015-05-30 12:36:02 -0400589 memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
590 if (nspm->alt_name)
591 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
592 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
593 nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
594 nd_label->position = __cpu_to_le16(pos);
595 nd_label->isetcookie = __cpu_to_le64(cookie);
Dan Williams16660ea2016-10-05 21:13:23 -0700596 nd_label->rawsize = __cpu_to_le64(resource_size(res));
597 nd_label->dpa = __cpu_to_le64(res->start);
Dan Williamsf524bf22015-05-30 12:36:02 -0400598 nd_label->slot = __cpu_to_le32(slot);
Dan Williams16660ea2016-10-05 21:13:23 -0700599 nd_dbg_dpa(nd_region, ndd, res, "%s\n", __func__);
Dan Williamsf524bf22015-05-30 12:36:02 -0400600
601 /* update label */
602 offset = nd_label_offset(ndd, nd_label);
603 rc = nvdimm_set_config_data(ndd, offset, nd_label,
Dan Williams564e8712017-06-03 18:30:43 +0900604 sizeof_namespace_label(ndd));
Dan Williamsf524bf22015-05-30 12:36:02 -0400605 if (rc < 0)
606 return rc;
607
608 /* Garbage collect the previous label */
Dan Williamsae8219f2016-09-19 16:04:21 -0700609 mutex_lock(&nd_mapping->lock);
Dan Williams16660ea2016-10-05 21:13:23 -0700610 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
611 if (!label_ent->label)
612 continue;
613 if (memcmp(nspm->uuid, label_ent->label->uuid,
614 NSLABEL_UUID_LEN) != 0)
615 continue;
616 victim = label_ent;
617 list_move_tail(&victim->list, &nd_mapping->labels);
618 break;
619 }
620 if (victim) {
Dan Williamsf524bf22015-05-30 12:36:02 -0400621 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
Dan Williams16660ea2016-10-05 21:13:23 -0700622 slot = to_slot(ndd, victim->label);
623 nd_label_free_slot(ndd, slot);
624 victim->label = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -0400625 }
626
627 /* update index */
628 rc = nd_label_write_index(ndd, ndd->ns_next,
629 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
Dan Williams16660ea2016-10-05 21:13:23 -0700630 if (rc == 0) {
631 list_for_each_entry(label_ent, &nd_mapping->labels, list)
632 if (!label_ent->label) {
633 label_ent->label = nd_label;
634 nd_label = NULL;
635 break;
636 }
637 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
638 "failed to track label: %d\n",
639 to_slot(ndd, nd_label));
640 if (nd_label)
641 rc = -ENXIO;
642 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700643 mutex_unlock(&nd_mapping->lock);
Dan Williamsf524bf22015-05-30 12:36:02 -0400644
Dan Williamsae8219f2016-09-19 16:04:21 -0700645 return rc;
Dan Williams0ba1c632015-05-30 12:35:36 -0400646}
647
648static bool is_old_resource(struct resource *res, struct resource **list, int n)
Dan Williamsf524bf22015-05-30 12:36:02 -0400649{
650 int i;
Dan Williams0ba1c632015-05-30 12:35:36 -0400651
652 if (res->flags & DPA_RESOURCE_ADJUSTED)
653 return false;
654 for (i = 0; i < n; i++)
655 if (res == list[i])
656 return true;
657 return false;
658}
659
660static struct resource *to_resource(struct nvdimm_drvdata *ndd,
661 struct nd_namespace_label *nd_label)
662{
663 struct resource *res;
664
665 for_each_dpa_resource(ndd, res) {
666 if (res->start != __le64_to_cpu(nd_label->dpa))
667 continue;
668 if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
669 continue;
670 return res;
671 }
672
673 return NULL;
674}
675
676/*
677 * 1/ Account all the labels that can be freed after this update
678 * 2/ Allocate and write the label to the staging (next) index
679 * 3/ Record the resources in the namespace device
680 */
681static int __blk_label_update(struct nd_region *nd_region,
682 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
683 int num_labels)
684{
Dan Williamsae8219f2016-09-19 16:04:21 -0700685 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
Dan Williamsf524bf22015-05-30 12:36:02 -0400686 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams0ba1c632015-05-30 12:35:36 -0400687 struct nd_namespace_label *nd_label;
Dan Williamsae8219f2016-09-19 16:04:21 -0700688 struct nd_label_ent *label_ent, *e;
Dan Williams0ba1c632015-05-30 12:35:36 -0400689 struct nd_namespace_index *nsindex;
690 unsigned long *free, *victim_map = NULL;
691 struct resource *res, **old_res_list;
692 struct nd_label_id label_id;
693 u8 uuid[NSLABEL_UUID_LEN];
Dan Williamsae8219f2016-09-19 16:04:21 -0700694 LIST_HEAD(list);
Dan Williams0ba1c632015-05-30 12:35:36 -0400695 u32 nslot, slot;
Dan Williamsf524bf22015-05-30 12:36:02 -0400696
Dan Williams0ba1c632015-05-30 12:35:36 -0400697 if (!preamble_next(ndd, &nsindex, &free, &nslot))
698 return -ENXIO;
Dan Williamsf524bf22015-05-30 12:36:02 -0400699
Dan Williams0ba1c632015-05-30 12:35:36 -0400700 old_res_list = nsblk->res;
701 nfree = nd_label_nfree(ndd);
702 old_num_resources = nsblk->num_resources;
703 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
704
705 /*
706 * We need to loop over the old resources a few times, which seems a
707 * bit inefficient, but we need to know that we have the label
708 * space before we start mutating the tracking structures.
709 * Otherwise the recovery method of last resort for userspace is
710 * disable and re-enable the parent region.
711 */
712 alloc = 0;
713 for_each_dpa_resource(ndd, res) {
714 if (strcmp(res->name, label_id.id) != 0)
715 continue;
716 if (!is_old_resource(res, old_res_list, old_num_resources))
717 alloc++;
718 }
719
720 victims = 0;
721 if (old_num_resources) {
722 /* convert old local-label-map to dimm-slot victim-map */
723 victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
724 GFP_KERNEL);
725 if (!victim_map)
726 return -ENOMEM;
727
728 /* mark unused labels for garbage collection */
729 for_each_clear_bit_le(slot, free, nslot) {
Dan Williams564e8712017-06-03 18:30:43 +0900730 nd_label = to_label(ndd, slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400731 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
732 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
733 continue;
734 res = to_resource(ndd, nd_label);
735 if (res && is_old_resource(res, old_res_list,
736 old_num_resources))
737 continue;
738 slot = to_slot(ndd, nd_label);
739 set_bit(slot, victim_map);
740 victims++;
741 }
742 }
743
744 /* don't allow updates that consume the last label */
745 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400746 dev_info(&nsblk->common.dev, "insufficient label space\n");
Dan Williams0ba1c632015-05-30 12:35:36 -0400747 kfree(victim_map);
748 return -ENOSPC;
749 }
750 /* from here on we need to abort on error */
751
752
753 /* assign all resources to the namespace before writing the labels */
754 nsblk->res = NULL;
755 nsblk->num_resources = 0;
756 for_each_dpa_resource(ndd, res) {
757 if (strcmp(res->name, label_id.id) != 0)
758 continue;
759 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
760 rc = -ENOMEM;
761 goto abort;
762 }
763 }
764
765 for (i = 0; i < nsblk->num_resources; i++) {
766 size_t offset;
767
768 res = nsblk->res[i];
769 if (is_old_resource(res, old_res_list, old_num_resources))
770 continue; /* carry-over */
771 slot = nd_label_alloc_slot(ndd);
772 if (slot == UINT_MAX)
773 goto abort;
774 dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
775
Dan Williams564e8712017-06-03 18:30:43 +0900776 nd_label = to_label(ndd, slot);
777 memset(nd_label, 0, sizeof_namespace_label(ndd));
Dan Williams0ba1c632015-05-30 12:35:36 -0400778 memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
779 if (nsblk->alt_name)
780 memcpy(nd_label->name, nsblk->alt_name,
781 NSLABEL_NAME_LEN);
782 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
783 nd_label->nlabel = __cpu_to_le16(0); /* N/A */
784 nd_label->position = __cpu_to_le16(0); /* N/A */
785 nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
786 nd_label->dpa = __cpu_to_le64(res->start);
787 nd_label->rawsize = __cpu_to_le64(resource_size(res));
788 nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
789 nd_label->slot = __cpu_to_le32(slot);
790
791 /* update label */
792 offset = nd_label_offset(ndd, nd_label);
793 rc = nvdimm_set_config_data(ndd, offset, nd_label,
Dan Williams564e8712017-06-03 18:30:43 +0900794 sizeof_namespace_label(ndd));
Dan Williams0ba1c632015-05-30 12:35:36 -0400795 if (rc < 0)
796 goto abort;
797 }
798
799 /* free up now unused slots in the new index */
800 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
801 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
802 nd_label_free_slot(ndd, slot);
803 }
804
805 /* update index */
806 rc = nd_label_write_index(ndd, ndd->ns_next,
807 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
808 if (rc)
809 goto abort;
810
811 /*
812 * Now that the on-dimm labels are up to date, fix up the tracking
813 * entries in nd_mapping->labels
814 */
815 nlabel = 0;
Dan Williamsae8219f2016-09-19 16:04:21 -0700816 mutex_lock(&nd_mapping->lock);
817 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
818 nd_label = label_ent->label;
819 if (!nd_label)
820 continue;
Dan Williams0ba1c632015-05-30 12:35:36 -0400821 nlabel++;
822 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
823 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
824 continue;
825 nlabel--;
Dan Williamsae8219f2016-09-19 16:04:21 -0700826 list_move(&label_ent->list, &list);
827 label_ent->label = NULL;
Dan Williams0ba1c632015-05-30 12:35:36 -0400828 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700829 list_splice_tail_init(&list, &nd_mapping->labels);
830 mutex_unlock(&nd_mapping->lock);
831
Dan Williams0ba1c632015-05-30 12:35:36 -0400832 if (nlabel + nsblk->num_resources > num_labels) {
833 /*
834 * Bug, we can't end up with more resources than
835 * available labels
836 */
837 WARN_ON_ONCE(1);
838 rc = -ENXIO;
839 goto out;
840 }
841
Dan Williamsae8219f2016-09-19 16:04:21 -0700842 mutex_lock(&nd_mapping->lock);
843 label_ent = list_first_entry_or_null(&nd_mapping->labels,
844 typeof(*label_ent), list);
845 if (!label_ent) {
846 WARN_ON(1);
847 mutex_unlock(&nd_mapping->lock);
848 rc = -ENXIO;
849 goto out;
850 }
Dan Williams0ba1c632015-05-30 12:35:36 -0400851 for_each_clear_bit_le(slot, free, nslot) {
Dan Williams564e8712017-06-03 18:30:43 +0900852 nd_label = to_label(ndd, slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400853 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
854 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
855 continue;
856 res = to_resource(ndd, nd_label);
857 res->flags &= ~DPA_RESOURCE_ADJUSTED;
Dan Williamsae8219f2016-09-19 16:04:21 -0700858 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
859 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
860 if (label_ent->label)
861 continue;
862 label_ent->label = nd_label;
863 nd_label = NULL;
864 break;
865 }
866 if (nd_label)
867 dev_WARN(&nsblk->common.dev,
868 "failed to track label slot%d\n", slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400869 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700870 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400871
872 out:
873 kfree(old_res_list);
874 kfree(victim_map);
875 return rc;
876
877 abort:
878 /*
879 * 1/ repair the allocated label bitmap in the index
880 * 2/ restore the resource list
881 */
882 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
883 kfree(nsblk->res);
884 nsblk->res = old_res_list;
885 nsblk->num_resources = old_num_resources;
886 old_res_list = NULL;
887 goto out;
888}
889
890static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
891{
Dan Williamsae8219f2016-09-19 16:04:21 -0700892 int i, old_num_labels = 0;
893 struct nd_label_ent *label_ent;
Dan Williams0ba1c632015-05-30 12:35:36 -0400894 struct nd_namespace_index *nsindex;
Dan Williams0ba1c632015-05-30 12:35:36 -0400895 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams0ba1c632015-05-30 12:35:36 -0400896
Dan Williamsae8219f2016-09-19 16:04:21 -0700897 mutex_lock(&nd_mapping->lock);
898 list_for_each_entry(label_ent, &nd_mapping->labels, list)
Dan Williams0ba1c632015-05-30 12:35:36 -0400899 old_num_labels++;
Dan Williamsae8219f2016-09-19 16:04:21 -0700900 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400901
902 /*
903 * We need to preserve all the old labels for the mapping so
904 * they can be garbage collected after writing the new labels.
905 */
Dan Williamsae8219f2016-09-19 16:04:21 -0700906 for (i = old_num_labels; i < num_labels; i++) {
907 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
908 if (!label_ent)
Dan Williams0ba1c632015-05-30 12:35:36 -0400909 return -ENOMEM;
Dan Williamsae8219f2016-09-19 16:04:21 -0700910 mutex_lock(&nd_mapping->lock);
911 list_add_tail(&label_ent->list, &nd_mapping->labels);
912 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400913 }
Dan Williams0ba1c632015-05-30 12:35:36 -0400914
Dan Williamsf524bf22015-05-30 12:36:02 -0400915 if (ndd->ns_current == -1 || ndd->ns_next == -1)
916 /* pass */;
917 else
Dan Williams0ba1c632015-05-30 12:35:36 -0400918 return max(num_labels, old_num_labels);
Dan Williamsf524bf22015-05-30 12:36:02 -0400919
920 nsindex = to_namespace_index(ndd, 0);
921 memset(nsindex, 0, ndd->nsarea.config_size);
922 for (i = 0; i < 2; i++) {
923 int rc = nd_label_write_index(ndd, i, i*2, ND_NSINDEX_INIT);
924
925 if (rc)
926 return rc;
927 }
928 ndd->ns_next = 1;
929 ndd->ns_current = 0;
930
Dan Williams0ba1c632015-05-30 12:35:36 -0400931 return max(num_labels, old_num_labels);
Dan Williamsf524bf22015-05-30 12:36:02 -0400932}
933
934static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
935{
936 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -0700937 struct nd_label_ent *label_ent, *e;
Dan Williamsf524bf22015-05-30 12:36:02 -0400938 struct nd_namespace_index *nsindex;
939 u8 label_uuid[NSLABEL_UUID_LEN];
Dan Williamsf524bf22015-05-30 12:36:02 -0400940 unsigned long *free;
Dan Williamsae8219f2016-09-19 16:04:21 -0700941 LIST_HEAD(list);
Dan Williamsf524bf22015-05-30 12:36:02 -0400942 u32 nslot, slot;
Dan Williamsae8219f2016-09-19 16:04:21 -0700943 int active = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -0400944
945 if (!uuid)
946 return 0;
947
948 /* no index || no labels == nothing to delete */
Dan Williamsae8219f2016-09-19 16:04:21 -0700949 if (!preamble_next(ndd, &nsindex, &free, &nslot))
Dan Williamsf524bf22015-05-30 12:36:02 -0400950 return 0;
951
Dan Williamsae8219f2016-09-19 16:04:21 -0700952 mutex_lock(&nd_mapping->lock);
953 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
954 struct nd_namespace_label *nd_label = label_ent->label;
955
956 if (!nd_label)
957 continue;
958 active++;
Dan Williamsf524bf22015-05-30 12:36:02 -0400959 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
960 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
961 continue;
Dan Williamsae8219f2016-09-19 16:04:21 -0700962 active--;
Dan Williamsf524bf22015-05-30 12:36:02 -0400963 slot = to_slot(ndd, nd_label);
964 nd_label_free_slot(ndd, slot);
965 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
Dan Williamsae8219f2016-09-19 16:04:21 -0700966 list_move_tail(&label_ent->list, &list);
967 label_ent->label = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -0400968 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700969 list_splice_tail_init(&list, &nd_mapping->labels);
Dan Williamsf524bf22015-05-30 12:36:02 -0400970
Dan Williamsae8219f2016-09-19 16:04:21 -0700971 if (active == 0) {
972 nd_mapping_free_labels(nd_mapping);
973 dev_dbg(ndd->dev, "%s: no more active labels\n", __func__);
Dan Williamsf524bf22015-05-30 12:36:02 -0400974 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700975 mutex_unlock(&nd_mapping->lock);
Dan Williamsf524bf22015-05-30 12:36:02 -0400976
977 return nd_label_write_index(ndd, ndd->ns_next,
978 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
979}
980
981int nd_pmem_namespace_label_update(struct nd_region *nd_region,
982 struct nd_namespace_pmem *nspm, resource_size_t size)
983{
984 int i;
985
986 for (i = 0; i < nd_region->ndr_mappings; i++) {
987 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams16660ea2016-10-05 21:13:23 -0700988 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
989 struct resource *res;
990 int rc, count = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -0400991
992 if (size == 0) {
993 rc = del_labels(nd_mapping, nspm->uuid);
994 if (rc)
995 return rc;
996 continue;
997 }
998
Dan Williams16660ea2016-10-05 21:13:23 -0700999 for_each_dpa_resource(ndd, res)
Nicolas Iooss2d9a0272016-10-29 13:28:52 +02001000 if (strncmp(res->name, "pmem", 4) == 0)
Dan Williams16660ea2016-10-05 21:13:23 -07001001 count++;
1002 WARN_ON_ONCE(!count);
1003
1004 rc = init_labels(nd_mapping, count);
Dan Williams0ba1c632015-05-30 12:35:36 -04001005 if (rc < 0)
Dan Williamsf524bf22015-05-30 12:36:02 -04001006 return rc;
1007
1008 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
1009 if (rc)
1010 return rc;
1011 }
1012
1013 return 0;
1014}
Dan Williams0ba1c632015-05-30 12:35:36 -04001015
1016int nd_blk_namespace_label_update(struct nd_region *nd_region,
1017 struct nd_namespace_blk *nsblk, resource_size_t size)
1018{
1019 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1020 struct resource *res;
1021 int count = 0;
1022
1023 if (size == 0)
1024 return del_labels(nd_mapping, nsblk->uuid);
1025
1026 for_each_dpa_resource(to_ndd(nd_mapping), res)
1027 count++;
1028
1029 count = init_labels(nd_mapping, count);
1030 if (count < 0)
1031 return count;
1032
1033 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
1034}