Blame gfs2/libgfs2/fs_bits.c

Packit 6ef888
#include "clusterautoconfig.h"
Packit 6ef888
Packit 6ef888
#include <inttypes.h>
Packit 6ef888
#include <stdlib.h>
Packit 6ef888
#include <string.h>
Packit 6ef888
#include <unistd.h>
Packit 6ef888
#include <limits.h>
Packit 6ef888
Packit 6ef888
#include "libgfs2.h"
Packit 6ef888
Packit 6ef888
#if BITS_PER_LONG == 32
Packit 6ef888
#define LBITMASK   (0x55555555UL)
Packit 6ef888
#define LBITSKIP55 (0x55555555UL)
Packit 6ef888
#define LBITSKIP00 (0x00000000UL)
Packit 6ef888
#else
Packit 6ef888
#define LBITMASK   (0x5555555555555555UL)
Packit 6ef888
#define LBITSKIP55 (0x5555555555555555UL)
Packit 6ef888
#define LBITSKIP00 (0x0000000000000000UL)
Packit 6ef888
#endif
Packit 6ef888
Packit 6ef888
#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * gfs2_bit_search
Packit 6ef888
 * @ptr: Pointer to bitmap data
Packit 6ef888
 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
Packit 6ef888
 * @state: The state we are searching for
Packit 6ef888
 *
Packit 6ef888
 * We xor the bitmap data with a patter which is the bitwise opposite
Packit 6ef888
 * of what we are looking for, this gives rise to a pattern of ones
Packit 6ef888
 * wherever there is a match. Since we have two bits per entry, we
Packit 6ef888
 * take this pattern, shift it down by one place and then and it with
Packit 6ef888
 * the original. All the even bit positions (0,2,4, etc) then represent
Packit 6ef888
 * successful matches, so we mask with 0x55555..... to remove the unwanted
Packit 6ef888
 * odd bit positions.
Packit 6ef888
 *
Packit 6ef888
 * This allows searching of a whole u64 at once (32 blocks) with a
Packit 6ef888
 * single test (on 64 bit arches).
Packit 6ef888
 */
Packit 6ef888
Packit 6ef888
static inline uint64_t gfs2_bit_search(const unsigned long long *ptr,
Packit 6ef888
				       unsigned long long mask,
Packit 6ef888
				       uint8_t state)
