1 .\" Copyright (c) 1996 Doug Rabson
3 .\" All rights reserved.
5 .\" This program is free software.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
16 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
17 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 .\" $FreeBSD: src/share/man/man9/VOP_RDWR.9,v 1.9.2.2 2001/12/17 11:30:18 ru Exp $
35 .Nd read or write a file
41 .Fn VOP_READ "struct vnode *vp" "struct uio *uio" "int ioflag" "struct ucred *cred"
43 .Fn VOP_WRITE "struct vnode *vp" "struct uio *uio" "int ioflag" "struct ucred *cred"
45 These entry points read or write the contents of a file
48 .Bl -tag -width ioflag
52 the location of the data to be read or written
56 the credentials of the caller
61 argument is used to give directives and hints to the filesystem.
62 When attempting a read, the high 16 bits are used to provide a
63 read-ahead hint (in units of filesystem blocks) that the filesystem
64 should attempt. The low 16 bits are a bit mask which can contain
66 .Bl -tag -width ".Dv IO_NODELOCKED"
74 underlying node already locked
77 flag set in file table
79 data already in VMIO space
82 The file should be locked on entry and will still be locked on exit.
84 Zero is returned on success, otherwise an error code is returned.
88 vop_read(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
93 long size, xfersize, blkoffset;
96 size = block size of filesystem;
98 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
99 bytesinfile = size of file - uio->uio_offset;
100 if (bytesinfile <= 0)
103 lbn = uio->uio_offset / size;
104 blkoffset = uio->uio_offset - lbn * size;
106 xfersize = size - blkoffset;
107 if (uio->uio_resid < xfersize)
108 xfersize = uio->uio_resid;
109 if (bytesinfile < xfersize)
110 xfersize = bytesinfile;
112 error = bread(vp, lbn, size, NOCRED, &bp);
120 * We should only get non-zero b_resid when an I/O error
121 * has occurred, which should cause us to break above.
122 * However, if the short read did not cause an error,
123 * then we want to ensure that we do not uiomove bad
124 * or uninitialized data.
127 if (size < xfersize) {
133 error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
146 vop_write(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
150 daddr_t lbn, nextlbn;
152 long size, resid, xfersize, blkoffset;
156 osize = size of file;
157 size = block size of filesystem;
158 resid = uio->uio_resid;
159 if (ioflag & IO_SYNC)
164 for (error = 0; uio->uio_resid > 0;) {
165 lbn = uio->uio_offset / size;
166 blkoffset = uio->uio_offset - lbn * size;
168 xfersize = size - blkoffset;
169 if (uio->uio_resid < xfersize)
170 xfersize = uio->uio_resid;
172 if (uio->uio_offset + xfersize > size of file)
173 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
180 error = find_block_in_file(vp, lbn, blkoffset + xfersize,
185 if (uio->uio_offset + xfersize > size of file)
186 set size of file to uio->uio_offset + xfersize;
188 error = uiomove((char *)bp->b_data + blkoffset, (int) xfersize, uio);
189 /* XXX ufs does not check the error here. Why? */
191 if (ioflag & IO_VMIO)
192 bp->b_flags |= B_RELBUF; /* ??? */
194 if (ioflag & IO_SYNC)
196 else if (xfersize + blkoffset == size)
201 if (error || xfersize == 0)
206 if (ioflag & IO_UNIT) {
207 VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, cred, uio->uio_procp);
208 uio->uio_offset -= resid - uio->uio_resid;
209 uio->uio_resid = resid;
211 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
213 error = VOP_UPDATE(vp, &tv, &tv, 1); /* XXX what does this do? */
222 The filesystem is full.
228 This man page was written by