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>
59 #ifdef MOUNT_CHAR_DEVS
75 /* Maximum widths of various fields. */
86 static void addstat(struct statfs *, struct statfs *);
87 static char *getmntpt(const char *);
88 static int int64width(int64_t);
89 static char *makenetvfslist(void);
90 static void prthuman(const struct statfs *, int64_t);
91 static void prthumanval(const char *, int64_t);
92 static intmax_t fsbtoblk(int64_t, uint64_t, u_long);
93 static void prtstat(struct statfs *, struct maxwidths *);
94 static size_t regetmntinfo(struct statfs **, long, const char **);
95 static void update_maxwidths(struct maxwidths *, const struct statfs *);
96 static void usage(void);
101 return (a > b ? a : b);
104 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
105 static int thousands;
106 #ifdef MOUNT_CHAR_DEVS
107 static struct ufs_args mdev;
111 main(int argc, char *argv[])
114 struct statfs statfsbuf, totalbuf;
115 struct maxwidths maxwidths;
116 struct statfs *mntbuf;
117 #ifdef MOUNT_CHAR_DEVS
118 struct iovec *iov = NULL;
121 #ifdef MOUNT_CHAR_DEVS
123 char errmsg[255] = {0};
126 const char **vfslist;
129 #ifdef MOUNT_CHAR_DEVS
134 (void)setlocale(LC_ALL, "");
135 memset(&maxwidths, 0, sizeof(maxwidths));
136 memset(&totalbuf, 0, sizeof(totalbuf));
137 totalbuf.f_bsize = DEV_BSIZE;
138 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
141 argc = xo_parse_args(argc, argv);
145 while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T,")) != -1)
154 * POSIX specifically discusses the behavior of
155 * both -k and -P. It states that the blocksize should
156 * be set to 1024. Thus, if this occurs, simply break
157 * rather than clobbering the old blocksize.
161 setenv("BLOCKSIZE", "512", 1);
168 setenv("BLOCKSIZE", "1g", 1);
182 setenv("BLOCKSIZE", "1024", 1);
186 /* Ignore duplicate -l */
190 xo_errx(1, "-l and -t are mutually exclusive.");
191 vfslist = makevfslist(makenetvfslist());
195 setenv("BLOCKSIZE", "1m", 1);
203 xo_errx(1, "-l and -t are mutually exclusive.");
205 xo_errx(1, "only one -t option may be specified");
207 vfslist = makevfslist(optarg);
224 /* everything (modulo -t) */
225 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
226 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
228 /* just the filesystems specified on the command line */
229 mntbuf = malloc(argc * sizeof(*mntbuf));
231 xo_err(1, "malloc()");
233 /* continued in for loop below */
236 xo_open_container("storage-system-information");
237 xo_open_list("filesystem");
239 /* iterate through specified filesystems */
240 for (; *argv; argv++) {
241 if (stat(*argv, &stbuf) < 0) {
242 if ((mntpt = getmntpt(*argv)) == NULL) {
243 xo_warn("%s", *argv);
247 #ifdef MOUNT_CHAR_DEVS
248 } else if (S_ISCHR(stbuf.st_mode)) {
249 if ((mntpt = getmntpt(*argv)) == NULL) {
251 mntpath = strdup("/tmp/df.XXXXXX");
252 if (mntpath == NULL) {
253 xo_warn("strdup failed");
257 mntpt = mkdtemp(mntpath);
259 xo_warn("mkdtemp(\"%s\") failed", mntpath);
265 free_iovec(&iov, &iovlen);
266 build_iovec_argf(&iov, &iovlen, "fstype", "%s",
268 build_iovec_argf(&iov, &iovlen, "fspath", "%s",
270 build_iovec_argf(&iov, &iovlen, "from", "%s",
272 build_iovec(&iov, &iovlen, "errmsg", errmsg,
274 if (nmount(iov, iovlen,
275 MNT_RDONLY|MNT_NOEXEC) < 0) {
277 xo_warn("%s: %s", *argv,
280 xo_warn("%s", *argv);
285 } else if (statfs(mntpt, &statfsbuf) == 0) {
286 statfsbuf.f_mntonname[0] = '\0';
287 prtstat(&statfsbuf, &maxwidths);
289 addstat(&totalbuf, &statfsbuf);
291 xo_warn("%s", *argv);
294 (void)unmount(mntpt, 0);
304 * Statfs does not take a `wait' flag, so we cannot
305 * implement nflag here.
307 if (statfs(mntpt, &statfsbuf) < 0) {
308 xo_warn("%s", mntpt);
314 * Check to make sure the arguments we've been given are
315 * satisfied. Return an error if we have been asked to
316 * list a mount point that does not match the other args
317 * we've been given (-l, -t, etc.).
319 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
324 /* the user asked for it, so ignore the ignore flag */
325 statfsbuf.f_flags &= ~MNT_IGNORE;
328 mntbuf[mntsize++] = statfsbuf;
331 memset(&maxwidths, 0, sizeof(maxwidths));
332 for (i = 0; i < mntsize; i++) {
333 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
334 update_maxwidths(&maxwidths, &mntbuf[i]);
336 addstat(&totalbuf, &mntbuf[i]);
339 for (i = 0; i < mntsize; i++)
340 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
341 prtstat(&mntbuf[i], &maxwidths);
343 xo_close_list("filesystem");
346 prtstat(&totalbuf, &maxwidths);
348 xo_close_container("storage-system-information");
354 getmntpt(const char *name)
357 struct statfs *mntbuf;
359 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
360 for (i = 0; i < mntsize; i++) {
361 if (!strcmp(mntbuf[i].f_mntfromname, name))
362 return (mntbuf[i].f_mntonname);
368 * Make a pass over the file system info in ``mntbuf'' filtering out
369 * file system types not in vfslist and possibly re-stating to get
370 * current (not cached) info. Returns the new count of valid statfs bufs.
373 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
376 struct statfs *mntbuf;
379 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
382 for (j = 0, i = 0; i < mntsize; i++) {
383 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
386 * XXX statfs(2) can fail for various reasons. It may be
387 * possible that the user does not have access to the
388 * pathname, if this happens, we will fall back on
389 * "stale" filesystem statistics.
391 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
392 if (nflag || error < 0)
395 xo_warnx("%s stats possibly stale",
396 mntbuf[i].f_mntonname);
397 mntbuf[j] = mntbuf[i];
405 prthuman(const struct statfs *sfsp, int64_t used)
408 prthumanval(" {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize);
409 prthumanval(" {:used/%6s}", used * sfsp->f_bsize);
410 prthumanval(" {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize);
414 prthumanval(const char *fmt, int64_t bytes)
419 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
420 if (hflag == UNITS_SI)
421 flags |= HN_DIVISOR_1000;
423 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
424 bytes, "", HN_AUTOSCALE, flags);
426 xo_attr("value", "%lld", (long long) bytes);
431 * Print an inode count in "human-readable" format.
434 prthumanvalinode(const char *fmt, int64_t bytes)
439 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
441 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
442 bytes, "", HN_AUTOSCALE, flags);
444 xo_attr("value", "%lld", (long long) bytes);
449 * Convert statfs returned file system size into BLOCKSIZE units.
452 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
454 return (num * (intmax_t) fsbs / (int64_t) bs);
458 * Print out status about a file system.
461 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
463 static long blocksize;
464 static int headerlen, timesthrough = 0;
465 static const char *header;
466 int64_t used, availblks, inodes;
469 if (++timesthrough == 1) {
470 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
471 mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
472 if (thousands) { /* make space for commas */
473 mwp->total += (mwp->total - 1) / 3;
474 mwp->used += (mwp->used - 1) / 3;
475 mwp->avail += (mwp->avail - 1) / 3;
476 mwp->iused += (mwp->iused - 1) / 3;
477 mwp->ifree += (mwp->ifree - 1) / 3;
481 mwp->total = mwp->used = mwp->avail =
484 header = getbsize(&headerlen, &blocksize);
485 mwp->total = imax(mwp->total, headerlen);
487 mwp->used = imax(mwp->used, (int)strlen("Used"));
488 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
490 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem");
492 xo_emit(" {T:/%-*s}", mwp->fstype, "Type");
493 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} Capacity",
495 mwp->used, "Used", mwp->avail, "Avail");
497 mwp->iused = imax(hflag ? 0 : mwp->iused,
498 (int)strlen(" iused"));
499 mwp->ifree = imax(hflag ? 0 : mwp->ifree,
500 (int)strlen("ifree"));
501 xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}",
502 mwp->iused - 2, "iused", mwp->ifree, "ifree");
504 xo_emit(" {T:Mounted on}\n");
507 xo_open_instance("filesystem");
508 /* Check for 0 block size. Can this happen? */
509 if (sfsp->f_bsize == 0) {
510 xo_warnx ("File system %s does not have a block size, assuming 512.",
514 xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname);
516 xo_emit(" {:type/%-*s}", mwp->fstype, sfsp->f_fstypename);
517 used = sfsp->f_blocks - sfsp->f_bfree;
518 availblks = sfsp->f_bavail + used;
520 prthuman(sfsp, used);
523 format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} "
524 "{t:available-blocks/%*j'd}";
526 format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} "
527 "{t:available-blocks/%*jd}";
529 mwp->total, fsbtoblk(sfsp->f_blocks,
530 sfsp->f_bsize, blocksize),
531 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
532 mwp->avail, fsbtoblk(sfsp->f_bavail,
533 sfsp->f_bsize, blocksize));
535 xo_emit(" {:used-percent/%5.0f}{U:%%}",
536 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
538 inodes = sfsp->f_files;
539 used = inodes - sfsp->f_ffree;
542 prthumanvalinode(" {:inodes-used/%5s}", used);
543 prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree);
546 format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}";
548 format = " {:inodes-used/%*jd} {:inodes-free/%*jd}";
549 xo_emit(format, mwp->iused, (intmax_t)used,
550 mwp->ifree, (intmax_t)sfsp->f_ffree);
552 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
553 inodes == 0 ? 100.0 :
554 (double)used / (double)inodes * 100.0);
557 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
558 xo_emit(" {:mounted-on}", sfsp->f_mntonname);
560 xo_close_instance("filesystem");
564 addstat(struct statfs *totalfsp, struct statfs *statfsp)
568 bsize = statfsp->f_bsize / totalfsp->f_bsize;
569 totalfsp->f_blocks += statfsp->f_blocks * bsize;
570 totalfsp->f_bfree += statfsp->f_bfree * bsize;
571 totalfsp->f_bavail += statfsp->f_bavail * bsize;
572 totalfsp->f_files += statfsp->f_files;
573 totalfsp->f_ffree += statfsp->f_ffree;
577 * Update the maximum field-width information in `mwp' based on
578 * the file system specified by `sfsp'.
581 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
583 static long blocksize = 0;
587 getbsize(&dummy, &blocksize);
589 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
590 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
591 mwp->total = imax(mwp->total, int64width(
592 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
593 mwp->used = imax(mwp->used,
594 int64width(fsbtoblk((int64_t)sfsp->f_blocks -
595 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
596 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
597 sfsp->f_bsize, blocksize)));
598 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
600 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
603 /* Return the width in characters of the specified value. */
605 int64width(int64_t val)
610 /* Negative or zero values require one extra digit. */
628 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
629 " [file | filesystem ...]\n");
636 char *str, *strptr, **listptr;
637 struct xvfsconf *xvfsp, *keep_xvfsp;
639 int cnt, i, maxvfsconf;
641 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
642 xo_warn("sysctl(vfs.conflist)");
645 xvfsp = malloc(buflen);
647 xo_warnx("malloc failed");
651 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
652 xo_warn("sysctl(vfs.conflist)");
656 maxvfsconf = buflen / sizeof(struct xvfsconf);
658 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
659 xo_warnx("malloc failed");
664 for (cnt = 0, i = 0; i < maxvfsconf; i++) {
665 if (xvfsp->vfc_flags & VFCF_NETWORK) {
666 listptr[cnt++] = strdup(xvfsp->vfc_name);
667 if (listptr[cnt-1] == NULL) {
668 xo_warnx("malloc failed");
678 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
680 xo_warnx("malloc failed");
686 *str = 'n'; *(str + 1) = 'o';
687 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
688 strlcpy(strptr, listptr[i], 32);
689 strptr += strlen(listptr[i]);