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