Skip to content

Commit d579104

Browse files
rhvgoyalMiklos Szeredi
authored andcommitted
ovl: Provide a mount option metacopy=on/off for metadata copyup
By default metadata only copy up is disabled. Provide a mount option so that users can choose one way or other. Also provide a kernel config and module option to enable/disable metacopy feature. metacopy feature requires redirect_dir=on when upper is present. Otherwise, it requires redirect_dir=follow atleast. As of now, metacopy does not work with nfs_export=on. So if both metacopy=on and nfs_export=on then nfs_export is disabled. Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent d6eac03 commit d579104

4 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎Documentation/filesystems/overlayfs.txt‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,30 @@ rightmost one and going left. In the above example lower1 will be the
262262
top, lower2 the middle and lower3 the bottom layer.
263263

264264

265+
Metadata only copy up
266+
--------------------
267+
268+
When metadata only copy up feature is enabled, overlayfs will only copy
269+
up metadata (as opposed to whole file), when a metadata specific operation
270+
like chown/chmod is performed. Full file will be copied up later when
271+
file is opened for WRITE operation.
272+
273+
In other words, this is delayed data copy up operation and data is copied
274+
up when there is a need to actually modify data.
275+
276+
There are multiple ways to enable/disable this feature. A config option
277+
CONFIG_OVERLAY_FS_METACOPY can be set/unset to enable/disable this feature
278+
by default. Or one can enable/disable it at module load time with module
279+
parameter metacopy=on/off. Lastly, there is also a per mount option
280+
metacopy=on/off to enable/disable this feature per mount.
281+
282+
Do not use metacopy=on with untrusted upper/lower directories. Otherwise
283+
it is possible that an attacker can create a handcrafted file with
284+
appropriate REDIRECT and METACOPY xattrs, and gain access to file on lower
285+
pointed by REDIRECT. This should not be possible on local system as setting
286+
"trusted." xattrs will require CAP_SYS_ADMIN. But it should be possible
287+
for untrusted layers like from a pen drive.
288+
265289
Sharing and copying layers
266290
--------------------------
267291

@@ -280,7 +304,7 @@ though it will not result in a crash or deadlock.
280304
Mounting an overlay using an upper layer path, where the upper layer path
281305
was previously used by another mounted overlay in combination with a
282306
different lower layer path, is allowed, unless the "inodes index" feature
283-
is enabled.
307+
or "metadata only copy up" feature is enabled.
284308

285309
With the "inodes index" feature, on the first time mount, an NFS file
286310
handle of the lower layer root directory, along with the UUID of the lower
@@ -293,6 +317,10 @@ lower root origin, mount will fail with ESTALE. An overlayfs mount with
293317
does not support NFS export, lower filesystem does not have a valid UUID or
294318
if the upper filesystem does not support extended attributes.
295319

320+
For "metadata only copy up" feature there is no verification mechanism at
321+
mount time. So if same upper is mounted with different set of lower, mount
322+
probably will succeed but expect the unexpected later on. So don't do it.
323+
296324
It is quite a common practice to copy overlay layers to a different
297325
directory tree on the same or different underlying filesystem, and even
298326
to a different machine. With the "inodes index" feature, trying to mount

‎fs/overlayfs/Kconfig‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ config OVERLAY_FS_NFS_EXPORT
6464
bool "Overlayfs: turn on NFS export feature by default"
6565
depends on OVERLAY_FS
6666
depends on OVERLAY_FS_INDEX
67+
depends on !OVERLAY_FS_METACOPY
6768
help
6869
If this config option is enabled then overlay filesystems will use
6970
the index directory to decode overlay NFS file handles by default.
@@ -103,3 +104,21 @@ config OVERLAY_FS_XINO_AUTO
103104
For more information, see Documentation/filesystems/overlayfs.txt
104105

105106
If unsure, say N.
107+
108+
config OVERLAY_FS_METACOPY
109+
bool "Overlayfs: turn on metadata only copy up feature by default"
110+
depends on OVERLAY_FS
111+
select OVERLAY_FS_REDIRECT_DIR
112+
help
113+
If this config option is enabled then overlay filesystems will
114+
copy up only metadata where appropriate and data copy up will
115+
happen when a file is opened for WRITE operation. It is still
116+
possible to turn off this feature globally with the "metacopy=off"
117+
module option or on a filesystem instance basis with the
118+
"metacopy=off" mount option.
119+
120+
Note, that this feature is not backward compatible. That is,
121+
mounting an overlay which has metacopy only inodes on a kernel
122+
that doesn't support this feature will have unexpected results.
123+
124+
If unsure, say N.

‎fs/overlayfs/ovl_entry.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct ovl_config {
1919
bool index;
2020
bool nfs_export;
2121
int xino;
22+
bool metacopy;
2223
};
2324

