19ddb49cbSWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 194b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 204b88c807SRodney W. Grimes * without specific prior written permission. 214b88c807SRodney W. Grimes * 224b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 234b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 244b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 254b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 264b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 274b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 284b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 294b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 304b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 314b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 324b88c807SRodney W. Grimes * SUCH DAMAGE. 334b88c807SRodney W. Grimes */ 344b88c807SRodney W. Grimes 3509a80d48SDavid E. O'Brien #if 0 364b88c807SRodney W. Grimes #ifndef lint 3716cc192aSSteve Price static const char copyright[] = 384b88c807SRodney W. Grimes "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 394b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 404b88c807SRodney W. Grimes #endif /* not lint */ 414b88c807SRodney W. Grimes 424b88c807SRodney W. Grimes #ifndef lint 4316cc192aSSteve Price static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; 444b88c807SRodney W. Grimes #endif /* not lint */ 4509a80d48SDavid E. O'Brien #endif 465eb43ac2SDavid E. O'Brien #include <sys/cdefs.h> 475eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 484b88c807SRodney W. Grimes 494b88c807SRodney W. Grimes #include <sys/param.h> 504b88c807SRodney W. Grimes #include <sys/stat.h> 514b88c807SRodney W. Grimes #include <sys/mount.h> 52a25695c3SJim Pirzyk #include <sys/sysctl.h> 53a78192e3SBruce Evans #include <ufs/ufs/ufsmount.h> 544b88c807SRodney W. Grimes #include <err.h> 557d3940bbSPawel Jakub Dawidek #include <libutil.h> 560bd9f151SIan Dowse #include <stdint.h> 574b88c807SRodney W. Grimes #include <stdio.h> 584b88c807SRodney W. Grimes #include <stdlib.h> 594b88c807SRodney W. Grimes #include <string.h> 60dd6d33e8SMichael Haro #include <sysexits.h> 614b88c807SRodney W. Grimes #include <unistd.h> 624b88c807SRodney W. Grimes 63532aff98SPoul-Henning Kamp #include "extern.h" 64532aff98SPoul-Henning Kamp 65dd6d33e8SMichael Haro #define UNITS_SI 1 66dd6d33e8SMichael Haro #define UNITS_2 2 67dd6d33e8SMichael Haro 6862edbd31SIan Dowse /* Maximum widths of various fields. */ 6962edbd31SIan Dowse struct maxwidths { 700bd9f151SIan Dowse int mntfrom; 71b56ca465SPawel Jakub Dawidek int fstype; 720bd9f151SIan Dowse int total; 730bd9f151SIan Dowse int used; 740bd9f151SIan Dowse int avail; 750bd9f151SIan Dowse int iused; 760bd9f151SIan Dowse int ifree; 7762edbd31SIan Dowse }; 7862edbd31SIan Dowse 79c6f13844SDavid E. O'Brien static void addstat(struct statfs *, struct statfs *); 80b7dbd3e9SMark Murray static char *getmntpt(const char *); 810bd9f151SIan Dowse static int int64width(int64_t); 82532aff98SPoul-Henning Kamp static char *makenetvfslist(void); 83fde81c7dSKirk McKusick static void prthuman(const struct statfs *, int64_t); 847d3940bbSPawel Jakub Dawidek static void prthumanval(int64_t); 85841fe8e8SDavid Schultz static intmax_t fsbtoblk(int64_t, uint64_t, u_long); 86532aff98SPoul-Henning Kamp static void prtstat(struct statfs *, struct maxwidths *); 87be2c4e54SDavid E. O'Brien static size_t regetmntinfo(struct statfs **, long, const char **); 88b7dbd3e9SMark Murray static void update_maxwidths(struct maxwidths *, const struct statfs *); 89532aff98SPoul-Henning Kamp static void usage(void); 904b88c807SRodney W. Grimes 910bd9f151SIan Dowse static __inline int 920bd9f151SIan Dowse imax(int a, int b) 9362edbd31SIan Dowse { 94b7dbd3e9SMark Murray return (a > b ? a : b); 9562edbd31SIan Dowse } 9662edbd31SIan Dowse 97b56ca465SPawel Jakub Dawidek static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag; 98532aff98SPoul-Henning Kamp static struct ufs_args mdev; 99532aff98SPoul-Henning Kamp 1004b88c807SRodney W. Grimes int 101f9bcb0beSWarner Losh main(int argc, char *argv[]) 1024b88c807SRodney W. Grimes { 1034b88c807SRodney W. Grimes struct stat stbuf; 104c6f13844SDavid E. O'Brien struct statfs statfsbuf, totalbuf; 10562edbd31SIan Dowse struct maxwidths maxwidths; 106c6f13844SDavid E. O'Brien struct statfs *mntbuf; 107a95a13bbSKris Kennaway const char *fstype; 108532aff98SPoul-Henning Kamp char *mntpath, *mntpt; 109532aff98SPoul-Henning Kamp const char **vfslist; 110*7b0514faSDag-Erling Smørgrav int i, mntsize; 111be2c4e54SDavid E. O'Brien int ch, rv; 112f3895a82SKris Kennaway 113f3895a82SKris Kennaway fstype = "ufs"; 1144b88c807SRodney W. Grimes 115076419d2SDavid E. O'Brien memset(&totalbuf, 0, sizeof(totalbuf)); 116076419d2SDavid E. O'Brien totalbuf.f_bsize = DEV_BSIZE; 117d7c881e8SWarner Losh strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN); 118a78192e3SBruce Evans vfslist = NULL; 119b56ca465SPawel Jakub Dawidek while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T")) != -1) 1204b88c807SRodney W. Grimes switch (ch) { 1215b42dac8SJulian Elischer case 'a': 1225b42dac8SJulian Elischer aflag = 1; 1235b42dac8SJulian Elischer break; 124dd6d33e8SMichael Haro case 'b': 125dd6d33e8SMichael Haro /* FALLTHROUGH */ 126dd6d33e8SMichael Haro case 'P': 127df464e43SChristian S.J. Peron /* 1286bccea7cSRebecca Cran * POSIX specifically discusses the behavior of 129df464e43SChristian S.J. Peron * both -k and -P. It states that the blocksize should 130df464e43SChristian S.J. Peron * be set to 1024. Thus, if this occurs, simply break 131df464e43SChristian S.J. Peron * rather than clobbering the old blocksize. 132df464e43SChristian S.J. Peron */ 133df464e43SChristian S.J. Peron if (kflag) 134df464e43SChristian S.J. Peron break; 1352966d28cSSean Farley setenv("BLOCKSIZE", "512", 1); 136dd6d33e8SMichael Haro hflag = 0; 137dd6d33e8SMichael Haro break; 138c6f13844SDavid E. O'Brien case 'c': 139c6f13844SDavid E. O'Brien cflag = 1; 140c6f13844SDavid E. O'Brien break; 14193a3fa19SJohn W. De Boskey case 'g': 1422966d28cSSean Farley setenv("BLOCKSIZE", "1g", 1); 14393a3fa19SJohn W. De Boskey hflag = 0; 14493a3fa19SJohn W. De Boskey break; 145dd6d33e8SMichael Haro case 'H': 146dd6d33e8SMichael Haro hflag = UNITS_SI; 147dd6d33e8SMichael Haro break; 148dd6d33e8SMichael Haro case 'h': 149dd6d33e8SMichael Haro hflag = UNITS_2; 150dd6d33e8SMichael Haro break; 1514b88c807SRodney W. Grimes case 'i': 1524b88c807SRodney W. Grimes iflag = 1; 1534b88c807SRodney W. Grimes break; 1547f0eabfdSGarrett Wollman case 'k': 155df464e43SChristian S.J. Peron kflag++; 1562966d28cSSean Farley setenv("BLOCKSIZE", "1024", 1); 157dd6d33e8SMichael Haro hflag = 0; 158dd6d33e8SMichael Haro break; 159a25695c3SJim Pirzyk case 'l': 160a25695c3SJim Pirzyk if (vfslist != NULL) 161a25695c3SJim Pirzyk errx(1, "-l and -t are mutually exclusive."); 162a25695c3SJim Pirzyk vfslist = makevfslist(makenetvfslist()); 16327f82335SWill Andrews lflag = 1; 164a25695c3SJim Pirzyk break; 165dd6d33e8SMichael Haro case 'm': 1662966d28cSSean Farley setenv("BLOCKSIZE", "1m", 1); 167dd6d33e8SMichael Haro hflag = 0; 1687f0eabfdSGarrett Wollman break; 1694b88c807SRodney W. Grimes case 'n': 1704b88c807SRodney W. Grimes nflag = 1; 1714b88c807SRodney W. Grimes break; 1724b88c807SRodney W. Grimes case 't': 17327f82335SWill Andrews if (lflag) 17427f82335SWill Andrews errx(1, "-l and -t are mutually exclusive."); 175a78192e3SBruce Evans if (vfslist != NULL) 1767b3a12a8SPhilippe Charnier errx(1, "only one -t option may be specified"); 177f3895a82SKris Kennaway fstype = optarg; 178a78192e3SBruce Evans vfslist = makevfslist(optarg); 1794b88c807SRodney W. Grimes break; 180b56ca465SPawel Jakub Dawidek case 'T': 181b56ca465SPawel Jakub Dawidek Tflag = 1; 182b56ca465SPawel Jakub Dawidek break; 1834b88c807SRodney W. Grimes case '?': 1844b88c807SRodney W. Grimes default: 1854b88c807SRodney W. Grimes usage(); 1864b88c807SRodney W. Grimes } 1874b88c807SRodney W. Grimes argc -= optind; 1884b88c807SRodney W. Grimes argv += optind; 1894b88c807SRodney W. Grimes 190b8904f2aSJoerg Wunsch rv = 0; 1914b88c807SRodney W. Grimes if (!*argv) { 192*7b0514faSDag-Erling Smørgrav /* everything (modulo -t) */ 193*7b0514faSDag-Erling Smørgrav mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 194a78192e3SBruce Evans mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 195*7b0514faSDag-Erling Smørgrav } else { 196*7b0514faSDag-Erling Smørgrav /* just the filesystems specified on the command line */ 197*7b0514faSDag-Erling Smørgrav mntbuf = malloc(argc * sizeof(*mntbuf)); 198*7b0514faSDag-Erling Smørgrav if (mntbuf == 0) 199*7b0514faSDag-Erling Smørgrav err(1, "malloc()"); 200*7b0514faSDag-Erling Smørgrav mntsize = 0; 201*7b0514faSDag-Erling Smørgrav /* continued in for loop below */ 2024b88c807SRodney W. Grimes } 2034b88c807SRodney W. Grimes 204*7b0514faSDag-Erling Smørgrav /* iterate through specified filesystems */ 2054b88c807SRodney W. Grimes for (; *argv; argv++) { 2064b88c807SRodney W. Grimes if (stat(*argv, &stbuf) < 0) { 2074b88c807SRodney W. Grimes if ((mntpt = getmntpt(*argv)) == 0) { 2084b88c807SRodney W. Grimes warn("%s", *argv); 209b8904f2aSJoerg Wunsch rv = 1; 2104b88c807SRodney W. Grimes continue; 2114b88c807SRodney W. Grimes } 212f3895a82SKris Kennaway } else if (S_ISCHR(stbuf.st_mode)) { 213f3895a82SKris Kennaway if ((mntpt = getmntpt(*argv)) == 0) { 214f3895a82SKris Kennaway mdev.fspec = *argv; 215f3895a82SKris Kennaway mntpath = strdup("/tmp/df.XXXXXX"); 216f3895a82SKris Kennaway if (mntpath == NULL) { 217f3895a82SKris Kennaway warn("strdup failed"); 218f3895a82SKris Kennaway rv = 1; 2194b88c807SRodney W. Grimes continue; 220f3895a82SKris Kennaway } 221f3895a82SKris Kennaway mntpt = mkdtemp(mntpath); 222f3895a82SKris Kennaway if (mntpt == NULL) { 223f3895a82SKris Kennaway warn("mkdtemp(\"%s\") failed", mntpath); 224f3895a82SKris Kennaway rv = 1; 225f3895a82SKris Kennaway free(mntpath); 226f3895a82SKris Kennaway continue; 227f3895a82SKris Kennaway } 228f3895a82SKris Kennaway if (mount(fstype, mntpt, MNT_RDONLY, 229f3895a82SKris Kennaway &mdev) != 0) { 230532aff98SPoul-Henning Kamp warn("%s", *argv); 231532aff98SPoul-Henning Kamp rv = 1; 232f3895a82SKris Kennaway (void)rmdir(mntpt); 233f3895a82SKris Kennaway free(mntpath); 234f3895a82SKris Kennaway continue; 235f3895a82SKris Kennaway } else if (statfs(mntpt, &statfsbuf) == 0) { 236f3895a82SKris Kennaway statfsbuf.f_mntonname[0] = '\0'; 23762edbd31SIan Dowse prtstat(&statfsbuf, &maxwidths); 238076419d2SDavid E. O'Brien if (cflag) 239076419d2SDavid E. O'Brien addstat(&totalbuf, &statfsbuf); 240f3895a82SKris Kennaway } else { 241f3895a82SKris Kennaway warn("%s", *argv); 242f3895a82SKris Kennaway rv = 1; 243f3895a82SKris Kennaway } 244f3895a82SKris Kennaway (void)unmount(mntpt, 0); 245f3895a82SKris Kennaway (void)rmdir(mntpt); 246f3895a82SKris Kennaway free(mntpath); 247f3895a82SKris Kennaway continue; 248f3895a82SKris Kennaway } 2494b88c807SRodney W. Grimes } else 2504b88c807SRodney W. Grimes mntpt = *argv; 2510e7d023fSBruce Evans 2524b88c807SRodney W. Grimes /* 2534b88c807SRodney W. Grimes * Statfs does not take a `wait' flag, so we cannot 2544b88c807SRodney W. Grimes * implement nflag here. 2554b88c807SRodney W. Grimes */ 2564b88c807SRodney W. Grimes if (statfs(mntpt, &statfsbuf) < 0) { 2574b88c807SRodney W. Grimes warn("%s", mntpt); 258b8904f2aSJoerg Wunsch rv = 1; 2594b88c807SRodney W. Grimes continue; 2604b88c807SRodney W. Grimes } 2610e7d023fSBruce Evans 2620e7d023fSBruce Evans /* 2630e7d023fSBruce Evans * Check to make sure the arguments we've been given are 2640e7d023fSBruce Evans * satisfied. Return an error if we have been asked to 2650e7d023fSBruce Evans * list a mount point that does not match the other args 2660e7d023fSBruce Evans * we've been given (-l, -t, etc.). 267c22acefbSJordan K. Hubbard */ 268c22acefbSJordan K. Hubbard if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 2690e7d023fSBruce Evans rv = 1; 270c22acefbSJordan K. Hubbard continue; 271c22acefbSJordan K. Hubbard } 2720e7d023fSBruce Evans 273*7b0514faSDag-Erling Smørgrav /* the user asked for it, so ignore the ignore flag */ 274*7b0514faSDag-Erling Smørgrav statfsbuf.f_flags &= ~MNT_IGNORE; 275*7b0514faSDag-Erling Smørgrav 276*7b0514faSDag-Erling Smørgrav /* add to list */ 277*7b0514faSDag-Erling Smørgrav mntbuf[mntsize++] = statfsbuf; 278*7b0514faSDag-Erling Smørgrav } 279*7b0514faSDag-Erling Smørgrav 28062edbd31SIan Dowse bzero(&maxwidths, sizeof(maxwidths)); 281*7b0514faSDag-Erling Smørgrav for (i = 0; i < mntsize; i++) { 282*7b0514faSDag-Erling Smørgrav if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) { 283*7b0514faSDag-Erling Smørgrav update_maxwidths(&maxwidths, &mntbuf[i]); 284076419d2SDavid E. O'Brien if (cflag) 285*7b0514faSDag-Erling Smørgrav addstat(&totalbuf, &mntbuf[i]); 2864b88c807SRodney W. Grimes } 287*7b0514faSDag-Erling Smørgrav } 288*7b0514faSDag-Erling Smørgrav for (i = 0; i < mntsize; i++) 289*7b0514faSDag-Erling Smørgrav if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 290*7b0514faSDag-Erling Smørgrav prtstat(&mntbuf[i], &maxwidths); 291076419d2SDavid E. O'Brien if (cflag) 292076419d2SDavid E. O'Brien prtstat(&totalbuf, &maxwidths); 293b8904f2aSJoerg Wunsch return (rv); 2944b88c807SRodney W. Grimes } 2954b88c807SRodney W. Grimes 296532aff98SPoul-Henning Kamp static char * 297b7dbd3e9SMark Murray getmntpt(const char *name) 2984b88c807SRodney W. Grimes { 299be2c4e54SDavid E. O'Brien size_t mntsize, i; 3004b88c807SRodney W. Grimes struct statfs *mntbuf; 3014b88c807SRodney W. Grimes 3024b88c807SRodney W. Grimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 3034b88c807SRodney W. Grimes for (i = 0; i < mntsize; i++) { 3044b88c807SRodney W. Grimes if (!strcmp(mntbuf[i].f_mntfromname, name)) 3054b88c807SRodney W. Grimes return (mntbuf[i].f_mntonname); 3064b88c807SRodney W. Grimes } 3074b88c807SRodney W. Grimes return (0); 3084b88c807SRodney W. Grimes } 3094b88c807SRodney W. Grimes 3104b88c807SRodney W. Grimes /* 3114b88c807SRodney W. Grimes * Make a pass over the file system info in ``mntbuf'' filtering out 312a78192e3SBruce Evans * file system types not in vfslist and possibly re-stating to get 3134b88c807SRodney W. Grimes * current (not cached) info. Returns the new count of valid statfs bufs. 3144b88c807SRodney W. Grimes */ 315be2c4e54SDavid E. O'Brien static size_t 316532aff98SPoul-Henning Kamp regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 3174b88c807SRodney W. Grimes { 318239c9e60SChristian S.J. Peron int error, i, j; 3194b88c807SRodney W. Grimes struct statfs *mntbuf; 3204b88c807SRodney W. Grimes 321a78192e3SBruce Evans if (vfslist == NULL) 3224b88c807SRodney W. Grimes return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 3234b88c807SRodney W. Grimes 3244b88c807SRodney W. Grimes mntbuf = *mntbufp; 325a78192e3SBruce Evans for (j = 0, i = 0; i < mntsize; i++) { 326a78192e3SBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 327a78192e3SBruce Evans continue; 328239c9e60SChristian S.J. Peron /* 329239c9e60SChristian S.J. Peron * XXX statfs(2) can fail for various reasons. It may be 330239c9e60SChristian S.J. Peron * possible that the user does not have access to the 331239c9e60SChristian S.J. Peron * pathname, if this happens, we will fall back on 332239c9e60SChristian S.J. Peron * "stale" filesystem statistics. 333239c9e60SChristian S.J. Peron */ 334239c9e60SChristian S.J. Peron error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]); 335239c9e60SChristian S.J. Peron if (nflag || error < 0) 336239c9e60SChristian S.J. Peron if (i != j) { 337239c9e60SChristian S.J. Peron if (error < 0) 338239c9e60SChristian S.J. Peron warnx("%s stats possibly stale", 339239c9e60SChristian S.J. Peron mntbuf[i].f_mntonname); 3404b88c807SRodney W. Grimes mntbuf[j] = mntbuf[i]; 341239c9e60SChristian S.J. Peron } 3424b88c807SRodney W. Grimes j++; 3434b88c807SRodney W. Grimes } 3444b88c807SRodney W. Grimes return (j); 3454b88c807SRodney W. Grimes } 3464b88c807SRodney W. Grimes 347532aff98SPoul-Henning Kamp static void 348fde81c7dSKirk McKusick prthuman(const struct statfs *sfsp, int64_t used) 349dd6d33e8SMichael Haro { 350dd6d33e8SMichael Haro 3517d3940bbSPawel Jakub Dawidek prthumanval(sfsp->f_blocks * sfsp->f_bsize); 3527d3940bbSPawel Jakub Dawidek prthumanval(used * sfsp->f_bsize); 3537d3940bbSPawel Jakub Dawidek prthumanval(sfsp->f_bavail * sfsp->f_bsize); 354dd6d33e8SMichael Haro } 355dd6d33e8SMichael Haro 356532aff98SPoul-Henning Kamp static void 3577d3940bbSPawel Jakub Dawidek prthumanval(int64_t bytes) 358dd6d33e8SMichael Haro { 3597d3940bbSPawel Jakub Dawidek char buf[6]; 3607d3940bbSPawel Jakub Dawidek int flags; 361dd6d33e8SMichael Haro 3627d3940bbSPawel Jakub Dawidek flags = HN_B | HN_NOSPACE | HN_DECIMAL; 3637d3940bbSPawel Jakub Dawidek if (hflag == UNITS_SI) 3647d3940bbSPawel Jakub Dawidek flags |= HN_DIVISOR_1000; 365dd6d33e8SMichael Haro 3667d3940bbSPawel Jakub Dawidek humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 3677d3940bbSPawel Jakub Dawidek bytes, "", HN_AUTOSCALE, flags); 3687d3940bbSPawel Jakub Dawidek 3697d3940bbSPawel Jakub Dawidek (void)printf(" %6s", buf); 370dd6d33e8SMichael Haro } 371dd6d33e8SMichael Haro 372dd6d33e8SMichael Haro /* 373b93ce3b7SSimon L. B. Nielsen * Print an inode count in "human-readable" format. 374b93ce3b7SSimon L. B. Nielsen */ 375b93ce3b7SSimon L. B. Nielsen static void 376b93ce3b7SSimon L. B. Nielsen prthumanvalinode(int64_t bytes) 377b93ce3b7SSimon L. B. Nielsen { 378b93ce3b7SSimon L. B. Nielsen char buf[6]; 379b93ce3b7SSimon L. B. Nielsen int flags; 380b93ce3b7SSimon L. B. Nielsen 381b93ce3b7SSimon L. B. Nielsen flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000; 382b93ce3b7SSimon L. B. Nielsen 383b93ce3b7SSimon L. B. Nielsen humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 384b93ce3b7SSimon L. B. Nielsen bytes, "", HN_AUTOSCALE, flags); 385b93ce3b7SSimon L. B. Nielsen 386b93ce3b7SSimon L. B. Nielsen (void)printf(" %5s", buf); 387b93ce3b7SSimon L. B. Nielsen } 388b93ce3b7SSimon L. B. Nielsen 389b93ce3b7SSimon L. B. Nielsen /* 3904b88c807SRodney W. Grimes * Convert statfs returned file system size into BLOCKSIZE units. 3914b88c807SRodney W. Grimes * Attempts to avoid overflow for large file systems. 3924b88c807SRodney W. Grimes */ 393841fe8e8SDavid Schultz static intmax_t 394841fe8e8SDavid Schultz fsbtoblk(int64_t num, uint64_t fsbs, u_long bs) 395841fe8e8SDavid Schultz { 396841fe8e8SDavid Schultz 397841fe8e8SDavid Schultz if (fsbs != 0 && fsbs < bs) 398841fe8e8SDavid Schultz return (num / (intmax_t)(bs / fsbs)); 399841fe8e8SDavid Schultz else 400841fe8e8SDavid Schultz return (num * (intmax_t)(fsbs / bs)); 401841fe8e8SDavid Schultz } 4024b88c807SRodney W. Grimes 4034b88c807SRodney W. Grimes /* 4044b88c807SRodney W. Grimes * Print out status about a file system. 4054b88c807SRodney W. Grimes */ 406532aff98SPoul-Henning Kamp static void 40762edbd31SIan Dowse prtstat(struct statfs *sfsp, struct maxwidths *mwp) 4084b88c807SRodney W. Grimes { 4092897dce8SAlexander Kabaev static long blocksize; 410b7dbd3e9SMark Murray static int headerlen, timesthrough = 0; 411a95a13bbSKris Kennaway static const char *header; 412fde81c7dSKirk McKusick int64_t used, availblks, inodes; 4134b88c807SRodney W. Grimes 4144b88c807SRodney W. Grimes if (++timesthrough == 1) { 4150bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 416b56ca465SPawel Jakub Dawidek mwp->fstype = imax(mwp->fstype, (int)strlen("Type")); 417dd6d33e8SMichael Haro if (hflag) { 418dd6d33e8SMichael Haro header = " Size"; 4190bd9f151SIan Dowse mwp->total = mwp->used = mwp->avail = 4200bd9f151SIan Dowse (int)strlen(header); 421dd6d33e8SMichael Haro } else { 422dd6d33e8SMichael Haro header = getbsize(&headerlen, &blocksize); 4230bd9f151SIan Dowse mwp->total = imax(mwp->total, headerlen); 424dd6d33e8SMichael Haro } 4250bd9f151SIan Dowse mwp->used = imax(mwp->used, (int)strlen("Used")); 4260bd9f151SIan Dowse mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 42762edbd31SIan Dowse 428b56ca465SPawel Jakub Dawidek (void)printf("%-*s", mwp->mntfrom, "Filesystem"); 429b56ca465SPawel Jakub Dawidek if (Tflag) 430b56ca465SPawel Jakub Dawidek (void)printf(" %-*s", mwp->fstype, "Type"); 431b56ca465SPawel Jakub Dawidek (void)printf(" %-*s %*s %*s Capacity", mwp->total, header, 4320bd9f151SIan Dowse mwp->used, "Used", mwp->avail, "Avail"); 43362edbd31SIan Dowse if (iflag) { 434b93ce3b7SSimon L. B. Nielsen mwp->iused = imax(hflag ? 0 : mwp->iused, 435b93ce3b7SSimon L. B. Nielsen (int)strlen(" iused")); 436b93ce3b7SSimon L. B. Nielsen mwp->ifree = imax(hflag ? 0 : mwp->ifree, 437b93ce3b7SSimon L. B. Nielsen (int)strlen("ifree")); 438f694b8adSMark Murray (void)printf(" %*s %*s %%iused", 4390bd9f151SIan Dowse mwp->iused - 2, "iused", mwp->ifree, "ifree"); 44062edbd31SIan Dowse } 4414b88c807SRodney W. Grimes (void)printf(" Mounted on\n"); 4424b88c807SRodney W. Grimes } 4430bd9f151SIan Dowse (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname); 444b56ca465SPawel Jakub Dawidek if (Tflag) 445b56ca465SPawel Jakub Dawidek (void)printf(" %-*s", mwp->fstype, sfsp->f_fstypename); 4464b88c807SRodney W. Grimes used = sfsp->f_blocks - sfsp->f_bfree; 4474b88c807SRodney W. Grimes availblks = sfsp->f_bavail + used; 448dd6d33e8SMichael Haro if (hflag) { 449dd6d33e8SMichael Haro prthuman(sfsp, used); 450dd6d33e8SMichael Haro } else { 451ea64ad77SKris Kennaway (void)printf(" %*jd %*jd %*jd", 452841fe8e8SDavid Schultz mwp->total, fsbtoblk(sfsp->f_blocks, 4530bd9f151SIan Dowse sfsp->f_bsize, blocksize), 454841fe8e8SDavid Schultz mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize), 455841fe8e8SDavid Schultz mwp->avail, fsbtoblk(sfsp->f_bavail, 4560bd9f151SIan Dowse sfsp->f_bsize, blocksize)); 457dd6d33e8SMichael Haro } 4584b88c807SRodney W. Grimes (void)printf(" %5.0f%%", 4594b88c807SRodney W. Grimes availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 4604b88c807SRodney W. Grimes if (iflag) { 4614b88c807SRodney W. Grimes inodes = sfsp->f_files; 4624b88c807SRodney W. Grimes used = inodes - sfsp->f_ffree; 463b93ce3b7SSimon L. B. Nielsen if (hflag) { 464b93ce3b7SSimon L. B. Nielsen (void)printf(" "); 465b93ce3b7SSimon L. B. Nielsen prthumanvalinode(used); 466b93ce3b7SSimon L. B. Nielsen prthumanvalinode(sfsp->f_ffree); 467b93ce3b7SSimon L. B. Nielsen } else { 468b93ce3b7SSimon L. B. Nielsen (void)printf(" %*jd %*jd", mwp->iused, (intmax_t)used, 469b93ce3b7SSimon L. B. Nielsen mwp->ifree, (intmax_t)sfsp->f_ffree); 470b93ce3b7SSimon L. B. Nielsen } 471b93ce3b7SSimon L. B. Nielsen (void)printf(" %4.0f%% ", inodes == 0 ? 100.0 : 4720bd9f151SIan Dowse (double)used / (double)inodes * 100.0); 473cca108e6SDavid E. O'Brien } else 474cca108e6SDavid E. O'Brien (void)printf(" "); 475c6f13844SDavid E. O'Brien if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0) 476076419d2SDavid E. O'Brien (void)printf(" %s", sfsp->f_mntonname); 477076419d2SDavid E. O'Brien (void)printf("\n"); 478076419d2SDavid E. O'Brien } 479076419d2SDavid E. O'Brien 480431586a8SXin LI static void 481076419d2SDavid E. O'Brien addstat(struct statfs *totalfsp, struct statfs *statfsp) 482076419d2SDavid E. O'Brien { 483c6f13844SDavid E. O'Brien uint64_t bsize; 484076419d2SDavid E. O'Brien 485c6f13844SDavid E. O'Brien bsize = statfsp->f_bsize / totalfsp->f_bsize; 486076419d2SDavid E. O'Brien totalfsp->f_blocks += statfsp->f_blocks * bsize; 487076419d2SDavid E. O'Brien totalfsp->f_bfree += statfsp->f_bfree * bsize; 488076419d2SDavid E. O'Brien totalfsp->f_bavail += statfsp->f_bavail * bsize; 489076419d2SDavid E. O'Brien totalfsp->f_files += statfsp->f_files; 490076419d2SDavid E. O'Brien totalfsp->f_ffree += statfsp->f_ffree; 4914b88c807SRodney W. Grimes } 4924b88c807SRodney W. Grimes 4934b88c807SRodney W. Grimes /* 49462edbd31SIan Dowse * Update the maximum field-width information in `mwp' based on 49562edbd31SIan Dowse * the file system specified by `sfsp'. 49662edbd31SIan Dowse */ 497532aff98SPoul-Henning Kamp static void 498b7dbd3e9SMark Murray update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 49962edbd31SIan Dowse { 5002897dce8SAlexander Kabaev static long blocksize = 0; 501dc474219SMike Barcroft int dummy; 50262edbd31SIan Dowse 50362edbd31SIan Dowse if (blocksize == 0) 50462edbd31SIan Dowse getbsize(&dummy, &blocksize); 50562edbd31SIan Dowse 5060bd9f151SIan Dowse mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 507b56ca465SPawel Jakub Dawidek mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename)); 5080bd9f151SIan Dowse mwp->total = imax(mwp->total, int64width( 509fde81c7dSKirk McKusick fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 5100bd9f151SIan Dowse mwp->used = imax(mwp->used, 5110bd9f151SIan Dowse int64width(fsbtoblk((int64_t)sfsp->f_blocks - 512fde81c7dSKirk McKusick (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 5130bd9f151SIan Dowse mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 51462edbd31SIan Dowse sfsp->f_bsize, blocksize))); 5150bd9f151SIan Dowse mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 51662edbd31SIan Dowse sfsp->f_ffree)); 5170bd9f151SIan Dowse mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 51862edbd31SIan Dowse } 51962edbd31SIan Dowse 5200bd9f151SIan Dowse /* Return the width in characters of the specified value. */ 5210bd9f151SIan Dowse static int 522fde81c7dSKirk McKusick int64width(int64_t val) 52362edbd31SIan Dowse { 5240bd9f151SIan Dowse int len; 52562edbd31SIan Dowse 52662edbd31SIan Dowse len = 0; 52762edbd31SIan Dowse /* Negative or zero values require one extra digit. */ 52862edbd31SIan Dowse if (val <= 0) { 52962edbd31SIan Dowse val = -val; 53062edbd31SIan Dowse len++; 53162edbd31SIan Dowse } 53262edbd31SIan Dowse while (val > 0) { 53362edbd31SIan Dowse len++; 53462edbd31SIan Dowse val /= 10; 53562edbd31SIan Dowse } 53662edbd31SIan Dowse 53762edbd31SIan Dowse return (len); 53862edbd31SIan Dowse } 53962edbd31SIan Dowse 540532aff98SPoul-Henning Kamp static void 541f9bcb0beSWarner Losh usage(void) 5424b88c807SRodney W. Grimes { 543dd6d33e8SMichael Haro 544a78192e3SBruce Evans (void)fprintf(stderr, 545b56ca465SPawel Jakub Dawidek "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [file | filesystem ...]\n"); 546dd6d33e8SMichael Haro exit(EX_USAGE); 5474b88c807SRodney W. Grimes } 548a25695c3SJim Pirzyk 549532aff98SPoul-Henning Kamp static char * 550f9bcb0beSWarner Losh makenetvfslist(void) 551a25695c3SJim Pirzyk { 552a25695c3SJim Pirzyk char *str, *strptr, **listptr; 553b7dbd3e9SMark Murray struct xvfsconf *xvfsp, *keep_xvfsp; 5545965373eSMaxime Henrion size_t buflen; 5555965373eSMaxime Henrion int cnt, i, maxvfsconf; 556a25695c3SJim Pirzyk 5575965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 5585965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 559a25695c3SJim Pirzyk return (NULL); 560a25695c3SJim Pirzyk } 5615965373eSMaxime Henrion xvfsp = malloc(buflen); 5625965373eSMaxime Henrion if (xvfsp == NULL) { 5635965373eSMaxime Henrion warnx("malloc failed"); 5645965373eSMaxime Henrion return (NULL); 5655965373eSMaxime Henrion } 566b7dbd3e9SMark Murray keep_xvfsp = xvfsp; 5675965373eSMaxime Henrion if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 5685965373eSMaxime Henrion warn("sysctl(vfs.conflist)"); 569b7dbd3e9SMark Murray free(keep_xvfsp); 5705965373eSMaxime Henrion return (NULL); 5715965373eSMaxime Henrion } 5725965373eSMaxime Henrion maxvfsconf = buflen / sizeof(struct xvfsconf); 573a25695c3SJim Pirzyk 574a25695c3SJim Pirzyk if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 575a25695c3SJim Pirzyk warnx("malloc failed"); 576b7dbd3e9SMark Murray free(keep_xvfsp); 577a25695c3SJim Pirzyk return (NULL); 578a25695c3SJim Pirzyk } 579a25695c3SJim Pirzyk 5805965373eSMaxime Henrion for (cnt = 0, i = 0; i < maxvfsconf; i++) { 5815965373eSMaxime Henrion if (xvfsp->vfc_flags & VFCF_NETWORK) { 5825965373eSMaxime Henrion listptr[cnt++] = strdup(xvfsp->vfc_name); 583a067aeceSGarrett Wollman if (listptr[cnt-1] == NULL) { 584a25695c3SJim Pirzyk warnx("malloc failed"); 585b7dbd3e9SMark Murray free(listptr); 586b7dbd3e9SMark Murray free(keep_xvfsp); 587a25695c3SJim Pirzyk return (NULL); 588a25695c3SJim Pirzyk } 589a25695c3SJim Pirzyk } 5905965373eSMaxime Henrion xvfsp++; 5915965373eSMaxime Henrion } 592a25695c3SJim Pirzyk 593cf5b29e1SRuslan Ermilov if (cnt == 0 || 594cf5b29e1SRuslan Ermilov (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 595cf5b29e1SRuslan Ermilov if (cnt > 0) 596a25695c3SJim Pirzyk warnx("malloc failed"); 597a25695c3SJim Pirzyk free(listptr); 598b7dbd3e9SMark Murray free(keep_xvfsp); 599a25695c3SJim Pirzyk return (NULL); 600a25695c3SJim Pirzyk } 601a25695c3SJim Pirzyk 602a25695c3SJim Pirzyk *str = 'n'; *(str + 1) = 'o'; 603a25695c3SJim Pirzyk for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 604d7c881e8SWarner Losh strlcpy(strptr, listptr[i], 32); 605a25695c3SJim Pirzyk strptr += strlen(listptr[i]); 606a25695c3SJim Pirzyk *strptr = ','; 607a25695c3SJim Pirzyk free(listptr[i]); 608a25695c3SJim Pirzyk } 60916fc3635SMark Murray *(--strptr) = '\0'; 610a25695c3SJim Pirzyk 611b7dbd3e9SMark Murray free(keep_xvfsp); 612a25695c3SJim Pirzyk free(listptr); 613a25695c3SJim Pirzyk return (str); 614a25695c3SJim Pirzyk } 615