source: mainline/uspace/srv/fs/minixfs/mfs_read.c@ 8c76c30

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

Fix some comments and remove unused nr_indirect var.

  • Property mode set to 100644
File size: 4.3 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 r = EOK;
62 *b = 0;
63 goto out;
64 }
65
66 r = read_map_ondisk(b, mnode, rblock);
67out:
68 return r;
69}
70
71static int read_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock)
72{
73 block_t *bi1, *bi2;
74 int r, nr_direct;
75 int ptrs_per_block;
76
77 assert(mnode);
78 const struct mfs_ino_info *ino_i = mnode->ino_i;
79
80 assert(ino_i);
81 assert(mnode->instance);
82
83 const struct mfs_sb_info *sbi = mnode->instance->sbi;
84 assert(sbi);
85
86 const int fs_version = sbi->fs_version;
87
88 if (fs_version == MFS_VERSION_V1) {
89 nr_direct = V1_NR_DIRECT_ZONES;
90 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
91 } else {
92 nr_direct = V2_NR_DIRECT_ZONES;
93 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
94 }
95
96 /*Check if the wanted block is in the direct zones*/
97 if (rblock < nr_direct) {
98 *b = ino_i->i_dzone[rblock];
99 r = EOK;
100 goto out;
101 }
102 rblock -= nr_direct - 1;
103
104 if (rblock < ptrs_per_block) {
105 /*The wanted block is in the single indirect zone chain*/
106 if (ino_i->i_izone[0] == 0) {
107 r = -1;
108 goto out;
109 }
110
111 r = read_ind_block(bi2, mnode->instance, ino_i->i_izone[0]);
112
113 if (r != EOK)
114 goto out;
115
116 *b = fs_version == MFS_VERSION_V1 ?
117 ((uint16_t *) bi1->data)[rblock] :
118 ((uint32_t *) bi1->data)[rblock];
119 goto out_block;
120 }
121
122 rblock -= ptrs_per_block - 1;
123
124 /*The wanted block is in the double indirect zone chain*/
125
126 /*read the first indirect zone of the chain*/
127 if (ino_i->i_izone[1] == 0) {
128 r = -1;
129 goto out;
130 }
131 r = read_ind_block(bi1, mnode->instance, ino_i->i_izone[1]);
132
133 if (r != EOK)
134 goto out;
135
136 /*
137 *Compute the position of the second indirect
138 *zone pointer in the chain.
139 */
140 uint32_t di_block = rblock / ptrs_per_block;
141
142 /*read the second indirect zone of the chain*/
143 if (fs_version == MFS_VERSION_V1) {
144 r = read_ind_block(bi2, mnode->instance,
145 ((uint16_t *) bi1->data)[di_block]);
146
147 if (r != EOK)
148 goto out_block;
149
150 *b = ((uint16_t *) bi2->data)[rblock % ptrs_per_block];
151 } else {
152 r = read_ind_block(bi2, mnode->instance,
153 ((uint32_t *) bi1->data)[di_block]);
154
155 if (r != EOK)
156 goto out_block;
157
158 *b = ((uint32_t *) bi2->data)[rblock % ptrs_per_block];
159 }
160 r = EOK;
161 block_put(bi2);
162
163out_block:
164 block_put(bi1);
165out:
166 return r;
167}
168
169/**
170 * @}
171 */
172
Note: See TracBrowser for help on using the repository browser.