xref: /illumos-gate/usr/src/uts/common/syscall/stat.c (revision 7c478bd9)
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 2004 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 /*
38*7c478bd9Sstevel@tonic-gate  * Get file attribute information through a file name or a file descriptor.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/mode.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/ioreq.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
60*7c478bd9Sstevel@tonic-gate #include <c2/audit.h>
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * Get the vp to be stated and the cred to be used for the call
64*7c478bd9Sstevel@tonic-gate  * to VOP_GETATTR
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * nmflag has the following values
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * 1 - Always do lookup.  i.e. stat, lstat.
71*7c478bd9Sstevel@tonic-gate  * 2 - Name is optional i.e. fstatat
72*7c478bd9Sstevel@tonic-gate  * 0 - Don't lookup name, vp is in file_p. i.e. fstat
73*7c478bd9Sstevel@tonic-gate  *
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate static int
76*7c478bd9Sstevel@tonic-gate cstatat_getvp(int fd, char *name, int nmflag,
77*7c478bd9Sstevel@tonic-gate     int follow, vnode_t **vp, cred_t **cred)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	vnode_t *startvp;
80*7c478bd9Sstevel@tonic-gate 	file_t *fp;
81*7c478bd9Sstevel@tonic-gate 	int error;
82*7c478bd9Sstevel@tonic-gate 	cred_t *cr;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	*vp = NULL;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/*
87*7c478bd9Sstevel@tonic-gate 	 * Only return EFAULT for fstatat when fd == AT_FDCWD && name == NULL
88*7c478bd9Sstevel@tonic-gate 	 */
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (fd == AT_FDCWD) {
91*7c478bd9Sstevel@tonic-gate 		if (name != NULL || nmflag != 2) {
92*7c478bd9Sstevel@tonic-gate 			startvp = NULL;
93*7c478bd9Sstevel@tonic-gate 			cr = CRED();
94*7c478bd9Sstevel@tonic-gate 			crhold(cr);
95*7c478bd9Sstevel@tonic-gate 		} else
96*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
97*7c478bd9Sstevel@tonic-gate 	} else {
98*7c478bd9Sstevel@tonic-gate 		char startchar;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 		if (nmflag == 1 || (nmflag == 2 && name != NULL)) {
101*7c478bd9Sstevel@tonic-gate 			if (copyin(name, &startchar, sizeof (char)))
102*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
103*7c478bd9Sstevel@tonic-gate 		} else {
104*7c478bd9Sstevel@tonic-gate 			startchar = '\0';
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 		if (startchar != '/' || nmflag == 0) {
107*7c478bd9Sstevel@tonic-gate 			if ((fp = getf(fd)) == NULL) {
108*7c478bd9Sstevel@tonic-gate 				return (EBADF);
109*7c478bd9Sstevel@tonic-gate 			}
110*7c478bd9Sstevel@tonic-gate 			startvp = fp->f_vnode;
111*7c478bd9Sstevel@tonic-gate 			cr = fp->f_cred;
112*7c478bd9Sstevel@tonic-gate 			crhold(cr);
113*7c478bd9Sstevel@tonic-gate 			VN_HOLD(startvp);
114*7c478bd9Sstevel@tonic-gate 			releasef(fd);
115*7c478bd9Sstevel@tonic-gate 		} else {
116*7c478bd9Sstevel@tonic-gate 			startvp = NULL;
117*7c478bd9Sstevel@tonic-gate 			cr = CRED();
118*7c478bd9Sstevel@tonic-gate 			crhold(cr);
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 	*cred = cr;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
124*7c478bd9Sstevel@tonic-gate 	if (audit_active)
125*7c478bd9Sstevel@tonic-gate 		audit_setfsat_path(1);
126*7c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if (nmflag == 1 || (nmflag == 2 && name != NULL)) {
130*7c478bd9Sstevel@tonic-gate lookup:
131*7c478bd9Sstevel@tonic-gate 		if (error = lookupnameat(name, UIO_USERSPACE, follow, NULLVPP,
132*7c478bd9Sstevel@tonic-gate 		    vp, startvp)) {
133*7c478bd9Sstevel@tonic-gate 			if (error == ESTALE)
134*7c478bd9Sstevel@tonic-gate 				goto lookup;
135*7c478bd9Sstevel@tonic-gate 			if (startvp != NULL)
136*7c478bd9Sstevel@tonic-gate 				VN_RELE(startvp);
137*7c478bd9Sstevel@tonic-gate 			crfree(cr);
138*7c478bd9Sstevel@tonic-gate 			return (error);
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 		if (startvp != NULL)
141*7c478bd9Sstevel@tonic-gate 			VN_RELE(startvp);
142*7c478bd9Sstevel@tonic-gate 	} else {
143*7c478bd9Sstevel@tonic-gate 		*vp = startvp;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	return (0);
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate  * Native syscall interfaces:
151*7c478bd9Sstevel@tonic-gate  *
152*7c478bd9Sstevel@tonic-gate  * N-bit kernel, N-bit applications, N-bit file offsets
153*7c478bd9Sstevel@tonic-gate  */
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate static int cstatat(int, char *, int, struct stat *, int, int);
156*7c478bd9Sstevel@tonic-gate static int cstat(vnode_t *vp, struct stat *, int, cred_t *);
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate int
159*7c478bd9Sstevel@tonic-gate stat(char *fname, struct stat *sb)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	return (cstatat(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL));
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate int
165*7c478bd9Sstevel@tonic-gate lstat(char *fname, struct stat *sb)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	return (cstatat(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0));
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /*
171*7c478bd9Sstevel@tonic-gate  * fstat can and should be fast, do an inline implementation here.
172*7c478bd9Sstevel@tonic-gate  */
173*7c478bd9Sstevel@tonic-gate #define	FSTAT_BODY(fd, sb, statfn)				\
174*7c478bd9Sstevel@tonic-gate 	{							\
175*7c478bd9Sstevel@tonic-gate 		file_t *fp;					\
176*7c478bd9Sstevel@tonic-gate 		int error;					\
177*7c478bd9Sstevel@tonic-gate 								\
178*7c478bd9Sstevel@tonic-gate 		if ((fp = getf(fd)) == NULL)			\
179*7c478bd9Sstevel@tonic-gate 			return (set_errno(EBADF));		\
180*7c478bd9Sstevel@tonic-gate 		if (audit_active)				\
181*7c478bd9Sstevel@tonic-gate 			audit_setfsat_path(1);			\
182*7c478bd9Sstevel@tonic-gate 		error = statfn(fp->f_vnode, sb, 0, fp->f_cred);	\
183*7c478bd9Sstevel@tonic-gate 		releasef(fd);					\
184*7c478bd9Sstevel@tonic-gate 		if (error)					\
185*7c478bd9Sstevel@tonic-gate 			return (set_errno(error));		\
186*7c478bd9Sstevel@tonic-gate 		return (0);					\
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate int
190*7c478bd9Sstevel@tonic-gate fstat(int fd, struct stat *sb)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat)
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate int
196*7c478bd9Sstevel@tonic-gate fstatat(int fd, char *name, struct stat *sb, int flags)
197*7c478bd9Sstevel@tonic-gate {
198*7c478bd9Sstevel@tonic-gate 	return (cstatat(fd, name, 2, sb, flags, 0));
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__i386_COMPAT)
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate /*
204*7c478bd9Sstevel@tonic-gate  * Handle all the "extended" stat operations in the same way;
205*7c478bd9Sstevel@tonic-gate  * validate the version, then call the real handler.
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate #define	XSTAT_BODY(ver, f, s, fn)			\
209*7c478bd9Sstevel@tonic-gate 	return (ver != _STAT_VER ? set_errno(EINVAL) : fn(f, s));
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate #endif	/* __i386 || __i386_COMPAT */
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate #if defined(__i386)
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate  * Syscalls for i386 applications that issue {,l,f}xstat() directly
217*7c478bd9Sstevel@tonic-gate  */
218*7c478bd9Sstevel@tonic-gate int
219*7c478bd9Sstevel@tonic-gate xstat(int version, char *fname, struct stat *sb)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 	XSTAT_BODY(version, fname, sb, stat)
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate int
225*7c478bd9Sstevel@tonic-gate lxstat(int version, char *fname, struct stat *sb)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate 	XSTAT_BODY(version, fname, sb, lstat)
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate int
231*7c478bd9Sstevel@tonic-gate fxstat(int version, int fd, struct stat *sb)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	XSTAT_BODY(version, fd, sb, fstat)
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate #endif	/* __i386 */
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*
239*7c478bd9Sstevel@tonic-gate  * Common code for stat(), lstat(), and fstat().
240*7c478bd9Sstevel@tonic-gate  * (32-bit kernel, 32-bit applications, 32-bit files)
241*7c478bd9Sstevel@tonic-gate  * (64-bit kernel, 64-bit applications, 64-bit files)
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate static int
244*7c478bd9Sstevel@tonic-gate cstat(vnode_t *vp, struct stat *ubp, int flag, cred_t *cr)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
247*7c478bd9Sstevel@tonic-gate 	struct stat sb;
248*7c478bd9Sstevel@tonic-gate 	vattr_t vattr;
249*7c478bd9Sstevel@tonic-gate 	int error;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
252*7c478bd9Sstevel@tonic-gate 	if ((error = VOP_GETATTR(vp, &vattr, flag, cr)) != 0)
253*7c478bd9Sstevel@tonic-gate 		return (error);
254*7c478bd9Sstevel@tonic-gate #ifdef	_ILP32
255*7c478bd9Sstevel@tonic-gate 	/*
256*7c478bd9Sstevel@tonic-gate 	 * (32-bit kernel, 32-bit applications, 32-bit files)
257*7c478bd9Sstevel@tonic-gate 	 * NOTE: 32-bit kernel maintains a 64-bit unsigend va_size.
258*7c478bd9Sstevel@tonic-gate 	 *
259*7c478bd9Sstevel@tonic-gate 	 * st_size of devices (VBLK and VCHR special files) is a special case.
260*7c478bd9Sstevel@tonic-gate 	 * POSIX does not define size behavior for special files, so the
261*7c478bd9Sstevel@tonic-gate 	 * following Solaris specific behavior is not a violation. Solaris
262*7c478bd9Sstevel@tonic-gate 	 * returns the size of the device.
263*7c478bd9Sstevel@tonic-gate 	 *
264*7c478bd9Sstevel@tonic-gate 	 * For compatibility with 32-bit programs which happen to do stat() on
265*7c478bd9Sstevel@tonic-gate 	 * a (mknod) bigger than 2GB we suppress the large file EOVERFLOW and
266*7c478bd9Sstevel@tonic-gate 	 * instead we return the value MAXOFF32_T (LONG_MAX).
267*7c478bd9Sstevel@tonic-gate 	 *
268*7c478bd9Sstevel@tonic-gate 	 * 32-bit applications that care about the size of devices should be
269*7c478bd9Sstevel@tonic-gate 	 * built 64-bit or use a large file interface (lfcompile(5) or lf64(5)).
270*7c478bd9Sstevel@tonic-gate 	 */
271*7c478bd9Sstevel@tonic-gate 	if ((vattr.va_size > MAXOFF32_T) &&
272*7c478bd9Sstevel@tonic-gate 	    ((vp->v_type == VBLK) || (vp->v_type == VCHR))) {
273*7c478bd9Sstevel@tonic-gate 		/* OVERFLOW | UNKNOWN_SIZE */
274*7c478bd9Sstevel@tonic-gate 		vattr.va_size = MAXOFF32_T;
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
277*7c478bd9Sstevel@tonic-gate 	if (vattr.va_size > MAXOFF_T || vattr.va_nblocks > LONG_MAX ||
278*7c478bd9Sstevel@tonic-gate 	    vattr.va_nodeid > ULONG_MAX)
279*7c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	bzero(&sb, sizeof (sb));
282*7c478bd9Sstevel@tonic-gate 	sb.st_dev = vattr.va_fsid;
283*7c478bd9Sstevel@tonic-gate 	sb.st_ino = (ino_t)vattr.va_nodeid;
284*7c478bd9Sstevel@tonic-gate 	sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
285*7c478bd9Sstevel@tonic-gate 	sb.st_nlink = vattr.va_nlink;
286*7c478bd9Sstevel@tonic-gate 	sb.st_uid = vattr.va_uid;
287*7c478bd9Sstevel@tonic-gate 	sb.st_gid = vattr.va_gid;
288*7c478bd9Sstevel@tonic-gate 	sb.st_rdev = vattr.va_rdev;
289*7c478bd9Sstevel@tonic-gate 	sb.st_size = (off_t)vattr.va_size;
290*7c478bd9Sstevel@tonic-gate 	sb.st_atim = vattr.va_atime;
291*7c478bd9Sstevel@tonic-gate 	sb.st_mtim = vattr.va_mtime;
292*7c478bd9Sstevel@tonic-gate 	sb.st_ctim = vattr.va_ctime;
293*7c478bd9Sstevel@tonic-gate 	sb.st_blksize = vattr.va_blksize;
294*7c478bd9Sstevel@tonic-gate 	sb.st_blocks = (blkcnt_t)vattr.va_nblocks;
295*7c478bd9Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
296*7c478bd9Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
297*7c478bd9Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
298*7c478bd9Sstevel@tonic-gate 			(void) strcpy(sb.st_fstype, vswp->vsw_name);
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 	if (copyout(&sb, ubp, sizeof (sb)))
301*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
302*7c478bd9Sstevel@tonic-gate 	return (0);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate static int
306*7c478bd9Sstevel@tonic-gate cstatat(int fd, char *name, int nmflag, struct stat *sb, int follow, int flags)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
309*7c478bd9Sstevel@tonic-gate 	int error;
310*7c478bd9Sstevel@tonic-gate 	cred_t *cred;
311*7c478bd9Sstevel@tonic-gate 	int link_follow;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
314*7c478bd9Sstevel@tonic-gate lookup:
315*7c478bd9Sstevel@tonic-gate 	if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred))
316*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
317*7c478bd9Sstevel@tonic-gate 	error = cstat(vp, sb, flags, cred);
318*7c478bd9Sstevel@tonic-gate 	crfree(cred);
319*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
320*7c478bd9Sstevel@tonic-gate out:
321*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
322*7c478bd9Sstevel@tonic-gate 		if (error == ESTALE &&
323*7c478bd9Sstevel@tonic-gate 		    (nmflag == 1 || (nmflag == 2 && name != NULL)))
324*7c478bd9Sstevel@tonic-gate 			goto lookup;
325*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
326*7c478bd9Sstevel@tonic-gate 	}
327*7c478bd9Sstevel@tonic-gate 	return (0);
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate /*
333*7c478bd9Sstevel@tonic-gate  * 64-bit kernel, 32-bit applications, 32-bit file offsets
334*7c478bd9Sstevel@tonic-gate  */
335*7c478bd9Sstevel@tonic-gate static int cstatat32(int, char *, int, struct stat32 *, int, int);
336*7c478bd9Sstevel@tonic-gate static int cstat32(vnode_t *, struct stat32 *, int, cred_t *);
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate int
339*7c478bd9Sstevel@tonic-gate stat32(char *fname, struct stat32 *sb)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	return (cstatat32(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL));
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate int
345*7c478bd9Sstevel@tonic-gate lstat32(char *fname, struct stat32 *sb)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	return (cstatat32(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0));
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate int
351*7c478bd9Sstevel@tonic-gate fstat32(int fd, struct stat32 *sb)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat32)
354*7c478bd9Sstevel@tonic-gate }
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate int
357*7c478bd9Sstevel@tonic-gate fstatat32(int fd, char *name, struct stat32 *sb, int flag)
358*7c478bd9Sstevel@tonic-gate {
359*7c478bd9Sstevel@tonic-gate 	return (cstatat32(fd, name, 2, sb, flag, 0));
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate #if defined(__i386_COMPAT)
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate  * Syscalls for i386 applications that issue {,l,f}xstat() directly
366*7c478bd9Sstevel@tonic-gate  */
367*7c478bd9Sstevel@tonic-gate int
368*7c478bd9Sstevel@tonic-gate xstat32(int version, char *fname, struct stat32 *sb)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	XSTAT_BODY(version, fname, sb, stat32)
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate int
374*7c478bd9Sstevel@tonic-gate lxstat32(int version, char *fname, struct stat32 *sb)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	XSTAT_BODY(version, fname, sb, lstat32)
377*7c478bd9Sstevel@tonic-gate }
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate int
380*7c478bd9Sstevel@tonic-gate fxstat32(int version, int fd, struct stat32 *sb)
381*7c478bd9Sstevel@tonic-gate {
382*7c478bd9Sstevel@tonic-gate 	XSTAT_BODY(version, fd, sb, fstat32)
383*7c478bd9Sstevel@tonic-gate }
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate #endif	/* __i386_COMPAT */
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate static int
388*7c478bd9Sstevel@tonic-gate cstat32(vnode_t *vp, struct stat32 *ubp, int flag, struct cred *cr)
389*7c478bd9Sstevel@tonic-gate {
390*7c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
391*7c478bd9Sstevel@tonic-gate 	struct stat32 sb;
392*7c478bd9Sstevel@tonic-gate 	vattr_t vattr;
393*7c478bd9Sstevel@tonic-gate 	int error;
394*7c478bd9Sstevel@tonic-gate 	dev32_t st_dev, st_rdev;
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
397*7c478bd9Sstevel@tonic-gate 	if (error = VOP_GETATTR(vp, &vattr, flag, cr))
398*7c478bd9Sstevel@tonic-gate 		return (error);
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	/* devices are a special case, see comments in cstat */
401*7c478bd9Sstevel@tonic-gate 	if ((vattr.va_size > MAXOFF32_T) &&
402*7c478bd9Sstevel@tonic-gate 	    ((vp->v_type == VBLK) || (vp->v_type == VCHR))) {
403*7c478bd9Sstevel@tonic-gate 		/* OVERFLOW | UNKNOWN_SIZE */
404*7c478bd9Sstevel@tonic-gate 		vattr.va_size = MAXOFF32_T;
405*7c478bd9Sstevel@tonic-gate 	}
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	/* check for large values */
408*7c478bd9Sstevel@tonic-gate 	if (!cmpldev(&st_dev, vattr.va_fsid) ||
409*7c478bd9Sstevel@tonic-gate 	    !cmpldev(&st_rdev, vattr.va_rdev) ||
410*7c478bd9Sstevel@tonic-gate 	    vattr.va_size > MAXOFF32_T ||
411*7c478bd9Sstevel@tonic-gate 	    vattr.va_nblocks > INT32_MAX ||
412*7c478bd9Sstevel@tonic-gate 	    vattr.va_nodeid > UINT32_MAX ||
413*7c478bd9Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_atime)) ||
414*7c478bd9Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_mtime)) ||
415*7c478bd9Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_ctime)))
416*7c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	bzero(&sb, sizeof (sb));
419*7c478bd9Sstevel@tonic-gate 	sb.st_dev = st_dev;
420*7c478bd9Sstevel@tonic-gate 	sb.st_ino = (ino32_t)vattr.va_nodeid;
421*7c478bd9Sstevel@tonic-gate 	sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
422*7c478bd9Sstevel@tonic-gate 	sb.st_nlink = vattr.va_nlink;
423*7c478bd9Sstevel@tonic-gate 	sb.st_uid = vattr.va_uid;
424*7c478bd9Sstevel@tonic-gate 	sb.st_gid = vattr.va_gid;
425*7c478bd9Sstevel@tonic-gate 	sb.st_rdev = st_rdev;
426*7c478bd9Sstevel@tonic-gate 	sb.st_size = (off32_t)vattr.va_size;
427*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(sb.st_atim), &(vattr.va_atime));
428*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(sb.st_mtim), &(vattr.va_mtime));
429*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(sb.st_ctim), &(vattr.va_ctime));
430*7c478bd9Sstevel@tonic-gate 	sb.st_blksize = vattr.va_blksize;
431*7c478bd9Sstevel@tonic-gate 	sb.st_blocks = (blkcnt32_t)vattr.va_nblocks;
432*7c478bd9Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
433*7c478bd9Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
434*7c478bd9Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
435*7c478bd9Sstevel@tonic-gate 			(void) strcpy(sb.st_fstype, vswp->vsw_name);
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 	if (copyout(&sb, ubp, sizeof (sb)))
438*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
439*7c478bd9Sstevel@tonic-gate 	return (0);
440*7c478bd9Sstevel@tonic-gate }
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate static int
443*7c478bd9Sstevel@tonic-gate cstatat32(int fd, char *name, int nmflag, struct stat32 *sb,
444*7c478bd9Sstevel@tonic-gate     int follow, int flags)
445*7c478bd9Sstevel@tonic-gate {
446*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
447*7c478bd9Sstevel@tonic-gate 	int error;
448*7c478bd9Sstevel@tonic-gate 	cred_t *cred;
449*7c478bd9Sstevel@tonic-gate 	int link_follow;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
452*7c478bd9Sstevel@tonic-gate lookup:
453*7c478bd9Sstevel@tonic-gate 	if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred))
454*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
455*7c478bd9Sstevel@tonic-gate 	error = cstat32(vp, sb, flags, cred);
456*7c478bd9Sstevel@tonic-gate 	crfree(cred);
457*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
458*7c478bd9Sstevel@tonic-gate out:
459*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
460*7c478bd9Sstevel@tonic-gate 		if (error == ESTALE &&
461*7c478bd9Sstevel@tonic-gate 		    (nmflag == 1 || (nmflag == 2 && name != NULL)))
462*7c478bd9Sstevel@tonic-gate 			goto lookup;
463*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
464*7c478bd9Sstevel@tonic-gate 	}
465*7c478bd9Sstevel@tonic-gate 	return (0);
466*7c478bd9Sstevel@tonic-gate }
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate #if defined(_ILP32)
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate /*
473*7c478bd9Sstevel@tonic-gate  * 32-bit kernel, 32-bit applications, 64-bit file offsets.
474*7c478bd9Sstevel@tonic-gate  *
475*7c478bd9Sstevel@tonic-gate  * These routines are implemented differently on 64-bit kernels.
476*7c478bd9Sstevel@tonic-gate  */
477*7c478bd9Sstevel@tonic-gate static int cstatat64(int, char *, int, struct stat64 *, int, int);
478*7c478bd9Sstevel@tonic-gate static int cstat64(vnode_t *, struct stat64 *, int, cred_t *);
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate int
481*7c478bd9Sstevel@tonic-gate stat64(char *fname, struct stat64 *sb)
482*7c478bd9Sstevel@tonic-gate {
483*7c478bd9Sstevel@tonic-gate 	return (cstatat64(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL));
484*7c478bd9Sstevel@tonic-gate }
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate int
487*7c478bd9Sstevel@tonic-gate lstat64(char *fname, struct stat64 *sb)
488*7c478bd9Sstevel@tonic-gate {
489*7c478bd9Sstevel@tonic-gate 	return (cstatat64(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0));
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate int
493*7c478bd9Sstevel@tonic-gate fstat64(int fd, struct stat64 *sb)
494*7c478bd9Sstevel@tonic-gate {
495*7c478bd9Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat64)
496*7c478bd9Sstevel@tonic-gate }
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate int
499*7c478bd9Sstevel@tonic-gate fstatat64(int fd, char *name, struct stat64 *sb, int flags)
500*7c478bd9Sstevel@tonic-gate {
501*7c478bd9Sstevel@tonic-gate 	return (cstatat64(fd, name, 2, sb, flags, 0));
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate static int
505*7c478bd9Sstevel@tonic-gate cstat64(vnode_t *vp, struct stat64 *ubp, int flag, cred_t *cr)
506*7c478bd9Sstevel@tonic-gate {
507*7c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
508*7c478bd9Sstevel@tonic-gate 	struct stat64 lsb;
509*7c478bd9Sstevel@tonic-gate 	vattr_t vattr;
510*7c478bd9Sstevel@tonic-gate 	int error;
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
513*7c478bd9Sstevel@tonic-gate 	if (error = VOP_GETATTR(vp, &vattr, flag, cr))
514*7c478bd9Sstevel@tonic-gate 		return (error);
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	bzero(&lsb, sizeof (lsb));
517*7c478bd9Sstevel@tonic-gate 	lsb.st_dev = vattr.va_fsid;
518*7c478bd9Sstevel@tonic-gate 	lsb.st_ino = vattr.va_nodeid;
519*7c478bd9Sstevel@tonic-gate 	lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
520*7c478bd9Sstevel@tonic-gate 	lsb.st_nlink = vattr.va_nlink;
521*7c478bd9Sstevel@tonic-gate 	lsb.st_uid = vattr.va_uid;
522*7c478bd9Sstevel@tonic-gate 	lsb.st_gid = vattr.va_gid;
523*7c478bd9Sstevel@tonic-gate 	lsb.st_rdev = vattr.va_rdev;
524*7c478bd9Sstevel@tonic-gate 	lsb.st_size = vattr.va_size;
525*7c478bd9Sstevel@tonic-gate 	lsb.st_atim = vattr.va_atime;
526*7c478bd9Sstevel@tonic-gate 	lsb.st_mtim = vattr.va_mtime;
527*7c478bd9Sstevel@tonic-gate 	lsb.st_ctim = vattr.va_ctime;
528*7c478bd9Sstevel@tonic-gate 	lsb.st_blksize = vattr.va_blksize;
529*7c478bd9Sstevel@tonic-gate 	lsb.st_blocks = vattr.va_nblocks;
530*7c478bd9Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
531*7c478bd9Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
532*7c478bd9Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
533*7c478bd9Sstevel@tonic-gate 			(void) strcpy(lsb.st_fstype, vswp->vsw_name);
534*7c478bd9Sstevel@tonic-gate 	}
535*7c478bd9Sstevel@tonic-gate 	if (copyout(&lsb, ubp, sizeof (lsb)))
536*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
537*7c478bd9Sstevel@tonic-gate 	return (0);
538*7c478bd9Sstevel@tonic-gate }
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate static int
541*7c478bd9Sstevel@tonic-gate cstatat64(int fd, char *name, int nmflag, struct stat64 *sb,
542*7c478bd9Sstevel@tonic-gate     int follow, int flags)
543*7c478bd9Sstevel@tonic-gate {
544*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
545*7c478bd9Sstevel@tonic-gate 	int error;
546*7c478bd9Sstevel@tonic-gate 	cred_t *cred;
547*7c478bd9Sstevel@tonic-gate 	int link_follow;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
550*7c478bd9Sstevel@tonic-gate lookup:
551*7c478bd9Sstevel@tonic-gate 	if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred))
552*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
553*7c478bd9Sstevel@tonic-gate 	error = cstat64(vp, sb, flags, cred);
554*7c478bd9Sstevel@tonic-gate 	crfree(cred);
555*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
556*7c478bd9Sstevel@tonic-gate out:
557*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
558*7c478bd9Sstevel@tonic-gate 		if (error == ESTALE &&
559*7c478bd9Sstevel@tonic-gate 		    (nmflag == 1 || (nmflag == 2 && name != NULL)))
560*7c478bd9Sstevel@tonic-gate 			goto lookup;
561*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 	return (0);
564*7c478bd9Sstevel@tonic-gate }
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate /*
571*7c478bd9Sstevel@tonic-gate  * 64-bit kernel, 32-bit applications, 64-bit file offsets.
572*7c478bd9Sstevel@tonic-gate  *
573*7c478bd9Sstevel@tonic-gate  * We'd really like to call the "native" stat calls for these ones,
574*7c478bd9Sstevel@tonic-gate  * but the problem is that the 64-bit ABI defines the 'stat64' structure
575*7c478bd9Sstevel@tonic-gate  * differently from the way the 32-bit ABI defines it.
576*7c478bd9Sstevel@tonic-gate  */
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate static int cstatat64_32(int, char *, int, struct stat64_32 *, int, int);
579*7c478bd9Sstevel@tonic-gate static int cstat64_32(vnode_t *, struct stat64_32 *, int, cred_t *);
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate int
582*7c478bd9Sstevel@tonic-gate stat64_32(char *fname, struct stat64_32 *sb)
583*7c478bd9Sstevel@tonic-gate {
584*7c478bd9Sstevel@tonic-gate 	return (cstatat64_32(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL));
585*7c478bd9Sstevel@tonic-gate }
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate int
588*7c478bd9Sstevel@tonic-gate lstat64_32(char *fname, struct stat64_32 *sb)
589*7c478bd9Sstevel@tonic-gate {
590*7c478bd9Sstevel@tonic-gate 	return (cstatat64_32(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0));
591*7c478bd9Sstevel@tonic-gate }
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate int
594*7c478bd9Sstevel@tonic-gate fstat64_32(int fd, struct stat64_32 *sb)
595*7c478bd9Sstevel@tonic-gate {
596*7c478bd9Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat64_32)
597*7c478bd9Sstevel@tonic-gate }
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate int
600*7c478bd9Sstevel@tonic-gate fstatat64_32(int fd, char *name, struct stat64_32 *sb, int flag)
601*7c478bd9Sstevel@tonic-gate {
602*7c478bd9Sstevel@tonic-gate 	return (cstatat64_32(fd, name, 2, sb, flag, 0));
603*7c478bd9Sstevel@tonic-gate }
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate static int
606*7c478bd9Sstevel@tonic-gate cstat64_32(vnode_t *vp, struct stat64_32 *ubp, int flag, cred_t *cr)
607*7c478bd9Sstevel@tonic-gate {
608*7c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
609*7c478bd9Sstevel@tonic-gate 	struct stat64_32 lsb;
610*7c478bd9Sstevel@tonic-gate 	vattr_t vattr;
611*7c478bd9Sstevel@tonic-gate 	int error;
612*7c478bd9Sstevel@tonic-gate 	dev32_t st_dev, st_rdev;
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
615*7c478bd9Sstevel@tonic-gate 	if (error = VOP_GETATTR(vp, &vattr, flag, cr))
616*7c478bd9Sstevel@tonic-gate 		return (error);
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	if (!cmpldev(&st_dev, vattr.va_fsid) ||
619*7c478bd9Sstevel@tonic-gate 	    !cmpldev(&st_rdev, vattr.va_rdev) ||
620*7c478bd9Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_atime)) ||
621*7c478bd9Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_mtime)) ||
622*7c478bd9Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_ctime)))
623*7c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	bzero(&lsb, sizeof (lsb));
626*7c478bd9Sstevel@tonic-gate 	lsb.st_dev = st_dev;
627*7c478bd9Sstevel@tonic-gate 	lsb.st_ino = vattr.va_nodeid;
628*7c478bd9Sstevel@tonic-gate 	lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
629*7c478bd9Sstevel@tonic-gate 	lsb.st_nlink = vattr.va_nlink;
630*7c478bd9Sstevel@tonic-gate 	lsb.st_uid = vattr.va_uid;
631*7c478bd9Sstevel@tonic-gate 	lsb.st_gid = vattr.va_gid;
632*7c478bd9Sstevel@tonic-gate 	lsb.st_rdev = st_rdev;
633*7c478bd9Sstevel@tonic-gate 	lsb.st_size = vattr.va_size;
634*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(lsb.st_atim), &(vattr.va_atime));
635*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(lsb.st_mtim), &(vattr.va_mtime));
636*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(lsb.st_ctim), &(vattr.va_ctime));
637*7c478bd9Sstevel@tonic-gate 	lsb.st_blksize = vattr.va_blksize;
638*7c478bd9Sstevel@tonic-gate 	lsb.st_blocks = vattr.va_nblocks;
639*7c478bd9Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
640*7c478bd9Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
641*7c478bd9Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
642*7c478bd9Sstevel@tonic-gate 			(void) strcpy(lsb.st_fstype, vswp->vsw_name);
643*7c478bd9Sstevel@tonic-gate 	}
644*7c478bd9Sstevel@tonic-gate 	if (copyout(&lsb, ubp, sizeof (lsb)))
645*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
646*7c478bd9Sstevel@tonic-gate 	return (0);
647*7c478bd9Sstevel@tonic-gate }
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate static int
650*7c478bd9Sstevel@tonic-gate cstatat64_32(int fd, char *name, int nmflag, struct stat64_32 *sb,
651*7c478bd9Sstevel@tonic-gate     int follow, int flags)
652*7c478bd9Sstevel@tonic-gate {
653*7c478bd9Sstevel@tonic-gate 	vnode_t  *vp;
654*7c478bd9Sstevel@tonic-gate 	int error;
655*7c478bd9Sstevel@tonic-gate 	cred_t *cred;
656*7c478bd9Sstevel@tonic-gate 	int link_follow;
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
659*7c478bd9Sstevel@tonic-gate lookup:
660*7c478bd9Sstevel@tonic-gate 	if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred))
661*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
662*7c478bd9Sstevel@tonic-gate 	error = cstat64_32(vp, sb, flags, cred);
663*7c478bd9Sstevel@tonic-gate 	crfree(cred);
664*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
665*7c478bd9Sstevel@tonic-gate out:
666*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
667*7c478bd9Sstevel@tonic-gate 		if (error == ESTALE &&
668*7c478bd9Sstevel@tonic-gate 		    (nmflag == 1 || (nmflag == 2 && name != NULL)))
669*7c478bd9Sstevel@tonic-gate 			goto lookup;
670*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
671*7c478bd9Sstevel@tonic-gate 	}
672*7c478bd9Sstevel@tonic-gate 	return (0);
673*7c478bd9Sstevel@tonic-gate }
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
676