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