1 /* $NetBSD: getmntinfo.c,v 1.1 2012/03/17 16:33:11 jruoho Exp $ */
3 * Copyright (c) 2007 The NetBSD Foundation, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/param.h>
29 #include <sys/ucred.h>
30 #include <sys/mount.h>
40 static struct statvfs *getnewstatvfs(void);
41 static void other_variants(const struct statvfs *, const int *, int,
43 static void setup_filer(void);
44 static void setup_ld0g(void);
45 static void setup_strpct(void);
47 static struct statvfs *allstatvfs;
48 static int sftotal, sfused;
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");
62 return (&allstatvfs[sfused++]);
66 other_variants(const struct statvfs *tmpl, const int *minfree, int minfreecnt,
67 const int *consumed, int consumedcnt)
73 for (i = 0; i < minfreecnt; i++)
74 for (j = 0; j < consumedcnt; j++) {
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;
87 * Parameter taken from:
88 * http://mail-index.NetBSD.org/tech-userlevel/2004/03/24/0001.html
93 static const struct statvfs tmpl = {
95 #define TOTAL 1147ULL GB
96 #define USED 132ULL MB
99 .f_blocks = TOTAL / BSIZE,
100 .f_bfree = (TOTAL - USED) / BSIZE,
101 .f_bavail = (TOTAL - USED) / BSIZE,
103 .f_mntfromname = "filer:/",
104 .f_mntonname = "/filer",
109 static const int minfree[] = { 0, 5, 10, 15, };
110 static const int consumed[] = { 0, 20, 60, 95, 100 };
112 *getnewstatvfs() = tmpl;
113 other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]),
114 consumed, sizeof(consumed) / sizeof(consumed[0]));
118 * Parameter taken from:
119 * http://mail-index.NetBSD.org/current-users/2004/03/01/0038.html
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
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",
142 static const int minfree[] = { 0, 5, 10, 15, };
143 static const int consumed[] = { 0, 20, 60, 95, 100 };
145 *getnewstatvfs() = tmpl;
146 other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]),
147 consumed, sizeof(consumed) / sizeof(consumed[0]));
151 * Test of strpct() with huge number.
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)
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",
175 *getnewstatvfs() = tmpl;
179 * Parameter taken from:
180 * http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=23600
185 static const struct statvfs tmpl = {
187 #define TOTAL 20971376ULL
188 #define USED 5719864ULL
189 #define AVAIL 15251512ULL
193 .f_bfree = TOTAL - USED,
195 .f_bresvd = TOTAL - USED - AVAIL,
196 .f_mntfromname = "/dev/wd0e",
197 .f_mntonname = "/mount/windows/C",
204 *getnewstatvfs() = tmpl;
208 getmntinfo(struct statvfs **mntbuf, int flags)
216 *mntbuf = allstatvfs;