* [PATCH RESEND 0/1] loop: introduce LO_FLAGS_NODEALLOC
@ 2022-08-06 11:49 Zhang Boyang
2022-08-06 11:49 ` [PATCH RESEND 1/1] " Zhang Boyang
0 siblings, 1 reply; 3+ messages in thread
From: Zhang Boyang @ 2022-08-06 11:49 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, linux-block
Cc: Tetsuo Handa, Jan Kara, Ming Lei, Darrick J . Wong,
Chaitanya Kulkarni, Damien Le Moal, Martin K . Petersen,
linux-kernel
Hi,
This patch gives userspace ability to prevent underlying file of loop
device to be sparse. Currently, if loop device is 'trimmed'
(BLKDISCARD), then underlying file will always become sparse and disk
space freed. This behaviour is good for thin provisioning but not good
for preallocated disk images. This patch introduces LO_FLAGS_NODEALLOC
flag, which will write zeroes to underlying file instead of punching
holes when BLKDISCARD is requested. Thus reducing file fragmentation of
preallocated disk images and improve performance.
I will also submit patches to util-linux to provide userspace support if
this patch is merged.
Best Regards,
Zhang Boyang
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH RESEND 1/1] loop: introduce LO_FLAGS_NODEALLOC
2022-08-06 11:49 [PATCH RESEND 0/1] loop: introduce LO_FLAGS_NODEALLOC Zhang Boyang
@ 2022-08-06 11:49 ` Zhang Boyang
2022-08-06 14:51 ` Jens Axboe
0 siblings, 1 reply; 3+ messages in thread
From: Zhang Boyang @ 2022-08-06 11:49 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, linux-block
Cc: Tetsuo Handa, Jan Kara, Ming Lei, Darrick J . Wong,
Chaitanya Kulkarni, Damien Le Moal, Martin K . Petersen,
linux-kernel, Zhang Boyang
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_NODEALLOC. 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.
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..a52ef0787202 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_NODEALLOC)) ?
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_NODEALLOC) ?
+ 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_nodealloc_show(struct loop_device *lo, char *buf)
+{
+ int nodealloc = (lo->lo_flags & LO_FLAGS_NODEALLOC);
+
+ return sysfs_emit(buf, "%s\n", nodealloc ? "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(nodealloc);
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_nodealloc.attr,
NULL,
};
diff --git a/include/uapi/linux/loop.h b/include/uapi/linux/loop.h
index 6f63527dd2ed..a6afd06ffa07 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_NODEALLOC = 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_NODEALLOC)
/* 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_NODEALLOC)
/* 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_NODEALLOC)
#include <asm/posix_types.h> /* for __kernel_old_dev_t */
#include <linux/types.h> /* for __u64 */
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH RESEND 1/1] loop: introduce LO_FLAGS_NODEALLOC
2022-08-06 11:49 ` [PATCH RESEND 1/1] " Zhang Boyang
@ 2022-08-06 14:51 ` Jens Axboe
0 siblings, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2022-08-06 14:51 UTC (permalink / raw)
To: Zhang Boyang, Christoph Hellwig, linux-block
Cc: Tetsuo Handa, Jan Kara, Ming Lei, Darrick J . Wong,
Chaitanya Kulkarni, Damien Le Moal, Martin K . Petersen,
linux-kernel
On 8/6/22 5:49 AM, 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_NODEALLOC. 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.
Just in terms of readability, I would prefer if NO_DEALLOC is used
consistently rather than NODEALLOC. The latter reads more like
node-alloc to me.
--
Jens Axboe
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-08-06 14:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-06 11:49 [PATCH RESEND 0/1] loop: introduce LO_FLAGS_NODEALLOC Zhang Boyang
2022-08-06 11:49 ` [PATCH RESEND 1/1] " Zhang Boyang
2022-08-06 14:51 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox