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.4 2003/09/28 14:39:14 hmp Exp $
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/sysctl.h>
49 #include <vfs/ufs/ufsmount.h>
50 #include <vfs/ufs/fs.h>
66 #define KILO_SZ(n) (n)
67 #define MEGA_SZ(n) ((n) * (n))
68 #define GIGA_SZ(n) ((n) * (n) * (n))
69 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
70 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
72 #define KILO_2_SZ (KILO_SZ(1024ULL))
73 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
74 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
75 #define TERA_2_SZ (TERA_SZ(1024ULL))
76 #define PETA_2_SZ (PETA_SZ(1024ULL))
78 #define KILO_SI_SZ (KILO_SZ(1000ULL))
79 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
80 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
81 #define TERA_SI_SZ (TERA_SZ(1000ULL))
82 #define PETA_SI_SZ (PETA_SZ(1000ULL))
84 /* Maximum widths of various fields. */
94 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
95 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
96 unsigned long long *valp;
98 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
100 unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
102 int bread(off_t, void *, int);
103 int checkvfsname(const char *, char **);
104 char *getmntpt(char *);
106 char *makenetvfslist(void);
107 char **makevfslist(char *);
108 void prthuman(struct statfs *, long);
109 void prthumanval(double);
110 void prtstat(struct statfs *, struct maxwidths *);
111 long regetmntinfo(struct statfs **, long, char **);
112 int ufs_df(char *, struct maxwidths *);
113 unit_t unit_adjust(double *);
114 void update_maxwidths(struct maxwidths *, struct statfs *);
117 int aflag = 0, hflag, iflag, nflag;
118 struct ufs_args mdev;
120 static __inline int imax(int a, int b)
122 return (a > b ? a : b);
126 main(int argc, char **argv)
129 struct statfs statfsbuf, *mntbuf;
130 struct maxwidths maxwidths;
132 char *mntpath, *mntpt, **vfslist;
139 while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1)
147 putenv("BLOCKSIZE=512");
151 putenv("BLOCKSIZE=1g");
166 putenv("BLOCKSIZE=1k");
171 errx(1, "-l and -t are mutually exclusive.");
172 vfslist = makevfslist(makenetvfslist());
175 putenv("BLOCKSIZE=1m");
183 errx(1, "only one -t option may be specified");
185 vfslist = makevfslist(optarg);
194 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
195 bzero(&maxwidths, sizeof(maxwidths));
196 for (i = 0; i < mntsize; i++)
197 update_maxwidths(&maxwidths, &mntbuf[i]);
201 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
202 bzero(&maxwidths, sizeof(maxwidths));
203 for (i = 0; i < mntsize; i++)
204 update_maxwidths(&maxwidths, &mntbuf[i]);
205 for (i = 0; i < mntsize; i++) {
206 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
207 prtstat(&mntbuf[i], &maxwidths);
212 for (; *argv; argv++) {
213 if (stat(*argv, &stbuf) < 0) {
214 if ((mntpt = getmntpt(*argv)) == 0) {
219 } else if (S_ISCHR(stbuf.st_mode)) {
220 if ((mntpt = getmntpt(*argv)) == 0) {
222 mntpath = strdup("/tmp/df.XXXXXX");
223 if (mntpath == NULL) {
224 warn("strdup failed");
228 mntpt = mkdtemp(mntpath);
230 warn("mkdtemp(\"%s\") failed", mntpath);
235 if (mount(fstype, mntpt, MNT_RDONLY,
237 rv = ufs_df(*argv, &maxwidths) || rv;
241 } else if (statfs(mntpt, &statfsbuf) == 0) {
242 statfsbuf.f_mntonname[0] = '\0';
243 prtstat(&statfsbuf, &maxwidths);
248 (void)unmount(mntpt, 0);
256 * Statfs does not take a `wait' flag, so we cannot
257 * implement nflag here.
259 if (statfs(mntpt, &statfsbuf) < 0) {
265 bzero(&maxwidths, sizeof(maxwidths));
266 update_maxwidths(&maxwidths, &statfsbuf);
268 prtstat(&statfsbuf, &maxwidths);
277 struct statfs *mntbuf;
279 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
280 for (i = 0; i < mntsize; i++) {
281 if (!strcmp(mntbuf[i].f_mntfromname, name))
282 return (mntbuf[i].f_mntonname);
288 * Make a pass over the filesystem info in ``mntbuf'' filtering out
289 * filesystem types not in vfslist and possibly re-stating to get
290 * current (not cached) info. Returns the new count of valid statfs bufs.
293 regetmntinfo(struct statfs **mntbufp, long mntsize, char **vfslist)
296 struct statfs *mntbuf;
299 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
302 for (j = 0, i = 0; i < mntsize; i++) {
303 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
306 (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
308 mntbuf[j] = mntbuf[i];
315 * Output in "human-readable" format. Uses 3 digits max and puts
316 * unit suffixes at the end. Makes output compact and easy to read,
317 * especially on huge disks.
321 unit_adjust(double *val)
325 unsigned int unit_sz;
329 unit_sz = abval ? ilogb(abval) / 10 : 0;
331 if (unit_sz >= UNIT_MAX) {
334 unit = unitp[unit_sz];
335 *val /= (double)valp[unit_sz];
342 prthuman(struct statfs *sfsp, long used)
345 prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize);
346 prthumanval((double)used * (double)sfsp->f_bsize);
347 prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize);
351 prthumanval(double bytes)
355 unit = unit_adjust(&bytes);
360 (void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]);
362 (void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]);
366 * Convert statfs returned filesystem size into BLOCKSIZE units.
367 * Attempts to avoid overflow for large filesystems.
369 #define fsbtoblk(num, fsbs, bs) \
370 (((fsbs) != 0 && (fsbs) < (bs)) ? \
371 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
374 * Print out status about a filesystem.
377 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
379 static long blocksize;
380 static int headerlen, timesthrough;
381 static const char *header;
382 long used, availblks, inodes;
384 if (++timesthrough == 1) {
385 mwp->mntfrom = imax(mwp->mntfrom, strlen("Filesystem"));
388 mwp->total = mwp->used = mwp->avail = strlen(header);
390 header = getbsize(&headerlen, &blocksize);
391 mwp->total = imax(mwp->total, headerlen);
393 mwp->used = imax(mwp->used, strlen("Used"));
394 mwp->avail = imax(mwp->avail, strlen("Avail"));
396 (void)printf("%-*s %-*s %*s %*s Capacity", mwp->mntfrom,
397 "Filesystem", mwp->total, header, mwp->used, "Used",
398 mwp->avail, "Avail");
400 mwp->iused = imax(mwp->iused, strlen(" iused"));
401 mwp->ifree = imax(mwp->ifree, strlen("ifree"));
402 (void)printf(" %*s %*s %%iused", mwp->iused - 2,
403 "iused", mwp->ifree, "ifree");
405 (void)printf(" Mounted on\n");
407 (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
408 used = sfsp->f_blocks - sfsp->f_bfree;
409 availblks = sfsp->f_bavail + used;
411 prthuman(sfsp, used);
413 (void)printf(" %*ld %*ld %*ld", mwp->total,
414 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
415 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
416 mwp->avail, fsbtoblk(sfsp->f_bavail, sfsp->f_bsize,
419 (void)printf(" %5.0f%%",
420 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
422 inodes = sfsp->f_files;
423 used = inodes - sfsp->f_ffree;
424 (void)printf(" %*ld %*ld %4.0f%% ", mwp->iused, used,
425 mwp->ifree, sfsp->f_ffree, inodes == 0 ? 100.0 :
426 (double)used / (double)inodes * 100.0);
429 (void)printf(" %s\n", sfsp->f_mntonname);
433 * Update the maximum field-width information in `mwp' based on
434 * the filesystem specified by `sfsp'.
437 update_maxwidths(struct maxwidths *mwp, struct statfs *sfsp)
439 static long blocksize;
443 getbsize(&dummy, &blocksize);
445 mwp->mntfrom = imax(mwp->mntfrom, strlen(sfsp->f_mntfromname));
446 mwp->total = imax(mwp->total, longwidth(fsbtoblk(sfsp->f_blocks,
447 sfsp->f_bsize, blocksize)));
448 mwp->used = imax(mwp->used, longwidth(fsbtoblk(sfsp->f_blocks -
449 sfsp->f_bfree, sfsp->f_bsize, blocksize)));
450 mwp->avail = imax(mwp->avail, longwidth(fsbtoblk(sfsp->f_bavail,
451 sfsp->f_bsize, blocksize)));
452 mwp->iused = imax(mwp->iused, longwidth(sfsp->f_files -
454 mwp->ifree = imax(mwp->ifree, longwidth(sfsp->f_ffree));
457 /* Return the width in characters of the specified long. */
464 /* Negative or zero values require one extra digit. */
478 * This code constitutes the pre-system call Berkeley df code for extracting
479 * information from filesystem superblocks.
486 #define sblock sb.iu_fs
491 ufs_df(char *file, struct maxwidths *mwp)
493 struct statfs statfsbuf;
501 if ((rfd = open(file, O_RDONLY)) < 0) {
505 if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
511 strcpy(sfsp->f_fstypename, "ufs");
513 sfsp->f_bsize = sblock.fs_fsize;
514 sfsp->f_iosize = sblock.fs_bsize;
515 sfsp->f_blocks = sblock.fs_dsize;
516 sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
517 sblock.fs_cstotal.cs_nffree;
518 sfsp->f_bavail = freespace(&sblock, sblock.fs_minfree);
519 sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
520 sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
521 sfsp->f_fsid.val[0] = 0;
522 sfsp->f_fsid.val[1] = 0;
523 if ((mntpt = getmntpt(file)) == 0)
525 memmove(&sfsp->f_mntonname[0], mntpt, (size_t)MNAMELEN);
526 memmove(&sfsp->f_mntfromname[0], file, (size_t)MNAMELEN);
533 bread(off_t off, void *buf, int cnt)
537 (void)lseek(rfd, off, SEEK_SET);
538 if ((nr = read(rfd, buf, (size_t)cnt)) != (ssize_t)cnt) {
539 /* Probably a dismounted disk if errno == EIO. */
541 (void)fprintf(stderr, "\ndf: %lld: %s\n",
542 (long long)off, strerror(nr > 0 ? EIO : errno));
552 (void)fprintf(stderr,
553 "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n");
560 char *str, *strptr, **listptr;
561 int mib[3], maxvfsconf, cnt=0, i;
563 struct ovfsconf *ptr;
565 mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM;
566 miblen=sizeof(maxvfsconf);
567 if (sysctl(mib, (unsigned int)(sizeof(mib) / sizeof(mib[0])),
568 &maxvfsconf, &miblen, NULL, 0)) {
569 warnx("sysctl failed");
573 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
574 warnx("malloc failed");
578 for (ptr = getvfsent(); ptr; ptr = getvfsent())
579 if (ptr->vfc_flags & VFCF_NETWORK) {
580 listptr[cnt++] = strdup(ptr->vfc_name);
581 if (listptr[cnt-1] == NULL) {
582 warnx("malloc failed");
588 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
590 warnx("malloc failed");
595 *str = 'n'; *(str + 1) = 'o';
596 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
597 strncpy(strptr, listptr[i], 32);
598 strptr += strlen(listptr[i]);