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/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
nd_btt_release(struct device * dev)24 static 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__);
30 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
31 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
32 kfree(nd_btt->uuid);
33 kfree(nd_btt);
34 }
35
36 static struct device_type nd_btt_device_type = {
37 .name = "nd_btt",
38 .release = nd_btt_release,
39 };
40
is_nd_btt(struct device * dev)41 bool is_nd_btt(struct device *dev)
42 {
43 return dev->type == &nd_btt_device_type;
44 }
45 EXPORT_SYMBOL(is_nd_btt);
46
to_nd_btt(struct device * dev)47 struct 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 }
54 EXPORT_SYMBOL(to_nd_btt);
55
56 static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
57 4096, 4104, 4160, 4224, 0 };
58
sector_size_show(struct device * dev,struct device_attribute * attr,char * buf)59 static 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
sector_size_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)67 static 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 }
84 static DEVICE_ATTR_RW(sector_size);
85
uuid_show(struct device * dev,struct device_attribute * attr,char * buf)86 static 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
uuid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)96 static 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 }
110 static DEVICE_ATTR_RW(uuid);
111
namespace_show(struct device * dev,struct device_attribute * attr,char * buf)112 static 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
namespace_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)125 static ssize_t namespace_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t len)
127 {
128 struct nd_btt *nd_btt = to_nd_btt(dev);
129 ssize_t rc;
130
131 device_lock(dev);
132 nvdimm_bus_lock(dev);
133 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
134 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
135 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
136 nvdimm_bus_unlock(dev);
137 device_unlock(dev);
138
139 return rc;
140 }
141 static DEVICE_ATTR_RW(namespace);
142
143 static struct attribute *nd_btt_attributes[] = {
144 &dev_attr_sector_size.attr,
145 &dev_attr_namespace.attr,
146 &dev_attr_uuid.attr,
147 NULL,
148 };
149
150 static struct attribute_group nd_btt_attribute_group = {
151 .attrs = nd_btt_attributes,
152 };
153
154 static const struct attribute_group *nd_btt_attribute_groups[] = {
155 &nd_btt_attribute_group,
156 &nd_device_attribute_group,
157 &nd_numa_attribute_group,
158 NULL,
159 };
160
__nd_btt_create(struct nd_region * nd_region,unsigned long lbasize,u8 * uuid,struct nd_namespace_common * ndns)161 static struct device *__nd_btt_create(struct nd_region *nd_region,
162 unsigned long lbasize, u8 *uuid,
163 struct nd_namespace_common *ndns)
164 {
165 struct nd_btt *nd_btt;
166 struct device *dev;
167
168 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
169 if (!nd_btt)
170 return NULL;
171
172 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
173 if (nd_btt->id < 0)
174 goto out_nd_btt;
175
176 nd_btt->lbasize = lbasize;
177 if (uuid) {
178 uuid = kmemdup(uuid, 16, GFP_KERNEL);
179 if (!uuid)
180 goto out_put_id;
181 }
182 nd_btt->uuid = uuid;
183 dev = &nd_btt->dev;
184 dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
185 dev->parent = &nd_region->dev;
186 dev->type = &nd_btt_device_type;
187 dev->groups = nd_btt_attribute_groups;
188 device_initialize(&nd_btt->dev);
189 if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
190 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
191 __func__, dev_name(ndns->claim));
192 put_device(dev);
193 return NULL;
194 }
195 return dev;
196
197 out_put_id:
198 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
199
200 out_nd_btt:
201 kfree(nd_btt);
202 return NULL;
203 }
204
nd_btt_create(struct nd_region * nd_region)205 struct device *nd_btt_create(struct nd_region *nd_region)
206 {
207 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
208
209 if (dev)
210 __nd_device_register(dev);
211 return dev;
212 }
213
uuid_is_null(u8 * uuid)214 static bool uuid_is_null(u8 *uuid)
215 {
216 static const u8 null_uuid[16];
217
218 return (memcmp(uuid, null_uuid, 16) == 0);
219 }
220
221 /**
222 * nd_btt_arena_is_valid - check if the metadata layout is valid
223 * @nd_btt: device with BTT geometry and backing device info
224 * @super: pointer to the arena's info block being tested
225 *
226 * Check consistency of the btt info block with itself by validating
227 * the checksum, and with the parent namespace by verifying the
228 * parent_uuid contained in the info block with the one supplied in.
229 *
230 * Returns:
231 * false for an invalid info block, true for a valid one
232 */
nd_btt_arena_is_valid(struct nd_btt * nd_btt,struct btt_sb * super)233 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
234 {
235 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
236 u64 checksum;
237
238 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
239 return false;
240
241 if (!uuid_is_null(super->parent_uuid))
242 if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
243 return false;
244
245 checksum = le64_to_cpu(super->checksum);
246 super->checksum = 0;
247 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
248 return false;
249 super->checksum = cpu_to_le64(checksum);
250
251 /* TODO: figure out action for this */
252 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
253 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
254
255 return true;
256 }
257 EXPORT_SYMBOL(nd_btt_arena_is_valid);
258
__nd_btt_probe(struct nd_btt * nd_btt,struct nd_namespace_common * ndns,struct btt_sb * btt_sb)259 static int __nd_btt_probe(struct nd_btt *nd_btt,
260 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
261 {
262 if (!btt_sb || !ndns || !nd_btt)
263 return -ENODEV;
264
265 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb)))
266 return -ENXIO;
267
268 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
269 return -ENXIO;
270
271 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
272 return -ENODEV;
273
274 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
275 nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
276 if (!nd_btt->uuid)
277 return -ENOMEM;
278
279 __nd_device_register(&nd_btt->dev);
280
281 return 0;
282 }
283
nd_btt_probe(struct nd_namespace_common * ndns,void * drvdata)284 int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata)
285 {
286 int rc;
287 struct device *dev;
288 struct btt_sb *btt_sb;
289 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
290
291 if (ndns->force_raw)
292 return -ENODEV;
293
294 nvdimm_bus_lock(&ndns->dev);
295 dev = __nd_btt_create(nd_region, 0, NULL, ndns);
296 nvdimm_bus_unlock(&ndns->dev);
297 if (!dev)
298 return -ENOMEM;
299 dev_set_drvdata(dev, drvdata);
300 btt_sb = kzalloc(sizeof(*btt_sb), GFP_KERNEL);
301 rc = __nd_btt_probe(to_nd_btt(dev), ndns, btt_sb);
302 kfree(btt_sb);
303 dev_dbg(&ndns->dev, "%s: btt: %s\n", __func__,
304 rc == 0 ? dev_name(dev) : "<none>");
305 if (rc < 0) {
306 struct nd_btt *nd_btt = to_nd_btt(dev);
307
308 __nd_detach_ndns(dev, &nd_btt->ndns);
309 put_device(dev);
310 }
311
312 return rc;
313 }
314 EXPORT_SYMBOL(nd_btt_probe);
315