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