xref: /freebsd/bin/df/df.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*
2  * Copyright (c) 1980, 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 static const char copyright[] =
41 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
42 	The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)df.c	8.9 (Berkeley) 5/8/95";
48 #else
49 static const char rcsid[] =
50 	"$Id: df.c,v 1.18 1997/10/12 13:55:43 joerg Exp $";
51 #endif
52 #endif /* not lint */
53 
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 #include <sys/mount.h>
57 #include <ufs/ufs/ufsmount.h>
58 
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 int	  checkvfsname __P((const char *, char **));
68 char	**makevfslist __P((char *));
69 long	  regetmntinfo __P((struct statfs **, long, char **));
70 int	  bread __P((off_t, void *, int));
71 char	 *getmntpt __P((char *));
72 void	  prtstat __P((struct statfs *, int));
73 int	  ufs_df __P((char *, int));
74 void	  usage __P((void));
75 
76 int	iflag, nflag;
77 struct	ufs_args mdev;
78 
79 int
80 main(argc, argv)
81 	int argc;
82 	char *argv[];
83 {
84 	struct stat stbuf;
85 	struct statfs statfsbuf, *mntbuf;
86 	long mntsize;
87 	int ch, err, i, maxwidth, rv, width;
88 	char *mntpt, **vfslist;
89 
90 	vfslist = NULL;
91 	while ((ch = getopt(argc, argv, "iknt:")) != -1)
92 		switch (ch) {
93 		case 'i':
94 			iflag = 1;
95 			break;
96 		case 'k':
97 			putenv("BLOCKSIZE=1k");
98 			break;
99 		case 'n':
100 			nflag = 1;
101 			break;
102 		case 't':
103 			if (vfslist != NULL)
104 				errx(1, "only one -t option may be specified.");
105 			vfslist = makevfslist(optarg);
106 			break;
107 		case '?':
108 		default:
109 			usage();
110 		}
111 	argc -= optind;
112 	argv += optind;
113 
114 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
115 	maxwidth = 0;
116 	for (i = 0; i < mntsize; i++) {
117 		width = strlen(mntbuf[i].f_mntfromname);
118 		if (width > maxwidth)
119 			maxwidth = width;
120 	}
121 
122 	rv = 0;
123 	if (!*argv) {
124 		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
125 		if (vfslist != NULL) {
126 			maxwidth = 0;
127 			for (i = 0; i < mntsize; i++) {
128 				width = strlen(mntbuf[i].f_mntfromname);
129 				if (width > maxwidth)
130 					maxwidth = width;
131 			}
132 		}
133 		for (i = 0; i < mntsize; i++)
134 			prtstat(&mntbuf[i], maxwidth);
135 		exit(rv);
136 	}
137 
138 	for (; *argv; argv++) {
139 		if (stat(*argv, &stbuf) < 0) {
140 			err = errno;
141 			if ((mntpt = getmntpt(*argv)) == 0) {
142 				warn("%s", *argv);
143 				rv = 1;
144 				continue;
145 			}
146 		} else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
147 			rv = ufs_df(*argv, maxwidth) || rv;
148 			continue;
149 		} else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
150 			if ((mntpt = getmntpt(*argv)) == 0) {
151 				mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
152 				mdev.fspec = *argv;
153 				if (mkdir(mntpt, DEFFILEMODE) != 0) {
154 					warn("%s", mntpt);
155 					rv = 1;
156 					continue;
157 				}
158 				if (mount("ufs", mntpt, MNT_RDONLY,
159 				    &mdev) != 0) {
160 					rv = ufs_df(*argv, maxwidth) || rv;
161 					(void)rmdir(mntpt);
162 					continue;
163 				} else if (statfs(mntpt, &statfsbuf) == 0) {
164 					statfsbuf.f_mntonname[0] = '\0';
165 					prtstat(&statfsbuf, maxwidth);
166 				} else {
167 					warn("%s", *argv);
168 					rv = 1;
169 				}
170 				(void)unmount(mntpt, 0);
171 				(void)rmdir(mntpt);
172 				continue;
173 			}
174 		} else
175 			mntpt = *argv;
176 		/*
177 		 * Statfs does not take a `wait' flag, so we cannot
178 		 * implement nflag here.
179 		 */
180 		if (statfs(mntpt, &statfsbuf) < 0) {
181 			warn("%s", mntpt);
182 			rv = 1;
183 			continue;
184 		}
185 		if (argc == 1)
186 			maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
187 		prtstat(&statfsbuf, maxwidth);
188 	}
189 	return (rv);
190 }
191 
192 char *
193 getmntpt(name)
194 	char *name;
195 {
196 	long mntsize, i;
197 	struct statfs *mntbuf;
198 
199 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
200 	for (i = 0; i < mntsize; i++) {
201 		if (!strcmp(mntbuf[i].f_mntfromname, name))
202 			return (mntbuf[i].f_mntonname);
203 	}
204 	return (0);
205 }
206 
207 /*
208  * Make a pass over the filesystem info in ``mntbuf'' filtering out
209  * filesystem types not in vfslist and possibly re-stating to get
210  * current (not cached) info.  Returns the new count of valid statfs bufs.
211  */
212 long
213 regetmntinfo(mntbufp, mntsize, vfslist)
214 	struct statfs **mntbufp;
215 	long mntsize;
216 	char **vfslist;
217 {
218 	int i, j;
219 	struct statfs *mntbuf;
220 
221 	if (vfslist == NULL)
222 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
223 
224 	mntbuf = *mntbufp;
225 	for (j = 0, i = 0; i < mntsize; i++) {
226 		if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
227 			continue;
228 		if (!nflag)
229 			(void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
230 		else if (i != j)
231 			mntbuf[j] = mntbuf[i];
232 		j++;
233 	}
234 	return (j);
235 }
236 
237 /*
238  * Convert statfs returned filesystem size into BLOCKSIZE units.
239  * Attempts to avoid overflow for large filesystems.
240  */
241 #define fsbtoblk(num, fsbs, bs) \
242 	(((fsbs) != 0 && (fsbs) < (bs)) ? \
243 		(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
244 
245 /*
246  * Print out status about a filesystem.
247  */
248 void
249 prtstat(sfsp, maxwidth)
250 	struct statfs *sfsp;
251 	int maxwidth;
252 {
253 	static long blocksize;
254 	static int headerlen, timesthrough;
255 	static char *header;
256 	long used, availblks, inodes;
257 
258 	if (maxwidth < 11)
259 		maxwidth = 11;
260 	if (++timesthrough == 1) {
261 		header = getbsize(&headerlen, &blocksize);
262 		(void)printf("%-*.*s %s     Used    Avail Capacity",
263 		    maxwidth, maxwidth, "Filesystem", header);
264 		if (iflag)
265 			(void)printf(" iused   ifree  %%iused");
266 		(void)printf("  Mounted on\n");
267 	}
268 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
269 	used = sfsp->f_blocks - sfsp->f_bfree;
270 	availblks = sfsp->f_bavail + used;
271 	(void)printf(" %*ld %8ld %8ld", headerlen,
272 	    fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
273 	    fsbtoblk(used, sfsp->f_bsize, blocksize),
274 	    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
275 	(void)printf(" %5.0f%%",
276 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
277 	if (iflag) {
278 		inodes = sfsp->f_files;
279 		used = inodes - sfsp->f_ffree;
280 		(void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
281 		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
282 	} else
283 		(void)printf("  ");
284 	(void)printf("  %s\n", sfsp->f_mntonname);
285 }
286 
287 /*
288  * This code constitutes the pre-system call Berkeley df code for extracting
289  * information from filesystem superblocks.
290  */
291 #include <ufs/ufs/dinode.h>
292 #include <ufs/ffs/fs.h>
293 #include <errno.h>
294 #include <fstab.h>
295 
296 union {
297 	struct fs iu_fs;
298 	char dummy[SBSIZE];
299 } sb;
300 #define sblock sb.iu_fs
301 
302 int	rfd;
303 
304 int
305 ufs_df(file, maxwidth)
306 	char *file;
307 	int maxwidth;
308 {
309 	struct statfs statfsbuf;
310 	struct statfs *sfsp;
311 	char *mntpt;
312 	static int synced;
313 
314 	if (synced++ == 0)
315 		sync();
316 
317 	if ((rfd = open(file, O_RDONLY)) < 0) {
318 		warn("%s", file);
319 		return (1);
320 	}
321 	if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
322 		(void)close(rfd);
323 		return (1);
324 	}
325 	sfsp = &statfsbuf;
326 	sfsp->f_type = 1;
327 	strcpy(sfsp->f_fstypename, "ufs");
328 	sfsp->f_flags = 0;
329 	sfsp->f_bsize = sblock.fs_fsize;
330 	sfsp->f_iosize = sblock.fs_bsize;
331 	sfsp->f_blocks = sblock.fs_dsize;
332 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
333 		sblock.fs_cstotal.cs_nffree;
334 	sfsp->f_bavail = freespace(&sblock, sblock.fs_minfree);
335 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
336 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
337 	sfsp->f_fsid.val[0] = 0;
338 	sfsp->f_fsid.val[1] = 0;
339 	if ((mntpt = getmntpt(file)) == 0)
340 		mntpt = "";
341 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
342 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
343 	prtstat(sfsp, maxwidth);
344 	(void)close(rfd);
345 	return (0);
346 }
347 
348 int
349 bread(off, buf, cnt)
350 	off_t off;
351 	void *buf;
352 	int cnt;
353 {
354 	int nr;
355 
356 	(void)lseek(rfd, off, SEEK_SET);
357 	if ((nr = read(rfd, buf, cnt)) != cnt) {
358 		/* Probably a dismounted disk if errno == EIO. */
359 		if (errno != EIO)
360 			(void)fprintf(stderr, "\ndf: %qd: %s\n",
361 			    off, strerror(nr > 0 ? EIO : errno));
362 		return (0);
363 	}
364 	return (1);
365 }
366 
367 void
368 usage()
369 {
370 	(void)fprintf(stderr,
371 	    "usage: df [-ikn] [-t type] [file | filesystem ...]\n");
372 	exit(1);
373 }
374