FreeBSD-5.3/share/man/man9/VOP_RDWR.9

Compare this file to the similar file:
Show the results in this format:

.\" -*- nroff -*-
.\"
.\" Copyright (c) 1996 Doug Rabson
.\"
.\" All rights reserved.
.\"
.\" This program is free software.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD: src/share/man/man9/VOP_RDWR.9,v 1.17 2004/07/08 00:43:50 alfred Exp $
.\"
.Dd July 24, 1996
.Os
.Dt VOP_RDWR 9
.Sh NAME
.Nm VOP_READ ,
.Nm VOP_WRITE
.Nd read or write a file
.Sh SYNOPSIS
.In sys/param.h
.In sys/vnode.h
.In sys/uio.h
.Ft int
.Fn VOP_READ "struct vnode *vp" "struct uio *uio" "int ioflag" "struct ucred *cred"
.Ft int
.Fn VOP_WRITE "struct vnode *vp" "struct uio *uio" "int ioflag" "struct ucred *cred"
.Sh DESCRIPTION
These entry points read or write the contents of a file
.Pp
The arguments are:
.Bl -tag -width ioflag
.It Fa vp
the vnode of the file
.It Fa uio
the location of the data to be read or written
.It Fa ioflag
various flags
.It Fa cnp
the credentials of the caller
.El
.Pp
The
.Fa ioflag
argument is used to give directives and hints to the file system.
When attempting a read, the high 16 bits are used to provide a
read-ahead hint (in units of file system blocks) that the file system
should attempt.
The low 16 bits are a bit mask which can contain
the following flags:
.Bl -tag -width IO_NODELOCKED
.It Dv IO_UNIT
do I/O as atomic unit
.It Dv IO_APPEND
append write to end
.It Dv IO_SYNC
do I/O synchronously
.It Dv IO_NODELOCKED
underlying node already locked
.It Dv IO_NDELAY
.Dv FNDELAY
flag set in file table
.It Dv IO_VMIO
data already in VMIO space
.El
.Sh LOCKS
The file should be locked on entry and will still be locked on exit.
.Sh RETURN VALUES
Zero is returned on success, otherwise an error code is returned.
.Sh PSEUDOCODE
.Bd -literal
int
vop_read(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
{
    struct buf *bp;
    off_t bytesinfile;
    daddr_t lbn, nextlbn;
    long size, xfersize, blkoffset;
    int error;

    size = block size of file system;

    for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
	bytesinfile = size of file - uio->uio_offset;
	if (bytesinfile <= 0)
	    break;

	lbn = uio->uio_offset / size;
	blkoffset = uio->uio_offset - lbn * size;

	xfersize = size - blkoffset;
	if (uio->uio_resid < xfersize)
	    xfersize = uio->uio_resid;
	if (bytesinfile < xfersize)
	    xfersize = bytesinfile;

	error = bread(vp, lbn, size, NOCRED, &bp);
	if (error) {
	    brelse(bp);
	    bp = NULL;
	    break;
	}

	/*
	 * We should only get non-zero b_resid when an I/O error
	 * has occurred, which should cause us to break above.
	 * However, if the short read did not cause an error,
	 * then we want to ensure that we do not uiomove bad
	 * or uninitialized data.
	 */
	size -= bp->b_resid;
	if (size < xfersize) {
	    if (size == 0)
		break;
	    xfersize = size;
	}

	error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
	if (error)
	    break;

	bqrelse(bp);
    }
    if (bp != NULL)
	bqrelse(bp);

    return (error);
}

int
vop_write(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
{
    struct buf *bp;
    off_t bytesinfile;
    daddr_t lbn, nextlbn;
    off_t osize;
    long size, resid, xfersize, blkoffset;
    int flags;
    int error;

    osize = size of file;
    size = block size of file system;
    resid = uio->uio_resid;
    if (ioflag & IO_SYNC)
	flags = B_SYNC;
    else
	flags = 0;

    for (error = 0; uio->uio_resid > 0;) {
	lbn = uio->uio_offset / size;
	blkoffset = uio->uio_offset - lbn * size;

	xfersize = size - blkoffset;
	if (uio->uio_resid < xfersize)
	    xfersize = uio->uio_resid;

	if (uio->uio_offset + xfersize > size of file)
	    vnode_pager_setsize(vp, uio->uio_offset + xfersize);

	if (size > xfersize)
	    flags |= B_CLRBUF;
	else
	    flags &= ~B_CLRBUF;

	error = find_block_in_file(vp, lbn, blkoffset + xfersize,
				   cred, &bp, flags);
	if (error)
	    break;

	if (uio->uio_offset + xfersize > size of file)
	    set size of file to uio->uio_offset + xfersize;

	error = uiomove((char *)bp->b_data + blkoffset, (int) xfersize, uio);
	/* XXX ufs does not check the error here.  Why? */

	if (ioflag & IO_VMIO)
	    bp->b_flags |= B_RELBUF; /* ??? */

	if (ioflag & IO_SYNC)
	    bwrite(bp);
	else if (xfersize + blkoffset == size)
	    bawrite(bp);
	else
	    bdwrite(bp);

	if (error || xfersize == 0)
	    break;
    }

    if (error) {
	if (ioflag & IO_UNIT) {
	    /* call private routine to truncate file. */
	    your_truncate(vp, osize, ioflag & IO_SYNC, cred, uio->uio_td);
	    uio->uio_offset -= resid - uio->uio_resid;
	    uio->uio_resid = resid;
	}
    } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
	struct timeval tv;
	error = VOP_UPDATE(vp, &tv, &tv, 1); /* XXX what does this do? */
    }

    return (error);
}
.Ed
.Sh ERRORS
.Bl -tag -width Er
.It Bq Er EFBIG
An attempt was made to write a file that exceeds the process's file size
limit or the maximum file size.
.It Bq Er ENOSPC
The file system is full.
.It Bq Er EPERM
An append-only flag is set on the file, but the caller is attempting to
write before the current end of file.
.El
.Sh SEE ALSO
.Xr uiomove 9 ,
.Xr vnode 9
.Sh AUTHORS
This man page was written by
.An Doug Rabson .