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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since afd9c3b was 8f6bb76e, checked in by Maurizio Lombardi <m.lombardi85@…>, 15 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
RevLine 
[6adba0a8]1/*
[2bbbfd3]2g
[6adba0a8]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
[930baca]34#include <assert.h>
35#include <errno.h>
36#include "mfs.h"
37#include "mfs_utils.h"
38
[2d7c77a]39static int
40rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
41 bool write_mode, uint32_t w_block);
[930baca]42
[5a29b4c]43static int
[8ab1adff]44reset_zone_content(struct mfs_instance *inst, uint32_t zone);
[5a29b4c]45
46static int
[8ab1adff]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);
[5a29b4c]54
55
[2d7c77a]56/**Given the position in the file expressed in
[930baca]57 *bytes, this function returns the on-disk block
58 *relative to that position.
59 *Returns zero if the block does not exist.
60 */
[2bbbfd3]61int
62read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
[930baca]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
[8f6bb76e]71 if (mnode->ino_i->i_size < pos) {
[cfbcd86]72 /*Trying to read beyond the end of file*/
[930baca]73 r = EOK;
[155f792]74 *b = 0;
[930baca]75 goto out;
76 }
77
[2d7c77a]78 r = rw_map_ondisk(b, mnode, rblock, false, 0);
[930baca]79out:
80 return r;
81}
82
[2bbbfd3]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)
[40f7297]107 r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
[2bbbfd3]108
[40f7297]109 return r;
[2bbbfd3]110}
111
[2d7c77a]112static int
113rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
114 bool write_mode, uint32_t w_block)
[930baca]115{
[8c76c30]116 int r, nr_direct;
[155f792]117 int ptrs_per_block;
[8ab1adff]118 uint32_t *ind_zone, *ind2_zone;
[930baca]119
120 assert(mnode);
[2d7c77a]121 struct mfs_ino_info *ino_i = mnode->ino_i;
[930baca]122
[155f792]123 assert(ino_i);
[930baca]124 assert(mnode->instance);
125
[5a29b4c]126 struct mfs_instance *inst = mnode->instance;
127 struct mfs_sb_info *sbi = inst->sbi;
[930baca]128 assert(sbi);
129
[2bbbfd3]130 const mfs_version_t fs_version = sbi->fs_version;
[155f792]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 }
[930baca]139
[8c76c30]140 /*Check if the wanted block is in the direct zones*/
[155f792]141 if (rblock < nr_direct) {
142 *b = ino_i->i_dzone[rblock];
[2d7c77a]143 if (write_mode) {
144 ino_i->i_dzone[rblock] = w_block;
145 ino_i->dirty = true;
146 }
[2bbbfd3]147 return EOK;
[930baca]148 }
[8f6bb76e]149
150 rblock -= nr_direct;
[930baca]151
152 if (rblock < ptrs_per_block) {
[8c76c30]153 /*The wanted block is in the single indirect zone chain*/
[155f792]154 if (ino_i->i_izone[0] == 0) {
[5a29b4c]155 if (write_mode) {
[8ab1adff]156 uint32_t zone;
157 r = alloc_zone_and_clear(inst, &zone);
[5a29b4c]158 if (r != EOK)
[2bbbfd3]159 return r;
[5a29b4c]160
[8ab1adff]161 ino_i->i_izone[0] = zone;
[5a29b4c]162 ino_i->dirty = true;
[8f6bb76e]163 } else {
[2bbbfd3]164 return -1;
[8f6bb76e]165 }
[930baca]166 }
167
[8ab1adff]168 r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
[152610a8]169 if (r != EOK)
[2bbbfd3]170 return r;
[930baca]171
[8ab1adff]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);
[152610a8]176 }
[930baca]177
[8ab1adff]178 goto out_free_ind1;
[930baca]179 }
180
[8f6bb76e]181 rblock -= ptrs_per_block;
[930baca]182
[8c76c30]183 /*The wanted block is in the double indirect zone chain*/
[930baca]184
[8c76c30]185 /*read the first indirect zone of the chain*/
[155f792]186 if (ino_i->i_izone[1] == 0) {
[5a29b4c]187 if (write_mode) {
[8ab1adff]188 uint32_t zone;
189 r = alloc_zone_and_clear(inst, &zone);
[5a29b4c]190 if (r != EOK)
[2bbbfd3]191 return r;
[5a29b4c]192
[8ab1adff]193 ino_i->i_izone[1] = zone;
[5a29b4c]194 ino_i->dirty = true;
[2bbbfd3]195 } else
196 return -1;
[930baca]197 }
[fde8a276]198
[8ab1adff]199 r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
[930baca]200 if (r != EOK)
[2bbbfd3]201 return r;
[930baca]202
[8c76c30]203 /*
204 *Compute the position of the second indirect
205 *zone pointer in the chain.
206 */
[8ab1adff]207 uint32_t ind2_off = rblock / ptrs_per_block;
[8c76c30]208
209 /*read the second indirect zone of the chain*/
[8ab1adff]210 if (ind_zone[ind2_off] == 0) {
[2d7c77a]211 if (write_mode) {
[8ab1adff]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;
[5a29b4c]221 }
[8ab1adff]222 }
[930baca]223
[8ab1adff]224 r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
225 if (r != EOK)
226 goto out_free_ind1;
[930baca]227
[8ab1adff]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);
[155f792]232 }
[8ab1adff]233
[8c76c30]234 r = EOK;
[930baca]235
[8ab1adff]236 free(ind2_zone);
237out_free_ind1:
238 free(ind_zone);
[930baca]239 return r;
240}
241
[5a29b4c]242
243static int
[8ab1adff]244reset_zone_content(struct mfs_instance *inst, uint32_t zone)
[5a29b4c]245{
246 block_t *b;
247 int r;
248
[8ab1adff]249 r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
[5a29b4c]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
[8ab1adff]261alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
[5a29b4c]262{
263 int r;
264
[8ab1adff]265 r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
[5a29b4c]266 if (r != EOK)
267 goto out;
268
[8ab1adff]269 r = reset_zone_content(inst, *zone);
[5a29b4c]270out:
271 return r;
272}
273
[8ab1adff]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;
[8f6bb76e]281 const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
282 sizeof(uint32_t);
[8ab1adff]283
[8f6bb76e]284 *ind_zone = malloc(max_ind_zone_ptrs);
[8ab1adff]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
[6adba0a8]341/**
342 * @}
343 */
344
Note: See TracBrowser for help on using the repository browser.