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

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

Fix memory leak in case of error in the prune_ind_zones() function.

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