1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 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. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 */ 36 37 #if 0 38 #ifndef lint 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"; 42 #endif /* not lint */ 43 44 #ifndef lint 45 static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; 46 #endif /* not lint */ 47 #endif 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 51 #include <sys/param.h> 52 #include <sys/stat.h> 53 #include <sys/mount.h> 54 #include <sys/sysctl.h> 55 #include <err.h> 56 #include <getopt.h> 57 #include <libutil.h> 58 #include <locale.h> 59 #include <stdint.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <sysexits.h> 64 #include <unistd.h> 65 #include <libxo/xo.h> 66 67 #include "extern.h" 68 69 #define UNITS_SI 1 70 #define UNITS_2 2 71 72 /* Maximum widths of various fields. */ 73 struct maxwidths { 74 int mntfrom; 75 int fstype; 76 int total; 77 int used; 78 int avail; 79 int iused; 80 int ifree; 81 }; 82 83 static void addstat(struct statfs *, struct statfs *); 84 static char *getmntpt(const char *); 85 static int int64width(int64_t); 86 static char *makenetvfslist(void); 87 static void prthuman(const struct statfs *, int64_t); 88 static void prthumanval(const char *, int64_t); 89 static intmax_t fsbtoblk(int64_t, uint64_t, u_long); 90 static void prtstat(struct statfs *, struct maxwidths *); 91 static size_t regetmntinfo(struct statfs **, long, const char **); 92 static void update_maxwidths(struct maxwidths *, const struct statfs *); 93 static void usage(void); 94 95 static __inline int 96 imax(int a, int b) 97 { 98 return (a > b ? a : b); 99 } 100 101 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag; 102 static int thousands; 103 104 static const struct option long_options[] = 105 { 106 { "si", no_argument, NULL, 'H' }, 107 { NULL, no_argument, NULL, 0 }, 108 }; 109 110 int 111 main(int argc, char *argv[]) 112 { 113 struct stat stbuf; 114 struct statfs statfsbuf, totalbuf; 115 struct maxwidths maxwidths; 116 struct statfs *mntbuf; 117 char *mntpt; 118 const char **vfslist; 119 int i, mntsize; 120 int ch, rv; 121 122 (void)setlocale(LC_ALL, ""); 123 memset(&maxwidths, 0, sizeof(maxwidths)); 124 memset(&totalbuf, 0, sizeof(totalbuf)); 125 totalbuf.f_bsize = DEV_BSIZE; 126 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN); 127 vfslist = NULL; 128 129 argc = xo_parse_args(argc, argv); 130 if (argc < 0) 131 exit(1); 132 133 while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options, 134 NULL)) != -1) 135 switch (ch) { 136 case 'a': 137 aflag = 1; 138 break; 139 case 'b': 140 /* FALLTHROUGH */ 141 case 'P': 142 /* 143 * POSIX specifically discusses the behavior of 144 * both -k and -P. It states that the blocksize should 145 * be set to 1024. Thus, if this occurs, simply break 146 * rather than clobbering the old blocksize. 147 */ 148 if (kflag) 149 break; 150 setenv("BLOCKSIZE", "512", 1); 151 hflag = 0; 152 break; 153 case 'c': 154 cflag = 1; 155 break; 156 case 'g': 157 setenv("BLOCKSIZE", "1g", 1); 158 hflag = 0; 159 break; 160 case 'H': 161 hflag = UNITS_SI; 162 break; 163 case 'h': 164 hflag = UNITS_2; 165 break; 166 case 'i': 167 iflag = 1; 168 break; 169 case 'k': 170 kflag++; 171 setenv("BLOCKSIZE", "1024", 1); 172 hflag = 0; 173 break; 174 case 'l': 175 /* Ignore duplicate -l */ 176 if (lflag) 177 break; 178 if (vfslist != NULL) 179 xo_errx(1, "-l and -t are mutually exclusive."); 180 vfslist = makevfslist(makenetvfslist()); 181 lflag = 1; 182 break; 183 case 'm': 184 setenv("BLOCKSIZE", "1m", 1); 185 hflag = 0; 186 break; 187 case 'n': 188 nflag = 1; 189 break; 190 case 't': 191 if (lflag) 192 xo_errx(1, "-l and -t are mutually exclusive."); 193 if (vfslist != NULL) 194 xo_errx(1, "only one -t option may be specified"); 195 vfslist = makevfslist(optarg); 196 break; 197 case 'T': 198 Tflag = 1; 199 break; 200 case ',': 201 thousands = 1; 202 break; 203 case '?': 204 default: 205 usage(); 206 } 207 argc -= optind; 208 argv += optind; 209 210 rv = 0; 211 if (!*argv) { 212 /* everything (modulo -t) */ 213 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 214 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 215 } else { 216 /* just the filesystems specified on the command line */ 217 mntbuf = malloc(argc * sizeof(*mntbuf)); 218 if (mntbuf == NULL) 219 xo_err(1, "malloc()"); 220 mntsize = 0; 221 /* continued in for loop below */ 222 } 223 224 xo_open_container("storage-system-information"); 225 xo_open_list("filesystem"); 226 227 /* iterate through specified filesystems */ 228 for (; *argv; argv++) { 229 if (stat(*argv, &stbuf) < 0) { 230 if ((mntpt = getmntpt(*argv)) == NULL) { 231 xo_warn("%s", *argv); 232 rv = 1; 233 continue; 234 } 235 } else if (S_ISCHR(stbuf.st_mode)) { 236 mntpt = getmntpt(*argv); 237 if (mntpt == NULL) { 238 xo_warnx("%s: not mounted", *argv); 239 rv = 1; 240 continue; 241 } 242 } else { 243 mntpt = *argv; 244 } 245 246 /* 247 * Statfs does not take a `wait' flag, so we cannot 248 * implement nflag here. 249 */ 250 if (statfs(mntpt, &statfsbuf) < 0) { 251 xo_warn("%s", mntpt); 252 rv = 1; 253 continue; 254 } 255 256 /* 257 * Check to make sure the arguments we've been given are 258 * satisfied. Return an error if we have been asked to 259 * list a mount point that does not match the other args 260 * we've been given (-l, -t, etc.). 261 */ 262 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 263 rv = 1; 264 continue; 265 } 266 267 /* the user asked for it, so ignore the ignore flag */ 268 statfsbuf.f_flags &= ~MNT_IGNORE; 269 270 /* add to list */ 271 mntbuf[mntsize++] = statfsbuf; 272 } 273 274 memset(&maxwidths, 0, sizeof(maxwidths)); 275 for (i = 0; i < mntsize; i++) { 276 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) { 277 update_maxwidths(&maxwidths, &mntbuf[i]); 278 if (cflag) 279 addstat(&totalbuf, &mntbuf[i]); 280 } 281 } 282 for (i = 0; i < mntsize; i++) 283 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 284 prtstat(&mntbuf[i], &maxwidths); 285 286 xo_close_list("filesystem"); 287 288 if (cflag) 289 prtstat(&totalbuf, &maxwidths); 290 291 xo_close_container("storage-system-information"); 292 xo_finish(); 293 exit(rv); 294 } 295 296 static char * 297 getmntpt(const char *name) 298 { 299 size_t mntsize, i; 300 struct statfs *mntbuf; 301 302 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 303 for (i = 0; i < mntsize; i++) { 304 if (!strcmp(mntbuf[i].f_mntfromname, name)) 305 return (mntbuf[i].f_mntonname); 306 } 307 return (NULL); 308 } 309 310 /* 311 * Make a pass over the file system info in ``mntbuf'' filtering out 312 * file system types not in vfslist and possibly re-stating to get 313 * current (not cached) info. Returns the new count of valid statfs bufs. 314 */ 315 static size_t 316 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 317 { 318 int error, i, j; 319 struct statfs *mntbuf; 320 321 if (vfslist == NULL) 322 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 323 324 mntbuf = *mntbufp; 325 for (j = 0, i = 0; i < mntsize; i++) { 326 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 327 continue; 328 /* 329 * XXX statfs(2) can fail for various reasons. It may be 330 * possible that the user does not have access to the 331 * pathname, if this happens, we will fall back on 332 * "stale" filesystem statistics. 333 */ 334 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]); 335 if (nflag || error < 0) 336 if (i != j) { 337 if (error < 0) 338 xo_warnx("%s stats possibly stale", 339 mntbuf[i].f_mntonname); 340 mntbuf[j] = mntbuf[i]; 341 } 342 j++; 343 } 344 return (j); 345 } 346 347 static void 348 prthuman(const struct statfs *sfsp, int64_t used) 349 { 350 351 prthumanval(" {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize); 352 prthumanval(" {:used/%6s}", used * sfsp->f_bsize); 353 prthumanval(" {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize); 354 } 355 356 static void 357 prthumanval(const char *fmt, int64_t bytes) 358 { 359 char buf[6]; 360 int flags; 361 362 flags = HN_B | HN_NOSPACE | HN_DECIMAL; 363 if (hflag == UNITS_SI) 364 flags |= HN_DIVISOR_1000; 365 366 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 367 bytes, "", HN_AUTOSCALE, flags); 368 369 xo_attr("value", "%lld", (long long) bytes); 370 xo_emit(fmt, buf); 371 } 372 373 /* 374 * Print an inode count in "human-readable" format. 375 */ 376 static void 377 prthumanvalinode(const char *fmt, int64_t bytes) 378 { 379 char buf[6]; 380 int flags; 381 382 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000; 383 384 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 385 bytes, "", HN_AUTOSCALE, flags); 386 387 xo_attr("value", "%lld", (long long) bytes); 388 xo_emit(fmt, buf); 389 } 390 391 /* 392 * Convert statfs returned file system size into BLOCKSIZE units. 393 */ 394 static intmax_t 395 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs) 396 { 397 return (num * (intmax_t) fsbs / (int64_t) bs); 398 } 399 400 /* 401 * Print out status about a file system. 402 */ 403 static void 404 prtstat(struct statfs *sfsp, struct maxwidths *mwp) 405 { 406 static long blocksize; 407 static int headerlen, timesthrough = 0; 408 static const char *header; 409 int64_t used, availblks, inodes; 410 const char *format; 411 412 if (++timesthrough == 1) { 413 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 414 mwp->fstype = imax(mwp->fstype, (int)strlen("Type")); 415 if (thousands) { /* make space for commas */ 416 mwp->total += (mwp->total - 1) / 3; 417 mwp->used += (mwp->used - 1) / 3; 418 mwp->avail += (mwp->avail - 1) / 3; 419 mwp->iused += (mwp->iused - 1) / 3; 420 mwp->ifree += (mwp->ifree - 1) / 3; 421 } 422 if (hflag) { 423 header = " Size"; 424 mwp->total = mwp->used = mwp->avail = 425 (int)strlen(header); 426 } else { 427 header = getbsize(&headerlen, &blocksize); 428 mwp->total = imax(mwp->total, headerlen); 429 } 430 mwp->used = imax(mwp->used, (int)strlen("Used")); 431 mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 432 433 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem"); 434 if (Tflag) 435 xo_emit(" {T:/%-*s}", mwp->fstype, "Type"); 436 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} {T:Capacity}", 437 mwp->total, header, 438 mwp->used, "Used", mwp->avail, "Avail"); 439 if (iflag) { 440 mwp->iused = imax(hflag ? 0 : mwp->iused, 441 (int)strlen(" iused")); 442 mwp->ifree = imax(hflag ? 0 : mwp->ifree, 443 (int)strlen("ifree")); 444 xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}", 445 mwp->iused - 2, "iused", mwp->ifree, "ifree"); 446 } 447 xo_emit(" {T:Mounted on}\n"); 448 } 449 450 xo_open_instance("filesystem"); 451 /* Check for 0 block size. Can this happen? */ 452 if (sfsp->f_bsize == 0) { 453 xo_warnx ("File system %s does not have a block size, assuming 512.", 454 sfsp->f_mntonname); 455 sfsp->f_bsize = 512; 456 } 457 xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname); 458 if (Tflag) 459 xo_emit(" {:type/%-*s}", mwp->fstype, sfsp->f_fstypename); 460 used = sfsp->f_blocks - sfsp->f_bfree; 461 availblks = sfsp->f_bavail + used; 462 if (hflag) { 463 prthuman(sfsp, used); 464 } else { 465 if (thousands) 466 format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} " 467 "{t:available-blocks/%*j'd}"; 468 else 469 format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} " 470 "{t:available-blocks/%*jd}"; 471 xo_emit(format, 472 mwp->total, fsbtoblk(sfsp->f_blocks, 473 sfsp->f_bsize, blocksize), 474 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize), 475 mwp->avail, fsbtoblk(sfsp->f_bavail, 476 sfsp->f_bsize, blocksize)); 477 } 478 xo_emit(" {:used-percent/%5.0f}{U:%%}", 479 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 480 if (iflag) { 481 inodes = sfsp->f_files; 482 used = inodes - sfsp->f_ffree; 483 if (hflag) { 484 xo_emit(" "); 485 prthumanvalinode(" {:inodes-used/%5s}", used); 486 prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree); 487 } else { 488 if (thousands) 489 format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}"; 490 else 491 format = " {:inodes-used/%*jd} {:inodes-free/%*jd}"; 492 xo_emit(format, mwp->iused, (intmax_t)used, 493 mwp->ifree, (intmax_t)sfsp->f_ffree); 494 } 495 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ", 496 inodes == 0 ? 100.0 : 497 (double)used / (double)inodes * 100.0); 498 } else 499 xo_emit(" "); 500 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0) 501 xo_emit(" {:mounted-on}", sfsp->f_mntonname); 502 xo_emit("\n"); 503 xo_close_instance("filesystem"); 504 } 505 506 static void 507 addstat(struct statfs *totalfsp, struct statfs *statfsp) 508 { 509 uint64_t bsize; 510 511 bsize = statfsp->f_bsize / totalfsp->f_bsize; 512 totalfsp->f_blocks += statfsp->f_blocks * bsize; 513 totalfsp->f_bfree += statfsp->f_bfree * bsize; 514 totalfsp->f_bavail += statfsp->f_bavail * bsize; 515 totalfsp->f_files += statfsp->f_files; 516 totalfsp->f_ffree += statfsp->f_ffree; 517 } 518 519 /* 520 * Update the maximum field-width information in `mwp' based on 521 * the file system specified by `sfsp'. 522 */ 523 static void 524 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 525 { 526 static long blocksize = 0; 527 int dummy; 528 529 if (blocksize == 0) 530 getbsize(&dummy, &blocksize); 531 532 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 533 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename)); 534 mwp->total = imax(mwp->total, int64width( 535 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 536 mwp->used = imax(mwp->used, 537 int64width(fsbtoblk((int64_t)sfsp->f_blocks - 538 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 539 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 540 sfsp->f_bsize, blocksize))); 541 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 542 sfsp->f_ffree)); 543 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 544 } 545 546 /* Return the width in characters of the specified value. */ 547 static int 548 int64width(int64_t val) 549 { 550 int len; 551 552 len = 0; 553 /* Negative or zero values require one extra digit. */ 554 if (val <= 0) { 555 val = -val; 556 len++; 557 } 558 while (val > 0) { 559 len++; 560 val /= 10; 561 } 562 563 return (len); 564 } 565 566 static void 567 usage(void) 568 { 569 570 xo_error( 571 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n" 572 " [file | filesystem ...]\n"); 573 exit(EX_USAGE); 574 } 575 576 static char * 577 makenetvfslist(void) 578 { 579 char *str, *strptr, **listptr; 580 struct xvfsconf *xvfsp, *keep_xvfsp; 581 size_t buflen; 582 int cnt, i, maxvfsconf; 583 584 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 585 xo_warn("sysctl(vfs.conflist)"); 586 return (NULL); 587 } 588 xvfsp = malloc(buflen); 589 if (xvfsp == NULL) { 590 xo_warnx("malloc failed"); 591 return (NULL); 592 } 593 keep_xvfsp = xvfsp; 594 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 595 xo_warn("sysctl(vfs.conflist)"); 596 free(keep_xvfsp); 597 return (NULL); 598 } 599 maxvfsconf = buflen / sizeof(struct xvfsconf); 600 601 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 602 xo_warnx("malloc failed"); 603 free(keep_xvfsp); 604 return (NULL); 605 } 606 607 for (cnt = 0, i = 0; i < maxvfsconf; i++) { 608 if (xvfsp->vfc_flags & VFCF_NETWORK) { 609 listptr[cnt++] = strdup(xvfsp->vfc_name); 610 if (listptr[cnt-1] == NULL) { 611 xo_warnx("malloc failed"); 612 free(listptr); 613 free(keep_xvfsp); 614 return (NULL); 615 } 616 } 617 xvfsp++; 618 } 619 620 if (cnt == 0 || 621 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 622 if (cnt > 0) 623 xo_warnx("malloc failed"); 624 free(listptr); 625 free(keep_xvfsp); 626 return (NULL); 627 } 628 629 *str = 'n'; *(str + 1) = 'o'; 630 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 631 strlcpy(strptr, listptr[i], 32); 632 strptr += strlen(listptr[i]); 633 *strptr = ','; 634 free(listptr[i]); 635 } 636 *(--strptr) = '\0'; 637 638 free(keep_xvfsp); 639 free(listptr); 640 return (str); 641 } 642