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