[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220806153022.83748-2-zhangboyang.id@gmail.com>
Date: Sat, 6 Aug 2022 23:30:22 +0800
From: Zhang Boyang <zhangboyang.id@...il.com>
To: Jens Axboe <axboe@...nel.dk>, Christoph Hellwig <hch@....de>,
linux-block@...r.kernel.org
Cc: Tetsuo Handa <penguin-kernel@...ove.SAKURA.ne.jp>,
Jan Kara <jack@...e.cz>, Ming Lei <ming.lei@...hat.com>,
"Darrick J . Wong" <djwong@...nel.org>,
Chaitanya Kulkarni <kch@...dia.com>,
Damien Le Moal <damien.lemoal@...nsource.wdc.com>,
"Martin K . Petersen" <martin.petersen@...cle.com>,
linux-kernel@...r.kernel.org,
Zhang Boyang <zhangboyang.id@...il.com>
Subject: [PATCH V2 1/1] loop: introduce LO_FLAGS_NO_DEALLOC
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.
Signed-off-by: Zhang Boyang <zhangboyang.id@...il.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
Powered by blists - more mailing lists