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

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

Remove the inode_grow() function and fix support to sparse files

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[6adba0a8]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
[930baca]33#include "mfs.h"
34
[2d7c77a]35static int
36rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
[44c6091f]37 bool write_mode, uint32_t w_block);
[930baca]38
[5a29b4c]39static int
[8ab1adff]40reset_zone_content(struct mfs_instance *inst, uint32_t zone);
[5a29b4c]41
42static int
[8ab1adff]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);
[5a29b4c]50
51
[2d7c77a]52/**Given the position in the file expressed in
[930baca]53 *bytes, this function returns the on-disk block
54 *relative to that position.
55 *Returns zero if the block does not exist.
56 */
[2bbbfd3]57int
58read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
[930baca]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
[8f6bb76e]67 if (mnode->ino_i->i_size < pos) {
[cfbcd86]68 /*Trying to read beyond the end of file*/
[930baca]69 r = EOK;
[155f792]70 *b = 0;
[930baca]71 goto out;
72 }
73
[2d7c77a]74 r = rw_map_ondisk(b, mnode, rblock, false, 0);
[930baca]75out:
76 return r;
77}
78
[2bbbfd3]79int
[44c6091f]80write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
81 uint32_t *old_zone)
[2bbbfd3]82{
83 const struct mfs_sb_info *sbi = mnode->instance->sbi;
84
85 /*Compute the relative block number in file*/
[8a49fed]86 int rblock = pos / sbi->block_size;
[2bbbfd3]87
88 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
89}
90
[2d7c77a]91static int
92rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
[44c6091f]93 bool write_mode, uint32_t w_block)
[930baca]94{
[8c76c30]95 int r, nr_direct;
[155f792]96 int ptrs_per_block;
[8ab1adff]97 uint32_t *ind_zone, *ind2_zone;
[930baca]98
99 assert(mnode);
[2d7c77a]100 struct mfs_ino_info *ino_i = mnode->ino_i;
[930baca]101
[155f792]102 assert(ino_i);
[930baca]103 assert(mnode->instance);
104
[5a29b4c]105 struct mfs_instance *inst = mnode->instance;
106 struct mfs_sb_info *sbi = inst->sbi;
[930baca]107 assert(sbi);
108
[2bbbfd3]109 const mfs_version_t fs_version = sbi->fs_version;
[155f792]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 }
[930baca]118
[8c76c30]119 /*Check if the wanted block is in the direct zones*/
[155f792]120 if (rblock < nr_direct) {
121 *b = ino_i->i_dzone[rblock];
[2d7c77a]122 if (write_mode) {
123 ino_i->i_dzone[rblock] = w_block;
124 ino_i->dirty = true;
125 }
[2bbbfd3]126 return EOK;
[930baca]127 }
[8f6bb76e]128
129 rblock -= nr_direct;
[930baca]130
131 if (rblock < ptrs_per_block) {
[8c76c30]132 /*The wanted block is in the single indirect zone chain*/
[155f792]133 if (ino_i->i_izone[0] == 0) {
[5a29b4c]134 if (write_mode) {
[8ab1adff]135 uint32_t zone;
136 r = alloc_zone_and_clear(inst, &zone);
[b2a18234]137 on_error(r, return r);
[5a29b4c]138
[8ab1adff]139 ino_i->i_izone[0] = zone;
[5a29b4c]140 ino_i->dirty = true;
[8f6bb76e]141 } else {
[b2a18234]142 /*Sparse block*/
143 *b = 0;
144 return EOK;
[8f6bb76e]145 }
[930baca]146 }
147
[8ab1adff]148 r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
[1878386]149 on_error(r, return r);
[930baca]150
[8ab1adff]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);
[152610a8]155 }
[930baca]156
[8ab1adff]157 goto out_free_ind1;
[930baca]158 }
159
[8f6bb76e]160 rblock -= ptrs_per_block;
[930baca]161
[8c76c30]162 /*The wanted block is in the double indirect zone chain*/
[930baca]163
[8c76c30]164 /*read the first indirect zone of the chain*/
[155f792]165 if (ino_i->i_izone[1] == 0) {
[5a29b4c]166 if (write_mode) {
[8ab1adff]167 uint32_t zone;
168 r = alloc_zone_and_clear(inst, &zone);
[1878386]169 on_error(r, return r);
[5a29b4c]170
[8ab1adff]171 ino_i->i_izone[1] = zone;
[5a29b4c]172 ino_i->dirty = true;
[b2a18234]173 } else {
174 /*Sparse block*/
175 *b = 0;
176 return EOK;
177 }
[930baca]178 }
[fde8a276]179
[8ab1adff]180 r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
[1878386]181 on_error(r, return r);
[930baca]182
[8c76c30]183 /*
184 *Compute the position of the second indirect
185 *zone pointer in the chain.
186 */
[8ab1adff]187 uint32_t ind2_off = rblock / ptrs_per_block;
[8c76c30]188
189 /*read the second indirect zone of the chain*/
[8ab1adff]190 if (ind_zone[ind2_off] == 0) {
[2d7c77a]191 if (write_mode) {
[8ab1adff]192 uint32_t zone;
193 r = alloc_zone_and_clear(inst, &zone);
[1878386]194 on_error(r, goto out_free_ind1);
195
[8ab1adff]196 ind_zone[ind2_off] = zone;
197 write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
198 } else {
[b2a18234]199 /*Sparse block*/
200 r = EOK;
201 *b = 0;
[8ab1adff]202 goto out_free_ind1;
[5a29b4c]203 }
[8ab1adff]204 }
[930baca]205
[8ab1adff]206 r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
[1878386]207 on_error(r, goto out_free_ind1);
[930baca]208
[8ab1adff]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);
[155f792]213 }
[8ab1adff]214
[8c76c30]215 r = EOK;
[930baca]216
[8ab1adff]217 free(ind2_zone);
218out_free_ind1:
219 free(ind_zone);
[930baca]220 return r;
221}
222
[1878386]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]) {
[70ac0af]248 r = mfs_free_zone(inst, ino_i->i_izone[0]);
[1878386]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
[01accb7]260 if ((fzone_to_free % ptrs_per_block) != 0)
261 ++fzone_to_free;
262
[1878386]263 /*free the entire double indirect zone*/
264 uint32_t *dbl_zone;
265
[01accb7]266 if (ino_i->i_izone[1] == 0) {
267 /*Nothing to be done*/
268 return EOK;
269 }
270
[1878386]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
[70ac0af]278 r = mfs_free_zone(inst, dbl_zone[i]);
[1878386]279 on_error(r, return r);
280 }
281
[01accb7]282 if (fzone_to_free == 0) {
[70ac0af]283 r = mfs_free_zone(inst, ino_i->i_izone[1]);
[1878386]284 ino_i->i_izone[1] = 0;
285 ino_i->dirty = true;
286 }
287 free(dbl_zone);
288
289 return r;
290}
[5a29b4c]291
292static int
[8ab1adff]293reset_zone_content(struct mfs_instance *inst, uint32_t zone)
[5a29b4c]294{
295 block_t *b;
296 int r;
297
[8ab1adff]298 r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
[1878386]299 on_error(r, return r);
[5a29b4c]300
301 memset(b->data, 0, b->size);
302 b->dirty = true;
303
[5f6168d]304 return block_put(b);
[5a29b4c]305}
306
307static int
[8ab1adff]308alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
[5a29b4c]309{
310 int r;
311
[70ac0af]312 r = mfs_alloc_zone(inst, zone);
[1878386]313 on_error(r, return r);
[5a29b4c]314
[8ab1adff]315 r = reset_zone_content(inst, *zone);
[5a29b4c]316 return r;
317}
318
[8ab1adff]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;
[8f6bb76e]326 const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
[44c6091f]327 sizeof(uint32_t);
[8ab1adff]328
[8f6bb76e]329 *ind_zone = malloc(max_ind_zone_ptrs);
[8ab1adff]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
[5f6168d]351 return block_put(b);
[8ab1adff]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);
[1878386]363 on_error(r, return r);
[8ab1adff]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;
[5f6168d]378
379 return block_put(b);
[8ab1adff]380}
381
382
[6adba0a8]383/**
384 * @}
[44c6091f]385 */
[6adba0a8]386
Note: See TracBrowser for help on using the repository browser.