[PATCH v2] gfs2/sysfs: Replace sprintf/snprintf with sysfs_emit()

Utkarsh Singh posted 1 patch 1 week, 2 days ago
fs/gfs2/sys.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)
[PATCH v2] gfs2/sysfs: Replace sprintf/snprintf with sysfs_emit()
Posted by Utkarsh Singh 1 week, 2 days ago
Documentation/filesystems/sysfs.rst mentions that show() should only
use sysfs_emit() or sysfs_emit_at() when formatting values returned
to user space. This patch updates the GFS2 sysfs interface accordingly.

It replaces uses of sprintf() and snprintf() in all *_show() functions
with sysfs_emit() to align with current kernel sysfs API best practices.
It also updates the GFS2_SHOW_UINT macro to use sysfs_emit() instead
of snprintf().

Signed-off-by: Utkarsh Singh <utkarsh.singh.em@gmail.com>
---
 v1 --> v2: Code style cleanup and maintainer updates 
 
 fs/gfs2/sys.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/fs/gfs2/sys.c b/fs/gfs2/sys.c
index c3c8842920d2..487fff098465 100644
--- a/fs/gfs2/sys.c
+++ b/fs/gfs2/sys.c
@@ -59,7 +59,7 @@ static struct kset *gfs2_kset;
 
 static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%u:%u\n",
+	return sysfs_emit(buf, "%u:%u\n",
 			MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
 }
 
@@ -68,7 +68,7 @@ static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
 	unsigned long f = sdp->sd_flags;
 	ssize_t s;
 
-	s = snprintf(buf, PAGE_SIZE,
+	s = sysfs_emit(buf,
 		     "Journal Checked:          %d\n"
 		     "Journal Live:             %d\n"
 		     "Journal ID:               %d\n"
@@ -140,7 +140,7 @@ static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
 
 static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
+	return sysfs_emit(buf, "%s\n", sdp->sd_fsname);
 }
 
 static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
@@ -150,7 +150,7 @@ static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
 	buf[0] = '\0';
 	if (uuid_is_null(&s->s_uuid))
 		return 0;
-	return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
+	return sysfs_emit(buf, "%pUB\n", &s->s_uuid);
 }
 
 static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
@@ -158,7 +158,7 @@ static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
 	struct super_block *sb = sdp->sd_vfs;
 	int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
 
-	return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
+	return sysfs_emit(buf, "%d\n", frozen);
 }
 
 static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
@@ -194,7 +194,7 @@ static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
 {
 	unsigned int b = gfs2_withdrawing_or_withdrawn(sdp);
-	return snprintf(buf, PAGE_SIZE, "%u\n", b);
+	return sysfs_emit(buf, "%u\n", b);
 }
 
 static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
@@ -397,7 +397,7 @@ static struct kobj_type gfs2_ktype = {
 static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
 {
 	const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
-	return sprintf(buf, "%s\n", ops->lm_proto_name);
+	return sysfs_emit(buf, "%s\n", ops->lm_proto_name);
 }
 
 static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
@@ -408,7 +408,7 @@ static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
 
 	if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
 		val = 1;
-	ret = sprintf(buf, "%d\n", val);
+	ret = sysfs_emit(buf, "%d\n", val);
 	return ret;
 }
 
@@ -437,7 +437,7 @@ static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
 {
 	int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
 
-	return sprintf(buf, "%d\n", val);
+	return sysfs_emit(buf, "%d\n", val);
 }
 
 static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
@@ -459,7 +459,7 @@ static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
 {
 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
-	return sprintf(buf, "%d\n", ls->ls_first);
+	return sysfs_emit(buf, "%d\n", ls->ls_first);
 }
 
 static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
