xref: /linux/fs/erofs/sysfs.c (revision 25489a4f556414445d342951615178368ee45cde)
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 #include "compress.h"
11 
12 enum {
13 	attr_feature,
14 	attr_drop_caches,
15 	attr_pointer_ui,
16 	attr_pointer_bool,
17 	attr_accel,
18 };
19 
20 enum {
21 	struct_erofs_sb_info,
22 	struct_erofs_mount_opts,
23 };
24 
25 struct erofs_attr {
26 	struct attribute attr;
27 	short attr_id;
28 	int struct_type, offset;
29 };
30 
31 #define EROFS_ATTR(_name, _mode, _id)					\
32 static struct erofs_attr erofs_attr_##_name = {				\
33 	.attr = {.name = __stringify(_name), .mode = _mode },		\
34 	.attr_id = attr_##_id,						\
35 }
36 #define EROFS_ATTR_FUNC(_name, _mode)	EROFS_ATTR(_name, _mode, _name)
37 #define EROFS_ATTR_FEATURE(_name)	EROFS_ATTR(_name, 0444, feature)
38 
39 #define EROFS_ATTR_OFFSET(_name, _mode, _id, _struct)	\
40 static struct erofs_attr erofs_attr_##_name = {			\
41 	.attr = {.name = __stringify(_name), .mode = _mode },	\
42 	.attr_id = attr_##_id,					\
43 	.struct_type = struct_##_struct,			\
44 	.offset = offsetof(struct _struct, _name),\
45 }
46 
47 #define EROFS_ATTR_RW(_name, _id, _struct)	\
48 	EROFS_ATTR_OFFSET(_name, 0644, _id, _struct)
49 
50 #define EROFS_RO_ATTR(_name, _id, _struct)	\
51 	EROFS_ATTR_OFFSET(_name, 0444, _id, _struct)
52 
53 #define EROFS_ATTR_RW_UI(_name, _struct)	\
54 	EROFS_ATTR_RW(_name, pointer_ui, _struct)
55 
56 #define EROFS_ATTR_RW_BOOL(_name, _struct)	\
57 	EROFS_ATTR_RW(_name, pointer_bool, _struct)
58 
59 #define ATTR_LIST(name) (&erofs_attr_##name.attr)
60 
61 #ifdef CONFIG_EROFS_FS_ZIP
62 EROFS_ATTR_RW_UI(sync_decompress, erofs_mount_opts);
63 EROFS_ATTR_FUNC(drop_caches, 0200);
64 #endif
65 #ifdef CONFIG_EROFS_FS_ZIP_ACCEL
66 EROFS_ATTR_FUNC(accel, 0644);
67 #endif
68 
69 static struct attribute *erofs_sb_attrs[] = {
70 #ifdef CONFIG_EROFS_FS_ZIP
71 	ATTR_LIST(sync_decompress),
72 	ATTR_LIST(drop_caches),
73 #endif
74 	NULL,
75 };
76 ATTRIBUTE_GROUPS(erofs_sb);
77 
78 static struct attribute *erofs_attrs[] = {
79 #ifdef CONFIG_EROFS_FS_ZIP_ACCEL
80 	ATTR_LIST(accel),
81 #endif
82 	NULL,
83 };
84 ATTRIBUTE_GROUPS(erofs);
85 
86 /* Features this copy of erofs supports */
87 EROFS_ATTR_FEATURE(zero_padding);
88 EROFS_ATTR_FEATURE(compr_cfgs);
89 EROFS_ATTR_FEATURE(big_pcluster);
90 EROFS_ATTR_FEATURE(chunked_file);
91 EROFS_ATTR_FEATURE(device_table);
92 EROFS_ATTR_FEATURE(compr_head2);
93 EROFS_ATTR_FEATURE(sb_chksum);
94 EROFS_ATTR_FEATURE(ztailpacking);
95 EROFS_ATTR_FEATURE(fragments);
96 EROFS_ATTR_FEATURE(dedupe);
97 EROFS_ATTR_FEATURE(48bit);
98 
99 static struct attribute *erofs_feat_attrs[] = {
100 	ATTR_LIST(zero_padding),
101 	ATTR_LIST(compr_cfgs),
102 	ATTR_LIST(big_pcluster),
103 	ATTR_LIST(chunked_file),
104 	ATTR_LIST(device_table),
105 	ATTR_LIST(compr_head2),
106 	ATTR_LIST(sb_chksum),
107 	ATTR_LIST(ztailpacking),
108 	ATTR_LIST(fragments),
109 	ATTR_LIST(dedupe),
110 	ATTR_LIST(48bit),
111 	NULL,
112 };
113 ATTRIBUTE_GROUPS(erofs_feat);
114 
115 static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
116 					  int struct_type, int offset)
117 {
118 	if (struct_type == struct_erofs_sb_info)
119 		return (unsigned char *)sbi + offset;
120 	if (struct_type == struct_erofs_mount_opts)
121 		return (unsigned char *)&sbi->opt + offset;
122 	return NULL;
123 }
124 
125 static ssize_t erofs_attr_show(struct kobject *kobj,
126 				struct attribute *attr, char *buf)
127 {
128 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
129 						s_kobj);
130 	struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
131 	unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
132 
133 	switch (a->attr_id) {
134 	case attr_feature:
135 		return sysfs_emit(buf, "supported\n");
136 	case attr_pointer_ui:
137 		if (!ptr)
138 			return 0;
139 		return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
140 	case attr_pointer_bool:
141 		if (!ptr)
142 			return 0;
143 		return sysfs_emit(buf, "%d\n", *(bool *)ptr);
144 	case attr_accel:
145 		return z_erofs_crypto_show_engines(buf, PAGE_SIZE, '\n');
146 	}
147 	return 0;
148 }
149 
150 static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
151 				const char *buf, size_t len)
152 {
153 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
154 						s_kobj);
155 	struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
156 	unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
157 	unsigned long t;
158 	int ret;
159 
160 	switch (a->attr_id) {
161 	case attr_pointer_ui:
162 		if (!ptr)
163 			return 0;
164 		ret = kstrtoul(skip_spaces(buf), 0, &t);
165 		if (ret)
166 			return ret;
167 		if (t != (unsigned int)t)
168 			return -ERANGE;
169 #ifdef CONFIG_EROFS_FS_ZIP
170 		if (!strcmp(a->attr.name, "sync_decompress") &&
171 		    (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
172 			return -EINVAL;
173 #endif
174 		*(unsigned int *)ptr = t;
175 		return len;
176 	case attr_pointer_bool:
177 		if (!ptr)
178 			return 0;
179 		ret = kstrtoul(skip_spaces(buf), 0, &t);
180 		if (ret)
181 			return ret;
182 		if (t != 0 && t != 1)
183 			return -EINVAL;
184 		*(bool *)ptr = !!t;
185 		return len;
186 #ifdef CONFIG_EROFS_FS_ZIP
187 	case attr_drop_caches:
188 		ret = kstrtoul(skip_spaces(buf), 0, &t);
189 		if (ret)
190 			return ret;
191 		if (t < 1 || t > 3)
192 			return -EINVAL;
193 
194 		if (t & 2)
195 			z_erofs_shrink_scan(sbi, ~0UL);
196 		if (t & 1)
197 			invalidate_mapping_pages(MNGD_MAPPING(sbi), 0, -1);
198 		return len;
199 #endif
200 #ifdef CONFIG_EROFS_FS_ZIP_ACCEL
201 	case attr_accel:
202 		buf = skip_spaces(buf);
203 		z_erofs_crypto_disable_all_engines();
204 		while (*buf) {
205 			t = strcspn(buf, "\n");
206 			ret = z_erofs_crypto_enable_engine(buf, t);
207 			if (ret < 0)
208 				return ret;
209 			buf += buf[t] != '\0' ? t + 1 : t;
210 		}
211 		return len;
212 #endif
213 	}
214 	return 0;
215 }
216 
217 static void erofs_sb_release(struct kobject *kobj)
218 {
219 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
220 						 s_kobj);
221 	complete(&sbi->s_kobj_unregister);
222 }
223 
224 static const struct sysfs_ops erofs_attr_ops = {
225 	.show	= erofs_attr_show,
226 	.store	= erofs_attr_store,
227 };
228 
229 static const struct kobj_type erofs_sb_ktype = {
230 	.default_groups = erofs_sb_groups,
231 	.sysfs_ops	= &erofs_attr_ops,
232 	.release	= erofs_sb_release,
233 };
234 
235 static const struct kobj_type erofs_ktype = {
236 	.default_groups = erofs_groups,
237 	.sysfs_ops	= &erofs_attr_ops,
238 };
239 
240 static struct kset erofs_root = {
241 	.kobj	= {.ktype = &erofs_ktype},
242 };
243 
244 static const struct kobj_type erofs_feat_ktype = {
245 	.default_groups = erofs_feat_groups,
246 	.sysfs_ops	= &erofs_attr_ops,
247 };
248 
249 static struct kobject erofs_feat = {
250 	.kset	= &erofs_root,
251 };
252 
253 int erofs_register_sysfs(struct super_block *sb)
254 {
255 	struct erofs_sb_info *sbi = EROFS_SB(sb);
256 	int err;
257 
258 	sbi->s_kobj.kset = &erofs_root;
259 	init_completion(&sbi->s_kobj_unregister);
260 	err = kobject_init_and_add(&sbi->s_kobj, &erofs_sb_ktype, NULL, "%s",
261 				   sb->s_sysfs_name);
262 	if (err) {
263 		kobject_put(&sbi->s_kobj);
264 		wait_for_completion(&sbi->s_kobj_unregister);
265 	}
266 	return err;
267 }
268 
269 void erofs_unregister_sysfs(struct super_block *sb)
270 {
271 	struct erofs_sb_info *sbi = EROFS_SB(sb);
272 
273 	if (sbi->s_kobj.state_in_sysfs) {
274 		kobject_del(&sbi->s_kobj);
275 		kobject_put(&sbi->s_kobj);
276 		wait_for_completion(&sbi->s_kobj_unregister);
277 	}
278 }
279 
280 void erofs_exit_sysfs(void)
281 {
282 	kobject_put(&erofs_feat);
283 	kset_unregister(&erofs_root);
284 }
285 
286 int __init erofs_init_sysfs(void)
287 {
288 	int ret;
289 
290 	kobject_set_name(&erofs_root.kobj, "erofs");
291 	erofs_root.kobj.parent = fs_kobj;
292 	ret = kset_register(&erofs_root);
293 	if (!ret) {
294 		ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
295 					   NULL, "features");
296 		if (!ret)
297 			return 0;
298 		erofs_exit_sysfs();
299 	}
300 	return ret;
301 }
302