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