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