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 * @(#) Copyright (c) 1980, 1990, 1993, 1994 The Regents of the University of California. All rights reserved. 39 * @(#)df.c 8.9 (Berkeley) 5/8/95 40 * $FreeBSD: src/bin/df/df.c,v 1.23.2.9 2002/07/01 00:14:24 iedowse Exp $ 41 * $DragonFly: src/bin/df/df.c,v 1.13 2008/08/07 09:59:49 swildner Exp $ 42 */ 43 44 #include <sys/cdefs.h> 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 #include <sys/mount.h> 48 #include <sys/sysctl.h> 49 #include <sys/statvfs.h> 50 51 #include <vfs/ufs/dinode.h> 52 #include <vfs/ufs/fs.h> 53 #include <vfs/ufs/ufsmount.h> 54 55 #include <err.h> 56 #include <errno.h> 57 #include <fcntl.h> 58 #include <fstab.h> 59 #include <libutil.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <sysexits.h> 64 #include <unistd.h> 65 66 #define UNITS_SI 1 67 #define UNITS_2 2 68 69 /* Maximum widths of various fields. */ 70 struct maxwidths { 71 int mntfrom; 72 int total; 73 int used; 74 int avail; 75 int iused; 76 int ifree; 77 }; 78 79 int bread(off_t, void *, int); 80 int checkvfsname(const char *, char **); 81 char *getmntpt(char *); 82 int quadwidth(int64_t); 83 char *makenetvfslist(void); 84 char **makevfslist(char *); 85 void prthuman(struct statvfs *, int64_t); 86 void prthumanval(int64_t); 87 void prtstat(struct statfs *, struct statvfs *, struct maxwidths *); 88 long regetmntinfo(struct statfs **, struct statvfs **, long, char **); 89 int ufs_df(char *, struct maxwidths *); 90 void update_maxwidths(struct maxwidths *, struct statfs *, struct statvfs *); 91 void usage(void); 92 93 int aflag = 0, hflag, iflag, nflag; 94 struct ufs_args mdev; 95 96 static __inline int 97 imax(int a, int b) 98 { 99 return (a > b ? a : b); 100 } 101 102 static __inline int64_t 103 qmax(int64_t a, int64_t b) 104 { 105 return (a > b ? a : b); 106 } 107 108 int 109 main(int argc, char **argv) 110 { 111 struct stat stbuf; 112 struct statfs statfsbuf, *mntbuf; 113 struct statvfs statvfsbuf, *mntvbuf; 114 struct maxwidths maxwidths; 115 const char *fstype; 116 char *mntpath, *mntpt, **vfslist; 117 long mntsize; 118 int ch, i, rv; 119 120 fstype = "ufs"; 121 122 vfslist = NULL; 123 while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1) 124 switch (ch) { 125 case 'a': 126 aflag = 1; 127 break; 128 case 'b': 129 /* FALLTHROUGH */ 130 case 'P': 131 if (setenv("BLOCKSIZE", "512", 1) != 0) 132 warn("setenv: cannot set BLOCKSIZE=512"); 133 hflag = 0; 134 break; 135 case 'g': 136 if (setenv("BLOCKSIZE", "1g", 1) != 0) 137 warn("setenv: cannot set BLOCKSIZE=1g"); 138 hflag = 0; 139 break; 140 case 'H': 141 hflag = UNITS_SI; 142 break; 143 case 'h': 144 hflag = UNITS_2; 145 break; 146 case 'i': 147 iflag = 1; 148 break; 149 case 'k': 150 if (setenv("BLOCKSIZE", "1k", 1) != 0) 151 warn("setenv: cannot set BLOCKSIZE=1k"); 152 hflag = 0; 153 break; 154 case 'l': 155 if (vfslist != NULL) 156 errx(1, "-l and -t are mutually exclusive."); 157 vfslist = makevfslist(makenetvfslist()); 158 break; 159 case 'm': 160 if (setenv("BLOCKSIZE", "1m", 1) != 0) 161 warn("setenv: cannot set BLOCKSIZE=1m"); 162 hflag = 0; 163 break; 164 case 'n': 165 nflag = 1; 166 break; 167 case 't': 168 if (vfslist != NULL) 169 errx(1, "only one -t option may be specified"); 170 fstype = optarg; 171 vfslist = makevfslist(optarg); 172 break; 173 case '?': 174 default: 175 usage(); 176 } 177 argc -= optind; 178 argv += optind; 179 180 mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT); 181 bzero(&maxwidths, sizeof(maxwidths)); 182 for (i = 0; i < mntsize; i++) 183 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]); 184 185 rv = 0; 186 if (!*argv) { 187 mntsize = regetmntinfo(&mntbuf, &mntvbuf, mntsize, vfslist); 188 bzero(&maxwidths, sizeof(maxwidths)); 189 for (i = 0; i < mntsize; i++) 190 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]); 191 for (i = 0; i < mntsize; i++) { 192 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 193 prtstat(&mntbuf[i], &mntvbuf[i], &maxwidths); 194 } 195 exit(rv); 196 } 197 198 for (; *argv; argv++) { 199 if (stat(*argv, &stbuf) < 0) { 200 if ((mntpt = getmntpt(*argv)) == 0) { 201 warn("%s", *argv); 202 rv = 1; 203 continue; 204 } 205 } else if (S_ISCHR(stbuf.st_mode)) { 206 if ((mntpt = getmntpt(*argv)) == 0) { 207 mdev.fspec = *argv; 208 mntpath = strdup("/tmp/df.XXXXXX"); 209 if (mntpath == NULL) { 210 warn("strdup failed"); 211 rv = 1; 212 continue; 213 } 214 mntpt = mkdtemp(mntpath); 215 if (mntpt == NULL) { 216 warn("mkdtemp(\"%s\") failed", mntpath); 217 rv = 1; 218 free(mntpath); 219 continue; 220 } 221 if (mount(fstype, mntpt, MNT_RDONLY, 222 &mdev) != 0) { 223 rv = ufs_df(*argv, &maxwidths) || rv; 224 rmdir(mntpt); 225 free(mntpath); 226 continue; 227 } else if (statfs(mntpt, &statfsbuf) == 0 && 228 statvfs(mntpt, &statvfsbuf) == 0) { 229 statfsbuf.f_mntonname[0] = '\0'; 230 prtstat(&statfsbuf, &statvfsbuf, &maxwidths); 231 } else { 232 warn("%s", *argv); 233 rv = 1; 234 } 235 unmount(mntpt, 0); 236 rmdir(mntpt); 237 free(mntpath); 238 continue; 239 } 240 } else 241 mntpt = *argv; 242 /* 243 * Statfs does not take a `wait' flag, so we cannot 244 * implement nflag here. 245 */ 246 if (statfs(mntpt, &statfsbuf) < 0) { 247 warn("%s", mntpt); 248 rv = 1; 249 continue; 250 } 251 if (statvfs(mntpt, &statvfsbuf) < 0) { 252 warn("%s", mntpt); 253 rv = 1; 254 continue; 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 if (argc == 1) { 268 bzero(&maxwidths, sizeof(maxwidths)); 269 update_maxwidths(&maxwidths, &statfsbuf, &statvfsbuf); 270 } 271 prtstat(&statfsbuf, &statvfsbuf, &maxwidths); 272 } 273 return (rv); 274 } 275 276 char * 277 getmntpt(char *name) 278 { 279 long mntsize, i; 280 struct statfs *mntbuf; 281 struct statvfs *mntvbuf; 282 283 mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT); 284 for (i = 0; i < mntsize; i++) { 285 if (!strcmp(mntbuf[i].f_mntfromname, name)) 286 return (mntbuf[i].f_mntonname); 287 } 288 return (0); 289 } 290 291 /* 292 * Make a pass over the filesystem info in ``mntbuf'' filtering out 293 * filesystem types not in vfslist and possibly re-stating to get 294 * current (not cached) info. Returns the new count of valid statfs bufs. 295 */ 296 long 297 regetmntinfo(struct statfs **mntbufp, struct statvfs **mntvbufp, long mntsize, char **vfslist) 298 { 299 int i, j; 300 struct statfs *mntbuf; 301 struct statvfs *mntvbuf; 302 303 if (vfslist == NULL) 304 return (nflag ? mntsize : getmntvinfo(mntbufp, mntvbufp, MNT_WAIT)); 305 306 mntbuf = *mntbufp; 307 mntvbuf = *mntvbufp; 308 for (j = 0, i = 0; i < mntsize; i++) { 309 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 310 continue; 311 if (!nflag) { 312 statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 313 statvfs(mntbuf[i].f_mntonname,&mntvbuf[j]); 314 } else if (i != j) { 315 mntbuf[j] = mntbuf[i]; 316 mntvbuf[j] = mntvbuf[i]; 317 } 318 j++; 319 } 320 return (j); 321 } 322 323 void 324 prthuman(struct statvfs *vsfsp, int64_t used) 325 { 326 prthumanval(vsfsp->f_blocks * vsfsp->f_bsize); 327 prthumanval(used * vsfsp->f_bsize); 328 prthumanval(vsfsp->f_bavail * vsfsp->f_bsize); 329 } 330 331 void 332 prthumanval(int64_t bytes) 333 { 334 char buf[6]; 335 int flags; 336 337 flags = HN_B | HN_NOSPACE | HN_DECIMAL; 338 if (hflag == UNITS_SI) 339 flags |= HN_DIVISOR_1000; 340 341 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 342 bytes, "", HN_AUTOSCALE, flags); 343 344 printf(" %6s", buf); 345 } 346 347 /* 348 * Convert statfs returned filesystem size into BLOCKSIZE units. 349 * Attempts to avoid overflow for large filesystems. 350 */ 351 static intmax_t 352 fsbtoblk(int64_t num, uint64_t bsize, u_long reqbsize) 353 { 354 if (bsize != 0 && bsize < reqbsize) 355 return (num / (intmax_t)(reqbsize / bsize)); 356 else 357 return (num * (intmax_t)(bsize / reqbsize)); 358 } 359 360 /* 361 * Print out status about a filesystem. 362 */ 363 void 364 prtstat(struct statfs *sfsp, struct statvfs *vsfsp, struct maxwidths *mwp) 365 { 366 static long blocksize; 367 static int headerlen, timesthrough; 368 static const char *header; 369 int64_t used, availblks, inodes; 370 371 if (++timesthrough == 1) { 372 mwp->mntfrom = imax(mwp->mntfrom, strlen("Filesystem")); 373 if (hflag) { 374 header = " Size"; 375 mwp->total = mwp->used = mwp->avail = strlen(header); 376 } else { 377 header = getbsize(&headerlen, &blocksize); 378 mwp->total = imax(mwp->total, headerlen); 379 } 380 mwp->used = imax(mwp->used, strlen("Used")); 381 mwp->avail = imax(mwp->avail, strlen("Avail")); 382 383 printf("%-*s %-*s %*s %*s Capacity", mwp->mntfrom, 384 "Filesystem", mwp->total, header, mwp->used, "Used", 385 mwp->avail, "Avail"); 386 if (iflag) { 387 mwp->iused = imax(mwp->iused, strlen(" iused")); 388 mwp->ifree = imax(mwp->ifree, strlen("ifree")); 389 printf(" %*s %*s %%iused", mwp->iused - 2, 390 "iused", mwp->ifree, "ifree"); 391 } 392 printf(" Mounted on\n"); 393 } 394 printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname); 395 used = vsfsp->f_blocks - vsfsp->f_bfree; 396 availblks = vsfsp->f_bavail + used; 397 if (hflag) { 398 prthuman(vsfsp, used); 399 } else { 400 printf(" %*jd %*jd %*jd", mwp->total, 401 fsbtoblk(vsfsp->f_blocks, vsfsp->f_bsize, blocksize), 402 mwp->used, fsbtoblk(used, vsfsp->f_bsize, blocksize), 403 mwp->avail, fsbtoblk(vsfsp->f_bavail, vsfsp->f_bsize, 404 blocksize)); 405 } 406 printf(" %5.0f%%", 407 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 408 if (iflag) { 409 inodes = vsfsp->f_files; 410 used = inodes - vsfsp->f_ffree; 411 printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used, 412 mwp->ifree, (intmax_t)vsfsp->f_ffree, inodes == 0 ? 100.0 : 413 (double)used / (double)inodes * 100.0); 414 } else 415 printf(" "); 416 printf(" %s\n", sfsp->f_mntonname); 417 } 418 419 /* 420 * Update the maximum field-width information in `mwp' based on 421 * the filesystem specified by `sfsp'. 422 */ 423 void 424 update_maxwidths(struct maxwidths *mwp, struct statfs *sfsp, struct statvfs *vsfsp) 425 { 426 static long blocksize; 427 int dummy; 428 429 if (blocksize == 0) 430 getbsize(&dummy, &blocksize); 431 432 mwp->mntfrom = imax(mwp->mntfrom, strlen(sfsp->f_mntfromname)); 433 mwp->total = imax(mwp->total, quadwidth(fsbtoblk(vsfsp->f_blocks, 434 vsfsp->f_bsize, blocksize))); 435 mwp->used = imax(mwp->used, quadwidth(fsbtoblk(vsfsp->f_blocks - 436 vsfsp->f_bfree, vsfsp->f_bsize, blocksize))); 437 mwp->avail = imax(mwp->avail, quadwidth(fsbtoblk(vsfsp->f_bavail, 438 vsfsp->f_bsize, blocksize))); 439 mwp->iused = imax(mwp->iused, quadwidth(vsfsp->f_files - 440 vsfsp->f_ffree)); 441 mwp->ifree = imax(mwp->ifree, quadwidth(vsfsp->f_ffree)); 442 } 443 444 /* Return the width in characters of the specified long. */ 445 int 446 quadwidth(int64_t val) 447 { 448 int len; 449 450 len = 0; 451 /* Negative or zero values require one extra digit. */ 452 if (val <= 0) { 453 val = -val; 454 len++; 455 } 456 while (val > 0) { 457 len++; 458 val /= 10; 459 } 460 return (len); 461 } 462 463 /* 464 * This code constitutes the pre-system call Berkeley df code for extracting 465 * information from filesystem superblocks. 466 */ 467 468 union { 469 struct fs iu_fs; 470 char dummy[SBSIZE]; 471 } sb; 472 #define sblock sb.iu_fs 473 474 int rfd; 475 476 int 477 ufs_df(char *file, struct maxwidths *mwp) 478 { 479 struct statfs statfsbuf; 480 struct statvfs statvfsbuf; 481 struct statfs *sfsp; 482 struct statvfs *vsfsp; 483 const char *mntpt; 484 static int synced; 485 486 if (synced++ == 0) 487 sync(); 488 489 if ((rfd = open(file, O_RDONLY)) < 0) { 490 warn("%s", file); 491 return (1); 492 } 493 if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) { 494 close(rfd); 495 return (1); 496 } 497 sfsp = &statfsbuf; 498 vsfsp = &statvfsbuf; 499 sfsp->f_type = 1; 500 strcpy(sfsp->f_fstypename, "ufs"); 501 sfsp->f_flags = 0; 502 sfsp->f_bsize = vsfsp->f_bsize = sblock.fs_fsize; 503 sfsp->f_iosize = vsfsp->f_frsize = sblock.fs_bsize; 504 sfsp->f_blocks = vsfsp->f_blocks = sblock.fs_dsize; 505 sfsp->f_bfree = vsfsp->f_bfree = 506 sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + 507 sblock.fs_cstotal.cs_nffree; 508 sfsp->f_bavail = vsfsp->f_bavail = freespace(&sblock, sblock.fs_minfree); 509 sfsp->f_files = vsfsp->f_files = sblock.fs_ncg * sblock.fs_ipg; 510 sfsp->f_ffree = vsfsp->f_ffree = sblock.fs_cstotal.cs_nifree; 511 sfsp->f_fsid.val[0] = 0; 512 sfsp->f_fsid.val[1] = 0; 513 if ((mntpt = getmntpt(file)) == 0) 514 mntpt = ""; 515 memmove(&sfsp->f_mntonname[0], mntpt, (size_t)MNAMELEN); 516 memmove(&sfsp->f_mntfromname[0], file, (size_t)MNAMELEN); 517 prtstat(sfsp, vsfsp, mwp); 518 close(rfd); 519 return (0); 520 } 521 522 int 523 bread(off_t off, void *buf, int cnt) 524 { 525 ssize_t nr; 526 527 lseek(rfd, off, SEEK_SET); 528 if ((nr = read(rfd, buf, (size_t)cnt)) != (ssize_t)cnt) { 529 /* Probably a dismounted disk if errno == EIO. */ 530 if (errno != EIO) 531 fprintf(stderr, "\ndf: %lld: %s\n", 532 (long long)off, strerror(nr > 0 ? EIO : errno)); 533 return (0); 534 } 535 return (1); 536 } 537 538 void 539 usage(void) 540 { 541 542 fprintf(stderr, 543 "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n"); 544 exit(EX_USAGE); 545 } 546 547 char * 548 makenetvfslist(void) 549 { 550 char *str, *strptr, **listptr; 551 int mib[3], maxvfsconf, cnt=0, i; 552 size_t miblen; 553 struct ovfsconf *ptr; 554 555 mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM; 556 miblen=sizeof(maxvfsconf); 557 if (sysctl(mib, (unsigned int)(sizeof(mib) / sizeof(mib[0])), 558 &maxvfsconf, &miblen, NULL, 0)) { 559 warnx("sysctl failed"); 560 return (NULL); 561 } 562 563 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 564 warnx("malloc failed"); 565 return (NULL); 566 } 567 568 for (ptr = getvfsent(); ptr; ptr = getvfsent()) 569 if (ptr->vfc_flags & VFCF_NETWORK) { 570 listptr[cnt++] = strdup(ptr->vfc_name); 571 if (listptr[cnt-1] == NULL) { 572 warnx("malloc failed"); 573 return (NULL); 574 } 575 } 576 577 if (cnt == 0 || 578 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 579 if (cnt > 0) 580 warnx("malloc failed"); 581 free(listptr); 582 return (NULL); 583 } 584 585 *str = 'n'; *(str + 1) = 'o'; 586 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 587 strncpy(strptr, listptr[i], 32); 588 strptr += strlen(listptr[i]); 589 *strptr = ','; 590 free(listptr[i]); 591 } 592 *(--strptr) = '\0'; 593 594 free(listptr); 595 return (str); 596 } 597