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