summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/syscall/lseek.c
blob: d03687eb68d0db3b84ae3ca6b50d1503f0cf15d8 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/filio.h>

/*
 * These are defined in unistd.h - but we can't include that
 */
#define	SEEK_SET	0	/* Set file pointer to "offset" */
#define	SEEK_CUR	1	/* Set file pointer to current plus "offset" */
#define	SEEK_END	2	/* Set file pointer to EOF plus "offset" */
#define	SEEK_DATA	3	/* Set file pointer to next data past offset */
#define	SEEK_HOLE	4	/* Set file pointer to next hole past offset */

/*
 * Seek on a file
 */

#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
/*
 * Workhorse for the 32-bit seek variants: lseek32 and llseek32
 *
 * 'max' represents the maximum possible representation of offset
 * in the data type corresponding to lseek and llseek. It is
 * MAXOFF32_T for off32_t and MAXOFFSET_T for off64_t.
 * We return EOVERFLOW if we cannot represent the resulting offset
 * in the data type.
 * We provide support for character devices to be seeked beyond MAXOFF32_T
 * by lseek. To maintain compatibility in such cases lseek passes
 * the arguments carefully to lseek_common when file is not regular.
 * (/dev/kmem is a good example of a > 2Gbyte seek!)
 */
static int
lseek32_common(file_t *fp, int stype, offset_t off, offset_t max,
    offset_t *retoff)
{
	vnode_t *vp;
	struct vattr vattr;
	int error;
	u_offset_t noff;
	offset_t curoff, newoff;
	int reg;

	vp = fp->f_vnode;
	reg = (vp->v_type == VREG);

	curoff = fp->f_offset;

	switch (stype) {
	case SEEK_SET:
		noff = (u_offset_t)off;
		if (reg && noff > max) {
			error = EINVAL;
			goto out;
		}
		break;

	case SEEK_CUR:
		if (reg && off > (max - curoff)) {
			error = EOVERFLOW;
			goto out;
		}
		noff = (u_offset_t)(off + curoff);
		if (reg && noff > max) {
			error = EINVAL;
			goto out;
		}
		break;

	case SEEK_END:
		vattr.va_mask = AT_SIZE;
		if (error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred)) {
			goto out;
		}
		if (reg && (off  > (max - (offset_t)vattr.va_size))) {
			error = EOVERFLOW;
			goto out;
		}
		noff = (u_offset_t)(off + (offset_t)vattr.va_size);
		if (reg && noff > max) {
			error = EINVAL;
			goto out;
		}
		break;

	case SEEK_DATA:
		/*
		 * Get and set the file pointer to the offset of the next
		 * data past "off"
		 */
		noff = (u_offset_t)off;
		error = VOP_IOCTL(vp, _FIO_SEEK_DATA, (intptr_t)(&noff),
		    FKIOCTL, kcred, NULL);
		if (error) {
			if (error != ENOTTY)
				return (error);
			/*
			 * The ioctl is not supported, check the supplied
			 * "off" is not past the end of file
			 */
			vattr.va_mask = AT_SIZE;
			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
			if (error)
				return (error);
			if (noff >= (u_offset_t)vattr.va_size)
				return (ENXIO);
		}
		if (reg && (noff > max))
			return (EOVERFLOW);

		fp->f_offset = (offset_t)noff;
		(*retoff) = (offset_t)noff;
		return (0);

	case SEEK_HOLE:
		/*
		 * Get and set the file pointer to the offset of the next
		 * hole past "off"
		 */
		noff = (u_offset_t)off;
		error = VOP_IOCTL(vp, _FIO_SEEK_HOLE, (intptr_t)(&noff),
		    FKIOCTL, kcred, NULL);
		if (error) {
			if (error != ENOTTY)
				return (error);
			/*
			 * ioctl is not supported, if the off is valid return
			 * the "virtual hole" at the end of the file.
			 */
			vattr.va_mask = AT_SIZE;
			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
			if (error)
				return (error);
			if (off < (offset_t)vattr.va_size)
				noff = (u_offset_t)vattr.va_size;
			else
				return (ENXIO);
		}
		if (reg && (noff > max))
			return (EOVERFLOW);

		fp->f_offset = (offset_t)noff;
		(*retoff) = (offset_t)noff;
		return (0);

	default:
		error = EINVAL;
		goto out;
	}

	ASSERT((reg && noff <= max) || !reg);
	newoff = (offset_t)noff;
	if ((error = VOP_SEEK(vp, curoff, &newoff)) == 0) {
		fp->f_offset = newoff;
		(*retoff) = newoff;
		return (0);
	}
out:
	return (error);
}

