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