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