Skip to content

Commit d19b984

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: add kernfs_ops->seq_{start|next|stop}()
kernfs_ops currently only supports single_open() behavior which is pretty restrictive. Add optional callbacks ->seq_{start|next|stop}() which, when implemented, are invoked for seq_file traversal. This allows full seq_file functionality for kernfs users. This currently doesn't have any user and doesn't change any behavior. v2: Refreshed on top of the updated "sysfs, kernfs: prepare read path for kernfs". Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 2d0cfbe commit d19b984

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
146146
static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
147147
{
148148
struct sysfs_open_file *of = sf->private;
149+
const struct kernfs_ops *ops;
149150

150151
/*
151152
* @of->mutex nests outside active ref and is just to ensure that
@@ -155,26 +156,42 @@ static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
155156
if (!sysfs_get_active(of->sd))
156157
return ERR_PTR(-ENODEV);
157158

158-
/*
159-
* The same behavior and code as single_open(). Returns !NULL if
160-
* pos is at the beginning; otherwise, NULL.
161-
*/
162-
return NULL + !*ppos;
159+
ops = kernfs_ops(of->sd);
160+
if (ops->seq_start) {
161+
return ops->seq_start(sf, ppos);
162+
} else {
163+
/*
164+
* The same behavior and code as single_open(). Returns
165+
* !NULL if pos is at the beginning; otherwise, NULL.
166+
*/
167+
return NULL + !*ppos;
168+
}
163169
}
164170

165171
static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
166172
{
167-
/*
168-
* The same behavior and code as single_open(), always terminate
169-
* after the initial read.
170-
*/
171-
++*ppos;
172-
return NULL;
173+
struct sysfs_open_file *of = sf->private;
174+
const struct kernfs_ops *ops = kernfs_ops(of->sd);
175+
176+
if (ops->seq_next) {
177+
return ops->seq_next(sf, v, ppos);
178+
} else {
179+
/*
180+
* The same behavior and code as single_open(), always
181+
* terminate after the initial read.
182+
*/
183+
++*ppos;
184+
return NULL;
185+
}
173186
}
174187

175188
static void kernfs_seq_stop(struct seq_file *sf, void *v)
176189
{
177190
struct sysfs_open_file *of = sf->private;
191+
const struct kernfs_ops *ops = kernfs_ops(of->sd);
192+
193+
if (ops->seq_stop)
194+
ops->seq_stop(sf, v);
178195

179196
sysfs_put_active(of->sd);
180197
mutex_unlock(&of->mutex);

‎include/linux/kernfs.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ struct kernfs_ops {
3737
/*
3838
* Read is handled by either seq_file or raw_read().
3939
*
40-
* If seq_show() is present, seq_file path is active. The behavior
41-
* is equivalent to single_open(). @sf->private points to the
40+
* If seq_show() is present, seq_file path is active. Other seq
41+
* operations are optional and if not implemented, the behavior is
42+
* equivalent to single_open(). @sf->private points to the
4243
* associated sysfs_open_file.
4344
*
4445
* read() is bounced through kernel buffer and a read larger than
4546
* PAGE_SIZE results in partial operation of PAGE_SIZE.
4647
*/
4748
int (*seq_show)(struct seq_file *sf, void *v);
4849

50+
void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
51+
void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
52+
void (*seq_stop)(struct seq_file *sf, void *v);
53+
4954
ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
5055
loff_t off);
5156

0 commit comments

Comments
 (0)