Esempio n. 1
0
/**
 * 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;
}
Esempio n. 2
0
/*
+------------------------------------------------------------------------------
| 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;
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
int accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
{
	int fd, index, ret;

	if (fd_get(socket, &fd) == fd_rsocket) {
		index = fd_open();
		if (index < 0)
			return index;

		ret = raccept(fd, addr, addrlen);
		if (ret < 0) {
			fd_close(index, &fd);
			return ret;
		}

		fd_store(index, ret, fd_rsocket, fd_ready);
		return index;
	} else if (fd_gets(socket) == fd_fork_listen) {
		index = fd_open();
		if (index < 0)
			return index;

		ret = real.accept(fd, addr, addrlen);
		if (ret < 0) {
			fd_close(index, &fd);
			return ret;
		}

		fd_store(index, ret, fd_normal, fd_fork_passive);
		return index;
	} else {
		return real.accept(fd, addr, addrlen);
	}
}
Esempio n. 5
0
offset_t seek(int fd, offset_t offset, char const* orig)
{
    int o;
    switch (orig[0])
    {
    case 's':
        o = SEEK_SET;
        break;
    case 'c':
        o = SEEK_CUR;
        break;
    case 'e':
        o = SEEK_END;
        break;
    default:
        return EOF;
    }

    File* f = fd_get(fd);

    if (f == NULL)
    {
        return EOF;
    }
    
    return f->Seek(offset, o);
}
Esempio n. 6
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;
}
Esempio n. 7
0
/**
 * 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;
}
Esempio n. 8
0
int getpeername(int socket, struct sockaddr *addr, socklen_t *addrlen)
{
	int fd;
	return (fd_get(socket, &fd) == fd_rsocket) ?
		rgetpeername(fd, addr, addrlen) :
		real.getpeername(fd, addr, addrlen);
}
/**
 * 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;
}
Esempio n. 10
0
int
bc_write(int fd, char *buf, int size)
{
	int ret, off;
	int nsize;
	char *nbuf;
	
	if (fd_get(fd) != -1) {
		nsize = getmodsize(size, sizeof (struct compat_utmp), 
		    sizeof (struct utmpx));

		if ((nbuf = (void *)malloc(nsize)) == NULL) {
			(void) fprintf(stderr, "write: malloc failed\n");
			exit(-1);
		}
		
		(void) memset(nbuf, 0, nsize);

		ret = conv2utmpx(nbuf, buf, size);

		if ((ret = _write(fd, nbuf, ret)) == -1) {
			free(nbuf);
			return (-1);
		}

		free(nbuf);
	
		ret = getmodsize(ret, sizeof (struct utmpx), 
		    sizeof (struct compat_utmp));
		return (ret);
	}

	return (_write(fd, buf, size));
}
Esempio n. 11
0
int getsockname(int socket, struct sockaddr *addr, socklen_t *addrlen)
{
	int fd;
	init_preload();
	return (fd_get(socket, &fd) == fd_rsocket) ?
		rgetsockname(fd, addr, addrlen) :
		real.getsockname(fd, addr, addrlen);
}
Esempio n. 12
0
int getsockopt(int socket, int level, int optname,
		void *optval, socklen_t *optlen)
{
	int fd;
	return (fd_get(socket, &fd) == fd_rsocket) ?
		rgetsockopt(fd, level, optname, optval, optlen) :
		real.getsockopt(fd, level, optname, optval, optlen);
}
Esempio n. 13
0
void fd_destroy(int fd)
{
    fd_t *cur = fd_get(fd);

    if (!cur) {
        return;
    }

    memset(cur, 0, sizeof(fd_t));
}
Esempio n. 14
0
size_t read(int fd, void* buf, size_t size)
{
    File* f = fd_get(fd);

    if (f == NULL)
    {
        return 0;
    }

    return f->Read(buf, size);
}
Esempio n. 15
0
offset_t tell(int fd)
{
    File* f = fd_get(fd);

    if (f == NULL)
    {
        return EOF;
    }
    
    return f->Tell();
}
Esempio n. 16
0
__int64 truncate(int fd, file_size_t size)
{
    File* f = fd_get(fd);

    if (f == NULL)
    {
        return EOF;
    }

    return f->Truncate(size);
}
Esempio n. 17
0
size_t write(int fd, void* buf, size_t size)
{
    File* f = fd_get(fd);

    if (f == NULL)
    {
        return 0;
    }

    return f->Write(buf, size);
}
Esempio n. 18
0
int listen(int socket, int backlog)
{
	int fd, ret;
	if (fd_get(socket, &fd) == fd_rsocket) {
		ret = rlisten(fd, backlog);
	} else {
		ret = real.listen(fd, backlog);
		if (!ret && fd_gets(socket) == fd_fork)
			fd_store(socket, fd, fd_normal, fd_fork_listen);
	}
	return ret;
}
Esempio n. 19
0
//------------------------------------------------
// 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;
}
Esempio n. 20
0
/**
 * 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;
}
Esempio n. 21
0
int fd_init(void)
{
    memset(fd_table, 0, sizeof(fd_t) * FD_MAX);

    posix_open(uart0_handler_pid, 0);
    fd_t fd = {
        .__active = 1,
        .fd = uart0_handler_pid,
        .read = (ssize_t ( *)(int, void *, size_t))posix_read,
        .write = (ssize_t ( *)(int, const void *, size_t))posix_write,
        .close = posix_close
    };
    memcpy(&fd_table[STDIN_FILENO], &fd, sizeof(fd_t));
    memcpy(&fd_table[STDOUT_FILENO], &fd, sizeof(fd_t));
    memcpy(&fd_table[STDERR_FILENO], &fd, sizeof(fd_t));
    return FD_MAX;
}

static int fd_get_next_free(void)
{
    for (int i = 0; i < FD_MAX; i++) {
        fd_t *cur = &fd_table[i];

        if (!cur->__active) {
            return i;
        }
    }

    return -1;
}

int fd_new(int internal_fd, ssize_t (*internal_read)(int, void *, size_t),
           ssize_t (*internal_write)(int, const void *, size_t),
           int (*internal_close)(int))
{
    int fd = fd_get_next_free();

    if (fd >= 0) {
        fd_t *fd_s = fd_get(fd);
        fd_s->__active = 1;
        fd_s->fd = internal_fd;
        fd_s->read = internal_read;
        fd_s->write = internal_write;
        fd_s->close = internal_close;
    }
    else {
        errno = ENFILE;
        return -1;
    }

    return fd;
}
Esempio n. 22
0
int fcntl(int socket, int cmd, ... /* arg */)
{
	va_list args;
	long lparam;
	void *pparam;
	int fd, ret;

	init_preload();
	va_start(args, cmd);
	switch (cmd) {
	case F_GETFD:
	case F_GETFL:
	case F_GETOWN:
	case F_GETSIG:
	case F_GETLEASE:
		ret = (fd_get(socket, &fd) == fd_rsocket) ?
			rfcntl(fd, cmd) : real.fcntl(fd, cmd);
		break;
	case F_DUPFD:
	/*case F_DUPFD_CLOEXEC:*/
	case F_SETFD:
	case F_SETFL:
	case F_SETOWN:
	case F_SETSIG:
	case F_SETLEASE:
	case F_NOTIFY:
		lparam = va_arg(args, long);
		ret = (fd_get(socket, &fd) == fd_rsocket) ?
			rfcntl(fd, cmd, lparam) : real.fcntl(fd, cmd, lparam);
		break;
	default:
		pparam = va_arg(args, void *);
		ret = (fd_get(socket, &fd) == fd_rsocket) ?
			rfcntl(fd, cmd, pparam) : real.fcntl(fd, cmd, pparam);
		break;
	}
	va_end(args);
	return ret;
}
Esempio n. 23
0
//------------------------------------------------
// Discover device storage capacity.
//
static bool discover_num_blocks(device* p_device) {
	int fd = fd_get(p_device);

	if (fd == -1) {
		return false;
	}

	g_fd_device = fd;
	uint64_t device_bytes = 0;

	ioctl(fd, BLKGETSIZE64, &device_bytes);
	p_device->num_large_blocks = device_bytes / g_large_block_ops_bytes;
	p_device->min_op_bytes = discover_min_op_bytes(fd, p_device->name);

	if (! (p_device->num_large_blocks && p_device->min_op_bytes)) {
		return false;
	}

	uint64_t num_min_op_blocks =
		(p_device->num_large_blocks * g_large_block_ops_bytes) /
			p_device->min_op_bytes;

	uint64_t read_req_min_op_blocks =
		(g_record_bytes + p_device->min_op_bytes - 1) / p_device->min_op_bytes;

	p_device->num_read_offsets = num_min_op_blocks - read_req_min_op_blocks + 1;
	p_device->read_bytes = read_req_min_op_blocks * p_device->min_op_bytes;

	if (g_output_file){
		fprintf(g_output_file, "-> Blocks Infomation:\n - %s size = %" PRIu64 " bytes\n - %" PRIu64 " large blocks\n - "
			"%" PRIu64 " %" PRIu32 "-byte blocks\n - buffers are %" PRIu32 " bytes\n",
				p_device->name, device_bytes, p_device->num_large_blocks,
				num_min_op_blocks, p_device->min_op_bytes, p_device->read_bytes);
		fprintf(g_output_file, "__________________________________________\n");
	}

	if (g_ref_tab_columns)
	{
		if (!g_device->ref_tab)
		{
			g_device->ref_tab = malloc((g_device->num_read_offsets * g_ref_tab_columns) / 8);
			memset(g_device->ref_tab, 0, sizeof(g_device->ref_tab));
			printf("Table of Reference created(%"PRIu64")!\n", (g_device->num_read_offsets * g_ref_tab_columns) / 64);
		}
	}else{
		return false;
	}

	return true;
}
Esempio n. 24
0
int __fxstat(int ver, int socket, struct stat *buf)
{
	int fd, ret;

	init_preload();
	if (fd_get(socket, &fd) == fd_rsocket) {
		ret = real.fxstat(ver, socket, buf);
		if (!ret)
			buf->st_mode = (buf->st_mode & ~S_IFMT) | __S_IFSOCK;
	} else {
		ret = real.fxstat(ver, fd, buf);
	}
	return ret;
}
Esempio n. 25
0
int close(int fd)
{
    File* f = fd_get(fd);

    if (f == NULL)
    {
        return EOF;
    }
    fd_remove(fd);

    f->Close();
    delete f;

    return 0;
}
Esempio n. 26
0
/*
 * public static native Stat fstat(FileDescriptor fd);
 */
