2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / internal_statvfs.c
blob28c1cb691fa1b329cf5fee60cd493b56ff85e4bd
1 /* Copyright (C) 1998-2003, 2004, 2005, 2006 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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <mntent.h>
23 #include <paths.h>
24 #include <stdbool.h>
25 #include <stdio_ext.h>
26 #include <string.h>
27 #include <sys/mount.h>
28 #include <sys/stat.h>
29 #include <sys/statfs.h>
30 #include <sys/statvfs.h>
31 #include "linux_fsinfo.h"
34 #ifndef STATFS
35 # define STATFS statfs
36 # define STATVFS statvfs
37 # define INTERNAL_STATVFS __internal_statvfs
40 int
41 __statvfs_getflags (const char *name, int fstype, struct stat64 *st)
43 if (st == NULL)
44 return 0;
46 const char *fsname = NULL;
47 const char *fsname2 = NULL;
49 /* Map the filesystem type we got from the statfs call to a string. */
50 switch (fstype)
52 case EXT2_SUPER_MAGIC:
53 fsname = "ext3";
54 fsname2 = "ext2";
55 break;
56 case DEVPTS_SUPER_MAGIC:
57 fsname= "devpts";
58 break;
59 case SHMFS_SUPER_MAGIC:
60 fsname = "tmpfs";
61 break;
62 case PROC_SUPER_MAGIC:
63 fsname = "proc";
64 break;
65 case USBDEVFS_SUPER_MAGIC:
66 fsname = "usbdevfs";
67 break;
68 case AUTOFS_SUPER_MAGIC:
69 fsname = "autofs";
70 break;
71 case NFS_SUPER_MAGIC:
72 fsname = "nfs";
73 break;
74 case SYSFS_MAGIC:
75 fsname = "sysfs";
76 break;
77 case REISERFS_SUPER_MAGIC:
78 fsname = "reiserfs";
79 break;
80 case XFS_SUPER_MAGIC:
81 fsname = "xfs";
82 break;
83 case JFS_SUPER_MAGIC:
84 fsname = "jfs";
85 break;
86 case HPFS_SUPER_MAGIC:
87 fsname = "hpfs";
88 break;
89 case DEVFS_SUPER_MAGIC:
90 fsname = "devfs";
91 break;
92 case ISOFS_SUPER_MAGIC:
93 fsname = "iso9660";
94 break;
95 case MSDOS_SUPER_MAGIC:
96 fsname = "msdos";
97 break;
98 case NTFS_SUPER_MAGIC:
99 fsname = "ntfs";
100 break;
103 FILE *mtab = __setmntent ("/proc/mounts", "r");
104 if (mtab == NULL)
105 mtab = __setmntent (_PATH_MOUNTED, "r");
107 int result = 0;
108 if (mtab != NULL)
110 bool success = false;
111 struct mntent mntbuf;
112 char tmpbuf[1024];
114 /* No locking needed. */
115 (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
117 again:
118 while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
120 /* In a first round we look for a given mount point, if
121 we have a name. */
122 if (name != NULL && strcmp (name, mntbuf.mnt_dir) != 0)
123 continue;
124 /* We need to look at the entry only if the filesystem
125 name matches. If we have a filesystem name. */
126 else if (fsname != NULL
127 && strcmp (fsname, mntbuf.mnt_type) != 0
128 && (fsname2 == NULL
129 || strcmp (fsname2, mntbuf.mnt_type) != 0))
130 continue;
132 /* Find out about the device the current entry is for. */
133 struct stat64 fsst;
134 if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
135 && st->st_dev == fsst.st_dev)
137 /* Bingo, we found the entry for the device FD is on.
138 Now interpret the option string. */
139 char *cp = mntbuf.mnt_opts;
140 char *opt;
142 while ((opt = strsep (&cp, ",")) != NULL)
143 if (strcmp (opt, "ro") == 0)
144 result |= ST_RDONLY;
145 else if (strcmp (opt, "nosuid") == 0)
146 result |= ST_NOSUID;
147 else if (strcmp (opt, "noexec") == 0)
148 result |= ST_NOEXEC;
149 else if (strcmp (opt, "nodev") == 0)
150 result |= ST_NODEV;
151 else if (strcmp (opt, "sync") == 0)
152 result |= ST_SYNCHRONOUS;
153 else if (strcmp (opt, "mand") == 0)
154 result |= ST_MANDLOCK;
155 else if (strcmp (opt, "noatime") == 0)
156 result |= ST_NOATIME;
157 else if (strcmp (opt, "nodiratime") == 0)
158 result |= ST_NODIRATIME;
159 else if (strcmp (opt, "relatime") == 0)
160 result |= ST_RELATIME;
162 /* We can stop looking for more entries. */
163 success = true;
164 break;
167 /* Maybe the kernel names for the filesystems changed or the
168 statvfs call got a name which was not the mount point. Check
169 again, this time without checking for name matches first. */
170 if (! success && (name != NULL || fsname != NULL))
172 if (name != NULL)
173 /* Try without a mount point name. */
174 name = NULL;
175 else
177 /* Try without a filesystem name. */
178 assert (fsname != NULL);
179 fsname = fsname2 = NULL;
182 /* It is not strictly allowed to use rewind here. But
183 this code is part of the implementation so it is
184 acceptable. */
185 rewind (mtab);
187 goto again;
190 /* Close the file. */
191 __endmntent (mtab);
194 return result;
196 #else
197 extern int __statvfs_getflags (const char *name, int fstype,
198 struct stat64 *st);
199 #endif
202 void
203 INTERNAL_STATVFS (const char *name, struct STATVFS *buf,
204 struct STATFS *fsbuf, struct stat64 *st)
206 /* Now fill in the fields we have information for. */
207 buf->f_bsize = fsbuf->f_bsize;
208 /* Linux has the f_frsize size only in later version of the kernel.
209 If the value is not filled in use f_bsize. */
210 buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize;
211 buf->f_blocks = fsbuf->f_blocks;
212 buf->f_bfree = fsbuf->f_bfree;
213 buf->f_bavail = fsbuf->f_bavail;
214 buf->f_files = fsbuf->f_files;
215 buf->f_ffree = fsbuf->f_ffree;
216 if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid))
217 buf->f_fsid = (fsbuf->f_fsid.__val[0]
218 | ((unsigned long int) fsbuf->f_fsid.__val[1]
219 << (8 * (sizeof (buf->f_fsid)
220 - sizeof (fsbuf->f_fsid.__val[0])))));
221 else
222 /* We cannot help here. The statvfs element is not large enough to
223 contain both words of the statfs f_fsid field. */
224 buf->f_fsid = fsbuf->f_fsid.__val[0];
225 #ifdef _STATVFSBUF_F_UNUSED
226 buf->__f_unused = 0;
227 #endif
228 buf->f_namemax = fsbuf->f_namelen;
229 memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
231 /* What remains to do is to fill the fields f_favail and f_flag. */
233 /* XXX I have no idea how to compute f_favail. Any idea??? */
234 buf->f_favail = buf->f_ffree;
236 /* Determining the flags is tricky. We have to read /proc/mounts or
237 the /etc/mtab file and search for the entry which matches the given
238 file. The way we can test for matching filesystem is using the
239 device number. */
240 buf->f_flag = __statvfs_getflags (name, fsbuf->f_type, st);