20 #include <linux/time.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
28 #include <linux/hrtimer.h>
31 static void __journal_temp_unlink_buffer(
struct journal_head *jh);
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
55 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires =
jiffies + journal->j_commit_interval;
60 journal->j_commit_timer.expires =
64 J_ASSERT(journal->j_running_transaction ==
NULL);
85 static int start_this_handle(journal_t *journal, handle_t *
handle)
89 int nblocks = handle->h_buffer_credits;
93 if (nblocks > journal->j_max_transaction_buffers) {
96 journal->j_max_transaction_buffers);
102 if (!journal->j_running_transaction) {
103 new_transaction = kzalloc(
sizeof(*new_transaction),
GFP_NOFS);
104 if (!new_transaction) {
106 goto alloc_transaction;
110 jbd_debug(3,
"New handle %p going live.\n", handle);
118 spin_lock(&journal->j_state_lock);
120 if (is_journal_aborted(journal) ||
121 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
122 spin_unlock(&journal->j_state_lock);
128 if (journal->j_barrier_count) {
129 spin_unlock(&journal->j_state_lock);
130 wait_event(journal->j_wait_transaction_locked,
131 journal->j_barrier_count == 0);
135 if (!journal->j_running_transaction) {
136 if (!new_transaction) {
137 spin_unlock(&journal->j_state_lock);
138 goto alloc_transaction;
140 get_transaction(journal, new_transaction);
141 new_transaction =
NULL;
144 transaction = journal->j_running_transaction;
150 if (transaction->t_state == T_LOCKED) {
155 spin_unlock(&journal->j_state_lock);
166 spin_lock(&transaction->t_handle_lock);
167 needed = transaction->t_outstanding_credits + nblocks;
169 if (needed > journal->j_max_transaction_buffers) {
177 jbd_debug(2,
"Handle %p starting new commit...\n", handle);
178 spin_unlock(&transaction->t_handle_lock);
182 spin_unlock(&journal->j_state_lock);
214 jbd_debug(2,
"Handle %p waiting for checkpoint...\n", handle);
215 spin_unlock(&transaction->t_handle_lock);
224 transaction->t_outstanding_credits += nblocks;
225 transaction->t_updates++;
226 transaction->t_handle_count++;
227 jbd_debug(4,
"Handle %p given %d credits (total %d, free %d)\n",
228 handle, nblocks, transaction->t_outstanding_credits,
230 spin_unlock(&transaction->t_handle_lock);
231 spin_unlock(&journal->j_state_lock);
236 kfree(new_transaction);
243 static handle_t *new_handle(
int nblocks)
245 handle_t *handle = jbd_alloc_handle(
GFP_NOFS);
248 memset(handle, 0,
sizeof(*handle));
249 handle->h_buffer_credits = nblocks;
274 handle_t *handle = journal_current_handle();
278 return ERR_PTR(-
EROFS);
281 J_ASSERT(handle->h_transaction->t_journal == journal);
286 handle = new_handle(nblocks);
292 err = start_this_handle(journal, handle);
294 jbd_free_handle(handle);
296 handle = ERR_PTR(err);
324 journal_t *journal = transaction->t_journal;
329 if (is_handle_aborted(handle))
334 spin_lock(&journal->j_state_lock);
337 if (handle->h_transaction->t_state != T_RUNNING) {
338 jbd_debug(3,
"denied handle %p %d blocks: "
339 "transaction not running\n", handle, nblocks);
343 spin_lock(&transaction->t_handle_lock);
344 wanted = transaction->t_outstanding_credits + nblocks;
346 if (wanted > journal->j_max_transaction_buffers) {
347 jbd_debug(3,
"denied handle %p %d blocks: "
348 "transaction too large\n", handle, nblocks);
353 jbd_debug(3,
"denied handle %p %d blocks: "
354 "insufficient log space\n", handle, nblocks);
358 handle->h_buffer_credits += nblocks;
359 transaction->t_outstanding_credits += nblocks;
362 jbd_debug(3,
"extended handle %p by %d\n", handle, nblocks);
364 spin_unlock(&transaction->t_handle_lock);
366 spin_unlock(&journal->j_state_lock);
390 journal_t *journal = transaction->t_journal;
395 if (is_handle_aborted(handle))
402 J_ASSERT(transaction->t_updates > 0);
403 J_ASSERT(journal_current_handle() == handle);
405 spin_lock(&journal->j_state_lock);
406 spin_lock(&transaction->t_handle_lock);
407 transaction->t_outstanding_credits -= handle->h_buffer_credits;
408 transaction->t_updates--;
410 if (!transaction->t_updates)
411 wake_up(&journal->j_wait_updates);
412 spin_unlock(&transaction->t_handle_lock);
414 jbd_debug(2,
"restarting handle %p\n", handle);
416 spin_unlock(&journal->j_state_lock);
419 handle->h_buffer_credits = nblocks;
420 ret = start_this_handle(journal, handle);
445 wait_event(journal->j_wait_transaction_locked,
446 journal->j_barrier_count == 0);
448 spin_lock(&journal->j_state_lock);
453 if (journal->j_barrier_count > 0) {
454 spin_unlock(&journal->j_state_lock);
457 ++journal->j_barrier_count;
466 spin_lock(&transaction->t_handle_lock);
467 if (!transaction->t_updates) {
468 spin_unlock(&transaction->t_handle_lock);
473 spin_unlock(&transaction->t_handle_lock);
474 spin_unlock(&journal->j_state_lock);
477 spin_lock(&journal->j_state_lock);
479 spin_unlock(&journal->j_state_lock);
490 J_ASSERT(journal->j_barrier_count != 0);
492 spin_lock(&journal->j_state_lock);
493 --journal->j_barrier_count;
494 spin_unlock(&journal->j_state_lock);
495 wake_up(&journal->j_wait_transaction_locked);
498 static void warn_dirty_buffer(
struct buffer_head *bh)
503 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
504 "There's a risk of filesystem corruption in case of system "
506 bdevname(bh->b_bdev, b), (
unsigned long long)bh->b_blocknr);
520 do_get_write_access(handle_t *handle,
struct journal_head *jh,
523 struct buffer_head *bh;
527 char *frozen_buffer =
NULL;
530 if (is_handle_aborted(handle))
533 transaction = handle->h_transaction;
534 journal = transaction->t_journal;
536 jbd_debug(5,
"journal_head %p, force_copy %d\n", jh, force_copy);
538 JBUFFER_TRACE(jh,
"entry");
545 jbd_lock_bh_state(bh);
560 if (buffer_dirty(bh)) {
569 journal->j_committing_transaction);
573 warn_dirty_buffer(bh);
580 JBUFFER_TRACE(jh,
"Journalling dirty buffer");
581 clear_buffer_dirty(bh);
582 set_buffer_jbddirty(bh);
588 if (is_handle_aborted(handle)) {
589 jbd_unlock_bh_state(bh);
613 JBUFFER_TRACE(jh,
"has frozen data");
622 JBUFFER_TRACE(jh,
"owned by older transaction");
625 journal->j_committing_transaction);
642 JBUFFER_TRACE(jh,
"on shadow: sleep");
643 jbd_unlock_bh_state(bh);
671 JBUFFER_TRACE(jh,
"generate frozen data");
672 if (!frozen_buffer) {
673 JBUFFER_TRACE(jh,
"allocate memory for buffer");
674 jbd_unlock_bh_state(bh);
676 jbd_alloc(jh2bh(jh)->b_size,
678 if (!frozen_buffer) {
680 "%s: OOM for frozen_buffer\n",
682 JBUFFER_TRACE(jh,
"oom!");
684 jbd_lock_bh_state(bh);
690 frozen_buffer =
NULL;
703 JBUFFER_TRACE(jh,
"no transaction");
705 JBUFFER_TRACE(jh,
"file as BJ_Reserved");
706 spin_lock(&journal->j_list_lock);
708 spin_unlock(&journal->j_list_lock);
717 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
718 "Possible IO failure.\n");
719 page = jh2bh(jh)->b_page;
725 jbd_unlock_bh_state(bh);
731 journal_cancel_revoke(handle, jh);
735 jbd_free(frozen_buffer, bh->b_size);
737 JBUFFER_TRACE(jh,
"exit");
760 rc = do_get_write_access(handle, jh, 0);
788 journal_t *journal = transaction->t_journal;
794 if (is_handle_aborted(handle))
798 JBUFFER_TRACE(jh,
"entry");
806 jbd_lock_bh_state(bh);
807 spin_lock(&journal->j_list_lock);
814 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
825 clear_buffer_dirty(jh2bh(jh));
830 JBUFFER_TRACE(jh,
"file as BJ_Reserved");
832 }
else if (jh->
b_transaction == journal->j_committing_transaction) {
836 JBUFFER_TRACE(jh,
"set next transaction");
839 spin_unlock(&journal->j_list_lock);
840 jbd_unlock_bh_state(bh);
849 JBUFFER_TRACE(jh,
"cancelling revoke");
850 journal_cancel_revoke(handle, jh);
885 char *committed_data =
NULL;
887 JBUFFER_TRACE(jh,
"entry");
894 err = do_get_write_access(handle, jh, 1);
900 committed_data = jbd_alloc(jh2bh(jh)->b_size,
GFP_NOFS);
901 if (!committed_data) {
909 jbd_lock_bh_state(bh);
913 JBUFFER_TRACE(jh,
"generate b_committed data");
914 if (!committed_data) {
915 jbd_unlock_bh_state(bh);
920 committed_data =
NULL;
923 jbd_unlock_bh_state(bh);
927 jbd_free(committed_data, bh->b_size);
950 journal_t *journal = handle->h_transaction->t_journal;
955 if (is_handle_aborted(handle))
959 JBUFFER_TRACE(jh,
"entry");
965 jbd_debug(4,
"jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
988 jbd_lock_bh_state(bh);
989 spin_lock(&journal->j_list_lock);
992 if (!buffer_mapped(bh)) {
993 JBUFFER_TRACE(jh,
"unmapped buffer, bailing out");
998 JBUFFER_TRACE(jh,
"has transaction");
1000 JBUFFER_TRACE(jh,
"belongs to older transaction");
1002 journal->j_committing_transaction);
1040 JBUFFER_TRACE(jh,
"Not stealing");
1050 if (buffer_dirty(bh)) {
1052 spin_unlock(&journal->j_list_lock);
1053 jbd_unlock_bh_state(bh);
1056 jbd_lock_bh_state(bh);
1057 spin_lock(&journal->j_list_lock);
1059 if (!buffer_mapped(bh)) {
1060 JBUFFER_TRACE(jh,
"buffer got unmapped");
1072 if (
unlikely(!buffer_uptodate(bh))) {
1079 JBUFFER_TRACE(jh,
"unfile from commit");
1080 __journal_temp_unlink_buffer(jh);
1097 JBUFFER_TRACE(jh,
"not on correct data list: unfile");
1099 JBUFFER_TRACE(jh,
"file as data");
1104 JBUFFER_TRACE(jh,
"not on a transaction");
1108 spin_unlock(&journal->j_list_lock);
1109 jbd_unlock_bh_state(bh);
1111 BUFFER_TRACE(bh,
"brelse");
1114 JBUFFER_TRACE(jh,
"exit");
1141 journal_t *journal = transaction->t_journal;
1145 JBUFFER_TRACE(jh,
"entry");
1146 if (is_handle_aborted(handle))
1149 jbd_lock_bh_state(bh);
1158 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1159 handle->h_buffer_credits--;
1170 JBUFFER_TRACE(jh,
"fastpath");
1172 journal->j_running_transaction);
1176 set_buffer_jbddirty(bh);
1185 JBUFFER_TRACE(jh,
"already on other transaction");
1187 journal->j_committing_transaction);
1197 JBUFFER_TRACE(jh,
"file as BJ_Metadata");
1198 spin_lock(&journal->j_list_lock);
1200 spin_unlock(&journal->j_list_lock);
1202 jbd_unlock_bh_state(bh);
1204 JBUFFER_TRACE(jh,
"exit");
1216 BUFFER_TRACE(bh,
"entry");
1239 journal_t *journal = transaction->t_journal;
1241 int drop_reserve = 0;
1243 int was_modified = 0;
1245 BUFFER_TRACE(bh,
"entry");
1247 jbd_lock_bh_state(bh);
1248 spin_lock(&journal->j_list_lock);
1250 if (!buffer_jbd(bh))
1257 "inconsistent data on disk")) {
1277 clear_buffer_dirty(bh);
1278 clear_buffer_jbddirty(bh);
1280 JBUFFER_TRACE(jh,
"belongs to current transaction: unfile");
1302 __journal_temp_unlink_buffer(jh);
1306 if (!buffer_jbd(bh)) {
1307 spin_unlock(&journal->j_list_lock);
1308 jbd_unlock_bh_state(bh);
1315 journal->j_committing_transaction));
1318 JBUFFER_TRACE(jh,
"belongs to older transaction");
1336 spin_unlock(&journal->j_list_lock);
1337 jbd_unlock_bh_state(bh);
1342 handle->h_buffer_credits++;
1366 journal_t *journal = transaction->t_journal;
1370 J_ASSERT(journal_current_handle() == handle);
1372 if (is_handle_aborted(handle))
1375 J_ASSERT(transaction->t_updates > 0);
1379 if (--handle->h_ref > 0) {
1380 jbd_debug(4,
"h_ref %d -> %d\n", handle->h_ref + 1,
1385 jbd_debug(4,
"Handle %p going down\n", handle);
1413 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1414 u64 commit_time, trans_time;
1416 journal->j_last_sync_writer =
pid;
1418 spin_lock(&journal->j_state_lock);
1419 commit_time = journal->j_average_commit_time;
1420 spin_unlock(&journal->j_state_lock);
1422 trans_time = ktime_to_ns(ktime_sub(
ktime_get(),
1423 transaction->t_start_time));
1425 commit_time =
min_t(
u64, commit_time,
1428 if (trans_time < commit_time) {
1437 spin_lock(&journal->j_state_lock);
1438 spin_lock(&transaction->t_handle_lock);
1439 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1440 transaction->t_updates--;
1441 if (!transaction->t_updates) {
1442 wake_up(&journal->j_wait_updates);
1443 if (journal->j_barrier_count)
1444 wake_up(&journal->j_wait_transaction_locked);
1453 if (handle->h_sync ||
1454 transaction->t_outstanding_credits >
1455 journal->j_max_transaction_buffers ||
1462 spin_unlock(&transaction->t_handle_lock);
1463 jbd_debug(2,
"transaction too old, requesting commit for "
1464 "handle %p\n", handle);
1467 spin_unlock(&journal->j_state_lock);
1476 spin_unlock(&transaction->t_handle_lock);
1477 spin_unlock(&journal->j_state_lock);
1482 jbd_free_handle(handle);
1500 if (IS_ERR(handle)) {
1501 ret = PTR_ERR(handle);
1536 last->b_tnext = first->
b_tprev = jh;
1572 static void __journal_temp_unlink_buffer(
struct journal_head *jh)
1576 struct buffer_head *bh = jh2bh(jh);
1578 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1585 J_ASSERT_JH(jh, transaction !=
NULL);
1591 list = &transaction->t_sync_datalist;
1594 transaction->t_nr_buffers--;
1595 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1596 list = &transaction->t_buffers;
1599 list = &transaction->t_forget;
1602 list = &transaction->t_iobuf_list;
1605 list = &transaction->t_shadow_list;
1608 list = &transaction->t_log_list;
1611 list = &transaction->t_reserved_list;
1614 list = &transaction->t_locked_list;
1618 __blist_del_buffer(list, jh);
1620 if (test_clear_buffer_jbddirty(bh))
1633 __journal_temp_unlink_buffer(jh);
1640 struct buffer_head *bh = jh2bh(jh);
1644 jbd_lock_bh_state(bh);
1645 spin_lock(&journal->j_list_lock);
1647 spin_unlock(&journal->j_list_lock);
1648 jbd_unlock_bh_state(bh);
1658 __journal_try_to_free_buffer(journal_t *journal,
struct buffer_head *bh)
1664 if (buffer_locked(bh) || buffer_dirty(bh))
1670 spin_lock(&journal->j_list_lock);
1674 JBUFFER_TRACE(jh,
"release data");
1680 JBUFFER_TRACE(jh,
"remove from checkpoint list");
1684 spin_unlock(&journal->j_list_lock);
1730 struct buffer_head *
head;
1731 struct buffer_head *bh;
1734 J_ASSERT(PageLocked(page));
1736 head = page_buffers(page);
1750 jbd_lock_bh_state(bh);
1751 __journal_try_to_free_buffer(journal, bh);
1753 jbd_unlock_bh_state(bh);
1756 }
while ((bh = bh->b_this_page) != head);
1779 struct buffer_head *bh = jh2bh(jh);
1782 JBUFFER_TRACE(jh,
"on running+cp transaction");
1783 __journal_temp_unlink_buffer(jh);
1789 clear_buffer_dirty(bh);
1793 JBUFFER_TRACE(jh,
"on running transaction");
1846 static int journal_unmap_buffer(journal_t *journal,
struct buffer_head *bh,
1853 BUFFER_TRACE(bh,
"entry");
1862 if (!buffer_jbd(bh))
1863 goto zap_buffer_unlocked;
1865 spin_lock(&journal->j_state_lock);
1866 jbd_lock_bh_state(bh);
1867 spin_lock(&journal->j_list_lock);
1871 goto zap_buffer_no_jh;
1897 if (transaction ==
NULL) {
1903 JBUFFER_TRACE(jh,
"not on any transaction: zap");
1907 if (!buffer_dirty(bh)) {
1916 if (journal->j_running_transaction) {
1920 JBUFFER_TRACE(jh,
"checkpointed: add to BJ_Forget");
1921 may_free = __dispose_buffer(jh,
1922 journal->j_running_transaction);
1929 if (journal->j_committing_transaction) {
1930 JBUFFER_TRACE(jh,
"give to committing trans");
1931 may_free = __dispose_buffer(jh,
1932 journal->j_committing_transaction);
1937 clear_buffer_jbddirty(bh);
1941 }
else if (transaction == journal->j_committing_transaction) {
1942 JBUFFER_TRACE(jh,
"on committing transaction");
1949 may_free = __dispose_buffer(jh, transaction);
1958 tid_t tid = journal->j_committing_transaction->t_tid;
1961 spin_unlock(&journal->j_list_lock);
1962 jbd_unlock_bh_state(bh);
1963 spin_unlock(&journal->j_state_lock);
1975 set_buffer_freed(bh);
1976 if (journal->j_running_transaction && buffer_jbddirty(bh))
1979 spin_unlock(&journal->j_list_lock);
1980 jbd_unlock_bh_state(bh);
1981 spin_unlock(&journal->j_state_lock);
1990 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1991 JBUFFER_TRACE(jh,
"on running transaction");
1992 may_free = __dispose_buffer(jh, transaction);
2006 spin_unlock(&journal->j_list_lock);
2007 jbd_unlock_bh_state(bh);
2008 spin_unlock(&journal->j_state_lock);
2009 zap_buffer_unlocked:
2010 clear_buffer_dirty(bh);
2011 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2012 clear_buffer_mapped(bh);
2013 clear_buffer_req(bh);
2014 clear_buffer_new(bh);
2029 unsigned long offset)
2031 struct buffer_head *
head, *bh, *
next;
2032 unsigned int curr_off = 0;
2035 if (!PageLocked(page))
2037 if (!page_has_buffers(page))
2044 head = bh = page_buffers(page);
2046 unsigned int next_off = curr_off + bh->b_size;
2047 next = bh->b_this_page;
2049 if (offset <= curr_off) {
2052 may_free &= journal_unmap_buffer(journal, bh,
2056 curr_off = next_off;
2059 }
while (bh != head);
2063 J_ASSERT(!page_has_buffers(page));
2075 struct buffer_head *bh = jh2bh(jh);
2077 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2096 if (buffer_dirty(bh))
2097 warn_dirty_buffer(bh);
2098 if (test_clear_buffer_dirty(bh) ||
2099 test_clear_buffer_jbddirty(bh))
2104 __journal_temp_unlink_buffer(jh);
2115 list = &transaction->t_sync_datalist;
2118 transaction->t_nr_buffers++;
2119 list = &transaction->t_buffers;
2122 list = &transaction->t_forget;
2125 list = &transaction->t_iobuf_list;
2128 list = &transaction->t_shadow_list;
2131 list = &transaction->t_log_list;
2134 list = &transaction->t_reserved_list;
2137 list = &transaction->t_locked_list;
2141 __blist_add_buffer(list, jh);
2145 set_buffer_jbddirty(bh);
2151 jbd_lock_bh_state(jh2bh(jh));
2152 spin_lock(&transaction->t_journal->j_list_lock);
2154 spin_unlock(&transaction->t_journal->j_list_lock);
2155 jbd_unlock_bh_state(jh2bh(jh));
2171 int was_dirty, jlist;
2172 struct buffer_head *bh = jh2bh(jh);
2174 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2189 was_dirty = test_clear_buffer_jbddirty(bh);
2190 __journal_temp_unlink_buffer(jh);
2198 if (buffer_freed(bh))
2208 set_buffer_jbddirty(bh);
2219 struct buffer_head *bh = jh2bh(jh);
2223 jbd_lock_bh_state(bh);
2224 spin_lock(&journal->j_list_lock);
2226 jbd_unlock_bh_state(bh);
2227 spin_unlock(&journal->j_list_lock);