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 <asm/semaphore.h>
29 static const char valid_change[16] = {
38 * gfs2_setbit - Set a bit in the bitmaps
39 * @buffer: the buffer that holds the bitmaps
40 * @buflen: the length (in bytes) of the buffer
41 * @block: the block to set
42 * @new_state: the new state of the block
46 void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
47 unsigned int buflen, uint32_t block, unsigned char new_state)
49 unsigned char *byte, *end, cur_state;
52 byte = buffer + (block / GFS2_NBBY);
53 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
54 end = buffer + buflen;
56 gfs2_assert(rgd->rd_sbd, byte < end);
58 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
60 if (valid_change[new_state * 4 + cur_state]) {
61 *byte ^= cur_state << bit;
62 *byte |= new_state << bit;
64 gfs2_consist_rgrpd(rgd);
68 * gfs2_testbit - test a bit in the bitmaps
69 * @buffer: the buffer that holds the bitmaps
70 * @buflen: the length (in bytes) of the buffer
71 * @block: the block to read
75 unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
76 unsigned int buflen, uint32_t block)
78 unsigned char *byte, *end, cur_state;
81 byte = buffer + (block / GFS2_NBBY);
82 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
83 end = buffer + buflen;
85 gfs2_assert(rgd->rd_sbd, byte < end);
87 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
93 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
94 * a block in a given allocation state.
95 * @buffer: the buffer that holds the bitmaps
96 * @buflen: the length (in bytes) of the buffer
97 * @goal: start search at this block's bit-pair (within @buffer)
98 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
99 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
101 * Scope of @goal and returned block number is only within this bitmap buffer,
102 * not entire rgrp or filesystem. @buffer will be offset from the actual
103 * beginning of a bitmap block buffer, skipping any header structures.
105 * Return: the block number (bitmap buffer scope) that was found
108 uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
109 unsigned int buflen, uint32_t goal,
110 unsigned char old_state)
112 unsigned char *byte, *end, alloc;
116 byte = buffer + (goal / GFS2_NBBY);
117 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
118 end = buffer + buflen;
119 alloc = (old_state & 1) ? 0 : 0x55;
122 if ((*byte & 0x55) == alloc) {
123 blk += (8 - bit) >> 1;
131 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
134 bit += GFS2_BIT_SIZE;
147 * gfs2_bitcount - count the number of bits in a certain state
148 * @buffer: the buffer that holds the bitmaps
149 * @buflen: the length (in bytes) of the buffer
150 * @state: the state of the block we're looking for
152 * Returns: The number of bits
155 uint32_t gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
156 unsigned int buflen, unsigned char state)
158 unsigned char *byte = buffer;
159 unsigned char *end = buffer + buflen;
160 unsigned char state1 = state << 2;
161 unsigned char state2 = state << 4;
162 unsigned char state3 = state << 6;
165 for (; byte < end; byte++) {
166 if (((*byte) & 0x03) == state)
168 if (((*byte) & 0x0C) == state1)
170 if (((*byte) & 0x30) == state2)
172 if (((*byte) & 0xC0) == state3)