options.c 4.4 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>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24
#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,
25
	opt_force, opt_err
L
Linus Torvalds 已提交
26 27
};

28
static const match_table_t tokens = {
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38
	{ 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" },
39
	{ opt_force, "force" },
L
Linus Torvalds 已提交
40 41 42 43
	{ opt_err, NULL }
};

/* Initialize an options object to reasonable defaults */
R
Roman Zippel 已提交
44
void hfsplus_fill_defaults(struct hfsplus_sb_info *opts)
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
	if (!opts)
		return;

	opts->creator = HFSPLUS_DEF_CR_TYPE;
	opts->type = HFSPLUS_DEF_CR_TYPE;
	opts->umask = current->fs->umask;
	opts->uid = current->uid;
	opts->gid = current->gid;
	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;
}

/* Parse options from mount. Returns 0 on failure */
/* input is the options passed to mount() as a string */
R
Roman Zippel 已提交
69
int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi)
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	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)) {
R
Roman Zippel 已提交
86
				printk(KERN_ERR "hfs: creator requires a 4 character value\n");
L
Linus Torvalds 已提交
87 88 89 90 91
				return 0;
			}
			break;
		case opt_type:
			if (match_fourchar(&args[0], &sbi->type)) {
R
Roman Zippel 已提交
92
				printk(KERN_ERR "hfs: type requires a 4 character value\n");
L
Linus Torvalds 已提交
93 94 95 96 97
				return 0;
			}
			break;
		case opt_umask:
			if (match_octal(&args[0], &tmp)) {
R
Roman Zippel 已提交
98
				printk(KERN_ERR "hfs: umask requires a value\n");
L
Linus Torvalds 已提交
99 100 101 102 103 104
				return 0;
			}
			sbi->umask = (umode_t)tmp;
			break;
		case opt_uid:
			if (match_int(&args[0], &tmp)) {
R
Roman Zippel 已提交
105
				printk(KERN_ERR "hfs: uid requires an argument\n");
L
Linus Torvalds 已提交
106 107 108 109 110 111
				return 0;
			}
			sbi->uid = (uid_t)tmp;
			break;
		case opt_gid:
			if (match_int(&args[0], &tmp)) {
R
Roman Zippel 已提交
112
				printk(KERN_ERR "hfs: gid requires an argument\n");
L
Linus Torvalds 已提交
113 114 115 116 117 118
				return 0;
			}
			sbi->gid = (gid_t)tmp;
			break;
		case opt_part:
			if (match_int(&args[0], &sbi->part)) {
R
Roman Zippel 已提交
119
				printk(KERN_ERR "hfs: part requires an argument\n");
L
Linus Torvalds 已提交
120 121 122 123 124
				return 0;
			}
			break;
		case opt_session:
			if (match_int(&args[0], &sbi->session)) {
R
Roman Zippel 已提交
125
				printk(KERN_ERR "hfs: session requires an argument\n");
L
Linus Torvalds 已提交
126 127 128 129 130
				return 0;
			}
			break;
		case opt_nls:
			if (sbi->nls) {
R
Roman Zippel 已提交
131
				printk(KERN_ERR "hfs: unable to change nls mapping\n");
L
Linus Torvalds 已提交
132 133 134
				return 0;
			}
			p = match_strdup(&args[0]);
135 136
			if (p)
				sbi->nls = load_nls(p);
L
Linus Torvalds 已提交
137
			if (!sbi->nls) {
R
Roman Zippel 已提交
138
				printk(KERN_ERR "hfs: unable to load nls mapping \"%s\"\n", p);
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149
				kfree(p);
				return 0;
			}
			kfree(p);
			break;
		case opt_decompose:
			sbi->flags &= ~HFSPLUS_SB_NODECOMPOSE;
			break;
		case opt_nodecompose:
			sbi->flags |= HFSPLUS_SB_NODECOMPOSE;
			break;
150 151 152
		case opt_force:
			sbi->flags |= HFSPLUS_SB_FORCE;
			break;
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		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 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

int hfsplus_show_options(struct seq_file *seq, struct vfsmount *mnt)
{
	struct hfsplus_sb_info *sbi = &HFSPLUS_SB(mnt->mnt_sb);

	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);
	seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask, sbi->uid, sbi->gid);
	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);
	if (sbi->flags & HFSPLUS_SB_NODECOMPOSE)
		seq_printf(seq, ",nodecompose");
	return 0;
}