blob: f20796ecc76ea02ecced284c2febf0cf056eed18 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
2 * kernfs.h - pseudo filesystem decoupled from vfs locking
3 *
4 * This file is released under the GPLv2.
5 */
6
7#ifndef __LINUX_KERNFS_H
8#define __LINUX_KERNFS_H
9
Tejun Heo879f40d2013-11-23 17:21:49 -050010#include <linux/kernel.h>
Tejun Heo5d0e26b2013-11-23 17:21:50 -050011#include <linux/err.h>
Tejun Heodd8a5b02013-11-28 14:54:20 -050012#include <linux/list.h>
13#include <linux/mutex.h>
Tejun Heo879f40d2013-11-23 17:21:49 -050014
Tejun Heo5d604182013-11-23 17:21:52 -050015struct file;
16struct iattr;
Tejun Heodd8a5b02013-11-28 14:54:20 -050017struct seq_file;
18struct vm_area_struct;
Tejun Heo5d604182013-11-23 17:21:52 -050019
Tejun Heob8441ed2013-11-24 09:54:58 -050020struct sysfs_dirent;
21
Tejun Heodd8a5b02013-11-28 14:54:20 -050022struct sysfs_open_file {
23 /* published fields */
24 struct sysfs_dirent *sd;
25 struct file *file;
26
27 /* private fields, do not use outside kernfs proper */
28 struct mutex mutex;
29 int event;
30 struct list_head list;
31
32 bool mmapped;
33 const struct vm_operations_struct *vm_ops;
34};
35
Tejun Heof6acf8b2013-11-28 14:54:21 -050036struct kernfs_ops {
37 /*
38 * Read is handled by either seq_file or raw_read().
39 *
Tejun Heod19b9842013-11-28 14:54:26 -050040 * 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
Tejun Heof6acf8b2013-11-28 14:54:21 -050043 * associated sysfs_open_file.
44 *
45 * read() is bounced through kernel buffer and a read larger than
46 * PAGE_SIZE results in partial operation of PAGE_SIZE.
47 */
48 int (*seq_show)(struct seq_file *sf, void *v);
49
Tejun Heod19b9842013-11-28 14:54:26 -050050 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
Tejun Heof6acf8b2013-11-28 14:54:21 -050054 ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
55 loff_t off);
56
57 /*
58 * write() is bounced through kernel buffer and a write larger than
59 * PAGE_SIZE results in partial operation of PAGE_SIZE.
60 */
61 ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
62 loff_t off);
63
64 int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
65};
66
Tejun Heo879f40d2013-11-23 17:21:49 -050067#ifdef CONFIG_SYSFS
68
Tejun Heo93b2b8e2013-11-28 14:54:15 -050069struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
70 const char *name, void *priv,
71 const void *ns);
Tejun Heo496f7392013-11-28 14:54:24 -050072struct sysfs_dirent *kernfs_create_file_ns(struct sysfs_dirent *parent,
73 const char *name,
74 umode_t mode, loff_t size,
75 const struct kernfs_ops *ops,
76 void *priv, const void *ns);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050077struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
78 const char *name,
79 struct sysfs_dirent *target);
Tejun Heo879f40d2013-11-23 17:21:49 -050080void kernfs_remove(struct sysfs_dirent *sd);
81int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
82 const void *ns);
Tejun Heo890ece12013-11-23 17:21:51 -050083int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
84 const char *new_name, const void *new_ns);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050085void kernfs_enable_ns(struct sysfs_dirent *sd);
Tejun Heo5d604182013-11-23 17:21:52 -050086int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
Tejun Heo024f6472013-11-28 14:54:27 -050087void kernfs_notify(struct sysfs_dirent *sd);
Tejun Heo879f40d2013-11-23 17:21:49 -050088
89#else /* CONFIG_SYSFS */
90
Tejun Heo5d0e26b2013-11-23 17:21:50 -050091static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -050092kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
93 const void *ns)
94{ return ERR_PTR(-ENOSYS); }
95
96static inline struct sysfs_dirent *
Tejun Heo496f7392013-11-28 14:54:24 -050097kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
98 umode_t mode, loff_t size, const struct kernfs_ops *ops,
99 void *priv, const void *ns)
100{ return ERR_PTR(-ENOSYS); }
101
102static inline struct sysfs_dirent *
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500103kernfs_create_link(struct sysfs_dirent *parent, const char *name,
104 struct sysfs_dirent *target)
105{ return ERR_PTR(-ENOSYS); }
106
Tejun Heo879f40d2013-11-23 17:21:49 -0500107static inline void kernfs_remove(struct sysfs_dirent *sd) { }
108
109static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
110 const char *name, const void *ns)
111{ return -ENOSYS; }
112
Tejun Heo890ece12013-11-23 17:21:51 -0500113static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
114 struct sysfs_dirent *new_parent,
115 const char *new_name, const void *new_ns)
116{ return -ENOSYS; }
117
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500118static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
119
Tejun Heo5d604182013-11-23 17:21:52 -0500120static inline int kernfs_setattr(struct sysfs_dirent *sd,
121 const struct iattr *iattr)
122{ return -ENOSYS; }
123
Tejun Heo024f6472013-11-28 14:54:27 -0500124static inline void kernfs_notify(struct sysfs_dirent *sd) { }
125
Tejun Heo879f40d2013-11-23 17:21:49 -0500126#endif /* CONFIG_SYSFS */
127
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500128static inline struct sysfs_dirent *
129kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
130{
131 return kernfs_create_dir_ns(parent, name, priv, NULL);
132}
133
Tejun Heo496f7392013-11-28 14:54:24 -0500134static inline struct sysfs_dirent *
135kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
136 loff_t size, const struct kernfs_ops *ops, void *priv)
137{
138 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
139}
140
Tejun Heo879f40d2013-11-23 17:21:49 -0500141static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
142 const char *name)
143{
144 return kernfs_remove_by_name_ns(parent, name, NULL);
145}
146
Tejun Heob8441ed2013-11-24 09:54:58 -0500147#endif /* __LINUX_KERNFS_H */