1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
8 * Test for filesystem features on given mount point or device
9 * -c test for 32bit chown support (via libc)
10 * -t test for working rlimit/ftruncate64 (via libc)
11 * -q test for quota support (kernel compile option)
12 * -u test for user quota enforcement support (mount option)
13 * -p test for project quota enforcement support (mount option)
14 * -g test for group quota enforcement support (mount option)
15 * -U test for user quota accounting support (mount option)
16 * -G test for group quota accounting support (mount option)
17 * -P test for project quota accounting support (mount option)
18 * Return code: 0 is true, anything else is error/not supported
20 * Test for machine features
21 * -A test whether AIO syscalls are available
22 * -r test whether mount_setattr syscall is supported
23 * -R test whether IO_URING syscalls are available
24 * -o report a number of online cpus
26 * -w report bits per long
31 #include <sys/quota.h>
32 #include <sys/resource.h>
45 #ifdef HAVE_LIBURING_H
49 #include "idmapped-mounts/missing.h"
68 fprintf(stderr, "Usage: feature [-v] -<q|u|g|p|U|G|P> <filesystem>\n");
69 fprintf(stderr, " feature [-v] -c <file>\n");
70 fprintf(stderr, " feature [-v] -t <file>\n");
71 fprintf(stderr, " feature -A | -r | -R | -o | -s | -w\n");
75 int check_big_ID(char *filename)
79 memset(&sbuf, 0, sizeof(struct stat64));
80 if (lstat64(filename, &sbuf) < 0) {
81 fprintf(stderr, "lstat64 failed on ");
86 /* 98789 is greater than 2^16 (65536) */
87 if ((uint32_t)sbuf.st_uid == 98789 || (uint32_t)sbuf.st_gid == 98789)
90 fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
91 filename, (int)sbuf.st_uid, (int)sbuf.st_gid);
96 haschown32(char *filename)
98 if (check_big_ID(filename) == 0)
101 if (chown(filename, 98789, 98789) < 0) {
102 fprintf(stderr, "chown failed on ");
107 if (check_big_ID(filename) == 0)
113 hastruncate64(char *filename)
115 struct rlimit64 rlimit64;
116 off64_t bigoff = 4294967307LL; /* > 2^32 */
120 getrlimit64(RLIMIT_FSIZE, &rlimit64);
121 rlimit64.rlim_cur = RLIM64_INFINITY;
122 setrlimit64(RLIMIT_FSIZE, &rlimit64);
124 signal(SIGXFSZ, SIG_IGN);
126 if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
127 fprintf(stderr, "open failed on ");
132 if (ftruncate64(fd, bigoff) < 0)
135 if (fstat64(fd, &bigst) < 0) {
136 fprintf(stderr, "fstat64 failed on ");
142 fprintf(stderr, "fstat64 on %s gave sz=%lld (truncsz=%lld)\n",
143 filename, (long long)bigst.st_size, (long long)bigoff);
145 if (bigst.st_size != bigoff)
151 hasxfsquota(int type, int q, char *device)
153 fs_quota_stat_t qstat;
156 memset(&qstat, 0, sizeof(fs_quota_stat_t));
160 if (access("/proc/fs/xfs/xqm", F_OK) < 0) {
162 fprintf(stderr, "can't access /proc/fs/xfs/xqm\n");
168 qcmd = QCMD(Q_XGETQSTAT, type);
171 if (quotactl(Q_SYNC, device, 0, (caddr_t)&qstat) == ENOPKG) {
173 fprintf(stderr, "Q_SYNC not supported\n");
182 if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
187 else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
189 else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
191 else if (q == XFS_QUOTA_PDQ_ENFD && qstat.qs_flags & XFS_QUOTA_PDQ_ENFD)
193 else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
195 else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
197 else if (q == XFS_QUOTA_PDQ_ACCT && qstat.qs_flags & XFS_QUOTA_PDQ_ACCT)
200 fprintf(stderr, "quota type (%d) not available\n", q);
205 check_aio_support(void)
208 struct io_context *ctx = NULL;
211 err = io_setup(1, &ctx);
215 if (err == -ENOSYS) /* CONFIG_AIO=n */
218 fprintf(stderr, "unexpected error from io_setup(): %s\n",
222 /* libaio was unavailable at build time; assume AIO is unsupported */
228 check_uring_support(void)
230 #ifdef HAVE_LIBURING_H
231 struct io_uring ring;
234 err = io_uring_queue_init(1, &ring, 0);
238 if (err == -ENOSYS) /* CONFIG_IO_URING=n */
241 fprintf(stderr, "unexpected error from io_uring_queue_init(): %s\n",
245 /* liburing is unavailable, assume IO_URING is unsupported */
251 check_mount_setattr_support(void)
254 struct mount_attr attr = {
255 .attr_set = MOUNT_ATTR_IDMAP,
259 /* mount_setattr() syscall not supported. */
260 err = sys_mount_setattr(-EBADF, "", AT_EMPTY_PATH, NULL, 0);
261 if (err && errno == ENOSYS)
264 /* idmapped mounts not supported */
265 err = sys_mount_setattr(-EBADF, ".", AT_EMPTY_PATH, &attr, sizeof(attr));
266 if (err && errno == E2BIG)
273 main(int argc, char **argv)
293 while ((c = getopt(argc, argv, "ActgGopPqrRsuUvw")) != EOF) {
348 /* filesystem features */
349 if (cflag|tflag|uflag|gflag|pflag|qflag|Uflag|Gflag|Pflag) {
350 if (optind != argc-1) /* need a device */
353 } else if (Aflag || rflag || Rflag || wflag || sflag || oflag) {
360 return(haschown32(fs));
362 return(hastruncate64(fs));
364 return(hasxfsquota(0, 0, fs));
366 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
368 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ENFD, fs));
370 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
372 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
374 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ACCT, fs));
376 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
379 return(check_aio_support());
382 return(check_mount_setattr_support());
385 return(check_uring_support());
388 printf("%d\n", getpagesize());
393 printf("%d\n", BITS_PER_LONG);
396 /* This can change under IRIX depending on whether we compile
397 * with -n32/-32 or -64
399 printf("%d\n", (int)(NBBY * sizeof(long)));
409 #if defined(_SC_NPROCESSORS_ONLN)
410 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
411 #elif defined(_SC_NPROC_ONLN)
412 ncpus = sysconf(_SC_NPROC_ONLN);
417 printf("%ld\n", ncpus);
422 fprintf(stderr, "feature: dunno what you're after.\n");