1 Index: linux-2.6.18.8/fs/jbd/commit.c
2 ===================================================================
3 --- linux-2.6.18.8.orig/fs/jbd/commit.c
4 +++ linux-2.6.18.8/fs/jbd/commit.c
7 #include <linux/pagemap.h>
8 #include <linux/smp_lock.h>
9 +#include <linux/crc32.h>
13 @@ -95,19 +96,23 @@ static int inverted_lock(journal_t *jour
17 -/* Done it all: now write the commit record. We should have
19 + * Done it all: now submit the commit record. We should have
20 * cleaned up our previous buffers by now, so if we are in abort
21 * mode we can now just skip the rest of the journal write
24 * Returns 1 if the journal needs to be aborted or 0 on success
26 -static int journal_write_commit_record(journal_t *journal,
27 - transaction_t *commit_transaction)
28 +static int journal_submit_commit_record(journal_t *journal,
29 + transaction_t *commit_transaction,
30 + struct buffer_head **cbh,
33 struct journal_head *descriptor;
34 + struct commit_header *tmp;
35 struct buffer_head *bh;
40 if (is_journal_aborted(journal))
41 @@ -119,21 +124,35 @@ static int journal_write_commit_record(j
43 bh = jh2bh(descriptor);
45 - /* AKPM: buglet - add `i' to tmp! */
46 - for (i = 0; i < bh->b_size; i += 512) {
47 - journal_header_t *tmp = (journal_header_t*)bh->b_data;
48 - tmp->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
49 - tmp->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
50 - tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
51 + tmp = (struct commit_header *)bh->b_data;
52 + tmp->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
53 + tmp->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
54 + tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
56 + if (JFS_HAS_COMPAT_FEATURE(journal,
57 + JFS_FEATURE_COMPAT_CHECKSUM)) {
58 + tmp->h_chksum_type = JFS_CRC32_CHKSUM;
59 + tmp->h_chksum_size = JFS_CRC32_CHKSUM_SIZE;
60 + tmp->h_chksum[0] = cpu_to_be32(crc32_sum);
63 - JBUFFER_TRACE(descriptor, "write commit block");
64 + JBUFFER_TRACE(descriptor, "submit commit block");
69 - if (journal->j_flags & JFS_BARRIER) {
70 + set_buffer_uptodate(bh);
71 + bh->b_end_io = journal_end_buffer_io_sync;
73 + if (journal->j_flags & JFS_BARRIER &&
74 + !JFS_HAS_INCOMPAT_FEATURE(journal,
75 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
77 set_buffer_ordered(bh);
80 - ret = sync_dirty_buffer(bh);
81 + ret = submit_bh(WRITE, bh);
83 /* is it possible for another commit to fail at roughly
84 * the same time as this one? If so, we don't want to
85 * trust the barrier flag in the super, but instead want
86 @@ -154,12 +173,70 @@ static int journal_write_commit_record(j
87 clear_buffer_ordered(bh);
88 set_buffer_uptodate(bh);
90 - ret = sync_dirty_buffer(bh);
91 + ret = submit_bh(WRITE, bh);
93 - put_bh(bh); /* One for getblk() */
94 - journal_put_journal_head(descriptor);
100 + * This function along with journal_submit_commit_record
101 + * allows to write the commit record asynchronously.
103 +static int journal_wait_on_commit_record(struct buffer_head *bh)
107 + clear_buffer_dirty(bh);
108 + wait_on_buffer(bh);
110 + if (unlikely(!buffer_uptodate(bh)))
112 + put_bh(bh); /* One for getblk() */
113 + journal_put_journal_head(bh2jh(bh));
119 + * Wait for all submitted IO to complete.
121 +static int journal_wait_on_locked_list(journal_t *journal,
122 + transaction_t *commit_transaction)
125 + struct journal_head *jh;
127 - return (ret == -EIO);
128 + while (commit_transaction->t_locked_list) {
129 + struct buffer_head *bh;
131 + jh = commit_transaction->t_locked_list->b_tprev;
134 + if (buffer_locked(bh)) {
135 + spin_unlock(&journal->j_list_lock);
136 + wait_on_buffer(bh);
137 + if (unlikely(!buffer_uptodate(bh)))
139 + spin_lock(&journal->j_list_lock);
141 + if (!inverted_lock(journal, bh)) {
143 + spin_lock(&journal->j_list_lock);
146 + if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
147 + __journal_unfile_buffer(jh);
148 + jbd_unlock_bh_state(bh);
149 + journal_remove_journal_head(bh);
152 + jbd_unlock_bh_state(bh);
155 + cond_resched_lock(&journal->j_list_lock);
160 void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
161 @@ -273,6 +350,20 @@ write_out_data:
162 journal_do_submit_data(wbuf, bufs);
165 +static inline __u32 jbd_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
167 + struct page *page = bh->b_page;
171 + addr = kmap_atomic(page, KM_USER0);
172 + checksum = crc32_be(crc32_sum,
173 + (void *)(addr + offset_in_page(bh->b_data)),
175 + kunmap_atomic(addr, KM_USER0);
180 * journal_commit_transaction
182 @@ -296,6 +387,8 @@ void journal_commit_transaction(journal_
186 + struct buffer_head *cbh = NULL; /* For transactional checksums */
187 + __u32 crc32_sum = ~0;
190 * First job: lock down the current transaction and wait for
191 @@ -439,38 +532,14 @@ void journal_commit_transaction(journal_
192 journal_submit_data_buffers(journal, commit_transaction);
195 - * Wait for all previously submitted IO to complete.
196 + * Wait for all previously submitted IO to complete if commit
197 + * record is to be written synchronously.
199 spin_lock(&journal->j_list_lock);
200 - while (commit_transaction->t_locked_list) {
201 - struct buffer_head *bh;
203 - jh = commit_transaction->t_locked_list->b_tprev;
206 - if (buffer_locked(bh)) {
207 - spin_unlock(&journal->j_list_lock);
208 - wait_on_buffer(bh);
209 - if (unlikely(!buffer_uptodate(bh)))
211 - spin_lock(&journal->j_list_lock);
213 - if (!inverted_lock(journal, bh)) {
215 - spin_lock(&journal->j_list_lock);
218 - if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
219 - __journal_unfile_buffer(jh);
220 - jbd_unlock_bh_state(bh);
221 - journal_remove_journal_head(bh);
224 - jbd_unlock_bh_state(bh);
227 - cond_resched_lock(&journal->j_list_lock);
229 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
230 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT))
231 + err = journal_wait_on_locked_list(journal,
232 + commit_transaction);
233 spin_unlock(&journal->j_list_lock);
236 @@ -643,6 +712,16 @@ void journal_commit_transaction(journal_
238 for (i = 0; i < bufs; i++) {
239 struct buffer_head *bh = wbuf[i];
241 + * Compute checksum.
243 + if (JFS_HAS_COMPAT_FEATURE(journal,
244 + JFS_FEATURE_COMPAT_CHECKSUM)) {
246 + jbd_checksum_data(crc32_sum,
251 clear_buffer_dirty(bh);
252 set_buffer_uptodate(bh);
253 @@ -659,6 +738,23 @@ start_journal_io:
257 + /* Done it all: now write the commit record asynchronously. */
259 + if (JFS_HAS_INCOMPAT_FEATURE(journal,
260 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
261 + err = journal_submit_commit_record(journal, commit_transaction,
264 + __journal_abort_hard(journal);
266 + spin_lock(&journal->j_list_lock);
267 + err = journal_wait_on_locked_list(journal,
268 + commit_transaction);
269 + spin_unlock(&journal->j_list_lock);
271 + __journal_abort_hard(journal);
274 /* Lo and behold: we have just managed to send a transaction to
275 the log. Before we can commit it, wait for the IO so far to
276 complete. Control buffers being written are on the
277 @@ -757,9 +853,15 @@ wait_for_iobuf:
280 jbd_debug(3, "JBD: commit phase 6\n");
282 - if (journal_write_commit_record(journal, commit_transaction))
285 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
286 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
287 + err = journal_submit_commit_record(journal, commit_transaction,
290 + __journal_abort_hard(journal);
292 + err = journal_wait_on_commit_record(cbh);
295 __journal_abort_hard(journal);
296 Index: linux-2.6.18.8/include/linux/jbd.h
297 ===================================================================
298 --- linux-2.6.18.8.orig/include/linux/jbd.h
299 +++ linux-2.6.18.8/include/linux/jbd.h
300 @@ -148,6 +148,29 @@ typedef struct journal_header_s
307 +#define JFS_CRC32_CHKSUM 1
308 +#define JFS_MD5_CHKSUM 2
309 +#define JFS_SHA1_CHKSUM 3
311 +#define JFS_CRC32_CHKSUM_SIZE 4
313 +#define JFS_CHECKSUM_BYTES (32 / sizeof(u32))
315 + * Commit block header for storing transactional checksums:
317 +struct commit_header
320 + __be32 h_blocktype;
322 + unsigned char h_chksum_type;
323 + unsigned char h_chksum_size;
324 + unsigned char h_padding[2];
325 + __be32 h_chksum[JFS_CHECKSUM_BYTES];
329 * The block tag: used to describe a single buffer in the journal
330 @@ -234,12 +257,16 @@ typedef struct journal_superblock_s
331 ((j)->j_format_version >= 2 && \
332 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
334 -#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
335 +#define JFS_FEATURE_COMPAT_CHECKSUM 0x00000001
337 +#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
338 +#define JFS_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004
340 /* Features known to this kernel version: */
341 -#define JFS_KNOWN_COMPAT_FEATURES 0
342 +#define JFS_KNOWN_COMPAT_FEATURES JFS_FEATURE_COMPAT_CHECKSUM
343 #define JFS_KNOWN_ROCOMPAT_FEATURES 0
344 -#define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE
345 +#define JFS_KNOWN_INCOMPAT_FEATURES (JFS_FEATURE_INCOMPAT_REVOKE | \
346 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)
350 @@ -1053,6 +1080,8 @@ extern int journal_check_available_fe
351 (journal_t *, unsigned long, unsigned long, unsigned long);
352 extern int journal_set_features
353 (journal_t *, unsigned long, unsigned long, unsigned long);
354 +extern int journal_clear_features
355 + (journal_t *, unsigned long, unsigned long, unsigned long);
356 extern int journal_create (journal_t *);
357 extern int journal_load (journal_t *journal);
358 extern void journal_destroy (journal_t *);
359 Index: linux-2.6.18.8/fs/jbd/recovery.c
360 ===================================================================
361 --- linux-2.6.18.8.orig/fs/jbd/recovery.c
362 +++ linux-2.6.18.8/fs/jbd/recovery.c
364 #include <linux/jbd.h>
365 #include <linux/errno.h>
366 #include <linux/slab.h>
367 +#include <linux/crc32.h>
371 @@ -307,6 +308,38 @@ int journal_skip_recovery(journal_t *jou
376 + * calc_chksums calculates the checksums for the blocks described in the
377 + * descriptor block.
379 +static int calc_chksums(journal_t *journal, struct buffer_head *bh,
380 + unsigned long *next_log_block, __u32 *crc32_sum)
382 + int i, num_blks, err;
383 + unsigned long io_block;
384 + struct buffer_head *obh;
386 + num_blks = count_tags(bh, journal->j_blocksize);
387 + /* Calculate checksum of the descriptor block. */
388 + *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
390 + for (i = 0; i < num_blks; i++) {
391 + io_block = (*next_log_block)++;
392 + wrap(journal, *next_log_block);
393 + err = jread(&obh, journal, io_block);
395 + printk(KERN_ERR "JBD: IO error %d recovering block "
396 + "%lu in log\n", err, io_block);
399 + *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
407 static int do_one_pass(journal_t *journal,
408 struct recovery_info *info, enum passtype pass)
410 @@ -318,6 +351,7 @@ static int do_one_pass(journal_t *journa
411 struct buffer_head * bh;
412 unsigned int sequence;
414 + __u32 crc32_sum = ~0; /* Transactional Checksums */
416 /* Precompute the maximum metadata descriptors in a descriptor block */
417 int MAX_BLOCKS_PER_DESC;
418 @@ -409,9 +443,24 @@ static int do_one_pass(journal_t *journa
420 case JFS_DESCRIPTOR_BLOCK:
421 /* If it is a valid descriptor block, replay it
422 - * in pass REPLAY; otherwise, just skip over the
423 - * blocks it describes. */
424 + * in pass REPLAY; if journal_checksums enabled, then
425 + * calculate checksums in PASS_SCAN, otherwise,
426 + * just skip over the blocks it describes. */
427 if (pass != PASS_REPLAY) {
428 + if (pass == PASS_SCAN &&
429 + JFS_HAS_COMPAT_FEATURE(journal,
430 + JFS_FEATURE_COMPAT_CHECKSUM) &&
431 + !info->end_transaction) {
432 + if (calc_chksums(journal, bh,
443 count_tags(bh, journal->j_blocksize);
444 wrap(journal, next_log_block);
445 @@ -506,9 +555,97 @@ static int do_one_pass(journal_t *journa
448 case JFS_COMMIT_BLOCK:
449 - /* Found an expected commit block: not much to
450 - * do other than move on to the next sequence
451 + /* How to differentiate between interrupted commit
452 + * and journal corruption ?
454 + * {nth transaction}
455 + * Checksum Verification Failed
457 + * ____________________
459 + * async_commit sync_commit
461 + * | GO TO NEXT "Journal Corruption"
464 + * {(n+1)th transanction}
466 + * _______|______________
468 + * Commit block found Commit block not found
470 + * "Journal Corruption" |
471 + * _____________|__________
473 + * nth trans corrupt OR nth trans
474 + * and (n+1)th interrupted interrupted
475 + * before commit block
476 + * could reach the disk.
477 + * (Cannot find the difference in above
478 + * mentioned conditions. Hence assume
479 + * "Interrupted Commit".)
482 + /* Found an expected commit block: if checksums
483 + * are present verify them in PASS_SCAN; else not
484 + * much to do other than move on to the next sequence
486 + if (pass == PASS_SCAN &&
487 + JFS_HAS_COMPAT_FEATURE(journal,
488 + JFS_FEATURE_COMPAT_CHECKSUM)) {
489 + int chksum_err, chksum_seen;
490 + struct commit_header *cbh =
491 + (struct commit_header *)bh->b_data;
492 + unsigned found_chksum =
493 + be32_to_cpu(cbh->h_chksum[0]);
495 + chksum_err = chksum_seen = 0;
497 + if (info->end_transaction) {
498 + printk(KERN_ERR "JBD: Transaction %u "
499 + "found to be corrupt.\n",
500 + next_commit_ID - 1);
505 + if (crc32_sum == found_chksum &&
506 + cbh->h_chksum_type == JFS_CRC32_CHKSUM &&
507 + cbh->h_chksum_size ==
508 + JFS_CRC32_CHKSUM_SIZE) {
510 + } else if (!(cbh->h_chksum_type == 0 &&
511 + cbh->h_chksum_size == 0 &&
512 + found_chksum == 0 &&
515 + * If fs is mounted using an old kernel and then
516 + * kernel with journal_chksum is used then we
517 + * get a situation where the journal flag has
518 + * checksum flag set but checksums are not
519 + * present i.e chksum = 0, in the individual
521 + * Hence to avoid checksum failures, in this
522 + * situation, this extra check is added.
528 + info->end_transaction = next_commit_ID;
530 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
531 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)){
533 + "JBD: Transaction %u "
534 + "found to be corrupt.\n",
545 @@ -544,9 +681,10 @@ static int do_one_pass(journal_t *journa
546 * transaction marks the end of the valid log.
549 - if (pass == PASS_SCAN)
550 - info->end_transaction = next_commit_ID;
552 + if (pass == PASS_SCAN) {
553 + if (!info->end_transaction)
554 + info->end_transaction = next_commit_ID;
556 /* It's really bad news if different passes end up at
557 * different places (but possible due to IO errors). */
558 if (info->end_transaction != next_commit_ID) {
559 Index: linux-2.6.18.8/fs/jbd/journal.c
560 ===================================================================
561 --- linux-2.6.18.8.orig/fs/jbd/journal.c
562 +++ linux-2.6.18.8/fs/jbd/journal.c
563 @@ -67,6 +67,7 @@ EXPORT_SYMBOL(journal_update_format);
564 EXPORT_SYMBOL(journal_check_used_features);
565 EXPORT_SYMBOL(journal_check_available_features);
566 EXPORT_SYMBOL(journal_set_features);
567 +EXPORT_SYMBOL(journal_clear_features);
568 EXPORT_SYMBOL(journal_create);
569 EXPORT_SYMBOL(journal_load);
570 EXPORT_SYMBOL(journal_destroy);
571 @@ -1573,6 +1574,33 @@ int journal_set_features (journal_t *jou
576 + * int journal_clear_features () - Clear a given journal feature in the superblock
577 + * @journal: Journal to act on.
578 + * @compat: bitmask of compatible features
579 + * @ro: bitmask of features that force read-only mount
580 + * @incompat: bitmask of incompatible features
582 + * Clear a given journal feature as present on the
583 + * superblock. Returns true if the requested features could be reset.
586 +int journal_clear_features (journal_t *journal, unsigned long compat,
587 + unsigned long ro, unsigned long incompat)
589 + journal_superblock_t *sb;
591 + jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
592 + compat, ro, incompat);
594 + sb = journal->j_superblock;
596 + sb->s_feature_compat &= ~cpu_to_be32(compat);
597 + sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
598 + sb->s_feature_incompat &= ~cpu_to_be32(incompat);
604 * int journal_update_format () - Update on-disk journal structure.
605 Index: linux-2.6.18.8/fs/Kconfig
606 ===================================================================
607 --- linux-2.6.18.8.orig/fs/Kconfig
608 +++ linux-2.6.18.8/fs/Kconfig
609 @@ -140,6 +140,7 @@ config EXT3_FS_SECURITY
615 This is a generic journaling layer for block devices. It is
616 currently used by the ext3 and OCFS2 file systems, but it could
617 Index: linux-2.6.18.8/Documentation/filesystems/ext3.txt
618 ===================================================================
619 --- linux-2.6.18.8.orig/Documentation/filesystems/ext3.txt
620 +++ linux-2.6.18.8/Documentation/filesystems/ext3.txt
621 @@ -14,6 +14,16 @@ Options
622 When mounting an ext3 filesystem, the following option are accepted:
625 +journal_checksum Enable checksumming of the journal transactions.
626 + This will allow the recovery code in e2fsck and the
627 + kernel to detect corruption in the kernel. It is a
628 + compatible change and will be ignored by older kernels.
630 +journal_async_commit Commit block can be written to disk without waiting
631 + for descriptor blocks. If enabled older kernels cannot
632 + mount the device. This will enable 'journal_checksum'
635 journal=update Update the ext3 file system's journal to the current