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