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