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