DAX imposes additional requirements to a device.  Add
bdev_supports_dax() which performs all the precondition checks
necessary for filesystem to mount the device with dax option.

Also add a new check to verify if a partition is aligned by 4KB.
When a partition is unaligned, any dax read/write access fails,
except for metadata update.

Signed-off-by: Toshi Kani <toshi.k...@hpe.com>
Cc: Alexander Viro <v...@zeniv.linux.org.uk>
Cc: Jens Axboe <ax...@fb.com>
Cc: "Theodore Ts'o" <ty...@mit.edu>
Cc: Andreas Dilger <adilger.ker...@dilger.ca>
Cc: Jan Kara <j...@suse.cz>
Cc: Dave Chinner <da...@fromorbit.com>
Cc: Dan Williams <dan.j.willi...@intel.com>
Cc: Ross Zwisler <ross.zwis...@linux.intel.com>
Cc: Christoph Hellwig <h...@infradead.org>
Cc: Boaz Harrosh <b...@plexistor.com>
---
 fs/block_dev.c         |   42 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/blkdev.h |    1 +
 2 files changed, 43 insertions(+)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index 7be17c4..e51a2c3 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -509,6 +509,48 @@ long bdev_direct_access(struct block_device *bdev, struct 
blk_dax_ctl *dax)
 }
 EXPORT_SYMBOL_GPL(bdev_direct_access);
 
+/**
+ * bdev_supports_dax() - Check if the block device supports DAX
+ * @sb: The superblock of the device
+ * @blocksize: The block size of the device
+ *
+ * Return: negative errno if unsupported, 0 if supported.
+ */
+int bdev_supports_dax(struct super_block *sb, int blocksize)
+{
+       struct blk_dax_ctl dax = {
+               .sector = 0,
+               .size = PAGE_SIZE,
+       };
+       int err;
+
+       if (blocksize != PAGE_SIZE) {
+               vfs_msg(sb, KERN_ERR, "error: unsupported blocksize for dax");
+               return -EINVAL;
+       }
+
+       err = bdev_direct_access(sb->s_bdev, &dax);
+       if (err < 0) {
+               switch (err) {
+               case -EOPNOTSUPP:
+                       vfs_msg(sb, KERN_ERR,
+                               "error: device does not support dax");
+                       break;
+               case -EINVAL:
+                       vfs_msg(sb, KERN_ERR,
+                               "error: unaligned partition for dax");
+                       break;
+               default:
+                       vfs_msg(sb, KERN_ERR,
+                               "error: dax access failed (%d)", err);
+               }
+               return err;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(bdev_supports_dax);
+
 /*
  * pseudo-fs
  */
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 78c48ab..6a792aa 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1688,6 +1688,7 @@ extern int bdev_read_page(struct block_device *, 
sector_t, struct page *);
 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
                                                struct writeback_control *);
 extern long bdev_direct_access(struct block_device *, struct blk_dax_ctl *);
+extern int bdev_supports_dax(struct super_block *, int);
 #else /* CONFIG_BLOCK */
 
 struct block_device;

Reply via email to