recovery.c 13.0 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
D
David Teigland 已提交
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
D
David Teigland 已提交
8 9 10 11 12 13
 */

#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
14
#include <linux/gfs2_ondisk.h>
15
#include <linux/crc32.h>
16
#include <linux/lm_interface.h>
D
David Teigland 已提交
17 18

#include "gfs2.h"
19
#include "incore.h"
D
David Teigland 已提交
20 21 22 23 24 25 26 27
#include "bmap.h"
#include "glock.h"
#include "glops.h"
#include "lm.h"
#include "lops.h"
#include "meta_io.h"
#include "recovery.h"
#include "super.h"
28
#include "util.h"
29
#include "dir.h"
D
David Teigland 已提交
30 31 32 33

int gfs2_replay_read_block(struct gfs2_jdesc *jd, unsigned int blk,
			   struct buffer_head **bh)
{
34
	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
35
	struct gfs2_glock *gl = ip->i_gl;
D
David Teigland 已提交
36
	int new = 0;
37 38
	u64 dblock;
	u32 extlen;
D
David Teigland 已提交
39 40
	int error;

41
	error = gfs2_extent_map(&ip->i_inode, blk, &new, &dblock, &extlen);
D
David Teigland 已提交
42 43 44
	if (error)
		return error;
	if (!dblock) {
45
		gfs2_consist_inode(ip);
D
David Teigland 已提交
46 47 48
		return -EIO;
	}

S
Steven Whitehouse 已提交
49
	*bh = gfs2_meta_ra(gl, dblock, extlen);
D
David Teigland 已提交
50 51 52 53

	return error;
}

54
int gfs2_revoke_add(struct gfs2_sbd *sdp, u64 blkno, unsigned int where)
D
David Teigland 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
	struct list_head *head = &sdp->sd_revoke_list;
	struct gfs2_revoke_replay *rr;
	int found = 0;

	list_for_each_entry(rr, head, rr_list) {
		if (rr->rr_blkno == blkno) {
			found = 1;
			break;
		}
	}

	if (found) {
		rr->rr_where = where;
		return 0;
	}

	rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_KERNEL);
	if (!rr)
		return -ENOMEM;

	rr->rr_blkno = blkno;
	rr->rr_where = where;
	list_add(&rr->rr_list, head);

	return 1;
}

83
int gfs2_revoke_check(struct gfs2_sbd *sdp, u64 blkno, unsigned int where)
D
David Teigland 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
{
	struct gfs2_revoke_replay *rr;
	int wrap, a, b, revoke;
	int found = 0;

	list_for_each_entry(rr, &sdp->sd_revoke_list, rr_list) {
		if (rr->rr_blkno == blkno) {
			found = 1;
			break;
		}
	}

	if (!found)
		return 0;

	wrap = (rr->rr_where < sdp->sd_replay_tail);
	a = (sdp->sd_replay_tail < where);
	b = (where < rr->rr_where);
	revoke = (wrap) ? (a || b) : (a && b);

	return revoke;
}

void gfs2_revoke_clean(struct gfs2_sbd *sdp)
{
	struct list_head *head = &sdp->sd_revoke_list;
	struct gfs2_revoke_replay *rr;

	while (!list_empty(head)) {
		rr = list_entry(head->next, struct gfs2_revoke_replay, rr_list);
		list_del(&rr->rr_list);
		kfree(rr);
	}
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
static int gfs2_log_header_in(struct gfs2_log_header_host *lh, const void *buf)
{
	const struct gfs2_log_header *str = buf;

	if (str->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
	    str->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH))
		return 1;

	lh->lh_sequence = be64_to_cpu(str->lh_sequence);
	lh->lh_flags = be32_to_cpu(str->lh_flags);
	lh->lh_tail = be32_to_cpu(str->lh_tail);
	lh->lh_blkno = be32_to_cpu(str->lh_blkno);
	lh->lh_hash = be32_to_cpu(str->lh_hash);
	return 0;
}

D
David Teigland 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/**
 * get_log_header - read the log header for a given segment
 * @jd: the journal
 * @blk: the block to look at
 * @lh: the log header to return
 *
 * Read the log header for a given segement in a given journal.  Do a few
 * sanity checks on it.
 *
 * Returns: 0 on success,
 *          1 if the header was invalid or incomplete,
 *          errno on error
 */

