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