]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/overlayfs/super.c
ovl: check for emptiness of redirect dir
[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>
e9be9d5e 13#include <linux/mount.h>
e9be9d5e
MS
14#include <linux/parser.h>
15#include <linux/module.h>
cc259639 16#include <linux/statfs.h>
f45827e8 17#include <linux/seq_file.h>
d837a49b 18#include <linux/posix_acl_xattr.h>
e9be9d5e 19#include "overlayfs.h"
bbb1e54d 20#include "ovl_entry.h"
e9be9d5e
MS
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Overlay filesystem");
24MODULE_LICENSE("GPL");
25
e9be9d5e
MS
26
27struct ovl_dir_cache;
28
a78d9f0d
MS
29#define OVL_MAX_STACK 500
30
e9be9d5e
MS
31
32static void ovl_dentry_release(struct dentry *dentry)
33{
34 struct ovl_entry *oe = dentry->d_fsdata;
35
36 if (oe) {
dd662667
MS
37 unsigned int i;
38
e9be9d5e 39 dput(oe->__upperdentry);
02b69b28 40 kfree(oe->redirect);
dd662667
MS
41 for (i = 0; i < oe->numlower; i++)
42 dput(oe->lowerstack[i].dentry);
e9be9d5e
MS
43 kfree_rcu(oe, rcu);
44 }
45}
46
2d902671
MS
47static struct dentry *ovl_d_real(struct dentry *dentry,
48 const struct inode *inode,
49 unsigned int open_flags)
d101a125
MS
50{
51 struct dentry *real;
52
ca4c8a3a 53 if (!d_is_reg(dentry)) {
d101a125
MS
54 if (!inode || inode == d_inode(dentry))
55 return dentry;
56 goto bug;
57 }
58
2d902671
MS
59 if (d_is_negative(dentry))
60 return dentry;
61
62 if (open_flags) {
63 int err = ovl_open_maybe_copy_up(dentry, open_flags);
64
65 if (err)
66 return ERR_PTR(err);
67 }
68
d101a125
MS
69 real = ovl_dentry_upper(dentry);
70 if (real && (!inode || inode == d_inode(real)))
71 return real;
72
73 real = ovl_dentry_lower(dentry);
74 if (!real)
75 goto bug;
76
c4fcfc16
MS
77 /* Handle recursion */
78 real = d_real(real, inode, open_flags);
79
d101a125
MS
80 if (!inode || inode == d_inode(real))
81 return real;
d101a125 82bug:
656189d2 83 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
d101a125
MS
84 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
85 return dentry;
86}
87
7c03b5d4
MS
88static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
89{
90 struct ovl_entry *oe = dentry->d_fsdata;
91 unsigned int i;
92 int ret = 1;
93
94 for (i = 0; i < oe->numlower; i++) {
95 struct dentry *d = oe->lowerstack[i].dentry;
96
97 if (d->d_flags & DCACHE_OP_REVALIDATE) {
98 ret = d->d_op->d_revalidate(d, flags);
99 if (ret < 0)
100 return ret;
101 if (!ret) {
102 if (!(flags & LOOKUP_RCU))
103 d_invalidate(d);
104 return -ESTALE;
105 }
106 }
107 }
108 return 1;
109}
110
111static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
112{
113 struct ovl_entry *oe = dentry->d_fsdata;
114 unsigned int i;
115 int ret = 1;
116
117 for (i = 0; i < oe->numlower; i++) {
118 struct dentry *d = oe->lowerstack[i].dentry;
119
120 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
121 ret = d->d_op->d_weak_revalidate(d, flags);
122 if (ret <= 0)
123 break;
124 }
125 }
126 return ret;
127}
128
e9be9d5e
MS
129static const struct dentry_operations ovl_dentry_operations = {
130 .d_release = ovl_dentry_release,
d101a125 131 .d_real = ovl_d_real,
e9be9d5e
MS
132};
133
7c03b5d4
MS
134static const struct dentry_operations ovl_reval_dentry_operations = {
135 .d_release = ovl_dentry_release,
d101a125 136 .d_real = ovl_d_real,
7c03b5d4
MS
137 .d_revalidate = ovl_dentry_revalidate,
138 .d_weak_revalidate = ovl_dentry_weak_revalidate,
139};
140
e9be9d5e
MS
141static void ovl_put_super(struct super_block *sb)
142{
143 struct ovl_fs *ufs = sb->s_fs_info;
dd662667 144 unsigned i;
e9be9d5e
MS
145
146 dput(ufs->workdir);
147 mntput(ufs->upper_mnt);
dd662667
MS
148 for (i = 0; i < ufs->numlower; i++)
149 mntput(ufs->lower_mnt[i]);
5ffdbe8b 150 kfree(ufs->lower_mnt);
e9be9d5e 151
f45827e8
EZ
152 kfree(ufs->config.lowerdir);
153 kfree(ufs->config.upperdir);
154 kfree(ufs->config.workdir);
3fe6e52f 155 put_cred(ufs->creator_cred);
e9be9d5e
MS
156 kfree(ufs);
157}
158
cc259639
AW
159/**
160 * ovl_statfs
161 * @sb: The overlayfs super block
162 * @buf: The struct kstatfs to fill in with stats
163 *
164 * Get the filesystem statistics. As writes always target the upper layer
4ebc5818 165 * filesystem pass the statfs to the upper filesystem (if it exists)
cc259639
AW
166 */
167static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
168{
169 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
170 struct dentry *root_dentry = dentry->d_sb->s_root;
171 struct path path;
172 int err;
173
4ebc5818 174 ovl_path_real(root_dentry, &path);
cc259639
AW
175
176 err = vfs_statfs(&path, buf);
177 if (!err) {
6b2d5fe4 178 buf->f_namelen = ofs->namelen;
cc259639
AW
179 buf->f_type = OVERLAYFS_SUPER_MAGIC;
180 }
181
182 return err;
183}
184
f45827e8
EZ
185/**
186 * ovl_show_options
187 *
188 * Prints the mount options for a given superblock.
189 * Returns zero; does not fail.
190 */
191static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
192{
193 struct super_block *sb = dentry->d_sb;
194 struct ovl_fs *ufs = sb->s_fs_info;
195
a068acf2 196 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
53a08cb9 197 if (ufs->config.upperdir) {
a068acf2
KC
198 seq_show_option(m, "upperdir", ufs->config.upperdir);
199 seq_show_option(m, "workdir", ufs->config.workdir);
53a08cb9 200 }
8d3095f4
MS
201 if (ufs->config.default_permissions)
202 seq_puts(m, ",default_permissions");
f45827e8
EZ
203 return 0;
204}
205
3cdf6fe9
SL
206static int ovl_remount(struct super_block *sb, int *flags, char *data)
207{
208 struct ovl_fs *ufs = sb->s_fs_info;
209
cc6f67bc 210 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
3cdf6fe9
SL
211 return -EROFS;
212
213 return 0;
214}
215
e9be9d5e
MS
216static const struct super_operations ovl_super_operations = {
217 .put_super = ovl_put_super,
cc259639 218 .statfs = ovl_statfs,
f45827e8 219 .show_options = ovl_show_options,
3cdf6fe9 220 .remount_fs = ovl_remount,
eead4f2d 221 .drop_inode = generic_delete_inode,
e9be9d5e
MS
222};
223
224enum {
225 OPT_LOWERDIR,
226 OPT_UPPERDIR,
227 OPT_WORKDIR,
8d3095f4 228 OPT_DEFAULT_PERMISSIONS,
a6c60655
MS
229 OPT_REDIRECT_DIR_ON,
230 OPT_REDIRECT_DIR_OFF,
e9be9d5e
MS
231 OPT_ERR,
232};
233
234static const match_table_t ovl_tokens = {
235 {OPT_LOWERDIR, "lowerdir=%s"},
236 {OPT_UPPERDIR, "upperdir=%s"},
237 {OPT_WORKDIR, "workdir=%s"},
8d3095f4 238 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
a6c60655
MS
239 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
240 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
e9be9d5e
MS
241 {OPT_ERR, NULL}
242};
243
91c77947
MS
244static char *ovl_next_opt(char **s)
245{
246 char *sbegin = *s;
247 char *p;
248
249 if (sbegin == NULL)
250 return NULL;
251
252 for (p = sbegin; *p; p++) {
253 if (*p == '\\') {
254 p++;
255 if (!*p)
256 break;
257 } else if (*p == ',') {
258 *p = '\0';
259 *s = p + 1;
260 return sbegin;
261 }
262 }
263 *s = NULL;
264 return sbegin;
265}
266
e9be9d5e
MS
267static int ovl_parse_opt(char *opt, struct ovl_config *config)
268{
269 char *p;
270
91c77947 271 while ((p = ovl_next_opt(&opt)) != NULL) {
e9be9d5e
MS
272 int token;
273 substring_t args[MAX_OPT_ARGS];
274
275 if (!*p)
276 continue;
277
278 token = match_token(p, ovl_tokens, args);
279 switch (token) {
280 case OPT_UPPERDIR:
281 kfree(config->upperdir);
282 config->upperdir = match_strdup(&args[0]);
283 if (!config->upperdir)
284 return -ENOMEM;
285 break;
286
287 case OPT_LOWERDIR:
288 kfree(config->lowerdir);
289 config->lowerdir = match_strdup(&args[0]);
290 if (!config->lowerdir)
291 return -ENOMEM;
292 break;
293
294 case OPT_WORKDIR:
295 kfree(config->workdir);
296 config->workdir = match_strdup(&args[0]);
297 if (!config->workdir)
298 return -ENOMEM;
299 break;
300
8d3095f4
MS
301 case OPT_DEFAULT_PERMISSIONS:
302 config->default_permissions = true;
303 break;
304
a6c60655
MS
305 case OPT_REDIRECT_DIR_ON:
306 config->redirect_dir = true;
307 break;
308
309 case OPT_REDIRECT_DIR_OFF:
310 config->redirect_dir = false;
311 break;
312
e9be9d5e 313 default:
bead55ef 314 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
e9be9d5e
MS
315 return -EINVAL;
316 }
317 }
71cbad7e 318
319 /* Workdir is useless in non-upper mount */
320 if (!config->upperdir && config->workdir) {
321 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
322 config->workdir);
323 kfree(config->workdir);
324 config->workdir = NULL;
325 }
326
e9be9d5e
MS
327 return 0;
328}
329
330#define OVL_WORKDIR_NAME "work"
331
332static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
333 struct dentry *dentry)
334{
335 struct inode *dir = dentry->d_inode;
336 struct dentry *work;
337 int err;
338 bool retried = false;
339
340 err = mnt_want_write(mnt);
341 if (err)
342 return ERR_PTR(err);
343
5955102c 344 inode_lock_nested(dir, I_MUTEX_PARENT);
e9be9d5e
MS
345retry:
346 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
347 strlen(OVL_WORKDIR_NAME));
348
349 if (!IS_ERR(work)) {
350 struct kstat stat = {
351 .mode = S_IFDIR | 0,
352 };
c11b9fdd
MS
353 struct iattr attr = {
354 .ia_valid = ATTR_MODE,
355 .ia_mode = stat.mode,
356 };
e9be9d5e
MS
357
358 if (work->d_inode) {
359 err = -EEXIST;
360 if (retried)
361 goto out_dput;
362
363 retried = true;
eea2fb48 364 ovl_workdir_cleanup(dir, mnt, work, 0);
e9be9d5e
MS
365 dput(work);
366 goto retry;
367 }
368
369 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
370 if (err)
371 goto out_dput;
c11b9fdd 372
cb348edb
MS
373 /*
374 * Try to remove POSIX ACL xattrs from workdir. We are good if:
375 *
376 * a) success (there was a POSIX ACL xattr and was removed)
377 * b) -ENODATA (there was no POSIX ACL xattr)
378 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
379 *
380 * There are various other error values that could effectively
381 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
382 * if the xattr name is too long), but the set of filesystems
383 * allowed as upper are limited to "normal" ones, where checking
384 * for the above two errors is sufficient.
385 */
c11b9fdd 386 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
e1ff3dd1 387 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
388 goto out_dput;
389
390 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
e1ff3dd1 391 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
392 goto out_dput;
393
394 /* Clear any inherited mode bits */
395 inode_lock(work->d_inode);
396 err = notify_change(work, &attr, NULL);
397 inode_unlock(work->d_inode);
398 if (err)
399 goto out_dput;
e9be9d5e
MS
400 }
401out_unlock:
5955102c 402 inode_unlock(dir);
e9be9d5e
MS
403 mnt_drop_write(mnt);
404
405 return work;
406
407out_dput:
408 dput(work);
409 work = ERR_PTR(err);
410 goto out_unlock;
411}
412
91c77947
MS
413static void ovl_unescape(char *s)
414{
415 char *d = s;
416
417 for (;; s++, d++) {
418 if (*s == '\\')
419 s++;
420 *d = *s;
421 if (!*s)
422 break;
423 }
424}
425
ab508822
MS
426static int ovl_mount_dir_noesc(const char *name, struct path *path)
427{
a78d9f0d 428 int err = -EINVAL;
ab508822 429
a78d9f0d
MS
430 if (!*name) {
431 pr_err("overlayfs: empty lowerdir\n");
432 goto out;
433 }
ab508822
MS
434 err = kern_path(name, LOOKUP_FOLLOW, path);
435 if (err) {
436 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
437 goto out;
438 }
439 err = -EINVAL;
7c03b5d4 440 if (ovl_dentry_weird(path->dentry)) {
ab508822
MS
441 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
442 goto out_put;
443 }
2b8c30e9 444 if (!d_is_dir(path->dentry)) {
ab508822
MS
445 pr_err("overlayfs: '%s' not a directory\n", name);
446 goto out_put;
447 }
448 return 0;
449
450out_put:
451 path_put(path);
452out:
453 return err;
454}
455
456static int ovl_mount_dir(const char *name, struct path *path)
457{
458 int err = -ENOMEM;
459 char *tmp = kstrdup(name, GFP_KERNEL);
460
461 if (tmp) {
462 ovl_unescape(tmp);
463 err = ovl_mount_dir_noesc(tmp, path);
7c03b5d4
MS
464
465 if (!err)
466 if (ovl_dentry_remote(path->dentry)) {
467 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
468 tmp);
469 path_put(path);
470 err = -EINVAL;
471 }
ab508822
MS
472 kfree(tmp);
473 }
474 return err;
475}
476
6b2d5fe4
MS
477static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
478 const char *name)
ab508822 479{
ab508822 480 struct kstatfs statfs;
6b2d5fe4
MS
481 int err = vfs_statfs(path, &statfs);
482
483 if (err)
484 pr_err("overlayfs: statfs failed on '%s'\n", name);
485 else
486 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
487
488 return err;
489}
490
491static int ovl_lower_dir(const char *name, struct path *path,
492 struct ovl_fs *ofs, int *stack_depth, bool *remote)
493{
494 int err;
ab508822 495
a78d9f0d 496 err = ovl_mount_dir_noesc(name, path);
ab508822
MS
497 if (err)
498 goto out;
499
6b2d5fe4
MS
500 err = ovl_check_namelen(path, ofs, name);
501 if (err)
ab508822 502 goto out_put;
6b2d5fe4 503
ab508822
MS
504 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
505
7c03b5d4
MS
506 if (ovl_dentry_remote(path->dentry))
507 *remote = true;
508
ab508822
MS
509 return 0;
510
511out_put:
512 path_put(path);
513out:
514 return err;
515}
516
e9be9d5e
MS
517/* Workdir should not be subdir of upperdir and vice versa */
518static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
519{
520 bool ok = false;
521
522 if (workdir != upperdir) {
523 ok = (lock_rename(workdir, upperdir) == NULL);
524 unlock_rename(workdir, upperdir);
525 }
526 return ok;
527}
528
a78d9f0d
MS
529static unsigned int ovl_split_lowerdirs(char *str)
530{
531 unsigned int ctr = 1;
532 char *s, *d;
533
534 for (s = d = str;; s++, d++) {
535 if (*s == '\\') {
536 s++;
537 } else if (*s == ':') {
538 *d = '\0';
539 ctr++;
540 continue;
541 }
542 *d = *s;
543 if (!*s)
544 break;
545 }
546 return ctr;
547}
548
0eb45fc3
AG
549static int __maybe_unused
550ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
551 struct dentry *dentry, struct inode *inode,
552 const char *name, void *buffer, size_t size)
553{
554 return ovl_xattr_get(dentry, handler->name, buffer, size);
555}
556
0c97be22
AG
557static int __maybe_unused
558ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
559 struct dentry *dentry, struct inode *inode,
560 const char *name, const void *value,
561 size_t size, int flags)
d837a49b
MS
562{
563 struct dentry *workdir = ovl_workdir(dentry);
564 struct inode *realinode = ovl_inode_real(inode, NULL);
565 struct posix_acl *acl = NULL;
566 int err;
567
568 /* Check that everything is OK before copy-up */
569 if (value) {
570 acl = posix_acl_from_xattr(&init_user_ns, value, size);
571 if (IS_ERR(acl))
572 return PTR_ERR(acl);
573 }
574 err = -EOPNOTSUPP;
575 if (!IS_POSIXACL(d_inode(workdir)))
576 goto out_acl_release;
577 if (!realinode->i_op->set_acl)
578 goto out_acl_release;
579 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
580 err = acl ? -EACCES : 0;
581 goto out_acl_release;
582 }
583 err = -EPERM;
584 if (!inode_owner_or_capable(inode))
585 goto out_acl_release;
586
587 posix_acl_release(acl);
588
fd3220d3
MS
589 /*
590 * Check if sgid bit needs to be cleared (actual setacl operation will
591 * be done with mounter's capabilities and so that won't do it for us).
592 */
593 if (unlikely(inode->i_mode & S_ISGID) &&
594 handler->flags == ACL_TYPE_ACCESS &&
595 !in_group_p(inode->i_gid) &&
596 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
597 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
598
599 err = ovl_setattr(dentry, &iattr);
600 if (err)
601 return err;
602 }
603
ce31513a
MS
604 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
605 if (!err)
606 ovl_copyattr(ovl_inode_real(inode, NULL), inode);
607
608 return err;
d837a49b
MS
609
610out_acl_release:
611 posix_acl_release(acl);
612 return err;
613}
614
0eb45fc3
AG
615static int ovl_own_xattr_get(const struct xattr_handler *handler,
616 struct dentry *dentry, struct inode *inode,
617 const char *name, void *buffer, size_t size)
618{
48fab5d7 619 return -EOPNOTSUPP;
0eb45fc3
AG
620}
621
d837a49b
MS
622static int ovl_own_xattr_set(const struct xattr_handler *handler,
623 struct dentry *dentry, struct inode *inode,
624 const char *name, const void *value,
625 size_t size, int flags)
626{
48fab5d7 627 return -EOPNOTSUPP;
d837a49b
MS
628}
629
0eb45fc3
AG
630static int ovl_other_xattr_get(const struct xattr_handler *handler,
631 struct dentry *dentry, struct inode *inode,
632 const char *name, void *buffer, size_t size)
633{
634 return ovl_xattr_get(dentry, name, buffer, size);
635}
636
0e585ccc
AG
637static int ovl_other_xattr_set(const struct xattr_handler *handler,
638 struct dentry *dentry, struct inode *inode,
639 const char *name, const void *value,
640 size_t size, int flags)
641{
642 return ovl_xattr_set(dentry, name, value, size, flags);
643}
644
0c97be22
AG
645static const struct xattr_handler __maybe_unused
646ovl_posix_acl_access_xattr_handler = {
d837a49b
MS
647 .name = XATTR_NAME_POSIX_ACL_ACCESS,
648 .flags = ACL_TYPE_ACCESS,
0eb45fc3 649 .get = ovl_posix_acl_xattr_get,
d837a49b
MS
650 .set = ovl_posix_acl_xattr_set,
651};
652
0c97be22
AG
653static const struct xattr_handler __maybe_unused
654ovl_posix_acl_default_xattr_handler = {
d837a49b
MS
655 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
656 .flags = ACL_TYPE_DEFAULT,
0eb45fc3 657 .get = ovl_posix_acl_xattr_get,
d837a49b
MS
658 .set = ovl_posix_acl_xattr_set,
659};
660
661static const struct xattr_handler ovl_own_xattr_handler = {
662 .prefix = OVL_XATTR_PREFIX,
0eb45fc3 663 .get = ovl_own_xattr_get,
d837a49b
MS
664 .set = ovl_own_xattr_set,
665};
666
667static const struct xattr_handler ovl_other_xattr_handler = {
668 .prefix = "", /* catch all */
0eb45fc3 669 .get = ovl_other_xattr_get,
d837a49b
MS
670 .set = ovl_other_xattr_set,
671};
672
673static const struct xattr_handler *ovl_xattr_handlers[] = {
0c97be22 674#ifdef CONFIG_FS_POSIX_ACL
d837a49b
MS
675 &ovl_posix_acl_access_xattr_handler,
676 &ovl_posix_acl_default_xattr_handler,
0c97be22 677#endif
d837a49b
MS
678 &ovl_own_xattr_handler,
679 &ovl_other_xattr_handler,
680 NULL
681};
682
e9be9d5e
MS
683static int ovl_fill_super(struct super_block *sb, void *data, int silent)
684{
53a08cb9
MS
685 struct path upperpath = { NULL, NULL };
686 struct path workpath = { NULL, NULL };
e9be9d5e 687 struct dentry *root_dentry;
39b681f8 688 struct inode *realinode;
e9be9d5e
MS
689 struct ovl_entry *oe;
690 struct ovl_fs *ufs;
a78d9f0d
MS
691 struct path *stack = NULL;
692 char *lowertmp;
693 char *lower;
694 unsigned int numlower;
695 unsigned int stacklen = 0;
dd662667 696 unsigned int i;
7c03b5d4 697 bool remote = false;
e9be9d5e
MS
698 int err;
699
f45827e8
EZ
700 err = -ENOMEM;
701 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
702 if (!ufs)
e9be9d5e
MS
703 goto out;
704
f45827e8
EZ
705 err = ovl_parse_opt((char *) data, &ufs->config);
706 if (err)
707 goto out_free_config;
708
e9be9d5e 709 err = -EINVAL;
53a08cb9 710 if (!ufs->config.lowerdir) {
07f2af7b
KK
711 if (!silent)
712 pr_err("overlayfs: missing 'lowerdir'\n");
e9be9d5e
MS
713 goto out_free_config;
714 }
715
53a08cb9 716 sb->s_stack_depth = 0;
cf9a6784 717 sb->s_maxbytes = MAX_LFS_FILESIZE;
53a08cb9 718 if (ufs->config.upperdir) {
53a08cb9
MS
719 if (!ufs->config.workdir) {
720 pr_err("overlayfs: missing 'workdir'\n");
721 goto out_free_config;
722 }
e9be9d5e 723
53a08cb9
MS
724 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
725 if (err)
726 goto out_free_config;
e9be9d5e 727
71cbad7e 728 /* Upper fs should not be r/o */
729 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
730 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
731 err = -EINVAL;
732 goto out_put_upperpath;
733 }
734
6b2d5fe4
MS
735 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
736 if (err)
737 goto out_put_upperpath;
738
53a08cb9
MS
739 err = ovl_mount_dir(ufs->config.workdir, &workpath);
740 if (err)
741 goto out_put_upperpath;
742
2f83fd8c 743 err = -EINVAL;
53a08cb9
MS
744 if (upperpath.mnt != workpath.mnt) {
745 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
746 goto out_put_workpath;
747 }
748 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
749 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
750 goto out_put_workpath;
751 }
752 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
cc259639 753 }
a78d9f0d
MS
754 err = -ENOMEM;
755 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
756 if (!lowertmp)
ab508822 757 goto out_put_workpath;
69c433ed 758
a78d9f0d
MS
759 err = -EINVAL;
760 stacklen = ovl_split_lowerdirs(lowertmp);
6be4506e 761 if (stacklen > OVL_MAX_STACK) {
fd36570a 762 pr_err("overlayfs: too many lower directories, limit is %d\n",
6be4506e 763 OVL_MAX_STACK);
a78d9f0d 764 goto out_free_lowertmp;
6be4506e 765 } else if (!ufs->config.upperdir && stacklen == 1) {
766 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
767 goto out_free_lowertmp;
768 }
a78d9f0d
MS
769
770 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
771 if (!stack)
772 goto out_free_lowertmp;
773
774 lower = lowertmp;
775 for (numlower = 0; numlower < stacklen; numlower++) {
6b2d5fe4
MS
776 err = ovl_lower_dir(lower, &stack[numlower], ufs,
777 &sb->s_stack_depth, &remote);
a78d9f0d
MS
778 if (err)
779 goto out_put_lowerpath;
780
781 lower = strchr(lower, '\0') + 1;
782 }
783
69c433ed 784 err = -EINVAL;
ab508822 785 sb->s_stack_depth++;
69c433ed
MS
786 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
787 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3b7a9a24 788 goto out_put_lowerpath;
69c433ed
MS
789 }
790
53a08cb9
MS
791 if (ufs->config.upperdir) {
792 ufs->upper_mnt = clone_private_mount(&upperpath);
793 err = PTR_ERR(ufs->upper_mnt);
794 if (IS_ERR(ufs->upper_mnt)) {
795 pr_err("overlayfs: failed to clone upperpath\n");
796 goto out_put_lowerpath;
797 }
d719e8f2
MS
798 /* Don't inherit atime flags */
799 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
800
801 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
3b7a9a24 802
53a08cb9
MS
803 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
804 err = PTR_ERR(ufs->workdir);
805 if (IS_ERR(ufs->workdir)) {
cc6f67bc
MS
806 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
807 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
808 sb->s_flags |= MS_RDONLY;
809 ufs->workdir = NULL;
53a08cb9 810 }
45aebeaf
VG
811
812 /*
813 * Upper should support d_type, else whiteouts are visible.
814 * Given workdir and upper are on same fs, we can do
21765194
VG
815 * iterate_dir() on workdir. This check requires successful
816 * creation of workdir in previous step.
45aebeaf 817 */
21765194
VG
818 if (ufs->workdir) {
819 err = ovl_check_d_type_supported(&workpath);
820 if (err < 0)
821 goto out_put_workdir;
45aebeaf 822
e7c0b599
VG
823 /*
824 * We allowed this configuration and don't want to
825 * break users over kernel upgrade. So warn instead
826 * of erroring out.
827 */
828 if (!err)
829 pr_warn("overlayfs: upper fs needs to support d_type.\n");
45aebeaf 830 }
e9be9d5e
MS
831 }
832
2f83fd8c 833 err = -ENOMEM;
a78d9f0d 834 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
dd662667 835 if (ufs->lower_mnt == NULL)
3b7a9a24 836 goto out_put_workdir;
a78d9f0d
MS
837 for (i = 0; i < numlower; i++) {
838 struct vfsmount *mnt = clone_private_mount(&stack[i]);
dd662667 839
2f83fd8c 840 err = PTR_ERR(mnt);
a78d9f0d
MS
841 if (IS_ERR(mnt)) {
842 pr_err("overlayfs: failed to clone lowerpath\n");
843 goto out_put_lower_mnt;
844 }
845 /*
846 * Make lower_mnt R/O. That way fchmod/fchown on lower file
847 * will fail instead of modifying lower fs.
848 */
d719e8f2 849 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
dd662667 850
a78d9f0d
MS
851 ufs->lower_mnt[ufs->numlower] = mnt;
852 ufs->numlower++;
853 }
e9be9d5e 854
71cbad7e 855 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
856 if (!ufs->upper_mnt)
e9be9d5e
MS
857 sb->s_flags |= MS_RDONLY;
858
7c03b5d4
MS
859 if (remote)
860 sb->s_d_op = &ovl_reval_dentry_operations;
861 else
862 sb->s_d_op = &ovl_dentry_operations;
e9be9d5e 863
3fe6e52f
AM
864 ufs->creator_cred = prepare_creds();
865 if (!ufs->creator_cred)
866 goto out_put_lower_mnt;
867
e9be9d5e 868 err = -ENOMEM;
a78d9f0d 869 oe = ovl_alloc_entry(numlower);
3b7a9a24 870 if (!oe)
3fe6e52f 871 goto out_put_cred;
e9be9d5e 872
655042cc
VG
873 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
874 sb->s_op = &ovl_super_operations;
875 sb->s_xattr = ovl_xattr_handlers;
876 sb->s_fs_info = ufs;
877 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
878
ca4c8a3a 879 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
e9be9d5e 880 if (!root_dentry)
3b7a9a24 881 goto out_free_oe;
e9be9d5e
MS
882
883 mntput(upperpath.mnt);
a78d9f0d
MS
884 for (i = 0; i < numlower; i++)
885 mntput(stack[i].mnt);
e9be9d5e 886 path_put(&workpath);
a78d9f0d 887 kfree(lowertmp);
e9be9d5e
MS
888
889 oe->__upperdentry = upperpath.dentry;
a78d9f0d
MS
890 for (i = 0; i < numlower; i++) {
891 oe->lowerstack[i].dentry = stack[i].dentry;
892 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
893 }
0f95502a 894 kfree(stack);
e9be9d5e
MS
895
896 root_dentry->d_fsdata = oe;
897
39b681f8
MS
898 realinode = d_inode(ovl_dentry_real(root_dentry));
899 ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry);
900 ovl_copyattr(realinode, d_inode(root_dentry));
ed06e069 901
e9be9d5e 902 sb->s_root = root_dentry;
e9be9d5e
MS
903
904 return 0;
905
3b7a9a24
MS
906out_free_oe:
907 kfree(oe);
3fe6e52f
AM
908out_put_cred:
909 put_cred(ufs->creator_cred);
e9be9d5e 910out_put_lower_mnt:
dd662667
MS
911 for (i = 0; i < ufs->numlower; i++)
912 mntput(ufs->lower_mnt[i]);
913 kfree(ufs->lower_mnt);
3b7a9a24
MS
914out_put_workdir:
915 dput(ufs->workdir);
e9be9d5e 916 mntput(ufs->upper_mnt);
e9be9d5e 917out_put_lowerpath:
a78d9f0d
MS
918 for (i = 0; i < numlower; i++)
919 path_put(&stack[i]);
920 kfree(stack);
921out_free_lowertmp:
922 kfree(lowertmp);
3b7a9a24
MS
923out_put_workpath:
924 path_put(&workpath);
e9be9d5e
MS
925out_put_upperpath:
926 path_put(&upperpath);
e9be9d5e 927out_free_config:
f45827e8
EZ
928 kfree(ufs->config.lowerdir);
929 kfree(ufs->config.upperdir);
930 kfree(ufs->config.workdir);
931 kfree(ufs);
e9be9d5e
MS
932out:
933 return err;
934}
935
936static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
937 const char *dev_name, void *raw_data)
938{
939 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
940}
941
942static struct file_system_type ovl_fs_type = {
943 .owner = THIS_MODULE,
ef94b186 944 .name = "overlay",
e9be9d5e
MS
945 .mount = ovl_mount,
946 .kill_sb = kill_anon_super,
947};
ef94b186 948MODULE_ALIAS_FS("overlay");
e9be9d5e
MS
949
950static int __init ovl_init(void)
951{
952 return register_filesystem(&ovl_fs_type);
953}
954
955static void __exit ovl_exit(void)
956{
957 unregister_filesystem(&ovl_fs_type);
958}
959
960module_init(ovl_init);
961module_exit(ovl_exit);