-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubr_kernio.c
98 lines (87 loc) · 2.16 KB
/
subr_kernio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/filedesc.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include <sys/buf.h>
#include <sys/mutex.h>
#include <sys/vnode.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/exec.h>
#include <sys/mbuf.h>
#include <sys/poll.h>
#include <sys/select.h>
#include "kernio.h"
struct vnode *
kio_open(const char *file, int flags, int cmode)
{
struct thread *td = curthread;
struct nameidata nd;
int error;
if (td->td_proc->p_fd->fd_rdir == NULL)
td->td_proc->p_fd->fd_rdir = rootvnode;
if (td->td_proc->p_fd->fd_cdir == NULL)
td->td_proc->p_fd->fd_cdir = rootvnode;
flags = FFLAGS(flags);
NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td);
error = vn_open_cred(&nd, &flags, cmode,V_SAVE | V_NORMAL , td->td_ucred, NULL);
NDFREE(&nd, NDF_ONLY_PNBUF);
if (error != 0)
return (NULL);
/* We just unlock so we hold a reference. */
VOP_UNLOCK(nd.ni_vp, 0);
return (nd.ni_vp);
}
void
kio_close(struct vnode *vp)
{
struct thread *td = curthread;
vn_close(vp, FWRITE, td->td_ucred, td);
}
int
kio_write(struct vnode *vp, void *buf, size_t size, unsigned long offset)
{
struct thread *td = curthread;
struct mount *mp;
struct uio auio;
struct iovec aiov;
bzero(&aiov, sizeof(aiov));
bzero(&auio, sizeof(auio));
aiov.iov_base = buf;
aiov.iov_len = size;
auio.uio_iov = &aiov;
auio.uio_offset = offset;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_iovcnt = 1;
auio.uio_resid = size;
auio.uio_td = td;
/*
* Do all of the junk required to write now.
*/
vn_start_write(vp, &mp, V_WAIT);
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
//VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
VOP_WRITE(vp, &auio, IO_UNIT | IO_ASYNC, td->td_ucred);
VOP_UNLOCK(vp, 0);
vn_finished_write(mp);
return (0);
}