[PATCH v2 22/25] scsi: scsi_transport_fc: Convert sprintf() family to sysfs_emit() family (part 2)

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


This focuses on the abused in macros.

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_transport_fc.c | 152 +++++++++++++++----------------
1 file changed, 74 insertions(+), 78 deletions(-)

diff --git a/drivers/scsi/scsi_transport_fc.c b/drivers/scsi/scsi_transport_fc.c
index 3335c31772e1..340cda452b44 100644
--- a/drivers/scsi/scsi_transport_fc.c
+++ b/drivers/scsi/scsi_transport_fc.c
@@ -114,7 +114,6 @@ static struct {
{ FC_PORTTYPE_NPIV, "NPIV VPORT" },
};
fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
-#define FC_PORTTYPE_MAX_NAMELEN 50

/* Reuse fc_port_type enum function for vport_type */
#define get_fc_vport_type_name get_fc_port_type_name
@@ -166,7 +165,6 @@ static struct {
};
fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
fc_enum_name_match(port_state, fc_port_state, fc_port_state_names)
-#define FC_PORTSTATE_MAX_NAMELEN 20


/* Convert fc_vport_state values to ascii string name */
@@ -186,7 +184,6 @@ static struct {
{ FC_VPORT_FAILED, "VPort Failed" },
};
fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
-#define FC_VPORTSTATE_MAX_NAMELEN 24

/* Reuse fc_vport_state enum function for vport_last_state */
#define get_fc_vport_last_state_name get_fc_vport_state_name
@@ -983,7 +980,7 @@ static void __exit fc_transport_exit(void)
* FC Remote Port Attribute Management
*/

-#define fc_rport_show_function(field, format_string, sz, cast) \
+#define fc_rport_show_function(field, format_string, cast) \
static ssize_t \
show_fc_rport_##field (struct device *dev, \
struct device_attribute *attr, char *buf) \
@@ -996,7 +993,7 @@ show_fc_rport_##field (struct device *dev, \
(rport->port_state == FC_PORTSTATE_DELETED) || \
(rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
i->f->get_rport_##field(rport); \
- return snprintf(buf, sz, format_string, cast rport->field); \
+ return sysfs_emit(buf, format_string, cast rport->field); \
}

#define fc_rport_store_function(field) \
@@ -1021,45 +1018,45 @@ store_fc_rport_##field(struct device *dev, \
return count; \
}

-#define fc_rport_rd_attr(field, format_string, sz) \
- fc_rport_show_function(field, format_string, sz, ) \
+#define fc_rport_rd_attr(field, format_string) \
+ fc_rport_show_function(field, format_string, ) \
static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
show_fc_rport_##field, NULL)

-#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
- fc_rport_show_function(field, format_string, sz, (cast)) \
+#define fc_rport_rd_attr_cast(field, format_string, cast) \
+ fc_rport_show_function(field, format_string, (cast)) \
static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
show_fc_rport_##field, NULL)

-#define fc_rport_rw_attr(field, format_string, sz) \
- fc_rport_show_function(field, format_string, sz, ) \
+#define fc_rport_rw_attr(field, format_string) \
+ fc_rport_show_function(field, format_string, ) \
fc_rport_store_function(field) \
static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
show_fc_rport_##field, \
store_fc_rport_##field)


-#define fc_private_rport_show_function(field, format_string, sz, cast) \
+#define fc_private_rport_show_function(field, format_string, cast) \
static ssize_t \
show_fc_rport_##field (struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct fc_rport *rport = transport_class_to_rport(dev); \
- return snprintf(buf, sz, format_string, cast rport->field); \
+ return sysfs_emit(buf, format_string, cast rport->field); \
}

-#define fc_private_rport_rd_attr(field, format_string, sz) \
- fc_private_rport_show_function(field, format_string, sz, ) \
+#define fc_private_rport_rd_attr(field, format_string) \
+ fc_private_rport_show_function(field, format_string, ) \
static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
show_fc_rport_##field, NULL)

