options.c 5.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *  linux/fs/hfsplus/options.c
 *
 * Copyright (C) 2001
 * Brad Boyer (flar@allandria.com)
 * (C) 2003 Ardis Technologies <roman@ardistech.com>
 *
 * Option parsing
 */

#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/parser.h>
#include <linux/nls.h>
R
Roman Zippel 已提交
16 17
#include <linux/mount.h>
#include <linux/seq_file.h>
18
#include <linux/slab.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25
#include "hfsplus_fs.h"

enum {
	opt_creator, opt_type,
	opt_umask, opt_uid, opt_gid,
	opt_part, opt_session, opt_nls,
	opt_nodecompose, opt_decompose,
26
	opt_barrier, opt_nobarrier,
27
	opt_force, opt_err
L
Linus Torvalds 已提交
28 29
};

30
static const match_table_t tokens = {
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40
	{ opt_creator, "creator=%s" },
	{ opt_type, "type=%s" },
	{ opt_umask, "umask=%o" },
	{ opt_uid, "uid=%u" },
	{ opt_gid, "gid=%u" },
	{ opt_part, "part=%u" },
	{ opt_session, "session=%u" },
	{ opt_nls, "nls=%s" },
	{ opt_decompose, "decompose" },
	{ opt_nodecompose, "nodecompose" },
41 42
	{ opt_barrier, "barrier" },
	{ opt_nobarrier, "nobarrier" },
43
	{ opt_force, "force" },
L
Linus Torvalds 已提交
44 45 46 47
	{ opt_err, NULL }
};

/* Initialize an options object to reasonable defaults */
R
Roman Zippel 已提交
48
void hfsplus_fill_defaults(struct hfsplus_sb_info *opts)
L
Linus Torvalds 已提交
49 50 51 52 53 54
{
	if (!opts)
		return;

	opts->creator = HFSPLUS_DEF_CR_TYPE;
	opts->type = HFSPLUS_DEF_CR_TYPE;
A
Al Viro 已提交
55
	opts->umask = current_umask();
56 57
	opts->uid = current_uid();
	opts->gid = current_gid();
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
	opts->part = -1;
	opts->session = -1;
}

/* convert a "four byte character" to a 32 bit int with error checks */
static inline int match_fourchar(substring_t *arg, u32 *result)
{
	if (arg->to - arg->from != 4)
		return -EINVAL;
	memcpy(result, arg->from, 4);
	return 0;
}

71 72 73 74 75 76 77
int hfsplus_parse_options_remount(char *input, int *force)
{
	char *p;
	substring_t args[MAX_OPT_ARGS];
	int token;

	if (!input)
V
Vyacheslav Dubeyko 已提交
78
		return 1;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	while ((p = strsep(&input, ",")) != NULL) {
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case opt_force:
			*force = 1;
			break;
		default:
			break;
		}
	}

	return 1;
}