2425
struct ovl_sb {

‎fs/overlayfs/super.c‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ static void ovl_entry_stack_free(struct ovl_entry *oe)
6464
dput(oe->lowerstack[i].dentry);
6565
}
6666

67+
static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
68+
module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
69+
MODULE_PARM_DESC(ovl_metacopy_def,
70+
"Default to on or off for the metadata only copy up feature");
71+
6772
static void ovl_dentry_release(struct dentry *dentry)
6873
{
6974
struct ovl_entry *oe = dentry->d_fsdata;
@@ -347,6 +352,9 @@ static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
347352
"on" : "off");
348353
if (ofs->config.xino != ovl_xino_def())
349354
seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
355+
if (ofs->config.metacopy != ovl_metacopy_def)
356+
seq_printf(m, ",metacopy=%s",
357+
ofs->config.metacopy ? "on" : "off");
350358
return 0;
351359
}
352360

@@ -384,6 +392,8 @@ enum {
384392
OPT_XINO_ON,
385393
OPT_XINO_OFF,
386394
OPT_XINO_AUTO,
395+
OPT_METACOPY_ON,
396+
OPT_METACOPY_OFF,
387397
OPT_ERR,
388398
};
389399

@@ -400,6 +410,8 @@ static const match_table_t ovl_tokens = {
400410
{OPT_XINO_ON, "xino=on"},
401411
{OPT_XINO_OFF, "xino=off"},
402412
{OPT_XINO_AUTO, "xino=auto"},
413+
{OPT_METACOPY_ON, "metacopy=on"},
414+
{OPT_METACOPY_OFF, "metacopy=off"},
403415
{OPT_ERR, NULL}
404416
};
405417

@@ -452,6 +464,7 @@ static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
452464
static int ovl_parse_opt(char *opt, struct ovl_config *config)
453465
{
454466
char *p;
467+
int err;
455468

456469
config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
457470
if (!config->redirect_mode)
@@ -526,6 +539,14 @@ static int ovl_parse_opt(char *opt, struct ovl_config *config)
526539
config->xino = OVL_XINO_AUTO;
527540
break;
528541

542+
case OPT_METACOPY_ON:
543+
config->metacopy = true;
544+
break;
545+
546+
case OPT_METACOPY_OFF:
547+
config->metacopy = false;
548+
break;
549+
529550
default:
530551
pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
531552
return -EINVAL;
@@ -540,7 +561,20 @@ static int ovl_parse_opt(char *opt, struct ovl_config *config)
540561
config->workdir = NULL;
541562
}
542563

543-
return ovl_parse_redirect_mode(config, config->redirect_mode);
564+
err = ovl_parse_redirect_mode(config, config->redirect_mode);
565+
if (err)
566+
return err;
567+
568+
/* metacopy feature with upper requires redirect_dir=on */
569+
if (config->upperdir && config->metacopy && !config->redirect_dir) {
570+
pr_warn("overlayfs: metadata only copy up requires \"redirect_dir=on\", falling back to metacopy=off.\n");
571+
config->metacopy = false;
572+
} else if (config->metacopy && !config->redirect_follow) {
573+
pr_warn("overlayfs: metadata only copy up requires \"redirect_dir=follow\" on non-upper mount, falling back to metacopy=off.\n");
574+
config->metacopy = false;
575+
}
576+
577+
return 0;
544578
}
545579

546580
#define OVL_WORKDIR_NAME "work"
@@ -1013,7 +1047,8 @@ static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
10131047
if (err) {
10141048
ofs->noxattr = true;
10151049
ofs->config.index = false;
1016-
pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
1050+
ofs->config.metacopy = false;
1051+
pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
10171052
err = 0;
10181053
} else {
10191054
vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
@@ -1035,7 +1070,6 @@ static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
10351070
pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
10361071
ofs->config.nfs_export = false;
10371072
}
1038-
10391073
out:
10401074
mnt_drop_write(mnt);
10411075
return err;
@@ -1346,6 +1380,7 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
13461380
ofs->config.index = ovl_index_def;
13471381
ofs->config.nfs_export = ovl_nfs_export_def;
13481382
ofs->config.xino = ovl_xino_def();
1383+
ofs->config.metacopy = ovl_metacopy_def;
13491384
err = ovl_parse_opt((char *) data, &ofs->config);
13501385
if (err)
13511386
goto out_err;
@@ -1416,6 +1451,11 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
14161451
}
14171452
}
14181453

1454+
if (ofs->config.metacopy && ofs->config.nfs_export) {
1455+
pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1456+
ofs->config.nfs_export = false;
1457+
}
1458+
14191459
if (ofs->config.nfs_export)
14201460
sb->s_export_op = &ovl_export_operations;
14211461

0 commit comments

Comments
 (0)