xref: /onnv-gate/usr/src/uts/common/syscall/stat.c (revision 12789:82cffaae72d5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52051Sprabahar  * Common Development and Distribution License (the "License").
62051Sprabahar  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
2111798SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
2312689SBrent.Paulson@Oracle.COM  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * Get file attribute information through a file name or a file descriptor.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/isa_defs.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/cred.h>
430Sstevel@tonic-gate #include <sys/systm.h>
440Sstevel@tonic-gate #include <sys/errno.h>
450Sstevel@tonic-gate #include <sys/fcntl.h>
460Sstevel@tonic-gate #include <sys/pathname.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/vfs.h>
490Sstevel@tonic-gate #include <sys/vnode.h>
500Sstevel@tonic-gate #include <sys/mode.h>
510Sstevel@tonic-gate #include <sys/file.h>
520Sstevel@tonic-gate #include <sys/proc.h>
530Sstevel@tonic-gate #include <sys/uio.h>
540Sstevel@tonic-gate #include <sys/debug.h>
550Sstevel@tonic-gate #include <sys/cmn_err.h>
560Sstevel@tonic-gate #include <c2/audit.h>
572051Sprabahar #include <fs/fs_subr.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Get the vp to be stated and the cred to be used for the call
610Sstevel@tonic-gate  * to VOP_GETATTR
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static int
cstatat_getvp(int fd,char * name,int follow,vnode_t ** vp,cred_t ** cred)6511798SRoger.Faulkner@Sun.COM cstatat_getvp(int fd, char *name, int follow, vnode_t **vp, cred_t **cred)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	vnode_t *startvp;
680Sstevel@tonic-gate 	file_t *fp;
690Sstevel@tonic-gate 	int error;
700Sstevel@tonic-gate 	cred_t *cr;
712051Sprabahar 	int estale_retry = 0;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	*vp = NULL;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * Only return EFAULT for fstatat when fd == AT_FDCWD && name == NULL
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (fd == AT_FDCWD) {
8011798SRoger.Faulkner@Sun.COM 		startvp = NULL;
8111798SRoger.Faulkner@Sun.COM 		cr = CRED();
8211798SRoger.Faulkner@Sun.COM 		crhold(cr);
830Sstevel@tonic-gate 	} else {
840Sstevel@tonic-gate 		char startchar;
850Sstevel@tonic-gate 
8611798SRoger.Faulkner@Sun.COM 		if (copyin(name, &startchar, sizeof (char)))
8711798SRoger.Faulkner@Sun.COM 			return (EFAULT);
8811798SRoger.Faulkner@Sun.COM 		if (startchar != '/') {
890Sstevel@tonic-gate 			if ((fp = getf(fd)) == NULL) {
900Sstevel@tonic-gate 				return (EBADF);
910Sstevel@tonic-gate 			}
920Sstevel@tonic-gate 			startvp = fp->f_vnode;
930Sstevel@tonic-gate 			cr = fp->f_cred;
940Sstevel@tonic-gate 			crhold(cr);
950Sstevel@tonic-gate 			VN_HOLD(startvp);
960Sstevel@tonic-gate 			releasef(fd);
970Sstevel@tonic-gate 		} else {
980Sstevel@tonic-gate 			startvp = NULL;
990Sstevel@tonic-gate 			cr = CRED();
1000Sstevel@tonic-gate 			crhold(cr);
1010Sstevel@tonic-gate 		}
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	*cred = cr;
1040Sstevel@tonic-gate 
105*12789SRoger.Faulkner@Oracle.COM 	if (AU_AUDITING() && startvp != NULL)
1060Sstevel@tonic-gate 		audit_setfsat_path(1);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate lookup:
10911798SRoger.Faulkner@Sun.COM 	if (error = lookupnameat(name, UIO_USERSPACE, follow, NULLVPP,
11011798SRoger.Faulkner@Sun.COM 	    vp, startvp)) {
11111798SRoger.Faulkner@Sun.COM 		if ((error == ESTALE) &&
11211798SRoger.Faulkner@Sun.COM 		    fs_need_estale_retry(estale_retry++))
11311798SRoger.Faulkner@Sun.COM 			goto lookup;
1140Sstevel@tonic-gate 		if (startvp != NULL)
1150Sstevel@tonic-gate 			VN_RELE(startvp);
11611798SRoger.Faulkner@Sun.COM 		crfree(cr);
11711798SRoger.Faulkner@Sun.COM 		return (error);
1180Sstevel@tonic-gate 	}
11911798SRoger.Faulkner@Sun.COM 	if (startvp != NULL)
12011798SRoger.Faulkner@Sun.COM 		VN_RELE(startvp);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	return (0);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * Native syscall interfaces:
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  * N-bit kernel, N-bit applications, N-bit file offsets
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate 
13111798SRoger.Faulkner@Sun.COM static int cstatat(int, char *, struct stat *, int, int);
1320Sstevel@tonic-gate static int cstat(vnode_t *vp, struct stat *, int, cred_t *);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * fstat can and should be fast, do an inline implementation here.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate #define	FSTAT_BODY(fd, sb, statfn)				\
1380Sstevel@tonic-gate 	{							\
1390Sstevel@tonic-gate 		file_t *fp;					\
1400Sstevel@tonic-gate 		int error;					\
1410Sstevel@tonic-gate 								\
14211798SRoger.Faulkner@Sun.COM 		if (fd == AT_FDCWD)				\
14311798SRoger.Faulkner@Sun.COM 			return (set_errno(EFAULT));		\
1440Sstevel@tonic-gate 		if ((fp = getf(fd)) == NULL)			\
1450Sstevel@tonic-gate 			return (set_errno(EBADF));		\
1460Sstevel@tonic-gate 		error = statfn(fp->f_vnode, sb, 0, fp->f_cred);	\
1470Sstevel@tonic-gate 		releasef(fd);					\
1480Sstevel@tonic-gate 		if (error)					\
1490Sstevel@tonic-gate 			return (set_errno(error));		\
1500Sstevel@tonic-gate 		return (0);					\
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
fstat(int fd,struct stat * sb)1540Sstevel@tonic-gate fstat(int fd, struct stat *sb)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat)
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int
fstatat(int fd,char * name,struct stat * sb,int flags)1600Sstevel@tonic-gate fstatat(int fd, char *name, struct stat *sb, int flags)
1610Sstevel@tonic-gate {
16211798SRoger.Faulkner@Sun.COM 	int followflag;
16311798SRoger.Faulkner@Sun.COM 	int csflags;
1640Sstevel@tonic-gate 
16511798SRoger.Faulkner@Sun.COM 	if (name == NULL)
16611798SRoger.Faulkner@Sun.COM 		return (fstat(fd, sb));
1670Sstevel@tonic-gate 
16811798SRoger.Faulkner@Sun.COM 	followflag = (flags & AT_SYMLINK_NOFOLLOW);
16911798SRoger.Faulkner@Sun.COM 	csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
17011798SRoger.Faulkner@Sun.COM 	if (followflag == 0)
17111798SRoger.Faulkner@Sun.COM 		csflags |= ATTR_REAL;	/* flag for procfs lookups */
1720Sstevel@tonic-gate 
17311798SRoger.Faulkner@Sun.COM 	return (cstatat(fd, name, sb, followflag, csflags));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate int
stat(char * name,struct stat * sb)17711798SRoger.Faulkner@Sun.COM stat(char *name, struct stat *sb)
1780Sstevel@tonic-gate {
17911798SRoger.Faulkner@Sun.COM 	return (fstatat(AT_FDCWD, name, sb, 0));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate int
lstat(char * name,struct stat * sb)18311798SRoger.Faulkner@Sun.COM lstat(char *name, struct stat *sb)
1840Sstevel@tonic-gate {
18511798SRoger.Faulkner@Sun.COM 	return (fstatat(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Common code for stat(), lstat(), and fstat().
1900Sstevel@tonic-gate  * (32-bit kernel, 32-bit applications, 32-bit files)
1910Sstevel@tonic-gate  * (64-bit kernel, 64-bit applications, 64-bit files)
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate static int
cstat(vnode_t * vp,struct stat * ubp,int flag,cred_t * cr)1940Sstevel@tonic-gate cstat(vnode_t *vp, struct stat *ubp, int flag, cred_t *cr)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	struct vfssw *vswp;
1970Sstevel@tonic-gate 	struct stat sb;
1980Sstevel@tonic-gate 	vattr_t vattr;
1990Sstevel@tonic-gate 	int error;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
2025331Samw 	if ((error = VOP_GETATTR(vp, &vattr, flag, cr, NULL)) != 0)
2030Sstevel@tonic-gate 		return (error);
2040Sstevel@tonic-gate #ifdef	_ILP32
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * (32-bit kernel, 32-bit applications, 32-bit files)
2070Sstevel@tonic-gate 	 * NOTE: 32-bit kernel maintains a 64-bit unsigend va_size.
2080Sstevel@tonic-gate 	 *
2090Sstevel@tonic-gate 	 * st_size of devices (VBLK and VCHR special files) is a special case.
2100Sstevel@tonic-gate 	 * POSIX does not define size behavior for special files, so the
2110Sstevel@tonic-gate 	 * following Solaris specific behavior is not a violation. Solaris
2120Sstevel@tonic-gate 	 * returns the size of the device.
2130Sstevel@tonic-gate 	 *
2140Sstevel@tonic-gate 	 * For compatibility with 32-bit programs which happen to do stat() on
2150Sstevel@tonic-gate 	 * a (mknod) bigger than 2GB we suppress the large file EOVERFLOW and
2160Sstevel@tonic-gate 	 * instead we return the value MAXOFF32_T (LONG_MAX).
2170Sstevel@tonic-gate 	 *
2180Sstevel@tonic-gate 	 * 32-bit applications that care about the size of devices should be
2190Sstevel@tonic-gate 	 * built 64-bit or use a large file interface (lfcompile(5) or lf64(5)).
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	if ((vattr.va_size > MAXOFF32_T) &&
2220Sstevel@tonic-gate 	    ((vp->v_type == VBLK) || (vp->v_type == VCHR))) {
2230Sstevel@tonic-gate 		/* OVERFLOW | UNKNOWN_SIZE */
2240Sstevel@tonic-gate 		vattr.va_size = MAXOFF32_T;
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate #endif	/* _ILP32 */
2270Sstevel@tonic-gate 	if (vattr.va_size > MAXOFF_T || vattr.va_nblocks > LONG_MAX ||
2280Sstevel@tonic-gate 	    vattr.va_nodeid > ULONG_MAX)
2290Sstevel@tonic-gate 		return (EOVERFLOW);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	bzero(&sb, sizeof (sb));
2320Sstevel@tonic-gate 	sb.st_dev = vattr.va_fsid;
2330Sstevel@tonic-gate 	sb.st_ino = (ino_t)vattr.va_nodeid;
2340Sstevel@tonic-gate 	sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
2350Sstevel@tonic-gate 	sb.st_nlink = vattr.va_nlink;
2360Sstevel@tonic-gate 	sb.st_uid = vattr.va_uid;
2370Sstevel@tonic-gate 	sb.st_gid = vattr.va_gid;
2380Sstevel@tonic-gate 	sb.st_rdev = vattr.va_rdev;
2390Sstevel@tonic-gate 	sb.st_size = (off_t)vattr.va_size;
2400Sstevel@tonic-gate 	sb.st_atim = vattr.va_atime;
2410Sstevel@tonic-gate 	sb.st_mtim = vattr.va_mtime;
2420Sstevel@tonic-gate 	sb.st_ctim = vattr.va_ctime;
2430Sstevel@tonic-gate 	sb.st_blksize = vattr.va_blksize;
2440Sstevel@tonic-gate 	sb.st_blocks = (blkcnt_t)vattr.va_nblocks;
2450Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
2460Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
2470Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
2480Sstevel@tonic-gate 			(void) strcpy(sb.st_fstype, vswp->vsw_name);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	if (copyout(&sb, ubp, sizeof (sb)))
2510Sstevel@tonic-gate 		return (EFAULT);
2520Sstevel@tonic-gate 	return (0);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static int
cstatat(int fd,char * name,struct stat * sb,int follow,int flags)25611798SRoger.Faulkner@Sun.COM cstatat(int fd, char *name, struct stat *sb, int follow, int flags)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	vnode_t *vp;
2590Sstevel@tonic-gate 	int error;
2600Sstevel@tonic-gate 	cred_t *cred;
2610Sstevel@tonic-gate 	int link_follow;
2622051Sprabahar 	int estale_retry = 0;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
2650Sstevel@tonic-gate lookup:
26611798SRoger.Faulkner@Sun.COM 	if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
2670Sstevel@tonic-gate 		return (set_errno(error));
2680Sstevel@tonic-gate 	error = cstat(vp, sb, flags, cred);
2690Sstevel@tonic-gate 	crfree(cred);
2700Sstevel@tonic-gate 	VN_RELE(vp);
2710Sstevel@tonic-gate 	if (error != 0) {
2720Sstevel@tonic-gate 		if (error == ESTALE &&
27311798SRoger.Faulkner@Sun.COM 		    fs_need_estale_retry(estale_retry++))
2740Sstevel@tonic-gate 			goto lookup;
2750Sstevel@tonic-gate 		return (set_errno(error));
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 	return (0);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * 64-bit kernel, 32-bit applications, 32-bit file offsets
2840Sstevel@tonic-gate  */
28511798SRoger.Faulkner@Sun.COM static int cstatat32(int, char *, struct stat32 *, int, int);
2860Sstevel@tonic-gate static int cstat32(vnode_t *, struct stat32 *, int, cred_t *);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate int
fstat32(int fd,struct stat32 * sb)2890Sstevel@tonic-gate fstat32(int fd, struct stat32 *sb)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat32)
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate int
fstatat32(int fd,char * name,struct stat32 * sb,int flags)2955302Sth199096 fstatat32(int fd, char *name, struct stat32 *sb, int flags)
2960Sstevel@tonic-gate {
29711798SRoger.Faulkner@Sun.COM 	int followflag;
29811798SRoger.Faulkner@Sun.COM 	int csflags;
2990Sstevel@tonic-gate 
30011798SRoger.Faulkner@Sun.COM 	if (name == NULL)
30111798SRoger.Faulkner@Sun.COM 		return (fstat32(fd, sb));
3020Sstevel@tonic-gate 
30311798SRoger.Faulkner@Sun.COM 	followflag = (flags & AT_SYMLINK_NOFOLLOW);
30411798SRoger.Faulkner@Sun.COM 	csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
30511798SRoger.Faulkner@Sun.COM 	if (followflag == 0)
30611798SRoger.Faulkner@Sun.COM 		csflags |= ATTR_REAL;	/* flag for procfs lookups */
30711798SRoger.Faulkner@Sun.COM 
30811798SRoger.Faulkner@Sun.COM 	return (cstatat32(fd, name, sb, followflag, csflags));
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate int
stat32(char * name,struct stat32 * sb)31211798SRoger.Faulkner@Sun.COM stat32(char *name, struct stat32 *sb)
3130Sstevel@tonic-gate {
31411798SRoger.Faulkner@Sun.COM 	return (fstatat32(AT_FDCWD, name, sb, 0));
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate int
lstat32(char * name,struct stat32 * sb)31811798SRoger.Faulkner@Sun.COM lstat32(char *name, struct stat32 *sb)
3190Sstevel@tonic-gate {
32011798SRoger.Faulkner@Sun.COM 	return (fstatat32(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static int
cstat32(vnode_t * vp,struct stat32 * ubp,int flag,struct cred * cr)3240Sstevel@tonic-gate cstat32(vnode_t *vp, struct stat32 *ubp, int flag, struct cred *cr)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	struct vfssw *vswp;
3270Sstevel@tonic-gate 	struct stat32 sb;
3280Sstevel@tonic-gate 	vattr_t vattr;
3290Sstevel@tonic-gate 	int error;
3300Sstevel@tonic-gate 	dev32_t st_dev, st_rdev;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
3335331Samw 	if (error = VOP_GETATTR(vp, &vattr, flag, cr, NULL))
3340Sstevel@tonic-gate 		return (error);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/* devices are a special case, see comments in cstat */
3370Sstevel@tonic-gate 	if ((vattr.va_size > MAXOFF32_T) &&
3380Sstevel@tonic-gate 	    ((vp->v_type == VBLK) || (vp->v_type == VCHR))) {
3390Sstevel@tonic-gate 		/* OVERFLOW | UNKNOWN_SIZE */
3400Sstevel@tonic-gate 		vattr.va_size = MAXOFF32_T;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/* check for large values */
3440Sstevel@tonic-gate 	if (!cmpldev(&st_dev, vattr.va_fsid) ||
3450Sstevel@tonic-gate 	    !cmpldev(&st_rdev, vattr.va_rdev) ||
3460Sstevel@tonic-gate 	    vattr.va_size > MAXOFF32_T ||
3470Sstevel@tonic-gate 	    vattr.va_nblocks > INT32_MAX ||
3480Sstevel@tonic-gate 	    vattr.va_nodeid > UINT32_MAX ||
3490Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_atime)) ||
3500Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_mtime)) ||
3510Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_ctime)))
3520Sstevel@tonic-gate 		return (EOVERFLOW);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	bzero(&sb, sizeof (sb));
3550Sstevel@tonic-gate 	sb.st_dev = st_dev;
3560Sstevel@tonic-gate 	sb.st_ino = (ino32_t)vattr.va_nodeid;
3570Sstevel@tonic-gate 	sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
3580Sstevel@tonic-gate 	sb.st_nlink = vattr.va_nlink;
3590Sstevel@tonic-gate 	sb.st_uid = vattr.va_uid;
3600Sstevel@tonic-gate 	sb.st_gid = vattr.va_gid;
3610Sstevel@tonic-gate 	sb.st_rdev = st_rdev;
3620Sstevel@tonic-gate 	sb.st_size = (off32_t)vattr.va_size;
3630Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(sb.st_atim), &(vattr.va_atime));
3640Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(sb.st_mtim), &(vattr.va_mtime));
3650Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(sb.st_ctim), &(vattr.va_ctime));
3660Sstevel@tonic-gate 	sb.st_blksize = vattr.va_blksize;
3670Sstevel@tonic-gate 	sb.st_blocks = (blkcnt32_t)vattr.va_nblocks;
3680Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
3690Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
3700Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
3710Sstevel@tonic-gate 			(void) strcpy(sb.st_fstype, vswp->vsw_name);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	if (copyout(&sb, ubp, sizeof (sb)))
3740Sstevel@tonic-gate 		return (EFAULT);
3750Sstevel@tonic-gate 	return (0);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate static int
cstatat32(int fd,char * name,struct stat32 * sb,int follow,int flags)37911798SRoger.Faulkner@Sun.COM cstatat32(int fd, char *name, struct stat32 *sb, int follow, int flags)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	vnode_t *vp;
3820Sstevel@tonic-gate 	int error;
3830Sstevel@tonic-gate 	cred_t *cred;
3840Sstevel@tonic-gate 	int link_follow;
3852051Sprabahar 	int estale_retry = 0;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
3880Sstevel@tonic-gate lookup:
38911798SRoger.Faulkner@Sun.COM 	if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
3900Sstevel@tonic-gate 		return (set_errno(error));
3910Sstevel@tonic-gate 	error = cstat32(vp, sb, flags, cred);
3920Sstevel@tonic-gate 	crfree(cred);
3930Sstevel@tonic-gate 	VN_RELE(vp);
3940Sstevel@tonic-gate 	if (error != 0) {
3950Sstevel@tonic-gate 		if (error == ESTALE &&
39611798SRoger.Faulkner@Sun.COM 		    fs_need_estale_retry(estale_retry++))
3970Sstevel@tonic-gate 			goto lookup;
3980Sstevel@tonic-gate 		return (set_errno(error));
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	return (0);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate #if defined(_ILP32)
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * 32-bit kernel, 32-bit applications, 64-bit file offsets.
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * These routines are implemented differently on 64-bit kernels.
4110Sstevel@tonic-gate  */
41211798SRoger.Faulkner@Sun.COM static int cstatat64(int, char *, struct stat64 *, int, int);
4130Sstevel@tonic-gate static int cstat64(vnode_t *, struct stat64 *, int, cred_t *);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate int
fstat64(int fd,struct stat64 * sb)4160Sstevel@tonic-gate fstat64(int fd, struct stat64 *sb)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat64)
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate int
fstatat64(int fd,char * name,struct stat64 * sb,int flags)4220Sstevel@tonic-gate fstatat64(int fd, char *name, struct stat64 *sb, int flags)
4230Sstevel@tonic-gate {
42411798SRoger.Faulkner@Sun.COM 	int followflag;
42511798SRoger.Faulkner@Sun.COM 	int csflags;
42611798SRoger.Faulkner@Sun.COM 
42711798SRoger.Faulkner@Sun.COM 	if (name == NULL)
42811798SRoger.Faulkner@Sun.COM 		return (fstat64(fd, sb));
42911798SRoger.Faulkner@Sun.COM 
43011798SRoger.Faulkner@Sun.COM 	followflag = (flags & AT_SYMLINK_NOFOLLOW);
43111798SRoger.Faulkner@Sun.COM 	csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
43211798SRoger.Faulkner@Sun.COM 	if (followflag == 0)
43311798SRoger.Faulkner@Sun.COM 		csflags |= ATTR_REAL;	/* flag for procfs lookups */
43411798SRoger.Faulkner@Sun.COM 
43511798SRoger.Faulkner@Sun.COM 	return (cstatat64(fd, name, sb, followflag, csflags));
43611798SRoger.Faulkner@Sun.COM }
43711798SRoger.Faulkner@Sun.COM 
43811798SRoger.Faulkner@Sun.COM int
stat64(char * name,struct stat64 * sb)43911798SRoger.Faulkner@Sun.COM stat64(char *name, struct stat64 *sb)
44011798SRoger.Faulkner@Sun.COM {
44111798SRoger.Faulkner@Sun.COM 	return (fstatat64(AT_FDCWD, name, sb, 0));
44211798SRoger.Faulkner@Sun.COM }
44311798SRoger.Faulkner@Sun.COM 
44411798SRoger.Faulkner@Sun.COM int
lstat64(char * name,struct stat64 * sb)44511798SRoger.Faulkner@Sun.COM lstat64(char *name, struct stat64 *sb)
44611798SRoger.Faulkner@Sun.COM {
44711798SRoger.Faulkner@Sun.COM 	return (fstatat64(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate static int
cstat64(vnode_t * vp,struct stat64 * ubp,int flag,cred_t * cr)4510Sstevel@tonic-gate cstat64(vnode_t *vp, struct stat64 *ubp, int flag, cred_t *cr)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	struct vfssw *vswp;
4540Sstevel@tonic-gate 	struct stat64 lsb;
4550Sstevel@tonic-gate 	vattr_t vattr;
4560Sstevel@tonic-gate 	int error;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
4595331Samw 	if (error = VOP_GETATTR(vp, &vattr, flag, cr, NULL))
4600Sstevel@tonic-gate 		return (error);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	bzero(&lsb, sizeof (lsb));
4630Sstevel@tonic-gate 	lsb.st_dev = vattr.va_fsid;
4640Sstevel@tonic-gate 	lsb.st_ino = vattr.va_nodeid;
4650Sstevel@tonic-gate 	lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
4660Sstevel@tonic-gate 	lsb.st_nlink = vattr.va_nlink;
4670Sstevel@tonic-gate 	lsb.st_uid = vattr.va_uid;
4680Sstevel@tonic-gate 	lsb.st_gid = vattr.va_gid;
4690Sstevel@tonic-gate 	lsb.st_rdev = vattr.va_rdev;
4700Sstevel@tonic-gate 	lsb.st_size = vattr.va_size;
4710Sstevel@tonic-gate 	lsb.st_atim = vattr.va_atime;
4720Sstevel@tonic-gate 	lsb.st_mtim = vattr.va_mtime;
4730Sstevel@tonic-gate 	lsb.st_ctim = vattr.va_ctime;
4740Sstevel@tonic-gate 	lsb.st_blksize = vattr.va_blksize;
4750Sstevel@tonic-gate 	lsb.st_blocks = vattr.va_nblocks;
4760Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
4770Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
4780Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
4790Sstevel@tonic-gate 			(void) strcpy(lsb.st_fstype, vswp->vsw_name);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	if (copyout(&lsb, ubp, sizeof (lsb)))
4820Sstevel@tonic-gate 		return (EFAULT);
4830Sstevel@tonic-gate 	return (0);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate static int
cstatat64(int fd,char * name,struct stat64 * sb,int follow,int flags)48711798SRoger.Faulkner@Sun.COM cstatat64(int fd, char *name, struct stat64 *sb, int follow, int flags)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	vnode_t *vp;
4900Sstevel@tonic-gate 	int error;
4910Sstevel@tonic-gate 	cred_t *cred;
4920Sstevel@tonic-gate 	int link_follow;
4932051Sprabahar 	int estale_retry = 0;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
4960Sstevel@tonic-gate lookup:
49711798SRoger.Faulkner@Sun.COM 	if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
4980Sstevel@tonic-gate 		return (set_errno(error));
4990Sstevel@tonic-gate 	error = cstat64(vp, sb, flags, cred);
5000Sstevel@tonic-gate 	crfree(cred);
5010Sstevel@tonic-gate 	VN_RELE(vp);
5020Sstevel@tonic-gate 	if (error != 0) {
5030Sstevel@tonic-gate 		if (error == ESTALE &&
50411798SRoger.Faulkner@Sun.COM 		    fs_need_estale_retry(estale_retry++))
5050Sstevel@tonic-gate 			goto lookup;
5060Sstevel@tonic-gate 		return (set_errno(error));
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 	return (0);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate #endif	/* _ILP32 */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * 64-bit kernel, 32-bit applications, 64-bit file offsets.
5170Sstevel@tonic-gate  *
5180Sstevel@tonic-gate  * We'd really like to call the "native" stat calls for these ones,
5190Sstevel@tonic-gate  * but the problem is that the 64-bit ABI defines the 'stat64' structure
5200Sstevel@tonic-gate  * differently from the way the 32-bit ABI defines it.
5210Sstevel@tonic-gate  */
5220Sstevel@tonic-gate 
52311798SRoger.Faulkner@Sun.COM static int cstatat64_32(int, char *, struct stat64_32 *, int, int);
5240Sstevel@tonic-gate static int cstat64_32(vnode_t *, struct stat64_32 *, int, cred_t *);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate int
fstat64_32(int fd,struct stat64_32 * sb)5270Sstevel@tonic-gate fstat64_32(int fd, struct stat64_32 *sb)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	FSTAT_BODY(fd, sb, cstat64_32)
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate int
fstatat64_32(int fd,char * name,struct stat64_32 * sb,int flags)5335302Sth199096 fstatat64_32(int fd, char *name, struct stat64_32 *sb, int flags)
5340Sstevel@tonic-gate {
53511798SRoger.Faulkner@Sun.COM 	int followflag;
53611798SRoger.Faulkner@Sun.COM 	int csflags;
53711798SRoger.Faulkner@Sun.COM 
53811798SRoger.Faulkner@Sun.COM 	if (name == NULL)
53911798SRoger.Faulkner@Sun.COM 		return (fstat64_32(fd, sb));
54011798SRoger.Faulkner@Sun.COM 
54111798SRoger.Faulkner@Sun.COM 	followflag = (flags & AT_SYMLINK_NOFOLLOW);
54211798SRoger.Faulkner@Sun.COM 	csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
54311798SRoger.Faulkner@Sun.COM 	if (followflag == 0)
54411798SRoger.Faulkner@Sun.COM 		csflags |= ATTR_REAL;	/* flag for procfs lookups */
54511798SRoger.Faulkner@Sun.COM 
54611798SRoger.Faulkner@Sun.COM 	return (cstatat64_32(fd, name, sb, followflag, csflags));
54711798SRoger.Faulkner@Sun.COM }
54811798SRoger.Faulkner@Sun.COM 
54911798SRoger.Faulkner@Sun.COM int
stat64_32(char * name,struct stat64_32 * sb)55011798SRoger.Faulkner@Sun.COM stat64_32(char *name, struct stat64_32 *sb)
55111798SRoger.Faulkner@Sun.COM {
55211798SRoger.Faulkner@Sun.COM 	return (fstatat64_32(AT_FDCWD, name, sb, 0));
55311798SRoger.Faulkner@Sun.COM }
55411798SRoger.Faulkner@Sun.COM 
55511798SRoger.Faulkner@Sun.COM int
lstat64_32(char * name,struct stat64_32 * sb)55611798SRoger.Faulkner@Sun.COM lstat64_32(char *name, struct stat64_32 *sb)
55711798SRoger.Faulkner@Sun.COM {
55811798SRoger.Faulkner@Sun.COM 	return (fstatat64_32(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate static int
cstat64_32(vnode_t * vp,struct stat64_32 * ubp,int flag,cred_t * cr)5620Sstevel@tonic-gate cstat64_32(vnode_t *vp, struct stat64_32 *ubp, int flag, cred_t *cr)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	struct vfssw *vswp;
5650Sstevel@tonic-gate 	struct stat64_32 lsb;
5660Sstevel@tonic-gate 	vattr_t vattr;
5670Sstevel@tonic-gate 	int error;
5680Sstevel@tonic-gate 	dev32_t st_dev, st_rdev;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
5715331Samw 	if (error = VOP_GETATTR(vp, &vattr, flag, cr, NULL))
5720Sstevel@tonic-gate 		return (error);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if (!cmpldev(&st_dev, vattr.va_fsid) ||
5750Sstevel@tonic-gate 	    !cmpldev(&st_rdev, vattr.va_rdev) ||
5760Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_atime)) ||
5770Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_mtime)) ||
5780Sstevel@tonic-gate 	    TIMESPEC_OVERFLOW(&(vattr.va_ctime)))
5790Sstevel@tonic-gate 		return (EOVERFLOW);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	bzero(&lsb, sizeof (lsb));
5820Sstevel@tonic-gate 	lsb.st_dev = st_dev;
5830Sstevel@tonic-gate 	lsb.st_ino = vattr.va_nodeid;
5840Sstevel@tonic-gate 	lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
5850Sstevel@tonic-gate 	lsb.st_nlink = vattr.va_nlink;
5860Sstevel@tonic-gate 	lsb.st_uid = vattr.va_uid;
5870Sstevel@tonic-gate 	lsb.st_gid = vattr.va_gid;
5880Sstevel@tonic-gate 	lsb.st_rdev = st_rdev;
5890Sstevel@tonic-gate 	lsb.st_size = vattr.va_size;
5900Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(lsb.st_atim), &(vattr.va_atime));
5910Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(lsb.st_mtim), &(vattr.va_mtime));
5920Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(&(lsb.st_ctim), &(vattr.va_ctime));
5930Sstevel@tonic-gate 	lsb.st_blksize = vattr.va_blksize;
5940Sstevel@tonic-gate 	lsb.st_blocks = vattr.va_nblocks;
5950Sstevel@tonic-gate 	if (vp->v_vfsp != NULL) {
5960Sstevel@tonic-gate 		vswp = &vfssw[vp->v_vfsp->vfs_fstype];
5970Sstevel@tonic-gate 		if (vswp->vsw_name && *vswp->vsw_name)
5980Sstevel@tonic-gate 			(void) strcpy(lsb.st_fstype, vswp->vsw_name);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 	if (copyout(&lsb, ubp, sizeof (lsb)))
6010Sstevel@tonic-gate 		return (EFAULT);
6020Sstevel@tonic-gate 	return (0);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate static int
cstatat64_32(int fd,char * name,struct stat64_32 * sb,int follow,int flags)60611798SRoger.Faulkner@Sun.COM cstatat64_32(int fd, char *name, struct stat64_32 *sb, int follow, int flags)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	vnode_t  *vp;
6090Sstevel@tonic-gate 	int error;
6100Sstevel@tonic-gate 	cred_t *cred;
6110Sstevel@tonic-gate 	int link_follow;
6122051Sprabahar 	int estale_retry = 0;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
6150Sstevel@tonic-gate lookup:
61611798SRoger.Faulkner@Sun.COM 	if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
6170Sstevel@tonic-gate 		return (set_errno(error));
6180Sstevel@tonic-gate 	error = cstat64_32(vp, sb, flags, cred);
6190Sstevel@tonic-gate 	crfree(cred);
6200Sstevel@tonic-gate 	VN_RELE(vp);
6210Sstevel@tonic-gate 	if (error != 0) {
6220Sstevel@tonic-gate 		if (error == ESTALE &&
62311798SRoger.Faulkner@Sun.COM 		    fs_need_estale_retry(estale_retry++))
6240Sstevel@tonic-gate 			goto lookup;
6250Sstevel@tonic-gate 		return (set_errno(error));
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	return (0);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
631