blob: 105d09dcb0645deda748fddc0fdb85899df9e5a0 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
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 Heo879f40d2013-11-23 17:21:49 -050010#include <linux/kernel.h>
Tejun Heo5d0e26b2013-11-23 17:21:50 -050011#include <linux/err.h>
Tejun Heodd8a5b02013-11-28 14:54:20 -050012#include <linux/list.h>
13#include <linux/mutex.h>
Tejun Heo517e64f2013-11-28 14:54:29 -050014#include <linux/lockdep.h>
Tejun Heo879f40d2013-11-23 17:21:49 -050015
Tejun Heo5d604182013-11-23 17:21:52 -050016struct file;
17struct iattr;
Tejun Heodd8a5b02013-11-28 14:54:20 -050018struct seq_file;
19struct vm_area_struct;
Tejun Heo5d604182013-11-23 17:21:52 -050020
Tejun Heob8441ed2013-11-24 09:54:58 -050021struct sysfs_dirent;
22
Tejun Heodd8a5b02013-11-28 14:54:20 -050023struct sysfs_open_file {
24 /* published fields */
25 struct sysfs_dirent *sd;
26 struct file *file;
27
28 /* private fields, do not use outside kernfs proper */
29 struct mutex mutex;
30 int event;
31 struct list_head list;
32
33 bool mmapped;
34 const struct vm_operations_struct *vm_ops;
35};
36
Tejun Heof6acf8b2013-11-28 14:54:21 -050037struct kernfs_ops {
38 /*
39 * Read is handled by either seq_file or raw_read().
40 *
Tejun Heod19b9842013-11-28 14:54:26 -050041 * 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
Tejun Heof6acf8b2013-11-28 14:54:21 -050044 * associated sysfs_open_file.
45 *
46 * read() is bounced through kernel buffer and a read larger than
47 * PAGE_SIZE results in partial operation of PAGE_SIZE.
48 */
49 int (*seq_show)(struct seq_file *sf, void *v);
50
Tejun Heod19b9842013-11-28 14:54:26 -050051 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);
54
Tejun Heof6acf8b2013-11-28 14:54:21 -050055 ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
56 loff_t off);
57
58 /*
59 * write() is bounced through kernel buffer and a write larger than
60 * PAGE_SIZE results in partial operation of PAGE_SIZE.
61 */
62 ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
63 loff_t off);
64
65 int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
Tejun Heo517e64f2013-11-28 14:54:29 -050066
67#ifdef CONFIG_DEBUG_LOCK_ALLOC
68 struct lock_class_key lockdep_key;
69#endif
Tejun Heof6acf8b2013-11-28 14:54:21 -050070};
71
Tejun Heo879f40d2013-11-23 17:21:49 -050072#ifdef CONFIG_SYSFS
73
Tejun Heo93b2b8e2013-11-28 14:54:15 -050074struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
75 const char *name, void *priv,
76 const void *ns);
Tejun Heo517e64f2013-11-28 14:54:29 -050077struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
78 const char *name,
79 umode_t mode, loff_t size,
80 const struct kernfs_ops *ops,
81 void *priv, const void *ns,
82 struct lock_class_key *key);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050083struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
84 const char *name,
85 struct sysfs_dirent *target);
Tejun Heo879f40d2013-11-23 17:21:49 -050086void kernfs_remove(struct sysfs_dirent *sd);
87int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
88 const void *ns);
Tejun Heo890ece12013-11-23 17:21:51 -050089int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
90 const char *new_name, const void *new_ns);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050091void kernfs_enable_ns(struct sysfs_dirent *sd);
Tejun Heo5d604182013-11-23 17:21:52 -050092int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
Tejun Heo024f6472013-11-28 14:54:27 -050093void kernfs_notify(struct sysfs_dirent *sd);
Tejun Heo879f40d2013-11-23 17:21:49 -050094
95#else /* CONFIG_SYSFS */
96
Tejun Heo5d0e26b2013-11-23 17:21:50 -050097static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -050098kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
99 const void *ns)
100{ return ERR_PTR(-ENOSYS); }
101
102static inline struct sysfs_dirent *
Tejun Heo517e64f2013-11-28 14:54:29 -0500103kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name,
104 umode_t mode, loff_t size,
105 const struct kernfs_ops *ops, void *priv,
106 const void *ns, struct lock_class_key *key)
Tejun Heo496f7392013-11-28 14:54:24 -0500107{ return ERR_PTR(-ENOSYS); }
108
109static inline struct sysfs_dirent *
Tejun Heo5d0e26b2013-11-23 17:21:50 -0500110kernfs_create_link(struct sysfs_dirent *parent, const char *name,
111 struct sysfs_dirent *target)
112{ return ERR_PTR(-ENOSYS); }
113
Tejun Heo879f40d2013-11-23 17:21:49 -0500114static inline void kernfs_remove(struct sysfs_dirent *sd) { }
115
116static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
117 const char *name, const void *ns)
118{ return -ENOSYS; }
119
Tejun Heo890ece12013-11-23 17:21:51 -0500120static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
121 struct sysfs_dirent *new_parent,
122 const char *new_name, const void *new_ns)
123{ return -ENOSYS; }
124
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500125static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
126
Tejun Heo5d604182013-11-23 17:21:52 -0500127static inline int kernfs_setattr(struct sysfs_dirent *sd,
128 const struct iattr *iattr)
129{ return -ENOSYS; }
130
Tejun Heo024f6472013-11-28 14:54:27 -0500131static inline void kernfs_notify(struct sysfs_dirent *sd) { }
132
Tejun Heo879f40d2013-11-23 17:21:49 -0500133#endif /* CONFIG_SYSFS */
134
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500135static inline struct sysfs_dirent *
136kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
137{
138 return kernfs_create_dir_ns(parent, name, priv, NULL);
139}
140
Tejun Heo496f7392013-11-28 14:54:24 -0500141static inline struct sysfs_dirent *
Tejun Heo517e64f2013-11-28 14:54:29 -0500142kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
143 umode_t mode, loff_t size, const struct kernfs_ops *ops,
144 void *priv, const void *ns)
145{
146 struct lock_class_key *key = NULL;
147
148#ifdef CONFIG_DEBUG_LOCK_ALLOC
149 key = (struct lock_class_key *)&ops->lockdep_key;
150#endif
151 return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv,
152 ns, key);
153}
154
155static inline struct sysfs_dirent *
Tejun Heo496f7392013-11-28 14:54:24 -0500156kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
157 loff_t size, const struct kernfs_ops *ops, void *priv)
158{
159 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
160}
161
Tejun Heo879f40d2013-11-23 17:21:49 -0500162static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
163 const char *name)
164{
165 return kernfs_remove_by_name_ns(parent, name, NULL);
166}
167
Tejun Heob8441ed2013-11-24 09:54:58 -0500168#endif /* __LINUX_KERNFS_H */