Skip to contentSkip to conversation

ovl: constant inode numbers (cont.)

PATCH In review Amir Goldstein 3 participants 38 messages Jun 1, 2017 – Aug 16, 2017

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

v3 cover ovl: constant inode numbers (cont.)

Miklos,

This series covers many of the constant inode number leftovers from v4.12.

Current status is:
- For non-hardlinks, st_ino is constant and consistent with d_ino
- For samefs and for non-dir, st_ino/d_ino is also persistent
- For samefs, st_dev;st_ino is also system-wide unique

The remaining leftovers:
- Constant st_ino/d_ino for hardlinks (WIP by me)
- System-wide unique st_dev;st_ino for non-samefs (WIP by Chandan?)

Tested constant and consistent d_ino with improved xfstest overlay/017, already upstream, which currently fails only on the hardlink copy up test.

v3:
- Relax same fs contrains from v4.12
- Non "impure" dir optimizations

v2:
- Lookup overlay entry with lookup_one_len_noperm()

v1:
- Resurect Miklos's constant d_ino POC
- Lookup overlay entry with lookup_one_len()

Amir Goldstein (5):
ovl: relax same fs constrain for ovl_check_origin()
ovl: relax same fs constrain for constant st_ino
vfs: factor out lookup_one_len_init()
vfs: add helper lookup_one_len_noperm()
ovl: consistent st_ino/d_ino

 fs/namei.c               | 117 +++++++++++++++++++++++------------------
 fs/overlayfs/inode.c     |  32 +++++++-----
 fs/overlayfs/namei.c     |  50 +++++++++++-------
 fs/overlayfs/overlayfs.h |   2 +-
 fs/overlayfs/readdir.c   | 133 ++++++++++++++++++++++++++++++++++++++++++++---
 include/linux/namei.h    |   1 +
 6 files changed, 244 insertions(+), 91 deletions(-)
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v3 1/5 ovl: relax same fs constrain for ovl_check_origin()

For the case of all layers not on the same fs, try to decode the copy up origin file handle on any of the lower layers.

---
 fs/overlayfs/namei.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)
