2 * Copyright (c) 1980, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#) Copyright (c) 1980, 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
39 * @(#)df.c 8.9 (Berkeley) 5/8/95
40 * $FreeBSD: src/bin/df/df.c,v 1.23.2.9 2002/07/01 00:14:24 iedowse Exp $
41 * $DragonFly: src/bin/df/df.c,v 1.13 2008/08/07 09:59:49 swildner Exp $
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/sysctl.h>
49 #include <sys/statvfs.h>
51 #include <vfs/ufs/dinode.h>
52 #include <vfs/ufs/fs.h>
53 #include <vfs/ufs/ufsmount.h>
69 /* Maximum widths of various fields. */
79 int bread(off_t, void *, int);
80 int checkvfsname(const char *, char **);
81 char *getmntpt(char *);
82 int quadwidth(int64_t);
83 char *makenetvfslist(void);
84 char **makevfslist(char *);
85 void prthuman(struct statvfs *, int64_t);
86 void prthumanval(int64_t);
87 void prtstat(struct statfs *, struct statvfs *, struct maxwidths *);
88 long regetmntinfo(struct statfs **, struct statvfs **, long, char **);
89 int ufs_df(char *, struct maxwidths *);
90 void update_maxwidths(struct maxwidths *, struct statfs *, struct statvfs *);
93 int aflag = 0, hflag, iflag, nflag;
99 return (a > b ? a : b);
102 static __inline int64_t
103 qmax(int64_t a, int64_t b)
105 return (a > b ? a : b);
109 main(int argc, char **argv)
112 struct statfs statfsbuf, *mntbuf;
113 struct statvfs statvfsbuf, *mntvbuf;
114 struct maxwidths maxwidths;
116 char *mntpath, *mntpt, **vfslist;
123 while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1)
131 if (putenv("BLOCKSIZE=512") != 0)
132 warn("putenv: cannot set BLOCKSIZE=512");
136 if (putenv("BLOCKSIZE=1g") != 0)
137 warn("putenv: cannot set BLOCKSIZE=1g");
150 if (putenv("BLOCKSIZE=1k") != 0)
151 warn("putenv: cannot set BLOCKSIZE=1k");
156 errx(1, "-l and -t are mutually exclusive.");
157 vfslist = makevfslist(makenetvfslist());
160 if (putenv("BLOCKSIZE=1m") != 0)
161 warn("putenv: cannot set BLOCKSIZE=1m");
169 errx(1, "only one -t option may be specified");
171 vfslist = makevfslist(optarg);
180 mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
181 bzero(&maxwidths, sizeof(maxwidths));
182 for (i = 0; i < mntsize; i++)
183 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
187 mntsize = regetmntinfo(&mntbuf, &mntvbuf, mntsize, vfslist);
188 bzero(&maxwidths, sizeof(maxwidths));
189 for (i = 0; i < mntsize; i++)
190 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
191 for (i = 0; i < mntsize; i++) {
192 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
193 prtstat(&mntbuf[i], &mntvbuf[i], &maxwidths);
198 for (; *argv; argv++) {
199 if (stat(*argv, &stbuf) < 0) {
200 if ((mntpt = getmntpt(*argv)) == 0) {
205 } else if (S_ISCHR(stbuf.st_mode)) {
206 if ((mntpt = getmntpt(*argv)) == 0) {
208 mntpath = strdup("/tmp/df.XXXXXX");
209 if (mntpath == NULL) {
210 warn("strdup failed");
214 mntpt = mkdtemp(mntpath);
216 warn("mkdtemp(\"%s\") failed", mntpath);
221 if (mount(fstype, mntpt, MNT_RDONLY,
223 rv = ufs_df(*argv, &maxwidths) || rv;
227 } else if (statfs(mntpt, &statfsbuf) == 0 &&
228 statvfs(mntpt, &statvfsbuf) == 0) {
229 statfsbuf.f_mntonname[0] = '\0';
230 prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
243 * Statfs does not take a `wait' flag, so we cannot
244 * implement nflag here.
246 if (statfs(mntpt, &statfsbuf) < 0) {
251 if (statvfs(mntpt, &statvfsbuf) < 0) {
257 bzero(&maxwidths, sizeof(maxwidths));
258 update_maxwidths(&maxwidths, &statfsbuf, &statvfsbuf);
260 prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
269 struct statfs *mntbuf;
270 struct statvfs *mntvbuf;
272 mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
273 for (i = 0; i < mntsize; i++) {
274 if (!strcmp(mntbuf[i].f_mntfromname, name))
275 return (mntbuf[i].f_mntonname);
281 * Make a pass over the filesystem info in ``mntbuf'' filtering out
282 * filesystem types not in vfslist and possibly re-stating to get
283 * current (not cached) info. Returns the new count of valid statfs bufs.
286 regetmntinfo(struct statfs **mntbufp, struct statvfs **mntvbufp, long mntsize, char **vfslist)
289 struct statfs *mntbuf;
290 struct statvfs *mntvbuf;
293 return (nflag ? mntsize : getmntvinfo(mntbufp, mntvbufp, MNT_WAIT));
297 for (j = 0, i = 0; i < mntsize; i++) {
298 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
301 statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
302 statvfs(mntbuf[i].f_mntonname,&mntvbuf[j]);
304 mntbuf[j] = mntbuf[i];
305 mntvbuf[j] = mntvbuf[i];
313 prthuman(struct statvfs *vsfsp, int64_t used)
315 prthumanval(vsfsp->f_blocks * vsfsp->f_bsize);
316 prthumanval(used * vsfsp->f_bsize);
317 prthumanval(vsfsp->f_bavail * vsfsp->f_bsize);
321 prthumanval(int64_t bytes)
326 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
327 if (hflag == UNITS_SI)
328 flags |= HN_DIVISOR_1000;
330 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
331 bytes, "", HN_AUTOSCALE, flags);
337 * Convert statfs returned filesystem size into BLOCKSIZE units.
338 * Attempts to avoid overflow for large filesystems.
342 fsbtoblk(int64_t num, long bsize, long reqbsize)
344 if (bsize && bsize < reqbsize)
345 num = num / (reqbsize / bsize);
347 num = num * (bsize / reqbsize);
352 * Print out status about a filesystem.
355 prtstat(struct statfs *sfsp, struct statvfs *vsfsp, struct maxwidths *mwp)
357 static long blocksize;
358 static int headerlen, timesthrough;
359 static const char *header;
360 int64_t used, availblks, inodes;
362 if (++timesthrough == 1) {
363 mwp->mntfrom = imax(mwp->mntfrom, strlen("Filesystem"));
366 mwp->total = mwp->used = mwp->avail = strlen(header);
368 header = getbsize(&headerlen, &blocksize);
369 mwp->total = imax(mwp->total, headerlen);
371 mwp->used = imax(mwp->used, strlen("Used"));
372 mwp->avail = imax(mwp->avail, strlen("Avail"));
374 printf("%-*s %-*s %*s %*s Capacity", mwp->mntfrom,
375 "Filesystem", mwp->total, header, mwp->used, "Used",
376 mwp->avail, "Avail");
378 mwp->iused = imax(mwp->iused, strlen(" iused"));
379 mwp->ifree = imax(mwp->ifree, strlen("ifree"));
380 printf(" %*s %*s %%iused", mwp->iused - 2,
381 "iused", mwp->ifree, "ifree");
383 printf(" Mounted on\n");
385 printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
386 used = vsfsp->f_blocks - vsfsp->f_bfree;
387 availblks = vsfsp->f_bavail + used;
389 prthuman(vsfsp, used);
391 printf(" %*lld %*lld %*lld", mwp->total,
392 fsbtoblk(vsfsp->f_blocks, vsfsp->f_bsize, blocksize),
393 mwp->used, fsbtoblk(used, vsfsp->f_bsize, blocksize),
394 mwp->avail, fsbtoblk(vsfsp->f_bavail, vsfsp->f_bsize,
398 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
400 inodes = vsfsp->f_files;
401 used = inodes - vsfsp->f_ffree;
402 printf(" %*lld %*lld %4.0f%% ", mwp->iused, used,
403 mwp->ifree, (int64_t)vsfsp->f_ffree, inodes == 0 ? 100.0 :
404 (double)used / (double)inodes * 100.0);
407 printf(" %s\n", sfsp->f_mntonname);
411 * Update the maximum field-width information in `mwp' based on
412 * the filesystem specified by `sfsp'.
415 update_maxwidths(struct maxwidths *mwp, struct statfs *sfsp, struct statvfs *vsfsp)
417 static long blocksize;
421 getbsize(&dummy, &blocksize);
423 mwp->mntfrom = imax(mwp->mntfrom, strlen(sfsp->f_mntfromname));
424 mwp->total = imax(mwp->total, quadwidth(fsbtoblk(vsfsp->f_blocks,
425 vsfsp->f_bsize, blocksize)));
426 mwp->used = imax(mwp->used, quadwidth(fsbtoblk(vsfsp->f_blocks -
427 vsfsp->f_bfree, vsfsp->f_bsize, blocksize)));
428 mwp->avail = imax(mwp->avail, quadwidth(fsbtoblk(vsfsp->f_bavail,
429 vsfsp->f_bsize, blocksize)));
430 mwp->iused = imax(mwp->iused, quadwidth(vsfsp->f_files -
432 mwp->ifree = imax(mwp->ifree, quadwidth(vsfsp->f_ffree));
435 /* Return the width in characters of the specified long. */
437 quadwidth(int64_t val)
442 /* Negative or zero values require one extra digit. */
455 * This code constitutes the pre-system call Berkeley df code for extracting
456 * information from filesystem superblocks.
463 #define sblock sb.iu_fs
468 ufs_df(char *file, struct maxwidths *mwp)
470 struct statfs statfsbuf;
471 struct statvfs statvfsbuf;
473 struct statvfs *vsfsp;
480 if ((rfd = open(file, O_RDONLY)) < 0) {
484 if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
491 strcpy(sfsp->f_fstypename, "ufs");
493 sfsp->f_bsize = vsfsp->f_bsize = sblock.fs_fsize;
494 sfsp->f_iosize = vsfsp->f_frsize = sblock.fs_bsize;
495 sfsp->f_blocks = vsfsp->f_blocks = sblock.fs_dsize;
496 sfsp->f_bfree = vsfsp->f_bfree =
497 sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
498 sblock.fs_cstotal.cs_nffree;
499 sfsp->f_bavail = vsfsp->f_bavail = freespace(&sblock, sblock.fs_minfree);
500 sfsp->f_files = vsfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
501 sfsp->f_ffree = vsfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
502 sfsp->f_fsid.val[0] = 0;
503 sfsp->f_fsid.val[1] = 0;
504 if ((mntpt = getmntpt(file)) == 0)
506 memmove(&sfsp->f_mntonname[0], mntpt, (size_t)MNAMELEN);
507 memmove(&sfsp->f_mntfromname[0], file, (size_t)MNAMELEN);
508 prtstat(sfsp, vsfsp, mwp);
514 bread(off_t off, void *buf, int cnt)
518 lseek(rfd, off, SEEK_SET);
519 if ((nr = read(rfd, buf, (size_t)cnt)) != (ssize_t)cnt) {
520 /* Probably a dismounted disk if errno == EIO. */
522 fprintf(stderr, "\ndf: %lld: %s\n",
523 (long long)off, strerror(nr > 0 ? EIO : errno));
534 "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n");
541 char *str, *strptr, **listptr;
542 int mib[3], maxvfsconf, cnt=0, i;
544 struct ovfsconf *ptr;
546 mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM;
547 miblen=sizeof(maxvfsconf);
548 if (sysctl(mib, (unsigned int)(sizeof(mib) / sizeof(mib[0])),
549 &maxvfsconf, &miblen, NULL, 0)) {
550 warnx("sysctl failed");
554 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
555 warnx("malloc failed");
559 for (ptr = getvfsent(); ptr; ptr = getvfsent())
560 if (ptr->vfc_flags & VFCF_NETWORK) {
561 listptr[cnt++] = strdup(ptr->vfc_name);
562 if (listptr[cnt-1] == NULL) {
563 warnx("malloc failed");
569 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
571 warnx("malloc failed");
576 *str = 'n'; *(str + 1) = 'o';
577 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
578 strncpy(strptr, listptr[i], 32);
579 strptr += strlen(listptr[i]);