xref: /minix3/tests/bin/df/getmntinfo.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: getmntinfo.c,v 1.1 2012/03/17 16:33:11 jruoho Exp $	*/
2*11be35a1SLionel Sambuc /*
3*11be35a1SLionel Sambuc  * Copyright (c) 2007 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc  * All rights reserved.
5*11be35a1SLionel Sambuc  *
6*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc  * are met:
9*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc  *
15*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
26*11be35a1SLionel Sambuc  */
27*11be35a1SLionel Sambuc 
28*11be35a1SLionel Sambuc #include <sys/param.h>
29*11be35a1SLionel Sambuc #include <sys/ucred.h>
30*11be35a1SLionel Sambuc #include <sys/mount.h>
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <err.h>
33*11be35a1SLionel Sambuc #include <stdlib.h>
34*11be35a1SLionel Sambuc #include <string.h>
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #define	KB		* 1024
37*11be35a1SLionel Sambuc #define	MB		* 1024 KB
38*11be35a1SLionel Sambuc #define	GB		* 1024 MB
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc static struct statvfs *getnewstatvfs(void);
41*11be35a1SLionel Sambuc static void other_variants(const struct statvfs *, const int *, int,
42*11be35a1SLionel Sambuc     const int *, int);
43*11be35a1SLionel Sambuc static void setup_filer(void);
44*11be35a1SLionel Sambuc static void setup_ld0g(void);
45*11be35a1SLionel Sambuc static void setup_strpct(void);
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc static struct statvfs *allstatvfs;
48*11be35a1SLionel Sambuc static int sftotal, sfused;
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc struct statvfs *
getnewstatvfs(void)51*11be35a1SLionel Sambuc getnewstatvfs(void)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc 	if (sftotal == sfused) {
55*11be35a1SLionel Sambuc 		sftotal = sftotal ? sftotal * 2 : 1;
56*11be35a1SLionel Sambuc 		allstatvfs = realloc(allstatvfs,
57*11be35a1SLionel Sambuc 		    sftotal * sizeof(struct statvfs));
58*11be35a1SLionel Sambuc 		if (allstatvfs == NULL)
59*11be35a1SLionel Sambuc 			err(EXIT_FAILURE, "realloc");
60*11be35a1SLionel Sambuc 	}
61*11be35a1SLionel Sambuc 
62*11be35a1SLionel Sambuc 	return (&allstatvfs[sfused++]);
63*11be35a1SLionel Sambuc }
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc void
other_variants(const struct statvfs * tmpl,const int * minfree,int minfreecnt,const int * consumed,int consumedcnt)66*11be35a1SLionel Sambuc other_variants(const struct statvfs *tmpl, const int *minfree, int minfreecnt,
67*11be35a1SLionel Sambuc     const int *consumed, int consumedcnt)
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc 	int64_t total, used;
70*11be35a1SLionel Sambuc 	struct statvfs *sf;
71*11be35a1SLionel Sambuc 	int i, j;
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 	for (i = 0; i < minfreecnt; i++)
74*11be35a1SLionel Sambuc 		for (j = 0; j < consumedcnt; j++) {
75*11be35a1SLionel Sambuc 			sf = getnewstatvfs();
76*11be35a1SLionel Sambuc 			*sf = *tmpl;
77*11be35a1SLionel Sambuc 			total = (int64_t)(u_long)sf->f_blocks * sf->f_bsize;
78*11be35a1SLionel Sambuc 			used =  total * consumed[j] / 100;
79*11be35a1SLionel Sambuc 			sf->f_bfree = (total - used) / sf->f_bsize;
80*11be35a1SLionel Sambuc 			sf->f_bavail = (total * (100 - minfree[i]) / 100 -
81*11be35a1SLionel Sambuc 			    used) / (int)sf->f_bsize;
82*11be35a1SLionel Sambuc 			sf->f_bresvd = sf->f_bfree - sf->f_bavail;
83*11be35a1SLionel Sambuc 		}
84*11be35a1SLionel Sambuc }
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc /*
87*11be35a1SLionel Sambuc  * Parameter taken from:
88*11be35a1SLionel Sambuc  * http://mail-index.NetBSD.org/tech-userlevel/2004/03/24/0001.html
89*11be35a1SLionel Sambuc  */
90*11be35a1SLionel Sambuc void
setup_filer(void)91*11be35a1SLionel Sambuc setup_filer(void)
92*11be35a1SLionel Sambuc {
93*11be35a1SLionel Sambuc 	static const struct statvfs tmpl = {
94*11be35a1SLionel Sambuc #define	BSIZE	512
95*11be35a1SLionel Sambuc #define	TOTAL	1147ULL GB
96*11be35a1SLionel Sambuc #define	USED	132ULL MB
97*11be35a1SLionel Sambuc 		.f_bsize = BSIZE,
98*11be35a1SLionel Sambuc 		.f_frsize = BSIZE,
99*11be35a1SLionel Sambuc 		.f_blocks = TOTAL / BSIZE,
100*11be35a1SLionel Sambuc 		.f_bfree = (TOTAL - USED) / BSIZE,
101*11be35a1SLionel Sambuc 		.f_bavail = (TOTAL - USED) / BSIZE,
102*11be35a1SLionel Sambuc 		.f_bresvd = 0,
103*11be35a1SLionel Sambuc 		.f_mntfromname = "filer:/",
104*11be35a1SLionel Sambuc 		.f_mntonname = "/filer",
105*11be35a1SLionel Sambuc #undef USED
106*11be35a1SLionel Sambuc #undef TOTAL
107*11be35a1SLionel Sambuc #undef BSIZE
108*11be35a1SLionel Sambuc 	};
109*11be35a1SLionel Sambuc 	static const int minfree[] = { 0, 5, 10, 15, };
110*11be35a1SLionel Sambuc 	static const int consumed[] = { 0, 20, 60, 95, 100 };
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc 	*getnewstatvfs() = tmpl;
113*11be35a1SLionel Sambuc 	other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]),
114*11be35a1SLionel Sambuc 	    consumed, sizeof(consumed) / sizeof(consumed[0]));
115*11be35a1SLionel Sambuc }
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc /*
118*11be35a1SLionel Sambuc  * Parameter taken from:
119*11be35a1SLionel Sambuc  * http://mail-index.NetBSD.org/current-users/2004/03/01/0038.html
120*11be35a1SLionel Sambuc  */
121*11be35a1SLionel Sambuc void
setup_ld0g(void)122*11be35a1SLionel Sambuc setup_ld0g(void)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc 	static const struct statvfs tmpl = {
125*11be35a1SLionel Sambuc #define	BSIZE	4096			/* Guess */
126*11be35a1SLionel Sambuc #define	TOTAL	1308726116ULL KB
127*11be35a1SLionel Sambuc #define	USED	17901268ULL KB
128*11be35a1SLionel Sambuc #define	AVAIL	1225388540ULL KB
129*11be35a1SLionel Sambuc 		.f_bsize = BSIZE,
130*11be35a1SLionel Sambuc 		.f_frsize = BSIZE,
131*11be35a1SLionel Sambuc 		.f_blocks = TOTAL / BSIZE,
132*11be35a1SLionel Sambuc 		.f_bfree = (TOTAL - USED) / BSIZE,
133*11be35a1SLionel Sambuc 		.f_bavail = AVAIL / BSIZE,
134*11be35a1SLionel Sambuc 		.f_bresvd = (TOTAL - USED) / BSIZE - AVAIL / BSIZE,
135*11be35a1SLionel Sambuc 		.f_mntfromname = "/dev/ld0g",
136*11be35a1SLionel Sambuc 		.f_mntonname = "/anon-root",
137*11be35a1SLionel Sambuc #undef AVAIL
138*11be35a1SLionel Sambuc #undef USED
139*11be35a1SLionel Sambuc #undef TOTAL
140*11be35a1SLionel Sambuc #undef BSIZE
141*11be35a1SLionel Sambuc 	};
142*11be35a1SLionel Sambuc 	static const int minfree[] = { 0, 5, 10, 15, };
143*11be35a1SLionel Sambuc 	static const int consumed[] = { 0, 20, 60, 95, 100 };
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc 	*getnewstatvfs() = tmpl;
146*11be35a1SLionel Sambuc 	other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]),
147*11be35a1SLionel Sambuc 	    consumed, sizeof(consumed) / sizeof(consumed[0]));
148*11be35a1SLionel Sambuc }
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc /*
151*11be35a1SLionel Sambuc  * Test of strpct() with huge number.
152*11be35a1SLionel Sambuc  */
153*11be35a1SLionel Sambuc void
setup_strpct(void)154*11be35a1SLionel Sambuc setup_strpct(void)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc 	static const struct statvfs tmpl = {
157*11be35a1SLionel Sambuc #define	BSIZE	4096			/* Guess */
158*11be35a1SLionel Sambuc #define	TOTAL	0x4ffffffffULL KB
159*11be35a1SLionel Sambuc #define	USED	(TOTAL / 2)
160*11be35a1SLionel Sambuc #define	AVAIL	(TOTAL / 2)
161*11be35a1SLionel Sambuc 		.f_bsize = BSIZE,
162*11be35a1SLionel Sambuc 		.f_frsize = BSIZE,
163*11be35a1SLionel Sambuc 		.f_blocks = TOTAL / BSIZE,
164*11be35a1SLionel Sambuc 		.f_bfree = (TOTAL - USED) / BSIZE,
165*11be35a1SLionel Sambuc 		.f_bavail = AVAIL / BSIZE,
166*11be35a1SLionel Sambuc 		.f_bresvd = (TOTAL - USED) / BSIZE - AVAIL / BSIZE,
167*11be35a1SLionel Sambuc 		.f_mntfromname = "/dev/strpct",
168*11be35a1SLionel Sambuc 		.f_mntonname = "/strpct",
169*11be35a1SLionel Sambuc #undef AVAIL
170*11be35a1SLionel Sambuc #undef USED
171*11be35a1SLionel Sambuc #undef TOTAL
172*11be35a1SLionel Sambuc #undef BSIZE
173*11be35a1SLionel Sambuc 	};
174*11be35a1SLionel Sambuc 
175*11be35a1SLionel Sambuc 	*getnewstatvfs() = tmpl;
176*11be35a1SLionel Sambuc }
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc /*
179*11be35a1SLionel Sambuc  * Parameter taken from:
180*11be35a1SLionel Sambuc  * http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=23600
181*11be35a1SLionel Sambuc  */
182*11be35a1SLionel Sambuc static void
setup_pr23600(void)183*11be35a1SLionel Sambuc setup_pr23600(void)
184*11be35a1SLionel Sambuc {
185*11be35a1SLionel Sambuc 	static const struct statvfs tmpl = {
186*11be35a1SLionel Sambuc #define	BSIZE	512
187*11be35a1SLionel Sambuc #define	TOTAL	20971376ULL
188*11be35a1SLionel Sambuc #define	USED	5719864ULL
189*11be35a1SLionel Sambuc #define	AVAIL	15251512ULL
190*11be35a1SLionel Sambuc 		.f_bsize = BSIZE,
191*11be35a1SLionel Sambuc 		.f_frsize = BSIZE,
192*11be35a1SLionel Sambuc 		.f_blocks = TOTAL,
193*11be35a1SLionel Sambuc 		.f_bfree = TOTAL - USED,
194*11be35a1SLionel Sambuc 		.f_bavail = AVAIL,
195*11be35a1SLionel Sambuc 		.f_bresvd = TOTAL - USED - AVAIL,
196*11be35a1SLionel Sambuc 		.f_mntfromname = "/dev/wd0e",
197*11be35a1SLionel Sambuc 		.f_mntonname = "/mount/windows/C",
198*11be35a1SLionel Sambuc #undef AVAIL
199*11be35a1SLionel Sambuc #undef USED
200*11be35a1SLionel Sambuc #undef TOTAL
201*11be35a1SLionel Sambuc #undef BSIZE
202*11be35a1SLionel Sambuc 	};
203*11be35a1SLionel Sambuc 
204*11be35a1SLionel Sambuc 	*getnewstatvfs() = tmpl;
205*11be35a1SLionel Sambuc }
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc int
getmntinfo(struct statvfs ** mntbuf,int flags)208*11be35a1SLionel Sambuc getmntinfo(struct statvfs **mntbuf, int flags)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc 
211*11be35a1SLionel Sambuc 	setup_filer();
212*11be35a1SLionel Sambuc 	setup_ld0g();
213*11be35a1SLionel Sambuc 	setup_strpct();
214*11be35a1SLionel Sambuc 	setup_pr23600();
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc 	*mntbuf = allstatvfs;
217*11be35a1SLionel Sambuc 	return (sfused);
218*11be35a1SLionel Sambuc }
219