fs/overlayfs/namei.c +25 -18
index f3136c31e72a..04b3d6cf4148 100644
@@ -272,28 +272,24 @@ static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
 static int ovl_check_origin(struct dentry *dentry, struct dentry *upperdentry,
 			    struct path **stackp, unsigned int *ctrp)
 {
-	struct super_block *same_sb = ovl_same_sb(dentry->d_sb);
 	struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
 	struct vfsmount *mnt;
-	struct dentry *origin;
+	struct dentry *origin = NULL;
+	int i;
 
-	if (!same_sb || !roe->numlower)
-		return 0;
 
-       /*
-	* Since all layers are on the same fs, we use the first layer for
-	* decoding the file handle.  We may get a disconnected dentry,
-	* which is fine, because we only need to hold the origin inode in
-	* cache and use its inode number.  We may even get a connected dentry,
-	* that is not under the first layer's root.  That is also fine for
-	* using it's inode number - it's the same as if we held a reference
-	* to a dentry in first layer that was moved under us.
-	*/
-	mnt = roe->lowerstack[0].mnt;
-
-	origin = ovl_get_origin(upperdentry, mnt);
-	if (IS_ERR_OR_NULL(origin))
-		return PTR_ERR(origin);
+	for (i = 0; i < roe->numlower; i++) {
+		mnt = roe->lowerstack[i].mnt;
+		origin = ovl_get_origin(upperdentry, mnt);
+		if (IS_ERR(origin))
+			return PTR_ERR(origin);
+
+		if (origin)
+			break;
+	}
+
+	if (!origin)
+		return 0;
 
 	BUG_ON(*stackp || *ctrp);
 	*stackp = kmalloc(sizeof(struct path), GFP_TEMPORARY);
@@ -301,6 +297,7 @@ static int ovl_check_origin(struct dentry *dentry, struct dentry *upperdentry,
 		dput(origin);
 		return -ENOMEM;
 	}
+
 	**stackp = (struct path) { .dentry = origin, .mnt = mnt };
 	*ctrp = 1;
 
@@ -372,6 +369,16 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
 		}
 		if (upperdentry && !d.is_dir) {
 			BUG_ON(!d.stop || d.redirect);
+			/*
+			 * Lookup copy up origin by decoding origin file handle.
+			 * We may get a disconnected dentry, which is fine,
+			 * because we only need to hold the origin inode in
+			 * cache and use its inode number.  We may even get a
+			 * connected dentry, that is not under any of the lower
+			 * layers root.  That is also fine for using it's inode
+			 * number - it's the same as if we held a reference
+			 * to a dentry in lower layer that was moved under us.
+			 */
 			err = ovl_check_origin(dentry, upperdentry,
 					       &stack, &ctr);
 			if (err)
Signature · 1 line
2.7.4
author maintainer
#2 lore raw

PATCH v3 2/5 ovl: relax same fs constrain for constant st_ino

In reply to Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

This guarantied constant and persistent st_ino/st_dev for non-dir, but not system-wide uniqueness, like in the same fs case.

---
 fs/overlayfs/inode.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
fs/overlayfs/inode.c +19 -13
index d613e2c41242..5f285c179bbd 100644
@@ -65,6 +65,7 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 	struct path realpath;
 	const struct cred *old_cred;
 	bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
+	bool samefs = ovl_same_sb(dentry->d_sb);
 	int err;
 
 	type = ovl_path_real(dentry, &realpath);
@@ -74,16 +75,16 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 		goto out;
 
 	/*
-	 * When all layers are on the same fs, all real inode number are
-	 * unique, so we use the overlay st_dev, which is friendly to du -x.
-	 *
-	 * We also use st_ino of the copy up origin, if we know it.
-	 * This guaranties constant st_dev/st_ino across copy up.
+	 * For non-dir or same fs, we use st_ino of the copy up origin, if we
+	 * know it. This guaranties constant st_dev/st_ino across copy up.
 	 *
 	 * If filesystem supports NFS export ops, this also guaranties
 	 * persistent st_ino across mount cycle.
+	 *
+	 * When all layers are on the same fs, all real inode number are
+	 * unique, so we use the overlay st_dev, which is friendly to du -x.
 	 */
-	if (ovl_same_sb(dentry->d_sb)) {
+	if (!is_dir || samefs) {
 		if (OVL_TYPE_ORIGIN(type)) {
 			struct kstat lowerstat;
 			u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
@@ -94,7 +95,6 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 			if (err)
 				goto out;
 
-			WARN_ON_ONCE(stat->dev != lowerstat.dev);
 			/*
 			 * Lower hardlinks are broken on copy up to different
 			 * upper files, so we cannot use the lower origin st_ino
@@ -102,17 +102,23 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 			 */
 			if (is_dir || lowerstat.nlink == 1)
 				stat->ino = lowerstat.ino;
+
+			if (samefs)
+				WARN_ON_ONCE(stat->dev != lowerstat.dev);
+			else
+				stat->dev = lowerstat.dev;
 		}
-		stat->dev = dentry->d_sb->s_dev;
-	} else if (is_dir) {
+		if (samefs)
+			stat->dev = dentry->d_sb->s_dev;
+	} else {
 		/*
-		 * If not all layers are on the same fs the pair {real st_ino;
-		 * overlay st_dev} is not unique, so use the non persistent
-		 * overlay st_ino.
-		 *
 		 * Always use the overlay st_dev for directories, so 'find
 		 * -xdev' will scan the entire overlay mount and won't cross the
 		 * overlay mount boundaries.
+		 *
+		 * If not all layers are on the same fs the pair {real st_ino;
+		 * overlay st_dev} is not unique, so use the non persistent
+		 * overlay st_ino for directories.
 		 */
 		stat->dev = dentry->d_sb->s_dev;
 		stat->ino = dentry->d_inode->i_ino;
Signature · 1 line
2.7.4
author maintainer
#11 lore raw

PATCH v3 3/5 vfs: factor out lookup_one_len_init()

In reply to Amir Goldstein

In preparation of adding a new helper lookup_one_len_noperm(), factor out the common bits of lookup_one_len{,_unlocked}().

---
 fs/namei.c | 88 +++++++++++++++++++++++++++-----------------------------------
 1 file changed, 38 insertions(+), 50 deletions(-)
fs/namei.c +38 -50
index 6571a5f5112e..263caf0efafe 100644
@@ -2446,50 +2446,61 @@ int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
 }
 EXPORT_SYMBOL(vfs_path_lookup);
 
-/**
- * lookup_one_len - filesystem helper to lookup single pathname component
- * @name:	pathname component to lookup
- * @base:	base directory to lookup from
- * @len:	maximum length @len should be interpreted to
- *
- * Note that this routine is purely a helper for filesystem usage and should
- * not be called by generic code.
- *
- * The caller must hold base->i_mutex.
- */
-struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
+static int lookup_one_len_init(struct qstr *this, struct dentry *base,
+			       const char *name, int len)
 {
-	struct qstr this;
 	unsigned int c;
-	int err;
-
-	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
 
-	this.name = name;
-	this.len = len;
-	this.hash = full_name_hash(base, name, len);
+	this->name = name;
+	this->len = len;
+	this->hash = full_name_hash(base, name, len);
 	if (!len)
-		return ERR_PTR(-EACCES);
+		return -EACCES;
 
 	if (unlikely(name[0] == '.')) {
 		if (len < 2 || (len == 2 && name[1] == '.'))
-			return ERR_PTR(-EACCES);
+			return -EACCES;
 	}
 
 	while (len--) {
 		c = *(const unsigned char *)name++;
 		if (c == '/' || c == '\0')
-			return ERR_PTR(-EACCES);
+			return -EACCES;
 	}
 	/*
 	 * See if the low-level filesystem might want
 	 * to use its own hash..
 	 */
 	if (base->d_flags & DCACHE_OP_HASH) {
-		int err = base->d_op->d_hash(base, &this);
+		int err = base->d_op->d_hash(base, this);
+
 		if (err < 0)
-			return ERR_PTR(err);
+			return err;
 	}
+	return 0;
+}
+
+/**
+ * lookup_one_len - filesystem helper to lookup single pathname component
+ * @name:	pathname component to lookup
+ * @base:	base directory to lookup from
+ * @len:	maximum length @len should be interpreted to
+ *
+ * Note that this routine is purely a helper for filesystem usage and should
+ * not be called by generic code.
+ *
+ * The caller must hold base->i_mutex.
+ */
+struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
+{
+	struct qstr this;
+	int err;
+
+	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
+
+	err = lookup_one_len_init(&this, base, name, len);
+	if (err)
+		return ERR_PTR(err);
 
 	err = inode_permission(base->d_inode, MAY_EXEC);
 	if (err)
@@ -2515,35 +2526,12 @@ struct dentry *lookup_one_len_unlocked(const char *name,
 				       struct dentry *base, int len)
 {
 	struct qstr this;
-	unsigned int c;
 	int err;
 	struct dentry *ret;
 
-	this.name = name;
-	this.len = len;
-	this.hash = full_name_hash(base, name, len);
-	if (!len)
-		return ERR_PTR(-EACCES);
-
-	if (unlikely(name[0] == '.')) {
-		if (len < 2 || (len == 2 && name[1] == '.'))
-			return ERR_PTR(-EACCES);
-	}
-
-	while (len--) {
-		c = *(const unsigned char *)name++;
-		if (c == '/' || c == '\0')
-			return ERR_PTR(-EACCES);
-	}
-	/*
-	 * See if the low-level filesystem might want
-	 * to use its own hash..
-	 */
-	if (base->d_flags & DCACHE_OP_HASH) {
-		int err = base->d_op->d_hash(base, &this);
-		if (err < 0)
-			return ERR_PTR(err);
-	}
+	err = lookup_one_len_init(&this, base, name, len);
+	if (err)
+		return ERR_PTR(err);
 
 	err = inode_permission(base->d_inode, MAY_EXEC);
 	if (err)
Signature · 1 line
2.7.4
author maintainer
#12 lore raw

PATCH v3 4/5 vfs: add helper lookup_one_len_noperm()

In reply to Amir Goldstein

This is a variant of lookup_one_len() that does not check task permissions.

---
 fs/namei.c            | 29 +++++++++++++++++++++++++++++
 include/linux/namei.h |  1 +
 2 files changed, 30 insertions(+)
fs/namei.c +29 -0
index 263caf0efafe..837da8bfb36f 100644
@@ -2511,6 +2511,35 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
 EXPORT_SYMBOL(lookup_one_len);
 
 /**
+ * lookup_one_len_noperm - filesystem helper to lookup single pathname component
+ * @name:	pathname component to lookup
+ * @base:	base directory to lookup from
+ * @len:	maximum length @len should be interpreted to
+ *
+ * Note that this routine is purely a helper for filesystem usage and should
+ * not be called by generic code.
+ *
+ * Unlike lookup_one_len, it does not check the task permissions.
+ *
+ * The caller must hold base->i_mutex.
+ */
+struct dentry *lookup_one_len_noperm(const char *name, struct dentry *base,
+				     int len)
+{
+	struct qstr this;
+	int err;
+
+	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
+
+	err = lookup_one_len_init(&this, base, name, len);
+	if (err)
+		return ERR_PTR(err);
+
+	return __lookup_hash(&this, base, 0);
+}
+EXPORT_SYMBOL(lookup_one_len);
+
+/**
  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
  * @name:	pathname component to lookup
  * @base:	base directory to lookup from
include/linux/namei.h +1 -0
index 8b4794e83196..afafd38f3680 100644
@@ -81,6 +81,7 @@ extern struct dentry *kern_path_locked(const char *, struct path *);
 extern int kern_path_mountpoint(int, const char *, struct path *, unsigned int);
 
 extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
+extern struct dentry *lookup_one_len_noperm(const char *, struct dentry *, int);
 extern struct dentry *lookup_one_len_unlocked(const char *, struct dentry *, int);
 
 extern int follow_down_one(struct path *);
Signature · 1 line
2.7.4
author maintainer
#14 lore raw

PATCH v3 5/5 ovl: consistent st_ino/d_ino

In reply to Amir Goldstein

This patch is based on an earlier POC by Miklos Szeredi.

When iterating an "impure" upper directory which may contain copy up entries, lookup the overlay dentries to see if we know their copy up origin and if we do, call vfs_getattr() on the overlay dentry to make sure that d_ino will be consistent with st_ino from stat(2).

This may cause performance overhead of lookup_slow() per upper entry for entries are not cached and it may cause memory pressure overhead populating dcache entries that would not have been populated otherwise.

The overhead is minimal if the iterated entries are already in dcache. It is also quite useful for the common case of 'ls -l' that readdir() pre populates the dcache with the listed entries, making the following stat() calls faster.

One exception to consistent d_ino is the case of a merge dir that was moved into an opaque parent prior to v4.12. This case will not have marked parent opaque dir "impure" and therefore, ovl_iterate() will optimize this to direct iteration on upper dir. This situation will sort itself out after another move into the opquae parent. Worst case is that the moved dir d_ino is inconsistent with its st_ino, which is not a regression from version < 4.12.

---
 fs/overlayfs/namei.c     |   7 ++-
 fs/overlayfs/overlayfs.h |   2 +-
 fs/overlayfs/readdir.c   | 133 ++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 132 insertions(+), 10 deletions(-)
fs/overlayfs/namei.c +5 -2
index 04b3d6cf4148..4ca6061f7bfa 100644
@@ -308,18 +308,21 @@ static int ovl_check_origin(struct dentry *dentry, struct dentry *upperdentry,
  * Returns next layer in stack starting from top.
  * Returns -1 if this is the last layer.
  */
-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 *idxp)
 {
 	struct ovl_entry *oe = dentry->d_fsdata;
 
 	BUG_ON(idx < 0);
 	if (idx == 0) {
 		ovl_path_upper(dentry, path);
-		if (path->dentry)
+		if (path->dentry) {
+			*idxp = 0;
 			return oe->numlower ? 1 : -1;
+		}
 		idx++;
 	}
 	BUG_ON(idx > oe->numlower);
+	*idxp = idx;
 	*path = oe->lowerstack[idx - 1];
 
 	return (idx < oe->numlower) ? idx + 1 : -1;
fs/overlayfs/overlayfs.h +1 -1
index 0623cebeefff..513e25e56eed 100644
@@ -233,7 +233,7 @@ static inline bool ovl_is_impuredir(struct dentry *dentry)
 
 
 /* namei.c */
-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 *idxp);
 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags);
 bool ovl_lower_positive(struct dentry *dentry);
 
fs/overlayfs/readdir.c +126 -7

216 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

In reply to Amir Goldstein
Quoted 75 lines · IST Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

This guarantied constant and persistent st_ino/st_dev for non-dir, but not system-wide uniqueness, like in the same fs case.

---
 fs/overlayfs/inode.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
fs/overlayfs/inode.c +15 -13
index d613e2c41242..5f285c179bbd 100644
@@ -65,6 +65,7 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 	struct path realpath;
 	const struct cred *old_cred;
 	bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
+	bool samefs = ovl_same_sb(dentry->d_sb);
 	int err;
 
 	type = ovl_path_real(dentry, &realpath);
@@ -74,16 +75,16 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 		goto out;
 
 	/*
-	 * When all layers are on the same fs, all real inode number are
-	 * unique, so we use the overlay st_dev, which is friendly to du -x.
-	 *
-	 * We also use st_ino of the copy up origin, if we know it.
-	 * This guaranties constant st_dev/st_ino across copy up.
+	 * For non-dir or same fs, we use st_ino of the copy up origin, if we
+	 * know it. This guaranties constant st_dev/st_ino across copy up.
 	 *
 	 * If filesystem supports NFS export ops, this also guaranties
 	 * persistent st_ino across mount cycle.
+	 *
+	 * When all layers are on the same fs, all real inode number are
+	 * unique, so we use the overlay st_dev, which is friendly to du -x.
 	 */
-	if (ovl_same_sb(dentry->d_sb)) {
+	if (!is_dir || samefs) {
 		if (OVL_TYPE_ORIGIN(type)) {
 			struct kstat lowerstat;
 			u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
@@ -94,7 +95,6 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 			if (err)
 				goto out;
 
-			WARN_ON_ONCE(stat->dev != lowerstat.dev);
 			/*
 			 * Lower hardlinks are broken on copy up to different
 			 * upper files, so we cannot use the lower origin st_ino
@@ -102,17 +102,23 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
 			 */
 			if (is_dir || lowerstat.nlink == 1)
 				stat->ino = lowerstat.ino;
+
+			if (samefs)
+				WARN_ON_ONCE(stat->dev != lowerstat.dev);
+			else
+				stat->dev = lowerstat.dev;
 		}
-		stat->dev = dentry->d_sb->s_dev;
-	} else if (is_dir) {
+		if (samefs)
+			stat->dev = dentry->d_sb->s_dev;
+	} else {
 		/*
-		 * If not all layers are on the same fs the pair {real st_ino;
-		 * overlay st_dev} is not unique, so use the non persistent
-		 * overlay st_ino.
-		 *

fs/overlayfs/inode.c int ovl_getattr(const struct path *path, struct kstat *stat, · line 124

Quoted 10 lines
fs/overlayfs/inode.c +4 -0
 		 * Always use the overlay st_dev for directories, so 'find
 		 * -xdev' will scan the entire overlay mount and won't cross the
 		 * overlay mount boundaries.
+		 *
+		 * If not all layers are on the same fs the pair {real st_ino;
+		 * overlay st_dev} is not unique, so use the non persistent
+		 * overlay st_ino for directories.
 		 */
 		stat->dev = dentry->d_sb->s_dev;
 		stat->ino = dentry->d_inode->i_ino;

Hi Amir,

The following question is not directly related to your patch.

With this patch applied, we have the following code snippet inside ovl_getattr(),

       if (!is_dir || samefs) {
                if (OVL_TYPE_ORIGIN(type)) {
                        struct kstat lowerstat;
                        u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);

                        ovl_path_lower(dentry, &realpath);
                        err = vfs_getattr(&realpath, &lowerstat,
                                          lowermask, flags);
                        if (err)
                                goto out;

When invoking vfs_getattr() on a non-dir file, the code assumes that the copy-up origin inode always exists in lowerdir. Are we assuming that after an overlayfs filesystem has been unmounted, the lower filesystem should never be manipulated (e.g. deleting the file which was the copy-up origin)? If the copy-up origin file gets deleted, On a later mount of the overlayfs filesystem, calls to stat(2) for the corresponding upperdir file will fail because of vfs_getattr() returning an error code.

Signature · 1 line
chandan
author maintainer
#4 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

Quoted 4 lines · Chandan Rajendra
Quoted 3 lines · IST Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

...

Quoted 18 lines

Hi Amir,

The following question is not directly related to your patch.

With this patch applied, we have the following code snippet inside ovl_getattr(),

       if (!is_dir || samefs) {
                if (OVL_TYPE_ORIGIN(type)) {
                        struct kstat lowerstat;
                        u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);

                        ovl_path_lower(dentry, &realpath);
                        err = vfs_getattr(&realpath, &lowerstat,
                                          lowermask, flags);
                        if (err)
                                goto out;
Quoted 2 lines

When invoking vfs_getattr() on a non-dir file, the code assumes that the copy-up origin inode always exists in lowerdir.

Code does not assume that.
Code verifies that the copy up origin stored in xattr overlay.origin is still valid
and not stale in ovl_check_origin().

Quoted 7 lines

Are we assuming that after an
overlayfs filesystem has been unmounted, the lower filesystem should never be manipulated (e.g. deleting the file which was the copy-up origin)? If the copy-up origin file gets deleted, On a later mount of the overlayfs filesystem, calls to stat(2) for the corresponding upperdir file will fail because of vfs_getattr() returning an error code.

The case you are describing can happen. It will cause ovl_check_origin() to NOT follow the stored copy up origin and in the snippet above OVL_TYPE_ORIGIN(type) will be false.

Furthermore, even if copy up origin was not deleted, but the lower layer has been copied (cp -a) or the lower filesystem does not support NFS export, or lower filesystem does not have a valid UUID (e.g. tmpfs). All those cases will end up with OVL_TYPE_ORIGIN(type) being false and then inode numbers will not remain constant across a mount cycle (or drop caches).

Amir.

maintainer
#5 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

In reply to Amir Goldstein
Quoted 5 lines · Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

This guarantied constant and persistent st_ino/st_dev for non-dir, but not system-wide uniqueness, like in the same fs case.

I'd rather leave this until the st_dev uniqueness is sorted out. Non
unique st_ino/st_dev is just asking for trouble.

Thanks,
Miklos

Quoted 82 lines
---
 fs/overlayfs/inode.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
fs/overlayfs/inode.c +19 -14
index d613e2c41242..5f285c179bbd 100644
@@ -65,6 +65,7 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
        struct path realpath;
        const struct cred *old_cred;
        bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
+       bool samefs = ovl_same_sb(dentry->d_sb);
        int err;
 
        type = ovl_path_real(dentry, &realpath);
@@ -74,16 +75,16 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
                goto out;
 
        /*
-        * When all layers are on the same fs, all real inode number are
-        * unique, so we use the overlay st_dev, which is friendly to du -x.
-        *
-        * We also use st_ino of the copy up origin, if we know it.
-        * This guaranties constant st_dev/st_ino across copy up.
+        * For non-dir or same fs, we use st_ino of the copy up origin, if we
+        * know it. This guaranties constant st_dev/st_ino across copy up.
         *
         * If filesystem supports NFS export ops, this also guaranties
         * persistent st_ino across mount cycle.
+        *
+        * When all layers are on the same fs, all real inode number are
+        * unique, so we use the overlay st_dev, which is friendly to du -x.
         */
-       if (ovl_same_sb(dentry->d_sb)) {
+       if (!is_dir || samefs) {
                if (OVL_TYPE_ORIGIN(type)) {
                        struct kstat lowerstat;
                        u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
@@ -94,7 +95,6 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
                        if (err)
                                goto out;
 
-                       WARN_ON_ONCE(stat->dev != lowerstat.dev);
                        /*
                         * Lower hardlinks are broken on copy up to different
                         * upper files, so we cannot use the lower origin st_ino
@@ -102,17 +102,23 @@ int ovl_getattr(const struct path *path, struct kstat *stat,
                         */
                        if (is_dir || lowerstat.nlink == 1)
                                stat->ino = lowerstat.ino;
+
+                       if (samefs)
+                               WARN_ON_ONCE(stat->dev != lowerstat.dev);
+                       else
+                               stat->dev = lowerstat.dev;
                }
-               stat->dev = dentry->d_sb->s_dev;
-       } else if (is_dir) {
+               if (samefs)
+                       stat->dev = dentry->d_sb->s_dev;
+       } else {
                /*
-                * If not all layers are on the same fs the pair {real st_ino;
-                * overlay st_dev} is not unique, so use the non persistent
-                * overlay st_ino.
-                *
                 * Always use the overlay st_dev for directories, so 'find
                 * -xdev' will scan the entire overlay mount and won't cross the
                 * overlay mount boundaries.
+                *
+                * If not all layers are on the same fs the pair {real st_ino;
+                * overlay st_dev} is not unique, so use the non persistent
+                * overlay st_ino for directories.
                 */
                stat->dev = dentry->d_sb->s_dev;
                stat->ino = dentry->d_inode->i_ino;
--

2.7.4

author maintainer
#6 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

Quoted 10 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

This guarantied constant and persistent st_ino/st_dev for non-dir, but not system-wide uniqueness, like in the same fs case.

I'd rather leave this until the st_dev uniqueness is sorted out. Non
unique st_ino/st_dev is just asking for trouble.

I don't mind leaving this out, but you do realize that this commit doesn't change st_dev/st_ino uniqueness one way or the other, right?

- Directory st_dev/st_ino *remains* constant, unique and non-persistent
- Non-dir st_dev/st_ino *remains* non-unique, but becomes constant and persistent

So I can't really see the harm in this patch.

Nevertheless, let me know if you want me to re-post consistent d_ino without the non-samefs relax patches. Actually, consistent d_ino patch is correct with or
without the non-samefs relax patches, but it misses an optimization for skipping vfs_getattr() for (impure && !samefs) case.

Amir.

maintainer
#7 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

Quoted 8 lines · Amir Goldstein
Quoted 7 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

This guarantied constant and persistent st_ino/st_dev for non-dir, but not system-wide uniqueness, like in the same fs case.

Quoted 6 lines
Quoted 3 lines

I'd rather leave this until the st_dev uniqueness is sorted out. Non
unique st_ino/st_dev is just asking for trouble.

I don't mind leaving this out, but you do realize that this commit doesn't change st_dev/st_ino uniqueness one way or the other, right?

Depends on how you look at it.

Before the patch it was (just the non-samefs, non-directory case):

- if lower: st_ino/st_dev comes from lower
- if upper: st_ino/st_dev comes from upper

This only violates the uniqueness rule if check+use of st_ino/st_dev races with the copy up

After the patch it's:

- if copied up: st_ino/st_dev comes from origin
- otherwise same as above

Now the uniqueness is violated for all copied up files (origin and overlay file will have the same st_ino/st_dev pair even though they possibly have different contents).

So that transforms a race into a permanent error, which is not good.

Thanks,
Miklos

author maintainer
#8 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

Quoted 28 lines · Miklos Szeredi
Quoted 8 lines · Amir Goldstein
Quoted 7 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein

For the case of all layers not on the same fs, use the copy up origin st_ino/st_dev for non-dir, if we know it.

This guarantied constant and persistent st_ino/st_dev for non-dir, but not system-wide uniqueness, like in the same fs case.

Quoted 6 lines
Quoted 3 lines

I'd rather leave this until the st_dev uniqueness is sorted out. Non
unique st_ino/st_dev is just asking for trouble.

I don't mind leaving this out, but you do realize that this commit doesn't change st_dev/st_ino uniqueness one way or the other, right?

Depends on how you look at it.

Before the patch it was (just the non-samefs, non-directory case):

- if lower: st_ino/st_dev comes from lower
- if upper: st_ino/st_dev comes from upper

This only violates the uniqueness rule if check+use of st_ino/st_dev races with the copy up

After the patch it's:

Quoted 9 lines

- if copied up: st_ino/st_dev comes from origin
- otherwise same as above

Now the uniqueness is violated for all copied up files (origin and overlay file will have the same st_ino/st_dev pair even though they possibly have different contents).

So that transforms a race into a permanent error, which is not good.

Right. of course.
So do you want me to re-post the consistent d_ino for samefs case only?

I would like to start posting the index/hardlink patches, so though I would flush out the constino buffer first.

Thanks,
Amir.

maintainer
#9 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

Quoted 2 lines · Amir Goldstein

I would like to start posting the index/hardlink patches, so though I would flush out the constino buffer first.

No need to resend, I'll just leave this out.

Thanks,
Miklos

author maintainer
#10 lore raw

Re: [PATCH v3 2/5] ovl: relax same fs constrain for constant st_ino

Quoted 7 lines · Miklos Szeredi
Quoted 2 lines · Amir Goldstein

I would like to start posting the index/hardlink patches, so though I would flush out the constino buffer first.

No need to resend, I'll just leave this out.

OK. Please do leave this patch in:
ovl: relax same fs constrain for ovl_check_origin()

index/hardlinks code does NOT assume samefs
and requires that lookup will find the origin also for non samefs case.

Thanks,
Amir.

author maintainer
#13 lore raw

Re: [PATCH v3 4/5] vfs: add helper lookup_one_len_noperm()

In reply to Amir Goldstein
Quoted 34 lines · Amir Goldstein

This is a variant of lookup_one_len() that does not check task permissions.

---
 fs/namei.c            | 29 +++++++++++++++++++++++++++++
 include/linux/namei.h |  1 +
 2 files changed, 30 insertions(+)
fs/namei.c +17 -0
index 263caf0efafe..837da8bfb36f 100644
@@ -2511,6 +2511,35 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
 EXPORT_SYMBOL(lookup_one_len);
 
 /**
+ * lookup_one_len_noperm - filesystem helper to lookup single pathname component
+ * @name:      pathname component to lookup
+ * @base:      base directory to lookup from
+ * @len:       maximum length @len should be interpreted to
+ *
+ * Note that this routine is purely a helper for filesystem usage and should
+ * not be called by generic code.
+ *
+ * Unlike lookup_one_len, it does not check the task permissions.
+ *
+ * The caller must hold base->i_mutex.
+ */
+struct dentry *lookup_one_len_noperm(const char *name, struct dentry *base,
+                                    int len)
+{
+       struct qstr this;
+       int err;

fs/namei.c struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) · line 2540

Quoted 10 lines
fs/namei.c +10 -0
+
+       WARN_ON_ONCE(!inode_is_locked(base->d_inode));
+
+       err = lookup_one_len_init(&this, base, name, len);
+       if (err)
+               return ERR_PTR(err);
+
+       return __lookup_hash(&this, base, 0);
+}
+EXPORT_SYMBOL(lookup_one_len);

Miklos,

Chandan pointed out to me that I have a copy&paste typo:

+EXPORT_SYMBOL(lookup_one_len_noperm);

Please fix when applying if I don't end up sending another version.

Thanks,
Amir.

maintainer
#15 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Amir Goldstein
Quoted 6 lines · Amir Goldstein

This patch is based on an earlier POC by Miklos Szeredi.

When iterating an "impure" upper directory which may contain copy up entries, lookup the overlay dentries to see if we know their copy up origin and if we do, call vfs_getattr() on the overlay dentry to make sure that d_ino will be consistent with st_ino from stat(2).

Getting back to this, because there are issues:

- ".." can have different origin even if dir is pure (pure upper
residing in merged dir, pure lower residing in dir which has another lower layer (which is the origin) above it)

- we check impure at open time, but dir can become impure between two
getdents*(2) calls, we can get wrong inode numbers

Becoming impure needs to be handled differently from what this patch does, because we can't switch to cached readdir in the middle of the directory listing.

To handle that we need to add our own actor to the is_real case to fix up the inode numbers. This would fix the ".." case as well.

The problem is, we are inside underlying fs code when actor is called, so there's not much we can do without risking deadlocks. One thing we can do (hopefully) is call d_lookup() to check for a cached overlay dentry, and get the inode number out.

We need to store the full inode number in the overlay inode, because
we can't do vfs_getattr() either, but that's a good optimization anyway.

If the overlay dentry is not in the cache we need to abort the iteration, lookup the dentry, and continue iterating.

Thoughts? I'll have a go at this tomorrow.

Thanks,
Miklos

author maintainer
#16 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 8 lines · Miklos Szeredi
Quoted 6 lines · Amir Goldstein

This patch is based on an earlier POC by Miklos Szeredi.

When iterating an "impure" upper directory which may contain copy up entries, lookup the overlay dentries to see if we know their copy up origin and if we do, call vfs_getattr() on the overlay dentry to make sure that d_ino will be consistent with st_ino from stat(2).

Quoted 5 lines

Getting back to this, because there are issues:

- ".." can have different origin even if dir is pure (pure upper
residing in merged dir, pure lower residing in dir which has another lower layer (which is the origin) above it)

If we need to add complexity only for the sake of consistent d_ino for "..", then IMO we should leave it inconsistent (as is it today). Directories don't have consistent d_ino today and nobody complains (right?). As I wrote before, 'find' doesn't look at d_ino for directories, it always checks st_ino for dir (i.e. with find -ino N).
So as long as won't we are not aware of any application for which that matters, let's leave directories at "best effort" w.r.t consistent d_ino. They are already best effort w.r.t persistent st_ino (!samefs).

Quoted 2 lines

- we check impure at open time,

Not only. we also check impure (ovl_dir_is_real) at ovl_dir_reset(), so at first call to getdents(2).

Quoted 15 lines

but dir can become impure between two getdents*(2) calls, we can get wrong inode numbers

Becoming impure needs to be handled differently from what this patch does, because we can't switch to cached readdir in the middle of the directory listing.

To handle that we need to add our own actor to the is_real case to fix up the inode numbers. This would fix the ".." case as well.

The problem is, we are inside underlying fs code when actor is called, so there's not much we can do without risking deadlocks. One thing we can do (hopefully) is call d_lookup() to check for a cached overlay dentry, and get the inode number out.

Quoted 9 lines

We need to store the full inode number in the overlay inode, because
we can't do vfs_getattr() either, but that's a good optimization anyway.

If the overlay dentry is not in the cache we need to abort the iteration, lookup the dentry, and continue iterating.

Thoughts? I'll have a go at this tomorrow.

Thinking out loud: If we store od->version for is_real
dir containing the dentry version when we decided that od->is_real. Then we can compare version in ovl_iterate() and if version has changed then call ovl_dir_reset() and if is_real became false then "abort the iteration, lookup the dentry, and continue iterating." without having to go through all the pain involved with acting in underlying fs code context.

I might note, though you are probably aware, that dir becoming impure due to copy up is not a problem, because then dir is either already merge before copy up or we are already iterating real lower dir.

The rename origin into pure upper dir case, is synchronized with overlay dir inode lock against any single getdents(2) call, so dir becoming impure due to rename can only happen between two getdents(2) calls as you wrote.

Amir.

maintainer
#17 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 64 lines · Amir Goldstein
Quoted 8 lines · Miklos Szeredi
Quoted 6 lines · Amir Goldstein

This patch is based on an earlier POC by Miklos Szeredi.

When iterating an "impure" upper directory which may contain copy up entries, lookup the overlay dentries to see if we know their copy up origin and if we do, call vfs_getattr() on the overlay dentry to make sure that d_ino will be consistent with st_ino from stat(2).

Quoted 5 lines

Getting back to this, because there are issues:

- ".." can have different origin even if dir is pure (pure upper
residing in merged dir, pure lower residing in dir which has another lower layer (which is the origin) above it)

If we need to add complexity only for the sake of consistent d_ino for "..", then IMO we should leave it inconsistent (as is it today). Directories don't have consistent d_ino today and nobody complains (right?). As I wrote before, 'find' doesn't look at d_ino for directories, it always checks st_ino for dir (i.e. with find -ino N).
So as long as won't we are not aware of any application for which that matters, let's leave directories at "best effort" w.r.t consistent d_ino. They are already best effort w.r.t persistent st_ino (!samefs).

Quoted 2 lines

- we check impure at open time,

Not only. we also check impure (ovl_dir_is_real) at ovl_dir_reset(), so at first call to getdents(2).

Quoted 15 lines

but dir can become impure between two getdents*(2) calls, we can get wrong inode numbers

Becoming impure needs to be handled differently from what this patch does, because we can't switch to cached readdir in the middle of the directory listing.

To handle that we need to add our own actor to the is_real case to fix up the inode numbers. This would fix the ".." case as well.

The problem is, we are inside underlying fs code when actor is called, so there's not much we can do without risking deadlocks. One thing we can do (hopefully) is call d_lookup() to check for a cached overlay dentry, and get the inode number out.

Quoted 9 lines

We need to store the full inode number in the overlay inode, because
we can't do vfs_getattr() either, but that's a good optimization anyway.

If the overlay dentry is not in the cache we need to abort the iteration, lookup the dentry, and continue iterating.

Thoughts? I'll have a go at this tomorrow.

Thinking out loud: If we store od->version for is_real
dir containing the dentry version when we decided that od->is_real. Then we can compare version in ovl_iterate() and if version has changed then call ovl_dir_reset() and if is_real became false then "abort the iteration, lookup the dentry, and continue iterating." without having to go through all the pain involved with acting in underlying fs code context.

Quoted 3 lines

I might note, though you are probably aware, that dir becoming impure due to copy up is not a problem, because then dir is either already merge before copy up or we are already iterating real lower dir.

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

BTW, the ".." case could be handled pretty simply, parent is locked, we can query the inode number before calling underlying iterate. But surely we can leave this to the end.

Thanks,
Miklos

maintainer
#18 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 5 lines · Miklos Szeredi

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

On the other hand, we'll need to accommodate the native directory indexing (i.e. seekdir(3) hell) in the cache, which is going to add to complexity. Ugh.

Thanks,
Miklos

maintainer
#19 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 11 lines · Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

On the other hand, we'll need to accommodate the native directory indexing (i.e. seekdir(3) hell) in the cache, which is going to add to complexity. Ugh.

Another idea: we are allowed to omit directory entries added after the opendir/rewinddir. So if there was a simple way to filter out newly added entries which have origin then we are fine.

Thanks,
Miklos

author maintainer
#20 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 9 lines · Miklos Szeredi
Quoted 8 lines · Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

Quoted 8 lines
Quoted 3 lines

On the other hand, we'll need to accommodate the native directory indexing (i.e. seekdir(3) hell) in the cache, which is going to add to complexity. Ugh.

Another idea: we are allowed to omit directory entries added after the opendir/rewinddir. So if there was a simple way to filter out newly added entries which have origin then we are fine.

Then how about this: implement an actor that ONLY looks in overlay dcache for the entries.
If entry is in dcache then can take inode number from overlay inode.
If entry is not in dcache AND the file was just moved into dir (quite unlinkely) then write off d_ino as a transient inconsistency.

maintainer
#21 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 19 lines · Amir Goldstein
Quoted 9 lines · Miklos Szeredi
Quoted 8 lines · Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

Quoted 8 lines
Quoted 3 lines

On the other hand, we'll need to accommodate the native directory indexing (i.e. seekdir(3) hell) in the cache, which is going to add to complexity. Ugh.

Another idea: we are allowed to omit directory entries added after the opendir/rewinddir. So if there was a simple way to filter out newly added entries which have origin then we are fine.

Quoted 6 lines

Then how about this: implement an actor that ONLY looks in overlay dcache for the entries.
If entry is in dcache then can take inode number from overlay inode.
If entry is not in dcache AND the file was just moved into dir (quite unlinkely) then write off d_ino as a transient inconsistency.

Note: in theory the "just moved into dir" can be two weeks before. In practice it's unlikely that two calls of getdents(2) will be so far apart in time.

Still, this feels a bit too hacky.

Thanks,
Miklos

author maintainer
#22 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 15 lines · Miklos Szeredi
Quoted 14 lines · Amir Goldstein
Quoted 13 lines · Miklos Szeredi
Quoted 11 lines · Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

On the other hand, we'll need to accommodate the native directory indexing (i.e. seekdir(3) hell) in the cache, which is going to add to complexity. Ugh.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Another idea: we are allowed to omit directory entries added after the opendir/rewinddir. So if there was a simple way to filter out newly added entries which have origin then we are fine.

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

maintainer
#23 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 22 lines · Amir Goldstein
Quoted 15 lines · Miklos Szeredi
Quoted 14 lines · Amir Goldstein
Quoted 13 lines · Miklos Szeredi
Quoted 11 lines · Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

Right. The problem is when dir becomes impure due to rename between two getdents(2) calls. We can't call ovl_dir_reset() because the offset is not zero. We could do a "cache with head cut off" starting from the current offset in the dir and finish with that. Yeah, it's probably less complexity than trying to intercept the actor...

On the other hand, we'll need to accommodate the native directory indexing (i.e. seekdir(3) hell) in the cache, which is going to add to complexity. Ugh.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Another idea: we are allowed to omit directory entries added after the opendir/rewinddir. So if there was a simple way to filter out newly added entries which have origin then we are fine.

Quoted 6 lines

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Thanks,
Miklos

author maintainer
#24 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 5 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

And use this cache to ommit/fix entries with actor?

Just thought of another issue related to pseudo lower st_dev we need the underlying iter actor also for replacing real st_dev with pseudo st_dev.

Also, just realized that with inodes index, the upper non-dir inodes with nlink == 1 are not origin suspects. Can that help us somehow?

author maintainer
#25 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 4 lines · Amir Goldstein

Just thought of another issue related to pseudo lower st_dev we need the underlying iter actor also for replacing real st_dev with pseudo st_dev.

Yeh, that was stupid. please ignore.

maintainer
#26 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Amir Goldstein
Quoted 11 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 7 lines
Quoted 5 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

And use this cache to ommit/fix entries with actor?

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

Thanks,
Miklos

author maintainer
#27 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 12 lines · Miklos Szeredi
Quoted 11 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Wait a minute, if we want to keep the handling for merge dir separate, then this patch is perfectly valid for handling merge dir (and impure dir if we know about it at open time).

Yes, it does not cover the two corner cases that you mentioned, but it covers the likely real world cases, as proven by xfstest overlay/017.

Given that solving the corner cases requires completely new code that is mostly independent of this patch, why not merge this patch for v4.13?

Amir.

#28 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Miklos Szeredi
Quoted 18 lines · IST Miklos Szeredi
Quoted 17 lines · Amir Goldstein
Quoted 15 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Quoted 6 lines
Quoted 1 line

And use this cache to ommit/fix entries with actor?

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

Amir, Can I please take over the implementation of this feature.

Signature · 1 line
chandan
author maintainer
#29 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 19 lines · Chandan Rajendra
Quoted 18 lines · IST Miklos Szeredi
Quoted 17 lines · Amir Goldstein
Quoted 15 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

And use this cache to ommit/fix entries with actor?

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

Amir, Can I please take over the implementation of this feature.

Yeh, sure.
I suggest that you start from understanding the overlay readdir code. It's not trivial IMO, so I expect you come back with more questions. You should understand how od->cache is populated and when it is updated. The current od->cache is implemented for merge dirs and contains a merged list of entries from all layers.

The case of an impure non-merge dir needs a different sort of cache (list of entries with origin that were moved into upper dir), whose version is only ever updated on rename (and always under the overlay dir inode lock).

So I think you should start with my patch for the classic merge dir case and then add patches to maintain and iterate the "impure dir cache".

As my patch adds quite a bit of performance overhead for merge dir readdir, I expect there will be more optimization work needed before the work can land in upstream.

Good luck,
Amir.

maintainer
#30 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Miklos Szeredi
Quoted 18 lines · Miklos Szeredi
Quoted 17 lines · Amir Goldstein
Quoted 15 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Quoted 5 lines
Quoted 1 line

And use this cache to ommit/fix entries with actor?

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Please test.

Thanks,
Miklos

author maintainer
#31 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 21 lines · Miklos Szeredi
Quoted 20 lines · Miklos Szeredi
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 5 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

And use this cache to ommit/fix entries with actor?

Quoted 8 lines
Quoted 3 lines

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Please test.

I can confirm that xfstest overlay/017, the only test I have for constant d_ino,
passes. Although it does not cover all cases handled by the patch set (e.g. d_ino of merge parent).

Chandan,

Can you take on the task of improving test coverage for constant d_ino? You should create a new xfstest and cover more cases of constant d_ino and possibly also a test for clearing of impure xattr.

When the basic test is in place, I will be able to point at more corner cases to cover.

Also need to prepare a test to verify constant d_ino for non samefs for after this patch is integrated with your work.
overlay/017 is a good starting point, but can't use the standard _scratch_mount helpers that mount samefs overlay.

Thanks,
Amir.

author maintainer
#32 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 28 lines · Amir Goldstein
Quoted 27 lines · Miklos Szeredi
Quoted 18 lines · Miklos Szeredi
Quoted 17 lines · Amir Goldstein
Quoted 15 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Quoted 5 lines
Quoted 1 line

And use this cache to ommit/fix entries with actor?

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Quoted 8 lines
Quoted 2 lines

Please test.

I can confirm that xfstest overlay/017, the only test I have for constant d_ino,
passes. Although it does not cover all cases handled by the patch set (e.g. d_ino of merge parent).

Miklos,

Gave the code a quick review pass.
Overall looking good.
One thing that stands out in "my" patch ("ovl: constant d_ino across copy up") The comments and commit message claim "When all layers are on the same fs", but the patch doesn't actually checks that.
The reason is that my original patch did "call vfs_getattr() on the overlay entries",
which meant constant d_ino IFF constant st_ino.
Your modifications turned the code to do constant d_ino, even for non samefs, which is not that bad, but I don't think you meant for it.

Amir.

#35 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Amir Goldstein
Quoted 42 lines · IST Amir Goldstein
Quoted 21 lines · Miklos Szeredi
Quoted 20 lines · Miklos Szeredi
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 5 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

And use this cache to ommit/fix entries with actor?

Quoted 8 lines
Quoted 3 lines

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Please test.

I can confirm that xfstest overlay/017, the only test I have for constant d_ino,
passes. Although it does not cover all cases handled by the patch set (e.g. d_ino of merge parent).

Chandan,

Can you take on the task of improving test coverage for constant d_ino? You should create a new xfstest and cover more cases of constant d_ino and possibly also a test for clearing of impure xattr.

Quoted 7 lines

When the basic test is in place, I will be able to point at more corner cases to cover.

Also need to prepare a test to verify constant d_ino for non samefs for after this patch is integrated with your work.
overlay/017 is a good starting point, but can't use the standard _scratch_mount helpers that mount samefs overlay.

I will work on that.

Signature · 1 line
chandan
maintainer
#33 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Amir Goldstein
Quoted 38 lines · Amir Goldstein
Quoted 28 lines · Amir Goldstein
Quoted 27 lines · Miklos Szeredi
Quoted 18 lines · Miklos Szeredi
Quoted 17 lines · Amir Goldstein
Quoted 15 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

Quoted 5 lines
Quoted 1 line

And use this cache to ommit/fix entries with actor?

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Quoted 8 lines
Quoted 2 lines

Please test.

I can confirm that xfstest overlay/017, the only test I have for constant d_ino,
passes. Although it does not cover all cases handled by the patch set (e.g. d_ino of merge parent).

Quoted 4 lines

Miklos,

Gave the code a quick review pass.
Overall looking good.

Thanks for the review.

Quoted 3 lines

One thing that stands out in "my" patch ("ovl: constant d_ino across copy up") The comments and commit message claim "When all layers are on the same fs", but the patch doesn't actually checks that.

I does in ovl_cache_update_ino(). That check would be more logical
in ovl_calc_d_ino(), though. Fixed.

Thanks,
Miklos

#36 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Amir Goldstein
Quoted 42 lines · IST Amir Goldstein
Quoted 21 lines · Miklos Szeredi
Quoted 20 lines · Miklos Szeredi
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 5 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

And use this cache to ommit/fix entries with actor?

Quoted 8 lines
Quoted 3 lines

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Please test.

I can confirm that xfstest overlay/017, the only test I have for constant d_ino,
passes. Although it does not cover all cases handled by the patch set (e.g. d_ino of merge parent).

Chandan,

Can you take on the task of improving test coverage for constant d_ino? You should create a new xfstest and cover more cases of constant d_ino and possibly also a test for clearing of impure xattr.

Quoted 7 lines

When the basic test is in place, I will be able to point at more corner cases to cover.

Also need to prepare a test to verify constant d_ino for non samefs for after this patch is integrated with your work.
overlay/017 is a good starting point, but can't use the standard _scratch_mount helpers that mount samefs overlay.

Hi Amir,

In ovl_calc_d_ino() we have the following code snippet,

if ((p->name[0] == '.' && p->len == 1) ||

    ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
        return true;

At this point in the function, the directory which is being iterated must have an entry in upperdir. W.r.t the case of processing "." directory entry, I think we can limit it to be applicable only when rdd->dentry is either a merge dir or an impure dir. For a directory which is both pure and non-merge, we can return the value of p->ino as is.

The second part of the "if" condition takes care of the 'impure dir' part. So the first part of the "if" condition can be written as,

(p->name[0] == '.' && p->len == 1 && !OVL_TYPE_MERGE(ovl_path_type(rdd->dentry)))

Signature · 1 line
chandan
#37 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

Quoted 65 lines · IST Chandan Rajendra
Quoted 42 lines · IST Amir Goldstein
Quoted 21 lines · Miklos Szeredi
Quoted 20 lines · Miklos Szeredi
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 7 lines · Amir Goldstein

Following up on your idea:
- check in ovl_iterate() if version has changed and if dir became impure
- if it did, populate od->cache, but keep the dir od->is_real
- iterate upper cache entries and call ovl_cache_update_ino()
- Then actor of real dir iterator can use the cache to ommit entries or use p->ino from cache if p->real_ino match real d_ino, but differs from p->ino.

Quoted 5 lines

For non-merge dirs we can have a simplified cache just containing the entries with origin, recreated when the version changes or updated in rename, whichever is simpler. A non-merge dir will never become a merge one, so we can keep the handling separate.

And use this cache to ommit/fix entries with actor?

Quoted 8 lines
Quoted 3 lines

Right. I think fixing up is better, because to correctly omit entries we'd need separate lists for each open directory. For fixing up we can use a common one, just like for the merged dir.

And back to this, I pushed a branch named "ovl-d_ino" to my vfs tree.

Please test.

I can confirm that xfstest overlay/017, the only test I have for constant d_ino,
passes. Although it does not cover all cases handled by the patch set (e.g. d_ino of merge parent).

Chandan,

Can you take on the task of improving test coverage for constant d_ino? You should create a new xfstest and cover more cases of constant d_ino and possibly also a test for clearing of impure xattr.

Quoted 7 lines

When the basic test is in place, I will be able to point at more corner cases to cover.

Also need to prepare a test to verify constant d_ino for non samefs for after this patch is integrated with your work.
overlay/017 is a good starting point, but can't use the standard _scratch_mount helpers that mount samefs overlay.

Hi Amir,

In ovl_calc_d_ino() we have the following code snippet,

if ((p->name[0] == '.' && p->len == 1) ||

    ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
        return true;

At this point in the function, the directory which is being iterated must have an entry in upperdir. W.r.t the case of processing "." directory entry, I think we can limit it to be applicable only when rdd->dentry is either a merge dir or an impure dir. For a directory which is both pure and non-merge, we can return the value of p->ino as is.

Quoted 6 lines

The second part of the "if" condition takes care of the 'impure dir' part. So the first part of the "if" condition can be written as,

(p->name[0] == '.' && p->len == 1 && !OVL_TYPE_MERGE(ovl_path_type(rdd->dentry)))

Sorry about the noise. I found out that in ovl_iterate(), the code which is responsible for merging entries from a "merge dir" can also end up invoking ovl_calc_d_ino().

Signature · 1 line
chandan
author maintainer
#34 lore raw

Re: [PATCH v3 5/5] ovl: consistent st_ino/d_ino

In reply to Miklos Szeredi
Quoted 10 lines · Miklos Szeredi
Quoted 3 lines · Amir Goldstein

One thing that stands out in "my" patch ("ovl: constant d_ino across copy up") The comments and commit message claim "When all layers are on the same fs", but the patch doesn't actually checks that.

I does in ovl_cache_update_ino(). That check would be more logical
in ovl_calc_d_ino(), though. Fixed.

I see the patches in overlayfs-next still don't have the ovl_same_sb() test in ovl_calc_d_ino(). I don't really mind.
BTW, do you intend to apply/rework Chandan's non-samefs patches for next? if so, the current location of ovl_same_sb() really doesn't matter..

Amir.