blob: f036d7544d4a6c4aab7295dc844c78ef6e08848a [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
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 "overlayfs.h"
19
20MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21MODULE_DESCRIPTION("Overlay filesystem");
22MODULE_LICENSE("GPL");
23
24
25struct ovl_dir_cache;
26
27#define OVL_MAX_STACK 500
28
29static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
30module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
David Brazdil0f672f62019-12-10 10:32:29 +000031MODULE_PARM_DESC(redirect_dir,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032 "Default to on or off for the redirect_dir feature");
33
34static bool ovl_redirect_always_follow =
35 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
36module_param_named(redirect_always_follow, ovl_redirect_always_follow,
37 bool, 0644);
David Brazdil0f672f62019-12-10 10:32:29 +000038MODULE_PARM_DESC(redirect_always_follow,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 "Follow redirects even if redirect_dir feature is turned off");
40
41static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
42module_param_named(index, ovl_index_def, bool, 0644);
David Brazdil0f672f62019-12-10 10:32:29 +000043MODULE_PARM_DESC(index,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 "Default to on or off for the inodes index feature");
45
46static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
47module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
David Brazdil0f672f62019-12-10 10:32:29 +000048MODULE_PARM_DESC(nfs_export,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 "Default to on or off for the NFS export feature");
50
51static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
52module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
David Brazdil0f672f62019-12-10 10:32:29 +000053MODULE_PARM_DESC(xino_auto,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 "Auto enable xino feature");
55
56static void ovl_entry_stack_free(struct ovl_entry *oe)
57{
58 unsigned int i;
59
60 for (i = 0; i < oe->numlower; i++)
61 dput(oe->lowerstack[i].dentry);
62}
63
64static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
65module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
David Brazdil0f672f62019-12-10 10:32:29 +000066MODULE_PARM_DESC(metacopy,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 "Default to on or off for the metadata only copy up feature");
68
69static void ovl_dentry_release(struct dentry *dentry)
70{
71 struct ovl_entry *oe = dentry->d_fsdata;
72
73 if (oe) {
74 ovl_entry_stack_free(oe);
75 kfree_rcu(oe, rcu);
76 }
77}
78
79static struct dentry *ovl_d_real(struct dentry *dentry,
80 const struct inode *inode)
81{
Olivier Deprez0e641232021-09-23 10:07:05 +020082 struct dentry *real = NULL, *lower;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083
84 /* It's an overlay file */
85 if (inode && d_inode(dentry) == inode)
86 return dentry;
87
88 if (!d_is_reg(dentry)) {
89 if (!inode || inode == d_inode(dentry))
90 return dentry;
91 goto bug;
92 }
93
94 real = ovl_dentry_upper(dentry);
95 if (real && (inode == d_inode(real)))
96 return real;
97
98 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
99 return real;
100
Olivier Deprez0e641232021-09-23 10:07:05 +0200101 lower = ovl_dentry_lowerdata(dentry);
102 if (!lower)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 goto bug;
Olivier Deprez0e641232021-09-23 10:07:05 +0200104 real = lower;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105
106 /* Handle recursion */
107 real = d_real(real, inode);
108
109 if (!inode || inode == d_inode(real))
110 return real;
111bug:
Olivier Deprez0e641232021-09-23 10:07:05 +0200112 WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
113 __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
114 inode ? inode->i_ino : 0, real,
115 real && d_inode(real) ? d_inode(real)->i_ino : 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116 return dentry;
117}
118
119static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
120{
121 struct ovl_entry *oe = dentry->d_fsdata;
122 unsigned int i;
123 int ret = 1;
124
125 for (i = 0; i < oe->numlower; i++) {
126 struct dentry *d = oe->lowerstack[i].dentry;
127
128 if (d->d_flags & DCACHE_OP_REVALIDATE) {
129 ret = d->d_op->d_revalidate(d, flags);
130 if (ret < 0)
131 return ret;
132 if (!ret) {
133 if (!(flags & LOOKUP_RCU))
134 d_invalidate(d);
135 return -ESTALE;
136 }
137 }
138 }
139 return 1;
140}
141
142static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
143{
144 struct ovl_entry *oe = dentry->d_fsdata;
145 unsigned int i;
146 int ret = 1;
147
148 for (i = 0; i < oe->numlower; i++) {
149 struct dentry *d = oe->lowerstack[i].dentry;
150
151 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
152 ret = d->d_op->d_weak_revalidate(d, flags);
153 if (ret <= 0)
154 break;
155 }
156 }
157 return ret;
158}
159
160static const struct dentry_operations ovl_dentry_operations = {
161 .d_release = ovl_dentry_release,
162 .d_real = ovl_d_real,
163};
164
165static const struct dentry_operations ovl_reval_dentry_operations = {
166 .d_release = ovl_dentry_release,
167 .d_real = ovl_d_real,
168 .d_revalidate = ovl_dentry_revalidate,
169 .d_weak_revalidate = ovl_dentry_weak_revalidate,
170};
171
172static struct kmem_cache *ovl_inode_cachep;
173
174static struct inode *ovl_alloc_inode(struct super_block *sb)
175{
176 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
177
178 if (!oi)
179 return NULL;
180
181 oi->cache = NULL;
182 oi->redirect = NULL;
183 oi->version = 0;
184 oi->flags = 0;
185 oi->__upperdentry = NULL;
186 oi->lower = NULL;
187 oi->lowerdata = NULL;
188 mutex_init(&oi->lock);
189
190 return &oi->vfs_inode;
191}
192
David Brazdil0f672f62019-12-10 10:32:29 +0000193static void ovl_free_inode(struct inode *inode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194{
David Brazdil0f672f62019-12-10 10:32:29 +0000195 struct ovl_inode *oi = OVL_I(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000196
David Brazdil0f672f62019-12-10 10:32:29 +0000197 kfree(oi->redirect);
198 mutex_destroy(&oi->lock);
199 kmem_cache_free(ovl_inode_cachep, oi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200}
201
202static void ovl_destroy_inode(struct inode *inode)
203{
204 struct ovl_inode *oi = OVL_I(inode);
205
206 dput(oi->__upperdentry);
207 iput(oi->lower);
208 if (S_ISDIR(inode->i_mode))
209 ovl_dir_cache_free(inode);
210 else
211 iput(oi->lowerdata);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212}
213
214static void ovl_free_fs(struct ovl_fs *ofs)
215{
216 unsigned i;
217
David Brazdil0f672f62019-12-10 10:32:29 +0000218 iput(ofs->workbasedir_trap);
219 iput(ofs->indexdir_trap);
220 iput(ofs->workdir_trap);
221 iput(ofs->upperdir_trap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 dput(ofs->indexdir);
223 dput(ofs->workdir);
224 if (ofs->workdir_locked)
225 ovl_inuse_unlock(ofs->workbasedir);
226 dput(ofs->workbasedir);
227 if (ofs->upperdir_locked)
228 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
229 mntput(ofs->upper_mnt);
David Brazdil0f672f62019-12-10 10:32:29 +0000230 for (i = 0; i < ofs->numlower; i++) {
231 iput(ofs->lower_layers[i].trap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 mntput(ofs->lower_layers[i].mnt);
David Brazdil0f672f62019-12-10 10:32:29 +0000233 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 for (i = 0; i < ofs->numlowerfs; i++)
235 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
236 kfree(ofs->lower_layers);
237 kfree(ofs->lower_fs);
238
239 kfree(ofs->config.lowerdir);
240 kfree(ofs->config.upperdir);
241 kfree(ofs->config.workdir);
242 kfree(ofs->config.redirect_mode);
243 if (ofs->creator_cred)
244 put_cred(ofs->creator_cred);
245 kfree(ofs);
246}
247
248static void ovl_put_super(struct super_block *sb)
249{
250 struct ovl_fs *ofs = sb->s_fs_info;
251
252 ovl_free_fs(ofs);
253}
254
255/* Sync real dirty inodes in upper filesystem (if it exists) */
256static int ovl_sync_fs(struct super_block *sb, int wait)
257{
258 struct ovl_fs *ofs = sb->s_fs_info;
259 struct super_block *upper_sb;
260 int ret;
261
262 if (!ofs->upper_mnt)
263 return 0;
264
265 /*
266 * If this is a sync(2) call or an emergency sync, all the super blocks
267 * will be iterated, including upper_sb, so no need to do anything.
268 *
269 * If this is a syncfs(2) call, then we do need to call
270 * sync_filesystem() on upper_sb, but enough if we do it when being
271 * called with wait == 1.
272 */
273 if (!wait)
274 return 0;
275
276 upper_sb = ofs->upper_mnt->mnt_sb;
277
278 down_read(&upper_sb->s_umount);
279 ret = sync_filesystem(upper_sb);
280 up_read(&upper_sb->s_umount);
281
282 return ret;
283}
284
285/**
286 * ovl_statfs
287 * @sb: The overlayfs super block
288 * @buf: The struct kstatfs to fill in with stats
289 *
290 * Get the filesystem statistics. As writes always target the upper layer
291 * filesystem pass the statfs to the upper filesystem (if it exists)
292 */
293static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
294{
295 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
296 struct dentry *root_dentry = dentry->d_sb->s_root;
297 struct path path;
298 int err;
299
300 ovl_path_real(root_dentry, &path);
301
302 err = vfs_statfs(&path, buf);
303 if (!err) {
304 buf->f_namelen = ofs->namelen;
305 buf->f_type = OVERLAYFS_SUPER_MAGIC;
306 }
307
308 return err;
309}
310
311/* Will this overlay be forced to mount/remount ro? */
312static bool ovl_force_readonly(struct ovl_fs *ofs)
313{
314 return (!ofs->upper_mnt || !ofs->workdir);
315}
316
317static const char *ovl_redirect_mode_def(void)
318{
319 return ovl_redirect_dir_def ? "on" : "off";
320}
321
322enum {
323 OVL_XINO_OFF,
324 OVL_XINO_AUTO,
325 OVL_XINO_ON,
326};
327
328static const char * const ovl_xino_str[] = {
329 "off",
330 "auto",
331 "on",
332};
333
334static inline int ovl_xino_def(void)
335{
336 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
337}
338
339/**
340 * ovl_show_options
341 *
342 * Prints the mount options for a given superblock.
343 * Returns zero; does not fail.
344 */
345static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
346{
347 struct super_block *sb = dentry->d_sb;
348 struct ovl_fs *ofs = sb->s_fs_info;
349
350 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
351 if (ofs->config.upperdir) {
352 seq_show_option(m, "upperdir", ofs->config.upperdir);
353 seq_show_option(m, "workdir", ofs->config.workdir);
354 }
355 if (ofs->config.default_permissions)
356 seq_puts(m, ",default_permissions");
357 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
358 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
359 if (ofs->config.index != ovl_index_def)
360 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
361 if (ofs->config.nfs_export != ovl_nfs_export_def)
362 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
363 "on" : "off");
364 if (ofs->config.xino != ovl_xino_def())
365 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
366 if (ofs->config.metacopy != ovl_metacopy_def)
367 seq_printf(m, ",metacopy=%s",
368 ofs->config.metacopy ? "on" : "off");
369 return 0;
370}
371
372static int ovl_remount(struct super_block *sb, int *flags, char *data)
373{
374 struct ovl_fs *ofs = sb->s_fs_info;
375
376 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
377 return -EROFS;
378
379 return 0;
380}
381
382static const struct super_operations ovl_super_operations = {
383 .alloc_inode = ovl_alloc_inode,
David Brazdil0f672f62019-12-10 10:32:29 +0000384 .free_inode = ovl_free_inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000385 .destroy_inode = ovl_destroy_inode,
386 .drop_inode = generic_delete_inode,
387 .put_super = ovl_put_super,
388 .sync_fs = ovl_sync_fs,
389 .statfs = ovl_statfs,
390 .show_options = ovl_show_options,
391 .remount_fs = ovl_remount,
392};
393
394enum {
395 OPT_LOWERDIR,
396 OPT_UPPERDIR,
397 OPT_WORKDIR,
398 OPT_DEFAULT_PERMISSIONS,
399 OPT_REDIRECT_DIR,
400 OPT_INDEX_ON,
401 OPT_INDEX_OFF,
402 OPT_NFS_EXPORT_ON,
403 OPT_NFS_EXPORT_OFF,
404 OPT_XINO_ON,
405 OPT_XINO_OFF,
406 OPT_XINO_AUTO,
407 OPT_METACOPY_ON,
408 OPT_METACOPY_OFF,
409 OPT_ERR,
410};
411
412static const match_table_t ovl_tokens = {
413 {OPT_LOWERDIR, "lowerdir=%s"},
414 {OPT_UPPERDIR, "upperdir=%s"},
415 {OPT_WORKDIR, "workdir=%s"},
416 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
417 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
418 {OPT_INDEX_ON, "index=on"},
419 {OPT_INDEX_OFF, "index=off"},
420 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
421 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
422 {OPT_XINO_ON, "xino=on"},
423 {OPT_XINO_OFF, "xino=off"},
424 {OPT_XINO_AUTO, "xino=auto"},
425 {OPT_METACOPY_ON, "metacopy=on"},
426 {OPT_METACOPY_OFF, "metacopy=off"},
427 {OPT_ERR, NULL}
428};
429
430static char *ovl_next_opt(char **s)
431{
432 char *sbegin = *s;
433 char *p;
434
435 if (sbegin == NULL)
436 return NULL;
437
438 for (p = sbegin; *p; p++) {
439 if (*p == '\\') {
440 p++;
441 if (!*p)
442 break;
443 } else if (*p == ',') {
444 *p = '\0';
445 *s = p + 1;
446 return sbegin;
447 }
448 }
449 *s = NULL;
450 return sbegin;
451}
452
453static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
454{
455 if (strcmp(mode, "on") == 0) {
456 config->redirect_dir = true;
457 /*
458 * Does not make sense to have redirect creation without
459 * redirect following.
460 */
461 config->redirect_follow = true;
462 } else if (strcmp(mode, "follow") == 0) {
463 config->redirect_follow = true;
464 } else if (strcmp(mode, "off") == 0) {
465 if (ovl_redirect_always_follow)
466 config->redirect_follow = true;
467 } else if (strcmp(mode, "nofollow") != 0) {
468 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
469 mode);
470 return -EINVAL;
471 }
472
473 return 0;
474}
475
476static int ovl_parse_opt(char *opt, struct ovl_config *config)
477{
478 char *p;
479 int err;
480 bool metacopy_opt = false, redirect_opt = false;
481
482 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
483 if (!config->redirect_mode)
484 return -ENOMEM;
485
486 while ((p = ovl_next_opt(&opt)) != NULL) {
487 int token;
488 substring_t args[MAX_OPT_ARGS];
489
490 if (!*p)
491 continue;
492
493 token = match_token(p, ovl_tokens, args);
494 switch (token) {
495 case OPT_UPPERDIR:
496 kfree(config->upperdir);
497 config->upperdir = match_strdup(&args[0]);
498 if (!config->upperdir)
499 return -ENOMEM;
500 break;
501
502 case OPT_LOWERDIR:
503 kfree(config->lowerdir);
504 config->lowerdir = match_strdup(&args[0]);
505 if (!config->lowerdir)
506 return -ENOMEM;
507 break;
508
509 case OPT_WORKDIR:
510 kfree(config->workdir);
511 config->workdir = match_strdup(&args[0]);
512 if (!config->workdir)
513 return -ENOMEM;
514 break;
515
516 case OPT_DEFAULT_PERMISSIONS:
517 config->default_permissions = true;
518 break;
519
520 case OPT_REDIRECT_DIR:
521 kfree(config->redirect_mode);
522 config->redirect_mode = match_strdup(&args[0]);
523 if (!config->redirect_mode)
524 return -ENOMEM;
525 redirect_opt = true;
526 break;
527
528 case OPT_INDEX_ON:
529 config->index = true;
530 break;
531
532 case OPT_INDEX_OFF:
533 config->index = false;
534 break;
535
536 case OPT_NFS_EXPORT_ON:
537 config->nfs_export = true;
538 break;
539
540 case OPT_NFS_EXPORT_OFF:
541 config->nfs_export = false;
542 break;
543
544 case OPT_XINO_ON:
545 config->xino = OVL_XINO_ON;
546 break;
547
548 case OPT_XINO_OFF:
549 config->xino = OVL_XINO_OFF;
550 break;
551
552 case OPT_XINO_AUTO:
553 config->xino = OVL_XINO_AUTO;
554 break;
555
556 case OPT_METACOPY_ON:
557 config->metacopy = true;
558 metacopy_opt = true;
559 break;
560
561 case OPT_METACOPY_OFF:
562 config->metacopy = false;
563 break;
564
565 default:
566 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
567 return -EINVAL;
568 }
569 }
570
571 /* Workdir is useless in non-upper mount */
572 if (!config->upperdir && config->workdir) {
573 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
574 config->workdir);
575 kfree(config->workdir);
576 config->workdir = NULL;
577 }
578
579 err = ovl_parse_redirect_mode(config, config->redirect_mode);
580 if (err)
581 return err;
582
583 /*
584 * This is to make the logic below simpler. It doesn't make any other
585 * difference, since config->redirect_dir is only used for upper.
586 */
587 if (!config->upperdir && config->redirect_follow)
588 config->redirect_dir = true;
589
590 /* Resolve metacopy -> redirect_dir dependency */
591 if (config->metacopy && !config->redirect_dir) {
592 if (metacopy_opt && redirect_opt) {
593 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
594 config->redirect_mode);
595 return -EINVAL;
596 }
597 if (redirect_opt) {
598 /*
599 * There was an explicit redirect_dir=... that resulted
600 * in this conflict.
601 */
602 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
603 config->redirect_mode);
604 config->metacopy = false;
605 } else {
606 /* Automatically enable redirect otherwise. */
607 config->redirect_follow = config->redirect_dir = true;
608 }
609 }
610
611 return 0;
612}
613
614#define OVL_WORKDIR_NAME "work"
615#define OVL_INDEXDIR_NAME "index"
616
617static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
618 const char *name, bool persist)
619{
620 struct inode *dir = ofs->workbasedir->d_inode;
621 struct vfsmount *mnt = ofs->upper_mnt;
622 struct dentry *work;
623 int err;
624 bool retried = false;
625 bool locked = false;
626
627 inode_lock_nested(dir, I_MUTEX_PARENT);
628 locked = true;
629
630retry:
631 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
632
633 if (!IS_ERR(work)) {
634 struct iattr attr = {
635 .ia_valid = ATTR_MODE,
636 .ia_mode = S_IFDIR | 0,
637 };
638
639 if (work->d_inode) {
640 err = -EEXIST;
641 if (retried)
642 goto out_dput;
643
644 if (persist)
645 goto out_unlock;
646
647 retried = true;
648 ovl_workdir_cleanup(dir, mnt, work, 0);
649 dput(work);
650 goto retry;
651 }
652
653 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
654 err = PTR_ERR(work);
655 if (IS_ERR(work))
656 goto out_err;
657
658 /*
659 * Try to remove POSIX ACL xattrs from workdir. We are good if:
660 *
661 * a) success (there was a POSIX ACL xattr and was removed)
662 * b) -ENODATA (there was no POSIX ACL xattr)
663 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
664 *
665 * There are various other error values that could effectively
666 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
667 * if the xattr name is too long), but the set of filesystems
668 * allowed as upper are limited to "normal" ones, where checking
669 * for the above two errors is sufficient.
670 */
671 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
672 if (err && err != -ENODATA && err != -EOPNOTSUPP)
673 goto out_dput;
674
675 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
676 if (err && err != -ENODATA && err != -EOPNOTSUPP)
677 goto out_dput;
678
679 /* Clear any inherited mode bits */
680 inode_lock(work->d_inode);
681 err = notify_change(work, &attr, NULL);
682 inode_unlock(work->d_inode);
683 if (err)
684 goto out_dput;
685 } else {
686 err = PTR_ERR(work);
687 goto out_err;
688 }
689out_unlock:
690 if (locked)
691 inode_unlock(dir);
692
693 return work;
694
695out_dput:
696 dput(work);
697out_err:
698 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
699 ofs->config.workdir, name, -err);
700 work = NULL;
701 goto out_unlock;
702}
703
704static void ovl_unescape(char *s)
705{
706 char *d = s;
707
708 for (;; s++, d++) {
709 if (*s == '\\')
710 s++;
711 *d = *s;
712 if (!*s)
713 break;
714 }
715}
716
717static int ovl_mount_dir_noesc(const char *name, struct path *path)
718{
719 int err = -EINVAL;
720
721 if (!*name) {
722 pr_err("overlayfs: empty lowerdir\n");
723 goto out;
724 }
725 err = kern_path(name, LOOKUP_FOLLOW, path);
726 if (err) {
727 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
728 goto out;
729 }
730 err = -EINVAL;
731 if (ovl_dentry_weird(path->dentry)) {
732 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
733 goto out_put;
734 }
735 if (!d_is_dir(path->dentry)) {
736 pr_err("overlayfs: '%s' not a directory\n", name);
737 goto out_put;
738 }
739 return 0;
740
741out_put:
742 path_put_init(path);
743out:
744 return err;
745}
746
747static int ovl_mount_dir(const char *name, struct path *path)
748{
749 int err = -ENOMEM;
750 char *tmp = kstrdup(name, GFP_KERNEL);
751
752 if (tmp) {
753 ovl_unescape(tmp);
754 err = ovl_mount_dir_noesc(tmp, path);
755
756 if (!err)
757 if (ovl_dentry_remote(path->dentry)) {
758 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
759 tmp);
760 path_put_init(path);
761 err = -EINVAL;
762 }
763 kfree(tmp);
764 }
765 return err;
766}
767
768static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
769 const char *name)
770{
771 struct kstatfs statfs;
772 int err = vfs_statfs(path, &statfs);
773
774 if (err)
775 pr_err("overlayfs: statfs failed on '%s'\n", name);
776 else
777 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
778
779 return err;
780}
781
782static int ovl_lower_dir(const char *name, struct path *path,
783 struct ovl_fs *ofs, int *stack_depth, bool *remote)
784{
785 int fh_type;
786 int err;
787
788 err = ovl_mount_dir_noesc(name, path);
789 if (err)
790 goto out;
791
792 err = ovl_check_namelen(path, ofs, name);
793 if (err)
794 goto out_put;
795
796 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
797
798 if (ovl_dentry_remote(path->dentry))
799 *remote = true;
800
801 /*
802 * The inodes index feature and NFS export need to encode and decode
803 * file handles, so they require that all layers support them.
804 */
805 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
806 if ((ofs->config.nfs_export ||
807 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
808 ofs->config.index = false;
809 ofs->config.nfs_export = false;
810 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
811 name);
812 }
813
814 /* Check if lower fs has 32bit inode numbers */
815 if (fh_type != FILEID_INO32_GEN)
816 ofs->xino_bits = 0;
817
818 return 0;
819
820out_put:
821 path_put_init(path);
822out:
823 return err;
824}
825
826/* Workdir should not be subdir of upperdir and vice versa */
827static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
828{
829 bool ok = false;
830
831 if (workdir != upperdir) {
832 ok = (lock_rename(workdir, upperdir) == NULL);
833 unlock_rename(workdir, upperdir);
834 }
835 return ok;
836}
837
838static unsigned int ovl_split_lowerdirs(char *str)
839{
840 unsigned int ctr = 1;
841 char *s, *d;
842
843 for (s = d = str;; s++, d++) {
844 if (*s == '\\') {
845 s++;
846 } else if (*s == ':') {
847 *d = '\0';
848 ctr++;
849 continue;
850 }
851 *d = *s;
852 if (!*s)
853 break;
854 }
855 return ctr;
856}
857
858static int __maybe_unused
859ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
860 struct dentry *dentry, struct inode *inode,
861 const char *name, void *buffer, size_t size)
862{
863 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
864}
865
866static int __maybe_unused
867ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
868 struct dentry *dentry, struct inode *inode,
869 const char *name, const void *value,
870 size_t size, int flags)
871{
872 struct dentry *workdir = ovl_workdir(dentry);
873 struct inode *realinode = ovl_inode_real(inode);
874 struct posix_acl *acl = NULL;
875 int err;
876
877 /* Check that everything is OK before copy-up */
878 if (value) {
879 acl = posix_acl_from_xattr(&init_user_ns, value, size);
880 if (IS_ERR(acl))
881 return PTR_ERR(acl);
882 }
883 err = -EOPNOTSUPP;
884 if (!IS_POSIXACL(d_inode(workdir)))
885 goto out_acl_release;
886 if (!realinode->i_op->set_acl)
887 goto out_acl_release;
888 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
889 err = acl ? -EACCES : 0;
890 goto out_acl_release;
891 }
892 err = -EPERM;
893 if (!inode_owner_or_capable(inode))
894 goto out_acl_release;
895
896 posix_acl_release(acl);
897
898 /*
899 * Check if sgid bit needs to be cleared (actual setacl operation will
900 * be done with mounter's capabilities and so that won't do it for us).
901 */
902 if (unlikely(inode->i_mode & S_ISGID) &&
903 handler->flags == ACL_TYPE_ACCESS &&
904 !in_group_p(inode->i_gid) &&
905 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
906 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
907
908 err = ovl_setattr(dentry, &iattr);
909 if (err)
910 return err;
911 }
912
913 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
914 if (!err)
915 ovl_copyattr(ovl_inode_real(inode), inode);
916
917 return err;
918
919out_acl_release:
920 posix_acl_release(acl);
921 return err;
922}
923
924static int ovl_own_xattr_get(const struct xattr_handler *handler,
925 struct dentry *dentry, struct inode *inode,
926 const char *name, void *buffer, size_t size)
927{
928 return -EOPNOTSUPP;
929}
930
931static int ovl_own_xattr_set(const struct xattr_handler *handler,
932 struct dentry *dentry, struct inode *inode,
933 const char *name, const void *value,
934 size_t size, int flags)
935{
936 return -EOPNOTSUPP;
937}
938
939static int ovl_other_xattr_get(const struct xattr_handler *handler,
940 struct dentry *dentry, struct inode *inode,
941 const char *name, void *buffer, size_t size)
942{
943 return ovl_xattr_get(dentry, inode, name, buffer, size);
944}
945
946static int ovl_other_xattr_set(const struct xattr_handler *handler,
947 struct dentry *dentry, struct inode *inode,
948 const char *name, const void *value,
949 size_t size, int flags)
950{
951 return ovl_xattr_set(dentry, inode, name, value, size, flags);
952}
953
954static const struct xattr_handler __maybe_unused
955ovl_posix_acl_access_xattr_handler = {
956 .name = XATTR_NAME_POSIX_ACL_ACCESS,
957 .flags = ACL_TYPE_ACCESS,
958 .get = ovl_posix_acl_xattr_get,
959 .set = ovl_posix_acl_xattr_set,
960};
961
962static const struct xattr_handler __maybe_unused
963ovl_posix_acl_default_xattr_handler = {
964 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
965 .flags = ACL_TYPE_DEFAULT,
966 .get = ovl_posix_acl_xattr_get,
967 .set = ovl_posix_acl_xattr_set,
968};
969
970static const struct xattr_handler ovl_own_xattr_handler = {
971 .prefix = OVL_XATTR_PREFIX,
972 .get = ovl_own_xattr_get,
973 .set = ovl_own_xattr_set,
974};
975
976static const struct xattr_handler ovl_other_xattr_handler = {
977 .prefix = "", /* catch all */
978 .get = ovl_other_xattr_get,
979 .set = ovl_other_xattr_set,
980};
981
982static const struct xattr_handler *ovl_xattr_handlers[] = {
983#ifdef CONFIG_FS_POSIX_ACL
984 &ovl_posix_acl_access_xattr_handler,
985 &ovl_posix_acl_default_xattr_handler,
986#endif
987 &ovl_own_xattr_handler,
988 &ovl_other_xattr_handler,
989 NULL
990};
991
David Brazdil0f672f62019-12-10 10:32:29 +0000992static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
993 struct inode **ptrap, const char *name)
994{
995 struct inode *trap;
996 int err;
997
998 trap = ovl_get_trap_inode(sb, dir);
999 err = PTR_ERR_OR_ZERO(trap);
1000 if (err) {
1001 if (err == -ELOOP)
1002 pr_err("overlayfs: conflicting %s path\n", name);
1003 return err;
1004 }
1005
1006 *ptrap = trap;
1007 return 0;
1008}
1009
1010/*
1011 * Determine how we treat concurrent use of upperdir/workdir based on the
1012 * index feature. This is papering over mount leaks of container runtimes,
1013 * for example, an old overlay mount is leaked and now its upperdir is
1014 * attempted to be used as a lower layer in a new overlay mount.
1015 */
1016static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1017{
1018 if (ofs->config.index) {
1019 pr_err("overlayfs: %s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
1020 name);
1021 return -EBUSY;
1022 } else {
1023 pr_warn("overlayfs: %s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
1024 name);
1025 return 0;
1026 }
1027}
1028
1029static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1030 struct path *upperpath)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031{
1032 struct vfsmount *upper_mnt;
1033 int err;
1034
1035 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
1036 if (err)
1037 goto out;
1038
1039 /* Upper fs should not be r/o */
1040 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1041 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1042 err = -EINVAL;
1043 goto out;
1044 }
1045
1046 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1047 if (err)
1048 goto out;
1049
David Brazdil0f672f62019-12-10 10:32:29 +00001050 err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
1051 "upperdir");
1052 if (err)
1053 goto out;
1054
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001055 upper_mnt = clone_private_mount(upperpath);
1056 err = PTR_ERR(upper_mnt);
1057 if (IS_ERR(upper_mnt)) {
1058 pr_err("overlayfs: failed to clone upperpath\n");
1059 goto out;
1060 }
1061
1062 /* Don't inherit atime flags */
1063 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1064 ofs->upper_mnt = upper_mnt;
1065
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001066 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1067 ofs->upperdir_locked = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001069 err = ovl_report_in_use(ofs, "upperdir");
1070 if (err)
1071 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001072 }
1073
1074 err = 0;
1075out:
1076 return err;
1077}
1078
David Brazdil0f672f62019-12-10 10:32:29 +00001079static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1080 struct path *workpath)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001081{
1082 struct vfsmount *mnt = ofs->upper_mnt;
1083 struct dentry *temp;
1084 int fh_type;
1085 int err;
1086
1087 err = mnt_want_write(mnt);
1088 if (err)
1089 return err;
1090
1091 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1092 if (!ofs->workdir)
1093 goto out;
1094
David Brazdil0f672f62019-12-10 10:32:29 +00001095 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1096 if (err)
1097 goto out;
1098
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 /*
1100 * Upper should support d_type, else whiteouts are visible. Given
1101 * workdir and upper are on same fs, we can do iterate_dir() on
1102 * workdir. This check requires successful creation of workdir in
1103 * previous step.
1104 */
1105 err = ovl_check_d_type_supported(workpath);
1106 if (err < 0)
1107 goto out;
1108
1109 /*
1110 * We allowed this configuration and don't want to break users over
1111 * kernel upgrade. So warn instead of erroring out.
1112 */
1113 if (!err)
1114 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1115
1116 /* Check if upper/work fs supports O_TMPFILE */
1117 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1118 ofs->tmpfile = !IS_ERR(temp);
1119 if (ofs->tmpfile)
1120 dput(temp);
1121 else
1122 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1123
1124 /*
1125 * Check if upper/work fs supports trusted.overlay.* xattr
1126 */
1127 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
1128 if (err) {
1129 ofs->noxattr = true;
1130 ofs->config.index = false;
1131 ofs->config.metacopy = false;
1132 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1133 err = 0;
1134 } else {
1135 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
1136 }
1137
1138 /* Check if upper/work fs supports file handles */
1139 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1140 if (ofs->config.index && !fh_type) {
1141 ofs->config.index = false;
1142 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1143 }
1144
1145 /* Check if upper fs has 32bit inode numbers */
1146 if (fh_type != FILEID_INO32_GEN)
1147 ofs->xino_bits = 0;
1148
1149 /* NFS export of r/w mount depends on index */
1150 if (ofs->config.nfs_export && !ofs->config.index) {
1151 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1152 ofs->config.nfs_export = false;
1153 }
1154out:
1155 mnt_drop_write(mnt);
1156 return err;
1157}
1158
David Brazdil0f672f62019-12-10 10:32:29 +00001159static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1160 struct path *upperpath)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001161{
1162 int err;
1163 struct path workpath = { };
1164
1165 err = ovl_mount_dir(ofs->config.workdir, &workpath);
1166 if (err)
1167 goto out;
1168
1169 err = -EINVAL;
1170 if (upperpath->mnt != workpath.mnt) {
1171 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1172 goto out;
1173 }
1174 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1175 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1176 goto out;
1177 }
1178
1179 ofs->workbasedir = dget(workpath.dentry);
1180
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001181 if (ovl_inuse_trylock(ofs->workbasedir)) {
1182 ofs->workdir_locked = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001183 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001184 err = ovl_report_in_use(ofs, "workdir");
1185 if (err)
1186 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001187 }
1188
David Brazdil0f672f62019-12-10 10:32:29 +00001189 err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1190 "workdir");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191 if (err)
1192 goto out;
1193
David Brazdil0f672f62019-12-10 10:32:29 +00001194 err = ovl_make_workdir(sb, ofs, &workpath);
1195
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001196out:
1197 path_put(&workpath);
1198
1199 return err;
1200}
1201
David Brazdil0f672f62019-12-10 10:32:29 +00001202static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1203 struct ovl_entry *oe, struct path *upperpath)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204{
1205 struct vfsmount *mnt = ofs->upper_mnt;
1206 int err;
1207
1208 err = mnt_want_write(mnt);
1209 if (err)
1210 return err;
1211
1212 /* Verify lower root is upper root origin */
1213 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
1214 true);
1215 if (err) {
1216 pr_err("overlayfs: failed to verify upper root origin\n");
1217 goto out;
1218 }
1219
1220 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1221 if (ofs->indexdir) {
David Brazdil0f672f62019-12-10 10:32:29 +00001222 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1223 "indexdir");
1224 if (err)
1225 goto out;
1226
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001227 /*
1228 * Verify upper root is exclusively associated with index dir.
1229 * Older kernels stored upper fh in "trusted.overlay.origin"
1230 * xattr. If that xattr exists, verify that it is a match to
1231 * upper dir file handle. In any case, verify or set xattr
1232 * "trusted.overlay.upper" to indicate that index may have
1233 * directory entries.
1234 */
1235 if (ovl_check_origin_xattr(ofs->indexdir)) {
1236 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1237 upperpath->dentry, true, false);
1238 if (err)
1239 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1240 }
1241 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
1242 if (err)
1243 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
1244
1245 /* Cleanup bad/stale/orphan index entries */
1246 if (!err)
1247 err = ovl_indexdir_cleanup(ofs);
1248 }
1249 if (err || !ofs->indexdir)
1250 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1251
1252out:
1253 mnt_drop_write(mnt);
1254 return err;
1255}
1256
David Brazdil0f672f62019-12-10 10:32:29 +00001257static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001258{
1259 unsigned int i;
David Brazdil0f672f62019-12-10 10:32:29 +00001260
Olivier Deprez0e641232021-09-23 10:07:05 +02001261 if (!ofs->config.nfs_export && !ofs->upper_mnt)
David Brazdil0f672f62019-12-10 10:32:29 +00001262 return true;
1263
Olivier Deprez0e641232021-09-23 10:07:05 +02001264 /*
1265 * We allow using single lower with null uuid for index and nfs_export
1266 * for example to support those features with single lower squashfs.
1267 * To avoid regressions in setups of overlay with re-formatted lower
1268 * squashfs, do not allow decoding origin with lower null uuid unless
1269 * user opted-in to one of the new features that require following the
1270 * lower inode of non-dir upper.
1271 */
1272 if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
1273 uuid_is_null(uuid))
1274 return false;
1275
David Brazdil0f672f62019-12-10 10:32:29 +00001276 for (i = 0; i < ofs->numlowerfs; i++) {
1277 /*
1278 * We use uuid to associate an overlay lower file handle with a
1279 * lower layer, so we can accept lower fs with null uuid as long
1280 * as all lower layers with null uuid are on the same fs.
Olivier Deprez0e641232021-09-23 10:07:05 +02001281 * if we detect multiple lower fs with the same uuid, we
1282 * disable lower file handle decoding on all of them.
David Brazdil0f672f62019-12-10 10:32:29 +00001283 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001284 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid)) {
1285 ofs->lower_fs[i].bad_uuid = true;
David Brazdil0f672f62019-12-10 10:32:29 +00001286 return false;
Olivier Deprez0e641232021-09-23 10:07:05 +02001287 }
David Brazdil0f672f62019-12-10 10:32:29 +00001288 }
1289 return true;
1290}
1291
1292/* Get a unique fsid for the layer */
1293static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1294{
1295 struct super_block *sb = path->mnt->mnt_sb;
1296 unsigned int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001297 dev_t dev;
1298 int err;
Olivier Deprez0e641232021-09-23 10:07:05 +02001299 bool bad_uuid = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001300
1301 /* fsid 0 is reserved for upper fs even with non upper overlay */
1302 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1303 return 0;
1304
1305 for (i = 0; i < ofs->numlowerfs; i++) {
1306 if (ofs->lower_fs[i].sb == sb)
1307 return i + 1;
1308 }
1309
David Brazdil0f672f62019-12-10 10:32:29 +00001310 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001311 bad_uuid = true;
1312 if (ofs->config.index || ofs->config.nfs_export) {
1313 ofs->config.index = false;
1314 ofs->config.nfs_export = false;
1315 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1316 uuid_is_null(&sb->s_uuid) ? "null" :
1317 "conflicting",
1318 path->dentry);
1319 }
David Brazdil0f672f62019-12-10 10:32:29 +00001320 }
1321
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001322 err = get_anon_bdev(&dev);
1323 if (err) {
1324 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1325 return err;
1326 }
1327
1328 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1329 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
Olivier Deprez0e641232021-09-23 10:07:05 +02001330 ofs->lower_fs[ofs->numlowerfs].bad_uuid = bad_uuid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001331 ofs->numlowerfs++;
1332
1333 return ofs->numlowerfs;
1334}
1335
David Brazdil0f672f62019-12-10 10:32:29 +00001336static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs,
1337 struct path *stack, unsigned int numlower)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001338{
1339 int err;
1340 unsigned int i;
1341
1342 err = -ENOMEM;
1343 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1344 GFP_KERNEL);
1345 if (ofs->lower_layers == NULL)
1346 goto out;
1347
1348 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1349 GFP_KERNEL);
1350 if (ofs->lower_fs == NULL)
1351 goto out;
1352
1353 for (i = 0; i < numlower; i++) {
1354 struct vfsmount *mnt;
David Brazdil0f672f62019-12-10 10:32:29 +00001355 struct inode *trap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356 int fsid;
1357
David Brazdil0f672f62019-12-10 10:32:29 +00001358 err = fsid = ovl_get_fsid(ofs, &stack[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001359 if (err < 0)
1360 goto out;
1361
Olivier Deprez0e641232021-09-23 10:07:05 +02001362 /*
1363 * Check if lower root conflicts with this overlay layers before
1364 * checking if it is in-use as upperdir/workdir of "another"
1365 * mount, because we do not bother to check in ovl_is_inuse() if
1366 * the upperdir/workdir is in fact in-use by our
1367 * upperdir/workdir.
1368 */
David Brazdil0f672f62019-12-10 10:32:29 +00001369 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1370 if (err)
1371 goto out;
1372
1373 if (ovl_is_inuse(stack[i].dentry)) {
1374 err = ovl_report_in_use(ofs, "lowerdir");
Olivier Deprez0e641232021-09-23 10:07:05 +02001375 if (err) {
1376 iput(trap);
David Brazdil0f672f62019-12-10 10:32:29 +00001377 goto out;
Olivier Deprez0e641232021-09-23 10:07:05 +02001378 }
David Brazdil0f672f62019-12-10 10:32:29 +00001379 }
1380
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001381 mnt = clone_private_mount(&stack[i]);
1382 err = PTR_ERR(mnt);
1383 if (IS_ERR(mnt)) {
1384 pr_err("overlayfs: failed to clone lowerpath\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001385 iput(trap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001386 goto out;
1387 }
1388
1389 /*
1390 * Make lower layers R/O. That way fchmod/fchown on lower file
1391 * will fail instead of modifying lower fs.
1392 */
1393 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1394
David Brazdil0f672f62019-12-10 10:32:29 +00001395 ofs->lower_layers[ofs->numlower].trap = trap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001396 ofs->lower_layers[ofs->numlower].mnt = mnt;
1397 ofs->lower_layers[ofs->numlower].idx = i + 1;
1398 ofs->lower_layers[ofs->numlower].fsid = fsid;
1399 if (fsid) {
1400 ofs->lower_layers[ofs->numlower].fs =
1401 &ofs->lower_fs[fsid - 1];
1402 }
1403 ofs->numlower++;
1404 }
1405
1406 /*
1407 * When all layers on same fs, overlay can use real inode numbers.
1408 * With mount option "xino=on", mounter declares that there are enough
1409 * free high bits in underlying fs to hold the unique fsid.
1410 * If overlayfs does encounter underlying inodes using the high xino
1411 * bits reserved for fsid, it emits a warning and uses the original
1412 * inode number.
1413 */
1414 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
1415 ofs->xino_bits = 0;
1416 ofs->config.xino = OVL_XINO_OFF;
1417 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1418 /*
1419 * This is a roundup of number of bits needed for numlowerfs+1
1420 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1421 * upper fs even with non upper overlay.
1422 */
1423 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1424 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1425 }
1426
1427 if (ofs->xino_bits) {
1428 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1429 ofs->xino_bits);
1430 }
1431
1432 err = 0;
1433out:
1434 return err;
1435}
1436
1437static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1438 struct ovl_fs *ofs)
1439{
1440 int err;
1441 char *lowertmp, *lower;
1442 struct path *stack = NULL;
1443 unsigned int stacklen, numlower = 0, i;
1444 bool remote = false;
1445 struct ovl_entry *oe;
1446
1447 err = -ENOMEM;
1448 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1449 if (!lowertmp)
1450 goto out_err;
1451
1452 err = -EINVAL;
1453 stacklen = ovl_split_lowerdirs(lowertmp);
1454 if (stacklen > OVL_MAX_STACK) {
1455 pr_err("overlayfs: too many lower directories, limit is %d\n",
1456 OVL_MAX_STACK);
1457 goto out_err;
1458 } else if (!ofs->config.upperdir && stacklen == 1) {
1459 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1460 goto out_err;
1461 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1462 ofs->config.redirect_follow) {
1463 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1464 ofs->config.nfs_export = false;
1465 }
1466
1467 err = -ENOMEM;
1468 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1469 if (!stack)
1470 goto out_err;
1471
1472 err = -EINVAL;
1473 lower = lowertmp;
1474 for (numlower = 0; numlower < stacklen; numlower++) {
1475 err = ovl_lower_dir(lower, &stack[numlower], ofs,
1476 &sb->s_stack_depth, &remote);
1477 if (err)
1478 goto out_err;
1479
1480 lower = strchr(lower, '\0') + 1;
1481 }
1482
1483 err = -EINVAL;
1484 sb->s_stack_depth++;
1485 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1486 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1487 goto out_err;
1488 }
1489
David Brazdil0f672f62019-12-10 10:32:29 +00001490 err = ovl_get_lower_layers(sb, ofs, stack, numlower);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001491 if (err)
1492 goto out_err;
1493
1494 err = -ENOMEM;
1495 oe = ovl_alloc_entry(numlower);
1496 if (!oe)
1497 goto out_err;
1498
1499 for (i = 0; i < numlower; i++) {
1500 oe->lowerstack[i].dentry = dget(stack[i].dentry);
1501 oe->lowerstack[i].layer = &ofs->lower_layers[i];
1502 }
1503
1504 if (remote)
1505 sb->s_d_op = &ovl_reval_dentry_operations;
1506 else
1507 sb->s_d_op = &ovl_dentry_operations;
1508
1509out:
1510 for (i = 0; i < numlower; i++)
1511 path_put(&stack[i]);
1512 kfree(stack);
1513 kfree(lowertmp);
1514
1515 return oe;
1516
1517out_err:
1518 oe = ERR_PTR(err);
1519 goto out;
1520}
1521
David Brazdil0f672f62019-12-10 10:32:29 +00001522/*
1523 * Check if this layer root is a descendant of:
1524 * - another layer of this overlayfs instance
1525 * - upper/work dir of any overlayfs instance
1526 */
1527static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
Olivier Deprez0e641232021-09-23 10:07:05 +02001528 struct dentry *dentry, const char *name,
1529 bool is_lower)
David Brazdil0f672f62019-12-10 10:32:29 +00001530{
1531 struct dentry *next = dentry, *parent;
1532 int err = 0;
1533
1534 if (!dentry)
1535 return 0;
1536
1537 parent = dget_parent(next);
1538
1539 /* Walk back ancestors to root (inclusive) looking for traps */
1540 while (!err && parent != next) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001541 if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001542 err = -ELOOP;
1543 pr_err("overlayfs: overlapping %s path\n", name);
1544 } else if (ovl_is_inuse(parent)) {
1545 err = ovl_report_in_use(ofs, name);
1546 }
1547 next = parent;
1548 parent = dget_parent(next);
1549 dput(next);
1550 }
1551
1552 dput(parent);
1553
1554 return err;
1555}
1556
1557/*
1558 * Check if any of the layers or work dirs overlap.
1559 */
1560static int ovl_check_overlapping_layers(struct super_block *sb,
1561 struct ovl_fs *ofs)
1562{
1563 int i, err;
1564
1565 if (ofs->upper_mnt) {
1566 err = ovl_check_layer(sb, ofs, ofs->upper_mnt->mnt_root,
Olivier Deprez0e641232021-09-23 10:07:05 +02001567 "upperdir", false);
David Brazdil0f672f62019-12-10 10:32:29 +00001568 if (err)
1569 return err;
1570
1571 /*
1572 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1573 * this instance and covers overlapping work and index dirs,
1574 * unless work or index dir have been moved since created inside
1575 * workbasedir. In that case, we already have their traps in
1576 * inode cache and we will catch that case on lookup.
1577 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001578 err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1579 false);
David Brazdil0f672f62019-12-10 10:32:29 +00001580 if (err)
1581 return err;
1582 }
1583
1584 for (i = 0; i < ofs->numlower; i++) {
1585 err = ovl_check_layer(sb, ofs,
1586 ofs->lower_layers[i].mnt->mnt_root,
Olivier Deprez0e641232021-09-23 10:07:05 +02001587 "lowerdir", true);
David Brazdil0f672f62019-12-10 10:32:29 +00001588 if (err)
1589 return err;
1590 }
1591
1592 return 0;
1593}
1594
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001595static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1596{
1597 struct path upperpath = { };
1598 struct dentry *root_dentry;
1599 struct ovl_entry *oe;
1600 struct ovl_fs *ofs;
1601 struct cred *cred;
1602 int err;
1603
1604 err = -ENOMEM;
1605 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1606 if (!ofs)
1607 goto out;
1608
1609 ofs->creator_cred = cred = prepare_creds();
1610 if (!cred)
1611 goto out_err;
1612
1613 ofs->config.index = ovl_index_def;
1614 ofs->config.nfs_export = ovl_nfs_export_def;
1615 ofs->config.xino = ovl_xino_def();
1616 ofs->config.metacopy = ovl_metacopy_def;
1617 err = ovl_parse_opt((char *) data, &ofs->config);
1618 if (err)
1619 goto out_err;
1620
1621 err = -EINVAL;
1622 if (!ofs->config.lowerdir) {
1623 if (!silent)
1624 pr_err("overlayfs: missing 'lowerdir'\n");
1625 goto out_err;
1626 }
1627
1628 sb->s_stack_depth = 0;
1629 sb->s_maxbytes = MAX_LFS_FILESIZE;
1630 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
1631 if (ofs->config.xino != OVL_XINO_OFF)
1632 ofs->xino_bits = BITS_PER_LONG - 32;
1633
David Brazdil0f672f62019-12-10 10:32:29 +00001634 /* alloc/destroy_inode needed for setting up traps in inode cache */
1635 sb->s_op = &ovl_super_operations;
1636
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001637 if (ofs->config.upperdir) {
1638 if (!ofs->config.workdir) {
1639 pr_err("overlayfs: missing 'workdir'\n");
1640 goto out_err;
1641 }
1642
David Brazdil0f672f62019-12-10 10:32:29 +00001643 err = ovl_get_upper(sb, ofs, &upperpath);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001644 if (err)
1645 goto out_err;
1646
David Brazdil0f672f62019-12-10 10:32:29 +00001647 err = ovl_get_workdir(sb, ofs, &upperpath);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001648 if (err)
1649 goto out_err;
1650
1651 if (!ofs->workdir)
1652 sb->s_flags |= SB_RDONLY;
1653
1654 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1655 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
1656
1657 }
1658 oe = ovl_get_lowerstack(sb, ofs);
1659 err = PTR_ERR(oe);
1660 if (IS_ERR(oe))
1661 goto out_err;
1662
1663 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1664 if (!ofs->upper_mnt)
1665 sb->s_flags |= SB_RDONLY;
1666
1667 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
David Brazdil0f672f62019-12-10 10:32:29 +00001668 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001669 if (err)
1670 goto out_free_oe;
1671
1672 /* Force r/o mount with no index dir */
1673 if (!ofs->indexdir) {
1674 dput(ofs->workdir);
1675 ofs->workdir = NULL;
1676 sb->s_flags |= SB_RDONLY;
1677 }
1678
1679 }
1680
David Brazdil0f672f62019-12-10 10:32:29 +00001681 err = ovl_check_overlapping_layers(sb, ofs);
1682 if (err)
1683 goto out_free_oe;
1684
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001685 /* Show index=off in /proc/mounts for forced r/o mount */
1686 if (!ofs->indexdir) {
1687 ofs->config.index = false;
1688 if (ofs->upper_mnt && ofs->config.nfs_export) {
1689 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1690 ofs->config.nfs_export = false;
1691 }
1692 }
1693
1694 if (ofs->config.metacopy && ofs->config.nfs_export) {
1695 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1696 ofs->config.nfs_export = false;
1697 }
1698
1699 if (ofs->config.nfs_export)
1700 sb->s_export_op = &ovl_export_operations;
1701
1702 /* Never override disk quota limits or use reserved space */
1703 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1704
1705 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001706 sb->s_xattr = ovl_xattr_handlers;
1707 sb->s_fs_info = ofs;
1708 sb->s_flags |= SB_POSIXACL;
1709
1710 err = -ENOMEM;
1711 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1712 if (!root_dentry)
1713 goto out_free_oe;
1714
1715 root_dentry->d_fsdata = oe;
1716
1717 mntput(upperpath.mnt);
1718 if (upperpath.dentry) {
1719 ovl_dentry_set_upper_alias(root_dentry);
1720 if (ovl_is_impuredir(upperpath.dentry))
1721 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1722 }
1723
1724 /* Root is always merge -> can have whiteouts */
1725 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
1726 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
1727 ovl_set_upperdata(d_inode(root_dentry));
1728 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1729 ovl_dentry_lower(root_dentry), NULL);
1730
1731 sb->s_root = root_dentry;
1732
1733 return 0;
1734
1735out_free_oe:
1736 ovl_entry_stack_free(oe);
1737 kfree(oe);
1738out_err:
1739 path_put(&upperpath);
1740 ovl_free_fs(ofs);
1741out:
1742 return err;
1743}
1744
1745static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1746 const char *dev_name, void *raw_data)
1747{
1748 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1749}
1750
1751static struct file_system_type ovl_fs_type = {
1752 .owner = THIS_MODULE,
1753 .name = "overlay",
1754 .mount = ovl_mount,
1755 .kill_sb = kill_anon_super,
1756};
1757MODULE_ALIAS_FS("overlay");
1758
1759static void ovl_inode_init_once(void *foo)
1760{
1761 struct ovl_inode *oi = foo;
1762
1763 inode_init_once(&oi->vfs_inode);
1764}
1765
1766static int __init ovl_init(void)
1767{
1768 int err;
1769
1770 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1771 sizeof(struct ovl_inode), 0,
1772 (SLAB_RECLAIM_ACCOUNT|
1773 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1774 ovl_inode_init_once);
1775 if (ovl_inode_cachep == NULL)
1776 return -ENOMEM;
1777
1778 err = register_filesystem(&ovl_fs_type);
1779 if (err)
1780 kmem_cache_destroy(ovl_inode_cachep);
1781
1782 return err;
1783}
1784
1785static void __exit ovl_exit(void)
1786{
1787 unregister_filesystem(&ovl_fs_type);
1788
1789 /*
1790 * Make sure all delayed rcu free inodes are flushed before we
1791 * destroy cache.
1792 */
1793 rcu_barrier();
1794 kmem_cache_destroy(ovl_inode_cachep);
1795
1796}
1797
1798module_init(ovl_init);
1799module_exit(ovl_exit);