Skip to contentSkip to conversation

afs: avoid unhash/rehash

PATCH In review NeilBrown 2 participants 8 messages Jul 2

Subsystems (2)
lore
Share & tools
lore

NeilBrown

author
#0 lore raw

cover afs: avoid unhash/rehash

afs (like nfs and others) needs to block "open" requests while processing an unlink or rename because if the target is to be unlinked on the server, then the open must fail or create a new file, which needs to wait for the unlink to complete.

It does this by unhashing the dentry and later rehashing it. This will cause problems for proposed changes to locking which will lock the dentry rather than the parent directory. Unhashing will effectively unlock the name.

The second patch here addresses this by using ->d_fsdata to mark a dentry as being busy in unlink/rename and waiting in d_revalidate for that mark to be removed. This is the same approach that NFS has used for a while.

The first patch frees up ->d_fsdata which is currently otherwised use. It is being used for a purpose that ->d_time is more suitable for so the first patch switches to use d_time.

Thanks,
NeilBrown

[PATCH 1/2] afs: use d_time instead of d_fsdata
[PATCH 2/2] afs: don't unhash/rehash dentries during unlink/rename

author
#1 lore raw

PATCH 1/2 afs: use d_time instead of d_fsdata

From: NeilBrown <[email protected]>

afs uses ->d_fsdata to store version information for the parent directory. ->d_time is arguably a better field to store this information as the version is like a time stamp, and ->d_time is an unsigned long, while ->d_fsdata is a void *.

This will leave ->d_fsdata free for a different use ... which admittedly is also not a void*, but is certainly not at all a time.

Interestingly the value stored in ->d_time or d_fsdata is u64 which does not fit in "unsigned long" or "void *" on 32 bit hosts. Maybe that doesn't matter.

---
 fs/afs/dir.c      | 18 +++++++++---------
 fs/afs/internal.h |  8 ++++----
 2 files changed, 13 insertions(+), 13 deletions(-)
fs/afs/dir.c +9 -9
index 498b99ccdf0e..7ff84b410097 100644
@@ -795,7 +795,7 @@ static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry)
 		afs_dir_iterate(dir, &cookie->ctx, NULL, &data_version);
 	}
 
-	dentry->d_fsdata = (void *)(unsigned long)data_version;
+	dentry->d_time = (unsigned long)data_version;
 
 	/* Check to see if we already have an inode for the primary fid. */
 	inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
@@ -882,9 +882,9 @@ static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry)
 	}
 
 	if (op->file[0].scb.have_status)
-		dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
+		dentry->d_time = (unsigned long)op->file[0].scb.status.data_version;
 	else
-		dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
+		dentry->d_time = (unsigned long)op->file[0].dv_before;
 	ret = afs_put_operation(op);
 out:
 	kfree(cookie);
@@ -997,7 +997,7 @@ static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
 	_debug("splice %p", dentry->d_inode);
 	d = d_splice_alias(inode, dentry);
 	if (!IS_ERR_OR_NULL(d)) {
-		d->d_fsdata = dentry->d_fsdata;
+		d->d_time = dentry->d_time;
 		trace_afs_lookup(dvnode, &d->d_name, &fid);
 	} else {
 		trace_afs_lookup(dvnode, &dentry->d_name, &fid);
@@ -1027,7 +1027,7 @@ static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry)
 	 * version.
 	 */
 	dir_version = (long)READ_ONCE(dvnode->status.data_version);
-	de_version = (long)READ_ONCE(dentry->d_fsdata);
+	de_version = (long)READ_ONCE(dentry->d_time);
 	if (de_version != dir_version) {
 		dir_version = (long)READ_ONCE(dvnode->invalid_before);
 		if (de_version - dir_version < 0)
@@ -1087,7 +1087,7 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,
 	 * version.
 	 */
 	dir_version = dir->status.data_version;
-	de_version = (long)dentry->d_fsdata;
+	de_version = (long)dentry->d_time;
 	if (de_version == (long)dir_version)
 		goto out_valid_noupdate;
 
@@ -1148,7 +1148,7 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,
 	}
 
 out_valid:
