2 * Copyright (C) International Business Machines Corp., 2000-2005
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * jfs_txnmgr.c: transaction manager
24 * transaction starts with txBegin() and ends with txCommit()
27 * tlock is acquired at the time of update;
28 * (obviate scan at commit time for xtree and dtree)
29 * tlock and mp points to each other;
30 * (no hashlist for mp -> tlock).
33 * tlock on in-memory inode:
34 * in-place tlock in the in-memory inode itself;
35 * converted to page lock by iWrite() at commit time.
37 * tlock during write()/mmap() under anonymous transaction (tid = 0):
38 * transferred (?) to transaction at commit time.
40 * use the page itself to update allocation maps
41 * (obviate intermediate replication of allocation/deallocation data)
42 * hold on to mp+lock thru update of maps
47 #include <linux/vmalloc.h>
48 #include <linux/smp_lock.h>
49 #include <linux/completion.h>
50 #include <linux/suspend.h>
51 #include <linux/module.h>
52 #include <linux/moduleparam.h>
53 #include "jfs_incore.h"
54 #include "jfs_filsys.h"
55 #include "jfs_metapage.h"
56 #include "jfs_dinode.h"
59 #include "jfs_superblock.h"
60 #include "jfs_debug.h"
63 * transaction management structures
66 int freetid; /* index of a free tid structure */
67 int freelock; /* index first free lock word */
68 wait_queue_head_t freewait; /* eventlist of free tblock */
69 wait_queue_head_t freelockwait; /* eventlist of free tlock */
70 wait_queue_head_t lowlockwait; /* eventlist of ample tlocks */
71 int tlocksInUse; /* Number of tlocks in use */
72 spinlock_t LazyLock; /* synchronize sync_queue & unlock_queue */
73 /* struct tblock *sync_queue; * Transactions waiting for data sync */
74 struct list_head unlock_queue; /* Txns waiting to be released */
75 struct list_head anon_list; /* inodes having anonymous txns */
76 struct list_head anon_list2; /* inodes having anonymous txns
77 that couldn't be sync'ed */
80 int jfs_tlocks_low; /* Indicates low number of available tlocks */
82 #ifdef CONFIG_JFS_STATISTICS
86 uint txBegin_lockslow;
89 uint txBeginAnon_barrier;
90 uint txBeginAnon_lockslow;
92 uint txLockAlloc_freelock;
96 static int nTxBlock = -1; /* number of transaction blocks */
97 module_param(nTxBlock, int, 0);
98 MODULE_PARM_DESC(nTxBlock,
99 "Number of transaction blocks (max:65536)");
101 static int nTxLock = -1; /* number of transaction locks */
102 module_param(nTxLock, int, 0);
103 MODULE_PARM_DESC(nTxLock,
104 "Number of transaction locks (max:65536)");
106 struct tblock *TxBlock; /* transaction block table */
107 static int TxLockLWM; /* Low water mark for number of txLocks used */
108 static int TxLockHWM; /* High water mark for number of txLocks used */
109 static int TxLockVHWM; /* Very High water mark */
110 struct tlock *TxLock; /* transaction lock table */
114 * transaction management lock
116 static DEFINE_SPINLOCK(jfsTxnLock);
118 #define TXN_LOCK() spin_lock(&jfsTxnLock)
119 #define TXN_UNLOCK() spin_unlock(&jfsTxnLock)
121 #define LAZY_LOCK_INIT() spin_lock_init(&TxAnchor.LazyLock);
122 #define LAZY_LOCK(flags) spin_lock_irqsave(&TxAnchor.LazyLock, flags)
123 #define LAZY_UNLOCK(flags) spin_unlock_irqrestore(&TxAnchor.LazyLock, flags)
125 DECLARE_WAIT_QUEUE_HEAD(jfs_sync_thread_wait);
126 DECLARE_WAIT_QUEUE_HEAD(jfs_commit_thread_wait);
127 static int jfs_commit_thread_waking;
130 * Retry logic exist outside these macros to protect from spurrious wakeups.
132 static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
134 DECLARE_WAITQUEUE(wait, current);
136 add_wait_queue(event, &wait);
137 set_current_state(TASK_UNINTERRUPTIBLE);
140 current->state = TASK_RUNNING;
141 remove_wait_queue(event, &wait);
144 #define TXN_SLEEP(event)\
146 TXN_SLEEP_DROP_LOCK(event);\
150 #define TXN_WAKEUP(event) wake_up_all(event)
157 tid_t maxtid; /* 4: biggest tid ever used */
158 lid_t maxlid; /* 4: biggest lid ever used */
159 int ntid; /* 4: # of transactions performed */
160 int nlid; /* 4: # of tlocks acquired */
161 int waitlock; /* 4: # of tlock wait */
166 * external references
168 extern int lmGroupCommit(struct jfs_log *, struct tblock *);
169 extern int jfs_commit_inode(struct inode *, int);
170 extern int jfs_stop_threads;
172 extern struct completion jfsIOwait;
177 static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
178 struct tlock * tlck, struct commit * cd);
179 static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
180 struct tlock * tlck);
181 static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
182 struct tlock * tlck);
183 static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
184 struct tlock * tlck);
185 static void txAllocPMap(struct inode *ip, struct maplock * maplock,
186 struct tblock * tblk);
187 static void txForce(struct tblock * tblk);
188 static int txLog(struct jfs_log * log, struct tblock * tblk,
190 static void txUpdateMap(struct tblock * tblk);
191 static void txRelease(struct tblock * tblk);
192 static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
193 struct tlock * tlck);
194 static void LogSyncRelease(struct metapage * mp);
197 * transaction block/lock management
198 * ---------------------------------
202 * Get a transaction lock from the free list. If the number in use is
203 * greater than the high water mark, wake up the sync daemon. This should
204 * free some anonymous transaction locks. (TXN_LOCK must be held.)
206 static lid_t txLockAlloc(void)
210 INCREMENT(TxStat.txLockAlloc);
211 if (!TxAnchor.freelock) {
212 INCREMENT(TxStat.txLockAlloc_freelock);
215 while (!(lid = TxAnchor.freelock))
216 TXN_SLEEP(&TxAnchor.freelockwait);
217 TxAnchor.freelock = TxLock[lid].next;
218 HIGHWATERMARK(stattx.maxlid, lid);
219 if ((++TxAnchor.tlocksInUse > TxLockHWM) && (jfs_tlocks_low == 0)) {
220 jfs_info("txLockAlloc tlocks low");
222 wake_up(&jfs_sync_thread_wait);
228 static void txLockFree(lid_t lid)
231 TxLock[lid].next = TxAnchor.freelock;
232 TxAnchor.freelock = lid;
233 TxAnchor.tlocksInUse--;
234 if (jfs_tlocks_low && (TxAnchor.tlocksInUse < TxLockLWM)) {
235 jfs_info("txLockFree jfs_tlocks_low no more");
237 TXN_WAKEUP(&TxAnchor.lowlockwait);
239 TXN_WAKEUP(&TxAnchor.freelockwait);
245 * FUNCTION: initialize transaction management structures
249 * serialization: single thread at jfs_init()
256 /* Set defaults for nTxLock and nTxBlock if unset */
259 if (nTxBlock == -1) {
260 /* Base default on memory size */
262 if (si.totalram > (256 * 1024)) /* 1 GB */
265 nTxLock = si.totalram >> 2;
266 } else if (nTxBlock > (8 * 1024))
269 nTxLock = nTxBlock << 3;
272 nTxBlock = nTxLock >> 3;
274 /* Verify tunable parameters */
276 nTxBlock = 16; /* No one should set it this low */
277 if (nTxBlock > 65536)
280 nTxLock = 256; /* No one should set it this low */
284 printk(KERN_INFO "JFS: nTxBlock = %d, nTxLock = %d\n",
287 * initialize transaction block (tblock) table
289 * transaction id (tid) = tblock index
290 * tid = 0 is reserved.
292 TxLockLWM = (nTxLock * 4) / 10;
293 TxLockHWM = (nTxLock * 7) / 10;
294 TxLockVHWM = (nTxLock * 8) / 10;
296 size = sizeof(struct tblock) * nTxBlock;
297 TxBlock = (struct tblock *) vmalloc(size);
301 for (k = 1; k < nTxBlock - 1; k++) {
302 TxBlock[k].next = k + 1;
303 init_waitqueue_head(&TxBlock[k].gcwait);
304 init_waitqueue_head(&TxBlock[k].waitor);
307 init_waitqueue_head(&TxBlock[k].gcwait);
308 init_waitqueue_head(&TxBlock[k].waitor);
310 TxAnchor.freetid = 1;
311 init_waitqueue_head(&TxAnchor.freewait);
313 stattx.maxtid = 1; /* statistics */
316 * initialize transaction lock (tlock) table
318 * transaction lock id = tlock index
319 * tlock id = 0 is reserved.
321 size = sizeof(struct tlock) * nTxLock;
322 TxLock = (struct tlock *) vmalloc(size);
323 if (TxLock == NULL) {
328 /* initialize tlock table */
329 for (k = 1; k < nTxLock - 1; k++)
330 TxLock[k].next = k + 1;
332 init_waitqueue_head(&TxAnchor.freelockwait);
333 init_waitqueue_head(&TxAnchor.lowlockwait);
335 TxAnchor.freelock = 1;
336 TxAnchor.tlocksInUse = 0;
337 INIT_LIST_HEAD(&TxAnchor.anon_list);
338 INIT_LIST_HEAD(&TxAnchor.anon_list2);
341 INIT_LIST_HEAD(&TxAnchor.unlock_queue);
343 stattx.maxlid = 1; /* statistics */
351 * FUNCTION: clean up when module is unloaded
365 * FUNCTION: start a transaction.
367 * PARAMETER: sb - superblock
368 * flag - force for nested tx;
370 * RETURN: tid - transaction id
372 * note: flag force allows to start tx for nested tx
373 * to prevent deadlock on logsync barrier;
375 tid_t txBegin(struct super_block *sb, int flag)
381 jfs_info("txBegin: flag = 0x%x", flag);
382 log = JFS_SBI(sb)->log;
386 INCREMENT(TxStat.txBegin);
389 if (!(flag & COMMIT_FORCE)) {
391 * synchronize with logsync barrier
393 if (test_bit(log_SYNCBARRIER, &log->flag) ||
394 test_bit(log_QUIESCE, &log->flag)) {
395 INCREMENT(TxStat.txBegin_barrier);
396 TXN_SLEEP(&log->syncwait);
402 * Don't begin transaction if we're getting starved for tlocks
403 * unless COMMIT_FORCE or COMMIT_INODE (which may ultimately
406 if (TxAnchor.tlocksInUse > TxLockVHWM) {
407 INCREMENT(TxStat.txBegin_lockslow);
408 TXN_SLEEP(&TxAnchor.lowlockwait);
414 * allocate transaction id/block
416 if ((t = TxAnchor.freetid) == 0) {
417 jfs_info("txBegin: waiting for free tid");
418 INCREMENT(TxStat.txBegin_freetid);
419 TXN_SLEEP(&TxAnchor.freewait);
423 tblk = tid_to_tblock(t);
425 if ((tblk->next == 0) && !(flag & COMMIT_FORCE)) {
426 /* Don't let a non-forced transaction take the last tblk */
427 jfs_info("txBegin: waiting for free tid");
428 INCREMENT(TxStat.txBegin_freetid);
429 TXN_SLEEP(&TxAnchor.freewait);
433 TxAnchor.freetid = tblk->next;
436 * initialize transaction
440 * We can't zero the whole thing or we screw up another thread being
441 * awakened after sleeping on tblk->waitor
443 * memset(tblk, 0, sizeof(struct tblock));
445 tblk->next = tblk->last = tblk->xflag = tblk->flag = tblk->lsn = 0;
449 tblk->logtid = log->logtid;
453 HIGHWATERMARK(stattx.maxtid, t); /* statistics */
454 INCREMENT(stattx.ntid); /* statistics */
458 jfs_info("txBegin: returning tid = %d", t);
465 * NAME: txBeginAnon()
467 * FUNCTION: start an anonymous transaction.
468 * Blocks if logsync or available tlocks are low to prevent
469 * anonymous tlocks from depleting supply.
471 * PARAMETER: sb - superblock
475 void txBeginAnon(struct super_block *sb)
479 log = JFS_SBI(sb)->log;
482 INCREMENT(TxStat.txBeginAnon);
486 * synchronize with logsync barrier
488 if (test_bit(log_SYNCBARRIER, &log->flag) ||
489 test_bit(log_QUIESCE, &log->flag)) {
490 INCREMENT(TxStat.txBeginAnon_barrier);
491 TXN_SLEEP(&log->syncwait);
496 * Don't begin transaction if we're getting starved for tlocks
498 if (TxAnchor.tlocksInUse > TxLockVHWM) {
499 INCREMENT(TxStat.txBeginAnon_lockslow);
500 TXN_SLEEP(&TxAnchor.lowlockwait);
510 * function: free specified transaction block.
512 * logsync barrier processing:
516 void txEnd(tid_t tid)
518 struct tblock *tblk = tid_to_tblock(tid);
521 jfs_info("txEnd: tid = %d", tid);
525 * wakeup transactions waiting on the page locked
526 * by the current transaction
528 TXN_WAKEUP(&tblk->waitor);
530 log = JFS_SBI(tblk->sb)->log;
533 * Lazy commit thread can't free this guy until we mark it UNLOCKED,
534 * otherwise, we would be left with a transaction that may have been
537 * Lazy commit thread will turn off tblkGC_LAZY before calling this
540 if (tblk->flag & tblkGC_LAZY) {
541 jfs_info("txEnd called w/lazy tid: %d, tblk = 0x%p", tid, tblk);
544 spin_lock_irq(&log->gclock); // LOGGC_LOCK
545 tblk->flag |= tblkGC_UNLOCKED;
546 spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
550 jfs_info("txEnd: tid: %d, tblk = 0x%p", tid, tblk);
552 assert(tblk->next == 0);
555 * insert tblock back on freelist
557 tblk->next = TxAnchor.freetid;
558 TxAnchor.freetid = tid;
561 * mark the tblock not active
563 if (--log->active == 0) {
564 clear_bit(log_FLUSH, &log->flag);
567 * synchronize with logsync barrier
569 if (test_bit(log_SYNCBARRIER, &log->flag)) {
570 /* forward log syncpt */
573 jfs_info("log barrier off: 0x%x", log->lsn);
575 /* enable new transactions start */
576 clear_bit(log_SYNCBARRIER, &log->flag);
578 /* wakeup all waitors for logsync barrier */
579 TXN_WAKEUP(&log->syncwait);
584 * wakeup all waitors for a free tblock
586 TXN_WAKEUP(&TxAnchor.freewait);
595 * function: acquire a transaction lock on the specified <mp>
599 * return: transaction lock id
603 struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
606 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
611 struct xtlock *xtlck;
612 struct linelock *linelock;
618 if (S_ISDIR(ip->i_mode) && (type & tlckXTREE) &&
619 !(mp->xflag & COMMIT_PAGE)) {
621 * Directory inode is special. It can have both an xtree tlock
622 * and a dtree tlock associated with it.
629 /* is page not locked by a transaction ? */
633 jfs_info("txLock: tid:%d ip:0x%p mp:0x%p lid:%d", tid, ip, mp, lid);
635 /* is page locked by the requester transaction ? */
636 tlck = lid_to_tlock(lid);
637 if ((xtid = tlck->tid) == tid) {
643 * is page locked by anonymous transaction/lock ?
645 * (page update without transaction (i.e., file write) is
646 * locked under anonymous transaction tid = 0:
647 * anonymous tlocks maintained on anonymous tlock list of
648 * the inode of the page and available to all anonymous
649 * transactions until txCommit() time at which point
650 * they are transferred to the transaction tlock list of
651 * the commiting transaction of the inode)
656 tblk = tid_to_tblock(tid);
658 * The order of the tlocks in the transaction is important
659 * (during truncate, child xtree pages must be freed before
660 * parent's tlocks change the working map).
661 * Take tlock off anonymous list and add to tail of
664 * Note: We really need to get rid of the tid & lid and
665 * use list_head's. This code is getting UGLY!
667 if (jfs_ip->atlhead == lid) {
668 if (jfs_ip->atltail == lid) {
669 /* only anonymous txn.
670 * Remove from anon_list
672 list_del_init(&jfs_ip->anon_inode_list);
674 jfs_ip->atlhead = tlck->next;
677 for (last = jfs_ip->atlhead;
678 lid_to_tlock(last)->next != lid;
679 last = lid_to_tlock(last)->next) {
682 lid_to_tlock(last)->next = tlck->next;
683 if (jfs_ip->atltail == lid)
684 jfs_ip->atltail = last;
687 /* insert the tlock at tail of transaction tlock list */
690 lid_to_tlock(tblk->last)->next = lid;
706 tlck = lid_to_tlock(lid);
715 /* mark tlock for meta-data page */
716 if (mp->xflag & COMMIT_PAGE) {
718 tlck->flag = tlckPAGELOCK;
720 /* mark the page dirty and nohomeok */
721 metapage_nohomeok(mp);
723 jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
724 mp, mp->nohomeok, tid, tlck);
726 /* if anonymous transaction, and buffer is on the group
727 * commit synclist, mark inode to show this. This will
728 * prevent the buffer from being marked nohomeok for too
731 if ((tid == 0) && mp->lsn)
732 set_cflag(COMMIT_Synclist, ip);
734 /* mark tlock for in-memory inode */
736 tlck->flag = tlckINODELOCK;
740 /* bind the tlock and the page */
749 * enqueue transaction lock to transaction/inode
751 /* insert the tlock at tail of transaction tlock list */
753 tblk = tid_to_tblock(tid);
755 lid_to_tlock(tblk->last)->next = lid;
761 /* anonymous transaction:
762 * insert the tlock at head of inode anonymous tlock list
765 tlck->next = jfs_ip->atlhead;
766 jfs_ip->atlhead = lid;
767 if (tlck->next == 0) {
768 /* This inode's first anonymous transaction */
769 jfs_ip->atltail = lid;
771 list_add_tail(&jfs_ip->anon_inode_list,
772 &TxAnchor.anon_list);
777 /* initialize type dependent area for linelock */
778 linelock = (struct linelock *) & tlck->lock;
780 linelock->flag = tlckLINELOCK;
781 linelock->maxcnt = TLOCKSHORT;
784 switch (type & tlckTYPE) {
786 linelock->l2linesize = L2DTSLOTSIZE;
790 linelock->l2linesize = L2XTSLOTSIZE;
792 xtlck = (struct xtlock *) linelock;
793 xtlck->header.offset = 0;
794 xtlck->header.length = 2;
796 if (type & tlckNEW) {
797 xtlck->lwm.offset = XTENTRYSTART;
799 if (mp->xflag & COMMIT_PAGE)
800 p = (xtpage_t *) mp->data;
802 p = &jfs_ip->i_xtroot;
804 le16_to_cpu(p->header.nextindex);
806 xtlck->lwm.length = 0; /* ! */
807 xtlck->twm.offset = 0;
808 xtlck->hwm.offset = 0;
814 linelock->l2linesize = L2INODESLOTSIZE;
818 linelock->l2linesize = L2DATASLOTSIZE;
822 jfs_err("UFO tlock:0x%p", tlck);
826 * update tlock vector
834 * page is being locked by another transaction:
837 /* Only locks on ipimap or ipaimap should reach here */
838 /* assert(jfs_ip->fileset == AGGREGATE_I); */
839 if (jfs_ip->fileset != AGGREGATE_I) {
840 jfs_err("txLock: trying to lock locked page!");
841 dump_mem("ip", ip, sizeof(struct inode));
842 dump_mem("mp", mp, sizeof(struct metapage));
843 dump_mem("Locker's tblk", tid_to_tblock(tid),
844 sizeof(struct tblock));
845 dump_mem("Tlock", tlck, sizeof(struct tlock));
848 INCREMENT(stattx.waitlock); /* statistics */
850 release_metapage(mp);
852 xtid = tlck->tid; /* reaquire after dropping TXN_LOCK */
854 jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
857 /* Recheck everything since dropping TXN_LOCK */
858 if (xtid && (tlck->mp == mp) && (mp->lid == lid))
859 TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
862 jfs_info("txLock: awakened tid = %d, lid = %d", tid, lid);
871 * FUNCTION: Release buffers associated with transaction locks, but don't
872 * mark homeok yet. The allows other transactions to modify
873 * buffers, but won't let them go to disk until commit record
874 * actually gets written.
879 * RETURN: Errors from subroutines.
881 static void txRelease(struct tblock * tblk)
889 for (lid = tblk->next; lid; lid = tlck->next) {
890 tlck = lid_to_tlock(lid);
891 if ((mp = tlck->mp) != NULL &&
892 (tlck->type & tlckBTROOT) == 0) {
893 assert(mp->xflag & COMMIT_PAGE);
899 * wakeup transactions waiting on a page locked
900 * by the current transaction
902 TXN_WAKEUP(&tblk->waitor);
911 * FUNCTION: Initiates pageout of pages modified by tid in journalled
912 * objects and frees their lockwords.
914 static void txUnlock(struct tblock * tblk)
917 struct linelock *linelock;
918 lid_t lid, next, llid, k;
924 jfs_info("txUnlock: tblk = 0x%p", tblk);
925 log = JFS_SBI(tblk->sb)->log;
928 * mark page under tlock homeok (its log has been written):
930 for (lid = tblk->next; lid; lid = next) {
931 tlck = lid_to_tlock(lid);
934 jfs_info("unlocking lid = %d, tlck = 0x%p", lid, tlck);
936 /* unbind page from tlock */
937 if ((mp = tlck->mp) != NULL &&
938 (tlck->type & tlckBTROOT) == 0) {
939 assert(mp->xflag & COMMIT_PAGE);
945 assert(mp->nohomeok > 0);
946 _metapage_homeok(mp);
948 /* inherit younger/larger clsn */
949 LOGSYNC_LOCK(log, flags);
951 logdiff(difft, tblk->clsn, log);
952 logdiff(diffp, mp->clsn, log);
954 mp->clsn = tblk->clsn;
956 mp->clsn = tblk->clsn;
957 LOGSYNC_UNLOCK(log, flags);
959 assert(!(tlck->flag & tlckFREEPAGE));
964 /* insert tlock, and linelock(s) of the tlock if any,
965 * at head of freelist
969 llid = ((struct linelock *) & tlck->lock)->next;
971 linelock = (struct linelock *) lid_to_tlock(llid);
980 tblk->next = tblk->last = 0;
983 * remove tblock from logsynclist
984 * (allocation map pages inherited lsn of tblk and
985 * has been inserted in logsync list at txUpdateMap())
988 LOGSYNC_LOCK(log, flags);
990 list_del(&tblk->synclist);
991 LOGSYNC_UNLOCK(log, flags);
999 * function: allocate a transaction lock for freed page/entry;
1000 * for freed page, maplock is used as xtlock/dtlock type;
1002 struct tlock *txMaplock(tid_t tid, struct inode *ip, int type)
1004 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1006 struct tblock *tblk;
1008 struct maplock *maplock;
1015 lid = txLockAlloc();
1016 tlck = lid_to_tlock(lid);
1023 /* bind the tlock and the object */
1024 tlck->flag = tlckINODELOCK;
1031 * enqueue transaction lock to transaction/inode
1033 /* insert the tlock at tail of transaction tlock list */
1035 tblk = tid_to_tblock(tid);
1037 lid_to_tlock(tblk->last)->next = lid;
1043 /* anonymous transaction:
1044 * insert the tlock at head of inode anonymous tlock list
1047 tlck->next = jfs_ip->atlhead;
1048 jfs_ip->atlhead = lid;
1049 if (tlck->next == 0) {
1050 /* This inode's first anonymous transaction */
1051 jfs_ip->atltail = lid;
1052 list_add_tail(&jfs_ip->anon_inode_list,
1053 &TxAnchor.anon_list);
1059 /* initialize type dependent area for maplock */
1060 maplock = (struct maplock *) & tlck->lock;
1062 maplock->maxcnt = 0;
1072 * function: allocate a transaction lock for log vector list
1074 struct linelock *txLinelock(struct linelock * tlock)
1078 struct linelock *linelock;
1082 /* allocate a TxLock structure */
1083 lid = txLockAlloc();
1084 tlck = lid_to_tlock(lid);
1088 /* initialize linelock */
1089 linelock = (struct linelock *) tlck;
1091 linelock->flag = tlckLINELOCK;
1092 linelock->maxcnt = TLOCKLONG;
1093 linelock->index = 0;
1095 /* append linelock after tlock */
1096 linelock->next = tlock->next;
1105 * transaction commit management
1106 * -----------------------------
1112 * FUNCTION: commit the changes to the objects specified in
1113 * clist. For journalled segments only the
1114 * changes of the caller are committed, ie by tid.
1115 * for non-journalled segments the data are flushed to
1116 * disk and then the change to the disk inode and indirect
1117 * blocks committed (so blocks newly allocated to the
1118 * segment will be made a part of the segment atomically).
1120 * all of the segments specified in clist must be in
1121 * one file system. no more than 6 segments are needed
1122 * to handle all unix svcs.
1124 * if the i_nlink field (i.e. disk inode link count)
1125 * is zero, and the type of inode is a regular file or
1126 * directory, or symbolic link , the inode is truncated
1127 * to zero length. the truncation is committed but the
1128 * VM resources are unaffected until it is closed (see
1136 * on entry the inode lock on each segment is assumed
1141 int txCommit(tid_t tid, /* transaction identifier */
1142 int nip, /* number of inodes to commit */
1143 struct inode **iplist, /* list of inode to commit */
1148 struct jfs_log *log;
1149 struct tblock *tblk;
1153 struct jfs_inode_info *jfs_ip;
1156 struct super_block *sb;
1158 jfs_info("txCommit, tid = %d, flag = %d", tid, flag);
1159 /* is read-only file system ? */
1160 if (isReadOnly(iplist[0])) {
1165 sb = cd.sb = iplist[0]->i_sb;
1169 tid = txBegin(sb, 0);
1170 tblk = tid_to_tblock(tid);
1173 * initialize commit structure
1175 log = JFS_SBI(sb)->log;
1178 /* initialize log record descriptor in commit */
1180 lrd->logtid = cpu_to_le32(tblk->logtid);
1183 tblk->xflag |= flag;
1185 if ((flag & (COMMIT_FORCE | COMMIT_SYNC)) == 0)
1186 tblk->xflag |= COMMIT_LAZY;
1188 * prepare non-journaled objects for commit
1190 * flush data pages of non-journaled file
1191 * to prevent the file getting non-initialized disk blocks
1199 * acquire transaction lock on (on-disk) inodes
1201 * update on-disk inode from in-memory inode
1202 * acquiring transaction locks for AFTER records
1203 * on the on-disk inode of file object
1205 * sort the inodes array by inode number in descending order
1206 * to prevent deadlock when acquiring transaction lock
1207 * of on-disk inodes on multiple on-disk inode pages by
1208 * multiple concurrent transactions
1210 for (k = 0; k < cd.nip; k++) {
1211 top = (cd.iplist[k])->i_ino;
1212 for (n = k + 1; n < cd.nip; n++) {
1214 if (ip->i_ino > top) {
1216 cd.iplist[n] = cd.iplist[k];
1222 jfs_ip = JFS_IP(ip);
1225 * BUGBUG - This code has temporarily been removed. The
1226 * intent is to ensure that any file data is written before
1227 * the metadata is committed to the journal. This prevents
1228 * uninitialized data from appearing in a file after the
1229 * journal has been replayed. (The uninitialized data
1230 * could be sensitive data removed by another user.)
1232 * The problem now is that we are holding the IWRITELOCK
1233 * on the inode, and calling filemap_fdatawrite on an
1234 * unmapped page will cause a deadlock in jfs_get_block.
1236 * The long term solution is to pare down the use of
1237 * IWRITELOCK. We are currently holding it too long.
1238 * We could also be smarter about which data pages need
1239 * to be written before the transaction is committed and
1240 * when we don't need to worry about it at all.
1242 * if ((!S_ISDIR(ip->i_mode))
1243 * && (tblk->flag & COMMIT_DELETE) == 0) {
1244 * filemap_fdatawrite(ip->i_mapping);
1245 * filemap_fdatawait(ip->i_mapping);
1250 * Mark inode as not dirty. It will still be on the dirty
1251 * inode list, but we'll know not to commit it again unless
1252 * it gets marked dirty again
1254 clear_cflag(COMMIT_Dirty, ip);
1256 /* inherit anonymous tlock(s) of inode */
1257 if (jfs_ip->atlhead) {
1258 lid_to_tlock(jfs_ip->atltail)->next = tblk->next;
1259 tblk->next = jfs_ip->atlhead;
1261 tblk->last = jfs_ip->atltail;
1262 jfs_ip->atlhead = jfs_ip->atltail = 0;
1264 list_del_init(&jfs_ip->anon_inode_list);
1269 * acquire transaction lock on on-disk inode page
1270 * (become first tlock of the tblk's tlock list)
1272 if (((rc = diWrite(tid, ip))))
1277 * write log records from transaction locks
1279 * txUpdateMap() resets XAD_NEW in XAD.
1281 if ((rc = txLog(log, tblk, &cd)))
1285 * Ensure that inode isn't reused before
1286 * lazy commit thread finishes processing
1288 if (tblk->xflag & COMMIT_DELETE) {
1289 atomic_inc(&tblk->u.ip->i_count);
1291 * Avoid a rare deadlock
1293 * If the inode is locked, we may be blocked in
1294 * jfs_commit_inode. If so, we don't want the
1295 * lazy_commit thread doing the last iput() on the inode
1296 * since that may block on the locked inode. Instead,
1297 * commit the transaction synchronously, so the last iput
1298 * will be done by the calling thread (or later)
1300 if (tblk->u.ip->i_state & I_LOCK)
1301 tblk->xflag &= ~COMMIT_LAZY;
1304 ASSERT((!(tblk->xflag & COMMIT_DELETE)) ||
1305 ((tblk->u.ip->i_nlink == 0) &&
1306 !test_cflag(COMMIT_Nolink, tblk->u.ip)));
1309 * write COMMIT log record
1311 lrd->type = cpu_to_le16(LOG_COMMIT);
1313 lsn = lmLog(log, tblk, lrd, NULL);
1315 lmGroupCommit(log, tblk);
1318 * - transaction is now committed -
1322 * force pages in careful update
1323 * (imap addressing structure update)
1325 if (flag & COMMIT_FORCE)
1329 * update allocation map.
1331 * update inode allocation map and inode:
1332 * free pager lock on memory object of inode if any.
1333 * update block allocation map.
1335 * txUpdateMap() resets XAD_NEW in XAD.
1337 if (tblk->xflag & COMMIT_FORCE)
1341 * free transaction locks and pageout/free pages
1345 if ((tblk->flag & tblkGC_LAZY) == 0)
1350 * reset in-memory object state
1352 for (k = 0; k < cd.nip; k++) {
1354 jfs_ip = JFS_IP(ip);
1357 * reset in-memory inode state
1368 jfs_info("txCommit: tid = %d, returning %d", tid, rc);
1376 * FUNCTION: Writes AFTER log records for all lines modified
1377 * by tid for segments specified by inodes in comdata.
1378 * Code assumes only WRITELOCKS are recorded in lockwords.
1384 static int txLog(struct jfs_log * log, struct tblock * tblk, struct commit * cd)
1390 struct lrd *lrd = &cd->lrd;
1393 * write log record(s) for each tlock of transaction,
1395 for (lid = tblk->next; lid; lid = tlck->next) {
1396 tlck = lid_to_tlock(lid);
1398 tlck->flag |= tlckLOG;
1400 /* initialize lrd common */
1402 lrd->aggregate = cpu_to_le32(JFS_SBI(ip->i_sb)->aggregate);
1403 lrd->log.redopage.fileset = cpu_to_le32(JFS_IP(ip)->fileset);
1404 lrd->log.redopage.inode = cpu_to_le32(ip->i_ino);
1406 /* write log record of page from the tlock */
1407 switch (tlck->type & tlckTYPE) {
1409 xtLog(log, tblk, lrd, tlck);
1413 dtLog(log, tblk, lrd, tlck);
1417 diLog(log, tblk, lrd, tlck, cd);
1421 mapLog(log, tblk, lrd, tlck);
1425 dataLog(log, tblk, lrd, tlck);
1429 jfs_err("UFO tlock:0x%p", tlck);
1440 * function: log inode tlock and format maplock to update bmap;
1442 static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1443 struct tlock * tlck, struct commit * cd)
1446 struct metapage *mp;
1448 struct pxd_lock *pxdlock;
1452 /* initialize as REDOPAGE record format */
1453 lrd->log.redopage.type = cpu_to_le16(LOG_INODE);
1454 lrd->log.redopage.l2linesize = cpu_to_le16(L2INODESLOTSIZE);
1456 pxd = &lrd->log.redopage.pxd;
1461 if (tlck->type & tlckENTRY) {
1462 /* log after-image for logredo(): */
1463 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1464 // *pxd = mp->cm_pxd;
1465 PXDaddress(pxd, mp->index);
1467 mp->logical_size >> tblk->sb->s_blocksize_bits);
1468 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1470 /* mark page as homeward bound */
1471 tlck->flag |= tlckWRITEPAGE;
1472 } else if (tlck->type & tlckFREE) {
1476 * (pages of the freed inode extent have been invalidated and
1477 * a maplock for free of the extent has been formatted at
1480 * the tlock had been acquired on the inode allocation map page
1481 * (iag) that specifies the freed extent, even though the map
1482 * page is not itself logged, to prevent pageout of the map
1483 * page before the log;
1486 /* log LOG_NOREDOINOEXT of the freed inode extent for
1487 * logredo() to start NoRedoPage filters, and to update
1488 * imap and bmap for free of the extent;
1490 lrd->type = cpu_to_le16(LOG_NOREDOINOEXT);
1492 * For the LOG_NOREDOINOEXT record, we need
1493 * to pass the IAG number and inode extent
1494 * index (within that IAG) from which the
1495 * the extent being released. These have been
1496 * passed to us in the iplist[1] and iplist[2].
1498 lrd->log.noredoinoext.iagnum =
1499 cpu_to_le32((u32) (size_t) cd->iplist[1]);
1500 lrd->log.noredoinoext.inoext_idx =
1501 cpu_to_le32((u32) (size_t) cd->iplist[2]);
1503 pxdlock = (struct pxd_lock *) & tlck->lock;
1504 *pxd = pxdlock->pxd;
1505 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1508 tlck->flag |= tlckUPDATEMAP;
1510 /* mark page as homeward bound */
1511 tlck->flag |= tlckWRITEPAGE;
1513 jfs_err("diLog: UFO type tlck:0x%p", tlck);
1516 * alloc/free external EA extent
1518 * a maplock for txUpdateMap() to update bPWMAP for alloc/free
1519 * of the extent has been formatted at txLock() time;
1522 assert(tlck->type & tlckEA);
1524 /* log LOG_UPDATEMAP for logredo() to update bmap for
1525 * alloc of new (and free of old) external EA extent;
1527 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1528 pxdlock = (struct pxd_lock *) & tlck->lock;
1529 nlock = pxdlock->index;
1530 for (i = 0; i < nlock; i++, pxdlock++) {
1531 if (pxdlock->flag & mlckALLOCPXD)
1532 lrd->log.updatemap.type =
1533 cpu_to_le16(LOG_ALLOCPXD);
1535 lrd->log.updatemap.type =
1536 cpu_to_le16(LOG_FREEPXD);
1537 lrd->log.updatemap.nxd = cpu_to_le16(1);
1538 lrd->log.updatemap.pxd = pxdlock->pxd;
1540 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1544 tlck->flag |= tlckUPDATEMAP;
1546 #endif /* _JFS_WIP */
1555 * function: log data tlock
1557 static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1558 struct tlock * tlck)
1560 struct metapage *mp;
1565 /* initialize as REDOPAGE record format */
1566 lrd->log.redopage.type = cpu_to_le16(LOG_DATA);
1567 lrd->log.redopage.l2linesize = cpu_to_le16(L2DATASLOTSIZE);
1569 pxd = &lrd->log.redopage.pxd;
1571 /* log after-image for logredo(): */
1572 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1574 if (jfs_dirtable_inline(tlck->ip)) {
1576 * The table has been truncated, we've must have deleted
1577 * the last entry, so don't bother logging this
1581 metapage_homeok(mp);
1582 discard_metapage(mp);
1587 PXDaddress(pxd, mp->index);
1588 PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
1590 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1592 /* mark page as homeward bound */
1593 tlck->flag |= tlckWRITEPAGE;
1602 * function: log dtree tlock and format maplock to update bmap;
1604 static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1605 struct tlock * tlck)
1607 struct metapage *mp;
1608 struct pxd_lock *pxdlock;
1613 /* initialize as REDOPAGE/NOREDOPAGE record format */
1614 lrd->log.redopage.type = cpu_to_le16(LOG_DTREE);
1615 lrd->log.redopage.l2linesize = cpu_to_le16(L2DTSLOTSIZE);
1617 pxd = &lrd->log.redopage.pxd;
1619 if (tlck->type & tlckBTROOT)
1620 lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
1623 * page extension via relocation: entry insertion;
1624 * page extension in-place: entry insertion;
1625 * new right page from page split, reinitialized in-line
1626 * root from root page split: entry insertion;
1628 if (tlck->type & (tlckNEW | tlckEXTEND)) {
1629 /* log after-image of the new page for logredo():
1630 * mark log (LOG_NEW) for logredo() to initialize
1631 * freelist and update bmap for alloc of the new page;
1633 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1634 if (tlck->type & tlckEXTEND)
1635 lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
1637 lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
1638 // *pxd = mp->cm_pxd;
1639 PXDaddress(pxd, mp->index);
1641 mp->logical_size >> tblk->sb->s_blocksize_bits);
1642 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1644 /* format a maplock for txUpdateMap() to update bPMAP for
1645 * alloc of the new page;
1647 if (tlck->type & tlckBTROOT)
1649 tlck->flag |= tlckUPDATEMAP;
1650 pxdlock = (struct pxd_lock *) & tlck->lock;
1651 pxdlock->flag = mlckALLOCPXD;
1652 pxdlock->pxd = *pxd;
1656 /* mark page as homeward bound */
1657 tlck->flag |= tlckWRITEPAGE;
1662 * entry insertion/deletion,
1663 * sibling page link update (old right page before split);
1665 if (tlck->type & (tlckENTRY | tlckRELINK)) {
1666 /* log after-image for logredo(): */
1667 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1668 PXDaddress(pxd, mp->index);
1670 mp->logical_size >> tblk->sb->s_blocksize_bits);
1671 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1673 /* mark page as homeward bound */
1674 tlck->flag |= tlckWRITEPAGE;
1679 * page deletion: page has been invalidated
1680 * page relocation: source extent
1682 * a maplock for free of the page has been formatted
1683 * at txLock() time);
1685 if (tlck->type & (tlckFREE | tlckRELOCATE)) {
1686 /* log LOG_NOREDOPAGE of the deleted page for logredo()
1687 * to start NoRedoPage filter and to update bmap for free
1688 * of the deletd page
1690 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
1691 pxdlock = (struct pxd_lock *) & tlck->lock;
1692 *pxd = pxdlock->pxd;
1693 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1695 /* a maplock for txUpdateMap() for free of the page
1696 * has been formatted at txLock() time;
1698 tlck->flag |= tlckUPDATEMAP;
1707 * function: log xtree tlock and format maplock to update bmap;
1709 static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1710 struct tlock * tlck)
1713 struct metapage *mp;
1715 struct xtlock *xtlck;
1716 struct maplock *maplock;
1717 struct xdlistlock *xadlock;
1718 struct pxd_lock *pxdlock;
1725 /* initialize as REDOPAGE/NOREDOPAGE record format */
1726 lrd->log.redopage.type = cpu_to_le16(LOG_XTREE);
1727 lrd->log.redopage.l2linesize = cpu_to_le16(L2XTSLOTSIZE);
1729 page_pxd = &lrd->log.redopage.pxd;
1731 if (tlck->type & tlckBTROOT) {
1732 lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
1733 p = &JFS_IP(ip)->i_xtroot;
1734 if (S_ISDIR(ip->i_mode))
1735 lrd->log.redopage.type |=
1736 cpu_to_le16(LOG_DIR_XTREE);
1738 p = (xtpage_t *) mp->data;
1739 next = le16_to_cpu(p->header.nextindex);
1741 xtlck = (struct xtlock *) & tlck->lock;
1743 maplock = (struct maplock *) & tlck->lock;
1744 xadlock = (struct xdlistlock *) maplock;
1747 * entry insertion/extension;
1748 * sibling page link update (old right page before split);
1750 if (tlck->type & (tlckNEW | tlckGROW | tlckRELINK)) {
1751 /* log after-image for logredo():
1752 * logredo() will update bmap for alloc of new/extended
1753 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1754 * after-image of XADlist;
1755 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1756 * applying the after-image to the meta-data page.
1758 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1759 // *page_pxd = mp->cm_pxd;
1760 PXDaddress(page_pxd, mp->index);
1762 mp->logical_size >> tblk->sb->s_blocksize_bits);
1763 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1765 /* format a maplock for txUpdateMap() to update bPMAP
1766 * for alloc of new/extended extents of XAD[lwm:next)
1767 * from the page itself;
1768 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
1770 lwm = xtlck->lwm.offset;
1772 lwm = XTPAGEMAXSLOT;
1777 jfs_err("xtLog: lwm > next\n");
1780 tlck->flag |= tlckUPDATEMAP;
1781 xadlock->flag = mlckALLOCXADLIST;
1782 xadlock->count = next - lwm;
1783 if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
1787 * Lazy commit may allow xtree to be modified before
1788 * txUpdateMap runs. Copy xad into linelock to
1789 * preserve correct data.
1791 * We can fit twice as may pxd's as xads in the lock
1793 xadlock->flag = mlckALLOCPXDLIST;
1794 pxd = xadlock->xdlist = &xtlck->pxdlock;
1795 for (i = 0; i < xadlock->count; i++) {
1796 PXDaddress(pxd, addressXAD(&p->xad[lwm + i]));
1797 PXDlength(pxd, lengthXAD(&p->xad[lwm + i]));
1798 p->xad[lwm + i].flag &=
1799 ~(XAD_NEW | XAD_EXTENDED);
1804 * xdlist will point to into inode's xtree, ensure
1805 * that transaction is not committed lazily.
1807 xadlock->flag = mlckALLOCXADLIST;
1808 xadlock->xdlist = &p->xad[lwm];
1809 tblk->xflag &= ~COMMIT_LAZY;
1811 jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d "
1812 "count:%d", tlck->ip, mp, tlck, lwm, xadlock->count);
1817 /* mark page as homeward bound */
1818 tlck->flag |= tlckWRITEPAGE;
1824 * page deletion: file deletion/truncation (ref. xtTruncate())
1826 * (page will be invalidated after log is written and bmap
1827 * is updated from the page);
1829 if (tlck->type & tlckFREE) {
1830 /* LOG_NOREDOPAGE log for NoRedoPage filter:
1831 * if page free from file delete, NoRedoFile filter from
1832 * inode image of zero link count will subsume NoRedoPage
1833 * filters for each page;
1834 * if page free from file truncattion, write NoRedoPage
1837 * upadte of block allocation map for the page itself:
1838 * if page free from deletion and truncation, LOG_UPDATEMAP
1839 * log for the page itself is generated from processing
1840 * its parent page xad entries;
1842 /* if page free from file truncation, log LOG_NOREDOPAGE
1843 * of the deleted page for logredo() to start NoRedoPage
1844 * filter for the page;
1846 if (tblk->xflag & COMMIT_TRUNCATE) {
1847 /* write NOREDOPAGE for the page */
1848 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
1849 PXDaddress(page_pxd, mp->index);
1851 mp->logical_size >> tblk->sb->
1854 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1856 if (tlck->type & tlckBTROOT) {
1857 /* Empty xtree must be logged */
1858 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1860 cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1864 /* init LOG_UPDATEMAP of the freed extents
1865 * XAD[XTENTRYSTART:hwm) from the deleted page itself
1866 * for logredo() to update bmap;
1868 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1869 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEXADLIST);
1870 xtlck = (struct xtlock *) & tlck->lock;
1871 hwm = xtlck->hwm.offset;
1872 lrd->log.updatemap.nxd =
1873 cpu_to_le16(hwm - XTENTRYSTART + 1);
1874 /* reformat linelock for lmLog() */
1875 xtlck->header.offset = XTENTRYSTART;
1876 xtlck->header.length = hwm - XTENTRYSTART + 1;
1878 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1880 /* format a maplock for txUpdateMap() to update bmap
1881 * to free extents of XAD[XTENTRYSTART:hwm) from the
1882 * deleted page itself;
1884 tlck->flag |= tlckUPDATEMAP;
1885 xadlock->count = hwm - XTENTRYSTART + 1;
1886 if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
1890 * Lazy commit may allow xtree to be modified before
1891 * txUpdateMap runs. Copy xad into linelock to
1892 * preserve correct data.
1894 * We can fit twice as may pxd's as xads in the lock
1896 xadlock->flag = mlckFREEPXDLIST;
1897 pxd = xadlock->xdlist = &xtlck->pxdlock;
1898 for (i = 0; i < xadlock->count; i++) {
1900 addressXAD(&p->xad[XTENTRYSTART + i]));
1902 lengthXAD(&p->xad[XTENTRYSTART + i]));
1907 * xdlist will point to into inode's xtree, ensure
1908 * that transaction is not committed lazily.
1910 xadlock->flag = mlckFREEXADLIST;
1911 xadlock->xdlist = &p->xad[XTENTRYSTART];
1912 tblk->xflag &= ~COMMIT_LAZY;
1914 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
1915 tlck->ip, mp, xadlock->count);
1919 /* mark page as invalid */
1920 if (((tblk->xflag & COMMIT_PWMAP) || S_ISDIR(ip->i_mode))
1921 && !(tlck->type & tlckBTROOT))
1922 tlck->flag |= tlckFREEPAGE;
1924 else (tblk->xflag & COMMIT_PMAP)
1931 * page/entry truncation: file truncation (ref. xtTruncate())
1933 * |----------+------+------+---------------|
1935 * | | hwm - hwm before truncation
1936 * | next - truncation point
1937 * lwm - lwm before truncation
1940 if (tlck->type & tlckTRUNCATE) {
1941 pxd_t pxd; /* truncated extent of xad */
1945 * For truncation the entire linelock may be used, so it would
1946 * be difficult to store xad list in linelock itself.
1947 * Therefore, we'll just force transaction to be committed
1948 * synchronously, so that xtree pages won't be changed before
1951 tblk->xflag &= ~COMMIT_LAZY;
1952 lwm = xtlck->lwm.offset;
1954 lwm = XTPAGEMAXSLOT;
1955 hwm = xtlck->hwm.offset;
1956 twm = xtlck->twm.offset;
1961 /* log after-image for logredo():
1963 * logredo() will update bmap for alloc of new/extended
1964 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1965 * after-image of XADlist;
1966 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1967 * applying the after-image to the meta-data page.
1969 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1970 PXDaddress(page_pxd, mp->index);
1972 mp->logical_size >> tblk->sb->s_blocksize_bits);
1973 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1976 * truncate entry XAD[twm == next - 1]:
1978 if (twm == next - 1) {
1979 /* init LOG_UPDATEMAP for logredo() to update bmap for
1980 * free of truncated delta extent of the truncated
1981 * entry XAD[next - 1]:
1982 * (xtlck->pxdlock = truncated delta extent);
1984 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
1985 /* assert(pxdlock->type & tlckTRUNCATE); */
1986 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1987 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
1988 lrd->log.updatemap.nxd = cpu_to_le16(1);
1989 lrd->log.updatemap.pxd = pxdlock->pxd;
1990 pxd = pxdlock->pxd; /* save to format maplock */
1992 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1996 * free entries XAD[next:hwm]:
1999 /* init LOG_UPDATEMAP of the freed extents
2000 * XAD[next:hwm] from the deleted page itself
2001 * for logredo() to update bmap;
2003 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2004 lrd->log.updatemap.type =
2005 cpu_to_le16(LOG_FREEXADLIST);
2006 xtlck = (struct xtlock *) & tlck->lock;
2007 hwm = xtlck->hwm.offset;
2008 lrd->log.updatemap.nxd =
2009 cpu_to_le16(hwm - next + 1);
2010 /* reformat linelock for lmLog() */
2011 xtlck->header.offset = next;
2012 xtlck->header.length = hwm - next + 1;
2015 cpu_to_le32(lmLog(log, tblk, lrd, tlck));
2019 * format maplock(s) for txUpdateMap() to update bmap
2024 * allocate entries XAD[lwm:next):
2027 /* format a maplock for txUpdateMap() to update bPMAP
2028 * for alloc of new/extended extents of XAD[lwm:next)
2029 * from the page itself;
2030 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
2032 tlck->flag |= tlckUPDATEMAP;
2033 xadlock->flag = mlckALLOCXADLIST;
2034 xadlock->count = next - lwm;
2035 xadlock->xdlist = &p->xad[lwm];
2037 jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d "
2039 tlck->ip, mp, xadlock->count, lwm, next);
2045 * truncate entry XAD[twm == next - 1]:
2047 if (twm == next - 1) {
2048 struct pxd_lock *pxdlock;
2050 /* format a maplock for txUpdateMap() to update bmap
2051 * to free truncated delta extent of the truncated
2052 * entry XAD[next - 1];
2053 * (xtlck->pxdlock = truncated delta extent);
2055 tlck->flag |= tlckUPDATEMAP;
2056 pxdlock = (struct pxd_lock *) xadlock;
2057 pxdlock->flag = mlckFREEPXD;
2061 jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d "
2062 "hwm:%d", ip, mp, pxdlock->count, hwm);
2068 * free entries XAD[next:hwm]:
2071 /* format a maplock for txUpdateMap() to update bmap
2072 * to free extents of XAD[next:hwm] from thedeleted
2075 tlck->flag |= tlckUPDATEMAP;
2076 xadlock->flag = mlckFREEXADLIST;
2077 xadlock->count = hwm - next + 1;
2078 xadlock->xdlist = &p->xad[next];
2080 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d "
2082 tlck->ip, mp, xadlock->count, next, hwm);
2086 /* mark page as homeward bound */
2087 tlck->flag |= tlckWRITEPAGE;
2096 * function: log from maplock of freed data extents;
2098 void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
2099 struct tlock * tlck)
2101 struct pxd_lock *pxdlock;
2106 * page relocation: free the source page extent
2108 * a maplock for txUpdateMap() for free of the page
2109 * has been formatted at txLock() time saving the src
2110 * relocated page address;
2112 if (tlck->type & tlckRELOCATE) {
2113 /* log LOG_NOREDOPAGE of the old relocated page
2114 * for logredo() to start NoRedoPage filter;
2116 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
2117 pxdlock = (struct pxd_lock *) & tlck->lock;
2118 pxd = &lrd->log.redopage.pxd;
2119 *pxd = pxdlock->pxd;
2120 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2122 /* (N.B. currently, logredo() does NOT update bmap
2123 * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
2124 * if page free from relocation, LOG_UPDATEMAP log is
2125 * specifically generated now for logredo()
2126 * to update bmap for free of src relocated page;
2127 * (new flag LOG_RELOCATE may be introduced which will
2128 * inform logredo() to start NORedoPage filter and also
2129 * update block allocation map at the same time, thus
2130 * avoiding an extra log write);
2132 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2133 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
2134 lrd->log.updatemap.nxd = cpu_to_le16(1);
2135 lrd->log.updatemap.pxd = pxdlock->pxd;
2136 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2138 /* a maplock for txUpdateMap() for free of the page
2139 * has been formatted at txLock() time;
2141 tlck->flag |= tlckUPDATEMAP;
2146 * Otherwise it's not a relocate request
2150 /* log LOG_UPDATEMAP for logredo() to update bmap for
2151 * free of truncated/relocated delta extent of the data;
2152 * e.g.: external EA extent, relocated/truncated extent
2153 * from xtTailgate();
2155 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2156 pxdlock = (struct pxd_lock *) & tlck->lock;
2157 nlock = pxdlock->index;
2158 for (i = 0; i < nlock; i++, pxdlock++) {
2159 if (pxdlock->flag & mlckALLOCPXD)
2160 lrd->log.updatemap.type =
2161 cpu_to_le16(LOG_ALLOCPXD);
2163 lrd->log.updatemap.type =
2164 cpu_to_le16(LOG_FREEPXD);
2165 lrd->log.updatemap.nxd = cpu_to_le16(1);
2166 lrd->log.updatemap.pxd = pxdlock->pxd;
2168 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2169 jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
2170 (ulong) addressPXD(&pxdlock->pxd),
2171 lengthPXD(&pxdlock->pxd));
2175 tlck->flag |= tlckUPDATEMAP;
2183 * function: acquire maplock for EA/ACL extents or
2184 * set COMMIT_INLINE flag;
2186 void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
2188 struct tlock *tlck = NULL;
2189 struct pxd_lock *maplock = NULL, *pxdlock = NULL;
2192 * format maplock for alloc of new EA extent
2195 /* Since the newea could be a completely zeroed entry we need to
2196 * check for the two flags which indicate we should actually
2197 * commit new EA data
2199 if (newea->flag & DXD_EXTENT) {
2200 tlck = txMaplock(tid, ip, tlckMAP);
2201 maplock = (struct pxd_lock *) & tlck->lock;
2202 pxdlock = (struct pxd_lock *) maplock;
2203 pxdlock->flag = mlckALLOCPXD;
2204 PXDaddress(&pxdlock->pxd, addressDXD(newea));
2205 PXDlength(&pxdlock->pxd, lengthDXD(newea));
2208 } else if (newea->flag & DXD_INLINE) {
2211 set_cflag(COMMIT_Inlineea, ip);
2216 * format maplock for free of old EA extent
2218 if (!test_cflag(COMMIT_Nolink, ip) && oldea->flag & DXD_EXTENT) {
2220 tlck = txMaplock(tid, ip, tlckMAP);
2221 maplock = (struct pxd_lock *) & tlck->lock;
2222 pxdlock = (struct pxd_lock *) maplock;
2225 pxdlock->flag = mlckFREEPXD;
2226 PXDaddress(&pxdlock->pxd, addressDXD(oldea));
2227 PXDlength(&pxdlock->pxd, lengthDXD(oldea));
2236 * function: synchronously write pages locked by transaction
2237 * after txLog() but before txUpdateMap();
2239 void txForce(struct tblock * tblk)
2243 struct metapage *mp;
2246 * reverse the order of transaction tlocks in
2247 * careful update order of address index pages
2248 * (right to left, bottom up)
2250 tlck = lid_to_tlock(tblk->next);
2254 tlck = lid_to_tlock(lid);
2256 tlck->next = tblk->next;
2262 * synchronously write the page, and
2263 * hold the page for txUpdateMap();
2265 for (lid = tblk->next; lid; lid = next) {
2266 tlck = lid_to_tlock(lid);
2269 if ((mp = tlck->mp) != NULL &&
2270 (tlck->type & tlckBTROOT) == 0) {
2271 assert(mp->xflag & COMMIT_PAGE);
2273 if (tlck->flag & tlckWRITEPAGE) {
2274 tlck->flag &= ~tlckWRITEPAGE;
2276 /* do not release page to freelist */
2280 * The "right" thing to do here is to
2281 * synchronously write the metadata.
2282 * With the current implementation this
2283 * is hard since write_metapage requires
2284 * us to kunmap & remap the page. If we
2285 * have tlocks pointing into the metadata
2286 * pages, we don't want to do this. I think
2287 * we can get by with synchronously writing
2288 * the pages when they are released.
2290 assert(mp->nohomeok);
2291 set_bit(META_dirty, &mp->flag);
2292 set_bit(META_sync, &mp->flag);
2303 * function: update persistent allocation map (and working map
2308 static void txUpdateMap(struct tblock * tblk)
2311 struct inode *ipimap;
2314 struct maplock *maplock;
2315 struct pxd_lock pxdlock;
2318 struct metapage *mp = NULL;
2320 ipimap = JFS_SBI(tblk->sb)->ipimap;
2322 maptype = (tblk->xflag & COMMIT_PMAP) ? COMMIT_PMAP : COMMIT_PWMAP;
2326 * update block allocation map
2328 * update allocation state in pmap (and wmap) and
2329 * update lsn of the pmap page;
2332 * scan each tlock/page of transaction for block allocation/free:
2334 * for each tlock/page of transaction, update map.
2335 * ? are there tlock for pmap and pwmap at the same time ?
2337 for (lid = tblk->next; lid; lid = tlck->next) {
2338 tlck = lid_to_tlock(lid);
2340 if ((tlck->flag & tlckUPDATEMAP) == 0)
2343 if (tlck->flag & tlckFREEPAGE) {
2345 * Another thread may attempt to reuse freed space
2346 * immediately, so we want to get rid of the metapage
2347 * before anyone else has a chance to get it.
2348 * Lock metapage, update maps, then invalidate
2352 ASSERT(mp->xflag & COMMIT_PAGE);
2358 * . in-line PXD list:
2359 * . out-of-line XAD list:
2361 maplock = (struct maplock *) & tlck->lock;
2362 nlock = maplock->index;
2364 for (k = 0; k < nlock; k++, maplock++) {
2366 * allocate blocks in persistent map:
2368 * blocks have been allocated from wmap at alloc time;
2370 if (maplock->flag & mlckALLOC) {
2371 txAllocPMap(ipimap, maplock, tblk);
2374 * free blocks in persistent and working map:
2375 * blocks will be freed in pmap and then in wmap;
2377 * ? tblock specifies the PMAP/PWMAP based upon
2380 * free blocks in persistent map:
2381 * blocks will be freed from wmap at last reference
2382 * release of the object for regular files;
2384 * Alway free blocks from both persistent & working
2385 * maps for directories
2387 else { /* (maplock->flag & mlckFREE) */
2389 if (S_ISDIR(tlck->ip->i_mode))
2390 txFreeMap(ipimap, maplock,
2391 tblk, COMMIT_PWMAP);
2393 txFreeMap(ipimap, maplock,
2397 if (tlck->flag & tlckFREEPAGE) {
2398 if (!(tblk->flag & tblkGC_LAZY)) {
2399 /* This is equivalent to txRelease */
2400 ASSERT(mp->lid == lid);
2403 assert(mp->nohomeok == 1);
2404 metapage_homeok(mp);
2405 discard_metapage(mp);
2410 * update inode allocation map
2412 * update allocation state in pmap and
2413 * update lsn of the pmap page;
2414 * update in-memory inode flag/state
2416 * unlock mapper/write lock
2418 if (tblk->xflag & COMMIT_CREATE) {
2419 diUpdatePMap(ipimap, tblk->ino, FALSE, tblk);
2420 ipimap->i_state |= I_DIRTY;
2421 /* update persistent block allocation map
2422 * for the allocation of inode extent;
2424 pxdlock.flag = mlckALLOCPXD;
2425 pxdlock.pxd = tblk->u.ixpxd;
2427 txAllocPMap(ipimap, (struct maplock *) & pxdlock, tblk);
2428 } else if (tblk->xflag & COMMIT_DELETE) {
2430 diUpdatePMap(ipimap, ip->i_ino, TRUE, tblk);
2431 ipimap->i_state |= I_DIRTY;
2440 * function: allocate from persistent map;
2449 * allocate from persistent map;
2450 * free from persistent map;
2451 * (e.g., tmp file - free from working map at releae
2452 * of last reference);
2453 * free from persistent and working map;
2455 * lsn - log sequence number;
2457 static void txAllocPMap(struct inode *ip, struct maplock * maplock,
2458 struct tblock * tblk)
2460 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
2461 struct xdlistlock *xadlistlock;
2465 struct pxd_lock *pxdlock;
2466 struct xdlistlock *pxdlistlock;
2471 * allocate from persistent map;
2473 if (maplock->flag & mlckALLOCXADLIST) {
2474 xadlistlock = (struct xdlistlock *) maplock;
2475 xad = xadlistlock->xdlist;
2476 for (n = 0; n < xadlistlock->count; n++, xad++) {
2477 if (xad->flag & (XAD_NEW | XAD_EXTENDED)) {
2478 xaddr = addressXAD(xad);
2479 xlen = lengthXAD(xad);
2480 dbUpdatePMap(ipbmap, FALSE, xaddr,
2482 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
2483 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2484 (ulong) xaddr, xlen);
2487 } else if (maplock->flag & mlckALLOCPXD) {
2488 pxdlock = (struct pxd_lock *) maplock;
2489 xaddr = addressPXD(&pxdlock->pxd);
2490 xlen = lengthPXD(&pxdlock->pxd);
2491 dbUpdatePMap(ipbmap, FALSE, xaddr, (s64) xlen, tblk);
2492 jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong) xaddr, xlen);
2493 } else { /* (maplock->flag & mlckALLOCPXDLIST) */
2495 pxdlistlock = (struct xdlistlock *) maplock;
2496 pxd = pxdlistlock->xdlist;
2497 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2498 xaddr = addressPXD(pxd);
2499 xlen = lengthPXD(pxd);
2500 dbUpdatePMap(ipbmap, FALSE, xaddr, (s64) xlen,
2502 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2503 (ulong) xaddr, xlen);
2512 * function: free from persistent and/or working map;
2514 * todo: optimization
2516 void txFreeMap(struct inode *ip,
2517 struct maplock * maplock, struct tblock * tblk, int maptype)
2519 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
2520 struct xdlistlock *xadlistlock;
2524 struct pxd_lock *pxdlock;
2525 struct xdlistlock *pxdlistlock;
2529 jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
2530 tblk, maplock, maptype);
2533 * free from persistent map;
2535 if (maptype == COMMIT_PMAP || maptype == COMMIT_PWMAP) {
2536 if (maplock->flag & mlckFREEXADLIST) {
2537 xadlistlock = (struct xdlistlock *) maplock;
2538 xad = xadlistlock->xdlist;
2539 for (n = 0; n < xadlistlock->count; n++, xad++) {
2540 if (!(xad->flag & XAD_NEW)) {
2541 xaddr = addressXAD(xad);
2542 xlen = lengthXAD(xad);
2543 dbUpdatePMap(ipbmap, TRUE, xaddr,
2545 jfs_info("freePMap: xaddr:0x%lx "
2547 (ulong) xaddr, xlen);
2550 } else if (maplock->flag & mlckFREEPXD) {
2551 pxdlock = (struct pxd_lock *) maplock;
2552 xaddr = addressPXD(&pxdlock->pxd);
2553 xlen = lengthPXD(&pxdlock->pxd);
2554 dbUpdatePMap(ipbmap, TRUE, xaddr, (s64) xlen,
2556 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2557 (ulong) xaddr, xlen);
2558 } else { /* (maplock->flag & mlckALLOCPXDLIST) */
2560 pxdlistlock = (struct xdlistlock *) maplock;
2561 pxd = pxdlistlock->xdlist;
2562 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2563 xaddr = addressPXD(pxd);
2564 xlen = lengthPXD(pxd);
2565 dbUpdatePMap(ipbmap, TRUE, xaddr,
2567 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2568 (ulong) xaddr, xlen);
2574 * free from working map;
2576 if (maptype == COMMIT_PWMAP || maptype == COMMIT_WMAP) {
2577 if (maplock->flag & mlckFREEXADLIST) {
2578 xadlistlock = (struct xdlistlock *) maplock;
2579 xad = xadlistlock->xdlist;
2580 for (n = 0; n < xadlistlock->count; n++, xad++) {
2581 xaddr = addressXAD(xad);
2582 xlen = lengthXAD(xad);
2583 dbFree(ip, xaddr, (s64) xlen);
2585 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2586 (ulong) xaddr, xlen);
2588 } else if (maplock->flag & mlckFREEPXD) {
2589 pxdlock = (struct pxd_lock *) maplock;
2590 xaddr = addressPXD(&pxdlock->pxd);
2591 xlen = lengthPXD(&pxdlock->pxd);
2592 dbFree(ip, xaddr, (s64) xlen);
2593 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2594 (ulong) xaddr, xlen);
2595 } else { /* (maplock->flag & mlckFREEPXDLIST) */
2597 pxdlistlock = (struct xdlistlock *) maplock;
2598 pxd = pxdlistlock->xdlist;
2599 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2600 xaddr = addressPXD(pxd);
2601 xlen = lengthPXD(pxd);
2602 dbFree(ip, xaddr, (s64) xlen);
2603 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2604 (ulong) xaddr, xlen);
2614 * function: remove tlock from inode anonymous locklist
2616 void txFreelock(struct inode *ip)
2618 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2619 struct tlock *xtlck, *tlck;
2620 lid_t xlid = 0, lid;
2622 if (!jfs_ip->atlhead)
2626 xtlck = (struct tlock *) &jfs_ip->atlhead;
2628 while ((lid = xtlck->next) != 0) {
2629 tlck = lid_to_tlock(lid);
2630 if (tlck->flag & tlckFREELOCK) {
2631 xtlck->next = tlck->next;
2639 if (jfs_ip->atlhead)
2640 jfs_ip->atltail = xlid;
2642 jfs_ip->atltail = 0;
2644 * If inode was on anon_list, remove it
2646 list_del_init(&jfs_ip->anon_inode_list);
2655 * function: abort tx before commit;
2657 * frees line-locks and segment locks for all
2658 * segments in comdata structure.
2659 * Optionally sets state of file-system to FM_DIRTY in super-block.
2660 * log age of page-frames in memory for which caller has
2661 * are reset to 0 (to avoid logwarap).
2663 void txAbort(tid_t tid, int dirty)
2666 struct metapage *mp;
2667 struct tblock *tblk = tid_to_tblock(tid);
2671 * free tlocks of the transaction
2673 for (lid = tblk->next; lid; lid = next) {
2674 tlck = lid_to_tlock(lid);
2677 JFS_IP(tlck->ip)->xtlid = 0;
2683 * reset lsn of page to avoid logwarap:
2685 * (page may have been previously committed by another
2686 * transaction(s) but has not been paged, i.e.,
2687 * it may be on logsync list even though it has not
2688 * been logged for the current tx.)
2690 if (mp->xflag & COMMIT_PAGE && mp->lsn)
2693 /* insert tlock at head of freelist */
2699 /* caller will free the transaction block */
2701 tblk->next = tblk->last = 0;
2704 * mark filesystem dirty
2707 jfs_error(tblk->sb, "txAbort");
2713 * txLazyCommit(void)
2715 * All transactions except those changing ipimap (COMMIT_FORCE) are
2716 * processed by this routine. This insures that the inode and block
2717 * allocation maps are updated in order. For synchronous transactions,
2718 * let the user thread finish processing after txUpdateMap() is called.
2720 static void txLazyCommit(struct tblock * tblk)
2722 struct jfs_log *log;
2724 while (((tblk->flag & tblkGC_READY) == 0) &&
2725 ((tblk->flag & tblkGC_UNLOCKED) == 0)) {
2726 /* We must have gotten ahead of the user thread
2728 jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk);
2732 jfs_info("txLazyCommit: processing tblk 0x%p", tblk);
2736 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
2738 spin_lock_irq(&log->gclock); // LOGGC_LOCK
2740 tblk->flag |= tblkGC_COMMITTED;
2742 if (tblk->flag & tblkGC_READY)
2745 wake_up_all(&tblk->gcwait); // LOGGC_WAKEUP
2748 * Can't release log->gclock until we've tested tblk->flag
2750 if (tblk->flag & tblkGC_LAZY) {
2751 spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
2753 tblk->flag &= ~tblkGC_LAZY;
2754 txEnd(tblk - TxBlock); /* Convert back to tid */
2756 spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
2758 jfs_info("txLazyCommit: done: tblk = 0x%p", tblk);
2762 * jfs_lazycommit(void)
2764 * To be run as a kernel daemon. If lbmIODone is called in an interrupt
2765 * context, or where blocking is not wanted, this routine will process
2766 * committed transactions from the unlock queue.
2768 int jfs_lazycommit(void *arg)
2771 struct tblock *tblk;
2772 unsigned long flags;
2773 struct jfs_sb_info *sbi;
2775 daemonize("jfsCommit");
2777 complete(&jfsIOwait);
2781 jfs_commit_thread_waking = 0; /* OK to wake another thread */
2782 while (!list_empty(&TxAnchor.unlock_queue)) {
2784 list_for_each_entry(tblk, &TxAnchor.unlock_queue,
2787 sbi = JFS_SBI(tblk->sb);
2789 * For each volume, the transactions must be
2790 * handled in order. If another commit thread
2791 * is handling a tblk for this superblock,
2794 if (sbi->commit_state & IN_LAZYCOMMIT)
2797 sbi->commit_state |= IN_LAZYCOMMIT;
2801 * Remove transaction from queue
2803 list_del(&tblk->cqueue);
2809 sbi->commit_state &= ~IN_LAZYCOMMIT;
2811 * Don't continue in the for loop. (We can't
2812 * anyway, it's unsafe!) We want to go back to
2813 * the beginning of the list.
2818 /* If there was nothing to do, don't continue */
2822 /* In case a wakeup came while all threads were active */
2823 jfs_commit_thread_waking = 0;
2825 if (current->flags & PF_FREEZE) {
2827 refrigerator(PF_FREEZE);
2829 DECLARE_WAITQUEUE(wq, current);
2831 add_wait_queue(&jfs_commit_thread_wait, &wq);
2832 set_current_state(TASK_INTERRUPTIBLE);
2835 current->state = TASK_RUNNING;
2836 remove_wait_queue(&jfs_commit_thread_wait, &wq);
2838 } while (!jfs_stop_threads);
2840 if (!list_empty(&TxAnchor.unlock_queue))
2841 jfs_err("jfs_lazycommit being killed w/pending transactions!");
2843 jfs_info("jfs_lazycommit being killed\n");
2844 complete_and_exit(&jfsIOwait, 0);
2847 void txLazyUnlock(struct tblock * tblk)
2849 unsigned long flags;
2853 list_add_tail(&tblk->cqueue, &TxAnchor.unlock_queue);
2855 * Don't wake up a commit thread if there is already one servicing
2856 * this superblock, or if the last one we woke up hasn't started yet.
2858 if (!(JFS_SBI(tblk->sb)->commit_state & IN_LAZYCOMMIT) &&
2859 !jfs_commit_thread_waking) {
2860 jfs_commit_thread_waking = 1;
2861 wake_up(&jfs_commit_thread_wait);
2866 static void LogSyncRelease(struct metapage * mp)
2868 struct jfs_log *log = mp->log;
2870 assert(mp->nohomeok);
2872 metapage_homeok(mp);
2878 * Block all new transactions and push anonymous transactions to
2881 * This does almost the same thing as jfs_sync below. We don't
2882 * worry about deadlocking when jfs_tlocks_low is set, since we would
2883 * expect jfs_sync to get us out of that jam.
2885 void txQuiesce(struct super_block *sb)
2888 struct jfs_inode_info *jfs_ip;
2889 struct jfs_log *log = JFS_SBI(sb)->log;
2892 set_bit(log_QUIESCE, &log->flag);
2896 while (!list_empty(&TxAnchor.anon_list)) {
2897 jfs_ip = list_entry(TxAnchor.anon_list.next,
2898 struct jfs_inode_info,
2900 ip = &jfs_ip->vfs_inode;
2903 * inode will be removed from anonymous list
2904 * when it is committed
2907 tid = txBegin(ip->i_sb, COMMIT_INODE | COMMIT_FORCE);
2908 down(&jfs_ip->commit_sem);
2909 txCommit(tid, 1, &ip, 0);
2911 up(&jfs_ip->commit_sem);
2913 * Just to be safe. I don't know how
2914 * long we can run without blocking
2921 * If jfs_sync is running in parallel, there could be some inodes
2922 * on anon_list2. Let's check.
2924 if (!list_empty(&TxAnchor.anon_list2)) {
2925 list_splice(&TxAnchor.anon_list2, &TxAnchor.anon_list);
2926 INIT_LIST_HEAD(&TxAnchor.anon_list2);
2932 * We may need to kick off the group commit
2934 jfs_flush_journal(log, 0);
2940 * Allows transactions to start again following txQuiesce
2942 void txResume(struct super_block *sb)
2944 struct jfs_log *log = JFS_SBI(sb)->log;
2946 clear_bit(log_QUIESCE, &log->flag);
2947 TXN_WAKEUP(&log->syncwait);
2953 * To be run as a kernel daemon. This is awakened when tlocks run low.
2954 * We write any inodes that have anonymous tlocks so they will become
2957 int jfs_sync(void *arg)
2960 struct jfs_inode_info *jfs_ip;
2964 daemonize("jfsSync");
2966 complete(&jfsIOwait);
2970 * write each inode on the anonymous inode list
2973 while (jfs_tlocks_low && !list_empty(&TxAnchor.anon_list)) {
2974 jfs_ip = list_entry(TxAnchor.anon_list.next,
2975 struct jfs_inode_info,
2977 ip = &jfs_ip->vfs_inode;
2981 * Inode is being freed
2983 list_del_init(&jfs_ip->anon_inode_list);
2984 } else if (! down_trylock(&jfs_ip->commit_sem)) {
2986 * inode will be removed from anonymous list
2987 * when it is committed
2990 tid = txBegin(ip->i_sb, COMMIT_INODE);
2991 rc = txCommit(tid, 1, &ip, 0);
2993 up(&jfs_ip->commit_sem);
2997 * Just to be safe. I don't know how
2998 * long we can run without blocking
3003 /* We can't get the commit semaphore. It may
3004 * be held by a thread waiting for tlock's
3005 * so let's not block here. Save it to
3006 * put back on the anon_list.
3009 /* Take off anon_list */
3010 list_del(&jfs_ip->anon_inode_list);
3012 /* Put on anon_list2 */
3013 list_add(&jfs_ip->anon_inode_list,
3014 &TxAnchor.anon_list2);
3021 /* Add anon_list2 back to anon_list */
3022 list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
3024 if (current->flags & PF_FREEZE) {
3026 refrigerator(PF_FREEZE);
3028 DECLARE_WAITQUEUE(wq, current);
3030 add_wait_queue(&jfs_sync_thread_wait, &wq);
3031 set_current_state(TASK_INTERRUPTIBLE);
3034 current->state = TASK_RUNNING;
3035 remove_wait_queue(&jfs_sync_thread_wait, &wq);
3037 } while (!jfs_stop_threads);
3039 jfs_info("jfs_sync being killed");
3040 complete_and_exit(&jfsIOwait, 0);
3043 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
3044 int jfs_txanchor_read(char *buffer, char **start, off_t offset, int length,
3045 int *eof, void *data)
3054 waitqueue_active(&TxAnchor.freewait) ? "active" : "empty";
3056 waitqueue_active(&TxAnchor.freelockwait) ? "active" : "empty";
3058 waitqueue_active(&TxAnchor.lowlockwait) ? "active" : "empty";
3060 len += sprintf(buffer,
3066 "freelockwait = %s\n"
3067 "lowlockwait = %s\n"
3068 "tlocksInUse = %d\n"
3069 "jfs_tlocks_low = %d\n"
3070 "unlock_queue is %sempty\n",
3076 TxAnchor.tlocksInUse,
3078 list_empty(&TxAnchor.unlock_queue) ? "" : "not ");
3081 *start = buffer + begin;
3096 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
3097 int jfs_txstats_read(char *buffer, char **start, off_t offset, int length,
3098 int *eof, void *data)
3103 len += sprintf(buffer,
3106 "calls to txBegin = %d\n"
3107 "txBegin blocked by sync barrier = %d\n"
3108 "txBegin blocked by tlocks low = %d\n"
3109 "txBegin blocked by no free tid = %d\n"
3110 "calls to txBeginAnon = %d\n"
3111 "txBeginAnon blocked by sync barrier = %d\n"
3112 "txBeginAnon blocked by tlocks low = %d\n"
3113 "calls to txLockAlloc = %d\n"
3114 "tLockAlloc blocked by no free lock = %d\n",
3116 TxStat.txBegin_barrier,
3117 TxStat.txBegin_lockslow,
3118 TxStat.txBegin_freetid,
3120 TxStat.txBeginAnon_barrier,
3121 TxStat.txBeginAnon_lockslow,
3123 TxStat.txLockAlloc_freelock);
3126 *start = buffer + begin;