JNIEXPORT jobject JNICALL
Java_org_apache_hadoop_io_nativeio_NativeIO_fstat(
  JNIEnv *env, jclass clazz, jobject fd_object)
{
  jobject ret = NULL;

  int fd = fd_get(env, fd_object);

  struct stat s;
  int rc = fstat(fd, &s);
  if (rc != 0) {
    throw_ioe(env, errno);
    return ret;
  }
  return process_stat(env, s);
}
Esempio n. 27
0
int close(int fildes)
{
    fd_t *fd_obj = fd_get(fildes);

    if (!fd_obj) {
        errno = EBADF;
        return -1;
    }

    if (fd_obj->close(fd_obj->internal_fd) < 0) {
        errno = EIO;    // EINTR may not occur since RIOT has no signals yet.
        return -1;
    }

    fd_destroy(fd_obj->internal_fd);

    return 0;
}
Esempio n. 28
0
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
{
	void *file_addr;
	int fd;
	size_t ret;

	if (fd_get(out_fd, &fd) != fd_rsocket)
		return real.sendfile(fd, in_fd, offset, count);

	file_addr = mmap(NULL, count, PROT_READ, 0, in_fd, offset ? *offset : 0);
	if (file_addr == (void *) -1)
		return -1;

	ret = rwrite(fd, file_addr, count);
	if ((ret > 0) && offset)
		lseek(in_fd, ret, SEEK_CUR);
	munmap(file_addr, count);
	return ret;
}
Esempio n. 29
0
File: act.c Progetto: aanguss/act
//------------------------------------------------
// 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;
}
Esempio n. 30
0
int connect(int socket, const struct sockaddr *addr, socklen_t addrlen)
{
	int fd, ret;

	if (fd_get(socket, &fd) == fd_rsocket) {
		ret = rconnect(fd, addr, addrlen);
		if (!ret || errno == EINPROGRESS)
			return ret;

		ret = transpose_socket(socket, fd_normal);
		if (ret < 0)
			return ret;

		rclose(fd);
		fd = ret;
	} else if (fd_gets(socket) == fd_fork) {
		fd_store(socket, fd, fd_normal, fd_fork_active);
	}

	return real.connect(fd, addr, addrlen);
}