]>
Commit | Line | Data |
---|---|---|
8c2f7e86 DW |
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 | ||
24 | static void __nd_btt_detach_ndns(struct nd_btt *nd_btt) | |
25 | { | |
26 | struct nd_namespace_common *ndns = nd_btt->ndns; | |
27 | ||
28 | dev_WARN_ONCE(&nd_btt->dev, !mutex_is_locked(&ndns->dev.mutex) | |
29 | || ndns->claim != &nd_btt->dev, | |
30 | "%s: invalid claim\n", __func__); | |
31 | ndns->claim = NULL; | |
32 | nd_btt->ndns = NULL; | |
33 | put_device(&ndns->dev); | |
34 | } | |
35 | ||
36 | static void nd_btt_detach_ndns(struct nd_btt *nd_btt) | |
37 | { | |
38 | struct nd_namespace_common *ndns = nd_btt->ndns; | |
39 | ||
40 | if (!ndns) | |
41 | return; | |
42 | get_device(&ndns->dev); | |
43 | device_lock(&ndns->dev); | |
44 | __nd_btt_detach_ndns(nd_btt); | |
45 | device_unlock(&ndns->dev); | |
46 | put_device(&ndns->dev); | |
47 | } | |
48 | ||
49 | static bool __nd_btt_attach_ndns(struct nd_btt *nd_btt, | |
50 | struct nd_namespace_common *ndns) | |
51 | { | |
52 | if (ndns->claim) | |
53 | return false; | |
54 | dev_WARN_ONCE(&nd_btt->dev, !mutex_is_locked(&ndns->dev.mutex) | |
55 | || nd_btt->ndns, | |
56 | "%s: invalid claim\n", __func__); | |
57 | ndns->claim = &nd_btt->dev; | |
58 | nd_btt->ndns = ndns; | |
59 | get_device(&ndns->dev); | |
60 | return true; | |
61 | } | |
62 | ||
63 | static bool nd_btt_attach_ndns(struct nd_btt *nd_btt, | |
64 | struct nd_namespace_common *ndns) | |
65 | { | |
66 | bool claimed; | |
67 | ||
68 | device_lock(&ndns->dev); | |
69 | claimed = __nd_btt_attach_ndns(nd_btt, ndns); | |
70 | device_unlock(&ndns->dev); | |
71 | return claimed; | |
72 | } | |
73 | ||
74 | static void nd_btt_release(struct device *dev) | |
75 | { | |
76 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
77 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
78 | ||
79 | dev_dbg(dev, "%s\n", __func__); | |
80 | nd_btt_detach_ndns(nd_btt); | |
81 | ida_simple_remove(&nd_region->btt_ida, nd_btt->id); | |
82 | kfree(nd_btt->uuid); | |
83 | kfree(nd_btt); | |
84 | } | |
85 | ||
86 | static struct device_type nd_btt_device_type = { | |
87 | .name = "nd_btt", | |
88 | .release = nd_btt_release, | |
89 | }; | |
90 | ||
91 | bool is_nd_btt(struct device *dev) | |
92 | { | |
93 | return dev->type == &nd_btt_device_type; | |
94 | } | |
95 | EXPORT_SYMBOL(is_nd_btt); | |
96 | ||
97 | struct nd_btt *to_nd_btt(struct device *dev) | |
98 | { | |
99 | struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev); | |
100 | ||
101 | WARN_ON(!is_nd_btt(dev)); | |
102 | return nd_btt; | |
103 | } | |
104 | EXPORT_SYMBOL(to_nd_btt); | |
105 | ||
41cd8b70 VV |
106 | static const unsigned long btt_lbasize_supported[] = { 512, 520, 528, |
107 | 4096, 4104, 4160, 4224, 0 }; | |
8c2f7e86 DW |
108 | |
109 | static ssize_t sector_size_show(struct device *dev, | |
110 | struct device_attribute *attr, char *buf) | |
111 | { | |
112 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
113 | ||
114 | return nd_sector_size_show(nd_btt->lbasize, btt_lbasize_supported, buf); | |
115 | } | |
116 | ||
117 | static ssize_t sector_size_store(struct device *dev, | |
118 | struct device_attribute *attr, const char *buf, size_t len) | |
119 | { | |
120 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
121 | ssize_t rc; | |
122 | ||
123 | device_lock(dev); | |
124 | nvdimm_bus_lock(dev); | |
125 | rc = nd_sector_size_store(dev, buf, &nd_btt->lbasize, | |
126 | btt_lbasize_supported); | |
127 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
128 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
129 | nvdimm_bus_unlock(dev); | |
130 | device_unlock(dev); | |
131 | ||
132 | return rc ? rc : len; | |
133 | } | |
134 | static DEVICE_ATTR_RW(sector_size); | |
135 | ||
136 | static ssize_t uuid_show(struct device *dev, | |
137 | struct device_attribute *attr, char *buf) | |
138 | { | |
139 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
140 | ||
141 | if (nd_btt->uuid) | |
142 | return sprintf(buf, "%pUb\n", nd_btt->uuid); | |
143 | return sprintf(buf, "\n"); | |
144 | } | |
145 | ||
146 | static ssize_t uuid_store(struct device *dev, | |
147 | struct device_attribute *attr, const char *buf, size_t len) | |
148 | { | |
149 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
150 | ssize_t rc; | |
151 | ||
152 | device_lock(dev); | |
153 | rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len); | |
154 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
155 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
156 | device_unlock(dev); | |
157 | ||
158 | return rc ? rc : len; | |
159 | } | |
160 | static DEVICE_ATTR_RW(uuid); | |
161 | ||
162 | static ssize_t namespace_show(struct device *dev, | |
163 | struct device_attribute *attr, char *buf) | |
164 | { | |
165 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
166 | ssize_t rc; | |
167 | ||
168 | nvdimm_bus_lock(dev); | |
169 | rc = sprintf(buf, "%s\n", nd_btt->ndns | |
170 | ? dev_name(&nd_btt->ndns->dev) : ""); | |
171 | nvdimm_bus_unlock(dev); | |
172 | return rc; | |
173 | } | |
174 | ||
175 | static int namespace_match(struct device *dev, void *data) | |
176 | { | |
177 | char *name = data; | |
178 | ||
179 | return strcmp(name, dev_name(dev)) == 0; | |
180 | } | |
181 | ||
182 | static bool is_nd_btt_idle(struct device *dev) | |
183 | { | |
184 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
185 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
186 | ||
187 | if (nd_region->btt_seed == dev || nd_btt->ndns || dev->driver) | |
188 | return false; | |
189 | return true; | |
190 | } | |
191 | ||
192 | static ssize_t __namespace_store(struct device *dev, | |
193 | struct device_attribute *attr, const char *buf, size_t len) | |
194 | { | |
195 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
196 | struct nd_namespace_common *ndns; | |
197 | struct device *found; | |
198 | char *name; | |
199 | ||
200 | if (dev->driver) { | |
201 | dev_dbg(dev, "%s: -EBUSY\n", __func__); | |
202 | return -EBUSY; | |
203 | } | |
204 | ||
205 | name = kstrndup(buf, len, GFP_KERNEL); | |
206 | if (!name) | |
207 | return -ENOMEM; | |
208 | strim(name); | |
209 | ||
210 | if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0) | |
211 | /* pass */; | |
212 | else { | |
213 | len = -EINVAL; | |
214 | goto out; | |
215 | } | |
216 | ||
217 | ndns = nd_btt->ndns; | |
218 | if (strcmp(name, "") == 0) { | |
219 | /* detach the namespace and destroy / reset the btt device */ | |
220 | nd_btt_detach_ndns(nd_btt); | |
221 | if (is_nd_btt_idle(dev)) | |
222 | nd_device_unregister(dev, ND_ASYNC); | |
223 | else { | |
224 | nd_btt->lbasize = 0; | |
225 | kfree(nd_btt->uuid); | |
226 | nd_btt->uuid = NULL; | |
227 | } | |
228 | goto out; | |
229 | } else if (ndns) { | |
230 | dev_dbg(dev, "namespace already set to: %s\n", | |
231 | dev_name(&ndns->dev)); | |
232 | len = -EBUSY; | |
233 | goto out; | |
234 | } | |
235 | ||
236 | found = device_find_child(dev->parent, name, namespace_match); | |
237 | if (!found) { | |
238 | dev_dbg(dev, "'%s' not found under %s\n", name, | |
239 | dev_name(dev->parent)); | |
240 | len = -ENODEV; | |
241 | goto out; | |
242 | } | |
243 | ||
244 | ndns = to_ndns(found); | |
245 | if (__nvdimm_namespace_capacity(ndns) < SZ_16M) { | |
246 | dev_dbg(dev, "%s too small to host btt\n", name); | |
247 | len = -ENXIO; | |
248 | goto out_attach; | |
249 | } | |
250 | ||
251 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nd_btt->dev)); | |
252 | if (!nd_btt_attach_ndns(nd_btt, ndns)) { | |
253 | dev_dbg(dev, "%s already claimed\n", | |
254 | dev_name(&ndns->dev)); | |
255 | len = -EBUSY; | |
256 | } | |
257 | ||
258 | out_attach: | |
259 | put_device(&ndns->dev); /* from device_find_child */ | |
260 | out: | |
261 | kfree(name); | |
262 | return len; | |
263 | } | |
264 | ||
265 | static ssize_t namespace_store(struct device *dev, | |
266 | struct device_attribute *attr, const char *buf, size_t len) | |
267 | { | |
268 | ssize_t rc; | |
269 | ||
270 | nvdimm_bus_lock(dev); | |
271 | device_lock(dev); | |
272 | rc = __namespace_store(dev, attr, buf, len); | |
273 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
274 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
275 | device_unlock(dev); | |
276 | nvdimm_bus_unlock(dev); | |
277 | ||
278 | return rc; | |
279 | } | |
280 | static DEVICE_ATTR_RW(namespace); | |
281 | ||
282 | static struct attribute *nd_btt_attributes[] = { | |
283 | &dev_attr_sector_size.attr, | |
284 | &dev_attr_namespace.attr, | |
285 | &dev_attr_uuid.attr, | |
286 | NULL, | |
287 | }; | |
288 | ||
289 | static struct attribute_group nd_btt_attribute_group = { | |
290 | .attrs = nd_btt_attributes, | |
291 | }; | |
292 | ||
293 | static const struct attribute_group *nd_btt_attribute_groups[] = { | |
294 | &nd_btt_attribute_group, | |
295 | &nd_device_attribute_group, | |
74ae66c3 | 296 | &nd_numa_attribute_group, |
8c2f7e86 DW |
297 | NULL, |
298 | }; | |
299 | ||
300 | static struct device *__nd_btt_create(struct nd_region *nd_region, | |
301 | unsigned long lbasize, u8 *uuid, | |
302 | struct nd_namespace_common *ndns) | |
303 | { | |
304 | struct nd_btt *nd_btt; | |
305 | struct device *dev; | |
306 | ||
307 | nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL); | |
308 | if (!nd_btt) | |
309 | return NULL; | |
310 | ||
311 | nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL); | |
312 | if (nd_btt->id < 0) { | |
313 | kfree(nd_btt); | |
314 | return NULL; | |
315 | } | |
316 | ||
317 | nd_btt->lbasize = lbasize; | |
318 | if (uuid) | |
319 | uuid = kmemdup(uuid, 16, GFP_KERNEL); | |
320 | nd_btt->uuid = uuid; | |
321 | dev = &nd_btt->dev; | |
322 | dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id); | |
323 | dev->parent = &nd_region->dev; | |
324 | dev->type = &nd_btt_device_type; | |
325 | dev->groups = nd_btt_attribute_groups; | |
326 | device_initialize(&nd_btt->dev); | |
327 | if (ndns && !__nd_btt_attach_ndns(nd_btt, ndns)) { | |
328 | dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n", | |
329 | __func__, dev_name(ndns->claim)); | |
330 | put_device(dev); | |
331 | return NULL; | |
332 | } | |
333 | return dev; | |
334 | } | |
335 | ||
336 | struct device *nd_btt_create(struct nd_region *nd_region) | |
337 | { | |
338 | struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL); | |
339 | ||
340 | if (dev) | |
341 | __nd_device_register(dev); | |
342 | return dev; | |
343 | } | |
344 | ||
6ec68954 VV |
345 | static bool uuid_is_null(u8 *uuid) |
346 | { | |
347 | static const u8 null_uuid[16]; | |
348 | ||
349 | return (memcmp(uuid, null_uuid, 16) == 0); | |
350 | } | |
351 | ||
ab45e763 VV |
352 | /** |
353 | * nd_btt_arena_is_valid - check if the metadata layout is valid | |
354 | * @nd_btt: device with BTT geometry and backing device info | |
355 | * @super: pointer to the arena's info block being tested | |
356 | * | |
357 | * Check consistency of the btt info block with itself by validating | |
6ec68954 VV |
358 | * the checksum, and with the parent namespace by verifying the |
359 | * parent_uuid contained in the info block with the one supplied in. | |
ab45e763 VV |
360 | * |
361 | * Returns: | |
362 | * false for an invalid info block, true for a valid one | |
363 | */ | |
364 | bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super) | |
365 | { | |
6ec68954 | 366 | const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev); |
ab45e763 VV |
367 | u64 checksum; |
368 | ||
369 | if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0) | |
370 | return false; | |
371 | ||
6ec68954 VV |
372 | if (!uuid_is_null(super->parent_uuid)) |
373 | if (memcmp(super->parent_uuid, parent_uuid, 16) != 0) | |
374 | return false; | |
375 | ||
ab45e763 VV |
376 | checksum = le64_to_cpu(super->checksum); |
377 | super->checksum = 0; | |
378 | if (checksum != nd_btt_sb_checksum(super)) | |
379 | return false; | |
380 | super->checksum = cpu_to_le64(checksum); | |
381 | ||
382 | /* TODO: figure out action for this */ | |
383 | if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0) | |
384 | dev_info(&nd_btt->dev, "Found arena with an error flag\n"); | |
385 | ||
386 | return true; | |
387 | } | |
388 | EXPORT_SYMBOL(nd_btt_arena_is_valid); | |
389 | ||
8c2f7e86 DW |
390 | /* |
391 | * nd_btt_sb_checksum: compute checksum for btt info block | |
392 | * | |
393 | * Returns a fletcher64 checksum of everything in the given info block | |
394 | * except the last field (since that's where the checksum lives). | |
395 | */ | |
396 | u64 nd_btt_sb_checksum(struct btt_sb *btt_sb) | |
397 | { | |
5212e11f VV |
398 | u64 sum; |
399 | __le64 sum_save; | |
8c2f7e86 DW |
400 | |
401 | sum_save = btt_sb->checksum; | |
402 | btt_sb->checksum = 0; | |
403 | sum = nd_fletcher64(btt_sb, sizeof(*btt_sb), 1); | |
404 | btt_sb->checksum = sum_save; | |
405 | return sum; | |
406 | } | |
407 | EXPORT_SYMBOL(nd_btt_sb_checksum); | |
408 | ||
409 | static int __nd_btt_probe(struct nd_btt *nd_btt, | |
410 | struct nd_namespace_common *ndns, struct btt_sb *btt_sb) | |
411 | { | |
8c2f7e86 DW |
412 | if (!btt_sb || !ndns || !nd_btt) |
413 | return -ENODEV; | |
414 | ||
415 | if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb))) | |
416 | return -ENXIO; | |
417 | ||
418 | if (nvdimm_namespace_capacity(ndns) < SZ_16M) | |
419 | return -ENXIO; | |
420 | ||
ab45e763 | 421 | if (!nd_btt_arena_is_valid(nd_btt, btt_sb)) |
8c2f7e86 | 422 | return -ENODEV; |
8c2f7e86 DW |
423 | |
424 | nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize); | |
425 | nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL); | |
426 | if (!nd_btt->uuid) | |
427 | return -ENOMEM; | |
428 | ||
429 | __nd_device_register(&nd_btt->dev); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata) | |
435 | { | |
436 | int rc; | |
437 | struct device *dev; | |
438 | struct btt_sb *btt_sb; | |
439 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
440 | ||
441 | if (ndns->force_raw) | |
442 | return -ENODEV; | |
443 | ||
444 | nvdimm_bus_lock(&ndns->dev); | |
445 | dev = __nd_btt_create(nd_region, 0, NULL, ndns); | |
446 | nvdimm_bus_unlock(&ndns->dev); | |
447 | if (!dev) | |
448 | return -ENOMEM; | |
449 | dev_set_drvdata(dev, drvdata); | |
450 | btt_sb = kzalloc(sizeof(*btt_sb), GFP_KERNEL); | |
451 | rc = __nd_btt_probe(to_nd_btt(dev), ndns, btt_sb); | |
452 | kfree(btt_sb); | |
453 | dev_dbg(&ndns->dev, "%s: btt: %s\n", __func__, | |
454 | rc == 0 ? dev_name(dev) : "<none>"); | |
455 | if (rc < 0) { | |
456 | __nd_btt_detach_ndns(to_nd_btt(dev)); | |
457 | put_device(dev); | |
458 | } | |
459 | ||
460 | return rc; | |
461 | } | |
462 | EXPORT_SYMBOL(nd_btt_probe); |