blob: dac252bc9228130d7c72714731b1743e1a61e279 [file] [log] [blame]
Huang Jianan168e9a72021-12-01 22:54:36 +08001// 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,
Huang Jianan40452ff2021-12-06 22:35:52 +080019 struct_erofs_mount_opts,
Huang Jianan168e9a72021-12-01 22:54:36 +080020};
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
Huang Jianan40452ff2021-12-06 22:35:52 +080058#ifdef CONFIG_EROFS_FS_ZIP
59EROFS_ATTR_RW_UI(sync_decompress, erofs_mount_opts);
60#endif
61
Huang Jianan168e9a72021-12-01 22:54:36 +080062static struct attribute *erofs_attrs[] = {
Huang Jianan40452ff2021-12-06 22:35:52 +080063#ifdef CONFIG_EROFS_FS_ZIP
64 ATTR_LIST(sync_decompress),
65#endif
Huang Jianan168e9a72021-12-01 22:54:36 +080066 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);
Yue Huab921842021-12-28 13:46:04 +080078EROFS_ATTR_FEATURE(ztailpacking);
Huang Jianan168e9a72021-12-01 22:54:36 +080079
80static 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),
87 ATTR_LIST(sb_chksum),
Yue Huab921842021-12-28 13:46:04 +080088 ATTR_LIST(ztailpacking),
Huang Jianan168e9a72021-12-01 22:54:36 +080089 NULL,
90};
91ATTRIBUTE_GROUPS(erofs_feat);
92
93static unsigned char *__struct_ptr(struct erofs_sb_info *sbi,
94 int struct_type, int offset)
95{
96 if (struct_type == struct_erofs_sb_info)
97 return (unsigned char *)sbi + offset;
Huang Jianan40452ff2021-12-06 22:35:52 +080098 if (struct_type == struct_erofs_mount_opts)
99 return (unsigned char *)&sbi->opt + offset;
Huang Jianan168e9a72021-12-01 22:54:36 +0800100 return NULL;
101}
102
103static ssize_t erofs_attr_show(struct kobject *kobj,
104 struct attribute *attr, char *buf)
105{
106 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
107 s_kobj);
108 struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
109 unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
110
111 switch (a->attr_id) {
112 case attr_feature:
113 return sysfs_emit(buf, "supported\n");
114 case attr_pointer_ui:
115 if (!ptr)
116 return 0;
117 return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr);
118 case attr_pointer_bool:
119 if (!ptr)
120 return 0;
121 return sysfs_emit(buf, "%d\n", *(bool *)ptr);
122 }
123 return 0;
124}
125
126static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
127 const char *buf, size_t len)
128{
129 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
130 s_kobj);
131 struct erofs_attr *a = container_of(attr, struct erofs_attr, attr);
132 unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset);
133 unsigned long t;
134 int ret;
135
136 switch (a->attr_id) {
137 case attr_pointer_ui:
138 if (!ptr)
139 return 0;
140 ret = kstrtoul(skip_spaces(buf), 0, &t);
141 if (ret)
142 return ret;
143 if (t != (unsigned int)t)
144 return -ERANGE;
Huang Jianan40452ff2021-12-06 22:35:52 +0800145#ifdef CONFIG_EROFS_FS_ZIP
146 if (!strcmp(a->attr.name, "sync_decompress") &&
147 (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
148 return -EINVAL;
149#endif
Huang Jianan168e9a72021-12-01 22:54:36 +0800150 *(unsigned int *)ptr = t;
151 return len;
152 case attr_pointer_bool:
153 if (!ptr)
154 return 0;
155 ret = kstrtoul(skip_spaces(buf), 0, &t);
156 if (ret)
157 return ret;
158 if (t != 0 && t != 1)
159 return -EINVAL;
160 *(bool *)ptr = !!t;
161 return len;
162 }
163 return 0;
164}
165
166static void erofs_sb_release(struct kobject *kobj)
167{
168 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
169 s_kobj);
170 complete(&sbi->s_kobj_unregister);
171}
172
173static const struct sysfs_ops erofs_attr_ops = {
174 .show = erofs_attr_show,
175 .store = erofs_attr_store,
176};
177
178static struct kobj_type erofs_sb_ktype = {
179 .default_groups = erofs_groups,
180 .sysfs_ops = &erofs_attr_ops,
181 .release = erofs_sb_release,
182};
183
184static struct kobj_type erofs_ktype = {
185 .sysfs_ops = &erofs_attr_ops,
186};
187
188static struct kset erofs_root = {
189 .kobj = {.ktype = &erofs_ktype},
190};
191
192static struct kobj_type erofs_feat_ktype = {
193 .default_groups = erofs_feat_groups,
194 .sysfs_ops = &erofs_attr_ops,
195};
196
197static struct kobject erofs_feat = {
198 .kset = &erofs_root,
199};
200
201int erofs_register_sysfs(struct super_block *sb)
202{
203 struct erofs_sb_info *sbi = EROFS_SB(sb);
204 int err;
205
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,
209 "%s", sb->s_id);
210 if (err)
211 goto put_sb_kobj;
212 return 0;
213
214put_sb_kobj:
215 kobject_put(&sbi->s_kobj);
216 wait_for_completion(&sbi->s_kobj_unregister);
217 return err;
218}
219
220void erofs_unregister_sysfs(struct super_block *sb)
221{
222 struct erofs_sb_info *sbi = EROFS_SB(sb);
223
224 kobject_del(&sbi->s_kobj);
225 kobject_put(&sbi->s_kobj);
226 wait_for_completion(&sbi->s_kobj_unregister);
227}
228
229int __init erofs_init_sysfs(void)
230{
231 int ret;
232
233 kobject_set_name(&erofs_root.kobj, "erofs");
234 erofs_root.kobj.parent = fs_kobj;
235 ret = kset_register(&erofs_root);
236 if (ret)
237 goto root_err;
238
239 ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
240 NULL, "features");
241 if (ret)
242 goto feat_err;
243 return ret;
244
245feat_err:
246 kobject_put(&erofs_feat);
247 kset_unregister(&erofs_root);
248root_err:
249 return ret;
250}
251
252void erofs_exit_sysfs(void)
253{
254 kobject_put(&erofs_feat);
255 kset_unregister(&erofs_root);
256}