lkml.org 
[lkml]   [2022]   [Aug]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: [PATCH V2 1/1] loop: introduce LO_FLAGS_NO_DEALLOC
On Sat, Aug 06, 2022 at 11:30:22PM +0800, Zhang Boyang wrote:
> Previously, for file-backed loop devices, REQ_OP_DISCARD and
> REQ_OP_WRITE_ZEROES (without REQ_NOUNMAP) are implemented using
> fallocate(FALLOC_FL_PUNCH_HOLE), which will cause the underlying file to
> be sparse and disk space freed. The users have no choice to prevent this
> this from happening.
>
> This patch introduces LO_FLAGS_NO_DEALLOC. With this flag set,
> REQ_OP_DISCARD and REQ_OP_WRITE_ZEROES are forced to use
> fallocate(FALLOC_FL_ZERO_RANGE). The disk space of underlying file is
> kept allocated. This is useful if users, for example, want to use a
> preallocated file as the backing file.

Considering that discard isn't required to do anything, why not
echo 0 | sudo tee /sys/block/loopX/queue/discard_max_bytes ?

--D

> Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
> ---
> drivers/block/loop.c | 17 +++++++++++++++--
> include/uapi/linux/loop.h | 15 +++++++++++----
> 2 files changed, 26 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index 084f9b8a0ba3..36bd9906a154 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -483,11 +483,15 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
> * write zeroes the range. Otherwise, punch them out.
> */
> return lo_fallocate(lo, rq, pos,
> - (rq->cmd_flags & REQ_NOUNMAP) ?
> + ((rq->cmd_flags & REQ_NOUNMAP) ||
> + (lo->lo_flags & LO_FLAGS_NO_DEALLOC)) ?
> FALLOC_FL_ZERO_RANGE :
> FALLOC_FL_PUNCH_HOLE);
> case REQ_OP_DISCARD:
> - return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
> + return lo_fallocate(lo, rq, pos,
> + (lo->lo_flags & LO_FLAGS_NO_DEALLOC) ?
> + FALLOC_FL_ZERO_RANGE :
> + FALLOC_FL_PUNCH_HOLE);
> case REQ_OP_WRITE:
> if (cmd->use_aio)
> return lo_rw_aio(lo, cmd, pos, WRITE);
> @@ -719,12 +723,20 @@ static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
> return sysfs_emit(buf, "%s\n", dio ? "1" : "0");
> }
>
> +static ssize_t loop_attr_no_dealloc_show(struct loop_device *lo, char *buf)
> +{
> + int no_dealloc = (lo->lo_flags & LO_FLAGS_NO_DEALLOC);
> +
> + return sysfs_emit(buf, "%s\n", no_dealloc ? "1" : "0");
> +}
> +
> LOOP_ATTR_RO(backing_file);
> LOOP_ATTR_RO(offset);
> LOOP_ATTR_RO(sizelimit);
> LOOP_ATTR_RO(autoclear);
> LOOP_ATTR_RO(partscan);
> LOOP_ATTR_RO(dio);
> +LOOP_ATTR_RO(no_dealloc);
>
> static struct attribute *loop_attrs[] = {
> &loop_attr_backing_file.attr,
> @@ -733,6 +745,7 @@ static struct attribute *loop_attrs[] = {
> &loop_attr_autoclear.attr,
> &loop_attr_partscan.attr,
> &loop_attr_dio.attr,
> + &loop_attr_no_dealloc.attr,
> NULL,
> };
>
> diff --git a/include/uapi/linux/loop.h b/include/uapi/linux/loop.h
> index 6f63527dd2ed..91a0a8b1f298 100644
> --- a/include/uapi/linux/loop.h
> +++ b/include/uapi/linux/loop.h
> @@ -18,17 +18,24 @@ enum {
> LO_FLAGS_AUTOCLEAR = 4,
> LO_FLAGS_PARTSCAN = 8,
> LO_FLAGS_DIRECT_IO = 16,
> + LO_FLAGS_NO_DEALLOC = 32,
> };
>
> /* LO_FLAGS that can be set using LOOP_SET_STATUS(64) */
> -#define LOOP_SET_STATUS_SETTABLE_FLAGS (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
> +#define LOOP_SET_STATUS_SETTABLE_FLAGS (LO_FLAGS_AUTOCLEAR \
> + | LO_FLAGS_PARTSCAN \
> + | LO_FLAGS_NO_DEALLOC)
>
> /* LO_FLAGS that can be cleared using LOOP_SET_STATUS(64) */
> -#define LOOP_SET_STATUS_CLEARABLE_FLAGS (LO_FLAGS_AUTOCLEAR)
> +#define LOOP_SET_STATUS_CLEARABLE_FLAGS (LO_FLAGS_AUTOCLEAR \
> + | LO_FLAGS_NO_DEALLOC)
>
> /* LO_FLAGS that can be set using LOOP_CONFIGURE */
> -#define LOOP_CONFIGURE_SETTABLE_FLAGS (LO_FLAGS_READ_ONLY | LO_FLAGS_AUTOCLEAR \
> - | LO_FLAGS_PARTSCAN | LO_FLAGS_DIRECT_IO)
> +#define LOOP_CONFIGURE_SETTABLE_FLAGS (LO_FLAGS_READ_ONLY \
> + | LO_FLAGS_AUTOCLEAR \
> + | LO_FLAGS_PARTSCAN \
> + | LO_FLAGS_DIRECT_IO \
> + | LO_FLAGS_NO_DEALLOC)
>
> #include <asm/posix_types.h> /* for __kernel_old_dev_t */
> #include <linux/types.h> /* for __u64 */
> --
> 2.30.2
>

\
 
 \ /
  Last update: 2022-08-10 00:20    [W:0.061 / U:0.288 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site