-#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
- fc_private_rport_show_function(field, format_string, sz, (cast)) \
+#define fc_private_rport_rd_attr_cast(field, format_string, cast) \
+ fc_private_rport_show_function(field, format_string, (cast)) \
static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
show_fc_rport_##field, NULL)


-#define fc_private_rport_rd_enum_attr(title, maxlen) \
+#define fc_private_rport_rd_enum_attr(title) \
static ssize_t \
show_fc_rport_##title (struct device *dev, \
struct device_attribute *attr, char *buf) \
@@ -1069,7 +1066,7 @@ show_fc_rport_##title (struct device *dev, \
name = get_fc_##title##_name(rport->title); \
if (!name) \
return -EINVAL; \
- return snprintf(buf, maxlen, "%s\n", name); \
+ return sysfs_emit(buf, "%s\n", name); \
} \
static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
show_fc_rport_##title, NULL)
@@ -1112,7 +1109,7 @@ static FC_DEVICE_ATTR(rport, title, S_IRUGO, \

/* Fixed Remote Port Attributes */

-fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
+fc_private_rport_rd_attr(maxframe_size, "%u bytes\n");

static ssize_t
show_fc_rport_supported_classes (struct device *dev,
@@ -1175,7 +1172,7 @@ static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
return 0;
}

-fc_rport_show_function(dev_loss_tmo, "%u\n", 20, )
+fc_rport_show_function(dev_loss_tmo, "%u\n", )
static ssize_t
store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
@@ -1199,9 +1196,9 @@ static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,

/* Private Remote Port Attributes */

-fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
-fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
-fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
+fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", unsigned long long);
+fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", unsigned long long);
+fc_private_rport_rd_attr(port_id, "0x%06x\n");

static ssize_t
show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
@@ -1289,7 +1286,7 @@ show_fc_rport_port_state(struct device *dev,
static FC_DEVICE_ATTR(rport, port_state, 0444 | 0200,
show_fc_rport_port_state, fc_rport_set_marginal_state);

-fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
+fc_private_rport_rd_attr(scsi_target_id, "%d\n");

/*
* fast_io_fail_tmo attribute
@@ -1346,7 +1343,7 @@ static ssize_t fc_rport_fpinstat_##name(struct device *cd, \
{ \
struct fc_rport *rport = transport_class_to_rport(cd); \
\
- return snprintf(buf, 20, "0x%llx\n", rport->fpin_stats.name); \
+ return sysfs_emit(buf, "0x%llx\n", rport->fpin_stats.name); \
} \
static FC_DEVICE_ATTR(rport, fpin_##name, 0444, fc_rport_fpinstat_##name, NULL)

@@ -1411,7 +1408,7 @@ static struct attribute_group fc_rport_statistics_group = {
* involved in sysfs functions. The driver only gets involved if
* it's the "old" style that doesn't use rports.
*/
-#define fc_starget_show_function(field, format_string, sz, cast) \
+#define fc_starget_show_function(field, format_string, cast) \
static ssize_t \
show_fc_starget_##field (struct device *dev, \
struct device_attribute *attr, char *buf) \
@@ -1424,17 +1421,17 @@ show_fc_starget_##field (struct device *dev, \
fc_starget_##field(starget) = rport->field; \
else if (i->f->get_starget_##field) \
i->f->get_starget_##field(starget); \
- return snprintf(buf, sz, format_string, \
+ return sysfs_emit(buf, format_string, \
cast fc_starget_##field(starget)); \
}

-#define fc_starget_rd_attr(field, format_string, sz) \
- fc_starget_show_function(field, format_string, sz, ) \
+#define fc_starget_rd_attr(field, format_string) \
+ fc_starget_show_function(field, format_string, ) \
static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
show_fc_starget_##field, NULL)

-#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
- fc_starget_show_function(field, format_string, sz, (cast)) \
+#define fc_starget_rd_attr_cast(field, format_string, cast) \
+ fc_starget_show_function(field, format_string, (cast)) \
static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
show_fc_starget_##field, NULL)

@@ -1457,9 +1454,9 @@ static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
count++

/* The FC Transport SCSI Target Attributes: */
-fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
-fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
-fc_starget_rd_attr(port_id, "0x%06x\n", 20);
+fc_starget_rd_attr_cast(node_name, "0x%llx\n", unsigned long long);
+fc_starget_rd_attr_cast(port_name, "0x%llx\n", unsigned long long);
+fc_starget_rd_attr(port_id, "0x%06x\n");


/*
@@ -1477,7 +1474,7 @@ show_fc_vport_##field (struct device *dev, \
if ((i->f->get_vport_##field) && \
!(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
i->f->get_vport_##field(vport); \
- return snprintf(buf, sz, format_string, cast vport->field); \
+ return sysfs_emit(buf, format_string, cast vport->field); \
}

#define fc_vport_store_function(field) \
@@ -1544,7 +1541,7 @@ show_fc_vport_##field (struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct fc_vport *vport = transport_class_to_vport(dev); \
- return snprintf(buf, sz, format_string, cast vport->field); \
+ return sysfs_emit(buf, format_string, cast vport->field); \
}

#define fc_private_vport_store_u32_function(field) \
@@ -1584,7 +1581,7 @@ static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
store_fc_vport_##field)


-#define fc_private_vport_rd_enum_attr(title, maxlen) \
+#define fc_private_vport_rd_enum_attr(title) \
static ssize_t \
show_fc_vport_##title (struct device *dev, \
struct device_attribute *attr, \
@@ -1595,7 +1592,7 @@ show_fc_vport_##title (struct device *dev, \
name = get_fc_##title##_name(vport->title); \
if (!name) \
return -EINVAL; \
- return snprintf(buf, maxlen, "%s\n", name); \
+ return sysfs_emit(buf, "%s\n", name); \
} \
static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
show_fc_vport_##title, NULL)
@@ -1650,8 +1647,8 @@ static FC_DEVICE_ATTR(vport, title, S_IRUGO, \

/* Private Virtual Port Attributes */

-fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
-fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
+fc_private_vport_rd_enum_attr(vport_state);
+fc_private_vport_rd_enum_attr(vport_last_state);
fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);

@@ -1667,7 +1664,7 @@ show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
}
static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);

-fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
+fc_private_vport_rd_enum_attr(vport_type);

fc_private_vport_show_function(symbolic_name, "%s\n",
FC_VPORT_SYMBOLIC_NAMELEN + 1, )
@@ -1735,7 +1732,7 @@ static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
* Host Attribute Management
*/

-#define fc_host_show_function(field, format_string, sz, cast) \
+#define fc_host_show_function(field, format_string, cast) \
static ssize_t \
show_fc_host_##field (struct device *dev, \
struct device_attribute *attr, char *buf) \
@@ -1744,7 +1741,7 @@ show_fc_host_##field (struct device *dev, \
struct fc_internal *i = to_fc_internal(shost->transportt); \
if (i->f->get_host_##field) \
i->f->get_host_##field(shost); \
- return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
+ return sysfs_emit(buf, format_string, cast fc_host_##field(shost)); \
}

#define fc_host_store_function(field) \
@@ -1785,13 +1782,13 @@ store_fc_host_##field(struct device *dev, \
return count; \
}

-#define fc_host_rd_attr(field, format_string, sz) \
- fc_host_show_function(field, format_string, sz, ) \
+#define fc_host_rd_attr(field, format_string) \
+ fc_host_show_function(field, format_string, ) \
static FC_DEVICE_ATTR(host, field, S_IRUGO, \
show_fc_host_##field, NULL)

-#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
- fc_host_show_function(field, format_string, sz, (cast)) \
+#define fc_host_rd_attr_cast(field, format_string, cast) \
+ fc_host_show_function(field, format_string, (cast)) \
static FC_DEVICE_ATTR(host, field, S_IRUGO, \
show_fc_host_##field, NULL)

@@ -1802,7 +1799,7 @@ static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
show_fc_host_##field, \
store_fc_host_##field)

-#define fc_host_rd_enum_attr(title, maxlen) \
+#define fc_host_rd_enum_attr(title) \
static ssize_t \
show_fc_host_##title (struct device *dev, \
struct device_attribute *attr, char *buf) \
@@ -1815,7 +1812,7 @@ show_fc_host_##title (struct device *dev, \
name = get_fc_##title##_name(fc_host_##title(shost)); \
if (!name) \
return -EINVAL; \
- return snprintf(buf, maxlen, "%s\n", name); \
+ return sysfs_emit(buf, "%s\n", name); \
} \
static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)

