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