Linux Audio

Check our new training course

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