1 | /*
|
---|
2 | * Copyright (c) 2011 Maurizio Lombardi
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include "mfs.h"
|
---|
35 |
|
---|
36 | static int
|
---|
37 | find_free_bit_and_set(bitchunk_t *b, const int bsize,
|
---|
38 | const bool native, unsigned start_bit);
|
---|
39 |
|
---|
40 | static errno_t
|
---|
41 | mfs_free_bit(struct mfs_instance *inst, uint32_t idx, bmap_id_t bid);
|
---|
42 |
|
---|
43 | static errno_t
|
---|
44 | mfs_alloc_bit(struct mfs_instance *inst, uint32_t *idx, bmap_id_t bid);
|
---|
45 |
|
---|
46 | static errno_t
|
---|
47 | mfs_count_free_bits(struct mfs_instance *inst, bmap_id_t bid, uint32_t *free);
|
---|
48 |
|
---|
49 |
|
---|
50 | /**Allocate a new inode.
|
---|
51 | *
|
---|
52 | * @param inst Pointer to the filesystem instance.
|
---|
53 | * @param inum Pointer to a 32 bit number where the index of
|
---|
54 | * the new inode will be saved.
|
---|
55 | *
|
---|
56 | * @return EOK on success or an error code.
|
---|
57 | */
|
---|
58 | errno_t
|
---|
59 | mfs_alloc_inode(struct mfs_instance *inst, uint32_t *inum)
|
---|
60 | {
|
---|
61 | errno_t r = mfs_alloc_bit(inst, inum, BMAP_INODE);
|
---|
62 | return r;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**Free an inode.
|
---|
66 | *
|
---|
67 | * @param inst Pointer to the filesystem instance.
|
---|
68 | * @param inum Number of the inode to free.
|
---|
69 | *
|
---|
70 | * @return EOK on success or an error code.
|
---|
71 | */
|
---|
72 | errno_t
|
---|
73 | mfs_free_inode(struct mfs_instance *inst, uint32_t inum)
|
---|
74 | {
|
---|
75 | return mfs_free_bit(inst, inum, BMAP_INODE);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**Allocate a new zone.
|
---|
79 | *
|
---|
80 | * @param inst Pointer to the filesystem instance.
|
---|
81 | * @param zone Pointer to a 32 bit number where the index
|
---|
82 | * of the zone will be saved.
|
---|
83 | *
|
---|
84 | * @return EOK on success or an error code.
|
---|
85 | */
|
---|
86 | errno_t
|
---|
87 | mfs_alloc_zone(struct mfs_instance *inst, uint32_t *zone)
|
---|
88 | {
|
---|
89 | errno_t r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
|
---|
90 | if (r != EOK)
|
---|
91 | return r;
|
---|
92 |
|
---|
93 | /* Update the cached number of free zones */
|
---|
94 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
95 | if (sbi->nfree_zones_valid)
|
---|
96 | sbi->nfree_zones--;
|
---|
97 |
|
---|
98 | *zone += inst->sbi->firstdatazone - 1;
|
---|
99 | return r;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**Free a zone.
|
---|
103 | *
|
---|
104 | * @param inst Pointer to the filesystem instance.
|
---|
105 | * @param zone Index of the zone to free.
|
---|
106 | *
|
---|
107 | * @return EOK on success or an error code.
|
---|
108 | */
|
---|
109 | errno_t
|
---|
110 | mfs_free_zone(struct mfs_instance *inst, uint32_t zone)
|
---|
111 | {
|
---|
112 | errno_t r;
|
---|
113 |
|
---|
114 | zone -= inst->sbi->firstdatazone - 1;
|
---|
115 |
|
---|
116 | r = mfs_free_bit(inst, zone, BMAP_ZONE);
|
---|
117 | if (r != EOK)
|
---|
118 | return r;
|
---|
119 |
|
---|
120 | /* Update the cached number of free zones */
|
---|
121 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
122 | if (sbi->nfree_zones_valid)
|
---|
123 | sbi->nfree_zones++;
|
---|
124 |
|
---|
125 | return r;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Count the number of free zones
|
---|
129 | *
|
---|
130 | * @param inst Pointer to the instance structure.
|
---|
131 | * @param zones Pointer to the memory location where the result
|
---|
132 | * will be stored.
|
---|
133 | *
|
---|
134 | * @return EOK on success or an error code.
|
---|
135 | */
|
---|
136 | errno_t
|
---|
137 | mfs_count_free_zones(struct mfs_instance *inst, uint32_t *zones)
|
---|
138 | {
|
---|
139 | return mfs_count_free_bits(inst, BMAP_ZONE, zones);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Count the number of free inodes
|
---|
143 | *
|
---|
144 | * @param inst Pointer to the instance structure.
|
---|
145 | * @param zones Pointer to the memory location where the result
|
---|
146 | * will be stored.
|
---|
147 | *
|
---|
148 | * @return EOK on success or an error code.
|
---|
149 | */
|
---|
150 |
|
---|
151 | errno_t
|
---|
152 | mfs_count_free_inodes(struct mfs_instance *inst, uint32_t *inodes)
|
---|
153 | {
|
---|
154 | return mfs_count_free_bits(inst, BMAP_INODE, inodes);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Count the number of free bits in a bitmap
|
---|
158 | *
|
---|
159 | * @param inst Pointer to the instance structure.
|
---|
160 | * @param bid Type of the bitmap (inode or zone).
|
---|
161 | * @param free Pointer to the memory location where the result
|
---|
162 | * will be stores.
|
---|
163 | *
|
---|
164 | * @return EOK on success or an error code.
|
---|
165 | */
|
---|
166 | static errno_t
|
---|
167 | mfs_count_free_bits(struct mfs_instance *inst, bmap_id_t bid, uint32_t *free)
|
---|
168 | {
|
---|
169 | errno_t r;
|
---|
170 | unsigned start_block;
|
---|
171 | unsigned long nblocks;
|
---|
172 | unsigned long nbits;
|
---|
173 | unsigned long block;
|
---|
174 | unsigned long free_bits = 0;
|
---|
175 | bitchunk_t chunk;
|
---|
176 | size_t const bitchunk_bits = sizeof(bitchunk_t) * 8;
|
---|
177 | block_t *b;
|
---|
178 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
179 |
|
---|
180 | start_block = MFS_BMAP_START_BLOCK(sbi, bid);
|
---|
181 | nblocks = MFS_BMAP_SIZE_BLOCKS(sbi, bid);
|
---|
182 | nbits = MFS_BMAP_SIZE_BITS(sbi, bid);
|
---|
183 |
|
---|
184 | for (block = 0; block < nblocks; ++block) {
|
---|
185 | r = block_get(&b, inst->service_id, block + start_block,
|
---|
186 | BLOCK_FLAGS_NONE);
|
---|
187 | if (r != EOK)
|
---|
188 | return r;
|
---|
189 |
|
---|
190 | size_t i;
|
---|
191 | bitchunk_t *data = (bitchunk_t *) b->data;
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Read the bitmap block, chunk per chunk,
|
---|
195 | * counting the zero bits.
|
---|
196 | */
|
---|
197 | for (i = 0; i < sbi->block_size / sizeof(bitchunk_t); ++i) {
|
---|
198 | chunk = conv32(sbi->native, data[i]);
|
---|
199 |
|
---|
200 | size_t bit;
|
---|
201 | for (bit = 0; bit < bitchunk_bits && nbits > 0;
|
---|
202 | ++bit, --nbits) {
|
---|
203 | if (!(chunk & (1 << bit)))
|
---|
204 | free_bits++;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (nbits == 0)
|
---|
208 | break;
|
---|
209 | }
|
---|
210 |
|
---|
211 | r = block_put(b);
|
---|
212 | if (r != EOK)
|
---|
213 | return r;
|
---|
214 | }
|
---|
215 |
|
---|
216 | *free = free_bits;
|
---|
217 | assert(nbits == 0);
|
---|
218 |
|
---|
219 | return EOK;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**Clear a bit in a bitmap.
|
---|
223 | *
|
---|
224 | * @param inst Pointer to the filesystem instance.
|
---|
225 | * @param idx Index of the bit to clear.
|
---|
226 | * @param bid BMAP_ZONE if operating on the zone's bitmap,
|
---|
227 | * BMAP_INODE if operating on the inode's bitmap.
|
---|
228 | *
|
---|
229 | * @return EOK on success or an error code.
|
---|
230 | */
|
---|
231 | static errno_t
|
---|
232 | mfs_free_bit(struct mfs_instance *inst, uint32_t idx, bmap_id_t bid)
|
---|
233 | {
|
---|
234 | struct mfs_sb_info *sbi;
|
---|
235 | errno_t r;
|
---|
236 | unsigned start_block;
|
---|
237 | unsigned *search;
|
---|
238 | block_t *b;
|
---|
239 |
|
---|
240 | sbi = inst->sbi;
|
---|
241 |
|
---|
242 | start_block = MFS_BMAP_START_BLOCK(sbi, bid);
|
---|
243 |
|
---|
244 | if (bid == BMAP_ZONE) {
|
---|
245 | search = &sbi->zsearch;
|
---|
246 | if (idx > sbi->nzones) {
|
---|
247 | printf(NAME ": Error! Trying to free beyond the "
|
---|
248 | "bitmap max size\n");
|
---|
249 | return EIO;
|
---|
250 | }
|
---|
251 | } else {
|
---|
252 | /* bid == BMAP_INODE */
|
---|
253 | search = &sbi->isearch;
|
---|
254 | if (idx > sbi->ninodes) {
|
---|
255 | printf(NAME ": Error! Trying to free beyond the "
|
---|
256 | "bitmap max size\n");
|
---|
257 | return EIO;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* Compute the bitmap block */
|
---|
262 | uint32_t block = idx / (sbi->block_size * 8) + start_block;
|
---|
263 |
|
---|
264 | r = block_get(&b, inst->service_id, block, BLOCK_FLAGS_NONE);
|
---|
265 | if (r != EOK)
|
---|
266 | goto out_err;
|
---|
267 |
|
---|
268 | /* Compute the bit index in the block */
|
---|
269 | idx %= (sbi->block_size * 8);
|
---|
270 | bitchunk_t *ptr = b->data;
|
---|
271 | bitchunk_t chunk;
|
---|
272 | const size_t chunk_bits = sizeof(bitchunk_t) * 8;
|
---|
273 |
|
---|
274 | chunk = conv32(sbi->native, ptr[idx / chunk_bits]);
|
---|
275 | chunk &= ~(1 << (idx % chunk_bits));
|
---|
276 | ptr[idx / chunk_bits] = conv32(sbi->native, chunk);
|
---|
277 |
|
---|
278 | b->dirty = true;
|
---|
279 | r = block_put(b);
|
---|
280 |
|
---|
281 | if (*search > idx)
|
---|
282 | *search = idx;
|
---|
283 |
|
---|
284 | out_err:
|
---|
285 | return r;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**Search a free bit in a bitmap and mark it as used.
|
---|
289 | *
|
---|
290 | * @param inst Pointer to the filesystem instance.
|
---|
291 | * @param idx Pointer of a 32 bit number where the index
|
---|
292 | * of the found bit will be stored.
|
---|
293 | * @param bid BMAP_ZONE if operating on the zone's bitmap,
|
---|
294 | * BMAP_INODE if operating on the inode's bitmap.
|
---|
295 | *
|
---|
296 | * @return EOK on success or an error code.
|
---|
297 | */
|
---|
298 | static errno_t
|
---|
299 | mfs_alloc_bit(struct mfs_instance *inst, uint32_t *idx, bmap_id_t bid)
|
---|
300 | {
|
---|
301 | struct mfs_sb_info *sbi;
|
---|
302 | uint32_t limit;
|
---|
303 | unsigned long nblocks;
|
---|
304 | unsigned *search, i, start_block;
|
---|
305 | unsigned bits_per_block;
|
---|
306 | errno_t r;
|
---|
307 | int freebit;
|
---|
308 |
|
---|
309 | sbi = inst->sbi;
|
---|
310 |
|
---|
311 | start_block = MFS_BMAP_START_BLOCK(sbi, bid);
|
---|
312 | limit = MFS_BMAP_SIZE_BITS(sbi, bid);
|
---|
313 | nblocks = MFS_BMAP_SIZE_BLOCKS(sbi, bid);
|
---|
314 |
|
---|
315 | if (bid == BMAP_ZONE) {
|
---|
316 | search = &sbi->zsearch;
|
---|
317 | } else {
|
---|
318 | /* bid == BMAP_INODE */
|
---|
319 | search = &sbi->isearch;
|
---|
320 | }
|
---|
321 | bits_per_block = sbi->block_size * 8;
|
---|
322 |
|
---|
323 | block_t *b;
|
---|
324 |
|
---|
325 | retry:
|
---|
326 |
|
---|
327 | for (i = *search / bits_per_block; i < nblocks; ++i) {
|
---|
328 | r = block_get(&b, inst->service_id, i + start_block,
|
---|
329 | BLOCK_FLAGS_NONE);
|
---|
330 |
|
---|
331 | if (r != EOK)
|
---|
332 | goto out;
|
---|
333 |
|
---|
334 | unsigned tmp = *search % bits_per_block;
|
---|
335 |
|
---|
336 | freebit = find_free_bit_and_set(b->data, sbi->block_size,
|
---|
337 | sbi->native, tmp);
|
---|
338 | if (freebit == -1) {
|
---|
339 | /* No free bit in this block */
|
---|
340 | r = block_put(b);
|
---|
341 | if (r != EOK)
|
---|
342 | goto out;
|
---|
343 | continue;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* Free bit found in this block, compute the real index */
|
---|
347 | *idx = freebit + bits_per_block * i;
|
---|
348 | if (*idx > limit) {
|
---|
349 | /* Index is beyond the limit, it is invalid */
|
---|
350 | r = block_put(b);
|
---|
351 | if (r != EOK)
|
---|
352 | goto out;
|
---|
353 | break;
|
---|
354 | }
|
---|
355 |
|
---|
356 | *search = *idx;
|
---|
357 | b->dirty = true;
|
---|
358 | r = block_put(b);
|
---|
359 | goto out;
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (*search > 0) {
|
---|
363 | /* Repeat the search from the first bitmap block */
|
---|
364 | *search = 0;
|
---|
365 | goto retry;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /* Free bit not found, return error */
|
---|
369 | return ENOSPC;
|
---|
370 |
|
---|
371 | out:
|
---|
372 | return r;
|
---|
373 | }
|
---|
374 |
|
---|
375 | static int
|
---|
376 | find_free_bit_and_set(bitchunk_t *b, const int bsize,
|
---|
377 | const bool native, unsigned start_bit)
|
---|
378 | {
|
---|
379 | int r = -1;
|
---|
380 | unsigned i, j;
|
---|
381 | bitchunk_t chunk;
|
---|
382 | const size_t chunk_bits = sizeof(bitchunk_t) * 8;
|
---|
383 |
|
---|
384 | for (i = start_bit / chunk_bits;
|
---|
385 | i < bsize / sizeof(bitchunk_t); ++i) {
|
---|
386 |
|
---|
387 | if (!(~b[i])) {
|
---|
388 | /* No free bit in this chunk */
|
---|
389 | continue;
|
---|
390 | }
|
---|
391 |
|
---|
392 | chunk = conv32(native, b[i]);
|
---|
393 |
|
---|
394 | for (j = 0; j < chunk_bits; ++j) {
|
---|
395 | if (!(chunk & (1 << j))) {
|
---|
396 | r = i * chunk_bits + j;
|
---|
397 | chunk |= 1 << j;
|
---|
398 | b[i] = conv32(native, chunk);
|
---|
399 | goto found;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 | found:
|
---|
404 | return r;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * @}
|
---|
409 | */
|
---|
410 |
|
---|