jfs_logmgr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:54k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *   Copyright (c) International Business Machines Corp., 2000-2002
  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.  * jfs_logmgr.c: log manager
  21.  *
  22.  * for related information, see transaction manager (jfs_txnmgr.c), and
  23.  * recovery manager (jfs_logredo.c).
  24.  *
  25.  * note: for detail, RTFS.
  26.  *
  27.  * log buffer manager:
  28.  * special purpose buffer manager supporting log i/o requirements.
  29.  * per log serial pageout of logpage
  30.  * queuing i/o requests and redrive i/o at iodone
  31.  * maintain current logpage buffer
  32.  * no caching since append only
  33.  * appropriate jfs buffer cache buffers as needed
  34.  *
  35.  * group commit:
  36.  * transactions which wrote COMMIT records in the same in-memory
  37.  * log page during the pageout of previous/current log page(s) are
  38.  * committed together by the pageout of the page.
  39.  *
  40.  * TBD lazy commit:
  41.  * transactions are committed asynchronously when the log page
  42.  * containing it COMMIT is paged out when it becomes full;
  43.  *
  44.  * serialization:
  45.  * . a per log lock serialize log write.
  46.  * . a per log lock serialize group commit.
  47.  * . a per log lock serialize log open/close;
  48.  *
  49.  * TBD log integrity:
  50.  * careful-write (ping-pong) of last logpage to recover from crash
  51.  * in overwrite.
  52.  * detection of split (out-of-order) write of physical sectors
  53.  * of last logpage via timestamp at end of each sector
  54.  * with its mirror data array at trailer).
  55.  *
  56.  * alternatives:
  57.  * lsn - 64-bit monotonically increasing integer vs
  58.  * 32-bit lspn and page eor.
  59.  */
  60. #include <linux/fs.h>
  61. #include <linux/locks.h>
  62. #include <linux/blkdev.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/smp_lock.h>
  65. #include <linux/completion.h>
  66. #include "jfs_incore.h"
  67. #include "jfs_filsys.h"
  68. #include "jfs_metapage.h"
  69. #include "jfs_txnmgr.h"
  70. #include "jfs_debug.h"
  71. /*
  72.  * lbuf's ready to be redriven.  Protected by log_redrive_lock (jfsIO thread)
  73.  */
  74. static struct lbuf *log_redrive_list;
  75. static spinlock_t log_redrive_lock = SPIN_LOCK_UNLOCKED;
  76. DECLARE_WAIT_QUEUE_HEAD(jfs_IO_thread_wait);
  77. /*
  78.  * log read/write serialization (per log)
  79.  */
  80. #define LOG_LOCK_INIT(log) init_MUTEX(&(log)->loglock)
  81. #define LOG_LOCK(log) down(&((log)->loglock))
  82. #define LOG_UNLOCK(log) up(&((log)->loglock))
  83. /*
  84.  * log group commit serialization (per log)
  85.  */
  86. #define LOGGC_LOCK_INIT(log) spin_lock_init(&(log)->gclock)
  87. #define LOGGC_LOCK(log) spin_lock_irq(&(log)->gclock)
  88. #define LOGGC_UNLOCK(log) spin_unlock_irq(&(log)->gclock)
  89. #define LOGGC_WAKEUP(tblk) wake_up(&(tblk)->gcwait)
  90. /*
  91.  * log sync serialization (per log)
  92.  */
  93. #define LOGSYNC_DELTA(logsize) min((logsize)/8, 128*LOGPSIZE)
  94. #define LOGSYNC_BARRIER(logsize) ((logsize)/4)
  95. /*
  96. #define LOGSYNC_DELTA(logsize) min((logsize)/4, 256*LOGPSIZE)
  97. #define LOGSYNC_BARRIER(logsize) ((logsize)/2)
  98. */
  99. /*
  100.  * log buffer cache synchronization
  101.  */
  102. static spinlock_t jfsLCacheLock = SPIN_LOCK_UNLOCKED;
  103. #define LCACHE_LOCK(flags) spin_lock_irqsave(&jfsLCacheLock, flags)
  104. #define LCACHE_UNLOCK(flags) spin_unlock_irqrestore(&jfsLCacheLock, flags)
  105. /*
  106.  * See __SLEEP_COND in jfs_locks.h
  107.  */
  108. #define LCACHE_SLEEP_COND(wq, cond, flags)
  109. do {
  110. if (cond)
  111. break;
  112. __SLEEP_COND(wq, cond, LCACHE_LOCK(flags), LCACHE_UNLOCK(flags)); 
  113. } while (0)
  114. #define LCACHE_WAKEUP(event) wake_up(event)
  115. /*
  116.  * lbuf buffer cache (lCache) control
  117.  */
  118. /* log buffer manager pageout control (cumulative, inclusive) */
  119. #define lbmREAD 0x0001
  120. #define lbmWRITE 0x0002 /* enqueue at tail of write queue;
  121.  * init pageout if at head of queue;
  122.  */
  123. #define lbmRELEASE 0x0004 /* remove from write queue
  124.  * at completion of pageout;
  125.  * do not free/recycle it yet:
  126.  * caller will free it;
  127.  */
  128. #define lbmSYNC 0x0008 /* do not return to freelist
  129.  * when removed from write queue;
  130.  */
  131. #define lbmFREE 0x0010 /* return to freelist
  132.  * at completion of pageout;
  133.  * the buffer may be recycled;
  134.  */
  135. #define lbmDONE 0x0020
  136. #define lbmERROR 0x0040
  137. #define lbmGC 0x0080 /* lbmIODone to perform post-GC processing
  138.  * of log page
  139.  */
  140. #define lbmDIRECT 0x0100
  141. /*
  142.  * external references
  143.  */
  144. extern void txLazyUnlock(struct tblock * tblk);
  145. extern int jfs_stop_threads;
  146. extern struct completion jfsIOwait;
  147. /*
  148.  * forward references
  149.  */
  150. static int lmWriteRecord(struct jfs_log * log, struct tblock * tblk,
  151.  struct lrd * lrd, struct tlock * tlck);
  152. static int lmNextPage(struct jfs_log * log);
  153. static int lmLogFileSystem(struct jfs_log * log, char *uuid, int activate);
  154. static int lbmLogInit(struct jfs_log * log);
  155. static void lbmLogShutdown(struct jfs_log * log);
  156. static struct lbuf *lbmAllocate(struct jfs_log * log, int);
  157. static void lbmFree(struct lbuf * bp);
  158. static void lbmfree(struct lbuf * bp);
  159. static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp);
  160. static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
  161.      int cant_block);
  162. static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag);
  163. static int lbmIOWait(struct lbuf * bp, int flag);
  164. static void lbmIODone(struct buffer_head *bh, int);
  165. void lbmStartIO(struct lbuf * bp);
  166. void lmGCwrite(struct jfs_log * log, int cant_block);
  167. /*
  168.  * statistics
  169.  */
  170. #ifdef CONFIG_JFS_STATISTICS
  171. struct lmStat {
  172. uint commit; /* # of commit */
  173. uint pagedone; /* # of page written */
  174. uint submitted; /* # of pages submitted */
  175. } lmStat;
  176. #endif
  177. /*
  178.  * NAME: lmLog()
  179.  *
  180.  * FUNCTION: write a log record;
  181.  *
  182.  * PARAMETER:
  183.  *
  184.  * RETURN: lsn - offset to the next log record to write (end-of-log);
  185.  * -1  - error;
  186.  *
  187.  * note: todo: log error handler
  188.  */
  189. int lmLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  190.   struct tlock * tlck)
  191. {
  192. int lsn;
  193. int diffp, difft;
  194. struct metapage *mp = NULL;
  195. jFYI(1, ("lmLog: log:0x%p tblk:0x%p, lrd:0x%p tlck:0x%pn",
  196.  log, tblk, lrd, tlck));
  197. LOG_LOCK(log);
  198. /* log by (out-of-transaction) JFS ? */
  199. if (tblk == NULL)
  200. goto writeRecord;
  201. /* log from page ? */
  202. if (tlck == NULL ||
  203.     tlck->type & tlckBTROOT || (mp = tlck->mp) == NULL)
  204. goto writeRecord;
  205. /*
  206.  *      initialize/update page/transaction recovery lsn
  207.  */
  208. lsn = log->lsn;
  209. LOGSYNC_LOCK(log);
  210. /*
  211.  * initialize page lsn if first log write of the page
  212.  */
  213. if (mp->lsn == 0) {
  214. mp->log = log;
  215. mp->lsn = lsn;
  216. log->count++;
  217. /* insert page at tail of logsynclist */
  218. list_add_tail(&mp->synclist, &log->synclist);
  219. }
  220. /*
  221.  *      initialize/update lsn of tblock of the page
  222.  *
  223.  * transaction inherits oldest lsn of pages associated
  224.  * with allocation/deallocation of resources (their
  225.  * log records are used to reconstruct allocation map
  226.  * at recovery time: inode for inode allocation map,
  227.  * B+-tree index of extent descriptors for block
  228.  * allocation map);
  229.  * allocation map pages inherit transaction lsn at
  230.  * commit time to allow forwarding log syncpt past log
  231.  * records associated with allocation/deallocation of
  232.  * resources only after persistent map of these map pages
  233.  * have been updated and propagated to home.
  234.  */
  235. /*
  236.  * initialize transaction lsn:
  237.  */
  238. if (tblk->lsn == 0) {
  239. /* inherit lsn of its first page logged */
  240. tblk->lsn = mp->lsn;
  241. log->count++;
  242. /* insert tblock after the page on logsynclist */
  243. list_add(&tblk->synclist, &mp->synclist);
  244. }
  245. /*
  246.  * update transaction lsn:
  247.  */
  248. else {
  249. /* inherit oldest/smallest lsn of page */
  250. logdiff(diffp, mp->lsn, log);
  251. logdiff(difft, tblk->lsn, log);
  252. if (diffp < difft) {
  253. /* update tblock lsn with page lsn */
  254. tblk->lsn = mp->lsn;
  255. /* move tblock after page on logsynclist */
  256. list_del(&tblk->synclist);
  257. list_add(&tblk->synclist, &mp->synclist);
  258. }
  259. }
  260. LOGSYNC_UNLOCK(log);
  261. /*
  262.  *      write the log record
  263.  */
  264.       writeRecord:
  265. lsn = lmWriteRecord(log, tblk, lrd, tlck);
  266. /*
  267.  * forward log syncpt if log reached next syncpt trigger
  268.  */
  269. logdiff(diffp, lsn, log);
  270. if (diffp >= log->nextsync)
  271. lsn = lmLogSync(log, 0);
  272. /* update end-of-log lsn */
  273. log->lsn = lsn;
  274. LOG_UNLOCK(log);
  275. /* return end-of-log address */
  276. return lsn;
  277. }
  278. /*
  279.  * NAME: lmWriteRecord()
  280.  *
  281.  * FUNCTION: move the log record to current log page
  282.  *
  283.  * PARAMETER: cd - commit descriptor
  284.  *
  285.  * RETURN: end-of-log address
  286.  *
  287.  * serialization: LOG_LOCK() held on entry/exit
  288.  */
  289. static int
  290. lmWriteRecord(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  291.       struct tlock * tlck)
  292. {
  293. int lsn = 0; /* end-of-log address */
  294. struct lbuf *bp; /* dst log page buffer */
  295. struct logpage *lp; /* dst log page */
  296. caddr_t dst; /* destination address in log page */
  297. int dstoffset; /* end-of-log offset in log page */
  298. int freespace; /* free space in log page */
  299. caddr_t p; /* src meta-data page */
  300. caddr_t src;
  301. int srclen;
  302. int nbytes; /* number of bytes to move */
  303. int i;
  304. int len;
  305. struct linelock *linelock;
  306. struct lv *lv;
  307. struct lvd *lvd;
  308. int l2linesize;
  309. len = 0;
  310. /* retrieve destination log page to write */
  311. bp = (struct lbuf *) log->bp;
  312. lp = (struct logpage *) bp->l_ldata;
  313. dstoffset = log->eor;
  314. /* any log data to write ? */
  315. if (tlck == NULL)
  316. goto moveLrd;
  317. /*
  318.  *      move log record data
  319.  */
  320. /* retrieve source meta-data page to log */
  321. if (tlck->flag & tlckPAGELOCK) {
  322. p = (caddr_t) (tlck->mp->data);
  323. linelock = (struct linelock *) & tlck->lock;
  324. }
  325. /* retrieve source in-memory inode to log */
  326. else if (tlck->flag & tlckINODELOCK) {
  327. if (tlck->type & tlckDTREE)
  328. p = (caddr_t) &JFS_IP(tlck->ip)->i_dtroot;
  329. else
  330. p = (caddr_t) &JFS_IP(tlck->ip)->i_xtroot;
  331. linelock = (struct linelock *) & tlck->lock;
  332. }
  333. #ifdef _JFS_WIP
  334. else if (tlck->flag & tlckINLINELOCK) {
  335. inlinelock = (struct inlinelock *) & tlck;
  336. p = (caddr_t) & inlinelock->pxd;
  337. linelock = (struct linelock *) & tlck;
  338. }
  339. #endif /* _JFS_WIP */
  340. else {
  341. jERROR(2, ("lmWriteRecord: UFO tlck:0x%pn", tlck));
  342. return 0; /* Probably should trap */
  343. }
  344. l2linesize = linelock->l2linesize;
  345.       moveData:
  346. ASSERT(linelock->index <= linelock->maxcnt);
  347. lv = linelock->lv;
  348. for (i = 0; i < linelock->index; i++, lv++) {
  349. if (lv->length == 0)
  350. continue;
  351. /* is page full ? */
  352. if (dstoffset >= LOGPSIZE - LOGPTLRSIZE) {
  353. /* page become full: move on to next page */
  354. lmNextPage(log);
  355. bp = log->bp;
  356. lp = (struct logpage *) bp->l_ldata;
  357. dstoffset = LOGPHDRSIZE;
  358. }
  359. /*
  360.  * move log vector data
  361.  */
  362. src = (u8 *) p + (lv->offset << l2linesize);
  363. srclen = lv->length << l2linesize;
  364. len += srclen;
  365. while (srclen > 0) {
  366. freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
  367. nbytes = min(freespace, srclen);
  368. dst = (caddr_t) lp + dstoffset;
  369. memcpy(dst, src, nbytes);
  370. dstoffset += nbytes;
  371. /* is page not full ? */
  372. if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
  373. break;
  374. /* page become full: move on to next page */
  375. lmNextPage(log);
  376. bp = (struct lbuf *) log->bp;
  377. lp = (struct logpage *) bp->l_ldata;
  378. dstoffset = LOGPHDRSIZE;
  379. srclen -= nbytes;
  380. src += nbytes;
  381. }
  382. /*
  383.  * move log vector descriptor
  384.  */
  385. len += 4;
  386. lvd = (struct lvd *) ((caddr_t) lp + dstoffset);
  387. lvd->offset = cpu_to_le16(lv->offset);
  388. lvd->length = cpu_to_le16(lv->length);
  389. dstoffset += 4;
  390. jFYI(1,
  391.      ("lmWriteRecord: lv offset:%d length:%dn",
  392.       lv->offset, lv->length));
  393. }
  394. if ((i = linelock->next)) {
  395. linelock = (struct linelock *) lid_to_tlock(i);
  396. goto moveData;
  397. }
  398. /*
  399.  *      move log record descriptor
  400.  */
  401.       moveLrd:
  402. lrd->length = cpu_to_le16(len);
  403. src = (caddr_t) lrd;
  404. srclen = LOGRDSIZE;
  405. while (srclen > 0) {
  406. freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
  407. nbytes = min(freespace, srclen);
  408. dst = (caddr_t) lp + dstoffset;
  409. memcpy(dst, src, nbytes);
  410. dstoffset += nbytes;
  411. srclen -= nbytes;
  412. /* are there more to move than freespace of page ? */
  413. if (srclen)
  414. goto pageFull;
  415. /*
  416.  * end of log record descriptor
  417.  */
  418. /* update last log record eor */
  419. log->eor = dstoffset;
  420. bp->l_eor = dstoffset;
  421. lsn = (log->page << L2LOGPSIZE) + dstoffset;
  422. if (lrd->type & cpu_to_le16(LOG_COMMIT)) {
  423. tblk->clsn = lsn;
  424. jFYI(1,
  425.      ("wr: tclsn:0x%x, beor:0x%xn", tblk->clsn,
  426.       bp->l_eor));
  427. INCREMENT(lmStat.commit); /* # of commit */
  428. /*
  429.  * enqueue tblock for group commit:
  430.  *
  431.  * enqueue tblock of non-trivial/synchronous COMMIT
  432.  * at tail of group commit queue
  433.  * (trivial/asynchronous COMMITs are ignored by
  434.  * group commit.)
  435.  */
  436. LOGGC_LOCK(log);
  437. /* init tblock gc state */
  438. tblk->flag = tblkGC_QUEUE;
  439. tblk->bp = log->bp;
  440. tblk->pn = log->page;
  441. tblk->eor = log->eor;
  442. init_waitqueue_head(&tblk->gcwait);
  443. /* enqueue transaction to commit queue */
  444. tblk->cqnext = NULL;
  445. if (log->cqueue.head) {
  446. log->cqueue.tail->cqnext = tblk;
  447. log->cqueue.tail = tblk;
  448. } else
  449. log->cqueue.head = log->cqueue.tail = tblk;
  450. LOGGC_UNLOCK(log);
  451. }
  452. jFYI(1,
  453.      ("lmWriteRecord: lrd:0x%04x bp:0x%p pn:%d eor:0x%xn",
  454.       le16_to_cpu(lrd->type), log->bp, log->page,
  455.       dstoffset));
  456. /* page not full ? */
  457. if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
  458. return lsn;
  459.       pageFull:
  460. /* page become full: move on to next page */
  461. lmNextPage(log);
  462. bp = (struct lbuf *) log->bp;
  463. lp = (struct logpage *) bp->l_ldata;
  464. dstoffset = LOGPHDRSIZE;
  465. src += nbytes;
  466. }
  467. return lsn;
  468. }
  469. /*
  470.  * NAME: lmNextPage()
  471.  *
  472.  * FUNCTION: write current page and allocate next page.
  473.  *
  474.  * PARAMETER: log
  475.  *
  476.  * RETURN: 0
  477.  *
  478.  * serialization: LOG_LOCK() held on entry/exit
  479.  */
  480. static int lmNextPage(struct jfs_log * log)
  481. {
  482. struct logpage *lp;
  483. int lspn; /* log sequence page number */
  484. int pn; /* current page number */
  485. struct lbuf *bp;
  486. struct lbuf *nextbp;
  487. struct tblock *tblk;
  488. jFYI(1, ("lmNextPagen"));
  489. /* get current log page number and log sequence page number */
  490. pn = log->page;
  491. bp = log->bp;
  492. lp = (struct logpage *) bp->l_ldata;
  493. lspn = le32_to_cpu(lp->h.page);
  494. LOGGC_LOCK(log);
  495. /*
  496.  *      write or queue the full page at the tail of write queue
  497.  */
  498. /* get the tail tblk on commit queue */
  499. tblk = log->cqueue.tail;
  500. /* every tblk who has COMMIT record on the current page,
  501.  * and has not been committed, must be on commit queue
  502.  * since tblk is queued at commit queueu at the time
  503.  * of writing its COMMIT record on the page before
  504.  * page becomes full (even though the tblk thread
  505.  * who wrote COMMIT record may have been suspended
  506.  * currently);
  507.  */
  508. /* is page bound with outstanding tail tblk ? */
  509. if (tblk && tblk->pn == pn) {
  510. /* mark tblk for end-of-page */
  511. tblk->flag |= tblkGC_EOP;
  512. /* if page is not already on write queue,
  513.  * just enqueue (no lbmWRITE to prevent redrive)
  514.  * buffer to wqueue to ensure correct serial order
  515.  * of the pages since log pages will be added
  516.  * continuously (tblk bound with the page hasn't
  517.  * got around to init write of the page, either
  518.  * preempted or the page got filled by its COMMIT
  519.  * record);
  520.  * pages with COMMIT are paged out explicitly by
  521.  * tblk in lmGroupCommit();
  522.  */
  523. if (bp->l_wqnext == NULL) {
  524. /* bp->l_ceor = bp->l_eor; */
  525. /* lp->h.eor = lp->t.eor = bp->l_ceor; */
  526. lbmWrite(log, bp, 0, 0);
  527. }
  528. }
  529. /* page is not bound with outstanding tblk:
  530.  * init write or mark it to be redriven (lbmWRITE)
  531.  */
  532. else {
  533. /* finalize the page */
  534. bp->l_ceor = bp->l_eor;
  535. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
  536. lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE, 0);
  537. }
  538. LOGGC_UNLOCK(log);
  539. /*
  540.  *      allocate/initialize next page
  541.  */
  542. /* if log wraps, the first data page of log is 2
  543.  * (0 never used, 1 is superblock).
  544.  */
  545. log->page = (pn == log->size - 1) ? 2 : pn + 1;
  546. log->eor = LOGPHDRSIZE; /* ? valid page empty/full at logRedo() */
  547. /* allocate/initialize next log page buffer */
  548. nextbp = lbmAllocate(log, log->page);
  549. nextbp->l_eor = log->eor;
  550. log->bp = nextbp;
  551. /* initialize next log page */
  552. lp = (struct logpage *) nextbp->l_ldata;
  553. lp->h.page = lp->t.page = cpu_to_le32(lspn + 1);
  554. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
  555. jFYI(1, ("lmNextPage donen"));
  556. return 0;
  557. }
  558. /*
  559.  * NAME: lmGroupCommit()
  560.  *
  561.  * FUNCTION: group commit
  562.  * initiate pageout of the pages with COMMIT in the order of
  563.  * page number - redrive pageout of the page at the head of
  564.  * pageout queue until full page has been written.
  565.  *
  566.  * RETURN:
  567.  *
  568.  * NOTE:
  569.  * LOGGC_LOCK serializes log group commit queue, and
  570.  * transaction blocks on the commit queue.
  571.  * N.B. LOG_LOCK is NOT held during lmGroupCommit().
  572.  */
  573. int lmGroupCommit(struct jfs_log * log, struct tblock * tblk)
  574. {
  575. int rc = 0;
  576. LOGGC_LOCK(log);
  577. /* group committed already ? */
  578. if (tblk->flag & tblkGC_COMMITTED) {
  579. if (tblk->flag & tblkGC_ERROR)
  580. rc = EIO;
  581. LOGGC_UNLOCK(log);
  582. return rc;
  583. }
  584. jFYI(1,
  585.      ("lmGroup Commit: tblk = 0x%p, gcrtc = %dn", tblk,
  586.       log->gcrtc));
  587. /*
  588.  * group commit pageout in progress
  589.  */
  590. if ((!(log->cflag & logGC_PAGEOUT)) && log->cqueue.head) {
  591. /*
  592.  * only transaction in the commit queue:
  593.  *
  594.  * start one-transaction group commit as
  595.  * its group leader.
  596.  */
  597. log->cflag |= logGC_PAGEOUT;
  598. lmGCwrite(log, 0);
  599. }
  600. /* lmGCwrite gives up LOGGC_LOCK, check again */
  601. if (tblk->flag & tblkGC_COMMITTED) {
  602. if (tblk->flag & tblkGC_ERROR)
  603. rc = EIO;
  604. LOGGC_UNLOCK(log);
  605. return rc;
  606. }
  607. /* upcount transaction waiting for completion
  608.  */
  609. log->gcrtc++;
  610. if (tblk->xflag & COMMIT_LAZY) {
  611. tblk->flag |= tblkGC_LAZY;
  612. LOGGC_UNLOCK(log);
  613. return 0;
  614. }
  615. tblk->flag |= tblkGC_READY;
  616. __SLEEP_COND(tblk->gcwait, (tblk->flag & tblkGC_COMMITTED),
  617.      LOGGC_LOCK(log), LOGGC_UNLOCK(log));
  618. /* removed from commit queue */
  619. if (tblk->flag & tblkGC_ERROR)
  620. rc = EIO;
  621. LOGGC_UNLOCK(log);
  622. return rc;
  623. }
  624. /*
  625.  * NAME: lmGCwrite()
  626.  *
  627.  * FUNCTION: group commit write
  628.  * initiate write of log page, building a group of all transactions
  629.  * with commit records on that page.
  630.  *
  631.  * RETURN: None
  632.  *
  633.  * NOTE:
  634.  * LOGGC_LOCK must be held by caller.
  635.  * N.B. LOG_LOCK is NOT held during lmGroupCommit().
  636.  */
  637. void lmGCwrite(struct jfs_log * log, int cant_write)
  638. {
  639. struct lbuf *bp;
  640. struct logpage *lp;
  641. int gcpn; /* group commit page number */
  642. struct tblock *tblk;
  643. struct tblock *xtblk;
  644. /*
  645.  * build the commit group of a log page
  646.  *
  647.  * scan commit queue and make a commit group of all
  648.  * transactions with COMMIT records on the same log page.
  649.  */
  650. /* get the head tblk on the commit queue */
  651. tblk = xtblk = log->cqueue.head;
  652. gcpn = tblk->pn;
  653. while (tblk && tblk->pn == gcpn) {
  654. xtblk = tblk;
  655. /* state transition: (QUEUE, READY) -> COMMIT */
  656. tblk->flag |= tblkGC_COMMIT;
  657. tblk = tblk->cqnext;
  658. }
  659. tblk = xtblk; /* last tblk of the page */
  660. /*
  661.  * pageout to commit transactions on the log page.
  662.  */
  663. bp = (struct lbuf *) tblk->bp;
  664. lp = (struct logpage *) bp->l_ldata;
  665. /* is page already full ? */
  666. if (tblk->flag & tblkGC_EOP) {
  667. /* mark page to free at end of group commit of the page */
  668. tblk->flag &= ~tblkGC_EOP;
  669. tblk->flag |= tblkGC_FREE;
  670. bp->l_ceor = bp->l_eor;
  671. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
  672. jEVENT(0,
  673.        ("gc: tclsn:0x%x, bceor:0x%xn", tblk->clsn,
  674. bp->l_ceor));
  675. lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmGC,
  676.  cant_write);
  677. }
  678. /* page is not yet full */
  679. else {
  680. bp->l_ceor = tblk->eor; /* ? bp->l_ceor = bp->l_eor; */
  681. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
  682. jEVENT(0,
  683.        ("gc: tclsn:0x%x, bceor:0x%xn", tblk->clsn,
  684. bp->l_ceor));
  685. lbmWrite(log, bp, lbmWRITE | lbmGC, cant_write);
  686. }
  687. }
  688. /*
  689.  * NAME: lmPostGC()
  690.  *
  691.  * FUNCTION: group commit post-processing
  692.  * Processes transactions after their commit records have been written
  693.  * to disk, redriving log I/O if necessary.
  694.  *
  695.  * RETURN: None
  696.  *
  697.  * NOTE:
  698.  * This routine is called a interrupt time by lbmIODone
  699.  */
  700. void lmPostGC(struct lbuf * bp)
  701. {
  702. unsigned long flags;
  703. struct jfs_log *log = bp->l_log;
  704. struct logpage *lp;
  705. struct tblock *tblk;
  706. //LOGGC_LOCK(log);
  707. spin_lock_irqsave(&log->gclock, flags);
  708. /*
  709.  * current pageout of group commit completed.
  710.  *
  711.  * remove/wakeup transactions from commit queue who were
  712.  * group committed with the current log page
  713.  */
  714. while ((tblk = log->cqueue.head) && (tblk->flag & tblkGC_COMMIT)) {
  715. /* if transaction was marked GC_COMMIT then
  716.  * it has been shipped in the current pageout
  717.  * and made it to disk - it is committed.
  718.  */
  719. if (bp->l_flag & lbmERROR)
  720. tblk->flag |= tblkGC_ERROR;
  721. /* remove it from the commit queue */
  722. log->cqueue.head = tblk->cqnext;
  723. if (log->cqueue.head == NULL)
  724. log->cqueue.tail = NULL;
  725. tblk->flag &= ~tblkGC_QUEUE;
  726. tblk->cqnext = 0;
  727. jEVENT(0,
  728.        ("lmPostGC: tblk = 0x%p, flag = 0x%xn", tblk,
  729. tblk->flag));
  730. if (!(tblk->xflag & COMMIT_FORCE))
  731. /*
  732.  * Hand tblk over to lazy commit thread
  733.  */
  734. txLazyUnlock(tblk);
  735. else {
  736. /* state transition: COMMIT -> COMMITTED */
  737. tblk->flag |= tblkGC_COMMITTED;
  738. if (tblk->flag & tblkGC_READY) {
  739. log->gcrtc--;
  740. LOGGC_WAKEUP(tblk);
  741. }
  742. }
  743. /* was page full before pageout ?
  744.  * (and this is the last tblk bound with the page)
  745.  */
  746. if (tblk->flag & tblkGC_FREE)
  747. lbmFree(bp);
  748. /* did page become full after pageout ?
  749.  * (and this is the last tblk bound with the page)
  750.  */
  751. else if (tblk->flag & tblkGC_EOP) {
  752. /* finalize the page */
  753. lp = (struct logpage *) bp->l_ldata;
  754. bp->l_ceor = bp->l_eor;
  755. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  756. jEVENT(0, ("lmPostGC: calling lbmWriten"));
  757. lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE,
  758.  1);
  759. }
  760. }
  761. /* are there any transactions who have entered lnGroupCommit()
  762.  * (whose COMMITs are after that of the last log page written.
  763.  * They are waiting for new group commit (above at (SLEEP 1)):
  764.  * select the latest ready transaction as new group leader and
  765.  * wake her up to lead her group.
  766.  */
  767. if ((log->gcrtc > 0) && log->cqueue.head)
  768. /*
  769.  * Call lmGCwrite with new group leader
  770.  */
  771. lmGCwrite(log, 1);
  772. /* no transaction are ready yet (transactions are only just
  773.  * queued (GC_QUEUE) and not entered for group commit yet).
  774.  * let the first transaction entering group commit
  775.  * will elect hetself as new group leader.
  776.  */
  777. else
  778. log->cflag &= ~logGC_PAGEOUT;
  779. //LOGGC_UNLOCK(log);
  780. spin_unlock_irqrestore(&log->gclock, flags);
  781. return;
  782. }
  783. /*
  784.  * NAME: lmLogSync()
  785.  *
  786.  * FUNCTION: write log SYNCPT record for specified log
  787.  * if new sync address is available
  788.  * (normally the case if sync() is executed by back-ground
  789.  * process).
  790.  * if not, explicitly run jfs_blogsync() to initiate
  791.  * getting of new sync address.
  792.  * calculate new value of i_nextsync which determines when
  793.  * this code is called again.
  794.  *
  795.  * this is called only from lmLog().
  796.  *
  797.  * PARAMETER: ip - pointer to logs inode.
  798.  *
  799.  * RETURN: 0
  800.  *
  801.  * serialization: LOG_LOCK() held on entry/exit
  802.  */
  803. int lmLogSync(struct jfs_log * log, int nosyncwait)
  804. {
  805. int logsize;
  806. int written; /* written since last syncpt */
  807. int free; /* free space left available */
  808. int delta; /* additional delta to write normally */
  809. int more; /* additional write granted */
  810. struct lrd lrd;
  811. int lsn;
  812. struct logsyncblk *lp;
  813. /*
  814.  *      forward syncpt
  815.  */
  816. /* if last sync is same as last syncpt,
  817.  * invoke sync point forward processing to update sync.
  818.  */
  819. if (log->sync == log->syncpt) {
  820. LOGSYNC_LOCK(log);
  821. /* ToDo: push dirty metapages out to disk */
  822. //              bmLogSync(log);
  823. if (list_empty(&log->synclist))
  824. log->sync = log->lsn;
  825. else {
  826. lp = list_entry(log->synclist.next,
  827. struct logsyncblk, synclist);
  828. log->sync = lp->lsn;
  829. }
  830. LOGSYNC_UNLOCK(log);
  831. }
  832. /* if sync is different from last syncpt,
  833.  * write a SYNCPT record with syncpt = sync.
  834.  * reset syncpt = sync
  835.  */
  836. if (log->sync != log->syncpt) {
  837. struct super_block *sb = log->sb;
  838. struct jfs_sb_info *sbi = JFS_SBI(sb);
  839. /*
  840.  * We need to make sure all of the "written" metapages
  841.  * actually make it to disk
  842.  */
  843. fsync_inode_data_buffers(sbi->ipbmap);
  844. fsync_inode_data_buffers(sbi->ipimap);
  845. fsync_inode_data_buffers(sb->s_bdev->bd_inode);
  846. lrd.logtid = 0;
  847. lrd.backchain = 0;
  848. lrd.type = cpu_to_le16(LOG_SYNCPT);
  849. lrd.length = 0;
  850. lrd.log.syncpt.sync = cpu_to_le32(log->sync);
  851. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  852. log->syncpt = log->sync;
  853. } else
  854. lsn = log->lsn;
  855. /*
  856.  *      setup next syncpt trigger (SWAG)
  857.  */
  858. logsize = log->logsize;
  859. logdiff(written, lsn, log);
  860. free = logsize - written;
  861. delta = LOGSYNC_DELTA(logsize);
  862. more = min(free / 2, delta);
  863. if (more < 2 * LOGPSIZE) {
  864. jEVENT(1,
  865.        ("n ... Log Wrap ... Log Wrap ... Log Wrap ...nn"));
  866. /*
  867.  *      log wrapping
  868.  *
  869.  * option 1 - panic ? No.!
  870.  * option 2 - shutdown file systems
  871.  *            associated with log ?
  872.  * option 3 - extend log ?
  873.  */
  874. /*
  875.  * option 4 - second chance
  876.  *
  877.  * mark log wrapped, and continue.
  878.  * when all active transactions are completed,
  879.  * mark log vaild for recovery.
  880.  * if crashed during invalid state, log state
  881.  * implies invald log, forcing fsck().
  882.  */
  883. /* mark log state log wrap in log superblock */
  884. /* log->state = LOGWRAP; */
  885. /* reset sync point computation */
  886. log->syncpt = log->sync = lsn;
  887. log->nextsync = delta;
  888. } else
  889. /* next syncpt trigger = written + more */
  890. log->nextsync = written + more;
  891. /* return if lmLogSync() from outside of transaction, e.g., sync() */
  892. if (nosyncwait)
  893. return lsn;
  894. /* if number of bytes written from last sync point is more
  895.  * than 1/4 of the log size, stop new transactions from
  896.  * starting until all current transactions are completed
  897.  * by setting syncbarrier flag.
  898.  */
  899. if (written > LOGSYNC_BARRIER(logsize) && logsize > 32 * LOGPSIZE) {
  900. set_bit(log_SYNCBARRIER, &log->flag);
  901. jFYI(1, ("log barrier on: lsn=0x%x syncpt=0x%xn", lsn,
  902.  log->syncpt));
  903. }
  904. return lsn;
  905. }
  906. /*
  907.  * NAME: lmLogOpen()
  908.  *
  909.  * FUNCTION:    open the log on first open;
  910.  * insert filesystem in the active list of the log.
  911.  *
  912.  * PARAMETER: ipmnt - file system mount inode
  913.  * iplog  - log inode (out)
  914.  *
  915.  * RETURN:
  916.  *
  917.  * serialization:
  918.  */
  919. int lmLogOpen(struct super_block *sb, struct jfs_log ** logptr)
  920. {
  921. int rc;
  922. struct block_device *bdev;
  923. struct jfs_log *log;
  924. if (!(log = kmalloc(sizeof(struct jfs_log), GFP_KERNEL)))
  925. return ENOMEM;
  926. memset(log, 0, sizeof(struct jfs_log));
  927. init_waitqueue_head(&log->syncwait);
  928. log->sb = sb; /* This should be a list */
  929. if (!(JFS_SBI(sb)->mntflag & JFS_INLINELOG))
  930. goto externalLog;
  931. /*
  932.  *      in-line log in host file system
  933.  *
  934.  * file system to log have 1-to-1 relationship;
  935.  */
  936. set_bit(log_INLINELOG, &log->flag);
  937. log->bdev = sb->s_bdev;
  938. log->base = addressPXD(&JFS_SBI(sb)->logpxd);
  939. log->size = lengthPXD(&JFS_SBI(sb)->logpxd) >>
  940.     (L2LOGPSIZE - sb->s_blocksize_bits);
  941. log->l2bsize = sb->s_blocksize_bits;
  942. ASSERT(L2LOGPSIZE >= sb->s_blocksize_bits);
  943. /*
  944.  * initialize log.
  945.  */
  946. if ((rc = lmLogInit(log)))
  947. goto free;
  948. goto out;
  949. /*
  950.  *      external log as separate logical volume
  951.  *
  952.  * file systems to log may have n-to-1 relationship;
  953.  */
  954.       externalLog:
  955. /*
  956.  * TODO: Check for already opened log devices
  957.  */
  958. if (!(bdev = bdget(kdev_t_to_nr(JFS_SBI(sb)->logdev)))) {
  959. rc = ENODEV;
  960. goto free;
  961. }
  962. if ((rc = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS))) {
  963. rc = -rc;
  964. goto free;
  965. }
  966. log->bdev = bdev;
  967. memcpy(log->uuid, JFS_SBI(sb)->loguuid, sizeof(log->uuid));
  968. /*
  969.  * initialize log:
  970.  */
  971. if ((rc = lmLogInit(log)))
  972. goto close;
  973. /*
  974.  * add file system to log active file system list
  975.  */
  976. if ((rc = lmLogFileSystem(log, JFS_SBI(sb)->uuid, 1)))
  977. goto shutdown;
  978.       out:
  979. jFYI(1, ("lmLogOpen: exit(0)n"));
  980. *logptr = log;
  981. return 0;
  982. /*
  983.  *      unwind on error
  984.  */
  985.       shutdown: /* unwind lbmLogInit() */
  986. lbmLogShutdown(log);
  987.       close: /* close external log device */
  988. blkdev_put(bdev, BDEV_FS);
  989.       free: /* free log descriptor */
  990. kfree(log);
  991. jFYI(1, ("lmLogOpen: exit(%d)n", rc));
  992. return rc;
  993. }
  994. /*
  995.  * NAME: lmLogInit()
  996.  *
  997.  * FUNCTION: log initialization at first log open.
  998.  *
  999.  * logredo() (or logformat()) should have been run previously.
  1000.  * initialize the log inode from log superblock.
  1001.  * set the log state in the superblock to LOGMOUNT and
  1002.  * write SYNCPT log record.
  1003.  *
  1004.  * PARAMETER: log - log structure
  1005.  *
  1006.  * RETURN: 0 - if ok
  1007.  * EINVAL - bad log magic number or superblock dirty
  1008.  * error returned from logwait()
  1009.  *
  1010.  * serialization: single first open thread
  1011.  */
  1012. int lmLogInit(struct jfs_log * log)
  1013. {
  1014. int rc = 0;
  1015. struct lrd lrd;
  1016. struct logsuper *logsuper;
  1017. struct lbuf *bpsuper;
  1018. struct lbuf *bp;
  1019. struct logpage *lp;
  1020. int lsn;
  1021. jFYI(1, ("lmLogInit: log:0x%pn", log));
  1022. /*
  1023.  * log inode is overlaid on generic inode where
  1024.  * dinode have been zeroed out by iRead();
  1025.  */
  1026. /*
  1027.  * initialize log i/o
  1028.  */
  1029. if ((rc = lbmLogInit(log)))
  1030. return rc;
  1031. /*
  1032.  * validate log superblock
  1033.  */
  1034. if (!test_bit(log_INLINELOG, &log->flag))
  1035. log->l2bsize = 12; /* XXX kludge alert XXX */
  1036. if ((rc = lbmRead(log, 1, &bpsuper)))
  1037. goto errout10;
  1038. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1039. if (logsuper->magic != cpu_to_le32(LOGMAGIC)) {
  1040. jERROR(1, ("*** Log Format Error ! ***n"));
  1041. rc = EINVAL;
  1042. goto errout20;
  1043. }
  1044. /* logredo() should have been run successfully. */
  1045. if (logsuper->state != cpu_to_le32(LOGREDONE)) {
  1046. jERROR(1, ("*** Log Is Dirty ! ***n"));
  1047. rc = EINVAL;
  1048. goto errout20;
  1049. }
  1050. /* initialize log inode from log superblock */
  1051. if (test_bit(log_INLINELOG,&log->flag)) {
  1052. if (log->size != le32_to_cpu(logsuper->size)) {
  1053. rc = EINVAL;
  1054. goto errout20;
  1055. }
  1056. jFYI(0,
  1057.      ("lmLogInit: inline log:0x%p base:0x%Lx size:0x%xn",
  1058.       log, (unsigned long long) log->base, log->size));
  1059. } else {
  1060. if (memcmp(logsuper->uuid, log->uuid, 16)) {
  1061. jERROR(1,("wrong uuid on JFS log devicen"));
  1062. goto errout20;
  1063. }
  1064. log->size = le32_to_cpu(logsuper->size);
  1065. log->l2bsize = le32_to_cpu(logsuper->l2bsize);
  1066. jFYI(0,
  1067.      ("lmLogInit: external log:0x%p base:0x%Lx size:0x%xn",
  1068.       log, (unsigned long long) log->base, log->size));
  1069. }
  1070. log->page = le32_to_cpu(logsuper->end) / LOGPSIZE;
  1071. log->eor = le32_to_cpu(logsuper->end) - (LOGPSIZE * log->page);
  1072. /*
  1073.  * initialize for log append write mode
  1074.  */
  1075. /* establish current/end-of-log page/buffer */
  1076. if ((rc = lbmRead(log, log->page, &bp)))
  1077. goto errout20;
  1078. lp = (struct logpage *) bp->l_ldata;
  1079. jFYI(1, ("lmLogInit: lsn:0x%x page:%d eor:%d:%dn",
  1080.  le32_to_cpu(logsuper->end), log->page, log->eor,
  1081.  le16_to_cpu(lp->h.eor)));
  1082. //      ASSERT(log->eor == lp->h.eor);
  1083. log->bp = bp;
  1084. bp->l_pn = log->page;
  1085. bp->l_eor = log->eor;
  1086. /* initialize the group commit serialization lock */
  1087. LOGGC_LOCK_INIT(log);
  1088. /* if current page is full, move on to next page */
  1089. if (log->eor >= LOGPSIZE - LOGPTLRSIZE)
  1090. lmNextPage(log);
  1091. /* allocate/initialize the log write serialization lock */
  1092. LOG_LOCK_INIT(log);
  1093. /*
  1094.  * initialize log syncpoint
  1095.  */
  1096. /*
  1097.  * write the first SYNCPT record with syncpoint = 0
  1098.  * (i.e., log redo up to HERE !);
  1099.  * remove current page from lbm write queue at end of pageout
  1100.  * (to write log superblock update), but do not release to freelist;
  1101.  */
  1102. lrd.logtid = 0;
  1103. lrd.backchain = 0;
  1104. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1105. lrd.length = 0;
  1106. lrd.log.syncpt.sync = 0;
  1107. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1108. bp = log->bp;
  1109. bp->l_ceor = bp->l_eor;
  1110. lp = (struct logpage *) bp->l_ldata;
  1111. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1112. lbmWrite(log, bp, lbmWRITE | lbmSYNC, 0);
  1113. if ((rc = lbmIOWait(bp, 0)))
  1114. goto errout30;
  1115. /* initialize logsync parameters */
  1116. log->logsize = (log->size - 2) << L2LOGPSIZE;
  1117. log->lsn = lsn;
  1118. log->syncpt = lsn;
  1119. log->sync = log->syncpt;
  1120. log->nextsync = LOGSYNC_DELTA(log->logsize);
  1121. jFYI(1, ("lmLogInit: lsn:0x%x syncpt:0x%x sync:0x%xn",
  1122.  log->lsn, log->syncpt, log->sync));
  1123. LOGSYNC_LOCK_INIT(log);
  1124. INIT_LIST_HEAD(&log->synclist);
  1125. log->cqueue.head = log->cqueue.tail = 0;
  1126. log->count = 0;
  1127. /*
  1128.  * initialize for lazy/group commit
  1129.  */
  1130. log->clsn = lsn;
  1131. /*
  1132.  * update/write superblock
  1133.  */
  1134. logsuper->state = cpu_to_le32(LOGMOUNT);
  1135. log->serial = le32_to_cpu(logsuper->serial) + 1;
  1136. logsuper->serial = cpu_to_le32(log->serial);
  1137. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1138. if ((rc = lbmIOWait(bpsuper, lbmFREE)))
  1139. goto errout30;
  1140. jFYI(1, ("lmLogInit: exit(%d)n", rc));
  1141. return 0;
  1142. /*
  1143.  *      unwind on error
  1144.  */
  1145.       errout30: /* release log page */
  1146. lbmFree(bp);
  1147.       errout20: /* release log superblock */
  1148. lbmFree(bpsuper);
  1149.       errout10: /* unwind lbmLogInit() */
  1150. lbmLogShutdown(log);
  1151. jFYI(1, ("lmLogInit: exit(%d)n", rc));
  1152. return rc;
  1153. }
  1154. /*
  1155.  * NAME: lmLogClose()
  1156.  *
  1157.  * FUNCTION: remove file system <ipmnt> from active list of log <iplog>
  1158.  * and close it on last close.
  1159.  *
  1160.  * PARAMETER: sb - superblock
  1161.  * log - log inode
  1162.  *
  1163.  * RETURN: errors from subroutines
  1164.  *
  1165.  * serialization:
  1166.  */
  1167. int lmLogClose(struct super_block *sb, struct jfs_log * log)
  1168. {
  1169. int rc;
  1170. jFYI(1, ("lmLogClose: log:0x%pn", log));
  1171. if (!test_bit(log_INLINELOG, &log->flag))
  1172. goto externalLog;
  1173. /*
  1174.  *      in-line log in host file system
  1175.  */
  1176. rc = lmLogShutdown(log);
  1177. goto out;
  1178. /*
  1179.  *      external log as separate logical volume
  1180.  */
  1181.       externalLog:
  1182. lmLogFileSystem(log, JFS_SBI(sb)->uuid, 0);
  1183. rc = lmLogShutdown(log);
  1184. blkdev_put(log->bdev, BDEV_FS);
  1185.       out:
  1186. jFYI(0, ("lmLogClose: exit(%d)n", rc));
  1187. return rc;
  1188. }
  1189. /*
  1190.  * NAME: lmLogWait()
  1191.  *
  1192.  * FUNCTION: wait for all outstanding log records to be written to disk
  1193.  */
  1194. void lmLogWait(struct jfs_log *log)
  1195. {
  1196. int i;
  1197. jFYI(1, ("lmLogWait: log:0x%pn", log));
  1198. if (log->cqueue.head || !list_empty(&log->synclist)) {
  1199. /*
  1200.  * If there was very recent activity, we may need to wait
  1201.  * for the lazycommit thread to catch up
  1202.  */
  1203. for (i = 0; i < 800; i++) { /* Too much? */
  1204. current->state = TASK_INTERRUPTIBLE;
  1205. schedule_timeout(HZ / 4);
  1206. if ((log->cqueue.head == NULL) &&
  1207.     list_empty(&log->synclist))
  1208. break;
  1209. }
  1210. }
  1211. assert(log->cqueue.head == NULL);
  1212. assert(list_empty(&log->synclist));
  1213. }
  1214. /*
  1215.  * NAME: lmLogShutdown()
  1216.  *
  1217.  * FUNCTION: log shutdown at last LogClose().
  1218.  *
  1219.  * write log syncpt record.
  1220.  * update super block to set redone flag to 0.
  1221.  *
  1222.  * PARAMETER: log - log inode
  1223.  *
  1224.  * RETURN: 0 - success
  1225.  *
  1226.  * serialization: single last close thread
  1227.  */
  1228. int lmLogShutdown(struct jfs_log * log)
  1229. {
  1230. int rc;
  1231. struct lrd lrd;
  1232. int lsn;
  1233. struct logsuper *logsuper;
  1234. struct lbuf *bpsuper;
  1235. struct lbuf *bp;
  1236. struct logpage *lp;
  1237. jFYI(1, ("lmLogShutdown: log:0x%pn", log));
  1238. lmLogWait(log);
  1239. /*
  1240.  * We need to make sure all of the "written" metapages
  1241.  * actually make it to disk
  1242.  */
  1243. fsync_no_super(log->sb->s_dev);
  1244. /*
  1245.  * write the last SYNCPT record with syncpoint = 0
  1246.  * (i.e., log redo up to HERE !)
  1247.  */
  1248. lrd.logtid = 0;
  1249. lrd.backchain = 0;
  1250. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1251. lrd.length = 0;
  1252. lrd.log.syncpt.sync = 0;
  1253. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1254. bp = log->bp;
  1255. lp = (struct logpage *) bp->l_ldata;
  1256. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1257. lbmWrite(log, log->bp, lbmWRITE | lbmRELEASE | lbmSYNC, 0);
  1258. lbmIOWait(log->bp, lbmFREE);
  1259. /*
  1260.  * synchronous update log superblock
  1261.  * mark log state as shutdown cleanly
  1262.  * (i.e., Log does not need to be replayed).
  1263.  */
  1264. if ((rc = lbmRead(log, 1, &bpsuper)))
  1265. goto out;
  1266. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1267. logsuper->state = cpu_to_le32(LOGREDONE);
  1268. logsuper->end = cpu_to_le32(lsn);
  1269. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1270. rc = lbmIOWait(bpsuper, lbmFREE);
  1271. jFYI(1, ("lmLogShutdown: lsn:0x%x page:%d eor:%dn",
  1272.  lsn, log->page, log->eor));
  1273.       out:    
  1274. /*
  1275.  * shutdown per log i/o
  1276.  */
  1277. lbmLogShutdown(log);
  1278. if (rc) {
  1279. jFYI(1, ("lmLogShutdown: exit(%d)n", rc));
  1280. }
  1281. return rc;
  1282. }
  1283. /*
  1284.  * NAME: lmLogFileSystem()
  1285.  *
  1286.  * FUNCTION: insert (<activate> = true)/remove (<activate> = false)
  1287.  * file system into/from log active file system list.
  1288.  *
  1289.  * PARAMETE: log - pointer to logs inode.
  1290.  * fsdev - kdev_t of filesystem.
  1291.  * serial  - pointer to returned log serial number
  1292.  * activate - insert/remove device from active list.
  1293.  *
  1294.  * RETURN: 0 - success
  1295.  * errors returned by vms_iowait().
  1296.  */
  1297. static int lmLogFileSystem(struct jfs_log * log, char *uuid, int activate)
  1298. {
  1299. int rc = 0;
  1300. int i;
  1301. struct logsuper *logsuper;
  1302. struct lbuf *bpsuper;
  1303. /*
  1304.  * insert/remove file system device to log active file system list.
  1305.  */
  1306. if ((rc = lbmRead(log, 1, &bpsuper)))
  1307. return rc;
  1308. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1309. if (activate) {
  1310. for (i = 0; i < MAX_ACTIVE; i++)
  1311. if (!memcmp(logsuper->active[i].uuid, NULL_UUID, 16)) {
  1312. memcpy(logsuper->active[i].uuid, uuid, 16);
  1313. break;
  1314. }
  1315. if (i == MAX_ACTIVE) {
  1316. jERROR(1,("Too many file systems sharing journal!n"));
  1317. lbmFree(bpsuper);
  1318. return EMFILE; /* Is there a better rc? */
  1319. }
  1320. } else {
  1321. for (i = 0; i < MAX_ACTIVE; i++)
  1322. if (!memcmp(logsuper->active[i].uuid, uuid, 16)) {
  1323. memcpy(logsuper->active[i].uuid, NULL_UUID, 16);
  1324. break;
  1325. }
  1326. if (i == MAX_ACTIVE) {
  1327. jERROR(1,("Somebody stomped on the journal!n"));
  1328. lbmFree(bpsuper);
  1329. return EIO;
  1330. }
  1331. }
  1332. /*
  1333.  * synchronous write log superblock:
  1334.  *
  1335.  * write sidestream bypassing write queue:
  1336.  * at file system mount, log super block is updated for
  1337.  * activation of the file system before any log record
  1338.  * (MOUNT record) of the file system, and at file system
  1339.  * unmount, all meta data for the file system has been
  1340.  * flushed before log super block is updated for deactivation
  1341.  * of the file system.
  1342.  */
  1343. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1344. rc = lbmIOWait(bpsuper, lbmFREE);
  1345. return rc;
  1346. }
  1347. /*
  1348.  * log buffer manager (lbm)
  1349.  * ------------------------
  1350.  *
  1351.  * special purpose buffer manager supporting log i/o requirements.
  1352.  *
  1353.  * per log write queue:
  1354.  * log pageout occurs in serial order by fifo write queue and
  1355.  * restricting to a single i/o in pregress at any one time.
  1356.  * a circular singly-linked list
  1357.  * (log->wrqueue points to the tail, and buffers are linked via
  1358.  * bp->wrqueue field), and
  1359.  * maintains log page in pageout ot waiting for pageout in serial pageout.
  1360.  */
  1361. /*
  1362.  * lbmLogInit()
  1363.  *
  1364.  * initialize per log I/O setup at lmLogInit()
  1365.  */
  1366. static int lbmLogInit(struct jfs_log * log)
  1367. { /* log inode */
  1368. int i;
  1369. struct lbuf *lbuf;
  1370. jFYI(1, ("lbmLogInit: log:0x%pn", log));
  1371. /* initialize current buffer cursor */
  1372. log->bp = NULL;
  1373. /* initialize log device write queue */
  1374. log->wqueue = NULL;
  1375. /*
  1376.  * Each log has its own buffer pages allocated to it.  These are
  1377.  * not managed by the page cache.  This ensures that a transaction
  1378.  * writing to the log does not block trying to allocate a page from
  1379.  * the page cache (for the log).  This would be bad, since page
  1380.  * allocation waits on the kswapd thread that may be committing inodes
  1381.  * which would cause log activity.  Was that clear?  I'm trying to
  1382.  * avoid deadlock here.
  1383.  */
  1384. init_waitqueue_head(&log->free_wait);
  1385. log->lbuf_free = NULL;
  1386. for (i = 0; i < LOGPAGES; i++) {
  1387. lbuf = kmalloc(sizeof(struct lbuf), GFP_KERNEL);
  1388. if (lbuf == 0)
  1389. goto error;
  1390. lbuf->l_bh.b_data = lbuf->l_ldata =
  1391.     (char *) __get_free_page(GFP_KERNEL);
  1392. if (lbuf->l_ldata == 0) {
  1393. kfree(lbuf);
  1394. goto error;
  1395. }
  1396. lbuf->l_log = log;
  1397. init_waitqueue_head(&lbuf->l_ioevent);
  1398. lbuf->l_bh.b_size = LOGPSIZE;
  1399. lbuf->l_bh.b_dev = to_kdev_t(log->bdev->bd_dev);
  1400. lbuf->l_bh.b_end_io = lbmIODone;
  1401. lbuf->l_bh.b_private = lbuf;
  1402. lbuf->l_bh.b_page = virt_to_page(lbuf->l_ldata);
  1403. lbuf->l_bh.b_state = 0;
  1404. init_waitqueue_head(&lbuf->l_bh.b_wait);
  1405. lbuf->l_freelist = log->lbuf_free;
  1406. log->lbuf_free = lbuf;
  1407. }
  1408. return (0);
  1409.       error:
  1410. lbmLogShutdown(log);
  1411. return (ENOMEM);
  1412. }
  1413. /*
  1414.  * lbmLogShutdown()
  1415.  *
  1416.  * finalize per log I/O setup at lmLogShutdown()
  1417.  */
  1418. static void lbmLogShutdown(struct jfs_log * log)
  1419. {
  1420. struct lbuf *lbuf;
  1421. jFYI(1, ("lbmLogShutdown: log:0x%pn", log));
  1422. lbuf = log->lbuf_free;
  1423. while (lbuf) {
  1424. struct lbuf *next = lbuf->l_freelist;
  1425. free_page((unsigned long) lbuf->l_ldata);
  1426. kfree(lbuf);
  1427. lbuf = next;
  1428. }
  1429. log->bp = NULL;
  1430. }
  1431. /*
  1432.  * lbmAllocate()
  1433.  *
  1434.  * allocate an empty log buffer
  1435.  */
  1436. static struct lbuf *lbmAllocate(struct jfs_log * log, int pn)
  1437. {
  1438. struct lbuf *bp;
  1439. unsigned long flags;
  1440. /*
  1441.  * recycle from log buffer freelist if any
  1442.  */
  1443. LCACHE_LOCK(flags);
  1444. LCACHE_SLEEP_COND(log->free_wait, (bp = log->lbuf_free), flags);
  1445. log->lbuf_free = bp->l_freelist;
  1446. LCACHE_UNLOCK(flags);
  1447. bp->l_flag = 0;
  1448. bp->l_wqnext = NULL;
  1449. bp->l_freelist = NULL;
  1450. bp->l_pn = pn;
  1451. bp->l_blkno = log->base + (pn << (L2LOGPSIZE - log->l2bsize));
  1452. bp->l_bh.b_blocknr = bp->l_blkno;
  1453. bp->l_ceor = 0;
  1454. return bp;
  1455. }
  1456. /*
  1457.  * lbmFree()
  1458.  *
  1459.  * release a log buffer to freelist
  1460.  */
  1461. static void lbmFree(struct lbuf * bp)
  1462. {
  1463. unsigned long flags;
  1464. LCACHE_LOCK(flags);
  1465. lbmfree(bp);
  1466. LCACHE_UNLOCK(flags);
  1467. }
  1468. static void lbmfree(struct lbuf * bp)
  1469. {
  1470. struct jfs_log *log = bp->l_log;
  1471. assert(bp->l_wqnext == NULL);
  1472. /*
  1473.  * return the buffer to head of freelist
  1474.  */
  1475. bp->l_freelist = log->lbuf_free;
  1476. log->lbuf_free = bp;
  1477. wake_up(&log->free_wait);
  1478. return;
  1479. }
  1480. /*
  1481.  * NAME: lbmRedrive
  1482.  *
  1483.  * FUNCTION: add a log buffer to the the log redrive list
  1484.  *
  1485.  * PARAMETER:
  1486.  *     bp - log buffer
  1487.  *
  1488.  * NOTES:
  1489.  * Takes log_redrive_lock.
  1490.  */
  1491. static inline void lbmRedrive(struct lbuf *bp)
  1492. {
  1493. unsigned long flags;
  1494. spin_lock_irqsave(&log_redrive_lock, flags);
  1495. bp->l_redrive_next = log_redrive_list;
  1496. log_redrive_list = bp;
  1497. spin_unlock_irqrestore(&log_redrive_lock, flags);
  1498. wake_up(&jfs_IO_thread_wait);
  1499. }
  1500. /*
  1501.  * lbmRead()
  1502.  */
  1503. static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)
  1504. {
  1505. struct lbuf *bp;
  1506. /*
  1507.  * allocate a log buffer
  1508.  */
  1509. *bpp = bp = lbmAllocate(log, pn);
  1510. jFYI(1, ("lbmRead: bp:0x%p pn:0x%xn", bp, pn));
  1511. bp->l_flag |= lbmREAD;
  1512. bp->l_bh.b_reqnext = NULL;
  1513. clear_bit(BH_Uptodate, &bp->l_bh.b_state);
  1514. lock_buffer(&bp->l_bh);
  1515. set_bit(BH_Mapped, &bp->l_bh.b_state);
  1516. set_bit(BH_Req, &bp->l_bh.b_state);
  1517. bp->l_bh.b_rdev = bp->l_bh.b_dev;
  1518. bp->l_bh.b_rsector = bp->l_blkno << (log->l2bsize - 9);
  1519. generic_make_request(READ, &bp->l_bh);
  1520. run_task_queue(&tq_disk);
  1521. wait_event(bp->l_ioevent, (bp->l_flag != lbmREAD));
  1522. return 0;
  1523. }
  1524. /*
  1525.  * lbmWrite()
  1526.  *
  1527.  * buffer at head of pageout queue stays after completion of
  1528.  * partial-page pageout and redriven by explicit initiation of
  1529.  * pageout by caller until full-page pageout is completed and
  1530.  * released.
  1531.  *
  1532.  * device driver i/o done redrives pageout of new buffer at
  1533.  * head of pageout queue when current buffer at head of pageout
  1534.  * queue is released at the completion of its full-page pageout.
  1535.  *
  1536.  * LOGGC_LOCK() serializes lbmWrite() by lmNextPage() and lmGroupCommit().
  1537.  * LCACHE_LOCK() serializes xflag between lbmWrite() and lbmIODone()
  1538.  */
  1539. static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
  1540.      int cant_block)
  1541. {
  1542. struct lbuf *tail;
  1543. unsigned long flags;
  1544. jFYI(1, ("lbmWrite: bp:0x%p flag:0x%x pn:0x%xn",
  1545.  bp, flag, bp->l_pn));
  1546. /* map the logical block address to physical block address */
  1547. bp->l_blkno =
  1548.     log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1549. LCACHE_LOCK(flags); /* disable+lock */
  1550. /*
  1551.  * initialize buffer for device driver
  1552.  */
  1553. bp->l_flag = flag;
  1554. /*
  1555.  *      insert bp at tail of write queue associated with log
  1556.  *
  1557.  * (request is either for bp already/currently at head of queue
  1558.  * or new bp to be inserted at tail)
  1559.  */
  1560. tail = log->wqueue;
  1561. /* is buffer not already on write queue ? */
  1562. if (bp->l_wqnext == NULL) {
  1563. /* insert at tail of wqueue */
  1564. if (tail == NULL) {
  1565. log->wqueue = bp;
  1566. bp->l_wqnext = bp;
  1567. } else {
  1568. log->wqueue = bp;
  1569. bp->l_wqnext = tail->l_wqnext;
  1570. tail->l_wqnext = bp;
  1571. }
  1572. tail = bp;
  1573. }
  1574. /* is buffer at head of wqueue and for write ? */
  1575. if ((bp != tail->l_wqnext) || !(flag & lbmWRITE)) {
  1576. LCACHE_UNLOCK(flags); /* unlock+enable */
  1577. return;
  1578. }
  1579. LCACHE_UNLOCK(flags); /* unlock+enable */
  1580. if (cant_block)
  1581. lbmRedrive(bp);
  1582. else if (flag & lbmSYNC)
  1583. lbmStartIO(bp);
  1584. else {
  1585. LOGGC_UNLOCK(log);
  1586. lbmStartIO(bp);
  1587. LOGGC_LOCK(log);
  1588. }
  1589. }
  1590. /*
  1591.  * lbmDirectWrite()
  1592.  *
  1593.  * initiate pageout bypassing write queue for sidestream
  1594.  * (e.g., log superblock) write;
  1595.  */
  1596. static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag)
  1597. {
  1598. jEVENT(0, ("lbmDirectWrite: bp:0x%p flag:0x%x pn:0x%xn",
  1599.    bp, flag, bp->l_pn));
  1600. /*
  1601.  * initialize buffer for device driver
  1602.  */
  1603. bp->l_flag = flag | lbmDIRECT;
  1604. /* map the logical block address to physical block address */
  1605. bp->l_blkno =
  1606.     log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1607. /*
  1608.  *      initiate pageout of the page
  1609.  */
  1610. lbmStartIO(bp);
  1611. }
  1612. /*
  1613.  * NAME: lbmStartIO()
  1614.  *
  1615.  * FUNCTION: Interface to DD strategy routine
  1616.  *
  1617.  * RETURN:      none
  1618.  *
  1619.  * serialization: LCACHE_LOCK() is NOT held during log i/o;
  1620.  */
  1621. void lbmStartIO(struct lbuf * bp)
  1622. {
  1623. jFYI(1, ("lbmStartIOn"));
  1624. bp->l_bh.b_reqnext = NULL;
  1625. set_bit(BH_Dirty, &bp->l_bh.b_state);
  1626. //      lock_buffer(&bp->l_bh);
  1627. assert(!test_bit(BH_Lock, &bp->l_bh.b_state));
  1628. set_bit(BH_Lock, &bp->l_bh.b_state);
  1629. set_bit(BH_Mapped, &bp->l_bh.b_state);
  1630. set_bit(BH_Req, &bp->l_bh.b_state);
  1631. bp->l_bh.b_rdev = bp->l_bh.b_dev;
  1632. bp->l_bh.b_rsector = bp->l_blkno << (bp->l_log->l2bsize - 9);
  1633. generic_make_request(WRITE, &bp->l_bh);
  1634. INCREMENT(lmStat.submitted);
  1635. run_task_queue(&tq_disk);
  1636. jFYI(1, ("lbmStartIO donen"));
  1637. }
  1638. /*
  1639.  * lbmIOWait()
  1640.  */
  1641. static int lbmIOWait(struct lbuf * bp, int flag)
  1642. {
  1643. unsigned long flags;
  1644. int rc = 0;
  1645. jFYI(1,
  1646.      ("lbmIOWait1: bp:0x%p flag:0x%x:0x%xn", bp, bp->l_flag,
  1647.       flag));
  1648. LCACHE_LOCK(flags); /* disable+lock */
  1649. LCACHE_SLEEP_COND(bp->l_ioevent, (bp->l_flag & lbmDONE), flags);
  1650. rc = (bp->l_flag & lbmERROR) ? EIO : 0;
  1651. if (flag & lbmFREE)
  1652. lbmfree(bp);
  1653. LCACHE_UNLOCK(flags); /* unlock+enable */
  1654. jFYI(1,
  1655.      ("lbmIOWait2: bp:0x%p flag:0x%x:0x%xn", bp, bp->l_flag,
  1656.       flag));
  1657. return rc;
  1658. }
  1659. /*
  1660.  * lbmIODone()
  1661.  *
  1662.  * executed at INTIODONE level
  1663.  */
  1664. static void lbmIODone(struct buffer_head *bh, int uptodate)
  1665. {
  1666. struct lbuf *bp = bh->b_private;
  1667. struct lbuf *nextbp, *tail;
  1668. struct jfs_log *log;
  1669. unsigned long flags;
  1670. /*
  1671.  * get back jfs buffer bound to the i/o buffer
  1672.  */
  1673. jEVENT(0, ("lbmIODone: bp:0x%p flag:0x%xn", bp, bp->l_flag));
  1674. LCACHE_LOCK(flags); /* disable+lock */
  1675. unlock_buffer(&bp->l_bh);
  1676. bp->l_flag |= lbmDONE;
  1677. if (!uptodate) {
  1678. bp->l_flag |= lbmERROR;
  1679. jERROR(1, ("lbmIODone: I/O error in JFS logn"));
  1680. }
  1681. /*
  1682.  *      pagein completion
  1683.  */
  1684. if (bp->l_flag & lbmREAD) {
  1685. bp->l_flag &= ~lbmREAD;
  1686. LCACHE_UNLOCK(flags); /* unlock+enable */
  1687. /* wakeup I/O initiator */
  1688. LCACHE_WAKEUP(&bp->l_ioevent);
  1689. return;
  1690. }
  1691. /*
  1692.  *      pageout completion
  1693.  *
  1694.  * the bp at the head of write queue has completed pageout.
  1695.  *
  1696.  * if single-commit/full-page pageout, remove the current buffer
  1697.  * from head of pageout queue, and redrive pageout with
  1698.  * the new buffer at head of pageout queue;
  1699.  * otherwise, the partial-page pageout buffer stays at
  1700.  * the head of pageout queue to be redriven for pageout
  1701.  * by lmGroupCommit() until full-page pageout is completed.
  1702.  */
  1703. bp->l_flag &= ~lbmWRITE;
  1704. INCREMENT(lmStat.pagedone);
  1705. /* update committed lsn */
  1706. log = bp->l_log;
  1707. log->clsn = (bp->l_pn << L2LOGPSIZE) + bp->l_ceor;
  1708. if (bp->l_flag & lbmDIRECT) {
  1709. LCACHE_WAKEUP(&bp->l_ioevent);
  1710. LCACHE_UNLOCK(flags);
  1711. return;
  1712. }
  1713. tail = log->wqueue;
  1714. /* single element queue */
  1715. if (bp == tail) {
  1716. /* remove head buffer of full-page pageout
  1717.  * from log device write queue
  1718.  */
  1719. if (bp->l_flag & lbmRELEASE) {
  1720. log->wqueue = NULL;
  1721. bp->l_wqnext = NULL;
  1722. }
  1723. }
  1724. /* multi element queue */
  1725. else {
  1726. /* remove head buffer of full-page pageout
  1727.  * from log device write queue
  1728.  */
  1729. if (bp->l_flag & lbmRELEASE) {
  1730. nextbp = tail->l_wqnext = bp->l_wqnext;
  1731. bp->l_wqnext = NULL;
  1732. /*
  1733.  * redrive pageout of next page at head of write queue:
  1734.  * redrive next page without any bound tblk
  1735.  * (i.e., page w/o any COMMIT records), or
  1736.  * first page of new group commit which has been
  1737.  * queued after current page (subsequent pageout
  1738.  * is performed synchronously, except page without
  1739.  * any COMMITs) by lmGroupCommit() as indicated
  1740.  * by lbmWRITE flag;
  1741.  */
  1742. if (nextbp->l_flag & lbmWRITE) {
  1743. /*
  1744.  * We can't do the I/O at interrupt time.
  1745.  * The jfsIO thread can do it
  1746.  */
  1747. lbmRedrive(nextbp);
  1748. }
  1749. }
  1750. }
  1751. /*
  1752.  *      synchronous pageout:
  1753.  *
  1754.  * buffer has not necessarily been removed from write queue
  1755.  * (e.g., synchronous write of partial-page with COMMIT):
  1756.  * leave buffer for i/o initiator to dispose
  1757.  */
  1758. if (bp->l_flag & lbmSYNC) {
  1759. LCACHE_UNLOCK(flags); /* unlock+enable */
  1760. /* wakeup I/O initiator */
  1761. LCACHE_WAKEUP(&bp->l_ioevent);
  1762. }
  1763. /*
  1764.  *      Group Commit pageout:
  1765.  */
  1766. else if (bp->l_flag & lbmGC) {
  1767. LCACHE_UNLOCK(flags);
  1768. lmPostGC(bp);
  1769. }
  1770. /*
  1771.  *      asynchronous pageout:
  1772.  *
  1773.  * buffer must have been removed from write queue:
  1774.  * insert buffer at head of freelist where it can be recycled
  1775.  */
  1776. else {
  1777. assert(bp->l_flag & lbmRELEASE);
  1778. assert(bp->l_flag & lbmFREE);
  1779. lbmfree(bp);
  1780. LCACHE_UNLOCK(flags); /* unlock+enable */
  1781. }
  1782. }
  1783. int jfsIOWait(void *arg)
  1784. {
  1785. struct lbuf *bp;
  1786. jFYI(1, ("jfsIOWait is here!n"));
  1787. lock_kernel();
  1788. daemonize();
  1789. current->tty = NULL;
  1790. strcpy(current->comm, "jfsIO");
  1791. unlock_kernel();
  1792. spin_lock_irq(&current->sigmask_lock);
  1793. sigfillset(&current->blocked);
  1794. recalc_sigpending(current);
  1795. spin_unlock_irq(&current->sigmask_lock);
  1796. complete(&jfsIOwait);
  1797. do {
  1798. DECLARE_WAITQUEUE(wq, current);
  1799. spin_lock_irq(&log_redrive_lock);
  1800. while ((bp = log_redrive_list)) {
  1801. log_redrive_list = bp->l_redrive_next;
  1802. bp->l_redrive_next = NULL;
  1803. spin_unlock_irq(&log_redrive_lock);
  1804. lbmStartIO(bp);
  1805. spin_lock_irq(&log_redrive_lock);
  1806. }
  1807. add_wait_queue(&jfs_IO_thread_wait, &wq);
  1808. set_current_state(TASK_INTERRUPTIBLE);
  1809. spin_unlock_irq(&log_redrive_lock);
  1810. schedule();
  1811. current->state = TASK_RUNNING;
  1812. remove_wait_queue(&jfs_IO_thread_wait, &wq);
  1813. } while (!jfs_stop_threads);
  1814. jFYI(1,("jfsIOWait being killed!n"));
  1815. complete(&jfsIOwait);
  1816. return 0;
  1817. }
  1818. /*
  1819.  * NAME: lmLogFormat()/jfs_logform()
  1820.  *
  1821.  * FUNCTION: format file system log
  1822.  *
  1823.  * PARAMETERS:
  1824.  *      log - volume log
  1825.  * logAddress - start address of log space in FS block
  1826.  * logSize - length of log space in FS block;
  1827.  *
  1828.  * RETURN: 0 - success
  1829.  * -EIO - i/o error
  1830.  *
  1831.  * XXX: We're synchronously writing one page at a time.  This needs to
  1832.  * be improved by writing multiple pages at once.
  1833.  */
  1834. int lmLogFormat(struct jfs_log *log, s64 logAddress, int logSize)
  1835. {
  1836. int rc = -EIO;
  1837. struct jfs_sb_info *sbi = JFS_SBI(log->sb);
  1838. struct logsuper *logsuper;
  1839. struct logpage *lp;
  1840. int lspn; /* log sequence page number */
  1841. struct lrd *lrd_ptr;
  1842. int npages = 0;
  1843. struct lbuf *bp;
  1844. jFYI(0, ("lmLogFormat: logAddress:%Ld logSize:%dn",
  1845.  (long long)logAddress, logSize));
  1846. /* allocate a log buffer */
  1847. bp = lbmAllocate(log, 1);
  1848. npages = logSize >> sbi->l2nbperpage;
  1849. /*
  1850.  *      log space:
  1851.  *
  1852.  * page 0 - reserved;
  1853.  * page 1 - log superblock;
  1854.  * page 2 - log data page: A SYNC log record is written
  1855.  *          into this page at logform time;
  1856.  * pages 3-N - log data page: set to empty log data pages;
  1857.  */
  1858. /*
  1859.  *      init log superblock: log page 1
  1860.  */
  1861. logsuper = (struct logsuper *) bp->l_ldata;
  1862. logsuper->magic = cpu_to_le32(LOGMAGIC);
  1863. logsuper->version = cpu_to_le32(LOGVERSION);
  1864. logsuper->state = cpu_to_le32(LOGREDONE);
  1865. logsuper->flag = cpu_to_le32(sbi->mntflag); /* ? */
  1866. logsuper->size = cpu_to_le32(npages);
  1867. logsuper->bsize = cpu_to_le32(sbi->bsize);
  1868. logsuper->l2bsize = cpu_to_le32(sbi->l2bsize);
  1869. logsuper->end = cpu_to_le32(2 * LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE);
  1870. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  1871. bp->l_blkno = logAddress + sbi->nbperpage;
  1872. lbmStartIO(bp);
  1873. if ((rc = lbmIOWait(bp, 0)))
  1874. goto exit;
  1875. /*
  1876.  *      init pages 2 to npages-1 as log data pages:
  1877.  *
  1878.  * log page sequence number (lpsn) initialization:
  1879.  *
  1880.  * pn:   0     1     2     3                 n-1
  1881.  *       +-----+-----+=====+=====+===.....===+=====+
  1882.  * lspn:             N-1   0     1           N-2
  1883.  *                   <--- N page circular file ---->
  1884.  *
  1885.  * the N (= npages-2) data pages of the log is maintained as
  1886.  * a circular file for the log records;
  1887.  * lpsn grows by 1 monotonically as each log page is written
  1888.  * to the circular file of the log;
  1889.  * and setLogpage() will not reset the page number even if
  1890.  * the eor is equal to LOGPHDRSIZE. In order for binary search
  1891.  * still work in find log end process, we have to simulate the
  1892.  * log wrap situation at the log format time.
  1893.  * The 1st log page written will have the highest lpsn. Then
  1894.  * the succeeding log pages will have ascending order of
  1895.  * the lspn starting from 0, ... (N-2)
  1896.  */
  1897. lp = (struct logpage *) bp->l_ldata;
  1898. /*
  1899.  * initialize 1st log page to be written: lpsn = N - 1,
  1900.  * write a SYNCPT log record is written to this page
  1901.  */
  1902. lp->h.page = lp->t.page = cpu_to_le32(npages - 3);
  1903. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE + LOGRDSIZE);
  1904. lrd_ptr = (struct lrd *) &lp->data;
  1905. lrd_ptr->logtid = 0;
  1906. lrd_ptr->backchain = 0;
  1907. lrd_ptr->type = cpu_to_le16(LOG_SYNCPT);
  1908. lrd_ptr->length = 0;
  1909. lrd_ptr->log.syncpt.sync = 0;
  1910. bp->l_blkno += sbi->nbperpage;
  1911. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  1912. lbmStartIO(bp);
  1913. if ((rc = lbmIOWait(bp, 0)))
  1914. goto exit;
  1915. /*
  1916.  *      initialize succeeding log pages: lpsn = 0, 1, ..., (N-2)
  1917.  */
  1918. for (lspn = 0; lspn < npages - 3; lspn++) {
  1919. lp->h.page = lp->t.page = cpu_to_le32(lspn);
  1920. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
  1921. bp->l_blkno += sbi->nbperpage;
  1922. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  1923. lbmStartIO(bp);
  1924. if ((rc = lbmIOWait(bp, 0)))
  1925. goto exit;
  1926. }
  1927. rc = 0;
  1928. exit:
  1929. /*
  1930.  *      finalize log
  1931.  */
  1932. /* release the buffer */
  1933. lbmFree(bp);
  1934. return rc;
  1935. }
  1936. #ifdef CONFIG_JFS_STATISTICS
  1937. int jfs_lmstats_read(char *buffer, char **start, off_t offset, int length,
  1938.       int *eof, void *data)
  1939. {
  1940. int len = 0;
  1941. off_t begin;
  1942. len += sprintf(buffer,
  1943.        "JFS Logmgr statsn"
  1944.        "================n"
  1945.        "commits = %dn"
  1946.        "writes submitted = %dn"
  1947.        "writes completed = %dn",
  1948.        lmStat.commit,
  1949.        lmStat.submitted,
  1950.        lmStat.pagedone);
  1951. begin = offset;
  1952. *start = buffer + begin;
  1953. len -= begin;
  1954. if (len > length)
  1955. len = length;
  1956. else
  1957. *eof = 1;
  1958. if (len < 0)
  1959. len = 0;
  1960. return len;
  1961. }
  1962. #endif /* CONFIG_JFS_STATISTICS */