1 /* $NetBSD: df.c,v 1.85 2009/01/25 14:18:21 lukem Exp $ */ 2 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 #include <sys/cdefs.h> 38 #ifndef lint 39 __COPYRIGHT( 40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\ 41 The Regents of the University of California. All rights reserved."); 42 #endif /* not lint */ 43 44 #ifndef lint 45 #if 0 46 static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94"; 47 #else 48 __RCSID("$NetBSD: df.c,v 1.85 2009/01/25 14:18:21 lukem Exp $"); 49 #endif 50 #endif /* not lint */ 51 52 #include <sys/param.h> 53 #include <sys/stat.h> 54 #include <sys/mount.h> 55 56 #include <assert.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <locale.h> 61 #include <util.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 extern char *strpct(u_long, u_long, u_int); 68 69 int main(int, char *[]); 70 int bread(off_t, void *, int); 71 char *getmntpt(char *); 72 void prtstat(struct statvfs *, int); 73 int selected(const char *, size_t); 74 void maketypelist(char *); 75 long regetmntinfo(struct statvfs **, long); 76 void usage(void); 77 void prthumanval(int64_t, const char *); 78 void prthuman(struct statvfs *, int64_t, int64_t); 79 const char * 80 strpct64(uint64_t, uint64_t, u_int); 81 82 int aflag, gflag, hflag, iflag, lflag, nflag, Pflag; 83 long usize = 0; 84 char **typelist = NULL; 85 86 int 87 main(int argc, char *argv[]) 88 { 89 struct stat stbuf; 90 struct statvfs *mntbuf; 91 long mntsize; 92 int ch, i, maxwidth, width; 93 char *mntpt; 94 95 setprogname(argv[0]); 96 (void)setlocale(LC_ALL, ""); 97 98 while ((ch = getopt(argc, argv, "aGghiklmnPt:")) != -1) 99 switch (ch) { 100 case 'a': 101 aflag = 1; 102 break; 103 case 'g': 104 hflag = 0; 105 usize = 1024 * 1024 * 1024; 106 break; 107 case 'G': 108 gflag = 1; 109 break; 110 case 'h': 111 hflag = 1; 112 usize = 0; 113 break; 114 case 'i': 115 iflag = 1; 116 break; 117 case 'k': 118 hflag = 0; 119 usize = 1024; 120 break; 121 case 'l': 122 lflag = 1; 123 break; 124 case 'm': 125 hflag = 0; 126 usize = 1024 * 1024; 127 break; 128 case 'n': 129 nflag = 1; 130 break; 131 case 'P': 132 Pflag = 1; 133 break; 134 case 't': 135 if (typelist != NULL) 136 errx(EXIT_FAILURE, 137 "only one -t option may be specified."); 138 maketypelist(optarg); 139 break; 140 case '?': 141 default: 142 usage(); 143 } 144 145 if (gflag && (Pflag || iflag)) 146 errx(EXIT_FAILURE, 147 "only one of -G and -P or -i may be specified"); 148 if (Pflag && iflag) 149 errx(EXIT_FAILURE, 150 "only one of -P and -i may be specified"); 151 #if 0 152 /* 153 * The block size cannot be checked until after getbsize() is called. 154 */ 155 if (Pflag && (hflag || (usize != 1024 && usize != 512))) 156 errx(EXIT_FAILURE, 157 "non-standard block size incompatible with -P"); 158 #endif 159 argc -= optind; 160 argv += optind; 161 162 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 163 if (mntsize == 0) 164 err(EXIT_FAILURE, 165 "retrieving information on mounted file systems"); 166 167 if (*argv == NULL) { 168 mntsize = regetmntinfo(&mntbuf, mntsize); 169 } else { 170 if ((mntbuf = malloc(argc * sizeof(*mntbuf))) == NULL) 171 err(EXIT_FAILURE, "can't allocate statvfs array"); 172 mntsize = 0; 173 for (/*EMPTY*/; *argv != NULL; argv++) { 174 if (stat(*argv, &stbuf) < 0) { 175 if ((mntpt = getmntpt(*argv)) == 0) { 176 warn("%s", *argv); 177 continue; 178 } 179 } else if (S_ISBLK(stbuf.st_mode)) { 180 if ((mntpt = getmntpt(*argv)) == 0) 181 mntpt = *argv; 182 } else 183 mntpt = *argv; 184 /* 185 * Statfs does not take a `wait' flag, so we cannot 186 * implement nflag here. 187 */ 188 if (!statvfs(mntpt, &mntbuf[mntsize])) 189 if (lflag && 190 (mntbuf[mntsize].f_flag & MNT_LOCAL) == 0) 191 warnx("Warning: %s is not a local %s", 192 *argv, "file system"); 193 else if 194 (!selected(mntbuf[mntsize].f_fstypename, 195 sizeof(mntbuf[mntsize].f_fstypename))) 196 warnx("Warning: %s mounted as a %s %s", 197 *argv, 198 mntbuf[mntsize].f_fstypename, 199 "file system"); 200 else 201 ++mntsize; 202 else 203 warn("%s", *argv); 204 } 205 } 206 207 maxwidth = 0; 208 for (i = 0; i < mntsize; i++) { 209 width = (int)strlen(mntbuf[i].f_mntfromname); 210 if (width > maxwidth) 211 maxwidth = width; 212 } 213 for (i = 0; i < mntsize; i++) 214 prtstat(&mntbuf[i], maxwidth); 215 exit(0); 216 /* NOTREACHED */ 217 } 218 219 char * 220 getmntpt(char *name) 221 { 222 long mntsize, i; 223 struct statvfs *mntbuf; 224 225 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 226 for (i = 0; i < mntsize; i++) { 227 if (!strcmp(mntbuf[i].f_mntfromname, name)) 228 return (mntbuf[i].f_mntonname); 229 } 230 return (0); 231 } 232 233 static enum { IN_LIST, NOT_IN_LIST } which; 234 235 int 236 selected(const char *type, size_t len) 237 { 238 char **av; 239 240 /* If no type specified, it's always selected. */ 241 if (typelist == NULL) 242 return (1); 243 for (av = typelist; *av != NULL; ++av) 244 if (!strncmp(type, *av, len)) 245 return (which == IN_LIST ? 1 : 0); 246 return (which == IN_LIST ? 0 : 1); 247 } 248 249 void 250 maketypelist(char *fslist) 251 { 252 int i; 253 char *nextcp, **av; 254 255 if ((fslist == NULL) || (fslist[0] == '\0')) 256 errx(EXIT_FAILURE, "empty type list"); 257 258 /* 259 * XXX 260 * Note: the syntax is "noxxx,yyy" for no xxx's and 261 * no yyy's, not the more intuitive "noyyy,noyyy". 262 */ 263 if (fslist[0] == 'n' && fslist[1] == 'o') { 264 fslist += 2; 265 which = NOT_IN_LIST; 266 } else 267 which = IN_LIST; 268 269 /* Count the number of types. */ 270 for (i = 1, nextcp = fslist; 271 (nextcp = strchr(nextcp, ',')) != NULL; i++) 272 ++nextcp; 273 274 /* Build an array of that many types. */ 275 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL) 276 err(EXIT_FAILURE, "can't allocate type array"); 277 av[0] = fslist; 278 for (i = 1, nextcp = fslist; 279 (nextcp = strchr(nextcp, ',')) != NULL; i++) { 280 *nextcp = '\0'; 281 av[i] = ++nextcp; 282 } 283 /* Terminate the array. */ 284 av[i] = NULL; 285 } 286 287 /* 288 * Make a pass over the filesystem info in ``mntbuf'' filtering out 289 * filesystem types not in ``fsmask'' and possibly re-stating to get 290 * current (not cached) info. Returns the new count of valid statvfs bufs. 291 */ 292 long 293 regetmntinfo(struct statvfs **mntbufp, long mntsize) 294 { 295 int i, j; 296 struct statvfs *mntbuf; 297 298 if (!lflag && typelist == NULL && aflag) 299 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 300 301 mntbuf = *mntbufp; 302 j = 0; 303 for (i = 0; i < mntsize; i++) { 304 if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0) 305 continue; 306 if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0) 307 continue; 308 if (!selected(mntbuf[i].f_fstypename, 309 sizeof(mntbuf[i].f_fstypename))) 310 continue; 311 if (nflag) 312 mntbuf[j] = mntbuf[i]; 313 else { 314 struct statvfs layerbuf = mntbuf[i]; 315 (void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]); 316 /* 317 * If the FS name changed, then new data is for 318 * a different layer and we don't want it. 319 */ 320 if (memcmp(layerbuf.f_mntfromname, 321 mntbuf[j].f_mntfromname, MNAMELEN)) 322 mntbuf[j] = layerbuf; 323 } 324 j++; 325 } 326 return (j); 327 } 328 329 void 330 prthumanval(int64_t bytes, const char *pad) 331 { 332 char buf[6]; 333 334 (void)humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 335 bytes, "", HN_AUTOSCALE, 336 HN_B | HN_NOSPACE | HN_DECIMAL); 337 338 (void)printf("%s %6s", pad, buf); 339 } 340 341 void 342 prthuman(struct statvfs *sfsp, int64_t used, int64_t bavail) 343 { 344 345 prthumanval((int64_t)(sfsp->f_blocks * sfsp->f_frsize), " "); 346 prthumanval((int64_t)(used * sfsp->f_frsize), " "); 347 prthumanval((int64_t)(bavail * sfsp->f_frsize), " "); 348 } 349 350 /* 351 * Convert statvfs returned filesystem size into BLOCKSIZE units. 352 * Attempts to avoid overflow for large filesystems. 353 */ 354 #define fsbtoblk(num, fsbs, bs) \ 355 (((fsbs) != 0 && (uint64_t)(fsbs) < (uint64_t)(bs)) ? \ 356 (int64_t)(num) / (int64_t)((bs) / (fsbs)) : \ 357 (int64_t)(num) * (int64_t)((fsbs) / (bs))) 358 359 /* 360 * Print out status about a filesystem. 361 */ 362 void 363 prtstat(struct statvfs *sfsp, int maxwidth) 364 { 365 static long blocksize; 366 static int headerlen, timesthrough; 367 static const char *header; 368 static const char full[] = "100%"; 369 static const char empty[] = " 0%"; 370 int64_t used, availblks, inodes; 371 int64_t bavail; 372 373 if (gflag) { 374 /* 375 * From SunOS-5.6: 376 * 377 * /var (/dev/dsk/c0t0d0s3 ): 8192 block size 1024 frag size 378 * 984242 total blocks 860692 free blocks 859708 available 249984 total files 379 * 248691 free files 8388611 filesys id 380 * ufs fstype 0x00000004 flag 255 filename length 381 * 382 */ 383 (void)printf("%10s (%-12s): %7ld block size %12ld frag size\n", 384 sfsp->f_mntonname, sfsp->f_mntfromname, 385 sfsp->f_iosize, /* On UFS/FFS systems this is 386 * also called the "optimal 387 * transfer block size" but it 388 * is of course the file 389 * system's block size too. 390 */ 391 sfsp->f_bsize); /* not so surprisingly the 392 * "fundamental file system 393 * block size" is the frag 394 * size. 395 */ 396 (void)printf("%10" PRId64 " total blocks %10" PRId64 397 " free blocks %10" PRId64 " available\n", 398 (uint64_t)sfsp->f_blocks, (uint64_t)sfsp->f_bfree, 399 (uint64_t)sfsp->f_bavail); 400 (void)printf("%10" PRId64 " total files %10" PRId64 401 " free files %12lx filesys id\n", 402 (uint64_t)sfsp->f_ffree, (uint64_t)sfsp->f_files, 403 sfsp->f_fsid); 404 (void)printf("%10s fstype %#15lx flag %17ld filename " 405 "length\n", sfsp->f_fstypename, sfsp->f_flag, 406 sfsp->f_namemax); 407 (void)printf("%10lu owner %17" PRId64 " syncwrites %12" PRId64 408 " asyncwrites\n\n", (unsigned long)sfsp->f_owner, 409 sfsp->f_syncwrites, sfsp->f_asyncwrites); 410 411 /* 412 * a concession by the structured programming police to the 413 * indentation police.... 414 */ 415 return; 416 } 417 if (maxwidth < 12) 418 maxwidth = 12; 419 if (++timesthrough == 1) { 420 switch (blocksize = usize) { 421 case 1024: 422 header = Pflag ? "1024-blocks" : "1K-blocks"; 423 headerlen = (int)strlen(header); 424 break; 425 case 1024 * 1024: 426 header = "1M-blocks"; 427 headerlen = (int)strlen(header); 428 break; 429 case 1024 * 1024 * 1024: 430 header = "1G-blocks"; 431 headerlen = (int)strlen(header); 432 break; 433 default: 434 if (hflag) { 435 header = "Size"; 436 headerlen = (int)strlen(header); 437 } else 438 header = getbsize(&headerlen, &blocksize); 439 break; 440 } 441 if (Pflag) { 442 /* 443 * either: 444 * "Filesystem 1024-blocks Used Available Capacity Mounted on\n" 445 * or: 446 * "Filesystem 512-blocks Used Available Capacity Mounted on\n" 447 */ 448 if (blocksize != 1024 && blocksize != 512) 449 errx(EXIT_FAILURE, 450 "non-standard block size incompatible with -P"); 451 (void)printf("Filesystem %s Used Available Capacity " 452 "Mounted on\n", header); 453 } else { 454 (void)printf("%-*.*s %s Used Avail %%Cap", 455 maxwidth - (headerlen - 9), 456 maxwidth - (headerlen - 9), 457 "Filesystem", header); 458 if (iflag) 459 (void)printf(" iUsed iAvail %%iCap"); 460 (void)printf(" Mounted on\n"); 461 } 462 } 463 used = sfsp->f_blocks - sfsp->f_bfree; 464 bavail = sfsp->f_bfree - sfsp->f_bresvd; 465 availblks = bavail + used; 466 if (Pflag) { 467 assert(hflag == 0); 468 assert(blocksize > 0); 469 /* 470 * "%s %d %d %d %s %s\n", <file system name>, <total space>, 471 * <space used>, <space free>, <percentage used>, 472 * <file system root> 473 */ 474 (void)printf("%s %" PRId64 " %" PRId64 " %" PRId64 " %s %s\n", 475 sfsp->f_mntfromname, 476 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize), 477 fsbtoblk(used, sfsp->f_bsize, blocksize), 478 fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize), 479 availblks == 0 ? full : strpct64((uint64_t) used, 480 (uint64_t) availblks, 0), sfsp->f_mntonname); 481 /* 482 * another concession by the structured programming police to 483 * the indentation police.... 484 * 485 * Note iflag cannot be set when Pflag is set. 486 */ 487 return; 488 } 489 490 (void)printf("%-*.*s ", maxwidth, maxwidth, sfsp->f_mntfromname); 491 492 if (hflag) 493 prthuman(sfsp, used, bavail); 494 else 495 (void)printf("%10" PRId64 " %10" PRId64 " %10" PRId64, 496 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize), 497 fsbtoblk(used, sfsp->f_frsize, blocksize), 498 fsbtoblk(bavail, sfsp->f_frsize, blocksize)); 499 (void)printf(" %4s", 500 availblks == 0 ? full : 501 /* We know that these values are never negative */ 502 strpct64((uint64_t)used, (uint64_t)availblks, 0)); 503 if (iflag) { 504 inodes = sfsp->f_files; 505 used = inodes - sfsp->f_ffree; 506 (void)printf(" %8jd %8jd %4s", 507 (intmax_t)used, (intmax_t)sfsp->f_ffree, 508 inodes == 0 ? (used == 0 ? empty : full) : 509 strpct64((uint64_t)used, (uint64_t)inodes, 0)); 510 } 511 (void)printf(" %s\n", sfsp->f_mntonname); 512 } 513 514 void 515 usage(void) 516 { 517 518 (void)fprintf(stderr, 519 "Usage: %s [-aGgln] [-hkm|-ihkm|-Pk] [-t type] [file | " 520 "file_system ...]\n", 521 getprogname()); 522 exit(1); 523 /* NOTREACHED */ 524 } 525 526 const char * 527 strpct64(uint64_t numerator, uint64_t denominator, u_int digits) 528 { 529 530 while (denominator > ULONG_MAX) { 531 numerator >>= 1; 532 denominator >>= 1; 533 } 534 return (strpct((u_long)numerator, (u_long)denominator, digits)); 535 } 536