xref: /netbsd/lib/libc/compat/sys/compat_statfs.c (revision 6550d01e)
1 /*	$NetBSD: compat_statfs.c,v 1.5 2009/02/03 05:04:52 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: compat_statfs.c,v 1.5 2009/02/03 05:04:52 lukem Exp $");
35 #endif /* LIBC_SCCS and not lint */
36 
37 #define __LIBC12_SOURCE__
38 
39 #include "namespace.h"
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <compat/sys/mount.h>
44 #include <compat/include/fstypes.h>
45 #include <string.h>
46 #include <stdlib.h>
47 
48 __warn_references(statfs,
49     "warning: reference to obsolete statfs(); use statvfs()")
50 
51 __warn_references(fstatfs,
52     "warning: reference to obsolete fstatfs(); use fstatvfs()")
53 
54 __warn_references(fhstatfs,
55     "warning: reference to obsolete fhstatfs(); use fhstatvfs()")
56 
57 __warn_references(getfsstat,
58     "warning: reference to obsolete getfsstat(); use getvfsstat()")
59 
60 /*
61  * Convert from a new statvfs to an old statfs structure.
62  */
63 
64 static void vfs2fs(struct statfs12 *, const struct statvfs *);
65 
66 #define MOUNTNO_NONE	0
67 #define MOUNTNO_UFS	1		/* UNIX "Fast" Filesystem */
68 #define MOUNTNO_NFS	2		/* Network Filesystem */
69 #define MOUNTNO_MFS	3		/* Memory Filesystem */
70 #define MOUNTNO_MSDOS	4		/* MSDOS Filesystem */
71 #define MOUNTNO_CD9660	5		/* iso9660 cdrom */
72 #define MOUNTNO_FDESC	6		/* /dev/fd filesystem */
73 #define MOUNTNO_KERNFS	7		/* kernel variable filesystem */
74 #define MOUNTNO_DEVFS	8		/* device node filesystem */
75 #define MOUNTNO_AFS	9		/* AFS 3.x */
76 static const struct {
77 	const char *name;
78 	const int value;
79 } nv[] = {
80 	{ MOUNT_UFS, MOUNTNO_UFS },
81 	{ MOUNT_NFS, MOUNTNO_NFS },
82 	{ MOUNT_MFS, MOUNTNO_MFS },
83 	{ MOUNT_MSDOS, MOUNTNO_MSDOS },
84 	{ MOUNT_CD9660, MOUNTNO_CD9660 },
85 	{ MOUNT_FDESC, MOUNTNO_FDESC },
86 	{ MOUNT_KERNFS, MOUNTNO_KERNFS },
87 	{ MOUNT_AFS, MOUNTNO_AFS },
88 };
89 
90 static void
91 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
92 {
93 	size_t i = 0;
94 	bfs->f_type = 0;
95 	bfs->f_oflags = (short)fs->f_flag;
96 
97 	for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
98 		if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
99 			bfs->f_type = nv[i].value;
100 			break;
101 		}
102 	}
103 #define CLAMP(a)	(long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
104 	bfs->f_bsize = CLAMP(fs->f_frsize);
105 	bfs->f_iosize = CLAMP(fs->f_iosize);
106 	bfs->f_blocks = CLAMP(fs->f_blocks);
107 	bfs->f_bfree = CLAMP(fs->f_bfree);
108 	if (fs->f_bfree > fs->f_bresvd)
109 		bfs->f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
110 	else
111 		bfs->f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
112 	bfs->f_files = CLAMP(fs->f_files);
113 	bfs->f_ffree = CLAMP(fs->f_ffree);
114 	bfs->f_fsid = fs->f_fsidx;
115 	bfs->f_owner = fs->f_owner;
116 	bfs->f_flags = (long)fs->f_flag;
117 	bfs->f_syncwrites = CLAMP(fs->f_syncwrites);
118 	bfs->f_asyncwrites = CLAMP(fs->f_asyncwrites);
119 	(void)strncpy(bfs->f_fstypename, fs->f_fstypename,
120 	    sizeof(bfs->f_fstypename));
121 	(void)strncpy(bfs->f_mntonname, fs->f_mntonname,
122 	    sizeof(bfs->f_mntonname));
123 	(void)strncpy(bfs->f_mntfromname, fs->f_mntfromname,
124 	    sizeof(bfs->f_mntfromname));
125 }
126 
127 int
128 statfs(const char *file, struct statfs12 *ost)
129 {
130 	struct statvfs nst;
131 	int ret;
132 
133 	if ((ret = statvfs(file, &nst)) == -1)
134 		return ret;
135 	vfs2fs(ost, &nst);
136 	return ret;
137 }
138 
139 int
140 fstatfs(int f, struct statfs12 *ost)
141 {
142 	struct statvfs nst;
143 	int ret;
144 
145 	if ((ret = fstatvfs(f, &nst)) == -1)
146 		return ret;
147 	vfs2fs(ost, &nst);
148 	return ret;
149 }
150 
151 int __fhstatvfs140(const void *fhp, size_t fh_size, struct statvfs *buf,
152     int flags);
153 
154 int
155 fhstatfs(const struct compat_30_fhandle *fh, struct statfs12 *ost)
156 {
157 	struct statvfs nst;
158 	int ret;
159 
160 	if ((ret = __fhstatvfs140(fh, FHANDLE30_SIZE, &nst, ST_WAIT)) == -1)
161 		return ret;
162 	vfs2fs(ost, &nst);
163 	return ret;
164 }
165 
166 int
167 getfsstat(struct statfs12 *ost, long size, int flags)
168 {
169 	struct statvfs *nst;
170 	int ret, i;
171 	size_t bsize = (size_t)(size / sizeof(*ost)) * sizeof(*nst);
172 
173 	if (ost != NULL) {
174 		if ((nst = malloc(bsize)) == NULL)
175 			return -1;
176 	} else
177 		nst = NULL;
178 
179 	if ((ret = getvfsstat(nst, bsize, flags)) == -1)
180 		goto done;
181 	if (nst)
182 		for (i = 0; i < ret; i++)
183 			vfs2fs(&ost[i], &nst[i]);
184 done:
185 	if (nst)
186 		free(nst);
187 	return ret;
188 }
189