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 | static int
|
---|
43 | reset_zone_content(struct mfs_instance *inst, uint32_t zone);
|
---|
44 |
|
---|
45 | static int
|
---|
46 | alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone);
|
---|
47 |
|
---|
48 | static int
|
---|
49 | read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone);
|
---|
50 |
|
---|
51 | static int
|
---|
52 | write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone);
|
---|
53 |
|
---|
54 |
|
---|
55 | /**Given the position in the file expressed in
|
---|
56 | *bytes, this function returns the on-disk block
|
---|
57 | *relative to that position.
|
---|
58 | *Returns zero if the block does not exist.
|
---|
59 | */
|
---|
60 | int
|
---|
61 | read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
|
---|
62 | {
|
---|
63 | int r;
|
---|
64 | const struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
65 | const int block_size = sbi->block_size;
|
---|
66 |
|
---|
67 | /*Compute relative block number in file*/
|
---|
68 | int rblock = pos / block_size;
|
---|
69 |
|
---|
70 | if (mnode->ino_i->i_size < pos) {
|
---|
71 | /*Trying to read beyond the end of file*/
|
---|
72 | r = EOK;
|
---|
73 | *b = 0;
|
---|
74 | goto out;
|
---|
75 | }
|
---|
76 |
|
---|
77 | r = rw_map_ondisk(b, mnode, rblock, false, 0);
|
---|
78 | out:
|
---|
79 | return r;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int
|
---|
83 | write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
|
---|
84 | uint32_t *old_zone)
|
---|
85 | {
|
---|
86 | const struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
87 | const int block_size = sbi->block_size;
|
---|
88 |
|
---|
89 | /*Compute the relative block number in file*/
|
---|
90 | int rblock = pos / block_size;
|
---|
91 |
|
---|
92 | return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
|
---|
93 | }
|
---|
94 |
|
---|
95 | int
|
---|
96 | free_zone(struct mfs_node *mnode, const uint32_t zone)
|
---|
97 | {
|
---|
98 | int r;
|
---|
99 | uint32_t old_zone;
|
---|
100 |
|
---|
101 | r = rw_map_ondisk(&old_zone, mnode, zone, true, 0);
|
---|
102 | if (r != EOK)
|
---|
103 | return r;
|
---|
104 |
|
---|
105 | if (old_zone > 0)
|
---|
106 | r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
|
---|
107 |
|
---|
108 | return r;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static int
|
---|
112 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
---|
113 | bool write_mode, uint32_t w_block)
|
---|
114 | {
|
---|
115 | int r, nr_direct;
|
---|
116 | int ptrs_per_block;
|
---|
117 | uint32_t *ind_zone, *ind2_zone;
|
---|
118 |
|
---|
119 | assert(mnode);
|
---|
120 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
121 |
|
---|
122 | assert(ino_i);
|
---|
123 | assert(mnode->instance);
|
---|
124 |
|
---|
125 | struct mfs_instance *inst = mnode->instance;
|
---|
126 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
127 | assert(sbi);
|
---|
128 |
|
---|
129 | const mfs_version_t fs_version = sbi->fs_version;
|
---|
130 |
|
---|
131 | if (fs_version == MFS_VERSION_V1) {
|
---|
132 | nr_direct = V1_NR_DIRECT_ZONES;
|
---|
133 | ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
|
---|
134 | } else {
|
---|
135 | nr_direct = V2_NR_DIRECT_ZONES;
|
---|
136 | ptrs_per_block = sbi->block_size / sizeof(uint32_t);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*Check if the wanted block is in the direct zones*/
|
---|
140 | if (rblock < nr_direct) {
|
---|
141 | *b = ino_i->i_dzone[rblock];
|
---|
142 | if (write_mode) {
|
---|
143 | ino_i->i_dzone[rblock] = w_block;
|
---|
144 | ino_i->dirty = true;
|
---|
145 | }
|
---|
146 | return EOK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | rblock -= nr_direct;
|
---|
150 |
|
---|
151 | if (rblock < ptrs_per_block) {
|
---|
152 | /*The wanted block is in the single indirect zone chain*/
|
---|
153 | if (ino_i->i_izone[0] == 0) {
|
---|
154 | if (write_mode) {
|
---|
155 | uint32_t zone;
|
---|
156 | r = alloc_zone_and_clear(inst, &zone);
|
---|
157 | if (r != EOK)
|
---|
158 | return r;
|
---|
159 |
|
---|
160 | ino_i->i_izone[0] = zone;
|
---|
161 | ino_i->dirty = true;
|
---|
162 | } else {
|
---|
163 | return -1;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
|
---|
168 | if (r != EOK)
|
---|
169 | return r;
|
---|
170 |
|
---|
171 | *b = ind_zone[rblock];
|
---|
172 | if (write_mode) {
|
---|
173 | ind_zone[rblock] = w_block;
|
---|
174 | write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
|
---|
175 | }
|
---|
176 |
|
---|
177 | goto out_free_ind1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | rblock -= ptrs_per_block;
|
---|
181 |
|
---|
182 | /*The wanted block is in the double indirect zone chain*/
|
---|
183 |
|
---|
184 | /*read the first indirect zone of the chain*/
|
---|
185 | if (ino_i->i_izone[1] == 0) {
|
---|
186 | if (write_mode) {
|
---|
187 | uint32_t zone;
|
---|
188 | r = alloc_zone_and_clear(inst, &zone);
|
---|
189 | if (r != EOK)
|
---|
190 | return r;
|
---|
191 |
|
---|
192 | ino_i->i_izone[1] = zone;
|
---|
193 | ino_i->dirty = true;
|
---|
194 | } else
|
---|
195 | return -1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
|
---|
199 | if (r != EOK)
|
---|
200 | return r;
|
---|
201 |
|
---|
202 | /*
|
---|
203 | *Compute the position of the second indirect
|
---|
204 | *zone pointer in the chain.
|
---|
205 | */
|
---|
206 | uint32_t ind2_off = rblock / ptrs_per_block;
|
---|
207 |
|
---|
208 | /*read the second indirect zone of the chain*/
|
---|
209 | if (ind_zone[ind2_off] == 0) {
|
---|
210 | if (write_mode) {
|
---|
211 | uint32_t zone;
|
---|
212 | r = alloc_zone_and_clear(inst, &zone);
|
---|
213 | if(r != EOK)
|
---|
214 | goto out_free_ind1;
|
---|
215 | ind_zone[ind2_off] = zone;
|
---|
216 | write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
|
---|
217 | } else {
|
---|
218 | r = -1;
|
---|
219 | goto out_free_ind1;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
|
---|
224 | if (r != EOK)
|
---|
225 | goto out_free_ind1;
|
---|
226 |
|
---|
227 | *b = ind2_zone[ind2_off % ptrs_per_block];
|
---|
228 | if (write_mode) {
|
---|
229 | ind2_zone[ind2_off % ptrs_per_block] = w_block;
|
---|
230 | write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
|
---|
231 | }
|
---|
232 |
|
---|
233 | r = EOK;
|
---|
234 |
|
---|
235 | free(ind2_zone);
|
---|
236 | out_free_ind1:
|
---|
237 | free(ind_zone);
|
---|
238 | return r;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | static int
|
---|
243 | reset_zone_content(struct mfs_instance *inst, uint32_t zone)
|
---|
244 | {
|
---|
245 | block_t *b;
|
---|
246 | int r;
|
---|
247 |
|
---|
248 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
|
---|
249 | if (r != EOK)
|
---|
250 | return r;
|
---|
251 |
|
---|
252 | memset(b->data, 0, b->size);
|
---|
253 | b->dirty = true;
|
---|
254 | block_put(b);
|
---|
255 |
|
---|
256 | return EOK;
|
---|
257 | }
|
---|
258 |
|
---|
259 | static int
|
---|
260 | alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
|
---|
261 | {
|
---|
262 | int r;
|
---|
263 |
|
---|
264 | r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
|
---|
265 | if (r != EOK)
|
---|
266 | goto out;
|
---|
267 |
|
---|
268 | r = reset_zone_content(inst, *zone);
|
---|
269 | out:
|
---|
270 | return r;
|
---|
271 | }
|
---|
272 |
|
---|
273 | static int
|
---|
274 | read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
|
---|
275 | {
|
---|
276 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
277 | int r;
|
---|
278 | unsigned i;
|
---|
279 | block_t *b;
|
---|
280 | const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
|
---|
281 | sizeof(uint32_t);
|
---|
282 |
|
---|
283 | *ind_zone = malloc(max_ind_zone_ptrs);
|
---|
284 | if (*ind_zone == NULL)
|
---|
285 | return ENOMEM;
|
---|
286 |
|
---|
287 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NONE);
|
---|
288 | if (r != EOK) {
|
---|
289 | free(*ind_zone);
|
---|
290 | return r;
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (sbi->fs_version == MFS_VERSION_V1) {
|
---|
294 | uint16_t *src_ptr = b->data;
|
---|
295 |
|
---|
296 | for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
|
---|
297 | (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
|
---|
298 | } else {
|
---|
299 | uint32_t *src_ptr = b->data;
|
---|
300 |
|
---|
301 | for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
|
---|
302 | (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
|
---|
303 | }
|
---|
304 |
|
---|
305 | block_put(b);
|
---|
306 |
|
---|
307 | return EOK;
|
---|
308 | }
|
---|
309 |
|
---|
310 | static int
|
---|
311 | write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
|
---|
312 | {
|
---|
313 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
314 | int r;
|
---|
315 | unsigned i;
|
---|
316 | block_t *b;
|
---|
317 |
|
---|
318 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NONE);
|
---|
319 | if (r != EOK)
|
---|
320 | return r;
|
---|
321 |
|
---|
322 | if (sbi->fs_version == MFS_VERSION_V1) {
|
---|
323 | uint16_t *dest_ptr = b->data;
|
---|
324 |
|
---|
325 | for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
|
---|
326 | dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
|
---|
327 | } else {
|
---|
328 | uint32_t *dest_ptr = b->data;
|
---|
329 |
|
---|
330 | for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
|
---|
331 | dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
|
---|
332 |
|
---|
333 | }
|
---|
334 | b->dirty = true;
|
---|
335 | block_put(b);
|
---|
336 | return EOK;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * @}
|
---|
342 | */
|
---|
343 |
|
---|