@@ -1845,22 +1842,22 @@ static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
count++


-#define fc_private_host_show_function(field, format_string, sz, cast) \
+#define fc_private_host_show_function(field, format_string, cast) \
static ssize_t \
show_fc_host_##field (struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct Scsi_Host *shost = transport_class_to_shost(dev); \
- return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
+ return sysfs_emit(buf, format_string, cast fc_host_##field(shost)); \
}

-#define fc_private_host_rd_attr(field, format_string, sz) \
- fc_private_host_show_function(field, format_string, sz, ) \
+#define fc_private_host_rd_attr(field, format_string) \
+ fc_private_host_show_function(field, format_string, ) \
static FC_DEVICE_ATTR(host, field, S_IRUGO, \
show_fc_host_##field, NULL)

-#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
- fc_private_host_show_function(field, format_string, sz, (cast)) \
+#define fc_private_host_rd_attr_cast(field, format_string, cast) \
+ fc_private_host_show_function(field, format_string, (cast)) \
static FC_DEVICE_ATTR(host, field, S_IRUGO, \
show_fc_host_##field, NULL)

@@ -1920,20 +1917,20 @@ static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
show_fc_host_supported_speeds, NULL);


-fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
-fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
-fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
+fc_private_host_rd_attr_cast(node_name, "0x%llx\n", unsigned long long);
+fc_private_host_rd_attr_cast(port_name, "0x%llx\n", unsigned long long);
+fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n",
unsigned long long);
-fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
-fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
-fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
-fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
-fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
-fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
-fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
-fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
-fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
-fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
+fc_private_host_rd_attr(maxframe_size, "%u bytes\n");
+fc_private_host_rd_attr(max_npiv_vports, "%u\n");
+fc_private_host_rd_attr(serial_number, "%s\n");
+fc_private_host_rd_attr(manufacturer, "%s\n");
+fc_private_host_rd_attr(model, "%s\n");
+fc_private_host_rd_attr(model_description, "%s\n");
+fc_private_host_rd_attr(hardware_version, "%s\n");
+fc_private_host_rd_attr(driver_version, "%s\n");
+fc_private_host_rd_attr(firmware_version, "%s\n");
+fc_private_host_rd_attr(optionrom_version, "%s\n");


/* Dynamic Host Attributes */
@@ -1972,14 +1969,13 @@ static FC_DEVICE_ATTR(host, speed, S_IRUGO,
show_fc_host_speed, NULL);


-fc_host_rd_attr(port_id, "0x%06x\n", 20);
-fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
-fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
-fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
-fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
+fc_host_rd_attr(port_id, "0x%06x\n");
+fc_host_rd_enum_attr(port_type);
+fc_host_rd_enum_attr(port_state);
+fc_host_rd_attr_cast(fabric_name, "0x%llx\n", unsigned long long);
+fc_host_rd_attr(symbolic_name, "%s\n");

-fc_private_host_show_function(system_hostname, "%s\n",
- FC_SYMBOLIC_NAME_SIZE + 1, )
+fc_private_host_show_function(system_hostname, "%s\n", )
fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
show_fc_host_system_hostname, store_fc_host_system_hostname);
@@ -2079,12 +2075,12 @@ store_fc_private_host_dev_loss_tmo(struct device *dev,
return count;
}

-fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
+fc_private_host_show_function(dev_loss_tmo, "%d\n", );
static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
show_fc_host_dev_loss_tmo,
store_fc_private_host_dev_loss_tmo);

-fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
+fc_private_host_rd_attr(npiv_vports_inuse, "%u\n");

/*
* Host Statistics Management
--
2.29.2