| 1 |
// 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 |
#include "compress.h" |
| 11 |
|
| 12 |
enum { |
| 13 |
attr_feature, |
| 14 |
attr_drop_caches, |
| 15 |
attr_pointer_ui, |
| 16 |
attr_pointer_bool, |
| 17 |
attr_accel, |
| 18 |
}; |
| 19 |
|
| 20 |
enum { |
| 21 |
struct_erofs_sb_info, |
| 22 |
struct_erofs_mount_opts, |
| 23 |
}; |
| 24 |
|
| 25 |
struct erofs_attr { |
| 26 |
struct attribute attr; |
| 27 |
short attr_id; |
| 28 |
int struct_type, offset; |
| 29 |
}; |
| 30 |
|
| 31 |
#define EROFS_ATTR(_name, _mode, _id) \ |
| 32 |
static struct erofs_attr erofs_attr_##_name = { \ |
| 33 |
.attr = {.name = __stringify(_name), .mode = _mode }, \ |
| 34 |
.attr_id = attr_##_id, \ |
| 35 |
} |
| 36 |
#define EROFS_ATTR_FUNC(_name, _mode) EROFS_ATTR(_name, _mode, _name) |
| 37 |
#define EROFS_ATTR_FEATURE(_name) EROFS_ATTR(_name, 0444, feature) |
| 38 |
|
| 39 |
#define EROFS_ATTR_OFFSET(_name, _mode, _id, _struct) \ |
| 40 |
static struct erofs_attr erofs_attr_##_name = { \ |
| 41 |
.attr = {.name = __stringify(_name), .mode = _mode }, \ |
| 42 |
.attr_id = attr_##_id, \ |
| 43 |
.struct_type = struct_##_struct, \ |
| 44 |
.offset = offsetof(struct _struct, _name),\ |
| 45 |
} |
| 46 |
|
| 47 |
#define EROFS_ATTR_RW(_name, _id, _struct) \ |
| 48 |
EROFS_ATTR_OFFSET(_name, 0644, _id, _struct) |
| 49 |
|
| 50 |
#define EROFS_RO_ATTR(_name, _id, _struct) \ |
| 51 |
EROFS_ATTR_OFFSET(_name, 0444, _id, _struct) |
| 52 |
|
| 53 |
#define EROFS_ATTR_RW_UI(_name, _struct) \ |
| 54 |
EROFS_ATTR_RW(_name, pointer_ui, _struct) |
| 55 |
|
| 56 |
#define EROFS_ATTR_RW_BOOL(_name, _struct) \ |
| 57 |
EROFS_ATTR_RW(_name, pointer_bool, _struct) |
| 58 |
|
| 59 |
#define ATTR_LIST(name) (&erofs_attr_##name.attr) |
| 60 |
|
| 61 |
#ifdef CONFIG_EROFS_FS_ZIP |
| 62 |
EROFS_ATTR_RW_UI(sync_decompress, erofs_sb_info); |
| 63 |
EROFS_ATTR_FUNC(drop_caches, 0200); |
| 64 |
#endif |
| 65 |
#ifdef CONFIG_EROFS_FS_ZIP_ACCEL |
| 66 |
EROFS_ATTR_FUNC(accel, 0644); |
| 67 |
#endif |
| 68 |
EROFS_ATTR_RW_UI(dir_ra_bytes, erofs_sb_info); |
| 69 |
|
| 70 |
static struct attribute *erofs_sb_attrs[] = { |
| 71 |
#ifdef CONFIG_EROFS_FS_ZIP |
| 72 |
ATTR_LIST(sync_decompress), |
| 73 |
ATTR_LIST(drop_caches), |
| 74 |
#endif |
| 75 |
ATTR_LIST(dir_ra_bytes), |
| 76 |
NULL, |
| 77 |
}; |
| 78 |
ATTRIBUTE_GROUPS(erofs_sb); |
| 79 |
|
| 80 |
static struct attribute *erofs_attrs[] = { |
| 81 |
#ifdef CONFIG_EROFS_FS_ZIP_ACCEL |
| 82 |
ATTR_LIST(accel), |
| 83 |
#endif |
| 84 |
NULL, |
| 85 |
}; |
| 86 |
ATTRIBUTE_GROUPS(erofs); |
| 87 |
|
| 88 |
/* Features this copy of erofs supports */ |
| 89 |
EROFS_ATTR_FEATURE(compr_cfgs); |
| 90 |
EROFS_ATTR_FEATURE(big_pcluster); |
| 91 |
EROFS_ATTR_FEATURE(chunked_file); |
| 92 |
EROFS_ATTR_FEATURE(device_table); |
| 93 |
EROFS_ATTR_FEATURE(compr_head2); |
| 94 |
EROFS_ATTR_FEATURE(sb_chksum); |
| 95 |
EROFS_ATTR_FEATURE(ztailpacking); |
| 96 |
EROFS_ATTR_FEATURE(fragments); |
| 97 |
EROFS_ATTR_FEATURE(dedupe); |
| 98 |
EROFS_ATTR_FEATURE(48bit); |
| 99 |
EROFS_ATTR_FEATURE(metabox); |
| 100 |
|
| 101 |
static struct attribute *erofs_feat_attrs[] = { |
| 102 |
ATTR_LIST(compr_cfgs), |
| 103 |
ATTR_LIST(big_pcluster), |
| 104 |
ATTR_LIST(chunked_file), |
| 105 |
ATTR_LIST(device_table), |
| 106 |
ATTR_LIST(compr_head2), |
| 107 |
ATTR_LIST(sb_chksum), |
| 108 |
ATTR_LIST(ztailpacking), |
| 109 |
ATTR_LIST(fragments), |
| 110 |
ATTR_LIST(dedupe), |
| 111 |
ATTR_LIST(48bit), |
| 112 |
ATTR_LIST(metabox), |
| 113 |
NULL, |
| 114 |
}; |
| 115 |
ATTRIBUTE_GROUPS(erofs_feat); |
| 116 |
|
| 117 |
static unsigned char *__struct_ptr(struct erofs_sb_info *sbi, |
| 118 |
int struct_type, int offset) |
| 119 |
{ |
| 120 |
if (struct_type == struct_erofs_sb_info) |
| 121 |
return (unsigned char *)sbi + offset; |
| 122 |
if (struct_type == struct_erofs_mount_opts) |
| 123 |
return (unsigned char *)&sbi->opt + offset; |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
static ssize_t erofs_attr_show(struct kobject *kobj, |
| 128 |
struct attribute *attr, char *buf) |
| 129 |
{ |
| 130 |
struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info, |
| 131 |
s_kobj); |
| 132 |
struct erofs_attr *a = container_of(attr, struct erofs_attr, attr); |
| 133 |
unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset); |
| 134 |
|
| 135 |
switch (a->attr_id) { |
| 136 |
case attr_feature: |
| 137 |
return sysfs_emit(buf, "supported\n"); |
| 138 |
case attr_pointer_ui: |
| 139 |
if (!ptr) |
| 140 |
return 0; |
| 141 |
return sysfs_emit(buf, "%u\n", *(unsigned int *)ptr); |
| 142 |
case attr_pointer_bool: |
| 143 |
if (!ptr) |
| 144 |
return 0; |
| 145 |
return sysfs_emit(buf, "%d\n", *(bool *)ptr); |
| 146 |
case attr_accel: |
| 147 |
return z_erofs_crypto_show_engines(buf, PAGE_SIZE, '\n'); |
| 148 |
} |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr, |
| 153 |
const char *buf, size_t len) |
| 154 |
{ |
| 155 |
struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info, |
| 156 |
s_kobj); |
| 157 |
struct erofs_attr *a = container_of(attr, struct erofs_attr, attr); |
| 158 |
unsigned char *ptr = __struct_ptr(sbi, a->struct_type, a->offset); |
| 159 |
unsigned long t; |
| 160 |
int ret; |
| 161 |
|
| 162 |
switch (a->attr_id) { |
| 163 |
case attr_pointer_ui: |
| 164 |
if (!ptr) |
| 165 |
return 0; |
| 166 |
ret = kstrtoul(skip_spaces(buf), 0, &t); |
| 167 |
if (ret) |
| 168 |
return ret; |
| 169 |
if (t != (unsigned int)t) |
| 170 |
return -ERANGE; |
| 171 |
if (IS_ENABLED(CONFIG_EROFS_FS_ZIP) && |
| 172 |
!strcmp(a->attr.name, "sync_decompress") && |
| 173 |
(t > EROFS_SYNC_DECOMPRESS_FORCE_OFF)) |
| 174 |
return -EINVAL; |
| 175 |
*(unsigned int *)ptr = t; |
| 176 |
return len; |
| 177 |
case attr_pointer_bool: |
| 178 |
if (!ptr) |
| 179 |
return 0; |
| 180 |
ret = kstrtoul(skip_spaces(buf), 0, &t); |
| 181 |
if (ret) |
| 182 |
return ret; |
| 183 |
if (t != 0 && t != 1) |
| 184 |
return -EINVAL; |
| 185 |
*(bool *)ptr = !!t; |
| 186 |
return len; |
| 187 |
#ifdef CONFIG_EROFS_FS_ZIP |
| 188 |
case attr_drop_caches: |
| 189 |
ret = kstrtoul(skip_spaces(buf), 0, &t); |
| 190 |
if (ret) |
| 191 |
return ret; |
| 192 |
if (t < 1 || t > 3) |
| 193 |
return -EINVAL; |
| 194 |
|
| 195 |
if (t & 2) |
| 196 |
z_erofs_shrink_scan(sbi, ~0UL); |
| 197 |
if (t & 1) |
| 198 |
invalidate_mapping_pages(MNGD_MAPPING(sbi), 0, -1); |
| 199 |
return len; |
| 200 |
#endif |
| 201 |
#ifdef CONFIG_EROFS_FS_ZIP_ACCEL |
| 202 |
case attr_accel: |
| 203 |
buf = skip_spaces(buf); |
| 204 |
z_erofs_crypto_disable_all_engines(); |
| 205 |
while (*buf) { |
| 206 |
t = strcspn(buf, "\n"); |
| 207 |
ret = z_erofs_crypto_enable_engine(buf, t); |
| 208 |
if (ret < 0) |
| 209 |
return ret; |
| 210 |
buf += buf[t] != '\0' ? t + 1 : t; |
| 211 |
} |
| 212 |
return len; |
| 213 |
#endif |
| 214 |
} |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
static void erofs_sb_release(struct kobject *kobj) |
| 219 |
{ |
| 220 |
struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info, |
| 221 |
s_kobj); |
| 222 |
complete(&sbi->s_kobj_unregister); |
| 223 |
} |
| 224 |
|
| 225 |
static const struct sysfs_ops erofs_attr_ops = { |
| 226 |
.show = erofs_attr_show, |
| 227 |
.store = erofs_attr_store, |
| 228 |
}; |
| 229 |
|
| 230 |
static const struct kobj_type erofs_sb_ktype = { |
| 231 |
.default_groups = erofs_sb_groups, |
| 232 |
.sysfs_ops = &erofs_attr_ops, |
| 233 |
.release = erofs_sb_release, |
| 234 |
}; |
| 235 |
|
| 236 |
static const struct kobj_type erofs_ktype = { |
| 237 |
.default_groups = erofs_groups, |
| 238 |
.sysfs_ops = &erofs_attr_ops, |
| 239 |
}; |
| 240 |
|
| 241 |
static struct kset erofs_root = { |
| 242 |
.kobj = {.ktype = &erofs_ktype}, |
| 243 |
}; |
| 244 |
|
| 245 |
static const struct kobj_type erofs_feat_ktype = { |
| 246 |
.default_groups = erofs_feat_groups, |
| 247 |
.sysfs_ops = &erofs_attr_ops, |
| 248 |
}; |
| 249 |
|
| 250 |
static struct kobject erofs_feat = { |
| 251 |
.kset = &erofs_root, |
| 252 |
}; |
| 253 |
|
| 254 |
int erofs_register_sysfs(struct super_block *sb) |
| 255 |
{ |
| 256 |
struct erofs_sb_info *sbi = EROFS_SB(sb); |
| 257 |
int err; |
| 258 |
|
| 259 |
sbi->s_kobj.kset = &erofs_root; |
| 260 |
init_completion(&sbi->s_kobj_unregister); |
| 261 |
err = kobject_init_and_add(&sbi->s_kobj, &erofs_sb_ktype, NULL, "%s", |
| 262 |
sb->s_sysfs_name); |
| 263 |
if (err) { |
| 264 |
kobject_put(&sbi->s_kobj); |
| 265 |
wait_for_completion(&sbi->s_kobj_unregister); |
| 266 |
} |
| 267 |
return err; |
| 268 |
} |
| 269 |
|
| 270 |
void erofs_unregister_sysfs(struct super_block *sb) |
| 271 |
{ |
| 272 |
struct erofs_sb_info *sbi = EROFS_SB(sb); |
| 273 |
|
| 274 |
if (sbi->s_kobj.state_in_sysfs) { |
| 275 |
kobject_del(&sbi->s_kobj); |
| 276 |
kobject_put(&sbi->s_kobj); |
| 277 |
wait_for_completion(&sbi->s_kobj_unregister); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void erofs_exit_sysfs(void) |
| 282 |
{ |
| 283 |
kobject_put(&erofs_feat); |
| 284 |
kset_unregister(&erofs_root); |
| 285 |
} |
| 286 |
|
| 287 |
int __init erofs_init_sysfs(void) |
| 288 |
{ |
| 289 |
int ret; |
| 290 |
|
| 291 |
kobject_set_name(&erofs_root.kobj, "erofs"); |
| 292 |
erofs_root.kobj.parent = fs_kobj; |
| 293 |
ret = kset_register(&erofs_root); |
| 294 |
if (!ret) { |
| 295 |
ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype, |
| 296 |
NULL, "features"); |
| 297 |
if (!ret) |
| 298 |
return 0; |
| 299 |
erofs_exit_sysfs(); |
| 300 |
} |
| 301 |
return ret; |
| 302 |
} |