]> err.no Git - linux-2.6/blobdiff - fs/jfs/jfs_txnmgr.c
JFS: Fix race in txLock
[linux-2.6] / fs / jfs / jfs_txnmgr.c
index 98e16d93e14647c047bd3eac0333e8894c51ea68..c7a92f9deb2b93c269d2b945418030f37f53ba51 100644 (file)
@@ -42,7 +42,6 @@
  * hold on to mp+lock thru update of maps
  */
 
-
 #include <linux/fs.h>
 #include <linux/vmalloc.h>
 #include <linux/smp_lock.h>
@@ -51,6 +50,7 @@
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include "jfs_incore.h"
+#include "jfs_inode.h"
 #include "jfs_filsys.h"
 #include "jfs_metapage.h"
 #include "jfs_dinode.h"
@@ -109,7 +109,6 @@ static int TxLockHWM;               /* High water mark for number of txLocks used */
 static int TxLockVHWM;         /* Very High water mark */
 struct tlock *TxLock;           /* transaction lock table */
 
-
 /*
  *      transaction management lock
  */
@@ -149,7 +148,6 @@ static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
 
 #define TXN_WAKEUP(event) wake_up_all(event)
 
-
 /*
  *      statistics
  */
@@ -161,16 +159,6 @@ static struct {
        int waitlock;           /* 4: # of tlock wait */
 } stattx;
 
-
-/*
- * external references
- */
-extern int lmGroupCommit(struct jfs_log *, struct tblock *);
-extern int jfs_commit_inode(struct inode *, int);
-extern int jfs_stop_threads;
-
-extern struct completion jfsIOwait;
-
 /*
  * forward references
  */
@@ -227,6 +215,7 @@ static lid_t txLockAlloc(void)
 
 static void txLockFree(lid_t lid)
 {
+       TxLock[lid].tid = 0;
        TxLock[lid].next = TxAnchor.freelock;
        TxAnchor.freelock = lid;
        TxAnchor.tlocksInUse--;
@@ -357,7 +346,6 @@ void txExit(void)
        TxBlock = NULL;
 }
 
-
 /*
  * NAME:        txBegin()
  *
@@ -459,7 +447,6 @@ tid_t txBegin(struct super_block *sb, int flag)
        return t;
 }
 
-
 /*
  * NAME:        txBeginAnon()
  *
@@ -502,7 +489,6 @@ void txBeginAnon(struct super_block *sb)
        TXN_UNLOCK();
 }
 
-
 /*
  *      txEnd()
  *
@@ -566,8 +552,10 @@ void txEnd(tid_t tid)
                 * synchronize with logsync barrier
                 */
                if (test_bit(log_SYNCBARRIER, &log->flag)) {
-                       /* forward log syncpt */
-                       /* lmSync(log); */
+                       TXN_UNLOCK();
+
+                       /* write dirty metadata & forward log syncpt */
+                       jfs_syncpt(log, 1);
 
                        jfs_info("log barrier off: 0x%x", log->lsn);
 
@@ -576,18 +564,19 @@ void txEnd(tid_t tid)
 
                        /* wakeup all waitors for logsync barrier */
                        TXN_WAKEUP(&log->syncwait);
+
+                       goto wakeup;
                }
        }
 
+       TXN_UNLOCK();
+wakeup:
        /*
         * wakeup all waitors for a free tblock
         */
        TXN_WAKEUP(&TxAnchor.freewait);
-
-       TXN_UNLOCK();
 }
 
-
 /*
  *      txLock()
  *
@@ -633,8 +622,10 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
 
        /* is page locked by the requester transaction ? */
        tlck = lid_to_tlock(lid);
