Dan Williams | 3d88002 | 2015-05-31 15:02:11 -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/module.h> |
| 14 | #include <linux/device.h> |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 15 | #include <linux/sort.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 16 | #include <linux/slab.h> |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 17 | #include <linux/list.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 18 | #include <linux/nd.h> |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 19 | #include "nd-core.h" |
Dan Williams | ca6a465 | 2017-01-13 20:36:58 -0800 | [diff] [blame^] | 20 | #include "pmem.h" |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 21 | #include "nd.h" |
| 22 | |
| 23 | static void namespace_io_release(struct device *dev) |
| 24 | { |
| 25 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 26 | |
| 27 | kfree(nsio); |
| 28 | } |
| 29 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 30 | static void namespace_pmem_release(struct device *dev) |
| 31 | { |
| 32 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 33 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 34 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 35 | if (nspm->id >= 0) |
| 36 | ida_simple_remove(&nd_region->ns_ida, nspm->id); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 37 | kfree(nspm->alt_name); |
| 38 | kfree(nspm->uuid); |
| 39 | kfree(nspm); |
| 40 | } |
| 41 | |
| 42 | static void namespace_blk_release(struct device *dev) |
| 43 | { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 44 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 45 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 46 | |
| 47 | if (nsblk->id >= 0) |
| 48 | ida_simple_remove(&nd_region->ns_ida, nsblk->id); |
| 49 | kfree(nsblk->alt_name); |
| 50 | kfree(nsblk->uuid); |
| 51 | kfree(nsblk->res); |
| 52 | kfree(nsblk); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Bhumika Goyal | 970d14e | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 55 | static const struct device_type namespace_io_device_type = { |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 56 | .name = "nd_namespace_io", |
| 57 | .release = namespace_io_release, |
| 58 | }; |
| 59 | |
Bhumika Goyal | 970d14e | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 60 | static const struct device_type namespace_pmem_device_type = { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 61 | .name = "nd_namespace_pmem", |
| 62 | .release = namespace_pmem_release, |
| 63 | }; |
| 64 | |
Bhumika Goyal | 970d14e | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 65 | static const struct device_type namespace_blk_device_type = { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 66 | .name = "nd_namespace_blk", |
| 67 | .release = namespace_blk_release, |
| 68 | }; |
| 69 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 70 | static bool is_namespace_pmem(const struct device *dev) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 71 | { |
| 72 | return dev ? dev->type == &namespace_pmem_device_type : false; |
| 73 | } |
| 74 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 75 | static bool is_namespace_blk(const struct device *dev) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 76 | { |
| 77 | return dev ? dev->type == &namespace_blk_device_type : false; |
| 78 | } |
| 79 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 80 | static bool is_namespace_io(const struct device *dev) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 81 | { |
| 82 | return dev ? dev->type == &namespace_io_device_type : false; |
| 83 | } |
| 84 | |
Dan Williams | e07ecd7 | 2016-01-05 18:37:23 -0800 | [diff] [blame] | 85 | static int is_uuid_busy(struct device *dev, void *data) |
| 86 | { |
| 87 | u8 *uuid1 = data, *uuid2 = NULL; |
| 88 | |
| 89 | if (is_namespace_pmem(dev)) { |
| 90 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 91 | |
| 92 | uuid2 = nspm->uuid; |
| 93 | } else if (is_namespace_blk(dev)) { |
| 94 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 95 | |
| 96 | uuid2 = nsblk->uuid; |
| 97 | } else if (is_nd_btt(dev)) { |
| 98 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 99 | |
| 100 | uuid2 = nd_btt->uuid; |
| 101 | } else if (is_nd_pfn(dev)) { |
| 102 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 103 | |
| 104 | uuid2 = nd_pfn->uuid; |
| 105 | } |
| 106 | |
| 107 | if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0) |
| 108 | return -EBUSY; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int is_namespace_uuid_busy(struct device *dev, void *data) |
| 114 | { |
| 115 | if (is_nd_pmem(dev) || is_nd_blk(dev)) |
| 116 | return device_for_each_child(dev, data, is_uuid_busy); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * nd_is_uuid_unique - verify that no other namespace has @uuid |
| 122 | * @dev: any device on a nvdimm_bus |
| 123 | * @uuid: uuid to check |
| 124 | */ |
| 125 | bool nd_is_uuid_unique(struct device *dev, u8 *uuid) |
| 126 | { |
| 127 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 128 | |
| 129 | if (!nvdimm_bus) |
| 130 | return false; |
| 131 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev)); |
| 132 | if (device_for_each_child(&nvdimm_bus->dev, uuid, |
| 133 | is_namespace_uuid_busy) != 0) |
| 134 | return false; |
| 135 | return true; |
| 136 | } |
| 137 | |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 138 | bool pmem_should_map_pages(struct device *dev) |
| 139 | { |
| 140 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 141 | struct nd_namespace_io *nsio; |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 142 | |
| 143 | if (!IS_ENABLED(CONFIG_ZONE_DEVICE)) |
| 144 | return false; |
| 145 | |
| 146 | if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags)) |
| 147 | return false; |
| 148 | |
| 149 | if (is_nd_pfn(dev) || is_nd_btt(dev)) |
| 150 | return false; |
| 151 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 152 | nsio = to_nd_namespace_io(dev); |
| 153 | if (region_intersects(nsio->res.start, resource_size(&nsio->res), |
| 154 | IORESOURCE_SYSTEM_RAM, |
| 155 | IORES_DESC_NONE) == REGION_MIXED) |
| 156 | return false; |
| 157 | |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 158 | return ARCH_MEMREMAP_PMEM == MEMREMAP_WB; |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 159 | } |
| 160 | EXPORT_SYMBOL(pmem_should_map_pages); |
| 161 | |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 162 | const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, |
| 163 | char *name) |
| 164 | { |
| 165 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 166 | const char *suffix = NULL; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 167 | |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 168 | if (ndns->claim && is_nd_btt(ndns->claim)) |
| 169 | suffix = "s"; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 170 | |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 171 | if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) { |
Dan Williams | 0122073 | 2016-10-05 09:09:44 -0700 | [diff] [blame] | 172 | int nsidx = 0; |
| 173 | |
| 174 | if (is_namespace_pmem(&ndns->dev)) { |
| 175 | struct nd_namespace_pmem *nspm; |
| 176 | |
| 177 | nspm = to_nd_namespace_pmem(&ndns->dev); |
| 178 | nsidx = nspm->id; |
| 179 | } |
| 180 | |
| 181 | if (nsidx) |
| 182 | sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx, |
| 183 | suffix ? suffix : ""); |
| 184 | else |
| 185 | sprintf(name, "pmem%d%s", nd_region->id, |
| 186 | suffix ? suffix : ""); |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 187 | } else if (is_namespace_blk(&ndns->dev)) { |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 188 | struct nd_namespace_blk *nsblk; |
| 189 | |
| 190 | nsblk = to_nd_namespace_blk(&ndns->dev); |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 191 | sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, |
| 192 | suffix ? suffix : ""); |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 193 | } else { |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | return name; |
| 198 | } |
| 199 | EXPORT_SYMBOL(nvdimm_namespace_disk_name); |
| 200 | |
Vishal Verma | 6ec6895 | 2015-07-29 14:58:09 -0600 | [diff] [blame] | 201 | const u8 *nd_dev_to_uuid(struct device *dev) |
| 202 | { |
| 203 | static const u8 null_uuid[16]; |
| 204 | |
| 205 | if (!dev) |
| 206 | return null_uuid; |
| 207 | |
| 208 | if (is_namespace_pmem(dev)) { |
| 209 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 210 | |
| 211 | return nspm->uuid; |
| 212 | } else if (is_namespace_blk(dev)) { |
| 213 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 214 | |
| 215 | return nsblk->uuid; |
| 216 | } else |
| 217 | return null_uuid; |
| 218 | } |
| 219 | EXPORT_SYMBOL(nd_dev_to_uuid); |
| 220 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 221 | static ssize_t nstype_show(struct device *dev, |
| 222 | struct device_attribute *attr, char *buf) |
| 223 | { |
| 224 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 225 | |
| 226 | return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); |
| 227 | } |
| 228 | static DEVICE_ATTR_RO(nstype); |
| 229 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 230 | static ssize_t __alt_name_store(struct device *dev, const char *buf, |
| 231 | const size_t len) |
| 232 | { |
| 233 | char *input, *pos, *alt_name, **ns_altname; |
| 234 | ssize_t rc; |
| 235 | |
| 236 | if (is_namespace_pmem(dev)) { |
| 237 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 238 | |
| 239 | ns_altname = &nspm->alt_name; |
| 240 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 241 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 242 | |
| 243 | ns_altname = &nsblk->alt_name; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 244 | } else |
| 245 | return -ENXIO; |
| 246 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 247 | if (dev->driver || to_ndns(dev)->claim) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 248 | return -EBUSY; |
| 249 | |
| 250 | input = kmemdup(buf, len + 1, GFP_KERNEL); |
| 251 | if (!input) |
| 252 | return -ENOMEM; |
| 253 | |
| 254 | input[len] = '\0'; |
| 255 | pos = strim(input); |
| 256 | if (strlen(pos) + 1 > NSLABEL_NAME_LEN) { |
| 257 | rc = -EINVAL; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL); |
| 262 | if (!alt_name) { |
| 263 | rc = -ENOMEM; |
| 264 | goto out; |
| 265 | } |
| 266 | kfree(*ns_altname); |
| 267 | *ns_altname = alt_name; |
| 268 | sprintf(*ns_altname, "%s", pos); |
| 269 | rc = len; |
| 270 | |
| 271 | out: |
| 272 | kfree(input); |
| 273 | return rc; |
| 274 | } |
| 275 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 276 | static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk) |
| 277 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 278 | struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 279 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 280 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 281 | struct nd_label_id label_id; |
| 282 | resource_size_t size = 0; |
| 283 | struct resource *res; |
| 284 | |
| 285 | if (!nsblk->uuid) |
| 286 | return 0; |
| 287 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); |
| 288 | for_each_dpa_resource(ndd, res) |
| 289 | if (strcmp(res->name, label_id.id) == 0) |
| 290 | size += resource_size(res); |
| 291 | return size; |
| 292 | } |
| 293 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 294 | static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) |
| 295 | { |
| 296 | struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); |
| 297 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 298 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 299 | struct nd_label_id label_id; |
| 300 | struct resource *res; |
| 301 | int count, i; |
| 302 | |
| 303 | if (!nsblk->uuid || !nsblk->lbasize || !ndd) |
| 304 | return false; |
| 305 | |
| 306 | count = 0; |
| 307 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); |
| 308 | for_each_dpa_resource(ndd, res) { |
| 309 | if (strcmp(res->name, label_id.id) != 0) |
| 310 | continue; |
| 311 | /* |
Geert Uytterhoeven | ae551e9 | 2016-08-31 11:45:25 +0200 | [diff] [blame] | 312 | * Resources with unacknowledged adjustments indicate a |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 313 | * failure to update labels |
| 314 | */ |
| 315 | if (res->flags & DPA_RESOURCE_ADJUSTED) |
| 316 | return false; |
| 317 | count++; |
| 318 | } |
| 319 | |
| 320 | /* These values match after a successful label update */ |
| 321 | if (count != nsblk->num_resources) |
| 322 | return false; |
| 323 | |
| 324 | for (i = 0; i < nsblk->num_resources; i++) { |
| 325 | struct resource *found = NULL; |
| 326 | |
| 327 | for_each_dpa_resource(ndd, res) |
| 328 | if (res == nsblk->res[i]) { |
| 329 | found = res; |
| 330 | break; |
| 331 | } |
| 332 | /* stale resource */ |
| 333 | if (!found) |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) |
| 341 | { |
| 342 | resource_size_t size; |
| 343 | |
| 344 | nvdimm_bus_lock(&nsblk->common.dev); |
| 345 | size = __nd_namespace_blk_validate(nsblk); |
| 346 | nvdimm_bus_unlock(&nsblk->common.dev); |
| 347 | |
| 348 | return size; |
| 349 | } |
| 350 | EXPORT_SYMBOL(nd_namespace_blk_validate); |
| 351 | |
| 352 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 353 | static int nd_namespace_label_update(struct nd_region *nd_region, |
| 354 | struct device *dev) |
| 355 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 356 | dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim, |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 357 | "namespace must be idle during label update\n"); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 358 | if (dev->driver || to_ndns(dev)->claim) |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 359 | return 0; |
| 360 | |
| 361 | /* |
| 362 | * Only allow label writes that will result in a valid namespace |
| 363 | * or deletion of an existing namespace. |
| 364 | */ |
| 365 | if (is_namespace_pmem(dev)) { |
| 366 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 367 | resource_size_t size = resource_size(&nspm->nsio.res); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 368 | |
| 369 | if (size == 0 && nspm->uuid) |
| 370 | /* delete allocation */; |
| 371 | else if (!nspm->uuid) |
| 372 | return 0; |
| 373 | |
| 374 | return nd_pmem_namespace_label_update(nd_region, nspm, size); |
| 375 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 376 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 377 | resource_size_t size = nd_namespace_blk_size(nsblk); |
| 378 | |
| 379 | if (size == 0 && nsblk->uuid) |
| 380 | /* delete allocation */; |
| 381 | else if (!nsblk->uuid || !nsblk->lbasize) |
| 382 | return 0; |
| 383 | |
| 384 | return nd_blk_namespace_label_update(nd_region, nsblk, size); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 385 | } else |
| 386 | return -ENXIO; |
| 387 | } |
| 388 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 389 | static ssize_t alt_name_store(struct device *dev, |
| 390 | struct device_attribute *attr, const char *buf, size_t len) |
| 391 | { |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 392 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 393 | ssize_t rc; |
| 394 | |
| 395 | device_lock(dev); |
| 396 | nvdimm_bus_lock(dev); |
| 397 | wait_nvdimm_bus_probe_idle(dev); |
| 398 | rc = __alt_name_store(dev, buf, len); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 399 | if (rc >= 0) |
| 400 | rc = nd_namespace_label_update(nd_region, dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 401 | dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc); |
| 402 | nvdimm_bus_unlock(dev); |
| 403 | device_unlock(dev); |
| 404 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 405 | return rc < 0 ? rc : len; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static ssize_t alt_name_show(struct device *dev, |
| 409 | struct device_attribute *attr, char *buf) |
| 410 | { |
| 411 | char *ns_altname; |
| 412 | |
| 413 | if (is_namespace_pmem(dev)) { |
| 414 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 415 | |
| 416 | ns_altname = nspm->alt_name; |
| 417 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 418 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 419 | |
| 420 | ns_altname = nsblk->alt_name; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 421 | } else |
| 422 | return -ENXIO; |
| 423 | |
| 424 | return sprintf(buf, "%s\n", ns_altname ? ns_altname : ""); |
| 425 | } |
| 426 | static DEVICE_ATTR_RW(alt_name); |
| 427 | |
| 428 | static int scan_free(struct nd_region *nd_region, |
| 429 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, |
| 430 | resource_size_t n) |
| 431 | { |
| 432 | bool is_blk = strncmp(label_id->id, "blk", 3) == 0; |
| 433 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 434 | int rc = 0; |
| 435 | |
| 436 | while (n) { |
| 437 | struct resource *res, *last; |
| 438 | resource_size_t new_start; |
| 439 | |
| 440 | last = NULL; |
| 441 | for_each_dpa_resource(ndd, res) |
| 442 | if (strcmp(res->name, label_id->id) == 0) |
| 443 | last = res; |
| 444 | res = last; |
| 445 | if (!res) |
| 446 | return 0; |
| 447 | |
| 448 | if (n >= resource_size(res)) { |
| 449 | n -= resource_size(res); |
| 450 | nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc); |
| 451 | nvdimm_free_dpa(ndd, res); |
| 452 | /* retry with last resource deleted */ |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Keep BLK allocations relegated to high DPA as much as |
| 458 | * possible |
| 459 | */ |
| 460 | if (is_blk) |
| 461 | new_start = res->start + n; |
| 462 | else |
| 463 | new_start = res->start; |
| 464 | |
| 465 | rc = adjust_resource(res, new_start, resource_size(res) - n); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 466 | if (rc == 0) |
| 467 | res->flags |= DPA_RESOURCE_ADJUSTED; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 468 | nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc); |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * shrink_dpa_allocation - for each dimm in region free n bytes for label_id |
| 477 | * @nd_region: the set of dimms to reclaim @n bytes from |
| 478 | * @label_id: unique identifier for the namespace consuming this dpa range |
| 479 | * @n: number of bytes per-dimm to release |
| 480 | * |
| 481 | * Assumes resources are ordered. Starting from the end try to |
| 482 | * adjust_resource() the allocation to @n, but if @n is larger than the |
| 483 | * allocation delete it and find the 'new' last allocation in the label |
| 484 | * set. |
| 485 | */ |
| 486 | static int shrink_dpa_allocation(struct nd_region *nd_region, |
| 487 | struct nd_label_id *label_id, resource_size_t n) |
| 488 | { |
| 489 | int i; |
| 490 | |
| 491 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 492 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 493 | int rc; |
| 494 | |
| 495 | rc = scan_free(nd_region, nd_mapping, label_id, n); |
| 496 | if (rc) |
| 497 | return rc; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static resource_size_t init_dpa_allocation(struct nd_label_id *label_id, |
| 504 | struct nd_region *nd_region, struct nd_mapping *nd_mapping, |
| 505 | resource_size_t n) |
| 506 | { |
| 507 | bool is_blk = strncmp(label_id->id, "blk", 3) == 0; |
| 508 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 509 | resource_size_t first_dpa; |
| 510 | struct resource *res; |
| 511 | int rc = 0; |
| 512 | |
| 513 | /* allocate blk from highest dpa first */ |
| 514 | if (is_blk) |
| 515 | first_dpa = nd_mapping->start + nd_mapping->size - n; |
| 516 | else |
| 517 | first_dpa = nd_mapping->start; |
| 518 | |
| 519 | /* first resource allocation for this label-id or dimm */ |
| 520 | res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n); |
| 521 | if (!res) |
| 522 | rc = -EBUSY; |
| 523 | |
| 524 | nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc); |
| 525 | return rc ? n : 0; |
| 526 | } |
| 527 | |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * space_valid() - validate free dpa space against constraints |
| 531 | * @nd_region: hosting region of the free space |
| 532 | * @ndd: dimm device data for debug |
| 533 | * @label_id: namespace id to allocate space |
| 534 | * @prev: potential allocation that precedes free space |
| 535 | * @next: allocation that follows the given free space range |
| 536 | * @exist: first allocation with same id in the mapping |
| 537 | * @n: range that must satisfied for pmem allocations |
| 538 | * @valid: free space range to validate |
| 539 | * |
| 540 | * BLK-space is valid as long as it does not precede a PMEM |
| 541 | * allocation in a given region. PMEM-space must be contiguous |
| 542 | * and adjacent to an existing existing allocation (if one |
| 543 | * exists). If reserving PMEM any space is valid. |
| 544 | */ |
| 545 | static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd, |
| 546 | struct nd_label_id *label_id, struct resource *prev, |
| 547 | struct resource *next, struct resource *exist, |
| 548 | resource_size_t n, struct resource *valid) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 549 | { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 550 | bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0; |
| 551 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
| 552 | |
| 553 | if (valid->start >= valid->end) |
| 554 | goto invalid; |
| 555 | |
| 556 | if (is_reserve) |
| 557 | return; |
| 558 | |
| 559 | if (!is_pmem) { |
| 560 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 561 | struct nvdimm_bus *nvdimm_bus; |
| 562 | struct blk_alloc_info info = { |
| 563 | .nd_mapping = nd_mapping, |
| 564 | .available = nd_mapping->size, |
| 565 | .res = valid, |
| 566 | }; |
| 567 | |
| 568 | WARN_ON(!is_nd_blk(&nd_region->dev)); |
| 569 | nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 570 | device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy); |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | /* allocation needs to be contiguous, so this is all or nothing */ |
| 575 | if (resource_size(valid) < n) |
| 576 | goto invalid; |
| 577 | |
| 578 | /* we've got all the space we need and no existing allocation */ |
| 579 | if (!exist) |
| 580 | return; |
| 581 | |
| 582 | /* allocation needs to be contiguous with the existing namespace */ |
| 583 | if (valid->start == exist->end + 1 |
| 584 | || valid->end == exist->start - 1) |
| 585 | return; |
| 586 | |
| 587 | invalid: |
| 588 | /* truncate @valid size to 0 */ |
| 589 | valid->end = valid->start - 1; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | enum alloc_loc { |
| 593 | ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER, |
| 594 | }; |
| 595 | |
| 596 | static resource_size_t scan_allocate(struct nd_region *nd_region, |
| 597 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, |
| 598 | resource_size_t n) |
| 599 | { |
| 600 | resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1; |
| 601 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
| 602 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 603 | struct resource *res, *exist = NULL, valid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 604 | const resource_size_t to_allocate = n; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 605 | int first; |
| 606 | |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 607 | for_each_dpa_resource(ndd, res) |
| 608 | if (strcmp(label_id->id, res->name) == 0) |
| 609 | exist = res; |
| 610 | |
| 611 | valid.start = nd_mapping->start; |
| 612 | valid.end = mapping_end; |
| 613 | valid.name = "free space"; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 614 | retry: |
| 615 | first = 0; |
| 616 | for_each_dpa_resource(ndd, res) { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 617 | struct resource *next = res->sibling, *new_res = NULL; |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 618 | resource_size_t allocate, available = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 619 | enum alloc_loc loc = ALLOC_ERR; |
| 620 | const char *action; |
| 621 | int rc = 0; |
| 622 | |
| 623 | /* ignore resources outside this nd_mapping */ |
| 624 | if (res->start > mapping_end) |
| 625 | continue; |
| 626 | if (res->end < nd_mapping->start) |
| 627 | continue; |
| 628 | |
| 629 | /* space at the beginning of the mapping */ |
| 630 | if (!first++ && res->start > nd_mapping->start) { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 631 | valid.start = nd_mapping->start; |
| 632 | valid.end = res->start - 1; |
| 633 | space_valid(nd_region, ndd, label_id, NULL, next, exist, |
| 634 | to_allocate, &valid); |
| 635 | available = resource_size(&valid); |
| 636 | if (available) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 637 | loc = ALLOC_BEFORE; |
| 638 | } |
| 639 | |
| 640 | /* space between allocations */ |
| 641 | if (!loc && next) { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 642 | valid.start = res->start + resource_size(res); |
| 643 | valid.end = min(mapping_end, next->start - 1); |
| 644 | space_valid(nd_region, ndd, label_id, res, next, exist, |
| 645 | to_allocate, &valid); |
| 646 | available = resource_size(&valid); |
| 647 | if (available) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 648 | loc = ALLOC_MID; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* space at the end of the mapping */ |
| 652 | if (!loc && !next) { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 653 | valid.start = res->start + resource_size(res); |
| 654 | valid.end = mapping_end; |
| 655 | space_valid(nd_region, ndd, label_id, res, next, exist, |
| 656 | to_allocate, &valid); |
| 657 | available = resource_size(&valid); |
| 658 | if (available) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 659 | loc = ALLOC_AFTER; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | if (!loc || !available) |
| 663 | continue; |
| 664 | allocate = min(available, n); |
| 665 | switch (loc) { |
| 666 | case ALLOC_BEFORE: |
| 667 | if (strcmp(res->name, label_id->id) == 0) { |
| 668 | /* adjust current resource up */ |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 669 | rc = adjust_resource(res, res->start - allocate, |
| 670 | resource_size(res) + allocate); |
| 671 | action = "cur grow up"; |
| 672 | } else |
| 673 | action = "allocate"; |
| 674 | break; |
| 675 | case ALLOC_MID: |
| 676 | if (strcmp(next->name, label_id->id) == 0) { |
| 677 | /* adjust next resource up */ |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 678 | rc = adjust_resource(next, next->start |
| 679 | - allocate, resource_size(next) |
| 680 | + allocate); |
| 681 | new_res = next; |
| 682 | action = "next grow up"; |
| 683 | } else if (strcmp(res->name, label_id->id) == 0) { |
| 684 | action = "grow down"; |
| 685 | } else |
| 686 | action = "allocate"; |
| 687 | break; |
| 688 | case ALLOC_AFTER: |
| 689 | if (strcmp(res->name, label_id->id) == 0) |
| 690 | action = "grow down"; |
| 691 | else |
| 692 | action = "allocate"; |
| 693 | break; |
| 694 | default: |
| 695 | return n; |
| 696 | } |
| 697 | |
| 698 | if (strcmp(action, "allocate") == 0) { |
| 699 | /* BLK allocate bottom up */ |
| 700 | if (!is_pmem) |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 701 | valid.start += available - allocate; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 702 | |
| 703 | new_res = nvdimm_allocate_dpa(ndd, label_id, |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 704 | valid.start, allocate); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 705 | if (!new_res) |
| 706 | rc = -EBUSY; |
| 707 | } else if (strcmp(action, "grow down") == 0) { |
| 708 | /* adjust current resource down */ |
| 709 | rc = adjust_resource(res, res->start, resource_size(res) |
| 710 | + allocate); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 711 | if (rc == 0) |
| 712 | res->flags |= DPA_RESOURCE_ADJUSTED; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | if (!new_res) |
| 716 | new_res = res; |
| 717 | |
| 718 | nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n", |
| 719 | action, loc, rc); |
| 720 | |
| 721 | if (rc) |
| 722 | return n; |
| 723 | |
| 724 | n -= allocate; |
| 725 | if (n) { |
| 726 | /* |
| 727 | * Retry scan with newly inserted resources. |
| 728 | * For example, if we did an ALLOC_BEFORE |
| 729 | * insertion there may also have been space |
| 730 | * available for an ALLOC_AFTER insertion, so we |
| 731 | * need to check this same resource again |
| 732 | */ |
| 733 | goto retry; |
| 734 | } else |
| 735 | return 0; |
| 736 | } |
| 737 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 738 | /* |
| 739 | * If we allocated nothing in the BLK case it may be because we are in |
| 740 | * an initial "pmem-reserve pass". Only do an initial BLK allocation |
| 741 | * when none of the DPA space is reserved. |
| 742 | */ |
| 743 | if ((is_pmem || !ndd->dpa.child) && n == to_allocate) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 744 | return init_dpa_allocation(label_id, nd_region, nd_mapping, n); |
| 745 | return n; |
| 746 | } |
| 747 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 748 | static int merge_dpa(struct nd_region *nd_region, |
| 749 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id) |
| 750 | { |
| 751 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 752 | struct resource *res; |
| 753 | |
| 754 | if (strncmp("pmem", label_id->id, 4) == 0) |
| 755 | return 0; |
| 756 | retry: |
| 757 | for_each_dpa_resource(ndd, res) { |
| 758 | int rc; |
| 759 | struct resource *next = res->sibling; |
| 760 | resource_size_t end = res->start + resource_size(res); |
| 761 | |
| 762 | if (!next || strcmp(res->name, label_id->id) != 0 |
| 763 | || strcmp(next->name, label_id->id) != 0 |
| 764 | || end != next->start) |
| 765 | continue; |
| 766 | end += resource_size(next); |
| 767 | nvdimm_free_dpa(ndd, next); |
| 768 | rc = adjust_resource(res, res->start, end - res->start); |
| 769 | nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc); |
| 770 | if (rc) |
| 771 | return rc; |
| 772 | res->flags |= DPA_RESOURCE_ADJUSTED; |
| 773 | goto retry; |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static int __reserve_free_pmem(struct device *dev, void *data) |
| 780 | { |
| 781 | struct nvdimm *nvdimm = data; |
| 782 | struct nd_region *nd_region; |
| 783 | struct nd_label_id label_id; |
| 784 | int i; |
| 785 | |
| 786 | if (!is_nd_pmem(dev)) |
| 787 | return 0; |
| 788 | |
| 789 | nd_region = to_nd_region(dev); |
| 790 | if (nd_region->ndr_mappings == 0) |
| 791 | return 0; |
| 792 | |
| 793 | memset(&label_id, 0, sizeof(label_id)); |
| 794 | strcat(label_id.id, "pmem-reserve"); |
| 795 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 796 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 797 | resource_size_t n, rem = 0; |
| 798 | |
| 799 | if (nd_mapping->nvdimm != nvdimm) |
| 800 | continue; |
| 801 | |
| 802 | n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem); |
| 803 | if (n == 0) |
| 804 | return 0; |
| 805 | rem = scan_allocate(nd_region, nd_mapping, &label_id, n); |
| 806 | dev_WARN_ONCE(&nd_region->dev, rem, |
| 807 | "pmem reserve underrun: %#llx of %#llx bytes\n", |
| 808 | (unsigned long long) n - rem, |
| 809 | (unsigned long long) n); |
| 810 | return rem ? -ENXIO : 0; |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static void release_free_pmem(struct nvdimm_bus *nvdimm_bus, |
| 817 | struct nd_mapping *nd_mapping) |
| 818 | { |
| 819 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 820 | struct resource *res, *_res; |
| 821 | |
| 822 | for_each_dpa_resource_safe(ndd, res, _res) |
| 823 | if (strcmp(res->name, "pmem-reserve") == 0) |
| 824 | nvdimm_free_dpa(ndd, res); |
| 825 | } |
| 826 | |
| 827 | static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus, |
| 828 | struct nd_mapping *nd_mapping) |
| 829 | { |
| 830 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 831 | int rc; |
| 832 | |
| 833 | rc = device_for_each_child(&nvdimm_bus->dev, nvdimm, |
| 834 | __reserve_free_pmem); |
| 835 | if (rc) |
| 836 | release_free_pmem(nvdimm_bus, nd_mapping); |
| 837 | return rc; |
| 838 | } |
| 839 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 840 | /** |
| 841 | * grow_dpa_allocation - for each dimm allocate n bytes for @label_id |
| 842 | * @nd_region: the set of dimms to allocate @n more bytes from |
| 843 | * @label_id: unique identifier for the namespace consuming this dpa range |
| 844 | * @n: number of bytes per-dimm to add to the existing allocation |
| 845 | * |
| 846 | * Assumes resources are ordered. For BLK regions, first consume |
| 847 | * BLK-only available DPA free space, then consume PMEM-aliased DPA |
| 848 | * space starting at the highest DPA. For PMEM regions start |
| 849 | * allocations from the start of an interleave set and end at the first |
| 850 | * BLK allocation or the end of the interleave set, whichever comes |
| 851 | * first. |
| 852 | */ |
| 853 | static int grow_dpa_allocation(struct nd_region *nd_region, |
| 854 | struct nd_label_id *label_id, resource_size_t n) |
| 855 | { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 856 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 857 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 858 | int i; |
| 859 | |
| 860 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 861 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 862 | resource_size_t rem = n; |
| 863 | int rc, j; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 864 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 865 | /* |
| 866 | * In the BLK case try once with all unallocated PMEM |
| 867 | * reserved, and once without |
| 868 | */ |
| 869 | for (j = is_pmem; j < 2; j++) { |
| 870 | bool blk_only = j == 0; |
| 871 | |
| 872 | if (blk_only) { |
| 873 | rc = reserve_free_pmem(nvdimm_bus, nd_mapping); |
| 874 | if (rc) |
| 875 | return rc; |
| 876 | } |
| 877 | rem = scan_allocate(nd_region, nd_mapping, |
| 878 | label_id, rem); |
| 879 | if (blk_only) |
| 880 | release_free_pmem(nvdimm_bus, nd_mapping); |
| 881 | |
| 882 | /* try again and allow encroachments into PMEM */ |
| 883 | if (rem == 0) |
| 884 | break; |
| 885 | } |
| 886 | |
| 887 | dev_WARN_ONCE(&nd_region->dev, rem, |
| 888 | "allocation underrun: %#llx of %#llx bytes\n", |
| 889 | (unsigned long long) n - rem, |
| 890 | (unsigned long long) n); |
| 891 | if (rem) |
| 892 | return -ENXIO; |
| 893 | |
| 894 | rc = merge_dpa(nd_region, nd_mapping, label_id); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 895 | if (rc) |
| 896 | return rc; |
| 897 | } |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 902 | static void nd_namespace_pmem_set_resource(struct nd_region *nd_region, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 903 | struct nd_namespace_pmem *nspm, resource_size_t size) |
| 904 | { |
| 905 | struct resource *res = &nspm->nsio.res; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 906 | resource_size_t offset = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 907 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 908 | if (size && !nspm->uuid) { |
| 909 | WARN_ON_ONCE(1); |
| 910 | size = 0; |
| 911 | } |
| 912 | |
| 913 | if (size && nspm->uuid) { |
| 914 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 915 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 916 | struct nd_label_id label_id; |
| 917 | struct resource *res; |
| 918 | |
| 919 | if (!ndd) { |
| 920 | size = 0; |
| 921 | goto out; |
| 922 | } |
| 923 | |
| 924 | nd_label_gen_id(&label_id, nspm->uuid, 0); |
| 925 | |
| 926 | /* calculate a spa offset from the dpa allocation offset */ |
| 927 | for_each_dpa_resource(ndd, res) |
| 928 | if (strcmp(res->name, label_id.id) == 0) { |
| 929 | offset = (res->start - nd_mapping->start) |
| 930 | * nd_region->ndr_mappings; |
| 931 | goto out; |
| 932 | } |
| 933 | |
| 934 | WARN_ON_ONCE(1); |
| 935 | size = 0; |
| 936 | } |
| 937 | |
| 938 | out: |
| 939 | res->start = nd_region->ndr_start + offset; |
| 940 | res->end = res->start + size - 1; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 941 | } |
| 942 | |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 943 | static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where) |
| 944 | { |
| 945 | if (!uuid) { |
| 946 | dev_dbg(dev, "%s: uuid not set\n", where); |
| 947 | return true; |
| 948 | } |
| 949 | return false; |
| 950 | } |
| 951 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 952 | static ssize_t __size_store(struct device *dev, unsigned long long val) |
| 953 | { |
| 954 | resource_size_t allocated = 0, available = 0; |
| 955 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 956 | struct nd_namespace_common *ndns = to_ndns(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 957 | struct nd_mapping *nd_mapping; |
| 958 | struct nvdimm_drvdata *ndd; |
| 959 | struct nd_label_id label_id; |
| 960 | u32 flags = 0, remainder; |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 961 | int rc, i, id = -1; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 962 | u8 *uuid = NULL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 963 | |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 964 | if (dev->driver || ndns->claim) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 965 | return -EBUSY; |
| 966 | |
| 967 | if (is_namespace_pmem(dev)) { |
| 968 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 969 | |
| 970 | uuid = nspm->uuid; |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 971 | id = nspm->id; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 972 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 973 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 974 | |
| 975 | uuid = nsblk->uuid; |
| 976 | flags = NSLABEL_FLAG_LOCAL; |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 977 | id = nsblk->id; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * We need a uuid for the allocation-label and dimm(s) on which |
| 982 | * to store the label. |
| 983 | */ |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 984 | if (uuid_not_set(uuid, dev, __func__)) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 985 | return -ENXIO; |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 986 | if (nd_region->ndr_mappings == 0) { |
| 987 | dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__); |
| 988 | return -ENXIO; |
| 989 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 990 | |
| 991 | div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder); |
| 992 | if (remainder) { |
| 993 | dev_dbg(dev, "%llu is not %dK aligned\n", val, |
| 994 | (SZ_4K * nd_region->ndr_mappings) / SZ_1K); |
| 995 | return -EINVAL; |
| 996 | } |
| 997 | |
| 998 | nd_label_gen_id(&label_id, uuid, flags); |
| 999 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1000 | nd_mapping = &nd_region->mapping[i]; |
| 1001 | ndd = to_ndd(nd_mapping); |
| 1002 | |
| 1003 | /* |
| 1004 | * All dimms in an interleave set, or the base dimm for a blk |
| 1005 | * region, need to be enabled for the size to be changed. |
| 1006 | */ |
| 1007 | if (!ndd) |
| 1008 | return -ENXIO; |
| 1009 | |
| 1010 | allocated += nvdimm_allocated_dpa(ndd, &label_id); |
| 1011 | } |
| 1012 | available = nd_region_available_dpa(nd_region); |
| 1013 | |
| 1014 | if (val > available + allocated) |
| 1015 | return -ENOSPC; |
| 1016 | |
| 1017 | if (val == allocated) |
| 1018 | return 0; |
| 1019 | |
| 1020 | val = div_u64(val, nd_region->ndr_mappings); |
| 1021 | allocated = div_u64(allocated, nd_region->ndr_mappings); |
| 1022 | if (val < allocated) |
| 1023 | rc = shrink_dpa_allocation(nd_region, &label_id, |
| 1024 | allocated - val); |
| 1025 | else |
| 1026 | rc = grow_dpa_allocation(nd_region, &label_id, val - allocated); |
| 1027 | |
| 1028 | if (rc) |
| 1029 | return rc; |
| 1030 | |
| 1031 | if (is_namespace_pmem(dev)) { |
| 1032 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1033 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1034 | nd_namespace_pmem_set_resource(nd_region, nspm, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1035 | val * nd_region->ndr_mappings); |
| 1036 | } |
| 1037 | |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 1038 | /* |
| 1039 | * Try to delete the namespace if we deleted all of its |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 1040 | * allocation, this is not the seed or 0th device for the |
| 1041 | * region, and it is not actively claimed by a btt, pfn, or dax |
| 1042 | * instance. |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 1043 | */ |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 1044 | if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim) |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 1045 | nd_device_unregister(dev, ND_ASYNC); |
| 1046 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1047 | return rc; |
| 1048 | } |
| 1049 | |
| 1050 | static ssize_t size_store(struct device *dev, |
| 1051 | struct device_attribute *attr, const char *buf, size_t len) |
| 1052 | { |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1053 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1054 | unsigned long long val; |
| 1055 | u8 **uuid = NULL; |
| 1056 | int rc; |
| 1057 | |
| 1058 | rc = kstrtoull(buf, 0, &val); |
| 1059 | if (rc) |
| 1060 | return rc; |
| 1061 | |
| 1062 | device_lock(dev); |
| 1063 | nvdimm_bus_lock(dev); |
| 1064 | wait_nvdimm_bus_probe_idle(dev); |
| 1065 | rc = __size_store(dev, val); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1066 | if (rc >= 0) |
| 1067 | rc = nd_namespace_label_update(nd_region, dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1068 | |
| 1069 | if (is_namespace_pmem(dev)) { |
| 1070 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1071 | |
| 1072 | uuid = &nspm->uuid; |
| 1073 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1074 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1075 | |
| 1076 | uuid = &nsblk->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | if (rc == 0 && val == 0 && uuid) { |
| 1080 | /* setting size zero == 'delete namespace' */ |
| 1081 | kfree(*uuid); |
| 1082 | *uuid = NULL; |
| 1083 | } |
| 1084 | |
| 1085 | dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0 |
| 1086 | ? "fail" : "success", rc); |
| 1087 | |
| 1088 | nvdimm_bus_unlock(dev); |
| 1089 | device_unlock(dev); |
| 1090 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1091 | return rc < 0 ? rc : len; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1094 | resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1095 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1096 | struct device *dev = &ndns->dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1097 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1098 | if (is_namespace_pmem(dev)) { |
| 1099 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1100 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1101 | return resource_size(&nspm->nsio.res); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1102 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1103 | return nd_namespace_blk_size(to_nd_namespace_blk(dev)); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1104 | } else if (is_namespace_io(dev)) { |
| 1105 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 1106 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1107 | return resource_size(&nsio->res); |
| 1108 | } else |
| 1109 | WARN_ONCE(1, "unknown namespace type\n"); |
| 1110 | return 0; |
| 1111 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1112 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1113 | resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) |
| 1114 | { |
| 1115 | resource_size_t size; |
| 1116 | |
| 1117 | nvdimm_bus_lock(&ndns->dev); |
| 1118 | size = __nvdimm_namespace_capacity(ndns); |
| 1119 | nvdimm_bus_unlock(&ndns->dev); |
| 1120 | |
| 1121 | return size; |
| 1122 | } |
| 1123 | EXPORT_SYMBOL(nvdimm_namespace_capacity); |
| 1124 | |
| 1125 | static ssize_t size_show(struct device *dev, |
| 1126 | struct device_attribute *attr, char *buf) |
| 1127 | { |
| 1128 | return sprintf(buf, "%llu\n", (unsigned long long) |
| 1129 | nvdimm_namespace_capacity(to_ndns(dev))); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1130 | } |
Fabian Frederick | b44fe76 | 2016-12-04 10:54:08 -0800 | [diff] [blame] | 1131 | static DEVICE_ATTR(size, 0444, size_show, size_store); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1132 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1133 | static u8 *namespace_to_uuid(struct device *dev) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1134 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1135 | if (is_namespace_pmem(dev)) { |
| 1136 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1137 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1138 | return nspm->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1139 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1140 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1141 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1142 | return nsblk->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1143 | } else |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1144 | return ERR_PTR(-ENXIO); |
| 1145 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1146 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1147 | static ssize_t uuid_show(struct device *dev, |
| 1148 | struct device_attribute *attr, char *buf) |
| 1149 | { |
| 1150 | u8 *uuid = namespace_to_uuid(dev); |
| 1151 | |
| 1152 | if (IS_ERR(uuid)) |
| 1153 | return PTR_ERR(uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1154 | if (uuid) |
| 1155 | return sprintf(buf, "%pUb\n", uuid); |
| 1156 | return sprintf(buf, "\n"); |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * namespace_update_uuid - check for a unique uuid and whether we're "renaming" |
| 1161 | * @nd_region: parent region so we can updates all dimms in the set |
| 1162 | * @dev: namespace type for generating label_id |
| 1163 | * @new_uuid: incoming uuid |
| 1164 | * @old_uuid: reference to the uuid storage location in the namespace object |
| 1165 | */ |
| 1166 | static int namespace_update_uuid(struct nd_region *nd_region, |
| 1167 | struct device *dev, u8 *new_uuid, u8 **old_uuid) |
| 1168 | { |
| 1169 | u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0; |
| 1170 | struct nd_label_id old_label_id; |
| 1171 | struct nd_label_id new_label_id; |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1172 | int i; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1173 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1174 | if (!nd_is_uuid_unique(dev, new_uuid)) |
| 1175 | return -EINVAL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1176 | |
| 1177 | if (*old_uuid == NULL) |
| 1178 | goto out; |
| 1179 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1180 | /* |
| 1181 | * If we've already written a label with this uuid, then it's |
| 1182 | * too late to rename because we can't reliably update the uuid |
| 1183 | * without losing the old namespace. Userspace must delete this |
| 1184 | * namespace to abandon the old uuid. |
| 1185 | */ |
| 1186 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1187 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1188 | |
| 1189 | /* |
| 1190 | * This check by itself is sufficient because old_uuid |
| 1191 | * would be NULL above if this uuid did not exist in the |
| 1192 | * currently written set. |
| 1193 | * |
| 1194 | * FIXME: can we delete uuid with zero dpa allocated? |
| 1195 | */ |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1196 | if (list_empty(&nd_mapping->labels)) |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1197 | return -EBUSY; |
| 1198 | } |
| 1199 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1200 | nd_label_gen_id(&old_label_id, *old_uuid, flags); |
| 1201 | nd_label_gen_id(&new_label_id, new_uuid, flags); |
| 1202 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1203 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1204 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 1205 | struct resource *res; |
| 1206 | |
| 1207 | for_each_dpa_resource(ndd, res) |
| 1208 | if (strcmp(res->name, old_label_id.id) == 0) |
| 1209 | sprintf((void *) res->name, "%s", |
| 1210 | new_label_id.id); |
| 1211 | } |
| 1212 | kfree(*old_uuid); |
| 1213 | out: |
| 1214 | *old_uuid = new_uuid; |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | static ssize_t uuid_store(struct device *dev, |
| 1219 | struct device_attribute *attr, const char *buf, size_t len) |
| 1220 | { |
| 1221 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1222 | u8 *uuid = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1223 | ssize_t rc = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1224 | u8 **ns_uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1225 | |
| 1226 | if (is_namespace_pmem(dev)) { |
| 1227 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1228 | |
| 1229 | ns_uuid = &nspm->uuid; |
| 1230 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1231 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1232 | |
| 1233 | ns_uuid = &nsblk->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1234 | } else |
| 1235 | return -ENXIO; |
| 1236 | |
| 1237 | device_lock(dev); |
| 1238 | nvdimm_bus_lock(dev); |
| 1239 | wait_nvdimm_bus_probe_idle(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1240 | if (to_ndns(dev)->claim) |
| 1241 | rc = -EBUSY; |
| 1242 | if (rc >= 0) |
| 1243 | rc = nd_uuid_store(dev, &uuid, buf, len); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1244 | if (rc >= 0) |
| 1245 | rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1246 | if (rc >= 0) |
| 1247 | rc = nd_namespace_label_update(nd_region, dev); |
| 1248 | else |
| 1249 | kfree(uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1250 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
| 1251 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); |
| 1252 | nvdimm_bus_unlock(dev); |
| 1253 | device_unlock(dev); |
| 1254 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1255 | return rc < 0 ? rc : len; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1256 | } |
| 1257 | static DEVICE_ATTR_RW(uuid); |
| 1258 | |
| 1259 | static ssize_t resource_show(struct device *dev, |
| 1260 | struct device_attribute *attr, char *buf) |
| 1261 | { |
| 1262 | struct resource *res; |
| 1263 | |
| 1264 | if (is_namespace_pmem(dev)) { |
| 1265 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1266 | |
| 1267 | res = &nspm->nsio.res; |
| 1268 | } else if (is_namespace_io(dev)) { |
| 1269 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 1270 | |
| 1271 | res = &nsio->res; |
| 1272 | } else |
| 1273 | return -ENXIO; |
| 1274 | |
| 1275 | /* no address to convey if the namespace has no allocation */ |
| 1276 | if (resource_size(res) == 0) |
| 1277 | return -ENXIO; |
| 1278 | return sprintf(buf, "%#llx\n", (unsigned long long) res->start); |
| 1279 | } |
| 1280 | static DEVICE_ATTR_RO(resource); |
| 1281 | |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 1282 | static const unsigned long ns_lbasize_supported[] = { 512, 520, 528, |
| 1283 | 4096, 4104, 4160, 4224, 0 }; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1284 | |
| 1285 | static ssize_t sector_size_show(struct device *dev, |
| 1286 | struct device_attribute *attr, char *buf) |
| 1287 | { |
| 1288 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1289 | |
| 1290 | if (!is_namespace_blk(dev)) |
| 1291 | return -ENXIO; |
| 1292 | |
| 1293 | return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf); |
| 1294 | } |
| 1295 | |
| 1296 | static ssize_t sector_size_store(struct device *dev, |
| 1297 | struct device_attribute *attr, const char *buf, size_t len) |
| 1298 | { |
| 1299 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1300 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1301 | ssize_t rc = 0; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1302 | |
| 1303 | if (!is_namespace_blk(dev)) |
| 1304 | return -ENXIO; |
| 1305 | |
| 1306 | device_lock(dev); |
| 1307 | nvdimm_bus_lock(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1308 | if (to_ndns(dev)->claim) |
| 1309 | rc = -EBUSY; |
| 1310 | if (rc >= 0) |
| 1311 | rc = nd_sector_size_store(dev, buf, &nsblk->lbasize, |
| 1312 | ns_lbasize_supported); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1313 | if (rc >= 0) |
| 1314 | rc = nd_namespace_label_update(nd_region, dev); |
| 1315 | dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__, |
| 1316 | rc, rc < 0 ? "tried" : "wrote", buf, |
| 1317 | buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1318 | nvdimm_bus_unlock(dev); |
| 1319 | device_unlock(dev); |
| 1320 | |
| 1321 | return rc ? rc : len; |
| 1322 | } |
| 1323 | static DEVICE_ATTR_RW(sector_size); |
| 1324 | |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 1325 | static ssize_t dpa_extents_show(struct device *dev, |
| 1326 | struct device_attribute *attr, char *buf) |
| 1327 | { |
| 1328 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1329 | struct nd_label_id label_id; |
| 1330 | int count = 0, i; |
| 1331 | u8 *uuid = NULL; |
| 1332 | u32 flags = 0; |
| 1333 | |
| 1334 | nvdimm_bus_lock(dev); |
| 1335 | if (is_namespace_pmem(dev)) { |
| 1336 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1337 | |
| 1338 | uuid = nspm->uuid; |
| 1339 | flags = 0; |
| 1340 | } else if (is_namespace_blk(dev)) { |
| 1341 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1342 | |
| 1343 | uuid = nsblk->uuid; |
| 1344 | flags = NSLABEL_FLAG_LOCAL; |
| 1345 | } |
| 1346 | |
| 1347 | if (!uuid) |
| 1348 | goto out; |
| 1349 | |
| 1350 | nd_label_gen_id(&label_id, uuid, flags); |
| 1351 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1352 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1353 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 1354 | struct resource *res; |
| 1355 | |
| 1356 | for_each_dpa_resource(ndd, res) |
| 1357 | if (strcmp(res->name, label_id.id) == 0) |
| 1358 | count++; |
| 1359 | } |
| 1360 | out: |
| 1361 | nvdimm_bus_unlock(dev); |
| 1362 | |
| 1363 | return sprintf(buf, "%d\n", count); |
| 1364 | } |
| 1365 | static DEVICE_ATTR_RO(dpa_extents); |
| 1366 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1367 | static ssize_t holder_show(struct device *dev, |
| 1368 | struct device_attribute *attr, char *buf) |
| 1369 | { |
| 1370 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1371 | ssize_t rc; |
| 1372 | |
| 1373 | device_lock(dev); |
| 1374 | rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); |
| 1375 | device_unlock(dev); |
| 1376 | |
| 1377 | return rc; |
| 1378 | } |
| 1379 | static DEVICE_ATTR_RO(holder); |
| 1380 | |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1381 | static ssize_t mode_show(struct device *dev, |
| 1382 | struct device_attribute *attr, char *buf) |
| 1383 | { |
| 1384 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1385 | struct device *claim; |
| 1386 | char *mode; |
| 1387 | ssize_t rc; |
| 1388 | |
| 1389 | device_lock(dev); |
| 1390 | claim = ndns->claim; |
Dan Williams | 9c41242 | 2016-01-23 15:34:10 -0800 | [diff] [blame] | 1391 | if (claim && is_nd_btt(claim)) |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1392 | mode = "safe"; |
Dan Williams | 9c41242 | 2016-01-23 15:34:10 -0800 | [diff] [blame] | 1393 | else if (claim && is_nd_pfn(claim)) |
| 1394 | mode = "memory"; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1395 | else if (claim && is_nd_dax(claim)) |
| 1396 | mode = "dax"; |
Dan Williams | 9c41242 | 2016-01-23 15:34:10 -0800 | [diff] [blame] | 1397 | else if (!claim && pmem_should_map_pages(dev)) |
| 1398 | mode = "memory"; |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1399 | else |
| 1400 | mode = "raw"; |
| 1401 | rc = sprintf(buf, "%s\n", mode); |
| 1402 | device_unlock(dev); |
| 1403 | |
| 1404 | return rc; |
| 1405 | } |
| 1406 | static DEVICE_ATTR_RO(mode); |
| 1407 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1408 | static ssize_t force_raw_store(struct device *dev, |
| 1409 | struct device_attribute *attr, const char *buf, size_t len) |
| 1410 | { |
| 1411 | bool force_raw; |
| 1412 | int rc = strtobool(buf, &force_raw); |
| 1413 | |
| 1414 | if (rc) |
| 1415 | return rc; |
| 1416 | |
| 1417 | to_ndns(dev)->force_raw = force_raw; |
| 1418 | return len; |
| 1419 | } |
| 1420 | |
| 1421 | static ssize_t force_raw_show(struct device *dev, |
| 1422 | struct device_attribute *attr, char *buf) |
| 1423 | { |
| 1424 | return sprintf(buf, "%d\n", to_ndns(dev)->force_raw); |
| 1425 | } |
| 1426 | static DEVICE_ATTR_RW(force_raw); |
| 1427 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1428 | static struct attribute *nd_namespace_attributes[] = { |
| 1429 | &dev_attr_nstype.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1430 | &dev_attr_size.attr, |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1431 | &dev_attr_mode.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1432 | &dev_attr_uuid.attr, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1433 | &dev_attr_holder.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1434 | &dev_attr_resource.attr, |
| 1435 | &dev_attr_alt_name.attr, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1436 | &dev_attr_force_raw.attr, |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1437 | &dev_attr_sector_size.attr, |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 1438 | &dev_attr_dpa_extents.attr, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1439 | NULL, |
| 1440 | }; |
| 1441 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1442 | static umode_t namespace_visible(struct kobject *kobj, |
| 1443 | struct attribute *a, int n) |
| 1444 | { |
| 1445 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1446 | |
| 1447 | if (a == &dev_attr_resource.attr) { |
| 1448 | if (is_namespace_blk(dev)) |
| 1449 | return 0; |
| 1450 | return a->mode; |
| 1451 | } |
| 1452 | |
| 1453 | if (is_namespace_pmem(dev) || is_namespace_blk(dev)) { |
| 1454 | if (a == &dev_attr_size.attr) |
Fabian Frederick | b44fe76 | 2016-12-04 10:54:08 -0800 | [diff] [blame] | 1455 | return 0644; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1456 | |
| 1457 | if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr) |
| 1458 | return 0; |
| 1459 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1460 | return a->mode; |
| 1461 | } |
| 1462 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1463 | if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr |
| 1464 | || a == &dev_attr_holder.attr |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1465 | || a == &dev_attr_force_raw.attr |
| 1466 | || a == &dev_attr_mode.attr) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1467 | return a->mode; |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1472 | static struct attribute_group nd_namespace_attribute_group = { |
| 1473 | .attrs = nd_namespace_attributes, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1474 | .is_visible = namespace_visible, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1475 | }; |
| 1476 | |
| 1477 | static const struct attribute_group *nd_namespace_attribute_groups[] = { |
| 1478 | &nd_device_attribute_group, |
| 1479 | &nd_namespace_attribute_group, |
Toshi Kani | 74ae66c | 2015-06-19 12:18:34 -0600 | [diff] [blame] | 1480 | &nd_numa_attribute_group, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1481 | NULL, |
| 1482 | }; |
| 1483 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1484 | struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) |
| 1485 | { |
| 1486 | struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1487 | struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1488 | struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL; |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1489 | struct nd_namespace_common *ndns = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1490 | resource_size_t size; |
| 1491 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1492 | if (nd_btt || nd_pfn || nd_dax) { |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1493 | if (nd_btt) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1494 | ndns = nd_btt->ndns; |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1495 | else if (nd_pfn) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1496 | ndns = nd_pfn->ndns; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1497 | else if (nd_dax) |
| 1498 | ndns = nd_dax->nd_pfn.ndns; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1499 | |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1500 | if (!ndns) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1501 | return ERR_PTR(-ENODEV); |
| 1502 | |
| 1503 | /* |
| 1504 | * Flush any in-progess probes / removals in the driver |
| 1505 | * for the raw personality of this namespace. |
| 1506 | */ |
| 1507 | device_lock(&ndns->dev); |
| 1508 | device_unlock(&ndns->dev); |
| 1509 | if (ndns->dev.driver) { |
| 1510 | dev_dbg(&ndns->dev, "is active, can't bind %s\n", |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1511 | dev_name(dev)); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1512 | return ERR_PTR(-EBUSY); |
| 1513 | } |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1514 | if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1515 | "host (%s) vs claim (%s) mismatch\n", |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1516 | dev_name(dev), |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1517 | dev_name(ndns->claim))) |
| 1518 | return ERR_PTR(-ENXIO); |
| 1519 | } else { |
| 1520 | ndns = to_ndns(dev); |
| 1521 | if (ndns->claim) { |
| 1522 | dev_dbg(dev, "claimed by %s, failing probe\n", |
| 1523 | dev_name(ndns->claim)); |
| 1524 | |
| 1525 | return ERR_PTR(-ENXIO); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | size = nvdimm_namespace_capacity(ndns); |
| 1530 | if (size < ND_MIN_NAMESPACE_SIZE) { |
| 1531 | dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", |
| 1532 | &size, ND_MIN_NAMESPACE_SIZE); |
| 1533 | return ERR_PTR(-ENODEV); |
| 1534 | } |
| 1535 | |
| 1536 | if (is_namespace_pmem(&ndns->dev)) { |
| 1537 | struct nd_namespace_pmem *nspm; |
| 1538 | |
| 1539 | nspm = to_nd_namespace_pmem(&ndns->dev); |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 1540 | if (uuid_not_set(nspm->uuid, &ndns->dev, __func__)) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1541 | return ERR_PTR(-ENODEV); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1542 | } else if (is_namespace_blk(&ndns->dev)) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1543 | struct nd_namespace_blk *nsblk; |
| 1544 | |
| 1545 | nsblk = to_nd_namespace_blk(&ndns->dev); |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 1546 | if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__)) |
| 1547 | return ERR_PTR(-ENODEV); |
| 1548 | if (!nsblk->lbasize) { |
| 1549 | dev_dbg(&ndns->dev, "%s: sector size not set\n", |
| 1550 | __func__); |
| 1551 | return ERR_PTR(-ENODEV); |
| 1552 | } |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1553 | if (!nd_namespace_blk_validate(nsblk)) |
| 1554 | return ERR_PTR(-ENODEV); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | return ndns; |
| 1558 | } |
| 1559 | EXPORT_SYMBOL(nvdimm_namespace_common_probe); |
| 1560 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1561 | static struct device **create_namespace_io(struct nd_region *nd_region) |
| 1562 | { |
| 1563 | struct nd_namespace_io *nsio; |
| 1564 | struct device *dev, **devs; |
| 1565 | struct resource *res; |
| 1566 | |
| 1567 | nsio = kzalloc(sizeof(*nsio), GFP_KERNEL); |
| 1568 | if (!nsio) |
| 1569 | return NULL; |
| 1570 | |
| 1571 | devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); |
| 1572 | if (!devs) { |
| 1573 | kfree(nsio); |
| 1574 | return NULL; |
| 1575 | } |
| 1576 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1577 | dev = &nsio->common.dev; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1578 | dev->type = &namespace_io_device_type; |
| 1579 | dev->parent = &nd_region->dev; |
| 1580 | res = &nsio->res; |
| 1581 | res->name = dev_name(&nd_region->dev); |
| 1582 | res->flags = IORESOURCE_MEM; |
| 1583 | res->start = nd_region->ndr_start; |
| 1584 | res->end = res->start + nd_region->ndr_size - 1; |
| 1585 | |
| 1586 | devs[0] = dev; |
| 1587 | return devs; |
| 1588 | } |
| 1589 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1590 | static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid, |
| 1591 | u64 cookie, u16 pos) |
| 1592 | { |
| 1593 | struct nd_namespace_label *found = NULL; |
| 1594 | int i; |
| 1595 | |
| 1596 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1597 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1598 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1599 | bool found_uuid = false; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1600 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1601 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
| 1602 | struct nd_namespace_label *nd_label = label_ent->label; |
| 1603 | u16 position, nlabel; |
| 1604 | u64 isetcookie; |
| 1605 | |
| 1606 | if (!nd_label) |
| 1607 | continue; |
| 1608 | isetcookie = __le64_to_cpu(nd_label->isetcookie); |
| 1609 | position = __le16_to_cpu(nd_label->position); |
| 1610 | nlabel = __le16_to_cpu(nd_label->nlabel); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1611 | |
| 1612 | if (isetcookie != cookie) |
| 1613 | continue; |
| 1614 | |
| 1615 | if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0) |
| 1616 | continue; |
| 1617 | |
| 1618 | if (found_uuid) { |
| 1619 | dev_dbg(to_ndd(nd_mapping)->dev, |
| 1620 | "%s duplicate entry for uuid\n", |
| 1621 | __func__); |
| 1622 | return false; |
| 1623 | } |
| 1624 | found_uuid = true; |
| 1625 | if (nlabel != nd_region->ndr_mappings) |
| 1626 | continue; |
| 1627 | if (position != pos) |
| 1628 | continue; |
| 1629 | found = nd_label; |
| 1630 | break; |
| 1631 | } |
| 1632 | if (found) |
| 1633 | break; |
| 1634 | } |
| 1635 | return found != NULL; |
| 1636 | } |
| 1637 | |
| 1638 | static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id) |
| 1639 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1640 | int i; |
| 1641 | |
| 1642 | if (!pmem_id) |
| 1643 | return -ENODEV; |
| 1644 | |
| 1645 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1646 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1647 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1648 | struct nd_namespace_label *nd_label = NULL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1649 | u64 hw_start, hw_end, pmem_start, pmem_end; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1650 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1651 | |
Dan Williams | 9cf8bd5 | 2016-12-15 20:04:31 -0800 | [diff] [blame] | 1652 | lockdep_assert_held(&nd_mapping->lock); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1653 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
| 1654 | nd_label = label_ent->label; |
| 1655 | if (!nd_label) |
| 1656 | continue; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1657 | if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0) |
| 1658 | break; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1659 | nd_label = NULL; |
| 1660 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1661 | |
| 1662 | if (!nd_label) { |
| 1663 | WARN_ON(1); |
| 1664 | return -EINVAL; |
| 1665 | } |
| 1666 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1667 | /* |
| 1668 | * Check that this label is compliant with the dpa |
| 1669 | * range published in NFIT |
| 1670 | */ |
| 1671 | hw_start = nd_mapping->start; |
| 1672 | hw_end = hw_start + nd_mapping->size; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1673 | pmem_start = __le64_to_cpu(nd_label->dpa); |
| 1674 | pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize); |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1675 | if (pmem_start >= hw_start && pmem_start < hw_end |
| 1676 | && pmem_end <= hw_end && pmem_end > hw_start) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1677 | /* pass */; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1678 | else { |
| 1679 | dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n", |
| 1680 | dev_name(ndd->dev), nd_label->uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1681 | return -EINVAL; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1682 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1683 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1684 | /* move recently validated label to the front of the list */ |
| 1685 | list_move(&label_ent->list, &nd_mapping->labels); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1686 | } |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1691 | * create_namespace_pmem - validate interleave set labelling, retrieve label0 |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1692 | * @nd_region: region with mappings to validate |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1693 | * @nspm: target namespace to create |
| 1694 | * @nd_label: target pmem namespace label to evaluate |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1695 | */ |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1696 | struct device *create_namespace_pmem(struct nd_region *nd_region, |
| 1697 | struct nd_namespace_label *nd_label) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1698 | { |
Dan Williams | 86ef58a | 2017-02-28 18:32:48 -0800 | [diff] [blame] | 1699 | u64 altcookie = nd_region_interleave_set_altcookie(nd_region); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1700 | u64 cookie = nd_region_interleave_set_cookie(nd_region); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1701 | struct nd_label_ent *label_ent; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1702 | struct nd_namespace_pmem *nspm; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1703 | struct nd_mapping *nd_mapping; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1704 | resource_size_t size = 0; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1705 | struct resource *res; |
| 1706 | struct device *dev; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1707 | int rc = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1708 | u16 i; |
| 1709 | |
Dan Williams | 4765218 | 2016-09-15 18:08:05 -0700 | [diff] [blame] | 1710 | if (cookie == 0) { |
| 1711 | dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n"); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1712 | return ERR_PTR(-ENXIO); |
Dan Williams | 4765218 | 2016-09-15 18:08:05 -0700 | [diff] [blame] | 1713 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1714 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1715 | if (__le64_to_cpu(nd_label->isetcookie) != cookie) { |
| 1716 | dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n", |
| 1717 | nd_label->uuid); |
Dan Williams | 86ef58a | 2017-02-28 18:32:48 -0800 | [diff] [blame] | 1718 | if (__le64_to_cpu(nd_label->isetcookie) != altcookie) |
| 1719 | return ERR_PTR(-EAGAIN); |
| 1720 | |
| 1721 | dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n", |
| 1722 | nd_label->uuid); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1723 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1724 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1725 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
| 1726 | if (!nspm) |
| 1727 | return ERR_PTR(-ENOMEM); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1728 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1729 | nspm->id = -1; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1730 | dev = &nspm->nsio.common.dev; |
| 1731 | dev->type = &namespace_pmem_device_type; |
| 1732 | dev->parent = &nd_region->dev; |
| 1733 | res = &nspm->nsio.res; |
| 1734 | res->name = dev_name(&nd_region->dev); |
| 1735 | res->flags = IORESOURCE_MEM; |
| 1736 | |
Dan Williams | 86ef58a | 2017-02-28 18:32:48 -0800 | [diff] [blame] | 1737 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1738 | if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i)) |
| 1739 | continue; |
| 1740 | if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i)) |
| 1741 | continue; |
| 1742 | break; |
| 1743 | } |
| 1744 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1745 | if (i < nd_region->ndr_mappings) { |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1746 | struct nvdimm_drvdata *ndd = to_ndd(&nd_region->mapping[i]); |
| 1747 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1748 | /* |
| 1749 | * Give up if we don't find an instance of a uuid at each |
| 1750 | * position (from 0 to nd_region->ndr_mappings - 1), or if we |
| 1751 | * find a dimm with two instances of the same uuid. |
| 1752 | */ |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1753 | dev_err(&nd_region->dev, "%s missing label for %pUb\n", |
| 1754 | dev_name(ndd->dev), nd_label->uuid); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1755 | rc = -EINVAL; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1756 | goto err; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1757 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1758 | |
| 1759 | /* |
| 1760 | * Fix up each mapping's 'labels' to have the validated pmem label for |
| 1761 | * that position at labels[0], and NULL at labels[1]. In the process, |
| 1762 | * check that the namespace aligns with interleave-set. We know |
| 1763 | * that it does not overlap with any blk namespaces by virtue of |
| 1764 | * the dimm being enabled (i.e. nd_label_reserve_dpa() |
| 1765 | * succeeded). |
| 1766 | */ |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1767 | rc = select_pmem_id(nd_region, nd_label->uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1768 | if (rc) |
| 1769 | goto err; |
| 1770 | |
| 1771 | /* Calculate total size and populate namespace properties from label0 */ |
| 1772 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1773 | struct nd_namespace_label *label0; |
| 1774 | |
| 1775 | nd_mapping = &nd_region->mapping[i]; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1776 | label_ent = list_first_entry_or_null(&nd_mapping->labels, |
| 1777 | typeof(*label_ent), list); |
| 1778 | label0 = label_ent ? label_ent->label : 0; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1779 | |
| 1780 | if (!label0) { |
| 1781 | WARN_ON(1); |
| 1782 | continue; |
| 1783 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1784 | |
| 1785 | size += __le64_to_cpu(label0->rawsize); |
| 1786 | if (__le16_to_cpu(label0->position) != 0) |
| 1787 | continue; |
| 1788 | WARN_ON(nspm->alt_name || nspm->uuid); |
| 1789 | nspm->alt_name = kmemdup((void __force *) label0->name, |
| 1790 | NSLABEL_NAME_LEN, GFP_KERNEL); |
| 1791 | nspm->uuid = kmemdup((void __force *) label0->uuid, |
| 1792 | NSLABEL_UUID_LEN, GFP_KERNEL); |
| 1793 | } |
| 1794 | |
| 1795 | if (!nspm->alt_name || !nspm->uuid) { |
| 1796 | rc = -ENOMEM; |
| 1797 | goto err; |
| 1798 | } |
| 1799 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1800 | nd_namespace_pmem_set_resource(nd_region, nspm, size); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1801 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1802 | return dev; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1803 | err: |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1804 | namespace_pmem_release(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1805 | switch (rc) { |
| 1806 | case -EINVAL: |
| 1807 | dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__); |
| 1808 | break; |
| 1809 | case -ENODEV: |
| 1810 | dev_dbg(&nd_region->dev, "%s: label not found\n", __func__); |
| 1811 | break; |
| 1812 | default: |
| 1813 | dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n", |
| 1814 | __func__, rc); |
| 1815 | break; |
| 1816 | } |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1817 | return ERR_PTR(rc); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1818 | } |
| 1819 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1820 | struct resource *nsblk_add_resource(struct nd_region *nd_region, |
| 1821 | struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk, |
| 1822 | resource_size_t start) |
| 1823 | { |
| 1824 | struct nd_label_id label_id; |
| 1825 | struct resource *res; |
| 1826 | |
| 1827 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); |
| 1828 | res = krealloc(nsblk->res, |
| 1829 | sizeof(void *) * (nsblk->num_resources + 1), |
| 1830 | GFP_KERNEL); |
| 1831 | if (!res) |
| 1832 | return NULL; |
| 1833 | nsblk->res = (struct resource **) res; |
| 1834 | for_each_dpa_resource(ndd, res) |
| 1835 | if (strcmp(res->name, label_id.id) == 0 |
| 1836 | && res->start == start) { |
| 1837 | nsblk->res[nsblk->num_resources++] = res; |
| 1838 | return res; |
| 1839 | } |
| 1840 | return NULL; |
| 1841 | } |
| 1842 | |
| 1843 | static struct device *nd_namespace_blk_create(struct nd_region *nd_region) |
| 1844 | { |
| 1845 | struct nd_namespace_blk *nsblk; |
| 1846 | struct device *dev; |
| 1847 | |
| 1848 | if (!is_nd_blk(&nd_region->dev)) |
| 1849 | return NULL; |
| 1850 | |
| 1851 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); |
| 1852 | if (!nsblk) |
| 1853 | return NULL; |
| 1854 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1855 | dev = &nsblk->common.dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1856 | dev->type = &namespace_blk_device_type; |
| 1857 | nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); |
| 1858 | if (nsblk->id < 0) { |
| 1859 | kfree(nsblk); |
| 1860 | return NULL; |
| 1861 | } |
| 1862 | dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id); |
| 1863 | dev->parent = &nd_region->dev; |
| 1864 | dev->groups = nd_namespace_attribute_groups; |
| 1865 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1866 | return &nsblk->common.dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1867 | } |
| 1868 | |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 1869 | static struct device *nd_namespace_pmem_create(struct nd_region *nd_region) |
| 1870 | { |
| 1871 | struct nd_namespace_pmem *nspm; |
| 1872 | struct resource *res; |
| 1873 | struct device *dev; |
| 1874 | |
| 1875 | if (!is_nd_pmem(&nd_region->dev)) |
| 1876 | return NULL; |
| 1877 | |
| 1878 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
| 1879 | if (!nspm) |
| 1880 | return NULL; |
| 1881 | |
| 1882 | dev = &nspm->nsio.common.dev; |
| 1883 | dev->type = &namespace_pmem_device_type; |
| 1884 | dev->parent = &nd_region->dev; |
| 1885 | res = &nspm->nsio.res; |
| 1886 | res->name = dev_name(&nd_region->dev); |
| 1887 | res->flags = IORESOURCE_MEM; |
| 1888 | |
| 1889 | nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); |
| 1890 | if (nspm->id < 0) { |
| 1891 | kfree(nspm); |
| 1892 | return NULL; |
| 1893 | } |
| 1894 | dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id); |
| 1895 | dev->parent = &nd_region->dev; |
| 1896 | dev->groups = nd_namespace_attribute_groups; |
| 1897 | nd_namespace_pmem_set_resource(nd_region, nspm, 0); |
| 1898 | |
| 1899 | return dev; |
| 1900 | } |
| 1901 | |
| 1902 | void nd_region_create_ns_seed(struct nd_region *nd_region) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1903 | { |
| 1904 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 1905 | |
| 1906 | if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO) |
| 1907 | return; |
| 1908 | |
| 1909 | if (is_nd_blk(&nd_region->dev)) |
| 1910 | nd_region->ns_seed = nd_namespace_blk_create(nd_region); |
| 1911 | else |
| 1912 | nd_region->ns_seed = nd_namespace_pmem_create(nd_region); |
| 1913 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1914 | /* |
| 1915 | * Seed creation failures are not fatal, provisioning is simply |
| 1916 | * disabled until memory becomes available |
| 1917 | */ |
| 1918 | if (!nd_region->ns_seed) |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 1919 | dev_err(&nd_region->dev, "failed to create %s namespace\n", |
| 1920 | is_nd_blk(&nd_region->dev) ? "blk" : "pmem"); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1921 | else |
| 1922 | nd_device_register(nd_region->ns_seed); |
| 1923 | } |
| 1924 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1925 | void nd_region_create_dax_seed(struct nd_region *nd_region) |
| 1926 | { |
| 1927 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 1928 | nd_region->dax_seed = nd_dax_create(nd_region); |
| 1929 | /* |
| 1930 | * Seed creation failures are not fatal, provisioning is simply |
| 1931 | * disabled until memory becomes available |
| 1932 | */ |
| 1933 | if (!nd_region->dax_seed) |
| 1934 | dev_err(&nd_region->dev, "failed to create dax namespace\n"); |
| 1935 | } |
| 1936 | |
Dan Williams | 2dc4333 | 2015-12-13 11:41:36 -0800 | [diff] [blame] | 1937 | void nd_region_create_pfn_seed(struct nd_region *nd_region) |
| 1938 | { |
| 1939 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 1940 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
| 1941 | /* |
| 1942 | * Seed creation failures are not fatal, provisioning is simply |
| 1943 | * disabled until memory becomes available |
| 1944 | */ |
| 1945 | if (!nd_region->pfn_seed) |
| 1946 | dev_err(&nd_region->dev, "failed to create pfn namespace\n"); |
| 1947 | } |
| 1948 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1949 | void nd_region_create_btt_seed(struct nd_region *nd_region) |
| 1950 | { |
| 1951 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 1952 | nd_region->btt_seed = nd_btt_create(nd_region); |
| 1953 | /* |
| 1954 | * Seed creation failures are not fatal, provisioning is simply |
| 1955 | * disabled until memory becomes available |
| 1956 | */ |
| 1957 | if (!nd_region->btt_seed) |
| 1958 | dev_err(&nd_region->dev, "failed to create btt namespace\n"); |
| 1959 | } |
| 1960 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1961 | static int add_namespace_resource(struct nd_region *nd_region, |
| 1962 | struct nd_namespace_label *nd_label, struct device **devs, |
| 1963 | int count) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1964 | { |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1965 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1966 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1967 | int i; |
| 1968 | |
| 1969 | for (i = 0; i < count; i++) { |
| 1970 | u8 *uuid = namespace_to_uuid(devs[i]); |
| 1971 | struct resource *res; |
| 1972 | |
| 1973 | if (IS_ERR_OR_NULL(uuid)) { |
| 1974 | WARN_ON(1); |
| 1975 | continue; |
| 1976 | } |
| 1977 | |
| 1978 | if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0) |
| 1979 | continue; |
| 1980 | if (is_namespace_blk(devs[i])) { |
| 1981 | res = nsblk_add_resource(nd_region, ndd, |
| 1982 | to_nd_namespace_blk(devs[i]), |
| 1983 | __le64_to_cpu(nd_label->dpa)); |
| 1984 | if (!res) |
| 1985 | return -ENXIO; |
| 1986 | nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count); |
| 1987 | } else { |
| 1988 | dev_err(&nd_region->dev, |
| 1989 | "error: conflicting extents for uuid: %pUb\n", |
| 1990 | nd_label->uuid); |
| 1991 | return -ENXIO; |
| 1992 | } |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | return i; |
| 1997 | } |
| 1998 | |
| 1999 | struct device *create_namespace_blk(struct nd_region *nd_region, |
| 2000 | struct nd_namespace_label *nd_label, int count) |
| 2001 | { |
| 2002 | |
| 2003 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 2004 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2005 | struct nd_namespace_blk *nsblk; |
Nicolas Iooss | 238b323 | 2016-11-26 20:18:04 +0100 | [diff] [blame] | 2006 | char name[NSLABEL_NAME_LEN]; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2007 | struct device *dev = NULL; |
| 2008 | struct resource *res; |
| 2009 | |
| 2010 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); |
| 2011 | if (!nsblk) |
| 2012 | return ERR_PTR(-ENOMEM); |
| 2013 | dev = &nsblk->common.dev; |
| 2014 | dev->type = &namespace_blk_device_type; |
| 2015 | dev->parent = &nd_region->dev; |
| 2016 | nsblk->id = -1; |
| 2017 | nsblk->lbasize = __le64_to_cpu(nd_label->lbasize); |
| 2018 | nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN, |
| 2019 | GFP_KERNEL); |
| 2020 | if (!nsblk->uuid) |
| 2021 | goto blk_err; |
| 2022 | memcpy(name, nd_label->name, NSLABEL_NAME_LEN); |
| 2023 | if (name[0]) |
| 2024 | nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, |
| 2025 | GFP_KERNEL); |
| 2026 | res = nsblk_add_resource(nd_region, ndd, nsblk, |
| 2027 | __le64_to_cpu(nd_label->dpa)); |
| 2028 | if (!res) |
| 2029 | goto blk_err; |
| 2030 | nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count); |
| 2031 | return dev; |
| 2032 | blk_err: |
| 2033 | namespace_blk_release(dev); |
| 2034 | return ERR_PTR(-ENXIO); |
| 2035 | } |
| 2036 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 2037 | static int cmp_dpa(const void *a, const void *b) |
| 2038 | { |
| 2039 | const struct device *dev_a = *(const struct device **) a; |
| 2040 | const struct device *dev_b = *(const struct device **) b; |
| 2041 | struct nd_namespace_blk *nsblk_a, *nsblk_b; |
| 2042 | struct nd_namespace_pmem *nspm_a, *nspm_b; |
| 2043 | |
| 2044 | if (is_namespace_io(dev_a)) |
| 2045 | return 0; |
| 2046 | |
| 2047 | if (is_namespace_blk(dev_a)) { |
| 2048 | nsblk_a = to_nd_namespace_blk(dev_a); |
| 2049 | nsblk_b = to_nd_namespace_blk(dev_b); |
| 2050 | |
| 2051 | return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start, |
| 2052 | sizeof(resource_size_t)); |
| 2053 | } |
| 2054 | |
| 2055 | nspm_a = to_nd_namespace_pmem(dev_a); |
| 2056 | nspm_b = to_nd_namespace_pmem(dev_b); |
| 2057 | |
| 2058 | return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start, |
| 2059 | sizeof(resource_size_t)); |
| 2060 | } |
| 2061 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2062 | static struct device **scan_labels(struct nd_region *nd_region) |
| 2063 | { |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2064 | int i, count = 0; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2065 | struct device *dev, **devs = NULL; |
| 2066 | struct nd_label_ent *label_ent, *e; |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2067 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 2068 | resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2069 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2070 | /* "safe" because create_namespace_pmem() might list_move() label_ent */ |
| 2071 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2072 | struct nd_namespace_label *nd_label = label_ent->label; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2073 | struct device **__devs; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2074 | u32 flags; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2075 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2076 | if (!nd_label) |
| 2077 | continue; |
| 2078 | flags = __le32_to_cpu(nd_label->flags); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2079 | if (is_nd_blk(&nd_region->dev) |
| 2080 | == !!(flags & NSLABEL_FLAG_LOCAL)) |
| 2081 | /* pass, region matches label type */; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2082 | else |
| 2083 | continue; |
| 2084 | |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2085 | /* skip labels that describe extents outside of the region */ |
| 2086 | if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end) |
| 2087 | continue; |
| 2088 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2089 | i = add_namespace_resource(nd_region, nd_label, devs, count); |
| 2090 | if (i < 0) |
| 2091 | goto err; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2092 | if (i < count) |
| 2093 | continue; |
| 2094 | __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL); |
| 2095 | if (!__devs) |
| 2096 | goto err; |
| 2097 | memcpy(__devs, devs, sizeof(dev) * count); |
| 2098 | kfree(devs); |
| 2099 | devs = __devs; |
| 2100 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2101 | if (is_nd_blk(&nd_region->dev)) { |
| 2102 | dev = create_namespace_blk(nd_region, nd_label, count); |
| 2103 | if (IS_ERR(dev)) |
| 2104 | goto err; |
| 2105 | devs[count++] = dev; |
| 2106 | } else { |
| 2107 | dev = create_namespace_pmem(nd_region, nd_label); |
| 2108 | if (IS_ERR(dev)) { |
| 2109 | switch (PTR_ERR(dev)) { |
| 2110 | case -EAGAIN: |
| 2111 | /* skip invalid labels */ |
| 2112 | continue; |
| 2113 | case -ENODEV: |
| 2114 | /* fallthrough to seed creation */ |
| 2115 | break; |
| 2116 | default: |
| 2117 | goto err; |
| 2118 | } |
| 2119 | } else |
| 2120 | devs[count++] = dev; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2121 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2122 | } |
| 2123 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2124 | dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n", |
| 2125 | __func__, count, is_nd_blk(&nd_region->dev) |
| 2126 | ? "blk" : "pmem", count == 1 ? "" : "s"); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2127 | |
| 2128 | if (count == 0) { |
| 2129 | /* Publish a zero-sized namespace for userspace to configure. */ |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2130 | nd_mapping_free_labels(nd_mapping); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2131 | |
| 2132 | devs = kcalloc(2, sizeof(dev), GFP_KERNEL); |
| 2133 | if (!devs) |
| 2134 | goto err; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2135 | if (is_nd_blk(&nd_region->dev)) { |
| 2136 | struct nd_namespace_blk *nsblk; |
| 2137 | |
| 2138 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); |
| 2139 | if (!nsblk) |
| 2140 | goto err; |
| 2141 | dev = &nsblk->common.dev; |
| 2142 | dev->type = &namespace_blk_device_type; |
| 2143 | } else { |
| 2144 | struct nd_namespace_pmem *nspm; |
| 2145 | |
| 2146 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
| 2147 | if (!nspm) |
| 2148 | goto err; |
| 2149 | dev = &nspm->nsio.common.dev; |
| 2150 | dev->type = &namespace_pmem_device_type; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2151 | nd_namespace_pmem_set_resource(nd_region, nspm, 0); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2152 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2153 | dev->parent = &nd_region->dev; |
| 2154 | devs[count++] = dev; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2155 | } else if (is_nd_pmem(&nd_region->dev)) { |
| 2156 | /* clean unselected labels */ |
| 2157 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2158 | struct list_head *l, *e; |
| 2159 | LIST_HEAD(list); |
| 2160 | int j; |
| 2161 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2162 | nd_mapping = &nd_region->mapping[i]; |
| 2163 | if (list_empty(&nd_mapping->labels)) { |
| 2164 | WARN_ON(1); |
| 2165 | continue; |
| 2166 | } |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2167 | |
| 2168 | j = count; |
| 2169 | list_for_each_safe(l, e, &nd_mapping->labels) { |
| 2170 | if (!j--) |
| 2171 | break; |
| 2172 | list_move_tail(l, &list); |
| 2173 | } |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2174 | nd_mapping_free_labels(nd_mapping); |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2175 | list_splice_init(&list, &nd_mapping->labels); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2176 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2177 | } |
| 2178 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 2179 | if (count > 1) |
| 2180 | sort(devs, count, sizeof(struct device *), cmp_dpa, NULL); |
| 2181 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2182 | return devs; |
| 2183 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2184 | err: |
Dan Carpenter | 75d2971 | 2016-10-12 09:34:29 +0300 | [diff] [blame] | 2185 | if (devs) { |
| 2186 | for (i = 0; devs[i]; i++) |
| 2187 | if (is_nd_blk(&nd_region->dev)) |
| 2188 | namespace_blk_release(devs[i]); |
| 2189 | else |
| 2190 | namespace_pmem_release(devs[i]); |
| 2191 | kfree(devs); |
| 2192 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2193 | return NULL; |
| 2194 | } |
| 2195 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2196 | static struct device **create_namespaces(struct nd_region *nd_region) |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2197 | { |
| 2198 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 2199 | struct device **devs; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2200 | int i; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2201 | |
| 2202 | if (nd_region->ndr_mappings == 0) |
| 2203 | return NULL; |
| 2204 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2205 | /* lock down all mappings while we scan labels */ |
| 2206 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2207 | nd_mapping = &nd_region->mapping[i]; |
| 2208 | mutex_lock_nested(&nd_mapping->lock, i); |
| 2209 | } |
| 2210 | |
| 2211 | devs = scan_labels(nd_region); |
| 2212 | |
| 2213 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2214 | int reverse = nd_region->ndr_mappings - 1 - i; |
| 2215 | |
| 2216 | nd_mapping = &nd_region->mapping[reverse]; |
| 2217 | mutex_unlock(&nd_mapping->lock); |
| 2218 | } |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2219 | |
| 2220 | return devs; |
| 2221 | } |
| 2222 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2223 | static int init_active_labels(struct nd_region *nd_region) |
| 2224 | { |
| 2225 | int i; |
| 2226 | |
| 2227 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2228 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 2229 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 2230 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2231 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2232 | int count, j; |
| 2233 | |
| 2234 | /* |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 2235 | * If the dimm is disabled then we may need to prevent |
| 2236 | * the region from being activated. |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2237 | */ |
| 2238 | if (!ndd) { |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 2239 | if (test_bit(NDD_LOCKED, &nvdimm->flags)) |
| 2240 | /* fail, label data may be unreadable */; |
| 2241 | else if (test_bit(NDD_ALIASING, &nvdimm->flags)) |
| 2242 | /* fail, labels needed to disambiguate dpa */; |
| 2243 | else |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2244 | return 0; |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 2245 | |
| 2246 | dev_err(&nd_region->dev, "%s: is %s, failing probe\n", |
| 2247 | dev_name(&nd_mapping->nvdimm->dev), |
| 2248 | test_bit(NDD_LOCKED, &nvdimm->flags) |
| 2249 | ? "locked" : "disabled"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2250 | return -ENXIO; |
| 2251 | } |
| 2252 | nd_mapping->ndd = ndd; |
| 2253 | atomic_inc(&nvdimm->busy); |
| 2254 | get_ndd(ndd); |
| 2255 | |
| 2256 | count = nd_label_active_count(ndd); |
| 2257 | dev_dbg(ndd->dev, "%s: %d\n", __func__, count); |
| 2258 | if (!count) |
| 2259 | continue; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2260 | for (j = 0; j < count; j++) { |
| 2261 | struct nd_namespace_label *label; |
| 2262 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2263 | label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL); |
| 2264 | if (!label_ent) |
| 2265 | break; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2266 | label = nd_label_active(ndd, j); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2267 | label_ent->label = label; |
| 2268 | |
| 2269 | mutex_lock(&nd_mapping->lock); |
| 2270 | list_add_tail(&label_ent->list, &nd_mapping->labels); |
| 2271 | mutex_unlock(&nd_mapping->lock); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2272 | } |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2273 | |
| 2274 | if (j >= count) |
| 2275 | continue; |
| 2276 | |
| 2277 | mutex_lock(&nd_mapping->lock); |
| 2278 | nd_mapping_free_labels(nd_mapping); |
| 2279 | mutex_unlock(&nd_mapping->lock); |
| 2280 | return -ENOMEM; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2286 | int nd_region_register_namespaces(struct nd_region *nd_region, int *err) |
| 2287 | { |
| 2288 | struct device **devs = NULL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2289 | int i, rc = 0, type; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2290 | |
| 2291 | *err = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2292 | nvdimm_bus_lock(&nd_region->dev); |
| 2293 | rc = init_active_labels(nd_region); |
| 2294 | if (rc) { |
| 2295 | nvdimm_bus_unlock(&nd_region->dev); |
| 2296 | return rc; |
| 2297 | } |
| 2298 | |
| 2299 | type = nd_region_to_nstype(nd_region); |
| 2300 | switch (type) { |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2301 | case ND_DEVICE_NAMESPACE_IO: |
| 2302 | devs = create_namespace_io(nd_region); |
| 2303 | break; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2304 | case ND_DEVICE_NAMESPACE_PMEM: |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2305 | case ND_DEVICE_NAMESPACE_BLK: |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2306 | devs = create_namespaces(nd_region); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2307 | break; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2308 | default: |
| 2309 | break; |
| 2310 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2311 | nvdimm_bus_unlock(&nd_region->dev); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2312 | |
| 2313 | if (!devs) |
| 2314 | return -ENODEV; |
| 2315 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2316 | for (i = 0; devs[i]; i++) { |
| 2317 | struct device *dev = devs[i]; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2318 | int id; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2319 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2320 | if (type == ND_DEVICE_NAMESPACE_BLK) { |
| 2321 | struct nd_namespace_blk *nsblk; |
| 2322 | |
| 2323 | nsblk = to_nd_namespace_blk(dev); |
| 2324 | id = ida_simple_get(&nd_region->ns_ida, 0, 0, |
| 2325 | GFP_KERNEL); |
| 2326 | nsblk->id = id; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2327 | } else if (type == ND_DEVICE_NAMESPACE_PMEM) { |
| 2328 | struct nd_namespace_pmem *nspm; |
| 2329 | |
| 2330 | nspm = to_nd_namespace_pmem(dev); |
| 2331 | id = ida_simple_get(&nd_region->ns_ida, 0, 0, |
| 2332 | GFP_KERNEL); |
| 2333 | nspm->id = id; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2334 | } else |
| 2335 | id = i; |
| 2336 | |
| 2337 | if (id < 0) |
| 2338 | break; |
| 2339 | dev_set_name(dev, "namespace%d.%d", nd_region->id, id); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2340 | dev->groups = nd_namespace_attribute_groups; |
| 2341 | nd_device_register(dev); |
| 2342 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2343 | if (i) |
| 2344 | nd_region->ns_seed = devs[0]; |
| 2345 | |
| 2346 | if (devs[i]) { |
| 2347 | int j; |
| 2348 | |
| 2349 | for (j = i; devs[j]; j++) { |
| 2350 | struct device *dev = devs[j]; |
| 2351 | |
| 2352 | device_initialize(dev); |
| 2353 | put_device(dev); |
| 2354 | } |
| 2355 | *err = j - i; |
| 2356 | /* |
| 2357 | * All of the namespaces we tried to register failed, so |
| 2358 | * fail region activation. |
| 2359 | */ |
| 2360 | if (*err == 0) |
| 2361 | rc = -ENODEV; |
| 2362 | } |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2363 | kfree(devs); |
| 2364 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2365 | if (rc == -ENODEV) |
| 2366 | return rc; |
| 2367 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2368 | return i; |
| 2369 | } |