kernfs.h 10.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 21
struct file;
struct iattr;
22 23
struct seq_file;
struct vm_area_struct;
24 25
struct super_block;
struct file_system_type;
26

27 28
struct kernfs_open_node;
struct kernfs_iattrs;
29 30

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

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

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

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

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

61 62
struct kernfs_elem_symlink {
	struct kernfs_node	*target_kn;
63 64
};

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

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

90
	struct rb_node		rb;
91

92 93 94 95
	union {
		struct kernfs_node	*removed_list;
	} u;

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

	void			*priv;

106 107 108
	unsigned short		flags;
	umode_t			mode;
	unsigned int		ino;
109
	struct kernfs_iattrs	*iattr;
110
};
111

T
Tejun Heo 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
/*
 * 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);
};

126 127
struct kernfs_root {
	/* published fields */
128
	struct kernfs_node	*kn;
129 130 131

	/* private fields, do not use outside kernfs proper */
	struct ida		ino_ida;
T
Tejun Heo 已提交
132
	struct kernfs_dir_ops	*dir_ops;
133
	wait_queue_head_t	deactivate_waitq;
134 135
};

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

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

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

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

178
	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
179 180 181 182

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	lockdep_key;
#endif
T
Tejun Heo 已提交
183 184
};

185 186
#ifdef CONFIG_SYSFS

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

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

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

218 219 220 221
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);
222

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

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

248 249 250 251 252 253 254
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);

255 256
#else	/* CONFIG_SYSFS */

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

260
static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
261

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

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

270 271
static inline void kernfs_get(struct kernfs_node *kn) { }
static inline void kernfs_put(struct kernfs_node *kn) { }
272

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

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

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

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

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

296
static inline void kernfs_remove(struct kernfs_node *kn) { }
297

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

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

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

311
static inline void kernfs_notify(struct kernfs_node *kn) { }
312

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

325 326
#endif	/* CONFIG_SYSFS */

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

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

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

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

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

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

374
#endif	/* __LINUX_KERNFS_H */