2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)quotaon.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.sbin/quotaon/quotaon.c,v 1.4.2.1 2001/07/19 05:17:06 kris Exp $
39 * $DragonFly: src/usr.sbin/quotaon/quotaon.c,v 1.8 2005/03/18 00:52:49 joerg Exp $
43 * Turn quota on/off for a filesystem.
45 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <vfs/ufs/quota.h>
56 const char *qfname
= QUOTAFILENAME
;
57 const char *qfextension
[] = INITQFNAMES
;
59 int aflag
; /* all file systems */
60 int gflag
; /* operate on group quotas */
61 int uflag
; /* operate on user quotas */
62 int vflag
; /* verbose */
64 int hasquota(struct fstab
*, int, char **);
65 int oneof(char *, char *[], int);
66 int quotaonoff(struct fstab
*fs
, int, int, char *);
67 int readonly(struct fstab
*);
68 static void usage(void);
71 main(int argc
, char **argv
)
74 char ch
, *qfnp
, *whoami
;
75 long argnum
, done
= 0;
76 int i
, offmode
= 0, errs
= 0;
78 whoami
= strrchr(*argv
, '/') + 1;
79 if (whoami
== (char *)1)
81 if (strcmp(whoami
, "quotaoff") == 0)
83 else if (strcmp(whoami
, "quotaon") != 0)
84 errx(1, "name must be quotaon or quotaoff");
85 while ((ch
= getopt(argc
, argv
, "avug")) != -1) {
105 if (argc
<= 0 && !aflag
)
107 if (!gflag
&& !uflag
) {
112 while ((fs
= getfsent()) != NULL
) {
113 if (strcmp(fs
->fs_vfstype
, "ufs") ||
114 strcmp(fs
->fs_type
, FSTAB_RW
))
117 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
118 errs
+= quotaonoff(fs
, offmode
, GRPQUOTA
, qfnp
);
119 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
120 errs
+= quotaonoff(fs
, offmode
, USRQUOTA
, qfnp
);
123 if ((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
124 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) {
126 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
127 errs
+= quotaonoff(fs
, offmode
, GRPQUOTA
, qfnp
);
128 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
129 errs
+= quotaonoff(fs
, offmode
, USRQUOTA
, qfnp
);
133 for (i
= 0; i
< argc
; i
++)
134 if ((done
& (1 << i
)) == 0)
135 warnx("%s not found in fstab", argv
[i
]);
142 fprintf(stderr
, "%s\n%s\n%s\n%s\n",
143 "usage: quotaon [-g] [-u] [-v] -a",
144 " quotaon [-g] [-u] [-v] filesystem ...",
145 " quotaoff [-g] [-u] [-v] -a",
146 " quotaoff [-g] [-u] [-v] filesystem ...");
151 quotaonoff(struct fstab
*fs
, int offmode
, int type
, char *qfpathname
)
153 if (strcmp(fs
->fs_file
, "/") && readonly(fs
))
156 if (quotactl(fs
->fs_file
, QCMD(Q_QUOTAOFF
, type
), 0, 0) < 0) {
157 warn("%s", fs
->fs_file
);
161 printf("%s: quotas turned off\n", fs
->fs_file
);
164 if (quotactl(fs
->fs_file
, QCMD(Q_QUOTAON
, type
), 0, qfpathname
) < 0) {
165 warnx("using %s on", qfpathname
);
166 warn("%s", fs
->fs_file
);
170 printf("%s: %s quotas turned on\n", fs
->fs_file
,
176 * Check to see if target appears in list of size cnt.
179 oneof(char *target
, char *list
[], int cnt
)
183 for (i
= 0; i
< cnt
; i
++)
184 if (strcmp(target
, list
[i
]) == 0)
190 * Check to see if a particular quota is to be enabled.
193 hasquota(struct fstab
*fs
, int type
, char **qfnamep
)
197 static char initname
, usrname
[100], grpname
[100];
198 static char buf
[BUFSIZ
];
201 sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
202 sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
205 strcpy(buf
, fs
->fs_mntops
);
206 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
207 if ((cp
= strchr(opt
, '=')))
209 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
211 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
220 sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
226 * Verify file system is mounted and not readonly.
229 readonly(struct fstab
*fs
)
233 if (statfs(fs
->fs_file
, &fsbuf
) < 0 ||
234 strcmp(fsbuf
.f_mntonname
, fs
->fs_file
) ||
235 strcmp(fsbuf
.f_mntfromname
, fs
->fs_spec
)) {
236 printf("%s: not mounted\n", fs
->fs_file
);
239 if (fsbuf
.f_flags
& MNT_RDONLY
) {
240 printf("%s: mounted read-only\n", fs
->fs_file
);