xref: /illumos-gate/usr/src/uts/common/syscall/statfs.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/inttypes.h>
38 #include <sys/types.h>
39 #include <sys/t_lock.h>
40 #include <sys/param.h>
41 #include <sys/errno.h>
42 #include <sys/fstyp.h>
43 #include <sys/systm.h>
44 #include <sys/vfs.h>
45 #include <sys/statfs.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/cmn_err.h>
49 #include <sys/debug.h>
50 #include <sys/pathname.h>
51 
52 #include <vm/page.h>
53 
54 #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
55 
56 /*
57  * statfs(2) and fstatfs(2) have been replaced by statvfs(2) and
58  * fstatvfs(2) and will be removed from the system in a near-future
59  * release.
60  *
61  * Supported here purely for 32-bit compatibility.
62  */
63 
64 static int cstatfs(struct vfs *, struct statfs32 *, int);
65 
66 int
67 statfs32(char *fname, struct statfs32 *sbp, int32_t len, int32_t fstyp)
68 {
69 	vnode_t *vp;
70 	int error;
71 
72 lookup:
73 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
74 		if (error == ESTALE)
75 			goto lookup;
76 		return (set_errno(error));
77 	}
78 	if (fstyp != 0)
79 		error = EINVAL;
80 	else
81 		error = cstatfs(vp->v_vfsp, sbp, len);
82 	VN_RELE(vp);
83 	if (error) {
84 		if (error == ESTALE)
85 			goto lookup;
86 		return (set_errno(error));
87 	}
88 	return (0);
89 }
90 
91 int
92 fstatfs32(int32_t fdes, struct statfs32 *sbp, int32_t len, int32_t fstyp)
93 {
94 	struct file *fp;
95 	int error;
96 
97 	if (fstyp != 0)
98 		return (set_errno(EINVAL));
99 	if ((fp = getf(fdes)) == NULL)
100 		return (set_errno(EBADF));
101 	error = cstatfs(fp->f_vnode->v_vfsp, sbp, len);
102 	releasef(fdes);
103 	if (error)
104 		return (set_errno(error));
105 	return (0);
106 }
107 
108 /*
109  * Common routine for fstatfs and statfs.
110  */
111 static int
112 cstatfs(struct vfs *vfsp, struct statfs32 *sbp, int len)
113 {
114 	struct statfs32 sfs;
115 	struct statvfs64 svfs;
116 	int error, i;
117 	char *cp, *cp2;
118 	struct vfssw *vswp;
119 
120 	if (len < 0 || len > sizeof (struct statfs))
121 		return (EINVAL);
122 	if (error = VFS_STATVFS(vfsp, &svfs))
123 		return (error);
124 
125 	if (svfs.f_blocks > UINT32_MAX || svfs.f_bfree > UINT32_MAX ||
126 	    svfs.f_files > UINT32_MAX || svfs.f_ffree > UINT32_MAX)
127 	    return (EOVERFLOW);
128 	/*
129 	 * Map statvfs fields into the old statfs structure.
130 	 */
131 	bzero(&sfs, sizeof (sfs));
132 	sfs.f_bsize = svfs.f_bsize;
133 	sfs.f_frsize = (svfs.f_frsize == svfs.f_bsize) ? 0 : svfs.f_frsize;
134 	sfs.f_blocks = svfs.f_blocks * (svfs.f_frsize / 512);
135 	sfs.f_bfree = svfs.f_bfree * (svfs.f_frsize / 512);
136 	sfs.f_files = svfs.f_files;
137 	sfs.f_ffree = svfs.f_ffree;
138 
139 	cp = svfs.f_fstr;
140 	cp2 = sfs.f_fname;
141 	i = 0;
142 	while (i++ < sizeof (sfs.f_fname))
143 		if (*cp != '\0')
144 			*cp2++ = *cp++;
145 		else
146 			*cp2++ = '\0';
147 	while (*cp != '\0' &&
148 	    i++ < (sizeof (svfs.f_fstr) - sizeof (sfs.f_fpack)))
149 		cp++;
150 	(void) strncpy(sfs.f_fpack, cp + 1, sizeof (sfs.f_fpack));
151 	if ((vswp = vfs_getvfssw(svfs.f_basetype)) == NULL)
152 		sfs.f_fstyp = 0;
153 	else {
154 		sfs.f_fstyp = vswp - vfssw;
155 		vfs_unrefvfssw(vswp);
156 	}
157 
158 	if (copyout(&sfs, sbp, len))
159 		return (EFAULT);
160 
161 	return (0);
162 }
163 
164 #endif	/* _SYSCALL32_IMPL || _ILP32 */
165