]>
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 | ||
106 | static const unsigned long btt_lbasize_supported[] = { 512, 4096, 0 }; | |
107 | ||
108 | static ssize_t sector_size_show(struct device *dev, | |
109 | struct device_attribute *attr, char *buf) | |
110 | { | |
111 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
112 | ||
113 | return nd_sector_size_show(nd_btt->lbasize, btt_lbasize_supported, buf); | |
114 | } | |
115 | ||
116 | static ssize_t sector_size_store(struct device *dev, | |
117 | struct device_attribute *attr, const char *buf, size_t len) | |
118 | { | |
119 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
120 | ssize_t rc; | |
121 | ||
122 | device_lock(dev); | |
123 | nvdimm_bus_lock(dev); | |
124 | rc = nd_sector_size_store(dev, buf, &nd_btt->lbasize, | |
125 | btt_lbasize_supported); | |
126 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
127 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
128 | nvdimm_bus_unlock(dev); | |
129 | device_unlock(dev); | |
130 | ||
131 | return rc ? rc : len; | |
132 | } | |
133 | static DEVICE_ATTR_RW(sector_size); | |
134 | ||
135 | static ssize_t uuid_show(struct device *dev, | |
136 | struct device_attribute *attr, char *buf) | |
137 | { | |
138 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
139 | ||
140 | if (nd_btt->uuid) | |
141 | return sprintf(buf, "%pUb\n", nd_btt->uuid); | |
142 | return sprintf(buf, "\n"); | |
143 | } | |
144 | ||
145 | static ssize_t uuid_store(struct device *dev, | |
146 | struct device_attribute *attr, const char *buf, size_t len) | |
147 | { | |
148 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
149 | ssize_t rc; | |
150 | ||
151 | device_lock(dev); | |
152 | rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len); | |
153 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
154 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
155 | device_unlock(dev); | |
156 | ||
157 | return rc ? rc : len; | |
158 | } | |
159 | static DEVICE_ATTR_RW(uuid); | |
160 | ||
161 | static ssize_t namespace_show(struct device *dev, | |
162 | struct device_attribute *attr, char *buf) | |
163 | { | |
164 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
165 | ssize_t rc; | |
166 | ||
167 | nvdimm_bus_lock(dev); | |
168 | rc = sprintf(buf, "%s\n", nd_btt->ndns | |
169 | ? dev_name(&nd_btt->ndns->dev) : ""); | |
170 | nvdimm_bus_unlock(dev); | |
171 | return rc; | |
172 | } | |
173 | ||
174 | static int namespace_match(struct device *dev, void *data) | |
175 | { | |
176 | char *name = data; | |
177 | ||
178 | return strcmp(name, dev_name(dev)) == 0; | |
179 | } | |
180 | ||
181 | static bool is_nd_btt_idle(struct device *dev) | |
182 | { | |
183 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
184 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
185 | ||
186 | if (nd_region->btt_seed == dev || nd_btt->ndns || dev->driver) | |
187 | return false; | |
188 | return true; | |
189 | } | |
190 | ||
191 | static ssize_t __namespace_store(struct device *dev, | |
192 | struct device_attribute *attr, const char *buf, size_t len) | |
193 | { | |
194 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
195 | struct nd_namespace_common *ndns; | |
196 | struct device *found; | |
197 | char *name; | |
198 | ||
199 | if (dev->driver) { | |
200 | dev_dbg(dev, "%s: -EBUSY\n", __func__); | |
201 | return -EBUSY; | |
202 | } | |
203 | ||
204 | name = kstrndup(buf, len, GFP_KERNEL); | |
205 | if (!name) | |
206 | return -ENOMEM; | |
207 | strim(name); | |
208 | ||
209 | if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0) | |
210 | /* pass */; | |
211 | else { | |
212 | len = -EINVAL; | |
213 | goto out; | |
214 | } | |
215 | ||
216 | ndns = nd_btt->ndns; | |
217 | if (strcmp(name, "") == 0) { | |
218 | /* detach the namespace and destroy / reset the btt device */ | |
219 | nd_btt_detach_ndns(nd_btt); | |
220 | if (is_nd_btt_idle(dev)) | |
221 | nd_device_unregister(dev, ND_ASYNC); | |
222 | else { | |
223 | nd_btt->lbasize = 0; | |
224 | kfree(nd_btt->uuid); | |
225 | nd_btt->uuid = NULL; | |
226 | } | |
227 | goto out; | |
228 | } else if (ndns) { | |
229 | dev_dbg(dev, "namespace already set to: %s\n", | |
230 | dev_name(&ndns->dev)); | |
231 | len = -EBUSY; | |
232 | goto out; | |
233 | } | |
234 | ||
235 | found = device_find_child(dev->parent, name, namespace_match); | |
236 | if (!found) { | |
237 | dev_dbg(dev, "'%s' not found under %s\n", name, | |
238 | dev_name(dev->parent)); | |
239 | len = -ENODEV; | |
240 | goto out; | |
241 | } | |
242 | ||
243 | ndns = to_ndns(found); | |
244 | if (__nvdimm_namespace_capacity(ndns) < SZ_16M) { | |
245 | dev_dbg(dev, "%s too small to host btt\n", name); | |
246 | len = -ENXIO; | |
247 | goto out_attach; | |
248 | } | |
249 | ||
250 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nd_btt->dev)); | |
251 | if (!nd_btt_attach_ndns(nd_btt, ndns)) { | |
252 | dev_dbg(dev, "%s already claimed\n", | |
253 | dev_name(&ndns->dev)); | |
254 | len = -EBUSY; | |
255 | } | |
256 | ||
257 | out_attach: | |
258 | put_device(&ndns->dev); /* from device_find_child */ | |
259 | out: | |
260 | kfree(name); | |
261 | return len; | |
262 | } | |
263 | ||
264 | static ssize_t namespace_store(struct device *dev, | |
265 | struct device_attribute *attr, const char *buf, size_t len) | |
266 | { | |
267 | ssize_t rc; | |
268 | ||
269 | nvdimm_bus_lock(dev); | |
270 | device_lock(dev); | |
271 | rc = __namespace_store(dev, attr, buf, len); | |
272 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
273 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
274 | device_unlock(dev); | |
275 | nvdimm_bus_unlock(dev); | |
276 | ||
277 | return rc; | |
278 | } | |
279 | static DEVICE_ATTR_RW(namespace); | |
280 | ||
281 | static struct attribute *nd_btt_attributes[] = { | |
282 | &dev_attr_sector_size.attr, | |
283 | &dev_attr_namespace.attr, | |
284 | &dev_attr_uuid.attr, | |
285 | NULL, | |
286 | }; | |
287 | ||
288 | static struct attribute_group nd_btt_attribute_group = { | |
289 | .attrs = nd_btt_attributes, | |
290 | }; | |
291 | ||
292 | static const struct attribute_group *nd_btt_attribute_groups[] = { | |
293 | &nd_btt_attribute_group, | |
294 | &nd_device_attribute_group, | |
295 | NULL, | |
296 | }; | |
297 | ||
298 | static struct device *__nd_btt_create(struct nd_region *nd_region, | |
299 | unsigned long lbasize, u8 *uuid, | |
300 | struct nd_namespace_common *ndns) | |
301 | { | |
302 | struct nd_btt *nd_btt; | |
303 | struct device *dev; | |
304 | ||
305 | nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL); | |
306 | if (!nd_btt) | |
307 | return NULL; | |
308 | ||
309 | nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL); | |
310 | if (nd_btt->id < 0) { | |
311 | kfree(nd_btt); | |
312 | return NULL; | |
313 | } | |
314 | ||
315 | nd_btt->lbasize = lbasize; | |
316 | if (uuid) | |
317 | uuid = kmemdup(uuid, 16, GFP_KERNEL); | |
318 | nd_btt->uuid = uuid; | |
319 | dev = &nd_btt->dev; | |
320 | dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id); | |
321 | dev->parent = &nd_region->dev; | |
322 | dev->type = &nd_btt_device_type; | |
323 | dev->groups = nd_btt_attribute_groups; | |
324 | device_initialize(&nd_btt->dev); | |
325 | if (ndns && !__nd_btt_attach_ndns(nd_btt, ndns)) { | |
326 | dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n", | |
327 | __func__, dev_name(ndns->claim)); | |
328 | put_device(dev); | |
329 | return NULL; | |
330 | } | |
331 | return dev; | |
332 | } | |
333 | ||
334 | struct device *nd_btt_create(struct nd_region *nd_region) | |
335 | { | |
336 | struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL); | |
337 | ||
338 | if (dev) | |
339 | __nd_device_register(dev); | |
340 | return dev; | |
341 | } | |
342 | ||
343 | /* | |
344 | * nd_btt_sb_checksum: compute checksum for btt info block | |
345 | * | |
346 | * Returns a fletcher64 checksum of everything in the given info block | |
347 | * except the last field (since that's where the checksum lives). | |
348 | */ | |
349 | u64 nd_btt_sb_checksum(struct btt_sb *btt_sb) | |
350 | { | |
5212e11f VV |
351 | u64 sum; |
352 | __le64 sum_save; | |
8c2f7e86 DW |
353 | |
354 | sum_save = btt_sb->checksum; | |
355 | btt_sb->checksum = 0; | |
356 | sum = nd_fletcher64(btt_sb, sizeof(*btt_sb), 1); | |
357 | btt_sb->checksum = sum_save; | |
358 | return sum; | |
359 | } | |
360 | EXPORT_SYMBOL(nd_btt_sb_checksum); | |
361 | ||
362 | static int __nd_btt_probe(struct nd_btt *nd_btt, | |
363 | struct nd_namespace_common *ndns, struct btt_sb *btt_sb) | |
364 | { | |
365 | u64 checksum; | |
366 | ||
367 | if (!btt_sb || !ndns || !nd_btt) | |
368 | return -ENODEV; | |
369 | ||
370 | if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb))) | |
371 | return -ENXIO; | |
372 | ||
373 | if (nvdimm_namespace_capacity(ndns) < SZ_16M) | |
374 | return -ENXIO; | |
375 | ||
376 | if (memcmp(btt_sb->signature, BTT_SIG, BTT_SIG_LEN) != 0) | |
377 | return -ENODEV; | |
378 | ||
379 | checksum = le64_to_cpu(btt_sb->checksum); | |
380 | btt_sb->checksum = 0; | |
381 | if (checksum != nd_btt_sb_checksum(btt_sb)) | |
382 | return -ENODEV; | |
383 | btt_sb->checksum = cpu_to_le64(checksum); | |
384 | ||
385 | nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize); | |
386 | nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL); | |
387 | if (!nd_btt->uuid) | |
388 | return -ENOMEM; | |
389 | ||
390 | __nd_device_register(&nd_btt->dev); | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata) | |
396 | { | |
397 | int rc; | |
398 | struct device *dev; | |
399 | struct btt_sb *btt_sb; | |
400 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
401 | ||
402 | if (ndns->force_raw) | |
403 | return -ENODEV; | |
404 | ||
405 | nvdimm_bus_lock(&ndns->dev); | |
406 | dev = __nd_btt_create(nd_region, 0, NULL, ndns); | |
407 | nvdimm_bus_unlock(&ndns->dev); | |
408 | if (!dev) | |
409 | return -ENOMEM; | |
410 | dev_set_drvdata(dev, drvdata); | |
411 | btt_sb = kzalloc(sizeof(*btt_sb), GFP_KERNEL); | |
412 | rc = __nd_btt_probe(to_nd_btt(dev), ndns, btt_sb); | |
413 | kfree(btt_sb); | |
414 | dev_dbg(&ndns->dev, "%s: btt: %s\n", __func__, | |
415 | rc == 0 ? dev_name(dev) : "<none>"); | |
416 | if (rc < 0) { | |
417 | __nd_btt_detach_ndns(to_nd_btt(dev)); | |
418 | put_device(dev); | |
419 | } | |
420 | ||
421 | return rc; | |
422 | } | |
423 | EXPORT_SYMBOL(nd_btt_probe); |