Switch over pagesize feature.c option, add a check for project quota.
[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  *   -p  test for project quota enforcement support (mount option)
40  *   -g  test for group quota enforcement support (mount option)
41  *   -U  test for user quota accounting support (mount option)
42  *   -G  test for group quota accounting support (mount option)
43  *   -P  test for project quota accounting support (mount option)
44  * Return code: 0 is true, anything else is error/not supported
45  *
46  * Test for machine features
47  *   -s  report pagesize
48  *   -w  report bits per long
49  */
50
51 #include "global.h"
52
53 #include <sys/quota.h>
54 #include <sys/resource.h>
55 #include <signal.h>
56
57 #ifdef HAVE_XFS_XQM_H
58 #include <xfs/xqm.h>
59 #endif
60
61 #ifndef USRQUOTA
62 #define USRQUOTA  0
63 #endif
64
65 #ifndef GRPQUOTA
66 #define GRPQUOTA  1
67 #endif
68
69 #ifndef PRJQUOTA
70 #define PRJQUOTA  2
71 #endif
72
73 int verbose = 0;
74
75 void
76 usage(void)
77 {
78         fprintf(stderr, "Usage: feature [-v] -<q|u|g|p|U|G|P> <filesystem>\n");
79         fprintf(stderr, "       feature [-v] -c <file>\n");
80         fprintf(stderr, "       feature [-v] -t <file>\n");
81         fprintf(stderr, "       feature -s | -w\n");
82         exit(1);
83 }
84
85 int check_big_ID(char *filename)
86 {
87         struct stat64   sbuf;
88
89         memset(&sbuf, 0, sizeof(struct stat64));
90         if (lstat64(filename, &sbuf) < 0) {
91                 fprintf(stderr, "lstat64 failed on ");
92                 perror(filename);
93                 return(1);
94         }
95
96         /* 98789 is greater than 2^16 (65536) */
97         if ((__uint32_t)sbuf.st_uid == 98789 || (__uint32_t)sbuf.st_gid == 98789)
98                 return(0);
99         if (verbose)
100                 fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
101                         filename, (int)sbuf.st_uid, (int)sbuf.st_gid);
102         return(1);
103 }
104
105 int
106 haschown32(char *filename)
107 {
108         if (check_big_ID(filename) == 0)
109                 return(0);
110
111         if (chown(filename, 98789, 98789) < 0) {
112                 fprintf(stderr, "chown failed on ");
113                 perror(filename);
114                 return(1);
115         }
116
117         if (check_big_ID(filename) == 0)
118                 return(0);
119         return (1);
120 }
121
122 int
123 hastruncate64(char *filename)
124 {
125         struct rlimit64 rlimit64;
126         off64_t bigoff = 4294967307LL;  /* > 2^32 */
127         struct stat64 bigst;
128         int fd;
129
130         getrlimit64(RLIMIT_FSIZE, &rlimit64);
131         rlimit64.rlim_cur = RLIM64_INFINITY;
132         setrlimit64(RLIMIT_FSIZE, &rlimit64);
133
134         signal(SIGXFSZ, SIG_IGN);
135
136         if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
137                 fprintf(stderr, "open failed on ");
138                 perror(filename);
139                 return(1);
140         }
141
142         if (ftruncate64(fd, bigoff) < 0)
143                 return(1);
144
145         if (fstat64(fd, &bigst) < 0) {
146                 fprintf(stderr, "fstat64 failed on ");
147                 perror(filename);
148                 return(1);
149         }
150
151         if (verbose)
152                 fprintf(stderr, "fstat64 on %s gave sz=%lld (truncsz=%lld)\n",
153                         filename, (long long)bigst.st_size, (long long)bigoff);
154
155         if (bigst.st_size != bigoff)
156                 return(1);
157         return(0);
158 }
159
160 int
161 hasxfsquota(int type, int q, char *device)
162 {
163         fs_quota_stat_t qstat;
164         int             qcmd;
165
166         memset(&qstat, 0, sizeof(fs_quota_stat_t));
167
168 #ifdef QCMD
169         if (q == 0) {
170                 if (access("/proc/fs/xfs/xqm", F_OK) < 0) {
171                         if (verbose) {
172                                 fprintf(stderr, "can't access /proc/fs/xfs/xqm\n");
173                         }
174                         return 1;
175                 }
176                 return 0;
177         }
178         qcmd = QCMD(Q_XGETQSTAT, type);
179 #else
180         if (q == 0) {
181                 if (quotactl(Q_SYNC, device, 0, (caddr_t)&qstat) == ENOPKG) {
182                         if (verbose) {
183                                 fprintf(stderr, "Q_SYNC not supported\n");
184                         }
185                         return 1;
186                 }
187                 return 0;
188         }
189         qcmd = Q_GETQSTAT;
190 #endif
191
192         if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
193                 if (verbose)
194                         perror("quotactl");
195                 return (1);
196         }
197         else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
198                 return (0);
199         else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
200                 return (0);
201         else if (q == XFS_QUOTA_PDQ_ENFD && qstat.qs_flags & XFS_QUOTA_PDQ_ENFD)
202                 return (0);
203         else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
204                 return (0);
205         else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
206                 return (0);
207         else if (q == XFS_QUOTA_PDQ_ACCT && qstat.qs_flags & XFS_QUOTA_PDQ_ACCT)
208                 return (0);
209         if (verbose)
210                 fprintf(stderr, "quota type (%d) not available\n", q);
211         return (1);
212 }
213
214 int
215 main(int argc, char **argv)
216 {
217         int     c;
218         int     cflag = 0;
219         int     tflag = 0;
220         int     gflag = 0;
221         int     Gflag = 0;
222         int     pflag = 0;
223         int     Pflag = 0;
224         int     qflag = 0;
225         int     sflag = 0;
226         int     uflag = 0;
227         int     Uflag = 0;
228         int     wflag = 0;
229         char    *fs = NULL;
230
231         while ((c = getopt(argc, argv, "ctgGpPqsuUvw")) != EOF) {
232                 switch (c) {
233                 case 'c':
234                         cflag++;
235                         break;
236                 case 't':
237                         tflag++;
238                         break;
239                 case 'g':
240                         gflag++;
241                         break;
242                 case 'G':
243                         Gflag++;
244                         break;
245                 case 'p':
246                         pflag++;
247                         break;
248                 case 'P':
249                         Pflag++;
250                         break;
251                 case 'q':
252                         qflag++;
253                         break;
254                 case 's':
255                         sflag++;
256                         break;
257                 case 'u':
258                         uflag++;
259                         break;
260                 case 'U':
261                         Uflag++;
262                         break;
263                 case 'w':
264                         wflag++;
265                         break;
266                 case 'v':
267                         verbose++;
268                         break;
269                 default:
270                         usage();
271                 }
272         }
273
274         /* filesystem features */
275         if (cflag|tflag|uflag|gflag|pflag|qflag|Uflag|Gflag|Pflag) {
276                 if (optind != argc-1)   /* need a device */
277                         usage();
278                 fs = argv[argc-1];
279         } else if (wflag || sflag) {
280                 if (optind != argc)
281                         usage();
282         } else 
283                 usage();
284
285         if (cflag)
286                 return(haschown32(fs));
287         if (tflag)
288                 return(hastruncate64(fs));
289         if (qflag)
290                 return(hasxfsquota(0, 0, fs));
291         if (gflag)
292                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
293         if (pflag)
294                 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ENFD, fs));
295         if (uflag)
296                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
297         if (Gflag)
298                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
299         if (Pflag)
300                 return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ACCT, fs));
301         if (Uflag)
302                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
303
304         if (sflag) {
305                 printf("%d\n", getpagesize());
306                 exit(0);
307         }
308         if (wflag) {
309 #ifdef BITS_PER_LONG
310                 printf("%d\n", BITS_PER_LONG);
311 #else
312 #ifdef NBBY
313                 /* This can change under IRIX depending on whether we compile
314                  * with -n32/-32 or -64
315                  */
316                 printf("%d\n", (int)(NBBY * sizeof(long)));
317 #else
318 bozo!
319 #endif
320 #endif
321                 exit(0);
322         }
323
324         fprintf(stderr, "feature: dunno what you're after.\n");
325         return(1);
326 }