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_JUST_DEACTIVATED	= 0x0010, /* used to aid lockdep annotation */
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
	const void		*ns;	/* namespace tag */
93
	unsigned int		hash;	/* ns + name hash */
94
	union {
95 96 97
		struct kernfs_elem_dir		dir;
		struct kernfs_elem_symlink	symlink;
		struct kernfs_elem_attr		attr;
98 99 100 101
	};

	void			*priv;

102 103 104
	unsigned short		flags;
	umode_t			mode;
	unsigned int		ino;
105
	struct kernfs_iattrs	*iattr;
106
};
107

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

122 123
struct kernfs_root {
	/* published fields */
124
	struct kernfs_node	*kn;
125 126 127

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

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

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

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

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

174
	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
175 176 177 178

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	lockdep_key;
#endif
T
Tejun Heo 已提交
179 180
};

181 182
#ifdef CONFIG_SYSFS

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

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

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

214 215 216 217
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);
218

T
Tejun Heo 已提交
219 220
struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
				       void *priv);
221 222
void kernfs_destroy_root(struct kernfs_root *root);

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

244 245 246 247 248 249 250
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);

251 252
#else	/* CONFIG_SYSFS */

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

256
static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
257

258
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
259 260
{ return false; }

261 262
static inline struct kernfs_node *
kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
263 264 265
		       const void *ns)
{ return NULL; }

266 267
static inline void kernfs_get(struct kernfs_node *kn) { }
static inline void kernfs_put(struct kernfs_node *kn) { }
268

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

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

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

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

287 288 289
static inline struct kernfs_node *
kernfs_create_link(struct kernfs_node *parent, const char *name,
		   struct kernfs_node *target)
290 291
{ return ERR_PTR(-ENOSYS); }

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

294
static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
295 296 297
					   const char *name, const void *ns)
{ return -ENOSYS; }

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

303
static inline int kernfs_setattr(struct kernfs_node *kn,
304 305 306
				 const struct iattr *iattr)
{ return -ENOSYS; }

307
static inline void kernfs_notify(struct kernfs_node *kn) { }
308

309 310 311 312 313 314 315 316 317 318 319 320
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) { }

321 322
#endif	/* CONFIG_SYSFS */

323 324
static inline struct kernfs_node *
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
325
{
326
	return kernfs_find_and_get_ns(kn, name, NULL);
327 328
}

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

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

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

357
static inline int kernfs_remove_by_name(struct kernfs_node *parent,
358 359 360 361 362
					const char *name)
{
	return kernfs_remove_by_name_ns(parent, name, NULL);
}

363 364 365 366 367 368 369
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);
}

370
#endif	/* __LINUX_KERNFS_H */