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

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

mfs minor fixes:

  • remove old asserts
  • do not allow to write beyond the maximum file size
  • fix a little bug when deleting sparse files
  • Property mode set to 100644
File size: 9.2 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
58mfs_read_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
80mfs_write_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 if (pos >= sbi->max_file_size) {
86 /*Can't write beyond the maximum file size*/
87 return EINVAL;
88 }
89
90 /*Compute the relative block number in file*/
91 int rblock = pos / sbi->block_size;
92
93 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
94}
95
96static int
97rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
98 bool write_mode, uint32_t w_block)
99{
100 int r, nr_direct;
101 int ptrs_per_block;
102 uint32_t *ind_zone, *ind2_zone;
103
104 struct mfs_ino_info *ino_i = mnode->ino_i;
105 struct mfs_instance *inst = mnode->instance;
106 struct mfs_sb_info *sbi = inst->sbi;
107
108 const mfs_version_t fs_version = sbi->fs_version;
109 const bool deleting = write_mode && (w_block == 0);
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 && !deleting) {
135 uint32_t zone;
136 r = alloc_zone_and_clear(inst, &zone);
137 if (r != EOK)
138 return r;
139
140 ino_i->i_izone[0] = zone;
141 ino_i->dirty = true;
142 } else {
143 /*Sparse block*/
144 *b = 0;
145 return EOK;
146 }
147 }
148
149 r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
150 if (r != EOK)
151 return r;
152
153 *b = ind_zone[rblock];
154 if (write_mode) {
155 ind_zone[rblock] = w_block;
156 write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
157 }
158
159 goto out_free_ind1;
160 }
161
162 rblock -= ptrs_per_block;
163
164 /*The wanted block is in the double indirect zone chain*/
165
166 /*read the first indirect zone of the chain*/
167 if (ino_i->i_izone[1] == 0) {
168 if (write_mode && !deleting) {
169 uint32_t zone;
170 r = alloc_zone_and_clear(inst, &zone);
171 if (r != EOK)
172 return r;
173
174 ino_i->i_izone[1] = zone;
175 ino_i->dirty = true;
176 } else {
177 /*Sparse block*/
178 *b = 0;
179 return EOK;
180 }
181 }
182
183 r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
184 if (r != EOK)
185 return r;
186
187 /*
188 *Compute the position of the second indirect
189 *zone pointer in the chain.
190 */
191 uint32_t ind2_off = rblock / ptrs_per_block;
192
193 /*read the second indirect zone of the chain*/
194 if (ind_zone[ind2_off] == 0) {
195 if (write_mode && !deleting) {
196 uint32_t zone;
197 r = alloc_zone_and_clear(inst, &zone);
198 if (r != EOK)
199 goto out_free_ind1;
200
201 ind_zone[ind2_off] = zone;
202 write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
203 } else {
204 /*Sparse block*/
205 r = EOK;
206 *b = 0;
207 goto out_free_ind1;
208 }
209 }
210
211 r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
212 if (r != EOK)
213 goto out_free_ind1;
214
215 *b = ind2_zone[rblock - (ind2_off * ptrs_per_block)];
216 if (write_mode) {
217 ind2_zone[rblock - (ind2_off * ptrs_per_block)] = w_block;
218 write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
219 }
220
221 r = EOK;
222
223 free(ind2_zone);
224out_free_ind1:
225 free(ind_zone);
226 return r;
227}
228
229/*Free unused indirect zones*/
230int
231mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size)
232{
233 struct mfs_instance *inst = mnode->instance;
234 struct mfs_sb_info *sbi = inst->sbi;
235 struct mfs_ino_info *ino_i = mnode->ino_i;
236 int nr_direct, ptrs_per_block, rblock, r;
237 int i;
238
239 mfs_version_t fs_version = sbi->fs_version;
240
241 if (fs_version == MFS_VERSION_V1) {
242 nr_direct = V1_NR_DIRECT_ZONES;
243 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
244 } else {
245 nr_direct = V2_NR_DIRECT_ZONES;
246 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
247 }
248
249 rblock = new_size / sbi->block_size;
250
251 if (rblock < nr_direct) {
252 /*free the single indirect zone*/
253 if (ino_i->i_izone[0]) {
254 r = mfs_free_zone(inst, ino_i->i_izone[0]);
255 if (r != EOK)
256 return r;
257
258 ino_i->i_izone[0] = 0;
259 ino_i->dirty = true;
260 }
261 }
262
263 rblock -= nr_direct + ptrs_per_block;
264
265 int fzone_to_free = (rblock < 0 ? 0 : rblock) / ptrs_per_block;
266
267 if ((fzone_to_free % ptrs_per_block) != 0)
268 ++fzone_to_free;
269
270 /*free the entire double indirect zone*/
271 uint32_t *dbl_zone;
272
273 if (ino_i->i_izone[1] == 0) {
274 /*Nothing to be done*/
275 return EOK;
276 }
277
278 r = read_ind_zone(inst, ino_i->i_izone[1], &dbl_zone);
279 if (r != EOK)
280 return r;
281
282 for (i = fzone_to_free; i < ptrs_per_block; ++i) {
283 if (dbl_zone[i] == 0)
284 continue;
285
286 r = mfs_free_zone(inst, dbl_zone[i]);
287 if (r != EOK)
288 goto out;
289 }
290
291 if (fzone_to_free == 0) {
292 r = mfs_free_zone(inst, ino_i->i_izone[1]);
293 ino_i->i_izone[1] = 0;
294 ino_i->dirty = true;
295 }
296out:
297 free(dbl_zone);
298 return r;
299}
300
301static int
302reset_zone_content(struct mfs_instance *inst, uint32_t zone)
303{
304 block_t *b;
305 int r;
306
307 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
308 if (r != EOK)
309 return r;
310
311 memset(b->data, 0, b->size);
312 b->dirty = true;
313
314 return block_put(b);
315}
316
317static int
318alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
319{
320 int r;
321
322 r = mfs_alloc_zone(inst, zone);
323 if (r != EOK)
324 return r;
325
326 r = reset_zone_content(inst, *zone);
327 return r;
328}
329
330static int
331read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
332{
333 struct mfs_sb_info *sbi = inst->sbi;
334 int r;
335 unsigned i;
336 block_t *b;
337 const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
338 sizeof(uint32_t);
339
340 *ind_zone = malloc(max_ind_zone_ptrs);
341 if (*ind_zone == NULL)
342 return ENOMEM;
343
344 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NONE);
345 if (r != EOK) {
346 free(*ind_zone);
347 return r;
348 }
349
350 if (sbi->fs_version == MFS_VERSION_V1) {
351 uint16_t *src_ptr = b->data;
352
353 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
354 (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
355 } else {
356 uint32_t *src_ptr = b->data;
357
358 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
359 (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
360 }
361
362 return block_put(b);
363}
364
365static int
366write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
367{
368 struct mfs_sb_info *sbi = inst->sbi;
369 int r;
370 unsigned i;
371 block_t *b;
372
373 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
374 if (r != EOK)
375 return r;
376
377 if (sbi->fs_version == MFS_VERSION_V1) {
378 uint16_t *dest_ptr = b->data;
379
380 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
381 dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
382 } else {
383 uint32_t *dest_ptr = b->data;
384
385 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
386 dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
387
388 }
389 b->dirty = true;
390
391 return block_put(b);
392}
393
394
395/**
396 * @}
397 */
398
Note: See TracBrowser for help on using the repository browser.