The parameter max_hw_wzeroes_unmap_sectors in queue_limits should be
equal to max_write_zeroes_sectors if it is set to a non-zero value.
However, the stacked md drivers call md_init_stacking_limits() to
initialize this parameter to UINT_MAX but only adjust
max_write_zeroes_sectors when setting limits. Therefore, this
discrepancy triggers a value check failure in blk_validate_limits().
 $ modprobe scsi_debug num_parts=2 dev_size_mb=8 lbprz=1 lbpws=1
 $ mdadm --create /dev/md0 --level=0 --raid-device=2 /dev/sda1 /dev/sda2
   mdadm: Defaulting to version 1.2 metadata
   mdadm: RUN_ARRAY failed: Invalid argument
Fix this failure by explicitly setting max_hw_wzeroes_unmap_sectors to
max_write_zeroes_sectors. Since the linear and raid0 drivers support
write zeroes, so they can support unmap write zeroes operation if all of
the backend devices support it. However, the raid1/10/5 drivers don't
support write zeroes, so we have to set it to zero.
Fixes: 0c40d7cb5ef3 ("block: introduce max_{hw|user}_wzeroes_unmap_sectors to queue limits")
Reported-by: John Garry <john.g.garry@oracle.com>
Closes: https://lore.kernel.org/linux-block/803a2183-a0bb-4b7a-92f1-afc5097630d2@oracle.com/
Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Tested-by: John Garry <john.g.garry@oracle.com>
Reviewed-by: Li Nan <linan122@huawei.com>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Link: https://lore.kernel.org/linux-raid/20250910111107.3247530-2-yi.zhang@huaweicloud.com
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
        md_init_stacking_limits(&lim);
        lim.max_hw_sectors = mddev->chunk_sectors;
        lim.max_write_zeroes_sectors = mddev->chunk_sectors;
+       lim.max_hw_wzeroes_unmap_sectors = mddev->chunk_sectors;
        lim.io_min = mddev->chunk_sectors << 9;
        err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY);
        if (err)
 
        md_init_stacking_limits(&lim);
        lim.max_hw_sectors = mddev->chunk_sectors;
        lim.max_write_zeroes_sectors = mddev->chunk_sectors;
+       lim.max_hw_wzeroes_unmap_sectors = mddev->chunk_sectors;
        lim.io_min = mddev->chunk_sectors << 9;
        lim.io_opt = lim.io_min * mddev->raid_disks;
        lim.chunk_sectors = mddev->chunk_sectors;
 
 
        md_init_stacking_limits(&lim);
        lim.max_write_zeroes_sectors = 0;
+       lim.max_hw_wzeroes_unmap_sectors = 0;
        lim.features |= BLK_FEAT_ATOMIC_WRITES;
        err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY);
        if (err)
 
 
        md_init_stacking_limits(&lim);
        lim.max_write_zeroes_sectors = 0;
+       lim.max_hw_wzeroes_unmap_sectors = 0;
        lim.io_min = mddev->chunk_sectors << 9;
        lim.chunk_sectors = mddev->chunk_sectors;
        lim.io_opt = lim.io_min * raid10_nr_stripes(conf);
 
        lim.features |= BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE;
        lim.discard_granularity = stripe;
        lim.max_write_zeroes_sectors = 0;
+       lim.max_hw_wzeroes_unmap_sectors = 0;
        mddev_stack_rdev_limits(mddev, &lim, 0);
        rdev_for_each(rdev, mddev)
                queue_limits_stack_bdev(&lim, rdev->bdev, rdev->new_data_offset,