]>
Commit | Line | Data |
---|---|---|
470decc6 | 1 | /* |
f7f4bccb | 2 | * linux/fs/jbd2/commit.c |
470decc6 DK |
3 | * |
4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 | |
5 | * | |
6 | * Copyright 1998 Red Hat corp --- All Rights Reserved | |
7 | * | |
8 | * This file is part of the Linux kernel and is made available under | |
9 | * the terms of the GNU General Public License, version 2, or at your | |
10 | * option, any later version, incorporated herein by reference. | |
11 | * | |
12 | * Journal commit routines for the generic filesystem journaling code; | |
13 | * part of the ext2fs journaling system. | |
14 | */ | |
15 | ||
16 | #include <linux/time.h> | |
17 | #include <linux/fs.h> | |
f7f4bccb | 18 | #include <linux/jbd2.h> |
470decc6 DK |
19 | #include <linux/errno.h> |
20 | #include <linux/slab.h> | |
21 | #include <linux/mm.h> | |
22 | #include <linux/pagemap.h> | |
8e85fb3f | 23 | #include <linux/jiffies.h> |
818d276c | 24 | #include <linux/crc32.h> |
cd1aac32 AK |
25 | #include <linux/writeback.h> |
26 | #include <linux/backing-dev.h> | |
470decc6 DK |
27 | |
28 | /* | |
29 | * Default IO end handler for temporary BJ_IO buffer_heads. | |
30 | */ | |
31 | static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate) | |
32 | { | |
33 | BUFFER_TRACE(bh, ""); | |
34 | if (uptodate) | |
35 | set_buffer_uptodate(bh); | |
36 | else | |
37 | clear_buffer_uptodate(bh); | |
38 | unlock_buffer(bh); | |
39 | } | |
40 | ||
41 | /* | |
87c89c23 JK |
42 | * When an ext4 file is truncated, it is possible that some pages are not |
43 | * successfully freed, because they are attached to a committing transaction. | |
470decc6 DK |
44 | * After the transaction commits, these pages are left on the LRU, with no |
45 | * ->mapping, and with attached buffers. These pages are trivially reclaimable | |
46 | * by the VM, but their apparent absence upsets the VM accounting, and it makes | |
47 | * the numbers in /proc/meminfo look odd. | |
48 | * | |
49 | * So here, we have a buffer which has just come off the forget list. Look to | |
50 | * see if we can strip all buffers from the backing page. | |
51 | * | |
52 | * Called under lock_journal(), and possibly under journal_datalist_lock. The | |
53 | * caller provided us with a ref against the buffer, and we drop that here. | |
54 | */ | |
55 | static void release_buffer_page(struct buffer_head *bh) | |
56 | { | |
57 | struct page *page; | |
58 | ||
59 | if (buffer_dirty(bh)) | |
60 | goto nope; | |
61 | if (atomic_read(&bh->b_count) != 1) | |
62 | goto nope; | |
63 | page = bh->b_page; | |
64 | if (!page) | |
65 | goto nope; | |
66 | if (page->mapping) | |
67 | goto nope; | |
68 | ||
69 | /* OK, it's a truncated page */ | |
70 | if (TestSetPageLocked(page)) | |
71 | goto nope; | |
72 | ||
73 | page_cache_get(page); | |
74 | __brelse(bh); | |
75 | try_to_free_buffers(page); | |
76 | unlock_page(page); | |
77 | page_cache_release(page); | |
78 | return; | |
79 | ||
80 | nope: | |
81 | __brelse(bh); | |
82 | } | |
83 | ||
818d276c GS |
84 | /* |
85 | * Done it all: now submit the commit record. We should have | |
470decc6 DK |
86 | * cleaned up our previous buffers by now, so if we are in abort |
87 | * mode we can now just skip the rest of the journal write | |
88 | * entirely. | |
89 | * | |
90 | * Returns 1 if the journal needs to be aborted or 0 on success | |
91 | */ | |
818d276c GS |
92 | static int journal_submit_commit_record(journal_t *journal, |
93 | transaction_t *commit_transaction, | |
94 | struct buffer_head **cbh, | |
95 | __u32 crc32_sum) | |
470decc6 DK |
96 | { |
97 | struct journal_head *descriptor; | |
818d276c | 98 | struct commit_header *tmp; |
470decc6 | 99 | struct buffer_head *bh; |
818d276c | 100 | int ret; |
470decc6 | 101 | int barrier_done = 0; |
736603ab | 102 | struct timespec now = current_kernel_time(); |
470decc6 DK |
103 | |
104 | if (is_journal_aborted(journal)) | |
105 | return 0; | |
106 | ||
f7f4bccb | 107 | descriptor = jbd2_journal_get_descriptor_buffer(journal); |
470decc6 DK |
108 | if (!descriptor) |
109 | return 1; | |
110 | ||
111 | bh = jh2bh(descriptor); | |
112 | ||
818d276c GS |
113 | tmp = (struct commit_header *)bh->b_data; |
114 | tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); | |
115 | tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK); | |
116 | tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid); | |
736603ab TT |
117 | tmp->h_commit_sec = cpu_to_be64(now.tv_sec); |
118 | tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec); | |
818d276c GS |
119 | |
120 | if (JBD2_HAS_COMPAT_FEATURE(journal, | |
121 | JBD2_FEATURE_COMPAT_CHECKSUM)) { | |
122 | tmp->h_chksum_type = JBD2_CRC32_CHKSUM; | |
123 | tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE; | |
124 | tmp->h_chksum[0] = cpu_to_be32(crc32_sum); | |
470decc6 DK |
125 | } |
126 | ||
818d276c GS |
127 | JBUFFER_TRACE(descriptor, "submit commit block"); |
128 | lock_buffer(bh); | |
c4b8e635 | 129 | get_bh(bh); |
470decc6 | 130 | set_buffer_dirty(bh); |
818d276c GS |
131 | set_buffer_uptodate(bh); |
132 | bh->b_end_io = journal_end_buffer_io_sync; | |
133 | ||
134 | if (journal->j_flags & JBD2_BARRIER && | |
4d605179 | 135 | !JBD2_HAS_INCOMPAT_FEATURE(journal, |
818d276c | 136 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { |
470decc6 DK |
137 | set_buffer_ordered(bh); |
138 | barrier_done = 1; | |
139 | } | |
818d276c | 140 | ret = submit_bh(WRITE, bh); |
c4e35e07 DK |
141 | if (barrier_done) |
142 | clear_buffer_ordered(bh); | |
818d276c | 143 | |
470decc6 DK |
144 | /* is it possible for another commit to fail at roughly |
145 | * the same time as this one? If so, we don't want to | |
146 | * trust the barrier flag in the super, but instead want | |
147 | * to remember if we sent a barrier request | |
148 | */ | |
149 | if (ret == -EOPNOTSUPP && barrier_done) { | |
150 | char b[BDEVNAME_SIZE]; | |
151 | ||
152 | printk(KERN_WARNING | |
153 | "JBD: barrier-based sync failed on %s - " | |
154 | "disabling barriers\n", | |
155 | bdevname(journal->j_dev, b)); | |
156 | spin_lock(&journal->j_state_lock); | |
f7f4bccb | 157 | journal->j_flags &= ~JBD2_BARRIER; |
470decc6 DK |
158 | spin_unlock(&journal->j_state_lock); |
159 | ||
160 | /* And try again, without the barrier */ | |
034772b0 | 161 | lock_buffer(bh); |
470decc6 DK |
162 | set_buffer_uptodate(bh); |
163 | set_buffer_dirty(bh); | |
818d276c | 164 | ret = submit_bh(WRITE, bh); |
470decc6 | 165 | } |
818d276c GS |
166 | *cbh = bh; |
167 | return ret; | |
168 | } | |
169 | ||
170 | /* | |
171 | * This function along with journal_submit_commit_record | |
172 | * allows to write the commit record asynchronously. | |
173 | */ | |
174 | static int journal_wait_on_commit_record(struct buffer_head *bh) | |
175 | { | |
176 | int ret = 0; | |
177 | ||
178 | clear_buffer_dirty(bh); | |
179 | wait_on_buffer(bh); | |
470decc6 | 180 | |
818d276c GS |
181 | if (unlikely(!buffer_uptodate(bh))) |
182 | ret = -EIO; | |
183 | put_bh(bh); /* One for getblk() */ | |
184 | jbd2_journal_put_journal_head(bh2jh(bh)); | |
185 | ||
186 | return ret; | |
470decc6 DK |
187 | } |
188 | ||
cd1aac32 AK |
189 | /* |
190 | * write the filemap data using writepage() address_space_operations. | |
191 | * We don't do block allocation here even for delalloc. We don't | |
192 | * use writepages() because with dealyed allocation we may be doing | |
193 | * block allocation in writepages(). | |
194 | */ | |
195 | static int journal_submit_inode_data_buffers(struct address_space *mapping) | |
196 | { | |
197 | int ret; | |
198 | struct writeback_control wbc = { | |
199 | .sync_mode = WB_SYNC_ALL, | |
200 | .nr_to_write = mapping->nrpages * 2, | |
201 | .range_start = 0, | |
202 | .range_end = i_size_read(mapping->host), | |
203 | .for_writepages = 1, | |
204 | }; | |
205 | ||
206 | ret = generic_writepages(mapping, &wbc); | |
207 | return ret; | |
208 | } | |
209 | ||
c851ed54 JK |
210 | /* |
211 | * Submit all the data buffers of inode associated with the transaction to | |
212 | * disk. | |
213 | * | |
214 | * We are in a committing transaction. Therefore no new inode can be added to | |
215 | * our inode list. We use JI_COMMIT_RUNNING flag to protect inode we currently | |
216 | * operate on from being released while we write out pages. | |
217 | */ | |
cd1aac32 | 218 | static int journal_submit_data_buffers(journal_t *journal, |
c851ed54 JK |
219 | transaction_t *commit_transaction) |
220 | { | |
221 | struct jbd2_inode *jinode; | |
222 | int err, ret = 0; | |
223 | struct address_space *mapping; | |
224 | ||
225 | spin_lock(&journal->j_list_lock); | |
226 | list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { | |
227 | mapping = jinode->i_vfs_inode->i_mapping; | |
228 | jinode->i_flags |= JI_COMMIT_RUNNING; | |
229 | spin_unlock(&journal->j_list_lock); | |
cd1aac32 AK |
230 | /* |
231 | * submit the inode data buffers. We use writepage | |
232 | * instead of writepages. Because writepages can do | |
233 | * block allocation with delalloc. We need to write | |
234 | * only allocated blocks here. | |
235 | */ | |
236 | err = journal_submit_inode_data_buffers(mapping); | |
c851ed54 JK |
237 | if (!ret) |
238 | ret = err; | |
239 | spin_lock(&journal->j_list_lock); | |
240 | J_ASSERT(jinode->i_transaction == commit_transaction); | |
241 | jinode->i_flags &= ~JI_COMMIT_RUNNING; | |
242 | wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); | |
243 | } | |
244 | spin_unlock(&journal->j_list_lock); | |
245 | return ret; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Wait for data submitted for writeout, refile inodes to proper | |
250 | * transaction if needed. | |
251 | * | |
252 | */ | |
253 | static int journal_finish_inode_data_buffers(journal_t *journal, | |
254 | transaction_t *commit_transaction) | |
255 | { | |
256 | struct jbd2_inode *jinode, *next_i; | |
257 | int err, ret = 0; | |
258 | ||
cd1aac32 | 259 | /* For locking, see the comment in journal_submit_data_buffers() */ |
c851ed54 JK |
260 | spin_lock(&journal->j_list_lock); |
261 | list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { | |
262 | jinode->i_flags |= JI_COMMIT_RUNNING; | |
263 | spin_unlock(&journal->j_list_lock); | |
264 | err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping); | |
265 | if (!ret) | |
266 | ret = err; | |
267 | spin_lock(&journal->j_list_lock); | |
268 | jinode->i_flags &= ~JI_COMMIT_RUNNING; | |
269 | wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); | |
270 | } | |
271 | ||
272 | /* Now refile inode to proper lists */ | |
273 | list_for_each_entry_safe(jinode, next_i, | |
274 | &commit_transaction->t_inode_list, i_list) { | |
275 | list_del(&jinode->i_list); | |
276 | if (jinode->i_next_transaction) { | |
277 | jinode->i_transaction = jinode->i_next_transaction; | |
278 | jinode->i_next_transaction = NULL; | |
279 | list_add(&jinode->i_list, | |
280 | &jinode->i_transaction->t_inode_list); | |
281 | } else { | |
282 | jinode->i_transaction = NULL; | |
283 | } | |
284 | } | |
285 | spin_unlock(&journal->j_list_lock); | |
286 | ||
287 | return ret; | |
288 | } | |
289 | ||
818d276c GS |
290 | static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh) |
291 | { | |
292 | struct page *page = bh->b_page; | |
293 | char *addr; | |
294 | __u32 checksum; | |
295 | ||
296 | addr = kmap_atomic(page, KM_USER0); | |
297 | checksum = crc32_be(crc32_sum, | |
298 | (void *)(addr + offset_in_page(bh->b_data)), bh->b_size); | |
299 | kunmap_atomic(addr, KM_USER0); | |
300 | ||
301 | return checksum; | |
302 | } | |
303 | ||
304 | static void write_tag_block(int tag_bytes, journal_block_tag_t *tag, | |
18eba7aa | 305 | unsigned long long block) |
b517bea1 ZB |
306 | { |
307 | tag->t_blocknr = cpu_to_be32(block & (u32)~0); | |
cd02ff0b | 308 | if (tag_bytes > JBD2_TAG_SIZE32) |
b517bea1 ZB |
309 | tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1); |
310 | } | |
311 | ||
470decc6 | 312 | /* |
f7f4bccb | 313 | * jbd2_journal_commit_transaction |
470decc6 DK |
314 | * |
315 | * The primary function for committing a transaction to the log. This | |
316 | * function is called by the journal thread to begin a complete commit. | |
317 | */ | |
f7f4bccb | 318 | void jbd2_journal_commit_transaction(journal_t *journal) |
470decc6 | 319 | { |
8e85fb3f | 320 | struct transaction_stats_s stats; |
470decc6 DK |
321 | transaction_t *commit_transaction; |
322 | struct journal_head *jh, *new_jh, *descriptor; | |
323 | struct buffer_head **wbuf = journal->j_wbuf; | |
324 | int bufs; | |
325 | int flags; | |
326 | int err; | |
18eba7aa | 327 | unsigned long long blocknr; |
470decc6 DK |
328 | char *tagp = NULL; |
329 | journal_header_t *header; | |
330 | journal_block_tag_t *tag = NULL; | |
331 | int space_left = 0; | |
332 | int first_tag = 0; | |
333 | int tag_flag; | |
334 | int i; | |
b517bea1 | 335 | int tag_bytes = journal_tag_bytes(journal); |
818d276c GS |
336 | struct buffer_head *cbh = NULL; /* For transactional checksums */ |
337 | __u32 crc32_sum = ~0; | |
470decc6 DK |
338 | |
339 | /* | |
340 | * First job: lock down the current transaction and wait for | |
341 | * all outstanding updates to complete. | |
342 | */ | |
343 | ||
344 | #ifdef COMMIT_STATS | |
345 | spin_lock(&journal->j_list_lock); | |
346 | summarise_journal_usage(journal); | |
347 | spin_unlock(&journal->j_list_lock); | |
348 | #endif | |
349 | ||
f7f4bccb MC |
350 | /* Do we need to erase the effects of a prior jbd2_journal_flush? */ |
351 | if (journal->j_flags & JBD2_FLUSHED) { | |
470decc6 | 352 | jbd_debug(3, "super block updated\n"); |
f7f4bccb | 353 | jbd2_journal_update_superblock(journal, 1); |
470decc6 DK |
354 | } else { |
355 | jbd_debug(3, "superblock not updated\n"); | |
356 | } | |
357 | ||
358 | J_ASSERT(journal->j_running_transaction != NULL); | |
359 | J_ASSERT(journal->j_committing_transaction == NULL); | |
360 | ||
361 | commit_transaction = journal->j_running_transaction; | |
362 | J_ASSERT(commit_transaction->t_state == T_RUNNING); | |
363 | ||
364 | jbd_debug(1, "JBD: starting commit of transaction %d\n", | |
365 | commit_transaction->t_tid); | |
366 | ||
367 | spin_lock(&journal->j_state_lock); | |
368 | commit_transaction->t_state = T_LOCKED; | |
369 | ||
8e85fb3f JL |
370 | stats.u.run.rs_wait = commit_transaction->t_max_wait; |
371 | stats.u.run.rs_locked = jiffies; | |
372 | stats.u.run.rs_running = jbd2_time_diff(commit_transaction->t_start, | |
373 | stats.u.run.rs_locked); | |
374 | ||
470decc6 DK |
375 | spin_lock(&commit_transaction->t_handle_lock); |
376 | while (commit_transaction->t_updates) { | |
377 | DEFINE_WAIT(wait); | |
378 | ||
379 | prepare_to_wait(&journal->j_wait_updates, &wait, | |
380 | TASK_UNINTERRUPTIBLE); | |
381 | if (commit_transaction->t_updates) { | |
382 | spin_unlock(&commit_transaction->t_handle_lock); | |
383 | spin_unlock(&journal->j_state_lock); | |
384 | schedule(); | |
385 | spin_lock(&journal->j_state_lock); | |
386 | spin_lock(&commit_transaction->t_handle_lock); | |
387 | } | |
388 | finish_wait(&journal->j_wait_updates, &wait); | |
389 | } | |
390 | spin_unlock(&commit_transaction->t_handle_lock); | |
391 | ||
392 | J_ASSERT (commit_transaction->t_outstanding_credits <= | |
393 | journal->j_max_transaction_buffers); | |
394 | ||
395 | /* | |
396 | * First thing we are allowed to do is to discard any remaining | |
397 | * BJ_Reserved buffers. Note, it is _not_ permissible to assume | |
398 | * that there are no such buffers: if a large filesystem | |
399 | * operation like a truncate needs to split itself over multiple | |
f7f4bccb | 400 | * transactions, then it may try to do a jbd2_journal_restart() while |
470decc6 DK |
401 | * there are still BJ_Reserved buffers outstanding. These must |
402 | * be released cleanly from the current transaction. | |
403 | * | |
404 | * In this case, the filesystem must still reserve write access | |
405 | * again before modifying the buffer in the new transaction, but | |
406 | * we do not require it to remember exactly which old buffers it | |
407 | * has reserved. This is consistent with the existing behaviour | |
f7f4bccb | 408 | * that multiple jbd2_journal_get_write_access() calls to the same |
470decc6 DK |
409 | * buffer are perfectly permissable. |
410 | */ | |
411 | while (commit_transaction->t_reserved_list) { | |
412 | jh = commit_transaction->t_reserved_list; | |
413 | JBUFFER_TRACE(jh, "reserved, unused: refile"); | |
414 | /* | |
f7f4bccb | 415 | * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may |
470decc6 DK |
416 | * leave undo-committed data. |
417 | */ | |
418 | if (jh->b_committed_data) { | |
419 | struct buffer_head *bh = jh2bh(jh); | |
420 | ||
421 | jbd_lock_bh_state(bh); | |
af1e76d6 | 422 | jbd2_free(jh->b_committed_data, bh->b_size); |
470decc6 DK |
423 | jh->b_committed_data = NULL; |
424 | jbd_unlock_bh_state(bh); | |
425 | } | |
f7f4bccb | 426 | jbd2_journal_refile_buffer(journal, jh); |
470decc6 DK |
427 | } |
428 | ||
429 | /* | |
430 | * Now try to drop any written-back buffers from the journal's | |
431 | * checkpoint lists. We do this *before* commit because it potentially | |
432 | * frees some memory | |
433 | */ | |
434 | spin_lock(&journal->j_list_lock); | |
f7f4bccb | 435 | __jbd2_journal_clean_checkpoint_list(journal); |
470decc6 DK |
436 | spin_unlock(&journal->j_list_lock); |
437 | ||
438 | jbd_debug (3, "JBD: commit phase 1\n"); | |
439 | ||
440 | /* | |
441 | * Switch to a new revoke table. | |
442 | */ | |
f7f4bccb | 443 | jbd2_journal_switch_revoke_table(journal); |
470decc6 | 444 | |
8e85fb3f JL |
445 | stats.u.run.rs_flushing = jiffies; |
446 | stats.u.run.rs_locked = jbd2_time_diff(stats.u.run.rs_locked, | |
447 | stats.u.run.rs_flushing); | |
448 | ||
470decc6 DK |
449 | commit_transaction->t_state = T_FLUSH; |
450 | journal->j_committing_transaction = commit_transaction; | |
451 | journal->j_running_transaction = NULL; | |
452 | commit_transaction->t_log_start = journal->j_head; | |
453 | wake_up(&journal->j_wait_transaction_locked); | |
454 | spin_unlock(&journal->j_state_lock); | |
455 | ||
456 | jbd_debug (3, "JBD: commit phase 2\n"); | |
457 | ||
470decc6 DK |
458 | /* |
459 | * Now start flushing things to disk, in the order they appear | |
460 | * on the transaction lists. Data blocks go first. | |
461 | */ | |
cd1aac32 | 462 | err = journal_submit_data_buffers(journal, commit_transaction); |
470decc6 | 463 | if (err) |
a7fa2baf | 464 | jbd2_journal_abort(journal, err); |
470decc6 | 465 | |
f7f4bccb | 466 | jbd2_journal_write_revoke_records(journal, commit_transaction); |
470decc6 DK |
467 | |
468 | jbd_debug(3, "JBD: commit phase 2\n"); | |
469 | ||
470decc6 DK |
470 | /* |
471 | * Way to go: we have now written out all of the data for a | |
472 | * transaction! Now comes the tricky part: we need to write out | |
473 | * metadata. Loop over the transaction's entire buffer list: | |
474 | */ | |
02c471cb | 475 | spin_lock(&journal->j_state_lock); |
470decc6 | 476 | commit_transaction->t_state = T_COMMIT; |
02c471cb | 477 | spin_unlock(&journal->j_state_lock); |
470decc6 | 478 | |
8e85fb3f JL |
479 | stats.u.run.rs_logging = jiffies; |
480 | stats.u.run.rs_flushing = jbd2_time_diff(stats.u.run.rs_flushing, | |
481 | stats.u.run.rs_logging); | |
482 | stats.u.run.rs_blocks = commit_transaction->t_outstanding_credits; | |
483 | stats.u.run.rs_blocks_logged = 0; | |
484 | ||
1dfc3220 JB |
485 | J_ASSERT(commit_transaction->t_nr_buffers <= |
486 | commit_transaction->t_outstanding_credits); | |
487 | ||
87c89c23 | 488 | err = 0; |
470decc6 DK |
489 | descriptor = NULL; |
490 | bufs = 0; | |
491 | while (commit_transaction->t_buffers) { | |
492 | ||
493 | /* Find the next buffer to be journaled... */ | |
494 | ||
495 | jh = commit_transaction->t_buffers; | |
496 | ||
497 | /* If we're in abort mode, we just un-journal the buffer and | |
498 | release it for background writing. */ | |
499 | ||
500 | if (is_journal_aborted(journal)) { | |
501 | JBUFFER_TRACE(jh, "journal is aborting: refile"); | |
f7f4bccb | 502 | jbd2_journal_refile_buffer(journal, jh); |
470decc6 DK |
503 | /* If that was the last one, we need to clean up |
504 | * any descriptor buffers which may have been | |
505 | * already allocated, even if we are now | |
506 | * aborting. */ | |
507 | if (!commit_transaction->t_buffers) | |
508 | goto start_journal_io; | |
509 | continue; | |
510 | } | |
511 | ||
512 | /* Make sure we have a descriptor block in which to | |
513 | record the metadata buffer. */ | |
514 | ||
515 | if (!descriptor) { | |
516 | struct buffer_head *bh; | |
517 | ||
518 | J_ASSERT (bufs == 0); | |
519 | ||
520 | jbd_debug(4, "JBD: get descriptor\n"); | |
521 | ||
f7f4bccb | 522 | descriptor = jbd2_journal_get_descriptor_buffer(journal); |
470decc6 | 523 | if (!descriptor) { |
a7fa2baf | 524 | jbd2_journal_abort(journal, -EIO); |
470decc6 DK |
525 | continue; |
526 | } | |
527 | ||
528 | bh = jh2bh(descriptor); | |
529 | jbd_debug(4, "JBD: got buffer %llu (%p)\n", | |
530 | (unsigned long long)bh->b_blocknr, bh->b_data); | |
531 | header = (journal_header_t *)&bh->b_data[0]; | |
f7f4bccb MC |
532 | header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); |
533 | header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK); | |
470decc6 DK |
534 | header->h_sequence = cpu_to_be32(commit_transaction->t_tid); |
535 | ||
536 | tagp = &bh->b_data[sizeof(journal_header_t)]; | |
537 | space_left = bh->b_size - sizeof(journal_header_t); | |
538 | first_tag = 1; | |
539 | set_buffer_jwrite(bh); | |
540 | set_buffer_dirty(bh); | |
541 | wbuf[bufs++] = bh; | |
542 | ||
543 | /* Record it so that we can wait for IO | |
544 | completion later */ | |
545 | BUFFER_TRACE(bh, "ph3: file as descriptor"); | |
f7f4bccb | 546 | jbd2_journal_file_buffer(descriptor, commit_transaction, |
470decc6 DK |
547 | BJ_LogCtl); |
548 | } | |
549 | ||
550 | /* Where is the buffer to be written? */ | |
551 | ||
f7f4bccb | 552 | err = jbd2_journal_next_log_block(journal, &blocknr); |
470decc6 DK |
553 | /* If the block mapping failed, just abandon the buffer |
554 | and repeat this loop: we'll fall into the | |
555 | refile-on-abort condition above. */ | |
556 | if (err) { | |
a7fa2baf | 557 | jbd2_journal_abort(journal, err); |
470decc6 DK |
558 | continue; |
559 | } | |
560 | ||
561 | /* | |
562 | * start_this_handle() uses t_outstanding_credits to determine | |
563 | * the free space in the log, but this counter is changed | |
f7f4bccb | 564 | * by jbd2_journal_next_log_block() also. |
470decc6 DK |
565 | */ |
566 | commit_transaction->t_outstanding_credits--; | |
567 | ||
568 | /* Bump b_count to prevent truncate from stumbling over | |
569 | the shadowed buffer! @@@ This can go if we ever get | |
570 | rid of the BJ_IO/BJ_Shadow pairing of buffers. */ | |
571 | atomic_inc(&jh2bh(jh)->b_count); | |
572 | ||
573 | /* Make a temporary IO buffer with which to write it out | |
574 | (this will requeue both the metadata buffer and the | |
575 | temporary IO buffer). new_bh goes on BJ_IO*/ | |
576 | ||
577 | set_bit(BH_JWrite, &jh2bh(jh)->b_state); | |
578 | /* | |
f7f4bccb | 579 | * akpm: jbd2_journal_write_metadata_buffer() sets |
470decc6 DK |
580 | * new_bh->b_transaction to commit_transaction. |
581 | * We need to clean this up before we release new_bh | |
582 | * (which is of type BJ_IO) | |
583 | */ | |
584 | JBUFFER_TRACE(jh, "ph3: write metadata"); | |
f7f4bccb | 585 | flags = jbd2_journal_write_metadata_buffer(commit_transaction, |
470decc6 DK |
586 | jh, &new_jh, blocknr); |
587 | set_bit(BH_JWrite, &jh2bh(new_jh)->b_state); | |
588 | wbuf[bufs++] = jh2bh(new_jh); | |
589 | ||
590 | /* Record the new block's tag in the current descriptor | |
591 | buffer */ | |
592 | ||
593 | tag_flag = 0; | |
594 | if (flags & 1) | |
f7f4bccb | 595 | tag_flag |= JBD2_FLAG_ESCAPE; |
470decc6 | 596 | if (!first_tag) |
f7f4bccb | 597 | tag_flag |= JBD2_FLAG_SAME_UUID; |
470decc6 DK |
598 | |
599 | tag = (journal_block_tag_t *) tagp; | |
b517bea1 | 600 | write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr); |
470decc6 | 601 | tag->t_flags = cpu_to_be32(tag_flag); |
b517bea1 ZB |
602 | tagp += tag_bytes; |
603 | space_left -= tag_bytes; | |
470decc6 DK |
604 | |
605 | if (first_tag) { | |
606 | memcpy (tagp, journal->j_uuid, 16); | |
607 | tagp += 16; | |
608 | space_left -= 16; | |
609 | first_tag = 0; | |
610 | } | |
611 | ||
612 | /* If there's no more to do, or if the descriptor is full, | |
613 | let the IO rip! */ | |
614 | ||
615 | if (bufs == journal->j_wbufsize || | |
616 | commit_transaction->t_buffers == NULL || | |
b517bea1 | 617 | space_left < tag_bytes + 16) { |
470decc6 DK |
618 | |
619 | jbd_debug(4, "JBD: Submit %d IOs\n", bufs); | |
620 | ||
621 | /* Write an end-of-descriptor marker before | |
622 | submitting the IOs. "tag" still points to | |
623 | the last tag we set up. */ | |
624 | ||
f7f4bccb | 625 | tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG); |
470decc6 DK |
626 | |
627 | start_journal_io: | |
628 | for (i = 0; i < bufs; i++) { | |
629 | struct buffer_head *bh = wbuf[i]; | |
818d276c GS |
630 | /* |
631 | * Compute checksum. | |
632 | */ | |
633 | if (JBD2_HAS_COMPAT_FEATURE(journal, | |
634 | JBD2_FEATURE_COMPAT_CHECKSUM)) { | |
635 | crc32_sum = | |
636 | jbd2_checksum_data(crc32_sum, bh); | |
637 | } | |
638 | ||
470decc6 DK |
639 | lock_buffer(bh); |
640 | clear_buffer_dirty(bh); | |
641 | set_buffer_uptodate(bh); | |
642 | bh->b_end_io = journal_end_buffer_io_sync; | |
643 | submit_bh(WRITE, bh); | |
644 | } | |
645 | cond_resched(); | |
8e85fb3f | 646 | stats.u.run.rs_blocks_logged += bufs; |
470decc6 DK |
647 | |
648 | /* Force a new descriptor to be generated next | |
649 | time round the loop. */ | |
650 | descriptor = NULL; | |
651 | bufs = 0; | |
652 | } | |
653 | } | |
654 | ||
818d276c GS |
655 | /* Done it all: now write the commit record asynchronously. */ |
656 | ||
657 | if (JBD2_HAS_INCOMPAT_FEATURE(journal, | |
658 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { | |
659 | err = journal_submit_commit_record(journal, commit_transaction, | |
660 | &cbh, crc32_sum); | |
661 | if (err) | |
662 | __jbd2_journal_abort_hard(journal); | |
818d276c GS |
663 | } |
664 | ||
c851ed54 JK |
665 | /* |
666 | * This is the right place to wait for data buffers both for ASYNC | |
667 | * and !ASYNC commit. If commit is ASYNC, we need to wait only after | |
668 | * the commit block went to disk (which happens above). If commit is | |
669 | * SYNC, we need to wait for data buffers before we start writing | |
670 | * commit block, which happens below in such setting. | |
671 | */ | |
672 | err = journal_finish_inode_data_buffers(journal, commit_transaction); | |
673 | if (err) | |
674 | jbd2_journal_abort(journal, err); | |
675 | ||
470decc6 DK |
676 | /* Lo and behold: we have just managed to send a transaction to |
677 | the log. Before we can commit it, wait for the IO so far to | |
678 | complete. Control buffers being written are on the | |
679 | transaction's t_log_list queue, and metadata buffers are on | |
680 | the t_iobuf_list queue. | |
681 | ||
682 | Wait for the buffers in reverse order. That way we are | |
683 | less likely to be woken up until all IOs have completed, and | |
684 | so we incur less scheduling load. | |
685 | */ | |
686 | ||
87c89c23 | 687 | jbd_debug(3, "JBD: commit phase 3\n"); |
470decc6 DK |
688 | |
689 | /* | |
690 | * akpm: these are BJ_IO, and j_list_lock is not needed. | |
691 | * See __journal_try_to_free_buffer. | |
692 | */ | |
693 | wait_for_iobuf: | |
694 | while (commit_transaction->t_iobuf_list != NULL) { | |
695 | struct buffer_head *bh; | |
696 | ||
697 | jh = commit_transaction->t_iobuf_list->b_tprev; | |
698 | bh = jh2bh(jh); | |
699 | if (buffer_locked(bh)) { | |
700 | wait_on_buffer(bh); | |
701 | goto wait_for_iobuf; | |
702 | } | |
703 | if (cond_resched()) | |
704 | goto wait_for_iobuf; | |
705 | ||
706 | if (unlikely(!buffer_uptodate(bh))) | |
707 | err = -EIO; | |
708 | ||
709 | clear_buffer_jwrite(bh); | |
710 | ||
711 | JBUFFER_TRACE(jh, "ph4: unfile after journal write"); | |
f7f4bccb | 712 | jbd2_journal_unfile_buffer(journal, jh); |
470decc6 DK |
713 | |
714 | /* | |
715 | * ->t_iobuf_list should contain only dummy buffer_heads | |
f7f4bccb | 716 | * which were created by jbd2_journal_write_metadata_buffer(). |
470decc6 DK |
717 | */ |
718 | BUFFER_TRACE(bh, "dumping temporary bh"); | |
f7f4bccb | 719 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
720 | __brelse(bh); |
721 | J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0); | |
722 | free_buffer_head(bh); | |
723 | ||
724 | /* We also have to unlock and free the corresponding | |
725 | shadowed buffer */ | |
726 | jh = commit_transaction->t_shadow_list->b_tprev; | |
727 | bh = jh2bh(jh); | |
728 | clear_bit(BH_JWrite, &bh->b_state); | |
729 | J_ASSERT_BH(bh, buffer_jbddirty(bh)); | |
730 | ||
731 | /* The metadata is now released for reuse, but we need | |
732 | to remember it against this transaction so that when | |
733 | we finally commit, we can do any checkpointing | |
734 | required. */ | |
735 | JBUFFER_TRACE(jh, "file as BJ_Forget"); | |
f7f4bccb | 736 | jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget); |
470decc6 DK |
737 | /* Wake up any transactions which were waiting for this |
738 | IO to complete */ | |
739 | wake_up_bit(&bh->b_state, BH_Unshadow); | |
740 | JBUFFER_TRACE(jh, "brelse shadowed buffer"); | |
741 | __brelse(bh); | |
742 | } | |
743 | ||
744 | J_ASSERT (commit_transaction->t_shadow_list == NULL); | |
745 | ||
87c89c23 | 746 | jbd_debug(3, "JBD: commit phase 4\n"); |
470decc6 DK |
747 | |
748 | /* Here we wait for the revoke record and descriptor record buffers */ | |
749 | wait_for_ctlbuf: | |
750 | while (commit_transaction->t_log_list != NULL) { | |
751 | struct buffer_head *bh; | |
752 | ||
753 | jh = commit_transaction->t_log_list->b_tprev; | |
754 | bh = jh2bh(jh); | |
755 | if (buffer_locked(bh)) { | |
756 | wait_on_buffer(bh); | |
757 | goto wait_for_ctlbuf; | |
758 | } | |
759 | if (cond_resched()) | |
760 | goto wait_for_ctlbuf; | |
761 | ||
762 | if (unlikely(!buffer_uptodate(bh))) | |
763 | err = -EIO; | |
764 | ||
765 | BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile"); | |
766 | clear_buffer_jwrite(bh); | |
f7f4bccb MC |
767 | jbd2_journal_unfile_buffer(journal, jh); |
768 | jbd2_journal_put_journal_head(jh); | |
470decc6 DK |
769 | __brelse(bh); /* One for getblk */ |
770 | /* AKPM: bforget here */ | |
771 | } | |
772 | ||
87c89c23 | 773 | jbd_debug(3, "JBD: commit phase 5\n"); |
470decc6 | 774 | |
818d276c GS |
775 | if (!JBD2_HAS_INCOMPAT_FEATURE(journal, |
776 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { | |
777 | err = journal_submit_commit_record(journal, commit_transaction, | |
778 | &cbh, crc32_sum); | |
779 | if (err) | |
780 | __jbd2_journal_abort_hard(journal); | |
781 | } | |
b048d846 MC |
782 | if (!err && !is_journal_aborted(journal)) |
783 | err = journal_wait_on_commit_record(cbh); | |
470decc6 DK |
784 | |
785 | if (err) | |
a7fa2baf | 786 | jbd2_journal_abort(journal, err); |
470decc6 DK |
787 | |
788 | /* End of a transaction! Finally, we can do checkpoint | |
789 | processing: any buffers committed as a result of this | |
790 | transaction can be removed from any checkpoint list it was on | |
791 | before. */ | |
792 | ||
87c89c23 | 793 | jbd_debug(3, "JBD: commit phase 6\n"); |
470decc6 | 794 | |
c851ed54 | 795 | J_ASSERT(list_empty(&commit_transaction->t_inode_list)); |
470decc6 DK |
796 | J_ASSERT(commit_transaction->t_buffers == NULL); |
797 | J_ASSERT(commit_transaction->t_checkpoint_list == NULL); | |
798 | J_ASSERT(commit_transaction->t_iobuf_list == NULL); | |
799 | J_ASSERT(commit_transaction->t_shadow_list == NULL); | |
800 | J_ASSERT(commit_transaction->t_log_list == NULL); | |
801 | ||
802 | restart_loop: | |
803 | /* | |
804 | * As there are other places (journal_unmap_buffer()) adding buffers | |
805 | * to this list we have to be careful and hold the j_list_lock. | |
806 | */ | |
807 | spin_lock(&journal->j_list_lock); | |
808 | while (commit_transaction->t_forget) { | |
809 | transaction_t *cp_transaction; | |
810 | struct buffer_head *bh; | |
811 | ||
812 | jh = commit_transaction->t_forget; | |
813 | spin_unlock(&journal->j_list_lock); | |
814 | bh = jh2bh(jh); | |
815 | jbd_lock_bh_state(bh); | |
816 | J_ASSERT_JH(jh, jh->b_transaction == commit_transaction || | |
817 | jh->b_transaction == journal->j_running_transaction); | |
818 | ||
819 | /* | |
820 | * If there is undo-protected committed data against | |
821 | * this buffer, then we can remove it now. If it is a | |
822 | * buffer needing such protection, the old frozen_data | |
823 | * field now points to a committed version of the | |
824 | * buffer, so rotate that field to the new committed | |
825 | * data. | |
826 | * | |
827 | * Otherwise, we can just throw away the frozen data now. | |
828 | */ | |
829 | if (jh->b_committed_data) { | |
af1e76d6 | 830 | jbd2_free(jh->b_committed_data, bh->b_size); |
470decc6 DK |
831 | jh->b_committed_data = NULL; |
832 | if (jh->b_frozen_data) { | |
833 | jh->b_committed_data = jh->b_frozen_data; | |
834 | jh->b_frozen_data = NULL; | |
835 | } | |
836 | } else if (jh->b_frozen_data) { | |
af1e76d6 | 837 | jbd2_free(jh->b_frozen_data, bh->b_size); |
470decc6 DK |
838 | jh->b_frozen_data = NULL; |
839 | } | |
840 | ||
841 | spin_lock(&journal->j_list_lock); | |
842 | cp_transaction = jh->b_cp_transaction; | |
843 | if (cp_transaction) { | |
844 | JBUFFER_TRACE(jh, "remove from old cp transaction"); | |
8e85fb3f | 845 | cp_transaction->t_chp_stats.cs_dropped++; |
f7f4bccb | 846 | __jbd2_journal_remove_checkpoint(jh); |
470decc6 DK |
847 | } |
848 | ||
849 | /* Only re-checkpoint the buffer_head if it is marked | |
850 | * dirty. If the buffer was added to the BJ_Forget list | |
f7f4bccb | 851 | * by jbd2_journal_forget, it may no longer be dirty and |
470decc6 DK |
852 | * there's no point in keeping a checkpoint record for |
853 | * it. */ | |
854 | ||
855 | /* A buffer which has been freed while still being | |
856 | * journaled by a previous transaction may end up still | |
857 | * being dirty here, but we want to avoid writing back | |
858 | * that buffer in the future now that the last use has | |
859 | * been committed. That's not only a performance gain, | |
860 | * it also stops aliasing problems if the buffer is left | |
861 | * behind for writeback and gets reallocated for another | |
862 | * use in a different page. */ | |
863 | if (buffer_freed(bh)) { | |
864 | clear_buffer_freed(bh); | |
865 | clear_buffer_jbddirty(bh); | |
866 | } | |
867 | ||
868 | if (buffer_jbddirty(bh)) { | |
869 | JBUFFER_TRACE(jh, "add to new checkpointing trans"); | |
f7f4bccb | 870 | __jbd2_journal_insert_checkpoint(jh, commit_transaction); |
470decc6 | 871 | JBUFFER_TRACE(jh, "refile for checkpoint writeback"); |
f7f4bccb | 872 | __jbd2_journal_refile_buffer(jh); |
470decc6 DK |
873 | jbd_unlock_bh_state(bh); |
874 | } else { | |
875 | J_ASSERT_BH(bh, !buffer_dirty(bh)); | |
876 | /* The buffer on BJ_Forget list and not jbddirty means | |
877 | * it has been freed by this transaction and hence it | |
878 | * could not have been reallocated until this | |
879 | * transaction has committed. *BUT* it could be | |
880 | * reallocated once we have written all the data to | |
881 | * disk and before we process the buffer on BJ_Forget | |
882 | * list. */ | |
883 | JBUFFER_TRACE(jh, "refile or unfile freed buffer"); | |
f7f4bccb | 884 | __jbd2_journal_refile_buffer(jh); |
470decc6 DK |
885 | if (!jh->b_transaction) { |
886 | jbd_unlock_bh_state(bh); | |
887 | /* needs a brelse */ | |
f7f4bccb | 888 | jbd2_journal_remove_journal_head(bh); |
470decc6 DK |
889 | release_buffer_page(bh); |
890 | } else | |
891 | jbd_unlock_bh_state(bh); | |
892 | } | |
893 | cond_resched_lock(&journal->j_list_lock); | |
894 | } | |
895 | spin_unlock(&journal->j_list_lock); | |
896 | /* | |
f5a7a6b0 JK |
897 | * This is a bit sleazy. We use j_list_lock to protect transition |
898 | * of a transaction into T_FINISHED state and calling | |
899 | * __jbd2_journal_drop_transaction(). Otherwise we could race with | |
900 | * other checkpointing code processing the transaction... | |
470decc6 DK |
901 | */ |
902 | spin_lock(&journal->j_state_lock); | |
903 | spin_lock(&journal->j_list_lock); | |
904 | /* | |
905 | * Now recheck if some buffers did not get attached to the transaction | |
906 | * while the lock was dropped... | |
907 | */ | |
908 | if (commit_transaction->t_forget) { | |
909 | spin_unlock(&journal->j_list_lock); | |
910 | spin_unlock(&journal->j_state_lock); | |
911 | goto restart_loop; | |
912 | } | |
913 | ||
914 | /* Done with this transaction! */ | |
915 | ||
87c89c23 | 916 | jbd_debug(3, "JBD: commit phase 7\n"); |
470decc6 DK |
917 | |
918 | J_ASSERT(commit_transaction->t_state == T_COMMIT); | |
919 | ||
8e85fb3f JL |
920 | commit_transaction->t_start = jiffies; |
921 | stats.u.run.rs_logging = jbd2_time_diff(stats.u.run.rs_logging, | |
922 | commit_transaction->t_start); | |
923 | ||
924 | /* | |
925 | * File the transaction for history | |
926 | */ | |
927 | stats.ts_type = JBD2_STATS_RUN; | |
928 | stats.ts_tid = commit_transaction->t_tid; | |
929 | stats.u.run.rs_handle_count = commit_transaction->t_handle_count; | |
930 | spin_lock(&journal->j_history_lock); | |
931 | memcpy(journal->j_history + journal->j_history_cur, &stats, | |
932 | sizeof(stats)); | |
933 | if (++journal->j_history_cur == journal->j_history_max) | |
934 | journal->j_history_cur = 0; | |
935 | ||
936 | /* | |
937 | * Calculate overall stats | |
938 | */ | |
939 | journal->j_stats.ts_tid++; | |
940 | journal->j_stats.u.run.rs_wait += stats.u.run.rs_wait; | |
941 | journal->j_stats.u.run.rs_running += stats.u.run.rs_running; | |
942 | journal->j_stats.u.run.rs_locked += stats.u.run.rs_locked; | |
943 | journal->j_stats.u.run.rs_flushing += stats.u.run.rs_flushing; | |
944 | journal->j_stats.u.run.rs_logging += stats.u.run.rs_logging; | |
945 | journal->j_stats.u.run.rs_handle_count += stats.u.run.rs_handle_count; | |
946 | journal->j_stats.u.run.rs_blocks += stats.u.run.rs_blocks; | |
947 | journal->j_stats.u.run.rs_blocks_logged += stats.u.run.rs_blocks_logged; | |
948 | spin_unlock(&journal->j_history_lock); | |
949 | ||
470decc6 DK |
950 | commit_transaction->t_state = T_FINISHED; |
951 | J_ASSERT(commit_transaction == journal->j_committing_transaction); | |
952 | journal->j_commit_sequence = commit_transaction->t_tid; | |
953 | journal->j_committing_transaction = NULL; | |
954 | spin_unlock(&journal->j_state_lock); | |
955 | ||
f89b7795 JK |
956 | if (commit_transaction->t_checkpoint_list == NULL && |
957 | commit_transaction->t_checkpoint_io_list == NULL) { | |
f7f4bccb | 958 | __jbd2_journal_drop_transaction(journal, commit_transaction); |
470decc6 DK |
959 | } else { |
960 | if (journal->j_checkpoint_transactions == NULL) { | |
961 | journal->j_checkpoint_transactions = commit_transaction; | |
962 | commit_transaction->t_cpnext = commit_transaction; | |
963 | commit_transaction->t_cpprev = commit_transaction; | |
964 | } else { | |
965 | commit_transaction->t_cpnext = | |
966 | journal->j_checkpoint_transactions; | |
967 | commit_transaction->t_cpprev = | |
968 | commit_transaction->t_cpnext->t_cpprev; | |
969 | commit_transaction->t_cpnext->t_cpprev = | |
970 | commit_transaction; | |
971 | commit_transaction->t_cpprev->t_cpnext = | |
972 | commit_transaction; | |
973 | } | |
974 | } | |
975 | spin_unlock(&journal->j_list_lock); | |
976 | ||
977 | jbd_debug(1, "JBD: commit %d complete, head %d\n", | |
978 | journal->j_commit_sequence, journal->j_tail_sequence); | |
979 | ||
980 | wake_up(&journal->j_wait_done_commit); | |
981 | } |