]> Pileus Git - ~andy/linux/blob - include/linux/kernfs.h
Revert "kernfs: remove KERNFS_REMOVED"
[~andy/linux] / 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_REMOVED          = 0x0010,
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         union {
93                 struct kernfs_node      *removed_list;
94         } u;
95
96         const void              *ns;    /* namespace tag */
97         unsigned int            hash;   /* ns + name hash */
98         union {
99                 struct kernfs_elem_dir          dir;
100                 struct kernfs_elem_symlink      symlink;
101                 struct kernfs_elem_attr         attr;
102         };
103
104         void                    *priv;
105
106         unsigned short          flags;
107         umode_t                 mode;
108         unsigned int            ino;
109         struct kernfs_iattrs    *iattr;
110 };
111
112 /*
113  * kernfs_dir_ops may be specified on kernfs_create_root() to support
114  * directory manipulation syscalls.  These optional callbacks are invoked
115  * on the matching syscalls and can perform any kernfs operations which
116  * don't necessarily have to be the exact operation requested.
117  */
118 struct kernfs_dir_ops {
119         int (*mkdir)(struct kernfs_node *parent, const char *name,
120                      umode_t mode);
121         int (*rmdir)(struct kernfs_node *kn);
122         int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
123                       const char *new_name);
124 };
125
126 struct kernfs_root {
127         /* published fields */
128         struct kernfs_node      *kn;
129
130         /* private fields, do not use outside kernfs proper */
131         struct ida              ino_ida;
132         struct kernfs_dir_ops   *dir_ops;
133         wait_queue_head_t       deactivate_waitq;
134 };
135
136 struct kernfs_open_file {
137         /* published fields */
138         struct kernfs_node      *kn;
139         struct file             *file;
140
141         /* private fields, do not use outside kernfs proper */
142         struct mutex            mutex;
143         int                     event;
144         struct list_head        list;
145
146         bool                    mmapped;
147         const struct vm_operations_struct *vm_ops;
148 };
149
150 struct kernfs_ops {
151         /*
152          * Read is handled by either seq_file or raw_read().
153          *
154          * If seq_show() is present, seq_file path is active.  Other seq
155          * operations are optional and if not implemented, the behavior is
156          * equivalent to single_open().  @sf->private points to the
157          * associated kernfs_open_file.
158          *
159          * read() is bounced through kernel buffer and a read larger than
160          * PAGE_SIZE results in partial operation of PAGE_SIZE.
161          */
162         int (*seq_show)(struct seq_file *sf, void *v);
163
164         void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
165         void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
166         void (*seq_stop)(struct seq_file *sf, void *v);
167
168         ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
169                         loff_t off);
170
171         /*
172          * write() is bounced through kernel buffer and a write larger than
173          * PAGE_SIZE results in partial operation of PAGE_SIZE.
174          */
175         ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
176                          loff_t off);
177
178         int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
179
180 #ifdef CONFIG_DEBUG_LOCK_ALLOC
181         struct lock_class_key   lockdep_key;
182 #endif
183 };
184
185 #ifdef CONFIG_SYSFS
186
187 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
188 {
189         return kn->flags & KERNFS_TYPE_MASK;
190 }
191
192 /**
193  * kernfs_enable_ns - enable namespace under a directory
194  * @kn: directory of interest, should be empty
195  *
196  * This is to be called right after @kn is created to enable namespace
197  * under it.  All children of @kn must have non-NULL namespace tags and
198  * only the ones which match the super_block's tag will be visible.
199  */
200 static inline void kernfs_enable_ns(struct kernfs_node *kn)
201 {
202         WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
203         WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
204         kn->flags |= KERNFS_NS;
205 }
206
207 /**
208  * kernfs_ns_enabled - test whether namespace is enabled
209  * @kn: the node to test
210  *
211  * Test whether namespace filtering is enabled for the children of @ns.
212  */
213 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
214 {
215         return kn->flags & KERNFS_NS;
216 }
217
218 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
219                                            const char *name, const void *ns);
220 void kernfs_get(struct kernfs_node *kn);
221 void kernfs_put(struct kernfs_node *kn);
222
223 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
224                                        void *priv);
225 void kernfs_destroy_root(struct kernfs_root *root);
226
227 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
228                                          const char *name, umode_t mode,
229                                          void *priv, const void *ns);
230 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
231                                          const char *name,
232                                          umode_t mode, loff_t size,
233                                          const struct kernfs_ops *ops,
234                                          void *priv, const void *ns,
235                                          bool name_is_static,
236                                          struct lock_class_key *key);
237 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
238                                        const char *name,
239                                        struct kernfs_node *target);
240 void kernfs_remove(struct kernfs_node *kn);
241 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
242                              const void *ns);
243 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
244                      const char *new_name, const void *new_ns);
245 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
246 void kernfs_notify(struct kernfs_node *kn);
247
248 const void *kernfs_super_ns(struct super_block *sb);
249 struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
250                                struct kernfs_root *root, const void *ns);
251 void kernfs_kill_sb(struct super_block *sb);
252
253 void kernfs_init(void);
254
255 #else   /* CONFIG_SYSFS */
256
257 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
258 { return 0; }   /* whatever */
259
260 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
261
262 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
263 { return false; }
264
265 static inline struct kernfs_node *
266 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
267                        const void *ns)
268 { return NULL; }
269
270 static inline void kernfs_get(struct kernfs_node *kn) { }
271 static inline void kernfs_put(struct kernfs_node *kn) { }
272
273 static inline struct kernfs_root *
274 kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
275 { return ERR_PTR(-ENOSYS); }
276
277 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
278
279 static inline struct kernfs_node *
280 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
281                      umode_t mode, void *priv, const void *ns)
282 { return ERR_PTR(-ENOSYS); }
283
284 static inline struct kernfs_node *
285 __kernfs_create_file(struct kernfs_node *parent, const char *name,
286                      umode_t mode, loff_t size, const struct kernfs_ops *ops,
287                      void *priv, const void *ns, bool name_is_static,
288                      struct lock_class_key *key)
289 { return ERR_PTR(-ENOSYS); }
290
291 static inline struct kernfs_node *
292 kernfs_create_link(struct kernfs_node *parent, const char *name,
293                    struct kernfs_node *target)
294 { return ERR_PTR(-ENOSYS); }
295
296 static inline void kernfs_remove(struct kernfs_node *kn) { }
297
298 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
299                                            const char *name, const void *ns)
300 { return -ENOSYS; }
301
302 static inline int kernfs_rename_ns(struct kernfs_node *kn,
303                                    struct kernfs_node *new_parent,
304                                    const char *new_name, const void *new_ns)
305 { return -ENOSYS; }
306
307 static inline int kernfs_setattr(struct kernfs_node *kn,
308                                  const struct iattr *iattr)
309 { return -ENOSYS; }
310
311 static inline void kernfs_notify(struct kernfs_node *kn) { }
312
313 static inline const void *kernfs_super_ns(struct super_block *sb)
314 { return NULL; }
315
316 static inline struct dentry *
317 kernfs_mount_ns(struct file_system_type *fs_type, int flags,
318                 struct kernfs_root *root, const void *ns)
319 { return ERR_PTR(-ENOSYS); }
320
321 static inline void kernfs_kill_sb(struct super_block *sb) { }
322
323 static inline void kernfs_init(void) { }
324
325 #endif  /* CONFIG_SYSFS */
326
327 static inline struct kernfs_node *
328 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
329 {
330         return kernfs_find_and_get_ns(kn, name, NULL);
331 }
332
333 static inline struct kernfs_node *
334 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
335                   void *priv)
336 {
337         return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
338 }
339
340 static inline struct kernfs_node *
341 kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
342                       umode_t mode, loff_t size, const struct kernfs_ops *ops,
343                       void *priv, const void *ns)
344 {
345         struct lock_class_key *key = NULL;
346
347 #ifdef CONFIG_DEBUG_LOCK_ALLOC
348         key = (struct lock_class_key *)&ops->lockdep_key;
349 #endif
350         return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
351                                     false, key);
352 }
353
354 static inline struct kernfs_node *
355 kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
356                    loff_t size, const struct kernfs_ops *ops, void *priv)
357 {
358         return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
359 }
360
361 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
362                                         const char *name)
363 {
364         return kernfs_remove_by_name_ns(parent, name, NULL);
365 }
366
367 static inline struct dentry *
368 kernfs_mount(struct file_system_type *fs_type, int flags,
369              struct kernfs_root *root)
370 {
371         return kernfs_mount_ns(fs_type, flags, root, NULL);
372 }
373
374 #endif  /* __LINUX_KERNFS_H */