Packit 6ef888
{
Packit 6ef888
	unsigned long long tmp;
Packit 6ef888
	static const unsigned long long search[] = {
Packit 6ef888
		[0] = 0xffffffffffffffffULL,
Packit 6ef888
		[1] = 0xaaaaaaaaaaaaaaaaULL,
Packit 6ef888
		[2] = 0x5555555555555555ULL,
Packit 6ef888
		[3] = 0x0000000000000000ULL,
Packit 6ef888
	};
Packit 6ef888
	tmp = le64_to_cpu(*ptr) ^ search[state];
Packit 6ef888
	tmp &= (tmp >> 1);
Packit 6ef888
	tmp &= mask;
Packit 6ef888
	return tmp;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/**
Packit 6ef888
 * gfs2_bitfit - Find a free block in the bitmaps
Packit 6ef888
 * @buffer: the buffer that holds the bitmaps
Packit 6ef888
 * @buflen: the length (in bytes) of the buffer
Packit 6ef888
 * @goal: the block to try to allocate
Packit 6ef888
 * @old_state: the state of the block we're looking for
Packit 6ef888
 *
Packit 6ef888
 * Return: the block number that was allocated
Packit 6ef888
 */
Packit 6ef888
unsigned long gfs2_bitfit(const unsigned char *buf, const unsigned int len,
Packit 6ef888
			  unsigned long goal, unsigned char state)
Packit 6ef888
{
Packit 6ef888
	unsigned long spoint = (goal << 1) & ((8 * sizeof(unsigned long long)) - 1);
Packit 6ef888
	const unsigned long long *ptr = ((unsigned long long *)buf) + (goal >> 5);
Packit 6ef888
	const unsigned long long *end = (unsigned long long *)
Packit 6ef888
		(buf + ALIGN(len, sizeof(unsigned long long)));
Packit 6ef888
	unsigned long long tmp;
Packit 6ef888
	unsigned long long mask = 0x5555555555555555ULL;
Packit 6ef888
	unsigned long bit;
Packit 6ef888
Packit 6ef888
	if (state > 3)
Packit 6ef888
		return 0;
Packit 6ef888
Packit 6ef888
	/* Mask off bits we don't care about at the start of the search */
Packit 6ef888
	mask <<= spoint;
Packit 6ef888
	tmp = gfs2_bit_search(ptr, mask, state);
Packit 6ef888
	ptr++;
Packit 6ef888
	while(tmp == 0 && ptr < end) {
Packit 6ef888
		tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
Packit 6ef888
		ptr++;
Packit 6ef888
	}
Packit 6ef888
	/* Mask off any bits which are more than len bytes from the start */
Packit 6ef888
	if (ptr == end && (len & (sizeof(unsigned long long) - 1)))
Packit 6ef888
		tmp &= (((unsigned long long)~0) >>
Packit 6ef888
			(64 - 8 * (len & (sizeof(unsigned long long) - 1))));
Packit 6ef888
	/* Didn't find anything, so return */
Packit 6ef888
	if (tmp == 0)
Packit 6ef888
		return BFITNOENT;
Packit 6ef888
	ptr--;
Packit 6ef888
	bit = ffsll(tmp);
Packit 6ef888
	bit /= 2;	/* two bits per entry in the bitmap */
Packit 6ef888
	return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/*
Packit 6ef888
 * check_range - check if blkno is within FS limits
Packit 6ef888
 * @sdp: super block
Packit 6ef888
 * @blkno: block number
Packit 6ef888
 *
Packit 6ef888
 * Returns: 0 if ok, -1 if out of bounds
Packit 6ef888
 */
Packit 6ef888
int gfs2_check_range(struct gfs2_sbd *sdp, uint64_t blkno)
Packit 6ef888
{
Packit 6ef888
	if((blkno > sdp->fssize) || (blkno <= LGFS2_SB_ADDR(sdp)))
Packit 6ef888
		return -1;
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/*
Packit 6ef888
 * gfs2_set_bitmap
Packit 6ef888
 * @sdp: super block
Packit 6ef888
 * @blkno: block number relative to file system
Packit 6ef888
 * @state: one of three possible states
Packit 6ef888
 *
Packit 6ef888
 * This function sets the value of a bit of the
Packit 6ef888
 * file system bitmap.
Packit 6ef888
 *
Packit 6ef888
 * Returns: 0 on success, -1 on error
Packit 6ef888
 */
Packit 6ef888
int gfs2_set_bitmap(lgfs2_rgrp_t rgd, uint64_t blkno, int state)
Packit 6ef888
{
Packit 6ef888
	int           buf;
Packit 6ef888
	uint32_t        rgrp_block;
Packit 6ef888
	struct gfs2_bitmap *bits = NULL;
Packit 6ef888
	unsigned char *byte, cur_state;
Packit 6ef888
	unsigned int bit;
Packit 6ef888
Packit 6ef888
	/* FIXME: should GFS2_BLKST_INVALID be allowed */
Packit 6ef888
	if ((state < GFS2_BLKST_FREE) || (state > GFS2_BLKST_DINODE))
Packit 6ef888
		return -1;
Packit 6ef888
Packit 6ef888
	if(!rgd || blkno < rgd->ri.ri_data0)
Packit 6ef888
		return -1;
Packit 6ef888
Packit 6ef888
	rgrp_block = (uint32_t)(blkno - rgd->ri.ri_data0);
Packit 6ef888
	for(buf= 0; buf < rgd->ri.ri_length; buf++){
Packit 6ef888
		bits = &(rgd->bits[buf]);
Packit 6ef888
		if(rgrp_block < ((bits->bi_start + bits->bi_len)*GFS2_NBBY))
Packit 6ef888
			break;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	if (bits == NULL)
Packit 6ef888
		return -1;
Packit 6ef888
	byte = (unsigned char *)(bits->bi_bh->b_data + bits->bi_offset) +
Packit 6ef888
		(rgrp_block/GFS2_NBBY - bits->bi_start);
Packit 6ef888
	bit = (rgrp_block % GFS2_NBBY) * GFS2_BIT_SIZE;
Packit 6ef888
Packit 6ef888
	cur_state = (*byte >> bit) & GFS2_BIT_MASK;
Packit 6ef888
	*byte ^= cur_state << bit;
Packit 6ef888
	*byte |= state << bit;
Packit 6ef888
Packit 6ef888
	bmodified(bits->bi_bh);
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
/*
Packit 6ef888
 * gfs2_get_bitmap - get value of FS bitmap
Packit 6ef888
 * @sdp: super block
Packit 6ef888
 * @blkno: block number relative to file system
Packit 6ef888
 *
Packit 6ef888
 * This function gets the value of a bit of the
Packit 6ef888
 * file system bitmap.
Packit 6ef888
 * Possible state values for a block in the bitmap are:
Packit 6ef888
 *  GFS_BLKST_FREE     (0)
Packit 6ef888
 *  GFS_BLKST_USED     (1)
Packit 6ef888
 *  GFS_BLKST_INVALID  (2)
Packit 6ef888
 *  GFS_BLKST_DINODE   (3)
Packit 6ef888
 *
Packit 6ef888
 * Returns: state on success, -1 on error
Packit 6ef888
 */
Packit 6ef888
int lgfs2_get_bitmap(struct gfs2_sbd *sdp, uint64_t blkno, struct rgrp_tree *rgd)
Packit 6ef888
{
Packit 6ef888
	uint64_t offset;
Packit 6ef888
	uint32_t i = 0;
Packit 6ef888
	char *byte;
Packit 6ef888
	unsigned int bit;
Packit 6ef888
	struct gfs2_bitmap *bi;
Packit 6ef888
Packit 6ef888
	if (rgd == NULL) {
Packit 6ef888
		rgd = gfs2_blk2rgrpd(sdp, blkno);
Packit 6ef888
		if(rgd == NULL)
Packit 6ef888
			return -1;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	offset = blkno - rgd->ri.ri_data0;
Packit 6ef888
	if (offset > UINT_MAX) {
Packit 6ef888
		errno = EINVAL;
Packit 6ef888
		return -1;
Packit 6ef888
	}
Packit 6ef888
	if (offset >= rgd->ri.ri_data0 + rgd->ri.ri_data) {
Packit 6ef888
		errno = E2BIG;
Packit 6ef888
		return -1;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	if (offset >= (rgd->bits->bi_start + rgd->bits->bi_len) * GFS2_NBBY) {
Packit 6ef888
		offset += (sizeof(struct gfs2_rgrp) - sizeof(struct gfs2_meta_header))
Packit 6ef888
		          * GFS2_NBBY;
Packit 6ef888
		i = offset / sdp->sd_blocks_per_bitmap;
Packit 6ef888
		offset -= i * sdp->sd_blocks_per_bitmap;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	bi = &rgd->bits[i];
Packit 6ef888
	if (!bi->bi_bh)
Packit 6ef888
		return GFS2_BLKST_FREE;
Packit 6ef888
Packit 6ef888
	byte = (bi->bi_bh->b_data + bi->bi_offset) + (offset/GFS2_NBBY);
Packit 6ef888
	bit = (offset % GFS2_NBBY) * GFS2_BIT_SIZE;
Packit 6ef888
Packit 6ef888
	return (*byte >> bit) & GFS2_BIT_MASK;
Packit 6ef888
}