sysfs.c 6.0 KB

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