[07a3253d] | 1 | /* |
---|
| 2 | * IMFS Device Node Handlers |
---|
| 3 | * |
---|
| 4 | * This file contains the set of handlers used to process operations on |
---|
| 5 | * IMFS memory file nodes. The memory files are created in memory using |
---|
| 6 | * malloc'ed memory. Thus any data stored in one of these files is lost |
---|
[a5305f6b] | 7 | * at system shutdown unless special arrangements to copy the data to |
---|
[07a3253d] | 8 | * some type of non-volailte storage are made by the application. |
---|
| 9 | * |
---|
[08311cc3] | 10 | * COPYRIGHT (c) 1989-1999. |
---|
[07a3253d] | 11 | * On-Line Applications Research Corporation (OAR). |
---|
| 12 | * |
---|
| 13 | * The license and distribution terms for this file may be |
---|
| 14 | * found in the file LICENSE in this distribution or at |
---|
[cf0539b1] | 15 | * http://www.rtems.com/license/LICENSE. |
---|
[07a3253d] | 16 | * |
---|
| 17 | * $Id$ |
---|
| 18 | */ |
---|
| 19 | |
---|
[d6b1d73] | 20 | #if HAVE_CONFIG_H |
---|
| 21 | #include "config.h" |
---|
| 22 | #endif |
---|
| 23 | |
---|
[07a3253d] | 24 | #include <stdlib.h> |
---|
[c873f40] | 25 | #include <string.h> |
---|
[07a3253d] | 26 | #include <assert.h> |
---|
| 27 | #include <errno.h> |
---|
| 28 | |
---|
| 29 | #include <rtems.h> |
---|
| 30 | #include <rtems/libio.h> |
---|
| 31 | #include "imfs.h" |
---|
[c058578] | 32 | #include <rtems/libio_.h> |
---|
[b2709481] | 33 | #include <rtems/seterr.h> |
---|
[07a3253d] | 34 | |
---|
[a5305f6b] | 35 | #define MEMFILE_STATIC |
---|
[07a3253d] | 36 | |
---|
| 37 | /* |
---|
[a5305f6b] | 38 | * Prototypes of private routines |
---|
[07a3253d] | 39 | */ |
---|
| 40 | |
---|
| 41 | MEMFILE_STATIC int IMFS_memfile_extend( |
---|
| 42 | IMFS_jnode_t *the_jnode, |
---|
| 43 | off_t new_length |
---|
| 44 | ); |
---|
| 45 | |
---|
| 46 | MEMFILE_STATIC int IMFS_memfile_addblock( |
---|
| 47 | IMFS_jnode_t *the_jnode, |
---|
| 48 | unsigned int block |
---|
| 49 | ); |
---|
| 50 | |
---|
| 51 | MEMFILE_STATIC int IMFS_memfile_remove_block( |
---|
| 52 | IMFS_jnode_t *the_jnode, |
---|
| 53 | unsigned int block |
---|
| 54 | ); |
---|
| 55 | |
---|
| 56 | MEMFILE_STATIC block_p *IMFS_memfile_get_block_pointer( |
---|
| 57 | IMFS_jnode_t *the_jnode, |
---|
| 58 | unsigned int block, |
---|
| 59 | int malloc_it |
---|
| 60 | ); |
---|
| 61 | |
---|
[d32359a7] | 62 | MEMFILE_STATIC ssize_t IMFS_memfile_read( |
---|
[07a3253d] | 63 | IMFS_jnode_t *the_jnode, |
---|
| 64 | off_t start, |
---|
| 65 | unsigned char *destination, |
---|
| 66 | unsigned int length |
---|
| 67 | ); |
---|
| 68 | |
---|
[d32359a7] | 69 | ssize_t IMFS_memfile_write( /* cannot be static as used in imfs_fchmod.c */ |
---|
[07a3253d] | 70 | IMFS_jnode_t *the_jnode, |
---|
| 71 | off_t start, |
---|
| 72 | const unsigned char *source, |
---|
| 73 | unsigned int length |
---|
| 74 | ); |
---|
| 75 | |
---|
[2b30225] | 76 | int memfile_check_rmnod( IMFS_jnode_t *the_jnode ); |
---|
| 77 | |
---|
[07a3253d] | 78 | void *memfile_alloc_block(void); |
---|
| 79 | |
---|
| 80 | void memfile_free_block( |
---|
| 81 | void *memory |
---|
| 82 | ); |
---|
| 83 | |
---|
| 84 | /* |
---|
| 85 | * memfile_open |
---|
| 86 | * |
---|
| 87 | * This routine processes the open() system call. Note that there is |
---|
| 88 | * nothing special to be done at open() time. |
---|
| 89 | */ |
---|
| 90 | |
---|
| 91 | int memfile_open( |
---|
| 92 | rtems_libio_t *iop, |
---|
| 93 | const char *pathname, |
---|
[f91bbe64] | 94 | uint32_t flag, |
---|
| 95 | uint32_t mode |
---|
[07a3253d] | 96 | ) |
---|
| 97 | { |
---|
| 98 | IMFS_jnode_t *the_jnode; |
---|
| 99 | |
---|
| 100 | the_jnode = iop->file_info; |
---|
| 101 | |
---|
[624867b] | 102 | /* |
---|
| 103 | * Perform 'copy on write' for linear files |
---|
| 104 | */ |
---|
| 105 | if ((iop->flags & (LIBIO_FLAGS_WRITE | LIBIO_FLAGS_APPEND)) |
---|
| 106 | && (the_jnode->type == IMFS_LINEAR_FILE)) { |
---|
| 107 | uint32_t count = the_jnode->info.linearfile.size; |
---|
| 108 | const unsigned char *buffer = the_jnode->info.linearfile.direct; |
---|
| 109 | |
---|
| 110 | the_jnode->type = IMFS_MEMORY_FILE; |
---|
| 111 | the_jnode->info.file.size = 0; |
---|
| 112 | the_jnode->info.file.indirect = 0; |
---|
| 113 | the_jnode->info.file.doubly_indirect = 0; |
---|
| 114 | the_jnode->info.file.triply_indirect = 0; |
---|
| 115 | if ((count != 0) |
---|
| 116 | && (IMFS_memfile_write(the_jnode, 0, buffer, count) == -1)) |
---|
| 117 | return -1; |
---|
| 118 | } |
---|
[e42a135] | 119 | if (iop->flags & LIBIO_FLAGS_APPEND) |
---|
| 120 | iop->offset = the_jnode->info.file.size; |
---|
| 121 | |
---|
[eb22ebe] | 122 | iop->size = the_jnode->info.file.size; |
---|
[07a3253d] | 123 | return 0; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /* |
---|
| 127 | * memfile_close |
---|
| 128 | * |
---|
| 129 | * This routine processes the close() system call. Note that there is |
---|
| 130 | * nothing to flush or memory to free at this point. |
---|
| 131 | */ |
---|
| 132 | |
---|
| 133 | int memfile_close( |
---|
| 134 | rtems_libio_t *iop |
---|
| 135 | ) |
---|
| 136 | { |
---|
| 137 | IMFS_jnode_t *the_jnode; |
---|
| 138 | |
---|
| 139 | the_jnode = iop->file_info; |
---|
| 140 | |
---|
[dd19c0b] | 141 | if (iop->flags & LIBIO_FLAGS_APPEND) |
---|
| 142 | iop->offset = the_jnode->info.file.size; |
---|
| 143 | |
---|
[2b30225] | 144 | memfile_check_rmnod( the_jnode ); |
---|
[07a3253d] | 145 | return 0; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /* |
---|
| 149 | * memfile_read |
---|
| 150 | * |
---|
| 151 | * This routine processes the read() system call. |
---|
| 152 | */ |
---|
| 153 | |
---|
[d32359a7] | 154 | ssize_t memfile_read( |
---|
[07a3253d] | 155 | rtems_libio_t *iop, |
---|
| 156 | void *buffer, |
---|
[7192476f] | 157 | size_t count |
---|
[07a3253d] | 158 | ) |
---|
| 159 | { |
---|
| 160 | IMFS_jnode_t *the_jnode; |
---|
| 161 | |
---|
| 162 | the_jnode = iop->file_info; |
---|
| 163 | |
---|
| 164 | return IMFS_memfile_read( the_jnode, iop->offset, buffer, count ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* |
---|
| 168 | * memfile_write |
---|
| 169 | * |
---|
| 170 | * This routine processes the write() system call. |
---|
| 171 | */ |
---|
| 172 | |
---|
[d32359a7] | 173 | ssize_t memfile_write( |
---|
[07a3253d] | 174 | rtems_libio_t *iop, |
---|
| 175 | const void *buffer, |
---|
[7192476f] | 176 | size_t count |
---|
[07a3253d] | 177 | ) |
---|
| 178 | { |
---|
| 179 | IMFS_jnode_t *the_jnode; |
---|
[d32359a7] | 180 | ssize_t status; |
---|
[07a3253d] | 181 | |
---|
| 182 | the_jnode = iop->file_info; |
---|
| 183 | |
---|
[efb1b830] | 184 | status = IMFS_memfile_write( the_jnode, iop->offset, buffer, count ); |
---|
| 185 | iop->size = the_jnode->info.file.size; |
---|
| 186 | |
---|
| 187 | return status; |
---|
[07a3253d] | 188 | } |
---|
| 189 | |
---|
| 190 | /* |
---|
| 191 | * memfile_ioctl |
---|
| 192 | * |
---|
| 193 | * This routine processes the ioctl() system call. |
---|
| 194 | * |
---|
| 195 | * NOTE: No ioctl()'s are supported for in-memory files. |
---|
| 196 | */ |
---|
| 197 | |
---|
| 198 | int memfile_ioctl( |
---|
| 199 | rtems_libio_t *iop, |
---|
[f91bbe64] | 200 | uint32_t command, |
---|
[07a3253d] | 201 | void *buffer |
---|
| 202 | ) |
---|
| 203 | { |
---|
| 204 | IMFS_jnode_t *the_jnode; |
---|
| 205 | |
---|
| 206 | the_jnode = iop->file_info; |
---|
| 207 | |
---|
| 208 | return 0; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /* |
---|
| 212 | * memfile_lseek |
---|
| 213 | * |
---|
| 214 | * This routine processes the lseek() system call. |
---|
| 215 | */ |
---|
| 216 | |
---|
| 217 | int memfile_lseek( |
---|
| 218 | rtems_libio_t *iop, |
---|
| 219 | off_t offset, |
---|
| 220 | int whence |
---|
| 221 | ) |
---|
| 222 | { |
---|
| 223 | IMFS_jnode_t *the_jnode; |
---|
| 224 | |
---|
| 225 | the_jnode = iop->file_info; |
---|
| 226 | |
---|
[d1941587] | 227 | if (the_jnode->type == IMFS_LINEAR_FILE) { |
---|
| 228 | if (iop->offset > the_jnode->info.linearfile.size) |
---|
| 229 | iop->offset = the_jnode->info.linearfile.size; |
---|
| 230 | } |
---|
| 231 | else { /* Must be a block file (IMFS_MEMORY_FILE). */ |
---|
| 232 | if (IMFS_memfile_extend( the_jnode, iop->offset )) |
---|
[b2709481] | 233 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
[07a3253d] | 234 | |
---|
[d1941587] | 235 | iop->size = the_jnode->info.file.size; |
---|
| 236 | } |
---|
[efb1b830] | 237 | return iop->offset; |
---|
[07a3253d] | 238 | } |
---|
| 239 | |
---|
| 240 | /* |
---|
| 241 | * memfile_stat |
---|
| 242 | * |
---|
| 243 | * This IMFS_stat() can be used. |
---|
| 244 | */ |
---|
| 245 | |
---|
| 246 | /* |
---|
| 247 | * memfile_ftruncate |
---|
| 248 | * |
---|
| 249 | * This routine processes the ftruncate() system call. |
---|
| 250 | */ |
---|
| 251 | |
---|
| 252 | int memfile_ftruncate( |
---|
| 253 | rtems_libio_t *iop, |
---|
| 254 | off_t length |
---|
| 255 | ) |
---|
| 256 | { |
---|
| 257 | IMFS_jnode_t *the_jnode; |
---|
| 258 | |
---|
| 259 | the_jnode = iop->file_info; |
---|
| 260 | |
---|
| 261 | /* |
---|
| 262 | * POSIX 1003.1b does not specify what happens if you truncate a file |
---|
| 263 | * and the new length is greater than the current size. We treat this |
---|
| 264 | * as an extend operation. |
---|
| 265 | */ |
---|
| 266 | |
---|
| 267 | if ( length > the_jnode->info.file.size ) |
---|
| 268 | return IMFS_memfile_extend( the_jnode, length ); |
---|
| 269 | |
---|
| 270 | /* |
---|
| 271 | * The in-memory files do not currently reclaim memory until the file is |
---|
| 272 | * deleted. So we leave the previously allocated blocks in place for |
---|
| 273 | * future use and just set the length. |
---|
| 274 | */ |
---|
| 275 | |
---|
| 276 | the_jnode->info.file.size = length; |
---|
[efb1b830] | 277 | iop->size = the_jnode->info.file.size; |
---|
[07a3253d] | 278 | |
---|
| 279 | IMFS_update_atime( the_jnode ); |
---|
| 280 | |
---|
| 281 | return 0; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /* |
---|
| 285 | * IMFS_memfile_extend |
---|
| 286 | * |
---|
| 287 | * This routine insures that the in-memory file is of the length |
---|
[a5305f6b] | 288 | * specified. If necessary, it will allocate memory blocks to |
---|
[07a3253d] | 289 | * extend the file. |
---|
| 290 | */ |
---|
| 291 | |
---|
| 292 | MEMFILE_STATIC int IMFS_memfile_extend( |
---|
| 293 | IMFS_jnode_t *the_jnode, |
---|
| 294 | off_t new_length |
---|
| 295 | ) |
---|
| 296 | { |
---|
| 297 | unsigned int block; |
---|
| 298 | unsigned int new_blocks; |
---|
| 299 | unsigned int old_blocks; |
---|
| 300 | |
---|
| 301 | /* |
---|
| 302 | * Perform internal consistency checks |
---|
| 303 | */ |
---|
| 304 | |
---|
| 305 | assert( the_jnode ); |
---|
| 306 | if ( !the_jnode ) |
---|
[b2709481] | 307 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 308 | |
---|
| 309 | assert( the_jnode->type == IMFS_MEMORY_FILE ); |
---|
| 310 | if ( the_jnode->type != IMFS_MEMORY_FILE ) |
---|
[b2709481] | 311 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 312 | |
---|
| 313 | if ( new_length >= IMFS_MEMFILE_MAXIMUM_SIZE ) |
---|
[b2709481] | 314 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
[07a3253d] | 315 | |
---|
| 316 | if ( new_length <= the_jnode->info.file.size ) |
---|
| 317 | return 0; |
---|
| 318 | |
---|
| 319 | /* |
---|
| 320 | * Calculate the number of range of blocks to allocate |
---|
| 321 | */ |
---|
| 322 | |
---|
| 323 | new_blocks = new_length / IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 324 | old_blocks = the_jnode->info.file.size / IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 325 | |
---|
| 326 | /* |
---|
| 327 | * Now allocate each of those blocks. |
---|
| 328 | */ |
---|
| 329 | |
---|
| 330 | for ( block=old_blocks ; block<=new_blocks ; block++ ) { |
---|
| 331 | if ( IMFS_memfile_addblock( the_jnode, block ) ) { |
---|
| 332 | for ( ; block>=old_blocks ; block-- ) { |
---|
| 333 | IMFS_memfile_remove_block( the_jnode, block ); |
---|
| 334 | } |
---|
[b2709481] | 335 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
[07a3253d] | 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | /* |
---|
| 340 | * Set the new length of the file. |
---|
| 341 | */ |
---|
| 342 | |
---|
| 343 | the_jnode->info.file.size = new_length; |
---|
| 344 | return 0; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /* |
---|
| 348 | * IMFS_memfile_addblock |
---|
| 349 | * |
---|
| 350 | * This routine adds a single block to the specified in-memory file. |
---|
| 351 | */ |
---|
| 352 | |
---|
| 353 | MEMFILE_STATIC int IMFS_memfile_addblock( |
---|
| 354 | IMFS_jnode_t *the_jnode, |
---|
| 355 | unsigned int block |
---|
| 356 | ) |
---|
| 357 | { |
---|
| 358 | block_p memory; |
---|
| 359 | block_p *block_entry_ptr; |
---|
| 360 | |
---|
| 361 | assert( the_jnode ); |
---|
| 362 | if ( !the_jnode ) |
---|
[b2709481] | 363 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 364 | |
---|
| 365 | assert( the_jnode->type == IMFS_MEMORY_FILE ); |
---|
| 366 | if ( the_jnode->type != IMFS_MEMORY_FILE ) |
---|
[b2709481] | 367 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 368 | |
---|
| 369 | block_entry_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 1 ); |
---|
| 370 | if ( *block_entry_ptr ) |
---|
| 371 | return 0; |
---|
| 372 | |
---|
| 373 | #if 0 |
---|
[3b2c473] | 374 | fprintf(stdout, "%d %p", block, block_entry_ptr ); |
---|
[07a3253d] | 375 | fflush(stdout); |
---|
| 376 | #endif |
---|
| 377 | |
---|
| 378 | memory = memfile_alloc_block(); |
---|
| 379 | if ( !memory ) |
---|
| 380 | return 1; |
---|
| 381 | *block_entry_ptr = memory; |
---|
| 382 | |
---|
| 383 | return 0; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | /* |
---|
| 387 | * IMFS_memfile_remove_block |
---|
| 388 | * |
---|
| 389 | * This routine removes the specified block from the in-memory file. |
---|
| 390 | * |
---|
| 391 | * NOTE: This is a support routine and is called only to remove |
---|
| 392 | * the last block or set of blocks in a file. Removing a |
---|
| 393 | * block from the middle of a file would be exceptionally |
---|
| 394 | * dangerous and the results unpredictable. |
---|
| 395 | */ |
---|
| 396 | |
---|
| 397 | MEMFILE_STATIC int IMFS_memfile_remove_block( |
---|
| 398 | IMFS_jnode_t *the_jnode, |
---|
| 399 | unsigned int block |
---|
| 400 | ) |
---|
| 401 | { |
---|
| 402 | block_p *block_entry_ptr; |
---|
| 403 | block_p ptr; |
---|
| 404 | |
---|
| 405 | block_entry_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 406 | ptr = *block_entry_ptr; |
---|
| 407 | *block_entry_ptr = 0; |
---|
| 408 | |
---|
| 409 | memfile_free_block( ptr ); |
---|
| 410 | |
---|
| 411 | return 1; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | /* |
---|
| 415 | * memfile_free_blocks_in_table |
---|
| 416 | * |
---|
| 417 | * This is a support routine for IMFS_memfile_remove. It frees all the |
---|
| 418 | * blocks in one of the indirection tables. |
---|
| 419 | */ |
---|
| 420 | |
---|
| 421 | void memfile_free_blocks_in_table( |
---|
| 422 | block_p **block_table, |
---|
| 423 | int entries |
---|
| 424 | ) |
---|
| 425 | { |
---|
| 426 | int i; |
---|
| 427 | block_p *b; |
---|
| 428 | |
---|
| 429 | /* |
---|
| 430 | * Perform internal consistency checks |
---|
| 431 | */ |
---|
| 432 | |
---|
| 433 | assert( block_table ); |
---|
| 434 | if ( !block_table ) |
---|
| 435 | return; |
---|
| 436 | |
---|
| 437 | /* |
---|
| 438 | * Now go through all the slots in the table and free the memory. |
---|
| 439 | */ |
---|
| 440 | |
---|
| 441 | b = *block_table; |
---|
| 442 | |
---|
| 443 | for ( i=0 ; i<entries ; i++ ) { |
---|
| 444 | if ( b[i] ) { |
---|
| 445 | memfile_free_block( b[i] ); |
---|
| 446 | b[i] = 0; |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | /* |
---|
[a5305f6b] | 451 | * Now that all the blocks in the block table are free, we can |
---|
[07a3253d] | 452 | * free the block table itself. |
---|
| 453 | */ |
---|
| 454 | |
---|
| 455 | memfile_free_block( *block_table ); |
---|
| 456 | *block_table = 0; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | /* |
---|
| 460 | * IMFS_memfile_remove |
---|
| 461 | * |
---|
| 462 | * This routine frees all memory associated with an in memory file. |
---|
| 463 | * |
---|
[a5305f6b] | 464 | * NOTE: This is an exceptionally conservative implementation. |
---|
[07a3253d] | 465 | * It will check EVERY pointer which is non-NULL and insure |
---|
| 466 | * any child non-NULL pointers are freed. Optimistically, all that |
---|
| 467 | * is necessary is to scan until a NULL pointer is found. There |
---|
| 468 | * should be no allocated data past that point. |
---|
| 469 | * |
---|
| 470 | * In experimentation on the powerpc simulator, it was noted |
---|
| 471 | * that using blocks which held 128 slots versus 16 slots made |
---|
| 472 | * a significant difference in the performance of this routine. |
---|
| 473 | * |
---|
| 474 | * Regardless until the IMFS implementation is proven, it |
---|
| 475 | * is better to stick to simple, easy to understand algorithms. |
---|
| 476 | */ |
---|
| 477 | |
---|
| 478 | int IMFS_memfile_remove( |
---|
| 479 | IMFS_jnode_t *the_jnode |
---|
| 480 | ) |
---|
| 481 | { |
---|
| 482 | IMFS_memfile_t *info; |
---|
| 483 | int i; |
---|
| 484 | int j; |
---|
| 485 | unsigned int to_free; |
---|
| 486 | block_p *p; |
---|
| 487 | |
---|
| 488 | /* |
---|
| 489 | * Perform internal consistency checks |
---|
| 490 | */ |
---|
| 491 | |
---|
| 492 | assert( the_jnode ); |
---|
| 493 | if ( !the_jnode ) |
---|
[b2709481] | 494 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 495 | |
---|
| 496 | assert( the_jnode->type == IMFS_MEMORY_FILE ); |
---|
| 497 | if ( the_jnode->type != IMFS_MEMORY_FILE ) |
---|
[b2709481] | 498 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 499 | |
---|
| 500 | /* |
---|
| 501 | * Eventually this could be set smarter at each call to |
---|
| 502 | * memfile_free_blocks_in_table to greatly speed this up. |
---|
| 503 | */ |
---|
| 504 | |
---|
| 505 | to_free = IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 506 | |
---|
| 507 | /* |
---|
| 508 | * Now start freeing blocks in this order: |
---|
| 509 | * + indirect |
---|
| 510 | * + doubly indirect |
---|
| 511 | * + triply indirect |
---|
| 512 | */ |
---|
| 513 | |
---|
| 514 | info = &the_jnode->info.file; |
---|
| 515 | |
---|
| 516 | if ( info->indirect ) { |
---|
| 517 | memfile_free_blocks_in_table( &info->indirect, to_free ); |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | if ( info->doubly_indirect ) { |
---|
| 521 | |
---|
| 522 | for ( i=0 ; i<IMFS_MEMFILE_BLOCK_SLOTS ; i++ ) { |
---|
| 523 | if ( info->doubly_indirect[i] ) { |
---|
| 524 | memfile_free_blocks_in_table( |
---|
| 525 | (block_p **)&info->doubly_indirect[i], to_free ); |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | memfile_free_blocks_in_table( &info->doubly_indirect, to_free ); |
---|
| 529 | |
---|
| 530 | } |
---|
[a5305f6b] | 531 | |
---|
[07a3253d] | 532 | if ( info->triply_indirect ) { |
---|
| 533 | for ( i=0 ; i<IMFS_MEMFILE_BLOCK_SLOTS ; i++ ) { |
---|
| 534 | p = (block_p *) info->triply_indirect[i]; |
---|
[d79ef17] | 535 | if ( !p ) /* ensure we have a valid pointer */ |
---|
| 536 | break; |
---|
[07a3253d] | 537 | for ( j=0 ; j<IMFS_MEMFILE_BLOCK_SLOTS ; j++ ) { |
---|
| 538 | if ( p[j] ) { |
---|
| 539 | memfile_free_blocks_in_table( (block_p **)&p[j], to_free); |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | memfile_free_blocks_in_table( |
---|
| 543 | (block_p **)&info->triply_indirect[i], to_free ); |
---|
| 544 | } |
---|
| 545 | memfile_free_blocks_in_table( |
---|
| 546 | (block_p **)&info->triply_indirect, to_free ); |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | return 0; |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | /* |
---|
| 553 | * IMFS_memfile_read |
---|
| 554 | * |
---|
| 555 | * This routine read from memory file pointed to by the_jnode into |
---|
| 556 | * the specified data buffer specified by destination. The file |
---|
| 557 | * is NOT extended. An offset greater than the length of the file |
---|
| 558 | * is considered an error. Read from an offset for more bytes than |
---|
[a5305f6b] | 559 | * are between the offset and the end of the file will result in |
---|
[07a3253d] | 560 | * reading the data between offset and the end of the file (truncated |
---|
| 561 | * read). |
---|
| 562 | */ |
---|
| 563 | |
---|
[d32359a7] | 564 | MEMFILE_STATIC ssize_t IMFS_memfile_read( |
---|
[07a3253d] | 565 | IMFS_jnode_t *the_jnode, |
---|
| 566 | off_t start, |
---|
| 567 | unsigned char *destination, |
---|
| 568 | unsigned int length |
---|
| 569 | ) |
---|
| 570 | { |
---|
| 571 | block_p *block_ptr; |
---|
| 572 | unsigned int block; |
---|
| 573 | unsigned int my_length; |
---|
| 574 | unsigned int to_copy = 0; |
---|
| 575 | unsigned int last_byte; |
---|
| 576 | unsigned int copied; |
---|
| 577 | unsigned int start_offset; |
---|
| 578 | unsigned char *dest; |
---|
| 579 | |
---|
| 580 | dest = destination; |
---|
| 581 | |
---|
| 582 | /* |
---|
| 583 | * Perform internal consistency checks |
---|
| 584 | */ |
---|
| 585 | |
---|
| 586 | assert( the_jnode ); |
---|
| 587 | if ( !the_jnode ) |
---|
[b2709481] | 588 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 589 | |
---|
[d1941587] | 590 | assert( the_jnode->type == IMFS_MEMORY_FILE || |
---|
| 591 | the_jnode->type == IMFS_LINEAR_FILE ); |
---|
| 592 | if ( the_jnode->type != IMFS_MEMORY_FILE && |
---|
| 593 | the_jnode->type != IMFS_LINEAR_FILE ) |
---|
[b2709481] | 594 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 595 | |
---|
| 596 | /* |
---|
| 597 | * Error checks on arguments |
---|
| 598 | */ |
---|
| 599 | |
---|
| 600 | assert( dest ); |
---|
| 601 | if ( !dest ) |
---|
[b2709481] | 602 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
[07a3253d] | 603 | |
---|
| 604 | /* |
---|
| 605 | * If there is nothing to read, then quick exit. |
---|
| 606 | */ |
---|
| 607 | |
---|
| 608 | my_length = length; |
---|
| 609 | if ( !my_length ) |
---|
[b2709481] | 610 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
[07a3253d] | 611 | |
---|
[d1941587] | 612 | /* |
---|
| 613 | * Linear files (as created from a tar file are easier to handle |
---|
| 614 | * than block files). |
---|
| 615 | */ |
---|
| 616 | if (the_jnode->type == IMFS_LINEAR_FILE) { |
---|
| 617 | unsigned char *file_ptr; |
---|
| 618 | |
---|
| 619 | file_ptr = (unsigned char *)the_jnode->info.linearfile.direct; |
---|
| 620 | |
---|
| 621 | if (my_length > (the_jnode->info.linearfile.size - start)) |
---|
| 622 | my_length = the_jnode->info.linearfile.size - start; |
---|
| 623 | |
---|
| 624 | memcpy(dest, &file_ptr[start], my_length); |
---|
| 625 | |
---|
| 626 | IMFS_update_atime( the_jnode ); |
---|
| 627 | |
---|
| 628 | return my_length; |
---|
| 629 | } |
---|
| 630 | |
---|
[07a3253d] | 631 | /* |
---|
| 632 | * If the last byte we are supposed to read is past the end of this |
---|
| 633 | * in memory file, then shorten the length to read. |
---|
| 634 | */ |
---|
| 635 | |
---|
| 636 | last_byte = start + length; |
---|
| 637 | if ( last_byte > the_jnode->info.file.size ) |
---|
| 638 | my_length = the_jnode->info.file.size - start; |
---|
| 639 | |
---|
| 640 | copied = 0; |
---|
| 641 | |
---|
| 642 | /* |
---|
| 643 | * Three phases to the read: |
---|
| 644 | * + possibly the last part of one block |
---|
| 645 | * + all of zero of more blocks |
---|
| 646 | * + possibly the first part of one block |
---|
| 647 | */ |
---|
| 648 | |
---|
| 649 | /* |
---|
| 650 | * Phase 1: possibly the last part of one block |
---|
| 651 | */ |
---|
| 652 | |
---|
| 653 | start_offset = start % IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 654 | block = start / IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 655 | if ( start_offset ) { |
---|
| 656 | to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK - start_offset; |
---|
| 657 | if ( to_copy > my_length ) |
---|
| 658 | to_copy = my_length; |
---|
| 659 | block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 660 | assert( block_ptr ); |
---|
| 661 | if ( !block_ptr ) |
---|
| 662 | return copied; |
---|
| 663 | memcpy( dest, &(*block_ptr)[ start_offset ], to_copy ); |
---|
| 664 | dest += to_copy; |
---|
| 665 | block++; |
---|
| 666 | my_length -= to_copy; |
---|
| 667 | copied += to_copy; |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | /* |
---|
| 671 | * Phase 2: all of zero of more blocks |
---|
| 672 | */ |
---|
| 673 | |
---|
| 674 | to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 675 | while ( my_length >= IMFS_MEMFILE_BYTES_PER_BLOCK ) { |
---|
| 676 | block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 677 | assert( block_ptr ); |
---|
| 678 | if ( !block_ptr ) |
---|
| 679 | return copied; |
---|
| 680 | memcpy( dest, &(*block_ptr)[ 0 ], to_copy ); |
---|
| 681 | dest += to_copy; |
---|
| 682 | block++; |
---|
| 683 | my_length -= to_copy; |
---|
| 684 | copied += to_copy; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | /* |
---|
| 688 | * Phase 3: possibly the first part of one block |
---|
| 689 | */ |
---|
| 690 | |
---|
| 691 | assert( my_length < IMFS_MEMFILE_BYTES_PER_BLOCK ); |
---|
| 692 | |
---|
| 693 | if ( my_length ) { |
---|
| 694 | block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 695 | assert( block_ptr ); |
---|
| 696 | if ( !block_ptr ) |
---|
| 697 | return copied; |
---|
| 698 | memcpy( dest, &(*block_ptr)[ 0 ], my_length ); |
---|
| 699 | copied += my_length; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | IMFS_update_atime( the_jnode ); |
---|
| 703 | |
---|
| 704 | return copied; |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | /* |
---|
| 708 | * IMFS_memfile_write |
---|
| 709 | * |
---|
| 710 | * This routine writes the specified data buffer into the in memory |
---|
| 711 | * file pointed to by the_jnode. The file is extended as needed. |
---|
| 712 | */ |
---|
| 713 | |
---|
[d32359a7] | 714 | MEMFILE_STATIC ssize_t IMFS_memfile_write( |
---|
[07a3253d] | 715 | IMFS_jnode_t *the_jnode, |
---|
| 716 | off_t start, |
---|
| 717 | const unsigned char *source, |
---|
| 718 | unsigned int length |
---|
| 719 | ) |
---|
| 720 | { |
---|
| 721 | block_p *block_ptr; |
---|
| 722 | unsigned int block; |
---|
| 723 | int status; |
---|
| 724 | unsigned int my_length; |
---|
| 725 | unsigned int to_copy = 0; |
---|
| 726 | unsigned int last_byte; |
---|
| 727 | unsigned int start_offset; |
---|
| 728 | int copied; |
---|
| 729 | const unsigned char *src; |
---|
| 730 | |
---|
| 731 | src = source; |
---|
| 732 | |
---|
| 733 | /* |
---|
| 734 | * Perform internal consistency checks |
---|
| 735 | */ |
---|
| 736 | |
---|
| 737 | assert( the_jnode ); |
---|
| 738 | if ( !the_jnode ) |
---|
[b2709481] | 739 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 740 | |
---|
| 741 | assert( the_jnode->type == IMFS_MEMORY_FILE ); |
---|
| 742 | if ( the_jnode->type != IMFS_MEMORY_FILE ) |
---|
[b2709481] | 743 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[07a3253d] | 744 | |
---|
| 745 | /* |
---|
| 746 | * Error check arguments |
---|
| 747 | */ |
---|
| 748 | |
---|
| 749 | assert( source ); |
---|
| 750 | if ( !source ) |
---|
[b2709481] | 751 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
[07a3253d] | 752 | |
---|
| 753 | |
---|
| 754 | /* |
---|
| 755 | * If there is nothing to write, then quick exit. |
---|
| 756 | */ |
---|
| 757 | |
---|
| 758 | my_length = length; |
---|
| 759 | if ( !my_length ) |
---|
[b2709481] | 760 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
[07a3253d] | 761 | |
---|
| 762 | /* |
---|
| 763 | * If the last byte we are supposed to write is past the end of this |
---|
| 764 | * in memory file, then extend the length. |
---|
| 765 | */ |
---|
| 766 | |
---|
| 767 | last_byte = start + length; |
---|
| 768 | if ( last_byte > the_jnode->info.file.size ) { |
---|
| 769 | status = IMFS_memfile_extend( the_jnode, last_byte ); |
---|
| 770 | if ( status ) |
---|
[b2709481] | 771 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
[07a3253d] | 772 | } |
---|
| 773 | |
---|
| 774 | copied = 0; |
---|
| 775 | |
---|
| 776 | /* |
---|
| 777 | * Three phases to the write: |
---|
| 778 | * + possibly the last part of one block |
---|
| 779 | * + all of zero of more blocks |
---|
| 780 | * + possibly the first part of one block |
---|
| 781 | */ |
---|
| 782 | |
---|
| 783 | /* |
---|
| 784 | * Phase 1: possibly the last part of one block |
---|
| 785 | */ |
---|
| 786 | |
---|
| 787 | start_offset = start % IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 788 | block = start / IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 789 | if ( start_offset ) { |
---|
| 790 | to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK - start_offset; |
---|
| 791 | if ( to_copy > my_length ) |
---|
| 792 | to_copy = my_length; |
---|
| 793 | block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 794 | assert( block_ptr ); |
---|
| 795 | if ( !block_ptr ) |
---|
| 796 | return copied; |
---|
[b10f6e1] | 797 | #if 0 |
---|
[3b2c473] | 798 | fprintf(stdout, "write %d at %d in %d: %*s\n", to_copy, start_offset, block, to_copy, src ); |
---|
[b10f6e1] | 799 | #endif |
---|
[07a3253d] | 800 | memcpy( &(*block_ptr)[ start_offset ], src, to_copy ); |
---|
| 801 | src += to_copy; |
---|
| 802 | block++; |
---|
| 803 | my_length -= to_copy; |
---|
| 804 | copied += to_copy; |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | /* |
---|
| 808 | * Phase 2: all of zero of more blocks |
---|
| 809 | */ |
---|
| 810 | |
---|
| 811 | to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK; |
---|
| 812 | while ( my_length >= IMFS_MEMFILE_BYTES_PER_BLOCK ) { |
---|
| 813 | block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 814 | assert( block_ptr ); |
---|
| 815 | if ( !block_ptr ) |
---|
| 816 | return copied; |
---|
[b10f6e1] | 817 | #if 0 |
---|
[3b2c473] | 818 | fprintf(stdout, "write %d in %d: %*s\n", to_copy, block, to_copy, src ); |
---|
[b10f6e1] | 819 | #endif |
---|
[07a3253d] | 820 | memcpy( &(*block_ptr)[ 0 ], src, to_copy ); |
---|
| 821 | src += to_copy; |
---|
| 822 | block++; |
---|
| 823 | my_length -= to_copy; |
---|
| 824 | copied += to_copy; |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | /* |
---|
| 828 | * Phase 3: possibly the first part of one block |
---|
| 829 | */ |
---|
| 830 | |
---|
| 831 | assert( my_length < IMFS_MEMFILE_BYTES_PER_BLOCK ); |
---|
| 832 | |
---|
| 833 | to_copy = my_length; |
---|
| 834 | if ( my_length ) { |
---|
| 835 | block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 ); |
---|
| 836 | assert( block_ptr ); |
---|
| 837 | if ( !block_ptr ) |
---|
| 838 | return copied; |
---|
[b10f6e1] | 839 | #if 0 |
---|
[3b2c473] | 840 | fprintf(stdout, "write %d in %d: %*s\n", to_copy, block, to_copy, src ); |
---|
[b10f6e1] | 841 | #endif |
---|
[07a3253d] | 842 | memcpy( &(*block_ptr)[ 0 ], src, my_length ); |
---|
| 843 | my_length = 0; |
---|
| 844 | copied += to_copy; |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | IMFS_atime_mtime_update( the_jnode ); |
---|
| 848 | |
---|
| 849 | return copied; |
---|
| 850 | } |
---|
| 851 | |
---|
| 852 | /* |
---|
| 853 | * IMFS_memfile_get_block_pointer |
---|
| 854 | * |
---|
| 855 | * This routine looks up the block pointer associated with the given block |
---|
| 856 | * number. If that block has not been allocated and "malloc_it" is |
---|
| 857 | * TRUE, then the block is allocated. Otherwise, it is an error. |
---|
| 858 | */ |
---|
| 859 | |
---|
[b10f6e1] | 860 | #if 0 |
---|
| 861 | block_p *IMFS_memfile_get_block_pointer_DEBUG( |
---|
| 862 | IMFS_jnode_t *the_jnode, |
---|
| 863 | unsigned int block, |
---|
| 864 | int malloc_it |
---|
| 865 | ); |
---|
| 866 | |
---|
[07a3253d] | 867 | block_p *IMFS_memfile_get_block_pointer( |
---|
| 868 | IMFS_jnode_t *the_jnode, |
---|
| 869 | unsigned int block, |
---|
| 870 | int malloc_it |
---|
| 871 | ) |
---|
[b10f6e1] | 872 | { |
---|
| 873 | block_p *p; |
---|
| 874 | |
---|
| 875 | p = IMFS_memfile_get_block_pointer_DEBUG( the_jnode, block, malloc_it ); |
---|
[3b2c473] | 876 | fprintf(stdout, "(%d -> %p) ", block, p ); |
---|
[b10f6e1] | 877 | return p; |
---|
| 878 | } |
---|
| 879 | |
---|
| 880 | block_p *IMFS_memfile_get_block_pointer_DEBUG( |
---|
| 881 | #else |
---|
| 882 | block_p *IMFS_memfile_get_block_pointer( |
---|
| 883 | #endif |
---|
| 884 | IMFS_jnode_t *the_jnode, |
---|
| 885 | unsigned int block, |
---|
| 886 | int malloc_it |
---|
| 887 | ) |
---|
[07a3253d] | 888 | { |
---|
| 889 | unsigned int my_block; |
---|
| 890 | IMFS_memfile_t *info; |
---|
| 891 | unsigned int singly; |
---|
| 892 | unsigned int doubly; |
---|
| 893 | unsigned int triply; |
---|
| 894 | block_p *p; |
---|
| 895 | block_p *p1; |
---|
| 896 | block_p *p2; |
---|
| 897 | |
---|
| 898 | /* |
---|
| 899 | * Perform internal consistency checks |
---|
| 900 | */ |
---|
| 901 | |
---|
| 902 | assert( the_jnode ); |
---|
| 903 | if ( !the_jnode ) |
---|
| 904 | return NULL; |
---|
| 905 | |
---|
| 906 | assert( the_jnode->type == IMFS_MEMORY_FILE ); |
---|
| 907 | if ( the_jnode->type != IMFS_MEMORY_FILE ) |
---|
| 908 | return NULL; |
---|
| 909 | |
---|
| 910 | info = &the_jnode->info.file; |
---|
| 911 | |
---|
| 912 | my_block = block; |
---|
| 913 | |
---|
| 914 | /* |
---|
| 915 | * Is the block number in the simple indirect portion? |
---|
| 916 | */ |
---|
| 917 | |
---|
| 918 | if ( my_block <= LAST_INDIRECT ) { |
---|
| 919 | #if 0 |
---|
[3b2c473] | 920 | fprintf(stdout, "(s %d) ", block ); |
---|
[07a3253d] | 921 | fflush(stdout); |
---|
| 922 | #endif |
---|
| 923 | p = info->indirect; |
---|
| 924 | |
---|
| 925 | if ( malloc_it ) { |
---|
| 926 | |
---|
| 927 | if ( !p ) { |
---|
| 928 | p = memfile_alloc_block(); |
---|
| 929 | if ( !p ) |
---|
| 930 | return 0; |
---|
| 931 | info->indirect = p; |
---|
| 932 | } |
---|
| 933 | return &info->indirect[ my_block ]; |
---|
| 934 | } |
---|
| 935 | |
---|
| 936 | if ( !p ) |
---|
| 937 | return 0; |
---|
| 938 | |
---|
| 939 | return &info->indirect[ my_block ]; |
---|
| 940 | } |
---|
| 941 | |
---|
| 942 | /* |
---|
| 943 | * Is the block number in the doubly indirect portion? |
---|
| 944 | */ |
---|
| 945 | |
---|
| 946 | if ( my_block <= LAST_DOUBLY_INDIRECT ) { |
---|
| 947 | #if 0 |
---|
[3b2c473] | 948 | fprintf(stdout, "(d %d) ", block ); |
---|
[07a3253d] | 949 | fflush(stdout); |
---|
| 950 | #endif |
---|
| 951 | |
---|
| 952 | my_block -= FIRST_DOUBLY_INDIRECT; |
---|
| 953 | |
---|
| 954 | singly = my_block % IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 955 | doubly = my_block / IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 956 | |
---|
| 957 | p = info->doubly_indirect; |
---|
| 958 | if ( malloc_it ) { |
---|
| 959 | |
---|
| 960 | if ( !p ) { |
---|
| 961 | p = memfile_alloc_block(); |
---|
| 962 | if ( !p ) |
---|
| 963 | return 0; |
---|
| 964 | info->doubly_indirect = p; |
---|
| 965 | } |
---|
| 966 | |
---|
| 967 | p1 = (block_p *)p[ doubly ]; |
---|
| 968 | if ( !p1 ) { |
---|
| 969 | p1 = memfile_alloc_block(); |
---|
| 970 | if ( !p1 ) |
---|
| 971 | return 0; |
---|
| 972 | p[ doubly ] = (block_p) p1; |
---|
| 973 | } |
---|
| 974 | |
---|
[b10f6e1] | 975 | return (block_p *)&p1[ singly ]; |
---|
[07a3253d] | 976 | } |
---|
| 977 | |
---|
| 978 | if ( !p ) |
---|
| 979 | return 0; |
---|
| 980 | |
---|
| 981 | p = (block_p *)p[ doubly ]; |
---|
[a5305f6b] | 982 | if ( !p ) |
---|
[07a3253d] | 983 | return 0; |
---|
| 984 | |
---|
| 985 | #if 0 |
---|
[3b2c473] | 986 | fprintf(stdout, "(d %d %d %d %d %p %p) ", block, my_block, doubly, |
---|
[07a3253d] | 987 | singly, p, &p[singly] ); |
---|
| 988 | fflush(stdout); |
---|
| 989 | #endif |
---|
| 990 | return (block_p *)&p[ singly ]; |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | #if 0 |
---|
[3b2c473] | 994 | fprintf(stdout, "(t %d) ", block ); |
---|
[07a3253d] | 995 | fflush(stdout); |
---|
| 996 | #endif |
---|
| 997 | /* |
---|
| 998 | * Is the block number in the triply indirect portion? |
---|
| 999 | */ |
---|
| 1000 | |
---|
| 1001 | if ( my_block <= LAST_TRIPLY_INDIRECT ) { |
---|
| 1002 | my_block -= FIRST_TRIPLY_INDIRECT; |
---|
| 1003 | |
---|
| 1004 | singly = my_block % IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 1005 | doubly = my_block / IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 1006 | triply = doubly / IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 1007 | doubly %= IMFS_MEMFILE_BLOCK_SLOTS; |
---|
| 1008 | |
---|
| 1009 | p = info->triply_indirect; |
---|
| 1010 | |
---|
| 1011 | if ( malloc_it ) { |
---|
| 1012 | if ( !p ) { |
---|
| 1013 | p = memfile_alloc_block(); |
---|
| 1014 | if ( !p ) |
---|
| 1015 | return 0; |
---|
| 1016 | info->triply_indirect = p; |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | p1 = (block_p *) p[ triply ]; |
---|
| 1020 | if ( !p1 ) { |
---|
| 1021 | p1 = memfile_alloc_block(); |
---|
| 1022 | if ( !p1 ) |
---|
| 1023 | return 0; |
---|
| 1024 | p[ triply ] = (block_p) p1; |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | p2 = (block_p *)p1[ doubly ]; |
---|
| 1028 | if ( !p2 ) { |
---|
| 1029 | p2 = memfile_alloc_block(); |
---|
| 1030 | if ( !p2 ) |
---|
| 1031 | return 0; |
---|
| 1032 | p1[ doubly ] = (block_p) p2; |
---|
| 1033 | } |
---|
| 1034 | return (block_p *)&p2[ singly ]; |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | if ( !p ) |
---|
| 1038 | return 0; |
---|
| 1039 | |
---|
| 1040 | #if 0 |
---|
[3b2c473] | 1041 | fprintf(stdout, "(t %d %d %d %d %d) ", block, my_block, triply, doubly, singly ); |
---|
[07a3253d] | 1042 | fflush(stdout); |
---|
| 1043 | #endif |
---|
| 1044 | p1 = (block_p *) p[ triply ]; |
---|
| 1045 | if ( !p1 ) |
---|
| 1046 | return 0; |
---|
| 1047 | |
---|
| 1048 | p2 = (block_p *)p1[ doubly ]; |
---|
| 1049 | if ( !p ) |
---|
| 1050 | return 0; |
---|
| 1051 | |
---|
| 1052 | return (block_p *)&p2[ singly ]; |
---|
| 1053 | } |
---|
| 1054 | |
---|
| 1055 | /* |
---|
| 1056 | * This means the requested block number is out of range. |
---|
| 1057 | */ |
---|
| 1058 | |
---|
| 1059 | return 0; |
---|
| 1060 | } |
---|
| 1061 | |
---|
| 1062 | /* |
---|
| 1063 | * memfile_alloc_block |
---|
| 1064 | * |
---|
| 1065 | * Allocate a block for an in-memory file. |
---|
| 1066 | */ |
---|
| 1067 | |
---|
| 1068 | int memfile_blocks_allocated = 0; |
---|
| 1069 | |
---|
| 1070 | void *memfile_alloc_block(void) |
---|
| 1071 | { |
---|
| 1072 | void *memory; |
---|
| 1073 | |
---|
| 1074 | memory = (void *)calloc(1, IMFS_MEMFILE_BYTES_PER_BLOCK); |
---|
| 1075 | if ( memory ) |
---|
| 1076 | memfile_blocks_allocated++; |
---|
| 1077 | |
---|
| 1078 | return memory; |
---|
| 1079 | } |
---|
| 1080 | |
---|
| 1081 | /* |
---|
[b10f6e1] | 1082 | * memfile_free_block |
---|
[07a3253d] | 1083 | * |
---|
| 1084 | * Free a block from an in-memory file. |
---|
| 1085 | */ |
---|
| 1086 | |
---|
| 1087 | void memfile_free_block( |
---|
| 1088 | void *memory |
---|
| 1089 | ) |
---|
| 1090 | { |
---|
| 1091 | #if 0 |
---|
[3b2c473] | 1092 | fprintf(stdout, "(d %p) ", memory ); |
---|
[07a3253d] | 1093 | fflush(stdout); |
---|
| 1094 | #endif |
---|
| 1095 | free(memory); |
---|
| 1096 | memfile_blocks_allocated--; |
---|
| 1097 | } |
---|
| 1098 | |
---|
[b568ccb] | 1099 | |
---|
| 1100 | /* |
---|
| 1101 | * memfile_rmnod |
---|
| 1102 | * |
---|
[a5305f6b] | 1103 | * This routine is available from the optable to remove a node |
---|
[b568ccb] | 1104 | * from the IMFS file system. |
---|
| 1105 | */ |
---|
| 1106 | |
---|
| 1107 | int memfile_rmnod( |
---|
| 1108 | rtems_filesystem_location_info_t *pathloc /* IN */ |
---|
| 1109 | ) |
---|
| 1110 | { |
---|
[a5305f6b] | 1111 | IMFS_jnode_t *the_jnode; |
---|
[b568ccb] | 1112 | |
---|
| 1113 | the_jnode = (IMFS_jnode_t *) pathloc->node_access; |
---|
| 1114 | |
---|
[a5305f6b] | 1115 | /* |
---|
| 1116 | * Take the node out of the parent's chain that contains this node |
---|
[b568ccb] | 1117 | */ |
---|
| 1118 | |
---|
| 1119 | if ( the_jnode->Parent != NULL ) { |
---|
| 1120 | Chain_Extract( (Chain_Node *) the_jnode ); |
---|
| 1121 | the_jnode->Parent = NULL; |
---|
| 1122 | } |
---|
| 1123 | |
---|
| 1124 | /* |
---|
| 1125 | * Decrement the link counter and see if we can free the space. |
---|
| 1126 | */ |
---|
| 1127 | |
---|
| 1128 | the_jnode->st_nlink--; |
---|
| 1129 | IMFS_update_ctime( the_jnode ); |
---|
| 1130 | |
---|
[2b30225] | 1131 | return memfile_check_rmnod( the_jnode ); |
---|
| 1132 | } |
---|
| 1133 | |
---|
| 1134 | |
---|
| 1135 | int memfile_check_rmnod( IMFS_jnode_t *the_jnode ){ |
---|
| 1136 | |
---|
[b568ccb] | 1137 | /* |
---|
| 1138 | * The file cannot be open and the link must be less than 1 to free. |
---|
| 1139 | */ |
---|
| 1140 | |
---|
| 1141 | if ( !rtems_libio_is_file_open( the_jnode ) && (the_jnode->st_nlink < 1) ) { |
---|
| 1142 | |
---|
[a5305f6b] | 1143 | /* |
---|
[b568ccb] | 1144 | * Is the rtems_filesystem_current is this node? |
---|
| 1145 | */ |
---|
| 1146 | |
---|
[2b30225] | 1147 | if ( rtems_filesystem_current.node_access == the_jnode ) |
---|
[b568ccb] | 1148 | rtems_filesystem_current.node_access = NULL; |
---|
| 1149 | |
---|
| 1150 | /* |
---|
| 1151 | * Free memory associated with a memory file. |
---|
| 1152 | */ |
---|
[d1941587] | 1153 | if (the_jnode->type != IMFS_LINEAR_FILE) |
---|
| 1154 | IMFS_memfile_remove( the_jnode ); |
---|
[b568ccb] | 1155 | |
---|
| 1156 | free( the_jnode ); |
---|
| 1157 | } |
---|
| 1158 | |
---|
| 1159 | return 0; |
---|
| 1160 | } |
---|