fsusage.c 7.8 KB

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