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