AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
iobuf.h
Go to the documentation of this file.
1#ifndef _FS_IOBUF_H
2#define _FS_IOBUF_H
3
4/*
5 * An iobuf is the memory that a read fills or a write empties, as it
6 * moves through the VFS, the devices and the sockets (fs/iobuf.c).
7 *
8 * A system call's buffer is in user memory; the kernel's own reads and
9 * writes (a filesystem reading its device, execve() reading a file) use
10 * kernel memory. Whoever moves the bytes doesn't need to know which:
11 * iobuf_out() puts bytes in the buffer and iobuf_in() takes them out,
12 * each from where the last one stopped, and a user buffer goes through
13 * copyout() and copyin() (sys/uaccess.h), which return -EFAULT where the
14 * memory turns out not to be there.
15 *
16 * The buffer may be in several pieces (segments, as readv()'s vectors
17 * would be), taken in order; `resid` is how much is left of them. A read
18 * returns how much it filled (the resid it started with, less the resid
19 * it ends with), and a write how much it emptied. A fault ends the
20 * transfer: `fault` is set, so that a loop waiting for more stops. What
21 * moved before the fault counts, and if nothing did, the call returns
22 * -EFAULT.
23 */
24
25#include <core/system.h>
26
28struct iobuf_seg {
29 char *base;
30 size_t len;
31};
32
35struct iobuf {
36 struct iobuf_seg *seg; /* the segments left (the first may be partly done) */
37 int nseg;
38 size_t resid; /* bytes left in them */
39 int user; /* whether it's user memory */
40 int fault; /* a copy faulted, so the transfer is over */
41 struct iobuf_seg one; /* a single buffer's segment */
42};
43
44/* Sets up an iobuf of one buffer: `len` bytes at `buf`, in user memory
45 * if `user` is set. */
46void iobuf_init(struct iobuf *io, void *buf, size_t len, int user);
47
48/* Copies up to `n` bytes (at most resid) from the kernel's `src` into the
49 * buffer. Returns how many (fewer if a fault stopped it), or -EFAULT if a
50 * fault came first. */
51ssize_t iobuf_out(struct iobuf *io, const void *src, size_t n);
52
53/* Copies up to `n` bytes (at most resid) from the buffer into the
54 * kernel's `dst`. Returns how many (fewer if a fault stopped it), or
55 * -EFAULT if a fault came first. */
56ssize_t iobuf_in(struct iobuf *io, void *dst, size_t n);
57
58/* Copies from the buffer as iobuf_in() does, but without moving on: the
59 * caller then passes over what it took with iobuf_skip(). */
60ssize_t iobuf_peek(struct iobuf *io, void *dst, size_t n);
61
62/* Writes up to `n` zero bytes into the buffer, as iobuf_out() does. */
63ssize_t iobuf_zero(struct iobuf *io, size_t n);
64
65/* Passes over up to `n` bytes of the buffer, untouched (a write that's
66 * thrown away). Returns how many (at most resid). */
67size_t iobuf_skip(struct iobuf *io, size_t n);
68
72static inline size_t iobuf_limit(struct iobuf *io, size_t n)
73{
74 size_t cut = io->resid > n ? io->resid - n : 0;
75 io->resid -= cut;
76 return cut;
77}
78
80static inline void iobuf_unlimit(struct iobuf *io, size_t cut)
81{
82 io->resid += cut;
83}
84
85#endif /* ! _FS_IOBUF_H */
ssize_t iobuf_out(struct iobuf *io, const void *src, size_t n)
Copies bytes from the kernel into the iobuf's buffer, and moves past them.
Definition iobuf.c:115
ssize_t iobuf_in(struct iobuf *io, void *dst, size_t n)
Copies bytes from the iobuf's buffer into the kernel, and moves past them.
Definition iobuf.c:135
ssize_t iobuf_zero(struct iobuf *io, size_t n)
Fills bytes of the iobuf's buffer with zeros, and moves past them.
Definition iobuf.c:167
size_t iobuf_skip(struct iobuf *io, size_t n)
Moves an iobuf past bytes, without copying them.
Definition iobuf.c:81
void iobuf_init(struct iobuf *io, void *buf, size_t len, int user)
Sets up an iobuf for one buffer.
Definition iobuf.c:19
ssize_t iobuf_peek(struct iobuf *io, void *dst, size_t n)
Copies bytes from the iobuf's buffer into the kernel, without moving past them.
Definition iobuf.c:154
static void iobuf_unlimit(struct iobuf *io, size_t cut)
Gives back what iobuf_limit() cut.
Definition iobuf.h:80
static size_t iobuf_limit(struct iobuf *io, size_t n)
Cuts the buffer to n bytes for a while, so that what it's passed to moves no more.
Definition iobuf.h:72
A piece of an iobuf's memory.
Definition iobuf.h:28
size_t len
Definition iobuf.h:30
char * base
Definition iobuf.h:29
An iobuf.
Definition iobuf.h:35
int nseg
Definition iobuf.h:37
struct iobuf_seg one
Definition iobuf.h:41
int user
Definition iobuf.h:39
int fault
Definition iobuf.h:40
struct iobuf_seg * seg
Definition iobuf.h:36
size_t resid
Definition iobuf.h:38
long int ssize_t
Definition types.h:12