kernfs.h 10.5 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/completion.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
#define KERNFS_TYPE_MASK	0x000f
38
#define KERNFS_ACTIVE_REF	KERNFS_FILE
T
Tejun Heo 已提交
39
#define KERNFS_FLAG_MASK	~KERNFS_TYPE_MASK
40 41

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

50 51
/* type-specific structures for kernfs_node union members */
struct kernfs_elem_dir {
52
	unsigned long		subdirs;
53
	/* children rbtree starts here and goes through kn->rb */
54 55 56 57
	struct rb_root		children;

	/*
	 * The kernfs hierarchy this directory belongs to.  This fits
58
	 * better directly in kernfs_node but is here to save space.
59 60 61 62
	 */
	struct kernfs_root	*root;
};

63 64
struct kernfs_elem_symlink {
	struct kernfs_node	*target_kn;
65 66
};

67
struct kernfs_elem_attr {
68
	const struct kernfs_ops	*ops;
69
	struct kernfs_open_node	*open;
70 71 72 73
	loff_t			size;
};

/*
74 75
 * kernfs_node - the building block of kernfs hierarchy.  Each and every
 * kernfs node is represented by single kernfs_node.  Most fields are
76 77
 * private to kernfs and shouldn't be accessed directly by kernfs users.
 *
78 79 80
 * As long as s_count reference is held, the kernfs_node itself is
 * accessible.  Dereferencing elem or any other outer entity requires
 * active reference.
81
 */
82
struct kernfs_node {
83 84
	atomic_t		count;
	atomic_t		active;
85 86 87 88
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lockdep_map	dep_map;
#endif
	/* the following two fields are published */
89 90
	struct kernfs_node	*parent;
	const char		*name;
91

92
	struct rb_node		rb;
93

94
	union {
95
		struct completion	*completion;
96 97 98
		struct kernfs_node	*removed_list;
	} u;

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

	void			*priv;

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

T
Tejun Heo 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128
/*
 * kernfs_dir_ops may be specified on kernfs_create_root() to support
 * directory manipulation 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.
 */
struct kernfs_dir_ops {
	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);
};

129 130
struct kernfs_root {
	/* published fields */
131
	struct kernfs_node	*kn;
132 133 134

	/* private fields, do not use outside kernfs proper */
	struct ida		ino_ida;
T
Tejun Heo 已提交
135
	struct kernfs_dir_ops	*dir_ops;
136 137
};

138
struct kernfs_open_file {
139
	/* published fields */
140
	struct kernfs_node	*kn;
141 142 143 144 145 146 147 148 149 150 151
	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 已提交
152 153 154 155
struct kernfs_ops {
	/*
	 * Read is handled by either seq_file or raw_read().
	 *
156 157 158
	 * 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
159
	 * associated kernfs_open_file.
T
Tejun Heo 已提交
160 161 162 163 164
	 *
	 * 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);
165 166 167 168

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

170
	ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
171 172 173 174 175 176
			loff_t off);

	/*
	 * write() is bounced through kernel buffer and a write larger than
	 * PAGE_SIZE results in partial operation of PAGE_SIZE.
	 */
177
	ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
178 179
			 loff_t off);

180
	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
181 182 183 184

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	lockdep_key;
#endif
T
Tejun Heo 已提交
185 186
};

187 188
#ifdef CONFIG_SYSFS

T
Tejun Heo 已提交
189
static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
190
{
T
Tejun Heo 已提交
191
	return kn->flags & KERNFS_TYPE_MASK;
192 193 194 195
}

/**
 * kernfs_enable_ns - enable namespace under a directory
196
 * @kn: directory of interest, should be empty
197
 *
198 199
 * 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
200 201
 * only the ones which match the super_block's tag will be visible.
 */
202
static inline void kernfs_enable_ns(struct kernfs_node *kn)
203
{
T
Tejun Heo 已提交
204
	WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
205
	WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
T
Tejun Heo 已提交
206
	kn->flags |= KERNFS_NS;
207 208
}

209 210
/**
 * kernfs_ns_enabled - test whether namespace is enabled
211
 * @kn: the node to test
212 213 214
 *
 * Test whether namespace filtering is enabled for the children of @ns.
 */
215
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
216
{
T
Tejun Heo 已提交
217
	return kn->flags & KERNFS_NS;
218 219
}

220 221 222 223
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);
224

T
Tejun Heo 已提交
225 226
struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
				       void *priv);
227 228
void kernfs_destroy_root(struct kernfs_root *root);

229
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
230 231
					 const char *name, umode_t mode,
					 void *priv, const void *ns);
232 233 234 235 236 237 238
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);
239 240 241 242 243
struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
				       const char *name,
				       struct kernfs_node *target);
void kernfs_remove(struct kernfs_node *kn);
int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
244
			     const void *ns);
245
int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
246
		     const char *new_name, const void *new_ns);
247 248
int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
void kernfs_notify(struct kernfs_node *kn);
249

250 251 252 253 254 255 256
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);

257 258
#else	/* CONFIG_SYSFS */

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

262
static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
263

264
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
265 266
{ return false; }

267 268
static inline struct kernfs_node *
kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
269 270 271
		       const void *ns)
{ return NULL; }

272 273
static inline void kernfs_get(struct kernfs_node *kn) { }
static inline void kernfs_put(struct kernfs_node *kn) { }
274

T
Tejun Heo 已提交
275 276
static inline struct kernfs_root *
kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
277 278 279 280
{ return ERR_PTR(-ENOSYS); }

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

281
static inline struct kernfs_node *
282 283
kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
		     umode_t mode, void *priv, const void *ns)
284 285
{ return ERR_PTR(-ENOSYS); }

286
static inline struct kernfs_node *
287 288 289 290
__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)
291 292
{ return ERR_PTR(-ENOSYS); }

293 294 295
static inline struct kernfs_node *
kernfs_create_link(struct kernfs_node *parent, const char *name,
		   struct kernfs_node *target)
296 297
{ return ERR_PTR(-ENOSYS); }

298
static inline void kernfs_remove(struct kernfs_node *kn) { }
299

300
static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
301 302 303
					   const char *name, const void *ns)
{ return -ENOSYS; }

304 305
static inline int kernfs_rename_ns(struct kernfs_node *kn,
				   struct kernfs_node *new_parent,
306 307 308
				   const char *new_name, const void *new_ns)
{ return -ENOSYS; }

309
static inline int kernfs_setattr(struct kernfs_node *kn,
310 311 312
				 const struct iattr *iattr)
{ return -ENOSYS; }

313
static inline void kernfs_notify(struct kernfs_node *kn) { }
314

315 316 317 318 319 320 321 322 323 324 325 326
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) { }

327 328
#endif	/* CONFIG_SYSFS */

329 330
static inline struct kernfs_node *
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
331
{
332
	return kernfs_find_and_get_ns(kn, name, NULL);
333 334
}

335
static inline struct kernfs_node *
336 337
kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
		  void *priv)
338
{
339
	return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
340 341
}

342 343
static inline struct kernfs_node *
kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
344 345 346 347 348 349 350 351
		      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
352 353
	return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
				    false, key);
354 355
}

356 357
static inline struct kernfs_node *
kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
358 359 360 361 362
		   loff_t size, const struct kernfs_ops *ops, void *priv)
{
	return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
}

363
static inline int kernfs_remove_by_name(struct kernfs_node *parent,
364 365 366 367 368
					const char *name)
{
	return kernfs_remove_by_name_ns(parent, name, NULL);
}

369 370 371 372 373 374 375
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);
}

376
#endif	/* __LINUX_KERNFS_H */