Merge branch 'master'
[linux-2.6] / fs / gfs2 / bits.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
4  *
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.
8  */
9
10 /*
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.
17  */
18
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>
26
27 #include "gfs2.h"
28 #include "lm_interface.h"
29 #include "incore.h"
30 #include "bits.h"
31 #include "util.h"
32
33 static const char valid_change[16] = {
34                 /* current */
35         /* n */ 0, 1, 0, 1,
36         /* e */ 1, 0, 0, 0,
37         /* w */ 0, 0, 0, 0,
38                 1, 0, 0, 0
39 };
40
41 /**
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
47  *
48  */
49
50 void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
51                  unsigned int buflen, uint32_t block, unsigned char new_state)
52 {
53         unsigned char *byte, *end, cur_state;
54         unsigned int bit;
55
56         byte = buffer + (block / GFS2_NBBY);
57         bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
58         end = buffer + buflen;
59
60         gfs2_assert(rgd->rd_sbd, byte < end);
61
62         cur_state = (*byte >> bit) & GFS2_BIT_MASK;
63
64         if (valid_change[new_state * 4 + cur_state]) {
65                 *byte ^= cur_state << bit;
66                 *byte |= new_state << bit;
67         } else
68                 gfs2_consist_rgrpd(rgd);
69 }
70
71 /**
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
76  *
77  */
78
79 unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
80                            unsigned int buflen, uint32_t block)
81 {
82         unsigned char *byte, *end, cur_state;
83         unsigned int bit;
84
85         byte = buffer + (block / GFS2_NBBY);
86         bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
87         end = buffer + buflen;
88
89         gfs2_assert(rgd->rd_sbd, byte < end);
90
91         cur_state = (*byte >> bit) & GFS2_BIT_MASK;
92
93         return cur_state;
94 }
95
96 /**
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)
104  *
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.
108  *
109  * Return: the block number (bitmap buffer scope) that was found
110  */
111
112 uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
113                      unsigned int buflen, uint32_t goal,
114                      unsigned char old_state)
115 {
116         unsigned char *byte, *end, alloc;
117         uint32_t blk = goal;
118         unsigned int bit;
119
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;
124
125         while (byte < end) {
126                 if ((*byte & 0x55) == alloc) {
127                         blk += (8 - bit) >> 1;
128
129                         bit = 0;
130                         byte++;
131
132                         continue;
133                 }
134
135                 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
136                         return blk;
137
138                 bit += GFS2_BIT_SIZE;
139                 if (bit >= 8) {
140                         bit = 0;
141                         byte++;
142                 }
143
144                 blk++;
145         }
146
147         return BFITNOENT;
148 }
149
150 /**
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
155  *
156  * Returns: The number of bits
157  */
158
159 uint32_t gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
160                        unsigned int buflen, unsigned char state)
161 {
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;
167         uint32_t count = 0;
168
169         for (; byte < end; byte++) {
170                 if (((*byte) & 0x03) == state)
171                         count++;
172                 if (((*byte) & 0x0C) == state1)
173                         count++;
174                 if (((*byte) & 0x30) == state2)
175                         count++;
176                 if (((*byte) & 0xC0) == state3)
177                         count++;
178         }
179
180         return count;
181 }
182