source: mainline/uspace/srv/fs/minixfs/mfs_rw.c@ bb7e8382

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bb7e8382 was 8f6bb76e, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago
  • Fix bug when allocating memory space for the indirect zone vector
  • relative block number calculation was not correct
  • Property mode set to 100644
File size: 7.8 KB
Line 
1/*
2g
3 * Copyright (c) 2011 Maurizio Lombardi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup fs
31 * @{
32 */
33
34#include <assert.h>
35#include <errno.h>
36#include "mfs.h"
37#include "mfs_utils.h"
38
39static int
40rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
41 bool write_mode, uint32_t w_block);
42
43static int
44reset_zone_content(struct mfs_instance *inst, uint32_t zone);
45
46static int
47alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone);
48
49static int
50read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone);
51
52static int
53write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone);
54
55
56/**Given the position in the file expressed in
57 *bytes, this function returns the on-disk block
58 *relative to that position.
59 *Returns zero if the block does not exist.
60 */
61int
62read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
63{
64 int r;
65 const struct mfs_sb_info *sbi = mnode->instance->sbi;
66 const int block_size = sbi->block_size;
67
68 /*Compute relative block number in file*/
69 int rblock = pos / block_size;
70
71 if (mnode->ino_i->i_size < pos) {
72 /*Trying to read beyond the end of file*/
73 r = EOK;
74 *b = 0;
75 goto out;
76 }
77
78 r = rw_map_ondisk(b, mnode, rblock, false, 0);
79out:
80 return r;
81}
82
83int
84write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
85 uint32_t *old_zone)
86{
87 const struct mfs_sb_info *sbi = mnode->instance->sbi;
88 const int block_size = sbi->block_size;
89
90 /*Compute the relative block number in file*/
91 int rblock = pos / block_size;
92
93 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
94}
95
96int
97free_zone(struct mfs_node *mnode, const uint32_t zone)
98{
99 int r;
100 uint32_t old_zone;
101
102 r = rw_map_ondisk(&old_zone, mnode, zone, true, 0);
103 if (r != EOK)
104 return r;
105
106 if (old_zone > 0)
107 r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
108
109 return r;
110}
111
112static int
113rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
114 bool write_mode, uint32_t w_block)
115{
116 int r, nr_direct;
117 int ptrs_per_block;
118 uint32_t *ind_zone, *ind2_zone;
119
120 assert(mnode);
121 struct mfs_ino_info *ino_i = mnode->ino_i;
122
123 assert(ino_i);
124 assert(mnode->instance);
125
126 struct mfs_instance *inst = mnode->instance;
127 struct mfs_sb_info *sbi = inst->sbi;
128 assert(sbi);
129
130 const mfs_version_t fs_version = sbi->fs_version;
131
132 if (fs_version == MFS_VERSION_V1) {
133 nr_direct = V1_NR_DIRECT_ZONES;
134 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
135 } else {
136 nr_direct = V2_NR_DIRECT_ZONES;
137 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
138 }
139
140 /*Check if the wanted block is in the direct zones*/
141 if (rblock < nr_direct) {
142 *b = ino_i->i_dzone[rblock];
143 if (write_mode) {
144 ino_i->i_dzone[rblock] = w_block;
145 ino_i->dirty = true;
146 }
147 return EOK;
148 }
149
150 rblock -= nr_direct;
151
152 if (rblock < ptrs_per_block) {
153 /*The wanted block is in the single indirect zone chain*/
154 if (ino_i->i_izone[0] == 0) {
155 if (write_mode) {
156 uint32_t zone;
157 r = alloc_zone_and_clear(inst, &zone);
158 if (r != EOK)
159 return r;
160
161 ino_i->i_izone[0] = zone;
162 ino_i->dirty = true;
163 } else {
164 return -1;
165 }
166 }
167
168 r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
169 if (r != EOK)
170 return r;
171
172 *b = ind_zone[rblock];
173 if (write_mode) {
174 ind_zone[rblock] = w_block;
175 write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
176 }
177
178 goto out_free_ind1;
179 }
180
181 rblock -= ptrs_per_block;
182
183 /*The wanted block is in the double indirect zone chain*/
184
185 /*read the first indirect zone of the chain*/
186 if (ino_i->i_izone[1] == 0) {
187 if (write_mode) {
188 uint32_t zone;
189 r = alloc_zone_and_clear(inst, &zone);
190 if (r != EOK)
191 return r;
192
193 ino_i->i_izone[1] = zone;
194 ino_i->dirty = true;
195 } else
196 return -1;
197 }
198
199 r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
200 if (r != EOK)
201 return r;
202
203 /*
204 *Compute the position of the second indirect
205 *zone pointer in the chain.
206 */
207 uint32_t ind2_off = rblock / ptrs_per_block;
208
209 /*read the second indirect zone of the chain*/
210 if (ind_zone[ind2_off] == 0) {
211 if (write_mode) {
212 uint32_t zone;
213 r = alloc_zone_and_clear(inst, &zone);
214 if(r != EOK)
215 goto out_free_ind1;
216 ind_zone[ind2_off] = zone;
217 write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
218 } else {
219 r = -1;
220 goto out_free_ind1;
221 }
222 }
223
224 r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
225 if (r != EOK)
226 goto out_free_ind1;
227
228 *b = ind2_zone[ind2_off % ptrs_per_block];
229 if (write_mode) {
230 ind2_zone[ind2_off % ptrs_per_block] = w_block;
231 write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
232 }
233
234 r = EOK;
235
236 free(ind2_zone);
237out_free_ind1:
238 free(ind_zone);
239 return r;
240}
241
242
243static int
244reset_zone_content(struct mfs_instance *inst, uint32_t zone)
245{
246 block_t *b;
247 int r;
248
249 r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
250 if (r != EOK)
251 return r;
252
253 memset(b->data, 0, b->size);
254 b->dirty = true;
255 block_put(b);
256
257 return EOK;
258}
259
260static int
261alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
262{
263 int r;
264
265 r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
266 if (r != EOK)
267 goto out;
268
269 r = reset_zone_content(inst, *zone);
270out:
271 return r;
272}
273
274static int
275read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
276{
277 struct mfs_sb_info *sbi = inst->sbi;
278 int r;
279 unsigned i;
280 block_t *b;
281 const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
282 sizeof(uint32_t);
283
284 *ind_zone = malloc(max_ind_zone_ptrs);
285 if (*ind_zone == NULL)
286 return ENOMEM;
287
288 r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NONE);
289 if (r != EOK) {
290 free(*ind_zone);
291 return r;
292 }
293
294 if (sbi->fs_version == MFS_VERSION_V1) {
295 uint16_t *src_ptr = b->data;
296
297 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
298 (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
299 } else {
300 uint32_t *src_ptr = b->data;
301
302 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
303 (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
304 }
305
306 block_put(b);
307
308 return EOK;
309}
310
311static int
312write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
313{
314 struct mfs_sb_info *sbi = inst->sbi;
315 int r;
316 unsigned i;
317 block_t *b;
318
319 r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NONE);
320 if (r != EOK)
321 return r;
322
323 if (sbi->fs_version == MFS_VERSION_V1) {
324 uint16_t *dest_ptr = b->data;
325
326 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
327 dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
328 } else {
329 uint32_t *dest_ptr = b->data;
330
331 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
332 dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
333
334 }
335 b->dirty = true;
336 block_put(b);
337 return EOK;
338}
339
340
341/**
342 * @}
343 */
344
Note: See TracBrowser for help on using the repository browser.