MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / jfs / jfs_logmgr.c
blob83dc99db1e5b0002243d0cae5f6b01b3fbbc950e
1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
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.
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
21 * jfs_logmgr.c: log manager
23 * for related information, see transaction manager (jfs_txnmgr.c), and
24 * recovery manager (jfs_logredo.c).
26 * note: for detail, RTFS.
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
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.
41 * TBD lazy commit:
42 * transactions are committed asynchronously when the log page
43 * containing it COMMIT is paged out when it becomes full;
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;
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).
57 * alternatives:
58 * lsn - 64-bit monotonically increasing integer vs
59 * 32-bit lspn and page eor.
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"
78 * lbuf's ready to be redriven. Protected by log_redrive_lock (jfsIO thread)
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);
86 * log read/write serialization (per log)
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))
94 * log group commit serialization (per log)
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)
103 * log sync serialization (per log)
105 #define LOGSYNC_DELTA(logsize) min((logsize)/8, 128*LOGPSIZE)
106 #define LOGSYNC_BARRIER(logsize) ((logsize)/4)
108 #define LOGSYNC_DELTA(logsize) min((logsize)/4, 256*LOGPSIZE)
109 #define LOGSYNC_BARRIER(logsize) ((logsize)/2)
114 * log buffer cache synchronization
116 static spinlock_t jfsLCacheLock = SPIN_LOCK_UNLOCKED;
118 #define LCACHE_LOCK(flags) spin_lock_irqsave(&jfsLCacheLock, flags)
119 #define LCACHE_UNLOCK(flags) spin_unlock_irqrestore(&jfsLCacheLock, flags)
122 * See __SLEEP_COND in jfs_locks.h
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)
131 #define LCACHE_WAKEUP(event) wake_up(event)
135 * lbuf buffer cache (lCache) control
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;
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;
147 #define lbmSYNC 0x0008 /* do not return to freelist
148 * when removed from write queue;
150 #define lbmFREE 0x0010 /* return to freelist
151 * at completion of pageout;
152 * the buffer may be recycled;
154 #define lbmDONE 0x0020
155 #define lbmERROR 0x0040
156 #define lbmGC 0x0080 /* lbmIODone to perform post-GC processing
157 * of log page
159 #define lbmDIRECT 0x0100
162 * Global list of active external journals
164 LIST_HEAD(jfs_external_logs);
165 struct jfs_log *dummy_log = NULL;
166 DECLARE_MUTEX(jfs_log_sem);
169 * external references
171 extern void txLazyUnlock(struct tblock * tblk);
172 extern int jfs_stop_threads;
173 extern struct completion jfsIOwait;
174 extern int jfs_tlocks_low;
177 * forward references
179 static int lmWriteRecord(struct jfs_log * log, struct tblock * tblk,
180 struct lrd * lrd, struct tlock * tlck);
182 static int lmNextPage(struct jfs_log * log);
183 static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
184 int activate);
186 static int open_inline_log(struct super_block *sb);
187 static int open_dummy_log(struct super_block *sb);
188 static int lbmLogInit(struct jfs_log * log);
189 static void lbmLogShutdown(struct jfs_log * log);
190 static struct lbuf *lbmAllocate(struct jfs_log * log, int);
191 static void lbmFree(struct lbuf * bp);
192 static void lbmfree(struct lbuf * bp);
193 static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp);
194 static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag, int cant_block);
195 static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag);
196 static int lbmIOWait(struct lbuf * bp, int flag);
197 static bio_end_io_t lbmIODone;
198 static void lbmStartIO(struct lbuf * bp);
199 static void lmGCwrite(struct jfs_log * log, int cant_block);
200 static int lmLogSync(struct jfs_log * log, int nosyncwait);
205 * statistics
207 #ifdef CONFIG_JFS_STATISTICS
208 struct lmStat {
209 uint commit; /* # of commit */
210 uint pagedone; /* # of page written */
211 uint submitted; /* # of pages submitted */
212 uint full_page; /* # of full pages submitted */
213 uint partial_page; /* # of partial pages submitted */
214 } lmStat;
215 #endif
219 * NAME: lmLog()
221 * FUNCTION: write a log record;
223 * PARAMETER:
225 * RETURN: lsn - offset to the next log record to write (end-of-log);
226 * -1 - error;
228 * note: todo: log error handler
230 int lmLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
231 struct tlock * tlck)
233 int lsn;
234 int diffp, difft;
235 struct metapage *mp = NULL;
237 jfs_info("lmLog: log:0x%p tblk:0x%p, lrd:0x%p tlck:0x%p",
238 log, tblk, lrd, tlck);
240 LOG_LOCK(log);
242 /* log by (out-of-transaction) JFS ? */
243 if (tblk == NULL)
244 goto writeRecord;
246 /* log from page ? */
247 if (tlck == NULL ||
248 tlck->type & tlckBTROOT || (mp = tlck->mp) == NULL)
249 goto writeRecord;
252 * initialize/update page/transaction recovery lsn
254 lsn = log->lsn;
256 LOGSYNC_LOCK(log);
259 * initialize page lsn if first log write of the page
261 if (mp->lsn == 0) {
262 mp->log = log;
263 mp->lsn = lsn;
264 log->count++;
266 /* insert page at tail of logsynclist */
267 list_add_tail(&mp->synclist, &log->synclist);
271 * initialize/update lsn of tblock of the page
273 * transaction inherits oldest lsn of pages associated
274 * with allocation/deallocation of resources (their
275 * log records are used to reconstruct allocation map
276 * at recovery time: inode for inode allocation map,
277 * B+-tree index of extent descriptors for block
278 * allocation map);
279 * allocation map pages inherit transaction lsn at
280 * commit time to allow forwarding log syncpt past log
281 * records associated with allocation/deallocation of
282 * resources only after persistent map of these map pages
283 * have been updated and propagated to home.
286 * initialize transaction lsn:
288 if (tblk->lsn == 0) {
289 /* inherit lsn of its first page logged */
290 tblk->lsn = mp->lsn;
291 log->count++;
293 /* insert tblock after the page on logsynclist */
294 list_add(&tblk->synclist, &mp->synclist);
297 * update transaction lsn:
299 else {
300 /* inherit oldest/smallest lsn of page */
301 logdiff(diffp, mp->lsn, log);
302 logdiff(difft, tblk->lsn, log);
303 if (diffp < difft) {
304 /* update tblock lsn with page lsn */
305 tblk->lsn = mp->lsn;
307 /* move tblock after page on logsynclist */
308 list_move(&tblk->synclist, &mp->synclist);
312 LOGSYNC_UNLOCK(log);
315 * write the log record
317 writeRecord:
318 lsn = lmWriteRecord(log, tblk, lrd, tlck);
321 * forward log syncpt if log reached next syncpt trigger
323 logdiff(diffp, lsn, log);
324 if (diffp >= log->nextsync)
325 lsn = lmLogSync(log, 0);
327 /* update end-of-log lsn */
328 log->lsn = lsn;
330 LOG_UNLOCK(log);
332 /* return end-of-log address */
333 return lsn;
338 * NAME: lmWriteRecord()
340 * FUNCTION: move the log record to current log page
342 * PARAMETER: cd - commit descriptor
344 * RETURN: end-of-log address
346 * serialization: LOG_LOCK() held on entry/exit
348 static int
349 lmWriteRecord(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
350 struct tlock * tlck)
352 int lsn = 0; /* end-of-log address */
353 struct lbuf *bp; /* dst log page buffer */
354 struct logpage *lp; /* dst log page */
355 caddr_t dst; /* destination address in log page */
356 int dstoffset; /* end-of-log offset in log page */
357 int freespace; /* free space in log page */
358 caddr_t p; /* src meta-data page */
359 caddr_t src;
360 int srclen;
361 int nbytes; /* number of bytes to move */
362 int i;
363 int len;
364 struct linelock *linelock;
365 struct lv *lv;
366 struct lvd *lvd;
367 int l2linesize;
369 len = 0;
371 /* retrieve destination log page to write */
372 bp = (struct lbuf *) log->bp;
373 lp = (struct logpage *) bp->l_ldata;
374 dstoffset = log->eor;
376 /* any log data to write ? */
377 if (tlck == NULL)
378 goto moveLrd;
381 * move log record data
383 /* retrieve source meta-data page to log */
384 if (tlck->flag & tlckPAGELOCK) {
385 p = (caddr_t) (tlck->mp->data);
386 linelock = (struct linelock *) & tlck->lock;
388 /* retrieve source in-memory inode to log */
389 else if (tlck->flag & tlckINODELOCK) {
390 if (tlck->type & tlckDTREE)
391 p = (caddr_t) &JFS_IP(tlck->ip)->i_dtroot;
392 else
393 p = (caddr_t) &JFS_IP(tlck->ip)->i_xtroot;
394 linelock = (struct linelock *) & tlck->lock;
396 #ifdef _JFS_WIP
397 else if (tlck->flag & tlckINLINELOCK) {
399 inlinelock = (struct inlinelock *) & tlck;
400 p = (caddr_t) & inlinelock->pxd;
401 linelock = (struct linelock *) & tlck;
403 #endif /* _JFS_WIP */
404 else {
405 jfs_err("lmWriteRecord: UFO tlck:0x%p", tlck);
406 return 0; /* Probably should trap */
408 l2linesize = linelock->l2linesize;
410 moveData:
411 ASSERT(linelock->index <= linelock->maxcnt);
413 lv = linelock->lv;
414 for (i = 0; i < linelock->index; i++, lv++) {
415 if (lv->length == 0)
416 continue;
418 /* is page full ? */
419 if (dstoffset >= LOGPSIZE - LOGPTLRSIZE) {
420 /* page become full: move on to next page */
421 lmNextPage(log);
423 bp = log->bp;
424 lp = (struct logpage *) bp->l_ldata;
425 dstoffset = LOGPHDRSIZE;
429 * move log vector data
431 src = (u8 *) p + (lv->offset << l2linesize);
432 srclen = lv->length << l2linesize;
433 len += srclen;
434 while (srclen > 0) {
435 freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
436 nbytes = min(freespace, srclen);
437 dst = (caddr_t) lp + dstoffset;
438 memcpy(dst, src, nbytes);
439 dstoffset += nbytes;
441 /* is page not full ? */
442 if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
443 break;
445 /* page become full: move on to next page */
446 lmNextPage(log);
448 bp = (struct lbuf *) log->bp;
449 lp = (struct logpage *) bp->l_ldata;
450 dstoffset = LOGPHDRSIZE;
452 srclen -= nbytes;
453 src += nbytes;
457 * move log vector descriptor
459 len += 4;
460 lvd = (struct lvd *) ((caddr_t) lp + dstoffset);
461 lvd->offset = cpu_to_le16(lv->offset);
462 lvd->length = cpu_to_le16(lv->length);
463 dstoffset += 4;
464 jfs_info("lmWriteRecord: lv offset:%d length:%d",
465 lv->offset, lv->length);
468 if ((i = linelock->next)) {
469 linelock = (struct linelock *) lid_to_tlock(i);
470 goto moveData;
474 * move log record descriptor
476 moveLrd:
477 lrd->length = cpu_to_le16(len);
479 src = (caddr_t) lrd;
480 srclen = LOGRDSIZE;
482 while (srclen > 0) {
483 freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
484 nbytes = min(freespace, srclen);
485 dst = (caddr_t) lp + dstoffset;
486 memcpy(dst, src, nbytes);
488 dstoffset += nbytes;
489 srclen -= nbytes;
491 /* are there more to move than freespace of page ? */
492 if (srclen)
493 goto pageFull;
496 * end of log record descriptor
499 /* update last log record eor */
500 log->eor = dstoffset;
501 bp->l_eor = dstoffset;
502 lsn = (log->page << L2LOGPSIZE) + dstoffset;
504 if (lrd->type & cpu_to_le16(LOG_COMMIT)) {
505 tblk->clsn = lsn;
506 jfs_info("wr: tclsn:0x%x, beor:0x%x", tblk->clsn,
507 bp->l_eor);
509 INCREMENT(lmStat.commit); /* # of commit */
512 * enqueue tblock for group commit:
514 * enqueue tblock of non-trivial/synchronous COMMIT
515 * at tail of group commit queue
516 * (trivial/asynchronous COMMITs are ignored by
517 * group commit.)
519 LOGGC_LOCK(log);
521 /* init tblock gc state */
522 tblk->flag = tblkGC_QUEUE;
523 tblk->bp = log->bp;
524 tblk->pn = log->page;
525 tblk->eor = log->eor;
527 /* enqueue transaction to commit queue */
528 list_add_tail(&tblk->cqueue, &log->cqueue);
530 LOGGC_UNLOCK(log);
533 jfs_info("lmWriteRecord: lrd:0x%04x bp:0x%p pn:%d eor:0x%x",
534 le16_to_cpu(lrd->type), log->bp, log->page, dstoffset);
536 /* page not full ? */
537 if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
538 return lsn;
540 pageFull:
541 /* page become full: move on to next page */
542 lmNextPage(log);
544 bp = (struct lbuf *) log->bp;
545 lp = (struct logpage *) bp->l_ldata;
546 dstoffset = LOGPHDRSIZE;
547 src += nbytes;
550 return lsn;
555 * NAME: lmNextPage()
557 * FUNCTION: write current page and allocate next page.
559 * PARAMETER: log
561 * RETURN: 0
563 * serialization: LOG_LOCK() held on entry/exit
565 static int lmNextPage(struct jfs_log * log)
567 struct logpage *lp;
568 int lspn; /* log sequence page number */
569 int pn; /* current page number */
570 struct lbuf *bp;
571 struct lbuf *nextbp;
572 struct tblock *tblk;
574 /* get current log page number and log sequence page number */
575 pn = log->page;
576 bp = log->bp;
577 lp = (struct logpage *) bp->l_ldata;
578 lspn = le32_to_cpu(lp->h.page);
580 LOGGC_LOCK(log);
583 * write or queue the full page at the tail of write queue
585 /* get the tail tblk on commit queue */
586 if (list_empty(&log->cqueue))
587 tblk = NULL;
588 else
589 tblk = list_entry(log->cqueue.prev, struct tblock, cqueue);
591 /* every tblk who has COMMIT record on the current page,
592 * and has not been committed, must be on commit queue
593 * since tblk is queued at commit queueu at the time
594 * of writing its COMMIT record on the page before
595 * page becomes full (even though the tblk thread
596 * who wrote COMMIT record may have been suspended
597 * currently);
600 /* is page bound with outstanding tail tblk ? */
601 if (tblk && tblk->pn == pn) {
602 /* mark tblk for end-of-page */
603 tblk->flag |= tblkGC_EOP;
605 if (log->cflag & logGC_PAGEOUT) {
606 /* if page is not already on write queue,
607 * just enqueue (no lbmWRITE to prevent redrive)
608 * buffer to wqueue to ensure correct serial order
609 * of the pages since log pages will be added
610 * continuously
612 if (bp->l_wqnext == NULL)
613 lbmWrite(log, bp, 0, 0);
614 } else {
616 * No current GC leader, initiate group commit
618 log->cflag |= logGC_PAGEOUT;
619 lmGCwrite(log, 0);
622 /* page is not bound with outstanding tblk:
623 * init write or mark it to be redriven (lbmWRITE)
625 else {
626 /* finalize the page */
627 bp->l_ceor = bp->l_eor;
628 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
629 lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE, 0);
631 LOGGC_UNLOCK(log);
634 * allocate/initialize next page
636 /* if log wraps, the first data page of log is 2
637 * (0 never used, 1 is superblock).
639 log->page = (pn == log->size - 1) ? 2 : pn + 1;
640 log->eor = LOGPHDRSIZE; /* ? valid page empty/full at logRedo() */
642 /* allocate/initialize next log page buffer */
643 nextbp = lbmAllocate(log, log->page);
644 nextbp->l_eor = log->eor;
645 log->bp = nextbp;
647 /* initialize next log page */
648 lp = (struct logpage *) nextbp->l_ldata;
649 lp->h.page = lp->t.page = cpu_to_le32(lspn + 1);
650 lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
652 return 0;
657 * NAME: lmGroupCommit()
659 * FUNCTION: group commit
660 * initiate pageout of the pages with COMMIT in the order of
661 * page number - redrive pageout of the page at the head of
662 * pageout queue until full page has been written.
664 * RETURN:
666 * NOTE:
667 * LOGGC_LOCK serializes log group commit queue, and
668 * transaction blocks on the commit queue.
669 * N.B. LOG_LOCK is NOT held during lmGroupCommit().
671 int lmGroupCommit(struct jfs_log * log, struct tblock * tblk)
673 int rc = 0;
675 LOGGC_LOCK(log);
677 /* group committed already ? */
678 if (tblk->flag & tblkGC_COMMITTED) {
679 if (tblk->flag & tblkGC_ERROR)
680 rc = -EIO;
682 LOGGC_UNLOCK(log);
683 return rc;
685 jfs_info("lmGroup Commit: tblk = 0x%p, gcrtc = %d", tblk, log->gcrtc);
687 if (tblk->xflag & COMMIT_LAZY)
688 tblk->flag |= tblkGC_LAZY;
690 if ((!(log->cflag & logGC_PAGEOUT)) && (!list_empty(&log->cqueue)) &&
691 (!(tblk->xflag & COMMIT_LAZY) || test_bit(log_FLUSH, &log->flag)
692 || jfs_tlocks_low)) {
694 * No pageout in progress
696 * start group commit as its group leader.
698 log->cflag |= logGC_PAGEOUT;
700 lmGCwrite(log, 0);
703 if (tblk->xflag & COMMIT_LAZY) {
705 * Lazy transactions can leave now
707 LOGGC_UNLOCK(log);
708 return 0;
711 /* lmGCwrite gives up LOGGC_LOCK, check again */
713 if (tblk->flag & tblkGC_COMMITTED) {
714 if (tblk->flag & tblkGC_ERROR)
715 rc = -EIO;
717 LOGGC_UNLOCK(log);
718 return rc;
721 /* upcount transaction waiting for completion
723 log->gcrtc++;
724 tblk->flag |= tblkGC_READY;
726 __SLEEP_COND(tblk->gcwait, (tblk->flag & tblkGC_COMMITTED),
727 LOGGC_LOCK(log), LOGGC_UNLOCK(log));
729 /* removed from commit queue */
730 if (tblk->flag & tblkGC_ERROR)
731 rc = -EIO;
733 LOGGC_UNLOCK(log);
734 return rc;
738 * NAME: lmGCwrite()
740 * FUNCTION: group commit write
741 * initiate write of log page, building a group of all transactions
742 * with commit records on that page.
744 * RETURN: None
746 * NOTE:
747 * LOGGC_LOCK must be held by caller.
748 * N.B. LOG_LOCK is NOT held during lmGroupCommit().
750 static void lmGCwrite(struct jfs_log * log, int cant_write)
752 struct lbuf *bp;
753 struct logpage *lp;
754 int gcpn; /* group commit page number */
755 struct tblock *tblk;
756 struct tblock *xtblk = NULL;
759 * build the commit group of a log page
761 * scan commit queue and make a commit group of all
762 * transactions with COMMIT records on the same log page.
764 /* get the head tblk on the commit queue */
765 gcpn = list_entry(log->cqueue.next, struct tblock, cqueue)->pn;
767 list_for_each_entry(tblk, &log->cqueue, cqueue) {
768 if (tblk->pn != gcpn)
769 break;
771 xtblk = tblk;
773 /* state transition: (QUEUE, READY) -> COMMIT */
774 tblk->flag |= tblkGC_COMMIT;
776 tblk = xtblk; /* last tblk of the page */
779 * pageout to commit transactions on the log page.
781 bp = (struct lbuf *) tblk->bp;
782 lp = (struct logpage *) bp->l_ldata;
783 /* is page already full ? */
784 if (tblk->flag & tblkGC_EOP) {
785 /* mark page to free at end of group commit of the page */
786 tblk->flag &= ~tblkGC_EOP;
787 tblk->flag |= tblkGC_FREE;
788 bp->l_ceor = bp->l_eor;
789 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
790 lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmGC,
791 cant_write);
792 INCREMENT(lmStat.full_page);
794 /* page is not yet full */
795 else {
796 bp->l_ceor = tblk->eor; /* ? bp->l_ceor = bp->l_eor; */
797 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
798 lbmWrite(log, bp, lbmWRITE | lbmGC, cant_write);
799 INCREMENT(lmStat.partial_page);
804 * NAME: lmPostGC()
806 * FUNCTION: group commit post-processing
807 * Processes transactions after their commit records have been written
808 * to disk, redriving log I/O if necessary.
810 * RETURN: None
812 * NOTE:
813 * This routine is called a interrupt time by lbmIODone
815 static void lmPostGC(struct lbuf * bp)
817 unsigned long flags;
818 struct jfs_log *log = bp->l_log;
819 struct logpage *lp;
820 struct tblock *tblk, *temp;
822 //LOGGC_LOCK(log);
823 spin_lock_irqsave(&log->gclock, flags);
825 * current pageout of group commit completed.
827 * remove/wakeup transactions from commit queue who were
828 * group committed with the current log page
830 list_for_each_entry_safe(tblk, temp, &log->cqueue, cqueue) {
831 if (!(tblk->flag & tblkGC_COMMIT))
832 break;
833 /* if transaction was marked GC_COMMIT then
834 * it has been shipped in the current pageout
835 * and made it to disk - it is committed.
838 if (bp->l_flag & lbmERROR)
839 tblk->flag |= tblkGC_ERROR;
841 /* remove it from the commit queue */
842 list_del(&tblk->cqueue);
843 tblk->flag &= ~tblkGC_QUEUE;
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;
851 jfs_info("lmPostGC: tblk = 0x%p, flag = 0x%x", tblk,
852 tblk->flag);
854 if (!(tblk->xflag & COMMIT_FORCE))
856 * Hand tblk over to lazy commit thread
858 txLazyUnlock(tblk);
859 else {
860 /* state transition: COMMIT -> COMMITTED */
861 tblk->flag |= tblkGC_COMMITTED;
863 if (tblk->flag & tblkGC_READY)
864 log->gcrtc--;
866 LOGGC_WAKEUP(tblk);
869 /* was page full before pageout ?
870 * (and this is the last tblk bound with the page)
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)
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,
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.
896 if ((!list_empty(&log->cqueue)) &&
897 ((log->gcrtc > 0) || (tblk->bp->l_wqnext != NULL) ||
898 test_bit(log_FLUSH, &log->flag) || jfs_tlocks_low))
900 * Call lmGCwrite with new group leader
902 lmGCwrite(log, 1);
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.
909 else
910 log->cflag &= ~logGC_PAGEOUT;
912 //LOGGC_UNLOCK(log);
913 spin_unlock_irqrestore(&log->gclock, flags);
914 return;
918 * NAME: lmLogSync()
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.
929 * this is called only from lmLog().
931 * PARAMETER: ip - pointer to logs inode.
933 * RETURN: 0
935 * serialization: LOG_LOCK() held on entry/exit
937 static int lmLogSync(struct jfs_log * log, int nosyncwait)
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;
949 * forward syncpt
951 /* if last sync is same as last syncpt,
952 * invoke sync point forward processing to update sync.
955 if (log->sync == log->syncpt) {
956 LOGSYNC_LOCK(log);
957 /* ToDo: push dirty metapages out to disk */
958 // bmLogSync(log);
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;
967 LOGSYNC_UNLOCK(log);
971 /* if sync is different from last syncpt,
972 * write a SYNCPT record with syncpt = sync.
973 * reset syncpt = sync
975 if (log->sync != log->syncpt) {
976 struct jfs_sb_info *sbi;
979 * We need to make sure all of the "written" metapages
980 * actually make it to disk
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);
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);
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);
1000 log->syncpt = log->sync;
1001 } else
1002 lsn = log->lsn;
1005 * setup next syncpt trigger (SWAG)
1007 logsize = log->logsize;
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");
1016 * log wrapping
1018 * option 1 - panic ? No.!
1019 * option 2 - shutdown file systems
1020 * associated with log ?
1021 * option 3 - extend log ?
1024 * option 4 - second chance
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().
1032 /* mark log state log wrap in log superblock */
1033 /* log->state = LOGWRAP; */
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;
1042 /* return if lmLogSync() from outside of transaction, e.g., sync() */
1043 if (nosyncwait)
1044 return lsn;
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.
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);
1056 * We may have to initiate group commit
1058 jfs_flush_journal(log, 0);
1061 return lsn;
1066 * NAME: lmLogOpen()
1068 * FUNCTION: open the log on first open;
1069 * insert filesystem in the active list of the log.
1071 * PARAMETER: ipmnt - file system mount inode
1072 * iplog - log inode (out)
1074 * RETURN:
1076 * serialization:
1078 int lmLogOpen(struct super_block *sb)
1080 int rc;
1081 struct block_device *bdev;
1082 struct jfs_log *log;
1083 struct jfs_sb_info *sbi = JFS_SBI(sb);
1085 if (sbi->flag & JFS_NOINTEGRITY)
1086 return open_dummy_log(sb);
1088 if (sbi->mntflag & JFS_INLINELOG)
1089 return open_inline_log(sb);
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;
1101 * add file system to log active file system list
1103 if ((rc = lmLogFileSystem(log, sbi, 1))) {
1104 up(&jfs_log_sem);
1105 return rc;
1107 goto journal_found;
1111 if (!(log = kmalloc(sizeof(struct jfs_log), GFP_KERNEL))) {
1112 up(&jfs_log_sem);
1113 return -ENOMEM;
1115 memset(log, 0, sizeof(struct jfs_log));
1116 INIT_LIST_HEAD(&log->sb_list);
1119 * external log as separate logical volume
1121 * file systems to log may have n-to-1 relationship;
1124 bdev = open_by_devnum(sbi->logdev, FMODE_READ|FMODE_WRITE);
1125 if (IS_ERR(bdev)) {
1126 rc = -PTR_ERR(bdev);
1127 goto free;
1130 if ((rc = bd_claim(bdev, log))) {
1131 goto close;
1134 log->bdev = bdev;
1135 memcpy(log->uuid, sbi->loguuid, sizeof(log->uuid));
1138 * initialize log:
1140 if ((rc = lmLogInit(log)))
1141 goto unclaim;
1143 list_add(&log->journal_list, &jfs_external_logs);
1146 * add file system to log active file system list
1148 if ((rc = lmLogFileSystem(log, sbi, 1)))
1149 goto shutdown;
1151 journal_found:
1152 LOG_LOCK(log);
1153 list_add(&sbi->log_list, &log->sb_list);
1154 sbi->log = log;
1155 LOG_UNLOCK(log);
1157 up(&jfs_log_sem);
1158 return 0;
1161 * unwind on error
1163 shutdown: /* unwind lbmLogInit() */
1164 list_del(&log->journal_list);
1165 lbmLogShutdown(log);
1167 unclaim:
1168 bd_release(bdev);
1170 close: /* close external log device */
1171 blkdev_put(bdev);
1173 free: /* free log descriptor */
1174 up(&jfs_log_sem);
1175 kfree(log);
1177 jfs_warn("lmLogOpen: exit(%d)", rc);
1178 return rc;
1181 static int open_inline_log(struct super_block *sb)
1183 struct jfs_log *log;
1184 int rc;
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);
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);
1200 * initialize log.
1202 if ((rc = lmLogInit(log))) {
1203 kfree(log);
1204 jfs_warn("lmLogOpen: exit(%d)", rc);
1205 return rc;
1208 list_add(&JFS_SBI(sb)->log_list, &log->sb_list);
1209 JFS_SBI(sb)->log = log;
1211 return rc;
1214 static int open_dummy_log(struct super_block *sb)
1216 int rc;
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;
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;
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);
1246 return 0;
1250 * NAME: lmLogInit()
1252 * FUNCTION: log initialization at first log open.
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.
1259 * PARAMETER: log - log structure
1261 * RETURN: 0 - if ok
1262 * -EINVAL - bad log magic number or superblock dirty
1263 * error returned from logwait()
1265 * serialization: single first open thread
1267 int lmLogInit(struct jfs_log * log)
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;
1277 jfs_info("lmLogInit: log:0x%p", log);
1279 /* initialize the group commit serialization lock */
1280 LOGGC_LOCK_INIT(log);
1282 /* allocate/initialize the log write serialization lock */
1283 LOG_LOCK_INIT(log);
1285 LOGSYNC_LOCK_INIT(log);
1287 INIT_LIST_HEAD(&log->synclist);
1289 init_waitqueue_head(&log->syncwait);
1291 INIT_LIST_HEAD(&log->cqueue);
1292 log->flush_tblk = NULL;
1294 log->count = 0;
1297 * initialize log i/o
1299 if ((rc = lbmLogInit(log)))
1300 return rc;
1302 if (!test_bit(log_INLINELOG, &log->flag))
1303 log->l2bsize = L2LOGPSIZE;
1305 /* check for disabled journaling to disk */
1306 if (log->no_integrity) {
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.
1312 bp = lbmAllocate(log , 0);
1313 log->bp = bp;
1314 bp->l_pn = bp->l_eor = 0;
1315 } else {
1317 * validate log superblock
1319 if ((rc = lbmRead(log, 1, &bpsuper)))
1320 goto errout10;
1322 logsuper = (struct logsuper *) bpsuper->l_ldata;
1324 if (logsuper->magic != cpu_to_le32(LOGMAGIC)) {
1325 jfs_warn("*** Log Format Error ! ***");
1326 rc = -EINVAL;
1327 goto errout20;
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;
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;
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;
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);
1358 log->page = le32_to_cpu(logsuper->end) / LOGPSIZE;
1359 log->eor = le32_to_cpu(logsuper->end) - (LOGPSIZE * log->page);
1362 * initialize for log append write mode
1364 /* establish current/end-of-log page/buffer */
1365 if ((rc = lbmRead(log, log->page, &bp)))
1366 goto errout20;
1368 lp = (struct logpage *) bp->l_ldata;
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));
1374 log->bp = bp;
1375 bp->l_pn = log->page;
1376 bp->l_eor = log->eor;
1378 /* if current page is full, move on to next page */
1379 if (log->eor >= LOGPSIZE - LOGPTLRSIZE)
1380 lmNextPage(log);
1383 * initialize log syncpoint
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;
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;
1407 * update/write superblock
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;
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);
1424 jfs_info("lmLogInit: lsn:0x%x syncpt:0x%x sync:0x%x",
1425 log->lsn, log->syncpt, log->sync);
1428 * initialize for lazy/group commit
1430 log->clsn = lsn;
1432 return 0;
1435 * unwind on error
1437 errout30: /* release log page */
1438 lbmFree(bp);
1440 errout20: /* release log superblock */
1441 lbmFree(bpsuper);
1443 errout10: /* unwind lbmLogInit() */
1444 lbmLogShutdown(log);
1446 jfs_warn("lmLogInit: exit(%d)", rc);
1447 return rc;
1452 * NAME: lmLogClose()
1454 * FUNCTION: remove file system <ipmnt> from active list of log <iplog>
1455 * and close it on last close.
1457 * PARAMETER: sb - superblock
1459 * RETURN: errors from subroutines
1461 * serialization:
1463 int lmLogClose(struct super_block *sb)
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;
1470 jfs_info("lmLogClose: log:0x%p", log);
1472 down(&jfs_log_sem);
1473 LOG_LOCK(log);
1474 list_del(&sbi->log_list);
1475 LOG_UNLOCK(log);
1476 sbi->log = NULL;
1479 * We need to make sure all of the "written" metapages
1480 * actually make it to disk
1482 sync_blockdev(sb->s_bdev);
1484 if (test_bit(log_INLINELOG, &log->flag)) {
1486 * in-line log in host file system
1488 rc = lmLogShutdown(log);
1489 kfree(log);
1490 goto out;
1493 if (!log->no_integrity)
1494 lmLogFileSystem(log, sbi, 0);
1496 if (!list_empty(&log->sb_list))
1497 goto out;
1500 * TODO: ensure that the dummy_log is in a state to allow
1501 * lbmLogShutdown to deallocate all the buffers and call
1502 * kfree against dummy_log. For now, leave dummy_log & its
1503 * buffers in memory, and resuse if another no-integrity mount
1504 * is requested.
1506 if (log->no_integrity)
1507 goto out;
1510 * external log as separate logical volume
1512 list_del(&log->journal_list);
1513 bdev = log->bdev;
1514 rc = lmLogShutdown(log);
1516 bd_release(bdev);
1517 blkdev_put(bdev);
1519 kfree(log);
1521 out:
1522 up(&jfs_log_sem);
1523 jfs_info("lmLogClose: exit(%d)", rc);
1524 return rc;
1529 * NAME: jfs_flush_journal()
1531 * FUNCTION: initiate write of any outstanding transactions to the journal
1532 * and optionally wait until they are all written to disk
1534 * wait == 0 flush until latest txn is committed, don't wait
1535 * wait == 1 flush until latest txn is committed, wait
1536 * wait > 1 flush until all txn's are complete, wait
1538 void jfs_flush_journal(struct jfs_log *log, int wait)
1540 int i;
1541 struct tblock *target = NULL;
1543 /* jfs_write_inode may call us during read-only mount */
1544 if (!log)
1545 return;
1547 jfs_info("jfs_flush_journal: log:0x%p wait=%d", log, wait);
1549 LOGGC_LOCK(log);
1551 if (!list_empty(&log->cqueue)) {
1553 * This ensures that we will keep writing to the journal as long
1554 * as there are unwritten commit records
1556 target = list_entry(log->cqueue.prev, struct tblock, cqueue);
1558 if (test_bit(log_FLUSH, &log->flag)) {
1560 * We're already flushing.
1561 * if flush_tblk is NULL, we are flushing everything,
1562 * so leave it that way. Otherwise, update it to the
1563 * latest transaction
1565 if (log->flush_tblk)
1566 log->flush_tblk = target;
1567 } else {
1568 /* Only flush until latest transaction is committed */
1569 log->flush_tblk = target;
1570 set_bit(log_FLUSH, &log->flag);
1573 * Initiate I/O on outstanding transactions
1575 if (!(log->cflag & logGC_PAGEOUT)) {
1576 log->cflag |= logGC_PAGEOUT;
1577 lmGCwrite(log, 0);
1581 if ((wait > 1) || test_bit(log_SYNCBARRIER, &log->flag)) {
1582 /* Flush until all activity complete */
1583 set_bit(log_FLUSH, &log->flag);
1584 log->flush_tblk = NULL;
1587 if (wait && target && !(target->flag & tblkGC_COMMITTED)) {
1588 DECLARE_WAITQUEUE(__wait, current);
1590 add_wait_queue(&target->gcwait, &__wait);
1591 set_current_state(TASK_UNINTERRUPTIBLE);
1592 LOGGC_UNLOCK(log);
1593 schedule();
1594 current->state = TASK_RUNNING;
1595 LOGGC_LOCK(log);
1596 remove_wait_queue(&target->gcwait, &__wait);
1598 LOGGC_UNLOCK(log);
1600 if (wait < 2)
1601 return;
1604 * If there was recent activity, we may need to wait
1605 * for the lazycommit thread to catch up
1607 if ((!list_empty(&log->cqueue)) || !list_empty(&log->synclist)) {
1608 for (i = 0; i < 800; i++) { /* Too much? */
1609 current->state = TASK_INTERRUPTIBLE;
1610 schedule_timeout(HZ / 4);
1611 if (list_empty(&log->cqueue) &&
1612 list_empty(&log->synclist))
1613 break;
1616 assert(list_empty(&log->cqueue));
1617 assert(list_empty(&log->synclist));
1618 clear_bit(log_FLUSH, &log->flag);
1622 * NAME: lmLogShutdown()
1624 * FUNCTION: log shutdown at last LogClose().
1626 * write log syncpt record.
1627 * update super block to set redone flag to 0.
1629 * PARAMETER: log - log inode
1631 * RETURN: 0 - success
1633 * serialization: single last close thread
1635 int lmLogShutdown(struct jfs_log * log)
1637 int rc;
1638 struct lrd lrd;
1639 int lsn;
1640 struct logsuper *logsuper;
1641 struct lbuf *bpsuper;
1642 struct lbuf *bp;
1643 struct logpage *lp;
1645 jfs_info("lmLogShutdown: log:0x%p", log);
1647 jfs_flush_journal(log, 2);
1650 * write the last SYNCPT record with syncpoint = 0
1651 * (i.e., log redo up to HERE !)
1653 lrd.logtid = 0;
1654 lrd.backchain = 0;
1655 lrd.type = cpu_to_le16(LOG_SYNCPT);
1656 lrd.length = 0;
1657 lrd.log.syncpt.sync = 0;
1659 lsn = lmWriteRecord(log, NULL, &lrd, NULL);
1660 bp = log->bp;
1661 lp = (struct logpage *) bp->l_ldata;
1662 lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
1663 lbmWrite(log, log->bp, lbmWRITE | lbmRELEASE | lbmSYNC, 0);
1664 lbmIOWait(log->bp, lbmFREE);
1667 * synchronous update log superblock
1668 * mark log state as shutdown cleanly
1669 * (i.e., Log does not need to be replayed).
1671 if ((rc = lbmRead(log, 1, &bpsuper)))
1672 goto out;
1674 logsuper = (struct logsuper *) bpsuper->l_ldata;
1675 logsuper->state = cpu_to_le32(LOGREDONE);
1676 logsuper->end = cpu_to_le32(lsn);
1677 lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
1678 rc = lbmIOWait(bpsuper, lbmFREE);
1680 jfs_info("lmLogShutdown: lsn:0x%x page:%d eor:%d",
1681 lsn, log->page, log->eor);
1683 out:
1685 * shutdown per log i/o
1687 lbmLogShutdown(log);
1689 if (rc) {
1690 jfs_warn("lmLogShutdown: exit(%d)", rc);
1692 return rc;
1697 * NAME: lmLogFileSystem()
1699 * FUNCTION: insert (<activate> = true)/remove (<activate> = false)
1700 * file system into/from log active file system list.
1702 * PARAMETE: log - pointer to logs inode.
1703 * fsdev - kdev_t of filesystem.
1704 * serial - pointer to returned log serial number
1705 * activate - insert/remove device from active list.
1707 * RETURN: 0 - success
1708 * errors returned by vms_iowait().
1710 static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
1711 int activate)
1713 int rc = 0;
1714 int i;
1715 struct logsuper *logsuper;
1716 struct lbuf *bpsuper;
1717 char *uuid = sbi->uuid;
1720 * insert/remove file system device to log active file system list.
1722 if ((rc = lbmRead(log, 1, &bpsuper)))
1723 return rc;
1725 logsuper = (struct logsuper *) bpsuper->l_ldata;
1726 if (activate) {
1727 for (i = 0; i < MAX_ACTIVE; i++)
1728 if (!memcmp(logsuper->active[i].uuid, NULL_UUID, 16)) {
1729 memcpy(logsuper->active[i].uuid, uuid, 16);
1730 sbi->aggregate = i;
1731 break;
1733 if (i == MAX_ACTIVE) {
1734 jfs_warn("Too many file systems sharing journal!");
1735 lbmFree(bpsuper);
1736 return -EMFILE; /* Is there a better rc? */
1738 } else {
1739 for (i = 0; i < MAX_ACTIVE; i++)
1740 if (!memcmp(logsuper->active[i].uuid, uuid, 16)) {
1741 memcpy(logsuper->active[i].uuid, NULL_UUID, 16);
1742 break;
1744 if (i == MAX_ACTIVE) {
1745 jfs_warn("Somebody stomped on the journal!");
1746 lbmFree(bpsuper);
1747 return -EIO;
1753 * synchronous write log superblock:
1755 * write sidestream bypassing write queue:
1756 * at file system mount, log super block is updated for
1757 * activation of the file system before any log record
1758 * (MOUNT record) of the file system, and at file system
1759 * unmount, all meta data for the file system has been
1760 * flushed before log super block is updated for deactivation
1761 * of the file system.
1763 lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
1764 rc = lbmIOWait(bpsuper, lbmFREE);
1766 return rc;
1770 * log buffer manager (lbm)
1771 * ------------------------
1773 * special purpose buffer manager supporting log i/o requirements.
1775 * per log write queue:
1776 * log pageout occurs in serial order by fifo write queue and
1777 * restricting to a single i/o in pregress at any one time.
1778 * a circular singly-linked list
1779 * (log->wrqueue points to the tail, and buffers are linked via
1780 * bp->wrqueue field), and
1781 * maintains log page in pageout ot waiting for pageout in serial pageout.
1785 * lbmLogInit()
1787 * initialize per log I/O setup at lmLogInit()
1789 static int lbmLogInit(struct jfs_log * log)
1790 { /* log inode */
1791 int i;
1792 struct lbuf *lbuf;
1794 jfs_info("lbmLogInit: log:0x%p", log);
1796 /* initialize current buffer cursor */
1797 log->bp = NULL;
1799 /* initialize log device write queue */
1800 log->wqueue = NULL;
1803 * Each log has its own buffer pages allocated to it. These are
1804 * not managed by the page cache. This ensures that a transaction
1805 * writing to the log does not block trying to allocate a page from
1806 * the page cache (for the log). This would be bad, since page
1807 * allocation waits on the kswapd thread that may be committing inodes
1808 * which would cause log activity. Was that clear? I'm trying to
1809 * avoid deadlock here.
1811 init_waitqueue_head(&log->free_wait);
1813 log->lbuf_free = NULL;
1815 for (i = 0; i < LOGPAGES; i++) {
1816 lbuf = kmalloc(sizeof(struct lbuf), GFP_KERNEL);
1817 if (lbuf == 0)
1818 goto error;
1819 lbuf->l_ldata = (char *) get_zeroed_page(GFP_KERNEL);
1820 if (lbuf->l_ldata == 0) {
1821 kfree(lbuf);
1822 goto error;
1824 lbuf->l_log = log;
1825 init_waitqueue_head(&lbuf->l_ioevent);
1827 lbuf->l_freelist = log->lbuf_free;
1828 log->lbuf_free = lbuf;
1831 return (0);
1833 error:
1834 lbmLogShutdown(log);
1835 return -ENOMEM;
1840 * lbmLogShutdown()
1842 * finalize per log I/O setup at lmLogShutdown()
1844 static void lbmLogShutdown(struct jfs_log * log)
1846 struct lbuf *lbuf;
1848 jfs_info("lbmLogShutdown: log:0x%p", log);
1850 lbuf = log->lbuf_free;
1851 while (lbuf) {
1852 struct lbuf *next = lbuf->l_freelist;
1853 free_page((unsigned long) lbuf->l_ldata);
1854 kfree(lbuf);
1855 lbuf = next;
1858 log->bp = NULL;
1863 * lbmAllocate()
1865 * allocate an empty log buffer
1867 static struct lbuf *lbmAllocate(struct jfs_log * log, int pn)
1869 struct lbuf *bp;
1870 unsigned long flags;
1873 * recycle from log buffer freelist if any
1875 LCACHE_LOCK(flags);
1876 LCACHE_SLEEP_COND(log->free_wait, (bp = log->lbuf_free), flags);
1877 log->lbuf_free = bp->l_freelist;
1878 LCACHE_UNLOCK(flags);
1880 bp->l_flag = 0;
1882 bp->l_wqnext = NULL;
1883 bp->l_freelist = NULL;
1885 bp->l_pn = pn;
1886 bp->l_blkno = log->base + (pn << (L2LOGPSIZE - log->l2bsize));
1887 bp->l_ceor = 0;
1889 return bp;
1894 * lbmFree()
1896 * release a log buffer to freelist
1898 static void lbmFree(struct lbuf * bp)
1900 unsigned long flags;
1902 LCACHE_LOCK(flags);
1904 lbmfree(bp);
1906 LCACHE_UNLOCK(flags);
1909 static void lbmfree(struct lbuf * bp)
1911 struct jfs_log *log = bp->l_log;
1913 assert(bp->l_wqnext == NULL);
1916 * return the buffer to head of freelist
1918 bp->l_freelist = log->lbuf_free;
1919 log->lbuf_free = bp;
1921 wake_up(&log->free_wait);
1922 return;
1927 * NAME: lbmRedrive
1929 * FUNCTION: add a log buffer to the the log redrive list
1931 * PARAMETER:
1932 * bp - log buffer
1934 * NOTES:
1935 * Takes log_redrive_lock.
1937 static inline void lbmRedrive(struct lbuf *bp)
1939 unsigned long flags;
1941 spin_lock_irqsave(&log_redrive_lock, flags);
1942 bp->l_redrive_next = log_redrive_list;
1943 log_redrive_list = bp;
1944 spin_unlock_irqrestore(&log_redrive_lock, flags);
1946 wake_up(&jfs_IO_thread_wait);
1951 * lbmRead()
1953 static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)
1955 struct bio *bio;
1956 struct lbuf *bp;
1959 * allocate a log buffer
1961 *bpp = bp = lbmAllocate(log, pn);
1962 jfs_info("lbmRead: bp:0x%p pn:0x%x", bp, pn);
1964 bp->l_flag |= lbmREAD;
1966 bio = bio_alloc(GFP_NOFS, 1);
1968 bio->bi_sector = bp->l_blkno << (log->l2bsize - 9);
1969 bio->bi_bdev = log->bdev;
1970 bio->bi_io_vec[0].bv_page = virt_to_page(bp->l_ldata);
1971 bio->bi_io_vec[0].bv_len = LOGPSIZE;
1972 bio->bi_io_vec[0].bv_offset = 0;
1974 bio->bi_vcnt = 1;
1975 bio->bi_idx = 0;
1976 bio->bi_size = LOGPSIZE;
1978 bio->bi_end_io = lbmIODone;
1979 bio->bi_private = bp;
1980 submit_bio(READ_SYNC, bio);
1982 wait_event(bp->l_ioevent, (bp->l_flag != lbmREAD));
1984 return 0;
1989 * lbmWrite()
1991 * buffer at head of pageout queue stays after completion of
1992 * partial-page pageout and redriven by explicit initiation of
1993 * pageout by caller until full-page pageout is completed and
1994 * released.
1996 * device driver i/o done redrives pageout of new buffer at
1997 * head of pageout queue when current buffer at head of pageout
1998 * queue is released at the completion of its full-page pageout.
2000 * LOGGC_LOCK() serializes lbmWrite() by lmNextPage() and lmGroupCommit().
2001 * LCACHE_LOCK() serializes xflag between lbmWrite() and lbmIODone()
2003 static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
2004 int cant_block)
2006 struct lbuf *tail;
2007 unsigned long flags;
2009 jfs_info("lbmWrite: bp:0x%p flag:0x%x pn:0x%x", bp, flag, bp->l_pn);
2011 /* map the logical block address to physical block address */
2012 bp->l_blkno =
2013 log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
2015 LCACHE_LOCK(flags); /* disable+lock */
2018 * initialize buffer for device driver
2020 bp->l_flag = flag;
2023 * insert bp at tail of write queue associated with log
2025 * (request is either for bp already/currently at head of queue
2026 * or new bp to be inserted at tail)
2028 tail = log->wqueue;
2030 /* is buffer not already on write queue ? */
2031 if (bp->l_wqnext == NULL) {
2032 /* insert at tail of wqueue */
2033 if (tail == NULL) {
2034 log->wqueue = bp;
2035 bp->l_wqnext = bp;
2036 } else {
2037 log->wqueue = bp;
2038 bp->l_wqnext = tail->l_wqnext;
2039 tail->l_wqnext = bp;
2042 tail = bp;
2045 /* is buffer at head of wqueue and for write ? */
2046 if ((bp != tail->l_wqnext) || !(flag & lbmWRITE)) {
2047 LCACHE_UNLOCK(flags); /* unlock+enable */
2048 return;
2051 LCACHE_UNLOCK(flags); /* unlock+enable */
2053 if (cant_block)
2054 lbmRedrive(bp);
2055 else if (flag & lbmSYNC)
2056 lbmStartIO(bp);
2057 else {
2058 LOGGC_UNLOCK(log);
2059 lbmStartIO(bp);
2060 LOGGC_LOCK(log);
2066 * lbmDirectWrite()
2068 * initiate pageout bypassing write queue for sidestream
2069 * (e.g., log superblock) write;
2071 static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag)
2073 jfs_info("lbmDirectWrite: bp:0x%p flag:0x%x pn:0x%x",
2074 bp, flag, bp->l_pn);
2077 * initialize buffer for device driver
2079 bp->l_flag = flag | lbmDIRECT;
2081 /* map the logical block address to physical block address */
2082 bp->l_blkno =
2083 log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
2086 * initiate pageout of the page
2088 lbmStartIO(bp);
2093 * NAME: lbmStartIO()
2095 * FUNCTION: Interface to DD strategy routine
2097 * RETURN: none
2099 * serialization: LCACHE_LOCK() is NOT held during log i/o;
2101 static void lbmStartIO(struct lbuf * bp)
2103 struct bio *bio;
2104 struct jfs_log *log = bp->l_log;
2106 jfs_info("lbmStartIO\n");
2108 bio = bio_alloc(GFP_NOFS, 1);
2109 bio->bi_sector = bp->l_blkno << (log->l2bsize - 9);
2110 bio->bi_bdev = log->bdev;
2111 bio->bi_io_vec[0].bv_page = virt_to_page(bp->l_ldata);
2112 bio->bi_io_vec[0].bv_len = LOGPSIZE;
2113 bio->bi_io_vec[0].bv_offset = 0;
2115 bio->bi_vcnt = 1;
2116 bio->bi_idx = 0;
2117 bio->bi_size = LOGPSIZE;
2119 bio->bi_end_io = lbmIODone;
2120 bio->bi_private = bp;
2122 /* check if journaling to disk has been disabled */
2123 if (!log->no_integrity) {
2124 submit_bio(WRITE_SYNC, bio);
2125 INCREMENT(lmStat.submitted);
2127 else {
2128 bio->bi_size = 0;
2129 lbmIODone(bio, 0, 0); /* 2nd argument appears to not be used => 0
2130 * 3rd argument appears to not be used => 0
2137 * lbmIOWait()
2139 static int lbmIOWait(struct lbuf * bp, int flag)
2141 unsigned long flags;
2142 int rc = 0;
2144 jfs_info("lbmIOWait1: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
2146 LCACHE_LOCK(flags); /* disable+lock */
2148 LCACHE_SLEEP_COND(bp->l_ioevent, (bp->l_flag & lbmDONE), flags);
2150 rc = (bp->l_flag & lbmERROR) ? -EIO : 0;
2152 if (flag & lbmFREE)
2153 lbmfree(bp);
2155 LCACHE_UNLOCK(flags); /* unlock+enable */
2157 jfs_info("lbmIOWait2: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
2158 return rc;
2162 * lbmIODone()
2164 * executed at INTIODONE level
2166 static int lbmIODone(struct bio *bio, unsigned int bytes_done, int error)
2168 struct lbuf *bp = bio->bi_private;
2169 struct lbuf *nextbp, *tail;
2170 struct jfs_log *log;
2171 unsigned long flags;
2173 if (bio->bi_size)
2174 return 1;
2177 * get back jfs buffer bound to the i/o buffer
2179 jfs_info("lbmIODone: bp:0x%p flag:0x%x", bp, bp->l_flag);
2181 LCACHE_LOCK(flags); /* disable+lock */
2183 bp->l_flag |= lbmDONE;
2185 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2186 bp->l_flag |= lbmERROR;
2188 jfs_err("lbmIODone: I/O error in JFS log");
2191 bio_put(bio);
2194 * pagein completion
2196 if (bp->l_flag & lbmREAD) {
2197 bp->l_flag &= ~lbmREAD;
2199 LCACHE_UNLOCK(flags); /* unlock+enable */
2201 /* wakeup I/O initiator */
2202 LCACHE_WAKEUP(&bp->l_ioevent);
2204 return 0;
2208 * pageout completion
2210 * the bp at the head of write queue has completed pageout.
2212 * if single-commit/full-page pageout, remove the current buffer
2213 * from head of pageout queue, and redrive pageout with
2214 * the new buffer at head of pageout queue;
2215 * otherwise, the partial-page pageout buffer stays at
2216 * the head of pageout queue to be redriven for pageout
2217 * by lmGroupCommit() until full-page pageout is completed.
2219 bp->l_flag &= ~lbmWRITE;
2220 INCREMENT(lmStat.pagedone);
2222 /* update committed lsn */
2223 log = bp->l_log;
2224 log->clsn = (bp->l_pn << L2LOGPSIZE) + bp->l_ceor;
2226 if (bp->l_flag & lbmDIRECT) {
2227 LCACHE_WAKEUP(&bp->l_ioevent);
2228 LCACHE_UNLOCK(flags);
2229 return 0;
2232 tail = log->wqueue;
2234 /* single element queue */
2235 if (bp == tail) {
2236 /* remove head buffer of full-page pageout
2237 * from log device write queue
2239 if (bp->l_flag & lbmRELEASE) {
2240 log->wqueue = NULL;
2241 bp->l_wqnext = NULL;
2244 /* multi element queue */
2245 else {
2246 /* remove head buffer of full-page pageout
2247 * from log device write queue
2249 if (bp->l_flag & lbmRELEASE) {
2250 nextbp = tail->l_wqnext = bp->l_wqnext;
2251 bp->l_wqnext = NULL;
2254 * redrive pageout of next page at head of write queue:
2255 * redrive next page without any bound tblk
2256 * (i.e., page w/o any COMMIT records), or
2257 * first page of new group commit which has been
2258 * queued after current page (subsequent pageout
2259 * is performed synchronously, except page without
2260 * any COMMITs) by lmGroupCommit() as indicated
2261 * by lbmWRITE flag;
2263 if (nextbp->l_flag & lbmWRITE) {
2265 * We can't do the I/O at interrupt time.
2266 * The jfsIO thread can do it
2268 lbmRedrive(nextbp);
2274 * synchronous pageout:
2276 * buffer has not necessarily been removed from write queue
2277 * (e.g., synchronous write of partial-page with COMMIT):
2278 * leave buffer for i/o initiator to dispose
2280 if (bp->l_flag & lbmSYNC) {
2281 LCACHE_UNLOCK(flags); /* unlock+enable */
2283 /* wakeup I/O initiator */
2284 LCACHE_WAKEUP(&bp->l_ioevent);
2288 * Group Commit pageout:
2290 else if (bp->l_flag & lbmGC) {
2291 LCACHE_UNLOCK(flags);
2292 lmPostGC(bp);
2296 * asynchronous pageout:
2298 * buffer must have been removed from write queue:
2299 * insert buffer at head of freelist where it can be recycled
2301 else {
2302 assert(bp->l_flag & lbmRELEASE);
2303 assert(bp->l_flag & lbmFREE);
2304 lbmfree(bp);
2306 LCACHE_UNLOCK(flags); /* unlock+enable */
2309 return 0;
2312 int jfsIOWait(void *arg)
2314 struct lbuf *bp;
2316 daemonize("jfsIO");
2318 complete(&jfsIOwait);
2320 do {
2321 DECLARE_WAITQUEUE(wq, current);
2323 spin_lock_irq(&log_redrive_lock);
2324 while ((bp = log_redrive_list) != 0) {
2325 log_redrive_list = bp->l_redrive_next;
2326 bp->l_redrive_next = NULL;
2327 spin_unlock_irq(&log_redrive_lock);
2328 lbmStartIO(bp);
2329 spin_lock_irq(&log_redrive_lock);
2331 if (current->flags & PF_FREEZE) {
2332 spin_unlock_irq(&log_redrive_lock);
2333 refrigerator(PF_FREEZE);
2334 } else {
2335 add_wait_queue(&jfs_IO_thread_wait, &wq);
2336 set_current_state(TASK_INTERRUPTIBLE);
2337 spin_unlock_irq(&log_redrive_lock);
2338 schedule();
2339 current->state = TASK_RUNNING;
2340 remove_wait_queue(&jfs_IO_thread_wait, &wq);
2342 } while (!jfs_stop_threads);
2344 jfs_info("jfsIOWait being killed!");
2345 complete_and_exit(&jfsIOwait, 0);
2349 * NAME: lmLogFormat()/jfs_logform()
2351 * FUNCTION: format file system log
2353 * PARAMETERS:
2354 * log - volume log
2355 * logAddress - start address of log space in FS block
2356 * logSize - length of log space in FS block;
2358 * RETURN: 0 - success
2359 * -EIO - i/o error
2361 * XXX: We're synchronously writing one page at a time. This needs to
2362 * be improved by writing multiple pages at once.
2364 int lmLogFormat(struct jfs_log *log, s64 logAddress, int logSize)
2366 int rc = -EIO;
2367 struct jfs_sb_info *sbi;
2368 struct logsuper *logsuper;
2369 struct logpage *lp;
2370 int lspn; /* log sequence page number */
2371 struct lrd *lrd_ptr;
2372 int npages = 0;
2373 struct lbuf *bp;
2375 jfs_info("lmLogFormat: logAddress:%Ld logSize:%d",
2376 (long long)logAddress, logSize);
2378 sbi = list_entry(log->sb_list.next, struct jfs_sb_info, log_list);
2380 /* allocate a log buffer */
2381 bp = lbmAllocate(log, 1);
2383 npages = logSize >> sbi->l2nbperpage;
2386 * log space:
2388 * page 0 - reserved;
2389 * page 1 - log superblock;
2390 * page 2 - log data page: A SYNC log record is written
2391 * into this page at logform time;
2392 * pages 3-N - log data page: set to empty log data pages;
2395 * init log superblock: log page 1
2397 logsuper = (struct logsuper *) bp->l_ldata;
2399 logsuper->magic = cpu_to_le32(LOGMAGIC);
2400 logsuper->version = cpu_to_le32(LOGVERSION);
2401 logsuper->state = cpu_to_le32(LOGREDONE);
2402 logsuper->flag = cpu_to_le32(sbi->mntflag); /* ? */
2403 logsuper->size = cpu_to_le32(npages);
2404 logsuper->bsize = cpu_to_le32(sbi->bsize);
2405 logsuper->l2bsize = cpu_to_le32(sbi->l2bsize);
2406 logsuper->end = cpu_to_le32(2 * LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE);
2408 bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
2409 bp->l_blkno = logAddress + sbi->nbperpage;
2410 lbmStartIO(bp);
2411 if ((rc = lbmIOWait(bp, 0)))
2412 goto exit;
2415 * init pages 2 to npages-1 as log data pages:
2417 * log page sequence number (lpsn) initialization:
2419 * pn: 0 1 2 3 n-1
2420 * +-----+-----+=====+=====+===.....===+=====+
2421 * lspn: N-1 0 1 N-2
2422 * <--- N page circular file ---->
2424 * the N (= npages-2) data pages of the log is maintained as
2425 * a circular file for the log records;
2426 * lpsn grows by 1 monotonically as each log page is written
2427 * to the circular file of the log;
2428 * and setLogpage() will not reset the page number even if
2429 * the eor is equal to LOGPHDRSIZE. In order for binary search
2430 * still work in find log end process, we have to simulate the
2431 * log wrap situation at the log format time.
2432 * The 1st log page written will have the highest lpsn. Then
2433 * the succeeding log pages will have ascending order of
2434 * the lspn starting from 0, ... (N-2)
2436 lp = (struct logpage *) bp->l_ldata;
2438 * initialize 1st log page to be written: lpsn = N - 1,
2439 * write a SYNCPT log record is written to this page
2441 lp->h.page = lp->t.page = cpu_to_le32(npages - 3);
2442 lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE + LOGRDSIZE);
2444 lrd_ptr = (struct lrd *) &lp->data;
2445 lrd_ptr->logtid = 0;
2446 lrd_ptr->backchain = 0;
2447 lrd_ptr->type = cpu_to_le16(LOG_SYNCPT);
2448 lrd_ptr->length = 0;
2449 lrd_ptr->log.syncpt.sync = 0;
2451 bp->l_blkno += sbi->nbperpage;
2452 bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
2453 lbmStartIO(bp);
2454 if ((rc = lbmIOWait(bp, 0)))
2455 goto exit;
2458 * initialize succeeding log pages: lpsn = 0, 1, ..., (N-2)
2460 for (lspn = 0; lspn < npages - 3; lspn++) {
2461 lp->h.page = lp->t.page = cpu_to_le32(lspn);
2462 lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
2464 bp->l_blkno += sbi->nbperpage;
2465 bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
2466 lbmStartIO(bp);
2467 if ((rc = lbmIOWait(bp, 0)))
2468 goto exit;
2471 rc = 0;
2472 exit:
2474 * finalize log
2476 /* release the buffer */
2477 lbmFree(bp);
2479 return rc;
2482 #ifdef CONFIG_JFS_STATISTICS
2483 int jfs_lmstats_read(char *buffer, char **start, off_t offset, int length,
2484 int *eof, void *data)
2486 int len = 0;
2487 off_t begin;
2489 len += sprintf(buffer,
2490 "JFS Logmgr stats\n"
2491 "================\n"
2492 "commits = %d\n"
2493 "writes submitted = %d\n"
2494 "writes completed = %d\n"
2495 "full pages submitted = %d\n"
2496 "partial pages submitted = %d\n",
2497 lmStat.commit,
2498 lmStat.submitted,
2499 lmStat.pagedone,
2500 lmStat.full_page,
2501 lmStat.partial_page);
2503 begin = offset;
2504 *start = buffer + begin;
2505 len -= begin;
2507 if (len > length)
2508 len = length;
2509 else
2510 *eof = 1;
2512 if (len < 0)
2513 len = 0;
2515 return len;
2517 #endif /* CONFIG_JFS_STATISTICS */