OSDN Git Service

Fix e2fsck's handling of external journals,and update journal
[android-x86/external-e2fsprogs.git] / e2fsck / recovery.c
1 /*
2  * linux/fs/recovery.c
3  * 
4  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
5  *
6  * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
7  *
8  * This file is part of the Linux kernel and is made available under
9  * the terms of the GNU General Public License, version 2, or at your
10  * option, any later version, incorporated herein by reference.
11  *
12  * Journal recovery routines for the generic filesystem journaling code;
13  * part of the ext2fs journaling system.  
14  */
15
16 #ifndef __KERNEL__
17 #include "jfs_user.h"
18 #else
19 #include <linux/sched.h>
20 #include <linux/fs.h>
21 #include <linux/jbd.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/locks.h>
25 #endif
26
27 /*
28  * Maintain information about the progress of the recovery job, so that
29  * the different passes can carry information between them. 
30  */
31 struct recovery_info 
32 {
33         tid_t           start_transaction;      
34         tid_t           end_transaction;
35         
36         int             nr_replays;
37         int             nr_revokes;
38         int             nr_revoke_hits;
39 };
40
41 enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
42 static int do_one_pass(journal_t *journal,
43                                 struct recovery_info *info, enum passtype pass);
44 static int scan_revoke_records(journal_t *, struct buffer_head *,
45                                 tid_t, struct recovery_info *);
46
47 #ifdef __KERNEL__
48
49 /* Release readahead buffers after use */
50 void journal_brelse_array(struct buffer_head *b[], int n)
51 {
52         while (--n >= 0)
53                 brelse (b[n]);
54 }
55
56
57 /*
58  * When reading from the journal, we are going through the block device
59  * layer directly and so there is no readahead being done for us.  We
60  * need to implement any readahead ourselves if we want it to happen at
61  * all.  Recovery is basically one long sequential read, so make sure we
62  * do the IO in reasonably large chunks.
63  *
64  * This is not so critical that we need to be enormously clever about
65  * the readahead size, though.  128K is a purely arbitrary, good-enough
66  * fixed value.
67  */
68
69 #define MAXBUF 8
70 static int do_readahead(journal_t *journal, unsigned int start)
71 {
72         int err;
73         unsigned int max, nbufs, next;
74         unsigned long blocknr;
75         struct buffer_head *bh;
76         
77         struct buffer_head * bufs[MAXBUF];
78         
79         /* Do up to 128K of readahead */
80         max = start + (128 * 1024 / journal->j_blocksize);
81         if (max > journal->j_maxlen)
82                 max = journal->j_maxlen;
83
84         /* Do the readahead itself.  We'll submit MAXBUF buffer_heads at
85          * a time to the block device IO layer. */
86         
87         nbufs = 0;
88         
89         for (next = start; next < max; next++) {
90                 err = journal_bmap(journal, next, &blocknr);
91
92                 if (err) {
93                         printk (KERN_ERR "JBD: bad block at offset %u\n",
94                                 next);
95                         goto failed;
96                 }
97
98                 bh = getblk(journal->j_dev, blocknr, journal->j_blocksize);
99                 if (!bh) {
100                         err = -ENOMEM;
101                         goto failed;
102                 }
103
104                 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
105                         bufs[nbufs++] = bh;
106                         if (nbufs == MAXBUF) {
107                                 ll_rw_block(READ, nbufs, bufs);
108                                 journal_brelse_array(bufs, nbufs);
109                                 nbufs = 0;
110                         }
111                 } else
112                         brelse(bh);
113         }
114
115         if (nbufs)
116                 ll_rw_block(READ, nbufs, bufs);
117         err = 0;
118
119 failed: 
120         if (nbufs) 
121                 journal_brelse_array(bufs, nbufs);
122         return err;
123 }
124
125 #endif /* __KERNEL__ */
126
127
128 /*
129  * Read a block from the journal
130  */
131
132 static int jread(struct buffer_head **bhp, journal_t *journal, 
133                  unsigned int offset)
134 {
135         int err;
136         unsigned long blocknr;
137         struct buffer_head *bh;
138
139         *bhp = NULL;
140
141         J_ASSERT (offset < journal->j_maxlen);
142         
143         err = journal_bmap(journal, offset, &blocknr);
144
145         if (err) {
146                 printk (KERN_ERR "JBD: bad block at offset %u\n",
147                         offset);
148                 return err;
149         }
150
151         bh = getblk(journal->j_dev, blocknr, journal->j_blocksize);
152         if (!bh)
153                 return -ENOMEM;
154
155         if (!buffer_uptodate(bh)) {
156                 /* If this is a brand new buffer, start readahead.
157                    Otherwise, we assume we are already reading it.  */
158                 if (!buffer_req(bh))
159                         do_readahead(journal, offset);
160                 wait_on_buffer(bh);
161         }
162
163         if (!buffer_uptodate(bh)) {
164                 printk (KERN_ERR "JBD: Failed to read block at offset %u\n",
165                         offset);
166                 brelse(bh);
167                 return -EIO;
168         }
169
170         *bhp = bh;
171         return 0;
172 }
173
174
175 /*
176  * Count the number of in-use tags in a journal descriptor block.
177  */
178
179 static int count_tags(struct buffer_head *bh, int size)
180 {
181         char *                  tagp;
182         journal_block_tag_t *   tag;
183         int                     nr = 0;
184
185         tagp = &bh->b_data[sizeof(journal_header_t)];
186
187         while ((tagp - bh->b_data + sizeof(journal_block_tag_t)) <= size) {
188                 tag = (journal_block_tag_t *) tagp;
189
190                 nr++;
191                 tagp += sizeof(journal_block_tag_t);
192                 if (!(tag->t_flags & htonl(JFS_FLAG_SAME_UUID)))
193                         tagp += 16;
194
195                 if (tag->t_flags & htonl(JFS_FLAG_LAST_TAG))
196                         break;
197         }
198
199         return nr;
200 }
201
202
203 /* Make sure we wrap around the log correctly! */
204 #define wrap(journal, var)                                              \
205 do {                                                                    \
206         if (var >= (journal)->j_last)                                   \
207                 var -= ((journal)->j_last - (journal)->j_first);        \
208 } while (0)
209
210 /*
211  * journal_recover
212  *
213  * The primary function for recovering the log contents when mounting a
214  * journaled device.  
215  * 
216  * Recovery is done in three passes.  In the first pass, we look for the
217  * end of the log.  In the second, we assemble the list of revoke
218  * blocks.  In the third and final pass, we replay any un-revoked blocks
219  * in the log.  
220  */
221
222 int journal_recover(journal_t *journal)
223 {
224         int                     err;
225         journal_superblock_t *  sb;
226
227         struct recovery_info    info;
228         
229         memset(&info, 0, sizeof(info));
230         sb = journal->j_superblock;
231         
232         /* 
233          * The journal superblock's s_start field (the current log head)
234          * is always zero if, and only if, the journal was cleanly
235          * unmounted.  
236          */
237
238         if (!sb->s_start) {
239                 jbd_debug(1, "No recovery required, last transaction %d\n",
240                           ntohl(sb->s_sequence));
241                 journal->j_transaction_sequence = ntohl(sb->s_sequence) + 1;
242                 return 0;
243         }
244         
245
246         err = do_one_pass(journal, &info, PASS_SCAN);
247         if (!err)
248                 err = do_one_pass(journal, &info, PASS_REVOKE);
249         if (!err)
250                 err = do_one_pass(journal, &info, PASS_REPLAY);
251
252         jbd_debug(0, "JBD: recovery, exit status %d, "
253                   "recovered transactions %u to %u\n",
254                   err, info.start_transaction, info.end_transaction);
255         jbd_debug(0, "JBD: Replayed %d and revoked %d/%d blocks\n", 
256                   info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
257
258         /* Restart the log at the next transaction ID, thus invalidating
259          * any existing commit records in the log. */
260         journal->j_transaction_sequence = ++info.end_transaction;
261                 
262         journal_clear_revoke(journal);
263         fsync_no_super(journal->j_fs_dev);
264         return err;
265 }
266
267 /*
268  * journal_skip_recovery
269  *
270  * Locate any valid recovery information from the journal and set up the
271  * journal structures in memory to ignore it (presumably because the
272  * caller has evidence that it is out of date).  
273  *
274  * We perform one pass over the journal to allow us to tell the user how
275  * much recovery information is being erased, and to let us initialise
276  * the journal transaction sequence numbers to the next unused ID. 
277  */
278
279 int journal_skip_recovery(journal_t *journal)
280 {
281         int                     err;
282         journal_superblock_t *  sb;
283
284         struct recovery_info    info;
285         
286         memset (&info, 0, sizeof(info));
287         sb = journal->j_superblock;
288         
289         err = do_one_pass(journal, &info, PASS_SCAN);
290
291         if (err) {
292                 printk(KERN_ERR "JBD: error %d scanning journal\n", err);
293                 ++journal->j_transaction_sequence;
294         } else {
295 #ifdef CONFIG_JBD_DEBUG
296                 int dropped = info.end_transaction - ntohl(sb->s_sequence);
297 #endif
298                 
299                 jbd_debug(0, 
300                           "JBD: ignoring %d transaction%s from the journal.\n",
301                           dropped, (dropped == 1) ? "" : "s");
302                 journal->j_transaction_sequence = ++info.end_transaction;
303         }
304
305         journal->j_tail = 0;
306         
307         return err;
308 }
309
310 static int do_one_pass(journal_t *journal,
311                         struct recovery_info *info, enum passtype pass)
312 {
313         
314         unsigned int            first_commit_ID, next_commit_ID;
315         unsigned long           next_log_block;
316         int                     err, success = 0;
317         journal_superblock_t *  sb;
318         journal_header_t *      tmp;
319         struct buffer_head *    bh;
320         unsigned int            sequence;
321         int                     blocktype;
322         
323         /* Precompute the maximum metadata descriptors in a descriptor block */
324         int                     MAX_BLOCKS_PER_DESC;
325         MAX_BLOCKS_PER_DESC = ((journal->j_blocksize-sizeof(journal_header_t))
326                                / sizeof(journal_block_tag_t));
327
328         /* 
329          * First thing is to establish what we expect to find in the log
330          * (in terms of transaction IDs), and where (in terms of log
331          * block offsets): query the superblock.  
332          */
333
334         sb = journal->j_superblock;
335         next_commit_ID = ntohl(sb->s_sequence);
336         next_log_block = ntohl(sb->s_start);
337
338         first_commit_ID = next_commit_ID;
339         if (pass == PASS_SCAN)
340                 info->start_transaction = first_commit_ID;
341
342         jbd_debug(1, "Starting recovery pass %d\n", pass);
343
344         /*
345          * Now we walk through the log, transaction by transaction,
346          * making sure that each transaction has a commit block in the
347          * expected place.  Each complete transaction gets replayed back
348          * into the main filesystem. 
349          */
350
351         while (1) {
352                 int                     flags;
353                 char *                  tagp;
354                 journal_block_tag_t *   tag;
355                 struct buffer_head *    obh;
356                 struct buffer_head *    nbh;
357                 
358                 /* If we already know where to stop the log traversal,
359                  * check right now that we haven't gone past the end of
360                  * the log. */
361                 
362                 if (pass != PASS_SCAN)
363                         if (tid_geq(next_commit_ID, info->end_transaction))
364                                 break;
365
366                 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
367                           next_commit_ID, next_log_block, journal->j_last);
368
369                 /* Skip over each chunk of the transaction looking
370                  * either the next descriptor block or the final commit
371                  * record. */
372                 
373                 jbd_debug(3, "JBD: checking block %ld\n", next_log_block);
374                 err = jread(&bh, journal, next_log_block);
375                 if (err)
376                         goto failed;
377
378                 next_log_block++;
379                 wrap(journal, next_log_block);
380                 
381                 /* What kind of buffer is it? 
382                  * 
383                  * If it is a descriptor block, check that it has the
384                  * expected sequence number.  Otherwise, we're all done
385                  * here. */
386
387                 tmp = (journal_header_t *)bh->b_data;
388                 
389                 if (tmp->h_magic != htonl(JFS_MAGIC_NUMBER)) {
390                         brelse(bh);
391                         break;
392                 }
393
394                 blocktype = ntohl(tmp->h_blocktype);
395                 sequence = ntohl(tmp->h_sequence);
396                 jbd_debug(3, "Found magic %d, sequence %d\n", 
397                           blocktype, sequence);
398                 
399                 if (sequence != next_commit_ID) {
400                         brelse(bh);
401                         break;
402                 }
403                 
404                 /* OK, we have a valid descriptor block which matches
405                  * all of the sequence number checks.  What are we going
406                  * to do with it?  That depends on the pass... */
407
408                 switch(blocktype) {
409                 case JFS_DESCRIPTOR_BLOCK:
410                         /* If it is a valid descriptor block, replay it
411                          * in pass REPLAY; otherwise, just skip over the
412                          * blocks it describes. */
413                         if (pass != PASS_REPLAY) {
414                                 next_log_block +=
415                                         count_tags(bh, journal->j_blocksize);
416                                 wrap(journal, next_log_block);
417                                 brelse(bh);
418                                 continue;
419                         }
420
421                         /* A descriptor block: we can now write all of
422                          * the data blocks.  Yay, useful work is finally
423                          * getting done here! */
424
425                         tagp = &bh->b_data[sizeof(journal_header_t)];
426                         while ((tagp - bh->b_data +sizeof(journal_block_tag_t))
427                                <= journal->j_blocksize) {
428                                 unsigned long io_block;
429
430                                 tag = (journal_block_tag_t *) tagp;
431                                 flags = ntohl(tag->t_flags);
432                                 
433                                 io_block = next_log_block++;
434                                 wrap(journal, next_log_block);
435                                 err = jread(&obh, journal, io_block);
436                                 if (err) {
437                                         /* Recover what we can, but
438                                          * report failure at the end. */
439                                         success = err;
440                                         printk (KERN_ERR 
441                                                 "JBD: IO error %d recovering "
442                                                 "block %ld in log\n",
443                                                 err, io_block);
444                                 } else {
445                                         unsigned long blocknr;
446                                         
447                                         J_ASSERT(obh != NULL);
448                                         blocknr = ntohl(tag->t_blocknr);
449
450                                         /* If the block has been
451                                          * revoked, then we're all done
452                                          * here. */
453                                         if (journal_test_revoke
454                                             (journal, blocknr, 
455                                              next_commit_ID)) {
456                                                 brelse(obh);
457                                                 ++info->nr_revoke_hits;
458                                                 goto skip_write;
459                                         }
460                                                                 
461                                         /* Find a buffer for the new
462                                          * data being restored */
463                                         nbh = getblk(journal->j_fs_dev, blocknr,
464                                                      journal->j_blocksize);
465                                         if (nbh == NULL) {
466                                                 printk(KERN_ERR 
467                                                        "JBD: Out of memory "
468                                                        "during recovery.\n");
469                                                 err = -ENOMEM;
470                                                 brelse(bh);
471                                                 brelse(obh);
472                                                 goto failed;
473                                         }
474
475                                         memcpy(nbh->b_data, obh->b_data,
476                                                         journal->j_blocksize);
477                                         if (flags & JFS_FLAG_ESCAPE) {
478                                                 *((unsigned int *)bh->b_data) =
479                                                         htonl(JFS_MAGIC_NUMBER);
480                                         }
481
482                                         BUFFER_TRACE(nbh, "marking dirty");
483                                         mark_buffer_dirty(nbh);
484                                         BUFFER_TRACE(nbh, "marking uptodate");
485                                         mark_buffer_uptodate(nbh, 1);
486                                         ++info->nr_replays;
487                                         /* ll_rw_block(WRITE, 1, &nbh); */
488                                         brelse(obh);
489                                         brelse(nbh);
490                                 }
491                                 
492                         skip_write:
493                                 tagp += sizeof(journal_block_tag_t);
494                                 if (!(flags & JFS_FLAG_SAME_UUID))
495                                         tagp += 16;
496
497                                 if (flags & JFS_FLAG_LAST_TAG)
498                                         break;
499                         }
500                         
501                         brelse(bh);
502                         continue;
503
504                 case JFS_COMMIT_BLOCK:
505                         /* Found an expected commit block: not much to
506                          * do other than move on to the next sequence
507                          * number. */
508                         brelse(bh);
509                         next_commit_ID++;
510                         continue;
511
512                 case JFS_REVOKE_BLOCK:
513                         /* If we aren't in the REVOKE pass, then we can
514                          * just skip over this block. */
515                         if (pass != PASS_REVOKE) {
516                                 brelse(bh);
517                                 continue;
518                         }
519
520                         err = scan_revoke_records(journal, bh,
521                                                   next_commit_ID, info);
522                         brelse(bh);
523                         if (err)
524                                 goto failed;
525                         continue;
526
527                 default:
528                         jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
529                                   blocktype);
530                         goto done;
531                 }
532         }
533
534  done:
535         /* 
536          * We broke out of the log scan loop: either we came to the
537          * known end of the log or we found an unexpected block in the
538          * log.  If the latter happened, then we know that the "current"
539          * transaction marks the end of the valid log.
540          */
541         
542         if (pass == PASS_SCAN)
543                 info->end_transaction = next_commit_ID;
544         else {
545                 /* It's really bad news if different passes end up at
546                  * different places (but possible due to IO errors). */
547                 if (info->end_transaction != next_commit_ID) {
548                         printk (KERN_ERR "JBD: recovery pass %d ended at "
549                                 "transaction %u, expected %u\n",
550                                 pass, next_commit_ID, info->end_transaction);
551                         if (!success)
552                                 success = -EIO;
553                 }
554         }
555
556         return success;
557
558  failed:
559         return err;
560 }
561
562
563 /* Scan a revoke record, marking all blocks mentioned as revoked. */
564
565 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh, 
566                                tid_t sequence, struct recovery_info *info)
567 {
568         journal_revoke_header_t *header;
569         int offset, max;
570
571         header = (journal_revoke_header_t *) bh->b_data;
572         offset = sizeof(journal_revoke_header_t);
573         max = ntohl(header->r_count);
574         
575         while (offset < max) {
576                 unsigned long blocknr;
577                 int err;
578                 
579                 blocknr = ntohl(* ((unsigned int *) (bh->b_data+offset)));
580                 offset += 4;
581                 err = journal_set_revoke(journal, blocknr, sequence);
582                 if (err)
583                         return err;
584                 ++info->nr_revokes;
585         }
586         return 0;
587 }