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

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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