opts.c 38.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2005-2019 Junjiro R. Okajima
 */

/*
 * mount options/flags
 */

#include <linux/namei.h>
#include <linux/types.h> /* a distribution requires */
#include <linux/parser.h>
#include "aufs.h"

/* ---------------------------------------------------------------------- */

enum {
	Opt_br,
19
20
	Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
	Opt_idel, Opt_imod,
J. R. Okajima's avatar
J. R. Okajima committed
21
	Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
22
	Opt_rdblk_def, Opt_rdhash_def,
J. R. Okajima's avatar
J. R. Okajima committed
23
	Opt_xino, Opt_noxino,
J. R. Okajima's avatar
J. R. Okajima committed
24
25
26
	Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
	Opt_trunc_xino_path, Opt_itrunc_xino,
	Opt_trunc_xib, Opt_notrunc_xib,
J. R. Okajima's avatar
J. R. Okajima committed
27
	Opt_shwh, Opt_noshwh,
28
	Opt_plink, Opt_noplink, Opt_list_plink,
J. R. Okajima's avatar
J. R. Okajima committed
29
	Opt_udba,
30
	Opt_dio, Opt_nodio,
J. R. Okajima's avatar
J. R. Okajima committed
31
	Opt_diropq_a, Opt_diropq_w,
32
	Opt_warn_perm, Opt_nowarn_perm,
33
	Opt_wbr_copyup, Opt_wbr_create,
34
	Opt_fhsm_sec,
35
	Opt_verbose, Opt_noverbose,
J. R. Okajima's avatar
J. R. Okajima committed
36
	Opt_sum, Opt_nosum, Opt_wsum,
J. R. Okajima's avatar
J. R. Okajima committed
37
	Opt_dirperm1, Opt_nodirperm1,
38
	Opt_dirren, Opt_nodirren,
J. R. Okajima's avatar
J. R. Okajima committed
39
	Opt_acl, Opt_noacl,
40
41
42
43
44
45
46
	Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
};

