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