source: mainline/uspace/srv/fs/minixfs/mfs_read.c@ 6adba0a8

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

Add copyright headers

  • Property mode set to 100644
File size: 4.2 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, nr_indirect;
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 nr_indirect = V1_NR_INDIRECT_ZONES;
91 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
92 } else {
93 nr_direct = V2_NR_DIRECT_ZONES;
94 nr_indirect = V2_NR_INDIRECT_ZONES;
95 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
96 }
97
98 if (rblock < nr_direct) {
99 *b = ino_i->i_dzone[rblock];
100 r = EOK;
101 goto out;
102 }
103 rblock -= nr_direct - 1;
104
105 /*Check if the wanted block is in the single indirect zone*/
106 if (rblock < ptrs_per_block) {
107 if (ino_i->i_izone[0] == 0) {
108 r = -1;
109 goto out;
110 }
111
112 r = read_ind_block(bi2, mnode->instance, ino_i->i_izone[0]);
113
114 if (r != EOK)
115 goto out;
116
117 *b = fs_version == MFS_VERSION_V1 ?
118 ((uint16_t *) bi1->data)[rblock] :
119 ((uint32_t *) bi1->data)[rblock];
120 goto out_block;
121 }
122
123 rblock -= ptrs_per_block - 1;
124
125 /*The wanted block is in the double indirect zone*/
126 uint32_t di_block = rblock / ptrs_per_block;
127
128 /*read the first indirect zone*/
129 if (ino_i->i_izone[1] == 0) {
130 r = -1;
131 goto out;
132 }
133
134 r = read_ind_block(bi1, mnode->instance, ino_i->i_izone[1]);
135
136 if (r != EOK)
137 goto out;
138
139 /*read the second indirect zone*/
140 if (fs_version == MFS_VERSION_V1) {
141 r = read_ind_block(bi2, mnode->instance,
142 ((uint16_t *) bi1->data)[di_block]);
143
144 if (r != EOK)
145 goto out_block;
146
147 *b = ((uint16_t *) bi2->data)[rblock % ptrs_per_block];
148 } else {
149 r = read_ind_block(bi2, mnode->instance,
150 ((uint32_t *) bi1->data)[di_block]);
151
152 if (r != EOK)
153 goto out_block;
154
155 *b = ((uint32_t *) bi2->data)[rblock % ptrs_per_block];
156 }
157 block_put(bi2);
158
159out_block:
160 block_put(bi1);
161out:
162 return r;
163}
164
165/**
166 * @}
167 */
168
Note: See TracBrowser for help on using the repository browser.