blob: 38267cc9420c6d8ab3a5b87fac2b4cf81d1923a2 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Tejun Heob8441ed2013-11-24 09:54:58 -05002/*
3 * kernfs.h - pseudo filesystem decoupled from vfs locking
Tejun Heob8441ed2013-11-24 09:54:58 -05004 */
5
6#ifndef __LINUX_KERNFS_H
7#define __LINUX_KERNFS_H
8
Tejun Heo879f40d2013-11-23 17:21:49 -05009#include <linux/kernel.h>
Tejun Heo5d0e26b2013-11-23 17:21:50 -050010#include <linux/err.h>
Tejun Heodd8a5b02013-11-28 14:54:20 -050011#include <linux/list.h>
12#include <linux/mutex.h>
Tejun Heobc755552013-11-28 14:54:41 -050013#include <linux/idr.h>
Tejun Heo517e64f2013-11-28 14:54:29 -050014#include <linux/lockdep.h>
Tejun Heocf9e5a72013-11-29 17:18:32 -050015#include <linux/rbtree.h>
16#include <linux/atomic.h>
Dmitry Torokhov488dee92018-07-20 21:56:47 +000017#include <linux/uidgid.h>
Tejun Heoabd54f02014-02-03 14:02:55 -050018#include <linux/wait.h>
Tejun Heo879f40d2013-11-23 17:21:49 -050019
Tejun Heo5d604182013-11-23 17:21:52 -050020struct file;
Tejun Heo917f56c2014-01-17 09:57:49 -050021struct dentry;
Tejun Heo5d604182013-11-23 17:21:52 -050022struct iattr;
Tejun Heodd8a5b02013-11-28 14:54:20 -050023struct seq_file;
24struct vm_area_struct;
Tejun Heo4b93dc92013-11-28 14:54:43 -050025struct super_block;
26struct file_system_type;
Johannes Weiner147e1a92019-03-05 15:45:45 -080027struct poll_table_struct;
David Howells23bf1b62018-11-01 23:07:26 +000028struct fs_context;
Tejun Heo5d604182013-11-23 17:21:52 -050029
David Howells23bf1b62018-11-01 23:07:26 +000030struct kernfs_fs_context;
Tejun Heoc525aad2013-12-11 14:11:55 -050031struct kernfs_open_node;
32struct kernfs_iattrs;
Tejun Heocf9e5a72013-11-29 17:18:32 -050033
34enum kernfs_node_type {
Tejun Heodf23fc32013-12-11 14:11:56 -050035 KERNFS_DIR = 0x0001,
36 KERNFS_FILE = 0x0002,
37 KERNFS_LINK = 0x0004,
Tejun Heocf9e5a72013-11-29 17:18:32 -050038};
39
Tejun Heodf23fc32013-12-11 14:11:56 -050040#define KERNFS_TYPE_MASK 0x000f
Tejun Heodf23fc32013-12-11 14:11:56 -050041#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
Tejun Heocf9e5a72013-11-29 17:18:32 -050042
43enum kernfs_node_flag {
Tejun Heod35258e2014-02-03 14:09:12 -050044 KERNFS_ACTIVATED = 0x0010,
Tejun Heodf23fc32013-12-11 14:11:56 -050045 KERNFS_NS = 0x0020,
46 KERNFS_HAS_SEQ_SHOW = 0x0040,
47 KERNFS_HAS_MMAP = 0x0080,
48 KERNFS_LOCKDEP = 0x0100,
Tejun Heo6b0afc22014-02-03 14:03:01 -050049 KERNFS_SUICIDAL = 0x0400,
50 KERNFS_SUICIDED = 0x0800,
Eric W. Biedermanea015212015-05-13 16:09:29 -050051 KERNFS_EMPTY_DIR = 0x1000,
Tejun Heo0e67db22016-12-27 14:49:03 -050052 KERNFS_HAS_RELEASE = 0x2000,
Tejun Heocf9e5a72013-11-29 17:18:32 -050053};
54
Tejun Heod35258e2014-02-03 14:09:12 -050055/* @flags for kernfs_create_root() */
56enum kernfs_root_flag {
Tejun Heo555724a2014-05-12 13:56:27 -040057 /*
58 * kernfs_nodes are created in the deactivated state and invisible.
59 * They require explicit kernfs_activate() to become visible. This
60 * can be used to make related nodes become visible atomically
61 * after all nodes are created successfully.
62 */
63 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
64
65 /*
Christina Quast0d1a3932019-04-02 16:01:46 +020066 * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2)
Tejun Heo555724a2014-05-12 13:56:27 -040067 * succeeds regardless of the RW permissions. sysfs had an extra
68 * layer of enforcement where open(2) fails with -EACCES regardless
69 * of CAP_DAC_OVERRIDE if the permission doesn't have the
70 * respective read or write access at all (none of S_IRUGO or
71 * S_IWUGO) or the respective operation isn't implemented. The
72 * following flag enables that behavior.
73 */
74 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
Shaohua Liaa818822017-07-12 11:49:51 -070075
76 /*
77 * The filesystem supports exportfs operation, so userspace can use
78 * fhandle to access nodes of the fs.
79 */
80 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
Tejun Heod35258e2014-02-03 14:09:12 -050081};
82
Tejun Heo324a56e2013-12-11 14:11:53 -050083/* type-specific structures for kernfs_node union members */
84struct kernfs_elem_dir {
Tejun Heocf9e5a72013-11-29 17:18:32 -050085 unsigned long subdirs;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050086 /* children rbtree starts here and goes through kn->rb */
Tejun Heocf9e5a72013-11-29 17:18:32 -050087 struct rb_root children;
88
89 /*
90 * The kernfs hierarchy this directory belongs to. This fits
Tejun Heo324a56e2013-12-11 14:11:53 -050091 * better directly in kernfs_node but is here to save space.
Tejun Heocf9e5a72013-11-29 17:18:32 -050092 */
93 struct kernfs_root *root;
94};
95
Tejun Heo324a56e2013-12-11 14:11:53 -050096struct kernfs_elem_symlink {
97 struct kernfs_node *target_kn;
Tejun Heocf9e5a72013-11-29 17:18:32 -050098};
99
Tejun Heo324a56e2013-12-11 14:11:53 -0500100struct kernfs_elem_attr {
Tejun Heocf9e5a72013-11-29 17:18:32 -0500101 const struct kernfs_ops *ops;
Tejun Heoc525aad2013-12-11 14:11:55 -0500102 struct kernfs_open_node *open;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500103 loff_t size;
Tejun Heoecca47c2014-07-01 16:41:03 -0400104 struct kernfs_node *notify_next; /* for kernfs_notify() */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500105};
106
107/*
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 * kernfs_node - the building block of kernfs hierarchy. Each and every
109 * kernfs node is represented by single kernfs_node. Most fields are
Tejun Heocf9e5a72013-11-29 17:18:32 -0500110 * private to kernfs and shouldn't be accessed directly by kernfs users.
111 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500112 * As long as s_count reference is held, the kernfs_node itself is
113 * accessible. Dereferencing elem or any other outer entity requires
114 * active reference.
Tejun Heocf9e5a72013-11-29 17:18:32 -0500115 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500116struct kernfs_node {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500117 atomic_t count;
118 atomic_t active;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500119#ifdef CONFIG_DEBUG_LOCK_ALLOC
120 struct lockdep_map dep_map;
121#endif
Tejun Heo3eef34a2014-02-07 13:32:07 -0500122 /*
123 * Use kernfs_get_parent() and kernfs_name/path() instead of
124 * accessing the following two fields directly. If the node is
125 * never moved to a different parent, it is safe to access the
126 * parent directly.
127 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500128 struct kernfs_node *parent;
129 const char *name;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500130
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500131 struct rb_node rb;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500132
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500133 const void *ns; /* namespace tag */
Greg Kroah-Hartman9b0925a2014-01-13 14:09:38 -0800134 unsigned int hash; /* ns + name hash */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500135 union {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500136 struct kernfs_elem_dir dir;
137 struct kernfs_elem_symlink symlink;
138 struct kernfs_elem_attr attr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500139 };
140
141 void *priv;
142
Tejun Heo67c04962019-11-04 15:54:30 -0800143 /*
144 * 64bit unique ID. Lower 32bits carry the inode number and lower
145 * generation.
146 */
147 u64 id;
148
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500149 unsigned short flags;
150 umode_t mode;
Tejun Heoc525aad2013-12-11 14:11:55 -0500151 struct kernfs_iattrs *iattr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500152};
Tejun Heob8441ed2013-11-24 09:54:58 -0500153
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500154/*
Tejun Heo90c07c82014-02-03 14:09:09 -0500155 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
156 * syscalls. These optional callbacks are invoked on the matching syscalls
157 * and can perform any kernfs operations which don't necessarily have to be
158 * the exact operation requested. An active reference is held for each
159 * kernfs_node parameter.
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500160 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500161struct kernfs_syscall_ops {
Tejun Heo6a7fed42014-02-03 14:09:10 -0500162 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
163
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500164 int (*mkdir)(struct kernfs_node *parent, const char *name,
165 umode_t mode);
166 int (*rmdir)(struct kernfs_node *kn);
167 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
168 const char *new_name);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -0500169 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
170 struct kernfs_root *root);
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500171};
172
Tejun Heoba7443b2013-11-28 14:54:40 -0500173struct kernfs_root {
174 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500175 struct kernfs_node *kn;
Tejun Heod35258e2014-02-03 14:09:12 -0500176 unsigned int flags; /* KERNFS_ROOT_* flags */
Tejun Heobc755552013-11-28 14:54:41 -0500177
178 /* private fields, do not use outside kernfs proper */
Shaohua Li7d350792017-07-12 11:49:46 -0700179 struct idr ino_idr;
Tejun Heoe23f5682019-11-04 15:54:29 -0800180 u32 last_ino;
Shaohua Li4a3ef682017-07-12 11:49:47 -0700181 u32 next_generation;
Tejun Heo90c07c82014-02-03 14:09:09 -0500182 struct kernfs_syscall_ops *syscall_ops;
Tejun Heo7d568a82014-04-09 11:07:30 -0400183
184 /* list of kernfs_super_info of this root, protected by kernfs_mutex */
185 struct list_head supers;
186
Tejun Heoabd54f02014-02-03 14:02:55 -0500187 wait_queue_head_t deactivate_waitq;
Tejun Heoba7443b2013-11-28 14:54:40 -0500188};
189
Tejun Heoc525aad2013-12-11 14:11:55 -0500190struct kernfs_open_file {
Tejun Heodd8a5b02013-11-28 14:54:20 -0500191 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500192 struct kernfs_node *kn;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500193 struct file *file;
Tejun Heo0e67db22016-12-27 14:49:03 -0500194 struct seq_file *seq_file;
Tejun Heo25363902014-02-03 14:09:14 -0500195 void *priv;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500196
197 /* private fields, do not use outside kernfs proper */
198 struct mutex mutex;
Chris Wilsone4234a12016-03-31 11:45:06 +0100199 struct mutex prealloc_mutex;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500200 int event;
201 struct list_head list;
NeilBrown2b758692014-10-13 16:41:28 +1100202 char *prealloc_buf;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500203
Tejun Heob7ce40c2014-03-04 15:38:46 -0500204 size_t atomic_write_len;
Tejun Heoa1d82af2016-12-27 14:49:02 -0500205 bool mmapped:1;
Tejun Heo0e67db22016-12-27 14:49:03 -0500206 bool released:1;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500207 const struct vm_operations_struct *vm_ops;
208};
209
Tejun Heof6acf8b2013-11-28 14:54:21 -0500210struct kernfs_ops {
211 /*
Tejun Heo0e67db22016-12-27 14:49:03 -0500212 * Optional open/release methods. Both are called with
213 * @of->seq_file populated.
214 */
215 int (*open)(struct kernfs_open_file *of);
216 void (*release)(struct kernfs_open_file *of);
217
218 /*
Tejun Heof6acf8b2013-11-28 14:54:21 -0500219 * Read is handled by either seq_file or raw_read().
220 *
Tejun Heod19b9842013-11-28 14:54:26 -0500221 * If seq_show() is present, seq_file path is active. Other seq
222 * operations are optional and if not implemented, the behavior is
223 * equivalent to single_open(). @sf->private points to the
Tejun Heoc525aad2013-12-11 14:11:55 -0500224 * associated kernfs_open_file.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500225 *
226 * read() is bounced through kernel buffer and a read larger than
227 * PAGE_SIZE results in partial operation of PAGE_SIZE.
228 */
229 int (*seq_show)(struct seq_file *sf, void *v);
230
Tejun Heod19b9842013-11-28 14:54:26 -0500231 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
232 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
233 void (*seq_stop)(struct seq_file *sf, void *v);
234
Tejun Heoc525aad2013-12-11 14:11:55 -0500235 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500236 loff_t off);
237
238 /*
Tejun Heo4d3773c2014-02-03 14:09:13 -0500239 * write() is bounced through kernel buffer. If atomic_write_len
240 * is not set, a write larger than PAGE_SIZE results in partial
241 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
242 * writes upto the specified size are executed atomically but
243 * larger ones are rejected with -E2BIG.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500244 */
Tejun Heo4d3773c2014-02-03 14:09:13 -0500245 size_t atomic_write_len;
NeilBrown2b758692014-10-13 16:41:28 +1100246 /*
247 * "prealloc" causes a buffer to be allocated at open for
248 * all read/write requests. As ->seq_show uses seq_read()
249 * which does its own allocation, it is incompatible with
250 * ->prealloc. Provide ->read and ->write with ->prealloc.
251 */
252 bool prealloc;
Tejun Heoc525aad2013-12-11 14:11:55 -0500253 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500254 loff_t off);
255
Johannes Weiner147e1a92019-03-05 15:45:45 -0800256 __poll_t (*poll)(struct kernfs_open_file *of,
257 struct poll_table_struct *pt);
258
Tejun Heoc525aad2013-12-11 14:11:55 -0500259 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -0500260
261#ifdef CONFIG_DEBUG_LOCK_ALLOC
262 struct lock_class_key lockdep_key;
263#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -0500264};
265
David Howells23bf1b62018-11-01 23:07:26 +0000266/*
267 * The kernfs superblock creation/mount parameter context.
268 */
269struct kernfs_fs_context {
270 struct kernfs_root *root; /* Root of the hierarchy being mounted */
271 void *ns_tag; /* Namespace tag of the mount (or NULL) */
272 unsigned long magic; /* File system specific magic number */
273
274 /* The following are set/used by kernfs_mount() */
275 bool new_sb_created; /* Set to T if we allocated a new sb */
276};
277
Tejun Heoba341d52014-02-03 14:09:17 -0500278#ifdef CONFIG_KERNFS
Tejun Heo879f40d2013-11-23 17:21:49 -0500279
Tejun Heodf23fc32013-12-11 14:11:56 -0500280static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500281{
Tejun Heodf23fc32013-12-11 14:11:56 -0500282 return kn->flags & KERNFS_TYPE_MASK;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500283}
284
Tejun Heo67c04962019-11-04 15:54:30 -0800285static inline ino_t kernfs_id_ino(u64 id)
286{
287 return (u32)id;
288}
289
290static inline u32 kernfs_id_gen(u64 id)
291{
292 return id >> 32;
293}
294
295static inline ino_t kernfs_ino(struct kernfs_node *kn)
296{
297 return kernfs_id_ino(kn->id);
298}
299
300static inline ino_t kernfs_gen(struct kernfs_node *kn)
301{
302 return kernfs_id_gen(kn->id);
303}
304
Tejun Heocf9e5a72013-11-29 17:18:32 -0500305/**
306 * kernfs_enable_ns - enable namespace under a directory
Tejun Heo324a56e2013-12-11 14:11:53 -0500307 * @kn: directory of interest, should be empty
Tejun Heocf9e5a72013-11-29 17:18:32 -0500308 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500309 * This is to be called right after @kn is created to enable namespace
310 * under it. All children of @kn must have non-NULL namespace tags and
Tejun Heocf9e5a72013-11-29 17:18:32 -0500311 * only the ones which match the super_block's tag will be visible.
312 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500313static inline void kernfs_enable_ns(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500314{
Tejun Heodf23fc32013-12-11 14:11:56 -0500315 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500316 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
Tejun Heodf23fc32013-12-11 14:11:56 -0500317 kn->flags |= KERNFS_NS;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500318}
319
Tejun Heoac9bba02013-11-29 17:19:09 -0500320/**
321 * kernfs_ns_enabled - test whether namespace is enabled
Tejun Heo324a56e2013-12-11 14:11:53 -0500322 * @kn: the node to test
Tejun Heoac9bba02013-11-29 17:19:09 -0500323 *
324 * Test whether namespace filtering is enabled for the children of @ns.
325 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500326static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500327{
Tejun Heodf23fc32013-12-11 14:11:56 -0500328 return kn->flags & KERNFS_NS;
Tejun Heoac9bba02013-11-29 17:19:09 -0500329}
330
Tejun Heo3eef34a2014-02-07 13:32:07 -0500331int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
Aditya Kali9f6df572016-01-29 02:54:04 -0600332int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
333 char *buf, size_t buflen);
Tejun Heo3eef34a2014-02-07 13:32:07 -0500334void pr_cont_kernfs_name(struct kernfs_node *kn);
335void pr_cont_kernfs_path(struct kernfs_node *kn);
336struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500337struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
338 const char *name, const void *ns);
Tejun Heobd96f762015-11-20 15:55:52 -0500339struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
340 const char *path, const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500341void kernfs_get(struct kernfs_node *kn);
342void kernfs_put(struct kernfs_node *kn);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500343
Tejun Heo0c23b222014-02-03 14:09:15 -0500344struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
345struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
Tejun Heofb029152015-06-18 16:54:28 -0400346struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
Tejun Heo0c23b222014-02-03 14:09:15 -0500347
Aditya Kalifb3c8312016-01-29 02:54:08 -0600348struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
349 struct super_block *sb);
Tejun Heo90c07c82014-02-03 14:09:09 -0500350struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500351 unsigned int flags, void *priv);
Tejun Heoba7443b2013-11-28 14:54:40 -0500352void kernfs_destroy_root(struct kernfs_root *root);
353
Tejun Heo324a56e2013-12-11 14:11:53 -0500354struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500355 const char *name, umode_t mode,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000356 kuid_t uid, kgid_t gid,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500357 void *priv, const void *ns);
Eric W. Biedermanea015212015-05-13 16:09:29 -0500358struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
359 const char *name);
Tejun Heo2063d602013-12-11 16:02:57 -0500360struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000361 const char *name, umode_t mode,
362 kuid_t uid, kgid_t gid,
363 loff_t size,
Tejun Heo2063d602013-12-11 16:02:57 -0500364 const struct kernfs_ops *ops,
365 void *priv, const void *ns,
Tejun Heo2063d602013-12-11 16:02:57 -0500366 struct lock_class_key *key);
Tejun Heo324a56e2013-12-11 14:11:53 -0500367struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
368 const char *name,
369 struct kernfs_node *target);
Tejun Heod35258e2014-02-03 14:09:12 -0500370void kernfs_activate(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500371void kernfs_remove(struct kernfs_node *kn);
Tejun Heo6b0afc22014-02-03 14:03:01 -0500372void kernfs_break_active_protection(struct kernfs_node *kn);
373void kernfs_unbreak_active_protection(struct kernfs_node *kn);
374bool kernfs_remove_self(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500375int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heo879f40d2013-11-23 17:21:49 -0500376 const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500377int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500378 const char *new_name, const void *new_ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500379int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
Johannes Weiner147e1a92019-03-05 15:45:45 -0800380__poll_t kernfs_generic_poll(struct kernfs_open_file *of,
381 struct poll_table_struct *pt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500382void kernfs_notify(struct kernfs_node *kn);
Tejun Heo879f40d2013-11-23 17:21:49 -0500383
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200384int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
385 void *value, size_t size);
386int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
387 const void *value, size_t size, int flags);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100388
Tejun Heo4b93dc92013-11-28 14:54:43 -0500389const void *kernfs_super_ns(struct super_block *sb);
David Howells23bf1b62018-11-01 23:07:26 +0000390int kernfs_get_tree(struct fs_context *fc);
391void kernfs_free_fs_context(struct fs_context *fc);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500392void kernfs_kill_sb(struct super_block *sb);
393
394void kernfs_init(void);
395
Tejun Heofe0f7262019-11-04 15:54:30 -0800396struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
397 u64 id);
Tejun Heoba341d52014-02-03 14:09:17 -0500398#else /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500399
Tejun Heodf23fc32013-12-11 14:11:56 -0500400static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500401{ return 0; } /* whatever */
402
Tejun Heo324a56e2013-12-11 14:11:53 -0500403static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
Tejun Heocf9e5a72013-11-29 17:18:32 -0500404
Tejun Heo324a56e2013-12-11 14:11:53 -0500405static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500406{ return false; }
407
Tejun Heo3eef34a2014-02-07 13:32:07 -0500408static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
409{ return -ENOSYS; }
410
Tejun Heo0e0b2af2016-08-10 11:23:43 -0400411static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
412 struct kernfs_node *kn,
413 char *buf, size_t buflen)
414{ return -ENOSYS; }
415
Tejun Heo3eef34a2014-02-07 13:32:07 -0500416static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
417static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
418
419static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
420{ return NULL; }
421
Tejun Heo324a56e2013-12-11 14:11:53 -0500422static inline struct kernfs_node *
423kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
Tejun Heoccf73cf2013-11-28 14:54:30 -0500424 const void *ns)
425{ return NULL; }
Tejun Heobd96f762015-11-20 15:55:52 -0500426static inline struct kernfs_node *
427kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
428 const void *ns)
429{ return NULL; }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500430
Tejun Heo324a56e2013-12-11 14:11:53 -0500431static inline void kernfs_get(struct kernfs_node *kn) { }
432static inline void kernfs_put(struct kernfs_node *kn) { }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500433
Tejun Heo0c23b222014-02-03 14:09:15 -0500434static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
435{ return NULL; }
436
437static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
438{ return NULL; }
439
Tejun Heofb029152015-06-18 16:54:28 -0400440static inline struct inode *
441kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
442{ return NULL; }
443
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500444static inline struct kernfs_root *
Tejun Heod35258e2014-02-03 14:09:12 -0500445kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
446 void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500447{ return ERR_PTR(-ENOSYS); }
448
449static inline void kernfs_destroy_root(struct kernfs_root *root) { }
450
Tejun Heo324a56e2013-12-11 14:11:53 -0500451static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500452kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000453 umode_t mode, kuid_t uid, kgid_t gid,
454 void *priv, const void *ns)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500455{ return ERR_PTR(-ENOSYS); }
456
Tejun Heo324a56e2013-12-11 14:11:53 -0500457static inline struct kernfs_node *
Tejun Heo2063d602013-12-11 16:02:57 -0500458__kernfs_create_file(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000459 umode_t mode, kuid_t uid, kgid_t gid,
460 loff_t size, const struct kernfs_ops *ops,
Tejun Heodfeb07502015-02-13 14:36:31 -0800461 void *priv, const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500462{ return ERR_PTR(-ENOSYS); }
463
Tejun Heo324a56e2013-12-11 14:11:53 -0500464static inline struct kernfs_node *
465kernfs_create_link(struct kernfs_node *parent, const char *name,
466 struct kernfs_node *target)
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500467{ return ERR_PTR(-ENOSYS); }
468
Tejun Heod35258e2014-02-03 14:09:12 -0500469static inline void kernfs_activate(struct kernfs_node *kn) { }
470
Tejun Heo324a56e2013-12-11 14:11:53 -0500471static inline void kernfs_remove(struct kernfs_node *kn) { }
Tejun Heo879f40d2013-11-23 17:21:49 -0500472
Tejun Heo6b0afc22014-02-03 14:03:01 -0500473static inline bool kernfs_remove_self(struct kernfs_node *kn)
474{ return false; }
475
Tejun Heo324a56e2013-12-11 14:11:53 -0500476static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
Tejun Heo879f40d2013-11-23 17:21:49 -0500477 const char *name, const void *ns)
478{ return -ENOSYS; }
479
Tejun Heo324a56e2013-12-11 14:11:53 -0500480static inline int kernfs_rename_ns(struct kernfs_node *kn,
481 struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500482 const char *new_name, const void *new_ns)
483{ return -ENOSYS; }
484
Tejun Heo324a56e2013-12-11 14:11:53 -0500485static inline int kernfs_setattr(struct kernfs_node *kn,
Tejun Heo5d604182013-11-23 17:21:52 -0500486 const struct iattr *iattr)
487{ return -ENOSYS; }
488
Tejun Heo324a56e2013-12-11 14:11:53 -0500489static inline void kernfs_notify(struct kernfs_node *kn) { }
Tejun Heo024f6472013-11-28 14:54:27 -0500490
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200491static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
492 void *value, size_t size)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100493{ return -ENOSYS; }
494
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200495static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
496 const void *value, size_t size, int flags)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100497{ return -ENOSYS; }
498
Tejun Heo4b93dc92013-11-28 14:54:43 -0500499static inline const void *kernfs_super_ns(struct super_block *sb)
500{ return NULL; }
501
David Howells23bf1b62018-11-01 23:07:26 +0000502static inline int kernfs_get_tree(struct fs_context *fc)
503{ return -ENOSYS; }
504
505static inline void kernfs_free_fs_context(struct fs_context *fc) { }
Tejun Heo4b93dc92013-11-28 14:54:43 -0500506
507static inline void kernfs_kill_sb(struct super_block *sb) { }
508
509static inline void kernfs_init(void) { }
510
Tejun Heoba341d52014-02-03 14:09:17 -0500511#endif /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500512
Tejun Heo3abb1d92016-08-10 11:23:44 -0400513/**
514 * kernfs_path - build full path of a given node
515 * @kn: kernfs_node of interest
516 * @buf: buffer to copy @kn's name into
517 * @buflen: size of @buf
518 *
Konstantin Khlebnikov8f5be0e2018-08-13 09:52:09 +0300519 * If @kn is NULL result will be "(null)".
520 *
521 * Returns the length of the full path. If the full length is equal to or
522 * greater than @buflen, @buf contains the truncated path with the trailing
523 * '\0'. On error, -errno is returned.
Tejun Heo3abb1d92016-08-10 11:23:44 -0400524 */
525static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
526{
527 return kernfs_path_from_node(kn, NULL, buf, buflen);
528}
529
Tejun Heo324a56e2013-12-11 14:11:53 -0500530static inline struct kernfs_node *
531kernfs_find_and_get(struct kernfs_node *kn, const char *name)
Tejun Heoccf73cf2013-11-28 14:54:30 -0500532{
Tejun Heo324a56e2013-12-11 14:11:53 -0500533 return kernfs_find_and_get_ns(kn, name, NULL);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500534}
535
Tejun Heo324a56e2013-12-11 14:11:53 -0500536static inline struct kernfs_node *
Tejun Heobd96f762015-11-20 15:55:52 -0500537kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
538{
539 return kernfs_walk_and_get_ns(kn, path, NULL);
540}
541
542static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500543kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
544 void *priv)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500545{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000546 return kernfs_create_dir_ns(parent, name, mode,
547 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
548 priv, NULL);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500549}
550
Tejun Heo324a56e2013-12-11 14:11:53 -0500551static inline struct kernfs_node *
552kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000553 umode_t mode, kuid_t uid, kgid_t gid,
554 loff_t size, const struct kernfs_ops *ops,
Tejun Heo517e64f2013-11-28 14:54:29 -0500555 void *priv, const void *ns)
556{
557 struct lock_class_key *key = NULL;
558
559#ifdef CONFIG_DEBUG_LOCK_ALLOC
560 key = (struct lock_class_key *)&ops->lockdep_key;
561#endif
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000562 return __kernfs_create_file(parent, name, mode, uid, gid,
563 size, ops, priv, ns, key);
Tejun Heo517e64f2013-11-28 14:54:29 -0500564}
565
Tejun Heo324a56e2013-12-11 14:11:53 -0500566static inline struct kernfs_node *
567kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
Tejun Heo496f7392013-11-28 14:54:24 -0500568 loff_t size, const struct kernfs_ops *ops, void *priv)
569{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000570 return kernfs_create_file_ns(parent, name, mode,
571 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
572 size, ops, priv, NULL);
Tejun Heo496f7392013-11-28 14:54:24 -0500573}
574
Tejun Heo324a56e2013-12-11 14:11:53 -0500575static inline int kernfs_remove_by_name(struct kernfs_node *parent,
Tejun Heo879f40d2013-11-23 17:21:49 -0500576 const char *name)
577{
578 return kernfs_remove_by_name_ns(parent, name, NULL);
579}
580
Tejun Heo0c23b222014-02-03 14:09:15 -0500581static inline int kernfs_rename(struct kernfs_node *kn,
582 struct kernfs_node *new_parent,
583 const char *new_name)
584{
585 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
586}
587
Tejun Heob8441ed2013-11-24 09:54:58 -0500588#endif /* __LINUX_KERNFS_H */