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

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

rename mfs_read.c, new name is mfs_rw.c

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