int rtems_rfs_buffer_handle_request (rtems_rfs_file_system* fs, rtems_rfs_buffer_handle* handle, rtems_rfs_buffer_block block, bool read) { int rc; /* * If the handle has a buffer release it. This allows a handle to be reused * without needing to close then open it again. */ if (rtems_rfs_buffer_handle_has_block (handle)) { /* * Treat block 0 as special to handle the loading of the super block. */ if (block && (rtems_rfs_buffer_bnum (handle) == block)) return 0; if (rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_REQUEST)) printf ("rtems-rfs: buffer-request: handle has buffer: %" PRIu32 "\n", rtems_rfs_buffer_bnum (handle)); rc = rtems_rfs_buffer_handle_release (fs, handle); if (rc > 0) return rc; handle->dirty = false; handle->bnum = 0; } if (rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_REQUEST)) printf ("rtems-rfs: buffer-request: block=%" PRIu32 "\n", block); /* * First check to see if the buffer has already been requested and is * currently attached to a handle. If it is share the access. A buffer could * be shared where different parts of the block have separate functions. An * example is an inode block and the file system needs to handle 2 inodes in * the same block at the same time. */ if (fs->buffers_count) { /* * Check the active buffer list for shared buffers. */ handle->buffer = rtems_rfs_scan_chain (&fs->buffers, &fs->buffers_count, block); if (rtems_rfs_buffer_handle_has_block (handle) && rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_REQUEST)) printf ("rtems-rfs: buffer-request: buffer shared: refs: %d\n", rtems_rfs_buffer_refs (handle) + 1); } /* * If the buffer has not been found check the local cache of released * buffers. There are release and released modified lists to preserve the * state. */ if (!rtems_rfs_fs_no_local_cache (fs) && !rtems_rfs_buffer_handle_has_block (handle)) { /* * Check the local cache of released buffers. */ if (fs->release_count) handle->buffer = rtems_rfs_scan_chain (&fs->release, &fs->release_count, block); if (!rtems_rfs_buffer_handle_has_block (handle) && fs->release_modified_count) { handle->buffer = rtems_rfs_scan_chain (&fs->release_modified, &fs->release_modified_count, block); /* * If we found a buffer retain the dirty buffer state. */ if (rtems_rfs_buffer_handle_has_block (handle)) rtems_rfs_buffer_mark_dirty (handle); } } /* * If not located we request the buffer from the I/O layer. */ if (!rtems_rfs_buffer_handle_has_block (handle)) { rc = rtems_rfs_buffer_io_request (fs, block, read, &handle->buffer); if (rc > 0) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_REQUEST)) printf ("rtems-rfs: buffer-request: block=%" PRIu32 ": bdbuf-%s: %d: %s\n", block, read ? "read" : "get", rc, strerror (rc)); return rc; } rtems_chain_set_off_chain (rtems_rfs_buffer_link(handle)); } /* * Increase the reference count of the buffer. */ rtems_rfs_buffer_refs_up (handle); rtems_chain_append (&fs->buffers, rtems_rfs_buffer_link (handle)); fs->buffers_count++; handle->buffer->user = (void*) ((intptr_t) block); handle->bnum = block; if (rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_REQUEST)) printf ("rtems-rfs: buffer-request: block=%" PRIu32 " bdbuf-%s=%" PRIu32 " refs=%d\n", block, read ? "read" : "get", handle->buffer->block, handle->buffer->references); return 0; }
int rtems_rfs_file_seek (rtems_rfs_file_handle* handle, rtems_rfs_pos pos, rtems_rfs_pos* new_pos) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_FILE_IO)) printf ("rtems-rfs: file-seek: new=%" PRIu64 "\n", pos); /* * This call only sets the position if it is in a valid part of the file. The * user can request past the end of the file then write to extend the * file. The lseek entry states: * * "Although lseek() may position the file offset beyond the end of the * file, this function does not itself extend the size of the file." * * This means the file needs to set the file size to the pos only when a * write occurs. */ if (pos < rtems_rfs_file_shared_get_size (rtems_rfs_file_fs (handle), handle->shared)) { rtems_rfs_file_set_bpos (handle, pos); /* * If the file has a block check if it maps to the current position and it * does not release it. That will force us to get the block at the new * position when the I/O starts. */ if (rtems_rfs_buffer_handle_has_block (&handle->buffer)) { rtems_rfs_buffer_block block; int rc; rc = rtems_rfs_block_map_find (rtems_rfs_file_fs (handle), rtems_rfs_file_map (handle), rtems_rfs_file_bpos (handle), &block); if (rc > 0) return rc; if (rtems_rfs_buffer_bnum (&handle->buffer) != block) { rc = rtems_rfs_buffer_handle_release (rtems_rfs_file_fs (handle), rtems_rfs_file_buffer (handle)); if (rc > 0) return rc; } } } else { /* * The seek is outside the current file so release any buffer. A write will * extend the file. */ int rc = rtems_rfs_file_io_release (handle); if (rc > 0) return rc; } *new_pos = pos; return 0; }
int rtems_rfs_buffer_handle_release (rtems_rfs_file_system* fs, rtems_rfs_buffer_handle* handle) { int rc = 0; if (rtems_rfs_buffer_handle_has_block (handle)) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_RELEASE)) printf ("rtems-rfs: buffer-release: block=%" PRIu32 " %s refs=%d %s\n", rtems_rfs_buffer_bnum (handle), rtems_rfs_buffer_dirty (handle) ? "(dirty)" : "", rtems_rfs_buffer_refs (handle), rtems_rfs_buffer_refs (handle) == 0 ? "BAD REF COUNT" : ""); if (rtems_rfs_buffer_refs (handle) > 0) rtems_rfs_buffer_refs_down (handle); if (rtems_rfs_buffer_refs (handle) == 0) { rtems_chain_extract (rtems_rfs_buffer_link (handle)); fs->buffers_count--; if (rtems_rfs_fs_no_local_cache (fs)) { handle->buffer->user = (void*) 0; rc = rtems_rfs_buffer_io_release (handle->buffer, rtems_rfs_buffer_dirty (handle)); } else { /* * If the total number of held buffers is higher than the configured * value remove a buffer from the queue with the most buffers and * release. The buffers are held on the queues with the newest at the * head. * * This code stops a large series of transactions causing all the * buffers in the cache being held in queues of this file system. */ if ((fs->release_count + fs->release_modified_count) >= fs->max_held_buffers) { rtems_rfs_buffer* buffer; bool modified; if (rtems_rfs_trace (RTEMS_RFS_TRACE_BUFFER_HANDLE_RELEASE)) printf ("rtems-rfs: buffer-release: local cache overflow:" " %" PRIu32 "\n", fs->release_count + fs->release_modified_count); if (fs->release_count > fs->release_modified_count) { buffer = (rtems_rfs_buffer*) rtems_chain_get (&fs->release); fs->release_count--; modified = false; } else { buffer = (rtems_rfs_buffer*) rtems_chain_get (&fs->release_modified); fs->release_modified_count--; modified = true; } buffer->user = (void*) 0; rc = rtems_rfs_buffer_io_release (buffer, modified); } if (rtems_rfs_buffer_dirty (handle)) { rtems_chain_append (&fs->release_modified, rtems_rfs_buffer_link (handle)); fs->release_modified_count++; } else { rtems_chain_append (&fs->release, rtems_rfs_buffer_link (handle)); fs->release_count++; } } } handle->buffer = NULL; } return rc; }
static int rtems_rfs_shell_inode (rtems_rfs_file_system* fs, int argc, char *argv[]) { rtems_rfs_ino start; rtems_rfs_ino end; rtems_rfs_ino total; rtems_rfs_ino ino; bool show_all; bool error_check_only; bool forced; bool have_start; bool have_end; int arg; int b; int rc; total = fs->group_inodes * fs->group_count; start = RTEMS_RFS_ROOT_INO; end = total - 1; show_all = false; error_check_only = false; forced = false; have_start = have_end = false; for (arg = 1; arg < argc; arg++) { if (argv[arg][0] == '-') { switch (argv[arg][1]) { case 'a': show_all = true; break; case 'e': error_check_only = true; break; case 'f': forced = true; break; default: printf ("warning: option ignored: %s\n", argv[arg]); break; } } else { if (have_end && have_start) printf ("warning: option ignored: %s\n", argv[arg]); else if (!have_start) { start = end = strtoul (argv[arg], 0, 0); have_start = true; } else { end = strtoul (argv[arg], 0, 0); have_end = true; } } } if ((start >= total) || (end >= total)) { printf ("error: inode out of range (0->%" PRId32 ").\n", total - 1); return 1; } rtems_rfs_shell_lock_rfs (fs); for (ino = start; ino <= end; ino++) { rtems_rfs_inode_handle inode; bool allocated; rc = rtems_rfs_group_bitmap_test (fs, true, ino, &allocated); if (rc > 0) { rtems_rfs_shell_unlock_rfs (fs); printf ("error: testing inode state: ino=%" PRIu32 ": (%d) %s\n", ino, rc, strerror (rc)); return 1; } if (show_all || allocated) { uint16_t mode; bool error; rc = rtems_rfs_inode_open (fs, ino, &inode, true); if (rc > 0) { rtems_rfs_shell_unlock_rfs (fs); printf ("error: opening inode handle: ino=%" PRIu32 ": (%d) %s\n", ino, rc, strerror (rc)); return 1; } error = false; mode = rtems_rfs_inode_get_mode (&inode); if (error_check_only) { if (!RTEMS_RFS_S_ISDIR (mode) && !RTEMS_RFS_S_ISCHR (mode) && !RTEMS_RFS_S_ISBLK (mode) && !RTEMS_RFS_S_ISREG (mode) && !RTEMS_RFS_S_ISLNK (mode)) error = true; else { #if NEED_TO_HANDLE_DIFFERENT_TYPES int b; for (b = 0; b < RTEMS_RFS_INODE_BLOCKS; b++) { uint32_t block; block = rtems_rfs_inode_get_block (&inode, b); if ((block <= RTEMS_RFS_SUPERBLOCK_SIZE) || (block >= rtems_rfs_fs_blocks (fs))) error = true; } #endif } } if (!error_check_only || error) { printf (" %5" PRIu32 ": pos=%06" PRIu32 ":%04zx %c ", ino, rtems_rfs_buffer_bnum (&inode.buffer), inode.offset * RTEMS_RFS_INODE_SIZE, allocated ? 'A' : 'F'); if (!allocated && !forced) printf (" --\n"); else { const char* type; type = "UKN"; if (RTEMS_RFS_S_ISDIR (mode)) type = "DIR"; else if (RTEMS_RFS_S_ISCHR (mode)) type = "CHR"; else if (RTEMS_RFS_S_ISBLK (mode)) type = "BLK"; else if (RTEMS_RFS_S_ISREG (mode)) type = "REG"; else if (RTEMS_RFS_S_ISLNK (mode)) type = "LNK"; printf ("links=%03i mode=%04x (%s/%03o) bo=%04u bc=%04" PRIu32 " b=[", rtems_rfs_inode_get_links (&inode), mode, type, mode & ((1 << 10) - 1), rtems_rfs_inode_get_block_offset (&inode), rtems_rfs_inode_get_block_count (&inode)); for (b = 0; b < (RTEMS_RFS_INODE_BLOCKS - 1); b++) printf ("%" PRIu32 " ", rtems_rfs_inode_get_block (&inode, b)); printf ("%" PRIu32 "]\n", rtems_rfs_inode_get_block (&inode, b)); } } rc = rtems_rfs_inode_close (fs, &inode); if (rc > 0) { rtems_rfs_shell_unlock_rfs (fs); printf ("error: closing inode handle: ino=%" PRIu32 ": (%d) %s\n", ino, rc, strerror (rc)); return 1; } } } rtems_rfs_shell_unlock_rfs (fs); return 0; }