AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
fops.c File Reference

The operations on open files: each goes to the socket layer, the device's driver, or the vnode's filesystem (its fops). More...

#include <core/system.h>
#include <fs/vfs.h>
#include <fs/posix.h>
#include <fs/pipe.h>
#include <dev/dev.h>
#include <net/socket.h>
#include <aquila/fcntl.h>

Go to the source code of this file.

Functions

int vfs_file_open (struct file *file)
 Opens a file whose vnode is set: a device's driver, a FIFO's pipe, or the filesystem opens it.
ssize_t vfs_file_read (struct file *file, struct iobuf *io)
 Reads from an open file, at its offset.
ssize_t vfs_file_write (struct file *file, struct iobuf *io)
 Writes to an open file, at its offset.
int vfs_file_ioctl (struct file *file, int request, void *argp)
 Passes an ioctl() request to an open file's device or filesystem.
off_t vfs_file_lseek (struct file *file, off_t offset, int whence)
 Moves an open file's offset.
ssize_t vfs_file_readdir (struct file *file, struct dirent *dirent)
 Reads the next entry of an open directory.
ssize_t vfs_file_close (struct file *file)
 Closes an open file, when its last reference goes: its lock is released, then its socket, device or filesystem closes it.
int vfs_file_trunc (struct file *file, off_t len)
 Changes the length of an open file.
struct waitq * vfs_file_wait_queue (struct file *file, int write)
 Finds the wait queue that a reader (or a writer) of an open file sleeps on until it can go on, which poll() and select() wait on too.
int vfs_file_can_read (struct file *file, size_t size)
 Says whether a read of an open file would go on without waiting, for poll() and select().
int vfs_file_can_write (struct file *file, size_t size)
 Says whether a write to an open file would go on without waiting, for poll(), select() and non-blocking writes.
int vfs_file_eof (struct file *file)
 Says whether an open file is at its end, so that a read returns 0 rather than waiting.

Detailed Description

The operations on open files: each goes to the socket layer, the device's driver, or the vnode's filesystem (its fops).

Definition in file fops.c.

Function Documentation

◆ vfs_file_can_read()

int vfs_file_can_read ( struct file * file,
size_t size )

Says whether a read of an open file would go on without waiting, for poll() and select().

A regular file or a directory never waits.

Parameters
filethe open file
sizehow many bytes would be read
Returns
1 if it would, 0 if it would wait, or a negative errno

Definition at line 262 of file fops.c.

References FILE_SOCKET, file::flags, ISDEV, kdev_file_can_read(), socket_can_read(), vfs_file_can_read(), and VNODE_DEV.

Referenced by fd_ready(), and vfs_file_can_read().

◆ vfs_file_can_write()

int vfs_file_can_write ( struct file * file,
size_t size )

Says whether a write to an open file would go on without waiting, for poll(), select() and non-blocking writes.

A regular file never waits.

Parameters
filethe open file
sizehow many bytes would be written
Returns
1 if it would, 0 if it would wait, or a negative errno

Definition at line 295 of file fops.c.

References FILE_SOCKET, file::flags, ISDEV, kdev_file_can_write(), socket_can_write(), vfs_file_can_write(), and VNODE_DEV.

Referenced by fd_ready(), posix_file_write(), and vfs_file_can_write().

◆ vfs_file_close()

ssize_t vfs_file_close ( struct file * file)

Closes an open file, when its last reference goes: its lock is released, then its socket, device or filesystem closes it.

Parameters
filethe open file
Returns
0, or a negative errno

Definition at line 183 of file fops.c.

References FILE_SOCKET, file::flags, ISDEV, kdev_file_close(), posix_file_close(), socket_shutdown(), vfs_file_close(), vfs_file_lock_release(), and VNODE_DEV.

Referenced by file_put(), and vfs_file_close().

◆ vfs_file_eof()

int vfs_file_eof ( struct file * file)

Says whether an open file is at its end, so that a read returns 0 rather than waiting.

Parameters
filethe open file
Returns
1 at the end, 0 otherwise, or a negative errno

Definition at line 325 of file fops.c.

References ISDEV, kdev_file_eof(), vfs_file_eof(), and VNODE_DEV.

Referenced by posix_file_read(), posix_file_write(), and vfs_file_eof().

