Merge tag 'usb-4.12-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[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/cdev.h>
19 #include <linux/hash.h>
20 #include <linux/slab.h>
21 #include <linux/dax.h>
22 #include <linux/fs.h>
23
24 static dev_t dax_devt;
25 DEFINE_STATIC_SRCU(dax_srcu);
26 static struct vfsmount *dax_mnt;
27 static DEFINE_IDA(dax_minor_ida);
28 static struct kmem_cache *dax_cache __read_mostly;
29 static struct super_block *dax_superblock __read_mostly;
30
31 #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
32 static struct hlist_head dax_host_list[DAX_HASH_SIZE];
33 static DEFINE_SPINLOCK(dax_host_lock);
34
35 int dax_read_lock(void)
36 {
37         return srcu_read_lock(&dax_srcu);
38 }
39 EXPORT_SYMBOL_GPL(dax_read_lock);
40
41 void dax_read_unlock(int id)
42 {
43         srcu_read_unlock(&dax_srcu, id);
44 }
45 EXPORT_SYMBOL_GPL(dax_read_unlock);
46
47 #ifdef CONFIG_BLOCK
48 int 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 }
59 EXPORT_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  */
71 int __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 }
115 EXPORT_SYMBOL_GPL(__bdev_dax_supported);
116 #endif
117
118 /**
119  * struct dax_device - anchor object for dax services
120  * @inode: core vfs
121  * @cdev: optional character interface for "device dax"
122  * @host: optional name for lookups where the device path is not available
123  * @private: dax driver private data
124  * @alive: !alive + rcu grace period == no new operations / mappings
125  */
126 struct dax_device {
127         struct hlist_node list;
128         struct inode inode;
129         struct cdev cdev;
130         const char *host;
131         void *private;
132         bool alive;
133         const struct dax_operations *ops;
134 };
135
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  */
147 long 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 }
173 EXPORT_SYMBOL_GPL(dax_direct_access);
174
175 bool dax_alive(struct dax_device *dax_dev)
176 {
177         lockdep_assert_held(&dax_srcu);
178         return dax_dev->alive;
179 }
180 EXPORT_SYMBOL_GPL(dax_alive);
181
182 static int dax_host_hash(const char *host)
183 {
184         return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
185 }
186
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  */
193 void kill_dax(struct dax_device *dax_dev)
194 {
195         if (!dax_dev)
196                 return;
197
198         dax_dev->alive = false;
199
200         synchronize_srcu(&dax_srcu);
201
202         spin_lock(&dax_host_lock);
203         hlist_del_init(&dax_dev->list);
204         spin_unlock(&dax_host_lock);
205
206         dax_dev->private = NULL;
207 }
208 EXPORT_SYMBOL_GPL(kill_dax);
209
210 static struct inode *dax_alloc_inode(struct super_block *sb)
211 {
212         struct dax_device *dax_dev;
213
214         dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
215         return &dax_dev->inode;
216 }
217
218 static struct dax_device *to_dax_dev(struct inode *inode)
219 {
220         return container_of(inode, struct dax_device, inode);
221 }
222
223 static void dax_i_callback(struct rcu_head *head)
224 {
225         struct inode *inode = container_of(head, struct inode, i_rcu);
226         struct dax_device *dax_dev = to_dax_dev(inode);
227
228         kfree(dax_dev->host);
229         dax_dev->host = NULL;
230         ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
231         kmem_cache_free(dax_cache, dax_dev);
232 }
233
234 static void dax_destroy_inode(struct inode *inode)
235 {
236         struct dax_device *dax_dev = to_dax_dev(inode);
237
238         WARN_ONCE(dax_dev->alive,
239                         "kill_dax() must be called before final iput()\n");
240         call_rcu(&inode->i_rcu, dax_i_callback);
241 }
242
243 static const struct super_operations dax_sops = {
244         .statfs = simple_statfs,
245         .alloc_inode = dax_alloc_inode,
246         .destroy_inode = dax_destroy_inode,
247         .drop_inode = generic_delete_inode,
248 };
249
250 static struct dentry *dax_mount(struct file_system_type *fs_type,
251                 int flags, const char *dev_name, void *data)
252 {
253         return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
254 }
255
256 static struct file_system_type dax_fs_type = {
257         .name = "dax",
258         .mount = dax_mount,
259         .kill_sb = kill_anon_super,
260 };
261
262 static int dax_test(struct inode *inode, void *data)
263 {
264         dev_t devt = *(dev_t *) data;
265
266         return inode->i_rdev == devt;
267 }
268
269 static int dax_set(struct inode *inode, void *data)
270 {
271         dev_t devt = *(dev_t *) data;
272
273         inode->i_rdev = devt;
274         return 0;
275 }
276
277 static struct dax_device *dax_dev_get(dev_t devt)
278 {
279         struct dax_device *dax_dev;
280         struct inode *inode;
281
282         inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
283                         dax_test, dax_set, &devt);
284
285         if (!inode)
286                 return NULL;
287
288         dax_dev = to_dax_dev(inode);
289         if (inode->i_state & I_NEW) {
290                 dax_dev->alive = true;
291                 inode->i_cdev = &dax_dev->cdev;
292                 inode->i_mode = S_IFCHR;
293                 inode->i_flags = S_DAX;
294                 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
295                 unlock_new_inode(inode);
296         }
297
298         return dax_dev;
299 }
300
301 static void dax_add_host(struct dax_device *dax_dev, const char *host)
302 {
303         int hash;
304
305         /*
306          * Unconditionally init dax_dev since it's coming from a
307          * non-zeroed slab cache
308          */
309         INIT_HLIST_NODE(&dax_dev->list);
310         dax_dev->host = host;
311         if (!host)
312                 return;
313
314         hash = dax_host_hash(host);
315         spin_lock(&dax_host_lock);
316         hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
317         spin_unlock(&dax_host_lock);
318 }
319
320 struct dax_device *alloc_dax(void *private, const char *__host,
321                 const struct dax_operations *ops)
322 {
323         struct dax_device *dax_dev;
324         const char *host;
325         dev_t devt;
326         int minor;
327
328         host = kstrdup(__host, GFP_KERNEL);
329         if (__host && !host)
330                 return NULL;
331
332         minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
333         if (minor < 0)
334                 goto err_minor;
335
336         devt = MKDEV(MAJOR(dax_devt), minor);
337         dax_dev = dax_dev_get(devt);
338         if (!dax_dev)
339                 goto err_dev;
340
341         dax_add_host(dax_dev, host);
342         dax_dev->ops = ops;
343         dax_dev->private = private;
344         return dax_dev;
345
346  err_dev:
347         ida_simple_remove(&dax_minor_ida, minor);
348  err_minor:
349         kfree(host);
350         return NULL;
351 }
352 EXPORT_SYMBOL_GPL(alloc_dax);
353
354 void put_dax(struct dax_device *dax_dev)
355 {
356         if (!dax_dev)
357                 return;
358         iput(&dax_dev->inode);
359 }
360 EXPORT_SYMBOL_GPL(put_dax);
361
362 /**
363  * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
364  * @host: alternate name for the device registered by a dax driver
365  */
366 struct dax_device *dax_get_by_host(const char *host)
367 {
368         struct dax_device *dax_dev, *found = NULL;
369         int hash, id;
370
371         if (!host)
372                 return NULL;
373
374         hash = dax_host_hash(host);
375
376         id = dax_read_lock();
377         spin_lock(&dax_host_lock);
378         hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
379                 if (!dax_alive(dax_dev)
380                                 || strcmp(host, dax_dev->host) != 0)
381                         continue;
382
383                 if (igrab(&dax_dev->inode))
384                         found = dax_dev;
385                 break;
386         }
387         spin_unlock(&dax_host_lock);
388         dax_read_unlock(id);
389
390         return found;
391 }
392 EXPORT_SYMBOL_GPL(dax_get_by_host);
393
394 /**
395  * inode_dax: convert a public inode into its dax_dev
396  * @inode: An inode with i_cdev pointing to a dax_dev
397  *
398  * Note this is not equivalent to to_dax_dev() which is for private
399  * internal use where we know the inode filesystem type == dax_fs_type.
400  */
401 struct dax_device *inode_dax(struct inode *inode)
402 {
403         struct cdev *cdev = inode->i_cdev;
404
405         return container_of(cdev, struct dax_device, cdev);
406 }
407 EXPORT_SYMBOL_GPL(inode_dax);
408
409 struct inode *dax_inode(struct dax_device *dax_dev)
410 {
411         return &dax_dev->inode;
412 }
413 EXPORT_SYMBOL_GPL(dax_inode);
414
415 void *dax_get_private(struct dax_device *dax_dev)
416 {
417         return dax_dev->private;
418 }
419 EXPORT_SYMBOL_GPL(dax_get_private);
420
421 static void init_once(void *_dax_dev)
422 {
423         struct dax_device *dax_dev = _dax_dev;
424         struct inode *inode = &dax_dev->inode;
425
426         inode_init_once(inode);
427 }
428
429 static int __dax_fs_init(void)
430 {
431         int rc;
432
433         dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
434                         (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
435                          SLAB_MEM_SPREAD|SLAB_ACCOUNT),
436                         init_once);
437         if (!dax_cache)
438                 return -ENOMEM;
439
440         rc = register_filesystem(&dax_fs_type);
441         if (rc)
442                 goto err_register_fs;
443
444         dax_mnt = kern_mount(&dax_fs_type);
445         if (IS_ERR(dax_mnt)) {
446                 rc = PTR_ERR(dax_mnt);
447                 goto err_mount;
448         }
449         dax_superblock = dax_mnt->mnt_sb;
450
451         return 0;
452
453  err_mount:
454         unregister_filesystem(&dax_fs_type);
455  err_register_fs:
456         kmem_cache_destroy(dax_cache);
457
458         return rc;
459 }
460
461 static void __dax_fs_exit(void)
462 {
463         kern_unmount(dax_mnt);
464         unregister_filesystem(&dax_fs_type);
465         kmem_cache_destroy(dax_cache);
466 }
467
468 static int __init dax_fs_init(void)
469 {
470         int rc;
471
472         rc = __dax_fs_init();
473         if (rc)
474                 return rc;
475
476         rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
477         if (rc)
478                 __dax_fs_exit();
479         return rc;
480 }
481
482 static void __exit dax_fs_exit(void)
483 {
484         unregister_chrdev_region(dax_devt, MINORMASK+1);
485         ida_destroy(&dax_minor_ida);
486         __dax_fs_exit();
487 }
488
489 MODULE_AUTHOR("Intel Corporation");
490 MODULE_LICENSE("GPL v2");
491 subsys_initcall(dax_fs_init);
492 module_exit(dax_fs_exit);