xref: /onnv-gate/usr/src/uts/common/syscall/statfs.c (revision 2051:50a87bae0c3a)
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
5*2051Sprabahar  * Common Development and Distribution License (the "License").
6*2051Sprabahar  * 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  */
210Sstevel@tonic-gate /*
22*2051Sprabahar  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/inttypes.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/t_lock.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <sys/errno.h>
410Sstevel@tonic-gate #include <sys/fstyp.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/vfs.h>
440Sstevel@tonic-gate #include <sys/statfs.h>
450Sstevel@tonic-gate #include <sys/vnode.h>
460Sstevel@tonic-gate #include <sys/file.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/pathname.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <vm/page.h>
52*2051Sprabahar #include <fs/fs_subr.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * statfs(2) and fstatfs(2) have been replaced by statvfs(2) and
580Sstevel@tonic-gate  * fstatvfs(2) and will be removed from the system in a near-future
590Sstevel@tonic-gate  * release.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Supported here purely for 32-bit compatibility.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static int cstatfs(struct vfs *, struct statfs32 *, int);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate int
statfs32(char * fname,struct statfs32 * sbp,int32_t len,int32_t fstyp)670Sstevel@tonic-gate statfs32(char *fname, struct statfs32 *sbp, int32_t len, int32_t fstyp)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	vnode_t *vp;
700Sstevel@tonic-gate 	int error;
71*2051Sprabahar 	int estale_retry = 0;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate lookup:
740Sstevel@tonic-gate 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
75*2051Sprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
760Sstevel@tonic-gate 			goto lookup;
770Sstevel@tonic-gate 		return (set_errno(error));
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	if (fstyp != 0)
800Sstevel@tonic-gate 		error = EINVAL;
810Sstevel@tonic-gate 	else
820Sstevel@tonic-gate 		error = cstatfs(vp->v_vfsp, sbp, len);
830Sstevel@tonic-gate 	VN_RELE(vp);
840Sstevel@tonic-gate 	if (error) {
85*2051Sprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
860Sstevel@tonic-gate 			goto lookup;
870Sstevel@tonic-gate 		return (set_errno(error));
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 	return (0);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate int
fstatfs32(int32_t fdes,struct statfs32 * sbp,int32_t len,int32_t fstyp)930Sstevel@tonic-gate fstatfs32(int32_t fdes, struct statfs32 *sbp, int32_t len, int32_t fstyp)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	struct file *fp;
960Sstevel@tonic-gate 	int error;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (fstyp != 0)
990Sstevel@tonic-gate 		return (set_errno(EINVAL));
1000Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
1010Sstevel@tonic-gate 		return (set_errno(EBADF));
1020Sstevel@tonic-gate 	error = cstatfs(fp->f_vnode->v_vfsp, sbp, len);
1030Sstevel@tonic-gate 	releasef(fdes);
1040Sstevel@tonic-gate 	if (error)
1050Sstevel@tonic-gate 		return (set_errno(error));
1060Sstevel@tonic-gate 	return (0);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Common routine for fstatfs and statfs.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate static int
cstatfs(struct vfs * vfsp,struct statfs32 * sbp,int len)1130Sstevel@tonic-gate cstatfs(struct vfs *vfsp, struct statfs32 *sbp, int len)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	struct statfs32 sfs;
1160Sstevel@tonic-gate 	struct statvfs64 svfs;
1170Sstevel@tonic-gate 	int error, i;
1180Sstevel@tonic-gate 	char *cp, *cp2;
1190Sstevel@tonic-gate 	struct vfssw *vswp;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (len < 0 || len > sizeof (struct statfs))
1220Sstevel@tonic-gate 		return (EINVAL);
1230Sstevel@tonic-gate 	if (error = VFS_STATVFS(vfsp, &svfs))
1240Sstevel@tonic-gate 		return (error);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (svfs.f_blocks > UINT32_MAX || svfs.f_bfree > UINT32_MAX ||
1270Sstevel@tonic-gate 	    svfs.f_files > UINT32_MAX || svfs.f_ffree > UINT32_MAX)
1280Sstevel@tonic-gate 	    return (EOVERFLOW);
1290Sstevel@tonic-gate 	/*
1300Sstevel@tonic-gate 	 * Map statvfs fields into the old statfs structure.
1310Sstevel@tonic-gate 	 */
1320Sstevel@tonic-gate 	bzero(&sfs, sizeof (sfs));
1330Sstevel@tonic-gate 	sfs.f_bsize = svfs.f_bsize;
1340Sstevel@tonic-gate 	sfs.f_frsize = (svfs.f_frsize == svfs.f_bsize) ? 0 : svfs.f_frsize;
1350Sstevel@tonic-gate 	sfs.f_blocks = svfs.f_blocks * (svfs.f_frsize / 512);
1360Sstevel@tonic-gate 	sfs.f_bfree = svfs.f_bfree * (svfs.f_frsize / 512);
1370Sstevel@tonic-gate 	sfs.f_files = svfs.f_files;
1380Sstevel@tonic-gate 	sfs.f_ffree = svfs.f_ffree;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	cp = svfs.f_fstr;
1410Sstevel@tonic-gate 	cp2 = sfs.f_fname;
1420Sstevel@tonic-gate 	i = 0;
1430Sstevel@tonic-gate 	while (i++ < sizeof (sfs.f_fname))
1440Sstevel@tonic-gate 		if (*cp != '\0')
1450Sstevel@tonic-gate 			*cp2++ = *cp++;
1460Sstevel@tonic-gate 		else
1470Sstevel@tonic-gate 			*cp2++ = '\0';
1480Sstevel@tonic-gate 	while (*cp != '\0' &&
1490Sstevel@tonic-gate 	    i++ < (sizeof (svfs.f_fstr) - sizeof (sfs.f_fpack)))
1500Sstevel@tonic-gate 		cp++;
1510Sstevel@tonic-gate 	(void) strncpy(sfs.f_fpack, cp + 1, sizeof (sfs.f_fpack));
1520Sstevel@tonic-gate 	if ((vswp = vfs_getvfssw(svfs.f_basetype)) == NULL)
1530Sstevel@tonic-gate 		sfs.f_fstyp = 0;
1540Sstevel@tonic-gate 	else {
1550Sstevel@tonic-gate 		sfs.f_fstyp = vswp - vfssw;
1560Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (copyout(&sfs, sbp, len))
1600Sstevel@tonic-gate 		return (EFAULT);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	return (0);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL || _ILP32 */
166