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