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