blob: c8893f6634705eb8da4795455ce67820ae98d7e4 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
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
Tejun Heo879f40d2013-11-23 17:21:49 -050010#include <linux/kernel.h>
Tejun Heo5d0e26b2013-11-23 17:21:50 -050011#include <linux/err.h>
Tejun Heodd8a5b02013-11-28 14:54:20 -050012#include <linux/list.h>
13#include <linux/mutex.h>
Tejun Heobc755552013-11-28 14:54:41 -050014#include <linux/idr.h>
Tejun Heo517e64f2013-11-28 14:54:29 -050015#include <linux/lockdep.h>
Tejun Heocf9e5a72013-11-29 17:18:32 -050016#include <linux/rbtree.h>
17#include <linux/atomic.h>
Dmitry Torokhov488dee92018-07-20 21:56:47 +000018#include <linux/uidgid.h>
Tejun Heoabd54f02014-02-03 14:02:55 -050019#include <linux/wait.h>
Tejun Heo879f40d2013-11-23 17:21:49 -050020
Tejun Heo5d604182013-11-23 17:21:52 -050021struct file;
Tejun Heo917f56c2014-01-17 09:57:49 -050022struct dentry;
Tejun Heo5d604182013-11-23 17:21:52 -050023struct iattr;
Tejun Heodd8a5b02013-11-28 14:54:20 -050024struct seq_file;
25struct vm_area_struct;
Tejun Heo4b93dc92013-11-28 14:54:43 -050026struct super_block;
27struct file_system_type;
Johannes Weiner147e1a92019-03-05 15:45:45 -080028struct poll_table_struct;
David Howells23bf1b62018-11-01 23:07:26 +000029struct fs_context;
Tejun Heo5d604182013-11-23 17:21:52 -050030
David Howells23bf1b62018-11-01 23:07:26 +000031struct kernfs_fs_context;
Tejun Heoc525aad2013-12-11 14:11:55 -050032struct kernfs_open_node;
33struct kernfs_iattrs;
Tejun Heocf9e5a72013-11-29 17:18:32 -050034
35enum kernfs_node_type {
Tejun Heodf23fc32013-12-11 14:11:56 -050036 KERNFS_DIR = 0x0001,
37 KERNFS_FILE = 0x0002,
38 KERNFS_LINK = 0x0004,
Tejun Heocf9e5a72013-11-29 17:18:32 -050039};
40
Tejun Heodf23fc32013-12-11 14:11:56 -050041#define KERNFS_TYPE_MASK 0x000f
Tejun Heodf23fc32013-12-11 14:11:56 -050042#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
Tejun Heocf9e5a72013-11-29 17:18:32 -050043
44enum kernfs_node_flag {
Tejun Heod35258e2014-02-03 14:09:12 -050045 KERNFS_ACTIVATED = 0x0010,
Tejun Heodf23fc32013-12-11 14:11:56 -050046 KERNFS_NS = 0x0020,
47 KERNFS_HAS_SEQ_SHOW = 0x0040,
48 KERNFS_HAS_MMAP = 0x0080,
49 KERNFS_LOCKDEP = 0x0100,
Tejun Heo6b0afc22014-02-03 14:03:01 -050050 KERNFS_SUICIDAL = 0x0400,
51 KERNFS_SUICIDED = 0x0800,
Eric W. Biedermanea015212015-05-13 16:09:29 -050052 KERNFS_EMPTY_DIR = 0x1000,
Tejun Heo0e67db22016-12-27 14:49:03 -050053 KERNFS_HAS_RELEASE = 0x2000,
Tejun Heocf9e5a72013-11-29 17:18:32 -050054};
55
Tejun Heod35258e2014-02-03 14:09:12 -050056/* @flags for kernfs_create_root() */
57enum kernfs_root_flag {
Tejun Heo555724a2014-05-12 13:56:27 -040058 /*
59 * kernfs_nodes are created in the deactivated state and invisible.
60 * They require explicit kernfs_activate() to become visible. This
61 * can be used to make related nodes become visible atomically
62 * after all nodes are created successfully.
63 */
64 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
65
66 /*
67 * For regular flies, if the opener has CAP_DAC_OVERRIDE, open(2)
68 * succeeds regardless of the RW permissions. sysfs had an extra
69 * layer of enforcement where open(2) fails with -EACCES regardless
70 * of CAP_DAC_OVERRIDE if the permission doesn't have the
71 * respective read or write access at all (none of S_IRUGO or
72 * S_IWUGO) or the respective operation isn't implemented. The
73 * following flag enables that behavior.
74 */
75 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
Shaohua Liaa818822017-07-12 11:49:51 -070076
77 /*
78 * The filesystem supports exportfs operation, so userspace can use
79 * fhandle to access nodes of the fs.
80 */
81 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
Tejun Heod35258e2014-02-03 14:09:12 -050082};
83
Tejun Heo324a56e2013-12-11 14:11:53 -050084/* type-specific structures for kernfs_node union members */
85struct kernfs_elem_dir {
Tejun Heocf9e5a72013-11-29 17:18:32 -050086 unsigned long subdirs;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050087 /* children rbtree starts here and goes through kn->rb */
Tejun Heocf9e5a72013-11-29 17:18:32 -050088 struct rb_root children;
89
90 /*
91 * The kernfs hierarchy this directory belongs to. This fits
Tejun Heo324a56e2013-12-11 14:11:53 -050092 * better directly in kernfs_node but is here to save space.
Tejun Heocf9e5a72013-11-29 17:18:32 -050093 */
94 struct kernfs_root *root;
95};
96
Tejun Heo324a56e2013-12-11 14:11:53 -050097struct kernfs_elem_symlink {
98 struct kernfs_node *target_kn;
Tejun Heocf9e5a72013-11-29 17:18:32 -050099};
100
Tejun Heo324a56e2013-12-11 14:11:53 -0500101struct kernfs_elem_attr {
Tejun Heocf9e5a72013-11-29 17:18:32 -0500102 const struct kernfs_ops *ops;
Tejun Heoc525aad2013-12-11 14:11:55 -0500103 struct kernfs_open_node *open;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500104 loff_t size;
Tejun Heoecca47c2014-07-01 16:41:03 -0400105 struct kernfs_node *notify_next; /* for kernfs_notify() */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500106};
107
Shaohua Lic53cd492017-07-12 11:49:50 -0700108/* represent a kernfs node */
109union kernfs_node_id {
110 struct {
Shaohua Liaa818822017-07-12 11:49:51 -0700111 /*
112 * blktrace will export this struct as a simplified 'struct
113 * fid' (which is a big data struction), so userspace can use
114 * it to find kernfs node. The layout must match the first two
115 * fields of 'struct fid' exactly.
116 */
Shaohua Lic53cd492017-07-12 11:49:50 -0700117 u32 ino;
118 u32 generation;
119 };
120 u64 id;
121};
122
Tejun Heocf9e5a72013-11-29 17:18:32 -0500123/*
Tejun Heo324a56e2013-12-11 14:11:53 -0500124 * kernfs_node - the building block of kernfs hierarchy. Each and every
125 * kernfs node is represented by single kernfs_node. Most fields are
Tejun Heocf9e5a72013-11-29 17:18:32 -0500126 * private to kernfs and shouldn't be accessed directly by kernfs users.
127 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500128 * As long as s_count reference is held, the kernfs_node itself is
129 * accessible. Dereferencing elem or any other outer entity requires
130 * active reference.
Tejun Heocf9e5a72013-11-29 17:18:32 -0500131 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500132struct kernfs_node {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500133 atomic_t count;
134 atomic_t active;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500135#ifdef CONFIG_DEBUG_LOCK_ALLOC
136 struct lockdep_map dep_map;
137#endif
Tejun Heo3eef34a2014-02-07 13:32:07 -0500138 /*
139 * Use kernfs_get_parent() and kernfs_name/path() instead of
140 * accessing the following two fields directly. If the node is
141 * never moved to a different parent, it is safe to access the
142 * parent directly.
143 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500144 struct kernfs_node *parent;
145 const char *name;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500146
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500147 struct rb_node rb;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500148
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500149 const void *ns; /* namespace tag */
Greg Kroah-Hartman9b0925a2014-01-13 14:09:38 -0800150 unsigned int hash; /* ns + name hash */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500151 union {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500152 struct kernfs_elem_dir dir;
153 struct kernfs_elem_symlink symlink;
154 struct kernfs_elem_attr attr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500155 };
156
157 void *priv;
158
Shaohua Lic53cd492017-07-12 11:49:50 -0700159 union kernfs_node_id id;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500160 unsigned short flags;
161 umode_t mode;
Tejun Heoc525aad2013-12-11 14:11:55 -0500162 struct kernfs_iattrs *iattr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500163};
Tejun Heob8441ed2013-11-24 09:54:58 -0500164
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500165/*
Tejun Heo90c07c82014-02-03 14:09:09 -0500166 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
167 * syscalls. These optional callbacks are invoked on the matching syscalls
168 * and can perform any kernfs operations which don't necessarily have to be
169 * the exact operation requested. An active reference is held for each
170 * kernfs_node parameter.
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500171 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500172struct kernfs_syscall_ops {
Tejun Heo6a7fed42014-02-03 14:09:10 -0500173 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
174
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500175 int (*mkdir)(struct kernfs_node *parent, const char *name,
176 umode_t mode);
177 int (*rmdir)(struct kernfs_node *kn);
178 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
179 const char *new_name);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -0500180 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
181 struct kernfs_root *root);
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500182};
183
Tejun Heoba7443b2013-11-28 14:54:40 -0500184struct kernfs_root {
185 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500186 struct kernfs_node *kn;
Tejun Heod35258e2014-02-03 14:09:12 -0500187 unsigned int flags; /* KERNFS_ROOT_* flags */
Tejun Heobc755552013-11-28 14:54:41 -0500188
189 /* private fields, do not use outside kernfs proper */
Shaohua Li7d350792017-07-12 11:49:46 -0700190 struct idr ino_idr;
Shaohua Li4a3ef682017-07-12 11:49:47 -0700191 u32 next_generation;
Tejun Heo90c07c82014-02-03 14:09:09 -0500192 struct kernfs_syscall_ops *syscall_ops;
Tejun Heo7d568a82014-04-09 11:07:30 -0400193
194 /* list of kernfs_super_info of this root, protected by kernfs_mutex */
195 struct list_head supers;
196
Tejun Heoabd54f02014-02-03 14:02:55 -0500197 wait_queue_head_t deactivate_waitq;
Tejun Heoba7443b2013-11-28 14:54:40 -0500198};
199
Tejun Heoc525aad2013-12-11 14:11:55 -0500200struct kernfs_open_file {
Tejun Heodd8a5b02013-11-28 14:54:20 -0500201 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500202 struct kernfs_node *kn;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500203 struct file *file;
Tejun Heo0e67db22016-12-27 14:49:03 -0500204 struct seq_file *seq_file;
Tejun Heo2536390d2014-02-03 14:09:14 -0500205 void *priv;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500206
207 /* private fields, do not use outside kernfs proper */
208 struct mutex mutex;
Chris Wilsone4234a12016-03-31 11:45:06 +0100209 struct mutex prealloc_mutex;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500210 int event;
211 struct list_head list;
NeilBrown2b758692014-10-13 16:41:28 +1100212 char *prealloc_buf;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500213
Tejun Heob7ce40c2014-03-04 15:38:46 -0500214 size_t atomic_write_len;
Tejun Heoa1d82af2016-12-27 14:49:02 -0500215 bool mmapped:1;
Tejun Heo0e67db22016-12-27 14:49:03 -0500216 bool released:1;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500217 const struct vm_operations_struct *vm_ops;
218};
219
Tejun Heof6acf8b2013-11-28 14:54:21 -0500220struct kernfs_ops {
221 /*
Tejun Heo0e67db22016-12-27 14:49:03 -0500222 * Optional open/release methods. Both are called with
223 * @of->seq_file populated.
224 */
225 int (*open)(struct kernfs_open_file *of);
226 void (*release)(struct kernfs_open_file *of);
227
228 /*
Tejun Heof6acf8b2013-11-28 14:54:21 -0500229 * Read is handled by either seq_file or raw_read().
230 *
Tejun Heod19b9842013-11-28 14:54:26 -0500231 * If seq_show() is present, seq_file path is active. Other seq
232 * operations are optional and if not implemented, the behavior is
233 * equivalent to single_open(). @sf->private points to the
Tejun Heoc525aad2013-12-11 14:11:55 -0500234 * associated kernfs_open_file.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500235 *
236 * read() is bounced through kernel buffer and a read larger than
237 * PAGE_SIZE results in partial operation of PAGE_SIZE.
238 */
239 int (*seq_show)(struct seq_file *sf, void *v);
240
Tejun Heod19b9842013-11-28 14:54:26 -0500241 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
242 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
243 void (*seq_stop)(struct seq_file *sf, void *v);
244
Tejun Heoc525aad2013-12-11 14:11:55 -0500245 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500246 loff_t off);
247
248 /*
Tejun Heo4d3773c2014-02-03 14:09:13 -0500249 * write() is bounced through kernel buffer. If atomic_write_len
250 * is not set, a write larger than PAGE_SIZE results in partial
251 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
252 * writes upto the specified size are executed atomically but
253 * larger ones are rejected with -E2BIG.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500254 */
Tejun Heo4d3773c2014-02-03 14:09:13 -0500255 size_t atomic_write_len;
NeilBrown2b758692014-10-13 16:41:28 +1100256 /*
257 * "prealloc" causes a buffer to be allocated at open for
258 * all read/write requests. As ->seq_show uses seq_read()
259 * which does its own allocation, it is incompatible with
260 * ->prealloc. Provide ->read and ->write with ->prealloc.
261 */
262 bool prealloc;
Tejun Heoc525aad2013-12-11 14:11:55 -0500263 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500264 loff_t off);
265
Johannes Weiner147e1a92019-03-05 15:45:45 -0800266 __poll_t (*poll)(struct kernfs_open_file *of,
267 struct poll_table_struct *pt);
268
Tejun Heoc525aad2013-12-11 14:11:55 -0500269 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -0500270
271#ifdef CONFIG_DEBUG_LOCK_ALLOC
272 struct lock_class_key lockdep_key;
273#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -0500274};
275
David Howells23bf1b62018-11-01 23:07:26 +0000276/*
277 * The kernfs superblock creation/mount parameter context.
278 */
279struct kernfs_fs_context {
280 struct kernfs_root *root; /* Root of the hierarchy being mounted */
281 void *ns_tag; /* Namespace tag of the mount (or NULL) */
282 unsigned long magic; /* File system specific magic number */
283
284 /* The following are set/used by kernfs_mount() */
285 bool new_sb_created; /* Set to T if we allocated a new sb */
286};
287
Tejun Heoba341d52014-02-03 14:09:17 -0500288#ifdef CONFIG_KERNFS
Tejun Heo879f40d2013-11-23 17:21:49 -0500289
Tejun Heodf23fc32013-12-11 14:11:56 -0500290static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500291{
Tejun Heodf23fc32013-12-11 14:11:56 -0500292 return kn->flags & KERNFS_TYPE_MASK;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500293}
294
295/**
296 * kernfs_enable_ns - enable namespace under a directory
Tejun Heo324a56e2013-12-11 14:11:53 -0500297 * @kn: directory of interest, should be empty
Tejun Heocf9e5a72013-11-29 17:18:32 -0500298 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500299 * This is to be called right after @kn is created to enable namespace
300 * under it. All children of @kn must have non-NULL namespace tags and
Tejun Heocf9e5a72013-11-29 17:18:32 -0500301 * only the ones which match the super_block's tag will be visible.
302 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500303static inline void kernfs_enable_ns(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500304{
Tejun Heodf23fc32013-12-11 14:11:56 -0500305 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500306 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
Tejun Heodf23fc32013-12-11 14:11:56 -0500307 kn->flags |= KERNFS_NS;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500308}
309
Tejun Heoac9bba02013-11-29 17:19:09 -0500310/**
311 * kernfs_ns_enabled - test whether namespace is enabled
Tejun Heo324a56e2013-12-11 14:11:53 -0500312 * @kn: the node to test
Tejun Heoac9bba02013-11-29 17:19:09 -0500313 *
314 * Test whether namespace filtering is enabled for the children of @ns.
315 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500316static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500317{
Tejun Heodf23fc32013-12-11 14:11:56 -0500318 return kn->flags & KERNFS_NS;
Tejun Heoac9bba02013-11-29 17:19:09 -0500319}
320
Tejun Heo3eef34a2014-02-07 13:32:07 -0500321int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
Aditya Kali9f6df572016-01-29 02:54:04 -0600322int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
323 char *buf, size_t buflen);
Tejun Heo3eef34a2014-02-07 13:32:07 -0500324void pr_cont_kernfs_name(struct kernfs_node *kn);
325void pr_cont_kernfs_path(struct kernfs_node *kn);
326struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500327struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
328 const char *name, const void *ns);
Tejun Heobd96f762015-11-20 15:55:52 -0500329struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
330 const char *path, const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500331void kernfs_get(struct kernfs_node *kn);
332void kernfs_put(struct kernfs_node *kn);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500333
Tejun Heo0c23b222014-02-03 14:09:15 -0500334struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
335struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
Tejun Heofb029152015-06-18 16:54:28 -0400336struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
Tejun Heo0c23b222014-02-03 14:09:15 -0500337
Aditya Kalifb3c8312016-01-29 02:54:08 -0600338struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
339 struct super_block *sb);
Tejun Heo90c07c82014-02-03 14:09:09 -0500340struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500341 unsigned int flags, void *priv);
Tejun Heoba7443b2013-11-28 14:54:40 -0500342void kernfs_destroy_root(struct kernfs_root *root);
343
Tejun Heo324a56e2013-12-11 14:11:53 -0500344struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500345 const char *name, umode_t mode,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000346 kuid_t uid, kgid_t gid,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500347 void *priv, const void *ns);
Eric W. Biedermanea015212015-05-13 16:09:29 -0500348struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
349 const char *name);
Tejun Heo2063d602013-12-11 16:02:57 -0500350struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000351 const char *name, umode_t mode,
352 kuid_t uid, kgid_t gid,
353 loff_t size,
Tejun Heo2063d602013-12-11 16:02:57 -0500354 const struct kernfs_ops *ops,
355 void *priv, const void *ns,
Tejun Heo2063d602013-12-11 16:02:57 -0500356 struct lock_class_key *key);
Tejun Heo324a56e2013-12-11 14:11:53 -0500357struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
358 const char *name,
359 struct kernfs_node *target);
Tejun Heod35258e2014-02-03 14:09:12 -0500360void kernfs_activate(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500361void kernfs_remove(struct kernfs_node *kn);
Tejun Heo6b0afc22014-02-03 14:03:01 -0500362void kernfs_break_active_protection(struct kernfs_node *kn);
363void kernfs_unbreak_active_protection(struct kernfs_node *kn);
364bool kernfs_remove_self(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500365int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heo879f40d2013-11-23 17:21:49 -0500366 const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500367int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500368 const char *new_name, const void *new_ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500369int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
Johannes Weiner147e1a92019-03-05 15:45:45 -0800370__poll_t kernfs_generic_poll(struct kernfs_open_file *of,
371 struct poll_table_struct *pt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500372void kernfs_notify(struct kernfs_node *kn);
Tejun Heo879f40d2013-11-23 17:21:49 -0500373
Tejun Heo4b93dc92013-11-28 14:54:43 -0500374const void *kernfs_super_ns(struct super_block *sb);
David Howells23bf1b62018-11-01 23:07:26 +0000375int kernfs_get_tree(struct fs_context *fc);
376void kernfs_free_fs_context(struct fs_context *fc);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500377void kernfs_kill_sb(struct super_block *sb);
378
379void kernfs_init(void);
380
Shaohua Li69fd5c32017-07-12 11:49:55 -0700381struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root,
382 const union kernfs_node_id *id);
Tejun Heoba341d52014-02-03 14:09:17 -0500383#else /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500384
Tejun Heodf23fc32013-12-11 14:11:56 -0500385static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500386{ return 0; } /* whatever */
387
Tejun Heo324a56e2013-12-11 14:11:53 -0500388static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
Tejun Heocf9e5a72013-11-29 17:18:32 -0500389
Tejun Heo324a56e2013-12-11 14:11:53 -0500390static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500391{ return false; }
392
Tejun Heo3eef34a2014-02-07 13:32:07 -0500393static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
394{ return -ENOSYS; }
395
Tejun Heo0e0b2af2016-08-10 11:23:43 -0400396static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
397 struct kernfs_node *kn,
398 char *buf, size_t buflen)
399{ return -ENOSYS; }
400
Tejun Heo3eef34a2014-02-07 13:32:07 -0500401static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
402static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
403
404static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
405{ return NULL; }
406
Tejun Heo324a56e2013-12-11 14:11:53 -0500407static inline struct kernfs_node *
408kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
Tejun Heoccf73cf2013-11-28 14:54:30 -0500409 const void *ns)
410{ return NULL; }
Tejun Heobd96f762015-11-20 15:55:52 -0500411static inline struct kernfs_node *
412kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
413 const void *ns)
414{ return NULL; }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500415
Tejun Heo324a56e2013-12-11 14:11:53 -0500416static inline void kernfs_get(struct kernfs_node *kn) { }
417static inline void kernfs_put(struct kernfs_node *kn) { }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500418
Tejun Heo0c23b222014-02-03 14:09:15 -0500419static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
420{ return NULL; }
421
422static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
423{ return NULL; }
424
Tejun Heofb029152015-06-18 16:54:28 -0400425static inline struct inode *
426kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
427{ return NULL; }
428
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500429static inline struct kernfs_root *
Tejun Heod35258e2014-02-03 14:09:12 -0500430kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
431 void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500432{ return ERR_PTR(-ENOSYS); }
433
434static inline void kernfs_destroy_root(struct kernfs_root *root) { }
435
Tejun Heo324a56e2013-12-11 14:11:53 -0500436static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500437kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000438 umode_t mode, kuid_t uid, kgid_t gid,
439 void *priv, const void *ns)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500440{ return ERR_PTR(-ENOSYS); }
441
Tejun Heo324a56e2013-12-11 14:11:53 -0500442static inline struct kernfs_node *
Tejun Heo2063d602013-12-11 16:02:57 -0500443__kernfs_create_file(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000444 umode_t mode, kuid_t uid, kgid_t gid,
445 loff_t size, const struct kernfs_ops *ops,
Tejun Heodfeb07502015-02-13 14:36:31 -0800446 void *priv, const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500447{ return ERR_PTR(-ENOSYS); }
448
Tejun Heo324a56e2013-12-11 14:11:53 -0500449static inline struct kernfs_node *
450kernfs_create_link(struct kernfs_node *parent, const char *name,
451 struct kernfs_node *target)
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500452{ return ERR_PTR(-ENOSYS); }
453
Tejun Heod35258e2014-02-03 14:09:12 -0500454static inline void kernfs_activate(struct kernfs_node *kn) { }
455
Tejun Heo324a56e2013-12-11 14:11:53 -0500456static inline void kernfs_remove(struct kernfs_node *kn) { }
Tejun Heo879f40d2013-11-23 17:21:49 -0500457
Tejun Heo6b0afc22014-02-03 14:03:01 -0500458static inline bool kernfs_remove_self(struct kernfs_node *kn)
459{ return false; }
460
Tejun Heo324a56e2013-12-11 14:11:53 -0500461static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
Tejun Heo879f40d2013-11-23 17:21:49 -0500462 const char *name, const void *ns)
463{ return -ENOSYS; }
464
Tejun Heo324a56e2013-12-11 14:11:53 -0500465static inline int kernfs_rename_ns(struct kernfs_node *kn,
466 struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500467 const char *new_name, const void *new_ns)
468{ return -ENOSYS; }
469
Tejun Heo324a56e2013-12-11 14:11:53 -0500470static inline int kernfs_setattr(struct kernfs_node *kn,
Tejun Heo5d604182013-11-23 17:21:52 -0500471 const struct iattr *iattr)
472{ return -ENOSYS; }
473
Tejun Heo324a56e2013-12-11 14:11:53 -0500474static inline void kernfs_notify(struct kernfs_node *kn) { }
Tejun Heo024f6472013-11-28 14:54:27 -0500475
Tejun Heo4b93dc92013-11-28 14:54:43 -0500476static inline const void *kernfs_super_ns(struct super_block *sb)
477{ return NULL; }
478
David Howells23bf1b62018-11-01 23:07:26 +0000479static inline int kernfs_get_tree(struct fs_context *fc)
480{ return -ENOSYS; }
481
482static inline void kernfs_free_fs_context(struct fs_context *fc) { }
Tejun Heo4b93dc92013-11-28 14:54:43 -0500483
484static inline void kernfs_kill_sb(struct super_block *sb) { }
485
486static inline void kernfs_init(void) { }
487
Tejun Heoba341d52014-02-03 14:09:17 -0500488#endif /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500489
Tejun Heo3abb1d92016-08-10 11:23:44 -0400490/**
491 * kernfs_path - build full path of a given node
492 * @kn: kernfs_node of interest
493 * @buf: buffer to copy @kn's name into
494 * @buflen: size of @buf
495 *
Konstantin Khlebnikov8f5be0e2018-08-13 09:52:09 +0300496 * If @kn is NULL result will be "(null)".
497 *
498 * Returns the length of the full path. If the full length is equal to or
499 * greater than @buflen, @buf contains the truncated path with the trailing
500 * '\0'. On error, -errno is returned.
Tejun Heo3abb1d92016-08-10 11:23:44 -0400501 */
502static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
503{
504 return kernfs_path_from_node(kn, NULL, buf, buflen);
505}
506
Tejun Heo324a56e2013-12-11 14:11:53 -0500507static inline struct kernfs_node *
508kernfs_find_and_get(struct kernfs_node *kn, const char *name)
Tejun Heoccf73cf2013-11-28 14:54:30 -0500509{
Tejun Heo324a56e2013-12-11 14:11:53 -0500510 return kernfs_find_and_get_ns(kn, name, NULL);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500511}
512
Tejun Heo324a56e2013-12-11 14:11:53 -0500513static inline struct kernfs_node *
Tejun Heobd96f762015-11-20 15:55:52 -0500514kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
515{
516 return kernfs_walk_and_get_ns(kn, path, NULL);
517}
518
519static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500520kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
521 void *priv)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500522{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000523 return kernfs_create_dir_ns(parent, name, mode,
524 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
525 priv, NULL);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500526}
527
Tejun Heo324a56e2013-12-11 14:11:53 -0500528static inline struct kernfs_node *
529kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000530 umode_t mode, kuid_t uid, kgid_t gid,
531 loff_t size, const struct kernfs_ops *ops,
Tejun Heo517e64f2013-11-28 14:54:29 -0500532 void *priv, const void *ns)
533{
534 struct lock_class_key *key = NULL;
535
536#ifdef CONFIG_DEBUG_LOCK_ALLOC
537 key = (struct lock_class_key *)&ops->lockdep_key;
538#endif
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000539 return __kernfs_create_file(parent, name, mode, uid, gid,
540 size, ops, priv, ns, key);
Tejun Heo517e64f2013-11-28 14:54:29 -0500541}
542
Tejun Heo324a56e2013-12-11 14:11:53 -0500543static inline struct kernfs_node *
544kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
Tejun Heo496f7392013-11-28 14:54:24 -0500545 loff_t size, const struct kernfs_ops *ops, void *priv)
546{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000547 return kernfs_create_file_ns(parent, name, mode,
548 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
549 size, ops, priv, NULL);
Tejun Heo496f7392013-11-28 14:54:24 -0500550}
551
Tejun Heo324a56e2013-12-11 14:11:53 -0500552static inline int kernfs_remove_by_name(struct kernfs_node *parent,
Tejun Heo879f40d2013-11-23 17:21:49 -0500553 const char *name)
554{
555 return kernfs_remove_by_name_ns(parent, name, NULL);
556}
557
Tejun Heo0c23b222014-02-03 14:09:15 -0500558static inline int kernfs_rename(struct kernfs_node *kn,
559 struct kernfs_node *new_parent,
560 const char *new_name)
561{
562 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
563}
564
Tejun Heob8441ed2013-11-24 09:54:58 -0500565#endif /* __LINUX_KERNFS_H */