import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / fs / jbd / 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         if (offset >= journal->j_maxlen) {
142                 printk(KERN_ERR "JBD: corrupted journal superblock\n");
143                 return -EIO;
144         }
145
146         err = journal_bmap(journal, offset, &blocknr);
147
148         if (err) {
149                 printk (KERN_ERR "JBD: bad block at offset %u\n",
150                         offset);
151                 return err;
152         }
153
154         bh = getblk(journal->j_dev, blocknr, journal->j_blocksize);
155         if (!bh)
156                 return -ENOMEM;
157
158         if (!buffer_uptodate(bh)) {
159                 /* If this is a brand new buffer, start readahead.
160                    Otherwise, we assume we are already reading it.  */
161                 if (!buffer_req(bh))
162                         do_readahead(journal, offset);
163                 wait_on_buffer(bh);
164         }
165
166         if (!buffer_uptodate(bh)) {
167                 printk (KERN_ERR "JBD: Failed to read block at offset %u\n",
168                         offset);
169                 brelse(bh);
170                 return -EIO;
171         }
172
173         *bhp = bh;
174         return 0;
175 }
176
177
178 /*
179  * Count the number of in-use tags in a journal descriptor block.
180  */
181
182 static int count_tags(struct buffer_head *bh, int size)
183 {
184         char *                  tagp;
185         journal_block_tag_t *   tag;
186         int                     nr = 0;
187
188         tagp = &bh->b_data[sizeof(journal_header_t)];
189
190         while ((tagp - bh->b_data + sizeof(journal_block_tag_t)) <= size) {
191                 tag = (journal_block_tag_t *) tagp;
192
193                 nr++;
194                 tagp += sizeof(journal_block_tag_t);
195                 if (!(tag->t_flags & htonl(JFS_FLAG_SAME_UUID)))
196                         tagp += 16;
197
198                 if (tag->t_flags & htonl(JFS_FLAG_LAST_TAG))
199                         break;
200         }
201
202         return nr;
203 }
204
205
206 /* Make sure we wrap around the log correctly! */
207 #define wrap(journal, var)                                              \
208 do {                                                                    \
209         if (var >= (journal)->j_last)                                   \
210                 var -= ((journal)->j_last - (journal)->j_first);        \
211 } while (0)
212
213 /**
214  * int journal_recover(journal_t *journal) - recovers a on-disk journal
215  * @journal: the journal to recover
216  * 
217  * The primary function for recovering the log contents when mounting a
218  * journaled device.  
219  */
220 int journal_recover(journal_t *journal)
221 {
222 /*
223  * Recovery is done in three passes.  In the first pass, we look for the
224  * end of the log.  In the second, we assemble the list of revoke
225  * blocks.  In the third and final pass, we replay any un-revoked blocks
226  * in the log.  
227  */
228
229         int                     err;
230         journal_superblock_t *  sb;
231
232         struct recovery_info    info;
233         
234         memset(&info, 0, sizeof(info));
235         sb = journal->j_superblock;
236         
237         /* 
238          * The journal superblock's s_start field (the current log head)
239          * is always zero if, and only if, the journal was cleanly
240          * unmounted.  
241          */
242
243         if (!sb->s_start) {
244                 jbd_debug(1, "No recovery required, last transaction %d\n",
245                           ntohl(sb->s_sequence));
246                 journal->j_transaction_sequence = ntohl(sb->s_sequence) + 1;
247                 return 0;
248         }
249         
250
251         err = do_one_pass(journal, &info, PASS_SCAN);
252         if (!err)
253                 err = do_one_pass(journal, &info, PASS_REVOKE);
254         if (!err)
255                 err = do_one_pass(journal, &info, PASS_REPLAY);
256
257         jbd_debug(0, "JBD: recovery, exit status %d, "
258                   "recovered transactions %u to %u\n",
259                   err, info.start_transaction, info.end_transaction);
260         jbd_debug(0, "JBD: Replayed %d and revoked %d/%d blocks\n", 
261                   info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
262
263         /* Restart the log at the next transaction ID, thus invalidating
264          * any existing commit records in the log. */
265         journal->j_transaction_sequence = ++info.end_transaction;
266                 
267         journal_clear_revoke(journal);
268         fsync_no_super(journal->j_fs_dev);
269         return err;
270 }
271
272 /**
273  * int journal_skip_recovery() - Start journal and wipe exiting records 
274  * @journal: journal to startup
275  * 
276  * Locate any valid recovery information from the journal and set up the
277  * journal structures in memory to ignore it (presumably because the
278  * caller has evidence that it is out of date).  
279  * This function does'nt appear to be exorted..
280  */
281 int journal_skip_recovery(journal_t *journal)
282 {
283 /*
284  * We perform one pass over the journal to allow us to tell the user how
285  * much recovery information is being erased, and to let us initialise
286  * the journal transaction sequence numbers to the next unused ID. 
287  */
288
289         int                     err;
290         journal_superblock_t *  sb;
291
292         struct recovery_info    info;
293         
294         memset (&info, 0, sizeof(info));
295         sb = journal->j_superblock;
296         
297         err = do_one_pass(journal, &info, PASS_SCAN);
298
299         if (err) {
300                 printk(KERN_ERR "JBD: error %d scanning journal\n", err);
301                 ++journal->j_transaction_sequence;
302         } else {
303 #ifdef CONFIG_JBD_DEBUG
304                 int dropped = info.end_transaction - ntohl(sb->s_sequence);
305 #endif
306                 
307                 jbd_debug(0, 
308                           "JBD: ignoring %d transaction%s from the journal.\n",
309                           dropped, (dropped == 1) ? "" : "s");
310                 journal->j_transaction_sequence = ++info.end_transaction;
311         }
312
313         journal->j_tail = 0;
314         
315         return err;
316 }
317
318 static int do_one_pass(journal_t *journal,
319                         struct recovery_info *info, enum passtype pass)
320 {
321         
322         unsigned int            first_commit_ID, next_commit_ID;
323         unsigned long           next_log_block;
324         int                     err, success = 0;
325         journal_superblock_t *  sb;
326         journal_header_t *      tmp;
327         struct buffer_head *    bh;
328         unsigned int            sequence;
329         int                     blocktype;
330         
331         /* Precompute the maximum metadata descriptors in a descriptor block */
332         int                     MAX_BLOCKS_PER_DESC;
333         MAX_BLOCKS_PER_DESC = ((journal->j_blocksize-sizeof(journal_header_t))
334                                / sizeof(journal_block_tag_t));
335
336         /* 
337          * First thing is to establish what we expect to find in the log
338          * (in terms of transaction IDs), and where (in terms of log
339          * block offsets): query the superblock.  
340          */
341
342         sb = journal->j_superblock;
343         next_commit_ID = ntohl(sb->s_sequence);
344         next_log_block = ntohl(sb->s_start);
345
346         first_commit_ID = next_commit_ID;
347         if (pass == PASS_SCAN)
348                 info->start_transaction = first_commit_ID;
349
350         jbd_debug(1, "Starting recovery pass %d\n", pass);
351
352         /*
353          * Now we walk through the log, transaction by transaction,
354          * making sure that each transaction has a commit block in the
355          * expected place.  Each complete transaction gets replayed back
356          * into the main filesystem. 
357          */
358
359         while (1) {
360                 int                     flags;
361                 char *                  tagp;
362                 journal_block_tag_t *   tag;
363                 struct buffer_head *    obh;
364                 struct buffer_head *    nbh;
365                 
366                 /* If we already know where to stop the log traversal,
367                  * check right now that we haven't gone past the end of
368                  * the log. */
369                 
370                 if (pass != PASS_SCAN)
371                         if (tid_geq(next_commit_ID, info->end_transaction))
372                                 break;
373
374                 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
375                           next_commit_ID, next_log_block, journal->j_last);
376
377                 /* Skip over each chunk of the transaction looking
378                  * either the next descriptor block or the final commit
379                  * record. */
380                 
381                 jbd_debug(3, "JBD: checking block %ld\n", next_log_block);
382                 err = jread(&bh, journal, next_log_block);
383                 if (err)
384                         goto failed;
385
386                 next_log_block++;
387                 wrap(journal, next_log_block);
388                 
389                 /* What kind of buffer is it? 
390                  * 
391                  * If it is a descriptor block, check that it has the
392                  * expected sequence number.  Otherwise, we're all done
393                  * here. */
394
395                 tmp = (journal_header_t *)bh->b_data;
396                 
397                 if (tmp->h_magic != htonl(JFS_MAGIC_NUMBER)) {
398                         brelse(bh);
399                         break;
400                 }
401
402                 blocktype = ntohl(tmp->h_blocktype);
403                 sequence = ntohl(tmp->h_sequence);
404                 jbd_debug(3, "Found magic %d, sequence %d\n", 
405                           blocktype, sequence);
406                 
407                 if (sequence != next_commit_ID) {
408                         brelse(bh);
409                         break;
410                 }
411                 
412                 /* OK, we have a valid descriptor block which matches
413                  * all of the sequence number checks.  What are we going
414                  * to do with it?  That depends on the pass... */
415
416                 switch(blocktype) {
417                 case JFS_DESCRIPTOR_BLOCK:
418                         /* If it is a valid descriptor block, replay it
419                          * in pass REPLAY; otherwise, just skip over the
420                          * blocks it describes. */
421                         if (pass != PASS_REPLAY) {
422                                 next_log_block +=
423                                         count_tags(bh, journal->j_blocksize);
424                                 wrap(journal, next_log_block);
425                                 brelse(bh);
426                                 continue;
427                         }
428
429                         /* A descriptor block: we can now write all of
430                          * the data blocks.  Yay, useful work is finally
431                          * getting done here! */
432
433                         tagp = &bh->b_data[sizeof(journal_header_t)];
434                         while ((tagp - bh->b_data +sizeof(journal_block_tag_t))
435                                <= journal->j_blocksize) {
436                                 unsigned long io_block;
437
438                                 tag = (journal_block_tag_t *) tagp;
439                                 flags = ntohl(tag->t_flags);
440                                 
441                                 io_block = next_log_block++;
442                                 wrap(journal, next_log_block);
443                                 err = jread(&obh, journal, io_block);
444                                 if (err) {
445                                         /* Recover what we can, but
446                                          * report failure at the end. */
447                                         success = err;
448                                         printk (KERN_ERR 
449                                                 "JBD: IO error %d recovering "
450                                                 "block %ld in log\n",
451                                                 err, io_block);
452                                 } else {
453                                         unsigned long blocknr;
454                                         
455                                         J_ASSERT(obh != NULL);
456                                         blocknr = ntohl(tag->t_blocknr);
457
458                                         /* If the block has been
459                                          * revoked, then we're all done
460                                          * here. */
461                                         if (journal_test_revoke
462                                             (journal, blocknr, 
463                                              next_commit_ID)) {
464                                                 brelse(obh);
465                                                 ++info->nr_revoke_hits;
466                                                 goto skip_write;
467                                         }
468                                                                 
469                                         /* Find a buffer for the new
470                                          * data being restored */
471                                         nbh = getblk(journal->j_fs_dev, blocknr,
472                                                      journal->j_blocksize);
473                                         if (nbh == NULL) {
474                                                 printk(KERN_ERR 
475                                                        "JBD: Out of memory "
476                                                        "during recovery.\n");
477                                                 err = -ENOMEM;
478                                                 brelse(bh);
479                                                 brelse(obh);
480                                                 goto failed;
481                                         }
482
483                                         lock_buffer(nbh);
484                                         memcpy(nbh->b_data, obh->b_data,
485                                                         journal->j_blocksize);
486                                         if (flags & JFS_FLAG_ESCAPE) {
487                                                 *((unsigned int *)bh->b_data) =
488                                                         htonl(JFS_MAGIC_NUMBER);
489                                         }
490
491                                         BUFFER_TRACE(nbh, "marking dirty");
492                                         mark_buffer_dirty(nbh);
493                                         BUFFER_TRACE(nbh, "marking uptodate");
494                                         mark_buffer_uptodate(nbh, 1);
495                                         unlock_buffer(nbh);
496                                         ++info->nr_replays;
497                                         /* ll_rw_block(WRITE, 1, &nbh); */
498                                         brelse(obh);
499                                         brelse(nbh);
500                                 }
501                                 
502                         skip_write:
503                                 tagp += sizeof(journal_block_tag_t);
504                                 if (!(flags & JFS_FLAG_SAME_UUID))
505                                         tagp += 16;
506
507                                 if (flags & JFS_FLAG_LAST_TAG)
508                                         break;
509                         }
510                         
511                         brelse(bh);
512                         continue;
513
514                 case JFS_COMMIT_BLOCK:
515                         /* Found an expected commit block: not much to
516                          * do other than move on to the next sequence
517                          * number. */
518                         brelse(bh);
519                         next_commit_ID++;
520                         continue;
521
522                 case JFS_REVOKE_BLOCK:
523                         /* If we aren't in the REVOKE pass, then we can
524                          * just skip over this block. */
525                         if (pass != PASS_REVOKE) {
526                                 brelse(bh);
527                                 continue;
528                         }
529
530                         err = scan_revoke_records(journal, bh,
531                                                   next_commit_ID, info);
532                         brelse(bh);
533                         if (err)
534                                 goto failed;
535                         continue;
536
537                 default:
538                         jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
539                                   blocktype);
540                         brelse(bh);
541                         goto done;
542                 }
543         }
544
545  done:
546         /* 
547          * We broke out of the log scan loop: either we came to the
548          * known end of the log or we found an unexpected block in the
549          * log.  If the latter happened, then we know that the "current"
550          * transaction marks the end of the valid log.
551          */
552         
553         if (pass == PASS_SCAN)
554                 info->end_transaction = next_commit_ID;
555         else {
556                 /* It's really bad news if different passes end up at
557                  * different places (but possible due to IO errors). */
558                 if (info->end_transaction != next_commit_ID) {
559                         printk (KERN_ERR "JBD: recovery pass %d ended at "
560                                 "transaction %u, expected %u\n",
561                                 pass, next_commit_ID, info->end_transaction);
562                         if (!success)
563                                 success = -EIO;
564                 }
565         }
566
567         return success;
568
569  failed:
570         return err;
571 }
572
573
574 /* Scan a revoke record, marking all blocks mentioned as revoked. */
575
576 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh, 
577                                tid_t sequence, struct recovery_info *info)
578 {
579         journal_revoke_header_t *header;
580         int offset, max;
581
582         header = (journal_revoke_header_t *) bh->b_data;
583         offset = sizeof(journal_revoke_header_t);
584         max = ntohl(header->r_count);
585         
586         while (offset < max) {
587                 unsigned long blocknr;
588                 int err;
589                 
590                 blocknr = ntohl(* ((unsigned int *) (bh->b_data+offset)));
591                 offset += 4;
592                 err = journal_set_revoke(journal, blocknr, sequence);
593                 if (err)
594                         return err;
595                 ++info->nr_revokes;
596         }
597         return 0;
598 }