options.c 5.3 KB

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