blob: 43b02b5525eb2898d4e0df1f91039a8d39b89706 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
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>
Roman Zippel717dd80e2005-09-06 15:18:48 -070016#include <linux/mount.h>
17#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "hfsplus_fs.h"
20
21enum {
22 opt_creator, opt_type,
23 opt_umask, opt_uid, opt_gid,
24 opt_part, opt_session, opt_nls,
25 opt_nodecompose, opt_decompose,
Roman Zippelb0b623c2005-11-29 19:34:41 -080026 opt_force, opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
Steven Whitehousea447c092008-10-13 10:46:57 +010029static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 { opt_creator, "creator=%s" },
31 { opt_type, "type=%s" },
32 { opt_umask, "umask=%o" },
33 { opt_uid, "uid=%u" },
34 { opt_gid, "gid=%u" },
35 { opt_part, "part=%u" },
36 { opt_session, "session=%u" },
37 { opt_nls, "nls=%s" },
38 { opt_decompose, "decompose" },
39 { opt_nodecompose, "nodecompose" },
Roman Zippelb0b623c2005-11-29 19:34:41 -080040 { opt_force, "force" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 { opt_err, NULL }
42};
43
44/* Initialize an options object to reasonable defaults */
Roman Zippel717dd80e2005-09-06 15:18:48 -070045void hfsplus_fill_defaults(struct hfsplus_sb_info *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 if (!opts)
48 return;
49
50 opts->creator = HFSPLUS_DEF_CR_TYPE;
51 opts->type = HFSPLUS_DEF_CR_TYPE;
Al Viroce3b0f82009-03-29 19:08:22 -040052 opts->umask = current_umask();
David Howells4ac84892008-11-14 10:38:54 +110053 opts->uid = current_uid();
54 opts->gid = current_gid();
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 opts->part = -1;
56 opts->session = -1;
57}
58
59/* convert a "four byte character" to a 32 bit int with error checks */
60static inline int match_fourchar(substring_t *arg, u32 *result)
61{
62 if (arg->to - arg->from != 4)
63 return -EINVAL;
64 memcpy(result, arg->from, 4);
65 return 0;
66}
67
Christoph Hellwig6f80dfe2010-11-07 23:01:17 +010068int hfsplus_parse_options_remount(char *input, int *force)
69{
70 char *p;
71 substring_t args[MAX_OPT_ARGS];
72 int token;
73
74 if (!input)
75 return 0;
76
77 while ((p = strsep(&input, ",")) != NULL) {
78 if (!*p)
79 continue;
80
81 token = match_token(p, tokens, args);
82 switch (token) {
83 case opt_force:
84 *force = 1;
85 break;
86 default:
87 break;
88 }
89 }
90
91 return 1;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* Parse options from mount. Returns 0 on failure */
95/* input is the options passed to mount() as a string */
Roman Zippel717dd80e2005-09-06 15:18:48 -070096int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 char *p;
99 substring_t args[MAX_OPT_ARGS];
100 int tmp, token;
101
102 if (!input)
103 goto done;
104
105 while ((p = strsep(&input, ",")) != NULL) {
106 if (!*p)
107 continue;
108
109 token = match_token(p, tokens, args);
110 switch (token) {
111 case opt_creator:
112 if (match_fourchar(&args[0], &sbi->creator)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800113 printk(KERN_ERR "hfs: creator requires a 4 character value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115 }
116 break;
117 case opt_type:
118 if (match_fourchar(&args[0], &sbi->type)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800119 printk(KERN_ERR "hfs: type requires a 4 character value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121 }
122 break;
123 case opt_umask:
124 if (match_octal(&args[0], &tmp)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800125 printk(KERN_ERR "hfs: umask requires a value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127 }
128 sbi->umask = (umode_t)tmp;
129 break;
130 case opt_uid:
131 if (match_int(&args[0], &tmp)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800132 printk(KERN_ERR "hfs: uid requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134 }
135 sbi->uid = (uid_t)tmp;
136 break;
137 case opt_gid:
138 if (match_int(&args[0], &tmp)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800139 printk(KERN_ERR "hfs: gid requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 0;
141 }
142 sbi->gid = (gid_t)tmp;
143 break;
144 case opt_part:
145 if (match_int(&args[0], &sbi->part)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800146 printk(KERN_ERR "hfs: part requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148 }
149 break;
150 case opt_session:
151 if (match_int(&args[0], &sbi->session)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800152 printk(KERN_ERR "hfs: session requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154 }
155 break;
156 case opt_nls:
157 if (sbi->nls) {
Roman Zippel634725a2006-01-18 17:43:05 -0800158 printk(KERN_ERR "hfs: unable to change nls mapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
160 }
161 p = match_strdup(&args[0]);
Jim Meyeringcd6fda32008-04-29 00:59:08 -0700162 if (p)
163 sbi->nls = load_nls(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!sbi->nls) {
Roman Zippel634725a2006-01-18 17:43:05 -0800165 printk(KERN_ERR "hfs: unable to load nls mapping \"%s\"\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kfree(p);
167 return 0;
168 }
169 kfree(p);
170 break;
171 case opt_decompose:
Christoph Hellwig84adede2010-10-01 05:45:20 +0200172 clear_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 case opt_nodecompose:
Christoph Hellwig84adede2010-10-01 05:45:20 +0200175 set_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
Roman Zippelb0b623c2005-11-29 19:34:41 -0800177 case opt_force:
Christoph Hellwig84adede2010-10-01 05:45:20 +0200178 set_bit(HFSPLUS_SB_FORCE, &sbi->flags);
Roman Zippelb0b623c2005-11-29 19:34:41 -0800179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 default:
181 return 0;
182 }
183 }
184
185done:
186 if (!sbi->nls) {
187 /* try utf8 first, as this is the old default behaviour */
188 sbi->nls = load_nls("utf8");
189 if (!sbi->nls)
190 sbi->nls = load_nls_default();
191 if (!sbi->nls)
192 return 0;
193 }
194
195 return 1;
196}
Roman Zippel717dd80e2005-09-06 15:18:48 -0700197
198int hfsplus_show_options(struct seq_file *seq, struct vfsmount *mnt)
199{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200200 struct hfsplus_sb_info *sbi = HFSPLUS_SB(mnt->mnt_sb);
Roman Zippel717dd80e2005-09-06 15:18:48 -0700201
202 if (sbi->creator != HFSPLUS_DEF_CR_TYPE)
203 seq_printf(seq, ",creator=%.4s", (char *)&sbi->creator);
204 if (sbi->type != HFSPLUS_DEF_CR_TYPE)
205 seq_printf(seq, ",type=%.4s", (char *)&sbi->type);
206 seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask, sbi->uid, sbi->gid);
207 if (sbi->part >= 0)
208 seq_printf(seq, ",part=%u", sbi->part);
209 if (sbi->session >= 0)
210 seq_printf(seq, ",session=%u", sbi->session);
211 if (sbi->nls)
212 seq_printf(seq, ",nls=%s", sbi->nls->charset);
Christoph Hellwig84adede2010-10-01 05:45:20 +0200213 if (test_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags))
Roman Zippel717dd80e2005-09-06 15:18:48 -0700214 seq_printf(seq, ",nodecompose");
215 return 0;
216}