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