1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 1991-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <http://www.gnu.org/licenses/>. */
21 #include <stdio_ext.h>
24 #include <sys/sysmacros.h>
27 #include "linux_fsinfo.h"
28 #include <not-cancel.h>
30 static long int posix_pathconf (const char *file, int name);
32 /* Define this first, so it can be inlined. */
33 #define __pathconf static posix_pathconf
34 #include <sysdeps/posix/pathconf.c>
37 /* Get file-specific information about FILE. */
39 __pathconf (const char *file, int name)
46 return __statfs_link_max (__statfs (file, &fsbuf), &fsbuf, file, -1);
48 case _PC_FILESIZEBITS:
49 return __statfs_filesize_max (__statfs (file, &fsbuf), &fsbuf);
52 return __statfs_symlinks (__statfs (file, &fsbuf), &fsbuf);
54 case _PC_CHOWN_RESTRICTED:
55 return __statfs_chown_restricted (__statfs (file, &fsbuf), &fsbuf);
58 return posix_pathconf (file, name);
64 distinguish_extX (const struct statfs *fsbuf, const char *file, int fd)
70 if ((file == NULL ? fstat64 (fd, &st) : stat64 (file, &st)) != 0)
71 /* Strange. The statfd call worked, but stat fails. Default to
72 the more pessimistic value. */
75 __snprintf (buf, sizeof (buf), "/sys/dev/block/%u:%u",
76 gnu_dev_major (st.st_dev), gnu_dev_minor (st.st_dev));
78 ssize_t n = __readlink (buf, path, sizeof (path));
79 if (n != -1 && n < sizeof (path))
82 char *base = strdupa (basename (path));
83 __snprintf (path, sizeof (path), "/sys/fs/ext4/%s", base);
85 return __access (path, F_OK) == 0 ? EXT4_LINK_MAX : EXT2_LINK_MAX;
88 /* XXX Is there a better way to distinguish ext2/3 from ext4 than
89 iterating over the mounted filesystems and compare the device
91 FILE *mtab = __setmntent ("/proc/mounts", "r");
93 mtab = __setmntent (_PATH_MOUNTED, "r");
95 /* By default be conservative. */
96 long int result = EXT2_LINK_MAX;
102 /* No locking needed. */
103 (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
105 while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
107 if (strcmp (mntbuf.mnt_type, "ext2") != 0
108 && strcmp (mntbuf.mnt_type, "ext3") != 0
109 && strcmp (mntbuf.mnt_type, "ext4") != 0)
113 if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
114 && st.st_dev == fsst.st_dev)
116 if (strcmp (mntbuf.mnt_type, "ext4") == 0)
117 result = EXT4_LINK_MAX;
122 /* Close the file. */
130 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
132 __statfs_link_max (int result, const struct statfs *fsbuf, const char *file,
138 /* Not possible, return the default value. */
139 return LINUX_LINK_MAX;
141 /* Some error occured. */
145 switch (fsbuf->f_type)
147 case EXT2_SUPER_MAGIC:
148 /* Unfortunately the kernel does not return a different magic number
149 for ext4. This would be necessary to easily detect etx4 since it
150 has a different LINK_MAX value. Therefore we have to find it out
152 return distinguish_extX (fsbuf, file, fd);
154 case F2FS_SUPER_MAGIC:
155 return F2FS_LINK_MAX;
157 case MINIX_SUPER_MAGIC:
158 case MINIX_SUPER_MAGIC2:
159 return MINIX_LINK_MAX;
161 case MINIX2_SUPER_MAGIC:
162 case MINIX2_SUPER_MAGIC2:
163 return MINIX2_LINK_MAX;
165 case XENIX_SUPER_MAGIC:
166 return XENIX_LINK_MAX;
168 case SYSV4_SUPER_MAGIC:
169 case SYSV2_SUPER_MAGIC:
170 return SYSV_LINK_MAX;
172 case COH_SUPER_MAGIC:
179 case REISERFS_SUPER_MAGIC:
180 return REISERFS_LINK_MAX;
182 case XFS_SUPER_MAGIC:
185 case LUSTRE_SUPER_MAGIC:
186 return LUSTRE_LINK_MAX;
189 return LINUX_LINK_MAX;
194 /* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf); */
196 __statfs_filesize_max (int result, const struct statfs *fsbuf)
201 /* Not possible, return the default value. */
204 /* Some error occured. */
208 switch (fsbuf->f_type)
210 case F2FS_SUPER_MAGIC:
213 case BTRFS_SUPER_MAGIC:
216 case EXT2_SUPER_MAGIC:
219 case REISERFS_SUPER_MAGIC:
220 case XFS_SUPER_MAGIC:
221 case SMB_SUPER_MAGIC:
222 case NTFS_SUPER_MAGIC:
223 case UDF_SUPER_MAGIC:
224 case JFS_SUPER_MAGIC:
225 case VXFS_SUPER_MAGIC:
226 case CGROUP_SUPER_MAGIC:
227 case LUSTRE_SUPER_MAGIC:
230 case MSDOS_SUPER_MAGIC:
231 case JFFS_SUPER_MAGIC:
232 case JFFS2_SUPER_MAGIC:
233 case NCP_SUPER_MAGIC:
234 case ROMFS_SUPER_MAGIC:
243 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
245 __statfs_symlinks (int result, const struct statfs *fsbuf)
250 /* Not possible, return the default value. */
253 /* Some error occured. */
257 switch (fsbuf->f_type)
259 case ADFS_SUPER_MAGIC:
262 case DEVPTS_SUPER_MAGIC:
263 case EFS_SUPER_MAGIC:
265 case MSDOS_SUPER_MAGIC:
266 case NTFS_SUPER_MAGIC:
267 case QNX4_SUPER_MAGIC:
268 case ROMFS_SUPER_MAGIC:
269 /* No symlink support. */
278 /* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf);*/
280 __statfs_chown_restricted (int result, const struct statfs *fsbuf)
285 /* Not possible, return the default value. */
288 /* Some error occured. */
294 switch (fsbuf->f_type)
296 case XFS_SUPER_MAGIC:
297 /* Read the value from /proc/sys/fs/xfs/restrict_chown. If we cannot
298 read it default to assume the restriction is in place. */
299 fd = open_not_cancel_2 ("/proc/sys/fs/xfs/restrict_chown", O_RDONLY);
303 if (TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, 2)) == 2
304 && buf[0] >= '0' && buf[0] <= '1')
305 retval = buf[0] - '0';
307 close_not_cancel_no_status (fd);