20 #include <linux/time.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
28 #include <linux/hrtimer.h>
31 #include <linux/module.h>
33 static void __jbd2_journal_temp_unlink_buffer(
struct journal_head *jh);
34 static void __jbd2_journal_unfile_buffer(
struct journal_head *jh);
39 J_ASSERT(!transaction_cache);
45 if (transaction_cache)
52 if (transaction_cache) {
54 transaction_cache =
NULL;
83 transaction->t_journal = journal;
84 transaction->t_state = T_RUNNING;
86 transaction->t_tid = journal->j_transaction_sequence++;
87 transaction->t_expires =
jiffies + journal->j_commit_interval;
90 atomic_set(&transaction->t_outstanding_credits, 0);
92 INIT_LIST_HEAD(&transaction->t_inode_list);
93 INIT_LIST_HEAD(&transaction->t_private_list);
99 J_ASSERT(journal->j_running_transaction ==
NULL);
101 transaction->t_max_wait = 0;
102 transaction->t_start =
jiffies;
128 #ifdef CONFIG_JBD2_DEBUG
129 if (jbd2_journal_enable_debug &&
131 ts = jbd2_time_diff(ts, transaction->t_start);
132 spin_lock(&transaction->t_handle_lock);
133 if (ts > transaction->t_max_wait)
134 transaction->t_max_wait =
ts;
135 spin_unlock(&transaction->t_handle_lock);
147 static int start_this_handle(journal_t *journal, handle_t *
handle,
152 int needed, need_to_start;
153 int nblocks = handle->h_buffer_credits;
156 if (nblocks > journal->j_max_transaction_buffers) {
159 journal->j_max_transaction_buffers);
164 if (!journal->j_running_transaction) {
165 new_transaction = kmem_cache_zalloc(transaction_cache,
167 if (!new_transaction) {
177 goto alloc_transaction;
183 jbd_debug(3,
"New handle %p going live.\n", handle);
191 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
192 if (is_journal_aborted(journal) ||
193 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
200 if (journal->j_barrier_count) {
202 wait_event(journal->j_wait_transaction_locked,
203 journal->j_barrier_count == 0);
207 if (!journal->j_running_transaction) {
209 if (!new_transaction)
210 goto alloc_transaction;
212 if (!journal->j_running_transaction) {
213 jbd2_get_transaction(journal, new_transaction);
214 new_transaction =
NULL;
220 transaction = journal->j_running_transaction;
226 if (transaction->t_state == T_LOCKED) {
243 &transaction->t_outstanding_credits);
245 if (needed > journal->j_max_transaction_buffers) {
253 jbd_debug(2,
"Handle %p starting new commit...\n", handle);
254 atomic_sub(nblocks, &transaction->t_outstanding_credits);
257 tid = transaction->t_tid;
258 need_to_start = !tid_geq(journal->j_commit_request, tid);
293 jbd_debug(2,
"Handle %p waiting for checkpoint...\n", handle);
294 atomic_sub(nblocks, &transaction->t_outstanding_credits);
306 update_t_max_wait(transaction, ts);
310 jbd_debug(4,
"Handle %p given %d credits (total %d, free %d)\n",
324 static handle_t *new_handle(
int nblocks)
326 handle_t *handle = jbd2_alloc_handle(
GFP_NOFS);
329 memset(handle, 0,
sizeof(*handle));
330 handle->h_buffer_credits = nblocks;
334 &jbd2_handle_key, 0);
356 handle_t *handle = journal_current_handle();
360 return ERR_PTR(-
EROFS);
363 J_ASSERT(handle->h_transaction->t_journal == journal);
368 handle = new_handle(nblocks);
374 err = start_this_handle(journal, handle, gfp_mask);
376 jbd2_free_handle(handle);
378 handle = ERR_PTR(err);
415 journal_t *journal = transaction->t_journal;
420 if (is_handle_aborted(handle))
428 if (handle->h_transaction->t_state != T_RUNNING) {
429 jbd_debug(3,
"denied handle %p %d blocks: "
430 "transaction not running\n", handle, nblocks);
434 spin_lock(&transaction->t_handle_lock);
435 wanted =
atomic_read(&transaction->t_outstanding_credits) + nblocks;
437 if (wanted > journal->j_max_transaction_buffers) {
438 jbd_debug(3,
"denied handle %p %d blocks: "
439 "transaction too large\n", handle, nblocks);
444 jbd_debug(3,
"denied handle %p %d blocks: "
445 "insufficient log space\n", handle, nblocks);
449 handle->h_buffer_credits += nblocks;
450 atomic_add(nblocks, &transaction->t_outstanding_credits);
453 jbd_debug(3,
"extended handle %p by %d\n", handle, nblocks);
455 spin_unlock(&transaction->t_handle_lock);
480 journal_t *journal = transaction->t_journal;
482 int need_to_start,
ret;
486 if (is_handle_aborted(handle))
493 J_ASSERT(
atomic_read(&transaction->t_updates) > 0);
494 J_ASSERT(journal_current_handle() == handle);
497 spin_lock(&transaction->t_handle_lock);
499 &transaction->t_outstanding_credits);
501 wake_up(&journal->j_wait_updates);
502 spin_unlock(&transaction->t_handle_lock);
504 jbd_debug(2,
"restarting handle %p\n", handle);
505 tid = transaction->t_tid;
506 need_to_start = !tid_geq(journal->j_commit_request, tid);
512 handle->h_buffer_credits = nblocks;
513 ret = start_this_handle(journal, handle, gfp_mask);
540 ++journal->j_barrier_count;
549 spin_lock(&transaction->t_handle_lock);
553 spin_unlock(&transaction->t_handle_lock);
557 spin_unlock(&transaction->t_handle_lock);
584 J_ASSERT(journal->j_barrier_count != 0);
588 --journal->j_barrier_count;
590 wake_up(&journal->j_wait_transaction_locked);
593 static void warn_dirty_buffer(
struct buffer_head *bh)
598 "JBD2: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
599 "There's a risk of filesystem corruption in case of system "
601 bdevname(bh->b_bdev, b), (
unsigned long long)bh->b_blocknr);
615 do_get_write_access(handle_t *handle,
struct journal_head *jh,
618 struct buffer_head *bh;
622 char *frozen_buffer =
NULL;
625 if (is_handle_aborted(handle))
628 transaction = handle->h_transaction;
629 journal = transaction->t_journal;
631 jbd_debug(5,
"journal_head %p, force_copy %d\n", jh, force_copy);
633 JBUFFER_TRACE(jh,
"entry");
640 jbd_lock_bh_state(bh);
655 if (buffer_dirty(bh)) {
664 journal->j_committing_transaction);
668 warn_dirty_buffer(bh);
675 JBUFFER_TRACE(jh,
"Journalling dirty buffer");
676 clear_buffer_dirty(bh);
677 set_buffer_jbddirty(bh);
683 if (is_handle_aborted(handle)) {
684 jbd_unlock_bh_state(bh);
708 JBUFFER_TRACE(jh,
"has frozen data");
717 JBUFFER_TRACE(jh,
"owned by older transaction");
720 journal->j_committing_transaction);
737 JBUFFER_TRACE(jh,
"on shadow: sleep");
738 jbd_unlock_bh_state(bh);
766 JBUFFER_TRACE(jh,
"generate frozen data");
767 if (!frozen_buffer) {
768 JBUFFER_TRACE(jh,
"allocate memory for buffer");
769 jbd_unlock_bh_state(bh);
773 if (!frozen_buffer) {
775 "%s: OOM for frozen_buffer\n",
777 JBUFFER_TRACE(jh,
"oom!");
779 jbd_lock_bh_state(bh);
785 frozen_buffer =
NULL;
798 JBUFFER_TRACE(jh,
"no transaction");
800 JBUFFER_TRACE(jh,
"file as BJ_Reserved");
801 spin_lock(&journal->j_list_lock);
803 spin_unlock(&journal->j_list_lock);
812 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
813 "Possible IO failure.\n");
814 page = jh2bh(jh)->b_page;
829 jbd_unlock_bh_state(bh);
835 jbd2_journal_cancel_revoke(handle, jh);
841 JBUFFER_TRACE(jh,
"exit");
864 rc = do_get_write_access(handle, jh, 0);
892 journal_t *journal = transaction->t_journal;
898 if (is_handle_aborted(handle))
902 JBUFFER_TRACE(jh,
"entry");
910 jbd_lock_bh_state(bh);
911 spin_lock(&journal->j_list_lock);
918 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
929 clear_buffer_dirty(jh2bh(jh));
933 JBUFFER_TRACE(jh,
"file as BJ_Reserved");
935 }
else if (jh->
b_transaction == journal->j_committing_transaction) {
939 JBUFFER_TRACE(jh,
"set next transaction");
942 spin_unlock(&journal->j_list_lock);
943 jbd_unlock_bh_state(bh);
952 JBUFFER_TRACE(jh,
"cancelling revoke");
953 jbd2_journal_cancel_revoke(handle, jh);
989 char *committed_data =
NULL;
991 JBUFFER_TRACE(jh,
"entry");
998 err = do_get_write_access(handle, jh, 1);
1005 if (!committed_data) {
1013 jbd_lock_bh_state(bh);
1017 JBUFFER_TRACE(jh,
"generate b_committed data");
1018 if (!committed_data) {
1019 jbd_unlock_bh_state(bh);
1024 committed_data =
NULL;
1027 jbd_unlock_bh_state(bh);
1047 struct jbd2_buffer_trigger_type *
type)
1055 struct jbd2_buffer_trigger_type *triggers)
1057 struct buffer_head *bh = jh2bh(jh);
1059 if (!triggers || !triggers->t_frozen)
1062 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1066 struct jbd2_buffer_trigger_type *triggers)
1068 if (!triggers || !triggers->t_abort)
1071 triggers->t_abort(triggers, jh2bh(jh));
1102 journal_t *journal = transaction->t_journal;
1107 JBUFFER_TRACE(jh,
"entry");
1108 if (is_handle_aborted(handle))
1110 if (!buffer_jbd(bh)) {
1115 jbd_lock_bh_state(bh);
1124 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1125 handle->h_buffer_credits--;
1136 JBUFFER_TRACE(jh,
"fastpath");
1138 journal->j_running_transaction)) {
1140 "jh->b_transaction (%llu, %p, %u) != "
1141 "journal->j_running_transaction (%p, %u)",
1143 (
unsigned long long) bh->b_blocknr,
1146 journal->j_running_transaction,
1147 journal->j_running_transaction ?
1148 journal->j_running_transaction->t_tid : 0);
1154 set_buffer_jbddirty(bh);
1163 JBUFFER_TRACE(jh,
"already on other transaction");
1165 journal->j_committing_transaction)) {
1167 "jh->b_transaction (%llu, %p, %u) != "
1168 "journal->j_committing_transaction (%p, %u)",
1170 (
unsigned long long) bh->b_blocknr,
1173 journal->j_committing_transaction,
1174 journal->j_committing_transaction ?
1175 journal->j_committing_transaction->t_tid : 0);
1180 "jh->b_next_transaction (%llu, %p, %u) != "
1181 "transaction (%p, %u)",
1183 (
unsigned long long) bh->b_blocknr,
1187 transaction, transaction->t_tid);
1198 JBUFFER_TRACE(jh,
"file as BJ_Metadata");
1199 spin_lock(&journal->j_list_lock);
1201 spin_unlock(&journal->j_list_lock);
1203 jbd_unlock_bh_state(bh);
1205 JBUFFER_TRACE(jh,
"exit");
1218 BUFFER_TRACE(bh,
"entry");
1241 journal_t *journal = transaction->t_journal;
1243 int drop_reserve = 0;
1245 int was_modified = 0;
1247 BUFFER_TRACE(bh,
"entry");
1249 jbd_lock_bh_state(bh);
1250 spin_lock(&journal->j_list_lock);
1252 if (!buffer_jbd(bh))
1259 "inconsistent data on disk")) {
1279 clear_buffer_dirty(bh);
1280 clear_buffer_jbddirty(bh);
1282 JBUFFER_TRACE(jh,
"belongs to current transaction: unfile");
1304 __jbd2_journal_temp_unlink_buffer(jh);
1307 __jbd2_journal_unfile_buffer(jh);
1308 if (!buffer_jbd(bh)) {
1309 spin_unlock(&journal->j_list_lock);
1310 jbd_unlock_bh_state(bh);
1317 journal->j_committing_transaction));
1320 JBUFFER_TRACE(jh,
"belongs to older transaction");
1338 spin_unlock(&journal->j_list_lock);
1339 jbd_unlock_bh_state(bh);
1344 handle->h_buffer_credits++;
1368 journal_t *journal = transaction->t_journal;
1369 int err, wait_for_commit = 0;
1373 J_ASSERT(journal_current_handle() == handle);
1375 if (is_handle_aborted(handle))
1378 J_ASSERT(
atomic_read(&transaction->t_updates) > 0);
1382 if (--handle->h_ref > 0) {
1383 jbd_debug(4,
"h_ref %d -> %d\n", handle->h_ref + 1,
1388 jbd_debug(4,
"Handle %p going down\n", handle);
1418 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1419 u64 commit_time, trans_time;
1421 journal->j_last_sync_writer =
pid;
1424 commit_time = journal->j_average_commit_time;
1427 trans_time = ktime_to_ns(ktime_sub(
ktime_get(),
1428 transaction->t_start_time));
1430 commit_time =
max_t(
u64, commit_time,
1431 1000*journal->j_min_batch_time);
1432 commit_time =
min_t(
u64, commit_time,
1433 1000*journal->j_max_batch_time);
1435 if (trans_time < commit_time) {
1444 transaction->t_synchronous_commit = 1;
1447 &transaction->t_outstanding_credits);
1455 if (handle->h_sync ||
1456 (
atomic_read(&transaction->t_outstanding_credits) >
1457 journal->j_max_transaction_buffers) ||
1463 jbd_debug(2,
"transaction too old, requesting commit for "
1464 "handle %p\n", handle);
1473 wait_for_commit = 1;
1482 tid = transaction->t_tid;
1484 wake_up(&journal->j_wait_updates);
1485 if (journal->j_barrier_count)
1486 wake_up(&journal->j_wait_transaction_locked);
1489 if (wait_for_commit)
1494 jbd2_free_handle(handle);
1512 if (IS_ERR(handle)) {
1513 ret = PTR_ERR(handle);
1548 last->b_tnext = first->
b_tprev = jh;
1584 static void __jbd2_journal_temp_unlink_buffer(
struct journal_head *jh)
1588 struct buffer_head *bh = jh2bh(jh);
1590 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1597 J_ASSERT_JH(jh, transaction !=
NULL);
1603 transaction->t_nr_buffers--;
1604 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1605 list = &transaction->t_buffers;
1608 list = &transaction->t_forget;
1611 list = &transaction->t_iobuf_list;
1614 list = &transaction->t_shadow_list;
1617 list = &transaction->t_log_list;
1620 list = &transaction->t_reserved_list;
1624 __blist_del_buffer(list, jh);
1626 if (test_clear_buffer_jbddirty(bh))
1637 static void __jbd2_journal_unfile_buffer(
struct journal_head *jh)
1639 __jbd2_journal_temp_unlink_buffer(jh);
1646 struct buffer_head *bh = jh2bh(jh);
1650 jbd_lock_bh_state(bh);
1651 spin_lock(&journal->j_list_lock);
1652 __jbd2_journal_unfile_buffer(jh);
1653 spin_unlock(&journal->j_list_lock);
1654 jbd_unlock_bh_state(bh);
1664 __journal_try_to_free_buffer(journal_t *journal,
struct buffer_head *bh)
1670 if (buffer_locked(bh) || buffer_dirty(bh))
1676 spin_lock(&journal->j_list_lock);
1679 JBUFFER_TRACE(jh,
"remove from checkpoint list");
1682 spin_unlock(&journal->j_list_lock);
1726 struct page *page,
gfp_t gfp_mask)
1728 struct buffer_head *
head;
1729 struct buffer_head *bh;
1732 J_ASSERT(PageLocked(page));
1734 head = page_buffers(page);
1748 jbd_lock_bh_state(bh);
1749 __journal_try_to_free_buffer(journal, bh);
1751 jbd_unlock_bh_state(bh);
1754 }
while ((bh = bh->b_this_page) != head);
1777 struct buffer_head *bh = jh2bh(jh);
1780 JBUFFER_TRACE(jh,
"on running+cp transaction");
1781 __jbd2_journal_temp_unlink_buffer(jh);
1787 clear_buffer_dirty(bh);
1791 JBUFFER_TRACE(jh,
"on running transaction");
1792 __jbd2_journal_unfile_buffer(jh);
1844 static int journal_unmap_buffer(journal_t *journal,
struct buffer_head *bh,
1851 BUFFER_TRACE(bh,
"entry");
1860 if (!buffer_jbd(bh))
1861 goto zap_buffer_unlocked;
1865 jbd_lock_bh_state(bh);
1866 spin_lock(&journal->j_list_lock);
1870 goto zap_buffer_no_jh;
1896 if (transaction ==
NULL) {
1902 JBUFFER_TRACE(jh,
"not on any transaction: zap");
1906 if (!buffer_dirty(bh)) {
1915 if (journal->j_running_transaction) {
1919 JBUFFER_TRACE(jh,
"checkpointed: add to BJ_Forget");
1920 may_free = __dispose_buffer(jh,
1921 journal->j_running_transaction);
1928 if (journal->j_committing_transaction) {
1929 JBUFFER_TRACE(jh,
"give to committing trans");
1930 may_free = __dispose_buffer(jh,
1931 journal->j_committing_transaction);
1936 clear_buffer_jbddirty(bh);
1940 }
else if (transaction == journal->j_committing_transaction) {
1941 JBUFFER_TRACE(jh,
"on committing transaction");
1948 tid_t tid = journal->j_committing_transaction->t_tid;
1951 spin_unlock(&journal->j_list_lock);
1952 jbd_unlock_bh_state(bh);
1963 set_buffer_freed(bh);
1964 if (journal->j_running_transaction && buffer_jbddirty(bh))
1967 spin_unlock(&journal->j_list_lock);
1968 jbd_unlock_bh_state(bh);
1978 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1979 JBUFFER_TRACE(jh,
"on running transaction");
1980 may_free = __dispose_buffer(jh, transaction);
1995 spin_unlock(&journal->j_list_lock);
1996 jbd_unlock_bh_state(bh);
1998 zap_buffer_unlocked:
1999 clear_buffer_dirty(bh);
2000 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2001 clear_buffer_mapped(bh);
2002 clear_buffer_req(bh);
2003 clear_buffer_new(bh);
2004 clear_buffer_delay(bh);
2005 clear_buffer_unwritten(bh);
2021 unsigned long offset)
2023 struct buffer_head *
head, *bh, *
next;
2024 unsigned int curr_off = 0;
2027 if (!PageLocked(page))
2029 if (!page_has_buffers(page))
2036 head = bh = page_buffers(page);
2038 unsigned int next_off = curr_off + bh->b_size;
2039 next = bh->b_this_page;
2041 if (offset <= curr_off) {
2044 may_free &= journal_unmap_buffer(journal, bh,
2048 curr_off = next_off;
2051 }
while (bh != head);
2055 J_ASSERT(!page_has_buffers(page));
2067 struct buffer_head *bh = jh2bh(jh);
2069 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2088 if (buffer_dirty(bh))
2089 warn_dirty_buffer(bh);
2090 if (test_clear_buffer_dirty(bh) ||
2091 test_clear_buffer_jbddirty(bh))
2096 __jbd2_journal_temp_unlink_buffer(jh);
2107 transaction->t_nr_buffers++;
2108 list = &transaction->t_buffers;
2111 list = &transaction->t_forget;
2114 list = &transaction->t_iobuf_list;
2117 list = &transaction->t_shadow_list;
2120 list = &transaction->t_log_list;
2123 list = &transaction->t_reserved_list;
2127 __blist_add_buffer(list, jh);
2131 set_buffer_jbddirty(bh);
2137 jbd_lock_bh_state(jh2bh(jh));
2138 spin_lock(&transaction->t_journal->j_list_lock);
2140 spin_unlock(&transaction->t_journal->j_list_lock);
2141 jbd_unlock_bh_state(jh2bh(jh));
2157 int was_dirty, jlist;
2158 struct buffer_head *bh = jh2bh(jh);
2160 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2166 __jbd2_journal_unfile_buffer(jh);
2175 was_dirty = test_clear_buffer_jbddirty(bh);
2176 __jbd2_journal_temp_unlink_buffer(jh);
2184 if (buffer_freed(bh))
2194 set_buffer_jbddirty(bh);
2205 struct buffer_head *bh = jh2bh(jh);
2209 jbd_lock_bh_state(bh);
2210 spin_lock(&journal->j_list_lock);
2212 jbd_unlock_bh_state(bh);
2213 spin_unlock(&journal->j_list_lock);
2223 journal_t *journal = transaction->t_journal;
2225 if (is_handle_aborted(handle))
2228 jbd_debug(4,
"Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2229 transaction->t_tid);
2244 if (jinode->i_transaction == transaction ||
2245 jinode->i_next_transaction == transaction)
2248 spin_lock(&journal->j_list_lock);
2250 if (jinode->i_transaction == transaction ||
2251 jinode->i_next_transaction == transaction)
2259 if (!transaction->t_need_data_flush)
2260 transaction->t_need_data_flush = 1;
2263 if (jinode->i_transaction) {
2264 J_ASSERT(jinode->i_next_transaction ==
NULL);
2265 J_ASSERT(jinode->i_transaction ==
2266 journal->j_committing_transaction);
2271 J_ASSERT(!jinode->i_next_transaction);
2273 list_add(&jinode->i_list, &transaction->t_inode_list);
2275 spin_unlock(&journal->j_list_lock);
2301 struct jbd2_inode *jinode,
2308 if (!jinode->i_transaction)
2314 commit_trans = journal->j_committing_transaction;
2316 spin_lock(&journal->j_list_lock);
2317 inode_trans = jinode->i_transaction;
2318 spin_unlock(&journal->j_list_lock);
2319 if (inode_trans == commit_trans) {