All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rjones@redhat.com>
To: miklos@szeredi.hu
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	eblake@redhat.com, libguestfs@redhat.com
Subject: [PATCH v2] fuse: Allow fallocate(FALLOC_FL_ZERO_RANGE)
Date: Wed, 12 May 2021 11:37:04 +0100	[thread overview]
Message-ID: <20210512103704.3505086-2-rjones@redhat.com> (raw)
In-Reply-To: <20210512103704.3505086-1-rjones@redhat.com>

libnbd's nbdfuse utility would like to translate fallocate zero
requests into NBD_CMD_WRITE_ZEROES.  Currently the fuse module filters
these out, returning -EOPNOTSUPP.  This commit treats these almost the
same way as FALLOC_FL_PUNCH_HOLE except not calling
truncate_pagecache_range.

A way to test this is with the following script:

--------------------
  #!/bin/bash
  # Requires fuse >= 3, nbdkit >= 1.8, and latest nbdfuse from
  # https://gitlab.com/nbdkit/libnbd/-/tree/master/fuse
  set -e
  set -x

  export output=$PWD/output
  rm -f test.img $output

  # Create an nbdkit instance that prints the NBD requests seen.
  nbdkit sh - <<'EOF'
  case "$1" in
    get_size) echo 1M ;;
    can_write|can_trim|can_zero|can_fast_zero) ;;
    pread) echo "$@" >>$output; dd if=/dev/zero count=$3 iflag=count_bytes ;;
    pwrite) echo "$@" >>$output; cat >/dev/null ;;
    trim|zero) echo "$@" >>$output ;;
    *) exit 2 ;;
  esac
  EOF

  # Fuse-mount NBD instance as a file.
  touch test.img
  nbdfuse test.img nbd://localhost & sleep 2
  ls -lh test.img

  # Run a read, write, trim and zero request.
  dd if=test.img of=/dev/null bs=512 skip=1024 count=1
  dd if=/dev/zero of=test.img bs=512 skip=2048 count=1
  fallocate -p -l 512 -o 4096 test.img
  fallocate -z -l 512 -o 8192 test.img

  # Print the output from the NBD server.
  cat $output

  # Clean up.
  fusermount3 -u test.img
  killall nbdkit
  rm test.img $output
  --------------------

which will print:

  pread  4096 524288    # number depends on readahead
  pwrite  512 0
  trim  512 4096
  zero  512 8192 may_trim

The last line indicates that the FALLOC_FL_ZERO_RANGE request was
successfully passed through by the kernel module to nbdfuse,
translated to NBD_CMD_WRITE_ZEROES and sent through to the server.

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 fs/fuse/file.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 09ef2a4d25ed..22e8e88c78d4 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -2907,11 +2907,13 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
 	};
 	int err;
 	bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
-			   (mode & FALLOC_FL_PUNCH_HOLE);
+			   (mode & FALLOC_FL_PUNCH_HOLE) ||
+			   (mode & FALLOC_FL_ZERO_RANGE);
 
 	bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
 
-	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
+	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
+		     FALLOC_FL_ZERO_RANGE))
 		return -EOPNOTSUPP;
 
 	if (fm->fc->no_fallocate)
@@ -2926,7 +2928,8 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
 				goto out;
 		}
 
-		if (mode & FALLOC_FL_PUNCH_HOLE) {
+		if ((mode & FALLOC_FL_PUNCH_HOLE) ||
+		    (mode & FALLOC_FL_ZERO_RANGE)) {
 			loff_t endbyte = offset + length - 1;
 
 			err = fuse_writeback_range(inode, offset, endbyte);
-- 
2.31.1


  reply	other threads:[~2021-05-12 10:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 10:37 [PATCH v2] fuse: Allow fallocate(FALLOC_FL_ZERO_RANGE) Richard W.M. Jones
2021-05-12 10:37 ` Richard W.M. Jones [this message]
2021-05-12 14:27   ` Shachar Sharon
2021-05-12 14:49     ` Richard W.M. Jones

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=20210512103704.3505086-2-rjones@redhat.com \
    --to=rjones@redhat.com \
    --cc=eblake@redhat.com \
    --cc=libguestfs@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.