source: mainline/uspace/srv/fs/minixfs/mfs_rw.c@ 2bbbfd3

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

Add functions to add or remove zones from inodes

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2g
3 * Copyright (c) 2011 Maurizio Lombardi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup fs
31 * @{
32 */
33
34#include <assert.h>
35#include <errno.h>
36#include "mfs.h"
37#include "mfs_utils.h"
38
39static int
40rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
41 bool write_mode, uint32_t w_block);
42
43static int
44reset_block_content(struct mfs_instance *inst, uint32_t block);
45
46static int
47alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *block);
48
49
50/**Given the position in the file expressed in
51 *bytes, this function returns the on-disk block
52 *relative to that position.
53 *Returns zero if the block does not exist.
54 */
55int
56read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
57{
58 int r;
59 const struct mfs_sb_info *sbi = mnode->instance->sbi;
60 const int block_size = sbi->block_size;
61
62 /*Compute relative block number in file*/
63 int rblock = pos / block_size;
64
65 if (mnode->ino_i->i_size < (int32_t) pos) {
66 /*Trying to read beyond the end of file*/
67 r = EOK;
68 *b = 0;
69 goto out;
70 }
71
72 r = rw_map_ondisk(b, mnode, rblock, false, 0);
73out:
74 return r;
75}
76
77int
78write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
79 uint32_t *old_zone)
80{
81 const struct mfs_sb_info *sbi = mnode->instance->sbi;
82 const int block_size = sbi->block_size;
83
84 /*Compute the relative block number in file*/
85 int rblock = pos / block_size;
86
87 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
88}
89
90int
91free_zone(struct mfs_node *mnode, const uint32_t zone)
92{
93 int r;
94 uint32_t old_zone;
95
96 r = rw_map_ondisk(&old_zone, mnode, zone, true, 0);
97 if (r != EOK)
98 return r;
99
100 if (old_zone > 0)
101 mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
102
103 return EOK;
104}
105
106static int
107rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
108 bool write_mode, uint32_t w_block)
109{
110 int r, nr_direct;
111 int ptrs_per_block;
112 block_t *bi1;
113 block_t *bi2;
114
115 assert(mnode);
116 struct mfs_ino_info *ino_i = mnode->ino_i;
117
118 assert(ino_i);
119 assert(mnode->instance);
120
121 struct mfs_instance *inst = mnode->instance;
122 struct mfs_sb_info *sbi = inst->sbi;
123 assert(sbi);
124
125 const mfs_version_t fs_version = sbi->fs_version;
126
127 if (fs_version == MFS_VERSION_V1) {
128 nr_direct = V1_NR_DIRECT_ZONES;
129 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
130 } else {
131 nr_direct = V2_NR_DIRECT_ZONES;
132 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
133 }
134
135 /*Check if the wanted block is in the direct zones*/
136 if (rblock < nr_direct) {
137 *b = ino_i->i_dzone[rblock];
138 if (write_mode) {
139 ino_i->i_dzone[rblock] = w_block;
140 ino_i->dirty = true;
141 }
142 return EOK;
143 }
144 rblock -= nr_direct - 1;
145
146 if (rblock < ptrs_per_block) {
147 /*The wanted block is in the single indirect zone chain*/
148 if (ino_i->i_izone[0] == 0) {
149 if (write_mode) {
150 uint32_t block;
151 r = alloc_zone_and_clear(inst, &block);
152 if (r != EOK)
153 return r;
154
155 ino_i->i_izone[0] = block;
156 ino_i->dirty = true;
157 } else
158 return -1;
159 }
160
161 r = block_get(&bi1, inst->handle, ino_i->i_izone[0],
162 BLOCK_FLAGS_NONE);
163 if (r != EOK)
164 return r;
165
166 if (fs_version == MFS_VERSION_V1) {
167 uint16_t *tmp = &(((uint16_t *) bi1->data)[rblock]);
168 *b = conv16(sbi->native, *tmp);
169 if (write_mode) {
170 *tmp = conv16(sbi->native, w_block);
171 bi1->dirty = true;
172 }
173 } else {
174 uint32_t *tmp = &(((uint32_t *) bi1->data)[rblock]);
175 *b = conv32(sbi->native, *tmp);
176 if (write_mode) {
177 *tmp = conv32(sbi->native, w_block);
178 bi1->dirty = true;
179 }
180 }
181
182 goto out_put_1;
183 }
184
185 rblock -= ptrs_per_block - 1;
186
187 /*The wanted block is in the double indirect zone chain*/
188
189 /*read the first indirect zone of the chain*/
190 if (ino_i->i_izone[1] == 0) {
191 if (write_mode) {
192 uint32_t block;
193 r = alloc_zone_and_clear(inst, &block);
194 if (r != EOK)
195 return r;
196
197 ino_i->i_izone[1] = block;
198 ino_i->dirty = true;
199 } else
200 return -1;
201 }
202
203 r = block_get(&bi1, inst->handle, ino_i->i_izone[1],
204 BLOCK_FLAGS_NONE);
205 if (r != EOK)
206 return r;
207
208 /*
209 *Compute the position of the second indirect
210 *zone pointer in the chain.
211 */
212 uint32_t ind2_block = rblock / ptrs_per_block;
213
214 /*read the second indirect zone of the chain*/
215 if (fs_version == MFS_VERSION_V1) {
216 uint16_t *pt16 = bi1->data;
217 uint16_t blk = conv16(sbi->native, pt16[ind2_block]);
218
219 if (blk == 0) {
220 if (write_mode) {
221 uint32_t block;
222 r = alloc_zone_and_clear(inst, &block);
223 if (r != EOK)
224 return r;
225
226 blk = block;
227 pt16[ind2_block] = conv16(sbi->native, blk);
228 bi1->dirty = true;
229 } else
230 return -1;
231 }
232
233 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
234
235 if (r != EOK)
236 goto out_put_1;
237
238 pt16 = bi2->data;
239 pt16 += ind2_block % ptrs_per_block;
240 *b = conv16(sbi->native, *pt16);
241 if (write_mode) {
242 *pt16 = conv16(sbi->native, w_block);
243 bi2->dirty = true;
244 }
245 } else {
246 uint32_t *pt32 = bi1->data;
247 uint32_t blk = conv32(sbi->native, pt32[ind2_block]);
248
249 if (blk == 0) {
250 if (write_mode) {
251 uint32_t block;
252 r = alloc_zone_and_clear(inst, &block);
253 if (r != EOK)
254 return r;
255
256 blk = block;
257 pt32[ind2_block] = conv32(sbi->native, blk);
258 bi1->dirty = true;
259 } else
260 return -1;
261 }
262
263 r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
264
265 if (r != EOK)
266 goto out_put_1;
267
268 pt32 = bi2->data;
269 pt32 += ind2_block % ptrs_per_block;
270 *b = conv32(sbi->native, *pt32);
271 if (write_mode) {
272 *pt32 = conv32(sbi->native, w_block);
273 bi2->dirty = true;
274 }
275 }
276 r = EOK;
277
278 block_put(bi2);
279out_put_1:
280 block_put(bi1);
281 return r;
282}
283
284
285static int
286reset_block_content(struct mfs_instance *inst, uint32_t block)
287{
288 block_t *b;
289 int r;
290
291 r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NOREAD);
292 if (r != EOK)
293 return r;
294
295 memset(b->data, 0, b->size);
296 b->dirty = true;
297 block_put(b);
298
299 return EOK;
300}
301
302static int
303alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *block)
304{
305 int r;
306
307 r = mfs_alloc_bit(inst, block, BMAP_ZONE);
308 if (r != EOK)
309 goto out;
310
311 r = reset_block_content(inst, *block);
312out:
313 return r;
314}
315
316/**
317 * @}
318 */
319
Note: See TracBrowser for help on using the repository browser.