source: mainline/uspace/srv/fs/minixfs/mfs_balloc.c@ a5bad72

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a5bad72 was a5bad72, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

remove old debug messages

  • Property mode set to 100644
File size: 4.9 KB
Line 
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#include "mfs_utils.h"
36
37static int
38find_free_bit_and_set(bitchunk_t *b, const int bsize,
39 const bool native, unsigned start_bit);
40
41int
42mfs_free_bit(struct mfs_instance *inst, uint32_t idx, bmap_id_t bid)
43{
44 struct mfs_sb_info *sbi;
45 int r;
46 unsigned start_block;
47 block_t *b;
48
49 assert(inst != NULL);
50 sbi = inst->sbi;
51 assert(sbi != NULL);
52
53 if (bid == BMAP_ZONE) {
54 start_block = 2 + sbi->ibmap_blocks;
55 if (idx > sbi->nzones) {
56 printf(NAME ": Error! Trying to free beyond the" \
57 "bitmap max size\n");
58 return -1;
59 }
60 } else {
61 /*bid == BMAP_INODE*/
62 start_block = 2;
63 if (idx > sbi->ninodes) {
64 printf(NAME ": Error! Trying to free beyond the" \
65 "bitmap max size\n");
66 return -1;
67 }
68 }
69
70 /*Compute the bitmap block*/
71 uint32_t block = idx / (sbi->block_size * 8) + start_block;
72
73 r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
74 on_error(r, goto out_err);
75
76 /*Compute the bit index in the block*/
77 idx %= (sbi->block_size * 8);
78 bitchunk_t *ptr = b->data;
79 bitchunk_t chunk;
80 const size_t chunk_bits = sizeof(bitchunk_t) * 8;
81
82 chunk = conv32(sbi->native, ptr[idx / chunk_bits]);
83 chunk &= ~(1 << (idx % chunk_bits));
84 ptr[idx / chunk_bits] = conv32(sbi->native, chunk);
85 b->dirty = true;
86 r = block_put(b);
87 mfsdebug("free index %u\n", idx);
88
89out_err:
90 return r;
91}
92
93int
94mfs_alloc_bit(struct mfs_instance *inst, uint32_t *idx, bmap_id_t bid)
95{
96 struct mfs_sb_info *sbi;
97 uint32_t limit;
98 unsigned long nblocks;
99 unsigned *search, i, start_block;
100 unsigned bits_per_block;
101 int r, freebit;
102
103 assert(inst != NULL);
104 sbi = inst->sbi;
105 assert(sbi != NULL);
106
107 if (bid == BMAP_ZONE) {
108 search = &sbi->zsearch;
109 start_block = 2 + sbi->ibmap_blocks;
110 nblocks = sbi->zbmap_blocks;
111 limit = sbi->nzones;
112 } else {
113 /*bid == BMAP_INODE*/
114 search = &sbi->isearch;
115 start_block = 2;
116 nblocks = sbi->ibmap_blocks;
117 limit = sbi->ninodes;
118 }
119 bits_per_block = sbi->block_size * 8;
120
121 block_t *b;
122
123retry:
124
125 for (i = *search / bits_per_block; i < nblocks; ++i) {
126 r = block_get(&b, inst->handle, i + start_block,
127 BLOCK_FLAGS_NONE);
128
129 on_error(r, goto out);
130
131 unsigned tmp = *search % bits_per_block;
132
133 freebit = find_free_bit_and_set(b->data, sbi->block_size,
134 sbi->native, tmp);
135 if (freebit == -1) {
136 /*No free bit in this block*/
137 block_put(b);
138 continue;
139 }
140
141 /*Free bit found in this block, compute the real index*/
142 *idx = freebit + bits_per_block * i;
143 *idx += (bid == BMAP_INODE) ? 1 : 0;
144 mfsdebug("alloc index %d %d\n", (int) *idx, i);
145 if (*idx > limit) {
146 /*Index is beyond the limit, it is invalid*/
147 r = block_put(b);
148 on_error(r, goto out);
149 break;
150 }
151
152 *search = *idx;
153 b->dirty = true;
154 r = block_put(b);
155 goto out;
156 }
157
158 if (*search > 0) {
159 /*Repeat the search from the first bitmap block*/
160 *search = 0;
161 goto retry;
162 }
163
164 /*Free bit not found, return error*/
165 return ENOSPC;
166
167out:
168 return r;
169}
170
171static int
172find_free_bit_and_set(bitchunk_t *b, const int bsize,
173 const bool native, unsigned start_bit)
174{
175 int r = -1;
176 unsigned i, j;
177 bitchunk_t chunk;
178 const size_t chunk_bits = sizeof(bitchunk_t) * 8;
179
180 for (i = start_bit / chunk_bits;
181 i < bsize / sizeof(bitchunk_t); ++i) {
182 if (!(~b[i])) {
183 /*No free bit in this chunk*/
184 continue;
185 }
186
187 chunk = conv32(native, b[i]);
188
189 for (j = 0; j < chunk_bits; ++j) {
190 if (!(chunk & (1 << j))) {
191 r = i * chunk_bits + j;
192 chunk |= 1 << j;
193 b[i] = conv32(native, chunk);
194 goto found;
195 }
196 }
197 }
198found:
199 return r;
200}
201
202/**
203 * @}
204 */
205
Note: See TracBrowser for help on using the repository browser.