[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 |
|
---|
[f213ae7] | 33 | #include <stdlib.h>
|
---|
| 34 | #include "mfs.h"
|
---|
| 35 |
|
---|
[10eb754] | 36 | static int
|
---|
| 37 | mfs_write_inode_raw(struct mfs_node *mnode);
|
---|
| 38 |
|
---|
[cdab59e] | 39 | static int
|
---|
| 40 | mfs2_write_inode_raw(struct mfs_node *mnode);
|
---|
| 41 |
|
---|
[4bf0052a] | 42 | static int
|
---|
| 43 | mfs_read_inode_raw(const struct mfs_instance *instance,
|
---|
| 44 | struct mfs_ino_info **ino_ptr, uint16_t inum);
|
---|
[c922bc7] | 45 |
|
---|
[4bf0052a] | 46 | static int
|
---|
| 47 | mfs2_read_inode_raw(const struct mfs_instance *instance,
|
---|
| 48 | struct mfs_ino_info **ino_ptr, uint32_t inum);
|
---|
[c922bc7] | 49 |
|
---|
[77ec4d9] | 50 | /**Read a MINIX inode from disk
|
---|
| 51 | *
|
---|
| 52 | * @param inst Pointer to the filesystem instance.
|
---|
| 53 | * @param ino_i Pointer to the generic MINIX inode
|
---|
| 54 | * where the inode content will be stored.
|
---|
| 55 | * @param index index of the inode to read.
|
---|
| 56 | *
|
---|
| 57 | * @return EOK on success or a negative error code.
|
---|
| 58 | */
|
---|
[c922bc7] | 59 | int
|
---|
[3a5ee6c] | 60 | mfs_get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
|
---|
[44c6091f] | 61 | fs_index_t index)
|
---|
[c922bc7] | 62 | {
|
---|
| 63 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
[4bf0052a] | 64 | int r;
|
---|
[c922bc7] | 65 |
|
---|
| 66 | if (sbi->fs_version == MFS_VERSION_V1) {
|
---|
| 67 | /*Read a MFS V1 inode*/
|
---|
[4bf0052a] | 68 | r = mfs_read_inode_raw(inst, ino_i, index);
|
---|
[c922bc7] | 69 | } else {
|
---|
| 70 | /*Read a MFS V2/V3 inode*/
|
---|
[4bf0052a] | 71 | r = mfs2_read_inode_raw(inst, ino_i, index);
|
---|
[c922bc7] | 72 | }
|
---|
| 73 |
|
---|
[4bf0052a] | 74 | return r;
|
---|
[c922bc7] | 75 | }
|
---|
| 76 |
|
---|
[4bf0052a] | 77 | static int
|
---|
| 78 | mfs_read_inode_raw(const struct mfs_instance *instance,
|
---|
| 79 | struct mfs_ino_info **ino_ptr, uint16_t inum) {
|
---|
| 80 | struct mfs_inode *ino;
|
---|
[155f792] | 81 | struct mfs_ino_info *ino_i = NULL;
|
---|
[f213ae7] | 82 | struct mfs_sb_info *sbi;
|
---|
| 83 | block_t *b;
|
---|
[4bf0052a] | 84 | int i, r;
|
---|
[10eb754] | 85 |
|
---|
| 86 | sbi = instance->sbi;
|
---|
[e33100c] | 87 |
|
---|
| 88 | /*inode 0 does not exist*/
|
---|
| 89 | inum -= 1;
|
---|
| 90 |
|
---|
[10eb754] | 91 | const int ino_off = inum % sbi->ino_per_block;
|
---|
[f213ae7] | 92 |
|
---|
[b438804] | 93 | ino_i = malloc(sizeof(*ino_i));
|
---|
[f213ae7] | 94 |
|
---|
[4bf0052a] | 95 | if (!ino_i) {
|
---|
| 96 | r = ENOMEM;
|
---|
[155f792] | 97 | goto out_err;
|
---|
[4bf0052a] | 98 | }
|
---|
[f213ae7] | 99 |
|
---|
[10eb754] | 100 | const int itable_off = sbi->itable_off;
|
---|
[8b86ed26] | 101 |
|
---|
[03bc76a] | 102 | r = block_get(&b, instance->service_id,
|
---|
[44c6091f] | 103 | itable_off + inum / sbi->ino_per_block,
|
---|
[4bf0052a] | 104 | BLOCK_FLAGS_NONE);
|
---|
[c699b0c] | 105 | if (r != EOK)
|
---|
| 106 | goto out_err;
|
---|
[f213ae7] | 107 |
|
---|
[4bf0052a] | 108 | ino = b->data + ino_off * sizeof(struct mfs_inode);
|
---|
[f213ae7] | 109 |
|
---|
[155f792] | 110 | ino_i->i_mode = conv16(sbi->native, ino->i_mode);
|
---|
| 111 | ino_i->i_uid = conv16(sbi->native, ino->i_uid);
|
---|
| 112 | ino_i->i_size = conv32(sbi->native, ino->i_size);
|
---|
| 113 | ino_i->i_mtime = conv32(sbi->native, ino->i_mtime);
|
---|
[7cb975e] | 114 | ino_i->i_nlinks = ino->i_nlinks;
|
---|
[f213ae7] | 115 |
|
---|
| 116 | for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
|
---|
[155f792] | 117 | ino_i->i_dzone[i] = conv16(sbi->native, ino->i_dzone[i]);
|
---|
[f213ae7] | 118 |
|
---|
| 119 | for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
|
---|
[155f792] | 120 | ino_i->i_izone[i] = conv16(sbi->native, ino->i_izone[i]);
|
---|
[f213ae7] | 121 |
|
---|
[4bf0052a] | 122 | r = block_put(b);
|
---|
[54caa41b] | 123 | ino_i->dirty = false;
|
---|
[4bf0052a] | 124 | *ino_ptr = ino_i;
|
---|
[54caa41b] | 125 |
|
---|
[4bf0052a] | 126 | return r;
|
---|
[155f792] | 127 |
|
---|
| 128 | out_err:
|
---|
| 129 | if (ino_i)
|
---|
| 130 | free(ino_i);
|
---|
[4bf0052a] | 131 | return EOK;
|
---|
[f213ae7] | 132 | }
|
---|
| 133 |
|
---|
[4bf0052a] | 134 | static int
|
---|
| 135 | mfs2_read_inode_raw(const struct mfs_instance *instance,
|
---|
| 136 | struct mfs_ino_info **ino_ptr, uint32_t inum) {
|
---|
| 137 | struct mfs2_inode *ino;
|
---|
[155f792] | 138 | struct mfs_ino_info *ino_i = NULL;
|
---|
[df22c36] | 139 | struct mfs_sb_info *sbi;
|
---|
| 140 | block_t *b;
|
---|
[4bf0052a] | 141 | int i, r;
|
---|
[df22c36] | 142 |
|
---|
[b438804] | 143 | ino_i = malloc(sizeof(*ino_i));
|
---|
[df22c36] | 144 |
|
---|
[4bf0052a] | 145 | if (!ino_i) {
|
---|
| 146 | r = ENOMEM;
|
---|
[155f792] | 147 | goto out_err;
|
---|
[4bf0052a] | 148 | }
|
---|
[df22c36] | 149 |
|
---|
| 150 | sbi = instance->sbi;
|
---|
| 151 |
|
---|
[e33100c] | 152 | /*inode 0 does not exist*/
|
---|
| 153 | inum -= 1;
|
---|
| 154 |
|
---|
[10eb754] | 155 | const int itable_off = sbi->itable_off;
|
---|
| 156 | const int ino_off = inum % sbi->ino_per_block;
|
---|
[cfff7a8f] | 157 |
|
---|
[03bc76a] | 158 | r = block_get(&b, instance->service_id,
|
---|
[44c6091f] | 159 | itable_off + inum / sbi->ino_per_block,
|
---|
[4bf0052a] | 160 | BLOCK_FLAGS_NONE);
|
---|
[c699b0c] | 161 | if (r != EOK)
|
---|
| 162 | goto out_err;
|
---|
[df22c36] | 163 |
|
---|
[4bf0052a] | 164 | ino = b->data + ino_off * sizeof(struct mfs2_inode);
|
---|
[df22c36] | 165 |
|
---|
[155f792] | 166 | ino_i->i_mode = conv16(sbi->native, ino->i_mode);
|
---|
| 167 | ino_i->i_nlinks = conv16(sbi->native, ino->i_nlinks);
|
---|
| 168 | ino_i->i_uid = conv16(sbi->native, ino->i_uid);
|
---|
| 169 | ino_i->i_gid = conv16(sbi->native, ino->i_gid);
|
---|
| 170 | ino_i->i_size = conv32(sbi->native, ino->i_size);
|
---|
| 171 | ino_i->i_atime = conv32(sbi->native, ino->i_atime);
|
---|
| 172 | ino_i->i_mtime = conv32(sbi->native, ino->i_mtime);
|
---|
| 173 | ino_i->i_ctime = conv32(sbi->native, ino->i_ctime);
|
---|
[df22c36] | 174 |
|
---|
| 175 | for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
|
---|
[155f792] | 176 | ino_i->i_dzone[i] = conv32(sbi->native, ino->i_dzone[i]);
|
---|
[df22c36] | 177 |
|
---|
| 178 | for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
|
---|
[155f792] | 179 | ino_i->i_izone[i] = conv32(sbi->native, ino->i_izone[i]);
|
---|
[df22c36] | 180 |
|
---|
[4bf0052a] | 181 | r = block_put(b);
|
---|
[54caa41b] | 182 | ino_i->dirty = false;
|
---|
[4bf0052a] | 183 | *ino_ptr = ino_i;
|
---|
[54caa41b] | 184 |
|
---|
[4bf0052a] | 185 | return r;
|
---|
[155f792] | 186 |
|
---|
| 187 | out_err:
|
---|
| 188 | if (ino_i)
|
---|
| 189 | free(ino_i);
|
---|
[4bf0052a] | 190 | return EOK;
|
---|
[df22c36] | 191 | }
|
---|
| 192 |
|
---|
[77ec4d9] | 193 | /**Write a MINIX inode on disk (if marked as dirty)
|
---|
| 194 | *
|
---|
| 195 | * @param mnode Pointer to the generic MINIX inode in memory.
|
---|
| 196 | *
|
---|
| 197 | * @return EOK on success or a negative error code.
|
---|
| 198 | */
|
---|
[10eb754] | 199 | int
|
---|
[5f509cc] | 200 | mfs_put_inode(struct mfs_node *mnode)
|
---|
[10eb754] | 201 | {
|
---|
| 202 | int rc = EOK;
|
---|
| 203 |
|
---|
| 204 | if (!mnode->ino_i->dirty)
|
---|
| 205 | goto out;
|
---|
| 206 |
|
---|
| 207 | struct mfs_instance *inst = mnode->instance;
|
---|
| 208 | struct mfs_sb_info *sbi = inst->sbi;
|
---|
| 209 |
|
---|
| 210 | if (sbi->fs_version == MFS_VERSION_V1)
|
---|
| 211 | rc = mfs_write_inode_raw(mnode);
|
---|
[cdab59e] | 212 | else
|
---|
| 213 | rc = mfs2_write_inode_raw(mnode);
|
---|
[10eb754] | 214 |
|
---|
| 215 | out:
|
---|
| 216 | return rc;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | static int
|
---|
| 220 | mfs_write_inode_raw(struct mfs_node *mnode)
|
---|
| 221 | {
|
---|
| 222 | int i, r;
|
---|
| 223 | block_t *b;
|
---|
| 224 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
| 225 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
| 226 |
|
---|
[3ab580a] | 227 | const uint32_t inum = ino_i->index - 1;
|
---|
[10eb754] | 228 | const int itable_off = sbi->itable_off;
|
---|
[3ab580a] | 229 | const int ino_off = inum % sbi->ino_per_block;
|
---|
[10eb754] | 230 | const bool native = sbi->native;
|
---|
| 231 |
|
---|
[03bc76a] | 232 | r = block_get(&b, mnode->instance->service_id,
|
---|
[44c6091f] | 233 | itable_off + inum / sbi->ino_per_block,
|
---|
| 234 | BLOCK_FLAGS_NONE);
|
---|
[10eb754] | 235 |
|
---|
[c699b0c] | 236 | if (r != EOK)
|
---|
| 237 | goto out;
|
---|
[10eb754] | 238 |
|
---|
| 239 | struct mfs_inode *ino = b->data;
|
---|
| 240 | ino += ino_off;
|
---|
| 241 |
|
---|
| 242 | ino->i_mode = conv16(native, ino_i->i_mode);
|
---|
| 243 | ino->i_uid = conv16(native, ino_i->i_uid);
|
---|
| 244 | ino->i_gid = ino_i->i_gid;
|
---|
| 245 | ino->i_nlinks = ino_i->i_nlinks;
|
---|
| 246 | ino->i_size = conv32(native, ino_i->i_size);
|
---|
| 247 | ino->i_mtime = conv32(native, ino_i->i_mtime);
|
---|
| 248 |
|
---|
| 249 | for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
|
---|
| 250 | ino->i_dzone[i] = conv16(native, ino_i->i_dzone[i]);
|
---|
| 251 | for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
|
---|
| 252 | ino->i_izone[i] = conv16(native, ino_i->i_izone[i]);
|
---|
| 253 |
|
---|
| 254 | b->dirty = true;
|
---|
[9530d94] | 255 | r = block_put(b);
|
---|
[10eb754] | 256 |
|
---|
| 257 | ino_i->dirty = false;
|
---|
| 258 | out:
|
---|
| 259 | return r;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[cdab59e] | 262 | static int
|
---|
| 263 | mfs2_write_inode_raw(struct mfs_node *mnode)
|
---|
| 264 | {
|
---|
| 265 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
| 266 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
| 267 | block_t *b;
|
---|
| 268 | int i, r;
|
---|
| 269 |
|
---|
[3ab580a] | 270 | const uint32_t inum = ino_i->index - 1;
|
---|
[cdab59e] | 271 | const int itable_off = sbi->itable_off;
|
---|
[3ab580a] | 272 | const int ino_off = inum % sbi->ino_per_block;
|
---|
[cdab59e] | 273 | const bool native = sbi->native;
|
---|
[44c6091f] | 274 |
|
---|
[03bc76a] | 275 | r = block_get(&b, mnode->instance->service_id,
|
---|
[44c6091f] | 276 | itable_off + inum / sbi->ino_per_block,
|
---|
| 277 | BLOCK_FLAGS_NONE);
|
---|
[cdab59e] | 278 |
|
---|
[c699b0c] | 279 | if (r != EOK)
|
---|
| 280 | goto out;
|
---|
[cdab59e] | 281 |
|
---|
| 282 | struct mfs2_inode *ino2 = b->data;
|
---|
| 283 | ino2 += ino_off;
|
---|
| 284 |
|
---|
| 285 | ino2->i_mode = conv16(native, ino_i->i_mode);
|
---|
[c2fcfc0] | 286 | ino2->i_nlinks = conv16(native, ino_i->i_nlinks);
|
---|
[cdab59e] | 287 | ino2->i_uid = conv16(native, ino_i->i_uid);
|
---|
| 288 | ino2->i_gid = conv16(native, ino_i->i_gid);
|
---|
[44c6091f] | 289 | ino2->i_size = conv32(native, ino_i->i_size);
|
---|
[cdab59e] | 290 | ino2->i_atime = conv32(native, ino_i->i_atime);
|
---|
| 291 | ino2->i_mtime = conv32(native, ino_i->i_mtime);
|
---|
| 292 | ino2->i_ctime = conv32(native, ino_i->i_ctime);
|
---|
| 293 |
|
---|
| 294 | for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
|
---|
| 295 | ino2->i_dzone[i] = conv32(native, ino_i->i_dzone[i]);
|
---|
| 296 |
|
---|
| 297 | for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
|
---|
| 298 | ino2->i_izone[i] = conv32(native, ino_i->i_izone[i]);
|
---|
| 299 |
|
---|
| 300 | b->dirty = true;
|
---|
[9530d94] | 301 | r = block_put(b);
|
---|
[cdab59e] | 302 | ino_i->dirty = false;
|
---|
| 303 |
|
---|
| 304 | out:
|
---|
| 305 | return r;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[77ec4d9] | 308 | /**Reduce the inode size of a given number of bytes
|
---|
| 309 | *
|
---|
| 310 | * @param mnode Pointer to the generic MINIX inode in memory.
|
---|
| 311 | * @param size_shrink Number of bytes that will be subtracted to the inode.
|
---|
| 312 | *
|
---|
| 313 | * @return EOK on success or a negative error code.
|
---|
| 314 | */
|
---|
[8a49fed] | 315 | int
|
---|
[3a5ee6c] | 316 | mfs_inode_shrink(struct mfs_node *mnode, size_t size_shrink)
|
---|
[8a49fed] | 317 | {
|
---|
| 318 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
| 319 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
---|
| 320 | const size_t bs = sbi->block_size;
|
---|
| 321 | int r;
|
---|
| 322 |
|
---|
[44799a0] | 323 | if (size_shrink == 0) {
|
---|
| 324 | /*File is empty*/
|
---|
| 325 | return EOK;
|
---|
| 326 | }
|
---|
[8a49fed] | 327 |
|
---|
| 328 | const size_t old_size = ino_i->i_size;
|
---|
| 329 | const size_t new_size = ino_i->i_size - size_shrink;
|
---|
| 330 |
|
---|
| 331 | assert(size_shrink <= old_size);
|
---|
| 332 |
|
---|
| 333 | ino_i->dirty = true;
|
---|
| 334 |
|
---|
| 335 | /*Compute the number of zones to free*/
|
---|
[38b7233] | 336 | unsigned zones_to_free;
|
---|
[8a49fed] | 337 |
|
---|
[38b7233] | 338 | size_t diff = old_size - new_size;
|
---|
| 339 | zones_to_free = diff / bs;
|
---|
[8a49fed] | 340 |
|
---|
[38b7233] | 341 | if (diff % bs != 0)
|
---|
| 342 | zones_to_free++;
|
---|
[8a49fed] | 343 |
|
---|
| 344 | uint32_t pos = old_size - 1;
|
---|
| 345 | unsigned i;
|
---|
| 346 | for (i = 0; i < zones_to_free; ++i, pos -= bs) {
|
---|
| 347 | uint32_t old_zone;
|
---|
| 348 |
|
---|
[3a5ee6c] | 349 | r = mfs_write_map(mnode, pos, 0, &old_zone);
|
---|
[c699b0c] | 350 | if (r != EOK)
|
---|
| 351 | goto exit_error;
|
---|
[8a49fed] | 352 |
|
---|
| 353 | ino_i->i_size -= bs;
|
---|
| 354 |
|
---|
| 355 | if (old_zone == 0)
|
---|
| 356 | continue; /*Sparse block*/
|
---|
| 357 |
|
---|
[70ac0af] | 358 | r = mfs_free_zone(mnode->instance, old_zone);
|
---|
[c699b0c] | 359 | if (r != EOK)
|
---|
| 360 | goto exit_error;
|
---|
[8a49fed] | 361 | }
|
---|
| 362 |
|
---|
| 363 | ino_i->i_size = new_size;
|
---|
[1878386] | 364 |
|
---|
[3a5ee6c] | 365 | return mfs_prune_ind_zones(mnode, new_size);
|
---|
[8a49fed] | 366 |
|
---|
| 367 | exit_error:
|
---|
| 368 | return r;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[6adba0a8] | 371 | /**
|
---|
| 372 | * @}
|
---|
[44c6091f] | 373 | */
|
---|
[6adba0a8] | 374 |
|
---|