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