options.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfsplus/options.c
  4. *
  5. * Copyright (C) 2001
  6. * Brad Boyer (flar@allandria.com)
  7. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  8. *
  9. * Option parsing
  10. */
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/parser.h>
  15. #include <linux/nls.h>
  16. #include <linux/mount.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/slab.h>
  19. #include "hfsplus_fs.h"
  20. enum {
  21. opt_creator, opt_type,
  22. opt_umask, opt_uid, opt_gid,
  23. opt_part, opt_session, opt_nls,
  24. opt_nodecompose, opt_decompose,
  25. opt_barrier, opt_nobarrier,
  26. opt_force, opt_err
  27. };
  28. static const match_table_t tokens = {
  29. { opt_creator, "creator=%s" },
  30. { opt_type, "type=%s" },
  31. { opt_umask, "umask=%o" },
  32. { opt_uid, "uid=%u" },
  33. { opt_gid, "gid=%u" },
  34. { opt_part, "part=%u" },
  35. { opt_session, "session=%u" },
  36. { opt_nls, "nls=%s" },
  37. { opt_decompose, "decompose" },
  38. { opt_nodecompose, "nodecompose" },
  39. { opt_barrier, "barrier" },
  40. { opt_nobarrier, "nobarrier" },
  41. { opt_force, "force" },
  42. { opt_err, NULL }
  43. };
  44. /* Initialize an options object to reasonable defaults */
  45. void hfsplus_fill_defaults(struct hfsplus_sb_info *opts)
  46. {
  47. if (!opts)
  48. return;
  49. opts->creator = HFSPLUS_DEF_CR_TYPE;
  50. opts->type = HFSPLUS_DEF_CR_TYPE;
  51. opts->umask = current_umask();
  52. opts->uid = current_uid();
  53. opts->gid = current_gid();
  54. opts->part = -1;
  55. opts->session = -1;
  56. }
  57. /* convert a "four byte character" to a 32 bit int with error checks */
  58. static inline int match_fourchar(substring_t *arg, u32 *result)
  59. {
  60. if (arg->to - arg->from != 4)
  61. return -EINVAL;
  62. memcpy(result, arg->from, 4);
  63. return 0;
  64. }
  65. int hfsplus_parse_options_remount(char *input, int *force)
  66. {
  67. char *p;
  68. substring_t args[MAX_OPT_ARGS];
  69. int token;
  70. if (!input)
  71. return 1;
  72. while ((p = strsep(&input, ",")) != NULL) {
  73. if (!*p)
  74. continue;
  75. token = match_token(p, tokens, args);
  76. switch (token) {
  77. case opt_force:
  78. *force = 1;
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. return 1;
  85. }
  86. /* Parse options from mount. Returns 0 on failure */
  87. /* input is the options passed to mount() as a string */
  88. int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi)
  89. {
  90. char *p;
  91. substring_t args[MAX_OPT_ARGS];
  92. int tmp, token;
  93. if (!input)
  94. goto done;
  95. while ((p = strsep(&input, ",")) != NULL) {
  96. if (!*p)
  97. continue;
  98. token = match_token(p, tokens, args);
  99. switch (token) {
  100. case opt_creator:
  101. if (match_fourchar(&args[0], &sbi->creator)) {
  102. pr_err("creator requires a 4 character value\n");
  103. return 0;
  104. }
  105. break;
  106. case opt_type:
  107. if (match_fourchar(&args[0], &sbi->type)) {
  108. pr_err("type requires a 4 character value\n");
  109. return 0;
  110. }
  111. break;
  112. case opt_umask:
  113. if (match_octal(&args[0], &tmp)) {
  114. pr_err("umask requires a value\n");
  115. return 0;
  116. }
  117. sbi->umask = (umode_t)tmp;
  118. break;
  119. case opt_uid:
  120. if (match_int(&args[0], &tmp)) {
  121. pr_err("uid requires an argument\n");
  122. return 0;
  123. }
  124. sbi->uid = make_kuid(current_user_ns(), (uid_t)tmp);
  125. if (!uid_valid(sbi->uid)) {
  126. pr_err("invalid uid specified\n");
  127. return 0;
  128. } else {
  129. set_bit(HFSPLUS_SB_UID, &sbi->flags);
  130. }
  131. break;
  132. case opt_gid:
  133. if (match_int(&args[0], &tmp)) {
  134. pr_err("gid requires an argument\n");
  135. return 0;
  136. }
  137. sbi->gid = make_kgid(current_user_ns(), (gid_t)tmp);
  138. if (!gid_valid(sbi->gid)) {
  139. pr_err("invalid gid specified\n");
  140. return 0;
  141. } else {
  142. set_bit(HFSPLUS_SB_GID, &sbi->flags);
  143. }
  144. break;
  145. case opt_part:
  146. if (match_int(&args[0], &sbi->part)) {
  147. pr_err("part requires an argument\n");
  148. return 0;
  149. }
  150. break;
  151. case opt_session:
  152. if (match_int(&args[0], &sbi->session)) {
  153. pr_err("session requires an argument\n");
  154. return 0;
  155. }
  156. break;
  157. case opt_nls:
  158. if (sbi->nls) {
  159. pr_err("unable to change nls mapping\n");
  160. return 0;
  161. }
  162. p = match_strdup(&args[0]);
  163. if (p)
  164. sbi->nls = load_nls(p);
  165. if (!sbi->nls) {
  166. pr_err("unable to load nls mapping \"%s\"\n",
  167. p);
  168. kfree(p);
  169. return 0;
  170. }
  171. kfree(p);
  172. break;
  173. case opt_decompose:
  174. clear_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
  175. break;
  176. case opt_nodecompose:
  177. set_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
  178. break;
  179. case opt_barrier:
  180. clear_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags);
  181. break;
  182. case opt_nobarrier:
  183. set_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags);
  184. break;
  185. case opt_force:
  186. set_bit(HFSPLUS_SB_FORCE, &sbi->flags);
  187. break;
  188. default:
  189. return 0;
  190. }
  191. }
  192. done:
  193. if (!sbi->nls) {
  194. /* try utf8 first, as this is the old default behaviour */
  195. sbi->nls = load_nls("utf8");
  196. if (!sbi->nls)
  197. sbi->nls = load_nls_default();
  198. if (!sbi->nls)
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. int hfsplus_show_options(struct seq_file *seq, struct dentry *root)
  204. {
  205. struct hfsplus_sb_info *sbi = HFSPLUS_SB(root->d_sb);
  206. if (sbi->creator != HFSPLUS_DEF_CR_TYPE)
  207. seq_show_option_n(seq, "creator", (char *)&sbi->creator, 4);
  208. if (sbi->type != HFSPLUS_DEF_CR_TYPE)
  209. seq_show_option_n(seq, "type", (char *)&sbi->type, 4);
  210. seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask,
  211. from_kuid_munged(&init_user_ns, sbi->uid),
  212. from_kgid_munged(&init_user_ns, sbi->gid));
  213. if (sbi->part >= 0)
  214. seq_printf(seq, ",part=%u", sbi->part);
  215. if (sbi->session >= 0)
  216. seq_printf(seq, ",session=%u", sbi->session);
  217. if (sbi->nls)
  218. seq_printf(seq, ",nls=%s", sbi->nls->charset);
  219. if (test_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags))
  220. seq_puts(seq, ",nodecompose");
  221. if (test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
  222. seq_puts(seq, ",nobarrier");
  223. return 0;
  224. }