kernfs: remove kernfs_addrm_cxt
[deliverable/linux.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>
ea1c472d 18#include <linux/wait.h>
879f40d1 19
5d60418e
TH
20struct file;
21struct iattr;
dd8a5b03
TH
22struct seq_file;
23struct vm_area_struct;
4b93dc9b
TH
24struct super_block;
25struct file_system_type;
5d60418e 26
c525aadd
TH
27struct kernfs_open_node;
28struct kernfs_iattrs;
cf9e5a73
TH
29
30enum kernfs_node_type {
df23fc39
TH
31 KERNFS_DIR = 0x0001,
32 KERNFS_FILE = 0x0002,
33 KERNFS_LINK = 0x0004,
cf9e5a73
TH
34};
35
df23fc39 36#define KERNFS_TYPE_MASK 0x000f
df23fc39 37#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
cf9e5a73
TH
38
39enum kernfs_node_flag {
45a140e5 40 KERNFS_JUST_DEACTIVATED = 0x0010, /* used to aid lockdep annotation */
df23fc39
TH
41 KERNFS_NS = 0x0020,
42 KERNFS_HAS_SEQ_SHOW = 0x0040,
43 KERNFS_HAS_MMAP = 0x0080,
44 KERNFS_LOCKDEP = 0x0100,
2063d608 45 KERNFS_STATIC_NAME = 0x0200,
cf9e5a73
TH
46};
47
324a56e1
TH
48/* type-specific structures for kernfs_node union members */
49struct kernfs_elem_dir {
cf9e5a73 50 unsigned long subdirs;
adc5e8b5 51 /* children rbtree starts here and goes through kn->rb */
cf9e5a73
TH
52 struct rb_root children;
53
54 /*
55 * The kernfs hierarchy this directory belongs to. This fits
324a56e1 56 * better directly in kernfs_node but is here to save space.
cf9e5a73
TH
57 */
58 struct kernfs_root *root;
59};
60
324a56e1
TH
61struct kernfs_elem_symlink {
62 struct kernfs_node *target_kn;
cf9e5a73
TH
63};
64
324a56e1 65struct kernfs_elem_attr {
cf9e5a73 66 const struct kernfs_ops *ops;
c525aadd 67 struct kernfs_open_node *open;
cf9e5a73
TH
68 loff_t size;
69};
70
71/*
324a56e1
TH
72 * kernfs_node - the building block of kernfs hierarchy. Each and every
73 * kernfs node is represented by single kernfs_node. Most fields are
cf9e5a73
TH
74 * private to kernfs and shouldn't be accessed directly by kernfs users.
75 *
324a56e1
TH
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.
cf9e5a73 79 */
324a56e1 80struct kernfs_node {
adc5e8b5
TH
81 atomic_t count;
82 atomic_t active;
cf9e5a73
TH
83#ifdef CONFIG_DEBUG_LOCK_ALLOC
84 struct lockdep_map dep_map;
85#endif
86 /* the following two fields are published */
adc5e8b5
TH
87 struct kernfs_node *parent;
88 const char *name;
cf9e5a73 89
adc5e8b5 90 struct rb_node rb;
cf9e5a73 91
adc5e8b5
TH
92 const void *ns; /* namespace tag */
93 unsigned int hash; /* ns + name hash */
cf9e5a73 94 union {
adc5e8b5
TH
95 struct kernfs_elem_dir dir;
96 struct kernfs_elem_symlink symlink;
97 struct kernfs_elem_attr attr;
cf9e5a73
TH
98 };
99
100 void *priv;
101
adc5e8b5
TH
102 unsigned short flags;
103 umode_t mode;
104 unsigned int ino;
c525aadd 105 struct kernfs_iattrs *iattr;
cf9e5a73 106};
b8441ed2 107
80b9bbef
TH
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 */
114struct 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
ba7443bc
TH
122struct kernfs_root {
123 /* published fields */
324a56e1 124 struct kernfs_node *kn;
bc755553
TH
125
126 /* private fields, do not use outside kernfs proper */
127 struct ida ino_ida;
80b9bbef 128 struct kernfs_dir_ops *dir_ops;
ea1c472d 129 wait_queue_head_t deactivate_waitq;
ba7443bc
TH
130};
131
c525aadd 132struct kernfs_open_file {
dd8a5b03 133 /* published fields */
324a56e1 134 struct kernfs_node *kn;
dd8a5b03
TH
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
f6acf8bb
TH
146struct kernfs_ops {
147 /*
148 * Read is handled by either seq_file or raw_read().
149 *
d19b9846
TH
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
c525aadd 153 * associated kernfs_open_file.
f6acf8bb
TH
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);
d19b9846
TH
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);
f6acf8bb 163
c525aadd 164 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
f6acf8bb
TH
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 */
c525aadd 171 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
f6acf8bb
TH
172 loff_t off);
173
c525aadd 174 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
517e64f5
TH
175
176#ifdef CONFIG_DEBUG_LOCK_ALLOC
177 struct lock_class_key lockdep_key;
178#endif
f6acf8bb
TH
179};
180
879f40d1
TH
181#ifdef CONFIG_SYSFS
182
df23fc39 183static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
cf9e5a73 184{
df23fc39 185 return kn->flags & KERNFS_TYPE_MASK;
cf9e5a73
TH
186}
187
188/**
189 * kernfs_enable_ns - enable namespace under a directory
324a56e1 190 * @kn: directory of interest, should be empty
cf9e5a73 191 *
324a56e1
TH
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
cf9e5a73
TH
194 * only the ones which match the super_block's tag will be visible.
195 */
324a56e1 196static inline void kernfs_enable_ns(struct kernfs_node *kn)
cf9e5a73 197{
df23fc39 198 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
adc5e8b5 199 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
df23fc39 200 kn->flags |= KERNFS_NS;
cf9e5a73
TH
201}
202
ac9bba03
TH
203/**
204 * kernfs_ns_enabled - test whether namespace is enabled
324a56e1 205 * @kn: the node to test
ac9bba03
TH
206 *
207 * Test whether namespace filtering is enabled for the children of @ns.
208 */
324a56e1 209static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
ac9bba03 210{
df23fc39 211 return kn->flags & KERNFS_NS;
ac9bba03
TH
212}
213
324a56e1
TH
214struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
215 const char *name, const void *ns);
216void kernfs_get(struct kernfs_node *kn);
217void kernfs_put(struct kernfs_node *kn);
ccf73cf3 218
80b9bbef
TH
219struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
220 void *priv);
ba7443bc
TH
221void kernfs_destroy_root(struct kernfs_root *root);
222
324a56e1 223struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
bb8b9d09
TH
224 const char *name, umode_t mode,
225 void *priv, const void *ns);
2063d608
TH
226struct 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);
324a56e1
TH
233struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
234 const char *name,
235 struct kernfs_node *target);
236void kernfs_remove(struct kernfs_node *kn);
237int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
879f40d1 238 const void *ns);
324a56e1 239int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
890ece16 240 const char *new_name, const void *new_ns);
324a56e1
TH
241int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
242void kernfs_notify(struct kernfs_node *kn);
879f40d1 243
4b93dc9b
TH
244const void *kernfs_super_ns(struct super_block *sb);
245struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
246 struct kernfs_root *root, const void *ns);
247void kernfs_kill_sb(struct super_block *sb);
248
249void kernfs_init(void);
250
879f40d1
TH
251#else /* CONFIG_SYSFS */
252
df23fc39 253static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
cf9e5a73
TH
254{ return 0; } /* whatever */
255
324a56e1 256static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
cf9e5a73 257
324a56e1 258static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
ac9bba03
TH
259{ return false; }
260
324a56e1
TH
261static inline struct kernfs_node *
262kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
ccf73cf3
TH
263 const void *ns)
264{ return NULL; }
265
324a56e1
TH
266static inline void kernfs_get(struct kernfs_node *kn) { }
267static inline void kernfs_put(struct kernfs_node *kn) { }
ccf73cf3 268
80b9bbef
TH
269static inline struct kernfs_root *
270kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
ba7443bc
TH
271{ return ERR_PTR(-ENOSYS); }
272
273static inline void kernfs_destroy_root(struct kernfs_root *root) { }
274
324a56e1 275static inline struct kernfs_node *
bb8b9d09
TH
276kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
277 umode_t mode, void *priv, const void *ns)
93b2b8e4
TH
278{ return ERR_PTR(-ENOSYS); }
279
324a56e1 280static inline struct kernfs_node *
2063d608
TH
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)
496f7394
TH
285{ return ERR_PTR(-ENOSYS); }
286
324a56e1
TH
287static inline struct kernfs_node *
288kernfs_create_link(struct kernfs_node *parent, const char *name,
289 struct kernfs_node *target)
5d0e26bb
TH
290{ return ERR_PTR(-ENOSYS); }
291
324a56e1 292static inline void kernfs_remove(struct kernfs_node *kn) { }
879f40d1 293
324a56e1 294static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
879f40d1
TH
295 const char *name, const void *ns)
296{ return -ENOSYS; }
297
324a56e1
TH
298static inline int kernfs_rename_ns(struct kernfs_node *kn,
299 struct kernfs_node *new_parent,
890ece16
TH
300 const char *new_name, const void *new_ns)
301{ return -ENOSYS; }
302
324a56e1 303static inline int kernfs_setattr(struct kernfs_node *kn,
5d60418e
TH
304 const struct iattr *iattr)
305{ return -ENOSYS; }
306
324a56e1 307static inline void kernfs_notify(struct kernfs_node *kn) { }
024f6471 308
4b93dc9b
TH
309static inline const void *kernfs_super_ns(struct super_block *sb)
310{ return NULL; }
311
312static inline struct dentry *
313kernfs_mount_ns(struct file_system_type *fs_type, int flags,
314 struct kernfs_root *root, const void *ns)
315{ return ERR_PTR(-ENOSYS); }
316
317static inline void kernfs_kill_sb(struct super_block *sb) { }
318
319static inline void kernfs_init(void) { }
320
879f40d1
TH
321#endif /* CONFIG_SYSFS */
322
324a56e1
TH
323static inline struct kernfs_node *
324kernfs_find_and_get(struct kernfs_node *kn, const char *name)
ccf73cf3 325{
324a56e1 326 return kernfs_find_and_get_ns(kn, name, NULL);
ccf73cf3
TH
327}
328
324a56e1 329static inline struct kernfs_node *
bb8b9d09
TH
330kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
331 void *priv)
93b2b8e4 332{
bb8b9d09 333 return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
93b2b8e4
TH
334}
335
324a56e1
TH
336static inline struct kernfs_node *
337kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
517e64f5
TH
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
2063d608
TH
346 return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
347 false, key);
517e64f5
TH
348}
349
324a56e1
TH
350static inline struct kernfs_node *
351kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
496f7394
TH
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
324a56e1 357static inline int kernfs_remove_by_name(struct kernfs_node *parent,
879f40d1
TH
358 const char *name)
359{
360 return kernfs_remove_by_name_ns(parent, name, NULL);
361}
362
4b93dc9b
TH
363static inline struct dentry *
364kernfs_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
b8441ed2 370#endif /* __LINUX_KERNFS_H */
This page took 0.054489 seconds and 5 git commands to generate.