Skip to content

Commit f6acf8b

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: introduce kernfs_ops
We're in the process of separating out core sysfs functionality into kernfs which will deal with sysfs_dirents directly. This patch introduces kernfs_ops which hosts methods kernfs users implement and updates fs/sysfs/file.c such that sysfs_kf_*() functions populate kernfs_ops and kernfs_file_*() functions call the matching entries from kernfs_ops. kernfs_ops contains the following groups of methods. * seq_show() - for kernfs files which use seq_file for reads. * read() - for direct read implementations. Used iff seq_show() is not implemented. * write() - for writes. * mmap() - for mmaps. Notes: * sysfs_elem_attr->ops is added so that kernfs_ops can be accessed from sysfs_dirent. kernfs_ops() helper is added to verify locking and access the field. * SYSFS_FLAG_HAS_(SEQ_SHOW|MMAP) added. sd->s_attr->ops is accessible only while holding active_ref and there are cases where we want to take different actions depending on which ops are implemented. These two flags cache whether the two ops are implemented for those. * kernfs_file_*() no longer test sysfs type but chooses different behaviors depending on which methods in kernfs_ops are implemented. The conversions are trivial except for the open path. As kernfs_file_open() now decides whether to allow read/write accesses depending on the kernfs_ops implemented, the presence of methods in kobjs and attribute_bin should be propagated to kernfs_ops. sysfs_add_file_mode_ns() is updated so that it propagates presence / absence of the callbacks through _empty, _ro, _wo, _rw kernfs_ops. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dd8a5b0 commit f6acf8b

3 files changed

Lines changed: 141 additions & 34 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 112 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ static struct sysfs_open_file *sysfs_of(struct file *file)
5757
return ((struct seq_file *)file->private_data)->private;
5858
}
5959

60+
/*
61+
* Determine the kernfs_ops for the given sysfs_dirent. This function must
62+
* be called while holding an active reference.
63+
*/
64+
static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd)
65+
{
66+
if (!sysfs_ignore_lockdep(sd))
67+
lockdep_assert_held(sd);
68+
return sd->s_attr.ops;
69+
}
70+
6071
/*
6172
* Determine ktype->sysfs_ops for the given sysfs_dirent. This function
6273
* must be called while holding an active reference.
@@ -180,7 +191,7 @@ static int kernfs_seq_show(struct seq_file *sf, void *v)
180191

181192
of->event = atomic_read(&of->sd->s_attr.open->event);
182193

183-
return sysfs_kf_seq_show(sf, v);
194+
return of->sd->s_attr.ops->seq_show(sf, v);
184195
}
185196

186197
static const struct seq_operations kernfs_seq_ops = {
@@ -201,6 +212,7 @@ static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
201212
loff_t *ppos)
202213
{
203214
ssize_t len = min_t(size_t, count, PAGE_SIZE);
215+
const struct kernfs_ops *ops;
204216
char *buf;
205217

206218
buf = kmalloc(len, GFP_KERNEL);
@@ -218,7 +230,11 @@ static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
218230
goto out_free;
219231
}
220232

221-
len = sysfs_kf_bin_read(of, buf, len, *ppos);
233+
ops = kernfs_ops(of->sd);
234+
if (ops->read)
235+
len = ops->read(of, buf, len, *ppos);
236+
else
237+
len = -EINVAL;
222238

223239
sysfs_put_active(of->sd);
224240
mutex_unlock(&of->mutex);
@@ -250,10 +266,10 @@ static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
250266
{
251267
struct sysfs_open_file *of = sysfs_of(file);
252268

253-
if (sysfs_is_bin(of->sd))
254-
return kernfs_file_direct_read(of, user_buf, count, ppos);
255-
else
269+
if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
256270
return seq_read(file, user_buf, count, ppos);
271+
else
272+
return kernfs_file_direct_read(of, user_buf, count, ppos);
257273
}
258274

259275
/* kernfs write callback for regular sysfs files */
@@ -312,6 +328,7 @@ static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
312328
{
313329
struct sysfs_open_file *of = sysfs_of(file);
314330
ssize_t len = min_t(size_t, count, PAGE_SIZE);
331+
const struct kernfs_ops *ops;
315332
char *buf;
316333

317334
buf = kmalloc(len + 1, GFP_KERNEL);
@@ -335,10 +352,11 @@ static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
335352
goto out_free;
336353
}
337354

