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

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

cstyle

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