My Kernel v0.1.0
file.h
1
8#ifndef KERNEL_FILE_H
9#define KERNEL_FILE_H
10
11#include <kernel/atomic.h>
12#include <kernel/error.h>
13#include <kernel/spinlock.h>
14
15#include <sys/fcntl.h>
16
17struct vnode;
18struct sockaddr;
19struct msghdr;
20
21#define FD_STDIN 0
22#define FD_STDOUT 1
23#define FD_STDERR 2
24
25/*
26 * File description flag.
27 *
28 * These are set when opening a file description (open, socket, ...) or by
29 * using other syscalls (e.g. fcntl).
30 */
31enum file_flags {
32 FD_READ = _FREAD,
33 FD_WRITE = _FWRITE,
34 FD_RW = FD_READ | FD_WRITE,
35 FD_APPEND = _FAPPEND,
36 FD_NOINHERIT = _FNOINHERIT, /* FD_CLOEXEC is already defined by fcntl(). */
37};
38
45struct file {
46 off_t pos;
47 void *priv;
48 struct vnode *vnode;
49 const struct file_operations *ops;
52 int flags;
53};
54
55void __file_put(struct file *file);
56
60static inline struct file *file_get(struct file *file)
61{
62 atomic_inc(&file->refcount);
63 return file;
64}
65
71static inline void file_put(struct file *file)
72{
73 int count;
74
75 count = atomic_dec(&file->refcount);
76 if (count > 1)
77 return;
78
79 __file_put(file);
80}
81
92 error_t (*open)(struct file *);
99 void (*close)(struct file *);
101 ssize_t (*write)(struct file *, const char *, size_t);
103 ssize_t (*read)(struct file *, char *, size_t);
105 size_t (*size)(struct file *);
107 error_t (*bind)(struct file *, struct sockaddr *addr, size_t len);
109 error_t (*connect)(struct file *, struct sockaddr *addr, size_t len);
111 ssize_t (*sendmsg)(struct file *, const struct msghdr *, int flags);
113 ssize_t (*recvmsg)(struct file *, struct msghdr *, int flags);
124 off_t (*seek)(struct file *, off_t, int whence);
125};
126
128struct file *file_open(struct vnode *, const struct file_operations *);
129
135off_t default_file_seek(struct file *file, off_t off, int whence);
136
138static inline void file_close(struct file *file)
139{
140 file_put(file);
141}
142
144void file_accessed(struct file *file);
146void file_modified(struct file *file);
147
148#define __file_ops(_default, _file, _ops, ...) \
149 (_file->ops->_ops ? _file->ops->_ops(_file __VA_OPT__(, ) __VA_ARGS__) \
150 : _default)
151
152#define file_ops(_file, _ops, ...) \
153 __file_ops(E_NOT_SUPPORTED, _file, _ops, __VA_ARGS__)
154
155#define file_size(file) file_ops(file, size)
156
157#define file_write(file, buf, len) file_ops(file, write, buf, len)
158#define file_read(file, buf, len) file_ops(file, read, buf, len)
159#define file_seek(file, off, whence) file_ops(file, seek, off, whence)
160
161#define file_bind(file, addr, len) file_ops(file, bind, addr, len)
162#define file_connect(file, addr, len) file_ops(file, connect, addr, len)
163
164#define file_sendmsg(file, msg, flags) file_ops(file, sendmsg, msg, flags)
165ssize_t file_send(struct file *file, const char *data, size_t len, int flags);
166ssize_t file_sendto(struct file *file, const char *data, size_t len, int flags,
167 struct sockaddr *addr, size_t addrlen);
168
169#define file_recvmsg(file, msg, flags) file_ops(file, recvmsg, msg, flags)
170ssize_t file_recv(struct file *file, const char *data, size_t len, int flags);
171ssize_t file_recvfrom(struct file *file, const char *data, size_t len,
172 int flags, struct sockaddr *addr, size_t *addrlen);
173
174#endif /* KERNEL_FILE_H */
175
static struct file * file_get(struct file *file)
Increment an open file description's reference count.
Definition: file.h:60
static void file_close(struct file *file)
Free a file struct and release its content.
Definition: file.h:138
static void file_put(struct file *file)
Decrement an open file description's reference count.
Definition: file.h:71
off_t default_file_seek(struct file *file, off_t off, int whence)
Reposition the open file description offset.
Definition: file.c:68
void file_modified(struct file *file)
Update the file's vnode's last modification time.
Definition: file.c:63
void file_accessed(struct file *file)
Update the file's vnode's last access time.
Definition: file.c:58
struct file * file_open(struct vnode *, const struct file_operations *)
Create a new file structure.
Definition: file.c:10
An atomic value.
Definition: atomic.h:22
Operations that can be performed on an opened file.
Definition: file.h:85
off_t(* seek)(struct file *, off_t, int whence)
Reposition the open file description offset.
Definition: file.h:124
ssize_t(* sendmsg)(struct file *, const struct msghdr *, int flags)
Send a message through an endpoint (socket)
Definition: file.h:111
error_t(* bind)(struct file *, struct sockaddr *addr, size_t len)
Associate a socket with a local address.
Definition: file.h:107
error_t(* open)(struct file *)
Filesystem-specific open function (optional).
Definition: file.h:92
size_t(* size)(struct file *)
Compute the file's total size in memory.
Definition: file.h:105
ssize_t(* read)(struct file *, char *, size_t)
Read the content at the current position in the file.
Definition: file.h:103
void(* close)(struct file *)
Called when closing a file (optional).
Definition: file.h:99
ssize_t(* recvmsg)(struct file *, struct msghdr *, int flags)
Send a message through an endpoint (socket)
Definition: file.h:113
ssize_t(* write)(struct file *, const char *, size_t)
Write a buffer to the file at the current position.
Definition: file.h:101
error_t(* connect)(struct file *, struct sockaddr *addr, size_t len)
Connect to a remote host.
Definition: file.h:109
Opened file description.
Definition: file.h:45
void * priv
Private data used by the driver.
Definition: file.h:47
const struct file_operations * ops
Definition: file.h:49
off_t pos
Current offset into the file.
Definition: file.h:46
atomic_t refcount
Number of references to this file.
Definition: file.h:50
spinlock_t lock
Synchronization lock.
Definition: file.h:51
struct vnode * vnode
The file's vnode in the VFS.
Definition: file.h:48
int flags
Parameter flags (.
Definition: file.h:52
Spinlock.
Definition: spinlock.h:29
represents a single virtual node
Definition: vfs.h:206