blob: 5d2c766828488784fe03c7fb2c90bbf4da87f9bc [file] [log] [blame]
Dan Williams8c2f7e82015-06-25 04:20:04 -04001/*
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/blkdev.h>
14#include <linux/device.h>
15#include <linux/genhd.h>
16#include <linux/sizes.h>
17#include <linux/slab.h>
18#include <linux/fs.h>
19#include <linux/mm.h>
20#include "nd-core.h"
21#include "btt.h"
22#include "nd.h"
23
Dan Williams8c2f7e82015-06-25 04:20:04 -040024static void nd_btt_release(struct device *dev)
25{
26 struct nd_region *nd_region = to_nd_region(dev->parent);
27 struct nd_btt *nd_btt = to_nd_btt(dev);
28
29 dev_dbg(dev, "%s\n", __func__);
Dan Williamse1455742015-07-30 17:57:47 -040030 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -040031 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
32 kfree(nd_btt->uuid);
33 kfree(nd_btt);
34}
35
36static struct device_type nd_btt_device_type = {
37 .name = "nd_btt",
38 .release = nd_btt_release,
39};
40
41bool is_nd_btt(struct device *dev)
42{
43 return dev->type == &nd_btt_device_type;
44}
45EXPORT_SYMBOL(is_nd_btt);
46
47struct nd_btt *to_nd_btt(struct device *dev)
48{
49 struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
50
51 WARN_ON(!is_nd_btt(dev));
52 return nd_btt;
53}
54EXPORT_SYMBOL(to_nd_btt);
55
Vishal Verma41cd8b72015-06-25 04:21:52 -040056static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
57 4096, 4104, 4160, 4224, 0 };
Dan Williams8c2f7e82015-06-25 04:20:04 -040058
59static ssize_t sector_size_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
61{
62 struct nd_btt *nd_btt = to_nd_btt(dev);
63
64 return nd_sector_size_show(nd_btt->lbasize, btt_lbasize_supported, buf);
65}
66
67static ssize_t sector_size_store(struct device *dev,
68 struct device_attribute *attr, const char *buf, size_t len)
69{
70 struct nd_btt *nd_btt = to_nd_btt(dev);
71 ssize_t rc;
72
73 device_lock(dev);
74 nvdimm_bus_lock(dev);
75 rc = nd_sector_size_store(dev, buf, &nd_btt->lbasize,
76 btt_lbasize_supported);
77 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
78 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
79 nvdimm_bus_unlock(dev);
80 device_unlock(dev);
81
82 return rc ? rc : len;
83}
84static DEVICE_ATTR_RW(sector_size);
85
86static ssize_t uuid_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
88{
89 struct nd_btt *nd_btt = to_nd_btt(dev);
90
91 if (nd_btt->uuid)
92 return sprintf(buf, "%pUb\n", nd_btt->uuid);
93 return sprintf(buf, "\n");
94}
95
96static ssize_t uuid_store(struct device *dev,
97 struct device_attribute *attr, const char *buf, size_t len)
98{
99 struct nd_btt *nd_btt = to_nd_btt(dev);
100 ssize_t rc;
101
102 device_lock(dev);
103 rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
104 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
105 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
106 device_unlock(dev);
107
108 return rc ? rc : len;
109}
110static DEVICE_ATTR_RW(uuid);
111
112static ssize_t namespace_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
114{
115 struct nd_btt *nd_btt = to_nd_btt(dev);
116 ssize_t rc;
117
118 nvdimm_bus_lock(dev);
119 rc = sprintf(buf, "%s\n", nd_btt->ndns
120 ? dev_name(&nd_btt->ndns->dev) : "");
121 nvdimm_bus_unlock(dev);
122 return rc;
123}
124
Dan Williams8c2f7e82015-06-25 04:20:04 -0400125static ssize_t namespace_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t len)
127{
Dan Williamse1455742015-07-30 17:57:47 -0400128 struct nd_btt *nd_btt = to_nd_btt(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400129 ssize_t rc;
130
Dan Williams8c2f7e82015-06-25 04:20:04 -0400131 device_lock(dev);
Axel Lin4be9c1f2015-09-16 21:24:47 +0800132 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400133 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400134 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
135 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400136 nvdimm_bus_unlock(dev);
Axel Lin4be9c1f2015-09-16 21:24:47 +0800137 device_unlock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400138
139 return rc;
140}
141static DEVICE_ATTR_RW(namespace);
142
Vishal Vermaabe8b4e2016-07-27 16:38:59 -0600143static ssize_t size_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
145{
146 struct nd_btt *nd_btt = to_nd_btt(dev);
147 ssize_t rc;
148
149 device_lock(dev);
150 if (dev->driver)
151 rc = sprintf(buf, "%llu\n", nd_btt->size);
152 else {
153 /* no size to convey if the btt instance is disabled */
154 rc = -ENXIO;
155 }
156 device_unlock(dev);
157
158 return rc;
159}
160static DEVICE_ATTR_RO(size);
161
Dan Williams8c2f7e82015-06-25 04:20:04 -0400162static struct attribute *nd_btt_attributes[] = {
163 &dev_attr_sector_size.attr,
164 &dev_attr_namespace.attr,
165 &dev_attr_uuid.attr,
Vishal Vermaabe8b4e2016-07-27 16:38:59 -0600166 &dev_attr_size.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -0400167 NULL,
168};
169
170static struct attribute_group nd_btt_attribute_group = {
171 .attrs = nd_btt_attributes,
172};
173
174static const struct attribute_group *nd_btt_attribute_groups[] = {
175 &nd_btt_attribute_group,
176 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -0600177 &nd_numa_attribute_group,
Dan Williams8c2f7e82015-06-25 04:20:04 -0400178 NULL,
179};
180
181static struct device *__nd_btt_create(struct nd_region *nd_region,
182 unsigned long lbasize, u8 *uuid,
183 struct nd_namespace_common *ndns)
184{
185 struct nd_btt *nd_btt;
186 struct device *dev;
187
188 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
189 if (!nd_btt)
190 return NULL;
191
192 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
Aditya Pakki4d8fc7d2019-03-25 16:55:27 -0500193 if (nd_btt->id < 0)
194 goto out_nd_btt;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400195
196 nd_btt->lbasize = lbasize;
Aditya Pakki4d8fc7d2019-03-25 16:55:27 -0500197 if (uuid) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400198 uuid = kmemdup(uuid, 16, GFP_KERNEL);
Aditya Pakki4d8fc7d2019-03-25 16:55:27 -0500199 if (!uuid)
200 goto out_put_id;
201 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400202 nd_btt->uuid = uuid;
203 dev = &nd_btt->dev;
204 dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
205 dev->parent = &nd_region->dev;
206 dev->type = &nd_btt_device_type;
207 dev->groups = nd_btt_attribute_groups;
208 device_initialize(&nd_btt->dev);
Dan Williamse1455742015-07-30 17:57:47 -0400209 if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400210 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
211 __func__, dev_name(ndns->claim));
212 put_device(dev);
213 return NULL;
214 }
215 return dev;
Aditya Pakki4d8fc7d2019-03-25 16:55:27 -0500216
217out_put_id:
218 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
219
220out_nd_btt:
221 kfree(nd_btt);
222 return NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400223}
224
225struct device *nd_btt_create(struct nd_region *nd_region)
226{
227 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
228
Markus Elfringd4c57252016-07-24 11:05:34 +0200229 __nd_device_register(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400230 return dev;
231}
232
Vishal Verma6ec68952015-07-29 14:58:09 -0600233static bool uuid_is_null(u8 *uuid)
234{
235 static const u8 null_uuid[16];
236
237 return (memcmp(uuid, null_uuid, 16) == 0);
238}
239
Vishal Vermaab45e762015-07-29 14:58:08 -0600240/**
241 * nd_btt_arena_is_valid - check if the metadata layout is valid
242 * @nd_btt: device with BTT geometry and backing device info
243 * @super: pointer to the arena's info block being tested
244 *
245 * Check consistency of the btt info block with itself by validating
Vishal Verma6ec68952015-07-29 14:58:09 -0600246 * the checksum, and with the parent namespace by verifying the
247 * parent_uuid contained in the info block with the one supplied in.
Vishal Vermaab45e762015-07-29 14:58:08 -0600248 *
249 * Returns:
250 * false for an invalid info block, true for a valid one
251 */
252bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
253{
Vishal Verma6ec68952015-07-29 14:58:09 -0600254 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
Vishal Vermaab45e762015-07-29 14:58:08 -0600255 u64 checksum;
256
257 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
258 return false;
259
Vishal Verma6ec68952015-07-29 14:58:09 -0600260 if (!uuid_is_null(super->parent_uuid))
261 if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
262 return false;
263
Vishal Vermaab45e762015-07-29 14:58:08 -0600264 checksum = le64_to_cpu(super->checksum);
265 super->checksum = 0;
Dan Williamse1455742015-07-30 17:57:47 -0400266 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
Vishal Vermaab45e762015-07-29 14:58:08 -0600267 return false;
268 super->checksum = cpu_to_le64(checksum);
269
270 /* TODO: figure out action for this */
271 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
272 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
273
274 return true;
275}
276EXPORT_SYMBOL(nd_btt_arena_is_valid);
277
Dan Williams8c2f7e82015-06-25 04:20:04 -0400278static int __nd_btt_probe(struct nd_btt *nd_btt,
279 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
280{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400281 if (!btt_sb || !ndns || !nd_btt)
282 return -ENODEV;
283
284 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb)))
285 return -ENXIO;
286
287 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
288 return -ENXIO;
289
Vishal Vermaab45e762015-07-29 14:58:08 -0600290 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
Dan Williams8c2f7e82015-06-25 04:20:04 -0400291 return -ENODEV;
292
Dan Williams8c2f7e82015-06-25 04:20:04 -0400293 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
294 nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
295 if (!nd_btt->uuid)
296 return -ENOMEM;
297
298 __nd_device_register(&nd_btt->dev);
299
300 return 0;
301}
302
Dan Williams200c79d2016-03-22 00:22:16 -0700303int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400304{
305 int rc;
Dan Williamse32bc722016-03-17 18:23:09 -0700306 struct device *btt_dev;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400307 struct btt_sb *btt_sb;
308 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
309
310 if (ndns->force_raw)
311 return -ENODEV;
312
313 nvdimm_bus_lock(&ndns->dev);
Dan Williamse32bc722016-03-17 18:23:09 -0700314 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400315 nvdimm_bus_unlock(&ndns->dev);
Dan Williamse32bc722016-03-17 18:23:09 -0700316 if (!btt_dev)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400317 return -ENOMEM;
Dan Williamse32bc722016-03-17 18:23:09 -0700318 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
319 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
320 dev_dbg(dev, "%s: btt: %s\n", __func__,
321 rc == 0 ? dev_name(btt_dev) : "<none>");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400322 if (rc < 0) {
Dan Williamse32bc722016-03-17 18:23:09 -0700323 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400324
Dan Williamse32bc722016-03-17 18:23:09 -0700325 __nd_detach_ndns(btt_dev, &nd_btt->ndns);
326 put_device(btt_dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400327 }
328
329 return rc;
330}
331EXPORT_SYMBOL(nd_btt_probe);