blob: 75fcbe5c9d657d73f00d1831028ce9299c30abfe [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
2 * kernfs.h - pseudo filesystem decoupled from vfs locking
3 *
4 * This file is released under the GPLv2.
5 */
6
7#ifndef __LINUX_KERNFS_H
8#define __LINUX_KERNFS_H
9
Tejun Heo879f40d2013-11-23 17:21:49 -050010#include <linux/kernel.h>
Tejun Heo5d0e26b2013-11-23 17:21:50 -050011#include <linux/err.h>
Tejun Heodd8a5b02013-11-28 14:54:20 -050012#include <linux/list.h>
13#include <linux/mutex.h>
Tejun Heobc755552013-11-28 14:54:41 -050014#include <linux/idr.h>
Tejun Heo517e64f2013-11-28 14:54:29 -050015#include <linux/lockdep.h>
Tejun Heo879f40d2013-11-23 17:21:49 -050016
Tejun Heo5d604182013-11-23 17:21:52 -050017struct file;
18struct iattr;
Tejun Heodd8a5b02013-11-28 14:54:20 -050019struct seq_file;
20struct vm_area_struct;
Tejun Heo4b93dc92013-11-28 14:54:43 -050021struct super_block;
22struct file_system_type;
Tejun Heo5d604182013-11-23 17:21:52 -050023
Tejun Heob8441ed2013-11-24 09:54:58 -050024struct sysfs_dirent;
25
Tejun Heoba7443b2013-11-28 14:54:40 -050026struct kernfs_root {
27 /* published fields */
28 struct sysfs_dirent *sd;
Tejun Heobc755552013-11-28 14:54:41 -050029
30 /* private fields, do not use outside kernfs proper */
31 struct ida ino_ida;
Tejun Heoba7443b2013-11-28 14:54:40 -050032};
33
Tejun Heodd8a5b02013-11-28 14:54:20 -050034struct sysfs_open_file {
35 /* published fields */
36 struct sysfs_dirent *sd;
37 struct file *file;
38
39 /* private fields, do not use outside kernfs proper */
40 struct mutex mutex;
41 int event;
42 struct list_head list;
43
44 bool mmapped;
45 const struct vm_operations_struct *vm_ops;
46};
47
Tejun Heof6acf8b2013-11-28 14:54:21 -050048struct kernfs_ops {
49 /*
50 * Read is handled by either seq_file or raw_read().
51 *
Tejun Heod19b9842013-11-28 14:54:26 -050052 * If seq_show() is present, seq_file path is active. Other seq
53 * operations are optional and if not implemented, the behavior is
54 * equivalent to single_open(). @sf->private points to the
Tejun Heof6acf8b2013-11-28 14:54:21 -050055 * associated sysfs_open_file.
56 *
57 * read() is bounced through kernel buffer and a read larger than
58 * PAGE_SIZE results in partial operation of PAGE_SIZE.
59 */
60 int (*seq_show)(struct seq_file *sf, void *v);
61
Tejun Heod19b9842013-11-28 14:54:26 -050062 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
63 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
64 void (*seq_stop)(struct seq_file *sf, void *v);
65
Tejun Heof6acf8b2013-11-28 14:54:21 -050066 ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
67 loff_t off);
68
69 /*
70 * write() is bounced through kernel buffer and a write larger than
71 * PAGE_SIZE results in partial operation of PAGE_SIZE.
72 */
73 ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
74 loff_t off);
75
76 int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -050077
78#ifdef CONFIG_DEBUG_LOCK_ALLOC
79 struct lock_class_key lockdep_key;
80#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -050081};
82
Tejun Heo879f40d2013-11-23 17:21:49 -050083#ifdef CONFIG_SYSFS
84
Tejun Heoccf73cf2013-11-28 14:54:30 -050085struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
86 const char *name, const void *ns);
87void kernfs_get(struct sysfs_dirent *sd);
88void kernfs_put(struct sysfs_dirent *sd);
89
Tejun Heoba7443b2013-11-28 14:54:40 -050090struct kernfs_root *kernfs_create_root(void *priv);
91void kernfs_destroy_root(struct kernfs_root *root);
92
Tejun Heo93b2b8e2013-11-28 14:54:15 -050093struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
94 const char *name, void *priv,
95 const void *ns);
Tejun Heo517e64f2013-11-28 14:54:29 -050096struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
97 const char *name,
98 umode_t mode, loff_t size,
99 const struct kernfs_ops *ops,
100 void *priv, const void *ns,
101 struct lock_class_key *key);
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500102struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
103 const char *name,
104 struct sysfs_dirent *target);
Tejun Heo879f40d2013-11-23 17:21:49 -0500105void kernfs_remove(struct sysfs_dirent *sd);
106int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
107 const void *ns);
Tejun Heo890ece12013-11-23 17:21:51 -0500108int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
109 const char *new_name, const void *new_ns);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500110void kernfs_enable_ns(struct sysfs_dirent *sd);
Tejun Heo5d604182013-11-23 17:21:52 -0500111int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
Tejun Heo024f6472013-11-28 14:54:27 -0500112void kernfs_notify(struct sysfs_dirent *sd);
Tejun Heo879f40d2013-11-23 17:21:49 -0500113
Tejun Heo4b93dc92013-11-28 14:54:43 -0500114const void *kernfs_super_ns(struct super_block *sb);
115struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
116 struct kernfs_root *root, const void *ns);
117void kernfs_kill_sb(struct super_block *sb);
118
119void kernfs_init(void);
120
Tejun Heo879f40d2013-11-23 17:21:49 -0500121#else /* CONFIG_SYSFS */
122
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500123static inline struct sysfs_dirent *
Tejun Heoccf73cf2013-11-28 14:54:30 -0500124kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name,
125 const void *ns)
126{ return NULL; }
127
128static inline void kernfs_get(struct sysfs_dirent *sd) { }
129static inline void kernfs_put(struct sysfs_dirent *sd) { }
130
Tejun Heoba7443b2013-11-28 14:54:40 -0500131static inline struct kernfs_root *kernfs_create_root(void *priv)
132{ return ERR_PTR(-ENOSYS); }
133
134static inline void kernfs_destroy_root(struct kernfs_root *root) { }
135
Tejun Heoccf73cf2013-11-28 14:54:30 -0500136static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500137kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
138 const void *ns)
139{ return ERR_PTR(-ENOSYS); }
140
141static inline struct sysfs_dirent *
Tejun Heo517e64f2013-11-28 14:54:29 -0500142kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name,
143 umode_t mode, loff_t size,
144 const struct kernfs_ops *ops, void *priv,
145 const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500146{ return ERR_PTR(-ENOSYS); }
147
148static inline struct sysfs_dirent *
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500149kernfs_create_link(struct sysfs_dirent *parent, const char *name,
150 struct sysfs_dirent *target)
151{ return ERR_PTR(-ENOSYS); }
152
Tejun Heo879f40d2013-11-23 17:21:49 -0500153static inline void kernfs_remove(struct sysfs_dirent *sd) { }
154
155static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
156 const char *name, const void *ns)
157{ return -ENOSYS; }
158
Tejun Heo890ece12013-11-23 17:21:51 -0500159static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
160 struct sysfs_dirent *new_parent,
161 const char *new_name, const void *new_ns)
162{ return -ENOSYS; }
163
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500164static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
165
Tejun Heo5d604182013-11-23 17:21:52 -0500166static inline int kernfs_setattr(struct sysfs_dirent *sd,
167 const struct iattr *iattr)
168{ return -ENOSYS; }
169
Tejun Heo024f6472013-11-28 14:54:27 -0500170static inline void kernfs_notify(struct sysfs_dirent *sd) { }
171
Tejun Heo4b93dc92013-11-28 14:54:43 -0500172static inline const void *kernfs_super_ns(struct super_block *sb)
173{ return NULL; }
174
175static inline struct dentry *
176kernfs_mount_ns(struct file_system_type *fs_type, int flags,
177 struct kernfs_root *root, const void *ns)
178{ return ERR_PTR(-ENOSYS); }
179
180static inline void kernfs_kill_sb(struct super_block *sb) { }
181
182static inline void kernfs_init(void) { }
183
Tejun Heo879f40d2013-11-23 17:21:49 -0500184#endif /* CONFIG_SYSFS */
185
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500186static inline struct sysfs_dirent *
Tejun Heoccf73cf2013-11-28 14:54:30 -0500187kernfs_find_and_get(struct sysfs_dirent *sd, const char *name)
188{
189 return kernfs_find_and_get_ns(sd, name, NULL);
190}
191
192static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500193kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
194{
195 return kernfs_create_dir_ns(parent, name, priv, NULL);
196}
197
Tejun Heo496f7392013-11-28 14:54:24 -0500198static inline struct sysfs_dirent *
Tejun Heo517e64f2013-11-28 14:54:29 -0500199kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
200 umode_t mode, loff_t size, const struct kernfs_ops *ops,
201 void *priv, const void *ns)
202{
203 struct lock_class_key *key = NULL;
204
205#ifdef CONFIG_DEBUG_LOCK_ALLOC
206 key = (struct lock_class_key *)&ops->lockdep_key;
207#endif
208 return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv,
209 ns, key);
210}
211
212static inline struct sysfs_dirent *
Tejun Heo496f7392013-11-28 14:54:24 -0500213kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
214 loff_t size, const struct kernfs_ops *ops, void *priv)
215{
216 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
217}
218
Tejun Heo879f40d2013-11-23 17:21:49 -0500219static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
220 const char *name)
221{
222 return kernfs_remove_by_name_ns(parent, name, NULL);
223}
224
Tejun Heo4b93dc92013-11-28 14:54:43 -0500225static inline struct dentry *
226kernfs_mount(struct file_system_type *fs_type, int flags,
227 struct kernfs_root *root)
228{
229 return kernfs_mount_ns(fs_type, flags, root, NULL);
230}
231
Tejun Heob8441ed2013-11-24 09:54:58 -0500232#endif /* __LINUX_KERNFS_H */