Commit | Line | Data |
---|---|---|
55716d26 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
b8441ed2 TH |
2 | /* |
3 | * kernfs.h - pseudo filesystem decoupled from vfs locking | |
b8441ed2 TH |
4 | */ |
5 | ||
6 | #ifndef __LINUX_KERNFS_H | |
7 | #define __LINUX_KERNFS_H | |
8 | ||
5d0e26bb | 9 | #include <linux/err.h> |
dd8a5b03 TH |
10 | #include <linux/list.h> |
11 | #include <linux/mutex.h> | |
bc755553 | 12 | #include <linux/idr.h> |
517e64f5 | 13 | #include <linux/lockdep.h> |
cf9e5a73 TH |
14 | #include <linux/rbtree.h> |
15 | #include <linux/atomic.h> | |
79f1c730 AS |
16 | #include <linux/bug.h> |
17 | #include <linux/types.h> | |
488dee96 | 18 | #include <linux/uidgid.h> |
abd54f02 | 19 | #include <linux/wait.h> |
393c3714 | 20 | #include <linux/rwsem.h> |
1d25b84e | 21 | #include <linux/cache.h> |
879f40d1 | 22 | |
5d60418e | 23 | struct file; |
917f56ca | 24 | struct dentry; |
5d60418e | 25 | struct iattr; |
dd8a5b03 TH |
26 | struct seq_file; |
27 | struct vm_area_struct; | |
79f1c730 | 28 | struct vm_operations_struct; |
4b93dc9b TH |
29 | struct super_block; |
30 | struct file_system_type; | |
147e1a97 | 31 | struct poll_table_struct; |
23bf1b6b | 32 | struct fs_context; |
5d60418e | 33 | |
23bf1b6b | 34 | struct kernfs_fs_context; |
c525aadd TH |
35 | struct kernfs_open_node; |
36 | struct kernfs_iattrs; | |
cf9e5a73 | 37 | |
1d25b84e IK |
38 | /* |
39 | * NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash | |
40 | * table of locks. | |
41 | * Having a small hash table would impact scalability, since | |
42 | * more and more kernfs_node objects will end up using same lock | |
43 | * and having a very large hash table would waste memory. | |
44 | * | |
45 | * At the moment size of hash table of locks is being set based on | |
46 | * the number of CPUs as follows: | |
47 | * | |
48 | * NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS | |
49 | * 1 1 2 | |
50 | * 2-3 2 4 | |
51 | * 4-7 4 16 | |
52 | * 8-15 6 64 | |
53 | * 16-31 8 256 | |
54 | * 32 and more 10 1024 | |
55 | * | |
56 | * The above relation between NR_CPU and number of locks is based | |
57 | * on some internal experimentation which involved booting qemu | |
58 | * with different values of smp, performing some sysfs operations | |
59 | * on all CPUs and observing how increase in number of locks impacts | |
60 | * completion time of these sysfs operations on each CPU. | |
61 | */ | |
62 | #ifdef CONFIG_SMP | |
63 | #define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32))) | |
64 | #else | |
65 | #define NR_KERNFS_LOCK_BITS 1 | |
66 | #endif | |
67 | ||
68 | #define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS) | |
69 | ||
70 | /* | |
71 | * There's one kernfs_open_file for each open file and one kernfs_open_node | |
72 | * for each kernfs_node with one or more open files. | |
73 | * | |
74 | * filp->private_data points to seq_file whose ->private points to | |
75 | * kernfs_open_file. | |
76 | * | |
77 | * kernfs_open_files are chained at kernfs_open_node->files, which is | |
78 | * protected by kernfs_global_locks.open_file_mutex[i]. | |
79 | * | |
80 | * To reduce possible contention in sysfs access, arising due to single | |
81 | * locks, use an array of locks (e.g. open_file_mutex) and use kernfs_node | |
82 | * object address as hash keys to get the index of these locks. | |
83 | * | |
84 | * Hashed mutexes are safe to use here because operations using these don't | |
85 | * rely on global exclusion. | |
86 | * | |
87 | * In future we intend to replace other global locks with hashed ones as well. | |
88 | * kernfs_global_locks acts as a holder for all such hash tables. | |
89 | */ | |
90 | struct kernfs_global_locks { | |
91 | struct mutex open_file_mutex[NR_KERNFS_LOCKS]; | |
92 | }; | |
93 | ||
cf9e5a73 | 94 | enum kernfs_node_type { |
df23fc39 TH |
95 | KERNFS_DIR = 0x0001, |
96 | KERNFS_FILE = 0x0002, | |
97 | KERNFS_LINK = 0x0004, | |
cf9e5a73 TH |
98 | }; |
99 | ||
0c47383b DX |
100 | #define KERNFS_TYPE_MASK 0x000f |
101 | #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK | |
102 | #define KERNFS_MAX_USER_XATTRS 128 | |
103 | #define KERNFS_USER_XATTR_SIZE_LIMIT (128 << 10) | |
cf9e5a73 TH |
104 | |
105 | enum kernfs_node_flag { | |
d35258ef | 106 | KERNFS_ACTIVATED = 0x0010, |
df23fc39 TH |
107 | KERNFS_NS = 0x0020, |
108 | KERNFS_HAS_SEQ_SHOW = 0x0040, | |
109 | KERNFS_HAS_MMAP = 0x0080, | |
110 | KERNFS_LOCKDEP = 0x0100, | |
783bd07d | 111 | KERNFS_HIDDEN = 0x0200, |
6b0afc2a TH |
112 | KERNFS_SUICIDAL = 0x0400, |
113 | KERNFS_SUICIDED = 0x0800, | |
ea015218 | 114 | KERNFS_EMPTY_DIR = 0x1000, |
0e67db2f | 115 | KERNFS_HAS_RELEASE = 0x2000, |
c2549174 | 116 | KERNFS_REMOVING = 0x4000, |
cf9e5a73 TH |
117 | }; |
118 | ||
d35258ef TH |
119 | /* @flags for kernfs_create_root() */ |
120 | enum kernfs_root_flag { | |
555724a8 TH |
121 | /* |
122 | * kernfs_nodes are created in the deactivated state and invisible. | |
123 | * They require explicit kernfs_activate() to become visible. This | |
124 | * can be used to make related nodes become visible atomically | |
125 | * after all nodes are created successfully. | |
126 | */ | |
127 | KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001, | |
128 | ||
129 | /* | |
0d1a393d | 130 | * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2) |
555724a8 TH |
131 | * succeeds regardless of the RW permissions. sysfs had an extra |
132 | * layer of enforcement where open(2) fails with -EACCES regardless | |
133 | * of CAP_DAC_OVERRIDE if the permission doesn't have the | |
134 | * respective read or write access at all (none of S_IRUGO or | |
135 | * S_IWUGO) or the respective operation isn't implemented. The | |
136 | * following flag enables that behavior. | |
137 | */ | |
138 | KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002, | |
aa818825 SL |
139 | |
140 | /* | |
141 | * The filesystem supports exportfs operation, so userspace can use | |
142 | * fhandle to access nodes of the fs. | |
143 | */ | |
144 | KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004, | |
0c47383b DX |
145 | |
146 | /* | |
147 | * Support user xattrs to be written to nodes rooted at this root. | |
148 | */ | |
149 | KERNFS_ROOT_SUPPORT_USER_XATTR = 0x0008, | |
d35258ef TH |
150 | }; |
151 | ||
324a56e1 TH |
152 | /* type-specific structures for kernfs_node union members */ |
153 | struct kernfs_elem_dir { | |
cf9e5a73 | 154 | unsigned long subdirs; |
adc5e8b5 | 155 | /* children rbtree starts here and goes through kn->rb */ |
cf9e5a73 TH |
156 | struct rb_root children; |
157 | ||
158 | /* | |
159 | * The kernfs hierarchy this directory belongs to. This fits | |
324a56e1 | 160 | * better directly in kernfs_node but is here to save space. |
cf9e5a73 TH |
161 | */ |
162 | struct kernfs_root *root; | |
895adbec IK |
163 | /* |
164 | * Monotonic revision counter, used to identify if a directory | |
165 | * node has changed during negative dentry revalidation. | |
166 | */ | |
167 | unsigned long rev; | |
cf9e5a73 TH |
168 | }; |
169 | ||
324a56e1 TH |
170 | struct kernfs_elem_symlink { |
171 | struct kernfs_node *target_kn; | |
cf9e5a73 TH |
172 | }; |
173 | ||
324a56e1 | 174 | struct kernfs_elem_attr { |
cf9e5a73 | 175 | const struct kernfs_ops *ops; |
086c00c7 | 176 | struct kernfs_open_node __rcu *open; |
cf9e5a73 | 177 | loff_t size; |
2fd26970 | 178 | struct kernfs_node *notify_next; /* for kernfs_notify() */ |
cf9e5a73 TH |
179 | }; |
180 | ||
181 | /* | |
324a56e1 TH |
182 | * kernfs_node - the building block of kernfs hierarchy. Each and every |
183 | * kernfs node is represented by single kernfs_node. Most fields are | |
cf9e5a73 TH |
184 | * private to kernfs and shouldn't be accessed directly by kernfs users. |
185 | * | |
21774fd8 | 186 | * As long as count reference is held, the kernfs_node itself is |
324a56e1 TH |
187 | * accessible. Dereferencing elem or any other outer entity requires |
188 | * active reference. | |
cf9e5a73 | 189 | */ |
324a56e1 | 190 | struct kernfs_node { |
adc5e8b5 TH |
191 | atomic_t count; |
192 | atomic_t active; | |
cf9e5a73 TH |
193 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
194 | struct lockdep_map dep_map; | |
195 | #endif | |
3eef34ad TH |
196 | /* |
197 | * Use kernfs_get_parent() and kernfs_name/path() instead of | |
198 | * accessing the following two fields directly. If the node is | |
199 | * never moved to a different parent, it is safe to access the | |
200 | * parent directly. | |
201 | */ | |
adc5e8b5 TH |
202 | struct kernfs_node *parent; |
203 | const char *name; | |
cf9e5a73 | 204 | |
adc5e8b5 | 205 | struct rb_node rb; |
cf9e5a73 | 206 | |
adc5e8b5 | 207 | const void *ns; /* namespace tag */ |
9b0925a6 | 208 | unsigned int hash; /* ns + name hash */ |
cf9e5a73 | 209 | union { |
adc5e8b5 TH |
210 | struct kernfs_elem_dir dir; |
211 | struct kernfs_elem_symlink symlink; | |
212 | struct kernfs_elem_attr attr; | |
cf9e5a73 TH |
213 | }; |
214 | ||
215 | void *priv; | |
216 | ||
67c0496e | 217 | /* |
40430452 TH |
218 | * 64bit unique ID. On 64bit ino setups, id is the ino. On 32bit, |
219 | * the low 32bits are ino and upper generation. | |
67c0496e TH |
220 | */ |
221 | u64 id; | |
222 | ||
adc5e8b5 TH |
223 | unsigned short flags; |
224 | umode_t mode; | |
c525aadd | 225 | struct kernfs_iattrs *iattr; |
cf9e5a73 | 226 | }; |
b8441ed2 | 227 | |
80b9bbef | 228 | /* |
90c07c89 TH |
229 | * kernfs_syscall_ops may be specified on kernfs_create_root() to support |
230 | * syscalls. These optional callbacks are invoked on the matching syscalls | |
231 | * and can perform any kernfs operations which don't necessarily have to be | |
232 | * the exact operation requested. An active reference is held for each | |
233 | * kernfs_node parameter. | |
80b9bbef | 234 | */ |
90c07c89 | 235 | struct kernfs_syscall_ops { |
6a7fed4e TH |
236 | int (*show_options)(struct seq_file *sf, struct kernfs_root *root); |
237 | ||
80b9bbef TH |
238 | int (*mkdir)(struct kernfs_node *parent, const char *name, |
239 | umode_t mode); | |
240 | int (*rmdir)(struct kernfs_node *kn); | |
241 | int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent, | |
242 | const char *new_name); | |
4f41fc59 SH |
243 | int (*show_path)(struct seq_file *sf, struct kernfs_node *kn, |
244 | struct kernfs_root *root); | |
80b9bbef TH |
245 | }; |
246 | ||
f2eb478f | 247 | struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root); |
ba7443bc | 248 | |
c525aadd | 249 | struct kernfs_open_file { |
dd8a5b03 | 250 | /* published fields */ |
324a56e1 | 251 | struct kernfs_node *kn; |
dd8a5b03 | 252 | struct file *file; |
0e67db2f | 253 | struct seq_file *seq_file; |
2536390d | 254 | void *priv; |
dd8a5b03 TH |
255 | |
256 | /* private fields, do not use outside kernfs proper */ | |
257 | struct mutex mutex; | |
e4234a1f | 258 | struct mutex prealloc_mutex; |
dd8a5b03 TH |
259 | int event; |
260 | struct list_head list; | |
2b75869b | 261 | char *prealloc_buf; |
dd8a5b03 | 262 | |
b7ce40cf | 263 | size_t atomic_write_len; |
a1d82aff | 264 | bool mmapped:1; |
0e67db2f | 265 | bool released:1; |
dd8a5b03 TH |
266 | const struct vm_operations_struct *vm_ops; |
267 | }; | |
268 | ||
f6acf8bb | 269 | struct kernfs_ops { |
0e67db2f TH |
270 | /* |
271 | * Optional open/release methods. Both are called with | |
272 | * @of->seq_file populated. | |
273 | */ | |
274 | int (*open)(struct kernfs_open_file *of); | |
275 | void (*release)(struct kernfs_open_file *of); | |
276 | ||
f6acf8bb TH |
277 | /* |
278 | * Read is handled by either seq_file or raw_read(). | |
279 | * | |
d19b9846 TH |
280 | * If seq_show() is present, seq_file path is active. Other seq |
281 | * operations are optional and if not implemented, the behavior is | |
282 | * equivalent to single_open(). @sf->private points to the | |
c525aadd | 283 | * associated kernfs_open_file. |
f6acf8bb TH |
284 | * |
285 | * read() is bounced through kernel buffer and a read larger than | |
286 | * PAGE_SIZE results in partial operation of PAGE_SIZE. | |
287 | */ | |
288 | int (*seq_show)(struct seq_file *sf, void *v); | |
d19b9846 TH |
289 | |
290 | void *(*seq_start)(struct seq_file *sf, loff_t *ppos); | |
291 | void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos); | |
292 | void (*seq_stop)(struct seq_file *sf, void *v); | |
f6acf8bb | 293 | |
c525aadd | 294 | ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes, |
f6acf8bb TH |
295 | loff_t off); |
296 | ||
297 | /* | |
4d3773c4 TH |
298 | * write() is bounced through kernel buffer. If atomic_write_len |
299 | * is not set, a write larger than PAGE_SIZE results in partial | |
300 | * operations of PAGE_SIZE chunks. If atomic_write_len is set, | |
301 | * writes upto the specified size are executed atomically but | |
302 | * larger ones are rejected with -E2BIG. | |
f6acf8bb | 303 | */ |
4d3773c4 | 304 | size_t atomic_write_len; |
2b75869b N |
305 | /* |
306 | * "prealloc" causes a buffer to be allocated at open for | |
307 | * all read/write requests. As ->seq_show uses seq_read() | |
308 | * which does its own allocation, it is incompatible with | |
309 | * ->prealloc. Provide ->read and ->write with ->prealloc. | |
310 | */ | |
311 | bool prealloc; | |
c525aadd | 312 | ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes, |
f6acf8bb TH |
313 | loff_t off); |
314 | ||
147e1a97 JW |
315 | __poll_t (*poll)(struct kernfs_open_file *of, |
316 | struct poll_table_struct *pt); | |
317 | ||
c525aadd | 318 | int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma); |
f6acf8bb TH |
319 | }; |
320 | ||
23bf1b6b DH |
321 | /* |
322 | * The kernfs superblock creation/mount parameter context. | |
323 | */ | |
324 | struct kernfs_fs_context { | |
325 | struct kernfs_root *root; /* Root of the hierarchy being mounted */ | |
326 | void *ns_tag; /* Namespace tag of the mount (or NULL) */ | |
327 | unsigned long magic; /* File system specific magic number */ | |
328 | ||
329 | /* The following are set/used by kernfs_mount() */ | |
330 | bool new_sb_created; /* Set to T if we allocated a new sb */ | |
331 | }; | |
332 | ||
ba341d55 | 333 | #ifdef CONFIG_KERNFS |
879f40d1 | 334 | |
df23fc39 | 335 | static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn) |
cf9e5a73 | 336 | { |
df23fc39 | 337 | return kn->flags & KERNFS_TYPE_MASK; |
cf9e5a73 TH |
338 | } |
339 | ||
67c0496e TH |
340 | static inline ino_t kernfs_id_ino(u64 id) |
341 | { | |
40430452 TH |
342 | /* id is ino if ino_t is 64bit; otherwise, low 32bits */ |
343 | if (sizeof(ino_t) >= sizeof(u64)) | |
344 | return id; | |
345 | else | |
346 | return (u32)id; | |
67c0496e TH |
347 | } |
348 | ||
349 | static inline u32 kernfs_id_gen(u64 id) | |
350 | { | |
40430452 TH |
351 | /* gen is fixed at 1 if ino_t is 64bit; otherwise, high 32bits */ |
352 | if (sizeof(ino_t) >= sizeof(u64)) | |
353 | return 1; | |
354 | else | |
355 | return id >> 32; | |
67c0496e TH |
356 | } |
357 | ||
358 | static inline ino_t kernfs_ino(struct kernfs_node *kn) | |
359 | { | |
360 | return kernfs_id_ino(kn->id); | |
361 | } | |
362 | ||
363 | static inline ino_t kernfs_gen(struct kernfs_node *kn) | |
364 | { | |
365 | return kernfs_id_gen(kn->id); | |
366 | } | |
367 | ||
cf9e5a73 TH |
368 | /** |
369 | * kernfs_enable_ns - enable namespace under a directory | |
324a56e1 | 370 | * @kn: directory of interest, should be empty |
cf9e5a73 | 371 | * |
324a56e1 TH |
372 | * This is to be called right after @kn is created to enable namespace |
373 | * under it. All children of @kn must have non-NULL namespace tags and | |
cf9e5a73 TH |
374 | * only the ones which match the super_block's tag will be visible. |
375 | */ | |
324a56e1 | 376 | static inline void kernfs_enable_ns(struct kernfs_node *kn) |
cf9e5a73 | 377 | { |
df23fc39 | 378 | WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR); |
adc5e8b5 | 379 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children)); |
df23fc39 | 380 | kn->flags |= KERNFS_NS; |
cf9e5a73 TH |
381 | } |
382 | ||
ac9bba03 TH |
383 | /** |
384 | * kernfs_ns_enabled - test whether namespace is enabled | |
324a56e1 | 385 | * @kn: the node to test |
ac9bba03 TH |
386 | * |
387 | * Test whether namespace filtering is enabled for the children of @ns. | |
388 | */ | |
324a56e1 | 389 | static inline bool kernfs_ns_enabled(struct kernfs_node *kn) |
ac9bba03 | 390 | { |
df23fc39 | 391 | return kn->flags & KERNFS_NS; |
ac9bba03 TH |
392 | } |
393 | ||
3eef34ad | 394 | int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen); |
9f6df573 AK |
395 | int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn, |
396 | char *buf, size_t buflen); | |
3eef34ad TH |
397 | void pr_cont_kernfs_name(struct kernfs_node *kn); |
398 | void pr_cont_kernfs_path(struct kernfs_node *kn); | |
399 | struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn); | |
324a56e1 TH |
400 | struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, |
401 | const char *name, const void *ns); | |
bd96f76a TH |
402 | struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent, |
403 | const char *path, const void *ns); | |
324a56e1 TH |
404 | void kernfs_get(struct kernfs_node *kn); |
405 | void kernfs_put(struct kernfs_node *kn); | |
ccf73cf3 | 406 | |
0c23b225 TH |
407 | struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry); |
408 | struct kernfs_root *kernfs_root_from_sb(struct super_block *sb); | |
fb02915f | 409 | struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn); |
0c23b225 | 410 | |
fb3c8315 AK |
411 | struct dentry *kernfs_node_dentry(struct kernfs_node *kn, |
412 | struct super_block *sb); | |
90c07c89 | 413 | struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, |
d35258ef | 414 | unsigned int flags, void *priv); |
ba7443bc TH |
415 | void kernfs_destroy_root(struct kernfs_root *root); |
416 | ||
324a56e1 | 417 | struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, |
bb8b9d09 | 418 | const char *name, umode_t mode, |
488dee96 | 419 | kuid_t uid, kgid_t gid, |
bb8b9d09 | 420 | void *priv, const void *ns); |
ea015218 EB |
421 | struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent, |
422 | const char *name); | |
2063d608 | 423 | struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent, |
488dee96 DT |
424 | const char *name, umode_t mode, |
425 | kuid_t uid, kgid_t gid, | |
426 | loff_t size, | |
2063d608 TH |
427 | const struct kernfs_ops *ops, |
428 | void *priv, const void *ns, | |
2063d608 | 429 | struct lock_class_key *key); |
324a56e1 TH |
430 | struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, |
431 | const char *name, | |
432 | struct kernfs_node *target); | |
d35258ef | 433 | void kernfs_activate(struct kernfs_node *kn); |
783bd07d | 434 | void kernfs_show(struct kernfs_node *kn, bool show); |
324a56e1 | 435 | void kernfs_remove(struct kernfs_node *kn); |
6b0afc2a TH |
436 | void kernfs_break_active_protection(struct kernfs_node *kn); |
437 | void kernfs_unbreak_active_protection(struct kernfs_node *kn); | |
438 | bool kernfs_remove_self(struct kernfs_node *kn); | |
324a56e1 | 439 | int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, |
879f40d1 | 440 | const void *ns); |
324a56e1 | 441 | int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, |
890ece16 | 442 | const char *new_name, const void *new_ns); |
324a56e1 | 443 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr); |
147e1a97 JW |
444 | __poll_t kernfs_generic_poll(struct kernfs_open_file *of, |
445 | struct poll_table_struct *pt); | |
324a56e1 | 446 | void kernfs_notify(struct kernfs_node *kn); |
879f40d1 | 447 | |
1537ad15 OM |
448 | int kernfs_xattr_get(struct kernfs_node *kn, const char *name, |
449 | void *value, size_t size); | |
450 | int kernfs_xattr_set(struct kernfs_node *kn, const char *name, | |
451 | const void *value, size_t size, int flags); | |
b230d5ab | 452 | |
4b93dc9b | 453 | const void *kernfs_super_ns(struct super_block *sb); |
23bf1b6b DH |
454 | int kernfs_get_tree(struct fs_context *fc); |
455 | void kernfs_free_fs_context(struct fs_context *fc); | |
4b93dc9b TH |
456 | void kernfs_kill_sb(struct super_block *sb); |
457 | ||
458 | void kernfs_init(void); | |
459 | ||
fe0f726c TH |
460 | struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root, |
461 | u64 id); | |
ba341d55 | 462 | #else /* CONFIG_KERNFS */ |
879f40d1 | 463 | |
df23fc39 | 464 | static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn) |
cf9e5a73 TH |
465 | { return 0; } /* whatever */ |
466 | ||
324a56e1 | 467 | static inline void kernfs_enable_ns(struct kernfs_node *kn) { } |
cf9e5a73 | 468 | |
324a56e1 | 469 | static inline bool kernfs_ns_enabled(struct kernfs_node *kn) |
ac9bba03 TH |
470 | { return false; } |
471 | ||
3eef34ad TH |
472 | static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) |
473 | { return -ENOSYS; } | |
474 | ||
0e0b2afd TH |
475 | static inline int kernfs_path_from_node(struct kernfs_node *root_kn, |
476 | struct kernfs_node *kn, | |
477 | char *buf, size_t buflen) | |
478 | { return -ENOSYS; } | |
479 | ||
3eef34ad TH |
480 | static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { } |
481 | static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { } | |
482 | ||
483 | static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn) | |
484 | { return NULL; } | |
485 | ||
324a56e1 TH |
486 | static inline struct kernfs_node * |
487 | kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name, | |
ccf73cf3 TH |
488 | const void *ns) |
489 | { return NULL; } | |
bd96f76a TH |
490 | static inline struct kernfs_node * |
491 | kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path, | |
492 | const void *ns) | |
493 | { return NULL; } | |
ccf73cf3 | 494 | |
324a56e1 TH |
495 | static inline void kernfs_get(struct kernfs_node *kn) { } |
496 | static inline void kernfs_put(struct kernfs_node *kn) { } | |
ccf73cf3 | 497 | |
0c23b225 TH |
498 | static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry) |
499 | { return NULL; } | |
500 | ||
501 | static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb) | |
502 | { return NULL; } | |
503 | ||
fb02915f TH |
504 | static inline struct inode * |
505 | kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn) | |
506 | { return NULL; } | |
507 | ||
80b9bbef | 508 | static inline struct kernfs_root * |
d35258ef TH |
509 | kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags, |
510 | void *priv) | |
ba7443bc TH |
511 | { return ERR_PTR(-ENOSYS); } |
512 | ||
513 | static inline void kernfs_destroy_root(struct kernfs_root *root) { } | |
514 | ||
324a56e1 | 515 | static inline struct kernfs_node * |
bb8b9d09 | 516 | kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, |
488dee96 DT |
517 | umode_t mode, kuid_t uid, kgid_t gid, |
518 | void *priv, const void *ns) | |
93b2b8e4 TH |
519 | { return ERR_PTR(-ENOSYS); } |
520 | ||
324a56e1 | 521 | static inline struct kernfs_node * |
2063d608 | 522 | __kernfs_create_file(struct kernfs_node *parent, const char *name, |
488dee96 DT |
523 | umode_t mode, kuid_t uid, kgid_t gid, |
524 | loff_t size, const struct kernfs_ops *ops, | |
dfeb0750 | 525 | void *priv, const void *ns, struct lock_class_key *key) |
496f7394 TH |
526 | { return ERR_PTR(-ENOSYS); } |
527 | ||
324a56e1 TH |
528 | static inline struct kernfs_node * |
529 | kernfs_create_link(struct kernfs_node *parent, const char *name, | |
530 | struct kernfs_node *target) | |
5d0e26bb TH |
531 | { return ERR_PTR(-ENOSYS); } |
532 | ||
d35258ef TH |
533 | static inline void kernfs_activate(struct kernfs_node *kn) { } |
534 | ||
324a56e1 | 535 | static inline void kernfs_remove(struct kernfs_node *kn) { } |
879f40d1 | 536 | |
6b0afc2a TH |
537 | static inline bool kernfs_remove_self(struct kernfs_node *kn) |
538 | { return false; } | |
539 | ||
324a56e1 | 540 | static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn, |
879f40d1 TH |
541 | const char *name, const void *ns) |
542 | { return -ENOSYS; } | |
543 | ||
324a56e1 TH |
544 | static inline int kernfs_rename_ns(struct kernfs_node *kn, |
545 | struct kernfs_node *new_parent, | |
890ece16 TH |
546 | const char *new_name, const void *new_ns) |
547 | { return -ENOSYS; } | |
548 | ||
324a56e1 | 549 | static inline int kernfs_setattr(struct kernfs_node *kn, |
5d60418e TH |
550 | const struct iattr *iattr) |
551 | { return -ENOSYS; } | |
552 | ||
324a56e1 | 553 | static inline void kernfs_notify(struct kernfs_node *kn) { } |
024f6471 | 554 | |
1537ad15 OM |
555 | static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name, |
556 | void *value, size_t size) | |
b230d5ab OM |
557 | { return -ENOSYS; } |
558 | ||
1537ad15 OM |
559 | static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name, |
560 | const void *value, size_t size, int flags) | |
b230d5ab OM |
561 | { return -ENOSYS; } |
562 | ||
4b93dc9b TH |
563 | static inline const void *kernfs_super_ns(struct super_block *sb) |
564 | { return NULL; } | |
565 | ||
23bf1b6b DH |
566 | static inline int kernfs_get_tree(struct fs_context *fc) |
567 | { return -ENOSYS; } | |
568 | ||
569 | static inline void kernfs_free_fs_context(struct fs_context *fc) { } | |
4b93dc9b TH |
570 | |
571 | static inline void kernfs_kill_sb(struct super_block *sb) { } | |
572 | ||
573 | static inline void kernfs_init(void) { } | |
574 | ||
ba341d55 | 575 | #endif /* CONFIG_KERNFS */ |
879f40d1 | 576 | |
3abb1d90 TH |
577 | /** |
578 | * kernfs_path - build full path of a given node | |
579 | * @kn: kernfs_node of interest | |
580 | * @buf: buffer to copy @kn's name into | |
581 | * @buflen: size of @buf | |
582 | * | |
8f5be0ec KK |
583 | * If @kn is NULL result will be "(null)". |
584 | * | |
585 | * Returns the length of the full path. If the full length is equal to or | |
586 | * greater than @buflen, @buf contains the truncated path with the trailing | |
587 | * '\0'. On error, -errno is returned. | |
3abb1d90 TH |
588 | */ |
589 | static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen) | |
590 | { | |
591 | return kernfs_path_from_node(kn, NULL, buf, buflen); | |
592 | } | |
593 | ||
324a56e1 TH |
594 | static inline struct kernfs_node * |
595 | kernfs_find_and_get(struct kernfs_node *kn, const char *name) | |
ccf73cf3 | 596 | { |
324a56e1 | 597 | return kernfs_find_and_get_ns(kn, name, NULL); |
ccf73cf3 TH |
598 | } |
599 | ||
bd96f76a TH |
600 | static inline struct kernfs_node * |
601 | kernfs_walk_and_get(struct kernfs_node *kn, const char *path) | |
602 | { | |
603 | return kernfs_walk_and_get_ns(kn, path, NULL); | |
604 | } | |
605 | ||
324a56e1 | 606 | static inline struct kernfs_node * |
bb8b9d09 TH |
607 | kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode, |
608 | void *priv) | |
93b2b8e4 | 609 | { |
488dee96 DT |
610 | return kernfs_create_dir_ns(parent, name, mode, |
611 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, | |
612 | priv, NULL); | |
93b2b8e4 TH |
613 | } |
614 | ||
324a56e1 | 615 | static inline int kernfs_remove_by_name(struct kernfs_node *parent, |
879f40d1 TH |
616 | const char *name) |
617 | { | |
618 | return kernfs_remove_by_name_ns(parent, name, NULL); | |
619 | } | |
620 | ||
0c23b225 TH |
621 | static inline int kernfs_rename(struct kernfs_node *kn, |
622 | struct kernfs_node *new_parent, | |
623 | const char *new_name) | |
624 | { | |
625 | return kernfs_rename_ns(kn, new_parent, new_name, NULL); | |
626 | } | |
627 | ||
b8441ed2 | 628 | #endif /* __LINUX_KERNFS_H */ |