static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
A
Al Viro 已提交
150
			  struct gfs2_log_header_host *head)
D
David Teigland 已提交
151 152
{
	struct buffer_head *bh;
A
Al Viro 已提交
153
	struct gfs2_log_header_host lh;
154
	const u32 nothing = 0;
155
	u32 hash;
D
David Teigland 已提交
156 157 158 159 160 161
	int error;

	error = gfs2_replay_read_block(jd, blk, &bh);
	if (error)
		return error;

162 163 164 165
	hash = crc32_le((u32)~0, bh->b_data, sizeof(struct gfs2_log_header) -
					     sizeof(u32));
	hash = crc32_le(hash, (unsigned char const *)&nothing, sizeof(nothing));
	hash ^= (u32)~0;
166
	error = gfs2_log_header_in(&lh, bh->b_data);
D
David Teigland 已提交
167 168
	brelse(bh);

169
	if (error || lh.lh_blkno != blk || lh.lh_hash != hash)
D
David Teigland 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		return 1;

	*head = lh;

	return 0;
}

/**
 * find_good_lh - find a good log header
 * @jd: the journal
 * @blk: the segment to start searching from
 * @lh: the log header to fill in
 * @forward: if true search forward in the log, else search backward
 *
 * Call get_log_header() to get a log header for a segment, but if the
 * segment is bad, either scan forward or backward until we find a good one.
 *
 * Returns: errno
 */

static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk,
A
Al Viro 已提交
191
			struct gfs2_log_header_host *head)
D
David Teigland 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
{
	unsigned int orig_blk = *blk;
	int error;

	for (;;) {
		error = get_log_header(jd, *blk, head);
		if (error <= 0)
			return error;

		if (++*blk == jd->jd_blocks)
			*blk = 0;

		if (*blk == orig_blk) {
205
			gfs2_consist_inode(GFS2_I(jd->jd_inode));
D
David Teigland 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
			return -EIO;
		}
	}
}

/**
 * jhead_scan - make sure we've found the head of the log
 * @jd: the journal
 * @head: this is filled in with the log descriptor of the head
 *
 * At this point, seg and lh should be either the head of the log or just
 * before.  Scan forward until we find the head.
 *
 * Returns: errno
 */

A
Al Viro 已提交
222
static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
D
David Teigland 已提交
223 224
{
	unsigned int blk = head->lh_blkno;
A
Al Viro 已提交
225
	struct gfs2_log_header_host lh;
D
David Teigland 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
	int error;

	for (;;) {
		if (++blk == jd->jd_blocks)
			blk = 0;

		error = get_log_header(jd, blk, &lh);
		if (error < 0)
			return error;
		if (error == 1)
			continue;

		if (lh.lh_sequence == head->lh_sequence) {
239
			gfs2_consist_inode(GFS2_I(jd->jd_inode));
D
David Teigland 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
			return -EIO;
		}
		if (lh.lh_sequence < head->lh_sequence)
			break;

		*head = lh;
	}

	return 0;
}

/**
 * gfs2_find_jhead - find the head of a log
 * @jd: the journal
 * @head: the log descriptor for the head of the log is returned here
 *
 * Do a binary search of a journal and find the valid log entry with the
 * highest sequence number.  (i.e. the log head)
 *
 * Returns: errno
 */

A
Al Viro 已提交
262
int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
D
David Teigland 已提交
263
{
A
Al Viro 已提交
264
	struct gfs2_log_header_host lh_1, lh_m;
265
	u32 blk_1, blk_2, blk_m;
D
David Teigland 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	int error;

	blk_1 = 0;
	blk_2 = jd->jd_blocks - 1;

	for (;;) {
		blk_m = (blk_1 + blk_2) / 2;

		error = find_good_lh(jd, &blk_1, &lh_1);
		if (error)
			return error;

		error = find_good_lh(jd, &blk_m, &lh_m);
		if (error)
			return error;

		if (blk_1 == blk_m || blk_m == blk_2)
			break;

		if (lh_1.lh_sequence <= lh_m.lh_sequence)
			blk_1 = blk_m;
		else
			blk_2 = blk_m;
	}

	error = jhead_scan(jd, &lh_1);
	if (error)
		return error;

	*head = lh_1;

	return error;
}

/**
 * foreach_descriptor - go through the active part of the log
 * @jd: the journal
 * @start: the first log header in the active region
 * @end: the last log header (don't process the contents of this entry))
 *
 * Call a given function once for every log descriptor in the active
 * portion of the log.
 *
 * Returns: errno
 */

