linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Akinobu Mita <akinobu.mita@gmail.com>
To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	corbet@lwn.net, david@redhat.com, osalvador@suse.de,
	shuah@kernel.org, Zhao Gongyi <zhaogongyi@huawei.com>
Cc: Akinobu Mita <akinobu.mita@gmail.com>,
	Wei Yongjun <weiyongjun1@huawei.com>
Subject: [PATCH] lib/notifier-error-inject: fix error when writing errno to debugfs file
Date: Sat, 17 Sep 2022 16:06:08 +0900	[thread overview]
Message-ID: <20220917070608.28210-1-akinobu.mita@gmail.com> (raw)

The simple attribute files do not accept a negative value since the
commit 488dac0c9237 ("libfs: fix error cast of negative value in
simple_attr_write()"), so we can no longer use DEFINE_SIMPLE_ATTRIBUTE() to
define a file operations for errno value.

Fixes: 488dac0c9237 ("libfs: fix error cast of negative value in simple_attr_write()")
Reported-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
---
 lib/notifier-error-inject.c | 38 ++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/lib/notifier-error-inject.c b/lib/notifier-error-inject.c
index 21016b32d313..30ec41f58d53 100644
--- a/lib/notifier-error-inject.c
+++ b/lib/notifier-error-inject.c
@@ -3,20 +3,44 @@
 
 #include "notifier-error-inject.h"
 
-static int debugfs_errno_set(void *data, u64 val)
+static int notifier_err_errno_show(struct seq_file *m, void *data)
 {
-	*(int *)data = clamp_t(int, val, -MAX_ERRNO, 0);
+	int *value = m->private;
+
+	seq_printf(m, "%d\n", *value);
+
 	return 0;
 }
 
-static int debugfs_errno_get(void *data, u64 *val)
+static int notifier_err_errno_open(struct inode *inode, struct file *file)
 {
-	*val = *(int *)data;
-	return 0;
+	return single_open(file, notifier_err_errno_show, inode->i_private);
+}
+
+static ssize_t notifier_err_errno_write(struct file *file, const char __user *ubuf, size_t len,
+					loff_t *offp)
+{
+	struct seq_file *m = file->private_data;
+	int *value = m->private;
+	int ret;
+
+	ret = kstrtoint_from_user(ubuf, len, 0, value);
+	if (ret)
+		return ret;
+
+	*value = clamp(*value, -MAX_ERRNO, 0);
+
+	return len;
 }
 
-DEFINE_SIMPLE_ATTRIBUTE(fops_errno, debugfs_errno_get, debugfs_errno_set,
-			"%lld\n");
+static const struct file_operations fops_errno = {
+	.owner = THIS_MODULE,
+	.open = notifier_err_errno_open,
+	.read = seq_read,
+	.write = notifier_err_errno_write,
+	.llseek = seq_lseek,
+	.release = single_release,
+};
 
 static struct dentry *debugfs_create_errno(const char *name, umode_t mode,
 				struct dentry *parent, int *value)
-- 
2.34.1



                 reply	other threads:[~2022-09-17  7:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220917070608.28210-1-akinobu.mita@gmail.com \
    --to=akinobu.mita@gmail.com \
    --cc=corbet@lwn.net \
    --cc=david@redhat.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=osalvador@suse.de \
    --cc=shuah@kernel.org \
    --cc=weiyongjun1@huawei.com \
    --cc=zhaogongyi@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox