df.c revision 1.73 1 /* $NetBSD: df.c,v 1.73 2007/06/24 01:52:46 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT(
40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n");
42 #endif /* not lint */
43
44 #ifndef lint
45 #if 0
46 static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94";
47 #else
48 __RCSID("$NetBSD: df.c,v 1.73 2007/06/24 01:52:46 christos Exp $");
49 #endif
50 #endif /* not lint */
51
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <util.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 extern char *strpct(u_long, u_long, u_int);
66
67 int main(int, char *[]);
68 int bread(off_t, void *, int);
69 char *getmntpt(char *);
70 void prtstat(struct statvfs *, int);
71 int selected(const char *);
72 void maketypelist(char *);
73 long regetmntinfo(struct statvfs **, long);
74 void usage(void);
75 void prthumanval(int64_t, const char *);
76 void prthuman(struct statvfs *, int64_t, int64_t);
77 const char *
78 strpct64(uint64_t, uint64_t, u_int);
79
80 int aflag, gflag, hflag, iflag, lflag, nflag, Pflag;
81 long usize = 0;
82 char **typelist = NULL;
83
84 int
85 main(int argc, char *argv[])
86 {
87 struct stat stbuf;
88 struct statvfs *mntbuf;
89 long mntsize;
90 int ch, i, maxwidth, width;
91 char *mntpt;
92
93 while ((ch = getopt(argc, argv, "aGghiklmnPt:")) != -1)
94 switch (ch) {
95 case 'a':
96 aflag = 1;
97 break;
98 case 'G':
99 hflag = 0;
100 usize = 1024 * 1024 * 1024;
101 break;
102 case 'g':
103 gflag = 1;
104 break;
105 case 'h':
106 hflag = 1;
107 usize = 0;
108 break;
109 case 'i':
110 iflag = 1;
111 break;
112 case 'k':
113 hflag = 0;
114 usize = 1024;
115 break;
116 case 'l':
117 lflag = 1;
118 break;
119 case 'm':
120 hflag = 0;
121 usize = 1024 * 1024;
122 break;
123 case 'n':
124 nflag = 1;
125 break;
126 case 'P':
127 Pflag = 1;
128 break;
129 case 't':
130 if (typelist != NULL)
131 errx(1, "only one -t option may be specified.");
132 maketypelist(optarg);
133 break;
134 case '?':
135 default:
136 usage();
137 }
138
139 if (gflag && (Pflag || iflag))
140 errx(1, "only one of -g and -P or -i may be specified");
141 if (Pflag && iflag)
142 errx(1, "only one of -P and -i may be specified");
143
144 argc -= optind;
145 argv += optind;
146
147 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
148 if (mntsize == 0)
149 err(1, "retrieving information on mounted file systems");
150
151 if (*argv == NULL) {
152 mntsize = regetmntinfo(&mntbuf, mntsize);
153 } else {
154 if ((mntbuf = malloc(argc * sizeof(*mntbuf))) == NULL)
155 err(1, "can't allocate statvfs array");
156 mntsize = 0;
157 for (; *argv != NULL; argv++) {
158 if (stat(*argv, &stbuf) < 0) {
159 if ((mntpt = getmntpt(*argv)) == 0) {
160 warn("%s", *argv);
161 continue;
162 }
163 } else if (S_ISBLK(stbuf.st_mode)) {
164 if ((mntpt = getmntpt(*argv)) == 0)
165 mntpt = *argv;
166 } else
167 mntpt = *argv;
168 /*
169 * Statfs does not take a `wait' flag, so we cannot
170 * implement nflag here.
171 */
172 if (!statvfs(mntpt, &mntbuf[mntsize]))
173 if (lflag &&
174 (mntbuf[mntsize].f_flag & MNT_LOCAL) == 0)
175 warnx("Warning: %s is not a local %s",
176 *argv, "file system");
177 else if
178 (!selected(mntbuf[mntsize].f_fstypename))
179 warnx("Warning: %s mounted as a %s %s",
180 *argv,
181 mntbuf[mntsize].f_fstypename,
182 "file system");
183 else
184 ++mntsize;
185 else
186 warn("%s", *argv);
187 }
188 }
189
190 maxwidth = 0;
191 for (i = 0; i < mntsize; i++) {
192 width = strlen(mntbuf[i].f_mntfromname);
193 if (width > maxwidth)
194 maxwidth = width;
195 }
196 for (i = 0; i < mntsize; i++)
197 prtstat(&mntbuf[i], maxwidth);
198 exit(0);
199 /* NOTREACHED */
200 }
201
202 char *
203 getmntpt(char *name)
204 {
205 long mntsize, i;
206 struct statvfs *mntbuf;
207
208 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
209 for (i = 0; i < mntsize; i++) {
210 if (!strcmp(mntbuf[i].f_mntfromname, name))
211 return (mntbuf[i].f_mntonname);
212 }
213 return (0);
214 }
215
216 static enum { IN_LIST, NOT_IN_LIST } which;
217
218 int
219 selected(const char *type)
220 {
221 char **av;
222
223 /* If no type specified, it's always selected. */
224 if (typelist == NULL)
225 return (1);
226 for (av = typelist; *av != NULL; ++av)
227 if (!strncmp(type, *av, MFSNAMELEN))
228 return (which == IN_LIST ? 1 : 0);
229 return (which == IN_LIST ? 0 : 1);
230 }
231
232 void
233 maketypelist(char *fslist)
234 {
235 int i;
236 char *nextcp, **av;
237
238 if ((fslist == NULL) || (fslist[0] == '\0'))
239 errx(1, "empty type list");
240
241 /*
242 * XXX
243 * Note: the syntax is "noxxx,yyy" for no xxx's and
244 * no yyy's, not the more intuitive "noyyy,noyyy".
245 */
246 if (fslist[0] == 'n' && fslist[1] == 'o') {
247 fslist += 2;
248 which = NOT_IN_LIST;
249 } else
250 which = IN_LIST;
251
252 /* Count the number of types. */
253 for (i = 1, nextcp = fslist;
254 (nextcp = strchr(nextcp, ',')) != NULL; i++)
255 ++nextcp;
256
257 /* Build an array of that many types. */
258 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
259 err(1, "can't allocate type array");
260 av[0] = fslist;
261 for (i = 1, nextcp = fslist;
262 (nextcp = strchr(nextcp, ',')) != NULL; i++) {
263 *nextcp = '\0';
264 av[i] = ++nextcp;
265 }
266 /* Terminate the array. */
267 av[i] = NULL;
268 }
269
270 /*
271 * Make a pass over the filesystem info in ``mntbuf'' filtering out
272 * filesystem types not in ``fsmask'' and possibly re-stating to get
273 * current (not cached) info. Returns the new count of valid statvfs bufs.
274 */
275 long
276 regetmntinfo(struct statvfs **mntbufp, long mntsize)
277 {
278 int i, j;
279 struct statvfs *mntbuf;
280
281 if (!lflag && typelist == NULL && aflag)
282 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
283
284 mntbuf = *mntbufp;
285 j = 0;
286 for (i = 0; i < mntsize; i++) {
287 if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0)
288 continue;
289 if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0)
290 continue;
291 if (!selected(mntbuf[i].f_fstypename))
292 continue;
293 if (nflag)
294 mntbuf[j] = mntbuf[i];
295 else {
296 struct statvfs layerbuf = mntbuf[i];
297 (void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]);
298 /*
299 * If the FS name changed, then new data is for
300 * a different layer and we don't want it.
301 */
302 if (memcmp(layerbuf.f_mntfromname,
303 mntbuf[j].f_mntfromname, MNAMELEN))
304 mntbuf[j] = layerbuf;
305 }
306 j++;
307 }
308 return (j);
309 }
310
311 void
312 prthumanval(int64_t bytes, const char *pad)
313 {
314 char buf[6];
315
316 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
317 bytes, "", HN_AUTOSCALE,
318 HN_B | HN_NOSPACE | HN_DECIMAL);
319
320 (void)printf("%s %6s", pad, buf);
321 }
322
323 void
324 prthuman(struct statvfs *sfsp, int64_t used, int64_t bavail)
325 {
326
327 prthumanval(sfsp->f_blocks * sfsp->f_frsize, "");
328 prthumanval(used * sfsp->f_frsize, " ");
329 prthumanval(bavail * sfsp->f_frsize, " ");
330 }
331
332 /*
333 * Convert statvfs returned filesystem size into BLOCKSIZE units.
334 * Attempts to avoid overflow for large filesystems.
335 */
336 #define fsbtoblk(num, fsbs, bs) \
337 (((fsbs) != 0 && (fsbs) < (bs)) ? \
338 (int64_t)(num) / (int64_t)((bs) / (fsbs)) : \
339 (int64_t)(num) * ((fsbs) / (bs)))
340
341 /*
342 * Print out status about a filesystem.
343 */
344 void
345 prtstat(struct statvfs *sfsp, int maxwidth)
346 {
347 static long blocksize;
348 static int headerlen, timesthrough;
349 static const char *header;
350 static const char full[] = "100%";
351 static const char empty[] = " 0%";
352 int64_t used, availblks, inodes;
353 int64_t bavail;
354
355 if (gflag) {
356 /*
357 * From SunOS-5.6:
358 *
359 * /var (/dev/dsk/c0t0d0s3 ): 8192 block size 1024 frag size
360 * 984242 total blocks 860692 free blocks 859708 available 249984 total files
361 * 248691 free files 8388611 filesys id
362 * ufs fstype 0x00000004 flag 255 filename length
363 *
364 */
365 (void)printf("%10s (%-12s): %7ld block size %12ld frag size\n",
366 sfsp->f_mntonname, sfsp->f_mntfromname,
367 sfsp->f_iosize, /* On UFS/FFS systems this is
368 * also called the "optimal
369 * transfer block size" but it
370 * is of course the file
371 * system's block size too.
372 */
373 sfsp->f_bsize); /* not so surprisingly the
374 * "fundamental file system
375 * block size" is the frag
376 * size.
377 */
378 (void)printf("%10" PRId64 " total blocks %10" PRId64
379 " free blocks %10" PRId64 " available\n",
380 (uint64_t)sfsp->f_blocks, (uint64_t)sfsp->f_bfree,
381 (uint64_t)sfsp->f_bavail);
382 (void)printf("%10" PRId64 " total files %10" PRId64
383 " free files %12lx filesys id\n",
384 (uint64_t)sfsp->f_ffree, (uint64_t)sfsp->f_files,
385 sfsp->f_fsid);
386 (void)printf("%10s fstype %#15lx flag %17ld filename "
387 "length\n", sfsp->f_fstypename, sfsp->f_flag,
388 sfsp->f_namemax);
389 (void)printf("%10lu owner %17" PRId64 " syncwrites %12" PRId64
390 " asyncwrites\n\n", (unsigned long)sfsp->f_owner,
391 sfsp->f_syncwrites, sfsp->f_asyncwrites);
392
393 /*
394 * a concession by the structured programming police to the
395 * indentation police....
396 */
397 return;
398 }
399 if (maxwidth < 12)
400 maxwidth = 12;
401 if (++timesthrough == 1) {
402 switch (blocksize = usize) {
403 case 1024:
404 header = Pflag ? "1024-blocks" : "1K-blocks";
405 headerlen = strlen(header);
406 break;
407 case 1024 * 1024:
408 header = Pflag ? "1048576-blocks" : "1M-blocks";
409 headerlen = strlen(header);
410 break;
411 case 1024 * 1024 * 1024:
412 header = Pflag ? "1073741824-blocks" : "1G-blocks";
413 headerlen = strlen(header);
414 break;
415 default:
416 if (hflag) {
417 header = " Size";
418 headerlen = strlen(header);
419 } else
420 header = getbsize(&headerlen, &blocksize);
421 break;
422 }
423 if (Pflag) {
424 /*
425 * "Filesystem {1024,512}-blocks Used Available
426 * Capacity Mounted on\n"
427 */
428 (void)printf("Filesystem %s Used Available Capacity "
429 "Mounted on\n", header);
430 } else {
431 (void)printf("%-*.*s %s Used Avail %%Cap",
432 maxwidth - (headerlen - 9),
433 maxwidth - (headerlen - 9),
434 "Filesystem", header);
435 if (iflag)
436 (void)printf(" iUsed iAvail %%iCap");
437 (void)printf(" Mounted on\n");
438 }
439 }
440 used = sfsp->f_blocks - sfsp->f_bfree;
441 bavail = sfsp->f_bfree - sfsp->f_bresvd;
442 availblks = bavail + used;
443 if (Pflag) {
444 /*
445 * "%s %d %d %d %s %s\n", <file system name>, <total space>,
446 * <space used>, <space free>, <percentage used>,
447 * <file system root>
448 */
449 (void)printf("%s %" PRId64 " %" PRId64 " %" PRId64 " %s %s\n",
450 sfsp->f_mntfromname,
451 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
452 fsbtoblk(used, sfsp->f_bsize, blocksize),
453 fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize),
454 availblks == 0 ? full : strpct64((uint64_t) used,
455 (uint64_t) availblks, 0), sfsp->f_mntonname);
456 /*
457 * another concession by the structured programming police to
458 * the indentation police....
459 *
460 * Note iflag cannot be set when Pflag is set.
461 */
462 return;
463 }
464
465 (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
466
467 if (hflag)
468 prthuman(sfsp, used, bavail);
469 else
470 (void)printf("%10" PRId64 " %10" PRId64 " %10" PRId64,
471 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
472 fsbtoblk(used, sfsp->f_frsize, blocksize),
473 fsbtoblk(bavail, sfsp->f_frsize, blocksize));
474 (void)printf(" %4s",
475 availblks == 0 ? full :
476 /* We know that these values are never negative */
477 strpct64((uint64_t)used, (uint64_t)availblks, 0));
478 if (iflag) {
479 inodes = sfsp->f_files;
480 used = inodes - sfsp->f_ffree;
481 (void)printf(" %8ld %8ld %4s",
482 (u_long)used, (u_long)sfsp->f_ffree,
483 inodes == 0 ? (used == 0 ? empty : full) :
484 strpct64((uint64_t)used, (uint64_t)inodes, 0));
485 }
486 (void)printf(" %s\n", sfsp->f_mntonname);
487 }
488
489 void
490 usage(void)
491 {
492
493 (void)fprintf(stderr,
494 "Usage: %s [-aGghklmn] [-i|-p] [-t type] [file | "
495 "file_system ...]\n",
496 getprogname());
497 exit(1);
498 /* NOTREACHED */
499 }
500
501 const char *
502 strpct64(uint64_t numerator, uint64_t denominator, u_int digits)
503 {
504
505 while (denominator > ULONG_MAX) {
506 numerator >>= 1;
507 denominator >>= 1;
508 }
509 return (strpct((u_long)numerator, (u_long)denominator, digits));
510 }
511