kernfs.h 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * kernfs.h - pseudo filesystem decoupled from vfs locking
 *
 * This file is released under the GPLv2.
 */

#ifndef __LINUX_KERNFS_H
#define __LINUX_KERNFS_H

10
#include <linux/kernel.h>
11
#include <linux/err.h>
12 13
#include <linux/list.h>
#include <linux/mutex.h>
14
#include <linux/idr.h>
15
#include <linux/lockdep.h>
16 17
#include <linux/rbtree.h>
#include <linux/atomic.h>
18
#include <linux/wait.h>
19

20
struct file;
21
struct dentry;
22
struct iattr;
23 24
struct seq_file;
struct vm_area_struct;
25 26
struct super_block;
struct file_system_type;
27

28 29
struct kernfs_open_node;
struct kernfs_iattrs;
30 31

enum kernfs_node_type {
T
Tejun Heo 已提交
32 33 34
	KERNFS_DIR		= 0x0001,
	KERNFS_FILE		= 0x0002,
	KERNFS_LINK		= 0x0004,
35 36
};

T
Tejun Heo 已提交
37 38
#define KERNFS_TYPE_MASK	0x000f
#define KERNFS_FLAG_MASK	~KERNFS_TYPE_MASK
39 40

enum kernfs_node_flag {
41
	KERNFS_ACTIVATED	= 0x0010,
T
Tejun Heo 已提交
42 43 44 45
	KERNFS_NS		= 0x0020,
	KERNFS_HAS_SEQ_SHOW	= 0x0040,
	KERNFS_HAS_MMAP		= 0x0080,
	KERNFS_LOCKDEP		= 0x0100,
46
	KERNFS_STATIC_NAME	= 0x0200,
47 48
	KERNFS_SUICIDAL		= 0x0400,
	KERNFS_SUICIDED		= 0x0800,
49 50
};

51 52 53 54 55
/* @flags for kernfs_create_root() */
enum kernfs_root_flag {
	KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
};

56 57
/* type-specific structures for kernfs_node union members */
struct kernfs_elem_dir {
58
	unsigned long		subdirs;
59
	/* children rbtree starts here and goes through kn->rb */
60 61 62 63
	struct rb_root		children;

	/*
	 * The kernfs hierarchy this directory belongs to.  This fits
64
	 * better directly in kernfs_node but is here to save space.
65 66 67 68
	 */
	struct kernfs_root	*root;
};

69 70
struct kernfs_elem_symlink {
	struct kernfs_node	*target_kn;
71 72
};

73
struct kernfs_elem_attr {
74
	const struct kernfs_ops	*ops;
75
	struct kernfs_open_node	*open;
76 77 78 79
	loff_t			size;
};

/*
80 81
 * kernfs_node - the building block of kernfs hierarchy.  Each and every
 * kernfs node is represented by single kernfs_node.  Most fields are
82 83
 * private to kernfs and shouldn't be accessed directly by kernfs users.
 *
84 85 86
 * As long as s_count reference is held, the kernfs_node itself is
 * accessible.  Dereferencing elem or any other outer entity requires
 * active reference.
87
 */
88
struct kernfs_node {
89 90
	atomic_t		count;
	atomic_t		active;
91 92 93
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lockdep_map	dep_map;
#endif
94 95 96 97 98 99
	/*
	 * Use kernfs_get_parent() and kernfs_name/path() instead of
	 * accessing the following two fields directly.  If the node is
	 * never moved to a different parent, it is safe to access the
	 * parent directly.
	 */
100 101
	struct kernfs_node	*parent;
	const char		*name;
102

103
	struct rb_node		rb;
104

105
	const void		*ns;	/* namespace tag */
106
	unsigned int		hash;	/* ns + name hash */
107
	union {
108 109 110
		struct kernfs_elem_dir		dir;
		struct kernfs_elem_symlink	symlink;
		struct kernfs_elem_attr		attr;
111 112 113 114
	};

	void			*priv;

115 116 117
	unsigned short		flags;
	umode_t			mode;
	unsigned int		ino;
118
	struct kernfs_iattrs	*iattr;
119
};
120

T
Tejun Heo 已提交
121
/*
122 123 124 125 126
 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
 * syscalls.  These optional callbacks are invoked on the matching syscalls
 * and can perform any kernfs operations which don't necessarily have to be
 * the exact operation requested.  An active reference is held for each
 * kernfs_node parameter.
T
Tejun Heo 已提交
127
 */
128
struct kernfs_syscall_ops {
129 130 131
	int (*remount_fs)(struct kernfs_root *root, int *flags, char *data);
	int (*show_options)(struct seq_file *sf, struct kernfs_root *root);

T
Tejun Heo 已提交
132 133 134 135 136 137 138
	int (*mkdir)(struct kernfs_node *parent, const char *name,
		     umode_t mode);
	int (*rmdir)(struct kernfs_node *kn);
	int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
		      const char *new_name);
};

139 140
struct kernfs_root {
	/* published fields */
141
	struct kernfs_node	*kn;
142
	unsigned int		flags;	/* KERNFS_ROOT_* flags */
143 144 145

	/* private fields, do not use outside kernfs proper */
	struct ida		ino_ida;
146
	struct kernfs_syscall_ops *syscall_ops;
147
	wait_queue_head_t	deactivate_waitq;
148 149
};

150
struct kernfs_open_file {
151
	/* published fields */
152
	struct kernfs_node	*kn;
153
	struct file		*file;
T
Tejun Heo 已提交
154
	void			*priv;
155 156 157 158 159 160 161 162 163 164

	/* private fields, do not use outside kernfs proper */
	struct mutex		mutex;
	int			event;
	struct list_head	list;

	bool			mmapped;
	const struct vm_operations_struct *vm_ops;
};

T
Tejun Heo 已提交
165 166 167 168
struct kernfs_ops {
	/*
	 * Read is handled by either seq_file or raw_read().
	 *
169 170 171
	 * If seq_show() is present, seq_file path is active.  Other seq
	 * operations are optional and if not implemented, the behavior is
	 * equivalent to single_open().  @sf->private points to the
172
	 * associated kernfs_open_file.
T
Tejun Heo 已提交
173 174 175 176 177
	 *
	 * read() is bounced through kernel buffer and a read larger than
	 * PAGE_SIZE results in partial operation of PAGE_SIZE.
	 */
	int (*seq_show)(struct seq_file *sf, void *v);
178 179 180 181

	void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
	void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
	void (*seq_stop)(struct seq_file *sf, void *v);
T
Tejun Heo 已提交
182

183
	ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
184 185 186
			loff_t off);

	/*
187 188 189 190 191
	 * write() is bounced through kernel buffer.  If atomic_write_len
	 * is not set, a write larger than PAGE_SIZE results in partial
	 * operations of PAGE_SIZE chunks.  If atomic_write_len is set,
	 * writes upto the specified size are executed atomically but
	 * larger ones are rejected with -E2BIG.
T
Tejun Heo 已提交
192
	 */
193
	size_t atomic_write_len;
194
	ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
195 196
			 loff_t off);

197
	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
198 199 200 201

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	lockdep_key;
#endif
T
Tejun Heo 已提交
202 203
};

204 205
#ifdef CONFIG_SYSFS

T
Tejun Heo 已提交
206
static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
207
{
T
Tejun Heo 已提交
208
	return kn->flags & KERNFS_TYPE_MASK;
209 210 211 212
}

/**
 * kernfs_enable_ns - enable namespace under a directory
213
 * @kn: directory of interest, should be empty
214
 *
215 216
 * This is to be called right after @kn is created to enable namespace
 * under it.  All children of @kn must have non-NULL namespace tags and
217 218
 * only the ones which match the super_block's tag will be visible.
 */
219
static inline void kernfs_enable_ns(struct kernfs_node *kn)
220
{
T
Tejun Heo 已提交
221
	WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
222
	WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
T
Tejun Heo 已提交
223
	kn->flags |= KERNFS_NS;
224 225
}

226 227
/**
 * kernfs_ns_enabled - test whether namespace is enabled
228
 * @kn: the node to test
229 230 231
 *
 * Test whether namespace filtering is enabled for the children of @ns.
 */
232
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
233
{
T
Tejun Heo 已提交
234
	return kn->flags & KERNFS_NS;
235 236
}

237 238 239 240 241 242
int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
char * __must_check kernfs_path(struct kernfs_node *kn, char *buf,
				size_t buflen);
void pr_cont_kernfs_name(struct kernfs_node *kn);
void pr_cont_kernfs_path(struct kernfs_node *kn);
struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
243 244 245 246
struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
					   const char *name, const void *ns);
void kernfs_get(struct kernfs_node *kn);
void kernfs_put(struct kernfs_node *kn);
247

248 249 250
struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);

251
struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
252
				       unsigned int flags, void *priv);
253 254
void kernfs_destroy_root(struct kernfs_root *root);

255
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
256 257
					 const char *name, umode_t mode,
					 void *priv, const void *ns);
258 259 260 261 262 263 264
struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
					 const char *name,
					 umode_t mode, loff_t size,
					 const struct kernfs_ops *ops,
					 void *priv, const void *ns,
					 bool name_is_static,
					 struct lock_class_key *key);
265 266 267
struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
				       const char *name,
				       struct kernfs_node *target);
268
void kernfs_activate(struct kernfs_node *kn);
269
void kernfs_remove(struct kernfs_node *kn);
270 271 272
void kernfs_break_active_protection(struct kernfs_node *kn);
void kernfs_unbreak_active_protection(struct kernfs_node *kn);
bool kernfs_remove_self(struct kernfs_node *kn);
273
int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
274
			     const void *ns);
275
int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
276
		     const char *new_name, const void *new_ns);
277 278
int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
void kernfs_notify(struct kernfs_node *kn);
279

