2 * Copyright (c) 1980, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
51 #include <sys/mount.h>
52 #include <sys/sysctl.h>
53 #ifdef MOUNT_CHAR_DEVS
54 #include <ufs/ufs/ufsmount.h>
60 #ifdef MOUNT_CHAR_DEVS
76 /* Maximum widths of various fields. */
87 static void addstat(struct statfs *, struct statfs *);
88 static char *getmntpt(const char *);
89 static int int64width(int64_t);
90 static char *makenetvfslist(void);
91 static void prthuman(const struct statfs *, int64_t);
92 static void prthumanval(const char *, int64_t);
93 static intmax_t fsbtoblk(int64_t, uint64_t, u_long);
94 static void prtstat(struct statfs *, struct maxwidths *);
95 static size_t regetmntinfo(struct statfs **, long, const char **);
96 static void update_maxwidths(struct maxwidths *, const struct statfs *);
97 static void usage(void);
102 return (a > b ? a : b);
105 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
106 static int thousands;
107 #ifdef MOUNT_CHAR_DEVS
108 static struct ufs_args mdev;
111 static const struct option long_options[] =
113 { "si", no_argument, NULL, 'H' },
114 { NULL, no_argument, NULL, 0 },
118 main(int argc, char *argv[])
121 struct statfs statfsbuf, totalbuf;
122 struct maxwidths maxwidths;
123 struct statfs *mntbuf;
124 #ifdef MOUNT_CHAR_DEVS
125 struct iovec *iov = NULL;
128 #ifdef MOUNT_CHAR_DEVS
130 char errmsg[255] = {0};
133 const char **vfslist;
136 #ifdef MOUNT_CHAR_DEVS
141 (void)setlocale(LC_ALL, "");
142 memset(&maxwidths, 0, sizeof(maxwidths));
143 memset(&totalbuf, 0, sizeof(totalbuf));
144 totalbuf.f_bsize = DEV_BSIZE;
145 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
148 argc = xo_parse_args(argc, argv);
152 while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options,
162 * POSIX specifically discusses the behavior of
163 * both -k and -P. It states that the blocksize should
164 * be set to 1024. Thus, if this occurs, simply break
165 * rather than clobbering the old blocksize.
169 setenv("BLOCKSIZE", "512", 1);
176 setenv("BLOCKSIZE", "1g", 1);
190 setenv("BLOCKSIZE", "1024", 1);
194 /* Ignore duplicate -l */
198 xo_errx(1, "-l and -t are mutually exclusive.");
199 vfslist = makevfslist(makenetvfslist());
203 setenv("BLOCKSIZE", "1m", 1);
211 xo_errx(1, "-l and -t are mutually exclusive.");
213 xo_errx(1, "only one -t option may be specified");
215 vfslist = makevfslist(optarg);
232 /* everything (modulo -t) */
233 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
234 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
236 /* just the filesystems specified on the command line */
237 mntbuf = malloc(argc * sizeof(*mntbuf));
239 xo_err(1, "malloc()");
241 /* continued in for loop below */
244 xo_open_container("storage-system-information");
245 xo_open_list("filesystem");
247 /* iterate through specified filesystems */
248 for (; *argv; argv++) {
249 if (stat(*argv, &stbuf) < 0) {
250 if ((mntpt = getmntpt(*argv)) == NULL) {
251 xo_warn("%s", *argv);
255 } else if (S_ISCHR(stbuf.st_mode)) {
256 if ((mntpt = getmntpt(*argv)) == NULL) {
257 #ifdef MOUNT_CHAR_DEVS
259 "df on unmounted devices is deprecated");
261 mntpath = strdup("/tmp/df.XXXXXX");
262 if (mntpath == NULL) {
263 xo_warn("strdup failed");
267 mntpt = mkdtemp(mntpath);
269 xo_warn("mkdtemp(\"%s\") failed", mntpath);
275 free_iovec(&iov, &iovlen);
276 build_iovec_argf(&iov, &iovlen, "fstype", "%s",
278 build_iovec_argf(&iov, &iovlen, "fspath", "%s",
280 build_iovec_argf(&iov, &iovlen, "from", "%s",
282 build_iovec(&iov, &iovlen, "errmsg", errmsg,
284 if (nmount(iov, iovlen,
285 MNT_RDONLY|MNT_NOEXEC) < 0) {
287 xo_warn("%s: %s", *argv,
290 xo_warn("%s", *argv);
295 } else if (statfs(mntpt, &statfsbuf) == 0) {
296 statfsbuf.f_mntonname[0] = '\0';
297 prtstat(&statfsbuf, &maxwidths);
299 addstat(&totalbuf, &statfsbuf);
301 xo_warn("%s", *argv);
304 (void)unmount(mntpt, 0);
309 xo_warnx("%s: not mounted", *argv);
318 * Statfs does not take a `wait' flag, so we cannot
319 * implement nflag here.
321 if (statfs(mntpt, &statfsbuf) < 0) {
322 xo_warn("%s", mntpt);
328 * Check to make sure the arguments we've been given are
329 * satisfied. Return an error if we have been asked to
330 * list a mount point that does not match the other args
331 * we've been given (-l, -t, etc.).
333 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
338 /* the user asked for it, so ignore the ignore flag */
339 statfsbuf.f_flags &= ~MNT_IGNORE;
342 mntbuf[mntsize++] = statfsbuf;
345 memset(&maxwidths, 0, sizeof(maxwidths));
346 for (i = 0; i < mntsize; i++) {
347 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
348 update_maxwidths(&maxwidths, &mntbuf[i]);
350 addstat(&totalbuf, &mntbuf[i]);
353 for (i = 0; i < mntsize; i++)
354 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
355 prtstat(&mntbuf[i], &maxwidths);
357 xo_close_list("filesystem");
360 prtstat(&totalbuf, &maxwidths);
362 xo_close_container("storage-system-information");
368 getmntpt(const char *name)
371 struct statfs *mntbuf;
373 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
374 for (i = 0; i < mntsize; i++) {
375 if (!strcmp(mntbuf[i].f_mntfromname, name))
376 return (mntbuf[i].f_mntonname);
382 * Make a pass over the file system info in ``mntbuf'' filtering out
383 * file system types not in vfslist and possibly re-stating to get
384 * current (not cached) info. Returns the new count of valid statfs bufs.
387 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
390 struct statfs *mntbuf;
393 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
396 for (j = 0, i = 0; i < mntsize; i++) {
397 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
400 * XXX statfs(2) can fail for various reasons. It may be
401 * possible that the user does not have access to the
402 * pathname, if this happens, we will fall back on
403 * "stale" filesystem statistics.
405 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
406 if (nflag || error < 0)
409 xo_warnx("%s stats possibly stale",
410 mntbuf[i].f_mntonname);
411 mntbuf[j] = mntbuf[i];
419 prthuman(const struct statfs *sfsp, int64_t used)
422 prthumanval(" {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize);
423 prthumanval(" {:used/%6s}", used * sfsp->f_bsize);
424 prthumanval(" {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize);
428 prthumanval(const char *fmt, int64_t bytes)
433 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
434 if (hflag == UNITS_SI)
435 flags |= HN_DIVISOR_1000;
437 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
438 bytes, "", HN_AUTOSCALE, flags);
440 xo_attr("value", "%lld", (long long) bytes);
445 * Print an inode count in "human-readable" format.
448 prthumanvalinode(const char *fmt, int64_t bytes)
453 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
455 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
456 bytes, "", HN_AUTOSCALE, flags);
458 xo_attr("value", "%lld", (long long) bytes);
463 * Convert statfs returned file system size into BLOCKSIZE units.
466 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
468 return (num * (intmax_t) fsbs / (int64_t) bs);
472 * Print out status about a file system.
475 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
477 static long blocksize;
478 static int headerlen, timesthrough = 0;
479 static const char *header;
480 int64_t used, availblks, inodes;
483 if (++timesthrough == 1) {
484 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
485 mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
486 if (thousands) { /* make space for commas */
487 mwp->total += (mwp->total - 1) / 3;
488 mwp->used += (mwp->used - 1) / 3;
489 mwp->avail += (mwp->avail - 1) / 3;
490 mwp->iused += (mwp->iused - 1) / 3;
491 mwp->ifree += (mwp->ifree - 1) / 3;
495 mwp->total = mwp->used = mwp->avail =
498 header = getbsize(&headerlen, &blocksize);
499 mwp->total = imax(mwp->total, headerlen);
501 mwp->used = imax(mwp->used, (int)strlen("Used"));
502 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
504 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem");
506 xo_emit(" {T:/%-*s}", mwp->fstype, "Type");
507 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} Capacity",
509 mwp->used, "Used", mwp->avail, "Avail");
511 mwp->iused = imax(hflag ? 0 : mwp->iused,
512 (int)strlen(" iused"));
513 mwp->ifree = imax(hflag ? 0 : mwp->ifree,
514 (int)strlen("ifree"));
515 xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}",
516 mwp->iused - 2, "iused", mwp->ifree, "ifree");
518 xo_emit(" {T:Mounted on}\n");
521 xo_open_instance("filesystem");
522 /* Check for 0 block size. Can this happen? */
523 if (sfsp->f_bsize == 0) {
524 xo_warnx ("File system %s does not have a block size, assuming 512.",
528 xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname);
530 xo_emit(" {:type/%-*s}", mwp->fstype, sfsp->f_fstypename);
531 used = sfsp->f_blocks - sfsp->f_bfree;
532 availblks = sfsp->f_bavail + used;
534 prthuman(sfsp, used);
537 format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} "
538 "{t:available-blocks/%*j'd}";
540 format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} "
541 "{t:available-blocks/%*jd}";
543 mwp->total, fsbtoblk(sfsp->f_blocks,
544 sfsp->f_bsize, blocksize),
545 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
546 mwp->avail, fsbtoblk(sfsp->f_bavail,
547 sfsp->f_bsize, blocksize));
549 xo_emit(" {:used-percent/%5.0f}{U:%%}",
550 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
552 inodes = sfsp->f_files;
553 used = inodes - sfsp->f_ffree;
556 prthumanvalinode(" {:inodes-used/%5s}", used);
557 prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree);
560 format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}";
562 format = " {:inodes-used/%*jd} {:inodes-free/%*jd}";
563 xo_emit(format, mwp->iused, (intmax_t)used,
564 mwp->ifree, (intmax_t)sfsp->f_ffree);
566 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
567 inodes == 0 ? 100.0 :
568 (double)used / (double)inodes * 100.0);
571 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
572 xo_emit(" {:mounted-on}", sfsp->f_mntonname);
574 xo_close_instance("filesystem");
578 addstat(struct statfs *totalfsp, struct statfs *statfsp)
582 bsize = statfsp->f_bsize / totalfsp->f_bsize;
583 totalfsp->f_blocks += statfsp->f_blocks * bsize;
584 totalfsp->f_bfree += statfsp->f_bfree * bsize;
585 totalfsp->f_bavail += statfsp->f_bavail * bsize;
586 totalfsp->f_files += statfsp->f_files;
587 totalfsp->f_ffree += statfsp->f_ffree;
591 * Update the maximum field-width information in `mwp' based on
592 * the file system specified by `sfsp'.
595 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
597 static long blocksize = 0;
601 getbsize(&dummy, &blocksize);
603 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
604 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
605 mwp->total = imax(mwp->total, int64width(
606 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
607 mwp->used = imax(mwp->used,
608 int64width(fsbtoblk((int64_t)sfsp->f_blocks -
609 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
610 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
611 sfsp->f_bsize, blocksize)));
612 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
614 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
617 /* Return the width in characters of the specified value. */
619 int64width(int64_t val)
624 /* Negative or zero values require one extra digit. */
642 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
643 " [file | filesystem ...]\n");
650 char *str, *strptr, **listptr;
651 struct xvfsconf *xvfsp, *keep_xvfsp;
653 int cnt, i, maxvfsconf;
655 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
656 xo_warn("sysctl(vfs.conflist)");
659 xvfsp = malloc(buflen);
661 xo_warnx("malloc failed");
665 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
666 xo_warn("sysctl(vfs.conflist)");
670 maxvfsconf = buflen / sizeof(struct xvfsconf);
672 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
673 xo_warnx("malloc failed");
678 for (cnt = 0, i = 0; i < maxvfsconf; i++) {
679 if (xvfsp->vfc_flags & VFCF_NETWORK) {
680 listptr[cnt++] = strdup(xvfsp->vfc_name);
681 if (listptr[cnt-1] == NULL) {
682 xo_warnx("malloc failed");
692 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
694 xo_warnx("malloc failed");
700 *str = 'n'; *(str + 1) = 'o';
701 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
702 strlcpy(strptr, listptr[i], 32);
703 strptr += strlen(listptr[i]);