[PATCH 3/4] ovl: only lock readdir for accessing the cache

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The only reason parallel readdirs cannot run on the same inode is shared
access to the readdir cache.

Move lock/unlock to only protect the cache.  Exception is the refcount
which now uses atomic ops.

Signed-off-by: Miklos Szeredi <mszeredi@xxxxxxxxxx>
---
 fs/overlayfs/readdir.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/fs/overlayfs/readdir.c b/fs/overlayfs/readdir.c
index edee9f86f469..b98e0d17f40e 100644
--- a/fs/overlayfs/readdir.c
+++ b/fs/overlayfs/readdir.c
@@ -245,8 +245,10 @@ static void ovl_cache_put(struct ovl_dir_file *od, struct inode *inode)
 	struct ovl_dir_cache *cache = od->cache;
 
 	if (refcount_dec_and_test(&cache->refcount)) {
+		ovl_inode_lock(inode);
 		if (ovl_dir_cache(inode) == cache)
 			ovl_set_dir_cache(inode, NULL);
+		ovl_inode_unlock(inode);
 
 		ovl_cache_free(&cache->entries);
 		kfree(cache);
@@ -733,12 +735,18 @@ static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
 	}
 
 	if (ovl_is_impure_dir(file)) {
+		ovl_inode_lock(file_inode(file));
 		rdt.cache = ovl_cache_get_impure(&file->f_path);
-		if (IS_ERR(rdt.cache))
+		if (IS_ERR(rdt.cache)) {
+			ovl_inode_unlock(file_inode(file));
 			return PTR_ERR(rdt.cache);
+		}
 	}
 
 	err = iterate_dir(od->realfile, &rdt.ctx);
+
+	if (rdt.cache)
+		ovl_inode_unlock(file_inode(file));
 	ctx->pos = rdt.ctx.pos;
 
 	return err;
@@ -758,7 +766,6 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)
 	if (!ctx->pos)
 		ovl_dir_reset(file);
 
-	ovl_inode_lock(file_inode(file));
 	if (od->is_real) {
 		/*
 		 * If parent is merge, then need to adjust d_ino for '..', if
@@ -773,9 +780,10 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)
 		} else {
 			err = iterate_dir(od->realfile, ctx);
 		}
-		goto out;
+		goto out_revert;
 	}
 
+	ovl_inode_lock(file_inode(file));
 	if (!od->cache) {
 		struct ovl_dir_cache *cache;
 
@@ -808,6 +816,7 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)
 	err = 0;
 out:
 	ovl_inode_unlock(file_inode(file));
+out_revert:
 	revert_creds(old_cred);
 	return err;
 }
@@ -817,7 +826,6 @@ static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
 	loff_t res;
 	struct ovl_dir_file *od = file->private_data;
 
-	ovl_inode_lock(file_inode(file));
 	if (!file->f_pos)
 		ovl_dir_reset(file);
 
@@ -834,21 +842,22 @@ static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
 		case SEEK_SET:
 			break;
 		default:
-			goto out_unlock;
+			goto out;
 		}
 		if (offset < 0)
-			goto out_unlock;
+			goto out;
 
 		if (offset != file->f_pos) {
 			file->f_pos = offset;
-			if (od->cache)
+			if (od->cache) {
+				ovl_inode_lock(file_inode(file));
 				ovl_seek_cursor(od, offset);
+				ovl_inode_unlock(file_inode(file));
+			}
 		}
 		res = offset;
 	}
-out_unlock:
-	ovl_inode_unlock(file_inode(file));
-
+out:
 	return res;
 }
 
@@ -930,11 +939,8 @@ static int ovl_dir_release(struct inode *inode, struct file *file)
 {
 	struct ovl_dir_file *od = file->private_data;
 
-	if (od->cache) {
-		ovl_inode_lock(inode);
+	if (od->cache)
 		ovl_cache_put(od, inode);
-		ovl_inode_unlock(inode);
-	}
 	fput(od->realfile);
 	if (od->upperfile)
 		fput(od->upperfile);
-- 
2.44.0





[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux