xfs: Verify correctness of upgrading an fs to support large extent counters
[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  *   -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
25  *   -s  report pagesize
26  *   -w  report bits per long
27  */
28
29 #include "global.h"
30
31 #include <sys/quota.h>
32 #include <sys/resource.h>
33 #include <signal.h>
34 #include <syscall.h>
35 #include <unistd.h>
36
37 #ifdef HAVE_XFS_XQM_H
38 #include <xfs/xqm.h>
39 #endif
40
41 #ifdef HAVE_LIBAIO_H
42 #include <libaio.h>
43 #endif
44
45 #ifdef HAVE_LIBURING_H
46 #include <liburing.h>
47 #endif
48
49 #include "vfs/missing.h"
50
51 #ifndef USRQUOTA
52 #define USRQUOTA  0
53 #endif
54
55 #ifndef GRPQUOTA
56 #define GRPQUOTA  1
57 #endif
58
59 #ifndef PRJQUOTA
60 #define PRJQUOTA  2
61 #endif
62
63 int verbose = 0;
64
65 void
66 usage(void)
67 {
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");
72         exit(1);
73 }
74
75 int check_big_ID(char *filename)
76 {
77         struct stat64   sbuf;
78
79         memset(&sbuf, 0, sizeof(struct stat64));
80         if (lstat64(filename, &sbuf) < 0) {
81                 fprintf(stderr, "lstat64 failed on ");
82                 perror(filename);
83                 return(1);
84         }
85
86         /* 98789 is greater than 2^16 (65536) */
87         if ((uint32_t)sbuf.st_uid == 98789 || (uint32_t)sbuf.st_gid == 98789)
88                 return(0);
89         if (verbose)
90                 fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
91                         filename, (int)sbuf.st_uid, (int)sbuf.st_gid);
92         return(1);
93 }
94
95 int
96 haschown32(char *filename)
97 {
98         if (check_big_ID(filename) == 0)
99                 return(0);
100
101         if (chown(filename, 98789, 98789) < 0) {
102                 fprintf(stderr, "chown failed on ");
103                 perror(filename);
104                 return(1);
105         }
106
107         if (check_big_ID(filename) == 0)
108                 return(0);
109         return (1);
110 }
111
112 int
113 hastruncate64(char *filename)
114 {
115         struct rlimit64 rlimit64;
116         off64_t bigoff = 4294967307LL;  /* > 2^32 */
117         struct stat64 bigst;
118         int fd;
119
120         getrlimit64(RLIMIT_FSIZE, &rlimit64);
121         rlimit64.rlim_cur = RLIM64_INFINITY;
122         setrlimit64(RLIMIT_FSIZE, &rlimit64);
123
124         signal(SIGXFSZ, SIG_IGN);
125
126         if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
127                 fprintf(stderr, "open failed on ");
128                 perror(filename);
129                 return(1);
130         }
131
132         if (ftruncate64(fd, bigoff) < 0)
133                 return(1);
134
135         if (fstat64(fd, &bigst) < 0) {
136                 fprintf(stderr, "fstat64 failed on ");
137                 perror(filename);
138                 return(1);
139         }
140
141         if (verbose)
142                 fprintf(stderr, "fstat64 on %s gave sz=%lld (truncsz=%lld)\n",
143                         filename, (long long)bigst.st_size, (long long)bigoff);
144
145         if (bigst.st_size != bigoff)
146                 return(1);
147         return(0);
148 }
149
150 int
151 hasxfsquota(int type, int q, char *device)
152 {
153         fs_quota_stat_t qstat;
154         int             qcmd;
155
156         memset(&qstat, 0, sizeof(fs_quota_stat_t));
157
158 #ifdef QCMD
159         if (q == 0) {
160                 if (access("/proc/fs/xfs/xqm", F_OK) < 0) {
161                         if (verbose) {
162                                 fprintf(stderr, "can't access /proc/fs/xfs/xqm\n");
163                         }
164                         return 1;
165                 }
166                 return 0;
167         }
168         qcmd = QCMD(Q_XGETQSTAT, type);
169 #else
170         if (q == 0) {
171                 if (quotactl(Q_SYNC, device, 0, (caddr_t)&qstat) == ENOPKG) {
172                         if (verbose) {
173                                 fprintf(stderr, "Q_SYNC not supported\n");
174                         }
175                         return 1;
176                 }
177                 return 0;
178         }
179         qcmd = Q_GETQSTAT;
180 #endif
181
182         if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
183                 if (verbose)
184                         perror("quotactl");
185                 return (1);
186         }
187         else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
188                 return (0);
189         else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
190                 return (0);
191         else if (q == XFS_QUOTA_PDQ_ENFD && qstat.qs_flags & XFS_QUOTA_PDQ_ENFD)
192                 return (0);
193         else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
194                 return (0);
195         else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
196                 return (0);
197         else if (q == XFS_QUOTA_PDQ_ACCT && qstat.qs_flags & XFS_QUOTA_PDQ_ACCT)
198                 return (0);
199         if (verbose)
200                 fprintf(stderr, "quota type (%d) not available\n", q);
201         return (1);
202 }
203
204 static int
205 check_aio_support(void)
206 {
207 #ifdef HAVE_LIBAIO_H
208         struct io_context *ctx = NULL;
209         int err;
210
211         err = io_setup(1, &ctx);
212         if (err == 0)
213                 return 0;
214
215         if (err == -ENOSYS) /* CONFIG_AIO=n */
216                 return 1;
217
218         fprintf(stderr, "unexpected error from io_setup(): %s\n",
219                 strerror(-err));
220         return 2;
221 #else
222         /* libaio was unavailable at build time; assume AIO is unsupported */
223         return 1;
224 #endif
225 }
226
227 static int
228 check_uring_support(void)
229 {
230 #ifdef HAVE_LIBURING_H
231         struct io_uring ring;
232         int err;
233
234         err = io_uring_queue_init(1, &ring, 0);
235         if (err == 0)
236                 return 0;
237
238         if (err == -ENOSYS) /* CONFIG_IO_URING=n */
239                 return 1;
240
241         fprintf(stderr, "unexpected error from io_uring_queue_init(): %s\n",
242                 strerror(-err));
243         return 2;
244 #else
245         /* liburing is unavailable, assume IO_URING is unsupported */
246         return 1;
247 #endif
248 }
249
250 static int
251 check_mount_setattr_support(void)
252 {
253         int err;
254         struct mount_attr attr = {
255                 .attr_set       = MOUNT_ATTR_IDMAP,
256                 .userns_fd      = -EBADF,
257         };
258
259         /* mount_setattr() syscall not supported. */
260         err = sys_mount_setattr(-EBADF, "", AT_EMPTY_PATH, NULL, 0);
261         if (err && errno == ENOSYS)
262                 return 1;
263
264         /* idmapped mounts not supported */
265         err = sys_mount_setattr(-EBADF, ".", AT_EMPTY_PATH, &attr, sizeof(attr));
266         if (err && errno == E2BIG)
267                 return 1;
268
269         return 0;
270 }
271
272 int
273 main(int argc, char **argv)
274 {
275         int     c;
276         int     Aflag = 0;
277         int     cflag = 0;
278         int     tflag = 0;
279         int     gflag = 0;
280         int     Gflag = 0;
281         int     pflag = 0;
282         int     Pflag = 0;
283         int     qflag = 0;
284         int     rflag = 0;
285         int     Rflag = 0;
286         int     sflag = 0;
287         int     uflag = 0;
288         int     Uflag = 0;
289         int     wflag = 0;
290         int     oflag = 0;
291         char    *fs = NULL;
292
293         while ((c = getopt(argc, argv, "ActgGopPqrRsuUvw")) != EOF) {
294                 switch (c) {
295                 case 'A':
296                         Aflag++;
297                         break;
298                 case 'c':
299                         cflag++;
300                         break;
301                 case 't':
302                         tflag++;
303                         break;
304                 case 'g':
305                         gflag++;
306                         break;
307                 case 'G':
308                         Gflag++;
309                         break;
310                 case 'o':
311                         oflag++;
312                         break;
313                 case 'p':
314                         pflag++;
315                         break;
316                 case 'P':
317                         Pflag++;
318                         break;
319                 case 'q':
320                         qflag++;
321                         break;
322                 case 'r':
323                         rflag++;
324                         break;
325                 case 'R':
326                         Rflag++;
327                         break;
328                 case 's':
329                         sflag++;
330                         break;
331                 case 'u':
332                         uflag++;
333                         break;
334                 case 'U':
335                         Uflag++;
336                         break;
337                 case 'w':
338                         wflag++;
339                         break;
340                 case 'v':
341                         verbose++;
342                         break;
343                 default:
344                         usage();
345                 }
346         }
347
348         /* filesystem features */
349         if (cflag|tflag|uflag|gflag|pflag|qflag|Uflag|Gflag|Pflag) {
350                 if (optind != argc-1)   /* need a device */
351                         usage();
352                 fs = argv[argc-1];
353         } else if (Aflag || rflag || Rflag || wflag || sflag || oflag) {
354                 if (optind != argc)
355                         usage();
356         } else
357                 usage();
358
359         if (cflag)
360                 return(haschown32(fs));
361         if (tflag)
362                 return(hastruncate64(fs));
363         if (qflag)
364                 return(hasxfsquota(0, 0, fs));
365         if (gflag)
366                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
367         if (pflag)
368                 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ENFD, fs));
369         if (uflag)
370                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
371         if (Gflag)
372                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
373         if (Pflag)
374                 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ACCT, fs));
375         if (Uflag)
376                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
377
378         if (Aflag)
379                 return(check_aio_support());
380
381         if (rflag)
382                 return(check_mount_setattr_support());
383
384         if (Rflag)
385                 return(check_uring_support());
386
387         if (sflag) {
388                 printf("%d\n", getpagesize());
389                 exit(0);
390         }
391         if (wflag) {
392 #ifdef BITS_PER_LONG
393                 printf("%d\n", BITS_PER_LONG);
394 #else
395 #ifdef NBBY
396                 /* This can change under IRIX depending on whether we compile
397                  * with -n32/-32 or -64
398                  */
399                 printf("%d\n", (int)(NBBY * sizeof(long)));
400 #else
401 bozo!
402 #endif
403 #endif
404                 exit(0);
405         }
406         if (oflag) {
407                 long ncpus = -1;
408
409 #if defined(_SC_NPROCESSORS_ONLN)
410                 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
411 #elif defined(_SC_NPROC_ONLN)
412                 ncpus = sysconf(_SC_NPROC_ONLN);
413 #endif
414                 if (ncpus == -1)
415                         ncpus = 1;
416
417                 printf("%ld\n", ncpus);
418
419                 exit(0);
420         }
421
422         fprintf(stderr, "feature: dunno what you're after.\n");
423         return(1);
424 }