@@ -492,7 +492,7 @@ static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
 {
 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
-	return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
+	return sysfs_emit(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
 }
 
 int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
@@ -550,18 +550,18 @@ static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
 {
 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
-	return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
+	return sysfs_emit(buf, "%d\n", ls->ls_recover_jid_done);
 }
 
 static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
 {
 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
-	return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
+	return sysfs_emit(buf, "%d\n", ls->ls_recover_jid_status);
 }
 
 static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
 {
-	return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
+	return sysfs_emit(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
 }
 
 static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
@@ -626,7 +626,7 @@ static struct attribute *lock_module_attrs[] = {
 
 static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%u %u\n",
+	return sysfs_emit(buf, "%u %u\n",
 			sdp->sd_tune.gt_quota_scale_num,
 			sdp->sd_tune.gt_quota_scale_den);
 }
@@ -679,7 +679,7 @@ static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
 #define TUNE_ATTR_2(name, store)                                              \
 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
 {                                                                             \
-	return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
+	return sysfs_emit(buf, "%u\n", sdp->sd_tune.gt_##name);               \
 }                                                                             \
 TUNE_ATTR_3(name, name##_show, store)
 
-- 
2.43.0
Re: [PATCH v2] gfs2/sysfs: Replace sprintf/snprintf with sysfs_emit()
Posted by Andreas Gruenbacher 1 week, 2 days ago
On Mon, Sep 22, 2025 at 7:53 PM Utkarsh Singh
<utkarsh.singh.em@gmail.com> wrote:
> Documentation/filesystems/sysfs.rst mentions that show() should only
> use sysfs_emit() or sysfs_emit_at() when formatting values returned
> to user space. This patch updates the GFS2 sysfs interface accordingly.
>
> It replaces uses of sprintf() and snprintf() in all *_show() functions
> with sysfs_emit() to align with current kernel sysfs API best practices.

I've applied that part. I'm not pushing this into the upcoming merge
window anymore, though.

> It also updates the GFS2_SHOW_UINT macro to use sysfs_emit() instead
> of snprintf().

Huh, what's that?

> Signed-off-by: Utkarsh Singh <utkarsh.singh.em@gmail.com>
> ---
>  v1 --> v2: Code style cleanup and maintainer updates
>
>  fs/gfs2/sys.c | 32 ++++++++++++++++----------------
>  1 file changed, 16 insertions(+), 16 deletions(-)
>
> diff --git a/fs/gfs2/sys.c b/fs/gfs2/sys.c
> index c3c8842920d2..487fff098465 100644
> --- a/fs/gfs2/sys.c
> +++ b/fs/gfs2/sys.c
> @@ -59,7 +59,7 @@ static struct kset *gfs2_kset;
>
>  static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
>  {
> -       return snprintf(buf, PAGE_SIZE, "%u:%u\n",
> +       return sysfs_emit(buf, "%u:%u\n",
>                         MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
>  }
>
> @@ -68,7 +68,7 @@ static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
>         unsigned long f = sdp->sd_flags;
>         ssize_t s;
>
> -       s = snprintf(buf, PAGE_SIZE,
> +       s = sysfs_emit(buf,
>                      "Journal Checked:          %d\n"
>                      "Journal Live:             %d\n"
>                      "Journal ID:               %d\n"
> @@ -140,7 +140,7 @@ static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
>
>  static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
>  {
> -       return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
> +       return sysfs_emit(buf, "%s\n", sdp->sd_fsname);
>  }
>
>  static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
> @@ -150,7 +150,7 @@ static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
>         buf[0] = '\0';
>         if (uuid_is_null(&s->s_uuid))
>                 return 0;
> -       return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
> +       return sysfs_emit(buf, "%pUB\n", &s->s_uuid);
>  }
>
>  static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
> @@ -158,7 +158,7 @@ static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
>         struct super_block *sb = sdp->sd_vfs;
>         int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
>
> -       return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
> +       return sysfs_emit(buf, "%d\n", frozen);
>  }
>
>  static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
> @@ -194,7 +194,7 @@ static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
>  static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         unsigned int b = gfs2_withdrawing_or_withdrawn(sdp);
> -       return snprintf(buf, PAGE_SIZE, "%u\n", b);
> +       return sysfs_emit(buf, "%u\n", b);
>  }
>
>  static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
> @@ -397,7 +397,7 @@ static struct kobj_type gfs2_ktype = {
>  static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
> -       return sprintf(buf, "%s\n", ops->lm_proto_name);
> +       return sysfs_emit(buf, "%s\n", ops->lm_proto_name);
>  }
>
>  static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
> @@ -408,7 +408,7 @@ static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
>
>         if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
>                 val = 1;
> -       ret = sprintf(buf, "%d\n", val);
> +       ret = sysfs_emit(buf, "%d\n", val);
>         return ret;
>  }
>
> @@ -437,7 +437,7 @@ static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
>
> -       return sprintf(buf, "%d\n", val);
> +       return sysfs_emit(buf, "%d\n", val);
>  }
>
>  static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
> @@ -459,7 +459,7 @@ static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
>  static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
> -       return sprintf(buf, "%d\n", ls->ls_first);
> +       return sysfs_emit(buf, "%d\n", ls->ls_first);
>  }
>
>  static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
> @@ -492,7 +492,7 @@ static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
>  static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
> -       return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
> +       return sysfs_emit(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
>  }
>
>  int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
> @@ -550,18 +550,18 @@ static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
>  static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
> -       return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
> +       return sysfs_emit(buf, "%d\n", ls->ls_recover_jid_done);
>  }
>
>  static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
>  {
>         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
> -       return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
> +       return sysfs_emit(buf, "%d\n", ls->ls_recover_jid_status);
>  }
>
>  static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
>  {
> -       return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
> +       return sysfs_emit(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
>  }
>
>  static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
> @@ -626,7 +626,7 @@ static struct attribute *lock_module_attrs[] = {
>
>  static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
>  {
> -       return snprintf(buf, PAGE_SIZE, "%u %u\n",
> +       return sysfs_emit(buf, "%u %u\n",
>                         sdp->sd_tune.gt_quota_scale_num,
>                         sdp->sd_tune.gt_quota_scale_den);
>  }
> @@ -679,7 +679,7 @@ static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
>  #define TUNE_ATTR_2(name, store)                                              \
>  static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
>  {                                                                             \
> -       return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
> +       return sysfs_emit(buf, "%u\n", sdp->sd_tune.gt_##name);               \
>  }                                                                             \
>  TUNE_ATTR_3(name, name##_show, store)
>
> --
> 2.43.0
>

Thanks,
Andreas