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 |
|
---|
38 | static int
|
---|
39 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
---|
40 | bool write_mode, uint32_t w_block);
|
---|
41 |
|
---|
42 | /**Given the position in the file expressed in
|
---|
43 | *bytes, this function returns the on-disk block
|
---|
44 | *relative to that position.
|
---|
45 | *Returns zero if the block does not exist.
|
---|
46 | */
|
---|
47 | int 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 |
|
---|
62 | if (mnode->ino_i->i_size < (int32_t) pos) {
|
---|
63 | /*Trying to read beyond the end of file*/
|
---|
64 | r = EOK;
|
---|
65 | *b = 0;
|
---|
66 | goto out;
|
---|
67 | }
|
---|
68 |
|
---|
69 | r = rw_map_ondisk(b, mnode, rblock, false, 0);
|
---|
70 | out:
|
---|
71 | return r;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static int
|
---|
75 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
---|
76 | bool write_mode, uint32_t w_block)
|
---|
77 | {
|
---|
78 | int r, nr_direct;
|
---|
79 | int ptrs_per_block;
|
---|
80 | block_t *bi1;
|
---|
81 | block_t *bi2;
|
---|
82 |
|
---|
83 | assert(mnode);
|
---|
84 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
85 |
|
---|
86 | assert(ino_i);
|
---|
87 | assert(mnode->instance);
|
---|
88 |
|
---|
89 | const struct mfs_instance *inst = mnode->instance;
|
---|
90 | const struct mfs_sb_info *sbi = inst->sbi;
|
---|
91 | assert(sbi);
|
---|
92 |
|
---|
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 | }
|
---|
102 |
|
---|
103 | /*Check if the wanted block is in the direct zones*/
|
---|
104 | if (rblock < nr_direct) {
|
---|
105 | *b = ino_i->i_dzone[rblock];
|
---|
106 | if (write_mode) {
|
---|
107 | ino_i->i_dzone[rblock] = w_block;
|
---|
108 | ino_i->dirty = true;
|
---|
109 | }
|
---|
110 | r = EOK;
|
---|
111 | goto out;
|
---|
112 | }
|
---|
113 | rblock -= nr_direct - 1;
|
---|
114 |
|
---|
115 | if (rblock < ptrs_per_block) {
|
---|
116 | /*The wanted block is in the single indirect zone chain*/
|
---|
117 | if (ino_i->i_izone[0] == 0) {
|
---|
118 | r = -1;
|
---|
119 | goto out;
|
---|
120 | }
|
---|
121 |
|
---|
122 | r = block_get(&bi1, inst->handle, ino_i->i_izone[0],
|
---|
123 | BLOCK_FLAGS_NONE);
|
---|
124 | if (r != EOK)
|
---|
125 | goto out;
|
---|
126 |
|
---|
127 | if (fs_version == MFS_VERSION_V1) {
|
---|
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 | }
|
---|
134 | } else {
|
---|
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 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | goto out_put_1;
|
---|
144 | }
|
---|
145 |
|
---|
146 | rblock -= ptrs_per_block - 1;
|
---|
147 |
|
---|
148 | /*The wanted block is in the double indirect zone chain*/
|
---|
149 |
|
---|
150 | /*read the first indirect zone of the chain*/
|
---|
151 | if (ino_i->i_izone[1] == 0) {
|
---|
152 | r = -1;
|
---|
153 | goto out;
|
---|
154 | }
|
---|
155 |
|
---|
156 | r = block_get(&bi1, inst->handle, ino_i->i_izone[1],
|
---|
157 | BLOCK_FLAGS_NONE);
|
---|
158 |
|
---|
159 | if (r != EOK)
|
---|
160 | goto out;
|
---|
161 |
|
---|
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*/
|
---|
169 | if (fs_version == MFS_VERSION_V1) {
|
---|
170 | uint16_t *pt16 = bi1->data;
|
---|
171 | uint16_t blk = conv16(sbi->native, pt16[di_block]);
|
---|
172 |
|
---|
173 | r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
|
---|
174 |
|
---|
175 | if (r != EOK)
|
---|
176 | goto out_put_1;
|
---|
177 |
|
---|
178 | pt16 = bi2->data;
|
---|
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 | }
|
---|
185 | } else {
|
---|
186 | uint32_t *pt32 = bi1->data;
|
---|
187 | uint32_t blk = conv32(sbi->native, pt32[di_block]);
|
---|
188 |
|
---|
189 | r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
|
---|
190 |
|
---|
191 | if (r != EOK)
|
---|
192 | goto out_put_1;
|
---|
193 |
|
---|
194 | pt32 = bi2->data;
|
---|
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 | }
|
---|
201 | }
|
---|
202 | r = EOK;
|
---|
203 |
|
---|
204 | block_put(bi2);
|
---|
205 | out_put_1:
|
---|
206 | block_put(bi1);
|
---|
207 | out:
|
---|
208 | return r;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * @}
|
---|
213 | */
|
---|
214 |
|
---|