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 jfs_info("log barrier off: 0x%x", log
->lsn
);
572 /* enable new transactions start */
573 clear_bit(log_SYNCBARRIER
, &log
->flag
);
575 /* wakeup all waitors for logsync barrier */
576 TXN_WAKEUP(&log
->syncwait
);
580 /* forward log syncpt */
590 * wakeup all waitors for a free tblock
592 TXN_WAKEUP(&TxAnchor
.freewait
);
599 * function: acquire a transaction lock on the specified <mp>
603 * return: transaction lock id
607 struct tlock
*txLock(tid_t tid
, struct inode
*ip
, struct metapage
* mp
,
610 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
615 struct xtlock
*xtlck
;
616 struct linelock
*linelock
;
622 if (S_ISDIR(ip
->i_mode
) && (type
& tlckXTREE
) &&
623 !(mp
->xflag
& COMMIT_PAGE
)) {
625 * Directory inode is special. It can have both an xtree tlock
626 * and a dtree tlock associated with it.
633 /* is page not locked by a transaction ? */
637 jfs_info("txLock: tid:%d ip:0x%p mp:0x%p lid:%d", tid
, ip
, mp
, lid
);
639 /* is page locked by the requester transaction ? */
640 tlck
= lid_to_tlock(lid
);
641 if ((xtid
= tlck
->tid
) == tid
) {
647 * is page locked by anonymous transaction/lock ?
649 * (page update without transaction (i.e., file write) is
650 * locked under anonymous transaction tid = 0:
651 * anonymous tlocks maintained on anonymous tlock list of
652 * the inode of the page and available to all anonymous
653 * transactions until txCommit() time at which point
654 * they are transferred to the transaction tlock list of
655 * the commiting transaction of the inode)
660 tblk
= tid_to_tblock(tid
);
662 * The order of the tlocks in the transaction is important
663 * (during truncate, child xtree pages must be freed before
664 * parent's tlocks change the working map).
665 * Take tlock off anonymous list and add to tail of
668 * Note: We really need to get rid of the tid & lid and
669 * use list_head's. This code is getting UGLY!
671 if (jfs_ip
->atlhead
== lid
) {
672 if (jfs_ip
->atltail
== lid
) {
673 /* only anonymous txn.
674 * Remove from anon_list
676 list_del_init(&jfs_ip
->anon_inode_list
);
678 jfs_ip
->atlhead
= tlck
->next
;
681 for (last
= jfs_ip
->atlhead
;
682 lid_to_tlock(last
)->next
!= lid
;
683 last
= lid_to_tlock(last
)->next
) {
686 lid_to_tlock(last
)->next
= tlck
->next
;
687 if (jfs_ip
->atltail
== lid
)
688 jfs_ip
->atltail
= last
;
691 /* insert the tlock at tail of transaction tlock list */
694 lid_to_tlock(tblk
->last
)->next
= lid
;
710 tlck
= lid_to_tlock(lid
);
719 /* mark tlock for meta-data page */
720 if (mp
->xflag
& COMMIT_PAGE
) {
722 tlck
->flag
= tlckPAGELOCK
;
724 /* mark the page dirty and nohomeok */
725 metapage_nohomeok(mp
);
727 jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
728 mp
, mp
->nohomeok
, tid
, tlck
);
730 /* if anonymous transaction, and buffer is on the group
731 * commit synclist, mark inode to show this. This will
732 * prevent the buffer from being marked nohomeok for too
735 if ((tid
== 0) && mp
->lsn
)
736 set_cflag(COMMIT_Synclist
, ip
);
738 /* mark tlock for in-memory inode */
740 tlck
->flag
= tlckINODELOCK
;
744 /* bind the tlock and the page */
753 * enqueue transaction lock to transaction/inode
755 /* insert the tlock at tail of transaction tlock list */
757 tblk
= tid_to_tblock(tid
);
759 lid_to_tlock(tblk
->last
)->next
= lid
;
765 /* anonymous transaction:
766 * insert the tlock at head of inode anonymous tlock list
769 tlck
->next
= jfs_ip
->atlhead
;
770 jfs_ip
->atlhead
= lid
;
771 if (tlck
->next
== 0) {
772 /* This inode's first anonymous transaction */
773 jfs_ip
->atltail
= lid
;
775 list_add_tail(&jfs_ip
->anon_inode_list
,
776 &TxAnchor
.anon_list
);
781 /* initialize type dependent area for linelock */
782 linelock
= (struct linelock
*) & tlck
->lock
;
784 linelock
->flag
= tlckLINELOCK
;
785 linelock
->maxcnt
= TLOCKSHORT
;
788 switch (type
& tlckTYPE
) {
790 linelock
->l2linesize
= L2DTSLOTSIZE
;
794 linelock
->l2linesize
= L2XTSLOTSIZE
;
796 xtlck
= (struct xtlock
*) linelock
;
797 xtlck
->header
.offset
= 0;
798 xtlck
->header
.length
= 2;
800 if (type
& tlckNEW
) {
801 xtlck
->lwm
.offset
= XTENTRYSTART
;
803 if (mp
->xflag
& COMMIT_PAGE
)
804 p
= (xtpage_t
*) mp
->data
;
806 p
= &jfs_ip
->i_xtroot
;
808 le16_to_cpu(p
->header
.nextindex
);
810 xtlck
->lwm
.length
= 0; /* ! */
811 xtlck
->twm
.offset
= 0;
812 xtlck
->hwm
.offset
= 0;
818 linelock
->l2linesize
= L2INODESLOTSIZE
;
822 linelock
->l2linesize
= L2DATASLOTSIZE
;
826 jfs_err("UFO tlock:0x%p", tlck
);
830 * update tlock vector
838 * page is being locked by another transaction:
841 /* Only locks on ipimap or ipaimap should reach here */
842 /* assert(jfs_ip->fileset == AGGREGATE_I); */
843 if (jfs_ip
->fileset
!= AGGREGATE_I
) {
844 jfs_err("txLock: trying to lock locked page!");
845 dump_mem("ip", ip
, sizeof(struct inode
));
846 dump_mem("mp", mp
, sizeof(struct metapage
));
847 dump_mem("Locker's tblk", tid_to_tblock(tid
),
848 sizeof(struct tblock
));
849 dump_mem("Tlock", tlck
, sizeof(struct tlock
));
852 INCREMENT(stattx
.waitlock
); /* statistics */
854 release_metapage(mp
);
856 xtid
= tlck
->tid
; /* reaquire after dropping TXN_LOCK */
858 jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
861 /* Recheck everything since dropping TXN_LOCK */
862 if (xtid
&& (tlck
->mp
== mp
) && (mp
->lid
== lid
))
863 TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid
)->waitor
);
866 jfs_info("txLock: awakened tid = %d, lid = %d", tid
, lid
);
875 * FUNCTION: Release buffers associated with transaction locks, but don't
876 * mark homeok yet. The allows other transactions to modify
877 * buffers, but won't let them go to disk until commit record
878 * actually gets written.
883 * RETURN: Errors from subroutines.
885 static void txRelease(struct tblock
* tblk
)
893 for (lid
= tblk
->next
; lid
; lid
= tlck
->next
) {
894 tlck
= lid_to_tlock(lid
);
895 if ((mp
= tlck
->mp
) != NULL
&&
896 (tlck
->type
& tlckBTROOT
) == 0) {
897 assert(mp
->xflag
& COMMIT_PAGE
);
903 * wakeup transactions waiting on a page locked
904 * by the current transaction
906 TXN_WAKEUP(&tblk
->waitor
);
915 * FUNCTION: Initiates pageout of pages modified by tid in journalled
916 * objects and frees their lockwords.
918 static void txUnlock(struct tblock
* tblk
)
921 struct linelock
*linelock
;
922 lid_t lid
, next
, llid
, k
;
928 jfs_info("txUnlock: tblk = 0x%p", tblk
);
929 log
= JFS_SBI(tblk
->sb
)->log
;
932 * mark page under tlock homeok (its log has been written):
934 for (lid
= tblk
->next
; lid
; lid
= next
) {
935 tlck
= lid_to_tlock(lid
);
938 jfs_info("unlocking lid = %d, tlck = 0x%p", lid
, tlck
);
940 /* unbind page from tlock */
941 if ((mp
= tlck
->mp
) != NULL
&&
942 (tlck
->type
& tlckBTROOT
) == 0) {
943 assert(mp
->xflag
& COMMIT_PAGE
);
949 assert(mp
->nohomeok
> 0);
950 _metapage_homeok(mp
);
952 /* inherit younger/larger clsn */
953 LOGSYNC_LOCK(log
, flags
);
955 logdiff(difft
, tblk
->clsn
, log
);
956 logdiff(diffp
, mp
->clsn
, log
);
958 mp
->clsn
= tblk
->clsn
;
960 mp
->clsn
= tblk
->clsn
;
961 LOGSYNC_UNLOCK(log
, flags
);
963 assert(!(tlck
->flag
& tlckFREEPAGE
));
968 /* insert tlock, and linelock(s) of the tlock if any,
969 * at head of freelist
973 llid
= ((struct linelock
*) & tlck
->lock
)->next
;
975 linelock
= (struct linelock
*) lid_to_tlock(llid
);
984 tblk
->next
= tblk
->last
= 0;
987 * remove tblock from logsynclist
988 * (allocation map pages inherited lsn of tblk and
989 * has been inserted in logsync list at txUpdateMap())
992 LOGSYNC_LOCK(log
, flags
);
994 list_del(&tblk
->synclist
);
995 LOGSYNC_UNLOCK(log
, flags
);
1003 * function: allocate a transaction lock for freed page/entry;
1004 * for freed page, maplock is used as xtlock/dtlock type;
1006 struct tlock
*txMaplock(tid_t tid
, struct inode
*ip
, int type
)
1008 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
1010 struct tblock
*tblk
;
1012 struct maplock
*maplock
;
1019 lid
= txLockAlloc();
1020 tlck
= lid_to_tlock(lid
);
1027 /* bind the tlock and the object */
1028 tlck
->flag
= tlckINODELOCK
;
1035 * enqueue transaction lock to transaction/inode
1037 /* insert the tlock at tail of transaction tlock list */
1039 tblk
= tid_to_tblock(tid
);
1041 lid_to_tlock(tblk
->last
)->next
= lid
;
1047 /* anonymous transaction:
1048 * insert the tlock at head of inode anonymous tlock list
1051 tlck
->next
= jfs_ip
->atlhead
;
1052 jfs_ip
->atlhead
= lid
;
1053 if (tlck
->next
== 0) {
1054 /* This inode's first anonymous transaction */
1055 jfs_ip
->atltail
= lid
;
1056 list_add_tail(&jfs_ip
->anon_inode_list
,
1057 &TxAnchor
.anon_list
);
1063 /* initialize type dependent area for maplock */
1064 maplock
= (struct maplock
*) & tlck
->lock
;
1066 maplock
->maxcnt
= 0;
1076 * function: allocate a transaction lock for log vector list
1078 struct linelock
*txLinelock(struct linelock
* tlock
)
1082 struct linelock
*linelock
;
1086 /* allocate a TxLock structure */
1087 lid
= txLockAlloc();
1088 tlck
= lid_to_tlock(lid
);
1092 /* initialize linelock */
1093 linelock
= (struct linelock
*) tlck
;
1095 linelock
->flag
= tlckLINELOCK
;
1096 linelock
->maxcnt
= TLOCKLONG
;
1097 linelock
->index
= 0;
1099 /* append linelock after tlock */
1100 linelock
->next
= tlock
->next
;
1109 * transaction commit management
1110 * -----------------------------
1116 * FUNCTION: commit the changes to the objects specified in
1117 * clist. For journalled segments only the
1118 * changes of the caller are committed, ie by tid.
1119 * for non-journalled segments the data are flushed to
1120 * disk and then the change to the disk inode and indirect
1121 * blocks committed (so blocks newly allocated to the
1122 * segment will be made a part of the segment atomically).
1124 * all of the segments specified in clist must be in
1125 * one file system. no more than 6 segments are needed
1126 * to handle all unix svcs.
1128 * if the i_nlink field (i.e. disk inode link count)
1129 * is zero, and the type of inode is a regular file or
1130 * directory, or symbolic link , the inode is truncated
1131 * to zero length. the truncation is committed but the
1132 * VM resources are unaffected until it is closed (see
1140 * on entry the inode lock on each segment is assumed
1145 int txCommit(tid_t tid
, /* transaction identifier */
1146 int nip
, /* number of inodes to commit */
1147 struct inode
**iplist
, /* list of inode to commit */
1152 struct jfs_log
*log
;
1153 struct tblock
*tblk
;
1157 struct jfs_inode_info
*jfs_ip
;
1160 struct super_block
*sb
;
1162 jfs_info("txCommit, tid = %d, flag = %d", tid
, flag
);
1163 /* is read-only file system ? */
1164 if (isReadOnly(iplist
[0])) {
1169 sb
= cd
.sb
= iplist
[0]->i_sb
;
1173 tid
= txBegin(sb
, 0);
1174 tblk
= tid_to_tblock(tid
);
1177 * initialize commit structure
1179 log
= JFS_SBI(sb
)->log
;
1182 /* initialize log record descriptor in commit */
1184 lrd
->logtid
= cpu_to_le32(tblk
->logtid
);
1187 tblk
->xflag
|= flag
;
1189 if ((flag
& (COMMIT_FORCE
| COMMIT_SYNC
)) == 0)
1190 tblk
->xflag
|= COMMIT_LAZY
;
1192 * prepare non-journaled objects for commit
1194 * flush data pages of non-journaled file
1195 * to prevent the file getting non-initialized disk blocks
1203 * acquire transaction lock on (on-disk) inodes
1205 * update on-disk inode from in-memory inode
1206 * acquiring transaction locks for AFTER records
1207 * on the on-disk inode of file object
1209 * sort the inodes array by inode number in descending order
1210 * to prevent deadlock when acquiring transaction lock
1211 * of on-disk inodes on multiple on-disk inode pages by
1212 * multiple concurrent transactions
1214 for (k
= 0; k
< cd
.nip
; k
++) {
1215 top
= (cd
.iplist
[k
])->i_ino
;
1216 for (n
= k
+ 1; n
< cd
.nip
; n
++) {
1218 if (ip
->i_ino
> top
) {
1220 cd
.iplist
[n
] = cd
.iplist
[k
];
1226 jfs_ip
= JFS_IP(ip
);
1229 * BUGBUG - This code has temporarily been removed. The
1230 * intent is to ensure that any file data is written before
1231 * the metadata is committed to the journal. This prevents
1232 * uninitialized data from appearing in a file after the
1233 * journal has been replayed. (The uninitialized data
1234 * could be sensitive data removed by another user.)
1236 * The problem now is that we are holding the IWRITELOCK
1237 * on the inode, and calling filemap_fdatawrite on an
1238 * unmapped page will cause a deadlock in jfs_get_block.
1240 * The long term solution is to pare down the use of
1241 * IWRITELOCK. We are currently holding it too long.
1242 * We could also be smarter about which data pages need
1243 * to be written before the transaction is committed and
1244 * when we don't need to worry about it at all.
1246 * if ((!S_ISDIR(ip->i_mode))
1247 * && (tblk->flag & COMMIT_DELETE) == 0) {
1248 * filemap_fdatawrite(ip->i_mapping);
1249 * filemap_fdatawait(ip->i_mapping);
1254 * Mark inode as not dirty. It will still be on the dirty
1255 * inode list, but we'll know not to commit it again unless
1256 * it gets marked dirty again
1258 clear_cflag(COMMIT_Dirty
, ip
);
1260 /* inherit anonymous tlock(s) of inode */
1261 if (jfs_ip
->atlhead
) {
1262 lid_to_tlock(jfs_ip
->atltail
)->next
= tblk
->next
;
1263 tblk
->next
= jfs_ip
->atlhead
;
1265 tblk
->last
= jfs_ip
->atltail
;
1266 jfs_ip
->atlhead
= jfs_ip
->atltail
= 0;
1268 list_del_init(&jfs_ip
->anon_inode_list
);
1273 * acquire transaction lock on on-disk inode page
1274 * (become first tlock of the tblk's tlock list)
1276 if (((rc
= diWrite(tid
, ip
))))
1281 * write log records from transaction locks
1283 * txUpdateMap() resets XAD_NEW in XAD.
1285 if ((rc
= txLog(log
, tblk
, &cd
)))
1289 * Ensure that inode isn't reused before
1290 * lazy commit thread finishes processing
1292 if (tblk
->xflag
& COMMIT_DELETE
) {
1293 atomic_inc(&tblk
->u
.ip
->i_count
);
1295 * Avoid a rare deadlock
1297 * If the inode is locked, we may be blocked in
1298 * jfs_commit_inode. If so, we don't want the
1299 * lazy_commit thread doing the last iput() on the inode
1300 * since that may block on the locked inode. Instead,
1301 * commit the transaction synchronously, so the last iput
1302 * will be done by the calling thread (or later)
1304 if (tblk
->u
.ip
->i_state
& I_LOCK
)
1305 tblk
->xflag
&= ~COMMIT_LAZY
;
1308 ASSERT((!(tblk
->xflag
& COMMIT_DELETE
)) ||
1309 ((tblk
->u
.ip
->i_nlink
== 0) &&
1310 !test_cflag(COMMIT_Nolink
, tblk
->u
.ip
)));
1313 * write COMMIT log record
1315 lrd
->type
= cpu_to_le16(LOG_COMMIT
);
1317 lsn
= lmLog(log
, tblk
, lrd
, NULL
);
1319 lmGroupCommit(log
, tblk
);
1322 * - transaction is now committed -
1326 * force pages in careful update
1327 * (imap addressing structure update)
1329 if (flag
& COMMIT_FORCE
)
1333 * update allocation map.
1335 * update inode allocation map and inode:
1336 * free pager lock on memory object of inode if any.
1337 * update block allocation map.
1339 * txUpdateMap() resets XAD_NEW in XAD.
1341 if (tblk
->xflag
& COMMIT_FORCE
)
1345 * free transaction locks and pageout/free pages
1349 if ((tblk
->flag
& tblkGC_LAZY
) == 0)
1354 * reset in-memory object state
1356 for (k
= 0; k
< cd
.nip
; k
++) {
1358 jfs_ip
= JFS_IP(ip
);
1361 * reset in-memory inode state
1372 jfs_info("txCommit: tid = %d, returning %d", tid
, rc
);
1380 * FUNCTION: Writes AFTER log records for all lines modified
1381 * by tid for segments specified by inodes in comdata.
1382 * Code assumes only WRITELOCKS are recorded in lockwords.
1388 static int txLog(struct jfs_log
* log
, struct tblock
* tblk
, struct commit
* cd
)
1394 struct lrd
*lrd
= &cd
->lrd
;
1397 * write log record(s) for each tlock of transaction,
1399 for (lid
= tblk
->next
; lid
; lid
= tlck
->next
) {
1400 tlck
= lid_to_tlock(lid
);
1402 tlck
->flag
|= tlckLOG
;
1404 /* initialize lrd common */
1406 lrd
->aggregate
= cpu_to_le32(JFS_SBI(ip
->i_sb
)->aggregate
);
1407 lrd
->log
.redopage
.fileset
= cpu_to_le32(JFS_IP(ip
)->fileset
);
1408 lrd
->log
.redopage
.inode
= cpu_to_le32(ip
->i_ino
);
1410 /* write log record of page from the tlock */
1411 switch (tlck
->type
& tlckTYPE
) {
1413 xtLog(log
, tblk
, lrd
, tlck
);
1417 dtLog(log
, tblk
, lrd
, tlck
);
1421 diLog(log
, tblk
, lrd
, tlck
, cd
);
1425 mapLog(log
, tblk
, lrd
, tlck
);
1429 dataLog(log
, tblk
, lrd
, tlck
);
1433 jfs_err("UFO tlock:0x%p", tlck
);
1444 * function: log inode tlock and format maplock to update bmap;
1446 static int diLog(struct jfs_log
* log
, struct tblock
* tblk
, struct lrd
* lrd
,
1447 struct tlock
* tlck
, struct commit
* cd
)
1450 struct metapage
*mp
;
1452 struct pxd_lock
*pxdlock
;
1456 /* initialize as REDOPAGE record format */
1457 lrd
->log
.redopage
.type
= cpu_to_le16(LOG_INODE
);
1458 lrd
->log
.redopage
.l2linesize
= cpu_to_le16(L2INODESLOTSIZE
);
1460 pxd
= &lrd
->log
.redopage
.pxd
;
1465 if (tlck
->type
& tlckENTRY
) {
1466 /* log after-image for logredo(): */
1467 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1468 // *pxd = mp->cm_pxd;
1469 PXDaddress(pxd
, mp
->index
);
1471 mp
->logical_size
>> tblk
->sb
->s_blocksize_bits
);
1472 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1474 /* mark page as homeward bound */
1475 tlck
->flag
|= tlckWRITEPAGE
;
1476 } else if (tlck
->type
& tlckFREE
) {
1480 * (pages of the freed inode extent have been invalidated and
1481 * a maplock for free of the extent has been formatted at
1484 * the tlock had been acquired on the inode allocation map page
1485 * (iag) that specifies the freed extent, even though the map
1486 * page is not itself logged, to prevent pageout of the map
1487 * page before the log;
1490 /* log LOG_NOREDOINOEXT of the freed inode extent for
1491 * logredo() to start NoRedoPage filters, and to update
1492 * imap and bmap for free of the extent;
1494 lrd
->type
= cpu_to_le16(LOG_NOREDOINOEXT
);
1496 * For the LOG_NOREDOINOEXT record, we need
1497 * to pass the IAG number and inode extent
1498 * index (within that IAG) from which the
1499 * the extent being released. These have been
1500 * passed to us in the iplist[1] and iplist[2].
1502 lrd
->log
.noredoinoext
.iagnum
=
1503 cpu_to_le32((u32
) (size_t) cd
->iplist
[1]);
1504 lrd
->log
.noredoinoext
.inoext_idx
=
1505 cpu_to_le32((u32
) (size_t) cd
->iplist
[2]);
1507 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1508 *pxd
= pxdlock
->pxd
;
1509 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
1512 tlck
->flag
|= tlckUPDATEMAP
;
1514 /* mark page as homeward bound */
1515 tlck
->flag
|= tlckWRITEPAGE
;
1517 jfs_err("diLog: UFO type tlck:0x%p", tlck
);
1520 * alloc/free external EA extent
1522 * a maplock for txUpdateMap() to update bPWMAP for alloc/free
1523 * of the extent has been formatted at txLock() time;
1526 assert(tlck
->type
& tlckEA
);
1528 /* log LOG_UPDATEMAP for logredo() to update bmap for
1529 * alloc of new (and free of old) external EA extent;
1531 lrd
->type
= cpu_to_le16(LOG_UPDATEMAP
);
1532 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1533 nlock
= pxdlock
->index
;
1534 for (i
= 0; i
< nlock
; i
++, pxdlock
++) {
1535 if (pxdlock
->flag
& mlckALLOCPXD
)
1536 lrd
->log
.updatemap
.type
=
1537 cpu_to_le16(LOG_ALLOCPXD
);
1539 lrd
->log
.updatemap
.type
=
1540 cpu_to_le16(LOG_FREEPXD
);
1541 lrd
->log
.updatemap
.nxd
= cpu_to_le16(1);
1542 lrd
->log
.updatemap
.pxd
= pxdlock
->pxd
;
1544 cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
1548 tlck
->flag
|= tlckUPDATEMAP
;
1550 #endif /* _JFS_WIP */
1559 * function: log data tlock
1561 static int dataLog(struct jfs_log
* log
, struct tblock
* tblk
, struct lrd
* lrd
,
1562 struct tlock
* tlck
)
1564 struct metapage
*mp
;
1569 /* initialize as REDOPAGE record format */
1570 lrd
->log
.redopage
.type
= cpu_to_le16(LOG_DATA
);
1571 lrd
->log
.redopage
.l2linesize
= cpu_to_le16(L2DATASLOTSIZE
);
1573 pxd
= &lrd
->log
.redopage
.pxd
;
1575 /* log after-image for logredo(): */
1576 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1578 if (jfs_dirtable_inline(tlck
->ip
)) {
1580 * The table has been truncated, we've must have deleted
1581 * the last entry, so don't bother logging this
1585 metapage_homeok(mp
);
1586 discard_metapage(mp
);
1591 PXDaddress(pxd
, mp
->index
);
1592 PXDlength(pxd
, mp
->logical_size
>> tblk
->sb
->s_blocksize_bits
);
1594 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1596 /* mark page as homeward bound */
1597 tlck
->flag
|= tlckWRITEPAGE
;
1606 * function: log dtree tlock and format maplock to update bmap;
1608 static void dtLog(struct jfs_log
* log
, struct tblock
* tblk
, struct lrd
* lrd
,
1609 struct tlock
* tlck
)
1611 struct metapage
*mp
;
1612 struct pxd_lock
*pxdlock
;
1617 /* initialize as REDOPAGE/NOREDOPAGE record format */
1618 lrd
->log
.redopage
.type
= cpu_to_le16(LOG_DTREE
);
1619 lrd
->log
.redopage
.l2linesize
= cpu_to_le16(L2DTSLOTSIZE
);
1621 pxd
= &lrd
->log
.redopage
.pxd
;
1623 if (tlck
->type
& tlckBTROOT
)
1624 lrd
->log
.redopage
.type
|= cpu_to_le16(LOG_BTROOT
);
1627 * page extension via relocation: entry insertion;
1628 * page extension in-place: entry insertion;
1629 * new right page from page split, reinitialized in-line
1630 * root from root page split: entry insertion;
1632 if (tlck
->type
& (tlckNEW
| tlckEXTEND
)) {
1633 /* log after-image of the new page for logredo():
1634 * mark log (LOG_NEW) for logredo() to initialize
1635 * freelist and update bmap for alloc of the new page;
1637 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1638 if (tlck
->type
& tlckEXTEND
)
1639 lrd
->log
.redopage
.type
|= cpu_to_le16(LOG_EXTEND
);
1641 lrd
->log
.redopage
.type
|= cpu_to_le16(LOG_NEW
);
1642 // *pxd = mp->cm_pxd;
1643 PXDaddress(pxd
, mp
->index
);
1645 mp
->logical_size
>> tblk
->sb
->s_blocksize_bits
);
1646 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1648 /* format a maplock for txUpdateMap() to update bPMAP for
1649 * alloc of the new page;
1651 if (tlck
->type
& tlckBTROOT
)
1653 tlck
->flag
|= tlckUPDATEMAP
;
1654 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1655 pxdlock
->flag
= mlckALLOCPXD
;
1656 pxdlock
->pxd
= *pxd
;
1660 /* mark page as homeward bound */
1661 tlck
->flag
|= tlckWRITEPAGE
;
1666 * entry insertion/deletion,
1667 * sibling page link update (old right page before split);
1669 if (tlck
->type
& (tlckENTRY
| tlckRELINK
)) {
1670 /* log after-image for logredo(): */
1671 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1672 PXDaddress(pxd
, mp
->index
);
1674 mp
->logical_size
>> tblk
->sb
->s_blocksize_bits
);
1675 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1677 /* mark page as homeward bound */
1678 tlck
->flag
|= tlckWRITEPAGE
;
1683 * page deletion: page has been invalidated
1684 * page relocation: source extent
1686 * a maplock for free of the page has been formatted
1687 * at txLock() time);
1689 if (tlck
->type
& (tlckFREE
| tlckRELOCATE
)) {
1690 /* log LOG_NOREDOPAGE of the deleted page for logredo()
1691 * to start NoRedoPage filter and to update bmap for free
1692 * of the deletd page
1694 lrd
->type
= cpu_to_le16(LOG_NOREDOPAGE
);
1695 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1696 *pxd
= pxdlock
->pxd
;
1697 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
1699 /* a maplock for txUpdateMap() for free of the page
1700 * has been formatted at txLock() time;
1702 tlck
->flag
|= tlckUPDATEMAP
;
1711 * function: log xtree tlock and format maplock to update bmap;
1713 static void xtLog(struct jfs_log
* log
, struct tblock
* tblk
, struct lrd
* lrd
,
1714 struct tlock
* tlck
)
1717 struct metapage
*mp
;
1719 struct xtlock
*xtlck
;
1720 struct maplock
*maplock
;
1721 struct xdlistlock
*xadlock
;
1722 struct pxd_lock
*pxdlock
;
1729 /* initialize as REDOPAGE/NOREDOPAGE record format */
1730 lrd
->log
.redopage
.type
= cpu_to_le16(LOG_XTREE
);
1731 lrd
->log
.redopage
.l2linesize
= cpu_to_le16(L2XTSLOTSIZE
);
1733 page_pxd
= &lrd
->log
.redopage
.pxd
;
1735 if (tlck
->type
& tlckBTROOT
) {
1736 lrd
->log
.redopage
.type
|= cpu_to_le16(LOG_BTROOT
);
1737 p
= &JFS_IP(ip
)->i_xtroot
;
1738 if (S_ISDIR(ip
->i_mode
))
1739 lrd
->log
.redopage
.type
|=
1740 cpu_to_le16(LOG_DIR_XTREE
);
1742 p
= (xtpage_t
*) mp
->data
;
1743 next
= le16_to_cpu(p
->header
.nextindex
);
1745 xtlck
= (struct xtlock
*) & tlck
->lock
;
1747 maplock
= (struct maplock
*) & tlck
->lock
;
1748 xadlock
= (struct xdlistlock
*) maplock
;
1751 * entry insertion/extension;
1752 * sibling page link update (old right page before split);
1754 if (tlck
->type
& (tlckNEW
| tlckGROW
| tlckRELINK
)) {
1755 /* log after-image for logredo():
1756 * logredo() will update bmap for alloc of new/extended
1757 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1758 * after-image of XADlist;
1759 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1760 * applying the after-image to the meta-data page.
1762 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1763 // *page_pxd = mp->cm_pxd;
1764 PXDaddress(page_pxd
, mp
->index
);
1766 mp
->logical_size
>> tblk
->sb
->s_blocksize_bits
);
1767 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1769 /* format a maplock for txUpdateMap() to update bPMAP
1770 * for alloc of new/extended extents of XAD[lwm:next)
1771 * from the page itself;
1772 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
1774 lwm
= xtlck
->lwm
.offset
;
1776 lwm
= XTPAGEMAXSLOT
;
1781 jfs_err("xtLog: lwm > next\n");
1784 tlck
->flag
|= tlckUPDATEMAP
;
1785 xadlock
->flag
= mlckALLOCXADLIST
;
1786 xadlock
->count
= next
- lwm
;
1787 if ((xadlock
->count
<= 4) && (tblk
->xflag
& COMMIT_LAZY
)) {
1791 * Lazy commit may allow xtree to be modified before
1792 * txUpdateMap runs. Copy xad into linelock to
1793 * preserve correct data.
1795 * We can fit twice as may pxd's as xads in the lock
1797 xadlock
->flag
= mlckALLOCPXDLIST
;
1798 pxd
= xadlock
->xdlist
= &xtlck
->pxdlock
;
1799 for (i
= 0; i
< xadlock
->count
; i
++) {
1800 PXDaddress(pxd
, addressXAD(&p
->xad
[lwm
+ i
]));
1801 PXDlength(pxd
, lengthXAD(&p
->xad
[lwm
+ i
]));
1802 p
->xad
[lwm
+ i
].flag
&=
1803 ~(XAD_NEW
| XAD_EXTENDED
);
1808 * xdlist will point to into inode's xtree, ensure
1809 * that transaction is not committed lazily.
1811 xadlock
->flag
= mlckALLOCXADLIST
;
1812 xadlock
->xdlist
= &p
->xad
[lwm
];
1813 tblk
->xflag
&= ~COMMIT_LAZY
;
1815 jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d "
1816 "count:%d", tlck
->ip
, mp
, tlck
, lwm
, xadlock
->count
);
1821 /* mark page as homeward bound */
1822 tlck
->flag
|= tlckWRITEPAGE
;
1828 * page deletion: file deletion/truncation (ref. xtTruncate())
1830 * (page will be invalidated after log is written and bmap
1831 * is updated from the page);
1833 if (tlck
->type
& tlckFREE
) {
1834 /* LOG_NOREDOPAGE log for NoRedoPage filter:
1835 * if page free from file delete, NoRedoFile filter from
1836 * inode image of zero link count will subsume NoRedoPage
1837 * filters for each page;
1838 * if page free from file truncattion, write NoRedoPage
1841 * upadte of block allocation map for the page itself:
1842 * if page free from deletion and truncation, LOG_UPDATEMAP
1843 * log for the page itself is generated from processing
1844 * its parent page xad entries;
1846 /* if page free from file truncation, log LOG_NOREDOPAGE
1847 * of the deleted page for logredo() to start NoRedoPage
1848 * filter for the page;
1850 if (tblk
->xflag
& COMMIT_TRUNCATE
) {
1851 /* write NOREDOPAGE for the page */
1852 lrd
->type
= cpu_to_le16(LOG_NOREDOPAGE
);
1853 PXDaddress(page_pxd
, mp
->index
);
1855 mp
->logical_size
>> tblk
->sb
->
1858 cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
1860 if (tlck
->type
& tlckBTROOT
) {
1861 /* Empty xtree must be logged */
1862 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1864 cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1868 /* init LOG_UPDATEMAP of the freed extents
1869 * XAD[XTENTRYSTART:hwm) from the deleted page itself
1870 * for logredo() to update bmap;
1872 lrd
->type
= cpu_to_le16(LOG_UPDATEMAP
);
1873 lrd
->log
.updatemap
.type
= cpu_to_le16(LOG_FREEXADLIST
);
1874 xtlck
= (struct xtlock
*) & tlck
->lock
;
1875 hwm
= xtlck
->hwm
.offset
;
1876 lrd
->log
.updatemap
.nxd
=
1877 cpu_to_le16(hwm
- XTENTRYSTART
+ 1);
1878 /* reformat linelock for lmLog() */
1879 xtlck
->header
.offset
= XTENTRYSTART
;
1880 xtlck
->header
.length
= hwm
- XTENTRYSTART
+ 1;
1882 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1884 /* format a maplock for txUpdateMap() to update bmap
1885 * to free extents of XAD[XTENTRYSTART:hwm) from the
1886 * deleted page itself;
1888 tlck
->flag
|= tlckUPDATEMAP
;
1889 xadlock
->count
= hwm
- XTENTRYSTART
+ 1;
1890 if ((xadlock
->count
<= 4) && (tblk
->xflag
& COMMIT_LAZY
)) {
1894 * Lazy commit may allow xtree to be modified before
1895 * txUpdateMap runs. Copy xad into linelock to
1896 * preserve correct data.
1898 * We can fit twice as may pxd's as xads in the lock
1900 xadlock
->flag
= mlckFREEPXDLIST
;
1901 pxd
= xadlock
->xdlist
= &xtlck
->pxdlock
;
1902 for (i
= 0; i
< xadlock
->count
; i
++) {
1904 addressXAD(&p
->xad
[XTENTRYSTART
+ i
]));
1906 lengthXAD(&p
->xad
[XTENTRYSTART
+ i
]));
1911 * xdlist will point to into inode's xtree, ensure
1912 * that transaction is not committed lazily.
1914 xadlock
->flag
= mlckFREEXADLIST
;
1915 xadlock
->xdlist
= &p
->xad
[XTENTRYSTART
];
1916 tblk
->xflag
&= ~COMMIT_LAZY
;
1918 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
1919 tlck
->ip
, mp
, xadlock
->count
);
1923 /* mark page as invalid */
1924 if (((tblk
->xflag
& COMMIT_PWMAP
) || S_ISDIR(ip
->i_mode
))
1925 && !(tlck
->type
& tlckBTROOT
))
1926 tlck
->flag
|= tlckFREEPAGE
;
1928 else (tblk->xflag & COMMIT_PMAP)
1935 * page/entry truncation: file truncation (ref. xtTruncate())
1937 * |----------+------+------+---------------|
1939 * | | hwm - hwm before truncation
1940 * | next - truncation point
1941 * lwm - lwm before truncation
1944 if (tlck
->type
& tlckTRUNCATE
) {
1945 pxd_t pxd
; /* truncated extent of xad */
1949 * For truncation the entire linelock may be used, so it would
1950 * be difficult to store xad list in linelock itself.
1951 * Therefore, we'll just force transaction to be committed
1952 * synchronously, so that xtree pages won't be changed before
1955 tblk
->xflag
&= ~COMMIT_LAZY
;
1956 lwm
= xtlck
->lwm
.offset
;
1958 lwm
= XTPAGEMAXSLOT
;
1959 hwm
= xtlck
->hwm
.offset
;
1960 twm
= xtlck
->twm
.offset
;
1965 /* log after-image for logredo():
1967 * logredo() will update bmap for alloc of new/extended
1968 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1969 * after-image of XADlist;
1970 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1971 * applying the after-image to the meta-data page.
1973 lrd
->type
= cpu_to_le16(LOG_REDOPAGE
);
1974 PXDaddress(page_pxd
, mp
->index
);
1976 mp
->logical_size
>> tblk
->sb
->s_blocksize_bits
);
1977 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
1980 * truncate entry XAD[twm == next - 1]:
1982 if (twm
== next
- 1) {
1983 /* init LOG_UPDATEMAP for logredo() to update bmap for
1984 * free of truncated delta extent of the truncated
1985 * entry XAD[next - 1]:
1986 * (xtlck->pxdlock = truncated delta extent);
1988 pxdlock
= (struct pxd_lock
*) & xtlck
->pxdlock
;
1989 /* assert(pxdlock->type & tlckTRUNCATE); */
1990 lrd
->type
= cpu_to_le16(LOG_UPDATEMAP
);
1991 lrd
->log
.updatemap
.type
= cpu_to_le16(LOG_FREEPXD
);
1992 lrd
->log
.updatemap
.nxd
= cpu_to_le16(1);
1993 lrd
->log
.updatemap
.pxd
= pxdlock
->pxd
;
1994 pxd
= pxdlock
->pxd
; /* save to format maplock */
1996 cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
2000 * free entries XAD[next:hwm]:
2003 /* init LOG_UPDATEMAP of the freed extents
2004 * XAD[next:hwm] from the deleted page itself
2005 * for logredo() to update bmap;
2007 lrd
->type
= cpu_to_le16(LOG_UPDATEMAP
);
2008 lrd
->log
.updatemap
.type
=
2009 cpu_to_le16(LOG_FREEXADLIST
);
2010 xtlck
= (struct xtlock
*) & tlck
->lock
;
2011 hwm
= xtlck
->hwm
.offset
;
2012 lrd
->log
.updatemap
.nxd
=
2013 cpu_to_le16(hwm
- next
+ 1);
2014 /* reformat linelock for lmLog() */
2015 xtlck
->header
.offset
= next
;
2016 xtlck
->header
.length
= hwm
- next
+ 1;
2019 cpu_to_le32(lmLog(log
, tblk
, lrd
, tlck
));
2023 * format maplock(s) for txUpdateMap() to update bmap
2028 * allocate entries XAD[lwm:next):
2031 /* format a maplock for txUpdateMap() to update bPMAP
2032 * for alloc of new/extended extents of XAD[lwm:next)
2033 * from the page itself;
2034 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
2036 tlck
->flag
|= tlckUPDATEMAP
;
2037 xadlock
->flag
= mlckALLOCXADLIST
;
2038 xadlock
->count
= next
- lwm
;
2039 xadlock
->xdlist
= &p
->xad
[lwm
];
2041 jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d "
2043 tlck
->ip
, mp
, xadlock
->count
, lwm
, next
);
2049 * truncate entry XAD[twm == next - 1]:
2051 if (twm
== next
- 1) {
2052 struct pxd_lock
*pxdlock
;
2054 /* format a maplock for txUpdateMap() to update bmap
2055 * to free truncated delta extent of the truncated
2056 * entry XAD[next - 1];
2057 * (xtlck->pxdlock = truncated delta extent);
2059 tlck
->flag
|= tlckUPDATEMAP
;
2060 pxdlock
= (struct pxd_lock
*) xadlock
;
2061 pxdlock
->flag
= mlckFREEPXD
;
2065 jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d "
2066 "hwm:%d", ip
, mp
, pxdlock
->count
, hwm
);
2072 * free entries XAD[next:hwm]:
2075 /* format a maplock for txUpdateMap() to update bmap
2076 * to free extents of XAD[next:hwm] from thedeleted
2079 tlck
->flag
|= tlckUPDATEMAP
;
2080 xadlock
->flag
= mlckFREEXADLIST
;
2081 xadlock
->count
= hwm
- next
+ 1;
2082 xadlock
->xdlist
= &p
->xad
[next
];
2084 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d "
2086 tlck
->ip
, mp
, xadlock
->count
, next
, hwm
);
2090 /* mark page as homeward bound */
2091 tlck
->flag
|= tlckWRITEPAGE
;
2100 * function: log from maplock of freed data extents;
2102 void mapLog(struct jfs_log
* log
, struct tblock
* tblk
, struct lrd
* lrd
,
2103 struct tlock
* tlck
)
2105 struct pxd_lock
*pxdlock
;
2110 * page relocation: free the source page extent
2112 * a maplock for txUpdateMap() for free of the page
2113 * has been formatted at txLock() time saving the src
2114 * relocated page address;
2116 if (tlck
->type
& tlckRELOCATE
) {
2117 /* log LOG_NOREDOPAGE of the old relocated page
2118 * for logredo() to start NoRedoPage filter;
2120 lrd
->type
= cpu_to_le16(LOG_NOREDOPAGE
);
2121 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2122 pxd
= &lrd
->log
.redopage
.pxd
;
2123 *pxd
= pxdlock
->pxd
;
2124 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
2126 /* (N.B. currently, logredo() does NOT update bmap
2127 * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
2128 * if page free from relocation, LOG_UPDATEMAP log is
2129 * specifically generated now for logredo()
2130 * to update bmap for free of src relocated page;
2131 * (new flag LOG_RELOCATE may be introduced which will
2132 * inform logredo() to start NORedoPage filter and also
2133 * update block allocation map at the same time, thus
2134 * avoiding an extra log write);
2136 lrd
->type
= cpu_to_le16(LOG_UPDATEMAP
);
2137 lrd
->log
.updatemap
.type
= cpu_to_le16(LOG_FREEPXD
);
2138 lrd
->log
.updatemap
.nxd
= cpu_to_le16(1);
2139 lrd
->log
.updatemap
.pxd
= pxdlock
->pxd
;
2140 lrd
->backchain
= cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
2142 /* a maplock for txUpdateMap() for free of the page
2143 * has been formatted at txLock() time;
2145 tlck
->flag
|= tlckUPDATEMAP
;
2150 * Otherwise it's not a relocate request
2154 /* log LOG_UPDATEMAP for logredo() to update bmap for
2155 * free of truncated/relocated delta extent of the data;
2156 * e.g.: external EA extent, relocated/truncated extent
2157 * from xtTailgate();
2159 lrd
->type
= cpu_to_le16(LOG_UPDATEMAP
);
2160 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2161 nlock
= pxdlock
->index
;
2162 for (i
= 0; i
< nlock
; i
++, pxdlock
++) {
2163 if (pxdlock
->flag
& mlckALLOCPXD
)
2164 lrd
->log
.updatemap
.type
=
2165 cpu_to_le16(LOG_ALLOCPXD
);
2167 lrd
->log
.updatemap
.type
=
2168 cpu_to_le16(LOG_FREEPXD
);
2169 lrd
->log
.updatemap
.nxd
= cpu_to_le16(1);
2170 lrd
->log
.updatemap
.pxd
= pxdlock
->pxd
;
2172 cpu_to_le32(lmLog(log
, tblk
, lrd
, NULL
));
2173 jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
2174 (ulong
) addressPXD(&pxdlock
->pxd
),
2175 lengthPXD(&pxdlock
->pxd
));
2179 tlck
->flag
|= tlckUPDATEMAP
;
2187 * function: acquire maplock for EA/ACL extents or
2188 * set COMMIT_INLINE flag;
2190 void txEA(tid_t tid
, struct inode
*ip
, dxd_t
* oldea
, dxd_t
* newea
)
2192 struct tlock
*tlck
= NULL
;
2193 struct pxd_lock
*maplock
= NULL
, *pxdlock
= NULL
;
2196 * format maplock for alloc of new EA extent
2199 /* Since the newea could be a completely zeroed entry we need to
2200 * check for the two flags which indicate we should actually
2201 * commit new EA data
2203 if (newea
->flag
& DXD_EXTENT
) {
2204 tlck
= txMaplock(tid
, ip
, tlckMAP
);
2205 maplock
= (struct pxd_lock
*) & tlck
->lock
;
2206 pxdlock
= (struct pxd_lock
*) maplock
;
2207 pxdlock
->flag
= mlckALLOCPXD
;
2208 PXDaddress(&pxdlock
->pxd
, addressDXD(newea
));
2209 PXDlength(&pxdlock
->pxd
, lengthDXD(newea
));
2212 } else if (newea
->flag
& DXD_INLINE
) {
2215 set_cflag(COMMIT_Inlineea
, ip
);
2220 * format maplock for free of old EA extent
2222 if (!test_cflag(COMMIT_Nolink
, ip
) && oldea
->flag
& DXD_EXTENT
) {
2224 tlck
= txMaplock(tid
, ip
, tlckMAP
);
2225 maplock
= (struct pxd_lock
*) & tlck
->lock
;
2226 pxdlock
= (struct pxd_lock
*) maplock
;
2229 pxdlock
->flag
= mlckFREEPXD
;
2230 PXDaddress(&pxdlock
->pxd
, addressDXD(oldea
));
2231 PXDlength(&pxdlock
->pxd
, lengthDXD(oldea
));
2240 * function: synchronously write pages locked by transaction
2241 * after txLog() but before txUpdateMap();
2243 void txForce(struct tblock
* tblk
)
2247 struct metapage
*mp
;
2250 * reverse the order of transaction tlocks in
2251 * careful update order of address index pages
2252 * (right to left, bottom up)
2254 tlck
= lid_to_tlock(tblk
->next
);
2258 tlck
= lid_to_tlock(lid
);
2260 tlck
->next
= tblk
->next
;
2266 * synchronously write the page, and
2267 * hold the page for txUpdateMap();
2269 for (lid
= tblk
->next
; lid
; lid
= next
) {
2270 tlck
= lid_to_tlock(lid
);
2273 if ((mp
= tlck
->mp
) != NULL
&&
2274 (tlck
->type
& tlckBTROOT
) == 0) {
2275 assert(mp
->xflag
& COMMIT_PAGE
);
2277 if (tlck
->flag
& tlckWRITEPAGE
) {
2278 tlck
->flag
&= ~tlckWRITEPAGE
;
2280 /* do not release page to freelist */
2284 * The "right" thing to do here is to
2285 * synchronously write the metadata.
2286 * With the current implementation this
2287 * is hard since write_metapage requires
2288 * us to kunmap & remap the page. If we
2289 * have tlocks pointing into the metadata
2290 * pages, we don't want to do this. I think
2291 * we can get by with synchronously writing
2292 * the pages when they are released.
2294 assert(mp
->nohomeok
);
2295 set_bit(META_dirty
, &mp
->flag
);
2296 set_bit(META_sync
, &mp
->flag
);
2307 * function: update persistent allocation map (and working map
2312 static void txUpdateMap(struct tblock
* tblk
)
2315 struct inode
*ipimap
;
2318 struct maplock
*maplock
;
2319 struct pxd_lock pxdlock
;
2322 struct metapage
*mp
= NULL
;
2324 ipimap
= JFS_SBI(tblk
->sb
)->ipimap
;
2326 maptype
= (tblk
->xflag
& COMMIT_PMAP
) ? COMMIT_PMAP
: COMMIT_PWMAP
;
2330 * update block allocation map
2332 * update allocation state in pmap (and wmap) and
2333 * update lsn of the pmap page;
2336 * scan each tlock/page of transaction for block allocation/free:
2338 * for each tlock/page of transaction, update map.
2339 * ? are there tlock for pmap and pwmap at the same time ?
2341 for (lid
= tblk
->next
; lid
; lid
= tlck
->next
) {
2342 tlck
= lid_to_tlock(lid
);
2344 if ((tlck
->flag
& tlckUPDATEMAP
) == 0)
2347 if (tlck
->flag
& tlckFREEPAGE
) {
2349 * Another thread may attempt to reuse freed space
2350 * immediately, so we want to get rid of the metapage
2351 * before anyone else has a chance to get it.
2352 * Lock metapage, update maps, then invalidate
2356 ASSERT(mp
->xflag
& COMMIT_PAGE
);
2362 * . in-line PXD list:
2363 * . out-of-line XAD list:
2365 maplock
= (struct maplock
*) & tlck
->lock
;
2366 nlock
= maplock
->index
;
2368 for (k
= 0; k
< nlock
; k
++, maplock
++) {
2370 * allocate blocks in persistent map:
2372 * blocks have been allocated from wmap at alloc time;
2374 if (maplock
->flag
& mlckALLOC
) {
2375 txAllocPMap(ipimap
, maplock
, tblk
);
2378 * free blocks in persistent and working map:
2379 * blocks will be freed in pmap and then in wmap;
2381 * ? tblock specifies the PMAP/PWMAP based upon
2384 * free blocks in persistent map:
2385 * blocks will be freed from wmap at last reference
2386 * release of the object for regular files;
2388 * Alway free blocks from both persistent & working
2389 * maps for directories
2391 else { /* (maplock->flag & mlckFREE) */
2393 if (S_ISDIR(tlck
->ip
->i_mode
))
2394 txFreeMap(ipimap
, maplock
,
2395 tblk
, COMMIT_PWMAP
);
2397 txFreeMap(ipimap
, maplock
,
2401 if (tlck
->flag
& tlckFREEPAGE
) {
2402 if (!(tblk
->flag
& tblkGC_LAZY
)) {
2403 /* This is equivalent to txRelease */
2404 ASSERT(mp
->lid
== lid
);
2407 assert(mp
->nohomeok
== 1);
2408 metapage_homeok(mp
);
2409 discard_metapage(mp
);
2414 * update inode allocation map
2416 * update allocation state in pmap and
2417 * update lsn of the pmap page;
2418 * update in-memory inode flag/state
2420 * unlock mapper/write lock
2422 if (tblk
->xflag
& COMMIT_CREATE
) {
2423 diUpdatePMap(ipimap
, tblk
->ino
, FALSE
, tblk
);
2424 ipimap
->i_state
|= I_DIRTY
;
2425 /* update persistent block allocation map
2426 * for the allocation of inode extent;
2428 pxdlock
.flag
= mlckALLOCPXD
;
2429 pxdlock
.pxd
= tblk
->u
.ixpxd
;
2431 txAllocPMap(ipimap
, (struct maplock
*) & pxdlock
, tblk
);
2432 } else if (tblk
->xflag
& COMMIT_DELETE
) {
2434 diUpdatePMap(ipimap
, ip
->i_ino
, TRUE
, tblk
);
2435 ipimap
->i_state
|= I_DIRTY
;
2444 * function: allocate from persistent map;
2453 * allocate from persistent map;
2454 * free from persistent map;
2455 * (e.g., tmp file - free from working map at releae
2456 * of last reference);
2457 * free from persistent and working map;
2459 * lsn - log sequence number;
2461 static void txAllocPMap(struct inode
*ip
, struct maplock
* maplock
,
2462 struct tblock
* tblk
)
2464 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
2465 struct xdlistlock
*xadlistlock
;
2469 struct pxd_lock
*pxdlock
;
2470 struct xdlistlock
*pxdlistlock
;
2475 * allocate from persistent map;
2477 if (maplock
->flag
& mlckALLOCXADLIST
) {
2478 xadlistlock
= (struct xdlistlock
*) maplock
;
2479 xad
= xadlistlock
->xdlist
;
2480 for (n
= 0; n
< xadlistlock
->count
; n
++, xad
++) {
2481 if (xad
->flag
& (XAD_NEW
| XAD_EXTENDED
)) {
2482 xaddr
= addressXAD(xad
);
2483 xlen
= lengthXAD(xad
);
2484 dbUpdatePMap(ipbmap
, FALSE
, xaddr
,
2486 xad
->flag
&= ~(XAD_NEW
| XAD_EXTENDED
);
2487 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2488 (ulong
) xaddr
, xlen
);
2491 } else if (maplock
->flag
& mlckALLOCPXD
) {
2492 pxdlock
= (struct pxd_lock
*) maplock
;
2493 xaddr
= addressPXD(&pxdlock
->pxd
);
2494 xlen
= lengthPXD(&pxdlock
->pxd
);
2495 dbUpdatePMap(ipbmap
, FALSE
, xaddr
, (s64
) xlen
, tblk
);
2496 jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong
) xaddr
, xlen
);
2497 } else { /* (maplock->flag & mlckALLOCPXDLIST) */
2499 pxdlistlock
= (struct xdlistlock
*) maplock
;
2500 pxd
= pxdlistlock
->xdlist
;
2501 for (n
= 0; n
< pxdlistlock
->count
; n
++, pxd
++) {
2502 xaddr
= addressPXD(pxd
);
2503 xlen
= lengthPXD(pxd
);
2504 dbUpdatePMap(ipbmap
, FALSE
, xaddr
, (s64
) xlen
,
2506 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2507 (ulong
) xaddr
, xlen
);
2516 * function: free from persistent and/or working map;
2518 * todo: optimization
2520 void txFreeMap(struct inode
*ip
,
2521 struct maplock
* maplock
, struct tblock
* tblk
, int maptype
)
2523 struct inode
*ipbmap
= JFS_SBI(ip
->i_sb
)->ipbmap
;
2524 struct xdlistlock
*xadlistlock
;
2528 struct pxd_lock
*pxdlock
;
2529 struct xdlistlock
*pxdlistlock
;
2533 jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
2534 tblk
, maplock
, maptype
);
2537 * free from persistent map;
2539 if (maptype
== COMMIT_PMAP
|| maptype
== COMMIT_PWMAP
) {
2540 if (maplock
->flag
& mlckFREEXADLIST
) {
2541 xadlistlock
= (struct xdlistlock
*) maplock
;
2542 xad
= xadlistlock
->xdlist
;
2543 for (n
= 0; n
< xadlistlock
->count
; n
++, xad
++) {
2544 if (!(xad
->flag
& XAD_NEW
)) {
2545 xaddr
= addressXAD(xad
);
2546 xlen
= lengthXAD(xad
);
2547 dbUpdatePMap(ipbmap
, TRUE
, xaddr
,
2549 jfs_info("freePMap: xaddr:0x%lx "
2551 (ulong
) xaddr
, xlen
);
2554 } else if (maplock
->flag
& mlckFREEPXD
) {
2555 pxdlock
= (struct pxd_lock
*) maplock
;
2556 xaddr
= addressPXD(&pxdlock
->pxd
);
2557 xlen
= lengthPXD(&pxdlock
->pxd
);
2558 dbUpdatePMap(ipbmap
, TRUE
, xaddr
, (s64
) xlen
,
2560 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2561 (ulong
) xaddr
, xlen
);
2562 } else { /* (maplock->flag & mlckALLOCPXDLIST) */
2564 pxdlistlock
= (struct xdlistlock
*) maplock
;
2565 pxd
= pxdlistlock
->xdlist
;
2566 for (n
= 0; n
< pxdlistlock
->count
; n
++, pxd
++) {
2567 xaddr
= addressPXD(pxd
);
2568 xlen
= lengthPXD(pxd
);
2569 dbUpdatePMap(ipbmap
, TRUE
, xaddr
,
2571 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2572 (ulong
) xaddr
, xlen
);
2578 * free from working map;
2580 if (maptype
== COMMIT_PWMAP
|| maptype
== COMMIT_WMAP
) {
2581 if (maplock
->flag
& mlckFREEXADLIST
) {
2582 xadlistlock
= (struct xdlistlock
*) maplock
;
2583 xad
= xadlistlock
->xdlist
;
2584 for (n
= 0; n
< xadlistlock
->count
; n
++, xad
++) {
2585 xaddr
= addressXAD(xad
);
2586 xlen
= lengthXAD(xad
);
2587 dbFree(ip
, xaddr
, (s64
) xlen
);
2589 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2590 (ulong
) xaddr
, xlen
);
2592 } else if (maplock
->flag
& mlckFREEPXD
) {
2593 pxdlock
= (struct pxd_lock
*) maplock
;
2594 xaddr
= addressPXD(&pxdlock
->pxd
);
2595 xlen
= lengthPXD(&pxdlock
->pxd
);
2596 dbFree(ip
, xaddr
, (s64
) xlen
);
2597 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2598 (ulong
) xaddr
, xlen
);
2599 } else { /* (maplock->flag & mlckFREEPXDLIST) */
2601 pxdlistlock
= (struct xdlistlock
*) maplock
;
2602 pxd
= pxdlistlock
->xdlist
;
2603 for (n
= 0; n
< pxdlistlock
->count
; n
++, pxd
++) {
2604 xaddr
= addressPXD(pxd
);
2605 xlen
= lengthPXD(pxd
);
2606 dbFree(ip
, xaddr
, (s64
) xlen
);
2607 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2608 (ulong
) xaddr
, xlen
);
2618 * function: remove tlock from inode anonymous locklist
2620 void txFreelock(struct inode
*ip
)
2622 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2623 struct tlock
*xtlck
, *tlck
;
2624 lid_t xlid
= 0, lid
;
2626 if (!jfs_ip
->atlhead
)
2630 xtlck
= (struct tlock
*) &jfs_ip
->atlhead
;
2632 while ((lid
= xtlck
->next
) != 0) {
2633 tlck
= lid_to_tlock(lid
);
2634 if (tlck
->flag
& tlckFREELOCK
) {
2635 xtlck
->next
= tlck
->next
;
2643 if (jfs_ip
->atlhead
)
2644 jfs_ip
->atltail
= xlid
;
2646 jfs_ip
->atltail
= 0;
2648 * If inode was on anon_list, remove it
2650 list_del_init(&jfs_ip
->anon_inode_list
);
2659 * function: abort tx before commit;
2661 * frees line-locks and segment locks for all
2662 * segments in comdata structure.
2663 * Optionally sets state of file-system to FM_DIRTY in super-block.
2664 * log age of page-frames in memory for which caller has
2665 * are reset to 0 (to avoid logwarap).
2667 void txAbort(tid_t tid
, int dirty
)
2670 struct metapage
*mp
;
2671 struct tblock
*tblk
= tid_to_tblock(tid
);
2675 * free tlocks of the transaction
2677 for (lid
= tblk
->next
; lid
; lid
= next
) {
2678 tlck
= lid_to_tlock(lid
);
2681 JFS_IP(tlck
->ip
)->xtlid
= 0;
2687 * reset lsn of page to avoid logwarap:
2689 * (page may have been previously committed by another
2690 * transaction(s) but has not been paged, i.e.,
2691 * it may be on logsync list even though it has not
2692 * been logged for the current tx.)
2694 if (mp
->xflag
& COMMIT_PAGE
&& mp
->lsn
)
2697 /* insert tlock at head of freelist */
2703 /* caller will free the transaction block */
2705 tblk
->next
= tblk
->last
= 0;
2708 * mark filesystem dirty
2711 jfs_error(tblk
->sb
, "txAbort");
2717 * txLazyCommit(void)
2719 * All transactions except those changing ipimap (COMMIT_FORCE) are
2720 * processed by this routine. This insures that the inode and block
2721 * allocation maps are updated in order. For synchronous transactions,
2722 * let the user thread finish processing after txUpdateMap() is called.
2724 static void txLazyCommit(struct tblock
* tblk
)
2726 struct jfs_log
*log
;
2728 while (((tblk
->flag
& tblkGC_READY
) == 0) &&
2729 ((tblk
->flag
& tblkGC_UNLOCKED
) == 0)) {
2730 /* We must have gotten ahead of the user thread
2732 jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk
);
2736 jfs_info("txLazyCommit: processing tblk 0x%p", tblk
);
2740 log
= (struct jfs_log
*) JFS_SBI(tblk
->sb
)->log
;
2742 spin_lock_irq(&log
->gclock
); // LOGGC_LOCK
2744 tblk
->flag
|= tblkGC_COMMITTED
;
2746 if (tblk
->flag
& tblkGC_READY
)
2749 wake_up_all(&tblk
->gcwait
); // LOGGC_WAKEUP
2752 * Can't release log->gclock until we've tested tblk->flag
2754 if (tblk
->flag
& tblkGC_LAZY
) {
2755 spin_unlock_irq(&log
->gclock
); // LOGGC_UNLOCK
2757 tblk
->flag
&= ~tblkGC_LAZY
;
2758 txEnd(tblk
- TxBlock
); /* Convert back to tid */
2760 spin_unlock_irq(&log
->gclock
); // LOGGC_UNLOCK
2762 jfs_info("txLazyCommit: done: tblk = 0x%p", tblk
);
2766 * jfs_lazycommit(void)
2768 * To be run as a kernel daemon. If lbmIODone is called in an interrupt
2769 * context, or where blocking is not wanted, this routine will process
2770 * committed transactions from the unlock queue.
2772 int jfs_lazycommit(void *arg
)
2775 struct tblock
*tblk
;
2776 unsigned long flags
;
2777 struct jfs_sb_info
*sbi
;
2779 daemonize("jfsCommit");
2781 complete(&jfsIOwait
);
2785 jfs_commit_thread_waking
= 0; /* OK to wake another thread */
2786 while (!list_empty(&TxAnchor
.unlock_queue
)) {
2788 list_for_each_entry(tblk
, &TxAnchor
.unlock_queue
,
2791 sbi
= JFS_SBI(tblk
->sb
);
2793 * For each volume, the transactions must be
2794 * handled in order. If another commit thread
2795 * is handling a tblk for this superblock,
2798 if (sbi
->commit_state
& IN_LAZYCOMMIT
)
2801 sbi
->commit_state
|= IN_LAZYCOMMIT
;
2805 * Remove transaction from queue
2807 list_del(&tblk
->cqueue
);
2813 sbi
->commit_state
&= ~IN_LAZYCOMMIT
;
2815 * Don't continue in the for loop. (We can't
2816 * anyway, it's unsafe!) We want to go back to
2817 * the beginning of the list.
2822 /* If there was nothing to do, don't continue */
2826 /* In case a wakeup came while all threads were active */
2827 jfs_commit_thread_waking
= 0;
2829 if (current
->flags
& PF_FREEZE
) {
2831 refrigerator(PF_FREEZE
);
2833 DECLARE_WAITQUEUE(wq
, current
);
2835 add_wait_queue(&jfs_commit_thread_wait
, &wq
);
2836 set_current_state(TASK_INTERRUPTIBLE
);
2839 current
->state
= TASK_RUNNING
;
2840 remove_wait_queue(&jfs_commit_thread_wait
, &wq
);
2842 } while (!jfs_stop_threads
);
2844 if (!list_empty(&TxAnchor
.unlock_queue
))
2845 jfs_err("jfs_lazycommit being killed w/pending transactions!");
2847 jfs_info("jfs_lazycommit being killed\n");
2848 complete_and_exit(&jfsIOwait
, 0);
2851 void txLazyUnlock(struct tblock
* tblk
)
2853 unsigned long flags
;
2857 list_add_tail(&tblk
->cqueue
, &TxAnchor
.unlock_queue
);
2859 * Don't wake up a commit thread if there is already one servicing
2860 * this superblock, or if the last one we woke up hasn't started yet.
2862 if (!(JFS_SBI(tblk
->sb
)->commit_state
& IN_LAZYCOMMIT
) &&
2863 !jfs_commit_thread_waking
) {
2864 jfs_commit_thread_waking
= 1;
2865 wake_up(&jfs_commit_thread_wait
);
2870 static void LogSyncRelease(struct metapage
* mp
)
2872 struct jfs_log
*log
= mp
->log
;
2874 assert(mp
->nohomeok
);
2876 metapage_homeok(mp
);
2882 * Block all new transactions and push anonymous transactions to
2885 * This does almost the same thing as jfs_sync below. We don't
2886 * worry about deadlocking when jfs_tlocks_low is set, since we would
2887 * expect jfs_sync to get us out of that jam.
2889 void txQuiesce(struct super_block
*sb
)
2892 struct jfs_inode_info
*jfs_ip
;
2893 struct jfs_log
*log
= JFS_SBI(sb
)->log
;
2896 set_bit(log_QUIESCE
, &log
->flag
);
2900 while (!list_empty(&TxAnchor
.anon_list
)) {
2901 jfs_ip
= list_entry(TxAnchor
.anon_list
.next
,
2902 struct jfs_inode_info
,
2904 ip
= &jfs_ip
->vfs_inode
;
2907 * inode will be removed from anonymous list
2908 * when it is committed
2911 tid
= txBegin(ip
->i_sb
, COMMIT_INODE
| COMMIT_FORCE
);
2912 down(&jfs_ip
->commit_sem
);
2913 txCommit(tid
, 1, &ip
, 0);
2915 up(&jfs_ip
->commit_sem
);
2917 * Just to be safe. I don't know how
2918 * long we can run without blocking
2925 * If jfs_sync is running in parallel, there could be some inodes
2926 * on anon_list2. Let's check.
2928 if (!list_empty(&TxAnchor
.anon_list2
)) {
2929 list_splice(&TxAnchor
.anon_list2
, &TxAnchor
.anon_list
);
2930 INIT_LIST_HEAD(&TxAnchor
.anon_list2
);
2936 * We may need to kick off the group commit
2938 jfs_flush_journal(log
, 0);
2944 * Allows transactions to start again following txQuiesce
2946 void txResume(struct super_block
*sb
)
2948 struct jfs_log
*log
= JFS_SBI(sb
)->log
;
2950 clear_bit(log_QUIESCE
, &log
->flag
);
2951 TXN_WAKEUP(&log
->syncwait
);
2957 * To be run as a kernel daemon. This is awakened when tlocks run low.
2958 * We write any inodes that have anonymous tlocks so they will become
2961 int jfs_sync(void *arg
)
2964 struct jfs_inode_info
*jfs_ip
;
2968 daemonize("jfsSync");
2970 complete(&jfsIOwait
);
2974 * write each inode on the anonymous inode list
2977 while (jfs_tlocks_low
&& !list_empty(&TxAnchor
.anon_list
)) {
2978 jfs_ip
= list_entry(TxAnchor
.anon_list
.next
,
2979 struct jfs_inode_info
,
2981 ip
= &jfs_ip
->vfs_inode
;
2985 * Inode is being freed
2987 list_del_init(&jfs_ip
->anon_inode_list
);
2988 } else if (! down_trylock(&jfs_ip
->commit_sem
)) {
2990 * inode will be removed from anonymous list
2991 * when it is committed
2994 tid
= txBegin(ip
->i_sb
, COMMIT_INODE
);
2995 rc
= txCommit(tid
, 1, &ip
, 0);
2997 up(&jfs_ip
->commit_sem
);
3001 * Just to be safe. I don't know how
3002 * long we can run without blocking
3007 /* We can't get the commit semaphore. It may
3008 * be held by a thread waiting for tlock's
3009 * so let's not block here. Save it to
3010 * put back on the anon_list.
3013 /* Take off anon_list */
3014 list_del(&jfs_ip
->anon_inode_list
);
3016 /* Put on anon_list2 */
3017 list_add(&jfs_ip
->anon_inode_list
,
3018 &TxAnchor
.anon_list2
);
3025 /* Add anon_list2 back to anon_list */
3026 list_splice_init(&TxAnchor
.anon_list2
, &TxAnchor
.anon_list
);
3028 if (current
->flags
& PF_FREEZE
) {
3030 refrigerator(PF_FREEZE
);
3032 DECLARE_WAITQUEUE(wq
, current
);
3034 add_wait_queue(&jfs_sync_thread_wait
, &wq
);
3035 set_current_state(TASK_INTERRUPTIBLE
);
3038 current
->state
= TASK_RUNNING
;
3039 remove_wait_queue(&jfs_sync_thread_wait
, &wq
);
3041 } while (!jfs_stop_threads
);
3043 jfs_info("jfs_sync being killed");
3044 complete_and_exit(&jfsIOwait
, 0);
3047 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
3048 int jfs_txanchor_read(char *buffer
, char **start
, off_t offset
, int length
,
3049 int *eof
, void *data
)
3058 waitqueue_active(&TxAnchor
.freewait
) ? "active" : "empty";
3060 waitqueue_active(&TxAnchor
.freelockwait
) ? "active" : "empty";
3062 waitqueue_active(&TxAnchor
.lowlockwait
) ? "active" : "empty";
3064 len
+= sprintf(buffer
,
3070 "freelockwait = %s\n"
3071 "lowlockwait = %s\n"
3072 "tlocksInUse = %d\n"
3073 "jfs_tlocks_low = %d\n"
3074 "unlock_queue is %sempty\n",
3080 TxAnchor
.tlocksInUse
,
3082 list_empty(&TxAnchor
.unlock_queue
) ? "" : "not ");
3085 *start
= buffer
+ begin
;
3100 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
3101 int jfs_txstats_read(char *buffer
, char **start
, off_t offset
, int length
,
3102 int *eof
, void *data
)
3107 len
+= sprintf(buffer
,
3110 "calls to txBegin = %d\n"
3111 "txBegin blocked by sync barrier = %d\n"
3112 "txBegin blocked by tlocks low = %d\n"
3113 "txBegin blocked by no free tid = %d\n"
3114 "calls to txBeginAnon = %d\n"
3115 "txBeginAnon blocked by sync barrier = %d\n"
3116 "txBeginAnon blocked by tlocks low = %d\n"
3117 "calls to txLockAlloc = %d\n"
3118 "tLockAlloc blocked by no free lock = %d\n",
3120 TxStat
.txBegin_barrier
,
3121 TxStat
.txBegin_lockslow
,
3122 TxStat
.txBegin_freetid
,
3124 TxStat
.txBeginAnon_barrier
,
3125 TxStat
.txBeginAnon_lockslow
,
3127 TxStat
.txLockAlloc_freelock
);
3130 *start
= buffer
+ begin
;