2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
36 * This routine finds (to an approximation) the first block in the physical
37 * log which contains the given cycle. It uses a binary search algorithm.
38 * Note that the algorithm can not be perfect because the disk will not
39 * necessarily be perfect.
42 xlog_find_cycle_start(xlog_t
*log
,
44 xfs_daddr_t first_blk
,
45 xfs_daddr_t
*last_blk
,
52 mid_blk
= BLK_AVG(first_blk
, *last_blk
);
53 while (mid_blk
!= first_blk
&& mid_blk
!= *last_blk
) {
54 if ((error
= xlog_bread(log
, mid_blk
, 1, bp
)))
56 mid_cycle
= GET_CYCLE(XFS_BUF_PTR(bp
), ARCH_CONVERT
);
57 if (mid_cycle
== cycle
) {
59 /* last_half_cycle == mid_cycle */
62 /* first_half_cycle == mid_cycle */
64 mid_blk
= BLK_AVG(first_blk
, *last_blk
);
66 ASSERT((mid_blk
== first_blk
&& mid_blk
+1 == *last_blk
) ||
67 (mid_blk
== *last_blk
&& mid_blk
-1 == first_blk
));
70 } /* xlog_find_cycle_start */
74 * Check that the range of blocks does not contain the cycle number
75 * given. The scan needs to occur from front to back and the ptr into the
76 * region must be updated since a later routine will need to perform another
77 * test. If the region is completely good, we end up returning the same
80 * Set blkno to -1 if we encounter no errors. This is an invalid block number
81 * since we don't ever expect logs to get this large.
85 xlog_find_verify_cycle( xlog_t
*log
,
86 xfs_daddr_t start_blk
,
88 uint stop_on_cycle_no
,
96 xfs_daddr_t bufblks
= nbblks
;
98 while (!(bp
= xlog_get_bp(bufblks
, log
->l_mp
))) {
99 /* can't get enough memory to do everything in one big buffer */
106 for (i
= start_blk
; i
< start_blk
+ nbblks
; i
+= bufblks
) {
107 int bcount
= min(bufblks
, (start_blk
+ nbblks
- i
));
109 if ((error
= xlog_bread(log
, i
, bcount
, bp
)))
112 buf
= XFS_BUF_PTR(bp
);
113 for (j
= 0; j
< bcount
; j
++) {
114 cycle
= GET_CYCLE(buf
, ARCH_CONVERT
);
115 if (cycle
== stop_on_cycle_no
) {
130 } /* xlog_find_verify_cycle */
134 * Potentially backup over partial log record write.
136 * In the typical case, last_blk is the number of the block directly after
137 * a good log record. Therefore, we subtract one to get the block number
138 * of the last block in the given buffer. extra_bblks contains the number
139 * of blocks we would have read on a previous read. This happens when the
140 * last log record is split over the end of the physical log.
142 * extra_bblks is the number of blocks potentially verified on a previous
143 * call to this routine.
147 xlog_find_verify_log_record(xlog_t
*log
,
148 xfs_daddr_t start_blk
,
149 xfs_daddr_t
*last_blk
,
155 xlog_rec_header_t
*head
= NULL
;
158 int num_blks
= *last_blk
- start_blk
;
160 ASSERT(start_blk
!= 0 || *last_blk
!= start_blk
);
162 if (!(bp
= xlog_get_bp(num_blks
, log
->l_mp
))) {
163 if (!(bp
= xlog_get_bp(1, log
->l_mp
)))
166 buf
= XFS_BUF_PTR(bp
);
168 if ((error
= xlog_bread(log
, start_blk
, num_blks
, bp
)))
170 buf
= XFS_BUF_PTR(bp
) + (num_blks
- 1) * BBSIZE
;
174 for (i
=(*last_blk
)-1; i
>=0; i
--) {
176 /* legal log record not found */
177 xlog_warn("XFS: Log inconsistent (didn't find previous header)");
179 error
= XFS_ERROR(EIO
);
183 if (smallmem
&& (error
= xlog_bread(log
, i
, 1, bp
)))
185 head
= (xlog_rec_header_t
*)buf
;
187 if (INT_GET(head
->h_magicno
, ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
)
195 * We hit the beginning of the physical log & still no header. Return
196 * to caller. If caller can handle a return of -1, then this routine
197 * will be called again for the end of the physical log.
204 /* we have the final block of the good log (the first block
205 * of the log record _before_ the head. So we check the uuid.
208 if ((error
= xlog_header_check_mount(log
->l_mp
, head
)))
212 * We may have found a log record header before we expected one.
213 * last_blk will be the 1st block # with a given cycle #. We may end
214 * up reading an entire log record. In this case, we don't want to
215 * reset last_blk. Only when last_blk points in the middle of a log
216 * record do we update last_blk.
218 if (*last_blk
- i
+ extra_bblks
219 != BTOBB(INT_GET(head
->h_len
, ARCH_CONVERT
))+1)
226 } /* xlog_find_verify_log_record */
229 * Head is defined to be the point of the log where the next log write
230 * write could go. This means that incomplete LR writes at the end are
231 * eliminated when calculating the head. We aren't guaranteed that previous
232 * LR have complete transactions. We only know that a cycle number of
233 * current cycle number -1 won't be present in the log if we start writing
234 * from our current block number.
236 * last_blk contains the block number of the first block with a given
239 * Also called from xfs_log_print.c
241 * Return: zero if normal, non-zero if error.
244 xlog_find_head(xlog_t
*log
,
245 xfs_daddr_t
*return_head_blk
)
248 xfs_daddr_t new_blk
, first_blk
, start_blk
, last_blk
, head_blk
;
250 uint first_half_cycle
, last_half_cycle
;
252 int error
, log_bbnum
= log
->l_logBBsize
;
254 /* Is the end of the log device zeroed? */
255 if ((error
= xlog_find_zeroed(log
, &first_blk
)) == -1) {
256 *return_head_blk
= first_blk
;
258 /* is the whole lot zeroed? */
260 /* Linux XFS shouldn't generate totally zeroed logs -
261 * mkfs etc write a dummy unmount record to a fresh
262 * log so we can store the uuid in there
264 xlog_warn("XFS: totally zeroed log\n");
269 xlog_warn("XFS: empty log check failed");
273 first_blk
= 0; /* get cycle # of 1st block */
274 bp
= xlog_get_bp(1,log
->l_mp
);
277 if ((error
= xlog_bread(log
, 0, 1, bp
)))
279 first_half_cycle
= GET_CYCLE(XFS_BUF_PTR(bp
), ARCH_CONVERT
);
281 last_blk
= head_blk
= log_bbnum
-1; /* get cycle # of last block */
282 if ((error
= xlog_bread(log
, last_blk
, 1, bp
)))
284 last_half_cycle
= GET_CYCLE(XFS_BUF_PTR(bp
), ARCH_CONVERT
);
285 ASSERT(last_half_cycle
!= 0);
288 * If the 1st half cycle number is equal to the last half cycle number,
289 * then the entire log is stamped with the same cycle number. In this
290 * case, head_blk can't be set to zero (which makes sense). The below
291 * math doesn't work out properly with head_blk equal to zero. Instead,
292 * we set it to log_bbnum which is an illegal block number, but this
293 * value makes the math correct. If head_blk doesn't changed through
294 * all the tests below, *head_blk is set to zero at the very end rather
295 * than log_bbnum. In a sense, log_bbnum and zero are the same block
296 * in a circular file.
298 if (first_half_cycle
== last_half_cycle
) {
300 * In this case we believe that the entire log should have cycle
301 * number last_half_cycle. We need to scan backwards from the
302 * end verifying that there are no holes still containing
303 * last_half_cycle - 1. If we find such a hole, then the start
304 * of that hole will be the new head. The simple case looks like
305 * x | x ... | x - 1 | x
306 * Another case that fits this picture would be
307 * x | x + 1 | x ... | x
308 * In this case the head really is somwhere at the end of the
309 * log, as one of the latest writes at the beginning was incomplete.
311 * x | x + 1 | x ... | x - 1 | x
312 * This is really the combination of the above two cases, and the
313 * head has to end up at the start of the x-1 hole at the end of
316 * In the 256k log case, we will read from the beginning to the
317 * end of the log and search for cycle numbers equal to x-1. We
318 * don't worry about the x+1 blocks that we encounter, because
319 * we know that they cannot be the head since the log started with
322 head_blk
= log_bbnum
;
323 stop_on_cycle
= last_half_cycle
- 1;
326 * In this case we want to find the first block with cycle number
327 * matching last_half_cycle. We expect the log to be some
330 * The first block with cycle number x (last_half_cycle) will be
331 * where the new head belongs. First we do a binary search for
332 * the first occurrence of last_half_cycle. The binary search
333 * may not be totally accurate, so then we scan back from there
334 * looking for occurrences of last_half_cycle before us. If
335 * that backwards scan wraps around the beginning of the log,
336 * then we look for occurrences of last_half_cycle - 1 at the
337 * end of the log. The cases we're looking for look like
338 * x + 1 ... | x | x + 1 | x ...
339 * ^ binary search stopped here
341 * x + 1 ... | x ... | x - 1 | x
342 * <---------> less than scan distance
344 stop_on_cycle
= last_half_cycle
;
345 if ((error
= xlog_find_cycle_start(log
, bp
, first_blk
,
346 &head_blk
, last_half_cycle
)))
351 * Now validate the answer. Scan back some number of maximum possible
352 * blocks and make sure each one has the expected cycle number. The
353 * maximum is determined by the total possible amount of buffering
354 * in the in-core log. The following number can be made tighter if
355 * we actually look at the block size of the filesystem.
357 num_scan_bblks
= BTOBB(XLOG_MAX_ICLOGS
<<XLOG_MAX_RECORD_BSHIFT
);
358 if (head_blk
>= num_scan_bblks
) {
360 * We are guaranteed that the entire check can be performed
363 start_blk
= head_blk
- num_scan_bblks
;
364 if ((error
= xlog_find_verify_cycle(log
, start_blk
, num_scan_bblks
,
365 stop_on_cycle
, &new_blk
)))
369 } else { /* need to read 2 parts of log */
371 * We are going to scan backwards in the log in two parts. First
372 * we scan the physical end of the log. In this part of the log,
373 * we are looking for blocks with cycle number last_half_cycle - 1.
374 * If we find one, then we know that the log starts there, as we've
375 * found a hole that didn't get written in going around the end
376 * of the physical log. The simple case for this is
377 * x + 1 ... | x ... | x - 1 | x
378 * <---------> less than scan distance
379 * If all of the blocks at the end of the log have cycle number
380 * last_half_cycle, then we check the blocks at the start of the
381 * log looking for occurrences of last_half_cycle. If we find one,
382 * then our current estimate for the location of the first
383 * occurrence of last_half_cycle is wrong and we move back to the
384 * hole we've found. This case looks like
385 * x + 1 ... | x | x + 1 | x ...
386 * ^ binary search stopped here
387 * Another case we need to handle that only occurs in 256k logs is
388 * x + 1 ... | x ... | x+1 | x ...
389 * ^ binary search stops here
390 * In a 256k log, the scan at the end of the log will see the x+1
391 * blocks. We need to skip past those since that is certainly not
392 * the head of the log. By searching for last_half_cycle-1 we
395 start_blk
= log_bbnum
- num_scan_bblks
+ head_blk
;
396 ASSERT(head_blk
<= INT_MAX
&& (xfs_daddr_t
) num_scan_bblks
-head_blk
>= 0);
397 if ((error
= xlog_find_verify_cycle(log
, start_blk
,
398 num_scan_bblks
-(int)head_blk
, (stop_on_cycle
- 1),
407 * Scan beginning of log now. The last part of the physical log
408 * is good. This scan needs to verify that it doesn't find the
412 ASSERT(head_blk
<= INT_MAX
);
413 if ((error
= xlog_find_verify_cycle(log
, start_blk
, (int) head_blk
,
414 stop_on_cycle
, &new_blk
)))
422 * Now we need to make sure head_blk is not pointing to a block in
423 * the middle of a log record.
425 num_scan_bblks
= BTOBB(XLOG_MAX_RECORD_BSIZE
);
426 if (head_blk
>= num_scan_bblks
) {
427 start_blk
= head_blk
- num_scan_bblks
; /* don't read head_blk */
429 /* start ptr at last block ptr before head_blk */
430 if ((error
= xlog_find_verify_log_record(log
,
434 error
= XFS_ERROR(EIO
);
440 ASSERT(head_blk
<= INT_MAX
);
441 if ((error
= xlog_find_verify_log_record(log
,
445 /* We hit the beginning of the log during our search */
446 start_blk
= log_bbnum
- num_scan_bblks
+ head_blk
;
448 ASSERT(start_blk
<= INT_MAX
&& (xfs_daddr_t
) log_bbnum
-start_blk
>= 0);
449 ASSERT(head_blk
<= INT_MAX
);
450 if ((error
= xlog_find_verify_log_record(log
,
453 (int)head_blk
)) == -1) {
454 error
= XFS_ERROR(EIO
);
458 if (new_blk
!= log_bbnum
)
465 if (head_blk
== log_bbnum
)
466 *return_head_blk
= 0;
468 *return_head_blk
= head_blk
;
470 * When returning here, we have a good block number. Bad block
471 * means that during a previous crash, we didn't have a clean break
472 * from cycle number N to cycle number N-1. In this case, we need
473 * to find the first block with cycle number N-1.
481 xlog_warn("XFS: failed to find log head");
484 } /* xlog_find_head */
487 * Find the sync block number or the tail of the log.
489 * This will be the block number of the last record to have its
490 * associated buffers synced to disk. Every log record header has
491 * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
492 * to get a sync block number. The only concern is to figure out which
493 * log record header to believe.
495 * The following algorithm uses the log record header with the largest
496 * lsn. The entire log record does not need to be valid. We only care
497 * that the header is valid.
499 * We could speed up search by using current head_blk buffer, but it is not
503 xlog_find_tail(xlog_t
*log
,
504 xfs_daddr_t
*head_blk
,
505 xfs_daddr_t
*tail_blk
,
508 xlog_rec_header_t
*rhead
;
509 xlog_op_header_t
*op_head
;
512 xfs_daddr_t umount_data_blk
;
513 xfs_daddr_t after_umount_blk
;
519 * Find previous log record
521 if ((error
= xlog_find_head(log
, head_blk
)))
524 bp
= xlog_get_bp(1,log
->l_mp
);
527 if (*head_blk
== 0) { /* special case */
528 if ((error
= xlog_bread(log
, 0, 1, bp
)))
530 if (GET_CYCLE(XFS_BUF_PTR(bp
), ARCH_CONVERT
) == 0) {
532 /* leave all other log inited values alone */
538 * Search backwards looking for log record header block
540 ASSERT(*head_blk
< INT_MAX
);
541 for (i
=(int)(*head_blk
)-1; i
>=0; i
--) {
542 if ((error
= xlog_bread(log
, i
, 1, bp
)))
544 if (INT_GET(*(uint
*)(XFS_BUF_PTR(bp
)), ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
) {
550 * If we haven't found the log record header block, start looking
551 * again from the end of the physical log. XXXmiken: There should be
552 * a check here to make sure we didn't search more than N blocks in
556 for (i
=log
->l_logBBsize
-1; i
>=(int)(*head_blk
); i
--) {
557 if ((error
= xlog_bread(log
, i
, 1, bp
)))
559 if (INT_GET(*(uint
*)(XFS_BUF_PTR(bp
)), ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
) {
566 xlog_warn("XFS: xlog_find_tail: couldn't find sync record");
568 return XFS_ERROR(EIO
);
571 /* find blk_no of tail of log */
572 rhead
= (xlog_rec_header_t
*)XFS_BUF_PTR(bp
);
573 *tail_blk
= BLOCK_LSN(rhead
->h_tail_lsn
, ARCH_CONVERT
);
576 * Reset log values according to the state of the log when we
577 * crashed. In the case where head_blk == 0, we bump curr_cycle
578 * one because the next write starts a new cycle rather than
579 * continuing the cycle of the last good log record. At this
580 * point we have guaranteed that all partial log records have been
581 * accounted for. Therefore, we know that the last good log record
582 * written was complete and ended exactly on the end boundary
583 * of the physical log.
585 log
->l_prev_block
= i
;
586 log
->l_curr_block
= (int)*head_blk
;
587 log
->l_curr_cycle
= INT_GET(rhead
->h_cycle
, ARCH_CONVERT
);
590 log
->l_tail_lsn
= INT_GET(rhead
->h_tail_lsn
, ARCH_CONVERT
);
591 log
->l_last_sync_lsn
= INT_GET(rhead
->h_lsn
, ARCH_CONVERT
);
592 log
->l_grant_reserve_cycle
= log
->l_curr_cycle
;
593 log
->l_grant_reserve_bytes
= BBTOB(log
->l_curr_block
);
594 log
->l_grant_write_cycle
= log
->l_curr_cycle
;
595 log
->l_grant_write_bytes
= BBTOB(log
->l_curr_block
);
598 * Look for unmount record. If we find it, then we know there
599 * was a clean unmount. Since 'i' could be the last block in
600 * the physical log, we convert to a log block before comparing
603 * Save the current tail lsn to use to pass to
604 * xlog_clear_stale_blocks() below. We won't want to clear the
605 * unmount record if there is one, so we pass the lsn of the
606 * unmount record rather than the block after it.
608 after_umount_blk
= (i
+ 2) % log
->l_logBBsize
;
609 tail_lsn
= log
->l_tail_lsn
;
610 if (*head_blk
== after_umount_blk
&& INT_GET(rhead
->h_num_logops
, ARCH_CONVERT
) == 1) {
611 umount_data_blk
= (i
+ 1) % log
->l_logBBsize
;
612 if ((error
= xlog_bread(log
, umount_data_blk
, 1, bp
))) {
615 op_head
= (xlog_op_header_t
*)XFS_BUF_PTR(bp
);
616 if (op_head
->oh_flags
& XLOG_UNMOUNT_TRANS
) {
618 * Set tail and last sync so that newly written
619 * log records will point recovery to after the
620 * current unmount record.
622 ASSIGN_ANY_LSN(log
->l_tail_lsn
, log
->l_curr_cycle
,
623 after_umount_blk
, ARCH_NOCONVERT
);
624 ASSIGN_ANY_LSN(log
->l_last_sync_lsn
, log
->l_curr_cycle
,
625 after_umount_blk
, ARCH_NOCONVERT
);
626 *tail_blk
= after_umount_blk
;
632 * Make sure that there are no blocks in front of the head
633 * with the same cycle number as the head. This can happen
634 * because we allow multiple outstanding log writes concurrently,
635 * and the later writes might make it out before earlier ones.
637 * We use the lsn from before modifying it so that we'll never
638 * overwrite the unmount record after a clean unmount.
640 * Do this only if we are going to recover the filesystem
643 error
= xlog_clear_stale_blocks(log
, tail_lsn
);
651 xlog_warn("XFS: failed to locate log tail");
654 } /* xlog_find_tail */
658 * Is the log zeroed at all?
660 * The last binary search should be changed to perform an X block read
661 * once X becomes small enough. You can then search linearly through
662 * the X blocks. This will cut down on the number of reads we need to do.
664 * If the log is partially zeroed, this routine will pass back the blkno
665 * of the first block with cycle number 0. It won't have a complete LR
669 * 0 => the log is completely written to
670 * -1 => use *blk_no as the first block of the log
671 * >0 => error has occurred
674 xlog_find_zeroed(struct log
*log
,
678 uint first_cycle
, last_cycle
;
679 xfs_daddr_t new_blk
, last_blk
, start_blk
;
680 xfs_daddr_t num_scan_bblks
;
681 int error
, log_bbnum
= log
->l_logBBsize
;
684 /* check totally zeroed log */
685 bp
= xlog_get_bp(1,log
->l_mp
);
688 if ((error
= xlog_bread(log
, 0, 1, bp
)))
690 first_cycle
= GET_CYCLE(XFS_BUF_PTR(bp
), ARCH_CONVERT
);
691 if (first_cycle
== 0) { /* completely zeroed log */
697 /* check partially zeroed log */
698 if ((error
= xlog_bread(log
, log_bbnum
-1, 1, bp
)))
700 last_cycle
= GET_CYCLE(XFS_BUF_PTR(bp
), ARCH_CONVERT
);
701 if (last_cycle
!= 0) { /* log completely written to */
704 } else if (first_cycle
!= 1) {
706 * If the cycle of the last block is zero, the cycle of
707 * the first block must be 1. If it's not, maybe we're
708 * not looking at a log... Bail out.
710 xlog_warn("XFS: Log inconsistent or not a log (last==0, first!=1)");
711 return XFS_ERROR(EINVAL
);
714 /* we have a partially zeroed log */
715 last_blk
= log_bbnum
-1;
716 if ((error
= xlog_find_cycle_start(log
, bp
, 0, &last_blk
, 0)))
720 * Validate the answer. Because there is no way to guarantee that
721 * the entire log is made up of log records which are the same size,
722 * we scan over the defined maximum blocks. At this point, the maximum
723 * is not chosen to mean anything special. XXXmiken
725 num_scan_bblks
= BTOBB(XLOG_MAX_ICLOGS
<<XLOG_MAX_RECORD_BSHIFT
);
726 ASSERT(num_scan_bblks
<= INT_MAX
);
728 if (last_blk
< num_scan_bblks
)
729 num_scan_bblks
= last_blk
;
730 start_blk
= last_blk
- num_scan_bblks
;
733 * We search for any instances of cycle number 0 that occur before
734 * our current estimate of the head. What we're trying to detect is
735 * 1 ... | 0 | 1 | 0...
736 * ^ binary search ends here
738 if ((error
= xlog_find_verify_cycle(log
, start_blk
,
739 (int)num_scan_bblks
, 0, &new_blk
)))
745 * Potentially backup over partial log record write. We don't need
746 * to search the end of the log because we know it is zero.
748 if ((error
= xlog_find_verify_log_record(log
, start_blk
,
749 &last_blk
, 0)) == -1) {
750 error
= XFS_ERROR(EIO
);
761 } /* xlog_find_zeroed */
763 /* stuff for transactional view */
765 xlog_unpack_data(xlog_rec_header_t
*rhead
,
770 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
771 uint
*up
= (uint
*)dp
;
775 for (i
=0; i
<BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
)); i
++) {
776 /* these are both on-disk, so don't endian flip twice */
777 *(uint
*)dp
= *(uint
*)&rhead
->h_cycle_data
[i
];
780 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
781 /* divide length by 4 to get # words */
782 for (i
=0; i
< INT_GET(rhead
->h_len
, ARCH_CONVERT
) >> 2; i
++) {
783 chksum
^= INT_GET(*up
, ARCH_CONVERT
);
786 if (chksum
!= INT_GET(rhead
->h_chksum
, ARCH_CONVERT
)) {
787 if (!INT_ISZERO(rhead
->h_chksum
, ARCH_CONVERT
) ||
788 ((log
->l_flags
& XLOG_CHKSUM_MISMATCH
) == 0)) {
790 "XFS: LogR chksum mismatch: was (0x%x) is (0x%x)",
791 INT_GET(rhead
->h_chksum
, ARCH_CONVERT
), chksum
);
793 "XFS: Disregard message if filesystem was created with non-DEBUG kernel");
794 log
->l_flags
|= XLOG_CHKSUM_MISMATCH
;
797 #endif /* DEBUG && XFS_LOUD_RECOVERY */
798 } /* xlog_unpack_data */
801 STATIC xlog_recover_t
*
802 xlog_recover_find_tid(xlog_recover_t
*q
,
805 xlog_recover_t
*p
= q
;
808 if (p
->r_log_tid
== tid
)
813 } /* xlog_recover_find_tid */
816 xlog_recover_put_hashq(xlog_recover_t
**q
,
817 xlog_recover_t
*trans
)
821 } /* xlog_recover_put_hashq */
824 xlog_recover_new_tid(xlog_recover_t
**q
,
828 xlog_recover_t
*trans
;
830 trans
= kmem_zalloc(sizeof(xlog_recover_t
), 0);
831 trans
->r_log_tid
= tid
;
833 xlog_recover_put_hashq(q
, trans
);
834 } /* xlog_recover_new_tid */
838 xlog_recover_unlink_tid(xlog_recover_t
**q
,
839 xlog_recover_t
*trans
)
850 if (tp
->r_next
== trans
) {
858 "XFS: xlog_recover_unlink_tid: trans not found");
860 return XFS_ERROR(EIO
);
862 tp
->r_next
= tp
->r_next
->r_next
;
865 } /* xlog_recover_unlink_tid */
868 * Free up any resources allocated by the transaction
870 * Remember that EFIs, EFDs, and IUNLINKs are handled later.
873 xlog_recover_free_trans(xlog_recover_t
*trans
)
875 xlog_recover_item_t
*first_item
, *item
, *free_item
;
878 item
= first_item
= trans
->r_itemq
;
881 item
= item
->ri_next
;
882 /* Free the regions in the item. */
883 for (i
= 0; i
< free_item
->ri_cnt
; i
++) {
884 kmem_free(free_item
->ri_buf
[i
].i_addr
,
885 free_item
->ri_buf
[i
].i_len
);
887 /* Free the item itself */
888 kmem_free(free_item
->ri_buf
,
889 (free_item
->ri_total
* sizeof(xfs_log_iovec_t
)));
890 kmem_free(free_item
, sizeof(xlog_recover_item_t
));
891 } while (first_item
!= item
);
892 /* Free the transaction recover structure */
893 kmem_free(trans
, sizeof(xlog_recover_t
));
894 } /* xlog_recover_free_trans */
898 xlog_recover_commit_trans(xlog_t
*log
,
900 xlog_recover_t
*trans
,
905 if ((error
= xlog_recover_unlink_tid(q
, trans
)))
907 if ((error
= xlog_recover_do_trans(log
, trans
, pass
)))
909 xlog_recover_free_trans(trans
); /* no error */
911 } /* xlog_recover_commit_trans */
914 xlog_recover_insert_item_backq(xlog_recover_item_t
**q
,
915 xlog_recover_item_t
*item
)
918 item
->ri_prev
= item
->ri_next
= item
;
922 item
->ri_prev
= (*q
)->ri_prev
;
923 (*q
)->ri_prev
= item
;
924 item
->ri_prev
->ri_next
= item
;
926 } /* xlog_recover_insert_item_backq */
929 xlog_recover_add_item(xlog_recover_item_t
**itemq
)
931 xlog_recover_item_t
*item
;
933 item
= kmem_zalloc(sizeof(xlog_recover_item_t
), 0);
934 xlog_recover_insert_item_backq(itemq
, item
);
935 } /* xlog_recover_add_item */
937 /* The next region to add is the start of a new region. It could be
938 * a whole region or it could be the first part of a new region. Because
939 * of this, the assumption here is that the type and size fields of all
940 * format structures fit into the first 32 bits of the structure.
942 * This works because all regions must be 32 bit aligned. Therefore, we
943 * either have both fields or we have neither field. In the case we have
944 * neither field, the data part of the region is zero length. We only have
945 * a log_op_header and can throw away the header since a new one will appear
946 * later. If we have at least 4 bytes, then we can determine how many regions
947 * will appear in the current log item.
950 xlog_recover_add_to_trans(xlog_recover_t
*trans
,
954 xfs_inode_log_format_t
*in_f
; /* any will do */
955 xlog_recover_item_t
*item
;
960 ptr
= kmem_zalloc(len
, 0);
963 in_f
= (xfs_inode_log_format_t
*)ptr
;
964 item
= trans
->r_itemq
;
966 ASSERT(*(uint
*)dp
== XFS_TRANS_HEADER_MAGIC
);
967 if (len
== sizeof(xfs_trans_header_t
))
968 xlog_recover_add_item(&trans
->r_itemq
);
969 bcopy(dp
, &trans
->r_theader
, len
); /* s, d, l */
972 if (item
->ri_prev
->ri_total
!= 0 &&
973 item
->ri_prev
->ri_total
== item
->ri_prev
->ri_cnt
) {
974 xlog_recover_add_item(&trans
->r_itemq
);
976 item
= trans
->r_itemq
;
977 item
= item
->ri_prev
;
979 if (item
->ri_total
== 0) { /* first region to be added */
980 item
->ri_total
= in_f
->ilf_size
;
981 ASSERT(item
->ri_total
<= XLOG_MAX_REGIONS_IN_ITEM
);
982 item
->ri_buf
= kmem_zalloc((item
->ri_total
*
983 sizeof(xfs_log_iovec_t
)), 0);
985 ASSERT(item
->ri_total
> item
->ri_cnt
);
986 /* Description region is ri_buf[0] */
987 item
->ri_buf
[item
->ri_cnt
].i_addr
= ptr
;
988 item
->ri_buf
[item
->ri_cnt
].i_len
= len
;
991 } /* xlog_recover_add_to_trans */
994 xlog_recover_add_to_cont_trans(xlog_recover_t
*trans
,
998 xlog_recover_item_t
*item
;
999 xfs_caddr_t ptr
, old_ptr
;
1002 item
= trans
->r_itemq
;
1004 /* finish copying rest of trans header */
1005 xlog_recover_add_item(&trans
->r_itemq
);
1006 ptr
= (xfs_caddr_t
)&trans
->r_theader
+sizeof(xfs_trans_header_t
)-len
;
1007 bcopy(dp
, ptr
, len
); /* s, d, l */
1010 item
= item
->ri_prev
;
1012 old_ptr
= item
->ri_buf
[item
->ri_cnt
-1].i_addr
;
1013 old_len
= item
->ri_buf
[item
->ri_cnt
-1].i_len
;
1015 ptr
= kmem_realloc(old_ptr
, len
+old_len
, old_len
, 0);
1016 bcopy(dp
, &ptr
[old_len
], len
); /* s, d, l */
1017 item
->ri_buf
[item
->ri_cnt
-1].i_len
+= len
;
1018 item
->ri_buf
[item
->ri_cnt
-1].i_addr
= ptr
;
1020 } /* xlog_recover_add_to_cont_trans */
1023 xlog_recover_unmount_trans(xlog_recover_t
*trans
)
1025 /* Do nothing now */
1026 xlog_warn("XFS: xlog_recover_unmount_trans: Unmount LR");
1028 } /* xlog_recover_unmount_trans */
1032 xlog_recover_process_data(xlog_t
*log
,
1033 xlog_recover_t
*rhash
[],
1034 xlog_rec_header_t
*rhead
,
1038 xfs_caddr_t lp
= dp
+INT_GET(rhead
->h_len
, ARCH_CONVERT
);
1039 int num_logops
= INT_GET(rhead
->h_num_logops
, ARCH_CONVERT
);
1040 xlog_op_header_t
*ohead
;
1041 xlog_recover_t
*trans
;
1047 /* check the log format matches our own - else we can't recover */
1048 if (xlog_header_check_recover(log
->l_mp
, rhead
))
1049 return (XFS_ERROR(EIO
));
1052 ASSERT(dp
+ sizeof(xlog_op_header_t
) <= lp
);
1053 ohead
= (xlog_op_header_t
*)dp
;
1054 dp
+= sizeof(xlog_op_header_t
);
1055 if (ohead
->oh_clientid
!= XFS_TRANSACTION
&&
1056 ohead
->oh_clientid
!= XFS_LOG
) {
1057 xlog_warn("XFS: xlog_recover_process_data: bad clientid");
1059 return (XFS_ERROR(EIO
));
1061 tid
= INT_GET(ohead
->oh_tid
, ARCH_CONVERT
);
1062 hash
= XLOG_RHASH(tid
);
1063 trans
= xlog_recover_find_tid(rhash
[hash
], tid
);
1064 if (trans
== NULL
) { /* not found; add new tid */
1065 if (ohead
->oh_flags
& XLOG_START_TRANS
)
1066 xlog_recover_new_tid(&rhash
[hash
], tid
, INT_GET(rhead
->h_lsn
, ARCH_CONVERT
));
1068 ASSERT(dp
+INT_GET(ohead
->oh_len
, ARCH_CONVERT
) <= lp
);
1069 flags
= ohead
->oh_flags
& ~XLOG_END_TRANS
;
1070 if (flags
& XLOG_WAS_CONT_TRANS
)
1071 flags
&= ~XLOG_CONTINUE_TRANS
;
1073 case XLOG_COMMIT_TRANS
: {
1074 error
= xlog_recover_commit_trans(log
, &rhash
[hash
],
1078 case XLOG_UNMOUNT_TRANS
: {
1079 error
= xlog_recover_unmount_trans(trans
);
1082 case XLOG_WAS_CONT_TRANS
: {
1083 error
= xlog_recover_add_to_cont_trans(trans
, dp
,
1084 INT_GET(ohead
->oh_len
, ARCH_CONVERT
));
1087 case XLOG_START_TRANS
: {
1088 xlog_warn("XFS: xlog_recover_process_data: bad transaction");
1090 error
= XFS_ERROR(EIO
);
1094 case XLOG_CONTINUE_TRANS
: {
1095 error
= xlog_recover_add_to_trans(trans
, dp
,
1096 INT_GET(ohead
->oh_len
, ARCH_CONVERT
));
1100 xlog_warn("XFS: xlog_recover_process_data: bad flag");
1102 error
= XFS_ERROR(EIO
);
1109 dp
+= INT_GET(ohead
->oh_len
, ARCH_CONVERT
);
1113 } /* xlog_recover_process_data */
1116 * Read the log from tail to head and process the log records found.
1117 * Handle the two cases where the tail and head are in the same cycle
1118 * and where the active portion of the log wraps around the end of
1119 * the physical log separately. The pass parameter is passed through
1120 * to the routines called to process the data and is not looked at
1124 xlog_do_recovery_pass(xlog_t
*log
,
1125 xfs_daddr_t head_blk
,
1126 xfs_daddr_t tail_blk
,
1129 xlog_rec_header_t
*rhead
;
1131 xfs_caddr_t bufaddr
;
1132 xfs_buf_t
*hbp
, *dbp
;
1134 int bblks
, split_bblks
;
1135 xlog_recover_t
*rhash
[XLOG_RHASH_SIZE
];
1138 hbp
= xlog_get_bp(1,log
->l_mp
);
1141 dbp
= xlog_get_bp(BTOBB(XLOG_MAX_RECORD_BSIZE
),log
->l_mp
);
1146 bzero(rhash
, sizeof(rhash
));
1147 if (tail_blk
<= head_blk
) {
1148 for (blk_no
= tail_blk
; blk_no
< head_blk
; ) {
1149 if ((error
= xlog_bread(log
, blk_no
, 1, hbp
)))
1151 rhead
= (xlog_rec_header_t
*)XFS_BUF_PTR(hbp
);
1152 ASSERT(INT_GET(rhead
->h_magicno
, ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
);
1153 ASSERT(BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
) <= INT_MAX
));
1154 bblks
= (int) BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
)); /* blocks in data section */
1156 if ((error
= xlog_bread(log
, blk_no
+1, bblks
, dbp
)))
1158 xlog_unpack_data(rhead
, XFS_BUF_PTR(dbp
), log
);
1159 if ((error
= xlog_recover_process_data(log
, rhash
,
1160 rhead
, XFS_BUF_PTR(dbp
),
1164 blk_no
+= (bblks
+1);
1168 * Perform recovery around the end of the physical log. When the head
1169 * is not on the same cycle number as the tail, we can't do a sequential
1170 * recovery as above.
1173 while (blk_no
< log
->l_logBBsize
) {
1175 /* Read header of one block */
1176 if ((error
= xlog_bread(log
, blk_no
, 1, hbp
)))
1178 rhead
= (xlog_rec_header_t
*)XFS_BUF_PTR(hbp
);
1179 ASSERT(INT_GET(rhead
->h_magicno
, ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
);
1180 ASSERT(BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
) <= INT_MAX
));
1181 bblks
= (int) BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
));
1183 /* LR body must have data or it wouldn't have been written */
1185 blk_no
++; /* successfully read header */
1186 ASSERT(blk_no
<= log
->l_logBBsize
);
1188 if ((INT_GET(rhead
->h_magicno
, ARCH_CONVERT
) != XLOG_HEADER_MAGIC_NUM
) ||
1189 (BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
) > INT_MAX
)) ||
1191 (blk_no
> log
->l_logBBsize
)) {
1192 error
= EFSCORRUPTED
;
1196 /* Read in data for log record */
1197 if (blk_no
+bblks
<= log
->l_logBBsize
) {
1198 if ((error
= xlog_bread(log
, blk_no
, bblks
, dbp
)))
1201 /* This log record is split across physical end of log */
1203 if (blk_no
!= log
->l_logBBsize
) {
1205 /* some data is before physical end of log */
1206 ASSERT(blk_no
<= INT_MAX
);
1207 split_bblks
= log
->l_logBBsize
- (int)blk_no
;
1208 ASSERT(split_bblks
> 0);
1209 if ((error
= xlog_bread(log
, blk_no
, split_bblks
, dbp
)))
1212 bufaddr
= XFS_BUF_PTR(dbp
);
1213 XFS_BUF_SET_PTR(dbp
, bufaddr
+ BBTOB(split_bblks
),
1214 BBTOB(bblks
- split_bblks
));
1215 if ((error
= xlog_bread(log
, 0, bblks
- split_bblks
, dbp
)))
1217 XFS_BUF_SET_PTR(dbp
, bufaddr
, XLOG_MAX_RECORD_BSIZE
);
1219 xlog_unpack_data(rhead
, XFS_BUF_PTR(dbp
), log
);
1220 if ((error
= xlog_recover_process_data(log
, rhash
,
1221 rhead
, XFS_BUF_PTR(dbp
),
1227 ASSERT(blk_no
>= log
->l_logBBsize
);
1228 blk_no
-= log
->l_logBBsize
;
1230 /* read first part of physical log */
1231 while (blk_no
< head_blk
) {
1232 if ((error
= xlog_bread(log
, blk_no
, 1, hbp
)))
1234 rhead
= (xlog_rec_header_t
*)XFS_BUF_PTR(hbp
);
1235 ASSERT(INT_GET(rhead
->h_magicno
, ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
);
1236 ASSERT(BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
) <= INT_MAX
));
1237 bblks
= (int) BTOBB(INT_GET(rhead
->h_len
, ARCH_CONVERT
));
1239 if ((error
= xlog_bread(log
, blk_no
+1, bblks
, dbp
)))
1241 xlog_unpack_data(rhead
, XFS_BUF_PTR(dbp
), log
);
1242 if ((error
= xlog_recover_process_data(log
, rhash
,
1243 rhead
, XFS_BUF_PTR(dbp
),
1246 blk_no
+= (bblks
+1);