Commit | Line | Data |
---|---|---|
b8441ed2 TH |
1 | /* |
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 | ||
879f40d1 | 10 | #include <linux/kernel.h> |
5d0e26bb | 11 | #include <linux/err.h> |
dd8a5b03 TH |
12 | #include <linux/list.h> |
13 | #include <linux/mutex.h> | |
bc755553 | 14 | #include <linux/idr.h> |
517e64f5 | 15 | #include <linux/lockdep.h> |
879f40d1 | 16 | |
5d60418e TH |
17 | struct file; |
18 | struct iattr; | |
dd8a5b03 TH |
19 | struct seq_file; |
20 | struct vm_area_struct; | |
4b93dc9b TH |
21 | struct super_block; |
22 | struct file_system_type; | |
5d60418e | 23 | |
b8441ed2 TH |
24 | struct sysfs_dirent; |
25 | ||
ba7443bc TH |
26 | struct kernfs_root { |
27 | /* published fields */ | |
28 | struct sysfs_dirent *sd; | |
bc755553 TH |
29 | |
30 | /* private fields, do not use outside kernfs proper */ | |
31 | struct ida ino_ida; | |
ba7443bc TH |
32 | }; |
33 | ||
dd8a5b03 TH |
34 | struct sysfs_open_file { |
35 | /* published fields */ | |
36 | struct sysfs_dirent *sd; | |
37 | struct file *file; | |
38 | ||
39 | /* private fields, do not use outside kernfs proper */ | |
40 | struct mutex mutex; | |
41 | int event; | |
42 | struct list_head list; | |
43 | ||
44 | bool mmapped; | |
45 | const struct vm_operations_struct *vm_ops; | |
46 | }; | |
47 | ||
f6acf8bb TH |
48 | struct kernfs_ops { |
49 | /* | |
50 | * Read is handled by either seq_file or raw_read(). | |
51 | * | |
d19b9846 TH |
52 | * If seq_show() is present, seq_file path is active. Other seq |
53 | * operations are optional and if not implemented, the behavior is | |
54 | * equivalent to single_open(). @sf->private points to the | |
f6acf8bb TH |
55 | * associated sysfs_open_file. |
56 | * | |
57 | * read() is bounced through kernel buffer and a read larger than | |
58 | * PAGE_SIZE results in partial operation of PAGE_SIZE. | |
59 | */ | |
60 | int (*seq_show)(struct seq_file *sf, void *v); | |
d19b9846 TH |
61 | |
62 | void *(*seq_start)(struct seq_file *sf, loff_t *ppos); | |
63 | void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos); | |
64 | void (*seq_stop)(struct seq_file *sf, void *v); | |
f6acf8bb TH |
65 | |
66 | ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes, | |
67 | loff_t off); | |
68 | ||
69 | /* | |
70 | * write() is bounced through kernel buffer and a write larger than | |
71 | * PAGE_SIZE results in partial operation of PAGE_SIZE. | |
72 | */ | |
73 | ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes, | |
74 | loff_t off); | |
75 | ||
76 | int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma); | |
517e64f5 TH |
77 | |
78 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
79 | struct lock_class_key lockdep_key; | |
80 | #endif | |
f6acf8bb TH |
81 | }; |
82 | ||
879f40d1 TH |
83 | #ifdef CONFIG_SYSFS |
84 | ||
ccf73cf3 TH |
85 | struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent, |
86 | const char *name, const void *ns); | |
87 | void kernfs_get(struct sysfs_dirent *sd); | |
88 | void kernfs_put(struct sysfs_dirent *sd); | |
89 | ||
ba7443bc TH |
90 | struct kernfs_root *kernfs_create_root(void *priv); |
91 | void kernfs_destroy_root(struct kernfs_root *root); | |
92 | ||
93b2b8e4 TH |
93 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
94 | const char *name, void *priv, | |
95 | const void *ns); | |
517e64f5 TH |
96 | struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent, |
97 | const char *name, | |
98 | umode_t mode, loff_t size, | |
99 | const struct kernfs_ops *ops, | |
100 | void *priv, const void *ns, | |
101 | struct lock_class_key *key); | |
5d0e26bb TH |
102 | struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, |
103 | const char *name, | |
104 | struct sysfs_dirent *target); | |
879f40d1 TH |
105 | void kernfs_remove(struct sysfs_dirent *sd); |
106 | int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, | |
107 | const void *ns); | |
890ece16 TH |
108 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
109 | const char *new_name, const void *new_ns); | |
93b2b8e4 | 110 | void kernfs_enable_ns(struct sysfs_dirent *sd); |
5d60418e | 111 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); |
024f6471 | 112 | void kernfs_notify(struct sysfs_dirent *sd); |
879f40d1 | 113 | |
4b93dc9b TH |
114 | const void *kernfs_super_ns(struct super_block *sb); |
115 | struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, | |
116 | struct kernfs_root *root, const void *ns); | |
117 | void kernfs_kill_sb(struct super_block *sb); | |
118 | ||
119 | void kernfs_init(void); | |
120 | ||
879f40d1 TH |
121 | #else /* CONFIG_SYSFS */ |
122 | ||
ccf73cf3 TH |
123 | static inline struct sysfs_dirent * |
124 | kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name, | |
125 | const void *ns) | |
126 | { return NULL; } | |
127 | ||
128 | static inline void kernfs_get(struct sysfs_dirent *sd) { } | |
129 | static inline void kernfs_put(struct sysfs_dirent *sd) { } | |
130 | ||
ba7443bc TH |
131 | static inline struct kernfs_root *kernfs_create_root(void *priv) |
132 | { return ERR_PTR(-ENOSYS); } | |
133 | ||
134 | static inline void kernfs_destroy_root(struct kernfs_root *root) { } | |
135 | ||
93b2b8e4 TH |
136 | static inline struct sysfs_dirent * |
137 | kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv, | |
138 | const void *ns) | |
139 | { return ERR_PTR(-ENOSYS); } | |
140 | ||
496f7394 | 141 | static inline struct sysfs_dirent * |
517e64f5 TH |
142 | kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name, |
143 | umode_t mode, loff_t size, | |
144 | const struct kernfs_ops *ops, void *priv, | |
145 | const void *ns, struct lock_class_key *key) | |
496f7394 TH |
146 | { return ERR_PTR(-ENOSYS); } |
147 | ||
5d0e26bb TH |
148 | static inline struct sysfs_dirent * |
149 | kernfs_create_link(struct sysfs_dirent *parent, const char *name, | |
150 | struct sysfs_dirent *target) | |
151 | { return ERR_PTR(-ENOSYS); } | |
152 | ||
879f40d1 TH |
153 | static inline void kernfs_remove(struct sysfs_dirent *sd) { } |
154 | ||
155 | static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, | |
156 | const char *name, const void *ns) | |
157 | { return -ENOSYS; } | |
158 | ||
890ece16 TH |
159 | static inline int kernfs_rename_ns(struct sysfs_dirent *sd, |
160 | struct sysfs_dirent *new_parent, | |
161 | const char *new_name, const void *new_ns) | |
162 | { return -ENOSYS; } | |
163 | ||
93b2b8e4 TH |
164 | static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { } |
165 | ||
5d60418e TH |
166 | static inline int kernfs_setattr(struct sysfs_dirent *sd, |
167 | const struct iattr *iattr) | |
168 | { return -ENOSYS; } | |
169 | ||
024f6471 TH |
170 | static inline void kernfs_notify(struct sysfs_dirent *sd) { } |
171 | ||
4b93dc9b TH |
172 | static inline const void *kernfs_super_ns(struct super_block *sb) |
173 | { return NULL; } | |
174 | ||
175 | static inline struct dentry * | |
176 | kernfs_mount_ns(struct file_system_type *fs_type, int flags, | |
177 | struct kernfs_root *root, const void *ns) | |
178 | { return ERR_PTR(-ENOSYS); } | |
179 | ||
180 | static inline void kernfs_kill_sb(struct super_block *sb) { } | |
181 | ||
182 | static inline void kernfs_init(void) { } | |
183 | ||
879f40d1 TH |
184 | #endif /* CONFIG_SYSFS */ |
185 | ||
93b2b8e4 | 186 | static inline struct sysfs_dirent * |
ccf73cf3 TH |
187 | kernfs_find_and_get(struct sysfs_dirent *sd, const char *name) |
188 | { | |
189 | return kernfs_find_and_get_ns(sd, name, NULL); | |
190 | } | |
191 | ||
192 | static inline struct sysfs_dirent * | |
93b2b8e4 TH |
193 | kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv) |
194 | { | |
195 | return kernfs_create_dir_ns(parent, name, priv, NULL); | |
196 | } | |
197 | ||
517e64f5 TH |
198 | static inline struct sysfs_dirent * |
199 | kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name, | |
200 | umode_t mode, loff_t size, const struct kernfs_ops *ops, | |
201 | void *priv, const void *ns) | |
202 | { | |
203 | struct lock_class_key *key = NULL; | |
204 | ||
205 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
206 | key = (struct lock_class_key *)&ops->lockdep_key; | |
207 | #endif | |
208 | return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv, | |
209 | ns, key); | |
210 | } | |
211 | ||
496f7394 TH |
212 | static inline struct sysfs_dirent * |
213 | kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode, | |
214 | loff_t size, const struct kernfs_ops *ops, void *priv) | |
215 | { | |
216 | return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL); | |
217 | } | |
218 | ||
879f40d1 TH |
219 | static inline int kernfs_remove_by_name(struct sysfs_dirent *parent, |
220 | const char *name) | |
221 | { | |
222 | return kernfs_remove_by_name_ns(parent, name, NULL); | |
223 | } | |
224 | ||
4b93dc9b TH |
225 | static inline struct dentry * |
226 | kernfs_mount(struct file_system_type *fs_type, int flags, | |
227 | struct kernfs_root *root) | |
228 | { | |
229 | return kernfs_mount_ns(fs_type, flags, root, NULL); | |
230 | } | |
231 | ||
b8441ed2 | 232 | #endif /* __LINUX_KERNFS_H */ |