static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start,
			      unsigned int end, int pass)
{
315
	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
D
David Teigland 已提交
316 317 318 319 320 321
	struct buffer_head *bh;
	struct gfs2_log_descriptor *ld;
	int error = 0;
	u32 length;
	__be64 *ptr;
	unsigned int offset = sizeof(struct gfs2_log_descriptor);
322 323
	offset += sizeof(__be64) - 1;
	offset &= ~(sizeof(__be64) - 1);
D
David Teigland 已提交
324 325 326 327 328 329 330 331 332 333 334 335

	while (start != end) {
		error = gfs2_replay_read_block(jd, start, &bh);
		if (error)
			return error;
		if (gfs2_meta_check(sdp, bh)) {
			brelse(bh);
			return -EIO;
		}
		ld = (struct gfs2_log_descriptor *)bh->b_data;
		length = be32_to_cpu(ld->ld_length);

336
		if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
A
Al Viro 已提交
337
			struct gfs2_log_header_host lh;
D
David Teigland 已提交
338 339 340
			error = get_log_header(jd, start, &lh);
			if (!error) {
				gfs2_replay_incr_blk(sdp, &start);
341
				brelse(bh);
D
David Teigland 已提交
342 343 344
				continue;
			}
			if (error == 1) {
345
				gfs2_consist_inode(GFS2_I(jd->jd_inode));
D
David Teigland 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
				error = -EIO;
			}
			brelse(bh);
			return error;
		} else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
			brelse(bh);
			return -EIO;
		}
		ptr = (__be64 *)(bh->b_data + offset);
		error = lops_scan_elements(jd, start, ld, ptr, pass);
		if (error) {
			brelse(bh);
			return error;
		}

		while (length--)
			gfs2_replay_incr_blk(sdp, &start);

		brelse(bh);
	}

	return 0;
}

/**
 * clean_journal - mark a dirty journal as being clean
 * @sdp: the filesystem
 * @jd: the journal
 * @gl: the journal's glock
 * @head: the head journal to start from
 *
 * Returns: errno
 */

A
Al Viro 已提交
380
static int clean_journal(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
D
David Teigland 已提交
381
{
382 383
	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
D
David Teigland 已提交
384 385
	unsigned int lblock;
	struct gfs2_log_header *lh;
386
	u32 hash;
D
David Teigland 已提交
387 388
	struct buffer_head *bh;
	int error;
389
	struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
S
Steven Whitehouse 已提交
390

D
David Teigland 已提交
391 392
	lblock = head->lh_blkno;
	gfs2_replay_incr_blk(sdp, &lblock);
393
	bh_map.b_size = 1 << ip->i_inode.i_blkbits;
394
	error = gfs2_block_map(&ip->i_inode, lblock, &bh_map, 0);
D
David Teigland 已提交
395 396
	if (error)
		return error;
397
	if (!bh_map.b_blocknr) {
D
David Teigland 已提交
398 399 400 401
		gfs2_consist_inode(ip);
		return -EIO;
	}

402
	bh = sb_getblk(sdp->sd_vfs, bh_map.b_blocknr);
D
David Teigland 已提交
403 404 405 406 407 408 409 410 411
	lock_buffer(bh);
	memset(bh->b_data, 0, bh->b_size);
	set_buffer_uptodate(bh);
	clear_buffer_dirty(bh);
	unlock_buffer(bh);

	lh = (struct gfs2_log_header *)bh->b_data;
	memset(lh, 0, sizeof(struct gfs2_log_header));
	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
412
	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
413
	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
D
David Teigland 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	lh->lh_sequence = cpu_to_be64(head->lh_sequence + 1);
	lh->lh_flags = cpu_to_be32(GFS2_LOG_HEAD_UNMOUNT);
	lh->lh_blkno = cpu_to_be32(lblock);
	hash = gfs2_disk_hash((const char *)lh, sizeof(struct gfs2_log_header));
	lh->lh_hash = cpu_to_be32(hash);

	set_buffer_dirty(bh);
	if (sync_dirty_buffer(bh))
		gfs2_io_error_bh(sdp, bh);
	brelse(bh);

	return error;
}

/**
 * gfs2_recover_journal - recovery a given journal
 * @jd: the struct gfs2_jdesc describing the journal
 *
 * Acquire the journal's lock, check to see if the journal is clean, and
 * do recovery if necessary.
 *
 * Returns: errno
 */

D
David Teigland 已提交
438
int gfs2_recover_journal(struct gfs2_jdesc *jd)
D
David Teigland 已提交
439
{
440 441
	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
A
Al Viro 已提交
442
	struct gfs2_log_header_host head;
D
David Teigland 已提交
443 444 445 446 447 448
	struct gfs2_holder j_gh, ji_gh, t_gh;
	unsigned long t;
	int ro = 0;
	unsigned int pass;
	int error;

449 450 451
	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
		fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
			jd->jd_jid);
D
David Teigland 已提交
452

453
		/* Aquire the journal lock so we can do recovery */
D
David Teigland 已提交
454

455 456 457 458 459 460 461
		error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
					  LM_ST_EXCLUSIVE,
					  LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
					  &j_gh);
		switch (error) {
		case 0:
			break;
462

463 464 465
		case GLR_TRYFAILED:
			fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
			error = 0;
466

467 468 469
		default:
			goto fail;
		};
