2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
11 * These routines are used by the resource group routines (rgrp.c)
12 * to keep track of block allocation. Each block is represented by two
13 * bits. One bit indicates whether or not the block is used. (1=used,
14 * 0=free) The other bit indicates whether or not the block contains a
15 * dinode or not. (1=dinode, 0=not-dinode) So, each byte represents
16 * GFS2_NBBY (i.e. 4) blocks.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/completion.h>
23 #include <linux/buffer_head.h>
24 #include <linux/gfs2_ondisk.h>
25 #include <asm/semaphore.h>
28 #include "lm_interface.h"
33 static const char valid_change[16] = {
42 * gfs2_setbit - Set a bit in the bitmaps
43 * @buffer: the buffer that holds the bitmaps
44 * @buflen: the length (in bytes) of the buffer
45 * @block: the block to set
46 * @new_state: the new state of the block
50 void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
51 unsigned int buflen, uint32_t block, unsigned char new_state)
53 unsigned char *byte, *end, cur_state;
56 byte = buffer + (block / GFS2_NBBY);
57 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
58 end = buffer + buflen;
60 gfs2_assert(rgd->rd_sbd, byte < end);
62 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
64 if (valid_change[new_state * 4 + cur_state]) {
65 *byte ^= cur_state << bit;
66 *byte |= new_state << bit;
68 gfs2_consist_rgrpd(rgd);
72 * gfs2_testbit - test a bit in the bitmaps
73 * @buffer: the buffer that holds the bitmaps
74 * @buflen: the length (in bytes) of the buffer
75 * @block: the block to read
79 unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
80 unsigned int buflen, uint32_t block)
82 unsigned char *byte, *end, cur_state;
85 byte = buffer + (block / GFS2_NBBY);
86 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
87 end = buffer + buflen;
89 gfs2_assert(rgd->rd_sbd, byte < end);
91 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
97 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
98 * a block in a given allocation state.
99 * @buffer: the buffer that holds the bitmaps
100 * @buflen: the length (in bytes) of the buffer
101 * @goal: start search at this block's bit-pair (within @buffer)
102 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
103 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
105 * Scope of @goal and returned block number is only within this bitmap buffer,
106 * not entire rgrp or filesystem. @buffer will be offset from the actual
107 * beginning of a bitmap block buffer, skipping any header structures.
109 * Return: the block number (bitmap buffer scope) that was found
112 uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
113 unsigned int buflen, uint32_t goal,
114 unsigned char old_state)
116 unsigned char *byte, *end, alloc;
120 byte = buffer + (goal / GFS2_NBBY);
121 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
122 end = buffer + buflen;
123 alloc = (old_state & 1) ? 0 : 0x55;
126 if ((*byte & 0x55) == alloc) {
127 blk += (8 - bit) >> 1;
135 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
138 bit += GFS2_BIT_SIZE;
151 * gfs2_bitcount - count the number of bits in a certain state
152 * @buffer: the buffer that holds the bitmaps
153 * @buflen: the length (in bytes) of the buffer
154 * @state: the state of the block we're looking for
156 * Returns: The number of bits
159 uint32_t gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
160 unsigned int buflen, unsigned char state)
162 unsigned char *byte = buffer;
163 unsigned char *end = buffer + buflen;
164 unsigned char state1 = state << 2;
165 unsigned char state2 = state << 4;
166 unsigned char state3 = state << 6;
169 for (; byte < end; byte++) {
170 if (((*byte) & 0x03) == state)
172 if (((*byte) & 0x0C) == state1)
174 if (((*byte) & 0x30) == state2)
176 if (((*byte) & 0xC0) == state3)