kernfs: add kernfs_open_file->priv
[linux-2.6-block.git] / include / linux / kernfs.h
CommitLineData
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>
cf9e5a73
TH
16#include <linux/rbtree.h>
17#include <linux/atomic.h>
abd54f02 18#include <linux/wait.h>
879f40d1 19
5d60418e 20struct file;
917f56ca 21struct dentry;
5d60418e 22struct iattr;
dd8a5b03
TH
23struct seq_file;
24struct vm_area_struct;
4b93dc9b
TH
25struct super_block;
26struct file_system_type;
5d60418e 27
c525aadd
TH
28struct kernfs_open_node;
29struct kernfs_iattrs;
cf9e5a73
TH
30
31enum kernfs_node_type {
df23fc39
TH
32 KERNFS_DIR = 0x0001,
33 KERNFS_FILE = 0x0002,
34 KERNFS_LINK = 0x0004,
cf9e5a73
TH
35};
36
df23fc39 37#define KERNFS_TYPE_MASK 0x000f
df23fc39 38#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
cf9e5a73
TH
39
40enum kernfs_node_flag {
d35258ef 41 KERNFS_ACTIVATED = 0x0010,
df23fc39
TH
42 KERNFS_NS = 0x0020,
43 KERNFS_HAS_SEQ_SHOW = 0x0040,
44 KERNFS_HAS_MMAP = 0x0080,
45 KERNFS_LOCKDEP = 0x0100,
2063d608 46 KERNFS_STATIC_NAME = 0x0200,
6b0afc2a
TH
47 KERNFS_SUICIDAL = 0x0400,
48 KERNFS_SUICIDED = 0x0800,
cf9e5a73
TH
49};
50
d35258ef
TH
51/* @flags for kernfs_create_root() */
52enum kernfs_root_flag {
53 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
54};
55
324a56e1
TH
56/* type-specific structures for kernfs_node union members */
57struct kernfs_elem_dir {
cf9e5a73 58 unsigned long subdirs;
adc5e8b5 59 /* children rbtree starts here and goes through kn->rb */
cf9e5a73
TH
60 struct rb_root children;
61
62 /*
63 * The kernfs hierarchy this directory belongs to. This fits
324a56e1 64 * better directly in kernfs_node but is here to save space.
cf9e5a73
TH
65 */
66 struct kernfs_root *root;
67};
68
324a56e1
TH
69struct kernfs_elem_symlink {
70 struct kernfs_node *target_kn;
cf9e5a73
TH
71};
72
324a56e1 73struct kernfs_elem_attr {
cf9e5a73 74 const struct kernfs_ops *ops;
c525aadd 75 struct kernfs_open_node *open;
cf9e5a73
TH
76 loff_t size;
77};
78
79/*
324a56e1
TH
80 * kernfs_node - the building block of kernfs hierarchy. Each and every
81 * kernfs node is represented by single kernfs_node. Most fields are
cf9e5a73
TH
82 * private to kernfs and shouldn't be accessed directly by kernfs users.
83 *
324a56e1
TH
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.
cf9e5a73 87 */
324a56e1 88struct kernfs_node {
adc5e8b5
TH
89 atomic_t count;
90 atomic_t active;
cf9e5a73
TH
91#ifdef CONFIG_DEBUG_LOCK_ALLOC
92 struct lockdep_map dep_map;
93#endif
94 /* the following two fields are published */
adc5e8b5
TH
95 struct kernfs_node *parent;
96 const char *name;
cf9e5a73 97
adc5e8b5 98 struct rb_node rb;
cf9e5a73 99
adc5e8b5 100 const void *ns; /* namespace tag */
9b0925a6 101 unsigned int hash; /* ns + name hash */
cf9e5a73 102 union {
adc5e8b5
TH
103 struct kernfs_elem_dir dir;
104 struct kernfs_elem_symlink symlink;
105 struct kernfs_elem_attr attr;
cf9e5a73
TH
106 };
107
108 void *priv;
109
adc5e8b5
TH
110 unsigned short flags;
111 umode_t mode;
112 unsigned int ino;
c525aadd 113 struct kernfs_iattrs *iattr;
cf9e5a73 114};
b8441ed2 115
80b9bbef 116/*
90c07c89
TH
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.
80b9bbef 122 */
90c07c89 123struct kernfs_syscall_ops {
6a7fed4e
TH
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
80b9bbef
TH
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
ba7443bc
TH
134struct kernfs_root {
135 /* published fields */
324a56e1 136 struct kernfs_node *kn;
d35258ef 137 unsigned int flags; /* KERNFS_ROOT_* flags */
bc755553
TH
138
139 /* private fields, do not use outside kernfs proper */
140 struct ida ino_ida;
90c07c89 141 struct kernfs_syscall_ops *syscall_ops;
abd54f02 142 wait_queue_head_t deactivate_waitq;
ba7443bc
TH
143};
144
c525aadd 145struct kernfs_open_file {
dd8a5b03 146 /* published fields */
324a56e1 147 struct kernfs_node *kn;
dd8a5b03 148 struct file *file;
2536390d 149 void *priv;
dd8a5b03
TH
150
151 /* private fields, do not use outside kernfs proper */
152 struct mutex mutex;
153 int event;
154 struct list_head list;
155
156 bool mmapped;
157 const struct vm_operations_struct *vm_ops;
158};
159
f6acf8bb
TH
160struct kernfs_ops {
161 /*
162 * Read is handled by either seq_file or raw_read().
163 *
d19b9846
TH
164 * If seq_show() is present, seq_file path is active. Other seq
165 * operations are optional and if not implemented, the behavior is
166 * equivalent to single_open(). @sf->private points to the
c525aadd 167 * associated kernfs_open_file.
f6acf8bb
TH
168 *
169 * read() is bounced through kernel buffer and a read larger than
170 * PAGE_SIZE results in partial operation of PAGE_SIZE.
171 */
172 int (*seq_show)(struct seq_file *sf, void *v);
d19b9846
TH
173
174 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
175 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
176 void (*seq_stop)(struct seq_file *sf, void *v);
f6acf8bb 177
c525aadd 178 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
f6acf8bb
TH
179 loff_t off);
180
181 /*
4d3773c4
TH
182 * write() is bounced through kernel buffer. If atomic_write_len
183 * is not set, a write larger than PAGE_SIZE results in partial
184 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
185 * writes upto the specified size are executed atomically but
186 * larger ones are rejected with -E2BIG.
f6acf8bb 187 */
4d3773c4 188 size_t atomic_write_len;
c525aadd 189 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
f6acf8bb
TH
190 loff_t off);
191
c525aadd 192 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
517e64f5
TH
193
194#ifdef CONFIG_DEBUG_LOCK_ALLOC
195 struct lock_class_key lockdep_key;
196#endif
f6acf8bb
TH
197};
198
879f40d1
TH
199#ifdef CONFIG_SYSFS
200
df23fc39 201static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
cf9e5a73 202{
df23fc39 203 return kn->flags & KERNFS_TYPE_MASK;
cf9e5a73
TH
204}
205
206/**
207 * kernfs_enable_ns - enable namespace under a directory
324a56e1 208 * @kn: directory of interest, should be empty
cf9e5a73 209 *
324a56e1
TH
210 * This is to be called right after @kn is created to enable namespace
211 * under it. All children of @kn must have non-NULL namespace tags and
cf9e5a73
TH
212 * only the ones which match the super_block's tag will be visible.
213 */
324a56e1 214static inline void kernfs_enable_ns(struct kernfs_node *kn)
cf9e5a73 215{
df23fc39 216 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
adc5e8b5 217 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
df23fc39 218 kn->flags |= KERNFS_NS;
cf9e5a73
TH
219}
220
ac9bba03
TH
221/**
222 * kernfs_ns_enabled - test whether namespace is enabled
324a56e1 223 * @kn: the node to test
ac9bba03
TH
224 *
225 * Test whether namespace filtering is enabled for the children of @ns.
226 */
324a56e1 227static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
ac9bba03 228{
df23fc39 229 return kn->flags & KERNFS_NS;
ac9bba03
TH
230}
231
324a56e1
TH
232struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
233 const char *name, const void *ns);
234void kernfs_get(struct kernfs_node *kn);
235void kernfs_put(struct kernfs_node *kn);
ccf73cf3 236
90c07c89 237struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
d35258ef 238 unsigned int flags, void *priv);
ba7443bc
TH
239void kernfs_destroy_root(struct kernfs_root *root);
240
324a56e1 241struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
bb8b9d09
TH
242 const char *name, umode_t mode,
243 void *priv, const void *ns);
2063d608
TH
244struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
245 const char *name,
246 umode_t mode, loff_t size,
247 const struct kernfs_ops *ops,
248 void *priv, const void *ns,
249 bool name_is_static,
250 struct lock_class_key *key);
324a56e1
TH
251struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
252 const char *name,
253 struct kernfs_node *target);
d35258ef 254void kernfs_activate(struct kernfs_node *kn);
324a56e1 255void kernfs_remove(struct kernfs_node *kn);
6b0afc2a
TH
256void kernfs_break_active_protection(struct kernfs_node *kn);
257void kernfs_unbreak_active_protection(struct kernfs_node *kn);
258bool kernfs_remove_self(struct kernfs_node *kn);
324a56e1 259int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
879f40d1 260 const void *ns);
324a56e1 261int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
890ece16 262 const char *new_name, const void *new_ns);
324a56e1
TH
263int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
264void kernfs_notify(struct kernfs_node *kn);
879f40d1 265
4b93dc9b
TH
266const void *kernfs_super_ns(struct super_block *sb);
267struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
268 struct kernfs_root *root, const void *ns);
269void kernfs_kill_sb(struct super_block *sb);
270
271void kernfs_init(void);
272
879f40d1
TH
273#else /* CONFIG_SYSFS */
274
df23fc39 275static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
cf9e5a73
TH
276{ return 0; } /* whatever */
277
324a56e1 278static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
cf9e5a73 279
324a56e1 280static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
ac9bba03
TH
281{ return false; }
282
324a56e1
TH
283static inline struct kernfs_node *
284kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
ccf73cf3
TH
285 const void *ns)
286{ return NULL; }
287
324a56e1
TH
288static inline void kernfs_get(struct kernfs_node *kn) { }
289static inline void kernfs_put(struct kernfs_node *kn) { }
ccf73cf3 290
80b9bbef 291static inline struct kernfs_root *
d35258ef
TH
292kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
293 void *priv)
ba7443bc
TH
294{ return ERR_PTR(-ENOSYS); }
295
296static inline void kernfs_destroy_root(struct kernfs_root *root) { }
297
324a56e1 298static inline struct kernfs_node *
bb8b9d09
TH
299kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
300 umode_t mode, void *priv, const void *ns)
93b2b8e4
TH
301{ return ERR_PTR(-ENOSYS); }
302
324a56e1 303static inline struct kernfs_node *
2063d608
TH
304__kernfs_create_file(struct kernfs_node *parent, const char *name,
305 umode_t mode, loff_t size, const struct kernfs_ops *ops,
306 void *priv, const void *ns, bool name_is_static,
307 struct lock_class_key *key)
496f7394
TH
308{ return ERR_PTR(-ENOSYS); }
309
324a56e1
TH
310static inline struct kernfs_node *
311kernfs_create_link(struct kernfs_node *parent, const char *name,
312 struct kernfs_node *target)
5d0e26bb
TH
313{ return ERR_PTR(-ENOSYS); }
314
d35258ef
TH
315static inline void kernfs_activate(struct kernfs_node *kn) { }
316
324a56e1 317static inline void kernfs_remove(struct kernfs_node *kn) { }
879f40d1 318
6b0afc2a
TH
319static inline bool kernfs_remove_self(struct kernfs_node *kn)
320{ return false; }
321
324a56e1 322static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
879f40d1
TH
323 const char *name, const void *ns)
324{ return -ENOSYS; }
325
324a56e1
TH
326static inline int kernfs_rename_ns(struct kernfs_node *kn,
327 struct kernfs_node *new_parent,
890ece16
TH
328 const char *new_name, const void *new_ns)
329{ return -ENOSYS; }
330
324a56e1 331static inline int kernfs_setattr(struct kernfs_node *kn,
5d60418e
TH
332 const struct iattr *iattr)
333{ return -ENOSYS; }
334
324a56e1 335static inline void kernfs_notify(struct kernfs_node *kn) { }
024f6471 336
4b93dc9b
TH
337static inline const void *kernfs_super_ns(struct super_block *sb)
338{ return NULL; }
339
340static inline struct dentry *
341kernfs_mount_ns(struct file_system_type *fs_type, int flags,
342 struct kernfs_root *root, const void *ns)
343{ return ERR_PTR(-ENOSYS); }
344
345static inline void kernfs_kill_sb(struct super_block *sb) { }
346
347static inline void kernfs_init(void) { }
348
879f40d1
TH
349#endif /* CONFIG_SYSFS */
350
324a56e1
TH
351static inline struct kernfs_node *
352kernfs_find_and_get(struct kernfs_node *kn, const char *name)
ccf73cf3 353{
324a56e1 354 return kernfs_find_and_get_ns(kn, name, NULL);
ccf73cf3
TH
355}
356
324a56e1 357static inline struct kernfs_node *
bb8b9d09
TH
358kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
359 void *priv)
93b2b8e4 360{
bb8b9d09 361 return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
93b2b8e4
TH
362}
363
324a56e1
TH
364static inline struct kernfs_node *
365kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
517e64f5
TH
366 umode_t mode, loff_t size, const struct kernfs_ops *ops,
367 void *priv, const void *ns)
368{
369 struct lock_class_key *key = NULL;
370
371#ifdef CONFIG_DEBUG_LOCK_ALLOC
372 key = (struct lock_class_key *)&ops->lockdep_key;
373#endif
2063d608
TH
374 return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
375 false, key);
517e64f5
TH
376}
377
324a56e1
TH
378static inline struct kernfs_node *
379kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
496f7394
TH
380 loff_t size, const struct kernfs_ops *ops, void *priv)
381{
382 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
383}
384
324a56e1 385static inline int kernfs_remove_by_name(struct kernfs_node *parent,
879f40d1
TH
386 const char *name)
387{
388 return kernfs_remove_by_name_ns(parent, name, NULL);
389}
390
4b93dc9b
TH
391static inline struct dentry *
392kernfs_mount(struct file_system_type *fs_type, int flags,
393 struct kernfs_root *root)
394{
395 return kernfs_mount_ns(fs_type, flags, root, NULL);
396}
397
b8441ed2 398#endif /* __LINUX_KERNFS_H */