Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/hfsplus/options.c |
| 4 | * |
| 5 | * Copyright (C) 2001 |
| 6 | * Brad Boyer (flar@allandria.com) |
| 7 | * (C) 2003 Ardis Technologies <roman@ardistech.com> |
| 8 | * |
| 9 | * Option parsing |
| 10 | */ |
| 11 | |
| 12 | #include <linux/string.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/parser.h> |
| 16 | #include <linux/nls.h> |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 17 | #include <linux/mount.h> |
| 18 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "hfsplus_fs.h" |
| 21 | |
| 22 | enum { |
| 23 | opt_creator, opt_type, |
| 24 | opt_umask, opt_uid, opt_gid, |
| 25 | opt_part, opt_session, opt_nls, |
| 26 | opt_nodecompose, opt_decompose, |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 27 | opt_barrier, opt_nobarrier, |
Roman Zippel | b0b623c | 2005-11-29 19:34:41 -0800 | [diff] [blame] | 28 | opt_force, opt_err |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 31 | static const match_table_t tokens = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { opt_creator, "creator=%s" }, |
| 33 | { opt_type, "type=%s" }, |
| 34 | { opt_umask, "umask=%o" }, |
| 35 | { opt_uid, "uid=%u" }, |
| 36 | { opt_gid, "gid=%u" }, |
| 37 | { opt_part, "part=%u" }, |
| 38 | { opt_session, "session=%u" }, |
| 39 | { opt_nls, "nls=%s" }, |
| 40 | { opt_decompose, "decompose" }, |
| 41 | { opt_nodecompose, "nodecompose" }, |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 42 | { opt_barrier, "barrier" }, |
| 43 | { opt_nobarrier, "nobarrier" }, |
Roman Zippel | b0b623c | 2005-11-29 19:34:41 -0800 | [diff] [blame] | 44 | { opt_force, "force" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { opt_err, NULL } |
| 46 | }; |
| 47 | |
| 48 | /* Initialize an options object to reasonable defaults */ |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 49 | void hfsplus_fill_defaults(struct hfsplus_sb_info *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | if (!opts) |
| 52 | return; |
| 53 | |
| 54 | opts->creator = HFSPLUS_DEF_CR_TYPE; |
| 55 | opts->type = HFSPLUS_DEF_CR_TYPE; |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 56 | opts->umask = current_umask(); |
David Howells | 4ac8489 | 2008-11-14 10:38:54 +1100 | [diff] [blame] | 57 | opts->uid = current_uid(); |
| 58 | opts->gid = current_gid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | opts->part = -1; |
| 60 | opts->session = -1; |
| 61 | } |
| 62 | |
| 63 | /* convert a "four byte character" to a 32 bit int with error checks */ |
| 64 | static inline int match_fourchar(substring_t *arg, u32 *result) |
| 65 | { |
| 66 | if (arg->to - arg->from != 4) |
| 67 | return -EINVAL; |
| 68 | memcpy(result, arg->from, 4); |
| 69 | return 0; |
| 70 | } |
| 71 | |
Christoph Hellwig | 6f80dfe | 2010-11-07 23:01:17 +0100 | [diff] [blame] | 72 | int hfsplus_parse_options_remount(char *input, int *force) |
| 73 | { |
| 74 | char *p; |
| 75 | substring_t args[MAX_OPT_ARGS]; |
| 76 | int token; |
| 77 | |
| 78 | if (!input) |
Vyacheslav Dubeyko | bd2c003 | 2014-03-03 15:38:35 -0800 | [diff] [blame] | 79 | return 1; |
Christoph Hellwig | 6f80dfe | 2010-11-07 23:01:17 +0100 | [diff] [blame] | 80 | |
| 81 | while ((p = strsep(&input, ",")) != NULL) { |
| 82 | if (!*p) |
| 83 | continue; |
| 84 | |
| 85 | token = match_token(p, tokens, args); |
| 86 | switch (token) { |
| 87 | case opt_force: |
| 88 | *force = 1; |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return 1; |
| 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* Parse options from mount. Returns 0 on failure */ |
| 99 | /* input is the options passed to mount() as a string */ |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 100 | int hfsplus_parse_options(char *input, struct hfsplus_sb_info *sbi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | char *p; |
| 103 | substring_t args[MAX_OPT_ARGS]; |
| 104 | int tmp, token; |
| 105 | |
| 106 | if (!input) |
| 107 | goto done; |
| 108 | |
| 109 | while ((p = strsep(&input, ",")) != NULL) { |
| 110 | if (!*p) |
| 111 | continue; |
| 112 | |
| 113 | token = match_token(p, tokens, args); |
| 114 | switch (token) { |
| 115 | case opt_creator: |
| 116 | if (match_fourchar(&args[0], &sbi->creator)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 117 | pr_err("creator requires a 4 character value\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | break; |
| 121 | case opt_type: |
| 122 | if (match_fourchar(&args[0], &sbi->type)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 123 | pr_err("type requires a 4 character value\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | break; |
| 127 | case opt_umask: |
| 128 | if (match_octal(&args[0], &tmp)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 129 | pr_err("umask requires a value\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | sbi->umask = (umode_t)tmp; |
| 133 | break; |
| 134 | case opt_uid: |
| 135 | if (match_int(&args[0], &tmp)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 136 | pr_err("uid requires an argument\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | } |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 139 | sbi->uid = make_kuid(current_user_ns(), (uid_t)tmp); |
| 140 | if (!uid_valid(sbi->uid)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 141 | pr_err("invalid uid specified\n"); |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | break; |
| 145 | case opt_gid: |
| 146 | if (match_int(&args[0], &tmp)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 147 | pr_err("gid requires an argument\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return 0; |
| 149 | } |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 150 | sbi->gid = make_kgid(current_user_ns(), (gid_t)tmp); |
| 151 | if (!gid_valid(sbi->gid)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 152 | pr_err("invalid gid specified\n"); |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | break; |
| 156 | case opt_part: |
| 157 | if (match_int(&args[0], &sbi->part)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 158 | pr_err("part requires an argument\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | break; |
| 162 | case opt_session: |
| 163 | if (match_int(&args[0], &sbi->session)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 164 | pr_err("session requires an argument\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | break; |
| 168 | case opt_nls: |
| 169 | if (sbi->nls) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 170 | pr_err("unable to change nls mapping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | p = match_strdup(&args[0]); |
Jim Meyering | cd6fda3 | 2008-04-29 00:59:08 -0700 | [diff] [blame] | 174 | if (p) |
| 175 | sbi->nls = load_nls(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (!sbi->nls) { |
Fabian Frederick | d8983ca | 2014-06-06 14:36:26 -0700 | [diff] [blame] | 177 | pr_err("unable to load nls mapping \"%s\"\n", |
| 178 | p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | kfree(p); |
| 180 | return 0; |
| 181 | } |
| 182 | kfree(p); |
| 183 | break; |
| 184 | case opt_decompose: |
Christoph Hellwig | 84adede | 2010-10-01 05:45:20 +0200 | [diff] [blame] | 185 | clear_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | break; |
| 187 | case opt_nodecompose: |
Christoph Hellwig | 84adede | 2010-10-01 05:45:20 +0200 | [diff] [blame] | 188 | set_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | break; |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 190 | case opt_barrier: |
| 191 | clear_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags); |
| 192 | break; |
| 193 | case opt_nobarrier: |
| 194 | set_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags); |
| 195 | break; |
Roman Zippel | b0b623c | 2005-11-29 19:34:41 -0800 | [diff] [blame] | 196 | case opt_force: |
Christoph Hellwig | 84adede | 2010-10-01 05:45:20 +0200 | [diff] [blame] | 197 | set_bit(HFSPLUS_SB_FORCE, &sbi->flags); |
Roman Zippel | b0b623c | 2005-11-29 19:34:41 -0800 | [diff] [blame] | 198 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | default: |
| 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | done: |
| 205 | if (!sbi->nls) { |
| 206 | /* try utf8 first, as this is the old default behaviour */ |
| 207 | sbi->nls = load_nls("utf8"); |
| 208 | if (!sbi->nls) |
| 209 | sbi->nls = load_nls_default(); |
| 210 | if (!sbi->nls) |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | return 1; |
| 215 | } |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 216 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 217 | int hfsplus_show_options(struct seq_file *seq, struct dentry *root) |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 218 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 219 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(root->d_sb); |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 220 | |
| 221 | if (sbi->creator != HFSPLUS_DEF_CR_TYPE) |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 222 | seq_show_option_n(seq, "creator", (char *)&sbi->creator, 4); |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 223 | if (sbi->type != HFSPLUS_DEF_CR_TYPE) |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 224 | seq_show_option_n(seq, "type", (char *)&sbi->type, 4); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 225 | seq_printf(seq, ",umask=%o,uid=%u,gid=%u", sbi->umask, |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 226 | from_kuid_munged(&init_user_ns, sbi->uid), |
| 227 | from_kgid_munged(&init_user_ns, sbi->gid)); |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 228 | if (sbi->part >= 0) |
| 229 | seq_printf(seq, ",part=%u", sbi->part); |
| 230 | if (sbi->session >= 0) |
| 231 | seq_printf(seq, ",session=%u", sbi->session); |
| 232 | if (sbi->nls) |
| 233 | seq_printf(seq, ",nls=%s", sbi->nls->charset); |
Christoph Hellwig | 84adede | 2010-10-01 05:45:20 +0200 | [diff] [blame] | 234 | if (test_bit(HFSPLUS_SB_NODECOMPOSE, &sbi->flags)) |
Fabian Frederick | d8983ca | 2014-06-06 14:36:26 -0700 | [diff] [blame] | 235 | seq_puts(seq, ",nodecompose"); |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 236 | if (test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags)) |
Fabian Frederick | d8983ca | 2014-06-06 14:36:26 -0700 | [diff] [blame] | 237 | seq_puts(seq, ",nobarrier"); |
Roman Zippel | 717dd80e | 2005-09-06 15:18:48 -0700 | [diff] [blame] | 238 | return 0; |
| 239 | } |