Linux: Fix lock ordering
authorSimon Wilkinson <sxw@inf.ed.ac.uk>
Mon, 16 Nov 2009 22:52:01 +0000 (22:52 +0000)
committerDerrick Brashear <shadow|account-1000005@unknown>
Wed, 18 Nov 2009 13:15:27 +0000 (05:15 -0800)
The inode mutex (or semaphore) should be held before acquiring the
alloc semaphore. Fix the lock ordering to avoid theoretical deadlocks.

Change-Id: Ifc572083dc92fff51be6785b0bcc92152fab1d73
Reviewed-on: http://gerrit.openafs.org/835
Reviewed-by: Marc Dionne <marc.c.dionne@gmail.com>
Tested-by: Derrick Brashear <shadow@dementia.org>
Reviewed-by: Derrick Brashear <shadow@dementia.org>

src/afs/LINUX/osi_file.c

index 378cda8..a1eebe3 100644 (file)
@@ -191,14 +191,14 @@ osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
        return code;
     ObtainWriteLock(&afs_xosi, 321);
     AFS_GUNLOCK();
-#ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
-    down_write(&inode->i_alloc_sem);
-#endif
 #ifdef STRUCT_INODE_HAS_I_MUTEX
     mutex_lock(&inode->i_mutex);
 #else
     down(&inode->i_sem);
 #endif
+#ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
+    down_write(&inode->i_alloc_sem);
+#endif
     newattrs.ia_size = asize;
     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
     newattrs.ia_ctime = CURRENT_TIME;
@@ -220,14 +220,14 @@ osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
     if (!code)
        truncate_inode_pages(&inode->i_data, asize);
     code = -code;
+#ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
+    up_write(&inode->i_alloc_sem);
+#endif
 #ifdef STRUCT_INODE_HAS_I_MUTEX
     mutex_unlock(&inode->i_mutex);
 #else
     up(&inode->i_sem);
 #endif
-#ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
-    up_write(&inode->i_alloc_sem);
-#endif
     AFS_GLOCK();
     ReleaseWriteLock(&afs_xosi);
     return code;