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