]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/nvdimm/label.c
d6233d220bfd72d9c6de8c682b041ac2da0c43c9
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/device.h>
14 #include <linux/ndctl.h>
15 #include <linux/slab.h>
22 static u32
best_seq(u32 a
, u32 b
)
24 a
&= NSINDEX_SEQ_MASK
;
25 b
&= NSINDEX_SEQ_MASK
;
31 else if (nd_inc_seq(a
) == b
)
37 unsigned sizeof_namespace_label(struct nvdimm_drvdata
*ndd
)
39 return ndd
->nslabel_size
;
42 size_t sizeof_namespace_index(struct nvdimm_drvdata
*ndd
)
46 if (ndd
->nsindex_size
)
47 return ndd
->nsindex_size
;
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.
57 index_span
= ndd
->nsarea
.config_size
/ (sizeof_namespace_label(ndd
) + 1);
58 index_span
/= NSINDEX_ALIGN
* 2;
59 ndd
->nsindex_size
= index_span
* NSINDEX_ALIGN
;
61 return ndd
->nsindex_size
;
64 int nvdimm_num_label_slots(struct nvdimm_drvdata
*ndd
)
66 return ndd
->nsarea
.config_size
/ (sizeof_namespace_label(ndd
) + 1);
69 static int __nd_label_validate(struct nvdimm_drvdata
*ndd
)
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.
98 struct nd_namespace_index
*nsindex
[] = {
99 to_namespace_index(ndd
, 0),
100 to_namespace_index(ndd
, 1),
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;
108 for (i
= 0; i
< num_index
; i
++) {
110 u8 sig
[NSINDEX_SIG_LEN
];
111 u64 sum_save
, sum
, size
;
112 unsigned int version
, labelsize
;
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",
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
);
125 labelsize
= 1 << (7 + nsindex
[i
]->labelsize
);
129 if (labelsize
!= sizeof_namespace_label(ndd
)) {
130 dev_dbg(dev
, "%s: nsindex%d labelsize %d invalid\n",
131 __func__
, i
, nsindex
[i
]->labelsize
);
135 sum_save
= __le64_to_cpu(nsindex
[i
]->checksum
);
136 nsindex
[i
]->checksum
= __cpu_to_le64(0);
137 sum
= nd_fletcher64(nsindex
[i
], sizeof_namespace_index(ndd
), 1);
138 nsindex
[i
]->checksum
= __cpu_to_le64(sum_save
);
139 if (sum
!= sum_save
) {
140 dev_dbg(dev
, "%s: nsindex%d checksum invalid\n",
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",
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
));
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
));
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",
176 nslot
= __le32_to_cpu(nsindex
[i
]->nslot
);
177 if (nslot
* sizeof_namespace_label(ndd
)
178 + 2 * sizeof_namespace_index(ndd
)
179 > ndd
->nsarea
.config_size
) {
180 dev_dbg(dev
, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
182 ndd
->nsarea
.config_size
);
193 for (i
= 0; i
< num_index
; i
++)
196 /* can't have num_valid > 0 but valid[] = { false, false } */
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
))
213 int nd_label_validate(struct nvdimm_drvdata
*ndd
)
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
222 int label_size
[] = { 256, 128 };
225 for (i
= 0; i
< ARRAY_SIZE(label_size
); i
++) {
226 ndd
->nslabel_size
= label_size
[i
];
227 rc
= __nd_label_validate(ndd
);
235 void nd_label_copy(struct nvdimm_drvdata
*ndd
, struct nd_namespace_index
*dst
,
236 struct nd_namespace_index
*src
)
243 memcpy(dst
, src
, sizeof_namespace_index(ndd
));
246 static struct nd_namespace_label
*nd_label_base(struct nvdimm_drvdata
*ndd
)
248 void *base
= to_namespace_index(ndd
, 0);
250 return base
+ 2 * sizeof_namespace_index(ndd
);
253 static int to_slot(struct nvdimm_drvdata
*ndd
,
254 struct nd_namespace_label
*nd_label
)
256 unsigned long label
, base
;
258 label
= (unsigned long) nd_label
;
259 base
= (unsigned long) nd_label_base(ndd
);
261 return (label
- base
) / sizeof_namespace_label(ndd
);
264 static struct nd_namespace_label
*to_label(struct nvdimm_drvdata
*ndd
, int slot
)
266 unsigned long label
, base
;
268 base
= (unsigned long) nd_label_base(ndd
);
269 label
= base
+ sizeof_namespace_label(ndd
) * slot
;
271 return (struct nd_namespace_label
*) label
;
274 #define for_each_clear_bit_le(bit, addr, size) \
275 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
277 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
280 * preamble_index - common variable initialization for nd_label_* routines
281 * @ndd: dimm container for the relevant label set
282 * @idx: namespace_index index
283 * @nsindex_out: on return set to the currently active namespace index
284 * @free: on return set to the free label bitmap in the index
285 * @nslot: on return set to the number of slots in the label space
287 static bool preamble_index(struct nvdimm_drvdata
*ndd
, int idx
,
288 struct nd_namespace_index
**nsindex_out
,
289 unsigned long **free
, u32
*nslot
)
291 struct nd_namespace_index
*nsindex
;
293 nsindex
= to_namespace_index(ndd
, idx
);
297 *free
= (unsigned long *) nsindex
->free
;
298 *nslot
= __le32_to_cpu(nsindex
->nslot
);
299 *nsindex_out
= nsindex
;
304 char *nd_label_gen_id(struct nd_label_id
*label_id
, u8
*uuid
, u32 flags
)
306 if (!label_id
|| !uuid
)
308 snprintf(label_id
->id
, ND_LABEL_ID_SIZE
, "%s-%pUb",
309 flags
& NSLABEL_FLAG_LOCAL
? "blk" : "pmem", uuid
);
313 static bool preamble_current(struct nvdimm_drvdata
*ndd
,
314 struct nd_namespace_index
**nsindex
,
315 unsigned long **free
, u32
*nslot
)
317 return preamble_index(ndd
, ndd
->ns_current
, nsindex
,
321 static bool preamble_next(struct nvdimm_drvdata
*ndd
,
322 struct nd_namespace_index
**nsindex
,
323 unsigned long **free
, u32
*nslot
)
325 return preamble_index(ndd
, ndd
->ns_next
, nsindex
,
329 static bool slot_valid(struct nd_namespace_label
*nd_label
, u32 slot
)
331 /* check that we are written where we expect to be written */
332 if (slot
!= __le32_to_cpu(nd_label
->slot
))
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
)
343 int nd_label_reserve_dpa(struct nvdimm_drvdata
*ndd
)
345 struct nd_namespace_index
*nsindex
;
349 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
350 return 0; /* no label, nothing to reserve */
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
;
360 nd_label
= to_label(ndd
, slot
);
362 if (!slot_valid(nd_label
, slot
))
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");
379 int nd_label_active_count(struct nvdimm_drvdata
*ndd
)
381 struct nd_namespace_index
*nsindex
;
386 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
389 for_each_clear_bit_le(slot
, free
, nslot
) {
390 struct nd_namespace_label
*nd_label
;
392 nd_label
= to_label(ndd
, slot
);
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
);
400 "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
401 __func__
, slot
, label_slot
, dpa
, size
);
409 struct nd_namespace_label
*nd_label_active(struct nvdimm_drvdata
*ndd
, int n
)
411 struct nd_namespace_index
*nsindex
;
415 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
418 for_each_clear_bit_le(slot
, free
, nslot
) {
419 struct nd_namespace_label
*nd_label
;
421 nd_label
= to_label(ndd
, slot
);
422 if (!slot_valid(nd_label
, slot
))
426 return to_label(ndd
, slot
);
432 u32
nd_label_alloc_slot(struct nvdimm_drvdata
*ndd
)
434 struct nd_namespace_index
*nsindex
;
438 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
441 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
443 slot
= find_next_bit_le(free
, nslot
, 0);
447 clear_bit_le(slot
, free
);
452 bool nd_label_free_slot(struct nvdimm_drvdata
*ndd
, u32 slot
)
454 struct nd_namespace_index
*nsindex
;
458 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
461 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
464 return !test_and_set_bit_le(slot
, free
);
468 u32
nd_label_nfree(struct nvdimm_drvdata
*ndd
)
470 struct nd_namespace_index
*nsindex
;
474 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
476 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
477 return nvdimm_num_label_slots(ndd
);
479 return bitmap_weight(free
, nslot
);
482 static int nd_label_write_index(struct nvdimm_drvdata
*ndd
, int index
, u32 seq
,
485 struct nd_namespace_index
*nsindex
;
486 unsigned long offset
;
491 nsindex
= to_namespace_index(ndd
, index
);
492 if (flags
& ND_NSINDEX_INIT
)
493 nslot
= nvdimm_num_label_slots(ndd
);
495 nslot
= __le32_to_cpu(nsindex
->nslot
);
497 memcpy(nsindex
->sig
, NSINDEX_SIGNATURE
, NSINDEX_SIG_LEN
);
498 memset(&nsindex
->flags
, 0, 3);
499 nsindex
->labelsize
= sizeof_namespace_label(ndd
) >> 8;
500 nsindex
->seq
= __cpu_to_le32(seq
);
501 offset
= (unsigned long) nsindex
502 - (unsigned long) to_namespace_index(ndd
, 0);
503 nsindex
->myoff
= __cpu_to_le64(offset
);
504 nsindex
->mysize
= __cpu_to_le64(sizeof_namespace_index(ndd
));
505 offset
= (unsigned long) to_namespace_index(ndd
,
506 nd_label_next_nsindex(index
))
507 - (unsigned long) to_namespace_index(ndd
, 0);
508 nsindex
->otheroff
= __cpu_to_le64(offset
);
509 offset
= (unsigned long) nd_label_base(ndd
)
510 - (unsigned long) to_namespace_index(ndd
, 0);
511 nsindex
->labeloff
= __cpu_to_le64(offset
);
512 nsindex
->nslot
= __cpu_to_le32(nslot
);
513 nsindex
->major
= __cpu_to_le16(1);
514 nsindex
->minor
= __cpu_to_le16(1);
515 nsindex
->checksum
= __cpu_to_le64(0);
516 if (flags
& ND_NSINDEX_INIT
) {
517 unsigned long *free
= (unsigned long *) nsindex
->free
;
518 u32 nfree
= ALIGN(nslot
, BITS_PER_LONG
);
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
);
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
));
532 if (flags
& ND_NSINDEX_INIT
)
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
);
545 static unsigned long nd_label_offset(struct nvdimm_drvdata
*ndd
,
546 struct nd_namespace_label
*nd_label
)
548 return (unsigned long) nd_label
549 - (unsigned long) to_namespace_index(ndd
, 0);
552 static int __pmem_label_update(struct nd_region
*nd_region
,
553 struct nd_mapping
*nd_mapping
, struct nd_namespace_pmem
*nspm
,
556 u64 cookie
= nd_region_interleave_set_cookie(nd_region
);
557 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
558 struct nd_label_ent
*label_ent
, *victim
= NULL
;
559 struct nd_namespace_label
*nd_label
;
560 struct nd_namespace_index
*nsindex
;
561 struct nd_label_id label_id
;
562 struct resource
*res
;
568 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
571 nd_label_gen_id(&label_id
, nspm
->uuid
, 0);
572 for_each_dpa_resource(ndd
, res
)
573 if (strcmp(res
->name
, label_id
.id
) == 0)
581 /* allocate and write the label to the staging (next) index */
582 slot
= nd_label_alloc_slot(ndd
);
583 if (slot
== UINT_MAX
)
585 dev_dbg(ndd
->dev
, "%s: allocated: %d\n", __func__
, slot
);
587 nd_label
= to_label(ndd
, slot
);
588 memset(nd_label
, 0, sizeof_namespace_label(ndd
));
589 memcpy(nd_label
->uuid
, nspm
->uuid
, NSLABEL_UUID_LEN
);
591 memcpy(nd_label
->name
, nspm
->alt_name
, NSLABEL_NAME_LEN
);
592 nd_label
->flags
= __cpu_to_le32(NSLABEL_FLAG_UPDATING
);
593 nd_label
->nlabel
= __cpu_to_le16(nd_region
->ndr_mappings
);
594 nd_label
->position
= __cpu_to_le16(pos
);
595 nd_label
->isetcookie
= __cpu_to_le64(cookie
);
596 nd_label
->rawsize
= __cpu_to_le64(resource_size(res
));
597 nd_label
->dpa
= __cpu_to_le64(res
->start
);
598 nd_label
->slot
= __cpu_to_le32(slot
);
599 nd_dbg_dpa(nd_region
, ndd
, res
, "%s\n", __func__
);
602 offset
= nd_label_offset(ndd
, nd_label
);
603 rc
= nvdimm_set_config_data(ndd
, offset
, nd_label
,
604 sizeof_namespace_label(ndd
));
608 /* Garbage collect the previous label */
609 mutex_lock(&nd_mapping
->lock
);
610 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
611 if (!label_ent
->label
)
613 if (memcmp(nspm
->uuid
, label_ent
->label
->uuid
,
614 NSLABEL_UUID_LEN
) != 0)
617 list_move_tail(&victim
->list
, &nd_mapping
->labels
);
621 dev_dbg(ndd
->dev
, "%s: free: %d\n", __func__
, slot
);
622 slot
= to_slot(ndd
, victim
->label
);
623 nd_label_free_slot(ndd
, slot
);
624 victim
->label
= NULL
;
628 rc
= nd_label_write_index(ndd
, ndd
->ns_next
,
629 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
631 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
)
632 if (!label_ent
->label
) {
633 label_ent
->label
= nd_label
;
637 dev_WARN_ONCE(&nspm
->nsio
.common
.dev
, nd_label
,
638 "failed to track label: %d\n",
639 to_slot(ndd
, nd_label
));
643 mutex_unlock(&nd_mapping
->lock
);
648 static bool is_old_resource(struct resource
*res
, struct resource
**list
, int n
)
652 if (res
->flags
& DPA_RESOURCE_ADJUSTED
)
654 for (i
= 0; i
< n
; i
++)
660 static struct resource
*to_resource(struct nvdimm_drvdata
*ndd
,
661 struct nd_namespace_label
*nd_label
)
663 struct resource
*res
;
665 for_each_dpa_resource(ndd
, res
) {
666 if (res
->start
!= __le64_to_cpu(nd_label
->dpa
))
668 if (resource_size(res
) != __le64_to_cpu(nd_label
->rawsize
))
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
681 static int __blk_label_update(struct nd_region
*nd_region
,
682 struct nd_mapping
*nd_mapping
, struct nd_namespace_blk
*nsblk
,
685 int i
, alloc
, victims
, nfree
, old_num_resources
, nlabel
, rc
= -ENXIO
;
686 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
687 struct nd_namespace_label
*nd_label
;
688 struct nd_label_ent
*label_ent
, *e
;
689 struct nd_namespace_index
*nsindex
;
690 unsigned long *free
, *victim_map
= NULL
;
691 struct resource
*res
, **old_res_list
;
692 struct nd_label_id label_id
;
693 u8 uuid
[NSLABEL_UUID_LEN
];
697 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
700 old_res_list
= nsblk
->res
;
701 nfree
= nd_label_nfree(ndd
);
702 old_num_resources
= nsblk
->num_resources
;
703 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
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.
713 for_each_dpa_resource(ndd
, res
) {
714 if (strcmp(res
->name
, label_id
.id
) != 0)
716 if (!is_old_resource(res
, old_res_list
, old_num_resources
))
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),
728 /* mark unused labels for garbage collection */
729 for_each_clear_bit_le(slot
, free
, nslot
) {
730 nd_label
= to_label(ndd
, slot
);
731 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
732 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
734 res
= to_resource(ndd
, nd_label
);
735 if (res
&& is_old_resource(res
, old_res_list
,
738 slot
= to_slot(ndd
, nd_label
);
739 set_bit(slot
, victim_map
);
744 /* don't allow updates that consume the last label */
745 if (nfree
- alloc
< 0 || nfree
- alloc
+ victims
< 1) {
746 dev_info(&nsblk
->common
.dev
, "insufficient label space\n");
750 /* from here on we need to abort on error */
753 /* assign all resources to the namespace before writing the labels */
755 nsblk
->num_resources
= 0;
756 for_each_dpa_resource(ndd
, res
) {
757 if (strcmp(res
->name
, label_id
.id
) != 0)
759 if (!nsblk_add_resource(nd_region
, ndd
, nsblk
, res
->start
)) {
765 for (i
= 0; i
< nsblk
->num_resources
; 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
)
774 dev_dbg(ndd
->dev
, "%s: allocated: %d\n", __func__
, slot
);
776 nd_label
= to_label(ndd
, slot
);
777 memset(nd_label
, 0, sizeof_namespace_label(ndd
));
778 memcpy(nd_label
->uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
);
780 memcpy(nd_label
->name
, nsblk
->alt_name
,
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
);
792 offset
= nd_label_offset(ndd
, nd_label
);
793 rc
= nvdimm_set_config_data(ndd
, offset
, nd_label
,
794 sizeof_namespace_label(ndd
));
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
);
806 rc
= nd_label_write_index(ndd
, ndd
->ns_next
,
807 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
812 * Now that the on-dimm labels are up to date, fix up the tracking
813 * entries in nd_mapping->labels
816 mutex_lock(&nd_mapping
->lock
);
817 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
818 nd_label
= label_ent
->label
;
822 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
823 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
826 list_move(&label_ent
->list
, &list
);
827 label_ent
->label
= NULL
;
829 list_splice_tail_init(&list
, &nd_mapping
->labels
);
830 mutex_unlock(&nd_mapping
->lock
);
832 if (nlabel
+ nsblk
->num_resources
> num_labels
) {
834 * Bug, we can't end up with more resources than
842 mutex_lock(&nd_mapping
->lock
);
843 label_ent
= list_first_entry_or_null(&nd_mapping
->labels
,
844 typeof(*label_ent
), list
);
847 mutex_unlock(&nd_mapping
->lock
);
851 for_each_clear_bit_le(slot
, free
, nslot
) {
852 nd_label
= to_label(ndd
, slot
);
853 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
854 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
856 res
= to_resource(ndd
, nd_label
);
857 res
->flags
&= ~DPA_RESOURCE_ADJUSTED
;
858 dev_vdbg(&nsblk
->common
.dev
, "assign label slot: %d\n", slot
);
859 list_for_each_entry_from(label_ent
, &nd_mapping
->labels
, list
) {
860 if (label_ent
->label
)
862 label_ent
->label
= nd_label
;
867 dev_WARN(&nsblk
->common
.dev
,
868 "failed to track label slot%d\n", slot
);
870 mutex_unlock(&nd_mapping
->lock
);
879 * 1/ repair the allocated label bitmap in the index
880 * 2/ restore the resource list
882 nd_label_copy(ndd
, nsindex
, to_current_namespace_index(ndd
));
884 nsblk
->res
= old_res_list
;
885 nsblk
->num_resources
= old_num_resources
;
890 static int init_labels(struct nd_mapping
*nd_mapping
, int num_labels
)
892 int i
, old_num_labels
= 0;
893 struct nd_label_ent
*label_ent
;
894 struct nd_namespace_index
*nsindex
;
895 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
897 mutex_lock(&nd_mapping
->lock
);
898 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
)
900 mutex_unlock(&nd_mapping
->lock
);
903 * We need to preserve all the old labels for the mapping so
904 * they can be garbage collected after writing the new labels.
906 for (i
= old_num_labels
; i
< num_labels
; i
++) {
907 label_ent
= kzalloc(sizeof(*label_ent
), GFP_KERNEL
);
910 mutex_lock(&nd_mapping
->lock
);
911 list_add_tail(&label_ent
->list
, &nd_mapping
->labels
);
912 mutex_unlock(&nd_mapping
->lock
);
915 if (ndd
->ns_current
== -1 || ndd
->ns_next
== -1)
918 return max(num_labels
, old_num_labels
);
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
);
931 return max(num_labels
, old_num_labels
);
934 static int del_labels(struct nd_mapping
*nd_mapping
, u8
*uuid
)
936 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
937 struct nd_label_ent
*label_ent
, *e
;
938 struct nd_namespace_index
*nsindex
;
939 u8 label_uuid
[NSLABEL_UUID_LEN
];
948 /* no index || no labels == nothing to delete */
949 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
952 mutex_lock(&nd_mapping
->lock
);
953 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
954 struct nd_namespace_label
*nd_label
= label_ent
->label
;
959 memcpy(label_uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
960 if (memcmp(label_uuid
, uuid
, NSLABEL_UUID_LEN
) != 0)
963 slot
= to_slot(ndd
, nd_label
);
964 nd_label_free_slot(ndd
, slot
);
965 dev_dbg(ndd
->dev
, "%s: free: %d\n", __func__
, slot
);
966 list_move_tail(&label_ent
->list
, &list
);
967 label_ent
->label
= NULL
;
969 list_splice_tail_init(&list
, &nd_mapping
->labels
);
972 nd_mapping_free_labels(nd_mapping
);
973 dev_dbg(ndd
->dev
, "%s: no more active labels\n", __func__
);
975 mutex_unlock(&nd_mapping
->lock
);
977 return nd_label_write_index(ndd
, ndd
->ns_next
,
978 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
981 int nd_pmem_namespace_label_update(struct nd_region
*nd_region
,
982 struct nd_namespace_pmem
*nspm
, resource_size_t size
)
986 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
987 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
988 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
989 struct resource
*res
;
993 rc
= del_labels(nd_mapping
, nspm
->uuid
);
999 for_each_dpa_resource(ndd
, res
)
1000 if (strncmp(res
->name
, "pmem", 4) == 0)
1002 WARN_ON_ONCE(!count
);
1004 rc
= init_labels(nd_mapping
, count
);
1008 rc
= __pmem_label_update(nd_region
, nd_mapping
, nspm
, i
);
1016 int nd_blk_namespace_label_update(struct nd_region
*nd_region
,
1017 struct nd_namespace_blk
*nsblk
, resource_size_t size
)
1019 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
1020 struct resource
*res
;
1024 return del_labels(nd_mapping
, nsblk
->uuid
);
1026 for_each_dpa_resource(to_ndd(nd_mapping
), res
)
1029 count
= init_labels(nd_mapping
, count
);
1033 return __blk_label_update(nd_region
, nd_mapping
, nsblk
, count
);