1 1.31 hannken /* $NetBSD: quotaon.c,v 1.31 2022/04/26 15:39:00 hannken Exp $ */ 2 1.8 christos 3 1.1 cgd /* 4 1.4 mycroft * Copyright (c) 1980, 1990, 1993 5 1.4 mycroft * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * This code is derived from software contributed to Berkeley by 8 1.1 cgd * Robert Elz at The University of Melbourne. 9 1.1 cgd * 10 1.1 cgd * Redistribution and use in source and binary forms, with or without 11 1.1 cgd * modification, are permitted provided that the following conditions 12 1.1 cgd * are met: 13 1.1 cgd * 1. Redistributions of source code must retain the above copyright 14 1.1 cgd * notice, this list of conditions and the following disclaimer. 15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 cgd * notice, this list of conditions and the following disclaimer in the 17 1.1 cgd * documentation and/or other materials provided with the distribution. 18 1.19 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 cgd * may be used to endorse or promote products derived from this software 20 1.1 cgd * without specific prior written permission. 21 1.1 cgd * 22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 cgd * SUCH DAMAGE. 33 1.1 cgd */ 34 1.1 cgd 35 1.10 lukem #include <sys/cdefs.h> 36 1.1 cgd #ifndef lint 37 1.22 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\ 38 1.22 lukem The Regents of the University of California. All rights reserved."); 39 1.1 cgd #endif /* not lint */ 40 1.1 cgd 41 1.1 cgd #ifndef lint 42 1.8 christos #if 0 43 1.8 christos static char sccsid[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93"; 44 1.8 christos #else 45 1.31 hannken __RCSID("$NetBSD: quotaon.c,v 1.31 2022/04/26 15:39:00 hannken Exp $"); 46 1.8 christos #endif 47 1.1 cgd #endif /* not lint */ 48 1.1 cgd 49 1.1 cgd /* 50 1.1 cgd * Turn quota on/off for a filesystem. 51 1.1 cgd */ 52 1.1 cgd #include <sys/param.h> 53 1.1 cgd #include <sys/file.h> 54 1.1 cgd #include <sys/mount.h> 55 1.24 bouyer 56 1.29 dholland #include <quota.h> 57 1.24 bouyer #include <ufs/ufs/quota1.h> 58 1.12 perry 59 1.12 perry #include <err.h> 60 1.12 perry #include <fstab.h> 61 1.1 cgd #include <stdio.h> 62 1.25 christos #include <errno.h> 63 1.14 matt #include <stdlib.h> 64 1.5 cgd #include <string.h> 65 1.12 perry #include <unistd.h> 66 1.30 christos #include <util.h> 67 1.1 cgd 68 1.1 cgd 69 1.25 christos static int vflag; /* verbose */ 70 1.25 christos 71 1.28 dholland static void usage(void) __dead; 72 1.30 christos static int quotaonoff(struct fstab *, struct quotahandle *, int, int, int, 73 1.30 christos const char *); 74 1.30 christos static int readonly(struct fstab *, const char *); 75 1.29 dholland static int oneof(const char *target, char *list[], int cnt); 76 1.8 christos 77 1.8 christos int 78 1.25 christos main(int argc, char *argv[]) 79 1.1 cgd { 80 1.8 christos struct fstab *fs; 81 1.29 dholland struct quotahandle *qh; 82 1.1 cgd long argnum, done = 0; 83 1.1 cgd int i, offmode = 0, errs = 0; 84 1.29 dholland unsigned restrictions; 85 1.7 mark int ch; 86 1.1 cgd 87 1.29 dholland int aflag = 0; /* all file systems */ 88 1.29 dholland int gflag = 0; /* operate on group quotas */ 89 1.29 dholland int uflag = 0; /* operate on user quotas */ 90 1.29 dholland int noguflag = 0; /* operate on both (by default) */ 91 1.29 dholland 92 1.16 cgd if (strcmp(getprogname(), "quotaoff") == 0) 93 1.1 cgd offmode++; 94 1.16 cgd else if (strcmp(getprogname(), "quotaon") != 0) 95 1.17 grant errx(1, "Name must be quotaon or quotaoff"); 96 1.8 christos 97 1.7 mark while ((ch = getopt(argc, argv, "avug")) != -1) { 98 1.1 cgd switch(ch) { 99 1.1 cgd case 'a': 100 1.1 cgd aflag++; 101 1.1 cgd break; 102 1.1 cgd case 'g': 103 1.1 cgd gflag++; 104 1.1 cgd break; 105 1.1 cgd case 'u': 106 1.1 cgd uflag++; 107 1.1 cgd break; 108 1.1 cgd case 'v': 109 1.1 cgd vflag++; 110 1.1 cgd break; 111 1.1 cgd default: 112 1.8 christos usage(); 113 1.8 christos break; 114 1.1 cgd } 115 1.1 cgd } 116 1.1 cgd argc -= optind; 117 1.1 cgd argv += optind; 118 1.8 christos 119 1.1 cgd if (argc <= 0 && !aflag) 120 1.8 christos usage(); 121 1.8 christos 122 1.1 cgd if (!gflag && !uflag) { 123 1.29 dholland noguflag = 1; 124 1.29 dholland } 125 1.29 dholland 126 1.29 dholland /* 127 1.29 dholland * XXX at the moment quota_open also uses getfsent(), but it 128 1.29 dholland * uses it only up front. To avoid conflicting with it, let it 129 1.29 dholland * initialize first. 130 1.29 dholland */ 131 1.29 dholland qh = quota_open("/"); 132 1.29 dholland if (qh != NULL) { 133 1.29 dholland quota_close(qh); 134 1.1 cgd } 135 1.29 dholland 136 1.1 cgd setfsent(); 137 1.1 cgd while ((fs = getfsent()) != NULL) { 138 1.30 christos char buf[MAXPATHLEN]; 139 1.30 christos const char *fsspec; 140 1.18 perseant if ((strcmp(fs->fs_vfstype, "ffs") && 141 1.18 perseant strcmp(fs->fs_vfstype, "lfs")) || 142 1.1 cgd strcmp(fs->fs_type, FSTAB_RW)) 143 1.1 cgd continue; 144 1.29 dholland 145 1.30 christos fsspec = getfsspecname(buf, sizeof(buf), fs->fs_spec); 146 1.30 christos if (fsspec == NULL) { 147 1.30 christos warn("%s", buf); 148 1.30 christos continue; 149 1.30 christos } 150 1.29 dholland if (!aflag) { 151 1.29 dholland if ((argnum = oneof(fs->fs_file, argv, argc)) < 0 && 152 1.30 christos (argnum = oneof(fsspec, argv, argc)) < 0) { 153 1.29 dholland continue; 154 1.29 dholland } 155 1.29 dholland done |= 1U << argnum; 156 1.29 dholland } 157 1.29 dholland 158 1.29 dholland qh = quota_open(fs->fs_file); 159 1.29 dholland if (qh == NULL) { 160 1.29 dholland if (!aflag) { 161 1.29 dholland warn("quota_open"); 162 1.29 dholland errs++; 163 1.29 dholland } 164 1.29 dholland continue; 165 1.29 dholland } 166 1.29 dholland 167 1.29 dholland restrictions = quota_getrestrictions(qh); 168 1.29 dholland if ((restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) == 0) { 169 1.29 dholland /* Not a quota v1 volume, skip it */ 170 1.29 dholland if (!aflag) { 171 1.29 dholland errno = EBUSY; 172 1.29 dholland warn("%s", fs->fs_file); 173 1.29 dholland errs++; 174 1.29 dholland } 175 1.29 dholland quota_close(qh); 176 1.1 cgd continue; 177 1.1 cgd } 178 1.29 dholland 179 1.29 dholland /* 180 1.29 dholland * The idea here is to warn if someone explicitly 181 1.29 dholland * tries to turn on group quotas and there are no 182 1.29 dholland * group quotas, and likewise for user quotas, but not 183 1.29 dholland * to warn if just doing the default thing and one of 184 1.29 dholland * the quota types isn't configured. 185 1.29 dholland */ 186 1.29 dholland 187 1.29 dholland if (noguflag) { 188 1.30 christos errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 0, fsspec); 189 1.30 christos errs += quotaonoff(fs, qh, offmode, USRQUOTA, 0, fsspec); 190 1.29 dholland } 191 1.29 dholland if (gflag) { 192 1.30 christos errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 1, fsspec); 193 1.29 dholland } 194 1.29 dholland if (uflag) { 195 1.30 christos errs += quotaonoff(fs, qh, offmode, USRQUOTA, 1, fsspec); 196 1.1 cgd } 197 1.29 dholland quota_close(qh); 198 1.1 cgd } 199 1.1 cgd endfsent(); 200 1.1 cgd for (i = 0; i < argc; i++) 201 1.25 christos if ((done & (1U << i)) == 0) 202 1.8 christos warnx("%s not found in fstab", argv[i]); 203 1.25 christos return errs; 204 1.1 cgd } 205 1.1 cgd 206 1.8 christos static void 207 1.25 christos usage(void) 208 1.1 cgd { 209 1.25 christos const char *p = getprogname(); 210 1.25 christos (void) fprintf(stderr, "Usage: %s [-g] [-u] [-v] -a\n" 211 1.25 christos "\t%s [-g] [-u] [-v] filesys ...\n", p, p); 212 1.1 cgd exit(1); 213 1.1 cgd } 214 1.1 cgd 215 1.8 christos static int 216 1.29 dholland quotaonoff(struct fstab *fs, struct quotahandle *qh, int offmode, int idtype, 217 1.30 christos int warn_on_enxio, const char *fsspec) 218 1.1 cgd { 219 1.24 bouyer const char *mode = (offmode == 1) ? "off" : "on"; 220 1.31 hannken const char *type; 221 1.1 cgd 222 1.30 christos if (strcmp(fs->fs_file, "/") && readonly(fs, fsspec)) { 223 1.25 christos return 1; 224 1.29 dholland } 225 1.24 bouyer 226 1.1 cgd if (offmode) { 227 1.31 hannken type = quota_idtype_getname(qh, idtype); 228 1.29 dholland if (quota_quotaoff(qh, idtype)) { 229 1.29 dholland if (warn_on_enxio || errno != ENXIO) { 230 1.29 dholland warn("quota%s for %s", mode, fs->fs_file); 231 1.29 dholland } 232 1.29 dholland return 1; 233 1.29 dholland } 234 1.24 bouyer } else { 235 1.29 dholland if (quota_quotaon(qh, idtype)) { 236 1.29 dholland if (warn_on_enxio || errno != ENXIO) { 237 1.29 dholland warn("quota%s for %s", mode, fs->fs_file); 238 1.29 dholland } 239 1.29 dholland return 1; 240 1.29 dholland } 241 1.31 hannken type = quota_idtype_getname(qh, idtype); 242 1.1 cgd } 243 1.24 bouyer 244 1.24 bouyer if (vflag) { 245 1.24 bouyer printf("%s: %s quotas turned %s\n", 246 1.31 hannken fs->fs_file, type, mode); 247 1.1 cgd } 248 1.25 christos return 0; 249 1.1 cgd } 250 1.1 cgd 251 1.1 cgd /* 252 1.1 cgd * Verify file system is mounted and not readonly. 253 1.1 cgd */ 254 1.8 christos static int 255 1.30 christos readonly(struct fstab *fs, const char *fsspec) 256 1.1 cgd { 257 1.21 christos struct statvfs fsbuf; 258 1.1 cgd 259 1.21 christos if (statvfs(fs->fs_file, &fsbuf) < 0 || 260 1.1 cgd strcmp(fsbuf.f_mntonname, fs->fs_file) || 261 1.30 christos strcmp(fsbuf.f_mntfromname, fsspec)) { 262 1.1 cgd printf("%s: not mounted\n", fs->fs_file); 263 1.25 christos return 1; 264 1.1 cgd } 265 1.21 christos if (fsbuf.f_flag & MNT_RDONLY) { 266 1.1 cgd printf("%s: mounted read-only\n", fs->fs_file); 267 1.25 christos return 1; 268 1.1 cgd } 269 1.25 christos return 0; 270 1.1 cgd } 271 1.29 dholland 272 1.29 dholland /* 273 1.29 dholland * Check to see if target appears in list of size cnt. 274 1.29 dholland */ 275 1.29 dholland static int 276 1.29 dholland oneof(const char *target, char *list[], int cnt) 277 1.29 dholland { 278 1.29 dholland int i; 279 1.29 dholland 280 1.29 dholland for (i = 0; i < cnt; i++) 281 1.29 dholland if (strcmp(target, list[i]) == 0) 282 1.29 dholland return i; 283 1.29 dholland return -1; 284 1.29 dholland } 285