afs: avoid unhash/rehash
Subsystems (3)
v2 cover afs: avoid unhash/rehash
cifs (like nfs and afs) 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.
Note that I think there are still possible races which I haven't tried to fix. Because cifs_open() constructs a full path name to send to the server, it could construct and use that name *after* the rename has happened on the server, but *before* d_move() has recorded the new name in the dcache. Then it would try to open a name that no longer exists. I think that could be fixed by locking the file inode in cifs_open() before calling build_path_from_dentry(), and holding the lock until the last usage of full_path. This would ensure no unlink or rename were happening at the same time.
The first patch frees up ->d_fsdata which is currently otherwise used. It is being used for a purpose that ->d_time is more suitable for, so the first patch switches to use d_time.
10 years ago cifs *did* use ->d_time but was changed to use ->d_fsdata by Miklos with no justification (a00be0e31f8d). Miklos: do you have any memory of why that was done?
Thanks,
NeilBrown
[PATCH v2 1/2] smb/client: use d_time to store a timestamp in dentry, [PATCH v2 2/2] smb/client: don't unhashed and rehash to prevent new
PATCH v2 1/2 smb/client: use d_time to store a timestamp in dentry, not d_fsdata
From: NeilBrown <[email protected]>
smb/client uses d_fsdata is exactly the way that d_time is intended to be used. It previous used d_time but this was changed in
Commit: a00be0e31f8d ("cifs: don't use ->d_time")
without any reason being given.
This patch effectively reverts that patch (though it doesn't remove the helpers) so that d_fsdata can be used for something more generic.
As ->d_time is NOT initialised by d_alloc() (unlike ->d_fsdata), it is important to initialise it in atomic_open (all dentries go through either ->lookup or ->atomic_open before being hashed, and both of these now initialise ->d_time).
--- fs/smb/client/cifsfs.h | 4 ++-- fs/smb/client/dir.c | 1 + 2 files changed, 3 insertions(+), 2 deletions(-)
fs/smb/client/cifsfs.h +2 -2
@@ -34,12 +34,12 @@ cifs_uniqueid_to_ino_t(u64 fileid)static inline void cifs_set_time (struct dentry *dentry,unsigned long time) { - dentry->d_fsdata = (void *) time; + dentry->d_time = time; }static inline unsigned long cifs_get_time (struct dentry *dentry) { -return (unsigned long ) dentry->d_fsdata; +return dentry->d_time; }extern struct file_system_type cifs_fs_type, smb3_fs_type;
fs/smb/client/dir.c +1 -0
@@ -598,6 +598,7 @@ int cifs_atomic_open(struct inode *dir, struct dentry *direntry, }else {d_instantiate (direntry, inode); } +cifs_set_time (direntry, jiffies);if ((oflags & (O_CREAT |O_EXCL )) == (O_CREAT |O_EXCL )) file->f_mode |=FMODE_CREATED ;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH v2 2/2 smb/client: don't unhashed and rehash to prevent new opens.
In reply to NeilBrownFrom: NeilBrown <[email protected]>
smb/client needs to block new opens of the target of unlink and rename while the operation is progressing. This stablises d_count() and allows a determination of whether a "silly-rename" is required.
It currently unhashes the dentry which will cause lookup to block on the parent directory i_rwsem. Proposed changes to locking will cause this approach to stop working and the exclusivity will be provided for the dentry only, and only while it is hashed.
So we introduce a new machanism similar to that used by nfs (and soon afs). ->d_fsdata (currently unused by smb/client) is set to a non-NULL value when lookups need to be blocked. ->d_revalidate checks for this and blocks. This might still allow d_count() to increment, but once it has been tested as 1, there can be no new opens completed.
Unlike unhash which does not need to be reverted on error, and which must not be reverted on a successful d_move, blocking of opens must always be reverted. So we don't block the open until after the last early "return", and we always unblock on the final "return".
--- fs/smb/client/dir.c | 3 ++ fs/smb/client/inode.c | 74 ++++++++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 29 deletions(-)
fs/smb/client/dir.c +3 -0
@@ -877,6 +877,9 @@ cifs_d_revalidate(struct inode *dir, const struct qstr *name,if (flags &LOOKUP_RCU )return -ECHILD ; +/* Wait for pending rename/unlink */ +wait_var_event (&direntry->d_fsdata, direntry->d_fsdata ==NULL ); +if (d_really_is_positive (direntry)) {int rc;struct inode *inode =d_inode (direntry);
fs/smb/client/inode.c +45 -29
134 lines not on this page · raw message
Signature · 1 line
2.50.0.107.gf914562f5916.dirty