summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/sfio/sfrd.c
blob: 0eb881b19b37205c7a13b2b27a9e100c46a0bf5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#include	"sfhdr.h"

/*	Internal function to do a hard read.
**	This knows about discipline and memory mapping, peek read.
**
**	Written by Kiem-Phong Vo.
*/

/* synchronize unseekable write streams */
#if __STD_C
static void _sfwrsync(void)
#else
static void _sfwrsync()
#endif
{	reg Sfpool_t*	p;
	reg Sfio_t*	f;
	reg int		n;

	/* sync all pool heads */
	for(p = _Sfpool.next; p; p = p->next)
	{	if(p->n_sf <= 0)
			continue;
		f = p->sf[0];
		if(!SFFROZEN(f) && f->next > f->data &&
		   (f->mode&SF_WRITE) && f->extent < 0 )
			(void)_sfflsbuf(f,-1);
	}

	/* and all the ones in the discrete pool */
	for(n = 0; n < _Sfpool.n_sf; ++n)
	{	f = _Sfpool.sf[n];

		if(!SFFROZEN(f) && f->next > f->data &&
		   (f->mode&SF_WRITE) && f->extent < 0 )
			(void)_sfflsbuf(f,-1);
	}
}

#if __STD_C
ssize_t sfrd(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc)
#else
ssize_t sfrd(f,buf,n,disc)
Sfio_t*		f;
Void_t*		buf;
size_t		n;
Sfdisc_t*	disc;
#endif
{
	Sfoff_t		r;
	reg Sfdisc_t*	dc;
	reg int		local, rcrv, dosync, oerrno;
	SFMTXDECL(f);

	SFMTXENTER(f,-1);

	GETLOCAL(f,local);
	if((rcrv = f->mode & (SF_RC|SF_RV)) )
		f->mode &= ~(SF_RC|SF_RV);
	f->bits &= ~SF_JUSTSEEK;

	if(f->mode&SF_PKRD)
		SFMTXRETURN(f, -1);

	if(!local && !(f->bits&SF_DCDOWN)) /* an external user's call */
	{	if(f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
			SFMTXRETURN(f, -1);
		if(f->next < f->endb)
		{	if(SFSYNC(f) < 0)
				SFMTXRETURN(f, -1);
			if((f->mode&(SF_SYNCED|SF_READ)) == (SF_SYNCED|SF_READ) )
			{	f->endb = f->next = f->endr = f->data;
				f->mode &= ~SF_SYNCED;
			}
#ifdef MAP_TYPE
			if((f->bits&SF_MMAP) && f->data)
			{	SFMUNMAP(f, f->data, f->endb-f->data);
				f->data = NIL(uchar*);
			}
#endif
			f->next = f->endb = f->endr = f->endw = f->data;
		}
	}

	for(dosync = 0;;)
	{	/* stream locked by sfsetfd() */
		if(!(f->flags&SF_STRING) && f->file < 0)
			SFMTXRETURN(f, 0);

		f->flags &= ~(SF_EOF|SF_ERROR);

		dc = disc;
		if(f->flags&SF_STRING)
		{	if((r = (f->data+f->extent) - f->next) < 0)
				r = 0;
			if(r <= 0)
				goto do_except;
			SFMTXRETURN(f, (ssize_t)r);
		}

		/* warn that a read is about to happen */
		SFDISC(f,dc,readf);
		if(dc && dc->exceptf && (f->flags&SF_IOCHECK) )
		{	reg int	rv;
			if(local)
				SETLOCAL(f);
			if((rv = _sfexcept(f,SF_READ,n,dc)) > 0)
				n = rv;
			else if(rv < 0)
			{	f->flags |= SF_ERROR;
				SFMTXRETURN(f, (ssize_t)rv);
			}
		}

#ifdef MAP_TYPE
		if(f->bits&SF_MMAP)
		{	reg ssize_t	a, round;
			sfstat_t	st;

			/* determine if we have to copy data to buffer */
			if((uchar*)buf >= f->data && (uchar*)buf <= f->endb)
			{	n += f->endb - f->next;
				buf = NIL(char*);
			}

			/* actual seek location */
			if((f->flags&(SF_SHARE|SF_PUBLIC)) == (SF_SHARE|SF_PUBLIC) &&
			   (r = SFSK(f,(Sfoff_t)0,SEEK_CUR,dc)) != f->here)
				f->here = r;
			else	f->here -= f->endb-f->next;

			/* before mapping, make sure we have data to map */
			if((f->flags&SF_SHARE) || (size_t)(r = f->extent-f->here) < n)
			{	if((r = sysfstatf(f->file,&st)) < 0)
					goto do_except;
				if((r = (f->extent = st.st_size) - f->here) <= 0 )
				{	r = 0;	/* eof */
					goto do_except;
				}
			}

			/* make sure current position is page aligned */
			if((a = (size_t)(f->here%_Sfpage)) != 0)
			{	f->here -= a;
				r += a;
			}

			/* map minimal requirement */
			if(r > (round = (1 + (n+a)/f->size)*f->size) )
				r = round;

			if(f->data)
				SFMUNMAP(f, f->data, f->endb-f->data);

			for(;;)
			{	f->data = (uchar*) sysmmapf((caddr_t)0, (size_t)r,
							(PROT_READ|PROT_WRITE),
							MAP_PRIVATE,
							f->file, (sfoff_t)f->here);
				if(f->data && (caddr_t)f->data != (caddr_t)(-1))
					break;
				else
				{	f->data = NIL(uchar*);
					if((r >>= 1) < (_Sfpage*SF_NMAP) ||
					   (errno != EAGAIN && errno != ENOMEM) )
						break;
				}
			}

			if(f->data)
			{	if(f->bits&SF_SEQUENTIAL)
					SFMMSEQON(f,f->data,r);
				f->next = f->data+a;
				f->endr = f->endb = f->data+r;
				f->endw = f->data;
				f->here += r;

				/* make known our seek location */
				(void)SFSK(f,f->here,SEEK_SET,dc);

				if(buf)
				{	if(n > (size_t)(r-a))
						n = (ssize_t)(r-a);
					memcpy(buf,f->next,n);
					f->next += n;
				}
				else	n = f->endb - f->next;

				SFMTXRETURN(f, n);
			}
			else
			{	r = -1;
				f->here += a;

				/* reset seek pointer to its physical location */
				(void)SFSK(f,f->here,SEEK_SET,dc);

				/* make a buffer */
				(void)SFSETBUF(f,(Void_t*)f->tiny,(size_t)SF_UNBOUND);

				if(!buf)
				{	buf = (Void_t*)f->data;
					n = f->size;
				}
			}
		}
#endif

		/* sync unseekable write streams to prevent deadlock */
		if(!dosync && f->extent < 0)
		{	dosync = 1;
			_sfwrsync();
		}

		/* make sure file pointer is right */
		if(f->extent >= 0 && (f->flags&SF_SHARE) )
		{	if(!(f->flags&SF_PUBLIC) )
				f->here = SFSK(f,f->here,SEEK_SET,dc);
			else	f->here = SFSK(f,(Sfoff_t)0,SEEK_CUR,dc);
		}

		oerrno = errno;
		errno = 0;

		if(dc && dc->readf)
		{	int	share = f->flags&SF_SHARE;

			if(rcrv) /* pass on rcrv for possible continuations */
				f->mode |= rcrv;
				/* tell readf that no peeking necessary */
			else	f->flags &= ~SF_SHARE;

			SFDCRD(f,buf,n,dc,r);

			/* reset flags */
			if(rcrv)
				f->mode &= ~rcrv;
			else	f->flags |= share;
		}
		else if(SFISNULL(f))
			r = 0;
		else if(f->extent < 0 && (f->flags&SF_SHARE) && rcrv)
		{	/* try peek read */
			r = sfpkrd(f->file, (char*)buf, n,
				    (rcrv&SF_RC) ? (int)f->getr : -1,
				    -1L, (rcrv&SF_RV) ? 1 : 0);
			if(r > 0)
			{	if(rcrv&SF_RV)
					f->mode |= SF_PKRD;
				else	f->mode |= SF_RC;
			}
		}
		else	r = sysreadf(f->file,buf,n);

		if(errno == 0 )
			errno = oerrno;

		if(r > 0 )
		{	if(!(f->bits&SF_DCDOWN) )	/* not a continuation call */
			{	if(!(f->mode&SF_PKRD) )
				{	f->here += r;
					if(f->extent >= 0 && f->extent < f->here)
						f->extent = f->here;
				}
				if((uchar*)buf >= f->data &&
				   (uchar*)buf < f->data+f->size)
					f->endb = f->endr = ((uchar*)buf) + r;
			}

			SFMTXRETURN(f, (ssize_t)r);
		}

	do_except:
		if(local)
			SETLOCAL(f);
		switch(_sfexcept(f,SF_READ,(ssize_t)r,dc))
		{
		case SF_ECONT :
			goto do_continue;
		case SF_EDONE :
			n = local ? 0 : (ssize_t)r;
			SFMTXRETURN(f,n);
		case SF_EDISC :
			if(!local && !(f->flags&SF_STRING))
				goto do_continue;
			/* else fall thru */
		case SF_ESTACK :
			SFMTXRETURN(f, -1);
		}

	do_continue:
		for(dc = f->disc; dc; dc = dc->disc)
			if(dc == disc)
				break;
		disc = dc;
	}
}