off32_t
lseek32(int32_t fdes, off32_t off, int32_t stype)
{
	file_t *fp;
	int error;
	offset_t retoff;

	if ((fp = getf(fdes)) == NULL)
		return ((off32_t)set_errno(EBADF));

	/*
	 * lseek32 returns EOVERFLOW if we cannot represent the resulting
	 * offset from seek in a 32-bit off_t.
	 * The following routines are sensitive to sign extensions and
	 * calculations and if ever you change this make sure it works for
	 * special files.
	 *
	 * When VREG is not set we do the check for stype != SEEK_SET
	 * to send the unsigned value to lseek_common and not the sign
	 * extended value. (The maximum representable value is not
	 * checked by lseek_common for special files.)
	 */
	if (fp->f_vnode->v_type == VREG || stype != SEEK_SET)
		error = lseek32_common(fp, stype, (offset_t)off,
		    (offset_t)MAXOFF32_T, &retoff);
	else if (stype == SEEK_SET)
		error = lseek32_common(fp, stype, (offset_t)(uint_t)off,
		    (offset_t)(uint_t)UINT_MAX, &retoff);

	releasef(fdes);
	if (!error)
		return ((off32_t)retoff);
	return ((off32_t)set_errno(error));
}

/*
 * 64-bit seeks from 32-bit applications
 */
offset_t
llseek32(int32_t fdes, uint32_t off1, uint32_t off2, int stype)
{
	file_t *fp;
	int error;
	offset_t retoff;
#if defined(_LITTLE_ENDIAN)
	offset_t off = ((u_offset_t)off2 << 32) | (u_offset_t)off1;
#else
	offset_t off = ((u_offset_t)off1 << 32) | (u_offset_t)off2;
#endif

	if ((fp = getf(fdes)) == NULL)
		error = EBADF;
	else {
		error = lseek32_common(fp, stype, off, MAXOFFSET_T, &retoff);
		releasef(fdes);
	}

	return (error ? (offset_t)set_errno(error) : retoff);
}
#endif	/* _SYSCALL32_IMPL || _ILP32 */

#ifdef _LP64
/*
 * Seek on a file.
 *
 * Life is almost simple again (at least until we do 128-bit files ;-)
 * This is both 'lseek' and 'llseek' to a 64-bit application.
 */
off_t
lseek64(int fdes, off_t off, int stype)
{
	file_t *fp;
	vnode_t *vp;
	struct vattr vattr;
	int error;
	off_t old_off;
	offset_t new_off;

	if ((fp = getf(fdes)) == NULL)
		return ((off_t)set_errno(EBADF));

	vp = fp->f_vnode;
	new_off = off;

	switch (stype) {
	case SEEK_CUR:
		new_off += fp->f_offset;
		break;

	case SEEK_END:
		vattr.va_mask = AT_SIZE;
		if ((error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred)) != 0)
			goto lseek64error;
		new_off += vattr.va_size;
		break;

	case SEEK_SET:
		break;

	case SEEK_DATA:
		/*
		 * Get and set the file pointer to the offset of the next
		 * data past "off"
		 */
		new_off = (offset_t)off;
		error = VOP_IOCTL(vp, _FIO_SEEK_DATA, (intptr_t)(&new_off),
		    FKIOCTL, kcred, NULL);
		if (error) {
			if (error != ENOTTY) {
				goto lseek64error;
			}
			/*
			 * The ioctl is not supported, check the supplied off
			 * is not past end of file
			 */
			vattr.va_mask = AT_SIZE;
			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
			if (error)
				goto lseek64error;
			if (new_off >= (offset_t)vattr.va_size) {
				error = ENXIO;
				goto lseek64error;
			}
		}
		fp->f_offset = new_off;
		releasef(fdes);
		return (new_off);

	case SEEK_HOLE:
		/*
		 * Get and set the file pointer to the offset of the next
		 * hole past "off"
		 */
		new_off = off;
		error = VOP_IOCTL(vp, _FIO_SEEK_HOLE, (intptr_t)(&new_off),
		    FKIOCTL, kcred, NULL);
		if (error) {
			if (error != ENOTTY)
				goto lseek64error;
			/*
			 * ioctl is not supported, if the off is valid return
			 * the "virtual hole" at the end of the file.
			 */
			vattr.va_mask = AT_SIZE;
			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
			if (error)
				goto lseek64error;
			if (off < (offset_t)vattr.va_size) {
				new_off = (offset_t)vattr.va_size;
			} else {
				error = ENXIO;
				goto lseek64error;
			}
		}
		fp->f_offset = new_off;
		releasef(fdes);
		return (new_off);

	default:
		error = EINVAL;
		goto lseek64error;
	}

	old_off = fp->f_offset;
	if ((error = VOP_SEEK(vp, old_off, &new_off)) == 0) {
		fp->f_offset = new_off;
		releasef(fdes);
		return (new_off);
	}

lseek64error:
	releasef(fdes);
	return ((off_t)set_errno(error));
}
#endif	/* _LP64 */