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