xref: /titanic_51/usr/src/uts/common/syscall/lseek.c (revision c2580b931007758eab8cb5ae8726ebe1588e259b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/cred.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/debug.h>
47 #include <sys/cmn_err.h>
48 #include <sys/filio.h>
49 
50 /*
51  * These are defined in unistd.h - but we can't include that
52  */
53 #define	SEEK_SET	0	/* Set file pointer to "offset" */
54 #define	SEEK_CUR	1	/* Set file pointer to current plus "offset" */
55 #define	SEEK_END	2	/* Set file pointer to EOF plus "offset" */
56 #define	SEEK_DATA	3	/* Set file pointer to next data past offset */
57 #define	SEEK_HOLE	4	/* Set file pointer to next hole past offset */
58 
59 /*
60  * Seek on a file
61  */
62 
63 #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
64 /*
65  * Workhorse for the 32-bit seek variants: lseek32 and llseek32
66  *
67  * 'max' represents the maximum possible representation of offset
68  * in the data type corresponding to lseek and llseek. It is
69  * MAXOFF32_T for off32_t and MAXOFFSET_T for off64_t.
70  * We return EOVERFLOW if we cannot represent the resulting offset
71  * in the data type.
72  * We provide support for character devices to be seeked beyond MAXOFF32_T
73  * by lseek. To maintain compatibility in such cases lseek passes
74  * the arguments carefully to lseek_common when file is not regular.
75  * (/dev/kmem is a good example of a > 2Gbyte seek!)
76  */
77 static int
78 lseek32_common(file_t *fp, int stype, offset_t off, offset_t max,
79     offset_t *retoff)
80 {
81 	vnode_t *vp;
82 	struct vattr vattr;
83 	int error;
84 	u_offset_t noff;
85 	offset_t curoff, newoff;
86 	int reg;
87 
88 	vp = fp->f_vnode;
89 	reg = (vp->v_type == VREG);
90 
91 	curoff = fp->f_offset;
92 
93 	switch (stype) {
94 	case SEEK_SET:
95 		noff = (u_offset_t)off;
96 		if (reg && noff > max) {
97 			error = EINVAL;
98 			goto out;
99 		}
100 		break;
101 
102 	case SEEK_CUR:
103 		if (reg && off > (max - curoff)) {
104 			error = EOVERFLOW;
105 			goto out;
106 		}
107 		noff = (u_offset_t)(off + curoff);
108 		if (reg && noff > max) {
109 			error = EINVAL;
110 			goto out;
111 		}
112 		break;
113 
114 	case SEEK_END:
115 		vattr.va_mask = AT_SIZE;
116 		if (error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred)) {
117 			goto out;
118 		}
119 		if (reg && (off  > (max - (offset_t)vattr.va_size))) {
120 			error = EOVERFLOW;
121 			goto out;
122 		}
123 		noff = (u_offset_t)(off + (offset_t)vattr.va_size);
124 		if (reg && noff > max) {
125 			error = EINVAL;
126 			goto out;
127 		}
128 		break;
129 
130 	case SEEK_DATA:
131 		/*
132 		 * Get and set the file pointer to the offset of the next
133 		 * data past "off"
134 		 */
135 		noff = (u_offset_t)off;
136 		error = VOP_IOCTL(vp, _FIO_SEEK_DATA, (intptr_t)(&noff),
137 		    FKIOCTL, kcred, NULL);
138 		if (error) {
139 			if (error != ENOTTY)
140 				return (error);
141 			/*
142 			 * The ioctl is not supported, check the supplied
143 			 * "off" is not past the end of file
144 			 */
145 			vattr.va_mask = AT_SIZE;
146 			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
147 			if (error)
148 				return (error);
149 			if (noff >= (u_offset_t)vattr.va_size)
150 				return (ENXIO);
151 		}
152 		if (reg && (noff > max))
153 			return (EOVERFLOW);
154 
155 		fp->f_offset = (offset_t)noff;
156 		(*retoff) = (offset_t)noff;
157 		return (0);
158 
159 	case SEEK_HOLE:
160 		/*
161 		 * Get and set the file pointer to the offset of the next
162 		 * hole past "off"
163 		 */
164 		noff = (u_offset_t)off;
165 		error = VOP_IOCTL(vp, _FIO_SEEK_HOLE, (intptr_t)(&noff),
166 		    FKIOCTL, kcred, NULL);
167 		if (error) {
168 			if (error != ENOTTY)
169 				return (error);
170 			/*
171 			 * ioctl is not supported, if the off is valid return
172 			 * the "virtual hole" at the end of the file.
173 			 */
174 			vattr.va_mask = AT_SIZE;
175 			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
176 			if (error)
177 				return (error);
178 			if (off < (offset_t)vattr.va_size)
179 				noff = (u_offset_t)vattr.va_size;
180 			else
181 				return (ENXIO);
182 		}
183 		if (reg && (noff > max))
184 			return (EOVERFLOW);
185 
186 		fp->f_offset = (offset_t)noff;
187 		(*retoff) = (offset_t)noff;
188 		return (0);
189 
190 	default:
191 		error = EINVAL;
192 		goto out;
193 	}
194 
195 	ASSERT((reg && noff <= max) || !reg);
196 	newoff = (offset_t)noff;
197 	if ((error = VOP_SEEK(vp, curoff, &newoff)) == 0) {
198 		fp->f_offset = newoff;
199 		(*retoff) = newoff;
200 		return (0);
201 	}
202 out:
203 	return (error);
204 }
205 
206 off32_t
207 lseek32(int32_t fdes, off32_t off, int32_t stype)
208 {
209 	file_t *fp;
210 	int error;
211 	offset_t retoff;
212 
213 	if ((fp = getf(fdes)) == NULL)
214 		return ((off32_t)set_errno(EBADF));
215 
216 	/*
217 	 * lseek32 returns EOVERFLOW if we cannot represent the resulting
218 	 * offset from seek in a 32-bit off_t.
219 	 * The following routines are sensitive to sign extensions and
220 	 * calculations and if ever you change this make sure it works for
221 	 * special files.
222 	 *
223 	 * When VREG is not set we do the check for stype != SEEK_SET
224 	 * to send the unsigned value to lseek_common and not the sign
225 	 * extended value. (The maximum representable value is not
226 	 * checked by lseek_common for special files.)
227 	 */
228 	if (fp->f_vnode->v_type == VREG || stype != SEEK_SET)
229 		error = lseek32_common(fp, stype, (offset_t)off,
230 		    (offset_t)MAXOFF32_T, &retoff);
231 	else if (stype == SEEK_SET)
232 		error = lseek32_common(fp, stype, (offset_t)(uint_t)off,
233 		    (offset_t)(uint_t)UINT_MAX, &retoff);
234 
235 	releasef(fdes);
236 	if (!error)
237 		return ((off32_t)retoff);
238 	return ((off32_t)set_errno(error));
239 }
240 
241 /*
242  * 64-bit seeks from 32-bit applications
243  */
244 offset_t
245 llseek32(int32_t fdes, uint32_t off1, uint32_t off2, int stype)
246 {
247 	file_t *fp;
248 	int error;
249 	offset_t retoff;
250 #if defined(_LITTLE_ENDIAN)
251 	offset_t off = ((u_offset_t)off2 << 32) | (u_offset_t)off1;
252 #else
253 	offset_t off = ((u_offset_t)off1 << 32) | (u_offset_t)off2;
254 #endif
255 
256 	if ((fp = getf(fdes)) == NULL)
257 		error = EBADF;
258 	else {
259 		error = lseek32_common(fp, stype, off, MAXOFFSET_T, &retoff);
260 		releasef(fdes);
261 	}
262 
263 	return (error ? (offset_t)set_errno(error) : retoff);
264 }
265 #endif	/* _SYSCALL32_IMPL || _ILP32 */
266 
267 #ifdef _LP64
268 /*
269  * Seek on a file.
270  *
271  * Life is almost simple again (at least until we do 128-bit files ;-)
272  * This is both 'lseek' and 'llseek' to a 64-bit application.
273  */
274 off_t
275 lseek64(int fdes, off_t off, int stype)
276 {
277 	file_t *fp;
278 	vnode_t *vp;
279 	struct vattr vattr;
280 	int error;
281 	off_t old_off;
282 	offset_t new_off;
283 
284 	if ((fp = getf(fdes)) == NULL)
285 		return ((off_t)set_errno(EBADF));
286 
287 	vp = fp->f_vnode;
288 	new_off = off;
289 
290 	switch (stype) {
291 	case SEEK_CUR:
292 		new_off += fp->f_offset;
293 		break;
294 
295 	case SEEK_END:
296 		vattr.va_mask = AT_SIZE;
297 		if ((error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred)) != 0)
298 			goto lseek64error;
299 		new_off += vattr.va_size;
300 		break;
301 
302 	case SEEK_SET:
303 		break;
304 
305 	case SEEK_DATA:
306 		/*
307 		 * Get and set the file pointer to the offset of the next
308 		 * data past "off"
309 		 */
310 		new_off = (offset_t)off;
311 		error = VOP_IOCTL(vp, _FIO_SEEK_DATA, (intptr_t)(&new_off),
312 		    FKIOCTL, kcred, NULL);
313 		if (error) {
314 			if (error != ENOTTY) {
315 				goto lseek64error;
316 			}
317 			/*
318 			 * The ioctl is not supported, check the supplied off
319 			 * is not past end of file
320 			 */
321 			vattr.va_mask = AT_SIZE;
322 			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
323 			if (error)
324 				goto lseek64error;
325 			if (new_off >= (offset_t)vattr.va_size) {
326 				error = ENXIO;
327 				goto lseek64error;
328 			}
329 		}
330 		fp->f_offset = new_off;
331 		releasef(fdes);
332 		return (new_off);
333 
334 	case SEEK_HOLE:
335 		/*
336 		 * Get and set the file pointer to the offset of the next
337 		 * hole past "off"
338 		 */
339 		new_off = off;
340 		error = VOP_IOCTL(vp, _FIO_SEEK_HOLE, (intptr_t)(&new_off),
341 		    FKIOCTL, kcred, NULL);
342 		if (error) {
343 			if (error != ENOTTY)
344 				goto lseek64error;
345 			/*
346 			 * ioctl is not supported, if the off is valid return
347 			 * the "virtual hole" at the end of the file.
348 			 */
349 			vattr.va_mask = AT_SIZE;
350 			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
351 			if (error)
352 				goto lseek64error;
353 			if (off < (offset_t)vattr.va_size) {
354 				new_off = (offset_t)vattr.va_size;
355 			} else {
356 				error = ENXIO;
357 				goto lseek64error;
358 			}
359 		}
360 		fp->f_offset = new_off;
361 		releasef(fdes);
362 		return (new_off);
363 
364 	default:
365 		error = EINVAL;
366 		goto lseek64error;
367 	}
368 
369 	old_off = fp->f_offset;
370 	if ((error = VOP_SEEK(vp, old_off, &new_off)) == 0) {
371 		fp->f_offset = new_off;
372 		releasef(fdes);
373 		return (new_off);
374 	}
375 
376 lseek64error:
377 	releasef(fdes);
378 	return ((off_t)set_errno(error));
379 }
380 #endif	/* _LP64 */
381