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