/** * this function is a POSIX compliant version, which will open a file and return * a file descriptor. * * @param file the path name of file. * @param flags the file open flags. * @param mode * * @return the non-negative integer on successful open, others for failed. */ int open(const char *file, int flags, int mode) { int fd, result; struct dfs_fd *d; /* allocate a fd */ fd = fd_new(); if (fd < 0) { rt_set_errno(-DFS_STATUS_ENOMEM); return -1; } d = fd_get(fd); result = dfs_file_open(d, file, flags); if (result < 0) { /* release the ref-count of fd */ fd_put(d); fd_put(d); rt_set_errno(result); return -1; } /* release the ref-count of fd */ fd_put(d); return fd; }
/** * this function is a POSIX compliant version, which will close the open * file descriptor. * * @param fd the file descriptor. * * @return 0 on successful, -1 on failed. */ int close(int fd) { int result; struct dfs_fd *d; d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-DFS_STATUS_EBADF); return -1; } result = dfs_file_close(d); fd_put(d); if (result < 0) { rt_set_errno(result); return -1; } fd_put(d); return 0; }
/** * this function is a POSIX compliant version, which will write specified data buffer * length for an open file descriptor. * * @param fd the file descriptor * @param buf the data buffer to be written. * @param len the data buffer length. * * @return the actual written data buffer length. */ int write(int fd, const void *buf, size_t len) { int result; struct dfs_fd *d; /* get the fd */ d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-DFS_STATUS_EBADF); return -1; } result = dfs_file_write(d, buf, len); if (result < 0) { fd_put(d); rt_set_errno(result); return -1; } /* release the ref-count of fd */ fd_put(d); return result; }
/* +------------------------------------------------------------------------------ | Function : write +------------------------------------------------------------------------------ | Description : | | Parameters : | Returns : | +------------------------------------------------------------------------------ */ int write(int fd, char *buf, int len) { int result; struct dfs_fd* d; /* get the fd */ d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-RT_ERROR); return -1; } result = dfile_raw_write(d, buf, len); if (result < 0) { rt_set_errno(result); fd_put(d); return -1; } /* release the ref-count of fd */ fd_put(d); return result; }
/** * this function is a POSIX compliant version, which will read specified data buffer * length for an open file descriptor. * * @param fd the file descriptor. * @param buf the buffer to save the read data. * @param len the maximal length of data buffer * * @return the actual read data buffer length */ int read(int fd, void *buf, size_t len) { int result; struct dfs_fd *d; /* get the fd */ d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-RT_ERROR); return -1; } result = dfs_file_read(d, buf, len); if (result < 0) { fd_put(d); rt_set_errno(result); return -1; } /* release the ref-count of fd */ fd_put(d); return result; }
/** * this function is a POSIX compliant version, which will seek the offset for * an open file descriptor. * * @param fd the file descriptor. * @param offset the offset to be seeked. * @param whence the directory of seek. * * @return the current file position, or -1 on failed. */ off_t lseek(int fd, off_t offset, int whence) { int result; struct dfs_fd *d; d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-DFS_STATUS_EBADF); return -1; } switch (whence) { case DFS_SEEK_SET: break; case DFS_SEEK_CUR: offset += d->pos; break; case DFS_SEEK_END: offset += d->size; break; default: rt_set_errno(-DFS_STATUS_EINVAL); return -1; } if (offset < 0) { rt_set_errno(-DFS_STATUS_EINVAL); return -1; } result = dfs_file_lseek(d, offset); if (result < 0) { fd_put(d); rt_set_errno(result); return -1; } /* release the ref-count of fd */ fd_put(d); return offset; }
//------------------------------------------------ // Discover device storage capacity, etc. // static bool discover_device(device* dev) { int fd = fd_get(dev); if (fd == -1) { return false; } uint64_t device_bytes; if (g_scfg.file_size == 0) { ioctl(fd, BLKGETSIZE64, &device_bytes); } else { // undocumented file mode device_bytes = g_scfg.file_size; if (ftruncate(fd, (off_t)device_bytes) != 0) { fprintf(stdout, "ERROR: ftruncate file %s errno %d '%s'\n", dev->name, errno, act_strerror(errno)); fd_put(dev, fd); return false; } } dev->n_large_blocks = device_bytes / g_scfg.large_block_ops_bytes; dev->min_op_bytes = discover_min_op_bytes(fd, dev->name); fd_put(dev, fd); if (dev->n_large_blocks == 0) { fprintf(stdout, "ERROR: %s ioctl to discover size\n", dev->name); return false; } if (dev->min_op_bytes == 0) { return false; } fprintf(stdout, "%s size = %" PRIu64 " bytes, %" PRIu64 " large blocks, " "minimum IO size = %" PRIu32 " bytes\n", dev->name, device_bytes, dev->n_large_blocks, dev->min_op_bytes); discover_read_pattern(dev); if (g_scfg.commit_to_device) { discover_write_pattern(dev); } // else - write load is all accounted for with large-block writes. return true; }
/* Processing reads when they return from aio_read */ static void process_read(as_async_info_t *info) { if(!g_running) { return; } cf_atomic_int_decr(&g_read_reqs_queued); uint64_t stop_time = cf_getms(); fd_put(info->p_readreq.p_device, info->fd); if (stop_time != -1) { histogram_insert_data_point(g_p_raw_read_histogram, safe_delta_ms(info->raw_start_time, stop_time)); histogram_insert_data_point(g_p_read_histogram, safe_delta_ms(info->p_readreq.start_time, stop_time)); histogram_insert_data_point( info->p_readreq.p_device->p_raw_read_histogram, safe_delta_ms(info->raw_start_time, stop_time)); } if (g_use_valloc && info->p_buffer) { free(info->p_buffer); } uintptr_t temp = (uintptr_t)info; cf_queue_push(async_info_queue, (void*)&temp); }
static int read_async_from_device(as_async_info_t *info, aio_context_t aio_ctx) { int fd = fd_get(info->p_readreq.p_device); info->fd = fd; if (fd == -1) { return -1; } struct iocb *aio_cb = &info->aio_cb; aio_read_setup(aio_cb, fd, info); int ret = io_submit(aio_ctx, 1 , &aio_cb); if(ret != 1) { fd_put(info->p_readreq.p_device, fd); if(ret < 0) { fprintf(stdout, "Error: io_submit failed \n"); } else { fprintf(stdout,"Error: Failed to submit IO \n"); } return -1; } return 0; }
/* +------------------------------------------------------------------------------ | Function : lseek +------------------------------------------------------------------------------ | Description : | | Parameters : | Returns : | +------------------------------------------------------------------------------ */ int lseek(int fd, int offset, int dir) { int result; struct dfs_fd* d; d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-RT_ERROR); return -1; } switch (dir) { case DFS_SEEK_SET: break; case DFS_SEEK_CUR: offset += d->pos; break; case DFS_SEEK_END: offset += d->size; break; } result = dfile_raw_lseek(d, offset); if (result < 0) { rt_set_errno(result); fd_put(d); return -1; } /* release the ref-count of fd */ fd_put(d); return offset; }
/* +------------------------------------------------------------------------------ | Function : close +------------------------------------------------------------------------------ | Description : | | Parameters : | Returns : | +------------------------------------------------------------------------------ */ int close(int fd) { int result; struct dfs_fd* d; d = fd_get(fd); if (d == RT_NULL) { rt_set_errno(-RT_ERROR); return -1; } result = dfile_raw_close(d); fd_put(d); fd_put(d); if (result < 0) { rt_set_errno(result); return -1; } return 0; }
int open(char const* name) { File* f = directory_service->OpenFile(name, true, true, false, false, false); if (f == NULL) { return EOF; } int fd = fd_put(f); if (fd == EOF) { f->Close(); delete f; } return fd; }
int close(int file) { if(current->proc->flags & PROC_FLAG_DEBUG) { debug("[info]CLOSE(%x)\n", file); } process_t *p = current->proc; if(!p->fd[file]->ino) { errno = EBADF; return -1; } int retval = vfs_close(p->fd[file]->ino); vfs_free(p->fd[file]->ino); fd_put(p->fd[file]); p->fd[file] = 0; return retval; }
//------------------------------------------------ // Do one device write operation. // static uint64_t write_to_device(device* p_device, uint64_t offset, uint32_t size, uint8_t* p_buffer) { int fd = fd_get(p_device); if (fd == -1) { return -1; } if (lseek(fd, offset, SEEK_SET) != offset || write(fd, p_buffer, size) != (ssize_t)size) { close(fd); fprintf(stdout, "ERROR: seek & write\n"); return -1; } uint64_t stop_us = cf_getus(); fd_put(p_device, fd); return stop_us; }
//------------------------------------------------ // Do one device write operation. // static uint64_t write_to_device(device* dev, uint64_t offset, uint32_t size, const uint8_t* buf) { int fd = fd_get(dev); if (fd == -1) { return -1; } if (! pwrite_all(fd, buf, size, offset)) { close(fd); fprintf(stdout, "ERROR: writing %s: %d '%s'\n", dev->name, errno, act_strerror(errno)); return -1; } uint64_t stop_ns = get_ns(); fd_put(dev, fd); return stop_ns; }
//------------------------------------------------ // Discover device storage capacity. // static void discover_num_blocks(device* p_device) { int fd = fd_get(p_device); if (fd == -1) { p_device->num_512_blocks = 0; p_device->num_large_blocks = 0; return; } uint64_t device_bytes = 0; ioctl(fd, BLKGETSIZE64, &device_bytes); p_device->num_large_blocks = device_bytes / g_large_block_ops_bytes; p_device->num_512_blocks = (p_device->num_large_blocks * g_large_block_ops_bytes) / MIN_BLOCK_BYTES; fprintf(stdout, "%s size = %" PRIu64 " bytes, %" PRIu64 " 512-byte blocks, %" PRIu64 " large blocks\n", p_device->name, device_bytes, p_device->num_512_blocks, p_device->num_large_blocks); fd_put(p_device, fd); }