xref: /openbmc/linux/fs/erofs/sysfs.c (revision 40452ffca3c1a0f2994e826f9fa213b107f1a2d4)
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 
79 static struct attribute *erofs_feat_attrs[] = {
80 	ATTR_LIST(zero_padding),
81 	ATTR_LIST(compr_cfgs),
82 	ATTR_LIST(big_pcluster),
83 	ATTR_LIST(chunked_file),
84 	ATTR_LIST(device_table),
85 	ATTR_LIST(compr_head2),
86 	ATTR_LIST(sb_chksum),
87 	NULL,
88 };
89 ATTRIBUTE_GROUPS(erofs_feat);
90 
91 static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
92 					  int struct_type, int offset)
93 {
94 	if (struct_type == struct_erofs_sb_info)
95 		return (unsigned char *)sbi + offset;
96 	if (struct_type == struct_erofs_mount_opts)
97 		return (unsigned char *)&sbi->opt + offset;
98 	return NULL;
99 }
100 
101 static ssize_t erofs_attr_show(struct kobject *kobj,
102 				struct attribute *attr, char *buf)
103 {
104 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
105 						s_kobj);
106 	struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
107 	unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
108 
109 	switch (a->attr_id) {
110 	case attr_feature:
111 		return sysfs_emit(buf, "supported\n");
112 	case attr_pointer_ui:
113 		if (!ptr)
114 			return 0;
115 		return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
116 	case attr_pointer_bool:
117 		if (!ptr)
118 			return 0;
119 		return sysfs_emit(buf, "%d\n", *(bool *)ptr);
120 	}
121 	return 0;
122 }
123 
124 static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
125 						const char *buf, size_t len)
126 {
127 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
128 						s_kobj);
129 	struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
130 	unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
131 	unsigned long t;
132 	int ret;
133 
134 	switch (a->attr_id) {
135 	case attr_pointer_ui:
136 		if (!ptr)
137 			return 0;
138 		ret = kstrtoul(skip_spaces(buf), 0, &t);
139 		if (ret)
140 			return ret;
141 		if (t != (unsigned int)t)
142 			return -ERANGE;
143 #ifdef CONFIG_EROFS_FS_ZIP
144 		if (!strcmp(a->attr.name, "sync_decompress") &&
145 		    (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
146 			return -EINVAL;
147 #endif
148 		*(unsigned int *)ptr = t;
149 		return len;
150 	case attr_pointer_bool:
151 		if (!ptr)
152 			return 0;
153 		ret = kstrtoul(skip_spaces(buf), 0, &t);
154 		if (ret)
155 			return ret;
156 		if (t != 0 && t != 1)
157 			return -EINVAL;
158 		*(bool *)ptr = !!t;
159 		return len;
160 	}
161 	return 0;
162 }
163 
164 static void erofs_sb_release(struct kobject *kobj)
165 {
166 	struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
167 						 s_kobj);
168 	complete(&sbi->s_kobj_unregister);
169 }
170 
171 static const struct sysfs_ops erofs_attr_ops = {
172 	.show	= erofs_attr_show,
173 	.store	= erofs_attr_store,
174 };
175 
176 static struct kobj_type erofs_sb_ktype = {
177 	.default_groups = erofs_groups,
178 	.sysfs_ops	= &erofs_attr_ops,
179 	.release	= erofs_sb_release,
180 };
181 
182 static struct kobj_type erofs_ktype = {
183 	.sysfs_ops	= &erofs_attr_ops,
184 };
185 
186 static struct kset erofs_root = {
187 	.kobj	= {.ktype = &erofs_ktype},
188 };
189 
190 static struct kobj_type erofs_feat_ktype = {
191 	.default_groups = erofs_feat_groups,
192 	.sysfs_ops	= &erofs_attr_ops,
193 };
194 
195 static struct kobject erofs_feat = {
196 	.kset	= &erofs_root,
197 };
198 
199 int erofs_register_sysfs(struct super_block *sb)
200 {
201 	struct erofs_sb_info *sbi = EROFS_SB(sb);
202 	int err;
203 
204 	sbi->s_kobj.kset = &erofs_root;
205 	init_completion(&sbi->s_kobj_unregister);
206 	err = kobject_init_and_add(&sbi->s_kobj, &erofs_sb_ktype, NULL,
207 				   "%s", sb->s_id);
208 	if (err)
209 		goto put_sb_kobj;
210 	return 0;
211 
212 put_sb_kobj:
213 	kobject_put(&sbi->s_kobj);
214 	wait_for_completion(&sbi->s_kobj_unregister);
215 	return err;
216 }
217 
218 void erofs_unregister_sysfs(struct super_block *sb)
219 {
220 	struct erofs_sb_info *sbi = EROFS_SB(sb);
221 
222 	kobject_del(&sbi->s_kobj);
223 	kobject_put(&sbi->s_kobj);
224 	wait_for_completion(&sbi->s_kobj_unregister);
225 }
226 
227 int __init erofs_init_sysfs(void)
228 {
229 	int ret;
230 
231 	kobject_set_name(&erofs_root.kobj, "erofs");
232 	erofs_root.kobj.parent = fs_kobj;
233 	ret = kset_register(&erofs_root);
234 	if (ret)
235 		goto root_err;
236 
237 	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
238 				   NULL, "features");
239 	if (ret)
240 		goto feat_err;
241 	return ret;
242 
243 feat_err:
244 	kobject_put(&erofs_feat);
245 	kset_unregister(&erofs_root);
246 root_err:
247 	return ret;
248 }
249 
250 void erofs_exit_sysfs(void)
251 {
252 	kobject_put(&erofs_feat);
253 	kset_unregister(&erofs_root);
254 }
255