1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 1991-2024 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 <https://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
)
68 struct __stat64_t64 st
;
70 if ((file
== NULL
? __fstat64_time64 (fd
, &st
)
71 : __stat64_time64 (file
, &st
)) != 0)
72 /* Strange. The statfd call worked, but stat fails. Default to
73 the more pessimistic value. */
76 __snprintf (buf
, sizeof (buf
), "/sys/dev/block/%u:%u",
77 __gnu_dev_major (st
.st_dev
), __gnu_dev_minor (st
.st_dev
));
79 ssize_t n
= __readlink (buf
, path
, sizeof (path
));
80 if (n
!= -1 && n
< sizeof (path
))
83 char *base
= strdupa (__basename (path
));
84 __snprintf (path
, sizeof (path
), "/sys/fs/ext4/%s", base
);
86 return __access (path
, F_OK
) == 0 ? EXT4_LINK_MAX
: EXT2_LINK_MAX
;
89 /* XXX Is there a better way to distinguish ext2/3 from ext4 than
90 iterating over the mounted filesystems and compare the device
92 FILE *mtab
= __setmntent ("/proc/mounts", "r");
94 mtab
= __setmntent (_PATH_MOUNTED
, "r");
96 /* By default be conservative. */
97 long int result
= EXT2_LINK_MAX
;
100 struct mntent mntbuf
;
103 /* No locking needed. */
104 (void) __fsetlocking (mtab
, FSETLOCKING_BYCALLER
);
106 while (__getmntent_r (mtab
, &mntbuf
, tmpbuf
, sizeof (tmpbuf
)))
108 if (strcmp (mntbuf
.mnt_type
, "ext2") != 0
109 && strcmp (mntbuf
.mnt_type
, "ext3") != 0
110 && strcmp (mntbuf
.mnt_type
, "ext4") != 0)
113 struct __stat64_t64 fsst
;
114 if (__stat64_time64 (mntbuf
.mnt_dir
, &fsst
) >= 0
115 && st
.st_dev
== fsst
.st_dev
)
117 if (strcmp (mntbuf
.mnt_type
, "ext4") == 0)
118 result
= EXT4_LINK_MAX
;
123 /* Close the file. */
131 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
133 __statfs_link_max (int result
, const struct statfs
*fsbuf
, const char *file
,
139 /* Not possible, return the default value. */
140 return LINUX_LINK_MAX
;
142 /* Some error occurred. */
146 switch (fsbuf
->f_type
)
148 case EXT2_SUPER_MAGIC
:
149 /* Unfortunately the kernel does not return a different magic number
150 for ext4. This would be necessary to easily detect etx4 since it
151 has a different LINK_MAX value. Therefore we have to find it out
153 return distinguish_extX (fsbuf
, file
, fd
);
155 case F2FS_SUPER_MAGIC
:
156 return F2FS_LINK_MAX
;
158 case MINIX_SUPER_MAGIC
:
159 case MINIX_SUPER_MAGIC2
:
160 return MINIX_LINK_MAX
;
162 case MINIX2_SUPER_MAGIC
:
163 case MINIX2_SUPER_MAGIC2
:
164 return MINIX2_LINK_MAX
;
166 case XENIX_SUPER_MAGIC
:
167 return XENIX_LINK_MAX
;
169 case SYSV4_SUPER_MAGIC
:
170 case SYSV2_SUPER_MAGIC
:
171 return SYSV_LINK_MAX
;
173 case COH_SUPER_MAGIC
:
180 case REISERFS_SUPER_MAGIC
:
181 return REISERFS_LINK_MAX
;
183 case XFS_SUPER_MAGIC
:
186 case LUSTRE_SUPER_MAGIC
:
187 return LUSTRE_LINK_MAX
;
190 return LINUX_LINK_MAX
;
195 /* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf); */
197 __statfs_filesize_max (int result
, const struct statfs
*fsbuf
)
202 /* Not possible, return the default value. */
205 /* Some error occurred. */
209 switch (fsbuf
->f_type
)
211 case F2FS_SUPER_MAGIC
:
214 case BTRFS_SUPER_MAGIC
:
217 case EXT2_SUPER_MAGIC
:
220 case REISERFS_SUPER_MAGIC
:
221 case XFS_SUPER_MAGIC
:
222 case SMB_SUPER_MAGIC
:
223 case NTFS_SUPER_MAGIC
:
224 case UDF_SUPER_MAGIC
:
225 case JFS_SUPER_MAGIC
:
226 case VXFS_SUPER_MAGIC
:
227 case CGROUP_SUPER_MAGIC
:
228 case LUSTRE_SUPER_MAGIC
:
231 case MSDOS_SUPER_MAGIC
:
232 case JFFS_SUPER_MAGIC
:
233 case JFFS2_SUPER_MAGIC
:
234 case NCP_SUPER_MAGIC
:
235 case ROMFS_SUPER_MAGIC
:
244 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
246 __statfs_symlinks (int result
, const struct statfs
*fsbuf
)
251 /* Not possible, return the default value. */
254 /* Some error occurred. */
258 switch (fsbuf
->f_type
)
260 case ADFS_SUPER_MAGIC
:
263 case DEVPTS_SUPER_MAGIC
:
264 case EFS_SUPER_MAGIC
:
266 case MSDOS_SUPER_MAGIC
:
267 case NTFS_SUPER_MAGIC
:
268 case QNX4_SUPER_MAGIC
:
269 case ROMFS_SUPER_MAGIC
:
270 /* No symlink support. */
279 /* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf);*/
281 __statfs_chown_restricted (int result
, const struct statfs
*fsbuf
)
286 /* Not possible, return the default value. */
289 /* Some error occurred. */