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