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