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
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 <assert.h>
34#include <errno.h>
35#include "mfs.h"
36#include "mfs_utils.h"
37
38static int
39rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
40 bool write_mode, uint32_t w_block);
41
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
49/**Given the position in the file expressed in
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
69 if (mnode->ino_i->i_size < (int32_t) pos) {
70 /*Trying to read beyond the end of file*/
71 r = EOK;
72 *b = 0;
73 goto out;
74 }
75
76 r = rw_map_ondisk(b, mnode, rblock, false, 0);
77out:
78 return r;
79}
80
81static int
82rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
83 bool write_mode, uint32_t w_block)
84{
85 int r, nr_direct;
86 int ptrs_per_block;
87 block_t *bi1;
88 block_t *bi2;
89
90 assert(mnode);
91 struct mfs_ino_info *ino_i = mnode->ino_i;
92
93 assert(ino_i);
94 assert(mnode->instance);
95
96 struct mfs_instance *inst = mnode->instance;
97 struct mfs_sb_info *sbi = inst->sbi;
98 assert(sbi);
99
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 }
109
110 /*Check if the wanted block is in the direct zones*/
111 if (rblock < nr_direct) {
112 *b = ino_i->i_dzone[rblock];
113 if (write_mode) {
114 ino_i->i_dzone[rblock] = w_block;
115 ino_i->dirty = true;
116 }
117 r = EOK;
118 goto out;
119 }
120 rblock -= nr_direct - 1;
121
122 if (rblock < ptrs_per_block) {
123 /*The wanted block is in the single indirect zone chain*/
124 if (ino_i->i_izone[0] == 0) {
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 }
137 }
138
139 r = block_get(&bi1, inst->handle, ino_i->i_izone[0],
140 BLOCK_FLAGS_NONE);
141 if (r != EOK)
142 goto out;
143
144 if (fs_version == MFS_VERSION_V1) {
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 }
151 } else {
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 }
158 }
159
160 goto out_put_1;
161 }
162
163 rblock -= ptrs_per_block - 1;
164
165 /*The wanted block is in the double indirect zone chain*/
166
167 /*read the first indirect zone of the chain*/
168 if (ino_i->i_izone[1] == 0) {
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 }
181 }
182
183 r = block_get(&bi1, inst->handle, ino_i->i_izone[1],
184 BLOCK_FLAGS_NONE);
185
186 if (r != EOK)
187 goto out;
188
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*/
196 if (fs_version == MFS_VERSION_V1) {
197 uint16_t *pt16 = bi1->data;
198 uint16_t blk = conv16(sbi->native, pt16[di_block]);
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 }
215
216 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
217
218 if (r != EOK)
219 goto out_put_1;
220
221 pt16 = bi2->data;
222 pt16 += di_block % ptrs_per_block;
223 *b = conv16(sbi->native, *pt16);
224 if (write_mode) {
225 *pt16 = conv16(sbi->native, w_block);
226 bi2->dirty = true;
227 }
228 } else {
229 uint32_t *pt32 = bi1->data;
230 uint32_t blk = conv32(sbi->native, pt32[di_block]);
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 }
247
248 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
249
250 if (r != EOK)
251 goto out_put_1;
252
253 pt32 = bi2->data;
254 pt32 += di_block % ptrs_per_block;
255 *b = conv32(sbi->native, *pt32);
256 if (write_mode) {
257 *pt32 = conv32(sbi->native, w_block);
258 bi2->dirty = true;
259 }
260 }
261 r = EOK;
262
263 block_put(bi2);
264out_put_1:
265 block_put(bi1);
266out:
267 return r;
268}
269
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
302/**
303 * @}
304 */
305
Note: See TracBrowser for help on using the repository browser.