kernfs.h 9.8 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 18
#include <linux/rbtree.h>
#include <linux/atomic.h>
#include <linux/completion.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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

enum kernfs_node_type {
	SYSFS_DIR		= 0x0001,
	SYSFS_KOBJ_ATTR		= 0x0002,
	SYSFS_KOBJ_LINK		= 0x0004,
};

#define SYSFS_TYPE_MASK		0x000f
#define SYSFS_COPY_NAME		(SYSFS_DIR | SYSFS_KOBJ_LINK)
#define SYSFS_ACTIVE_REF	SYSFS_KOBJ_ATTR
#define SYSFS_FLAG_MASK		~SYSFS_TYPE_MASK

enum kernfs_node_flag {
	SYSFS_FLAG_REMOVED	= 0x0010,
	SYSFS_FLAG_NS		= 0x0020,
	SYSFS_FLAG_HAS_SEQ_SHOW	= 0x0040,
	SYSFS_FLAG_HAS_MMAP	= 0x0080,
	SYSFS_FLAG_LOCKDEP	= 0x0100,
};

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

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

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

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

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

91
	struct rb_node		rb;
92 93 94

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

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

	void			*priv;

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

114 115
struct kernfs_root {
	/* published fields */
116
	struct kernfs_node	*kn;
117 118 119

	/* private fields, do not use outside kernfs proper */
	struct ida		ino_ida;
120 121
};

122
struct kernfs_open_file {
123
	/* published fields */
124
	struct kernfs_node	*kn;
125 126 127 128 129 130 131 132 133 134 135
	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 已提交
136 137 138 139
struct kernfs_ops {
	/*
	 * Read is handled by either seq_file or raw_read().
	 *
140 141 142
	 * 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
143
	 * associated kernfs_open_file.
T
Tejun Heo 已提交
144 145 146 147 148
	 *
	 * 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);
149 150 151 152

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

154
	ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
T
Tejun Heo 已提交
155 156 157 158 159 160
			loff_t off);

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

164
	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
165 166 167 168

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	lockdep_key;
#endif
T
Tejun Heo 已提交
169 170
};

171 172
#ifdef CONFIG_SYSFS

173
static inline enum kernfs_node_type sysfs_type(struct kernfs_node *kn)
174
{
175
	return kn->flags & SYSFS_TYPE_MASK;
176 177 178 179
}

/**
 * kernfs_enable_ns - enable namespace under a directory
180
 * @kn: directory of interest, should be empty
181
 *
182 183
 * 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
184 185
 * only the ones which match the super_block's tag will be visible.
 */
186
static inline void kernfs_enable_ns(struct kernfs_node *kn)
187
{
188
	WARN_ON_ONCE(sysfs_type(kn) != SYSFS_DIR);
189 190
	WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
	kn->flags |= SYSFS_FLAG_NS;
191 192
}

193 194
/**
 * kernfs_ns_enabled - test whether namespace is enabled
195
 * @kn: the node to test
196 197 198
 *
 * Test whether namespace filtering is enabled for the children of @ns.
 */
199
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
200
{
201
	return kn->flags & SYSFS_FLAG_NS;
202 203
}

204 205 206 207
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);
208

209 210 211
struct kernfs_root *kernfs_create_root(void *priv);
void kernfs_destroy_root(struct kernfs_root *root);

212 213 214 215 216 217 218 219 220 221 222 223 224 225
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
					 const char *name, void *priv,
					 const void *ns);
struct kernfs_node *kernfs_create_file_ns_key(struct kernfs_node *parent,
					      const char *name,
					      umode_t mode, loff_t size,
					      const struct kernfs_ops *ops,
					      void *priv, const void *ns,
					      struct lock_class_key *key);
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,
226
			     const void *ns);
227
int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
228
		     const char *new_name, const void *new_ns);
229 230
int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
void kernfs_notify(struct kernfs_node *kn);
231

232 233 234 235 236 237 238
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);

239 240
#else	/* CONFIG_SYSFS */

241
static inline enum kernfs_node_type sysfs_type(struct kernfs_node *kn)
242 243
{ return 0; }	/* whatever */

244
static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
245

246
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
247 248
{ return false; }

249 250
static inline struct kernfs_node *
kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
251 252 253
		       const void *ns)
{ return NULL; }

254 255
static inline void kernfs_get(struct kernfs_node *kn) { }
static inline void kernfs_put(struct kernfs_node *kn) { }
256

257 258 259 260 261
static inline struct kernfs_root *kernfs_create_root(void *priv)
{ return ERR_PTR(-ENOSYS); }

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

262 263
static inline struct kernfs_node *
kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, void *priv,
264 265 266
		     const void *ns)
{ return ERR_PTR(-ENOSYS); }

267 268
static inline struct kernfs_node *
kernfs_create_file_ns_key(struct kernfs_node *parent, const char *name,
269 270 271
			  umode_t mode, loff_t size,
			  const struct kernfs_ops *ops, void *priv,
			  const void *ns, struct lock_class_key *key)
272 273
{ return ERR_PTR(-ENOSYS); }

274 275 276
static inline struct kernfs_node *
kernfs_create_link(struct kernfs_node *parent, const char *name,
		   struct kernfs_node *target)
277 278
{ return ERR_PTR(-ENOSYS); }

279
static inline void kernfs_remove(struct kernfs_node *kn) { }
280

281
static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
282 283 284
					   const char *name, const void *ns)
{ return -ENOSYS; }

285 286
static inline int kernfs_rename_ns(struct kernfs_node *kn,
				   struct kernfs_node *new_parent,
287 288 289
				   const char *new_name, const void *new_ns)
{ return -ENOSYS; }

290
static inline int kernfs_setattr(struct kernfs_node *kn,
291 292 293
				 const struct iattr *iattr)
{ return -ENOSYS; }

294
static inline void kernfs_notify(struct kernfs_node *kn) { }
295

296 297 298 299 300 301 302 303 304 305 306 307
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) { }

308 309
#endif	/* CONFIG_SYSFS */

310 311
static inline struct kernfs_node *
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
312
{
313
	return kernfs_find_and_get_ns(kn, name, NULL);
314 315
}

316 317
static inline struct kernfs_node *
kernfs_create_dir(struct kernfs_node *parent, const char *name, void *priv)
318 319 320 321
{
	return kernfs_create_dir_ns(parent, name, priv, NULL);
}

322 323
static inline struct kernfs_node *
kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
324 325 326 327 328 329 330 331 332 333 334 335
		      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
	return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv,
					 ns, key);
}

336 337
static inline struct kernfs_node *
kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
338 339 340 341 342
		   loff_t size, const struct kernfs_ops *ops, void *priv)
{
	return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
}

343
static inline int kernfs_remove_by_name(struct kernfs_node *parent,
344 345 346 347 348
					const char *name)
{
	return kernfs_remove_by_name_ns(parent, name, NULL);
}

349 350 351 352 353 354 355
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);
}

356
#endif	/* __LINUX_KERNFS_H */