Home | History | Annotate | Line # | Download | only in quotaon
quotaon.c revision 1.29
      1  1.29  dholland /*	$NetBSD: quotaon.c,v 1.29 2012/01/30 16:45:13 dholland 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.29  dholland __RCSID("$NetBSD: quotaon.c,v 1.29 2012/01/30 16:45:13 dholland 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.1       cgd 
     67   1.1       cgd 
     68  1.25  christos static int	vflag;		/* verbose */
     69  1.25  christos 
     70  1.28  dholland static void usage(void) __dead;
     71  1.29  dholland static int quotaonoff(struct fstab *, struct quotahandle *, int, int, int);
     72  1.25  christos static int readonly(struct fstab *);
     73  1.29  dholland static int oneof(const char *target, char *list[], int cnt);
     74   1.8  christos 
     75   1.8  christos int
     76  1.25  christos main(int argc, char *argv[])
     77   1.1       cgd {
     78   1.8  christos 	struct fstab *fs;
     79  1.29  dholland 	struct quotahandle *qh;
     80   1.1       cgd 	long argnum, done = 0;
     81   1.1       cgd 	int i, offmode = 0, errs = 0;
     82  1.29  dholland 	unsigned restrictions;
     83   1.7      mark 	int ch;
     84   1.1       cgd 
     85  1.29  dholland 	int aflag = 0;		/* all file systems */
     86  1.29  dholland 	int gflag = 0;		/* operate on group quotas */
     87  1.29  dholland 	int uflag = 0;		/* operate on user quotas */
     88  1.29  dholland 	int noguflag = 0;	/* operate on both (by default) */
     89  1.29  dholland 
     90  1.16       cgd 	if (strcmp(getprogname(), "quotaoff") == 0)
     91   1.1       cgd 		offmode++;
     92  1.16       cgd 	else if (strcmp(getprogname(), "quotaon") != 0)
     93  1.17     grant 		errx(1, "Name must be quotaon or quotaoff");
     94   1.8  christos 
     95   1.7      mark 	while ((ch = getopt(argc, argv, "avug")) != -1) {
     96   1.1       cgd 		switch(ch) {
     97   1.1       cgd 		case 'a':
     98   1.1       cgd 			aflag++;
     99   1.1       cgd 			break;
    100   1.1       cgd 		case 'g':
    101   1.1       cgd 			gflag++;
    102   1.1       cgd 			break;
    103   1.1       cgd 		case 'u':
    104   1.1       cgd 			uflag++;
    105   1.1       cgd 			break;
    106   1.1       cgd 		case 'v':
    107   1.1       cgd 			vflag++;
    108   1.1       cgd 			break;
    109   1.1       cgd 		default:
    110   1.8  christos 			usage();
    111   1.8  christos 			break;
    112   1.1       cgd 		}
    113   1.1       cgd 	}
    114   1.1       cgd 	argc -= optind;
    115   1.1       cgd 	argv += optind;
    116   1.8  christos 
    117   1.1       cgd 	if (argc <= 0 && !aflag)
    118   1.8  christos 		usage();
    119   1.8  christos 
    120   1.1       cgd 	if (!gflag && !uflag) {
    121  1.29  dholland 		noguflag = 1;
    122  1.29  dholland 	}
    123  1.29  dholland 
    124  1.29  dholland 	/*
    125  1.29  dholland 	 * XXX at the moment quota_open also uses getfsent(), but it
    126  1.29  dholland 	 * uses it only up front. To avoid conflicting with it, let it
    127  1.29  dholland 	 * initialize first.
    128  1.29  dholland 	 */
    129  1.29  dholland 	qh = quota_open("/");
    130  1.29  dholland 	if (qh != NULL) {
    131  1.29  dholland 		quota_close(qh);
    132   1.1       cgd 	}
    133  1.29  dholland 
    134   1.1       cgd 	setfsent();
    135   1.1       cgd 	while ((fs = getfsent()) != NULL) {
    136  1.18  perseant 		if ((strcmp(fs->fs_vfstype, "ffs") &&
    137  1.18  perseant 		     strcmp(fs->fs_vfstype, "lfs")) ||
    138   1.1       cgd 		    strcmp(fs->fs_type, FSTAB_RW))
    139   1.1       cgd 			continue;
    140  1.29  dholland 
    141  1.29  dholland 		if (!aflag) {
    142  1.29  dholland 			if ((argnum = oneof(fs->fs_file, argv, argc)) < 0 &&
    143  1.29  dholland 			    (argnum = oneof(fs->fs_spec, argv, argc)) < 0) {
    144  1.29  dholland 				continue;
    145  1.29  dholland 			}
    146  1.29  dholland 			done |= 1U << argnum;
    147  1.29  dholland 		}
    148  1.29  dholland 
    149  1.29  dholland 		qh = quota_open(fs->fs_file);
    150  1.29  dholland 		if (qh == NULL) {
    151  1.29  dholland 			if (!aflag) {
    152  1.29  dholland 				warn("quota_open");
    153  1.29  dholland 				errs++;
    154  1.29  dholland 			}
    155  1.29  dholland 			continue;
    156  1.29  dholland 		}
    157  1.29  dholland 
    158  1.29  dholland 		restrictions = quota_getrestrictions(qh);
    159  1.29  dholland 		if ((restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) == 0) {
    160  1.29  dholland 			/* Not a quota v1 volume, skip it */
    161  1.29  dholland 			if (!aflag) {
    162  1.29  dholland 				errno = EBUSY;
    163  1.29  dholland 				warn("%s", fs->fs_file);
    164  1.29  dholland 				errs++;
    165  1.29  dholland 			}
    166  1.29  dholland 			quota_close(qh);
    167   1.1       cgd 			continue;
    168   1.1       cgd 		}
    169  1.29  dholland 
    170  1.29  dholland 		/*
    171  1.29  dholland 		 * The idea here is to warn if someone explicitly
    172  1.29  dholland 		 * tries to turn on group quotas and there are no
    173  1.29  dholland 		 * group quotas, and likewise for user quotas, but not
    174  1.29  dholland 		 * to warn if just doing the default thing and one of
    175  1.29  dholland 		 * the quota types isn't configured.
    176  1.29  dholland 		 */
    177  1.29  dholland 
    178  1.29  dholland 		if (noguflag) {
    179  1.29  dholland 			errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 0);
    180  1.29  dholland 			errs += quotaonoff(fs, qh, offmode, USRQUOTA, 0);
    181  1.29  dholland 		}
    182  1.29  dholland 		if (gflag) {
    183  1.29  dholland 			errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 1);
    184  1.29  dholland 		}
    185  1.29  dholland 		if (uflag) {
    186  1.29  dholland 			errs += quotaonoff(fs, qh, offmode, USRQUOTA, 1);
    187   1.1       cgd 		}
    188  1.29  dholland 		quota_close(qh);
    189   1.1       cgd 	}
    190   1.1       cgd 	endfsent();
    191   1.1       cgd 	for (i = 0; i < argc; i++)
    192  1.25  christos 		if ((done & (1U << i)) == 0)
    193   1.8  christos 			warnx("%s not found in fstab", argv[i]);
    194  1.25  christos 	return errs;
    195   1.1       cgd }
    196   1.1       cgd 
    197   1.8  christos static void
    198  1.25  christos usage(void)
    199   1.1       cgd {
    200  1.25  christos 	const char *p = getprogname();
    201  1.25  christos 	(void) fprintf(stderr, "Usage: %s [-g] [-u] [-v] -a\n"
    202  1.25  christos 	    "\t%s [-g] [-u] [-v] filesys ...\n", p, p);
    203   1.1       cgd 	exit(1);
    204   1.1       cgd }
    205   1.1       cgd 
    206   1.8  christos static int
    207  1.29  dholland quotaonoff(struct fstab *fs, struct quotahandle *qh, int offmode, int idtype,
    208  1.29  dholland 	   int warn_on_enxio)
    209   1.1       cgd {
    210  1.24    bouyer 	const char *mode = (offmode == 1) ? "off" : "on";
    211   1.1       cgd 
    212  1.29  dholland 	if (strcmp(fs->fs_file, "/") && readonly(fs)) {
    213  1.25  christos 		return 1;
    214  1.29  dholland 	}
    215  1.24    bouyer 
    216   1.1       cgd 	if (offmode) {
    217  1.29  dholland 		if (quota_quotaoff(qh, idtype)) {
    218  1.29  dholland 			if (warn_on_enxio || errno != ENXIO) {
    219  1.29  dholland 				warn("quota%s for %s", mode, fs->fs_file);
    220  1.29  dholland 			}
    221  1.29  dholland 			return 1;
    222  1.29  dholland 		}
    223  1.24    bouyer 	} else {
    224  1.29  dholland 		if (quota_quotaon(qh, idtype)) {
    225  1.29  dholland 			if (warn_on_enxio || errno != ENXIO) {
    226  1.29  dholland 				warn("quota%s for %s", mode, fs->fs_file);
    227  1.29  dholland 			}
    228  1.29  dholland 			return 1;
    229  1.29  dholland 		}
    230   1.1       cgd 	}
    231  1.24    bouyer 
    232  1.24    bouyer 	if (vflag) {
    233  1.24    bouyer 		printf("%s: %s quotas turned %s\n",
    234  1.29  dholland 		    fs->fs_file, quota_idtype_getname(qh, idtype), mode);
    235   1.1       cgd 	}
    236  1.25  christos 	return 0;
    237   1.1       cgd }
    238   1.1       cgd 
    239   1.1       cgd /*
    240   1.1       cgd  * Verify file system is mounted and not readonly.
    241   1.1       cgd  */
    242   1.8  christos static int
    243  1.25  christos readonly(struct fstab *fs)
    244   1.1       cgd {
    245  1.21  christos 	struct statvfs fsbuf;
    246   1.1       cgd 
    247  1.21  christos 	if (statvfs(fs->fs_file, &fsbuf) < 0 ||
    248   1.1       cgd 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
    249   1.1       cgd 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
    250   1.1       cgd 		printf("%s: not mounted\n", fs->fs_file);
    251  1.25  christos 		return 1;
    252   1.1       cgd 	}
    253  1.21  christos 	if (fsbuf.f_flag & MNT_RDONLY) {
    254   1.1       cgd 		printf("%s: mounted read-only\n", fs->fs_file);
    255  1.25  christos 		return 1;
    256   1.1       cgd 	}
    257  1.25  christos 	return 0;
    258   1.1       cgd }
    259  1.29  dholland 
    260  1.29  dholland /*
    261  1.29  dholland  * Check to see if target appears in list of size cnt.
    262  1.29  dholland  */
    263  1.29  dholland static int
    264  1.29  dholland oneof(const char *target, char *list[], int cnt)
    265  1.29  dholland {
    266  1.29  dholland 	int i;
    267  1.29  dholland 
    268  1.29  dholland 	for (i = 0; i < cnt; i++)
    269  1.29  dholland 		if (strcmp(target, list[i]) == 0)
    270  1.29  dholland 			return i;
    271  1.29  dholland 	return -1;
    272  1.29  dholland }
    273