Commit 4cbec7e8 authored by Mike Christie's avatar Mike Christie Committed by Martin K. Petersen
Browse files

scsi: target: Fix unmap setup during configuration



This issue was found and also debugged by Carl Lei <me@xecycle.info>.

If the device is not enabled, iblock/file will have not setup their
se_device to bdev/file mappings. If a user tries to config the unmap
settings at this time, we will then crash trying to access a NULL pointer
where the bdev/file should be.

This patch adds a check to make sure the device is configured before
we try to call the configure_unmap callout.

Fixes: 34bd1dca ("scsi: target: Detect UNMAP support post configuration")
Reported-by: default avatarCarl Lei <me@xecycle.info>
Signed-off-by: default avatarMike Christie <michael.christie@oracle.com>
Link: https://lore.kernel.org/r/20240209215247.5213-1-michael.christie@oracle.com


Reviewed-by: default avatarMaurizio Lombardi <mlombard@redhat.com>
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
parent 17e94b25
Loading
Loading
Loading
Loading
+32 −16
Original line number Diff line number Diff line
@@ -759,6 +759,29 @@ static ssize_t emulate_tas_store(struct config_item *item,
	return count;
}

static int target_try_configure_unmap(struct se_device *dev,
				      const char *config_opt)
{
	if (!dev->transport->configure_unmap) {
		pr_err("Generic Block Discard not supported\n");
		return -ENOSYS;
	}

	if (!target_dev_configured(dev)) {
		pr_err("Generic Block Discard setup for %s requires device to be configured\n",
		       config_opt);
		return -ENODEV;
	}

	if (!dev->transport->configure_unmap(dev)) {
		pr_err("Generic Block Discard setup for %s failed\n",
		       config_opt);
		return -ENOSYS;
	}

	return 0;
}

static ssize_t emulate_tpu_store(struct config_item *item,
		const char *page, size_t count)
{
@@ -776,11 +799,9 @@ static ssize_t emulate_tpu_store(struct config_item *item,
	 * Discard supported is detected iblock_create_virtdevice().
	 */
	if (flag && !da->max_unmap_block_desc_count) {
		if (!dev->transport->configure_unmap ||
		    !dev->transport->configure_unmap(dev)) {
			pr_err("Generic Block Discard not supported\n");
			return -ENOSYS;
		}
		ret = target_try_configure_unmap(dev, "emulate_tpu");
		if (ret)
			return ret;
	}

	da->emulate_tpu = flag;
@@ -806,11 +827,9 @@ static ssize_t emulate_tpws_store(struct config_item *item,
	 * Discard supported is detected iblock_create_virtdevice().
	 */
	if (flag && !da->max_unmap_block_desc_count) {
		if (!dev->transport->configure_unmap ||
		    !dev->transport->configure_unmap(dev)) {
			pr_err("Generic Block Discard not supported\n");
			return -ENOSYS;
		}
		ret = target_try_configure_unmap(dev, "emulate_tpws");
		if (ret)
			return ret;
	}

	da->emulate_tpws = flag;
@@ -1022,12 +1041,9 @@ static ssize_t unmap_zeroes_data_store(struct config_item *item,
	 * Discard supported is detected iblock_configure_device().
	 */
	if (flag && !da->max_unmap_block_desc_count) {
		if (!dev->transport->configure_unmap ||
		    !dev->transport->configure_unmap(dev)) {
			pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set because max_unmap_block_desc_count is zero\n",
			       da->da_dev);
			return -ENOSYS;
		}
		ret = target_try_configure_unmap(dev, "unmap_zeroes_data");
		if (ret)
			return ret;
	}
	da->unmap_zeroes_data = flag;
	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",