14b88c807SRodney W. Grimes /* 24b88c807SRodney W. Grimes * Copyright (c) 1980, 1990, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * (c) UNIX System Laboratories, Inc. 54b88c807SRodney W. Grimes * All or some portions of this file are derived from material licensed 64b88c807SRodney W. Grimes * to the University of California by American Telephone and Telegraph 74b88c807SRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 84b88c807SRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 94b88c807SRodney W. Grimes * 104b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 114b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 124b88c807SRodney W. Grimes * are met: 134b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 154b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 164b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 174b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 184b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 194b88c807SRodney W. Grimes * must display the following acknowledgement: 204b88c807SRodney W. Grimes * This product includes software developed by the University of 214b88c807SRodney W. Grimes * California, Berkeley and its contributors. 224b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 234b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 244b88c807SRodney W. Grimes * without specific prior written permission. 254b88c807SRodney W. Grimes * 264b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 274b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 284b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 294b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 304b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 314b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 324b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 334b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 344b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 354b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 364b88c807SRodney W. Grimes * SUCH DAMAGE. 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 3909a80d48SDavid E. O'Brien #if 0 404b88c807SRodney W. Grimes #ifndef lint 4116cc192aSSteve Price static const char copyright[] = 424b88c807SRodney W. Grimes "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 434b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 444b88c807SRodney W. Grimes #endif /* not lint */ 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #ifndef lint 4716cc192aSSteve Price static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; 484b88c807SRodney W. Grimes #endif /* not lint */ 4909a80d48SDavid E. O'Brien #endif 505eb43ac2SDavid E. O'Brien #include <sys/cdefs.h> 515eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 524b88c807SRodney W. Grimes 534b88c807SRodney W. Grimes #include <sys/param.h> 544b88c807SRodney W. Grimes #include <sys/stat.h> 554b88c807SRodney W. Grimes #include <sys/mount.h> 56a25695c3SJim Pirzyk #include <sys/sysctl.h> 57a78192e3SBruce Evans #include <ufs/ufs/ufsmount.h> 584b88c807SRodney W. Grimes #include <err.h> 59dd6d33e8SMichael Haro #include <math.h> 600bd9f151SIan Dowse #include <stdint.h> 614b88c807SRodney W. Grimes #include <stdio.h> 624b88c807SRodney W. Grimes #include <stdlib.h> 634b88c807SRodney W. Grimes #include <string.h> 64dd6d33e8SMichael Haro #include <sysexits.h> 654b88c807SRodney W. Grimes #include <unistd.h> 664b88c807SRodney W. Grimes 67532aff98SPoul-Henning Kamp #include "extern.h" 68532aff98SPoul-Henning Kamp 69dd6d33e8SMichael Haro #define UNITS_SI 1 70dd6d33e8SMichael Haro #define UNITS_2 2 71dd6d33e8SMichael Haro 72dd6d33e8SMichael Haro #define KILO_SZ(n) (n) 73dd6d33e8SMichael Haro #define MEGA_SZ(n) ((n) * (n)) 74dd6d33e8SMichael Haro #define GIGA_SZ(n) ((n) * (n) * (n)) 75dd6d33e8SMichael Haro #define TERA_SZ(n) ((n) * (n) * (n) * (n)) 76dd6d33e8SMichael Haro #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n)) 77dd6d33e8SMichael Haro 78dd6d33e8SMichael Haro #define KILO_2_SZ (KILO_SZ(1024ULL)) 79dd6d33e8SMichael Haro #define MEGA_2_SZ (MEGA_SZ(1024ULL)) 80dd6d33e8SMichael Haro #define GIGA_2_SZ (GIGA_SZ(1024ULL)) 81dd6d33e8SMichael Haro #define TERA_2_SZ (TERA_SZ(1024ULL)) 82dd6d33e8SMichael Haro #define PETA_2_SZ (PETA_SZ(1024ULL)) 83dd6d33e8SMichael Haro 84dd6d33e8SMichael Haro #define KILO_SI_SZ (KILO_SZ(1000ULL)) 85dd6d33e8SMichael Haro #define MEGA_SI_SZ (MEGA_SZ(1000ULL)) 86dd6d33e8SMichael Haro #define GIGA_SI_SZ (GIGA_SZ(1000ULL)) 87dd6d33e8SMichael Haro #define TERA_SI_SZ (TERA_SZ(1000ULL)) 88dd6d33e8SMichael Haro #define PETA_SI_SZ (PETA_SZ(1000ULL)) 89dd6d33e8SMichael Haro 9062edbd31SIan Dowse /* Maximum widths of various fields. */ 9162edbd31SIan Dowse struct maxwidths { 920bd9f151SIan Dowse int mntfrom; 930bd9f151SIan Dowse int total; 940bd9f151SIan Dowse int used; 950bd9f151SIan Dowse int avail; 960bd9f151SIan Dowse int iused; 970bd9f151SIan Dowse int ifree; 9862edbd31SIan Dowse }; 9962edbd31SIan Dowse 100b7dbd3e9SMark Murray static uintmax_t vals_si [] = { 101b7dbd3e9SMark Murray 1, 102b7dbd3e9SMark Murray KILO_SI_SZ, 103b7dbd3e9SMark Murray MEGA_SI_SZ, 104b7dbd3e9SMark Murray GIGA_SI_SZ, 105b7dbd3e9SMark Murray TERA_SI_SZ, 106b7dbd3e9SMark Murray PETA_SI_SZ 107b7dbd3e9SMark Murray }; 108b7dbd3e9SMark Murray static uintmax_t vals_base2[] = { 109b7dbd3e9SMark Murray 1, 110b7dbd3e9SMark Murray KILO_2_SZ, 111b7dbd3e9SMark Murray MEGA_2_SZ, 112b7dbd3e9SMark Murray GIGA_2_SZ, 113b7dbd3e9SMark Murray TERA_2_SZ, 114b7dbd3e9SMark Murray PETA_2_SZ 115b7dbd3e9SMark Murray }; 116b7dbd3e9SMark Murray static uintmax_t *valp; 117dd6d33e8SMichael Haro 118dd6d33e8SMichael Haro typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t; 119dd6d33e8SMichael Haro 120b7dbd3e9SMark Murray static unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA }; 121dd6d33e8SMichael Haro 122b7dbd3e9SMark Murray static char *getmntpt(const char *); 1230bd9f151SIan Dowse static int int64width(int64_t); 124532aff98SPoul-Henning Kamp static char *makenetvfslist(void); 125fde81c7dSKirk McKusick static void prthuman(const struct statfs *, int64_t); 126532aff98SPoul-Henning Kamp static void prthumanval(double); 127532aff98SPoul-Henning Kamp static void prtstat(struct statfs *, struct maxwidths *); 128be2c4e54SDavid E. O'Brien static size_t regetmntinfo(struct statfs **, long, const char **); 129532aff98SPoul-Henning Kamp static unit_t unit_adjust(double *); 130b7dbd3e9SMark Murray static void update_maxwidths(struct maxwidths *, const struct statfs *); 131532aff98SPoul-Henning Kamp static void usage(void); 1324b88c807SRodney W. Grimes 1330bd9f151SIan Dowse static __inline int 1340bd9f151SIan Dowse imax(int a, int b) 13562edbd31SIan Dowse { 136b7dbd3e9SMark Murray return (a > b ? a : b); 13762edbd31SIan Dowse } 13862edbd31SIan Dowse 139532aff98SPoul-Henning Kamp static int aflag = 0, hflag, iflag, nflag; 140532aff98SPoul-Henning Kamp static struct ufs_args mdev; 141532aff98SPoul-Henning Kamp 1424b88c807SRodney W. Grimes int 143f9bcb0beSWarner Losh main(int argc, char *argv[]) 1444b88c807SRodney W. Grimes { 1454b88c807SRodney W. Grimes struct stat stbuf; 1464b88c807SRodney W. Grimes struct statfs statfsbuf, *mntbuf; 14762edbd31SIan Dowse struct maxwidths maxwidths; 148a95a13bbSKris Kennaway const char *fstype; 149532aff98SPoul-Henning Kamp char *mntpath, *mntpt; 150532aff98SPoul-Henning Kamp const char **vfslist; 151be2c4e54SDavid E. O'Brien size_t i, mntsize; 152be2c4e54SDavid E. O'Brien int ch, rv; 153f3895a82SKris Kennaway 154f3895a82SKris Kennaway fstype = "ufs"; 1554b88c807SRodney W. Grimes 156a78192e3SBruce Evans vfslist = NULL; 157a25695c3SJim Pirzyk while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1) 1584b88c807SRodney W. Grimes switch (ch) { 1595b42dac8SJulian Elischer case 'a': 1605b42dac8SJulian Elischer aflag = 1; 1615b42dac8SJulian Elischer break; 162dd6d33e8SMichael Haro case 'b': 163dd6d33e8SMichael Haro /* FALLTHROUGH */ 164dd6d33e8SMichael Haro case 'P': 165dd6d33e8SMichael Haro putenv("BLOCKSIZE=512"); 166dd6d33e8SMichael Haro hflag = 0; 167dd6d33e8SMichael Haro break; 16893a3fa19SJohn W. De Boskey case 'g': 16993a3fa19SJohn W. De Boskey putenv("BLOCKSIZE=1g"); 17093a3fa19SJohn W. De Boskey hflag = 0; 17193a3fa19SJohn W. De Boskey break; 172dd6d33e8SMichael Haro case 'H': 173dd6d33e8SMichael Haro hflag = UNITS_SI; 174dd6d33e8SMichael Haro valp = vals_si; 175dd6d33e8SMichael Haro break; 176dd6d33e8SMichael Haro case 'h': 177dd6d33e8SMichael Haro hflag = UNITS_2; 178dd6d33e8SMichael Haro valp = vals_base2; 179dd6d33e8SMichael Haro break; 1804b88c807SRodney W. Grimes case 'i': 1814b88c807SRodney W. Grimes iflag = 1; 1824b88c807SRodney W. Grimes break; 1837f0eabfdSGarrett Wollman case 'k': 1849d4081eeSDavid Greenman putenv("BLOCKSIZE=1k"); 185dd6d33e8SMichael Haro hflag = 0; 186dd6d33e8SMichael Haro break; 187a25695c3SJim Pirzyk case 'l': 188a25695c3SJim Pirzyk if (vfslist != NULL) 189a25695c3SJim Pirzyk errx(1, "-l and -t are mutually exclusive."); 190a25695c3SJim Pirzyk vfslist = makevfslist(makenetvfslist()); 191a25695c3SJim Pirzyk break; 192dd6d33e8SMichael Haro case 'm': 193dd6d33e8SMichael Haro putenv("BLOCKSIZE=1m"); 194dd6d33e8SMichael Haro hflag = 0; 1957f0eabfdSGarrett Wollman break; 1964b88c807SRodney W. Grimes case 'n': 1974b88c807SRodney W. Grimes nflag = 1; 1984b88c807SRodney W. Grimes break; 1994b88c807SRodney W. Grimes case 't': 200a78192e3SBruce Evans if (vfslist != NULL) 2017b3a12a8SPhilippe Charnier errx(1, "only one -t option may be specified"); 202f3895a82SKris Kennaway fstype = optarg; 203a78192e3SBruce Evans vfslist = makevfslist(optarg); 2044b88c807SRodney W. Grimes break; 2054b88c807SRodney W. Grimes case '?': 2064b88c807SRodney W. Grimes default: 2074b88c807SRodney W. Grimes usage(); 2084b88c807SRodney W. Grimes } 2094b88c807SRodney W. Grimes argc -= optind; 2104b88c807SRodney W. Grimes argv += optind; 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 21362edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 21462edbd31SIan Dowse for (i = 0; i < mntsize; i++) 21562edbd31SIan Dowse update_maxwidths(&maxwidths, &mntbuf[i]); 2164b88c807SRodney W. Grimes 217b8904f2aSJoerg Wunsch rv = 0; 2184b88c807SRodney W. Grimes if (!*argv) { 219a78192e3SBruce Evans mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 22062edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 22162edbd31SIan Dowse for (i = 0; i < mntsize; i++) 22262edbd31SIan Dowse update_maxwidths(&maxwidths, &mntbuf[i]); 2235b42dac8SJulian Elischer for (i = 0; i < mntsize; i++) { 2245b42dac8SJulian Elischer if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 22562edbd31SIan Dowse prtstat(&mntbuf[i], &maxwidths); 2265b42dac8SJulian Elischer } 227b8904f2aSJoerg Wunsch exit(rv); 2284b88c807SRodney W. Grimes } 2294b88c807SRodney W. Grimes 2304b88c807SRodney W. Grimes for (; *argv; argv++) { 2314b88c807SRodney W. Grimes if (stat(*argv, &stbuf) < 0) { 2324b88c807SRodney W. Grimes if ((mntpt = getmntpt(*argv)) == 0) { 2334b88c807SRodney W. Grimes warn("%s", *argv); 234b8904f2aSJoerg Wunsch rv = 1; 2354b88c807SRodney W. Grimes continue; 2364b88c807SRodney W. Grimes } 237f3895a82SKris Kennaway } else if (S_ISCHR(stbuf.st_mode)) { 238f3895a82SKris Kennaway if ((mntpt = getmntpt(*argv)) == 0) { 239f3895a82SKris Kennaway mdev.fspec = *argv; 240f3895a82SKris Kennaway mntpath = strdup("/tmp/df.XXXXXX"); 241f3895a82SKris Kennaway if (mntpath == NULL) { 242f3895a82SKris Kennaway warn("strdup failed"); 243f3895a82SKris Kennaway rv = 1; 2444b88c807SRodney W. Grimes continue; 245f3895a82SKris Kennaway } 246f3895a82SKris Kennaway mntpt = mkdtemp(mntpath); 247f3895a82SKris Kennaway if (mntpt == NULL) { 248f3895a82SKris Kennaway warn("mkdtemp(\"%s\") failed", mntpath); 249f3895a82SKris Kennaway rv = 1; 250f3895a82SKris Kennaway free(mntpath); 251f3895a82SKris Kennaway continue; 252f3895a82SKris Kennaway } 253f3895a82SKris Kennaway if (mount(fstype, mntpt, MNT_RDONLY, 254f3895a82SKris Kennaway &mdev) != 0) { 255532aff98SPoul-Henning Kamp warn("%s", *argv); 256532aff98SPoul-Henning Kamp rv = 1; 257f3895a82SKris Kennaway (void)rmdir(mntpt); 258f3895a82SKris Kennaway free(mntpath); 259f3895a82SKris Kennaway continue; 260f3895a82SKris Kennaway } else if (statfs(mntpt, &statfsbuf) == 0) { 261f3895a82SKris Kennaway statfsbuf.f_mntonname[0] = '\0'; 26262edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 263f3895a82SKris Kennaway } else { 264f3895a82SKris Kennaway warn("%s", *argv); 265f3895a82SKris Kennaway rv = 1; 266f3895a82SKris Kennaway } 267f3895a82SKris Kennaway (void)unmount(mntpt, 0); 268f3895a82SKris Kennaway (void)rmdir(mntpt); 269f3895a82SKris Kennaway free(mntpath); 270f3895a82SKris Kennaway continue; 271f3895a82SKris Kennaway } 2724b88c807SRodney W. Grimes } else 2734b88c807SRodney W. Grimes mntpt = *argv; 2740e7d023fSBruce Evans 2754b88c807SRodney W. Grimes /* 2764b88c807SRodney W. Grimes * Statfs does not take a `wait' flag, so we cannot 2774b88c807SRodney W. Grimes * implement nflag here. 2784b88c807SRodney W. Grimes */ 2794b88c807SRodney W. Grimes if (statfs(mntpt, &statfsbuf) < 0) { 2804b88c807SRodney W. Grimes warn("%s", mntpt); 281b8904f2aSJoerg Wunsch rv = 1; 2824b88c807SRodney W. Grimes continue; 2834b88c807SRodney W. Grimes } 2840e7d023fSBruce Evans 2850e7d023fSBruce Evans /* 2860e7d023fSBruce Evans * Check to make sure the arguments we've been given are 2870e7d023fSBruce Evans * satisfied. Return an error if we have been asked to 2880e7d023fSBruce Evans * list a mount point that does not match the other args 2890e7d023fSBruce Evans * we've been given (-l, -t, etc.). 290c22acefbSJordan K. Hubbard */ 291c22acefbSJordan K. Hubbard if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 2920e7d023fSBruce Evans rv = 1; 293c22acefbSJordan K. Hubbard continue; 294c22acefbSJordan K. Hubbard } 2950e7d023fSBruce Evans 29662edbd31SIan Dowse if (argc == 1) { 29762edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 29862edbd31SIan Dowse update_maxwidths(&maxwidths, &statfsbuf); 29962edbd31SIan Dowse } 30062edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 3014b88c807SRodney W. Grimes } 302b8904f2aSJoerg Wunsch return (rv); 3034b88c807SRodney W. Grimes } 3044b88c807SRodney W. Grimes 305532aff98SPoul-Henning Kamp static char * 306b7dbd3e9SMark Murray getmntpt(const char *name) 3074b88c807SRodney W. Grimes { 308be2c4e54SDavid E. O'Brien size_t mntsize, i; 3094b88c807SRodney W. Grimes struct statfs *mntbuf; 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 3124b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 3134b88c807SRodney W. Grimes if (!strcmp(mntbuf[i].f_mntfromname, name)) 3144b88c807SRodney W. Grimes return (mntbuf[i].f_mntonname); 3154b88c807SRodney W. Grimes } 3164b88c807SRodney W. Grimes return (0); 3174b88c807SRodney W. Grimes } 3184b88c807SRodney W. Grimes 3194b88c807SRodney W. Grimes /* 3204b88c807SRodney W. Grimes * Make a pass over the file system info in ``mntbuf'' filtering out 321a78192e3SBruce Evans * file system types not in vfslist and possibly re-stating to get 3224b88c807SRodney W. Grimes * current (not cached) info. Returns the new count of valid statfs bufs. 3234b88c807SRodney W. Grimes */ 324be2c4e54SDavid E. O'Brien static size_t 325532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 3264b88c807SRodney W. Grimes { 3274b88c807SRodney W. Grimes int i, j; 3284b88c807SRodney W. Grimes struct statfs *mntbuf; 3294b88c807SRodney W. Grimes 330a78192e3SBruce Evans if (vfslist == NULL) 3314b88c807SRodney W. Grimes return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes mntbuf = *mntbufp; 334a78192e3SBruce Evans for (j = 0, i = 0; i < mntsize; i++) { 335a78192e3SBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 336a78192e3SBruce Evans continue; 3374b88c807SRodney W. Grimes if (!nflag) 3384b88c807SRodney W. Grimes (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 3394b88c807SRodney W. Grimes else if (i != j) 3404b88c807SRodney W. Grimes mntbuf[j] = mntbuf[i]; 3414b88c807SRodney W. Grimes j++; 3424b88c807SRodney W. Grimes } 3434b88c807SRodney W. Grimes return (j); 3444b88c807SRodney W. Grimes } 3454b88c807SRodney W. Grimes 3464b88c807SRodney W. Grimes /* 347dd6d33e8SMichael Haro * Output in "human-readable" format. Uses 3 digits max and puts 348dd6d33e8SMichael Haro * unit suffixes at the end. Makes output compact and easy to read, 349dd6d33e8SMichael Haro * especially on huge disks. 350dd6d33e8SMichael Haro * 351dd6d33e8SMichael Haro */ 352532aff98SPoul-Henning Kamp static unit_t 353f9bcb0beSWarner Losh unit_adjust(double *val) 354dd6d33e8SMichael Haro { 355dd6d33e8SMichael Haro double abval; 356dd6d33e8SMichael Haro unit_t unit; 357b7dbd3e9SMark Murray int unit_sz; 358dd6d33e8SMichael Haro 359dd6d33e8SMichael Haro abval = fabs(*val); 360dd6d33e8SMichael Haro 361dd6d33e8SMichael Haro unit_sz = abval ? ilogb(abval) / 10 : 0; 362dd6d33e8SMichael Haro 363b7dbd3e9SMark Murray if (unit_sz >= (int)UNIT_MAX) { 364dd6d33e8SMichael Haro unit = NONE; 365dd6d33e8SMichael Haro } else { 366dd6d33e8SMichael Haro unit = unitp[unit_sz]; 367dd6d33e8SMichael Haro *val /= (double)valp[unit_sz]; 368dd6d33e8SMichael Haro } 369dd6d33e8SMichael Haro 370dd6d33e8SMichael Haro return (unit); 371dd6d33e8SMichael Haro } 372dd6d33e8SMichael Haro 373532aff98SPoul-Henning Kamp static void 374fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used) 375dd6d33e8SMichael Haro { 376dd6d33e8SMichael Haro 377dd6d33e8SMichael Haro prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize); 378dd6d33e8SMichael Haro prthumanval((double)used * (double)sfsp->f_bsize); 379dd6d33e8SMichael Haro prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize); 380dd6d33e8SMichael Haro } 381dd6d33e8SMichael Haro 382532aff98SPoul-Henning Kamp static void 383f9bcb0beSWarner Losh prthumanval(double bytes) 384dd6d33e8SMichael Haro { 385dd6d33e8SMichael Haro 386dd6d33e8SMichael Haro unit_t unit; 387dd6d33e8SMichael Haro unit = unit_adjust(&bytes); 388dd6d33e8SMichael Haro 389dd6d33e8SMichael Haro if (bytes == 0) 390dd6d33e8SMichael Haro (void)printf(" 0B"); 391dd6d33e8SMichael Haro else if (bytes > 10) 392dd6d33e8SMichael Haro (void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]); 393dd6d33e8SMichael Haro else 394dd6d33e8SMichael Haro (void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]); 395dd6d33e8SMichael Haro } 396dd6d33e8SMichael Haro 397dd6d33e8SMichael Haro /* 3984b88c807SRodney W. Grimes * Convert statfs returned file system size into BLOCKSIZE units. 3994b88c807SRodney W. Grimes * Attempts to avoid overflow for large file systems. 4004b88c807SRodney W. Grimes */ 4014b88c807SRodney W. Grimes #define fsbtoblk(num, fsbs, bs) \ 4024b88c807SRodney W. Grimes (((fsbs) != 0 && (fsbs) < (bs)) ? \ 4034b88c807SRodney W. Grimes (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs))) 4044b88c807SRodney W. Grimes 4054b88c807SRodney W. Grimes /* 4064b88c807SRodney W. Grimes * Print out status about a file system. 4074b88c807SRodney W. Grimes */ 408532aff98SPoul-Henning Kamp static void 40962edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp) 4104b88c807SRodney W. Grimes { 411fde81c7dSKirk McKusick static u_long blocksize; 412b7dbd3e9SMark Murray static int headerlen, timesthrough = 0; 413a95a13bbSKris Kennaway static const char *header; 414fde81c7dSKirk McKusick int64_t used, availblks, inodes; 4154b88c807SRodney W. Grimes 4164b88c807SRodney W. Grimes if (++timesthrough == 1) { 4170bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 418dd6d33e8SMichael Haro if (hflag) { 419dd6d33e8SMichael Haro header = " Size"; 4200bd9f151SIan Dowse mwp->total = mwp->used = mwp->avail = 4210bd9f151SIan Dowse (int)strlen(header); 422dd6d33e8SMichael Haro } else { 423dd6d33e8SMichael Haro header = getbsize(&headerlen, &blocksize); 4240bd9f151SIan Dowse mwp->total = imax(mwp->total, headerlen); 425dd6d33e8SMichael Haro } 4260bd9f151SIan Dowse mwp->used = imax(mwp->used, (int)strlen("Used")); 4270bd9f151SIan Dowse mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 42862edbd31SIan Dowse 429f694b8adSMark Murray (void)printf("%-*s %-*s %*s %*s Capacity", 4300bd9f151SIan Dowse mwp->mntfrom, "Filesystem", mwp->total, header, 4310bd9f151SIan Dowse mwp->used, "Used", mwp->avail, "Avail"); 43262edbd31SIan Dowse if (iflag) { 4330bd9f151SIan Dowse mwp->iused = imax(mwp->iused, (int)strlen(" iused")); 4340bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, (int)strlen("ifree")); 435f694b8adSMark Murray (void)printf(" %*s %*s %%iused", 4360bd9f151SIan Dowse mwp->iused - 2, "iused", mwp->ifree, "ifree"); 43762edbd31SIan Dowse } 4384b88c807SRodney W. Grimes (void)printf(" Mounted on\n"); 4394b88c807SRodney W. Grimes } 4400bd9f151SIan Dowse (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname); 4414b88c807SRodney W. Grimes used = sfsp->f_blocks - sfsp->f_bfree; 4424b88c807SRodney W. Grimes availblks = sfsp->f_bavail + used; 443dd6d33e8SMichael Haro if (hflag) { 444dd6d33e8SMichael Haro prthuman(sfsp, used); 445dd6d33e8SMichael Haro } else { 446ea64ad77SKris Kennaway (void)printf(" %*jd %*jd %*jd", 4470bd9f151SIan Dowse mwp->total, (intmax_t)fsbtoblk(sfsp->f_blocks, 4480bd9f151SIan Dowse sfsp->f_bsize, blocksize), 4490bd9f151SIan Dowse mwp->used, (intmax_t)fsbtoblk(used, sfsp->f_bsize, 4500bd9f151SIan Dowse blocksize), 4510bd9f151SIan Dowse mwp->avail, (intmax_t)fsbtoblk(sfsp->f_bavail, 4520bd9f151SIan Dowse sfsp->f_bsize, blocksize)); 453dd6d33e8SMichael Haro } 4544b88c807SRodney W. Grimes (void)printf(" %5.0f%%", 4554b88c807SRodney W. Grimes availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 4564b88c807SRodney W. Grimes if (iflag) { 4574b88c807SRodney W. Grimes inodes = sfsp->f_files; 4584b88c807SRodney W. Grimes used = inodes - sfsp->f_ffree; 4590bd9f151SIan Dowse (void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used, 4600bd9f151SIan Dowse mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 : 4610bd9f151SIan Dowse (double)used / (double)inodes * 100.0); 4624b88c807SRodney W. Grimes } else 4634b88c807SRodney W. Grimes (void)printf(" "); 4644b88c807SRodney W. Grimes (void)printf(" %s\n", sfsp->f_mntonname); 4654b88c807SRodney W. Grimes } 4664b88c807SRodney W. Grimes 4674b88c807SRodney W. Grimes /* 46862edbd31SIan Dowse * Update the maximum field-width information in `mwp' based on 46962edbd31SIan Dowse * the file system specified by `sfsp'. 47062edbd31SIan Dowse */ 471532aff98SPoul-Henning Kamp static void 472b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 47362edbd31SIan Dowse { 474fde81c7dSKirk McKusick static u_long blocksize = 0; 475dc474219SMike Barcroft int dummy; 47662edbd31SIan Dowse 47762edbd31SIan Dowse if (blocksize == 0) 47862edbd31SIan Dowse getbsize(&dummy, &blocksize); 47962edbd31SIan Dowse 4800bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 4810bd9f151SIan Dowse mwp->total = imax(mwp->total, int64width( 482fde81c7dSKirk McKusick fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 4830bd9f151SIan Dowse mwp->used = imax(mwp->used, 4840bd9f151SIan Dowse int64width(fsbtoblk((int64_t)sfsp->f_blocks - 485fde81c7dSKirk McKusick (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 4860bd9f151SIan Dowse mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 48762edbd31SIan Dowse sfsp->f_bsize, blocksize))); 4880bd9f151SIan Dowse mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 48962edbd31SIan Dowse sfsp->f_ffree)); 4900bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 49162edbd31SIan Dowse } 49262edbd31SIan Dowse 4930bd9f151SIan Dowse /* Return the width in characters of the specified value. */ 4940bd9f151SIan Dowse static int 495fde81c7dSKirk McKusick int64width(int64_t val) 49662edbd31SIan Dowse { 4970bd9f151SIan Dowse int len; 49862edbd31SIan Dowse 49962edbd31SIan Dowse len = 0; 50062edbd31SIan Dowse /* Negative or zero values require one extra digit. */ 50162edbd31SIan Dowse if (val <= 0) { 50262edbd31SIan Dowse val = -val; 50362edbd31SIan Dowse len++; 50462edbd31SIan Dowse } 50562edbd31SIan Dowse while (val > 0) { 50662edbd31SIan Dowse len++; 50762edbd31SIan Dowse val /= 10; 50862edbd31SIan Dowse } 50962edbd31SIan Dowse 51062edbd31SIan Dowse return (len); 51162edbd31SIan Dowse } 51262edbd31SIan Dowse 513532aff98SPoul-Henning Kamp static void 514f9bcb0beSWarner Losh usage(void) 5154b88c807SRodney W. Grimes { 516dd6d33e8SMichael Haro 517a78192e3SBruce Evans (void)fprintf(stderr, 518a25695c3SJim Pirzyk "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n"); 519dd6d33e8SMichael Haro exit(EX_USAGE); 5204b88c807SRodney W. Grimes } 521a25695c3SJim Pirzyk 522532aff98SPoul-Henning Kamp static char * 523f9bcb0beSWarner Losh makenetvfslist(void) 524a25695c3SJim Pirzyk { 525a25695c3SJim Pirzyk char *str, *strptr, **listptr; 526b7dbd3e9SMark Murray struct xvfsconf *xvfsp, *keep_xvfsp; 5275965373eSMaxime Henrion size_t buflen; 5285965373eSMaxime Henrion int cnt, i, maxvfsconf; 529a25695c3SJim Pirzyk 5305965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 5315965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 532a25695c3SJim Pirzyk return (NULL); 533a25695c3SJim Pirzyk } 5345965373eSMaxime Henrion xvfsp = malloc(buflen); 5355965373eSMaxime Henrion if (xvfsp == NULL) { 5365965373eSMaxime Henrion warnx("malloc failed"); 5375965373eSMaxime Henrion return (NULL); 5385965373eSMaxime Henrion } 539b7dbd3e9SMark Murray keep_xvfsp = xvfsp; 5405965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 5415965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 542b7dbd3e9SMark Murray free(keep_xvfsp); 5435965373eSMaxime Henrion return (NULL); 5445965373eSMaxime Henrion } 5455965373eSMaxime Henrion maxvfsconf = buflen / sizeof(struct xvfsconf); 546a25695c3SJim Pirzyk 547a25695c3SJim Pirzyk if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 548a25695c3SJim Pirzyk warnx("malloc failed"); 549b7dbd3e9SMark Murray free(keep_xvfsp); 550a25695c3SJim Pirzyk return (NULL); 551a25695c3SJim Pirzyk } 552a25695c3SJim Pirzyk 5535965373eSMaxime Henrion for (cnt = 0, i = 0; i < maxvfsconf; i++) { 5545965373eSMaxime Henrion if (xvfsp->vfc_flags & VFCF_NETWORK) { 5555965373eSMaxime Henrion listptr[cnt++] = strdup(xvfsp->vfc_name); 556a067aeceSGarrett Wollman if (listptr[cnt-1] == NULL) { 557a25695c3SJim Pirzyk warnx("malloc failed"); 558b7dbd3e9SMark Murray free(listptr); 559b7dbd3e9SMark Murray free(keep_xvfsp); 560a25695c3SJim Pirzyk return (NULL); 561a25695c3SJim Pirzyk } 562a25695c3SJim Pirzyk } 5635965373eSMaxime Henrion xvfsp++; 5645965373eSMaxime Henrion } 565a25695c3SJim Pirzyk 566cf5b29e1SRuslan Ermilov if (cnt == 0 || 567cf5b29e1SRuslan Ermilov (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 568cf5b29e1SRuslan Ermilov if (cnt > 0) 569a25695c3SJim Pirzyk warnx("malloc failed"); 570a25695c3SJim Pirzyk free(listptr); 571b7dbd3e9SMark Murray free(keep_xvfsp); 572a25695c3SJim Pirzyk return (NULL); 573a25695c3SJim Pirzyk } 574a25695c3SJim Pirzyk 575a25695c3SJim Pirzyk *str = 'n'; *(str + 1) = 'o'; 576a25695c3SJim Pirzyk for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 577a25695c3SJim Pirzyk strncpy(strptr, listptr[i], 32); 578a25695c3SJim Pirzyk strptr += strlen(listptr[i]); 579a25695c3SJim Pirzyk *strptr = ','; 580a25695c3SJim Pirzyk free(listptr[i]); 581a25695c3SJim Pirzyk } 58216fc3635SMark Murray *(--strptr) = '\0'; 583a25695c3SJim Pirzyk 584b7dbd3e9SMark Murray free(keep_xvfsp); 585a25695c3SJim Pirzyk free(listptr); 586a25695c3SJim Pirzyk return (str); 587a25695c3SJim Pirzyk } 588