[PATCH 32/35] cifs: Use cifs_rename_pending_delete() fallback also for rmdir()

Pali Rohár posted 35 patches 1 month ago
[PATCH 32/35] cifs: Use cifs_rename_pending_delete() fallback also for rmdir()
Posted by Pali Rohár 1 month ago
When ->rmdir() callback fails on the -EBUSY error then use the silly rename
functionality fallback. Removing of directories has exactly same problem
with DELETE_PENDING state as removal of the files.

Empty directory which is opened on Windows server will stay in
DELETE_PENDING state until the last user on server does not close it.
Directory itself is not immediately unlinked at remove call, like file.

Note that currently the ->rmdir() callbacks do not return -EBUSY error.
This will be implemented in follow up changes.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 fs/smb/client/inode.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/fs/smb/client/inode.c b/fs/smb/client/inode.c
index 545964cac9cd..abe3108e4963 100644
--- a/fs/smb/client/inode.c
+++ b/fs/smb/client/inode.c
@@ -1700,6 +1700,7 @@ static int
 cifs_rename_pending_delete(const unsigned int xid,
 			   struct cifs_tcon *tcon,
 			   const char *full_path,
+			   bool is_dir,
 			   struct dentry *dentry)
 {
 	int oplock = 0;
@@ -1811,7 +1812,8 @@ cifs_rename_pending_delete(const unsigned int xid,
 		.tcon = tcon,
 		.cifs_sb = cifs_sb,
 		.desired_access = DELETE,
-		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
+		.create_options = cifs_create_options(cifs_sb,
+					is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR),
 		.disposition = FILE_OPEN,
 		.path = full_path,
 		.fid = &fid,
@@ -1962,7 +1964,7 @@ int cifs_unlink(struct inode *dir, struct dentry *dentry)
 	} else if (rc == -ENOENT) {
 		d_drop(dentry);
 	} else if (rc == -EBUSY) {
-		rc = cifs_rename_pending_delete(xid, tcon, full_path, dentry);
+		rc = cifs_rename_pending_delete(xid, tcon, full_path, false /* is_dir */, dentry);
 		if (rc == 0) {
 			cifs_mark_open_handles_for_deleted_file(inode, full_path);
 			cifs_drop_nlink(inode);
@@ -2298,6 +2300,9 @@ int cifs_rmdir(struct inode *inode, struct dentry *direntry)
 	}
 
 	rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
+	if (rc == -EBUSY)
+		rc = cifs_rename_pending_delete(xid, tcon, full_path, true /* is_dir */, direntry);
+
 	cifs_put_tlink(tlink);
 
 	if (!rc) {
-- 
2.20.1