280 281 282 283 284 285 286
const void *kernfs_super_ns(struct super_block *sb);
struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
			       struct kernfs_root *root, const void *ns);
void kernfs_kill_sb(struct super_block *sb);

void kernfs_init(void);

287 288
#else	/* CONFIG_SYSFS */

T
Tejun Heo 已提交
289
static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
290 291
{ return 0; }	/* whatever */

292
static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
293

294
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
295 296
{ return false; }

297 298 299 300 301 302 303 304 305 306 307 308 309
static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
{ return -ENOSYS; }

static inline char * __must_check kernfs_path(struct kernfs_node *kn, char *buf,
					      size_t buflen)
{ return NULL; }

static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }

static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
{ return NULL; }

310 311
static inline struct kernfs_node *
kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
312 313 314
		       const void *ns)
{ return NULL; }

315 316
static inline void kernfs_get(struct kernfs_node *kn) { }
static inline void kernfs_put(struct kernfs_node *kn) { }
317

318 319 320 321 322 323
static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
{ return NULL; }

static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
{ return NULL; }

T
Tejun Heo 已提交
324
static inline struct kernfs_root *
325 326
kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
		   void *priv)
327 328 329 330
{ return ERR_PTR(-ENOSYS); }

static inline void kernfs_destroy_root(struct kernfs_root *root) { }

331
static inline struct kernfs_node *
332 333
kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
		     umode_t mode, void *priv, const void *ns)
334 335
{ return ERR_PTR(-ENOSYS); }

336
static inline struct kernfs_node *
337 338 339 340
__kernfs_create_file(struct kernfs_node *parent, const char *name,
		     umode_t mode, loff_t size, const struct kernfs_ops *ops,
		     void *priv, const void *ns, bool name_is_static,
		     struct lock_class_key *key)
341 342
{ return ERR_PTR(-ENOSYS); }

343 344 345
static inline struct kernfs_node *
kernfs_create_link(struct kernfs_node *parent, const char *name,
		   struct kernfs_node *target)
346 347
{ return ERR_PTR(-ENOSYS); }

348 349
static inline void kernfs_activate(struct kernfs_node *kn) { }

350
static inline void kernfs_remove(struct kernfs_node *kn) { }
351

352 353 354
static inline bool kernfs_remove_self(struct kernfs_node *kn)
{ return false; }

355
static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
356 357 358
					   const char *name, const void *ns)
{ return -ENOSYS; }

359 360
static inline int kernfs_rename_ns(struct kernfs_node *kn,
				   struct kernfs_node *new_parent,
361 362 363
				   const char *new_name, const void *new_ns)
{ return -ENOSYS; }

364
static inline int kernfs_setattr(struct kernfs_node *kn,
365 366 367
				 const struct iattr *iattr)
{ return -ENOSYS; }

368
static inline void kernfs_notify(struct kernfs_node *kn) { }
369

370 371 372 373 374 375 376 377 378 379 380 381
static inline const void *kernfs_super_ns(struct super_block *sb)
{ return NULL; }

static inline struct dentry *
kernfs_mount_ns(struct file_system_type *fs_type, int flags,
		struct kernfs_root *root, const void *ns)
{ return ERR_PTR(-ENOSYS); }

static inline void kernfs_kill_sb(struct super_block *sb) { }

static inline void kernfs_init(void) { }

382 383
#endif	/* CONFIG_SYSFS */

384 385
static inline struct kernfs_node *
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
386
{
387
	return kernfs_find_and_get_ns(kn, name, NULL);
388 389
}

390
static inline struct kernfs_node *
391 392
kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
		  void *priv)
393
{
394
	return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
395 396
}

397 398
static inline struct kernfs_node *
kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
399 400 401 402 403 404 405 406
		      umode_t mode, loff_t size, const struct kernfs_ops *ops,
		      void *priv, const void *ns)
{
	struct lock_class_key *key = NULL;

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	key = (struct lock_class_key *)&ops->lockdep_key;
#endif
407 408
	return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
				    false, key);
409 410
}

411 412
static inline struct kernfs_node *
kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
413 414 415 416 417
		   loff_t size, const struct kernfs_ops *ops, void *priv)
{
	return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
}

418
static inline int kernfs_remove_by_name(struct kernfs_node *parent,
419 420 421 422 423
					const char *name)
{
	return kernfs_remove_by_name_ns(parent, name, NULL);
}

424 425 426 427 428 429 430
static inline int kernfs_rename(struct kernfs_node *kn,
				struct kernfs_node *new_parent,
				const char *new_name)
{
	return kernfs_rename_ns(kn, new_parent, new_name, NULL);
}

431 432 433 434 435 436 437
static inline struct dentry *
kernfs_mount(struct file_system_type *fs_type, int flags,
	     struct kernfs_root *root)
{
	return kernfs_mount_ns(fs_type, flags, root, NULL);
}

438
#endif	/* __LINUX_KERNFS_H */