|
14 | 14 | #include <linux/module.h> |
15 | 15 | #include <linux/mount.h> |
16 | 16 | #include <linux/magic.h> |
| 17 | +#include <linux/genhd.h> |
17 | 18 | #include <linux/cdev.h> |
18 | 19 | #include <linux/hash.h> |
19 | 20 | #include <linux/slab.h> |
@@ -47,6 +48,75 @@ void dax_read_unlock(int id) |
47 | 48 | } |
48 | 49 | EXPORT_SYMBOL_GPL(dax_read_unlock); |
49 | 50 |
|
| 51 | +int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size, |
| 52 | + pgoff_t *pgoff) |
| 53 | +{ |
| 54 | + phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512; |
| 55 | + |
| 56 | + if (pgoff) |
| 57 | + *pgoff = PHYS_PFN(phys_off); |
| 58 | + if (phys_off % PAGE_SIZE || size % PAGE_SIZE) |
| 59 | + return -EINVAL; |
| 60 | + return 0; |
| 61 | +} |
| 62 | +EXPORT_SYMBOL(bdev_dax_pgoff); |
| 63 | + |
| 64 | +/** |
| 65 | + * __bdev_dax_supported() - Check if the device supports dax for filesystem |
| 66 | + * @sb: The superblock of the device |
| 67 | + * @blocksize: The block size of the device |
| 68 | + * |
| 69 | + * This is a library function for filesystems to check if the block device |
| 70 | + * can be mounted with dax option. |
| 71 | + * |
| 72 | + * Return: negative errno if unsupported, 0 if supported. |
| 73 | + */ |
| 74 | +int __bdev_dax_supported(struct super_block *sb, int blocksize) |
| 75 | +{ |
| 76 | + struct block_device *bdev = sb->s_bdev; |
| 77 | + struct dax_device *dax_dev; |
| 78 | + pgoff_t pgoff; |
| 79 | + int err, id; |
| 80 | + void *kaddr; |
| 81 | + pfn_t pfn; |
| 82 | + long len; |
| 83 | + |
| 84 | + if (blocksize != PAGE_SIZE) { |
| 85 | + pr_err("VFS (%s): error: unsupported blocksize for dax\n", |
| 86 | + sb->s_id); |
| 87 | + return -EINVAL; |
| 88 | + } |
| 89 | + |
| 90 | + err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff); |
| 91 | + if (err) { |
| 92 | + pr_err("VFS (%s): error: unaligned partition for dax\n", |
| 93 | + sb->s_id); |
| 94 | + return err; |
| 95 | + } |
| 96 | + |
| 97 | + dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); |
| 98 | + if (!dax_dev) { |
| 99 | + pr_err("VFS (%s): error: device does not support dax\n", |
| 100 | + sb->s_id); |
| 101 | + return -EOPNOTSUPP; |
| 102 | + } |
| 103 | + |
| 104 | + id = dax_read_lock(); |
| 105 | + len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn); |
| 106 | + dax_read_unlock(id); |
| 107 | + |
| 108 | + put_dax(dax_dev); |
| 109 | + |
| 110 | + if (len < 1) { |
| 111 | + pr_err("VFS (%s): error: dax access failed (%ld)", |
| 112 | + sb->s_id, len); |
| 113 | + return len < 0 ? len : -EIO; |
| 114 | + } |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | +EXPORT_SYMBOL_GPL(__bdev_dax_supported); |
| 119 | + |
50 | 120 | /** |
51 | 121 | * struct dax_device - anchor object for dax services |
52 | 122 | * @inode: core vfs |
|
0 commit comments