-       if ((xtid = tlck->tid) == tid)
+       if ((xtid = tlck->tid) == tid) {
+               TXN_UNLOCK();
                goto grantLock;
+       }
 
        /*
         * is page locked by anonymous transaction/lock ?
@@ -649,6 +640,7 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
         */
        if (xtid == 0) {
                tlck->tid = tid;
+               TXN_UNLOCK();
                tblk = tid_to_tblock(tid);
                /*
                 * The order of the tlocks in the transaction is important
@@ -665,7 +657,9 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
                                /* only anonymous txn.
                                 * Remove from anon_list
                                 */
+                               TXN_LOCK();
                                list_del_init(&jfs_ip->anon_inode_list);
+                               TXN_UNLOCK();
                        }
                        jfs_ip->atlhead = tlck->next;
                } else {
@@ -706,17 +700,18 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
         */
        tlck->tid = tid;
 
+       TXN_UNLOCK();
+
        /* mark tlock for meta-data page */
        if (mp->xflag & COMMIT_PAGE) {
 
                tlck->flag = tlckPAGELOCK;
 
                /* mark the page dirty and nohomeok */
-               mark_metapage_dirty(mp);
-               atomic_inc(&mp->nohomeok);
+               metapage_nohomeok(mp);
 
                jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
-                        mp, atomic_read(&mp->nohomeok), tid, tlck);
+                        mp, mp->nohomeok, tid, tlck);
 
                /* if anonymous transaction, and buffer is on the group
                 * commit synclist, mark inode to show this.  This will
@@ -762,8 +757,10 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
                if (tlck->next == 0) {
                        /* This inode's first anonymous transaction */
                        jfs_ip->atltail = lid;
+                       TXN_LOCK();
                        list_add_tail(&jfs_ip->anon_inode_list,
                                      &TxAnchor.anon_list);
+                       TXN_UNLOCK();
                }
        }
 
@@ -821,8 +818,6 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
       grantLock:
        tlck->type |= type;
 
-       TXN_UNLOCK();
-
        return tlck;
 
        /*
@@ -841,17 +836,24 @@ struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
                BUG();
        }
        INCREMENT(stattx.waitlock);     /* statistics */
+       TXN_UNLOCK();
        release_metapage(mp);
+       TXN_LOCK();
+       xtid = tlck->tid;       /* reaquire after dropping TXN_LOCK */
 
        jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
                 tid, xtid, lid);
-       TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
+
+       /* Recheck everything since dropping TXN_LOCK */
+       if (xtid && (tlck->mp == mp) && (mp->lid == lid))
+               TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
+       else
+               TXN_UNLOCK();
        jfs_info("txLock: awakened     tid = %d, lid = %d", tid, lid);
 
        return NULL;
 }
 
-
 /*
  * NAME:        txRelease()
  *
@@ -891,7 +893,6 @@ static void txRelease(struct tblock * tblk)
        TXN_UNLOCK();
 }
 
-
 /*
  * NAME:        txUnlock()
  *
@@ -906,6 +907,7 @@ static void txUnlock(struct tblock * tblk)
        struct metapage *mp;
        struct jfs_log *log;
        int difft, diffp;
+       unsigned long flags;
 
        jfs_info("txUnlock: tblk = 0x%p", tblk);
        log = JFS_SBI(tblk->sb)->log;
@@ -925,19 +927,14 @@ static void txUnlock(struct tblock * tblk)
                        assert(mp->xflag & COMMIT_PAGE);
 
                        /* hold buffer
-                        *
-                        * It's possible that someone else has the metapage.
-                        * The only things were changing are nohomeok, which
-                        * is handled atomically, and clsn which is protected
-                        * by the LOGSYNC_LOCK.
                         */
-                       hold_metapage(mp, 1);
+                       hold_metapage(mp);
 
-                       assert(atomic_read(&mp->nohomeok) > 0);
-                       atomic_dec(&mp->nohomeok);
+                       assert(mp->nohomeok > 0);
+                       _metapage_homeok(mp);
 
                        /* inherit younger/larger clsn */
-                       LOGSYNC_LOCK(log);
+                       LOGSYNC_LOCK(log, flags);
                        if (mp->clsn) {
                                logdiff(difft, tblk->clsn, log);
                                logdiff(diffp, mp->clsn, log);
@@ -945,16 +942,11 @@ static void txUnlock(struct tblock * tblk)
                                        mp->clsn = tblk->clsn;
                        } else
                                mp->clsn = tblk->clsn;
