Skip to content

Commit e49dacc

Browse files
yoeaxboe
authored andcommitted
nbd: implement the WRITE_ZEROES command
The NBD protocol defines a message for zeroing out a region of an export Add support to the kernel driver for that message. Signed-off-by: Wouter Verhelst <[email protected]> Cc: Eric Blake <[email protected]> Reviewed-by: Damien Le Moal <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent f55d3b8 commit e49dacc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

drivers/block/nbd.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
363363
}
364364
if (nbd->config->flags & NBD_FLAG_ROTATIONAL)
365365
lim.features |= BLK_FEAT_ROTATIONAL;
366+
if (nbd->config->flags & NBD_FLAG_SEND_WRITE_ZEROES)
367+
lim.max_write_zeroes_sectors = UINT_MAX >> SECTOR_SHIFT;
366368

367369
lim.logical_block_size = blksize;
368370
lim.physical_block_size = blksize;
@@ -432,6 +434,8 @@ static u32 req_to_nbd_cmd_type(struct request *req)
432434
return NBD_CMD_WRITE;
433435
case REQ_OP_READ:
434436
return NBD_CMD_READ;
437+
case REQ_OP_WRITE_ZEROES:
438+
return NBD_CMD_WRITE_ZEROES;
435439
default:
436440
return U32_MAX;
437441
}
@@ -648,6 +652,8 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
648652

649653
if (req->cmd_flags & REQ_FUA)
650654
nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
655+
if ((req->cmd_flags & REQ_NOUNMAP) && (type == NBD_CMD_WRITE_ZEROES))
656+
nbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE;
651657

652658
/* We did a partial send previously, and we at least sent the whole
653659
* request struct, so just go and send the rest of the pages in the
@@ -1717,6 +1723,8 @@ static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
17171723
seq_puts(s, "NBD_FLAG_SEND_FUA\n");
17181724
if (flags & NBD_FLAG_SEND_TRIM)
17191725
seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1726+
if (flags & NBD_FLAG_SEND_WRITE_ZEROES)
1727+
seq_puts(s, "NBD_FLAG_SEND_WRITE_ZEROES\n");
17201728

17211729
return 0;
17221730
}

include/uapi/linux/nbd.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ enum {
4242
NBD_CMD_WRITE = 1,
4343
NBD_CMD_DISC = 2,
4444
NBD_CMD_FLUSH = 3,
45-
NBD_CMD_TRIM = 4
45+
NBD_CMD_TRIM = 4,
4646
/* userspace defines additional extension commands */
47+
NBD_CMD_WRITE_ZEROES = 6,
4748
};
4849

4950
/* values for flags field, these are server interaction specific. */
@@ -53,11 +54,13 @@ enum {
5354
#define NBD_FLAG_SEND_FUA (1 << 3) /* send FUA (forced unit access) */
5455
#define NBD_FLAG_ROTATIONAL (1 << 4) /* device is rotational */
5556
#define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */
57+
#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6) /* supports WRITE_ZEROES */
5658
/* there is a gap here to match userspace */
5759
#define NBD_FLAG_CAN_MULTI_CONN (1 << 8) /* Server supports multiple connections per export. */
5860

5961
/* values for cmd flags in the upper 16 bits of request type */
6062
#define NBD_CMD_FLAG_FUA (1 << 16) /* FUA (forced unit access) op */
63+
#define NBD_CMD_FLAG_NO_HOLE (1 << 17) /* Do not punch a hole for WRITE_ZEROES */
6164

6265
/* These are client behavior specific flags. */
6366
#define NBD_CFLAG_DESTROY_ON_DISCONNECT (1 << 0) /* delete the nbd device on

0 commit comments

Comments
 (0)