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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2. ** Write ahead logging implementation copyright Chris Mason 2000
  3. **
  4. ** The background commits make this code very interelated, and 
  5. ** overly complex.  I need to rethink things a bit....The major players:
  6. **
  7. ** journal_begin -- call with the number of blocks you expect to log.  
  8. **                  If the current transaction is too
  9. **      old, it will block until the current transaction is 
  10. **      finished, and then start a new one.
  11. **     Usually, your transaction will get joined in with 
  12. **                  previous ones for speed.
  13. **
  14. ** journal_join  -- same as journal_begin, but won't block on the current 
  15. **                  transaction regardless of age.  Don't ever call
  16. **                  this.  Ever.  There are only two places it should be 
  17. **                  called from, and they are both inside this file.
  18. **
  19. ** journal_mark_dirty -- adds blocks into this transaction.  clears any flags 
  20. **                       that might make them get sent to disk
  21. **                       and then marks them BH_JDirty.  Puts the buffer head 
  22. **                       into the current transaction hash.  
  23. **
  24. ** journal_end -- if the current transaction is batchable, it does nothing
  25. **                   otherwise, it could do an async/synchronous commit, or
  26. **                   a full flush of all log and real blocks in the 
  27. **                   transaction.
  28. **
  29. ** flush_old_commits -- if the current transaction is too old, it is ended and 
  30. **                      commit blocks are sent to disk.  Forces commit blocks 
  31. **                      to disk for all backgrounded commits that have been 
  32. **                      around too long.
  33. **      -- Note, if you call this as an immediate flush from 
  34. **         from within kupdate, it will ignore the immediate flag
  35. **
  36. ** The commit thread -- a writer process for async commits.  It allows a 
  37. **                      a process to request a log flush on a task queue.
  38. **                      the commit will happen once the commit thread wakes up.
  39. **                      The benefit here is the writer (with whatever
  40. **                      related locks it has) doesn't have to wait for the
  41. **                      log blocks to hit disk if it doesn't want to.
  42. */
  43. #include <linux/config.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/system.h>
  46. #include <linux/sched.h>
  47. #include <asm/semaphore.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/reiserfs_fs.h>
  50. #include <linux/kernel.h>
  51. #include <linux/errno.h>
  52. #include <linux/fcntl.h>
  53. #include <linux/locks.h>
  54. #include <linux/stat.h>
  55. #include <linux/string.h>
  56. #include <linux/smp_lock.h>
  57. /* the number of mounted filesystems.  This is used to decide when to
  58. ** start and kill the commit thread
  59. */
  60. static int reiserfs_mounted_fs_count = 0 ;
  61. /* wake this up when you add something to the commit thread task queue */
  62. DECLARE_WAIT_QUEUE_HEAD(reiserfs_commit_thread_wait) ;
  63. /* wait on this if you need to be sure you task queue entries have been run */
  64. static DECLARE_WAIT_QUEUE_HEAD(reiserfs_commit_thread_done) ;
  65. DECLARE_TASK_QUEUE(reiserfs_commit_thread_tq) ;
  66. #define JOURNAL_TRANS_HALF 1018   /* must be correct to keep the desc and commit
  67.      structs at 4k */
  68. #define BUFNR 64 /*read ahead */
  69. /* cnode stat bits.  Move these into reiserfs_fs.h */
  70. #define BLOCK_FREED 2 /* this block was freed, and can't be written.  */
  71. #define BLOCK_FREED_HOLDER 3    /* this block was freed during this transaction, and can't be written */
  72. #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
  73. /* flags for do_journal_end */
  74. #define FLUSH_ALL   1 /* flush commit and real blocks */
  75. #define COMMIT_NOW  2 /* end and commit this transaction */
  76. #define WAIT        4 /* wait for the log blocks to hit the disk*/
  77. /* state bits for the journal */
  78. #define WRITERS_BLOCKED 1      /* set when new writers not allowed */
  79. static int do_journal_end(struct reiserfs_transaction_handle *,struct super_block *,unsigned long nblocks,int flags) ;
  80. static int flush_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
  81. static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall)  ;
  82. static int can_dirty(struct reiserfs_journal_cnode *cn) ;
  83. static int remove_from_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, struct buffer_head *bh, int remove_freed);
  84. static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks);
  85. static void init_journal_hash(struct super_block *p_s_sb) {
  86.   memset(SB_JOURNAL(p_s_sb)->j_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
  87. }
  88. /*
  89. ** clears BH_Dirty and sticks the buffer on the clean list.  Called because I can't allow refile_buffer to
  90. ** make schedule happen after I've freed a block.  Look at remove_from_transaction and journal_mark_freed for
  91. ** more details.
  92. */
  93. static int reiserfs_clean_and_file_buffer(struct buffer_head *bh) {
  94.   if (bh) {
  95.     clear_bit(BH_Dirty, &bh->b_state) ;
  96.     refile_buffer(bh) ;
  97.   }
  98.   return 0 ;
  99. }
  100. static struct reiserfs_bitmap_node *
  101. allocate_bitmap_node(struct super_block *p_s_sb) {
  102.   struct reiserfs_bitmap_node *bn ;
  103.   static int id = 0 ;
  104.   bn = reiserfs_kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS, p_s_sb) ;
  105.   if (!bn) {
  106.     return NULL ;
  107.   }
  108.   bn->data = reiserfs_kmalloc(p_s_sb->s_blocksize, GFP_NOFS, p_s_sb) ;
  109.   if (!bn->data) {
  110.     reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
  111.     return NULL ;
  112.   }
  113.   bn->id = id++ ;
  114.   memset(bn->data, 0, p_s_sb->s_blocksize) ;
  115.   INIT_LIST_HEAD(&bn->list) ;
  116.   return bn ;
  117. }
  118. static struct reiserfs_bitmap_node *
  119. get_bitmap_node(struct super_block *p_s_sb) {
  120.   struct reiserfs_bitmap_node *bn = NULL;
  121.   struct list_head *entry = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
  122.   SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes++ ;
  123. repeat:
  124.   if(entry != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
  125.     bn = list_entry(entry, struct reiserfs_bitmap_node, list) ;
  126.     list_del(entry) ;
  127.     memset(bn->data, 0, p_s_sb->s_blocksize) ;
  128.     SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
  129.     return bn ;
  130.   }
  131.   bn = allocate_bitmap_node(p_s_sb) ;
  132.   if (!bn) {
  133.     yield();
  134.     goto repeat ;
  135.   }
  136.   return bn ;
  137. }
  138. static inline void free_bitmap_node(struct super_block *p_s_sb,
  139.                                     struct reiserfs_bitmap_node *bn) {
  140.   SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes-- ;
  141.   if (SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
  142.     reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
  143.     reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
  144.   } else {
  145.     list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
  146.     SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
  147.   }
  148. }
  149. static void allocate_bitmap_nodes(struct super_block *p_s_sb) {
  150.   int i ;
  151.   struct reiserfs_bitmap_node *bn = NULL ;
  152.   for (i = 0 ; i < REISERFS_MIN_BITMAP_NODES ; i++) {
  153.     bn = allocate_bitmap_node(p_s_sb) ;
  154.     if (bn) {
  155.       list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
  156.       SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
  157.     } else {
  158.       break ; // this is ok, we'll try again when more are needed 
  159.     }
  160.   }
  161. }
  162. static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
  163.                                   struct reiserfs_list_bitmap *jb) {
  164.   int bmap_nr = block / (p_s_sb->s_blocksize << 3) ;
  165.   int bit_nr = block % (p_s_sb->s_blocksize << 3) ;
  166.   if (!jb->bitmaps[bmap_nr]) {
  167.     jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb) ;
  168.   }
  169.   set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data) ;
  170.   return 0 ;
  171. }
  172. static void cleanup_bitmap_list(struct super_block *p_s_sb,
  173.                                 struct reiserfs_list_bitmap *jb) {
  174.   int i;
  175.   for (i = 0 ; i < SB_BMAP_NR(p_s_sb) ; i++) {
  176.     if (jb->bitmaps[i]) {
  177.       free_bitmap_node(p_s_sb, jb->bitmaps[i]) ;
  178.       jb->bitmaps[i] = NULL ;
  179.     }
  180.   }
  181. }
  182. /*
  183. ** only call this on FS unmount.
  184. */
  185. static int free_list_bitmaps(struct super_block *p_s_sb,
  186.                              struct reiserfs_list_bitmap *jb_array) {
  187.   int i ;
  188.   struct reiserfs_list_bitmap *jb ;
  189.   for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
  190.     jb = jb_array + i ;
  191.     jb->journal_list = NULL ;
  192.     cleanup_bitmap_list(p_s_sb, jb) ;
  193.     vfree(jb->bitmaps) ;
  194.     jb->bitmaps = NULL ;
  195.   }
  196.   return 0;
  197. }
  198. static int free_bitmap_nodes(struct super_block *p_s_sb) {
  199.   struct list_head *next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
  200.   struct reiserfs_bitmap_node *bn ;
  201.   while(next != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
  202.     bn = list_entry(next, struct reiserfs_bitmap_node, list) ;
  203.     list_del(next) ;
  204.     reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
  205.     reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
  206.     next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
  207.     SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
  208.   }
  209.   return 0 ;
  210. }
  211. /*
  212. ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps. 
  213. ** jb_array is the array to be filled in.
  214. */
  215. int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
  216.                                    struct reiserfs_list_bitmap *jb_array,
  217.    int bmap_nr) {
  218.   int i ;
  219.   int failed = 0 ;
  220.   struct reiserfs_list_bitmap *jb ;
  221.   int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *) ;
  222.   for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
  223.     jb = jb_array + i ;
  224.     jb->journal_list = NULL ;
  225.     jb->bitmaps = vmalloc( mem ) ;
  226.     if (!jb->bitmaps) {
  227.       reiserfs_warning("clm-2000, unable to allocate bitmaps for journal listsn") ;
  228.       failed = 1;   
  229.       break ;
  230.     }
  231.     memset(jb->bitmaps, 0, mem) ;
  232.   }
  233.   if (failed) {
  234.     free_list_bitmaps(p_s_sb, jb_array) ;
  235.     return -1 ;
  236.   }
  237.   return 0 ;
  238. }
  239. /*
  240. ** find an available list bitmap.  If you can't find one, flush a commit list 
  241. ** and try again
  242. */
  243. static struct reiserfs_list_bitmap *
  244. get_list_bitmap(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
  245.   int i,j ; 
  246.   struct reiserfs_list_bitmap *jb = NULL ;
  247.   for (j = 0 ; j < (JOURNAL_NUM_BITMAPS * 3) ; j++) {
  248.     i = SB_JOURNAL(p_s_sb)->j_list_bitmap_index ;
  249.     SB_JOURNAL(p_s_sb)->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS ;
  250.     jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
  251.     if (SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
  252.       flush_commit_list(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list, 1) ;
  253.       if (!SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
  254. break ;
  255.       }
  256.     } else {
  257.       break ;
  258.     }
  259.   }
  260.   if (jb->journal_list) { /* double check to make sure if flushed correctly */
  261.     return NULL ;
  262.   }
  263.   jb->journal_list = jl ;
  264.   return jb ;
  265. }
  266. /* 
  267. ** allocates a new chunk of X nodes, and links them all together as a list.
  268. ** Uses the cnode->next and cnode->prev pointers
  269. ** returns NULL on failure
  270. */
  271. static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes) {
  272.   struct reiserfs_journal_cnode *head ;
  273.   int i ;
  274.   if (num_cnodes <= 0) {
  275.     return NULL ;
  276.   }
  277.   head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
  278.   if (!head) {
  279.     return NULL ;
  280.   }
  281.   memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
  282.   head[0].prev = NULL ;
  283.   head[0].next = head + 1 ;
  284.   for (i = 1 ; i < num_cnodes; i++) {
  285.     head[i].prev = head + (i - 1) ;
  286.     head[i].next = head + (i + 1) ; /* if last one, overwrite it after the if */
  287.   }
  288.   head[num_cnodes -1].next = NULL ;
  289.   return head ;
  290. }
  291. /*
  292. ** pulls a cnode off the free list, or returns NULL on failure 
  293. */
  294. static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb) {
  295.   struct reiserfs_journal_cnode *cn ;
  296.   reiserfs_check_lock_depth("get_cnode") ;
  297.   if (SB_JOURNAL(p_s_sb)->j_cnode_free <= 0) {
  298.     return NULL ;
  299.   }
  300.   SB_JOURNAL(p_s_sb)->j_cnode_used++ ;
  301.   SB_JOURNAL(p_s_sb)->j_cnode_free-- ;
  302.   cn = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
  303.   if (!cn) {
  304.     return cn ;
  305.   }
  306.   if (cn->next) {
  307.     cn->next->prev = NULL ;
  308.   }
  309.   SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn->next ;
  310.   memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ;
  311.   return cn ;
  312. }
  313. /*
  314. ** returns a cnode to the free list 
  315. */
  316. static void free_cnode(struct super_block *p_s_sb, struct reiserfs_journal_cnode *cn) {
  317.   reiserfs_check_lock_depth("free_cnode") ;
  318.   SB_JOURNAL(p_s_sb)->j_cnode_used-- ;
  319.   SB_JOURNAL(p_s_sb)->j_cnode_free++ ;
  320.   /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
  321.   cn->next = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
  322.   if (SB_JOURNAL(p_s_sb)->j_cnode_free_list) {
  323.     SB_JOURNAL(p_s_sb)->j_cnode_free_list->prev = cn ;
  324.   }
  325.   cn->prev = NULL ; /* not needed with the memset, but I might kill the memset, and forget to do this */
  326.   SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn ;
  327. }
  328. static int clear_prepared_bits(struct buffer_head *bh) {
  329.   clear_bit(BH_JPrepared, &bh->b_state) ;
  330.   return 0 ;
  331. }
  332. /* buffer is in current transaction */
  333. inline int buffer_journaled(const struct buffer_head *bh) {
  334.   if (bh)
  335.     return test_bit(BH_JDirty, &((struct buffer_head *)bh)->b_state) ;
  336.   else
  337.     return 0 ;
  338. }
  339. /* disk block was taken off free list before being in a finished transation, or written to disk
  340. ** journal_new blocks can be reused immediately, for any purpose
  341. */ 
  342. inline int buffer_journal_new(const struct buffer_head *bh) {
  343.   if (bh) 
  344.     return test_bit(BH_JNew, &((struct buffer_head *)bh)->b_state) ;
  345.   else
  346.     return 0 ;
  347. }
  348. inline int mark_buffer_journal_new(struct buffer_head *bh) {
  349.   if (bh) {
  350.     set_bit(BH_JNew, &bh->b_state) ;
  351.   }
  352.   return 0 ;
  353. }
  354. inline int mark_buffer_not_journaled(struct buffer_head *bh) {
  355.   if (bh) 
  356.     clear_bit(BH_JDirty, &bh->b_state) ;
  357.   return 0 ;
  358. }
  359. /* utility function to force a BUG if it is called without the big
  360. ** kernel lock held.  caller is the string printed just before calling BUG()
  361. */
  362. void reiserfs_check_lock_depth(char *caller) {
  363. #ifdef CONFIG_SMP
  364.   if (current->lock_depth < 0) {
  365.     printk("%s called without kernel lock heldn", caller) ;
  366.     show_reiserfs_locks() ;
  367.     BUG() ;
  368.   }
  369. #else
  370.   ;
  371. #endif
  372. }
  373. /* return a cnode with same dev, block number and size in table, or null if not found */
  374. static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct reiserfs_journal_cnode **table,
  375.                                       kdev_t dev,long bl,int size) {
  376.   struct reiserfs_journal_cnode *cn ;
  377.   cn = journal_hash(table, dev, bl) ;
  378.   while(cn) {
  379.     if ((cn->blocknr == bl) && (cn->dev == dev))
  380.       return cn ;
  381.     cn = cn->hnext ;
  382.   }
  383.   return (struct reiserfs_journal_cnode *)0 ;
  384. }
  385. /* returns a cnode with same size, block number and dev as bh in the current transaction hash.  NULL if not found */
  386. static inline struct reiserfs_journal_cnode *get_journal_hash(struct super_block *p_s_sb, struct buffer_head *bh) {
  387.   struct reiserfs_journal_cnode *cn ;
  388.   if (bh) {
  389.     cn =  get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_hash_table, bh->b_dev, bh->b_blocknr, bh->b_size) ;
  390.   }
  391.   else {
  392.     return (struct reiserfs_journal_cnode *)0 ;
  393.   }
  394.   return cn ;
  395. }
  396. /* once upon a time, the journal would deadlock.  a lot.  Now, when
  397. ** CONFIG_REISERFS_CHECK is defined, anytime someone enters a
  398. ** transaction, it pushes itself into this ugly static list, and pops
  399. ** itself off before calling journal_end.  I made a SysRq key to dump
  400. ** the list, and tell me what the writers are when I'm deadlocked.  */
  401. /* are you depending on the compiler
  402.                                    to optimize this function away
  403.                                    everywhere it is called? It is not
  404.                                    obvious how this works, but I
  405.                                    suppose debugging code need not be
  406.                                    clear.  -Hans */
  407. static char *journal_writers[512] ;
  408. int push_journal_writer(char *s) {
  409. #ifdef CONFIG_REISERFS_CHECK
  410.   int i ;
  411.   for (i = 0 ; i < 512 ; i++) {
  412.     if (!journal_writers[i]) {
  413.       journal_writers[i] = s ;
  414.       return i ;
  415.     }
  416.   }
  417.   return -1 ;
  418. #else
  419.   return 0 ;
  420. #endif
  421. }
  422. int pop_journal_writer(int index) {
  423. #ifdef CONFIG_REISERFS_CHECK
  424.   if (index >= 0) {
  425.     journal_writers[index] = NULL ;
  426.   }
  427. #endif
  428.   return 0 ;
  429. }
  430. int dump_journal_writers(void) {
  431.   int i ;
  432.   for (i = 0 ; i < 512 ; i++) {
  433.     if (journal_writers[i]) {
  434.       printk("%d: %sn", i, journal_writers[i]) ;
  435.     }
  436.   }
  437.   return 0 ;
  438. }
  439. /*
  440. ** this actually means 'can this block be reallocated yet?'.  If you set search_all, a block can only be allocated
  441. ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
  442. ** being overwritten by a replay after crashing.
  443. **
  444. ** If you don't set search_all, a block can only be allocated if it is not in the current transaction.  Since deleting
  445. ** a block removes it from the current transaction, this case should never happen.  If you don't set search_all, make
  446. ** sure you never write the block without logging it.
  447. **
  448. ** next_zero_bit is a suggestion about the next block to try for find_forward.
  449. ** when bl is rejected because it is set in a journal list bitmap, we search
  450. ** for the next zero bit in the bitmap that rejected bl.  Then, we return that
  451. ** through next_zero_bit for find_forward to try.
  452. **
  453. ** Just because we return something in next_zero_bit does not mean we won't
  454. ** reject it on the next call to reiserfs_in_journal
  455. **
  456. */
  457. int reiserfs_in_journal(struct super_block *p_s_sb, kdev_t dev, 
  458.                         int bmap_nr, int bit_nr, int size, int search_all, 
  459. unsigned int *next_zero_bit) {
  460.   struct reiserfs_journal_cnode *cn ;
  461.   struct reiserfs_list_bitmap *jb ;
  462.   int i ;
  463.   unsigned long bl;
  464.   *next_zero_bit = 0 ; /* always start this at zero. */
  465.   /* we aren't logging all blocks are safe for reuse */
  466.   if (reiserfs_dont_log(p_s_sb)) {
  467.     return 0 ;
  468.   }
  469.   PROC_INFO_INC( p_s_sb, journal.in_journal );
  470.   /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
  471.   ** if we crash before the transaction that freed it commits,  this transaction won't
  472.   ** have committed either, and the block will never be written
  473.   */
  474.   if (search_all) {
  475.     for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
  476.       PROC_INFO_INC( p_s_sb, journal.in_journal_bitmap );
  477.       jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
  478.       if (jb->journal_list && jb->bitmaps[bmap_nr] &&
  479.           test_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data)) {
  480. *next_zero_bit = find_next_zero_bit((unsigned long *)
  481.                              (jb->bitmaps[bmap_nr]->data),
  482.                              p_s_sb->s_blocksize << 3, bit_nr+1) ; 
  483. return 1 ;
  484.       }
  485.     }
  486.   }
  487.   bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
  488.   /* is it in any old transactions? */
  489.   if (search_all && (cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_list_hash_table, dev,bl,size))) {
  490.     return 1; 
  491.   }
  492.   /* is it in the current transaction.  This should never happen */
  493.   if ((cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_hash_table, dev,bl,size))) {
  494.     return 1; 
  495.   }
  496.   PROC_INFO_INC( p_s_sb, journal.in_journal_reusable );
  497.   /* safe for reuse */
  498.   return 0 ;
  499. }
  500. /* insert cn into table
  501. */
  502. inline void insert_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_cnode *cn) {
  503.   struct reiserfs_journal_cnode *cn_orig ;
  504.   cn_orig = journal_hash(table, cn->dev, cn->blocknr) ;
  505.   cn->hnext = cn_orig ;
  506.   cn->hprev = NULL ;
  507.   if (cn_orig) {
  508.     cn_orig->hprev = cn ;
  509.   }
  510.   journal_hash(table, cn->dev, cn->blocknr) =  cn ;
  511. }
  512. /* lock the current transaction */
  513. inline static void lock_journal(struct super_block *p_s_sb) {
  514.   PROC_INFO_INC( p_s_sb, journal.lock_journal );
  515.   while(atomic_read(&(SB_JOURNAL(p_s_sb)->j_wlock)) > 0) {
  516.     PROC_INFO_INC( p_s_sb, journal.lock_journal_wait );
  517.     sleep_on(&(SB_JOURNAL(p_s_sb)->j_wait)) ;
  518.   }
  519.   atomic_set(&(SB_JOURNAL(p_s_sb)->j_wlock), 1) ;
  520. }
  521. /* unlock the current transaction */
  522. inline static void unlock_journal(struct super_block *p_s_sb) {
  523.   atomic_dec(&(SB_JOURNAL(p_s_sb)->j_wlock)) ;
  524.   wake_up(&(SB_JOURNAL(p_s_sb)->j_wait)) ;
  525. }
  526. /*
  527. ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
  528. ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
  529. ** transaction.
  530. */
  531. static void cleanup_freed_for_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
  532.   struct reiserfs_list_bitmap *jb = jl->j_list_bitmap ;
  533.   if (jb) {
  534.     cleanup_bitmap_list(p_s_sb, jb) ;
  535.   }
  536.   jl->j_list_bitmap->journal_list = NULL ;
  537.   jl->j_list_bitmap = NULL ;
  538. }
  539. /*
  540. ** if this journal list still has commit blocks unflushed, send them to disk.
  541. **
  542. ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
  543. ** Before the commit block can by written, every other log block must be safely on disk
  544. **
  545. */
  546. static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) {
  547.   int i, count ;
  548.   int index = 0 ;
  549.   int bn ;
  550.   int retry_count = 0 ;
  551.   int orig_commit_left = 0 ;
  552.   struct buffer_head *tbh = NULL ;
  553.   struct reiserfs_journal_list *other_jl ;
  554.   reiserfs_check_lock_depth("flush_commit_list") ;
  555.   if (atomic_read(&jl->j_older_commits_done)) {
  556.     return 0 ;
  557.   }
  558.   /* before we can put our commit blocks on disk, we have to make sure everyone older than
  559.   ** us is on disk too
  560.   */
  561.   if (jl->j_len <= 0) {
  562.     return 0 ;
  563.   }
  564.   if (flushall) {
  565.     /* we _must_ make sure the transactions are committed in order.  Start with the
  566.     ** index after this one, wrap all the way around 
  567.     */
  568.     index = (jl - SB_JOURNAL_LIST(s)) + 1 ;
  569.     for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
  570.       other_jl = SB_JOURNAL_LIST(s) + ( (index + i) % JOURNAL_LIST_COUNT) ;
  571.       if (other_jl && other_jl != jl && other_jl->j_len > 0 && other_jl->j_trans_id > 0 && 
  572.           other_jl->j_trans_id <= jl->j_trans_id && (atomic_read(&(jl->j_older_commits_done)) == 0)) {
  573.         flush_commit_list(s, other_jl, 0) ;
  574.       }
  575.     }
  576.   }
  577.   count = 0 ;
  578.   /* don't flush the commit list for the current transactoin */
  579.   if (jl == ((SB_JOURNAL_LIST(s) + SB_JOURNAL_LIST_INDEX(s)))) {
  580.     return 0 ;
  581.   }
  582.   /* make sure nobody is trying to flush this one at the same time */
  583.   if (atomic_read(&(jl->j_commit_flushing))) {
  584.     sleep_on(&(jl->j_commit_wait)) ;
  585.     if (flushall) {
  586.       atomic_set(&(jl->j_older_commits_done), 1) ;
  587.     }
  588.     return 0 ;
  589.   }
  590.   
  591.   /* this commit is done, exit */
  592.   if (atomic_read(&(jl->j_commit_left)) <= 0) {
  593.     if (flushall) {
  594.       atomic_set(&(jl->j_older_commits_done), 1) ;
  595.     }
  596.     return 0 ;
  597.   }
  598.   /* keeps others from flushing while we are flushing */
  599.   atomic_set(&(jl->j_commit_flushing), 1) ; 
  600.   if (jl->j_len > JOURNAL_TRANS_MAX) {
  601.     reiserfs_panic(s, "journal-512: flush_commit_list: length is %lu, list number %dn", jl->j_len, jl - SB_JOURNAL_LIST(s)) ;
  602.     return 0 ;
  603.   }
  604.   orig_commit_left = atomic_read(&(jl->j_commit_left)) ; 
  605.   /* start by checking all the commit blocks in this transaction.  
  606.   ** Add anyone not on disk into tbh.  Stop checking once commit_left <= 1, because that means we
  607.   ** only have the commit block left 
  608.   */
  609. retry:
  610.   count = 0 ;
  611.   for (i = 0 ; atomic_read(&(jl->j_commit_left)) > 1 && i < (jl->j_len + 1) ; i++) {  /* everything but commit_bh */
  612.     bn = reiserfs_get_journal_block(s) + (jl->j_start+i) % JOURNAL_BLOCK_COUNT;
  613.     tbh = sb_get_hash_table(s, bn) ;
  614. /* kill this sanity check */
  615. if (count > (orig_commit_left + 2)) {
  616. reiserfs_panic(s, "journal-539: flush_commit_list: BAD count(%d) > orig_commit_left(%d)!n", count, orig_commit_left) ;
  617. }
  618.     if (tbh) {
  619.       if (buffer_locked(tbh)) { /* wait on it, redo it just to make sure */
  620. wait_on_buffer(tbh) ;
  621. if (!buffer_uptodate(tbh)) {
  622.   reiserfs_panic(s, "journal-584, buffer write failedn") ;
  623. }
  624.       } 
  625.       if (buffer_dirty(tbh)) {
  626. printk("journal-569: flush_commit_list, block already dirty!n") ;
  627.       } else {
  628. mark_buffer_dirty(tbh) ;
  629.       }
  630.       ll_rw_block(WRITE, 1, &tbh) ;
  631.       count++ ;
  632.       put_bh(tbh) ; /* once for our get_hash */
  633.     } 
  634.   }
  635.   /* wait on everyone in tbh before writing commit block*/
  636.   if (count > 0) {
  637.     for (i = 0 ; atomic_read(&(jl->j_commit_left)) > 1 && 
  638.                  i < (jl->j_len + 1) ; i++) {  /* everything but commit_bh */
  639.       bn = reiserfs_get_journal_block(s) + (jl->j_start + i) % JOURNAL_BLOCK_COUNT  ;
  640.       tbh = sb_get_hash_table(s, bn) ;
  641.       wait_on_buffer(tbh) ;
  642.       if (!buffer_uptodate(tbh)) {
  643. reiserfs_panic(s, "journal-601, buffer write failedn") ;
  644.       }
  645.       put_bh(tbh) ; /* once for our get_hash */
  646.       bforget(tbh) ;    /* once due to original getblk in do_journal_end */
  647.       atomic_dec(&(jl->j_commit_left)) ;
  648.     }
  649.   }
  650.   if (atomic_read(&(jl->j_commit_left)) != 1) { /* just the commit_bh left, flush it without calling getblk for everyone */
  651.     if (retry_count < 2) {
  652.       printk("journal-582: flush_commit_list, not all log blocks on disk yet, trying againn") ;
  653.       retry_count++ ;
  654.       goto retry;
  655.     }
  656.     reiserfs_panic(s, "journal-563: flush_commit_list: BAD, j_commit_left is %u, should be 1n", 
  657.    atomic_read(&(jl->j_commit_left)));
  658.   }
  659.   mark_buffer_dirty(jl->j_commit_bh) ;
  660.   ll_rw_block(WRITE, 1, &(jl->j_commit_bh)) ;
  661.   wait_on_buffer(jl->j_commit_bh) ;
  662.   if (!buffer_uptodate(jl->j_commit_bh)) {
  663.     reiserfs_panic(s, "journal-615: buffer write failedn") ;
  664.   }
  665.   atomic_dec(&(jl->j_commit_left)) ;
  666.   bforget(jl->j_commit_bh) ;
  667.   /* now, every commit block is on the disk.  It is safe to allow blocks freed during this transaction to be reallocated */
  668.   cleanup_freed_for_journal_list(s, jl) ;
  669.   if (flushall) {
  670.     atomic_set(&(jl->j_older_commits_done), 1) ;
  671.   }
  672.   atomic_set(&(jl->j_commit_flushing), 0) ;
  673.   wake_up(&(jl->j_commit_wait)) ;
  674.   s->s_dirt = 1 ;
  675.   return 0 ;
  676. }
  677. /*
  678. ** flush_journal_list frequently needs to find a newer transaction for a given block.  This does that, or 
  679. ** returns NULL if it can't find anything 
  680. */
  681. static struct reiserfs_journal_list *find_newer_jl_for_cn(struct reiserfs_journal_cnode *cn) {
  682.   kdev_t dev = cn->dev;
  683.   unsigned long blocknr = cn->blocknr ;
  684.   cn = cn->hprev ;
  685.   while(cn) {
  686.     if (cn->dev == dev && cn->blocknr == blocknr && cn->jlist) {
  687.       return cn->jlist ;
  688.     }
  689.     cn = cn->hprev ;
  690.   }
  691.   return NULL ;
  692. }
  693. /*
  694. ** once all the real blocks have been flushed, it is safe to remove them from the
  695. ** journal list for this transaction.  Aside from freeing the cnode, this also allows the
  696. ** block to be reallocated for data blocks if it had been deleted.
  697. */
  698. static void remove_all_from_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, int debug) {
  699.   struct buffer_head fake_bh ;
  700.   struct reiserfs_journal_cnode *cn, *last ;
  701.   cn = jl->j_realblock ;
  702.   /* which is better, to lock once around the whole loop, or
  703.   ** to lock for each call to remove_from_journal_list?
  704.   */
  705.   while(cn) {
  706.     if (cn->blocknr != 0) {
  707.       if (debug) {
  708.         printk("block %lu, bh is %d, state %ldn", cn->blocknr, cn->bh ? 1: 0, 
  709.         cn->state) ;
  710.       }
  711.       fake_bh.b_blocknr = cn->blocknr ;
  712.       fake_bh.b_dev = cn->dev ;
  713.       cn->state = 0 ;
  714.       remove_from_journal_list(p_s_sb, jl, &fake_bh, 1) ;
  715.     }
  716.     last = cn ;
  717.     cn = cn->next ;
  718.     free_cnode(p_s_sb, last) ;
  719.   }
  720.   jl->j_realblock = NULL ;
  721. }
  722. /*
  723. ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
  724. ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
  725. ** releasing blocks in this transaction for reuse as data blocks.
  726. ** called by flush_journal_list, before it calls remove_all_from_journal_list
  727. **
  728. */
  729. static int _update_journal_header_block(struct super_block *p_s_sb, unsigned long offset, unsigned long trans_id) {
  730.   struct reiserfs_journal_header *jh ;
  731.   if (trans_id >= SB_JOURNAL(p_s_sb)->j_last_flush_trans_id) {
  732.     if (buffer_locked((SB_JOURNAL(p_s_sb)->j_header_bh)))  {
  733.       wait_on_buffer((SB_JOURNAL(p_s_sb)->j_header_bh)) ;
  734.       if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
  735.         reiserfs_panic(p_s_sb, "journal-699: buffer write failedn") ;
  736.       }
  737.     }
  738.     SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
  739.     SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = offset ;
  740.     jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
  741.     jh->j_last_flush_trans_id = cpu_to_le32(trans_id) ;
  742.     jh->j_first_unflushed_offset = cpu_to_le32(offset) ;
  743.     jh->j_mount_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_mount_id) ;
  744.     set_bit(BH_Dirty, &(SB_JOURNAL(p_s_sb)->j_header_bh->b_state)) ;
  745.     ll_rw_block(WRITE, 1, &(SB_JOURNAL(p_s_sb)->j_header_bh)) ;
  746.     wait_on_buffer((SB_JOURNAL(p_s_sb)->j_header_bh)) ; 
  747.     if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
  748.       printk( "reiserfs: journal-837: IO error during journal replayn" );
  749.       return -EIO ;
  750.     }
  751.   }
  752.   return 0 ;
  753. }
  754. static int update_journal_header_block(struct super_block *p_s_sb, 
  755.                                        unsigned long offset, 
  756.        unsigned long trans_id) {
  757.     if (_update_journal_header_block(p_s_sb, offset, trans_id)) {
  758. reiserfs_panic(p_s_sb, "journal-712: buffer write failedn") ;
  759.     }
  760.     return 0 ;
  761. }
  762. /* 
  763. ** flush any and all journal lists older than you are 
  764. ** can only be called from flush_journal_list
  765. */
  766. static int flush_older_journal_lists(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, unsigned long trans_id) {
  767.   int i, index ;
  768.   struct reiserfs_journal_list *other_jl ;
  769.   index = jl - SB_JOURNAL_LIST(p_s_sb) ;
  770.   for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
  771.     other_jl = SB_JOURNAL_LIST(p_s_sb) + ((index + i) % JOURNAL_LIST_COUNT) ;
  772.     if (other_jl && other_jl->j_len > 0 && 
  773.         other_jl->j_trans_id > 0 && 
  774. other_jl->j_trans_id < trans_id && 
  775.         other_jl != jl) {
  776.       /* do not flush all */
  777.       flush_journal_list(p_s_sb, other_jl, 0) ; 
  778.     }
  779.   }
  780.   return 0 ;
  781. }
  782. static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate) {
  783.     if (buffer_journaled(bh)) {
  784.         reiserfs_warning("clm-2084: pinned buffer %lu:%s sent to diskn",
  785.                  bh->b_blocknr, kdevname(bh->b_dev)) ;
  786.     }
  787.     mark_buffer_uptodate(bh, uptodate) ;
  788.     unlock_buffer(bh) ;
  789.     put_bh(bh) ;
  790. }
  791. static void submit_logged_buffer(struct buffer_head *bh) {
  792.     lock_buffer(bh) ;
  793.     get_bh(bh) ;
  794.     bh->b_end_io = reiserfs_end_buffer_io_sync ;
  795.     mark_buffer_notjournal_new(bh) ;
  796.     clear_bit(BH_Dirty, &bh->b_state) ;
  797.     submit_bh(WRITE, bh) ;
  798. }
  799. /* flush a journal list, both commit and real blocks
  800. **
  801. ** always set flushall to 1, unless you are calling from inside
  802. ** flush_journal_list
  803. **
  804. ** IMPORTANT.  This can only be called while there are no journal writers, 
  805. ** and the journal is locked.  That means it can only be called from 
  806. ** do_journal_end, or by journal_release
  807. */
  808. static int flush_journal_list(struct super_block *s, 
  809.                               struct reiserfs_journal_list *jl, int flushall) {
  810.   struct reiserfs_journal_list *pjl ;
  811.   struct reiserfs_journal_cnode *cn, *last ;
  812.   int count ;
  813.   int was_jwait = 0 ;
  814.   int was_dirty = 0 ;
  815.   struct buffer_head *saved_bh ; 
  816.   unsigned long j_len_saved = jl->j_len ;
  817.   if (j_len_saved <= 0) {
  818.     return 0 ;
  819.   }
  820.   if (atomic_read(&SB_JOURNAL(s)->j_wcount) != 0) {
  821.     reiserfs_warning("clm-2048: flush_journal_list called with wcount %dn",
  822.                       atomic_read(&SB_JOURNAL(s)->j_wcount)) ;
  823.   }
  824.   /* if someone is getting the commit list, we must wait for them */
  825.   while (atomic_read(&(jl->j_commit_flushing))) { 
  826.     sleep_on(&(jl->j_commit_wait)) ;
  827.   }
  828.   /* if someone is flushing this list, we must wait for them */
  829.   while (atomic_read(&(jl->j_flushing))) {
  830.     sleep_on(&(jl->j_flush_wait)) ;
  831.   }
  832.   /* this list is now ours, we can change anything we want */
  833.   atomic_set(&(jl->j_flushing), 1) ;
  834.   count = 0 ;
  835.   if (j_len_saved > JOURNAL_TRANS_MAX) {
  836.     reiserfs_panic(s, "journal-715: flush_journal_list, length is %lu, list number %dn", j_len_saved, jl - SB_JOURNAL_LIST(s)) ;
  837.     atomic_dec(&(jl->j_flushing)) ;
  838.     return 0 ;
  839.   }
  840.   /* if all the work is already done, get out of here */
  841.   if (atomic_read(&(jl->j_nonzerolen)) <= 0 && 
  842.       atomic_read(&(jl->j_commit_left)) <= 0) {
  843.     goto flush_older_and_return ;
  844.   } 
  845.   /* start by putting the commit list on disk.  This will also flush 
  846.   ** the commit lists of any olders transactions
  847.   */
  848.   flush_commit_list(s, jl, 1) ;
  849.   /* are we done now? */
  850.   if (atomic_read(&(jl->j_nonzerolen)) <= 0 && 
  851.       atomic_read(&(jl->j_commit_left)) <= 0) {
  852.     goto flush_older_and_return ;
  853.   }
  854.   /* loop through each cnode, see if we need to write it, 
  855.   ** or wait on a more recent transaction, or just ignore it 
  856.   */
  857.   if (atomic_read(&(SB_JOURNAL(s)->j_wcount)) != 0) {
  858.     reiserfs_panic(s, "journal-844: panic journal list is flushing, wcount is not 0n") ;
  859.   }
  860.   cn = jl->j_realblock ;
  861.   while(cn) {
  862.     was_jwait = 0 ;
  863.     was_dirty = 0 ;
  864.     saved_bh = NULL ;
  865.     /* blocknr of 0 is no longer in the hash, ignore it */
  866.     if (cn->blocknr == 0) {
  867.       goto free_cnode ;
  868.     }
  869.     pjl = find_newer_jl_for_cn(cn) ;
  870.     /* the order is important here.  We check pjl to make sure we
  871.     ** don't clear BH_JDirty_wait if we aren't the one writing this
  872.     ** block to disk
  873.     */
  874.     if (!pjl && cn->bh) {
  875.       saved_bh = cn->bh ;
  876.       /* we do this to make sure nobody releases the buffer while 
  877.       ** we are working with it 
  878.       */
  879.       get_bh(saved_bh) ;
  880.       if (buffer_journal_dirty(saved_bh)) {
  881.         was_jwait = 1 ;
  882. mark_buffer_notjournal_dirty(saved_bh) ;
  883.         /* undo the inc from journal_mark_dirty */
  884. put_bh(saved_bh) ;
  885.       }
  886.       if (can_dirty(cn)) {
  887.         was_dirty = 1 ;
  888.       }
  889.     }
  890.     /* if someone has this block in a newer transaction, just make
  891.     ** sure they are commited, and don't try writing it to disk
  892.     */
  893.     if (pjl) {
  894.       flush_commit_list(s, pjl, 1) ;
  895.       goto free_cnode ;
  896.     }
  897.     /* bh == NULL when the block got to disk on its own, OR, 
  898.     ** the block got freed in a future transaction 
  899.     */
  900.     if (saved_bh == NULL) {
  901.       goto free_cnode ;
  902.     }
  903.     /* this should never happen.  kupdate_one_transaction has this list
  904.     ** locked while it works, so we should never see a buffer here that
  905.     ** is not marked JDirty_wait
  906.     */
  907.     if ((!was_jwait) && !buffer_locked(saved_bh)) {
  908. printk("journal-813: BAD! buffer %lu %cdirty %cjwait, not in a newer tranasctionn", saved_bh->b_blocknr,
  909.         was_dirty ? ' ' : '!', was_jwait ? ' ' : '!') ;
  910.     }
  911.     /* kupdate_one_transaction waits on the buffers it is writing, so we
  912.     ** should never see locked buffers here
  913.     */
  914.     if (buffer_locked(saved_bh)) {
  915.       printk("clm-2083: locked buffer %lu in flush_journal_listn", 
  916.               saved_bh->b_blocknr) ;
  917.       wait_on_buffer(saved_bh) ;
  918.       if (!buffer_uptodate(saved_bh)) {
  919.         reiserfs_panic(s, "journal-923: buffer write failedn") ;
  920.       }
  921.     } 
  922.     if (was_dirty) { 
  923.       /* we inc again because saved_bh gets decremented at free_cnode */
  924.       get_bh(saved_bh) ;
  925.       set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
  926.       submit_logged_buffer(saved_bh) ;
  927.       count++ ;
  928.     } else {
  929.       printk("clm-2082: Unable to flush buffer %lu in flush_journal_listn",
  930.               saved_bh->b_blocknr) ;
  931.     }
  932. free_cnode:
  933.     last = cn ;
  934.     cn = cn->next ;
  935.     if (saved_bh) {
  936.       /* we incremented this to keep others from taking the buffer head away */
  937.       put_bh(saved_bh) ;
  938.       if (atomic_read(&(saved_bh->b_count)) < 0) {
  939.         printk("journal-945: saved_bh->b_count < 0") ;
  940.       }
  941.     }
  942.   }
  943.   if (count > 0) {
  944.     cn = jl->j_realblock ;
  945.     while(cn) {
  946.       if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
  947. if (!cn->bh) {
  948.   reiserfs_panic(s, "journal-1011: cn->bh is NULLn") ;
  949. }
  950. wait_on_buffer(cn->bh) ;
  951. if (!cn->bh) {
  952.   reiserfs_panic(s, "journal-1012: cn->bh is NULLn") ;
  953. }
  954. if (!buffer_uptodate(cn->bh)) {
  955.   reiserfs_panic(s, "journal-949: buffer write failedn") ;
  956. }
  957. refile_buffer(cn->bh) ;
  958.         brelse(cn->bh) ;
  959.       }
  960.       cn = cn->next ;
  961.     }
  962.   }
  963. flush_older_and_return:
  964.   /* before we can update the journal header block, we _must_ flush all 
  965.   ** real blocks from all older transactions to disk.  This is because
  966.   ** once the header block is updated, this transaction will not be
  967.   ** replayed after a crash
  968.   */
  969.   if (flushall) {
  970.     flush_older_journal_lists(s, jl, jl->j_trans_id) ;
  971.   } 
  972.   
  973.   /* before we can remove everything from the hash tables for this 
  974.   ** transaction, we must make sure it can never be replayed
  975.   **
  976.   ** since we are only called from do_journal_end, we know for sure there
  977.   ** are no allocations going on while we are flushing journal lists.  So,
  978.   ** we only need to update the journal header block for the last list
  979.   ** being flushed
  980.   */
  981.   if (flushall) {
  982.     update_journal_header_block(s, (jl->j_start + jl->j_len + 2) % JOURNAL_BLOCK_COUNT, jl->j_trans_id) ;
  983.   }
  984.   remove_all_from_journal_list(s, jl, 0) ;
  985.   jl->j_len = 0 ;
  986.   atomic_set(&(jl->j_nonzerolen), 0) ;
  987.   jl->j_start = 0 ;
  988.   jl->j_realblock = NULL ;
  989.   jl->j_commit_bh = NULL ;
  990.   jl->j_trans_id = 0 ;
  991.   atomic_dec(&(jl->j_flushing)) ;
  992.   wake_up(&(jl->j_flush_wait)) ;
  993.   return 0 ;
  994. static int kupdate_one_transaction(struct super_block *s,
  995.                                     struct reiserfs_journal_list *jl) 
  996. {
  997.     struct reiserfs_journal_list *pjl ; /* previous list for this cn */
  998.     struct reiserfs_journal_cnode *cn, *walk_cn ;
  999.     unsigned long blocknr ;
  1000.     int run = 0 ;
  1001.     int orig_trans_id = jl->j_trans_id ;
  1002.     struct buffer_head *saved_bh ; 
  1003.     int ret = 0 ;
  1004.     /* if someone is getting the commit list, we must wait for them */
  1005.     while (atomic_read(&(jl->j_commit_flushing))) {
  1006.         sleep_on(&(jl->j_commit_wait)) ;
  1007.     }
  1008.     /* if someone is flushing this list, we must wait for them */
  1009.     while (atomic_read(&(jl->j_flushing))) {
  1010.         sleep_on(&(jl->j_flush_wait)) ;
  1011.     }
  1012.     /* was it flushed while we slept? */
  1013.     if (jl->j_len <= 0 || jl->j_trans_id != orig_trans_id) {
  1014.         return 0 ;
  1015.     }
  1016.     /* this list is now ours, we can change anything we want */
  1017.     atomic_set(&(jl->j_flushing), 1) ;
  1018. loop_start:
  1019.     cn = jl->j_realblock ;
  1020.     while(cn) {
  1021.         saved_bh = NULL ;
  1022.         /* if the blocknr == 0, this has been cleared from the hash,
  1023.         ** skip it
  1024.         */
  1025.         if (cn->blocknr == 0) {
  1026.             goto next ;
  1027.         }
  1028.         /* look for a more recent transaction that logged this
  1029.         ** buffer.  Only the most recent transaction with a buffer in
  1030.         ** it is allowed to send that buffer to disk
  1031.         */
  1032.         pjl = find_newer_jl_for_cn(cn) ;
  1033.         if (run == 0 && !pjl && cn->bh && buffer_journal_dirty(cn->bh) &&
  1034.             can_dirty(cn)) 
  1035.         {
  1036.             if (!test_bit(BH_JPrepared, &cn->bh->b_state)) {
  1037.                 set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
  1038. submit_logged_buffer(cn->bh) ;
  1039.             } else {
  1040.                 /* someone else is using this buffer.  We can't 
  1041.                 ** send it to disk right now because they might
  1042.                 ** be changing/logging it.
  1043.                 */
  1044.                 ret = 1 ;
  1045.             }
  1046.         } else if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
  1047.             clear_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
  1048.             if (!pjl && cn->bh) {
  1049.                 wait_on_buffer(cn->bh) ;
  1050.             }
  1051.             /* check again, someone could have logged while we scheduled */
  1052.             pjl = find_newer_jl_for_cn(cn) ;
  1053.             /* before the JDirty_wait bit is set, the 
  1054.             ** buffer is added to the hash list.  So, if we are
  1055.             ** run in the middle of a do_journal_end, we will notice
  1056.             ** if this buffer was logged and added from the latest
  1057.             ** transaction.  In this case, we don't want to decrement
  1058.             ** b_count
  1059.             */
  1060.             if (!pjl && cn->bh && buffer_journal_dirty(cn->bh)) {
  1061.                 blocknr = cn->blocknr ;
  1062.                 walk_cn = cn ;
  1063.                 saved_bh= cn->bh ;
  1064.                 /* update all older transactions to show this block
  1065.                 ** was flushed
  1066.                 */
  1067.                 mark_buffer_notjournal_dirty(cn->bh) ;
  1068.                 while(walk_cn) {
  1069.                     if (walk_cn->bh && walk_cn->blocknr == blocknr && 
  1070.                          walk_cn->dev == cn->dev) {
  1071.                         if (walk_cn->jlist) {
  1072.                             atomic_dec(&(walk_cn->jlist->j_nonzerolen)) ;
  1073.                         }
  1074.                         walk_cn->bh = NULL ;
  1075.                     }
  1076.                     walk_cn = walk_cn->hnext ;
  1077.                 }
  1078.                 if (atomic_read(&saved_bh->b_count) < 1) {
  1079.                     reiserfs_warning("clm-2081: bad count on %lun", 
  1080.                                       saved_bh->b_blocknr) ;
  1081.                 }
  1082.                 brelse(saved_bh) ;
  1083.             }
  1084.         }
  1085.         /*
  1086.         ** if the more recent transaction is committed to the log,
  1087.         ** this buffer can be considered flushed.  Decrement our
  1088.         ** counters to reflect one less buffer that needs writing.
  1089.         **
  1090.         ** note, this relies on all of the above code being
  1091.         ** schedule free once pjl comes back non-null.
  1092.         */
  1093.         if (pjl && cn->bh && atomic_read(&pjl->j_commit_left) == 0) {
  1094.             atomic_dec(&cn->jlist->j_nonzerolen) ;
  1095.             cn->bh = NULL ;
  1096.         } 
  1097. next:
  1098.         cn = cn->next ;
  1099.     }
  1100.     /* the first run through the loop sends all the dirty buffers to
  1101.     ** ll_rw_block.
  1102.     ** the second run through the loop does all the accounting
  1103.     */
  1104.     if (run++ == 0) {
  1105.         goto loop_start ;
  1106.     }
  1107.     atomic_set(&(jl->j_flushing), 0) ;
  1108.     wake_up(&(jl->j_flush_wait)) ;
  1109.     return ret ;
  1110. }
  1111. /* since we never give dirty buffers to bdflush/kupdate, we have to
  1112. ** flush them ourselves.  This runs through the journal lists, finds
  1113. ** old metadata in need of flushing and sends it to disk.
  1114. ** this does not end transactions, commit anything, or free
  1115. ** cnodes.
  1116. **
  1117. ** returns the highest transaction id that was flushed last time
  1118. */
  1119. static unsigned long reiserfs_journal_kupdate(struct super_block *s) {
  1120.     struct reiserfs_journal_list *jl ;
  1121.     int i ;
  1122.     int start ;
  1123.     time_t age ;
  1124.     int ret = 0 ;
  1125.     start = SB_JOURNAL_LIST_INDEX(s) ;
  1126.     /* safety check to prevent flush attempts during a mount */
  1127.     if (start < 0) {
  1128.         return 0 ;
  1129.     }
  1130.     i = (start + 1) % JOURNAL_LIST_COUNT ;
  1131.     while(i != start) {
  1132.         jl = SB_JOURNAL_LIST(s) + i  ;
  1133.         age = CURRENT_TIME - jl->j_timestamp ;
  1134.         if (jl->j_len > 0 && // age >= (JOURNAL_MAX_COMMIT_AGE * 2) && 
  1135.             atomic_read(&(jl->j_nonzerolen)) > 0 &&
  1136.             atomic_read(&(jl->j_commit_left)) == 0) {
  1137.             if (jl->j_trans_id == SB_JOURNAL(s)->j_trans_id) {
  1138.                 break ;
  1139.             }
  1140.             /* if ret was already 1, we want to preserve that */
  1141.             ret |= kupdate_one_transaction(s, jl) ;
  1142.         } 
  1143.         if (atomic_read(&(jl->j_nonzerolen)) > 0) {
  1144.             ret |= 1 ;
  1145.         }
  1146.         i = (i + 1) % JOURNAL_LIST_COUNT ;
  1147.     }
  1148.     return ret ;
  1149. }
  1150. /*
  1151. ** removes any nodes in table with name block and dev as bh.
  1152. ** only touchs the hnext and hprev pointers.
  1153. */
  1154. void remove_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_list *jl,struct buffer_head *bh,
  1155.                          int remove_freed){
  1156.   struct reiserfs_journal_cnode *cur ;
  1157.   struct reiserfs_journal_cnode **head ;
  1158.   if (!bh)
  1159.     return ;
  1160.   head= &(journal_hash(table, bh->b_dev, bh->b_blocknr)) ;
  1161.   if (!head) {
  1162.     return ;
  1163.   }
  1164.   cur = *head ;
  1165.   while(cur) {
  1166.     if (cur->blocknr == bh->b_blocknr && cur->dev == bh->b_dev && (jl == NULL || jl == cur->jlist) && 
  1167.         (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
  1168.       if (cur->hnext) {
  1169.         cur->hnext->hprev = cur->hprev ;
  1170.       }
  1171.       if (cur->hprev) {
  1172. cur->hprev->hnext = cur->hnext ;
  1173.       } else {
  1174. *head = cur->hnext ;
  1175.       }
  1176.       cur->blocknr = 0 ;
  1177.       cur->dev = 0 ;
  1178.       cur->state = 0 ;
  1179.       if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
  1180. atomic_dec(&(cur->jlist->j_nonzerolen)) ;
  1181.       cur->bh = NULL ;
  1182.       cur->jlist = NULL ;
  1183.     } 
  1184.     cur = cur->hnext ;
  1185.   }
  1186. }
  1187. static void free_journal_ram(struct super_block *p_s_sb) {
  1188.   vfree(SB_JOURNAL(p_s_sb)->j_cnode_free_orig) ;
  1189.   free_list_bitmaps(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap) ;
  1190.   free_bitmap_nodes(p_s_sb) ; /* must be after free_list_bitmaps */
  1191.   if (SB_JOURNAL(p_s_sb)->j_header_bh) {
  1192.     brelse(SB_JOURNAL(p_s_sb)->j_header_bh) ;
  1193.   }
  1194.   vfree(SB_JOURNAL(p_s_sb)) ;
  1195. }
  1196. /*
  1197. ** call on unmount.  Only set error to 1 if you haven't made your way out
  1198. ** of read_super() yet.  Any other caller must keep error at 0.
  1199. */
  1200. static int do_journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, int error) {
  1201.   struct reiserfs_transaction_handle myth ;
  1202.   /* we only want to flush out transactions if we were called with error == 0
  1203.   */
  1204.   if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
  1205.     /* end the current trans */
  1206.     do_journal_end(th, p_s_sb,10, FLUSH_ALL) ;
  1207.     /* make sure something gets logged to force our way into the flush code */
  1208.     journal_join(&myth, p_s_sb, 1) ;
  1209.     reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
  1210.     journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
  1211.     do_journal_end(&myth, p_s_sb,1, FLUSH_ALL) ;
  1212.   }
  1213.   /* we decrement before we wake up, because the commit thread dies off
  1214.   ** when it has been woken up and the count is <= 0
  1215.   */
  1216.   reiserfs_mounted_fs_count-- ;
  1217.   wake_up(&reiserfs_commit_thread_wait) ;
  1218.   sleep_on(&reiserfs_commit_thread_done) ;
  1219.   free_journal_ram(p_s_sb) ;
  1220.   return 0 ;
  1221. }
  1222. /*
  1223. ** call on unmount.  flush all journal trans, release all alloc'd ram
  1224. */
  1225. int journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
  1226.   return do_journal_release(th, p_s_sb, 0) ;
  1227. }
  1228. /*
  1229. ** only call from an error condition inside reiserfs_read_super!
  1230. */
  1231. int journal_release_error(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
  1232.   return do_journal_release(th, p_s_sb, 1) ;
  1233. }
  1234. /* compares description block with commit block.  returns 1 if they differ, 0 if they are the same */
  1235. static int journal_compare_desc_commit(struct super_block *p_s_sb, struct reiserfs_journal_desc *desc, 
  1236.                struct reiserfs_journal_commit *commit) {
  1237.   if (le32_to_cpu(commit->j_trans_id) != le32_to_cpu(desc->j_trans_id) || 
  1238.       le32_to_cpu(commit->j_len) != le32_to_cpu(desc->j_len) || 
  1239.       le32_to_cpu(commit->j_len) > JOURNAL_TRANS_MAX || 
  1240.       le32_to_cpu(commit->j_len) <= 0 
  1241.   ) {
  1242.     return 1 ;
  1243.   }
  1244.   return 0 ;
  1245. }
  1246. /* returns 0 if it did not find a description block  
  1247. ** returns -1 if it found a corrupt commit block
  1248. ** returns 1 if both desc and commit were valid 
  1249. */
  1250. static int journal_transaction_is_valid(struct super_block *p_s_sb, struct buffer_head *d_bh, unsigned long *oldest_invalid_trans_id, unsigned long *newest_mount_id) {
  1251.   struct reiserfs_journal_desc *desc ;
  1252.   struct reiserfs_journal_commit *commit ;
  1253.   struct buffer_head *c_bh ;
  1254.   unsigned long offset ;
  1255.   if (!d_bh)
  1256.       return 0 ;
  1257.   desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
  1258.   if (le32_to_cpu(desc->j_len) > 0 && !memcmp(desc->j_magic, JOURNAL_DESC_MAGIC, 8)) {
  1259.     if (oldest_invalid_trans_id && *oldest_invalid_trans_id && le32_to_cpu(desc->j_trans_id) > *oldest_invalid_trans_id) {
  1260.       reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-986: transaction "
  1261.               "is valid returning because trans_id %d is greater than "
  1262.       "oldest_invalid %lun", le32_to_cpu(desc->j_trans_id), 
  1263.        *oldest_invalid_trans_id);
  1264.       return 0 ;
  1265.     }
  1266.     if (newest_mount_id && *newest_mount_id > le32_to_cpu(desc->j_mount_id)) {
  1267.       reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1087: transaction "
  1268.                      "is valid returning because mount_id %d is less than "
  1269.      "newest_mount_id %lun", desc->j_mount_id, 
  1270.      *newest_mount_id) ;
  1271.       return -1 ;
  1272.     }
  1273.     offset = d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb) ;
  1274.     /* ok, we have a journal description block, lets see if the transaction was valid */
  1275.     c_bh = sb_bread(p_s_sb, reiserfs_get_journal_block(p_s_sb) + ((offset + le32_to_cpu(desc->j_len) + 1) % JOURNAL_BLOCK_COUNT)) ;
  1276.     if (!c_bh)
  1277.       return 0 ;
  1278.     commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
  1279.     if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
  1280.       reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, 
  1281.                      "journal_transaction_is_valid, commit offset %ld had bad "
  1282.      "time %d or length %dn", 
  1283.      c_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb),
  1284.      le32_to_cpu(commit->j_trans_id), 
  1285.      le32_to_cpu(commit->j_len));
  1286.       brelse(c_bh) ;
  1287.       if (oldest_invalid_trans_id)
  1288.         *oldest_invalid_trans_id = le32_to_cpu(desc->j_trans_id) ;
  1289. reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1004: "
  1290.                "transaction_is_valid setting oldest invalid trans_id "
  1291.        "to %dn", le32_to_cpu(desc->j_trans_id)) ;
  1292.       return -1; 
  1293.     }
  1294.     brelse(c_bh) ;
  1295.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1006: found valid "
  1296.                    "transaction start offset %lu, len %d id %dn", 
  1297.    d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb), 
  1298.    le32_to_cpu(desc->j_len), le32_to_cpu(desc->j_trans_id)) ;
  1299.     return 1 ;
  1300.   } else {
  1301.     return 0 ;
  1302.   }
  1303. }
  1304. static void brelse_array(struct buffer_head **heads, int num) {
  1305.   int i ;
  1306.   for (i = 0 ; i < num ; i++) {
  1307.     brelse(heads[i]) ;
  1308.   }
  1309. }
  1310. /*
  1311. ** given the start, and values for the oldest acceptable transactions,
  1312. ** this either reads in a replays a transaction, or returns because the transaction
  1313. ** is invalid, or too old.
  1314. */
  1315. static int journal_read_transaction(struct super_block *p_s_sb, unsigned long cur_dblock, unsigned long oldest_start, 
  1316.     unsigned long oldest_trans_id, unsigned long newest_mount_id) {
  1317.   struct reiserfs_journal_desc *desc ;
  1318.   struct reiserfs_journal_commit *commit ;
  1319.   unsigned long trans_id = 0 ;
  1320.   struct buffer_head *c_bh ;
  1321.   struct buffer_head *d_bh ;
  1322.   struct buffer_head **log_blocks = NULL ;
  1323.   struct buffer_head **real_blocks = NULL ;
  1324.   unsigned long trans_offset ;
  1325.   int i;
  1326.   d_bh = sb_bread(p_s_sb, cur_dblock) ;
  1327.   if (!d_bh)
  1328.     return 1 ;
  1329.   desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
  1330.   trans_offset = d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb) ;
  1331.   reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
  1332.                  "journal_read_transaction, offset %lu, len %d mount_id %dn", 
  1333.  d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb), 
  1334.  le32_to_cpu(desc->j_len), le32_to_cpu(desc->j_mount_id)) ;
  1335.   if (le32_to_cpu(desc->j_trans_id) < oldest_trans_id) {
  1336.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
  1337.                    "journal_read_trans skipping because %lu is too oldn", 
  1338.    cur_dblock - reiserfs_get_journal_block(p_s_sb)) ;
  1339.     brelse(d_bh) ;
  1340.     return 1 ;
  1341.   }
  1342.   if (le32_to_cpu(desc->j_mount_id) != newest_mount_id) {
  1343.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
  1344.                    "journal_read_trans skipping because %d is != "
  1345.    "newest_mount_id %lun", le32_to_cpu(desc->j_mount_id), 
  1346.     newest_mount_id) ;
  1347.     brelse(d_bh) ;
  1348.     return 1 ;
  1349.   }
  1350.   c_bh = sb_bread(p_s_sb, reiserfs_get_journal_block(p_s_sb) + ((trans_offset + le32_to_cpu(desc->j_len) + 1) % JOURNAL_BLOCK_COUNT)) ;
  1351.   if (!c_bh) {
  1352.     brelse(d_bh) ;
  1353.     return 1 ;
  1354.   }
  1355.   commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
  1356.   if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
  1357.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal_read_transaction, "
  1358.                    "commit offset %ld had bad time %d or length %dn", 
  1359.    c_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb), 
  1360.    le32_to_cpu(commit->j_trans_id), le32_to_cpu(commit->j_len));
  1361.     brelse(c_bh) ;
  1362.     brelse(d_bh) ;
  1363.     return 1; 
  1364.   }
  1365.   trans_id = le32_to_cpu(desc->j_trans_id) ;
  1366.   /* now we know we've got a good transaction, and it was inside the valid time ranges */
  1367.   log_blocks = reiserfs_kmalloc(le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
  1368.   real_blocks = reiserfs_kmalloc(le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
  1369.   if (!log_blocks  || !real_blocks) {
  1370.     brelse(c_bh) ;
  1371.     brelse(d_bh) ;
  1372.     reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1373.     reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1374.     reiserfs_warning("journal-1169: kmalloc failed, unable to mount FSn") ;
  1375.     return -1 ;
  1376.   }
  1377.   /* get all the buffer heads */
  1378.   for(i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
  1379.     log_blocks[i] = sb_getblk(p_s_sb, reiserfs_get_journal_block(p_s_sb) + (trans_offset + 1 + i) % JOURNAL_BLOCK_COUNT);
  1380.     if (i < JOURNAL_TRANS_HALF) {
  1381.       real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(desc->j_realblock[i])) ;
  1382.     } else {
  1383.       real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(commit->j_realblock[i - JOURNAL_TRANS_HALF])) ;
  1384.     }
  1385.     if (real_blocks[i]->b_blocknr >= reiserfs_get_journal_block(p_s_sb) &&
  1386.         real_blocks[i]->b_blocknr < (reiserfs_get_journal_block(p_s_sb)+JOURNAL_BLOCK_COUNT)) {
  1387.       reiserfs_warning("journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log blockn") ;
  1388.       brelse_array(log_blocks, i) ;
  1389.       brelse_array(real_blocks, i) ;
  1390.       brelse(c_bh) ;
  1391.       brelse(d_bh) ;
  1392.       reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1393.       reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1394.       return -1 ;
  1395.     }
  1396.   }
  1397.   /* read in the log blocks, memcpy to the corresponding real block */
  1398.   ll_rw_block(READ, le32_to_cpu(desc->j_len), log_blocks) ;
  1399.   for (i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
  1400.     wait_on_buffer(log_blocks[i]) ;
  1401.     if (!buffer_uptodate(log_blocks[i])) {
  1402.       reiserfs_warning("journal-1212: REPLAY FAILURE fsck required! buffer write failedn") ;
  1403.       brelse_array(log_blocks + i, le32_to_cpu(desc->j_len) - i) ;
  1404.       brelse_array(real_blocks, le32_to_cpu(desc->j_len)) ;
  1405.       brelse(c_bh) ;
  1406.       brelse(d_bh) ;
  1407.       reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1408.       reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1409.       return -1 ;
  1410.     }
  1411.     memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data, real_blocks[i]->b_size) ;
  1412.     mark_buffer_uptodate(real_blocks[i], 1) ;
  1413.     brelse(log_blocks[i]) ;
  1414.   }
  1415.   /* flush out the real blocks */
  1416.   for (i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
  1417.     set_bit(BH_Dirty, &(real_blocks[i]->b_state)) ;
  1418.     ll_rw_block(WRITE, 1, real_blocks + i) ;
  1419.   }
  1420.   for (i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
  1421.     wait_on_buffer(real_blocks[i]) ; 
  1422.     if (!buffer_uptodate(real_blocks[i])) {
  1423.       reiserfs_warning("journal-1226: REPLAY FAILURE, fsck required! buffer write failedn") ;
  1424.       brelse_array(real_blocks + i, le32_to_cpu(desc->j_len) - i) ;
  1425.       brelse(c_bh) ;
  1426.       brelse(d_bh) ;
  1427.       reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1428.       reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1429.       return -1 ;
  1430.     }
  1431.     brelse(real_blocks[i]) ;
  1432.   }
  1433.   cur_dblock = reiserfs_get_journal_block(p_s_sb) + ((trans_offset + le32_to_cpu(desc->j_len) + 2) % JOURNAL_BLOCK_COUNT) ;
  1434.   reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1095: setting journal "
  1435.                  "start to offset %ldn", 
  1436.  cur_dblock - reiserfs_get_journal_block(p_s_sb)) ;
  1437.   
  1438.   /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
  1439.   SB_JOURNAL(p_s_sb)->j_start = cur_dblock - reiserfs_get_journal_block(p_s_sb) ;
  1440.   SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
  1441.   SB_JOURNAL(p_s_sb)->j_trans_id = trans_id + 1;
  1442.   brelse(c_bh) ;
  1443.   brelse(d_bh) ;
  1444.   reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1445.   reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
  1446.   return 0 ;
  1447. }
  1448. /*
  1449. ** read and replay the log
  1450. ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
  1451. ** transaction.  This tests that before finding all the transactions in the log, whic makes normal mount times fast.
  1452. **
  1453. ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
  1454. **
  1455. ** On exit, it sets things up so the first transaction will work correctly.
  1456. */
  1457. struct buffer_head * reiserfs_breada (kdev_t dev, int block, int bufsize,
  1458.     unsigned int max_block)
  1459. {
  1460. struct buffer_head * bhlist[BUFNR];
  1461. unsigned int blocks = BUFNR;
  1462. struct buffer_head * bh;
  1463. int i, j;
  1464. bh = getblk (dev, block, bufsize);
  1465. if (buffer_uptodate (bh))
  1466. return (bh);   
  1467. if (block + BUFNR > max_block) {
  1468. blocks = max_block - block;
  1469. }
  1470. bhlist[0] = bh;
  1471. j = 1;
  1472. for (i = 1; i < blocks; i++) {
  1473. bh = getblk (dev, block + i, bufsize);
  1474. if (buffer_uptodate (bh)) {
  1475. brelse (bh);
  1476. break;
  1477. }
  1478. else bhlist[j++] = bh;
  1479. }
  1480. ll_rw_block (READ, j, bhlist);
  1481. for(i = 1; i < j; i++) 
  1482. brelse (bhlist[i]);
  1483. bh = bhlist[0];
  1484. wait_on_buffer (bh);
  1485. if (buffer_uptodate (bh))
  1486. return bh;
  1487. brelse (bh);
  1488. return NULL;
  1489. }
  1490. static int journal_read(struct super_block *p_s_sb) {
  1491.   struct reiserfs_journal_desc *desc ;
  1492.   unsigned long last_flush_trans_id = 0 ;
  1493.   unsigned long oldest_trans_id = 0;
  1494.   unsigned long oldest_invalid_trans_id = 0 ;
  1495.   time_t start ;
  1496.   unsigned long last_flush_start = 0;
  1497.   unsigned long oldest_start = 0;
  1498.   unsigned long cur_dblock = 0 ;
  1499.   unsigned long newest_mount_id = 9 ;
  1500.   struct buffer_head *d_bh ;
  1501.   struct reiserfs_journal_header *jh ;
  1502.   int valid_journal_header = 0 ;
  1503.   int replay_count = 0 ;
  1504.   int continue_replay = 1 ;
  1505.   int ret ;
  1506.   cur_dblock = reiserfs_get_journal_block(p_s_sb) ;
  1507.   printk("reiserfs: checking transaction log (device %s) ...n",
  1508.           kdevname(p_s_sb->s_dev)) ;
  1509.   start = CURRENT_TIME ;
  1510.   /* step 1, read in the journal header block.  Check the transaction it says 
  1511.   ** is the first unflushed, and if that transaction is not valid, 
  1512.   ** replay is done
  1513.   */
  1514.   SB_JOURNAL(p_s_sb)->j_header_bh = sb_bread(p_s_sb, 
  1515.                                           reiserfs_get_journal_block(p_s_sb) + 
  1516.   JOURNAL_BLOCK_COUNT) ;
  1517.   if (!SB_JOURNAL(p_s_sb)->j_header_bh) {
  1518.     return 1 ;
  1519.   }
  1520.   jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
  1521.   if (le32_to_cpu(jh->j_first_unflushed_offset) >= 0 && 
  1522.       le32_to_cpu(jh->j_first_unflushed_offset) < JOURNAL_BLOCK_COUNT &&
  1523.       le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
  1524.     last_flush_start = reiserfs_get_journal_block(p_s_sb) + 
  1525.                        le32_to_cpu(jh->j_first_unflushed_offset) ;
  1526.     last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
  1527.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1153: found in "
  1528.                    "header: first_unflushed_offset %d, last_flushed_trans_id "
  1529.    "%lun", le32_to_cpu(jh->j_first_unflushed_offset), 
  1530.    last_flush_trans_id) ;
  1531.     valid_journal_header = 1 ;
  1532.     /* now, we try to read the first unflushed offset.  If it is not valid, 
  1533.     ** there is nothing more we can do, and it makes no sense to read 
  1534.     ** through the whole log.
  1535.     */
  1536.     d_bh = sb_bread(p_s_sb, reiserfs_get_journal_block(p_s_sb) + le32_to_cpu(jh->j_first_unflushed_offset)) ;
  1537.     ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL) ;
  1538.     if (!ret) {
  1539.       continue_replay = 0 ;
  1540.     }
  1541.     brelse(d_bh) ;
  1542.   }
  1543.   if (continue_replay && is_read_only(p_s_sb->s_dev)) {
  1544.     printk("clm-2076: device is readonly, unable to replay logn") ;
  1545.     return -1 ;
  1546.   }
  1547.   if (continue_replay && (p_s_sb->s_flags & MS_RDONLY)) {
  1548.     printk("Warning, log replay starting on readonly filesystemn") ;    
  1549.   }
  1550.   /* ok, there are transactions that need to be replayed.  start with the first log block, find
  1551.   ** all the valid transactions, and pick out the oldest.
  1552.   */
  1553.   while(continue_replay && cur_dblock < (reiserfs_get_journal_block(p_s_sb) + JOURNAL_BLOCK_COUNT)) {
  1554.     d_bh = reiserfs_breada(p_s_sb->s_dev, cur_dblock, p_s_sb->s_blocksize,
  1555.    reiserfs_get_journal_block(p_s_sb) + JOURNAL_BLOCK_COUNT) ;
  1556.     ret = journal_transaction_is_valid(p_s_sb, d_bh, &oldest_invalid_trans_id, &newest_mount_id) ;
  1557.     if (ret == 1) {
  1558.       desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
  1559.       if (oldest_start == 0) { /* init all oldest_ values */
  1560.         oldest_trans_id = le32_to_cpu(desc->j_trans_id) ;
  1561. oldest_start = d_bh->b_blocknr ;
  1562. newest_mount_id = le32_to_cpu(desc->j_mount_id) ;
  1563. reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1179: Setting "
  1564.                "oldest_start to offset %lu, trans_id %lun", 
  1565.        oldest_start - reiserfs_get_journal_block(p_s_sb), 
  1566.        oldest_trans_id) ;
  1567.       } else if (oldest_trans_id > le32_to_cpu(desc->j_trans_id)) { 
  1568.         /* one we just read was older */
  1569.         oldest_trans_id = le32_to_cpu(desc->j_trans_id) ;
  1570. oldest_start = d_bh->b_blocknr ;
  1571. reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1180: Resetting "
  1572.                "oldest_start to offset %lu, trans_id %lun", 
  1573. oldest_start - reiserfs_get_journal_block(p_s_sb), 
  1574. oldest_trans_id) ;
  1575.       }
  1576.       if (newest_mount_id < le32_to_cpu(desc->j_mount_id)) {
  1577.         newest_mount_id = le32_to_cpu(desc->j_mount_id) ;
  1578. reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
  1579.               "newest_mount_id to %dn", le32_to_cpu(desc->j_mount_id));
  1580.       }
  1581.       cur_dblock += le32_to_cpu(desc->j_len) + 2 ;
  1582.     } 
  1583.     else {
  1584.       cur_dblock++ ;
  1585.     }
  1586.     brelse(d_bh) ;
  1587.   }
  1588.   /* step three, starting at the oldest transaction, replay */
  1589.   if (last_flush_start > 0) {
  1590.     oldest_start = last_flush_start ;
  1591.     oldest_trans_id = last_flush_trans_id + 1 ;
  1592.   } 
  1593.   cur_dblock = oldest_start ;
  1594.   if (oldest_trans_id)  {
  1595.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1206: Starting replay "
  1596.                    "from offset %lu, trans_id %lun", 
  1597.    cur_dblock - reiserfs_get_journal_block(p_s_sb), 
  1598.    oldest_trans_id) ;
  1599.   }
  1600.   replay_count = 0 ;
  1601.   while(continue_replay && oldest_trans_id > 0) {
  1602.     ret = journal_read_transaction(p_s_sb, cur_dblock, oldest_start, oldest_trans_id, newest_mount_id) ;
  1603.     if (ret < 0) {
  1604.       return ret ;
  1605.     } else if (ret != 0) {
  1606.       break ;
  1607.     }
  1608.     cur_dblock = reiserfs_get_journal_block(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start ;
  1609.     replay_count++ ;
  1610.    if (cur_dblock == oldest_start)
  1611.         break;
  1612.   }
  1613.   if (oldest_trans_id == 0) {
  1614.     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1225: No valid "
  1615.                    "transactions foundn") ;
  1616.   }
  1617.   /* j_start does not get set correctly if we don't replay any transactions.
  1618.   ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
  1619.   ** copy the trans_id from the header
  1620.   */
  1621.   if (valid_journal_header && replay_count == 0) { 
  1622.     SB_JOURNAL(p_s_sb)->j_start = le32_to_cpu(jh->j_first_unflushed_offset) ;
  1623.     SB_JOURNAL(p_s_sb)->j_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
  1624.     SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
  1625.     SB_JOURNAL(p_s_sb)->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
  1626.   } else {
  1627.     SB_JOURNAL(p_s_sb)->j_mount_id = newest_mount_id + 1 ;
  1628.   }
  1629.   reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
  1630.                  "newest_mount_id to %lun", SB_JOURNAL(p_s_sb)->j_mount_id) ;
  1631.   SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = SB_JOURNAL(p_s_sb)->j_start ; 
  1632.   if (replay_count > 0) {
  1633.     printk("reiserfs: replayed %d transactions in %lu secondsn", replay_count, 
  1634.     CURRENT_TIME - start) ;
  1635.   }
  1636.   if (!is_read_only(p_s_sb->s_dev) && 
  1637.        _update_journal_header_block(p_s_sb, SB_JOURNAL(p_s_sb)->j_start, 
  1638.                                    SB_JOURNAL(p_s_sb)->j_last_flush_trans_id))
  1639.   {
  1640.       /* replay failed, caller must call free_journal_ram and abort
  1641.       ** the mount
  1642.       */
  1643.       return -1 ;
  1644.   }
  1645.   return 0 ;
  1646. }
  1647. struct reiserfs_journal_commit_task {
  1648.   struct super_block *p_s_sb ;
  1649.   int jindex ;
  1650.   int wake_on_finish ; /* if this is one, we wake the task_done queue, if it
  1651.                        ** is zero, we free the whole struct on finish
  1652.        */
  1653.   struct reiserfs_journal_commit_task *self ;
  1654.   struct wait_queue *task_done ;
  1655.   struct tq_struct task ;
  1656. } ;
  1657. static void reiserfs_journal_commit_task_func(struct reiserfs_journal_commit_task *ct) {
  1658.   struct reiserfs_journal_list *jl ;
  1659.   jl = SB_JOURNAL_LIST(ct->p_s_sb) + ct->jindex ;
  1660.   flush_commit_list(ct->p_s_sb, SB_JOURNAL_LIST(ct->p_s_sb) + ct->jindex, 1) ; 
  1661.   if (jl->j_len > 0 && atomic_read(&(jl->j_nonzerolen)) > 0 &&
  1662.       atomic_read(&(jl->j_commit_left)) == 0) {
  1663.     kupdate_one_transaction(ct->p_s_sb, jl) ;
  1664.   }
  1665.   reiserfs_kfree(ct->self, sizeof(struct reiserfs_journal_commit_task), ct->p_s_sb) ;
  1666. }
  1667. static void setup_commit_task_arg(struct reiserfs_journal_commit_task *ct,
  1668.                                   struct super_block *p_s_sb, 
  1669.   int jindex) {
  1670.   if (!ct) {
  1671.     reiserfs_panic(NULL, "journal-1360: setup_commit_task_arg called with NULL structn") ;
  1672.   }
  1673.   ct->p_s_sb = p_s_sb ;
  1674.   ct->jindex = jindex ;
  1675.   ct->task_done = NULL ;
  1676.   INIT_LIST_HEAD(&ct->task.list) ;
  1677.   ct->task.sync = 0 ;
  1678.   ct->task.routine = (void *)(void *)reiserfs_journal_commit_task_func ; 
  1679.   ct->self = ct ;
  1680.   ct->task.data = (void *)ct ;
  1681. }
  1682. static void commit_flush_async(struct super_block *p_s_sb, int jindex) {
  1683.   struct reiserfs_journal_commit_task *ct ;
  1684.   /* using GFP_NOFS, GFP_KERNEL could try to flush inodes, which will try
  1685.   ** to start/join a transaction, which will deadlock
  1686.   */
  1687.   ct = reiserfs_kmalloc(sizeof(struct reiserfs_journal_commit_task), GFP_NOFS, p_s_sb) ;
  1688.   if (ct) {
  1689.     setup_commit_task_arg(ct, p_s_sb, jindex) ;
  1690.     queue_task(&(ct->task), &reiserfs_commit_thread_tq);
  1691.     wake_up(&reiserfs_commit_thread_wait) ;
  1692.   } else {
  1693. #ifdef CONFIG_REISERFS_CHECK
  1694.     reiserfs_warning("journal-1540: kmalloc failed, doing sync commitn") ;
  1695. #endif
  1696.     flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1) ;
  1697.   }
  1698. }
  1699. /*
  1700. ** this is the commit thread.  It is started with kernel_thread on
  1701. ** FS mount, and journal_release() waits for it to exit.
  1702. **
  1703. ** It could do a periodic commit, but there is a lot code for that
  1704. ** elsewhere right now, and I only wanted to implement this little
  1705. ** piece for starters.
  1706. **
  1707. ** All we do here is sleep on the j_commit_thread_wait wait queue, and
  1708. ** then run the per filesystem commit task queue when we wakeup.
  1709. */
  1710. static int reiserfs_journal_commit_thread(void *nullp) {
  1711.   daemonize() ;
  1712.   spin_lock_irq(&current->sigmask_lock);
  1713.   sigfillset(&current->blocked);
  1714.   recalc_sigpending(current);
  1715.   spin_unlock_irq(&current->sigmask_lock);
  1716.   sprintf(current->comm, "kreiserfsd") ;
  1717.   lock_kernel() ;
  1718.   while(1) {
  1719.     while(TQ_ACTIVE(reiserfs_commit_thread_tq)) {
  1720.       run_task_queue(&reiserfs_commit_thread_tq) ;
  1721.     }
  1722.     /* if there aren't any more filesystems left, break */
  1723.     if (reiserfs_mounted_fs_count <= 0) {
  1724.       run_task_queue(&reiserfs_commit_thread_tq) ;
  1725.       break ;
  1726.     }
  1727.     wake_up(&reiserfs_commit_thread_done) ;
  1728.     interruptible_sleep_on_timeout(&reiserfs_commit_thread_wait, 5 * HZ) ;
  1729.   }
  1730.   unlock_kernel() ;
  1731.   wake_up(&reiserfs_commit_thread_done) ;
  1732.   return 0 ;
  1733. }
  1734. static void journal_list_init(struct super_block *p_s_sb) {
  1735.   int i ;
  1736.   for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
  1737.     init_waitqueue_head(&(SB_JOURNAL_LIST(p_s_sb)[i].j_commit_wait)) ;
  1738.     init_waitqueue_head(&(SB_JOURNAL_LIST(p_s_sb)[i].j_flush_wait)) ;
  1739.   }
  1740. }
  1741. /*
  1742. ** must be called once on fs mount.  calls journal_read for you
  1743. */
  1744. int journal_init(struct super_block *p_s_sb) {
  1745.   int num_cnodes = JOURNAL_BLOCK_COUNT * 2 ;
  1746.   if (sizeof(struct reiserfs_journal_commit) != 4096 ||
  1747.       sizeof(struct reiserfs_journal_desc) != 4096
  1748.      ) {
  1749.     printk("journal-1249: commit or desc struct not 4096 %Zd %Zdn", sizeof(struct reiserfs_journal_commit), 
  1750.         sizeof(struct reiserfs_journal_desc)) ;
  1751.     return 1 ;
  1752.   }
  1753.   /* sanity check to make sure they don't overflow the journal */
  1754.   if (JOURNAL_BLOCK_COUNT > reiserfs_get_journal_orig_size(p_s_sb)) {
  1755.     printk("journal-1393: current JOURNAL_BLOCK_COUNT (%d) is too big.  This FS was created with a journal size of %lu blocksn",
  1756.             JOURNAL_BLOCK_COUNT, reiserfs_get_journal_orig_size(p_s_sb)) ;
  1757.     return 1 ;
  1758.   }
  1759.   SB_JOURNAL(p_s_sb) = vmalloc(sizeof (struct reiserfs_journal)) ;
  1760.   if (!SB_JOURNAL(p_s_sb)) {
  1761.     printk("journal-1256: unable to get memory for journal structuren") ;
  1762.     return 1 ;
  1763.   }
  1764.   memset(SB_JOURNAL(p_s_sb), 0, sizeof(struct reiserfs_journal)) ;
  1765.   SB_JOURNAL(p_s_sb)->j_list_bitmap_index = 0 ;
  1766.   SB_JOURNAL_LIST_INDEX(p_s_sb) = -10000 ; /* make sure flush_old_commits does not try to flush a list while replay is on */
  1767.   /* clear out the journal list array */
  1768.   memset(SB_JOURNAL_LIST(p_s_sb), 0, sizeof(struct reiserfs_journal_list) * JOURNAL_LIST_COUNT) ; 
  1769.   journal_list_init(p_s_sb) ;
  1770.   memset(SB_JOURNAL(p_s_sb)->j_list_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
  1771.   memset(journal_writers, 0, sizeof(char *) * 512) ; /* debug code */
  1772.   INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
  1773.   INIT_LIST_HEAD(&(SB_JOURNAL(p_s_sb)->j_dummy_inode.i_dirty_buffers)) ;
  1774.   reiserfs_allocate_list_bitmaps(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap, 
  1775.                                  SB_BMAP_NR(p_s_sb)) ;
  1776.   allocate_bitmap_nodes(p_s_sb) ;
  1777.   SB_JOURNAL(p_s_sb)->j_start = 0 ;
  1778.   SB_JOURNAL(p_s_sb)->j_len = 0 ;
  1779.   SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
  1780.   atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
  1781.   SB_JOURNAL(p_s_sb)->j_bcount = 0 ;   
  1782.   SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;   
  1783.   SB_JOURNAL(p_s_sb)->j_last = NULL ;   
  1784.   SB_JOURNAL(p_s_sb)->j_first = NULL ;     
  1785.   init_waitqueue_head(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
  1786.   init_waitqueue_head(&(SB_JOURNAL(p_s_sb)->j_wait)) ; 
  1787.   SB_JOURNAL(p_s_sb)->j_trans_id = 10 ;  
  1788.   SB_JOURNAL(p_s_sb)->j_mount_id = 10 ; 
  1789.   SB_JOURNAL(p_s_sb)->j_state = 0 ;
  1790.   atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
  1791.   atomic_set(&(SB_JOURNAL(p_s_sb)->j_wlock), 0) ;
  1792.   SB_JOURNAL(p_s_sb)->j_cnode_free_list = allocate_cnodes(num_cnodes) ;
  1793.   SB_JOURNAL(p_s_sb)->j_cnode_free_orig = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
  1794.   SB_JOURNAL(p_s_sb)->j_cnode_free = SB_JOURNAL(p_s_sb)->j_cnode_free_list ? num_cnodes : 0 ;
  1795.   SB_JOURNAL(p_s_sb)->j_cnode_used = 0 ;
  1796.   SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
  1797.   init_journal_hash(p_s_sb) ;
  1798.   SB_JOURNAL_LIST(p_s_sb)[0].j_list_bitmap = get_list_bitmap(p_s_sb, SB_JOURNAL_LIST(p_s_sb)) ;
  1799.   if (!(SB_JOURNAL_LIST(p_s_sb)[0].j_list_bitmap)) {
  1800.     reiserfs_warning("journal-2005, get_list_bitmap failed for journal list 0n") ;
  1801.     return 1 ;
  1802.   }
  1803.   if (journal_read(p_s_sb) < 0) {
  1804.     reiserfs_warning("Replay Failure, unable to mountn") ;
  1805.     free_journal_ram(p_s_sb) ;
  1806.     return 1 ;
  1807.   }
  1808.   SB_JOURNAL_LIST_INDEX(p_s_sb) = 0 ; /* once the read is done, we can set this
  1809.                                          where it belongs */
  1810.   INIT_LIST_HEAD (&SB_JOURNAL(p_s_sb)->j_prealloc_list);
  1811.   if (reiserfs_dont_log (p_s_sb))
  1812.     return 0;
  1813.   reiserfs_mounted_fs_count++ ;
  1814.   if (reiserfs_mounted_fs_count <= 1) {
  1815.     kernel_thread((void *)(void *)reiserfs_journal_commit_thread, NULL,
  1816.                   CLONE_FS | CLONE_FILES | CLONE_VM) ;
  1817.   }
  1818.   return 0 ;
  1819. }
  1820. /*
  1821. ** test for a polite end of the current transaction.  Used by file_write, and should
  1822. ** be used by delete to make sure they don't write more than can fit inside a single
  1823. ** transaction
  1824. */
  1825. int journal_transaction_should_end(struct reiserfs_transaction_handle *th, int new_alloc) {
  1826.   time_t now = CURRENT_TIME ;
  1827.   if (reiserfs_dont_log(th->t_super)) 
  1828.     return 0 ;
  1829.   if ( SB_JOURNAL(th->t_super)->j_must_wait > 0 ||
  1830.        (SB_JOURNAL(th->t_super)->j_len_alloc + new_alloc) >= JOURNAL_MAX_BATCH || 
  1831.        atomic_read(&(SB_JOURNAL(th->t_super)->j_jlock)) ||
  1832.       (now - SB_JOURNAL(th->t_super)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE ||
  1833.        SB_JOURNAL(th->t_super)->j_cnode_free < (JOURNAL_TRANS_MAX * 3)) { 
  1834.     return 1 ;
  1835.   }
  1836.   return 0 ;
  1837. }
  1838. /* this must be called inside a transaction, and requires the 
  1839. ** kernel_lock to be held
  1840. */
  1841. void reiserfs_block_writes(struct reiserfs_transaction_handle *th) {
  1842.     struct super_block *s = th->t_super ;
  1843.     SB_JOURNAL(s)->j_must_wait = 1 ;
  1844.     set_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
  1845.     return ;
  1846. }
  1847. /* this must be called without a transaction started, and does not
  1848. ** require BKL
  1849. */
  1850. void reiserfs_allow_writes(struct super_block *s) {
  1851.     clear_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
  1852.     wake_up(&SB_JOURNAL(s)->j_join_wait) ;
  1853. }
  1854. /* this must be called without a transaction started, and does not
  1855. ** require BKL
  1856. */
  1857. void reiserfs_wait_on_write_block(struct super_block *s) {
  1858.     wait_event(SB_JOURNAL(s)->j_join_wait, 
  1859.                !test_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state)) ;
  1860. }
  1861. /* join == true if you must join an existing transaction.
  1862. ** join == false if you can deal with waiting for others to finish
  1863. **
  1864. ** this will block until the transaction is joinable.  send the number of blocks you
  1865. ** expect to use in nblocks.
  1866. */
  1867. static int do_journal_begin_r(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,unsigned long nblocks,int join) {
  1868.   time_t now = CURRENT_TIME ;
  1869.   int old_trans_id  ;
  1870.   reiserfs_check_lock_depth("journal_begin") ;
  1871.   RFALSE( p_s_sb->s_flags & MS_RDONLY, 
  1872.   "clm-2078: calling journal_begin on readonly FS") ;
  1873.   if (reiserfs_dont_log(p_s_sb)) {
  1874.     th->t_super = p_s_sb ; /* others will check this for the don't log flag */
  1875.     return 0 ;
  1876.   }
  1877.   PROC_INFO_INC( p_s_sb, journal.journal_being );
  1878. relock:
  1879.   lock_journal(p_s_sb) ;
  1880.   if (test_bit(WRITERS_BLOCKED, &SB_JOURNAL(p_s_sb)->j_state)) {
  1881.     unlock_journal(p_s_sb) ;
  1882.     reiserfs_wait_on_write_block(p_s_sb) ;
  1883.     PROC_INFO_INC( p_s_sb, journal.journal_relock_writers );
  1884.     goto relock ;
  1885.   }
  1886.   /* if there is no room in the journal OR
  1887.   ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning 
  1888.   ** we don't sleep if there aren't other writers
  1889.   */
  1890.   if (  (!join && SB_JOURNAL(p_s_sb)->j_must_wait > 0) ||
  1891.      ( !join && (SB_JOURNAL(p_s_sb)->j_len_alloc + nblocks + 2) >= JOURNAL_MAX_BATCH) || 
  1892.      (!join && atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0 && SB_JOURNAL(p_s_sb)->j_trans_start_time > 0 && 
  1893.       (now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE) ||
  1894.      (!join && atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) ) ||
  1895.      (!join && SB_JOURNAL(p_s_sb)->j_cnode_free < (JOURNAL_TRANS_MAX * 3))) {
  1896.     unlock_journal(p_s_sb) ; /* allow others to finish this transaction */
  1897.     /* if writer count is 0, we can just force this transaction to end, and start
  1898.     ** a new one afterwards.
  1899.     */
  1900.     if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0) {
  1901.       struct reiserfs_transaction_handle myth ;
  1902.       journal_join(&myth, p_s_sb, 1) ;
  1903.       reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
  1904.       journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
  1905.       do_journal_end(&myth, p_s_sb,1,COMMIT_NOW) ;
  1906.     } else {
  1907.       /* but if the writer count isn't zero, we have to wait for the current writers to finish.
  1908.       ** They won't batch on transaction end once we set j_jlock
  1909.       */
  1910.       atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
  1911.       old_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
  1912.       while(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) &&
  1913.             SB_JOURNAL(p_s_sb)->j_trans_id == old_trans_id) {
  1914. sleep_on(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
  1915.       }
  1916.     }
  1917.     PROC_INFO_INC( p_s_sb, journal.journal_relock_wcount );
  1918.     goto relock ;
  1919.   }
  1920.   if (SB_JOURNAL(p_s_sb)->j_trans_start_time == 0) { /* we are the first writer, set trans_id */
  1921.     SB_JOURNAL(p_s_sb)->j_trans_start_time = now ;
  1922.   }
  1923.   atomic_inc(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
  1924.   SB_JOURNAL(p_s_sb)->j_len_alloc += nblocks ;
  1925.   th->t_blocks_logged = 0 ;
  1926.   th->t_blocks_allocated = nblocks ;
  1927.   th->t_super = p_s_sb ;
  1928.   th->t_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
  1929.   th->t_caller = "Unknown" ;
  1930.   unlock_journal(p_s_sb) ;
  1931.   p_s_sb->s_dirt = 1; 
  1932.   return 0 ;
  1933. }
  1934. static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
  1935.   return do_journal_begin_r(th, p_s_sb, nblocks, 1) ;
  1936. }
  1937. int journal_begin(struct reiserfs_transaction_handle *th, struct super_block  * p_s_sb, unsigned long nblocks) {
  1938.   return do_journal_begin_r(th, p_s_sb, nblocks, 0) ;
  1939. }
  1940. /* not used at all */
  1941. int journal_prepare(struct super_block  * p_s_sb, struct buffer_head *bh) {
  1942.   return 0 ;
  1943. }
  1944. /*
  1945. ** puts bh into the current transaction.  If it was already there, reorders removes the
  1946. ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
  1947. **
  1948. ** if it was dirty, cleans and files onto the clean list.  I can't let it be dirty again until the
  1949. ** transaction is committed.
  1950. ** 
  1951. ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
  1952. */
  1953. int journal_mark_dirty(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
  1954.   struct reiserfs_journal_cnode *cn = NULL;
  1955.   int count_already_incd = 0 ;
  1956.   int prepared = 0 ;
  1957.   PROC_INFO_INC( p_s_sb, journal.mark_dirty );
  1958.   if (reiserfs_dont_log(th->t_super)) {
  1959.     mark_buffer_dirty(bh) ;
  1960.     return 0 ;
  1961.   }
  1962.   if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
  1963.     reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ldn", 
  1964.                    th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
  1965.   }
  1966.   p_s_sb->s_dirt = 1 ;
  1967.   prepared = test_and_clear_bit(BH_JPrepared, &bh->b_state) ;
  1968.   /* already in this transaction, we are done */
  1969.   if (buffer_journaled(bh)) {
  1970.     PROC_INFO_INC( p_s_sb, journal.mark_dirty_already );
  1971.     return 0 ;
  1972.   }
  1973.   /* this must be turned into a panic instead of a warning.  We can't allow
  1974.   ** a dirty or journal_dirty or locked buffer to be logged, as some changes
  1975.   ** could get to disk too early.  NOT GOOD.
  1976.   */
  1977.   if (!prepared || buffer_locked(bh)) {
  1978.     printk("journal-1777: buffer %lu bad state %cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAITn", bh->b_blocknr, prepared ? ' ' : '!', 
  1979.                             buffer_locked(bh) ? ' ' : '!',
  1980.     buffer_dirty(bh) ? ' ' : '!',
  1981.     buffer_journal_dirty(bh) ? ' ' : '!') ;
  1982.     show_reiserfs_locks() ;
  1983.   }
  1984.   count_already_incd = clear_prepared_bits(bh) ;
  1985.   if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0) {
  1986.     printk("journal-1409: journal_mark_dirty returning because j_wcount was %dn", atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount))) ;
  1987.     return 1 ;
  1988.   }
  1989.   /* this error means I've screwed up, and we've overflowed the transaction.  
  1990.   ** Nothing can be done here, except make the FS readonly or panic.
  1991.   */ 
  1992.   if (SB_JOURNAL(p_s_sb)->j_len >= JOURNAL_TRANS_MAX) { 
  1993.     reiserfs_panic(th->t_super, "journal-1413: journal_mark_dirty: j_len (%lu) is too bign", SB_JOURNAL(p_s_sb)->j_len) ;
  1994.   }
  1995.   if (buffer_journal_dirty(bh)) {
  1996.     count_already_incd = 1 ;
  1997.     PROC_INFO_INC( p_s_sb, journal.mark_dirty_notjournal );
  1998.     mark_buffer_notjournal_dirty(bh) ;
  1999.   }
  2000.   if (buffer_dirty(bh)) {
  2001.     clear_bit(BH_Dirty, &bh->b_state) ;
  2002.   }
  2003.   if (buffer_journaled(bh)) { /* must double check after getting lock */
  2004.     goto done ;
  2005.   }
  2006.   if (SB_JOURNAL(p_s_sb)->j_len > SB_JOURNAL(p_s_sb)->j_len_alloc) {
  2007.     SB_JOURNAL(p_s_sb)->j_len_alloc = SB_JOURNAL(p_s_sb)->j_len + JOURNAL_PER_BALANCE_CNT ;
  2008.   }
  2009.   set_bit(BH_JDirty, &bh->b_state) ;
  2010.   /* now put this guy on the end */
  2011.   if (!cn) {
  2012.     cn = get_cnode(p_s_sb) ;
  2013.     if (!cn) {
  2014.       reiserfs_panic(p_s_sb, "get_cnode failed!n"); 
  2015.     }
  2016.     if (th->t_blocks_logged == th->t_blocks_allocated) {
  2017.       th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT ;
  2018.       SB_JOURNAL(p_s_sb)->j_len_alloc += JOURNAL_PER_BALANCE_CNT ;
  2019.     }
  2020.     th->t_blocks_logged++ ;
  2021.     SB_JOURNAL(p_s_sb)->j_len++ ;
  2022.     cn->bh = bh ;
  2023.     cn->blocknr = bh->b_blocknr ;
  2024.     cn->dev = bh->b_dev ;
  2025.     cn->jlist = NULL ;
  2026.     insert_journal_hash(SB_JOURNAL(p_s_sb)->j_hash_table, cn) ;
  2027.     if (!count_already_incd) {
  2028.       get_bh(bh) ;
  2029.     }
  2030.   }
  2031.   cn->next = NULL ;
  2032.   cn->prev = SB_JOURNAL(p_s_sb)->j_last ;
  2033.   cn->bh = bh ;
  2034.   if (SB_JOURNAL(p_s_sb)->j_last) {
  2035.     SB_JOURNAL(p_s_sb)->j_last->next = cn ;
  2036.     SB_JOURNAL(p_s_sb)->j_last = cn ;
  2037.   } else {
  2038.     SB_JOURNAL(p_s_sb)->j_first = cn ;
  2039.     SB_JOURNAL(p_s_sb)->j_last = cn ;
  2040.   }
  2041. done:
  2042.   return 0 ;
  2043. }
  2044. /*
  2045. ** if buffer already in current transaction, do a journal_mark_dirty
  2046. ** otherwise, just mark it dirty and move on.  Used for writes to meta blocks
  2047. ** that don't need journaling
  2048. */
  2049. int journal_mark_dirty_nolog(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
  2050.   if (reiserfs_dont_log(th->t_super) || buffer_journaled(bh) || 
  2051.       buffer_journal_dirty(bh)) {
  2052.     return journal_mark_dirty(th, p_s_sb, bh) ;
  2053.   }
  2054.   if (get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_list_hash_table, bh->b_dev,bh->b_blocknr,bh->b_size)) {
  2055.     return journal_mark_dirty(th, p_s_sb, bh) ;
  2056.   }
  2057.   mark_buffer_dirty(bh) ;
  2058.   return 0 ;
  2059. }
  2060. int journal_end(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
  2061.   return do_journal_end(th, p_s_sb, nblocks, 0) ;
  2062. }
  2063. /* removes from the current transaction, relsing and descrementing any counters.  
  2064. ** also files the removed buffer directly onto the clean list
  2065. **
  2066. ** called by journal_mark_freed when a block has been deleted
  2067. **
  2068. ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
  2069. */
  2070. static int remove_from_transaction(struct super_block *p_s_sb, unsigned long blocknr, int already_cleaned) {
  2071.   struct buffer_head *bh ;
  2072.   struct reiserfs_journal_cnode *cn ;
  2073.   int ret = 0;
  2074.   cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_hash_table, p_s_sb->s_dev, blocknr, p_s_sb->s_blocksize) ;
  2075.   if (!cn || !cn->bh) {
  2076.     return ret ;
  2077.   }
  2078.   bh = cn->bh ;
  2079.   if (cn->prev) {
  2080.     cn->prev->next = cn->next ;
  2081.   }
  2082.   if (cn->next) {
  2083.     cn->next->prev = cn->prev ;
  2084.   }
  2085.   if (cn == SB_JOURNAL(p_s_sb)->j_first) {
  2086.     SB_JOURNAL(p_s_sb)->j_first = cn->next ;  
  2087.   }
  2088.   if (cn == SB_JOURNAL(p_s_sb)->j_last) {
  2089.     SB_JOURNAL(p_s_sb)->j_last = cn->prev ;
  2090.   }
  2091.   remove_journal_hash(SB_JOURNAL(p_s_sb)->j_hash_table, NULL, bh, 0) ; 
  2092.   mark_buffer_not_journaled(bh) ; /* don't log this one */
  2093.   if (!already_cleaned) {
  2094.     mark_buffer_notjournal_dirty(bh) ; 
  2095.     put_bh(bh) ;
  2096.     if (atomic_read(&(bh->b_count)) < 0) {
  2097.       printk("journal-1752: remove from trans, b_count < 0n") ;
  2098.     }
  2099.     if (!buffer_locked(bh)) reiserfs_clean_and_file_buffer(bh) ; 
  2100.     ret = 1 ;
  2101.   }
  2102.   SB_JOURNAL(p_s_sb)->j_len-- ;
  2103.   SB_JOURNAL(p_s_sb)->j_len_alloc-- ;
  2104.   free_cnode(p_s_sb, cn) ;
  2105.   return ret ;
  2106. }
  2107. /* removes from a specific journal list hash */
  2108. static int remove_from_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, struct buffer_head *bh, int remove_freed) {
  2109.   remove_journal_hash(SB_JOURNAL(s)->j_list_hash_table, jl, bh, remove_freed) ;
  2110.   return 0 ;
  2111. }
  2112. /*
  2113. ** for any cnode in a journal list, it can only be dirtied of all the
  2114. ** transactions that include it are commited to disk.
  2115. ** this checks through each transaction, and returns 1 if you are allowed to dirty,
  2116. ** and 0 if you aren't
  2117. **
  2118. ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
  2119. ** blocks for a given transaction on disk
  2120. **
  2121. */
  2122. static int can_dirty(struct reiserfs_journal_cnode *cn) {
  2123.   kdev_t dev = cn->dev ;
  2124.   unsigned long blocknr = cn->blocknr  ;
  2125.   struct reiserfs_journal_cnode *cur = cn->hprev ;
  2126.   int can_dirty = 1 ;
  2127.   
  2128.   /* first test hprev.  These are all newer than cn, so any node here
  2129.   ** with the name block number and dev means this node can't be sent
  2130.   ** to disk right now.
  2131.   */
  2132.   while(cur && can_dirty) {
  2133.     if (cur->jlist && cur->bh && cur->blocknr && cur->dev == dev && 
  2134.         cur->blocknr == blocknr) {
  2135.       can_dirty = 0 ;
  2136.     }
  2137.     cur = cur->hprev ;
  2138.   }
  2139.   /* then test hnext.  These are all older than cn.  As long as they
  2140.   ** are committed to the log, it is safe to write cn to disk
  2141.   */
  2142.   cur = cn->hnext ;
  2143.   while(cur && can_dirty) {
  2144.     if (cur->jlist && cur->jlist->j_len > 0 && 
  2145.         atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh && 
  2146.         cur->blocknr && cur->dev == dev && cur->blocknr == blocknr) {
  2147.       can_dirty = 0 ;
  2148.     }
  2149.     cur = cur->hnext ;
  2150.   }
  2151.   return can_dirty ;
  2152. }
  2153. /* syncs the commit blocks, but does not force the real buffers to disk
  2154. ** will wait until the current transaction is done/commited before returning 
  2155. */
  2156. int journal_end_sync(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
  2157.   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
  2158.     reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
  2159.     journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
  2160.   }
  2161.   return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT) ;
  2162. }
  2163. int show_reiserfs_locks(void) {
  2164.   dump_journal_writers() ;
  2165.   return 0 ;
  2166. }
  2167. /*
  2168. ** used to get memory back from async commits that are floating around
  2169. ** and to reclaim any blocks deleted but unusable because their commits
  2170. ** haven't hit disk yet.  called from bitmap.c
  2171. **
  2172. ** if it starts flushing things, it ors SCHEDULE_OCCURRED into repeat.
  2173. ** note, this is just if schedule has a chance of occuring.  I need to 
  2174. ** change flush_commit_lists to have a repeat parameter too.
  2175. **
  2176. */
  2177. void flush_async_commits(struct super_block *p_s_sb) {
  2178.   int i ;
  2179.   for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
  2180.     if (i != SB_JOURNAL_LIST_INDEX(p_s_sb)) {
  2181.       flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + i, 1) ; 
  2182.     }
  2183.   }
  2184. }
  2185. /*
  2186. ** flushes any old transactions to disk
  2187. ** ends the current transaction if it is too old
  2188. **
  2189. ** also calls flush_journal_list with old_only == 1, which allows me to reclaim
  2190. ** memory and such from the journal lists whose real blocks are all on disk.
  2191. **
  2192. ** called by sync_dev_journal from buffer.c
  2193. */
  2194. int flush_old_commits(struct super_block *p_s_sb, int immediate) {
  2195.   int i ;
  2196.   int count = 0;
  2197.   int start ; 
  2198.   time_t now ; 
  2199.   struct reiserfs_transaction_handle th ; 
  2200.   start =  SB_JOURNAL_LIST_INDEX(p_s_sb) ;
  2201.   now = CURRENT_TIME ;
  2202.   /* safety check so we don't flush while we are replaying the log during mount */
  2203.   if (SB_JOURNAL_LIST_INDEX(p_s_sb) < 0) {
  2204.     return 0  ;
  2205.   }
  2206.   /* starting with oldest, loop until we get to the start */
  2207.   i = (SB_JOURNAL_LIST_INDEX(p_s_sb) + 1) % JOURNAL_LIST_COUNT ;
  2208.   while(i != start) {
  2209.     if (SB_JOURNAL_LIST(p_s_sb)[i].j_len > 0 && ((now - SB_JOURNAL_LIST(p_s_sb)[i].j_timestamp) > JOURNAL_MAX_COMMIT_AGE ||
  2210.        immediate)) {
  2211.       /* we have to check again to be sure the current transaction did not change */
  2212.       if (i != SB_JOURNAL_LIST_INDEX(p_s_sb))  {
  2213. flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + i, 1) ;
  2214.       }
  2215.     }
  2216.     i = (i + 1) % JOURNAL_LIST_COUNT ;
  2217.     count++ ;
  2218.   }
  2219.   /* now, check the current transaction.  If there are no writers, and it is too old, finish it, and
  2220.   ** force the commit blocks to disk
  2221.   */
  2222.   if (!immediate && atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0 &&  
  2223.      SB_JOURNAL(p_s_sb)->j_trans_start_time > 0 && 
  2224.      SB_JOURNAL(p_s_sb)->j_len > 0 && 
  2225.      (now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE) {
  2226.     journal_join(&th, p_s_sb, 1) ;
  2227.     reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
  2228.     journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
  2229.     do_journal_end(&th, p_s_sb,1, COMMIT_NOW) ;
  2230.   } else if (immediate) { /* belongs above, but I wanted this to be very explicit as a special case.  If they say to 
  2231.                              flush, we must be sure old transactions hit the disk too. */
  2232.     journal_join(&th, p_s_sb, 1) ;
  2233.     reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
  2234.     journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
  2235.     do_journal_end(&th, p_s_sb,1, COMMIT_NOW | WAIT) ;
  2236.   }
  2237.    reiserfs_journal_kupdate(p_s_sb) ;
  2238.    return 0 ;
  2239. }
  2240. /*
  2241. ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
  2242. ** 
  2243. ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all 
  2244. ** the writers are done.  By the time it wakes up, the transaction it was called has already ended, so it just
  2245. ** flushes the commit list and returns 0.
  2246. **
  2247. ** Won't batch when flush or commit_now is set.  Also won't batch when others are waiting on j_join_wait.
  2248. ** 
  2249. ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
  2250. */
  2251. static int check_journal_end(struct reiserfs_transaction_handle *th, struct super_block  * p_s_sb, 
  2252.                              unsigned long nblocks, int flags) {
  2253.   time_t now ;
  2254.   int flush = flags & FLUSH_ALL ;
  2255.   int commit_now = flags & COMMIT_NOW ;
  2256.   int wait_on_commit = flags & WAIT ;
  2257.   if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
  2258.     reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ldn", 
  2259.                    th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
  2260.   }
  2261.   SB_JOURNAL(p_s_sb)->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged) ;
  2262.   if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) { /* <= 0 is allowed.  unmounting might not call begin */
  2263.     atomic_dec(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
  2264.   }
  2265.   /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released 
  2266.   ** will be dealt with by next transaction that actually writes something, but should be taken
  2267.   ** care of in this trans
  2268.   */
  2269.   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
  2270.     int wcount = atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
  2271.     unlock_journal(p_s_sb) ;
  2272.     if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock))  > 0 && wcount <= 0) {
  2273.       atomic_dec(&(SB_JOURNAL(p_s_sb)->j_jlock)) ;
  2274.       wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
  2275.     }
  2276.     return 0 ;
  2277.   }
  2278.   /* if wcount > 0, and we are called to with flush or commit_now,
  2279.   ** we wait on j_join_wait.  We will wake up when the last writer has
  2280.   ** finished the transaction, and started it on its way to the disk.
  2281.   ** Then, we flush the commit or journal list, and just return 0 
  2282.   ** because the rest of journal end was already done for this transaction.
  2283.   */
  2284.   if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) {
  2285.     if (flush || commit_now) {
  2286.       int orig_jindex = SB_JOURNAL_LIST_INDEX(p_s_sb) ;
  2287.       atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
  2288.       if (flush) {
  2289.         SB_JOURNAL(p_s_sb)->j_next_full_flush = 1 ;
  2290.       }
  2291.       unlock_journal(p_s_sb) ;
  2292.       /* sleep while the current transaction is still j_jlocked */
  2293.       while(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) && 
  2294.             SB_JOURNAL(p_s_sb)->j_trans_id == th->t_trans_id) {
  2295. sleep_on(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
  2296.       }
  2297.       if (commit_now) {
  2298. if (wait_on_commit) {
  2299.   flush_commit_list(p_s_sb,  SB_JOURNAL_LIST(p_s_sb) + orig_jindex, 1) ;
  2300. } else {
  2301.   commit_flush_async(p_s_sb, orig_jindex) ; 
  2302. }
  2303.       }
  2304.       return 0 ;
  2305.     } 
  2306.     unlock_journal(p_s_sb) ;
  2307.     return 0 ;
  2308.   }
  2309.   /* deal with old transactions where we are the last writers */
  2310.   now = CURRENT_TIME ;
  2311.   if ((now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE) {
  2312.     commit_now = 1 ;
  2313.     SB_JOURNAL(p_s_sb)->j_next_async_flush = 1 ;
  2314.   }
  2315.   /* don't batch when someone is waiting on j_join_wait */
  2316.   /* don't batch when syncing the commit or flushing the whole trans */
  2317.   if (!(SB_JOURNAL(p_s_sb)->j_must_wait > 0) && !(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock))) && !flush && !commit_now && 
  2318.       (SB_JOURNAL(p_s_sb)->j_len < JOURNAL_MAX_BATCH)  && 
  2319.       SB_JOURNAL(p_s_sb)->j_len_alloc < JOURNAL_MAX_BATCH && SB_JOURNAL(p_s_sb)->j_cnode_free > (JOURNAL_TRANS_MAX * 3)) {
  2320.     SB_JOURNAL(p_s_sb)->j_bcount++ ;
  2321.     unlock_journal(p_s_sb) ;
  2322.     return 0 ;
  2323.   }
  2324.   if (SB_JOURNAL(p_s_sb)->j_start > JOURNAL_BLOCK_COUNT) {
  2325.     reiserfs_panic(p_s_sb, "journal-003: journal_end: j_start (%ld) is too highn", SB_JOURNAL(p_s_sb)->j_start) ;
  2326.   }
  2327.   return 1 ;
  2328. }
  2329. /*
  2330. ** Does all the work that makes deleting blocks safe.
  2331. ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
  2332. ** 
  2333. ** otherwise:
  2334. ** set a bit for the block in the journal bitmap.  That will prevent it from being allocated for unformatted nodes
  2335. ** before this transaction has finished.
  2336. **
  2337. ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers.  That will prevent any old transactions with
  2338. ** this block from trying to flush to the real location.  Since we aren't removing the cnode from the journal_list_hash,
  2339. ** the block can't be reallocated yet.
  2340. **
  2341. ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
  2342. */
  2343. int journal_mark_freed(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long blocknr) {
  2344.   struct reiserfs_journal_cnode *cn = NULL ;
  2345.   struct buffer_head *bh = NULL ;
  2346.   struct reiserfs_list_bitmap *jb = NULL ;
  2347.   int cleaned = 0 ;
  2348.   
  2349.   if (reiserfs_dont_log(th->t_super)) {
  2350.     bh = sb_get_hash_table(p_s_sb, blocknr) ;
  2351.     if (bh && buffer_dirty (bh)) {
  2352.       printk ("journal_mark_freed(dont_log): dirty buffer on hash list: %lx %ldn", bh->b_state, blocknr);
  2353.       BUG ();
  2354.     }
  2355.     brelse (bh);
  2356.     return 0 ;
  2357.   }
  2358.   bh = sb_get_hash_table(p_s_sb, blocknr) ;
  2359.   /* if it is journal new, we just remove it from this transaction */
  2360.   if (bh && buffer_journal_new(bh)) {
  2361.     mark_buffer_notjournal_new(bh) ;
  2362.     clear_prepared_bits(bh) ;
  2363.     cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
  2364.   } else {
  2365.     /* set the bit for this block in the journal bitmap for this transaction */
  2366.     jb = SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_list_bitmap ;
  2367.     if (!jb) {
  2368.       reiserfs_panic(p_s_sb, "journal-1702: journal_mark_freed, journal_list_bitmap is NULLn") ;
  2369.     }
  2370.     set_bit_in_list_bitmap(p_s_sb, blocknr, jb) ;
  2371.     /* Note, the entire while loop is not allowed to schedule.  */
  2372.     if (bh) {
  2373.       clear_prepared_bits(bh) ;
  2374.     }
  2375.     cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
  2376.     /* find all older transactions with this block, make sure they don't try to write it out */
  2377.     cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_list_hash_table, p_s_sb->s_dev, blocknr, p_s_sb->s_blocksize) ;
  2378.     while (cn) {
  2379.       if (p_s_sb->s_dev == cn->dev && blocknr == cn->blocknr) {
  2380. set_bit(BLOCK_FREED, &cn->state) ;
  2381. if (cn->bh) {
  2382.   if (!cleaned) {
  2383.     /* remove_from_transaction will brelse the buffer if it was 
  2384.     ** in the current trans
  2385.     */
  2386.     mark_buffer_notjournal_dirty(cn->bh) ;
  2387.     cleaned = 1 ;
  2388.     put_bh(cn->bh) ;
  2389.     if (atomic_read(&(cn->bh->b_count)) < 0) {
  2390.       printk("journal-2138: cn->bh->b_count < 0n") ;
  2391.     }
  2392.   }
  2393.   if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
  2394.     atomic_dec(&(cn->jlist->j_nonzerolen)) ;
  2395.   }
  2396.   cn->bh = NULL ; 
  2397.       }
  2398.       cn = cn->hnext ;
  2399.     }
  2400.   }
  2401.   if (bh) {
  2402.     reiserfs_clean_and_file_buffer(bh) ;
  2403.     put_bh(bh) ; /* get_hash grabs the buffer */
  2404.     if (atomic_read(&(bh->b_count)) < 0) {
  2405.       printk("journal-2165: bh->b_count < 0n") ;
  2406.     }
  2407.   }
  2408.   return 0 ;
  2409. }
  2410. void reiserfs_update_inode_transaction(struct inode *inode) {
  2411.   
  2412.   inode->u.reiserfs_i.i_trans_index = SB_JOURNAL_LIST_INDEX(inode->i_sb);
  2413.   inode->u.reiserfs_i.i_trans_id = SB_JOURNAL(inode->i_sb)->j_trans_id ;
  2414. }
  2415. static int reiserfs_inode_in_this_transaction(struct inode *inode) {
  2416.   if (inode->u.reiserfs_i.i_trans_id == SB_JOURNAL(inode->i_sb)->j_trans_id || 
  2417.       inode->u.reiserfs_i.i_trans_id == 0) {
  2418.     return 1; 
  2419.   } 
  2420.   return 0 ;
  2421. }
  2422. void reiserfs_commit_for_inode(struct inode *inode) {
  2423.   struct reiserfs_journal_list *jl ;
  2424.   struct reiserfs_transaction_handle th ;
  2425.   struct super_block *sb = inode->i_sb ;
  2426.   jl = SB_JOURNAL_LIST(sb) + inode->u.reiserfs_i.i_trans_index ;
  2427.   /* is it from the current transaction, or from an unknown transaction? */
  2428.   if (reiserfs_inode_in_this_transaction(inode)) {
  2429.     journal_join(&th, sb, 1) ;
  2430.     reiserfs_update_inode_transaction(inode) ;
  2431.     journal_end_sync(&th, sb, 1) ;
  2432.   } else if (jl->j_trans_id == inode->u.reiserfs_i.i_trans_id) {
  2433.     flush_commit_list(sb, jl, 1) ;
  2434.   }
  2435.   /* if the transaction id does not match, this list is long since flushed
  2436.   ** and we don't have to do anything here
  2437.   */
  2438. }
  2439. void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb, 
  2440.                                       struct buffer_head *bh) {
  2441.   PROC_INFO_INC( p_s_sb, journal.restore_prepared );
  2442.   if (reiserfs_dont_log (p_s_sb))
  2443.     return;
  2444.   if (!bh) {
  2445.     return ;
  2446.   }
  2447.   clear_bit(BH_JPrepared, &bh->b_state) ;
  2448. }
  2449. extern struct tree_balance *cur_tb ;
  2450. /*
  2451. ** before we can change a metadata block, we have to make sure it won't
  2452. ** be written to disk while we are altering it.  So, we must:
  2453. ** clean it
  2454. ** wait on it.
  2455. ** 
  2456. */
  2457. void reiserfs_prepare_for_journal(struct super_block *p_s_sb, 
  2458.                                   struct buffer_head *bh, int wait) {
  2459.   int retry_count = 0 ;
  2460.   PROC_INFO_INC( p_s_sb, journal.prepare );
  2461.   if (reiserfs_dont_log (p_s_sb))
  2462.     return;
  2463.   while(!test_bit(BH_JPrepared, &bh->b_state) ||
  2464.         (wait && buffer_locked(bh))) {
  2465.     if (buffer_journaled(bh)) {
  2466.       set_bit(BH_JPrepared, &bh->b_state) ;
  2467.       return ;
  2468.     }
  2469.     set_bit(BH_JPrepared, &bh->b_state) ;
  2470.     if (wait) {
  2471.       RFALSE( buffer_locked(bh) && cur_tb != NULL,
  2472.       "waiting while do_balance was runningn") ;
  2473.       wait_on_buffer(bh) ;
  2474.     }
  2475.     PROC_INFO_INC( p_s_sb, journal.prepare_retry );
  2476.     retry_count++ ;
  2477.   }
  2478. }
  2479. /* 
  2480. ** long and ugly.  If flush, will not return until all commit
  2481. ** blocks and all real buffers in the trans are on disk.
  2482. ** If no_async, won't return until all commit blocks are on disk.
  2483. **
  2484. ** keep reading, there are comments as you go along
  2485. */
  2486. static int do_journal_end(struct reiserfs_transaction_handle *th, struct super_block  * p_s_sb, unsigned long nblocks, 
  2487.           int flags) {
  2488.   struct reiserfs_journal_cnode *cn, *next, *jl_cn; 
  2489.   struct reiserfs_journal_cnode *last_cn = NULL;
  2490.   struct reiserfs_journal_desc *desc ; 
  2491.   struct reiserfs_journal_commit *commit ; 
  2492.   struct buffer_head *c_bh ; /* commit bh */
  2493.   struct buffer_head *d_bh ; /* desc bh */
  2494.   int cur_write_start = 0 ; /* start index of current log write */
  2495.   int cur_blocks_left = 0 ; /* number of journal blocks left to write */
  2496.   int old_start ;
  2497.   int i ;
  2498.   int jindex ;
  2499.   int orig_jindex ;
  2500.   int flush = flags & FLUSH_ALL ;
  2501.   int commit_now = flags & COMMIT_NOW ;
  2502.   int wait_on_commit = flags & WAIT ;
  2503.   struct reiserfs_super_block *rs ; 
  2504.   if (reiserfs_dont_log(th->t_super)) {
  2505.     return 0 ;
  2506.   }
  2507.   lock_journal(p_s_sb) ;
  2508.   if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
  2509.     flags |= FLUSH_ALL ;
  2510.     flush = 1 ;
  2511.   }
  2512.   if (SB_JOURNAL(p_s_sb)->j_next_async_flush) {
  2513.     flags |= COMMIT_NOW ;
  2514.     commit_now = 1 ;
  2515.   }
  2516.   /* check_journal_end locks the journal, and unlocks if it does not return 1 
  2517.   ** it tells us if we should continue with the journal_end, or just return
  2518.   */
  2519.   if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
  2520.     return 0 ;
  2521.   }
  2522.   /* check_journal_end might set these, check again */
  2523.   if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
  2524.     flush = 1 ;
  2525.   }
  2526.   if (SB_JOURNAL(p_s_sb)->j_next_async_flush) {
  2527.     commit_now = 1 ;
  2528.   }
  2529.   /*
  2530.   ** j must wait means we have to flush the log blocks, and the real blocks for
  2531.   ** this transaction
  2532.   */
  2533.   if (SB_JOURNAL(p_s_sb)->j_must_wait > 0) {
  2534.     flush = 1 ;
  2535.   }
  2536. #ifdef REISERFS_PREALLOCATE
  2537.   reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
  2538.       * the transaction */
  2539. #endif
  2540.   
  2541.   rs = SB_DISK_SUPER_BLOCK(p_s_sb) ;
  2542.   /* setup description block */
  2543.   d_bh = sb_getblk(p_s_sb, reiserfs_get_journal_block(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start) ; 
  2544.   mark_buffer_uptodate(d_bh, 1) ;
  2545.   desc = (struct reiserfs_journal_desc *)(d_bh)->b_data ;
  2546.   memset(desc, 0, sizeof(struct reiserfs_journal_desc)) ;
  2547.   memcpy(desc->j_magic, JOURNAL_DESC_MAGIC, 8) ;
  2548.   desc->j_trans_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_trans_id) ;
  2549.   /* setup commit block.  Don't write (keep it clean too) this one until after everyone else is written */
  2550.   c_bh = sb_getblk(p_s_sb,  reiserfs_get_journal_block(p_s_sb) + 
  2551.            ((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 1) % JOURNAL_BLOCK_COUNT)) ;
  2552.   commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
  2553.   memset(commit, 0, sizeof(struct reiserfs_journal_commit)) ;
  2554.   commit->j_trans_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_trans_id) ;
  2555.   mark_buffer_uptodate(c_bh, 1) ;
  2556.   /* init this journal list */
  2557.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_older_commits_done), 0) ;
  2558.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
  2559.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_timestamp = SB_JOURNAL(p_s_sb)->j_trans_start_time ;
  2560.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_bh = c_bh ;
  2561.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_start = SB_JOURNAL(p_s_sb)->j_start ;
  2562.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_len = SB_JOURNAL(p_s_sb)->j_len ;  
  2563.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_nonzerolen), SB_JOURNAL(p_s_sb)->j_len) ;
  2564.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_left), SB_JOURNAL(p_s_sb)->j_len + 2);
  2565.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_realblock = NULL ;
  2566.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_flushing), 1) ;
  2567.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_flushing), 1) ;
  2568.   /* which is faster, locking/unlocking at the start and end of the for
  2569.   ** or locking once per iteration around the insert_journal_hash?
  2570.   ** eitherway, we are write locking insert_journal_hash.  The ENTIRE FOR
  2571.   ** LOOP MUST not cause schedule to occur.
  2572.   */
  2573.   /* for each real block, add it to the journal list hash,
  2574.   ** copy into real block index array in the commit or desc block
  2575.   */
  2576.   for (i = 0, cn = SB_JOURNAL(p_s_sb)->j_first ; cn ; cn = cn->next, i++) {
  2577.     if (test_bit(BH_JDirty, &cn->bh->b_state) ) {
  2578.       jl_cn = get_cnode(p_s_sb) ;
  2579.       if (!jl_cn) {
  2580.         reiserfs_panic(p_s_sb, "journal-1676, get_cnode returned NULLn") ;
  2581.       }
  2582.       if (i == 0) {
  2583.         SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_realblock = jl_cn ;
  2584.       }
  2585.       jl_cn->prev = last_cn ;
  2586.       jl_cn->next = NULL ;
  2587.       if (last_cn) {
  2588.         last_cn->next = jl_cn ;
  2589.       }
  2590.       last_cn = jl_cn ;
  2591.       if (cn->bh->b_blocknr >= reiserfs_get_journal_block(p_s_sb) &&
  2592.           cn->bh->b_blocknr < (reiserfs_get_journal_block(p_s_sb) + JOURNAL_BLOCK_COUNT)) {
  2593.         reiserfs_panic(p_s_sb, "journal-2332: Trying to log block %lu, which is a log blockn", cn->bh->b_blocknr) ;
  2594.       }
  2595.       jl_cn->blocknr = cn->bh->b_blocknr ; 
  2596.       jl_cn->state = 0 ;
  2597.       jl_cn->dev = cn->bh->b_dev ; 
  2598.       jl_cn->bh = cn->bh ;
  2599.       jl_cn->jlist = SB_JOURNAL_LIST(p_s_sb) + SB_JOURNAL_LIST_INDEX(p_s_sb) ;
  2600.       insert_journal_hash(SB_JOURNAL(p_s_sb)->j_list_hash_table, jl_cn) ; 
  2601.       if (i < JOURNAL_TRANS_HALF) {
  2602. desc->j_realblock[i] = cpu_to_le32(cn->bh->b_blocknr) ;
  2603.       } else {
  2604. commit->j_realblock[i - JOURNAL_TRANS_HALF] = cpu_to_le32(cn->bh->b_blocknr) ;
  2605.       }
  2606.     } else {
  2607.       i-- ;
  2608.     }
  2609.   }
  2610.   desc->j_len = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_len)  ;
  2611.   desc->j_mount_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_mount_id) ;
  2612.   desc->j_trans_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_trans_id) ;
  2613.   commit->j_len = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_len) ;
  2614.   /* special check in case all buffers in the journal were marked for not logging */
  2615.   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
  2616.     brelse(d_bh) ;
  2617.     brelse(c_bh) ;
  2618.     unlock_journal(p_s_sb) ;
  2619. printk("journal-2020: do_journal_end: BAD desc->j_len is ZEROn") ;
  2620.     atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
  2621.     wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
  2622.     return 0 ;
  2623.   }
  2624.   /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
  2625.   cur_write_start = SB_JOURNAL(p_s_sb)->j_start ;
  2626.   cur_blocks_left = SB_JOURNAL(p_s_sb)->j_len  ;
  2627.   cn = SB_JOURNAL(p_s_sb)->j_first ;
  2628.   jindex = 1 ; /* start at one so we don't get the desc again */
  2629.   while(cur_blocks_left > 0) {
  2630.     /* copy all the real blocks into log area.  dirty log blocks */
  2631.     if (test_bit(BH_JDirty, &cn->bh->b_state)) {
  2632.       struct buffer_head *tmp_bh ;
  2633.       tmp_bh = sb_getblk(p_s_sb, reiserfs_get_journal_block(p_s_sb) + 
  2634.      ((cur_write_start + jindex) % JOURNAL_BLOCK_COUNT)) ;
  2635.       mark_buffer_uptodate(tmp_bh, 1) ;
  2636.       memcpy(tmp_bh->b_data, cn->bh->b_data, cn->bh->b_size) ;  
  2637.       jindex++ ;
  2638.     } else {
  2639.       /* JDirty cleared sometime during transaction.  don't log this one */
  2640.       printk("journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!n") ;
  2641.     }
  2642.     cn = cn->next ;
  2643.     cur_blocks_left-- ;
  2644.   }
  2645.   /* we are done  with both the c_bh and d_bh, but
  2646.   ** c_bh must be written after all other commit blocks,
  2647.   ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
  2648.   */
  2649.   /* now loop through and mark all buffers from this transaction as JDirty_wait
  2650.   ** clear the JDirty bit, clear BH_JNew too.  
  2651.   ** if they weren't JDirty, they weren't logged, just relse them and move on
  2652.   */
  2653.   cn = SB_JOURNAL(p_s_sb)->j_first ; 
  2654.   while(cn) {
  2655.     clear_bit(BH_JNew, &(cn->bh->b_state)) ;
  2656.     if (test_bit(BH_JDirty, &(cn->bh->b_state))) {
  2657.       set_bit(BH_JDirty_wait, &(cn->bh->b_state)) ; 
  2658.       clear_bit(BH_JDirty, &(cn->bh->b_state)) ;
  2659.     } else {
  2660.       brelse(cn->bh) ;
  2661.     }
  2662.     next = cn->next ;
  2663.     free_cnode(p_s_sb, cn) ;
  2664.     cn = next ;
  2665.   }
  2666.   /* unlock the journal list for committing and flushing */
  2667.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_flushing), 0) ;
  2668.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_flushing), 0) ;
  2669.   orig_jindex = SB_JOURNAL_LIST_INDEX(p_s_sb) ;
  2670.   jindex = (SB_JOURNAL_LIST_INDEX(p_s_sb) + 1) % JOURNAL_LIST_COUNT ; 
  2671.   SB_JOURNAL_LIST_INDEX(p_s_sb) = jindex ;
  2672.   /* write any buffers that must hit disk before this commit is done */
  2673.   fsync_inode_buffers(&(SB_JOURNAL(p_s_sb)->j_dummy_inode)) ;
  2674.   /* honor the flush and async wishes from the caller */
  2675.   if (flush) {
  2676.   
  2677.     flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + orig_jindex, 1) ;
  2678.     flush_journal_list(p_s_sb,  SB_JOURNAL_LIST(p_s_sb) + orig_jindex , 1) ;  
  2679.   } else if (commit_now) {
  2680.     if (wait_on_commit) {
  2681.       flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + orig_jindex, 1) ;
  2682.     } else {
  2683.       commit_flush_async(p_s_sb, orig_jindex) ; 
  2684.     }
  2685.   }
  2686.   /* reset journal values for the next transaction */
  2687.   old_start = SB_JOURNAL(p_s_sb)->j_start ;
  2688.   SB_JOURNAL(p_s_sb)->j_start = (SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 2) % JOURNAL_BLOCK_COUNT;
  2689.   atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
  2690.   SB_JOURNAL(p_s_sb)->j_bcount = 0 ;
  2691.   SB_JOURNAL(p_s_sb)->j_last = NULL ;
  2692.   SB_JOURNAL(p_s_sb)->j_first = NULL ;
  2693.   SB_JOURNAL(p_s_sb)->j_len = 0 ;
  2694.   SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;
  2695.   SB_JOURNAL(p_s_sb)->j_trans_id++ ;
  2696.   SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
  2697.   SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
  2698.   SB_JOURNAL(p_s_sb)->j_next_full_flush = 0 ;
  2699.   SB_JOURNAL(p_s_sb)->j_next_async_flush = 0 ;
  2700.   init_journal_hash(p_s_sb) ; 
  2701.   /* if the next transaction has any chance of wrapping, flush 
  2702.   ** transactions that might get overwritten.  If any journal lists are very 
  2703.   ** old flush them as well.  
  2704.   */
  2705.   for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
  2706.     jindex = i ;
  2707.     if (SB_JOURNAL_LIST(p_s_sb)[jindex].j_len > 0 && SB_JOURNAL(p_s_sb)->j_start <= SB_JOURNAL_LIST(p_s_sb)[jindex].j_start) {
  2708.       if ((SB_JOURNAL(p_s_sb)->j_start + JOURNAL_TRANS_MAX + 1) >= SB_JOURNAL_LIST(p_s_sb)[jindex].j_start) {
  2709. flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1) ; 
  2710.       }
  2711.     } else if (SB_JOURNAL_LIST(p_s_sb)[jindex].j_len > 0 && 
  2712.               (SB_JOURNAL(p_s_sb)->j_start + JOURNAL_TRANS_MAX + 1) > JOURNAL_BLOCK_COUNT) {
  2713.       if (((SB_JOURNAL(p_s_sb)->j_start + JOURNAL_TRANS_MAX + 1) % JOURNAL_BLOCK_COUNT) >= 
  2714.             SB_JOURNAL_LIST(p_s_sb)[jindex].j_start) {
  2715. flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1 ) ; 
  2716.       }
  2717.     } 
  2718.     /* this check should always be run, to send old lists to disk */
  2719.     if (SB_JOURNAL_LIST(p_s_sb)[jindex].j_len > 0 && 
  2720.               SB_JOURNAL_LIST(p_s_sb)[jindex].j_timestamp < 
  2721.       (CURRENT_TIME - (JOURNAL_MAX_TRANS_AGE * 4))) {
  2722. flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1 ) ; 
  2723.     }
  2724.   }
  2725.   /* if the next journal_list is still in use, flush it */
  2726.   if (SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_len != 0) {
  2727.     flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + SB_JOURNAL_LIST_INDEX(p_s_sb), 1) ; 
  2728.   }
  2729.   /* we don't want anyone flushing the new transaction's list */
  2730.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_flushing), 1) ;
  2731.   atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_flushing), 1) ;
  2732.   SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_list_bitmap = get_list_bitmap(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + 
  2733.  SB_JOURNAL_LIST_INDEX(p_s_sb)) ;
  2734.   if (!(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_list_bitmap)) {
  2735.     reiserfs_panic(p_s_sb, "journal-1996: do_journal_end, could not get a list bitmapn") ;
  2736.   }
  2737.   unlock_journal(p_s_sb) ;
  2738.   atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
  2739.   /* wake up any body waiting to join. */
  2740.   wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
  2741.   return 0 ;
  2742. }