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
46 #include <linux/vmalloc.h>
47 #include <linux/completion.h>
48 #include <linux/freezer.h>
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/kthread.h>
52 #include <linux/seq_file.h>
53 #include "jfs_incore.h"
54 #include "jfs_inode.h"
55 #include "jfs_filsys.h"
56 #include "jfs_metapage.h"
57 #include "jfs_dinode.h"
60 #include "jfs_superblock.h"
61 #include "jfs_debug.h"
64 * transaction management structures
67 int freetid; /* index of a free tid structure */
68 int freelock; /* index first free lock word */
69 wait_queue_head_t freewait; /* eventlist of free tblock */
70 wait_queue_head_t freelockwait; /* eventlist of free tlock */
71 wait_queue_head_t lowlockwait; /* eventlist of ample tlocks */
72 int tlocksInUse; /* Number of tlocks in use */
73 spinlock_t LazyLock; /* synchronize sync_queue & unlock_queue */
74 /* struct tblock *sync_queue; * Transactions waiting for data sync */
75 struct list_head unlock_queue; /* Txns waiting to be released */
76 struct list_head anon_list; /* inodes having anonymous txns */
77 struct list_head anon_list2; /* inodes having anonymous txns
78 that couldn't be sync'ed */
81 int jfs_tlocks_low; /* Indicates low number of available tlocks */
83 #ifdef CONFIG_JFS_STATISTICS
87 uint txBegin_lockslow;
90 uint txBeginAnon_barrier;
91 uint txBeginAnon_lockslow;
93 uint txLockAlloc_freelock;
97 static int nTxBlock = -1; /* number of transaction blocks */
98 module_param(nTxBlock, int, 0);
99 MODULE_PARM_DESC(nTxBlock,
100 "Number of transaction blocks (max:65536)");
102 static int nTxLock = -1; /* number of transaction locks */
103 module_param(nTxLock, int, 0);
104 MODULE_PARM_DESC(nTxLock,
105 "Number of transaction locks (max:65536)");
107 struct tblock *TxBlock; /* transaction block table */
108 static int TxLockLWM; /* Low water mark for number of txLocks used */
109 static int TxLockHWM; /* High water mark for number of txLocks used */
110 static int TxLockVHWM; /* Very High water mark */
111 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 static DECLARE_WAIT_QUEUE_HEAD(jfs_commit_thread_wait);
126 static int jfs_commit_thread_waking;
129 * Retry logic exist outside these macros to protect from spurrious wakeups.
131 static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
133 DECLARE_WAITQUEUE(wait, current);
135 add_wait_queue(event, &wait);
136 set_current_state(TASK_UNINTERRUPTIBLE);
139 remove_wait_queue(event, &wait);
142 #define TXN_SLEEP(event)\
144 TXN_SLEEP_DROP_LOCK(event);\
148 #define TXN_WAKEUP(event) wake_up_all(event)
154 tid_t maxtid; /* 4: biggest tid ever used */
155 lid_t maxlid; /* 4: biggest lid ever used */
156 int ntid; /* 4: # of transactions performed */
157 int nlid; /* 4: # of tlocks acquired */
158 int waitlock; /* 4: # of tlock wait */
164 static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
165 struct tlock * tlck, struct commit * cd);
166 static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
167 struct tlock * tlck);
168 static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
169 struct tlock * tlck);
170 static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
171 struct tlock * tlck);
172 static void txAllocPMap(struct inode *ip, struct maplock * maplock,
173 struct tblock * tblk);
174 static void txForce(struct tblock * tblk);
175 static int txLog(struct jfs_log * log, struct tblock * tblk,
177 static void txUpdateMap(struct tblock * tblk);
178 static void txRelease(struct tblock * tblk);
179 static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
180 struct tlock * tlck);
181 static void LogSyncRelease(struct metapage * mp);
184 * transaction block/lock management
185 * ---------------------------------
189 * Get a transaction lock from the free list. If the number in use is
190 * greater than the high water mark, wake up the sync daemon. This should
191 * free some anonymous transaction locks. (TXN_LOCK must be held.)
193 static lid_t txLockAlloc(void)
197 INCREMENT(TxStat.txLockAlloc);
198 if (!TxAnchor.freelock) {
199 INCREMENT(TxStat.txLockAlloc_freelock);
202 while (!(lid = TxAnchor.freelock))
203 TXN_SLEEP(&TxAnchor.freelockwait);
204 TxAnchor.freelock = TxLock[lid].next;
205 HIGHWATERMARK(stattx.maxlid, lid);
206 if ((++TxAnchor.tlocksInUse > TxLockHWM) && (jfs_tlocks_low == 0)) {
207 jfs_info("txLockAlloc tlocks low");
209 wake_up_process(jfsSyncThread);
215 static void txLockFree(lid_t lid)
218 TxLock[lid].next = TxAnchor.freelock;
219 TxAnchor.freelock = lid;
220 TxAnchor.tlocksInUse--;
221 if (jfs_tlocks_low && (TxAnchor.tlocksInUse < TxLockLWM)) {
222 jfs_info("txLockFree jfs_tlocks_low no more");
224 TXN_WAKEUP(&TxAnchor.lowlockwait);
226 TXN_WAKEUP(&TxAnchor.freelockwait);
232 * FUNCTION: initialize transaction management structures
236 * serialization: single thread at jfs_init()
243 /* Set defaults for nTxLock and nTxBlock if unset */
246 if (nTxBlock == -1) {
247 /* Base default on memory size */
249 if (si.totalram > (256 * 1024)) /* 1 GB */
252 nTxLock = si.totalram >> 2;
253 } else if (nTxBlock > (8 * 1024))
256 nTxLock = nTxBlock << 3;
259 nTxBlock = nTxLock >> 3;
261 /* Verify tunable parameters */
263 nTxBlock = 16; /* No one should set it this low */
264 if (nTxBlock > 65536)
267 nTxLock = 256; /* No one should set it this low */
271 printk(KERN_INFO "JFS: nTxBlock = %d, nTxLock = %d\n",
274 * initialize transaction block (tblock) table
276 * transaction id (tid) = tblock index
277 * tid = 0 is reserved.
279 TxLockLWM = (nTxLock * 4) / 10;
280 TxLockHWM = (nTxLock * 7) / 10;
281 TxLockVHWM = (nTxLock * 8) / 10;
283 size = sizeof(struct tblock) * nTxBlock;
284 TxBlock = vmalloc(size);
288 for (k = 1; k < nTxBlock - 1; k++) {
289 TxBlock[k].next = k + 1;
290 init_waitqueue_head(&TxBlock[k].gcwait);
291 init_waitqueue_head(&TxBlock[k].waitor);
294 init_waitqueue_head(&TxBlock[k].gcwait);
295 init_waitqueue_head(&TxBlock[k].waitor);
297 TxAnchor.freetid = 1;
298 init_waitqueue_head(&TxAnchor.freewait);
300 stattx.maxtid = 1; /* statistics */
303 * initialize transaction lock (tlock) table
305 * transaction lock id = tlock index
306 * tlock id = 0 is reserved.
308 size = sizeof(struct tlock) * nTxLock;
309 TxLock = vmalloc(size);
310 if (TxLock == NULL) {
315 /* initialize tlock table */
316 for (k = 1; k < nTxLock - 1; k++)
317 TxLock[k].next = k + 1;
319 init_waitqueue_head(&TxAnchor.freelockwait);
320 init_waitqueue_head(&TxAnchor.lowlockwait);
322 TxAnchor.freelock = 1;
323 TxAnchor.tlocksInUse = 0;
324 INIT_LIST_HEAD(&TxAnchor.anon_list);
325 INIT_LIST_HEAD(&TxAnchor.anon_list2);
328 INIT_LIST_HEAD(&TxAnchor.unlock_queue);
330 stattx.maxlid = 1; /* statistics */
338 * FUNCTION: clean up when module is unloaded
351 * FUNCTION: start a transaction.
353 * PARAMETER: sb - superblock
354 * flag - force for nested tx;
356 * RETURN: tid - transaction id
358 * note: flag force allows to start tx for nested tx
359 * to prevent deadlock on logsync barrier;
361 tid_t txBegin(struct super_block *sb, int flag)
367 jfs_info("txBegin: flag = 0x%x", flag);
368 log = JFS_SBI(sb)->log;
371 jfs_error(sb, "read-only filesystem\n");
377 INCREMENT(TxStat.txBegin);
380 if (!(flag & COMMIT_FORCE)) {
382 * synchronize with logsync barrier
384 if (test_bit(log_SYNCBARRIER, &log->flag) ||
385 test_bit(log_QUIESCE, &log->flag)) {
386 INCREMENT(TxStat.txBegin_barrier);
387 TXN_SLEEP(&log->syncwait);
393 * Don't begin transaction if we're getting starved for tlocks
394 * unless COMMIT_FORCE or COMMIT_INODE (which may ultimately
397 if (TxAnchor.tlocksInUse > TxLockVHWM) {
398 INCREMENT(TxStat.txBegin_lockslow);
399 TXN_SLEEP(&TxAnchor.lowlockwait);
405 * allocate transaction id/block
407 if ((t = TxAnchor.freetid) == 0) {
408 jfs_info("txBegin: waiting for free tid");
409 INCREMENT(TxStat.txBegin_freetid);
410 TXN_SLEEP(&TxAnchor.freewait);
414 tblk = tid_to_tblock(t);
416 if ((tblk->next == 0) && !(flag & COMMIT_FORCE)) {
417 /* Don't let a non-forced transaction take the last tblk */
418 jfs_info("txBegin: waiting for free tid");
419 INCREMENT(TxStat.txBegin_freetid);
420 TXN_SLEEP(&TxAnchor.freewait);
424 TxAnchor.freetid = tblk->next;
427 * initialize transaction
431 * We can't zero the whole thing or we screw up another thread being
432 * awakened after sleeping on tblk->waitor
434 * memset(tblk, 0, sizeof(struct tblock));
436 tblk->next = tblk->last = tblk->xflag = tblk->flag = tblk->lsn = 0;
440 tblk->logtid = log->logtid;
444 HIGHWATERMARK(stattx.maxtid, t); /* statistics */
445 INCREMENT(stattx.ntid); /* statistics */
449 jfs_info("txBegin: returning tid = %d", t);
455 * NAME: txBeginAnon()
457 * FUNCTION: start an anonymous transaction.
458 * Blocks if logsync or available tlocks are low to prevent
459 * anonymous tlocks from depleting supply.
461 * PARAMETER: sb - superblock
465 void txBeginAnon(struct super_block *sb)
469 log = JFS_SBI(sb)->log;
472 INCREMENT(TxStat.txBeginAnon);
476 * synchronize with logsync barrier
478 if (test_bit(log_SYNCBARRIER, &log->flag) ||
479 test_bit(log_QUIESCE, &log->flag)) {
480 INCREMENT(TxStat.txBeginAnon_barrier);
481 TXN_SLEEP(&log->syncwait);
486 * Don't begin transaction if we're getting starved for tlocks
488 if (TxAnchor.tlocksInUse > TxLockVHWM) {
489 INCREMENT(TxStat.txBeginAnon_lockslow);
490 TXN_SLEEP(&TxAnchor.lowlockwait);
499 * function: free specified transaction block.
501 * logsync barrier processing:
505 void txEnd(tid_t tid)
507 struct tblock *tblk = tid_to_tblock(tid);
510 jfs_info("txEnd: tid = %d", tid);
514 * wakeup transactions waiting on the page locked
515 * by the current transaction
517 TXN_WAKEUP(&tblk->waitor);
519 log = JFS_SBI(tblk->sb)->log;
522 * Lazy commit thread can't free this guy until we mark it UNLOCKED,
523 * otherwise, we would be left with a transaction that may have been
526 * Lazy commit thread will turn off tblkGC_LAZY before calling this
529 if (tblk->flag & tblkGC_LAZY) {
530 jfs_info("txEnd called w/lazy tid: %d, tblk = 0x%p", tid, tblk);
533 spin_lock_irq(&log->gclock); // LOGGC_LOCK
534 tblk->flag |= tblkGC_UNLOCKED;
535 spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
539 jfs_info("txEnd: tid: %d, tblk = 0x%p", tid, tblk);
541 assert(tblk->next == 0);
544 * insert tblock back on freelist
546 tblk->next = TxAnchor.freetid;
547 TxAnchor.freetid = tid;
550 * mark the tblock not active
552 if (--log->active == 0) {
553 clear_bit(log_FLUSH, &log->flag);
556 * synchronize with logsync barrier
558 if (test_bit(log_SYNCBARRIER, &log->flag)) {
561 /* write dirty metadata & forward log syncpt */
564 jfs_info("log barrier off: 0x%x", log->lsn);
566 /* enable new transactions start */
567 clear_bit(log_SYNCBARRIER, &log->flag);
569 /* wakeup all waitors for logsync barrier */
570 TXN_WAKEUP(&log->syncwait);
579 * wakeup all waitors for a free tblock
581 TXN_WAKEUP(&TxAnchor.freewait);
587 * function: acquire a transaction lock on the specified <mp>
591 * return: transaction lock id
595 struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
598 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
603 struct xtlock *xtlck;
604 struct linelock *linelock;
610 if (S_ISDIR(ip->i_mode) && (type & tlckXTREE) &&
611 !(mp->xflag & COMMIT_PAGE)) {
613 * Directory inode is special. It can have both an xtree tlock
614 * and a dtree tlock associated with it.
621 /* is page not locked by a transaction ? */
625 jfs_info("txLock: tid:%d ip:0x%p mp:0x%p lid:%d", tid, ip, mp, lid);
627 /* is page locked by the requester transaction ? */
628 tlck = lid_to_tlock(lid);
629 if ((xtid = tlck->tid) == tid) {
635 * is page locked by anonymous transaction/lock ?
637 * (page update without transaction (i.e., file write) is
638 * locked under anonymous transaction tid = 0:
639 * anonymous tlocks maintained on anonymous tlock list of
640 * the inode of the page and available to all anonymous
641 * transactions until txCommit() time at which point
642 * they are transferred to the transaction tlock list of
643 * the committing transaction of the inode)
648 tblk = tid_to_tblock(tid);
650 * The order of the tlocks in the transaction is important
651 * (during truncate, child xtree pages must be freed before
652 * parent's tlocks change the working map).
653 * Take tlock off anonymous list and add to tail of
656 * Note: We really need to get rid of the tid & lid and
657 * use list_head's. This code is getting UGLY!
659 if (jfs_ip->atlhead == lid) {
660 if (jfs_ip->atltail == lid) {
661 /* only anonymous txn.
662 * Remove from anon_list
665 list_del_init(&jfs_ip->anon_inode_list);
668 jfs_ip->atlhead = tlck->next;
671 for (last = jfs_ip->atlhead;
672 lid_to_tlock(last)->next != lid;
673 last = lid_to_tlock(last)->next) {
676 lid_to_tlock(last)->next = tlck->next;
677 if (jfs_ip->atltail == lid)
678 jfs_ip->atltail = last;
681 /* insert the tlock at tail of transaction tlock list */
684 lid_to_tlock(tblk->last)->next = lid;
700 tlck = lid_to_tlock(lid);
709 /* mark tlock for meta-data page */
710 if (mp->xflag & COMMIT_PAGE) {
712 tlck->flag = tlckPAGELOCK;
714 /* mark the page dirty and nohomeok */
715 metapage_nohomeok(mp);
717 jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
718 mp, mp->nohomeok, tid, tlck);
720 /* if anonymous transaction, and buffer is on the group
721 * commit synclist, mark inode to show this. This will
722 * prevent the buffer from being marked nohomeok for too
725 if ((tid == 0) && mp->lsn)
726 set_cflag(COMMIT_Synclist, ip);
728 /* mark tlock for in-memory inode */
730 tlck->flag = tlckINODELOCK;
732 if (S_ISDIR(ip->i_mode))
733 tlck->flag |= tlckDIRECTORY;
737 /* bind the tlock and the page */
746 * enqueue transaction lock to transaction/inode
748 /* insert the tlock at tail of transaction tlock list */
750 tblk = tid_to_tblock(tid);
752 lid_to_tlock(tblk->last)->next = lid;
758 /* anonymous transaction:
759 * insert the tlock at head of inode anonymous tlock list
762 tlck->next = jfs_ip->atlhead;
763 jfs_ip->atlhead = lid;
764 if (tlck->next == 0) {
765 /* This inode's first anonymous transaction */
766 jfs_ip->atltail = lid;
768 list_add_tail(&jfs_ip->anon_inode_list,
769 &TxAnchor.anon_list);
774 /* initialize type dependent area for linelock */
775 linelock = (struct linelock *) & tlck->lock;
777 linelock->flag = tlckLINELOCK;
778 linelock->maxcnt = TLOCKSHORT;
781 switch (type & tlckTYPE) {
783 linelock->l2linesize = L2DTSLOTSIZE;
787 linelock->l2linesize = L2XTSLOTSIZE;
789 xtlck = (struct xtlock *) linelock;
790 xtlck->header.offset = 0;
791 xtlck->header.length = 2;
793 if (type & tlckNEW) {
794 xtlck->lwm.offset = XTENTRYSTART;
796 if (mp->xflag & COMMIT_PAGE)
797 p = (xtpage_t *) mp->data;
799 p = &jfs_ip->i_xtroot;
801 le16_to_cpu(p->header.nextindex);
803 xtlck->lwm.length = 0; /* ! */
804 xtlck->twm.offset = 0;
805 xtlck->hwm.offset = 0;
811 linelock->l2linesize = L2INODESLOTSIZE;
815 linelock->l2linesize = L2DATASLOTSIZE;
819 jfs_err("UFO tlock:0x%p", tlck);
823 * update tlock vector
831 * page is being locked by another transaction:
834 /* Only locks on ipimap or ipaimap should reach here */
835 /* assert(jfs_ip->fileset == AGGREGATE_I); */
836 if (jfs_ip->fileset != AGGREGATE_I) {
837 printk(KERN_ERR "txLock: trying to lock locked page!");
838 print_hex_dump(KERN_ERR, "ip: ", DUMP_PREFIX_ADDRESS, 16, 4,
840 print_hex_dump(KERN_ERR, "mp: ", DUMP_PREFIX_ADDRESS, 16, 4,
842 print_hex_dump(KERN_ERR, "Locker's tblock: ",
843 DUMP_PREFIX_ADDRESS, 16, 4, tid_to_tblock(tid),
844 sizeof(struct tblock), 0);
845 print_hex_dump(KERN_ERR, "Tlock: ", DUMP_PREFIX_ADDRESS, 16, 4,
846 tlck, sizeof(*tlck), 0);
849 INCREMENT(stattx.waitlock); /* statistics */
851 release_metapage(mp);
853 xtid = tlck->tid; /* reacquire after dropping TXN_LOCK */
855 jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
858 /* Recheck everything since dropping TXN_LOCK */
859 if (xtid && (tlck->mp == mp) && (mp->lid == lid))
860 TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
863 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);
910 * FUNCTION: Initiates pageout of pages modified by tid in journalled
911 * objects and frees their lockwords.
913 static void txUnlock(struct tblock * tblk)
916 struct linelock *linelock;
917 lid_t lid, next, llid, k;
923 jfs_info("txUnlock: tblk = 0x%p", tblk);
924 log = JFS_SBI(tblk->sb)->log;
927 * mark page under tlock homeok (its log has been written):
929 for (lid = tblk->next; lid; lid = next) {
930 tlck = lid_to_tlock(lid);
933 jfs_info("unlocking lid = %d, tlck = 0x%p", lid, tlck);
935 /* unbind page from tlock */
936 if ((mp = tlck->mp) != NULL &&
937 (tlck->type & tlckBTROOT) == 0) {
938 assert(mp->xflag & COMMIT_PAGE);
944 assert(mp->nohomeok > 0);
945 _metapage_homeok(mp);
947 /* inherit younger/larger clsn */
948 LOGSYNC_LOCK(log, flags);
950 logdiff(difft, tblk->clsn, log);
951 logdiff(diffp, mp->clsn, log);
953 mp->clsn = tblk->clsn;
955 mp->clsn = tblk->clsn;
956 LOGSYNC_UNLOCK(log, flags);
958 assert(!(tlck->flag & tlckFREEPAGE));
963 /* insert tlock, and linelock(s) of the tlock if any,
964 * at head of freelist
968 llid = ((struct linelock *) & tlck->lock)->next;
970 linelock = (struct linelock *) lid_to_tlock(llid);
979 tblk->next = tblk->last = 0;
982 * remove tblock from logsynclist
983 * (allocation map pages inherited lsn of tblk and
984 * has been inserted in logsync list at txUpdateMap())
987 LOGSYNC_LOCK(log, flags);
989 list_del(&tblk->synclist);
990 LOGSYNC_UNLOCK(log, flags);
997 * function: allocate a transaction lock for freed page/entry;
998 * for freed page, maplock is used as xtlock/dtlock type;
1000 struct tlock *txMaplock(tid_t tid, struct inode *ip, int type)
1002 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1004 struct tblock *tblk;
1006 struct maplock *maplock;
1013 lid = txLockAlloc();
1014 tlck = lid_to_tlock(lid);
1021 /* bind the tlock and the object */
1022 tlck->flag = tlckINODELOCK;
1023 if (S_ISDIR(ip->i_mode))
1024 tlck->flag |= tlckDIRECTORY;
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;
1071 * function: allocate a transaction lock for log vector list
1073 struct linelock *txLinelock(struct linelock * tlock)
1077 struct linelock *linelock;
1081 /* allocate a TxLock structure */
1082 lid = txLockAlloc();
1083 tlck = lid_to_tlock(lid);
1087 /* initialize linelock */
1088 linelock = (struct linelock *) tlck;
1090 linelock->flag = tlckLINELOCK;
1091 linelock->maxcnt = TLOCKLONG;
1092 linelock->index = 0;
1093 if (tlck->flag & tlckDIRECTORY)
1094 linelock->flag |= tlckDIRECTORY;
1096 /* append linelock after tlock */
1097 linelock->next = tlock->next;
1104 * transaction commit management
1105 * -----------------------------
1111 * FUNCTION: commit the changes to the objects specified in
1112 * clist. For journalled segments only the
1113 * changes of the caller are committed, ie by tid.
1114 * for non-journalled segments the data are flushed to
1115 * disk and then the change to the disk inode and indirect
1116 * blocks committed (so blocks newly allocated to the
1117 * segment will be made a part of the segment atomically).
1119 * all of the segments specified in clist must be in
1120 * one file system. no more than 6 segments are needed
1121 * to handle all unix svcs.
1123 * if the i_nlink field (i.e. disk inode link count)
1124 * is zero, and the type of inode is a regular file or
1125 * directory, or symbolic link , the inode is truncated
1126 * to zero length. the truncation is committed but the
1127 * VM resources are unaffected until it is closed (see
1135 * on entry the inode lock on each segment is assumed
1140 int txCommit(tid_t tid, /* transaction identifier */
1141 int nip, /* number of inodes to commit */
1142 struct inode **iplist, /* list of inode to commit */
1147 struct jfs_log *log;
1148 struct tblock *tblk;
1151 struct jfs_inode_info *jfs_ip;
1154 struct super_block *sb;
1156 jfs_info("txCommit, tid = %d, flag = %d", tid, flag);
1157 /* is read-only file system ? */
1158 if (isReadOnly(iplist[0])) {
1163 sb = cd.sb = iplist[0]->i_sb;
1167 tid = txBegin(sb, 0);
1168 tblk = tid_to_tblock(tid);
1171 * initialize commit structure
1173 log = JFS_SBI(sb)->log;
1176 /* initialize log record descriptor in commit */
1178 lrd->logtid = cpu_to_le32(tblk->logtid);
1181 tblk->xflag |= flag;
1183 if ((flag & (COMMIT_FORCE | COMMIT_SYNC)) == 0)
1184 tblk->xflag |= COMMIT_LAZY;
1186 * prepare non-journaled objects for commit
1188 * flush data pages of non-journaled file
1189 * to prevent the file getting non-initialized disk blocks
1197 * acquire transaction lock on (on-disk) inodes
1199 * update on-disk inode from in-memory inode
1200 * acquiring transaction locks for AFTER records
1201 * on the on-disk inode of file object
1203 * sort the inodes array by inode number in descending order
1204 * to prevent deadlock when acquiring transaction lock
1205 * of on-disk inodes on multiple on-disk inode pages by
1206 * multiple concurrent transactions
1208 for (k = 0; k < cd.nip; k++) {
1209 top = (cd.iplist[k])->i_ino;
1210 for (n = k + 1; n < cd.nip; n++) {
1212 if (ip->i_ino > top) {
1214 cd.iplist[n] = cd.iplist[k];
1220 jfs_ip = JFS_IP(ip);
1223 * BUGBUG - This code has temporarily been removed. The
1224 * intent is to ensure that any file data is written before
1225 * the metadata is committed to the journal. This prevents
1226 * uninitialized data from appearing in a file after the
1227 * journal has been replayed. (The uninitialized data
1228 * could be sensitive data removed by another user.)
1230 * The problem now is that we are holding the IWRITELOCK
1231 * on the inode, and calling filemap_fdatawrite on an
1232 * unmapped page will cause a deadlock in jfs_get_block.
1234 * The long term solution is to pare down the use of
1235 * IWRITELOCK. We are currently holding it too long.
1236 * We could also be smarter about which data pages need
1237 * to be written before the transaction is committed and
1238 * when we don't need to worry about it at all.
1240 * if ((!S_ISDIR(ip->i_mode))
1241 * && (tblk->flag & COMMIT_DELETE) == 0)
1242 * filemap_write_and_wait(ip->i_mapping);
1246 * Mark inode as not dirty. It will still be on the dirty
1247 * inode list, but we'll know not to commit it again unless
1248 * it gets marked dirty again
1250 clear_cflag(COMMIT_Dirty, ip);
1252 /* inherit anonymous tlock(s) of inode */
1253 if (jfs_ip->atlhead) {
1254 lid_to_tlock(jfs_ip->atltail)->next = tblk->next;
1255 tblk->next = jfs_ip->atlhead;
1257 tblk->last = jfs_ip->atltail;
1258 jfs_ip->atlhead = jfs_ip->atltail = 0;
1260 list_del_init(&jfs_ip->anon_inode_list);
1265 * acquire transaction lock on on-disk inode page
1266 * (become first tlock of the tblk's tlock list)
1268 if (((rc = diWrite(tid, ip))))
1273 * write log records from transaction locks
1275 * txUpdateMap() resets XAD_NEW in XAD.
1277 if ((rc = txLog(log, tblk, &cd)))
1281 * Ensure that inode isn't reused before
1282 * lazy commit thread finishes processing
1284 if (tblk->xflag & COMMIT_DELETE) {
1287 * Avoid a rare deadlock
1289 * If the inode is locked, we may be blocked in
1290 * jfs_commit_inode. If so, we don't want the
1291 * lazy_commit thread doing the last iput() on the inode
1292 * since that may block on the locked inode. Instead,
1293 * commit the transaction synchronously, so the last iput
1294 * will be done by the calling thread (or later)
1297 * I believe this code is no longer needed. Splitting I_LOCK
1298 * into two bits, I_NEW and I_SYNC should prevent this
1299 * deadlock as well. But since I don't have a JFS testload
1300 * to verify this, only a trivial s/I_LOCK/I_SYNC/ was done.
1303 if (tblk->u.ip->i_state & I_SYNC)
1304 tblk->xflag &= ~COMMIT_LAZY;
1307 ASSERT((!(tblk->xflag & COMMIT_DELETE)) ||
1308 ((tblk->u.ip->i_nlink == 0) &&
1309 !test_cflag(COMMIT_Nolink, tblk->u.ip)));
1312 * write COMMIT log record
1314 lrd->type = cpu_to_le16(LOG_COMMIT);
1316 lmLog(log, tblk, lrd, NULL);
1318 lmGroupCommit(log, tblk);
1321 * - transaction is now committed -
1325 * force pages in careful update
1326 * (imap addressing structure update)
1328 if (flag & COMMIT_FORCE)
1332 * update allocation map.
1334 * update inode allocation map and inode:
1335 * free pager lock on memory object of inode if any.
1336 * update block allocation map.
1338 * txUpdateMap() resets XAD_NEW in XAD.
1340 if (tblk->xflag & COMMIT_FORCE)
1344 * free transaction locks and pageout/free pages
1348 if ((tblk->flag & tblkGC_LAZY) == 0)
1353 * reset in-memory object state
1355 for (k = 0; k < cd.nip; k++) {
1357 jfs_ip = JFS_IP(ip);
1360 * reset in-memory inode state
1371 jfs_info("txCommit: tid = %d, returning %d", tid, rc);
1378 * FUNCTION: Writes AFTER log records for all lines modified
1379 * by tid for segments specified by inodes in comdata.
1380 * Code assumes only WRITELOCKS are recorded in lockwords.
1386 static int txLog(struct jfs_log * log, struct tblock * tblk, struct commit * cd)
1392 struct lrd *lrd = &cd->lrd;
1395 * write log record(s) for each tlock of transaction,
1397 for (lid = tblk->next; lid; lid = tlck->next) {
1398 tlck = lid_to_tlock(lid);
1400 tlck->flag |= tlckLOG;
1402 /* initialize lrd common */
1404 lrd->aggregate = cpu_to_le32(JFS_SBI(ip->i_sb)->aggregate);
1405 lrd->log.redopage.fileset = cpu_to_le32(JFS_IP(ip)->fileset);
1406 lrd->log.redopage.inode = cpu_to_le32(ip->i_ino);
1408 /* write log record of page from the tlock */
1409 switch (tlck->type & tlckTYPE) {
1411 xtLog(log, tblk, lrd, tlck);
1415 dtLog(log, tblk, lrd, tlck);
1419 diLog(log, tblk, lrd, tlck, cd);
1423 mapLog(log, tblk, lrd, tlck);
1427 dataLog(log, tblk, lrd, tlck);
1431 jfs_err("UFO tlock:0x%p", tlck);
1441 * function: log inode tlock and format maplock to update bmap;
1443 static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1444 struct tlock * tlck, struct commit * cd)
1447 struct metapage *mp;
1449 struct pxd_lock *pxdlock;
1453 /* initialize as REDOPAGE record format */
1454 lrd->log.redopage.type = cpu_to_le16(LOG_INODE);
1455 lrd->log.redopage.l2linesize = cpu_to_le16(L2INODESLOTSIZE);
1457 pxd = &lrd->log.redopage.pxd;
1462 if (tlck->type & tlckENTRY) {
1463 /* log after-image for logredo(): */
1464 lrd->type = cpu_to_le16(LOG_REDOPAGE);
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 */
1554 * function: log data tlock
1556 static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1557 struct tlock * tlck)
1559 struct metapage *mp;
1564 /* initialize as REDOPAGE record format */
1565 lrd->log.redopage.type = cpu_to_le16(LOG_DATA);
1566 lrd->log.redopage.l2linesize = cpu_to_le16(L2DATASLOTSIZE);
1568 pxd = &lrd->log.redopage.pxd;
1570 /* log after-image for logredo(): */
1571 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1573 if (jfs_dirtable_inline(tlck->ip)) {
1575 * The table has been truncated, we've must have deleted
1576 * the last entry, so don't bother logging this
1580 metapage_homeok(mp);
1581 discard_metapage(mp);
1586 PXDaddress(pxd, mp->index);
1587 PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
1589 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1591 /* mark page as homeward bound */
1592 tlck->flag |= tlckWRITEPAGE;
1600 * function: log dtree tlock and format maplock to update bmap;
1602 static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1603 struct tlock * tlck)
1605 struct metapage *mp;
1606 struct pxd_lock *pxdlock;
1611 /* initialize as REDOPAGE/NOREDOPAGE record format */
1612 lrd->log.redopage.type = cpu_to_le16(LOG_DTREE);
1613 lrd->log.redopage.l2linesize = cpu_to_le16(L2DTSLOTSIZE);
1615 pxd = &lrd->log.redopage.pxd;
1617 if (tlck->type & tlckBTROOT)
1618 lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
1621 * page extension via relocation: entry insertion;
1622 * page extension in-place: entry insertion;
1623 * new right page from page split, reinitialized in-line
1624 * root from root page split: entry insertion;
1626 if (tlck->type & (tlckNEW | tlckEXTEND)) {
1627 /* log after-image of the new page for logredo():
1628 * mark log (LOG_NEW) for logredo() to initialize
1629 * freelist and update bmap for alloc of the new page;
1631 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1632 if (tlck->type & tlckEXTEND)
1633 lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
1635 lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
1636 PXDaddress(pxd, mp->index);
1638 mp->logical_size >> tblk->sb->s_blocksize_bits);
1639 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1641 /* format a maplock for txUpdateMap() to update bPMAP for
1642 * alloc of the new page;
1644 if (tlck->type & tlckBTROOT)
1646 tlck->flag |= tlckUPDATEMAP;
1647 pxdlock = (struct pxd_lock *) & tlck->lock;
1648 pxdlock->flag = mlckALLOCPXD;
1649 pxdlock->pxd = *pxd;
1653 /* mark page as homeward bound */
1654 tlck->flag |= tlckWRITEPAGE;
1659 * entry insertion/deletion,
1660 * sibling page link update (old right page before split);
1662 if (tlck->type & (tlckENTRY | tlckRELINK)) {
1663 /* log after-image for logredo(): */
1664 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1665 PXDaddress(pxd, mp->index);
1667 mp->logical_size >> tblk->sb->s_blocksize_bits);
1668 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1670 /* mark page as homeward bound */
1671 tlck->flag |= tlckWRITEPAGE;
1676 * page deletion: page has been invalidated
1677 * page relocation: source extent
1679 * a maplock for free of the page has been formatted
1680 * at txLock() time);
1682 if (tlck->type & (tlckFREE | tlckRELOCATE)) {
1683 /* log LOG_NOREDOPAGE of the deleted page for logredo()
1684 * to start NoRedoPage filter and to update bmap for free
1685 * of the deletd page
1687 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
1688 pxdlock = (struct pxd_lock *) & tlck->lock;
1689 *pxd = pxdlock->pxd;
1690 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1692 /* a maplock for txUpdateMap() for free of the page
1693 * has been formatted at txLock() time;
1695 tlck->flag |= tlckUPDATEMAP;
1703 * function: log xtree tlock and format maplock to update bmap;
1705 static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1706 struct tlock * tlck)
1709 struct metapage *mp;
1711 struct xtlock *xtlck;
1712 struct maplock *maplock;
1713 struct xdlistlock *xadlock;
1714 struct pxd_lock *pxdlock;
1721 /* initialize as REDOPAGE/NOREDOPAGE record format */
1722 lrd->log.redopage.type = cpu_to_le16(LOG_XTREE);
1723 lrd->log.redopage.l2linesize = cpu_to_le16(L2XTSLOTSIZE);
1725 page_pxd = &lrd->log.redopage.pxd;
1727 if (tlck->type & tlckBTROOT) {
1728 lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
1729 p = &JFS_IP(ip)->i_xtroot;
1730 if (S_ISDIR(ip->i_mode))
1731 lrd->log.redopage.type |=
1732 cpu_to_le16(LOG_DIR_XTREE);
1734 p = (xtpage_t *) mp->data;
1735 next = le16_to_cpu(p->header.nextindex);
1737 xtlck = (struct xtlock *) & tlck->lock;
1739 maplock = (struct maplock *) & tlck->lock;
1740 xadlock = (struct xdlistlock *) maplock;
1743 * entry insertion/extension;
1744 * sibling page link update (old right page before split);
1746 if (tlck->type & (tlckNEW | tlckGROW | tlckRELINK)) {
1747 /* log after-image for logredo():
1748 * logredo() will update bmap for alloc of new/extended
1749 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1750 * after-image of XADlist;
1751 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1752 * applying the after-image to the meta-data page.
1754 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1755 PXDaddress(page_pxd, mp->index);
1757 mp->logical_size >> tblk->sb->s_blocksize_bits);
1758 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1760 /* format a maplock for txUpdateMap() to update bPMAP
1761 * for alloc of new/extended extents of XAD[lwm:next)
1762 * from the page itself;
1763 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
1765 lwm = xtlck->lwm.offset;
1767 lwm = XTPAGEMAXSLOT;
1772 jfs_err("xtLog: lwm > next");
1775 tlck->flag |= tlckUPDATEMAP;
1776 xadlock->flag = mlckALLOCXADLIST;
1777 xadlock->count = next - lwm;
1778 if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
1782 * Lazy commit may allow xtree to be modified before
1783 * txUpdateMap runs. Copy xad into linelock to
1784 * preserve correct data.
1786 * We can fit twice as may pxd's as xads in the lock
1788 xadlock->flag = mlckALLOCPXDLIST;
1789 pxd = xadlock->xdlist = &xtlck->pxdlock;
1790 for (i = 0; i < xadlock->count; i++) {
1791 PXDaddress(pxd, addressXAD(&p->xad[lwm + i]));
1792 PXDlength(pxd, lengthXAD(&p->xad[lwm + i]));
1793 p->xad[lwm + i].flag &=
1794 ~(XAD_NEW | XAD_EXTENDED);
1799 * xdlist will point to into inode's xtree, ensure
1800 * that transaction is not committed lazily.
1802 xadlock->flag = mlckALLOCXADLIST;
1803 xadlock->xdlist = &p->xad[lwm];
1804 tblk->xflag &= ~COMMIT_LAZY;
1806 jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d count:%d",
1807 tlck->ip, mp, tlck, lwm, xadlock->count);
1812 /* mark page as homeward bound */
1813 tlck->flag |= tlckWRITEPAGE;
1819 * page deletion: file deletion/truncation (ref. xtTruncate())
1821 * (page will be invalidated after log is written and bmap
1822 * is updated from the page);
1824 if (tlck->type & tlckFREE) {
1825 /* LOG_NOREDOPAGE log for NoRedoPage filter:
1826 * if page free from file delete, NoRedoFile filter from
1827 * inode image of zero link count will subsume NoRedoPage
1828 * filters for each page;
1829 * if page free from file truncattion, write NoRedoPage
1832 * upadte of block allocation map for the page itself:
1833 * if page free from deletion and truncation, LOG_UPDATEMAP
1834 * log for the page itself is generated from processing
1835 * its parent page xad entries;
1837 /* if page free from file truncation, log LOG_NOREDOPAGE
1838 * of the deleted page for logredo() to start NoRedoPage
1839 * filter for the page;
1841 if (tblk->xflag & COMMIT_TRUNCATE) {
1842 /* write NOREDOPAGE for the page */
1843 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
1844 PXDaddress(page_pxd, mp->index);
1846 mp->logical_size >> tblk->sb->
1849 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1851 if (tlck->type & tlckBTROOT) {
1852 /* Empty xtree must be logged */
1853 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1855 cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1859 /* init LOG_UPDATEMAP of the freed extents
1860 * XAD[XTENTRYSTART:hwm) from the deleted page itself
1861 * for logredo() to update bmap;
1863 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1864 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEXADLIST);
1865 xtlck = (struct xtlock *) & tlck->lock;
1866 hwm = xtlck->hwm.offset;
1867 lrd->log.updatemap.nxd =
1868 cpu_to_le16(hwm - XTENTRYSTART + 1);
1869 /* reformat linelock for lmLog() */
1870 xtlck->header.offset = XTENTRYSTART;
1871 xtlck->header.length = hwm - XTENTRYSTART + 1;
1873 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1875 /* format a maplock for txUpdateMap() to update bmap
1876 * to free extents of XAD[XTENTRYSTART:hwm) from the
1877 * deleted page itself;
1879 tlck->flag |= tlckUPDATEMAP;
1880 xadlock->count = hwm - XTENTRYSTART + 1;
1881 if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
1885 * Lazy commit may allow xtree to be modified before
1886 * txUpdateMap runs. Copy xad into linelock to
1887 * preserve correct data.
1889 * We can fit twice as may pxd's as xads in the lock
1891 xadlock->flag = mlckFREEPXDLIST;
1892 pxd = xadlock->xdlist = &xtlck->pxdlock;
1893 for (i = 0; i < xadlock->count; i++) {
1895 addressXAD(&p->xad[XTENTRYSTART + i]));
1897 lengthXAD(&p->xad[XTENTRYSTART + i]));
1902 * xdlist will point to into inode's xtree, ensure
1903 * that transaction is not committed lazily.
1905 xadlock->flag = mlckFREEXADLIST;
1906 xadlock->xdlist = &p->xad[XTENTRYSTART];
1907 tblk->xflag &= ~COMMIT_LAZY;
1909 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
1910 tlck->ip, mp, xadlock->count);
1914 /* mark page as invalid */
1915 if (((tblk->xflag & COMMIT_PWMAP) || S_ISDIR(ip->i_mode))
1916 && !(tlck->type & tlckBTROOT))
1917 tlck->flag |= tlckFREEPAGE;
1919 else (tblk->xflag & COMMIT_PMAP)
1926 * page/entry truncation: file truncation (ref. xtTruncate())
1928 * |----------+------+------+---------------|
1930 * | | hwm - hwm before truncation
1931 * | next - truncation point
1932 * lwm - lwm before truncation
1935 if (tlck->type & tlckTRUNCATE) {
1936 pxd_t pxd; /* truncated extent of xad */
1940 * For truncation the entire linelock may be used, so it would
1941 * be difficult to store xad list in linelock itself.
1942 * Therefore, we'll just force transaction to be committed
1943 * synchronously, so that xtree pages won't be changed before
1946 tblk->xflag &= ~COMMIT_LAZY;
1947 lwm = xtlck->lwm.offset;
1949 lwm = XTPAGEMAXSLOT;
1950 hwm = xtlck->hwm.offset;
1951 twm = xtlck->twm.offset;
1956 /* log after-image for logredo():
1958 * logredo() will update bmap for alloc of new/extended
1959 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1960 * after-image of XADlist;
1961 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1962 * applying the after-image to the meta-data page.
1964 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1965 PXDaddress(page_pxd, mp->index);
1967 mp->logical_size >> tblk->sb->s_blocksize_bits);
1968 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1971 * truncate entry XAD[twm == next - 1]:
1973 if (twm == next - 1) {
1974 /* init LOG_UPDATEMAP for logredo() to update bmap for
1975 * free of truncated delta extent of the truncated
1976 * entry XAD[next - 1]:
1977 * (xtlck->pxdlock = truncated delta extent);
1979 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
1980 /* assert(pxdlock->type & tlckTRUNCATE); */
1981 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1982 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
1983 lrd->log.updatemap.nxd = cpu_to_le16(1);
1984 lrd->log.updatemap.pxd = pxdlock->pxd;
1985 pxd = pxdlock->pxd; /* save to format maplock */
1987 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1991 * free entries XAD[next:hwm]:
1994 /* init LOG_UPDATEMAP of the freed extents
1995 * XAD[next:hwm] from the deleted page itself
1996 * for logredo() to update bmap;
1998 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1999 lrd->log.updatemap.type =
2000 cpu_to_le16(LOG_FREEXADLIST);
2001 xtlck = (struct xtlock *) & tlck->lock;
2002 hwm = xtlck->hwm.offset;
2003 lrd->log.updatemap.nxd =
2004 cpu_to_le16(hwm - next + 1);
2005 /* reformat linelock for lmLog() */
2006 xtlck->header.offset = next;
2007 xtlck->header.length = hwm - next + 1;
2010 cpu_to_le32(lmLog(log, tblk, lrd, tlck));
2014 * format maplock(s) for txUpdateMap() to update bmap
2019 * allocate entries XAD[lwm:next):
2022 /* format a maplock for txUpdateMap() to update bPMAP
2023 * for alloc of new/extended extents of XAD[lwm:next)
2024 * from the page itself;
2025 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
2027 tlck->flag |= tlckUPDATEMAP;
2028 xadlock->flag = mlckALLOCXADLIST;
2029 xadlock->count = next - lwm;
2030 xadlock->xdlist = &p->xad[lwm];
2032 jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d lwm:%d next:%d",
2033 tlck->ip, mp, xadlock->count, lwm, next);
2039 * truncate entry XAD[twm == next - 1]:
2041 if (twm == next - 1) {
2042 /* format a maplock for txUpdateMap() to update bmap
2043 * to free truncated delta extent of the truncated
2044 * entry XAD[next - 1];
2045 * (xtlck->pxdlock = truncated delta extent);
2047 tlck->flag |= tlckUPDATEMAP;
2048 pxdlock = (struct pxd_lock *) xadlock;
2049 pxdlock->flag = mlckFREEPXD;
2053 jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d hwm:%d",
2054 ip, mp, pxdlock->count, hwm);
2060 * free entries XAD[next:hwm]:
2063 /* format a maplock for txUpdateMap() to update bmap
2064 * to free extents of XAD[next:hwm] from thedeleted
2067 tlck->flag |= tlckUPDATEMAP;
2068 xadlock->flag = mlckFREEXADLIST;
2069 xadlock->count = hwm - next + 1;
2070 xadlock->xdlist = &p->xad[next];
2072 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d next:%d hwm:%d",
2073 tlck->ip, mp, xadlock->count, next, hwm);
2077 /* mark page as homeward bound */
2078 tlck->flag |= tlckWRITEPAGE;
2086 * function: log from maplock of freed data extents;
2088 static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
2089 struct tlock * tlck)
2091 struct pxd_lock *pxdlock;
2096 * page relocation: free the source page extent
2098 * a maplock for txUpdateMap() for free of the page
2099 * has been formatted at txLock() time saving the src
2100 * relocated page address;
2102 if (tlck->type & tlckRELOCATE) {
2103 /* log LOG_NOREDOPAGE of the old relocated page
2104 * for logredo() to start NoRedoPage filter;
2106 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
2107 pxdlock = (struct pxd_lock *) & tlck->lock;
2108 pxd = &lrd->log.redopage.pxd;
2109 *pxd = pxdlock->pxd;
2110 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2112 /* (N.B. currently, logredo() does NOT update bmap
2113 * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
2114 * if page free from relocation, LOG_UPDATEMAP log is
2115 * specifically generated now for logredo()
2116 * to update bmap for free of src relocated page;
2117 * (new flag LOG_RELOCATE may be introduced which will
2118 * inform logredo() to start NORedoPage filter and also
2119 * update block allocation map at the same time, thus
2120 * avoiding an extra log write);
2122 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2123 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
2124 lrd->log.updatemap.nxd = cpu_to_le16(1);
2125 lrd->log.updatemap.pxd = pxdlock->pxd;
2126 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2128 /* a maplock for txUpdateMap() for free of the page
2129 * has been formatted at txLock() time;
2131 tlck->flag |= tlckUPDATEMAP;
2136 * Otherwise it's not a relocate request
2140 /* log LOG_UPDATEMAP for logredo() to update bmap for
2141 * free of truncated/relocated delta extent of the data;
2142 * e.g.: external EA extent, relocated/truncated extent
2143 * from xtTailgate();
2145 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2146 pxdlock = (struct pxd_lock *) & tlck->lock;
2147 nlock = pxdlock->index;
2148 for (i = 0; i < nlock; i++, pxdlock++) {
2149 if (pxdlock->flag & mlckALLOCPXD)
2150 lrd->log.updatemap.type =
2151 cpu_to_le16(LOG_ALLOCPXD);
2153 lrd->log.updatemap.type =
2154 cpu_to_le16(LOG_FREEPXD);
2155 lrd->log.updatemap.nxd = cpu_to_le16(1);
2156 lrd->log.updatemap.pxd = pxdlock->pxd;
2158 cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2159 jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
2160 (ulong) addressPXD(&pxdlock->pxd),
2161 lengthPXD(&pxdlock->pxd));
2165 tlck->flag |= tlckUPDATEMAP;
2172 * function: acquire maplock for EA/ACL extents or
2173 * set COMMIT_INLINE flag;
2175 void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
2177 struct tlock *tlck = NULL;
2178 struct pxd_lock *maplock = NULL, *pxdlock = NULL;
2181 * format maplock for alloc of new EA extent
2184 /* Since the newea could be a completely zeroed entry we need to
2185 * check for the two flags which indicate we should actually
2186 * commit new EA data
2188 if (newea->flag & DXD_EXTENT) {
2189 tlck = txMaplock(tid, ip, tlckMAP);
2190 maplock = (struct pxd_lock *) & tlck->lock;
2191 pxdlock = (struct pxd_lock *) maplock;
2192 pxdlock->flag = mlckALLOCPXD;
2193 PXDaddress(&pxdlock->pxd, addressDXD(newea));
2194 PXDlength(&pxdlock->pxd, lengthDXD(newea));
2197 } else if (newea->flag & DXD_INLINE) {
2200 set_cflag(COMMIT_Inlineea, ip);
2205 * format maplock for free of old EA extent
2207 if (!test_cflag(COMMIT_Nolink, ip) && oldea->flag & DXD_EXTENT) {
2209 tlck = txMaplock(tid, ip, tlckMAP);
2210 maplock = (struct pxd_lock *) & tlck->lock;
2211 pxdlock = (struct pxd_lock *) maplock;
2214 pxdlock->flag = mlckFREEPXD;
2215 PXDaddress(&pxdlock->pxd, addressDXD(oldea));
2216 PXDlength(&pxdlock->pxd, lengthDXD(oldea));
2224 * function: synchronously write pages locked by transaction
2225 * after txLog() but before txUpdateMap();
2227 static void txForce(struct tblock * tblk)
2231 struct metapage *mp;
2234 * reverse the order of transaction tlocks in
2235 * careful update order of address index pages
2236 * (right to left, bottom up)
2238 tlck = lid_to_tlock(tblk->next);
2242 tlck = lid_to_tlock(lid);
2244 tlck->next = tblk->next;
2250 * synchronously write the page, and
2251 * hold the page for txUpdateMap();
2253 for (lid = tblk->next; lid; lid = next) {
2254 tlck = lid_to_tlock(lid);
2257 if ((mp = tlck->mp) != NULL &&
2258 (tlck->type & tlckBTROOT) == 0) {
2259 assert(mp->xflag & COMMIT_PAGE);
2261 if (tlck->flag & tlckWRITEPAGE) {
2262 tlck->flag &= ~tlckWRITEPAGE;
2264 /* do not release page to freelist */
2268 * The "right" thing to do here is to
2269 * synchronously write the metadata.
2270 * With the current implementation this
2271 * is hard since write_metapage requires
2272 * us to kunmap & remap the page. If we
2273 * have tlocks pointing into the metadata
2274 * pages, we don't want to do this. I think
2275 * we can get by with synchronously writing
2276 * the pages when they are released.
2278 assert(mp->nohomeok);
2279 set_bit(META_dirty, &mp->flag);
2280 set_bit(META_sync, &mp->flag);
2290 * function: update persistent allocation map (and working map
2295 static void txUpdateMap(struct tblock * tblk)
2298 struct inode *ipimap;
2301 struct maplock *maplock;
2302 struct pxd_lock pxdlock;
2305 struct metapage *mp = NULL;
2307 ipimap = JFS_SBI(tblk->sb)->ipimap;
2309 maptype = (tblk->xflag & COMMIT_PMAP) ? COMMIT_PMAP : COMMIT_PWMAP;
2313 * update block allocation map
2315 * update allocation state in pmap (and wmap) and
2316 * update lsn of the pmap page;
2319 * scan each tlock/page of transaction for block allocation/free:
2321 * for each tlock/page of transaction, update map.
2322 * ? are there tlock for pmap and pwmap at the same time ?
2324 for (lid = tblk->next; lid; lid = tlck->next) {
2325 tlck = lid_to_tlock(lid);
2327 if ((tlck->flag & tlckUPDATEMAP) == 0)
2330 if (tlck->flag & tlckFREEPAGE) {
2332 * Another thread may attempt to reuse freed space
2333 * immediately, so we want to get rid of the metapage
2334 * before anyone else has a chance to get it.
2335 * Lock metapage, update maps, then invalidate
2339 ASSERT(mp->xflag & COMMIT_PAGE);
2345 * . in-line PXD list:
2346 * . out-of-line XAD list:
2348 maplock = (struct maplock *) & tlck->lock;
2349 nlock = maplock->index;
2351 for (k = 0; k < nlock; k++, maplock++) {
2353 * allocate blocks in persistent map:
2355 * blocks have been allocated from wmap at alloc time;
2357 if (maplock->flag & mlckALLOC) {
2358 txAllocPMap(ipimap, maplock, tblk);
2361 * free blocks in persistent and working map:
2362 * blocks will be freed in pmap and then in wmap;
2364 * ? tblock specifies the PMAP/PWMAP based upon
2367 * free blocks in persistent map:
2368 * blocks will be freed from wmap at last reference
2369 * release of the object for regular files;
2371 * Alway free blocks from both persistent & working
2372 * maps for directories
2374 else { /* (maplock->flag & mlckFREE) */
2376 if (tlck->flag & tlckDIRECTORY)
2377 txFreeMap(ipimap, maplock,
2378 tblk, COMMIT_PWMAP);
2380 txFreeMap(ipimap, maplock,
2384 if (tlck->flag & tlckFREEPAGE) {
2385 if (!(tblk->flag & tblkGC_LAZY)) {
2386 /* This is equivalent to txRelease */
2387 ASSERT(mp->lid == lid);
2390 assert(mp->nohomeok == 1);
2391 metapage_homeok(mp);
2392 discard_metapage(mp);
2397 * update inode allocation map
2399 * update allocation state in pmap and
2400 * update lsn of the pmap page;
2401 * update in-memory inode flag/state
2403 * unlock mapper/write lock
2405 if (tblk->xflag & COMMIT_CREATE) {
2406 diUpdatePMap(ipimap, tblk->ino, false, tblk);
2407 /* update persistent block allocation map
2408 * for the allocation of inode extent;
2410 pxdlock.flag = mlckALLOCPXD;
2411 pxdlock.pxd = tblk->u.ixpxd;
2413 txAllocPMap(ipimap, (struct maplock *) & pxdlock, tblk);
2414 } else if (tblk->xflag & COMMIT_DELETE) {
2416 diUpdatePMap(ipimap, ip->i_ino, true, tblk);
2424 * function: allocate from persistent map;
2433 * allocate from persistent map;
2434 * free from persistent map;
2435 * (e.g., tmp file - free from working map at releae
2436 * of last reference);
2437 * free from persistent and working map;
2439 * lsn - log sequence number;
2441 static void txAllocPMap(struct inode *ip, struct maplock * maplock,
2442 struct tblock * tblk)
2444 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
2445 struct xdlistlock *xadlistlock;
2449 struct pxd_lock *pxdlock;
2450 struct xdlistlock *pxdlistlock;
2455 * allocate from persistent map;
2457 if (maplock->flag & mlckALLOCXADLIST) {
2458 xadlistlock = (struct xdlistlock *) maplock;
2459 xad = xadlistlock->xdlist;
2460 for (n = 0; n < xadlistlock->count; n++, xad++) {
2461 if (xad->flag & (XAD_NEW | XAD_EXTENDED)) {
2462 xaddr = addressXAD(xad);
2463 xlen = lengthXAD(xad);
2464 dbUpdatePMap(ipbmap, false, xaddr,
2466 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
2467 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2468 (ulong) xaddr, xlen);
2471 } else if (maplock->flag & mlckALLOCPXD) {
2472 pxdlock = (struct pxd_lock *) maplock;
2473 xaddr = addressPXD(&pxdlock->pxd);
2474 xlen = lengthPXD(&pxdlock->pxd);
2475 dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen, tblk);
2476 jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong) xaddr, xlen);
2477 } else { /* (maplock->flag & mlckALLOCPXDLIST) */
2479 pxdlistlock = (struct xdlistlock *) maplock;
2480 pxd = pxdlistlock->xdlist;
2481 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2482 xaddr = addressPXD(pxd);
2483 xlen = lengthPXD(pxd);
2484 dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen,
2486 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2487 (ulong) xaddr, xlen);
2495 * function: free from persistent and/or working map;
2497 * todo: optimization
2499 void txFreeMap(struct inode *ip,
2500 struct maplock * maplock, struct tblock * tblk, int maptype)
2502 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
2503 struct xdlistlock *xadlistlock;
2507 struct pxd_lock *pxdlock;
2508 struct xdlistlock *pxdlistlock;
2512 jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
2513 tblk, maplock, maptype);
2516 * free from persistent map;
2518 if (maptype == COMMIT_PMAP || maptype == COMMIT_PWMAP) {
2519 if (maplock->flag & mlckFREEXADLIST) {
2520 xadlistlock = (struct xdlistlock *) maplock;
2521 xad = xadlistlock->xdlist;
2522 for (n = 0; n < xadlistlock->count; n++, xad++) {
2523 if (!(xad->flag & XAD_NEW)) {
2524 xaddr = addressXAD(xad);
2525 xlen = lengthXAD(xad);
2526 dbUpdatePMap(ipbmap, true, xaddr,
2528 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2529 (ulong) xaddr, xlen);
2532 } else if (maplock->flag & mlckFREEPXD) {
2533 pxdlock = (struct pxd_lock *) maplock;
2534 xaddr = addressPXD(&pxdlock->pxd);
2535 xlen = lengthPXD(&pxdlock->pxd);
2536 dbUpdatePMap(ipbmap, true, xaddr, (s64) xlen,
2538 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2539 (ulong) xaddr, xlen);
2540 } else { /* (maplock->flag & mlckALLOCPXDLIST) */
2542 pxdlistlock = (struct xdlistlock *) maplock;
2543 pxd = pxdlistlock->xdlist;
2544 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2545 xaddr = addressPXD(pxd);
2546 xlen = lengthPXD(pxd);
2547 dbUpdatePMap(ipbmap, true, xaddr,
2549 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2550 (ulong) xaddr, xlen);
2556 * free from working map;
2558 if (maptype == COMMIT_PWMAP || maptype == COMMIT_WMAP) {
2559 if (maplock->flag & mlckFREEXADLIST) {
2560 xadlistlock = (struct xdlistlock *) maplock;
2561 xad = xadlistlock->xdlist;
2562 for (n = 0; n < xadlistlock->count; n++, xad++) {
2563 xaddr = addressXAD(xad);
2564 xlen = lengthXAD(xad);
2565 dbFree(ip, xaddr, (s64) xlen);
2567 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2568 (ulong) xaddr, xlen);
2570 } else if (maplock->flag & mlckFREEPXD) {
2571 pxdlock = (struct pxd_lock *) maplock;
2572 xaddr = addressPXD(&pxdlock->pxd);
2573 xlen = lengthPXD(&pxdlock->pxd);
2574 dbFree(ip, xaddr, (s64) xlen);
2575 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2576 (ulong) xaddr, xlen);
2577 } else { /* (maplock->flag & mlckFREEPXDLIST) */
2579 pxdlistlock = (struct xdlistlock *) maplock;
2580 pxd = pxdlistlock->xdlist;
2581 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2582 xaddr = addressPXD(pxd);
2583 xlen = lengthPXD(pxd);
2584 dbFree(ip, xaddr, (s64) xlen);
2585 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2586 (ulong) xaddr, xlen);
2595 * function: remove tlock from inode anonymous locklist
2597 void txFreelock(struct inode *ip)
2599 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2600 struct tlock *xtlck, *tlck;
2601 lid_t xlid = 0, lid;
2603 if (!jfs_ip->atlhead)
2607 xtlck = (struct tlock *) &jfs_ip->atlhead;
2609 while ((lid = xtlck->next) != 0) {
2610 tlck = lid_to_tlock(lid);
2611 if (tlck->flag & tlckFREELOCK) {
2612 xtlck->next = tlck->next;
2620 if (jfs_ip->atlhead)
2621 jfs_ip->atltail = xlid;
2623 jfs_ip->atltail = 0;
2625 * If inode was on anon_list, remove it
2627 list_del_init(&jfs_ip->anon_inode_list);
2635 * function: abort tx before commit;
2637 * frees line-locks and segment locks for all
2638 * segments in comdata structure.
2639 * Optionally sets state of file-system to FM_DIRTY in super-block.
2640 * log age of page-frames in memory for which caller has
2641 * are reset to 0 (to avoid logwarap).
2643 void txAbort(tid_t tid, int dirty)
2646 struct metapage *mp;
2647 struct tblock *tblk = tid_to_tblock(tid);
2651 * free tlocks of the transaction
2653 for (lid = tblk->next; lid; lid = next) {
2654 tlck = lid_to_tlock(lid);
2657 JFS_IP(tlck->ip)->xtlid = 0;
2663 * reset lsn of page to avoid logwarap:
2665 * (page may have been previously committed by another
2666 * transaction(s) but has not been paged, i.e.,
2667 * it may be on logsync list even though it has not
2668 * been logged for the current tx.)
2670 if (mp->xflag & COMMIT_PAGE && mp->lsn)
2673 /* insert tlock at head of freelist */
2679 /* caller will free the transaction block */
2681 tblk->next = tblk->last = 0;
2684 * mark filesystem dirty
2687 jfs_error(tblk->sb, "\n");
2693 * txLazyCommit(void)
2695 * All transactions except those changing ipimap (COMMIT_FORCE) are
2696 * processed by this routine. This insures that the inode and block
2697 * allocation maps are updated in order. For synchronous transactions,
2698 * let the user thread finish processing after txUpdateMap() is called.
2700 static void txLazyCommit(struct tblock * tblk)
2702 struct jfs_log *log;
2704 while (((tblk->flag & tblkGC_READY) == 0) &&
2705 ((tblk->flag & tblkGC_UNLOCKED) == 0)) {
2706 /* We must have gotten ahead of the user thread
2708 jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk);
2712 jfs_info("txLazyCommit: processing tblk 0x%p", tblk);
2716 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
2718 spin_lock_irq(&log->gclock); // LOGGC_LOCK
2720 tblk->flag |= tblkGC_COMMITTED;
2722 if (tblk->flag & tblkGC_READY)
2725 wake_up_all(&tblk->gcwait); // LOGGC_WAKEUP
2728 * Can't release log->gclock until we've tested tblk->flag
2730 if (tblk->flag & tblkGC_LAZY) {
2731 spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
2733 tblk->flag &= ~tblkGC_LAZY;
2734 txEnd(tblk - TxBlock); /* Convert back to tid */
2736 spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
2738 jfs_info("txLazyCommit: done: tblk = 0x%p", tblk);
2742 * jfs_lazycommit(void)
2744 * To be run as a kernel daemon. If lbmIODone is called in an interrupt
2745 * context, or where blocking is not wanted, this routine will process
2746 * committed transactions from the unlock queue.
2748 int jfs_lazycommit(void *arg)
2751 struct tblock *tblk;
2752 unsigned long flags;
2753 struct jfs_sb_info *sbi;
2757 jfs_commit_thread_waking = 0; /* OK to wake another thread */
2758 while (!list_empty(&TxAnchor.unlock_queue)) {
2760 list_for_each_entry(tblk, &TxAnchor.unlock_queue,
2763 sbi = JFS_SBI(tblk->sb);
2765 * For each volume, the transactions must be
2766 * handled in order. If another commit thread
2767 * is handling a tblk for this superblock,
2770 if (sbi->commit_state & IN_LAZYCOMMIT)
2773 sbi->commit_state |= IN_LAZYCOMMIT;
2777 * Remove transaction from queue
2779 list_del(&tblk->cqueue);
2785 sbi->commit_state &= ~IN_LAZYCOMMIT;
2787 * Don't continue in the for loop. (We can't
2788 * anyway, it's unsafe!) We want to go back to
2789 * the beginning of the list.
2794 /* If there was nothing to do, don't continue */
2798 /* In case a wakeup came while all threads were active */
2799 jfs_commit_thread_waking = 0;
2801 if (freezing(current)) {
2805 DECLARE_WAITQUEUE(wq, current);
2807 add_wait_queue(&jfs_commit_thread_wait, &wq);
2808 set_current_state(TASK_INTERRUPTIBLE);
2811 remove_wait_queue(&jfs_commit_thread_wait, &wq);
2813 } while (!kthread_should_stop());
2815 if (!list_empty(&TxAnchor.unlock_queue))
2816 jfs_err("jfs_lazycommit being killed w/pending transactions!");
2818 jfs_info("jfs_lazycommit being killed");
2822 void txLazyUnlock(struct tblock * tblk)
2824 unsigned long flags;
2828 list_add_tail(&tblk->cqueue, &TxAnchor.unlock_queue);
2830 * Don't wake up a commit thread if there is already one servicing
2831 * this superblock, or if the last one we woke up hasn't started yet.
2833 if (!(JFS_SBI(tblk->sb)->commit_state & IN_LAZYCOMMIT) &&
2834 !jfs_commit_thread_waking) {
2835 jfs_commit_thread_waking = 1;
2836 wake_up(&jfs_commit_thread_wait);
2841 static void LogSyncRelease(struct metapage * mp)
2843 struct jfs_log *log = mp->log;
2845 assert(mp->nohomeok);
2847 metapage_homeok(mp);
2853 * Block all new transactions and push anonymous transactions to
2856 * This does almost the same thing as jfs_sync below. We don't
2857 * worry about deadlocking when jfs_tlocks_low is set, since we would
2858 * expect jfs_sync to get us out of that jam.
2860 void txQuiesce(struct super_block *sb)
2863 struct jfs_inode_info *jfs_ip;
2864 struct jfs_log *log = JFS_SBI(sb)->log;
2867 set_bit(log_QUIESCE, &log->flag);
2871 while (!list_empty(&TxAnchor.anon_list)) {
2872 jfs_ip = list_entry(TxAnchor.anon_list.next,
2873 struct jfs_inode_info,
2875 ip = &jfs_ip->vfs_inode;
2878 * inode will be removed from anonymous list
2879 * when it is committed
2882 tid = txBegin(ip->i_sb, COMMIT_INODE | COMMIT_FORCE);
2883 mutex_lock(&jfs_ip->commit_mutex);
2884 txCommit(tid, 1, &ip, 0);
2886 mutex_unlock(&jfs_ip->commit_mutex);
2888 * Just to be safe. I don't know how
2889 * long we can run without blocking
2896 * If jfs_sync is running in parallel, there could be some inodes
2897 * on anon_list2. Let's check.
2899 if (!list_empty(&TxAnchor.anon_list2)) {
2900 list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
2906 * We may need to kick off the group commit
2908 jfs_flush_journal(log, 0);
2914 * Allows transactions to start again following txQuiesce
2916 void txResume(struct super_block *sb)
2918 struct jfs_log *log = JFS_SBI(sb)->log;
2920 clear_bit(log_QUIESCE, &log->flag);
2921 TXN_WAKEUP(&log->syncwait);
2927 * To be run as a kernel daemon. This is awakened when tlocks run low.
2928 * We write any inodes that have anonymous tlocks so they will become
2931 int jfs_sync(void *arg)
2934 struct jfs_inode_info *jfs_ip;
2939 * write each inode on the anonymous inode list
2942 while (jfs_tlocks_low && !list_empty(&TxAnchor.anon_list)) {
2943 jfs_ip = list_entry(TxAnchor.anon_list.next,
2944 struct jfs_inode_info,
2946 ip = &jfs_ip->vfs_inode;
2950 * Inode is being freed
2952 list_del_init(&jfs_ip->anon_inode_list);
2953 } else if (mutex_trylock(&jfs_ip->commit_mutex)) {
2955 * inode will be removed from anonymous list
2956 * when it is committed
2959 tid = txBegin(ip->i_sb, COMMIT_INODE);
2960 txCommit(tid, 1, &ip, 0);
2962 mutex_unlock(&jfs_ip->commit_mutex);
2966 * Just to be safe. I don't know how
2967 * long we can run without blocking
2972 /* We can't get the commit mutex. It may
2973 * be held by a thread waiting for tlock's
2974 * so let's not block here. Save it to
2975 * put back on the anon_list.
2978 /* Move from anon_list to anon_list2 */
2979 list_move(&jfs_ip->anon_inode_list,
2980 &TxAnchor.anon_list2);
2987 /* Add anon_list2 back to anon_list */
2988 list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
2990 if (freezing(current)) {
2994 set_current_state(TASK_INTERRUPTIBLE);
2998 } while (!kthread_should_stop());
3000 jfs_info("jfs_sync being killed");
3004 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
3005 static int jfs_txanchor_proc_show(struct seq_file *m, void *v)
3012 waitqueue_active(&TxAnchor.freewait) ? "active" : "empty";
3014 waitqueue_active(&TxAnchor.freelockwait) ? "active" : "empty";
3016 waitqueue_active(&TxAnchor.lowlockwait) ? "active" : "empty";
3024 "freelockwait = %s\n"
3025 "lowlockwait = %s\n"
3026 "tlocksInUse = %d\n"
3027 "jfs_tlocks_low = %d\n"
3028 "unlock_queue is %sempty\n",
3034 TxAnchor.tlocksInUse,
3036 list_empty(&TxAnchor.unlock_queue) ? "" : "not ");
3040 static int jfs_txanchor_proc_open(struct inode *inode, struct file *file)
3042 return single_open(file, jfs_txanchor_proc_show, NULL);
3045 const struct file_operations jfs_txanchor_proc_fops = {
3046 .open = jfs_txanchor_proc_open,
3048 .llseek = seq_lseek,
3049 .release = single_release,
3053 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
3054 static int jfs_txstats_proc_show(struct seq_file *m, void *v)
3059 "calls to txBegin = %d\n"
3060 "txBegin blocked by sync barrier = %d\n"
3061 "txBegin blocked by tlocks low = %d\n"
3062 "txBegin blocked by no free tid = %d\n"
3063 "calls to txBeginAnon = %d\n"
3064 "txBeginAnon blocked by sync barrier = %d\n"
3065 "txBeginAnon blocked by tlocks low = %d\n"
3066 "calls to txLockAlloc = %d\n"
3067 "tLockAlloc blocked by no free lock = %d\n",
3069 TxStat.txBegin_barrier,
3070 TxStat.txBegin_lockslow,
3071 TxStat.txBegin_freetid,
3073 TxStat.txBeginAnon_barrier,
3074 TxStat.txBeginAnon_lockslow,
3076 TxStat.txLockAlloc_freelock);
3080 static int jfs_txstats_proc_open(struct inode *inode, struct file *file)
3082 return single_open(file, jfs_txstats_proc_show, NULL);
3085 const struct file_operations jfs_txstats_proc_fops = {
3086 .open = jfs_txstats_proc_open,
3088 .llseek = seq_lseek,
3089 .release = single_release,