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