9b5a4bb88c6491909dfcbc1dde379cba954d2575
[deliverable/linux.git] / include / linux / kernfs.h
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
10 #include <linux/kernel.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/idr.h>
15 #include <linux/lockdep.h>
16 #include <linux/rbtree.h>
17 #include <linux/atomic.h>
18 #include <linux/wait.h>
19
20 struct file;
21 struct iattr;
22 struct seq_file;
23 struct vm_area_struct;
24 struct super_block;
25 struct file_system_type;
26
27 struct kernfs_open_node;
28 struct kernfs_iattrs;
29
30 enum kernfs_node_type {
31 KERNFS_DIR = 0x0001,
32 KERNFS_FILE = 0x0002,
33 KERNFS_LINK = 0x0004,
34 };
35
36 #define KERNFS_TYPE_MASK 0x000f
37 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
38
39 enum kernfs_node_flag {
40 KERNFS_JUST_DEACTIVATED = 0x0010, /* used to aid lockdep annotation */
41 KERNFS_NS = 0x0020,
42 KERNFS_HAS_SEQ_SHOW = 0x0040,
43 KERNFS_HAS_MMAP = 0x0080,
44 KERNFS_LOCKDEP = 0x0100,
45 KERNFS_STATIC_NAME = 0x0200,
46 };
47
48 /* type-specific structures for kernfs_node union members */
49 struct kernfs_elem_dir {
50 unsigned long subdirs;
51 /* children rbtree starts here and goes through kn->rb */
52 struct rb_root children;
53
54 /*
55 * The kernfs hierarchy this directory belongs to. This fits
56 * better directly in kernfs_node but is here to save space.
57 */
58 struct kernfs_root *root;
59 };
60
61 struct kernfs_elem_symlink {
62 struct kernfs_node *target_kn;
63 };
64
65 struct kernfs_elem_attr {
66 const struct kernfs_ops *ops;
67 struct kernfs_open_node *open;
68 loff_t size;
69 };
70
71 /*
72 * kernfs_node - the building block of kernfs hierarchy. Each and every
73 * kernfs node is represented by single kernfs_node. Most fields are
74 * private to kernfs and shouldn't be accessed directly by kernfs users.
75 *
76 * As long as s_count reference is held, the kernfs_node itself is
77 * accessible. Dereferencing elem or any other outer entity requires
78 * active reference.
79 */
80 struct kernfs_node {
81 atomic_t count;
82 atomic_t active;
83 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 struct lockdep_map dep_map;
85 #endif
86 /* the following two fields are published */
87 struct kernfs_node *parent;
88 const char *name;
89
90 struct rb_node rb;
91
92 const void *ns; /* namespace tag */
93 unsigned int hash; /* ns + name hash */
94 union {
95 struct kernfs_elem_dir dir;
96 struct kernfs_elem_symlink symlink;
97 struct kernfs_elem_attr attr;
98 };
99
100 void *priv;
101
102 unsigned short flags;
103 umode_t mode;
104 unsigned int ino;
105 struct kernfs_iattrs *iattr;
106 };
107
108 /*
109 * kernfs_dir_ops may be specified on kernfs_create_root() to support
110 * directory manipulation syscalls. These optional callbacks are invoked
111 * on the matching syscalls and can perform any kernfs operations which
112 * don't necessarily have to be the exact operation requested.
113 */
114 struct kernfs_dir_ops {
115 int (*mkdir)(struct kernfs_node *parent, const char *name,
116 umode_t mode);
117 int (*rmdir)(struct kernfs_node *kn);
118 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
119 const char *new_name);
120 };
121
122 struct kernfs_root {
123 /* published fields */
124 struct kernfs_node *kn;
125
126 /* private fields, do not use outside kernfs proper */
127 struct ida ino_ida;
128 struct kernfs_dir_ops *dir_ops;
129 wait_queue_head_t deactivate_waitq;
130 };
131
132 struct kernfs_open_file {
133 /* published fields */
134 struct kernfs_node *kn;
135 struct file *file;
136
137 /* private fields, do not use outside kernfs proper */
138 struct mutex mutex;
139 int event;
140 struct list_head list;
141
142 bool mmapped;
143 const struct vm_operations_struct *vm_ops;
144 };
145
146 struct kernfs_ops {
147 /*
148 * Read is handled by either seq_file or raw_read().
149 *
150 * If seq_show() is present, seq_file path is active. Other seq
151 * operations are optional and if not implemented, the behavior is
152 * equivalent to single_open(). @sf->private points to the
153 * associated kernfs_open_file.
154 *
155 * read() is bounced through kernel buffer and a read larger than
156 * PAGE_SIZE results in partial operation of PAGE_SIZE.
157 */
158 int (*seq_show)(struct seq_file *sf, void *v);
159
160 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
161 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
162 void (*seq_stop)(struct seq_file *sf, void *v);
163
164 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
165 loff_t off);
166
167 /*
168 * write() is bounced through kernel buffer and a write larger than
169 * PAGE_SIZE results in partial operation of PAGE_SIZE.
170 */
171 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
172 loff_t off);
173
174 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
175
176 #ifdef CONFIG_DEBUG_LOCK_ALLOC
177 struct lock_class_key lockdep_key;
178 #endif
179 };
180
181 #ifdef CONFIG_SYSFS
182
183 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
184 {
185 return kn->flags & KERNFS_TYPE_MASK;
186 }
187
188 /**
189 * kernfs_enable_ns - enable namespace under a directory
190 * @kn: directory of interest, should be empty
191 *
192 * This is to be called right after @kn is created to enable namespace
193 * under it. All children of @kn must have non-NULL namespace tags and
194 * only the ones which match the super_block's tag will be visible.
195 */
196 static inline void kernfs_enable_ns(struct kernfs_node *kn)
197 {
198 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
199 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
200 kn->flags |= KERNFS_NS;
201 }
202
203 /**
204 * kernfs_ns_enabled - test whether namespace is enabled
205 * @kn: the node to test
206 *
207 * Test whether namespace filtering is enabled for the children of @ns.
208 */
209 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
210 {
211 return kn->flags & KERNFS_NS;
212 }
213
214 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
215 const char *name, const void *ns);
216 void kernfs_get(struct kernfs_node *kn);
217 void kernfs_put(struct kernfs_node *kn);
218
219 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
220 void *priv);
221 void kernfs_destroy_root(struct kernfs_root *root);
222
223 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
224 const char *name, umode_t mode,
225 void *priv, const void *ns);
226 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
227 const char *name,
228 umode_t mode, loff_t size,
229 const struct kernfs_ops *ops,
230 void *priv, const void *ns,
231 bool name_is_static,
232 struct lock_class_key *key);
233 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
234 const char *name,
235 struct kernfs_node *target);
236 void kernfs_remove(struct kernfs_node *kn);
237 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
238 const void *ns);
239 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
240 const char *new_name, const void *new_ns);
241 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
242 void kernfs_notify(struct kernfs_node *kn);
243
244 const void *kernfs_super_ns(struct super_block *sb);
245 struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
246 struct kernfs_root *root, const void *ns);
247 void kernfs_kill_sb(struct super_block *sb);
248
249 void kernfs_init(void);
250
251 #else /* CONFIG_SYSFS */
252
253 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
254 { return 0; } /* whatever */
255
256 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
257
258 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
259 { return false; }
260
261 static inline struct kernfs_node *
262 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
263 const void *ns)
264 { return NULL; }
265
266 static inline void kernfs_get(struct kernfs_node *kn) { }
267 static inline void kernfs_put(struct kernfs_node *kn) { }
268
269 static inline struct kernfs_root *
270 kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
271 { return ERR_PTR(-ENOSYS); }
272
273 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
274
275 static inline struct kernfs_node *
276 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
277 umode_t mode, void *priv, const void *ns)
278 { return ERR_PTR(-ENOSYS); }
279
280 static inline struct kernfs_node *
281 __kernfs_create_file(struct kernfs_node *parent, const char *name,
282 umode_t mode, loff_t size, const struct kernfs_ops *ops,
283 void *priv, const void *ns, bool name_is_static,
284 struct lock_class_key *key)
285 { return ERR_PTR(-ENOSYS); }
286
287 static inline struct kernfs_node *
288 kernfs_create_link(struct kernfs_node *parent, const char *name,
289 struct kernfs_node *target)
290 { return ERR_PTR(-ENOSYS); }
291
292 static inline void kernfs_remove(struct kernfs_node *kn) { }
293
294 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
295 const char *name, const void *ns)
296 { return -ENOSYS; }
297
298 static inline int kernfs_rename_ns(struct kernfs_node *kn,
299 struct kernfs_node *new_parent,
300 const char *new_name, const void *new_ns)
301 { return -ENOSYS; }
302
303 static inline int kernfs_setattr(struct kernfs_node *kn,
304 const struct iattr *iattr)
305 { return -ENOSYS; }
306
307 static inline void kernfs_notify(struct kernfs_node *kn) { }
308
309 static inline const void *kernfs_super_ns(struct super_block *sb)
310 { return NULL; }
311
312 static inline struct dentry *
313 kernfs_mount_ns(struct file_system_type *fs_type, int flags,
314 struct kernfs_root *root, const void *ns)
315 { return ERR_PTR(-ENOSYS); }
316
317 static inline void kernfs_kill_sb(struct super_block *sb) { }
318
319 static inline void kernfs_init(void) { }
320
321 #endif /* CONFIG_SYSFS */
322
323 static inline struct kernfs_node *
324 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
325 {
326 return kernfs_find_and_get_ns(kn, name, NULL);
327 }
328
329 static inline struct kernfs_node *
330 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
331 void *priv)
332 {
333 return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
334 }
335
336 static inline struct kernfs_node *
337 kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
338 umode_t mode, loff_t size, const struct kernfs_ops *ops,
339 void *priv, const void *ns)
340 {
341 struct lock_class_key *key = NULL;
342
343 #ifdef CONFIG_DEBUG_LOCK_ALLOC
344 key = (struct lock_class_key *)&ops->lockdep_key;
345 #endif
346 return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
347 false, key);
348 }
349
350 static inline struct kernfs_node *
351 kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
352 loff_t size, const struct kernfs_ops *ops, void *priv)
353 {
354 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
355 }
356
357 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
358 const char *name)
359 {
360 return kernfs_remove_by_name_ns(parent, name, NULL);
361 }
362
363 static inline struct dentry *
364 kernfs_mount(struct file_system_type *fs_type, int flags,
365 struct kernfs_root *root)
366 {
367 return kernfs_mount_ns(fs_type, flags, root, NULL);
368 }
369
370 #endif /* __LINUX_KERNFS_H */
This page took 0.037461 seconds and 4 git commands to generate.