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>
20 struct_erofs_mount_opts
,
24 struct attribute attr
;
26 int struct_type
, offset
;
29 #define EROFS_ATTR(_name, _mode, _id) \
30 static struct erofs_attr erofs_attr_##_name = { \
31 .attr = {.name = __stringify(_name), .mode = _mode }, \
32 .attr_id = attr_##_id, \
34 #define EROFS_ATTR_FUNC(_name, _mode) EROFS_ATTR(_name, _mode, _name)
35 #define EROFS_ATTR_FEATURE(_name) EROFS_ATTR(_name, 0444, feature)
37 #define EROFS_ATTR_OFFSET(_name, _mode, _id, _struct) \
38 static 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),\
45 #define EROFS_ATTR_RW(_name, _id, _struct) \
46 EROFS_ATTR_OFFSET(_name, 0644, _id, _struct)
48 #define EROFS_RO_ATTR(_name, _id, _struct) \
49 EROFS_ATTR_OFFSET(_name, 0444, _id, _struct)
51 #define EROFS_ATTR_RW_UI(_name, _struct) \
52 EROFS_ATTR_RW(_name, pointer_ui, _struct)
54 #define EROFS_ATTR_RW_BOOL(_name, _struct) \
55 EROFS_ATTR_RW(_name, pointer_bool, _struct)
57 #define ATTR_LIST(name) (&erofs_attr_##name.attr)
59 #ifdef CONFIG_EROFS_FS_ZIP
60 EROFS_ATTR_RW_UI(sync_decompress
, erofs_mount_opts
);
61 EROFS_ATTR_FUNC(drop_caches
, 0200);
64 static struct attribute
*erofs_attrs
[] = {
65 #ifdef CONFIG_EROFS_FS_ZIP
66 ATTR_LIST(sync_decompress
),
67 ATTR_LIST(drop_caches
),
71 ATTRIBUTE_GROUPS(erofs
);
73 /* Features this copy of erofs supports */
74 EROFS_ATTR_FEATURE(zero_padding
);
75 EROFS_ATTR_FEATURE(compr_cfgs
);
76 EROFS_ATTR_FEATURE(big_pcluster
);
77 EROFS_ATTR_FEATURE(chunked_file
);
78 EROFS_ATTR_FEATURE(device_table
);
79 EROFS_ATTR_FEATURE(compr_head2
);
80 EROFS_ATTR_FEATURE(sb_chksum
);
81 EROFS_ATTR_FEATURE(ztailpacking
);
82 EROFS_ATTR_FEATURE(fragments
);
83 EROFS_ATTR_FEATURE(dedupe
);
85 static 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
),
93 ATTR_LIST(ztailpacking
),
98 ATTRIBUTE_GROUPS(erofs_feat
);
100 static unsigned char *__struct_ptr(struct erofs_sb_info
*sbi
,
101 int struct_type
, int offset
)
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
;
110 static ssize_t
erofs_attr_show(struct kobject
*kobj
,
111 struct attribute
*attr
, char *buf
)
113 struct erofs_sb_info
*sbi
= container_of(kobj
, struct erofs_sb_info
,
115 struct erofs_attr
*a
= container_of(attr
, struct erofs_attr
, attr
);
116 unsigned char *ptr
= __struct_ptr(sbi
, a
->struct_type
, a
->offset
);
118 switch (a
->attr_id
) {
120 return sysfs_emit(buf
, "supported\n");
121 case attr_pointer_ui
:
124 return sysfs_emit(buf
, "%u\n", *(unsigned int *)ptr
);
125 case attr_pointer_bool
:
128 return sysfs_emit(buf
, "%d\n", *(bool *)ptr
);
133 static ssize_t
erofs_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
134 const char *buf
, size_t len
)
136 struct erofs_sb_info
*sbi
= container_of(kobj
, struct erofs_sb_info
,
138 struct erofs_attr
*a
= container_of(attr
, struct erofs_attr
, attr
);
139 unsigned char *ptr
= __struct_ptr(sbi
, a
->struct_type
, a
->offset
);
143 switch (a
->attr_id
) {
144 case attr_pointer_ui
:
147 ret
= kstrtoul(skip_spaces(buf
), 0, &t
);
150 if (t
!= (unsigned int)t
)
152 #ifdef CONFIG_EROFS_FS_ZIP
153 if (!strcmp(a
->attr
.name
, "sync_decompress") &&
154 (t
> EROFS_SYNC_DECOMPRESS_FORCE_OFF
))
157 *(unsigned int *)ptr
= t
;
159 case attr_pointer_bool
:
162 ret
= kstrtoul(skip_spaces(buf
), 0, &t
);
165 if (t
!= 0 && t
!= 1)
169 #ifdef CONFIG_EROFS_FS_ZIP
170 case attr_drop_caches
:
171 ret
= kstrtoul(skip_spaces(buf
), 0, &t
);
178 z_erofs_shrink_scan(sbi
, ~0UL);
180 invalidate_mapping_pages(MNGD_MAPPING(sbi
), 0, -1);
187 static void erofs_sb_release(struct kobject
*kobj
)
189 struct erofs_sb_info
*sbi
= container_of(kobj
, struct erofs_sb_info
,
191 complete(&sbi
->s_kobj_unregister
);
194 static const struct sysfs_ops erofs_attr_ops
= {
195 .show
= erofs_attr_show
,
196 .store
= erofs_attr_store
,
199 static const struct kobj_type erofs_sb_ktype
= {
200 .default_groups
= erofs_groups
,
201 .sysfs_ops
= &erofs_attr_ops
,
202 .release
= erofs_sb_release
,
205 static const struct kobj_type erofs_ktype
= {
206 .sysfs_ops
= &erofs_attr_ops
,
209 static struct kset erofs_root
= {
210 .kobj
= {.ktype
= &erofs_ktype
},
213 static const struct kobj_type erofs_feat_ktype
= {
214 .default_groups
= erofs_feat_groups
,
215 .sysfs_ops
= &erofs_attr_ops
,
218 static struct kobject erofs_feat
= {
222 int erofs_register_sysfs(struct super_block
*sb
)
224 struct erofs_sb_info
*sbi
= EROFS_SB(sb
);
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",
232 kobject_put(&sbi
->s_kobj
);
233 wait_for_completion(&sbi
->s_kobj_unregister
);
238 void erofs_unregister_sysfs(struct super_block
*sb
)
240 struct erofs_sb_info
*sbi
= EROFS_SB(sb
);
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
);
249 int __init
erofs_init_sysfs(void)
253 kobject_set_name(&erofs_root
.kobj
, "erofs");
254 erofs_root
.kobj
.parent
= fs_kobj
;
255 ret
= kset_register(&erofs_root
);
259 ret
= kobject_init_and_add(&erofs_feat
, &erofs_feat_ktype
,
266 kobject_put(&erofs_feat
);
267 kset_unregister(&erofs_root
);
272 void erofs_exit_sysfs(void)
274 kobject_put(&erofs_feat
);
275 kset_unregister(&erofs_root
);