xref: /dragonfly/share/man/man9/VOP_RDWR.9 (revision cdecd76a)
1.\" -*- nroff -*-
2.\"
3.\" Copyright (c) 1996 Doug Rabson
4.\"
5.\" All rights reserved.
6.\"
7.\" This program is free software.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.\"
29.\" $FreeBSD: src/share/man/man9/VOP_RDWR.9,v 1.9.2.2 2001/12/17 11:30:18 ru Exp $
30.\" $DragonFly: src/share/man/man9/VOP_RDWR.9,v 1.3 2003/07/27 05:36:06 hmp Exp $
31.\"
32.Dd July 24, 1996
33.Os
34.Dt VOP_RDWR 9
35.Sh NAME
36.Nm VOP_READ ,
37.Nm VOP_WRITE
38.Nd read or write a file
39.Sh SYNOPSIS
40.In sys/param.h
41.In sys/vnode.h
42.In sys/uio.h
43.Ft int
44.Fn VOP_READ "struct vnode *vp" "struct uio *uio" "int ioflag" "struct ucred *cred"
45.Ft int
46.Fn VOP_WRITE "struct vnode *vp" "struct uio *uio" "int ioflag" "struct ucred *cred"
47.Sh DESCRIPTION
48These entry points read or write the contents of a file
49.Pp
50The arguments are:
51.Bl -tag -width ioflag
52.It Fa vp
53the vnode of the file
54.It Fa uio
55the location of the data to be read or written
56.It Fa ioflag
57various flags
58.It Fa cnp
59the credentials of the caller
60.El
61.Pp
62The
63.Fa ioflag
64argument is used to give directives and hints to the filesystem.
65When attempting a read, the high 16 bits are used to provide a
66read-ahead hint (in units of filesystem blocks) that the filesystem
67should attempt.  The low 16 bits are a bit mask which can contain
68the following flags:
69.Bl -tag -width IO_NODELOCKED
70.It Dv IO_UNIT
71do I/O as atomic unit
72.It Dv IO_APPEND
73append write to end
74.It Dv IO_SYNC
75do I/O synchronously
76.It Dv IO_NODELOCKED
77underlying node already locked
78.It Dv IO_NDELAY
79.Dv FNDELAY
80flag set in file table
81.It Dv IO_VMIO
82data already in VMIO space
83.El
84.Sh LOCKS
85The file should be locked on entry and will still be locked on exit.
86.Sh RETURN VALUES
87Zero is returned on success, otherwise an error code is returned.
88.Sh PSEUDOCODE
89.Bd -literal
90int
91vop_read(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
92{
93    struct buf *bp;
94    off_t bytesinfile;
95    daddr_t lbn, nextlbn;
96    long size, xfersize, blkoffset;
97    int error;
98
99    size = block size of filesystem;
100
101    for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
102	bytesinfile = size of file - uio->uio_offset;
103	if (bytesinfile <= 0)
104	    break;
105
106	lbn = uio->uio_offset / size;
107	blkoffset = uio->uio_offset - lbn * size;
108
109	xfersize = size - blkoffset;
110	if (uio->uio_resid < xfersize)
111	    xfersize = uio->uio_resid;
112	if (bytesinfile < xfersize)
113	    xfersize = bytesinfile;
114
115	error = bread(vp, lbn, size, NOCRED, &bp);
116	if (error) {
117	    brelse(bp);
118	    bp = NULL;
119	    break;
120	}
121
122	/*
123	 * We should only get non-zero b_resid when an I/O error
124	 * has occurred, which should cause us to break above.
125	 * However, if the short read did not cause an error,
126	 * then we want to ensure that we do not uiomove bad
127	 * or uninitialized data.
128	 */
129	size -= bp->b_resid;
130	if (size < xfersize) {
131	    if (size == 0)
132		break;
133	    xfersize = size;
134	}
135
136	error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
137	if (error)
138	    break;
139
140	bqrelse(bp);
141    }
142    if (bp != NULL)
143	bqrelse(bp);
144
145    return error;
146}
147
148int
149vop_write(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
150{
151    struct buf *bp;
152    off_t bytesinfile;
153    daddr_t lbn, nextlbn;
154    off_t osize;
155    long size, resid, xfersize, blkoffset;
156    int flags;
157    int error;
158
159    osize = size of file;
160    size = block size of filesystem;
161    resid = uio->uio_resid;
162    if (ioflag & IO_SYNC)
163	flags = B_SYNC;
164    else
165	flags = 0;
166
167    for (error = 0; uio->uio_resid > 0;) {
168	lbn = uio->uio_offset / size;
169	blkoffset = uio->uio_offset - lbn * size;
170
171	xfersize = size - blkoffset;
172	if (uio->uio_resid < xfersize)
173	    xfersize = uio->uio_resid;
174
175	if (uio->uio_offset + xfersize > size of file)
176	    vnode_pager_setsize(vp, uio->uio_offset + xfersize);
177
178	if (size > xfersize)
179	    flags |= B_CLRBUF;
180	else
181	    flags &= ~B_CLRBUF;
182
183	error = find_block_in_file(vp, lbn, blkoffset + xfersize,
184				   cred, &bp, flags);
185	if (error)
186	    break;
187
188	if (uio->uio_offset + xfersize > size of file)
189	    set size of file to uio->uio_offset + xfersize;
190
191	error = uiomove((char *)bp->b_data + blkoffset, (int) xfersize, uio);
192	/* XXX ufs does not check the error here.  Why? */
193
194	if (ioflag & IO_VMIO)
195	    bp->b_flags |= B_RELBUF; /* ??? */
196
197	if (ioflag & IO_SYNC)
198	    bwrite(bp);
199	else if (xfersize + blkoffset == size)
200	    bawrite(bp);
201	else
202	    bdwrite(bp);
203
204	if (error || xfersize == 0)
205	    break;
206    }
207
208    if (error) {
209	if (ioflag & IO_UNIT) {
210	    VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, cred, uio->uio_procp);
211	    uio->uio_offset -= resid - uio->uio_resid;
212	    uio->uio_resid = resid;
213	}
214    } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
215	struct timeval tv;
216	error = VOP_UPDATE(vp, &tv, &tv, 1); /* XXX what does this do? */
217    }
218
219    return error;
220}
221.Ed
222.Sh ERRORS
223.Bl -tag -width Er
224.It Bq Er ENOSPC
225The filesystem is full.
226.El
227.Sh SEE ALSO
228.Xr uiomove 9 ,
229.Xr vnode 9
230.Sh AUTHORS
231This man page was written by
232.An Doug Rabson .
233