PageRenderTime 89ms CodeModel.GetById 5ms app.highlight 65ms RepoModel.GetById 0ms app.codeStats 2ms

/fs/xfs/xfs_log_recover.c

https://bitbucket.org/emiliolopez/linux
C | 5829 lines | 3640 code | 561 blank | 1628 comment | 673 complexity | 175fc7830e2a6de277b7ad8431160061 MD5 | raw file

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

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