Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / fs / overlayfs / super.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e9be9d5e
MS
2/*
3 *
4 * Copyright (C) 2011 Novell Inc.
e9be9d5e
MS
5 */
6
5b825c3a 7#include <uapi/linux/magic.h>
e9be9d5e
MS
8#include <linux/fs.h>
9#include <linux/namei.h>
10#include <linux/xattr.h>
e9be9d5e 11#include <linux/mount.h>
e9be9d5e
MS
12#include <linux/parser.h>
13#include <linux/module.h>
cc259639 14#include <linux/statfs.h>
f45827e8 15#include <linux/seq_file.h>
d837a49b 16#include <linux/posix_acl_xattr.h>
e487d889 17#include <linux/exportfs.h>
2b1a7746 18#include <linux/file.h>
1784fbc2 19#include <linux/fs_context.h>
dcb399de 20#include <linux/fs_parser.h>
e9be9d5e 21#include "overlayfs.h"
7fb7998b 22#include "params.h"
e9be9d5e
MS
23
24MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
25MODULE_DESCRIPTION("Overlay filesystem");
26MODULE_LICENSE("GPL");
27
e9be9d5e
MS
28
29struct ovl_dir_cache;
30
11b3f8ae 31static struct dentry *ovl_d_real(struct dentry *dentry, enum d_real_type type)
d101a125 32{
11b3f8ae 33 struct dentry *upper, *lower;
184996e9 34 int err;
d101a125 35
11b3f8ae
AG
36 switch (type) {
37 case D_REAL_DATA:
38 case D_REAL_METADATA:
39 break;
40 default:
def3ae83 41 goto bug;
11b3f8ae 42 }
e8c985ba 43
ca4c8a3a 44 if (!d_is_reg(dentry)) {
def3ae83
AG
45 /* d_real_inode() is only relevant for regular files */
46 return dentry;
d101a125
MS
47 }
48
11b3f8ae
AG
49 upper = ovl_dentry_upper(dentry);
50 if (upper && (type == D_REAL_METADATA ||
51 ovl_has_upperdata(d_inode(dentry))))
52 return upper;
d101a125 53
11b3f8ae
AG
54 if (type == D_REAL_METADATA) {
55 lower = ovl_dentry_lower(dentry);
56 goto real_lower;
57 }
2c3d7358 58
41665644 59 /*
11b3f8ae 60 * Best effort lazy lookup of lowerdata for D_REAL_DATA case to return
41665644 61 * the real lowerdata dentry. The only current caller of d_real() with
11b3f8ae 62 * D_REAL_DATA is d_real_inode() from trace_uprobe and this caller is
41665644
AG
63 * likely going to be followed reading from the file, before placing
64 * uprobes on offset within the file, so lowerdata should be available
65 * when setting the uprobe.
66 */
184996e9
AL
67 err = ovl_verify_lowerdata(dentry);
68 if (err)
69 goto bug;
cef4cbff
MS
70 lower = ovl_dentry_lowerdata(dentry);
71 if (!lower)
d101a125
MS
72 goto bug;
73
11b3f8ae
AG
74real_lower:
75 /* Handle recursion into stacked lower fs */
76 return d_real(lower, type);
c4fcfc16 77
d101a125 78bug:
11b3f8ae 79 WARN(1, "%s(%pd4, %d): real dentry not found\n", __func__, dentry, type);
d101a125
MS
80 return dentry;
81}
82
3bb7df92 83static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
7c03b5d4 84{
7c03b5d4
MS
85 int ret = 1;
86
41665644
AG
87 if (!d)
88 return 1;
89
3bb7df92
MS
90 if (weak) {
91 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
92 ret = d->d_op->d_weak_revalidate(d, flags);
93 } else if (d->d_flags & DCACHE_OP_REVALIDATE) {
5be1fa8a
AV
94 struct dentry *parent;
95 struct inode *dir;
96 struct name_snapshot n;
97
98 if (flags & LOOKUP_RCU) {
99 parent = READ_ONCE(d->d_parent);
100 dir = d_inode_rcu(parent);
101 if (!dir)
102 return -ECHILD;
103 } else {
104 parent = dget_parent(d);
105 dir = d_inode(parent);
106 }
107 take_dentry_name_snapshot(&n, d);
108 ret = d->d_op->d_revalidate(dir, &n.name, d, flags);
109 release_dentry_name_snapshot(&n);
110 if (!(flags & LOOKUP_RCU))
111 dput(parent);
3bb7df92
MS
112 if (!ret) {
113 if (!(flags & LOOKUP_RCU))
114 d_invalidate(d);
115 ret = -ESTALE;
7c03b5d4
MS
116 }
117 }
3bb7df92 118 return ret;
7c03b5d4
MS
119}
120
3bb7df92
MS
121static int ovl_dentry_revalidate_common(struct dentry *dentry,
122 unsigned int flags, bool weak)
7c03b5d4 123{
c54719c9
AV
124 struct ovl_entry *oe;
125 struct ovl_path *lowerstack;
672e4268 126 struct inode *inode = d_inode_rcu(dentry);
bccece1e 127 struct dentry *upper;
7c03b5d4
MS
128 unsigned int i;
129 int ret = 1;
130
672e4268
CZ
131 /* Careful in RCU mode */
132 if (!inode)
133 return -ECHILD;
134
c54719c9
AV
135 oe = OVL_I_E(inode);
136 lowerstack = ovl_lowerstack(oe);
672e4268 137 upper = ovl_i_dentry_upper(inode);
bccece1e
MS
138 if (upper)
139 ret = ovl_revalidate_real(upper, flags, weak);
140
5522c9c7
AG
141 for (i = 0; ret > 0 && i < ovl_numlower(oe); i++)
142 ret = ovl_revalidate_real(lowerstack[i].dentry, flags, weak);
143
7c03b5d4
MS
144 return ret;
145}
146
5be1fa8a
AV
147static int ovl_dentry_revalidate(struct inode *dir, const struct qstr *name,
148 struct dentry *dentry, unsigned int flags)
3bb7df92
MS
149{
150 return ovl_dentry_revalidate_common(dentry, flags, false);
151}
152
153static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
154{
155 return ovl_dentry_revalidate_common(dentry, flags, true);
156}
157
e9be9d5e 158static const struct dentry_operations ovl_dentry_operations = {
d101a125 159 .d_real = ovl_d_real,
7c03b5d4
MS
160 .d_revalidate = ovl_dentry_revalidate,
161 .d_weak_revalidate = ovl_dentry_weak_revalidate,
162};
163
13cf199d
AG
164static struct kmem_cache *ovl_inode_cachep;
165
166static struct inode *ovl_alloc_inode(struct super_block *sb)
167{
fd60b288 168 struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL);
13cf199d 169
b3885bd6
HN
170 if (!oi)
171 return NULL;
172
04a01ac7 173 oi->cache = NULL;
cf31c463 174 oi->redirect = NULL;
04a01ac7 175 oi->version = 0;
13c72075 176 oi->flags = 0;
09d8b586 177 oi->__upperdentry = NULL;
2b21da92 178 oi->lowerdata_redirect = NULL;
0af950f5 179 oi->oe = NULL;
a015dafc 180 mutex_init(&oi->lock);
25b7713a 181
13cf199d
AG
182 return &oi->vfs_inode;
183}
184
0b269ded 185static void ovl_free_inode(struct inode *inode)
13cf199d 186{
0b269ded 187 struct ovl_inode *oi = OVL_I(inode);
13cf199d 188
0b269ded 189 kfree(oi->redirect);
d9e8319a 190 kfree(oi->oe);
0b269ded
AV
191 mutex_destroy(&oi->lock);
192 kmem_cache_free(ovl_inode_cachep, oi);
13cf199d
AG
193}
194
195static void ovl_destroy_inode(struct inode *inode)
196{
09d8b586
MS
197 struct ovl_inode *oi = OVL_I(inode);
198
199 dput(oi->__upperdentry);
d9e8319a 200 ovl_stack_put(ovl_lowerstack(oi->oe), ovl_numlower(oi->oe));
2664bd08
VG
201 if (S_ISDIR(inode->i_mode))
202 ovl_dir_cache_free(inode);
2b21da92
AG
203 else
204 kfree(oi->lowerdata_redirect);
13cf199d
AG
205}
206
a9075cdb
MS
207static void ovl_put_super(struct super_block *sb)
208{
f01d0889 209 struct ovl_fs *ofs = OVL_FS(sb);
a9075cdb 210
1784fbc2
CB
211 if (ofs)
212 ovl_free_fs(ofs);
a9075cdb
MS
213}
214
e8d4bfe3 215/* Sync real dirty inodes in upper filesystem (if it exists) */
e593b2bf
AG
216static int ovl_sync_fs(struct super_block *sb, int wait)
217{
f01d0889 218 struct ovl_fs *ofs = OVL_FS(sb);
e593b2bf
AG
219 struct super_block *upper_sb;
220 int ret;
221
335d3fc5 222 ret = ovl_sync_status(ofs);
34b4540e
HX
223
224 if (ret < 0)
335d3fc5 225 return -EIO;
335d3fc5
SD
226
227 if (!ret)
228 return ret;
e8d4bfe3
CX
229
230 /*
32b1924b
KK
231 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
232 * All the super blocks will be iterated, including upper_sb.
e8d4bfe3
CX
233 *
234 * If this is a syncfs(2) call, then we do need to call
235 * sync_filesystem() on upper_sb, but enough if we do it when being
236 * called with wait == 1.
237 */
238 if (!wait)
e593b2bf
AG
239 return 0;
240
08f4c7c8 241 upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
e8d4bfe3 242
e593b2bf 243 down_read(&upper_sb->s_umount);
e8d4bfe3 244 ret = sync_filesystem(upper_sb);
e593b2bf 245 up_read(&upper_sb->s_umount);
e8d4bfe3 246
e593b2bf
AG
247 return ret;
248}
249
cc259639
AW
250/**
251 * ovl_statfs
9c5dd803 252 * @dentry: The dentry to query
cc259639
AW
253 * @buf: The struct kstatfs to fill in with stats
254 *
255 * Get the filesystem statistics. As writes always target the upper layer
4ebc5818 256 * filesystem pass the statfs to the upper filesystem (if it exists)
cc259639
AW
257 */
258static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
259{
b0504bfe
AG
260 struct super_block *sb = dentry->d_sb;
261 struct ovl_fs *ofs = OVL_FS(sb);
262 struct dentry *root_dentry = sb->s_root;
cc259639
AW
263 struct path path;
264 int err;
265
4ebc5818 266 ovl_path_real(root_dentry, &path);
cc259639
AW
267
268 err = vfs_statfs(&path, buf);
269 if (!err) {
6b2d5fe4 270 buf->f_namelen = ofs->namelen;
cc259639 271 buf->f_type = OVERLAYFS_SUPER_MAGIC;
b0504bfe
AG
272 if (ovl_has_fsid(ofs))
273 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
cc259639
AW
274 }
275
276 return err;
277}
278
e9be9d5e 279static const struct super_operations ovl_super_operations = {
13cf199d 280 .alloc_inode = ovl_alloc_inode,
0b269ded 281 .free_inode = ovl_free_inode,
13cf199d
AG
282 .destroy_inode = ovl_destroy_inode,
283 .drop_inode = generic_delete_inode,
e9be9d5e 284 .put_super = ovl_put_super,
e593b2bf 285 .sync_fs = ovl_sync_fs,
cc259639 286 .statfs = ovl_statfs,
f45827e8 287 .show_options = ovl_show_options,
e9be9d5e
MS
288};
289
e9be9d5e 290#define OVL_WORKDIR_NAME "work"
02bcd157 291#define OVL_INDEXDIR_NAME "index"
e9be9d5e 292
ad204488 293static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
6b8aa129 294 const char *name, bool persist)
e9be9d5e 295{
ad204488 296 struct inode *dir = ofs->workbasedir->d_inode;
08f4c7c8 297 struct vfsmount *mnt = ovl_upper_mnt(ofs);
e9be9d5e
MS
298 struct dentry *work;
299 int err;
300 bool retried = false;
301
5955102c 302 inode_lock_nested(dir, I_MUTEX_PARENT);
e9be9d5e 303retry:
22f289ce 304 work = ovl_lookup_upper(ofs, name, ofs->workbasedir, strlen(name));
e9be9d5e
MS
305
306 if (!IS_ERR(work)) {
c11b9fdd
MS
307 struct iattr attr = {
308 .ia_valid = ATTR_MODE,
32a3d848 309 .ia_mode = S_IFDIR | 0,
c11b9fdd 310 };
e9be9d5e
MS
311
312 if (work->d_inode) {
313 err = -EEXIST;
314 if (retried)
315 goto out_dput;
316
6b8aa129
AG
317 if (persist)
318 goto out_unlock;
319
e9be9d5e 320 retried = true;
576bb263 321 err = ovl_workdir_cleanup(ofs, dir, mnt, work, 0);
e9be9d5e 322 dput(work);
235ce9ed
AG
323 if (err == -EINVAL) {
324 work = ERR_PTR(err);
325 goto out_unlock;
326 }
e9be9d5e
MS
327 goto retry;
328 }
329
c54b3869
N
330 work = ovl_do_mkdir(ofs, dir, work, attr.ia_mode);
331 err = PTR_ERR(work);
332 if (IS_ERR(work))
333 goto out_err;
1f5573cf
MS
334
335 /* Weird filesystem returning with hashed negative (kernfs)? */
336 err = -EINVAL;
337 if (d_really_is_negative(work))
338 goto out_dput;
c11b9fdd 339
cb348edb
MS
340 /*
341 * Try to remove POSIX ACL xattrs from workdir. We are good if:
342 *
343 * a) success (there was a POSIX ACL xattr and was removed)
344 * b) -ENODATA (there was no POSIX ACL xattr)
345 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
346 *
347 * There are various other error values that could effectively
348 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
349 * if the xattr name is too long), but the set of filesystems
350 * allowed as upper are limited to "normal" ones, where checking
351 * for the above two errors is sufficient.
352 */
31acceb9 353 err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_DEFAULT);
e1ff3dd1 354 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
355 goto out_dput;
356
31acceb9 357 err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_ACCESS);
e1ff3dd1 358 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
359 goto out_dput;
360
361 /* Clear any inherited mode bits */
362 inode_lock(work->d_inode);
a15506ea 363 err = ovl_do_notify_change(ofs, work, &attr);
c11b9fdd
MS
364 inode_unlock(work->d_inode);
365 if (err)
366 goto out_dput;
6b8aa129
AG
367 } else {
368 err = PTR_ERR(work);
369 goto out_err;
e9be9d5e
MS
370 }
371out_unlock:
2068cf7d 372 inode_unlock(dir);
e9be9d5e
MS
373 return work;
374
375out_dput:
376 dput(work);
6b8aa129 377out_err:
1bd0a3ae 378 pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
ad204488 379 ofs->config.workdir, name, -err);
6b8aa129 380 work = NULL;
e9be9d5e
MS
381 goto out_unlock;
382}
383
2d343087 384static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs,
6b2d5fe4 385 const char *name)
ab508822 386{
ab508822 387 struct kstatfs statfs;
6b2d5fe4
MS
388 int err = vfs_statfs(path, &statfs);
389
390 if (err)
1bd0a3ae 391 pr_err("statfs failed on '%s'\n", name);
6b2d5fe4
MS
392 else
393 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
394
395 return err;
396}
397
398static int ovl_lower_dir(const char *name, struct path *path,
f4288844 399 struct ovl_fs *ofs, int *stack_depth)
6b2d5fe4 400{
e487d889 401 int fh_type;
6b2d5fe4 402 int err;
ab508822 403
6b2d5fe4
MS
404 err = ovl_check_namelen(path, ofs, name);
405 if (err)
b8e42a65 406 return err;
6b2d5fe4 407
ab508822
MS
408 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
409
02bcd157 410 /*
f168f109
AG
411 * The inodes index feature and NFS export need to encode and decode
412 * file handles, so they require that all layers support them.
02bcd157 413 */
e487d889 414 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
f168f109 415 if ((ofs->config.nfs_export ||
e487d889 416 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
02bcd157 417 ofs->config.index = false;
f168f109 418 ofs->config.nfs_export = false;
1bd0a3ae 419 pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
f168f109 420 name);
02bcd157 421 }
16aac5ad 422 ofs->nofh |= !fh_type;
b0e0f697
AG
423 /*
424 * Decoding origin file handle is required for persistent st_ino.
425 * Without persistent st_ino, xino=auto falls back to xino=off.
426 */
427 if (ofs->config.xino == OVL_XINO_AUTO &&
428 ofs->config.upperdir && !fh_type) {
429 ofs->config.xino = OVL_XINO_OFF;
430 pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n",
431 name);
432 }
02bcd157 433
e487d889
AG
434 /* Check if lower fs has 32bit inode numbers */
435 if (fh_type != FILEID_INO32_GEN)
0f831ec8 436 ofs->xino_mode = -1;
e487d889 437
ab508822 438 return 0;
ab508822
MS
439}
440
e9be9d5e
MS
441/* Workdir should not be subdir of upperdir and vice versa */
442static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
443{
444 bool ok = false;
445
446 if (workdir != upperdir) {
a8b00268
AV
447 struct dentry *trap = lock_rename(workdir, upperdir);
448 if (!IS_ERR(trap))
449 unlock_rename(workdir, upperdir);
450 ok = (trap == NULL);
e9be9d5e
MS
451 }
452 return ok;
453}
454
146d62e5
AG
455static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
456 struct inode **ptrap, const char *name)
457{
458 struct inode *trap;
459 int err;
460
461 trap = ovl_get_trap_inode(sb, dir);
1dac6f5b
AB
462 err = PTR_ERR_OR_ZERO(trap);
463 if (err) {
146d62e5 464 if (err == -ELOOP)
1bd0a3ae 465 pr_err("conflicting %s path\n", name);
146d62e5
AG
466 return err;
467 }
468
469 *ptrap = trap;
470 return 0;
471}
472
0be0bfd2
AG
473/*
474 * Determine how we treat concurrent use of upperdir/workdir based on the
475 * index feature. This is papering over mount leaks of container runtimes,
476 * for example, an old overlay mount is leaked and now its upperdir is
477 * attempted to be used as a lower layer in a new overlay mount.
478 */
479static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
480{
481 if (ofs->config.index) {
1bd0a3ae 482 pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
0be0bfd2
AG
483 name);
484 return -EBUSY;
485 } else {
1bd0a3ae 486 pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
0be0bfd2
AG
487 name);
488 return 0;
489 }
490}
491
146d62e5 492static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
b36a5780
CB
493 struct ovl_layer *upper_layer,
494 const struct path *upperpath)
6ee8acf0 495{
5064975e 496 struct vfsmount *upper_mnt;
6ee8acf0
MS
497 int err;
498
e21a6c57
AG
499 /* Upperdir path should not be r/o */
500 if (__mnt_is_readonly(upperpath->mnt)) {
1bd0a3ae 501 pr_err("upper fs is r/o, try multi-lower layers mount\n");
6ee8acf0
MS
502 err = -EINVAL;
503 goto out;
504 }
505
ad204488 506 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
6ee8acf0
MS
507 if (err)
508 goto out;
509
b8e42a65 510 err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
146d62e5
AG
511 "upperdir");
512 if (err)
513 goto out;
514
5064975e
MS
515 upper_mnt = clone_private_mount(upperpath);
516 err = PTR_ERR(upper_mnt);
517 if (IS_ERR(upper_mnt)) {
1bd0a3ae 518 pr_err("failed to clone upperpath\n");
5064975e
MS
519 goto out;
520 }
521
522 /* Don't inherit atime flags */
523 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
b8e42a65
MS
524 upper_layer->mnt = upper_mnt;
525 upper_layer->idx = 0;
526 upper_layer->fsid = 0;
8c25741a 527
654255fa
JX
528 /*
529 * Inherit SB_NOSEC flag from upperdir.
530 *
531 * This optimization changes behavior when a security related attribute
532 * (suid/sgid/security.*) is changed on an underlying layer. This is
533 * okay because we don't yet have guarantees in that case, but it will
534 * need careful treatment once we want to honour changes to underlying
535 * filesystems.
536 */
537 if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
538 sb->s_flags |= SB_NOSEC;
539
08f4c7c8 540 if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
8c25741a 541 ofs->upperdir_locked = true;
8c25741a 542 } else {
0be0bfd2
AG
543 err = ovl_report_in_use(ofs, "upperdir");
544 if (err)
545 goto out;
8c25741a
MS
546 }
547
6ee8acf0
MS
548 err = 0;
549out:
550 return err;
551}
552
cad218ab
AG
553/*
554 * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
555 * negative values if error is encountered.
556 */
576bb263 557static int ovl_check_rename_whiteout(struct ovl_fs *ofs)
cad218ab 558{
576bb263 559 struct dentry *workdir = ofs->workdir;
cad218ab
AG
560 struct inode *dir = d_inode(workdir);
561 struct dentry *temp;
562 struct dentry *dest;
563 struct dentry *whiteout;
564 struct name_snapshot name;
565 int err;
566
567 inode_lock_nested(dir, I_MUTEX_PARENT);
568
576bb263 569 temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0));
cad218ab
AG
570 err = PTR_ERR(temp);
571 if (IS_ERR(temp))
572 goto out_unlock;
573
576bb263 574 dest = ovl_lookup_temp(ofs, workdir);
cad218ab
AG
575 err = PTR_ERR(dest);
576 if (IS_ERR(dest)) {
577 dput(temp);
578 goto out_unlock;
579 }
580
581 /* Name is inline and stable - using snapshot as a copy helper */
582 take_dentry_name_snapshot(&name, temp);
576bb263 583 err = ovl_do_rename(ofs, dir, temp, dir, dest, RENAME_WHITEOUT);
cad218ab
AG
584 if (err) {
585 if (err == -EINVAL)
586 err = 0;
587 goto cleanup_temp;
588 }
589
22f289ce 590 whiteout = ovl_lookup_upper(ofs, name.name.name, workdir, name.name.len);
cad218ab
AG
591 err = PTR_ERR(whiteout);
592 if (IS_ERR(whiteout))
593 goto cleanup_temp;
594
bc8df7a3 595 err = ovl_upper_is_whiteout(ofs, whiteout);
cad218ab
AG
596
597 /* Best effort cleanup of whiteout and temp file */
598 if (err)
576bb263 599 ovl_cleanup(ofs, dir, whiteout);
cad218ab
AG
600 dput(whiteout);
601
602cleanup_temp:
576bb263 603 ovl_cleanup(ofs, dir, temp);
cad218ab
AG
604 release_dentry_name_snapshot(&name);
605 dput(temp);
606 dput(dest);
607
608out_unlock:
609 inode_unlock(dir);
610
611 return err;
612}
613
576bb263
CB
614static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
615 struct dentry *parent,
c86243b0
VG
616 const char *name, umode_t mode)
617{
618 size_t len = strlen(name);
619 struct dentry *child;
620
621 inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
22f289ce 622 child = ovl_lookup_upper(ofs, name, parent, len);
c86243b0 623 if (!IS_ERR(child) && !child->d_inode)
576bb263 624 child = ovl_create_real(ofs, parent->d_inode, child,
c86243b0
VG
625 OVL_CATTR(mode));
626 inode_unlock(parent->d_inode);
627 dput(parent);
628
629 return child;
630}
631
632/*
633 * Creates $workdir/work/incompat/volatile/dirty file if it is not already
634 * present.
635 */
636static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
637{
638 unsigned int ctr;
639 struct dentry *d = dget(ofs->workbasedir);
640 static const char *const volatile_path[] = {
641 OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
642 };
643 const char *const *name = volatile_path;
644
645 for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
576bb263 646 d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
c86243b0
VG
647 if (IS_ERR(d))
648 return PTR_ERR(d);
649 }
650 dput(d);
651 return 0;
652}
653
146d62e5 654static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
2d343087 655 const struct path *workpath)
8ed61dc3 656{
08f4c7c8 657 struct vfsmount *mnt = ovl_upper_mnt(ofs);
2b1a7746
MS
658 struct dentry *workdir;
659 struct file *tmpfile;
d80172c2
AG
660 bool rename_whiteout;
661 bool d_type;
e487d889 662 int fh_type;
8ed61dc3
MS
663 int err;
664
2ba9d57e
AG
665 err = mnt_want_write(mnt);
666 if (err)
667 return err;
668
235ce9ed
AG
669 workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
670 err = PTR_ERR(workdir);
671 if (IS_ERR_OR_NULL(workdir))
2ba9d57e 672 goto out;
8ed61dc3 673
235ce9ed
AG
674 ofs->workdir = workdir;
675
146d62e5
AG
676 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
677 if (err)
678 goto out;
679
8ed61dc3
MS
680 /*
681 * Upper should support d_type, else whiteouts are visible. Given
682 * workdir and upper are on same fs, we can do iterate_dir() on
683 * workdir. This check requires successful creation of workdir in
684 * previous step.
685 */
686 err = ovl_check_d_type_supported(workpath);
687 if (err < 0)
2ba9d57e 688 goto out;
8ed61dc3 689
d80172c2
AG
690 d_type = err;
691 if (!d_type)
1bd0a3ae 692 pr_warn("upper fs needs to support d_type.\n");
8ed61dc3
MS
693
694 /* Check if upper/work fs supports O_TMPFILE */
2b1a7746
MS
695 tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0);
696 ofs->tmpfile = !IS_ERR(tmpfile);
ad204488 697 if (ofs->tmpfile)
2b1a7746 698 fput(tmpfile);
8ed61dc3 699 else
1bd0a3ae 700 pr_warn("upper fs does not support tmpfile.\n");
8ed61dc3 701
cad218ab
AG
702
703 /* Check if upper/work fs supports RENAME_WHITEOUT */
576bb263 704 err = ovl_check_rename_whiteout(ofs);
cad218ab
AG
705 if (err < 0)
706 goto out;
707
d80172c2
AG
708 rename_whiteout = err;
709 if (!rename_whiteout)
cad218ab
AG
710 pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
711
8ed61dc3 712 /*
2d2f2d73 713 * Check if upper/work fs supports (trusted|user).overlay.* xattr
8ed61dc3 714 */
c914c0e2 715 err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
8ed61dc3 716 if (err) {
b10b85fe 717 pr_warn("failed to set xattr on upper\n");
ad204488 718 ofs->noxattr = true;
af5f2396
AG
719 if (ovl_redirect_follow(ofs)) {
720 ofs->config.redirect_mode = OVL_REDIRECT_NOFOLLOW;
721 pr_warn("...falling back to redirect_dir=nofollow.\n");
722 }
723 if (ofs->config.metacopy) {
b0e0f697 724 ofs->config.metacopy = false;
af5f2396
AG
725 pr_warn("...falling back to metacopy=off.\n");
726 }
727 if (ofs->config.index) {
728 ofs->config.index = false;
729 pr_warn("...falling back to index=off.\n");
b0e0f697 730 }
d9544c1b
AG
731 if (ovl_has_fsid(ofs)) {
732 ofs->config.uuid = OVL_UUID_NULL;
733 pr_warn("...falling back to uuid=null.\n");
734 }
b0e0f697
AG
735 /*
736 * xattr support is required for persistent st_ino.
737 * Without persistent st_ino, xino=auto falls back to xino=off.
738 */
739 if (ofs->config.xino == OVL_XINO_AUTO) {
740 ofs->config.xino = OVL_XINO_OFF;
b10b85fe 741 pr_warn("...falling back to xino=off.\n");
b0e0f697 742 }
b10b85fe
MS
743 if (err == -EPERM && !ofs->config.userxattr)
744 pr_info("try mounting with 'userxattr' option\n");
2ba9d57e 745 err = 0;
8ed61dc3 746 } else {
c914c0e2 747 ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
8ed61dc3
MS
748 }
749
d80172c2
AG
750 /*
751 * We allowed sub-optimal upper fs configuration and don't want to break
752 * users over kernel upgrade, but we never allowed remote upper fs, so
753 * we can enforce strict requirements for remote upper fs.
754 */
755 if (ovl_dentry_remote(ofs->workdir) &&
756 (!d_type || !rename_whiteout || ofs->noxattr)) {
757 pr_err("upper fs missing required features.\n");
758 err = -EINVAL;
759 goto out;
760 }
761
c86243b0
VG
762 /*
763 * For volatile mount, create a incompat/volatile/dirty file to keep
764 * track of it.
765 */
766 if (ofs->config.ovl_volatile) {
767 err = ovl_create_volatile_dirty(ofs);
768 if (err < 0) {
769 pr_err("Failed to create volatile/dirty file.\n");
770 goto out;
771 }
772 }
773
8ed61dc3 774 /* Check if upper/work fs supports file handles */
e487d889
AG
775 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
776 if (ofs->config.index && !fh_type) {
ad204488 777 ofs->config.index = false;
1bd0a3ae 778 pr_warn("upper fs does not support file handles, falling back to index=off.\n");
8ed61dc3 779 }
16aac5ad 780 ofs->nofh |= !fh_type;
8ed61dc3 781
e487d889
AG
782 /* Check if upper fs has 32bit inode numbers */
783 if (fh_type != FILEID_INO32_GEN)
0f831ec8 784 ofs->xino_mode = -1;
e487d889 785
f168f109
AG
786 /* NFS export of r/w mount depends on index */
787 if (ofs->config.nfs_export && !ofs->config.index) {
1bd0a3ae 788 pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
f168f109
AG
789 ofs->config.nfs_export = false;
790 }
2ba9d57e
AG
791out:
792 mnt_drop_write(mnt);
793 return err;
8ed61dc3
MS
794}
795
146d62e5 796static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
b36a5780
CB
797 const struct path *upperpath,
798 const struct path *workpath)
520d7c86
MS
799{
800 int err;
520d7c86
MS
801
802 err = -EINVAL;
b36a5780 803 if (upperpath->mnt != workpath->mnt) {
1bd0a3ae 804 pr_err("workdir and upperdir must reside under the same mount\n");
b36a5780 805 return err;
520d7c86 806 }
b36a5780 807 if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) {
1bd0a3ae 808 pr_err("workdir and upperdir must be separate subtrees\n");
b36a5780 809 return err;
520d7c86
MS
810 }
811
b36a5780 812 ofs->workbasedir = dget(workpath->dentry);
8c25741a 813
8c25741a 814 if (ovl_inuse_trylock(ofs->workbasedir)) {
ad204488 815 ofs->workdir_locked = true;
520d7c86 816 } else {
0be0bfd2
AG
817 err = ovl_report_in_use(ofs, "workdir");
818 if (err)
b36a5780 819 return err;
520d7c86
MS
820 }
821
0be0bfd2
AG
822 err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
823 "workdir");
824 if (err)
b36a5780 825 return err;
bca44b52 826
b36a5780 827 return ovl_make_workdir(sb, ofs, workpath);
520d7c86
MS
828}
829
146d62e5 830static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
2d343087 831 struct ovl_entry *oe, const struct path *upperpath)
f7e3a7d9 832{
08f4c7c8 833 struct vfsmount *mnt = ovl_upper_mnt(ofs);
235ce9ed 834 struct dentry *indexdir;
5b02bfc1
AG
835 struct dentry *origin = ovl_lowerstack(oe)->dentry;
836 const struct ovl_fh *fh;
f7e3a7d9
MS
837 int err;
838
5b02bfc1
AG
839 fh = ovl_get_origin_fh(ofs, origin);
840 if (IS_ERR(fh))
841 return PTR_ERR(fh);
842
2ba9d57e
AG
843 err = mnt_want_write(mnt);
844 if (err)
5b02bfc1 845 goto out_free_fh;
2ba9d57e 846
f7e3a7d9 847 /* Verify lower root is upper root origin */
5b02bfc1 848 err = ovl_verify_origin_fh(ofs, upperpath->dentry, fh, true);
f7e3a7d9 849 if (err) {
1bd0a3ae 850 pr_err("failed to verify upper root origin\n");
f7e3a7d9
MS
851 goto out;
852 }
853
470c1563
AG
854 /* index dir will act also as workdir */
855 iput(ofs->workdir_trap);
856 ofs->workdir_trap = NULL;
857 dput(ofs->workdir);
858 ofs->workdir = NULL;
235ce9ed
AG
859 indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
860 if (IS_ERR(indexdir)) {
861 err = PTR_ERR(indexdir);
862 } else if (indexdir) {
02d70090
AG
863 ofs->workdir = indexdir;
864 err = ovl_setup_trap(sb, indexdir, &ofs->workdir_trap,
146d62e5
AG
865 "indexdir");
866 if (err)
867 goto out;
868
ad1d615c
AG
869 /*
870 * Verify upper root is exclusively associated with index dir.
2d2f2d73 871 * Older kernels stored upper fh in ".overlay.origin"
ad1d615c
AG
872 * xattr. If that xattr exists, verify that it is a match to
873 * upper dir file handle. In any case, verify or set xattr
2d2f2d73 874 * ".overlay.upper" to indicate that index may have
ad1d615c
AG
875 * directory entries.
876 */
02d70090
AG
877 if (ovl_check_origin_xattr(ofs, indexdir)) {
878 err = ovl_verify_origin_xattr(ofs, indexdir,
5b02bfc1
AG
879 OVL_XATTR_ORIGIN,
880 upperpath->dentry, true,
881 false);
ad1d615c 882 if (err)
1bd0a3ae 883 pr_err("failed to verify index dir 'origin' xattr\n");
ad1d615c 884 }
02d70090 885 err = ovl_verify_upper(ofs, indexdir, upperpath->dentry, true);
f7e3a7d9 886 if (err)
1bd0a3ae 887 pr_err("failed to verify index dir 'upper' xattr\n");
f7e3a7d9
MS
888
889 /* Cleanup bad/stale/orphan index entries */
890 if (!err)
1eff1a1d 891 err = ovl_indexdir_cleanup(ofs);
f7e3a7d9 892 }
02d70090 893 if (err || !indexdir)
1bd0a3ae 894 pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
f7e3a7d9
MS
895
896out:
2ba9d57e 897 mnt_drop_write(mnt);
5b02bfc1
AG
898out_free_fh:
899 kfree(fh);
f7e3a7d9
MS
900 return err;
901}
902
9df085f3
AG
903static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
904{
905 unsigned int i;
906
08f4c7c8 907 if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
9df085f3
AG
908 return true;
909
a888db31
AG
910 /*
911 * We allow using single lower with null uuid for index and nfs_export
912 * for example to support those features with single lower squashfs.
913 * To avoid regressions in setups of overlay with re-formatted lower
914 * squashfs, do not allow decoding origin with lower null uuid unless
915 * user opted-in to one of the new features that require following the
916 * lower inode of non-dir upper.
917 */
ca45275c 918 if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid))
a888db31
AG
919 return false;
920
1b81dddd 921 for (i = 0; i < ofs->numfs; i++) {
9df085f3
AG
922 /*
923 * We use uuid to associate an overlay lower file handle with a
924 * lower layer, so we can accept lower fs with null uuid as long
925 * as all lower layers with null uuid are on the same fs.
7e63c87f
AG
926 * if we detect multiple lower fs with the same uuid, we
927 * disable lower file handle decoding on all of them.
9df085f3 928 */
1b81dddd
AG
929 if (ofs->fs[i].is_lower &&
930 uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
07f1e596 931 ofs->fs[i].bad_uuid = true;
9df085f3 932 return false;
7e63c87f 933 }
9df085f3
AG
934 }
935 return true;
936}
937
5148626b 938/* Get a unique fsid for the layer */
9df085f3 939static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
5148626b 940{
9df085f3 941 struct super_block *sb = path->mnt->mnt_sb;
5148626b
AG
942 unsigned int i;
943 dev_t dev;
944 int err;
7e63c87f 945 bool bad_uuid = false;
b0e0f697 946 bool warn = false;
5148626b 947
07f1e596
AG
948 for (i = 0; i < ofs->numfs; i++) {
949 if (ofs->fs[i].sb == sb)
950 return i;
5148626b
AG
951 }
952
9df085f3 953 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
7e63c87f 954 bad_uuid = true;
b0e0f697
AG
955 if (ofs->config.xino == OVL_XINO_AUTO) {
956 ofs->config.xino = OVL_XINO_OFF;
957 warn = true;
958 }
7e63c87f
AG
959 if (ofs->config.index || ofs->config.nfs_export) {
960 ofs->config.index = false;
961 ofs->config.nfs_export = false;
b0e0f697
AG
962 warn = true;
963 }
964 if (warn) {
965 pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n",
7e63c87f
AG
966 uuid_is_null(&sb->s_uuid) ? "null" :
967 "conflicting",
dcb399de 968 path->dentry, ovl_xino_mode(&ofs->config));
7e63c87f 969 }
9df085f3
AG
970 }
971
5148626b
AG
972 err = get_anon_bdev(&dev);
973 if (err) {
1bd0a3ae 974 pr_err("failed to get anonymous bdev for lowerpath\n");
5148626b
AG
975 return err;
976 }
977
07f1e596
AG
978 ofs->fs[ofs->numfs].sb = sb;
979 ofs->fs[ofs->numfs].pseudo_dev = dev;
980 ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
5148626b 981
07f1e596 982 return ofs->numfs++;
5148626b
AG
983}
984
37ebf056
AG
985/*
986 * The fsid after the last lower fsid is used for the data layers.
987 * It is a "null fs" with a null sb, null uuid, and no pseudo dev.
988 */
989static int ovl_get_data_fsid(struct ovl_fs *ofs)
990{
991 return ofs->numfs;
992}
993
994
94375f9d 995static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
b36a5780 996 struct ovl_fs_context *ctx, struct ovl_layer *layers)
520d7c86
MS
997{
998 int err;
999 unsigned int i;
b36a5780 1000 size_t nr_merged_lower;
520d7c86 1001
b36a5780 1002 ofs->fs = kcalloc(ctx->nr + 2, sizeof(struct ovl_sb), GFP_KERNEL);
07f1e596 1003 if (ofs->fs == NULL)
9e88f905 1004 return -ENOMEM;
5148626b 1005
37ebf056
AG
1006 /*
1007 * idx/fsid 0 are reserved for upper fs even with lower only overlay
1008 * and the last fsid is reserved for "null fs" of the data layers.
1009 */
07f1e596
AG
1010 ofs->numfs++;
1011
07f1e596 1012 /*
b7bf9908
AG
1013 * All lower layers that share the same fs as upper layer, use the same
1014 * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
1015 * only overlay to simplify ovl_fs_free().
1b81dddd 1016 * is_lower will be set if upper fs is shared with a lower layer.
07f1e596 1017 */
b7bf9908
AG
1018 err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1019 if (err) {
1020 pr_err("failed to get anonymous bdev for upper fs\n");
9e88f905 1021 return err;
b7bf9908
AG
1022 }
1023
08f4c7c8
MS
1024 if (ovl_upper_mnt(ofs)) {
1025 ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
1b81dddd 1026 ofs->fs[0].is_lower = false;
07f1e596
AG
1027 }
1028
b36a5780
CB
1029 nr_merged_lower = ctx->nr - ctx->nr_data;
1030 for (i = 0; i < ctx->nr; i++) {
1031 struct ovl_fs_context_layer *l = &ctx->lower[i];
520d7c86 1032 struct vfsmount *mnt;
146d62e5 1033 struct inode *trap;
5148626b 1034 int fsid;
520d7c86 1035
b36a5780
CB
1036 if (i < nr_merged_lower)
1037 fsid = ovl_get_fsid(ofs, &l->path);
37ebf056
AG
1038 else
1039 fsid = ovl_get_data_fsid(ofs);
9e88f905
AG
1040 if (fsid < 0)
1041 return fsid;
520d7c86 1042
24f14009 1043 /*
1044 * Check if lower root conflicts with this overlay layers before
1045 * checking if it is in-use as upperdir/workdir of "another"
1046 * mount, because we do not bother to check in ovl_is_inuse() if
1047 * the upperdir/workdir is in fact in-use by our
1048 * upperdir/workdir.
1049 */
b36a5780 1050 err = ovl_setup_trap(sb, l->path.dentry, &trap, "lowerdir");
146d62e5 1051 if (err)
9e88f905 1052 return err;
146d62e5 1053
b36a5780 1054 if (ovl_is_inuse(l->path.dentry)) {
0be0bfd2 1055 err = ovl_report_in_use(ofs, "lowerdir");
24f14009 1056 if (err) {
1057 iput(trap);
9e88f905 1058 return err;
24f14009 1059 }
0be0bfd2
AG
1060 }
1061
b36a5780 1062 mnt = clone_private_mount(&l->path);
520d7c86
MS
1063 err = PTR_ERR(mnt);
1064 if (IS_ERR(mnt)) {
1bd0a3ae 1065 pr_err("failed to clone lowerpath\n");
146d62e5 1066 iput(trap);
9e88f905 1067 return err;
520d7c86 1068 }
5148626b 1069
520d7c86
MS
1070 /*
1071 * Make lower layers R/O. That way fchmod/fchown on lower file
1072 * will fail instead of modifying lower fs.
1073 */
1074 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1075
13464165
MS
1076 layers[ofs->numlayer].trap = trap;
1077 layers[ofs->numlayer].mnt = mnt;
1078 layers[ofs->numlayer].idx = ofs->numlayer;
1079 layers[ofs->numlayer].fsid = fsid;
1080 layers[ofs->numlayer].fs = &ofs->fs[fsid];
a535116d
AG
1081 /* Store for printing lowerdir=... in ovl_show_options() */
1082 ofs->config.lowerdirs[ofs->numlayer] = l->name;
b36a5780 1083 l->name = NULL;
94375f9d 1084 ofs->numlayer++;
1b81dddd 1085 ofs->fs[fsid].is_lower = true;
520d7c86 1086 }
e487d889 1087
795939a9
AG
1088 /*
1089 * When all layers on same fs, overlay can use real inode numbers.
926e94d7
AG
1090 * With mount option "xino=<on|auto>", mounter declares that there are
1091 * enough free high bits in underlying fs to hold the unique fsid.
795939a9
AG
1092 * If overlayfs does encounter underlying inodes using the high xino
1093 * bits reserved for fsid, it emits a warning and uses the original
dfe51d47
AG
1094 * inode number or a non persistent inode number allocated from a
1095 * dedicated range.
795939a9 1096 */
08f4c7c8 1097 if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
0f831ec8
AG
1098 if (ofs->config.xino == OVL_XINO_ON)
1099 pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1100 ofs->xino_mode = 0;
53afcd31
AG
1101 } else if (ofs->config.xino == OVL_XINO_OFF) {
1102 ofs->xino_mode = -1;
926e94d7 1103 } else if (ofs->xino_mode < 0) {
795939a9 1104 /*
07f1e596 1105 * This is a roundup of number of bits needed for encoding
dfe51d47
AG
1106 * fsid, where fsid 0 is reserved for upper fs (even with
1107 * lower only overlay) +1 extra bit is reserved for the non
1108 * persistent inode number range that is used for resolving
1109 * xino lower bits overflow.
795939a9 1110 */
dfe51d47
AG
1111 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1112 ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
795939a9
AG
1113 }
1114
0f831ec8 1115 if (ofs->xino_mode > 0) {
1bd0a3ae 1116 pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
0f831ec8 1117 ofs->xino_mode);
795939a9 1118 }
e487d889 1119
9e88f905 1120 return 0;
520d7c86
MS
1121}
1122
4155c10a 1123static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
b36a5780
CB
1124 struct ovl_fs_context *ctx,
1125 struct ovl_fs *ofs,
1126 struct ovl_layer *layers)
53dbb0b4
MS
1127{
1128 int err;
b8e42a65 1129 unsigned int i;
b36a5780 1130 size_t nr_merged_lower;
4155c10a 1131 struct ovl_entry *oe;
b36a5780
CB
1132 struct ovl_path *lowerstack;
1133
1134 struct ovl_fs_context_layer *l;
53dbb0b4 1135
b36a5780 1136 if (!ofs->config.upperdir && ctx->nr == 1) {
1bd0a3ae 1137 pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
b8e42a65 1138 return ERR_PTR(-EINVAL);
53dbb0b4
MS
1139 }
1140
eb3a04a8
MS
1141 if (ctx->nr == ctx->nr_data) {
1142 pr_err("at least one non-data lowerdir is required\n");
1143 return ERR_PTR(-EINVAL);
1144 }
1145
b36a5780
CB
1146 err = -EINVAL;
1147 for (i = 0; i < ctx->nr; i++) {
1148 l = &ctx->lower[i];
53dbb0b4 1149
b36a5780 1150 err = ovl_lower_dir(l->name, &l->path, ofs, &sb->s_stack_depth);
53dbb0b4 1151 if (err)
b36a5780 1152 return ERR_PTR(err);
53dbb0b4
MS
1153 }
1154
1155 err = -EINVAL;
1156 sb->s_stack_depth++;
1157 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1bd0a3ae 1158 pr_err("maximum fs stacking depth exceeded\n");
b36a5780 1159 return ERR_PTR(err);
53dbb0b4
MS
1160 }
1161
b36a5780 1162 err = ovl_get_layers(sb, ofs, ctx, layers);
4155c10a 1163 if (err)
b36a5780 1164 return ERR_PTR(err);
4155c10a
MS
1165
1166 err = -ENOMEM;
37ebf056 1167 /* Data-only layers are not merged in root directory */
b36a5780
CB
1168 nr_merged_lower = ctx->nr - ctx->nr_data;
1169 oe = ovl_alloc_entry(nr_merged_lower);
4155c10a 1170 if (!oe)
b36a5780 1171 return ERR_PTR(err);
4155c10a 1172
5522c9c7 1173 lowerstack = ovl_lowerstack(oe);
b36a5780
CB
1174 for (i = 0; i < nr_merged_lower; i++) {
1175 l = &ctx->lower[i];
1176 lowerstack[i].dentry = dget(l->path.dentry);
1177 lowerstack[i].layer = &ofs->layers[i + 1];
4155c10a 1178 }
b36a5780 1179 ofs->numdatalayer = ctx->nr_data;
4155c10a
MS
1180
1181 return oe;
53dbb0b4
MS
1182}
1183
146d62e5
AG
1184/*
1185 * Check if this layer root is a descendant of:
1186 * - another layer of this overlayfs instance
1187 * - upper/work dir of any overlayfs instance
146d62e5 1188 */
0be0bfd2 1189static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
708fa015
MS
1190 struct dentry *dentry, const char *name,
1191 bool is_lower)
146d62e5 1192{
9179c21d 1193 struct dentry *next = dentry, *parent;
146d62e5
AG
1194 int err = 0;
1195
9179c21d 1196 if (!dentry)
146d62e5
AG
1197 return 0;
1198
9179c21d
MS
1199 parent = dget_parent(next);
1200
1201 /* Walk back ancestors to root (inclusive) looking for traps */
1202 while (!err && parent != next) {
708fa015 1203 if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
146d62e5 1204 err = -ELOOP;
1bd0a3ae 1205 pr_err("overlapping %s path\n", name);
0be0bfd2
AG
1206 } else if (ovl_is_inuse(parent)) {
1207 err = ovl_report_in_use(ofs, name);
146d62e5 1208 }
146d62e5 1209 next = parent;
9179c21d
MS
1210 parent = dget_parent(next);
1211 dput(next);
146d62e5
AG
1212 }
1213
9179c21d 1214 dput(parent);
146d62e5
AG
1215
1216 return err;
1217}
1218
1219/*
1220 * Check if any of the layers or work dirs overlap.
1221 */
1222static int ovl_check_overlapping_layers(struct super_block *sb,
1223 struct ovl_fs *ofs)
1224{
1225 int i, err;
1226
08f4c7c8
MS
1227 if (ovl_upper_mnt(ofs)) {
1228 err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
708fa015 1229 "upperdir", false);
146d62e5
AG
1230 if (err)
1231 return err;
1232
1233 /*
1234 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1235 * this instance and covers overlapping work and index dirs,
1236 * unless work or index dir have been moved since created inside
1237 * workbasedir. In that case, we already have their traps in
1238 * inode cache and we will catch that case on lookup.
1239 */
708fa015
MS
1240 err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1241 false);
146d62e5
AG
1242 if (err)
1243 return err;
1244 }
1245
94375f9d 1246 for (i = 1; i < ofs->numlayer; i++) {
0be0bfd2 1247 err = ovl_check_layer(sb, ofs,
94375f9d 1248 ofs->layers[i].mnt->mnt_root,
708fa015 1249 "lowerdir", true);
146d62e5
AG
1250 if (err)
1251 return err;
1252 }
1253
1254 return 0;
1255}
1256
2effc5c2
AG
1257static struct dentry *ovl_get_root(struct super_block *sb,
1258 struct dentry *upperdentry,
1259 struct ovl_entry *oe)
1260{
1261 struct dentry *root;
420332b9 1262 struct ovl_fs *ofs = OVL_FS(sb);
5522c9c7 1263 struct ovl_path *lowerpath = ovl_lowerstack(oe);
62c832ed
AG
1264 unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1265 int fsid = lowerpath->layer->fsid;
1266 struct ovl_inode_params oip = {
1267 .upperdentry = upperdentry,
0af950f5 1268 .oe = oe,
62c832ed 1269 };
2effc5c2
AG
1270
1271 root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1272 if (!root)
1273 return NULL;
1274
2effc5c2 1275 if (upperdentry) {
62c832ed
AG
1276 /* Root inode uses upper st_ino/i_ino */
1277 ino = d_inode(upperdentry)->i_ino;
1278 fsid = 0;
2effc5c2 1279 ovl_dentry_set_upper_alias(root);
610afc0b 1280 if (ovl_is_impuredir(sb, upperdentry))
2effc5c2
AG
1281 ovl_set_flag(OVL_IMPURE, d_inode(root));
1282 }
1283
420332b9
AG
1284 /* Look for xwhiteouts marker except in the lowermost layer */
1285 for (int i = 0; i < ovl_numlower(oe) - 1; i++, lowerpath++) {
1286 struct path path = {
1287 .mnt = lowerpath->layer->mnt,
1288 .dentry = lowerpath->dentry,
1289 };
1290
1291 /* overlay.opaque=x means xwhiteouts directory */
1292 if (ovl_get_opaquedir_val(ofs, &path) == 'x') {
1293 ovl_layer_set_xwhiteouts(ofs, lowerpath->layer);
1294 ovl_dentry_set_xwhiteouts(root);
1295 }
1296 }
1297
2effc5c2
AG
1298 /* Root is always merge -> can have whiteouts */
1299 ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1300 ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1301 ovl_set_upperdata(d_inode(root));
62c832ed 1302 ovl_inode_init(d_inode(root), &oip, ino, fsid);
0af950f5 1303 ovl_dentry_init_flags(root, upperdentry, oe, DCACHE_OP_WEAK_REVALIDATE);
367d002d
AG
1304 /* root keeps a reference of upperdentry */
1305 dget(upperdentry);
2effc5c2
AG
1306
1307 return root;
1308}
1309
7fb7998b 1310int ovl_fill_super(struct super_block *sb, struct fs_context *fc)
e9be9d5e 1311{
1784fbc2
CB
1312 struct ovl_fs *ofs = sb->s_fs_info;
1313 struct ovl_fs_context *ctx = fc->fs_private;
539a0879 1314 const struct cred *old_cred = NULL;
e9be9d5e 1315 struct dentry *root_dentry;
4155c10a 1316 struct ovl_entry *oe;
b8e42a65 1317 struct ovl_layer *layers;
51f8f3c4 1318 struct cred *cred;
e9be9d5e
MS
1319 int err;
1320
9efb069d 1321 err = -EIO;
1784fbc2
CB
1322 if (WARN_ON(fc->user_ns != current_user_ns()))
1323 goto out_err;
9efb069d 1324
f4288844
MS
1325 sb->s_d_op = &ovl_dentry_operations;
1326
d7b49b10 1327 err = -ENOMEM;
539a0879
CB
1328 if (!ofs->creator_cred)
1329 ofs->creator_cred = cred = prepare_creds();
1330 else
1331 cred = (struct cred *)ofs->creator_cred;
c6fe6254
MS
1332 if (!cred)
1333 goto out_err;
1334
539a0879
CB
1335 old_cred = ovl_override_creds(sb);
1336
1784fbc2 1337 err = ovl_fs_params_verify(ctx, &ofs->config);
f45827e8 1338 if (err)
a9075cdb 1339 goto out_err;
f45827e8 1340
e9be9d5e 1341 err = -EINVAL;
b36a5780 1342 if (ctx->nr == 0) {
1784fbc2 1343 if (!(fc->sb_flags & SB_SILENT))
1bd0a3ae 1344 pr_err("missing 'lowerdir'\n");
a9075cdb 1345 goto out_err;
e9be9d5e
MS
1346 }
1347
b8e42a65 1348 err = -ENOMEM;
b36a5780 1349 layers = kcalloc(ctx->nr + 1, sizeof(struct ovl_layer), GFP_KERNEL);
b8e42a65
MS
1350 if (!layers)
1351 goto out_err;
1352
a535116d
AG
1353 ofs->config.lowerdirs = kcalloc(ctx->nr + 1, sizeof(char *), GFP_KERNEL);
1354 if (!ofs->config.lowerdirs) {
1355 kfree(layers);
1356 goto out_err;
1357 }
b8e42a65 1358 ofs->layers = layers;
a535116d
AG
1359 /*
1360 * Layer 0 is reserved for upper even if there's no upper.
0cea4c09
AG
1361 * config.lowerdirs[0] is used for storing the user provided colon
1362 * separated lowerdir string.
a535116d 1363 */
0cea4c09
AG
1364 ofs->config.lowerdirs[0] = ctx->lowerdir_all;
1365 ctx->lowerdir_all = NULL;
b8e42a65
MS
1366 ofs->numlayer = 1;
1367
53a08cb9 1368 sb->s_stack_depth = 0;
cf9a6784 1369 sb->s_maxbytes = MAX_LFS_FILESIZE;
4d314f78 1370 atomic_long_set(&ofs->last_ino, 1);
4f119628 1371 /* Assume underlying fs uses 32bit inodes unless proven otherwise */
53afcd31 1372 if (ofs->config.xino != OVL_XINO_OFF) {
0f831ec8 1373 ofs->xino_mode = BITS_PER_LONG - 32;
53afcd31
AG
1374 if (!ofs->xino_mode) {
1375 pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1376 ofs->config.xino = OVL_XINO_OFF;
1377 }
1378 }
795939a9 1379
146d62e5
AG
1380 /* alloc/destroy_inode needed for setting up traps in inode cache */
1381 sb->s_op = &ovl_super_operations;
1382
ad204488 1383 if (ofs->config.upperdir) {
335d3fc5
SD
1384 struct super_block *upper_sb;
1385
d7b49b10 1386 err = -EINVAL;
ad204488 1387 if (!ofs->config.workdir) {
1bd0a3ae 1388 pr_err("missing 'workdir'\n");
a9075cdb 1389 goto out_err;
53a08cb9 1390 }
e9be9d5e 1391
b36a5780 1392 err = ovl_get_upper(sb, ofs, &layers[0], &ctx->upper);
53a08cb9 1393 if (err)
a9075cdb 1394 goto out_err;
2cac0c00 1395
335d3fc5
SD
1396 upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
1397 if (!ovl_should_sync(ofs)) {
1398 ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
1399 if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
1400 err = -EIO;
1401 pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
1402 goto out_err;
1403 }
1404 }
1405
b36a5780 1406 err = ovl_get_workdir(sb, ofs, &ctx->upper, &ctx->work);
8ed61dc3 1407 if (err)
a9075cdb 1408 goto out_err;
c6fe6254 1409
ad204488 1410 if (!ofs->workdir)
1751e8a6 1411 sb->s_flags |= SB_RDONLY;
6e88256e 1412
335d3fc5
SD
1413 sb->s_stack_depth = upper_sb->s_stack_depth;
1414 sb->s_time_gran = upper_sb->s_time_gran;
e9be9d5e 1415 }
b36a5780 1416 oe = ovl_get_lowerstack(sb, ctx, ofs, layers);
4155c10a
MS
1417 err = PTR_ERR(oe);
1418 if (IS_ERR(oe))
a9075cdb 1419 goto out_err;
e9be9d5e 1420
71cbad7e 1421 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
08f4c7c8 1422 if (!ovl_upper_mnt(ofs))
1751e8a6 1423 sb->s_flags |= SB_RDONLY;
e9be9d5e 1424
b0504bfe
AG
1425 if (!ovl_origin_uuid(ofs) && ofs->numfs > 1) {
1426 pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=null.\n");
1427 ofs->config.uuid = OVL_UUID_NULL;
d9544c1b
AG
1428 } else if (ovl_has_fsid(ofs) && ovl_upper_mnt(ofs)) {
1429 /* Use per instance persistent uuid/fsid */
1430 ovl_init_uuid_xattr(sb, ofs, &ctx->upper);
5830fb6b
PT
1431 }
1432
470c1563 1433 if (!ovl_force_readonly(ofs) && ofs->config.index) {
b36a5780 1434 err = ovl_get_indexdir(sb, ofs, oe, &ctx->upper);
54fb347e 1435 if (err)
4155c10a 1436 goto out_free_oe;
6e88256e 1437
972d0093 1438 /* Force r/o mount with no index dir */
02d70090 1439 if (!ofs->workdir)
1751e8a6 1440 sb->s_flags |= SB_RDONLY;
02bcd157
AG
1441 }
1442
146d62e5
AG
1443 err = ovl_check_overlapping_layers(sb, ofs);
1444 if (err)
1445 goto out_free_oe;
1446
972d0093 1447 /* Show index=off in /proc/mounts for forced r/o mount */
02d70090 1448 if (!ofs->workdir) {
ad204488 1449 ofs->config.index = false;
08f4c7c8 1450 if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
1bd0a3ae 1451 pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
f168f109
AG
1452 ofs->config.nfs_export = false;
1453 }
1454 }
02bcd157 1455
d5791044 1456 if (ofs->config.metacopy && ofs->config.nfs_export) {
1bd0a3ae 1457 pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
d5791044
VG
1458 ofs->config.nfs_export = false;
1459 }
1460
16aac5ad
AG
1461 /*
1462 * Support encoding decodable file handles with nfs_export=on
1463 * and encoding non-decodable file handles with nfs_export=off
1464 * if all layers support file handles.
1465 */
8383f174
AG
1466 if (ofs->config.nfs_export)
1467 sb->s_export_op = &ovl_export_operations;
16aac5ad
AG
1468 else if (!ofs->nofh)
1469 sb->s_export_op = &ovl_export_fid_operations;
8383f174 1470
51f8f3c4
KK
1471 /* Never override disk quota limits or use reserved space */
1472 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1473
655042cc 1474 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
420a62dd 1475 sb->s_xattr = ovl_xattr_handlers(ofs);
ad204488 1476 sb->s_fs_info = ofs;
2bc5e5e8 1477#ifdef CONFIG_FS_POSIX_ACL
7c4d37c2 1478 sb->s_flags |= SB_POSIXACL;
2bc5e5e8 1479#endif
b836c4d2 1480 sb->s_iflags |= SB_I_SKIP_SYNC;
2bc5e5e8
CB
1481 /*
1482 * Ensure that umask handling is done by the filesystems used
1483 * for the the upper layer instead of overlayfs as that would
1484 * lead to unexpected results.
1485 */
1486 sb->s_iflags |= SB_I_NOUMASK;
1f65e57d 1487 sb->s_iflags |= SB_I_EVM_HMAC_UNSUPPORTED;
655042cc 1488
c6fe6254 1489 err = -ENOMEM;
b36a5780 1490 root_dentry = ovl_get_root(sb, ctx->upper.dentry, oe);
e9be9d5e 1491 if (!root_dentry)
4155c10a 1492 goto out_free_oe;
e9be9d5e 1493
e9be9d5e 1494 sb->s_root = root_dentry;
e9be9d5e 1495
539a0879 1496 ovl_revert_creds(old_cred);
e9be9d5e
MS
1497 return 0;
1498
4155c10a 1499out_free_oe:
163db0da 1500 ovl_free_entry(oe);
4155c10a 1501out_err:
539a0879
CB
1502 /*
1503 * Revert creds before calling ovl_free_fs() which will call
1504 * put_cred() and put_cred() requires that the cred's that are
1505 * put are not the caller's creds, i.e., current->cred.
1506 */
1507 if (old_cred)
1508 ovl_revert_creds(old_cred);
ad204488 1509 ovl_free_fs(ofs);
1784fbc2 1510 sb->s_fs_info = NULL;
e9be9d5e
MS
1511 return err;
1512}
1513
f01d0889 1514struct file_system_type ovl_fs_type = {
1784fbc2
CB
1515 .owner = THIS_MODULE,
1516 .name = "overlay",
1517 .init_fs_context = ovl_init_fs_context,
1518 .parameters = ovl_parameter_spec,
1519 .fs_flags = FS_USERNS_MOUNT,
1520 .kill_sb = kill_anon_super,
e9be9d5e 1521};
ef94b186 1522MODULE_ALIAS_FS("overlay");
e9be9d5e 1523
13cf199d
AG
1524static void ovl_inode_init_once(void *foo)
1525{
1526 struct ovl_inode *oi = foo;
1527
1528 inode_init_once(&oi->vfs_inode);
1529}
1530
e9be9d5e
MS
1531static int __init ovl_init(void)
1532{
13cf199d
AG
1533 int err;
1534
1535 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1536 sizeof(struct ovl_inode), 0,
1537 (SLAB_RECLAIM_ACCOUNT|
f88c3fb8 1538 SLAB_ACCOUNT),
13cf199d
AG
1539 ovl_inode_init_once);
1540 if (ovl_inode_cachep == NULL)
1541 return -ENOMEM;
1542
a6293b3e
AG
1543 err = register_filesystem(&ovl_fs_type);
1544 if (!err)
1545 return 0;
2406a307 1546
2406a307 1547 kmem_cache_destroy(ovl_inode_cachep);
13cf199d
AG
1548
1549 return err;
e9be9d5e
MS
1550}
1551
1552static void __exit ovl_exit(void)
1553{
1554 unregister_filesystem(&ovl_fs_type);
13cf199d
AG
1555
1556 /*
1557 * Make sure all delayed rcu free inodes are flushed before we
1558 * destroy cache.
1559 */
1560 rcu_barrier();
1561 kmem_cache_destroy(ovl_inode_cachep);
e9be9d5e
MS
1562}
1563
1564module_init(ovl_init);
1565module_exit(ovl_exit);