From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: darrick.wong@oracle.com
Cc: axboe@kernel.dk, linux-block@vger.kernel.org, tytso@mit.edu,
martin.petersen@oracle.com, snitzer@redhat.com,
linux-api@vger.kernel.org, bfoster@redhat.com, xfs@oss.sgi.com,
hch@infradead.org, dm-devel@redhat.com,
linux-fsdevel@vger.kernel.org
Subject: [PATCH 3/3] block: implement (some of) fallocate for block devices
Date: Tue, 12 Apr 2016 21:01:42 -0700 [thread overview]
Message-ID: <20160413040142.10562.42269.stgit@birch.djwong.org> (raw)
In-Reply-To: <20160413040121.10562.98998.stgit@birch.djwong.org>
After much discussion, it seems that the fallocate feature flag
FALLOC_FL_ZERO_RANGE maps nicely to SCSI WRITE SAME; and the feature
FALLOC_FL_PUNCH_HOLE maps nicely to the devices that have been
whitelisted for zeroing SCSI UNMAP. Punch still requires that
FALLOC_FL_KEEP_SIZE is set. A length that goes past the end of the
device will be clamped to the device size if KEEP_SIZE is set; or will
return -EINVAL if not. Both start and length must be aligned to the
device's logical block size.
Since the semantics of fallocate are fairly well established already,
wire up the two pieces. The other fallocate variants (collapse range,
insert range, and allocate blocks) are not supported.
v2: Incorporate feedback from Christoph & Linus. Tentatively add
a requirement that the fallocate arguments be aligned to logical block
size, and put in a few XXX comments ahead of LSF discussion.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
fs/block_dev.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/open.c | 3 +-
2 files changed, 89 insertions(+), 1 deletion(-)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 20a2c02..5c8eb0c 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -29,6 +29,7 @@
#include <linux/log2.h>
#include <linux/cleancache.h>
#include <linux/dax.h>
+#include <linux/falloc.h>
#include <asm/uaccess.h>
#include "internal.h"
@@ -1790,6 +1791,91 @@ static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
#define blkdev_mmap generic_file_mmap
#endif
+#define BLKDEV_FALLOC_FL_SUPPORTED \
+ (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
+ FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
+
+static long blkdev_fallocate(struct file *file, int mode, loff_t start,
+ loff_t len)
+{
+ struct block_device *bdev = I_BDEV(bdev_file_inode(file));
+ struct request_queue *q = bdev_get_queue(bdev);
+ struct address_space *mapping;
+ loff_t end = start + len - 1;
+ loff_t isize;
+ int error;
+
+ /* Fail if we don't recognize the flags. */
+ if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
+ return -EOPNOTSUPP;
+
+ /* Don't go off the end of the device. */
+ isize = i_size_read(bdev->bd_inode);
+ if (start >= isize)
+ return -EINVAL;
+ if (end > isize) {
+ if (mode & FALLOC_FL_KEEP_SIZE) {
+ len = isize - start;
+ end = start + len - 1;
+ } else
+ return -EINVAL;
+ }
+
+ /*
+ * Don't allow IO that isn't aligned to logical block size.
+ * XXX: Should zero and punch write zeroes through the page cache
+ * if start or end aren't lbs aligned?
+ * XXX: What about thinp which prefers io_min alignment?
+ */
+ if ((start | len) & bdev_logical_block_size(bdev) - 1)
+ return -EINVAL;
+
+ /* Invalidate the page cache, including dirty pages. */
+ mapping = bdev->bd_inode->i_mapping;
+ truncate_inode_pages_range(mapping, start, end);
+
+ switch (mode) {
+ case FALLOC_FL_ZERO_RANGE:
+ case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
+ error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
+ GFP_KERNEL, false);
+ if (error)
+ return error;
+ break;
+ case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
+ /* Only punch if the device can do zeroing discard. */
+ if (!blk_queue_discard(q) || !q->limits.discard_zeroes_data)
+ return -EOPNOTSUPP;
+ error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
+ GFP_KERNEL, 0);
+ if (error)
+ return error;
+ break;
+ case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
+ /*
+ * XXX: a well known search engine vendor interprets this
+ * flag (in other circumstances) to mean "I don't care if
+ * we can read stale contents later". Is it appropriate
+ * to wire this up to the non-zeroing discard?
+ */
+ error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
+ GFP_KERNEL, 0);
+ if (error)
+ return error;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ /*
+ * Invalidate again; if someone wandered in and dirtied a page,
+ * the caller will be given -EBUSY;
+ */
+ return invalidate_inode_pages2_range(mapping,
+ start >> PAGE_SHIFT,
+ end >> PAGE_SHIFT);
+}
+
const struct file_operations def_blk_fops = {
.open = blkdev_open,
.release = blkdev_close,
@@ -1804,6 +1890,7 @@ const struct file_operations def_blk_fops = {
#endif
.splice_read = generic_file_splice_read,
.splice_write = iter_file_splice_write,
+ .fallocate = blkdev_fallocate,
};
int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
diff --git a/fs/open.c b/fs/open.c
index 17cb6b1..f9ebe32 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -289,7 +289,8 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
* Let individual file system decide if it supports preallocation
* for directories or not.
*/
- if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
+ if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
+ !S_ISBLK(inode->i_mode))
return -ENODEV;
/* Check for wrap through zero too */
_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs
next prev parent reply other threads:[~2016-04-13 4:02 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-13 4:01 [RFC DONOTMERGE v8 0/3] fallocate for block devices Darrick J. Wong
2016-04-13 4:01 ` [PATCH 1/3] block: invalidate the page cache when issuing BLKZEROOUT Darrick J. Wong
2016-04-13 4:01 ` [PATCH 2/3] block: require write_same and discard requests align to logical block size Darrick J. Wong
2016-04-13 14:23 ` Christoph Hellwig
2016-04-13 4:01 ` Darrick J. Wong [this message]
2016-04-13 4:04 ` [PATCH 4/3] block: test fallocate for block devices Darrick J. Wong
-- strict thread matches above, loose matches on Subject: below --
2016-06-17 1:17 [PATCH v9 0/3] " Darrick J. Wong
2016-06-17 1:17 ` [PATCH 3/3] block: implement (some of) " Darrick J. Wong
2016-08-26 0:02 [PATCH v10 0/3] " Darrick J. Wong
2016-08-26 0:02 ` [PATCH 3/3] block: implement (some of) " Darrick J. Wong
2016-09-29 0:39 [PATCH v10 0/3] " Darrick J. Wong
2016-09-29 0:39 ` [PATCH 3/3] block: implement (some of) " Darrick J. Wong
2016-09-29 1:42 ` Bart Van Assche
2016-09-29 2:09 ` Darrick J. Wong
2016-09-29 5:57 ` Hannes Reinecke
2016-09-29 21:16 [PATCH v11 0/3] " Darrick J. Wong
2016-09-29 21:16 ` [PATCH 3/3] block: implement (some of) " Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160413040142.10562.42269.stgit@birch.djwong.org \
--to=darrick.wong@oracle.com \
--cc=axboe@kernel.dk \
--cc=bfoster@redhat.com \
--cc=dm-devel@redhat.com \
--cc=hch@infradead.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=snitzer@redhat.com \
--cc=tytso@mit.edu \
--cc=xfs@oss.sgi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).