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 * 4. Neither the name of the University nor the names of its contributors 1938451Smsmith * may be used to endorse or promote products derived from this software 2038451Smsmith * without specific prior written permission. 2138451Smsmith * 2238451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2338451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2438451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2538451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2638451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2738451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2838451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2938451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3038451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3138451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3238451Smsmith * SUCH DAMAGE. 3338451Smsmith * 3438451Smsmith * @(#)read.c 8.1 (Berkeley) 6/11/93 3538451Smsmith * 36344291Skevans * 3738451Smsmith * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University 3838451Smsmith * All Rights Reserved. 3938451Smsmith * 4038451Smsmith * Author: Alessandro Forin 41344291Skevans * 4238451Smsmith * Permission to use, copy, modify and distribute this software and its 4338451Smsmith * documentation is hereby granted, provided that both the copyright 4438451Smsmith * notice and this permission notice appear in all copies of the 4538451Smsmith * software, derivative works or modified versions, and any portions 4638451Smsmith * thereof, and that both notices appear in supporting documentation. 47344291Skevans * 4838451Smsmith * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 4938451Smsmith * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 5038451Smsmith * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51344291Skevans * 5238451Smsmith * Carnegie Mellon requests users of this software to return to 53344291Skevans * 5438451Smsmith * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 5538451Smsmith * School of Computer Science 5638451Smsmith * Carnegie Mellon University 5738451Smsmith * Pittsburgh PA 15213-3890 58344291Skevans * 5938451Smsmith * any improvements or extensions that they make and grant Carnegie the 6038451Smsmith * rights to redistribute these changes. 6138451Smsmith */ 6238451Smsmith 6384221Sdillon#include <sys/cdefs.h> 6484221Sdillon__FBSDID("$FreeBSD: stable/11/stand/libsa/read.c 344291 2019-02-19 18:50:20Z kevans $"); 6584221Sdillon 6638451Smsmith#include <sys/param.h> 6738451Smsmith#include "stand.h" 6838451Smsmith 6938451Smsmithssize_t 7065470Smsmithread(int fd, void *dest, size_t bcount) 7138451Smsmith{ 72344291Skevans struct open_file *f = &files[fd]; 73344291Skevans size_t resid; 7438451Smsmith 75344291Skevans if ((unsigned)fd >= SOPEN_MAX || !(f->f_flags & F_READ)) { 76344291Skevans errno = EBADF; 77344291Skevans return (-1); 7838451Smsmith } 79344291Skevans if (f->f_flags & F_RAW) { 80344291Skevans twiddle(4); 81344291Skevans errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 82344291Skevans btodb(f->f_offset), bcount, dest, &resid); 83344291Skevans if (errno) 84344291Skevans return (-1); 85344291Skevans f->f_offset += resid; 86344291Skevans return (resid); 87344291Skevans } 8865470Smsmith 89344291Skevans /* 90344291Skevans * Optimise reads from regular files using a readahead buffer. 91344291Skevans * If the request can't be satisfied from the current buffer contents, 92344291Skevans * check to see if it should be bypassed, or refill the buffer and 93344291Skevans * complete the request. 94344291Skevans */ 95344291Skevans resid = bcount; 96344291Skevans for (;;) { 97344291Skevans size_t ccount, cresid; 98344291Skevans /* how much can we supply? */ 99344291Skevans ccount = imin(f->f_ralen, resid); 100344291Skevans if (ccount > 0) { 101344291Skevans bcopy(f->f_rabuf + f->f_raoffset, dest, ccount); 102344291Skevans f->f_raoffset += ccount; 103344291Skevans f->f_ralen -= ccount; 104344291Skevans resid -= ccount; 105344291Skevans if (resid == 0) 106344291Skevans return (bcount); 107344291Skevans dest = (char *)dest + ccount; 108344291Skevans } 109344291Skevans 110344291Skevans /* will filling the readahead buffer again not help? */ 111344291Skevans if (f->f_rabuf == NULL || resid >= SOPEN_RASIZE) { 112344291Skevans /* 113344291Skevans * bypass the rest of the request and leave the 114344291Skevans * buffer empty 115344291Skevans */ 116344291Skevans errno = (f->f_ops->fo_read)(f, dest, resid, &cresid); 117344291Skevans if (errno != 0) 118344291Skevans return (-1); 119344291Skevans return (bcount - cresid); 120344291Skevans } 121344291Skevans 122344291Skevans /* fetch more data */ 123344291Skevans errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE, 124344291Skevans &cresid); 125344291Skevans if (errno != 0) 126344291Skevans return (-1); 127344291Skevans f->f_raoffset = 0; 128344291Skevans f->f_ralen = SOPEN_RASIZE - cresid; 129344291Skevans /* no more data, return what we had */ 130344291Skevans if (f->f_ralen == 0) 131344291Skevans return (bcount - resid); 13238451Smsmith } 13338451Smsmith} 134