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]
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>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/vnode.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 */
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
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!)
77 lseek32_common(file_t
*fp
, int stype
, offset_t off
, offset_t max
,
84 offset_t curoff
, newoff
;
88 reg
= (vp
->v_type
== VREG
);
90 curoff
= fp
->f_offset
;
95 if (reg
&& noff
> max
) {
102 if (reg
&& off
> (max
- curoff
)) {
106 noff
= (uoff_t
)(off
+ curoff
);
107 if (reg
&& noff
> max
) {
114 vattr
.va_mask
= AT_SIZE
;
115 if (error
= fop_getattr(vp
, &vattr
, 0, fp
->f_cred
, NULL
)) {
118 if (reg
&& (off
> (max
- (offset_t
)vattr
.va_size
))) {
122 noff
= (uoff_t
)(off
+ (offset_t
)vattr
.va_size
);
123 if (reg
&& noff
> max
) {
131 * Get and set the file pointer to the offset of the next
135 error
= fop_ioctl(vp
, _FIO_SEEK_DATA
, (intptr_t)(&noff
),
136 FKIOCTL
, kcred
, NULL
, NULL
);
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
);
148 if (noff
>= (uoff_t
)vattr
.va_size
)
151 if (reg
&& (noff
> max
))
154 fp
->f_offset
= (offset_t
)noff
;
155 (*retoff
) = (offset_t
)noff
;
160 * Get and set the file pointer to the offset of the next
164 error
= fop_ioctl(vp
, _FIO_SEEK_HOLE
, (intptr_t)(&noff
),
165 FKIOCTL
, kcred
, NULL
, NULL
);
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
);
177 if (off
< (offset_t
)vattr
.va_size
)
178 noff
= (uoff_t
)vattr
.va_size
;
182 if (reg
&& (noff
> max
))
185 fp
->f_offset
= (offset_t
)noff
;
186 (*retoff
) = (offset_t
)noff
;
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
;
206 lseek32(int32_t fdes
, off32_t off
, int32_t stype
)
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
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
);
236 return ((off32_t
)retoff
);
237 return ((off32_t
)set_errno(error
));
241 * 64-bit seeks from 32-bit applications
244 llseek32(int32_t fdes
, uint32_t off1
, uint32_t off2
, int stype
)
249 #if defined(_LITTLE_ENDIAN)
250 offset_t off
= ((uoff_t
)off2
<< 32) | (uoff_t
)off1
;
252 offset_t off
= ((uoff_t
)off1
<< 32) | (uoff_t
)off2
;
255 if ((fp
= getf(fdes
)) == NULL
)
258 error
= lseek32_common(fp
, stype
, off
, MAXOFFSET_T
, &retoff
);
262 return (error
? (offset_t
)set_errno(error
) : retoff
);
264 #endif /* _SYSCALL32_IMPL || _ILP32 */
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.
274 lseek64(int fdes
, off_t off
, int stype
)
283 if ((fp
= getf(fdes
)) == NULL
)
284 return ((off_t
)set_errno(EBADF
));
291 new_off
+= fp
->f_offset
;
295 vattr
.va_mask
= AT_SIZE
;
296 if ((error
= fop_getattr(vp
, &vattr
, 0, fp
->f_cred
, NULL
)) != 0)
298 new_off
+= vattr
.va_size
;
306 * Get and set the file pointer to the offset of the next
309 new_off
= (offset_t
)off
;
310 error
= fop_ioctl(vp
, _FIO_SEEK_DATA
, (intptr_t)(&new_off
),
311 FKIOCTL
, kcred
, NULL
, NULL
);
313 if (error
!= ENOTTY
) {
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
);
324 if (new_off
>= (offset_t
)vattr
.va_size
) {
329 fp
->f_offset
= new_off
;
335 * Get and set the file pointer to the offset of the next
339 error
= fop_ioctl(vp
, _FIO_SEEK_HOLE
, (intptr_t)(&new_off
),
340 FKIOCTL
, kcred
, NULL
, NULL
);
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
);
352 if (off
< (offset_t
)vattr
.va_size
) {
353 new_off
= (offset_t
)vattr
.va_size
;
359 fp
->f_offset
= new_off
;
368 old_off
= fp
->f_offset
;
369 if ((error
= fop_seek(vp
, old_off
, &new_off
, NULL
)) == 0) {
370 fp
->f_offset
= new_off
;
377 return ((off_t
)set_errno(error
));