read.c revision 124811
138451Smsmith/* $NetBSD: read.c,v 1.8 1997/01/22 00:38:12 cgd Exp $ */ 238451Smsmith 338451Smsmith/*- 438451Smsmith * Copyright (c) 1993 538451Smsmith * The Regents of the University of California. All rights reserved. 638451Smsmith * 738451Smsmith * This code is derived from software contributed to Berkeley by 838451Smsmith * The Mach Operating System project at Carnegie-Mellon University. 938451Smsmith * 1038451Smsmith * Redistribution and use in source and binary forms, with or without 1138451Smsmith * modification, are permitted provided that the following conditions 1238451Smsmith * are met: 1338451Smsmith * 1. Redistributions of source code must retain the above copyright 1438451Smsmith * notice, this list of conditions and the following disclaimer. 1538451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1638451Smsmith * notice, this list of conditions and the following disclaimer in the 1738451Smsmith * documentation and/or other materials provided with the distribution. 1838451Smsmith * 3. All advertising materials mentioning features or use of this software 1938451Smsmith * must display the following acknowledgement: 2038451Smsmith * This product includes software developed by the University of 2138451Smsmith * California, Berkeley and its contributors. 2238451Smsmith * 4. Neither the name of the University nor the names of its contributors 2338451Smsmith * may be used to endorse or promote products derived from this software 2438451Smsmith * without specific prior written permission. 2538451Smsmith * 2638451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2738451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2838451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2938451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3038451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3138451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3238451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3338451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3438451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3538451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3638451Smsmith * SUCH DAMAGE. 3738451Smsmith * 3838451Smsmith * @(#)read.c 8.1 (Berkeley) 6/11/93 3938451Smsmith * 4038451Smsmith * 4138451Smsmith * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University 4238451Smsmith * All Rights Reserved. 4338451Smsmith * 4438451Smsmith * Author: Alessandro Forin 4538451Smsmith * 4638451Smsmith * Permission to use, copy, modify and distribute this software and its 4738451Smsmith * documentation is hereby granted, provided that both the copyright 4838451Smsmith * notice and this permission notice appear in all copies of the 4938451Smsmith * software, derivative works or modified versions, and any portions 5038451Smsmith * thereof, and that both notices appear in supporting documentation. 5138451Smsmith * 5238451Smsmith * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 5338451Smsmith * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 5438451Smsmith * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5538451Smsmith * 5638451Smsmith * Carnegie Mellon requests users of this software to return to 5738451Smsmith * 5838451Smsmith * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 5938451Smsmith * School of Computer Science 6038451Smsmith * Carnegie Mellon University 6138451Smsmith * Pittsburgh PA 15213-3890 6238451Smsmith * 6338451Smsmith * any improvements or extensions that they make and grant Carnegie the 6438451Smsmith * rights to redistribute these changes. 6538451Smsmith */ 6638451Smsmith 6784221Sdillon#include <sys/cdefs.h> 6884221Sdillon__FBSDID("$FreeBSD: head/lib/libstand/read.c 124811 2004-01-21 20:12:23Z jhb $"); 6984221Sdillon 7038451Smsmith#include <sys/param.h> 7138451Smsmith#include "stand.h" 7238451Smsmith 7338451Smsmithssize_t 7465470Smsmithread(int fd, void *dest, size_t bcount) 7538451Smsmith{ 7665470Smsmith struct open_file *f = &files[fd]; 7765470Smsmith size_t resid; 7838451Smsmith 7965470Smsmith if ((unsigned)fd >= SOPEN_MAX || !(f->f_flags & F_READ)) { 8065470Smsmith errno = EBADF; 8165470Smsmith return (-1); 8265470Smsmith } 8365470Smsmith if (f->f_flags & F_RAW) { 8465470Smsmith twiddle(); 8565470Smsmith errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 8665470Smsmith btodb(f->f_offset), bcount, dest, &resid); 8765470Smsmith if (errno) 8865470Smsmith return (-1); 8965470Smsmith f->f_offset += resid; 9065470Smsmith return (resid); 9165470Smsmith } 9265470Smsmith 9365470Smsmith /* 9465470Smsmith * Optimise reads from regular files using a readahead buffer. 9565470Smsmith * If the request can't be satisfied from the current buffer contents, 9665470Smsmith * check to see if it should be bypassed, or refill the buffer and complete 9765470Smsmith * the request. 9865470Smsmith */ 9965470Smsmith resid = bcount; 10065470Smsmith for (;;) { 10165470Smsmith size_t ccount, cresid; 10265470Smsmith /* how much can we supply? */ 10365470Smsmith ccount = imin(f->f_ralen, resid); 10465470Smsmith if (ccount > 0) { 10565470Smsmith bcopy(f->f_rabuf + f->f_raoffset, dest, ccount); 10665470Smsmith f->f_raoffset += ccount; 10765470Smsmith f->f_ralen -= ccount; 10865470Smsmith resid -= ccount; 10965470Smsmith if (resid == 0) 11065470Smsmith return(bcount); 11165470Smsmith dest += ccount; 11238451Smsmith } 11365470Smsmith 11465470Smsmith /* will filling the readahead buffer again not help? */ 11565470Smsmith if (resid >= SOPEN_RASIZE) { 11665470Smsmith /* bypass the rest of the request and leave the buffer empty */ 11765470Smsmith if ((errno = (f->f_ops->fo_read)(f, dest, resid, &cresid))) 118124811Sjhb return (-1); 11965470Smsmith return(bcount - cresid); 12038451Smsmith } 12165470Smsmith 12265470Smsmith /* fetch more data */ 12365470Smsmith if ((errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE, &cresid))) 124124811Sjhb return (-1); 12565470Smsmith f->f_raoffset = 0; 12665470Smsmith f->f_ralen = SOPEN_RASIZE - cresid; 12765470Smsmith /* no more data, return what we had */ 12865470Smsmith if (f->f_ralen == 0) 12965470Smsmith return(bcount - resid); 13065470Smsmith } 13138451Smsmith} 132