[PATCH v2 11/25] scsi: smartpqi: Convert sprintf() family to sysfs_emit() family

From: Li Zhijian
Date: Tue Mar 19 2024 - 02:37:44 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: Don Brace <don.brace@xxxxxxxxxxxxx>
CC: "James E.J. Bottomley" <jejb@xxxxxxxxxxxxx>
CC: "Martin K. Petersen" <martin.petersen@xxxxxxxxxx>
CC: storagedev@xxxxxxxxxxxxx
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/smartpqi/smartpqi_init.c | 86 +++++++++++++--------------
1 file changed, 41 insertions(+), 45 deletions(-)

diff --git a/drivers/scsi/smartpqi/smartpqi_init.c b/drivers/scsi/smartpqi/smartpqi_init.c
index 385180c98be4..b4ad519f049e 100644
--- a/drivers/scsi/smartpqi/smartpqi_init.c
+++ b/drivers/scsi/smartpqi/smartpqi_init.c
@@ -6889,13 +6889,13 @@ static ssize_t pqi_firmware_version_show(struct device *dev,
shost = class_to_shost(dev);
ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->firmware_version);
+ return sysfs_emit(buffer, "%s\n", ctrl_info->firmware_version);
}

static ssize_t pqi_driver_version_show(struct device *dev,
struct device_attribute *attr, char *buffer)
{
- return scnprintf(buffer, PAGE_SIZE, "%s\n", DRIVER_VERSION BUILD_TIMESTAMP);
+ return sysfs_emit(buffer, "%s\n", DRIVER_VERSION BUILD_TIMESTAMP);
}

static ssize_t pqi_serial_number_show(struct device *dev,
@@ -6907,7 +6907,7 @@ static ssize_t pqi_serial_number_show(struct device *dev,
shost = class_to_shost(dev);
ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->serial_number);
+ return sysfs_emit(buffer, "%s\n", ctrl_info->serial_number);
}

static ssize_t pqi_model_show(struct device *dev,
@@ -6919,7 +6919,7 @@ static ssize_t pqi_model_show(struct device *dev,
shost = class_to_shost(dev);
ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->model);
+ return sysfs_emit(buffer, "%s\n", ctrl_info->model);
}

static ssize_t pqi_vendor_show(struct device *dev,
@@ -6931,7 +6931,7 @@ static ssize_t pqi_vendor_show(struct device *dev,
shost = class_to_shost(dev);
ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->vendor);
+ return sysfs_emit(buffer, "%s\n", ctrl_info->vendor);
}

static ssize_t pqi_host_rescan_store(struct device *dev,
@@ -6952,14 +6952,14 @@ static ssize_t pqi_lockup_action_show(struct device *dev,

for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
if (pqi_lockup_actions[i].action == pqi_lockup_action)
- count += scnprintf(buffer + count, PAGE_SIZE - count,
+ count += sysfs_emit_at(buffer, count,
"[%s] ", pqi_lockup_actions[i].name);
else
- count += scnprintf(buffer + count, PAGE_SIZE - count,
+ count += sysfs_emit_at(buffer, count,
"%s ", pqi_lockup_actions[i].name);
}

- count += scnprintf(buffer + count, PAGE_SIZE - count, "\n");
+ count += sysfs_emit_at(buffer, count, "\n");

return count;
}
@@ -6990,8 +6990,8 @@ static ssize_t pqi_host_enable_stream_detection_show(struct device *dev,
struct Scsi_Host *shost = class_to_shost(dev);
struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, 10, "%x\n",
- ctrl_info->enable_stream_detection);
+ return sysfs_emit(buffer, "%x\n",
+ ctrl_info->enable_stream_detection);
}

static ssize_t pqi_host_enable_stream_detection_store(struct device *dev,
@@ -7018,7 +7018,7 @@ static ssize_t pqi_host_enable_r5_writes_show(struct device *dev,
struct Scsi_Host *shost = class_to_shost(dev);
struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r5_writes);
+ return sysfs_emit(buffer, "%x\n", ctrl_info->enable_r5_writes);
}

static ssize_t pqi_host_enable_r5_writes_store(struct device *dev,
@@ -7045,7 +7045,7 @@ static ssize_t pqi_host_enable_r6_writes_show(struct device *dev,
struct Scsi_Host *shost = class_to_shost(dev);
struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);

- return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r6_writes);
+ return sysfs_emit(buffer, "%x\n", ctrl_info->enable_r6_writes);
}

