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

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

Add function to free unused indirect zones when a file is resized.
Use on_error() macro instead of if() else..

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