vserver 1.9.5.x5
[linux-2.6.git] / fs / jfs / jfs_logmgr.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or 
8  *   (at your option) any later version.
9  * 
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software 
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 /*
21  *      jfs_logmgr.c: log manager
22  *
23  * for related information, see transaction manager (jfs_txnmgr.c), and
24  * recovery manager (jfs_logredo.c).
25  *
26  * note: for detail, RTFS.
27  *
28  *      log buffer manager:
29  * special purpose buffer manager supporting log i/o requirements.
30  * per log serial pageout of logpage
31  * queuing i/o requests and redrive i/o at iodone
32  * maintain current logpage buffer
33  * no caching since append only
34  * appropriate jfs buffer cache buffers as needed
35  *
36  *      group commit:
37  * transactions which wrote COMMIT records in the same in-memory
38  * log page during the pageout of previous/current log page(s) are
39  * committed together by the pageout of the page.
40  *
41  *      TBD lazy commit:
42  * transactions are committed asynchronously when the log page
43  * containing it COMMIT is paged out when it becomes full;
44  *
45  *      serialization:
46  * . a per log lock serialize log write.
47  * . a per log lock serialize group commit.
48  * . a per log lock serialize log open/close;
49  *
50  *      TBD log integrity:
51  * careful-write (ping-pong) of last logpage to recover from crash
52  * in overwrite.
53  * detection of split (out-of-order) write of physical sectors
54  * of last logpage via timestamp at end of each sector
55  * with its mirror data array at trailer).
56  *
57  *      alternatives:
58  * lsn - 64-bit monotonically increasing integer vs
59  * 32-bit lspn and page eor.
60  */
61
62 #include <linux/fs.h>
63 #include <linux/blkdev.h>
64 #include <linux/interrupt.h>
65 #include <linux/smp_lock.h>
66 #include <linux/completion.h>
67 #include <linux/buffer_head.h>          /* for sync_blockdev() */
68 #include <linux/bio.h>
69 #include <linux/suspend.h>
70 #include "jfs_incore.h"
71 #include "jfs_filsys.h"
72 #include "jfs_metapage.h"
73 #include "jfs_txnmgr.h"
74 #include "jfs_debug.h"
75
76
77 /*
78  * lbuf's ready to be redriven.  Protected by log_redrive_lock (jfsIO thread)
79  */
80 static struct lbuf *log_redrive_list;
81 static DEFINE_SPINLOCK(log_redrive_lock);
82 DECLARE_WAIT_QUEUE_HEAD(jfs_IO_thread_wait);
83
84
85 /*
86  *      log read/write serialization (per log)
87  */
88 #define LOG_LOCK_INIT(log)      init_MUTEX(&(log)->loglock)
89 #define LOG_LOCK(log)           down(&((log)->loglock))
90 #define LOG_UNLOCK(log)         up(&((log)->loglock))
91
92
93 /*
94  *      log group commit serialization (per log)
95  */
96
97 #define LOGGC_LOCK_INIT(log)    spin_lock_init(&(log)->gclock)
98 #define LOGGC_LOCK(log)         spin_lock_irq(&(log)->gclock)
99 #define LOGGC_UNLOCK(log)       spin_unlock_irq(&(log)->gclock)
100 #define LOGGC_WAKEUP(tblk)      wake_up_all(&(tblk)->gcwait)
101
102 /*
103  *      log sync serialization (per log)
104  */
105 #define LOGSYNC_DELTA(logsize)          min((logsize)/8, 128*LOGPSIZE)
106 #define LOGSYNC_BARRIER(logsize)        ((logsize)/4)
107 /*
108 #define LOGSYNC_DELTA(logsize)          min((logsize)/4, 256*LOGPSIZE)
109 #define LOGSYNC_BARRIER(logsize)        ((logsize)/2)
110 */
111
112
113 /*
114  *      log buffer cache synchronization
115  */
116 static DEFINE_SPINLOCK(jfsLCacheLock);
117
118 #define LCACHE_LOCK(flags)      spin_lock_irqsave(&jfsLCacheLock, flags)
119 #define LCACHE_UNLOCK(flags)    spin_unlock_irqrestore(&jfsLCacheLock, flags)
120
121 /*
122  * See __SLEEP_COND in jfs_locks.h
123  */
124 #define LCACHE_SLEEP_COND(wq, cond, flags)      \
125 do {                                            \
126         if (cond)                               \
127                 break;                          \
128         __SLEEP_COND(wq, cond, LCACHE_LOCK(flags), LCACHE_UNLOCK(flags)); \
129 } while (0)
130
131 #define LCACHE_WAKEUP(event)    wake_up(event)
132
133
134 /*
135  *      lbuf buffer cache (lCache) control
136  */
137 /* log buffer manager pageout control (cumulative, inclusive) */
138 #define lbmREAD         0x0001
139 #define lbmWRITE        0x0002  /* enqueue at tail of write queue;
140                                  * init pageout if at head of queue;
141                                  */
142 #define lbmRELEASE      0x0004  /* remove from write queue
143                                  * at completion of pageout;
144                                  * do not free/recycle it yet:
145                                  * caller will free it;
146                                  */
147 #define lbmSYNC         0x0008  /* do not return to freelist
148                                  * when removed from write queue;
149                                  */
150 #define lbmFREE         0x0010  /* return to freelist
151                                  * at completion of pageout;
152                                  * the buffer may be recycled;
153                                  */
154 #define lbmDONE         0x0020
155 #define lbmERROR        0x0040
156 #define lbmGC           0x0080  /* lbmIODone to perform post-GC processing
157                                  * of log page
158                                  */
159 #define lbmDIRECT       0x0100
160
161 /*
162  * Global list of active external journals
163  */
164 static LIST_HEAD(jfs_external_logs);
165 static struct jfs_log *dummy_log = NULL;
166 static DECLARE_MUTEX(jfs_log_sem);
167
168 /*
169  * external references
170  */
171 extern void txLazyUnlock(struct tblock * tblk);
172 extern int jfs_stop_threads;
173 extern struct completion jfsIOwait;
174 extern int jfs_tlocks_low;
175
176 /*
177  * forward references
178  */
179 static int lmWriteRecord(struct jfs_log * log, struct tblock * tblk,
180                          struct lrd * lrd, struct tlock * tlck);
181
182 static int lmNextPage(struct jfs_log * log);
183 static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
184                            int activate);
185
186 static int open_inline_log(struct super_block *sb);
187 static int open_dummy_log(struct super_block *sb);
188 static int lbmLogInit(struct jfs_log * log);
189 static void lbmLogShutdown(struct jfs_log * log);
190 static struct lbuf *lbmAllocate(struct jfs_log * log, int);
191 static void lbmFree(struct lbuf * bp);
192 static void lbmfree(struct lbuf * bp);
193 static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp);
194 static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag, int cant_block);
195 static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag);
196 static int lbmIOWait(struct lbuf * bp, int flag);
197 static bio_end_io_t lbmIODone;
198 static void lbmStartIO(struct lbuf * bp);
199 static void lmGCwrite(struct jfs_log * log, int cant_block);
200 static int lmLogSync(struct jfs_log * log, int nosyncwait);
201
202
203
204 /*
205  *      statistics
206  */
207 #ifdef CONFIG_JFS_STATISTICS
208 static struct lmStat {
209         uint commit;            /* # of commit */
210         uint pagedone;          /* # of page written */
211         uint submitted;         /* # of pages submitted */
212         uint full_page;         /* # of full pages submitted */
213         uint partial_page;      /* # of partial pages submitted */
214 } lmStat;
215 #endif
216
217
218 /*
219  * NAME:        lmLog()
220  *
221  * FUNCTION:    write a log record;
222  *
223  * PARAMETER:
224  *
225  * RETURN:      lsn - offset to the next log record to write (end-of-log);
226  *              -1  - error;
227  *
228  * note: todo: log error handler
229  */
230 int lmLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
231           struct tlock * tlck)
232 {
233         int lsn;
234         int diffp, difft;
235         struct metapage *mp = NULL;
236
237         jfs_info("lmLog: log:0x%p tblk:0x%p, lrd:0x%p tlck:0x%p",
238                  log, tblk, lrd, tlck);
239
240         LOG_LOCK(log);
241
242         /* log by (out-of-transaction) JFS ? */
243         if (tblk == NULL)
244                 goto writeRecord;
245
246         /* log from page ? */
247         if (tlck == NULL ||
248             tlck->type & tlckBTROOT || (mp = tlck->mp) == NULL)
249                 goto writeRecord;
250
251         /*
252          *      initialize/update page/transaction recovery lsn
253          */
254         lsn = log->lsn;
255
256         LOGSYNC_LOCK(log);
257
258         /*
259          * initialize page lsn if first log write of the page
260          */
261         if (mp->lsn == 0) {
262                 mp->log = log;
263                 mp->lsn = lsn;
264                 log->count++;
265
266                 /* insert page at tail of logsynclist */
267                 list_add_tail(&mp->synclist, &log->synclist);
268         }
269
270         /*
271          *      initialize/update lsn of tblock of the page
272          *
273          * transaction inherits oldest lsn of pages associated
274          * with allocation/deallocation of resources (their
275          * log records are used to reconstruct allocation map
276          * at recovery time: inode for inode allocation map,
277          * B+-tree index of extent descriptors for block
278          * allocation map);
279          * allocation map pages inherit transaction lsn at
280          * commit time to allow forwarding log syncpt past log
281          * records associated with allocation/deallocation of
282          * resources only after persistent map of these map pages
283          * have been updated and propagated to home.
284          */
285         /*
286          * initialize transaction lsn:
287          */
288         if (tblk->lsn == 0) {
289                 /* inherit lsn of its first page logged */
290                 tblk->lsn = mp->lsn;
291                 log->count++;
292
293                 /* insert tblock after the page on logsynclist */
294                 list_add(&tblk->synclist, &mp->synclist);
295         }
296         /*
297          * update transaction lsn:
298          */
299         else {
300                 /* inherit oldest/smallest lsn of page */
301                 logdiff(diffp, mp->lsn, log);
302                 logdiff(difft, tblk->lsn, log);
303                 if (diffp < difft) {
304                         /* update tblock lsn with page lsn */
305                         tblk->lsn = mp->lsn;
306
307                         /* move tblock after page on logsynclist */
308                         list_move(&tblk->synclist, &mp->synclist);
309                 }
310         }
311
312         LOGSYNC_UNLOCK(log);
313
314         /*
315          *      write the log record
316          */
317       writeRecord:
318         lsn = lmWriteRecord(log, tblk, lrd, tlck);
319
320         /*
321          * forward log syncpt if log reached next syncpt trigger
322          */
323         logdiff(diffp, lsn, log);
324         if (diffp >= log->nextsync)
325                 lsn = lmLogSync(log, 0);
326
327         /* update end-of-log lsn */
328         log->lsn = lsn;
329
330         LOG_UNLOCK(log);
331
332         /* return end-of-log address */
333         return lsn;
334 }
335
336
337 /*
338  * NAME:        lmWriteRecord()
339  *
340  * FUNCTION:    move the log record to current log page
341  *
342  * PARAMETER:   cd      - commit descriptor
343  *
344  * RETURN:      end-of-log address
345  *                      
346  * serialization: LOG_LOCK() held on entry/exit
347  */
348 static int
349 lmWriteRecord(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
350               struct tlock * tlck)
351 {
352         int lsn = 0;            /* end-of-log address */
353         struct lbuf *bp;        /* dst log page buffer */
354         struct logpage *lp;     /* dst log page */
355         caddr_t dst;            /* destination address in log page */
356         int dstoffset;          /* end-of-log offset in log page */
357         int freespace;          /* free space in log page */
358         caddr_t p;              /* src meta-data page */
359         caddr_t src;
360         int srclen;
361         int nbytes;             /* number of bytes to move */
362         int i;
363         int len;
364         struct linelock *linelock;
365         struct lv *lv;
366         struct lvd *lvd;
367         int l2linesize;
368
369         len = 0;
370
371         /* retrieve destination log page to write */
372         bp = (struct lbuf *) log->bp;
373         lp = (struct logpage *) bp->l_ldata;
374         dstoffset = log->eor;
375
376         /* any log data to write ? */
377         if (tlck == NULL)
378                 goto moveLrd;
379
380         /*
381          *      move log record data
382          */
383         /* retrieve source meta-data page to log */
384         if (tlck->flag & tlckPAGELOCK) {
385                 p = (caddr_t) (tlck->mp->data);
386                 linelock = (struct linelock *) & tlck->lock;
387         }
388         /* retrieve source in-memory inode to log */
389         else if (tlck->flag & tlckINODELOCK) {
390                 if (tlck->type & tlckDTREE)
391                         p = (caddr_t) &JFS_IP(tlck->ip)->i_dtroot;
392                 else
393                         p = (caddr_t) &JFS_IP(tlck->ip)->i_xtroot;
394                 linelock = (struct linelock *) & tlck->lock;
395         }
396 #ifdef  _JFS_WIP
397         else if (tlck->flag & tlckINLINELOCK) {
398
399                 inlinelock = (struct inlinelock *) & tlck;
400                 p = (caddr_t) & inlinelock->pxd;
401                 linelock = (struct linelock *) & tlck;
402         }
403 #endif                          /* _JFS_WIP */
404         else {
405                 jfs_err("lmWriteRecord: UFO tlck:0x%p", tlck);
406                 return 0;       /* Probably should trap */
407         }
408         l2linesize = linelock->l2linesize;
409
410       moveData:
411         ASSERT(linelock->index <= linelock->maxcnt);
412
413         lv = linelock->lv;
414         for (i = 0; i < linelock->index; i++, lv++) {
415                 if (lv->length == 0)
416                         continue;
417
418                 /* is page full ? */
419                 if (dstoffset >= LOGPSIZE - LOGPTLRSIZE) {
420                         /* page become full: move on to next page */
421                         lmNextPage(log);
422
423                         bp = log->bp;
424                         lp = (struct logpage *) bp->l_ldata;
425                         dstoffset = LOGPHDRSIZE;
426                 }
427
428                 /*
429                  * move log vector data
430                  */
431                 src = (u8 *) p + (lv->offset << l2linesize);
432                 srclen = lv->length << l2linesize;
433                 len += srclen;
434                 while (srclen > 0) {
435                         freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
436                         nbytes = min(freespace, srclen);
437                         dst = (caddr_t) lp + dstoffset;
438                         memcpy(dst, src, nbytes);
439                         dstoffset += nbytes;
440
441                         /* is page not full ? */
442                         if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
443                                 break;
444
445                         /* page become full: move on to next page */
446                         lmNextPage(log);
447
448                         bp = (struct lbuf *) log->bp;
449                         lp = (struct logpage *) bp->l_ldata;
450                         dstoffset = LOGPHDRSIZE;
451
452                         srclen -= nbytes;
453                         src += nbytes;
454                 }
455
456                 /*
457                  * move log vector descriptor
458                  */
459                 len += 4;
460                 lvd = (struct lvd *) ((caddr_t) lp + dstoffset);
461                 lvd->offset = cpu_to_le16(lv->offset);
462                 lvd->length = cpu_to_le16(lv->length);
463                 dstoffset += 4;
464                 jfs_info("lmWriteRecord: lv offset:%d length:%d",
465                          lv->offset, lv->length);
466         }
467
468         if ((i = linelock->next)) {
469                 linelock = (struct linelock *) lid_to_tlock(i);
470                 goto moveData;
471         }
472
473         /*
474          *      move log record descriptor
475          */
476       moveLrd:
477         lrd->length = cpu_to_le16(len);
478
479         src = (caddr_t) lrd;
480         srclen = LOGRDSIZE;
481
482         while (srclen > 0) {
483                 freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
484                 nbytes = min(freespace, srclen);
485                 dst = (caddr_t) lp + dstoffset;
486                 memcpy(dst, src, nbytes);
487
488                 dstoffset += nbytes;
489                 srclen -= nbytes;
490
491                 /* are there more to move than freespace of page ? */
492                 if (srclen)
493                         goto pageFull;
494
495                 /*
496                  * end of log record descriptor
497                  */
498
499                 /* update last log record eor */
500                 log->eor = dstoffset;
501                 bp->l_eor = dstoffset;
502                 lsn = (log->page << L2LOGPSIZE) + dstoffset;
503
504                 if (lrd->type & cpu_to_le16(LOG_COMMIT)) {
505                         tblk->clsn = lsn;
506                         jfs_info("wr: tclsn:0x%x, beor:0x%x", tblk->clsn,
507                                  bp->l_eor);
508
509                         INCREMENT(lmStat.commit);       /* # of commit */
510
511                         /*
512                          * enqueue tblock for group commit:
513                          *
514                          * enqueue tblock of non-trivial/synchronous COMMIT
515                          * at tail of group commit queue
516                          * (trivial/asynchronous COMMITs are ignored by
517                          * group commit.)
518                          */
519                         LOGGC_LOCK(log);
520
521                         /* init tblock gc state */
522                         tblk->flag = tblkGC_QUEUE;
523                         tblk->bp = log->bp;
524                         tblk->pn = log->page;
525                         tblk->eor = log->eor;
526
527                         /* enqueue transaction to commit queue */
528                         list_add_tail(&tblk->cqueue, &log->cqueue);
529
530                         LOGGC_UNLOCK(log);
531                 }
532
533                 jfs_info("lmWriteRecord: lrd:0x%04x bp:0x%p pn:%d eor:0x%x",
534                         le16_to_cpu(lrd->type), log->bp, log->page, dstoffset);
535
536                 /* page not full ? */
537                 if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
538                         return lsn;
539
540               pageFull:
541                 /* page become full: move on to next page */
542                 lmNextPage(log);
543
544                 bp = (struct lbuf *) log->bp;
545                 lp = (struct logpage *) bp->l_ldata;
546                 dstoffset = LOGPHDRSIZE;
547                 src += nbytes;
548         }
549
550         return lsn;
551 }
552
553
554 /*
555  * NAME:        lmNextPage()
556  *
557  * FUNCTION:    write current page and allocate next page.
558  *
559  * PARAMETER:   log
560  *
561  * RETURN:      0
562  *                      
563  * serialization: LOG_LOCK() held on entry/exit
564  */
565 static int lmNextPage(struct jfs_log * log)
566 {
567         struct logpage *lp;
568         int lspn;               /* log sequence page number */
569         int pn;                 /* current page number */
570         struct lbuf *bp;
571         struct lbuf *nextbp;
572         struct tblock *tblk;
573
574         /* get current log page number and log sequence page number */
575         pn = log->page;
576         bp = log->bp;
577         lp = (struct logpage *) bp->l_ldata;
578         lspn = le32_to_cpu(lp->h.page);
579
580         LOGGC_LOCK(log);
581
582         /*
583          *      write or queue the full page at the tail of write queue
584          */
585         /* get the tail tblk on commit queue */
586         if (list_empty(&log->cqueue))
587                 tblk = NULL;
588         else
589                 tblk = list_entry(log->cqueue.prev, struct tblock, cqueue);
590
591         /* every tblk who has COMMIT record on the current page,
592          * and has not been committed, must be on commit queue
593          * since tblk is queued at commit queueu at the time
594          * of writing its COMMIT record on the page before
595          * page becomes full (even though the tblk thread
596          * who wrote COMMIT record may have been suspended
597          * currently);
598          */
599
600         /* is page bound with outstanding tail tblk ? */
601         if (tblk && tblk->pn == pn) {
602                 /* mark tblk for end-of-page */
603                 tblk->flag |= tblkGC_EOP;
604
605                 if (log->cflag & logGC_PAGEOUT) {
606                         /* if page is not already on write queue,
607                          * just enqueue (no lbmWRITE to prevent redrive)
608                          * buffer to wqueue to ensure correct serial order
609                          * of the pages since log pages will be added
610                          * continuously
611                          */
612                         if (bp->l_wqnext == NULL)
613                                 lbmWrite(log, bp, 0, 0);
614                 } else {
615                         /*
616                          * No current GC leader, initiate group commit
617                          */
618                         log->cflag |= logGC_PAGEOUT;
619                         lmGCwrite(log, 0);
620                 }
621         }
622         /* page is not bound with outstanding tblk:
623          * init write or mark it to be redriven (lbmWRITE)
624          */
625         else {
626                 /* finalize the page */
627                 bp->l_ceor = bp->l_eor;
628                 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
629                 lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE, 0);
630         }
631         LOGGC_UNLOCK(log);
632
633         /*
634          *      allocate/initialize next page
635          */
636         /* if log wraps, the first data page of log is 2
637          * (0 never used, 1 is superblock).
638          */
639         log->page = (pn == log->size - 1) ? 2 : pn + 1;
640         log->eor = LOGPHDRSIZE; /* ? valid page empty/full at logRedo() */
641
642         /* allocate/initialize next log page buffer */
643         nextbp = lbmAllocate(log, log->page);
644         nextbp->l_eor = log->eor;
645         log->bp = nextbp;
646
647         /* initialize next log page */
648         lp = (struct logpage *) nextbp->l_ldata;
649         lp->h.page = lp->t.page = cpu_to_le32(lspn + 1);
650         lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
651
652         return 0;
653 }
654
655
656 /*
657  * NAME:        lmGroupCommit()
658  *
659  * FUNCTION:    group commit
660  *      initiate pageout of the pages with COMMIT in the order of
661  *      page number - redrive pageout of the page at the head of
662  *      pageout queue until full page has been written.
663  *
664  * RETURN:      
665  *
666  * NOTE:
667  *      LOGGC_LOCK serializes log group commit queue, and
668  *      transaction blocks on the commit queue.
669  *      N.B. LOG_LOCK is NOT held during lmGroupCommit().
670  */
671 int lmGroupCommit(struct jfs_log * log, struct tblock * tblk)
672 {
673         int rc = 0;
674
675         LOGGC_LOCK(log);
676
677         /* group committed already ? */
678         if (tblk->flag & tblkGC_COMMITTED) {
679                 if (tblk->flag & tblkGC_ERROR)
680                         rc = -EIO;
681
682                 LOGGC_UNLOCK(log);
683                 return rc;
684         }
685         jfs_info("lmGroup Commit: tblk = 0x%p, gcrtc = %d", tblk, log->gcrtc);
686
687         if (tblk->xflag & COMMIT_LAZY)
688                 tblk->flag |= tblkGC_LAZY;
689
690         if ((!(log->cflag & logGC_PAGEOUT)) && (!list_empty(&log->cqueue)) &&
691             (!(tblk->xflag & COMMIT_LAZY) || test_bit(log_FLUSH, &log->flag)
692              || jfs_tlocks_low)) {
693                 /*
694                  * No pageout in progress
695                  *
696                  * start group commit as its group leader.
697                  */
698                 log->cflag |= logGC_PAGEOUT;
699
700                 lmGCwrite(log, 0);
701         }
702
703         if (tblk->xflag & COMMIT_LAZY) {
704                 /*
705                  * Lazy transactions can leave now
706                  */
707                 LOGGC_UNLOCK(log);
708                 return 0;
709         }
710
711         /* lmGCwrite gives up LOGGC_LOCK, check again */
712
713         if (tblk->flag & tblkGC_COMMITTED) {
714                 if (tblk->flag & tblkGC_ERROR)
715                         rc = -EIO;
716
717                 LOGGC_UNLOCK(log);
718                 return rc;
719         }
720
721         /* upcount transaction waiting for completion
722          */
723         log->gcrtc++;
724         tblk->flag |= tblkGC_READY;
725
726         __SLEEP_COND(tblk->gcwait, (tblk->flag & tblkGC_COMMITTED),
727                      LOGGC_LOCK(log), LOGGC_UNLOCK(log));
728
729         /* removed from commit queue */
730         if (tblk->flag & tblkGC_ERROR)
731                 rc = -EIO;
732
733         LOGGC_UNLOCK(log);
734         return rc;
735 }
736
737 /*
738  * NAME:        lmGCwrite()
739  *
740  * FUNCTION:    group commit write
741  *      initiate write of log page, building a group of all transactions
742  *      with commit records on that page.
743  *
744  * RETURN:      None
745  *
746  * NOTE:
747  *      LOGGC_LOCK must be held by caller.
748  *      N.B. LOG_LOCK is NOT held during lmGroupCommit().
749  */
750 static void lmGCwrite(struct jfs_log * log, int cant_write)
751 {
752         struct lbuf *bp;
753         struct logpage *lp;
754         int gcpn;               /* group commit page number */
755         struct tblock *tblk;
756         struct tblock *xtblk = NULL;
757
758         /*
759          * build the commit group of a log page
760          *
761          * scan commit queue and make a commit group of all
762          * transactions with COMMIT records on the same log page.
763          */
764         /* get the head tblk on the commit queue */
765         gcpn = list_entry(log->cqueue.next, struct tblock, cqueue)->pn;
766
767         list_for_each_entry(tblk, &log->cqueue, cqueue) {
768                 if (tblk->pn != gcpn)
769                         break;
770
771                 xtblk = tblk;
772
773                 /* state transition: (QUEUE, READY) -> COMMIT */
774                 tblk->flag |= tblkGC_COMMIT;
775         }
776         tblk = xtblk;           /* last tblk of the page */
777
778         /*
779          * pageout to commit transactions on the log page.
780          */
781         bp = (struct lbuf *) tblk->bp;
782         lp = (struct logpage *) bp->l_ldata;
783         /* is page already full ? */
784         if (tblk->flag & tblkGC_EOP) {
785                 /* mark page to free at end of group commit of the page */
786                 tblk->flag &= ~tblkGC_EOP;
787                 tblk->flag |= tblkGC_FREE;
788                 bp->l_ceor = bp->l_eor;
789                 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
790                 lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmGC,
791                          cant_write);
792                 INCREMENT(lmStat.full_page);
793         }
794         /* page is not yet full */
795         else {
796                 bp->l_ceor = tblk->eor; /* ? bp->l_ceor = bp->l_eor; */
797                 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
798                 lbmWrite(log, bp, lbmWRITE | lbmGC, cant_write);
799                 INCREMENT(lmStat.partial_page);
800         }
801 }
802
803 /*
804  * NAME:        lmPostGC()
805  *
806  * FUNCTION:    group commit post-processing
807  *      Processes transactions after their commit records have been written
808  *      to disk, redriving log I/O if necessary.
809  *
810  * RETURN:      None
811  *
812  * NOTE:
813  *      This routine is called a interrupt time by lbmIODone
814  */
815 static void lmPostGC(struct lbuf * bp)
816 {
817         unsigned long flags;
818         struct jfs_log *log = bp->l_log;
819         struct logpage *lp;
820         struct tblock *tblk, *temp;
821
822         //LOGGC_LOCK(log);
823         spin_lock_irqsave(&log->gclock, flags);
824         /*
825          * current pageout of group commit completed.
826          *
827          * remove/wakeup transactions from commit queue who were
828          * group committed with the current log page
829          */
830         list_for_each_entry_safe(tblk, temp, &log->cqueue, cqueue) {
831                 if (!(tblk->flag & tblkGC_COMMIT))
832                         break;
833                 /* if transaction was marked GC_COMMIT then
834                  * it has been shipped in the current pageout
835                  * and made it to disk - it is committed.
836                  */
837
838                 if (bp->l_flag & lbmERROR)
839                         tblk->flag |= tblkGC_ERROR;
840
841                 /* remove it from the commit queue */
842                 list_del(&tblk->cqueue);
843                 tblk->flag &= ~tblkGC_QUEUE;
844
845                 if (tblk == log->flush_tblk) {
846                         /* we can stop flushing the log now */
847                         clear_bit(log_FLUSH, &log->flag);
848                         log->flush_tblk = NULL;
849                 }
850
851                 jfs_info("lmPostGC: tblk = 0x%p, flag = 0x%x", tblk,
852                          tblk->flag);
853
854                 if (!(tblk->xflag & COMMIT_FORCE))
855                         /*
856                          * Hand tblk over to lazy commit thread
857                          */
858                         txLazyUnlock(tblk);
859                 else {
860                         /* state transition: COMMIT -> COMMITTED */
861                         tblk->flag |= tblkGC_COMMITTED;
862
863                         if (tblk->flag & tblkGC_READY)
864                                 log->gcrtc--;
865
866                         LOGGC_WAKEUP(tblk);
867                 }
868
869                 /* was page full before pageout ?
870                  * (and this is the last tblk bound with the page)
871                  */
872                 if (tblk->flag & tblkGC_FREE)
873                         lbmFree(bp);
874                 /* did page become full after pageout ?
875                  * (and this is the last tblk bound with the page)
876                  */
877                 else if (tblk->flag & tblkGC_EOP) {
878                         /* finalize the page */
879                         lp = (struct logpage *) bp->l_ldata;
880                         bp->l_ceor = bp->l_eor;
881                         lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
882                         jfs_info("lmPostGC: calling lbmWrite");
883                         lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE,
884                                  1);
885                 }
886
887         }
888
889         /* are there any transactions who have entered lnGroupCommit()
890          * (whose COMMITs are after that of the last log page written.
891          * They are waiting for new group commit (above at (SLEEP 1))
892          * or lazy transactions are on a full (queued) log page,
893          * select the latest ready transaction as new group leader and
894          * wake her up to lead her group.
895          */
896         if ((!list_empty(&log->cqueue)) &&
897             ((log->gcrtc > 0) || (tblk->bp->l_wqnext != NULL) ||
898              test_bit(log_FLUSH, &log->flag) || jfs_tlocks_low))
899                 /*
900                  * Call lmGCwrite with new group leader
901                  */
902                 lmGCwrite(log, 1);
903
904         /* no transaction are ready yet (transactions are only just
905          * queued (GC_QUEUE) and not entered for group commit yet).
906          * the first transaction entering group commit
907          * will elect herself as new group leader.
908          */
909         else
910                 log->cflag &= ~logGC_PAGEOUT;
911
912         //LOGGC_UNLOCK(log);
913         spin_unlock_irqrestore(&log->gclock, flags);
914         return;
915 }
916
917 /*
918  * NAME:        lmLogSync()
919  *
920  * FUNCTION:    write log SYNCPT record for specified log
921  *      if new sync address is available
922  *      (normally the case if sync() is executed by back-ground
923  *      process).
924  *      if not, explicitly run jfs_blogsync() to initiate
925  *      getting of new sync address.
926  *      calculate new value of i_nextsync which determines when
927  *      this code is called again.
928  *
929  *      this is called only from lmLog().
930  *
931  * PARAMETER:   ip      - pointer to logs inode.
932  *
933  * RETURN:      0
934  *                      
935  * serialization: LOG_LOCK() held on entry/exit
936  */
937 static int lmLogSync(struct jfs_log * log, int nosyncwait)
938 {
939         int logsize;
940         int written;            /* written since last syncpt */
941         int free;               /* free space left available */
942         int delta;              /* additional delta to write normally */
943         int more;               /* additional write granted */
944         struct lrd lrd;
945         int lsn;
946         struct logsyncblk *lp;
947
948         /*
949          *      forward syncpt
950          */
951         /* if last sync is same as last syncpt,
952          * invoke sync point forward processing to update sync.
953          */
954
955         if (log->sync == log->syncpt) {
956                 LOGSYNC_LOCK(log);
957                 /* ToDo: push dirty metapages out to disk */
958 //              bmLogSync(log);
959
960                 if (list_empty(&log->synclist))
961                         log->sync = log->lsn;
962                 else {
963                         lp = list_entry(log->synclist.next,
964                                         struct logsyncblk, synclist);
965                         log->sync = lp->lsn;
966                 }
967                 LOGSYNC_UNLOCK(log);
968
969         }
970
971         /* if sync is different from last syncpt,
972          * write a SYNCPT record with syncpt = sync.
973          * reset syncpt = sync
974          */
975         if (log->sync != log->syncpt) {
976                 struct jfs_sb_info *sbi;
977
978                 /*
979                  * We need to make sure all of the "written" metapages
980                  * actually make it to disk
981                  */
982                 list_for_each_entry(sbi, &log->sb_list, log_list) {
983                         if (sbi->flag & JFS_NOINTEGRITY)
984                                 continue;
985                         filemap_fdatawrite(sbi->ipbmap->i_mapping);
986                         filemap_fdatawrite(sbi->ipimap->i_mapping);
987                         filemap_fdatawrite(sbi->sb->s_bdev->bd_inode->i_mapping);
988                 }
989                 list_for_each_entry(sbi, &log->sb_list, log_list) {
990                         if (sbi->flag & JFS_NOINTEGRITY)
991                                 continue;
992                         filemap_fdatawait(sbi->ipbmap->i_mapping);
993                         filemap_fdatawait(sbi->ipimap->i_mapping);
994                         filemap_fdatawait(sbi->sb->s_bdev->bd_inode->i_mapping);
995                 }
996
997                 lrd.logtid = 0;
998                 lrd.backchain = 0;
999                 lrd.type = cpu_to_le16(LOG_SYNCPT);
1000                 lrd.length = 0;
1001                 lrd.log.syncpt.sync = cpu_to_le32(log->sync);
1002                 lsn = lmWriteRecord(log, NULL, &lrd, NULL);
1003
1004                 log->syncpt = log->sync;
1005         } else
1006                 lsn = log->lsn;
1007
1008         /*
1009          *      setup next syncpt trigger (SWAG)
1010          */
1011         logsize = log->logsize;
1012
1013         logdiff(written, lsn, log);
1014         free = logsize - written;
1015         delta = LOGSYNC_DELTA(logsize);
1016         more = min(free / 2, delta);
1017         if (more < 2 * LOGPSIZE) {
1018                 jfs_warn("\n ... Log Wrap ... Log Wrap ... Log Wrap ...\n");
1019                 /*
1020                  *      log wrapping
1021                  *
1022                  * option 1 - panic ? No.!
1023                  * option 2 - shutdown file systems
1024                  *            associated with log ?
1025                  * option 3 - extend log ?
1026                  */
1027                 /*
1028                  * option 4 - second chance
1029                  *
1030                  * mark log wrapped, and continue.
1031                  * when all active transactions are completed,
1032                  * mark log vaild for recovery.
1033                  * if crashed during invalid state, log state
1034                  * implies invald log, forcing fsck().
1035                  */
1036                 /* mark log state log wrap in log superblock */
1037                 /* log->state = LOGWRAP; */
1038
1039                 /* reset sync point computation */
1040                 log->syncpt = log->sync = lsn;
1041                 log->nextsync = delta;
1042         } else
1043                 /* next syncpt trigger = written + more */
1044                 log->nextsync = written + more;
1045
1046         /* return if lmLogSync() from outside of transaction, e.g., sync() */
1047         if (nosyncwait)
1048                 return lsn;
1049
1050         /* if number of bytes written from last sync point is more
1051          * than 1/4 of the log size, stop new transactions from
1052          * starting until all current transactions are completed
1053          * by setting syncbarrier flag.
1054          */
1055         if (written > LOGSYNC_BARRIER(logsize) && logsize > 32 * LOGPSIZE) {
1056                 set_bit(log_SYNCBARRIER, &log->flag);
1057                 jfs_info("log barrier on: lsn=0x%x syncpt=0x%x", lsn,
1058                          log->syncpt);
1059                 /*
1060                  * We may have to initiate group commit
1061                  */
1062                 jfs_flush_journal(log, 0);
1063         }
1064
1065         return lsn;
1066 }
1067
1068
1069 /*
1070  * NAME:        lmLogOpen()
1071  *
1072  * FUNCTION:    open the log on first open;
1073  *      insert filesystem in the active list of the log.
1074  *
1075  * PARAMETER:   ipmnt   - file system mount inode
1076  *              iplog   - log inode (out)
1077  *
1078  * RETURN:
1079  *
1080  * serialization:
1081  */
1082 int lmLogOpen(struct super_block *sb)
1083 {
1084         int rc;
1085         struct block_device *bdev;
1086         struct jfs_log *log;
1087         struct jfs_sb_info *sbi = JFS_SBI(sb);
1088
1089         if (sbi->flag & JFS_NOINTEGRITY)
1090                 return open_dummy_log(sb);
1091         
1092         if (sbi->mntflag & JFS_INLINELOG)
1093                 return open_inline_log(sb);
1094
1095         down(&jfs_log_sem);
1096         list_for_each_entry(log, &jfs_external_logs, journal_list) {
1097                 if (log->bdev->bd_dev == sbi->logdev) {
1098                         if (memcmp(log->uuid, sbi->loguuid,
1099                                    sizeof(log->uuid))) {
1100                                 jfs_warn("wrong uuid on JFS journal\n");
1101                                 up(&jfs_log_sem);
1102                                 return -EINVAL;
1103                         }
1104                         /*
1105                          * add file system to log active file system list
1106                          */
1107                         if ((rc = lmLogFileSystem(log, sbi, 1))) {
1108                                 up(&jfs_log_sem);
1109                                 return rc;
1110                         }
1111                         goto journal_found;
1112                 }
1113         }
1114
1115         if (!(log = kmalloc(sizeof(struct jfs_log), GFP_KERNEL))) {
1116                 up(&jfs_log_sem);
1117                 return -ENOMEM;
1118         }
1119         memset(log, 0, sizeof(struct jfs_log));
1120         INIT_LIST_HEAD(&log->sb_list);
1121
1122         /*
1123          *      external log as separate logical volume
1124          *
1125          * file systems to log may have n-to-1 relationship;
1126          */
1127
1128         bdev = open_by_devnum(sbi->logdev, FMODE_READ|FMODE_WRITE);
1129         if (IS_ERR(bdev)) {
1130                 rc = -PTR_ERR(bdev);
1131                 goto free;
1132         }
1133
1134         if ((rc = bd_claim(bdev, log))) {
1135                 goto close;
1136         }
1137
1138         log->bdev = bdev;
1139         memcpy(log->uuid, sbi->loguuid, sizeof(log->uuid));
1140         
1141         /*
1142          * initialize log:
1143          */
1144         if ((rc = lmLogInit(log)))
1145                 goto unclaim;
1146
1147         list_add(&log->journal_list, &jfs_external_logs);
1148
1149         /*
1150          * add file system to log active file system list
1151          */
1152         if ((rc = lmLogFileSystem(log, sbi, 1)))
1153                 goto shutdown;
1154
1155 journal_found:
1156         LOG_LOCK(log);
1157         list_add(&sbi->log_list, &log->sb_list);
1158         sbi->log = log;
1159         LOG_UNLOCK(log);
1160
1161         up(&jfs_log_sem);
1162         return 0;
1163
1164         /*
1165          *      unwind on error
1166          */
1167       shutdown:         /* unwind lbmLogInit() */
1168         list_del(&log->journal_list);
1169         lbmLogShutdown(log);
1170
1171       unclaim:
1172         bd_release(bdev);
1173
1174       close:            /* close external log device */
1175         blkdev_put(bdev);
1176
1177       free:             /* free log descriptor */
1178         up(&jfs_log_sem);
1179         kfree(log);
1180
1181         jfs_warn("lmLogOpen: exit(%d)", rc);
1182         return rc;
1183 }
1184
1185 static int open_inline_log(struct super_block *sb)
1186 {
1187         struct jfs_log *log;
1188         int rc;
1189
1190         if (!(log = kmalloc(sizeof(struct jfs_log), GFP_KERNEL)))
1191                 return -ENOMEM;
1192         memset(log, 0, sizeof(struct jfs_log));
1193         INIT_LIST_HEAD(&log->sb_list);
1194
1195         set_bit(log_INLINELOG, &log->flag);
1196         log->bdev = sb->s_bdev;
1197         log->base = addressPXD(&JFS_SBI(sb)->logpxd);
1198         log->size = lengthPXD(&JFS_SBI(sb)->logpxd) >>
1199             (L2LOGPSIZE - sb->s_blocksize_bits);
1200         log->l2bsize = sb->s_blocksize_bits;
1201         ASSERT(L2LOGPSIZE >= sb->s_blocksize_bits);
1202
1203         /*
1204          * initialize log.
1205          */
1206         if ((rc = lmLogInit(log))) {
1207                 kfree(log);
1208                 jfs_warn("lmLogOpen: exit(%d)", rc);
1209                 return rc;
1210         }
1211
1212         list_add(&JFS_SBI(sb)->log_list, &log->sb_list);
1213         JFS_SBI(sb)->log = log;
1214
1215         return rc;
1216 }
1217
1218 static int open_dummy_log(struct super_block *sb)
1219 {
1220         int rc;
1221
1222         down(&jfs_log_sem);
1223         if (!dummy_log) {
1224                 dummy_log = kmalloc(sizeof(struct jfs_log), GFP_KERNEL);
1225                 if (!dummy_log) {
1226                         up(&jfs_log_sem);
1227                         return -ENOMEM;
1228                 }
1229                 memset(dummy_log, 0, sizeof(struct jfs_log));
1230                 INIT_LIST_HEAD(&dummy_log->sb_list);
1231                 dummy_log->no_integrity = 1;
1232                 /* Make up some stuff */
1233                 dummy_log->base = 0;
1234                 dummy_log->size = 1024;
1235                 rc = lmLogInit(dummy_log);
1236                 if (rc) {
1237                         kfree(dummy_log);
1238                         dummy_log = NULL;
1239                         up(&jfs_log_sem);
1240                         return rc;
1241                 }
1242         }
1243
1244         LOG_LOCK(dummy_log);
1245         list_add(&JFS_SBI(sb)->log_list, &dummy_log->sb_list);
1246         JFS_SBI(sb)->log = dummy_log;
1247         LOG_UNLOCK(dummy_log);
1248         up(&jfs_log_sem);
1249
1250         return 0;
1251 }
1252
1253 /*
1254  * NAME:        lmLogInit()
1255  *
1256  * FUNCTION:    log initialization at first log open.
1257  *
1258  *      logredo() (or logformat()) should have been run previously.
1259  *      initialize the log from log superblock.
1260  *      set the log state in the superblock to LOGMOUNT and
1261  *      write SYNCPT log record.
1262  *              
1263  * PARAMETER:   log     - log structure
1264  *
1265  * RETURN:      0       - if ok
1266  *              -EINVAL - bad log magic number or superblock dirty
1267  *              error returned from logwait()
1268  *                      
1269  * serialization: single first open thread
1270  */
1271 int lmLogInit(struct jfs_log * log)
1272 {
1273         int rc = 0;
1274         struct lrd lrd;
1275         struct logsuper *logsuper;
1276         struct lbuf *bpsuper;
1277         struct lbuf *bp;
1278         struct logpage *lp;
1279         int lsn = 0;
1280
1281         jfs_info("lmLogInit: log:0x%p", log);
1282
1283         /* initialize the group commit serialization lock */
1284         LOGGC_LOCK_INIT(log);
1285
1286         /* allocate/initialize the log write serialization lock */
1287         LOG_LOCK_INIT(log);
1288
1289         LOGSYNC_LOCK_INIT(log);
1290
1291         INIT_LIST_HEAD(&log->synclist);
1292
1293         init_waitqueue_head(&log->syncwait);
1294
1295         INIT_LIST_HEAD(&log->cqueue);
1296         log->flush_tblk = NULL;
1297
1298         log->count = 0;
1299
1300         /*
1301          * initialize log i/o
1302          */
1303         if ((rc = lbmLogInit(log)))
1304                 return rc;
1305
1306         if (!test_bit(log_INLINELOG, &log->flag))
1307                 log->l2bsize = L2LOGPSIZE;
1308         
1309         /* check for disabled journaling to disk */
1310         if (log->no_integrity) {
1311                 /*
1312                  * Journal pages will still be filled.  When the time comes
1313                  * to actually do the I/O, the write is not done, and the
1314                  * endio routine is called directly.
1315                  */
1316                 bp = lbmAllocate(log , 0);
1317                 log->bp = bp;
1318                 bp->l_pn = bp->l_eor = 0;
1319         } else {
1320                 /*
1321                  * validate log superblock
1322                  */
1323                 if ((rc = lbmRead(log, 1, &bpsuper)))
1324                         goto errout10;
1325
1326                 logsuper = (struct logsuper *) bpsuper->l_ldata;
1327
1328                 if (logsuper->magic != cpu_to_le32(LOGMAGIC)) {
1329                         jfs_warn("*** Log Format Error ! ***");
1330                         rc = -EINVAL;
1331                         goto errout20;
1332                 }
1333
1334                 /* logredo() should have been run successfully. */
1335                 if (logsuper->state != cpu_to_le32(LOGREDONE)) {
1336                         jfs_warn("*** Log Is Dirty ! ***");
1337                         rc = -EINVAL;
1338                         goto errout20;
1339                 }
1340
1341                 /* initialize log from log superblock */
1342                 if (test_bit(log_INLINELOG,&log->flag)) {
1343                         if (log->size != le32_to_cpu(logsuper->size)) {
1344                                 rc = -EINVAL;
1345                                 goto errout20;
1346                         }
1347                         jfs_info("lmLogInit: inline log:0x%p base:0x%Lx "
1348                                  "size:0x%x", log,
1349                                  (unsigned long long) log->base, log->size);
1350                 } else {
1351                         if (memcmp(logsuper->uuid, log->uuid, 16)) {
1352                                 jfs_warn("wrong uuid on JFS log device");
1353                                 goto errout20;
1354                         }
1355                         log->size = le32_to_cpu(logsuper->size);
1356                         log->l2bsize = le32_to_cpu(logsuper->l2bsize);
1357                         jfs_info("lmLogInit: external log:0x%p base:0x%Lx "
1358                                  "size:0x%x", log,
1359                                  (unsigned long long) log->base, log->size);
1360                 }
1361
1362                 log->page = le32_to_cpu(logsuper->end) / LOGPSIZE;
1363                 log->eor = le32_to_cpu(logsuper->end) - (LOGPSIZE * log->page);
1364
1365                 /*
1366                  * initialize for log append write mode
1367                  */
1368                 /* establish current/end-of-log page/buffer */
1369                 if ((rc = lbmRead(log, log->page, &bp)))
1370                         goto errout20;
1371
1372                 lp = (struct logpage *) bp->l_ldata;
1373
1374                 jfs_info("lmLogInit: lsn:0x%x page:%d eor:%d:%d",
1375                          le32_to_cpu(logsuper->end), log->page, log->eor,
1376                          le16_to_cpu(lp->h.eor));
1377
1378                 log->bp = bp;
1379                 bp->l_pn = log->page;
1380                 bp->l_eor = log->eor;
1381
1382                 /* if current page is full, move on to next page */
1383                 if (log->eor >= LOGPSIZE - LOGPTLRSIZE)
1384                         lmNextPage(log);
1385
1386                 /*
1387                  * initialize log syncpoint
1388                  */
1389                 /*
1390                  * write the first SYNCPT record with syncpoint = 0
1391                  * (i.e., log redo up to HERE !);
1392                  * remove current page from lbm write queue at end of pageout
1393                  * (to write log superblock update), but do not release to
1394                  * freelist;
1395                  */
1396                 lrd.logtid = 0;
1397                 lrd.backchain = 0;
1398                 lrd.type = cpu_to_le16(LOG_SYNCPT);
1399                 lrd.length = 0;
1400                 lrd.log.syncpt.sync = 0;
1401                 lsn = lmWriteRecord(log, NULL, &lrd, NULL);
1402                 bp = log->bp;
1403                 bp->l_ceor = bp->l_eor;
1404                 lp = (struct logpage *) bp->l_ldata;
1405                 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
1406                 lbmWrite(log, bp, lbmWRITE | lbmSYNC, 0);
1407                 if ((rc = lbmIOWait(bp, 0)))
1408                         goto errout30;
1409
1410                 /*
1411                  * update/write superblock
1412                  */
1413                 logsuper->state = cpu_to_le32(LOGMOUNT);
1414                 log->serial = le32_to_cpu(logsuper->serial) + 1;
1415                 logsuper->serial = cpu_to_le32(log->serial);
1416                 lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
1417                 if ((rc = lbmIOWait(bpsuper, lbmFREE)))
1418                         goto errout30;
1419         }
1420
1421         /* initialize logsync parameters */
1422         log->logsize = (log->size - 2) << L2LOGPSIZE;
1423         log->lsn = lsn;
1424         log->syncpt = lsn;
1425         log->sync = log->syncpt;
1426         log->nextsync = LOGSYNC_DELTA(log->logsize);
1427
1428         jfs_info("lmLogInit: lsn:0x%x syncpt:0x%x sync:0x%x",
1429                  log->lsn, log->syncpt, log->sync);
1430
1431         /*
1432          * initialize for lazy/group commit
1433          */
1434         log->clsn = lsn;
1435
1436         return 0;
1437
1438         /*
1439          *      unwind on error
1440          */
1441       errout30:         /* release log page */
1442         log->wqueue = NULL;
1443         bp->l_wqnext = NULL;
1444         lbmFree(bp);
1445
1446       errout20:         /* release log superblock */
1447         lbmFree(bpsuper);
1448
1449       errout10:         /* unwind lbmLogInit() */
1450         lbmLogShutdown(log);
1451
1452         jfs_warn("lmLogInit: exit(%d)", rc);
1453         return rc;
1454 }
1455
1456
1457 /*
1458  * NAME:        lmLogClose()
1459  *
1460  * FUNCTION:    remove file system <ipmnt> from active list of log <iplog>
1461  *              and close it on last close.
1462  *
1463  * PARAMETER:   sb      - superblock
1464  *
1465  * RETURN:      errors from subroutines
1466  *
1467  * serialization:
1468  */
1469 int lmLogClose(struct super_block *sb)
1470 {
1471         struct jfs_sb_info *sbi = JFS_SBI(sb);
1472         struct jfs_log *log = sbi->log;
1473         struct block_device *bdev;
1474         int rc = 0;
1475
1476         jfs_info("lmLogClose: log:0x%p", log);
1477
1478         down(&jfs_log_sem);
1479         LOG_LOCK(log);
1480         list_del(&sbi->log_list);
1481         LOG_UNLOCK(log);
1482         sbi->log = NULL;
1483
1484         /*
1485          * We need to make sure all of the "written" metapages
1486          * actually make it to disk
1487          */
1488         sync_blockdev(sb->s_bdev);
1489
1490         if (test_bit(log_INLINELOG, &log->flag)) {
1491                 /*
1492                  *      in-line log in host file system
1493                  */
1494                 rc = lmLogShutdown(log);
1495                 kfree(log);
1496                 goto out;
1497         }
1498
1499         if (!log->no_integrity)
1500                 lmLogFileSystem(log, sbi, 0);
1501
1502         if (!list_empty(&log->sb_list))
1503                 goto out;
1504
1505         /*
1506          * TODO: ensure that the dummy_log is in a state to allow
1507          * lbmLogShutdown to deallocate all the buffers and call
1508          * kfree against dummy_log.  For now, leave dummy_log & its
1509          * buffers in memory, and resuse if another no-integrity mount
1510          * is requested.
1511          */
1512         if (log->no_integrity)
1513                 goto out;
1514
1515         /*
1516          *      external log as separate logical volume
1517          */
1518         list_del(&log->journal_list);
1519         bdev = log->bdev;
1520         rc = lmLogShutdown(log);
1521
1522         bd_release(bdev);
1523         blkdev_put(bdev);
1524
1525         kfree(log);
1526
1527       out:
1528         up(&jfs_log_sem);
1529         jfs_info("lmLogClose: exit(%d)", rc);
1530         return rc;
1531 }
1532
1533
1534 /*
1535  * NAME:        jfs_flush_journal()
1536  *
1537  * FUNCTION:    initiate write of any outstanding transactions to the journal
1538  *              and optionally wait until they are all written to disk
1539  *
1540  *              wait == 0  flush until latest txn is committed, don't wait
1541  *              wait == 1  flush until latest txn is committed, wait
1542  *              wait > 1   flush until all txn's are complete, wait
1543  */
1544 void jfs_flush_journal(struct jfs_log *log, int wait)
1545 {
1546         int i;
1547         struct tblock *target = NULL;
1548
1549         /* jfs_write_inode may call us during read-only mount */
1550         if (!log)
1551                 return;
1552
1553         jfs_info("jfs_flush_journal: log:0x%p wait=%d", log, wait);
1554
1555         LOGGC_LOCK(log);
1556
1557         if (!list_empty(&log->cqueue)) {
1558                 /*
1559                  * This ensures that we will keep writing to the journal as long
1560                  * as there are unwritten commit records
1561                  */
1562                 target = list_entry(log->cqueue.prev, struct tblock, cqueue);
1563
1564                 if (test_bit(log_FLUSH, &log->flag)) {
1565                         /*
1566                          * We're already flushing.
1567                          * if flush_tblk is NULL, we are flushing everything,
1568                          * so leave it that way.  Otherwise, update it to the
1569                          * latest transaction
1570                          */
1571                         if (log->flush_tblk)
1572                                 log->flush_tblk = target;
1573                 } else {
1574                         /* Only flush until latest transaction is committed */
1575                         log->flush_tblk = target;
1576                         set_bit(log_FLUSH, &log->flag);
1577
1578                         /*
1579                          * Initiate I/O on outstanding transactions
1580                          */
1581                         if (!(log->cflag & logGC_PAGEOUT)) {
1582                                 log->cflag |= logGC_PAGEOUT;
1583                                 lmGCwrite(log, 0);
1584                         }
1585                 }
1586         }
1587         if ((wait > 1) || test_bit(log_SYNCBARRIER, &log->flag)) {
1588                 /* Flush until all activity complete */
1589                 set_bit(log_FLUSH, &log->flag);
1590                 log->flush_tblk = NULL;
1591         }
1592
1593         if (wait && target && !(target->flag & tblkGC_COMMITTED)) {
1594                 DECLARE_WAITQUEUE(__wait, current);
1595
1596                 add_wait_queue(&target->gcwait, &__wait);
1597                 set_current_state(TASK_UNINTERRUPTIBLE);
1598                 LOGGC_UNLOCK(log);
1599                 schedule();
1600                 current->state = TASK_RUNNING;
1601                 LOGGC_LOCK(log);
1602                 remove_wait_queue(&target->gcwait, &__wait);
1603         }
1604         LOGGC_UNLOCK(log);
1605
1606         if (wait < 2)
1607                 return;
1608
1609         /*
1610          * If there was recent activity, we may need to wait
1611          * for the lazycommit thread to catch up
1612          */
1613         if ((!list_empty(&log->cqueue)) || !list_empty(&log->synclist)) {
1614                 for (i = 0; i < 800; i++) {     /* Too much? */
1615                         current->state = TASK_INTERRUPTIBLE;
1616                         schedule_timeout(HZ / 4);
1617                         if (list_empty(&log->cqueue) &&
1618                             list_empty(&log->synclist))
1619                                 break;
1620                 }
1621         }
1622         assert(list_empty(&log->cqueue));
1623         assert(list_empty(&log->synclist));
1624         clear_bit(log_FLUSH, &log->flag);
1625 }
1626
1627 /*
1628  * NAME:        lmLogShutdown()
1629  *
1630  * FUNCTION:    log shutdown at last LogClose().
1631  *
1632  *              write log syncpt record.
1633  *              update super block to set redone flag to 0.
1634  *
1635  * PARAMETER:   log     - log inode
1636  *
1637  * RETURN:      0       - success
1638  *                      
1639  * serialization: single last close thread
1640  */
1641 int lmLogShutdown(struct jfs_log * log)
1642 {
1643         int rc;
1644         struct lrd lrd;
1645         int lsn;
1646         struct logsuper *logsuper;
1647         struct lbuf *bpsuper;
1648         struct lbuf *bp;
1649         struct logpage *lp;
1650
1651         jfs_info("lmLogShutdown: log:0x%p", log);
1652
1653         jfs_flush_journal(log, 2);
1654
1655         /*
1656          * write the last SYNCPT record with syncpoint = 0
1657          * (i.e., log redo up to HERE !)
1658          */
1659         lrd.logtid = 0;
1660         lrd.backchain = 0;
1661         lrd.type = cpu_to_le16(LOG_SYNCPT);
1662         lrd.length = 0;
1663         lrd.log.syncpt.sync = 0;
1664         
1665         lsn = lmWriteRecord(log, NULL, &lrd, NULL);
1666         bp = log->bp;
1667         lp = (struct logpage *) bp->l_ldata;
1668         lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
1669         lbmWrite(log, log->bp, lbmWRITE | lbmRELEASE | lbmSYNC, 0);
1670         lbmIOWait(log->bp, lbmFREE);
1671
1672         /*
1673          * synchronous update log superblock
1674          * mark log state as shutdown cleanly
1675          * (i.e., Log does not need to be replayed).
1676          */
1677         if ((rc = lbmRead(log, 1, &bpsuper)))
1678                 goto out;
1679
1680         logsuper = (struct logsuper *) bpsuper->l_ldata;
1681         logsuper->state = cpu_to_le32(LOGREDONE);
1682         logsuper->end = cpu_to_le32(lsn);
1683         lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
1684         rc = lbmIOWait(bpsuper, lbmFREE);
1685
1686         jfs_info("lmLogShutdown: lsn:0x%x page:%d eor:%d",
1687                  lsn, log->page, log->eor);
1688
1689       out:    
1690         /*
1691          * shutdown per log i/o
1692          */
1693         lbmLogShutdown(log);
1694
1695         if (rc) {
1696                 jfs_warn("lmLogShutdown: exit(%d)", rc);
1697         }
1698         return rc;
1699 }
1700
1701
1702 /*
1703  * NAME:        lmLogFileSystem()
1704  *
1705  * FUNCTION:    insert (<activate> = true)/remove (<activate> = false)
1706  *      file system into/from log active file system list.
1707  *
1708  * PARAMETE:    log     - pointer to logs inode.
1709  *              fsdev   - kdev_t of filesystem.
1710  *              serial  - pointer to returned log serial number
1711  *              activate - insert/remove device from active list.
1712  *
1713  * RETURN:      0       - success
1714  *              errors returned by vms_iowait().
1715  */
1716 static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
1717                            int activate)
1718 {
1719         int rc = 0;
1720         int i;
1721         struct logsuper *logsuper;
1722         struct lbuf *bpsuper;
1723         char *uuid = sbi->uuid;
1724
1725         /*
1726          * insert/remove file system device to log active file system list.
1727          */
1728         if ((rc = lbmRead(log, 1, &bpsuper)))
1729                 return rc;
1730
1731         logsuper = (struct logsuper *) bpsuper->l_ldata;
1732         if (activate) {
1733                 for (i = 0; i < MAX_ACTIVE; i++)
1734                         if (!memcmp(logsuper->active[i].uuid, NULL_UUID, 16)) {
1735                                 memcpy(logsuper->active[i].uuid, uuid, 16);
1736                                 sbi->aggregate = i;
1737                                 break;
1738                         }
1739                 if (i == MAX_ACTIVE) {
1740                         jfs_warn("Too many file systems sharing journal!");
1741                         lbmFree(bpsuper);
1742                         return -EMFILE; /* Is there a better rc? */
1743                 }
1744         } else {
1745                 for (i = 0; i < MAX_ACTIVE; i++)
1746                         if (!memcmp(logsuper->active[i].uuid, uuid, 16)) {
1747                                 memcpy(logsuper->active[i].uuid, NULL_UUID, 16);
1748                                 break;
1749                         }
1750                 if (i == MAX_ACTIVE) {
1751                         jfs_warn("Somebody stomped on the journal!");
1752                         lbmFree(bpsuper);
1753                         return -EIO;
1754                 }
1755                 
1756         }
1757
1758         /*
1759          * synchronous write log superblock:
1760          *
1761          * write sidestream bypassing write queue:
1762          * at file system mount, log super block is updated for
1763          * activation of the file system before any log record
1764          * (MOUNT record) of the file system, and at file system
1765          * unmount, all meta data for the file system has been
1766          * flushed before log super block is updated for deactivation
1767          * of the file system.
1768          */
1769         lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
1770         rc = lbmIOWait(bpsuper, lbmFREE);
1771
1772         return rc;
1773 }
1774
1775 /*
1776  *              log buffer manager (lbm)
1777  *              ------------------------
1778  *
1779  * special purpose buffer manager supporting log i/o requirements.
1780  *
1781  * per log write queue:
1782  * log pageout occurs in serial order by fifo write queue and
1783  * restricting to a single i/o in pregress at any one time.
1784  * a circular singly-linked list
1785  * (log->wrqueue points to the tail, and buffers are linked via
1786  * bp->wrqueue field), and
1787  * maintains log page in pageout ot waiting for pageout in serial pageout.
1788  */
1789
1790 /*
1791  *      lbmLogInit()
1792  *
1793  * initialize per log I/O setup at lmLogInit()
1794  */
1795 static int lbmLogInit(struct jfs_log * log)
1796 {                               /* log inode */
1797         int i;
1798         struct lbuf *lbuf;
1799
1800         jfs_info("lbmLogInit: log:0x%p", log);
1801
1802         /* initialize current buffer cursor */
1803         log->bp = NULL;
1804
1805         /* initialize log device write queue */
1806         log->wqueue = NULL;
1807
1808         /*
1809          * Each log has its own buffer pages allocated to it.  These are
1810          * not managed by the page cache.  This ensures that a transaction
1811          * writing to the log does not block trying to allocate a page from
1812          * the page cache (for the log).  This would be bad, since page
1813          * allocation waits on the kswapd thread that may be committing inodes
1814          * which would cause log activity.  Was that clear?  I'm trying to
1815          * avoid deadlock here.
1816          */
1817         init_waitqueue_head(&log->free_wait);
1818
1819         log->lbuf_free = NULL;
1820
1821         for (i = 0; i < LOGPAGES; i++) {
1822                 lbuf = kmalloc(sizeof(struct lbuf), GFP_KERNEL);
1823                 if (lbuf == 0)
1824                         goto error;
1825                 lbuf->l_ldata = (char *) get_zeroed_page(GFP_KERNEL);
1826                 if (lbuf->l_ldata == 0) {
1827                         kfree(lbuf);
1828                         goto error;
1829                 }
1830                 lbuf->l_log = log;
1831                 init_waitqueue_head(&lbuf->l_ioevent);
1832
1833                 lbuf->l_freelist = log->lbuf_free;
1834                 log->lbuf_free = lbuf;
1835         }
1836
1837         return (0);
1838
1839       error:
1840         lbmLogShutdown(log);
1841         return -ENOMEM;
1842 }
1843
1844
1845 /*
1846  *      lbmLogShutdown()
1847  *
1848  * finalize per log I/O setup at lmLogShutdown()
1849  */
1850 static void lbmLogShutdown(struct jfs_log * log)
1851 {
1852         struct lbuf *lbuf;
1853
1854         jfs_info("lbmLogShutdown: log:0x%p", log);
1855
1856         lbuf = log->lbuf_free;
1857         while (lbuf) {
1858                 struct lbuf *next = lbuf->l_freelist;
1859                 free_page((unsigned long) lbuf->l_ldata);
1860                 kfree(lbuf);
1861                 lbuf = next;
1862         }
1863
1864         log->bp = NULL;
1865 }
1866
1867
1868 /*
1869  *      lbmAllocate()
1870  *
1871  * allocate an empty log buffer
1872  */
1873 static struct lbuf *lbmAllocate(struct jfs_log * log, int pn)
1874 {
1875         struct lbuf *bp;
1876         unsigned long flags;
1877
1878         /*
1879          * recycle from log buffer freelist if any
1880          */
1881         LCACHE_LOCK(flags);
1882         LCACHE_SLEEP_COND(log->free_wait, (bp = log->lbuf_free), flags);
1883         log->lbuf_free = bp->l_freelist;
1884         LCACHE_UNLOCK(flags);
1885
1886         bp->l_flag = 0;
1887
1888         bp->l_wqnext = NULL;
1889         bp->l_freelist = NULL;
1890
1891         bp->l_pn = pn;
1892         bp->l_blkno = log->base + (pn << (L2LOGPSIZE - log->l2bsize));
1893         bp->l_ceor = 0;
1894
1895         return bp;
1896 }
1897
1898
1899 /*
1900  *      lbmFree()
1901  *
1902  * release a log buffer to freelist
1903  */
1904 static void lbmFree(struct lbuf * bp)
1905 {
1906         unsigned long flags;
1907
1908         LCACHE_LOCK(flags);
1909
1910         lbmfree(bp);
1911
1912         LCACHE_UNLOCK(flags);
1913 }
1914
1915 static void lbmfree(struct lbuf * bp)
1916 {
1917         struct jfs_log *log = bp->l_log;
1918
1919         assert(bp->l_wqnext == NULL);
1920
1921         /*
1922          * return the buffer to head of freelist
1923          */
1924         bp->l_freelist = log->lbuf_free;
1925         log->lbuf_free = bp;
1926
1927         wake_up(&log->free_wait);
1928         return;
1929 }
1930
1931
1932 /*
1933  * NAME:        lbmRedrive
1934  *
1935  * FUNCTION:    add a log buffer to the the log redrive list
1936  *
1937  * PARAMETER:
1938  *     bp       - log buffer
1939  *
1940  * NOTES:
1941  *      Takes log_redrive_lock.
1942  */
1943 static inline void lbmRedrive(struct lbuf *bp)
1944 {
1945         unsigned long flags;
1946
1947         spin_lock_irqsave(&log_redrive_lock, flags);
1948         bp->l_redrive_next = log_redrive_list;
1949         log_redrive_list = bp;
1950         spin_unlock_irqrestore(&log_redrive_lock, flags);
1951
1952         wake_up(&jfs_IO_thread_wait);
1953 }
1954
1955
1956 /*
1957  *      lbmRead()
1958  */
1959 static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)
1960 {
1961         struct bio *bio;
1962         struct lbuf *bp;
1963
1964         /*
1965          * allocate a log buffer
1966          */
1967         *bpp = bp = lbmAllocate(log, pn);
1968         jfs_info("lbmRead: bp:0x%p pn:0x%x", bp, pn);
1969
1970         bp->l_flag |= lbmREAD;
1971
1972         bio = bio_alloc(GFP_NOFS, 1);
1973
1974         bio->bi_sector = bp->l_blkno << (log->l2bsize - 9);
1975         bio->bi_bdev = log->bdev;
1976         bio->bi_io_vec[0].bv_page = virt_to_page(bp->l_ldata);
1977         bio->bi_io_vec[0].bv_len = LOGPSIZE;
1978         bio->bi_io_vec[0].bv_offset = 0;
1979
1980         bio->bi_vcnt = 1;
1981         bio->bi_idx = 0;
1982         bio->bi_size = LOGPSIZE;
1983
1984         bio->bi_end_io = lbmIODone;
1985         bio->bi_private = bp;
1986         submit_bio(READ_SYNC, bio);
1987
1988         wait_event(bp->l_ioevent, (bp->l_flag != lbmREAD));
1989
1990         return 0;
1991 }
1992
1993
1994 /*
1995  *      lbmWrite()
1996  *
1997  * buffer at head of pageout queue stays after completion of
1998  * partial-page pageout and redriven by explicit initiation of
1999  * pageout by caller until full-page pageout is completed and
2000  * released.
2001  *
2002  * device driver i/o done redrives pageout of new buffer at
2003  * head of pageout queue when current buffer at head of pageout
2004  * queue is released at the completion of its full-page pageout.
2005  *
2006  * LOGGC_LOCK() serializes lbmWrite() by lmNextPage() and lmGroupCommit().
2007  * LCACHE_LOCK() serializes xflag between lbmWrite() and lbmIODone()
2008  */
2009 static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
2010                      int cant_block)
2011 {
2012         struct lbuf *tail;
2013         unsigned long flags;
2014
2015         jfs_info("lbmWrite: bp:0x%p flag:0x%x pn:0x%x", bp, flag, bp->l_pn);
2016
2017         /* map the logical block address to physical block address */
2018         bp->l_blkno =
2019             log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
2020
2021         LCACHE_LOCK(flags);             /* disable+lock */
2022
2023         /*
2024          * initialize buffer for device driver
2025          */
2026         bp->l_flag = flag;
2027
2028         /*
2029          *      insert bp at tail of write queue associated with log
2030          *
2031          * (request is either for bp already/currently at head of queue
2032          * or new bp to be inserted at tail)
2033          */
2034         tail = log->wqueue;
2035
2036         /* is buffer not already on write queue ? */
2037         if (bp->l_wqnext == NULL) {
2038                 /* insert at tail of wqueue */
2039                 if (tail == NULL) {
2040                         log->wqueue = bp;
2041                         bp->l_wqnext = bp;
2042                 } else {
2043                         log->wqueue = bp;
2044                         bp->l_wqnext = tail->l_wqnext;
2045                         tail->l_wqnext = bp;
2046                 }
2047
2048                 tail = bp;
2049         }
2050
2051         /* is buffer at head of wqueue and for write ? */
2052         if ((bp != tail->l_wqnext) || !(flag & lbmWRITE)) {
2053                 LCACHE_UNLOCK(flags);   /* unlock+enable */
2054                 return;
2055         }
2056
2057         LCACHE_UNLOCK(flags);   /* unlock+enable */
2058
2059         if (cant_block)
2060                 lbmRedrive(bp);
2061         else if (flag & lbmSYNC)
2062                 lbmStartIO(bp);
2063         else {
2064                 LOGGC_UNLOCK(log);
2065                 lbmStartIO(bp);
2066                 LOGGC_LOCK(log);
2067         }
2068 }
2069
2070
2071 /*
2072  *      lbmDirectWrite()
2073  *
2074  * initiate pageout bypassing write queue for sidestream
2075  * (e.g., log superblock) write;
2076  */
2077 static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag)
2078 {
2079         jfs_info("lbmDirectWrite: bp:0x%p flag:0x%x pn:0x%x",
2080                  bp, flag, bp->l_pn);
2081
2082         /*
2083          * initialize buffer for device driver
2084          */
2085         bp->l_flag = flag | lbmDIRECT;
2086
2087         /* map the logical block address to physical block address */
2088         bp->l_blkno =
2089             log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
2090
2091         /*
2092          *      initiate pageout of the page
2093          */
2094         lbmStartIO(bp);
2095 }
2096
2097
2098 /*
2099  * NAME:        lbmStartIO()
2100  *
2101  * FUNCTION:    Interface to DD strategy routine
2102  *
2103  * RETURN:      none
2104  *
2105  * serialization: LCACHE_LOCK() is NOT held during log i/o;
2106  */
2107 static void lbmStartIO(struct lbuf * bp)
2108 {
2109         struct bio *bio;
2110         struct jfs_log *log = bp->l_log;
2111
2112         jfs_info("lbmStartIO\n");
2113
2114         bio = bio_alloc(GFP_NOFS, 1);
2115         bio->bi_sector = bp->l_blkno << (log->l2bsize - 9);
2116         bio->bi_bdev = log->bdev;
2117         bio->bi_io_vec[0].bv_page = virt_to_page(bp->l_ldata);
2118         bio->bi_io_vec[0].bv_len = LOGPSIZE;
2119         bio->bi_io_vec[0].bv_offset = 0;
2120
2121         bio->bi_vcnt = 1;
2122         bio->bi_idx = 0;
2123         bio->bi_size = LOGPSIZE;
2124
2125         bio->bi_end_io = lbmIODone;
2126         bio->bi_private = bp;
2127
2128         /* check if journaling to disk has been disabled */
2129         if (!log->no_integrity) {
2130                 submit_bio(WRITE_SYNC, bio);
2131                 INCREMENT(lmStat.submitted);
2132         }
2133         else {
2134                 bio->bi_size = 0;
2135                 lbmIODone(bio, 0, 0); /* 2nd argument appears to not be used => 0
2136                                        *  3rd argument appears to not be used => 0
2137                                        */
2138         }
2139 }
2140
2141
2142 /*
2143  *      lbmIOWait()
2144  */
2145 static int lbmIOWait(struct lbuf * bp, int flag)
2146 {
2147         unsigned long flags;
2148         int rc = 0;
2149
2150         jfs_info("lbmIOWait1: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
2151
2152         LCACHE_LOCK(flags);             /* disable+lock */
2153
2154         LCACHE_SLEEP_COND(bp->l_ioevent, (bp->l_flag & lbmDONE), flags);
2155
2156         rc = (bp->l_flag & lbmERROR) ? -EIO : 0;
2157
2158         if (flag & lbmFREE)
2159                 lbmfree(bp);
2160
2161         LCACHE_UNLOCK(flags);   /* unlock+enable */
2162
2163         jfs_info("lbmIOWait2: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
2164         return rc;
2165 }
2166
2167 /*
2168  *      lbmIODone()
2169  *
2170  * executed at INTIODONE level
2171  */
2172 static int lbmIODone(struct bio *bio, unsigned int bytes_done, int error)
2173 {
2174         struct lbuf *bp = bio->bi_private;
2175         struct lbuf *nextbp, *tail;
2176         struct jfs_log *log;
2177         unsigned long flags;
2178
2179         if (bio->bi_size)
2180                 return 1;
2181
2182         /*
2183          * get back jfs buffer bound to the i/o buffer
2184          */
2185         jfs_info("lbmIODone: bp:0x%p flag:0x%x", bp, bp->l_flag);
2186
2187         LCACHE_LOCK(flags);             /* disable+lock */
2188
2189         bp->l_flag |= lbmDONE;
2190
2191         if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2192                 bp->l_flag |= lbmERROR;
2193
2194                 jfs_err("lbmIODone: I/O error in JFS log");
2195         }
2196
2197         bio_put(bio);
2198
2199         /*
2200          *      pagein completion
2201          */
2202         if (bp->l_flag & lbmREAD) {
2203                 bp->l_flag &= ~lbmREAD;
2204
2205                 LCACHE_UNLOCK(flags);   /* unlock+enable */
2206
2207                 /* wakeup I/O initiator */
2208                 LCACHE_WAKEUP(&bp->l_ioevent);
2209
2210                 return 0;
2211         }
2212
2213         /*
2214          *      pageout completion
2215          *
2216          * the bp at the head of write queue has completed pageout.
2217          *
2218          * if single-commit/full-page pageout, remove the current buffer
2219          * from head of pageout queue, and redrive pageout with
2220          * the new buffer at head of pageout queue;
2221          * otherwise, the partial-page pageout buffer stays at
2222          * the head of pageout queue to be redriven for pageout
2223          * by lmGroupCommit() until full-page pageout is completed.
2224          */
2225         bp->l_flag &= ~lbmWRITE;
2226         INCREMENT(lmStat.pagedone);
2227
2228         /* update committed lsn */
2229         log = bp->l_log;
2230         log->clsn = (bp->l_pn << L2LOGPSIZE) + bp->l_ceor;
2231
2232         if (bp->l_flag & lbmDIRECT) {
2233                 LCACHE_WAKEUP(&bp->l_ioevent);
2234                 LCACHE_UNLOCK(flags);
2235                 return 0;
2236         }
2237
2238         tail = log->wqueue;
2239
2240         /* single element queue */
2241         if (bp == tail) {
2242                 /* remove head buffer of full-page pageout
2243                  * from log device write queue
2244                  */
2245                 if (bp->l_flag & lbmRELEASE) {
2246                         log->wqueue = NULL;
2247                         bp->l_wqnext = NULL;
2248                 }
2249         }
2250         /* multi element queue */
2251         else {
2252                 /* remove head buffer of full-page pageout
2253                  * from log device write queue
2254                  */
2255                 if (bp->l_flag & lbmRELEASE) {
2256                         nextbp = tail->l_wqnext = bp->l_wqnext;
2257                         bp->l_wqnext = NULL;
2258
2259                         /*
2260                          * redrive pageout of next page at head of write queue:
2261                          * redrive next page without any bound tblk
2262                          * (i.e., page w/o any COMMIT records), or
2263                          * first page of new group commit which has been
2264                          * queued after current page (subsequent pageout
2265                          * is performed synchronously, except page without
2266                          * any COMMITs) by lmGroupCommit() as indicated
2267                          * by lbmWRITE flag;
2268                          */
2269                         if (nextbp->l_flag & lbmWRITE) {
2270                                 /*
2271                                  * We can't do the I/O at interrupt time.
2272                                  * The jfsIO thread can do it
2273                                  */
2274                                 lbmRedrive(nextbp);
2275                         }
2276                 }
2277         }
2278
2279         /*
2280          *      synchronous pageout:
2281          *
2282          * buffer has not necessarily been removed from write queue
2283          * (e.g., synchronous write of partial-page with COMMIT):
2284          * leave buffer for i/o initiator to dispose
2285          */
2286         if (bp->l_flag & lbmSYNC) {
2287                 LCACHE_UNLOCK(flags);   /* unlock+enable */
2288
2289                 /* wakeup I/O initiator */
2290                 LCACHE_WAKEUP(&bp->l_ioevent);
2291         }
2292
2293         /*
2294          *      Group Commit pageout:
2295          */
2296         else if (bp->l_flag & lbmGC) {
2297                 LCACHE_UNLOCK(flags);
2298                 lmPostGC(bp);
2299         }
2300
2301         /*
2302          *      asynchronous pageout:
2303          *
2304          * buffer must have been removed from write queue:
2305          * insert buffer at head of freelist where it can be recycled
2306          */
2307         else {
2308                 assert(bp->l_flag & lbmRELEASE);
2309                 assert(bp->l_flag & lbmFREE);
2310                 lbmfree(bp);
2311
2312                 LCACHE_UNLOCK(flags);   /* unlock+enable */
2313         }
2314
2315         return 0;
2316 }
2317
2318 int jfsIOWait(void *arg)
2319 {
2320         struct lbuf *bp;
2321
2322         daemonize("jfsIO");
2323
2324         complete(&jfsIOwait);
2325
2326         do {
2327                 DECLARE_WAITQUEUE(wq, current);
2328
2329                 spin_lock_irq(&log_redrive_lock);
2330                 while ((bp = log_redrive_list) != 0) {
2331                         log_redrive_list = bp->l_redrive_next;
2332                         bp->l_redrive_next = NULL;
2333                         spin_unlock_irq(&log_redrive_lock);
2334                         lbmStartIO(bp);
2335                         spin_lock_irq(&log_redrive_lock);
2336                 }
2337                 if (current->flags & PF_FREEZE) {
2338                         spin_unlock_irq(&log_redrive_lock);
2339                         refrigerator(PF_FREEZE);
2340                 } else {
2341                         add_wait_queue(&jfs_IO_thread_wait, &wq);
2342                         set_current_state(TASK_INTERRUPTIBLE);
2343                         spin_unlock_irq(&log_redrive_lock);
2344                         schedule();
2345                         current->state = TASK_RUNNING;
2346                         remove_wait_queue(&jfs_IO_thread_wait, &wq);
2347                 }
2348         } while (!jfs_stop_threads);
2349
2350         jfs_info("jfsIOWait being killed!");
2351         complete_and_exit(&jfsIOwait, 0);
2352 }
2353
2354 /*
2355  * NAME:        lmLogFormat()/jfs_logform()
2356  *
2357  * FUNCTION:    format file system log
2358  *
2359  * PARAMETERS:
2360  *      log     - volume log
2361  *      logAddress - start address of log space in FS block
2362  *      logSize - length of log space in FS block;
2363  *
2364  * RETURN:      0       - success
2365  *              -EIO    - i/o error
2366  *
2367  * XXX: We're synchronously writing one page at a time.  This needs to
2368  *      be improved by writing multiple pages at once.
2369  */
2370 int lmLogFormat(struct jfs_log *log, s64 logAddress, int logSize)
2371 {
2372         int rc = -EIO;
2373         struct jfs_sb_info *sbi;
2374         struct logsuper *logsuper;
2375         struct logpage *lp;
2376         int lspn;               /* log sequence page number */
2377         struct lrd *lrd_ptr;
2378         int npages = 0;
2379         struct lbuf *bp;
2380
2381         jfs_info("lmLogFormat: logAddress:%Ld logSize:%d",
2382                  (long long)logAddress, logSize);
2383
2384         sbi = list_entry(log->sb_list.next, struct jfs_sb_info, log_list);
2385
2386         /* allocate a log buffer */
2387         bp = lbmAllocate(log, 1);
2388
2389         npages = logSize >> sbi->l2nbperpage;
2390
2391         /*
2392          *      log space:
2393          *
2394          * page 0 - reserved;
2395          * page 1 - log superblock;
2396          * page 2 - log data page: A SYNC log record is written
2397          *          into this page at logform time;
2398          * pages 3-N - log data page: set to empty log data pages;
2399          */
2400         /*
2401          *      init log superblock: log page 1
2402          */
2403         logsuper = (struct logsuper *) bp->l_ldata;
2404
2405         logsuper->magic = cpu_to_le32(LOGMAGIC);
2406         logsuper->version = cpu_to_le32(LOGVERSION);
2407         logsuper->state = cpu_to_le32(LOGREDONE);
2408         logsuper->flag = cpu_to_le32(sbi->mntflag);     /* ? */
2409         logsuper->size = cpu_to_le32(npages);
2410         logsuper->bsize = cpu_to_le32(sbi->bsize);
2411         logsuper->l2bsize = cpu_to_le32(sbi->l2bsize);
2412         logsuper->end = cpu_to_le32(2 * LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE);
2413
2414         bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
2415         bp->l_blkno = logAddress + sbi->nbperpage;
2416         lbmStartIO(bp);
2417         if ((rc = lbmIOWait(bp, 0)))
2418                 goto exit;
2419
2420         /*
2421          *      init pages 2 to npages-1 as log data pages:
2422          *
2423          * log page sequence number (lpsn) initialization:
2424          *
2425          * pn:   0     1     2     3                 n-1
2426          *       +-----+-----+=====+=====+===.....===+=====+
2427          * lspn:             N-1   0     1           N-2
2428          *                   <--- N page circular file ---->
2429          *
2430          * the N (= npages-2) data pages of the log is maintained as
2431          * a circular file for the log records;
2432          * lpsn grows by 1 monotonically as each log page is written
2433          * to the circular file of the log;
2434          * and setLogpage() will not reset the page number even if
2435          * the eor is equal to LOGPHDRSIZE. In order for binary search
2436          * still work in find log end process, we have to simulate the
2437          * log wrap situation at the log format time.
2438          * The 1st log page written will have the highest lpsn. Then
2439          * the succeeding log pages will have ascending order of
2440          * the lspn starting from 0, ... (N-2)
2441          */
2442         lp = (struct logpage *) bp->l_ldata;
2443         /*
2444          * initialize 1st log page to be written: lpsn = N - 1,
2445          * write a SYNCPT log record is written to this page
2446          */
2447         lp->h.page = lp->t.page = cpu_to_le32(npages - 3);
2448         lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE + LOGRDSIZE);
2449
2450         lrd_ptr = (struct lrd *) &lp->data;
2451         lrd_ptr->logtid = 0;
2452         lrd_ptr->backchain = 0;
2453         lrd_ptr->type = cpu_to_le16(LOG_SYNCPT);
2454         lrd_ptr->length = 0;
2455         lrd_ptr->log.syncpt.sync = 0;
2456
2457         bp->l_blkno += sbi->nbperpage;
2458         bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
2459         lbmStartIO(bp);
2460         if ((rc = lbmIOWait(bp, 0)))
2461                 goto exit;
2462
2463         /*
2464          *      initialize succeeding log pages: lpsn = 0, 1, ..., (N-2)
2465          */
2466         for (lspn = 0; lspn < npages - 3; lspn++) {
2467                 lp->h.page = lp->t.page = cpu_to_le32(lspn);
2468                 lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
2469
2470                 bp->l_blkno += sbi->nbperpage;
2471                 bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
2472                 lbmStartIO(bp);
2473                 if ((rc = lbmIOWait(bp, 0)))
2474                         goto exit;
2475         }
2476
2477         rc = 0;
2478 exit:
2479         /*
2480          *      finalize log
2481          */
2482         /* release the buffer */
2483         lbmFree(bp);
2484
2485         return rc;
2486 }
2487
2488 #ifdef CONFIG_JFS_STATISTICS
2489 int jfs_lmstats_read(char *buffer, char **start, off_t offset, int length,
2490                       int *eof, void *data)
2491 {
2492         int len = 0;
2493         off_t begin;
2494
2495         len += sprintf(buffer,
2496                        "JFS Logmgr stats\n"
2497                        "================\n"
2498                        "commits = %d\n"
2499                        "writes submitted = %d\n"
2500                        "writes completed = %d\n"
2501                        "full pages submitted = %d\n"
2502                        "partial pages submitted = %d\n",
2503                        lmStat.commit,
2504                        lmStat.submitted,
2505                        lmStat.pagedone,
2506                        lmStat.full_page,
2507                        lmStat.partial_page);
2508
2509         begin = offset;
2510         *start = buffer + begin;
2511         len -= begin;
2512
2513         if (len > length)
2514                 len = length;
2515         else
2516                 *eof = 1;
2517
2518         if (len < 0)
2519                 len = 0;
2520
2521         return len;
2522 }
2523 #endif /* CONFIG_JFS_STATISTICS */