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