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