-                       LOGSYNC_UNLOCK(log);
+                       LOGSYNC_UNLOCK(log, flags);
 
                        assert(!(tlck->flag & tlckFREEPAGE));
 
-                       if (tlck->flag & tlckWRITEPAGE) {
-                               write_metapage(mp);
-                       } else {
-                               /* release page which has been forced */
-                               release_metapage(mp);
-                       }
+                       put_metapage(mp);
                }
 
                /* insert tlock, and linelock(s) of the tlock if any,
@@ -981,14 +973,13 @@ static void txUnlock(struct tblock * tblk)
         * has been inserted in logsync list at txUpdateMap())
         */
        if (tblk->lsn) {
-               LOGSYNC_LOCK(log);
+               LOGSYNC_LOCK(log, flags);
                log->count--;
                list_del(&tblk->synclist);
-               LOGSYNC_UNLOCK(log);
+               LOGSYNC_UNLOCK(log, flags);
        }
 }
 
-
 /*
  *      txMaplock()
  *
@@ -1061,7 +1052,6 @@ struct tlock *txMaplock(tid_t tid, struct inode *ip, int type)
        return tlck;
 }
 
-
 /*
  *      txLinelock()
  *
@@ -1095,8 +1085,6 @@ struct linelock *txLinelock(struct linelock * tlock)
        return linelock;
 }
 
-
-
 /*
  *              transaction commit management
  *              -----------------------------
@@ -1365,7 +1353,6 @@ int txCommit(tid_t tid,           /* transaction identifier */
        return rc;
 }
 
-
 /*
  * NAME:        txLog()
  *
@@ -1429,7 +1416,6 @@ static int txLog(struct jfs_log * log, struct tblock * tblk, struct commit * cd)
        return rc;
 }
 
-
 /*
  *      diLog()
  *
@@ -1457,7 +1443,6 @@ static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
        if (tlck->type & tlckENTRY) {
                /* log after-image for logredo(): */
                lrd->type = cpu_to_le16(LOG_REDOPAGE);
-//              *pxd = mp->cm_pxd;
                PXDaddress(pxd, mp->index);
                PXDlength(pxd,
                          mp->logical_size >> tblk->sb->s_blocksize_bits);
@@ -1544,7 +1529,6 @@ static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
        return rc;
 }
 
-
 /*
  *      dataLog()
  *
@@ -1573,8 +1557,8 @@ static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
                 * the last entry, so don't bother logging this
                 */
                mp->lid = 0;
-               hold_metapage(mp, 0);
-               atomic_dec(&mp->nohomeok);
+               grab_metapage(mp);
+               metapage_homeok(mp);
                discard_metapage(mp);
                tlck->mp = NULL;
                return 0;
@@ -1591,7 +1575,6 @@ static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
        return 0;
 }
 
-
 /*
  *      dtLog()
  *
@@ -1631,7 +1614,6 @@ static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
                        lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
                else
                        lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
-//              *pxd = mp->cm_pxd;
                PXDaddress(pxd, mp->index);
                PXDlength(pxd,
                          mp->logical_size >> tblk->sb->s_blocksize_bits);
@@ -1696,7 +1678,6 @@ static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
        return;
 }
 
-
 /*
  *      xtLog()
  *
@@ -1752,7 +1733,6 @@ static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
                 * applying the after-image to the meta-data page.
                 */
                lrd->type = cpu_to_le16(LOG_REDOPAGE);
-//              *page_pxd = mp->cm_pxd;
                PXDaddress(page_pxd, mp->index);
                PXDlength(page_pxd,
                          mp->logical_size >> tblk->sb->s_blocksize_bits);
@@ -2085,7 +2065,6 @@ static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
        return;
 }
 
