1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-05-03 18:41:59 +00:00
|
|
|
#if 0
|
1994-05-26 06:35:07 +00:00
|
|
|
#ifndef lint
|
1998-07-15 06:48:52 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1990, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94";
|
|
|
|
#endif /* not lint */
|
2003-05-03 18:41:59 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix up / report on disk quotas & usage
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
2002-05-12 21:37:08 +00:00
|
|
|
#include <sys/disklabel.h>
|
2007-02-04 06:33:15 +00:00
|
|
|
#include <sys/mount.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/quota.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
|
1998-07-15 06:48:52 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
#include <grp.h>
|
2009-12-27 06:28:01 +00:00
|
|
|
#include <libutil.h>
|
1998-07-15 06:48:52 +00:00
|
|
|
#include <pwd.h>
|
2012-09-27 23:31:06 +00:00
|
|
|
#include <stdint.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-07-15 06:48:52 +00:00
|
|
|
#include <unistd.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2008-01-26 12:03:26 +00:00
|
|
|
#include "quotacheck.h"
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
char *qfname = QUOTAFILENAME;
|
|
|
|
char *qfextension[] = INITQFNAMES;
|
|
|
|
char *quotagroup = QUOTAGROUP;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct fs sblk;
|
|
|
|
char dummy[MAXBSIZE];
|
2002-11-20 02:13:32 +00:00
|
|
|
} sb_un;
|
|
|
|
#define sblock sb_un.sblk
|
|
|
|
union {
|
|
|
|
struct cg cgblk;
|
|
|
|
char dummy[MAXBSIZE];
|
|
|
|
} cg_un;
|
|
|
|
#define cgblk cg_un.cgblk
|
1994-05-26 06:35:07 +00:00
|
|
|
long dev_bsize = 1;
|
2002-01-25 20:45:30 +00:00
|
|
|
ino_t maxino;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode {
|
|
|
|
struct ufs1_dinode dp1;
|
|
|
|
struct ufs2_dinode dp2;
|
|
|
|
};
|
|
|
|
#define DIP(dp, field) \
|
|
|
|
((sblock.fs_magic == FS_UFS1_MAGIC) ? \
|
|
|
|
(dp)->dp1.field : (dp)->dp2.field)
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#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 */
|
|
|
|
struct fileusage *fuhead[MAXQUOTAS][FUHASH];
|
|
|
|
|
2002-08-21 18:11:48 +00:00
|
|
|
int aflag; /* all file systems */
|
2009-12-28 23:01:47 +00:00
|
|
|
int cflag; /* convert format to 32 or 64 bit size */
|
1994-05-26 06:35:07 +00:00
|
|
|
int gflag; /* check group quotas */
|
|
|
|
int uflag; /* check user quotas */
|
|
|
|
int vflag; /* verbose */
|
|
|
|
int fi; /* open disk file descriptor */
|
|
|
|
|
|
|
|
struct fileusage *
|
2009-12-27 06:28:01 +00:00
|
|
|
addid(u_long, int, char *, const char *);
|
2002-11-20 02:13:32 +00:00
|
|
|
void bread(ufs2_daddr_t, char *, long);
|
2002-03-21 13:20:49 +00:00
|
|
|
void freeinodebuf(void);
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode *
|
2002-03-21 13:20:49 +00:00
|
|
|
getnextinode(ino_t);
|
|
|
|
int getquotagid(void);
|
1994-05-26 06:35:07 +00:00
|
|
|
struct fileusage *
|
2002-03-21 13:20:49 +00:00
|
|
|
lookup(u_long, int);
|
|
|
|
int oneof(char *, char*[], int);
|
2009-12-27 06:28:01 +00:00
|
|
|
void printchanges(const char *, int, struct dqblk *, struct fileusage *,
|
|
|
|
u_long);
|
2002-11-20 02:13:32 +00:00
|
|
|
void setinodebuf(ino_t);
|
2009-12-27 06:28:01 +00:00
|
|
|
int update(const char *, struct quotafile *, int);
|
2002-03-21 13:20:49 +00:00
|
|
|
void usage(void);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
int
|
2008-07-02 15:51:59 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-01-25 20:45:30 +00:00
|
|
|
struct fstab *fs;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
2009-12-27 06:28:01 +00:00
|
|
|
struct quotafile *qfu, *qfg;
|
2004-01-22 07:23:36 +00:00
|
|
|
int i, argnum, maxrun, errs, ch;
|
1994-05-26 06:35:07 +00:00
|
|
|
long done = 0;
|
2004-01-22 07:23:36 +00:00
|
|
|
char *name;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
errs = maxrun = 0;
|
2009-12-28 23:01:47 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ac:guvl:")) != -1) {
|
1994-05-26 06:35:07 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
2009-12-28 23:01:47 +00:00
|
|
|
case 'c':
|
|
|
|
if (cflag)
|
|
|
|
usage();
|
|
|
|
cflag = atoi(optarg);
|
|
|
|
break;
|
1994-05-26 06:35:07 +00: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();
|
2009-12-28 23:01:47 +00:00
|
|
|
if (cflag && cflag != 32 && cflag != 64)
|
|
|
|
usage();
|
1994-05-26 06:35:07 +00:00
|
|
|
if (!gflag && !uflag) {
|
|
|
|
gflag++;
|
|
|
|
uflag++;
|
|
|
|
}
|
|
|
|
if (gflag) {
|
|
|
|
setgrent();
|
1996-02-27 08:04:48 +00:00
|
|
|
while ((gr = getgrent()) != NULL)
|
2007-01-20 12:28:15 +00:00
|
|
|
(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name,
|
|
|
|
NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
endgrent();
|
|
|
|
}
|
|
|
|
if (uflag) {
|
|
|
|
setpwent();
|
1996-02-27 08:04:48 +00:00
|
|
|
while ((pw = getpwent()) != NULL)
|
2007-01-20 12:28:15 +00:00
|
|
|
(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name,
|
|
|
|
NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
endpwent();
|
|
|
|
}
|
2006-11-07 19:07:52 +00:00
|
|
|
/*
|
2008-01-26 12:03:26 +00:00
|
|
|
* The maxrun (-l) option is now deprecated.
|
2006-11-07 19:07:52 +00:00
|
|
|
*/
|
2008-01-26 12:03:26 +00:00
|
|
|
if (maxrun > 0)
|
|
|
|
warnx("the -l option is now deprecated");
|
1994-05-26 06:35:07 +00:00
|
|
|
if (aflag)
|
2009-12-27 06:28:01 +00:00
|
|
|
exit(checkfstab(uflag, gflag));
|
1994-05-26 06:35:07 +00:00
|
|
|
if (setfsent() == 0)
|
1997-06-16 06:38:14 +00:00
|
|
|
errx(1, "%s: can't open", FSTAB);
|
1994-05-26 06:35:07 +00:00
|
|
|
while ((fs = getfsent()) != NULL) {
|
|
|
|
if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
|
2009-12-27 06:28:01 +00:00
|
|
|
(argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
|
1994-05-26 06:35:07 +00:00
|
|
|
(name = blockcheck(fs->fs_spec))) {
|
|
|
|
done |= 1 << argnum;
|
2009-12-27 06:28:01 +00:00
|
|
|
qfu = NULL;
|
|
|
|
if (uflag)
|
|
|
|
qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR);
|
|
|
|
qfg = NULL;
|
|
|
|
if (gflag)
|
|
|
|
qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR);
|
|
|
|
if (qfu == NULL && qfg == NULL)
|
|
|
|
continue;
|
|
|
|
errs += chkquota(name, qfu, qfg);
|
|
|
|
if (qfu)
|
|
|
|
quota_close(qfu);
|
|
|
|
if (qfg)
|
|
|
|
quota_close(qfg);
|
1994-05-26 06:35:07 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-07-02 15:51:59 +00:00
|
|
|
usage(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2008-07-02 15:51:59 +00:00
|
|
|
(void)fprintf(stderr, "%s\n%s\n",
|
2009-12-28 23:01:47 +00:00
|
|
|
"usage: quotacheck [-guv] [-c 32 | 64] [-l maxrun] -a",
|
|
|
|
" quotacheck [-guv] [-c 32 | 64] filesystem ...");
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-06-21 06:18:05 +00:00
|
|
|
/*
|
|
|
|
* Possible superblock locations ordered from most to least likely.
|
|
|
|
*/
|
|
|
|
static int sblock_try[] = SBLOCKSEARCH;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
2002-08-21 18:11:48 +00:00
|
|
|
* Scan the specified file system to check quota(s) present on it.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
|
|
|
int
|
2009-12-27 06:28:01 +00:00
|
|
|
chkquota(char *specname, struct quotafile *qfu, struct quotafile *qfg)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-01-25 20:45:30 +00:00
|
|
|
struct fileusage *fup;
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode *dp;
|
1994-05-26 06:35:07 +00:00
|
|
|
int cg, i, mode, errs = 0;
|
2007-01-20 12:28:15 +00:00
|
|
|
ino_t ino, inosused, userino = 0, groupino = 0;
|
2008-01-15 06:33:20 +00:00
|
|
|
dev_t dev, userdev = 0, groupdev = 0;
|
2007-01-20 12:28:15 +00:00
|
|
|
struct stat sb;
|
2009-12-27 06:28:01 +00:00
|
|
|
const char *mntpt;
|
|
|
|
char *cp;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfu != NULL)
|
|
|
|
mntpt = quota_fsname(qfu);
|
|
|
|
else if (qfg != NULL)
|
|
|
|
mntpt = quota_fsname(qfg);
|
|
|
|
else
|
2009-12-28 23:01:47 +00:00
|
|
|
errx(1, "null quotafile information passed to chkquota()\n");
|
|
|
|
if (cflag) {
|
|
|
|
if (vflag && qfu != NULL)
|
|
|
|
printf("%s: convert user quota to %d bits\n",
|
|
|
|
mntpt, cflag);
|
|
|
|
if (qfu != NULL && quota_convert(qfu, cflag) < 0) {
|
|
|
|
if (errno == EBADF)
|
|
|
|
errx(1,
|
|
|
|
"%s: cannot convert an active quota file",
|
|
|
|
mntpt);
|
|
|
|
err(1, "user quota conversion to size %d failed",
|
|
|
|
cflag);
|
|
|
|
}
|
|
|
|
if (vflag && qfg != NULL)
|
|
|
|
printf("%s: convert group quota to %d bits\n",
|
|
|
|
mntpt, cflag);
|
|
|
|
if (qfg != NULL && quota_convert(qfg, cflag) < 0) {
|
|
|
|
if (errno == EBADF)
|
|
|
|
errx(1,
|
|
|
|
"%s: cannot convert an active quota file",
|
|
|
|
mntpt);
|
|
|
|
err(1, "group quota conversion to size %d failed",
|
|
|
|
cflag);
|
|
|
|
}
|
|
|
|
}
|
2009-12-27 06:28:01 +00:00
|
|
|
if ((fi = open(specname, O_RDONLY, 0)) < 0) {
|
|
|
|
warn("%s", specname);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2008-01-15 06:33:20 +00:00
|
|
|
if ((stat(mntpt, &sb)) < 0) {
|
|
|
|
warn("%s", mntpt);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
dev = sb.st_dev;
|
1994-05-26 06:35:07 +00:00
|
|
|
if (vflag) {
|
|
|
|
(void)printf("*** Checking ");
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfu)
|
|
|
|
(void)printf("user%s", qfg ? " and " : "");
|
|
|
|
if (qfg)
|
|
|
|
(void)printf("group");
|
|
|
|
(void)printf(" quotas for %s (%s)\n", specname, mntpt);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfu) {
|
|
|
|
if (stat(quota_qfname(qfu), &sb) == 0) {
|
2007-01-20 12:28:15 +00:00
|
|
|
userino = sb.st_ino;
|
2008-01-15 06:33:20 +00:00
|
|
|
userdev = sb.st_dev;
|
|
|
|
}
|
2007-01-20 12:28:15 +00:00
|
|
|
}
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfg) {
|
|
|
|
if (stat(quota_qfname(qfg), &sb) == 0) {
|
2007-01-20 12:28:15 +00:00
|
|
|
groupino = sb.st_ino;
|
2008-01-15 06:33:20 +00:00
|
|
|
groupdev = sb.st_dev;
|
|
|
|
}
|
2007-01-20 12:28:15 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
sync();
|
1995-06-21 03:55:12 +00:00
|
|
|
dev_bsize = 1;
|
2002-06-21 06:18:05 +00:00
|
|
|
for (i = 0; sblock_try[i] != -1; i++) {
|
|
|
|
bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
|
|
|
|
if ((sblock.fs_magic == FS_UFS1_MAGIC ||
|
|
|
|
(sblock.fs_magic == FS_UFS2_MAGIC &&
|
2002-11-27 02:18:58 +00:00
|
|
|
sblock.fs_sblockloc == sblock_try[i])) &&
|
2002-06-21 06:18:05 +00:00
|
|
|
sblock.fs_bsize <= MAXBSIZE &&
|
|
|
|
sblock.fs_bsize >= sizeof(struct fs))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sblock_try[i] == -1) {
|
2002-08-21 18:11:48 +00:00
|
|
|
warn("Cannot find file system superblock");
|
2002-06-21 06:18:05 +00:00
|
|
|
return (1);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
|
|
|
|
maxino = sblock.fs_ncg * sblock.fs_ipg;
|
2002-11-20 02:13:32 +00:00
|
|
|
for (cg = 0; cg < sblock.fs_ncg; cg++) {
|
|
|
|
ino = cg * sblock.fs_ipg;
|
|
|
|
setinodebuf(ino);
|
|
|
|
bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
|
|
|
|
sblock.fs_cgsize);
|
|
|
|
if (sblock.fs_magic == FS_UFS2_MAGIC)
|
|
|
|
inosused = cgblk.cg_initediblk;
|
|
|
|
else
|
|
|
|
inosused = sblock.fs_ipg;
|
|
|
|
/*
|
|
|
|
* If we are using soft updates, then we can trust the
|
|
|
|
* cylinder group inode allocation maps to tell us which
|
|
|
|
* inodes are allocated. We will scan the used inode map
|
|
|
|
* to find the inodes that are really in use, and then
|
|
|
|
* read only those inodes in from disk.
|
|
|
|
*/
|
|
|
|
if (sblock.fs_flags & FS_DOSOFTDEP) {
|
|
|
|
if (!cg_chkmagic(&cgblk))
|
|
|
|
errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
|
|
|
|
cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
|
|
|
|
for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
|
|
|
|
if (*cp == 0)
|
|
|
|
continue;
|
|
|
|
for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
|
|
|
|
if (*cp & i)
|
|
|
|
break;
|
|
|
|
inosused--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (inosused <= 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
2002-11-20 02:13:32 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < inosused; i++, ino++) {
|
|
|
|
if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO ||
|
|
|
|
(mode = DIP(dp, di_mode) & IFMT) == 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
2007-01-20 12:28:15 +00:00
|
|
|
/*
|
|
|
|
* XXX: Do not account for UIDs or GIDs that appear
|
|
|
|
* to be negative to prevent generating 100GB+
|
|
|
|
* quota files.
|
|
|
|
*/
|
2008-07-02 15:51:59 +00:00
|
|
|
if ((int)DIP(dp, di_uid) < 0 ||
|
2007-01-20 12:28:15 +00:00
|
|
|
(int)DIP(dp, di_gid) < 0) {
|
|
|
|
if (vflag) {
|
|
|
|
if (aflag)
|
|
|
|
(void)printf("%s: ", mntpt);
|
2012-09-27 23:31:06 +00:00
|
|
|
(void)printf("out of range UID/GID (%u/%u) ino=%ju\n",
|
2007-01-20 12:28:15 +00:00
|
|
|
DIP(dp, di_uid), DIP(dp,di_gid),
|
2012-09-27 23:31:06 +00:00
|
|
|
(uintmax_t)ino);
|
2007-01-20 12:28:15 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-07-02 15:51:59 +00:00
|
|
|
/*
|
2007-01-20 12:28:15 +00:00
|
|
|
* Do not account for file system snapshot files
|
|
|
|
* or the actual quota data files to be consistent
|
|
|
|
* with how they are handled inside the kernel.
|
|
|
|
*/
|
|
|
|
#ifdef SF_SNAPSHOT
|
|
|
|
if (DIP(dp, di_flags) & SF_SNAPSHOT)
|
|
|
|
continue;
|
|
|
|
#endif
|
2008-01-15 06:33:20 +00:00
|
|
|
if ((ino == userino && dev == userdev) ||
|
|
|
|
(ino == groupino && dev == groupdev))
|
2007-01-20 12:28:15 +00:00
|
|
|
continue;
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfg) {
|
2002-06-21 06:18:05 +00:00
|
|
|
fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
|
2007-01-20 12:28:15 +00:00
|
|
|
(char *)0, mntpt);
|
1994-05-26 06:35:07 +00:00
|
|
|
fup->fu_curinodes++;
|
|
|
|
if (mode == IFREG || mode == IFDIR ||
|
|
|
|
mode == IFLNK)
|
2002-06-21 06:18:05 +00:00
|
|
|
fup->fu_curblocks += DIP(dp, di_blocks);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfu) {
|
2002-06-21 06:18:05 +00:00
|
|
|
fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
|
2007-01-20 12:28:15 +00:00
|
|
|
(char *)0, mntpt);
|
1994-05-26 06:35:07 +00:00
|
|
|
fup->fu_curinodes++;
|
|
|
|
if (mode == IFREG || mode == IFDIR ||
|
|
|
|
mode == IFLNK)
|
2002-06-21 06:18:05 +00:00
|
|
|
fup->fu_curblocks += DIP(dp, di_blocks);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeinodebuf();
|
2009-12-27 06:28:01 +00:00
|
|
|
if (qfu)
|
|
|
|
errs += update(mntpt, qfu, USRQUOTA);
|
|
|
|
if (qfg)
|
|
|
|
errs += update(mntpt, qfg, GRPQUOTA);
|
1994-05-26 06:35:07 +00:00
|
|
|
close(fi);
|
2007-09-14 03:12:19 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (errs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update a specified quota file.
|
|
|
|
*/
|
|
|
|
int
|
2009-12-27 06:28:01 +00:00
|
|
|
update(const char *fsname, struct quotafile *qf, int type)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-01-25 20:45:30 +00:00
|
|
|
struct fileusage *fup;
|
2007-01-20 12:28:15 +00:00
|
|
|
u_long id, lastid, highid = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct dqblk dqbuf;
|
2009-12-28 23:01:47 +00:00
|
|
|
struct stat sb;
|
1994-05-26 06:35:07 +00:00
|
|
|
static struct dqblk zerodqbuf;
|
|
|
|
static struct fileusage zerofileusage;
|
|
|
|
|
2007-01-20 12:28:15 +00:00
|
|
|
/*
|
|
|
|
* Scan the on-disk quota file and record any usage changes.
|
|
|
|
*/
|
2009-12-27 06:28:01 +00:00
|
|
|
lastid = quota_maxid(qf);
|
|
|
|
for (id = 0; id <= lastid; id++) {
|
|
|
|
if (quota_read(qf, &dqbuf, id) < 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
dqbuf = zerodqbuf;
|
2007-01-20 12:28:15 +00:00
|
|
|
if ((fup = lookup(id, type)) == NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
fup = &zerofileusage;
|
2007-01-20 12:28:15 +00:00
|
|
|
if (fup->fu_curinodes || fup->fu_curblocks ||
|
|
|
|
dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
|
|
|
|
dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
|
|
|
|
highid = id;
|
1994-05-26 06:35:07 +00:00
|
|
|
if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
|
|
|
|
dqbuf.dqb_curblocks == fup->fu_curblocks) {
|
|
|
|
fup->fu_curinodes = 0;
|
|
|
|
fup->fu_curblocks = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-23 02:10:19 +00:00
|
|
|
printchanges(fsname, type, &dqbuf, fup, id);
|
1994-05-26 06:35:07 +00:00
|
|
|
dqbuf.dqb_curinodes = fup->fu_curinodes;
|
|
|
|
dqbuf.dqb_curblocks = fup->fu_curblocks;
|
2009-12-27 06:28:01 +00:00
|
|
|
(void) quota_write_usage(qf, &dqbuf, id);
|
1994-05-26 06:35:07 +00:00
|
|
|
fup->fu_curinodes = 0;
|
|
|
|
fup->fu_curblocks = 0;
|
|
|
|
}
|
2007-01-20 12:28:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the hash table looking for ids with non-zero usage
|
|
|
|
* that are not currently recorded in the quota file. E.g.
|
|
|
|
* ids that are past the end of the current file.
|
|
|
|
*/
|
2009-12-27 06:28:01 +00:00
|
|
|
for (id = 0; id < FUHASH; id++) {
|
|
|
|
for (fup = fuhead[type][id]; fup != NULL; fup = fup->fu_next) {
|
2007-01-20 12:28:15 +00:00
|
|
|
if (fup->fu_id <= lastid)
|
|
|
|
continue;
|
|
|
|
if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
|
|
|
|
continue;
|
|
|
|
bzero(&dqbuf, sizeof(struct dqblk));
|
|
|
|
if (fup->fu_id > highid)
|
|
|
|
highid = fup->fu_id;
|
2009-12-27 06:28:01 +00:00
|
|
|
printchanges(fsname, type, &dqbuf, fup, fup->fu_id);
|
2007-01-20 12:28:15 +00:00
|
|
|
dqbuf.dqb_curinodes = fup->fu_curinodes;
|
|
|
|
dqbuf.dqb_curblocks = fup->fu_curblocks;
|
2009-12-27 06:28:01 +00:00
|
|
|
(void) quota_write_usage(qf, &dqbuf, fup->fu_id);
|
2007-01-20 12:28:15 +00:00
|
|
|
fup->fu_curinodes = 0;
|
|
|
|
fup->fu_curblocks = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-12-28 23:01:47 +00:00
|
|
|
/*
|
|
|
|
* If this is old format file, then size may be smaller,
|
|
|
|
* so ensure that we only truncate when it will make things
|
|
|
|
* smaller, and not if it will grow an old format file.
|
|
|
|
*/
|
|
|
|
if (highid < lastid &&
|
|
|
|
stat(quota_qfname(qf), &sb) == 0 &&
|
|
|
|
sb.st_size > (((off_t)highid + 2) * sizeof(struct dqblk)))
|
2009-12-27 06:28:01 +00:00
|
|
|
truncate(quota_qfname(qf),
|
|
|
|
(((off_t)highid + 2) * sizeof(struct dqblk)));
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if target appears in list of size cnt.
|
|
|
|
*/
|
|
|
|
int
|
2008-07-02 15:51:59 +00:00
|
|
|
oneof(char *target, char *list[], int cnt)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-01-25 20:45:30 +00:00
|
|
|
int i;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
if (strcmp(target, list[i]) == 0)
|
|
|
|
return (i);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the group identifier for quota files.
|
|
|
|
*/
|
|
|
|
int
|
2008-07-02 15:51:59 +00:00
|
|
|
getquotagid(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
|
1996-02-27 08:04:48 +00:00
|
|
|
if ((gr = getgrnam(quotagroup)) != NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
return (gr->gr_gid);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to manage the file usage table.
|
|
|
|
*
|
|
|
|
* Lookup an id of a specific type.
|
|
|
|
*/
|
|
|
|
struct fileusage *
|
2008-07-02 15:51:59 +00:00
|
|
|
lookup(u_long id, int type)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-01-25 20:45:30 +00:00
|
|
|
struct fileusage *fup;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
|
|
|
|
if (fup->fu_id == id)
|
|
|
|
return (fup);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new file usage id if it does not already exist.
|
|
|
|
*/
|
|
|
|
struct fileusage *
|
2009-12-27 06:28:01 +00:00
|
|
|
addid(u_long id, int type, char *name, const char *fsname)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
struct fileusage *fup, **fhp;
|
|
|
|
int len;
|
|
|
|
|
1996-02-27 08:04:48 +00:00
|
|
|
if ((fup = lookup(id, type)) != NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
return (fup);
|
|
|
|
if (name)
|
|
|
|
len = strlen(name);
|
|
|
|
else
|
2007-01-23 02:10:19 +00:00
|
|
|
len = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
|
1998-07-15 06:48:52 +00:00
|
|
|
errx(1, "calloc failed");
|
1994-05-26 06:35:07 +00:00
|
|
|
fhp = &fuhead[type][id & (FUHASH - 1)];
|
|
|
|
fup->fu_next = *fhp;
|
|
|
|
*fhp = fup;
|
|
|
|
fup->fu_id = id;
|
|
|
|
if (name)
|
|
|
|
bcopy(name, fup->fu_name, len + 1);
|
1996-02-21 18:40:54 +00:00
|
|
|
else {
|
|
|
|
(void)sprintf(fup->fu_name, "%lu", id);
|
2007-01-20 12:28:15 +00:00
|
|
|
if (vflag) {
|
|
|
|
if (aflag && fsname != NULL)
|
|
|
|
(void)printf("%s: ", fsname);
|
2008-07-02 15:51:59 +00:00
|
|
|
printf("unknown %cid: %lu\n",
|
1996-02-21 18:40:54 +00:00
|
|
|
type == USRQUOTA ? 'u' : 'g', id);
|
2007-01-20 12:28:15 +00:00
|
|
|
}
|
1996-02-21 18:40:54 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
return (fup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special purpose version of ginode used to optimize pass
|
|
|
|
* over all the inodes in numerical order.
|
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
static ino_t nextino, lastinum, lastvalidinum;
|
|
|
|
static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
|
|
|
|
static caddr_t inodebuf;
|
|
|
|
#define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode *
|
2002-11-20 02:13:32 +00:00
|
|
|
getnextinode(ino_t inumber)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
long size;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t dblk;
|
|
|
|
union dinode *dp;
|
|
|
|
static caddr_t nextinop;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2002-06-21 06:18:05 +00:00
|
|
|
if (inumber != nextino++ || inumber > lastvalidinum)
|
2012-09-27 23:31:06 +00:00
|
|
|
errx(1, "bad inode number %ju to nextinode",
|
|
|
|
(uintmax_t)inumber);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (inumber >= lastinum) {
|
|
|
|
readcnt++;
|
|
|
|
dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
|
|
|
|
if (readcnt % readpercg == 0) {
|
|
|
|
size = partialsize;
|
|
|
|
lastinum += partialcnt;
|
|
|
|
} else {
|
|
|
|
size = inobufsize;
|
|
|
|
lastinum += fullcnt;
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
/*
|
|
|
|
* If bread returns an error, it will already have zeroed
|
|
|
|
* out the buffer, so we do not need to do so here.
|
|
|
|
*/
|
|
|
|
bread(dblk, inodebuf, size);
|
|
|
|
nextinop = inodebuf;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
dp = (union dinode *)nextinop;
|
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC)
|
|
|
|
nextinop += sizeof(struct ufs1_dinode);
|
|
|
|
else
|
|
|
|
nextinop += sizeof(struct ufs2_dinode);
|
|
|
|
return (dp);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to scan a set of inodes.
|
|
|
|
*/
|
|
|
|
void
|
2002-11-20 02:13:32 +00:00
|
|
|
setinodebuf(ino_t inum)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
2002-11-20 02:13:32 +00:00
|
|
|
if (inum % sblock.fs_ipg != 0)
|
2012-09-27 23:31:06 +00:00
|
|
|
errx(1, "bad inode number %ju to setinodebuf", (uintmax_t)inum);
|
2002-11-20 02:13:32 +00:00
|
|
|
lastvalidinum = inum + sblock.fs_ipg - 1;
|
|
|
|
nextino = inum;
|
|
|
|
lastinum = inum;
|
1994-05-26 06:35:07 +00:00
|
|
|
readcnt = 0;
|
2002-11-20 02:13:32 +00:00
|
|
|
if (inodebuf != NULL)
|
|
|
|
return;
|
1994-05-26 06:35:07 +00:00
|
|
|
inobufsize = blkroundup(&sblock, INOBUFSIZE);
|
2002-06-21 06:18:05 +00:00
|
|
|
fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
|
|
|
|
sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
|
1994-05-26 06:35:07 +00:00
|
|
|
readpercg = sblock.fs_ipg / fullcnt;
|
|
|
|
partialcnt = sblock.fs_ipg % fullcnt;
|
2002-06-21 06:18:05 +00:00
|
|
|
partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
|
|
|
|
sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
|
1994-05-26 06:35:07 +00:00
|
|
|
if (partialcnt != 0) {
|
|
|
|
readpercg++;
|
|
|
|
} else {
|
|
|
|
partialcnt = fullcnt;
|
|
|
|
partialsize = inobufsize;
|
|
|
|
}
|
2002-11-20 02:13:32 +00:00
|
|
|
if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
|
|
|
|
errx(1, "cannot allocate space for inode buffer");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free up data structures used to scan inodes.
|
|
|
|
*/
|
|
|
|
void
|
2008-07-02 15:51:59 +00:00
|
|
|
freeinodebuf(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (inodebuf != NULL)
|
|
|
|
free(inodebuf);
|
|
|
|
inodebuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read specified disk blocks.
|
|
|
|
*/
|
|
|
|
void
|
2008-07-02 15:51:59 +00:00
|
|
|
bread(ufs2_daddr_t bno, char *buf, long cnt)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
|
|
|
|
read(fi, buf, cnt) != cnt)
|
2002-11-20 02:13:32 +00:00
|
|
|
errx(1, "bread failed on block %ld", (long)bno);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2007-01-20 12:28:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Display updated block and i-node counts.
|
|
|
|
*/
|
|
|
|
void
|
2009-12-27 06:28:01 +00:00
|
|
|
printchanges(const char *fsname, int type, struct dqblk *dp,
|
2008-07-02 15:51:59 +00:00
|
|
|
struct fileusage *fup, u_long id)
|
2007-01-20 12:28:15 +00:00
|
|
|
{
|
|
|
|
if (!vflag)
|
|
|
|
return;
|
|
|
|
if (aflag)
|
|
|
|
(void)printf("%s: ", fsname);
|
2007-01-23 02:10:19 +00:00
|
|
|
if (fup->fu_name[0] == '\0')
|
|
|
|
(void)printf("%-8lu fixed ", id);
|
|
|
|
else
|
|
|
|
(void)printf("%-8s fixed ", fup->fu_name);
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case GRPQUOTA:
|
|
|
|
(void)printf("(group):");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USRQUOTA:
|
|
|
|
(void)printf("(user): ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
(void)printf("(unknown quota type %d)", type);
|
|
|
|
break;
|
|
|
|
}
|
2007-01-20 12:28:15 +00:00
|
|
|
if (dp->dqb_curinodes != fup->fu_curinodes)
|
|
|
|
(void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
|
|
|
|
(u_long)fup->fu_curinodes);
|
|
|
|
if (dp->dqb_curblocks != fup->fu_curblocks)
|
2008-07-02 15:51:59 +00:00
|
|
|
(void)printf("\tblocks %lu -> %lu",
|
2007-01-20 12:28:15 +00:00
|
|
|
(u_long)dp->dqb_curblocks,
|
|
|
|
(u_long)fup->fu_curblocks);
|
|
|
|
(void)printf("\n");
|
|
|
|
}
|