1838
1838
* We're outside-transaction here. Either or both of j_running_transaction
1839
1839
* and j_committing_transaction may be NULL.
1841
static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1841
static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
1843
1844
transaction_t *transaction;
1844
1845
struct journal_head *jh;
1845
1846
int may_free = 1;
1848
1848
BUFFER_TRACE(bh, "entry");
1851
1852
* It is safe to proceed here without the j_list_lock because the
1852
1853
* buffers cannot be stolen by try_to_free_buffers as long as we are
1865
1866
goto zap_buffer_no_jh;
1869
* We cannot remove the buffer from checkpoint lists until the
1870
* transaction adding inode to orphan list (let's call it T)
1871
* is committed. Otherwise if the transaction changing the
1872
* buffer would be cleaned from the journal before T is
1873
* committed, a crash will cause that the correct contents of
1874
* the buffer will be lost. On the other hand we have to
1875
* clear the buffer dirty bit at latest at the moment when the
1876
* transaction marking the buffer as freed in the filesystem
1877
* structures is committed because from that moment on the
1878
* block can be reallocated and used by a different page.
1879
* Since the block hasn't been freed yet but the inode has
1880
* already been added to orphan list, it is safe for us to add
1881
* the buffer to BJ_Forget list of the newest transaction.
1883
* Also we have to clear buffer_mapped flag of a truncated buffer
1884
* because the buffer_head may be attached to the page straddling
1885
* i_size (can happen only when blocksize < pagesize) and thus the
1886
* buffer_head can be reused when the file is extended again. So we end
1887
* up keeping around invalidated buffers attached to transactions'
1888
* BJ_Forget list just to stop checkpointing code from cleaning up
1889
* the transaction this buffer was modified in.
1867
1891
transaction = jh->b_transaction;
1868
1892
if (transaction == NULL) {
1869
1893
/* First case: not on any transaction. If it
1889
1913
* committed, the buffer won't be needed any
1891
1915
JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1892
ret = __dispose_buffer(jh,
1916
may_free = __dispose_buffer(jh,
1893
1917
journal->j_running_transaction);
1894
journal_put_journal_head(jh);
1895
spin_unlock(&journal->j_list_lock);
1896
jbd_unlock_bh_state(bh);
1897
spin_unlock(&journal->j_state_lock);
1900
1920
/* There is no currently-running transaction. So the
1901
1921
* orphan record which we wrote for this file must have
1903
1923
* the committing transaction, if it exists. */
1904
1924
if (journal->j_committing_transaction) {
1905
1925
JBUFFER_TRACE(jh, "give to committing trans");
1906
ret = __dispose_buffer(jh,
1926
may_free = __dispose_buffer(jh,
1907
1927
journal->j_committing_transaction);
1908
journal_put_journal_head(jh);
1909
spin_unlock(&journal->j_list_lock);
1910
jbd_unlock_bh_state(bh);
1911
spin_unlock(&journal->j_state_lock);
1914
1930
/* The orphan record's transaction has
1915
1931
* committed. We can cleanse this buffer */
1929
1945
goto zap_buffer;
1932
* If it is committing, we simply cannot touch it. We
1933
* can remove it's next_transaction pointer from the
1934
* running transaction if that is set, but nothing
1948
* The buffer is committing, we simply cannot touch
1949
* it. If the page is straddling i_size we have to wait
1950
* for commit and try again.
1953
tid_t tid = journal->j_committing_transaction->t_tid;
1955
journal_put_journal_head(jh);
1956
spin_unlock(&journal->j_list_lock);
1957
jbd_unlock_bh_state(bh);
1958
spin_unlock(&journal->j_state_lock);
1960
log_wait_commit(journal, tid);
1965
* OK, buffer won't be reachable after truncate. We just set
1966
* j_next_transaction to the running transaction (if there is
1967
* one) and mark buffer as freed so that commit code knows it
1968
* should clear dirty bits when it is done with the buffer.
1936
1970
set_buffer_freed(bh);
1937
if (jh->b_next_transaction) {
1938
J_ASSERT(jh->b_next_transaction ==
1939
journal->j_running_transaction);
1940
jh->b_next_transaction = NULL;
1971
if (journal->j_running_transaction && buffer_jbddirty(bh))
1972
jh->b_next_transaction = journal->j_running_transaction;
1942
1973
journal_put_journal_head(jh);
1943
1974
spin_unlock(&journal->j_list_lock);
1944
1975
jbd_unlock_bh_state(bh);
1992
* This is tricky. Although the buffer is truncated, it may be reused
1993
* if blocksize < pagesize and it is attached to the page straddling
1994
* EOF. Since the buffer might have been added to BJ_Forget list of the
1995
* running transaction, journal_get_write_access() won't clear
1996
* b_modified and credit accounting gets confused. So clear b_modified
1960
1999
journal_put_journal_head(jh);
1961
2000
zap_buffer_no_jh:
1962
2001
spin_unlock(&journal->j_list_lock);
2005
2044
if (offset <= curr_off) {
2006
2045
/* This block is wholly outside the truncation point */
2007
2046
lock_buffer(bh);
2008
may_free &= journal_unmap_buffer(journal, bh);
2047
may_free &= journal_unmap_buffer(journal, bh,
2009
2049
unlock_buffer(bh);
2011
2051
curr_off = next_off;
2142
2182
__journal_temp_unlink_buffer(jh);
2143
2183
jh->b_transaction = jh->b_next_transaction;
2144
2184
jh->b_next_transaction = NULL;
2145
__journal_file_buffer(jh, jh->b_transaction,
2146
jh->b_modified ? BJ_Metadata : BJ_Reserved);
2185
if (buffer_freed(bh))
2187
else if (jh->b_modified)
2188
jlist = BJ_Metadata;
2190
jlist = BJ_Reserved;
2191
__journal_file_buffer(jh, jh->b_transaction, jlist);
2147
2192
J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);