blob: d0912cf02087e49e64dddf6f1254f8ed0886ae6c [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 Heo879f40d2013-11-23 17:21:49 -050014
Tejun Heo5d604182013-11-23 17:21:52 -050015struct file;
16struct iattr;
Tejun Heodd8a5b02013-11-28 14:54:20 -050017struct seq_file;
18struct vm_area_struct;
Tejun Heo5d604182013-11-23 17:21:52 -050019
Tejun Heob8441ed2013-11-24 09:54:58 -050020struct sysfs_dirent;
21
Tejun Heodd8a5b02013-11-28 14:54:20 -050022struct 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 Heof6acf8b2013-11-28 14:54:21 -050036struct 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 Heo879f40d2013-11-23 17:21:49 -050062#ifdef CONFIG_SYSFS
63
Tejun Heo93b2b8e2013-11-28 14:54:15 -050064struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
65 const char *name, void *priv,
66 const void *ns);
Tejun Heo496f7392013-11-28 14:54:24 -050067struct sysfs_dirent *kernfs_create_file_ns(struct sysfs_dirent *parent,
68 const char *name,
69 umode_t mode, loff_t size,
70 const struct kernfs_ops *ops,
71 void *priv, const void *ns);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050072struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
73 const char *name,
74 struct sysfs_dirent *target);
Tejun Heo879f40d2013-11-23 17:21:49 -050075void kernfs_remove(struct sysfs_dirent *sd);
76int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
77 const void *ns);
Tejun Heo890ece12013-11-23 17:21:51 -050078int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
79 const char *new_name, const void *new_ns);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050080void kernfs_enable_ns(struct sysfs_dirent *sd);
Tejun Heo5d604182013-11-23 17:21:52 -050081int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
Tejun Heo879f40d2013-11-23 17:21:49 -050082
83#else /* CONFIG_SYSFS */
84
Tejun Heo5d0e26b2013-11-23 17:21:50 -050085static inline struct sysfs_dirent *
Tejun Heo93b2b8e2013-11-28 14:54:15 -050086kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
87 const void *ns)
88{ return ERR_PTR(-ENOSYS); }
89
90static inline struct sysfs_dirent *
Tejun Heo496f7392013-11-28 14:54:24 -050091kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
92 umode_t mode, loff_t size, const struct kernfs_ops *ops,
93 void *priv, const void *ns)
94{ return ERR_PTR(-ENOSYS); }
95
96static inline struct sysfs_dirent *
Tejun Heo5d0e26b2013-11-23 17:21:50 -050097kernfs_create_link(struct sysfs_dirent *parent, const char *name,
98 struct sysfs_dirent *target)
99{ return ERR_PTR(-ENOSYS); }
100
Tejun Heo879f40d2013-11-23 17:21:49 -0500101static inline void kernfs_remove(struct sysfs_dirent *sd) { }
102
103static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
104 const char *name, const void *ns)
105{ return -ENOSYS; }
106
Tejun Heo890ece12013-11-23 17:21:51 -0500107static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
108 struct sysfs_dirent *new_parent,
109 const char *new_name, const void *new_ns)
110{ return -ENOSYS; }
111
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500112static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
113
Tejun Heo5d604182013-11-23 17:21:52 -0500114static inline int kernfs_setattr(struct sysfs_dirent *sd,
115 const struct iattr *iattr)
116{ return -ENOSYS; }
117
Tejun Heo879f40d2013-11-23 17:21:49 -0500118#endif /* CONFIG_SYSFS */
119
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500120static inline struct sysfs_dirent *
121kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
122{
123 return kernfs_create_dir_ns(parent, name, priv, NULL);
124}
125
Tejun Heo496f7392013-11-28 14:54:24 -0500126static inline struct sysfs_dirent *
127kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
128 loff_t size, const struct kernfs_ops *ops, void *priv)
129{
130 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
131}
132
Tejun Heo879f40d2013-11-23 17:21:49 -0500133static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
134 const char *name)
135{
136 return kernfs_remove_by_name_ns(parent, name, NULL);
137}
138
Tejun Heob8441ed2013-11-24 09:54:58 -0500139#endif /* __LINUX_KERNFS_H */