kernfs: restructure removal path to fix possible premature return
[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
TH
91
92 union {
324a56e1 93 struct kernfs_node *removed_list;
cf9e5a73
TH
94 } u;
95
adc5e8b5
TH
96 const void *ns; /* namespace tag */
97 unsigned int hash; /* ns + name hash */
cf9e5a73 98 union {
adc5e8b5
TH
99 struct kernfs_elem_dir dir;
100 struct kernfs_elem_symlink symlink;
101 struct kernfs_elem_attr attr;
cf9e5a73
TH
102 };
103
104 void *priv;
105
adc5e8b5
TH
106 unsigned short flags;
107 umode_t mode;
108 unsigned int ino;
c525aadd 109 struct kernfs_iattrs *iattr;
cf9e5a73 110};
b8441ed2 111
80b9bbef
TH
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 */
118struct 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
ba7443bc
TH
126struct kernfs_root {
127 /* published fields */
324a56e1 128 struct kernfs_node *kn;
bc755553
TH
129
130 /* private fields, do not use outside kernfs proper */
131 struct ida ino_ida;
80b9bbef 132 struct kernfs_dir_ops *dir_ops;
ea1c472d 133 wait_queue_head_t deactivate_waitq;
ba7443bc
TH
134};
135
c525aadd 136struct kernfs_open_file {
dd8a5b03 137 /* published fields */
324a56e1 138 struct kernfs_node *kn;
dd8a5b03
TH
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
f6acf8bb
TH
150struct kernfs_ops {
151 /*
152 * Read is handled by either seq_file or raw_read().
153 *
d19b9846
TH
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
c525aadd 157 * associated kernfs_open_file.
f6acf8bb
TH
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);
d19b9846
TH
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);
f6acf8bb 167
c525aadd 168 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
f6acf8bb
TH
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 */
c525aadd 175 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
f6acf8bb
TH
176 loff_t off);
177
c525aadd 178 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
517e64f5
TH
179
180#ifdef CONFIG_DEBUG_LOCK_ALLOC
181 struct lock_class_key lockdep_key;
182#endif
f6acf8bb
TH
183};
184
879f40d1
TH
185#ifdef CONFIG_SYSFS
186
df23fc39 187static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
cf9e5a73 188{
df23fc39 189 return kn->flags & KERNFS_TYPE_MASK;
cf9e5a73
TH
190}
191
192/**
193 * kernfs_enable_ns - enable namespace under a directory
324a56e1 194 * @kn: directory of interest, should be empty
cf9e5a73 195 *
324a56e1
TH
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
cf9e5a73
TH
198 * only the ones which match the super_block's tag will be visible.
199 */
324a56e1 200static inline void kernfs_enable_ns(struct kernfs_node *kn)
cf9e5a73 201{
df23fc39 202 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
adc5e8b5 203 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
df23fc39 204 kn->flags |= KERNFS_NS;
cf9e5a73
TH
205}
206
ac9bba03
TH
207/**
208 * kernfs_ns_enabled - test whether namespace is enabled
324a56e1 209 * @kn: the node to test
ac9bba03
TH
210 *
211 * Test whether namespace filtering is enabled for the children of @ns.
212 */
324a56e1 213static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
ac9bba03 214{
df23fc39 215 return kn->flags & KERNFS_NS;
ac9bba03
TH
216}
217
324a56e1
TH
218struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
219 const char *name, const void *ns);
220void kernfs_get(struct kernfs_node *kn);
221void kernfs_put(struct kernfs_node *kn);
ccf73cf3 222
80b9bbef
TH
223struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
224 void *priv);
ba7443bc
TH
225void kernfs_destroy_root(struct kernfs_root *root);
226
324a56e1 227struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
bb8b9d09
TH
228 const char *name, umode_t mode,
229 void *priv, const void *ns);
2063d608
TH
230struct 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);
324a56e1
TH
237struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
238 const char *name,
239 struct kernfs_node *target);
240void kernfs_remove(struct kernfs_node *kn);
241int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
879f40d1 242 const void *ns);
324a56e1 243int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
890ece16 244 const char *new_name, const void *new_ns);
324a56e1
TH
245int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
246void kernfs_notify(struct kernfs_node *kn);
879f40d1 247
4b93dc9b
TH
248const void *kernfs_super_ns(struct super_block *sb);
249struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
250 struct kernfs_root *root, const void *ns);
251void kernfs_kill_sb(struct super_block *sb);
252
253void kernfs_init(void);
254
879f40d1
TH
255#else /* CONFIG_SYSFS */
256
df23fc39 257static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
cf9e5a73
TH
258{ return 0; } /* whatever */
259
324a56e1 260static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
cf9e5a73 261
324a56e1 262static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
ac9bba03
TH
263{ return false; }
264
324a56e1
TH
265static inline struct kernfs_node *
266kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
ccf73cf3
TH
267 const void *ns)
268{ return NULL; }
269
324a56e1
TH
270static inline void kernfs_get(struct kernfs_node *kn) { }
271static inline void kernfs_put(struct kernfs_node *kn) { }
ccf73cf3 272
80b9bbef
TH
273static inline struct kernfs_root *
274kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
ba7443bc
TH
275{ return ERR_PTR(-ENOSYS); }
276
277static inline void kernfs_destroy_root(struct kernfs_root *root) { }
278
324a56e1 279static inline struct kernfs_node *
bb8b9d09
TH
280kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
281 umode_t mode, void *priv, const void *ns)
93b2b8e4
TH
282{ return ERR_PTR(-ENOSYS); }
283
324a56e1 284static inline struct kernfs_node *
2063d608
TH
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)
496f7394
TH
289{ return ERR_PTR(-ENOSYS); }
290
324a56e1
TH
291static inline struct kernfs_node *
292kernfs_create_link(struct kernfs_node *parent, const char *name,
293 struct kernfs_node *target)
5d0e26bb
TH
294{ return ERR_PTR(-ENOSYS); }
295
324a56e1 296static inline void kernfs_remove(struct kernfs_node *kn) { }
879f40d1 297
324a56e1 298static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
879f40d1
TH
299 const char *name, const void *ns)
300{ return -ENOSYS; }
301
324a56e1
TH
302static inline int kernfs_rename_ns(struct kernfs_node *kn,
303 struct kernfs_node *new_parent,
890ece16
TH
304 const char *new_name, const void *new_ns)
305{ return -ENOSYS; }
306
324a56e1 307static inline int kernfs_setattr(struct kernfs_node *kn,
5d60418e
TH
308 const struct iattr *iattr)
309{ return -ENOSYS; }
310
324a56e1 311static inline void kernfs_notify(struct kernfs_node *kn) { }
024f6471 312
4b93dc9b
TH
313static inline const void *kernfs_super_ns(struct super_block *sb)
314{ return NULL; }
315
316static inline struct dentry *
317kernfs_mount_ns(struct file_system_type *fs_type, int flags,
318 struct kernfs_root *root, const void *ns)
319{ return ERR_PTR(-ENOSYS); }
320
321static inline void kernfs_kill_sb(struct super_block *sb) { }
322
323static inline void kernfs_init(void) { }
324
879f40d1
TH
325#endif /* CONFIG_SYSFS */
326
324a56e1
TH
327static inline struct kernfs_node *
328kernfs_find_and_get(struct kernfs_node *kn, const char *name)
ccf73cf3 329{
324a56e1 330 return kernfs_find_and_get_ns(kn, name, NULL);
ccf73cf3
TH
331}
332
324a56e1 333static inline struct kernfs_node *
bb8b9d09
TH
334kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
335 void *priv)
93b2b8e4 336{
bb8b9d09 337 return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
93b2b8e4
TH
338}
339
324a56e1
TH
340static inline struct kernfs_node *
341kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
517e64f5
TH
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
2063d608
TH
350 return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
351 false, key);
517e64f5
TH
352}
353
324a56e1
TH
354static inline struct kernfs_node *
355kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
496f7394
TH
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
324a56e1 361static inline int kernfs_remove_by_name(struct kernfs_node *parent,
879f40d1
TH
362 const char *name)
363{
364 return kernfs_remove_by_name_ns(parent, name, NULL);
365}
366
4b93dc9b
TH
367static inline struct dentry *
368kernfs_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
b8441ed2 374#endif /* __LINUX_KERNFS_H */
This page took 0.047048 seconds and 5 git commands to generate.