xref: /openbmc/linux/fs/erofs/sysfs.c (revision 7fc96d71)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C), 2008-2021, OPPO Mobile Comm Corp., Ltd.
4  *             https://www.oppo.com/
5  */
6 #include <linux/sysfs.h>
7 #include <linux/kobject.h>
8 
9 #include "internal.h"
10 
11 enum {
12 	attr_feature,
13 	attr_pointer_ui,
14 	attr_pointer_bool,
15 };
16 
17 enum {
18 	struct_erofs_sb_info,
19 	struct_erofs_mount_opts,
20 };
21 
22 struct erofs_attr {
23 	struct attribute attr;
24 	short attr_id;
25 	int struct_type, offset;
26 };
27 
28 #define EROFS_ATTR(_name, _mode, _id)					\
29 static struct erofs_attr erofs_attr_##_name = {				\
30 	.attr = {.name = __stringify(_name), .mode = _mode },		\
31 	.attr_id = attr_##_id,						\
32 }
33 #define EROFS_ATTR_FUNC(_name, _mode)	EROFS_ATTR(_name, _mode, _name)
34 #define EROFS_ATTR_FEATURE(_name)	EROFS_ATTR(_name, 0444, feature)
35 
36 #define EROFS_ATTR_OFFSET(_name, _mode, _id, _struct)	\
37 static struct erofs_attr erofs_attr_##_name = {			\
38 	.attr = {.name = __stringify(_name), .mode = _mode },	\
39 	.attr_id = attr_##_id,					\
40 	.struct_type = struct_##_struct,			\
41 	.offset = offsetof(struct _struct, _name),\
42 }
43 
44 #define EROFS_ATTR_RW(_name, _id, _struct)	\
45 	EROFS_ATTR_OFFSET(_name, 0644, _id, _struct)
46 
47 #define EROFS_RO_ATTR(_name, _id, _struct)	\
48 	EROFS_ATTR_OFFSET(_name, 0444, _id, _struct)
49 
50 #define EROFS_ATTR_RW_UI(_name, _struct)	\
51 	EROFS_ATTR_RW(_name, pointer_ui, _struct)
52 
53 #define EROFS_ATTR_RW_BOOL(_name, _struct)	\
54 	EROFS_ATTR_RW(_name, pointer_bool, _struct)
55 
56 #define ATTR_LIST(name) (&erofs_attr_##name.attr)
57 
58 #ifdef CONFIG_EROFS_FS_ZIP
59 EROFS_ATTR_RW_UI(sync_decompress, erofs_mount_opts);
60 #endif
61 
62 static struct attribute *erofs_attrs[] = {
63 #ifdef CONFIG_EROFS_FS_ZIP
64 	ATTR_LIST(sync_decompress),
65 #endif
66 	NULL,
67 };
68 ATTRIBUTE_GROUPS(erofs);
69 
70 /* Features this copy of erofs supports */
71 EROFS_ATTR_FEATURE(zero_padding);
72 EROFS_ATTR_FEATURE(compr_cfgs);
73 EROFS_ATTR_FEATURE(big_pcluster);
74 EROFS_ATTR_FEATURE(chunked_file);
75 EROFS_ATTR_FEATURE(device_table);
76 EROFS_ATTR_FEATURE(compr_head2);
77 EROFS_ATTR_FEATURE(sb_chksum);
78 EROFS_ATTR_FEATURE(ztailpacking);
79 
80 static struct attribute *erofs_feat_attrs[] = {
81 	ATTR_LIST(zero_padding),
82 	ATTR_LIST(compr_cfgs),
83 	ATTR_LIST(big_pcluster),
84 	ATTR_LIST(chunked_file),
85 	ATTR_LIST(device_table),
86 	ATTR_LIST(compr_head2),
87 	ATTR_LIST(sb_chksum),
88 	ATTR_LIST(ztailpacking),
89 	NULL,
90 };
91 ATTRIBUTE_GROUPS(erofs_feat);
92 
93 static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
94 					  int struct_type, int offset)
95 {
96 	if (struct_type == struct_erofs_sb_info)
97 		return (unsigned char *)sbi + offset;
98 	if (struct_type == struct_erofs_mount_opts)
99 		return (unsigned char *)&sbi->opt + offset;
100 	return NULL;
101 }
102 
103 static ssize_t erofs_attr_show(struct kobject *kobj,
104 				struct attribute *attr, char *buf)
105 {
106 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
107 						s_kobj);
108 	struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
109 	unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
110 
111 	switch (a->attr_id) {
112 	case attr_feature:
113 		return sysfs_emit(buf, "supported\n");
114 	case attr_pointer_ui:
115 		if (!ptr)
116 			return 0;
117 		return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
118 	case attr_pointer_bool:
119 		if (!ptr)
120 			return 0;
121 		return sysfs_emit(buf, "%d\n", *(bool *)ptr);
122 	}
123 	return 0;
124 }
125 
126 static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
127 						const char *buf, size_t len)
128 {
129 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
130 						s_kobj);
131 	struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
132 	unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
133 	unsigned long t;
134 	int ret;
135 
136 	switch (a->attr_id) {
137 	case attr_pointer_ui:
138 		if (!ptr)
139 			return 0;
140 		ret = kstrtoul(skip_spaces(buf), 0, &t);
141 		if (ret)
142 			return ret;
143 		if (t != (unsigned int)t)
144 			return -ERANGE;
145 #ifdef CONFIG_EROFS_FS_ZIP
146 		if (!strcmp(a->attr.name, "sync_decompress") &&
147 		    (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
148 			return -EINVAL;
149 #endif
150 		*(unsigned int *)ptr = t;
151 		return len;
152 	case attr_pointer_bool:
153 		if (!ptr)
154 			return 0;
155 		ret = kstrtoul(skip_spaces(buf), 0, &t);
156 		if (ret)
157 			return ret;
158 		if (t != 0 && t != 1)
159 			return -EINVAL;
160 		*(bool *)ptr = !!t;
161 		return len;
162 	}
163 	return 0;
164 }
165 
166 static void erofs_sb_release(struct kobject *kobj)
167 {
168 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
169 						 s_kobj);
170 	complete(&sbi->s_kobj_unregister);
171 }
172 
173 static const struct sysfs_ops erofs_attr_ops = {
174 	.show	= erofs_attr_show,
175 	.store	= erofs_attr_store,
176 };
177 
178 static struct kobj_type erofs_sb_ktype = {
179 	.default_groups = erofs_groups,
180 	.sysfs_ops	= &erofs_attr_ops,
181 	.release	= erofs_sb_release,
182 };
183 
184 static struct kobj_type erofs_ktype = {
185 	.sysfs_ops	= &erofs_attr_ops,
186 };
187 
188 static struct kset erofs_root = {
189 	.kobj	= {.ktype = &erofs_ktype},
190 };
191 
192 static struct kobj_type erofs_feat_ktype = {
193 	.default_groups = erofs_feat_groups,
194 	.sysfs_ops	= &erofs_attr_ops,
195 };
196 
197 static struct kobject erofs_feat = {
198 	.kset	= &erofs_root,
199 };
200 
201 int erofs_register_sysfs(struct super_block *sb)
202 {
203 	struct erofs_sb_info *sbi = EROFS_SB(sb);
204 	int err;
205 
206 	sbi->s_kobj.kset = &erofs_root;
207 	init_completion(&sbi->s_kobj_unregister);
208 	err = kobject_init_and_add(&sbi->s_kobj, &erofs_sb_ktype, NULL, "%s",
209 			erofs_is_fscache_mode(sb) ? sbi->opt.fsid : sb->s_id);
210 	if (err)
211 		goto put_sb_kobj;
212 	return 0;
213 
214 put_sb_kobj:
215 	kobject_put(&sbi->s_kobj);
216 	wait_for_completion(&sbi->s_kobj_unregister);
217 	return err;
218 }
219 
220 void erofs_unregister_sysfs(struct super_block *sb)
221 {
222 	struct erofs_sb_info *sbi = EROFS_SB(sb);
223 
224 	if (sbi->s_kobj.state_in_sysfs) {
225 		kobject_del(&sbi->s_kobj);
226 		kobject_put(&sbi->s_kobj);
227 		wait_for_completion(&sbi->s_kobj_unregister);
228 	}
229 }
230 
231 int __init erofs_init_sysfs(void)
232 {
233 	int ret;
234 
235 	kobject_set_name(&erofs_root.kobj, "erofs");
236 	erofs_root.kobj.parent = fs_kobj;
237 	ret = kset_register(&erofs_root);
238 	if (ret)
239 		goto root_err;
240 
241 	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
242 				   NULL, "features");
243 	if (ret)
244 		goto feat_err;
245 	return ret;
246 
247 feat_err:
248 	kobject_put(&erofs_feat);
249 	kset_unregister(&erofs_root);
250 root_err:
251 	return ret;
252 }
253 
254 void erofs_exit_sysfs(void)
255 {
256 	kobject_put(&erofs_feat);
257 	kset_unregister(&erofs_root);
258 }
259