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 int bdev_dax_pgoff(struct block_device
*bdev
, sector_t sector
, size_t size
,
52 phys_addr_t phys_off
= (get_start_sect(bdev
) + sector
) * 512;
55 *pgoff
= PHYS_PFN(phys_off
);
56 if (phys_off
% PAGE_SIZE
|| size
% PAGE_SIZE
)
60 EXPORT_SYMBOL(bdev_dax_pgoff
);
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
67 * This is a library function for filesystems to check if the block device
68 * can be mounted with dax option.
70 * Return: negative errno if unsupported, 0 if supported.
72 int __bdev_dax_supported(struct super_block
*sb
, int blocksize
)
74 struct block_device
*bdev
= sb
->s_bdev
;
75 struct dax_device
*dax_dev
;
82 if (blocksize
!= PAGE_SIZE
) {
83 pr_err("VFS (%s): error: unsupported blocksize for dax\n",
88 err
= bdev_dax_pgoff(bdev
, 0, PAGE_SIZE
, &pgoff
);
90 pr_err("VFS (%s): error: unaligned partition for dax\n",
95 dax_dev
= dax_get_by_host(bdev
->bd_disk
->disk_name
);
97 pr_err("VFS (%s): error: device does not support dax\n",
102 id
= dax_read_lock();
103 len
= dax_direct_access(dax_dev
, pgoff
, 1, &kaddr
, &pfn
);
109 pr_err("VFS (%s): error: dax access failed (%ld)",
111 return len
< 0 ? len
: -EIO
;
116 EXPORT_SYMBOL_GPL(__bdev_dax_supported
);
119 enum dax_device_flags
{
120 /* !alive + rcu grace period == no new operations / mappings */
122 /* gate whether dax_flush() calls the low level flush routine */
127 * struct dax_device - anchor object for dax services
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
135 struct hlist_node list
;
141 const struct dax_operations
*ops
;
144 static ssize_t
write_cache_show(struct device
*dev
,
145 struct device_attribute
*attr
, char *buf
)
147 struct dax_device
*dax_dev
= dax_get_by_host(dev_name(dev
));
150 WARN_ON_ONCE(!dax_dev
);
154 rc
= sprintf(buf
, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE
,
160 static ssize_t
write_cache_store(struct device
*dev
,
161 struct device_attribute
*attr
, const char *buf
, size_t len
)
164 int rc
= strtobool(buf
, &write_cache
);
165 struct dax_device
*dax_dev
= dax_get_by_host(dev_name(dev
));
167 WARN_ON_ONCE(!dax_dev
);
173 else if (write_cache
)
174 set_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
176 clear_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
181 static DEVICE_ATTR_RW(write_cache
);
183 static umode_t
dax_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
185 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
186 struct dax_device
*dax_dev
= dax_get_by_host(dev_name(dev
));
188 WARN_ON_ONCE(!dax_dev
);
192 if (a
== &dev_attr_write_cache
.attr
&& !dax_dev
->ops
->flush
)
197 static struct attribute
*dax_attributes
[] = {
198 &dev_attr_write_cache
.attr
,
202 struct attribute_group dax_attribute_group
= {
204 .attrs
= dax_attributes
,
205 .is_visible
= dax_visible
,
207 EXPORT_SYMBOL_GPL(dax_attribute_group
);
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
217 * Return: negative errno if an error occurs, otherwise the number of
218 * pages accessible at the device relative @pgoff.
220 long dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
, long nr_pages
,
221 void **kaddr
, pfn_t
*pfn
)
226 * The device driver is allowed to sleep, in order to make the
227 * memory directly accessible.
234 if (!dax_alive(dax_dev
))
240 avail
= dax_dev
->ops
->direct_access(dax_dev
, pgoff
, nr_pages
,
244 return min(avail
, nr_pages
);
246 EXPORT_SYMBOL_GPL(dax_direct_access
);
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
)
251 if (!dax_alive(dax_dev
))
254 return dax_dev
->ops
->copy_from_iter(dax_dev
, pgoff
, addr
, bytes
, i
);
256 EXPORT_SYMBOL_GPL(dax_copy_from_iter
);
258 void dax_flush(struct dax_device
*dax_dev
, pgoff_t pgoff
, void *addr
,
261 if (!dax_alive(dax_dev
))
264 if (!test_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
))
267 if (dax_dev
->ops
->flush
)
268 dax_dev
->ops
->flush(dax_dev
, pgoff
, addr
, size
);
270 EXPORT_SYMBOL_GPL(dax_flush
);
272 void dax_write_cache(struct dax_device
*dax_dev
, bool wc
)
275 set_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
277 clear_bit(DAXDEV_WRITE_CACHE
, &dax_dev
->flags
);
279 EXPORT_SYMBOL_GPL(dax_write_cache
);
281 bool dax_alive(struct dax_device
*dax_dev
)
283 lockdep_assert_held(&dax_srcu
);
284 return test_bit(DAXDEV_ALIVE
, &dax_dev
->flags
);
286 EXPORT_SYMBOL_GPL(dax_alive
);
288 static int dax_host_hash(const char *host
)
290 return hashlen_hash(hashlen_string("DAX", host
)) % DAX_HASH_SIZE
;
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().
299 void kill_dax(struct dax_device
*dax_dev
)
304 clear_bit(DAXDEV_ALIVE
, &dax_dev
->flags
);
306 synchronize_srcu(&dax_srcu
);
308 spin_lock(&dax_host_lock
);
309 hlist_del_init(&dax_dev
->list
);
310 spin_unlock(&dax_host_lock
);
312 dax_dev
->private = NULL
;
314 EXPORT_SYMBOL_GPL(kill_dax
);
316 static struct inode
*dax_alloc_inode(struct super_block
*sb
)
318 struct dax_device
*dax_dev
;
321 dax_dev
= kmem_cache_alloc(dax_cache
, GFP_KERNEL
);
322 inode
= &dax_dev
->inode
;
327 static struct dax_device
*to_dax_dev(struct inode
*inode
)
329 return container_of(inode
, struct dax_device
, inode
);
332 static void dax_i_callback(struct rcu_head
*head
)
334 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
335 struct dax_device
*dax_dev
= to_dax_dev(inode
);
337 kfree(dax_dev
->host
);
338 dax_dev
->host
= NULL
;
340 ida_simple_remove(&dax_minor_ida
, MINOR(inode
->i_rdev
));
341 kmem_cache_free(dax_cache
, dax_dev
);
344 static void dax_destroy_inode(struct inode
*inode
)
346 struct dax_device
*dax_dev
= to_dax_dev(inode
);
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
);
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
,
360 static struct dentry
*dax_mount(struct file_system_type
*fs_type
,
361 int flags
, const char *dev_name
, void *data
)
363 return mount_pseudo(fs_type
, "dax:", &dax_sops
, NULL
, DAXFS_MAGIC
);
366 static struct file_system_type dax_fs_type
= {
369 .kill_sb
= kill_anon_super
,
372 static int dax_test(struct inode
*inode
, void *data
)
374 dev_t devt
= *(dev_t
*) data
;
376 return inode
->i_rdev
== devt
;
379 static int dax_set(struct inode
*inode
, void *data
)
381 dev_t devt
= *(dev_t
*) data
;
383 inode
->i_rdev
= devt
;
387 static struct dax_device
*dax_dev_get(dev_t devt
)
389 struct dax_device
*dax_dev
;
392 inode
= iget5_locked(dax_superblock
, hash_32(devt
+ DAXFS_MAGIC
, 31),
393 dax_test
, dax_set
, &devt
);
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
);
411 static void dax_add_host(struct dax_device
*dax_dev
, const char *host
)
416 * Unconditionally init dax_dev since it's coming from a
417 * non-zeroed slab cache
419 INIT_HLIST_NODE(&dax_dev
->list
);
420 dax_dev
->host
= host
;
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
);
430 struct dax_device
*alloc_dax(void *private, const char *__host
,
431 const struct dax_operations
*ops
)
433 struct dax_device
*dax_dev
;
438 host
= kstrdup(__host
, GFP_KERNEL
);
442 minor
= ida_simple_get(&dax_minor_ida
, 0, MINORMASK
+1, GFP_KERNEL
);
446 devt
= MKDEV(MAJOR(dax_devt
), minor
);
447 dax_dev
= dax_dev_get(devt
);
451 dax_add_host(dax_dev
, host
);
453 dax_dev
->private = private;
457 ida_simple_remove(&dax_minor_ida
, minor
);
462 EXPORT_SYMBOL_GPL(alloc_dax
);
464 void put_dax(struct dax_device
*dax_dev
)
468 iput(&dax_dev
->inode
);
470 EXPORT_SYMBOL_GPL(put_dax
);
473 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
474 * @host: alternate name for the device registered by a dax driver
476 struct dax_device
*dax_get_by_host(const char *host
)
478 struct dax_device
*dax_dev
, *found
= NULL
;
484 hash
= dax_host_hash(host
);
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)
493 if (igrab(&dax_dev
->inode
))
497 spin_unlock(&dax_host_lock
);
502 EXPORT_SYMBOL_GPL(dax_get_by_host
);
505 * inode_dax: convert a public inode into its dax_dev
506 * @inode: An inode with i_cdev pointing to a dax_dev
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.
511 struct dax_device
*inode_dax(struct inode
*inode
)
513 struct cdev
*cdev
= inode
->i_cdev
;
515 return container_of(cdev
, struct dax_device
, cdev
);
517 EXPORT_SYMBOL_GPL(inode_dax
);
519 struct inode
*dax_inode(struct dax_device
*dax_dev
)
521 return &dax_dev
->inode
;
523 EXPORT_SYMBOL_GPL(dax_inode
);
525 void *dax_get_private(struct dax_device
*dax_dev
)
527 return dax_dev
->private;
529 EXPORT_SYMBOL_GPL(dax_get_private
);
531 static void init_once(void *_dax_dev
)
533 struct dax_device
*dax_dev
= _dax_dev
;
534 struct inode
*inode
= &dax_dev
->inode
;
536 memset(dax_dev
, 0, sizeof(*dax_dev
));
537 inode_init_once(inode
);
540 static int __dax_fs_init(void)
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
),
551 rc
= register_filesystem(&dax_fs_type
);
553 goto err_register_fs
;
555 dax_mnt
= kern_mount(&dax_fs_type
);
556 if (IS_ERR(dax_mnt
)) {
557 rc
= PTR_ERR(dax_mnt
);
560 dax_superblock
= dax_mnt
->mnt_sb
;
565 unregister_filesystem(&dax_fs_type
);
567 kmem_cache_destroy(dax_cache
);
572 static void __dax_fs_exit(void)
574 kern_unmount(dax_mnt
);
575 unregister_filesystem(&dax_fs_type
);
576 kmem_cache_destroy(dax_cache
);
579 static int __init
dax_fs_init(void)
583 rc
= __dax_fs_init();
587 rc
= alloc_chrdev_region(&dax_devt
, 0, MINORMASK
+1, "dax");
593 static void __exit
dax_fs_exit(void)
595 unregister_chrdev_region(dax_devt
, MINORMASK
+1);
596 ida_destroy(&dax_minor_ida
);
600 MODULE_AUTHOR("Intel Corporation");
601 MODULE_LICENSE("GPL v2");
602 subsys_initcall(dax_fs_init
);
603 module_exit(dax_fs_exit
);