blob: 814643f7ee529eb6c9e27ec3e1653487d818595e [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;
Tejun Heo5d604182013-11-23 17:21:52 -050028
Tejun Heoc525aad2013-12-11 14:11:55 -050029struct kernfs_open_node;
30struct kernfs_iattrs;
Tejun Heocf9e5a72013-11-29 17:18:32 -050031
32enum kernfs_node_type {
Tejun Heodf23fc32013-12-11 14:11:56 -050033 KERNFS_DIR = 0x0001,
34 KERNFS_FILE = 0x0002,
35 KERNFS_LINK = 0x0004,
Tejun Heocf9e5a72013-11-29 17:18:32 -050036};
37
Tejun Heodf23fc32013-12-11 14:11:56 -050038#define KERNFS_TYPE_MASK 0x000f
Tejun Heodf23fc32013-12-11 14:11:56 -050039#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
Tejun Heocf9e5a72013-11-29 17:18:32 -050040
41enum kernfs_node_flag {
Tejun Heod35258e2014-02-03 14:09:12 -050042 KERNFS_ACTIVATED = 0x0010,
Tejun Heodf23fc32013-12-11 14:11:56 -050043 KERNFS_NS = 0x0020,
44 KERNFS_HAS_SEQ_SHOW = 0x0040,
45 KERNFS_HAS_MMAP = 0x0080,
46 KERNFS_LOCKDEP = 0x0100,
Tejun Heo6b0afc22014-02-03 14:03:01 -050047 KERNFS_SUICIDAL = 0x0400,
48 KERNFS_SUICIDED = 0x0800,
Eric W. Biedermanea015212015-05-13 16:09:29 -050049 KERNFS_EMPTY_DIR = 0x1000,
Tejun Heo0e67db22016-12-27 14:49:03 -050050 KERNFS_HAS_RELEASE = 0x2000,
Tejun Heocf9e5a72013-11-29 17:18:32 -050051};
52
Tejun Heod35258e2014-02-03 14:09:12 -050053/* @flags for kernfs_create_root() */
54enum kernfs_root_flag {
Tejun Heo555724a2014-05-12 13:56:27 -040055 /*
56 * kernfs_nodes are created in the deactivated state and invisible.
57 * They require explicit kernfs_activate() to become visible. This
58 * can be used to make related nodes become visible atomically
59 * after all nodes are created successfully.
60 */
61 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
62
63 /*
64 * For regular flies, if the opener has CAP_DAC_OVERRIDE, open(2)
65 * succeeds regardless of the RW permissions. sysfs had an extra
66 * layer of enforcement where open(2) fails with -EACCES regardless
67 * of CAP_DAC_OVERRIDE if the permission doesn't have the
68 * respective read or write access at all (none of S_IRUGO or
69 * S_IWUGO) or the respective operation isn't implemented. The
70 * following flag enables that behavior.
71 */
72 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
Shaohua Liaa818822017-07-12 11:49:51 -070073
74 /*
75 * The filesystem supports exportfs operation, so userspace can use
76 * fhandle to access nodes of the fs.
77 */
78 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
Tejun Heod35258e2014-02-03 14:09:12 -050079};
80
Tejun Heo324a56e2013-12-11 14:11:53 -050081/* type-specific structures for kernfs_node union members */
82struct kernfs_elem_dir {
Tejun Heocf9e5a72013-11-29 17:18:32 -050083 unsigned long subdirs;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050084 /* children rbtree starts here and goes through kn->rb */
Tejun Heocf9e5a72013-11-29 17:18:32 -050085 struct rb_root children;
86
87 /*
88 * The kernfs hierarchy this directory belongs to. This fits
Tejun Heo324a56e2013-12-11 14:11:53 -050089 * better directly in kernfs_node but is here to save space.
Tejun Heocf9e5a72013-11-29 17:18:32 -050090 */
91 struct kernfs_root *root;
92};
93
Tejun Heo324a56e2013-12-11 14:11:53 -050094struct kernfs_elem_symlink {
95 struct kernfs_node *target_kn;
Tejun Heocf9e5a72013-11-29 17:18:32 -050096};
97
Tejun Heo324a56e2013-12-11 14:11:53 -050098struct kernfs_elem_attr {
Tejun Heocf9e5a72013-11-29 17:18:32 -050099 const struct kernfs_ops *ops;
Tejun Heoc525aad2013-12-11 14:11:55 -0500100 struct kernfs_open_node *open;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500101 loff_t size;
Tejun Heoecca47c2014-07-01 16:41:03 -0400102 struct kernfs_node *notify_next; /* for kernfs_notify() */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500103};
104
Shaohua Lic53cd492017-07-12 11:49:50 -0700105/* represent a kernfs node */
106union kernfs_node_id {
107 struct {
Shaohua Liaa818822017-07-12 11:49:51 -0700108 /*
109 * blktrace will export this struct as a simplified 'struct
110 * fid' (which is a big data struction), so userspace can use
111 * it to find kernfs node. The layout must match the first two
112 * fields of 'struct fid' exactly.
113 */
Shaohua Lic53cd492017-07-12 11:49:50 -0700114 u32 ino;
115 u32 generation;
116 };
117 u64 id;
118};
119
Tejun Heocf9e5a72013-11-29 17:18:32 -0500120/*
Tejun Heo324a56e2013-12-11 14:11:53 -0500121 * kernfs_node - the building block of kernfs hierarchy. Each and every
122 * kernfs node is represented by single kernfs_node. Most fields are
Tejun Heocf9e5a72013-11-29 17:18:32 -0500123 * private to kernfs and shouldn't be accessed directly by kernfs users.
124 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500125 * As long as s_count reference is held, the kernfs_node itself is
126 * accessible. Dereferencing elem or any other outer entity requires
127 * active reference.
Tejun Heocf9e5a72013-11-29 17:18:32 -0500128 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500129struct kernfs_node {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500130 atomic_t count;
131 atomic_t active;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500132#ifdef CONFIG_DEBUG_LOCK_ALLOC
133 struct lockdep_map dep_map;
134#endif
Tejun Heo3eef34a2014-02-07 13:32:07 -0500135 /*
136 * Use kernfs_get_parent() and kernfs_name/path() instead of
137 * accessing the following two fields directly. If the node is
138 * never moved to a different parent, it is safe to access the
139 * parent directly.
140 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500141 struct kernfs_node *parent;
142 const char *name;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500143
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500144 struct rb_node rb;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500145
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500146 const void *ns; /* namespace tag */
Greg Kroah-Hartman9b0925a2014-01-13 14:09:38 -0800147 unsigned int hash; /* ns + name hash */
Tejun Heocf9e5a72013-11-29 17:18:32 -0500148 union {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500149 struct kernfs_elem_dir dir;
150 struct kernfs_elem_symlink symlink;
151 struct kernfs_elem_attr attr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500152 };
153
154 void *priv;
155
Shaohua Lic53cd492017-07-12 11:49:50 -0700156 union kernfs_node_id id;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500157 unsigned short flags;
158 umode_t mode;
Tejun Heoc525aad2013-12-11 14:11:55 -0500159 struct kernfs_iattrs *iattr;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500160};
Tejun Heob8441ed2013-11-24 09:54:58 -0500161
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500162/*
Tejun Heo90c07c82014-02-03 14:09:09 -0500163 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
164 * syscalls. These optional callbacks are invoked on the matching syscalls
165 * and can perform any kernfs operations which don't necessarily have to be
166 * the exact operation requested. An active reference is held for each
167 * kernfs_node parameter.
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500168 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500169struct kernfs_syscall_ops {
Tejun Heo6a7fed42014-02-03 14:09:10 -0500170 int (*remount_fs)(struct kernfs_root *root, int *flags, char *data);
171 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
172
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500173 int (*mkdir)(struct kernfs_node *parent, const char *name,
174 umode_t mode);
175 int (*rmdir)(struct kernfs_node *kn);
176 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
177 const char *new_name);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -0500178 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
179 struct kernfs_root *root);
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500180};
181
Tejun Heoba7443b2013-11-28 14:54:40 -0500182struct kernfs_root {
183 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500184 struct kernfs_node *kn;
Tejun Heod35258e2014-02-03 14:09:12 -0500185 unsigned int flags; /* KERNFS_ROOT_* flags */
Tejun Heobc755552013-11-28 14:54:41 -0500186
187 /* private fields, do not use outside kernfs proper */
Shaohua Li7d350792017-07-12 11:49:46 -0700188 struct idr ino_idr;
Shaohua Li4a3ef682017-07-12 11:49:47 -0700189 u32 next_generation;
Tejun Heo90c07c82014-02-03 14:09:09 -0500190 struct kernfs_syscall_ops *syscall_ops;
Tejun Heo7d568a82014-04-09 11:07:30 -0400191
192 /* list of kernfs_super_info of this root, protected by kernfs_mutex */
193 struct list_head supers;
194
Tejun Heoabd54f02014-02-03 14:02:55 -0500195 wait_queue_head_t deactivate_waitq;
Tejun Heoba7443b2013-11-28 14:54:40 -0500196};
197
Tejun Heoc525aad2013-12-11 14:11:55 -0500198struct kernfs_open_file {
Tejun Heodd8a5b02013-11-28 14:54:20 -0500199 /* published fields */
Tejun Heo324a56e2013-12-11 14:11:53 -0500200 struct kernfs_node *kn;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500201 struct file *file;
Tejun Heo0e67db22016-12-27 14:49:03 -0500202 struct seq_file *seq_file;
Tejun Heo25363902014-02-03 14:09:14 -0500203 void *priv;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500204
205 /* private fields, do not use outside kernfs proper */
206 struct mutex mutex;
Chris Wilsone4234a12016-03-31 11:45:06 +0100207 struct mutex prealloc_mutex;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500208 int event;
209 struct list_head list;
NeilBrown2b758692014-10-13 16:41:28 +1100210 char *prealloc_buf;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500211
Tejun Heob7ce40c2014-03-04 15:38:46 -0500212 size_t atomic_write_len;
Tejun Heoa1d82af2016-12-27 14:49:02 -0500213 bool mmapped:1;
Tejun Heo0e67db22016-12-27 14:49:03 -0500214 bool released:1;
Tejun Heodd8a5b02013-11-28 14:54:20 -0500215 const struct vm_operations_struct *vm_ops;
216};
217
Tejun Heof6acf8b2013-11-28 14:54:21 -0500218struct kernfs_ops {
219 /*
Tejun Heo0e67db22016-12-27 14:49:03 -0500220 * Optional open/release methods. Both are called with
221 * @of->seq_file populated.
222 */
223 int (*open)(struct kernfs_open_file *of);
224 void (*release)(struct kernfs_open_file *of);
225
226 /*
Tejun Heof6acf8b2013-11-28 14:54:21 -0500227 * Read is handled by either seq_file or raw_read().
228 *
Tejun Heod19b9842013-11-28 14:54:26 -0500229 * If seq_show() is present, seq_file path is active. Other seq
230 * operations are optional and if not implemented, the behavior is
231 * equivalent to single_open(). @sf->private points to the
Tejun Heoc525aad2013-12-11 14:11:55 -0500232 * associated kernfs_open_file.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500233 *
234 * read() is bounced through kernel buffer and a read larger than
235 * PAGE_SIZE results in partial operation of PAGE_SIZE.
236 */
237 int (*seq_show)(struct seq_file *sf, void *v);
238
Tejun Heod19b9842013-11-28 14:54:26 -0500239 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
240 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
241 void (*seq_stop)(struct seq_file *sf, void *v);
242
Tejun Heoc525aad2013-12-11 14:11:55 -0500243 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500244 loff_t off);
245
246 /*
Tejun Heo4d3773c2014-02-03 14:09:13 -0500247 * write() is bounced through kernel buffer. If atomic_write_len
248 * is not set, a write larger than PAGE_SIZE results in partial
249 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
250 * writes upto the specified size are executed atomically but
251 * larger ones are rejected with -E2BIG.
Tejun Heof6acf8b2013-11-28 14:54:21 -0500252 */
Tejun Heo4d3773c2014-02-03 14:09:13 -0500253 size_t atomic_write_len;
NeilBrown2b758692014-10-13 16:41:28 +1100254 /*
255 * "prealloc" causes a buffer to be allocated at open for
256 * all read/write requests. As ->seq_show uses seq_read()
257 * which does its own allocation, it is incompatible with
258 * ->prealloc. Provide ->read and ->write with ->prealloc.
259 */
260 bool prealloc;
Tejun Heoc525aad2013-12-11 14:11:55 -0500261 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500262 loff_t off);
263
Tejun Heoc525aad2013-12-11 14:11:55 -0500264 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -0500265
266#ifdef CONFIG_DEBUG_LOCK_ALLOC
267 struct lock_class_key lockdep_key;
268#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -0500269};
270
Tejun Heoba341d52014-02-03 14:09:17 -0500271#ifdef CONFIG_KERNFS
Tejun Heo879f40d2013-11-23 17:21:49 -0500272
Tejun Heodf23fc32013-12-11 14:11:56 -0500273static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500274{
Tejun Heodf23fc32013-12-11 14:11:56 -0500275 return kn->flags & KERNFS_TYPE_MASK;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500276}
277
278/**
279 * kernfs_enable_ns - enable namespace under a directory
Tejun Heo324a56e2013-12-11 14:11:53 -0500280 * @kn: directory of interest, should be empty
Tejun Heocf9e5a72013-11-29 17:18:32 -0500281 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500282 * This is to be called right after @kn is created to enable namespace
283 * under it. All children of @kn must have non-NULL namespace tags and
Tejun Heocf9e5a72013-11-29 17:18:32 -0500284 * only the ones which match the super_block's tag will be visible.
285 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500286static inline void kernfs_enable_ns(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500287{
Tejun Heodf23fc32013-12-11 14:11:56 -0500288 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500289 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
Tejun Heodf23fc32013-12-11 14:11:56 -0500290 kn->flags |= KERNFS_NS;
Tejun Heocf9e5a72013-11-29 17:18:32 -0500291}
292
Tejun Heoac9bba02013-11-29 17:19:09 -0500293/**
294 * kernfs_ns_enabled - test whether namespace is enabled
Tejun Heo324a56e2013-12-11 14:11:53 -0500295 * @kn: the node to test
Tejun Heoac9bba02013-11-29 17:19:09 -0500296 *
297 * Test whether namespace filtering is enabled for the children of @ns.
298 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500299static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500300{
Tejun Heodf23fc32013-12-11 14:11:56 -0500301 return kn->flags & KERNFS_NS;
Tejun Heoac9bba02013-11-29 17:19:09 -0500302}
303
Tejun Heo3eef34a2014-02-07 13:32:07 -0500304int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
Aditya Kali9f6df572016-01-29 02:54:04 -0600305int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
306 char *buf, size_t buflen);
Tejun Heo3eef34a2014-02-07 13:32:07 -0500307void pr_cont_kernfs_name(struct kernfs_node *kn);
308void pr_cont_kernfs_path(struct kernfs_node *kn);
309struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500310struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
311 const char *name, const void *ns);
Tejun Heobd96f762015-11-20 15:55:52 -0500312struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
313 const char *path, const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500314void kernfs_get(struct kernfs_node *kn);
315void kernfs_put(struct kernfs_node *kn);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500316
Tejun Heo0c23b222014-02-03 14:09:15 -0500317struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
318struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
Tejun Heofb029152015-06-18 16:54:28 -0400319struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
Tejun Heo0c23b222014-02-03 14:09:15 -0500320
Aditya Kalifb3c8312016-01-29 02:54:08 -0600321struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
322 struct super_block *sb);
Tejun Heo90c07c82014-02-03 14:09:09 -0500323struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500324 unsigned int flags, void *priv);
Tejun Heoba7443b2013-11-28 14:54:40 -0500325void kernfs_destroy_root(struct kernfs_root *root);
326
Tejun Heo324a56e2013-12-11 14:11:53 -0500327struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500328 const char *name, umode_t mode,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000329 kuid_t uid, kgid_t gid,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500330 void *priv, const void *ns);
Eric W. Biedermanea015212015-05-13 16:09:29 -0500331struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
332 const char *name);
Tejun Heo2063d602013-12-11 16:02:57 -0500333struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000334 const char *name, umode_t mode,
335 kuid_t uid, kgid_t gid,
336 loff_t size,
Tejun Heo2063d602013-12-11 16:02:57 -0500337 const struct kernfs_ops *ops,
338 void *priv, const void *ns,
Tejun Heo2063d602013-12-11 16:02:57 -0500339 struct lock_class_key *key);
Tejun Heo324a56e2013-12-11 14:11:53 -0500340struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
341 const char *name,
342 struct kernfs_node *target);
Tejun Heod35258e2014-02-03 14:09:12 -0500343void kernfs_activate(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500344void kernfs_remove(struct kernfs_node *kn);
Tejun Heo6b0afc22014-02-03 14:03:01 -0500345void kernfs_break_active_protection(struct kernfs_node *kn);
346void kernfs_unbreak_active_protection(struct kernfs_node *kn);
347bool kernfs_remove_self(struct kernfs_node *kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500348int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heo879f40d2013-11-23 17:21:49 -0500349 const void *ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500350int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500351 const char *new_name, const void *new_ns);
Tejun Heo324a56e2013-12-11 14:11:53 -0500352int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
353void kernfs_notify(struct kernfs_node *kn);
Tejun Heo879f40d2013-11-23 17:21:49 -0500354
Tejun Heo4b93dc92013-11-28 14:54:43 -0500355const void *kernfs_super_ns(struct super_block *sb);
356struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
Jianyu Zhan26fc9cd2014-04-26 15:40:28 +0800357 struct kernfs_root *root, unsigned long magic,
358 bool *new_sb_created, const void *ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500359void kernfs_kill_sb(struct super_block *sb);
Li Zefan4e264452014-06-30 11:50:28 +0800360struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500361
362void kernfs_init(void);
363
Shaohua Li69fd5c32017-07-12 11:49:55 -0700364struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root,
365 const union kernfs_node_id *id);
Tejun Heoba341d52014-02-03 14:09:17 -0500366#else /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500367
Tejun Heodf23fc32013-12-11 14:11:56 -0500368static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
Tejun Heocf9e5a72013-11-29 17:18:32 -0500369{ return 0; } /* whatever */
370
Tejun Heo324a56e2013-12-11 14:11:53 -0500371static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
Tejun Heocf9e5a72013-11-29 17:18:32 -0500372
Tejun Heo324a56e2013-12-11 14:11:53 -0500373static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
Tejun Heoac9bba02013-11-29 17:19:09 -0500374{ return false; }
375
Tejun Heo3eef34a2014-02-07 13:32:07 -0500376static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
377{ return -ENOSYS; }
378
Tejun Heo0e0b2af2016-08-10 11:23:43 -0400379static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
380 struct kernfs_node *kn,
381 char *buf, size_t buflen)
382{ return -ENOSYS; }
383
Tejun Heo3eef34a2014-02-07 13:32:07 -0500384static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
385static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
386
387static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
388{ return NULL; }
389
Tejun Heo324a56e2013-12-11 14:11:53 -0500390static inline struct kernfs_node *
391kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
Tejun Heoccf73cf2013-11-28 14:54:30 -0500392 const void *ns)
393{ return NULL; }
Tejun Heobd96f762015-11-20 15:55:52 -0500394static inline struct kernfs_node *
395kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
396 const void *ns)
397{ return NULL; }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500398
Tejun Heo324a56e2013-12-11 14:11:53 -0500399static inline void kernfs_get(struct kernfs_node *kn) { }
400static inline void kernfs_put(struct kernfs_node *kn) { }
Tejun Heoccf73cf2013-11-28 14:54:30 -0500401
Tejun Heo0c23b222014-02-03 14:09:15 -0500402static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
403{ return NULL; }
404
405static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
406{ return NULL; }
407
Tejun Heofb029152015-06-18 16:54:28 -0400408static inline struct inode *
409kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
410{ return NULL; }
411
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500412static inline struct kernfs_root *
Tejun Heod35258e2014-02-03 14:09:12 -0500413kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
414 void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500415{ return ERR_PTR(-ENOSYS); }
416
417static inline void kernfs_destroy_root(struct kernfs_root *root) { }
418
Tejun Heo324a56e2013-12-11 14:11:53 -0500419static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500420kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000421 umode_t mode, kuid_t uid, kgid_t gid,
422 void *priv, const void *ns)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500423{ return ERR_PTR(-ENOSYS); }
424
Tejun Heo324a56e2013-12-11 14:11:53 -0500425static inline struct kernfs_node *
Tejun Heo2063d602013-12-11 16:02:57 -0500426__kernfs_create_file(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000427 umode_t mode, kuid_t uid, kgid_t gid,
428 loff_t size, const struct kernfs_ops *ops,
Tejun Heodfeb07502015-02-13 14:36:31 -0800429 void *priv, const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500430{ return ERR_PTR(-ENOSYS); }
431
Tejun Heo324a56e2013-12-11 14:11:53 -0500432static inline struct kernfs_node *
433kernfs_create_link(struct kernfs_node *parent, const char *name,
434 struct kernfs_node *target)
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500435{ return ERR_PTR(-ENOSYS); }
436
Tejun Heod35258e2014-02-03 14:09:12 -0500437static inline void kernfs_activate(struct kernfs_node *kn) { }
438
Tejun Heo324a56e2013-12-11 14:11:53 -0500439static inline void kernfs_remove(struct kernfs_node *kn) { }
Tejun Heo879f40d2013-11-23 17:21:49 -0500440
Tejun Heo6b0afc22014-02-03 14:03:01 -0500441static inline bool kernfs_remove_self(struct kernfs_node *kn)
442{ return false; }
443
Tejun Heo324a56e2013-12-11 14:11:53 -0500444static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
Tejun Heo879f40d2013-11-23 17:21:49 -0500445 const char *name, const void *ns)
446{ return -ENOSYS; }
447
Tejun Heo324a56e2013-12-11 14:11:53 -0500448static inline int kernfs_rename_ns(struct kernfs_node *kn,
449 struct kernfs_node *new_parent,
Tejun Heo890ece12013-11-23 17:21:51 -0500450 const char *new_name, const void *new_ns)
451{ return -ENOSYS; }
452
Tejun Heo324a56e2013-12-11 14:11:53 -0500453static inline int kernfs_setattr(struct kernfs_node *kn,
Tejun Heo5d604182013-11-23 17:21:52 -0500454 const struct iattr *iattr)
455{ return -ENOSYS; }
456
Tejun Heo324a56e2013-12-11 14:11:53 -0500457static inline void kernfs_notify(struct kernfs_node *kn) { }
Tejun Heo024f6472013-11-28 14:54:27 -0500458
Tejun Heo4b93dc92013-11-28 14:54:43 -0500459static inline const void *kernfs_super_ns(struct super_block *sb)
460{ return NULL; }
461
462static inline struct dentry *
463kernfs_mount_ns(struct file_system_type *fs_type, int flags,
Jianyu Zhan26fc9cd2014-04-26 15:40:28 +0800464 struct kernfs_root *root, unsigned long magic,
465 bool *new_sb_created, const void *ns)
Tejun Heo4b93dc92013-11-28 14:54:43 -0500466{ return ERR_PTR(-ENOSYS); }
467
468static inline void kernfs_kill_sb(struct super_block *sb) { }
469
470static inline void kernfs_init(void) { }
471
Tejun Heoba341d52014-02-03 14:09:17 -0500472#endif /* CONFIG_KERNFS */
Tejun Heo879f40d2013-11-23 17:21:49 -0500473
Tejun Heo3abb1d92016-08-10 11:23:44 -0400474/**
475 * kernfs_path - build full path of a given node
476 * @kn: kernfs_node of interest
477 * @buf: buffer to copy @kn's name into
478 * @buflen: size of @buf
479 *
480 * Builds and returns the full path of @kn in @buf of @buflen bytes. The
481 * path is built from the end of @buf so the returned pointer usually
482 * doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
483 * and %NULL is returned.
484 */
485static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
486{
487 return kernfs_path_from_node(kn, NULL, buf, buflen);
488}
489
Tejun Heo324a56e2013-12-11 14:11:53 -0500490static inline struct kernfs_node *
491kernfs_find_and_get(struct kernfs_node *kn, const char *name)
Tejun Heoccf73cf2013-11-28 14:54:30 -0500492{
Tejun Heo324a56e2013-12-11 14:11:53 -0500493 return kernfs_find_and_get_ns(kn, name, NULL);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500494}
495
Tejun Heo324a56e2013-12-11 14:11:53 -0500496static inline struct kernfs_node *
Tejun Heobd96f762015-11-20 15:55:52 -0500497kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
498{
499 return kernfs_walk_and_get_ns(kn, path, NULL);
500}
501
502static inline struct kernfs_node *
Tejun Heobb8b9d02013-12-11 16:02:55 -0500503kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
504 void *priv)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500505{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000506 return kernfs_create_dir_ns(parent, name, mode,
507 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
508 priv, NULL);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500509}
510
Tejun Heo324a56e2013-12-11 14:11:53 -0500511static inline struct kernfs_node *
512kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000513 umode_t mode, kuid_t uid, kgid_t gid,
514 loff_t size, const struct kernfs_ops *ops,
Tejun Heo517e64f2013-11-28 14:54:29 -0500515 void *priv, const void *ns)
516{
517 struct lock_class_key *key = NULL;
518
519#ifdef CONFIG_DEBUG_LOCK_ALLOC
520 key = (struct lock_class_key *)&ops->lockdep_key;
521#endif
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000522 return __kernfs_create_file(parent, name, mode, uid, gid,
523 size, ops, priv, ns, key);
Tejun Heo517e64f2013-11-28 14:54:29 -0500524}
525
Tejun Heo324a56e2013-12-11 14:11:53 -0500526static inline struct kernfs_node *
527kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
Tejun Heo496f7392013-11-28 14:54:24 -0500528 loff_t size, const struct kernfs_ops *ops, void *priv)
529{
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000530 return kernfs_create_file_ns(parent, name, mode,
531 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
532 size, ops, priv, NULL);
Tejun Heo496f7392013-11-28 14:54:24 -0500533}
534
Tejun Heo324a56e2013-12-11 14:11:53 -0500535static inline int kernfs_remove_by_name(struct kernfs_node *parent,
Tejun Heo879f40d2013-11-23 17:21:49 -0500536 const char *name)
537{
538 return kernfs_remove_by_name_ns(parent, name, NULL);
539}
540
Tejun Heo0c23b222014-02-03 14:09:15 -0500541static inline int kernfs_rename(struct kernfs_node *kn,
542 struct kernfs_node *new_parent,
543 const char *new_name)
544{
545 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
546}
547
Tejun Heo4b93dc92013-11-28 14:54:43 -0500548static inline struct dentry *
549kernfs_mount(struct file_system_type *fs_type, int flags,
Jianyu Zhan26fc9cd2014-04-26 15:40:28 +0800550 struct kernfs_root *root, unsigned long magic,
551 bool *new_sb_created)
Tejun Heo4b93dc92013-11-28 14:54:43 -0500552{
Jianyu Zhan26fc9cd2014-04-26 15:40:28 +0800553 return kernfs_mount_ns(fs_type, flags, root,
554 magic, new_sb_created, NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500555}
556
Tejun Heob8441ed2013-11-24 09:54:58 -0500557#endif /* __LINUX_KERNFS_H */