-	dentry->d_fsdata = (void *)(unsigned long)dir_version;
+	dentry->d_time = (unsigned long)dir_version;
 out_valid_noupdate:
 	key_put(key);
 	_leave(" = 1 [valid]");
@@ -1935,7 +1935,7 @@ static void afs_rename_edit_dir(struct afs_operation *op)
 		spin_unlock(&new_inode->i_lock);
 	}
 
-	/* Now we can update d_fsdata on the dentries to reflect their
+	/* Now we can update d_time on the dentries to reflect their
 	 * new parent's data_version.
 	 */
 	afs_update_dentry_version(op, new_dvp, op->dentry);
@@ -2171,7 +2171,7 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	/* This bit is potentially nasty as there's a potential race with
-	 * afs_d_revalidate{,_rcu}().  We have to change d_fsdata on the dentry
+	 * afs_d_revalidate{,_rcu}().  We have to change d_time_ on the dentry
 	 * to reflect it's new parent's new data_version after the op, but
 	 * d_revalidate may see old_dentry between the op having taken place
 	 * and the version being updated.
fs/afs/internal.h +4 -4
index 0b72a8566299..a22027935482 100644
@@ -1764,17 +1764,17 @@ static inline struct inode *AFS_VNODE_TO_I(struct afs_vnode *vnode)
 }
 
 /*
- * Note that a dentry got changed.  We need to set d_fsdata to the data version
+ * Note that a dentry got changed.  We need to set d_time to the data version
  * number derived from the result of the operation.  It doesn't matter if
- * d_fsdata goes backwards as we'll just revalidate.
+ * d_time goes backwards as we'll just revalidate.
  */
 static inline void afs_update_dentry_version(struct afs_operation *op,
 					     struct afs_vnode_param *dir_vp,
 					     struct dentry *dentry)
 {
 	if (!op->cumul_error.error)
-		dentry->d_fsdata =
-			(void *)(unsigned long)dir_vp->scb.status.data_version;
+		dentry->d_time =
+			(unsigned long)dir_vp->scb.status.data_version;
 }
 
 /*
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
maintainer
#2 lore raw

Re: [PATCH 1/2] afs: use d_time instead of d_fsdata

Quoted 4 lines · NeilBrown

afs uses ->d_fsdata to store version information for the parent directory. ->d_time is arguably a better field to store this information as the version is like a time stamp, and ->d_time is an unsigned long, while ->d_fsdata is a void *.

It wasn't clear that ->d_time was available for private use by the filesystem.

Quoted 2 lines

This will leave ->d_fsdata free for a different use ... which admittedly is also not a void*, but is certainly not at all a time.

Are you going to use ->d_fsdata for something generic?

Quoted 3 lines

Interestingly the value stored in ->d_time or d_fsdata is u64 which does not fit in "unsigned long" or "void *" on 32 bit hosts. Maybe that doesn't matter.

I know. I just have to hope that someone triggers revalidation on a dirent before 4 billion changes have been made to the directory on a 32-bit machine. I could use both d_time and d_fsdata on 32-bit, I suppose. I really don't want to allocate an 8-byte blob for each dentry.

Maybe I should switch to a single-bit flag and "invalidate" all the dentries attached to a directory if I detect a jump in the directory's version number due to a third-party modification (the version is monotonically incremented for each modification committed).

I'm not sure how to do a mass invalidation without risking deadlock, though, because I'd quite likely be starting from a random dirent in a directory and then have to walk through all the other dirents of the directory. I suppose I could lock the directory (probably using dvnode->validate_lock) and then walk through d_children.

David

author
#3 lore raw

Re: [PATCH 1/2] afs: use d_time instead of d_fsdata

Quoted 8 lines · David Howells
Quoted 4 lines · NeilBrown

afs uses ->d_fsdata to store version information for the parent directory. ->d_time is arguably a better field to store this information as the version is like a time stamp, and ->d_time is an unsigned long, while ->d_fsdata is a void *.

It wasn't clear that ->d_time was available for private use by the filesystem.

Fair enough - it isn't clearly documented.
5 different filesystems use it, no common VFS code touches it.

Quoted 5 lines
Quoted 3 lines

This will leave ->d_fsdata free for a different use ... which admittedly is also not a void*, but is certainly not at all a time.

Are you going to use ->d_fsdata for something generic?

I only need 1 bit - a flag to say that the dentry is being removed on the server. I've occasionally thought of asking for a DCACHE_PRIVATE flag but ->d_fsdata is just as good.

Quoted 9 lines
Quoted 4 lines

Interestingly the value stored in ->d_time or d_fsdata is u64 which does not fit in "unsigned long" or "void *" on 32 bit hosts. Maybe that doesn't matter.

I know. I just have to hope that someone triggers revalidation on a dirent before 4 billion changes have been made to the directory on a 32-bit machine. I could use both d_time and d_fsdata on 32-bit, I suppose. I really don't want to allocate an 8-byte blob for each dentry.

I only want 1 bit of d_fsdata, and 60 bits is likely plenty for a version number. So that would be the easiest approach.

Quoted 11 lines

Maybe I should switch to a single-bit flag and "invalidate" all the dentries attached to a directory if I detect a jump in the directory's version number due to a third-party modification (the version is monotonically incremented for each modification committed).

I'm not sure how to do a mass invalidation without risking deadlock, though, because I'd quite likely be starting from a random dirent in a directory and then have to walk through all the other dirents of the directory. I suppose I could lock the directory (probably using dvnode->validate_lock) and then walk through d_children.

Walking d_children could work, but that list can be long so you want to allow scheduling points.
I haven't actually tried this, but I think you can allocate a DCACHE_CURSOR dentry on the stack and insert it in the list whenever you want to drop ->d_lock and schedule.

Maybe not worth the effort though - 32-bit is a vanishing breed.

NeilBrown

maintainer
#4 lore raw

Re: [PATCH 1/2] afs: use d_time instead of d_fsdata

In reply to NeilBrown
Quoted 4 lines · NeilBrown

afs uses ->d_fsdata to store version information for the parent directory. ->d_time is arguably a better field to store this information as the version is like a time stamp, and ->d_time is an unsigned long, while ->d_fsdata is a void *.

Might it be better to rename ->d_time to ->d_version, since that would seem to be more in line with how it's used?

Quoted 3 lines

Interestingly the value stored in ->d_time or d_fsdata is u64 which does not fit in "unsigned long" or "void *" on 32 bit hosts. Maybe that doesn't matter.

Hmmm... It looks like NFS may have a bug here:

static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)

{
	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
	    d_really_is_negative(dentry))
		return dentry->d_time == inode_peek_iversion_raw(dir);
	return nfs_verify_change_attribute(dir, dentry->d_time);
}

dentry->d_time may be 32-bits, but inode_peek_iversion_raw() is always 64 bits and i_version is set to the 64-bit fattr->change_attr.

David

author
#5 lore raw

Re: [PATCH 1/2] afs: use d_time instead of d_fsdata

Quoted 9 lines · David Howells
Quoted 4 lines · NeilBrown

afs uses ->d_fsdata to store version information for the parent directory. ->d_time is arguably a better field to store this information as the version is like a time stamp, and ->d_time is an unsigned long, while ->d_fsdata is a void *.

Might it be better to rename ->d_time to ->d_version, since that would seem to be more in line with how it's used?

Maybe.
fuse and kernfs store an incrementing version number, like afs cifs and vboxsf store jiffies
nfs stores whatever the server provides, which might be a counter or
might be a timestamp.

So d_version is arguably slightly better than d_time.

Quoted 15 lines
Quoted 4 lines

Interestingly the value stored in ->d_time or d_fsdata is u64 which does not fit in "unsigned long" or "void *" on 32 bit hosts. Maybe that doesn't matter.

Hmmm... It looks like NFS may have a bug here:

static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)

{
	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
	    d_really_is_negative(dentry))
		return dentry->d_time == inode_peek_iversion_raw(dir);
	return nfs_verify_change_attribute(dir, dentry->d_time);
}
Quoted 2 lines

dentry->d_time may be 32-bits, but inode_peek_iversion_raw() is always 64 bits and i_version is set to the 64-bit fattr->change_attr.

I wonder if there would be any appetite for making d_time (or d_version) always 64bit, much like i_ino was recently changed to u64.

NeilBrown

maintainer
#6 lore raw

Re: [PATCH 1/2] afs: use d_time instead of d_fsdata

Quoted 14 lines · NeilBrown
Quoted 14 lines
Quoted 3 lines

Interestingly the value stored in ->d_time or d_fsdata is u64 which does not fit in "unsigned long" or "void *" on 32 bit hosts. Maybe that doesn't matter.

Hmmm... It looks like NFS may have a bug here:

static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)

{
	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
	    d_really_is_negative(dentry))
		return dentry->d_time == inode_peek_iversion_raw(dir);
	return nfs_verify_change_attribute(dir, dentry->d_time);
}
Quoted 5 lines
Quoted 2 lines

dentry->d_time may be 32-bits, but inode_peek_iversion_raw() is always 64 bits and i_version is set to the 64-bit fattr->change_attr.

I wonder if there would be any appetite for making d_time (or d_version) always 64bit, much like i_ino was recently changed to u64.

I suspect Al wouldn't be happy about that. He counts the bits in struct dentry very carefully.

David

author
#7 lore raw

PATCH 2/2 afs: don't unhash/rehash dentries during unlink/rename

In reply to NeilBrown

From: NeilBrown <[email protected]>

afs needs to block lookup of dentries during unlink and rename. There are two reasons:
1/ If the target is to be removed, not silly-renamed, the subsequent
opens cannot be allowed as the file won't exist on the server.
2/ If the rename source is being moved between directories a lookup,
particularly d_revalidate, might change ->d_time asynchronously with rename changing ->d_time with possible incorrect results.

afs current unhashes the dentry to force a lookup which will wait on the directory lock, and rehashes afterwards. This is incompatible with proposed changed to directory locking which will require a dentry to remain hashed throughout rename/unlink/etc operations.

This patch copies a mechanism developed for NFS. ->d_fsdata which is currently unused is now set to a non-NULL value when lookups must be blocked. d_revalidate checks for this value, and waits for it to become NULL.

->d_lock is used to ensure d_revalidate never updates ->d_time while ->d_fsdata is set.

---
 fs/afs/afs.h      |  7 ++++++
 fs/afs/dir.c      | 64 +++++++++++++++++++++++++++++------------------
 fs/afs/internal.h |  5 +---
 3 files changed, 47 insertions(+), 29 deletions(-)
fs/afs/afs.h +7 -0
index ec3db00bd081..019e77b08458 100644
@@ -26,6 +26,13 @@ typedef u64			afs_volid_t;
 typedef u64			afs_vnodeid_t;
 typedef u64			afs_dataversion_t;
 
+/* This is stored in ->d_fsdata to stop d_revalidate looking at,
+ * and possibly changing, ->d_time on a dentry which is being moved
+ * between directories, and to block lookup for dentry that is
+ * being removed without silly-rename.
+ */
+#define AFS_FSDATA_BLOCKED ((void*)1)
+
 typedef enum {
 	AFSVL_RWVOL,			/* read/write volume */
 	AFSVL_ROVOL,			/* read-only volume */
fs/afs/dir.c +39 -25

142 lines not on this page · raw message

fs/afs/internal.h +1 -4
index a22027935482..a0afe552ae64 100644
@@ -900,10 +900,7 @@ struct afs_operation {
 			struct afs_symlink *symlink;
 		} create;
 		struct {
-			bool	need_rehash;
-		} unlink;
-		struct {
-			struct dentry	*rehash;
+			struct dentry	*unblock;
 			struct dentry	*tmp;
 			unsigned int	rename_flags;
 			bool		new_negative;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty