1 /* Copyright (C) 1998-2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <stdio_ext.h>
27 #include <sys/mount.h>
29 #include <sys/statfs.h>
30 #include <sys/statvfs.h>
31 #include "linux_fsinfo.h"
35 # define STATFS statfs
36 # define STATVFS statvfs
37 # define INTERNAL_STATVFS __internal_statvfs
41 __statvfs_getflags (const char *name
, int fstype
, struct stat64
*st
)
46 const char *fsname
= NULL
;
47 const char *fsname2
= NULL
;
49 /* Map the filesystem type we got from the statfs call to a string. */
52 case EXT2_SUPER_MAGIC
:
56 case DEVPTS_SUPER_MAGIC
:
59 case SHMFS_SUPER_MAGIC
:
62 case PROC_SUPER_MAGIC
:
65 case USBDEVFS_SUPER_MAGIC
:
68 case AUTOFS_SUPER_MAGIC
:
76 FILE *mtab
= __setmntent ("/proc/mounts", "r");
78 mtab
= __setmntent (_PATH_MOUNTED
, "r");
87 /* No locking needed. */
88 (void) __fsetlocking (mtab
, FSETLOCKING_BYCALLER
);
91 while (__getmntent_r (mtab
, &mntbuf
, tmpbuf
, sizeof (tmpbuf
)))
93 /* In a first round we look for a given mount point, if
95 if (name
!= NULL
&& strcmp (name
, mntbuf
.mnt_dir
) != 0)
97 /* We need to look at the entry only if the filesystem
98 name matches. If we have a filesystem name. */
99 else if (fsname
!= NULL
100 && strcmp (fsname
, mntbuf
.mnt_type
) != 0
102 || strcmp (fsname2
, mntbuf
.mnt_type
) != 0))
105 /* Find out about the device the current entry is for. */
107 if (stat64 (mntbuf
.mnt_dir
, &fsst
) >= 0
108 && st
->st_dev
== fsst
.st_dev
)
110 /* Bingo, we found the entry for the device FD is on.
111 Now interpret the option string. */
112 char *cp
= mntbuf
.mnt_opts
;
115 while ((opt
= strsep (&cp
, ",")) != NULL
)
116 if (strcmp (opt
, "ro") == 0)
118 else if (strcmp (opt
, "nosuid") == 0)
120 else if (strcmp (opt
, "noexec") == 0)
122 else if (strcmp (opt
, "nodev") == 0)
124 else if (strcmp (opt
, "sync") == 0)
125 result
|= ST_SYNCHRONOUS
;
126 else if (strcmp (opt
, "mand") == 0)
127 result
|= ST_MANDLOCK
;
128 else if (strcmp (opt
, "noatime") == 0)
129 result
|= ST_NOATIME
;
130 else if (strcmp (opt
, "nodiratime") == 0)
131 result
|= ST_NODIRATIME
;
133 /* We can stop looking for more entries. */
138 /* Maybe the kernel names for the filesystems changed or the
139 statvfs call got a name which was not the mount point. Check
140 again, this time without checking for name matches first. */
141 if (! success
&& (name
!= NULL
|| fsname
!= NULL
))
144 /* Try without a mount point name. */
148 /* Try without a filesystem name. */
149 assert (fsname
!= NULL
);
150 fsname
= fsname2
= NULL
;
153 /* It is not strictly allowed to use rewind here. But
154 this code is part of the implementation so it is
161 /* Close the file. */
168 extern int __statvfs_getflags (const char *name
, int fstype
,
174 INTERNAL_STATVFS (const char *name
, struct STATVFS
*buf
,
175 struct STATFS
*fsbuf
, struct stat64
*st
)
177 /* Now fill in the fields we have information for. */
178 buf
->f_bsize
= fsbuf
->f_bsize
;
179 /* Linux has the f_frsize size only in later version of the kernel.
180 If the value is not filled in use f_bsize. */
181 buf
->f_frsize
= fsbuf
->f_frsize
?: fsbuf
->f_bsize
;
182 buf
->f_blocks
= fsbuf
->f_blocks
;
183 buf
->f_bfree
= fsbuf
->f_bfree
;
184 buf
->f_bavail
= fsbuf
->f_bavail
;
185 buf
->f_files
= fsbuf
->f_files
;
186 buf
->f_ffree
= fsbuf
->f_ffree
;
187 if (sizeof (buf
->f_fsid
) == sizeof (fsbuf
->f_fsid
))
188 buf
->f_fsid
= (fsbuf
->f_fsid
.__val
[0]
189 | ((unsigned long int) fsbuf
->f_fsid
.__val
[1]
190 << (8 * (sizeof (buf
->f_fsid
)
191 - sizeof (fsbuf
->f_fsid
.__val
[0])))));
193 /* We cannot help here. The statvfs element is not large enough to
194 contain both words of the statfs f_fsid field. */
195 buf
->f_fsid
= fsbuf
->f_fsid
.__val
[0];
196 #ifdef _STATVFSBUF_F_UNUSED
199 buf
->f_namemax
= fsbuf
->f_namelen
;
200 memset (buf
->__f_spare
, '\0', sizeof (buf
->__f_spare
));
202 /* What remains to do is to fill the fields f_favail and f_flag. */
204 /* XXX I have no idea how to compute f_favail. Any idea??? */
205 buf
->f_favail
= buf
->f_ffree
;
207 /* Determining the flags is tricky. We have to read /proc/mounts or
208 the /etc/mtab file and search for the entry which matches the given
209 file. The way we can test for matching filesystem is using the
211 buf
->f_flag
= __statvfs_getflags (name
, fsbuf
->f_type
, st
);