338-
if (sysfs_is_bin(of->sd))
339-
len = sysfs_kf_bin_write(of, buf, len, *ppos);
355+
ops = kernfs_ops(of->sd);
356+
if (ops->write)
357+
len = ops->write(of, buf, len, *ppos);
340358
else
341-
len = sysfs_kf_write(of, buf, len, *ppos);
359+
len = -EINVAL;
342360

343361
sysfs_put_active(of->sd);
344362
mutex_unlock(&of->mutex);
@@ -524,6 +542,7 @@ static const struct vm_operations_struct kernfs_vm_ops = {
524542
static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
525543
{
526544
struct sysfs_open_file *of = sysfs_of(file);
545+
const struct kernfs_ops *ops;
527546
int rc;
528547

529548
mutex_lock(&of->mutex);
@@ -532,8 +551,9 @@ static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
532551
if (!sysfs_get_active(of->sd))
533552
goto out_unlock;
534553

535-
if (sysfs_is_bin(of->sd))
536-
rc = sysfs_kf_bin_mmap(of, vma);
554+
ops = kernfs_ops(of->sd);
555+
if (ops->mmap)
556+
rc = ops->mmap(of, vma);
537557
if (rc)
538558
goto out_put;
539559

@@ -660,34 +680,19 @@ static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
660680
static int kernfs_file_open(struct inode *inode, struct file *file)
661681
{
662682
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
663-
struct kobject *kobj = attr_sd->s_parent->priv;
683+
const struct kernfs_ops *ops;
664684
struct sysfs_open_file *of;
665685
bool has_read, has_write, has_mmap;
666686
int error = -EACCES;
667687

668-
/* need attr_sd for attr and ops, its parent for kobj */
669688
if (!sysfs_get_active(attr_sd))
670689
return -ENODEV;
671690

672-
if (sysfs_is_bin(attr_sd)) {
673-
struct bin_attribute *battr = attr_sd->priv;
691+
ops = kernfs_ops(attr_sd);
674692

675-
has_read = battr->read || battr->mmap;
676-
has_write = battr->write || battr->mmap;
677-
has_mmap = battr->mmap;
678-
} else {
679-
const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
680-
681-
/* every kobject with an attribute needs a ktype assigned */
682-
if (WARN(!ops, KERN_ERR
683-
"missing sysfs attribute operations for kobject: %s\n",
684-
kobject_name(kobj)))
685-
goto err_out;
686-
687-
has_read = ops->show;
688-
has_write = ops->store;
689-
has_mmap = false;
690-
}
693+
has_read = ops->seq_show || ops->read || ops->mmap;
694+
has_write = ops->write || ops->mmap;
695+
has_mmap = ops->mmap;
691696

692697
/* check perms and supported operations */
693698
if ((file->f_mode & FMODE_WRITE) &&
@@ -729,10 +734,10 @@ static int kernfs_file_open(struct inode *inode, struct file *file)
729734
* seq_file or is not requested. This unifies private data access
730735
* and readable regular files are the vast majority anyway.
731736
*/
732-
if (sysfs_is_bin(attr_sd))
733-
error = seq_open(file, NULL);
734-
else
737+
if (ops->seq_show)
735738
error = seq_open(file, &kernfs_seq_ops);
739+
else
740+
error = seq_open(file, NULL);
736741
if (error)
737742
goto err_free;
738743

@@ -777,7 +782,7 @@ void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
777782
struct sysfs_open_dirent *od;
778783
struct sysfs_open_file *of;
779784

780-
if (!sysfs_is_bin(sd))
785+
if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
781786
return;
782787

783788
spin_lock_irq(&sysfs_open_dirent_lock);
@@ -880,23 +885,96 @@ const struct file_operations kernfs_file_operations = {
880885
.poll = kernfs_file_poll,
881886
};
882887

888+
static const struct kernfs_ops sysfs_file_kfops_empty = {
889+
};
890+
891+
static const struct kernfs_ops sysfs_file_kfops_ro = {
892+
.seq_show = sysfs_kf_seq_show,
893+
};
894+
895+
static const struct kernfs_ops sysfs_file_kfops_wo = {
896+
.write = sysfs_kf_write,
897+
};
898+
899+
static const struct kernfs_ops sysfs_file_kfops_rw = {
900+
.seq_show = sysfs_kf_seq_show,
901+
.write = sysfs_kf_write,
902+
};
903+
904+
static const struct kernfs_ops sysfs_bin_kfops_ro = {
905+
.read = sysfs_kf_bin_read,
906+
};
907+
908+
static const struct kernfs_ops sysfs_bin_kfops_wo = {
909+
.write = sysfs_kf_bin_write,
910+
};
911+
912+
static const struct kernfs_ops sysfs_bin_kfops_rw = {
913+
.read = sysfs_kf_bin_read,
914+
.write = sysfs_kf_bin_write,
915+
.mmap = sysfs_kf_bin_mmap,
916+
};
917+
883918
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
884919
const struct attribute *attr, int type,
885920
umode_t amode, const void *ns)
886921
{
887922
umode_t mode = (amode & S_IALLUGO) | S_IFREG;
923+
const struct kernfs_ops *ops;
888924
struct sysfs_addrm_cxt acxt;
889925
struct sysfs_dirent *sd;
890926
int rc;
891927

928+
if (type == SYSFS_KOBJ_ATTR) {
929+
struct kobject *kobj = dir_sd->priv;
930+
const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
931+
932+
/* every kobject with an attribute needs a ktype assigned */
933+
if (WARN(!sysfs_ops, KERN_ERR
934+
"missing sysfs attribute operations for kobject: %s\n",
935+
kobject_name(kobj)))
936+
return -EINVAL;
937+
938+
if (sysfs_ops->show && sysfs_ops->store)
939+
ops = &sysfs_file_kfops_rw;
940+
else if (sysfs_ops->show)
941+
ops = &sysfs_file_kfops_ro;
942+
else if (sysfs_ops->store)
943+
ops = &sysfs_file_kfops_wo;
944+
else
945+
ops = &sysfs_file_kfops_empty;
946+
} else {
947+
struct bin_attribute *battr = (void *)attr;
948+
949+
if ((battr->read && battr->write) || battr->mmap)
950+
ops = &sysfs_bin_kfops_rw;
951+
else if (battr->read)
952+
ops = &sysfs_bin_kfops_ro;
953+
else if (battr->write)
954+
ops = &sysfs_bin_kfops_wo;
955+
else
956+
ops = &sysfs_file_kfops_empty;
957+
}
958+
892959
sd = sysfs_new_dirent(attr->name, mode, type);
893960
if (!sd)
894961
return -ENOMEM;
895962

963+
sd->s_attr.ops = ops;
896964
sd->s_ns = ns;
897965
sd->priv = (void *)attr;
898966
sysfs_dirent_init_lockdep(sd);
899967

968+
/*
969+
* sd->s_attr.ops is accesible only while holding active ref. We
970+
* need to know whether some ops are implemented outside active
971+
* ref. Cache their existence in flags.
972+
*/
973+
if (ops->seq_show)
974+
sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
975+
if (ops->mmap)
976+
sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
977+
900978
sysfs_addrm_start(&acxt);
901979
rc = sysfs_add_one(&acxt, sd, dir_sd);
902980
sysfs_addrm_finish(&acxt);

‎fs/sysfs/sysfs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct sysfs_elem_symlink {
2727
};
2828

2929
struct sysfs_elem_attr {
30+
const struct kernfs_ops *ops;
3031
struct sysfs_open_dirent *open;
3132
};
3233

@@ -89,6 +90,8 @@ struct sysfs_dirent {
8990
#define SYSFS_FLAG_MASK ~SYSFS_TYPE_MASK
9091
#define SYSFS_FLAG_NS 0x01000
9192
#define SYSFS_FLAG_REMOVED 0x02000
93+
#define SYSFS_FLAG_HAS_SEQ_SHOW 0x04000
94+
#define SYSFS_FLAG_HAS_MMAP 0x08000
9295

9396
static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
9497
{

‎include/linux/kernfs.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ struct sysfs_open_file {
3333
const struct vm_operations_struct *vm_ops;
3434
};
3535

36+
struct kernfs_ops {
37+
/*
38+
* Read is handled by either seq_file or raw_read().
39+
*
40+
* If seq_show() is present, seq_file path is active. The behavior
41+
* is equivalent to single_open(). @sf->private points to the
42+
* associated sysfs_open_file.
43+
*
44+
* read() is bounced through kernel buffer and a read larger than
45+
* PAGE_SIZE results in partial operation of PAGE_SIZE.
46+
*/
47+
int (*seq_show)(struct seq_file *sf, void *v);
48+
49+
ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
50+
loff_t off);
51+
52+
/*
53+
* write() is bounced through kernel buffer and a write larger than
54+
* PAGE_SIZE results in partial operation of PAGE_SIZE.
55+
*/
56+
ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
57+
loff_t off);
58+
59+
int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
60+
};
61+
3662
#ifdef CONFIG_SYSFS
3763

3864
struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,

0 commit comments

Comments
 (0)