2 * Copyright(c) 2017 Intel Corporation. All rights reserved.
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.
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.
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>
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
;
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
);
36 int dax_read_lock(void)
38 return srcu_read_lock(&dax_srcu
);
40 EXPORT_SYMBOL_GPL(dax_read_lock
);
42 void dax_read_unlock(int id
)
44 srcu_read_unlock(&dax_srcu
, id
);
46 EXPORT_SYMBOL_GPL(dax_read_unlock
);
49 #include <linux/blkdev.h>
51 int bdev_dax_pgoff(struct block_device
*bdev
, sector_t sector
, size_t size
,
54 phys_addr_t phys_off
= (get_start_sect(bdev
) + sector
) * 512;
57 *pgoff
= PHYS_PFN(phys_off
);
58 if (phys_off
% PAGE_SIZE
|| size
% PAGE_SIZE
)
62 EXPORT_SYMBOL(bdev_dax_pgoff
);
64 #if IS_ENABLED(CONFIG_FS_DAX)
65 struct dax_device
*fs_dax_get_by_bdev(struct block_device
*bdev
)
67 if (!blk_queue_dax(bdev
->bd_queue
))
69 return fs_dax_get_by_host(bdev
->bd_disk
->disk_name
);
71 EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev
);
75 * __bdev_dax_supported() - Check if the device supports dax for filesystem
76 * @sb: The superblock of the device
77 * @blocksize: The block size of the device
79 * This is a library function for filesystems to check if the block device
80 * can be mounted with dax option.
82 * Return: negative errno if unsupported, 0 if supported.
84 int __bdev_dax_supported(struct super_block
*sb
, int blocksize
)
86 struct block_device
*bdev
= sb
->s_bdev
;
87 struct dax_device
*dax_dev
;
94 if (blocksize
!= PAGE_SIZE
) {
95 pr_debug("VFS (%s): error: unsupported blocksize for dax\n",
100 err
= bdev_dax_pgoff(bdev
, 0, PAGE_SIZE
, &pgoff
);
102 pr_debug("VFS (%s): error: unaligned partition for dax\n",
107 dax_dev
= dax_get_by_host(bdev
->bd_disk
->disk_name
);
109 pr_debug("VFS (%s): error: device does not support dax\n",
114 id
= dax_read_lock();
115 len
= dax_direct_access(dax_dev
, pgoff
, 1, &kaddr
, &pfn
);
121 pr_debug("VFS (%s): error: dax access failed (%ld)\n",
123 return len
< 0 ? len
: -EIO
;
128 EXPORT_SYMBOL_GPL(__bdev_dax_supported
);
131 enum dax_device_flags
{
132 /* !alive + rcu grace period == no new operations / mappings */
134 /* gate whether dax_flush() calls the low level flush routine */
139 * struct dax_device - anchor object for dax services
141 * @cdev: optional character interface for "device dax"
142 * @host: optional name for lookups where the device path is not available
143 * @private: dax driver private data
144 * @flags: state and boolean properties
147 struct hlist_node list
;
153 const struct dax_operations
*ops
;
156 static ssize_t
write_cache_show(struct device
*dev
,
157 struct device_attribute
*attr
, char *buf
)
159 struct dax_device
*dax_dev
= dax_get_by_host(dev_name(dev
));
162 WARN_ON_ONCE(!dax_dev
);
166 rc
= sprintf(buf
, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE
,
172 static ssize_t
write_cache_store(struct device
*dev
,
173 struct device_attribute
*attr
, const char *buf
, size_t len
)
176 int rc
= strtobool(buf
, &write_cache
);
177 struct dax_device
*dax_dev
= dax_get_by_host(dev_name(dev
));
179 WARN_ON_ONCE(!dax_dev
);
185 else if (write_cache
)
186 set_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
188 clear_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
193 static DEVICE_ATTR_RW(write_cache
);
195 static umode_t
dax_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
197 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
198 struct dax_device
*dax_dev
= dax_get_by_host(dev_name(dev
));
200 WARN_ON_ONCE(!dax_dev
);
204 #ifndef CONFIG_ARCH_HAS_PMEM_API
205 if (a
== &dev_attr_write_cache
.attr
)
211 static struct attribute
*dax_attributes
[] = {
212 &dev_attr_write_cache
.attr
,
216 struct attribute_group dax_attribute_group
= {
218 .attrs
= dax_attributes
,
219 .is_visible
= dax_visible
,
221 EXPORT_SYMBOL_GPL(dax_attribute_group
);
224 * dax_direct_access() - translate a device pgoff to an absolute pfn
225 * @dax_dev: a dax_device instance representing the logical memory range
226 * @pgoff: offset in pages from the start of the device to translate
227 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
228 * @kaddr: output parameter that returns a virtual address mapping of pfn
229 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
231 * Return: negative errno if an error occurs, otherwise the number of
232 * pages accessible at the device relative @pgoff.
234 long dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
, long nr_pages
,
235 void **kaddr
, pfn_t
*pfn
)
240 * The device driver is allowed to sleep, in order to make the
241 * memory directly accessible.
248 if (!dax_alive(dax_dev
))
254 avail
= dax_dev
->ops
->direct_access(dax_dev
, pgoff
, nr_pages
,
258 return min(avail
, nr_pages
);
260 EXPORT_SYMBOL_GPL(dax_direct_access
);
262 size_t dax_copy_from_iter(struct dax_device
*dax_dev
, pgoff_t pgoff
, void *addr
,
263 size_t bytes
, struct iov_iter
*i
)
265 if (!dax_alive(dax_dev
))
268 return dax_dev
->ops
->copy_from_iter(dax_dev
, pgoff
, addr
, bytes
, i
);
270 EXPORT_SYMBOL_GPL(dax_copy_from_iter
);
272 #ifdef CONFIG_ARCH_HAS_PMEM_API
273 void arch_wb_cache_pmem(void *addr
, size_t size
);
274 void dax_flush(struct dax_device
*dax_dev
, void *addr
, size_t size
)
276 if (unlikely(!test_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
)))
279 arch_wb_cache_pmem(addr
, size
);
282 void dax_flush(struct dax_device
*dax_dev
, void *addr
, size_t size
)
286 EXPORT_SYMBOL_GPL(dax_flush
);
288 void dax_write_cache(struct dax_device
*dax_dev
, bool wc
)
291 set_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
293 clear_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
295 EXPORT_SYMBOL_GPL(dax_write_cache
);
297 bool dax_write_cache_enabled(struct dax_device
*dax_dev
)
299 return test_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
301 EXPORT_SYMBOL_GPL(dax_write_cache_enabled
);
303 bool dax_alive(struct dax_device
*dax_dev
)
305 lockdep_assert_held(&dax_srcu
);
306 return test_bit(DAXDEV_ALIVE
, &dax_dev
->flags
);
308 EXPORT_SYMBOL_GPL(dax_alive
);
310 static int dax_host_hash(const char *host
)
312 return hashlen_hash(hashlen_string("DAX", host
)) % DAX_HASH_SIZE
;
316 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
317 * that any fault handlers or operations that might have seen
318 * dax_alive(), have completed. Any operations that start after
319 * synchronize_srcu() has run will abort upon seeing !dax_alive().
321 void kill_dax(struct dax_device
*dax_dev
)
326 clear_bit(DAXDEV_ALIVE
, &dax_dev
->flags
);
328 synchronize_srcu(&dax_srcu
);
330 spin_lock(&dax_host_lock
);
331 hlist_del_init(&dax_dev
->list
);
332 spin_unlock(&dax_host_lock
);
334 dax_dev
->private = NULL
;
336 EXPORT_SYMBOL_GPL(kill_dax
);
338 static struct inode
*dax_alloc_inode(struct super_block
*sb
)
340 struct dax_device
*dax_dev
;
343 dax_dev
= kmem_cache_alloc(dax_cache
, GFP_KERNEL
);
347 inode
= &dax_dev
->inode
;
352 static struct dax_device
*to_dax_dev(struct inode
*inode
)
354 return container_of(inode
, struct dax_device
, inode
);
357 static void dax_i_callback(struct rcu_head
*head
)
359 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
360 struct dax_device
*dax_dev
= to_dax_dev(inode
);
362 kfree(dax_dev
->host
);
363 dax_dev
->host
= NULL
;
365 ida_simple_remove(&dax_minor_ida
, MINOR(inode
->i_rdev
));
366 kmem_cache_free(dax_cache
, dax_dev
);
369 static void dax_destroy_inode(struct inode
*inode
)
371 struct dax_device
*dax_dev
= to_dax_dev(inode
);
373 WARN_ONCE(test_bit(DAXDEV_ALIVE
, &dax_dev
->flags
),
374 "kill_dax() must be called before final iput()\n");
375 call_rcu(&inode
->i_rcu
, dax_i_callback
);
378 static const struct super_operations dax_sops
= {
379 .statfs
= simple_statfs
,
380 .alloc_inode
= dax_alloc_inode
,
381 .destroy_inode
= dax_destroy_inode
,
382 .drop_inode
= generic_delete_inode
,
385 static struct dentry
*dax_mount(struct file_system_type
*fs_type
,
386 int flags
, const char *dev_name
, void *data
)
388 return mount_pseudo(fs_type
, "dax:", &dax_sops
, NULL
, DAXFS_MAGIC
);
391 static struct file_system_type dax_fs_type
= {
394 .kill_sb
= kill_anon_super
,
397 static int dax_test(struct inode
*inode
, void *data
)
399 dev_t devt
= *(dev_t
*) data
;
401 return inode
->i_rdev
== devt
;
404 static int dax_set(struct inode
*inode
, void *data
)
406 dev_t devt
= *(dev_t
*) data
;
408 inode
->i_rdev
= devt
;
412 static struct dax_device
*dax_dev_get(dev_t devt
)
414 struct dax_device
*dax_dev
;
417 inode
= iget5_locked(dax_superblock
, hash_32(devt
+ DAXFS_MAGIC
, 31),
418 dax_test
, dax_set
, &devt
);
423 dax_dev
= to_dax_dev(inode
);
424 if (inode
->i_state
& I_NEW
) {
425 set_bit(DAXDEV_ALIVE
, &dax_dev
->flags
);
426 inode
->i_cdev
= &dax_dev
->cdev
;
427 inode
->i_mode
= S_IFCHR
;
428 inode
->i_flags
= S_DAX
;
429 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
430 unlock_new_inode(inode
);
436 static void dax_add_host(struct dax_device
*dax_dev
, const char *host
)
441 * Unconditionally init dax_dev since it's coming from a
442 * non-zeroed slab cache
444 INIT_HLIST_NODE(&dax_dev
->list
);
445 dax_dev
->host
= host
;
449 hash
= dax_host_hash(host
);
450 spin_lock(&dax_host_lock
);
451 hlist_add_head(&dax_dev
->list
, &dax_host_list
[hash
]);
452 spin_unlock(&dax_host_lock
);
455 struct dax_device
*alloc_dax(void *private, const char *__host
,
456 const struct dax_operations
*ops
)
458 struct dax_device
*dax_dev
;
463 host
= kstrdup(__host
, GFP_KERNEL
);
467 minor
= ida_simple_get(&dax_minor_ida
, 0, MINORMASK
+1, GFP_KERNEL
);
471 devt
= MKDEV(MAJOR(dax_devt
), minor
);
472 dax_dev
= dax_dev_get(devt
);
476 dax_add_host(dax_dev
, host
);
478 dax_dev
->private = private;
482 ida_simple_remove(&dax_minor_ida
, minor
);
487 EXPORT_SYMBOL_GPL(alloc_dax
);
489 void put_dax(struct dax_device
*dax_dev
)
493 iput(&dax_dev
->inode
);
495 EXPORT_SYMBOL_GPL(put_dax
);
498 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
499 * @host: alternate name for the device registered by a dax driver
501 struct dax_device
*dax_get_by_host(const char *host
)
503 struct dax_device
*dax_dev
, *found
= NULL
;
509 hash
= dax_host_hash(host
);
511 id
= dax_read_lock();
512 spin_lock(&dax_host_lock
);
513 hlist_for_each_entry(dax_dev
, &dax_host_list
[hash
], list
) {
514 if (!dax_alive(dax_dev
)
515 || strcmp(host
, dax_dev
->host
) != 0)
518 if (igrab(&dax_dev
->inode
))
522 spin_unlock(&dax_host_lock
);
527 EXPORT_SYMBOL_GPL(dax_get_by_host
);
530 * inode_dax: convert a public inode into its dax_dev
531 * @inode: An inode with i_cdev pointing to a dax_dev
533 * Note this is not equivalent to to_dax_dev() which is for private
534 * internal use where we know the inode filesystem type == dax_fs_type.
536 struct dax_device
*inode_dax(struct inode
*inode
)
538 struct cdev
*cdev
= inode
->i_cdev
;
540 return container_of(cdev
, struct dax_device
, cdev
);
542 EXPORT_SYMBOL_GPL(inode_dax
);
544 struct inode
*dax_inode(struct dax_device
*dax_dev
)
546 return &dax_dev
->inode
;
548 EXPORT_SYMBOL_GPL(dax_inode
);
550 void *dax_get_private(struct dax_device
*dax_dev
)
552 return dax_dev
->private;
554 EXPORT_SYMBOL_GPL(dax_get_private
);
556 static void init_once(void *_dax_dev
)
558 struct dax_device
*dax_dev
= _dax_dev
;
559 struct inode
*inode
= &dax_dev
->inode
;
561 memset(dax_dev
, 0, sizeof(*dax_dev
));
562 inode_init_once(inode
);
565 static int __dax_fs_init(void)
569 dax_cache
= kmem_cache_create("dax_cache", sizeof(struct dax_device
), 0,
570 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
571 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
),
576 rc
= register_filesystem(&dax_fs_type
);
578 goto err_register_fs
;
580 dax_mnt
= kern_mount(&dax_fs_type
);
581 if (IS_ERR(dax_mnt
)) {
582 rc
= PTR_ERR(dax_mnt
);
585 dax_superblock
= dax_mnt
->mnt_sb
;
590 unregister_filesystem(&dax_fs_type
);
592 kmem_cache_destroy(dax_cache
);
597 static void __dax_fs_exit(void)
599 kern_unmount(dax_mnt
);
600 unregister_filesystem(&dax_fs_type
);
601 kmem_cache_destroy(dax_cache
);
604 static int __init
dax_fs_init(void)
608 rc
= __dax_fs_init();
612 rc
= alloc_chrdev_region(&dax_devt
, 0, MINORMASK
+1, "dax");
618 static void __exit
dax_fs_exit(void)
620 unregister_chrdev_region(dax_devt
, MINORMASK
+1);
621 ida_destroy(&dax_minor_ida
);
625 MODULE_AUTHOR("Intel Corporation");
626 MODULE_LICENSE("GPL v2");
627 subsys_initcall(dax_fs_init
);
628 module_exit(dax_fs_exit
);