Refine lock acquisition order for renames.
authorMichael Vrable <mvrable@cs.ucsd.edu>
Wed, 10 Mar 2010 22:47:04 +0000 (14:47 -0800)
committerMichael Vrable <mvrable@cs.ucsd.edu>
Wed, 10 Mar 2010 22:47:04 +0000 (14:47 -0800)
nfs3/nfs3.c

index 818cd4b..33ca180 100644 (file)
@@ -754,17 +754,22 @@ void nfsproc3_rename_3_svc(rename3args *argp, RPCRequest *req)
         async_rpc_send_reply(req, &result);
         return;
     }
-    g_mutex_lock(dir1->lock);
-    encode_pre_wcc(wcc1, dir1);
 
     BlueSkyInode *dir2 = lookup_fh(req, &argp->to.dir);
     if (dir2 == NULL) {
         result.status = NFS3ERR_STALE;
-        g_mutex_unlock(dir1->lock);
         async_rpc_send_reply(req, &result);
         return;
     }
-    g_mutex_lock(dir2->lock);
+
+    if (dir1->inum < dir2->inum) {
+        g_mutex_lock(dir1->lock);
+        g_mutex_lock(dir2->lock);
+    } else if (dir1->inum > dir2->inum) {
+        g_mutex_lock(dir2->lock);
+        g_mutex_lock(dir1->lock);
+    }
+    encode_pre_wcc(wcc1, dir1);
     encode_pre_wcc(wcc2, dir1);
 
     gboolean status = bluesky_rename(dir1, argp->from.name,
@@ -780,8 +785,9 @@ void nfsproc3_rename_3_svc(rename3args *argp, RPCRequest *req)
     else
         result.status = NFS3ERR_PERM;
 
-    g_mutex_unlock(dir2->lock);
     g_mutex_unlock(dir1->lock);
+    if (dir1->inum != dir2->inum)
+        g_mutex_unlock(dir2->lock);
     async_rpc_send_reply(req, &result);
 }