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