121518Smckusick /* 241409Smckusick * Copyright (c) 1980, 1990 Regents of the University of California. 334365Sbostic * All rights reserved. 434365Sbostic * 541409Smckusick * This code is derived from software contributed to Berkeley by 641409Smckusick * Robert Elz at The University of Melbourne. 741409Smckusick * 834365Sbostic * Redistribution and use in source and binary forms are permitted 934778Sbostic * provided that the above copyright notice and this paragraph are 1034778Sbostic * duplicated in all such forms and that any documentation, 1134778Sbostic * advertising materials, and other materials related to such 1234778Sbostic * distribution and use acknowledge that the software was developed 1334778Sbostic * by the University of California, Berkeley. The name of the 1434778Sbostic * University may not be used to endorse or promote products derived 1534778Sbostic * from this software without specific prior written permission. 1634778Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1734778Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1834778Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1921518Smckusick */ 2021518Smckusick 2112662Smckusick #ifndef lint 2221518Smckusick char copyright[] = 2341409Smckusick "@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\ 2421518Smckusick All rights reserved.\n"; 2534365Sbostic #endif /* not lint */ 2612809Ssam 2721518Smckusick #ifndef lint 28*41437Smckusick static char sccsid[] = "@(#)quotaon.c 5.10 (Berkeley) 05/06/90"; 2934365Sbostic #endif /* not lint */ 3021518Smckusick 3112662Smckusick /* 3212670Smckusick * Turn quota on/off for a filesystem. 3312662Smckusick */ 3412662Smckusick #include <sys/param.h> 3512809Ssam #include <sys/file.h> 3639844Smckusick #include <sys/mount.h> 3741409Smckusick #include <ufs/quota.h> 3812662Smckusick #include <stdio.h> 3912662Smckusick #include <fstab.h> 4012662Smckusick 4141409Smckusick int aflag; /* all file systems */ 4241409Smckusick int gflag; /* operate on group quotas */ 4341409Smckusick int uflag; /* operate on user quotas */ 4412662Smckusick int vflag; /* verbose */ 4512662Smckusick 4612662Smckusick main(argc, argv) 4712670Smckusick int argc; 4812662Smckusick char **argv; 4912662Smckusick { 5012662Smckusick register struct fstab *fs; 51*41437Smckusick char ch, *qfnp, *whoami, *rindex(); 5241409Smckusick long argnum, done = 0; 5341409Smckusick int i, offmode = 0, errs = 0; 5441409Smckusick extern char *optarg; 5541409Smckusick extern int optind; 5612662Smckusick 5712670Smckusick whoami = rindex(*argv, '/') + 1; 5812670Smckusick if (whoami == (char *)1) 5912670Smckusick whoami = *argv; 6012670Smckusick if (strcmp(whoami, "quotaoff") == 0) 6112670Smckusick offmode++; 6212670Smckusick else if (strcmp(whoami, "quotaon") != 0) { 6312670Smckusick fprintf(stderr, "Name must be quotaon or quotaoff not %s\n", 6412670Smckusick whoami); 6512670Smckusick exit(1); 6612670Smckusick } 6741409Smckusick while ((ch = getopt(argc, argv, "avug")) != EOF) { 6841409Smckusick switch(ch) { 6941409Smckusick case 'a': 7041409Smckusick aflag++; 7141409Smckusick break; 7241409Smckusick case 'g': 7341409Smckusick gflag++; 7441409Smckusick break; 7541409Smckusick case 'u': 7641409Smckusick uflag++; 7741409Smckusick break; 7841409Smckusick case 'v': 7941409Smckusick vflag++; 8041409Smckusick break; 8141409Smckusick default: 8241409Smckusick usage(whoami); 8341409Smckusick } 8412662Smckusick } 8541409Smckusick argc -= optind; 8641409Smckusick argv += optind; 8741409Smckusick if (argc <= 0 && !aflag) 8841409Smckusick usage(whoami); 8941409Smckusick if (!gflag && !uflag) { 9041409Smckusick gflag++; 9141409Smckusick uflag++; 9212662Smckusick } 9312662Smckusick setfsent(); 9412662Smckusick while ((fs = getfsent()) != NULL) { 95*41437Smckusick if (strcmp(fs->fs_vfstype, "ufs") || 96*41437Smckusick strcmp(fs->fs_type, FSTAB_RW)) 97*41437Smckusick continue; 9841409Smckusick if (aflag) { 99*41437Smckusick if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 100*41437Smckusick errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 101*41437Smckusick if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 102*41437Smckusick errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 10312662Smckusick continue; 10441409Smckusick } 10541423Smckusick if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 10641409Smckusick (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 10741409Smckusick done |= 1 << argnum; 108*41437Smckusick if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 109*41437Smckusick errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 110*41437Smckusick if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 111*41437Smckusick errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 11241409Smckusick } 11312662Smckusick } 11412662Smckusick endfsent(); 11512723Smckusick for (i = 0; i < argc; i++) 11612723Smckusick if ((done & (1 << i)) == 0) 11737983Sbostic fprintf(stderr, "%s not found in fstab\n", 11812723Smckusick argv[i]); 11912670Smckusick exit(errs); 12012662Smckusick } 12112662Smckusick 12241409Smckusick usage(whoami) 12341409Smckusick char *whoami; 12441409Smckusick { 12541409Smckusick 12641409Smckusick fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami); 12741409Smckusick fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami); 12841409Smckusick exit(1); 12941409Smckusick } 13041409Smckusick 131*41437Smckusick quotaonoff(fs, offmode, type, qfpathname) 13212809Ssam register struct fstab *fs; 13341409Smckusick int offmode, type; 134*41437Smckusick char *qfpathname; 13512809Ssam { 13612809Ssam 13712809Ssam if (strcmp(fs->fs_file, "/") && readonly(fs)) 13812809Ssam return (1); 13912809Ssam if (offmode) { 140*41437Smckusick if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) { 141*41437Smckusick fprintf(stderr, "quotaoff: "); 142*41437Smckusick perror(fs->fs_file); 143*41437Smckusick return (1); 144*41437Smckusick } 14512809Ssam if (vflag) 14612809Ssam printf("%s: quotas turned off\n", fs->fs_file); 14712809Ssam return (0); 14812809Ssam } 149*41437Smckusick if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) { 150*41437Smckusick fprintf(stderr, "quotaon: using %s on", qfpathname); 151*41437Smckusick perror(fs->fs_file); 152*41437Smckusick return (1); 153*41437Smckusick } 15412809Ssam if (vflag) 15541409Smckusick printf("%s: %s quotas turned on\n", fs->fs_file, 15641409Smckusick qfextension[type]); 15712809Ssam return (0); 15812809Ssam } 15912809Ssam 16041409Smckusick /* 16141409Smckusick * Check to see if target appears in list of size cnt. 16241409Smckusick */ 16341409Smckusick oneof(target, list, cnt) 16441409Smckusick register char *target, *list[]; 16541409Smckusick int cnt; 16612662Smckusick { 16712662Smckusick register int i; 16812662Smckusick 16941409Smckusick for (i = 0; i < cnt; i++) 17041409Smckusick if (strcmp(target, list[i]) == 0) 17141409Smckusick return (i); 17241409Smckusick return (-1); 17341409Smckusick } 17441409Smckusick 17541409Smckusick /* 17641409Smckusick * Check to see if a particular quota is to be enabled. 17741409Smckusick */ 178*41437Smckusick hasquota(fs, type, qfnamep) 179*41437Smckusick register struct fstab *fs; 18041409Smckusick int type; 181*41437Smckusick char **qfnamep; 18241409Smckusick { 18341409Smckusick register char *opt; 184*41437Smckusick char *cp, *index(), *strtok(); 18541409Smckusick static char initname, usrname[100], grpname[100]; 186*41437Smckusick static char buf[BUFSIZ]; 18741409Smckusick 18841409Smckusick if (!initname) { 18941409Smckusick sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 19041409Smckusick sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 19141409Smckusick initname = 1; 19241409Smckusick } 193*41437Smckusick strcpy(buf, fs->fs_mntops); 19441409Smckusick for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 195*41437Smckusick if (cp = index(opt, '=')) 196*41437Smckusick *cp++ = '\0'; 19741409Smckusick if (type == USRQUOTA && strcmp(opt, usrname) == 0) 198*41437Smckusick break; 19941409Smckusick if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 200*41437Smckusick break; 20141409Smckusick } 202*41437Smckusick if (!opt) 203*41437Smckusick return (0); 204*41437Smckusick if (cp) { 205*41437Smckusick *qfnamep = cp; 206*41437Smckusick return (1); 207*41437Smckusick } 208*41437Smckusick (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 209*41437Smckusick *qfnamep = buf; 210*41437Smckusick return (1); 21112662Smckusick } 21212809Ssam 21312809Ssam /* 21412809Ssam * Verify file system is mounted and not readonly. 21512809Ssam */ 21612809Ssam readonly(fs) 21712809Ssam register struct fstab *fs; 21812809Ssam { 21939844Smckusick struct statfs fsbuf; 22012809Ssam 22139844Smckusick if (statfs(fs->fs_file, &fsbuf) < 0 || 22239844Smckusick strcmp(fsbuf.f_mntonname, fs->fs_file) || 22339844Smckusick strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { 22439844Smckusick printf("%s: not mounted\n", fs->fs_file); 22539844Smckusick return (1); 22612809Ssam } 22741410Smckusick if (fsbuf.f_flags & MNT_RDONLY) { 22839844Smckusick printf("%s: mounted read-only\n", fs->fs_file); 22939844Smckusick return (1); 23039844Smckusick } 23139844Smckusick return (0); 23212809Ssam } 233