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 | * |
| 40 | * If seq_show() is present, seq_file path is active. The behavior |
| 41 | * is equivalent to single_open(). @sf->private points to the |
| 42 | * associated sysfs_open_file. |
| 43 | * |
| 44 | * read() is bounced through kernel buffer and a read larger than |
| 45 | * PAGE_SIZE results in partial operation of PAGE_SIZE. |
| 46 | */ |
| 47 | int (*seq_show)(struct seq_file *sf, void *v); |
| 48 | |
| 49 | ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes, |
| 50 | loff_t off); |
| 51 | |
| 52 | /* |
| 53 | * write() is bounced through kernel buffer and a write larger than |
| 54 | * PAGE_SIZE results in partial operation of PAGE_SIZE. |
| 55 | */ |
| 56 | ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes, |
| 57 | loff_t off); |
| 58 | |
| 59 | int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma); |
| 60 | }; |
| 61 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 62 | #ifdef CONFIG_SYSFS |
| 63 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 64 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
| 65 | const char *name, void *priv, |
| 66 | const void *ns); |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 67 | struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, |
| 68 | const char *name, |
| 69 | struct sysfs_dirent *target); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 70 | void kernfs_remove(struct sysfs_dirent *sd); |
| 71 | int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, |
| 72 | const void *ns); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 73 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
| 74 | const char *new_name, const void *new_ns); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 75 | void kernfs_enable_ns(struct sysfs_dirent *sd); |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 76 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 77 | |
| 78 | #else /* CONFIG_SYSFS */ |
| 79 | |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 80 | static inline struct sysfs_dirent * |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 81 | kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv, |
| 82 | const void *ns) |
| 83 | { return ERR_PTR(-ENOSYS); } |
| 84 | |
| 85 | static inline struct sysfs_dirent * |
Tejun Heo | 5d0e26b | 2013-11-23 17:21:50 -0500 | [diff] [blame] | 86 | kernfs_create_link(struct sysfs_dirent *parent, const char *name, |
| 87 | struct sysfs_dirent *target) |
| 88 | { return ERR_PTR(-ENOSYS); } |
| 89 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 90 | static inline void kernfs_remove(struct sysfs_dirent *sd) { } |
| 91 | |
| 92 | static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, |
| 93 | const char *name, const void *ns) |
| 94 | { return -ENOSYS; } |
| 95 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 96 | static inline int kernfs_rename_ns(struct sysfs_dirent *sd, |
| 97 | struct sysfs_dirent *new_parent, |
| 98 | const char *new_name, const void *new_ns) |
| 99 | { return -ENOSYS; } |
| 100 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 101 | static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { } |
| 102 | |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 103 | static inline int kernfs_setattr(struct sysfs_dirent *sd, |
| 104 | const struct iattr *iattr) |
| 105 | { return -ENOSYS; } |
| 106 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 107 | #endif /* CONFIG_SYSFS */ |
| 108 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 109 | static inline struct sysfs_dirent * |
| 110 | kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv) |
| 111 | { |
| 112 | return kernfs_create_dir_ns(parent, name, priv, NULL); |
| 113 | } |
| 114 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 115 | static inline int kernfs_remove_by_name(struct sysfs_dirent *parent, |
| 116 | const char *name) |
| 117 | { |
| 118 | return kernfs_remove_by_name_ns(parent, name, NULL); |
| 119 | } |
| 120 | |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 121 | #endif /* __LINUX_KERNFS_H */ |