1168e9a76SHuang Jianan // SPDX-License-Identifier: GPL-2.0-or-later
2168e9a76SHuang Jianan /*
3168e9a76SHuang Jianan * Copyright (C), 2008-2021, OPPO Mobile Comm Corp., Ltd.
4168e9a76SHuang Jianan * https://www.oppo.com/
5168e9a76SHuang Jianan */
6168e9a76SHuang Jianan #include <linux/sysfs.h>
7168e9a76SHuang Jianan #include <linux/kobject.h>
8168e9a76SHuang Jianan
9168e9a76SHuang Jianan #include "internal.h"
10168e9a76SHuang Jianan
11168e9a76SHuang Jianan enum {
12168e9a76SHuang Jianan attr_feature,
13168e9a76SHuang Jianan attr_pointer_ui,
14168e9a76SHuang Jianan attr_pointer_bool,
15168e9a76SHuang Jianan };
16168e9a76SHuang Jianan
17168e9a76SHuang Jianan enum {
18168e9a76SHuang Jianan struct_erofs_sb_info,
1940452ffcSHuang Jianan struct_erofs_mount_opts,
20168e9a76SHuang Jianan };
21168e9a76SHuang Jianan
22168e9a76SHuang Jianan struct erofs_attr {
23168e9a76SHuang Jianan struct attribute attr;
24168e9a76SHuang Jianan short attr_id;
25168e9a76SHuang Jianan int struct_type, offset;
26168e9a76SHuang Jianan };
27168e9a76SHuang Jianan
28168e9a76SHuang Jianan #define EROFS_ATTR(_name, _mode, _id) \
29168e9a76SHuang Jianan static struct erofs_attr erofs_attr_##_name = { \
30168e9a76SHuang Jianan .attr = {.name = __stringify(_name), .mode = _mode }, \
31168e9a76SHuang Jianan .attr_id = attr_##_id, \
32168e9a76SHuang Jianan }
33168e9a76SHuang Jianan #define EROFS_ATTR_FUNC(_name, _mode) EROFS_ATTR(_name, _mode, _name)
34168e9a76SHuang Jianan #define EROFS_ATTR_FEATURE(_name) EROFS_ATTR(_name, 0444, feature)
35168e9a76SHuang Jianan
36168e9a76SHuang Jianan #define EROFS_ATTR_OFFSET(_name, _mode, _id, _struct) \
37168e9a76SHuang Jianan static struct erofs_attr erofs_attr_##_name = { \
38168e9a76SHuang Jianan .attr = {.name = __stringify(_name), .mode = _mode }, \
39168e9a76SHuang Jianan .attr_id = attr_##_id, \
40168e9a76SHuang Jianan .struct_type = struct_##_struct, \
41168e9a76SHuang Jianan .offset = offsetof(struct _struct, _name),\
42168e9a76SHuang Jianan }
43168e9a76SHuang Jianan
44168e9a76SHuang Jianan #define EROFS_ATTR_RW(_name, _id, _struct) \
45168e9a76SHuang Jianan EROFS_ATTR_OFFSET(_name, 0644, _id, _struct)
46168e9a76SHuang Jianan
47168e9a76SHuang Jianan #define EROFS_RO_ATTR(_name, _id, _struct) \
48168e9a76SHuang Jianan EROFS_ATTR_OFFSET(_name, 0444, _id, _struct)
49168e9a76SHuang Jianan
50168e9a76SHuang Jianan #define EROFS_ATTR_RW_UI(_name, _struct) \
51168e9a76SHuang Jianan EROFS_ATTR_RW(_name, pointer_ui, _struct)
52168e9a76SHuang Jianan
53168e9a76SHuang Jianan #define EROFS_ATTR_RW_BOOL(_name, _struct) \
54168e9a76SHuang Jianan EROFS_ATTR_RW(_name, pointer_bool, _struct)
55168e9a76SHuang Jianan
56168e9a76SHuang Jianan #define ATTR_LIST(name) (&erofs_attr_##name.attr)
57168e9a76SHuang Jianan
5840452ffcSHuang Jianan #ifdef CONFIG_EROFS_FS_ZIP
5940452ffcSHuang Jianan EROFS_ATTR_RW_UI(sync_decompress, erofs_mount_opts);
6040452ffcSHuang Jianan #endif
6140452ffcSHuang Jianan
62168e9a76SHuang Jianan static struct attribute *erofs_attrs[] = {
6340452ffcSHuang Jianan #ifdef CONFIG_EROFS_FS_ZIP
6440452ffcSHuang Jianan ATTR_LIST(sync_decompress),
6540452ffcSHuang Jianan #endif
66168e9a76SHuang Jianan NULL,
67168e9a76SHuang Jianan };
68168e9a76SHuang Jianan ATTRIBUTE_GROUPS(erofs);
69168e9a76SHuang Jianan
70168e9a76SHuang Jianan /* Features this copy of erofs supports */
71168e9a76SHuang Jianan EROFS_ATTR_FEATURE(zero_padding);
72168e9a76SHuang Jianan EROFS_ATTR_FEATURE(compr_cfgs);
73168e9a76SHuang Jianan EROFS_ATTR_FEATURE(big_pcluster);
74168e9a76SHuang Jianan EROFS_ATTR_FEATURE(chunked_file);
75168e9a76SHuang Jianan EROFS_ATTR_FEATURE(device_table);
76168e9a76SHuang Jianan EROFS_ATTR_FEATURE(compr_head2);
77168e9a76SHuang Jianan EROFS_ATTR_FEATURE(sb_chksum);
78ab92184fSYue Hu EROFS_ATTR_FEATURE(ztailpacking);
79b15b2e30SYue Hu EROFS_ATTR_FEATURE(fragments);
805c2a6425SGao Xiang EROFS_ATTR_FEATURE(dedupe);
81168e9a76SHuang Jianan
82168e9a76SHuang Jianan static struct attribute *erofs_feat_attrs[] = {
83168e9a76SHuang Jianan ATTR_LIST(zero_padding),
84168e9a76SHuang Jianan ATTR_LIST(compr_cfgs),
85168e9a76SHuang Jianan ATTR_LIST(big_pcluster),
86168e9a76SHuang Jianan ATTR_LIST(chunked_file),
87168e9a76SHuang Jianan ATTR_LIST(device_table),
88168e9a76SHuang Jianan ATTR_LIST(compr_head2),
89168e9a76SHuang Jianan ATTR_LIST(sb_chksum),
90ab92184fSYue Hu ATTR_LIST(ztailpacking),
91b15b2e30SYue Hu ATTR_LIST(fragments),
925c2a6425SGao Xiang ATTR_LIST(dedupe),
93168e9a76SHuang Jianan NULL,
94168e9a76SHuang Jianan };
95168e9a76SHuang Jianan ATTRIBUTE_GROUPS(erofs_feat);
96168e9a76SHuang Jianan
__struct_ptr(struct erofs_sb_info * sbi,int struct_type,int offset)97168e9a76SHuang Jianan static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
98168e9a76SHuang Jianan int struct_type, int offset)
99168e9a76SHuang Jianan {
100168e9a76SHuang Jianan if (struct_type == struct_erofs_sb_info)
101168e9a76SHuang Jianan return (unsigned char *)sbi + offset;
10240452ffcSHuang Jianan if (struct_type == struct_erofs_mount_opts)
10340452ffcSHuang Jianan return (unsigned char *)&sbi->opt + offset;
104168e9a76SHuang Jianan return NULL;
105168e9a76SHuang Jianan }
106168e9a76SHuang Jianan
erofs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)107168e9a76SHuang Jianan static ssize_t erofs_attr_show(struct kobject *kobj,
108168e9a76SHuang Jianan struct attribute *attr, char *buf)
109168e9a76SHuang Jianan {
110168e9a76SHuang Jianan struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
111168e9a76SHuang Jianan s_kobj);
112168e9a76SHuang Jianan struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
113168e9a76SHuang Jianan unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
114168e9a76SHuang Jianan
115168e9a76SHuang Jianan switch (a->attr_id) {
116168e9a76SHuang Jianan case attr_feature:
117168e9a76SHuang Jianan return sysfs_emit(buf, "supported\n");
118168e9a76SHuang Jianan case attr_pointer_ui:
119168e9a76SHuang Jianan if (!ptr)
120168e9a76SHuang Jianan return 0;
121168e9a76SHuang Jianan return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
122168e9a76SHuang Jianan case attr_pointer_bool:
123168e9a76SHuang Jianan if (!ptr)
124168e9a76SHuang Jianan return 0;
125168e9a76SHuang Jianan return sysfs_emit(buf, "%d\n", *(bool *)ptr);
126168e9a76SHuang Jianan }
127168e9a76SHuang Jianan return 0;
128168e9a76SHuang Jianan }
129168e9a76SHuang Jianan
erofs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)130168e9a76SHuang Jianan static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
131168e9a76SHuang Jianan const char *buf, size_t len)
132168e9a76SHuang Jianan {
133168e9a76SHuang Jianan struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
134168e9a76SHuang Jianan s_kobj);
135168e9a76SHuang Jianan struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
136168e9a76SHuang Jianan unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
137168e9a76SHuang Jianan unsigned long t;
138168e9a76SHuang Jianan int ret;
139168e9a76SHuang Jianan
140168e9a76SHuang Jianan switch (a->attr_id) {
141168e9a76SHuang Jianan case attr_pointer_ui:
142168e9a76SHuang Jianan if (!ptr)
143168e9a76SHuang Jianan return 0;
144168e9a76SHuang Jianan ret = kstrtoul(skip_spaces(buf), 0, &t);
145168e9a76SHuang Jianan if (ret)
146168e9a76SHuang Jianan return ret;
147168e9a76SHuang Jianan if (t != (unsigned int)t)
148168e9a76SHuang Jianan return -ERANGE;
14940452ffcSHuang Jianan #ifdef CONFIG_EROFS_FS_ZIP
15040452ffcSHuang Jianan if (!strcmp(a->attr.name, "sync_decompress") &&
15140452ffcSHuang Jianan (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
15240452ffcSHuang Jianan return -EINVAL;
15340452ffcSHuang Jianan #endif
154168e9a76SHuang Jianan *(unsigned int *)ptr = t;
155168e9a76SHuang Jianan return len;
156168e9a76SHuang Jianan case attr_pointer_bool:
157168e9a76SHuang Jianan if (!ptr)
158168e9a76SHuang Jianan return 0;
159168e9a76SHuang Jianan ret = kstrtoul(skip_spaces(buf), 0, &t);
160168e9a76SHuang Jianan if (ret)
161168e9a76SHuang Jianan return ret;
162168e9a76SHuang Jianan if (t != 0 && t != 1)
163168e9a76SHuang Jianan return -EINVAL;
164168e9a76SHuang Jianan *(bool *)ptr = !!t;
165168e9a76SHuang Jianan return len;
166168e9a76SHuang Jianan }
167168e9a76SHuang Jianan return 0;
168168e9a76SHuang Jianan }
169168e9a76SHuang Jianan
erofs_sb_release(struct kobject * kobj)170168e9a76SHuang Jianan static void erofs_sb_release(struct kobject *kobj)
171168e9a76SHuang Jianan {
172168e9a76SHuang Jianan struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
173168e9a76SHuang Jianan s_kobj);
174168e9a76SHuang Jianan complete(&sbi->s_kobj_unregister);
175168e9a76SHuang Jianan }
176168e9a76SHuang Jianan
177168e9a76SHuang Jianan static const struct sysfs_ops erofs_attr_ops = {
178168e9a76SHuang Jianan .show = erofs_attr_show,
179168e9a76SHuang Jianan .store = erofs_attr_store,
180168e9a76SHuang Jianan };
181168e9a76SHuang Jianan
182*339bc4d3SThomas Weißschuh static const struct kobj_type erofs_sb_ktype = {
183168e9a76SHuang Jianan .default_groups = erofs_groups,
184168e9a76SHuang Jianan .sysfs_ops = &erofs_attr_ops,
185168e9a76SHuang Jianan .release = erofs_sb_release,
186168e9a76SHuang Jianan };
187168e9a76SHuang Jianan
188*339bc4d3SThomas Weißschuh static const struct kobj_type erofs_ktype = {
189168e9a76SHuang Jianan .sysfs_ops = &erofs_attr_ops,
190168e9a76SHuang Jianan };
191168e9a76SHuang Jianan
192168e9a76SHuang Jianan static struct kset erofs_root = {
193168e9a76SHuang Jianan .kobj = {.ktype = &erofs_ktype},
194168e9a76SHuang Jianan };
195168e9a76SHuang Jianan
196*339bc4d3SThomas Weißschuh static const struct kobj_type erofs_feat_ktype = {
197168e9a76SHuang Jianan .default_groups = erofs_feat_groups,
198168e9a76SHuang Jianan .sysfs_ops = &erofs_attr_ops,
199168e9a76SHuang Jianan };
200168e9a76SHuang Jianan
201168e9a76SHuang Jianan static struct kobject erofs_feat = {
202168e9a76SHuang Jianan .kset = &erofs_root,
203168e9a76SHuang Jianan };
204168e9a76SHuang Jianan
erofs_register_sysfs(struct super_block * sb)205168e9a76SHuang Jianan int erofs_register_sysfs(struct super_block *sb)
206168e9a76SHuang Jianan {
207168e9a76SHuang Jianan struct erofs_sb_info *sbi = EROFS_SB(sb);
2082ef16441SJia Zhu char *name;
2092ef16441SJia Zhu char *str = NULL;
210168e9a76SHuang Jianan int err;
211168e9a76SHuang Jianan
2122ef16441SJia Zhu if (erofs_is_fscache_mode(sb)) {
21339bfcb81SJingbo Xu if (sbi->domain_id) {
21439bfcb81SJingbo Xu str = kasprintf(GFP_KERNEL, "%s,%s", sbi->domain_id,
21539bfcb81SJingbo Xu sbi->fsid);
2162ef16441SJia Zhu if (!str)
2172ef16441SJia Zhu return -ENOMEM;
2182ef16441SJia Zhu name = str;
2192ef16441SJia Zhu } else {
22039bfcb81SJingbo Xu name = sbi->fsid;
2212ef16441SJia Zhu }
2222ef16441SJia Zhu } else {
2232ef16441SJia Zhu name = sb->s_id;
2242ef16441SJia Zhu }
225168e9a76SHuang Jianan sbi->s_kobj.kset = &erofs_root;
226168e9a76SHuang Jianan init_completion(&sbi->s_kobj_unregister);
2272ef16441SJia Zhu err = kobject_init_and_add(&sbi->s_kobj, &erofs_sb_ktype, NULL, "%s", name);
2282ef16441SJia Zhu kfree(str);
229168e9a76SHuang Jianan if (err)
230168e9a76SHuang Jianan goto put_sb_kobj;
231168e9a76SHuang Jianan return 0;
232168e9a76SHuang Jianan
233168e9a76SHuang Jianan put_sb_kobj:
234168e9a76SHuang Jianan kobject_put(&sbi->s_kobj);
235168e9a76SHuang Jianan wait_for_completion(&sbi->s_kobj_unregister);
236168e9a76SHuang Jianan return err;
237168e9a76SHuang Jianan }
238168e9a76SHuang Jianan
erofs_unregister_sysfs(struct super_block * sb)239168e9a76SHuang Jianan void erofs_unregister_sysfs(struct super_block *sb)
240168e9a76SHuang Jianan {
241168e9a76SHuang Jianan struct erofs_sb_info *sbi = EROFS_SB(sb);
242168e9a76SHuang Jianan
243a942da24SDongliang Mu if (sbi->s_kobj.state_in_sysfs) {
244168e9a76SHuang Jianan kobject_del(&sbi->s_kobj);
245168e9a76SHuang Jianan kobject_put(&sbi->s_kobj);
246168e9a76SHuang Jianan wait_for_completion(&sbi->s_kobj_unregister);
247168e9a76SHuang Jianan }
248a942da24SDongliang Mu }
249168e9a76SHuang Jianan
erofs_init_sysfs(void)250168e9a76SHuang Jianan int __init erofs_init_sysfs(void)
251168e9a76SHuang Jianan {
252168e9a76SHuang Jianan int ret;
253168e9a76SHuang Jianan
254168e9a76SHuang Jianan kobject_set_name(&erofs_root.kobj, "erofs");
255168e9a76SHuang Jianan erofs_root.kobj.parent = fs_kobj;
256168e9a76SHuang Jianan ret = kset_register(&erofs_root);
257168e9a76SHuang Jianan if (ret)
258168e9a76SHuang Jianan goto root_err;
259168e9a76SHuang Jianan
260168e9a76SHuang Jianan ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
261168e9a76SHuang Jianan NULL, "features");
262168e9a76SHuang Jianan if (ret)
263168e9a76SHuang Jianan goto feat_err;
264168e9a76SHuang Jianan return ret;
265168e9a76SHuang Jianan
266168e9a76SHuang Jianan feat_err:
267168e9a76SHuang Jianan kobject_put(&erofs_feat);
268168e9a76SHuang Jianan kset_unregister(&erofs_root);
269168e9a76SHuang Jianan root_err:
270168e9a76SHuang Jianan return ret;
271168e9a76SHuang Jianan }
272168e9a76SHuang Jianan
erofs_exit_sysfs(void)273168e9a76SHuang Jianan void erofs_exit_sysfs(void)
274168e9a76SHuang Jianan {
275168e9a76SHuang Jianan kobject_put(&erofs_feat);
276168e9a76SHuang Jianan kset_unregister(&erofs_root);
277168e9a76SHuang Jianan }
278