Blame gfs2/libgfs2/recovery.c.bz1659490-gfs2_utils_Wrong_hash_value_used_to_clean_journals

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