1997-09-20 01:22:38 +04:00
|
|
|
/* $NetBSD: quotacheck.c,v 1.16 1997/09/19 21:22:38 christos Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
/*
|
1994-06-08 23:27:32 +04:00
|
|
|
* Copyright (c) 1980, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 23:25:07 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Robert Elz at The University of Melbourne.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-16 17:07:59 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-09 23:25:07 +04:00
|
|
|
#ifndef lint
|
1997-09-16 17:07:59 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-04-09 23:25:07 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
1997-09-16 17:07:59 +04:00
|
|
|
static char sccsid[] = "@(#)quotacheck.c 8.6 (Berkeley) 4/28/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
1997-09-20 01:22:38 +04:00
|
|
|
__RCSID("$NetBSD: quotacheck.c,v 1.16 1997/09/19 21:22:38 christos Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1993-04-09 23:25:07 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix up / report on disk quotas & usage
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
1997-09-16 17:07:59 +04:00
|
|
|
#include <sys/queue.h>
|
1994-06-08 23:27:32 +04:00
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/quota.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
|
1997-09-16 17:07:59 +04:00
|
|
|
#include <err.h>
|
1993-04-09 23:25:07 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
#include "fsutil.h"
|
1993-04-09 23:25:07 +04:00
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static char *qfname = QUOTAFILENAME;
|
|
|
|
static char *qfextension[] = INITQFNAMES;
|
|
|
|
static char *quotagroup = QUOTAGROUP;
|
|
|
|
|
|
|
|
static union {
|
1993-04-09 23:25:07 +04:00
|
|
|
struct fs sblk;
|
|
|
|
char dummy[MAXBSIZE];
|
|
|
|
} un;
|
|
|
|
#define sblock un.sblk
|
1996-09-28 03:25:34 +04:00
|
|
|
static long dev_bsize;
|
|
|
|
static long maxino;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
struct quotaname {
|
|
|
|
long flags;
|
|
|
|
char grpqfname[MAXPATHLEN + 1];
|
|
|
|
char usrqfname[MAXPATHLEN + 1];
|
|
|
|
};
|
|
|
|
#define HASUSR 1
|
|
|
|
#define HASGRP 2
|
|
|
|
|
|
|
|
struct fileusage {
|
|
|
|
struct fileusage *fu_next;
|
|
|
|
u_long fu_curinodes;
|
|
|
|
u_long fu_curblocks;
|
|
|
|
u_long fu_id;
|
|
|
|
char fu_name[1];
|
|
|
|
/* actually bigger */
|
|
|
|
};
|
|
|
|
#define FUHASH 1024 /* must be power of two */
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
|
|
|
|
|
|
|
|
static int aflag; /* all file systems */
|
|
|
|
static int gflag; /* check group quotas */
|
|
|
|
static int uflag; /* check user quotas */
|
|
|
|
static int vflag; /* verbose */
|
|
|
|
static int fi; /* open disk file descriptor */
|
|
|
|
static u_long highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
|
|
|
|
|
|
|
|
|
|
|
|
int main __P((int, char *[]));
|
|
|
|
static void usage __P((void));
|
|
|
|
static void *needchk __P((struct fstab *));
|
1996-09-28 23:20:44 +04:00
|
|
|
static int chkquota __P((const char *, const char *, const char *, void *,
|
|
|
|
pid_t *));
|
1996-09-28 03:25:34 +04:00
|
|
|
static int update __P((const char *, const char *, int));
|
|
|
|
static int oneof __P((char *, char *[], int));
|
|
|
|
static int getquotagid __P((void));
|
|
|
|
static int hasquota __P((struct fstab *, int, char **));
|
|
|
|
static struct fileusage *lookup __P((u_long, int));
|
|
|
|
static struct fileusage *addid __P((u_long, int, char *));
|
|
|
|
static struct dinode *getnextinode __P((ino_t));
|
|
|
|
static void resetinodebuf __P((void));
|
|
|
|
static void freeinodebuf __P((void));
|
|
|
|
static void bread __P((daddr_t, char *, long));
|
1994-06-08 23:27:32 +04:00
|
|
|
|
|
|
|
int
|
1993-04-09 23:25:07 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-06-08 23:27:32 +04:00
|
|
|
char *argv[];
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fstab *fs;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
1994-06-08 23:27:32 +04:00
|
|
|
struct quotaname *auxdata;
|
|
|
|
int i, argnum, maxrun, errs;
|
|
|
|
long done = 0;
|
1996-09-28 23:20:44 +04:00
|
|
|
int flags = CHECK_PREEN;
|
1996-03-31 01:34:25 +03:00
|
|
|
char *name;
|
|
|
|
int ch;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
errs = maxrun = 0;
|
1996-09-28 23:20:44 +04:00
|
|
|
while ((ch = getopt(argc, argv, "aguvdl:")) != -1) {
|
1993-04-09 23:25:07 +04:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
1996-09-28 23:20:44 +04:00
|
|
|
case 'd':
|
|
|
|
flags |= CHECK_DEBUG;
|
|
|
|
break;
|
1993-04-09 23:25:07 +04:00
|
|
|
case 'g':
|
|
|
|
gflag++;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag++;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag++;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
maxrun = atoi(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if ((argc == 0 && !aflag) || (argc > 0 && aflag))
|
|
|
|
usage();
|
|
|
|
if (!gflag && !uflag) {
|
|
|
|
gflag++;
|
|
|
|
uflag++;
|
|
|
|
}
|
|
|
|
if (gflag) {
|
|
|
|
setgrent();
|
|
|
|
while ((gr = getgrent()) != 0)
|
|
|
|
(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
|
|
|
|
endgrent();
|
|
|
|
}
|
|
|
|
if (uflag) {
|
|
|
|
setpwent();
|
|
|
|
while ((pw = getpwent()) != 0)
|
|
|
|
(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
if (aflag)
|
1996-09-28 23:20:44 +04:00
|
|
|
exit(checkfstab(flags, maxrun, needchk, chkquota));
|
1994-06-08 23:27:32 +04:00
|
|
|
if (setfsent() == 0)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s: can't open", FSTAB);
|
1993-04-09 23:25:07 +04:00
|
|
|
while ((fs = getfsent()) != NULL) {
|
|
|
|
if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
|
|
|
|
(argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
|
|
|
|
(auxdata = needchk(fs)) &&
|
|
|
|
(name = blockcheck(fs->fs_spec))) {
|
|
|
|
done |= 1 << argnum;
|
1996-09-28 03:25:34 +04:00
|
|
|
errs += chkquota(fs->fs_type, name, fs->fs_file,
|
1996-09-28 23:20:44 +04:00
|
|
|
auxdata, NULL);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
endfsent();
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if ((done & (1 << i)) == 0)
|
|
|
|
fprintf(stderr, "%s not found in %s\n",
|
|
|
|
argv[i], FSTAB);
|
|
|
|
exit(errs);
|
|
|
|
}
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static void
|
1993-04-09 23:25:07 +04:00
|
|
|
usage()
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
extern char *__progname;
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Usage:\t%s -a [-guv]\n\t%s [-guv] filesys ...\n", __progname,
|
|
|
|
__progname);
|
1993-04-09 23:25:07 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static void *
|
1993-04-09 23:25:07 +04:00
|
|
|
needchk(fs)
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fstab *fs;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct quotaname *qnp;
|
1993-04-09 23:25:07 +04:00
|
|
|
char *qfnp;
|
|
|
|
|
1995-11-28 08:25:28 +03:00
|
|
|
if (strcmp(fs->fs_vfstype, "ffs") ||
|
1993-04-09 23:25:07 +04:00
|
|
|
strcmp(fs->fs_type, FSTAB_RW))
|
1994-06-08 23:27:32 +04:00
|
|
|
return (NULL);
|
|
|
|
if ((qnp = malloc(sizeof(*qnp))) == NULL)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s", strerror(errno));
|
1993-04-09 23:25:07 +04:00
|
|
|
qnp->flags = 0;
|
|
|
|
if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
|
|
|
|
strcpy(qnp->grpqfname, qfnp);
|
|
|
|
qnp->flags |= HASGRP;
|
|
|
|
}
|
|
|
|
if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
|
|
|
|
strcpy(qnp->usrqfname, qfnp);
|
|
|
|
qnp->flags |= HASUSR;
|
|
|
|
}
|
|
|
|
if (qnp->flags)
|
1994-06-08 23:27:32 +04:00
|
|
|
return (qnp);
|
|
|
|
free(qnp);
|
|
|
|
return (NULL);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan the specified filesystem to check quota(s) present on it.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1996-09-28 23:20:44 +04:00
|
|
|
chkquota(type, fsname, mntpt, v, pid)
|
1996-09-28 03:25:34 +04:00
|
|
|
const char *type, *fsname, *mntpt;
|
|
|
|
void *v;
|
1996-09-28 23:20:44 +04:00
|
|
|
pid_t *pid;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct quotaname *qnp = v;
|
|
|
|
struct fileusage *fup;
|
|
|
|
struct dinode *dp;
|
1993-04-09 23:25:07 +04:00
|
|
|
int cg, i, mode, errs = 0;
|
|
|
|
ino_t ino;
|
|
|
|
|
1996-09-28 23:20:44 +04:00
|
|
|
if (pid != NULL) {
|
|
|
|
switch ((*pid = fork())) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
case -1:
|
|
|
|
err(1, "Cannot fork");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
|
1996-09-28 23:20:44 +04:00
|
|
|
warn("Cannot open %s", fsname);
|
1993-04-09 23:25:07 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (vflag) {
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("*** Checking ");
|
1993-04-09 23:25:07 +04:00
|
|
|
if (qnp->flags & HASUSR)
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("%s%s", qfextension[USRQUOTA],
|
1993-04-09 23:25:07 +04:00
|
|
|
(qnp->flags & HASGRP) ? " and " : "");
|
|
|
|
if (qnp->flags & HASGRP)
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("%s", qfextension[GRPQUOTA]);
|
|
|
|
(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
sync();
|
1994-08-16 23:22:02 +04:00
|
|
|
dev_bsize = 1;
|
1994-06-08 23:27:32 +04:00
|
|
|
bread(SBOFF, (char *)&sblock, (long)SBSIZE);
|
1993-04-09 23:25:07 +04:00
|
|
|
dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
|
|
|
|
maxino = sblock.fs_ncg * sblock.fs_ipg;
|
|
|
|
resetinodebuf();
|
|
|
|
for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
|
|
|
|
for (i = 0; i < sblock.fs_ipg; i++, ino++) {
|
|
|
|
if (ino < ROOTINO)
|
|
|
|
continue;
|
|
|
|
if ((dp = getnextinode(ino)) == NULL)
|
|
|
|
continue;
|
|
|
|
if ((mode = dp->di_mode & IFMT) == 0)
|
|
|
|
continue;
|
|
|
|
if (qnp->flags & HASGRP) {
|
|
|
|
fup = addid((u_long)dp->di_gid, GRPQUOTA,
|
|
|
|
(char *)0);
|
|
|
|
fup->fu_curinodes++;
|
|
|
|
if (mode == IFREG || mode == IFDIR ||
|
|
|
|
mode == IFLNK)
|
|
|
|
fup->fu_curblocks += dp->di_blocks;
|
|
|
|
}
|
|
|
|
if (qnp->flags & HASUSR) {
|
|
|
|
fup = addid((u_long)dp->di_uid, USRQUOTA,
|
|
|
|
(char *)0);
|
|
|
|
fup->fu_curinodes++;
|
|
|
|
if (mode == IFREG || mode == IFDIR ||
|
|
|
|
mode == IFLNK)
|
|
|
|
fup->fu_curblocks += dp->di_blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeinodebuf();
|
|
|
|
if (qnp->flags & HASUSR)
|
|
|
|
errs += update(mntpt, qnp->usrqfname, USRQUOTA);
|
|
|
|
if (qnp->flags & HASGRP)
|
|
|
|
errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
|
|
|
|
close(fi);
|
|
|
|
return (errs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update a specified quota file.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
update(fsname, quotafile, type)
|
1996-09-28 03:25:34 +04:00
|
|
|
const char *fsname, *quotafile;
|
|
|
|
int type;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fileusage *fup;
|
|
|
|
FILE *qfi, *qfo;
|
|
|
|
u_long id, lastid;
|
1993-04-09 23:25:07 +04:00
|
|
|
struct dqblk dqbuf;
|
|
|
|
static int warned = 0;
|
|
|
|
static struct dqblk zerodqbuf;
|
|
|
|
static struct fileusage zerofileusage;
|
|
|
|
|
|
|
|
if ((qfo = fopen(quotafile, "r+")) == NULL) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
qfo = fopen(quotafile, "w+");
|
|
|
|
if (qfo) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"quotacheck: creating quota file %s\n", quotafile);
|
|
|
|
#define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
|
|
|
|
(void) fchown(fileno(qfo), getuid(), getquotagid());
|
|
|
|
(void) fchmod(fileno(qfo), MODE);
|
|
|
|
} else {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"quotacheck: %s: %s\n", quotafile, strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((qfi = fopen(quotafile, "r")) == NULL) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"quotacheck: %s: %s\n", quotafile, strerror(errno));
|
|
|
|
(void) fclose(qfo);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
|
|
|
|
errno == EOPNOTSUPP && !warned && vflag) {
|
|
|
|
warned++;
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("*** Warning: %s\n",
|
1993-04-09 23:25:07 +04:00
|
|
|
"Quotas are not compiled into this kernel");
|
|
|
|
}
|
|
|
|
for (lastid = highid[type], id = 0; id <= lastid; id++) {
|
|
|
|
if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
|
|
|
|
dqbuf = zerodqbuf;
|
|
|
|
if ((fup = lookup(id, type)) == 0)
|
|
|
|
fup = &zerofileusage;
|
|
|
|
if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
|
|
|
|
dqbuf.dqb_curblocks == fup->fu_curblocks) {
|
|
|
|
fup->fu_curinodes = 0;
|
|
|
|
fup->fu_curblocks = 0;
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fseek(qfo, (long)sizeof(struct dqblk), 1);
|
1993-04-09 23:25:07 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (vflag) {
|
|
|
|
if (aflag)
|
|
|
|
printf("%s: ", fsname);
|
|
|
|
printf("%-8s fixed:", fup->fu_name);
|
|
|
|
if (dqbuf.dqb_curinodes != fup->fu_curinodes)
|
1996-09-28 03:25:34 +04:00
|
|
|
(void)printf("\tinodes %d -> %ld",
|
1993-04-09 23:25:07 +04:00
|
|
|
dqbuf.dqb_curinodes, fup->fu_curinodes);
|
|
|
|
if (dqbuf.dqb_curblocks != fup->fu_curblocks)
|
1996-09-28 03:25:34 +04:00
|
|
|
(void)printf("\tblocks %d -> %ld",
|
1993-04-09 23:25:07 +04:00
|
|
|
dqbuf.dqb_curblocks, fup->fu_curblocks);
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("\n");
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Reset time limit if have a soft limit and were
|
|
|
|
* previously under it, but are now over it.
|
|
|
|
*/
|
|
|
|
if (dqbuf.dqb_bsoftlimit &&
|
|
|
|
dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
|
|
|
|
fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
|
|
|
|
dqbuf.dqb_btime = 0;
|
|
|
|
if (dqbuf.dqb_isoftlimit &&
|
|
|
|
dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
|
|
|
|
fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
|
|
|
|
dqbuf.dqb_itime = 0;
|
|
|
|
dqbuf.dqb_curinodes = fup->fu_curinodes;
|
|
|
|
dqbuf.dqb_curblocks = fup->fu_curblocks;
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
|
1993-04-09 23:25:07 +04:00
|
|
|
(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
|
|
|
|
(caddr_t)&dqbuf);
|
|
|
|
fup->fu_curinodes = 0;
|
|
|
|
fup->fu_curblocks = 0;
|
|
|
|
}
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fclose(qfi);
|
|
|
|
(void) fflush(qfo);
|
|
|
|
(void) ftruncate(fileno(qfo),
|
1993-04-09 23:25:07 +04:00
|
|
|
(off_t)((highid[type] + 1) * sizeof(struct dqblk)));
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fclose(qfo);
|
1993-04-09 23:25:07 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if target appears in list of size cnt.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
oneof(target, list, cnt)
|
1996-09-28 03:25:34 +04:00
|
|
|
char *target, *list[];
|
1993-04-09 23:25:07 +04:00
|
|
|
int cnt;
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
int i;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
if (strcmp(target, list[i]) == 0)
|
|
|
|
return (i);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the group identifier for quota files.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
getquotagid()
|
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
if ((gr = getgrnam(quotagroup)) != NULL)
|
1993-04-09 23:25:07 +04:00
|
|
|
return (gr->gr_gid);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if a particular quota is to be enabled.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
hasquota(fs, type, qfnamep)
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fstab *fs;
|
1993-04-09 23:25:07 +04:00
|
|
|
int type;
|
|
|
|
char **qfnamep;
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
char *opt;
|
1997-09-20 01:22:38 +04:00
|
|
|
char *cp = NULL;
|
1993-04-09 23:25:07 +04:00
|
|
|
static char initname, usrname[100], grpname[100];
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
|
|
|
|
if (!initname) {
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)snprintf(usrname, sizeof(usrname),
|
|
|
|
"%s%s", qfextension[USRQUOTA], qfname);
|
|
|
|
(void)snprintf(grpname, sizeof(grpname),
|
|
|
|
"%s%s", qfextension[GRPQUOTA], qfname);
|
1993-04-09 23:25:07 +04:00
|
|
|
initname = 1;
|
|
|
|
}
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) strcpy(buf, fs->fs_mntops);
|
1993-04-09 23:25:07 +04:00
|
|
|
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
|
1996-09-28 03:25:34 +04:00
|
|
|
if ((cp = strchr(opt, '=')) != NULL)
|
1993-04-09 23:25:07 +04:00
|
|
|
*cp++ = '\0';
|
|
|
|
if (type == USRQUOTA && strcmp(opt, usrname) == 0)
|
|
|
|
break;
|
|
|
|
if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!opt)
|
|
|
|
return (0);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (cp)
|
1993-04-09 23:25:07 +04:00
|
|
|
*qfnamep = cp;
|
1994-06-08 23:27:32 +04:00
|
|
|
else {
|
|
|
|
(void)snprintf(buf, sizeof(buf),
|
|
|
|
"%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
|
|
|
|
*qfnamep = buf;
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to manage the file usage table.
|
|
|
|
*
|
|
|
|
* Lookup an id of a specific type.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct fileusage *
|
1993-04-09 23:25:07 +04:00
|
|
|
lookup(id, type)
|
|
|
|
u_long id;
|
|
|
|
int type;
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fileusage *fup;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
|
|
|
|
if (fup->fu_id == id)
|
|
|
|
return (fup);
|
1994-06-08 23:27:32 +04:00
|
|
|
return (NULL);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new file usage id if it does not already exist.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct fileusage *
|
1993-04-09 23:25:07 +04:00
|
|
|
addid(id, type, name)
|
|
|
|
u_long id;
|
|
|
|
int type;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
struct fileusage *fup, **fhp;
|
|
|
|
int len;
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
if ((fup = lookup(id, type)) != NULL)
|
1993-04-09 23:25:07 +04:00
|
|
|
return (fup);
|
|
|
|
if (name)
|
|
|
|
len = strlen(name);
|
|
|
|
else
|
|
|
|
len = 10;
|
1994-06-08 23:27:32 +04:00
|
|
|
if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s", strerror(errno));
|
1993-04-09 23:25:07 +04:00
|
|
|
fhp = &fuhead[type][id & (FUHASH - 1)];
|
|
|
|
fup->fu_next = *fhp;
|
|
|
|
*fhp = fup;
|
|
|
|
fup->fu_id = id;
|
|
|
|
if (id > highid[type])
|
|
|
|
highid[type] = id;
|
1994-06-08 23:27:32 +04:00
|
|
|
if (name)
|
1997-09-16 17:07:59 +04:00
|
|
|
memmove(fup->fu_name, name, len + 1);
|
1994-06-08 23:27:32 +04:00
|
|
|
else
|
1996-09-28 03:25:34 +04:00
|
|
|
(void)sprintf(fup->fu_name, "%lu", id);
|
1993-04-09 23:25:07 +04:00
|
|
|
return (fup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special purpose version of ginode used to optimize pass
|
|
|
|
* over all the inodes in numerical order.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static ino_t nextino, lastinum;
|
|
|
|
static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
|
|
|
|
static struct dinode *inodebuf;
|
1993-04-09 23:25:07 +04:00
|
|
|
#define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct dinode *
|
1993-04-09 23:25:07 +04:00
|
|
|
getnextinode(inumber)
|
|
|
|
ino_t inumber;
|
|
|
|
{
|
|
|
|
long size;
|
|
|
|
daddr_t dblk;
|
|
|
|
static struct dinode *dp;
|
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
if (inumber != nextino++ || inumber > maxino)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "bad inode number %d to nextinode", inumber);
|
1993-04-09 23:25:07 +04:00
|
|
|
if (inumber >= lastinum) {
|
|
|
|
readcnt++;
|
1994-06-08 23:27:32 +04:00
|
|
|
dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
|
1993-04-09 23:25:07 +04:00
|
|
|
if (readcnt % readpercg == 0) {
|
|
|
|
size = partialsize;
|
|
|
|
lastinum += partialcnt;
|
|
|
|
} else {
|
|
|
|
size = inobufsize;
|
|
|
|
lastinum += fullcnt;
|
|
|
|
}
|
|
|
|
bread(dblk, (char *)inodebuf, size);
|
|
|
|
dp = inodebuf;
|
|
|
|
}
|
|
|
|
return (dp++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to scan a set of inodes.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static void
|
1993-04-09 23:25:07 +04:00
|
|
|
resetinodebuf()
|
|
|
|
{
|
|
|
|
|
|
|
|
nextino = 0;
|
|
|
|
lastinum = 0;
|
|
|
|
readcnt = 0;
|
|
|
|
inobufsize = blkroundup(&sblock, INOBUFSIZE);
|
|
|
|
fullcnt = inobufsize / sizeof(struct dinode);
|
|
|
|
readpercg = sblock.fs_ipg / fullcnt;
|
|
|
|
partialcnt = sblock.fs_ipg % fullcnt;
|
|
|
|
partialsize = partialcnt * sizeof(struct dinode);
|
|
|
|
if (partialcnt != 0) {
|
|
|
|
readpercg++;
|
|
|
|
} else {
|
|
|
|
partialcnt = fullcnt;
|
|
|
|
partialsize = inobufsize;
|
|
|
|
}
|
|
|
|
if (inodebuf == NULL &&
|
1994-06-08 23:27:32 +04:00
|
|
|
(inodebuf = malloc((u_int)inobufsize)) == NULL)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s", strerror(errno));
|
1993-04-09 23:25:07 +04:00
|
|
|
while (nextino < ROOTINO)
|
|
|
|
getnextinode(nextino);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free up data structures used to scan inodes.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static void
|
1993-04-09 23:25:07 +04:00
|
|
|
freeinodebuf()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (inodebuf != NULL)
|
1994-06-08 23:27:32 +04:00
|
|
|
free(inodebuf);
|
1993-04-09 23:25:07 +04:00
|
|
|
inodebuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read specified disk blocks.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static void
|
1993-04-09 23:25:07 +04:00
|
|
|
bread(bno, buf, cnt)
|
|
|
|
daddr_t bno;
|
|
|
|
char *buf;
|
|
|
|
long cnt;
|
|
|
|
{
|
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
|
|
|
|
read(fi, buf, cnt) != cnt)
|
1996-09-28 03:25:34 +04:00
|
|
|
err(1, "block %d", bno);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|