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*41423Smckusick static char sccsid[] = "@(#)quotaon.c 5.9 (Berkeley) 05/05/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; 5141409Smckusick char ch, *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) { 9541409Smckusick if (aflag) { 9641409Smckusick if (gflag && hasquota(fs->fs_mntops, GRPQUOTA)) 9741409Smckusick errs += quotaonoff(fs, offmode, GRPQUOTA); 9841409Smckusick if (uflag && hasquota(fs->fs_mntops, USRQUOTA)) 9941409Smckusick errs += quotaonoff(fs, offmode, USRQUOTA); 10012662Smckusick continue; 10141409Smckusick } 102*41423Smckusick if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 10341409Smckusick (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 10441409Smckusick done |= 1 << argnum; 10541409Smckusick if (gflag && hasquota(fs->fs_mntops, GRPQUOTA)) 10641409Smckusick errs += quotaonoff(fs, offmode, GRPQUOTA); 10741409Smckusick if (uflag && hasquota(fs->fs_mntops, USRQUOTA)) 10841409Smckusick errs += quotaonoff(fs, offmode, USRQUOTA); 10941409Smckusick } 11012662Smckusick } 11112662Smckusick endfsent(); 11212723Smckusick for (i = 0; i < argc; i++) 11312723Smckusick if ((done & (1 << i)) == 0) 11437983Sbostic fprintf(stderr, "%s not found in fstab\n", 11512723Smckusick argv[i]); 11612670Smckusick exit(errs); 11712662Smckusick } 11812662Smckusick 11941409Smckusick usage(whoami) 12041409Smckusick char *whoami; 12141409Smckusick { 12241409Smckusick 12341409Smckusick fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami); 12441409Smckusick fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami); 12541409Smckusick exit(1); 12641409Smckusick } 12741409Smckusick 12841409Smckusick quotaonoff(fs, offmode, type) 12912809Ssam register struct fstab *fs; 13041409Smckusick int offmode, type; 13112809Ssam { 13241409Smckusick char quotafile[MAXPATHLEN + 1]; 13312809Ssam 13412809Ssam if (strcmp(fs->fs_file, "/") && readonly(fs)) 13512809Ssam return (1); 13612809Ssam if (offmode) { 13741409Smckusick if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) 13812809Ssam goto bad; 13912809Ssam if (vflag) 14012809Ssam printf("%s: quotas turned off\n", fs->fs_file); 14112809Ssam return (0); 14212809Ssam } 14341409Smckusick (void) sprintf(quotafile, "%s/%s.%s", fs->fs_file, qfname, 14441409Smckusick qfextension[type]); 14541409Smckusick if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, quotafile) < 0) 14612809Ssam goto bad; 14712809Ssam if (vflag) 14841409Smckusick printf("%s: %s quotas turned on\n", fs->fs_file, 14941409Smckusick qfextension[type]); 15012809Ssam return (0); 15112809Ssam bad: 15212809Ssam fprintf(stderr, "setquota: "); 15341409Smckusick perror(fs->fs_file); 15412809Ssam return (1); 15512809Ssam } 15612809Ssam 15741409Smckusick /* 15841409Smckusick * Check to see if target appears in list of size cnt. 15941409Smckusick */ 16041409Smckusick oneof(target, list, cnt) 16141409Smckusick register char *target, *list[]; 16241409Smckusick int cnt; 16312662Smckusick { 16412662Smckusick register int i; 16512662Smckusick 16641409Smckusick for (i = 0; i < cnt; i++) 16741409Smckusick if (strcmp(target, list[i]) == 0) 16841409Smckusick return (i); 16941409Smckusick return (-1); 17041409Smckusick } 17141409Smckusick 17241409Smckusick /* 17341409Smckusick * Check to see if a particular quota is to be enabled. 17441409Smckusick */ 17541409Smckusick hasquota(options, type) 17641409Smckusick char *options; 17741409Smckusick int type; 17841409Smckusick { 17941409Smckusick register char *opt; 18041409Smckusick char buf[BUFSIZ]; 18141409Smckusick char *strtok(); 18241409Smckusick static char initname, usrname[100], grpname[100]; 18341409Smckusick 18441409Smckusick if (!initname) { 18541409Smckusick sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 18641409Smckusick sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 18741409Smckusick initname = 1; 18841409Smckusick } 18941409Smckusick strcpy(buf, options); 19041409Smckusick for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 19141409Smckusick if (type == USRQUOTA && strcmp(opt, usrname) == 0) 19241409Smckusick return(1); 19341409Smckusick if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 19441409Smckusick return(1); 19541409Smckusick } 19612662Smckusick return (0); 19712662Smckusick } 19812809Ssam 19912809Ssam /* 20012809Ssam * Verify file system is mounted and not readonly. 20112809Ssam */ 20212809Ssam readonly(fs) 20312809Ssam register struct fstab *fs; 20412809Ssam { 20539844Smckusick struct statfs fsbuf; 20612809Ssam 20739844Smckusick if (statfs(fs->fs_file, &fsbuf) < 0 || 20839844Smckusick strcmp(fsbuf.f_mntonname, fs->fs_file) || 20939844Smckusick strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { 21039844Smckusick printf("%s: not mounted\n", fs->fs_file); 21139844Smckusick return (1); 21212809Ssam } 21341410Smckusick if (fsbuf.f_flags & MNT_RDONLY) { 21439844Smckusick printf("%s: mounted read-only\n", fs->fs_file); 21539844Smckusick return (1); 21639844Smckusick } 21739844Smckusick return (0); 21812809Ssam } 219