5.2-RC: odd df(1) output

From: Mitsuru IWASAKI <iwasaki_at_jp.FreeBSD.org>
Date: Wed, 10 Dec 2003 14:35:33 +0900 (JST)
Hi, I've found a odd df(1) output when filesystem available space
is negative value.

% df -k /usr
Filesystem  1K-blocks     Used  Avail Capacity  Mounted on
/dev/ad0s1g  18084782 16722518 -84518   101%    /usr
% df -m /usr
Filesystem  1M-blocks  Used             Avail Capacity  Mounted on
/dev/ad0s1g     17660 16329 36028797018963886   101%    /usr

It seems that this happen in case df block size is bigger than
fs block size(eg. df -m on 2048-byte block filesystem).
And we can reproduce the problem with simple program, such as:
----
#include <sys/types.h>
#include <inttypes.h>

int
main()
{
        int64_t num = -58420;
        uint64_t fsbs = 2048;
        u_long bs = 1048576;
        printf("%jd\n", (intmax_t)(num) / ((bs) / (fsbs)));
        printf("%jd\n", (intmax_t)(num) / (intmax_t)((bs) / (fsbs)));
        return 0;

}
----

Quick fix is attached below.  Thanks

Index: df.c
===================================================================
RCS file: /home/ncvs/src/bin/df/df.c,v
retrieving revision 1.53
diff -u -r1.53 df.c
--- df.c	12 Nov 2003 21:47:42 -0000	1.53
+++ df.c	10 Dec 2003 05:26:48 -0000
_at__at_ -398,9 +398,15 _at__at_
  * Convert statfs returned file system size into BLOCKSIZE units.
  * Attempts to avoid overflow for large file systems.
  */
-#define fsbtoblk(num, fsbs, bs) \
-	(((fsbs) != 0 && (fsbs) < (bs)) ? \
-		(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
+static intmax_t 
+fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
+{
+	if (fsbs != 0 && fsbs < bs) {
+		return (intmax_t)(num / (intmax_t)(bs / fsbs));
+	} else {
+		return (intmax_t)(num * (fsbs / bs));
+	}
+}
 
 /*
  * Print out status about a file system.
Received on Tue Dec 09 2003 - 20:35:39 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:33 UTC