1 Index: linux-2.6.16/fs/jbd/commit.c
2 ===================================================================
3 --- linux-2.6.16.orig/fs/jbd/commit.c
4 +++ linux-2.6.16/fs/jbd/commit.c
6 #include <linux/pagemap.h>
7 #include <linux/smp_lock.h>
8 #include <linux/jiffies.h>
9 +#include <linux/crc32.h>
12 * Default IO end handler for temporary BJ_IO buffer_heads.
13 @@ -94,19 +95,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 @@ -118,21 +123,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_INCOMPAT_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);
83 clear_buffer_ordered(bh);
84 /* is it possible for another commit to fail at roughly
85 @@ -152,14 +170,87 @@ static int journal_write_commit_record(j
86 spin_unlock(&journal->j_state_lock);
88 /* And try again, without the barrier */
90 set_buffer_uptodate(bh);
92 - ret = sync_dirty_buffer(bh);
93 + ret = submit_bh(WRITE, bh);
95 - put_bh(bh); /* One for getblk() */
96 - journal_put_journal_head(descriptor);
101 - return (ret == -EIO);
103 + * This function along with journal_submit_commit_record
104 + * allows to write the commit record asynchronously.
106 +static int journal_wait_on_commit_record(struct buffer_head *bh)
110 + clear_buffer_dirty(bh);
111 + wait_on_buffer(bh);
113 + if (unlikely(!buffer_uptodate(bh)))
115 + put_bh(bh); /* One for getblk() */
116 + journal_put_journal_head(bh2jh(bh));
122 + * Wait for all submitted IO to complete.
124 +static int journal_wait_on_locked_list(journal_t *journal,
125 + transaction_t *commit_transaction)
128 + struct journal_head *jh;
130 + while (commit_transaction->t_locked_list) {
131 + struct buffer_head *bh;
133 + jh = commit_transaction->t_locked_list->b_tprev;
136 + if (buffer_locked(bh)) {
137 + spin_unlock(&journal->j_list_lock);
138 + wait_on_buffer(bh);
139 + if (unlikely(!buffer_uptodate(bh)))
141 + spin_lock(&journal->j_list_lock);
143 + if (!inverted_lock(journal, bh)) {
145 + spin_lock(&journal->j_list_lock);
148 + if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
149 + __journal_unfile_buffer(jh);
150 + jbd_unlock_bh_state(bh);
151 + journal_remove_journal_head(bh);
154 + jbd_unlock_bh_state(bh);
157 + cond_resched_lock(&journal->j_list_lock);
162 +static inline __u32 jbd_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
164 + struct page *page = bh->b_page;
168 + addr = kmap_atomic(page, KM_USER0);
169 + checksum = crc32_be(crc32_sum,
170 + (void *)(addr + offset_in_page(bh->b_data)),
172 + kunmap_atomic(addr, KM_USER0);
176 void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
177 @@ -296,6 +387,8 @@ void journal_commit_transaction(journal_
181 + struct buffer_head *cbh = NULL; /* For transactional checksums */
182 + __u32 crc32_sum = ~0;
185 * First job: lock down the current transaction and wait for
186 @@ -439,38 +532,15 @@ void journal_commit_transaction(journal_
187 journal_submit_data_buffers(journal, commit_transaction);
190 - * Wait for all previously submitted IO to complete.
191 + * Wait for all previously submitted IO to complete if commit
192 + * record is to be written synchronously.
194 spin_lock(&journal->j_list_lock);
195 - while (commit_transaction->t_locked_list) {
196 - struct buffer_head *bh;
197 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
198 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT))
199 + err = journal_wait_on_locked_list(journal,
200 + commit_transaction);
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 spin_unlock(&journal->j_list_lock);
231 @@ -643,6 +713,16 @@ void journal_commit_transaction(journal_
233 for (i = 0; i < bufs; i++) {
234 struct buffer_head *bh = wbuf[i];
236 + * Compute checksum.
238 + if (JFS_HAS_COMPAT_FEATURE(journal,
239 + JFS_FEATURE_COMPAT_CHECKSUM)) {
241 + jbd_checksum_data(crc32_sum,
246 clear_buffer_dirty(bh);
247 set_buffer_uptodate(bh);
248 @@ -659,6 +739,23 @@ start_journal_io:
252 + /* Done it all: now write the commit record asynchronously. */
254 + if (JFS_HAS_INCOMPAT_FEATURE(journal,
255 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
256 + err = journal_submit_commit_record(journal, commit_transaction,
259 + __journal_abort_hard(journal);
261 + spin_lock(&journal->j_list_lock);
262 + err = journal_wait_on_locked_list(journal,
263 + commit_transaction);
264 + spin_unlock(&journal->j_list_lock);
266 + __journal_abort_hard(journal);
269 /* Lo and behold: we have just managed to send a transaction to
270 the log. Before we can commit it, wait for the IO so far to
271 complete. Control buffers being written are on the
272 @@ -757,9 +854,15 @@ wait_for_iobuf:
275 jbd_debug(3, "JBD: commit phase 6\n");
277 - if (journal_write_commit_record(journal, commit_transaction))
280 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
281 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
282 + err = journal_submit_commit_record(journal, commit_transaction,
285 + __journal_abort_hard(journal);
287 + err = journal_wait_on_commit_record(cbh);
290 __journal_abort_hard(journal);
291 Index: linux-2.6.16/fs/jbd/recovery.c
292 ===================================================================
293 --- linux-2.6.16.orig/fs/jbd/recovery.c
294 +++ linux-2.6.16/fs/jbd/recovery.c
296 #include <linux/jbd.h>
297 #include <linux/errno.h>
298 #include <linux/slab.h>
299 +#include <linux/crc32.h>
303 @@ -307,6 +308,38 @@ int journal_skip_recovery(journal_t *jou
308 + * calc_chksums calculates the checksums for the blocks described in the
309 + * descriptor block.
311 +static int calc_chksums(journal_t *journal, struct buffer_head *bh,
312 + unsigned long *next_log_block, __u32 *crc32_sum)
314 + int i, num_blks, err;
315 + unsigned long io_block;
316 + struct buffer_head *obh;
318 + num_blks = count_tags(bh, journal->j_blocksize);
319 + /* Calculate checksum of the descriptor block. */
320 + *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
322 + for (i = 0; i < num_blks; i++) {
323 + io_block = (*next_log_block)++;
324 + wrap(journal, *next_log_block);
325 + err = jread(&obh, journal, io_block);
327 + printk(KERN_ERR "JBD: IO error %d recovering block "
328 + "%lu in log\n", err, io_block);
331 + *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
339 static int do_one_pass(journal_t *journal,
340 struct recovery_info *info, enum passtype pass)
342 @@ -318,6 +351,7 @@ static int do_one_pass(journal_t *journa
343 struct buffer_head * bh;
344 unsigned int sequence;
346 + __u32 crc32_sum = ~0; /* Transactional Checksums */
348 /* Precompute the maximum metadata descriptors in a descriptor block */
349 int MAX_BLOCKS_PER_DESC;
350 @@ -409,9 +443,24 @@ static int do_one_pass(journal_t *journa
352 case JFS_DESCRIPTOR_BLOCK:
353 /* If it is a valid descriptor block, replay it
354 - * in pass REPLAY; otherwise, just skip over the
355 - * blocks it describes. */
356 + * in pass REPLAY; if journal_checksums enabled, then
357 + * calculate checksums in PASS_SCAN, otherwise,
358 + * just skip over the blocks it describes. */
359 if (pass != PASS_REPLAY) {
360 + if (pass == PASS_SCAN &&
361 + JFS_HAS_COMPAT_FEATURE(journal,
362 + JFS_FEATURE_COMPAT_CHECKSUM) &&
363 + !info->end_transaction) {
364 + if (calc_chksums(journal, bh,
375 count_tags(bh, journal->j_blocksize);
376 wrap(journal, next_log_block);
377 @@ -506,9 +555,97 @@ static int do_one_pass(journal_t *journa
380 case JFS_COMMIT_BLOCK:
381 - /* Found an expected commit block: not much to
382 - * do other than move on to the next sequence
383 + /* How to differentiate between interrupted commit
384 + * and journal corruption ?
386 + * {nth transaction}
387 + * Checksum Verification Failed
389 + * ____________________
391 + * async_commit sync_commit
393 + * | GO TO NEXT "Journal Corruption"
396 + * {(n+1)th transanction}
398 + * _______|______________
400 + * Commit block found Commit block not found
402 + * "Journal Corruption" |
403 + * _____________|__________
405 + * nth trans corrupt OR nth trans
406 + * and (n+1)th interrupted interrupted
407 + * before commit block
408 + * could reach the disk.
409 + * (Cannot find the difference in above
410 + * mentioned conditions. Hence assume
411 + * "Interrupted Commit".)
414 + /* Found an expected commit block: if checksums
415 + * are present verify them in PASS_SCAN; else not
416 + * much to do other than move on to the next sequence
418 + if (pass == PASS_SCAN &&
419 + JFS_HAS_COMPAT_FEATURE(journal,
420 + JFS_FEATURE_COMPAT_CHECKSUM)) {
421 + int chksum_err, chksum_seen;
422 + struct commit_header *cbh =
423 + (struct commit_header *)bh->b_data;
424 + unsigned found_chksum =
425 + be32_to_cpu(cbh->h_chksum[0]);
427 + chksum_err = chksum_seen = 0;
429 + if (info->end_transaction) {
430 + printk(KERN_ERR "JBD: Transaction %u "
431 + "found to be corrupt.\n",
432 + next_commit_ID - 1);
437 + if (crc32_sum == found_chksum &&
438 + cbh->h_chksum_type == JFS_CRC32_CHKSUM &&
439 + cbh->h_chksum_size ==
440 + JFS_CRC32_CHKSUM_SIZE) {
442 + } else if (!(cbh->h_chksum_type == 0 &&
443 + cbh->h_chksum_size == 0 &&
444 + found_chksum == 0 &&
447 + * If fs is mounted using an old kernel and then
448 + * kernel with journal_chksum is used then we
449 + * get a situation where the journal flag has
450 + * checksum flag set but checksums are not
451 + * present i.e chksum = 0, in the individual
453 + * Hence to avoid checksum failures, in this
454 + * situation, this extra check is added.
460 + info->end_transaction = next_commit_ID;
462 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
463 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)){
465 + "JBD: Transaction %u "
466 + "found to be corrupt.\n",
477 @@ -543,9 +680,10 @@ static int do_one_pass(journal_t *journa
478 * transaction marks the end of the valid log.
481 - if (pass == PASS_SCAN)
482 - info->end_transaction = next_commit_ID;
484 + if (pass == PASS_SCAN) {
485 + if (!info->end_transaction)
486 + info->end_transaction = next_commit_ID;
488 /* It's really bad news if different passes end up at
489 * different places (but possible due to IO errors). */
490 if (info->end_transaction != next_commit_ID) {
491 Index: linux-2.6.16/fs/jbd/journal.c
492 ===================================================================
493 --- linux-2.6.16.orig/fs/jbd/journal.c
494 +++ linux-2.6.16/fs/jbd/journal.c
495 @@ -64,6 +64,7 @@ EXPORT_SYMBOL(journal_update_format);
496 EXPORT_SYMBOL(journal_check_used_features);
497 EXPORT_SYMBOL(journal_check_available_features);
498 EXPORT_SYMBOL(journal_set_features);
499 +EXPORT_SYMBOL(journal_clear_features);
500 EXPORT_SYMBOL(journal_create);
501 EXPORT_SYMBOL(journal_load);
502 EXPORT_SYMBOL(journal_destroy);
503 @@ -1565,6 +1566,33 @@ int journal_set_features (journal_t *jou
508 + * int journal_clear_features () - Clear a given journal feature in the superblock
509 + * @journal: Journal to act on.
510 + * @compat: bitmask of compatible features
511 + * @ro: bitmask of features that force read-only mount
512 + * @incompat: bitmask of incompatible features
514 + * Clear a given journal feature as present on the
515 + * superblock. Returns true if the requested features could be reset.
518 +int journal_clear_features (journal_t *journal, unsigned long compat,
519 + unsigned long ro, unsigned long incompat)
521 + journal_superblock_t *sb;
523 + jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
524 + compat, ro, incompat);
526 + sb = journal->j_superblock;
528 + sb->s_feature_compat &= ~cpu_to_be32(compat);
529 + sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
530 + sb->s_feature_incompat &= ~cpu_to_be32(incompat);
536 * int journal_update_format () - Update on-disk journal structure.
537 Index: linux-2.6.16/fs/Kconfig
538 ===================================================================
539 --- linux-2.6.16.orig/fs/Kconfig
540 +++ linux-2.6.16/fs/Kconfig
541 @@ -140,6 +140,7 @@ config EXT3_FS_SECURITY
547 This is a generic journaling layer for block devices. It is
548 currently used by the ext3 and OCFS2 file systems, but it could
549 Index: linux-2.6.16/include/linux/jbd.h
550 ===================================================================
551 --- linux-2.6.16.orig/include/linux/jbd.h
552 +++ linux-2.6.16/include/linux/jbd.h
553 @@ -142,6 +142,29 @@ typedef struct journal_header_s
560 +#define JFS_CRC32_CHKSUM 1
561 +#define JFS_MD5_CHKSUM 2
562 +#define JFS_SHA1_CHKSUM 3
564 +#define JFS_CRC32_CHKSUM_SIZE 4
566 +#define JFS_CHECKSUM_BYTES (32 / sizeof(u32))
568 + * Commit block header for storing transactional checksums:
570 +struct commit_header
573 + __be32 h_blocktype;
575 + unsigned char h_chksum_type;
576 + unsigned char h_chksum_size;
577 + unsigned char h_padding[2];
578 + __be32 h_chksum[JFS_CHECKSUM_BYTES];
582 * The block tag: used to describe a single buffer in the journal
583 @@ -228,12 +251,16 @@ typedef struct journal_superblock_s
584 ((j)->j_format_version >= 2 && \
585 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
587 -#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
588 +#define JFS_FEATURE_COMPAT_CHECKSUM 0x00000001
590 +#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
591 +#define JFS_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004
593 /* Features known to this kernel version: */
594 -#define JFS_KNOWN_COMPAT_FEATURES 0
595 +#define JFS_KNOWN_COMPAT_FEATURES JFS_FEATURE_COMPAT_CHECKSUM
596 #define JFS_KNOWN_ROCOMPAT_FEATURES 0
597 -#define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE
598 +#define JFS_KNOWN_INCOMPAT_FEATURES (JFS_FEATURE_INCOMPAT_REVOKE | \
599 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)
603 @@ -1041,6 +1068,8 @@ extern int journal_check_available_fe
604 (journal_t *, unsigned long, unsigned long, unsigned long);
605 extern int journal_set_features
606 (journal_t *, unsigned long, unsigned long, unsigned long);
607 +extern int journal_clear_features
608 + (journal_t *, unsigned long, unsigned long, unsigned long);
609 extern int journal_create (journal_t *);
610 extern int journal_load (journal_t *journal);
611 extern void journal_destroy (journal_t *);
612 Index: linux-2.6.16/Documentation/filesystems/ext3.txt
613 ===================================================================
614 --- linux-2.6.16.orig/Documentation/filesystems/ext3.txt
615 +++ linux-2.6.16/Documentation/filesystems/ext3.txt
616 @@ -14,6 +14,16 @@ Options
617 When mounting an ext3 filesystem, the following option are accepted:
620 +journal_checksum Enable checksumming of the journal transactions.
621 + This will allow the recovery code in e2fsck and the
622 + kernel to detect corruption in the kernel. It is a
623 + compatible change and will be ignored by older kernels.
625 +journal_async_commit Commit block can be written to disk without waiting
626 + for descriptor blocks. If enabled older kernels cannot
627 + mount the device. This will enable 'journal_checksum'
630 journal=update Update the ext3 file system's journal to the current