Re: [PATCH v4 03/10] block: Introduce a new ioctl for copy

From: Damien Le Moal
Date: Tue Apr 26 2022 - 22:49:19 EST


On 4/26/22 19:12, Nitesh Shetty wrote:
> Add new BLKCOPY ioctl that offloads copying of one or more sources ranges
> to one or more destination in a device. COPY ioctl accepts a 'copy_range'
> structure that contains no of range, a reserved field , followed by an
> array of ranges. Each source range is represented by 'range_entry' that
> contains source start offset, destination start offset and length of
> source ranges (in bytes)
>
> MAX_COPY_NR_RANGE, limits the number of entries for the IOCTL and
> MAX_COPY_TOTAL_LENGTH limits the total copy length, IOCTL can handle.
>
> Example code, to issue BLKCOPY:
> /* Sample example to copy three entries with [dest,src,len],
> * [32768, 0, 4096] [36864, 4096, 4096] [40960,8192,4096] on same device */
>
> int main(void)
> {
> int i, ret, fd;
> unsigned long src = 0, dst = 32768, len = 4096;
> struct copy_range *cr;
> cr = (struct copy_range *)malloc(sizeof(*cr)+
> (sizeof(struct range_entry)*3));
> cr->nr_range = 3;
> cr->reserved = 0;
> for (i = 0; i< cr->nr_range; i++, src += len, dst += len) {
> cr->range_list[i].dst = dst;
> cr->range_list[i].src = src;
> cr->range_list[i].len = len;
> cr->range_list[i].comp_len = 0;
> }
> fd = open("/dev/nvme0n1", O_RDWR);
> if (fd < 0) return 1;
> ret = ioctl(fd, BLKCOPY, cr);
> if (ret != 0)
> printf("copy failed, ret= %d\n", ret);
> for (i=0; i< cr->nr_range; i++)
> if (cr->range_list[i].len != cr->range_list[i].comp_len)
> printf("Partial copy for entry %d: requested %llu, completed %llu\n",
> i, cr->range_list[i].len,
> cr->range_list[i].comp_len);
> close(fd);
> free(cr);
> return ret;
> }

Nice to have a code example. But please format it correctly.

>
> Signed-off-by: Nitesh Shetty <nj.shetty@xxxxxxxxxxx>
> Signed-off-by: Javier González <javier.gonz@xxxxxxxxxxx>
> Signed-off-by: Arnav Dawn <arnav.dawn@xxxxxxxxxxx>
> ---
> block/ioctl.c | 32 ++++++++++++++++++++++++++++++++
> include/uapi/linux/fs.h | 9 +++++++++
> 2 files changed, 41 insertions(+)
>
> diff --git a/block/ioctl.c b/block/ioctl.c
> index 46949f1b0dba..58d93c20ff30 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -120,6 +120,36 @@ static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
> return err;
> }
>
> +static int blk_ioctl_copy(struct block_device *bdev, fmode_t mode,
> + unsigned long arg)
> +{
> + struct copy_range crange, *ranges = NULL;
> + size_t payload_size = 0;
> + int ret;
> +
> + if (!(mode & FMODE_WRITE))
> + return -EBADF;
> +
> + if (copy_from_user(&crange, (void __user *)arg, sizeof(crange)))
> + return -EFAULT;
> +
> + if (unlikely(!crange.nr_range || crange.reserved || crange.nr_range >= MAX_COPY_NR_RANGE))
> + return -EINVAL;
> +
> + payload_size = (crange.nr_range * sizeof(struct range_entry)) + sizeof(crange);
> +
> + ranges = memdup_user((void __user *)arg, payload_size);
> + if (IS_ERR(ranges))
> + return PTR_ERR(ranges);
> +
> + ret = blkdev_issue_copy(bdev, ranges->nr_range, ranges->range_list, bdev, GFP_KERNEL);
> + if (copy_to_user((void __user *)arg, ranges, payload_size))
> + ret = -EFAULT;
> +
> + kfree(ranges);
> + return ret;
> +}
> +
> static int blk_ioctl_secure_erase(struct block_device *bdev, fmode_t mode,
> void __user *argp)
> {
> @@ -481,6 +511,8 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
> return blk_ioctl_discard(bdev, mode, arg);
> case BLKSECDISCARD:
> return blk_ioctl_secure_erase(bdev, mode, argp);
> + case BLKCOPY:
> + return blk_ioctl_copy(bdev, mode, arg);
> case BLKZEROOUT:
> return blk_ioctl_zeroout(bdev, mode, arg);
> case BLKGETDISKSEQ:
> diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
> index 822c28cebf3a..a3b13406ffb8 100644
> --- a/include/uapi/linux/fs.h
> +++ b/include/uapi/linux/fs.h
> @@ -78,6 +78,14 @@ struct range_entry {
> __u64 comp_len;
> };
>
> +struct copy_range {
> + __u64 nr_range;
> + __u64 reserved;
> +
> + /* Range_list always must be at the end */
> + struct range_entry range_list[];
> +};
> +
> /* extent-same (dedupe) ioctls; these MUST match the btrfs ioctl definitions */
> #define FILE_DEDUPE_RANGE_SAME 0
> #define FILE_DEDUPE_RANGE_DIFFERS 1
> @@ -199,6 +207,7 @@ struct fsxattr {
> #define BLKROTATIONAL _IO(0x12,126)
> #define BLKZEROOUT _IO(0x12,127)
> #define BLKGETDISKSEQ _IOR(0x12,128,__u64)
> +#define BLKCOPY _IOWR(0x12, 129, struct copy_range)
> /*
> * A jump here: 130-136 are reserved for zoned block devices
> * (see uapi/linux/blkzoned.h)


--
Damien Le Moal
Western Digital Research