Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <w@uter.be>
Cc: Eric Blake <eblake@redhat.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Link: https://lore.kernel.org/r/20240812133032.115134-3-w@uter.be
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Wouter Verhelst and committed by
Jens Axboe
e49dacc7 f55d3b82

+12 -1
+8
drivers/block/nbd.c
··· 363 363 } 364 364 if (nbd->config->flags & NBD_FLAG_ROTATIONAL) 365 365 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; 366 368 367 369 lim.logical_block_size = blksize; 368 370 lim.physical_block_size = blksize; ··· 434 432 return NBD_CMD_WRITE; 435 433 case REQ_OP_READ: 436 434 return NBD_CMD_READ; 435 + case REQ_OP_WRITE_ZEROES: 436 + return NBD_CMD_WRITE_ZEROES; 437 437 default: 438 438 return U32_MAX; 439 439 } ··· 652 648 653 649 if (req->cmd_flags & REQ_FUA) 654 650 nbd_cmd_flags |= NBD_CMD_FLAG_FUA; 651 + if ((req->cmd_flags & REQ_NOUNMAP) && (type == NBD_CMD_WRITE_ZEROES)) 652 + nbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE; 655 653 656 654 /* We did a partial send previously, and we at least sent the whole 657 655 * request struct, so just go and send the rest of the pages in the ··· 1723 1717 seq_puts(s, "NBD_FLAG_SEND_FUA\n"); 1724 1718 if (flags & NBD_FLAG_SEND_TRIM) 1725 1719 seq_puts(s, "NBD_FLAG_SEND_TRIM\n"); 1720 + if (flags & NBD_FLAG_SEND_WRITE_ZEROES) 1721 + seq_puts(s, "NBD_FLAG_SEND_WRITE_ZEROES\n"); 1726 1722 1727 1723 return 0; 1728 1724 }
+4 -1
include/uapi/linux/nbd.h
··· 42 42 NBD_CMD_WRITE = 1, 43 43 NBD_CMD_DISC = 2, 44 44 NBD_CMD_FLUSH = 3, 45 - NBD_CMD_TRIM = 4 45 + NBD_CMD_TRIM = 4, 46 46 /* userspace defines additional extension commands */ 47 + NBD_CMD_WRITE_ZEROES = 6, 47 48 }; 48 49 49 50 /* values for flags field, these are server interaction specific. */ ··· 54 53 #define NBD_FLAG_SEND_FUA (1 << 3) /* send FUA (forced unit access) */ 55 54 #define NBD_FLAG_ROTATIONAL (1 << 4) /* device is rotational */ 56 55 #define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */ 56 + #define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6) /* supports WRITE_ZEROES */ 57 57 /* there is a gap here to match userspace */ 58 58 #define NBD_FLAG_CAN_MULTI_CONN (1 << 8) /* Server supports multiple connections per export. */ 59 59 60 60 /* values for cmd flags in the upper 16 bits of request type */ 61 61 #define NBD_CMD_FLAG_FUA (1 << 16) /* FUA (forced unit access) op */ 62 + #define NBD_CMD_FLAG_NO_HOLE (1 << 17) /* Do not punch a hole for WRITE_ZEROES */ 62 63 63 64 /* These are client behavior specific flags. */ 64 65 #define NBD_CFLAG_DESTROY_ON_DISCONNECT (1 << 0) /* delete the nbd device on