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