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