options.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_force, opt_err
  25. };
  26. static const match_table_t tokens = {
  27. { opt_creator, "creator=%s" },
  28. { opt_type, "type=%s" },
  29. { opt_umask, "umask=%o" },
  30. { opt_uid, "uid=%u" },
  31. { opt_gid, "gid=%u" },
  32. { opt_part, "part=%u" },
  33. { opt_session, "session=%u" },
  34. { opt_nls, "nls=%s" },
  35. { opt_decompose, "decompose" },
  36. { opt_nodecompose, "nodecompose" },
  37. { opt_force, "force" },
  38. { opt_err, NULL }
  39. };
  40. /* Initialize an options object to reasonable defaults */
  41. void hfsplus_fill_defaults(struct hfsplus_sb_info *opts)
  42. {
  43. if (!opts)
  44. return;
  45. opts->creator = HFSPLUS_DEF_CR_TYPE;
  46. opts->type = HFSPLUS_DEF_CR_TYPE;
  47. opts->umask = current_umask();
  48. opts->uid = current_uid();
  49. opts->gid = current_gid();
  50. opts->part = -1;
  51. opts->session = -1;
  52. }
  53. /* convert a "four byte character" to a 32 bit int with error checks */
  54. static inline int match_fourchar(substring_t *arg, u32 *result)
  55. {
  56. if (arg->to - arg->from != 4)
  57. return -EINVAL;
  58. memcpy(result, arg->from, 4);
  59. return 0;
  60. }
  61. /* Parse options from mount. Returns 0 on failure */
  62. /* input is the options passed to mount() as a string */
  63. int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi)
  64. {
  65. char *p;
  66. substring_t args[MAX_OPT_ARGS];
  67. int tmp, token;
  68. if (!input)
  69. goto done;
  70. while ((p = strsep(&input, ",")) != NULL) {
  71. if (!*p)
  72. continue;
  73. token = match_token(p, tokens, args);
  74. switch (token) {
  75. case opt_creator:
  76. if (match_fourchar(&args[0], &sbi->creator)) {
  77. printk(KERN_ERR "hfs: creator requires a 4 character value\n");
  78. return 0;
  79. }
  80. break;
  81. case opt_type:
  82. if (match_fourchar(&args[0], &sbi->type)) {
  83. printk(KERN_ERR "hfs: type requires a 4 character value\n");
  84. return 0;
  85. }
  86. break;
  87. case opt_umask:
  88. if (match_octal(&args[0], &tmp)) {
  89. printk(KERN_ERR "hfs: umask requires a value\n");
  90. return 0;
  91. }
  92. sbi->umask = (umode_t)tmp;
  93. break;
  94. case opt_uid:
  95. if (match_int(&args[0], &tmp)) {
  96. printk(KERN_ERR "hfs: uid requires an argument\n");
  97. return 0;
  98. }
  99. sbi->uid = (uid_t)tmp;
  100. break;
  101. case opt_gid:
  102. if (match_int(&args[0], &tmp)) {
  103. printk(KERN_ERR "hfs: gid requires an argument\n");
  104. return 0;
  105. }
  106. sbi->gid = (gid_t)tmp;
  107. break;
  108. case opt_part:
  109. if (match_int(&args[0], &sbi->part)) {
  110. printk(KERN_ERR "hfs: part requires an argument\n");
  111. return 0;
  112. }
  113. break;
  114. case opt_session:
  115. if (match_int(&args[0], &sbi->session)) {
  116. printk(KERN_ERR "hfs: session requires an argument\n");
  117. return 0;
  118. }
  119. break;
  120. case opt_nls:
  121. if (sbi->nls) {
  122. printk(KERN_ERR "hfs: unable to change nls mapping\n");
  123. return 0;
  124. }
  125. p = match_strdup(&args[0]);
  126. if (p)
  127. sbi->nls = load_nls(p);
  128. if (!sbi->nls) {
  129. printk(KERN_ERR "hfs: unable to load nls mapping \"%s\"\n", p);
  130. kfree(p);
  131. return 0;
  132. }
  133. kfree(p);
  134. break;
  135. case opt_decompose:
  136. sbi->flags &= ~HFSPLUS_SB_NODECOMPOSE;
  137. break;
  138. case opt_nodecompose:
  139. sbi->flags |= HFSPLUS_SB_NODECOMPOSE;
  140. break;
  141. case opt_force:
  142. sbi->flags |= HFSPLUS_SB_FORCE;
  143. break;
  144. default:
  145. return 0;
  146. }
  147. }
  148. done:
  149. if (!sbi->nls) {
  150. /* try utf8 first, as this is the old default behaviour */
  151. sbi->nls = load_nls("utf8");
  152. if (!sbi->nls)
  153. sbi->nls = load_nls_default();
  154. if (!sbi->nls)
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. int hfsplus_show_options(struct seq_file *seq, struct vfsmount *mnt)
  160. {
  161. struct hfsplus_sb_info *sbi = &HFSPLUS_SB(mnt->mnt_sb);
  162. if (sbi->creator != HFSPLUS_DEF_CR_TYPE)
  163. seq_printf(seq, ",creator=%.4s", (char *)&sbi->creator);
  164. if (sbi->type != HFSPLUS_DEF_CR_TYPE)
  165. seq_printf(seq, ",type=%.4s", (char *)&sbi->type);
  166. seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask, sbi->uid, sbi->gid);
  167. if (sbi->part >= 0)
  168. seq_printf(seq, ",part=%u", sbi->part);
  169. if (sbi->session >= 0)
  170. seq_printf(seq, ",session=%u", sbi->session);
  171. if (sbi->nls)
  172. seq_printf(seq, ",nls=%s", sbi->nls->charset);
  173. if (sbi->flags & HFSPLUS_SB_NODECOMPOSE)
  174. seq_printf(seq, ",nodecompose");
  175. return 0;
  176. }