Crank up fsstress a whole lot, enable unwritten extents everywhere, remove need for...
[xfstests-dev.git] / src / feature.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 /*
34  * Test for filesystem features on given mount point or device
35  *   -c  test for 32bit chown support (via libc)
36  *   -t  test for working rlimit/ftruncate64 (via libc)
37  *   -q  test for quota support (kernel compile option)
38  *   -u  test for user quota enforcement support (mount option)
39  *   -g  test for group quota enforcement support (mount option)
40  *   -U  test for user quota accounting support (mount option)
41  *   -G  test for group quota accounting support (mount option)
42  * Return code: 0 is true, anything else is error/not supported
43  *
44  * Test for machine features
45  *   -p  report pagesize
46  *   -w  report bits per long
47  */
48
49 #include <xfs/libxfs.h>
50 #include <sys/quota.h>
51 #include <sys/resource.h>
52 #include <signal.h>
53 #include <xfs/xqm.h>
54
55 int verbose = 0;
56
57 void
58 usage(void)
59 {
60         fprintf(stderr, "Usage: feature [-v] -<q|u|g|U|G> <filesystem>\n");
61         fprintf(stderr, "       feature [-v] -c <file>\n");
62         fprintf(stderr, "       feature [-v] -t <file>\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 ((__u32)sbuf.st_uid == 98789 || (__u32)sbuf.st_gid == 98789)
79                 return(0);
80         if (verbose)
81                 fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
82                         filename, sbuf.st_uid, 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 = 4294967307;    /* > 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, bigst.st_size, 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         if (q == 0)
148                 return (access("/proc/fs/xfs/xqm", F_OK) < 0);
149
150         memset(&qstat, 0, sizeof(fs_quota_stat_t));
151         qcmd = QCMD(Q_XGETQSTAT, type);
152         if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
153                 if (verbose)
154                         perror("quotactl");
155                 return (1);
156         }
157         else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
158                 return (0);
159         else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
160                 return (0);
161         else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
162                 return (0);
163         else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
164                 return (0);
165         if (verbose)
166                 fprintf(stderr, "quota type (%d) not available\n", q);
167         return (1);
168 }
169
170 int
171 main(int argc, char **argv)
172 {
173         int     c;
174         int     cflag = 0;
175         int     tflag = 0;
176         int     gflag = 0;
177         int     Gflag = 0;
178         int     pflag = 0;
179         int     qflag = 0;
180         int     uflag = 0;
181         int     Uflag = 0;
182         int     wflag = 0;
183         char    *fs = NULL;
184
185         while ((c = getopt(argc, argv, "ctgGpquUvw")) != EOF) {
186                 switch (c) {
187                 case 'c':
188                         cflag++;
189                         break;
190                 case 't':
191                         tflag++;
192                         break;
193                 case 'g':
194                         gflag++;
195                         break;
196                 case 'G':
197                         Gflag++;
198                         break;
199                 case 'p':
200                         pflag++;
201                         break;
202                 case 'q':
203                         qflag++;
204                         break;
205                 case 'u':
206                         uflag++;
207                         break;
208                 case 'U':
209                         Uflag++;
210                         break;
211                 case 'w':
212                         wflag++;
213                         break;
214                 case 'v':
215                         verbose++;
216                         break;
217                 default:
218                         usage();
219                 }
220         }
221
222         /* filesystem features */
223         if (cflag || tflag || uflag || gflag || qflag || Uflag || Gflag) {
224                 if (optind != argc-1)   /* need a device */
225                         usage();
226                 fs = argv[argc-1];
227         } else if (wflag || pflag) {
228                 if (optind != argc)
229                         usage();
230         } else 
231                 usage();
232
233         if (cflag)
234                 return(haschown32(fs));
235         if (tflag)
236                 return(hastruncate64(fs));
237         if (qflag)
238                 return(hasxfsquota(0, 0, fs));
239         if (gflag)
240                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
241         if (uflag)
242                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
243         if (Gflag)
244                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
245         if (Uflag)
246                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
247
248         if (pflag) {
249                 printf("%d\n", getpagesize());
250                 exit(0);
251         }
252         if (wflag) {
253                 printf("%d\n", BITS_PER_LONG);
254                 exit(0);
255         }
256
257         fprintf(stderr, "feature: dunno what you're after.\n");
258         return(1);
259 }