xref: /illumos-gate/usr/src/boot/libsa/read.c (revision 22028508)
1199767f8SToomas Soome /*	$NetBSD: read.c,v 1.8 1997/01/22 00:38:12 cgd Exp $	*/
2199767f8SToomas Soome 
3692bf522SToomas Soome /*
4199767f8SToomas Soome  * Copyright (c) 1993
5199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * This code is derived from software contributed to Berkeley by
8199767f8SToomas Soome  * The Mach Operating System project at Carnegie-Mellon University.
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
18692bf522SToomas Soome  * 3. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
20199767f8SToomas Soome  *    without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome  * SUCH DAMAGE.
33199767f8SToomas Soome  *
34199767f8SToomas Soome  *	@(#)read.c	8.1 (Berkeley) 6/11/93
35692bf522SToomas Soome  *
36199767f8SToomas Soome  *
37199767f8SToomas Soome  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38199767f8SToomas Soome  * All Rights Reserved.
39199767f8SToomas Soome  *
40199767f8SToomas Soome  * Author: Alessandro Forin
41692bf522SToomas Soome  *
42199767f8SToomas Soome  * Permission to use, copy, modify and distribute this software and its
43199767f8SToomas Soome  * documentation is hereby granted, provided that both the copyright
44199767f8SToomas Soome  * notice and this permission notice appear in all copies of the
45199767f8SToomas Soome  * software, derivative works or modified versions, and any portions
46199767f8SToomas Soome  * thereof, and that both notices appear in supporting documentation.
47692bf522SToomas Soome  *
48199767f8SToomas Soome  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49199767f8SToomas Soome  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50199767f8SToomas Soome  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51692bf522SToomas Soome  *
52199767f8SToomas Soome  * Carnegie Mellon requests users of this software to return to
53692bf522SToomas Soome  *
54199767f8SToomas Soome  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55199767f8SToomas Soome  *  School of Computer Science
56199767f8SToomas Soome  *  Carnegie Mellon University
57199767f8SToomas Soome  *  Pittsburgh PA 15213-3890
58692bf522SToomas Soome  *
59199767f8SToomas Soome  * any improvements or extensions that they make and grant Carnegie the
60199767f8SToomas Soome  * rights to redistribute these changes.
61199767f8SToomas Soome  */
62199767f8SToomas Soome 
63199767f8SToomas Soome #include <sys/cdefs.h>
64199767f8SToomas Soome 
65199767f8SToomas Soome #include <sys/param.h>
66199767f8SToomas Soome #include "stand.h"
67199767f8SToomas Soome 
68199767f8SToomas Soome ssize_t
read(int fd,void * dest,size_t bcount)69199767f8SToomas Soome read(int fd, void *dest, size_t bcount)
70199767f8SToomas Soome {
71a2027c5dSToomas Soome 	struct open_file *f;
72692bf522SToomas Soome 	size_t resid;
73199767f8SToomas Soome 
74a2027c5dSToomas Soome 	f = fd2open_file(fd);
75a2027c5dSToomas Soome 	if (f == NULL || !(f->f_flags & F_READ)) {
76692bf522SToomas Soome 		errno = EBADF;
77692bf522SToomas Soome 		return (-1);
78199767f8SToomas Soome 	}
79692bf522SToomas Soome 	if (f->f_flags & F_RAW) {
80692bf522SToomas Soome 		twiddle(8);
81692bf522SToomas Soome 		errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
82692bf522SToomas Soome 		    btodb(f->f_offset), bcount, dest, &resid);
83692bf522SToomas Soome 		if (errno)
84692bf522SToomas Soome 			return (-1);
85692bf522SToomas Soome 		f->f_offset += resid;
86692bf522SToomas Soome 		return (resid);
87199767f8SToomas Soome 	}
88199767f8SToomas Soome 
89692bf522SToomas Soome 	/*
90692bf522SToomas Soome 	 * Optimise reads from regular files using a readahead buffer.
91692bf522SToomas Soome 	 * If the request can't be satisfied from the current buffer contents,
92692bf522SToomas Soome 	 * check to see if it should be bypassed, or refill the buffer and
93692bf522SToomas Soome 	 * complete the request.
94692bf522SToomas Soome 	 */
95692bf522SToomas Soome 	resid = bcount;
96692bf522SToomas Soome 	for (;;) {
97692bf522SToomas Soome 		size_t	ccount, cresid;
98692bf522SToomas Soome 		/* how much can we supply? */
99692bf522SToomas Soome 		ccount = imin(f->f_ralen, resid);
100692bf522SToomas Soome 		if (ccount > 0) {
101692bf522SToomas Soome 			bcopy(f->f_rabuf + f->f_raoffset, dest, ccount);
102692bf522SToomas Soome 			f->f_raoffset += ccount;
103692bf522SToomas Soome 			f->f_ralen -= ccount;
104692bf522SToomas Soome 			resid -= ccount;
105692bf522SToomas Soome 			if (resid == 0)
106692bf522SToomas Soome 				return (bcount);
107692bf522SToomas Soome 			dest = (char *)dest + ccount;
108692bf522SToomas Soome 		}
109692bf522SToomas Soome 
110692bf522SToomas Soome 		/* will filling the readahead buffer again not help? */
111692bf522SToomas Soome 		if (f->f_rabuf == NULL || resid >= SOPEN_RASIZE) {
112692bf522SToomas Soome 			/*
113692bf522SToomas Soome 			 * bypass the rest of the request and leave the
114692bf522SToomas Soome 			 * buffer empty
115692bf522SToomas Soome 			 */
116692bf522SToomas Soome 			errno = (f->f_ops->fo_read)(f, dest, resid, &cresid);
117692bf522SToomas Soome 			if (errno != 0)
118692bf522SToomas Soome 				return (-1);
119692bf522SToomas Soome 			return (bcount - cresid);
120692bf522SToomas Soome 		}
121692bf522SToomas Soome 
122692bf522SToomas Soome 		/* fetch more data */
123692bf522SToomas Soome 		errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE,
124692bf522SToomas Soome 		    &cresid);
125692bf522SToomas Soome 		if (errno != 0)
126692bf522SToomas Soome 			return (-1);
127692bf522SToomas Soome 		f->f_raoffset = 0;
128692bf522SToomas Soome 		f->f_ralen = SOPEN_RASIZE - cresid;
129692bf522SToomas Soome 		/* no more data, return what we had */
130692bf522SToomas Soome 		if (f->f_ralen == 0)
131692bf522SToomas Soome 			return (bcount - resid);
132692bf522SToomas Soome 	}
133199767f8SToomas Soome }
134