2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
53 #include <sys/mount.h>
54 #include <sys/sysctl.h>
55 #ifdef MOUNT_CHAR_DEVS
56 #include <ufs/ufs/ufsmount.h>
62 #ifdef MOUNT_CHAR_DEVS
78 /* Maximum widths of various fields. */
89 static void addstat(struct statfs *, struct statfs *);
90 static char *getmntpt(const char *);
91 static int int64width(int64_t);
92 static char *makenetvfslist(void);
93 static void prthuman(const struct statfs *, int64_t);
94 static void prthumanval(const char *, int64_t);
95 static intmax_t fsbtoblk(int64_t, uint64_t, u_long);
96 static void prtstat(struct statfs *, struct maxwidths *);
97 static size_t regetmntinfo(struct statfs **, long, const char **);
98 static void update_maxwidths(struct maxwidths *, const struct statfs *);
99 static void usage(void);
104 return (a > b ? a : b);
107 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
108 static int thousands;
109 #ifdef MOUNT_CHAR_DEVS
110 static struct ufs_args mdev;
113 static const struct option long_options[] =
115 { "si", no_argument, NULL, 'H' },
116 { NULL, no_argument, NULL, 0 },
120 main(int argc, char *argv[])
123 struct statfs statfsbuf, totalbuf;
124 struct maxwidths maxwidths;
125 struct statfs *mntbuf;
126 #ifdef MOUNT_CHAR_DEVS
127 struct iovec *iov = NULL;
130 #ifdef MOUNT_CHAR_DEVS
132 char errmsg[255] = {0};
135 const char **vfslist;
138 #ifdef MOUNT_CHAR_DEVS
143 (void)setlocale(LC_ALL, "");
144 memset(&maxwidths, 0, sizeof(maxwidths));
145 memset(&totalbuf, 0, sizeof(totalbuf));
146 totalbuf.f_bsize = DEV_BSIZE;
147 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
150 argc = xo_parse_args(argc, argv);
154 while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options,
164 * POSIX specifically discusses the behavior of
165 * both -k and -P. It states that the blocksize should
166 * be set to 1024. Thus, if this occurs, simply break
167 * rather than clobbering the old blocksize.
171 setenv("BLOCKSIZE", "512", 1);
178 setenv("BLOCKSIZE", "1g", 1);
192 setenv("BLOCKSIZE", "1024", 1);
196 /* Ignore duplicate -l */
200 xo_errx(1, "-l and -t are mutually exclusive.");
201 vfslist = makevfslist(makenetvfslist());
205 setenv("BLOCKSIZE", "1m", 1);
213 xo_errx(1, "-l and -t are mutually exclusive.");
215 xo_errx(1, "only one -t option may be specified");
217 vfslist = makevfslist(optarg);
234 /* everything (modulo -t) */
235 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
236 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
238 /* just the filesystems specified on the command line */
239 mntbuf = malloc(argc * sizeof(*mntbuf));
241 xo_err(1, "malloc()");
243 /* continued in for loop below */
246 xo_open_container("storage-system-information");
247 xo_open_list("filesystem");
249 /* iterate through specified filesystems */
250 for (; *argv; argv++) {
251 if (stat(*argv, &stbuf) < 0) {
252 if ((mntpt = getmntpt(*argv)) == NULL) {
253 xo_warn("%s", *argv);
257 } else if (S_ISCHR(stbuf.st_mode)) {
258 if ((mntpt = getmntpt(*argv)) == NULL) {
259 #ifdef MOUNT_CHAR_DEVS
261 "df on unmounted devices is deprecated");
263 mntpath = strdup("/tmp/df.XXXXXX");
264 if (mntpath == NULL) {
265 xo_warn("strdup failed");
269 mntpt = mkdtemp(mntpath);
271 xo_warn("mkdtemp(\"%s\") failed", mntpath);
277 free_iovec(&iov, &iovlen);
278 build_iovec_argf(&iov, &iovlen, "fstype", "%s",
280 build_iovec_argf(&iov, &iovlen, "fspath", "%s",
282 build_iovec_argf(&iov, &iovlen, "from", "%s",
284 build_iovec(&iov, &iovlen, "errmsg", errmsg,
286 if (nmount(iov, iovlen,
287 MNT_RDONLY|MNT_NOEXEC) < 0) {
289 xo_warn("%s: %s", *argv,
292 xo_warn("%s", *argv);
297 } else if (statfs(mntpt, &statfsbuf) == 0) {
298 statfsbuf.f_mntonname[0] = '\0';
299 prtstat(&statfsbuf, &maxwidths);
301 addstat(&totalbuf, &statfsbuf);
303 xo_warn("%s", *argv);
306 (void)unmount(mntpt, 0);
311 xo_warnx("%s: not mounted", *argv);
320 * Statfs does not take a `wait' flag, so we cannot
321 * implement nflag here.
323 if (statfs(mntpt, &statfsbuf) < 0) {
324 xo_warn("%s", mntpt);
330 * Check to make sure the arguments we've been given are
331 * satisfied. Return an error if we have been asked to
332 * list a mount point that does not match the other args
333 * we've been given (-l, -t, etc.).
335 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
340 /* the user asked for it, so ignore the ignore flag */
341 statfsbuf.f_flags &= ~MNT_IGNORE;
344 mntbuf[mntsize++] = statfsbuf;
347 memset(&maxwidths, 0, sizeof(maxwidths));
348 for (i = 0; i < mntsize; i++) {
349 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
350 update_maxwidths(&maxwidths, &mntbuf[i]);
352 addstat(&totalbuf, &mntbuf[i]);
355 for (i = 0; i < mntsize; i++)
356 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
357 prtstat(&mntbuf[i], &maxwidths);
359 xo_close_list("filesystem");
362 prtstat(&totalbuf, &maxwidths);
364 xo_close_container("storage-system-information");
370 getmntpt(const char *name)
373 struct statfs *mntbuf;
375 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
376 for (i = 0; i < mntsize; i++) {
377 if (!strcmp(mntbuf[i].f_mntfromname, name))
378 return (mntbuf[i].f_mntonname);
384 * Make a pass over the file system info in ``mntbuf'' filtering out
385 * file system types not in vfslist and possibly re-stating to get
386 * current (not cached) info. Returns the new count of valid statfs bufs.
389 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
392 struct statfs *mntbuf;
395 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
398 for (j = 0, i = 0; i < mntsize; i++) {
399 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
402 * XXX statfs(2) can fail for various reasons. It may be
403 * possible that the user does not have access to the
404 * pathname, if this happens, we will fall back on
405 * "stale" filesystem statistics.
407 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
408 if (nflag || error < 0)
411 xo_warnx("%s stats possibly stale",
412 mntbuf[i].f_mntonname);
413 mntbuf[j] = mntbuf[i];
421 prthuman(const struct statfs *sfsp, int64_t used)
424 prthumanval(" {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize);
425 prthumanval(" {:used/%6s}", used * sfsp->f_bsize);
426 prthumanval(" {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize);
430 prthumanval(const char *fmt, int64_t bytes)
435 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
436 if (hflag == UNITS_SI)
437 flags |= HN_DIVISOR_1000;
439 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
440 bytes, "", HN_AUTOSCALE, flags);
442 xo_attr("value", "%lld", (long long) bytes);
447 * Print an inode count in "human-readable" format.
450 prthumanvalinode(const char *fmt, int64_t bytes)
455 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
457 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
458 bytes, "", HN_AUTOSCALE, flags);
460 xo_attr("value", "%lld", (long long) bytes);
465 * Convert statfs returned file system size into BLOCKSIZE units.
468 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
470 return (num * (intmax_t) fsbs / (int64_t) bs);
474 * Print out status about a file system.
477 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
479 static long blocksize;
480 static int headerlen, timesthrough = 0;
481 static const char *header;
482 int64_t used, availblks, inodes;
485 if (++timesthrough == 1) {
486 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
487 mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
488 if (thousands) { /* make space for commas */
489 mwp->total += (mwp->total - 1) / 3;
490 mwp->used += (mwp->used - 1) / 3;
491 mwp->avail += (mwp->avail - 1) / 3;
492 mwp->iused += (mwp->iused - 1) / 3;
493 mwp->ifree += (mwp->ifree - 1) / 3;
497 mwp->total = mwp->used = mwp->avail =
500 header = getbsize(&headerlen, &blocksize);
501 mwp->total = imax(mwp->total, headerlen);
503 mwp->used = imax(mwp->used, (int)strlen("Used"));
504 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
506 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem");
508 xo_emit(" {T:/%-*s}", mwp->fstype, "Type");
509 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} {T:Capacity}",
511 mwp->used, "Used", mwp->avail, "Avail");
513 mwp->iused = imax(hflag ? 0 : mwp->iused,
514 (int)strlen(" iused"));
515 mwp->ifree = imax(hflag ? 0 : mwp->ifree,
516 (int)strlen("ifree"));
517 xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}",
518 mwp->iused - 2, "iused", mwp->ifree, "ifree");
520 xo_emit(" {T:Mounted on}\n");
523 xo_open_instance("filesystem");
524 /* Check for 0 block size. Can this happen? */
525 if (sfsp->f_bsize == 0) {
526 xo_warnx ("File system %s does not have a block size, assuming 512.",
530 xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname);
532 xo_emit(" {:type/%-*s}", mwp->fstype, sfsp->f_fstypename);
533 used = sfsp->f_blocks - sfsp->f_bfree;
534 availblks = sfsp->f_bavail + used;
536 prthuman(sfsp, used);
539 format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} "
540 "{t:available-blocks/%*j'd}";
542 format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} "
543 "{t:available-blocks/%*jd}";
545 mwp->total, fsbtoblk(sfsp->f_blocks,
546 sfsp->f_bsize, blocksize),
547 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
548 mwp->avail, fsbtoblk(sfsp->f_bavail,
549 sfsp->f_bsize, blocksize));
551 xo_emit(" {:used-percent/%5.0f}{U:%%}",
552 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
554 inodes = sfsp->f_files;
555 used = inodes - sfsp->f_ffree;
558 prthumanvalinode(" {:inodes-used/%5s}", used);
559 prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree);
562 format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}";
564 format = " {:inodes-used/%*jd} {:inodes-free/%*jd}";
565 xo_emit(format, mwp->iused, (intmax_t)used,
566 mwp->ifree, (intmax_t)sfsp->f_ffree);
568 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
569 inodes == 0 ? 100.0 :
570 (double)used / (double)inodes * 100.0);
573 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
574 xo_emit(" {:mounted-on}", sfsp->f_mntonname);
576 xo_close_instance("filesystem");
580 addstat(struct statfs *totalfsp, struct statfs *statfsp)
584 bsize = statfsp->f_bsize / totalfsp->f_bsize;
585 totalfsp->f_blocks += statfsp->f_blocks * bsize;
586 totalfsp->f_bfree += statfsp->f_bfree * bsize;
587 totalfsp->f_bavail += statfsp->f_bavail * bsize;
588 totalfsp->f_files += statfsp->f_files;
589 totalfsp->f_ffree += statfsp->f_ffree;
593 * Update the maximum field-width information in `mwp' based on
594 * the file system specified by `sfsp'.
597 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
599 static long blocksize = 0;
603 getbsize(&dummy, &blocksize);
605 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
606 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
607 mwp->total = imax(mwp->total, int64width(
608 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
609 mwp->used = imax(mwp->used,
610 int64width(fsbtoblk((int64_t)sfsp->f_blocks -
611 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
612 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
613 sfsp->f_bsize, blocksize)));
614 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
616 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
619 /* Return the width in characters of the specified value. */
621 int64width(int64_t val)
626 /* Negative or zero values require one extra digit. */
644 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
645 " [file | filesystem ...]\n");
652 char *str, *strptr, **listptr;
653 struct xvfsconf *xvfsp, *keep_xvfsp;
655 int cnt, i, maxvfsconf;
657 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
658 xo_warn("sysctl(vfs.conflist)");
661 xvfsp = malloc(buflen);
663 xo_warnx("malloc failed");
667 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
668 xo_warn("sysctl(vfs.conflist)");
672 maxvfsconf = buflen / sizeof(struct xvfsconf);
674 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
675 xo_warnx("malloc failed");
680 for (cnt = 0, i = 0; i < maxvfsconf; i++) {
681 if (xvfsp->vfc_flags & VFCF_NETWORK) {
682 listptr[cnt++] = strdup(xvfsp->vfc_name);
683 if (listptr[cnt-1] == NULL) {
684 xo_warnx("malloc failed");
694 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
696 xo_warnx("malloc failed");
702 *str = 'n'; *(str + 1) = 'o';
703 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
704 strlcpy(strptr, listptr[i], 32);
705 strptr += strlen(listptr[i]);