Home | History | Annotate | Line # | Download | only in df
df.c revision 1.26
      1 /*	$NetBSD: df.c,v 1.26 1998/04/08 23:16:37 fair 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 #ifndef lint
     43 __COPYRIGHT(
     44 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
     45 	The Regents of the University of California.  All rights reserved.\n");
     46 #endif /* not lint */
     47 
     48 #ifndef lint
     49 #if 0
     50 static char sccsid[] = "@(#)df.c	8.7 (Berkeley) 4/2/94";
     51 #else
     52 __RCSID("$NetBSD: df.c,v 1.26 1998/04/08 23:16:37 fair Exp $");
     53 #endif
     54 #endif /* not lint */
     55 
     56 #include <sys/param.h>
     57 #include <sys/stat.h>
     58 #include <sys/mount.h>
     59 #include <ufs/ufs/ufsmount.h>
     60 
     61 #include <err.h>
     62 #include <errno.h>
     63 #include <fcntl.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 
     69 extern char * strpct __P((u_long num, u_long denom, u_int digits));
     70 
     71 int	 main __P((int, char *[]));
     72 int	 bread __P((off_t, void *, int));
     73 char	*getmntpt __P((char *));
     74 void	 prtstat __P((struct statfs *, int));
     75 int	 ufs_df __P((char *, struct statfs *));
     76 int	 selected __P((const char *));
     77 void	 maketypelist __P((char *));
     78 long	 regetmntinfo __P((struct statfs **, long));
     79 void	 usage __P((void));
     80 
     81 int	iflag, kflag, lflag, nflag;
     82 char	**typelist = NULL;
     83 struct	ufs_args mdev;
     84 
     85 int
     86 main(argc, argv)
     87 	int argc;
     88 	char *argv[];
     89 {
     90 	struct stat stbuf;
     91 	struct statfs *mntbuf;
     92 	long mntsize;
     93 	int ch, i, maxwidth, width;
     94 	char *mntpt;
     95 
     96 	while ((ch = getopt(argc, argv, "iklnt:")) != -1)
     97 		switch (ch) {
     98 		case 'i':
     99 			iflag = 1;
    100 			break;
    101 		case 'k':
    102 			kflag = 1;
    103 			break;
    104 		case 'l':
    105 			lflag = 1;
    106 			break;
    107 		case 'n':
    108 			nflag = 1;
    109 			break;
    110 		case 't':
    111 			if (typelist != NULL)
    112 				errx(1, "only one -t option may be specified.");
    113 			maketypelist(optarg);
    114 			break;
    115 		case '?':
    116 		default:
    117 			usage();
    118 		}
    119 	argc -= optind;
    120 	argv += optind;
    121 
    122 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    123 	if (mntsize == 0)
    124 	        err(1, "retrieving information on mounted file systems");
    125 
    126 	if (!*argv) {
    127 		mntsize = regetmntinfo(&mntbuf, mntsize);
    128 	} else {
    129 		mntbuf = malloc(argc * sizeof(struct statfs));
    130 		mntsize = 0;
    131 		for (; *argv; argv++) {
    132 			if (stat(*argv, &stbuf) < 0) {
    133 				if ((mntpt = getmntpt(*argv)) == 0) {
    134 					warn("%s", *argv);
    135 					continue;
    136 				}
    137 			} else if (S_ISCHR(stbuf.st_mode)) {
    138 				if (!ufs_df(*argv, &mntbuf[mntsize]))
    139 					++mntsize;
    140 				continue;
    141 			} else if (S_ISBLK(stbuf.st_mode)) {
    142 				if ((mntpt = getmntpt(*argv)) == 0) {
    143 					mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
    144 					mdev.fspec = *argv;
    145 					if (mkdir(mntpt, DEFFILEMODE) != 0) {
    146 						warn("%s", mntpt);
    147 						continue;
    148 					}
    149 					if (mount(MOUNT_FFS, mntpt, MNT_RDONLY,
    150 					    &mdev) != 0) {
    151 						(void)rmdir(mntpt);
    152 						if (!ufs_df(*argv, &mntbuf[mntsize]))
    153 							++mntsize;
    154 						continue;
    155 					} else if (!statfs(mntpt, &mntbuf[mntsize])) {
    156 						mntbuf[mntsize].f_mntonname[0] = '\0';
    157 						++mntsize;
    158 					} else
    159 						warn("%s", *argv);
    160 					(void)unmount(mntpt, 0);
    161 					(void)rmdir(mntpt);
    162 					continue;
    163 				}
    164 			} else
    165 				mntpt = *argv;
    166 			/*
    167 			 * Statfs does not take a `wait' flag, so we cannot
    168 			 * implement nflag here.
    169 			 */
    170 			if (!statfs(mntpt, &mntbuf[mntsize]))
    171 				if (lflag &&
    172 				    (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0)
    173 					warnx("Warning: %s is not a local %s",
    174 					    *argv, "file system");
    175 				else if
    176 				    (!selected(mntbuf[mntsize].f_fstypename))
    177 					warnx("Warning: %s mounted as a %s %s",
    178 					    *argv,
    179 					    mntbuf[mntsize].f_fstypename,
    180 					    "file system");
    181 				else
    182 					++mntsize;
    183 			else
    184 				warn("%s", *argv);
    185 		}
    186 	}
    187 
    188 	maxwidth = 0;
    189 	for (i = 0; i < mntsize; i++) {
    190 		width = strlen(mntbuf[i].f_mntfromname);
    191 		if (width > maxwidth)
    192 			maxwidth = width;
    193 	}
    194 	for (i = 0; i < mntsize; i++)
    195 		prtstat(&mntbuf[i], maxwidth);
    196 	exit(0);
    197 }
    198 
    199 char *
    200 getmntpt(name)
    201 	char *name;
    202 {
    203 	long mntsize, i;
    204 	struct statfs *mntbuf;
    205 
    206 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    207 	for (i = 0; i < mntsize; i++) {
    208 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    209 			return (mntbuf[i].f_mntonname);
    210 	}
    211 	return (0);
    212 }
    213 
    214 static enum { IN_LIST, NOT_IN_LIST } which;
    215 
    216 int
    217 selected(type)
    218 	const char *type;
    219 {
    220 	char **av;
    221 
    222 	/* If no type specified, it's always selected. */
    223 	if (typelist == NULL)
    224 		return (1);
    225 	for (av = typelist; *av != NULL; ++av)
    226 		if (!strncmp(type, *av, MFSNAMELEN))
    227 			return (which == IN_LIST ? 1 : 0);
    228 	return (which == IN_LIST ? 0 : 1);
    229 }
    230 
    231 void
    232 maketypelist(fslist)
    233 	char *fslist;
    234 {
    235 	int i;
    236 	char *nextcp, **av;
    237 
    238 	if ((fslist == NULL) || (fslist[0] == '\0'))
    239 		errx(1, "empty type list");
    240 
    241 	/*
    242 	 * XXX
    243 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    244 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    245 	 */
    246 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    247 		fslist += 2;
    248 		which = NOT_IN_LIST;
    249 	} else
    250 		which = IN_LIST;
    251 
    252 	/* Count the number of types. */
    253 	for (i = 1, nextcp = fslist;
    254 	    (nextcp = strchr(nextcp, ',')) != NULL; i++)
    255 		++nextcp;
    256 
    257 	/* Build an array of that many types. */
    258 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    259 		err(1, "can't allocate type array");
    260 	av[0] = fslist;
    261 	for (i = 1, nextcp = fslist;
    262 	    (nextcp = strchr(nextcp, ',')) != NULL; i++) {
    263 		*nextcp = '\0';
    264 		av[i] = ++nextcp;
    265 	}
    266 	/* Terminate the array. */
    267 	av[i] = NULL;
    268 }
    269 
    270 /*
    271  * Make a pass over the filesystem info in ``mntbuf'' filtering out
    272  * filesystem types not in ``fsmask'' and possibly re-stating to get
    273  * current (not cached) info.  Returns the new count of valid statfs bufs.
    274  */
    275 long
    276 regetmntinfo(mntbufp, mntsize)
    277 	struct statfs **mntbufp;
    278 	long mntsize;
    279 {
    280 	int i, j;
    281 	struct statfs *mntbuf;
    282 
    283 	if (!lflag && typelist == NULL)
    284 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
    285 
    286 	mntbuf = *mntbufp;
    287 	j = 0;
    288 	for (i = 0; i < mntsize; i++) {
    289 		if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
    290 			continue;
    291 		if (!selected(mntbuf[i].f_fstypename))
    292 			continue;
    293 		if (nflag)
    294 			mntbuf[j] = mntbuf[i];
    295 		else
    296 			(void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
    297 		j++;
    298 	}
    299 	return (j);
    300 }
    301 
    302 /*
    303  * Convert statfs returned filesystem size into BLOCKSIZE units.
    304  * Attempts to avoid overflow for large filesystems.
    305  */
    306 #define fsbtoblk(num, fsbs, bs) \
    307 	(((fsbs) != 0 && (fsbs) < (bs)) ? \
    308 		(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
    309 
    310 /*
    311  * Print out status about a filesystem.
    312  */
    313 void
    314 prtstat(sfsp, maxwidth)
    315 	struct statfs *sfsp;
    316 	int maxwidth;
    317 {
    318 	static long blocksize;
    319 	static int headerlen, timesthrough;
    320 	static char *header;
    321 	long used, availblks, inodes;
    322 	static char     *full = "100%";
    323 
    324 	if (maxwidth < 11)
    325 		maxwidth = 11;
    326 	if (++timesthrough == 1) {
    327 		if (kflag) {
    328 			blocksize = 1024;
    329 			header = "1K-blocks";
    330 			headerlen = strlen(header);
    331 		} else
    332 			header = getbsize(&headerlen, &blocksize);
    333 		(void)printf("%-*.*s %s     Used    Avail Capacity",
    334 		    maxwidth, maxwidth, "Filesystem", header);
    335 		if (iflag)
    336 			(void)printf(" iused   ifree  %%iused");
    337 		(void)printf("  Mounted on\n");
    338 	}
    339 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
    340 	used = sfsp->f_blocks - sfsp->f_bfree;
    341 	availblks = sfsp->f_bavail + used;
    342 	(void)printf(" %*ld %8ld %8ld", headerlen,
    343 	    fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
    344 	    fsbtoblk(used, sfsp->f_bsize, blocksize),
    345 	    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
    346 	(void)printf(" %6s",
    347 	    availblks == 0 ? full : strpct((ulong)used, (ulong)availblks, 0));
    348 	if (iflag) {
    349 		inodes = sfsp->f_files;
    350 		used = inodes - sfsp->f_ffree;
    351 		(void)printf(" %7ld %7ld %6s ", used, sfsp->f_ffree,
    352 		   inodes == 0 ? full : strpct((ulong)used, (ulong)inodes, 0));
    353 	} else
    354 		(void)printf("  ");
    355 	(void)printf("  %s\n", sfsp->f_mntonname);
    356 }
    357 
    358 /*
    359  * This code constitutes the pre-system call Berkeley df code for extracting
    360  * information from filesystem superblocks.
    361  */
    362 #include <ufs/ufs/dinode.h>
    363 #include <ufs/ffs/fs.h>
    364 #include <errno.h>
    365 #include <fstab.h>
    366 
    367 union {
    368 	struct fs iu_fs;
    369 	char dummy[SBSIZE];
    370 } sb;
    371 #define sblock sb.iu_fs
    372 
    373 int	rfd;
    374 
    375 int
    376 ufs_df(file, sfsp)
    377 	char *file;
    378 	struct statfs *sfsp;
    379 {
    380 	char *mntpt;
    381 	static int synced;
    382 
    383 	if (synced++ == 0)
    384 		sync();
    385 
    386 	if ((rfd = open(file, O_RDONLY)) < 0) {
    387 		warn("%s", file);
    388 		return (-1);
    389 	}
    390 	if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
    391 		(void)close(rfd);
    392 		return (-1);
    393 	}
    394 	sfsp->f_type = 0;
    395 	sfsp->f_flags = 0;
    396 	sfsp->f_bsize = sblock.fs_fsize;
    397 	sfsp->f_iosize = sblock.fs_bsize;
    398 	sfsp->f_blocks = sblock.fs_dsize;
    399 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
    400 		sblock.fs_cstotal.cs_nffree;
    401 	sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
    402 		(sblock.fs_dsize - sfsp->f_bfree);
    403 	if (sfsp->f_bavail < 0)
    404 		sfsp->f_bavail = 0;
    405 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
    406 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
    407 	sfsp->f_fsid.val[0] = 0;
    408 	sfsp->f_fsid.val[1] = 0;
    409 	if ((mntpt = getmntpt(file)) == 0)
    410 		mntpt = "";
    411 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
    412 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
    413 	strncpy(sfsp->f_fstypename, MOUNT_FFS, MFSNAMELEN);
    414 	(void)close(rfd);
    415 	return (0);
    416 }
    417 
    418 int
    419 bread(off, buf, cnt)
    420 	off_t off;
    421 	void *buf;
    422 	int cnt;
    423 {
    424 	int nr;
    425 
    426 	(void)lseek(rfd, off, SEEK_SET);
    427 	if ((nr = read(rfd, buf, cnt)) != cnt) {
    428 		/* Probably a dismounted disk if errno == EIO. */
    429 		if (errno != EIO)
    430 			(void)fprintf(stderr, "\ndf: %qd: %s\n",
    431 			    (long long)off, strerror(nr > 0 ? EIO : errno));
    432 		return (0);
    433 	}
    434 	return (1);
    435 }
    436 
    437 void
    438 usage()
    439 {
    440 	(void)fprintf(stderr, "usage: df [-ikln] [-t type] [file | file_system ...]\n");
    441 	exit(1);
    442 }
    443