Merge remote-tracking branch 'asoc/topic/wm8960' into asoc-next
[linux-2.6-block.git] / fs / overlayfs / super.c
CommitLineData
e9be9d5e
MS
1/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/namei.h>
cf9a6784 12#include <linux/pagemap.h>
e9be9d5e
MS
13#include <linux/xattr.h>
14#include <linux/security.h>
15#include <linux/mount.h>
16#include <linux/slab.h>
17#include <linux/parser.h>
18#include <linux/module.h>
e458bcd1 19#include <linux/pagemap.h>
e9be9d5e 20#include <linux/sched.h>
cc259639 21#include <linux/statfs.h>
f45827e8 22#include <linux/seq_file.h>
e9be9d5e
MS
23#include "overlayfs.h"
24
25MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26MODULE_DESCRIPTION("Overlay filesystem");
27MODULE_LICENSE("GPL");
28
f45827e8
EZ
29struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
8d3095f4 33 bool default_permissions;
f45827e8
EZ
34};
35
e9be9d5e
MS
36/* private information held for overlayfs's superblock */
37struct ovl_fs {
38 struct vfsmount *upper_mnt;
dd662667
MS
39 unsigned numlower;
40 struct vfsmount **lower_mnt;
e9be9d5e 41 struct dentry *workdir;
cc259639 42 long lower_namelen;
f45827e8
EZ
43 /* pathnames of lower and upper dirs, for show_options */
44 struct ovl_config config;
e9be9d5e
MS
45};
46
47struct ovl_dir_cache;
48
49/* private information held for every overlayfs dentry */
50struct ovl_entry {
51 struct dentry *__upperdentry;
e9be9d5e
MS
52 struct ovl_dir_cache *cache;
53 union {
54 struct {
55 u64 version;
56 bool opaque;
57 };
58 struct rcu_head rcu;
59 };
dd662667
MS
60 unsigned numlower;
61 struct path lowerstack[];
e9be9d5e
MS
62};
63
a78d9f0d
MS
64#define OVL_MAX_STACK 500
65
dd662667
MS
66static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
67{
68 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
69}
e9be9d5e
MS
70
71enum ovl_path_type ovl_path_type(struct dentry *dentry)
72{
73 struct ovl_entry *oe = dentry->d_fsdata;
1afaba1e 74 enum ovl_path_type type = 0;
e9be9d5e
MS
75
76 if (oe->__upperdentry) {
1afaba1e
MS
77 type = __OVL_PATH_UPPER;
78
45d11738
KK
79 /*
80 * Non-dir dentry can hold lower dentry from previous
81 * location. Its purity depends only on opaque flag.
82 */
83 if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode))
84 type |= __OVL_PATH_MERGE;
85 else if (!oe->opaque)
1afaba1e 86 type |= __OVL_PATH_PURE;
9d7459d8
MS
87 } else {
88 if (oe->numlower > 1)
89 type |= __OVL_PATH_MERGE;
e9be9d5e 90 }
1afaba1e 91 return type;
e9be9d5e
MS
92}
93
94static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
95{
71d50928 96 return lockless_dereference(oe->__upperdentry);
e9be9d5e
MS
97}
98
99void ovl_path_upper(struct dentry *dentry, struct path *path)
100{
101 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
102 struct ovl_entry *oe = dentry->d_fsdata;
103
104 path->mnt = ofs->upper_mnt;
105 path->dentry = ovl_upperdentry_dereference(oe);
106}
107
108enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
109{
e9be9d5e
MS
110 enum ovl_path_type type = ovl_path_type(dentry);
111
1afaba1e 112 if (!OVL_TYPE_UPPER(type))
e9be9d5e
MS
113 ovl_path_lower(dentry, path);
114 else
115 ovl_path_upper(dentry, path);
116
117 return type;
118}
119
120struct dentry *ovl_dentry_upper(struct dentry *dentry)
121{
122 struct ovl_entry *oe = dentry->d_fsdata;
123
124 return ovl_upperdentry_dereference(oe);
125}
126
127struct dentry *ovl_dentry_lower(struct dentry *dentry)
128{
129 struct ovl_entry *oe = dentry->d_fsdata;
130
dd662667 131 return __ovl_dentry_lower(oe);
e9be9d5e
MS
132}
133
134struct dentry *ovl_dentry_real(struct dentry *dentry)
135{
136 struct ovl_entry *oe = dentry->d_fsdata;
137 struct dentry *realdentry;
138
139 realdentry = ovl_upperdentry_dereference(oe);
140 if (!realdentry)
dd662667 141 realdentry = __ovl_dentry_lower(oe);
e9be9d5e
MS
142
143 return realdentry;
144}
145
146struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
147{
148 struct dentry *realdentry;
149
150 realdentry = ovl_upperdentry_dereference(oe);
151 if (realdentry) {
152 *is_upper = true;
153 } else {
dd662667 154 realdentry = __ovl_dentry_lower(oe);
e9be9d5e
MS
155 *is_upper = false;
156 }
157 return realdentry;
158}
159
8d3095f4
MS
160struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
161 bool is_upper)
162{
163 if (is_upper) {
164 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
165
166 return ofs->upper_mnt;
167 } else {
168 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
169 }
170}
171
e9be9d5e
MS
172struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
173{
174 struct ovl_entry *oe = dentry->d_fsdata;
175
176 return oe->cache;
177}
178
8d3095f4
MS
179bool ovl_is_default_permissions(struct inode *inode)
180{
181 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
182
183 return ofs->config.default_permissions;
184}
185
e9be9d5e
MS
186void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
187{
188 struct ovl_entry *oe = dentry->d_fsdata;
189
190 oe->cache = cache;
191}
192
193void ovl_path_lower(struct dentry *dentry, struct path *path)
194{
e9be9d5e
MS
195 struct ovl_entry *oe = dentry->d_fsdata;
196
dd662667 197 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
e9be9d5e
MS
198}
199
200int ovl_want_write(struct dentry *dentry)
201{
202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
203 return mnt_want_write(ofs->upper_mnt);
204}
205
206void ovl_drop_write(struct dentry *dentry)
207{
208 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
209 mnt_drop_write(ofs->upper_mnt);
210}
211
212struct dentry *ovl_workdir(struct dentry *dentry)
213{
214 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
215 return ofs->workdir;
216}
217
218bool ovl_dentry_is_opaque(struct dentry *dentry)
219{
220 struct ovl_entry *oe = dentry->d_fsdata;
221 return oe->opaque;
222}
223
224void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
225{
226 struct ovl_entry *oe = dentry->d_fsdata;
227 oe->opaque = opaque;
228}
229
230void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
231{
232 struct ovl_entry *oe = dentry->d_fsdata;
233
5955102c 234 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
e9be9d5e
MS
235 WARN_ON(oe->__upperdentry);
236 BUG_ON(!upperdentry->d_inode);
237 /*
238 * Make sure upperdentry is consistent before making it visible to
239 * ovl_upperdentry_dereference().
240 */
241 smp_wmb();
242 oe->__upperdentry = upperdentry;
243}
244
245void ovl_dentry_version_inc(struct dentry *dentry)
246{
247 struct ovl_entry *oe = dentry->d_fsdata;
248
5955102c 249 WARN_ON(!inode_is_locked(dentry->d_inode));
e9be9d5e
MS
250 oe->version++;
251}
252
253u64 ovl_dentry_version_get(struct dentry *dentry)
254{
255 struct ovl_entry *oe = dentry->d_fsdata;
256
5955102c 257 WARN_ON(!inode_is_locked(dentry->d_inode));
e9be9d5e
MS
258 return oe->version;
259}
260
261bool ovl_is_whiteout(struct dentry *dentry)
262{
263 struct inode *inode = dentry->d_inode;
264
265 return inode && IS_WHITEOUT(inode);
266}
267
268static bool ovl_is_opaquedir(struct dentry *dentry)
269{
270 int res;
271 char val;
272 struct inode *inode = dentry->d_inode;
273
274 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
275 return false;
276
cead89bb 277 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
e9be9d5e
MS
278 if (res == 1 && val == 'y')
279 return true;
280
281 return false;
282}
283
284static void ovl_dentry_release(struct dentry *dentry)
285{
286 struct ovl_entry *oe = dentry->d_fsdata;
287
288 if (oe) {
dd662667
MS
289 unsigned int i;
290
e9be9d5e 291 dput(oe->__upperdentry);
dd662667
MS
292 for (i = 0; i < oe->numlower; i++)
293 dput(oe->lowerstack[i].dentry);
e9be9d5e
MS
294 kfree_rcu(oe, rcu);
295 }
296}
297
d101a125
MS
298static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
299{
300 struct dentry *real;
301
302 if (d_is_dir(dentry)) {
303 if (!inode || inode == d_inode(dentry))
304 return dentry;
305 goto bug;
306 }
307
308 real = ovl_dentry_upper(dentry);
309 if (real && (!inode || inode == d_inode(real)))
310 return real;
311
312 real = ovl_dentry_lower(dentry);
313 if (!real)
314 goto bug;
315
316 if (!inode || inode == d_inode(real))
317 return real;
318
319 /* Handle recursion */
320 if (real->d_flags & DCACHE_OP_REAL)
321 return real->d_op->d_real(real, inode);
322
323bug:
324 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
325 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
326 return dentry;
327}
328
7c03b5d4
MS
329static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
330{
331 struct ovl_entry *oe = dentry->d_fsdata;
332 unsigned int i;
333 int ret = 1;
334
335 for (i = 0; i < oe->numlower; i++) {
336 struct dentry *d = oe->lowerstack[i].dentry;
337
338 if (d->d_flags & DCACHE_OP_REVALIDATE) {
339 ret = d->d_op->d_revalidate(d, flags);
340 if (ret < 0)
341 return ret;
342 if (!ret) {
343 if (!(flags & LOOKUP_RCU))
344 d_invalidate(d);
345 return -ESTALE;
346 }
347 }
348 }
349 return 1;
350}
351
352static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
353{
354 struct ovl_entry *oe = dentry->d_fsdata;
355 unsigned int i;
356 int ret = 1;
357
358 for (i = 0; i < oe->numlower; i++) {
359 struct dentry *d = oe->lowerstack[i].dentry;
360
361 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
362 ret = d->d_op->d_weak_revalidate(d, flags);
363 if (ret <= 0)
364 break;
365 }
366 }
367 return ret;
368}
369
e9be9d5e
MS
370static const struct dentry_operations ovl_dentry_operations = {
371 .d_release = ovl_dentry_release,
4bacc9c9 372 .d_select_inode = ovl_d_select_inode,
d101a125 373 .d_real = ovl_d_real,
e9be9d5e
MS
374};
375
7c03b5d4
MS
376static const struct dentry_operations ovl_reval_dentry_operations = {
377 .d_release = ovl_dentry_release,
b5891cfa 378 .d_select_inode = ovl_d_select_inode,
d101a125 379 .d_real = ovl_d_real,
7c03b5d4
MS
380 .d_revalidate = ovl_dentry_revalidate,
381 .d_weak_revalidate = ovl_dentry_weak_revalidate,
382};
383
dd662667 384static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
e9be9d5e 385{
dd662667
MS
386 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
387 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
388
389 if (oe)
390 oe->numlower = numlower;
391
392 return oe;
e9be9d5e
MS
393}
394
7c03b5d4
MS
395static bool ovl_dentry_remote(struct dentry *dentry)
396{
397 return dentry->d_flags &
398 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
399}
400
401static bool ovl_dentry_weird(struct dentry *dentry)
402{
403 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
404 DCACHE_MANAGE_TRANSIT |
405 DCACHE_OP_HASH |
406 DCACHE_OP_COMPARE);
407}
408
e9be9d5e
MS
409static inline struct dentry *ovl_lookup_real(struct dentry *dir,
410 struct qstr *name)
411{
412 struct dentry *dentry;
413
38b78a5f 414 dentry = lookup_hash(name, dir);
e9be9d5e
MS
415
416 if (IS_ERR(dentry)) {
417 if (PTR_ERR(dentry) == -ENOENT)
418 dentry = NULL;
419 } else if (!dentry->d_inode) {
420 dput(dentry);
421 dentry = NULL;
7c03b5d4 422 } else if (ovl_dentry_weird(dentry)) {
a6f15d9a 423 dput(dentry);
7c03b5d4 424 /* Don't support traversing automounts and other weirdness */
a6f15d9a 425 dentry = ERR_PTR(-EREMOTE);
e9be9d5e
MS
426 }
427 return dentry;
428}
429
5ef88da5
MS
430/*
431 * Returns next layer in stack starting from top.
432 * Returns -1 if this is the last layer.
433 */
434int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
435{
436 struct ovl_entry *oe = dentry->d_fsdata;
437
438 BUG_ON(idx < 0);
439 if (idx == 0) {
440 ovl_path_upper(dentry, path);
441 if (path->dentry)
442 return oe->numlower ? 1 : -1;
443 idx++;
444 }
445 BUG_ON(idx > oe->numlower);
446 *path = oe->lowerstack[idx - 1];
447
448 return (idx < oe->numlower) ? idx + 1 : -1;
449}
450
e9be9d5e
MS
451struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
452 unsigned int flags)
453{
454 struct ovl_entry *oe;
3d3c6b89
MS
455 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
456 struct path *stack = NULL;
457 struct dentry *upperdir, *upperdentry = NULL;
458 unsigned int ctr = 0;
e9be9d5e 459 struct inode *inode = NULL;
3d3c6b89
MS
460 bool upperopaque = false;
461 struct dentry *this, *prev = NULL;
462 unsigned int i;
e9be9d5e
MS
463 int err;
464
3d3c6b89 465 upperdir = ovl_upperdentry_dereference(poe);
e9be9d5e 466 if (upperdir) {
3d3c6b89
MS
467 this = ovl_lookup_real(upperdir, &dentry->d_name);
468 err = PTR_ERR(this);
469 if (IS_ERR(this))
470 goto out;
471
3e01cee3 472 if (this) {
7c03b5d4
MS
473 if (unlikely(ovl_dentry_remote(this))) {
474 dput(this);
475 err = -EREMOTE;
476 goto out;
477 }
3d3c6b89
MS
478 if (ovl_is_whiteout(this)) {
479 dput(this);
480 this = NULL;
481 upperopaque = true;
3e01cee3 482 } else if (poe->numlower && ovl_is_opaquedir(this)) {
3d3c6b89 483 upperopaque = true;
e9be9d5e
MS
484 }
485 }
3d3c6b89 486 upperdentry = prev = this;
e9be9d5e 487 }
3d3c6b89
MS
488
489 if (!upperopaque && poe->numlower) {
490 err = -ENOMEM;
491 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
492 if (!stack)
493 goto out_put_upper;
e9be9d5e
MS
494 }
495
3d3c6b89
MS
496 for (i = 0; !upperopaque && i < poe->numlower; i++) {
497 bool opaque = false;
498 struct path lowerpath = poe->lowerstack[i];
499
3d3c6b89
MS
500 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
501 err = PTR_ERR(this);
09e10322
MS
502 if (IS_ERR(this)) {
503 /*
504 * If it's positive, then treat ENAMETOOLONG as ENOENT.
505 */
506 if (err == -ENAMETOOLONG && (upperdentry || ctr))
507 continue;
3d3c6b89 508 goto out_put;
09e10322 509 }
3d3c6b89
MS
510 if (!this)
511 continue;
3e01cee3
MS
512 if (ovl_is_whiteout(this)) {
513 dput(this);
514 break;
515 }
3d3c6b89 516 /*
3e01cee3
MS
517 * Only makes sense to check opaque dir if this is not the
518 * lowermost layer.
3d3c6b89 519 */
3e01cee3
MS
520 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
521 opaque = true;
a425c037 522
523 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
524 !S_ISDIR(this->d_inode->i_mode))) {
525 /*
526 * FIXME: check for upper-opaqueness maybe better done
527 * in remove code.
528 */
3d3c6b89
MS
529 if (prev == upperdentry)
530 upperopaque = true;
531 dput(this);
532 break;
533 }
a425c037 534 /*
535 * If this is a non-directory then stop here.
536 */
537 if (!S_ISDIR(this->d_inode->i_mode))
538 opaque = true;
539
3d3c6b89
MS
540 stack[ctr].dentry = this;
541 stack[ctr].mnt = lowerpath.mnt;
542 ctr++;
543 prev = this;
544 if (opaque)
545 break;
e9be9d5e
MS
546 }
547
3d3c6b89
MS
548 oe = ovl_alloc_entry(ctr);
549 err = -ENOMEM;
550 if (!oe)
551 goto out_put;
552
553 if (upperdentry || ctr) {
e9be9d5e
MS
554 struct dentry *realdentry;
555
3d3c6b89
MS
556 realdentry = upperdentry ? upperdentry : stack[0].dentry;
557
e9be9d5e
MS
558 err = -ENOMEM;
559 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
560 oe);
561 if (!inode)
3d3c6b89 562 goto out_free_oe;
e9be9d5e
MS
563 ovl_copyattr(realdentry->d_inode, inode);
564 }
565
3d3c6b89 566 oe->opaque = upperopaque;
e9be9d5e 567 oe->__upperdentry = upperdentry;
3d3c6b89
MS
568 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
569 kfree(stack);
e9be9d5e
MS
570 dentry->d_fsdata = oe;
571 d_add(dentry, inode);
572
573 return NULL;
574
3d3c6b89 575out_free_oe:
e9be9d5e 576 kfree(oe);
3d3c6b89
MS
577out_put:
578 for (i = 0; i < ctr; i++)
579 dput(stack[i].dentry);
580 kfree(stack);
581out_put_upper:
582 dput(upperdentry);
e9be9d5e
MS
583out:
584 return ERR_PTR(err);
585}
586
587struct file *ovl_path_open(struct path *path, int flags)
588{
589 return dentry_open(path, flags, current_cred());
590}
591
592static void ovl_put_super(struct super_block *sb)
593{
594 struct ovl_fs *ufs = sb->s_fs_info;
dd662667 595 unsigned i;
e9be9d5e
MS
596
597 dput(ufs->workdir);
598 mntput(ufs->upper_mnt);
dd662667
MS
599 for (i = 0; i < ufs->numlower; i++)
600 mntput(ufs->lower_mnt[i]);
5ffdbe8b 601 kfree(ufs->lower_mnt);
e9be9d5e 602
f45827e8
EZ
603 kfree(ufs->config.lowerdir);
604 kfree(ufs->config.upperdir);
605 kfree(ufs->config.workdir);
e9be9d5e
MS
606 kfree(ufs);
607}
608
cc259639
AW
609/**
610 * ovl_statfs
611 * @sb: The overlayfs super block
612 * @buf: The struct kstatfs to fill in with stats
613 *
614 * Get the filesystem statistics. As writes always target the upper layer
4ebc5818 615 * filesystem pass the statfs to the upper filesystem (if it exists)
cc259639
AW
616 */
617static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
618{
619 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
620 struct dentry *root_dentry = dentry->d_sb->s_root;
621 struct path path;
622 int err;
623
4ebc5818 624 ovl_path_real(root_dentry, &path);
cc259639
AW
625
626 err = vfs_statfs(&path, buf);
627 if (!err) {
628 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
629 buf->f_type = OVERLAYFS_SUPER_MAGIC;
630 }
631
632 return err;
633}
634
f45827e8
EZ
635/**
636 * ovl_show_options
637 *
638 * Prints the mount options for a given superblock.
639 * Returns zero; does not fail.
640 */
641static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
642{
643 struct super_block *sb = dentry->d_sb;
644 struct ovl_fs *ufs = sb->s_fs_info;
645
a068acf2 646 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
53a08cb9 647 if (ufs->config.upperdir) {
a068acf2
KC
648 seq_show_option(m, "upperdir", ufs->config.upperdir);
649 seq_show_option(m, "workdir", ufs->config.workdir);
53a08cb9 650 }
8d3095f4
MS
651 if (ufs->config.default_permissions)
652 seq_puts(m, ",default_permissions");
f45827e8
EZ
653 return 0;
654}
655
3cdf6fe9
SL
656static int ovl_remount(struct super_block *sb, int *flags, char *data)
657{
658 struct ovl_fs *ufs = sb->s_fs_info;
659
cc6f67bc 660 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
3cdf6fe9
SL
661 return -EROFS;
662
663 return 0;
664}
665
e9be9d5e
MS
666static const struct super_operations ovl_super_operations = {
667 .put_super = ovl_put_super,
cc259639 668 .statfs = ovl_statfs,
f45827e8 669 .show_options = ovl_show_options,
3cdf6fe9 670 .remount_fs = ovl_remount,
e9be9d5e
MS
671};
672
673enum {
674 OPT_LOWERDIR,
675 OPT_UPPERDIR,
676 OPT_WORKDIR,
8d3095f4 677 OPT_DEFAULT_PERMISSIONS,
e9be9d5e
MS
678 OPT_ERR,
679};
680
681static const match_table_t ovl_tokens = {
682 {OPT_LOWERDIR, "lowerdir=%s"},
683 {OPT_UPPERDIR, "upperdir=%s"},
684 {OPT_WORKDIR, "workdir=%s"},
8d3095f4 685 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
e9be9d5e
MS
686 {OPT_ERR, NULL}
687};
688
91c77947
MS
689static char *ovl_next_opt(char **s)
690{
691 char *sbegin = *s;
692 char *p;
693
694 if (sbegin == NULL)
695 return NULL;
696
697 for (p = sbegin; *p; p++) {
698 if (*p == '\\') {
699 p++;
700 if (!*p)
701 break;
702 } else if (*p == ',') {
703 *p = '\0';
704 *s = p + 1;
705 return sbegin;
706 }
707 }
708 *s = NULL;
709 return sbegin;
710}
711
e9be9d5e
MS
712static int ovl_parse_opt(char *opt, struct ovl_config *config)
713{
714 char *p;
715
91c77947 716 while ((p = ovl_next_opt(&opt)) != NULL) {
e9be9d5e
MS
717 int token;
718 substring_t args[MAX_OPT_ARGS];
719
720 if (!*p)
721 continue;
722
723 token = match_token(p, ovl_tokens, args);
724 switch (token) {
725 case OPT_UPPERDIR:
726 kfree(config->upperdir);
727 config->upperdir = match_strdup(&args[0]);
728 if (!config->upperdir)
729 return -ENOMEM;
730 break;
731
732 case OPT_LOWERDIR:
733 kfree(config->lowerdir);
734 config->lowerdir = match_strdup(&args[0]);
735 if (!config->lowerdir)
736 return -ENOMEM;
737 break;
738
739 case OPT_WORKDIR:
740 kfree(config->workdir);
741 config->workdir = match_strdup(&args[0]);
742 if (!config->workdir)
743 return -ENOMEM;
744 break;
745
8d3095f4
MS
746 case OPT_DEFAULT_PERMISSIONS:
747 config->default_permissions = true;
748 break;
749
e9be9d5e 750 default:
bead55ef 751 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
e9be9d5e
MS
752 return -EINVAL;
753 }
754 }
71cbad7e 755
756 /* Workdir is useless in non-upper mount */
757 if (!config->upperdir && config->workdir) {
758 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
759 config->workdir);
760 kfree(config->workdir);
761 config->workdir = NULL;
762 }
763
e9be9d5e
MS
764 return 0;
765}
766
767#define OVL_WORKDIR_NAME "work"
768
769static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
770 struct dentry *dentry)
771{
772 struct inode *dir = dentry->d_inode;
773 struct dentry *work;
774 int err;
775 bool retried = false;
776
777 err = mnt_want_write(mnt);
778 if (err)
779 return ERR_PTR(err);
780
5955102c 781 inode_lock_nested(dir, I_MUTEX_PARENT);
e9be9d5e
MS
782retry:
783 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
784 strlen(OVL_WORKDIR_NAME));
785
786 if (!IS_ERR(work)) {
787 struct kstat stat = {
788 .mode = S_IFDIR | 0,
789 };
790
791 if (work->d_inode) {
792 err = -EEXIST;
793 if (retried)
794 goto out_dput;
795
796 retried = true;
797 ovl_cleanup(dir, work);
798 dput(work);
799 goto retry;
800 }
801
802 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
803 if (err)
804 goto out_dput;
805 }
806out_unlock:
5955102c 807 inode_unlock(dir);
e9be9d5e
MS
808 mnt_drop_write(mnt);
809
810 return work;
811
812out_dput:
813 dput(work);
814 work = ERR_PTR(err);
815 goto out_unlock;
816}
817
91c77947
MS
818static void ovl_unescape(char *s)
819{
820 char *d = s;
821
822 for (;; s++, d++) {
823 if (*s == '\\')
824 s++;
825 *d = *s;
826 if (!*s)
827 break;
828 }
829}
830
ab508822
MS
831static int ovl_mount_dir_noesc(const char *name, struct path *path)
832{
a78d9f0d 833 int err = -EINVAL;
ab508822 834
a78d9f0d
MS
835 if (!*name) {
836 pr_err("overlayfs: empty lowerdir\n");
837 goto out;
838 }
ab508822
MS
839 err = kern_path(name, LOOKUP_FOLLOW, path);
840 if (err) {
841 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
842 goto out;
843 }
844 err = -EINVAL;
7c03b5d4 845 if (ovl_dentry_weird(path->dentry)) {
ab508822
MS
846 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
847 goto out_put;
848 }
849 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
850 pr_err("overlayfs: '%s' not a directory\n", name);
851 goto out_put;
852 }
853 return 0;
854
855out_put:
856 path_put(path);
857out:
858 return err;
859}
860
861static int ovl_mount_dir(const char *name, struct path *path)
862{
863 int err = -ENOMEM;
864 char *tmp = kstrdup(name, GFP_KERNEL);
865
866 if (tmp) {
867 ovl_unescape(tmp);
868 err = ovl_mount_dir_noesc(tmp, path);
7c03b5d4
MS
869
870 if (!err)
871 if (ovl_dentry_remote(path->dentry)) {
872 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
873 tmp);
874 path_put(path);
875 err = -EINVAL;
876 }
ab508822
MS
877 kfree(tmp);
878 }
879 return err;
880}
881
882static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
7c03b5d4 883 int *stack_depth, bool *remote)
ab508822
MS
884{
885 int err;
886 struct kstatfs statfs;
887
a78d9f0d 888 err = ovl_mount_dir_noesc(name, path);
ab508822
MS
889 if (err)
890 goto out;
891
892 err = vfs_statfs(path, &statfs);
893 if (err) {
894 pr_err("overlayfs: statfs failed on '%s'\n", name);
895 goto out_put;
896 }
897 *namelen = max(*namelen, statfs.f_namelen);
898 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
899
7c03b5d4
MS
900 if (ovl_dentry_remote(path->dentry))
901 *remote = true;
902
ab508822
MS
903 return 0;
904
905out_put:
906 path_put(path);
907out:
908 return err;
909}
910
e9be9d5e
MS
911/* Workdir should not be subdir of upperdir and vice versa */
912static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
913{
914 bool ok = false;
915
916 if (workdir != upperdir) {
917 ok = (lock_rename(workdir, upperdir) == NULL);
918 unlock_rename(workdir, upperdir);
919 }
920 return ok;
921}
922
a78d9f0d
MS
923static unsigned int ovl_split_lowerdirs(char *str)
924{
925 unsigned int ctr = 1;
926 char *s, *d;
927
928 for (s = d = str;; s++, d++) {
929 if (*s == '\\') {
930 s++;
931 } else if (*s == ':') {
932 *d = '\0';
933 ctr++;
934 continue;
935 }
936 *d = *s;
937 if (!*s)
938 break;
939 }
940 return ctr;
941}
942
e9be9d5e
MS
943static int ovl_fill_super(struct super_block *sb, void *data, int silent)
944{
53a08cb9
MS
945 struct path upperpath = { NULL, NULL };
946 struct path workpath = { NULL, NULL };
e9be9d5e
MS
947 struct dentry *root_dentry;
948 struct ovl_entry *oe;
949 struct ovl_fs *ufs;
a78d9f0d
MS
950 struct path *stack = NULL;
951 char *lowertmp;
952 char *lower;
953 unsigned int numlower;
954 unsigned int stacklen = 0;
dd662667 955 unsigned int i;
7c03b5d4 956 bool remote = false;
e9be9d5e
MS
957 int err;
958
f45827e8
EZ
959 err = -ENOMEM;
960 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
961 if (!ufs)
e9be9d5e
MS
962 goto out;
963
f45827e8
EZ
964 err = ovl_parse_opt((char *) data, &ufs->config);
965 if (err)
966 goto out_free_config;
967
e9be9d5e 968 err = -EINVAL;
53a08cb9 969 if (!ufs->config.lowerdir) {
07f2af7b
KK
970 if (!silent)
971 pr_err("overlayfs: missing 'lowerdir'\n");
e9be9d5e
MS
972 goto out_free_config;
973 }
974
53a08cb9 975 sb->s_stack_depth = 0;
cf9a6784 976 sb->s_maxbytes = MAX_LFS_FILESIZE;
53a08cb9 977 if (ufs->config.upperdir) {
53a08cb9
MS
978 if (!ufs->config.workdir) {
979 pr_err("overlayfs: missing 'workdir'\n");
980 goto out_free_config;
981 }
e9be9d5e 982
53a08cb9
MS
983 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
984 if (err)
985 goto out_free_config;
e9be9d5e 986
71cbad7e 987 /* Upper fs should not be r/o */
988 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
989 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
990 err = -EINVAL;
991 goto out_put_upperpath;
992 }
993
53a08cb9
MS
994 err = ovl_mount_dir(ufs->config.workdir, &workpath);
995 if (err)
996 goto out_put_upperpath;
997
2f83fd8c 998 err = -EINVAL;
53a08cb9
MS
999 if (upperpath.mnt != workpath.mnt) {
1000 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1001 goto out_put_workpath;
1002 }
1003 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1004 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1005 goto out_put_workpath;
1006 }
1007 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
cc259639 1008 }
a78d9f0d
MS
1009 err = -ENOMEM;
1010 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1011 if (!lowertmp)
ab508822 1012 goto out_put_workpath;
69c433ed 1013
a78d9f0d
MS
1014 err = -EINVAL;
1015 stacklen = ovl_split_lowerdirs(lowertmp);
6be4506e 1016 if (stacklen > OVL_MAX_STACK) {
1017 pr_err("overlayfs: too many lower directries, limit is %d\n",
1018 OVL_MAX_STACK);
a78d9f0d 1019 goto out_free_lowertmp;
6be4506e 1020 } else if (!ufs->config.upperdir && stacklen == 1) {
1021 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1022 goto out_free_lowertmp;
1023 }
a78d9f0d
MS
1024
1025 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1026 if (!stack)
1027 goto out_free_lowertmp;
1028
1029 lower = lowertmp;
1030 for (numlower = 0; numlower < stacklen; numlower++) {
1031 err = ovl_lower_dir(lower, &stack[numlower],
7c03b5d4
MS
1032 &ufs->lower_namelen, &sb->s_stack_depth,
1033 &remote);
a78d9f0d
MS
1034 if (err)
1035 goto out_put_lowerpath;
1036
1037 lower = strchr(lower, '\0') + 1;
1038 }
1039
69c433ed 1040 err = -EINVAL;
ab508822 1041 sb->s_stack_depth++;
69c433ed
MS
1042 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1043 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3b7a9a24 1044 goto out_put_lowerpath;
69c433ed
MS
1045 }
1046
53a08cb9
MS
1047 if (ufs->config.upperdir) {
1048 ufs->upper_mnt = clone_private_mount(&upperpath);
1049 err = PTR_ERR(ufs->upper_mnt);
1050 if (IS_ERR(ufs->upper_mnt)) {
1051 pr_err("overlayfs: failed to clone upperpath\n");
1052 goto out_put_lowerpath;
1053 }
3b7a9a24 1054
53a08cb9
MS
1055 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1056 err = PTR_ERR(ufs->workdir);
1057 if (IS_ERR(ufs->workdir)) {
cc6f67bc
MS
1058 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1059 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1060 sb->s_flags |= MS_RDONLY;
1061 ufs->workdir = NULL;
53a08cb9 1062 }
45aebeaf
VG
1063
1064 /*
1065 * Upper should support d_type, else whiteouts are visible.
1066 * Given workdir and upper are on same fs, we can do
1067 * iterate_dir() on workdir.
1068 */
1069 err = ovl_check_d_type_supported(&workpath);
1070 if (err < 0)
1071 goto out_put_workdir;
1072
1073 if (!err) {
1074 pr_err("overlayfs: upper fs needs to support d_type.\n");
1075 err = -EINVAL;
1076 goto out_put_workdir;
1077 }
e9be9d5e
MS
1078 }
1079
2f83fd8c 1080 err = -ENOMEM;
a78d9f0d 1081 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
dd662667 1082 if (ufs->lower_mnt == NULL)
3b7a9a24 1083 goto out_put_workdir;
a78d9f0d
MS
1084 for (i = 0; i < numlower; i++) {
1085 struct vfsmount *mnt = clone_private_mount(&stack[i]);
dd662667 1086
2f83fd8c 1087 err = PTR_ERR(mnt);
a78d9f0d
MS
1088 if (IS_ERR(mnt)) {
1089 pr_err("overlayfs: failed to clone lowerpath\n");
1090 goto out_put_lower_mnt;
1091 }
1092 /*
1093 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1094 * will fail instead of modifying lower fs.
1095 */
1096 mnt->mnt_flags |= MNT_READONLY;
dd662667 1097
a78d9f0d
MS
1098 ufs->lower_mnt[ufs->numlower] = mnt;
1099 ufs->numlower++;
1100 }
e9be9d5e 1101
71cbad7e 1102 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1103 if (!ufs->upper_mnt)
e9be9d5e
MS
1104 sb->s_flags |= MS_RDONLY;
1105
7c03b5d4
MS
1106 if (remote)
1107 sb->s_d_op = &ovl_reval_dentry_operations;
1108 else
1109 sb->s_d_op = &ovl_dentry_operations;
e9be9d5e
MS
1110
1111 err = -ENOMEM;
a78d9f0d 1112 oe = ovl_alloc_entry(numlower);
3b7a9a24
MS
1113 if (!oe)
1114 goto out_put_lower_mnt;
e9be9d5e 1115
3b7a9a24 1116 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
e9be9d5e 1117 if (!root_dentry)
3b7a9a24 1118 goto out_free_oe;
e9be9d5e
MS
1119
1120 mntput(upperpath.mnt);
a78d9f0d
MS
1121 for (i = 0; i < numlower; i++)
1122 mntput(stack[i].mnt);
e9be9d5e 1123 path_put(&workpath);
a78d9f0d 1124 kfree(lowertmp);
e9be9d5e
MS
1125
1126 oe->__upperdentry = upperpath.dentry;
a78d9f0d
MS
1127 for (i = 0; i < numlower; i++) {
1128 oe->lowerstack[i].dentry = stack[i].dentry;
1129 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1130 }
0f95502a 1131 kfree(stack);
e9be9d5e
MS
1132
1133 root_dentry->d_fsdata = oe;
1134
ed06e069
MS
1135 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1136 root_dentry->d_inode);
1137
cc259639 1138 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
e9be9d5e
MS
1139 sb->s_op = &ovl_super_operations;
1140 sb->s_root = root_dentry;
1141 sb->s_fs_info = ufs;
1142
1143 return 0;
1144
3b7a9a24
MS
1145out_free_oe:
1146 kfree(oe);
e9be9d5e 1147out_put_lower_mnt:
dd662667
MS
1148 for (i = 0; i < ufs->numlower; i++)
1149 mntput(ufs->lower_mnt[i]);
1150 kfree(ufs->lower_mnt);
3b7a9a24
MS
1151out_put_workdir:
1152 dput(ufs->workdir);
e9be9d5e 1153 mntput(ufs->upper_mnt);
e9be9d5e 1154out_put_lowerpath:
a78d9f0d
MS
1155 for (i = 0; i < numlower; i++)
1156 path_put(&stack[i]);
1157 kfree(stack);
1158out_free_lowertmp:
1159 kfree(lowertmp);
3b7a9a24
MS
1160out_put_workpath:
1161 path_put(&workpath);
e9be9d5e
MS
1162out_put_upperpath:
1163 path_put(&upperpath);
e9be9d5e 1164out_free_config:
f45827e8
EZ
1165 kfree(ufs->config.lowerdir);
1166 kfree(ufs->config.upperdir);
1167 kfree(ufs->config.workdir);
1168 kfree(ufs);
e9be9d5e
MS
1169out:
1170 return err;
1171}
1172
1173static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1174 const char *dev_name, void *raw_data)
1175{
1176 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1177}
1178
1179static struct file_system_type ovl_fs_type = {
1180 .owner = THIS_MODULE,
ef94b186 1181 .name = "overlay",
e9be9d5e
MS
1182 .mount = ovl_mount,
1183 .kill_sb = kill_anon_super,
1184};
ef94b186 1185MODULE_ALIAS_FS("overlay");
e9be9d5e
MS
1186
1187static int __init ovl_init(void)
1188{
1189 return register_filesystem(&ovl_fs_type);
1190}
1191
1192static void __exit ovl_exit(void)
1193{
1194 unregister_filesystem(&ovl_fs_type);
1195}
1196
1197module_init(ovl_init);
1198module_exit(ovl_exit);