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