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 | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 14 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 15 | struct file; |
| 16 | struct iattr; |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 17 | struct seq_file; |
| 18 | struct vm_area_struct; |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 19 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 20 | struct sysfs_dirent; |
| 21 | |
Tejun Heo | dd8a5b0 | 2013-11-28 14:54:20 -0500 | [diff] [blame] | 22 | struct sysfs_open_file { |
| 23 | /* published fields */ |
| 24 | struct sysfs_dirent *sd; |
| 25 | struct file *file; |
| 26 | |
| 27 | /* private fields, do not use outside kernfs proper */ |
| 28 | struct mutex mutex; |
| 29 | int event; |
| 30 | struct list_head list; |
| 31 | |
| 32 | bool mmapped; |
| 33 | const struct vm_operations_struct *vm_ops; |
| 34 | }; |
| 35 | |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 36 | struct kernfs_ops { |
| 37 | /* |
| 38 | * Read is handled by either seq_file or raw_read(). |
| 39 | * |
Tejun Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame^] | 40 | * If seq_show() is present, seq_file path is active. Other seq |
| 41 | * operations are optional and if not implemented, the behavior is |
| 42 | * equivalent to single_open(). @sf->private points to the |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 43 | * associated sysfs_open_file. |
| 44 | * |
| 45 | * read() is bounced through kernel buffer and a read larger than |
| 46 | * PAGE_SIZE results in partial operation of PAGE_SIZE. |
| 47 | */ |
| 48 | int (*seq_show)(struct seq_file *sf, void *v); |
| 49 | |
Tejun Heo | d19b984 | 2013-11-28 14:54:26 -0500 | [diff] [blame^] | 50 | void *(*seq_start)(struct seq_file *sf, loff_t *ppos); |
| 51 | void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos); |
| 52 | void (*seq_stop)(struct seq_file *sf, void *v); |
| 53 | |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 54 | ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes, |
| 55 | loff_t off); |
| 56 | |
| 57 | /* |
| 58 | * write() is bounced through kernel buffer and a write larger than |
| 59 | * PAGE_SIZE results in partial operation of PAGE_SIZE. |
| 60 | */ |
| 61 | ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes, |
| 62 | loff_t off); |
| 63 | |
| 64 | int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma); |
| 65 | }; |
| 66 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 67 | #ifdef CONFIG_SYSFS |
| 68 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 69 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
| 70 | const char *name, void *priv, |
| 71 | const void *ns); |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 72 | struct sysfs_dirent *kernfs_create_file_ns(struct sysfs_dirent *parent, |
| 73 | const char *name, |
| 74 | umode_t mode, loff_t size, |
| 75 | const struct kernfs_ops *ops, |
| 76 | void *priv, const void *ns); |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 77 | struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, |
| 78 | const char *name, |
| 79 | struct sysfs_dirent *target); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 80 | void kernfs_remove(struct sysfs_dirent *sd); |
| 81 | int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, |
| 82 | const void *ns); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 83 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
| 84 | const char *new_name, const void *new_ns); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 85 | void kernfs_enable_ns(struct sysfs_dirent *sd); |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 86 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 87 | |
| 88 | #else /* CONFIG_SYSFS */ |
| 89 | |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 90 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 91 | kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv, |
| 92 | const void *ns) |
| 93 | { return ERR_PTR(-ENOSYS); } |
| 94 | |
| 95 | static inline struct sysfs_dirent * |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 96 | kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name, |
| 97 | umode_t mode, loff_t size, const struct kernfs_ops *ops, |
| 98 | void *priv, const void *ns) |
| 99 | { return ERR_PTR(-ENOSYS); } |
| 100 | |
| 101 | static inline struct sysfs_dirent * |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 102 | kernfs_create_link(struct sysfs_dirent *parent, const char *name, |
| 103 | struct sysfs_dirent *target) |
| 104 | { return ERR_PTR(-ENOSYS); } |
| 105 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 106 | static inline void kernfs_remove(struct sysfs_dirent *sd) { } |
| 107 | |
| 108 | static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, |
| 109 | const char *name, const void *ns) |
| 110 | { return -ENOSYS; } |
| 111 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 112 | static inline int kernfs_rename_ns(struct sysfs_dirent *sd, |
| 113 | struct sysfs_dirent *new_parent, |
| 114 | const char *new_name, const void *new_ns) |
| 115 | { return -ENOSYS; } |
| 116 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 117 | static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { } |
| 118 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 119 | static inline int kernfs_setattr(struct sysfs_dirent *sd, |
| 120 | const struct iattr *iattr) |
| 121 | { return -ENOSYS; } |
| 122 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 123 | #endif /* CONFIG_SYSFS */ |
| 124 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 125 | static inline struct sysfs_dirent * |
| 126 | kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv) |
| 127 | { |
| 128 | return kernfs_create_dir_ns(parent, name, priv, NULL); |
| 129 | } |
| 130 | |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 131 | static inline struct sysfs_dirent * |
| 132 | kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode, |
| 133 | loff_t size, const struct kernfs_ops *ops, void *priv) |
| 134 | { |
| 135 | return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL); |
| 136 | } |
| 137 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 138 | static inline int kernfs_remove_by_name(struct sysfs_dirent *parent, |
| 139 | const char *name) |
| 140 | { |
| 141 | return kernfs_remove_by_name_ns(parent, name, NULL); |
| 142 | } |
| 143 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 144 | #endif /* __LINUX_KERNFS_H */ |