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