source: mainline/uspace/srv/fs/mfs/mfs_rw.c@ 4e00f87

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

cstyle, fix typo

  • Property mode set to 100644
File size: 9.7 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
[36cb22f]33#include <align.h>
[930baca]34#include "mfs.h"
35
[2d7c77a]36static int
37rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
[6d4d883]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.
[980671e7]56 *
57 * @param b Pointer to a 32bit number where the block number will be stored
58 * @param mnode Pointer to a generic MINIX inode in memory.
59 * @param pos Position in file.
60 *
61 * @return EOK on success or a negative error code.
[930baca]62 */
[2bbbfd3]63int
[3a5ee6c]64mfs_read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
[930baca]65{
66 int r;
67 const struct mfs_sb_info *sbi = mnode->instance->sbi;
68 const int block_size = sbi->block_size;
69
[6d4d883]70 /* Compute relative block number in file */
[930baca]71 int rblock = pos / block_size;
72
[36cb22f]73 if (ROUND_UP(mnode->ino_i->i_size, sbi->block_size) < pos) {
[6d4d883]74 /* Trying to read beyond the end of file */
[930baca]75 r = EOK;
[155f792]76 *b = 0;
[930baca]77 goto out;
78 }
79
[2d7c77a]80 r = rw_map_ondisk(b, mnode, rblock, false, 0);
[930baca]81out:
82 return r;
83}
84
[2bbbfd3]85int
[3a5ee6c]86mfs_write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
[6d4d883]87 uint32_t *old_zone)
[2bbbfd3]88{
89 const struct mfs_sb_info *sbi = mnode->instance->sbi;
90
[9a0be34]91 if (pos >= sbi->max_file_size) {
[6d4d883]92 /* Can't write beyond the maximum file size */
[9a0be34]93 return EINVAL;
94 }
95
[6d4d883]96 /* Compute the relative block number in file */
[8a49fed]97 int rblock = pos / sbi->block_size;
[2bbbfd3]98
99 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
100}
101
[2d7c77a]102static int
103rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
[6d4d883]104 bool write_mode, uint32_t w_block)
[930baca]105{
[8c76c30]106 int r, nr_direct;
[155f792]107 int ptrs_per_block;
[8ab1adff]108 uint32_t *ind_zone, *ind2_zone;
[930baca]109
[2d7c77a]110 struct mfs_ino_info *ino_i = mnode->ino_i;
[5a29b4c]111 struct mfs_instance *inst = mnode->instance;
112 struct mfs_sb_info *sbi = inst->sbi;
[930baca]113
[2bbbfd3]114 const mfs_version_t fs_version = sbi->fs_version;
[9a0be34]115 const bool deleting = write_mode && (w_block == 0);
[155f792]116
117 if (fs_version == MFS_VERSION_V1) {
118 nr_direct = V1_NR_DIRECT_ZONES;
119 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
120 } else {
121 nr_direct = V2_NR_DIRECT_ZONES;
122 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
123 }
[930baca]124
[6d4d883]125 /* Check if the wanted block is in the direct zones */
[155f792]126 if (rblock < nr_direct) {
127 *b = ino_i->i_dzone[rblock];
[2d7c77a]128 if (write_mode) {
129 ino_i->i_dzone[rblock] = w_block;
130 ino_i->dirty = true;
131 }
[2bbbfd3]132 return EOK;
[930baca]133 }
[8f6bb76e]134
135 rblock -= nr_direct;
[930baca]136
137 if (rblock < ptrs_per_block) {
[6d4d883]138 /* The wanted block is in the single indirect zone chain */
[155f792]139 if (ino_i->i_izone[0] == 0) {
[9a0be34]140 if (write_mode && !deleting) {
[8ab1adff]141 uint32_t zone;
142 r = alloc_zone_and_clear(inst, &zone);
[c699b0c]143 if (r != EOK)
144 return r;
[5a29b4c]145
[8ab1adff]146 ino_i->i_izone[0] = zone;
[5a29b4c]147 ino_i->dirty = true;
[8f6bb76e]148 } else {
[e895352]149 /* Sparse block */
[b2a18234]150 *b = 0;
151 return EOK;
[8f6bb76e]152 }
[930baca]153 }
154
[8ab1adff]155 r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
[c699b0c]156 if (r != EOK)
157 return r;
[930baca]158
[8ab1adff]159 *b = ind_zone[rblock];
160 if (write_mode) {
161 ind_zone[rblock] = w_block;
162 write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
[152610a8]163 }
[930baca]164
[8ab1adff]165 goto out_free_ind1;
[930baca]166 }
167
[8f6bb76e]168 rblock -= ptrs_per_block;
[930baca]169
[6d4d883]170 /* The wanted block is in the double indirect zone chain */
[930baca]171
[6d4d883]172 /* Read the first indirect zone of the chain */
[155f792]173 if (ino_i->i_izone[1] == 0) {
[9a0be34]174 if (write_mode && !deleting) {
[8ab1adff]175 uint32_t zone;
176 r = alloc_zone_and_clear(inst, &zone);
[c699b0c]177 if (r != EOK)
178 return r;
[5a29b4c]179
[8ab1adff]180 ino_i->i_izone[1] = zone;
[5a29b4c]181 ino_i->dirty = true;
[b2a18234]182 } else {
[6d4d883]183 /* Sparse block */
[b2a18234]184 *b = 0;
185 return EOK;
186 }
[930baca]187 }
[fde8a276]188
[8ab1adff]189 r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
[c699b0c]190 if (r != EOK)
191 return r;
[930baca]192
[8c76c30]193 /*
[6d4d883]194 * Compute the position of the second indirect
195 * zone pointer in the chain.
[8c76c30]196 */
[8ab1adff]197 uint32_t ind2_off = rblock / ptrs_per_block;
[8c76c30]198
[6d4d883]199 /* read the second indirect zone of the chain */
[8ab1adff]200 if (ind_zone[ind2_off] == 0) {
[9a0be34]201 if (write_mode && !deleting) {
[8ab1adff]202 uint32_t zone;
203 r = alloc_zone_and_clear(inst, &zone);
[c699b0c]204 if (r != EOK)
205 goto out_free_ind1;
[1878386]206
[8ab1adff]207 ind_zone[ind2_off] = zone;
208 write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
209 } else {
[6d4d883]210 /* Sparse block */
[b2a18234]211 r = EOK;
212 *b = 0;
[8ab1adff]213 goto out_free_ind1;
[5a29b4c]214 }
[8ab1adff]215 }
[930baca]216
[8ab1adff]217 r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
[c699b0c]218 if (r != EOK)
219 goto out_free_ind1;
[930baca]220
[5987f917]221 *b = ind2_zone[rblock - (ind2_off * ptrs_per_block)];
[8ab1adff]222 if (write_mode) {
[5987f917]223 ind2_zone[rblock - (ind2_off * ptrs_per_block)] = w_block;
[8ab1adff]224 write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
[155f792]225 }
[8ab1adff]226
[8c76c30]227 r = EOK;
[930baca]228
[8ab1adff]229 free(ind2_zone);
230out_free_ind1:
231 free(ind_zone);
[930baca]232 return r;
233}
234
[e895352]235/**Free unused indirect zones from a MINIX inode according to its new size.
[980671e7]236 *
237 * @param mnode Pointer to a generic MINIX inode in memory.
238 * @param new_size The new size of the inode.
239 *
240 * @return EOK on success or a negative error code.
241 */
[1878386]242int
[3a5ee6c]243mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size)
[1878386]244{
245 struct mfs_instance *inst = mnode->instance;
246 struct mfs_sb_info *sbi = inst->sbi;
247 struct mfs_ino_info *ino_i = mnode->ino_i;
248 int nr_direct, ptrs_per_block, rblock, r;
249 int i;
250
251 mfs_version_t fs_version = sbi->fs_version;
252
[980671e7]253 assert(new_size <= ino_i->i_size);
254
[1878386]255 if (fs_version == MFS_VERSION_V1) {
256 nr_direct = V1_NR_DIRECT_ZONES;
257 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
258 } else {
259 nr_direct = V2_NR_DIRECT_ZONES;
260 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
261 }
262
263 rblock = new_size / sbi->block_size;
264
265 if (rblock < nr_direct) {
[6d4d883]266 /* Free the single indirect zone */
[1878386]267 if (ino_i->i_izone[0]) {
[70ac0af]268 r = mfs_free_zone(inst, ino_i->i_izone[0]);
[c699b0c]269 if (r != EOK)
270 return r;
[1878386]271
272 ino_i->i_izone[0] = 0;
273 ino_i->dirty = true;
274 }
275 }
276
277 rblock -= nr_direct + ptrs_per_block;
278
279 int fzone_to_free = (rblock < 0 ? 0 : rblock) / ptrs_per_block;
280
[01accb7]281 if ((fzone_to_free % ptrs_per_block) != 0)
282 ++fzone_to_free;
283
[6d4d883]284 /* Free the entire double indirect zone */
[1878386]285 uint32_t *dbl_zone;
286
[01accb7]287 if (ino_i->i_izone[1] == 0) {
[6d4d883]288 /* Nothing to be done */
[01accb7]289 return EOK;
290 }
291
[1878386]292 r = read_ind_zone(inst, ino_i->i_izone[1], &dbl_zone);
[c699b0c]293 if (r != EOK)
294 return r;
[1878386]295
296 for (i = fzone_to_free; i < ptrs_per_block; ++i) {
297 if (dbl_zone[i] == 0)
298 continue;
299
[70ac0af]300 r = mfs_free_zone(inst, dbl_zone[i]);
[c699b0c]301 if (r != EOK)
302 goto out;
[1878386]303 }
304
[01accb7]305 if (fzone_to_free == 0) {
[70ac0af]306 r = mfs_free_zone(inst, ino_i->i_izone[1]);
[1878386]307 ino_i->i_izone[1] = 0;
308 ino_i->dirty = true;
309 }
[dd9af5d]310out:
[1878386]311 free(dbl_zone);
312 return r;
313}
[5a29b4c]314
315static int
[8ab1adff]316reset_zone_content(struct mfs_instance *inst, uint32_t zone)
[5a29b4c]317{
318 block_t *b;
319 int r;
320
[03bc76a]321 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
[c699b0c]322 if (r != EOK)
323 return r;
[5a29b4c]324
325 memset(b->data, 0, b->size);
326 b->dirty = true;
327
[5f6168d]328 return block_put(b);
[5a29b4c]329}
330
331static int
[8ab1adff]332alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
[5a29b4c]333{
334 int r;
335
[70ac0af]336 r = mfs_alloc_zone(inst, zone);
[c699b0c]337 if (r != EOK)
338 return r;
[5a29b4c]339
[8ab1adff]340 r = reset_zone_content(inst, *zone);
[5a29b4c]341 return r;
342}
343
[8ab1adff]344static int
345read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
346{
347 struct mfs_sb_info *sbi = inst->sbi;
348 int r;
349 unsigned i;
350 block_t *b;
[8f6bb76e]351 const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
[6d4d883]352 sizeof(uint32_t);
[8ab1adff]353
[8f6bb76e]354 *ind_zone = malloc(max_ind_zone_ptrs);
[8ab1adff]355 if (*ind_zone == NULL)
356 return ENOMEM;
357
[03bc76a]358 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NONE);
[8ab1adff]359 if (r != EOK) {
360 free(*ind_zone);
361 return r;
362 }
363
364 if (sbi->fs_version == MFS_VERSION_V1) {
365 uint16_t *src_ptr = b->data;
366
367 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
368 (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
369 } else {
370 uint32_t *src_ptr = b->data;
371
372 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
373 (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
374 }
375
[5f6168d]376 return block_put(b);
[8ab1adff]377}
378
379static int
380write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
381{
382 struct mfs_sb_info *sbi = inst->sbi;
383 int r;
384 unsigned i;
385 block_t *b;
386
[5e0427e8]387 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
[c699b0c]388 if (r != EOK)
389 return r;
[8ab1adff]390
391 if (sbi->fs_version == MFS_VERSION_V1) {
392 uint16_t *dest_ptr = b->data;
393
394 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
395 dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
396 } else {
397 uint32_t *dest_ptr = b->data;
398
399 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
400 dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
401
402 }
403 b->dirty = true;
[5f6168d]404
405 return block_put(b);
[8ab1adff]406}
407
408
[6adba0a8]409/**
410 * @}
[44c6091f]411 */
[6adba0a8]412
Note: See TracBrowser for help on using the repository browser.