1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C), 2008-2021, OPPO Mobile Comm Corp., Ltd.
4 * https://www.oppo.com/
6 #include <linux/sysfs.h>
7 #include <linux/kobject.h>
19 struct_erofs_mount_opts,
23 struct attribute attr;
25 int struct_type, offset;
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, \
33 #define EROFS_ATTR_FUNC(_name, _mode) EROFS_ATTR(_name, _mode, _name)
34 #define EROFS_ATTR_FEATURE(_name) EROFS_ATTR(_name, 0444, feature)
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),\
44 #define EROFS_ATTR_RW(_name, _id, _struct) \
45 EROFS_ATTR_OFFSET(_name, 0644, _id, _struct)
47 #define EROFS_RO_ATTR(_name, _id, _struct) \
48 EROFS_ATTR_OFFSET(_name, 0444, _id, _struct)
50 #define EROFS_ATTR_RW_UI(_name, _struct) \
51 EROFS_ATTR_RW(_name, pointer_ui, _struct)
53 #define EROFS_ATTR_RW_BOOL(_name, _struct) \
54 EROFS_ATTR_RW(_name, pointer_bool, _struct)
56 #define ATTR_LIST(name) (&erofs_attr_##name.attr)
58 #ifdef CONFIG_EROFS_FS_ZIP
59 EROFS_ATTR_RW_UI(sync_decompress, erofs_mount_opts);
62 static struct attribute *erofs_attrs[] = {
63 #ifdef CONFIG_EROFS_FS_ZIP
64 ATTR_LIST(sync_decompress),
68 ATTRIBUTE_GROUPS(erofs);
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);
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),
88 ATTR_LIST(ztailpacking),
91 ATTRIBUTE_GROUPS(erofs_feat);
93 static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
94 int struct_type, int offset)
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;
103 static ssize_t erofs_attr_show(struct kobject *kobj,
104 struct attribute *attr, char *buf)
106 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
108 struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
109 unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
111 switch (a->attr_id) {
113 return sysfs_emit(buf, "supported\n");
114 case attr_pointer_ui:
117 return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
118 case attr_pointer_bool:
121 return sysfs_emit(buf, "%d\n", *(bool *)ptr);
126 static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
127 const char *buf, size_t len)
129 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
131 struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
132 unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
136 switch (a->attr_id) {
137 case attr_pointer_ui:
140 ret = kstrtoul(skip_spaces(buf), 0, &t);
143 if (t != (unsigned int)t)
145 #ifdef CONFIG_EROFS_FS_ZIP
146 if (!strcmp(a->attr.name, "sync_decompress") &&
147 (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
150 *(unsigned int *)ptr = t;
152 case attr_pointer_bool:
155 ret = kstrtoul(skip_spaces(buf), 0, &t);
158 if (t != 0 && t != 1)
166 static void erofs_sb_release(struct kobject *kobj)
168 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
170 complete(&sbi->s_kobj_unregister);
173 static const struct sysfs_ops erofs_attr_ops = {
174 .show = erofs_attr_show,
175 .store = erofs_attr_store,
178 static struct kobj_type erofs_sb_ktype = {
179 .default_groups = erofs_groups,
180 .sysfs_ops = &erofs_attr_ops,
181 .release = erofs_sb_release,
184 static struct kobj_type erofs_ktype = {
185 .sysfs_ops = &erofs_attr_ops,
188 static struct kset erofs_root = {
189 .kobj = {.ktype = &erofs_ktype},
192 static struct kobj_type erofs_feat_ktype = {
193 .default_groups = erofs_feat_groups,
194 .sysfs_ops = &erofs_attr_ops,
197 static struct kobject erofs_feat = {
201 int erofs_register_sysfs(struct super_block *sb)
203 struct erofs_sb_info *sbi = EROFS_SB(sb);
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,
215 kobject_put(&sbi->s_kobj);
216 wait_for_completion(&sbi->s_kobj_unregister);
220 void erofs_unregister_sysfs(struct super_block *sb)
222 struct erofs_sb_info *sbi = EROFS_SB(sb);
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);
231 int __init erofs_init_sysfs(void)
235 kobject_set_name(&erofs_root.kobj, "erofs");
236 erofs_root.kobj.parent = fs_kobj;
237 ret = kset_register(&erofs_root);
241 ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
248 kobject_put(&erofs_feat);
249 kset_unregister(&erofs_root);
254 void erofs_exit_sysfs(void)
256 kobject_put(&erofs_feat);
257 kset_unregister(&erofs_root);