| 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 |
|
|---|
| 33 | #include "mfs.h"
|
|---|
| 34 | #include "mfs_utils.h"
|
|---|
| 35 |
|
|---|
| 36 | static int
|
|---|
| 37 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
|---|
| 38 | bool write_mode, uint32_t w_block);
|
|---|
| 39 |
|
|---|
| 40 | static int
|
|---|
| 41 | reset_zone_content(struct mfs_instance *inst, uint32_t zone);
|
|---|
| 42 |
|
|---|
| 43 | static int
|
|---|
| 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);
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | /**Given the position in the file expressed in
|
|---|
| 54 | *bytes, this function returns the on-disk block
|
|---|
| 55 | *relative to that position.
|
|---|
| 56 | *Returns zero if the block does not exist.
|
|---|
| 57 | */
|
|---|
| 58 | int
|
|---|
| 59 | read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
|
|---|
| 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 |
|
|---|
| 68 | if (mnode->ino_i->i_size < pos) {
|
|---|
| 69 | /*Trying to read beyond the end of file*/
|
|---|
| 70 | r = EOK;
|
|---|
| 71 | *b = 0;
|
|---|
| 72 | goto out;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | r = rw_map_ondisk(b, mnode, rblock, false, 0);
|
|---|
| 76 | out:
|
|---|
| 77 | return r;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | int
|
|---|
| 81 | write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
|
|---|
| 82 | uint32_t *old_zone)
|
|---|
| 83 | {
|
|---|
| 84 | const struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 85 |
|
|---|
| 86 | /*Compute the relative block number in file*/
|
|---|
| 87 | int rblock = pos / sbi->block_size;
|
|---|
| 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);
|
|---|
| 99 | on_error(r, return r);
|
|---|
| 100 |
|
|---|
| 101 | if (old_zone > 0)
|
|---|
| 102 | r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
|
|---|
| 103 |
|
|---|
| 104 | return r;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | static int
|
|---|
| 108 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
|---|
| 109 | bool write_mode, uint32_t w_block)
|
|---|
| 110 | {
|
|---|
| 111 | int r, nr_direct;
|
|---|
| 112 | int ptrs_per_block;
|
|---|
| 113 | uint32_t *ind_zone, *ind2_zone;
|
|---|
| 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 |
|
|---|
| 145 | rblock -= nr_direct;
|
|---|
| 146 |
|
|---|
| 147 | if (rblock < ptrs_per_block) {
|
|---|
| 148 | /*The wanted block is in the single indirect zone chain*/
|
|---|
| 149 | if (ino_i->i_izone[0] == 0) {
|
|---|
| 150 | if (write_mode) {
|
|---|
| 151 | uint32_t zone;
|
|---|
| 152 | r = alloc_zone_and_clear(inst, &zone);
|
|---|
| 153 | if (r != EOK)
|
|---|
| 154 | return r;
|
|---|
| 155 |
|
|---|
| 156 | ino_i->i_izone[0] = zone;
|
|---|
| 157 | ino_i->dirty = true;
|
|---|
| 158 | } else {
|
|---|
| 159 | return -1;
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
|
|---|
| 164 | on_error(r, return r);
|
|---|
| 165 |
|
|---|
| 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);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | goto out_free_ind1;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | rblock -= ptrs_per_block;
|
|---|
| 176 |
|
|---|
| 177 | /*The wanted block is in the double indirect zone chain*/
|
|---|
| 178 |
|
|---|
| 179 | /*read the first indirect zone of the chain*/
|
|---|
| 180 | if (ino_i->i_izone[1] == 0) {
|
|---|
| 181 | if (write_mode) {
|
|---|
| 182 | uint32_t zone;
|
|---|
| 183 | r = alloc_zone_and_clear(inst, &zone);
|
|---|
| 184 | on_error(r, return r);
|
|---|
| 185 |
|
|---|
| 186 | ino_i->i_izone[1] = zone;
|
|---|
| 187 | ino_i->dirty = true;
|
|---|
| 188 | } else
|
|---|
| 189 | return -1;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
|
|---|
| 193 | on_error(r, return r);
|
|---|
| 194 |
|
|---|
| 195 | /*
|
|---|
| 196 | *Compute the position of the second indirect
|
|---|
| 197 | *zone pointer in the chain.
|
|---|
| 198 | */
|
|---|
| 199 | uint32_t ind2_off = rblock / ptrs_per_block;
|
|---|
| 200 |
|
|---|
| 201 | /*read the second indirect zone of the chain*/
|
|---|
| 202 | if (ind_zone[ind2_off] == 0) {
|
|---|
| 203 | if (write_mode) {
|
|---|
| 204 | uint32_t zone;
|
|---|
| 205 | r = alloc_zone_and_clear(inst, &zone);
|
|---|
| 206 | on_error(r, goto out_free_ind1);
|
|---|
| 207 |
|
|---|
| 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;
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
|
|---|
| 217 | on_error(r, goto out_free_ind1);
|
|---|
| 218 |
|
|---|
| 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);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | r = EOK;
|
|---|
| 226 |
|
|---|
| 227 | free(ind2_zone);
|
|---|
| 228 | out_free_ind1:
|
|---|
| 229 | free(ind_zone);
|
|---|
| 230 | return r;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 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]) {
|
|---|
| 258 | r = mfs_free_bit(inst, ino_i->i_izone[0], BMAP_ZONE);
|
|---|
| 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 |
|
|---|
| 270 | if ((fzone_to_free % ptrs_per_block) != 0)
|
|---|
| 271 | ++fzone_to_free;
|
|---|
| 272 |
|
|---|
| 273 | /*free the entire double indirect zone*/
|
|---|
| 274 | uint32_t *dbl_zone;
|
|---|
| 275 |
|
|---|
| 276 | if (ino_i->i_izone[1] == 0) {
|
|---|
| 277 | /*Nothing to be done*/
|
|---|
| 278 | return EOK;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 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 |
|
|---|
| 288 | r = mfs_free_bit(inst, dbl_zone[i], BMAP_ZONE);
|
|---|
| 289 | on_error(r, return r);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | if (fzone_to_free == 0) {
|
|---|
| 293 | r = mfs_free_bit(inst, ino_i->i_izone[1], BMAP_ZONE);
|
|---|
| 294 | ino_i->i_izone[1] = 0;
|
|---|
| 295 | ino_i->dirty = true;
|
|---|
| 296 | }
|
|---|
| 297 | free(dbl_zone);
|
|---|
| 298 |
|
|---|
| 299 | return r;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | static int
|
|---|
| 303 | reset_zone_content(struct mfs_instance *inst, uint32_t zone)
|
|---|
| 304 | {
|
|---|
| 305 | block_t *b;
|
|---|
| 306 | int r;
|
|---|
| 307 |
|
|---|
| 308 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
|
|---|
| 309 | on_error(r, return r);
|
|---|
| 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
|
|---|
| 319 | alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
|
|---|
| 320 | {
|
|---|
| 321 | int r;
|
|---|
| 322 |
|
|---|
| 323 | r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
|
|---|
| 324 | on_error(r, return r);
|
|---|
| 325 |
|
|---|
| 326 | r = reset_zone_content(inst, *zone);
|
|---|
| 327 | return r;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 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;
|
|---|
| 337 | const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
|
|---|
| 338 | sizeof(uint32_t);
|
|---|
| 339 |
|
|---|
| 340 | *ind_zone = malloc(max_ind_zone_ptrs);
|
|---|
| 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);
|
|---|
| 363 |
|
|---|
| 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);
|
|---|
| 376 | on_error(r, return r);
|
|---|
| 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 |
|
|---|
| 396 | /**
|
|---|
| 397 | * @}
|
|---|
| 398 | */
|
|---|
| 399 |
|
|---|