read.c (7416e819ad3cc6fac1c547e9e587d4226a81ee96) read.c (313724bab940c1844fda3d797cf88cd46780e62a)
1/* $NetBSD: read.c,v 1.8 1997/01/22 00:38:12 cgd Exp $ */
2
3/*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.

--- 58 unchanged lines hidden (view full) ---

67#include "stand.h"
68
69ssize_t
70read(int fd, void *dest, size_t bcount)
71{
72 struct open_file *f = &files[fd];
73 size_t resid;
74
1/* $NetBSD: read.c,v 1.8 1997/01/22 00:38:12 cgd Exp $ */
2
3/*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.

--- 58 unchanged lines hidden (view full) ---

67#include "stand.h"
68
69ssize_t
70read(int fd, void *dest, size_t bcount)
71{
72 struct open_file *f = &files[fd];
73 size_t resid;
74
75 TSENTER();
76
75 if ((unsigned)fd >= SOPEN_MAX || !(f->f_flags & F_READ)) {
76 errno = EBADF;
77 return (-1);
78 }
79 if (f->f_flags & F_RAW) {
80 twiddle(4);
81 errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
82 btodb(f->f_offset), bcount, dest, &resid);
83 if (errno)
84 return (-1);
85 f->f_offset += resid;
77 if ((unsigned)fd >= SOPEN_MAX || !(f->f_flags & F_READ)) {
78 errno = EBADF;
79 return (-1);
80 }
81 if (f->f_flags & F_RAW) {
82 twiddle(4);
83 errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
84 btodb(f->f_offset), bcount, dest, &resid);
85 if (errno)
86 return (-1);
87 f->f_offset += resid;
88 TSEXIT();
86 return (resid);
87 }
88
89 /*
90 * Optimise reads from regular files using a readahead buffer.
91 * If the request can't be satisfied from the current buffer contents,
92 * check to see if it should be bypassed, or refill the buffer and
93 * complete the request.
94 */
95 resid = bcount;
96 for (;;) {
97 size_t ccount, cresid;
98 /* how much can we supply? */
99 ccount = imin(f->f_ralen, resid);
100 if (ccount > 0) {
101 bcopy(f->f_rabuf + f->f_raoffset, dest, ccount);
102 f->f_raoffset += ccount;
103 f->f_ralen -= ccount;
104 resid -= ccount;
89 return (resid);
90 }
91
92 /*
93 * Optimise reads from regular files using a readahead buffer.
94 * If the request can't be satisfied from the current buffer contents,
95 * check to see if it should be bypassed, or refill the buffer and
96 * complete the request.
97 */
98 resid = bcount;
99 for (;;) {
100 size_t ccount, cresid;
101 /* how much can we supply? */
102 ccount = imin(f->f_ralen, resid);
103 if (ccount > 0) {
104 bcopy(f->f_rabuf + f->f_raoffset, dest, ccount);
105 f->f_raoffset += ccount;
106 f->f_ralen -= ccount;
107 resid -= ccount;
105 if (resid == 0)
108 if (resid == 0) {
109 TSEXIT();
106 return (bcount);
110 return (bcount);
111 }
107 dest = (char *)dest + ccount;
108 }
109
110 /* will filling the readahead buffer again not help? */
111 if (f->f_rabuf == NULL || resid >= SOPEN_RASIZE) {
112 /*
113 * bypass the rest of the request and leave the
114 * buffer empty
115 */
116 errno = (f->f_ops->fo_read)(f, dest, resid, &cresid);
117 if (errno != 0)
118 return (-1);
112 dest = (char *)dest + ccount;
113 }
114
115 /* will filling the readahead buffer again not help? */
116 if (f->f_rabuf == NULL || resid >= SOPEN_RASIZE) {
117 /*
118 * bypass the rest of the request and leave the
119 * buffer empty
120 */
121 errno = (f->f_ops->fo_read)(f, dest, resid, &cresid);
122 if (errno != 0)
123 return (-1);
124 TSEXIT();
119 return (bcount - cresid);
120 }
121
122 /* fetch more data */
123 errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE,
124 &cresid);
125 if (errno != 0)
126 return (-1);
127 f->f_raoffset = 0;
128 f->f_ralen = SOPEN_RASIZE - cresid;
129 /* no more data, return what we had */
125 return (bcount - cresid);
126 }
127
128 /* fetch more data */
129 errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE,
130 &cresid);
131 if (errno != 0)
132 return (-1);
133 f->f_raoffset = 0;
134 f->f_ralen = SOPEN_RASIZE - cresid;
135 /* no more data, return what we had */
130 if (f->f_ralen == 0)
136 if (f->f_ralen == 0) {
137 TSEXIT();
131 return (bcount - resid);
138 return (bcount - resid);
139 }
132 }
133}
140 }
141}