[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 <assert.h>
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include "mfs.h"
|
---|
| 36 | #include "mfs_utils.h"
|
---|
| 37 |
|
---|
[2d7c77a] | 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);
|
---|
[930baca] | 41 |
|
---|
[5a29b4c] | 42 | static int
|
---|
[8ab1adff] | 43 | reset_zone_content(struct mfs_instance *inst, uint32_t zone);
|
---|
[5a29b4c] | 44 |
|
---|
| 45 | static int
|
---|
[8ab1adff] | 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);
|
---|
[5a29b4c] | 53 |
|
---|
| 54 |
|
---|
[2d7c77a] | 55 | /**Given the position in the file expressed in
|
---|
[930baca] | 56 | *bytes, this function returns the on-disk block
|
---|
| 57 | *relative to that position.
|
---|
| 58 | *Returns zero if the block does not exist.
|
---|
| 59 | */
|
---|
[2bbbfd3] | 60 | int
|
---|
| 61 | read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
|
---|
[930baca] | 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 |
|
---|
[8f6bb76e] | 70 | if (mnode->ino_i->i_size < pos) {
|
---|
[cfbcd86] | 71 | /*Trying to read beyond the end of file*/
|
---|
[930baca] | 72 | r = EOK;
|
---|
[155f792] | 73 | *b = 0;
|
---|
[930baca] | 74 | goto out;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[2d7c77a] | 77 | r = rw_map_ondisk(b, mnode, rblock, false, 0);
|
---|
[930baca] | 78 | out:
|
---|
| 79 | return r;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2bbbfd3] | 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)
|
---|
[40f7297] | 106 | r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
|
---|
[2bbbfd3] | 107 |
|
---|
[40f7297] | 108 | return r;
|
---|
[2bbbfd3] | 109 | }
|
---|
| 110 |
|
---|
[2d7c77a] | 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)
|
---|
[930baca] | 114 | {
|
---|
[8c76c30] | 115 | int r, nr_direct;
|
---|
[155f792] | 116 | int ptrs_per_block;
|
---|
[8ab1adff] | 117 | uint32_t *ind_zone, *ind2_zone;
|
---|
[930baca] | 118 |
|
---|
| 119 | assert(mnode);
|
---|
[2d7c77a] | 120 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
[930baca] | 121 |
|
---|
[155f792] | 122 | assert(ino_i);
|
---|
[930baca] | 123 | assert(mnode->instance);
|
---|
| 124 |
|
---|
[5a29b4c] | 125 | struct mfs_instance *inst = mnode->instance;
|
---|
| 126 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
[930baca] | 127 | assert(sbi);
|
---|
| 128 |
|
---|
[2bbbfd3] | 129 | const mfs_version_t fs_version = sbi->fs_version;
|
---|
[155f792] | 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 | }
|
---|
[930baca] | 138 |
|
---|
[8c76c30] | 139 | /*Check if the wanted block is in the direct zones*/
|
---|
[155f792] | 140 | if (rblock < nr_direct) {
|
---|
| 141 | *b = ino_i->i_dzone[rblock];
|
---|
[2d7c77a] | 142 | if (write_mode) {
|
---|
| 143 | ino_i->i_dzone[rblock] = w_block;
|
---|
| 144 | ino_i->dirty = true;
|
---|
| 145 | }
|
---|
[2bbbfd3] | 146 | return EOK;
|
---|
[930baca] | 147 | }
|
---|
[8f6bb76e] | 148 |
|
---|
| 149 | rblock -= nr_direct;
|
---|
[930baca] | 150 |
|
---|
| 151 | if (rblock < ptrs_per_block) {
|
---|
[8c76c30] | 152 | /*The wanted block is in the single indirect zone chain*/
|
---|
[155f792] | 153 | if (ino_i->i_izone[0] == 0) {
|
---|
[5a29b4c] | 154 | if (write_mode) {
|
---|
[8ab1adff] | 155 | uint32_t zone;
|
---|
| 156 | r = alloc_zone_and_clear(inst, &zone);
|
---|
[5a29b4c] | 157 | if (r != EOK)
|
---|
[2bbbfd3] | 158 | return r;
|
---|
[5a29b4c] | 159 |
|
---|
[8ab1adff] | 160 | ino_i->i_izone[0] = zone;
|
---|
[5a29b4c] | 161 | ino_i->dirty = true;
|
---|
[8f6bb76e] | 162 | } else {
|
---|
[2bbbfd3] | 163 | return -1;
|
---|
[8f6bb76e] | 164 | }
|
---|
[930baca] | 165 | }
|
---|
| 166 |
|
---|
[8ab1adff] | 167 | r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
|
---|
[152610a8] | 168 | if (r != EOK)
|
---|
[2bbbfd3] | 169 | return r;
|
---|
[930baca] | 170 |
|
---|
[8ab1adff] | 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);
|
---|
[152610a8] | 175 | }
|
---|
[930baca] | 176 |
|
---|
[8ab1adff] | 177 | goto out_free_ind1;
|
---|
[930baca] | 178 | }
|
---|
| 179 |
|
---|
[8f6bb76e] | 180 | rblock -= ptrs_per_block;
|
---|
[930baca] | 181 |
|
---|
[8c76c30] | 182 | /*The wanted block is in the double indirect zone chain*/
|
---|
[930baca] | 183 |
|
---|
[8c76c30] | 184 | /*read the first indirect zone of the chain*/
|
---|
[155f792] | 185 | if (ino_i->i_izone[1] == 0) {
|
---|
[5a29b4c] | 186 | if (write_mode) {
|
---|
[8ab1adff] | 187 | uint32_t zone;
|
---|
| 188 | r = alloc_zone_and_clear(inst, &zone);
|
---|
[5a29b4c] | 189 | if (r != EOK)
|
---|
[2bbbfd3] | 190 | return r;
|
---|
[5a29b4c] | 191 |
|
---|
[8ab1adff] | 192 | ino_i->i_izone[1] = zone;
|
---|
[5a29b4c] | 193 | ino_i->dirty = true;
|
---|
[2bbbfd3] | 194 | } else
|
---|
| 195 | return -1;
|
---|
[930baca] | 196 | }
|
---|
[fde8a276] | 197 |
|
---|
[8ab1adff] | 198 | r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
|
---|
[930baca] | 199 | if (r != EOK)
|
---|
[2bbbfd3] | 200 | return r;
|
---|
[930baca] | 201 |
|
---|
[8c76c30] | 202 | /*
|
---|
| 203 | *Compute the position of the second indirect
|
---|
| 204 | *zone pointer in the chain.
|
---|
| 205 | */
|
---|
[8ab1adff] | 206 | uint32_t ind2_off = rblock / ptrs_per_block;
|
---|
[8c76c30] | 207 |
|
---|
| 208 | /*read the second indirect zone of the chain*/
|
---|
[8ab1adff] | 209 | if (ind_zone[ind2_off] == 0) {
|
---|
[2d7c77a] | 210 | if (write_mode) {
|
---|
[8ab1adff] | 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;
|
---|
[5a29b4c] | 220 | }
|
---|
[8ab1adff] | 221 | }
|
---|
[930baca] | 222 |
|
---|
[8ab1adff] | 223 | r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
|
---|
| 224 | if (r != EOK)
|
---|
| 225 | goto out_free_ind1;
|
---|
[930baca] | 226 |
|
---|
[8ab1adff] | 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);
|
---|
[155f792] | 231 | }
|
---|
[8ab1adff] | 232 |
|
---|
[8c76c30] | 233 | r = EOK;
|
---|
[930baca] | 234 |
|
---|
[8ab1adff] | 235 | free(ind2_zone);
|
---|
| 236 | out_free_ind1:
|
---|
| 237 | free(ind_zone);
|
---|
[930baca] | 238 | return r;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[5a29b4c] | 241 |
|
---|
| 242 | static int
|
---|
[8ab1adff] | 243 | reset_zone_content(struct mfs_instance *inst, uint32_t zone)
|
---|
[5a29b4c] | 244 | {
|
---|
| 245 | block_t *b;
|
---|
| 246 | int r;
|
---|
| 247 |
|
---|
[8ab1adff] | 248 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
|
---|
[5a29b4c] | 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
|
---|
[8ab1adff] | 260 | alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
|
---|
[5a29b4c] | 261 | {
|
---|
| 262 | int r;
|
---|
| 263 |
|
---|
[8ab1adff] | 264 | r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
|
---|
[5a29b4c] | 265 | if (r != EOK)
|
---|
| 266 | goto out;
|
---|
| 267 |
|
---|
[8ab1adff] | 268 | r = reset_zone_content(inst, *zone);
|
---|
[5a29b4c] | 269 | out:
|
---|
| 270 | return r;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[8ab1adff] | 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;
|
---|
[8f6bb76e] | 280 | const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
|
---|
| 281 | sizeof(uint32_t);
|
---|
[8ab1adff] | 282 |
|
---|
[8f6bb76e] | 283 | *ind_zone = malloc(max_ind_zone_ptrs);
|
---|
[8ab1adff] | 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 |
|
---|
[6adba0a8] | 340 | /**
|
---|
| 341 | * @}
|
---|
| 342 | */
|
---|
| 343 |
|
---|