* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
[not found] ` <20160315194244.30093.6483.stgit@birch.djwong.org>
@ 2016-03-21 18:52 ` Mike Snitzer
2016-03-21 19:11 ` Darrick J. Wong
0 siblings, 1 reply; 12+ messages in thread
From: Mike Snitzer @ 2016-03-21 18:52 UTC (permalink / raw)
To: Darrick J. Wong
Cc: Jens Axboe, Christoph Hellwig, Theodore Ts'o,
Martin K. Petersen, linux-api, david,
linux-kernel@vger.kernel.org, shane.seymour, Bruce Fields,
device-mapper development, linux-fsdevel, Jeff Layton,
Linus Torvalds, Andrew Morton
On Tue, Mar 15, 2016 at 3:42 PM, Darrick J. Wong
<darrick.wong@oracle.com> wrote:
> 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.
I'd like to see fallocate (block allocation) extend down to DM thinp.
This more traditional use of fallocate would be useful for ensuring
ENOSPC won't occur -- especially important if the FS has committed
space in response to fallocate. As of now fallocate doesn't inform DM
thinp at all. Curious why you decided not to wire it up?
But I'm not sure what "it" (the "allocate blocks" variant) even is
given falloc.h doesn't show anything like "_ALLOCATE_BLOCKS"...
It would require a new block interface to pass the fallocate extent
down. But it seems bizarre to implement "some of" fallocate but not
the most widely used case for fallocate.
Mike
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
2016-03-21 18:52 ` [PATCH 3/3] block: implement (some of) fallocate for block devices Mike Snitzer
@ 2016-03-21 19:11 ` Darrick J. Wong
2016-03-21 19:22 ` Mike Snitzer
0 siblings, 1 reply; 12+ messages in thread
From: Darrick J. Wong @ 2016-03-21 19:11 UTC (permalink / raw)
To: Mike Snitzer
Cc: Jens Axboe, Christoph Hellwig, Theodore Ts'o,
Martin K. Petersen, linux-api, david,
linux-kernel@vger.kernel.org, shane.seymour, Bruce Fields,
device-mapper development, linux-fsdevel, Jeff Layton,
Linus Torvalds, Andrew Morton
On Mon, Mar 21, 2016 at 02:52:00PM -0400, Mike Snitzer wrote:
> On Tue, Mar 15, 2016 at 3:42 PM, Darrick J. Wong
> <darrick.wong@oracle.com> wrote:
> > 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.
>
> I'd like to see fallocate (block allocation) extend down to DM thinp.
> This more traditional use of fallocate would be useful for ensuring
> ENOSPC won't occur -- especially important if the FS has committed
> space in response to fallocate. As of now fallocate doesn't inform DM
> thinp at all. Curious why you decided not to wire it up?
I don't know what to wire it up to. :)
I didn't find any blkdev_* function that looked encouraging, though I
haven't dug too deeply into bfoster's "prototype a block reservation
allocation model" patchset yet. At a high level I'd guess that would
be a reasonable piece to connect to? It looks like the piece I want
is blk_provision_space().
> But I'm not sure what "it" (the "allocate blocks" variant) even is
> given falloc.h doesn't show anything like "_ALLOCATE_BLOCKS"...
The default behavior of fallocate is to allocate blocks, which means
that one invokes it by not passing any mode flags (except possibly
KEEP_SIZE).
> It would require a new block interface to pass the fallocate extent
> down. But it seems bizarre to implement "some of" fallocate but not
> the most widely used case for fallocate.
Agreed. I'd like to get the existing functionality wired up sooner than
later, and plumbing "allocate blocks" down to thinp can be done as a
followup.
(Or stall long enough that it becomes one patchset.)
--D
>
> Mike
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
2016-03-21 19:11 ` Darrick J. Wong
@ 2016-03-21 19:22 ` Mike Snitzer
[not found] ` <20160321192229.GA17220-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
0 siblings, 1 reply; 12+ messages in thread
From: Mike Snitzer @ 2016-03-21 19:22 UTC (permalink / raw)
To: Darrick J. Wong
Cc: Jens Axboe, Linus Torvalds, Bruce Fields, Theodore Ts'o,
Martin K. Petersen, linux-api, david,
linux-kernel@vger.kernel.org, shane.seymour, Christoph Hellwig,
linux-fsdevel, Jeff Layton, Andrew Morton,
device-mapper development
On Mon, Mar 21 2016 at 3:11pm -0400,
Darrick J. Wong <darrick.wong@oracle.com> wrote:
> On Mon, Mar 21, 2016 at 02:52:00PM -0400, Mike Snitzer wrote:
> > On Tue, Mar 15, 2016 at 3:42 PM, Darrick J. Wong
> > <darrick.wong@oracle.com> wrote:
> > > 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.
> >
> > I'd like to see fallocate (block allocation) extend down to DM thinp.
> > This more traditional use of fallocate would be useful for ensuring
> > ENOSPC won't occur -- especially important if the FS has committed
> > space in response to fallocate. As of now fallocate doesn't inform DM
> > thinp at all. Curious why you decided not to wire it up?
>
> I don't know what to wire it up to. :)
Fair enough. Yes something needs to be invented.
> I didn't find any blkdev_* function that looked encouraging, though I
> haven't dug too deeply into bfoster's "prototype a block reservation
> allocation model" patchset yet. At a high level I'd guess that would
> be a reasonable piece to connect to? It looks like the piece I want
> is blk_provision_space().
Yes, something like that.
> > But I'm not sure what "it" (the "allocate blocks" variant) even is
> > given falloc.h doesn't show anything like "_ALLOCATE_BLOCKS"...
>
> The default behavior of fallocate is to allocate blocks, which means
> that one invokes it by not passing any mode flags (except possibly
> KEEP_SIZE).
OK.
> > It would require a new block interface to pass the fallocate extent
> > down. But it seems bizarre to implement "some of" fallocate but not
> > the most widely used case for fallocate.
>
> Agreed. I'd like to get the existing functionality wired up sooner than
> later, and plumbing "allocate blocks" down to thinp can be done as a
> followup.
>
> (Or stall long enough that it becomes one patchset.)
Sure, sounds good. Glad we're in agreement.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
[not found] ` <20160321192229.GA17220-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
@ 2016-03-21 20:59 ` Brian Foster
0 siblings, 0 replies; 12+ messages in thread
From: Brian Foster @ 2016-03-21 20:59 UTC (permalink / raw)
To: Mike Snitzer
Cc: Darrick J. Wong, Jens Axboe, Linus Torvalds, Bruce Fields,
Theodore Ts'o, Martin K. Petersen,
linux-api-u79uwXL29TY76Z2rM5mHXA, david-FqsqvQoI3Ljby3iVrkZq2A,
linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
shane.seymour-ZPxbGqLxI0U, Christoph Hellwig, linux-fsdevel,
Jeff Layton, Andrew Morton, device-mapper development
On Mon, Mar 21, 2016 at 03:22:29PM -0400, Mike Snitzer wrote:
> On Mon, Mar 21 2016 at 3:11pm -0400,
> Darrick J. Wong <darrick.wong-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org> wrote:
>
> > On Mon, Mar 21, 2016 at 02:52:00PM -0400, Mike Snitzer wrote:
> > > On Tue, Mar 15, 2016 at 3:42 PM, Darrick J. Wong
> > > <darrick.wong-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org> wrote:
> > > > 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.
> > >
> > > I'd like to see fallocate (block allocation) extend down to DM thinp.
> > > This more traditional use of fallocate would be useful for ensuring
> > > ENOSPC won't occur -- especially important if the FS has committed
> > > space in response to fallocate. As of now fallocate doesn't inform DM
> > > thinp at all. Curious why you decided not to wire it up?
> >
> > I don't know what to wire it up to. :)
>
> Fair enough. Yes something needs to be invented.
>
> > I didn't find any blkdev_* function that looked encouraging, though I
> > haven't dug too deeply into bfoster's "prototype a block reservation
> > allocation model" patchset yet. At a high level I'd guess that would
> > be a reasonable piece to connect to? It looks like the piece I want
> > is blk_provision_space().
>
> Yes, something like that.
>
Just a note that the caveat/hack with the provision call in there is
that it returns an allocated block count. That was necessary to help
maintain the local reservation accounting. I'd love to find a way to
handle that more cleanly or take advantage of generic fallocate, but I
don't have a clear idea on how to do that at the moment. (I do wonder
whether an internal-only set of falloc "reserve" flags would fly...).
Anyways, that's a separate topic. Feel free to steal any of that dm-thin
provision code if it is useful for generic fallocate(). :)
Brian
> > > But I'm not sure what "it" (the "allocate blocks" variant) even is
> > > given falloc.h doesn't show anything like "_ALLOCATE_BLOCKS"...
> >
> > The default behavior of fallocate is to allocate blocks, which means
> > that one invokes it by not passing any mode flags (except possibly
> > KEEP_SIZE).
>
> OK.
>
> > > It would require a new block interface to pass the fallocate extent
> > > down. But it seems bizarre to implement "some of" fallocate but not
> > > the most widely used case for fallocate.
> >
> > Agreed. I'd like to get the existing functionality wired up sooner than
> > later, and plumbing "allocate blocks" down to thinp can be done as a
> > followup.
> >
> > (Or stall long enough that it becomes one patchset.)
>
> Sure, sounds good. Glad we're in agreement.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 3/3] block: implement (some of) fallocate for block devices
[not found] ` <20160413040121.10562.98998.stgit-PTl6brltDGh4DFYR7WNSRA@public.gmane.org>
@ 2016-04-13 4:01 ` Darrick J. Wong
0 siblings, 0 replies; 12+ messages in thread
From: Darrick J. Wong @ 2016-04-13 4:01 UTC (permalink / raw)
To: darrick.wong-QHcLZuEGTsvQT0dZR+AlfA
Cc: axboe-tSWWG44O7X1aa/9Udqfwiw, hch-wEGCiKHe2LqWVfeAwA7xHQ,
tytso-3s7WtUTddSA, martin.petersen-QHcLZuEGTsvQT0dZR+AlfA,
snitzer-H+wXaHxf7aLQT0dZR+AlfA, linux-api-u79uwXL29TY76Z2rM5mHXA,
bfoster-H+wXaHxf7aLQT0dZR+AlfA, xfs-VZNHf3L845pBDgjK7y7TUQ,
linux-block-u79uwXL29TY76Z2rM5mHXA,
dm-devel-H+wXaHxf7aLQT0dZR+AlfA,
linux-fsdevel-u79uwXL29TY76Z2rM5mHXA
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-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org>
---
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 */
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 3/3] block: implement (some of) fallocate for block devices
[not found] ` <146612624734.12764.4316680863289411106.stgit-PTl6brltDGh4DFYR7WNSRA@public.gmane.org>
@ 2016-06-17 1:17 ` Darrick J. Wong
0 siblings, 0 replies; 12+ messages in thread
From: Darrick J. Wong @ 2016-06-17 1:17 UTC (permalink / raw)
To: axboe-tSWWG44O7X1aa/9Udqfwiw, darrick.wong-QHcLZuEGTsvQT0dZR+AlfA
Cc: linux-block-u79uwXL29TY76Z2rM5mHXA, tytso-3s7WtUTddSA,
martin.petersen-QHcLZuEGTsvQT0dZR+AlfA,
snitzer-H+wXaHxf7aLQT0dZR+AlfA, linux-api-u79uwXL29TY76Z2rM5mHXA,
bfoster-H+wXaHxf7aLQT0dZR+AlfA, xfs-VZNHf3L845pBDgjK7y7TUQ,
hch-wEGCiKHe2LqWVfeAwA7xHQ, dm-devel-H+wXaHxf7aLQT0dZR+AlfA,
linux-fsdevel-u79uwXL29TY76Z2rM5mHXA
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.
v3: Forward port to 4.7.
Signed-off-by: Darrick J. Wong <darrick.wong-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org>
---
fs/block_dev.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/open.c | 3 +-
2 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 71ccab1..a3975c4 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -30,6 +30,7 @@
#include <linux/cleancache.h>
#include <linux/dax.h>
#include <linux/badblocks.h>
+#include <linux/falloc.h>
#include <asm/uaccess.h>
#include "internal.h"
@@ -1802,6 +1803,88 @@ static const struct address_space_operations def_blk_aops = {
.is_dirty_writeback = buffer_check_dirty_writeback,
};
+#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.
+ */
+ 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,
@@ -1816,6 +1899,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 93ae3cd..b2dbda4 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 */
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 3/3] block: implement (some of) fallocate for block devices
2016-08-26 0:02 [PATCH v10 0/3] " Darrick J. Wong
@ 2016-08-26 0:02 ` Darrick J. Wong
0 siblings, 0 replies; 12+ messages in thread
From: Darrick J. Wong @ 2016-08-26 0:02 UTC (permalink / raw)
To: axboe, darrick.wong
Cc: hch, tytso, martin.petersen, snitzer, linux-api, bfoster, xfs,
linux-block, dm-devel, linux-fsdevel, bart.vanassche
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.
v3: Forward port to 4.7.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
fs/block_dev.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/open.c | 3 +-
2 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index c3cdde8..4df3fc8 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -30,6 +30,7 @@
#include <linux/cleancache.h>
#include <linux/dax.h>
#include <linux/badblocks.h>
+#include <linux/falloc.h>
#include <asm/uaccess.h>
#include "internal.h"
@@ -1786,6 +1787,88 @@ static const struct address_space_operations def_blk_aops = {
.is_dirty_writeback = buffer_check_dirty_writeback,
};
+#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.
+ */
+ 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,
@@ -1800,6 +1883,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 4fd6e25..01b6092 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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 3/3] block: implement (some of) fallocate for block devices
2016-09-29 0:39 [PATCH v10 0/3] " Darrick J. Wong
@ 2016-09-29 0:39 ` Darrick J. Wong
2016-09-29 1:42 ` Bart Van Assche
2016-09-29 5:57 ` Hannes Reinecke
0 siblings, 2 replies; 12+ messages in thread
From: Darrick J. Wong @ 2016-09-29 0:39 UTC (permalink / raw)
To: axboe, akpm, darrick.wong
Cc: linux-block, tytso, martin.petersen, snitzer, linux-api, bfoster,
xfs, hch, dm-devel, linux-fsdevel, bart.vanassche
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.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
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.
v3: Forward port to 4.7.
v4: Forward port to 4.8.
---
fs/block_dev.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/open.c | 3 +-
2 files changed, 80 insertions(+), 1 deletion(-)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 08ae993..0c808fc 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -30,6 +30,7 @@
#include <linux/cleancache.h>
#include <linux/dax.h>
#include <linux/badblocks.h>
+#include <linux/falloc.h>
#include <asm/uaccess.h>
#include "internal.h"
@@ -1787,6 +1788,82 @@ static const struct address_space_operations def_blk_aops = {
.is_dirty_writeback = buffer_check_dirty_writeback,
};
+#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.
+ */
+ 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:
+ 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,
@@ -1801,6 +1878,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 4fd6e25..01b6092 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 */
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
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
1 sibling, 1 reply; 12+ messages in thread
From: Bart Van Assche @ 2016-09-29 1:42 UTC (permalink / raw)
To: Darrick J. Wong, axboe, akpm
Cc: linux-block, tytso, martin.petersen, snitzer, linux-api, bfoster,
xfs, hch, dm-devel, linux-fsdevel
On 09/28/16 17:39, Darrick J. Wong wrote:
> + if (end > isize) {
> + if (mode & FALLOC_FL_KEEP_SIZE) {
> + len = isize - start;
> + end = start + len - 1;
> + } else
> + return -EINVAL;
> + }
If FALLOC_FL_KEEP_SIZE has been set and end == isize the above code
won't reduce end to isize - 1. Shouldn't "end > isize" be changed into
"end >= isize" ?
> + 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:
> + error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
> + GFP_KERNEL, 0);
> + if (error)
> + return error;
> + break;
> + default:
> + return -EOPNOTSUPP;
> + }
Have you considered to move "if (error) return error" out of the switch
statement?
> + /*
> + * 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);
A comment might be appropriate here that since end is inclusive and
since the third argument of invalidate_inode_pages2_range() is inclusive
that rounding down will yield the correct result.
Bart.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
2016-09-29 1:42 ` Bart Van Assche
@ 2016-09-29 2:09 ` Darrick J. Wong
0 siblings, 0 replies; 12+ messages in thread
From: Darrick J. Wong @ 2016-09-29 2:09 UTC (permalink / raw)
To: Bart Van Assche
Cc: axboe, hch, tytso, martin.petersen, snitzer, linux-api, bfoster,
xfs, linux-block, dm-devel, linux-fsdevel, akpm
On Wed, Sep 28, 2016 at 06:42:14PM -0700, Bart Van Assche wrote:
> On 09/28/16 17:39, Darrick J. Wong wrote:
> >+ if (end > isize) {
> >+ if (mode & FALLOC_FL_KEEP_SIZE) {
> >+ len = isize - start;
> >+ end = start + len - 1;
> >+ } else
> >+ return -EINVAL;
> >+ }
>
> If FALLOC_FL_KEEP_SIZE has been set and end == isize the above code won't
> reduce end to isize - 1. Shouldn't "end > isize" be changed into "end >=
> isize" ?
Oops. Will fix and send out a v2.
> >+ 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:
> >+ error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
> >+ GFP_KERNEL, 0);
> >+ if (error)
> >+ return error;
> >+ break;
> >+ default:
> >+ return -EOPNOTSUPP;
> >+ }
>
> Have you considered to move "if (error) return error" out of the switch
> statement?
Sure, I could do that.
> >+ /*
> >+ * 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);
>
> A comment might be appropriate here that since end is inclusive and since
> the third argument of invalidate_inode_pages2_range() is inclusive that
> rounding down will yield the correct result.
/methot the documentation of invalidate_inode_pages2_range was clear
enough on that point, but I could throw that into the comment too.
--D
>
> Bart.
>
_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 3/3] block: implement (some of) fallocate for block devices
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 5:57 ` Hannes Reinecke
1 sibling, 0 replies; 12+ messages in thread
From: Hannes Reinecke @ 2016-09-29 5:57 UTC (permalink / raw)
To: Darrick J. Wong, axboe, akpm
Cc: linux-block, tytso, martin.petersen, snitzer, linux-api, bfoster,
xfs, hch, dm-devel, linux-fsdevel, bart.vanassche
On 09/29/2016 02:39 AM, Darrick J. Wong wrote:
> 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.
>
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> ---
> 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.
> v3: Forward port to 4.7.
> v4: Forward port to 4.8.
> ---
> fs/block_dev.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> fs/open.c | 3 +-
> 2 files changed, 80 insertions(+), 1 deletion(-)
>
Reviewed-by: Hannes Reinecke <hare@suse.com>
Cheers,
Hannes
--
Dr. Hannes Reinecke zSeries & Storage
hare@suse.de +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 3/3] block: implement (some of) fallocate for block devices
2016-09-29 21:16 [PATCH v11 0/3] " Darrick J. Wong
@ 2016-09-29 21:16 ` Darrick J. Wong
0 siblings, 0 replies; 12+ messages in thread
From: Darrick J. Wong @ 2016-09-29 21:16 UTC (permalink / raw)
To: axboe, akpm, darrick.wong
Cc: hch, Hannes Reinecke, tytso, martin.petersen, snitzer, linux-api,
bfoster, xfs, linux-block, dm-devel, hare, linux-fsdevel,
bart.vanassche
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.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Hannes Reinecke <hare@suse.com>
Reviewed-by: Bart Van Assche <bart.vanassche@sandisk.com>
---
fs/block_dev.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/open.c | 3 +-
2 files changed, 79 insertions(+), 1 deletion(-)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 08ae993..777fd9b 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -30,6 +30,7 @@
#include <linux/cleancache.h>
#include <linux/dax.h>
#include <linux/badblocks.h>
+#include <linux/falloc.h>
#include <asm/uaccess.h>
#include "internal.h"
@@ -1787,6 +1788,81 @@ static const struct address_space_operations def_blk_aops = {
.is_dirty_writeback = buffer_check_dirty_writeback,
};
+#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.
+ */
+ 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);
+ 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);
+ break;
+ case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
+ if (!blk_queue_discard(q))
+ return -EOPNOTSUPP;
+ error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
+ GFP_KERNEL, 0);
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ if (error)
+ return error;
+
+ /*
+ * Invalidate again; if someone wandered in and dirtied a page,
+ * the caller will be given -EBUSY. The third argument is
+ * inclusive, so the rounding here is safe.
+ */
+ 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,
@@ -1801,6 +1877,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 4fd6e25..01b6092 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
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2016-09-29 21:16 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20160315194221.30093.70506.stgit@birch.djwong.org>
[not found] ` <20160315194244.30093.6483.stgit@birch.djwong.org>
2016-03-21 18:52 ` [PATCH 3/3] block: implement (some of) fallocate for block devices Mike Snitzer
2016-03-21 19:11 ` Darrick J. Wong
2016-03-21 19:22 ` Mike Snitzer
[not found] ` <20160321192229.GA17220-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2016-03-21 20:59 ` Brian Foster
2016-04-13 4:01 [RFC DONOTMERGE v8 0/3] " Darrick J. Wong
[not found] ` <20160413040121.10562.98998.stgit-PTl6brltDGh4DFYR7WNSRA@public.gmane.org>
2016-04-13 4:01 ` [PATCH 3/3] block: implement (some of) " Darrick J. Wong
-- strict thread matches above, loose matches on Subject: below --
2016-06-17 1:17 [PATCH v9 0/3] " Darrick J. Wong
[not found] ` <146612624734.12764.4316680863289411106.stgit-PTl6brltDGh4DFYR7WNSRA@public.gmane.org>
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
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).