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