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 if (vfslist != NULL) 170 xo_errx(1, "-l and -t are mutually exclusive."); 171 vfslist = makevfslist(makenetvfslist()); 172 lflag = 1; 173 break; 174 case 'm': 175 setenv("BLOCKSIZE", "1m", 1); 176 hflag = 0; 177 break; 178 case 'n': 179 nflag = 1; 180 break; 181 case 't': 182 if (lflag) 183 xo_errx(1, "-l and -t are mutually exclusive."); 184 if (vfslist != NULL) 185 xo_errx(1, "only one -t option may be specified"); 186 fstype = optarg; 187 vfslist = makevfslist(optarg); 188 break; 189 case 'T': 190 Tflag = 1; 191 break; 192 case ',': 193 thousands = 1; 194 break; 195 case '?': 196 default: 197 usage(); 198 } 199 argc -= optind; 200 argv += optind; 201 202 rv = 0; 203 if (!*argv) { 204 /* everything (modulo -t) */ 205 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 206 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 207 } else { 208 /* just the filesystems specified on the command line */ 209 mntbuf = malloc(argc * sizeof(*mntbuf)); 210 if (mntbuf == NULL) 211 xo_err(1, "malloc()"); 212 mntsize = 0; 213 /* continued in for loop below */ 214 } 215 216 xo_open_container("storage-system-information"); 217 xo_open_list("filesystem"); 218 219 /* iterate through specified filesystems */ 220 for (; *argv; argv++) { 221 if (stat(*argv, &stbuf) < 0) { 222 if ((mntpt = getmntpt(*argv)) == NULL) { 223 xo_warn("%s", *argv); 224 rv = 1; 225 continue; 226 } 227 } else if (S_ISCHR(stbuf.st_mode)) { 228 if ((mntpt = getmntpt(*argv)) == NULL) { 229 mdev.fspec = *argv; 230 mntpath = strdup("/tmp/df.XXXXXX"); 231 if (mntpath == NULL) { 232 xo_warn("strdup failed"); 233 rv = 1; 234 continue; 235 } 236 mntpt = mkdtemp(mntpath); 237 if (mntpt == NULL) { 238 xo_warn("mkdtemp(\"%s\") failed", mntpath); 239 rv = 1; 240 free(mntpath); 241 continue; 242 } 243 if (mount(fstype, mntpt, MNT_RDONLY, 244 &mdev) != 0) { 245 xo_warn("%s", *argv); 246 rv = 1; 247 (void)rmdir(mntpt); 248 free(mntpath); 249 continue; 250 } else if (statfs(mntpt, &statfsbuf) == 0) { 251 statfsbuf.f_mntonname[0] = '\0'; 252 prtstat(&statfsbuf, &maxwidths); 253 if (cflag) 254 addstat(&totalbuf, &statfsbuf); 255 } else { 256 xo_warn("%s", *argv); 257 rv = 1; 258 } 259 (void)unmount(mntpt, 0); 260 (void)rmdir(mntpt); 261 free(mntpath); 262 continue; 263 } 264 } else 265 mntpt = *argv; 266 267 /* 268 * Statfs does not take a `wait' flag, so we cannot 269 * implement nflag here. 270 */ 271 if (statfs(mntpt, &statfsbuf) < 0) { 272 xo_warn("%s", mntpt); 273 rv = 1; 274 continue; 275 } 276 277 /* 278 * Check to make sure the arguments we've been given are 279 * satisfied. Return an error if we have been asked to 280 * list a mount point that does not match the other args 281 * we've been given (-l, -t, etc.). 282 */ 283 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { 284 rv = 1; 285 continue; 286 } 287 288 /* the user asked for it, so ignore the ignore flag */ 289 statfsbuf.f_flags &= ~MNT_IGNORE; 290 291 /* add to list */ 292 mntbuf[mntsize++] = statfsbuf; 293 } 294 295 memset(&maxwidths, 0, sizeof(maxwidths)); 296 for (i = 0; i < mntsize; i++) { 297 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) { 298 update_maxwidths(&maxwidths, &mntbuf[i]); 299 if (cflag) 300 addstat(&totalbuf, &mntbuf[i]); 301 } 302 } 303 for (i = 0; i < mntsize; i++) 304 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 305 prtstat(&mntbuf[i], &maxwidths); 306 307 xo_close_list("filesystem"); 308 309 if (cflag) 310 prtstat(&totalbuf, &maxwidths); 311 312 xo_close_container("storage-system-information"); 313 xo_finish(); 314 exit(rv); 315 } 316 317 static char * 318 getmntpt(const char *name) 319 { 320 size_t mntsize, i; 321 struct statfs *mntbuf; 322 323 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 324 for (i = 0; i < mntsize; i++) { 325 if (!strcmp(mntbuf[i].f_mntfromname, name)) 326 return (mntbuf[i].f_mntonname); 327 } 328 return (NULL); 329 } 330 331 /* 332 * Make a pass over the file system info in ``mntbuf'' filtering out 333 * file system types not in vfslist and possibly re-stating to get 334 * current (not cached) info. Returns the new count of valid statfs bufs. 335 */ 336 static size_t 337 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 338 { 339 int error, i, j; 340 struct statfs *mntbuf; 341 342 if (vfslist == NULL) 343 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 344 345 mntbuf = *mntbufp; 346 for (j = 0, i = 0; i < mntsize; i++) { 347 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 348 continue; 349 /* 350 * XXX statfs(2) can fail for various reasons. It may be 351 * possible that the user does not have access to the 352 * pathname, if this happens, we will fall back on 353 * "stale" filesystem statistics. 354 */ 355 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]); 356 if (nflag || error < 0) 357 if (i != j) { 358 if (error < 0) 359 xo_warnx("%s stats possibly stale", 360 mntbuf[i].f_mntonname); 361 mntbuf[j] = mntbuf[i]; 362 } 363 j++; 364 } 365 return (j); 366 } 367 368 static void 369 prthuman(const struct statfs *sfsp, int64_t used) 370 { 371 372 prthumanval(" {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize); 373 prthumanval(" {:used/%6s}", used * sfsp->f_bsize); 374 prthumanval(" {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize); 375 } 376 377 static void 378 prthumanval(const char *fmt, int64_t bytes) 379 { 380 char buf[6]; 381 int flags; 382 383 flags = HN_B | HN_NOSPACE | HN_DECIMAL; 384 if (hflag == UNITS_SI) 385 flags |= HN_DIVISOR_1000; 386 387 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 388 bytes, "", HN_AUTOSCALE, flags); 389 390 xo_attr("value", "%lld", (long long) bytes); 391 xo_emit(fmt, buf); 392 } 393 394 /* 395 * Print an inode count in "human-readable" format. 396 */ 397 static void 398 prthumanvalinode(const char *fmt, int64_t bytes) 399 { 400 char buf[6]; 401 int flags; 402 403 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000; 404 405 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 406 bytes, "", HN_AUTOSCALE, flags); 407 408 xo_attr("value", "%lld", (long long) bytes); 409 xo_emit(fmt, buf); 410 } 411 412 /* 413 * Convert statfs returned file system size into BLOCKSIZE units. 414 */ 415 static intmax_t 416 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs) 417 { 418 return (num * (intmax_t) fsbs / (int64_t) bs); 419 } 420 421 /* 422 * Print out status about a file system. 423 */ 424 static void 425 prtstat(struct statfs *sfsp, struct maxwidths *mwp) 426 { 427 static long blocksize; 428 static int headerlen, timesthrough = 0; 429 static const char *header; 430 int64_t used, availblks, inodes; 431 const char *format; 432 433 if (++timesthrough == 1) { 434 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 435 mwp->fstype = imax(mwp->fstype, (int)strlen("Type")); 436 if (thousands) { /* make space for commas */ 437 mwp->total += (mwp->total - 1) / 3; 438 mwp->used += (mwp->used - 1) / 3; 439 mwp->avail += (mwp->avail - 1) / 3; 440 mwp->iused += (mwp->iused - 1) / 3; 441 mwp->ifree += (mwp->ifree - 1) / 3; 442 } 443 if (hflag) { 444 header = " Size"; 445 mwp->total = mwp->used = mwp->avail = 446 (int)strlen(header); 447 } else { 448 header = getbsize(&headerlen, &blocksize); 449 mwp->total = imax(mwp->total, headerlen); 450 } 451 mwp->used = imax(mwp->used, (int)strlen("Used")); 452 mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 453 454 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem"); 455 if (Tflag) 456 xo_emit(" {T:/%-*s}", mwp->fstype, "Type"); 457 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} Capacity", 458 mwp->total, header, 459 mwp->used, "Used", mwp->avail, "Avail"); 460 if (iflag) { 461 mwp->iused = imax(hflag ? 0 : mwp->iused, 462 (int)strlen(" iused")); 463 mwp->ifree = imax(hflag ? 0 : mwp->ifree, 464 (int)strlen("ifree")); 465 xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}", 466 mwp->iused - 2, "iused", mwp->ifree, "ifree"); 467 } 468 xo_emit(" {T:Mounted on}\n"); 469 } 470 471 xo_open_instance("filesystem"); 472 /* Check for 0 block size. Can this happen? */ 473 if (sfsp->f_bsize == 0) { 474 xo_warnx ("File system %s does not have a block size, assuming 512.", 475 sfsp->f_mntonname); 476 sfsp->f_bsize = 512; 477 } 478 xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname); 479 if (Tflag) 480 xo_emit(" {:type/%-*s}", mwp->fstype, sfsp->f_fstypename); 481 used = sfsp->f_blocks - sfsp->f_bfree; 482 availblks = sfsp->f_bavail + used; 483 if (hflag) { 484 prthuman(sfsp, used); 485 } else { 486 if (thousands) 487 format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} " 488 "{t:available-blocks/%*j'd}"; 489 else 490 format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} " 491 "{t:available-blocks/%*jd}"; 492 xo_emit(format, 493 mwp->total, fsbtoblk(sfsp->f_blocks, 494 sfsp->f_bsize, blocksize), 495 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize), 496 mwp->avail, fsbtoblk(sfsp->f_bavail, 497 sfsp->f_bsize, blocksize)); 498 } 499 xo_emit(" {:used-percent/%5.0f}{U:%%}", 500 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 501 if (iflag) { 502 inodes = sfsp->f_files; 503 used = inodes - sfsp->f_ffree; 504 if (hflag) { 505 xo_emit(" "); 506 prthumanvalinode(" {:inodes-used/%5s}", used); 507 prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree); 508 } else { 509 if (thousands) 510 format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}"; 511 else 512 format = " {:inodes-used/%*jd} {:inodes-free/%*jd}"; 513 xo_emit(format, mwp->iused, (intmax_t)used, 514 mwp->ifree, (intmax_t)sfsp->f_ffree); 515 } 516 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ", 517 inodes == 0 ? 100.0 : 518 (double)used / (double)inodes * 100.0); 519 } else 520 xo_emit(" "); 521 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0) 522 xo_emit(" {:mounted-on}", sfsp->f_mntonname); 523 xo_emit("\n"); 524 xo_close_instance("filesystem"); 525 } 526 527 static void 528 addstat(struct statfs *totalfsp, struct statfs *statfsp) 529 { 530 uint64_t bsize; 531 532 bsize = statfsp->f_bsize / totalfsp->f_bsize; 533 totalfsp->f_blocks += statfsp->f_blocks * bsize; 534 totalfsp->f_bfree += statfsp->f_bfree * bsize; 535 totalfsp->f_bavail += statfsp->f_bavail * bsize; 536 totalfsp->f_files += statfsp->f_files; 537 totalfsp->f_ffree += statfsp->f_ffree; 538 } 539 540 /* 541 * Update the maximum field-width information in `mwp' based on 542 * the file system specified by `sfsp'. 543 */ 544 static void 545 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 546 { 547 static long blocksize = 0; 548 int dummy; 549 550 if (blocksize == 0) 551 getbsize(&dummy, &blocksize); 552 553 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 554 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename)); 555 mwp->total = imax(mwp->total, int64width( 556 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 557 mwp->used = imax(mwp->used, 558 int64width(fsbtoblk((int64_t)sfsp->f_blocks - 559 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 560 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 561 sfsp->f_bsize, blocksize))); 562 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 563 sfsp->f_ffree)); 564 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 565 } 566 567 /* Return the width in characters of the specified value. */ 568 static int 569 int64width(int64_t val) 570 { 571 int len; 572 573 len = 0; 574 /* Negative or zero values require one extra digit. */ 575 if (val <= 0) { 576 val = -val; 577 len++; 578 } 579 while (val > 0) { 580 len++; 581 val /= 10; 582 } 583 584 return (len); 585 } 586 587 static void 588 usage(void) 589 { 590 591 xo_error( 592 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n" 593 " [file | filesystem ...]\n"); 594 exit(EX_USAGE); 595 } 596 597 static char * 598 makenetvfslist(void) 599 { 600 char *str, *strptr, **listptr; 601 struct xvfsconf *xvfsp, *keep_xvfsp; 602 size_t buflen; 603 int cnt, i, maxvfsconf; 604 605 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 606 xo_warn("sysctl(vfs.conflist)"); 607 return (NULL); 608 } 609 xvfsp = malloc(buflen); 610 if (xvfsp == NULL) { 611 xo_warnx("malloc failed"); 612 return (NULL); 613 } 614 keep_xvfsp = xvfsp; 615 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 616 xo_warn("sysctl(vfs.conflist)"); 617 free(keep_xvfsp); 618 return (NULL); 619 } 620 maxvfsconf = buflen / sizeof(struct xvfsconf); 621 622 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 623 xo_warnx("malloc failed"); 624 free(keep_xvfsp); 625 return (NULL); 626 } 627 628 for (cnt = 0, i = 0; i < maxvfsconf; i++) { 629 if (xvfsp->vfc_flags & VFCF_NETWORK) { 630 listptr[cnt++] = strdup(xvfsp->vfc_name); 631 if (listptr[cnt-1] == NULL) { 632 xo_warnx("malloc failed"); 633 free(listptr); 634 free(keep_xvfsp); 635 return (NULL); 636 } 637 } 638 xvfsp++; 639 } 640 641 if (cnt == 0 || 642 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 643 if (cnt > 0) 644 xo_warnx("malloc failed"); 645 free(listptr); 646 free(keep_xvfsp); 647 return (NULL); 648 } 649 650 *str = 'n'; *(str + 1) = 'o'; 651 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 652 strlcpy(strptr, listptr[i], 32); 653 strptr += strlen(listptr[i]); 654 *strptr = ','; 655 free(listptr[i]); 656 } 657 *(--strptr) = '\0'; 658 659 free(keep_xvfsp); 660 free(listptr); 661 return (str); 662 } 663