]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/overlayfs/super.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[mirror_ubuntu-zesty-kernel.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
7c03b5d4
MS
298static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
299{
300 struct ovl_entry *oe = dentry->d_fsdata;
301 unsigned int i;
302 int ret = 1;
303
304 for (i = 0; i < oe->numlower; i++) {
305 struct dentry *d = oe->lowerstack[i].dentry;
306
307 if (d->d_flags & DCACHE_OP_REVALIDATE) {
308 ret = d->d_op->d_revalidate(d, flags);
309 if (ret < 0)
310 return ret;
311 if (!ret) {
312 if (!(flags & LOOKUP_RCU))
313 d_invalidate(d);
314 return -ESTALE;
315 }
316 }
317 }
318 return 1;
319}
320
321static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
322{
323 struct ovl_entry *oe = dentry->d_fsdata;
324 unsigned int i;
325 int ret = 1;
326
327 for (i = 0; i < oe->numlower; i++) {
328 struct dentry *d = oe->lowerstack[i].dentry;
329
330 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
331 ret = d->d_op->d_weak_revalidate(d, flags);
332 if (ret <= 0)
333 break;
334 }
335 }
336 return ret;
337}
338
e9be9d5e
MS
339static const struct dentry_operations ovl_dentry_operations = {
340 .d_release = ovl_dentry_release,
4bacc9c9 341 .d_select_inode = ovl_d_select_inode,
e9be9d5e
MS
342};
343
7c03b5d4
MS
344static const struct dentry_operations ovl_reval_dentry_operations = {
345 .d_release = ovl_dentry_release,
b5891cfa 346 .d_select_inode = ovl_d_select_inode,
7c03b5d4
MS
347 .d_revalidate = ovl_dentry_revalidate,
348 .d_weak_revalidate = ovl_dentry_weak_revalidate,
349};
350
dd662667 351static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
e9be9d5e 352{
dd662667
MS
353 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
354 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
355
356 if (oe)
357 oe->numlower = numlower;
358
359 return oe;
e9be9d5e
MS
360}
361
7c03b5d4
MS
362static bool ovl_dentry_remote(struct dentry *dentry)
363{
364 return dentry->d_flags &
365 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
366}
367
368static bool ovl_dentry_weird(struct dentry *dentry)
369{
370 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
371 DCACHE_MANAGE_TRANSIT |
372 DCACHE_OP_HASH |
373 DCACHE_OP_COMPARE);
374}
375
e9be9d5e
MS
376static inline struct dentry *ovl_lookup_real(struct dentry *dir,
377 struct qstr *name)
378{
379 struct dentry *dentry;
380
5955102c 381 inode_lock(dir->d_inode);
e9be9d5e 382 dentry = lookup_one_len(name->name, dir, name->len);
5955102c 383 inode_unlock(dir->d_inode);
e9be9d5e
MS
384
385 if (IS_ERR(dentry)) {
386 if (PTR_ERR(dentry) == -ENOENT)
387 dentry = NULL;
388 } else if (!dentry->d_inode) {
389 dput(dentry);
390 dentry = NULL;
7c03b5d4 391 } else if (ovl_dentry_weird(dentry)) {
a6f15d9a 392 dput(dentry);
7c03b5d4 393 /* Don't support traversing automounts and other weirdness */
a6f15d9a 394 dentry = ERR_PTR(-EREMOTE);
e9be9d5e
MS
395 }
396 return dentry;
397}
398
5ef88da5
MS
399/*
400 * Returns next layer in stack starting from top.
401 * Returns -1 if this is the last layer.
402 */
403int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
404{
405 struct ovl_entry *oe = dentry->d_fsdata;
406
407 BUG_ON(idx < 0);
408 if (idx == 0) {
409 ovl_path_upper(dentry, path);
410 if (path->dentry)
411 return oe->numlower ? 1 : -1;
412 idx++;
413 }
414 BUG_ON(idx > oe->numlower);
415 *path = oe->lowerstack[idx - 1];
416
417 return (idx < oe->numlower) ? idx + 1 : -1;
418}
419
e9be9d5e
MS
420struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
421 unsigned int flags)
422{
423 struct ovl_entry *oe;
3d3c6b89
MS
424 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
425 struct path *stack = NULL;
426 struct dentry *upperdir, *upperdentry = NULL;
427 unsigned int ctr = 0;
e9be9d5e 428 struct inode *inode = NULL;
3d3c6b89
MS
429 bool upperopaque = false;
430 struct dentry *this, *prev = NULL;
431 unsigned int i;
e9be9d5e
MS
432 int err;
433
3d3c6b89 434 upperdir = ovl_upperdentry_dereference(poe);
e9be9d5e 435 if (upperdir) {
3d3c6b89
MS
436 this = ovl_lookup_real(upperdir, &dentry->d_name);
437 err = PTR_ERR(this);
438 if (IS_ERR(this))
439 goto out;
440
3e01cee3 441 if (this) {
7c03b5d4
MS
442 if (unlikely(ovl_dentry_remote(this))) {
443 dput(this);
444 err = -EREMOTE;
445 goto out;
446 }
3d3c6b89
MS
447 if (ovl_is_whiteout(this)) {
448 dput(this);
449 this = NULL;
450 upperopaque = true;
3e01cee3 451 } else if (poe->numlower && ovl_is_opaquedir(this)) {
3d3c6b89 452 upperopaque = true;
e9be9d5e
MS
453 }
454 }
3d3c6b89 455 upperdentry = prev = this;
e9be9d5e 456 }
3d3c6b89
MS
457
458 if (!upperopaque && poe->numlower) {
459 err = -ENOMEM;
460 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
461 if (!stack)
462 goto out_put_upper;
e9be9d5e
MS
463 }
464
3d3c6b89
MS
465 for (i = 0; !upperopaque && i < poe->numlower; i++) {
466 bool opaque = false;
467 struct path lowerpath = poe->lowerstack[i];
468
3d3c6b89
MS
469 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
470 err = PTR_ERR(this);
09e10322
MS
471 if (IS_ERR(this)) {
472 /*
473 * If it's positive, then treat ENAMETOOLONG as ENOENT.
474 */
475 if (err == -ENAMETOOLONG && (upperdentry || ctr))
476 continue;
3d3c6b89 477 goto out_put;
09e10322 478 }
3d3c6b89
MS
479 if (!this)
480 continue;
3e01cee3
MS
481 if (ovl_is_whiteout(this)) {
482 dput(this);
483 break;
484 }
3d3c6b89 485 /*
3e01cee3
MS
486 * Only makes sense to check opaque dir if this is not the
487 * lowermost layer.
3d3c6b89 488 */
3e01cee3
MS
489 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
490 opaque = true;
a425c037 491
492 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
493 !S_ISDIR(this->d_inode->i_mode))) {
494 /*
495 * FIXME: check for upper-opaqueness maybe better done
496 * in remove code.
497 */
3d3c6b89
MS
498 if (prev == upperdentry)
499 upperopaque = true;
500 dput(this);
501 break;
502 }
a425c037 503 /*
504 * If this is a non-directory then stop here.
505 */
506 if (!S_ISDIR(this->d_inode->i_mode))
507 opaque = true;
508
3d3c6b89
MS
509 stack[ctr].dentry = this;
510 stack[ctr].mnt = lowerpath.mnt;
511 ctr++;
512 prev = this;
513 if (opaque)
514 break;
e9be9d5e
MS
515 }
516
3d3c6b89
MS
517 oe = ovl_alloc_entry(ctr);
518 err = -ENOMEM;
519 if (!oe)
520 goto out_put;
521
522 if (upperdentry || ctr) {
e9be9d5e
MS
523 struct dentry *realdentry;
524
3d3c6b89
MS
525 realdentry = upperdentry ? upperdentry : stack[0].dentry;
526
e9be9d5e
MS
527 err = -ENOMEM;
528 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
529 oe);
530 if (!inode)
3d3c6b89 531 goto out_free_oe;
e9be9d5e
MS
532 ovl_copyattr(realdentry->d_inode, inode);
533 }
534
3d3c6b89 535 oe->opaque = upperopaque;
e9be9d5e 536 oe->__upperdentry = upperdentry;
3d3c6b89
MS
537 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
538 kfree(stack);
e9be9d5e
MS
539 dentry->d_fsdata = oe;
540 d_add(dentry, inode);
541
542 return NULL;
543
3d3c6b89 544out_free_oe:
e9be9d5e 545 kfree(oe);
3d3c6b89
MS
546out_put:
547 for (i = 0; i < ctr; i++)
548 dput(stack[i].dentry);
549 kfree(stack);
550out_put_upper:
551 dput(upperdentry);
e9be9d5e
MS
552out:
553 return ERR_PTR(err);
554}
555
556struct file *ovl_path_open(struct path *path, int flags)
557{
558 return dentry_open(path, flags, current_cred());
559}
560
561static void ovl_put_super(struct super_block *sb)
562{
563 struct ovl_fs *ufs = sb->s_fs_info;
dd662667 564 unsigned i;
e9be9d5e
MS
565
566 dput(ufs->workdir);
567 mntput(ufs->upper_mnt);
dd662667
MS
568 for (i = 0; i < ufs->numlower; i++)
569 mntput(ufs->lower_mnt[i]);
5ffdbe8b 570 kfree(ufs->lower_mnt);
e9be9d5e 571
f45827e8
EZ
572 kfree(ufs->config.lowerdir);
573 kfree(ufs->config.upperdir);
574 kfree(ufs->config.workdir);
e9be9d5e
MS
575 kfree(ufs);
576}
577
cc259639
AW
578/**
579 * ovl_statfs
580 * @sb: The overlayfs super block
581 * @buf: The struct kstatfs to fill in with stats
582 *
583 * Get the filesystem statistics. As writes always target the upper layer
4ebc5818 584 * filesystem pass the statfs to the upper filesystem (if it exists)
cc259639
AW
585 */
586static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
587{
588 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
589 struct dentry *root_dentry = dentry->d_sb->s_root;
590 struct path path;
591 int err;
592
4ebc5818 593 ovl_path_real(root_dentry, &path);
cc259639
AW
594
595 err = vfs_statfs(&path, buf);
596 if (!err) {
597 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
598 buf->f_type = OVERLAYFS_SUPER_MAGIC;
599 }
600
601 return err;
602}
603
f45827e8
EZ
604/**
605 * ovl_show_options
606 *
607 * Prints the mount options for a given superblock.
608 * Returns zero; does not fail.
609 */
610static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
611{
612 struct super_block *sb = dentry->d_sb;
613 struct ovl_fs *ufs = sb->s_fs_info;
614
a068acf2 615 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
53a08cb9 616 if (ufs->config.upperdir) {
a068acf2
KC
617 seq_show_option(m, "upperdir", ufs->config.upperdir);
618 seq_show_option(m, "workdir", ufs->config.workdir);
53a08cb9 619 }
8d3095f4
MS
620 if (ufs->config.default_permissions)
621 seq_puts(m, ",default_permissions");
f45827e8
EZ
622 return 0;
623}
624
3cdf6fe9
SL
625static int ovl_remount(struct super_block *sb, int *flags, char *data)
626{
627 struct ovl_fs *ufs = sb->s_fs_info;
628
cc6f67bc 629 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
3cdf6fe9
SL
630 return -EROFS;
631
632 return 0;
633}
634
e9be9d5e
MS
635static const struct super_operations ovl_super_operations = {
636 .put_super = ovl_put_super,
cc259639 637 .statfs = ovl_statfs,
f45827e8 638 .show_options = ovl_show_options,
3cdf6fe9 639 .remount_fs = ovl_remount,
e9be9d5e
MS
640};
641
642enum {
643 OPT_LOWERDIR,
644 OPT_UPPERDIR,
645 OPT_WORKDIR,
8d3095f4 646 OPT_DEFAULT_PERMISSIONS,
e9be9d5e
MS
647 OPT_ERR,
648};
649
650static const match_table_t ovl_tokens = {
651 {OPT_LOWERDIR, "lowerdir=%s"},
652 {OPT_UPPERDIR, "upperdir=%s"},
653 {OPT_WORKDIR, "workdir=%s"},
8d3095f4 654 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
e9be9d5e
MS
655 {OPT_ERR, NULL}
656};
657
91c77947
MS
658static char *ovl_next_opt(char **s)
659{
660 char *sbegin = *s;
661 char *p;
662
663 if (sbegin == NULL)
664 return NULL;
665
666 for (p = sbegin; *p; p++) {
667 if (*p == '\\') {
668 p++;
669 if (!*p)
670 break;
671 } else if (*p == ',') {
672 *p = '\0';
673 *s = p + 1;
674 return sbegin;
675 }
676 }
677 *s = NULL;
678 return sbegin;
679}
680
e9be9d5e
MS
681static int ovl_parse_opt(char *opt, struct ovl_config *config)
682{
683 char *p;
684
91c77947 685 while ((p = ovl_next_opt(&opt)) != NULL) {
e9be9d5e
MS
686 int token;
687 substring_t args[MAX_OPT_ARGS];
688
689 if (!*p)
690 continue;
691
692 token = match_token(p, ovl_tokens, args);
693 switch (token) {
694 case OPT_UPPERDIR:
695 kfree(config->upperdir);
696 config->upperdir = match_strdup(&args[0]);
697 if (!config->upperdir)
698 return -ENOMEM;
699 break;
700
701 case OPT_LOWERDIR:
702 kfree(config->lowerdir);
703 config->lowerdir = match_strdup(&args[0]);
704 if (!config->lowerdir)
705 return -ENOMEM;
706 break;
707
708 case OPT_WORKDIR:
709 kfree(config->workdir);
710 config->workdir = match_strdup(&args[0]);
711 if (!config->workdir)
712 return -ENOMEM;
713 break;
714
8d3095f4
MS
715 case OPT_DEFAULT_PERMISSIONS:
716 config->default_permissions = true;
717 break;
718
e9be9d5e 719 default:
bead55ef 720 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
e9be9d5e
MS
721 return -EINVAL;
722 }
723 }
71cbad7e 724
725 /* Workdir is useless in non-upper mount */
726 if (!config->upperdir && config->workdir) {
727 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
728 config->workdir);
729 kfree(config->workdir);
730 config->workdir = NULL;
731 }
732
e9be9d5e
MS
733 return 0;
734}
735
736#define OVL_WORKDIR_NAME "work"
737
738static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
739 struct dentry *dentry)
740{
741 struct inode *dir = dentry->d_inode;
742 struct dentry *work;
743 int err;
744 bool retried = false;
745
746 err = mnt_want_write(mnt);
747 if (err)
748 return ERR_PTR(err);
749
5955102c 750 inode_lock_nested(dir, I_MUTEX_PARENT);
e9be9d5e
MS
751retry:
752 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
753 strlen(OVL_WORKDIR_NAME));
754
755 if (!IS_ERR(work)) {
756 struct kstat stat = {
757 .mode = S_IFDIR | 0,
758 };
759
760 if (work->d_inode) {
761 err = -EEXIST;
762 if (retried)
763 goto out_dput;
764
765 retried = true;
766 ovl_cleanup(dir, work);
767 dput(work);
768 goto retry;
769 }
770
771 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
772 if (err)
773 goto out_dput;
774 }
775out_unlock:
5955102c 776 inode_unlock(dir);
e9be9d5e
MS
777 mnt_drop_write(mnt);
778
779 return work;
780
781out_dput:
782 dput(work);
783 work = ERR_PTR(err);
784 goto out_unlock;
785}
786
91c77947
MS
787static void ovl_unescape(char *s)
788{
789 char *d = s;
790
791 for (;; s++, d++) {
792 if (*s == '\\')
793 s++;
794 *d = *s;
795 if (!*s)
796 break;
797 }
798}
799
ab508822
MS
800static int ovl_mount_dir_noesc(const char *name, struct path *path)
801{
a78d9f0d 802 int err = -EINVAL;
ab508822 803
a78d9f0d
MS
804 if (!*name) {
805 pr_err("overlayfs: empty lowerdir\n");
806 goto out;
807 }
ab508822
MS
808 err = kern_path(name, LOOKUP_FOLLOW, path);
809 if (err) {
810 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
811 goto out;
812 }
813 err = -EINVAL;
7c03b5d4 814 if (ovl_dentry_weird(path->dentry)) {
ab508822
MS
815 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
816 goto out_put;
817 }
818 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
819 pr_err("overlayfs: '%s' not a directory\n", name);
820 goto out_put;
821 }
822 return 0;
823
824out_put:
825 path_put(path);
826out:
827 return err;
828}
829
830static int ovl_mount_dir(const char *name, struct path *path)
831{
832 int err = -ENOMEM;
833 char *tmp = kstrdup(name, GFP_KERNEL);
834
835 if (tmp) {
836 ovl_unescape(tmp);
837 err = ovl_mount_dir_noesc(tmp, path);
7c03b5d4
MS
838
839 if (!err)
840 if (ovl_dentry_remote(path->dentry)) {
841 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
842 tmp);
843 path_put(path);
844 err = -EINVAL;
845 }
ab508822
MS
846 kfree(tmp);
847 }
848 return err;
849}
850
851static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
7c03b5d4 852 int *stack_depth, bool *remote)
ab508822
MS
853{
854 int err;
855 struct kstatfs statfs;
856
a78d9f0d 857 err = ovl_mount_dir_noesc(name, path);
ab508822
MS
858 if (err)
859 goto out;
860
861 err = vfs_statfs(path, &statfs);
862 if (err) {
863 pr_err("overlayfs: statfs failed on '%s'\n", name);
864 goto out_put;
865 }
866 *namelen = max(*namelen, statfs.f_namelen);
867 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
868
7c03b5d4
MS
869 if (ovl_dentry_remote(path->dentry))
870 *remote = true;
871
ab508822
MS
872 return 0;
873
874out_put:
875 path_put(path);
876out:
877 return err;
878}
879
e9be9d5e
MS
880/* Workdir should not be subdir of upperdir and vice versa */
881static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
882{
883 bool ok = false;
884
885 if (workdir != upperdir) {
886 ok = (lock_rename(workdir, upperdir) == NULL);
887 unlock_rename(workdir, upperdir);
888 }
889 return ok;
890}
891
a78d9f0d
MS
892static unsigned int ovl_split_lowerdirs(char *str)
893{
894 unsigned int ctr = 1;
895 char *s, *d;
896
897 for (s = d = str;; s++, d++) {
898 if (*s == '\\') {
899 s++;
900 } else if (*s == ':') {
901 *d = '\0';
902 ctr++;
903 continue;
904 }
905 *d = *s;
906 if (!*s)
907 break;
908 }
909 return ctr;
910}
911
e9be9d5e
MS
912static int ovl_fill_super(struct super_block *sb, void *data, int silent)
913{
53a08cb9
MS
914 struct path upperpath = { NULL, NULL };
915 struct path workpath = { NULL, NULL };
e9be9d5e
MS
916 struct dentry *root_dentry;
917 struct ovl_entry *oe;
918 struct ovl_fs *ufs;
a78d9f0d
MS
919 struct path *stack = NULL;
920 char *lowertmp;
921 char *lower;
922 unsigned int numlower;
923 unsigned int stacklen = 0;
dd662667 924 unsigned int i;
7c03b5d4 925 bool remote = false;
e9be9d5e
MS
926 int err;
927
f45827e8
EZ
928 err = -ENOMEM;
929 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
930 if (!ufs)
e9be9d5e
MS
931 goto out;
932
f45827e8
EZ
933 err = ovl_parse_opt((char *) data, &ufs->config);
934 if (err)
935 goto out_free_config;
936
e9be9d5e 937 err = -EINVAL;
53a08cb9
MS
938 if (!ufs->config.lowerdir) {
939 pr_err("overlayfs: missing 'lowerdir'\n");
e9be9d5e
MS
940 goto out_free_config;
941 }
942
53a08cb9 943 sb->s_stack_depth = 0;
cf9a6784 944 sb->s_maxbytes = MAX_LFS_FILESIZE;
53a08cb9 945 if (ufs->config.upperdir) {
53a08cb9
MS
946 if (!ufs->config.workdir) {
947 pr_err("overlayfs: missing 'workdir'\n");
948 goto out_free_config;
949 }
e9be9d5e 950
53a08cb9
MS
951 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
952 if (err)
953 goto out_free_config;
e9be9d5e 954
71cbad7e 955 /* Upper fs should not be r/o */
956 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
957 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
958 err = -EINVAL;
959 goto out_put_upperpath;
960 }
961
53a08cb9
MS
962 err = ovl_mount_dir(ufs->config.workdir, &workpath);
963 if (err)
964 goto out_put_upperpath;
965
2f83fd8c 966 err = -EINVAL;
53a08cb9
MS
967 if (upperpath.mnt != workpath.mnt) {
968 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
969 goto out_put_workpath;
970 }
971 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
972 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
973 goto out_put_workpath;
974 }
975 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
cc259639 976 }
a78d9f0d
MS
977 err = -ENOMEM;
978 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
979 if (!lowertmp)
ab508822 980 goto out_put_workpath;
69c433ed 981
a78d9f0d
MS
982 err = -EINVAL;
983 stacklen = ovl_split_lowerdirs(lowertmp);
6be4506e 984 if (stacklen > OVL_MAX_STACK) {
985 pr_err("overlayfs: too many lower directries, limit is %d\n",
986 OVL_MAX_STACK);
a78d9f0d 987 goto out_free_lowertmp;
6be4506e 988 } else if (!ufs->config.upperdir && stacklen == 1) {
989 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
990 goto out_free_lowertmp;
991 }
a78d9f0d
MS
992
993 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
994 if (!stack)
995 goto out_free_lowertmp;
996
997 lower = lowertmp;
998 for (numlower = 0; numlower < stacklen; numlower++) {
999 err = ovl_lower_dir(lower, &stack[numlower],
7c03b5d4
MS
1000 &ufs->lower_namelen, &sb->s_stack_depth,
1001 &remote);
a78d9f0d
MS
1002 if (err)
1003 goto out_put_lowerpath;
1004
1005 lower = strchr(lower, '\0') + 1;
1006 }
1007
69c433ed 1008 err = -EINVAL;
ab508822 1009 sb->s_stack_depth++;
69c433ed
MS
1010 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1011 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3b7a9a24 1012 goto out_put_lowerpath;
69c433ed
MS
1013 }
1014
53a08cb9
MS
1015 if (ufs->config.upperdir) {
1016 ufs->upper_mnt = clone_private_mount(&upperpath);
1017 err = PTR_ERR(ufs->upper_mnt);
1018 if (IS_ERR(ufs->upper_mnt)) {
1019 pr_err("overlayfs: failed to clone upperpath\n");
1020 goto out_put_lowerpath;
1021 }
3b7a9a24 1022
53a08cb9
MS
1023 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1024 err = PTR_ERR(ufs->workdir);
1025 if (IS_ERR(ufs->workdir)) {
cc6f67bc
MS
1026 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1027 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1028 sb->s_flags |= MS_RDONLY;
1029 ufs->workdir = NULL;
53a08cb9 1030 }
e9be9d5e
MS
1031 }
1032
2f83fd8c 1033 err = -ENOMEM;
a78d9f0d 1034 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
dd662667 1035 if (ufs->lower_mnt == NULL)
3b7a9a24 1036 goto out_put_workdir;
a78d9f0d
MS
1037 for (i = 0; i < numlower; i++) {
1038 struct vfsmount *mnt = clone_private_mount(&stack[i]);
dd662667 1039
2f83fd8c 1040 err = PTR_ERR(mnt);
a78d9f0d
MS
1041 if (IS_ERR(mnt)) {
1042 pr_err("overlayfs: failed to clone lowerpath\n");
1043 goto out_put_lower_mnt;
1044 }
1045 /*
1046 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1047 * will fail instead of modifying lower fs.
1048 */
1049 mnt->mnt_flags |= MNT_READONLY;
dd662667 1050
a78d9f0d
MS
1051 ufs->lower_mnt[ufs->numlower] = mnt;
1052 ufs->numlower++;
1053 }
e9be9d5e 1054
71cbad7e 1055 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1056 if (!ufs->upper_mnt)
e9be9d5e
MS
1057 sb->s_flags |= MS_RDONLY;
1058
7c03b5d4
MS
1059 if (remote)
1060 sb->s_d_op = &ovl_reval_dentry_operations;
1061 else
1062 sb->s_d_op = &ovl_dentry_operations;
e9be9d5e
MS
1063
1064 err = -ENOMEM;
a78d9f0d 1065 oe = ovl_alloc_entry(numlower);
3b7a9a24
MS
1066 if (!oe)
1067 goto out_put_lower_mnt;
e9be9d5e 1068
3b7a9a24 1069 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
e9be9d5e 1070 if (!root_dentry)
3b7a9a24 1071 goto out_free_oe;
e9be9d5e
MS
1072
1073 mntput(upperpath.mnt);
a78d9f0d
MS
1074 for (i = 0; i < numlower; i++)
1075 mntput(stack[i].mnt);
e9be9d5e 1076 path_put(&workpath);
a78d9f0d 1077 kfree(lowertmp);
e9be9d5e
MS
1078
1079 oe->__upperdentry = upperpath.dentry;
a78d9f0d
MS
1080 for (i = 0; i < numlower; i++) {
1081 oe->lowerstack[i].dentry = stack[i].dentry;
1082 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1083 }
0f95502a 1084 kfree(stack);
e9be9d5e
MS
1085
1086 root_dentry->d_fsdata = oe;
1087
ed06e069
MS
1088 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1089 root_dentry->d_inode);
1090
cc259639 1091 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
e9be9d5e
MS
1092 sb->s_op = &ovl_super_operations;
1093 sb->s_root = root_dentry;
1094 sb->s_fs_info = ufs;
1095
1096 return 0;
1097
3b7a9a24
MS
1098out_free_oe:
1099 kfree(oe);
e9be9d5e 1100out_put_lower_mnt:
dd662667
MS
1101 for (i = 0; i < ufs->numlower; i++)
1102 mntput(ufs->lower_mnt[i]);
1103 kfree(ufs->lower_mnt);
3b7a9a24
MS
1104out_put_workdir:
1105 dput(ufs->workdir);
e9be9d5e 1106 mntput(ufs->upper_mnt);
e9be9d5e 1107out_put_lowerpath:
a78d9f0d
MS
1108 for (i = 0; i < numlower; i++)
1109 path_put(&stack[i]);
1110 kfree(stack);
1111out_free_lowertmp:
1112 kfree(lowertmp);
3b7a9a24
MS
1113out_put_workpath:
1114 path_put(&workpath);
e9be9d5e
MS
1115out_put_upperpath:
1116 path_put(&upperpath);
e9be9d5e 1117out_free_config:
f45827e8
EZ
1118 kfree(ufs->config.lowerdir);
1119 kfree(ufs->config.upperdir);
1120 kfree(ufs->config.workdir);
1121 kfree(ufs);
e9be9d5e
MS
1122out:
1123 return err;
1124}
1125
1126static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1127 const char *dev_name, void *raw_data)
1128{
1129 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1130}
1131
1132static struct file_system_type ovl_fs_type = {
1133 .owner = THIS_MODULE,
ef94b186 1134 .name = "overlay",
e9be9d5e
MS
1135 .mount = ovl_mount,
1136 .kill_sb = kill_anon_super,
1137};
ef94b186 1138MODULE_ALIAS_FS("overlay");
e9be9d5e
MS
1139
1140static int __init ovl_init(void)
1141{
1142 return register_filesystem(&ovl_fs_type);
1143}
1144
1145static void __exit ovl_exit(void)
1146{
1147 unregister_filesystem(&ovl_fs_type);
1148}
1149
1150module_init(ovl_init);
1151module_exit(ovl_exit);