static match_table_t options = {
	{Opt_br, "br=%s"},
	{Opt_br, "br:%s"},

47
48
49
50
51
52
53
54
55
	{Opt_add, "add=%d:%s"},
	{Opt_add, "add:%d:%s"},
	{Opt_add, "ins=%d:%s"},
	{Opt_add, "ins:%d:%s"},
	{Opt_append, "append=%s"},
	{Opt_append, "append:%s"},
	{Opt_prepend, "prepend=%s"},
	{Opt_prepend, "prepend:%s"},

56
57
58
	{Opt_del, "del=%s"},
	{Opt_del, "del:%s"},
	/* {Opt_idel, "idel:%d"}, */
59
60
61
	{Opt_mod, "mod=%s"},
	{Opt_mod, "mod:%s"},
	/* {Opt_imod, "imod:%d:%s"}, */
62

J. R. Okajima's avatar
J. R. Okajima committed
63
64
	{Opt_dirwh, "dirwh=%d"},

J. R. Okajima's avatar
J. R. Okajima committed
65
66
	{Opt_xino, "xino=%s"},
	{Opt_noxino, "noxino"},
J. R. Okajima's avatar
J. R. Okajima committed
67
68
69
70
71
72
73
74
	{Opt_trunc_xino, "trunc_xino"},
	{Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
	{Opt_notrunc_xino, "notrunc_xino"},
	{Opt_trunc_xino_path, "trunc_xino=%s"},
	{Opt_itrunc_xino, "itrunc_xino=%d"},
	/* {Opt_zxino, "zxino=%s"}, */
	{Opt_trunc_xib, "trunc_xib"},
	{Opt_notrunc_xib, "notrunc_xib"},
J. R. Okajima's avatar
J. R. Okajima committed
75

76
77
78
79
80
81
82
83
84
85
86
87
#ifdef CONFIG_PROC_FS
	{Opt_plink, "plink"},
#else
	{Opt_ignore_silent, "plink"},
#endif

	{Opt_noplink, "noplink"},

#ifdef CONFIG_AUFS_DEBUG
	{Opt_list_plink, "list_plink"},
#endif

J. R. Okajima's avatar
J. R. Okajima committed
88
89
	{Opt_udba, "udba=%s"},

90
91
92
	{Opt_dio, "dio"},
	{Opt_nodio, "nodio"},

93
94
95
96
97
98
99
100
#ifdef CONFIG_AUFS_DIRREN
	{Opt_dirren, "dirren"},
	{Opt_nodirren, "nodirren"},
#else
	{Opt_ignore, "dirren"},
	{Opt_ignore_silent, "nodirren"},
#endif

101
102
103
104
105
106
#ifdef CONFIG_AUFS_FHSM
	{Opt_fhsm_sec, "fhsm_sec=%d"},
#else
	{Opt_ignore, "fhsm_sec=%d"},
#endif

J. R. Okajima's avatar
J. R. Okajima committed
107
108
109
110
111
	{Opt_diropq_a, "diropq=always"},
	{Opt_diropq_a, "diropq=a"},
	{Opt_diropq_w, "diropq=whiteouted"},
	{Opt_diropq_w, "diropq=w"},

112
113
114
	{Opt_warn_perm, "warn_perm"},
	{Opt_nowarn_perm, "nowarn_perm"},

J. R. Okajima's avatar
J. R. Okajima committed
115
116
117
118
	/* keep them temporary */
	{Opt_ignore_silent, "nodlgt"},
	{Opt_ignore, "clean_plink"},

J. R. Okajima's avatar
J. R. Okajima committed
119
120
121
122
123
#ifdef CONFIG_AUFS_SHWH
	{Opt_shwh, "shwh"},
#endif
	{Opt_noshwh, "noshwh"},

J. R. Okajima's avatar
J. R. Okajima committed
124
125
126
	{Opt_dirperm1, "dirperm1"},
	{Opt_nodirperm1, "nodirperm1"},

127
128
129
130
131
132
133
	{Opt_verbose, "verbose"},
	{Opt_verbose, "v"},
	{Opt_noverbose, "noverbose"},
	{Opt_noverbose, "quiet"},
	{Opt_noverbose, "q"},
	{Opt_noverbose, "silent"},

J. R. Okajima's avatar
J. R. Okajima committed
134
135
136
137
	{Opt_sum, "sum"},
	{Opt_nosum, "nosum"},
	{Opt_wsum, "wsum"},

138
139
140
141
142
143
	{Opt_rdcache, "rdcache=%d"},
	{Opt_rdblk, "rdblk=%d"},
	{Opt_rdblk_def, "rdblk=def"},
	{Opt_rdhash, "rdhash=%d"},
	{Opt_rdhash_def, "rdhash=def"},

144
145
146
147
148
149
	{Opt_wbr_create, "create=%s"},
	{Opt_wbr_create, "create_policy=%s"},
	{Opt_wbr_copyup, "cpup=%s"},
	{Opt_wbr_copyup, "copyup=%s"},
	{Opt_wbr_copyup, "copyup_policy=%s"},

J. R. Okajima's avatar
J. R. Okajima committed
150
151
152
153
154
155
156
157
158
	/* generic VFS flag */
#ifdef CONFIG_FS_POSIX_ACL
	{Opt_acl, "acl"},
	{Opt_noacl, "noacl"},
#else
	{Opt_ignore, "acl"},
	{Opt_ignore_silent, "noacl"},
#endif

159
160
161
	/* internal use for the scripts */
	{Opt_ignore_silent, "si=%s"},

J. R. Okajima's avatar
J. R. Okajima committed
162
163
164
165
166
167
	{Opt_br, "dirs=%s"},
	{Opt_ignore, "debug=%d"},
	{Opt_ignore, "delete=whiteout"},
	{Opt_ignore, "delete=all"},
	{Opt_ignore, "imap=%s"},

168
169
170
171
172
173
174
175
	/* temporary workaround, due to old mount(8)? */
	{Opt_ignore_silent, "relatime"},

	{Opt_err, NULL}
};

/* ---------------------------------------------------------------------- */

176
177
178
179
180
181
182
183
184
185
186
187
188
189
static const char *au_parser_pattern(int val, match_table_t tbl)
{
	struct match_token *p;

	p = tbl;
	while (p->pattern) {
		if (p->token == val)
			return p->pattern;
		p++;
	}
	BUG();
	return "??";
}

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
static const char *au_optstr(int *val, match_table_t tbl)
{
	struct match_token *p;
	int v;

	v = *val;
	if (!v)
		goto out;
	p = tbl;
	while (p->pattern) {
		if (p->token
		    && (v & p->token) == p->token) {
			*val &= ~p->token;
			return p->pattern;
		}
		p++;
	}

out:
	return NULL;
}

/* ---------------------------------------------------------------------- */

static match_table_t brperm = {
	{AuBrPerm_RO, AUFS_BRPERM_RO},
216
	{AuBrPerm_RR, AUFS_BRPERM_RR},
217
	{AuBrPerm_RW, AUFS_BRPERM_RW},
218
219
220
221
	{0, NULL}
};

static match_table_t brattr = {
222
223
224
	/* general */
	{AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
	{AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
J. R. Okajima's avatar
J. R. Okajima committed
225
226
	/* 'unpin' attrib is meaningless since linux-3.18-rc1 */
	{AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
227
228
229
#ifdef CONFIG_AUFS_FHSM
	{AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
#endif
J. R. Okajima's avatar
J. R. Okajima committed
230
231
232
233
234
235
236
237
238
#ifdef CONFIG_AUFS_XATTR
	{AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
	{AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
	{AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
	{AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
	{AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
	{AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
#endif

239
240
	/* ro/rr branch */
	{AuBrRAttr_WH, AUFS_BRRATTR_WH},
241
242

	/* rw branch */
243
	{AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
244
245
	{AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},

246
247
248
249
250
251
252
253
254
255
256
257
258
259
	{0, NULL}
};

static int br_attr_val(char *str, match_table_t table, substring_t args[])
{
	int attr, v;
	char *p;

	attr = 0;
	do {
		p = strchr(str, '+');
		if (p)
			*p = 0;
		v = match_token(str, table, args);
260
261
262
		if (v) {
			if (v & AuBrAttr_CMOO_Mask)
				attr &= ~AuBrAttr_CMOO_Mask;
263
			attr |= v;
264
		} else {
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
			if (p)
				*p = '+';
			pr_warn("ignored branch attribute %s\n", str);
			break;
		}
		if (p)
			str = p + 1;
	} while (p);

	return attr;
}

static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
{
	int sz;
	const char *p;
	char *q;

	q = str->a;
	*q = 0;
	p = au_optstr(&perm, brattr);
	if (p) {
		sz = strlen(p);
		memcpy(q, p, sz + 1);
		q += sz;
	} else
		goto out;

	do {
		p = au_optstr(&perm, brattr);
		if (p) {
			*q++ = '+';
			sz = strlen(p);
			memcpy(q, p, sz + 1);
			q += sz;
		}
	} while (p);

out:
	return q - str->a;
}

static int noinline_for_stack br_perm_val(char *perm)
{
309
	int val, bad, sz;
310
311
	char *p;
	substring_t args[MAX_OPT_ARGS];
312
	au_br_perm_str_t attr;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

	p = strchr(perm, '+');
	if (p)
		*p = 0;
	val = match_token(perm, brperm, args);
	if (!val) {
		if (p)
			*p = '+';
		pr_warn("ignored branch permission %s\n", perm);
		val = AuBrPerm_RO;
		goto out;
	}
	if (!p)
		goto out;

	val |= br_attr_val(p + 1, brattr, args);

330
331
332
	bad = 0;
	switch (val & AuBrPerm_Mask) {
	case AuBrPerm_RO:
333
	case AuBrPerm_RR:
334
335
336
337
338
339
340
341
		bad = val & AuBrWAttr_Mask;
		val &= ~AuBrWAttr_Mask;
		break;
	case AuBrPerm_RW:
		bad = val & AuBrRAttr_Mask;
		val &= ~AuBrRAttr_Mask;
		break;
	}
J. R. Okajima's avatar
J. R. Okajima committed
342
343
344
345
346
347
348
349
350
351
352

	/*
	 * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
	 * does not treat it as an error, just warning.
	 * this is a tiny guard for the user operation.
	 */
	if (val & AuBrAttr_UNPIN) {
		bad |= AuBrAttr_UNPIN;
		val &= ~AuBrAttr_UNPIN;
	}

353
354
355
356
357
358
	if (unlikely(bad)) {
		sz = au_do_optstr_br_attr(&attr, bad);
		AuDebugOn(!sz);
		pr_warn("ignored branch attribute %s\n", attr.a);
	}

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
out:
	return val;
}

void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
{
	au_br_perm_str_t attr;
	const char *p;
	char *q;
	int sz;

	q = str->a;
	p = au_optstr(&perm, brperm);
	AuDebugOn(!p || !*p);
	sz = strlen(p);
	memcpy(q, p, sz + 1);
	q += sz;

	sz = au_do_optstr_br_attr(&attr, perm);
	if (sz) {
		*q++ = '+';
		memcpy(q, attr.a, sz + 1);
	}

	AuDebugOn(strlen(str->a) >= sizeof(str->a));
}

/* ---------------------------------------------------------------------- */

388
389
390
static match_table_t udbalevel = {
	{AuOpt_UDBA_REVAL, "reval"},
	{AuOpt_UDBA_NONE, "none"},
J. R. Okajima's avatar
J. R. Okajima committed
391
392
393
394
395
396
#ifdef CONFIG_AUFS_HNOTIFY
	{AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
#ifdef CONFIG_AUFS_HFSNOTIFY
	{AuOpt_UDBA_HNOTIFY, "fsnotify"},
#endif
#endif
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
	{-1, NULL}
};

static int noinline_for_stack udba_val(char *str)
{
	substring_t args[MAX_OPT_ARGS];

	return match_token(str, udbalevel, args);
}

const char *au_optstr_udba(int udba)
{
	return au_parser_pattern(udba, udbalevel);
}

/* ---------------------------------------------------------------------- */

414
415
416
static match_table_t au_wbr_create_policy = {
	{AuWbrCreate_TDP, "tdp"},
	{AuWbrCreate_TDP, "top-down-parent"},
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
	{AuWbrCreate_RR, "rr"},
	{AuWbrCreate_RR, "round-robin"},
	{AuWbrCreate_MFS, "mfs"},
	{AuWbrCreate_MFS, "most-free-space"},
	{AuWbrCreate_MFSV, "mfs:%d"},
	{AuWbrCreate_MFSV, "most-free-space:%d"},

	/* top-down regardless the parent, and then mfs */
	{AuWbrCreate_TDMFS, "tdmfs:%d"},
	{AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},

	{AuWbrCreate_MFSRR, "mfsrr:%d"},
	{AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
	{AuWbrCreate_PMFS, "pmfs"},
	{AuWbrCreate_PMFSV, "pmfs:%d"},
	{AuWbrCreate_PMFSRR, "pmfsrr:%d"},
	{AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
434
435
436
437

	{-1, NULL}
};

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
static int au_wbr_mfs_wmark(substring_t *arg, char *str,
			    struct au_opt_wbr_create *create)
{
	int err;
	unsigned long long ull;

	err = 0;
	if (!match_u64(arg, &ull))
		create->mfsrr_watermark = ull;
	else {
		pr_err("bad integer in %s\n", str);
		err = -EINVAL;
	}

	return err;
}

static int au_wbr_mfs_sec(substring_t *arg, char *str,
			  struct au_opt_wbr_create *create)
{
	int n, err;

	err = 0;
	if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
		create->mfs_second = n;
	else {
		pr_err("bad integer in %s\n", str);
		err = -EINVAL;
	}

	return err;
}

471
472
473
static int noinline_for_stack
au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
{
474
	int err, e;
475
476
477
478
	substring_t args[MAX_OPT_ARGS];

	err = match_token(str, au_wbr_create_policy, args);
	create->wbr_create = err;
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
	switch (err) {
	case AuWbrCreate_MFSRRV:
	case AuWbrCreate_TDMFSV:
	case AuWbrCreate_PMFSRRV:
		e = au_wbr_mfs_wmark(&args[0], str, create);
		if (!e)
			e = au_wbr_mfs_sec(&args[1], str, create);
		if (unlikely(e))
			err = e;
		break;
	case AuWbrCreate_MFSRR:
	case AuWbrCreate_TDMFS:
	case AuWbrCreate_PMFSRR:
		e = au_wbr_mfs_wmark(&args[0], str, create);
		if (unlikely(e)) {
			err = e;
			break;
		}
		/*FALLTHROUGH*/
	case AuWbrCreate_MFS:
	case AuWbrCreate_PMFS:
		create->mfs_second = AUFS_MFS_DEF_SEC;
		break;
	case AuWbrCreate_MFSV:
	case AuWbrCreate_PMFSV:
		e = au_wbr_mfs_sec(&args[0], str, create);
		if (unlikely(e))
			err = e;
		break;
	}
509
510
511
512
513
514
515
516
517
518
519
520

	return err;
}

const char *au_optstr_wbr_create(int wbr_create)
{
	return au_parser_pattern(wbr_create, au_wbr_create_policy);
}

static match_table_t au_wbr_copyup_policy = {
	{AuWbrCopyup_TDP, "tdp"},
	{AuWbrCopyup_TDP, "top-down-parent"},
521
522
523
524
	{AuWbrCopyup_BUP, "bup"},
	{AuWbrCopyup_BUP, "bottom-up-parent"},
	{AuWbrCopyup_BU, "bu"},
	{AuWbrCopyup_BU, "bottom-up"},
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
	{-1, NULL}
};

static int noinline_for_stack au_wbr_copyup_val(char *str)
{
	substring_t args[MAX_OPT_ARGS];

	return match_token(str, au_wbr_copyup_policy, args);
}

const char *au_optstr_wbr_copyup(int wbr_copyup)
{
	return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
}

/* ---------------------------------------------------------------------- */

542
543
544
545
546
547
548
549
static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;

static void dump_opts(struct au_opts *opts)
{
#ifdef CONFIG_AUFS_DEBUG
	/* reduce stack space */
	union {
		struct au_opt_add *add;
550
		struct au_opt_del *del;
551
		struct au_opt_mod *mod;
J. R. Okajima's avatar
J. R. Okajima committed
552
		struct au_opt_xino *xino;
J. R. Okajima's avatar
J. R. Okajima committed
553
		struct au_opt_xino_itrunc *xino_itrunc;
554
		struct au_opt_wbr_create *create;
555
556
557
558
559
560
561
562
563
564
565
566
	} u;
	struct au_opt *opt;

	opt = opts->opt;
	while (opt->type != Opt_tail) {
		switch (opt->type) {
		case Opt_add:
			u.add = &opt->add;
			AuDbg("add {b%d, %s, 0x%x, %p}\n",
				  u.add->bindex, u.add->pathname, u.add->perm,
				  u.add->path.dentry);
			break;
567
568
569
570
571
572
		case Opt_del:
		case Opt_idel:
			u.del = &opt->del;
			AuDbg("del {%s, %p}\n",
			      u.del->pathname, u.del->h_path.dentry);
			break;
573
574
575
576
577
578
		case Opt_mod:
		case Opt_imod:
			u.mod = &opt->mod;
			AuDbg("mod {%s, 0x%x, %p}\n",
				  u.mod->path, u.mod->perm, u.mod->h_root);
			break;
579
580
581
582
583
584
585
586
587
588
589
590
		case Opt_append:
			u.add = &opt->add;
			AuDbg("append {b%d, %s, 0x%x, %p}\n",
				  u.add->bindex, u.add->pathname, u.add->perm,
				  u.add->path.dentry);
			break;
		case Opt_prepend:
			u.add = &opt->add;
			AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
				  u.add->bindex, u.add->pathname, u.add->perm,
				  u.add->path.dentry);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
591
592
593
		case Opt_dirwh:
			AuDbg("dirwh %d\n", opt->dirwh);
			break;
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
		case Opt_rdcache:
			AuDbg("rdcache %d\n", opt->rdcache);
			break;
		case Opt_rdblk:
			AuDbg("rdblk %u\n", opt->rdblk);
			break;
		case Opt_rdblk_def:
			AuDbg("rdblk_def\n");
			break;
		case Opt_rdhash:
			AuDbg("rdhash %u\n", opt->rdhash);
			break;
		case Opt_rdhash_def:
			AuDbg("rdhash_def\n");
			break;
J. R. Okajima's avatar
J. R. Okajima committed
609
610
611
612
		case Opt_xino:
			u.xino = &opt->xino;
			AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
613
614
615
616
617
618
619
620
621
622
623
		case Opt_trunc_xino:
			AuLabel(trunc_xino);
			break;
		case Opt_notrunc_xino:
			AuLabel(notrunc_xino);
			break;
		case Opt_trunc_xino_path:
		case Opt_itrunc_xino:
			u.xino_itrunc = &opt->xino_itrunc;
			AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
624
625
626
		case Opt_noxino:
			AuLabel(noxino);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
627
628
629
630
631
632
		case Opt_trunc_xib:
			AuLabel(trunc_xib);
			break;
		case Opt_notrunc_xib:
			AuLabel(notrunc_xib);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
633
634
635
636
637
638
		case Opt_shwh:
			AuLabel(shwh);
			break;
		case Opt_noshwh:
			AuLabel(noshwh);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
639
640
641
642
643
644
		case Opt_dirperm1:
			AuLabel(dirperm1);
			break;
		case Opt_nodirperm1:
			AuLabel(nodirperm1);
			break;
645
646
647
648
649
650
651
652
653
		case Opt_plink:
			AuLabel(plink);
			break;
		case Opt_noplink:
			AuLabel(noplink);
			break;
		case Opt_list_plink:
			AuLabel(list_plink);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
654
655
656
657
		case Opt_udba:
			AuDbg("udba %d, %s\n",
				  opt->udba, au_optstr_udba(opt->udba));
			break;
658
659
660
661
662
663
		case Opt_dio:
			AuLabel(dio);
			break;
		case Opt_nodio:
			AuLabel(nodio);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
664
665
666
667
668
669
		case Opt_diropq_a:
			AuLabel(diropq_a);
			break;
		case Opt_diropq_w:
			AuLabel(diropq_w);
			break;
670
671
672
673
674
675
		case Opt_warn_perm:
			AuLabel(warn_perm);
			break;
		case Opt_nowarn_perm:
			AuLabel(nowarn_perm);
			break;
676
677
678
679
680
681
		case Opt_verbose:
			AuLabel(verbose);
			break;
		case Opt_noverbose:
			AuLabel(noverbose);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
682
683
684
685
686
687
688
689
690
		case Opt_sum:
			AuLabel(sum);
			break;
		case Opt_nosum:
			AuLabel(nosum);
			break;
		case Opt_wsum:
			AuLabel(wsum);
			break;
691
692
693
694
		case Opt_wbr_create:
			u.create = &opt->wbr_create;
			AuDbg("create %d, %s\n", u.create->wbr_create,
				  au_optstr_wbr_create(u.create->wbr_create));
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
			switch (u.create->wbr_create) {
			case AuWbrCreate_MFSV:
			case AuWbrCreate_PMFSV:
				AuDbg("%d sec\n", u.create->mfs_second);
				break;
			case AuWbrCreate_MFSRR:
			case AuWbrCreate_TDMFS:
				AuDbg("%llu watermark\n",
					  u.create->mfsrr_watermark);
				break;
			case AuWbrCreate_MFSRRV:
			case AuWbrCreate_TDMFSV:
			case AuWbrCreate_PMFSRRV:
				AuDbg("%llu watermark, %d sec\n",
					  u.create->mfsrr_watermark,
					  u.create->mfs_second);
				break;
			}
713
714
715
716
717
			break;
		case Opt_wbr_copyup:
			AuDbg("copyup %d, %s\n", opt->wbr_copyup,
				  au_optstr_wbr_copyup(opt->wbr_copyup));
			break;
718
719
720
		case Opt_fhsm_sec:
			AuDbg("fhsm_sec %u\n", opt->fhsm_second);
			break;
721
722
723
724
725
726
		case Opt_dirren:
			AuLabel(dirren);
			break;
		case Opt_nodirren:
			AuLabel(nodirren);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
727
728
729
730
731
732
		case Opt_acl:
			AuLabel(acl);
			break;
		case Opt_noacl:
			AuLabel(noacl);
			break;
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
		default:
			BUG();
		}
		opt++;
	}
#endif
}

void au_opts_free(struct au_opts *opts)
{
	struct au_opt *opt;

	opt = opts->opt;
	while (opt->type != Opt_tail) {
		switch (opt->type) {
		case Opt_add:
749
750
		case Opt_append:
		case Opt_prepend:
751
752
			path_put(&opt->add.path);
			break;
753
754
755
756
		case Opt_del:
		case Opt_idel:
			path_put(&opt->del.h_path);
			break;
757
758
759
760
		case Opt_mod:
		case Opt_imod:
			dput(opt->mod.h_root);
			break;
J. R. Okajima's avatar
J. R. Okajima committed
761
762
763
		case Opt_xino:
			fput(opt->xino.file);
			break;
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
		}
		opt++;
	}
}

static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
		   aufs_bindex_t bindex)
{
	int err;
	struct au_opt_add *add = &opt->add;
	char *p;

	add->bindex = bindex;
	add->perm = AuBrPerm_RO;
	add->pathname = opt_str;
	p = strchr(opt_str, '=');
	if (p) {
		*p++ = 0;
		if (*p)
			add->perm = br_perm_val(p);
	}

	err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
	if (!err) {
788
		if (!p) {
789
			add->perm = AuBrPerm_RO;
790
791
792
			if (au_test_fs_rr(add->path.dentry->d_sb))
				add->perm = AuBrPerm_RR;
			else if (!bindex && !(sb_flags & SB_RDONLY))
793
794
				add->perm = AuBrPerm_RW;
		}
795
796
797
798
799
800
801
802
803
804
		opt->type = Opt_add;
		goto out;
	}
	pr_err("lookup failed %s (%d)\n", add->pathname, err);
	err = -EINVAL;

out:
	return err;
}

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
{
	int err;

	del->pathname = args[0].from;
	AuDbg("del path %s\n", del->pathname);

	err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
	if (unlikely(err))
		pr_err("lookup failed %s (%d)\n", del->pathname, err);

	return err;
}

#if 0 /* reserved for future use */
static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
			      struct au_opt_del *del, substring_t args[])
{
	int err;
	struct dentry *root;

	err = -EINVAL;
	root = sb->s_root;
	aufs_read_lock(root, AuLock_FLUSH);
	if (bindex < 0 || au_sbbot(sb) < bindex) {
		pr_err("out of bounds, %d\n", bindex);
		goto out;
	}

	err = 0;
	del->h_path.dentry = dget(au_h_dptr(root, bindex));
	del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));

out:
	aufs_read_unlock(root, !AuLock_IR);
	return err;
}
#endif

844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
static int noinline_for_stack
au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
{
	int err;
	struct path path;
	char *p;

	err = -EINVAL;
	mod->path = args[0].from;
	p = strchr(mod->path, '=');
	if (unlikely(!p)) {
		pr_err("no permission %s\n", args[0].from);
		goto out;
	}

	*p++ = 0;
	err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
	if (unlikely(err)) {
		pr_err("lookup failed %s (%d)\n", mod->path, err);
		goto out;
	}

	mod->perm = br_perm_val(p);
	AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
	mod->h_root = dget(path.dentry);
	path_put(&path);

out:
	return err;
}

#if 0 /* reserved for future use */
static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
			      struct au_opt_mod *mod, substring_t args[])
{
	int err;
	struct dentry *root;

	err = -EINVAL;
	root = sb->s_root;
	aufs_read_lock(root, AuLock_FLUSH);
	if (bindex < 0 || au_sbbot(sb) < bindex) {
		pr_err("out of bounds, %d\n", bindex);
		goto out;
	}

	err = 0;
	mod->perm = br_perm_val(args[1].from);
	AuDbg("mod path %s, perm 0x%x, %s\n",
	      mod->path, mod->perm, args[1].from);
	mod->h_root = dget(au_h_dptr(root, bindex));

out:
	aufs_read_unlock(root, !AuLock_IR);
	return err;
}
#endif

J. R. Okajima's avatar
J. R. Okajima committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
			      substring_t args[])
{
	int err;
	struct file *file;

	file = au_xino_create(sb, args[0].from, /*silent*/0);
	err = PTR_ERR(file);
	if (IS_ERR(file))
		goto out;

	err = -EINVAL;
	if (unlikely(file->f_path.dentry->d_sb == sb)) {
		fput(file);
		pr_err("%s must be outside\n", args[0].from);
		goto out;
	}

	err = 0;
	xino->file = file;
	xino->path = args[0].from;

out:
	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
static int noinline_for_stack
au_opts_parse_xino_itrunc_path(struct super_block *sb,
			       struct au_opt_xino_itrunc *xino_itrunc,
			       substring_t args[])
{
	int err;
	aufs_bindex_t bbot, bindex;
	struct path path;
	struct dentry *root;

	err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
	if (unlikely(err)) {
		pr_err("lookup failed %s (%d)\n", args[0].from, err);
		goto out;
	}

	xino_itrunc->bindex = -1;
	root = sb->s_root;
J. R. Okajima's avatar
J. R. Okajima committed
946
	aufs_read_lock(root, AuLock_FLUSH);
J. R. Okajima's avatar
J. R. Okajima committed
947
948
949
950
951
952
953
	bbot = au_sbbot(sb);
	for (bindex = 0; bindex <= bbot; bindex++) {
		if (au_h_dptr(root, bindex) == path.dentry) {
			xino_itrunc->bindex = bindex;
			break;
		}
	}
J. R. Okajima's avatar
J. R. Okajima committed
954
	aufs_read_unlock(root, !AuLock_IR);
J. R. Okajima's avatar
J. R. Okajima committed
955
956
957
958
959
960
961
962
963
964
965
	path_put(&path);

	if (unlikely(xino_itrunc->bindex < 0)) {
		pr_err("no such branch %s\n", args[0].from);
		err = -EINVAL;
	}

out:
	return err;
}

966
967
968
969
970
971
972
973
974
975
/* called without aufs lock */
int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
{
	int err, n, token;
	aufs_bindex_t bindex;
	unsigned char skipped;
	struct dentry *root;
	struct au_opt *opt, *opt_tail;
	char *opt_str;
	/* reduce the stack space */
J. R. Okajima's avatar
J. R. Okajima committed
976
977
978
979
	union {
		struct au_opt_xino_itrunc *xino_itrunc;
		struct au_opt_wbr_create *create;
	} u;
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
	struct {
		substring_t args[MAX_OPT_ARGS];
	} *a;

	err = -ENOMEM;
	a = kmalloc(sizeof(*a), GFP_NOFS);
	if (unlikely(!a))
		goto out;

	root = sb->s_root;
	err = 0;
	bindex = 0;
	opt = opts->opt;
	opt_tail = opt + opts->max_opt - 1;
	opt->type = Opt_tail;
	while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
		err = -EINVAL;
		skipped = 0;
		token = match_token(opt_str, options, a->args);
		switch (token) {
		case Opt_br:
			err = 0;
			while (!err && (opt_str = strsep(&a->args[0].from, ":"))
			       && *opt_str) {
				err = opt_add(opt, opt_str, opts->sb_flags,
					      bindex++);
				if (unlikely(!err && ++opt > opt_tail)) {
					err = -E2BIG;
					break;
				}
				opt->type = Opt_tail;
				skipped = 1;
			}
			break;
		case Opt_add:
			if (unlikely(match_int(&a->args[0], &n))) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			bindex = n;
			err = opt_add(opt, a->args[1].from, opts->sb_flags,
				      bindex);
			if (!err)
				opt->type = token;
			break;
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
		case Opt_append:
			err = opt_add(opt, a->args[0].from, opts->sb_flags,
				      /*dummy bindex*/1);
			if (!err)
				opt->type = token;
			break;
		case Opt_prepend:
			err = opt_add(opt, a->args[0].from, opts->sb_flags,
				      /*bindex*/0);
			if (!err)
				opt->type = token;
			break;
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
		case Opt_del:
			err = au_opts_parse_del(&opt->del, a->args);
			if (!err)
				opt->type = token;
			break;
#if 0 /* reserved for future use */
		case Opt_idel:
			del->pathname = "(indexed)";
			if (unlikely(match_int(&args[0], &n))) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			err = au_opts_parse_idel(sb, n, &opt->del, a->args);
			if (!err)
				opt->type = token;
			break;
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
#endif
		case Opt_mod:
			err = au_opts_parse_mod(&opt->mod, a->args);
			if (!err)
				opt->type = token;
			break;
#ifdef IMOD /* reserved for future use */
		case Opt_imod:
			u.mod->path = "(indexed)";
			if (unlikely(match_int(&a->args[0], &n))) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
			if (!err)
				opt->type = token;
			break;
1070
#endif
J. R. Okajima's avatar
J. R. Okajima committed
1071
1072
1073
1074
1075
1076
		case Opt_xino:
			err = au_opts_parse_xino(sb, &opt->xino, a->args);
			if (!err)
				opt->type = token;
			break;

J. R. Okajima's avatar
J. R. Okajima committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
		case Opt_trunc_xino_path:
			err = au_opts_parse_xino_itrunc_path
				(sb, &opt->xino_itrunc, a->args);
			if (!err)
				opt->type = token;
			break;

		case Opt_itrunc_xino:
			u.xino_itrunc = &opt->xino_itrunc;
			if (unlikely(match_int(&a->args[0], &n))) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			u.xino_itrunc->bindex = n;
J. R. Okajima's avatar
J. R. Okajima committed
1091
			aufs_read_lock(root, AuLock_FLUSH);
J. R. Okajima's avatar
J. R. Okajima committed
1092
1093
			if (n < 0 || au_sbbot(sb) < n) {
				pr_err("out of bounds, %d\n", n);
J. R. Okajima's avatar
J. R. Okajima committed
1094
				aufs_read_unlock(root, !AuLock_IR);
J. R. Okajima's avatar
J. R. Okajima committed
1095
1096
				break;
			}
J. R. Okajima's avatar
J. R. Okajima committed
1097
			aufs_read_unlock(root, !AuLock_IR);
J. R. Okajima's avatar
J. R. Okajima committed
1098
1099
1100
1101
			err = 0;
			opt->type = token;
			break;

J. R. Okajima's avatar
J. R. Okajima committed
1102
1103
1104
1105
1106
1107
1108
		case Opt_dirwh:
			if (unlikely(match_int(&a->args[0], &opt->dirwh)))
				break;
			err = 0;
			opt->type = token;
			break;

1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
		case Opt_rdcache:
			if (unlikely(match_int(&a->args[0], &n))) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			if (unlikely(n > AUFS_RDCACHE_MAX)) {
				pr_err("rdcache must be smaller than %d\n",
				       AUFS_RDCACHE_MAX);
				break;
			}
			opt->rdcache = n;
			err = 0;
			opt->type = token;
			break;
		case Opt_rdblk:
			if (unlikely(match_int(&a->args[0], &n)
				     || n < 0
				     || n > KMALLOC_MAX_SIZE)) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			if (unlikely(n && n < NAME_MAX)) {
				pr_err("rdblk must be larger than %d\n",
				       NAME_MAX);
				break;
			}
			opt->rdblk = n;
			err = 0;
			opt->type = token;
			break;
		case Opt_rdhash:
			if (unlikely(match_int(&a->args[0], &n)
				     || n < 0
				     || n * sizeof(struct hlist_head)
				     > KMALLOC_MAX_SIZE)) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			opt->rdhash = n;
			err = 0;
			opt->type = token;
			break;

J. R. Okajima's avatar
J. R. Okajima committed
1152
1153
		case Opt_trunc_xino:
		case Opt_notrunc_xino:
J. R. Okajima's avatar
J. R. Okajima committed
1154
		case Opt_noxino:
J. R. Okajima's avatar
J. R. Okajima committed
1155
1156
		case Opt_trunc_xib:
		case Opt_notrunc_xib:
J. R. Okajima's avatar
J. R. Okajima committed
1157
1158
		case Opt_shwh:
		case Opt_noshwh:
J. R. Okajima's avatar
J. R. Okajima committed
1159
1160
		case Opt_dirperm1:
		case Opt_nodirperm1:
1161
1162
1163
		case Opt_plink:
		case Opt_noplink:
		case Opt_list_plink:
1164
1165
		case Opt_dio:
		case Opt_nodio:
J. R. Okajima's avatar
J. R. Okajima committed
1166
1167
		case Opt_diropq_a:
		case Opt_diropq_w:
1168
1169
		case Opt_warn_perm:
		case Opt_nowarn_perm:
1170
1171
		case Opt_verbose:
		case Opt_noverbose:
J. R. Okajima's avatar
J. R. Okajima committed
1172
1173
1174
		case Opt_sum:
		case Opt_nosum:
		case Opt_wsum:
1175
1176
		case Opt_rdblk_def:
		case Opt_rdhash_def:
1177
1178
		case Opt_dirren:
		case Opt_nodirren:
J. R. Okajima's avatar
J. R. Okajima committed
1179
1180
		case Opt_acl:
		case Opt_noacl:
J. R. Okajima's avatar
J. R. Okajima committed
1181
1182
1183
1184
			err = 0;
			opt->type = token;
			break;

J. R. Okajima's avatar
J. R. Okajima committed
1185
1186
1187
1188
1189
1190
1191
1192
1193
		case Opt_udba:
			opt->udba = udba_val(a->args[0].from);
			if (opt->udba >= 0) {
				err = 0;
				opt->type = token;
			} else
				pr_err("wrong value, %s\n", opt_str);
			break;

1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
		case Opt_wbr_create:
			u.create = &opt->wbr_create;
			u.create->wbr_create
				= au_wbr_create_val(a->args[0].from, u.create);
			if (u.create->wbr_create >= 0) {
				err = 0;
				opt->type = token;
			} else
				pr_err("wrong value, %s\n", opt_str);
			break;
		case Opt_wbr_copyup:
			opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
			if (opt->wbr_copyup >= 0) {
				err = 0;
				opt->type = token;
			} else
				pr_err("wrong value, %s\n", opt_str);
			break;

1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
		case Opt_fhsm_sec:
			if (unlikely(match_int(&a->args[0], &n)
				     || n < 0)) {
				pr_err("bad integer in %s\n", opt_str);
				break;
			}
			if (sysaufs_brs) {
				opt->fhsm_second = n;
				opt->type = token;
			} else
				pr_warn("ignored %s\n", opt_str);
			err = 0;
			break;

1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
		case Opt_ignore:
			pr_warn("ignored %s\n", opt_str);
			/*FALLTHROUGH*/
		case Opt_ignore_silent:
			skipped = 1;
			err = 0;
			break;
		case Opt_err:
			pr_err("unknown option %s\n", opt_str);
			break;
		}

		if (!err && !skipped) {
			if (unlikely(++opt > opt_tail)) {
				err = -E2BIG;
				opt--;
				opt->type = Opt_tail;
				break;
			}
			opt->type = Opt_tail;
		}
	}

	au_kfree_rcu(a);
	dump_opts(opts);
	if (unlikely(err))
		au_opts_free(opts);

out:
	return err;
}

1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
static int au_opt_wbr_create(struct super_block *sb,
			     struct au_opt_wbr_create *create)
{
	int err;
	struct au_sbinfo *sbinfo;

	SiMustWriteLock(sb);

	err = 1; /* handled */
	sbinfo = au_sbi(sb);
	if (sbinfo->si_wbr_create_ops->fin) {
		err = sbinfo->si_wbr_create_ops->fin(sb);
		if (!err)
			err = 1;
	}

	sbinfo->si_wbr_create = create->wbr_create;
	sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
	switch (create->wbr_create) {
	case AuWbrCreate_MFSRRV:
	case AuWbrCreate_MFSRR:
	case AuWbrCreate_TDMFS:
	case AuWbrCreate_TDMFSV:
	case AuWbrCreate_PMFSRR:
	case AuWbrCreate_PMFSRRV:
		sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
		/*FALLTHROUGH*/
	case AuWbrCreate_MFS:
	case AuWbrCreate_MFSV:
	case AuWbrCreate_PMFS:
	case AuWbrCreate_PMFSV:
		sbinfo->si_wbr_mfs.mfs_expire
			= msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
		break;
	}
1294
1295
1296
1297
1298
1299
1300

	if (sbinfo->si_wbr_create_ops->init)
		sbinfo->si_wbr_create_ops->init(sb); /* ignore */

	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
/*
 * returns,
 * plus: processed without an error
 * zero: unprocessed
 */
static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
			 struct au_opts *opts)
{
	int err;
	struct au_sbinfo *sbinfo;

	SiMustWriteLock(sb);

	err = 1; /* handled */
	sbinfo = au_sbi(sb);
	switch (opt->type) {
J. R. Okajima's avatar
J. R. Okajima committed
1317
1318
1319
1320
1321
1322
	case Opt_udba:
		sbinfo->si_mntflags &= ~AuOptMask_UDBA;
		sbinfo->si_mntflags |= opt->udba;
		opts->given_udba |= opt->udba;
		break;

1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
	case Opt_plink:
		au_opt_set(sbinfo->si_mntflags, PLINK);
		break;
	case Opt_noplink:
		if (au_opt_test(sbinfo->si_mntflags, PLINK))
			au_plink_put(sb, /*verbose*/1);
		au_opt_clr(sbinfo->si_mntflags, PLINK);
		break;
	case Opt_list_plink:
		if (au_opt_test(sbinfo->si_mntflags, PLINK))
			au_plink_list(sb);
		break;

1336
1337
1338
1339
1340
1341
1342
1343
1344
	case Opt_dio:
		au_opt_set(sbinfo->si_mntflags, DIO);
		au_fset_opts(opts->flags, REFRESH_DYAOP);
		break;
	case Opt_nodio:
		au_opt_clr(sbinfo->si_mntflags, DIO);
		au_fset_opts(opts->flags, REFRESH_DYAOP);
		break;

1345
1346
1347
1348
	case Opt_fhsm_sec:
		au_fhsm_set(sbinfo, opt->fhsm_second);
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1349
1350
1351
1352
1353
1354
1355
	case Opt_diropq_a:
		au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
		break;
	case Opt_diropq_w:
		au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
		break;

1356
1357
1358
1359
1360
1361
1362
	case Opt_warn_perm:
		au_opt_set(sbinfo->si_mntflags, WARN_PERM);
		break;
	case Opt_nowarn_perm:
		au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
		break;

1363
1364
1365
1366
1367
1368
1369
	case Opt_verbose:
		au_opt_set(sbinfo->si_mntflags, VERBOSE);
		break;
	case Opt_noverbose:
		au_opt_clr(sbinfo->si_mntflags, VERBOSE);
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
	case Opt_sum:
		au_opt_set(sbinfo->si_mntflags, SUM);
		break;
	case Opt_wsum:
		au_opt_clr(sbinfo->si_mntflags, SUM);
		au_opt_set(sbinfo->si_mntflags, SUM_W);
	case Opt_nosum:
		au_opt_clr(sbinfo->si_mntflags, SUM);
		au_opt_clr(sbinfo->si_mntflags, SUM_W);
		break;

1381
1382
1383
1384
1385
1386
1387
1388
	case Opt_wbr_create:
		err = au_opt_wbr_create(sb, &opt->wbr_create);
		break;
	case Opt_wbr_copyup:
		sbinfo->si_wbr_copyup = opt->wbr_copyup;
		sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1389
1390
1391
1392
	case Opt_dirwh:
		sbinfo->si_dirwh = opt->dirwh;
		break;

1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
	case Opt_rdcache:
		sbinfo->si_rdcache
			= msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
		break;
	case Opt_rdblk:
		sbinfo->si_rdblk = opt->rdblk;
		break;
	case Opt_rdblk_def:
		sbinfo->si_rdblk = AUFS_RDBLK_DEF;
		break;
	case Opt_rdhash:
		sbinfo->si_rdhash = opt->rdhash;
		break;
	case Opt_rdhash_def:
		sbinfo->si_rdhash = AUFS_RDHASH_DEF;
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1410
1411
1412
1413
1414
1415
1416
	case Opt_shwh:
		au_opt_set(sbinfo->si_mntflags, SHWH);
		break;
	case Opt_noshwh:
		au_opt_clr(sbinfo->si_mntflags, SHWH);
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1417
1418
1419
1420
1421
1422
1423
	case Opt_dirperm1:
		au_opt_set(sbinfo->si_mntflags, DIRPERM1);
		break;
	case Opt_nodirperm1:
		au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
	case Opt_trunc_xino:
		au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
		break;
	case Opt_notrunc_xino:
		au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
		break;

	case Opt_trunc_xino_path:
	case Opt_itrunc_xino:
		err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
				    /*idx_begin*/0);
		if (!err)
			err = 1;
		break;

	case Opt_trunc_xib:
		au_fset_opts(opts->flags, TRUNC_XIB);
		break;
	case Opt_notrunc_xib:
		au_fclr_opts(opts->flags, TRUNC_XIB);
		break;

1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
	case Opt_dirren:
		err = 1;
		if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
			err = au_dr_opt_set(sb);
			if (!err)
				err = 1;
		}
		if (err == 1)
			au_opt_set(sbinfo->si_mntflags, DIRREN);
		break;
	case Opt_nodirren:
		err = 1;
		if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
			err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
							      DR_FLUSHED));
			if (!err)
				err = 1;
		}
		if (err == 1)
			au_opt_clr(sbinfo->si_mntflags, DIRREN);
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1468
1469
1470
1471
1472
1473
1474
	case Opt_acl:
		sb->s_flags |= SB_POSIXACL;
		break;
	case Opt_noacl:
		sb->s_flags &= ~SB_POSIXACL;
		break;

J. R. Okajima's avatar
J. R. Okajima committed
1475
1476
1477
1478
1479
1480
1481
1482
	default:
		err = 0;
		break;
	}

	return err;
}

1483
1484
1485
1486
1487
1488
1489
1490
1491
/*
 * returns tri-state.
 * plus: processed without an error
 * zero: unprocessed
 * minus: error
 */
static int au_opt_br(struct super_block *sb, struct au_opt *opt,
		     struct au_opts *opts)
{
1492
	int err, do_refresh;
1493
1494
1495

	err = 0;
	switch (opt->type) {
1496
1497
1498
1499
1500
1501
1502
1503
	case Opt_append:
		opt->add.bindex = au_sbbot(sb) + 1;
		if (opt->add.bindex < 0)
			opt->add.bindex = 0;
		goto add;
	case Opt_prepend:
		opt->add.bindex = 0;
	add: /* indented label */
1504
	case Opt_add:
1505
1506
		err = au_br_add(sb, &opt->add,
				au_ftest_opts(opts->flags, REMOUNT));
1507
1508
		if (!err) {
			err = 1;
1509
			au_fset_opts(opts->flags, REFRESH);
1510
1511
		}
		break;
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522

	case Opt_del:
	case Opt_idel:
		err = au_br_del(sb, &opt->del,
				au_ftest_opts(opts->flags, REMOUNT));
		if (!err) {
			err = 1;
			au_fset_opts(opts->flags, TRUNC_XIB);
			au_fset_opts(opts->flags, REFRESH);
		}
		break;
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534

	case Opt_mod:
	case Opt_imod:
		err = au_br_mod(sb, &opt->mod,
				au_ftest_opts(opts->flags, REMOUNT),
				&do_refresh);
		if (!err) {
			err = 1;
			if (do_refresh)
				au_fset_opts(opts->flags, REFRESH);
		}
		break;
1535
	}
J. R. Okajima's avatar
J. R. Okajima committed
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
	return err;
}

static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
		       struct au_opt_xino **opt_xino,
		       struct au_opts *opts)
{
	int err;

	err = 0;
	switch (opt->type) {
	case Opt_xino:
1548
1549
		err = au_xino_set(sb, &opt->xino,
				  !!au_ftest_opts(opts->flags, REMOUNT));
J. R. Okajima's avatar
J. R. Okajima committed
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
		if (unlikely(err))
			break;

		*opt_xino = &opt->xino;
		break;

	case Opt_noxino:
		au_xino_clr(sb);
		*opt_xino = (void *)-1;
		break;
	}
1561
1562
1563
1564

	return err;
}

1565
1566
1567
int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
		   unsigned int pending)
{
1568
	int err, fhsm;
1569
	aufs_bindex_t bindex, bbot;
1570
	unsigned char do_plink, skip, do_free, can_no_dreval;
1571
1572
	struct au_branch *br;
	struct au_wbr *wbr;
1573
	struct dentry *root, *dentry;
1574
1575
1576
1577
1578
1579
1580
	struct inode *dir, *h_dir;
	struct au_sbinfo *sbinfo;
	struct au_hinode *hdir;

	SiMustAnyLock(sb);

	sbinfo = au_sbi(sb);
J. R. Okajima's avatar
J. R. Okajima committed
1581
	AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
1582
1583
1584
1585

	if (!(sb_flags & SB_RDONLY)) {
		if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
			pr_warn("first branch should be rw\n");
J. R. Okajima's avatar
J. R. Okajima committed
1586
1587
		if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
			pr_warn_once("shwh should be used with ro\n");
1588
1589
	}

J. R. Okajima's avatar
J. R. Okajima committed
1590
1591
1592
1593
1594
1595
1596
1597
	if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
	    && !au_opt_test(sbinfo->si_mntflags, XINO))
		pr_warn_once("udba=*notify requires xino\n");

	if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
		pr_warn_once("dirperm1 breaks the protection"
			     " by the permission bits on the lower branch\n");

1598
	err = 0;
1599
	fhsm = 0;
1600
1601
1602
	root = sb->s_root;
	dir = d_inode(root);
	do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
1603
1604
	can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
				      UDBA_NONE);
