1 /* 2 * Copyright (c) 1980, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Robert Elz at The University of Melbourne. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static const char copyright[] = 39 "@(#) Copyright (c) 1980, 1990, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93"; 46 #endif 47 static const char rcsid[] = 48 "$Id$"; 49 #endif /* not lint */ 50 51 /* 52 * Turn quota on/off for a filesystem. 53 */ 54 #include <sys/param.h> 55 #include <sys/file.h> 56 #include <sys/mount.h> 57 #include <ufs/ufs/quota.h> 58 #include <err.h> 59 #include <fstab.h> 60 #include <stdio.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 char *qfname = QUOTAFILENAME; 65 char *qfextension[] = INITQFNAMES; 66 67 int aflag; /* all file systems */ 68 int gflag; /* operate on group quotas */ 69 int uflag; /* operate on user quotas */ 70 int vflag; /* verbose */ 71 72 int hasquota __P((struct fstab *, int, char **)); 73 int oneof __P((char *, char *[], int)); 74 int quotaonoff __P((struct fstab *fs, int, int, char *)); 75 int readonly __P((struct fstab *)); 76 static void usage __P((void)); 77 78 int 79 main(argc, argv) 80 int argc; 81 char **argv; 82 { 83 register struct fstab *fs; 84 char ch, *qfnp, *whoami; 85 long argnum, done = 0; 86 int i, offmode = 0, errs = 0; 87 88 whoami = rindex(*argv, '/') + 1; 89 if (whoami == (char *)1) 90 whoami = *argv; 91 if (strcmp(whoami, "quotaoff") == 0) 92 offmode++; 93 else if (strcmp(whoami, "quotaon") != 0) 94 errx(1, "name must be quotaon or quotaoff"); 95 while ((ch = getopt(argc, argv, "avug")) != -1) { 96 switch(ch) { 97 case 'a': 98 aflag++; 99 break; 100 case 'g': 101 gflag++; 102 break; 103 case 'u': 104 uflag++; 105 break; 106 case 'v': 107 vflag++; 108 break; 109 default: 110 usage(); 111 } 112 } 113 argc -= optind; 114 argv += optind; 115 if (argc <= 0 && !aflag) 116 usage(); 117 if (!gflag && !uflag) { 118 gflag++; 119 uflag++; 120 } 121 setfsent(); 122 while ((fs = getfsent()) != NULL) { 123 if (strcmp(fs->fs_vfstype, "ufs") || 124 strcmp(fs->fs_type, FSTAB_RW)) 125 continue; 126 if (aflag) { 127 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 128 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 129 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 130 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 131 continue; 132 } 133 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 134 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 135 done |= 1 << argnum; 136 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 137 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 138 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 139 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 140 } 141 } 142 endfsent(); 143 for (i = 0; i < argc; i++) 144 if ((done & (1 << i)) == 0) 145 warnx("%s not found in fstab", argv[i]); 146 exit(errs); 147 } 148 149 static void 150 usage() 151 { 152 153 fprintf(stderr, "%s\n%s\n%s\n%s\n", 154 "usage: quotaon [-g] [-u] [-v] -a", 155 " quotaon [-g] [-u] [-v] filesystem ...", 156 " quotaoff [-g] [-u] [-v] -a", 157 " quotaoff [-g] [-u] [-v] filesystem ..."); 158 exit(1); 159 } 160 161 int 162 quotaonoff(fs, offmode, type, qfpathname) 163 register struct fstab *fs; 164 int offmode, type; 165 char *qfpathname; 166 { 167 168 if (strcmp(fs->fs_file, "/") && readonly(fs)) 169 return (1); 170 if (offmode) { 171 if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) { 172 warn("%s", fs->fs_file); 173 return (1); 174 } 175 if (vflag) 176 printf("%s: quotas turned off\n", fs->fs_file); 177 return (0); 178 } 179 if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) { 180 warnx("using %s on", qfpathname); 181 warn("%s", fs->fs_file); 182 return (1); 183 } 184 if (vflag) 185 printf("%s: %s quotas turned on\n", fs->fs_file, 186 qfextension[type]); 187 return (0); 188 } 189 190 /* 191 * Check to see if target appears in list of size cnt. 192 */ 193 int 194 oneof(target, list, cnt) 195 register char *target, *list[]; 196 int cnt; 197 { 198 register int i; 199 200 for (i = 0; i < cnt; i++) 201 if (strcmp(target, list[i]) == 0) 202 return (i); 203 return (-1); 204 } 205 206 /* 207 * Check to see if a particular quota is to be enabled. 208 */ 209 int 210 hasquota(fs, type, qfnamep) 211 register struct fstab *fs; 212 int type; 213 char **qfnamep; 214 { 215 register char *opt; 216 char *cp; 217 static char initname, usrname[100], grpname[100]; 218 static char buf[BUFSIZ]; 219 220 if (!initname) { 221 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 222 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 223 initname = 1; 224 } 225 strcpy(buf, fs->fs_mntops); 226 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 227 if ((cp = index(opt, '='))) 228 *cp++ = '\0'; 229 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 230 break; 231 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 232 break; 233 } 234 if (!opt) 235 return (0); 236 if (cp) { 237 *qfnamep = cp; 238 return (1); 239 } 240 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 241 *qfnamep = buf; 242 return (1); 243 } 244 245 /* 246 * Verify file system is mounted and not readonly. 247 */ 248 int 249 readonly(fs) 250 register struct fstab *fs; 251 { 252 struct statfs fsbuf; 253 254 if (statfs(fs->fs_file, &fsbuf) < 0 || 255 strcmp(fsbuf.f_mntonname, fs->fs_file) || 256 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { 257 printf("%s: not mounted\n", fs->fs_file); 258 return (1); 259 } 260 if (fsbuf.f_flags & MNT_RDONLY) { 261 printf("%s: mounted read-only\n", fs->fs_file); 262 return (1); 263 } 264 return (0); 265 } 266