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

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

Fix compile time errors (malloc.h has been added)

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