1605
1606
1607
1608
1609
1610
	bbot = au_sbbot(sb);
	for (bindex = 0; !err && bindex <= bbot; bindex++) {
		skip = 0;
		h_dir = au_h_iptr(dir, bindex);
		br = au_sbr(sb, bindex);

J. R. Okajima's avatar
J. R. Okajima committed
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
		if ((br->br_perm & AuBrAttr_ICEX)
		    && !h_dir->i_op->listxattr)
			br->br_perm &= ~AuBrAttr_ICEX;
#if 0
		if ((br->br_perm & AuBrAttr_ICEX_SEC)
		    && (au_br_sb(br)->s_flags & SB_NOSEC))
			br->br_perm &= ~AuBrAttr_ICEX_SEC;
#endif

		do_free = 0;
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
		wbr = br->br_wbr;
		if (wbr)
			wbr_wh_read_lock(wbr);

		if (!au_br_writable(br->br_perm)) {
			do_free = !!wbr;
			skip = (!wbr
				|| (!wbr->wbr_whbase
				    && !wbr->wbr_plink
				    && !wbr->wbr_orph));
		} else if (!au_br_wh_linkable(br->br_perm)) {
			/* skip = (!br->br_whbase && !br->br_orph); */
			skip = (!wbr || !wbr->wbr_whbase);
			if (skip && wbr) {
				if (do_plink)
					skip = !!wbr->wbr_plink;
				else
					skip = !wbr->wbr_plink;
			}
		} else {
			/* skip = (br->br_whbase && br->br_ohph); */
			skip = (wbr && wbr->wbr_whbase);
			if (skip) {
				if (do_plink)
					skip = !!wbr->wbr_plink;
				else
					skip = !wbr->wbr_plink;
			}
		}
		if (wbr)
			wbr_wh_read_unlock(wbr);

1653
1654
1655
1656
1657
1658
1659
1660
1661
		if (can_no_dreval) {
			dentry = br->br_path.dentry;
			spin_lock(&dentry->d_lock);
			if (dentry->d_flags &
			    (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
				can_no_dreval = 0;
			spin_unlock(&dentry->d_lock);
		}

1662
1663
1664
1665
1666
		if (au_br_fhsm(br->br_perm)) {
			fhsm++;
			AuDebugOn(!br->br_fhsm);
		}

1667
1668
1669
1670
		if (skip)
			continue;

		hdir = au_hi(dir, bindex);
J. R. Okajima's avatar
J. R. Okajima committed
1671
		au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1672
1673
1674
1675
1676
		if (wbr)
			wbr_wh_write_lock(wbr);
		err = au_wh_init(br, sb);
		if (wbr)
			wbr_wh_write_unlock(wbr);
J. R. Okajima's avatar
J. R. Okajima committed
1677
		au_hn_inode_unlock(hdir);
1678
1679
1680
1681
1682
1683
1684

		if (!err && do_free) {
			au_kfree_rcu(wbr);
			br->br_wbr = NULL;
		}
	}

1685
1686
1687
1688
1689
	if (can_no_dreval)
		au_fset_si(sbinfo, NO_DREVAL);
	else
		au_fclr_si(sbinfo, NO_DREVAL);

1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
	if (fhsm >= 2) {
		au_fset_si(sbinfo, FHSM);
		for (bindex = bbot; bindex >= 0; bindex--) {
			br = au_sbr(sb, bindex);
			if (au_br_fhsm(br->br_perm)) {
				au_fhsm_set_bottom(sb, bindex);
				break;
			}
		}
	} else {
		au_fclr_si(sbinfo, FHSM);
		au_fhsm_set_bottom(sb, -1);
	}

1704
1705
1706
	return err;
}

1707
1708
1709
1710
int au_opts_mount(struct super_block *sb, struct au_opts *opts)
{
	int err;
	unsigned int tmp;
J. R. Okajima's avatar
J. R. Okajima committed
1711
	aufs_bindex_t bindex, bbot;
1712
	struct au_opt *opt;
J. R. Okajima's avatar
J. R. Okajima committed
1713
	struct au_opt_xino *opt_xino, xino;
1714
	struct au_sbinfo *sbinfo;
J. R. Okajima's avatar
J. R. Okajima committed
1715
1716
	struct au_branch *br;
	struct inode *dir;
1717
1718
1719
1720

	SiMustWriteLock(sb);

	err = 0;
J. R. Okajima's avatar
J. R. Okajima committed
1721
	opt_xino = NULL;
1722
1723
	opt = opts->opt;
	while (err >= 0 && opt->type != Opt_tail)
J. R. Okajima's avatar
J. R. Okajima committed
1724
		err = au_opt_simple(sb, opt++, opts);
1725
1726
1727
1728
1729
	if (err > 0)
		err = 0;
	else if (unlikely(err < 0))
		goto out;

J. R. Okajima's avatar
J. R. Okajima committed
1730
	/* disable xino and udba temporary */
1731
1732
	sbinfo = au_sbi(sb);
	tmp = sbinfo->si_mntflags;
J. R. Okajima's avatar
J. R. Okajima committed
1733
	au_opt_clr(sbinfo->si_mntflags, XINO);
J. R. Okajima's avatar
J. R. Okajima committed
1734
	au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
J. R. Okajima's avatar
J. R. Okajima committed
1735

1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
	opt = opts->opt;
	while (err >= 0 && opt->type != Opt_tail)
		err = au_opt_br(sb, opt++, opts);
	if (err > 0)
		err = 0;
	else if (unlikely(err < 0))
		goto out;

	bbot = au_sbbot(sb);
	if (unlikely(bbot < 0)) {
		err = -EINVAL;
		pr_err("no branches\n");
		goto out;
	}

J. R. Okajima's avatar
J. R. Okajima committed
1751
1752
1753
1754
1755
1756
1757
1758
	if (au_opt_test(tmp, XINO))
		au_opt_set(sbinfo->si_mntflags, XINO);
	opt = opts->opt;
	while (!err && opt->type != Opt_tail)
		err = au_opt_xino(sb, opt++, &opt_xino, opts);
	if (unlikely(err))
		goto out;

1759
1760
1761
1762
	err = au_opts_verify(sb, sb->s_flags, tmp);
	if (unlikely(err))
		goto out;

J. R. Okajima's avatar
J. R. Okajima committed
1763
1764
1765
1766
1767
1768
1769
	/* restore xino */
	if (au_opt_test(tmp, XINO) && !opt_xino) {
		xino.file = au_xino_def(sb);
		err = PTR_ERR(xino.file);
		if (IS_ERR(xino.file))
			goto out;

1770
		err = au_xino_set(sb, &xino, /*remount*/0);
J. R. Okajima's avatar
J. R. Okajima committed
1771
1772
1773
1774
1775
		fput(xino.file);
		if (unlikely(err))
			goto out;
	}

J. R. Okajima's avatar
J. R. Okajima committed
1776
1777
1778
1779
	/* restore udba */
	tmp &= AuOptMask_UDBA;
	sbinfo->si_mntflags &= ~AuOptMask_UDBA;
	sbinfo->si_mntflags |= tmp;
J. R. Okajima's avatar
J. R. Okajima committed
1780
	bbot = au_sbbot(sb);
J. R. Okajima's avatar
J. R. Okajima committed
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
	for (bindex = 0; bindex <= bbot; bindex++) {
		br = au_sbr(sb, bindex);
		err = au_hnotify_reset_br(tmp, br, br->br_perm);
		if (unlikely(err))
			AuIOErr("hnotify failed on br %d, %d, ignored\n",
				bindex, err);
		/* go on even if err */
	}
	if (au_opt_test(tmp, UDBA_HNOTIFY)) {
		dir = d_inode(sb->s_root);
		au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
	}
J. R. Okajima's avatar
J. R. Okajima committed
1793

1794
1795
1796
out:
	return err;
}
1797

1798
1799
1800
int au_opts_remount(struct super_block *sb, struct au_opts *opts)
{
	int err, rerr;
1801
	unsigned char no_dreval;
1802
1803
1804
1805
1806
1807
1808
	struct inode *dir;
	struct au_opt_xino *opt_xino;
	struct au_opt *opt;
	struct au_sbinfo *sbinfo;

	SiMustWriteLock(sb);

1809
1810
1811
1812
1813
	err = au_dr_opt_flush(sb);
	if (unlikely(err))
		goto out;
	au_fset_opts(opts->flags, DR_FLUSHED);

1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
	dir = d_inode(sb->s_root);
	sbinfo = au_sbi(sb);
	opt_xino = NULL;
	opt = opts->opt;
	while (err >= 0 && opt->type != Opt_tail) {
		err = au_opt_simple(sb, opt, opts);
		if (!err)
			err = au_opt_br(sb, opt, opts);
		if (!err)
			err = au_opt_xino(sb, opt, &opt_xino, opts);
		opt++;
	}
	if (err > 0)
		err = 0;
	AuTraceErr(err);
	/* go on even err */

1831
	no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1832
1833
1834
1835
	rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
	if (unlikely(rerr && !err))
		err = rerr;

1836
	if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
1837
		au_fset_opts(opts->flags, REFRESH_IDOP);
1838

1839
1840
1841
1842
1843
1844
1845
1846
	if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
		rerr = au_xib_trunc(sb);
		if (unlikely(rerr && !err))
			err = rerr;
	}

	/* will be handled by the caller */
	if (!au_ftest_opts(opts->flags, REFRESH)
1847
1848
	    && (opts->given_udba
		|| au_opt_test(sbinfo->si_mntflags, XINO)
1849
		|| au_ftest_opts(opts->flags, REFRESH_IDOP)
1850
		    ))
1851
1852
1853
1854
		au_fset_opts(opts->flags, REFRESH);

	AuDbg("status 0x%x\n", opts->flags);

1855
out:
1856
1857
1858
	return err;
}

1859
1860
1861
1862
1863
1864
/* ---------------------------------------------------------------------- */

unsigned int au_opt_udba(struct super_block *sb)
{
	return au_mntflags(sb) & AuOptMask_UDBA;
}