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