[PATCH] virtio_fs: use sysfs_emit_at() instead of snprintf()

chen zhang posted 1 patch 8 hours ago
fs/fuse/virtio_fs.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
[PATCH] virtio_fs: use sysfs_emit_at() instead of snprintf()
Posted by chen zhang 8 hours ago
Follow the advice in Documentation/filesystems/sysfs.rst:
show() should only use sysfs_emit() or sysfs_emit_at() when formatting
the value to be returned to user space.

Signed-off-by: chen zhang <chenzhang@kylinos.cn>
---
 fs/fuse/virtio_fs.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index b2f6486fe1d5..7466e5d6baa2 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -234,7 +234,6 @@ static ssize_t cpu_list_show(struct kobject *kobj,
 	struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj);
 	struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj);
 	unsigned int cpu, qid;
-	const size_t size = PAGE_SIZE - 1;
 	bool first = true;
 	int ret = 0, pos = 0;
 
@@ -244,18 +243,20 @@ static ssize_t cpu_list_show(struct kobject *kobj,
 	qid = fsvq->vq->index;
 	for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
 		if (qid < VQ_REQUEST || (fs->mq_map[cpu] == qid)) {
-			if (first)
-				ret = snprintf(buf + pos, size - pos, "%u", cpu);
-			else
-				ret = snprintf(buf + pos, size - pos, ", %u", cpu);
-
-			if (ret >= size - pos)
-				break;
-			first = false;
+			if (first) {
+				ret = sysfs_emit_at(buf, pos, "%u", cpu);
+				first = false;
+			} else
+				ret = sysfs_emit_at(buf, pos, ", %u", cpu);
+
+			if (ret < 0)
+				return  -EINVAL;
 			pos += ret;
 		}
 	}
-	ret = snprintf(buf + pos, size + 1 - pos, "\n");
+	ret = sysfs_emit_at(buf, pos, "\n");
+	if (ret < 0)
+		return -EINVAL;
 	return pos + ret;
 }
 
-- 
2.25.1