AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
trunc.c
Go to the documentation of this file.
1
4
5#include <core/system.h>
6#include <fs/vfs.h>
7#include <dev/dev.h>
8#include <mm/vm.h>
9
19int vfs_trunc(struct vnode *vnode, off_t len)
20{
21 vfs_log(LOG_DEBUG, "vfs_trunc(vnode=%p, len=%d)\n", vnode, len);
22
23 /* invalid request */
24 if (!vnode)
25 return -EINVAL;
26
27 /* a device node has no length to change */
28 if (ISDEV(vnode))
29 return -EINVAL;
30
31 /* invalid request */
32 if (!vnode->fs)
33 return -EINVAL;
34
35 /* the filesystem doesn't support it */
36 if (!vnode->fs->vops.trunc)
37 return -ENOSYS;
38
39 off_t old = vnode->size;
40 int err = vnode->fs->vops.trunc(vnode, len);
41
42 /* refresh the file's pages in the page cache between the old end and
43 * the new one */
44 if (!err)
45 vnode_pages_update(vnode, MIN(old, len), MAX(old, len));
46
47 return err;
48}
49
void vnode_pages_update(struct vnode *vnode, off_t start, off_t end)
Reads a file's cached pages again after a write or a truncation changed a range of it.
Definition vm_object.c:128
#define MIN(a, b)
Definition system.h:11
#define MAX(a, b)
Definition system.h:10
#define LOG_DEBUG
Definition printk.h:19
struct vops vops
Definition vfs.h:104
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
struct fs * fs
Definition vfs.h:127
size_t size
Definition vfs.h:115
int(*) trunc(struct vnode *vnode, off_t len)
Definition vfs.h:76
int vfs_trunc(struct vnode *vnode, off_t len)
Changes a file's length, through its filesystem.
Definition trunc.c:19
long int off_t
Definition types.h:10
#define ISDEV(vnode)
Definition vfs.h:209