| 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 <align.h>
|
|---|
| 34 | #include "mfs.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 | *
|
|---|
| 57 | * @param b Pointer to a 32bit number where the block number will be stored
|
|---|
| 58 | * @param mnode Pointer to a generic MINIX inode in memory.
|
|---|
| 59 | * @param pos Position in file.
|
|---|
| 60 | *
|
|---|
| 61 | * @return EOK on success or a negative error code.
|
|---|
| 62 | */
|
|---|
| 63 | int
|
|---|
| 64 | mfs_read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
|
|---|
| 65 | {
|
|---|
| 66 | int r;
|
|---|
| 67 | const struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 68 | const int block_size = sbi->block_size;
|
|---|
| 69 |
|
|---|
| 70 | /* Compute relative block number in file */
|
|---|
| 71 | int rblock = pos / block_size;
|
|---|
| 72 |
|
|---|
| 73 | if (ROUND_UP(mnode->ino_i->i_size, sbi->block_size) < pos) {
|
|---|
| 74 | /* Trying to read beyond the end of file */
|
|---|
| 75 | r = EOK;
|
|---|
| 76 | *b = 0;
|
|---|
| 77 | goto out;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | r = rw_map_ondisk(b, mnode, rblock, false, 0);
|
|---|
| 81 | out:
|
|---|
| 82 | return r;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | int
|
|---|
| 86 | mfs_write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
|
|---|
| 87 | uint32_t *old_zone)
|
|---|
| 88 | {
|
|---|
| 89 | const struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 90 |
|
|---|
| 91 | if (pos >= sbi->max_file_size) {
|
|---|
| 92 | /* Can't write beyond the maximum file size */
|
|---|
| 93 | return EINVAL;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /* Compute the relative block number in file */
|
|---|
| 97 | int rblock = pos / sbi->block_size;
|
|---|
| 98 |
|
|---|
| 99 | return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | static int
|
|---|
| 103 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
|---|
| 104 | bool write_mode, uint32_t w_block)
|
|---|
| 105 | {
|
|---|
| 106 | int r, nr_direct;
|
|---|
| 107 | int ptrs_per_block;
|
|---|
| 108 | uint32_t *ind_zone, *ind2_zone;
|
|---|
| 109 |
|
|---|
| 110 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| 111 | struct mfs_instance *inst = mnode->instance;
|
|---|
| 112 | struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| 113 |
|
|---|
| 114 | const mfs_version_t fs_version = sbi->fs_version;
|
|---|
| 115 | const bool deleting = write_mode && (w_block == 0);
|
|---|
| 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 | }
|
|---|
| 124 |
|
|---|
| 125 | /* Check if the wanted block is in the direct zones */
|
|---|
| 126 | if (rblock < nr_direct) {
|
|---|
| 127 | *b = ino_i->i_dzone[rblock];
|
|---|
| 128 | if (write_mode) {
|
|---|
| 129 | ino_i->i_dzone[rblock] = w_block;
|
|---|
| 130 | ino_i->dirty = true;
|
|---|
| 131 | }
|
|---|
| 132 | return EOK;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | rblock -= nr_direct;
|
|---|
| 136 |
|
|---|
| 137 | if (rblock < ptrs_per_block) {
|
|---|
| 138 | /* The wanted block is in the single indirect zone chain */
|
|---|
| 139 | if (ino_i->i_izone[0] == 0) {
|
|---|
| 140 | if (write_mode && !deleting) {
|
|---|
| 141 | uint32_t zone;
|
|---|
| 142 | r = alloc_zone_and_clear(inst, &zone);
|
|---|
| 143 | if (r != EOK)
|
|---|
| 144 | return r;
|
|---|
| 145 |
|
|---|
| 146 | ino_i->i_izone[0] = zone;
|
|---|
| 147 | ino_i->dirty = true;
|
|---|
| 148 | } else {
|
|---|
| 149 | /* Sparse block */
|
|---|
| 150 | *b = 0;
|
|---|
| 151 | return EOK;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
|
|---|
| 156 | if (r != EOK)
|
|---|
| 157 | return r;
|
|---|
| 158 |
|
|---|
| 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);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | goto out_free_ind1;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | rblock -= ptrs_per_block;
|
|---|
| 169 |
|
|---|
| 170 | /* The wanted block is in the double indirect zone chain */
|
|---|
| 171 |
|
|---|
| 172 | /* Read the first indirect zone of the chain */
|
|---|
| 173 | if (ino_i->i_izone[1] == 0) {
|
|---|
| 174 | if (write_mode && !deleting) {
|
|---|
| 175 | uint32_t zone;
|
|---|
| 176 | r = alloc_zone_and_clear(inst, &zone);
|
|---|
| 177 | if (r != EOK)
|
|---|
| 178 | return r;
|
|---|
| 179 |
|
|---|
| 180 | ino_i->i_izone[1] = zone;
|
|---|
| 181 | ino_i->dirty = true;
|
|---|
| 182 | } else {
|
|---|
| 183 | /* Sparse block */
|
|---|
| 184 | *b = 0;
|
|---|
| 185 | return EOK;
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
|
|---|
| 190 | if (r != EOK)
|
|---|
| 191 | return r;
|
|---|
| 192 |
|
|---|
| 193 | /*
|
|---|
| 194 | * Compute the position of the second indirect
|
|---|
| 195 | * zone pointer in the chain.
|
|---|
| 196 | */
|
|---|
| 197 | uint32_t ind2_off = rblock / ptrs_per_block;
|
|---|
| 198 |
|
|---|
| 199 | /* read the second indirect zone of the chain */
|
|---|
| 200 | if (ind_zone[ind2_off] == 0) {
|
|---|
| 201 | if (write_mode && !deleting) {
|
|---|
| 202 | uint32_t zone;
|
|---|
| 203 | r = alloc_zone_and_clear(inst, &zone);
|
|---|
| 204 | if (r != EOK)
|
|---|
| 205 | goto out_free_ind1;
|
|---|
| 206 |
|
|---|
| 207 | ind_zone[ind2_off] = zone;
|
|---|
| 208 | write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
|
|---|
| 209 | } else {
|
|---|
| 210 | /* Sparse block */
|
|---|
| 211 | r = EOK;
|
|---|
| 212 | *b = 0;
|
|---|
| 213 | goto out_free_ind1;
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
|
|---|
| 218 | if (r != EOK)
|
|---|
| 219 | goto out_free_ind1;
|
|---|
| 220 |
|
|---|
| 221 | *b = ind2_zone[rblock - (ind2_off * ptrs_per_block)];
|
|---|
| 222 | if (write_mode) {
|
|---|
| 223 | ind2_zone[rblock - (ind2_off * ptrs_per_block)] = w_block;
|
|---|
| 224 | write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | r = EOK;
|
|---|
| 228 |
|
|---|
| 229 | free(ind2_zone);
|
|---|
| 230 | out_free_ind1:
|
|---|
| 231 | free(ind_zone);
|
|---|
| 232 | return r;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /**Free unused indirect zones from a MINIX inode according to its new size.
|
|---|
| 236 | *
|
|---|
| 237 | * @param mnode Pointer to a generic MINIX inode in memory.
|
|---|
| 238 | * @param new_size The new size of the inode.
|
|---|
| 239 | *
|
|---|
| 240 | * @return EOK on success or a negative error code.
|
|---|
| 241 | */
|
|---|
| 242 | int
|
|---|
| 243 | mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size)
|
|---|
| 244 | {
|
|---|
| 245 | struct mfs_instance *inst = mnode->instance;
|
|---|
| 246 | struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| 247 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| 248 | int nr_direct, ptrs_per_block, rblock, r;
|
|---|
| 249 | int i;
|
|---|
| 250 |
|
|---|
| 251 | mfs_version_t fs_version = sbi->fs_version;
|
|---|
| 252 |
|
|---|
| 253 | assert(new_size <= ino_i->i_size);
|
|---|
| 254 |
|
|---|
| 255 | if (fs_version == MFS_VERSION_V1) {
|
|---|
| 256 | nr_direct = V1_NR_DIRECT_ZONES;
|
|---|
| 257 | ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
|
|---|
| 258 | } else {
|
|---|
| 259 | nr_direct = V2_NR_DIRECT_ZONES;
|
|---|
| 260 | ptrs_per_block = sbi->block_size / sizeof(uint32_t);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | rblock = new_size / sbi->block_size;
|
|---|
| 264 |
|
|---|
| 265 | if (rblock < nr_direct) {
|
|---|
| 266 | /* Free the single indirect zone */
|
|---|
| 267 | if (ino_i->i_izone[0]) {
|
|---|
| 268 | r = mfs_free_zone(inst, ino_i->i_izone[0]);
|
|---|
| 269 | if (r != EOK)
|
|---|
| 270 | return r;
|
|---|
| 271 |
|
|---|
| 272 | ino_i->i_izone[0] = 0;
|
|---|
| 273 | ino_i->dirty = true;
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | rblock -= nr_direct + ptrs_per_block;
|
|---|
| 278 |
|
|---|
| 279 | int fzone_to_free = (rblock < 0 ? 0 : rblock) / ptrs_per_block;
|
|---|
| 280 |
|
|---|
| 281 | if ((fzone_to_free % ptrs_per_block) != 0)
|
|---|
| 282 | ++fzone_to_free;
|
|---|
| 283 |
|
|---|
| 284 | /* Free the entire double indirect zone */
|
|---|
| 285 | uint32_t *dbl_zone;
|
|---|
| 286 |
|
|---|
| 287 | if (ino_i->i_izone[1] == 0) {
|
|---|
| 288 | /* Nothing to be done */
|
|---|
| 289 | return EOK;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | r = read_ind_zone(inst, ino_i->i_izone[1], &dbl_zone);
|
|---|
| 293 | if (r != EOK)
|
|---|
| 294 | return r;
|
|---|
| 295 |
|
|---|
| 296 | for (i = fzone_to_free; i < ptrs_per_block; ++i) {
|
|---|
| 297 | if (dbl_zone[i] == 0)
|
|---|
| 298 | continue;
|
|---|
| 299 |
|
|---|
| 300 | r = mfs_free_zone(inst, dbl_zone[i]);
|
|---|
| 301 | if (r != EOK)
|
|---|
| 302 | goto out;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | if (fzone_to_free == 0) {
|
|---|
| 306 | r = mfs_free_zone(inst, ino_i->i_izone[1]);
|
|---|
| 307 | ino_i->i_izone[1] = 0;
|
|---|
| 308 | ino_i->dirty = true;
|
|---|
| 309 | }
|
|---|
| 310 | out:
|
|---|
| 311 | free(dbl_zone);
|
|---|
| 312 | return r;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | static int
|
|---|
| 316 | reset_zone_content(struct mfs_instance *inst, uint32_t zone)
|
|---|
| 317 | {
|
|---|
| 318 | block_t *b;
|
|---|
| 319 | int r;
|
|---|
| 320 |
|
|---|
| 321 | r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
|
|---|
| 322 | if (r != EOK)
|
|---|
| 323 | return r;
|
|---|
| 324 |
|
|---|
| 325 | memset(b->data, 0, b->size);
|
|---|
| 326 | b->dirty = true;
|
|---|
| 327 |
|
|---|
| 328 | return block_put(b);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | static int
|
|---|
| 332 | alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
|
|---|
| 333 | {
|
|---|
| 334 | int r;
|
|---|
| 335 |
|
|---|
| 336 | r = mfs_alloc_zone(inst, zone);
|
|---|
| 337 | if (r != EOK)
|
|---|
| 338 | return r;
|
|---|
| 339 |
|
|---|
| 340 | r = reset_zone_content(inst, *zone);
|
|---|
| 341 | return r;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | static int
|
|---|
| 345 | read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
|
|---|
| 346 | {
|
|---|
| 347 | struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| 348 | int r;
|
|---|
| 349 | unsigned i;
|
|---|
| 350 | block_t *b;
|
|---|
| 351 | const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
|
|---|
| 352 | sizeof(uint32_t);
|
|---|
| 353 |
|
|---|
| 354 | *ind_zone = malloc(max_ind_zone_ptrs);
|
|---|
| 355 | if (*ind_zone == NULL)
|
|---|
| 356 | return ENOMEM;
|
|---|
| 357 |
|
|---|
| 358 | r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NONE);
|
|---|
| 359 | if (r != EOK) {
|
|---|
| 360 | free(*ind_zone);
|
|---|
| 361 | return r;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | if (sbi->fs_version == MFS_VERSION_V1) {
|
|---|
| 365 | uint16_t *src_ptr = b->data;
|
|---|
| 366 |
|
|---|
| 367 | for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
|
|---|
| 368 | (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
|
|---|
| 369 | } else {
|
|---|
| 370 | uint32_t *src_ptr = b->data;
|
|---|
| 371 |
|
|---|
| 372 | for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
|
|---|
| 373 | (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | return block_put(b);
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | static int
|
|---|
| 380 | write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
|
|---|
| 381 | {
|
|---|
| 382 | struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| 383 | int r;
|
|---|
| 384 | unsigned i;
|
|---|
| 385 | block_t *b;
|
|---|
| 386 |
|
|---|
| 387 | r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
|
|---|
| 388 | if (r != EOK)
|
|---|
| 389 | return r;
|
|---|
| 390 |
|
|---|
| 391 | if (sbi->fs_version == MFS_VERSION_V1) {
|
|---|
| 392 | uint16_t *dest_ptr = b->data;
|
|---|
| 393 |
|
|---|
| 394 | for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
|
|---|
| 395 | dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
|
|---|
| 396 | } else {
|
|---|
| 397 | uint32_t *dest_ptr = b->data;
|
|---|
| 398 |
|
|---|
| 399 | for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
|
|---|
| 400 | dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
|
|---|
| 401 |
|
|---|
| 402 | }
|
|---|
| 403 | b->dirty = true;
|
|---|
| 404 |
|
|---|
| 405 | return block_put(b);
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 | /**
|
|---|
| 410 | * @}
|
|---|
| 411 | */
|
|---|
| 412 |
|
|---|