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