source: mainline/uspace/srv/fs/minixfs/mfs_read.c@ 2d7c77a

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

Modify read_map_ondisk() to support write mode, it has been renamed to rw_map_ondisk.

When write_mode is set at "true", a reference to a block (which number is specified in the parameter
w_block) will be saved at the given position.

  • Property mode set to 100644
File size: 5.2 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 <assert.h>
34#include <errno.h>
35#include "mfs.h"
36#include "mfs_utils.h"
37
[2d7c77a]38static int
39rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
40 bool write_mode, uint32_t w_block);
[930baca]41
[2d7c77a]42/**Given the position in the file expressed in
[930baca]43 *bytes, this function returns the on-disk block
44 *relative to that position.
45 *Returns zero if the block does not exist.
46 */
47int read_map(uint32_t *b, const struct mfs_node *mnode, const uint32_t pos)
48{
49 int r;
50
51 assert(mnode);
52 assert(mnode->instance);
53
54 const struct mfs_sb_info *sbi = mnode->instance->sbi;
55 assert(sbi);
56
57 const int block_size = sbi->block_size;
58
59 /*Compute relative block number in file*/
60 int rblock = pos / block_size;
61
[155f792]62 if (mnode->ino_i->i_size < (int32_t) pos) {
[cfbcd86]63 /*Trying to read beyond the end of file*/
[930baca]64 r = EOK;
[155f792]65 *b = 0;
[930baca]66 goto out;
67 }
68
[2d7c77a]69 r = rw_map_ondisk(b, mnode, rblock, false, 0);
[930baca]70out:
71 return r;
72}
73
[2d7c77a]74static int
75rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
76 bool write_mode, uint32_t w_block)
[930baca]77{
[8c76c30]78 int r, nr_direct;
[155f792]79 int ptrs_per_block;
[152610a8]80 block_t *bi1;
81 block_t *bi2;
[930baca]82
83 assert(mnode);
[2d7c77a]84 struct mfs_ino_info *ino_i = mnode->ino_i;
[930baca]85
[155f792]86 assert(ino_i);
[930baca]87 assert(mnode->instance);
88
[152610a8]89 const struct mfs_instance *inst = mnode->instance;
90 const struct mfs_sb_info *sbi = inst->sbi;
[930baca]91 assert(sbi);
92
[155f792]93 const int fs_version = sbi->fs_version;
94
95 if (fs_version == MFS_VERSION_V1) {
96 nr_direct = V1_NR_DIRECT_ZONES;
97 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
98 } else {
99 nr_direct = V2_NR_DIRECT_ZONES;
100 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
101 }
[930baca]102
[8c76c30]103 /*Check if the wanted block is in the direct zones*/
[155f792]104 if (rblock < nr_direct) {
105 *b = ino_i->i_dzone[rblock];
[2d7c77a]106 if (write_mode) {
107 ino_i->i_dzone[rblock] = w_block;
108 ino_i->dirty = true;
109 }
[930baca]110 r = EOK;
111 goto out;
112 }
[155f792]113 rblock -= nr_direct - 1;
[930baca]114
115 if (rblock < ptrs_per_block) {
[8c76c30]116 /*The wanted block is in the single indirect zone chain*/
[155f792]117 if (ino_i->i_izone[0] == 0) {
[930baca]118 r = -1;
119 goto out;
120 }
121
[152610a8]122 r = block_get(&bi1, inst->handle, ino_i->i_izone[0],
123 BLOCK_FLAGS_NONE);
124 if (r != EOK)
125 goto out;
[930baca]126
[152610a8]127 if (fs_version == MFS_VERSION_V1) {
[2d7c77a]128 uint16_t *tmp = &(((uint16_t *) bi1->data)[rblock]);
129 *b = conv16(sbi->native, *tmp);
130 if (write_mode) {
131 *tmp = conv16(sbi->native, w_block);
132 bi1->dirty = true;
133 }
[152610a8]134 } else {
[2d7c77a]135 uint32_t *tmp = &(((uint32_t *) bi1->data)[rblock]);
136 *b = conv32(sbi->native, *tmp);
137 if (write_mode) {
138 *tmp = conv32(sbi->native, w_block);
139 bi1->dirty = true;
140 }
[152610a8]141 }
[930baca]142
[152610a8]143 goto out_put_1;
[930baca]144 }
145
146 rblock -= ptrs_per_block - 1;
147
[8c76c30]148 /*The wanted block is in the double indirect zone chain*/
[930baca]149
[8c76c30]150 /*read the first indirect zone of the chain*/
[155f792]151 if (ino_i->i_izone[1] == 0) {
[930baca]152 r = -1;
153 goto out;
154 }
[fde8a276]155
[152610a8]156 r = block_get(&bi1, inst->handle, ino_i->i_izone[1],
157 BLOCK_FLAGS_NONE);
[930baca]158
159 if (r != EOK)
160 goto out;
161
[8c76c30]162 /*
163 *Compute the position of the second indirect
164 *zone pointer in the chain.
165 */
166 uint32_t di_block = rblock / ptrs_per_block;
167
168 /*read the second indirect zone of the chain*/
[155f792]169 if (fs_version == MFS_VERSION_V1) {
[152610a8]170 uint16_t *pt16 = bi1->data;
171 uint16_t blk = conv16(sbi->native, pt16[di_block]);
[2d7c77a]172
[152610a8]173 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
[155f792]174
175 if (r != EOK)
[152610a8]176 goto out_put_1;
[155f792]177
[152610a8]178 pt16 = bi2->data;
[2d7c77a]179 pt16 += di_block % ptrs_per_block;
180 *b = conv16(sbi->native, *pt16);
181 if (write_mode) {
182 *pt16 = conv16(sbi->native, w_block);
183 bi2->dirty = false;
184 }
[155f792]185 } else {
[152610a8]186 uint32_t *pt32 = bi1->data;
187 uint32_t blk = conv32(sbi->native, pt32[di_block]);
[2d7c77a]188
[152610a8]189 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
[930baca]190
[155f792]191 if (r != EOK)
[152610a8]192 goto out_put_1;
[930baca]193
[152610a8]194 pt32 = bi2->data;
[2d7c77a]195 pt32 += di_block % ptrs_per_block;
196 *b = conv32(sbi->native, *pt32);
197 if (write_mode) {
198 *pt32 = conv32(sbi->native, w_block);
199 bi2->dirty = false;
200 }
[155f792]201 }
[8c76c30]202 r = EOK;
[930baca]203
[152610a8]204 block_put(bi2);
205out_put_1:
206 block_put(bi1);
[930baca]207out:
208 return r;
209}
210
[6adba0a8]211/**
212 * @}
213 */
214
Note: See TracBrowser for help on using the repository browser.