]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/dax/super.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[mirror_ubuntu-artful-kernel.git] / drivers / dax / super.c
1 /*
2 * Copyright(c) 2017 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/pagemap.h>
14 #include <linux/module.h>
15 #include <linux/mount.h>
16 #include <linux/magic.h>
17 #include <linux/genhd.h>
18 #include <linux/cdev.h>
19 #include <linux/hash.h>
20 #include <linux/slab.h>
21 #include <linux/uio.h>
22 #include <linux/dax.h>
23 #include <linux/fs.h>
24
25 static dev_t dax_devt;
26 DEFINE_STATIC_SRCU(dax_srcu);
27 static struct vfsmount *dax_mnt;
28 static DEFINE_IDA(dax_minor_ida);
29 static struct kmem_cache *dax_cache __read_mostly;
30 static struct super_block *dax_superblock __read_mostly;
31
32 #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
33 static struct hlist_head dax_host_list[DAX_HASH_SIZE];
34 static DEFINE_SPINLOCK(dax_host_lock);
35
36 int dax_read_lock(void)
37 {
38 return srcu_read_lock(&dax_srcu);
39 }
40 EXPORT_SYMBOL_GPL(dax_read_lock);
41
42 void dax_read_unlock(int id)
43 {
44 srcu_read_unlock(&dax_srcu, id);
45 }
46 EXPORT_SYMBOL_GPL(dax_read_unlock);
47
48 #ifdef CONFIG_BLOCK
49 int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
50 pgoff_t *pgoff)
51 {
52 phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
53
54 if (pgoff)
55 *pgoff = PHYS_PFN(phys_off);
56 if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
57 return -EINVAL;
58 return 0;
59 }
60 EXPORT_SYMBOL(bdev_dax_pgoff);
61
62 /**
63 * __bdev_dax_supported() - Check if the device supports dax for filesystem
64 * @sb: The superblock of the device
65 * @blocksize: The block size of the device
66 *
67 * This is a library function for filesystems to check if the block device
68 * can be mounted with dax option.
69 *
70 * Return: negative errno if unsupported, 0 if supported.
71 */
72 int __bdev_dax_supported(struct super_block *sb, int blocksize)
73 {
74 struct block_device *bdev = sb->s_bdev;
75 struct dax_device *dax_dev;
76 pgoff_t pgoff;
77 int err, id;
78 void *kaddr;
79 pfn_t pfn;
80 long len;
81
82 if (blocksize != PAGE_SIZE) {
83 pr_err("VFS (%s): error: unsupported blocksize for dax\n",
84 sb->s_id);
85 return -EINVAL;
86 }
87
88 err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
89 if (err) {
90 pr_err("VFS (%s): error: unaligned partition for dax\n",
91 sb->s_id);
92 return err;
93 }
94
95 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
96 if (!dax_dev) {
97 pr_err("VFS (%s): error: device does not support dax\n",
98 sb->s_id);
99 return -EOPNOTSUPP;
100 }
101
102 id = dax_read_lock();
103 len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
104 dax_read_unlock(id);
105
106 put_dax(dax_dev);
107
108 if (len < 1) {
109 pr_err("VFS (%s): error: dax access failed (%ld)",
110 sb->s_id, len);
111 return len < 0 ? len : -EIO;
112 }
113
114 return 0;
115 }
116 EXPORT_SYMBOL_GPL(__bdev_dax_supported);
117 #endif
118
119 enum dax_device_flags {
120 /* !alive + rcu grace period == no new operations / mappings */
121 DAXDEV_ALIVE,
122 /* gate whether dax_flush() calls the low level flush routine */
123 DAXDEV_WRITE_CACHE,
124 };
125
126 /**
127 * struct dax_device - anchor object for dax services
128 * @inode: core vfs
129 * @cdev: optional character interface for "device dax"
130 * @host: optional name for lookups where the device path is not available
131 * @private: dax driver private data
132 * @flags: state and boolean properties
133 */
134 struct dax_device {
135 struct hlist_node list;
136 struct inode inode;
137 struct cdev cdev;
138 const char *host;
139 void *private;
140 unsigned long flags;
141 const struct dax_operations *ops;
142 };
143
144 static ssize_t write_cache_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146 {
147 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
148 ssize_t rc;
149
150 WARN_ON_ONCE(!dax_dev);
151 if (!dax_dev)
152 return -ENXIO;
153
154 rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE,
155 &dax_dev->flags));
156 put_dax(dax_dev);
157 return rc;
158 }
159
160 static ssize_t write_cache_store(struct device *dev,
161 struct device_attribute *attr, const char *buf, size_t len)
162 {
163 bool write_cache;
164 int rc = strtobool(buf, &write_cache);
165 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
166
167 WARN_ON_ONCE(!dax_dev);
168 if (!dax_dev)
169 return -ENXIO;
170
171 if (rc)
172 len = rc;
173 else if (write_cache)
174 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
175 else
176 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
177
178 put_dax(dax_dev);
179 return len;
180 }
181 static DEVICE_ATTR_RW(write_cache);
182
183 static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
184 {
185 struct device *dev = container_of(kobj, typeof(*dev), kobj);
186 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
187
188 WARN_ON_ONCE(!dax_dev);
189 if (!dax_dev)
190 return 0;
191
192 if (a == &dev_attr_write_cache.attr && !dax_dev->ops->flush)
193 return 0;
194 return a->mode;
195 }
196
197 static struct attribute *dax_attributes[] = {
198 &dev_attr_write_cache.attr,
199 NULL,
200 };
201
202 struct attribute_group dax_attribute_group = {
203 .name = "dax",
204 .attrs = dax_attributes,
205 .is_visible = dax_visible,
206 };
207 EXPORT_SYMBOL_GPL(dax_attribute_group);
208
209 /**
210 * dax_direct_access() - translate a device pgoff to an absolute pfn
211 * @dax_dev: a dax_device instance representing the logical memory range
212 * @pgoff: offset in pages from the start of the device to translate
213 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
214 * @kaddr: output parameter that returns a virtual address mapping of pfn
215 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
216 *
217 * Return: negative errno if an error occurs, otherwise the number of
218 * pages accessible at the device relative @pgoff.
219 */
220 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
221 void **kaddr, pfn_t *pfn)
222 {
223 long avail;
224
225 /*
226 * The device driver is allowed to sleep, in order to make the
227 * memory directly accessible.
228 */
229 might_sleep();
230
231 if (!dax_dev)
232 return -EOPNOTSUPP;
233
234 if (!dax_alive(dax_dev))
235 return -ENXIO;
236
237 if (nr_pages < 0)
238 return nr_pages;
239
240 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
241 kaddr, pfn);
242 if (!avail)
243 return -ERANGE;
244 return min(avail, nr_pages);
245 }
246 EXPORT_SYMBOL_GPL(dax_direct_access);
247
248 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
249 size_t bytes, struct iov_iter *i)
250 {
251 if (!dax_alive(dax_dev))
252 return 0;
253
254 return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
255 }
256 EXPORT_SYMBOL_GPL(dax_copy_from_iter);
257
258 void dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
259 size_t size)
260 {
261 if (!dax_alive(dax_dev))
262 return;
263
264 if (!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags))
265 return;
266
267 if (dax_dev->ops->flush)
268 dax_dev->ops->flush(dax_dev, pgoff, addr, size);
269 }
270 EXPORT_SYMBOL_GPL(dax_flush);
271
272 void dax_write_cache(struct dax_device *dax_dev, bool wc)
273 {
274 if (wc)
275 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
276 else
277 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
278 }
279 EXPORT_SYMBOL_GPL(dax_write_cache);
280
281 bool dax_write_cache_enabled(struct dax_device *dax_dev)
282 {
283 return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
284 }
285 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
286
287 bool dax_alive(struct dax_device *dax_dev)
288 {
289 lockdep_assert_held(&dax_srcu);
290 return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
291 }
292 EXPORT_SYMBOL_GPL(dax_alive);
293
294 static int dax_host_hash(const char *host)
295 {
296 return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
297 }
298
299 /*
300 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
301 * that any fault handlers or operations that might have seen
302 * dax_alive(), have completed. Any operations that start after
303 * synchronize_srcu() has run will abort upon seeing !dax_alive().
304 */
305 void kill_dax(struct dax_device *dax_dev)
306 {
307 if (!dax_dev)
308 return;
309
310 clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
311
312 synchronize_srcu(&dax_srcu);
313
314 spin_lock(&dax_host_lock);
315 hlist_del_init(&dax_dev->list);
316 spin_unlock(&dax_host_lock);
317
318 dax_dev->private = NULL;
319 }
320 EXPORT_SYMBOL_GPL(kill_dax);
321
322 static struct inode *dax_alloc_inode(struct super_block *sb)
323 {
324 struct dax_device *dax_dev;
325 struct inode *inode;
326
327 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
328 inode = &dax_dev->inode;
329 inode->i_rdev = 0;
330 return inode;
331 }
332
333 static struct dax_device *to_dax_dev(struct inode *inode)
334 {
335 return container_of(inode, struct dax_device, inode);
336 }
337
338 static void dax_i_callback(struct rcu_head *head)
339 {
340 struct inode *inode = container_of(head, struct inode, i_rcu);
341 struct dax_device *dax_dev = to_dax_dev(inode);
342
343 kfree(dax_dev->host);
344 dax_dev->host = NULL;
345 if (inode->i_rdev)
346 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
347 kmem_cache_free(dax_cache, dax_dev);
348 }
349
350 static void dax_destroy_inode(struct inode *inode)
351 {
352 struct dax_device *dax_dev = to_dax_dev(inode);
353
354 WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
355 "kill_dax() must be called before final iput()\n");
356 call_rcu(&inode->i_rcu, dax_i_callback);
357 }
358
359 static const struct super_operations dax_sops = {
360 .statfs = simple_statfs,
361 .alloc_inode = dax_alloc_inode,
362 .destroy_inode = dax_destroy_inode,
363 .drop_inode = generic_delete_inode,
364 };
365
366 static struct dentry *dax_mount(struct file_system_type *fs_type,
367 int flags, const char *dev_name, void *data)
368 {
369 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
370 }
371
372 static struct file_system_type dax_fs_type = {
373 .name = "dax",
374 .mount = dax_mount,
375 .kill_sb = kill_anon_super,
376 };
377
378 static int dax_test(struct inode *inode, void *data)
379 {
380 dev_t devt = *(dev_t *) data;
381
382 return inode->i_rdev == devt;
383 }
384
385 static int dax_set(struct inode *inode, void *data)
386 {
387 dev_t devt = *(dev_t *) data;
388
389 inode->i_rdev = devt;
390 return 0;
391 }
392
393 static struct dax_device *dax_dev_get(dev_t devt)
394 {
395 struct dax_device *dax_dev;
396 struct inode *inode;
397
398 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
399 dax_test, dax_set, &devt);
400
401 if (!inode)
402 return NULL;
403
404 dax_dev = to_dax_dev(inode);
405 if (inode->i_state & I_NEW) {
406 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
407 inode->i_cdev = &dax_dev->cdev;
408 inode->i_mode = S_IFCHR;
409 inode->i_flags = S_DAX;
410 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
411 unlock_new_inode(inode);
412 }
413
414 return dax_dev;
415 }
416
417 static void dax_add_host(struct dax_device *dax_dev, const char *host)
418 {
419 int hash;
420
421 /*
422 * Unconditionally init dax_dev since it's coming from a
423 * non-zeroed slab cache
424 */
425 INIT_HLIST_NODE(&dax_dev->list);
426 dax_dev->host = host;
427 if (!host)
428 return;
429
430 hash = dax_host_hash(host);
431 spin_lock(&dax_host_lock);
432 hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
433 spin_unlock(&dax_host_lock);
434 }
435
436 struct dax_device *alloc_dax(void *private, const char *__host,
437 const struct dax_operations *ops)
438 {
439 struct dax_device *dax_dev;
440 const char *host;
441 dev_t devt;
442 int minor;
443
444 host = kstrdup(__host, GFP_KERNEL);
445 if (__host && !host)
446 return NULL;
447
448 minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
449 if (minor < 0)
450 goto err_minor;
451
452 devt = MKDEV(MAJOR(dax_devt), minor);
453 dax_dev = dax_dev_get(devt);
454 if (!dax_dev)
455 goto err_dev;
456
457 dax_add_host(dax_dev, host);
458 dax_dev->ops = ops;
459 dax_dev->private = private;
460 return dax_dev;
461
462 err_dev:
463 ida_simple_remove(&dax_minor_ida, minor);
464 err_minor:
465 kfree(host);
466 return NULL;
467 }
468 EXPORT_SYMBOL_GPL(alloc_dax);
469
470 void put_dax(struct dax_device *dax_dev)
471 {
472 if (!dax_dev)
473 return;
474 iput(&dax_dev->inode);
475 }
476 EXPORT_SYMBOL_GPL(put_dax);
477
478 /**
479 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
480 * @host: alternate name for the device registered by a dax driver
481 */
482 struct dax_device *dax_get_by_host(const char *host)
483 {
484 struct dax_device *dax_dev, *found = NULL;
485 int hash, id;
486
487 if (!host)
488 return NULL;
489
490 hash = dax_host_hash(host);
491
492 id = dax_read_lock();
493 spin_lock(&dax_host_lock);
494 hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
495 if (!dax_alive(dax_dev)
496 || strcmp(host, dax_dev->host) != 0)
497 continue;
498
499 if (igrab(&dax_dev->inode))
500 found = dax_dev;
501 break;
502 }
503 spin_unlock(&dax_host_lock);
504 dax_read_unlock(id);
505
506 return found;
507 }
508 EXPORT_SYMBOL_GPL(dax_get_by_host);
509
510 /**
511 * inode_dax: convert a public inode into its dax_dev
512 * @inode: An inode with i_cdev pointing to a dax_dev
513 *
514 * Note this is not equivalent to to_dax_dev() which is for private
515 * internal use where we know the inode filesystem type == dax_fs_type.
516 */
517 struct dax_device *inode_dax(struct inode *inode)
518 {
519 struct cdev *cdev = inode->i_cdev;
520
521 return container_of(cdev, struct dax_device, cdev);
522 }
523 EXPORT_SYMBOL_GPL(inode_dax);
524
525 struct inode *dax_inode(struct dax_device *dax_dev)
526 {
527 return &dax_dev->inode;
528 }
529 EXPORT_SYMBOL_GPL(dax_inode);
530
531 void *dax_get_private(struct dax_device *dax_dev)
532 {
533 return dax_dev->private;
534 }
535 EXPORT_SYMBOL_GPL(dax_get_private);
536
537 static void init_once(void *_dax_dev)
538 {
539 struct dax_device *dax_dev = _dax_dev;
540 struct inode *inode = &dax_dev->inode;
541
542 memset(dax_dev, 0, sizeof(*dax_dev));
543 inode_init_once(inode);
544 }
545
546 static int __dax_fs_init(void)
547 {
548 int rc;
549
550 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
551 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
552 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
553 init_once);
554 if (!dax_cache)
555 return -ENOMEM;
556
557 rc = register_filesystem(&dax_fs_type);
558 if (rc)
559 goto err_register_fs;
560
561 dax_mnt = kern_mount(&dax_fs_type);
562 if (IS_ERR(dax_mnt)) {
563 rc = PTR_ERR(dax_mnt);
564 goto err_mount;
565 }
566 dax_superblock = dax_mnt->mnt_sb;
567
568 return 0;
569
570 err_mount:
571 unregister_filesystem(&dax_fs_type);
572 err_register_fs:
573 kmem_cache_destroy(dax_cache);
574
575 return rc;
576 }
577
578 static void __dax_fs_exit(void)
579 {
580 kern_unmount(dax_mnt);
581 unregister_filesystem(&dax_fs_type);
582 kmem_cache_destroy(dax_cache);
583 }
584
585 static int __init dax_fs_init(void)
586 {
587 int rc;
588
589 rc = __dax_fs_init();
590 if (rc)
591 return rc;
592
593 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
594 if (rc)
595 __dax_fs_exit();
596 return rc;
597 }
598
599 static void __exit dax_fs_exit(void)
600 {
601 unregister_chrdev_region(dax_devt, MINORMASK+1);
602 ida_destroy(&dax_minor_ida);
603 __dax_fs_exit();
604 }
605
606 MODULE_AUTHOR("Intel Corporation");
607 MODULE_LICENSE("GPL v2");
608 subsys_initcall(dax_fs_init);
609 module_exit(dax_fs_exit);