1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "hfs_fs.h"
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29static u32 hfs_find_set_zero_bits(__be32 *bitmap, u32 size, u32 offset, u32 *max)
30{
31 __be32 *curr, *end;
32 u32 mask, start, len, n;
33 __be32 val;
34 int i;
35
36 len = *max;
37 if (!len)
38 return size;
39
40 curr = bitmap + (offset / 32);
41 end = bitmap + ((size + 31) / 32);
42
43
44 val = *curr;
45 if (~val) {
46 n = be32_to_cpu(val);
47 i = offset % 32;
48 mask = (1U << 31) >> i;
49 for (; i < 32; mask >>= 1, i++) {
50 if (!(n & mask))
51 goto found;
52 }
53 }
54
55
56 while (++curr < end) {
57 val = *curr;
58 if (~val) {
59 n = be32_to_cpu(val);
60 mask = 1 << 31;
61 for (i = 0; i < 32; mask >>= 1, i++) {
62 if (!(n & mask))
63 goto found;
64 }
65 }
66 }
67 return size;
68
69found:
70 start = (curr - bitmap) * 32 + i;
71 if (start >= size)
72 return start;
73
74 len = min(size - start, len);
75 while (1) {
76 n |= mask;
77 if (++i >= 32)
78 break;
79 mask >>= 1;
80 if (!--len || n & mask)
81 goto done;
82 }
83 if (!--len)
84 goto done;
85 *curr++ = cpu_to_be32(n);
86
87 while (1) {
88 n = be32_to_cpu(*curr);
89 if (len < 32)
90 break;
91 if (n) {
92 len = 32;
93 break;
94 }
95 *curr++ = cpu_to_be32(0xffffffff);
96 len -= 32;
97 }
98
99 mask = 1U << 31;
100 for (i = 0; i < len; i++) {
101 if (n & mask)
102 break;
103 n |= mask;
104 mask >>= 1;
105 }
106done:
107 *curr = cpu_to_be32(n);
108 *max = (curr - bitmap) * 32 + i - start;
109 return start;
110}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139u32 hfs_vbm_search_free(struct super_block *sb, u32 goal, u32 *num_bits)
140{
141 void *bitmap;
142 u32 pos;
143
144
145 if (!*num_bits)
146 return 0;
147
148 mutex_lock(&HFS_SB(sb)->bitmap_lock);
149 bitmap = HFS_SB(sb)->bitmap;
150
151 pos = hfs_find_set_zero_bits(bitmap, HFS_SB(sb)->fs_ablocks, goal, num_bits);
152 if (pos >= HFS_SB(sb)->fs_ablocks) {
153 if (goal)
154 pos = hfs_find_set_zero_bits(bitmap, goal, 0, num_bits);
155 if (pos >= HFS_SB(sb)->fs_ablocks) {
156 *num_bits = pos = 0;
157 goto out;
158 }
159 }
160
161 hfs_dbg(BITMAP, "alloc_bits: %u,%u\n", pos, *num_bits);
162 HFS_SB(sb)->free_ablocks -= *num_bits;
163 hfs_bitmap_dirty(sb);
164out:
165 mutex_unlock(&HFS_SB(sb)->bitmap_lock);
166 return pos;
167}
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193int hfs_clear_vbm_bits(struct super_block *sb, u16 start, u16 count)
194{
195 __be32 *curr;
196 u32 mask;
197 int i, len;
198
199
200 if (!count)
201 return 0;
202
203 hfs_dbg(BITMAP, "clear_bits: %u,%u\n", start, count);
204
205 if ((start + count) > HFS_SB(sb)->fs_ablocks)
206 return -2;
207
208 mutex_lock(&HFS_SB(sb)->bitmap_lock);
209
210 curr = HFS_SB(sb)->bitmap + (start / 32);
211 len = count;
212
213
214 i = start % 32;
215 if (i) {
216 int j = 32 - i;
217 mask = 0xffffffffU << j;
218 if (j > count) {
219 mask |= 0xffffffffU >> (i + count);
220 *curr &= cpu_to_be32(mask);
221 goto out;
222 }
223 *curr++ &= cpu_to_be32(mask);
224 count -= j;
225 }
226
227
228 while (count >= 32) {
229 *curr++ = 0;
230 count -= 32;
231 }
232
233 if (count) {
234 mask = 0xffffffffU >> count;
235 *curr &= cpu_to_be32(mask);
236 }
237out:
238 HFS_SB(sb)->free_ablocks += len;
239 mutex_unlock(&HFS_SB(sb)->bitmap_lock);
240 hfs_bitmap_dirty(sb);
241
242 return 0;
243}
244