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