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

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

Add functions to read and write indirect zones, it simplifies the rw_map_ondisk() function

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