source: mainline/uspace/srv/fs/mfs/mfs_rw.c@ a347a11

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

Improve comments in mfs_rw.c

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[6adba0a8]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
[930baca]33#include "mfs.h"
34
[2d7c77a]35static int
36rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
[44c6091f]37 bool write_mode, uint32_t w_block);
[930baca]38
[5a29b4c]39static int
[8ab1adff]40reset_zone_content(struct mfs_instance *inst, uint32_t zone);
[5a29b4c]41
42static int
[8ab1adff]43alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone);
44
45static int
46read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone);
47
48static int
49write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone);
[5a29b4c]50
51
[2d7c77a]52/**Given the position in the file expressed in
[930baca]53 *bytes, this function returns the on-disk block
54 *relative to that position.
[980671e7]55 *
56 * @param b Pointer to a 32bit number where the block number will be stored
57 * @param mnode Pointer to a generic MINIX inode in memory.
58 * @param pos Position in file.
59 *
60 * @return EOK on success or a negative error code.
[930baca]61 */
[2bbbfd3]62int
[3a5ee6c]63mfs_read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
[930baca]64{
65 int r;
66 const struct mfs_sb_info *sbi = mnode->instance->sbi;
67 const int block_size = sbi->block_size;
68
69 /*Compute relative block number in file*/
70 int rblock = pos / block_size;
71
[8f6bb76e]72 if (mnode->ino_i->i_size < pos) {
[cfbcd86]73 /*Trying to read beyond the end of file*/
[930baca]74 r = EOK;
[155f792]75 *b = 0;
[930baca]76 goto out;
77 }
78
[2d7c77a]79 r = rw_map_ondisk(b, mnode, rblock, false, 0);
[930baca]80out:
81 return r;
82}
83
[2bbbfd3]84int
[3a5ee6c]85mfs_write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
[44c6091f]86 uint32_t *old_zone)
[2bbbfd3]87{
88 const struct mfs_sb_info *sbi = mnode->instance->sbi;
89
[9a0be34]90 if (pos >= sbi->max_file_size) {
91 /*Can't write beyond the maximum file size*/
92 return EINVAL;
93 }
94
[2bbbfd3]95 /*Compute the relative block number in file*/
[8a49fed]96 int rblock = pos / sbi->block_size;
[2bbbfd3]97
98 return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
99}
100
[2d7c77a]101static int
102rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
[44c6091f]103 bool write_mode, uint32_t w_block)
[930baca]104{
[8c76c30]105 int r, nr_direct;
[155f792]106 int ptrs_per_block;
[8ab1adff]107 uint32_t *ind_zone, *ind2_zone;
[930baca]108
[2d7c77a]109 struct mfs_ino_info *ino_i = mnode->ino_i;
[5a29b4c]110 struct mfs_instance *inst = mnode->instance;
111 struct mfs_sb_info *sbi = inst->sbi;
[930baca]112
[2bbbfd3]113 const mfs_version_t fs_version = sbi->fs_version;
[9a0be34]114 const bool deleting = write_mode && (w_block == 0);
[155f792]115
116 if (fs_version == MFS_VERSION_V1) {
117 nr_direct = V1_NR_DIRECT_ZONES;
118 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
119 } else {
120 nr_direct = V2_NR_DIRECT_ZONES;
121 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
122 }
[930baca]123
[8c76c30]124 /*Check if the wanted block is in the direct zones*/
[155f792]125 if (rblock < nr_direct) {
126 *b = ino_i->i_dzone[rblock];
[2d7c77a]127 if (write_mode) {
128 ino_i->i_dzone[rblock] = w_block;
129 ino_i->dirty = true;
130 }
[2bbbfd3]131 return EOK;
[930baca]132 }
[8f6bb76e]133
134 rblock -= nr_direct;
[930baca]135
136 if (rblock < ptrs_per_block) {
[8c76c30]137 /*The wanted block is in the single indirect zone chain*/
[155f792]138 if (ino_i->i_izone[0] == 0) {
[9a0be34]139 if (write_mode && !deleting) {
[8ab1adff]140 uint32_t zone;
141 r = alloc_zone_and_clear(inst, &zone);
[c699b0c]142 if (r != EOK)
143 return r;
[5a29b4c]144
[8ab1adff]145 ino_i->i_izone[0] = zone;
[5a29b4c]146 ino_i->dirty = true;
[8f6bb76e]147 } else {
[b2a18234]148 /*Sparse block*/
149 *b = 0;
150 return EOK;
[8f6bb76e]151 }
[930baca]152 }
153
[8ab1adff]154 r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
[c699b0c]155 if (r != EOK)
156 return r;
[930baca]157
[8ab1adff]158 *b = ind_zone[rblock];
159 if (write_mode) {
160 ind_zone[rblock] = w_block;
161 write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
[152610a8]162 }
[930baca]163
[8ab1adff]164 goto out_free_ind1;
[930baca]165 }
166
[8f6bb76e]167 rblock -= ptrs_per_block;
[930baca]168
[8c76c30]169 /*The wanted block is in the double indirect zone chain*/
[930baca]170
[8c76c30]171 /*read the first indirect zone of the chain*/
[155f792]172 if (ino_i->i_izone[1] == 0) {
[9a0be34]173 if (write_mode && !deleting) {
[8ab1adff]174 uint32_t zone;
175 r = alloc_zone_and_clear(inst, &zone);
[c699b0c]176 if (r != EOK)
177 return r;
[5a29b4c]178
[8ab1adff]179 ino_i->i_izone[1] = zone;
[5a29b4c]180 ino_i->dirty = true;
[b2a18234]181 } else {
182 /*Sparse block*/
183 *b = 0;
184 return EOK;
185 }
[930baca]186 }
[fde8a276]187
[8ab1adff]188 r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
[c699b0c]189 if (r != EOK)
190 return r;
[930baca]191
[8c76c30]192 /*
193 *Compute the position of the second indirect
194 *zone pointer in the chain.
195 */
[8ab1adff]196 uint32_t ind2_off = rblock / ptrs_per_block;
[8c76c30]197
198 /*read the second indirect zone of the chain*/
[8ab1adff]199 if (ind_zone[ind2_off] == 0) {
[9a0be34]200 if (write_mode && !deleting) {
[8ab1adff]201 uint32_t zone;
202 r = alloc_zone_and_clear(inst, &zone);
[c699b0c]203 if (r != EOK)
204 goto out_free_ind1;
[1878386]205
[8ab1adff]206 ind_zone[ind2_off] = zone;
207 write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
208 } else {
[b2a18234]209 /*Sparse block*/
210 r = EOK;
211 *b = 0;
[8ab1adff]212 goto out_free_ind1;
[5a29b4c]213 }
[8ab1adff]214 }
[930baca]215
[8ab1adff]216 r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
[c699b0c]217 if (r != EOK)
218 goto out_free_ind1;
[930baca]219
[5987f917]220 *b = ind2_zone[rblock - (ind2_off * ptrs_per_block)];
[8ab1adff]221 if (write_mode) {
[5987f917]222 ind2_zone[rblock - (ind2_off * ptrs_per_block)] = w_block;
[8ab1adff]223 write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
[155f792]224 }
[8ab1adff]225
[8c76c30]226 r = EOK;
[930baca]227
[8ab1adff]228 free(ind2_zone);
229out_free_ind1:
230 free(ind_zone);
[930baca]231 return r;
232}
233
[980671e7]234/**Free unused indirect zones from a MINIX inode according to it's new size.
235 *
236 * @param mnode Pointer to a generic MINIX inode in memory.
237 * @param new_size The new size of the inode.
238 *
239 * @return EOK on success or a negative error code.
240 */
[1878386]241int
[3a5ee6c]242mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size)
[1878386]243{
244 struct mfs_instance *inst = mnode->instance;
245 struct mfs_sb_info *sbi = inst->sbi;
246 struct mfs_ino_info *ino_i = mnode->ino_i;
247 int nr_direct, ptrs_per_block, rblock, r;
248 int i;
249
250 mfs_version_t fs_version = sbi->fs_version;
251
[980671e7]252 assert(new_size <= ino_i->i_size);
253
[1878386]254 if (fs_version == MFS_VERSION_V1) {
255 nr_direct = V1_NR_DIRECT_ZONES;
256 ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
257 } else {
258 nr_direct = V2_NR_DIRECT_ZONES;
259 ptrs_per_block = sbi->block_size / sizeof(uint32_t);
260 }
261
262 rblock = new_size / sbi->block_size;
263
264 if (rblock < nr_direct) {
265 /*free the single indirect zone*/
266 if (ino_i->i_izone[0]) {
[70ac0af]267 r = mfs_free_zone(inst, ino_i->i_izone[0]);
[c699b0c]268 if (r != EOK)
269 return r;
[1878386]270
271 ino_i->i_izone[0] = 0;
272 ino_i->dirty = true;
273 }
274 }
275
276 rblock -= nr_direct + ptrs_per_block;
277
278 int fzone_to_free = (rblock < 0 ? 0 : rblock) / ptrs_per_block;
279
[01accb7]280 if ((fzone_to_free % ptrs_per_block) != 0)
281 ++fzone_to_free;
282
[1878386]283 /*free the entire double indirect zone*/
284 uint32_t *dbl_zone;
285
[01accb7]286 if (ino_i->i_izone[1] == 0) {
287 /*Nothing to be done*/
288 return EOK;
289 }
290
[1878386]291 r = read_ind_zone(inst, ino_i->i_izone[1], &dbl_zone);
[c699b0c]292 if (r != EOK)
293 return r;
[1878386]294
295 for (i = fzone_to_free; i < ptrs_per_block; ++i) {
296 if (dbl_zone[i] == 0)
297 continue;
298
[70ac0af]299 r = mfs_free_zone(inst, dbl_zone[i]);
[c699b0c]300 if (r != EOK)
301 goto out;
[1878386]302 }
303
[01accb7]304 if (fzone_to_free == 0) {
[70ac0af]305 r = mfs_free_zone(inst, ino_i->i_izone[1]);
[1878386]306 ino_i->i_izone[1] = 0;
307 ino_i->dirty = true;
308 }
[dd9af5d]309out:
[1878386]310 free(dbl_zone);
311 return r;
312}
[5a29b4c]313
314static int
[8ab1adff]315reset_zone_content(struct mfs_instance *inst, uint32_t zone)
[5a29b4c]316{
317 block_t *b;
318 int r;
319
[03bc76a]320 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
[c699b0c]321 if (r != EOK)
322 return r;
[5a29b4c]323
324 memset(b->data, 0, b->size);
325 b->dirty = true;
326
[5f6168d]327 return block_put(b);
[5a29b4c]328}
329
330static int
[8ab1adff]331alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
[5a29b4c]332{
333 int r;
334
[70ac0af]335 r = mfs_alloc_zone(inst, zone);
[c699b0c]336 if (r != EOK)
337 return r;
[5a29b4c]338
[8ab1adff]339 r = reset_zone_content(inst, *zone);
[5a29b4c]340 return r;
341}
342
[8ab1adff]343static int
344read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
345{
346 struct mfs_sb_info *sbi = inst->sbi;
347 int r;
348 unsigned i;
349 block_t *b;
[8f6bb76e]350 const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
[44c6091f]351 sizeof(uint32_t);
[8ab1adff]352
[8f6bb76e]353 *ind_zone = malloc(max_ind_zone_ptrs);
[8ab1adff]354 if (*ind_zone == NULL)
355 return ENOMEM;
356
[03bc76a]357 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NONE);
[8ab1adff]358 if (r != EOK) {
359 free(*ind_zone);
360 return r;
361 }
362
363 if (sbi->fs_version == MFS_VERSION_V1) {
364 uint16_t *src_ptr = b->data;
365
366 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
367 (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
368 } else {
369 uint32_t *src_ptr = b->data;
370
371 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
372 (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
373 }
374
[5f6168d]375 return block_put(b);
[8ab1adff]376}
377
378static int
379write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
380{
381 struct mfs_sb_info *sbi = inst->sbi;
382 int r;
383 unsigned i;
384 block_t *b;
385
[5e0427e8]386 r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
[c699b0c]387 if (r != EOK)
388 return r;
[8ab1adff]389
390 if (sbi->fs_version == MFS_VERSION_V1) {
391 uint16_t *dest_ptr = b->data;
392
393 for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
394 dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
395 } else {
396 uint32_t *dest_ptr = b->data;
397
398 for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
399 dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
400
401 }
402 b->dirty = true;
[5f6168d]403
404 return block_put(b);
[8ab1adff]405}
406
407
[6adba0a8]408/**
409 * @}
[44c6091f]410 */
[6adba0a8]411
Note: See TracBrowser for help on using the repository browser.