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