move mount stuff into common quota file, tidy a little, make a default.
[xfstests-dev.git] / src / feature.c
1 /*
2  * Copyright (c) 2000 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  *   -q  test for quota support (kernel compile option)
36  *   -u  test for user quota enforcement support (mount option)
37  *   -g  test for group quota enforcement support (mount option)
38  *   -U  test for user quota accounting support (mount option)
39  *   -G  test for group quota accounting support (mount option)
40  * Return code: 0 is true, anything else is error/not supported
41  */
42
43 #include <libxfs.h>
44 #include <sys/quota.h>
45 #include <xqm.h>
46
47 int verbose = 0;
48
49 void
50 usage(void)
51 {
52         fprintf(stderr, "Usage: feature [-v] -<q|u|g|U|G> <filesystem>\n");
53         exit(1);
54 }
55
56 int
57 hasxfsquota(int type, int q, char *device)
58 {
59         fs_quota_stat_t qstat;
60         int             qcmd;
61
62         memset(&qstat, 0, sizeof(fs_quota_stat_t));
63         qcmd = QCMD(Q_XGETQSTAT, type);
64         if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
65                 if (verbose)
66                         perror("quotactl");
67                 return (1);
68         }
69         else if (q == 0)
70                 return (0);
71         else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
72                 return (0);
73         else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
74                 return (0);
75         else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
76                 return (0);
77         else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
78                 return (0);
79         if (verbose)
80                 fprintf(stderr, "quota type (%d) not available\n", q);
81         return (1);
82 }
83
84 int
85 main(int argc, char **argv)
86 {
87         int     c;
88         int     gflag = 0;
89         int     Gflag = 0;
90         int     qflag = 0;
91         int     uflag = 0;
92         int     Uflag = 0;
93         char    *fs;
94
95         while ((c = getopt(argc, argv, "gGquUv")) != EOF) {
96                 switch (c) {
97                 case 'g':
98                         gflag++;
99                         break;
100                 case 'G':
101                         Gflag++;
102                         break;
103                 case 'q':
104                         qflag++;
105                         break;
106                 case 'u':
107                         uflag++;
108                         break;
109                 case 'U':
110                         Uflag++;
111                         break;
112                 case 'v':
113                         verbose++;
114                         break;
115                 default:
116                         usage();
117                 }
118         }
119
120         if (!uflag && !gflag && !qflag && !Uflag && !Gflag)
121                 usage();
122         if (optind != argc-1)
123                 usage();
124         fs = argv[argc-1];
125
126         if (qflag)
127                 return(hasxfsquota(0, 0, fs));
128         if (gflag)
129                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
130         if (uflag)
131                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
132         if (Gflag)
133                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
134         if (Uflag)
135                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
136
137         fprintf(stderr, "feature: dunno what you're doing?\n");
138         return(1);
139 }