kernfs.h 11.4 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 94
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lockdep_map	dep_map;
#endif
	/* the following two fields are published */
95 96
	struct kernfs_node	*parent;
	const char		*name;
97

98
	struct rb_node		rb;
99

100
	const void		*ns;	/* namespace tag */
101
	unsigned int		hash;	/* ns + name hash */
102
	union {
103 104 105
		struct kernfs_elem_dir		dir;
		struct kernfs_elem_symlink	symlink;
		struct kernfs_elem_attr		attr;
106 107 108 109
	};

	void			*priv;

110 111 112
	unsigned short		flags;
	umode_t			mode;
	unsigned int		ino;
113
	struct kernfs_iattrs	*iattr;
114
};
115

T
Tejun Heo 已提交
116
/*
117 118 119 120 121
 * 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 已提交
122
 */
123
struct kernfs_syscall_ops {
124 125 126
	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 已提交
127 128 129 130 131 132 133
	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);
};

134 135
struct kernfs_root {
	/* published fields */
136
	struct kernfs_node	*kn;
137
	unsigned int		flags;	/* KERNFS_ROOT_* flags */
138 139 140

	/* private fields, do not use outside kernfs proper */
	struct ida		ino_ida;
141
	struct kernfs_syscall_ops *syscall_ops;
142
	wait_queue_head_t	deactivate_waitq;
143 144
};

145
struct kernfs_open_file {
146
	/* published fields */
147
	struct kernfs_node	*kn;
148 149 150 151 152 153 154 155 156 157 158
	struct file		*file;

	/* 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 已提交
159 160 161 162
struct kernfs_ops {
	/*
	 * Read is handled by either seq_file or raw_read().
	 *
163 164 165
	 * 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
166
	 * associated kernfs_open_file.
T
Tejun Heo 已提交
167 168 169 170 171
	 *
	 * 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);
172 173 174 175

	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 已提交
176

177
	ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
178 179 180
			loff_t off);

	/*
181 182 183 184 185
	 * 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 已提交
186
	 */
187
	size_t atomic_write_len;
188
	ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
189 190
			 loff_t off);

191
	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
192 193 194 195

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	lockdep_key;
#endif
T
Tejun Heo 已提交
196 197
};

198 199
#ifdef CONFIG_SYSFS

T
Tejun Heo 已提交
200
static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
201
{
T
Tejun Heo 已提交
202
	return kn->flags & KERNFS_TYPE_MASK;
203 204 205 206
}

/**
 * kernfs_enable_ns - enable namespace under a directory
207
 * @kn: directory of interest, should be empty
208
 *
209 210
 * 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
211 212
 * only the ones which match the super_block's tag will be visible.
 */
213
static inline void kernfs_enable_ns(struct kernfs_node *kn)
214
{
T
Tejun Heo 已提交
215
	WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
216
	WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
T
Tejun Heo 已提交
217
	kn->flags |= KERNFS_NS;
218 219
}

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

231 232 233 234
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);
235

236
struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
237
				       unsigned int flags, void *priv);
238 239
void kernfs_destroy_root(struct kernfs_root *root);

240
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
241 242
					 const char *name, umode_t mode,
					 void *priv, const void *ns);
243 244 245 246 247 248 249
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);
250 251 252
struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
				       const char *name,
				       struct kernfs_node *target);
253
void kernfs_activate(struct kernfs_node *kn);
254
void kernfs_remove(struct kernfs_node *kn);
255 256 257
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);
258
int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
259
			     const void *ns);
260
int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
261
		     const char *new_name, const void *new_ns);
262 263
int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
void kernfs_notify(struct kernfs_node *kn);
264

265 266 267 268 269 270 271
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);

272 273
#else	/* CONFIG_SYSFS */

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

277
static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
278

279
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
280 281
{ return false; }

282 283
static inline struct kernfs_node *
kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
284 285 286
		       const void *ns)
{ return NULL; }

287 288
static inline void kernfs_get(struct kernfs_node *kn) { }
static inline void kernfs_put(struct kernfs_node *kn) { }
289

T
Tejun Heo 已提交
290
static inline struct kernfs_root *
291 292
kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
		   void *priv)
293 294 295 296
{ return ERR_PTR(-ENOSYS); }

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

297
static inline struct kernfs_node *
298 299
kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
		     umode_t mode, void *priv, const void *ns)
300 301
{ return ERR_PTR(-ENOSYS); }

302
static inline struct kernfs_node *
303 304 305 306
__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)
307 308
{ return ERR_PTR(-ENOSYS); }

309 310 311
static inline struct kernfs_node *
kernfs_create_link(struct kernfs_node *parent, const char *name,
		   struct kernfs_node *target)
312 313
{ return ERR_PTR(-ENOSYS); }

314 315
static inline void kernfs_activate(struct kernfs_node *kn) { }

316
static inline void kernfs_remove(struct kernfs_node *kn) { }
317

318 319 320
static inline bool kernfs_remove_self(struct kernfs_node *kn)
{ return false; }

321
static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
322 323 324
					   const char *name, const void *ns)
{ return -ENOSYS; }

325 326
static inline int kernfs_rename_ns(struct kernfs_node *kn,
				   struct kernfs_node *new_parent,
327 328 329
				   const char *new_name, const void *new_ns)
{ return -ENOSYS; }

330
static inline int kernfs_setattr(struct kernfs_node *kn,
331 332 333
				 const struct iattr *iattr)
{ return -ENOSYS; }

334
static inline void kernfs_notify(struct kernfs_node *kn) { }
335

336 337 338 339 340 341 342 343 344 345 346 347
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) { }

348 349
#endif	/* CONFIG_SYSFS */

350 351
static inline struct kernfs_node *
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
352
{
353
	return kernfs_find_and_get_ns(kn, name, NULL);
354 355
}

356
static inline struct kernfs_node *
357 358
kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
		  void *priv)
359
{
360
	return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
361 362
}

363 364
static inline struct kernfs_node *
kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
365 366 367 368 369 370 371 372
		      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
373 374
	return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
				    false, key);
375 376
}

377 378
static inline struct kernfs_node *
kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
379 380 381 382 383
		   loff_t size, const struct kernfs_ops *ops, void *priv)
{
	return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
}

384
static inline int kernfs_remove_by_name(struct kernfs_node *parent,
385 386 387 388 389
					const char *name)
{
	return kernfs_remove_by_name_ns(parent, name, NULL);
}

390 391 392 393 394 395 396
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);
}

397
#endif	/* __LINUX_KERNFS_H */