Add write support to debugfs. Signed-off-by: Junxian Huang <huangjunxian6@hisilicon.com> --- drivers/infiniband/hw/hns/hns_roce_debugfs.c | 38 +++++++++++++++++--- drivers/infiniband/hw/hns/hns_roce_debugfs.h | 1 + 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/drivers/infiniband/hw/hns/hns_roce_debugfs.c b/drivers/infiniband/hw/hns/hns_roce_debugfs.c index 3f0a692e6ac7..32773ffb6f6e 100644 --- a/drivers/infiniband/hw/hns/hns_roce_debugfs.c +++ b/drivers/infiniband/hw/hns/hns_roce_debugfs.c @@ -18,23 +18,50 @@ static int hns_debugfs_seqfile_open(struct inode *inode, struct file *f) return single_open(f, seqfile->read, seqfile->data); } +static ssize_t hns_debugfs_seqfile_write(struct file *file, + const char __user *buffer, + size_t count, loff_t *ppos) +{ + struct hns_debugfs_seqfile *seqfile = file_inode(file)->i_private; + char buf[16] = {}; + + if (!seqfile->write) + return -EOPNOTSUPP; + + if (count >= sizeof(buf)) + return -EINVAL; + + if (copy_from_user(buf, buffer, count)) + return -EFAULT; + + return seqfile->write(buf, count, seqfile->data); +} + static const struct file_operations hns_debugfs_seqfile_fops = { .owner = THIS_MODULE, .open = hns_debugfs_seqfile_open, .release = single_release, .read = seq_read, + .write = hns_debugfs_seqfile_write, .llseek = seq_lseek }; +struct hns_debugfs_rw_ops { + int (*read)(struct seq_file *seq, void *data); + ssize_t (*write)(char *buf, size_t count, void *data); +}; + static void init_debugfs_seqfile(struct hns_debugfs_seqfile *seq, const char *name, struct dentry *parent, - int (*read_fn)(struct seq_file *, void *), + const struct hns_debugfs_rw_ops *ops, void *data) { - seq->read = read_fn; + seq->read = ops->read; + seq->write = ops->write; seq->data = data; - debugfs_create_file(name, 0400, parent, seq, &hns_debugfs_seqfile_fops); + debugfs_create_file(name, ops->write ? 0600 : 0400, parent, + seq, &hns_debugfs_seqfile_fops); } static const char * const sw_stat_info[] = { @@ -75,11 +102,14 @@ static void create_sw_stat_debugfs(struct hns_roce_dev *hr_dev, struct dentry *parent) { struct hns_sw_stat_debugfs *dbgfs = &hr_dev->dbgfs.sw_stat_root; + const struct hns_debugfs_rw_ops ops = { + .read = sw_stat_debugfs_show, + }; dbgfs->root = debugfs_create_dir("sw_stat", parent); init_debugfs_seqfile(&dbgfs->sw_stat, "sw_stat", dbgfs->root, - sw_stat_debugfs_show, hr_dev); + &ops, hr_dev); } /* debugfs for device */ diff --git a/drivers/infiniband/hw/hns/hns_roce_debugfs.h b/drivers/infiniband/hw/hns/hns_roce_debugfs.h index 98e87bd3161e..4e77dea0fbf6 100644 --- a/drivers/infiniband/hw/hns/hns_roce_debugfs.h +++ b/drivers/infiniband/hw/hns/hns_roce_debugfs.h @@ -9,6 +9,7 @@ /* debugfs seqfile */ struct hns_debugfs_seqfile { int (*read)(struct seq_file *seq, void *data); + ssize_t (*write)(char *buf, size_t count, void *data); void *data; }; -- 2.33.0