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);
79 EROFS_ATTR_FEATURE(fragments);
80 EROFS_ATTR_FEATURE(dedupe);
82 static 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),
90 ATTR_LIST(ztailpacking),
95 ATTRIBUTE_GROUPS(erofs_feat);
97 static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
98 int struct_type, int offset)
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;
107 static ssize_t erofs_attr_show(struct kobject *kobj,
108 struct attribute *attr, char *buf)
110 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
112 struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
113 unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
115 switch (a->attr_id) {
117 return sysfs_emit(buf, "supported\n");
118 case attr_pointer_ui:
121 return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
122 case attr_pointer_bool:
125 return sysfs_emit(buf, "%d\n", *(bool *)ptr);
130 static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
131 const char *buf, size_t len)
133 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
135 struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
136 unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
140 switch (a->attr_id) {
141 case attr_pointer_ui:
144 ret = kstrtoul(skip_spaces(buf), 0, &t);
147 if (t != (unsigned int)t)
149 #ifdef CONFIG_EROFS_FS_ZIP
150 if (!strcmp(a->attr.name, "sync_decompress") &&
151 (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
154 *(unsigned int *)ptr = t;
156 case attr_pointer_bool:
159 ret = kstrtoul(skip_spaces(buf), 0, &t);
162 if (t != 0 && t != 1)
170 static void erofs_sb_release(struct kobject *kobj)
172 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
174 complete(&sbi->s_kobj_unregister);
177 static const struct sysfs_ops erofs_attr_ops = {
178 .show = erofs_attr_show,
179 .store = erofs_attr_store,
182 static const struct kobj_type erofs_sb_ktype = {
183 .default_groups = erofs_groups,
184 .sysfs_ops = &erofs_attr_ops,
185 .release = erofs_sb_release,
188 static const struct kobj_type erofs_ktype = {
189 .sysfs_ops = &erofs_attr_ops,
192 static struct kset erofs_root = {
193 .kobj = {.ktype = &erofs_ktype},
196 static const struct kobj_type erofs_feat_ktype = {
197 .default_groups = erofs_feat_groups,
198 .sysfs_ops = &erofs_attr_ops,
201 static struct kobject erofs_feat = {
205 int erofs_register_sysfs(struct super_block *sb)
207 struct erofs_sb_info *sbi = EROFS_SB(sb);
212 if (erofs_is_fscache_mode(sb)) {
213 if (sbi->domain_id) {
214 str = kasprintf(GFP_KERNEL, "%s,%s", sbi->domain_id,
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);
234 kobject_put(&sbi->s_kobj);
235 wait_for_completion(&sbi->s_kobj_unregister);
239 void erofs_unregister_sysfs(struct super_block *sb)
241 struct erofs_sb_info *sbi = EROFS_SB(sb);
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);
250 int __init erofs_init_sysfs(void)
254 kobject_set_name(&erofs_root.kobj, "erofs");
255 erofs_root.kobj.parent = fs_kobj;
256 ret = kset_register(&erofs_root);
260 ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
267 kobject_put(&erofs_feat);
268 kset_unregister(&erofs_root);
273 void erofs_exit_sysfs(void)
275 kobject_put(&erofs_feat);
276 kset_unregister(&erofs_root);