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 | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 21 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 22 | struct sysfs_dirent; |
| 23 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 24 | struct kernfs_root { |
| 25 | /* published fields */ |
| 26 | struct sysfs_dirent *sd; |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 27 | |
| 28 | /* private fields, do not use outside kernfs proper */ |
| 29 | struct ida ino_ida; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 30 | }; |
| 31 | |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 32 | struct 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 Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 46 | struct kernfs_ops { |
| 47 | /* |
| 48 | * Read is handled by either seq_file or raw_read(). |
| 49 | * |
Tejun Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame] | 50 | * 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 Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 53 | * 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 Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame] | 60 | 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 Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 64 | 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 Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 75 | |
| 76 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 77 | struct lock_class_key lockdep_key; |
| 78 | #endif |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 81 | #ifdef CONFIG_SYSFS |
| 82 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 83 | struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent, |
| 84 | const char *name, const void *ns); |
| 85 | void kernfs_get(struct sysfs_dirent *sd); |
| 86 | void kernfs_put(struct sysfs_dirent *sd); |
| 87 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 88 | struct kernfs_root *kernfs_create_root(void *priv); |
| 89 | void kernfs_destroy_root(struct kernfs_root *root); |
| 90 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 91 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
| 92 | const char *name, void *priv, |
| 93 | const void *ns); |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 94 | struct 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 Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 100 | struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, |
| 101 | const char *name, |
| 102 | struct sysfs_dirent *target); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 103 | void kernfs_remove(struct sysfs_dirent *sd); |
| 104 | int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, |
| 105 | const void *ns); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 106 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
| 107 | const char *new_name, const void *new_ns); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 108 | void kernfs_enable_ns(struct sysfs_dirent *sd); |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 109 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 110 | void kernfs_notify(struct sysfs_dirent *sd); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 111 | |
| 112 | #else /* CONFIG_SYSFS */ |
| 113 | |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 114 | static inline struct sysfs_dirent * |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 115 | kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name, |
| 116 | const void *ns) |
| 117 | { return NULL; } |
| 118 | |
| 119 | static inline void kernfs_get(struct sysfs_dirent *sd) { } |
| 120 | static inline void kernfs_put(struct sysfs_dirent *sd) { } |
| 121 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 122 | static inline struct kernfs_root *kernfs_create_root(void *priv) |
| 123 | { return ERR_PTR(-ENOSYS); } |
| 124 | |
| 125 | static inline void kernfs_destroy_root(struct kernfs_root *root) { } |
| 126 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 127 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 128 | kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv, |
| 129 | const void *ns) |
| 130 | { return ERR_PTR(-ENOSYS); } |
| 131 | |
| 132 | static inline struct sysfs_dirent * |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 133 | kernfs_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 Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 137 | { return ERR_PTR(-ENOSYS); } |
| 138 | |
| 139 | static inline struct sysfs_dirent * |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 140 | kernfs_create_link(struct sysfs_dirent *parent, const char *name, |
| 141 | struct sysfs_dirent *target) |
| 142 | { return ERR_PTR(-ENOSYS); } |
| 143 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 144 | static inline void kernfs_remove(struct sysfs_dirent *sd) { } |
| 145 | |
| 146 | static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, |
| 147 | const char *name, const void *ns) |
| 148 | { return -ENOSYS; } |
| 149 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 150 | static 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 Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 155 | static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { } |
| 156 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 157 | static inline int kernfs_setattr(struct sysfs_dirent *sd, |
| 158 | const struct iattr *iattr) |
| 159 | { return -ENOSYS; } |
| 160 | |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 161 | static inline void kernfs_notify(struct sysfs_dirent *sd) { } |
| 162 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 163 | #endif /* CONFIG_SYSFS */ |
| 164 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 165 | static inline struct sysfs_dirent * |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 166 | kernfs_find_and_get(struct sysfs_dirent *sd, const char *name) |
| 167 | { |
| 168 | return kernfs_find_and_get_ns(sd, name, NULL); |
| 169 | } |
| 170 | |
| 171 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 172 | kernfs_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 Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 177 | static inline struct sysfs_dirent * |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 178 | kernfs_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 | |
| 191 | static inline struct sysfs_dirent * |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 192 | kernfs_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 Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 198 | static 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 Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 204 | #endif /* __LINUX_KERNFS_H */ |