Blame gfs2/libgfs2/recovery.c

Packit 6ef888
#include "clusterautoconfig.h"
Packit 6ef888
Packit 6ef888
/*
Packit 6ef888
 * NOTE:
Packit 6ef888
 *
Packit 6ef888
 * This code was pilfered from the gfs2 kernel and adapted to userland.
Packit 6ef888
 * If you change this part, you should evaluate whether the upstream kernel
Packit 6ef888
 * version of recovery.c should be changed as well.  Likewise, if the
Packit 6ef888
 * upstream version changes, this part should be kept in sync.
Packit 6ef888
 * 
Packit 6ef888
 */
Packit 6ef888
Packit 6ef888
#include <errno.h>
Packit 6ef888
#include <string.h>
Packit 6ef888
#include "libgfs2.h"
Packit 6ef888
Packit 6ef888
void gfs2_replay_incr_blk(struct gfs2_inode *ip, unsigned int *blk)
Packit 6ef888
{
Packit 6ef888
	uint32_t jd_blocks = ip->i_di.di_size / ip->i_sbd->sd_sb.sb_bsize;
Packit 6ef888
Packit 6ef888
        if (++*blk == jd_blocks)
Packit 6ef888
                *blk = 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
int gfs2_replay_read_block(struct gfs2_inode *ip, unsigned int blk,
Packit 6ef888
			   struct gfs2_buffer_head **bh)
Packit 6ef888
{
Packit 6ef888
	int new = 0;
Packit 6ef888
	uint64_t dblock;
Packit 6ef888
Packit 6ef888
	block_map(ip, blk, &new, &dblock, NULL, FALSE);
Packit 6ef888
	if (!dblock)
Packit 6ef888
		return -EIO;
Packit 6ef888
Packit 6ef888
	*bh = bread(ip->i_sbd, dblock);
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * get_log_header - read the log header for a given segment
Packit 6ef888
 * @ip: the journal incore inode
Packit 6ef888
 * @blk: the block to look at
Packit 6ef888
 * @lh: the log header to return
Packit 6ef888
 *
Packit 6ef888
 * Read the log header for a given segement in a given journal.  Do a few
Packit 6ef888
 * sanity checks on it.
Packit 6ef888
 *
Packit 6ef888
 * Returns: 0 on success,
Packit 6ef888
 *          1 if the header was invalid or incomplete,
Packit 6ef888
 *          errno on error
Packit 6ef888
 */
Packit 6ef888
Packit 6ef888
int get_log_header(struct gfs2_inode *ip, unsigned int blk,
Packit 6ef888
		   struct gfs2_log_header *head)
Packit 6ef888
{
Packit 6ef888
	struct gfs2_buffer_head *bh;
Packit 6ef888
	struct gfs2_log_header lh, *tmp;
Packit 6ef888
	uint32_t hash, saved_hash;
Packit 6ef888
	uint32_t lh_crc = 0;
Packit 6ef888
	uint32_t crc;
Packit 6ef888
	int error;
Packit 6ef888
Packit 6ef888
	error = gfs2_replay_read_block(ip, blk, &bh;;
Packit 6ef888
	if (error)
Packit 6ef888
		return error;
Packit 6ef888
Packit 6ef888
	tmp = (struct gfs2_log_header *)bh->b_data;
Packit 6ef888
	saved_hash = tmp->lh_hash;
Packit 6ef888
	tmp->lh_hash = 0;
Packit 6ef888
	hash = lgfs2_log_header_hash(bh->b_data);
Packit 6ef888
	tmp->lh_hash = saved_hash;
Packit 6ef888
	crc = lgfs2_log_header_crc(bh->b_data, ip->i_sbd->bsize);
Packit 6ef888
	gfs2_log_header_in(&lh, bh->b_data);
Packit 6ef888
	brelse(bh);
Packit 6ef888
#ifdef GFS2_HAS_LH_V2
Packit 6ef888
	lh_crc = lh.lh_crc;
Packit 6ef888
#endif
Packit 6ef888
	if (error || lh.lh_blkno != blk || lh.lh_hash != hash)
Packit 6ef888
		return 1;
Packit 6ef888
	/* Don't check the crc if it's zero, as it is in pre-v2 log headers */
Packit 6ef888
	if (lh_crc != 0 && lh_crc != crc)
Packit 6ef888
		return 1;
Packit 6ef888
Packit 6ef888
	*head = lh;
Packit 6ef888
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * find_good_lh - find a good log header
Packit 6ef888
 * @ip: the journal incore inode
Packit 6ef888
 * @blk: the segment to start searching from
Packit 6ef888
 * @lh: the log header to fill in
Packit 6ef888
 * @forward: if true search forward in the log, else search backward
Packit 6ef888
 *
Packit 6ef888
 * Call get_log_header() to get a log header for a segment, but if the
Packit 6ef888
 * segment is bad, either scan forward or backward until we find a good one.
Packit 6ef888
 *
Packit 6ef888
 * Returns: errno
Packit 6ef888
 */
Packit 6ef888
static int find_good_lh(struct gfs2_inode *ip, unsigned int *blk, struct gfs2_log_header *head)
Packit 6ef888
{
Packit 6ef888
	unsigned int orig_blk = *blk;
Packit 6ef888
	int error;
Packit 6ef888
	uint32_t jd_blocks = ip->i_di.di_size / ip->i_sbd->sd_sb.sb_bsize;
Packit 6ef888
Packit 6ef888
	for (;;) {
Packit 6ef888
		error = get_log_header(ip, *blk, head);
Packit 6ef888
		if (error <= 0)
Packit 6ef888
			return error;
Packit 6ef888
Packit 6ef888
		if (++*blk == jd_blocks)
Packit 6ef888
			*blk = 0;
Packit 6ef888
Packit 6ef888
		if (*blk == orig_blk)
Packit 6ef888
			return -EIO;
Packit 6ef888
	}
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * jhead_scan - make sure we've found the head of the log
Packit 6ef888
 * @jd: the journal
Packit 6ef888
 * @head: this is filled in with the log descriptor of the head
Packit 6ef888
 *
Packit 6ef888
 * At this point, seg and lh should be either the head of the log or just
Packit 6ef888
 * before.  Scan forward until we find the head.
Packit 6ef888
 *
Packit 6ef888
 * Returns: errno
Packit 6ef888
 */
Packit 6ef888
Packit 6ef888
static int jhead_scan(struct gfs2_inode *ip, struct gfs2_log_header *head)
Packit 6ef888
{
Packit 6ef888
	unsigned int blk = head->lh_blkno;
Packit 6ef888
	uint32_t jd_blocks = ip->i_di.di_size / ip->i_sbd->sd_sb.sb_bsize;
Packit 6ef888
	struct gfs2_log_header lh;
Packit 6ef888
	int error;
Packit 6ef888
Packit 6ef888
	for (;;) {
Packit 6ef888
		if (++blk == jd_blocks)
Packit 6ef888
			blk = 0;
Packit 6ef888
Packit 6ef888
		error = get_log_header(ip, blk, &lh);
Packit 6ef888
		if (error < 0)
Packit 6ef888
			return error;
Packit 6ef888
		if (error == 1)
Packit 6ef888
			continue;
Packit 6ef888
Packit 6ef888
		if (lh.lh_sequence == head->lh_sequence)
Packit 6ef888
			return -EIO;
Packit 6ef888
		if (lh.lh_sequence < head->lh_sequence)
Packit 6ef888
			break;
Packit 6ef888
Packit 6ef888
		*head = lh;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * gfs2_find_jhead - find the head of a log
Packit 6ef888
 * @jd: the journal
Packit 6ef888
 * @head: the log descriptor for the head of the log is returned here
Packit 6ef888
 *
Packit 6ef888
 * Do a binary search of a journal and find the valid log entry with the
Packit 6ef888
 * highest sequence number.  (i.e. the log head)
Packit 6ef888
 *
Packit 6ef888
 * Returns: errno
Packit 6ef888
 */
Packit 6ef888
Packit 6ef888
int gfs2_find_jhead(struct gfs2_inode *ip, struct gfs2_log_header *head)
Packit 6ef888
{
Packit 6ef888
	struct gfs2_log_header lh_1, lh_m;
Packit 6ef888
	uint32_t blk_1, blk_2, blk_m;
Packit 6ef888
	uint32_t jd_blocks = ip->i_di.di_size / ip->i_sbd->sd_sb.sb_bsize;
Packit 6ef888
	int error;
Packit 6ef888
Packit 6ef888
	blk_1 = 0;
Packit 6ef888
	blk_2 = jd_blocks - 1;
Packit 6ef888
Packit 6ef888
	for (;;) {
Packit 6ef888
		blk_m = (blk_1 + blk_2) / 2;
Packit 6ef888
Packit 6ef888
		error = find_good_lh(ip, &blk_1, &lh_1);
Packit 6ef888
		if (error)
Packit 6ef888
			return error;
Packit 6ef888
Packit 6ef888
		error = find_good_lh(ip, &blk_m, &lh_m);
Packit 6ef888
		if (error)
Packit 6ef888
			return error;
Packit 6ef888
Packit 6ef888
		if (blk_1 == blk_m || blk_m == blk_2)
Packit 6ef888
			break;
Packit 6ef888
Packit 6ef888
		if (lh_1.lh_sequence <= lh_m.lh_sequence)
Packit 6ef888
			blk_1 = blk_m;
Packit 6ef888
		else
Packit 6ef888
			blk_2 = blk_m;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	error = jhead_scan(ip, &lh_1);
Packit 6ef888
	if (error)
Packit 6ef888
		return error;
Packit 6ef888
Packit 6ef888
	*head = lh_1;
Packit 6ef888
Packit 6ef888
	return error;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * clean_journal - mark a dirty journal as being clean
Packit 6ef888
 * @sdp: the filesystem
Packit 6ef888
 * @jd: the journal
Packit 6ef888
 * @head: the head journal to start from
Packit 6ef888
 *
Packit 6ef888
 * Returns: errno
Packit 6ef888
 */
Packit 6ef888
Packit 6ef888
int clean_journal(struct gfs2_inode *ip, struct gfs2_log_header *head)
Packit 6ef888
{
Packit 6ef888
	unsigned int lblock;
Packit 6ef888
	struct gfs2_log_header *lh;
Packit 6ef888
	uint32_t hash;
Packit 6ef888
	struct gfs2_buffer_head *bh;
Packit 6ef888
	int new = 0;
Packit 6ef888
	uint64_t dblock;
Packit 6ef888
Packit 6ef888
	lblock = head->lh_blkno;
Packit 6ef888
	gfs2_replay_incr_blk(ip, &lblock);
Packit 6ef888
	block_map(ip, lblock, &new, &dblock, NULL, 0);
Packit 6ef888
	if (!dblock)
Packit 6ef888
		return -EIO;
Packit 6ef888
Packit 6ef888
	bh = bread(ip->i_sbd, dblock);
Packit 6ef888
	memset(bh->b_data, 0, ip->i_sbd->bsize);
Packit 6ef888
Packit 6ef888
	lh = (struct gfs2_log_header *)bh->b_data;
Packit 6ef888
	memset(lh, 0, sizeof(struct gfs2_log_header));
Packit 6ef888
	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Packit 6ef888
	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Packit 6ef888
	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Packit 6ef888
	lh->lh_sequence = cpu_to_be64(head->lh_sequence + 1);
Packit 6ef888
	lh->lh_flags = cpu_to_be32(GFS2_LOG_HEAD_UNMOUNT);
Packit 6ef888
	lh->lh_blkno = cpu_to_be32(lblock);
Packit Service 395ff1
	hash = gfs2_disk_hash((const char *)lh, sizeof(struct gfs2_log_header));
Packit 6ef888
	lh->lh_hash = cpu_to_be32(hash);
Packit 6ef888
	bmodified(bh);
Packit 6ef888
	brelse(bh);
Packit 6ef888
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888