Merge branch 'work.mount0' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.git] / drivers / dax / super.c
index a93225c..26a654d 100644 (file)
@@ -1,14 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright(c) 2017 Intel Corporation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
  */
 #include <linux/pagemap.h>
 #include <linux/module.h>
@@ -74,22 +66,12 @@ struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev)
 EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
 #endif
 
-/**
- * __bdev_dax_supported() - Check if the device supports dax for filesystem
- * @bdev: block device to check
- * @blocksize: The block size of the device
- *
- * This is a library function for filesystems to check if the block device
- * can be mounted with dax option.
- *
- * Return: true if supported, false if unsupported
- */
-bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
+bool __generic_fsdax_supported(struct dax_device *dax_dev,
+               struct block_device *bdev, int blocksize, sector_t start,
+               sector_t sectors)
 {
-       struct dax_device *dax_dev;
        bool dax_enabled = false;
        pgoff_t pgoff, pgoff_end;
-       struct request_queue *q;
        char buf[BDEVNAME_SIZE];
        void *kaddr, *end_kaddr;
        pfn_t pfn, end_pfn;
@@ -103,21 +85,14 @@ bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
                return false;
        }
 
-       q = bdev_get_queue(bdev);
-       if (!q || !blk_queue_dax(q)) {
-               pr_debug("%s: error: request queue doesn't support dax\n",
-                               bdevname(bdev, buf));
-               return false;
-       }
-
-       err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
+       err = bdev_dax_pgoff(bdev, start, PAGE_SIZE, &pgoff);
        if (err) {
                pr_debug("%s: error: unaligned partition for dax\n",
                                bdevname(bdev, buf));
                return false;
        }
 
-       last_page = PFN_DOWN(i_size_read(bdev->bd_inode) - 1) * 8;
+       last_page = PFN_DOWN((start + sectors - 1) * 512) * PAGE_SIZE / 512;
        err = bdev_dax_pgoff(bdev, last_page, PAGE_SIZE, &pgoff_end);
        if (err) {
                pr_debug("%s: error: unaligned partition for dax\n",
@@ -125,20 +100,11 @@ bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
                return false;
        }
 
-       dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
-       if (!dax_dev) {
-               pr_debug("%s: error: device does not support dax\n",
-                               bdevname(bdev, buf));
-               return false;
-       }
-
        id = dax_read_lock();
        len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
        len2 = dax_direct_access(dax_dev, pgoff_end, 1, &end_kaddr, &end_pfn);
        dax_read_unlock(id);
 
-       put_dax(dax_dev);
-
        if (len < 1 || len2 < 1) {
                pr_debug("%s: error: dax access failed (%ld)\n",
                                bdevname(bdev, buf), len < 1 ? len : len2);
@@ -179,6 +145,49 @@ bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
        }
        return true;
 }
+EXPORT_SYMBOL_GPL(__generic_fsdax_supported);
+
+/**
+ * __bdev_dax_supported() - Check if the device supports dax for filesystem
+ * @bdev: block device to check
+ * @blocksize: The block size of the device
+ *
+ * This is a library function for filesystems to check if the block device
+ * can be mounted with dax option.
+ *
+ * Return: true if supported, false if unsupported
+ */
+bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
+{
+       struct dax_device *dax_dev;
+       struct request_queue *q;
+       char buf[BDEVNAME_SIZE];
+       bool ret;
+       int id;
+
+       q = bdev_get_queue(bdev);
+       if (!q || !blk_queue_dax(q)) {
+               pr_debug("%s: error: request queue doesn't support dax\n",
+                               bdevname(bdev, buf));
+               return false;
+       }
+
+       dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
+       if (!dax_dev) {
+               pr_debug("%s: error: device does not support dax\n",
+                               bdevname(bdev, buf));
+               return false;
+       }
+
+       id = dax_read_lock();
+       ret = dax_supported(dax_dev, bdev, blocksize, 0,
+                       i_size_read(bdev->bd_inode) / 512);
+       dax_read_unlock(id);
+
+       put_dax(dax_dev);
+
+       return ret;
+}
 EXPORT_SYMBOL_GPL(__bdev_dax_supported);
 #endif
 
@@ -187,6 +196,8 @@ enum dax_device_flags {
        DAXDEV_ALIVE,
        /* gate whether dax_flush() calls the low level flush routine */
        DAXDEV_WRITE_CACHE,
+       /* flag to check if device supports synchronous flush */
+       DAXDEV_SYNC,
 };
 
 /**
@@ -304,6 +315,15 @@ long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
 }
 EXPORT_SYMBOL_GPL(dax_direct_access);
 
+bool dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
+               int blocksize, sector_t start, sector_t len)
+{
+       if (!dax_alive(dax_dev))
+               return false;
+
+       return dax_dev->ops->dax_supported(dax_dev, bdev, blocksize, start, len);
+}
+
 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
                size_t bytes, struct iov_iter *i)
 {
@@ -355,6 +375,18 @@ bool dax_write_cache_enabled(struct dax_device *dax_dev)
 }
 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
 
+bool __dax_synchronous(struct dax_device *dax_dev)
+{
+       return test_bit(DAXDEV_SYNC, &dax_dev->flags);
+}
+EXPORT_SYMBOL_GPL(__dax_synchronous);
+
+void __set_dax_synchronous(struct dax_device *dax_dev)
+{
+       set_bit(DAXDEV_SYNC, &dax_dev->flags);
+}
+EXPORT_SYMBOL_GPL(__set_dax_synchronous);
+
 bool dax_alive(struct dax_device *dax_dev)
 {
        lockdep_assert_held(&dax_srcu);
@@ -512,7 +544,7 @@ static void dax_add_host(struct dax_device *dax_dev, const char *host)
 }
 
 struct dax_device *alloc_dax(void *private, const char *__host,
-               const struct dax_operations *ops)
+               const struct dax_operations *ops, unsigned long flags)
 {
        struct dax_device *dax_dev;
        const char *host;
@@ -535,6 +567,9 @@ struct dax_device *alloc_dax(void *private, const char *__host,
        dax_add_host(dax_dev, host);
        dax_dev->ops = ops;
        dax_dev->private = private;
+       if (flags & DAXDEV_F_SYNC)
+               set_dax_synchronous(dax_dev);
+
        return dax_dev;
 
  err_dev: