source: mainline/uspace/srv/fs/minixfs/mfs_read.c@ 147c9f6

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

Improves read_map() to avoid the need to call the read_ind_block() function.

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