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

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

fix to free_zone(): return result og mfs_free_bit() function

  • Property mode set to 100644
File size: 7.2 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
44reset_block_content(struct mfs_instance *inst, uint32_t block);
45
46static int
47alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *block);
48
49
[2d7c77a]50/**Given the position in the file expressed in
[930baca]51 *bytes, this function returns the on-disk block
52 *relative to that position.
53 *Returns zero if the block does not exist.
54 */
[2bbbfd3]55int
56read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
[930baca]57{
58 int r;
59 const struct mfs_sb_info *sbi = mnode->instance->sbi;
60 const int block_size = sbi->block_size;
61
62 /*Compute relative block number in file*/
63 int rblock = pos / block_size;
64
[155f792]65 if (mnode->ino_i->i_size < (int32_t) pos) {
[cfbcd86]66 /*Trying to read beyond the end of file*/
[930baca]67 r = EOK;
[155f792]68 *b = 0;
[930baca]69 goto out;
70 }
71
[2d7c77a]72 r = rw_map_ondisk(b, mnode, rblock, false, 0);
[930baca]73out:
74 return r;
75}
76
[2bbbfd3]77int
78write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
79 uint32_t *old_zone)
80{
81 const struct mfs_sb_info *sbi = mnode->instance->sbi;
82 const int block_size = sbi->block_size;
83
84 /*Compute the relative block number in file*/
85 int rblock = pos / block_size;
86
87 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
88}
89
90int
91free_zone(struct mfs_node *mnode, const uint32_t zone)
92{
93 int r;
94 uint32_t old_zone;
95
96 r = rw_map_ondisk(&old_zone, mnode, zone, true, 0);
97 if (r != EOK)
98 return r;
99
100 if (old_zone > 0)
[40f7297]101 r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
[2bbbfd3]102
[40f7297]103 return r;
[2bbbfd3]104}
105
[2d7c77a]106static int
107rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
108 bool write_mode, uint32_t w_block)
[930baca]109{
[8c76c30]110 int r, nr_direct;
[155f792]111 int ptrs_per_block;
[152610a8]112 block_t *bi1;
113 block_t *bi2;
[930baca]114
115 assert(mnode);
[2d7c77a]116 struct mfs_ino_info *ino_i = mnode->ino_i;
[930baca]117
[155f792]118 assert(ino_i);
[930baca]119 assert(mnode->instance);
120
[5a29b4c]121 struct mfs_instance *inst = mnode->instance;
122 struct mfs_sb_info *sbi = inst->sbi;
[930baca]123 assert(sbi);
124
[2bbbfd3]125 const mfs_version_t fs_version = sbi->fs_version;
[155f792]126
127 if (fs_version == MFS_VERSION_V1) {
128 nr_direct = V1_NR_DIRECT_ZONES;
129 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
130 } else {
131 nr_direct = V2_NR_DIRECT_ZONES;
132 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
133 }
[930baca]134
[8c76c30]135 /*Check if the wanted block is in the direct zones*/
[155f792]136 if (rblock < nr_direct) {
137 *b = ino_i->i_dzone[rblock];
[2d7c77a]138 if (write_mode) {
139 ino_i->i_dzone[rblock] = w_block;
140 ino_i->dirty = true;
141 }
[2bbbfd3]142 return EOK;
[930baca]143 }
[155f792]144 rblock -= nr_direct - 1;
[930baca]145
146 if (rblock < ptrs_per_block) {
[8c76c30]147 /*The wanted block is in the single indirect zone chain*/
[155f792]148 if (ino_i->i_izone[0] == 0) {
[5a29b4c]149 if (write_mode) {
150 uint32_t block;
151 r = alloc_zone_and_clear(inst, &block);
152 if (r != EOK)
[2bbbfd3]153 return r;
[5a29b4c]154
155 ino_i->i_izone[0] = block;
156 ino_i->dirty = true;
[2bbbfd3]157 } else
158 return -1;
[930baca]159 }
160
[152610a8]161 r = block_get(&bi1, inst->handle, ino_i->i_izone[0],
162 BLOCK_FLAGS_NONE);
163 if (r != EOK)
[2bbbfd3]164 return r;
[930baca]165
[152610a8]166 if (fs_version == MFS_VERSION_V1) {
[2d7c77a]167 uint16_t *tmp = &(((uint16_t *) bi1->data)[rblock]);
168 *b = conv16(sbi->native, *tmp);
169 if (write_mode) {
170 *tmp = conv16(sbi->native, w_block);
171 bi1->dirty = true;
172 }
[152610a8]173 } else {
[2d7c77a]174 uint32_t *tmp = &(((uint32_t *) bi1->data)[rblock]);
175 *b = conv32(sbi->native, *tmp);
176 if (write_mode) {
177 *tmp = conv32(sbi->native, w_block);
178 bi1->dirty = true;
179 }
[152610a8]180 }
[930baca]181
[152610a8]182 goto out_put_1;
[930baca]183 }
184
185 rblock -= ptrs_per_block - 1;
186
[8c76c30]187 /*The wanted block is in the double indirect zone chain*/
[930baca]188
[8c76c30]189 /*read the first indirect zone of the chain*/
[155f792]190 if (ino_i->i_izone[1] == 0) {
[5a29b4c]191 if (write_mode) {
192 uint32_t block;
193 r = alloc_zone_and_clear(inst, &block);
194 if (r != EOK)
[2bbbfd3]195 return r;
[5a29b4c]196
197 ino_i->i_izone[1] = block;
198 ino_i->dirty = true;
[2bbbfd3]199 } else
200 return -1;
[930baca]201 }
[fde8a276]202
[152610a8]203 r = block_get(&bi1, inst->handle, ino_i->i_izone[1],
204 BLOCK_FLAGS_NONE);
[930baca]205 if (r != EOK)
[2bbbfd3]206 return r;
[930baca]207
[8c76c30]208 /*
209 *Compute the position of the second indirect
210 *zone pointer in the chain.
211 */
[af8ce880]212 uint32_t ind2_block = rblock / ptrs_per_block;
[8c76c30]213
214 /*read the second indirect zone of the chain*/
[155f792]215 if (fs_version == MFS_VERSION_V1) {
[152610a8]216 uint16_t *pt16 = bi1->data;
[af8ce880]217 uint16_t blk = conv16(sbi->native, pt16[ind2_block]);
[5a29b4c]218
219 if (blk == 0) {
220 if (write_mode) {
221 uint32_t block;
222 r = alloc_zone_and_clear(inst, &block);
223 if (r != EOK)
[2bbbfd3]224 return r;
[5a29b4c]225
226 blk = block;
[af8ce880]227 pt16[ind2_block] = conv16(sbi->native, blk);
[5a29b4c]228 bi1->dirty = true;
[2bbbfd3]229 } else
230 return -1;
[5a29b4c]231 }
[2d7c77a]232
[152610a8]233 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
[155f792]234
235 if (r != EOK)
[152610a8]236 goto out_put_1;
[155f792]237
[152610a8]238 pt16 = bi2->data;
[af8ce880]239 pt16 += ind2_block % ptrs_per_block;
[2d7c77a]240 *b = conv16(sbi->native, *pt16);
241 if (write_mode) {
242 *pt16 = conv16(sbi->native, w_block);
[18346ec]243 bi2->dirty = true;
[2d7c77a]244 }
[155f792]245 } else {
[152610a8]246 uint32_t *pt32 = bi1->data;
[af8ce880]247 uint32_t blk = conv32(sbi->native, pt32[ind2_block]);
[5a29b4c]248
249 if (blk == 0) {
250 if (write_mode) {
251 uint32_t block;
252 r = alloc_zone_and_clear(inst, &block);
253 if (r != EOK)
[2bbbfd3]254 return r;
[5a29b4c]255
256 blk = block;
[af8ce880]257 pt32[ind2_block] = conv32(sbi->native, blk);
[5a29b4c]258 bi1->dirty = true;
[2bbbfd3]259 } else
260 return -1;
[5a29b4c]261 }
[2d7c77a]262
[152610a8]263 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
[930baca]264
[155f792]265 if (r != EOK)
[152610a8]266 goto out_put_1;
[930baca]267
[152610a8]268 pt32 = bi2->data;
[af8ce880]269 pt32 += ind2_block % ptrs_per_block;
[2d7c77a]270 *b = conv32(sbi->native, *pt32);
271 if (write_mode) {
272 *pt32 = conv32(sbi->native, w_block);
[18346ec]273 bi2->dirty = true;
[2d7c77a]274 }
[155f792]275 }
[8c76c30]276 r = EOK;
[930baca]277
[152610a8]278 block_put(bi2);
279out_put_1:
280 block_put(bi1);
[930baca]281 return r;
282}
283
[5a29b4c]284
285static int
286reset_block_content(struct mfs_instance *inst, uint32_t block)
287{
288 block_t *b;
289 int r;
290
291 r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NOREAD);
292 if (r != EOK)
293 return r;
294
295 memset(b->data, 0, b->size);
296 b->dirty = true;
297 block_put(b);
298
299 return EOK;
300}
301
302static int
303alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *block)
304{
305 int r;
306
307 r = mfs_alloc_bit(inst, block, BMAP_ZONE);
308 if (r != EOK)
309 goto out;
310
311 r = reset_block_content(inst, *block);
312out:
313 return r;
314}
315
[6adba0a8]316/**
317 * @}
318 */
319
Note: See TracBrowser for help on using the repository browser.