Whamcloud - gitweb
Align the types used by jbd2_journal_bmap and getblk with the kernel
[tools/e2fsprogs.git] / debugfs / journal.c
1 /*
2  * journal.c --- code for handling the "ext3" journal
3  *
4  * Copyright (C) 2000 Andreas Dilger
5  * Copyright (C) 2000 Theodore Ts'o
6  *
7  * Parts of the code are based on fs/jfs/journal.c by Stephen C. Tweedie
8  * Copyright (C) 1999 Red Hat Software
9  *
10  * This file may be redistributed under the terms of the
11  * GNU General Public License version 2 or at your discretion
12  * any later version.
13  */
14
15 #include "config.h"
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/param.h>
18 #include <sys/mount.h>
19 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
20 #endif
21 #ifdef HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24
25 #define E2FSCK_INCLUDE_INLINE_FUNCS
26 #include "uuid/uuid.h"
27 #include "journal.h"
28
29 #ifdef CONFIG_JBD_DEBUG         /* Enabled by configure --enable-jfs-debug */
30 static int bh_count = 0;
31 #endif
32
33 #if EXT2_FLAT_INCLUDES
34 #include "blkid.h"
35 #else
36 #include "blkid/blkid.h"
37 #endif
38
39 /*
40  * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
41  * This creates a larger static binary, and a smaller binary using
42  * shared libraries.  It's also probably slightly less CPU-efficient,
43  * which is why it's not on by default.  But, it's a good way of
44  * testing the functions in inode_io.c and fileio.c.
45  */
46 #undef USE_INODE_IO
47
48 /* Checksumming functions */
49 static int ext2fs_journal_verify_csum_type(journal_t *j,
50                                            journal_superblock_t *jsb)
51 {
52         if (!jbd2_journal_has_csum_v2or3(j))
53                 return 1;
54
55         return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
56 }
57
58 static __u32 ext2fs_journal_sb_csum(journal_superblock_t *jsb)
59 {
60         __u32 crc, old_crc;
61
62         old_crc = jsb->s_checksum;
63         jsb->s_checksum = 0;
64         crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
65                                sizeof(journal_superblock_t));
66         jsb->s_checksum = old_crc;
67
68         return crc;
69 }
70
71 static int ext2fs_journal_sb_csum_verify(journal_t *j,
72                                          journal_superblock_t *jsb)
73 {
74         __u32 provided, calculated;
75
76         if (!jbd2_journal_has_csum_v2or3(j))
77                 return 1;
78
79         provided = ext2fs_be32_to_cpu(jsb->s_checksum);
80         calculated = ext2fs_journal_sb_csum(jsb);
81
82         return provided == calculated;
83 }
84
85 static errcode_t ext2fs_journal_sb_csum_set(journal_t *j,
86                                             journal_superblock_t *jsb)
87 {
88         __u32 crc;
89
90         if (!jbd2_journal_has_csum_v2or3(j))
91                 return 0;
92
93         crc = ext2fs_journal_sb_csum(jsb);
94         jsb->s_checksum = ext2fs_cpu_to_be32(crc);
95         return 0;
96 }
97
98 /* Kernel compatibility functions for handling the journal.  These allow us
99  * to use the recovery.c file virtually unchanged from the kernel, so we
100  * don't have to do much to keep kernel and user recovery in sync.
101  */
102 int jbd2_journal_bmap(journal_t *journal, unsigned long block,
103                       unsigned long long *phys)
104 {
105 #ifdef USE_INODE_IO
106         *phys = block;
107         return 0;
108 #else
109         struct inode    *inode = journal->j_inode;
110         errcode_t       retval;
111         blk64_t         pblk;
112
113         if (!inode) {
114                 *phys = block;
115                 return 0;
116         }
117
118         retval = ext2fs_bmap2(inode->i_fs, inode->i_ino,
119                               &inode->i_ext2, NULL, 0, (blk64_t) block,
120                               0, &pblk);
121         *phys = pblk;
122         return (int) retval;
123 #endif
124 }
125
126 struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr,
127                            int blocksize)
128 {
129         struct buffer_head *bh;
130         int bufsize = sizeof(*bh) + kdev->k_fs->blocksize -
131                 sizeof(bh->b_data);
132         errcode_t retval;
133
134         retval = ext2fs_get_memzero(bufsize, &bh);
135         if (retval)
136                 return NULL;
137
138 #ifdef CONFIG_JBD_DEBUG
139         if (journal_enable_debug >= 3)
140                 bh_count++;
141 #endif
142         jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
143                   blocknr, blocksize, bh_count);
144
145         bh->b_fs = kdev->k_fs;
146         if (kdev->k_dev == K_DEV_FS)
147                 bh->b_io = kdev->k_fs->io;
148         else
149                 bh->b_io = kdev->k_fs->journal_io;
150         bh->b_size = blocksize;
151         bh->b_blocknr = blocknr;
152
153         return bh;
154 }
155
156 int sync_blockdev(kdev_t kdev)
157 {
158         io_channel      io;
159
160         if (kdev->k_dev == K_DEV_FS)
161                 io = kdev->k_fs->io;
162         else
163                 io = kdev->k_fs->journal_io;
164
165         return io_channel_flush(io) ? EIO : 0;
166 }
167
168 void ll_rw_block(int rw, int op_flags, int nr, struct buffer_head *bhp[])
169 {
170         errcode_t retval;
171         struct buffer_head *bh;
172
173         for (; nr > 0; --nr) {
174                 bh = *bhp++;
175                 if (rw == REQ_OP_READ && !bh->b_uptodate) {
176                         jfs_debug(3, "reading block %llu/%p\n",
177                                   bh->b_blocknr, (void *) bh);
178                         retval = io_channel_read_blk64(bh->b_io,
179                                                      bh->b_blocknr,
180                                                      1, bh->b_data);
181                         if (retval) {
182                                 com_err(bh->b_fs->device_name, retval,
183                                         "while reading block %llu\n",
184                                         bh->b_blocknr);
185                                 bh->b_err = (int) retval;
186                                 continue;
187                         }
188                         bh->b_uptodate = 1;
189                 } else if (rw == REQ_OP_WRITE && bh->b_dirty) {
190                         jfs_debug(3, "writing block %llu/%p\n",
191                                   bh->b_blocknr,
192                                   (void *) bh);
193                         retval = io_channel_write_blk64(bh->b_io,
194                                                       bh->b_blocknr,
195                                                       1, bh->b_data);
196                         if (retval) {
197                                 com_err(bh->b_fs->device_name, retval,
198                                         "while writing block %llu\n",
199                                         bh->b_blocknr);
200                                 bh->b_err = (int) retval;
201                                 continue;
202                         }
203                         bh->b_dirty = 0;
204                         bh->b_uptodate = 1;
205                 } else {
206                         jfs_debug(3, "no-op %s for block %llu\n",
207                                   rw == REQ_OP_READ ? "read" : "write",
208                                   bh->b_blocknr);
209                 }
210         }
211 }
212
213 void mark_buffer_dirty(struct buffer_head *bh)
214 {
215         bh->b_dirty = 1;
216 }
217
218 static void mark_buffer_clean(struct buffer_head *bh)
219 {
220         bh->b_dirty = 0;
221 }
222
223 void brelse(struct buffer_head *bh)
224 {
225         if (bh->b_dirty)
226                 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
227         jfs_debug(3, "freeing block %llu/%p (total %d)\n",
228                   bh->b_blocknr, (void *) bh, --bh_count);
229         ext2fs_free_mem(&bh);
230 }
231
232 int buffer_uptodate(struct buffer_head *bh)
233 {
234         return bh->b_uptodate;
235 }
236
237 void mark_buffer_uptodate(struct buffer_head *bh, int val)
238 {
239         bh->b_uptodate = val;
240 }
241
242 void wait_on_buffer(struct buffer_head *bh)
243 {
244         if (!bh->b_uptodate)
245                 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
246 }
247
248
249 static void ext2fs_clear_recover(ext2_filsys fs, int error)
250 {
251         ext2fs_clear_feature_journal_needs_recovery(fs->super);
252
253         /* if we had an error doing journal recovery, we need a full fsck */
254         if (error)
255                 fs->super->s_state &= ~EXT2_VALID_FS;
256         /*
257          * If we replayed the journal by definition the file system
258          * was mounted since the last time it was checked
259          */
260         if (fs->super->s_lastcheck >= fs->super->s_mtime)
261                 fs->super->s_lastcheck = fs->super->s_mtime - 1;
262         ext2fs_mark_super_dirty(fs);
263 }
264
265 /*
266  * This is a helper function to check the validity of the journal.
267  */
268 struct process_block_struct {
269         e2_blkcnt_t     last_block;
270 };
271
272 static int process_journal_block(ext2_filsys fs,
273                                  blk64_t        *block_nr,
274                                  e2_blkcnt_t blockcnt,
275                                  blk64_t ref_block EXT2FS_ATTR((unused)),
276                                  int ref_offset EXT2FS_ATTR((unused)),
277                                  void *priv_data)
278 {
279         struct process_block_struct *p;
280         blk64_t blk = *block_nr;
281
282         p = (struct process_block_struct *) priv_data;
283
284         if (!blk || blk < fs->super->s_first_data_block ||
285             blk >= ext2fs_blocks_count(fs->super))
286                 return BLOCK_ABORT;
287
288         if (blockcnt >= 0)
289                 p->last_block = blockcnt;
290         return 0;
291 }
292
293 static errcode_t ext2fs_get_journal(ext2_filsys fs, journal_t **ret_journal)
294 {
295         struct process_block_struct pb;
296         struct ext2_super_block *sb = fs->super;
297         struct ext2_super_block jsuper;
298         struct buffer_head      *bh;
299         struct inode            *j_inode = NULL;
300         struct kdev_s           *dev_fs = NULL, *dev_journal;
301         const char              *journal_name = 0;
302         journal_t               *journal = NULL;
303         errcode_t               retval = 0;
304         io_manager              io_ptr = 0;
305         unsigned long long      start = 0;
306         int                     ext_journal = 0;
307         int                     tried_backup_jnl = 0;
308
309         retval = ext2fs_get_memzero(sizeof(journal_t), &journal);
310         if (retval)
311                 return retval;
312
313         retval = ext2fs_get_memzero(2 * sizeof(struct kdev_s), &dev_fs);
314         if (retval)
315                 goto errout;
316         dev_journal = dev_fs+1;
317
318         dev_fs->k_fs = dev_journal->k_fs = fs;
319         dev_fs->k_dev = K_DEV_FS;
320         dev_journal->k_dev = K_DEV_JOURNAL;
321
322         journal->j_dev = dev_journal;
323         journal->j_fs_dev = dev_fs;
324         journal->j_inode = NULL;
325         journal->j_blocksize = fs->blocksize;
326
327         if (uuid_is_null(sb->s_journal_uuid)) {
328                 if (!sb->s_journal_inum) {
329                         retval = EXT2_ET_BAD_INODE_NUM;
330                         goto errout;
331                 }
332                 retval = ext2fs_get_memzero(sizeof(*j_inode), &j_inode);
333                 if (retval)
334                         goto errout;
335
336                 j_inode->i_fs = fs;
337                 j_inode->i_ino = sb->s_journal_inum;
338
339                 retval = ext2fs_read_inode(fs, sb->s_journal_inum,
340                                            &j_inode->i_ext2);
341                 if (retval) {
342 try_backup_journal:
343                         if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
344                             tried_backup_jnl)
345                                 goto errout;
346                         memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
347                         memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
348                                EXT2_N_BLOCKS*4);
349                         j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
350                         j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
351                         j_inode->i_ext2.i_links_count = 1;
352                         j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
353                         tried_backup_jnl++;
354                 }
355                 if (!j_inode->i_ext2.i_links_count ||
356                     !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
357                         retval = EXT2_ET_NO_JOURNAL;
358                         goto try_backup_journal;
359                 }
360                 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
361                     JBD2_MIN_JOURNAL_BLOCKS) {
362                         retval = EXT2_ET_JOURNAL_TOO_SMALL;
363                         goto try_backup_journal;
364                 }
365                 pb.last_block = -1;
366                 retval = ext2fs_block_iterate3(fs, j_inode->i_ino,
367                                                BLOCK_FLAG_HOLE, 0,
368                                                process_journal_block, &pb);
369                 if ((pb.last_block + 1) * fs->blocksize <
370                     (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
371                         retval = EXT2_ET_JOURNAL_TOO_SMALL;
372                         goto try_backup_journal;
373                 }
374                 if (tried_backup_jnl && (fs->flags & EXT2_FLAG_RW)) {
375                         retval = ext2fs_write_inode(fs, sb->s_journal_inum,
376                                                     &j_inode->i_ext2);
377                         if (retval)
378                                 goto errout;
379                 }
380
381                 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
382                         journal->j_blocksize;
383
384 #ifdef USE_INODE_IO
385                 retval = ext2fs_inode_io_intern2(fs, sb->s_journal_inum,
386                                                  &j_inode->i_ext2,
387                                                  &journal_name);
388                 if (retval)
389                         goto errout;
390
391                 io_ptr = inode_io_manager;
392 #else
393                 journal->j_inode = j_inode;
394                 fs->journal_io = fs->io;
395                 retval = (errcode_t) jbd2_journal_bmap(journal, 0, &start);
396                 if (retval)
397                         goto errout;
398 #endif
399         } else {
400                 ext_journal = 1;
401                 if (!fs->journal_name) {
402                         char uuid[37];
403                         blkid_cache blkid;
404
405                         blkid_get_cache(&blkid, NULL);
406                         uuid_unparse(sb->s_journal_uuid, uuid);
407                         fs->journal_name = blkid_get_devname(blkid,
408                                                               "UUID", uuid);
409                         if (!fs->journal_name)
410                                 fs->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
411                         blkid_put_cache(blkid);
412                 }
413                 journal_name = fs->journal_name;
414
415                 if (!journal_name) {
416                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
417                         goto errout;
418                 }
419
420                 jfs_debug(1, "Using journal file %s\n", journal_name);
421                 io_ptr = unix_io_manager;
422         }
423
424 #if 0
425         test_io_backing_manager = io_ptr;
426         io_ptr = test_io_manager;
427 #endif
428 #ifndef USE_INODE_IO
429         if (ext_journal)
430 #endif
431         {
432                 retval = io_ptr->open(journal_name, fs->flags & EXT2_FLAG_RW,
433                                       &fs->journal_io);
434         }
435         if (retval)
436                 goto errout;
437
438         io_channel_set_blksize(fs->journal_io, fs->blocksize);
439
440         if (ext_journal) {
441                 blk64_t maxlen;
442
443                 start = ext2fs_journal_sb_start(fs->blocksize) - 1;
444                 bh = getblk(dev_journal, start, fs->blocksize);
445                 if (!bh) {
446                         retval = EXT2_ET_NO_MEMORY;
447                         goto errout;
448                 }
449                 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
450                 retval = bh->b_err;
451                 if (retval) {
452                         brelse(bh);
453                         goto errout;
454                 }
455                 memcpy(&jsuper, start ? bh->b_data :
456                                 bh->b_data + SUPERBLOCK_OFFSET,
457                        sizeof(jsuper));
458 #ifdef WORDS_BIGENDIAN
459                 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
460                         ext2fs_swap_super(&jsuper);
461 #endif
462                 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
463                     !ext2fs_has_feature_journal_dev(&jsuper)) {
464                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
465                         brelse(bh);
466                         goto errout;
467                 }
468                 /* Make sure the journal UUID is correct */
469                 if (memcmp(jsuper.s_uuid, fs->super->s_journal_uuid,
470                            sizeof(jsuper.s_uuid))) {
471                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
472                         brelse(bh);
473                         goto errout;
474                 }
475
476                 /* Check the superblock checksum */
477                 if (ext2fs_has_feature_metadata_csum(&jsuper)) {
478                         struct struct_ext2_filsys fsx;
479                         struct ext2_super_block superx;
480                         void *p;
481
482                         p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
483                         memcpy(&fsx, fs, sizeof(fsx));
484                         memcpy(&superx, fs->super, sizeof(superx));
485                         fsx.super = &superx;
486                         ext2fs_set_feature_metadata_csum(fsx.super);
487                         if (!ext2fs_superblock_csum_verify(&fsx, p)) {
488                                 retval = EXT2_ET_LOAD_EXT_JOURNAL;
489                                 brelse(bh);
490                                 goto errout;
491                         }
492                 }
493                 brelse(bh);
494
495                 maxlen = ext2fs_blocks_count(&jsuper);
496                 journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen :
497                                     (1ULL << 32) - 1;
498                 start++;
499         }
500
501         bh = getblk(dev_journal, start, journal->j_blocksize);
502         if (!bh) {
503                 retval = EXT2_ET_NO_MEMORY;
504                 goto errout;
505         }
506
507         journal->j_sb_buffer = bh;
508         journal->j_superblock = (journal_superblock_t *)bh->b_data;
509
510 #ifdef USE_INODE_IO
511         if (j_inode)
512                 ext2fs_free_mem(&j_inode);
513 #endif
514
515         *ret_journal = journal;
516         return 0;
517
518 errout:
519         if (dev_fs)
520                 ext2fs_free_mem(&dev_fs);
521         if (j_inode)
522                 ext2fs_free_mem(&j_inode);
523         if (journal)
524                 ext2fs_free_mem(&journal);
525         return retval;
526 }
527
528 static errcode_t ext2fs_journal_fix_bad_inode(ext2_filsys fs)
529 {
530         struct ext2_super_block *sb = fs->super;
531         int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
532         int has_journal = ext2fs_has_feature_journal(fs->super);
533
534         if (has_journal || sb->s_journal_inum) {
535                 /* The journal inode is bogus, remove and force full fsck */
536                 return EXT2_ET_BAD_INODE_NUM;
537         } else if (recover) {
538                 return EXT2_ET_UNSUPP_FEATURE;
539         }
540         return 0;
541 }
542
543 #define V1_SB_SIZE      0x0024
544 static void clear_v2_journal_fields(journal_t *journal)
545 {
546         ext2_filsys fs = journal->j_dev->k_fs;
547
548         memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
549                fs->blocksize-V1_SB_SIZE);
550         mark_buffer_dirty(journal->j_sb_buffer);
551 }
552
553
554 static errcode_t ext2fs_journal_load(journal_t *journal)
555 {
556         ext2_filsys fs = journal->j_dev->k_fs;
557         journal_superblock_t *jsb;
558         struct buffer_head *jbh = journal->j_sb_buffer;
559
560         ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
561         if (jbh->b_err)
562                 return jbh->b_err;
563
564         jsb = journal->j_superblock;
565         /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
566         if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER))
567                 return ext2fs_journal_fix_bad_inode(fs);
568
569         switch (ntohl(jsb->s_header.h_blocktype)) {
570         case JBD2_SUPERBLOCK_V1:
571                 journal->j_format_version = 1;
572                 if (jsb->s_feature_compat ||
573                     jsb->s_feature_incompat ||
574                     jsb->s_feature_ro_compat ||
575                     jsb->s_nr_users)
576                         clear_v2_journal_fields(journal);
577                 break;
578
579         case JBD2_SUPERBLOCK_V2:
580                 journal->j_format_version = 2;
581                 if (ntohl(jsb->s_nr_users) > 1 &&
582                     uuid_is_null(fs->super->s_journal_uuid))
583                         clear_v2_journal_fields(journal);
584                 if (ntohl(jsb->s_nr_users) > 1)
585                         return EXT2_ET_JOURNAL_UNSUPP_VERSION;
586                 break;
587
588         /*
589          * These should never appear in a journal super block, so if
590          * they do, the journal is badly corrupted.
591          */
592         case JBD2_DESCRIPTOR_BLOCK:
593         case JBD2_COMMIT_BLOCK:
594         case JBD2_REVOKE_BLOCK:
595                 return EXT2_ET_CORRUPT_JOURNAL_SB;
596
597         /* If we don't understand the superblock major type, but there
598          * is a magic number, then it is likely to be a new format we
599          * just don't understand, so leave it alone. */
600         default:
601                 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
602         }
603
604         if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES))
605                 return EXT2_ET_UNSUPP_FEATURE;
606
607         if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES))
608                 return EXT2_ET_RO_UNSUPP_FEATURE;
609
610         /* Checksum v1-3 are mutually exclusive features. */
611         if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal))
612                 return EXT2_ET_CORRUPT_JOURNAL_SB;
613
614         if (jbd2_journal_has_csum_v2or3(journal) &&
615             jbd2_has_feature_checksum(journal))
616                 return EXT2_ET_CORRUPT_JOURNAL_SB;
617
618         if (!ext2fs_journal_verify_csum_type(journal, jsb) ||
619             !ext2fs_journal_sb_csum_verify(journal, jsb))
620                 return EXT2_ET_CORRUPT_JOURNAL_SB;
621
622         if (jbd2_journal_has_csum_v2or3(journal))
623                 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
624                                                    sizeof(jsb->s_uuid));
625
626         /* We have now checked whether we know enough about the journal
627          * format to be able to proceed safely, so any other checks that
628          * fail we should attempt to recover from. */
629         if (jsb->s_blocksize != htonl(journal->j_blocksize))
630                 return EXT2_ET_CORRUPT_JOURNAL_SB;
631
632         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
633                 journal->j_maxlen = ntohl(jsb->s_maxlen);
634         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen)
635                 return EXT2_ET_CORRUPT_JOURNAL_SB;
636
637         journal->j_tail_sequence = ntohl(jsb->s_sequence);
638         journal->j_transaction_sequence = journal->j_tail_sequence;
639         journal->j_tail = ntohl(jsb->s_start);
640         journal->j_first = ntohl(jsb->s_first);
641         journal->j_last = ntohl(jsb->s_maxlen);
642
643         return 0;
644 }
645
646 static void ext2fs_journal_release(ext2_filsys fs, journal_t *journal,
647                                    int reset, int drop)
648 {
649         journal_superblock_t *jsb;
650
651         if (drop)
652                 mark_buffer_clean(journal->j_sb_buffer);
653         else if (fs->flags & EXT2_FLAG_RW) {
654                 jsb = journal->j_superblock;
655                 jsb->s_sequence = htonl(journal->j_tail_sequence);
656                 if (reset)
657                         jsb->s_start = 0; /* this marks the journal as empty */
658                 ext2fs_journal_sb_csum_set(journal, jsb);
659                 mark_buffer_dirty(journal->j_sb_buffer);
660         }
661         brelse(journal->j_sb_buffer);
662
663         if (fs && fs->journal_io) {
664                 if (fs->io != fs->journal_io)
665                         io_channel_close(fs->journal_io);
666                 fs->journal_io = NULL;
667                 free(fs->journal_name);
668                 fs->journal_name = NULL;
669         }
670
671 #ifndef USE_INODE_IO
672         if (journal->j_inode)
673                 ext2fs_free_mem(&journal->j_inode);
674 #endif
675         if (journal->j_fs_dev)
676                 ext2fs_free_mem(&journal->j_fs_dev);
677         ext2fs_free_mem(&journal);
678 }
679
680 /*
681  * This function makes sure that the superblock fields regarding the
682  * journal are consistent.
683  */
684 static errcode_t ext2fs_check_ext3_journal(ext2_filsys fs)
685 {
686         struct ext2_super_block *sb = fs->super;
687         journal_t *journal;
688         int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
689         errcode_t retval;
690
691         /* If we don't have any journal features, don't do anything more */
692         if (!ext2fs_has_feature_journal(sb) &&
693             !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
694             uuid_is_null(sb->s_journal_uuid))
695                 return 0;
696
697         retval = ext2fs_get_journal(fs, &journal);
698         if (retval)
699                 return retval;
700
701         retval = ext2fs_journal_load(journal);
702         if (retval)
703                 goto err;
704
705         /*
706          * We want to make the flags consistent here.  We will not leave with
707          * needs_recovery set but has_journal clear.  We can't get in a loop
708          * with -y, -n, or -p, only if a user isn't making up their mind.
709          */
710         if (!ext2fs_has_feature_journal(sb)) {
711                 retval = EXT2_ET_JOURNAL_FLAGS_WRONG;
712                 goto err;
713         }
714
715         if (ext2fs_has_feature_journal(sb) &&
716             !ext2fs_has_feature_journal_needs_recovery(sb) &&
717             journal->j_superblock->s_start != 0) {
718                 retval = EXT2_ET_JOURNAL_FLAGS_WRONG;
719                 goto err;
720         }
721
722         /*
723          * If we don't need to do replay the journal, check to see if
724          * the journal's errno is set; if so, we need to mark the file
725          * system as being corrupt and clear the journal's s_errno.
726          */
727         if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
728             journal->j_superblock->s_errno) {
729                 fs->super->s_state |= EXT2_ERROR_FS;
730                 ext2fs_mark_super_dirty(fs);
731                 journal->j_superblock->s_errno = 0;
732                 ext2fs_journal_sb_csum_set(journal, journal->j_superblock);
733                 mark_buffer_dirty(journal->j_sb_buffer);
734         }
735
736 err:
737         ext2fs_journal_release(fs, journal, 0, retval ? 1 : 0);
738         return retval;
739 }
740
741 static errcode_t recover_ext3_journal(ext2_filsys fs)
742 {
743         journal_t *journal;
744         errcode_t retval;
745
746         retval = jbd2_journal_init_revoke_record_cache();
747         if (retval)
748                 return retval;
749
750         retval = jbd2_journal_init_revoke_table_cache();
751         if (retval)
752                 return retval;
753
754         retval = ext2fs_get_journal(fs, &journal);
755         if (retval)
756                 return retval;
757
758         retval = ext2fs_journal_load(journal);
759         if (retval)
760                 goto errout;
761
762         retval = jbd2_journal_init_revoke(journal, 1024);
763         if (retval)
764                 goto errout;
765
766         retval = -jbd2_journal_recover(journal);
767         if (retval)
768                 goto errout;
769
770         if (journal->j_failed_commit) {
771                 journal->j_superblock->s_errno = -EINVAL;
772                 mark_buffer_dirty(journal->j_sb_buffer);
773         }
774
775 errout:
776         jbd2_journal_destroy_revoke(journal);
777         jbd2_journal_destroy_revoke_record_cache();
778         jbd2_journal_destroy_revoke_table_cache();
779         ext2fs_journal_release(fs, journal, 1, 0);
780         return retval;
781 }
782
783 errcode_t ext2fs_run_ext3_journal(ext2_filsys *fsp)
784 {
785         ext2_filsys fs = *fsp;
786         io_manager io_ptr = fs->io->manager;
787         errcode_t       retval, recover_retval;
788         io_stats        stats = 0;
789         unsigned long long kbytes_written = 0;
790         char *fsname;
791         int fsflags;
792         int fsblocksize;
793
794         if (!(fs->flags & EXT2_FLAG_RW))
795                 return EXT2_ET_FILE_RO;
796
797         if (fs->flags & EXT2_FLAG_DIRTY)
798                 ext2fs_flush(fs);       /* Force out any modifications */
799
800         recover_retval = recover_ext3_journal(fs);
801
802         /*
803          * Reload the filesystem context to get up-to-date data from disk
804          * because journal recovery will change the filesystem under us.
805          */
806         if (fs->super->s_kbytes_written &&
807             fs->io->manager->get_stats)
808                 fs->io->manager->get_stats(fs->io, &stats);
809         if (stats && stats->bytes_written)
810                 kbytes_written = stats->bytes_written >> 10;
811
812         ext2fs_mmp_stop(fs);
813         fsname = fs->device_name;
814         fs->device_name = NULL;
815         fsflags = fs->flags;
816         fsblocksize = fs->blocksize;
817         ext2fs_free(fs);
818         *fsp = NULL;
819         retval = ext2fs_open(fsname, fsflags, 0, fsblocksize, io_ptr, fsp);
820         ext2fs_free_mem(&fsname);
821         if (retval)
822                 return retval;
823
824         fs = *fsp;
825         fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
826         fs->super->s_kbytes_written += kbytes_written;
827
828         /* Set the superblock flags */
829         ext2fs_clear_recover(fs, recover_retval != 0);
830
831         /*
832          * Do one last sanity check, and propagate journal->s_errno to
833          * the EXT2_ERROR_FS flag in the fs superblock if needed.
834          */
835         retval = ext2fs_check_ext3_journal(fs);
836         return retval ? retval : recover_retval;
837 }
838
839 errcode_t ext2fs_open_journal(ext2_filsys fs, journal_t **j)
840 {
841         journal_t *journal;
842         errcode_t retval;
843
844         retval = jbd2_journal_init_revoke_record_cache();
845         if (retval)
846                 return retval;
847
848         retval = jbd2_journal_init_revoke_table_cache();
849         if (retval)
850                 return retval;
851
852         retval = ext2fs_get_journal(fs, &journal);
853         if (retval)
854                 return retval;
855
856         retval = ext2fs_journal_load(journal);
857         if (retval)
858                 goto errout;
859
860         retval = jbd2_journal_init_revoke(journal, 1024);
861         if (retval)
862                 goto errout;
863
864         if (journal->j_failed_commit) {
865                 journal->j_superblock->s_errno = -EINVAL;
866                 mark_buffer_dirty(journal->j_sb_buffer);
867         }
868
869         *j = journal;
870         return 0;
871
872 errout:
873         jbd2_journal_destroy_revoke(journal);
874         jbd2_journal_destroy_revoke_record_cache();
875         jbd2_journal_destroy_revoke_table_cache();
876         ext2fs_journal_release(fs, journal, 1, 0);
877         return retval;
878 }
879
880 errcode_t ext2fs_close_journal(ext2_filsys fs, journal_t **j)
881 {
882         journal_t *journal = *j;
883
884         jbd2_journal_destroy_revoke(journal);
885         jbd2_journal_destroy_revoke_record_cache();
886         jbd2_journal_destroy_revoke_table_cache();
887         ext2fs_journal_release(fs, journal, 0, 0);
888         *j = NULL;
889
890         return 0;
891 }
892
893 void jbd2_commit_block_csum_set(journal_t *j, struct buffer_head *bh)
894 {
895         struct commit_header *h;
896         __u32 csum;
897
898         if (!jbd2_journal_has_csum_v2or3(j))
899                 return;
900
901         h = (struct commit_header *)(bh->b_data);
902         h->h_chksum_type = 0;
903         h->h_chksum_size = 0;
904         h->h_chksum[0] = 0;
905         csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
906         h->h_chksum[0] = ext2fs_cpu_to_be32(csum);
907 }
908
909 void jbd2_revoke_csum_set(journal_t *j, struct buffer_head *bh)
910 {
911         jbd2_descr_block_csum_set(j, bh);
912 }
913
914 void jbd2_descr_block_csum_set(journal_t *j, struct buffer_head *bh)
915 {
916         struct jbd2_journal_block_tail *tail;
917         __u32 csum;
918
919         if (!jbd2_journal_has_csum_v2or3(j))
920                 return;
921
922         tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
923                         sizeof(struct jbd2_journal_block_tail));
924         tail->t_checksum = 0;
925         csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
926         tail->t_checksum = ext2fs_cpu_to_be32(csum);
927 }
928
929 void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
930                              struct buffer_head *bh, __u32 sequence)
931 {
932         journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
933         __u32 csum32;
934         __be32 seq;
935
936         if (!jbd2_journal_has_csum_v2or3(j))
937                 return;
938
939         seq = ext2fs_cpu_to_be32(sequence);
940         csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
941         csum32 = jbd2_chksum(j, csum32, bh->b_data, bh->b_size);
942
943         if (jbd2_has_feature_csum3(j))
944                 tag3->t_checksum = ext2fs_cpu_to_be32(csum32);
945         else
946                 tag->t_checksum = ext2fs_cpu_to_be16(csum32);
947 }
948