blob: 9997cbf8beb50ece6abfa28dc18f3223037eab01 [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 Zippel717dd802005-09-06 15:18:48 -070016#include <linux/mount.h>
17#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "hfsplus_fs.h"
19
20enum {
21 opt_creator, opt_type,
22 opt_umask, opt_uid, opt_gid,
23 opt_part, opt_session, opt_nls,
24 opt_nodecompose, opt_decompose,
Roman Zippelb0b623c2005-11-29 19:34:41 -080025 opt_force, opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28static 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" },
Roman Zippelb0b623c2005-11-29 19:34:41 -080039 { opt_force, "force" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 { opt_err, NULL }
41};
42
43/* Initialize an options object to reasonable defaults */
Roman Zippel717dd802005-09-06 15:18:48 -070044void hfsplus_fill_defaults(struct hfsplus_sb_info *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 if (!opts)
47 return;
48
49 opts->creator = HFSPLUS_DEF_CR_TYPE;
50 opts->type = HFSPLUS_DEF_CR_TYPE;
51 opts->umask = current->fs->umask;
52 opts->uid = current->uid;
53 opts->gid = current->gid;
54 opts->part = -1;
55 opts->session = -1;
56}
57
58/* convert a "four byte character" to a 32 bit int with error checks */
59static inline int match_fourchar(substring_t *arg, u32 *result)
60{
61 if (arg->to - arg->from != 4)
62 return -EINVAL;
63 memcpy(result, arg->from, 4);
64 return 0;
65}
66
67/* Parse options from mount. Returns 0 on failure */
68/* input is the options passed to mount() as a string */
Roman Zippel717dd802005-09-06 15:18:48 -070069int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 char *p;
72 substring_t args[MAX_OPT_ARGS];
73 int tmp, token;
74
75 if (!input)
76 goto done;
77
78 while ((p = strsep(&input, ",")) != NULL) {
79 if (!*p)
80 continue;
81
82 token = match_token(p, tokens, args);
83 switch (token) {
84 case opt_creator:
85 if (match_fourchar(&args[0], &sbi->creator)) {
Roman Zippel634725a2006-01-18 17:43:05 -080086 printk(KERN_ERR "hfs: creator requires a 4 character value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88 }
89 break;
90 case opt_type:
91 if (match_fourchar(&args[0], &sbi->type)) {
Roman Zippel634725a2006-01-18 17:43:05 -080092 printk(KERN_ERR "hfs: type requires a 4 character value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94 }
95 break;
96 case opt_umask:
97 if (match_octal(&args[0], &tmp)) {
Roman Zippel634725a2006-01-18 17:43:05 -080098 printk(KERN_ERR "hfs: umask requires a value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100 }
101 sbi->umask = (umode_t)tmp;
102 break;
103 case opt_uid:
104 if (match_int(&args[0], &tmp)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800105 printk(KERN_ERR "hfs: uid requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107 }
108 sbi->uid = (uid_t)tmp;
109 break;
110 case opt_gid:
111 if (match_int(&args[0], &tmp)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800112 printk(KERN_ERR "hfs: gid requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114 }
115 sbi->gid = (gid_t)tmp;
116 break;
117 case opt_part:
118 if (match_int(&args[0], &sbi->part)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800119 printk(KERN_ERR "hfs: part requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121 }
122 break;
123 case opt_session:
124 if (match_int(&args[0], &sbi->session)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800125 printk(KERN_ERR "hfs: session requires an argument\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127 }
128 break;
129 case opt_nls:
130 if (sbi->nls) {
Roman Zippel634725a2006-01-18 17:43:05 -0800131 printk(KERN_ERR "hfs: unable to change nls mapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
133 }
134 p = match_strdup(&args[0]);
Jim Meyeringcd6fda32008-04-29 00:59:08 -0700135 if (p)
136 sbi->nls = load_nls(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!sbi->nls) {
Roman Zippel634725a2006-01-18 17:43:05 -0800138 printk(KERN_ERR "hfs: unable to load nls mapping \"%s\"\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 kfree(p);
140 return 0;
141 }
142 kfree(p);
143 break;
144 case opt_decompose:
145 sbi->flags &= ~HFSPLUS_SB_NODECOMPOSE;
146 break;
147 case opt_nodecompose:
148 sbi->flags |= HFSPLUS_SB_NODECOMPOSE;
149 break;
Roman Zippelb0b623c2005-11-29 19:34:41 -0800150 case opt_force:
151 sbi->flags |= HFSPLUS_SB_FORCE;
152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 default:
154 return 0;
155 }
156 }
157
158done:
159 if (!sbi->nls) {
160 /* try utf8 first, as this is the old default behaviour */
161 sbi->nls = load_nls("utf8");
162 if (!sbi->nls)
163 sbi->nls = load_nls_default();
164 if (!sbi->nls)
165 return 0;
166 }
167
168 return 1;
169}
Roman Zippel717dd802005-09-06 15:18:48 -0700170
171int hfsplus_show_options(struct seq_file *seq, struct vfsmount *mnt)
172{
173 struct hfsplus_sb_info *sbi = &HFSPLUS_SB(mnt->mnt_sb);
174
175 if (sbi->creator != HFSPLUS_DEF_CR_TYPE)
176 seq_printf(seq, ",creator=%.4s", (char *)&sbi->creator);
177 if (sbi->type != HFSPLUS_DEF_CR_TYPE)
178 seq_printf(seq, ",type=%.4s", (char *)&sbi->type);
179 seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask, sbi->uid, sbi->gid);
180 if (sbi->part >= 0)
181 seq_printf(seq, ",part=%u", sbi->part);
182 if (sbi->session >= 0)
183 seq_printf(seq, ",session=%u", sbi->session);
184 if (sbi->nls)
185 seq_printf(seq, ",nls=%s", sbi->nls->charset);
186 if (sbi->flags & HFSPLUS_SB_NODECOMPOSE)
187 seq_printf(seq, ",nodecompose");
188 return 0;
189}