1 /* $NetBSD: quotaon.c,v 1.22 2008/07/21 13:36:59 lukem Exp $ */
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93";
45 __RCSID("$NetBSD: quotaon.c,v 1.22 2008/07/21 13:36:59 lukem Exp $");
50 * Turn quota on/off for a filesystem.
52 #include <sys/param.h>
54 #include <sys/mount.h>
55 #include <ufs/ufs/quota.h>
64 const char *qfname
= QUOTAFILENAME
;
65 const char *qfextension
[] = INITQFNAMES
;
67 int aflag
; /* all file systems */
68 int gflag
; /* operate on group quotas */
69 int uflag
; /* operate on user quotas */
70 int vflag
; /* verbose */
72 int main
__P((int, char *[]));
74 static void usage
__P((void));
75 static int quotaonoff
__P((struct fstab
*, int, int, char *));
76 static int oneof
__P((const char *, char *[], int));
77 static int hasquota
__P((struct fstab
*, int, char **));
78 static int readonly
__P((struct fstab
*));
87 long argnum
, done
= 0;
88 int i
, offmode
= 0, errs
= 0;
91 if (strcmp(getprogname(), "quotaoff") == 0)
93 else if (strcmp(getprogname(), "quotaon") != 0)
94 errx(1, "Name must be quotaon or quotaoff");
96 while ((ch
= getopt(argc
, argv
, "avug")) != -1) {
118 if (argc
<= 0 && !aflag
)
121 if (!gflag
&& !uflag
) {
126 while ((fs
= getfsent()) != NULL
) {
127 if ((strcmp(fs
->fs_vfstype
, "ffs") &&
128 strcmp(fs
->fs_vfstype
, "lfs")) ||
129 strcmp(fs
->fs_type
, FSTAB_RW
))
132 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
133 errs
+= quotaonoff(fs
, offmode
, GRPQUOTA
, qfnp
);
134 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
135 errs
+= quotaonoff(fs
, offmode
, USRQUOTA
, qfnp
);
138 if ((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
139 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) {
141 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
142 errs
+= quotaonoff(fs
, offmode
, GRPQUOTA
, qfnp
);
143 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
144 errs
+= quotaonoff(fs
, offmode
, USRQUOTA
, qfnp
);
148 for (i
= 0; i
< argc
; i
++)
149 if ((done
& (1 << i
)) == 0)
150 warnx("%s not found in fstab", argv
[i
]);
158 (void) fprintf(stderr
, "usage:\n\t%s [-g] [-u] [-v] -a\n",
160 (void) fprintf(stderr
, "\t%s [-g] [-u] [-v] filesys ...\n",
166 quotaonoff(fs
, offmode
, type
, qfpathname
)
172 if (strcmp(fs
->fs_file
, "/") && readonly(fs
))
175 if (quotactl(fs
->fs_file
, QCMD(Q_QUOTAOFF
, type
), 0, 0) < 0) {
176 warn("%s", fs
->fs_file
);
180 printf("%s: %s quotas turned off\n",
181 fs
->fs_file
, qfextension
[type
]);
184 if (quotactl(fs
->fs_file
, QCMD(Q_QUOTAON
, type
), 0, qfpathname
) < 0) {
185 warn("%s quotas using %s on %s",
186 qfextension
[type
], qfpathname
, fs
->fs_file
);
190 printf("%s: %s quotas turned on\n", fs
->fs_file
,
196 * Check to see if target appears in list of size cnt.
199 oneof(target
, list
, cnt
)
206 for (i
= 0; i
< cnt
; i
++)
207 if (strcmp(target
, list
[i
]) == 0)
213 * Check to see if a particular quota is to be enabled.
216 hasquota(fs
, type
, qfnamep
)
223 static char initname
, usrname
[100], grpname
[100];
224 static char buf
[BUFSIZ
];
227 (void) snprintf(usrname
, sizeof(usrname
), "%s%s",
228 qfextension
[USRQUOTA
], qfname
);
229 (void) snprintf(grpname
, sizeof(grpname
), "%s%s",
230 qfextension
[GRPQUOTA
], qfname
);
233 strcpy(buf
, fs
->fs_mntops
);
234 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
235 if ((cp
= strchr(opt
, '=')) != NULL
)
237 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
239 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
248 (void) snprintf(buf
, sizeof(buf
), "%s/%s.%s", fs
->fs_file
, qfname
,
255 * Verify file system is mounted and not readonly.
261 struct statvfs fsbuf
;
263 if (statvfs(fs
->fs_file
, &fsbuf
) < 0 ||
264 strcmp(fsbuf
.f_mntonname
, fs
->fs_file
) ||
265 strcmp(fsbuf
.f_mntfromname
, fs
->fs_spec
)) {
266 printf("%s: not mounted\n", fs
->fs_file
);
269 if (fsbuf
.f_flag
& MNT_RDONLY
) {
270 printf("%s: mounted read-only\n", fs
->fs_file
);