xfs/194: actually check if we got the desired block size before proceeding
[xfstests-dev.git] / src / feature.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 /*
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
19  *
20  * Test for machine features
21  *   -A  test whether AIO syscalls are available
22  *   -o  report a number of online cpus
23  *   -s  report pagesize
24  *   -w  report bits per long
25  */
26
27 #include "global.h"
28
29 #include <sys/quota.h>
30 #include <sys/resource.h>
31 #include <signal.h>
32 #include <unistd.h>
33
34 #ifdef HAVE_XFS_XQM_H
35 #include <xfs/xqm.h>
36 #endif
37
38 #ifdef HAVE_LIBAIO_H
39 #include <libaio.h>
40 #endif
41
42 #ifndef USRQUOTA
43 #define USRQUOTA  0
44 #endif
45
46 #ifndef GRPQUOTA
47 #define GRPQUOTA  1
48 #endif
49
50 #ifndef PRJQUOTA
51 #define PRJQUOTA  2
52 #endif
53
54 int verbose = 0;
55
56 void
57 usage(void)
58 {
59         fprintf(stderr, "Usage: feature [-v] -<q|u|g|p|U|G|P> <filesystem>\n");
60         fprintf(stderr, "       feature [-v] -c <file>\n");
61         fprintf(stderr, "       feature [-v] -t <file>\n");
62         fprintf(stderr, "       feature -A | -o | -s | -w\n");
63         exit(1);
64 }
65
66 int check_big_ID(char *filename)
67 {
68         struct stat64   sbuf;
69
70         memset(&sbuf, 0, sizeof(struct stat64));
71         if (lstat64(filename, &sbuf) < 0) {
72                 fprintf(stderr, "lstat64 failed on ");
73                 perror(filename);
74                 return(1);
75         }
76
77         /* 98789 is greater than 2^16 (65536) */
78         if ((uint32_t)sbuf.st_uid == 98789 || (uint32_t)sbuf.st_gid == 98789)
79                 return(0);
80         if (verbose)
81                 fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
82                         filename, (int)sbuf.st_uid, (int)sbuf.st_gid);
83         return(1);
84 }
85
86 int
87 haschown32(char *filename)
88 {
89         if (check_big_ID(filename) == 0)
90                 return(0);
91
92         if (chown(filename, 98789, 98789) < 0) {
93                 fprintf(stderr, "chown failed on ");
94                 perror(filename);
95                 return(1);
96         }
97
98         if (check_big_ID(filename) == 0)
99                 return(0);
100         return (1);
101 }
102
103 int
104 hastruncate64(char *filename)
105 {
106         struct rlimit64 rlimit64;
107         off64_t bigoff = 4294967307LL;  /* > 2^32 */
108         struct stat64 bigst;
109         int fd;
110
111         getrlimit64(RLIMIT_FSIZE, &rlimit64);
112         rlimit64.rlim_cur = RLIM64_INFINITY;
113         setrlimit64(RLIMIT_FSIZE, &rlimit64);
114
115         signal(SIGXFSZ, SIG_IGN);
116
117         if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
118                 fprintf(stderr, "open failed on ");
119                 perror(filename);
120                 return(1);
121         }
122
123         if (ftruncate64(fd, bigoff) < 0)
124                 return(1);
125
126         if (fstat64(fd, &bigst) < 0) {
127                 fprintf(stderr, "fstat64 failed on ");
128                 perror(filename);
129                 return(1);
130         }
131
132         if (verbose)
133                 fprintf(stderr, "fstat64 on %s gave sz=%lld (truncsz=%lld)\n",
134                         filename, (long long)bigst.st_size, (long long)bigoff);
135
136         if (bigst.st_size != bigoff)
137                 return(1);
138         return(0);
139 }
140
141 int
142 hasxfsquota(int type, int q, char *device)
143 {
144         fs_quota_stat_t qstat;
145         int             qcmd;
146
147         memset(&qstat, 0, sizeof(fs_quota_stat_t));
148
149 #ifdef QCMD
150         if (q == 0) {
151                 if (access("/proc/fs/xfs/xqm", F_OK) < 0) {
152                         if (verbose) {
153                                 fprintf(stderr, "can't access /proc/fs/xfs/xqm\n");
154                         }
155                         return 1;
156                 }
157                 return 0;
158         }
159         qcmd = QCMD(Q_XGETQSTAT, type);
160 #else
161         if (q == 0) {
162                 if (quotactl(Q_SYNC, device, 0, (caddr_t)&qstat) == ENOPKG) {
163                         if (verbose) {
164                                 fprintf(stderr, "Q_SYNC not supported\n");
165                         }
166                         return 1;
167                 }
168                 return 0;
169         }
170         qcmd = Q_GETQSTAT;
171 #endif
172
173         if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
174                 if (verbose)
175                         perror("quotactl");
176                 return (1);
177         }
178         else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
179                 return (0);
180         else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
181                 return (0);
182         else if (q == XFS_QUOTA_PDQ_ENFD && qstat.qs_flags & XFS_QUOTA_PDQ_ENFD)
183                 return (0);
184         else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
185                 return (0);
186         else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
187                 return (0);
188         else if (q == XFS_QUOTA_PDQ_ACCT && qstat.qs_flags & XFS_QUOTA_PDQ_ACCT)
189                 return (0);
190         if (verbose)
191                 fprintf(stderr, "quota type (%d) not available\n", q);
192         return (1);
193 }
194
195 static int
196 check_aio_support(void)
197 {
198 #ifdef HAVE_LIBAIO_H
199         struct io_context *ctx = NULL;
200         int err;
201
202         err = io_setup(1, &ctx);
203         if (err == 0)
204                 return 0;
205
206         if (err == -ENOSYS) /* CONFIG_AIO=n */
207                 return 1;
208
209         fprintf(stderr, "unexpected error from io_setup(): %s\n",
210                 strerror(-err));
211         return 2;
212 #else
213         /* libaio was unavailable at build time; assume AIO is unsupported */
214         return 1;
215 #endif
216 }
217
218
219 int
220 main(int argc, char **argv)
221 {
222         int     c;
223         int     Aflag = 0;
224         int     cflag = 0;
225         int     tflag = 0;
226         int     gflag = 0;
227         int     Gflag = 0;
228         int     pflag = 0;
229         int     Pflag = 0;
230         int     qflag = 0;
231         int     sflag = 0;
232         int     uflag = 0;
233         int     Uflag = 0;
234         int     wflag = 0;
235         int     oflag = 0;
236         char    *fs = NULL;
237
238         while ((c = getopt(argc, argv, "ActgGopPqsuUvw")) != EOF) {
239                 switch (c) {
240                 case 'A':
241                         Aflag++;
242                         break;
243                 case 'c':
244                         cflag++;
245                         break;
246                 case 't':
247                         tflag++;
248                         break;
249                 case 'g':
250                         gflag++;
251                         break;
252                 case 'G':
253                         Gflag++;
254                         break;
255                 case 'o':
256                         oflag++;
257                         break;
258                 case 'p':
259                         pflag++;
260                         break;
261                 case 'P':
262                         Pflag++;
263                         break;
264                 case 'q':
265                         qflag++;
266                         break;
267                 case 's':
268                         sflag++;
269                         break;
270                 case 'u':
271                         uflag++;
272                         break;
273                 case 'U':
274                         Uflag++;
275                         break;
276                 case 'w':
277                         wflag++;
278                         break;
279                 case 'v':
280                         verbose++;
281                         break;
282                 default:
283                         usage();
284                 }
285         }
286
287         /* filesystem features */
288         if (cflag|tflag|uflag|gflag|pflag|qflag|Uflag|Gflag|Pflag) {
289                 if (optind != argc-1)   /* need a device */
290                         usage();
291                 fs = argv[argc-1];
292         } else if (Aflag || wflag || sflag || oflag) {
293                 if (optind != argc)
294                         usage();
295         } else 
296                 usage();
297
298         if (cflag)
299                 return(haschown32(fs));
300         if (tflag)
301                 return(hastruncate64(fs));
302         if (qflag)
303                 return(hasxfsquota(0, 0, fs));
304         if (gflag)
305                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
306         if (pflag)
307                 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ENFD, fs));
308         if (uflag)
309                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
310         if (Gflag)
311                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
312         if (Pflag)
313                 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ACCT, fs));
314         if (Uflag)
315                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
316
317         if (Aflag)
318                 return(check_aio_support());
319
320         if (sflag) {
321                 printf("%d\n", getpagesize());
322                 exit(0);
323         }
324         if (wflag) {
325 #ifdef BITS_PER_LONG
326                 printf("%d\n", BITS_PER_LONG);
327 #else
328 #ifdef NBBY
329                 /* This can change under IRIX depending on whether we compile
330                  * with -n32/-32 or -64
331                  */
332                 printf("%d\n", (int)(NBBY * sizeof(long)));
333 #else
334 bozo!
335 #endif
336 #endif
337                 exit(0);
338         }
339         if (oflag) {
340                 long ncpus = -1;
341
342 #if defined(_SC_NPROCESSORS_ONLN)
343                 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
344 #elif defined(_SC_NPROC_ONLN)
345                 ncpus = sysconf(_SC_NPROC_ONLN);
346 #endif
347                 if (ncpus == -1)
348                         ncpus = 1;
349
350                 printf("%ld\n", ncpus);
351
352                 exit(0);
353         }
354
355         fprintf(stderr, "feature: dunno what you're after.\n");
356         return(1);
357 }