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