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 | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 14 | #include <linux/lockdep.h> |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 15 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 16 | struct file; |
| 17 | struct iattr; |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 18 | struct seq_file; |
| 19 | struct vm_area_struct; |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 20 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 21 | struct sysfs_dirent; |
| 22 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame^] | 23 | struct kernfs_root { |
| 24 | /* published fields */ |
| 25 | struct sysfs_dirent *sd; |
| 26 | }; |
| 27 | |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 28 | struct sysfs_open_file { |
| 29 | /* published fields */ |
| 30 | struct sysfs_dirent *sd; |
| 31 | struct file *file; |
| 32 | |
| 33 | /* private fields, do not use outside kernfs proper */ |
| 34 | struct mutex mutex; |
| 35 | int event; |
| 36 | struct list_head list; |
| 37 | |
| 38 | bool mmapped; |
| 39 | const struct vm_operations_struct *vm_ops; |
| 40 | }; |
| 41 | |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 42 | struct kernfs_ops { |
| 43 | /* |
| 44 | * Read is handled by either seq_file or raw_read(). |
| 45 | * |
Tejun Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame] | 46 | * If seq_show() is present, seq_file path is active. Other seq |
| 47 | * operations are optional and if not implemented, the behavior is |
| 48 | * equivalent to single_open(). @sf->private points to the |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 49 | * associated sysfs_open_file. |
| 50 | * |
| 51 | * read() is bounced through kernel buffer and a read larger than |
| 52 | * PAGE_SIZE results in partial operation of PAGE_SIZE. |
| 53 | */ |
| 54 | int (*seq_show)(struct seq_file *sf, void *v); |
| 55 | |
Tejun Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame] | 56 | void *(*seq_start)(struct seq_file *sf, loff_t *ppos); |
| 57 | void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos); |
| 58 | void (*seq_stop)(struct seq_file *sf, void *v); |
| 59 | |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 60 | ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes, |
| 61 | loff_t off); |
| 62 | |
| 63 | /* |
| 64 | * write() is bounced through kernel buffer and a write larger than |
| 65 | * PAGE_SIZE results in partial operation of PAGE_SIZE. |
| 66 | */ |
| 67 | ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes, |
| 68 | loff_t off); |
| 69 | |
| 70 | int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma); |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 71 | |
| 72 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 73 | struct lock_class_key lockdep_key; |
| 74 | #endif |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 77 | #ifdef CONFIG_SYSFS |
| 78 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 79 | struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent, |
| 80 | const char *name, const void *ns); |
| 81 | void kernfs_get(struct sysfs_dirent *sd); |
| 82 | void kernfs_put(struct sysfs_dirent *sd); |
| 83 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame^] | 84 | struct kernfs_root *kernfs_create_root(void *priv); |
| 85 | void kernfs_destroy_root(struct kernfs_root *root); |
| 86 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 87 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
| 88 | const char *name, void *priv, |
| 89 | const void *ns); |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 90 | struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent, |
| 91 | const char *name, |
| 92 | umode_t mode, loff_t size, |
| 93 | const struct kernfs_ops *ops, |
| 94 | void *priv, const void *ns, |
| 95 | struct lock_class_key *key); |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 96 | struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, |
| 97 | const char *name, |
| 98 | struct sysfs_dirent *target); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 99 | void kernfs_remove(struct sysfs_dirent *sd); |
| 100 | int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, |
| 101 | const void *ns); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 102 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
| 103 | const char *new_name, const void *new_ns); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 104 | void kernfs_enable_ns(struct sysfs_dirent *sd); |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 105 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 106 | void kernfs_notify(struct sysfs_dirent *sd); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 107 | |
| 108 | #else /* CONFIG_SYSFS */ |
| 109 | |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 110 | static inline struct sysfs_dirent * |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 111 | kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name, |
| 112 | const void *ns) |
| 113 | { return NULL; } |
| 114 | |
| 115 | static inline void kernfs_get(struct sysfs_dirent *sd) { } |
| 116 | static inline void kernfs_put(struct sysfs_dirent *sd) { } |
| 117 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame^] | 118 | static inline struct kernfs_root *kernfs_create_root(void *priv) |
| 119 | { return ERR_PTR(-ENOSYS); } |
| 120 | |
| 121 | static inline void kernfs_destroy_root(struct kernfs_root *root) { } |
| 122 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 123 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 124 | kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv, |
| 125 | const void *ns) |
| 126 | { return ERR_PTR(-ENOSYS); } |
| 127 | |
| 128 | static inline struct sysfs_dirent * |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 129 | kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name, |
| 130 | umode_t mode, loff_t size, |
| 131 | const struct kernfs_ops *ops, void *priv, |
| 132 | const void *ns, struct lock_class_key *key) |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 133 | { return ERR_PTR(-ENOSYS); } |
| 134 | |
| 135 | static inline struct sysfs_dirent * |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 136 | kernfs_create_link(struct sysfs_dirent *parent, const char *name, |
| 137 | struct sysfs_dirent *target) |
| 138 | { return ERR_PTR(-ENOSYS); } |
| 139 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 140 | static inline void kernfs_remove(struct sysfs_dirent *sd) { } |
| 141 | |
| 142 | static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, |
| 143 | const char *name, const void *ns) |
| 144 | { return -ENOSYS; } |
| 145 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 146 | static inline int kernfs_rename_ns(struct sysfs_dirent *sd, |
| 147 | struct sysfs_dirent *new_parent, |
| 148 | const char *new_name, const void *new_ns) |
| 149 | { return -ENOSYS; } |
| 150 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 151 | static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { } |
| 152 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 153 | static inline int kernfs_setattr(struct sysfs_dirent *sd, |
| 154 | const struct iattr *iattr) |
| 155 | { return -ENOSYS; } |
| 156 | |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 157 | static inline void kernfs_notify(struct sysfs_dirent *sd) { } |
| 158 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 159 | #endif /* CONFIG_SYSFS */ |
| 160 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 161 | static inline struct sysfs_dirent * |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 162 | kernfs_find_and_get(struct sysfs_dirent *sd, const char *name) |
| 163 | { |
| 164 | return kernfs_find_and_get_ns(sd, name, NULL); |
| 165 | } |
| 166 | |
| 167 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 168 | kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv) |
| 169 | { |
| 170 | return kernfs_create_dir_ns(parent, name, priv, NULL); |
| 171 | } |
| 172 | |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 173 | static inline struct sysfs_dirent * |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 174 | kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name, |
| 175 | umode_t mode, loff_t size, const struct kernfs_ops *ops, |
| 176 | void *priv, const void *ns) |
| 177 | { |
| 178 | struct lock_class_key *key = NULL; |
| 179 | |
| 180 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 181 | key = (struct lock_class_key *)&ops->lockdep_key; |
| 182 | #endif |
| 183 | return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv, |
| 184 | ns, key); |
| 185 | } |
| 186 | |
| 187 | static inline struct sysfs_dirent * |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 188 | kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode, |
| 189 | loff_t size, const struct kernfs_ops *ops, void *priv) |
| 190 | { |
| 191 | return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL); |
| 192 | } |
| 193 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 194 | static inline int kernfs_remove_by_name(struct sysfs_dirent *parent, |
| 195 | const char *name) |
| 196 | { |
| 197 | return kernfs_remove_by_name_ns(parent, name, NULL); |
| 198 | } |
| 199 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 200 | #endif /* __LINUX_KERNFS_H */ |