patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / jfs / jfs_txnmgr.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
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.
9  * 
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.
14  *
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
18  */
19
20 /*
21  *      jfs_txnmgr.c: transaction manager
22  *
23  * notes:
24  * transaction starts with txBegin() and ends with txCommit()
25  * or txAbort().
26  *
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).
31  *
32  * special cases:
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.
36  *
37  * tlock during write()/mmap() under anonymous transaction (tid = 0):
38  * transferred (?) to transaction at commit time.
39  *
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
43  */
44
45
46 #include <linux/fs.h>
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"
57 #include "jfs_imap.h"
58 #include "jfs_dmap.h"
59 #include "jfs_superblock.h"
60 #include "jfs_debug.h"
61
62 /*
63  *      transaction management structures
64  */
65 static struct {
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 */
78 } TxAnchor;
79
80 int jfs_tlocks_low;             /* Indicates low number of available tlocks */
81
82 #ifdef CONFIG_JFS_STATISTICS
83 struct {
84         uint txBegin;
85         uint txBegin_barrier;
86         uint txBegin_lockslow;
87         uint txBegin_freetid;
88         uint txBeginAnon;
89         uint txBeginAnon_barrier;
90         uint txBeginAnon_lockslow;
91         uint txLockAlloc;
92         uint txLockAlloc_freelock;
93 } TxStat;
94 #endif
95
96 static int nTxBlock = 512;      /* number of transaction blocks */
97 module_param(nTxBlock, int, 0);
98 MODULE_PARM_DESC(nTxBlock,
99                  "Number of transaction blocks (default:512, max:65536)");
100
101 static int nTxLock = 4096;      /* number of transaction locks */
102 module_param(nTxLock, int, 0);
103 MODULE_PARM_DESC(nTxLock,
104                  "Number of transaction locks (default:4096, max:65536)");
105
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 */
111
112
113 /*
114  *      transaction management lock
115  */
116 static spinlock_t jfsTxnLock = SPIN_LOCK_UNLOCKED;
117
118 #define TXN_LOCK()              spin_lock(&jfsTxnLock)
119 #define TXN_UNLOCK()            spin_unlock(&jfsTxnLock)
120
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)
124
125 DECLARE_WAIT_QUEUE_HEAD(jfs_sync_thread_wait);
126 DECLARE_WAIT_QUEUE_HEAD(jfs_commit_thread_wait);
127
128 /*
129  * Retry logic exist outside these macros to protect from spurrious wakeups.
130  */
131 static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
132 {
133         DECLARE_WAITQUEUE(wait, current);
134
135         add_wait_queue(event, &wait);
136         set_current_state(TASK_UNINTERRUPTIBLE);
137         TXN_UNLOCK();
138         schedule();
139         current->state = TASK_RUNNING;
140         remove_wait_queue(event, &wait);
141 }
142
143 #define TXN_SLEEP(event)\
144 {\
145         TXN_SLEEP_DROP_LOCK(event);\
146         TXN_LOCK();\
147 }
148
149 #define TXN_WAKEUP(event) wake_up_all(event)
150
151
152 /*
153  *      statistics
154  */
155 struct {
156         tid_t maxtid;           /* 4: biggest tid ever used */
157         lid_t maxlid;           /* 4: biggest lid ever used */
158         int ntid;               /* 4: # of transactions performed */
159         int nlid;               /* 4: # of tlocks acquired */
160         int waitlock;           /* 4: # of tlock wait */
161 } stattx;
162
163
164 /*
165  * external references
166  */
167 extern int lmGroupCommit(struct jfs_log *, struct tblock *);
168 extern void lmSync(struct jfs_log *);
169 extern int jfs_commit_inode(struct inode *, int);
170 extern int jfs_stop_threads;
171
172 extern struct completion jfsIOwait;
173
174 /*
175  * forward references
176  */
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,
189                 struct commit * cd);
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);
195
196 /*
197  *              transaction block/lock management
198  *              ---------------------------------
199  */
200
201 /*
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.)
205  */
206 static lid_t txLockAlloc(void)
207 {
208         lid_t lid;
209
210         INCREMENT(TxStat.txLockAlloc);
211         if (!TxAnchor.freelock) {
212                 INCREMENT(TxStat.txLockAlloc_freelock);
213         }
214
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");
221                 jfs_tlocks_low = 1;
222                 wake_up(&jfs_sync_thread_wait);
223         }
224
225         return lid;
226 }
227
228 static void txLockFree(lid_t lid)
229 {
230         TxLock[lid].next = TxAnchor.freelock;
231         TxAnchor.freelock = lid;
232         TxAnchor.tlocksInUse--;
233         if (jfs_tlocks_low && (TxAnchor.tlocksInUse < TxLockLWM)) {
234                 jfs_info("txLockFree jfs_tlocks_low no more");
235                 jfs_tlocks_low = 0;
236                 TXN_WAKEUP(&TxAnchor.lowlockwait);
237         }
238         TXN_WAKEUP(&TxAnchor.freelockwait);
239 }
240
241 /*
242  * NAME:        txInit()
243  *
244  * FUNCTION:    initialize transaction management structures
245  *
246  * RETURN:
247  *
248  * serialization: single thread at jfs_init()
249  */
250 int txInit(void)
251 {
252         int k, size;
253
254         /* Verify tunable parameters */
255         if (nTxBlock < 16)
256                 nTxBlock = 16;  /* No one should set it this low */
257         if (nTxBlock > 65536)
258                 nTxBlock = 65536;
259         if (nTxLock < 256)
260                 nTxLock = 256;  /* No one should set it this low */
261         if (nTxLock > 65536)
262                 nTxLock = 65536;
263         /*
264          * initialize transaction block (tblock) table
265          *
266          * transaction id (tid) = tblock index
267          * tid = 0 is reserved.
268          */
269         TxLockLWM = (nTxLock * 4) / 10;
270         TxLockHWM = (nTxLock * 8) / 10;
271         TxLockVHWM = (nTxLock * 9) / 10;
272
273         size = sizeof(struct tblock) * nTxBlock;
274         TxBlock = (struct tblock *) vmalloc(size);
275         if (TxBlock == NULL)
276                 return -ENOMEM;
277
278         for (k = 1; k < nTxBlock - 1; k++) {
279                 TxBlock[k].next = k + 1;
280                 init_waitqueue_head(&TxBlock[k].gcwait);
281                 init_waitqueue_head(&TxBlock[k].waitor);
282         }
283         TxBlock[k].next = 0;
284         init_waitqueue_head(&TxBlock[k].gcwait);
285         init_waitqueue_head(&TxBlock[k].waitor);
286
287         TxAnchor.freetid = 1;
288         init_waitqueue_head(&TxAnchor.freewait);
289
290         stattx.maxtid = 1;      /* statistics */
291
292         /*
293          * initialize transaction lock (tlock) table
294          *
295          * transaction lock id = tlock index
296          * tlock id = 0 is reserved.
297          */
298         size = sizeof(struct tlock) * nTxLock;
299         TxLock = (struct tlock *) vmalloc(size);
300         if (TxLock == NULL) {
301                 vfree(TxBlock);
302                 return -ENOMEM;
303         }
304
305         /* initialize tlock table */
306         for (k = 1; k < nTxLock - 1; k++)
307                 TxLock[k].next = k + 1;
308         TxLock[k].next = 0;
309         init_waitqueue_head(&TxAnchor.freelockwait);
310         init_waitqueue_head(&TxAnchor.lowlockwait);
311
312         TxAnchor.freelock = 1;
313         TxAnchor.tlocksInUse = 0;
314         INIT_LIST_HEAD(&TxAnchor.anon_list);
315         INIT_LIST_HEAD(&TxAnchor.anon_list2);
316
317         LAZY_LOCK_INIT();
318         INIT_LIST_HEAD(&TxAnchor.unlock_queue);
319
320         stattx.maxlid = 1;      /* statistics */
321
322         return 0;
323 }
324
325 /*
326  * NAME:        txExit()
327  *
328  * FUNCTION:    clean up when module is unloaded
329  */
330 void txExit(void)
331 {
332         vfree(TxLock);
333         TxLock = 0;
334         vfree(TxBlock);
335         TxBlock = 0;
336 }
337
338
339 /*
340  * NAME:        txBegin()
341  *
342  * FUNCTION:    start a transaction.
343  *
344  * PARAMETER:   sb      - superblock
345  *              flag    - force for nested tx;
346  *
347  * RETURN:      tid     - transaction id
348  *
349  * note: flag force allows to start tx for nested tx
350  * to prevent deadlock on logsync barrier;
351  */
352 tid_t txBegin(struct super_block *sb, int flag)
353 {
354         tid_t t;
355         struct tblock *tblk;
356         struct jfs_log *log;
357
358         jfs_info("txBegin: flag = 0x%x", flag);
359         log = JFS_SBI(sb)->log;
360
361         TXN_LOCK();
362
363         INCREMENT(TxStat.txBegin);
364
365       retry:
366         if (!(flag & COMMIT_FORCE)) {
367                 /*
368                  * synchronize with logsync barrier
369                  */
370                 if (test_bit(log_SYNCBARRIER, &log->flag) ||
371                     test_bit(log_QUIESCE, &log->flag)) {
372                         INCREMENT(TxStat.txBegin_barrier);
373                         TXN_SLEEP(&log->syncwait);
374                         goto retry;
375                 }
376         }
377         if (flag == 0) {
378                 /*
379                  * Don't begin transaction if we're getting starved for tlocks
380                  * unless COMMIT_FORCE or COMMIT_INODE (which may ultimately
381                  * free tlocks)
382                  */
383                 if (TxAnchor.tlocksInUse > TxLockVHWM) {
384                         INCREMENT(TxStat.txBegin_lockslow);
385                         TXN_SLEEP(&TxAnchor.lowlockwait);
386                         goto retry;
387                 }
388         }
389
390         /*
391          * allocate transaction id/block
392          */
393         if ((t = TxAnchor.freetid) == 0) {
394                 jfs_info("txBegin: waiting for free tid");
395                 INCREMENT(TxStat.txBegin_freetid);
396                 TXN_SLEEP(&TxAnchor.freewait);
397                 goto retry;
398         }
399
400         tblk = tid_to_tblock(t);
401
402         if ((tblk->next == 0) && !(flag & COMMIT_FORCE)) {
403                 /* Don't let a non-forced transaction take the last tblk */
404                 jfs_info("txBegin: waiting for free tid");
405                 INCREMENT(TxStat.txBegin_freetid);
406                 TXN_SLEEP(&TxAnchor.freewait);
407                 goto retry;
408         }
409
410         TxAnchor.freetid = tblk->next;
411
412         /*
413          * initialize transaction
414          */
415
416         /*
417          * We can't zero the whole thing or we screw up another thread being
418          * awakened after sleeping on tblk->waitor
419          *
420          * memset(tblk, 0, sizeof(struct tblock));
421          */
422         tblk->next = tblk->last = tblk->xflag = tblk->flag = tblk->lsn = 0;
423
424         tblk->sb = sb;
425         ++log->logtid;
426         tblk->logtid = log->logtid;
427
428         ++log->active;
429
430         HIGHWATERMARK(stattx.maxtid, t);        /* statistics */
431         INCREMENT(stattx.ntid); /* statistics */
432
433         TXN_UNLOCK();
434
435         jfs_info("txBegin: returning tid = %d", t);
436
437         return t;
438 }
439
440
441 /*
442  * NAME:        txBeginAnon()
443  *
444  * FUNCTION:    start an anonymous transaction.
445  *              Blocks if logsync or available tlocks are low to prevent
446  *              anonymous tlocks from depleting supply.
447  *
448  * PARAMETER:   sb      - superblock
449  *
450  * RETURN:      none
451  */
452 void txBeginAnon(struct super_block *sb)
453 {
454         struct jfs_log *log;
455
456         log = JFS_SBI(sb)->log;
457
458         TXN_LOCK();
459         INCREMENT(TxStat.txBeginAnon);
460
461       retry:
462         /*
463          * synchronize with logsync barrier
464          */
465         if (test_bit(log_SYNCBARRIER, &log->flag) ||
466             test_bit(log_QUIESCE, &log->flag)) {
467                 INCREMENT(TxStat.txBeginAnon_barrier);
468                 TXN_SLEEP(&log->syncwait);
469                 goto retry;
470         }
471
472         /*
473          * Don't begin transaction if we're getting starved for tlocks
474          */
475         if (TxAnchor.tlocksInUse > TxLockVHWM) {
476                 INCREMENT(TxStat.txBeginAnon_lockslow);
477                 TXN_SLEEP(&TxAnchor.lowlockwait);
478                 goto retry;
479         }
480         TXN_UNLOCK();
481 }
482
483
484 /*
485  *      txEnd()
486  *
487  * function: free specified transaction block.
488  *
489  *      logsync barrier processing:
490  *
491  * serialization:
492  */
493 void txEnd(tid_t tid)
494 {
495         struct tblock *tblk = tid_to_tblock(tid);
496         struct jfs_log *log;
497
498         jfs_info("txEnd: tid = %d", tid);
499         TXN_LOCK();
500
501         /*
502          * wakeup transactions waiting on the page locked
503          * by the current transaction
504          */
505         TXN_WAKEUP(&tblk->waitor);
506
507         log = JFS_SBI(tblk->sb)->log;
508
509         /*
510          * Lazy commit thread can't free this guy until we mark it UNLOCKED,
511          * otherwise, we would be left with a transaction that may have been
512          * reused.
513          *
514          * Lazy commit thread will turn off tblkGC_LAZY before calling this
515          * routine.
516          */
517         if (tblk->flag & tblkGC_LAZY) {
518                 jfs_info("txEnd called w/lazy tid: %d, tblk = 0x%p", tid, tblk);
519                 TXN_UNLOCK();
520
521                 spin_lock_irq(&log->gclock);    // LOGGC_LOCK
522                 tblk->flag |= tblkGC_UNLOCKED;
523                 spin_unlock_irq(&log->gclock);  // LOGGC_UNLOCK
524                 return;
525         }
526
527         jfs_info("txEnd: tid: %d, tblk = 0x%p", tid, tblk);
528
529         assert(tblk->next == 0);
530
531         /*
532          * insert tblock back on freelist
533          */
534         tblk->next = TxAnchor.freetid;
535         TxAnchor.freetid = tid;
536
537         /*
538          * mark the tblock not active
539          */
540         if (--log->active == 0) {
541                 clear_bit(log_FLUSH, &log->flag);
542
543                 /*
544                  * synchronize with logsync barrier
545                  */
546                 if (test_bit(log_SYNCBARRIER, &log->flag)) {
547                         /* forward log syncpt */
548                         /* lmSync(log); */
549
550                         jfs_info("log barrier off: 0x%x", log->lsn);
551
552                         /* enable new transactions start */
553                         clear_bit(log_SYNCBARRIER, &log->flag);
554
555                         /* wakeup all waitors for logsync barrier */
556                         TXN_WAKEUP(&log->syncwait);
557                 }
558         }
559
560         /*
561          * wakeup all waitors for a free tblock
562          */
563         TXN_WAKEUP(&TxAnchor.freewait);
564
565         TXN_UNLOCK();
566 }
567
568
569 /*
570  *      txLock()
571  *
572  * function: acquire a transaction lock on the specified <mp>
573  *
574  * parameter:
575  *
576  * return:      transaction lock id
577  *
578  * serialization:
579  */
580 struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
581                      int type)
582 {
583         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
584         int dir_xtree = 0;
585         lid_t lid;
586         tid_t xtid;
587         struct tlock *tlck;
588         struct xtlock *xtlck;
589         struct linelock *linelock;
590         xtpage_t *p;
591         struct tblock *tblk;
592
593         TXN_LOCK();
594
595         if (S_ISDIR(ip->i_mode) && (type & tlckXTREE) &&
596             !(mp->xflag & COMMIT_PAGE)) {
597                 /*
598                  * Directory inode is special.  It can have both an xtree tlock
599                  * and a dtree tlock associated with it.
600                  */
601                 dir_xtree = 1;
602                 lid = jfs_ip->xtlid;
603         } else
604                 lid = mp->lid;
605
606         /* is page not locked by a transaction ? */
607         if (lid == 0)
608                 goto allocateLock;
609
610         jfs_info("txLock: tid:%d ip:0x%p mp:0x%p lid:%d", tid, ip, mp, lid);
611
612         /* is page locked by the requester transaction ? */
613         tlck = lid_to_tlock(lid);
614         if ((xtid = tlck->tid) == tid)
615                 goto grantLock;
616
617         /*
618          * is page locked by anonymous transaction/lock ?
619          *
620          * (page update without transaction (i.e., file write) is
621          * locked under anonymous transaction tid = 0:
622          * anonymous tlocks maintained on anonymous tlock list of
623          * the inode of the page and available to all anonymous
624          * transactions until txCommit() time at which point
625          * they are transferred to the transaction tlock list of
626          * the commiting transaction of the inode)
627          */
628         if (xtid == 0) {
629                 tlck->tid = tid;
630                 tblk = tid_to_tblock(tid);
631                 /*
632                  * The order of the tlocks in the transaction is important
633                  * (during truncate, child xtree pages must be freed before
634                  * parent's tlocks change the working map).
635                  * Take tlock off anonymous list and add to tail of
636                  * transaction list
637                  *
638                  * Note:  We really need to get rid of the tid & lid and
639                  * use list_head's.  This code is getting UGLY!
640                  */
641                 if (jfs_ip->atlhead == lid) {
642                         if (jfs_ip->atltail == lid) {
643                                 /* only anonymous txn.
644                                  * Remove from anon_list
645                                  */
646                                 list_del_init(&jfs_ip->anon_inode_list);
647                         }
648                         jfs_ip->atlhead = tlck->next;
649                 } else {
650                         lid_t last;
651                         for (last = jfs_ip->atlhead;
652                              lid_to_tlock(last)->next != lid;
653                              last = lid_to_tlock(last)->next) {
654                                 assert(last);
655                         }
656                         lid_to_tlock(last)->next = tlck->next;
657                         if (jfs_ip->atltail == lid)
658                                 jfs_ip->atltail = last;
659                 }
660
661                 /* insert the tlock at tail of transaction tlock list */
662
663                 if (tblk->next)
664                         lid_to_tlock(tblk->last)->next = lid;
665                 else
666                         tblk->next = lid;
667                 tlck->next = 0;
668                 tblk->last = lid;
669
670                 goto grantLock;
671         }
672
673         goto waitLock;
674
675         /*
676          * allocate a tlock
677          */
678       allocateLock:
679         lid = txLockAlloc();
680         tlck = lid_to_tlock(lid);
681
682         /*
683          * initialize tlock
684          */
685         tlck->tid = tid;
686
687         /* mark tlock for meta-data page */
688         if (mp->xflag & COMMIT_PAGE) {
689
690                 tlck->flag = tlckPAGELOCK;
691
692                 /* mark the page dirty and nohomeok */
693                 mark_metapage_dirty(mp);
694                 atomic_inc(&mp->nohomeok);
695
696                 jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
697                          mp, atomic_read(&mp->nohomeok), tid, tlck);
698
699                 /* if anonymous transaction, and buffer is on the group
700                  * commit synclist, mark inode to show this.  This will
701                  * prevent the buffer from being marked nohomeok for too
702                  * long a time.
703                  */
704                 if ((tid == 0) && mp->lsn)
705                         set_cflag(COMMIT_Synclist, ip);
706         }
707         /* mark tlock for in-memory inode */
708         else
709                 tlck->flag = tlckINODELOCK;
710
711         tlck->type = 0;
712
713         /* bind the tlock and the page */
714         tlck->ip = ip;
715         tlck->mp = mp;
716         if (dir_xtree)
717                 jfs_ip->xtlid = lid;
718         else
719                 mp->lid = lid;
720
721         /*
722          * enqueue transaction lock to transaction/inode
723          */
724         /* insert the tlock at tail of transaction tlock list */
725         if (tid) {
726                 tblk = tid_to_tblock(tid);
727                 if (tblk->next)
728                         lid_to_tlock(tblk->last)->next = lid;
729                 else
730                         tblk->next = lid;
731                 tlck->next = 0;
732                 tblk->last = lid;
733         }
734         /* anonymous transaction:
735          * insert the tlock at head of inode anonymous tlock list
736          */
737         else {
738                 tlck->next = jfs_ip->atlhead;
739                 jfs_ip->atlhead = lid;
740                 if (tlck->next == 0) {
741                         /* This inode's first anonymous transaction */
742                         jfs_ip->atltail = lid;
743                         list_add_tail(&jfs_ip->anon_inode_list,
744                                       &TxAnchor.anon_list);
745                 }
746         }
747
748         /* initialize type dependent area for linelock */
749         linelock = (struct linelock *) & tlck->lock;
750         linelock->next = 0;
751         linelock->flag = tlckLINELOCK;
752         linelock->maxcnt = TLOCKSHORT;
753         linelock->index = 0;
754
755         switch (type & tlckTYPE) {
756         case tlckDTREE:
757                 linelock->l2linesize = L2DTSLOTSIZE;
758                 break;
759
760         case tlckXTREE:
761                 linelock->l2linesize = L2XTSLOTSIZE;
762
763                 xtlck = (struct xtlock *) linelock;
764                 xtlck->header.offset = 0;
765                 xtlck->header.length = 2;
766
767                 if (type & tlckNEW) {
768                         xtlck->lwm.offset = XTENTRYSTART;
769                 } else {
770                         if (mp->xflag & COMMIT_PAGE)
771                                 p = (xtpage_t *) mp->data;
772                         else
773                                 p = &jfs_ip->i_xtroot;
774                         xtlck->lwm.offset =
775                             le16_to_cpu(p->header.nextindex);
776                 }
777                 xtlck->lwm.length = 0;  /* ! */
778                 xtlck->twm.offset = 0;
779                 xtlck->hwm.offset = 0;
780
781                 xtlck->index = 2;
782                 break;
783
784         case tlckINODE:
785                 linelock->l2linesize = L2INODESLOTSIZE;
786                 break;
787
788         case tlckDATA:
789                 linelock->l2linesize = L2DATASLOTSIZE;
790                 break;
791
792         default:
793                 jfs_err("UFO tlock:0x%p", tlck);
794         }
795
796         /*
797          * update tlock vector
798          */
799       grantLock:
800         tlck->type |= type;
801
802         TXN_UNLOCK();
803
804         return tlck;
805
806         /*
807          * page is being locked by another transaction:
808          */
809       waitLock:
810         /* Only locks on ipimap or ipaimap should reach here */
811         /* assert(jfs_ip->fileset == AGGREGATE_I); */
812         if (jfs_ip->fileset != AGGREGATE_I) {
813                 jfs_err("txLock: trying to lock locked page!");
814                 dump_mem("ip", ip, sizeof(struct inode));
815                 dump_mem("mp", mp, sizeof(struct metapage));
816                 dump_mem("Locker's tblk", tid_to_tblock(tid),
817                          sizeof(struct tblock));
818                 dump_mem("Tlock", tlck, sizeof(struct tlock));
819                 BUG();
820         }
821         INCREMENT(stattx.waitlock);     /* statistics */
822         release_metapage(mp);
823
824         jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
825                  tid, xtid, lid);
826         TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
827         jfs_info("txLock: awakened     tid = %d, lid = %d", tid, lid);
828
829         return NULL;
830 }
831
832
833 /*
834  * NAME:        txRelease()
835  *
836  * FUNCTION:    Release buffers associated with transaction locks, but don't
837  *              mark homeok yet.  The allows other transactions to modify
838  *              buffers, but won't let them go to disk until commit record
839  *              actually gets written.
840  *
841  * PARAMETER:
842  *              tblk    -
843  *
844  * RETURN:      Errors from subroutines.
845  */
846 static void txRelease(struct tblock * tblk)
847 {
848         struct metapage *mp;
849         lid_t lid;
850         struct tlock *tlck;
851
852         TXN_LOCK();
853
854         for (lid = tblk->next; lid; lid = tlck->next) {
855                 tlck = lid_to_tlock(lid);
856                 if ((mp = tlck->mp) != NULL &&
857                     (tlck->type & tlckBTROOT) == 0) {
858                         assert(mp->xflag & COMMIT_PAGE);
859                         mp->lid = 0;
860                 }
861         }
862
863         /*
864          * wakeup transactions waiting on a page locked
865          * by the current transaction
866          */
867         TXN_WAKEUP(&tblk->waitor);
868
869         TXN_UNLOCK();
870 }
871
872
873 /*
874  * NAME:        txUnlock()
875  *
876  * FUNCTION:    Initiates pageout of pages modified by tid in journalled
877  *              objects and frees their lockwords.
878  */
879 static void txUnlock(struct tblock * tblk)
880 {
881         struct tlock *tlck;
882         struct linelock *linelock;
883         lid_t lid, next, llid, k;
884         struct metapage *mp;
885         struct jfs_log *log;
886         int difft, diffp;
887
888         jfs_info("txUnlock: tblk = 0x%p", tblk);
889         log = JFS_SBI(tblk->sb)->log;
890
891         /*
892          * mark page under tlock homeok (its log has been written):
893          */
894         for (lid = tblk->next; lid; lid = next) {
895                 tlck = lid_to_tlock(lid);
896                 next = tlck->next;
897
898                 jfs_info("unlocking lid = %d, tlck = 0x%p", lid, tlck);
899
900                 /* unbind page from tlock */
901                 if ((mp = tlck->mp) != NULL &&
902                     (tlck->type & tlckBTROOT) == 0) {
903                         assert(mp->xflag & COMMIT_PAGE);
904
905                         /* hold buffer
906                          *
907                          * It's possible that someone else has the metapage.
908                          * The only things were changing are nohomeok, which
909                          * is handled atomically, and clsn which is protected
910                          * by the LOGSYNC_LOCK.
911                          */
912                         hold_metapage(mp, 1);
913
914                         assert(atomic_read(&mp->nohomeok) > 0);
915                         atomic_dec(&mp->nohomeok);
916
917                         /* inherit younger/larger clsn */
918                         LOGSYNC_LOCK(log);
919                         if (mp->clsn) {
920                                 logdiff(difft, tblk->clsn, log);
921                                 logdiff(diffp, mp->clsn, log);
922                                 if (difft > diffp)
923                                         mp->clsn = tblk->clsn;
924                         } else
925                                 mp->clsn = tblk->clsn;
926                         LOGSYNC_UNLOCK(log);
927
928                         assert(!(tlck->flag & tlckFREEPAGE));
929
930                         if (tlck->flag & tlckWRITEPAGE) {
931                                 write_metapage(mp);
932                         } else {
933                                 /* release page which has been forced */
934                                 release_metapage(mp);
935                         }
936                 }
937
938                 /* insert tlock, and linelock(s) of the tlock if any,
939                  * at head of freelist
940                  */
941                 TXN_LOCK();
942
943                 llid = ((struct linelock *) & tlck->lock)->next;
944                 while (llid) {
945                         linelock = (struct linelock *) lid_to_tlock(llid);
946                         k = linelock->next;
947                         txLockFree(llid);
948                         llid = k;
949                 }
950                 txLockFree(lid);
951
952                 TXN_UNLOCK();
953         }
954         tblk->next = tblk->last = 0;
955
956         /*
957          * remove tblock from logsynclist
958          * (allocation map pages inherited lsn of tblk and
959          * has been inserted in logsync list at txUpdateMap())
960          */
961         if (tblk->lsn) {
962                 LOGSYNC_LOCK(log);
963                 log->count--;
964                 list_del(&tblk->synclist);
965                 LOGSYNC_UNLOCK(log);
966         }
967 }
968
969
970 /*
971  *      txMaplock()
972  *
973  * function: allocate a transaction lock for freed page/entry;
974  *      for freed page, maplock is used as xtlock/dtlock type;
975  */
976 struct tlock *txMaplock(tid_t tid, struct inode *ip, int type)
977 {
978         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
979         lid_t lid;
980         struct tblock *tblk;
981         struct tlock *tlck;
982         struct maplock *maplock;
983
984         TXN_LOCK();
985
986         /*
987          * allocate a tlock
988          */
989         lid = txLockAlloc();
990         tlck = lid_to_tlock(lid);
991
992         /*
993          * initialize tlock
994          */
995         tlck->tid = tid;
996
997         /* bind the tlock and the object */
998         tlck->flag = tlckINODELOCK;
999         tlck->ip = ip;
1000         tlck->mp = NULL;
1001
1002         tlck->type = type;
1003
1004         /*
1005          * enqueue transaction lock to transaction/inode
1006          */
1007         /* insert the tlock at tail of transaction tlock list */
1008         if (tid) {
1009                 tblk = tid_to_tblock(tid);
1010                 if (tblk->next)
1011                         lid_to_tlock(tblk->last)->next = lid;
1012                 else
1013                         tblk->next = lid;
1014                 tlck->next = 0;
1015                 tblk->last = lid;
1016         }
1017         /* anonymous transaction:
1018          * insert the tlock at head of inode anonymous tlock list
1019          */
1020         else {
1021                 tlck->next = jfs_ip->atlhead;
1022                 jfs_ip->atlhead = lid;
1023                 if (tlck->next == 0) {
1024                         /* This inode's first anonymous transaction */
1025                         jfs_ip->atltail = lid;
1026                         list_add_tail(&jfs_ip->anon_inode_list,
1027                                       &TxAnchor.anon_list);
1028                 }
1029         }
1030
1031         TXN_UNLOCK();
1032
1033         /* initialize type dependent area for maplock */
1034         maplock = (struct maplock *) & tlck->lock;
1035         maplock->next = 0;
1036         maplock->maxcnt = 0;
1037         maplock->index = 0;
1038
1039         return tlck;
1040 }
1041
1042
1043 /*
1044  *      txLinelock()
1045  *
1046  * function: allocate a transaction lock for log vector list
1047  */
1048 struct linelock *txLinelock(struct linelock * tlock)
1049 {
1050         lid_t lid;
1051         struct tlock *tlck;
1052         struct linelock *linelock;
1053
1054         TXN_LOCK();
1055
1056         /* allocate a TxLock structure */
1057         lid = txLockAlloc();
1058         tlck = lid_to_tlock(lid);
1059
1060         TXN_UNLOCK();
1061
1062         /* initialize linelock */
1063         linelock = (struct linelock *) tlck;
1064         linelock->next = 0;
1065         linelock->flag = tlckLINELOCK;
1066         linelock->maxcnt = TLOCKLONG;
1067         linelock->index = 0;
1068
1069         /* append linelock after tlock */
1070         linelock->next = tlock->next;
1071         tlock->next = lid;
1072
1073         return linelock;
1074 }
1075
1076
1077
1078 /*
1079  *              transaction commit management
1080  *              -----------------------------
1081  */
1082
1083 /*
1084  * NAME:        txCommit()
1085  *
1086  * FUNCTION:    commit the changes to the objects specified in
1087  *              clist.  For journalled segments only the
1088  *              changes of the caller are committed, ie by tid.
1089  *              for non-journalled segments the data are flushed to
1090  *              disk and then the change to the disk inode and indirect
1091  *              blocks committed (so blocks newly allocated to the
1092  *              segment will be made a part of the segment atomically).
1093  *
1094  *              all of the segments specified in clist must be in
1095  *              one file system. no more than 6 segments are needed
1096  *              to handle all unix svcs.
1097  *
1098  *              if the i_nlink field (i.e. disk inode link count)
1099  *              is zero, and the type of inode is a regular file or
1100  *              directory, or symbolic link , the inode is truncated
1101  *              to zero length. the truncation is committed but the
1102  *              VM resources are unaffected until it is closed (see
1103  *              iput and iclose).
1104  *
1105  * PARAMETER:
1106  *
1107  * RETURN:
1108  *
1109  * serialization:
1110  *              on entry the inode lock on each segment is assumed
1111  *              to be held.
1112  *
1113  * i/o error:
1114  */
1115 int txCommit(tid_t tid,         /* transaction identifier */
1116              int nip,           /* number of inodes to commit */
1117              struct inode **iplist,     /* list of inode to commit */
1118              int flag)
1119 {
1120         int rc = 0;
1121         struct commit cd;
1122         struct jfs_log *log;
1123         struct tblock *tblk;
1124         struct lrd *lrd;
1125         int lsn;
1126         struct inode *ip;
1127         struct jfs_inode_info *jfs_ip;
1128         int k, n;
1129         ino_t top;
1130         struct super_block *sb;
1131
1132         jfs_info("txCommit, tid = %d, flag = %d", tid, flag);
1133         /* is read-only file system ? */
1134         if (isReadOnly(iplist[0])) {
1135                 rc = -EROFS;
1136                 goto TheEnd;
1137         }
1138
1139         sb = cd.sb = iplist[0]->i_sb;
1140         cd.tid = tid;
1141
1142         if (tid == 0)
1143                 tid = txBegin(sb, 0);
1144         tblk = tid_to_tblock(tid);
1145
1146         /*
1147          * initialize commit structure
1148          */
1149         log = JFS_SBI(sb)->log;
1150         cd.log = log;
1151
1152         /* initialize log record descriptor in commit */
1153         lrd = &cd.lrd;
1154         lrd->logtid = cpu_to_le32(tblk->logtid);
1155         lrd->backchain = 0;
1156
1157         tblk->xflag |= flag;
1158
1159         if ((flag & (COMMIT_FORCE | COMMIT_SYNC)) == 0)
1160                 tblk->xflag |= COMMIT_LAZY;
1161         /*
1162          *      prepare non-journaled objects for commit
1163          *
1164          * flush data pages of non-journaled file
1165          * to prevent the file getting non-initialized disk blocks
1166          * in case of crash.
1167          * (new blocks - )
1168          */
1169         cd.iplist = iplist;
1170         cd.nip = nip;
1171
1172         /*
1173          *      acquire transaction lock on (on-disk) inodes
1174          *
1175          * update on-disk inode from in-memory inode
1176          * acquiring transaction locks for AFTER records
1177          * on the on-disk inode of file object
1178          *
1179          * sort the inodes array by inode number in descending order
1180          * to prevent deadlock when acquiring transaction lock
1181          * of on-disk inodes on multiple on-disk inode pages by
1182          * multiple concurrent transactions
1183          */
1184         for (k = 0; k < cd.nip; k++) {
1185                 top = (cd.iplist[k])->i_ino;
1186                 for (n = k + 1; n < cd.nip; n++) {
1187                         ip = cd.iplist[n];
1188                         if (ip->i_ino > top) {
1189                                 top = ip->i_ino;
1190                                 cd.iplist[n] = cd.iplist[k];
1191                                 cd.iplist[k] = ip;
1192                         }
1193                 }
1194
1195                 ip = cd.iplist[k];
1196                 jfs_ip = JFS_IP(ip);
1197
1198                 /*
1199                  * BUGBUG - This code has temporarily been removed.  The
1200                  * intent is to ensure that any file data is written before
1201                  * the metadata is committed to the journal.  This prevents
1202                  * uninitialized data from appearing in a file after the
1203                  * journal has been replayed.  (The uninitialized data
1204                  * could be sensitive data removed by another user.)
1205                  *
1206                  * The problem now is that we are holding the IWRITELOCK
1207                  * on the inode, and calling filemap_fdatawrite on an
1208                  * unmapped page will cause a deadlock in jfs_get_block.
1209                  *
1210                  * The long term solution is to pare down the use of
1211                  * IWRITELOCK.  We are currently holding it too long.
1212                  * We could also be smarter about which data pages need
1213                  * to be written before the transaction is committed and
1214                  * when we don't need to worry about it at all.
1215                  *
1216                  * if ((!S_ISDIR(ip->i_mode))
1217                  *    && (tblk->flag & COMMIT_DELETE) == 0) {
1218                  *      filemap_fdatawrite(ip->i_mapping);
1219                  *      filemap_fdatawait(ip->i_mapping);
1220                  * }
1221                  */
1222
1223                 /*
1224                  * Mark inode as not dirty.  It will still be on the dirty
1225                  * inode list, but we'll know not to commit it again unless
1226                  * it gets marked dirty again
1227                  */
1228                 clear_cflag(COMMIT_Dirty, ip);
1229
1230                 /* inherit anonymous tlock(s) of inode */
1231                 if (jfs_ip->atlhead) {
1232                         lid_to_tlock(jfs_ip->atltail)->next = tblk->next;
1233                         tblk->next = jfs_ip->atlhead;
1234                         if (!tblk->last)
1235                                 tblk->last = jfs_ip->atltail;
1236                         jfs_ip->atlhead = jfs_ip->atltail = 0;
1237                         TXN_LOCK();
1238                         list_del_init(&jfs_ip->anon_inode_list);
1239                         TXN_UNLOCK();
1240                 }
1241
1242                 /*
1243                  * acquire transaction lock on on-disk inode page
1244                  * (become first tlock of the tblk's tlock list)
1245                  */
1246                 if (((rc = diWrite(tid, ip))))
1247                         goto out;
1248         }
1249
1250         /*
1251          *      write log records from transaction locks
1252          *
1253          * txUpdateMap() resets XAD_NEW in XAD.
1254          */
1255         if ((rc = txLog(log, tblk, &cd)))
1256                 goto TheEnd;
1257
1258         /*
1259          * Ensure that inode isn't reused before
1260          * lazy commit thread finishes processing
1261          */
1262         if (tblk->xflag & COMMIT_DELETE) {
1263                 atomic_inc(&tblk->u.ip->i_count);
1264                 /*
1265                  * Avoid a rare deadlock
1266                  *
1267                  * If the inode is locked, we may be blocked in
1268                  * jfs_commit_inode.  If so, we don't want the
1269                  * lazy_commit thread doing the last iput() on the inode
1270                  * since that may block on the locked inode.  Instead,
1271                  * commit the transaction synchronously, so the last iput
1272                  * will be done by the calling thread (or later)
1273                  */
1274                 if (tblk->u.ip->i_state & I_LOCK)
1275                         tblk->xflag &= ~COMMIT_LAZY;
1276         }
1277
1278         ASSERT((!(tblk->xflag & COMMIT_DELETE)) ||
1279                ((tblk->u.ip->i_nlink == 0) &&
1280                 !test_cflag(COMMIT_Nolink, tblk->u.ip)));
1281
1282         /*
1283          *      write COMMIT log record
1284          */
1285         lrd->type = cpu_to_le16(LOG_COMMIT);
1286         lrd->length = 0;
1287         lsn = lmLog(log, tblk, lrd, NULL);
1288
1289         lmGroupCommit(log, tblk);
1290
1291         /*
1292          *      - transaction is now committed -
1293          */
1294
1295         /*
1296          * force pages in careful update
1297          * (imap addressing structure update)
1298          */
1299         if (flag & COMMIT_FORCE)
1300                 txForce(tblk);
1301
1302         /*
1303          *      update allocation map.
1304          *
1305          * update inode allocation map and inode:
1306          * free pager lock on memory object of inode if any.
1307          * update  block allocation map.
1308          *
1309          * txUpdateMap() resets XAD_NEW in XAD.
1310          */
1311         if (tblk->xflag & COMMIT_FORCE)
1312                 txUpdateMap(tblk);
1313
1314         /*
1315          *      free transaction locks and pageout/free pages
1316          */
1317         txRelease(tblk);
1318
1319         if ((tblk->flag & tblkGC_LAZY) == 0)
1320                 txUnlock(tblk);
1321
1322
1323         /*
1324          *      reset in-memory object state
1325          */
1326         for (k = 0; k < cd.nip; k++) {
1327                 ip = cd.iplist[k];
1328                 jfs_ip = JFS_IP(ip);
1329
1330                 /*
1331                  * reset in-memory inode state
1332                  */
1333                 jfs_ip->bxflag = 0;
1334                 jfs_ip->blid = 0;
1335         }
1336
1337       out:
1338         if (rc != 0)
1339                 txAbort(tid, 1);
1340
1341       TheEnd:
1342         jfs_info("txCommit: tid = %d, returning %d", tid, rc);
1343         return rc;
1344 }
1345
1346
1347 /*
1348  * NAME:        txLog()
1349  *
1350  * FUNCTION:    Writes AFTER log records for all lines modified
1351  *              by tid for segments specified by inodes in comdata.
1352  *              Code assumes only WRITELOCKS are recorded in lockwords.
1353  *
1354  * PARAMETERS:
1355  *
1356  * RETURN :
1357  */
1358 static int txLog(struct jfs_log * log, struct tblock * tblk, struct commit * cd)
1359 {
1360         int rc = 0;
1361         struct inode *ip;
1362         lid_t lid;
1363         struct tlock *tlck;
1364         struct lrd *lrd = &cd->lrd;
1365
1366         /*
1367          * write log record(s) for each tlock of transaction,
1368          */
1369         for (lid = tblk->next; lid; lid = tlck->next) {
1370                 tlck = lid_to_tlock(lid);
1371
1372                 tlck->flag |= tlckLOG;
1373
1374                 /* initialize lrd common */
1375                 ip = tlck->ip;
1376                 lrd->aggregate = cpu_to_le32(JFS_SBI(ip->i_sb)->aggregate);
1377                 lrd->log.redopage.fileset = cpu_to_le32(JFS_IP(ip)->fileset);
1378                 lrd->log.redopage.inode = cpu_to_le32(ip->i_ino);
1379
1380                 /* write log record of page from the tlock */
1381                 switch (tlck->type & tlckTYPE) {
1382                 case tlckXTREE:
1383                         xtLog(log, tblk, lrd, tlck);
1384                         break;
1385
1386                 case tlckDTREE:
1387                         dtLog(log, tblk, lrd, tlck);
1388                         break;
1389
1390                 case tlckINODE:
1391                         diLog(log, tblk, lrd, tlck, cd);
1392                         break;
1393
1394                 case tlckMAP:
1395                         mapLog(log, tblk, lrd, tlck);
1396                         break;
1397
1398                 case tlckDATA:
1399                         dataLog(log, tblk, lrd, tlck);
1400                         break;
1401
1402                 default:
1403                         jfs_err("UFO tlock:0x%p", tlck);
1404                 }
1405         }
1406
1407         return rc;
1408 }
1409
1410
1411 /*
1412  *      diLog()
1413  *
1414  * function:    log inode tlock and format maplock to update bmap;
1415  */
1416 static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1417           struct tlock * tlck, struct commit * cd)
1418 {
1419         int rc = 0;
1420         struct metapage *mp;
1421         pxd_t *pxd;
1422         struct pxd_lock *pxdlock;
1423
1424         mp = tlck->mp;
1425
1426         /* initialize as REDOPAGE record format */
1427         lrd->log.redopage.type = cpu_to_le16(LOG_INODE);
1428         lrd->log.redopage.l2linesize = cpu_to_le16(L2INODESLOTSIZE);
1429
1430         pxd = &lrd->log.redopage.pxd;
1431
1432         /*
1433          *      inode after image
1434          */
1435         if (tlck->type & tlckENTRY) {
1436                 /* log after-image for logredo(): */
1437                 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1438 //              *pxd = mp->cm_pxd;
1439                 PXDaddress(pxd, mp->index);
1440                 PXDlength(pxd,
1441                           mp->logical_size >> tblk->sb->s_blocksize_bits);
1442                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1443
1444                 /* mark page as homeward bound */
1445                 tlck->flag |= tlckWRITEPAGE;
1446         } else if (tlck->type & tlckFREE) {
1447                 /*
1448                  *      free inode extent
1449                  *
1450                  * (pages of the freed inode extent have been invalidated and
1451                  * a maplock for free of the extent has been formatted at
1452                  * txLock() time);
1453                  *
1454                  * the tlock had been acquired on the inode allocation map page
1455                  * (iag) that specifies the freed extent, even though the map
1456                  * page is not itself logged, to prevent pageout of the map
1457                  * page before the log;
1458                  */
1459
1460                 /* log LOG_NOREDOINOEXT of the freed inode extent for
1461                  * logredo() to start NoRedoPage filters, and to update
1462                  * imap and bmap for free of the extent;
1463                  */
1464                 lrd->type = cpu_to_le16(LOG_NOREDOINOEXT);
1465                 /*
1466                  * For the LOG_NOREDOINOEXT record, we need
1467                  * to pass the IAG number and inode extent
1468                  * index (within that IAG) from which the
1469                  * the extent being released.  These have been
1470                  * passed to us in the iplist[1] and iplist[2].
1471                  */
1472                 lrd->log.noredoinoext.iagnum =
1473                     cpu_to_le32((u32) (size_t) cd->iplist[1]);
1474                 lrd->log.noredoinoext.inoext_idx =
1475                     cpu_to_le32((u32) (size_t) cd->iplist[2]);
1476
1477                 pxdlock = (struct pxd_lock *) & tlck->lock;
1478                 *pxd = pxdlock->pxd;
1479                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1480
1481                 /* update bmap */
1482                 tlck->flag |= tlckUPDATEMAP;
1483
1484                 /* mark page as homeward bound */
1485                 tlck->flag |= tlckWRITEPAGE;
1486         } else
1487                 jfs_err("diLog: UFO type tlck:0x%p", tlck);
1488 #ifdef  _JFS_WIP
1489         /*
1490          *      alloc/free external EA extent
1491          *
1492          * a maplock for txUpdateMap() to update bPWMAP for alloc/free
1493          * of the extent has been formatted at txLock() time;
1494          */
1495         else {
1496                 assert(tlck->type & tlckEA);
1497
1498                 /* log LOG_UPDATEMAP for logredo() to update bmap for
1499                  * alloc of new (and free of old) external EA extent;
1500                  */
1501                 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1502                 pxdlock = (struct pxd_lock *) & tlck->lock;
1503                 nlock = pxdlock->index;
1504                 for (i = 0; i < nlock; i++, pxdlock++) {
1505                         if (pxdlock->flag & mlckALLOCPXD)
1506                                 lrd->log.updatemap.type =
1507                                     cpu_to_le16(LOG_ALLOCPXD);
1508                         else
1509                                 lrd->log.updatemap.type =
1510                                     cpu_to_le16(LOG_FREEPXD);
1511                         lrd->log.updatemap.nxd = cpu_to_le16(1);
1512                         lrd->log.updatemap.pxd = pxdlock->pxd;
1513                         lrd->backchain =
1514                             cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1515                 }
1516
1517                 /* update bmap */
1518                 tlck->flag |= tlckUPDATEMAP;
1519         }
1520 #endif                          /* _JFS_WIP */
1521
1522         return rc;
1523 }
1524
1525
1526 /*
1527  *      dataLog()
1528  *
1529  * function:    log data tlock
1530  */
1531 static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1532             struct tlock * tlck)
1533 {
1534         struct metapage *mp;
1535         pxd_t *pxd;
1536
1537         mp = tlck->mp;
1538
1539         /* initialize as REDOPAGE record format */
1540         lrd->log.redopage.type = cpu_to_le16(LOG_DATA);
1541         lrd->log.redopage.l2linesize = cpu_to_le16(L2DATASLOTSIZE);
1542
1543         pxd = &lrd->log.redopage.pxd;
1544
1545         /* log after-image for logredo(): */
1546         lrd->type = cpu_to_le16(LOG_REDOPAGE);
1547
1548         if (JFS_IP(tlck->ip)->next_index < MAX_INLINE_DIRTABLE_ENTRY) {
1549                 /*
1550                  * The table has been truncated, we've must have deleted
1551                  * the last entry, so don't bother logging this
1552                  */
1553                 mp->lid = 0;
1554                 hold_metapage(mp, 0);
1555                 atomic_dec(&mp->nohomeok);
1556                 discard_metapage(mp);
1557                 tlck->mp = 0;
1558                 return 0;
1559         }
1560
1561         PXDaddress(pxd, mp->index);
1562         PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
1563
1564         lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1565
1566         /* mark page as homeward bound */
1567         tlck->flag |= tlckWRITEPAGE;
1568
1569         return 0;
1570 }
1571
1572
1573 /*
1574  *      dtLog()
1575  *
1576  * function:    log dtree tlock and format maplock to update bmap;
1577  */
1578 static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1579            struct tlock * tlck)
1580 {
1581         struct metapage *mp;
1582         struct pxd_lock *pxdlock;
1583         pxd_t *pxd;
1584
1585         mp = tlck->mp;
1586
1587         /* initialize as REDOPAGE/NOREDOPAGE record format */
1588         lrd->log.redopage.type = cpu_to_le16(LOG_DTREE);
1589         lrd->log.redopage.l2linesize = cpu_to_le16(L2DTSLOTSIZE);
1590
1591         pxd = &lrd->log.redopage.pxd;
1592
1593         if (tlck->type & tlckBTROOT)
1594                 lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
1595
1596         /*
1597          *      page extension via relocation: entry insertion;
1598          *      page extension in-place: entry insertion;
1599          *      new right page from page split, reinitialized in-line
1600          *      root from root page split: entry insertion;
1601          */
1602         if (tlck->type & (tlckNEW | tlckEXTEND)) {
1603                 /* log after-image of the new page for logredo():
1604                  * mark log (LOG_NEW) for logredo() to initialize
1605                  * freelist and update bmap for alloc of the new page;
1606                  */
1607                 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1608                 if (tlck->type & tlckEXTEND)
1609                         lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
1610                 else
1611                         lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
1612 //              *pxd = mp->cm_pxd;
1613                 PXDaddress(pxd, mp->index);
1614                 PXDlength(pxd,
1615                           mp->logical_size >> tblk->sb->s_blocksize_bits);
1616                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1617
1618                 /* format a maplock for txUpdateMap() to update bPMAP for
1619                  * alloc of the new page;
1620                  */
1621                 if (tlck->type & tlckBTROOT)
1622                         return;
1623                 tlck->flag |= tlckUPDATEMAP;
1624                 pxdlock = (struct pxd_lock *) & tlck->lock;
1625                 pxdlock->flag = mlckALLOCPXD;
1626                 pxdlock->pxd = *pxd;
1627
1628                 pxdlock->index = 1;
1629
1630                 /* mark page as homeward bound */
1631                 tlck->flag |= tlckWRITEPAGE;
1632                 return;
1633         }
1634
1635         /*
1636          *      entry insertion/deletion,
1637          *      sibling page link update (old right page before split);
1638          */
1639         if (tlck->type & (tlckENTRY | tlckRELINK)) {
1640                 /* log after-image for logredo(): */
1641                 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1642                 PXDaddress(pxd, mp->index);
1643                 PXDlength(pxd,
1644                           mp->logical_size >> tblk->sb->s_blocksize_bits);
1645                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1646
1647                 /* mark page as homeward bound */
1648                 tlck->flag |= tlckWRITEPAGE;
1649                 return;
1650         }
1651
1652         /*
1653          *      page deletion: page has been invalidated
1654          *      page relocation: source extent
1655          *
1656          *      a maplock for free of the page has been formatted
1657          *      at txLock() time);
1658          */
1659         if (tlck->type & (tlckFREE | tlckRELOCATE)) {
1660                 /* log LOG_NOREDOPAGE of the deleted page for logredo()
1661                  * to start NoRedoPage filter and to update bmap for free
1662                  * of the deletd page
1663                  */
1664                 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
1665                 pxdlock = (struct pxd_lock *) & tlck->lock;
1666                 *pxd = pxdlock->pxd;
1667                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1668
1669                 /* a maplock for txUpdateMap() for free of the page
1670                  * has been formatted at txLock() time;
1671                  */
1672                 tlck->flag |= tlckUPDATEMAP;
1673         }
1674         return;
1675 }
1676
1677
1678 /*
1679  *      xtLog()
1680  *
1681  * function:    log xtree tlock and format maplock to update bmap;
1682  */
1683 static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
1684            struct tlock * tlck)
1685 {
1686         struct inode *ip;
1687         struct metapage *mp;
1688         xtpage_t *p;
1689         struct xtlock *xtlck;
1690         struct maplock *maplock;
1691         struct xdlistlock *xadlock;
1692         struct pxd_lock *pxdlock;
1693         pxd_t *pxd;
1694         int next, lwm, hwm;
1695
1696         ip = tlck->ip;
1697         mp = tlck->mp;
1698
1699         /* initialize as REDOPAGE/NOREDOPAGE record format */
1700         lrd->log.redopage.type = cpu_to_le16(LOG_XTREE);
1701         lrd->log.redopage.l2linesize = cpu_to_le16(L2XTSLOTSIZE);
1702
1703         pxd = &lrd->log.redopage.pxd;
1704
1705         if (tlck->type & tlckBTROOT) {
1706                 lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
1707                 p = &JFS_IP(ip)->i_xtroot;
1708                 if (S_ISDIR(ip->i_mode))
1709                         lrd->log.redopage.type |=
1710                             cpu_to_le16(LOG_DIR_XTREE);
1711         } else
1712                 p = (xtpage_t *) mp->data;
1713         next = le16_to_cpu(p->header.nextindex);
1714
1715         xtlck = (struct xtlock *) & tlck->lock;
1716
1717         maplock = (struct maplock *) & tlck->lock;
1718         xadlock = (struct xdlistlock *) maplock;
1719
1720         /*
1721          *      entry insertion/extension;
1722          *      sibling page link update (old right page before split);
1723          */
1724         if (tlck->type & (tlckNEW | tlckGROW | tlckRELINK)) {
1725                 /* log after-image for logredo():
1726                  * logredo() will update bmap for alloc of new/extended
1727                  * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1728                  * after-image of XADlist;
1729                  * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1730                  * applying the after-image to the meta-data page.
1731                  */
1732                 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1733 //              *pxd = mp->cm_pxd;
1734                 PXDaddress(pxd, mp->index);
1735                 PXDlength(pxd,
1736                           mp->logical_size >> tblk->sb->s_blocksize_bits);
1737                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1738
1739                 /* format a maplock for txUpdateMap() to update bPMAP
1740                  * for alloc of new/extended extents of XAD[lwm:next)
1741                  * from the page itself;
1742                  * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
1743                  */
1744                 lwm = xtlck->lwm.offset;
1745                 if (lwm == 0)
1746                         lwm = XTPAGEMAXSLOT;
1747
1748                 if (lwm == next)
1749                         goto out;
1750                 if (lwm > next) {
1751                         jfs_err("xtLog: lwm > next\n");
1752                         goto out;
1753                 }
1754                 tlck->flag |= tlckUPDATEMAP;
1755                 xadlock->flag = mlckALLOCXADLIST;
1756                 xadlock->count = next - lwm;
1757                 if ((xadlock->count <= 2) && (tblk->xflag & COMMIT_LAZY)) {
1758                         int i;
1759                         /*
1760                          * Lazy commit may allow xtree to be modified before
1761                          * txUpdateMap runs.  Copy xad into linelock to
1762                          * preserve correct data.
1763                          */
1764                         xadlock->xdlist = &xtlck->pxdlock;
1765                         memcpy(xadlock->xdlist, &p->xad[lwm],
1766                                sizeof(xad_t) * xadlock->count);
1767
1768                         for (i = 0; i < xadlock->count; i++)
1769                                 p->xad[lwm + i].flag &=
1770                                     ~(XAD_NEW | XAD_EXTENDED);
1771                 } else {
1772                         /*
1773                          * xdlist will point to into inode's xtree, ensure
1774                          * that transaction is not committed lazily.
1775                          */
1776                         xadlock->xdlist = &p->xad[lwm];
1777                         tblk->xflag &= ~COMMIT_LAZY;
1778                 }
1779                 jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d "
1780                          "count:%d", tlck->ip, mp, tlck, lwm, xadlock->count);
1781
1782                 maplock->index = 1;
1783
1784               out:
1785                 /* mark page as homeward bound */
1786                 tlck->flag |= tlckWRITEPAGE;
1787
1788                 return;
1789         }
1790
1791         /*
1792          *      page deletion: file deletion/truncation (ref. xtTruncate())
1793          *
1794          * (page will be invalidated after log is written and bmap
1795          * is updated from the page);
1796          */
1797         if (tlck->type & tlckFREE) {
1798                 /* LOG_NOREDOPAGE log for NoRedoPage filter:
1799                  * if page free from file delete, NoRedoFile filter from
1800                  * inode image of zero link count will subsume NoRedoPage
1801                  * filters for each page;
1802                  * if page free from file truncattion, write NoRedoPage
1803                  * filter;
1804                  *
1805                  * upadte of block allocation map for the page itself:
1806                  * if page free from deletion and truncation, LOG_UPDATEMAP
1807                  * log for the page itself is generated from processing
1808                  * its parent page xad entries;
1809                  */
1810                 /* if page free from file truncation, log LOG_NOREDOPAGE
1811                  * of the deleted page for logredo() to start NoRedoPage
1812                  * filter for the page;
1813                  */
1814                 if (tblk->xflag & COMMIT_TRUNCATE) {
1815                         /* write NOREDOPAGE for the page */
1816                         lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
1817                         PXDaddress(pxd, mp->index);
1818                         PXDlength(pxd,
1819                                   mp->logical_size >> tblk->sb->
1820                                   s_blocksize_bits);
1821                         lrd->backchain =
1822                             cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1823
1824                         if (tlck->type & tlckBTROOT) {
1825                                 /* Empty xtree must be logged */
1826                                 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1827                                 lrd->backchain =
1828                                     cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1829                         }
1830                 }
1831
1832                 /* init LOG_UPDATEMAP of the freed extents
1833                  * XAD[XTENTRYSTART:hwm) from the deleted page itself
1834                  * for logredo() to update bmap;
1835                  */
1836                 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1837                 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEXADLIST);
1838                 xtlck = (struct xtlock *) & tlck->lock;
1839                 hwm = xtlck->hwm.offset;
1840                 lrd->log.updatemap.nxd =
1841                     cpu_to_le16(hwm - XTENTRYSTART + 1);
1842                 /* reformat linelock for lmLog() */
1843                 xtlck->header.offset = XTENTRYSTART;
1844                 xtlck->header.length = hwm - XTENTRYSTART + 1;
1845                 xtlck->index = 1;
1846                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1847
1848                 /* format a maplock for txUpdateMap() to update bmap
1849                  * to free extents of XAD[XTENTRYSTART:hwm) from the
1850                  * deleted page itself;
1851                  */
1852                 tlck->flag |= tlckUPDATEMAP;
1853                 xadlock->flag = mlckFREEXADLIST;
1854                 xadlock->count = hwm - XTENTRYSTART + 1;
1855                 if ((xadlock->count <= 2) && (tblk->xflag & COMMIT_LAZY)) {
1856                         /*
1857                          * Lazy commit may allow xtree to be modified before
1858                          * txUpdateMap runs.  Copy xad into linelock to
1859                          * preserve correct data.
1860                          */
1861                         xadlock->xdlist = &xtlck->pxdlock;
1862                         memcpy(xadlock->xdlist, &p->xad[XTENTRYSTART],
1863                                sizeof(xad_t) * xadlock->count);
1864                 } else {
1865                         /*
1866                          * xdlist will point to into inode's xtree, ensure
1867                          * that transaction is not committed lazily.
1868                          */
1869                         xadlock->xdlist = &p->xad[XTENTRYSTART];
1870                         tblk->xflag &= ~COMMIT_LAZY;
1871                 }
1872                 jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
1873                          tlck->ip, mp, xadlock->count);
1874
1875                 maplock->index = 1;
1876
1877                 /* mark page as invalid */
1878                 if (((tblk->xflag & COMMIT_PWMAP) || S_ISDIR(ip->i_mode))
1879                     && !(tlck->type & tlckBTROOT))
1880                         tlck->flag |= tlckFREEPAGE;
1881                 /*
1882                    else (tblk->xflag & COMMIT_PMAP)
1883                    ? release the page;
1884                  */
1885                 return;
1886         }
1887
1888         /*
1889          *      page/entry truncation: file truncation (ref. xtTruncate())
1890          *
1891          *     |----------+------+------+---------------|
1892          *                |      |      |
1893          *                |      |     hwm - hwm before truncation
1894          *                |     next - truncation point
1895          *               lwm - lwm before truncation
1896          * header ?
1897          */
1898         if (tlck->type & tlckTRUNCATE) {
1899                 pxd_t tpxd;     /* truncated extent of xad */
1900                 int twm;
1901
1902                 /*
1903                  * For truncation the entire linelock may be used, so it would
1904                  * be difficult to store xad list in linelock itself.
1905                  * Therefore, we'll just force transaction to be committed
1906                  * synchronously, so that xtree pages won't be changed before
1907                  * txUpdateMap runs.
1908                  */
1909                 tblk->xflag &= ~COMMIT_LAZY;
1910                 lwm = xtlck->lwm.offset;
1911                 if (lwm == 0)
1912                         lwm = XTPAGEMAXSLOT;
1913                 hwm = xtlck->hwm.offset;
1914                 twm = xtlck->twm.offset;
1915
1916                 /*
1917                  *      write log records
1918                  */
1919                 /* log after-image for logredo():
1920                  *
1921                  * logredo() will update bmap for alloc of new/extended
1922                  * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
1923                  * after-image of XADlist;
1924                  * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
1925                  * applying the after-image to the meta-data page.
1926                  */
1927                 lrd->type = cpu_to_le16(LOG_REDOPAGE);
1928                 PXDaddress(pxd, mp->index);
1929                 PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
1930                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1931
1932                 /*
1933                  * truncate entry XAD[twm == next - 1]:
1934                  */
1935                 if (twm == next - 1) {
1936                         /* init LOG_UPDATEMAP for logredo() to update bmap for
1937                          * free of truncated delta extent of the truncated
1938                          * entry XAD[next - 1]:
1939                          * (xtlck->pxdlock = truncated delta extent);
1940                          */
1941                         pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
1942                         /* assert(pxdlock->type & tlckTRUNCATE); */
1943                         lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1944                         lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
1945                         lrd->log.updatemap.nxd = cpu_to_le16(1);
1946                         lrd->log.updatemap.pxd = pxdlock->pxd;
1947                         tpxd = pxdlock->pxd;    /* save to format maplock */
1948                         lrd->backchain =
1949                             cpu_to_le32(lmLog(log, tblk, lrd, NULL));
1950                 }
1951
1952                 /*
1953                  * free entries XAD[next:hwm]:
1954                  */
1955                 if (hwm >= next) {
1956                         /* init LOG_UPDATEMAP of the freed extents
1957                          * XAD[next:hwm] from the deleted page itself
1958                          * for logredo() to update bmap;
1959                          */
1960                         lrd->type = cpu_to_le16(LOG_UPDATEMAP);
1961                         lrd->log.updatemap.type =
1962                             cpu_to_le16(LOG_FREEXADLIST);
1963                         xtlck = (struct xtlock *) & tlck->lock;
1964                         hwm = xtlck->hwm.offset;
1965                         lrd->log.updatemap.nxd =
1966                             cpu_to_le16(hwm - next + 1);
1967                         /* reformat linelock for lmLog() */
1968                         xtlck->header.offset = next;
1969                         xtlck->header.length = hwm - next + 1;
1970                         xtlck->index = 1;
1971                         lrd->backchain =
1972                             cpu_to_le32(lmLog(log, tblk, lrd, tlck));
1973                 }
1974
1975                 /*
1976                  *      format maplock(s) for txUpdateMap() to update bmap
1977                  */
1978                 maplock->index = 0;
1979
1980                 /*
1981                  * allocate entries XAD[lwm:next):
1982                  */
1983                 if (lwm < next) {
1984                         /* format a maplock for txUpdateMap() to update bPMAP
1985                          * for alloc of new/extended extents of XAD[lwm:next)
1986                          * from the page itself;
1987                          * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
1988                          */
1989                         tlck->flag |= tlckUPDATEMAP;
1990                         xadlock->flag = mlckALLOCXADLIST;
1991                         xadlock->count = next - lwm;
1992                         xadlock->xdlist = &p->xad[lwm];
1993
1994                         jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d "
1995                                  "lwm:%d next:%d",
1996                                  tlck->ip, mp, xadlock->count, lwm, next);
1997                         maplock->index++;
1998                         xadlock++;
1999                 }
2000
2001                 /*
2002                  * truncate entry XAD[twm == next - 1]:
2003                  */
2004                 if (twm == next - 1) {
2005                         struct pxd_lock *pxdlock;
2006
2007                         /* format a maplock for txUpdateMap() to update bmap
2008                          * to free truncated delta extent of the truncated
2009                          * entry XAD[next - 1];
2010                          * (xtlck->pxdlock = truncated delta extent);
2011                          */
2012                         tlck->flag |= tlckUPDATEMAP;
2013                         pxdlock = (struct pxd_lock *) xadlock;
2014                         pxdlock->flag = mlckFREEPXD;
2015                         pxdlock->count = 1;
2016                         pxdlock->pxd = tpxd;
2017
2018                         jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d "
2019                                  "hwm:%d", ip, mp, pxdlock->count, hwm);
2020                         maplock->index++;
2021                         xadlock++;
2022                 }
2023
2024                 /*
2025                  * free entries XAD[next:hwm]:
2026                  */
2027                 if (hwm >= next) {
2028                         /* format a maplock for txUpdateMap() to update bmap
2029                          * to free extents of XAD[next:hwm] from thedeleted
2030                          * page itself;
2031                          */
2032                         tlck->flag |= tlckUPDATEMAP;
2033                         xadlock->flag = mlckFREEXADLIST;
2034                         xadlock->count = hwm - next + 1;
2035                         xadlock->xdlist = &p->xad[next];
2036
2037                         jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d "
2038                                  "next:%d hwm:%d",
2039                                  tlck->ip, mp, xadlock->count, next, hwm);
2040                         maplock->index++;
2041                 }
2042
2043                 /* mark page as homeward bound */
2044                 tlck->flag |= tlckWRITEPAGE;
2045         }
2046         return;
2047 }
2048
2049
2050 /*
2051  *      mapLog()
2052  *
2053  * function:    log from maplock of freed data extents;
2054  */
2055 void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
2056             struct tlock * tlck)
2057 {
2058         struct pxd_lock *pxdlock;
2059         int i, nlock;
2060         pxd_t *pxd;
2061
2062         /*
2063          *      page relocation: free the source page extent
2064          *
2065          * a maplock for txUpdateMap() for free of the page
2066          * has been formatted at txLock() time saving the src
2067          * relocated page address;
2068          */
2069         if (tlck->type & tlckRELOCATE) {
2070                 /* log LOG_NOREDOPAGE of the old relocated page
2071                  * for logredo() to start NoRedoPage filter;
2072                  */
2073                 lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
2074                 pxdlock = (struct pxd_lock *) & tlck->lock;
2075                 pxd = &lrd->log.redopage.pxd;
2076                 *pxd = pxdlock->pxd;
2077                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2078
2079                 /* (N.B. currently, logredo() does NOT update bmap
2080                  * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
2081                  * if page free from relocation, LOG_UPDATEMAP log is
2082                  * specifically generated now for logredo()
2083                  * to update bmap for free of src relocated page;
2084                  * (new flag LOG_RELOCATE may be introduced which will
2085                  * inform logredo() to start NORedoPage filter and also
2086                  * update block allocation map at the same time, thus
2087                  * avoiding an extra log write);
2088                  */
2089                 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2090                 lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
2091                 lrd->log.updatemap.nxd = cpu_to_le16(1);
2092                 lrd->log.updatemap.pxd = pxdlock->pxd;
2093                 lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2094
2095                 /* a maplock for txUpdateMap() for free of the page
2096                  * has been formatted at txLock() time;
2097                  */
2098                 tlck->flag |= tlckUPDATEMAP;
2099                 return;
2100         }
2101         /*
2102
2103          * Otherwise it's not a relocate request
2104          *
2105          */
2106         else {
2107                 /* log LOG_UPDATEMAP for logredo() to update bmap for
2108                  * free of truncated/relocated delta extent of the data;
2109                  * e.g.: external EA extent, relocated/truncated extent
2110                  * from xtTailgate();
2111                  */
2112                 lrd->type = cpu_to_le16(LOG_UPDATEMAP);
2113                 pxdlock = (struct pxd_lock *) & tlck->lock;
2114                 nlock = pxdlock->index;
2115                 for (i = 0; i < nlock; i++, pxdlock++) {
2116                         if (pxdlock->flag & mlckALLOCPXD)
2117                                 lrd->log.updatemap.type =
2118                                     cpu_to_le16(LOG_ALLOCPXD);
2119                         else
2120                                 lrd->log.updatemap.type =
2121                                     cpu_to_le16(LOG_FREEPXD);
2122                         lrd->log.updatemap.nxd = cpu_to_le16(1);
2123                         lrd->log.updatemap.pxd = pxdlock->pxd;
2124                         lrd->backchain =
2125                             cpu_to_le32(lmLog(log, tblk, lrd, NULL));
2126                         jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
2127                                  (ulong) addressPXD(&pxdlock->pxd),
2128                                  lengthPXD(&pxdlock->pxd));
2129                 }
2130
2131                 /* update bmap */
2132                 tlck->flag |= tlckUPDATEMAP;
2133         }
2134 }
2135
2136
2137 /*
2138  *      txEA()
2139  *
2140  * function:    acquire maplock for EA/ACL extents or
2141  *              set COMMIT_INLINE flag;
2142  */
2143 void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
2144 {
2145         struct tlock *tlck = NULL;
2146         struct pxd_lock *maplock = NULL, *pxdlock = NULL;
2147
2148         /*
2149          * format maplock for alloc of new EA extent
2150          */
2151         if (newea) {
2152                 /* Since the newea could be a completely zeroed entry we need to
2153                  * check for the two flags which indicate we should actually
2154                  * commit new EA data
2155                  */
2156                 if (newea->flag & DXD_EXTENT) {
2157                         tlck = txMaplock(tid, ip, tlckMAP);
2158                         maplock = (struct pxd_lock *) & tlck->lock;
2159                         pxdlock = (struct pxd_lock *) maplock;
2160                         pxdlock->flag = mlckALLOCPXD;
2161                         PXDaddress(&pxdlock->pxd, addressDXD(newea));
2162                         PXDlength(&pxdlock->pxd, lengthDXD(newea));
2163                         pxdlock++;
2164                         maplock->index = 1;
2165                 } else if (newea->flag & DXD_INLINE) {
2166                         tlck = NULL;
2167
2168                         set_cflag(COMMIT_Inlineea, ip);
2169                 }
2170         }
2171
2172         /*
2173          * format maplock for free of old EA extent
2174          */
2175         if (!test_cflag(COMMIT_Nolink, ip) && oldea->flag & DXD_EXTENT) {
2176                 if (tlck == NULL) {
2177                         tlck = txMaplock(tid, ip, tlckMAP);
2178                         maplock = (struct pxd_lock *) & tlck->lock;
2179                         pxdlock = (struct pxd_lock *) maplock;
2180                         maplock->index = 0;
2181                 }
2182                 pxdlock->flag = mlckFREEPXD;
2183                 PXDaddress(&pxdlock->pxd, addressDXD(oldea));
2184                 PXDlength(&pxdlock->pxd, lengthDXD(oldea));
2185                 maplock->index++;
2186         }
2187 }
2188
2189
2190 /*
2191  *      txForce()
2192  *
2193  * function: synchronously write pages locked by transaction
2194  *              after txLog() but before txUpdateMap();
2195  */
2196 void txForce(struct tblock * tblk)
2197 {
2198         struct tlock *tlck;
2199         lid_t lid, next;
2200         struct metapage *mp;
2201
2202         /*
2203          * reverse the order of transaction tlocks in
2204          * careful update order of address index pages
2205          * (right to left, bottom up)
2206          */
2207         tlck = lid_to_tlock(tblk->next);
2208         lid = tlck->next;
2209         tlck->next = 0;
2210         while (lid) {
2211                 tlck = lid_to_tlock(lid);
2212                 next = tlck->next;
2213                 tlck->next = tblk->next;
2214                 tblk->next = lid;
2215                 lid = next;
2216         }
2217
2218         /*
2219          * synchronously write the page, and
2220          * hold the page for txUpdateMap();
2221          */
2222         for (lid = tblk->next; lid; lid = next) {
2223                 tlck = lid_to_tlock(lid);
2224                 next = tlck->next;
2225
2226                 if ((mp = tlck->mp) != NULL &&
2227                     (tlck->type & tlckBTROOT) == 0) {
2228                         assert(mp->xflag & COMMIT_PAGE);
2229
2230                         if (tlck->flag & tlckWRITEPAGE) {
2231                                 tlck->flag &= ~tlckWRITEPAGE;
2232
2233                                 /* do not release page to freelist */
2234
2235                                 /*
2236                                  * The "right" thing to do here is to
2237                                  * synchronously write the metadata.
2238                                  * With the current implementation this
2239                                  * is hard since write_metapage requires
2240                                  * us to kunmap & remap the page.  If we
2241                                  * have tlocks pointing into the metadata
2242                                  * pages, we don't want to do this.  I think
2243                                  * we can get by with synchronously writing
2244                                  * the pages when they are released.
2245                                  */
2246                                 assert(atomic_read(&mp->nohomeok));
2247                                 set_bit(META_dirty, &mp->flag);
2248                                 set_bit(META_sync, &mp->flag);
2249                         }
2250                 }
2251         }
2252 }
2253
2254
2255 /*
2256  *      txUpdateMap()
2257  *
2258  * function:    update persistent allocation map (and working map
2259  *              if appropriate);
2260  *
2261  * parameter:
2262  */
2263 static void txUpdateMap(struct tblock * tblk)
2264 {
2265         struct inode *ip;
2266         struct inode *ipimap;
2267         lid_t lid;
2268         struct tlock *tlck;
2269         struct maplock *maplock;
2270         struct pxd_lock pxdlock;
2271         int maptype;
2272         int k, nlock;
2273         struct metapage *mp = 0;
2274
2275         ipimap = JFS_SBI(tblk->sb)->ipimap;
2276
2277         maptype = (tblk->xflag & COMMIT_PMAP) ? COMMIT_PMAP : COMMIT_PWMAP;
2278
2279
2280         /*
2281          *      update block allocation map
2282          *
2283          * update allocation state in pmap (and wmap) and
2284          * update lsn of the pmap page;
2285          */
2286         /*
2287          * scan each tlock/page of transaction for block allocation/free:
2288          *
2289          * for each tlock/page of transaction, update map.
2290          *  ? are there tlock for pmap and pwmap at the same time ?
2291          */
2292         for (lid = tblk->next; lid; lid = tlck->next) {
2293                 tlck = lid_to_tlock(lid);
2294
2295                 if ((tlck->flag & tlckUPDATEMAP) == 0)
2296                         continue;
2297
2298                 if (tlck->flag & tlckFREEPAGE) {
2299                         /*
2300                          * Another thread may attempt to reuse freed space
2301                          * immediately, so we want to get rid of the metapage
2302                          * before anyone else has a chance to get it.
2303                          * Lock metapage, update maps, then invalidate
2304                          * the metapage.
2305                          */
2306                         mp = tlck->mp;
2307                         ASSERT(mp->xflag & COMMIT_PAGE);
2308                         hold_metapage(mp, 0);
2309                 }
2310
2311                 /*
2312                  * extent list:
2313                  * . in-line PXD list:
2314                  * . out-of-line XAD list:
2315                  */
2316                 maplock = (struct maplock *) & tlck->lock;
2317                 nlock = maplock->index;
2318
2319                 for (k = 0; k < nlock; k++, maplock++) {
2320                         /*
2321                          * allocate blocks in persistent map:
2322                          *
2323                          * blocks have been allocated from wmap at alloc time;
2324                          */
2325                         if (maplock->flag & mlckALLOC) {
2326                                 txAllocPMap(ipimap, maplock, tblk);
2327                         }
2328                         /*
2329                          * free blocks in persistent and working map:
2330                          * blocks will be freed in pmap and then in wmap;
2331                          *
2332                          * ? tblock specifies the PMAP/PWMAP based upon
2333                          * transaction
2334                          *
2335                          * free blocks in persistent map:
2336                          * blocks will be freed from wmap at last reference
2337                          * release of the object for regular files;
2338                          *
2339                          * Alway free blocks from both persistent & working
2340                          * maps for directories
2341                          */
2342                         else {  /* (maplock->flag & mlckFREE) */
2343
2344                                 if (S_ISDIR(tlck->ip->i_mode))
2345                                         txFreeMap(ipimap, maplock,
2346                                                   tblk, COMMIT_PWMAP);
2347                                 else
2348                                         txFreeMap(ipimap, maplock,
2349                                                   tblk, maptype);
2350                         }
2351                 }
2352                 if (tlck->flag & tlckFREEPAGE) {
2353                         if (!(tblk->flag & tblkGC_LAZY)) {
2354                                 /* This is equivalent to txRelease */
2355                                 ASSERT(mp->lid == lid);
2356                                 tlck->mp->lid = 0;
2357                         }
2358                         assert(atomic_read(&mp->nohomeok) == 1);
2359                         atomic_dec(&mp->nohomeok);
2360                         discard_metapage(mp);
2361                         tlck->mp = 0;
2362                 }
2363         }
2364         /*
2365          *      update inode allocation map
2366          *
2367          * update allocation state in pmap and
2368          * update lsn of the pmap page;
2369          * update in-memory inode flag/state
2370          *
2371          * unlock mapper/write lock
2372          */
2373         if (tblk->xflag & COMMIT_CREATE) {
2374                 diUpdatePMap(ipimap, tblk->ino, FALSE, tblk);
2375                 ipimap->i_state |= I_DIRTY;
2376                 /* update persistent block allocation map
2377                  * for the allocation of inode extent;
2378                  */
2379                 pxdlock.flag = mlckALLOCPXD;
2380                 pxdlock.pxd = tblk->u.ixpxd;
2381                 pxdlock.index = 1;
2382                 txAllocPMap(ipimap, (struct maplock *) & pxdlock, tblk);
2383         } else if (tblk->xflag & COMMIT_DELETE) {
2384                 ip = tblk->u.ip;
2385                 diUpdatePMap(ipimap, ip->i_ino, TRUE, tblk);
2386                 ipimap->i_state |= I_DIRTY;
2387                 iput(ip);
2388         }
2389 }
2390
2391
2392 /*
2393  *      txAllocPMap()
2394  *
2395  * function: allocate from persistent map;
2396  *
2397  * parameter:
2398  *      ipbmap  -
2399  *      malock -
2400  *              xad list:
2401  *              pxd:
2402  *
2403  *      maptype -
2404  *              allocate from persistent map;
2405  *              free from persistent map;
2406  *              (e.g., tmp file - free from working map at releae
2407  *               of last reference);
2408  *              free from persistent and working map;
2409  *
2410  *      lsn     - log sequence number;
2411  */
2412 static void txAllocPMap(struct inode *ip, struct maplock * maplock,
2413                         struct tblock * tblk)
2414 {
2415         struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
2416         struct xdlistlock *xadlistlock;
2417         xad_t *xad;
2418         s64 xaddr;
2419         int xlen;
2420         struct pxd_lock *pxdlock;
2421         struct xdlistlock *pxdlistlock;
2422         pxd_t *pxd;
2423         int n;
2424
2425         /*
2426          * allocate from persistent map;
2427          */
2428         if (maplock->flag & mlckALLOCXADLIST) {
2429                 xadlistlock = (struct xdlistlock *) maplock;
2430                 xad = xadlistlock->xdlist;
2431                 for (n = 0; n < xadlistlock->count; n++, xad++) {
2432                         if (xad->flag & (XAD_NEW | XAD_EXTENDED)) {
2433                                 xaddr = addressXAD(xad);
2434                                 xlen = lengthXAD(xad);
2435                                 dbUpdatePMap(ipbmap, FALSE, xaddr,
2436                                              (s64) xlen, tblk);
2437                                 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
2438                                 jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2439                                          (ulong) xaddr, xlen);
2440                         }
2441                 }
2442         } else if (maplock->flag & mlckALLOCPXD) {
2443                 pxdlock = (struct pxd_lock *) maplock;
2444                 xaddr = addressPXD(&pxdlock->pxd);
2445                 xlen = lengthPXD(&pxdlock->pxd);
2446                 dbUpdatePMap(ipbmap, FALSE, xaddr, (s64) xlen, tblk);
2447                 jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong) xaddr, xlen);
2448         } else {                /* (maplock->flag & mlckALLOCPXDLIST) */
2449
2450                 pxdlistlock = (struct xdlistlock *) maplock;
2451                 pxd = pxdlistlock->xdlist;
2452                 for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2453                         xaddr = addressPXD(pxd);
2454                         xlen = lengthPXD(pxd);
2455                         dbUpdatePMap(ipbmap, FALSE, xaddr, (s64) xlen,
2456                                      tblk);
2457                         jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
2458                                  (ulong) xaddr, xlen);
2459                 }
2460         }
2461 }
2462
2463
2464 /*
2465  *      txFreeMap()
2466  *
2467  * function:    free from persistent and/or working map;
2468  *
2469  * todo: optimization
2470  */
2471 void txFreeMap(struct inode *ip,
2472                struct maplock * maplock, struct tblock * tblk, int maptype)
2473 {
2474         struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
2475         struct xdlistlock *xadlistlock;
2476         xad_t *xad;
2477         s64 xaddr;
2478         int xlen;
2479         struct pxd_lock *pxdlock;
2480         struct xdlistlock *pxdlistlock;
2481         pxd_t *pxd;
2482         int n;
2483
2484         jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
2485                  tblk, maplock, maptype);
2486
2487         /*
2488          * free from persistent map;
2489          */
2490         if (maptype == COMMIT_PMAP || maptype == COMMIT_PWMAP) {
2491                 if (maplock->flag & mlckFREEXADLIST) {
2492                         xadlistlock = (struct xdlistlock *) maplock;
2493                         xad = xadlistlock->xdlist;
2494                         for (n = 0; n < xadlistlock->count; n++, xad++) {
2495                                 if (!(xad->flag & XAD_NEW)) {
2496                                         xaddr = addressXAD(xad);
2497                                         xlen = lengthXAD(xad);
2498                                         dbUpdatePMap(ipbmap, TRUE, xaddr,
2499                                                      (s64) xlen, tblk);
2500                                         jfs_info("freePMap: xaddr:0x%lx "
2501                                                  "xlen:%d",
2502                                                  (ulong) xaddr, xlen);
2503                                 }
2504                         }
2505                 } else if (maplock->flag & mlckFREEPXD) {
2506                         pxdlock = (struct pxd_lock *) maplock;
2507                         xaddr = addressPXD(&pxdlock->pxd);
2508                         xlen = lengthPXD(&pxdlock->pxd);
2509                         dbUpdatePMap(ipbmap, TRUE, xaddr, (s64) xlen,
2510                                      tblk);
2511                         jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2512                                  (ulong) xaddr, xlen);
2513                 } else {        /* (maplock->flag & mlckALLOCPXDLIST) */
2514
2515                         pxdlistlock = (struct xdlistlock *) maplock;
2516                         pxd = pxdlistlock->xdlist;
2517                         for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2518                                 xaddr = addressPXD(pxd);
2519                                 xlen = lengthPXD(pxd);
2520                                 dbUpdatePMap(ipbmap, TRUE, xaddr,
2521                                              (s64) xlen, tblk);
2522                                 jfs_info("freePMap: xaddr:0x%lx xlen:%d",
2523                                          (ulong) xaddr, xlen);
2524                         }
2525                 }
2526         }
2527
2528         /*
2529          * free from working map;
2530          */
2531         if (maptype == COMMIT_PWMAP || maptype == COMMIT_WMAP) {
2532                 if (maplock->flag & mlckFREEXADLIST) {
2533                         xadlistlock = (struct xdlistlock *) maplock;
2534                         xad = xadlistlock->xdlist;
2535                         for (n = 0; n < xadlistlock->count; n++, xad++) {
2536                                 xaddr = addressXAD(xad);
2537                                 xlen = lengthXAD(xad);
2538                                 dbFree(ip, xaddr, (s64) xlen);
2539                                 xad->flag = 0;
2540                                 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2541                                          (ulong) xaddr, xlen);
2542                         }
2543                 } else if (maplock->flag & mlckFREEPXD) {
2544                         pxdlock = (struct pxd_lock *) maplock;
2545                         xaddr = addressPXD(&pxdlock->pxd);
2546                         xlen = lengthPXD(&pxdlock->pxd);
2547                         dbFree(ip, xaddr, (s64) xlen);
2548                         jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2549                                  (ulong) xaddr, xlen);
2550                 } else {        /* (maplock->flag & mlckFREEPXDLIST) */
2551
2552                         pxdlistlock = (struct xdlistlock *) maplock;
2553                         pxd = pxdlistlock->xdlist;
2554                         for (n = 0; n < pxdlistlock->count; n++, pxd++) {
2555                                 xaddr = addressPXD(pxd);
2556                                 xlen = lengthPXD(pxd);
2557                                 dbFree(ip, xaddr, (s64) xlen);
2558                                 jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
2559                                          (ulong) xaddr, xlen);
2560                         }
2561                 }
2562         }
2563 }
2564
2565
2566 /*
2567  *      txFreelock()
2568  *
2569  * function:    remove tlock from inode anonymous locklist
2570  */
2571 void txFreelock(struct inode *ip)
2572 {
2573         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2574         struct tlock *xtlck, *tlck;
2575         lid_t xlid = 0, lid;
2576
2577         if (!jfs_ip->atlhead)
2578                 return;
2579
2580         TXN_LOCK();
2581         xtlck = (struct tlock *) &jfs_ip->atlhead;
2582
2583         while ((lid = xtlck->next)) {
2584                 tlck = lid_to_tlock(lid);
2585                 if (tlck->flag & tlckFREELOCK) {
2586                         xtlck->next = tlck->next;
2587                         txLockFree(lid);
2588                 } else {
2589                         xtlck = tlck;
2590                         xlid = lid;
2591                 }
2592         }
2593
2594         if (jfs_ip->atlhead)
2595                 jfs_ip->atltail = xlid;
2596         else {
2597                 jfs_ip->atltail = 0;
2598                 /*
2599                  * If inode was on anon_list, remove it
2600                  */
2601                 list_del_init(&jfs_ip->anon_inode_list);
2602         }
2603         TXN_UNLOCK();
2604 }
2605
2606
2607 /*
2608  *      txAbort()
2609  *
2610  * function: abort tx before commit;
2611  *
2612  * frees line-locks and segment locks for all
2613  * segments in comdata structure.
2614  * Optionally sets state of file-system to FM_DIRTY in super-block.
2615  * log age of page-frames in memory for which caller has
2616  * are reset to 0 (to avoid logwarap).
2617  */
2618 void txAbort(tid_t tid, int dirty)
2619 {
2620         lid_t lid, next;
2621         struct metapage *mp;
2622         struct tblock *tblk = tid_to_tblock(tid);
2623         struct tlock *tlck;
2624
2625         jfs_warn("txAbort: tid:%d dirty:0x%x", tid, dirty);
2626
2627         /*
2628          * free tlocks of the transaction
2629          */
2630         for (lid = tblk->next; lid; lid = next) {
2631                 tlck = lid_to_tlock(lid);
2632                 next = tlck->next;
2633                 mp = tlck->mp;
2634                 JFS_IP(tlck->ip)->xtlid = 0;
2635
2636                 if (mp) {
2637                         mp->lid = 0;
2638
2639                         /*
2640                          * reset lsn of page to avoid logwarap:
2641                          *
2642                          * (page may have been previously committed by another
2643                          * transaction(s) but has not been paged, i.e.,
2644                          * it may be on logsync list even though it has not
2645                          * been logged for the current tx.)
2646                          */
2647                         if (mp->xflag & COMMIT_PAGE && mp->lsn)
2648                                 LogSyncRelease(mp);
2649                 }
2650                 /* insert tlock at head of freelist */
2651                 TXN_LOCK();
2652                 txLockFree(lid);
2653                 TXN_UNLOCK();
2654         }
2655
2656         /* caller will free the transaction block */
2657
2658         tblk->next = tblk->last = 0;
2659
2660         /*
2661          * mark filesystem dirty
2662          */
2663         if (dirty)
2664                 jfs_error(tblk->sb, "txAbort");
2665
2666         return;
2667 }
2668
2669 /*
2670  *      txLazyCommit(void)
2671  *
2672  *      All transactions except those changing ipimap (COMMIT_FORCE) are
2673  *      processed by this routine.  This insures that the inode and block
2674  *      allocation maps are updated in order.  For synchronous transactions,
2675  *      let the user thread finish processing after txUpdateMap() is called.
2676  */
2677 static void txLazyCommit(struct tblock * tblk)
2678 {
2679         struct jfs_log *log;
2680
2681         while (((tblk->flag & tblkGC_READY) == 0) &&
2682                ((tblk->flag & tblkGC_UNLOCKED) == 0)) {
2683                 /* We must have gotten ahead of the user thread
2684                  */
2685                 jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk);
2686                 yield();
2687         }
2688
2689         jfs_info("txLazyCommit: processing tblk 0x%p", tblk);
2690
2691         txUpdateMap(tblk);
2692
2693         log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
2694
2695         spin_lock_irq(&log->gclock);    // LOGGC_LOCK
2696
2697         tblk->flag |= tblkGC_COMMITTED;
2698
2699         if (tblk->flag & tblkGC_READY)
2700                 log->gcrtc--;
2701
2702         wake_up_all(&tblk->gcwait);     // LOGGC_WAKEUP
2703
2704         /*
2705          * Can't release log->gclock until we've tested tblk->flag
2706          */
2707         if (tblk->flag & tblkGC_LAZY) {
2708                 spin_unlock_irq(&log->gclock);  // LOGGC_UNLOCK
2709                 txUnlock(tblk);
2710                 tblk->flag &= ~tblkGC_LAZY;
2711                 txEnd(tblk - TxBlock);  /* Convert back to tid */
2712         } else
2713                 spin_unlock_irq(&log->gclock);  // LOGGC_UNLOCK
2714
2715         jfs_info("txLazyCommit: done: tblk = 0x%p", tblk);
2716 }
2717
2718 /*
2719  *      jfs_lazycommit(void)
2720  *
2721  *      To be run as a kernel daemon.  If lbmIODone is called in an interrupt
2722  *      context, or where blocking is not wanted, this routine will process
2723  *      committed transactions from the unlock queue.
2724  */
2725 int jfs_lazycommit(void *arg)
2726 {
2727         int WorkDone;
2728         struct tblock *tblk;
2729         unsigned long flags;
2730         struct jfs_sb_info *sbi;
2731
2732         daemonize("jfsCommit");
2733
2734         complete(&jfsIOwait);
2735
2736         do {
2737                 LAZY_LOCK(flags);
2738                 while (!list_empty(&TxAnchor.unlock_queue)) {
2739                         WorkDone = 0;
2740                         list_for_each_entry(tblk, &TxAnchor.unlock_queue,
2741                                             cqueue) {
2742
2743                                 sbi = JFS_SBI(tblk->sb);
2744                                 /*
2745                                  * For each volume, the transactions must be
2746                                  * handled in order.  If another commit thread
2747                                  * is handling a tblk for this superblock,
2748                                  * skip it
2749                                  */
2750                                 if (sbi->commit_state & IN_LAZYCOMMIT)
2751                                         continue;
2752
2753                                 sbi->commit_state |= IN_LAZYCOMMIT;
2754                                 WorkDone = 1;
2755
2756                                 /*
2757                                  * Remove transaction from queue
2758                                  */
2759                                 list_del(&tblk->cqueue);
2760
2761                                 LAZY_UNLOCK(flags);
2762                                 txLazyCommit(tblk);
2763                                 LAZY_LOCK(flags);
2764
2765                                 sbi->commit_state &= ~IN_LAZYCOMMIT;
2766                                 /*
2767                                  * Don't continue in the for loop.  (We can't
2768                                  * anyway, it's unsafe!)  We want to go back to
2769                                  * the beginning of the list.
2770                                  */
2771                                 break;
2772                         }
2773
2774                         /* If there was nothing to do, don't continue */
2775                         if (!WorkDone)
2776                                 break;
2777                 }
2778
2779                 if (current->flags & PF_FREEZE) {
2780                         LAZY_UNLOCK(flags);
2781                         refrigerator(PF_FREEZE);
2782                 } else {
2783                         DECLARE_WAITQUEUE(wq, current);
2784
2785                         add_wait_queue(&jfs_commit_thread_wait, &wq);
2786                         set_current_state(TASK_INTERRUPTIBLE);
2787                         LAZY_UNLOCK(flags);
2788                         schedule();
2789                         current->state = TASK_RUNNING;
2790                         remove_wait_queue(&jfs_commit_thread_wait, &wq);
2791                 }
2792         } while (!jfs_stop_threads);
2793
2794         if (!list_empty(&TxAnchor.unlock_queue))
2795                 jfs_err("jfs_lazycommit being killed w/pending transactions!");
2796         else
2797                 jfs_info("jfs_lazycommit being killed\n");
2798         complete_and_exit(&jfsIOwait, 0);
2799 }
2800
2801 void txLazyUnlock(struct tblock * tblk)
2802 {
2803         unsigned long flags;
2804
2805         LAZY_LOCK(flags);
2806
2807         list_add_tail(&tblk->cqueue, &TxAnchor.unlock_queue);
2808         /*
2809          * Don't wake up a commit thread if there is already one servicing
2810          * this superblock.
2811          */
2812         if (!(JFS_SBI(tblk->sb)->commit_state & IN_LAZYCOMMIT))
2813                 wake_up(&jfs_commit_thread_wait);
2814         LAZY_UNLOCK(flags);
2815 }
2816
2817 static void LogSyncRelease(struct metapage * mp)
2818 {
2819         struct jfs_log *log = mp->log;
2820
2821         assert(atomic_read(&mp->nohomeok));
2822         assert(log);
2823         atomic_dec(&mp->nohomeok);
2824
2825         if (atomic_read(&mp->nohomeok))
2826                 return;
2827
2828         hold_metapage(mp, 0);
2829
2830         LOGSYNC_LOCK(log);
2831         mp->log = NULL;
2832         mp->lsn = 0;
2833         mp->clsn = 0;
2834         log->count--;
2835         list_del_init(&mp->synclist);
2836         LOGSYNC_UNLOCK(log);
2837
2838         release_metapage(mp);
2839 }
2840
2841 /*
2842  *      txQuiesce
2843  *
2844  *      Block all new transactions and push anonymous transactions to
2845  *      completion
2846  *
2847  *      This does almost the same thing as jfs_sync below.  We don't
2848  *      worry about deadlocking when jfs_tlocks_low is set, since we would
2849  *      expect jfs_sync to get us out of that jam.
2850  */
2851 void txQuiesce(struct super_block *sb)
2852 {
2853         struct inode *ip;
2854         struct jfs_inode_info *jfs_ip;
2855         struct jfs_log *log = JFS_SBI(sb)->log;
2856         tid_t tid;
2857
2858         set_bit(log_QUIESCE, &log->flag);
2859
2860         TXN_LOCK();
2861 restart:
2862         while (!list_empty(&TxAnchor.anon_list)) {
2863                 jfs_ip = list_entry(TxAnchor.anon_list.next,
2864                                     struct jfs_inode_info,
2865                                     anon_inode_list);
2866                 ip = &jfs_ip->vfs_inode;
2867
2868                 /*
2869                  * inode will be removed from anonymous list
2870                  * when it is committed
2871                  */
2872                 TXN_UNLOCK();
2873                 tid = txBegin(ip->i_sb, COMMIT_INODE | COMMIT_FORCE);
2874                 down(&jfs_ip->commit_sem);
2875                 txCommit(tid, 1, &ip, 0);
2876                 txEnd(tid);
2877                 up(&jfs_ip->commit_sem);
2878                 /*
2879                  * Just to be safe.  I don't know how
2880                  * long we can run without blocking
2881                  */
2882                 cond_resched();
2883                 TXN_LOCK();
2884         }
2885
2886         /*
2887          * If jfs_sync is running in parallel, there could be some inodes
2888          * on anon_list2.  Let's check.
2889          */
2890         if (!list_empty(&TxAnchor.anon_list2)) {
2891                 list_splice(&TxAnchor.anon_list2, &TxAnchor.anon_list);
2892                 INIT_LIST_HEAD(&TxAnchor.anon_list2);
2893                 goto restart;
2894         }
2895         TXN_UNLOCK();
2896
2897         /*
2898          * We may need to kick off the group commit
2899          */
2900         jfs_flush_journal(log, 0);
2901 }
2902
2903 /*
2904  * txResume()
2905  *
2906  * Allows transactions to start again following txQuiesce
2907  */
2908 void txResume(struct super_block *sb)
2909 {
2910         struct jfs_log *log = JFS_SBI(sb)->log;
2911
2912         clear_bit(log_QUIESCE, &log->flag);
2913         TXN_WAKEUP(&log->syncwait);
2914 }
2915
2916 /*
2917  *      jfs_sync(void)
2918  *
2919  *      To be run as a kernel daemon.  This is awakened when tlocks run low.
2920  *      We write any inodes that have anonymous tlocks so they will become
2921  *      available.
2922  */
2923 int jfs_sync(void *arg)
2924 {
2925         struct inode *ip;
2926         struct jfs_inode_info *jfs_ip;
2927         int rc;
2928         tid_t tid;
2929
2930         daemonize("jfsSync");
2931
2932         complete(&jfsIOwait);
2933
2934         do {
2935                 /*
2936                  * write each inode on the anonymous inode list
2937                  */
2938                 TXN_LOCK();
2939                 while (jfs_tlocks_low && !list_empty(&TxAnchor.anon_list)) {
2940                         jfs_ip = list_entry(TxAnchor.anon_list.next,
2941                                             struct jfs_inode_info,
2942                                             anon_inode_list);
2943                         ip = &jfs_ip->vfs_inode;
2944
2945                         if (! igrab(ip)) {
2946                                 /*
2947                                  * Inode is being freed
2948                                  */
2949                                 list_del_init(&jfs_ip->anon_inode_list);
2950                         } else if (! down_trylock(&jfs_ip->commit_sem)) {
2951                                 /*
2952                                  * inode will be removed from anonymous list
2953                                  * when it is committed
2954                                  */
2955                                 TXN_UNLOCK();
2956                                 tid = txBegin(ip->i_sb, COMMIT_INODE);
2957                                 rc = txCommit(tid, 1, &ip, 0);
2958                                 txEnd(tid);
2959                                 up(&jfs_ip->commit_sem);
2960
2961                                 iput(ip);
2962                                 /*
2963                                  * Just to be safe.  I don't know how
2964                                  * long we can run without blocking
2965                                  */
2966                                 cond_resched();
2967                                 TXN_LOCK();
2968                         } else {
2969                                 /* We can't get the commit semaphore.  It may
2970                                  * be held by a thread waiting for tlock's
2971                                  * so let's not block here.  Save it to
2972                                  * put back on the anon_list.
2973                                  */
2974
2975                                 /* Take off anon_list */
2976                                 list_del(&jfs_ip->anon_inode_list);
2977
2978                                 /* Put on anon_list2 */
2979                                 list_add(&jfs_ip->anon_inode_list,
2980                                          &TxAnchor.anon_list2);
2981
2982                                 TXN_UNLOCK();
2983                                 iput(ip);
2984                                 TXN_LOCK();
2985                         }
2986                 }
2987                 /* Add anon_list2 back to anon_list */
2988                 list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
2989
2990                 if (current->flags & PF_FREEZE) {
2991                         TXN_UNLOCK();
2992                         refrigerator(PF_FREEZE);
2993                 } else {
2994                         DECLARE_WAITQUEUE(wq, current);
2995
2996                         add_wait_queue(&jfs_sync_thread_wait, &wq);
2997                         set_current_state(TASK_INTERRUPTIBLE);
2998                         TXN_UNLOCK();
2999                         schedule();
3000                         current->state = TASK_RUNNING;
3001                         remove_wait_queue(&jfs_sync_thread_wait, &wq);
3002                 }
3003         } while (!jfs_stop_threads);
3004
3005         jfs_info("jfs_sync being killed");
3006         complete_and_exit(&jfsIOwait, 0);
3007 }
3008
3009 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
3010 int jfs_txanchor_read(char *buffer, char **start, off_t offset, int length,
3011                       int *eof, void *data)
3012 {
3013         int len = 0;
3014         off_t begin;
3015         char *freewait;
3016         char *freelockwait;
3017         char *lowlockwait;
3018
3019         freewait =
3020             waitqueue_active(&TxAnchor.freewait) ? "active" : "empty";
3021         freelockwait =
3022             waitqueue_active(&TxAnchor.freelockwait) ? "active" : "empty";
3023         lowlockwait =
3024             waitqueue_active(&TxAnchor.lowlockwait) ? "active" : "empty";
3025
3026         len += sprintf(buffer,
3027                        "JFS TxAnchor\n"
3028                        "============\n"
3029                        "freetid = %d\n"
3030                        "freewait = %s\n"
3031                        "freelock = %d\n"
3032                        "freelockwait = %s\n"
3033                        "lowlockwait = %s\n"
3034                        "tlocksInUse = %d\n"
3035                        "jfs_tlocks_low = %d\n"
3036                        "unlock_queue is %sempty\n",
3037                        TxAnchor.freetid,
3038                        freewait,
3039                        TxAnchor.freelock,
3040                        freelockwait,
3041                        lowlockwait,
3042                        TxAnchor.tlocksInUse,
3043                        jfs_tlocks_low,
3044                        list_empty(&TxAnchor.unlock_queue) ? "" : "not ");
3045
3046         begin = offset;
3047         *start = buffer + begin;
3048         len -= begin;
3049
3050         if (len > length)
3051                 len = length;
3052         else
3053                 *eof = 1;
3054
3055         if (len < 0)
3056                 len = 0;
3057
3058         return len;
3059 }
3060 #endif
3061
3062 #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
3063 int jfs_txstats_read(char *buffer, char **start, off_t offset, int length,
3064                      int *eof, void *data)
3065 {
3066         int len = 0;
3067         off_t begin;
3068
3069         len += sprintf(buffer,
3070                        "JFS TxStats\n"
3071                        "===========\n"
3072                        "calls to txBegin = %d\n"
3073                        "txBegin blocked by sync barrier = %d\n"
3074                        "txBegin blocked by tlocks low = %d\n"
3075                        "txBegin blocked by no free tid = %d\n"
3076                        "calls to txBeginAnon = %d\n"
3077                        "txBeginAnon blocked by sync barrier = %d\n"
3078                        "txBeginAnon blocked by tlocks low = %d\n"
3079                        "calls to txLockAlloc = %d\n"
3080                        "tLockAlloc blocked by no free lock = %d\n",
3081                        TxStat.txBegin,
3082                        TxStat.txBegin_barrier,
3083                        TxStat.txBegin_lockslow,
3084                        TxStat.txBegin_freetid,
3085                        TxStat.txBeginAnon,
3086                        TxStat.txBeginAnon_barrier,
3087                        TxStat.txBeginAnon_lockslow,
3088                        TxStat.txLockAlloc,
3089                        TxStat.txLockAlloc_freelock);
3090
3091         begin = offset;
3092         *start = buffer + begin;
3093         len -= begin;
3094
3095         if (len > length)
3096                 len = length;
3097         else
3098                 *eof = 1;
3099
3100         if (len < 0)
3101                 len = 0;
3102
3103         return len;
3104 }
3105 #endif