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