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