Commit 42dd69ae authored by Amir Goldstein's avatar Amir Goldstein

ovl: implement lazy lookup of lowerdata in data-only layers

Defer lookup of lowerdata in the data-only layers to first data access
or before copy up.

We perform lowerdata lookup before copy up even if copy up is metadata
only copy up.  We can further optimize this lookup later if needed.

We do best effort lazy lookup of lowerdata for d_real_inode(), because
this interface does not expect errors.  The only current in-tree caller
of d_real_inode() is trace_uprobe and this caller is likely going to be
followed reading from the file, before placing uprobes on offset within
the file, so lowerdata should be available when setting the uprobe.
Tested-by: default avatarkernel test robot <oliver.sang@intel.com>
Reviewed-by: default avatarAlexander Larsson <alexl@redhat.com>
Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
Signed-off-by: default avatarMiklos Szeredi <mszeredi@redhat.com>
parent 41665644
...@@ -1073,6 +1073,15 @@ static int ovl_copy_up_flags(struct dentry *dentry, int flags) ...@@ -1073,6 +1073,15 @@ static int ovl_copy_up_flags(struct dentry *dentry, int flags)
if (WARN_ON(disconnected && d_is_dir(dentry))) if (WARN_ON(disconnected && d_is_dir(dentry)))
return -EIO; return -EIO;
/*
* We may not need lowerdata if we are only doing metacopy up, but it is
* not very important to optimize this case, so do lazy lowerdata lookup
* before any copy up, so we can do it before taking ovl_inode_lock().
*/
err = ovl_maybe_lookup_lowerdata(dentry);
if (err)
return err;
old_cred = ovl_override_creds(dentry->d_sb); old_cred = ovl_override_creds(dentry->d_sb);
while (!err) { while (!err) {
struct dentry *next; struct dentry *next;
......
...@@ -107,15 +107,21 @@ static int ovl_real_fdget_meta(const struct file *file, struct fd *real, ...@@ -107,15 +107,21 @@ static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
{ {
struct dentry *dentry = file_dentry(file); struct dentry *dentry = file_dentry(file);
struct path realpath; struct path realpath;
int err;
real->flags = 0; real->flags = 0;
real->file = file->private_data; real->file = file->private_data;
if (allow_meta) if (allow_meta) {
ovl_path_real(dentry, &realpath); ovl_path_real(dentry, &realpath);
else } else {
/* lazy lookup of lowerdata */
err = ovl_maybe_lookup_lowerdata(dentry);
if (err)
return err;
ovl_path_realdata(dentry, &realpath); ovl_path_realdata(dentry, &realpath);
/* TODO: lazy lookup of lowerdata */ }
if (!realpath.dentry) if (!realpath.dentry)
return -EIO; return -EIO;
...@@ -153,6 +159,11 @@ static int ovl_open(struct inode *inode, struct file *file) ...@@ -153,6 +159,11 @@ static int ovl_open(struct inode *inode, struct file *file)
struct path realpath; struct path realpath;
int err; int err;
/* lazy lookup of lowerdata */
err = ovl_maybe_lookup_lowerdata(dentry);
if (err)
return err;
err = ovl_maybe_copy_up(dentry, file->f_flags); err = ovl_maybe_copy_up(dentry, file->f_flags);
if (err) if (err)
return err; return err;
...@@ -161,7 +172,6 @@ static int ovl_open(struct inode *inode, struct file *file) ...@@ -161,7 +172,6 @@ static int ovl_open(struct inode *inode, struct file *file)
file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
ovl_path_realdata(dentry, &realpath); ovl_path_realdata(dentry, &realpath);
/* TODO: lazy lookup of lowerdata */
if (!realpath.dentry) if (!realpath.dentry)
return -EIO; return -EIO;
......
...@@ -889,6 +889,52 @@ static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry, ...@@ -889,6 +889,52 @@ static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
return err; return err;
} }
/* Lazy lookup of lowerdata */
int ovl_maybe_lookup_lowerdata(struct dentry *dentry)
{
struct inode *inode = d_inode(dentry);
const char *redirect = ovl_lowerdata_redirect(inode);
struct ovl_path datapath = {};
const struct cred *old_cred;
int err;
if (!redirect || ovl_dentry_lowerdata(dentry))
return 0;
if (redirect[0] != '/')
return -EIO;
err = ovl_inode_lock_interruptible(inode);
if (err)
return err;
err = 0;
/* Someone got here before us? */
if (ovl_dentry_lowerdata(dentry))
goto out;
old_cred = ovl_override_creds(dentry->d_sb);
err = ovl_lookup_data_layers(dentry, redirect, &datapath);
revert_creds(old_cred);
if (err)
goto out_err;
err = ovl_dentry_set_lowerdata(dentry, &datapath);
if (err)
goto out_err;
out:
ovl_inode_unlock(inode);
dput(datapath.dentry);
return err;
out_err:
pr_warn_ratelimited("lazy lowerdata lookup failed (%pd2, err=%i)\n",
dentry, err);
goto out;
}
struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags) unsigned int flags)
{ {
...@@ -1072,14 +1118,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -1072,14 +1118,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
} }
} }
/* Lookup absolute redirect from lower metacopy in data-only layers */ /* Defer lookup of lowerdata in data-only layers to first access */
if (d.metacopy && ctr && ofs->numdatalayer && d.absolute_redirect) { if (d.metacopy && ctr && ofs->numdatalayer && d.absolute_redirect) {
err = ovl_lookup_data_layers(dentry, d.redirect, d.metacopy = false;
&stack[ctr]); ctr++;
if (!err) {
d.metacopy = false;
ctr++;
}
} }
/* /*
......
...@@ -396,6 +396,7 @@ enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path); ...@@ -396,6 +396,7 @@ enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path);
struct dentry *ovl_dentry_upper(struct dentry *dentry); struct dentry *ovl_dentry_upper(struct dentry *dentry);
struct dentry *ovl_dentry_lower(struct dentry *dentry); struct dentry *ovl_dentry_lower(struct dentry *dentry);
struct dentry *ovl_dentry_lowerdata(struct dentry *dentry); struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath);
const struct ovl_layer *ovl_i_layer_lower(struct inode *inode); const struct ovl_layer *ovl_i_layer_lower(struct inode *inode);
const struct ovl_layer *ovl_layer_lower(struct dentry *dentry); const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
struct dentry *ovl_dentry_real(struct dentry *dentry); struct dentry *ovl_dentry_real(struct dentry *dentry);
...@@ -558,6 +559,7 @@ struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh); ...@@ -558,6 +559,7 @@ struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
struct dentry *origin, bool verify); struct dentry *origin, bool verify);
int ovl_path_next(int idx, struct dentry *dentry, struct path *path); int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
int ovl_maybe_lookup_lowerdata(struct dentry *dentry);
struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags); unsigned int flags);
bool ovl_lower_positive(struct dentry *dentry); bool ovl_lower_positive(struct dentry *dentry);
......
...@@ -146,7 +146,7 @@ static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe) ...@@ -146,7 +146,7 @@ static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe)
{ {
struct ovl_path *lowerdata = ovl_lowerdata(oe); struct ovl_path *lowerdata = ovl_lowerdata(oe);
return lowerdata ? lowerdata->dentry : NULL; return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL;
} }
/* private information held for every overlayfs dentry */ /* private information held for every overlayfs dentry */
......
...@@ -82,13 +82,14 @@ static struct dentry *ovl_d_real(struct dentry *dentry, ...@@ -82,13 +82,14 @@ static struct dentry *ovl_d_real(struct dentry *dentry,
return real; return real;
/* /*
* XXX: We may need lazy lookup of lowerdata for !inode case to return * Best effort lazy lookup of lowerdata for !inode case to return
* the real lowerdata dentry. The only current caller of d_real() with * the real lowerdata dentry. The only current caller of d_real() with
* NULL inode is d_real_inode() from trace_uprobe and this caller is * NULL inode is d_real_inode() from trace_uprobe and this caller is
* likely going to be followed reading from the file, before placing * likely going to be followed reading from the file, before placing
* uprobes on offset within the file, so lowerdata should be available * uprobes on offset within the file, so lowerdata should be available
* when setting the uprobe. * when setting the uprobe.
*/ */
ovl_maybe_lookup_lowerdata(dentry);
lower = ovl_dentry_lowerdata(dentry); lower = ovl_dentry_lowerdata(dentry);
if (!lower) if (!lower)
goto bug; goto bug;
......
...@@ -229,8 +229,14 @@ void ovl_path_lowerdata(struct dentry *dentry, struct path *path) ...@@ -229,8 +229,14 @@ void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe); struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
if (lowerdata_dentry) { if (lowerdata_dentry) {
path->mnt = lowerdata->layer->mnt;
path->dentry = lowerdata_dentry; path->dentry = lowerdata_dentry;
/*
* Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
* Make sure that if lowerdata->dentry is visible, then
* datapath->layer is visible as well.
*/
smp_rmb();
path->mnt = READ_ONCE(lowerdata->layer)->mnt;
} else { } else {
*path = (struct path) { }; *path = (struct path) { };
} }
...@@ -292,6 +298,29 @@ struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) ...@@ -292,6 +298,29 @@ struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
return ovl_lowerdata_dentry(OVL_E(dentry)); return ovl_lowerdata_dentry(OVL_E(dentry));
} }
int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
{
struct ovl_entry *oe = OVL_E(dentry);
struct ovl_path *lowerdata = ovl_lowerdata(oe);
struct dentry *datadentry = datapath->dentry;
if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
return -EIO;
WRITE_ONCE(lowerdata->layer, datapath->layer);
/*
* Pairs with smp_rmb() in ovl_path_lowerdata().
* Make sure that if lowerdata->dentry is visible, then
* lowerdata->layer is visible as well.
*/
smp_wmb();
WRITE_ONCE(lowerdata->dentry, dget(datadentry));
ovl_dentry_update_reval(dentry, datadentry);
return 0;
}
struct dentry *ovl_dentry_real(struct dentry *dentry) struct dentry *ovl_dentry_real(struct dentry *dentry)
{ {
return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment