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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #if 0 40 #ifndef lint 41 static const char copyright[] = 42 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 43 The Regents of the University of California. All rights reserved.\n"; 44 #endif /* not lint */ 45 46 #ifndef lint 47 static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; 48 #endif /* not lint */ 49 #endif 50 #include <sys/cdefs.h> 51 __FBSDID("$FreeBSD$"); 52 53 #include <sys/param.h> 54 #include <sys/stat.h> 55 #include <sys/mount.h> 56 #include <sys/sysctl.h> 57 #include <ufs/ufs/ufsmount.h> 58 #include <err.h> 59 #include <math.h> 60 #include <inttypes.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <sysexits.h> 65 #include <unistd.h> 66 67 #include "extern.h" 68 69 #define UNITS_SI 1 70 #define UNITS_2 2 71 72 #define KILO_SZ(n) (n) 73 #define MEGA_SZ(n) ((n) * (n)) 74 #define GIGA_SZ(n) ((n) * (n) * (n)) 75 #define TERA_SZ(n) ((n) * (n) * (n) * (n)) 76 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n)) 77 78 #define KILO_2_SZ (KILO_SZ(1024ULL)) 79 #define MEGA_2_SZ (MEGA_SZ(1024ULL)) 80 #define GIGA_2_SZ (GIGA_SZ(1024ULL)) 81 #define TERA_2_SZ (TERA_SZ(1024ULL)) 82 #define PETA_2_SZ (PETA_SZ(1024ULL)) 83 84 #define KILO_SI_SZ (KILO_SZ(1000ULL)) 85 #define MEGA_SI_SZ (MEGA_SZ(1000ULL)) 86 #define GIGA_SI_SZ (GIGA_SZ(1000ULL)) 87 #define TERA_SI_SZ (TERA_SZ(1000ULL)) 88 #define PETA_SI_SZ (PETA_SZ(1000ULL)) 89 90 /* Maximum widths of various fields. */ 91 struct maxwidths { 92 size_t mntfrom; 93 size_t total; 94 size_t used; 95 size_t avail; 96 size_t iused; 97 size_t ifree; 98 }; 99 100 static uintmax_t vals_si [] = { 101 1, 102 KILO_SI_SZ, 103 MEGA_SI_SZ, 104 GIGA_SI_SZ, 105 TERA_SI_SZ, 106 PETA_SI_SZ 107 }; 108 static uintmax_t vals_base2[] = { 109 1, 110 KILO_2_SZ, 111 MEGA_2_SZ, 112 GIGA_2_SZ, 113 TERA_2_SZ, 114 PETA_2_SZ 115 }; 116 static uintmax_t *valp; 117 118 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t; 119 120 static unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA }; 121 122 static char *getmntpt(const char *); 123 static size_t longwidth(long); 124 static char *makenetvfslist(void); 125 static void prthuman(const struct statfs *, long); 126 static void prthumanval(double); 127 static void prtstat(struct statfs *, struct maxwidths *); 128 static long regetmntinfo(struct statfs **, long, const char **); 129 static unit_t unit_adjust(double *); 130 static void update_maxwidths(struct maxwidths *, const struct statfs *); 131 static void usage(void); 132 133 static __inline u_int 134 max(u_int a, u_int b) 135 { 136 return (a > b ? a : b); 137 } 138 139 static int aflag = 0, hflag, iflag, nflag; 140 static struct ufs_args mdev; 141 142 int 143 main(int argc, char *argv[]) 144 { 145 struct stat stbuf; 146 struct statfs statfsbuf, *mntbuf; 147 struct maxwidths maxwidths; 148 const char *fstype; 149 char *mntpath, *mntpt; 150 const char **vfslist; 151 long mntsize; 152 int ch, i, rv; 153 154 fstype = "ufs"; 155 156 vfslist = NULL; 157 while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1) 158 switch (ch) { 159 case 'a': 160 aflag = 1; 161 break; 162 case 'b': 163 /* FALLTHROUGH */ 164 case 'P': 165 putenv("BLOCKSIZE=512"); 166 hflag = 0; 167 break; 168 case 'g': 169 putenv("BLOCKSIZE=1g"); 170 hflag = 0; 171 break; 172 case 'H': 173 hflag = UNITS_SI; 174 valp = vals_si; 175 break; 176 case 'h': 177 hflag = UNITS_2; 178 valp = vals_base2; 179 break; 180 case 'i': 181 iflag = 1; 182 break; 183 case 'k': 184 putenv("BLOCKSIZE=1k"); 185 hflag = 0; 186 break; 187 case 'l': 188 if (vfslist != NULL) 189 errx(1, "-l and -t are mutually exclusive."); 190 vfslist = makevfslist(makenetvfslist()); 191 break; 192 case 'm': 193 putenv("BLOCKSIZE=1m"); 194 hflag = 0; 195 break; 196 case 'n': 197 nflag = 1; 198 break; 199 case 't': 200 if (vfslist != NULL) 201 errx(1, "only one -t option may be specified"); 202 fstype = optarg; 203 vfslist = makevfslist(optarg); 204 break; 205 case '?': 206 default: 207 usage(); 208 } 209 argc -= optind; 210 argv += optind; 211 212 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 213 bzero(&maxwidths, sizeof(maxwidths)); 214 for (i = 0; i < mntsize; i++) 215 update_maxwidths(&maxwidths, &mntbuf[i]); 216 217 rv = 0; 218 if (!*argv) { 219 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 220 bzero(&maxwidths, sizeof(maxwidths)); 221 for (i = 0; i < mntsize; i++) 222 update_maxwidths(&maxwidths, &mntbuf[i]); 223 for (i = 0; i < mntsize; i++) { 224 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 225 prtstat(&mntbuf[i], &maxwidths); 226 } 227 exit(rv); 228 } 229 230 for (; *argv; argv++) { 231 if (stat(*argv, &stbuf) < 0) { 232 if ((mntpt = getmntpt(*argv)) == 0) { 233 warn("%s", *argv); 234 rv = 1; 235 continue; 236 } 237 } else if (S_ISCHR(stbuf.st_mode)) { 238 if ((mntpt = getmntpt(*argv)) == 0) { 239 mdev.fspec = *argv; 240 mntpath = strdup("/tmp/df.XXXXXX"); 241 if (mntpath == NULL) { 242 warn("strdup failed"); 243 rv = 1; 244 continue; 245 } 246 mntpt = mkdtemp(mntpath); 247 if (mntpt == NULL) { 248 warn("mkdtemp(\"%s\") failed", mntpath); 249 rv = 1; 250 free(mntpath); 251 continue; 252 } 253 if (mount(fstype, mntpt, MNT_RDONLY, 254 &mdev) != 0) { 255 warn("%s", *argv); 256 rv = 1; 257 (void)rmdir(mntpt); 258 free(mntpath); 259 continue; 260 } else if (statfs(mntpt, &statfsbuf) == 0) { 261 statfsbuf.f_mntonname[0] = '\0'; 262 prtstat(&statfsbuf, &maxwidths); 263 } else { 264 warn("%s", *argv); 265 rv = 1; 266 } 267 (void)unmount(mntpt, 0); 268 (void)rmdir(mntpt); 269 free(mntpath); 270 continue; 271 } 272 } else 273 mntpt = *argv; 274 /* 275 * Statfs does not take a `wait' flag, so we cannot 276 * implement nflag here. 277 */ 278 if (statfs(mntpt, &statfsbuf) < 0) { 279 warn("%s", mntpt); 280 rv = 1; 281 continue; 282 } 283 if (argc == 1) { 284 bzero(&maxwidths, sizeof(maxwidths)); 285 update_maxwidths(&maxwidths, &statfsbuf); 286 } 287 prtstat(&statfsbuf, &maxwidths); 288 } 289 return (rv); 290 } 291 292 static char * 293 getmntpt(const char *name) 294 { 295 long mntsize, i; 296 struct statfs *mntbuf; 297 298 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 299 for (i = 0; i < mntsize; i++) { 300 if (!strcmp(mntbuf[i].f_mntfromname, name)) 301 return (mntbuf[i].f_mntonname); 302 } 303 return (0); 304 } 305 306 /* 307 * Make a pass over the file system info in ``mntbuf'' filtering out 308 * file system types not in vfslist and possibly re-stating to get 309 * current (not cached) info. Returns the new count of valid statfs bufs. 310 */ 311 static long 312 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist) 313 { 314 int i, j; 315 struct statfs *mntbuf; 316 317 if (vfslist == NULL) 318 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 319 320 mntbuf = *mntbufp; 321 for (j = 0, i = 0; i < mntsize; i++) { 322 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 323 continue; 324 if (!nflag) 325 (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 326 else if (i != j) 327 mntbuf[j] = mntbuf[i]; 328 j++; 329 } 330 return (j); 331 } 332 333 /* 334 * Output in "human-readable" format. Uses 3 digits max and puts 335 * unit suffixes at the end. Makes output compact and easy to read, 336 * especially on huge disks. 337 * 338 */ 339 static unit_t 340 unit_adjust(double *val) 341 { 342 double abval; 343 unit_t unit; 344 int unit_sz; 345 346 abval = fabs(*val); 347 348 unit_sz = abval ? ilogb(abval) / 10 : 0; 349 350 if (unit_sz >= (int)UNIT_MAX) { 351 unit = NONE; 352 } else { 353 unit = unitp[unit_sz]; 354 *val /= (double)valp[unit_sz]; 355 } 356 357 return (unit); 358 } 359 360 static void 361 prthuman(const struct statfs *sfsp, long used) 362 { 363 364 prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize); 365 prthumanval((double)used * (double)sfsp->f_bsize); 366 prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize); 367 } 368 369 static void 370 prthumanval(double bytes) 371 { 372 373 unit_t unit; 374 unit = unit_adjust(&bytes); 375 376 if (bytes == 0) 377 (void)printf(" 0B"); 378 else if (bytes > 10) 379 (void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]); 380 else 381 (void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]); 382 } 383 384 /* 385 * Convert statfs returned file system size into BLOCKSIZE units. 386 * Attempts to avoid overflow for large file systems. 387 */ 388 #define fsbtoblk(num, fsbs, bs) \ 389 (((fsbs) != 0 && (fsbs) < (bs)) ? \ 390 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs))) 391 392 /* 393 * Print out status about a file system. 394 */ 395 static void 396 prtstat(struct statfs *sfsp, struct maxwidths *mwp) 397 { 398 static long blocksize; 399 static int headerlen, timesthrough = 0; 400 static const char *header; 401 long used, availblks, inodes; 402 403 if (++timesthrough == 1) { 404 mwp->mntfrom = max(mwp->mntfrom, strlen("Filesystem")); 405 if (hflag) { 406 header = " Size"; 407 mwp->total = mwp->used = mwp->avail = strlen(header); 408 } else { 409 header = getbsize(&headerlen, &blocksize); 410 mwp->total = max(mwp->total, (u_int)headerlen); 411 } 412 mwp->used = max(mwp->used, strlen("Used")); 413 mwp->avail = max(mwp->avail, strlen("Avail")); 414 415 (void)printf("%-*s %-*s %*s %*s Capacity", 416 (u_int)mwp->mntfrom, "Filesystem", 417 (u_int)mwp->total, header, 418 (u_int)mwp->used, "Used", 419 (u_int)mwp->avail, "Avail"); 420 if (iflag) { 421 mwp->iused = max(mwp->iused, strlen(" iused")); 422 mwp->ifree = max(mwp->ifree, strlen("ifree")); 423 (void)printf(" %*s %*s %%iused", 424 (u_int)mwp->iused - 2, "iused", 425 (u_int)mwp->ifree, "ifree"); 426 } 427 (void)printf(" Mounted on\n"); 428 } 429 (void)printf("%-*s", (u_int)mwp->mntfrom, sfsp->f_mntfromname); 430 used = sfsp->f_blocks - sfsp->f_bfree; 431 availblks = sfsp->f_bavail + used; 432 if (hflag) { 433 prthuman(sfsp, used); 434 } else { 435 (void)printf(" %*ld %*ld %*ld", 436 (u_int)mwp->total, fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize), 437 (u_int)mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize), 438 (u_int)mwp->avail, fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, 439 blocksize)); 440 } 441 (void)printf(" %5.0f%%", 442 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 443 if (iflag) { 444 inodes = sfsp->f_files; 445 used = inodes - sfsp->f_ffree; 446 (void)printf(" %*ld %*ld %4.0f%% ", 447 (u_int)mwp->iused, used, 448 (u_int)mwp->ifree, sfsp->f_ffree, 449 inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0); 450 } else 451 (void)printf(" "); 452 (void)printf(" %s\n", sfsp->f_mntonname); 453 } 454 455 /* 456 * Update the maximum field-width information in `mwp' based on 457 * the file system specified by `sfsp'. 458 */ 459 static void 460 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 461 { 462 static long blocksize = 0; 463 int dummy; 464 465 if (blocksize == 0) 466 getbsize(&dummy, &blocksize); 467 468 mwp->mntfrom = max(mwp->mntfrom, strlen(sfsp->f_mntfromname)); 469 mwp->total = max(mwp->total, longwidth(fsbtoblk(sfsp->f_blocks, 470 sfsp->f_bsize, blocksize))); 471 mwp->used = max(mwp->used, longwidth(fsbtoblk(sfsp->f_blocks - 472 sfsp->f_bfree, sfsp->f_bsize, blocksize))); 473 mwp->avail = max(mwp->avail, longwidth(fsbtoblk(sfsp->f_bavail, 474 sfsp->f_bsize, blocksize))); 475 mwp->iused = max(mwp->iused, longwidth(sfsp->f_files - 476 sfsp->f_ffree)); 477 mwp->ifree = max(mwp->ifree, longwidth(sfsp->f_ffree)); 478 } 479 480 /* Return the width in characters of the specified long. */ 481 static size_t 482 longwidth(long val) 483 { 484 size_t len; 485 486 len = 0; 487 /* Negative or zero values require one extra digit. */ 488 if (val <= 0) { 489 val = -val; 490 len++; 491 } 492 while (val > 0) { 493 len++; 494 val /= 10; 495 } 496 497 return (len); 498 } 499 500 static void 501 usage(void) 502 { 503 504 (void)fprintf(stderr, 505 "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n"); 506 exit(EX_USAGE); 507 } 508 509 static char * 510 makenetvfslist(void) 511 { 512 char *str, *strptr, **listptr; 513 struct xvfsconf *xvfsp, *keep_xvfsp; 514 size_t buflen; 515 int cnt, i, maxvfsconf; 516 517 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 518 warn("sysctl(vfs.conflist)"); 519 return (NULL); 520 } 521 xvfsp = malloc(buflen); 522 if (xvfsp == NULL) { 523 warnx("malloc failed"); 524 return (NULL); 525 } 526 keep_xvfsp = xvfsp; 527 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 528 warn("sysctl(vfs.conflist)"); 529 free(keep_xvfsp); 530 return (NULL); 531 } 532 maxvfsconf = buflen / sizeof(struct xvfsconf); 533 534 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 535 warnx("malloc failed"); 536 free(keep_xvfsp); 537 return (NULL); 538 } 539 540 for (cnt = 0, i = 0; i < maxvfsconf; i++) { 541 if (xvfsp->vfc_flags & VFCF_NETWORK) { 542 listptr[cnt++] = strdup(xvfsp->vfc_name); 543 if (listptr[cnt-1] == NULL) { 544 warnx("malloc failed"); 545 free(listptr); 546 free(keep_xvfsp); 547 return (NULL); 548 } 549 } 550 xvfsp++; 551 } 552 553 if (cnt == 0 || 554 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 555 if (cnt > 0) 556 warnx("malloc failed"); 557 free(listptr); 558 free(keep_xvfsp); 559 return (NULL); 560 } 561 562 *str = 'n'; *(str + 1) = 'o'; 563 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 564 strncpy(strptr, listptr[i], 32); 565 strptr += strlen(listptr[i]); 566 *strptr = ','; 567 free(listptr[i]); 568 } 569 *(--strptr) = NULL; 570 571 free(keep_xvfsp); 572 free(listptr); 573 return (str); 574 } 575