fsusage.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* fsusage.c -- return space usage of mounted file systems
  2. Copyright (C) 1991, 1992, 1996, 1998, 1999, 2002, 2003, 2004, 2005
  3. Free Software 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 2, or (at your option)
  7. 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, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #if HAVE_INTTYPES_H
  19. # include <inttypes.h>
  20. #endif
  21. #if HAVE_STDINT_H
  22. # include <stdint.h>
  23. #endif
  24. #include <unistd.h>
  25. #ifndef UINTMAX_MAX
  26. # define UINTMAX_MAX ((uintmax_t) -1)
  27. #endif
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include "fsusage.h"
  31. #include <limits.h>
  32. #if HAVE_SYS_PARAM_H
  33. # include <sys/param.h>
  34. #endif
  35. #if HAVE_SYS_MOUNT_H
  36. # include <sys/mount.h>
  37. #endif
  38. #if HAVE_SYS_VFS_H
  39. # include <sys/vfs.h>
  40. #endif
  41. #if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */
  42. # include <sys/fs/s5param.h>
  43. #endif
  44. #if defined HAVE_SYS_FILSYS_H && !defined _CRAY
  45. # include <sys/filsys.h> /* SVR2 */
  46. #endif
  47. #include <fcntl.h>
  48. #if HAVE_SYS_STATFS_H
  49. # include <sys/statfs.h>
  50. #endif
  51. #if HAVE_DUSTAT_H /* AIX PS/2 */
  52. # include <sys/dustat.h>
  53. #endif
  54. #if HAVE_SYS_STATVFS_H /* SVR4 */
  55. # include <sys/statvfs.h>
  56. #endif
  57. #include "full-read.h"
  58. /* Many space usage primitives use all 1 bits to denote a value that is
  59. not applicable or unknown. Propagate this information by returning
  60. a uintmax_t value that is all 1 bits if X is all 1 bits, even if X
  61. is unsigned and narrower than uintmax_t. */
  62. #define PROPAGATE_ALL_ONES(x) \
  63. ((sizeof (x) < sizeof (uintmax_t) \
  64. && (~ (x) == (sizeof (x) < sizeof (int) \
  65. ? - (1 << (sizeof (x) * CHAR_BIT)) \
  66. : 0))) \
  67. ? UINTMAX_MAX : (x))
  68. /* Extract the top bit of X as an uintmax_t value. */
  69. #define EXTRACT_TOP_BIT(x) ((x) \
  70. & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1)))
  71. /* If a value is negative, many space usage primitives store it into an
  72. integer variable by assignment, even if the variable's type is unsigned.
  73. So, if a space usage variable X's top bit is set, convert X to the
  74. uintmax_t value V such that (- (uintmax_t) V) is the negative of
  75. the original value. If X's top bit is clear, just yield X.
  76. Use PROPAGATE_TOP_BIT if the original value might be negative;
  77. otherwise, use PROPAGATE_ALL_ONES. */
  78. #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))
  79. /* Fill in the fields of FSP with information about space usage for
  80. the file system on which FILE resides.
  81. DISK is the device on which FILE is mounted, for space-getting
  82. methods that need to know it.
  83. Return 0 if successful, -1 if not. When returning -1, ensure that
  84. ERRNO is either a system error value, or zero if DISK is NULL
  85. on a system that requires a non-NULL value. */
  86. int
  87. get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp)
  88. {
  89. #ifdef STAT_STATFS3_OSF1
  90. struct statfs fsd;
  91. if (statfs (file, &fsd, sizeof (struct statfs)) != 0)
  92. return -1;
  93. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
  94. #endif /* STAT_STATFS3_OSF1 */
  95. #ifdef STAT_STATFS2_FS_DATA /* Ultrix */
  96. struct fs_data fsd;
  97. if (statfs (file, &fsd) != 1)
  98. return -1;
  99. fsp->fsu_blocksize = 1024;
  100. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.fd_req.btot);
  101. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.fd_req.bfree);
  102. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.fd_req.bfreen);
  103. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.fd_req.bfreen) != 0;
  104. fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.fd_req.gtot);
  105. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.fd_req.gfree);
  106. #endif /* STAT_STATFS2_FS_DATA */
  107. #ifdef STAT_READ_FILSYS /* SVR2 */
  108. # ifndef SUPERBOFF
  109. # define SUPERBOFF (SUPERB * 512)
  110. # endif
  111. struct filsys fsd;
  112. int fd;
  113. if (! disk)
  114. {
  115. errno = 0;
  116. return -1;
  117. }
  118. fd = open (disk, O_RDONLY);
  119. if (fd < 0)
  120. return -1;
  121. lseek (fd, (off_t) SUPERBOFF, 0);
  122. if (full_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
  123. {
  124. close (fd);
  125. return -1;
  126. }
  127. close (fd);
  128. fsp->fsu_blocksize = (fsd.s_type == Fs2b ? 1024 : 512);
  129. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.s_fsize);
  130. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.s_tfree);
  131. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.s_tfree);
  132. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.s_tfree) != 0;
  133. fsp->fsu_files = (fsd.s_isize == -1
  134. ? UINTMAX_MAX
  135. : (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1));
  136. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.s_tinode);
  137. #endif /* STAT_READ_FILSYS */
  138. #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
  139. struct statfs fsd;
  140. if (statfs (file, &fsd) < 0)
  141. return -1;
  142. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
  143. # ifdef STATFS_TRUNCATES_BLOCK_COUNTS
  144. /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
  145. struct statfs are truncated to 2GB. These conditions detect that
  146. truncation, presumably without botching the 4.1.1 case, in which
  147. the values are not truncated. The correct counts are stored in
  148. undocumented spare fields. */
  149. if (fsd.f_blocks == 0x7fffffff / fsd.f_bsize && fsd.f_spare[0] > 0)
  150. {
  151. fsd.f_blocks = fsd.f_spare[0];
  152. fsd.f_bfree = fsd.f_spare[1];
  153. fsd.f_bavail = fsd.f_spare[2];
  154. }
  155. # endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
  156. #endif /* STAT_STATFS2_BSIZE */
  157. #ifdef STAT_STATFS2_FSIZE /* 4.4BSD */
  158. struct statfs fsd;
  159. if (statfs (file, &fsd) < 0)
  160. return -1;
  161. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
  162. #endif /* STAT_STATFS2_FSIZE */
  163. #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
  164. # if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN
  165. # define f_bavail f_bfree
  166. # endif
  167. struct statfs fsd;
  168. if (statfs (file, &fsd, sizeof fsd, 0) < 0)
  169. return -1;
  170. /* Empirically, the block counts on most SVR3 and SVR3-derived
  171. systems seem to always be in terms of 512-byte blocks,
  172. no matter what value f_bsize has. */
  173. # if _AIX || defined _CRAY
  174. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
  175. # else
  176. fsp->fsu_blocksize = 512;
  177. # endif
  178. #endif /* STAT_STATFS4 */
  179. #ifdef STAT_STATVFS /* SVR4 */
  180. struct statvfs fsd;
  181. if (statvfs (file, &fsd) < 0)
  182. return -1;
  183. /* f_frsize isn't guaranteed to be supported. */
  184. fsp->fsu_blocksize = (fsd.f_frsize
  185. ? PROPAGATE_ALL_ONES (fsd.f_frsize)
  186. : PROPAGATE_ALL_ONES (fsd.f_bsize));
  187. #endif /* STAT_STATVFS */
  188. #if !defined STAT_STATFS2_FS_DATA && !defined STAT_READ_FILSYS
  189. /* !Ultrix && !SVR2 */
  190. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks);
  191. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree);
  192. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail);
  193. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0;
  194. fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files);
  195. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);
  196. #endif /* not STAT_STATFS2_FS_DATA && not STAT_READ_FILSYS */
  197. return 0;
  198. }
  199. #if defined _AIX && defined _I386
  200. /* AIX PS/2 does not supply statfs. */
  201. int
  202. statfs (char *file, struct statfs *fsb)
  203. {
  204. struct stat stats;
  205. struct dustat fsd;
  206. if (stat (file, &stats) != 0)
  207. return -1;
  208. if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  209. return -1;
  210. fsb->f_type = 0;
  211. fsb->f_bsize = fsd.du_bsize;
  212. fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  213. fsb->f_bfree = fsd.du_tfree;
  214. fsb->f_bavail = fsd.du_tfree;
  215. fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
  216. fsb->f_ffree = fsd.du_tinode;
  217. fsb->f_fsid.val[0] = fsd.du_site;
  218. fsb->f_fsid.val[1] = fsd.du_pckno;
  219. return 0;
  220. }
  221. #endif /* _AIX && _I386 */