D
David Teigland 已提交
470

471
		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
472
					   LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
473 474 475 476 477
		if (error)
			goto fail_gunlock_j;
	} else {
		fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
	}
D
David Teigland 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

	fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);

	error = gfs2_jdesc_check(jd);
	if (error)
		goto fail_gunlock_ji;

	error = gfs2_find_jhead(jd, &head);
	if (error)
		goto fail_gunlock_ji;

	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
		fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
			jd->jd_jid);

		t = jiffies;

		/* Acquire a shared hold on the transaction lock */

497
		error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
498
					   LM_FLAG_NOEXP | LM_FLAG_PRIORITY |
499
					   GL_NOCANCEL | GL_NOCACHE, &t_gh);
D
David Teigland 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		if (error)
			goto fail_gunlock_ji;

		if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
			if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
				ro = 1;
		} else {
			if (sdp->sd_vfs->s_flags & MS_RDONLY)
				ro = 1;
		}

		if (ro) {
			fs_warn(sdp, "jid=%u: Can't replay: read-only FS\n",
				jd->jd_jid);
			error = -EROFS;
			goto fail_gunlock_tr;
		}

		fs_info(sdp, "jid=%u: Replaying journal...\n", jd->jd_jid);

		for (pass = 0; pass < 2; pass++) {
			lops_before_scan(jd, &head, pass);
			error = foreach_descriptor(jd, head.lh_tail,
						   head.lh_blkno, pass);
			lops_after_scan(jd, error, pass);
			if (error)
				goto fail_gunlock_tr;
		}

		error = clean_journal(jd, &head);
		if (error)
			goto fail_gunlock_tr;

		gfs2_glock_dq_uninit(&t_gh);
534
		t = DIV_ROUND_UP(jiffies - t, HZ);
D
David Teigland 已提交
535 536 537 538
		fs_info(sdp, "jid=%u: Journal replayed in %lus\n",
			jd->jd_jid, t);
	}

539 540
	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
		gfs2_glock_dq_uninit(&ji_gh);
D
David Teigland 已提交
541 542 543

	gfs2_lm_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);

544 545
	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
		gfs2_glock_dq_uninit(&j_gh);
D
David Teigland 已提交
546 547 548 549

	fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
	return 0;

550
fail_gunlock_tr:
D
David Teigland 已提交
551
	gfs2_glock_dq_uninit(&t_gh);
552 553 554 555 556 557
fail_gunlock_ji:
	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
		gfs2_glock_dq_uninit(&ji_gh);
fail_gunlock_j:
		gfs2_glock_dq_uninit(&j_gh);
	}
D
David Teigland 已提交
558 559 560

	fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");

561
fail:
D
David Teigland 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	gfs2_lm_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
	return error;
}

/**
 * gfs2_check_journals - Recover any dirty journals
 * @sdp: the filesystem
 *
 */

void gfs2_check_journals(struct gfs2_sbd *sdp)
{
	struct gfs2_jdesc *jd;

	for (;;) {
		jd = gfs2_jdesc_find_dirty(sdp);
		if (!jd)
			break;

		if (jd != sdp->sd_jdesc)
D
David Teigland 已提交
582
			gfs2_recover_journal(jd);
D
David Teigland 已提交
583 584 585
	}
}