1272343Sngie/* $NetBSD: getmntinfo.c,v 1.1 2012/03/17 16:33:11 jruoho Exp $ */ 2272343Sngie/* 3272343Sngie * Copyright (c) 2007 The NetBSD Foundation, Inc. 4272343Sngie * All rights reserved. 5272343Sngie * 6272343Sngie * Redistribution and use in source and binary forms, with or without 7272343Sngie * modification, are permitted provided that the following conditions 8272343Sngie * are met: 9272343Sngie * 1. Redistributions of source code must retain the above copyright 10272343Sngie * notice, this list of conditions and the following disclaimer. 11272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 12272343Sngie * notice, this list of conditions and the following disclaimer in the 13272343Sngie * documentation and/or other materials provided with the distribution. 14272343Sngie * 15272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25272343Sngie * POSSIBILITY OF SUCH DAMAGE. 26272343Sngie */ 27272343Sngie 28272343Sngie#include <sys/param.h> 29272343Sngie#include <sys/ucred.h> 30272343Sngie#include <sys/mount.h> 31272343Sngie 32272343Sngie#include <err.h> 33272343Sngie#include <stdlib.h> 34272343Sngie#include <string.h> 35272343Sngie 36272343Sngie#define KB * 1024 37272343Sngie#define MB * 1024 KB 38272343Sngie#define GB * 1024 MB 39272343Sngie 40272343Sngiestatic struct statvfs *getnewstatvfs(void); 41272343Sngiestatic void other_variants(const struct statvfs *, const int *, int, 42272343Sngie const int *, int); 43272343Sngiestatic void setup_filer(void); 44272343Sngiestatic void setup_ld0g(void); 45272343Sngiestatic void setup_strpct(void); 46272343Sngie 47272343Sngiestatic struct statvfs *allstatvfs; 48272343Sngiestatic int sftotal, sfused; 49272343Sngie 50272343Sngiestruct statvfs * 51272343Sngiegetnewstatvfs(void) 52272343Sngie{ 53272343Sngie 54272343Sngie if (sftotal == sfused) { 55272343Sngie sftotal = sftotal ? sftotal * 2 : 1; 56272343Sngie allstatvfs = realloc(allstatvfs, 57272343Sngie sftotal * sizeof(struct statvfs)); 58272343Sngie if (allstatvfs == NULL) 59272343Sngie err(EXIT_FAILURE, "realloc"); 60272343Sngie } 61272343Sngie 62272343Sngie return (&allstatvfs[sfused++]); 63272343Sngie} 64272343Sngie 65272343Sngievoid 66272343Sngieother_variants(const struct statvfs *tmpl, const int *minfree, int minfreecnt, 67272343Sngie const int *consumed, int consumedcnt) 68272343Sngie{ 69272343Sngie int64_t total, used; 70272343Sngie struct statvfs *sf; 71272343Sngie int i, j; 72272343Sngie 73272343Sngie for (i = 0; i < minfreecnt; i++) 74272343Sngie for (j = 0; j < consumedcnt; j++) { 75272343Sngie sf = getnewstatvfs(); 76272343Sngie *sf = *tmpl; 77272343Sngie total = (int64_t)(u_long)sf->f_blocks * sf->f_bsize; 78272343Sngie used = total * consumed[j] / 100; 79272343Sngie sf->f_bfree = (total - used) / sf->f_bsize; 80272343Sngie sf->f_bavail = (total * (100 - minfree[i]) / 100 - 81272343Sngie used) / (int)sf->f_bsize; 82272343Sngie sf->f_bresvd = sf->f_bfree - sf->f_bavail; 83272343Sngie } 84272343Sngie} 85272343Sngie 86272343Sngie/* 87272343Sngie * Parameter taken from: 88272343Sngie * http://mail-index.NetBSD.org/tech-userlevel/2004/03/24/0001.html 89272343Sngie */ 90272343Sngievoid 91272343Sngiesetup_filer(void) 92272343Sngie{ 93272343Sngie static const struct statvfs tmpl = { 94272343Sngie#define BSIZE 512 95272343Sngie#define TOTAL 1147ULL GB 96272343Sngie#define USED 132ULL MB 97272343Sngie .f_bsize = BSIZE, 98272343Sngie .f_frsize = BSIZE, 99272343Sngie .f_blocks = TOTAL / BSIZE, 100272343Sngie .f_bfree = (TOTAL - USED) / BSIZE, 101272343Sngie .f_bavail = (TOTAL - USED) / BSIZE, 102272343Sngie .f_bresvd = 0, 103272343Sngie .f_mntfromname = "filer:/", 104272343Sngie .f_mntonname = "/filer", 105272343Sngie#undef USED 106272343Sngie#undef TOTAL 107272343Sngie#undef BSIZE 108272343Sngie }; 109272343Sngie static const int minfree[] = { 0, 5, 10, 15, }; 110272343Sngie static const int consumed[] = { 0, 20, 60, 95, 100 }; 111272343Sngie 112272343Sngie *getnewstatvfs() = tmpl; 113272343Sngie other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]), 114272343Sngie consumed, sizeof(consumed) / sizeof(consumed[0])); 115272343Sngie} 116272343Sngie 117272343Sngie/* 118272343Sngie * Parameter taken from: 119272343Sngie * http://mail-index.NetBSD.org/current-users/2004/03/01/0038.html 120272343Sngie */ 121272343Sngievoid 122272343Sngiesetup_ld0g(void) 123272343Sngie{ 124272343Sngie static const struct statvfs tmpl = { 125272343Sngie#define BSIZE 4096 /* Guess */ 126272343Sngie#define TOTAL 1308726116ULL KB 127272343Sngie#define USED 17901268ULL KB 128272343Sngie#define AVAIL 1225388540ULL KB 129272343Sngie .f_bsize = BSIZE, 130272343Sngie .f_frsize = BSIZE, 131272343Sngie .f_blocks = TOTAL / BSIZE, 132272343Sngie .f_bfree = (TOTAL - USED) / BSIZE, 133272343Sngie .f_bavail = AVAIL / BSIZE, 134272343Sngie .f_bresvd = (TOTAL - USED) / BSIZE - AVAIL / BSIZE, 135272343Sngie .f_mntfromname = "/dev/ld0g", 136272343Sngie .f_mntonname = "/anon-root", 137272343Sngie#undef AVAIL 138272343Sngie#undef USED 139272343Sngie#undef TOTAL 140272343Sngie#undef BSIZE 141272343Sngie }; 142272343Sngie static const int minfree[] = { 0, 5, 10, 15, }; 143272343Sngie static const int consumed[] = { 0, 20, 60, 95, 100 }; 144272343Sngie 145272343Sngie *getnewstatvfs() = tmpl; 146272343Sngie other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]), 147272343Sngie consumed, sizeof(consumed) / sizeof(consumed[0])); 148272343Sngie} 149272343Sngie 150272343Sngie/* 151272343Sngie * Test of strpct() with huge number. 152272343Sngie */ 153272343Sngievoid 154272343Sngiesetup_strpct(void) 155272343Sngie{ 156272343Sngie static const struct statvfs tmpl = { 157272343Sngie#define BSIZE 4096 /* Guess */ 158272343Sngie#define TOTAL 0x4ffffffffULL KB 159272343Sngie#define USED (TOTAL / 2) 160272343Sngie#define AVAIL (TOTAL / 2) 161272343Sngie .f_bsize = BSIZE, 162272343Sngie .f_frsize = BSIZE, 163272343Sngie .f_blocks = TOTAL / BSIZE, 164272343Sngie .f_bfree = (TOTAL - USED) / BSIZE, 165272343Sngie .f_bavail = AVAIL / BSIZE, 166272343Sngie .f_bresvd = (TOTAL - USED) / BSIZE - AVAIL / BSIZE, 167272343Sngie .f_mntfromname = "/dev/strpct", 168272343Sngie .f_mntonname = "/strpct", 169272343Sngie#undef AVAIL 170272343Sngie#undef USED 171272343Sngie#undef TOTAL 172272343Sngie#undef BSIZE 173272343Sngie }; 174272343Sngie 175272343Sngie *getnewstatvfs() = tmpl; 176272343Sngie} 177272343Sngie 178272343Sngie/* 179272343Sngie * Parameter taken from: 180272343Sngie * http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=23600 181272343Sngie */ 182272343Sngiestatic void 183272343Sngiesetup_pr23600(void) 184272343Sngie{ 185272343Sngie static const struct statvfs tmpl = { 186272343Sngie#define BSIZE 512 187272343Sngie#define TOTAL 20971376ULL 188272343Sngie#define USED 5719864ULL 189272343Sngie#define AVAIL 15251512ULL 190272343Sngie .f_bsize = BSIZE, 191272343Sngie .f_frsize = BSIZE, 192272343Sngie .f_blocks = TOTAL, 193272343Sngie .f_bfree = TOTAL - USED, 194272343Sngie .f_bavail = AVAIL, 195272343Sngie .f_bresvd = TOTAL - USED - AVAIL, 196272343Sngie .f_mntfromname = "/dev/wd0e", 197272343Sngie .f_mntonname = "/mount/windows/C", 198272343Sngie#undef AVAIL 199272343Sngie#undef USED 200272343Sngie#undef TOTAL 201272343Sngie#undef BSIZE 202272343Sngie }; 203272343Sngie 204272343Sngie *getnewstatvfs() = tmpl; 205272343Sngie} 206272343Sngie 207272343Sngieint 208272343Sngiegetmntinfo(struct statvfs **mntbuf, int flags) 209272343Sngie{ 210272343Sngie 211272343Sngie setup_filer(); 212272343Sngie setup_ld0g(); 213272343Sngie setup_strpct(); 214272343Sngie setup_pr23600(); 215272343Sngie 216272343Sngie *mntbuf = allstatvfs; 217272343Sngie return (sfused); 218272343Sngie} 219