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