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