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,34 @@ 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");
68 - if (journal->j_flags & JFS_BARRIER) {
69 + set_buffer_uptodate(bh);
70 + bh->b_end_io = journal_end_buffer_io_sync;
72 + if (journal->j_flags & JFS_BARRIER &&
73 + !JFS_HAS_COMPAT_FEATURE(journal,
74 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
76 set_buffer_ordered(bh);
79 - ret = sync_dirty_buffer(bh);
80 + ret = submit_bh(WRITE, bh);
82 /* is it possible for another commit to fail at roughly
83 * the same time as this one? If so, we don't want to
84 * trust the barrier flag in the super, but instead want
85 @@ -154,12 +172,70 @@ static int journal_write_commit_record(j
86 clear_buffer_ordered(bh);
87 set_buffer_uptodate(bh);
89 - ret = sync_dirty_buffer(bh);
90 + ret = submit_bh(WRITE, bh);
92 - put_bh(bh); /* One for getblk() */
93 - journal_put_journal_head(descriptor);
99 + * This function along with journal_submit_commit_record
100 + * allows to write the commit record asynchronously.
102 +static int journal_wait_on_commit_record(struct buffer_head *bh)
106 + clear_buffer_dirty(bh);
107 + wait_on_buffer(bh);
109 + if (unlikely(!buffer_uptodate(bh)))
111 + put_bh(bh); /* One for getblk() */
112 + journal_put_journal_head(bh2jh(bh));
118 + * Wait for all submitted IO to complete.
120 +static int journal_wait_on_locked_list(journal_t *journal,
121 + transaction_t *commit_transaction)
124 + struct journal_head *jh;
126 - return (ret == -EIO);
127 + while (commit_transaction->t_locked_list) {
128 + struct buffer_head *bh;
130 + jh = commit_transaction->t_locked_list->b_tprev;
133 + if (buffer_locked(bh)) {
134 + spin_unlock(&journal->j_list_lock);
135 + wait_on_buffer(bh);
136 + if (unlikely(!buffer_uptodate(bh)))
138 + spin_lock(&journal->j_list_lock);
140 + if (!inverted_lock(journal, bh)) {
142 + spin_lock(&journal->j_list_lock);
145 + if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
146 + __journal_unfile_buffer(jh);
147 + jbd_unlock_bh_state(bh);
148 + journal_remove_journal_head(bh);
151 + jbd_unlock_bh_state(bh);
154 + cond_resched_lock(&journal->j_list_lock);
159 void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
160 @@ -273,6 +349,20 @@ write_out_data:
161 journal_do_submit_data(wbuf, bufs);
164 +static inline __u32 jbd_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
166 + struct page *page = bh->b_page;
171 + checksum = crc32_be(crc32_sum,
172 + (void *)(addr + offset_in_page(bh->b_data)),
179 * journal_commit_transaction
181 @@ -296,6 +386,8 @@ void journal_commit_transaction(journal_
185 + struct buffer_head *cbh = NULL; /* For transactional checksums */
186 + __u32 crc32_sum = ~0;
189 * First job: lock down the current transaction and wait for
190 @@ -439,38 +531,14 @@ void journal_commit_transaction(journal_
191 journal_submit_data_buffers(journal, commit_transaction);
194 - * Wait for all previously submitted IO to complete.
195 + * Wait for all previously submitted IO to complete if commit
196 + * record is to be written synchronously.
198 spin_lock(&journal->j_list_lock);
199 - while (commit_transaction->t_locked_list) {
200 - struct buffer_head *bh;
202 - jh = commit_transaction->t_locked_list->b_tprev;
205 - if (buffer_locked(bh)) {
206 - spin_unlock(&journal->j_list_lock);
207 - wait_on_buffer(bh);
208 - if (unlikely(!buffer_uptodate(bh)))
210 - spin_lock(&journal->j_list_lock);
212 - if (!inverted_lock(journal, bh)) {
214 - spin_lock(&journal->j_list_lock);
217 - if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
218 - __journal_unfile_buffer(jh);
219 - jbd_unlock_bh_state(bh);
220 - journal_remove_journal_head(bh);
223 - jbd_unlock_bh_state(bh);
226 - cond_resched_lock(&journal->j_list_lock);
228 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
229 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT))
230 + err = journal_wait_on_locked_list(journal,
231 + commit_transaction);
232 spin_unlock(&journal->j_list_lock);
235 @@ -643,6 +711,16 @@ void journal_commit_transaction(journal_
237 for (i = 0; i < bufs; i++) {
238 struct buffer_head *bh = wbuf[i];
240 + * Compute checksum.
242 + if (JFS_HAS_COMPAT_FEATURE(journal,
243 + JFS_FEATURE_COMPAT_CHECKSUM)) {
245 + jbd_checksum_data(crc32_sum,
250 clear_buffer_dirty(bh);
251 set_buffer_uptodate(bh);
252 @@ -659,6 +737,23 @@ start_journal_io:
256 + /* Done it all: now write the commit record asynchronously. */
258 + if (JFS_HAS_INCOMPAT_FEATURE(journal,
259 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
260 + err = journal_submit_commit_record(journal, commit_transaction,
263 + __journal_abort_hard(journal);
265 + spin_lock(&journal->j_list_lock);
266 + err = journal_wait_on_locked_list(journal,
267 + commit_transaction);
268 + spin_unlock(&journal->j_list_lock);
270 + __journal_abort_hard(journal);
273 /* Lo and behold: we have just managed to send a transaction to
274 the log. Before we can commit it, wait for the IO so far to
275 complete. Control buffers being written are on the
276 @@ -757,9 +852,15 @@ wait_for_iobuf:
279 jbd_debug(3, "JBD: commit phase 6\n");
281 - if (journal_write_commit_record(journal, commit_transaction))
284 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
285 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
286 + err = journal_submit_commit_record(journal, commit_transaction,
289 + __journal_abort_hard(journal);
291 + err = journal_wait_on_commit_record(cbh);
294 __journal_abort_hard(journal);
295 Index: linux-2.6.18.8/include/linux/jbd.h
296 ===================================================================
297 --- linux-2.6.18.8.orig/include/linux/jbd.h
298 +++ linux-2.6.18.8/include/linux/jbd.h
299 @@ -148,6 +148,29 @@ typedef struct journal_header_s
306 +#define JFS_CRC32_CHKSUM 1
307 +#define JFS_MD5_CHKSUM 2
308 +#define JFS_SHA1_CHKSUM 3
310 +#define JFS_CRC32_CHKSUM_SIZE 4
312 +#define JFS_CHECKSUM_BYTES (32 / sizeof(u32))
314 + * Commit block header for storing transactional checksums:
316 +struct commit_header
319 + __be32 h_blocktype;
321 + unsigned char h_chksum_type;
322 + unsigned char h_chksum_size;
323 + unsigned char h_padding[2];
324 + __be32 h_chksum[JFS_CHECKSUM_BYTES];
328 * The block tag: used to describe a single buffer in the journal
329 @@ -234,12 +257,16 @@ typedef struct journal_superblock_s
330 ((j)->j_format_version >= 2 && \
331 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
333 -#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
334 +#define JFS_FEATURE_COMPAT_CHECKSUM 0x00000001
336 +#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
337 +#define JFS_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004
339 /* Features known to this kernel version: */
340 -#define JFS_KNOWN_COMPAT_FEATURES 0
341 +#define JFS_KNOWN_COMPAT_FEATURES JFS_FEATURE_COMPAT_CHECKSUM
342 #define JFS_KNOWN_ROCOMPAT_FEATURES 0
343 -#define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE
344 +#define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE | \
345 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT
349 @@ -1053,6 +1080,8 @@ extern int journal_check_available_fe
350 (journal_t *, unsigned long, unsigned long, unsigned long);
351 extern int journal_set_features
352 (journal_t *, unsigned long, unsigned long, unsigned long);
353 +extern int journal_clear_features
354 + (journal_t *, unsigned long, unsigned long, unsigned long);
355 extern int journal_create (journal_t *);
356 extern int journal_load (journal_t *journal);
357 extern void journal_destroy (journal_t *);
358 Index: linux-2.6.18.8/fs/jbd/recovery.c
359 ===================================================================
360 --- linux-2.6.18.8.orig/fs/jbd/recovery.c
361 +++ linux-2.6.18.8/fs/jbd/recovery.c
363 #include <linux/jbd.h>
364 #include <linux/errno.h>
365 #include <linux/slab.h>
366 +#include <linux/crc32.h>
370 @@ -307,6 +308,37 @@ int journal_skip_recovery(journal_t *jou
375 + * calc_chksums calculates the checksums for the blocks described in the
376 + * descriptor block.
378 +static int calc_chksums(journal_t *journal, struct buffer_head *bh,
379 + unsigned long *next_log_block, __u32 *crc32_sum)
381 + int i, num_blks, err;
383 + struct buffer_head *obh;
385 + num_blks = count_tags(bh, journal->j_blocksize);
386 + /* Calculate checksum of the descriptor block. */
387 + *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
389 + for (i = 0; i < num_blks; i++) {
390 + io_block = (*next_log_block)++;
391 + wrap(journal, *next_log_block);
392 + err = jread(&obh, journal, io_block);
394 + printk(KERN_ERR "JBD: IO error %d recovering block "
395 + "%u in log\n", err, io_block);
398 + *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
405 static int do_one_pass(journal_t *journal,
406 struct recovery_info *info, enum passtype pass)
408 @@ -318,6 +350,7 @@ static int do_one_pass(journal_t *journa
409 struct buffer_head * bh;
410 unsigned int sequence;
412 + __u32 crc32_sum = ~0; /* Transactional Checksums */
414 /* Precompute the maximum metadata descriptors in a descriptor block */
415 int MAX_BLOCKS_PER_DESC;
416 @@ -409,9 +442,24 @@ static int do_one_pass(journal_t *journa
418 case JFS_DESCRIPTOR_BLOCK:
419 /* If it is a valid descriptor block, replay it
420 - * in pass REPLAY; otherwise, just skip over the
421 - * blocks it describes. */
422 + * in pass REPLAY; if journal_checksums enabled, then
423 + * calculate checksums in PASS_SCAN, otherwise,
424 + * just skip over the blocks it describes. */
425 if (pass != PASS_REPLAY) {
426 + if (pass == PASS_SCAN &&
427 + JFS_HAS_COMPAT_FEATURE(journal,
428 + JFS_FEATURE_COMPAT_CHECKSUM) &&
429 + !info->end_transaction) {
430 + if (calc_chksums(journal, bh,
441 count_tags(bh, journal->j_blocksize);
442 wrap(journal, next_log_block);
443 @@ -506,9 +554,97 @@ static int do_one_pass(journal_t *journa
446 case JFS_COMMIT_BLOCK:
447 - /* Found an expected commit block: not much to
448 - * do other than move on to the next sequence
449 + /* How to differentiate between interrupted commit
450 + * and journal corruption ?
452 + * {nth transaction}
453 + * Checksum Verification Failed
455 + * ____________________
457 + * async_commit sync_commit
459 + * | GO TO NEXT "Journal Corruption"
462 + * {(n+1)th transanction}
464 + * _______|______________
466 + * Commit block found Commit block not found
468 + * "Journal Corruption" |
469 + * _____________|__________
471 + * nth trans corrupt OR nth trans
472 + * and (n+1)th interrupted interrupted
473 + * before commit block
474 + * could reach the disk.
475 + * (Cannot find the difference in above
476 + * mentioned conditions. Hence assume
477 + * "Interrupted Commit".)
480 + /* Found an expected commit block: if checksums
481 + * are present verify them in PASS_SCAN; else not
482 + * much to do other than move on to the next sequence
484 + if (pass == PASS_SCAN &&
485 + JFS_HAS_COMPAT_FEATURE(journal,
486 + JFS_FEATURE_COMPAT_CHECKSUM)) {
487 + int chksum_err, chksum_seen;
488 + struct commit_header *cbh =
489 + (struct commit_header *)bh->b_data;
490 + unsigned found_chksum =
491 + be32_to_cpu(cbh->h_chksum[0]);
493 + chksum_err = chksum_seen = 0;
495 + if (info->end_transaction) {
496 + printk(KERN_ERR "JBD: Transaction %u "
497 + "found to be corrupt.\n",
498 + next_commit_ID - 1);
503 + if (crc32_sum == found_chksum &&
504 + cbh->h_chksum_type == JFS_CRC32_CHKSUM &&
505 + cbh->h_chksum_size ==
506 + JFS_CRC32_CHKSUM_SIZE) {
508 + } else if (!(cbh->h_chksum_type == 0 &&
509 + cbh->h_chksum_size == 0 &&
510 + found_chksum == 0 &&
513 + * If fs is mounted using an old kernel and then
514 + * kernel with journal_chksum is used then we
515 + * get a situation where the journal flag has
516 + * checksum flag set but checksums are not
517 + * present i.e chksum = 0, in the individual
519 + * Hence to avoid checksum failures, in this
520 + * situation, this extra check is added.
526 + info->end_transaction = next_commit_ID;
528 + if (!JFS_HAS_COMPAT_FEATURE(journal,
529 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)){
531 + "JBD: Transaction %u "
532 + "found to be corrupt.\n",
543 @@ -544,9 +680,10 @@ static int do_one_pass(journal_t *journa
544 * transaction marks the end of the valid log.
547 - if (pass == PASS_SCAN)
548 - info->end_transaction = next_commit_ID;
550 + if (pass == PASS_SCAN) {
551 + if (!info->end_transaction)
552 + info->end_transaction = next_commit_ID;
554 /* It's really bad news if different passes end up at
555 * different places (but possible due to IO errors). */
556 if (info->end_transaction != next_commit_ID) {
557 Index: linux-2.6.18.8/fs/jbd/journal.c
558 ===================================================================
559 --- linux-2.6.18.8.orig/fs/jbd/journal.c
560 +++ linux-2.6.18.8/fs/jbd/journal.c
561 @@ -67,6 +67,7 @@ EXPORT_SYMBOL(journal_update_format);
562 EXPORT_SYMBOL(journal_check_used_features);
563 EXPORT_SYMBOL(journal_check_available_features);
564 EXPORT_SYMBOL(journal_set_features);
565 +EXPORT_SYMBOL(journal_clear_features);
566 EXPORT_SYMBOL(journal_create);
567 EXPORT_SYMBOL(journal_load);
568 EXPORT_SYMBOL(journal_destroy);
569 @@ -1573,6 +1574,33 @@ int journal_set_features (journal_t *jou
574 + * int journal_clear_features () - Clear a given journal feature in the superblock
575 + * @journal: Journal to act on.
576 + * @compat: bitmask of compatible features
577 + * @ro: bitmask of features that force read-only mount
578 + * @incompat: bitmask of incompatible features
580 + * Clear a given journal feature as present on the
581 + * superblock. Returns true if the requested features could be reset.
584 +int journal_clear_features (journal_t *journal, unsigned long compat,
585 + unsigned long ro, unsigned long incompat)
587 + journal_superblock_t *sb;
589 + jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
590 + compat, ro, incompat);
592 + sb = journal->j_superblock;
594 + sb->s_feature_compat &= ~cpu_to_be32(compat);
595 + sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
596 + sb->s_feature_incompat &= ~cpu_to_be32(incompat);
602 * int journal_update_format () - Update on-disk journal structure.
603 Index: linux-2.6.18.8/fs/Kconfig
604 ===================================================================
605 --- linux-2.6.18.8.orig/fs/Kconfig
606 +++ linux-2.6.18.8/fs/Kconfig
607 @@ -140,6 +140,7 @@ config EXT3_FS_SECURITY
613 This is a generic journaling layer for block devices. It is
614 currently used by the ext3 and OCFS2 file systems, but it could
615 Index: linux-2.6.18.8/Documentation/filesystems/ext3.txt
616 ===================================================================
617 --- linux-2.6.18.8.orig/Documentation/filesystems/ext3.txt
618 +++ linux-2.6.18.8/Documentation/filesystems/ext3.txt
619 @@ -14,6 +14,16 @@ Options
620 When mounting an ext3 filesystem, the following option are accepted:
623 +journal_checksum Enable checksumming of the journal transactions.
624 + This will allow the recovery code in e2fsck and the
625 + kernel to detect corruption in the kernel. It is a
626 + compatible change and will be ignored by older kernels.
628 +journal_async_commit Commit block can be written to disk without waiting
629 + for descriptor blocks. If enabled older kernels cannot
630 + mount the device. This will enable 'journal_checksum'
633 journal=update Update the ext3 file system's journal to the current