0001
0002
0003
0004
0005 #include <linux/blkdev.h>
0006 #include <linux/device.h>
0007 #include <linux/sizes.h>
0008 #include <linux/slab.h>
0009 #include <linux/fs.h>
0010 #include <linux/mm.h>
0011 #include "nd-core.h"
0012 #include "btt.h"
0013 #include "nd.h"
0014
0015 static void nd_btt_release(struct device *dev)
0016 {
0017 struct nd_region *nd_region = to_nd_region(dev->parent);
0018 struct nd_btt *nd_btt = to_nd_btt(dev);
0019
0020 dev_dbg(dev, "trace\n");
0021 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
0022 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
0023 kfree(nd_btt->uuid);
0024 kfree(nd_btt);
0025 }
0026
0027 struct nd_btt *to_nd_btt(struct device *dev)
0028 {
0029 struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
0030
0031 WARN_ON(!is_nd_btt(dev));
0032 return nd_btt;
0033 }
0034 EXPORT_SYMBOL(to_nd_btt);
0035
0036 static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
0037 4096, 4104, 4160, 4224, 0 };
0038
0039 static ssize_t sector_size_show(struct device *dev,
0040 struct device_attribute *attr, char *buf)
0041 {
0042 struct nd_btt *nd_btt = to_nd_btt(dev);
0043
0044 return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
0045 }
0046
0047 static ssize_t sector_size_store(struct device *dev,
0048 struct device_attribute *attr, const char *buf, size_t len)
0049 {
0050 struct nd_btt *nd_btt = to_nd_btt(dev);
0051 ssize_t rc;
0052
0053 device_lock(dev);
0054 nvdimm_bus_lock(dev);
0055 rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
0056 btt_lbasize_supported);
0057 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0058 buf[len - 1] == '\n' ? "" : "\n");
0059 nvdimm_bus_unlock(dev);
0060 device_unlock(dev);
0061
0062 return rc ? rc : len;
0063 }
0064 static DEVICE_ATTR_RW(sector_size);
0065
0066 static ssize_t uuid_show(struct device *dev,
0067 struct device_attribute *attr, char *buf)
0068 {
0069 struct nd_btt *nd_btt = to_nd_btt(dev);
0070
0071 if (nd_btt->uuid)
0072 return sprintf(buf, "%pUb\n", nd_btt->uuid);
0073 return sprintf(buf, "\n");
0074 }
0075
0076 static ssize_t uuid_store(struct device *dev,
0077 struct device_attribute *attr, const char *buf, size_t len)
0078 {
0079 struct nd_btt *nd_btt = to_nd_btt(dev);
0080 ssize_t rc;
0081
0082 device_lock(dev);
0083 rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
0084 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0085 buf[len - 1] == '\n' ? "" : "\n");
0086 device_unlock(dev);
0087
0088 return rc ? rc : len;
0089 }
0090 static DEVICE_ATTR_RW(uuid);
0091
0092 static ssize_t namespace_show(struct device *dev,
0093 struct device_attribute *attr, char *buf)
0094 {
0095 struct nd_btt *nd_btt = to_nd_btt(dev);
0096 ssize_t rc;
0097
0098 nvdimm_bus_lock(dev);
0099 rc = sprintf(buf, "%s\n", nd_btt->ndns
0100 ? dev_name(&nd_btt->ndns->dev) : "");
0101 nvdimm_bus_unlock(dev);
0102 return rc;
0103 }
0104
0105 static ssize_t namespace_store(struct device *dev,
0106 struct device_attribute *attr, const char *buf, size_t len)
0107 {
0108 struct nd_btt *nd_btt = to_nd_btt(dev);
0109 ssize_t rc;
0110
0111 device_lock(dev);
0112 nvdimm_bus_lock(dev);
0113 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
0114 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0115 buf[len - 1] == '\n' ? "" : "\n");
0116 nvdimm_bus_unlock(dev);
0117 device_unlock(dev);
0118
0119 return rc;
0120 }
0121 static DEVICE_ATTR_RW(namespace);
0122
0123 static ssize_t size_show(struct device *dev,
0124 struct device_attribute *attr, char *buf)
0125 {
0126 struct nd_btt *nd_btt = to_nd_btt(dev);
0127 ssize_t rc;
0128
0129 device_lock(dev);
0130 if (dev->driver)
0131 rc = sprintf(buf, "%llu\n", nd_btt->size);
0132 else {
0133
0134 rc = -ENXIO;
0135 }
0136 device_unlock(dev);
0137
0138 return rc;
0139 }
0140 static DEVICE_ATTR_RO(size);
0141
0142 static ssize_t log_zero_flags_show(struct device *dev,
0143 struct device_attribute *attr, char *buf)
0144 {
0145 return sprintf(buf, "Y\n");
0146 }
0147 static DEVICE_ATTR_RO(log_zero_flags);
0148
0149 static struct attribute *nd_btt_attributes[] = {
0150 &dev_attr_sector_size.attr,
0151 &dev_attr_namespace.attr,
0152 &dev_attr_uuid.attr,
0153 &dev_attr_size.attr,
0154 &dev_attr_log_zero_flags.attr,
0155 NULL,
0156 };
0157
0158 static struct attribute_group nd_btt_attribute_group = {
0159 .attrs = nd_btt_attributes,
0160 };
0161
0162 static const struct attribute_group *nd_btt_attribute_groups[] = {
0163 &nd_btt_attribute_group,
0164 &nd_device_attribute_group,
0165 &nd_numa_attribute_group,
0166 NULL,
0167 };
0168
0169 static const struct device_type nd_btt_device_type = {
0170 .name = "nd_btt",
0171 .release = nd_btt_release,
0172 .groups = nd_btt_attribute_groups,
0173 };
0174
0175 bool is_nd_btt(struct device *dev)
0176 {
0177 return dev->type == &nd_btt_device_type;
0178 }
0179 EXPORT_SYMBOL(is_nd_btt);
0180
0181 static struct lock_class_key nvdimm_btt_key;
0182
0183 static struct device *__nd_btt_create(struct nd_region *nd_region,
0184 unsigned long lbasize, uuid_t *uuid,
0185 struct nd_namespace_common *ndns)
0186 {
0187 struct nd_btt *nd_btt;
0188 struct device *dev;
0189
0190 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
0191 if (!nd_btt)
0192 return NULL;
0193
0194 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
0195 if (nd_btt->id < 0)
0196 goto out_nd_btt;
0197
0198 nd_btt->lbasize = lbasize;
0199 if (uuid) {
0200 uuid = kmemdup(uuid, 16, GFP_KERNEL);
0201 if (!uuid)
0202 goto out_put_id;
0203 }
0204 nd_btt->uuid = uuid;
0205 dev = &nd_btt->dev;
0206 dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
0207 dev->parent = &nd_region->dev;
0208 dev->type = &nd_btt_device_type;
0209 device_initialize(&nd_btt->dev);
0210 lockdep_set_class(&nd_btt->dev.mutex, &nvdimm_btt_key);
0211 if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
0212 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
0213 dev_name(ndns->claim));
0214 put_device(dev);
0215 return NULL;
0216 }
0217 return dev;
0218
0219 out_put_id:
0220 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
0221
0222 out_nd_btt:
0223 kfree(nd_btt);
0224 return NULL;
0225 }
0226
0227 struct device *nd_btt_create(struct nd_region *nd_region)
0228 {
0229 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
0230
0231 nd_device_register(dev);
0232 return dev;
0233 }
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
0248 {
0249 const uuid_t *ns_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
0250 uuid_t parent_uuid;
0251 u64 checksum;
0252
0253 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
0254 return false;
0255
0256 import_uuid(&parent_uuid, super->parent_uuid);
0257 if (!uuid_is_null(&parent_uuid))
0258 if (!uuid_equal(&parent_uuid, ns_uuid))
0259 return false;
0260
0261 checksum = le64_to_cpu(super->checksum);
0262 super->checksum = 0;
0263 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
0264 return false;
0265 super->checksum = cpu_to_le64(checksum);
0266
0267
0268 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
0269 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
0270
0271 return true;
0272 }
0273 EXPORT_SYMBOL(nd_btt_arena_is_valid);
0274
0275 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
0276 struct btt_sb *btt_sb)
0277 {
0278 if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
0279
0280 nd_btt->initial_offset = 0;
0281 nd_btt->version_major = 2;
0282 nd_btt->version_minor = 0;
0283 if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
0284 return -ENXIO;
0285 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
0286 return -ENODEV;
0287 if ((le16_to_cpu(btt_sb->version_major) != 2) ||
0288 (le16_to_cpu(btt_sb->version_minor) != 0))
0289 return -ENODEV;
0290 } else {
0291
0292
0293
0294
0295 nd_btt->initial_offset = SZ_4K;
0296 nd_btt->version_major = 1;
0297 nd_btt->version_minor = 1;
0298 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
0299 return -ENXIO;
0300 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
0301 return -ENODEV;
0302 if ((le16_to_cpu(btt_sb->version_major) != 1) ||
0303 (le16_to_cpu(btt_sb->version_minor) != 1))
0304 return -ENODEV;
0305 }
0306 return 0;
0307 }
0308 EXPORT_SYMBOL(nd_btt_version);
0309
0310 static int __nd_btt_probe(struct nd_btt *nd_btt,
0311 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
0312 {
0313 int rc;
0314
0315 if (!btt_sb || !ndns || !nd_btt)
0316 return -ENODEV;
0317
0318 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
0319 return -ENXIO;
0320
0321 rc = nd_btt_version(nd_btt, ndns, btt_sb);
0322 if (rc < 0)
0323 return rc;
0324
0325 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
0326 nd_btt->uuid = kmemdup(&btt_sb->uuid, sizeof(uuid_t), GFP_KERNEL);
0327 if (!nd_btt->uuid)
0328 return -ENOMEM;
0329
0330 nd_device_register(&nd_btt->dev);
0331
0332 return 0;
0333 }
0334
0335 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
0336 {
0337 int rc;
0338 struct device *btt_dev;
0339 struct btt_sb *btt_sb;
0340 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
0341
0342 if (ndns->force_raw)
0343 return -ENODEV;
0344
0345 switch (ndns->claim_class) {
0346 case NVDIMM_CCLASS_NONE:
0347 case NVDIMM_CCLASS_BTT:
0348 case NVDIMM_CCLASS_BTT2:
0349 break;
0350 default:
0351 return -ENODEV;
0352 }
0353
0354 nvdimm_bus_lock(&ndns->dev);
0355 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
0356 nvdimm_bus_unlock(&ndns->dev);
0357 if (!btt_dev)
0358 return -ENOMEM;
0359 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
0360 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
0361 dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
0362 if (rc < 0) {
0363 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
0364
0365 nd_detach_ndns(btt_dev, &nd_btt->ndns);
0366 put_device(btt_dev);
0367 }
0368
0369 return rc;
0370 }
0371 EXPORT_SYMBOL(nd_btt_probe);