static ssize_t pqi_host_enable_r6_writes_store(struct device *dev,
@@ -7128,13 +7128,16 @@ static ssize_t pqi_unique_id_show(struct device *dev,

spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);

- return scnprintf(buffer, PAGE_SIZE,
- "%02X%02X%02X%02X%02X%02X%02X%02X"
- "%02X%02X%02X%02X%02X%02X%02X%02X\n",
- unique_id[0], unique_id[1], unique_id[2], unique_id[3],
- unique_id[4], unique_id[5], unique_id[6], unique_id[7],
- unique_id[8], unique_id[9], unique_id[10], unique_id[11],
- unique_id[12], unique_id[13], unique_id[14], unique_id[15]);
+ return sysfs_emit(buffer, "%02X%02X%02X%02X%02X%02X%02X%02X"
+ "%02X%02X%02X%02X%02X%02X%02X%02X\n",
+ unique_id[0], unique_id[1], unique_id[2],
+ unique_id[3],
+ unique_id[4], unique_id[5], unique_id[6],
+ unique_id[7],
+ unique_id[8], unique_id[9], unique_id[10],
+ unique_id[11],
+ unique_id[12], unique_id[13], unique_id[14],
+ unique_id[15]);
}

static ssize_t pqi_lunid_show(struct device *dev,
@@ -7164,7 +7167,7 @@ static ssize_t pqi_lunid_show(struct device *dev,

spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);

- return scnprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
+ return sysfs_emit(buffer, "0x%8phN\n", lunid);
}

#define MAX_PATHS 8
@@ -7208,13 +7211,12 @@ static ssize_t pqi_path_info_show(struct device *dev,
else
continue;

- output_len += scnprintf(buf + output_len,
- PAGE_SIZE - output_len,
- "[%d:%d:%d:%d] %20.20s ",
- ctrl_info->scsi_host->host_no,
- device->bus, device->target,
- device->lun,
- scsi_device_type(device->devtype));
+ output_len += sysfs_emit_at(buf, output_len,
+ "[%d:%d:%d:%d] %20.20s ",
+ ctrl_info->scsi_host->host_no,
+ device->bus, device->target,
+ device->lun,
+ scsi_device_type(device->devtype));

if (device->devtype == TYPE_RAID ||
pqi_is_logical_device(device))
@@ -7227,27 +7229,22 @@ static ssize_t pqi_path_info_show(struct device *dev,
if (phys_connector[1] < '0')
phys_connector[1] = '0';

- output_len += scnprintf(buf + output_len,
- PAGE_SIZE - output_len,
- "PORT: %.2s ", phys_connector);
+ output_len += sysfs_emit_at(buf, output_len,
+ "PORT: %.2s ", phys_connector);

box = device->box[i];
if (box != 0 && box != 0xFF)
- output_len += scnprintf(buf + output_len,
- PAGE_SIZE - output_len,
- "BOX: %hhu ", box);
+ output_len += sysfs_emit_at(buf, output_len,
+ "BOX: %hhu ", box);

if ((device->devtype == TYPE_DISK ||
device->devtype == TYPE_ZBC) &&
pqi_expose_device(device))
- output_len += scnprintf(buf + output_len,
- PAGE_SIZE - output_len,
- "BAY: %hhu ", bay);
+ output_len += sysfs_emit_at(buf, output_len,
+ "BAY: %hhu ", bay);

end_buffer:
- output_len += scnprintf(buf + output_len,
- PAGE_SIZE - output_len,
- "%s\n", active);
+ output_len += sysfs_emit_at(buf, output_len, "%s\n", active);
}

spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
@@ -7282,7 +7279,7 @@ static ssize_t pqi_sas_address_show(struct device *dev,

spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);

- return scnprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
+ return sysfs_emit(buffer, "0x%016llx\n", sas_address);
}

static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
@@ -7346,7 +7343,7 @@ static ssize_t pqi_raid_level_show(struct device *dev,

spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);

- return scnprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
+ return sysfs_emit(buffer, "%s\n", raid_level);
}

static ssize_t pqi_raid_bypass_cnt_show(struct device *dev,
@@ -7376,7 +7373,7 @@ static ssize_t pqi_raid_bypass_cnt_show(struct device *dev,

spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);

- return scnprintf(buffer, PAGE_SIZE, "0x%x\n", raid_bypass_cnt);
+ return sysfs_emit(buffer, "0x%x\n", raid_bypass_cnt);
}

static ssize_t pqi_sas_ncq_prio_enable_show(struct device *dev,
@@ -7402,8 +7399,7 @@ static ssize_t pqi_sas_ncq_prio_enable_show(struct device *dev,
return -ENODEV;
}

- output_len = snprintf(buf, PAGE_SIZE, "%d\n",
- device->ncq_prio_enable);
+ output_len = sysfs_emit(buf, "%d\n", device->ncq_prio_enable);
spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);

return output_len;
@@ -7455,7 +7451,7 @@ static ssize_t pqi_numa_node_show(struct device *dev,
sdev = to_scsi_device(dev);
ctrl_info = shost_to_hba(sdev->host);

- return scnprintf(buffer, PAGE_SIZE, "%d\n", ctrl_info->numa_node);
+ return sysfs_emit(buffer, "%d\n", ctrl_info->numa_node);
}

static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
--
2.29.2