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