Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 1 | /* |
| 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 Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 11 | #include <linux/err.h> |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 12 | #include <linux/list.h> |
| 13 | #include <linux/mutex.h> |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 14 | #include <linux/idr.h> |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 15 | #include <linux/lockdep.h> |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 16 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 17 | struct file; |
| 18 | struct iattr; |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 19 | struct seq_file; |
| 20 | struct vm_area_struct; |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 21 | struct super_block; |
| 22 | struct file_system_type; |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 23 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 24 | struct sysfs_dirent; |
| 25 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 26 | struct kernfs_root { |
| 27 | /* published fields */ |
| 28 | struct sysfs_dirent *sd; |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 29 | |
| 30 | /* private fields, do not use outside kernfs proper */ |
| 31 | struct ida ino_ida; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 32 | }; |
| 33 | |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 34 | struct 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 Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 48 | struct kernfs_ops { |
| 49 | /* |
| 50 | * Read is handled by either seq_file or raw_read(). |
| 51 | * |
Tejun Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame] | 52 | * 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 Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 55 | * 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 Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame] | 62 | 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 Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 66 | 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 Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 77 | |
| 78 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 79 | struct lock_class_key lockdep_key; |
| 80 | #endif |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 83 | #ifdef CONFIG_SYSFS |
| 84 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 85 | struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent, |
| 86 | const char *name, const void *ns); |
| 87 | void kernfs_get(struct sysfs_dirent *sd); |
| 88 | void kernfs_put(struct sysfs_dirent *sd); |
| 89 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 90 | struct kernfs_root *kernfs_create_root(void *priv); |
| 91 | void kernfs_destroy_root(struct kernfs_root *root); |
| 92 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 93 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
| 94 | const char *name, void *priv, |
| 95 | const void *ns); |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 96 | struct 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 Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 102 | struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, |
| 103 | const char *name, |
| 104 | struct sysfs_dirent *target); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 105 | void kernfs_remove(struct sysfs_dirent *sd); |
| 106 | int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, |
| 107 | const void *ns); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 108 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
| 109 | const char *new_name, const void *new_ns); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 110 | void kernfs_enable_ns(struct sysfs_dirent *sd); |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 111 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 112 | void kernfs_notify(struct sysfs_dirent *sd); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 113 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 114 | const void *kernfs_super_ns(struct super_block *sb); |
| 115 | struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, |
| 116 | struct kernfs_root *root, const void *ns); |
| 117 | void kernfs_kill_sb(struct super_block *sb); |
| 118 | |
| 119 | void kernfs_init(void); |
| 120 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 121 | #else /* CONFIG_SYSFS */ |
| 122 | |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 123 | static inline struct sysfs_dirent * |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 124 | kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name, |
| 125 | const void *ns) |
| 126 | { return NULL; } |
| 127 | |
| 128 | static inline void kernfs_get(struct sysfs_dirent *sd) { } |
| 129 | static inline void kernfs_put(struct sysfs_dirent *sd) { } |
| 130 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 131 | static inline struct kernfs_root *kernfs_create_root(void *priv) |
| 132 | { return ERR_PTR(-ENOSYS); } |
| 133 | |
| 134 | static inline void kernfs_destroy_root(struct kernfs_root *root) { } |
| 135 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 136 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 137 | kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv, |
| 138 | const void *ns) |
| 139 | { return ERR_PTR(-ENOSYS); } |
| 140 | |
| 141 | static inline struct sysfs_dirent * |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 142 | kernfs_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 Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 146 | { return ERR_PTR(-ENOSYS); } |
| 147 | |
| 148 | static inline struct sysfs_dirent * |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 149 | kernfs_create_link(struct sysfs_dirent *parent, const char *name, |
| 150 | struct sysfs_dirent *target) |
| 151 | { return ERR_PTR(-ENOSYS); } |
| 152 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 153 | static inline void kernfs_remove(struct sysfs_dirent *sd) { } |
| 154 | |
| 155 | static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, |
| 156 | const char *name, const void *ns) |
| 157 | { return -ENOSYS; } |
| 158 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 159 | static 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 Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 164 | static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { } |
| 165 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 166 | static inline int kernfs_setattr(struct sysfs_dirent *sd, |
| 167 | const struct iattr *iattr) |
| 168 | { return -ENOSYS; } |
| 169 | |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 170 | static inline void kernfs_notify(struct sysfs_dirent *sd) { } |
| 171 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 172 | static inline const void *kernfs_super_ns(struct super_block *sb) |
| 173 | { return NULL; } |
| 174 | |
| 175 | static inline struct dentry * |
| 176 | kernfs_mount_ns(struct file_system_type *fs_type, int flags, |
| 177 | struct kernfs_root *root, const void *ns) |
| 178 | { return ERR_PTR(-ENOSYS); } |
| 179 | |
| 180 | static inline void kernfs_kill_sb(struct super_block *sb) { } |
| 181 | |
| 182 | static inline void kernfs_init(void) { } |
| 183 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 184 | #endif /* CONFIG_SYSFS */ |
| 185 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 186 | static inline struct sysfs_dirent * |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 187 | kernfs_find_and_get(struct sysfs_dirent *sd, const char *name) |
| 188 | { |
| 189 | return kernfs_find_and_get_ns(sd, name, NULL); |
| 190 | } |
| 191 | |
| 192 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 193 | kernfs_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 Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 198 | static inline struct sysfs_dirent * |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 199 | kernfs_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 | |
| 212 | static inline struct sysfs_dirent * |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 213 | kernfs_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 Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 219 | static 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 Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 225 | static inline struct dentry * |
| 226 | kernfs_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 Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 232 | #endif /* __LINUX_KERNFS_H */ |