linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benny Halevy <bhalevy@panasas.com>
To: linux-nfs@vger.kernel.org
Subject: [PATCH 2/2] SQUASHME: pnfs-submit: clean up nfs_lock_alloc_layout
Date: Mon, 12 Jul 2010 21:40:15 +0300	[thread overview]
Message-ID: <1278960015-32215-1-git-send-email-bhalevy@panasas.com> (raw)
In-Reply-To: <4C3B611C.1000208@panasas.com>

Fix a bug where the function returned without taking the i_lock
if the layout hdr was already allocated.
Simplify by moving inode locking to caller.

Rename function as it no longer grabs the lock.
Clean up the implementation so it's clearer what's going on
and what are the likely cases vs. the unlikely ones.

Signed-off-by: Benny Halevy <bhalevy@panasas.com>
---
 fs/nfs/pnfs.c |   42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
index 4ba7595..053a5c1 100644
--- a/fs/nfs/pnfs.c
+++ b/fs/nfs/pnfs.c
@@ -938,36 +938,37 @@ alloc_init_layout(struct inode *ino)
 }
 
 /*
- * Lock and possibly allocate the inode layout
+ * Retrieve and possibly allocate the inode layout
  *
- * If successful, ino->i_lock is taken, and the caller must unlock.
+ * ino->i_lock must be taken by the caller.
  */
 static struct pnfs_layout_type *
-nfs_lock_alloc_layout(struct inode *ino)
+pnfs_alloc_layout(struct inode *ino)
 {
+	struct nfs_inode *nfsi = NFS_I(ino);
 	struct pnfs_layout_type *new = NULL;
 
-	dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, NFS_I(ino)->layout);
+	dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
 
-	if (NFS_I(ino)->layout == NULL) {
-		new = alloc_init_layout(ino);
-		if (new == NULL)
-			return NULL;
-		spin_lock(&ino->i_lock);
-		if (NFS_I(ino)->layout == NULL) {
-			NFS_I(ino)->layout = new;
-			new = NULL;
-		}
-	}
-	if (new) {
+	BUG_ON(!spin_is_locked(&ino->i_lock));
+	if (likely(nfsi->layout))
+		return nfsi->layout;
+
+	spin_unlock(&ino->i_lock);
+	new = alloc_init_layout(ino);
+	spin_lock(&ino->i_lock);
+
+	if (likely(nfsi->layout == NULL)) {	/* Won the race? */
+		nfsi->layout = new;
+	} else if (new) {
 		/* Reference the layout accross i_lock release and grab */
-		get_layout(NFS_I(ino)->layout);
+		get_layout(nfsi->layout);
 		spin_unlock(&ino->i_lock);
 		NFS_SERVER(ino)->pnfs_curr_ld->ld_io_ops->free_layout(new);
 		spin_lock(&ino->i_lock);
-		put_layout_locked(NFS_I(ino)->layout);
+		put_layout_locked(nfsi->layout);
 	}
-	return NFS_I(ino)->layout;
+	return nfsi->layout;
 }
 
 /*
@@ -1055,10 +1056,11 @@ _pnfs_update_layout(struct inode *ino,
 
 	if (take_ref)
 		*lsegpp = NULL;
-	lo = nfs_lock_alloc_layout(ino);
+	spin_lock(&ino->i_lock);
+	lo = pnfs_alloc_layout(ino);
 	if (lo == NULL) {
 		dprintk("%s ERROR: can't get pnfs_layout_type\n", __func__);
-		goto out;
+		goto out_unlock;
 	}
 
 	/* Check to see if the layout for the given range already exists */
-- 
1.7.1.1


  parent reply	other threads:[~2010-07-12 18:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-12 18:38 [PATCH 0/2] layout rework fixes Benny Halevy
2010-07-12 18:39 ` [PATCH 1/2] SQUASHME: pnfs-submit: check has_layout in __nfs4_close Benny Halevy
2010-07-12 18:40 ` Benny Halevy [this message]
2010-07-13 13:44   ` [PATCH 2/2] SQUASHME: pnfs-submit: clean up nfs_lock_alloc_layout William A. (Andy) Adamson
     [not found]     ` <AANLkTin5Wo-9paVQvfXZwDx5KZFkpSSspTCIN6vRCc8h-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-07-13 14:02       ` Benny Halevy
2010-07-13 14:22         ` William A. (Andy) Adamson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1278960015-32215-1-git-send-email-bhalevy@panasas.com \
    --to=bhalevy@panasas.com \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).