-
 /*
  *      mapLog()
  *
@@ -2172,7 +2151,6 @@ void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
        }
 }
 
-
 /*
  *      txEA()
  *
@@ -2225,7 +2203,6 @@ void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
        }
 }
 
-
 /*
  *      txForce()
  *
@@ -2270,7 +2247,8 @@ void txForce(struct tblock * tblk)
                                tlck->flag &= ~tlckWRITEPAGE;
 
                                /* do not release page to freelist */
-
+                               force_metapage(mp);
+#if 0
                                /*
                                 * The "right" thing to do here is to
                                 * synchronously write the metadata.
@@ -2282,15 +2260,15 @@ void txForce(struct tblock * tblk)
                                 * we can get by with synchronously writing
                                 * the pages when they are released.
                                 */
-                               assert(atomic_read(&mp->nohomeok));
+                               assert(mp->nohomeok);
                                set_bit(META_dirty, &mp->flag);
                                set_bit(META_sync, &mp->flag);
+#endif
                        }
                }
        }
 }
 
-
 /*
  *      txUpdateMap()
  *
@@ -2344,7 +2322,7 @@ static void txUpdateMap(struct tblock * tblk)
                         */
                        mp = tlck->mp;
                        ASSERT(mp->xflag & COMMIT_PAGE);
-                       hold_metapage(mp, 0);
+                       grab_metapage(mp);
                }
 
                /*
@@ -2394,8 +2372,8 @@ static void txUpdateMap(struct tblock * tblk)
                                ASSERT(mp->lid == lid);
                                tlck->mp->lid = 0;
                        }
-                       assert(atomic_read(&mp->nohomeok) == 1);
-                       atomic_dec(&mp->nohomeok);
+                       assert(mp->nohomeok == 1);
+                       metapage_homeok(mp);
                        discard_metapage(mp);
                        tlck->mp = NULL;
                }
@@ -2427,7 +2405,6 @@ static void txUpdateMap(struct tblock * tblk)
        }
 }
 
-
 /*
  *      txAllocPMap()
  *
@@ -2499,7 +2476,6 @@ static void txAllocPMap(struct inode *ip, struct maplock * maplock,
        }
 }
 
-
 /*
  *      txFreeMap()
  *
@@ -2601,7 +2577,6 @@ void txFreeMap(struct inode *ip,
        }
 }
 
-
 /*
  *      txFreelock()
  *
@@ -2642,7 +2617,6 @@ void txFreelock(struct inode *ip)
        TXN_UNLOCK();
 }
 
-
 /*
  *      txAbort()
  *
@@ -2816,9 +2790,9 @@ int jfs_lazycommit(void *arg)
                /* In case a wakeup came while all threads were active */
                jfs_commit_thread_waking = 0;
 
-               if (current->flags & PF_FREEZE) {
+               if (freezing(current)) {
                        LAZY_UNLOCK(flags);
-                       refrigerator(PF_FREEZE);
+                       refrigerator();
                } else {
                        DECLARE_WAITQUEUE(wq, current);
 
@@ -2861,24 +2835,9 @@ static void LogSyncRelease(struct metapage * mp)
 {
        struct jfs_log *log = mp->log;
 
-       assert(atomic_read(&mp->nohomeok));
+       assert(mp->nohomeok);
        assert(log);
-       atomic_dec(&mp->nohomeok);
-
-       if (atomic_read(&mp->nohomeok))
-               return;
-
-       hold_metapage(mp, 0);
-
-       LOGSYNC_LOCK(log);
-       mp->log = NULL;
-       mp->lsn = 0;
-       mp->clsn = 0;
-       log->count--;
-       list_del_init(&mp->synclist);
-       LOGSYNC_UNLOCK(log);
-
-       release_metapage(mp);
+       metapage_homeok(mp);
 }
 
 /*
@@ -3030,9 +2989,9 @@ int jfs_sync(void *arg)
                /* Add anon_list2 back to anon_list */
                list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
 
-               if (current->flags & PF_FREEZE) {
+               if (freezing(current)) {
                        TXN_UNLOCK();
-                       refrigerator(PF_FREEZE);
+                       refrigerator();
                } else {
                        DECLARE_WAITQUEUE(wq, current);