options.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  129. break;
  130. case opt_gid:
  131. if (match_int(&args[0], &tmp)) {
  132. pr_err("gid requires an argument\n");
  133. return 0;
  134. }
  135. sbi->gid = make_kgid(current_user_ns(), (gid_t)tmp);
  136. if (!gid_valid(sbi->gid)) {
  137. pr_err("invalid gid specified\n");
  138. return 0;
  139. }
  140. break;
  141. case opt_part:
  142. if (match_int(&args[0], &sbi->part)) {
  143. pr_err("part requires an argument\n");
  144. return 0;
  145. }
  146. break;
  147. case opt_session:
  148. if (match_int(&args[0], &sbi->session)) {
  149. pr_err("session requires an argument\n");
  150. return 0;
  151. }
  152. break;
  153. case opt_nls:
  154. if (sbi->nls) {
  155. pr_err("unable to change nls mapping\n");
  156. return 0;
  157. }
  158. p = match_strdup(&args[0]);
  159. if (p)
  160. sbi->nls = load_nls(p);
  161. if (!sbi->nls) {
  162. pr_err("unable to load nls mapping \"%s\"\n",
  163. p);
  164. kfree(p);
  165. return 0;
  166. }
  167. kfree(p);
  168. break;
  169. case opt_decompose:
  170. clear_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
  171. break;
  172. case opt_nodecompose:
  173. set_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
  174. break;
  175. case opt_barrier:
  176. clear_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags);
  177. break;
  178. case opt_nobarrier:
  179. set_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags);
  180. break;
  181. case opt_force:
  182. set_bit(HFSPLUS_SB_FORCE, &sbi->flags);
  183. break;
  184. default:
  185. return 0;
  186. }
  187. }
  188. done:
  189. if (!sbi->nls) {
  190. /* try utf8 first, as this is the old default behaviour */
  191. sbi->nls = load_nls("utf8");
  192. if (!sbi->nls)
  193. sbi->nls = load_nls_default();
  194. if (!sbi->nls)
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. int hfsplus_show_options(struct seq_file *seq, struct dentry *root)
  200. {
  201. struct hfsplus_sb_info *sbi = HFSPLUS_SB(root->d_sb);
  202. if (sbi->creator != HFSPLUS_DEF_CR_TYPE)
  203. seq_show_option_n(seq, "creator", (char *)&sbi->creator, 4);
  204. if (sbi->type != HFSPLUS_DEF_CR_TYPE)
  205. seq_show_option_n(seq, "type", (char *)&sbi->type, 4);
  206. seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask,
  207. from_kuid_munged(&init_user_ns, sbi->uid),
  208. from_kgid_munged(&init_user_ns, sbi->gid));
  209. if (sbi->part >= 0)
  210. seq_printf(seq, ",part=%u", sbi->part);
  211. if (sbi->session >= 0)
  212. seq_printf(seq, ",session=%u", sbi->session);
  213. if (sbi->nls)
  214. seq_printf(seq, ",nls=%s", sbi->nls->charset);
  215. if (test_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags))
  216. seq_puts(seq, ",nodecompose");
  217. if (test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
  218. seq_puts(seq, ",nobarrier");
  219. return 0;
  220. }