@@ -1843,15 +1843,16 @@ static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
18431843 * We're outside-transaction here. Either or both of j_running_transaction
18441844 * and j_committing_transaction may be NULL.
18451845 */
1846- static int journal_unmap_buffer (journal_t * journal , struct buffer_head * bh )
1846+ static int journal_unmap_buffer (journal_t * journal , struct buffer_head * bh ,
1847+ int partial_page )
18471848{
18481849 transaction_t * transaction ;
18491850 struct journal_head * jh ;
18501851 int may_free = 1 ;
1851- int ret ;
18521852
18531853 BUFFER_TRACE (bh , "entry" );
18541854
1855+ retry :
18551856 /*
18561857 * It is safe to proceed here without the j_list_lock because the
18571858 * buffers cannot be stolen by try_to_free_buffers as long as we are
@@ -1879,10 +1880,18 @@ static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
18791880 * clear the buffer dirty bit at latest at the moment when the
18801881 * transaction marking the buffer as freed in the filesystem
18811882 * structures is committed because from that moment on the
1882- * buffer can be reallocated and used by a different page.
1883+ * block can be reallocated and used by a different page.
18831884 * Since the block hasn't been freed yet but the inode has
18841885 * already been added to orphan list, it is safe for us to add
18851886 * the buffer to BJ_Forget list of the newest transaction.
1887+ *
1888+ * Also we have to clear buffer_mapped flag of a truncated buffer
1889+ * because the buffer_head may be attached to the page straddling
1890+ * i_size (can happen only when blocksize < pagesize) and thus the
1891+ * buffer_head can be reused when the file is extended again. So we end
1892+ * up keeping around invalidated buffers attached to transactions'
1893+ * BJ_Forget list just to stop checkpointing code from cleaning up
1894+ * the transaction this buffer was modified in.
18861895 */
18871896 transaction = jh -> b_transaction ;
18881897 if (transaction == NULL ) {
@@ -1909,27 +1918,19 @@ static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
19091918 * committed, the buffer won't be needed any
19101919 * longer. */
19111920 JBUFFER_TRACE (jh , "checkpointed: add to BJ_Forget" );
1912- ret = __dispose_buffer (jh ,
1921+ may_free = __dispose_buffer (jh ,
19131922 journal -> j_running_transaction );
1914- journal_put_journal_head (jh );
1915- spin_unlock (& journal -> j_list_lock );
1916- jbd_unlock_bh_state (bh );
1917- spin_unlock (& journal -> j_state_lock );
1918- return ret ;
1923+ goto zap_buffer ;
19191924 } else {
19201925 /* There is no currently-running transaction. So the
19211926 * orphan record which we wrote for this file must have
19221927 * passed into commit. We must attach this buffer to
19231928 * the committing transaction, if it exists. */
19241929 if (journal -> j_committing_transaction ) {
19251930 JBUFFER_TRACE (jh , "give to committing trans" );
1926- ret = __dispose_buffer (jh ,
1931+ may_free = __dispose_buffer (jh ,
19271932 journal -> j_committing_transaction );
1928- journal_put_journal_head (jh );
1929- spin_unlock (& journal -> j_list_lock );
1930- jbd_unlock_bh_state (bh );
1931- spin_unlock (& journal -> j_state_lock );
1932- return ret ;
1933+ goto zap_buffer ;
19331934 } else {
19341935 /* The orphan record's transaction has
19351936 * committed. We can cleanse this buffer */
@@ -1950,10 +1951,24 @@ static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
19501951 }
19511952 /*
19521953 * The buffer is committing, we simply cannot touch
1953- * it. So we just set j_next_transaction to the
1954- * running transaction (if there is one) and mark
1955- * buffer as freed so that commit code knows it should
1956- * clear dirty bits when it is done with the buffer.
1954+ * it. If the page is straddling i_size we have to wait
1955+ * for commit and try again.
1956+ */
1957+ if (partial_page ) {
1958+ tid_t tid = journal -> j_committing_transaction -> t_tid ;
1959+
1960+ journal_put_journal_head (jh );
1961+ spin_unlock (& journal -> j_list_lock );
1962+ jbd_unlock_bh_state (bh );
1963+ spin_unlock (& journal -> j_state_lock );
1964+ log_wait_commit (journal , tid );
1965+ goto retry ;
1966+ }
1967+ /*
1968+ * OK, buffer won't be reachable after truncate. We just set
1969+ * j_next_transaction to the running transaction (if there is
1970+ * one) and mark buffer as freed so that commit code knows it
1971+ * should clear dirty bits when it is done with the buffer.
19571972 */
19581973 set_buffer_freed (bh );
19591974 if (journal -> j_running_transaction && buffer_jbddirty (bh ))
@@ -1976,6 +1991,14 @@ static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
19761991 }
19771992
19781993zap_buffer :
1994+ /*
1995+ * This is tricky. Although the buffer is truncated, it may be reused
1996+ * if blocksize < pagesize and it is attached to the page straddling
1997+ * EOF. Since the buffer might have been added to BJ_Forget list of the
1998+ * running transaction, journal_get_write_access() won't clear
1999+ * b_modified and credit accounting gets confused. So clear b_modified
2000+ * here. */
2001+ jh -> b_modified = 0 ;
19792002 journal_put_journal_head (jh );
19802003zap_buffer_no_jh :
19812004 spin_unlock (& journal -> j_list_lock );
@@ -2024,7 +2047,8 @@ void journal_invalidatepage(journal_t *journal,
20242047 if (offset <= curr_off ) {
20252048 /* This block is wholly outside the truncation point */
20262049 lock_buffer (bh );
2027- may_free &= journal_unmap_buffer (journal , bh );
2050+ may_free &= journal_unmap_buffer (journal , bh ,
2051+ offset > 0 );
20282052 unlock_buffer (bh );
20292053 }
20302054 curr_off = next_off ;
0 commit comments