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