L
Linus Torvalds 已提交
97 98
/* Parse options from mount. Returns 0 on failure */
/* input is the options passed to mount() as a string */
R
Roman Zippel 已提交
99
int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi)
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
{
	char *p;
	substring_t args[MAX_OPT_ARGS];
	int tmp, token;

	if (!input)
		goto done;

	while ((p = strsep(&input, ",")) != NULL) {
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case opt_creator:
			if (match_fourchar(&args[0], &sbi->creator)) {
116
				pr_err("creator requires a 4 character value\n");
L
Linus Torvalds 已提交
117 118 119 120 121
				return 0;
			}
			break;
		case opt_type:
			if (match_fourchar(&args[0], &sbi->type)) {
122
				pr_err("type requires a 4 character value\n");
L
Linus Torvalds 已提交
123 124 125 126 127
				return 0;
			}
			break;
		case opt_umask:
			if (match_octal(&args[0], &tmp)) {
128
				pr_err("umask requires a value\n");
L
Linus Torvalds 已提交
129 130 131 132 133 134
				return 0;
			}
			sbi->umask = (umode_t)tmp;
			break;
		case opt_uid:
			if (match_int(&args[0], &tmp)) {
135
				pr_err("uid requires an argument\n");
L
Linus Torvalds 已提交
136 137
				return 0;
			}
138 139
			sbi->uid = make_kuid(current_user_ns(), (uid_t)tmp);
			if (!uid_valid(sbi->uid)) {
140
				pr_err("invalid uid specified\n");
141 142
				return 0;
			}
L
Linus Torvalds 已提交
143 144 145
			break;
		case opt_gid:
			if (match_int(&args[0], &tmp)) {
146
				pr_err("gid requires an argument\n");
L
Linus Torvalds 已提交
147 148
				return 0;
			}
149 150
			sbi->gid = make_kgid(current_user_ns(), (gid_t)tmp);
			if (!gid_valid(sbi->gid)) {
151
				pr_err("invalid gid specified\n");
152 153
				return 0;
			}
L
Linus Torvalds 已提交
154 155 156
			break;
		case opt_part:
			if (match_int(&args[0], &sbi->part)) {
157
				pr_err("part requires an argument\n");
L
Linus Torvalds 已提交
158 159 160 161 162
				return 0;
			}
			break;
		case opt_session:
			if (match_int(&args[0], &sbi->session)) {
163
				pr_err("session requires an argument\n");
L
Linus Torvalds 已提交
164 165 166 167 168
				return 0;
			}
			break;
		case opt_nls:
			if (sbi->nls) {
169
				pr_err("unable to change nls mapping\n");
L
Linus Torvalds 已提交
170 171 172
				return 0;
			}
			p = match_strdup(&args[0]);
173 174
			if (p)
				sbi->nls = load_nls(p);
L
Linus Torvalds 已提交
175
			if (!sbi->nls) {
176
				pr_err("unable to load "
177 178
						"nls mapping \"%s\"\n",
					p);
L
Linus Torvalds 已提交
179 180 181 182 183 184
				kfree(p);
				return 0;
			}
			kfree(p);
			break;
		case opt_decompose:
185
			clear_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
L
Linus Torvalds 已提交
186 187
			break;
		case opt_nodecompose:
188
			set_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags);
L
Linus Torvalds 已提交
189
			break;
190 191 192 193 194 195
		case opt_barrier:
			clear_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags);
			break;
		case opt_nobarrier:
			set_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags);
			break;
196
		case opt_force:
197
			set_bit(HFSPLUS_SB_FORCE, &sbi->flags);
198
			break;
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		default:
			return 0;
		}
	}

done:
	if (!sbi->nls) {
		/* try utf8 first, as this is the old default behaviour */
		sbi->nls = load_nls("utf8");
		if (!sbi->nls)
			sbi->nls = load_nls_default();
		if (!sbi->nls)
			return 0;
	}

	return 1;
}
R
Roman Zippel 已提交
216

217
int hfsplus_show_options(struct seq_file *seq, struct dentry *root)
R
Roman Zippel 已提交
218
{
219
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(root->d_sb);
R
Roman Zippel 已提交
220 221 222 223 224

	if (sbi->creator != HFSPLUS_DEF_CR_TYPE)
		seq_printf(seq, ",creator=%.4s", (char *)&sbi->creator);
	if (sbi->type != HFSPLUS_DEF_CR_TYPE)
		seq_printf(seq, ",type=%.4s", (char *)&sbi->type);
225
	seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask,
226 227
			from_kuid_munged(&init_user_ns, sbi->uid),
			from_kgid_munged(&init_user_ns, sbi->gid));
R
Roman Zippel 已提交
228 229 230 231 232 233
	if (sbi->part >= 0)
		seq_printf(seq, ",part=%u", sbi->part);
	if (sbi->session >= 0)
		seq_printf(seq, ",session=%u", sbi->session);
	if (sbi->nls)
		seq_printf(seq, ",nls=%s", sbi->nls->charset);
234
	if (test_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags))
R
Roman Zippel 已提交
235
		seq_printf(seq, ",nodecompose");
236 237
	if (test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
		seq_printf(seq, ",nobarrier");
R
Roman Zippel 已提交
238 239
	return 0;
}