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 {
T
Tejun Heo 已提交
40 41 42 43
	KERNFS_NS		= 0x0020,
	KERNFS_HAS_SEQ_SHOW	= 0x0040,
	KERNFS_HAS_MMAP		= 0x0080,
	KERNFS_LOCKDEP		= 0x0100,
44
	KERNFS_STATIC_NAME	= 0x0200,
45 46
};

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

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

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

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

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

89
	struct rb_node		rb;
90 91

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

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

	void			*priv;

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

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

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

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

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

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

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

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

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

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

184 185
#ifdef CONFIG_SYSFS

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

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

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

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

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

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

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

254 255
#else	/* CONFIG_SYSFS */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

324 325
#endif	/* CONFIG_SYSFS */

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

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

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

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

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

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

373
#endif	/* __LINUX_KERNFS_H */