◆ vfs_file_ioctl()

int vfs_file_ioctl ( struct file * file,
int request,
void * argp )

Passes an ioctl() request to an open file's device or filesystem.

Parameters
filethe open file
requestthe request
argpits argument
Returns
the request's result, or a negative errno

Definition at line 111 of file fops.c.

References ISDEV, kdev_file_ioctl(), vfs_file_ioctl(), and VNODE_DEV.

Referenced by sys_ioctl(), and vfs_file_ioctl().

◆ vfs_file_lseek()

off_t vfs_file_lseek ( struct file * file,
off_t offset,
int whence )

Moves an open file's offset.

Parameters
filethe open file
offsetthe offset, or the distance to move it
whenceSEEK_SET, SEEK_CUR or SEEK_END
Returns
the new offset, -ESPIPE for a pipe, or a negative errno

Definition at line 136 of file fops.c.

References ISDEV, kdev_file_lseek(), vfs_file_lseek(), and VNODE_DEV.

Referenced by sys_lseek(), and vfs_file_lseek().

◆ vfs_file_open()

int vfs_file_open ( struct file * file)

Opens a file whose vnode is set: a device's driver, a FIFO's pipe, or the filesystem opens it.

Parameters
filethe file, its vnode and flags set
Returns
0, -EISDIR to write to a directory, -ENOSYS, or the open's error

Definition at line 22 of file fops.c.

References file::flags, ISDEV, kdev_file_open(), pipefs_fifo_open(), vfs_file_open(), and VNODE_DEV.

Referenced by do_openat(), and vfs_file_open().

◆ vfs_file_read()

ssize_t vfs_file_read ( struct file * file,
struct iobuf * io )

Reads from an open file, at its offset.

Parameters
filethe open file
iowhere the data goes, and how much is wanted
Returns
the number of bytes read, 0 at the end, -EISDIR for a directory, or a negative errno

Definition at line 52 of file fops.c.

References FILE_SOCKET, file::flags, ISDEV, kdev_file_read(), NULL, socket_recv(), vfs_file_read(), and VNODE_DEV.

Referenced by sys_read(), and vfs_file_read().

◆ vfs_file_readdir()

ssize_t vfs_file_readdir ( struct file * file,
struct dirent * dirent )

Reads the next entry of an open directory.

Parameters
filethe open directory
direntthe entry, filled in
Returns
1 if an entry was read, 0 at the end, -ENOTDIR, or a negative errno

Definition at line 162 of file fops.c.

References vfs_file_readdir().

Referenced by sys_readdir(), and vfs_file_readdir().

◆ vfs_file_trunc()

int vfs_file_trunc ( struct file * file,
off_t len )

Changes the length of an open file.

Parameters
filethe open file
lenthe new length, in bytes
Returns
0, -EINVAL for a socket or a device, or a negative errno

Definition at line 213 of file fops.c.

References FILE_SOCKET, file::flags, ISDEV, and vfs_file_trunc().

Referenced by posix_file_open(), sys_ftruncate(), and vfs_file_trunc().

◆ vfs_file_wait_queue()

struct waitq * vfs_file_wait_queue ( struct file * file,
int write )

Finds the wait queue that a reader (or a writer) of an open file sleeps on until it can go on, which poll() and select() wait on too.

A socket says which; a device's and a pipe's are their vnode's (see fs/posix/read.c and write.c).

Parameters
filethe open file
writewhether it's the writers' queue, rather than the readers'
Returns
the wait queue, or NULL if there's none to tell

Definition at line 243 of file fops.c.

References FILE_SOCKET, file::flags, NULL, socket_wait_queue(), and vfs_file_wait_queue().

Referenced by fd_watch(), and vfs_file_wait_queue().

◆ vfs_file_write()

ssize_t vfs_file_write ( struct file * file,
struct iobuf * io )

Writes to an open file, at its offset.

Parameters
filethe open file
iothe data, and how much of it
Returns
the number of bytes written, or a negative errno

Definition at line 83 of file fops.c.

References FILE_SOCKET, file::flags, ISDEV, kdev_file_write(), NULL, socket_send(), vfs_file_write(), and VNODE_DEV.

Referenced by sys_write(), and vfs_file_write().