xref: /illumos-gate/usr/src/uts/common/syscall/statfs.c (revision dd29fa4a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*dd29fa4aSprabahar  * Common Development and Distribution License (the "License").
6*dd29fa4aSprabahar  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*dd29fa4aSprabahar  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
447c478bd9Sstevel@tonic-gate #include <sys/statfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <vm/page.h>
52*dd29fa4aSprabahar #include <fs/fs_subr.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * statfs(2) and fstatfs(2) have been replaced by statvfs(2) and
587c478bd9Sstevel@tonic-gate  * fstatvfs(2) and will be removed from the system in a near-future
597c478bd9Sstevel@tonic-gate  * release.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * Supported here purely for 32-bit compatibility.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static int cstatfs(struct vfs *, struct statfs32 *, int);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int
statfs32(char * fname,struct statfs32 * sbp,int32_t len,int32_t fstyp)677c478bd9Sstevel@tonic-gate statfs32(char *fname, struct statfs32 *sbp, int32_t len, int32_t fstyp)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	vnode_t *vp;
707c478bd9Sstevel@tonic-gate 	int error;
71*dd29fa4aSprabahar 	int estale_retry = 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate lookup:
747c478bd9Sstevel@tonic-gate 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
75*dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
767c478bd9Sstevel@tonic-gate 			goto lookup;
777c478bd9Sstevel@tonic-gate 		return (set_errno(error));
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	if (fstyp != 0)
807c478bd9Sstevel@tonic-gate 		error = EINVAL;
817c478bd9Sstevel@tonic-gate 	else
827c478bd9Sstevel@tonic-gate 		error = cstatfs(vp->v_vfsp, sbp, len);
837c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
847c478bd9Sstevel@tonic-gate 	if (error) {
85*dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
867c478bd9Sstevel@tonic-gate 			goto lookup;
877c478bd9Sstevel@tonic-gate 		return (set_errno(error));
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	return (0);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate int
fstatfs32(int32_t fdes,struct statfs32 * sbp,int32_t len,int32_t fstyp)937c478bd9Sstevel@tonic-gate fstatfs32(int32_t fdes, struct statfs32 *sbp, int32_t len, int32_t fstyp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	struct file *fp;
967c478bd9Sstevel@tonic-gate 	int error;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (fstyp != 0)
997c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1007c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
1017c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
1027c478bd9Sstevel@tonic-gate 	error = cstatfs(fp->f_vnode->v_vfsp, sbp, len);
1037c478bd9Sstevel@tonic-gate 	releasef(fdes);
1047c478bd9Sstevel@tonic-gate 	if (error)
1057c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1067c478bd9Sstevel@tonic-gate 	return (0);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Common routine for fstatfs and statfs.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static int
cstatfs(struct vfs * vfsp,struct statfs32 * sbp,int len)1137c478bd9Sstevel@tonic-gate cstatfs(struct vfs *vfsp, struct statfs32 *sbp, int len)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	struct statfs32 sfs;
1167c478bd9Sstevel@tonic-gate 	struct statvfs64 svfs;
1177c478bd9Sstevel@tonic-gate 	int error, i;
1187c478bd9Sstevel@tonic-gate 	char *cp, *cp2;
1197c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (len < 0 || len > sizeof (struct statfs))
1227c478bd9Sstevel@tonic-gate 		return (EINVAL);
1237c478bd9Sstevel@tonic-gate 	if (error = VFS_STATVFS(vfsp, &svfs))
1247c478bd9Sstevel@tonic-gate 		return (error);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (svfs.f_blocks > UINT32_MAX || svfs.f_bfree > UINT32_MAX ||
1277c478bd9Sstevel@tonic-gate 	    svfs.f_files > UINT32_MAX || svfs.f_ffree > UINT32_MAX)
1287c478bd9Sstevel@tonic-gate 	    return (EOVERFLOW);
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Map statvfs fields into the old statfs structure.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	bzero(&sfs, sizeof (sfs));
1337c478bd9Sstevel@tonic-gate 	sfs.f_bsize = svfs.f_bsize;
1347c478bd9Sstevel@tonic-gate 	sfs.f_frsize = (svfs.f_frsize == svfs.f_bsize) ? 0 : svfs.f_frsize;
1357c478bd9Sstevel@tonic-gate 	sfs.f_blocks = svfs.f_blocks * (svfs.f_frsize / 512);
1367c478bd9Sstevel@tonic-gate 	sfs.f_bfree = svfs.f_bfree * (svfs.f_frsize / 512);
1377c478bd9Sstevel@tonic-gate 	sfs.f_files = svfs.f_files;
1387c478bd9Sstevel@tonic-gate 	sfs.f_ffree = svfs.f_ffree;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	cp = svfs.f_fstr;
1417c478bd9Sstevel@tonic-gate 	cp2 = sfs.f_fname;
1427c478bd9Sstevel@tonic-gate 	i = 0;
1437c478bd9Sstevel@tonic-gate 	while (i++ < sizeof (sfs.f_fname))
1447c478bd9Sstevel@tonic-gate 		if (*cp != '\0')
1457c478bd9Sstevel@tonic-gate 			*cp2++ = *cp++;
1467c478bd9Sstevel@tonic-gate 		else
1477c478bd9Sstevel@tonic-gate 			*cp2++ = '\0';
1487c478bd9Sstevel@tonic-gate 	while (*cp != '\0' &&
1497c478bd9Sstevel@tonic-gate 	    i++ < (sizeof (svfs.f_fstr) - sizeof (sfs.f_fpack)))
1507c478bd9Sstevel@tonic-gate 		cp++;
1517c478bd9Sstevel@tonic-gate 	(void) strncpy(sfs.f_fpack, cp + 1, sizeof (sfs.f_fpack));
1527c478bd9Sstevel@tonic-gate 	if ((vswp = vfs_getvfssw(svfs.f_basetype)) == NULL)
1537c478bd9Sstevel@tonic-gate 		sfs.f_fstyp = 0;
1547c478bd9Sstevel@tonic-gate 	else {
1557c478bd9Sstevel@tonic-gate 		sfs.f_fstyp = vswp - vfssw;
1567c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (copyout(&sfs, sbp, len))
1607c478bd9Sstevel@tonic-gate 		return (EFAULT);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	return (0);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL || _ILP32 */
166