Home | History | Annotate | Line # | Download | only in quotaon
quotaon.c revision 1.21
      1  1.21  christos /*	$NetBSD: quotaon.c,v 1.21 2004/04/21 01:05:48 christos 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.10     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
     38  1.10     lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.21  christos __RCSID("$NetBSD: quotaon.c,v 1.21 2004/04/21 01:05:48 christos 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.3       cgd #include <ufs/ufs/quota.h>
     56  1.12     perry 
     57  1.12     perry #include <err.h>
     58  1.12     perry #include <fstab.h>
     59   1.1       cgd #include <stdio.h>
     60  1.14      matt #include <stdlib.h>
     61   1.5       cgd #include <string.h>
     62  1.12     perry #include <unistd.h>
     63   1.1       cgd 
     64   1.1       cgd char *qfname = QUOTAFILENAME;
     65   1.1       cgd char *qfextension[] = INITQFNAMES;
     66   1.1       cgd 
     67   1.1       cgd int	aflag;		/* all file systems */
     68   1.1       cgd int	gflag;		/* operate on group quotas */
     69   1.1       cgd int	uflag;		/* operate on user quotas */
     70   1.1       cgd int	vflag;		/* verbose */
     71   1.1       cgd 
     72   1.8  christos int main __P((int, char *[]));
     73   1.8  christos 
     74   1.8  christos static void usage __P((void));
     75   1.8  christos static int quotaonoff __P((struct fstab *, int, int, char *));
     76  1.13   mycroft static int oneof __P((const char *, char *[], int));
     77   1.8  christos static int hasquota __P((struct fstab *, int, char **));
     78   1.8  christos static int readonly __P((struct fstab *));
     79   1.8  christos 
     80   1.8  christos int
     81   1.1       cgd main(argc, argv)
     82   1.1       cgd 	int argc;
     83   1.8  christos 	char *argv[];
     84   1.1       cgd {
     85   1.8  christos 	struct fstab *fs;
     86   1.8  christos 	char *qfnp;
     87   1.1       cgd 	long argnum, done = 0;
     88   1.1       cgd 	int i, offmode = 0, errs = 0;
     89   1.7      mark 	int ch;
     90   1.1       cgd 
     91  1.16       cgd 	if (strcmp(getprogname(), "quotaoff") == 0)
     92   1.1       cgd 		offmode++;
     93  1.16       cgd 	else if (strcmp(getprogname(), "quotaon") != 0)
     94  1.17     grant 		errx(1, "Name must be quotaon or quotaoff");
     95   1.8  christos 
     96   1.7      mark 	while ((ch = getopt(argc, argv, "avug")) != -1) {
     97   1.1       cgd 		switch(ch) {
     98   1.1       cgd 		case 'a':
     99   1.1       cgd 			aflag++;
    100   1.1       cgd 			break;
    101   1.1       cgd 		case 'g':
    102   1.1       cgd 			gflag++;
    103   1.1       cgd 			break;
    104   1.1       cgd 		case 'u':
    105   1.1       cgd 			uflag++;
    106   1.1       cgd 			break;
    107   1.1       cgd 		case 'v':
    108   1.1       cgd 			vflag++;
    109   1.1       cgd 			break;
    110   1.1       cgd 		default:
    111   1.8  christos 			usage();
    112   1.8  christos 			break;
    113   1.1       cgd 		}
    114   1.1       cgd 	}
    115   1.1       cgd 	argc -= optind;
    116   1.1       cgd 	argv += optind;
    117   1.8  christos 
    118   1.1       cgd 	if (argc <= 0 && !aflag)
    119   1.8  christos 		usage();
    120   1.8  christos 
    121   1.1       cgd 	if (!gflag && !uflag) {
    122   1.1       cgd 		gflag++;
    123   1.1       cgd 		uflag++;
    124   1.1       cgd 	}
    125   1.1       cgd 	setfsent();
    126   1.1       cgd 	while ((fs = getfsent()) != NULL) {
    127  1.18  perseant 		if ((strcmp(fs->fs_vfstype, "ffs") &&
    128  1.18  perseant 		     strcmp(fs->fs_vfstype, "lfs")) ||
    129   1.1       cgd 		    strcmp(fs->fs_type, FSTAB_RW))
    130   1.1       cgd 			continue;
    131   1.1       cgd 		if (aflag) {
    132   1.1       cgd 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    133   1.1       cgd 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
    134   1.1       cgd 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    135   1.1       cgd 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
    136   1.1       cgd 			continue;
    137   1.1       cgd 		}
    138   1.1       cgd 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    139   1.1       cgd 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
    140   1.1       cgd 			done |= 1 << argnum;
    141   1.1       cgd 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    142   1.1       cgd 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
    143   1.1       cgd 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    144   1.1       cgd 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
    145   1.1       cgd 		}
    146   1.1       cgd 	}
    147   1.1       cgd 	endfsent();
    148   1.1       cgd 	for (i = 0; i < argc; i++)
    149   1.1       cgd 		if ((done & (1 << i)) == 0)
    150   1.8  christos 			warnx("%s not found in fstab", argv[i]);
    151   1.1       cgd 	exit(errs);
    152   1.1       cgd }
    153   1.1       cgd 
    154   1.8  christos static void
    155   1.8  christos usage()
    156   1.1       cgd {
    157   1.1       cgd 
    158  1.20      jmmv 	(void) fprintf(stderr, "usage:\n\t%s [-g] [-u] [-v] -a\n",
    159  1.16       cgd 	    getprogname());
    160  1.16       cgd 	(void) fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n",
    161  1.16       cgd 	    getprogname());
    162   1.1       cgd 	exit(1);
    163   1.1       cgd }
    164   1.1       cgd 
    165   1.8  christos static int
    166   1.1       cgd quotaonoff(fs, offmode, type, qfpathname)
    167   1.8  christos 	struct fstab *fs;
    168   1.1       cgd 	int offmode, type;
    169   1.1       cgd 	char *qfpathname;
    170   1.1       cgd {
    171   1.1       cgd 
    172   1.1       cgd 	if (strcmp(fs->fs_file, "/") && readonly(fs))
    173   1.1       cgd 		return (1);
    174   1.1       cgd 	if (offmode) {
    175   1.1       cgd 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
    176  1.15    itojun 			warn("%s", fs->fs_file);
    177   1.1       cgd 			return (1);
    178   1.1       cgd 		}
    179   1.1       cgd 		if (vflag)
    180   1.9     lukem 			printf("%s: %s quotas turned off\n",
    181   1.9     lukem 			    fs->fs_file, qfextension[type]);
    182   1.1       cgd 		return (0);
    183   1.1       cgd 	}
    184   1.1       cgd 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
    185   1.9     lukem 		warn("%s quotas using %s on %s",
    186   1.9     lukem 		    qfextension[type], qfpathname, fs->fs_file);
    187   1.1       cgd 		return (1);
    188   1.1       cgd 	}
    189   1.1       cgd 	if (vflag)
    190   1.1       cgd 		printf("%s: %s quotas turned on\n", fs->fs_file,
    191   1.1       cgd 		    qfextension[type]);
    192   1.1       cgd 	return (0);
    193   1.1       cgd }
    194   1.1       cgd 
    195   1.1       cgd /*
    196   1.1       cgd  * Check to see if target appears in list of size cnt.
    197   1.1       cgd  */
    198   1.8  christos static int
    199   1.1       cgd oneof(target, list, cnt)
    200  1.13   mycroft 	const char *target;
    201  1.13   mycroft 	char *list[];
    202   1.1       cgd 	int cnt;
    203   1.1       cgd {
    204   1.8  christos 	int i;
    205   1.1       cgd 
    206   1.1       cgd 	for (i = 0; i < cnt; i++)
    207   1.1       cgd 		if (strcmp(target, list[i]) == 0)
    208   1.1       cgd 			return (i);
    209   1.1       cgd 	return (-1);
    210   1.1       cgd }
    211   1.1       cgd 
    212   1.1       cgd /*
    213   1.1       cgd  * Check to see if a particular quota is to be enabled.
    214   1.1       cgd  */
    215   1.8  christos static int
    216   1.1       cgd hasquota(fs, type, qfnamep)
    217   1.8  christos 	struct fstab *fs;
    218   1.1       cgd 	int type;
    219   1.1       cgd 	char **qfnamep;
    220   1.1       cgd {
    221   1.8  christos 	char *opt;
    222  1.11      fair 	char *cp = NULL;
    223   1.1       cgd 	static char initname, usrname[100], grpname[100];
    224   1.1       cgd 	static char buf[BUFSIZ];
    225   1.1       cgd 
    226   1.1       cgd 	if (!initname) {
    227   1.8  christos 		(void) snprintf(usrname, sizeof(usrname), "%s%s",
    228   1.8  christos 		    qfextension[USRQUOTA], qfname);
    229   1.8  christos 		(void) snprintf(grpname, sizeof(grpname), "%s%s",
    230   1.8  christos 		    qfextension[GRPQUOTA], qfname);
    231   1.1       cgd 		initname = 1;
    232   1.1       cgd 	}
    233   1.1       cgd 	strcpy(buf, fs->fs_mntops);
    234   1.1       cgd 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    235   1.8  christos 		if ((cp = strchr(opt, '=')) != NULL)
    236   1.1       cgd 			*cp++ = '\0';
    237   1.1       cgd 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    238   1.1       cgd 			break;
    239   1.1       cgd 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    240   1.1       cgd 			break;
    241   1.1       cgd 	}
    242   1.1       cgd 	if (!opt)
    243   1.1       cgd 		return (0);
    244   1.1       cgd 	if (cp) {
    245   1.1       cgd 		*qfnamep = cp;
    246   1.1       cgd 		return (1);
    247   1.1       cgd 	}
    248   1.8  christos 	(void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname,
    249   1.8  christos 	    qfextension[type]);
    250   1.1       cgd 	*qfnamep = buf;
    251   1.1       cgd 	return (1);
    252   1.1       cgd }
    253   1.1       cgd 
    254   1.1       cgd /*
    255   1.1       cgd  * Verify file system is mounted and not readonly.
    256   1.1       cgd  */
    257   1.8  christos static int
    258   1.1       cgd readonly(fs)
    259   1.8  christos 	struct fstab *fs;
    260   1.1       cgd {
    261  1.21  christos 	struct statvfs fsbuf;
    262   1.1       cgd 
    263  1.21  christos 	if (statvfs(fs->fs_file, &fsbuf) < 0 ||
    264   1.1       cgd 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
    265   1.1       cgd 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
    266   1.1       cgd 		printf("%s: not mounted\n", fs->fs_file);
    267   1.1       cgd 		return (1);
    268   1.1       cgd 	}
    269  1.21  christos 	if (fsbuf.f_flag & MNT_RDONLY) {
    270   1.1       cgd 		printf("%s: mounted read-only\n", fs->fs_file);
    271   1.1       cgd 		return (1);
    272   1.1       cgd 	}
    273   1.1       cgd 	return (0);
    274   1.1       cgd }
    275