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