df: Remove old UFS superblock reading code.
[dragonfly.git] / bin / df / df.c
1 /*
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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * @(#) Copyright (c) 1980, 1990, 1993, 1994 The Regents of the University of California.  All rights reserved.
35  * @(#)df.c     8.9 (Berkeley) 5/8/95
36  * $FreeBSD: src/bin/df/df.c,v 1.23.2.9 2002/07/01 00:14:24 iedowse Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/mount.h>
42 #include <sys/sysctl.h>
43 #include <sys/statvfs.h>
44
45 #include <vfs/ufs/dinode.h>
46 #include <vfs/ufs/ufsmount.h>
47
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fstab.h>
52 #include <libutil.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58
59 #define UNITS_SI 1
60 #define UNITS_2 2
61
62 /* Maximum widths of various fields. */
63 struct maxwidths {
64         int mntfrom;
65         int fstype;
66         int total;
67         int used;
68         int avail;
69         int iused;
70         int ifree;
71 };
72
73 /* vfslist.c */
74 char    **makevfslist(char *);
75 int       checkvfsname(const char *, char **);
76
77 static char     *getmntpt(char *);
78 static int       quadwidth(int64_t);
79 static char     *makenetvfslist(void);
80 static void      prthuman(struct statvfs *, int64_t);
81 static void      prthumanval(int64_t);
82 static void      prtstat(struct statfs *, struct statvfs *, struct maxwidths *);
83 static long      regetmntinfo(struct statfs **, struct statvfs **, long, char **);
84 static void      update_maxwidths(struct maxwidths *, struct statfs *, struct statvfs *);
85 static void      usage(void);
86
87 int     aflag = 0, hflag, iflag, nflag, Tflag;
88 struct  ufs_args mdev;
89
90 static __inline int
91 imax(int a, int b)
92 {
93         return (a > b ? a : b);
94 }
95
96 static __inline int64_t
97 qmax(int64_t a, int64_t b)
98 {
99         return (a > b ? a : b);
100 }
101
102 int
103 main(int argc, char **argv)
104 {
105         struct stat stbuf;
106         struct statfs statfsbuf, *mntbuf;
107         struct statvfs statvfsbuf, *mntvbuf;
108         struct maxwidths maxwidths;
109         const char *fstype;
110         char *mntpath, *mntpt, **vfslist;
111         long mntsize;
112         int ch, i, rv;
113
114         fstype = "ufs";
115
116         vfslist = NULL;
117         while ((ch = getopt(argc, argv, "abgHhiklmnPt:T")) != -1)
118                 switch (ch) {
119                 case 'a':
120                         aflag = 1;
121                         break;
122                 case 'b':
123                                 /* FALLTHROUGH */
124                 case 'P':
125                         if (setenv("BLOCKSIZE", "512", 1) != 0)
126                                 warn("setenv: cannot set BLOCKSIZE=512");
127                         hflag = 0;
128                         break;
129                 case 'g':
130                         if (setenv("BLOCKSIZE", "1g", 1) != 0)
131                                 warn("setenv: cannot set BLOCKSIZE=1g");
132                         hflag = 0;
133                         break;
134                 case 'H':
135                         hflag = UNITS_SI;
136                         break;
137                 case 'h':
138                         hflag = UNITS_2;
139                         break;
140                 case 'i':
141                         iflag = 1;
142                         break;
143                 case 'k':
144                         if (setenv("BLOCKSIZE", "1k", 1) != 0)
145                                 warn("setenv: cannot set BLOCKSIZE=1k");
146                         hflag = 0;
147                         break;
148                 case 'l':
149                         if (vfslist != NULL)
150                                 errx(1, "-l and -t are mutually exclusive.");
151                         vfslist = makevfslist(makenetvfslist());
152                         break;
153                 case 'm':
154                         if (setenv("BLOCKSIZE", "1m", 1) != 0)
155                                 warn("setenv: cannot set BLOCKSIZE=1m");
156                         hflag = 0;
157                         break;
158                 case 'n':
159                         nflag = 1;
160                         break;
161                 case 't':
162                         if (vfslist != NULL)
163                                 errx(1, "only one -t option may be specified");
164                         fstype = optarg;
165                         vfslist = makevfslist(optarg);
166                         break;
167                 case 'T':
168                         Tflag = 1;
169                         break;
170                 case '?':
171                 default:
172                         usage();
173                 }
174         argc -= optind;
175         argv += optind;
176
177         mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
178         bzero(&maxwidths, sizeof(maxwidths));
179         for (i = 0; i < mntsize; i++)
180                 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
181
182         rv = 0;
183         if (!*argv) {
184                 mntsize = regetmntinfo(&mntbuf, &mntvbuf, mntsize, vfslist);
185                 bzero(&maxwidths, sizeof(maxwidths));
186                 for (i = 0; i < mntsize; i++)
187                         update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
188                 for (i = 0; i < mntsize; i++) {
189                         if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
190                                 prtstat(&mntbuf[i], &mntvbuf[i], &maxwidths);
191                 }
192                 exit(rv);
193         }
194
195         for (; *argv; argv++) {
196                 if (stat(*argv, &stbuf) < 0) {
197                         if ((mntpt = getmntpt(*argv)) == NULL) {
198                                 warn("%s", *argv);
199                                 rv = 1;
200                                 continue;
201                         }
202                 } else if (S_ISCHR(stbuf.st_mode)) {
203                         if ((mntpt = getmntpt(*argv)) == NULL) {
204                                 mdev.fspec = *argv;
205                                 mntpath = strdup("/tmp/df.XXXXXX");
206                                 if (mntpath == NULL) {
207                                         warn("strdup failed");
208                                         rv = 1;
209                                         continue;
210                                 }
211                                 mntpt = mkdtemp(mntpath);
212                                 if (mntpt == NULL) {
213                                         warn("mkdtemp(\"%s\") failed", mntpath);
214                                         rv = 1;
215                                         free(mntpath);
216                                         continue;
217                                 }
218                                 if (mount(fstype, mntpt, MNT_RDONLY,
219                                     &mdev) != 0) {
220                                         warn("%s", *argv);
221                                         rv = 1;
222                                         rmdir(mntpt);
223                                         free(mntpath);
224                                         continue;
225                                 } else if (statfs(mntpt, &statfsbuf) == 0 &&
226                                            statvfs(mntpt, &statvfsbuf) == 0) {
227                                         statfsbuf.f_mntonname[0] = '\0';
228                                         prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
229                                 } else {
230                                         warn("%s", *argv);
231                                         rv = 1;
232                                 }
233                                 unmount(mntpt, 0);
234                                 rmdir(mntpt);
235                                 free(mntpath);
236                                 continue;
237                         }
238                 } else
239                         mntpt = *argv;
240                 /*
241                  * Statfs does not take a `wait' flag, so we cannot
242                  * implement nflag here.
243                  */
244                 if (statfs(mntpt, &statfsbuf) < 0) {
245                         warn("%s", mntpt);
246                         rv = 1;
247                         continue;
248                 }
249                 if (statvfs(mntpt, &statvfsbuf) < 0) {
250                         warn("%s", mntpt);
251                         rv = 1;
252                         continue;
253                 }
254                 /*
255                  * Check to make sure the arguments we've been given are
256                  * satisfied. Return an error if we have been asked to
257                  * list a mount point that does not match the other args
258                  * we've been given (-l, -t, etc.).
259                  */
260                 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
261                         rv = 1;
262                         continue;
263                 }
264
265                 if (argc == 1) {
266                         bzero(&maxwidths, sizeof(maxwidths));
267                         update_maxwidths(&maxwidths, &statfsbuf, &statvfsbuf);
268                 }
269                 prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
270         }
271         return (rv);
272 }
273
274 static char *
275 getmntpt(char *name)
276 {
277         long mntsize, i;
278         struct statfs *mntbuf;
279         struct statvfs *mntvbuf;
280
281         mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
282         for (i = 0; i < mntsize; i++) {
283                 if (!strcmp(mntbuf[i].f_mntfromname, name))
284                         return (mntbuf[i].f_mntonname);
285         }
286         return (0);
287 }
288
289 /*
290  * Make a pass over the filesystem info in ``mntbuf'' filtering out
291  * filesystem types not in vfslist and possibly re-stating to get
292  * current (not cached) info.  Returns the new count of valid statfs bufs.
293  */
294 static long
295 regetmntinfo(struct statfs **mntbufp, struct statvfs **mntvbufp, long mntsize, char **vfslist)
296 {
297         int i, j;
298         struct statfs *mntbuf;
299         struct statvfs *mntvbuf;
300
301         if (vfslist == NULL)
302                 return (nflag ? mntsize : getmntvinfo(mntbufp, mntvbufp, MNT_WAIT));
303
304         mntbuf = *mntbufp;
305         mntvbuf = *mntvbufp;
306         for (j = 0, i = 0; i < mntsize; i++) {
307                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
308                         continue;
309                 if (!nflag) {
310                         statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
311                         statvfs(mntbuf[i].f_mntonname,&mntvbuf[j]);
312                 } else if (i != j) {
313                         mntbuf[j] = mntbuf[i];
314                         mntvbuf[j] = mntvbuf[i];
315                 }
316                 j++;
317         }
318         return (j);
319 }
320
321 static void
322 prthuman(struct statvfs *vsfsp, int64_t used)
323 {
324         prthumanval(vsfsp->f_blocks * vsfsp->f_bsize);
325         prthumanval(used * vsfsp->f_bsize);
326         prthumanval(vsfsp->f_bavail * vsfsp->f_bsize);
327 }
328
329 static void
330 prthumanval(int64_t bytes)
331 {
332         char buf[6];
333         int flags;
334
335         flags = HN_B | HN_NOSPACE | HN_DECIMAL;
336         if (hflag == UNITS_SI)
337                 flags |= HN_DIVISOR_1000;
338
339         humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
340             bytes, "", HN_AUTOSCALE, flags);
341
342         printf(" %6s", buf);
343 }
344
345 /*
346  * Print an inode count in "human-readable" format.
347  */
348 static void
349 prthumanvalinode(int64_t bytes)
350 {
351         char buf[6];
352         int flags;
353
354         flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
355
356         humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
357             bytes, "", HN_AUTOSCALE, flags);
358
359         printf(" %5s", buf);
360 }
361
362 /*
363  * Convert statfs returned filesystem size into BLOCKSIZE units.
364  * Attempts to avoid overflow for large filesystems.
365  */
366 static intmax_t
367 fsbtoblk(int64_t num, uint64_t bsize, u_long reqbsize)
368 {
369         if (bsize != 0 && bsize < reqbsize)
370                 return (num / (intmax_t)(reqbsize / bsize));
371         else
372                 return (num * (intmax_t)(bsize / reqbsize));
373 }
374
375 /*
376  * Print out status about a filesystem.
377  */
378 static void
379 prtstat(struct statfs *sfsp, struct statvfs *vsfsp, struct maxwidths *mwp)
380 {
381         static long blocksize;
382         static int headerlen, timesthrough;
383         static const char *header;
384         int64_t used, availblks, inodes;
385
386         if (++timesthrough == 1) {
387                 mwp->mntfrom = imax(mwp->mntfrom, strlen("Filesystem"));
388                 mwp->fstype = imax(mwp->fstype, strlen("Type"));
389                 if (hflag) {
390                         header = "  Size";
391                         mwp->total = mwp->used = mwp->avail = strlen(header);
392                 } else {
393                         header = getbsize(&headerlen, &blocksize);
394                         mwp->total = imax(mwp->total, headerlen);
395                 }
396                 mwp->used = imax(mwp->used, strlen("Used"));
397                 mwp->avail = imax(mwp->avail, strlen("Avail"));
398
399                 printf("%-*s", mwp->mntfrom, "Filesystem");
400                 if (Tflag)
401                         printf("  %-*s", mwp->fstype, "Type");
402                 printf(" %-*s %*s %*s Capacity", mwp->total, header, mwp->used,
403                     "Used", mwp->avail, "Avail");
404                 if (iflag) {
405                         mwp->iused = imax(hflag ? 0 : mwp->iused,
406                             (int)strlen("  iused"));
407                         mwp->ifree = imax(hflag ? 0 : mwp->ifree,
408                             (int)strlen("ifree"));
409                         printf(" %*s %*s %%iused", mwp->iused - 2,
410                             "iused", mwp->ifree, "ifree");
411                 }
412                 printf("  Mounted on\n");
413         }
414         printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
415         if (Tflag)
416                 printf("  %-*s", mwp->fstype, sfsp->f_fstypename);
417         used = vsfsp->f_blocks - vsfsp->f_bfree;
418         availblks = vsfsp->f_bavail + used;
419         if (hflag) {
420                 prthuman(vsfsp, used);
421         } else {
422                 printf(" %*jd %*jd %*jd", mwp->total,
423                     fsbtoblk(vsfsp->f_blocks, vsfsp->f_bsize, blocksize),
424                     mwp->used, fsbtoblk(used, vsfsp->f_bsize, blocksize),
425                     mwp->avail, fsbtoblk(vsfsp->f_bavail, vsfsp->f_bsize,
426                     blocksize));
427         }
428         printf(" %5.0f%%",
429             availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
430         if (iflag) {
431                 inodes = vsfsp->f_files;
432                 used = inodes - vsfsp->f_ffree;
433                 if (hflag) {
434                         printf("  ");
435                         prthumanvalinode(used);
436                         prthumanvalinode(vsfsp->f_ffree);
437                 } else {
438                         printf(" %*jd %*jd", mwp->iused, (intmax_t)used,
439                             mwp->ifree, (intmax_t)vsfsp->f_ffree);
440                 }
441                 printf(" %4.0f%% ", inodes == 0 ? 100.0 :
442                     (double)used / (double)inodes * 100.0);
443         } else
444                 printf("  ");
445         printf("  %s\n", sfsp->f_mntonname);
446 }
447
448 /*
449  * Update the maximum field-width information in `mwp' based on
450  * the filesystem specified by `sfsp'.
451  */
452 static void
453 update_maxwidths(struct maxwidths *mwp, struct statfs *sfsp, struct statvfs *vsfsp)
454 {
455         static long blocksize;
456         int dummy;
457
458         if (blocksize == 0)
459                 getbsize(&dummy, &blocksize);
460
461         mwp->mntfrom = imax(mwp->mntfrom, strlen(sfsp->f_mntfromname));
462         mwp->fstype = imax(mwp->fstype, strlen(sfsp->f_fstypename));
463         mwp->total = imax(mwp->total, quadwidth(fsbtoblk(vsfsp->f_blocks,
464             vsfsp->f_bsize, blocksize)));
465         mwp->used = imax(mwp->used, quadwidth(fsbtoblk(vsfsp->f_blocks -
466             vsfsp->f_bfree, vsfsp->f_bsize, blocksize)));
467         mwp->avail = imax(mwp->avail, quadwidth(fsbtoblk(vsfsp->f_bavail,
468             vsfsp->f_bsize, blocksize)));
469         mwp->iused = imax(mwp->iused, quadwidth(vsfsp->f_files -
470             vsfsp->f_ffree));
471         mwp->ifree = imax(mwp->ifree, quadwidth(vsfsp->f_ffree));
472 }
473
474 /* Return the width in characters of the specified long. */
475 static int
476 quadwidth(int64_t val)
477 {
478         int len;
479
480         len = 0;
481         /* Negative or zero values require one extra digit. */
482         if (val <= 0) {
483                 val = -val;
484                 len++;
485         }
486         while (val > 0) {
487                 len++;
488                 val /= 10;
489         }
490         return (len);
491 }
492
493 static void
494 usage(void)
495 {
496
497         fprintf(stderr,
498             "usage: df [-b | -H | -h | -k | -m | -P] [-ailnT] [-t type] [file | filesystem ...]\n");
499         exit(EX_USAGE);
500 }
501
502 static char *
503 makenetvfslist(void)
504 {
505         char *str, *strptr, **listptr;
506         int mib[3], maxvfsconf, cnt=0, i;
507         size_t miblen;
508         struct ovfsconf *ptr;
509
510         mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM;
511         miblen=sizeof(maxvfsconf);
512         if (sysctl(mib, (unsigned int)(sizeof(mib) / sizeof(mib[0])),
513             &maxvfsconf, &miblen, NULL, 0)) {
514                 warnx("sysctl failed");
515                 return (NULL);
516         }
517
518         if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
519                 warnx("malloc failed");
520                 return (NULL);
521         }
522
523         for (ptr = getvfsent(); ptr; ptr = getvfsent())
524                 if (ptr->vfc_flags & VFCF_NETWORK) {
525                         listptr[cnt++] = strdup(ptr->vfc_name);
526                         if (listptr[cnt-1] == NULL) {
527                                 warnx("malloc failed");
528                                 free(listptr);
529                                 return (NULL);
530                         }
531                 }
532
533         if (cnt == 0 ||
534             (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
535                 if (cnt > 0)
536                         warnx("malloc failed");
537                 free(listptr);
538                 return (NULL);
539         }
540
541         *str = 'n'; *(str + 1) = 'o';
542         for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
543                 strncpy(strptr, listptr[i], 32);
544                 strptr += strlen(listptr[i]);
545                 *strptr = ',';
546                 free(listptr[i]);
547         }
548         *(--strptr) = '\0';
549
550         free(listptr);
551         return (str);
552 }