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