[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,
|
---|
| 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
|
---|
| 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*/
|
---|
[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);
|
---|
| 99 | if (r != EOK)
|
---|
| 100 | return r;
|
---|
| 101 |
|
---|
| 102 | if (old_zone > 0)
|
---|
[40f7297] | 103 | r = mfs_free_bit(mnode->instance, old_zone, BMAP_ZONE);
|
---|
[2bbbfd3] | 104 |
|
---|
[40f7297] | 105 | return r;
|
---|
[2bbbfd3] | 106 | }
|
---|
| 107 |
|
---|
[2d7c77a] | 108 | static int
|
---|
| 109 | rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
|
---|
| 110 | bool write_mode, uint32_t w_block)
|
---|
[930baca] | 111 | {
|
---|
[8c76c30] | 112 | int r, nr_direct;
|
---|
[155f792] | 113 | int ptrs_per_block;
|
---|
[8ab1adff] | 114 | uint32_t *ind_zone, *ind2_zone;
|
---|
[930baca] | 115 |
|
---|
| 116 | assert(mnode);
|
---|
[2d7c77a] | 117 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
[930baca] | 118 |
|
---|
[155f792] | 119 | assert(ino_i);
|
---|
[930baca] | 120 | assert(mnode->instance);
|
---|
| 121 |
|
---|
[5a29b4c] | 122 | struct mfs_instance *inst = mnode->instance;
|
---|
| 123 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
[930baca] | 124 | assert(sbi);
|
---|
| 125 |
|
---|
[2bbbfd3] | 126 | const mfs_version_t fs_version = sbi->fs_version;
|
---|
[155f792] | 127 |
|
---|
| 128 | if (fs_version == MFS_VERSION_V1) {
|
---|
| 129 | nr_direct = V1_NR_DIRECT_ZONES;
|
---|
| 130 | ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
|
---|
| 131 | } else {
|
---|
| 132 | nr_direct = V2_NR_DIRECT_ZONES;
|
---|
| 133 | ptrs_per_block = sbi->block_size / sizeof(uint32_t);
|
---|
| 134 | }
|
---|
[930baca] | 135 |
|
---|
[8c76c30] | 136 | /*Check if the wanted block is in the direct zones*/
|
---|
[155f792] | 137 | if (rblock < nr_direct) {
|
---|
| 138 | *b = ino_i->i_dzone[rblock];
|
---|
[2d7c77a] | 139 | if (write_mode) {
|
---|
| 140 | ino_i->i_dzone[rblock] = w_block;
|
---|
| 141 | ino_i->dirty = true;
|
---|
| 142 | }
|
---|
[2bbbfd3] | 143 | return EOK;
|
---|
[930baca] | 144 | }
|
---|
[8f6bb76e] | 145 |
|
---|
| 146 | rblock -= nr_direct;
|
---|
[930baca] | 147 |
|
---|
| 148 | if (rblock < ptrs_per_block) {
|
---|
[8c76c30] | 149 | /*The wanted block is in the single indirect zone chain*/
|
---|
[155f792] | 150 | if (ino_i->i_izone[0] == 0) {
|
---|
[5a29b4c] | 151 | if (write_mode) {
|
---|
[8ab1adff] | 152 | uint32_t zone;
|
---|
| 153 | r = alloc_zone_and_clear(inst, &zone);
|
---|
[5a29b4c] | 154 | if (r != EOK)
|
---|
[2bbbfd3] | 155 | return r;
|
---|
[5a29b4c] | 156 |
|
---|
[8ab1adff] | 157 | ino_i->i_izone[0] = zone;
|
---|
[5a29b4c] | 158 | ino_i->dirty = true;
|
---|
[8f6bb76e] | 159 | } else {
|
---|
[2bbbfd3] | 160 | return -1;
|
---|
[8f6bb76e] | 161 | }
|
---|
[930baca] | 162 | }
|
---|
| 163 |
|
---|
[8ab1adff] | 164 | r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
|
---|
[152610a8] | 165 | if (r != EOK)
|
---|
[2bbbfd3] | 166 | return r;
|
---|
[930baca] | 167 |
|
---|
[8ab1adff] | 168 | *b = ind_zone[rblock];
|
---|
| 169 | if (write_mode) {
|
---|
| 170 | ind_zone[rblock] = w_block;
|
---|
| 171 | write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
|
---|
[152610a8] | 172 | }
|
---|
[930baca] | 173 |
|
---|
[8ab1adff] | 174 | goto out_free_ind1;
|
---|
[930baca] | 175 | }
|
---|
| 176 |
|
---|
[8f6bb76e] | 177 | rblock -= ptrs_per_block;
|
---|
[930baca] | 178 |
|
---|
[8c76c30] | 179 | /*The wanted block is in the double indirect zone chain*/
|
---|
[930baca] | 180 |
|
---|
[8c76c30] | 181 | /*read the first indirect zone of the chain*/
|
---|
[155f792] | 182 | if (ino_i->i_izone[1] == 0) {
|
---|
[5a29b4c] | 183 | if (write_mode) {
|
---|
[8ab1adff] | 184 | uint32_t zone;
|
---|
| 185 | r = alloc_zone_and_clear(inst, &zone);
|
---|
[5a29b4c] | 186 | if (r != EOK)
|
---|
[2bbbfd3] | 187 | return r;
|
---|
[5a29b4c] | 188 |
|
---|
[8ab1adff] | 189 | ino_i->i_izone[1] = zone;
|
---|
[5a29b4c] | 190 | ino_i->dirty = true;
|
---|
[2bbbfd3] | 191 | } else
|
---|
| 192 | return -1;
|
---|
[930baca] | 193 | }
|
---|
[fde8a276] | 194 |
|
---|
[8ab1adff] | 195 | r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
|
---|
[930baca] | 196 | if (r != EOK)
|
---|
[2bbbfd3] | 197 | return r;
|
---|
[930baca] | 198 |
|
---|
[8c76c30] | 199 | /*
|
---|
| 200 | *Compute the position of the second indirect
|
---|
| 201 | *zone pointer in the chain.
|
---|
| 202 | */
|
---|
[8ab1adff] | 203 | uint32_t ind2_off = rblock / ptrs_per_block;
|
---|
[8c76c30] | 204 |
|
---|
| 205 | /*read the second indirect zone of the chain*/
|
---|
[8ab1adff] | 206 | if (ind_zone[ind2_off] == 0) {
|
---|
[2d7c77a] | 207 | if (write_mode) {
|
---|
[8ab1adff] | 208 | uint32_t zone;
|
---|
| 209 | r = alloc_zone_and_clear(inst, &zone);
|
---|
| 210 | if(r != EOK)
|
---|
| 211 | goto out_free_ind1;
|
---|
| 212 | ind_zone[ind2_off] = zone;
|
---|
| 213 | write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
|
---|
| 214 | } else {
|
---|
| 215 | r = -1;
|
---|
| 216 | goto out_free_ind1;
|
---|
[5a29b4c] | 217 | }
|
---|
[8ab1adff] | 218 | }
|
---|
[930baca] | 219 |
|
---|
[8ab1adff] | 220 | r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
|
---|
| 221 | if (r != EOK)
|
---|
| 222 | goto out_free_ind1;
|
---|
[930baca] | 223 |
|
---|
[8ab1adff] | 224 | *b = ind2_zone[ind2_off % ptrs_per_block];
|
---|
| 225 | if (write_mode) {
|
---|
| 226 | ind2_zone[ind2_off % ptrs_per_block] = w_block;
|
---|
| 227 | write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
|
---|
[155f792] | 228 | }
|
---|
[8ab1adff] | 229 |
|
---|
[8c76c30] | 230 | r = EOK;
|
---|
[930baca] | 231 |
|
---|
[8ab1adff] | 232 | free(ind2_zone);
|
---|
| 233 | out_free_ind1:
|
---|
| 234 | free(ind_zone);
|
---|
[930baca] | 235 | return r;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[5a29b4c] | 238 |
|
---|
| 239 | static int
|
---|
[8ab1adff] | 240 | reset_zone_content(struct mfs_instance *inst, uint32_t zone)
|
---|
[5a29b4c] | 241 | {
|
---|
| 242 | block_t *b;
|
---|
| 243 | int r;
|
---|
| 244 |
|
---|
[8ab1adff] | 245 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NOREAD);
|
---|
[5a29b4c] | 246 | if (r != EOK)
|
---|
| 247 | return r;
|
---|
| 248 |
|
---|
| 249 | memset(b->data, 0, b->size);
|
---|
| 250 | b->dirty = true;
|
---|
| 251 | block_put(b);
|
---|
| 252 |
|
---|
| 253 | return EOK;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | static int
|
---|
[8ab1adff] | 257 | alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
|
---|
[5a29b4c] | 258 | {
|
---|
| 259 | int r;
|
---|
| 260 |
|
---|
[8ab1adff] | 261 | r = mfs_alloc_bit(inst, zone, BMAP_ZONE);
|
---|
[5a29b4c] | 262 | if (r != EOK)
|
---|
| 263 | goto out;
|
---|
| 264 |
|
---|
[8ab1adff] | 265 | r = reset_zone_content(inst, *zone);
|
---|
[5a29b4c] | 266 | out:
|
---|
| 267 | return r;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[8ab1adff] | 270 | static int
|
---|
| 271 | read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
|
---|
| 272 | {
|
---|
| 273 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
| 274 | int r;
|
---|
| 275 | unsigned i;
|
---|
| 276 | block_t *b;
|
---|
[8f6bb76e] | 277 | const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
|
---|
| 278 | sizeof(uint32_t);
|
---|
[8ab1adff] | 279 |
|
---|
[8f6bb76e] | 280 | *ind_zone = malloc(max_ind_zone_ptrs);
|
---|
[8ab1adff] | 281 | if (*ind_zone == NULL)
|
---|
| 282 | return ENOMEM;
|
---|
| 283 |
|
---|
| 284 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NONE);
|
---|
| 285 | if (r != EOK) {
|
---|
| 286 | free(*ind_zone);
|
---|
| 287 | return r;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | if (sbi->fs_version == MFS_VERSION_V1) {
|
---|
| 291 | uint16_t *src_ptr = b->data;
|
---|
| 292 |
|
---|
| 293 | for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
|
---|
| 294 | (*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
|
---|
| 295 | } else {
|
---|
| 296 | uint32_t *src_ptr = b->data;
|
---|
| 297 |
|
---|
| 298 | for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
|
---|
| 299 | (*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | block_put(b);
|
---|
| 303 |
|
---|
| 304 | return EOK;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | static int
|
---|
| 308 | write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
|
---|
| 309 | {
|
---|
| 310 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
| 311 | int r;
|
---|
| 312 | unsigned i;
|
---|
| 313 | block_t *b;
|
---|
| 314 |
|
---|
| 315 | r = block_get(&b, inst->handle, zone, BLOCK_FLAGS_NONE);
|
---|
| 316 | if (r != EOK)
|
---|
| 317 | return r;
|
---|
| 318 |
|
---|
| 319 | if (sbi->fs_version == MFS_VERSION_V1) {
|
---|
| 320 | uint16_t *dest_ptr = b->data;
|
---|
| 321 |
|
---|
| 322 | for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
|
---|
| 323 | dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
|
---|
| 324 | } else {
|
---|
| 325 | uint32_t *dest_ptr = b->data;
|
---|
| 326 |
|
---|
| 327 | for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
|
---|
| 328 | dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
|
---|
| 329 |
|
---|
| 330 | }
|
---|
| 331 | b->dirty = true;
|
---|
| 332 | block_put(b);
|
---|
| 333 | return EOK;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 |
|
---|
[6adba0a8] | 337 | /**
|
---|
| 338 | * @}
|
---|
| 339 | */
|
---|
| 340 |
|
---|