[PATCH v2 20/25] scsi: core: Convert sprintf() family to sysfs_emit() family

From: Li Zhijian
Date: Tue Mar 19 2024 - 02:37:09 EST


Per filesystems/sysfs.rst, show() should only use sysfs_emit()
or sysfs_emit_at() when formatting the value to be returned to user space.

coccinelle complains that there are still a couple of functions that use
snprintf(). Convert them to sysfs_emit().

sprintf() and scnprintf() will be converted as well if they have.

Generally, this patch is generated by
make coccicheck M=<path/to/file> MODE=patch \
COCCI=scripts/coccinelle/api/device_attr_show.cocci

No functional change intended

CC: "James E.J. Bottomley" <jejb@xxxxxxxxxxxxx>
CC: "Martin K. Petersen" <martin.petersen@xxxxxxxxxx>
CC: linux-scsi@xxxxxxxxxxxxxxx
Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxx>
---
This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
Split them per subsystem so that the maintainer can review it easily
[1] https://lore.kernel.org/lkml/20240116041129.3937800-1-lizhijian@xxxxxxxxxxx/
---
drivers/scsi/scsi_sysfs.c | 69 ++++++++++++++++++++-------------------
1 file changed, 35 insertions(+), 34 deletions(-)

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index 49dd34426d5e..2e9d93d0e770 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -163,7 +163,7 @@ show_##name (struct device *dev, struct device_attribute *attr, \
char *buf) \
{ \
struct Scsi_Host *shost = class_to_shost(dev); \
- return snprintf (buf, 20, format_string, shost->field); \
+ return sysfs_emit(buf, format_string, shost->field); \
}

