sysfs.c 7.1 KB

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