PageRenderTime 82ms CodeModel.GetById 23ms app.highlight 41ms RepoModel.GetById 0ms app.codeStats 2ms

/fs/xfs/xfs_log_recover.c

https://bitbucket.org/emiliolopez/linux
C | 5872 lines | 3674 code | 564 blank | 1634 comment | 694 complexity | 38acf4b57544ad66eb9765d057c186f1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
   3 * All Rights Reserved.
   4 *
   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
   7 * published by the Free Software Foundation.
   8 *
   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.
  13 *
  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
  17 */
  18#include "xfs.h"
  19#include "xfs_fs.h"
  20#include "xfs_shared.h"
  21#include "xfs_format.h"
  22#include "xfs_log_format.h"
  23#include "xfs_trans_resv.h"
  24#include "xfs_bit.h"
  25#include "xfs_sb.h"
  26#include "xfs_mount.h"
  27#include "xfs_da_format.h"
  28#include "xfs_da_btree.h"
  29#include "xfs_inode.h"
  30#include "xfs_trans.h"
  31#include "xfs_log.h"
  32#include "xfs_log_priv.h"
  33#include "xfs_log_recover.h"
  34#include "xfs_inode_item.h"
  35#include "xfs_extfree_item.h"
  36#include "xfs_trans_priv.h"
  37#include "xfs_alloc.h"
  38#include "xfs_ialloc.h"
  39#include "xfs_quota.h"
  40#include "xfs_cksum.h"
  41#include "xfs_trace.h"
  42#include "xfs_icache.h"
  43#include "xfs_bmap_btree.h"
  44#include "xfs_error.h"
  45#include "xfs_dir2.h"
  46#include "xfs_rmap_item.h"
  47#include "xfs_buf_item.h"
  48#include "xfs_refcount_item.h"
  49#include "xfs_bmap_item.h"
  50
  51#define BLK_AVG(blk1, blk2)	((blk1+blk2) >> 1)
  52
  53STATIC int
  54xlog_find_zeroed(
  55	struct xlog	*,
  56	xfs_daddr_t	*);
  57STATIC int
  58xlog_clear_stale_blocks(
  59	struct xlog	*,
  60	xfs_lsn_t);
  61#if defined(DEBUG)
  62STATIC void
  63xlog_recover_check_summary(
  64	struct xlog *);
  65#else
  66#define	xlog_recover_check_summary(log)
  67#endif
  68STATIC int
  69xlog_do_recovery_pass(
  70        struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
  71
  72/*
  73 * This structure is used during recovery to record the buf log items which
  74 * have been canceled and should not be replayed.
  75 */
  76struct xfs_buf_cancel {
  77	xfs_daddr_t		bc_blkno;
  78	uint			bc_len;
  79	int			bc_refcount;
  80	struct list_head	bc_list;
  81};
  82
  83/*
  84 * Sector aligned buffer routines for buffer create/read/write/access
  85 */
  86
  87/*
  88 * Verify the log-relative block number and length in basic blocks are valid for
  89 * an operation involving the given XFS log buffer. Returns true if the fields
  90 * are valid, false otherwise.
  91 */
  92static inline bool
  93xlog_verify_bp(
  94	struct xlog	*log,
  95	xfs_daddr_t	blk_no,
  96	int		bbcount)
  97{
  98	if (blk_no < 0 || blk_no >= log->l_logBBsize)
  99		return false;
 100	if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
 101		return false;
 102	return true;
 103}
 104
 105/*
 106 * Allocate a buffer to hold log data.  The buffer needs to be able
 107 * to map to a range of nbblks basic blocks at any valid (basic
 108 * block) offset within the log.
 109 */
 110STATIC xfs_buf_t *
 111xlog_get_bp(
 112	struct xlog	*log,
 113	int		nbblks)
 114{
 115	struct xfs_buf	*bp;
 116
 117	/*
 118	 * Pass log block 0 since we don't have an addr yet, buffer will be
 119	 * verified on read.
 120	 */
 121	if (!xlog_verify_bp(log, 0, nbblks)) {
 122		xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
 123			nbblks);
 124		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
 125		return NULL;
 126	}
 127
 128	/*
 129	 * We do log I/O in units of log sectors (a power-of-2
 130	 * multiple of the basic block size), so we round up the
 131	 * requested size to accommodate the basic blocks required
 132	 * for complete log sectors.
 133	 *
 134	 * In addition, the buffer may be used for a non-sector-
 135	 * aligned block offset, in which case an I/O of the
 136	 * requested size could extend beyond the end of the
 137	 * buffer.  If the requested size is only 1 basic block it
 138	 * will never straddle a sector boundary, so this won't be
 139	 * an issue.  Nor will this be a problem if the log I/O is
 140	 * done in basic blocks (sector size 1).  But otherwise we
 141	 * extend the buffer by one extra log sector to ensure
 142	 * there's space to accommodate this possibility.
 143	 */
 144	if (nbblks > 1 && log->l_sectBBsize > 1)
 145		nbblks += log->l_sectBBsize;
 146	nbblks = round_up(nbblks, log->l_sectBBsize);
 147
 148	bp = xfs_buf_get_uncached(log->l_mp->m_logdev_targp, nbblks, 0);
 149	if (bp)
 150		xfs_buf_unlock(bp);
 151	return bp;
 152}
 153
 154STATIC void
 155xlog_put_bp(
 156	xfs_buf_t	*bp)
 157{
 158	xfs_buf_free(bp);
 159}
 160
 161/*
 162 * Return the address of the start of the given block number's data
 163 * in a log buffer.  The buffer covers a log sector-aligned region.
 164 */
 165STATIC char *
 166xlog_align(
 167	struct xlog	*log,
 168	xfs_daddr_t	blk_no,
 169	int		nbblks,
 170	struct xfs_buf	*bp)
 171{
 172	xfs_daddr_t	offset = blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1);
 173
 174	ASSERT(offset + nbblks <= bp->b_length);
 175	return bp->b_addr + BBTOB(offset);
 176}
 177
 178
 179/*
 180 * nbblks should be uint, but oh well.  Just want to catch that 32-bit length.
 181 */
 182STATIC int
 183xlog_bread_noalign(
 184	struct xlog	*log,
 185	xfs_daddr_t	blk_no,
 186	int		nbblks,
 187	struct xfs_buf	*bp)
 188{
 189	int		error;
 190
 191	if (!xlog_verify_bp(log, blk_no, nbblks)) {
 192		xfs_warn(log->l_mp,
 193			 "Invalid log block/length (0x%llx, 0x%x) for buffer",
 194			 blk_no, nbblks);
 195		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
 196		return -EFSCORRUPTED;
 197	}
 198
 199	blk_no = round_down(blk_no, log->l_sectBBsize);
 200	nbblks = round_up(nbblks, log->l_sectBBsize);
 201
 202	ASSERT(nbblks > 0);
 203	ASSERT(nbblks <= bp->b_length);
 204
 205	XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
 206	bp->b_flags |= XBF_READ;
 207	bp->b_io_length = nbblks;
 208	bp->b_error = 0;
 209
 210	error = xfs_buf_submit_wait(bp);
 211	if (error && !XFS_FORCED_SHUTDOWN(log->l_mp))
 212		xfs_buf_ioerror_alert(bp, __func__);
 213	return error;
 214}
 215
 216STATIC int
 217xlog_bread(
 218	struct xlog	*log,
 219	xfs_daddr_t	blk_no,
 220	int		nbblks,
 221	struct xfs_buf	*bp,
 222	char		**offset)
 223{
 224	int		error;
 225
 226	error = xlog_bread_noalign(log, blk_no, nbblks, bp);
 227	if (error)
 228		return error;
 229
 230	*offset = xlog_align(log, blk_no, nbblks, bp);
 231	return 0;
 232}
 233
 234/*
 235 * Read at an offset into the buffer. Returns with the buffer in it's original
 236 * state regardless of the result of the read.
 237 */
 238STATIC int
 239xlog_bread_offset(
 240	struct xlog	*log,
 241	xfs_daddr_t	blk_no,		/* block to read from */
 242	int		nbblks,		/* blocks to read */
 243	struct xfs_buf	*bp,
 244	char		*offset)
 245{
 246	char		*orig_offset = bp->b_addr;
 247	int		orig_len = BBTOB(bp->b_length);
 248	int		error, error2;
 249
 250	error = xfs_buf_associate_memory(bp, offset, BBTOB(nbblks));
 251	if (error)
 252		return error;
 253
 254	error = xlog_bread_noalign(log, blk_no, nbblks, bp);
 255
 256	/* must reset buffer pointer even on error */
 257	error2 = xfs_buf_associate_memory(bp, orig_offset, orig_len);
 258	if (error)
 259		return error;
 260	return error2;
 261}
 262
 263/*
 264 * Write out the buffer at the given block for the given number of blocks.
 265 * The buffer is kept locked across the write and is returned locked.
 266 * This can only be used for synchronous log writes.
 267 */
 268STATIC int
 269xlog_bwrite(
 270	struct xlog	*log,
 271	xfs_daddr_t	blk_no,
 272	int		nbblks,
 273	struct xfs_buf	*bp)
 274{
 275	int		error;
 276
 277	if (!xlog_verify_bp(log, blk_no, nbblks)) {
 278		xfs_warn(log->l_mp,
 279			 "Invalid log block/length (0x%llx, 0x%x) for buffer",
 280			 blk_no, nbblks);
 281		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
 282		return -EFSCORRUPTED;
 283	}
 284
 285	blk_no = round_down(blk_no, log->l_sectBBsize);
 286	nbblks = round_up(nbblks, log->l_sectBBsize);
 287
 288	ASSERT(nbblks > 0);
 289	ASSERT(nbblks <= bp->b_length);
 290
 291	XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
 292	xfs_buf_hold(bp);
 293	xfs_buf_lock(bp);
 294	bp->b_io_length = nbblks;
 295	bp->b_error = 0;
 296
 297	error = xfs_bwrite(bp);
 298	if (error)
 299		xfs_buf_ioerror_alert(bp, __func__);
 300	xfs_buf_relse(bp);
 301	return error;
 302}
 303
 304#ifdef DEBUG
 305/*
 306 * dump debug superblock and log record information
 307 */
 308STATIC void
 309xlog_header_check_dump(
 310	xfs_mount_t		*mp,
 311	xlog_rec_header_t	*head)
 312{
 313	xfs_debug(mp, "%s:  SB : uuid = %pU, fmt = %d",
 314		__func__, &mp->m_sb.sb_uuid, XLOG_FMT);
 315	xfs_debug(mp, "    log : uuid = %pU, fmt = %d",
 316		&head->h_fs_uuid, be32_to_cpu(head->h_fmt));
 317}
 318#else
 319#define xlog_header_check_dump(mp, head)
 320#endif
 321
 322/*
 323 * check log record header for recovery
 324 */
 325STATIC int
 326xlog_header_check_recover(
 327	xfs_mount_t		*mp,
 328	xlog_rec_header_t	*head)
 329{
 330	ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
 331
 332	/*
 333	 * IRIX doesn't write the h_fmt field and leaves it zeroed
 334	 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
 335	 * a dirty log created in IRIX.
 336	 */
 337	if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
 338		xfs_warn(mp,
 339	"dirty log written in incompatible format - can't recover");
 340		xlog_header_check_dump(mp, head);
 341		XFS_ERROR_REPORT("xlog_header_check_recover(1)",
 342				 XFS_ERRLEVEL_HIGH, mp);
 343		return -EFSCORRUPTED;
 344	} else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
 345		xfs_warn(mp,
 346	"dirty log entry has mismatched uuid - can't recover");
 347		xlog_header_check_dump(mp, head);
 348		XFS_ERROR_REPORT("xlog_header_check_recover(2)",
 349				 XFS_ERRLEVEL_HIGH, mp);
 350		return -EFSCORRUPTED;
 351	}
 352	return 0;
 353}
 354
 355/*
 356 * read the head block of the log and check the header
 357 */
 358STATIC int
 359xlog_header_check_mount(
 360	xfs_mount_t		*mp,
 361	xlog_rec_header_t	*head)
 362{
 363	ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
 364
 365	if (uuid_is_null(&head->h_fs_uuid)) {
 366		/*
 367		 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
 368		 * h_fs_uuid is null, we assume this log was last mounted
 369		 * by IRIX and continue.
 370		 */
 371		xfs_warn(mp, "null uuid in log - IRIX style log");
 372	} else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
 373		xfs_warn(mp, "log has mismatched uuid - can't recover");
 374		xlog_header_check_dump(mp, head);
 375		XFS_ERROR_REPORT("xlog_header_check_mount",
 376				 XFS_ERRLEVEL_HIGH, mp);
 377		return -EFSCORRUPTED;
 378	}
 379	return 0;
 380}
 381
 382STATIC void
 383xlog_recover_iodone(
 384	struct xfs_buf	*bp)
 385{
 386	if (bp->b_error) {
 387		/*
 388		 * We're not going to bother about retrying
 389		 * this during recovery. One strike!
 390		 */
 391		if (!XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
 392			xfs_buf_ioerror_alert(bp, __func__);
 393			xfs_force_shutdown(bp->b_target->bt_mount,
 394						SHUTDOWN_META_IO_ERROR);
 395		}
 396	}
 397
 398	/*
 399	 * On v5 supers, a bli could be attached to update the metadata LSN.
 400	 * Clean it up.
 401	 */
 402	if (bp->b_fspriv)
 403		xfs_buf_item_relse(bp);
 404	ASSERT(bp->b_fspriv == NULL);
 405
 406	bp->b_iodone = NULL;
 407	xfs_buf_ioend(bp);
 408}
 409
 410/*
 411 * This routine finds (to an approximation) the first block in the physical
 412 * log which contains the given cycle.  It uses a binary search algorithm.
 413 * Note that the algorithm can not be perfect because the disk will not
 414 * necessarily be perfect.
 415 */
 416STATIC int
 417xlog_find_cycle_start(
 418	struct xlog	*log,
 419	struct xfs_buf	*bp,
 420	xfs_daddr_t	first_blk,
 421	xfs_daddr_t	*last_blk,
 422	uint		cycle)
 423{
 424	char		*offset;
 425	xfs_daddr_t	mid_blk;
 426	xfs_daddr_t	end_blk;
 427	uint		mid_cycle;
 428	int		error;
 429
 430	end_blk = *last_blk;
 431	mid_blk = BLK_AVG(first_blk, end_blk);
 432	while (mid_blk != first_blk && mid_blk != end_blk) {
 433		error = xlog_bread(log, mid_blk, 1, bp, &offset);
 434		if (error)
 435			return error;
 436		mid_cycle = xlog_get_cycle(offset);
 437		if (mid_cycle == cycle)
 438			end_blk = mid_blk;   /* last_half_cycle == mid_cycle */
 439		else
 440			first_blk = mid_blk; /* first_half_cycle == mid_cycle */
 441		mid_blk = BLK_AVG(first_blk, end_blk);
 442	}
 443	ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
 444	       (mid_blk == end_blk && mid_blk-1 == first_blk));
 445
 446	*last_blk = end_blk;
 447
 448	return 0;
 449}
 450
 451/*
 452 * Check that a range of blocks does not contain stop_on_cycle_no.
 453 * Fill in *new_blk with the block offset where such a block is
 454 * found, or with -1 (an invalid block number) if there is no such
 455 * block in the range.  The scan needs to occur from front to back
 456 * and the pointer into the region must be updated since a later
 457 * routine will need to perform another test.
 458 */
 459STATIC int
 460xlog_find_verify_cycle(
 461	struct xlog	*log,
 462	xfs_daddr_t	start_blk,
 463	int		nbblks,
 464	uint		stop_on_cycle_no,
 465	xfs_daddr_t	*new_blk)
 466{
 467	xfs_daddr_t	i, j;
 468	uint		cycle;
 469	xfs_buf_t	*bp;
 470	xfs_daddr_t	bufblks;
 471	char		*buf = NULL;
 472	int		error = 0;
 473
 474	/*
 475	 * Greedily allocate a buffer big enough to handle the full
 476	 * range of basic blocks we'll be examining.  If that fails,
 477	 * try a smaller size.  We need to be able to read at least
 478	 * a log sector, or we're out of luck.
 479	 */
 480	bufblks = 1 << ffs(nbblks);
 481	while (bufblks > log->l_logBBsize)
 482		bufblks >>= 1;
 483	while (!(bp = xlog_get_bp(log, bufblks))) {
 484		bufblks >>= 1;
 485		if (bufblks < log->l_sectBBsize)
 486			return -ENOMEM;
 487	}
 488
 489	for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
 490		int	bcount;
 491
 492		bcount = min(bufblks, (start_blk + nbblks - i));
 493
 494		error = xlog_bread(log, i, bcount, bp, &buf);
 495		if (error)
 496			goto out;
 497
 498		for (j = 0; j < bcount; j++) {
 499			cycle = xlog_get_cycle(buf);
 500			if (cycle == stop_on_cycle_no) {
 501				*new_blk = i+j;
 502				goto out;
 503			}
 504
 505			buf += BBSIZE;
 506		}
 507	}
 508
 509	*new_blk = -1;
 510
 511out:
 512	xlog_put_bp(bp);
 513	return error;
 514}
 515
 516/*
 517 * Potentially backup over partial log record write.
 518 *
 519 * In the typical case, last_blk is the number of the block directly after
 520 * a good log record.  Therefore, we subtract one to get the block number
 521 * of the last block in the given buffer.  extra_bblks contains the number
 522 * of blocks we would have read on a previous read.  This happens when the
 523 * last log record is split over the end of the physical log.
 524 *
 525 * extra_bblks is the number of blocks potentially verified on a previous
 526 * call to this routine.
 527 */
 528STATIC int
 529xlog_find_verify_log_record(
 530	struct xlog		*log,
 531	xfs_daddr_t		start_blk,
 532	xfs_daddr_t		*last_blk,
 533	int			extra_bblks)
 534{
 535	xfs_daddr_t		i;
 536	xfs_buf_t		*bp;
 537	char			*offset = NULL;
 538	xlog_rec_header_t	*head = NULL;
 539	int			error = 0;
 540	int			smallmem = 0;
 541	int			num_blks = *last_blk - start_blk;
 542	int			xhdrs;
 543
 544	ASSERT(start_blk != 0 || *last_blk != start_blk);
 545
 546	if (!(bp = xlog_get_bp(log, num_blks))) {
 547		if (!(bp = xlog_get_bp(log, 1)))
 548			return -ENOMEM;
 549		smallmem = 1;
 550	} else {
 551		error = xlog_bread(log, start_blk, num_blks, bp, &offset);
 552		if (error)
 553			goto out;
 554		offset += ((num_blks - 1) << BBSHIFT);
 555	}
 556
 557	for (i = (*last_blk) - 1; i >= 0; i--) {
 558		if (i < start_blk) {
 559			/* valid log record not found */
 560			xfs_warn(log->l_mp,
 561		"Log inconsistent (didn't find previous header)");
 562			ASSERT(0);
 563			error = -EIO;
 564			goto out;
 565		}
 566
 567		if (smallmem) {
 568			error = xlog_bread(log, i, 1, bp, &offset);
 569			if (error)
 570				goto out;
 571		}
 572
 573		head = (xlog_rec_header_t *)offset;
 574
 575		if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
 576			break;
 577
 578		if (!smallmem)
 579			offset -= BBSIZE;
 580	}
 581
 582	/*
 583	 * We hit the beginning of the physical log & still no header.  Return
 584	 * to caller.  If caller can handle a return of -1, then this routine
 585	 * will be called again for the end of the physical log.
 586	 */
 587	if (i == -1) {
 588		error = 1;
 589		goto out;
 590	}
 591
 592	/*
 593	 * We have the final block of the good log (the first block
 594	 * of the log record _before_ the head. So we check the uuid.
 595	 */
 596	if ((error = xlog_header_check_mount(log->l_mp, head)))
 597		goto out;
 598
 599	/*
 600	 * We may have found a log record header before we expected one.
 601	 * last_blk will be the 1st block # with a given cycle #.  We may end
 602	 * up reading an entire log record.  In this case, we don't want to
 603	 * reset last_blk.  Only when last_blk points in the middle of a log
 604	 * record do we update last_blk.
 605	 */
 606	if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
 607		uint	h_size = be32_to_cpu(head->h_size);
 608
 609		xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
 610		if (h_size % XLOG_HEADER_CYCLE_SIZE)
 611			xhdrs++;
 612	} else {
 613		xhdrs = 1;
 614	}
 615
 616	if (*last_blk - i + extra_bblks !=
 617	    BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
 618		*last_blk = i;
 619
 620out:
 621	xlog_put_bp(bp);
 622	return error;
 623}
 624
 625/*
 626 * Head is defined to be the point of the log where the next log write
 627 * could go.  This means that incomplete LR writes at the end are
 628 * eliminated when calculating the head.  We aren't guaranteed that previous
 629 * LR have complete transactions.  We only know that a cycle number of
 630 * current cycle number -1 won't be present in the log if we start writing
 631 * from our current block number.
 632 *
 633 * last_blk contains the block number of the first block with a given
 634 * cycle number.
 635 *
 636 * Return: zero if normal, non-zero if error.
 637 */
 638STATIC int
 639xlog_find_head(
 640	struct xlog	*log,
 641	xfs_daddr_t	*return_head_blk)
 642{
 643	xfs_buf_t	*bp;
 644	char		*offset;
 645	xfs_daddr_t	new_blk, first_blk, start_blk, last_blk, head_blk;
 646	int		num_scan_bblks;
 647	uint		first_half_cycle, last_half_cycle;
 648	uint		stop_on_cycle;
 649	int		error, log_bbnum = log->l_logBBsize;
 650
 651	/* Is the end of the log device zeroed? */
 652	error = xlog_find_zeroed(log, &first_blk);
 653	if (error < 0) {
 654		xfs_warn(log->l_mp, "empty log check failed");
 655		return error;
 656	}
 657	if (error == 1) {
 658		*return_head_blk = first_blk;
 659
 660		/* Is the whole lot zeroed? */
 661		if (!first_blk) {
 662			/* Linux XFS shouldn't generate totally zeroed logs -
 663			 * mkfs etc write a dummy unmount record to a fresh
 664			 * log so we can store the uuid in there
 665			 */
 666			xfs_warn(log->l_mp, "totally zeroed log");
 667		}
 668
 669		return 0;
 670	}
 671
 672	first_blk = 0;			/* get cycle # of 1st block */
 673	bp = xlog_get_bp(log, 1);
 674	if (!bp)
 675		return -ENOMEM;
 676
 677	error = xlog_bread(log, 0, 1, bp, &offset);
 678	if (error)
 679		goto bp_err;
 680
 681	first_half_cycle = xlog_get_cycle(offset);
 682
 683	last_blk = head_blk = log_bbnum - 1;	/* get cycle # of last block */
 684	error = xlog_bread(log, last_blk, 1, bp, &offset);
 685	if (error)
 686		goto bp_err;
 687
 688	last_half_cycle = xlog_get_cycle(offset);
 689	ASSERT(last_half_cycle != 0);
 690
 691	/*
 692	 * If the 1st half cycle number is equal to the last half cycle number,
 693	 * then the entire log is stamped with the same cycle number.  In this
 694	 * case, head_blk can't be set to zero (which makes sense).  The below
 695	 * math doesn't work out properly with head_blk equal to zero.  Instead,
 696	 * we set it to log_bbnum which is an invalid block number, but this
 697	 * value makes the math correct.  If head_blk doesn't changed through
 698	 * all the tests below, *head_blk is set to zero at the very end rather
 699	 * than log_bbnum.  In a sense, log_bbnum and zero are the same block
 700	 * in a circular file.
 701	 */
 702	if (first_half_cycle == last_half_cycle) {
 703		/*
 704		 * In this case we believe that the entire log should have
 705		 * cycle number last_half_cycle.  We need to scan backwards
 706		 * from the end verifying that there are no holes still
 707		 * containing last_half_cycle - 1.  If we find such a hole,
 708		 * then the start of that hole will be the new head.  The
 709		 * simple case looks like
 710		 *        x | x ... | x - 1 | x
 711		 * Another case that fits this picture would be
 712		 *        x | x + 1 | x ... | x
 713		 * In this case the head really is somewhere at the end of the
 714		 * log, as one of the latest writes at the beginning was
 715		 * incomplete.
 716		 * One more case is
 717		 *        x | x + 1 | x ... | x - 1 | x
 718		 * This is really the combination of the above two cases, and
 719		 * the head has to end up at the start of the x-1 hole at the
 720		 * end of the log.
 721		 *
 722		 * In the 256k log case, we will read from the beginning to the
 723		 * end of the log and search for cycle numbers equal to x-1.
 724		 * We don't worry about the x+1 blocks that we encounter,
 725		 * because we know that they cannot be the head since the log
 726		 * started with x.
 727		 */
 728		head_blk = log_bbnum;
 729		stop_on_cycle = last_half_cycle - 1;
 730	} else {
 731		/*
 732		 * In this case we want to find the first block with cycle
 733		 * number matching last_half_cycle.  We expect the log to be
 734		 * some variation on
 735		 *        x + 1 ... | x ... | x
 736		 * The first block with cycle number x (last_half_cycle) will
 737		 * be where the new head belongs.  First we do a binary search
 738		 * for the first occurrence of last_half_cycle.  The binary
 739		 * search may not be totally accurate, so then we scan back
 740		 * from there looking for occurrences of last_half_cycle before
 741		 * us.  If that backwards scan wraps around the beginning of
 742		 * the log, then we look for occurrences of last_half_cycle - 1
 743		 * at the end of the log.  The cases we're looking for look
 744		 * like
 745		 *                               v binary search stopped here
 746		 *        x + 1 ... | x | x + 1 | x ... | x
 747		 *                   ^ but we want to locate this spot
 748		 * or
 749		 *        <---------> less than scan distance
 750		 *        x + 1 ... | x ... | x - 1 | x
 751		 *                           ^ we want to locate this spot
 752		 */
 753		stop_on_cycle = last_half_cycle;
 754		if ((error = xlog_find_cycle_start(log, bp, first_blk,
 755						&head_blk, last_half_cycle)))
 756			goto bp_err;
 757	}
 758
 759	/*
 760	 * Now validate the answer.  Scan back some number of maximum possible
 761	 * blocks and make sure each one has the expected cycle number.  The
 762	 * maximum is determined by the total possible amount of buffering
 763	 * in the in-core log.  The following number can be made tighter if
 764	 * we actually look at the block size of the filesystem.
 765	 */
 766	num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
 767	if (head_blk >= num_scan_bblks) {
 768		/*
 769		 * We are guaranteed that the entire check can be performed
 770		 * in one buffer.
 771		 */
 772		start_blk = head_blk - num_scan_bblks;
 773		if ((error = xlog_find_verify_cycle(log,
 774						start_blk, num_scan_bblks,
 775						stop_on_cycle, &new_blk)))
 776			goto bp_err;
 777		if (new_blk != -1)
 778			head_blk = new_blk;
 779	} else {		/* need to read 2 parts of log */
 780		/*
 781		 * We are going to scan backwards in the log in two parts.
 782		 * First we scan the physical end of the log.  In this part
 783		 * of the log, we are looking for blocks with cycle number
 784		 * last_half_cycle - 1.
 785		 * If we find one, then we know that the log starts there, as
 786		 * we've found a hole that didn't get written in going around
 787		 * the end of the physical log.  The simple case for this is
 788		 *        x + 1 ... | x ... | x - 1 | x
 789		 *        <---------> less than scan distance
 790		 * If all of the blocks at the end of the log have cycle number
 791		 * last_half_cycle, then we check the blocks at the start of
 792		 * the log looking for occurrences of last_half_cycle.  If we
 793		 * find one, then our current estimate for the location of the
 794		 * first occurrence of last_half_cycle is wrong and we move
 795		 * back to the hole we've found.  This case looks like
 796		 *        x + 1 ... | x | x + 1 | x ...
 797		 *                               ^ binary search stopped here
 798		 * Another case we need to handle that only occurs in 256k
 799		 * logs is
 800		 *        x + 1 ... | x ... | x+1 | x ...
 801		 *                   ^ binary search stops here
 802		 * In a 256k log, the scan at the end of the log will see the
 803		 * x + 1 blocks.  We need to skip past those since that is
 804		 * certainly not the head of the log.  By searching for
 805		 * last_half_cycle-1 we accomplish that.
 806		 */
 807		ASSERT(head_blk <= INT_MAX &&
 808			(xfs_daddr_t) num_scan_bblks >= head_blk);
 809		start_blk = log_bbnum - (num_scan_bblks - head_blk);
 810		if ((error = xlog_find_verify_cycle(log, start_blk,
 811					num_scan_bblks - (int)head_blk,
 812					(stop_on_cycle - 1), &new_blk)))
 813			goto bp_err;
 814		if (new_blk != -1) {
 815			head_blk = new_blk;
 816			goto validate_head;
 817		}
 818
 819		/*
 820		 * Scan beginning of log now.  The last part of the physical
 821		 * log is good.  This scan needs to verify that it doesn't find
 822		 * the last_half_cycle.
 823		 */
 824		start_blk = 0;
 825		ASSERT(head_blk <= INT_MAX);
 826		if ((error = xlog_find_verify_cycle(log,
 827					start_blk, (int)head_blk,
 828					stop_on_cycle, &new_blk)))
 829			goto bp_err;
 830		if (new_blk != -1)
 831			head_blk = new_blk;
 832	}
 833
 834validate_head:
 835	/*
 836	 * Now we need to make sure head_blk is not pointing to a block in
 837	 * the middle of a log record.
 838	 */
 839	num_scan_bblks = XLOG_REC_SHIFT(log);
 840	if (head_blk >= num_scan_bblks) {
 841		start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
 842
 843		/* start ptr at last block ptr before head_blk */
 844		error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
 845		if (error == 1)
 846			error = -EIO;
 847		if (error)
 848			goto bp_err;
 849	} else {
 850		start_blk = 0;
 851		ASSERT(head_blk <= INT_MAX);
 852		error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
 853		if (error < 0)
 854			goto bp_err;
 855		if (error == 1) {
 856			/* We hit the beginning of the log during our search */
 857			start_blk = log_bbnum - (num_scan_bblks - head_blk);
 858			new_blk = log_bbnum;
 859			ASSERT(start_blk <= INT_MAX &&
 860				(xfs_daddr_t) log_bbnum-start_blk >= 0);
 861			ASSERT(head_blk <= INT_MAX);
 862			error = xlog_find_verify_log_record(log, start_blk,
 863							&new_blk, (int)head_blk);
 864			if (error == 1)
 865				error = -EIO;
 866			if (error)
 867				goto bp_err;
 868			if (new_blk != log_bbnum)
 869				head_blk = new_blk;
 870		} else if (error)
 871			goto bp_err;
 872	}
 873
 874	xlog_put_bp(bp);
 875	if (head_blk == log_bbnum)
 876		*return_head_blk = 0;
 877	else
 878		*return_head_blk = head_blk;
 879	/*
 880	 * When returning here, we have a good block number.  Bad block
 881	 * means that during a previous crash, we didn't have a clean break
 882	 * from cycle number N to cycle number N-1.  In this case, we need
 883	 * to find the first block with cycle number N-1.
 884	 */
 885	return 0;
 886
 887 bp_err:
 888	xlog_put_bp(bp);
 889
 890	if (error)
 891		xfs_warn(log->l_mp, "failed to find log head");
 892	return error;
 893}
 894
 895/*
 896 * Seek backwards in the log for log record headers.
 897 *
 898 * Given a starting log block, walk backwards until we find the provided number
 899 * of records or hit the provided tail block. The return value is the number of
 900 * records encountered or a negative error code. The log block and buffer
 901 * pointer of the last record seen are returned in rblk and rhead respectively.
 902 */
 903STATIC int
 904xlog_rseek_logrec_hdr(
 905	struct xlog		*log,
 906	xfs_daddr_t		head_blk,
 907	xfs_daddr_t		tail_blk,
 908	int			count,
 909	struct xfs_buf		*bp,
 910	xfs_daddr_t		*rblk,
 911	struct xlog_rec_header	**rhead,
 912	bool			*wrapped)
 913{
 914	int			i;
 915	int			error;
 916	int			found = 0;
 917	char			*offset = NULL;
 918	xfs_daddr_t		end_blk;
 919
 920	*wrapped = false;
 921
 922	/*
 923	 * Walk backwards from the head block until we hit the tail or the first
 924	 * block in the log.
 925	 */
 926	end_blk = head_blk > tail_blk ? tail_blk : 0;
 927	for (i = (int) head_blk - 1; i >= end_blk; i--) {
 928		error = xlog_bread(log, i, 1, bp, &offset);
 929		if (error)
 930			goto out_error;
 931
 932		if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
 933			*rblk = i;
 934			*rhead = (struct xlog_rec_header *) offset;
 935			if (++found == count)
 936				break;
 937		}
 938	}
 939
 940	/*
 941	 * If we haven't hit the tail block or the log record header count,
 942	 * start looking again from the end of the physical log. Note that
 943	 * callers can pass head == tail if the tail is not yet known.
 944	 */
 945	if (tail_blk >= head_blk && found != count) {
 946		for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
 947			error = xlog_bread(log, i, 1, bp, &offset);
 948			if (error)
 949				goto out_error;
 950
 951			if (*(__be32 *)offset ==
 952			    cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
 953				*wrapped = true;
 954				*rblk = i;
 955				*rhead = (struct xlog_rec_header *) offset;
 956				if (++found == count)
 957					break;
 958			}
 959		}
 960	}
 961
 962	return found;
 963
 964out_error:
 965	return error;
 966}
 967
 968/*
 969 * Seek forward in the log for log record headers.
 970 *
 971 * Given head and tail blocks, walk forward from the tail block until we find
 972 * the provided number of records or hit the head block. The return value is the
 973 * number of records encountered or a negative error code. The log block and
 974 * buffer pointer of the last record seen are returned in rblk and rhead
 975 * respectively.
 976 */
 977STATIC int
 978xlog_seek_logrec_hdr(
 979	struct xlog		*log,
 980	xfs_daddr_t		head_blk,
 981	xfs_daddr_t		tail_blk,
 982	int			count,
 983	struct xfs_buf		*bp,
 984	xfs_daddr_t		*rblk,
 985	struct xlog_rec_header	**rhead,
 986	bool			*wrapped)
 987{
 988	int			i;
 989	int			error;
 990	int			found = 0;
 991	char			*offset = NULL;
 992	xfs_daddr_t		end_blk;
 993
 994	*wrapped = false;
 995
 996	/*
 997	 * Walk forward from the tail block until we hit the head or the last
 998	 * block in the log.
 999	 */
1000	end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
1001	for (i = (int) tail_blk; i <= end_blk; i++) {
1002		error = xlog_bread(log, i, 1, bp, &offset);
1003		if (error)
1004			goto out_error;
1005
1006		if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
1007			*rblk = i;
1008			*rhead = (struct xlog_rec_header *) offset;
1009			if (++found == count)
1010				break;
1011		}
1012	}
1013
1014	/*
1015	 * If we haven't hit the head block or the log record header count,
1016	 * start looking again from the start of the physical log.
1017	 */
1018	if (tail_blk > head_blk && found != count) {
1019		for (i = 0; i < (int) head_blk; i++) {
1020			error = xlog_bread(log, i, 1, bp, &offset);
1021			if (error)
1022				goto out_error;
1023
1024			if (*(__be32 *)offset ==
1025			    cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
1026				*wrapped = true;
1027				*rblk = i;
1028				*rhead = (struct xlog_rec_header *) offset;
1029				if (++found == count)
1030					break;
1031			}
1032		}
1033	}
1034
1035	return found;
1036
1037out_error:
1038	return error;
1039}
1040
1041/*
1042 * Calculate distance from head to tail (i.e., unused space in the log).
1043 */
1044static inline int
1045xlog_tail_distance(
1046	struct xlog	*log,
1047	xfs_daddr_t	head_blk,
1048	xfs_daddr_t	tail_blk)
1049{
1050	if (head_blk < tail_blk)
1051		return tail_blk - head_blk;
1052
1053	return tail_blk + (log->l_logBBsize - head_blk);
1054}
1055
1056/*
1057 * Verify the log tail. This is particularly important when torn or incomplete
1058 * writes have been detected near the front of the log and the head has been
1059 * walked back accordingly.
1060 *
1061 * We also have to handle the case where the tail was pinned and the head
1062 * blocked behind the tail right before a crash. If the tail had been pushed
1063 * immediately prior to the crash and the subsequent checkpoint was only
1064 * partially written, it's possible it overwrote the last referenced tail in the
1065 * log with garbage. This is not a coherency problem because the tail must have
1066 * been pushed before it can be overwritten, but appears as log corruption to
1067 * recovery because we have no way to know the tail was updated if the
1068 * subsequent checkpoint didn't write successfully.
1069 *
1070 * Therefore, CRC check the log from tail to head. If a failure occurs and the
1071 * offending record is within max iclog bufs from the head, walk the tail
1072 * forward and retry until a valid tail is found or corruption is detected out
1073 * of the range of a possible overwrite.
1074 */
1075STATIC int
1076xlog_verify_tail(
1077	struct xlog		*log,
1078	xfs_daddr_t		head_blk,
1079	xfs_daddr_t		*tail_blk,
1080	int			hsize)
1081{
1082	struct xlog_rec_header	*thead;
1083	struct xfs_buf		*bp;
1084	xfs_daddr_t		first_bad;
1085	int			error = 0;
1086	bool			wrapped;
1087	xfs_daddr_t		tmp_tail;
1088	xfs_daddr_t		orig_tail = *tail_blk;
1089
1090	bp = xlog_get_bp(log, 1);
1091	if (!bp)
1092		return -ENOMEM;
1093
1094	/*
1095	 * Make sure the tail points to a record (returns positive count on
1096	 * success).
1097	 */
1098	error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, bp,
1099			&tmp_tail, &thead, &wrapped);
1100	if (error < 0)
1101		goto out;
1102	if (*tail_blk != tmp_tail)
1103		*tail_blk = tmp_tail;
1104
1105	/*
1106	 * Run a CRC check from the tail to the head. We can't just check
1107	 * MAX_ICLOGS records past the tail because the tail may point to stale
1108	 * blocks cleared during the search for the head/tail. These blocks are
1109	 * overwritten with zero-length records and thus record count is not a
1110	 * reliable indicator of the iclog state before a crash.
1111	 */
1112	first_bad = 0;
1113	error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
1114				      XLOG_RECOVER_CRCPASS, &first_bad);
1115	while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
1116		int	tail_distance;
1117
1118		/*
1119		 * Is corruption within range of the head? If so, retry from
1120		 * the next record. Otherwise return an error.
1121		 */
1122		tail_distance = xlog_tail_distance(log, head_blk, first_bad);
1123		if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
1124			break;
1125
1126		/* skip to the next record; returns positive count on success */
1127		error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2, bp,
1128				&tmp_tail, &thead, &wrapped);
1129		if (error < 0)
1130			goto out;
1131
1132		*tail_blk = tmp_tail;
1133		first_bad = 0;
1134		error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
1135					      XLOG_RECOVER_CRCPASS, &first_bad);
1136	}
1137
1138	if (!error && *tail_blk != orig_tail)
1139		xfs_warn(log->l_mp,
1140		"Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
1141			 orig_tail, *tail_blk);
1142out:
1143	xlog_put_bp(bp);
1144	return error;
1145}
1146
1147/*
1148 * Detect and trim torn writes from the head of the log.
1149 *
1150 * Storage without sector atomicity guarantees can result in torn writes in the
1151 * log in the event of a crash. Our only means to detect this scenario is via
1152 * CRC verification. While we can't always be certain that CRC verification
1153 * failure is due to a torn write vs. an unrelated corruption, we do know that
1154 * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
1155 * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
1156 * the log and treat failures in this range as torn writes as a matter of
1157 * policy. In the event of CRC failure, the head is walked back to the last good
1158 * record in the log and the tail is updated from that record and verified.
1159 */
1160STATIC int
1161xlog_verify_head(
1162	struct xlog		*log,
1163	xfs_daddr_t		*head_blk,	/* in/out: unverified head */
1164	xfs_daddr_t		*tail_blk,	/* out: tail block */
1165	struct xfs_buf		*bp,
1166	xfs_daddr_t		*rhead_blk,	/* start blk of last record */
1167	struct xlog_rec_header	**rhead,	/* ptr to last record */
1168	bool			*wrapped)	/* last rec. wraps phys. log */
1169{
1170	struct xlog_rec_header	*tmp_rhead;
1171	struct xfs_buf		*tmp_bp;
1172	xfs_daddr_t		first_bad;
1173	xfs_daddr_t		tmp_rhead_blk;
1174	int			found;
1175	int			error;
1176	bool			tmp_wrapped;
1177
1178	/*
1179	 * Check the head of the log for torn writes. Search backwards from the
1180	 * head until we hit the tail or the maximum number of log record I/Os
1181	 * that could have been in flight at one time. Use a temporary buffer so
1182	 * we don't trash the rhead/bp pointers from the caller.
1183	 */
1184	tmp_bp = xlog_get_bp(log, 1);
1185	if (!tmp_bp)
1186		return -ENOMEM;
1187	error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
1188				      XLOG_MAX_ICLOGS, tmp_bp, &tmp_rhead_blk,
1189				      &tmp_rhead, &tmp_wrapped);
1190	xlog_put_bp(tmp_bp);
1191	if (error < 0)
1192		return error;
1193
1194	/*
1195	 * Now run a CRC verification pass over the records starting at the
1196	 * block found above to the current head. If a CRC failure occurs, the
1197	 * log block of the first bad record is saved in first_bad.
1198	 */
1199	error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
1200				      XLOG_RECOVER_CRCPASS, &first_bad);
1201	if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
1202		/*
1203		 * We've hit a potential torn write. Reset the error and warn
1204		 * about it.
1205		 */
1206		error = 0;
1207		xfs_warn(log->l_mp,
1208"Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
1209			 first_bad, *head_blk);
1210
1211		/*
1212		 * Get the header block and buffer pointer for the last good
1213		 * record before the bad record.
1214		 *
1215		 * Note that xlog_find_tail() clears the blocks at the new head
1216		 * (i.e., the records with invalid CRC) if the cycle number
1217		 * matches the the current cycle.
1218		 */
1219		found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1, bp,
1220					      rhead_blk, rhead, wrapped);
1221		if (found < 0)
1222			return found;
1223		if (found == 0)		/* XXX: right thing to do here? */
1224			return -EIO;
1225
1226		/*
1227		 * Reset the head block to the starting block of the first bad
1228		 * log record and set the tail block based on the last good
1229		 * record.
1230		 *
1231		 * Bail out if the updated head/tail match as this indicates
1232		 * possible corruption outside of the acceptable
1233		 * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
1234		 */
1235		*head_blk = first_bad;
1236		*tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
1237		if (*head_blk == *tail_blk) {
1238			ASSERT(0);
1239			return 0;
1240		}
1241	}
1242	if (error)
1243		return error;
1244
1245	return xlog_verify_tail(log, *head_blk, tail_blk,
1246				be32_to_cpu((*rhead)->h_size));
1247}
1248
1249/*
1250 * Check whether the head of the log points to an unmount record. In other
1251 * words, determine whether the log is clean. If so, update the in-core state
1252 * appropriately.
1253 */
1254static int
1255xlog_check_unmount_rec(
1256	struct xlog		*log,
1257	xfs_daddr_t		*head_blk,
1258	xfs_daddr_t		*tail_blk,
1259	struct xlog_rec_header	*rhead,
1260	xfs_daddr_t		rhead_blk,
1261	struct xfs_buf		*bp,
1262	bool			*clean)
1263{
1264	struct xlog_op_header	*op_head;
1265	xfs_daddr_t		umount_data_blk;
1266	xfs_daddr_t		after_umount_blk;
1267	int			hblks;
1268	int			error;
1269	char			*offset;
1270
1271	*clean = false;
1272
1273	/*
1274	 * Look for unmount record. If we find it, then we know there was a
1275	 * clean unmount. Since 'i' could be the last block in the physical
1276	 * log, we convert to a log block before comparing to the head_blk.
1277	 *
1278	 * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
1279	 * below. We won't want to clear the unmount record if there is one, so
1280	 * we pass the lsn of the unmount record rather than the block after it.
1281	 */
1282	if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1283		int	h_size = be32_to_cpu(rhead->h_size);
1284		int	h_version = be32_to_cpu(rhead->h_version);
1285
1286		if ((h_version & XLOG_VERSION_2) &&
1287		    (h_size > XLOG_HEADER_CYCLE_SIZE)) {
1288			hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
1289			if (h_size % XLOG_HEADER_CYCLE_SIZE)
1290				hblks++;
1291		} else {
1292			hblks = 1;
1293		}
1294	} else {
1295		hblks = 1;
1296	}
1297	after_umount_blk = rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len));
1298	after_umount_blk = do_mod(after_umount_blk, log->l_logBBsize);
1299	if (*head_blk == after_umount_blk &&
1300	    be32_to_cpu(rhead->h_num_logops) == 1) {
1301		umount_data_blk = rhead_blk + hblks;
1302		umount_data_blk = do_mod(umount_data_blk, log->l_logBBsize);
1303		error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
1304		if (error)
1305			return error;
1306
1307		op_head = (struct xlog_op_header *)offset;
1308		if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
1309			/*
1310			 * Set tail and last sync so that newly written log
1311			 * records will point recovery to after the current
1312			 * unmount record.
1313			 */
1314			xlog_assign_atomic_lsn(&log->l_tail_lsn,
1315					log->l_curr_cycle, after_umount_blk);
1316			xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
1317					log->l_curr_cycle, after_umount_blk);
1318			*tail_blk = after_umount_blk;
1319
1320			*clean = true;
1321		}
1322	}
1323
1324	return 0;
1325}
1326
1327static void
1328xlog_set_state(
1329	struct xlog		*log,
1330	xfs_daddr_t		head_blk,
1331	struct xlog_rec_header	*rhead,
1332	xfs_daddr_t		rhead_blk,
1333	bool			bump_cycle)
1334{
1335	/*
1336	 * Reset log values according to the state of the log when we
1337	 * crashed.  In the case where head_blk == 0, we bump curr_cycle
1338	 * one because the next write starts a new cycle rather than
1339	 * continuing the cycle of the last good log record.  At this
1340	 * point we have guaranteed that all partial log records have been
1341	 * accounted for.  Therefore, we know that the last good log record
1342	 * written was complete and ended exactly on the end boundary
1343	 * of the physical log.
1344	 */
1345	log->l_prev_block = rhead_blk;
1346	log->l_curr_block = (int)head_blk;
1347	log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
1348	if (bump_cycle)
1349		log->l_curr_cycle++;
1350	atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
1351	atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
1352	xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
1353					BBTOB(log->l_curr_block));
1354	xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
1355					BBTOB(log->l_curr_block));
1356}
1357
1358/*
1359 * Find the sync block number or the tail of the log.
1360 *
1361 * This will be the block number of the last record to have its
1362 * associated buffers synced to disk.  Every log record header has
1363 * a sync lsn embedded in it.  LSNs hold block numbers, so it is easy
1364 * to get a sync block number.  The only concern is to figure out which
1365 * log record header to believe.
1366 *
1367 * The following algorithm uses the log record header with the largest
1368 * lsn.  The entire log record does not need to be valid.  We only care
1369 * that the header is valid.
1370 *
1371 * We could speed up search by using current head_blk buffer, but it is not
1372 * available.
1373 */
1374STATIC int
1375xlog_find_tail(
1376	struct xlog		*log,
1377	xfs_daddr_t		*head_blk,
1378	xfs_daddr_t		*tail_blk)
1379{
1380	xlog_rec_header_t	*rhead;
1381	char			*offset = NULL;
1382	xfs_buf_t		*bp;
1383	int			error;
1384	xfs_daddr_t		rhead_blk;
1385	xfs_lsn_t		tail_lsn;
1386	bool			wrapped = false;
1387	bool			clean = false;
1388
1389	/*
1390	 * Find previous log record
1391	 */
1392	if ((error = xlog_find_head(log, head_blk)))
1393		return error;
1394	ASSERT(*head_blk < INT_MAX);
1395
1396	bp = xlog_get_bp(log, 1);
1397	if (!bp)
1398		return -ENOMEM;
1399	if (*head_blk == 0) {				/* special case */
1400		error = xlog_bread(log, 0, 1, bp, &offset);
1401		if (error)
1402			goto done;
1403
1404		if (xlog_get_cycle(offset) == 0) {
1405			*tail_blk = 0;
1406			/* leave all other log inited values alone */
1407			goto done;
1408		}
1409	}
1410
1411	/*
1412	 * Search backwards through the log looking for the log record header
1413	 * block. This wraps all the way back around to the head so something is
1414	 * seriously wrong if we can't find it.
1415	 */
1416	error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, bp,
1417				      &rhead_blk, &rhead, &wrapped);
1418	if (error < 0)
1419		return error;
1420	if (!error) {
1421		xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
1422		return -EIO;
1423	}
1424	*tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
1425
1426	/*
1427	 * Set the log state based on the current head record.
1428	 */
1429	xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
1430	tail_lsn = atomic64_read(&log->l_tail_lsn);
1431
1432	/*
1433	 * Look for an unmount record at the head of the log. This sets the log
1434	 * state to determine whether recovery is necessary.
1435	 */
1436	error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
1437				       rhead_blk, bp, &clean);
1438	if (error)
1439		goto done;
1440
1441	/*
1442	 * Verify the log head if the log is not clean (e.g., we have anything
1443	 * but an unmount record at the head). This uses CRC verification to
1444	 * detect and trim torn writes. If discovered, CRC failures are
1445	 * considered torn writes and the log head is trimmed accordingly.
1446	 *
1447	 * Note that we can only run CRC verification when the log is dirty
1448	 * because there's no guarantee that the log data behind an unmount
1449	 * record is compatible with the current architecture.
1450	 */
1451	if (!clean) {
1452		xfs_daddr_t	orig_head = *head_blk;
1453
1454		error = xlog_verify_head(log, head_blk, tail_blk, bp,
1455					 &rhead_blk, &rhead, &wrapped);
1456		if (error)
1457			goto done;
1458
1459		/* update in-core state again if the head changed */
1460		if (*head_blk != orig_head) {
1461			xlog_set_state(log, *head_blk, rhead, rhead_blk,
1462				       wrapped);
1463			tail_lsn = atomic64_read(&log->l_tail_lsn);
1464			error = xlog_check_unmount_rec(log, head_blk, tail_blk,
1465						       rhead, rhead_blk, bp,
1466						       &clean);
1467			if (error)
1468				goto done;
1469		}
1470	}
1471
1472	/*
1473	 * Note that the unmount was clean. If the unmount was not clean, we
1474	 * need to know this to rebuild the superblock counters from the perag
1475	 * headers if we have a filesystem using non-persistent counters.
1476	 */
1477	if (clean)
1478		log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
1479
1480	/*
1481	 * Make sure that there are no blocks in front of the head
1482	 * with the same cycle number as the head.  This can happen
1483	 * because we allow multiple outstanding log writes concurrently,
1484	 * and the later writes might make it out before earlier ones.
1485	 *
1486	 * We use the lsn from before modifying it so that we'll never
1487	 * overwrite the unmount record after a clean unmount.
1488	 *
1489	 * Do this only if we are going to recover the filesystem
1490	 *
1491	 * NOTE: This used to say "if (!readonly)"
1492	 * However on Linux, we can & do recover a read-only filesystem.
1493	 * We only skip recovery if NORECOVERY is specified on mount,
1494	 * in which case we would not be here.
1495	 *
1496	 * But... if the -device- itself is readonly, just skip this.
1497	 * We can't recover this device anyway, so it won't matter.
1498	 */
1499	if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp))
1500		error = xlog_clear_stale_blocks(log, tail_lsn);
1501
1502done:
1503	xlog_put_bp(bp);
1504
1505	if (error)
1506		xfs_warn(log->l_mp, "failed to locate log tail");
1507	return error;
1508}
1509
1510/*
1511 * Is the log zeroed at all?
1512 *
1513 * The last binary search should be changed to perform an X block read
1514 * once X becomes small enough.  You can then search linearly through
1515 * the X blocks.  This will cut down on the number of reads we need to do.
1516 *
1517 * If the log is partially zeroed, this routine will pass back the blkno
1518 * of the first block with cycle number 0.  It won't have a complete LR
1519 * preceding it.
1520 *
1521 * Return:
1522 *	0  => the log is completely written to
1523 *	1 => use *blk_no as the first block of the log
1524 *	<0 => error has occurred
1525 */
1526STATIC int
1527xlog_find_zeroed(
1528	struct xlog	*log,
1529	xfs_daddr_t	*blk_no)
1530{
1531	xfs_buf_t	*bp;
1532	char		*offset;
1533	uint	        first_cycle, last_cycle;
1534	xfs_daddr_t	new_blk, last_blk, start_blk;
1535	xfs_daddr_t     num_scan_bblks;
1536	int	        error, log_bbnum = log->l_logBBsize;
1537
1538	*blk_no = 0;
1539
1540	/* check totally zeroed log */
1541	bp = xlog_get_bp(log, 1);
1542	if (!bp)
1543		return -ENOMEM;
1544	error = xlog_bread(log, 0, 1, bp, &offset);
1545	if (error)
1546		goto bp_err;
1547
1548	first_cycle = xlog_get_cycle(offset);
1549	if (first_cycle == 0) {		/* completely zeroed log */
1550		*blk_no = 0;
1551		xlog_put_bp(bp);
1552		return 1;
1553	}
1554
1555	/* check partially zeroed log */
1556	error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
1557	if (error)
1558		goto bp_err;
1559
1560	last_cycle = xlog_get_cycle(offset);
1561	if (last_cycle != 0) {		/* log completely written to */
1562		xlog_put_bp(bp);
1563		return 0;
1564	} else if (first_cycle != 1) {
1565		/*
1566		 * If the cycle of the last block is zero, the cycle of
1567		 * the first block must be 1. If it's not, maybe we're
1568		 * not looking at a log... Bail out.
1569		 */
1570		xfs_warn(log->l_mp,
1571			"Log inconsistent or not a log (last==0, first!=1)");
1572		error = -EINVAL;
1573		goto bp_err;
1574	}
1575
1576	/* we have a partially zeroed log */
1577	last_blk = log_bbnum-1;
1578	if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
1579		goto bp_err;
1580
1581	/*
1582	 * Validate the answer.  Because there is no way to guarantee that
1583	 * the entire log is made up of log records which are the same size,
1584	 * we scan over the defined maximum blocks.  At this point, the maximum
1585	 * is not chosen to mean anything special.   XXXmiken
1586	 */
1587	num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1588	ASSERT(num_scan_bblks <= INT_MAX);
1589
1590	if (last_blk < num_scan_bblks)
1591		num_scan_bblks = last_blk;
1592	start_blk = last_blk - num_scan_bblks;
1593
1594	/*
1595	 * We search for any instances of cycle number 0 that occur before
1596	 * our current estimate of the head.  What we're trying to detect is
1597	 *        1 ... | 0 | 1 | 0...
1598	 *                       ^ binary search ends here
1599	 */
1600	if ((error = xlog_find_verify_cycle(log, start_blk,
1601					 (int)num_scan_bblks, 0, &new_blk)))
1602		goto bp_err;
1603	if (new_blk != -1)
1604		last_blk = new_blk;
1605
1606	/*
1607	 * Potentially backup over partial log record write.  We don't need
1608	 * to search the end of the log because we know it is zero.
1609	 */
1610	error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
1611	if (error == 1)
1612		error = -EIO;
1613	if (error)
1614		goto bp_err;
1615
1616	*blk_no = last_blk;
1617bp_err:
1618	xlog_put_bp(bp);
1619	if (error)
1620		return error;
1621	return 1;
1622}
1623
1624/*
1625 * These are simple subroutines used by xlog_clear_stale_blocks() below
1626 * to initialize a buffer full of empty log record headers and write
1627 * them into the log.
1628 */
1629STATIC void
1630xlog_add_record(
1631	struct xlog		*log,
1632	char			*buf,
1633	int			cycle,
1634	int			block,
1635	int			tail_cycle,
1636	int			tail_block)
1637{
1638	xlog_rec_header_t	*recp = (xlog_rec_header_t *)buf;
1639
1640	memset(buf, 0, BBSIZE);
1641	recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1642	recp->h_cycle = cpu_to_be32(cycle);
1643	recp->h_version = cpu_to_be32(
1644			xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1645	recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1646	recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1647	recp->h_fmt = cpu_to_be32(XLOG_FMT);
1648	memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1649}
1650
1651STATIC int
1652xlog_write_log_records(
1653	struct xlog	*log,
1654	int		cycle,
1655	int		start_block,
1656	int		blocks,
1657	int		tail_cycle,
1658	int		tail_block)
1659{
1660	char		*offset;
1661	xfs_buf_t	*bp;
1662	int		balign, ealign;
1663	int		sectbb = log->l_sectBBsize;
1664	int		end_block = start_block + blocks;
1665	int		bufblks;
1666	int		error = 0;
1667	int		i, j = 0;
1668
1669	/*
1670	 * Greedily allocate a buffer big enough to handle the full
1671	 * range of basic blocks to be written.  If that fails, try
1672	 * a smaller size.  We need to be able to write at least a
1673	 * log sector, or we're out of luck.
1674	 */
1675	bufblks = 1 << ffs(blocks);
1676	while (bufblks > log->l_logBBsize)
1677		bufblks >>= 1;
1678	while (!(bp = xlog_get_bp(log, bufblks))) {
1679		bufblks >>= 1;
1680		if (bufblks < sectbb)
1681			return -ENOMEM;
1682	}
1683
1684	/* We may need to do a read at the start to fill in part of
1685	 * the buffer in the starting sector not covered by the first
1686	 * write below.
1687	 */
1688	balign = round_down(start_block, sectbb);
1689	if (balign != start_block) {
1690		error = xlog_bread_noalign(log, start_block, 1, bp);
1691		if (error)
1692			goto out_put_bp;
1693
1694		j = start_block - balign;
1695	}
1696
1697	for (i = start_block; i < end_block; i += bufblks) {
1698		int		bcount, endcount;
1699
1700		bcount = min(bufblks, end_block - start_block);
1701		endcount = bcount - j;
1702
1703		/* We may need to do a read at the end to fill in part of
1704		 * the buffer in the final sector not covered by the write.
1705		 * If this is the same sector as the above read, skip it.
1706		 */
1707		ealign = round_down(end_block, sectbb);
1708		if (j == 0 && (start_block + endcount > ealign)) {
1709			offset = bp->b_addr + BBTOB(ealign - start_block);
1710			error = xlog_bread_offset(log, ealign, sectbb,
1711							bp, offset);
1712			if (error)
1713				break;
1714
1715		}
1716
1717		offset = xlog_align(log, start_block, endcount, bp);
1718		for (; j < endcount; j++) {
1719			xlog_add_record(log, offset, cycle, i+j,
1720					tail_cycle, tail_block);
1721			offset += BBSIZE;
1722		}
1723		error = xlog_bwrite(log, start_block, endcount, bp);
1724		if (error)
1725			break;
1726		start_block += endcount;
1727		j = 0;
1728	}
1729
1730 out_put_bp:
1731	xlog_put_bp(bp);
1732	return error;
1733}
1734
1735/*
1736 * This routine is called to blow away any incomplete log writes out
1737 * in front of the log head.  We do this so that we won't become confused
1738 * if we come up, write only a little bit more, and then crash again.
1739 * If we leave the partial log records out there, this situation could
1740 * cause us to think those partial writes are valid blocks since they
1741 * have the current cycle number.  We get rid of them by overwriting them
1742 * with empty log records with the old cycle number rather than the
1743 * current one.
1744 *
1745 * The tail lsn is passed in rather than taken from
1746 * the log so that we will not write over the unmount record after a
1747 * clean unmount in a 512 block log.  Doing so would leave the log without
1748 * any valid log records in it until a new one was written.  If we crashed
1749 * during that time we would not be able to recover.
1750 */
1751STATIC int
1752xlog_clear_stale_blocks(
1753	struct xlog	*log,
1754	xfs_lsn_t	tail_lsn)
1755{
1756	int		tail_cycle, head_cycle;
1757	int		tail_block, head_block;
1758	int		

Large files files are truncated, but you can click here to view the full file