/*
@@ -228,7 +228,7 @@ show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
if (!name)
return -EINVAL;

- return snprintf(buf, 20, "%s\n", name);
+ return sysfs_emit(buf, "%s\n", name);
}

/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
@@ -241,12 +241,13 @@ show_shost_mode(unsigned int mode, char *buf)
ssize_t len = 0;

if (mode & MODE_INITIATOR)
- len = sprintf(buf, "%s", "Initiator");
+ len = sysfs_emit(buf, "%s", "Initiator");

if (mode & MODE_TARGET)
- len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
+ len += sysfs_emit_at(buf, len, "%s%s",
+ len ? ", " : "", "Target");

- len += sprintf(buf + len, "\n");
+ len += sysfs_emit_at(buf, len, "\n");

return len;
}
@@ -274,7 +275,7 @@ show_shost_active_mode(struct device *dev,
struct Scsi_Host *shost = class_to_shost(dev);

if (shost->active_mode == MODE_UNKNOWN)
- return snprintf(buf, 20, "unknown\n");
+ return sysfs_emit(buf, "unknown\n");
else
return show_shost_mode(shost->active_mode, buf);
}
@@ -324,8 +325,8 @@ show_shost_eh_deadline(struct device *dev,
struct Scsi_Host *shost = class_to_shost(dev);

if (shost->eh_deadline == -1)
- return snprintf(buf, strlen("off") + 2, "off\n");
- return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
+ return sysfs_emit(buf, "off\n");
+ return sysfs_emit(buf, "%u\n", shost->eh_deadline / HZ);
}

static ssize_t
@@ -382,14 +383,14 @@ static ssize_t
show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
{
struct Scsi_Host *shost = class_to_shost(dev);
- return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
+ return sysfs_emit(buf, "%d\n", scsi_host_busy(shost));
}
static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);

static ssize_t
show_use_blk_mq(struct device *dev, struct device_attribute *attr, char *buf)
{
- return sprintf(buf, "1\n");
+ return sysfs_emit(buf, "1\n");
}
static DEVICE_ATTR(use_blk_mq, S_IRUGO, show_use_blk_mq, NULL);

@@ -399,7 +400,7 @@ show_nr_hw_queues(struct device *dev, struct device_attribute *attr, char *buf)
struct Scsi_Host *shost = class_to_shost(dev);
struct blk_mq_tag_set *tag_set = &shost->tag_set;

- return snprintf(buf, 20, "%d\n", tag_set->nr_hw_queues);
+ return sysfs_emit(buf, "%d\n", tag_set->nr_hw_queues);
}
static DEVICE_ATTR(nr_hw_queues, S_IRUGO, show_nr_hw_queues, NULL);

@@ -589,7 +590,7 @@ sdev_show_##field (struct device *dev, struct device_attribute *attr, \
{ \
struct scsi_device *sdev; \
sdev = to_scsi_device(dev); \
- return snprintf (buf, 20, format_string, sdev->field); \
+ return sysfs_emit(buf, format_string, sdev->field); \
} \

/*
@@ -677,7 +678,7 @@ sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct scsi_device *sdev = to_scsi_device(dev);
- return snprintf(buf, 20, "%d\n", scsi_device_busy(sdev));
+ return sysfs_emit(buf, "%d\n", scsi_device_busy(sdev));
}
static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);

@@ -686,7 +687,7 @@ sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct scsi_device *sdev = to_scsi_device(dev);
- return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
+ return sysfs_emit(buf, "%d\n", atomic_read(&sdev->device_blocked));
}
static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);

@@ -698,7 +699,7 @@ sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
{
struct scsi_device *sdev;
sdev = to_scsi_device(dev);
- return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
+ return sysfs_emit(buf, "%d\n", sdev->request_queue->rq_timeout / HZ);
}

static ssize_t
@@ -719,7 +720,7 @@ sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *bu
{
struct scsi_device *sdev;
sdev = to_scsi_device(dev);
- return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
+ return sysfs_emit(buf, "%u\n", sdev->eh_timeout / HZ);
}

static ssize_t
@@ -855,7 +856,7 @@ show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
if (!name)
return -EINVAL;

- return snprintf(buf, 20, "%s\n", name);
+ return sysfs_emit(buf, "%s\n", name);
}

static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
@@ -870,7 +871,7 @@ show_queue_type_field(struct device *dev, struct device_attribute *attr,
if (sdev->simple_tags)
name = "simple";

- return snprintf(buf, 20, "%s\n", name);
+ return sysfs_emit(buf, "%s\n", name);
}

static ssize_t
@@ -950,7 +951,7 @@ static ssize_t
show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
+ return sysfs_emit(buf, "%d\n", (int)sizeof(atomic_t) * 8);
}

static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
@@ -962,7 +963,7 @@ show_iostat_##field(struct device *dev, struct device_attribute *attr, \
{ \
struct scsi_device *sdev = to_scsi_device(dev); \
unsigned long long count = atomic_read(&sdev->field); \
- return snprintf(buf, 20, "0x%llx\n", count); \
+ return sysfs_emit(buf, "0x%llx\n", count); \
} \
static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)

@@ -976,7 +977,7 @@ sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
{
struct scsi_device *sdev;
sdev = to_scsi_device(dev);
- return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
+ return sysfs_emit(buf, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
}
static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);

@@ -987,7 +988,7 @@ sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
{ \
struct scsi_device *sdev = to_scsi_device(dev); \
int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
- return snprintf(buf, 20, "%d\n", val); \
+ return sysfs_emit(buf, "%d\n", val); \
}

#define DECLARE_EVT_STORE(name, Cap_name) \
@@ -1089,14 +1090,14 @@ sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
name = sdev_bflags_name[i];

if (name)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "%s%s", len ? " " : "", name);
+ len += sysfs_emit_at(buf, len,
+ "%s%s", len ? " " : "", name);
else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "%sINVALID_BIT(%d)", len ? " " : "", i);
+ len += sysfs_emit_at(buf, len,
+ "%sINVALID_BIT(%d)", len ? " " : "", i);
}
if (len)
- len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
+ len += sysfs_emit_at(buf, len, "\n");
return len;
}
static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
@@ -1109,9 +1110,9 @@ sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
struct scsi_device *sdev = to_scsi_device(dev);

if (!sdev->handler)
- return snprintf(buf, 20, "detached\n");
+ return sysfs_emit(buf, "detached\n");

- return snprintf(buf, 20, "%s\n", sdev->handler->name);
+ return sysfs_emit(buf, "%s\n", sdev->handler->name);
}

static ssize_t
@@ -1169,7 +1170,7 @@ sdev_show_access_state(struct device *dev,
access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
access_state_name = scsi_access_state_name(access_state);

- return sprintf(buf, "%s\n",
+ return sysfs_emit(buf, "%s\n",
access_state_name ? access_state_name : "unknown");
}
static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
@@ -1185,9 +1186,9 @@ sdev_show_preferred_path(struct device *dev,
return -EINVAL;

if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
- return sprintf(buf, "1\n");
+ return sysfs_emit(buf, "1\n");
else
- return sprintf(buf, "0\n");
+ return sysfs_emit(buf, "0\n");
}
static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
#endif
@@ -1199,8 +1200,8 @@ sdev_show_queue_ramp_up_period(struct device *dev,
{
struct scsi_device *sdev;
sdev = to_scsi_device(dev);
- return snprintf(buf, 20, "%u\n",
- jiffies_to_msecs(sdev->queue_ramp_up_period));
+ return sysfs_emit(buf, "%u\n",
+ jiffies_to_msecs(sdev->queue_ramp_up_period));
}

static ssize_t
--
2.29.2