getmntinfo.c revision 272345
1142140Snjl/* $NetBSD: getmntinfo.c,v 1.1 2012/03/17 16:33:11 jruoho Exp $ */ 2142140Snjl/* 3142140Snjl * Copyright (c) 2007 The NetBSD Foundation, Inc. 4142140Snjl * All rights reserved. 5142140Snjl * 6142140Snjl * Redistribution and use in source and binary forms, with or without 7142140Snjl * modification, are permitted provided that the following conditions 8142140Snjl * are met: 9142140Snjl * 1. Redistributions of source code must retain the above copyright 10142140Snjl * notice, this list of conditions and the following disclaimer. 11142140Snjl * 2. Redistributions in binary form must reproduce the above copyright 12142140Snjl * notice, this list of conditions and the following disclaimer in the 13142140Snjl * documentation and/or other materials provided with the distribution. 14142140Snjl * 15142140Snjl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16142140Snjl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17142140Snjl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18142140Snjl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19142140Snjl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20142140Snjl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21142140Snjl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22142140Snjl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23142140Snjl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24142140Snjl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25142140Snjl * POSSIBILITY OF SUCH DAMAGE. 26142140Snjl */ 27142140Snjl 28142140Snjl#include <sys/param.h> 29142140Snjl#include <sys/ucred.h> 30142140Snjl#include <sys/mount.h> 31142140Snjl 32142140Snjl#include <err.h> 33142140Snjl#include <stdlib.h> 34142140Snjl#include <string.h> 35143902Snjl 36142140Snjl#define KB * 1024 37142140Snjl#define MB * 1024 KB 38142140Snjl#define GB * 1024 MB 39142140Snjl 40142140Snjlstatic struct statvfs *getnewstatvfs(void); 41182048Sjhbstatic void other_variants(const struct statvfs *, const int *, int, 42142140Snjl const int *, int); 43177040Sjhbstatic void setup_filer(void); 44142140Snjlstatic void setup_ld0g(void); 45144630Snjlstatic void setup_strpct(void); 46144630Snjl 47144630Snjlstatic struct statvfs *allstatvfs; 48144630Snjlstatic int sftotal, sfused; 49142140Snjl 50142140Snjlstruct statvfs * 51142140Snjlgetnewstatvfs(void) 52142140Snjl{ 53142140Snjl 54142140Snjl if (sftotal == sfused) { 55142140Snjl sftotal = sftotal ? sftotal * 2 : 1; 56142140Snjl allstatvfs = realloc(allstatvfs, 57142140Snjl sftotal * sizeof(struct statvfs)); 58142140Snjl if (allstatvfs == NULL) 59142140Snjl err(EXIT_FAILURE, "realloc"); 60142140Snjl } 61142140Snjl 62143902Snjl return (&allstatvfs[sfused++]); 63142140Snjl} 64142140Snjl 65142140Snjlvoid 66142140Snjlother_variants(const struct statvfs *tmpl, const int *minfree, int minfreecnt, 67142140Snjl const int *consumed, int consumedcnt) 68142140Snjl{ 69143902Snjl int64_t total, used; 70142140Snjl struct statvfs *sf; 71142140Snjl int i, j; 72142140Snjl 73143902Snjl for (i = 0; i < minfreecnt; i++) 74143902Snjl for (j = 0; j < consumedcnt; j++) { 75182048Sjhb sf = getnewstatvfs(); 76143902Snjl *sf = *tmpl; 77142140Snjl total = (int64_t)(u_long)sf->f_blocks * sf->f_bsize; 78142140Snjl used = total * consumed[j] / 100; 79142140Snjl sf->f_bfree = (total - used) / sf->f_bsize; 80142140Snjl sf->f_bavail = (total * (100 - minfree[i]) / 100 - 81142140Snjl used) / (int)sf->f_bsize; 82142140Snjl sf->f_bresvd = sf->f_bfree - sf->f_bavail; 83142140Snjl } 84142140Snjl} 85142140Snjl 86158446Snjl/* 87158446Snjl * Parameter taken from: 88142140Snjl * http://mail-index.NetBSD.org/tech-userlevel/2004/03/24/0001.html 89158446Snjl */ 90142140Snjlvoid 91158446Snjlsetup_filer(void) 92158446Snjl{ 93158446Snjl static const struct statvfs tmpl = { 94142140Snjl#define BSIZE 512 95158446Snjl#define TOTAL 1147ULL GB 96158446Snjl#define USED 132ULL MB 97182201Sjhb .f_bsize = BSIZE, 98182201Sjhb .f_frsize = BSIZE, 99142140Snjl .f_blocks = TOTAL / BSIZE, 100142140Snjl .f_bfree = (TOTAL - USED) / BSIZE, 101142140Snjl .f_bavail = (TOTAL - USED) / BSIZE, 102142140Snjl .f_bresvd = 0, 103142140Snjl .f_mntfromname = "filer:/", 104142140Snjl .f_mntonname = "/filer", 105142140Snjl#undef USED 106142140Snjl#undef TOTAL 107142140Snjl#undef BSIZE 108177296Sphk }; 109142140Snjl static const int minfree[] = { 0, 5, 10, 15, }; 110142140Snjl static const int consumed[] = { 0, 20, 60, 95, 100 }; 111142140Snjl 112142140Snjl *getnewstatvfs() = tmpl; 113142140Snjl other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]), 114143902Snjl consumed, sizeof(consumed) / sizeof(consumed[0])); 115143902Snjl} 116142140Snjl 117142140Snjl/* 118143902Snjl * Parameter taken from: 119142140Snjl * http://mail-index.NetBSD.org/current-users/2004/03/01/0038.html 120143902Snjl */ 121142140Snjlvoid 122142140Snjlsetup_ld0g(void) 123142140Snjl{ 124142140Snjl static const struct statvfs tmpl = { 125142140Snjl#define BSIZE 4096 /* Guess */ 126142140Snjl#define TOTAL 1308726116ULL KB 127142140Snjl#define USED 17901268ULL KB 128142140Snjl#define AVAIL 1225388540ULL KB 129142140Snjl .f_bsize = BSIZE, 130143902Snjl .f_frsize = BSIZE, 131142140Snjl .f_blocks = TOTAL / BSIZE, 132142140Snjl .f_bfree = (TOTAL - USED) / BSIZE, 133142140Snjl .f_bavail = AVAIL / BSIZE, 134142140Snjl .f_bresvd = (TOTAL - USED) / BSIZE - AVAIL / BSIZE, 135142140Snjl .f_mntfromname = "/dev/ld0g", 136142140Snjl .f_mntonname = "/anon-root", 137142140Snjl#undef AVAIL 138142140Snjl#undef USED 139142140Snjl#undef TOTAL 140143902Snjl#undef BSIZE 141142140Snjl }; 142142140Snjl static const int minfree[] = { 0, 5, 10, 15, }; 143142140Snjl static const int consumed[] = { 0, 20, 60, 95, 100 }; 144142140Snjl 145142140Snjl *getnewstatvfs() = tmpl; 146142140Snjl other_variants(&tmpl, minfree, sizeof(minfree) / sizeof(minfree[0]), 147142140Snjl consumed, sizeof(consumed) / sizeof(consumed[0])); 148142140Snjl} 149142140Snjl 150143902Snjl/* 151142140Snjl * Test of strpct() with huge number. 152142140Snjl */ 153142140Snjlvoid 154142140Snjlsetup_strpct(void) 155142140Snjl{ 156142140Snjl static const struct statvfs tmpl = { 157142140Snjl#define BSIZE 4096 /* Guess */ 158142140Snjl#define TOTAL 0x4ffffffffULL KB 159143902Snjl#define USED (TOTAL / 2) 160142140Snjl#define AVAIL (TOTAL / 2) 161142140Snjl .f_bsize = BSIZE, 162142140Snjl .f_frsize = BSIZE, 163142140Snjl .f_blocks = TOTAL / BSIZE, 164142140Snjl .f_bfree = (TOTAL - USED) / BSIZE, 165142140Snjl .f_bavail = AVAIL / BSIZE, 166142140Snjl .f_bresvd = (TOTAL - USED) / BSIZE - AVAIL / BSIZE, 167142140Snjl .f_mntfromname = "/dev/strpct", 168143902Snjl .f_mntonname = "/strpct", 169142140Snjl#undef AVAIL 170142140Snjl#undef USED 171142140Snjl#undef TOTAL 172142140Snjl#undef BSIZE 173142140Snjl }; 174142140Snjl 175142140Snjl *getnewstatvfs() = tmpl; 176142140Snjl} 177142140Snjl 178142140Snjl/* 179143902Snjl * Parameter taken from: 180142140Snjl * http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=23600 181142140Snjl */ 182142140Snjlstatic void 183142140Snjlsetup_pr23600(void) 184142140Snjl{ 185142140Snjl static const struct statvfs tmpl = { 186142140Snjl#define BSIZE 512 187142140Snjl#define TOTAL 20971376ULL 188142140Snjl#define USED 5719864ULL 189143902Snjl#define AVAIL 15251512ULL 190142140Snjl .f_bsize = BSIZE, 191142140Snjl .f_frsize = BSIZE, 192142140Snjl .f_blocks = TOTAL, 193142140Snjl .f_bfree = TOTAL - USED, 194142140Snjl .f_bavail = AVAIL, 195142140Snjl .f_bresvd = TOTAL - USED - AVAIL, 196142140Snjl .f_mntfromname = "/dev/wd0e", 197142140Snjl .f_mntonname = "/mount/windows/C", 198143902Snjl#undef AVAIL 199142140Snjl#undef USED 200142140Snjl#undef TOTAL 201142140Snjl#undef BSIZE 202142140Snjl }; 203142140Snjl 204142140Snjl *getnewstatvfs() = tmpl; 205142140Snjl} 206142140Snjl 207143902Snjlint 208142140Snjlgetmntinfo(struct statvfs **mntbuf, int flags) 209142140Snjl{ 210142140Snjl 211142140Snjl setup_filer(); 212142140Snjl setup_ld0g(); 213142140Snjl setup_strpct(); 214142140Snjl setup_pr23600(); 215142140Snjl 216142140Snjl *mntbuf = allstatvfs; 217142140Snjl return (sfused); 218142140Snjl} 219142140Snjl