2 * kernfs.h - pseudo filesystem decoupled from vfs locking
4 * This file is released under the GPLv2.
7 #ifndef __LINUX_KERNFS_H
8 #define __LINUX_KERNFS_H
10 #include <linux/kernel.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/lockdep.h>
19 struct vm_area_struct;
23 struct sysfs_open_file {
24 /* published fields */
25 struct sysfs_dirent *sd;
28 /* private fields, do not use outside kernfs proper */
31 struct list_head list;
34 const struct vm_operations_struct *vm_ops;
39 * Read is handled by either seq_file or raw_read().
41 * If seq_show() is present, seq_file path is active. Other seq
42 * operations are optional and if not implemented, the behavior is
43 * equivalent to single_open(). @sf->private points to the
44 * associated sysfs_open_file.
46 * read() is bounced through kernel buffer and a read larger than
47 * PAGE_SIZE results in partial operation of PAGE_SIZE.
49 int (*seq_show)(struct seq_file *sf, void *v);
51 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
52 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
53 void (*seq_stop)(struct seq_file *sf, void *v);
55 ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
59 * write() is bounced through kernel buffer and a write larger than
60 * PAGE_SIZE results in partial operation of PAGE_SIZE.
62 ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
65 int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
67 #ifdef CONFIG_DEBUG_LOCK_ALLOC
68 struct lock_class_key lockdep_key;
74 struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
75 const char *name, const void *ns);
76 void kernfs_get(struct sysfs_dirent *sd);
77 void kernfs_put(struct sysfs_dirent *sd);
79 struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
80 const char *name, void *priv,
82 struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
84 umode_t mode, loff_t size,
85 const struct kernfs_ops *ops,
86 void *priv, const void *ns,
87 struct lock_class_key *key);
88 struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
90 struct sysfs_dirent *target);
91 void kernfs_remove(struct sysfs_dirent *sd);
92 int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
94 int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
95 const char *new_name, const void *new_ns);
96 void kernfs_enable_ns(struct sysfs_dirent *sd);
97 int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
98 void kernfs_notify(struct sysfs_dirent *sd);
100 #else /* CONFIG_SYSFS */
102 static inline struct sysfs_dirent *
103 kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name,
107 static inline void kernfs_get(struct sysfs_dirent *sd) { }
108 static inline void kernfs_put(struct sysfs_dirent *sd) { }
110 static inline struct sysfs_dirent *
111 kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
113 { return ERR_PTR(-ENOSYS); }
115 static inline struct sysfs_dirent *
116 kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name,
117 umode_t mode, loff_t size,
118 const struct kernfs_ops *ops, void *priv,
119 const void *ns, struct lock_class_key *key)
120 { return ERR_PTR(-ENOSYS); }
122 static inline struct sysfs_dirent *
123 kernfs_create_link(struct sysfs_dirent *parent, const char *name,
124 struct sysfs_dirent *target)
125 { return ERR_PTR(-ENOSYS); }
127 static inline void kernfs_remove(struct sysfs_dirent *sd) { }
129 static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
130 const char *name, const void *ns)
133 static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
134 struct sysfs_dirent *new_parent,
135 const char *new_name, const void *new_ns)
138 static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
140 static inline int kernfs_setattr(struct sysfs_dirent *sd,
141 const struct iattr *iattr)
144 static inline void kernfs_notify(struct sysfs_dirent *sd) { }
146 #endif /* CONFIG_SYSFS */
148 static inline struct sysfs_dirent *
149 kernfs_find_and_get(struct sysfs_dirent *sd, const char *name)
151 return kernfs_find_and_get_ns(sd, name, NULL);
154 static inline struct sysfs_dirent *
155 kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
157 return kernfs_create_dir_ns(parent, name, priv, NULL);
160 static inline struct sysfs_dirent *
161 kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
162 umode_t mode, loff_t size, const struct kernfs_ops *ops,
163 void *priv, const void *ns)
165 struct lock_class_key *key = NULL;
167 #ifdef CONFIG_DEBUG_LOCK_ALLOC
168 key = (struct lock_class_key *)&ops->lockdep_key;
170 return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv,
174 static inline struct sysfs_dirent *
175 kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
176 loff_t size, const struct kernfs_ops *ops, void *priv)
178 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
181 static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
184 return kernfs_remove_by_name_ns(parent, name, NULL);
187 #endif /* __LINUX_KERNFS_H */