Skip to contentSkip to conversation

loop: introduce LO_FLAGS_NODEALLOC

PATCH RESEND In review Zhang Boyang 2 participants 3 messages Aug 6, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

cover loop: introduce LO_FLAGS_NODEALLOC

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

#1 lore raw

PATCH RESEND 1/1 loop: introduce LO_FLAGS_NODEALLOC

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.

---
 drivers/block/loop.c      | 17 +++++++++++++++--
 include/uapi/linux/loop.h | 15 +++++++++++----
 2 files changed, 26 insertions(+), 6 deletions(-)
drivers/block/loop.c +15 -2
index 084f9b8a0ba3..a52ef0787202 100644
@@ -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,
 };
 
include/uapi/linux/loop.h +11 -4
index 6f63527dd2ed..a6afd06ffa07 100644
@@ -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 */
Signature · 1 line
2.30.2
maintainer
#2 lore raw
Quoted 11 lines · 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.

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.

Signature · 1 line
Jens Axboe