xfs: kill XLOG_SECTOR_ROUND*()
[deliverable/linux.git] / fs / xfs / xfs_log_recover.c
CommitLineData
1da177e4 1/*
87c199c2 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4 24#include "xfs_trans.h"
a844f451
NS
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_bmap_btree.h"
a844f451
NS
32#include "xfs_alloc_btree.h"
33#include "xfs_ialloc_btree.h"
1da177e4 34#include "xfs_dir2_sf.h"
a844f451 35#include "xfs_attr_sf.h"
1da177e4 36#include "xfs_dinode.h"
1da177e4 37#include "xfs_inode.h"
a844f451 38#include "xfs_inode_item.h"
a844f451 39#include "xfs_alloc.h"
1da177e4
LT
40#include "xfs_ialloc.h"
41#include "xfs_log_priv.h"
42#include "xfs_buf_item.h"
1da177e4
LT
43#include "xfs_log_recover.h"
44#include "xfs_extfree_item.h"
45#include "xfs_trans_priv.h"
1da177e4
LT
46#include "xfs_quota.h"
47#include "xfs_rw.h"
43355099 48#include "xfs_utils.h"
0b1b213f 49#include "xfs_trace.h"
1da177e4
LT
50
51STATIC int xlog_find_zeroed(xlog_t *, xfs_daddr_t *);
52STATIC int xlog_clear_stale_blocks(xlog_t *, xfs_lsn_t);
1da177e4
LT
53#if defined(DEBUG)
54STATIC void xlog_recover_check_summary(xlog_t *);
1da177e4
LT
55#else
56#define xlog_recover_check_summary(log)
1da177e4
LT
57#endif
58
1da177e4
LT
59/*
60 * Sector aligned buffer routines for buffer create/read/write/access
61 */
62
6881a229
AE
63/* Number of basic blocks in a log sector */
64#define xlog_sectbb(log) (1 << (log)->l_sectbb_log)
65
5d77c0dc 66STATIC xfs_buf_t *
1da177e4
LT
67xlog_get_bp(
68 xlog_t *log,
3228149c 69 int nbblks)
1da177e4 70{
3228149c
DC
71 if (nbblks <= 0 || nbblks > log->l_logBBsize) {
72 xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
73 XFS_ERROR_REPORT("xlog_get_bp(1)",
74 XFS_ERRLEVEL_HIGH, log->l_mp);
75 return NULL;
76 }
1da177e4
LT
77
78 if (log->l_sectbb_log) {
3228149c 79 if (nbblks > 1)
5c17f533
AE
80 nbblks += xlog_sectbb(log);
81 nbblks = round_up(nbblks, xlog_sectbb(log));
1da177e4 82 }
3228149c 83 return xfs_buf_get_noaddr(BBTOB(nbblks), log->l_mp->m_logdev_targp);
1da177e4
LT
84}
85
5d77c0dc 86STATIC void
1da177e4
LT
87xlog_put_bp(
88 xfs_buf_t *bp)
89{
90 xfs_buf_free(bp);
91}
92
076e6acb
CH
93STATIC xfs_caddr_t
94xlog_align(
95 xlog_t *log,
96 xfs_daddr_t blk_no,
97 int nbblks,
98 xfs_buf_t *bp)
99{
100 xfs_caddr_t ptr;
101
102 if (!log->l_sectbb_log)
103 return XFS_BUF_PTR(bp);
104
105 ptr = XFS_BUF_PTR(bp) + BBTOB((int)blk_no & log->l_sectbb_mask);
106 ASSERT(XFS_BUF_SIZE(bp) >=
107 BBTOB(nbblks + (blk_no & log->l_sectbb_mask)));
108 return ptr;
109}
110
1da177e4
LT
111
112/*
113 * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
114 */
076e6acb
CH
115STATIC int
116xlog_bread_noalign(
1da177e4
LT
117 xlog_t *log,
118 xfs_daddr_t blk_no,
119 int nbblks,
120 xfs_buf_t *bp)
121{
122 int error;
123
3228149c
DC
124 if (nbblks <= 0 || nbblks > log->l_logBBsize) {
125 xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
126 XFS_ERROR_REPORT("xlog_bread(1)",
127 XFS_ERRLEVEL_HIGH, log->l_mp);
128 return EFSCORRUPTED;
129 }
130
1da177e4 131 if (log->l_sectbb_log) {
5c17f533
AE
132 blk_no = round_down(blk_no, xlog_sectbb(log));
133 nbblks = round_up(nbblks, xlog_sectbb(log));
1da177e4
LT
134 }
135
136 ASSERT(nbblks > 0);
137 ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
138 ASSERT(bp);
139
140 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
141 XFS_BUF_READ(bp);
142 XFS_BUF_BUSY(bp);
143 XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
144 XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
145
146 xfsbdstrat(log->l_mp, bp);
d64e31a2
DC
147 error = xfs_iowait(bp);
148 if (error)
1da177e4
LT
149 xfs_ioerror_alert("xlog_bread", log->l_mp,
150 bp, XFS_BUF_ADDR(bp));
151 return error;
152}
153
076e6acb
CH
154STATIC int
155xlog_bread(
156 xlog_t *log,
157 xfs_daddr_t blk_no,
158 int nbblks,
159 xfs_buf_t *bp,
160 xfs_caddr_t *offset)
161{
162 int error;
163
164 error = xlog_bread_noalign(log, blk_no, nbblks, bp);
165 if (error)
166 return error;
167
168 *offset = xlog_align(log, blk_no, nbblks, bp);
169 return 0;
170}
171
1da177e4
LT
172/*
173 * Write out the buffer at the given block for the given number of blocks.
174 * The buffer is kept locked across the write and is returned locked.
175 * This can only be used for synchronous log writes.
176 */
ba0f32d4 177STATIC int
1da177e4
LT
178xlog_bwrite(
179 xlog_t *log,
180 xfs_daddr_t blk_no,
181 int nbblks,
182 xfs_buf_t *bp)
183{
184 int error;
185
3228149c
DC
186 if (nbblks <= 0 || nbblks > log->l_logBBsize) {
187 xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
188 XFS_ERROR_REPORT("xlog_bwrite(1)",
189 XFS_ERRLEVEL_HIGH, log->l_mp);
190 return EFSCORRUPTED;
191 }
192
1da177e4 193 if (log->l_sectbb_log) {
5c17f533
AE
194 blk_no = round_down(blk_no, xlog_sectbb(log));
195 nbblks = round_up(nbblks, xlog_sectbb(log));
1da177e4
LT
196 }
197
198 ASSERT(nbblks > 0);
199 ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
200
201 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
202 XFS_BUF_ZEROFLAGS(bp);
203 XFS_BUF_BUSY(bp);
204 XFS_BUF_HOLD(bp);
205 XFS_BUF_PSEMA(bp, PRIBIO);
206 XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
207 XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
208
209 if ((error = xfs_bwrite(log->l_mp, bp)))
210 xfs_ioerror_alert("xlog_bwrite", log->l_mp,
211 bp, XFS_BUF_ADDR(bp));
212 return error;
213}
214
1da177e4
LT
215#ifdef DEBUG
216/*
217 * dump debug superblock and log record information
218 */
219STATIC void
220xlog_header_check_dump(
221 xfs_mount_t *mp,
222 xlog_rec_header_t *head)
223{
03daa57c
JP
224 cmn_err(CE_DEBUG, "%s: SB : uuid = %pU, fmt = %d\n",
225 __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
226 cmn_err(CE_DEBUG, " log : uuid = %pU, fmt = %d\n",
227 &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
1da177e4
LT
228}
229#else
230#define xlog_header_check_dump(mp, head)
231#endif
232
233/*
234 * check log record header for recovery
235 */
236STATIC int
237xlog_header_check_recover(
238 xfs_mount_t *mp,
239 xlog_rec_header_t *head)
240{
b53e675d 241 ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
1da177e4
LT
242
243 /*
244 * IRIX doesn't write the h_fmt field and leaves it zeroed
245 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
246 * a dirty log created in IRIX.
247 */
b53e675d 248 if (unlikely(be32_to_cpu(head->h_fmt) != XLOG_FMT)) {
1da177e4
LT
249 xlog_warn(
250 "XFS: dirty log written in incompatible format - can't recover");
251 xlog_header_check_dump(mp, head);
252 XFS_ERROR_REPORT("xlog_header_check_recover(1)",
253 XFS_ERRLEVEL_HIGH, mp);
254 return XFS_ERROR(EFSCORRUPTED);
255 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
256 xlog_warn(
257 "XFS: dirty log entry has mismatched uuid - can't recover");
258 xlog_header_check_dump(mp, head);
259 XFS_ERROR_REPORT("xlog_header_check_recover(2)",
260 XFS_ERRLEVEL_HIGH, mp);
261 return XFS_ERROR(EFSCORRUPTED);
262 }
263 return 0;
264}
265
266/*
267 * read the head block of the log and check the header
268 */
269STATIC int
270xlog_header_check_mount(
271 xfs_mount_t *mp,
272 xlog_rec_header_t *head)
273{
b53e675d 274 ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
1da177e4
LT
275
276 if (uuid_is_nil(&head->h_fs_uuid)) {
277 /*
278 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
279 * h_fs_uuid is nil, we assume this log was last mounted
280 * by IRIX and continue.
281 */
282 xlog_warn("XFS: nil uuid in log - IRIX style log");
283 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
284 xlog_warn("XFS: log has mismatched uuid - can't recover");
285 xlog_header_check_dump(mp, head);
286 XFS_ERROR_REPORT("xlog_header_check_mount",
287 XFS_ERRLEVEL_HIGH, mp);
288 return XFS_ERROR(EFSCORRUPTED);
289 }
290 return 0;
291}
292
293STATIC void
294xlog_recover_iodone(
295 struct xfs_buf *bp)
296{
1da177e4
LT
297 if (XFS_BUF_GETERROR(bp)) {
298 /*
299 * We're not going to bother about retrying
300 * this during recovery. One strike!
301 */
1da177e4 302 xfs_ioerror_alert("xlog_recover_iodone",
15ac08a8
CH
303 bp->b_mount, bp, XFS_BUF_ADDR(bp));
304 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
1da177e4 305 }
15ac08a8 306 bp->b_mount = NULL;
1da177e4
LT
307 XFS_BUF_CLR_IODONE_FUNC(bp);
308 xfs_biodone(bp);
309}
310
311/*
312 * This routine finds (to an approximation) the first block in the physical
313 * log which contains the given cycle. It uses a binary search algorithm.
314 * Note that the algorithm can not be perfect because the disk will not
315 * necessarily be perfect.
316 */
a8272ce0 317STATIC int
1da177e4
LT
318xlog_find_cycle_start(
319 xlog_t *log,
320 xfs_buf_t *bp,
321 xfs_daddr_t first_blk,
322 xfs_daddr_t *last_blk,
323 uint cycle)
324{
325 xfs_caddr_t offset;
326 xfs_daddr_t mid_blk;
327 uint mid_cycle;
328 int error;
329
330 mid_blk = BLK_AVG(first_blk, *last_blk);
331 while (mid_blk != first_blk && mid_blk != *last_blk) {
076e6acb
CH
332 error = xlog_bread(log, mid_blk, 1, bp, &offset);
333 if (error)
1da177e4 334 return error;
03bea6fe 335 mid_cycle = xlog_get_cycle(offset);
1da177e4
LT
336 if (mid_cycle == cycle) {
337 *last_blk = mid_blk;
338 /* last_half_cycle == mid_cycle */
339 } else {
340 first_blk = mid_blk;
341 /* first_half_cycle == mid_cycle */
342 }
343 mid_blk = BLK_AVG(first_blk, *last_blk);
344 }
345 ASSERT((mid_blk == first_blk && mid_blk+1 == *last_blk) ||
346 (mid_blk == *last_blk && mid_blk-1 == first_blk));
347
348 return 0;
349}
350
351/*
352 * Check that the range of blocks does not contain the cycle number
353 * given. The scan needs to occur from front to back and the ptr into the
354 * region must be updated since a later routine will need to perform another
355 * test. If the region is completely good, we end up returning the same
356 * last block number.
357 *
358 * Set blkno to -1 if we encounter no errors. This is an invalid block number
359 * since we don't ever expect logs to get this large.
360 */
361STATIC int
362xlog_find_verify_cycle(
363 xlog_t *log,
364 xfs_daddr_t start_blk,
365 int nbblks,
366 uint stop_on_cycle_no,
367 xfs_daddr_t *new_blk)
368{
369 xfs_daddr_t i, j;
370 uint cycle;
371 xfs_buf_t *bp;
372 xfs_daddr_t bufblks;
373 xfs_caddr_t buf = NULL;
374 int error = 0;
375
6881a229
AE
376 /*
377 * Greedily allocate a buffer big enough to handle the full
378 * range of basic blocks we'll be examining. If that fails,
379 * try a smaller size. We need to be able to read at least
380 * a log sector, or we're out of luck.
381 */
1da177e4 382 bufblks = 1 << ffs(nbblks);
1da177e4 383 while (!(bp = xlog_get_bp(log, bufblks))) {
1da177e4 384 bufblks >>= 1;
6881a229 385 if (bufblks < xlog_sectbb(log))
1da177e4
LT
386 return ENOMEM;
387 }
388
389 for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
390 int bcount;
391
392 bcount = min(bufblks, (start_blk + nbblks - i));
393
076e6acb
CH
394 error = xlog_bread(log, i, bcount, bp, &buf);
395 if (error)
1da177e4
LT
396 goto out;
397
1da177e4 398 for (j = 0; j < bcount; j++) {
03bea6fe 399 cycle = xlog_get_cycle(buf);
1da177e4
LT
400 if (cycle == stop_on_cycle_no) {
401 *new_blk = i+j;
402 goto out;
403 }
404
405 buf += BBSIZE;
406 }
407 }
408
409 *new_blk = -1;
410
411out:
412 xlog_put_bp(bp);
413 return error;
414}
415
416/*
417 * Potentially backup over partial log record write.
418 *
419 * In the typical case, last_blk is the number of the block directly after
420 * a good log record. Therefore, we subtract one to get the block number
421 * of the last block in the given buffer. extra_bblks contains the number
422 * of blocks we would have read on a previous read. This happens when the
423 * last log record is split over the end of the physical log.
424 *
425 * extra_bblks is the number of blocks potentially verified on a previous
426 * call to this routine.
427 */
428STATIC int
429xlog_find_verify_log_record(
430 xlog_t *log,
431 xfs_daddr_t start_blk,
432 xfs_daddr_t *last_blk,
433 int extra_bblks)
434{
435 xfs_daddr_t i;
436 xfs_buf_t *bp;
437 xfs_caddr_t offset = NULL;
438 xlog_rec_header_t *head = NULL;
439 int error = 0;
440 int smallmem = 0;
441 int num_blks = *last_blk - start_blk;
442 int xhdrs;
443
444 ASSERT(start_blk != 0 || *last_blk != start_blk);
445
446 if (!(bp = xlog_get_bp(log, num_blks))) {
447 if (!(bp = xlog_get_bp(log, 1)))
448 return ENOMEM;
449 smallmem = 1;
450 } else {
076e6acb
CH
451 error = xlog_bread(log, start_blk, num_blks, bp, &offset);
452 if (error)
1da177e4 453 goto out;
1da177e4
LT
454 offset += ((num_blks - 1) << BBSHIFT);
455 }
456
457 for (i = (*last_blk) - 1; i >= 0; i--) {
458 if (i < start_blk) {
459 /* valid log record not found */
460 xlog_warn(
461 "XFS: Log inconsistent (didn't find previous header)");
462 ASSERT(0);
463 error = XFS_ERROR(EIO);
464 goto out;
465 }
466
467 if (smallmem) {
076e6acb
CH
468 error = xlog_bread(log, i, 1, bp, &offset);
469 if (error)
1da177e4 470 goto out;
1da177e4
LT
471 }
472
473 head = (xlog_rec_header_t *)offset;
474
b53e675d 475 if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(head->h_magicno))
1da177e4
LT
476 break;
477
478 if (!smallmem)
479 offset -= BBSIZE;
480 }
481
482 /*
483 * We hit the beginning of the physical log & still no header. Return
484 * to caller. If caller can handle a return of -1, then this routine
485 * will be called again for the end of the physical log.
486 */
487 if (i == -1) {
488 error = -1;
489 goto out;
490 }
491
492 /*
493 * We have the final block of the good log (the first block
494 * of the log record _before_ the head. So we check the uuid.
495 */
496 if ((error = xlog_header_check_mount(log->l_mp, head)))
497 goto out;
498
499 /*
500 * We may have found a log record header before we expected one.
501 * last_blk will be the 1st block # with a given cycle #. We may end
502 * up reading an entire log record. In this case, we don't want to
503 * reset last_blk. Only when last_blk points in the middle of a log
504 * record do we update last_blk.
505 */
62118709 506 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
b53e675d 507 uint h_size = be32_to_cpu(head->h_size);
1da177e4
LT
508
509 xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
510 if (h_size % XLOG_HEADER_CYCLE_SIZE)
511 xhdrs++;
512 } else {
513 xhdrs = 1;
514 }
515
b53e675d
CH
516 if (*last_blk - i + extra_bblks !=
517 BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
1da177e4
LT
518 *last_blk = i;
519
520out:
521 xlog_put_bp(bp);
522 return error;
523}
524
525/*
526 * Head is defined to be the point of the log where the next log write
527 * write could go. This means that incomplete LR writes at the end are
528 * eliminated when calculating the head. We aren't guaranteed that previous
529 * LR have complete transactions. We only know that a cycle number of
530 * current cycle number -1 won't be present in the log if we start writing
531 * from our current block number.
532 *
533 * last_blk contains the block number of the first block with a given
534 * cycle number.
535 *
536 * Return: zero if normal, non-zero if error.
537 */
ba0f32d4 538STATIC int
1da177e4
LT
539xlog_find_head(
540 xlog_t *log,
541 xfs_daddr_t *return_head_blk)
542{
543 xfs_buf_t *bp;
544 xfs_caddr_t offset;
545 xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
546 int num_scan_bblks;
547 uint first_half_cycle, last_half_cycle;
548 uint stop_on_cycle;
549 int error, log_bbnum = log->l_logBBsize;
550
551 /* Is the end of the log device zeroed? */
552 if ((error = xlog_find_zeroed(log, &first_blk)) == -1) {
553 *return_head_blk = first_blk;
554
555 /* Is the whole lot zeroed? */
556 if (!first_blk) {
557 /* Linux XFS shouldn't generate totally zeroed logs -
558 * mkfs etc write a dummy unmount record to a fresh
559 * log so we can store the uuid in there
560 */
561 xlog_warn("XFS: totally zeroed log");
562 }
563
564 return 0;
565 } else if (error) {
566 xlog_warn("XFS: empty log check failed");
567 return error;
568 }
569
570 first_blk = 0; /* get cycle # of 1st block */
571 bp = xlog_get_bp(log, 1);
572 if (!bp)
573 return ENOMEM;
076e6acb
CH
574
575 error = xlog_bread(log, 0, 1, bp, &offset);
576 if (error)
1da177e4 577 goto bp_err;
076e6acb 578
03bea6fe 579 first_half_cycle = xlog_get_cycle(offset);
1da177e4
LT
580
581 last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
076e6acb
CH
582 error = xlog_bread(log, last_blk, 1, bp, &offset);
583 if (error)
1da177e4 584 goto bp_err;
076e6acb 585
03bea6fe 586 last_half_cycle = xlog_get_cycle(offset);
1da177e4
LT
587 ASSERT(last_half_cycle != 0);
588
589 /*
590 * If the 1st half cycle number is equal to the last half cycle number,
591 * then the entire log is stamped with the same cycle number. In this
592 * case, head_blk can't be set to zero (which makes sense). The below
593 * math doesn't work out properly with head_blk equal to zero. Instead,
594 * we set it to log_bbnum which is an invalid block number, but this
595 * value makes the math correct. If head_blk doesn't changed through
596 * all the tests below, *head_blk is set to zero at the very end rather
597 * than log_bbnum. In a sense, log_bbnum and zero are the same block
598 * in a circular file.
599 */
600 if (first_half_cycle == last_half_cycle) {
601 /*
602 * In this case we believe that the entire log should have
603 * cycle number last_half_cycle. We need to scan backwards
604 * from the end verifying that there are no holes still
605 * containing last_half_cycle - 1. If we find such a hole,
606 * then the start of that hole will be the new head. The
607 * simple case looks like
608 * x | x ... | x - 1 | x
609 * Another case that fits this picture would be
610 * x | x + 1 | x ... | x
c41564b5 611 * In this case the head really is somewhere at the end of the
1da177e4
LT
612 * log, as one of the latest writes at the beginning was
613 * incomplete.
614 * One more case is
615 * x | x + 1 | x ... | x - 1 | x
616 * This is really the combination of the above two cases, and
617 * the head has to end up at the start of the x-1 hole at the
618 * end of the log.
619 *
620 * In the 256k log case, we will read from the beginning to the
621 * end of the log and search for cycle numbers equal to x-1.
622 * We don't worry about the x+1 blocks that we encounter,
623 * because we know that they cannot be the head since the log
624 * started with x.
625 */
626 head_blk = log_bbnum;
627 stop_on_cycle = last_half_cycle - 1;
628 } else {
629 /*
630 * In this case we want to find the first block with cycle
631 * number matching last_half_cycle. We expect the log to be
632 * some variation on
633 * x + 1 ... | x ...
634 * The first block with cycle number x (last_half_cycle) will
635 * be where the new head belongs. First we do a binary search
636 * for the first occurrence of last_half_cycle. The binary
637 * search may not be totally accurate, so then we scan back
638 * from there looking for occurrences of last_half_cycle before
639 * us. If that backwards scan wraps around the beginning of
640 * the log, then we look for occurrences of last_half_cycle - 1
641 * at the end of the log. The cases we're looking for look
642 * like
643 * x + 1 ... | x | x + 1 | x ...
644 * ^ binary search stopped here
645 * or
646 * x + 1 ... | x ... | x - 1 | x
647 * <---------> less than scan distance
648 */
649 stop_on_cycle = last_half_cycle;
650 if ((error = xlog_find_cycle_start(log, bp, first_blk,
651 &head_blk, last_half_cycle)))
652 goto bp_err;
653 }
654
655 /*
656 * Now validate the answer. Scan back some number of maximum possible
657 * blocks and make sure each one has the expected cycle number. The
658 * maximum is determined by the total possible amount of buffering
659 * in the in-core log. The following number can be made tighter if
660 * we actually look at the block size of the filesystem.
661 */
662 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
663 if (head_blk >= num_scan_bblks) {
664 /*
665 * We are guaranteed that the entire check can be performed
666 * in one buffer.
667 */
668 start_blk = head_blk - num_scan_bblks;
669 if ((error = xlog_find_verify_cycle(log,
670 start_blk, num_scan_bblks,
671 stop_on_cycle, &new_blk)))
672 goto bp_err;
673 if (new_blk != -1)
674 head_blk = new_blk;
675 } else { /* need to read 2 parts of log */
676 /*
677 * We are going to scan backwards in the log in two parts.
678 * First we scan the physical end of the log. In this part
679 * of the log, we are looking for blocks with cycle number
680 * last_half_cycle - 1.
681 * If we find one, then we know that the log starts there, as
682 * we've found a hole that didn't get written in going around
683 * the end of the physical log. The simple case for this is
684 * x + 1 ... | x ... | x - 1 | x
685 * <---------> less than scan distance
686 * If all of the blocks at the end of the log have cycle number
687 * last_half_cycle, then we check the blocks at the start of
688 * the log looking for occurrences of last_half_cycle. If we
689 * find one, then our current estimate for the location of the
690 * first occurrence of last_half_cycle is wrong and we move
691 * back to the hole we've found. This case looks like
692 * x + 1 ... | x | x + 1 | x ...
693 * ^ binary search stopped here
694 * Another case we need to handle that only occurs in 256k
695 * logs is
696 * x + 1 ... | x ... | x+1 | x ...
697 * ^ binary search stops here
698 * In a 256k log, the scan at the end of the log will see the
699 * x + 1 blocks. We need to skip past those since that is
700 * certainly not the head of the log. By searching for
701 * last_half_cycle-1 we accomplish that.
702 */
703 start_blk = log_bbnum - num_scan_bblks + head_blk;
704 ASSERT(head_blk <= INT_MAX &&
705 (xfs_daddr_t) num_scan_bblks - head_blk >= 0);
706 if ((error = xlog_find_verify_cycle(log, start_blk,
707 num_scan_bblks - (int)head_blk,
708 (stop_on_cycle - 1), &new_blk)))
709 goto bp_err;
710 if (new_blk != -1) {
711 head_blk = new_blk;
712 goto bad_blk;
713 }
714
715 /*
716 * Scan beginning of log now. The last part of the physical
717 * log is good. This scan needs to verify that it doesn't find
718 * the last_half_cycle.
719 */
720 start_blk = 0;
721 ASSERT(head_blk <= INT_MAX);
722 if ((error = xlog_find_verify_cycle(log,
723 start_blk, (int)head_blk,
724 stop_on_cycle, &new_blk)))
725 goto bp_err;
726 if (new_blk != -1)
727 head_blk = new_blk;
728 }
729
730 bad_blk:
731 /*
732 * Now we need to make sure head_blk is not pointing to a block in
733 * the middle of a log record.
734 */
735 num_scan_bblks = XLOG_REC_SHIFT(log);
736 if (head_blk >= num_scan_bblks) {
737 start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
738
739 /* start ptr at last block ptr before head_blk */
740 if ((error = xlog_find_verify_log_record(log, start_blk,
741 &head_blk, 0)) == -1) {
742 error = XFS_ERROR(EIO);
743 goto bp_err;
744 } else if (error)
745 goto bp_err;
746 } else {
747 start_blk = 0;
748 ASSERT(head_blk <= INT_MAX);
749 if ((error = xlog_find_verify_log_record(log, start_blk,
750 &head_blk, 0)) == -1) {
751 /* We hit the beginning of the log during our search */
752 start_blk = log_bbnum - num_scan_bblks + head_blk;
753 new_blk = log_bbnum;
754 ASSERT(start_blk <= INT_MAX &&
755 (xfs_daddr_t) log_bbnum-start_blk >= 0);
756 ASSERT(head_blk <= INT_MAX);
757 if ((error = xlog_find_verify_log_record(log,
758 start_blk, &new_blk,
759 (int)head_blk)) == -1) {
760 error = XFS_ERROR(EIO);
761 goto bp_err;
762 } else if (error)
763 goto bp_err;
764 if (new_blk != log_bbnum)
765 head_blk = new_blk;
766 } else if (error)
767 goto bp_err;
768 }
769
770 xlog_put_bp(bp);
771 if (head_blk == log_bbnum)
772 *return_head_blk = 0;
773 else
774 *return_head_blk = head_blk;
775 /*
776 * When returning here, we have a good block number. Bad block
777 * means that during a previous crash, we didn't have a clean break
778 * from cycle number N to cycle number N-1. In this case, we need
779 * to find the first block with cycle number N-1.
780 */
781 return 0;
782
783 bp_err:
784 xlog_put_bp(bp);
785
786 if (error)
787 xlog_warn("XFS: failed to find log head");
788 return error;
789}
790
791/*
792 * Find the sync block number or the tail of the log.
793 *
794 * This will be the block number of the last record to have its
795 * associated buffers synced to disk. Every log record header has
796 * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
797 * to get a sync block number. The only concern is to figure out which
798 * log record header to believe.
799 *
800 * The following algorithm uses the log record header with the largest
801 * lsn. The entire log record does not need to be valid. We only care
802 * that the header is valid.
803 *
804 * We could speed up search by using current head_blk buffer, but it is not
805 * available.
806 */
5d77c0dc 807STATIC int
1da177e4
LT
808xlog_find_tail(
809 xlog_t *log,
810 xfs_daddr_t *head_blk,
65be6054 811 xfs_daddr_t *tail_blk)
1da177e4
LT
812{
813 xlog_rec_header_t *rhead;
814 xlog_op_header_t *op_head;
815 xfs_caddr_t offset = NULL;
816 xfs_buf_t *bp;
817 int error, i, found;
818 xfs_daddr_t umount_data_blk;
819 xfs_daddr_t after_umount_blk;
820 xfs_lsn_t tail_lsn;
821 int hblks;
822
823 found = 0;
824
825 /*
826 * Find previous log record
827 */
828 if ((error = xlog_find_head(log, head_blk)))
829 return error;
830
831 bp = xlog_get_bp(log, 1);
832 if (!bp)
833 return ENOMEM;
834 if (*head_blk == 0) { /* special case */
076e6acb
CH
835 error = xlog_bread(log, 0, 1, bp, &offset);
836 if (error)
1da177e4 837 goto bread_err;
076e6acb 838
03bea6fe 839 if (xlog_get_cycle(offset) == 0) {
1da177e4
LT
840 *tail_blk = 0;
841 /* leave all other log inited values alone */
842 goto exit;
843 }
844 }
845
846 /*
847 * Search backwards looking for log record header block
848 */
849 ASSERT(*head_blk < INT_MAX);
850 for (i = (int)(*head_blk) - 1; i >= 0; i--) {
076e6acb
CH
851 error = xlog_bread(log, i, 1, bp, &offset);
852 if (error)
1da177e4 853 goto bread_err;
076e6acb 854
b53e675d 855 if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(*(__be32 *)offset)) {
1da177e4
LT
856 found = 1;
857 break;
858 }
859 }
860 /*
861 * If we haven't found the log record header block, start looking
862 * again from the end of the physical log. XXXmiken: There should be
863 * a check here to make sure we didn't search more than N blocks in
864 * the previous code.
865 */
866 if (!found) {
867 for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
076e6acb
CH
868 error = xlog_bread(log, i, 1, bp, &offset);
869 if (error)
1da177e4 870 goto bread_err;
076e6acb 871
1da177e4 872 if (XLOG_HEADER_MAGIC_NUM ==
b53e675d 873 be32_to_cpu(*(__be32 *)offset)) {
1da177e4
LT
874 found = 2;
875 break;
876 }
877 }
878 }
879 if (!found) {
880 xlog_warn("XFS: xlog_find_tail: couldn't find sync record");
881 ASSERT(0);
882 return XFS_ERROR(EIO);
883 }
884
885 /* find blk_no of tail of log */
886 rhead = (xlog_rec_header_t *)offset;
b53e675d 887 *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
1da177e4
LT
888
889 /*
890 * Reset log values according to the state of the log when we
891 * crashed. In the case where head_blk == 0, we bump curr_cycle
892 * one because the next write starts a new cycle rather than
893 * continuing the cycle of the last good log record. At this
894 * point we have guaranteed that all partial log records have been
895 * accounted for. Therefore, we know that the last good log record
896 * written was complete and ended exactly on the end boundary
897 * of the physical log.
898 */
899 log->l_prev_block = i;
900 log->l_curr_block = (int)*head_blk;
b53e675d 901 log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
1da177e4
LT
902 if (found == 2)
903 log->l_curr_cycle++;
b53e675d
CH
904 log->l_tail_lsn = be64_to_cpu(rhead->h_tail_lsn);
905 log->l_last_sync_lsn = be64_to_cpu(rhead->h_lsn);
1da177e4
LT
906 log->l_grant_reserve_cycle = log->l_curr_cycle;
907 log->l_grant_reserve_bytes = BBTOB(log->l_curr_block);
908 log->l_grant_write_cycle = log->l_curr_cycle;
909 log->l_grant_write_bytes = BBTOB(log->l_curr_block);
910
911 /*
912 * Look for unmount record. If we find it, then we know there
913 * was a clean unmount. Since 'i' could be the last block in
914 * the physical log, we convert to a log block before comparing
915 * to the head_blk.
916 *
917 * Save the current tail lsn to use to pass to
918 * xlog_clear_stale_blocks() below. We won't want to clear the
919 * unmount record if there is one, so we pass the lsn of the
920 * unmount record rather than the block after it.
921 */
62118709 922 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
b53e675d
CH
923 int h_size = be32_to_cpu(rhead->h_size);
924 int h_version = be32_to_cpu(rhead->h_version);
1da177e4
LT
925
926 if ((h_version & XLOG_VERSION_2) &&
927 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
928 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
929 if (h_size % XLOG_HEADER_CYCLE_SIZE)
930 hblks++;
931 } else {
932 hblks = 1;
933 }
934 } else {
935 hblks = 1;
936 }
937 after_umount_blk = (i + hblks + (int)
b53e675d 938 BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
1da177e4
LT
939 tail_lsn = log->l_tail_lsn;
940 if (*head_blk == after_umount_blk &&
b53e675d 941 be32_to_cpu(rhead->h_num_logops) == 1) {
1da177e4 942 umount_data_blk = (i + hblks) % log->l_logBBsize;
076e6acb
CH
943 error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
944 if (error)
1da177e4 945 goto bread_err;
076e6acb 946
1da177e4
LT
947 op_head = (xlog_op_header_t *)offset;
948 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
949 /*
950 * Set tail and last sync so that newly written
951 * log records will point recovery to after the
952 * current unmount record.
953 */
03bea6fe
CH
954 log->l_tail_lsn =
955 xlog_assign_lsn(log->l_curr_cycle,
956 after_umount_blk);
957 log->l_last_sync_lsn =
958 xlog_assign_lsn(log->l_curr_cycle,
959 after_umount_blk);
1da177e4 960 *tail_blk = after_umount_blk;
92821e2b
DC
961
962 /*
963 * Note that the unmount was clean. If the unmount
964 * was not clean, we need to know this to rebuild the
965 * superblock counters from the perag headers if we
966 * have a filesystem using non-persistent counters.
967 */
968 log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
1da177e4
LT
969 }
970 }
971
972 /*
973 * Make sure that there are no blocks in front of the head
974 * with the same cycle number as the head. This can happen
975 * because we allow multiple outstanding log writes concurrently,
976 * and the later writes might make it out before earlier ones.
977 *
978 * We use the lsn from before modifying it so that we'll never
979 * overwrite the unmount record after a clean unmount.
980 *
981 * Do this only if we are going to recover the filesystem
982 *
983 * NOTE: This used to say "if (!readonly)"
984 * However on Linux, we can & do recover a read-only filesystem.
985 * We only skip recovery if NORECOVERY is specified on mount,
986 * in which case we would not be here.
987 *
988 * But... if the -device- itself is readonly, just skip this.
989 * We can't recover this device anyway, so it won't matter.
990 */
991 if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp)) {
992 error = xlog_clear_stale_blocks(log, tail_lsn);
993 }
994
995bread_err:
996exit:
997 xlog_put_bp(bp);
998
999 if (error)
1000 xlog_warn("XFS: failed to locate log tail");
1001 return error;
1002}
1003
1004/*
1005 * Is the log zeroed at all?
1006 *
1007 * The last binary search should be changed to perform an X block read
1008 * once X becomes small enough. You can then search linearly through
1009 * the X blocks. This will cut down on the number of reads we need to do.
1010 *
1011 * If the log is partially zeroed, this routine will pass back the blkno
1012 * of the first block with cycle number 0. It won't have a complete LR
1013 * preceding it.
1014 *
1015 * Return:
1016 * 0 => the log is completely written to
1017 * -1 => use *blk_no as the first block of the log
1018 * >0 => error has occurred
1019 */
a8272ce0 1020STATIC int
1da177e4
LT
1021xlog_find_zeroed(
1022 xlog_t *log,
1023 xfs_daddr_t *blk_no)
1024{
1025 xfs_buf_t *bp;
1026 xfs_caddr_t offset;
1027 uint first_cycle, last_cycle;
1028 xfs_daddr_t new_blk, last_blk, start_blk;
1029 xfs_daddr_t num_scan_bblks;
1030 int error, log_bbnum = log->l_logBBsize;
1031
6fdf8ccc
NS
1032 *blk_no = 0;
1033
1da177e4
LT
1034 /* check totally zeroed log */
1035 bp = xlog_get_bp(log, 1);
1036 if (!bp)
1037 return ENOMEM;
076e6acb
CH
1038 error = xlog_bread(log, 0, 1, bp, &offset);
1039 if (error)
1da177e4 1040 goto bp_err;
076e6acb 1041
03bea6fe 1042 first_cycle = xlog_get_cycle(offset);
1da177e4
LT
1043 if (first_cycle == 0) { /* completely zeroed log */
1044 *blk_no = 0;
1045 xlog_put_bp(bp);
1046 return -1;
1047 }
1048
1049 /* check partially zeroed log */
076e6acb
CH
1050 error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
1051 if (error)
1da177e4 1052 goto bp_err;
076e6acb 1053
03bea6fe 1054 last_cycle = xlog_get_cycle(offset);
1da177e4
LT
1055 if (last_cycle != 0) { /* log completely written to */
1056 xlog_put_bp(bp);
1057 return 0;
1058 } else if (first_cycle != 1) {
1059 /*
1060 * If the cycle of the last block is zero, the cycle of
1061 * the first block must be 1. If it's not, maybe we're
1062 * not looking at a log... Bail out.
1063 */
1064 xlog_warn("XFS: Log inconsistent or not a log (last==0, first!=1)");
1065 return XFS_ERROR(EINVAL);
1066 }
1067
1068 /* we have a partially zeroed log */
1069 last_blk = log_bbnum-1;
1070 if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
1071 goto bp_err;
1072
1073 /*
1074 * Validate the answer. Because there is no way to guarantee that
1075 * the entire log is made up of log records which are the same size,
1076 * we scan over the defined maximum blocks. At this point, the maximum
1077 * is not chosen to mean anything special. XXXmiken
1078 */
1079 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1080 ASSERT(num_scan_bblks <= INT_MAX);
1081
1082 if (last_blk < num_scan_bblks)
1083 num_scan_bblks = last_blk;
1084 start_blk = last_blk - num_scan_bblks;
1085
1086 /*
1087 * We search for any instances of cycle number 0 that occur before
1088 * our current estimate of the head. What we're trying to detect is
1089 * 1 ... | 0 | 1 | 0...
1090 * ^ binary search ends here
1091 */
1092 if ((error = xlog_find_verify_cycle(log, start_blk,
1093 (int)num_scan_bblks, 0, &new_blk)))
1094 goto bp_err;
1095 if (new_blk != -1)
1096 last_blk = new_blk;
1097
1098 /*
1099 * Potentially backup over partial log record write. We don't need
1100 * to search the end of the log because we know it is zero.
1101 */
1102 if ((error = xlog_find_verify_log_record(log, start_blk,
1103 &last_blk, 0)) == -1) {
1104 error = XFS_ERROR(EIO);
1105 goto bp_err;
1106 } else if (error)
1107 goto bp_err;
1108
1109 *blk_no = last_blk;
1110bp_err:
1111 xlog_put_bp(bp);
1112 if (error)
1113 return error;
1114 return -1;
1115}
1116
1117/*
1118 * These are simple subroutines used by xlog_clear_stale_blocks() below
1119 * to initialize a buffer full of empty log record headers and write
1120 * them into the log.
1121 */
1122STATIC void
1123xlog_add_record(
1124 xlog_t *log,
1125 xfs_caddr_t buf,
1126 int cycle,
1127 int block,
1128 int tail_cycle,
1129 int tail_block)
1130{
1131 xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1132
1133 memset(buf, 0, BBSIZE);
b53e675d
CH
1134 recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1135 recp->h_cycle = cpu_to_be32(cycle);
1136 recp->h_version = cpu_to_be32(
62118709 1137 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
b53e675d
CH
1138 recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1139 recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1140 recp->h_fmt = cpu_to_be32(XLOG_FMT);
1da177e4
LT
1141 memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1142}
1143
1144STATIC int
1145xlog_write_log_records(
1146 xlog_t *log,
1147 int cycle,
1148 int start_block,
1149 int blocks,
1150 int tail_cycle,
1151 int tail_block)
1152{
1153 xfs_caddr_t offset;
1154 xfs_buf_t *bp;
1155 int balign, ealign;
5c17f533 1156 int sectbb = xlog_sectbb(log);
1da177e4
LT
1157 int end_block = start_block + blocks;
1158 int bufblks;
1159 int error = 0;
1160 int i, j = 0;
1161
6881a229
AE
1162 /*
1163 * Greedily allocate a buffer big enough to handle the full
1164 * range of basic blocks to be written. If that fails, try
1165 * a smaller size. We need to be able to write at least a
1166 * log sector, or we're out of luck.
1167 */
1da177e4
LT
1168 bufblks = 1 << ffs(blocks);
1169 while (!(bp = xlog_get_bp(log, bufblks))) {
1170 bufblks >>= 1;
6881a229 1171 if (bufblks < xlog_sectbb(log))
1da177e4
LT
1172 return ENOMEM;
1173 }
1174
1175 /* We may need to do a read at the start to fill in part of
1176 * the buffer in the starting sector not covered by the first
1177 * write below.
1178 */
5c17f533 1179 balign = round_down(start_block, sectbb);
1da177e4 1180 if (balign != start_block) {
076e6acb
CH
1181 error = xlog_bread_noalign(log, start_block, 1, bp);
1182 if (error)
1183 goto out_put_bp;
1184
1da177e4
LT
1185 j = start_block - balign;
1186 }
1187
1188 for (i = start_block; i < end_block; i += bufblks) {
1189 int bcount, endcount;
1190
1191 bcount = min(bufblks, end_block - start_block);
1192 endcount = bcount - j;
1193
1194 /* We may need to do a read at the end to fill in part of
1195 * the buffer in the final sector not covered by the write.
1196 * If this is the same sector as the above read, skip it.
1197 */
5c17f533 1198 ealign = round_down(end_block, sectbb);
1da177e4
LT
1199 if (j == 0 && (start_block + endcount > ealign)) {
1200 offset = XFS_BUF_PTR(bp);
1201 balign = BBTOB(ealign - start_block);
234f56ac
DC
1202 error = XFS_BUF_SET_PTR(bp, offset + balign,
1203 BBTOB(sectbb));
076e6acb
CH
1204 if (error)
1205 break;
1206
1207 error = xlog_bread_noalign(log, ealign, sectbb, bp);
1208 if (error)
1209 break;
1210
1211 error = XFS_BUF_SET_PTR(bp, offset, bufblks);
234f56ac 1212 if (error)
1da177e4 1213 break;
1da177e4
LT
1214 }
1215
1216 offset = xlog_align(log, start_block, endcount, bp);
1217 for (; j < endcount; j++) {
1218 xlog_add_record(log, offset, cycle, i+j,
1219 tail_cycle, tail_block);
1220 offset += BBSIZE;
1221 }
1222 error = xlog_bwrite(log, start_block, endcount, bp);
1223 if (error)
1224 break;
1225 start_block += endcount;
1226 j = 0;
1227 }
076e6acb
CH
1228
1229 out_put_bp:
1da177e4
LT
1230 xlog_put_bp(bp);
1231 return error;
1232}
1233
1234/*
1235 * This routine is called to blow away any incomplete log writes out
1236 * in front of the log head. We do this so that we won't become confused
1237 * if we come up, write only a little bit more, and then crash again.
1238 * If we leave the partial log records out there, this situation could
1239 * cause us to think those partial writes are valid blocks since they
1240 * have the current cycle number. We get rid of them by overwriting them
1241 * with empty log records with the old cycle number rather than the
1242 * current one.
1243 *
1244 * The tail lsn is passed in rather than taken from
1245 * the log so that we will not write over the unmount record after a
1246 * clean unmount in a 512 block log. Doing so would leave the log without
1247 * any valid log records in it until a new one was written. If we crashed
1248 * during that time we would not be able to recover.
1249 */
1250STATIC int
1251xlog_clear_stale_blocks(
1252 xlog_t *log,
1253 xfs_lsn_t tail_lsn)
1254{
1255 int tail_cycle, head_cycle;
1256 int tail_block, head_block;
1257 int tail_distance, max_distance;
1258 int distance;
1259 int error;
1260
1261 tail_cycle = CYCLE_LSN(tail_lsn);
1262 tail_block = BLOCK_LSN(tail_lsn);
1263 head_cycle = log->l_curr_cycle;
1264 head_block = log->l_curr_block;
1265
1266 /*
1267 * Figure out the distance between the new head of the log
1268 * and the tail. We want to write over any blocks beyond the
1269 * head that we may have written just before the crash, but
1270 * we don't want to overwrite the tail of the log.
1271 */
1272 if (head_cycle == tail_cycle) {
1273 /*
1274 * The tail is behind the head in the physical log,
1275 * so the distance from the head to the tail is the
1276 * distance from the head to the end of the log plus
1277 * the distance from the beginning of the log to the
1278 * tail.
1279 */
1280 if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
1281 XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
1282 XFS_ERRLEVEL_LOW, log->l_mp);
1283 return XFS_ERROR(EFSCORRUPTED);
1284 }
1285 tail_distance = tail_block + (log->l_logBBsize - head_block);
1286 } else {
1287 /*
1288 * The head is behind the tail in the physical log,
1289 * so the distance from the head to the tail is just
1290 * the tail block minus the head block.
1291 */
1292 if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
1293 XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
1294 XFS_ERRLEVEL_LOW, log->l_mp);
1295 return XFS_ERROR(EFSCORRUPTED);
1296 }
1297 tail_distance = tail_block - head_block;
1298 }
1299
1300 /*
1301 * If the head is right up against the tail, we can't clear
1302 * anything.
1303 */
1304 if (tail_distance <= 0) {
1305 ASSERT(tail_distance == 0);
1306 return 0;
1307 }
1308
1309 max_distance = XLOG_TOTAL_REC_SHIFT(log);
1310 /*
1311 * Take the smaller of the maximum amount of outstanding I/O
1312 * we could have and the distance to the tail to clear out.
1313 * We take the smaller so that we don't overwrite the tail and
1314 * we don't waste all day writing from the head to the tail
1315 * for no reason.
1316 */
1317 max_distance = MIN(max_distance, tail_distance);
1318
1319 if ((head_block + max_distance) <= log->l_logBBsize) {
1320 /*
1321 * We can stomp all the blocks we need to without
1322 * wrapping around the end of the log. Just do it
1323 * in a single write. Use the cycle number of the
1324 * current cycle minus one so that the log will look like:
1325 * n ... | n - 1 ...
1326 */
1327 error = xlog_write_log_records(log, (head_cycle - 1),
1328 head_block, max_distance, tail_cycle,
1329 tail_block);
1330 if (error)
1331 return error;
1332 } else {
1333 /*
1334 * We need to wrap around the end of the physical log in
1335 * order to clear all the blocks. Do it in two separate
1336 * I/Os. The first write should be from the head to the
1337 * end of the physical log, and it should use the current
1338 * cycle number minus one just like above.
1339 */
1340 distance = log->l_logBBsize - head_block;
1341 error = xlog_write_log_records(log, (head_cycle - 1),
1342 head_block, distance, tail_cycle,
1343 tail_block);
1344
1345 if (error)
1346 return error;
1347
1348 /*
1349 * Now write the blocks at the start of the physical log.
1350 * This writes the remainder of the blocks we want to clear.
1351 * It uses the current cycle number since we're now on the
1352 * same cycle as the head so that we get:
1353 * n ... n ... | n - 1 ...
1354 * ^^^^^ blocks we're writing
1355 */
1356 distance = max_distance - (log->l_logBBsize - head_block);
1357 error = xlog_write_log_records(log, head_cycle, 0, distance,
1358 tail_cycle, tail_block);
1359 if (error)
1360 return error;
1361 }
1362
1363 return 0;
1364}
1365
1366/******************************************************************************
1367 *
1368 * Log recover routines
1369 *
1370 ******************************************************************************
1371 */
1372
1373STATIC xlog_recover_t *
1374xlog_recover_find_tid(
f0a76953 1375 struct hlist_head *head,
1da177e4
LT
1376 xlog_tid_t tid)
1377{
f0a76953
DC
1378 xlog_recover_t *trans;
1379 struct hlist_node *n;
1da177e4 1380
f0a76953
DC
1381 hlist_for_each_entry(trans, n, head, r_list) {
1382 if (trans->r_log_tid == tid)
1383 return trans;
1da177e4 1384 }
f0a76953 1385 return NULL;
1da177e4
LT
1386}
1387
1388STATIC void
f0a76953
DC
1389xlog_recover_new_tid(
1390 struct hlist_head *head,
1391 xlog_tid_t tid,
1392 xfs_lsn_t lsn)
1da177e4 1393{
f0a76953
DC
1394 xlog_recover_t *trans;
1395
1396 trans = kmem_zalloc(sizeof(xlog_recover_t), KM_SLEEP);
1397 trans->r_log_tid = tid;
1398 trans->r_lsn = lsn;
1399 INIT_LIST_HEAD(&trans->r_itemq);
1400
1401 INIT_HLIST_NODE(&trans->r_list);
1402 hlist_add_head(&trans->r_list, head);
1da177e4
LT
1403}
1404
1405STATIC void
1406xlog_recover_add_item(
f0a76953 1407 struct list_head *head)
1da177e4
LT
1408{
1409 xlog_recover_item_t *item;
1410
1411 item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
f0a76953
DC
1412 INIT_LIST_HEAD(&item->ri_list);
1413 list_add_tail(&item->ri_list, head);
1da177e4
LT
1414}
1415
1416STATIC int
1417xlog_recover_add_to_cont_trans(
9abbc539 1418 struct log *log,
1da177e4
LT
1419 xlog_recover_t *trans,
1420 xfs_caddr_t dp,
1421 int len)
1422{
1423 xlog_recover_item_t *item;
1424 xfs_caddr_t ptr, old_ptr;
1425 int old_len;
1426
f0a76953 1427 if (list_empty(&trans->r_itemq)) {
1da177e4
LT
1428 /* finish copying rest of trans header */
1429 xlog_recover_add_item(&trans->r_itemq);
1430 ptr = (xfs_caddr_t) &trans->r_theader +
1431 sizeof(xfs_trans_header_t) - len;
1432 memcpy(ptr, dp, len); /* d, s, l */
1433 return 0;
1434 }
f0a76953
DC
1435 /* take the tail entry */
1436 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
1da177e4
LT
1437
1438 old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
1439 old_len = item->ri_buf[item->ri_cnt-1].i_len;
1440
760dea67 1441 ptr = kmem_realloc(old_ptr, len+old_len, old_len, 0u);
1da177e4
LT
1442 memcpy(&ptr[old_len], dp, len); /* d, s, l */
1443 item->ri_buf[item->ri_cnt-1].i_len += len;
1444 item->ri_buf[item->ri_cnt-1].i_addr = ptr;
9abbc539 1445 trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
1da177e4
LT
1446 return 0;
1447}
1448
1449/*
1450 * The next region to add is the start of a new region. It could be
1451 * a whole region or it could be the first part of a new region. Because
1452 * of this, the assumption here is that the type and size fields of all
1453 * format structures fit into the first 32 bits of the structure.
1454 *
1455 * This works because all regions must be 32 bit aligned. Therefore, we
1456 * either have both fields or we have neither field. In the case we have
1457 * neither field, the data part of the region is zero length. We only have
1458 * a log_op_header and can throw away the header since a new one will appear
1459 * later. If we have at least 4 bytes, then we can determine how many regions
1460 * will appear in the current log item.
1461 */
1462STATIC int
1463xlog_recover_add_to_trans(
9abbc539 1464 struct log *log,
1da177e4
LT
1465 xlog_recover_t *trans,
1466 xfs_caddr_t dp,
1467 int len)
1468{
1469 xfs_inode_log_format_t *in_f; /* any will do */
1470 xlog_recover_item_t *item;
1471 xfs_caddr_t ptr;
1472
1473 if (!len)
1474 return 0;
f0a76953 1475 if (list_empty(&trans->r_itemq)) {
5a792c45
DC
1476 /* we need to catch log corruptions here */
1477 if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
1478 xlog_warn("XFS: xlog_recover_add_to_trans: "
1479 "bad header magic number");
1480 ASSERT(0);
1481 return XFS_ERROR(EIO);
1482 }
1da177e4
LT
1483 if (len == sizeof(xfs_trans_header_t))
1484 xlog_recover_add_item(&trans->r_itemq);
1485 memcpy(&trans->r_theader, dp, len); /* d, s, l */
1486 return 0;
1487 }
1488
1489 ptr = kmem_alloc(len, KM_SLEEP);
1490 memcpy(ptr, dp, len);
1491 in_f = (xfs_inode_log_format_t *)ptr;
1492
f0a76953
DC
1493 /* take the tail entry */
1494 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
1495 if (item->ri_total != 0 &&
1496 item->ri_total == item->ri_cnt) {
1497 /* tail item is in use, get a new one */
1da177e4 1498 xlog_recover_add_item(&trans->r_itemq);
f0a76953
DC
1499 item = list_entry(trans->r_itemq.prev,
1500 xlog_recover_item_t, ri_list);
1da177e4 1501 }
1da177e4
LT
1502
1503 if (item->ri_total == 0) { /* first region to be added */
e8fa6b48
CH
1504 if (in_f->ilf_size == 0 ||
1505 in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
1506 xlog_warn(
1507 "XFS: bad number of regions (%d) in inode log format",
1508 in_f->ilf_size);
1509 ASSERT(0);
1510 return XFS_ERROR(EIO);
1511 }
1512
1513 item->ri_total = in_f->ilf_size;
1514 item->ri_buf =
1515 kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
1516 KM_SLEEP);
1da177e4
LT
1517 }
1518 ASSERT(item->ri_total > item->ri_cnt);
1519 /* Description region is ri_buf[0] */
1520 item->ri_buf[item->ri_cnt].i_addr = ptr;
1521 item->ri_buf[item->ri_cnt].i_len = len;
1522 item->ri_cnt++;
9abbc539 1523 trace_xfs_log_recover_item_add(log, trans, item, 0);
1da177e4
LT
1524 return 0;
1525}
1526
f0a76953
DC
1527/*
1528 * Sort the log items in the transaction. Cancelled buffers need
1529 * to be put first so they are processed before any items that might
1530 * modify the buffers. If they are cancelled, then the modifications
1531 * don't need to be replayed.
1532 */
1da177e4
LT
1533STATIC int
1534xlog_recover_reorder_trans(
9abbc539
DC
1535 struct log *log,
1536 xlog_recover_t *trans,
1537 int pass)
1da177e4 1538{
f0a76953
DC
1539 xlog_recover_item_t *item, *n;
1540 LIST_HEAD(sort_list);
1541
1542 list_splice_init(&trans->r_itemq, &sort_list);
1543 list_for_each_entry_safe(item, n, &sort_list, ri_list) {
1544 xfs_buf_log_format_t *buf_f;
1da177e4 1545
f0a76953 1546 buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
1da177e4 1547
f0a76953 1548 switch (ITEM_TYPE(item)) {
1da177e4 1549 case XFS_LI_BUF:
f0a76953 1550 if (!(buf_f->blf_flags & XFS_BLI_CANCEL)) {
9abbc539
DC
1551 trace_xfs_log_recover_item_reorder_head(log,
1552 trans, item, pass);
f0a76953 1553 list_move(&item->ri_list, &trans->r_itemq);
1da177e4
LT
1554 break;
1555 }
1556 case XFS_LI_INODE:
1da177e4
LT
1557 case XFS_LI_DQUOT:
1558 case XFS_LI_QUOTAOFF:
1559 case XFS_LI_EFD:
1560 case XFS_LI_EFI:
9abbc539
DC
1561 trace_xfs_log_recover_item_reorder_tail(log,
1562 trans, item, pass);
f0a76953 1563 list_move_tail(&item->ri_list, &trans->r_itemq);
1da177e4
LT
1564 break;
1565 default:
1566 xlog_warn(
1567 "XFS: xlog_recover_reorder_trans: unrecognized type of log operation");
1568 ASSERT(0);
1569 return XFS_ERROR(EIO);
1570 }
f0a76953
DC
1571 }
1572 ASSERT(list_empty(&sort_list));
1da177e4
LT
1573 return 0;
1574}
1575
1576/*
1577 * Build up the table of buf cancel records so that we don't replay
1578 * cancelled data in the second pass. For buffer records that are
1579 * not cancel records, there is nothing to do here so we just return.
1580 *
1581 * If we get a cancel record which is already in the table, this indicates
1582 * that the buffer was cancelled multiple times. In order to ensure
1583 * that during pass 2 we keep the record in the table until we reach its
1584 * last occurrence in the log, we keep a reference count in the cancel
1585 * record in the table to tell us how many times we expect to see this
1586 * record during the second pass.
1587 */
1588STATIC void
1589xlog_recover_do_buffer_pass1(
1590 xlog_t *log,
1591 xfs_buf_log_format_t *buf_f)
1592{
1593 xfs_buf_cancel_t *bcp;
1594 xfs_buf_cancel_t *nextp;
1595 xfs_buf_cancel_t *prevp;
1596 xfs_buf_cancel_t **bucket;
1da177e4
LT
1597 xfs_daddr_t blkno = 0;
1598 uint len = 0;
1599 ushort flags = 0;
1600
1601 switch (buf_f->blf_type) {
1602 case XFS_LI_BUF:
1603 blkno = buf_f->blf_blkno;
1604 len = buf_f->blf_len;
1605 flags = buf_f->blf_flags;
1606 break;
1da177e4
LT
1607 }
1608
1609 /*
1610 * If this isn't a cancel buffer item, then just return.
1611 */
9abbc539
DC
1612 if (!(flags & XFS_BLI_CANCEL)) {
1613 trace_xfs_log_recover_buf_not_cancel(log, buf_f);
1da177e4 1614 return;
9abbc539 1615 }
1da177e4
LT
1616
1617 /*
1618 * Insert an xfs_buf_cancel record into the hash table of
1619 * them. If there is already an identical record, bump
1620 * its reference count.
1621 */
1622 bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
1623 XLOG_BC_TABLE_SIZE];
1624 /*
1625 * If the hash bucket is empty then just insert a new record into
1626 * the bucket.
1627 */
1628 if (*bucket == NULL) {
1629 bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
1630 KM_SLEEP);
1631 bcp->bc_blkno = blkno;
1632 bcp->bc_len = len;
1633 bcp->bc_refcount = 1;
1634 bcp->bc_next = NULL;
1635 *bucket = bcp;
1636 return;
1637 }
1638
1639 /*
1640 * The hash bucket is not empty, so search for duplicates of our
1641 * record. If we find one them just bump its refcount. If not
1642 * then add us at the end of the list.
1643 */
1644 prevp = NULL;
1645 nextp = *bucket;
1646 while (nextp != NULL) {
1647 if (nextp->bc_blkno == blkno && nextp->bc_len == len) {
1648 nextp->bc_refcount++;
9abbc539 1649 trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
1da177e4
LT
1650 return;
1651 }
1652 prevp = nextp;
1653 nextp = nextp->bc_next;
1654 }
1655 ASSERT(prevp != NULL);
1656 bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
1657 KM_SLEEP);
1658 bcp->bc_blkno = blkno;
1659 bcp->bc_len = len;
1660 bcp->bc_refcount = 1;
1661 bcp->bc_next = NULL;
1662 prevp->bc_next = bcp;
9abbc539 1663 trace_xfs_log_recover_buf_cancel_add(log, buf_f);
1da177e4
LT
1664}
1665
1666/*
1667 * Check to see whether the buffer being recovered has a corresponding
1668 * entry in the buffer cancel record table. If it does then return 1
1669 * so that it will be cancelled, otherwise return 0. If the buffer is
1670 * actually a buffer cancel item (XFS_BLI_CANCEL is set), then decrement
1671 * the refcount on the entry in the table and remove it from the table
1672 * if this is the last reference.
1673 *
1674 * We remove the cancel record from the table when we encounter its
1675 * last occurrence in the log so that if the same buffer is re-used
1676 * again after its last cancellation we actually replay the changes
1677 * made at that point.
1678 */
1679STATIC int
1680xlog_check_buffer_cancelled(
1681 xlog_t *log,
1682 xfs_daddr_t blkno,
1683 uint len,
1684 ushort flags)
1685{
1686 xfs_buf_cancel_t *bcp;
1687 xfs_buf_cancel_t *prevp;
1688 xfs_buf_cancel_t **bucket;
1689
1690 if (log->l_buf_cancel_table == NULL) {
1691 /*
1692 * There is nothing in the table built in pass one,
1693 * so this buffer must not be cancelled.
1694 */
1695 ASSERT(!(flags & XFS_BLI_CANCEL));
1696 return 0;
1697 }
1698
1699 bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
1700 XLOG_BC_TABLE_SIZE];
1701 bcp = *bucket;
1702 if (bcp == NULL) {
1703 /*
1704 * There is no corresponding entry in the table built
1705 * in pass one, so this buffer has not been cancelled.
1706 */
1707 ASSERT(!(flags & XFS_BLI_CANCEL));
1708 return 0;
1709 }
1710
1711 /*
1712 * Search for an entry in the buffer cancel table that
1713 * matches our buffer.
1714 */
1715 prevp = NULL;
1716 while (bcp != NULL) {
1717 if (bcp->bc_blkno == blkno && bcp->bc_len == len) {
1718 /*
1719 * We've go a match, so return 1 so that the
1720 * recovery of this buffer is cancelled.
1721 * If this buffer is actually a buffer cancel
1722 * log item, then decrement the refcount on the
1723 * one in the table and remove it if this is the
1724 * last reference.
1725 */
1726 if (flags & XFS_BLI_CANCEL) {
1727 bcp->bc_refcount--;
1728 if (bcp->bc_refcount == 0) {
1729 if (prevp == NULL) {
1730 *bucket = bcp->bc_next;
1731 } else {
1732 prevp->bc_next = bcp->bc_next;
1733 }
f0e2d93c 1734 kmem_free(bcp);
1da177e4
LT
1735 }
1736 }
1737 return 1;
1738 }
1739 prevp = bcp;
1740 bcp = bcp->bc_next;
1741 }
1742 /*
1743 * We didn't find a corresponding entry in the table, so
1744 * return 0 so that the buffer is NOT cancelled.
1745 */
1746 ASSERT(!(flags & XFS_BLI_CANCEL));
1747 return 0;
1748}
1749
1750STATIC int
1751xlog_recover_do_buffer_pass2(
1752 xlog_t *log,
1753 xfs_buf_log_format_t *buf_f)
1754{
1da177e4
LT
1755 xfs_daddr_t blkno = 0;
1756 ushort flags = 0;
1757 uint len = 0;
1758
1759 switch (buf_f->blf_type) {
1760 case XFS_LI_BUF:
1761 blkno = buf_f->blf_blkno;
1762 flags = buf_f->blf_flags;
1763 len = buf_f->blf_len;
1764 break;
1da177e4
LT
1765 }
1766
1767 return xlog_check_buffer_cancelled(log, blkno, len, flags);
1768}
1769
1770/*
1771 * Perform recovery for a buffer full of inodes. In these buffers,
1772 * the only data which should be recovered is that which corresponds
1773 * to the di_next_unlinked pointers in the on disk inode structures.
1774 * The rest of the data for the inodes is always logged through the
1775 * inodes themselves rather than the inode buffer and is recovered
1776 * in xlog_recover_do_inode_trans().
1777 *
1778 * The only time when buffers full of inodes are fully recovered is
1779 * when the buffer is full of newly allocated inodes. In this case
1780 * the buffer will not be marked as an inode buffer and so will be
1781 * sent to xlog_recover_do_reg_buffer() below during recovery.
1782 */
1783STATIC int
1784xlog_recover_do_inode_buffer(
1785 xfs_mount_t *mp,
1786 xlog_recover_item_t *item,
1787 xfs_buf_t *bp,
1788 xfs_buf_log_format_t *buf_f)
1789{
1790 int i;
1791 int item_index;
1792 int bit;
1793 int nbits;
1794 int reg_buf_offset;
1795 int reg_buf_bytes;
1796 int next_unlinked_offset;
1797 int inodes_per_buf;
1798 xfs_agino_t *logged_nextp;
1799 xfs_agino_t *buffer_nextp;
1da177e4
LT
1800 unsigned int *data_map = NULL;
1801 unsigned int map_size = 0;
1802
9abbc539
DC
1803 trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
1804
1da177e4
LT
1805 switch (buf_f->blf_type) {
1806 case XFS_LI_BUF:
1807 data_map = buf_f->blf_data_map;
1808 map_size = buf_f->blf_map_size;
1809 break;
1da177e4
LT
1810 }
1811 /*
1812 * Set the variables corresponding to the current region to
1813 * 0 so that we'll initialize them on the first pass through
1814 * the loop.
1815 */
1816 reg_buf_offset = 0;
1817 reg_buf_bytes = 0;
1818 bit = 0;
1819 nbits = 0;
1820 item_index = 0;
1821 inodes_per_buf = XFS_BUF_COUNT(bp) >> mp->m_sb.sb_inodelog;
1822 for (i = 0; i < inodes_per_buf; i++) {
1823 next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
1824 offsetof(xfs_dinode_t, di_next_unlinked);
1825
1826 while (next_unlinked_offset >=
1827 (reg_buf_offset + reg_buf_bytes)) {
1828 /*
1829 * The next di_next_unlinked field is beyond
1830 * the current logged region. Find the next
1831 * logged region that contains or is beyond
1832 * the current di_next_unlinked field.
1833 */
1834 bit += nbits;
1835 bit = xfs_next_bit(data_map, map_size, bit);
1836
1837 /*
1838 * If there are no more logged regions in the
1839 * buffer, then we're done.
1840 */
1841 if (bit == -1) {
1842 return 0;
1843 }
1844
1845 nbits = xfs_contig_bits(data_map, map_size,
1846 bit);
1847 ASSERT(nbits > 0);
1848 reg_buf_offset = bit << XFS_BLI_SHIFT;
1849 reg_buf_bytes = nbits << XFS_BLI_SHIFT;
1850 item_index++;
1851 }
1852
1853 /*
1854 * If the current logged region starts after the current
1855 * di_next_unlinked field, then move on to the next
1856 * di_next_unlinked field.
1857 */
1858 if (next_unlinked_offset < reg_buf_offset) {
1859 continue;
1860 }
1861
1862 ASSERT(item->ri_buf[item_index].i_addr != NULL);
1863 ASSERT((item->ri_buf[item_index].i_len % XFS_BLI_CHUNK) == 0);
1864 ASSERT((reg_buf_offset + reg_buf_bytes) <= XFS_BUF_COUNT(bp));
1865
1866 /*
1867 * The current logged region contains a copy of the
1868 * current di_next_unlinked field. Extract its value
1869 * and copy it to the buffer copy.
1870 */
1871 logged_nextp = (xfs_agino_t *)
1872 ((char *)(item->ri_buf[item_index].i_addr) +
1873 (next_unlinked_offset - reg_buf_offset));
1874 if (unlikely(*logged_nextp == 0)) {
1875 xfs_fs_cmn_err(CE_ALERT, mp,
1876 "bad inode buffer log record (ptr = 0x%p, bp = 0x%p). XFS trying to replay bad (0) inode di_next_unlinked field",
1877 item, bp);
1878 XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
1879 XFS_ERRLEVEL_LOW, mp);
1880 return XFS_ERROR(EFSCORRUPTED);
1881 }
1882
1883 buffer_nextp = (xfs_agino_t *)xfs_buf_offset(bp,
1884 next_unlinked_offset);
87c199c2 1885 *buffer_nextp = *logged_nextp;
1da177e4
LT
1886 }
1887
1888 return 0;
1889}
1890
1891/*
1892 * Perform a 'normal' buffer recovery. Each logged region of the
1893 * buffer should be copied over the corresponding region in the
1894 * given buffer. The bitmap in the buf log format structure indicates
1895 * where to place the logged data.
1896 */
1897/*ARGSUSED*/
1898STATIC void
1899xlog_recover_do_reg_buffer(
9abbc539 1900 struct xfs_mount *mp,
1da177e4
LT
1901 xlog_recover_item_t *item,
1902 xfs_buf_t *bp,
1903 xfs_buf_log_format_t *buf_f)
1904{
1905 int i;
1906 int bit;
1907 int nbits;
1da177e4
LT
1908 unsigned int *data_map = NULL;
1909 unsigned int map_size = 0;
1910 int error;
1911
9abbc539
DC
1912 trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
1913
1da177e4
LT
1914 switch (buf_f->blf_type) {
1915 case XFS_LI_BUF:
1916 data_map = buf_f->blf_data_map;
1917 map_size = buf_f->blf_map_size;
1918 break;
1da177e4
LT
1919 }
1920 bit = 0;
1921 i = 1; /* 0 is the buf format structure */
1922 while (1) {
1923 bit = xfs_next_bit(data_map, map_size, bit);
1924 if (bit == -1)
1925 break;
1926 nbits = xfs_contig_bits(data_map, map_size, bit);
1927 ASSERT(nbits > 0);
4b80916b 1928 ASSERT(item->ri_buf[i].i_addr != NULL);
1da177e4
LT
1929 ASSERT(item->ri_buf[i].i_len % XFS_BLI_CHUNK == 0);
1930 ASSERT(XFS_BUF_COUNT(bp) >=
1931 ((uint)bit << XFS_BLI_SHIFT)+(nbits<<XFS_BLI_SHIFT));
1932
1933 /*
1934 * Do a sanity check if this is a dquot buffer. Just checking
1935 * the first dquot in the buffer should do. XXXThis is
1936 * probably a good thing to do for other buf types also.
1937 */
1938 error = 0;
c8ad20ff
NS
1939 if (buf_f->blf_flags &
1940 (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
0c5e1ce8
CH
1941 if (item->ri_buf[i].i_addr == NULL) {
1942 cmn_err(CE_ALERT,
1943 "XFS: NULL dquot in %s.", __func__);
1944 goto next;
1945 }
8ec6dba2 1946 if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
0c5e1ce8
CH
1947 cmn_err(CE_ALERT,
1948 "XFS: dquot too small (%d) in %s.",
1949 item->ri_buf[i].i_len, __func__);
1950 goto next;
1951 }
1da177e4
LT
1952 error = xfs_qm_dqcheck((xfs_disk_dquot_t *)
1953 item->ri_buf[i].i_addr,
1954 -1, 0, XFS_QMOPT_DOWARN,
1955 "dquot_buf_recover");
0c5e1ce8
CH
1956 if (error)
1957 goto next;
1da177e4 1958 }
0c5e1ce8
CH
1959
1960 memcpy(xfs_buf_offset(bp,
1961 (uint)bit << XFS_BLI_SHIFT), /* dest */
1962 item->ri_buf[i].i_addr, /* source */
1963 nbits<<XFS_BLI_SHIFT); /* length */
1964 next:
1da177e4
LT
1965 i++;
1966 bit += nbits;
1967 }
1968
1969 /* Shouldn't be any more regions */
1970 ASSERT(i == item->ri_total);
1971}
1972
1973/*
1974 * Do some primitive error checking on ondisk dquot data structures.
1975 */
1976int
1977xfs_qm_dqcheck(
1978 xfs_disk_dquot_t *ddq,
1979 xfs_dqid_t id,
1980 uint type, /* used only when IO_dorepair is true */
1981 uint flags,
1982 char *str)
1983{
1984 xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
1985 int errs = 0;
1986
1987 /*
1988 * We can encounter an uninitialized dquot buffer for 2 reasons:
1989 * 1. If we crash while deleting the quotainode(s), and those blks got
1990 * used for user data. This is because we take the path of regular
1991 * file deletion; however, the size field of quotainodes is never
1992 * updated, so all the tricks that we play in itruncate_finish
1993 * don't quite matter.
1994 *
1995 * 2. We don't play the quota buffers when there's a quotaoff logitem.
1996 * But the allocation will be replayed so we'll end up with an
1997 * uninitialized quota block.
1998 *
1999 * This is all fine; things are still consistent, and we haven't lost
2000 * any quota information. Just don't complain about bad dquot blks.
2001 */
1149d96a 2002 if (be16_to_cpu(ddq->d_magic) != XFS_DQUOT_MAGIC) {
1da177e4
LT
2003 if (flags & XFS_QMOPT_DOWARN)
2004 cmn_err(CE_ALERT,
2005 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
1149d96a 2006 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
1da177e4
LT
2007 errs++;
2008 }
1149d96a 2009 if (ddq->d_version != XFS_DQUOT_VERSION) {
1da177e4
LT
2010 if (flags & XFS_QMOPT_DOWARN)
2011 cmn_err(CE_ALERT,
2012 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
1149d96a 2013 str, id, ddq->d_version, XFS_DQUOT_VERSION);
1da177e4
LT
2014 errs++;
2015 }
2016
1149d96a
CH
2017 if (ddq->d_flags != XFS_DQ_USER &&
2018 ddq->d_flags != XFS_DQ_PROJ &&
2019 ddq->d_flags != XFS_DQ_GROUP) {
1da177e4
LT
2020 if (flags & XFS_QMOPT_DOWARN)
2021 cmn_err(CE_ALERT,
2022 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
1149d96a 2023 str, id, ddq->d_flags);
1da177e4
LT
2024 errs++;
2025 }
2026
1149d96a 2027 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
1da177e4
LT
2028 if (flags & XFS_QMOPT_DOWARN)
2029 cmn_err(CE_ALERT,
2030 "%s : ondisk-dquot 0x%p, ID mismatch: "
2031 "0x%x expected, found id 0x%x",
1149d96a 2032 str, ddq, id, be32_to_cpu(ddq->d_id));
1da177e4
LT
2033 errs++;
2034 }
2035
2036 if (!errs && ddq->d_id) {
1149d96a
CH
2037 if (ddq->d_blk_softlimit &&
2038 be64_to_cpu(ddq->d_bcount) >=
2039 be64_to_cpu(ddq->d_blk_softlimit)) {
1da177e4
LT
2040 if (!ddq->d_btimer) {
2041 if (flags & XFS_QMOPT_DOWARN)
2042 cmn_err(CE_ALERT,
2043 "%s : Dquot ID 0x%x (0x%p) "
2044 "BLK TIMER NOT STARTED",
1149d96a 2045 str, (int)be32_to_cpu(ddq->d_id), ddq);
1da177e4
LT
2046 errs++;
2047 }
2048 }
1149d96a
CH
2049 if (ddq->d_ino_softlimit &&
2050 be64_to_cpu(ddq->d_icount) >=
2051 be64_to_cpu(ddq->d_ino_softlimit)) {
1da177e4
LT
2052 if (!ddq->d_itimer) {
2053 if (flags & XFS_QMOPT_DOWARN)
2054 cmn_err(CE_ALERT,
2055 "%s : Dquot ID 0x%x (0x%p) "
2056 "INODE TIMER NOT STARTED",
1149d96a 2057 str, (int)be32_to_cpu(ddq->d_id), ddq);
1da177e4
LT
2058 errs++;
2059 }
2060 }
1149d96a
CH
2061 if (ddq->d_rtb_softlimit &&
2062 be64_to_cpu(ddq->d_rtbcount) >=
2063 be64_to_cpu(ddq->d_rtb_softlimit)) {
1da177e4
LT
2064 if (!ddq->d_rtbtimer) {
2065 if (flags & XFS_QMOPT_DOWARN)
2066 cmn_err(CE_ALERT,
2067 "%s : Dquot ID 0x%x (0x%p) "
2068 "RTBLK TIMER NOT STARTED",
1149d96a 2069 str, (int)be32_to_cpu(ddq->d_id), ddq);
1da177e4
LT
2070 errs++;
2071 }
2072 }
2073 }
2074
2075 if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
2076 return errs;
2077
2078 if (flags & XFS_QMOPT_DOWARN)
2079 cmn_err(CE_NOTE, "Re-initializing dquot ID 0x%x", id);
2080
2081 /*
2082 * Typically, a repair is only requested by quotacheck.
2083 */
2084 ASSERT(id != -1);
2085 ASSERT(flags & XFS_QMOPT_DQREPAIR);
2086 memset(d, 0, sizeof(xfs_dqblk_t));
1149d96a
CH
2087
2088 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
2089 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
2090 d->dd_diskdq.d_flags = type;
2091 d->dd_diskdq.d_id = cpu_to_be32(id);
1da177e4
LT
2092
2093 return errs;
2094}
2095
2096/*
2097 * Perform a dquot buffer recovery.
2098 * Simple algorithm: if we have found a QUOTAOFF logitem of the same type
2099 * (ie. USR or GRP), then just toss this buffer away; don't recover it.
2100 * Else, treat it as a regular buffer and do recovery.
2101 */
2102STATIC void
2103xlog_recover_do_dquot_buffer(
2104 xfs_mount_t *mp,
2105 xlog_t *log,
2106 xlog_recover_item_t *item,
2107 xfs_buf_t *bp,
2108 xfs_buf_log_format_t *buf_f)
2109{
2110 uint type;
2111
9abbc539
DC
2112 trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
2113
1da177e4
LT
2114 /*
2115 * Filesystems are required to send in quota flags at mount time.
2116 */
2117 if (mp->m_qflags == 0) {
2118 return;
2119 }
2120
2121 type = 0;
2122 if (buf_f->blf_flags & XFS_BLI_UDQUOT_BUF)
2123 type |= XFS_DQ_USER;
c8ad20ff
NS
2124 if (buf_f->blf_flags & XFS_BLI_PDQUOT_BUF)
2125 type |= XFS_DQ_PROJ;
1da177e4
LT
2126 if (buf_f->blf_flags & XFS_BLI_GDQUOT_BUF)
2127 type |= XFS_DQ_GROUP;
2128 /*
2129 * This type of quotas was turned off, so ignore this buffer
2130 */
2131 if (log->l_quotaoffs_flag & type)
2132 return;
2133
9abbc539 2134 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
1da177e4
LT
2135}
2136
2137/*
2138 * This routine replays a modification made to a buffer at runtime.
2139 * There are actually two types of buffer, regular and inode, which
2140 * are handled differently. Inode buffers are handled differently
2141 * in that we only recover a specific set of data from them, namely
2142 * the inode di_next_unlinked fields. This is because all other inode
2143 * data is actually logged via inode records and any data we replay
2144 * here which overlaps that may be stale.
2145 *
2146 * When meta-data buffers are freed at run time we log a buffer item
2147 * with the XFS_BLI_CANCEL bit set to indicate that previous copies
2148 * of the buffer in the log should not be replayed at recovery time.
2149 * This is so that if the blocks covered by the buffer are reused for
2150 * file data before we crash we don't end up replaying old, freed
2151 * meta-data into a user's file.
2152 *
2153 * To handle the cancellation of buffer log items, we make two passes
2154 * over the log during recovery. During the first we build a table of
2155 * those buffers which have been cancelled, and during the second we
2156 * only replay those buffers which do not have corresponding cancel
2157 * records in the table. See xlog_recover_do_buffer_pass[1,2] above
2158 * for more details on the implementation of the table of cancel records.
2159 */
2160STATIC int
2161xlog_recover_do_buffer_trans(
2162 xlog_t *log,
2163 xlog_recover_item_t *item,
2164 int pass)
2165{
2166 xfs_buf_log_format_t *buf_f;
1da177e4
LT
2167 xfs_mount_t *mp;
2168 xfs_buf_t *bp;
2169 int error;
2170 int cancel;
2171 xfs_daddr_t blkno;
2172 int len;
2173 ushort flags;
6ad112bf 2174 uint buf_flags;
1da177e4
LT
2175
2176 buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
2177
2178 if (pass == XLOG_RECOVER_PASS1) {
2179 /*
2180 * In this pass we're only looking for buf items
2181 * with the XFS_BLI_CANCEL bit set.
2182 */
2183 xlog_recover_do_buffer_pass1(log, buf_f);
2184 return 0;
2185 } else {
2186 /*
2187 * In this pass we want to recover all the buffers
2188 * which have not been cancelled and are not
2189 * cancellation buffers themselves. The routine
2190 * we call here will tell us whether or not to
2191 * continue with the replay of this buffer.
2192 */
2193 cancel = xlog_recover_do_buffer_pass2(log, buf_f);
2194 if (cancel) {
9abbc539 2195 trace_xfs_log_recover_buf_cancel(log, buf_f);
1da177e4
LT
2196 return 0;
2197 }
2198 }
9abbc539 2199 trace_xfs_log_recover_buf_recover(log, buf_f);
1da177e4
LT
2200 switch (buf_f->blf_type) {
2201 case XFS_LI_BUF:
2202 blkno = buf_f->blf_blkno;
2203 len = buf_f->blf_len;
2204 flags = buf_f->blf_flags;
2205 break;
1da177e4
LT
2206 default:
2207 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
fc1f8c1c
NS
2208 "xfs_log_recover: unknown buffer type 0x%x, logdev %s",
2209 buf_f->blf_type, log->l_mp->m_logname ?
2210 log->l_mp->m_logname : "internal");
1da177e4
LT
2211 XFS_ERROR_REPORT("xlog_recover_do_buffer_trans",
2212 XFS_ERRLEVEL_LOW, log->l_mp);
2213 return XFS_ERROR(EFSCORRUPTED);
2214 }
2215
2216 mp = log->l_mp;
0cadda1c 2217 buf_flags = XBF_LOCK;
6ad112bf 2218 if (!(flags & XFS_BLI_INODE_BUF))
0cadda1c 2219 buf_flags |= XBF_MAPPED;
6ad112bf
CH
2220
2221 bp = xfs_buf_read(mp->m_ddev_targp, blkno, len, buf_flags);
1da177e4
LT
2222 if (XFS_BUF_ISERROR(bp)) {
2223 xfs_ioerror_alert("xlog_recover_do..(read#1)", log->l_mp,
2224 bp, blkno);
2225 error = XFS_BUF_GETERROR(bp);
2226 xfs_buf_relse(bp);
2227 return error;
2228 }
2229
2230 error = 0;
2231 if (flags & XFS_BLI_INODE_BUF) {
2232 error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
c8ad20ff
NS
2233 } else if (flags &
2234 (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
1da177e4
LT
2235 xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
2236 } else {
9abbc539 2237 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
1da177e4
LT
2238 }
2239 if (error)
2240 return XFS_ERROR(error);
2241
2242 /*
2243 * Perform delayed write on the buffer. Asynchronous writes will be
2244 * slower when taking into account all the buffers to be flushed.
2245 *
2246 * Also make sure that only inode buffers with good sizes stay in
2247 * the buffer cache. The kernel moves inodes in buffers of 1 block
2248 * or XFS_INODE_CLUSTER_SIZE bytes, whichever is bigger. The inode
2249 * buffers in the log can be a different size if the log was generated
2250 * by an older kernel using unclustered inode buffers or a newer kernel
2251 * running with a different inode cluster size. Regardless, if the
2252 * the inode buffer size isn't MAX(blocksize, XFS_INODE_CLUSTER_SIZE)
2253 * for *our* value of XFS_INODE_CLUSTER_SIZE, then we need to keep
2254 * the buffer out of the buffer cache so that the buffer won't
2255 * overlap with future reads of those inodes.
2256 */
2257 if (XFS_DINODE_MAGIC ==
b53e675d 2258 be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
1da177e4
LT
2259 (XFS_BUF_COUNT(bp) != MAX(log->l_mp->m_sb.sb_blocksize,
2260 (__uint32_t)XFS_INODE_CLUSTER_SIZE(log->l_mp)))) {
2261 XFS_BUF_STALE(bp);
2262 error = xfs_bwrite(mp, bp);
2263 } else {
15ac08a8
CH
2264 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2265 bp->b_mount = mp;
1da177e4
LT
2266 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2267 xfs_bdwrite(mp, bp);
2268 }
2269
2270 return (error);
2271}
2272
2273STATIC int
2274xlog_recover_do_inode_trans(
2275 xlog_t *log,
2276 xlog_recover_item_t *item,
2277 int pass)
2278{
2279 xfs_inode_log_format_t *in_f;
2280 xfs_mount_t *mp;
2281 xfs_buf_t *bp;
1da177e4
LT
2282 xfs_dinode_t *dip;
2283 xfs_ino_t ino;
2284 int len;
2285 xfs_caddr_t src;
2286 xfs_caddr_t dest;
2287 int error;
2288 int attr_index;
2289 uint fields;
347d1c01 2290 xfs_icdinode_t *dicp;
6d192a9b 2291 int need_free = 0;
1da177e4
LT
2292
2293 if (pass == XLOG_RECOVER_PASS1) {
2294 return 0;
2295 }
2296
6d192a9b
TS
2297 if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
2298 in_f = (xfs_inode_log_format_t *)item->ri_buf[0].i_addr;
2299 } else {
2300 in_f = (xfs_inode_log_format_t *)kmem_alloc(
2301 sizeof(xfs_inode_log_format_t), KM_SLEEP);
2302 need_free = 1;
2303 error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
2304 if (error)
2305 goto error;
2306 }
1da177e4
LT
2307 ino = in_f->ilf_ino;
2308 mp = log->l_mp;
1da177e4
LT
2309
2310 /*
2311 * Inode buffers can be freed, look out for it,
2312 * and do not replay the inode.
2313 */
a1941895
CH
2314 if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
2315 in_f->ilf_len, 0)) {
6d192a9b 2316 error = 0;
9abbc539 2317 trace_xfs_log_recover_inode_cancel(log, in_f);
6d192a9b
TS
2318 goto error;
2319 }
9abbc539 2320 trace_xfs_log_recover_inode_recover(log, in_f);
1da177e4 2321
6ad112bf 2322 bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len,
0cadda1c 2323 XBF_LOCK);
1da177e4
LT
2324 if (XFS_BUF_ISERROR(bp)) {
2325 xfs_ioerror_alert("xlog_recover_do..(read#2)", mp,
a1941895 2326 bp, in_f->ilf_blkno);
1da177e4
LT
2327 error = XFS_BUF_GETERROR(bp);
2328 xfs_buf_relse(bp);
6d192a9b 2329 goto error;
1da177e4
LT
2330 }
2331 error = 0;
2332 ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
a1941895 2333 dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
1da177e4
LT
2334
2335 /*
2336 * Make sure the place we're flushing out to really looks
2337 * like an inode!
2338 */
81591fe2 2339 if (unlikely(be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC)) {
1da177e4
LT
2340 xfs_buf_relse(bp);
2341 xfs_fs_cmn_err(CE_ALERT, mp,
2342 "xfs_inode_recover: Bad inode magic number, dino ptr = 0x%p, dino bp = 0x%p, ino = %Ld",
2343 dip, bp, ino);
2344 XFS_ERROR_REPORT("xlog_recover_do_inode_trans(1)",
2345 XFS_ERRLEVEL_LOW, mp);
6d192a9b
TS
2346 error = EFSCORRUPTED;
2347 goto error;
1da177e4 2348 }
347d1c01 2349 dicp = (xfs_icdinode_t *)(item->ri_buf[1].i_addr);
1da177e4
LT
2350 if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
2351 xfs_buf_relse(bp);
2352 xfs_fs_cmn_err(CE_ALERT, mp,
2353 "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, ino %Ld",
2354 item, ino);
2355 XFS_ERROR_REPORT("xlog_recover_do_inode_trans(2)",
2356 XFS_ERRLEVEL_LOW, mp);
6d192a9b
TS
2357 error = EFSCORRUPTED;
2358 goto error;
1da177e4
LT
2359 }
2360
2361 /* Skip replay when the on disk inode is newer than the log one */
81591fe2 2362 if (dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
1da177e4
LT
2363 /*
2364 * Deal with the wrap case, DI_MAX_FLUSH is less
2365 * than smaller numbers
2366 */
81591fe2 2367 if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
347d1c01 2368 dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
1da177e4
LT
2369 /* do nothing */
2370 } else {
2371 xfs_buf_relse(bp);
9abbc539 2372 trace_xfs_log_recover_inode_skip(log, in_f);
6d192a9b
TS
2373 error = 0;
2374 goto error;
1da177e4
LT
2375 }
2376 }
2377 /* Take the opportunity to reset the flush iteration count */
2378 dicp->di_flushiter = 0;
2379
2380 if (unlikely((dicp->di_mode & S_IFMT) == S_IFREG)) {
2381 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2382 (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
2383 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(3)",
2384 XFS_ERRLEVEL_LOW, mp, dicp);
2385 xfs_buf_relse(bp);
2386 xfs_fs_cmn_err(CE_ALERT, mp,
2387 "xfs_inode_recover: Bad regular inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2388 item, dip, bp, ino);
6d192a9b
TS
2389 error = EFSCORRUPTED;
2390 goto error;
1da177e4
LT
2391 }
2392 } else if (unlikely((dicp->di_mode & S_IFMT) == S_IFDIR)) {
2393 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2394 (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
2395 (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
2396 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(4)",
2397 XFS_ERRLEVEL_LOW, mp, dicp);
2398 xfs_buf_relse(bp);
2399 xfs_fs_cmn_err(CE_ALERT, mp,
2400 "xfs_inode_recover: Bad dir inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2401 item, dip, bp, ino);
6d192a9b
TS
2402 error = EFSCORRUPTED;
2403 goto error;
1da177e4
LT
2404 }
2405 }
2406 if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
2407 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(5)",
2408 XFS_ERRLEVEL_LOW, mp, dicp);
2409 xfs_buf_relse(bp);
2410 xfs_fs_cmn_err(CE_ALERT, mp,
2411 "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
2412 item, dip, bp, ino,
2413 dicp->di_nextents + dicp->di_anextents,
2414 dicp->di_nblocks);
6d192a9b
TS
2415 error = EFSCORRUPTED;
2416 goto error;
1da177e4
LT
2417 }
2418 if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
2419 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(6)",
2420 XFS_ERRLEVEL_LOW, mp, dicp);
2421 xfs_buf_relse(bp);
2422 xfs_fs_cmn_err(CE_ALERT, mp,
2423 "xfs_inode_recover: Bad inode log rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, forkoff 0x%x",
2424 item, dip, bp, ino, dicp->di_forkoff);
6d192a9b
TS
2425 error = EFSCORRUPTED;
2426 goto error;
1da177e4 2427 }
81591fe2 2428 if (unlikely(item->ri_buf[1].i_len > sizeof(struct xfs_icdinode))) {
1da177e4
LT
2429 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(7)",
2430 XFS_ERRLEVEL_LOW, mp, dicp);
2431 xfs_buf_relse(bp);
2432 xfs_fs_cmn_err(CE_ALERT, mp,
2433 "xfs_inode_recover: Bad inode log record length %d, rec ptr 0x%p",
2434 item->ri_buf[1].i_len, item);
6d192a9b
TS
2435 error = EFSCORRUPTED;
2436 goto error;
1da177e4
LT
2437 }
2438
2439 /* The core is in in-core format */
81591fe2 2440 xfs_dinode_to_disk(dip, (xfs_icdinode_t *)item->ri_buf[1].i_addr);
1da177e4
LT
2441
2442 /* the rest is in on-disk format */
81591fe2
CH
2443 if (item->ri_buf[1].i_len > sizeof(struct xfs_icdinode)) {
2444 memcpy((xfs_caddr_t) dip + sizeof(struct xfs_icdinode),
2445 item->ri_buf[1].i_addr + sizeof(struct xfs_icdinode),
2446 item->ri_buf[1].i_len - sizeof(struct xfs_icdinode));
1da177e4
LT
2447 }
2448
2449 fields = in_f->ilf_fields;
2450 switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
2451 case XFS_ILOG_DEV:
81591fe2 2452 xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
1da177e4
LT
2453 break;
2454 case XFS_ILOG_UUID:
81591fe2
CH
2455 memcpy(XFS_DFORK_DPTR(dip),
2456 &in_f->ilf_u.ilfu_uuid,
2457 sizeof(uuid_t));
1da177e4
LT
2458 break;
2459 }
2460
2461 if (in_f->ilf_size == 2)
2462 goto write_inode_buffer;
2463 len = item->ri_buf[2].i_len;
2464 src = item->ri_buf[2].i_addr;
2465 ASSERT(in_f->ilf_size <= 4);
2466 ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
2467 ASSERT(!(fields & XFS_ILOG_DFORK) ||
2468 (len == in_f->ilf_dsize));
2469
2470 switch (fields & XFS_ILOG_DFORK) {
2471 case XFS_ILOG_DDATA:
2472 case XFS_ILOG_DEXT:
81591fe2 2473 memcpy(XFS_DFORK_DPTR(dip), src, len);
1da177e4
LT
2474 break;
2475
2476 case XFS_ILOG_DBROOT:
7cc95a82 2477 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
81591fe2 2478 (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
1da177e4
LT
2479 XFS_DFORK_DSIZE(dip, mp));
2480 break;
2481
2482 default:
2483 /*
2484 * There are no data fork flags set.
2485 */
2486 ASSERT((fields & XFS_ILOG_DFORK) == 0);
2487 break;
2488 }
2489
2490 /*
2491 * If we logged any attribute data, recover it. There may or
2492 * may not have been any other non-core data logged in this
2493 * transaction.
2494 */
2495 if (in_f->ilf_fields & XFS_ILOG_AFORK) {
2496 if (in_f->ilf_fields & XFS_ILOG_DFORK) {
2497 attr_index = 3;
2498 } else {
2499 attr_index = 2;
2500 }
2501 len = item->ri_buf[attr_index].i_len;
2502 src = item->ri_buf[attr_index].i_addr;
2503 ASSERT(len == in_f->ilf_asize);
2504
2505 switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
2506 case XFS_ILOG_ADATA:
2507 case XFS_ILOG_AEXT:
2508 dest = XFS_DFORK_APTR(dip);
2509 ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
2510 memcpy(dest, src, len);
2511 break;
2512
2513 case XFS_ILOG_ABROOT:
2514 dest = XFS_DFORK_APTR(dip);
7cc95a82
CH
2515 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
2516 len, (xfs_bmdr_block_t*)dest,
1da177e4
LT
2517 XFS_DFORK_ASIZE(dip, mp));
2518 break;
2519
2520 default:
2521 xlog_warn("XFS: xlog_recover_do_inode_trans: Invalid flag");
2522 ASSERT(0);
2523 xfs_buf_relse(bp);
6d192a9b
TS
2524 error = EIO;
2525 goto error;
1da177e4
LT
2526 }
2527 }
2528
2529write_inode_buffer:
dd0bbad8
CH
2530 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2531 bp->b_mount = mp;
2532 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2533 xfs_bdwrite(mp, bp);
6d192a9b
TS
2534error:
2535 if (need_free)
f0e2d93c 2536 kmem_free(in_f);
6d192a9b 2537 return XFS_ERROR(error);
1da177e4
LT
2538}
2539
2540/*
2541 * Recover QUOTAOFF records. We simply make a note of it in the xlog_t
2542 * structure, so that we know not to do any dquot item or dquot buffer recovery,
2543 * of that type.
2544 */
2545STATIC int
2546xlog_recover_do_quotaoff_trans(
2547 xlog_t *log,
2548 xlog_recover_item_t *item,
2549 int pass)
2550{
2551 xfs_qoff_logformat_t *qoff_f;
2552
2553 if (pass == XLOG_RECOVER_PASS2) {
2554 return (0);
2555 }
2556
2557 qoff_f = (xfs_qoff_logformat_t *)item->ri_buf[0].i_addr;
2558 ASSERT(qoff_f);
2559
2560 /*
2561 * The logitem format's flag tells us if this was user quotaoff,
77a7cce4 2562 * group/project quotaoff or both.
1da177e4
LT
2563 */
2564 if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
2565 log->l_quotaoffs_flag |= XFS_DQ_USER;
77a7cce4
NS
2566 if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
2567 log->l_quotaoffs_flag |= XFS_DQ_PROJ;
1da177e4
LT
2568 if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
2569 log->l_quotaoffs_flag |= XFS_DQ_GROUP;
2570
2571 return (0);
2572}
2573
2574/*
2575 * Recover a dquot record
2576 */
2577STATIC int
2578xlog_recover_do_dquot_trans(
2579 xlog_t *log,
2580 xlog_recover_item_t *item,
2581 int pass)
2582{
2583 xfs_mount_t *mp;
2584 xfs_buf_t *bp;
2585 struct xfs_disk_dquot *ddq, *recddq;
2586 int error;
2587 xfs_dq_logformat_t *dq_f;
2588 uint type;
2589
2590 if (pass == XLOG_RECOVER_PASS1) {
2591 return 0;
2592 }
2593 mp = log->l_mp;
2594
2595 /*
2596 * Filesystems are required to send in quota flags at mount time.
2597 */
2598 if (mp->m_qflags == 0)
2599 return (0);
2600
2601 recddq = (xfs_disk_dquot_t *)item->ri_buf[1].i_addr;
0c5e1ce8
CH
2602
2603 if (item->ri_buf[1].i_addr == NULL) {
2604 cmn_err(CE_ALERT,
2605 "XFS: NULL dquot in %s.", __func__);
2606 return XFS_ERROR(EIO);
2607 }
8ec6dba2 2608 if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
0c5e1ce8
CH
2609 cmn_err(CE_ALERT,
2610 "XFS: dquot too small (%d) in %s.",
2611 item->ri_buf[1].i_len, __func__);
2612 return XFS_ERROR(EIO);
2613 }
2614
1da177e4
LT
2615 /*
2616 * This type of quotas was turned off, so ignore this record.
2617 */
b53e675d 2618 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
1da177e4
LT
2619 ASSERT(type);
2620 if (log->l_quotaoffs_flag & type)
2621 return (0);
2622
2623 /*
2624 * At this point we know that quota was _not_ turned off.
2625 * Since the mount flags are not indicating to us otherwise, this
2626 * must mean that quota is on, and the dquot needs to be replayed.
2627 * Remember that we may not have fully recovered the superblock yet,
2628 * so we can't do the usual trick of looking at the SB quota bits.
2629 *
2630 * The other possibility, of course, is that the quota subsystem was
2631 * removed since the last mount - ENOSYS.
2632 */
2633 dq_f = (xfs_dq_logformat_t *)item->ri_buf[0].i_addr;
2634 ASSERT(dq_f);
2635 if ((error = xfs_qm_dqcheck(recddq,
2636 dq_f->qlf_id,
2637 0, XFS_QMOPT_DOWARN,
2638 "xlog_recover_do_dquot_trans (log copy)"))) {
2639 return XFS_ERROR(EIO);
2640 }
2641 ASSERT(dq_f->qlf_len == 1);
2642
2643 error = xfs_read_buf(mp, mp->m_ddev_targp,
2644 dq_f->qlf_blkno,
2645 XFS_FSB_TO_BB(mp, dq_f->qlf_len),
2646 0, &bp);
2647 if (error) {
2648 xfs_ioerror_alert("xlog_recover_do..(read#3)", mp,
2649 bp, dq_f->qlf_blkno);
2650 return error;
2651 }
2652 ASSERT(bp);
2653 ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
2654
2655 /*
2656 * At least the magic num portion should be on disk because this
2657 * was among a chunk of dquots created earlier, and we did some
2658 * minimal initialization then.
2659 */
2660 if (xfs_qm_dqcheck(ddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
2661 "xlog_recover_do_dquot_trans")) {
2662 xfs_buf_relse(bp);
2663 return XFS_ERROR(EIO);
2664 }
2665
2666 memcpy(ddq, recddq, item->ri_buf[1].i_len);
2667
2668 ASSERT(dq_f->qlf_size == 2);
15ac08a8
CH
2669 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2670 bp->b_mount = mp;
1da177e4
LT
2671 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2672 xfs_bdwrite(mp, bp);
2673
2674 return (0);
2675}
2676
2677/*
2678 * This routine is called to create an in-core extent free intent
2679 * item from the efi format structure which was logged on disk.
2680 * It allocates an in-core efi, copies the extents from the format
2681 * structure into it, and adds the efi to the AIL with the given
2682 * LSN.
2683 */
6d192a9b 2684STATIC int
1da177e4
LT
2685xlog_recover_do_efi_trans(
2686 xlog_t *log,
2687 xlog_recover_item_t *item,
2688 xfs_lsn_t lsn,
2689 int pass)
2690{
6d192a9b 2691 int error;
1da177e4
LT
2692 xfs_mount_t *mp;
2693 xfs_efi_log_item_t *efip;
2694 xfs_efi_log_format_t *efi_formatp;
1da177e4
LT
2695
2696 if (pass == XLOG_RECOVER_PASS1) {
6d192a9b 2697 return 0;
1da177e4
LT
2698 }
2699
2700 efi_formatp = (xfs_efi_log_format_t *)item->ri_buf[0].i_addr;
1da177e4
LT
2701
2702 mp = log->l_mp;
2703 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
6d192a9b
TS
2704 if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
2705 &(efip->efi_format)))) {
2706 xfs_efi_item_free(efip);
2707 return error;
2708 }
1da177e4
LT
2709 efip->efi_next_extent = efi_formatp->efi_nextents;
2710 efip->efi_flags |= XFS_EFI_COMMITTED;
2711
a9c21c1b 2712 spin_lock(&log->l_ailp->xa_lock);
1da177e4 2713 /*
783a2f65 2714 * xfs_trans_ail_update() drops the AIL lock.
1da177e4 2715 */
783a2f65 2716 xfs_trans_ail_update(log->l_ailp, (xfs_log_item_t *)efip, lsn);
6d192a9b 2717 return 0;
1da177e4
LT
2718}
2719
2720
2721/*
2722 * This routine is called when an efd format structure is found in
2723 * a committed transaction in the log. It's purpose is to cancel
2724 * the corresponding efi if it was still in the log. To do this
2725 * it searches the AIL for the efi with an id equal to that in the
2726 * efd format structure. If we find it, we remove the efi from the
2727 * AIL and free it.
2728 */
2729STATIC void
2730xlog_recover_do_efd_trans(
2731 xlog_t *log,
2732 xlog_recover_item_t *item,
2733 int pass)
2734{
1da177e4
LT
2735 xfs_efd_log_format_t *efd_formatp;
2736 xfs_efi_log_item_t *efip = NULL;
2737 xfs_log_item_t *lip;
1da177e4 2738 __uint64_t efi_id;
27d8d5fe 2739 struct xfs_ail_cursor cur;
783a2f65 2740 struct xfs_ail *ailp = log->l_ailp;
1da177e4
LT
2741
2742 if (pass == XLOG_RECOVER_PASS1) {
2743 return;
2744 }
2745
2746 efd_formatp = (xfs_efd_log_format_t *)item->ri_buf[0].i_addr;
6d192a9b
TS
2747 ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
2748 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
2749 (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
2750 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
1da177e4
LT
2751 efi_id = efd_formatp->efd_efi_id;
2752
2753 /*
2754 * Search for the efi with the id in the efd format structure
2755 * in the AIL.
2756 */
a9c21c1b
DC
2757 spin_lock(&ailp->xa_lock);
2758 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
1da177e4
LT
2759 while (lip != NULL) {
2760 if (lip->li_type == XFS_LI_EFI) {
2761 efip = (xfs_efi_log_item_t *)lip;
2762 if (efip->efi_format.efi_id == efi_id) {
2763 /*
783a2f65 2764 * xfs_trans_ail_delete() drops the
1da177e4
LT
2765 * AIL lock.
2766 */
783a2f65 2767 xfs_trans_ail_delete(ailp, lip);
8ae2c0f6 2768 xfs_efi_item_free(efip);
a9c21c1b 2769 spin_lock(&ailp->xa_lock);
27d8d5fe 2770 break;
1da177e4
LT
2771 }
2772 }
a9c21c1b 2773 lip = xfs_trans_ail_cursor_next(ailp, &cur);
1da177e4 2774 }
a9c21c1b
DC
2775 xfs_trans_ail_cursor_done(ailp, &cur);
2776 spin_unlock(&ailp->xa_lock);
1da177e4
LT
2777}
2778
2779/*
2780 * Perform the transaction
2781 *
2782 * If the transaction modifies a buffer or inode, do it now. Otherwise,
2783 * EFIs and EFDs get queued up by adding entries into the AIL for them.
2784 */
2785STATIC int
2786xlog_recover_do_trans(
2787 xlog_t *log,
2788 xlog_recover_t *trans,
2789 int pass)
2790{
2791 int error = 0;
f0a76953 2792 xlog_recover_item_t *item;
1da177e4 2793
9abbc539 2794 error = xlog_recover_reorder_trans(log, trans, pass);
ff0205e0 2795 if (error)
1da177e4 2796 return error;
ff0205e0 2797
f0a76953 2798 list_for_each_entry(item, &trans->r_itemq, ri_list) {
9abbc539 2799 trace_xfs_log_recover_item_recover(log, trans, item, pass);
ff0205e0
CH
2800 switch (ITEM_TYPE(item)) {
2801 case XFS_LI_BUF:
2802 error = xlog_recover_do_buffer_trans(log, item, pass);
2803 break;
2804 case XFS_LI_INODE:
2805 error = xlog_recover_do_inode_trans(log, item, pass);
2806 break;
2807 case XFS_LI_EFI:
2808 error = xlog_recover_do_efi_trans(log, item,
2809 trans->r_lsn, pass);
2810 break;
2811 case XFS_LI_EFD:
1da177e4 2812 xlog_recover_do_efd_trans(log, item, pass);
ff0205e0
CH
2813 error = 0;
2814 break;
2815 case XFS_LI_DQUOT:
2816 error = xlog_recover_do_dquot_trans(log, item, pass);
2817 break;
2818 case XFS_LI_QUOTAOFF:
2819 error = xlog_recover_do_quotaoff_trans(log, item,
2820 pass);
2821 break;
2822 default:
2823 xlog_warn(
2824 "XFS: invalid item type (%d) xlog_recover_do_trans", ITEM_TYPE(item));
1da177e4
LT
2825 ASSERT(0);
2826 error = XFS_ERROR(EIO);
2827 break;
2828 }
ff0205e0
CH
2829
2830 if (error)
2831 return error;
f0a76953 2832 }
1da177e4 2833
ff0205e0 2834 return 0;
1da177e4
LT
2835}
2836
2837/*
2838 * Free up any resources allocated by the transaction
2839 *
2840 * Remember that EFIs, EFDs, and IUNLINKs are handled later.
2841 */
2842STATIC void
2843xlog_recover_free_trans(
2844 xlog_recover_t *trans)
2845{
f0a76953 2846 xlog_recover_item_t *item, *n;
1da177e4
LT
2847 int i;
2848
f0a76953
DC
2849 list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
2850 /* Free the regions in the item. */
2851 list_del(&item->ri_list);
2852 for (i = 0; i < item->ri_cnt; i++)
2853 kmem_free(item->ri_buf[i].i_addr);
1da177e4 2854 /* Free the item itself */
f0a76953
DC
2855 kmem_free(item->ri_buf);
2856 kmem_free(item);
2857 }
1da177e4 2858 /* Free the transaction recover structure */
f0e2d93c 2859 kmem_free(trans);
1da177e4
LT
2860}
2861
2862STATIC int
2863xlog_recover_commit_trans(
2864 xlog_t *log,
1da177e4
LT
2865 xlog_recover_t *trans,
2866 int pass)
2867{
2868 int error;
2869
f0a76953 2870 hlist_del(&trans->r_list);
1da177e4
LT
2871 if ((error = xlog_recover_do_trans(log, trans, pass)))
2872 return error;
2873 xlog_recover_free_trans(trans); /* no error */
2874 return 0;
2875}
2876
2877STATIC int
2878xlog_recover_unmount_trans(
2879 xlog_recover_t *trans)
2880{
2881 /* Do nothing now */
2882 xlog_warn("XFS: xlog_recover_unmount_trans: Unmount LR");
2883 return 0;
2884}
2885
2886/*
2887 * There are two valid states of the r_state field. 0 indicates that the
2888 * transaction structure is in a normal state. We have either seen the
2889 * start of the transaction or the last operation we added was not a partial
2890 * operation. If the last operation we added to the transaction was a
2891 * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
2892 *
2893 * NOTE: skip LRs with 0 data length.
2894 */
2895STATIC int
2896xlog_recover_process_data(
2897 xlog_t *log,
f0a76953 2898 struct hlist_head rhash[],
1da177e4
LT
2899 xlog_rec_header_t *rhead,
2900 xfs_caddr_t dp,
2901 int pass)
2902{
2903 xfs_caddr_t lp;
2904 int num_logops;
2905 xlog_op_header_t *ohead;
2906 xlog_recover_t *trans;
2907 xlog_tid_t tid;
2908 int error;
2909 unsigned long hash;
2910 uint flags;
2911
b53e675d
CH
2912 lp = dp + be32_to_cpu(rhead->h_len);
2913 num_logops = be32_to_cpu(rhead->h_num_logops);
1da177e4
LT
2914
2915 /* check the log format matches our own - else we can't recover */
2916 if (xlog_header_check_recover(log->l_mp, rhead))
2917 return (XFS_ERROR(EIO));
2918
2919 while ((dp < lp) && num_logops) {
2920 ASSERT(dp + sizeof(xlog_op_header_t) <= lp);
2921 ohead = (xlog_op_header_t *)dp;
2922 dp += sizeof(xlog_op_header_t);
2923 if (ohead->oh_clientid != XFS_TRANSACTION &&
2924 ohead->oh_clientid != XFS_LOG) {
2925 xlog_warn(
2926 "XFS: xlog_recover_process_data: bad clientid");
2927 ASSERT(0);
2928 return (XFS_ERROR(EIO));
2929 }
67fcb7bf 2930 tid = be32_to_cpu(ohead->oh_tid);
1da177e4 2931 hash = XLOG_RHASH(tid);
f0a76953 2932 trans = xlog_recover_find_tid(&rhash[hash], tid);
1da177e4
LT
2933 if (trans == NULL) { /* not found; add new tid */
2934 if (ohead->oh_flags & XLOG_START_TRANS)
2935 xlog_recover_new_tid(&rhash[hash], tid,
b53e675d 2936 be64_to_cpu(rhead->h_lsn));
1da177e4 2937 } else {
9742bb93
LM
2938 if (dp + be32_to_cpu(ohead->oh_len) > lp) {
2939 xlog_warn(
2940 "XFS: xlog_recover_process_data: bad length");
2941 WARN_ON(1);
2942 return (XFS_ERROR(EIO));
2943 }
1da177e4
LT
2944 flags = ohead->oh_flags & ~XLOG_END_TRANS;
2945 if (flags & XLOG_WAS_CONT_TRANS)
2946 flags &= ~XLOG_CONTINUE_TRANS;
2947 switch (flags) {
2948 case XLOG_COMMIT_TRANS:
2949 error = xlog_recover_commit_trans(log,
f0a76953 2950 trans, pass);
1da177e4
LT
2951 break;
2952 case XLOG_UNMOUNT_TRANS:
2953 error = xlog_recover_unmount_trans(trans);
2954 break;
2955 case XLOG_WAS_CONT_TRANS:
9abbc539
DC
2956 error = xlog_recover_add_to_cont_trans(log,
2957 trans, dp,
2958 be32_to_cpu(ohead->oh_len));
1da177e4
LT
2959 break;
2960 case XLOG_START_TRANS:
2961 xlog_warn(
2962 "XFS: xlog_recover_process_data: bad transaction");
2963 ASSERT(0);
2964 error = XFS_ERROR(EIO);
2965 break;
2966 case 0:
2967 case XLOG_CONTINUE_TRANS:
9abbc539 2968 error = xlog_recover_add_to_trans(log, trans,
67fcb7bf 2969 dp, be32_to_cpu(ohead->oh_len));
1da177e4
LT
2970 break;
2971 default:
2972 xlog_warn(
2973 "XFS: xlog_recover_process_data: bad flag");
2974 ASSERT(0);
2975 error = XFS_ERROR(EIO);
2976 break;
2977 }
2978 if (error)
2979 return error;
2980 }
67fcb7bf 2981 dp += be32_to_cpu(ohead->oh_len);
1da177e4
LT
2982 num_logops--;
2983 }
2984 return 0;
2985}
2986
2987/*
2988 * Process an extent free intent item that was recovered from
2989 * the log. We need to free the extents that it describes.
2990 */
3c1e2bbe 2991STATIC int
1da177e4
LT
2992xlog_recover_process_efi(
2993 xfs_mount_t *mp,
2994 xfs_efi_log_item_t *efip)
2995{
2996 xfs_efd_log_item_t *efdp;
2997 xfs_trans_t *tp;
2998 int i;
3c1e2bbe 2999 int error = 0;
1da177e4
LT
3000 xfs_extent_t *extp;
3001 xfs_fsblock_t startblock_fsb;
3002
3003 ASSERT(!(efip->efi_flags & XFS_EFI_RECOVERED));
3004
3005 /*
3006 * First check the validity of the extents described by the
3007 * EFI. If any are bad, then assume that all are bad and
3008 * just toss the EFI.
3009 */
3010 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3011 extp = &(efip->efi_format.efi_extents[i]);
3012 startblock_fsb = XFS_BB_TO_FSB(mp,
3013 XFS_FSB_TO_DADDR(mp, extp->ext_start));
3014 if ((startblock_fsb == 0) ||
3015 (extp->ext_len == 0) ||
3016 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
3017 (extp->ext_len >= mp->m_sb.sb_agblocks)) {
3018 /*
3019 * This will pull the EFI from the AIL and
3020 * free the memory associated with it.
3021 */
3022 xfs_efi_release(efip, efip->efi_format.efi_nextents);
3c1e2bbe 3023 return XFS_ERROR(EIO);
1da177e4
LT
3024 }
3025 }
3026
3027 tp = xfs_trans_alloc(mp, 0);
3c1e2bbe 3028 error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 0, 0);
fc6149d8
DC
3029 if (error)
3030 goto abort_error;
1da177e4
LT
3031 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
3032
3033 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3034 extp = &(efip->efi_format.efi_extents[i]);
fc6149d8
DC
3035 error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
3036 if (error)
3037 goto abort_error;
1da177e4
LT
3038 xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
3039 extp->ext_len);
3040 }
3041
3042 efip->efi_flags |= XFS_EFI_RECOVERED;
e5720eec 3043 error = xfs_trans_commit(tp, 0);
3c1e2bbe 3044 return error;
fc6149d8
DC
3045
3046abort_error:
3047 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3048 return error;
1da177e4
LT
3049}
3050
1da177e4
LT
3051/*
3052 * When this is called, all of the EFIs which did not have
3053 * corresponding EFDs should be in the AIL. What we do now
3054 * is free the extents associated with each one.
3055 *
3056 * Since we process the EFIs in normal transactions, they
3057 * will be removed at some point after the commit. This prevents
3058 * us from just walking down the list processing each one.
3059 * We'll use a flag in the EFI to skip those that we've already
3060 * processed and use the AIL iteration mechanism's generation
3061 * count to try to speed this up at least a bit.
3062 *
3063 * When we start, we know that the EFIs are the only things in
3064 * the AIL. As we process them, however, other items are added
3065 * to the AIL. Since everything added to the AIL must come after
3066 * everything already in the AIL, we stop processing as soon as
3067 * we see something other than an EFI in the AIL.
3068 */
3c1e2bbe 3069STATIC int
1da177e4
LT
3070xlog_recover_process_efis(
3071 xlog_t *log)
3072{
3073 xfs_log_item_t *lip;
3074 xfs_efi_log_item_t *efip;
3c1e2bbe 3075 int error = 0;
27d8d5fe 3076 struct xfs_ail_cursor cur;
a9c21c1b 3077 struct xfs_ail *ailp;
1da177e4 3078
a9c21c1b
DC
3079 ailp = log->l_ailp;
3080 spin_lock(&ailp->xa_lock);
3081 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
1da177e4
LT
3082 while (lip != NULL) {
3083 /*
3084 * We're done when we see something other than an EFI.
27d8d5fe 3085 * There should be no EFIs left in the AIL now.
1da177e4
LT
3086 */
3087 if (lip->li_type != XFS_LI_EFI) {
27d8d5fe 3088#ifdef DEBUG
a9c21c1b 3089 for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
27d8d5fe
DC
3090 ASSERT(lip->li_type != XFS_LI_EFI);
3091#endif
1da177e4
LT
3092 break;
3093 }
3094
3095 /*
3096 * Skip EFIs that we've already processed.
3097 */
3098 efip = (xfs_efi_log_item_t *)lip;
3099 if (efip->efi_flags & XFS_EFI_RECOVERED) {
a9c21c1b 3100 lip = xfs_trans_ail_cursor_next(ailp, &cur);
1da177e4
LT
3101 continue;
3102 }
3103
a9c21c1b
DC
3104 spin_unlock(&ailp->xa_lock);
3105 error = xlog_recover_process_efi(log->l_mp, efip);
3106 spin_lock(&ailp->xa_lock);
27d8d5fe
DC
3107 if (error)
3108 goto out;
a9c21c1b 3109 lip = xfs_trans_ail_cursor_next(ailp, &cur);
1da177e4 3110 }
27d8d5fe 3111out:
a9c21c1b
DC
3112 xfs_trans_ail_cursor_done(ailp, &cur);
3113 spin_unlock(&ailp->xa_lock);
3c1e2bbe 3114 return error;
1da177e4
LT
3115}
3116
3117/*
3118 * This routine performs a transaction to null out a bad inode pointer
3119 * in an agi unlinked inode hash bucket.
3120 */
3121STATIC void
3122xlog_recover_clear_agi_bucket(
3123 xfs_mount_t *mp,
3124 xfs_agnumber_t agno,
3125 int bucket)
3126{
3127 xfs_trans_t *tp;
3128 xfs_agi_t *agi;
3129 xfs_buf_t *agibp;
3130 int offset;
3131 int error;
3132
3133 tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
5e1be0fb
CH
3134 error = xfs_trans_reserve(tp, 0, XFS_CLEAR_AGI_BUCKET_LOG_RES(mp),
3135 0, 0, 0);
e5720eec
DC
3136 if (error)
3137 goto out_abort;
1da177e4 3138
5e1be0fb
CH
3139 error = xfs_read_agi(mp, tp, agno, &agibp);
3140 if (error)
e5720eec 3141 goto out_abort;
1da177e4 3142
5e1be0fb 3143 agi = XFS_BUF_TO_AGI(agibp);
16259e7d 3144 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
1da177e4
LT
3145 offset = offsetof(xfs_agi_t, agi_unlinked) +
3146 (sizeof(xfs_agino_t) * bucket);
3147 xfs_trans_log_buf(tp, agibp, offset,
3148 (offset + sizeof(xfs_agino_t) - 1));
3149
e5720eec
DC
3150 error = xfs_trans_commit(tp, 0);
3151 if (error)
3152 goto out_error;
3153 return;
3154
3155out_abort:
3156 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3157out_error:
3158 xfs_fs_cmn_err(CE_WARN, mp, "xlog_recover_clear_agi_bucket: "
3159 "failed to clear agi %d. Continuing.", agno);
3160 return;
1da177e4
LT
3161}
3162
23fac50f
CH
3163STATIC xfs_agino_t
3164xlog_recover_process_one_iunlink(
3165 struct xfs_mount *mp,
3166 xfs_agnumber_t agno,
3167 xfs_agino_t agino,
3168 int bucket)
3169{
3170 struct xfs_buf *ibp;
3171 struct xfs_dinode *dip;
3172 struct xfs_inode *ip;
3173 xfs_ino_t ino;
3174 int error;
3175
3176 ino = XFS_AGINO_TO_INO(mp, agno, agino);
3177 error = xfs_iget(mp, NULL, ino, 0, 0, &ip, 0);
3178 if (error)
3179 goto fail;
3180
3181 /*
3182 * Get the on disk inode to find the next inode in the bucket.
3183 */
0cadda1c 3184 error = xfs_itobp(mp, NULL, ip, &dip, &ibp, XBF_LOCK);
23fac50f 3185 if (error)
0e446673 3186 goto fail_iput;
23fac50f 3187
23fac50f 3188 ASSERT(ip->i_d.di_nlink == 0);
0e446673 3189 ASSERT(ip->i_d.di_mode != 0);
23fac50f
CH
3190
3191 /* setup for the next pass */
3192 agino = be32_to_cpu(dip->di_next_unlinked);
3193 xfs_buf_relse(ibp);
3194
3195 /*
3196 * Prevent any DMAPI event from being sent when the reference on
3197 * the inode is dropped.
3198 */
3199 ip->i_d.di_dmevmask = 0;
3200
0e446673 3201 IRELE(ip);
23fac50f
CH
3202 return agino;
3203
0e446673
CH
3204 fail_iput:
3205 IRELE(ip);
23fac50f
CH
3206 fail:
3207 /*
3208 * We can't read in the inode this bucket points to, or this inode
3209 * is messed up. Just ditch this bucket of inodes. We will lose
3210 * some inodes and space, but at least we won't hang.
3211 *
3212 * Call xlog_recover_clear_agi_bucket() to perform a transaction to
3213 * clear the inode pointer in the bucket.
3214 */
3215 xlog_recover_clear_agi_bucket(mp, agno, bucket);
3216 return NULLAGINO;
3217}
3218
1da177e4
LT
3219/*
3220 * xlog_iunlink_recover
3221 *
3222 * This is called during recovery to process any inodes which
3223 * we unlinked but not freed when the system crashed. These
3224 * inodes will be on the lists in the AGI blocks. What we do
3225 * here is scan all the AGIs and fully truncate and free any
3226 * inodes found on the lists. Each inode is removed from the
3227 * lists when it has been fully truncated and is freed. The
3228 * freeing of the inode and its removal from the list must be
3229 * atomic.
3230 */
d96f8f89 3231STATIC void
1da177e4
LT
3232xlog_recover_process_iunlinks(
3233 xlog_t *log)
3234{
3235 xfs_mount_t *mp;
3236 xfs_agnumber_t agno;
3237 xfs_agi_t *agi;
3238 xfs_buf_t *agibp;
1da177e4 3239 xfs_agino_t agino;
1da177e4
LT
3240 int bucket;
3241 int error;
3242 uint mp_dmevmask;
3243
3244 mp = log->l_mp;
3245
3246 /*
3247 * Prevent any DMAPI event from being sent while in this function.
3248 */
3249 mp_dmevmask = mp->m_dmevmask;
3250 mp->m_dmevmask = 0;
3251
3252 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
3253 /*
3254 * Find the agi for this ag.
3255 */
5e1be0fb
CH
3256 error = xfs_read_agi(mp, NULL, agno, &agibp);
3257 if (error) {
3258 /*
3259 * AGI is b0rked. Don't process it.
3260 *
3261 * We should probably mark the filesystem as corrupt
3262 * after we've recovered all the ag's we can....
3263 */
3264 continue;
1da177e4
LT
3265 }
3266 agi = XFS_BUF_TO_AGI(agibp);
1da177e4
LT
3267
3268 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
16259e7d 3269 agino = be32_to_cpu(agi->agi_unlinked[bucket]);
1da177e4 3270 while (agino != NULLAGINO) {
1da177e4
LT
3271 /*
3272 * Release the agi buffer so that it can
3273 * be acquired in the normal course of the
3274 * transaction to truncate and free the inode.
3275 */
3276 xfs_buf_relse(agibp);
3277
23fac50f
CH
3278 agino = xlog_recover_process_one_iunlink(mp,
3279 agno, agino, bucket);
1da177e4
LT
3280
3281 /*
3282 * Reacquire the agibuffer and continue around
5e1be0fb
CH
3283 * the loop. This should never fail as we know
3284 * the buffer was good earlier on.
1da177e4 3285 */
5e1be0fb
CH
3286 error = xfs_read_agi(mp, NULL, agno, &agibp);
3287 ASSERT(error == 0);
1da177e4 3288 agi = XFS_BUF_TO_AGI(agibp);
1da177e4
LT
3289 }
3290 }
3291
3292 /*
3293 * Release the buffer for the current agi so we can
3294 * go on to the next one.
3295 */
3296 xfs_buf_relse(agibp);
3297 }
3298
3299 mp->m_dmevmask = mp_dmevmask;
3300}
3301
3302
3303#ifdef DEBUG
3304STATIC void
3305xlog_pack_data_checksum(
3306 xlog_t *log,
3307 xlog_in_core_t *iclog,
3308 int size)
3309{
3310 int i;
b53e675d 3311 __be32 *up;
1da177e4
LT
3312 uint chksum = 0;
3313
b53e675d 3314 up = (__be32 *)iclog->ic_datap;
1da177e4
LT
3315 /* divide length by 4 to get # words */
3316 for (i = 0; i < (size >> 2); i++) {
b53e675d 3317 chksum ^= be32_to_cpu(*up);
1da177e4
LT
3318 up++;
3319 }
b53e675d 3320 iclog->ic_header.h_chksum = cpu_to_be32(chksum);
1da177e4
LT
3321}
3322#else
3323#define xlog_pack_data_checksum(log, iclog, size)
3324#endif
3325
3326/*
3327 * Stamp cycle number in every block
3328 */
3329void
3330xlog_pack_data(
3331 xlog_t *log,
3332 xlog_in_core_t *iclog,
3333 int roundoff)
3334{
3335 int i, j, k;
3336 int size = iclog->ic_offset + roundoff;
b53e675d 3337 __be32 cycle_lsn;
1da177e4 3338 xfs_caddr_t dp;
1da177e4
LT
3339
3340 xlog_pack_data_checksum(log, iclog, size);
3341
3342 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
3343
3344 dp = iclog->ic_datap;
3345 for (i = 0; i < BTOBB(size) &&
3346 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
b53e675d
CH
3347 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
3348 *(__be32 *)dp = cycle_lsn;
1da177e4
LT
3349 dp += BBSIZE;
3350 }
3351
62118709 3352 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
b28708d6
CH
3353 xlog_in_core_2_t *xhdr = iclog->ic_data;
3354
1da177e4
LT
3355 for ( ; i < BTOBB(size); i++) {
3356 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3357 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
b53e675d
CH
3358 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
3359 *(__be32 *)dp = cycle_lsn;
1da177e4
LT
3360 dp += BBSIZE;
3361 }
3362
3363 for (i = 1; i < log->l_iclog_heads; i++) {
3364 xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
3365 }
3366 }
3367}
3368
3369#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
3370STATIC void
3371xlog_unpack_data_checksum(
3372 xlog_rec_header_t *rhead,
3373 xfs_caddr_t dp,
3374 xlog_t *log)
3375{
b53e675d 3376 __be32 *up = (__be32 *)dp;
1da177e4
LT
3377 uint chksum = 0;
3378 int i;
3379
3380 /* divide length by 4 to get # words */
b53e675d
CH
3381 for (i=0; i < be32_to_cpu(rhead->h_len) >> 2; i++) {
3382 chksum ^= be32_to_cpu(*up);
1da177e4
LT
3383 up++;
3384 }
b53e675d 3385 if (chksum != be32_to_cpu(rhead->h_chksum)) {
1da177e4
LT
3386 if (rhead->h_chksum ||
3387 ((log->l_flags & XLOG_CHKSUM_MISMATCH) == 0)) {
3388 cmn_err(CE_DEBUG,
b6574520 3389 "XFS: LogR chksum mismatch: was (0x%x) is (0x%x)\n",
b53e675d 3390 be32_to_cpu(rhead->h_chksum), chksum);
1da177e4
LT
3391 cmn_err(CE_DEBUG,
3392"XFS: Disregard message if filesystem was created with non-DEBUG kernel");
62118709 3393 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1da177e4 3394 cmn_err(CE_DEBUG,
b6574520 3395 "XFS: LogR this is a LogV2 filesystem\n");
1da177e4
LT
3396 }
3397 log->l_flags |= XLOG_CHKSUM_MISMATCH;
3398 }
3399 }
3400}
3401#else
3402#define xlog_unpack_data_checksum(rhead, dp, log)
3403#endif
3404
3405STATIC void
3406xlog_unpack_data(
3407 xlog_rec_header_t *rhead,
3408 xfs_caddr_t dp,
3409 xlog_t *log)
3410{
3411 int i, j, k;
1da177e4 3412
b53e675d 3413 for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
1da177e4 3414 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
b53e675d 3415 *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
1da177e4
LT
3416 dp += BBSIZE;
3417 }
3418
62118709 3419 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
b28708d6 3420 xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
b53e675d 3421 for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
1da177e4
LT
3422 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3423 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
b53e675d 3424 *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
1da177e4
LT
3425 dp += BBSIZE;
3426 }
3427 }
3428
3429 xlog_unpack_data_checksum(rhead, dp, log);
3430}
3431
3432STATIC int
3433xlog_valid_rec_header(
3434 xlog_t *log,
3435 xlog_rec_header_t *rhead,
3436 xfs_daddr_t blkno)
3437{
3438 int hlen;
3439
b53e675d 3440 if (unlikely(be32_to_cpu(rhead->h_magicno) != XLOG_HEADER_MAGIC_NUM)) {
1da177e4
LT
3441 XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
3442 XFS_ERRLEVEL_LOW, log->l_mp);
3443 return XFS_ERROR(EFSCORRUPTED);
3444 }
3445 if (unlikely(
3446 (!rhead->h_version ||
b53e675d 3447 (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
1da177e4 3448 xlog_warn("XFS: %s: unrecognised log version (%d).",
34a622b2 3449 __func__, be32_to_cpu(rhead->h_version));
1da177e4
LT
3450 return XFS_ERROR(EIO);
3451 }
3452
3453 /* LR body must have data or it wouldn't have been written */
b53e675d 3454 hlen = be32_to_cpu(rhead->h_len);
1da177e4
LT
3455 if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
3456 XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
3457 XFS_ERRLEVEL_LOW, log->l_mp);
3458 return XFS_ERROR(EFSCORRUPTED);
3459 }
3460 if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
3461 XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
3462 XFS_ERRLEVEL_LOW, log->l_mp);
3463 return XFS_ERROR(EFSCORRUPTED);
3464 }
3465 return 0;
3466}
3467
3468/*
3469 * Read the log from tail to head and process the log records found.
3470 * Handle the two cases where the tail and head are in the same cycle
3471 * and where the active portion of the log wraps around the end of
3472 * the physical log separately. The pass parameter is passed through
3473 * to the routines called to process the data and is not looked at
3474 * here.
3475 */
3476STATIC int
3477xlog_do_recovery_pass(
3478 xlog_t *log,
3479 xfs_daddr_t head_blk,
3480 xfs_daddr_t tail_blk,
3481 int pass)
3482{
3483 xlog_rec_header_t *rhead;
3484 xfs_daddr_t blk_no;
fc5bc4c8 3485 xfs_caddr_t offset;
1da177e4
LT
3486 xfs_buf_t *hbp, *dbp;
3487 int error = 0, h_size;
3488 int bblks, split_bblks;
3489 int hblks, split_hblks, wrapped_hblks;
f0a76953 3490 struct hlist_head rhash[XLOG_RHASH_SIZE];
1da177e4
LT
3491
3492 ASSERT(head_blk != tail_blk);
3493
3494 /*
3495 * Read the header of the tail block and get the iclog buffer size from
3496 * h_size. Use this to tell how many sectors make up the log header.
3497 */
62118709 3498 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1da177e4
LT
3499 /*
3500 * When using variable length iclogs, read first sector of
3501 * iclog header and extract the header size from it. Get a
3502 * new hbp that is the correct size.
3503 */
3504 hbp = xlog_get_bp(log, 1);
3505 if (!hbp)
3506 return ENOMEM;
076e6acb
CH
3507
3508 error = xlog_bread(log, tail_blk, 1, hbp, &offset);
3509 if (error)
1da177e4 3510 goto bread_err1;
076e6acb 3511
1da177e4
LT
3512 rhead = (xlog_rec_header_t *)offset;
3513 error = xlog_valid_rec_header(log, rhead, tail_blk);
3514 if (error)
3515 goto bread_err1;
b53e675d
CH
3516 h_size = be32_to_cpu(rhead->h_size);
3517 if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
1da177e4
LT
3518 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
3519 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
3520 if (h_size % XLOG_HEADER_CYCLE_SIZE)
3521 hblks++;
3522 xlog_put_bp(hbp);
3523 hbp = xlog_get_bp(log, hblks);
3524 } else {
3525 hblks = 1;
3526 }
3527 } else {
3528 ASSERT(log->l_sectbb_log == 0);
3529 hblks = 1;
3530 hbp = xlog_get_bp(log, 1);
3531 h_size = XLOG_BIG_RECORD_BSIZE;
3532 }
3533
3534 if (!hbp)
3535 return ENOMEM;
3536 dbp = xlog_get_bp(log, BTOBB(h_size));
3537 if (!dbp) {
3538 xlog_put_bp(hbp);
3539 return ENOMEM;
3540 }
3541
3542 memset(rhash, 0, sizeof(rhash));
3543 if (tail_blk <= head_blk) {
3544 for (blk_no = tail_blk; blk_no < head_blk; ) {
076e6acb
CH
3545 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3546 if (error)
1da177e4 3547 goto bread_err2;
076e6acb 3548
1da177e4
LT
3549 rhead = (xlog_rec_header_t *)offset;
3550 error = xlog_valid_rec_header(log, rhead, blk_no);
3551 if (error)
3552 goto bread_err2;
3553
3554 /* blocks in data section */
b53e675d 3555 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
076e6acb
CH
3556 error = xlog_bread(log, blk_no + hblks, bblks, dbp,
3557 &offset);
1da177e4
LT
3558 if (error)
3559 goto bread_err2;
076e6acb 3560
1da177e4
LT
3561 xlog_unpack_data(rhead, offset, log);
3562 if ((error = xlog_recover_process_data(log,
3563 rhash, rhead, offset, pass)))
3564 goto bread_err2;
3565 blk_no += bblks + hblks;
3566 }
3567 } else {
3568 /*
3569 * Perform recovery around the end of the physical log.
3570 * When the head is not on the same cycle number as the tail,
3571 * we can't do a sequential recovery as above.
3572 */
3573 blk_no = tail_blk;
3574 while (blk_no < log->l_logBBsize) {
3575 /*
3576 * Check for header wrapping around physical end-of-log
3577 */
fc5bc4c8 3578 offset = XFS_BUF_PTR(hbp);
1da177e4
LT
3579 split_hblks = 0;
3580 wrapped_hblks = 0;
3581 if (blk_no + hblks <= log->l_logBBsize) {
3582 /* Read header in one read */
076e6acb
CH
3583 error = xlog_bread(log, blk_no, hblks, hbp,
3584 &offset);
1da177e4
LT
3585 if (error)
3586 goto bread_err2;
1da177e4
LT
3587 } else {
3588 /* This LR is split across physical log end */
3589 if (blk_no != log->l_logBBsize) {
3590 /* some data before physical log end */
3591 ASSERT(blk_no <= INT_MAX);
3592 split_hblks = log->l_logBBsize - (int)blk_no;
3593 ASSERT(split_hblks > 0);
076e6acb
CH
3594 error = xlog_bread(log, blk_no,
3595 split_hblks, hbp,
3596 &offset);
3597 if (error)
1da177e4 3598 goto bread_err2;
1da177e4 3599 }
076e6acb 3600
1da177e4
LT
3601 /*
3602 * Note: this black magic still works with
3603 * large sector sizes (non-512) only because:
3604 * - we increased the buffer size originally
3605 * by 1 sector giving us enough extra space
3606 * for the second read;
3607 * - the log start is guaranteed to be sector
3608 * aligned;
3609 * - we read the log end (LR header start)
3610 * _first_, then the log start (LR header end)
3611 * - order is important.
3612 */
234f56ac 3613 wrapped_hblks = hblks - split_hblks;
234f56ac 3614 error = XFS_BUF_SET_PTR(hbp,
fc5bc4c8 3615 offset + BBTOB(split_hblks),
1da177e4 3616 BBTOB(hblks - split_hblks));
076e6acb
CH
3617 if (error)
3618 goto bread_err2;
3619
3620 error = xlog_bread_noalign(log, 0,
3621 wrapped_hblks, hbp);
3622 if (error)
3623 goto bread_err2;
3624
fc5bc4c8 3625 error = XFS_BUF_SET_PTR(hbp, offset,
234f56ac 3626 BBTOB(hblks));
1da177e4
LT
3627 if (error)
3628 goto bread_err2;
1da177e4
LT
3629 }
3630 rhead = (xlog_rec_header_t *)offset;
3631 error = xlog_valid_rec_header(log, rhead,
3632 split_hblks ? blk_no : 0);
3633 if (error)
3634 goto bread_err2;
3635
b53e675d 3636 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
1da177e4
LT
3637 blk_no += hblks;
3638
3639 /* Read in data for log record */
3640 if (blk_no + bblks <= log->l_logBBsize) {
076e6acb
CH
3641 error = xlog_bread(log, blk_no, bblks, dbp,
3642 &offset);
1da177e4
LT
3643 if (error)
3644 goto bread_err2;
1da177e4
LT
3645 } else {
3646 /* This log record is split across the
3647 * physical end of log */
fc5bc4c8 3648 offset = XFS_BUF_PTR(dbp);
1da177e4
LT
3649 split_bblks = 0;
3650 if (blk_no != log->l_logBBsize) {
3651 /* some data is before the physical
3652 * end of log */
3653 ASSERT(!wrapped_hblks);
3654 ASSERT(blk_no <= INT_MAX);
3655 split_bblks =
3656 log->l_logBBsize - (int)blk_no;
3657 ASSERT(split_bblks > 0);
076e6acb
CH
3658 error = xlog_bread(log, blk_no,
3659 split_bblks, dbp,
3660 &offset);
3661 if (error)
1da177e4 3662 goto bread_err2;
1da177e4 3663 }
076e6acb 3664
1da177e4
LT
3665 /*
3666 * Note: this black magic still works with
3667 * large sector sizes (non-512) only because:
3668 * - we increased the buffer size originally
3669 * by 1 sector giving us enough extra space
3670 * for the second read;
3671 * - the log start is guaranteed to be sector
3672 * aligned;
3673 * - we read the log end (LR header start)
3674 * _first_, then the log start (LR header end)
3675 * - order is important.
3676 */
234f56ac 3677 error = XFS_BUF_SET_PTR(dbp,
fc5bc4c8 3678 offset + BBTOB(split_bblks),
1da177e4 3679 BBTOB(bblks - split_bblks));
234f56ac 3680 if (error)
1da177e4 3681 goto bread_err2;
076e6acb
CH
3682
3683 error = xlog_bread_noalign(log, wrapped_hblks,
3684 bblks - split_bblks,
3685 dbp);
3686 if (error)
3687 goto bread_err2;
3688
fc5bc4c8 3689 error = XFS_BUF_SET_PTR(dbp, offset, h_size);
076e6acb
CH
3690 if (error)
3691 goto bread_err2;
1da177e4
LT
3692 }
3693 xlog_unpack_data(rhead, offset, log);
3694 if ((error = xlog_recover_process_data(log, rhash,
3695 rhead, offset, pass)))
3696 goto bread_err2;
3697 blk_no += bblks;
3698 }
3699
3700 ASSERT(blk_no >= log->l_logBBsize);
3701 blk_no -= log->l_logBBsize;
3702
3703 /* read first part of physical log */
3704 while (blk_no < head_blk) {
076e6acb
CH
3705 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3706 if (error)
1da177e4 3707 goto bread_err2;
076e6acb 3708
1da177e4
LT
3709 rhead = (xlog_rec_header_t *)offset;
3710 error = xlog_valid_rec_header(log, rhead, blk_no);
3711 if (error)
3712 goto bread_err2;
076e6acb 3713
b53e675d 3714 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
076e6acb
CH
3715 error = xlog_bread(log, blk_no+hblks, bblks, dbp,
3716 &offset);
3717 if (error)
1da177e4 3718 goto bread_err2;
076e6acb 3719
1da177e4
LT
3720 xlog_unpack_data(rhead, offset, log);
3721 if ((error = xlog_recover_process_data(log, rhash,
3722 rhead, offset, pass)))
3723 goto bread_err2;
3724 blk_no += bblks + hblks;
3725 }
3726 }
3727
3728 bread_err2:
3729 xlog_put_bp(dbp);
3730 bread_err1:
3731 xlog_put_bp(hbp);
3732 return error;
3733}
3734
3735/*
3736 * Do the recovery of the log. We actually do this in two phases.
3737 * The two passes are necessary in order to implement the function
3738 * of cancelling a record written into the log. The first pass
3739 * determines those things which have been cancelled, and the
3740 * second pass replays log items normally except for those which
3741 * have been cancelled. The handling of the replay and cancellations
3742 * takes place in the log item type specific routines.
3743 *
3744 * The table of items which have cancel records in the log is allocated
3745 * and freed at this level, since only here do we know when all of
3746 * the log recovery has been completed.
3747 */
3748STATIC int
3749xlog_do_log_recovery(
3750 xlog_t *log,
3751 xfs_daddr_t head_blk,
3752 xfs_daddr_t tail_blk)
3753{
3754 int error;
3755
3756 ASSERT(head_blk != tail_blk);
3757
3758 /*
3759 * First do a pass to find all of the cancelled buf log items.
3760 * Store them in the buf_cancel_table for use in the second pass.
3761 */
3762 log->l_buf_cancel_table =
3763 (xfs_buf_cancel_t **)kmem_zalloc(XLOG_BC_TABLE_SIZE *
3764 sizeof(xfs_buf_cancel_t*),
3765 KM_SLEEP);
3766 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3767 XLOG_RECOVER_PASS1);
3768 if (error != 0) {
f0e2d93c 3769 kmem_free(log->l_buf_cancel_table);
1da177e4
LT
3770 log->l_buf_cancel_table = NULL;
3771 return error;
3772 }
3773 /*
3774 * Then do a second pass to actually recover the items in the log.
3775 * When it is complete free the table of buf cancel items.
3776 */
3777 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3778 XLOG_RECOVER_PASS2);
3779#ifdef DEBUG
6d192a9b 3780 if (!error) {
1da177e4
LT
3781 int i;
3782
3783 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
3784 ASSERT(log->l_buf_cancel_table[i] == NULL);
3785 }
3786#endif /* DEBUG */
3787
f0e2d93c 3788 kmem_free(log->l_buf_cancel_table);
1da177e4
LT
3789 log->l_buf_cancel_table = NULL;
3790
3791 return error;
3792}
3793
3794/*
3795 * Do the actual recovery
3796 */
3797STATIC int
3798xlog_do_recover(
3799 xlog_t *log,
3800 xfs_daddr_t head_blk,
3801 xfs_daddr_t tail_blk)
3802{
3803 int error;
3804 xfs_buf_t *bp;
3805 xfs_sb_t *sbp;
3806
3807 /*
3808 * First replay the images in the log.
3809 */
3810 error = xlog_do_log_recovery(log, head_blk, tail_blk);
3811 if (error) {
3812 return error;
3813 }
3814
3815 XFS_bflush(log->l_mp->m_ddev_targp);
3816
3817 /*
3818 * If IO errors happened during recovery, bail out.
3819 */
3820 if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
3821 return (EIO);
3822 }
3823
3824 /*
3825 * We now update the tail_lsn since much of the recovery has completed
3826 * and there may be space available to use. If there were no extent
3827 * or iunlinks, we can free up the entire log and set the tail_lsn to
3828 * be the last_sync_lsn. This was set in xlog_find_tail to be the
3829 * lsn of the last known good LR on disk. If there are extent frees
3830 * or iunlinks they will have some entries in the AIL; so we look at
3831 * the AIL to determine how to set the tail_lsn.
3832 */
3833 xlog_assign_tail_lsn(log->l_mp);
3834
3835 /*
3836 * Now that we've finished replaying all buffer and inode
3837 * updates, re-read in the superblock.
3838 */
3839 bp = xfs_getsb(log->l_mp, 0);
3840 XFS_BUF_UNDONE(bp);
bebf963f
LM
3841 ASSERT(!(XFS_BUF_ISWRITE(bp)));
3842 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
1da177e4 3843 XFS_BUF_READ(bp);
bebf963f 3844 XFS_BUF_UNASYNC(bp);
1da177e4 3845 xfsbdstrat(log->l_mp, bp);
d64e31a2
DC
3846 error = xfs_iowait(bp);
3847 if (error) {
1da177e4
LT
3848 xfs_ioerror_alert("xlog_do_recover",
3849 log->l_mp, bp, XFS_BUF_ADDR(bp));
3850 ASSERT(0);
3851 xfs_buf_relse(bp);
3852 return error;
3853 }
3854
3855 /* Convert superblock from on-disk format */
3856 sbp = &log->l_mp->m_sb;
2bdf7cd0 3857 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
1da177e4 3858 ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
62118709 3859 ASSERT(xfs_sb_good_version(sbp));
1da177e4
LT
3860 xfs_buf_relse(bp);
3861
5478eead
LM
3862 /* We've re-read the superblock so re-initialize per-cpu counters */
3863 xfs_icsb_reinit_counters(log->l_mp);
3864
1da177e4
LT
3865 xlog_recover_check_summary(log);
3866
3867 /* Normal transactions can now occur */
3868 log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
3869 return 0;
3870}
3871
3872/*
3873 * Perform recovery and re-initialize some log variables in xlog_find_tail.
3874 *
3875 * Return error or zero.
3876 */
3877int
3878xlog_recover(
65be6054 3879 xlog_t *log)
1da177e4
LT
3880{
3881 xfs_daddr_t head_blk, tail_blk;
3882 int error;
3883
3884 /* find the tail of the log */
65be6054 3885 if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
1da177e4
LT
3886 return error;
3887
3888 if (tail_blk != head_blk) {
3889 /* There used to be a comment here:
3890 *
3891 * disallow recovery on read-only mounts. note -- mount
3892 * checks for ENOSPC and turns it into an intelligent
3893 * error message.
3894 * ...but this is no longer true. Now, unless you specify
3895 * NORECOVERY (in which case this function would never be
3896 * called), we just go ahead and recover. We do this all
3897 * under the vfs layer, so we can get away with it unless
3898 * the device itself is read-only, in which case we fail.
3899 */
3a02ee18 3900 if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
1da177e4
LT
3901 return error;
3902 }
3903
3904 cmn_err(CE_NOTE,
fc1f8c1c
NS
3905 "Starting XFS recovery on filesystem: %s (logdev: %s)",
3906 log->l_mp->m_fsname, log->l_mp->m_logname ?
3907 log->l_mp->m_logname : "internal");
1da177e4
LT
3908
3909 error = xlog_do_recover(log, head_blk, tail_blk);
3910 log->l_flags |= XLOG_RECOVERY_NEEDED;
3911 }
3912 return error;
3913}
3914
3915/*
3916 * In the first part of recovery we replay inodes and buffers and build
3917 * up the list of extent free items which need to be processed. Here
3918 * we process the extent free items and clean up the on disk unlinked
3919 * inode lists. This is separated from the first part of recovery so
3920 * that the root and real-time bitmap inodes can be read in from disk in
3921 * between the two stages. This is necessary so that we can free space
3922 * in the real-time portion of the file system.
3923 */
3924int
3925xlog_recover_finish(
4249023a 3926 xlog_t *log)
1da177e4
LT
3927{
3928 /*
3929 * Now we're ready to do the transactions needed for the
3930 * rest of recovery. Start with completing all the extent
3931 * free intent records and then process the unlinked inode
3932 * lists. At this point, we essentially run in normal mode
3933 * except that we're still performing recovery actions
3934 * rather than accepting new requests.
3935 */
3936 if (log->l_flags & XLOG_RECOVERY_NEEDED) {
3c1e2bbe
DC
3937 int error;
3938 error = xlog_recover_process_efis(log);
3939 if (error) {
3940 cmn_err(CE_ALERT,
3941 "Failed to recover EFIs on filesystem: %s",
3942 log->l_mp->m_fsname);
3943 return error;
3944 }
1da177e4
LT
3945 /*
3946 * Sync the log to get all the EFIs out of the AIL.
3947 * This isn't absolutely necessary, but it helps in
3948 * case the unlink transactions would have problems
3949 * pushing the EFIs out of the way.
3950 */
a14a348b 3951 xfs_log_force(log->l_mp, XFS_LOG_SYNC);
1da177e4 3952
4249023a 3953 xlog_recover_process_iunlinks(log);
1da177e4
LT
3954
3955 xlog_recover_check_summary(log);
3956
3957 cmn_err(CE_NOTE,
fc1f8c1c
NS
3958 "Ending XFS recovery on filesystem: %s (logdev: %s)",
3959 log->l_mp->m_fsname, log->l_mp->m_logname ?
3960 log->l_mp->m_logname : "internal");
1da177e4
LT
3961 log->l_flags &= ~XLOG_RECOVERY_NEEDED;
3962 } else {
3963 cmn_err(CE_DEBUG,
b6574520 3964 "!Ending clean XFS mount for filesystem: %s\n",
1da177e4
LT
3965 log->l_mp->m_fsname);
3966 }
3967 return 0;
3968}
3969
3970
3971#if defined(DEBUG)
3972/*
3973 * Read all of the agf and agi counters and check that they
3974 * are consistent with the superblock counters.
3975 */
3976void
3977xlog_recover_check_summary(
3978 xlog_t *log)
3979{
3980 xfs_mount_t *mp;
3981 xfs_agf_t *agfp;
1da177e4
LT
3982 xfs_buf_t *agfbp;
3983 xfs_buf_t *agibp;
1da177e4
LT
3984 xfs_buf_t *sbbp;
3985#ifdef XFS_LOUD_RECOVERY
3986 xfs_sb_t *sbp;
3987#endif
3988 xfs_agnumber_t agno;
3989 __uint64_t freeblks;
3990 __uint64_t itotal;
3991 __uint64_t ifree;
5e1be0fb 3992 int error;
1da177e4
LT
3993
3994 mp = log->l_mp;
3995
3996 freeblks = 0LL;
3997 itotal = 0LL;
3998 ifree = 0LL;
3999 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
4805621a
FCH
4000 error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
4001 if (error) {
4002 xfs_fs_cmn_err(CE_ALERT, mp,
4003 "xlog_recover_check_summary(agf)"
4004 "agf read failed agno %d error %d",
4005 agno, error);
4006 } else {
4007 agfp = XFS_BUF_TO_AGF(agfbp);
4008 freeblks += be32_to_cpu(agfp->agf_freeblks) +
4009 be32_to_cpu(agfp->agf_flcount);
4010 xfs_buf_relse(agfbp);
1da177e4 4011 }
1da177e4 4012
5e1be0fb
CH
4013 error = xfs_read_agi(mp, NULL, agno, &agibp);
4014 if (!error) {
4015 struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
16259e7d 4016
5e1be0fb
CH
4017 itotal += be32_to_cpu(agi->agi_count);
4018 ifree += be32_to_cpu(agi->agi_freecount);
4019 xfs_buf_relse(agibp);
4020 }
1da177e4
LT
4021 }
4022
4023 sbbp = xfs_getsb(mp, 0);
4024#ifdef XFS_LOUD_RECOVERY
4025 sbp = &mp->m_sb;
2bdf7cd0 4026 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(sbbp));
1da177e4
LT
4027 cmn_err(CE_NOTE,
4028 "xlog_recover_check_summary: sb_icount %Lu itotal %Lu",
4029 sbp->sb_icount, itotal);
4030 cmn_err(CE_NOTE,
4031 "xlog_recover_check_summary: sb_ifree %Lu itotal %Lu",
4032 sbp->sb_ifree, ifree);
4033 cmn_err(CE_NOTE,
4034 "xlog_recover_check_summary: sb_fdblocks %Lu freeblks %Lu",
4035 sbp->sb_fdblocks, freeblks);
4036#if 0
4037 /*
4038 * This is turned off until I account for the allocation
4039 * btree blocks which live in free space.
4040 */
4041 ASSERT(sbp->sb_icount == itotal);
4042 ASSERT(sbp->sb_ifree == ifree);
4043 ASSERT(sbp->sb_fdblocks == freeblks);
4044#endif
4045#endif
4046 xfs_buf_relse(sbbp);
4047}
4048#endif /* DEBUG */
This page took 0.696159 seconds and 5 git commands to generate.