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