fsusage.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* fsusage.c -- return space usage of mounted file systems
  2. Copyright (C) 1991-1992, 1996, 1998-1999, 2002-2006, 2009-2013 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "fsusage.h"
  16. #include <limits.h>
  17. #include <sys/types.h>
  18. #if STAT_STATVFS || STAT_STATVFS64 /* POSIX 1003.1-2001 (and later) with XSI */
  19. # include <sys/statvfs.h>
  20. #else
  21. /* Don't include backward-compatibility files unless they're needed.
  22. Eventually we'd like to remove all this cruft. */
  23. # include <fcntl.h>
  24. # include <unistd.h>
  25. # include <sys/stat.h>
  26. #if HAVE_SYS_PARAM_H
  27. # include <sys/param.h>
  28. #endif
  29. #if HAVE_SYS_MOUNT_H
  30. # include <sys/mount.h>
  31. #endif
  32. #if HAVE_SYS_VFS_H
  33. # include <sys/vfs.h>
  34. #endif
  35. # if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */
  36. # include <sys/fs/s5param.h>
  37. # endif
  38. # if defined HAVE_SYS_FILSYS_H && !defined _CRAY
  39. # include <sys/filsys.h> /* SVR2 */
  40. # endif
  41. # if HAVE_SYS_STATFS_H
  42. # include <sys/statfs.h>
  43. # endif
  44. # if HAVE_DUSTAT_H /* AIX PS/2 */
  45. # include <sys/dustat.h>
  46. # endif
  47. # include "full-read.h"
  48. #endif
  49. /* The results of open() in this file are not used with fchdir,
  50. therefore save some unnecessary work in fchdir.c. */
  51. #undef open
  52. #undef close
  53. /* Many space usage primitives use all 1 bits to denote a value that is
  54. not applicable or unknown. Propagate this information by returning
  55. a uintmax_t value that is all 1 bits if X is all 1 bits, even if X
  56. is unsigned and narrower than uintmax_t. */
  57. #define PROPAGATE_ALL_ONES(x) \
  58. ((sizeof (x) < sizeof (uintmax_t) \
  59. && (~ (x) == (sizeof (x) < sizeof (int) \
  60. ? - (1 << (sizeof (x) * CHAR_BIT)) \
  61. : 0))) \
  62. ? UINTMAX_MAX : (uintmax_t) (x))
  63. /* Extract the top bit of X as an uintmax_t value. */
  64. #define EXTRACT_TOP_BIT(x) ((x) \
  65. & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1)))
  66. /* If a value is negative, many space usage primitives store it into an
  67. integer variable by assignment, even if the variable's type is unsigned.
  68. So, if a space usage variable X's top bit is set, convert X to the
  69. uintmax_t value V such that (- (uintmax_t) V) is the negative of
  70. the original value. If X's top bit is clear, just yield X.
  71. Use PROPAGATE_TOP_BIT if the original value might be negative;
  72. otherwise, use PROPAGATE_ALL_ONES. */
  73. #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))
  74. #ifdef STAT_STATVFS
  75. /* Return true if statvfs works. This is false for statvfs on systems
  76. with GNU libc on Linux kernels before 2.6.36, which stats all
  77. preceding entries in /proc/mounts; that makes df hang if even one
  78. of the corresponding file systems is hard-mounted but not available. */
  79. # if ! (__linux__ && (__GLIBC__ || __UCLIBC__))
  80. /* The FRSIZE fallback is not required in this case. */
  81. # undef STAT_STATFS2_FRSIZE
  82. static int statvfs_works (void) { return 1; }
  83. # else
  84. # include <string.h> /* for strverscmp */
  85. # include <sys/utsname.h>
  86. # include <sys/statfs.h>
  87. # define STAT_STATFS2_BSIZE 1
  88. static int
  89. statvfs_works (void)
  90. {
  91. static int statvfs_works_cache = -1;
  92. struct utsname name;
  93. if (statvfs_works_cache < 0)
  94. statvfs_works_cache = (uname (&name) == 0
  95. && 0 <= strverscmp (name.release, "2.6.36"));
  96. return statvfs_works_cache;
  97. }
  98. # endif
  99. #endif
  100. /* Fill in the fields of FSP with information about space usage for
  101. the file system on which FILE resides.
  102. DISK is the device on which FILE is mounted, for space-getting
  103. methods that need to know it.
  104. Return 0 if successful, -1 if not. When returning -1, ensure that
  105. ERRNO is either a system error value, or zero if DISK is NULL
  106. on a system that requires a non-NULL value. */
  107. int
  108. get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp)
  109. {
  110. #ifdef STAT_STATVFS /* POSIX, except pre-2.6.36 glibc/Linux */
  111. if (statvfs_works ())
  112. {
  113. struct statvfs vfsd;
  114. if (statvfs (file, &vfsd) < 0)
  115. return -1;
  116. /* f_frsize isn't guaranteed to be supported. */
  117. fsp->fsu_blocksize = (vfsd.f_frsize
  118. ? PROPAGATE_ALL_ONES (vfsd.f_frsize)
  119. : PROPAGATE_ALL_ONES (vfsd.f_bsize));
  120. fsp->fsu_blocks = PROPAGATE_ALL_ONES (vfsd.f_blocks);
  121. fsp->fsu_bfree = PROPAGATE_ALL_ONES (vfsd.f_bfree);
  122. fsp->fsu_bavail = PROPAGATE_TOP_BIT (vfsd.f_bavail);
  123. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (vfsd.f_bavail) != 0;
  124. fsp->fsu_files = PROPAGATE_ALL_ONES (vfsd.f_files);
  125. fsp->fsu_ffree = PROPAGATE_ALL_ONES (vfsd.f_ffree);
  126. return 0;
  127. }
  128. #endif
  129. #if defined STAT_STATVFS64 /* AIX */
  130. struct statvfs64 fsd;
  131. if (statvfs64 (file, &fsd) < 0)
  132. return -1;
  133. /* f_frsize isn't guaranteed to be supported. */
  134. fsp->fsu_blocksize = (fsd.f_frsize
  135. ? PROPAGATE_ALL_ONES (fsd.f_frsize)
  136. : PROPAGATE_ALL_ONES (fsd.f_bsize));
  137. #elif defined STAT_STATFS2_FS_DATA /* Ultrix */
  138. struct fs_data fsd;
  139. if (statfs (file, &fsd) != 1)
  140. return -1;
  141. fsp->fsu_blocksize = 1024;
  142. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.fd_req.btot);
  143. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.fd_req.bfree);
  144. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.fd_req.bfreen);
  145. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.fd_req.bfreen) != 0;
  146. fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.fd_req.gtot);
  147. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.fd_req.gfree);
  148. #elif defined STAT_READ_FILSYS /* SVR2 */
  149. # ifndef SUPERBOFF
  150. # define SUPERBOFF (SUPERB * 512)
  151. # endif
  152. struct filsys fsd;
  153. int fd;
  154. if (! disk)
  155. {
  156. errno = 0;
  157. return -1;
  158. }
  159. fd = open (disk, O_RDONLY);
  160. if (fd < 0)
  161. return -1;
  162. lseek (fd, (off_t) SUPERBOFF, 0);
  163. if (full_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
  164. {
  165. close (fd);
  166. return -1;
  167. }
  168. close (fd);
  169. fsp->fsu_blocksize = (fsd.s_type == Fs2b ? 1024 : 512);
  170. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.s_fsize);
  171. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.s_tfree);
  172. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.s_tfree);
  173. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.s_tfree) != 0;
  174. fsp->fsu_files = (fsd.s_isize == -1
  175. ? UINTMAX_MAX
  176. : (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1));
  177. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.s_tinode);
  178. #elif defined STAT_STATFS3_OSF1 /* OSF/1 */
  179. struct statfs fsd;
  180. if (statfs (file, &fsd, sizeof (struct statfs)) != 0)
  181. return -1;
  182. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
  183. #elif defined STAT_STATFS2_FRSIZE /* 2.6 < glibc/Linux < 2.6.36 */
  184. struct statfs fsd;
  185. if (statfs (file, &fsd) < 0)
  186. return -1;
  187. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_frsize);
  188. #elif defined STAT_STATFS2_BSIZE /* glibc/Linux < 2.6, 4.3BSD, SunOS 4, \
  189. Mac OS X < 10.4, FreeBSD < 5.0, \
  190. NetBSD < 3.0, OpenBSD < 4.4 */
  191. struct statfs fsd;
  192. if (statfs (file, &fsd) < 0)
  193. return -1;
  194. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
  195. # ifdef STATFS_TRUNCATES_BLOCK_COUNTS
  196. /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
  197. struct statfs are truncated to 2GB. These conditions detect that
  198. truncation, presumably without botching the 4.1.1 case, in which
  199. the values are not truncated. The correct counts are stored in
  200. undocumented spare fields. */
  201. if (fsd.f_blocks == 0x7fffffff / fsd.f_bsize && fsd.f_spare[0] > 0)
  202. {
  203. fsd.f_blocks = fsd.f_spare[0];
  204. fsd.f_bfree = fsd.f_spare[1];
  205. fsd.f_bavail = fsd.f_spare[2];
  206. }
  207. # endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
  208. #elif defined STAT_STATFS2_FSIZE /* 4.4BSD and older NetBSD */
  209. struct statfs fsd;
  210. if (statfs (file, &fsd) < 0)
  211. return -1;
  212. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
  213. #elif defined STAT_STATFS4 /* SVR3, Dynix, old Irix, old AIX, \
  214. Dolphin */
  215. # if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN
  216. # define f_bavail f_bfree
  217. # endif
  218. struct statfs fsd;
  219. if (statfs (file, &fsd, sizeof fsd, 0) < 0)
  220. return -1;
  221. /* Empirically, the block counts on most SVR3 and SVR3-derived
  222. systems seem to always be in terms of 512-byte blocks,
  223. no matter what value f_bsize has. */
  224. # if _AIX || defined _CRAY
  225. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
  226. # else
  227. fsp->fsu_blocksize = 512;
  228. # endif
  229. #endif
  230. #if (defined STAT_STATVFS64 || defined STAT_STATFS3_OSF1 \
  231. || defined STAT_STATFS2_FRSIZE || defined STAT_STATFS2_BSIZE \
  232. || defined STAT_STATFS2_FSIZE || defined STAT_STATFS4)
  233. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks);
  234. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree);
  235. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail);
  236. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0;
  237. fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files);
  238. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);
  239. #endif
  240. (void) disk; /* avoid argument-unused warning */
  241. return 0;
  242. }
  243. #if defined _AIX && defined _I386
  244. /* AIX PS/2 does not supply statfs. */
  245. int
  246. statfs (char *file, struct statfs *fsb)
  247. {
  248. struct stat stats;
  249. struct dustat fsd;
  250. if (stat (file, &stats) != 0)
  251. return -1;
  252. if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  253. return -1;
  254. fsb->f_type = 0;
  255. fsb->f_bsize = fsd.du_bsize;
  256. fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  257. fsb->f_bfree = fsd.du_tfree;
  258. fsb->f_bavail = fsd.du_tfree;
  259. fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
  260. fsb->f_ffree = fsd.du_tinode;
  261. fsb->f_fsid.val[0] = fsd.du_site;
  262. fsb->f_fsid.val[1] = fsd.du_pckno;
  263. return 0;
  264. }
  265. #endif /* _AIX && _I386 */