blob: 936b61bd504e45848444ae67bc218466379e8b7a [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
Shaohua Lic53cd492017-07-12 11:49:50 -0700107/* represent a kernfs node */
108union kernfs_node_id {
109 struct {
Shaohua Liaa818822017-07-12 11:49:51 -0700110 /*
111 * blktrace will export this struct as a simplified 'struct
112 * fid' (which is a big data struction), so userspace can use
113 * it to find kernfs node. The layout must match the first two
114 * fields of 'struct fid' exactly.
115 */
Shaohua Lic53cd492017-07-12 11:49:50 -0700116 u32 ino;
117 u32 generation;
118 };
119 u64 id;
120};
121
Tejun Heocf9e5a72013-11-29 17:18:32 -0500122/*
Tejun Heo324a56e2013-12-11 14:11:53 -0500123 * kernfs_node - the building block of kernfs hierarchy. Each and every
124 * kernfs node is represented by single kernfs_node. Most fields are
Tejun Heocf9e5a72013-11-29 17:18:32 -0500125 * private to kernfs and shouldn't be accessed directly by kernfs users.
126 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500127 * As long as s_count reference is held, the kernfs_node itself is
128 * accessible. Dereferencing elem or any other outer entity requires
129 * active reference.
Tejun Heocf9e5a72013-11-29 17:18:32 -0500130 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500131struct kernfs_node {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500132 atomic_t count;
133 atomic_t active;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500134#ifdef CONFIG_DEBUG_LOCK_ALLOC
135 struct lockdep_map dep_map;
136#endif
Tejun Heo3eef34a2014-02-07 13:32:07 -0500137 /*
138 * Use kernfs_get_parent() and kernfs_name/path() instead of
139 * accessing the following two fields directly. If the node is
140 * never moved to a different parent, it is safe to access the
141 * parent directly.
142 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500143 struct kernfs_node *parent;
144 const char *name;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500145
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500146 struct rb_node rb;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500147
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500148 const void *ns; /* namespace tag */
Greg Kroah-Hartman9b0925a2014-01-13 14:09:38 -0800149 unsigned int hash; /* ns + name hash */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500150 union {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500151 struct kernfs_elem_dir dir;
152 struct kernfs_elem_symlink symlink;
153 struct kernfs_elem_attr attr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500154 };
155
156 void *priv;
157
Shaohua Lic53cd492017-07-12 11:49:50 -0700158 union kernfs_node_id id;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500159 unsigned short flags;
160 umode_t mode;
Tejun Heoc525aad2013-12-11 14:11:55 -0500161 struct kernfs_iattrs *iattr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500162};
Tejun Heob8441ed2013-11-24 09:54:58 -0500163
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500164/*
Tejun Heo90c07c82014-02-03 14:09:09 -0500165 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
166 * syscalls. These optional callbacks are invoked on the matching syscalls
167 * and can perform any kernfs operations which don't necessarily have to be
168 * the exact operation requested. An active reference is held for each
169 * kernfs_node parameter.
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500170 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500171struct kernfs_syscall_ops {
Tejun Heo6a7fed42014-02-03 14:09:10 -0500172 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
173
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500174 int (*mkdir)(struct kernfs_node *parent, const char *name,
175 umode_t mode);
176 int (*rmdir)(struct kernfs_node *kn);
177 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
178 const char *new_name);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -0500179 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
180 struct kernfs_root *root);
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500181};
182
Tejun Heoba7443b2013-11-28 14:54:40 -0500183struct kernfs_root {
184 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500185 struct kernfs_node *kn;
Tejun Heod35258e2014-02-03 14:09:12 -0500186 unsigned int flags; /* KERNFS_ROOT_* flags */
Tejun Heobc755552013-11-28 14:54:41 -0500187
188 /* private fields, do not use outside kernfs proper */
Shaohua Li7d350792017-07-12 11:49:46 -0700189 struct idr ino_idr;
Shaohua Li4a3ef682017-07-12 11:49:47 -0700190 u32 next_generation;
Tejun Heo90c07c82014-02-03 14:09:09 -0500191 struct kernfs_syscall_ops *syscall_ops;
Tejun Heo7d568a82014-04-09 11:07:30 -0400192
193 /* list of kernfs_super_info of this root, protected by kernfs_mutex */
194 struct list_head supers;
195
Tejun Heoabd54f02014-02-03 14:02:55 -0500196 wait_queue_head_t deactivate_waitq;
Tejun Heoba7443b2013-11-28 14:54:40 -0500197};
198
Tejun Heoc525aad2013-12-11 14:11:55 -0500199struct kernfs_open_file {
Tejun Heodd8a5b02013-11-28 14:54:20 -0500200 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500201 struct kernfs_node *kn;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500202 struct file *file;
Tejun Heo0e67db22016-12-27 14:49:03 -0500203 struct seq_file *seq_file;
Tejun Heo2536390d2014-02-03 14:09:14 -0500204 void *priv;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500205
206 /* private fields, do not use outside kernfs proper */
207 struct mutex mutex;
Chris Wilsone4234a12016-03-31 11:45:06 +0100208 struct mutex prealloc_mutex;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500209 int event;
210 struct list_head list;
NeilBrown2b758692014-10-13 16:41:28 +1100211 char *prealloc_buf;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500212
Tejun Heob7ce40c2014-03-04 15:38:46 -0500213 size_t atomic_write_len;
Tejun Heoa1d82af2016-12-27 14:49:02 -0500214 bool mmapped:1;
Tejun Heo0e67db22016-12-27 14:49:03 -0500215 bool released:1;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500216 const struct vm_operations_struct *vm_ops;
217};
218
Tejun Heof6acf8b2013-11-28 14:54:21 -0500219struct kernfs_ops {
220 /*
Tejun Heo0e67db22016-12-27 14:49:03 -0500221 * Optional open/release methods. Both are called with
222 * @of->seq_file populated.
223 */
224 int (*open)(struct kernfs_open_file *of);
225 void (*release)(struct kernfs_open_file *of);
226
227 /*
Tejun Heof6acf8b2013-11-28 14:54:21 -0500228 * Read is handled by either seq_file or raw_read().
229 *
Tejun Heod19b9842013-11-28 14:54:26 -0500230 * If seq_show() is present, seq_file path is active. Other seq
231 * operations are optional and if not implemented, the behavior is
232 * equivalent to single_open(). @sf->private points to the
Tejun Heoc525aad2013-12-11 14:11:55 -0500233 * associated kernfs_open_file.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500234 *
235 * read() is bounced through kernel buffer and a read larger than
236 * PAGE_SIZE results in partial operation of PAGE_SIZE.
237 */
238 int (*seq_show)(struct seq_file *sf, void *v);
239
Tejun Heod19b9842013-11-28 14:54:26 -0500240 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
241 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
242 void (*seq_stop)(struct seq_file *sf, void *v);
243
Tejun Heoc525aad2013-12-11 14:11:55 -0500244 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500245 loff_t off);
246
247 /*
Tejun Heo4d3773c2014-02-03 14:09:13 -0500248 * write() is bounced through kernel buffer. If atomic_write_len
249 * is not set, a write larger than PAGE_SIZE results in partial
250 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
251 * writes upto the specified size are executed atomically but
252 * larger ones are rejected with -E2BIG.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500253 */
Tejun Heo4d3773c2014-02-03 14:09:13 -0500254 size_t atomic_write_len;
NeilBrown2b758692014-10-13 16:41:28 +1100255 /*
256 * "prealloc" causes a buffer to be allocated at open for
257 * all read/write requests. As ->seq_show uses seq_read()
258 * which does its own allocation, it is incompatible with
259 * ->prealloc. Provide ->read and ->write with ->prealloc.
260 */
261 bool prealloc;
Tejun Heoc525aad2013-12-11 14:11:55 -0500262 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500263 loff_t off);
264
Johannes Weiner147e1a92019-03-05 15:45:45 -0800265 __poll_t (*poll)(struct kernfs_open_file *of,
266 struct poll_table_struct *pt);
267
Tejun Heoc525aad2013-12-11 14:11:55 -0500268 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -0500269
270#ifdef CONFIG_DEBUG_LOCK_ALLOC
271 struct lock_class_key lockdep_key;
272#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -0500273};
274
David Howells23bf1b62018-11-01 23:07:26 +0000275/*
276 * The kernfs superblock creation/mount parameter context.
277 */
278struct kernfs_fs_context {
279 struct kernfs_root *root; /* Root of the hierarchy being mounted */
280 void *ns_tag; /* Namespace tag of the mount (or NULL) */
281 unsigned long magic; /* File system specific magic number */
282
283 /* The following are set/used by kernfs_mount() */
284 bool new_sb_created; /* Set to T if we allocated a new sb */
285};
286
Tejun Heoba341d52014-02-03 14:09:17 -0500287#ifdef CONFIG_KERNFS
Tejun Heo879f40d2013-11-23 17:21:49 -0500288
Tejun Heodf23fc32013-12-11 14:11:56 -0500289static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500290{
Tejun Heodf23fc32013-12-11 14:11:56 -0500291 return kn->flags & KERNFS_TYPE_MASK;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500292}
293
294/**
295 * kernfs_enable_ns - enable namespace under a directory
Tejun Heo324a56e2013-12-11 14:11:53 -0500296 * @kn: directory of interest, should be empty
Tejun Heocf9e5a72013-11-29 17:18:32 -0500297 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500298 * This is to be called right after @kn is created to enable namespace
299 * under it. All children of @kn must have non-NULL namespace tags and
Tejun Heocf9e5a72013-11-29 17:18:32 -0500300 * only the ones which match the super_block's tag will be visible.
301 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500302static inline void kernfs_enable_ns(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500303{
Tejun Heodf23fc32013-12-11 14:11:56 -0500304 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500305 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
Tejun Heodf23fc32013-12-11 14:11:56 -0500306 kn->flags |= KERNFS_NS;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500307}
308
Tejun Heoac9bba02013-11-29 17:19:09 -0500309/**
310 * kernfs_ns_enabled - test whether namespace is enabled
Tejun Heo324a56e2013-12-11 14:11:53 -0500311 * @kn: the node to test
Tejun Heoac9bba02013-11-29 17:19:09 -0500312 *
313 * Test whether namespace filtering is enabled for the children of @ns.
314 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500315static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500316{
Tejun Heodf23fc32013-12-11 14:11:56 -0500317 return kn->flags & KERNFS_NS;
Tejun Heoac9bba02013-11-29 17:19:09 -0500318}
319
Tejun Heo3eef34a2014-02-07 13:32:07 -0500320int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
Aditya Kali9f6df572016-01-29 02:54:04 -0600321int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
322 char *buf, size_t buflen);
Tejun Heo3eef34a2014-02-07 13:32:07 -0500323void pr_cont_kernfs_name(struct kernfs_node *kn);
324void pr_cont_kernfs_path(struct kernfs_node *kn);
325struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500326struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
327 const char *name, const void *ns);
Tejun Heobd96f762015-11-20 15:55:52 -0500328struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
329 const char *path, const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500330void kernfs_get(struct kernfs_node *kn);
331void kernfs_put(struct kernfs_node *kn);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500332
Tejun Heo0c23b222014-02-03 14:09:15 -0500333struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
334struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
Tejun Heofb029152015-06-18 16:54:28 -0400335struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
Tejun Heo0c23b222014-02-03 14:09:15 -0500336
Aditya Kalifb3c8312016-01-29 02:54:08 -0600337struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
338 struct super_block *sb);
Tejun Heo90c07c82014-02-03 14:09:09 -0500339struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500340 unsigned int flags, void *priv);
Tejun Heoba7443b2013-11-28 14:54:40 -0500341void kernfs_destroy_root(struct kernfs_root *root);
342
Tejun Heo324a56e2013-12-11 14:11:53 -0500343struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500344 const char *name, umode_t mode,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000345 kuid_t uid, kgid_t gid,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500346 void *priv, const void *ns);
Eric W. Biedermanea015212015-05-13 16:09:29 -0500347struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
348 const char *name);
Tejun Heo2063d602013-12-11 16:02:57 -0500349struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000350 const char *name, umode_t mode,
351 kuid_t uid, kgid_t gid,
352 loff_t size,
Tejun Heo2063d602013-12-11 16:02:57 -0500353 const struct kernfs_ops *ops,
354 void *priv, const void *ns,
Tejun Heo2063d602013-12-11 16:02:57 -0500355 struct lock_class_key *key);
Tejun Heo324a56e2013-12-11 14:11:53 -0500356struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
357 const char *name,
358 struct kernfs_node *target);
Tejun Heod35258e2014-02-03 14:09:12 -0500359void kernfs_activate(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500360void kernfs_remove(struct kernfs_node *kn);
Tejun Heo6b0afc22014-02-03 14:03:01 -0500361void kernfs_break_active_protection(struct kernfs_node *kn);
362void kernfs_unbreak_active_protection(struct kernfs_node *kn);
363bool kernfs_remove_self(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500364int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heo879f40d2013-11-23 17:21:49 -0500365 const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500366int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500367 const char *new_name, const void *new_ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500368int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
Johannes Weiner147e1a92019-03-05 15:45:45 -0800369__poll_t kernfs_generic_poll(struct kernfs_open_file *of,
370 struct poll_table_struct *pt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500371void kernfs_notify(struct kernfs_node *kn);
Tejun Heo879f40d2013-11-23 17:21:49 -0500372
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200373int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
374 void *value, size_t size);
375int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
376 const void *value, size_t size, int flags);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100377
Tejun Heo4b93dc92013-11-28 14:54:43 -0500378const void *kernfs_super_ns(struct super_block *sb);
David Howells23bf1b62018-11-01 23:07:26 +0000379int kernfs_get_tree(struct fs_context *fc);
380void kernfs_free_fs_context(struct fs_context *fc);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500381void kernfs_kill_sb(struct super_block *sb);
382
383void kernfs_init(void);
384
Shaohua Li69fd5c32017-07-12 11:49:55 -0700385struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root,
386 const union kernfs_node_id *id);
Tejun Heoba341d52014-02-03 14:09:17 -0500387#else /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500388
Tejun Heodf23fc32013-12-11 14:11:56 -0500389static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500390{ return 0; } /* whatever */
391
Tejun Heo324a56e2013-12-11 14:11:53 -0500392static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
Tejun Heocf9e5a72013-11-29 17:18:32 -0500393
Tejun Heo324a56e2013-12-11 14:11:53 -0500394static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500395{ return false; }
396
Tejun Heo3eef34a2014-02-07 13:32:07 -0500397static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
398{ return -ENOSYS; }
399
Tejun Heo0e0b2af2016-08-10 11:23:43 -0400400static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
401 struct kernfs_node *kn,
402 char *buf, size_t buflen)
403{ return -ENOSYS; }
404
Tejun Heo3eef34a2014-02-07 13:32:07 -0500405static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
406static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
407
408static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
409{ return NULL; }
410
Tejun Heo324a56e2013-12-11 14:11:53 -0500411static inline struct kernfs_node *
412kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
Tejun Heoccf73cf2013-11-28 14:54:30 -0500413 const void *ns)
414{ return NULL; }
Tejun Heobd96f762015-11-20 15:55:52 -0500415static inline struct kernfs_node *
416kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
417 const void *ns)
418{ return NULL; }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500419
Tejun Heo324a56e2013-12-11 14:11:53 -0500420static inline void kernfs_get(struct kernfs_node *kn) { }
421static inline void kernfs_put(struct kernfs_node *kn) { }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500422
Tejun Heo0c23b222014-02-03 14:09:15 -0500423static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
424{ return NULL; }
425
426static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
427{ return NULL; }
428
Tejun Heofb029152015-06-18 16:54:28 -0400429static inline struct inode *
430kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
431{ return NULL; }
432
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500433static inline struct kernfs_root *
Tejun Heod35258e2014-02-03 14:09:12 -0500434kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
435 void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500436{ return ERR_PTR(-ENOSYS); }
437
438static inline void kernfs_destroy_root(struct kernfs_root *root) { }
439
Tejun Heo324a56e2013-12-11 14:11:53 -0500440static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500441kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000442 umode_t mode, kuid_t uid, kgid_t gid,
443 void *priv, const void *ns)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500444{ return ERR_PTR(-ENOSYS); }
445
Tejun Heo324a56e2013-12-11 14:11:53 -0500446static inline struct kernfs_node *
Tejun Heo2063d602013-12-11 16:02:57 -0500447__kernfs_create_file(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000448 umode_t mode, kuid_t uid, kgid_t gid,
449 loff_t size, const struct kernfs_ops *ops,
Tejun Heodfeb07502015-02-13 14:36:31 -0800450 void *priv, const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500451{ return ERR_PTR(-ENOSYS); }
452
Tejun Heo324a56e2013-12-11 14:11:53 -0500453static inline struct kernfs_node *
454kernfs_create_link(struct kernfs_node *parent, const char *name,
455 struct kernfs_node *target)
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500456{ return ERR_PTR(-ENOSYS); }
457
Tejun Heod35258e2014-02-03 14:09:12 -0500458static inline void kernfs_activate(struct kernfs_node *kn) { }
459
Tejun Heo324a56e2013-12-11 14:11:53 -0500460static inline void kernfs_remove(struct kernfs_node *kn) { }
Tejun Heo879f40d2013-11-23 17:21:49 -0500461
Tejun Heo6b0afc22014-02-03 14:03:01 -0500462static inline bool kernfs_remove_self(struct kernfs_node *kn)
463{ return false; }
464
Tejun Heo324a56e2013-12-11 14:11:53 -0500465static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
Tejun Heo879f40d2013-11-23 17:21:49 -0500466 const char *name, const void *ns)
467{ return -ENOSYS; }
468
Tejun Heo324a56e2013-12-11 14:11:53 -0500469static inline int kernfs_rename_ns(struct kernfs_node *kn,
470 struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500471 const char *new_name, const void *new_ns)
472{ return -ENOSYS; }
473
Tejun Heo324a56e2013-12-11 14:11:53 -0500474static inline int kernfs_setattr(struct kernfs_node *kn,
Tejun Heo5d604182013-11-23 17:21:52 -0500475 const struct iattr *iattr)
476{ return -ENOSYS; }
477
Tejun Heo324a56e2013-12-11 14:11:53 -0500478static inline void kernfs_notify(struct kernfs_node *kn) { }
Tejun Heo024f6472013-11-28 14:54:27 -0500479
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200480static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
481 void *value, size_t size)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100482{ return -ENOSYS; }
483
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200484static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
485 const void *value, size_t size, int flags)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100486{ return -ENOSYS; }
487
Tejun Heo4b93dc92013-11-28 14:54:43 -0500488static inline const void *kernfs_super_ns(struct super_block *sb)
489{ return NULL; }
490
David Howells23bf1b62018-11-01 23:07:26 +0000491static inline int kernfs_get_tree(struct fs_context *fc)
492{ return -ENOSYS; }
493
494static inline void kernfs_free_fs_context(struct fs_context *fc) { }
Tejun Heo4b93dc92013-11-28 14:54:43 -0500495
496static inline void kernfs_kill_sb(struct super_block *sb) { }
497
498static inline void kernfs_init(void) { }
499
Tejun Heoba341d52014-02-03 14:09:17 -0500500#endif /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500501
Tejun Heo3abb1d92016-08-10 11:23:44 -0400502/**
503 * kernfs_path - build full path of a given node
504 * @kn: kernfs_node of interest
505 * @buf: buffer to copy @kn's name into
506 * @buflen: size of @buf
507 *
Konstantin Khlebnikov8f5be0e2018-08-13 09:52:09 +0300508 * If @kn is NULL result will be "(null)".
509 *
510 * Returns the length of the full path. If the full length is equal to or
511 * greater than @buflen, @buf contains the truncated path with the trailing
512 * '\0'. On error, -errno is returned.
Tejun Heo3abb1d92016-08-10 11:23:44 -0400513 */
514static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
515{
516 return kernfs_path_from_node(kn, NULL, buf, buflen);
517}
518
Tejun Heo324a56e2013-12-11 14:11:53 -0500519static inline struct kernfs_node *
520kernfs_find_and_get(struct kernfs_node *kn, const char *name)
Tejun Heoccf73cf2013-11-28 14:54:30 -0500521{
Tejun Heo324a56e2013-12-11 14:11:53 -0500522 return kernfs_find_and_get_ns(kn, name, NULL);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500523}
524
Tejun Heo324a56e2013-12-11 14:11:53 -0500525static inline struct kernfs_node *
Tejun Heobd96f762015-11-20 15:55:52 -0500526kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
527{
528 return kernfs_walk_and_get_ns(kn, path, NULL);
529}
530
531static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500532kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
533 void *priv)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500534{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000535 return kernfs_create_dir_ns(parent, name, mode,
536 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
537 priv, NULL);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500538}
539
Tejun Heo324a56e2013-12-11 14:11:53 -0500540static inline struct kernfs_node *
541kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000542 umode_t mode, kuid_t uid, kgid_t gid,
543 loff_t size, const struct kernfs_ops *ops,
Tejun Heo517e64f2013-11-28 14:54:29 -0500544 void *priv, const void *ns)
545{
546 struct lock_class_key *key = NULL;
547
548#ifdef CONFIG_DEBUG_LOCK_ALLOC
549 key = (struct lock_class_key *)&ops->lockdep_key;
550#endif
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000551 return __kernfs_create_file(parent, name, mode, uid, gid,
552 size, ops, priv, ns, key);
Tejun Heo517e64f2013-11-28 14:54:29 -0500553}
554
Tejun Heo324a56e2013-12-11 14:11:53 -0500555static inline struct kernfs_node *
556kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
Tejun Heo496f7392013-11-28 14:54:24 -0500557 loff_t size, const struct kernfs_ops *ops, void *priv)
558{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000559 return kernfs_create_file_ns(parent, name, mode,
560 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
561 size, ops, priv, NULL);
Tejun Heo496f7392013-11-28 14:54:24 -0500562}
563
Tejun Heo324a56e2013-12-11 14:11:53 -0500564static inline int kernfs_remove_by_name(struct kernfs_node *parent,
Tejun Heo879f40d2013-11-23 17:21:49 -0500565 const char *name)
566{
567 return kernfs_remove_by_name_ns(parent, name, NULL);
568}
569
Tejun Heo0c23b222014-02-03 14:09:15 -0500570static inline int kernfs_rename(struct kernfs_node *kn,
571 struct kernfs_node *new_parent,
572 const char *new_name)
573{
574 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
575}
576
Tejun Heob8441ed2013-11-24 09:54:58 -0500577#endif /* __LINUX_KERNFS_H */