1 Index: linux-2.6.16.53-0.16/fs/jbd/commit.c
2 ===================================================================
3 --- linux-2.6.16.53-0.16.orig/fs/jbd/commit.c
4 +++ linux-2.6.16.53-0.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 @@ -153,12 +172,84 @@ 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);
98 - return (ret == -EIO);
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 + 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 +static inline __u32 jbd_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
161 + struct page *page = bh->b_page;
165 + addr = kmap_atomic(page, KM_USER0);
166 + checksum = crc32_be(crc32_sum,
167 + (void *)(addr + offset_in_page(bh->b_data)),
169 + kunmap_atomic(addr, KM_USER0);
174 @@ -184,6 +275,8 @@ void journal_commit_transaction(journal_
178 + struct buffer_head *cbh = NULL; /* For transactional checksums */
179 + __u32 crc32_sum = ~0;
182 * First job: lock down the current transaction and wait for
183 @@ -395,38 +488,15 @@ write_out_data:
187 - * Wait for all previously submitted IO to complete.
188 + * Wait for all previously submitted IO to complete if commit
189 + * record is to be written synchronously.
191 spin_lock(&journal->j_list_lock);
192 - while (commit_transaction->t_locked_list) {
193 - struct buffer_head *bh;
194 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
195 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT))
196 + err = journal_wait_on_locked_list(journal,
197 + commit_transaction);
199 - jh = commit_transaction->t_locked_list->b_tprev;
202 - if (buffer_locked(bh)) {
203 - spin_unlock(&journal->j_list_lock);
204 - wait_on_buffer(bh);
205 - if (unlikely(!buffer_uptodate(bh)))
207 - spin_lock(&journal->j_list_lock);
209 - if (!inverted_lock(journal, bh)) {
211 - spin_lock(&journal->j_list_lock);
214 - if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
215 - __journal_unfile_buffer(jh);
216 - jbd_unlock_bh_state(bh);
217 - journal_remove_journal_head(bh);
220 - jbd_unlock_bh_state(bh);
223 - cond_resched_lock(&journal->j_list_lock);
225 spin_unlock(&journal->j_list_lock);
228 @@ -598,6 +668,16 @@ write_out_data:
230 for (i = 0; i < bufs; i++) {
231 struct buffer_head *bh = wbuf[i];
233 + * Compute checksum.
235 + if (JFS_HAS_COMPAT_FEATURE(journal,
236 + JFS_FEATURE_COMPAT_CHECKSUM)) {
238 + jbd_checksum_data(crc32_sum,
243 clear_buffer_dirty(bh);
244 set_buffer_uptodate(bh);
245 @@ -614,6 +694,23 @@ start_journal_io:
249 + /* Done it all: now write the commit record asynchronously. */
251 + if (JFS_HAS_INCOMPAT_FEATURE(journal,
252 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
253 + err = journal_submit_commit_record(journal, commit_transaction,
256 + __journal_abort_hard(journal);
258 + spin_lock(&journal->j_list_lock);
259 + err = journal_wait_on_locked_list(journal,
260 + commit_transaction);
261 + spin_unlock(&journal->j_list_lock);
263 + __journal_abort_hard(journal);
266 /* Lo and behold: we have just managed to send a transaction to
267 the log. Before we can commit it, wait for the IO so far to
268 complete. Control buffers being written are on the
269 @@ -712,9 +809,15 @@ wait_for_iobuf:
272 jbd_debug(3, "JBD: commit phase 6\n");
274 - if (journal_write_commit_record(journal, commit_transaction))
277 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
278 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
279 + err = journal_submit_commit_record(journal, commit_transaction,
282 + __journal_abort_hard(journal);
284 + err = journal_wait_on_commit_record(cbh);
287 __journal_abort_hard(journal);
288 Index: linux-2.6.16.53-0.16/include/linux/jbd.h
289 ===================================================================
290 --- linux-2.6.16.53-0.16.orig/include/linux/jbd.h
291 +++ linux-2.6.16.53-0.16/include/linux/jbd.h
292 @@ -142,6 +142,29 @@ typedef struct journal_header_s
299 +#define JFS_CRC32_CHKSUM 1
300 +#define JFS_MD5_CHKSUM 2
301 +#define JFS_SHA1_CHKSUM 3
303 +#define JFS_CRC32_CHKSUM_SIZE 4
305 +#define JFS_CHECKSUM_BYTES (32 / sizeof(u32))
307 + * Commit block header for storing transactional checksums:
309 +struct commit_header
312 + __be32 h_blocktype;
314 + unsigned char h_chksum_type;
315 + unsigned char h_chksum_size;
316 + unsigned char h_padding[2];
317 + __be32 h_chksum[JFS_CHECKSUM_BYTES];
321 * The block tag: used to describe a single buffer in the journal
322 @@ -228,12 +251,16 @@ typedef struct journal_superblock_s
323 ((j)->j_format_version >= 2 && \
324 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
326 -#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
327 +#define JFS_FEATURE_COMPAT_CHECKSUM 0x00000001
329 +#define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
330 +#define JFS_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004
332 /* Features known to this kernel version: */
333 -#define JFS_KNOWN_COMPAT_FEATURES 0
334 +#define JFS_KNOWN_COMPAT_FEATURES JFS_FEATURE_COMPAT_CHECKSUM
335 #define JFS_KNOWN_ROCOMPAT_FEATURES 0
336 -#define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE
337 +#define JFS_KNOWN_INCOMPAT_FEATURES (JFS_FEATURE_INCOMPAT_REVOKE | \
338 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)
342 @@ -1041,6 +1068,8 @@ extern int journal_check_available_fe
343 (journal_t *, unsigned long, unsigned long, unsigned long);
344 extern int journal_set_features
345 (journal_t *, unsigned long, unsigned long, unsigned long);
346 +extern int journal_clear_features
347 + (journal_t *, unsigned long, unsigned long, unsigned long);
348 extern int journal_create (journal_t *);
349 extern int journal_load (journal_t *journal);
350 extern void journal_destroy (journal_t *);
351 Index: linux-2.6.16.53-0.16/fs/jbd/recovery.c
352 ===================================================================
353 --- linux-2.6.16.53-0.16.orig/fs/jbd/recovery.c
354 +++ linux-2.6.16.53-0.16/fs/jbd/recovery.c
356 #include <linux/jbd.h>
357 #include <linux/errno.h>
358 #include <linux/slab.h>
359 +#include <linux/crc32.h>
363 @@ -307,6 +308,38 @@ int journal_skip_recovery(journal_t *jou
368 + * calc_chksums calculates the checksums for the blocks described in the
369 + * descriptor block.
371 +static int calc_chksums(journal_t *journal, struct buffer_head *bh,
372 + unsigned long *next_log_block, __u32 *crc32_sum)
374 + int i, num_blks, err;
375 + unsigned long io_block;
376 + struct buffer_head *obh;
378 + num_blks = count_tags(bh, journal->j_blocksize);
379 + /* Calculate checksum of the descriptor block. */
380 + *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
382 + for (i = 0; i < num_blks; i++) {
383 + io_block = (*next_log_block)++;
384 + wrap(journal, *next_log_block);
385 + err = jread(&obh, journal, io_block);
387 + printk(KERN_ERR "JBD: IO error %d recovering block "
388 + "%lu in log\n", err, io_block);
391 + *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
399 static int do_one_pass(journal_t *journal,
400 struct recovery_info *info, enum passtype pass)
402 @@ -318,6 +351,7 @@ static int do_one_pass(journal_t *journa
403 struct buffer_head * bh;
404 unsigned int sequence;
406 + __u32 crc32_sum = ~0; /* Transactional Checksums */
408 /* Precompute the maximum metadata descriptors in a descriptor block */
409 int MAX_BLOCKS_PER_DESC;
410 @@ -409,9 +443,24 @@ static int do_one_pass(journal_t *journa
412 case JFS_DESCRIPTOR_BLOCK:
413 /* If it is a valid descriptor block, replay it
414 - * in pass REPLAY; otherwise, just skip over the
415 - * blocks it describes. */
416 + * in pass REPLAY; if journal_checksums enabled, then
417 + * calculate checksums in PASS_SCAN, otherwise,
418 + * just skip over the blocks it describes. */
419 if (pass != PASS_REPLAY) {
420 + if (pass == PASS_SCAN &&
421 + JFS_HAS_COMPAT_FEATURE(journal,
422 + JFS_FEATURE_COMPAT_CHECKSUM) &&
423 + !info->end_transaction) {
424 + if (calc_chksums(journal, bh,
435 count_tags(bh, journal->j_blocksize);
436 wrap(journal, next_log_block);
437 @@ -506,9 +555,97 @@ static int do_one_pass(journal_t *journa
440 case JFS_COMMIT_BLOCK:
441 - /* Found an expected commit block: not much to
442 - * do other than move on to the next sequence
443 + /* How to differentiate between interrupted commit
444 + * and journal corruption ?
446 + * {nth transaction}
447 + * Checksum Verification Failed
449 + * ____________________
451 + * async_commit sync_commit
453 + * | GO TO NEXT "Journal Corruption"
456 + * {(n+1)th transanction}
458 + * _______|______________
460 + * Commit block found Commit block not found
462 + * "Journal Corruption" |
463 + * _____________|__________
465 + * nth trans corrupt OR nth trans
466 + * and (n+1)th interrupted interrupted
467 + * before commit block
468 + * could reach the disk.
469 + * (Cannot find the difference in above
470 + * mentioned conditions. Hence assume
471 + * "Interrupted Commit".)
474 + /* Found an expected commit block: if checksums
475 + * are present verify them in PASS_SCAN; else not
476 + * much to do other than move on to the next sequence
478 + if (pass == PASS_SCAN &&
479 + JFS_HAS_COMPAT_FEATURE(journal,
480 + JFS_FEATURE_COMPAT_CHECKSUM)) {
481 + int chksum_err, chksum_seen;
482 + struct commit_header *cbh =
483 + (struct commit_header *)bh->b_data;
484 + unsigned found_chksum =
485 + be32_to_cpu(cbh->h_chksum[0]);
487 + chksum_err = chksum_seen = 0;
489 + if (info->end_transaction) {
490 + printk(KERN_ERR "JBD: Transaction %u "
491 + "found to be corrupt.\n",
492 + next_commit_ID - 1);
497 + if (crc32_sum == found_chksum &&
498 + cbh->h_chksum_type == JFS_CRC32_CHKSUM &&
499 + cbh->h_chksum_size ==
500 + JFS_CRC32_CHKSUM_SIZE) {
502 + } else if (!(cbh->h_chksum_type == 0 &&
503 + cbh->h_chksum_size == 0 &&
504 + found_chksum == 0 &&
507 + * If fs is mounted using an old kernel and then
508 + * kernel with journal_chksum is used then we
509 + * get a situation where the journal flag has
510 + * checksum flag set but checksums are not
511 + * present i.e chksum = 0, in the individual
513 + * Hence to avoid checksum failures, in this
514 + * situation, this extra check is added.
520 + info->end_transaction = next_commit_ID;
522 + if (!JFS_HAS_INCOMPAT_FEATURE(journal,
523 + JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)){
525 + "JBD: Transaction %u "
526 + "found to be corrupt.\n",
537 @@ -543,9 +680,10 @@ static int do_one_pass(journal_t *journa
538 * transaction marks the end of the valid log.
541 - if (pass == PASS_SCAN)
542 - info->end_transaction = next_commit_ID;
544 + if (pass == PASS_SCAN) {
545 + if (!info->end_transaction)
546 + info->end_transaction = next_commit_ID;
548 /* It's really bad news if different passes end up at
549 * different places (but possible due to IO errors). */
550 if (info->end_transaction != next_commit_ID) {
551 Index: linux-2.6.16.53-0.16/fs/jbd/journal.c
552 ===================================================================
553 --- linux-2.6.16.53-0.16.orig/fs/jbd/journal.c
554 +++ linux-2.6.16.53-0.16/fs/jbd/journal.c
555 @@ -64,6 +64,7 @@ EXPORT_SYMBOL(journal_update_format);
556 EXPORT_SYMBOL(journal_check_used_features);
557 EXPORT_SYMBOL(journal_check_available_features);
558 EXPORT_SYMBOL(journal_set_features);
559 +EXPORT_SYMBOL(journal_clear_features);
560 EXPORT_SYMBOL(journal_create);
561 EXPORT_SYMBOL(journal_load);
562 EXPORT_SYMBOL(journal_destroy);
563 @@ -1565,6 +1566,33 @@ int journal_set_features (journal_t *jou
568 + * int journal_clear_features () - Clear a given journal feature in the superblock
569 + * @journal: Journal to act on.
570 + * @compat: bitmask of compatible features
571 + * @ro: bitmask of features that force read-only mount
572 + * @incompat: bitmask of incompatible features
574 + * Clear a given journal feature as present on the
575 + * superblock. Returns true if the requested features could be reset.
578 +int journal_clear_features (journal_t *journal, unsigned long compat,
579 + unsigned long ro, unsigned long incompat)
581 + journal_superblock_t *sb;
583 + jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
584 + compat, ro, incompat);
586 + sb = journal->j_superblock;
588 + sb->s_feature_compat &= ~cpu_to_be32(compat);
589 + sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
590 + sb->s_feature_incompat &= ~cpu_to_be32(incompat);
596 * int journal_update_format () - Update on-disk journal structure.
597 Index: linux-2.6.16.53-0.16/fs/Kconfig
598 ===================================================================
599 --- linux-2.6.16.53-0.16.orig/fs/Kconfig
600 +++ linux-2.6.16.53-0.16/fs/Kconfig
601 @@ -140,6 +140,7 @@ config EXT3_FS_SECURITY
607 This is a generic journaling layer for block devices. It is
608 currently used by the ext3 and OCFS2 file systems, but it could
609 Index: linux-2.6.16.53-0.16/Documentation/filesystems/ext3.txt
610 ===================================================================
611 --- linux-2.6.16.53-0.16.orig/Documentation/filesystems/ext3.txt
612 +++ linux-2.6.16.53-0.16/Documentation/filesystems/ext3.txt
613 @@ -14,6 +14,16 @@ Options
614 When mounting an ext3 filesystem, the following option are accepted:
617 +journal_checksum Enable checksumming of the journal transactions.
618 + This will allow the recovery code in e2fsck and the
619 + kernel to detect corruption in the kernel. It is a
620 + compatible change and will be ignored by older kernels.
622 +journal_async_commit Commit block can be written to disk without waiting
623 + for descriptor blocks. If enabled older kernels cannot
624 + mount the device. This will enable 'journal_checksum'
627 journal=update Update the ext3 file system's journal to the current