blob: fad8b986800fbdd61dd1d54400675d85a23d6a10 [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 Heo5d604182013-11-23 17:21:52 -050021
Tejun Heob8441ed2013-11-24 09:54:58 -050022struct sysfs_dirent;
23
Tejun Heoba7443b2013-11-28 14:54:40 -050024struct kernfs_root {
25 /* published fields */
26 struct sysfs_dirent *sd;
Tejun Heobc755552013-11-28 14:54:41 -050027
28 /* private fields, do not use outside kernfs proper */
29 struct ida ino_ida;
Tejun Heoba7443b2013-11-28 14:54:40 -050030};
31
Tejun Heodd8a5b02013-11-28 14:54:20 -050032struct sysfs_open_file {
33 /* published fields */
34 struct sysfs_dirent *sd;
35 struct file *file;
36
37 /* private fields, do not use outside kernfs proper */
38 struct mutex mutex;
39 int event;
40 struct list_head list;
41
42 bool mmapped;
43 const struct vm_operations_struct *vm_ops;
44};
45
Tejun Heof6acf8b2013-11-28 14:54:21 -050046struct kernfs_ops {
47 /*
48 * Read is handled by either seq_file or raw_read().
49 *
Tejun Heod19b9842013-11-28 14:54:26 -050050 * If seq_show() is present, seq_file path is active. Other seq
51 * operations are optional and if not implemented, the behavior is
52 * equivalent to single_open(). @sf->private points to the
Tejun Heof6acf8b2013-11-28 14:54:21 -050053 * associated sysfs_open_file.
54 *
55 * read() is bounced through kernel buffer and a read larger than
56 * PAGE_SIZE results in partial operation of PAGE_SIZE.
57 */
58 int (*seq_show)(struct seq_file *sf, void *v);
59
Tejun Heod19b9842013-11-28 14:54:26 -050060 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
61 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
62 void (*seq_stop)(struct seq_file *sf, void *v);
63
Tejun Heof6acf8b2013-11-28 14:54:21 -050064 ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
65 loff_t off);
66
67 /*
68 * write() is bounced through kernel buffer and a write larger than
69 * PAGE_SIZE results in partial operation of PAGE_SIZE.
70 */
71 ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
72 loff_t off);
73
74 int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -050075
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 struct lock_class_key lockdep_key;
78#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -050079};
80
Tejun Heo879f40d2013-11-23 17:21:49 -050081#ifdef CONFIG_SYSFS
82
Tejun Heoccf73cf2013-11-28 14:54:30 -050083struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
84 const char *name, const void *ns);
85void kernfs_get(struct sysfs_dirent *sd);
86void kernfs_put(struct sysfs_dirent *sd);
87
Tejun Heoba7443b2013-11-28 14:54:40 -050088struct kernfs_root *kernfs_create_root(void *priv);
89void kernfs_destroy_root(struct kernfs_root *root);
90
Tejun Heo93b2b8e2013-11-28 14:54:15 -050091struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
92 const char *name, void *priv,
93 const void *ns);
Tejun Heo517e64f2013-11-28 14:54:29 -050094struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
95 const char *name,
96 umode_t mode, loff_t size,
97 const struct kernfs_ops *ops,
98 void *priv, const void *ns,
99 struct lock_class_key *key);
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500100struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
101 const char *name,
102 struct sysfs_dirent *target);
Tejun Heo879f40d2013-11-23 17:21:49 -0500103void kernfs_remove(struct sysfs_dirent *sd);
104int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
105 const void *ns);
Tejun Heo890ece12013-11-23 17:21:51 -0500106int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
107 const char *new_name, const void *new_ns);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500108void kernfs_enable_ns(struct sysfs_dirent *sd);
Tejun Heo5d604182013-11-23 17:21:52 -0500109int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
Tejun Heo024f6472013-11-28 14:54:27 -0500110void kernfs_notify(struct sysfs_dirent *sd);
Tejun Heo879f40d2013-11-23 17:21:49 -0500111
112#else /* CONFIG_SYSFS */
113
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500114static inline struct sysfs_dirent *
Tejun Heoccf73cf2013-11-28 14:54:30 -0500115kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name,
116 const void *ns)
117{ return NULL; }
118
119static inline void kernfs_get(struct sysfs_dirent *sd) { }
120static inline void kernfs_put(struct sysfs_dirent *sd) { }
121
Tejun Heoba7443b2013-11-28 14:54:40 -0500122static inline struct kernfs_root *kernfs_create_root(void *priv)
123{ return ERR_PTR(-ENOSYS); }
124
125static inline void kernfs_destroy_root(struct kernfs_root *root) { }
126
Tejun Heoccf73cf2013-11-28 14:54:30 -0500127static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500128kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
129 const void *ns)
130{ return ERR_PTR(-ENOSYS); }
131
132static inline struct sysfs_dirent *
Tejun Heo517e64f2013-11-28 14:54:29 -0500133kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name,
134 umode_t mode, loff_t size,
135 const struct kernfs_ops *ops, void *priv,
136 const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500137{ return ERR_PTR(-ENOSYS); }
138
139static inline struct sysfs_dirent *
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500140kernfs_create_link(struct sysfs_dirent *parent, const char *name,
141 struct sysfs_dirent *target)
142{ return ERR_PTR(-ENOSYS); }
143
Tejun Heo879f40d2013-11-23 17:21:49 -0500144static inline void kernfs_remove(struct sysfs_dirent *sd) { }
145
146static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
147 const char *name, const void *ns)
148{ return -ENOSYS; }
149
Tejun Heo890ece12013-11-23 17:21:51 -0500150static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
151 struct sysfs_dirent *new_parent,
152 const char *new_name, const void *new_ns)
153{ return -ENOSYS; }
154
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500155static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
156
Tejun Heo5d604182013-11-23 17:21:52 -0500157static inline int kernfs_setattr(struct sysfs_dirent *sd,
158 const struct iattr *iattr)
159{ return -ENOSYS; }
160
Tejun Heo024f6472013-11-28 14:54:27 -0500161static inline void kernfs_notify(struct sysfs_dirent *sd) { }
162
Tejun Heo879f40d2013-11-23 17:21:49 -0500163#endif /* CONFIG_SYSFS */
164
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500165static inline struct sysfs_dirent *
Tejun Heoccf73cf2013-11-28 14:54:30 -0500166kernfs_find_and_get(struct sysfs_dirent *sd, const char *name)
167{
168 return kernfs_find_and_get_ns(sd, name, NULL);
169}
170
171static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500172kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
173{
174 return kernfs_create_dir_ns(parent, name, priv, NULL);
175}
176
Tejun Heo496f7392013-11-28 14:54:24 -0500177static inline struct sysfs_dirent *
Tejun Heo517e64f2013-11-28 14:54:29 -0500178kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
179 umode_t mode, loff_t size, const struct kernfs_ops *ops,
180 void *priv, const void *ns)
181{
182 struct lock_class_key *key = NULL;
183
184#ifdef CONFIG_DEBUG_LOCK_ALLOC
185 key = (struct lock_class_key *)&ops->lockdep_key;
186#endif
187 return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv,
188 ns, key);
189}
190
191static inline struct sysfs_dirent *
Tejun Heo496f7392013-11-28 14:54:24 -0500192kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
193 loff_t size, const struct kernfs_ops *ops, void *priv)
194{
195 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
196}
197
Tejun Heo879f40d2013-11-23 17:21:49 -0500198static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
199 const char *name)
200{
201 return kernfs_remove_by_name_ns(parent, name, NULL);
202}
203
Tejun Heob8441ed2013-11-24 09:54:58 -0500204#endif /* __LINUX_KERNFS_H */