]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/dax/super.c
ce9e563e6e1d47715a776405c3b86b283900a283
[karo-tx-linux.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_alive(struct dax_device *dax_dev)
282 {
283         lockdep_assert_held(&dax_srcu);
284         return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
285 }
286 EXPORT_SYMBOL_GPL(dax_alive);
287
288 static int dax_host_hash(const char *host)
289 {
290         return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
291 }
292
293 /*
294  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
295  * that any fault handlers or operations that might have seen
296  * dax_alive(), have completed.  Any operations that start after
297  * synchronize_srcu() has run will abort upon seeing !dax_alive().
298  */
299 void kill_dax(struct dax_device *dax_dev)
300 {
301         if (!dax_dev)
302                 return;
303
304         clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
305
306         synchronize_srcu(&dax_srcu);
307
308         spin_lock(&dax_host_lock);
309         hlist_del_init(&dax_dev->list);
310         spin_unlock(&dax_host_lock);
311
312         dax_dev->private = NULL;
313 }
314 EXPORT_SYMBOL_GPL(kill_dax);
315
316 static struct inode *dax_alloc_inode(struct super_block *sb)
317 {
318         struct dax_device *dax_dev;
319         struct inode *inode;
320
321         dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
322         inode = &dax_dev->inode;
323         inode->i_rdev = 0;
324         return inode;
325 }
326
327 static struct dax_device *to_dax_dev(struct inode *inode)
328 {
329         return container_of(inode, struct dax_device, inode);
330 }
331
332 static void dax_i_callback(struct rcu_head *head)
333 {
334         struct inode *inode = container_of(head, struct inode, i_rcu);
335         struct dax_device *dax_dev = to_dax_dev(inode);
336
337         kfree(dax_dev->host);
338         dax_dev->host = NULL;
339         if (inode->i_rdev)
340                 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
341         kmem_cache_free(dax_cache, dax_dev);
342 }
343
344 static void dax_destroy_inode(struct inode *inode)
345 {
346         struct dax_device *dax_dev = to_dax_dev(inode);
347
348         WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
349                         "kill_dax() must be called before final iput()\n");
350         call_rcu(&inode->i_rcu, dax_i_callback);
351 }
352
353 static const struct super_operations dax_sops = {
354         .statfs = simple_statfs,
355         .alloc_inode = dax_alloc_inode,
356         .destroy_inode = dax_destroy_inode,
357         .drop_inode = generic_delete_inode,
358 };
359
360 static struct dentry *dax_mount(struct file_system_type *fs_type,
361                 int flags, const char *dev_name, void *data)
362 {
363         return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
364 }
365
366 static struct file_system_type dax_fs_type = {
367         .name = "dax",
368         .mount = dax_mount,
369         .kill_sb = kill_anon_super,
370 };
371
372 static int dax_test(struct inode *inode, void *data)
373 {
374         dev_t devt = *(dev_t *) data;
375
376         return inode->i_rdev == devt;
377 }
378
379 static int dax_set(struct inode *inode, void *data)
380 {
381         dev_t devt = *(dev_t *) data;
382
383         inode->i_rdev = devt;
384         return 0;
385 }
386
387 static struct dax_device *dax_dev_get(dev_t devt)
388 {
389         struct dax_device *dax_dev;
390         struct inode *inode;
391
392         inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
393                         dax_test, dax_set, &devt);
394
395         if (!inode)
396                 return NULL;
397
398         dax_dev = to_dax_dev(inode);
399         if (inode->i_state & I_NEW) {
400                 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
401                 inode->i_cdev = &dax_dev->cdev;
402                 inode->i_mode = S_IFCHR;
403                 inode->i_flags = S_DAX;
404                 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
405                 unlock_new_inode(inode);
406         }
407
408         return dax_dev;
409 }
410
411 static void dax_add_host(struct dax_device *dax_dev, const char *host)
412 {
413         int hash;
414
415         /*
416          * Unconditionally init dax_dev since it's coming from a
417          * non-zeroed slab cache
418          */
419         INIT_HLIST_NODE(&dax_dev->list);
420         dax_dev->host = host;
421         if (!host)
422                 return;
423
424         hash = dax_host_hash(host);
425         spin_lock(&dax_host_lock);
426         hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
427         spin_unlock(&dax_host_lock);
428 }
429
430 struct dax_device *alloc_dax(void *private, const char *__host,
431                 const struct dax_operations *ops)
432 {
433         struct dax_device *dax_dev;
434         const char *host;
435         dev_t devt;
436         int minor;
437
438         host = kstrdup(__host, GFP_KERNEL);
439         if (__host && !host)
440                 return NULL;
441
442         minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
443         if (minor < 0)
444                 goto err_minor;
445
446         devt = MKDEV(MAJOR(dax_devt), minor);
447         dax_dev = dax_dev_get(devt);
448         if (!dax_dev)
449                 goto err_dev;
450
451         dax_add_host(dax_dev, host);
452         dax_dev->ops = ops;
453         dax_dev->private = private;
454         return dax_dev;
455
456  err_dev:
457         ida_simple_remove(&dax_minor_ida, minor);
458  err_minor:
459         kfree(host);
460         return NULL;
461 }
462 EXPORT_SYMBOL_GPL(alloc_dax);
463
464 void put_dax(struct dax_device *dax_dev)
465 {
466         if (!dax_dev)
467                 return;
468         iput(&dax_dev->inode);
469 }
470 EXPORT_SYMBOL_GPL(put_dax);
471
472 /**
473  * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
474  * @host: alternate name for the device registered by a dax driver
475  */
476 struct dax_device *dax_get_by_host(const char *host)
477 {
478         struct dax_device *dax_dev, *found = NULL;
479         int hash, id;
480
481         if (!host)
482                 return NULL;
483
484         hash = dax_host_hash(host);
485
486         id = dax_read_lock();
487         spin_lock(&dax_host_lock);
488         hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
489                 if (!dax_alive(dax_dev)
490                                 || strcmp(host, dax_dev->host) != 0)
491                         continue;
492
493                 if (igrab(&dax_dev->inode))
494                         found = dax_dev;
495                 break;
496         }
497         spin_unlock(&dax_host_lock);
498         dax_read_unlock(id);
499
500         return found;
501 }
502 EXPORT_SYMBOL_GPL(dax_get_by_host);
503
504 /**
505  * inode_dax: convert a public inode into its dax_dev
506  * @inode: An inode with i_cdev pointing to a dax_dev
507  *
508  * Note this is not equivalent to to_dax_dev() which is for private
509  * internal use where we know the inode filesystem type == dax_fs_type.
510  */
511 struct dax_device *inode_dax(struct inode *inode)
512 {
513         struct cdev *cdev = inode->i_cdev;
514
515         return container_of(cdev, struct dax_device, cdev);
516 }
517 EXPORT_SYMBOL_GPL(inode_dax);
518
519 struct inode *dax_inode(struct dax_device *dax_dev)
520 {
521         return &dax_dev->inode;
522 }
523 EXPORT_SYMBOL_GPL(dax_inode);
524
525 void *dax_get_private(struct dax_device *dax_dev)
526 {
527         return dax_dev->private;
528 }
529 EXPORT_SYMBOL_GPL(dax_get_private);
530
531 static void init_once(void *_dax_dev)
532 {
533         struct dax_device *dax_dev = _dax_dev;
534         struct inode *inode = &dax_dev->inode;
535
536         memset(dax_dev, 0, sizeof(*dax_dev));
537         inode_init_once(inode);
538 }
539
540 static int __dax_fs_init(void)
541 {
542         int rc;
543
544         dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
545                         (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
546                          SLAB_MEM_SPREAD|SLAB_ACCOUNT),
547                         init_once);
548         if (!dax_cache)
549                 return -ENOMEM;
550
551         rc = register_filesystem(&dax_fs_type);
552         if (rc)
553                 goto err_register_fs;
554
555         dax_mnt = kern_mount(&dax_fs_type);
556         if (IS_ERR(dax_mnt)) {
557                 rc = PTR_ERR(dax_mnt);
558                 goto err_mount;
559         }
560         dax_superblock = dax_mnt->mnt_sb;
561
562         return 0;
563
564  err_mount:
565         unregister_filesystem(&dax_fs_type);
566  err_register_fs:
567         kmem_cache_destroy(dax_cache);
568
569         return rc;
570 }
571
572 static void __dax_fs_exit(void)
573 {
574         kern_unmount(dax_mnt);
575         unregister_filesystem(&dax_fs_type);
576         kmem_cache_destroy(dax_cache);
577 }
578
579 static int __init dax_fs_init(void)
580 {
581         int rc;
582
583         rc = __dax_fs_init();
584         if (rc)
585                 return rc;
586
587         rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
588         if (rc)
589                 __dax_fs_exit();
590         return rc;
591 }
592
593 static void __exit dax_fs_exit(void)
594 {
595         unregister_chrdev_region(dax_devt, MINORMASK+1);
596         ida_destroy(&dax_minor_ida);
597         __dax_fs_exit();
598 }
599
600 MODULE_AUTHOR("Intel Corporation");
601 MODULE_LICENSE("GPL v2");
602 subsys_initcall(dax_fs_init);
603 module_exit(dax_fs_exit);