* Copyright (c) 1980, 1990 Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Robert Elz at The University of Melbourne.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
static char sccsid
[] = "@(#)quotacheck.c 5.17 (Berkeley) %G%";
* Fix up / report on disk quotas & usage
#include <ufs/ufs/dinode.h>
#include <ufs/ufs/quota.h>
char *qfname
= QUOTAFILENAME
;
char *qfextension
[] = INITQFNAMES
;
char *quotagroup
= QUOTAGROUP
;
char grpqfname
[MAXPATHLEN
+ 1];
char usrqfname
[MAXPATHLEN
+ 1];
struct fileusage
*fu_next
;
#define FUHASH 1024 /* must be power of two */
struct fileusage
*fuhead
[MAXQUOTAS
][FUHASH
];
struct fileusage
*lookup();
struct fileusage
*addid();
struct dinode
*getnextinode();
int aflag
; /* all file systems */
int gflag
; /* check group quotas */
int uflag
; /* check user quotas */
int fi
; /* open disk file descriptor */
u_long highid
[MAXQUOTAS
]; /* highest addid()'ed identifier per type */
register struct fstab
*fs
;
register struct passwd
*pw
;
register struct group
*gr
;
int i
, argnum
, maxrun
, errs
= 0;
char ch
, *name
, *blockcheck();
int needchk(), chkquota();
while ((ch
= getopt(argc
, argv
, "aguvl:")) != EOF
) {
if ((argc
== 0 && !aflag
) || (argc
> 0 && aflag
))
while ((gr
= getgrent()) != 0)
(void) addid((u_long
)gr
->gr_gid
, GRPQUOTA
, gr
->gr_name
);
while ((pw
= getpwent()) != 0)
(void) addid((u_long
)pw
->pw_uid
, USRQUOTA
, pw
->pw_name
);
exit(checkfstab(1, maxrun
, needchk
, chkquota
));
fprintf(stderr
, "Can't open ");
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
))) {
errs
+= chkquota(name
, fs
->fs_file
, auxdata
);
for (i
= 0; i
< argc
; i
++)
if ((done
& (1 << i
)) == 0)
fprintf(stderr
, "%s not found in %s\n",
(void) fprintf(stderr
, "usage:\t%s\n\t%s\n",
"quotacheck [-guv] filesys ...");
register struct fstab
*fs
;
register struct quotaname
*qnp
;
if (strcmp(fs
->fs_vfstype
, "ufs") ||
strcmp(fs
->fs_type
, FSTAB_RW
))
if ((qnp
= (struct quotaname
*)malloc(sizeof *qnp
)) == 0) {
fprintf(stderr
, "out of memory for quota structures\n");
if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
)) {
strcpy(qnp
->grpqfname
, qfnp
);
if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
)) {
strcpy(qnp
->usrqfname
, qfnp
);
* Scan the specified filesystem to check quota(s) present on it.
chkquota(fsname
, mntpt
, qnp
)
register struct quotaname
*qnp
;
register struct fileusage
*fup
;
register struct dinode
*dp
;
int cg
, i
, mode
, errs
= 0;
if ((fi
= open(fsname
, 0)) < 0) {
fprintf(stdout
, "*** Checking ");
fprintf(stdout
, "%s%s", qfextension
[USRQUOTA
],
(qnp
->flags
& HASGRP
) ? " and " : "");
fprintf(stdout
, "%s", qfextension
[GRPQUOTA
]);
fprintf(stdout
, " quotas for %s (%s)\n", fsname
, mntpt
);
bread(SBOFF
, (char *)&sblock
, (long)SBSIZE
);
dev_bsize
= sblock
.fs_fsize
/ fsbtodb(&sblock
, 1);
maxino
= sblock
.fs_ncg
* sblock
.fs_ipg
;
for (ino
= 0, cg
= 0; cg
< sblock
.fs_ncg
; cg
++) {
for (i
= 0; i
< sblock
.fs_ipg
; i
++, ino
++) {
if ((dp
= getnextinode(ino
)) == NULL
)
if ((mode
= dp
->di_mode
& IFMT
) == 0)
if (qnp
->flags
& HASGRP
) {
fup
= addid((u_long
)dp
->di_gid
, GRPQUOTA
,
if (mode
== IFREG
|| mode
== IFDIR
||
fup
->fu_curblocks
+= dp
->di_blocks
;
if (qnp
->flags
& HASUSR
) {
fup
= addid((u_long
)dp
->di_uid
, USRQUOTA
,
if (mode
== IFREG
|| mode
== IFDIR
||
fup
->fu_curblocks
+= dp
->di_blocks
;
errs
+= update(mntpt
, qnp
->usrqfname
, USRQUOTA
);
errs
+= update(mntpt
, qnp
->grpqfname
, GRPQUOTA
);
* Update a specified quota file.
update(fsname
, quotafile
, type
)
char *fsname
, *quotafile
;
register struct fileusage
*fup
;
register FILE *qfi
, *qfo
;
register u_long id
, lastid
;
static struct dqblk zerodqbuf
;
static struct fileusage zerofileusage
;
if ((qfo
= fopen(quotafile
, "r+")) == NULL
) {
qfo
= fopen(quotafile
, "w+");
"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
);
"quotacheck: %s: %s\n", quotafile
, strerror(errno
));
if ((qfi
= fopen(quotafile
, "r")) == NULL
) {
"quotacheck: %s: %s\n", quotafile
, strerror(errno
));
if (quotactl(fsname
, QCMD(Q_SYNC
, type
), (u_long
)0, (caddr_t
)0) < 0 &&
errno
== EOPNOTSUPP
&& !warned
&& vflag
) {
fprintf(stdout
, "*** Warning: %s\n",
"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)
if ((fup
= lookup(id
, type
)) == 0)
if (dqbuf
.dqb_curinodes
== fup
->fu_curinodes
&&
dqbuf
.dqb_curblocks
== fup
->fu_curblocks
) {
fseek(qfo
, (long)sizeof(struct dqblk
), 1);
printf("%-8s fixed:", fup
->fu_name
);
if (dqbuf
.dqb_curinodes
!= fup
->fu_curinodes
)
fprintf(stdout
, "\tinodes %d -> %d",
dqbuf
.dqb_curinodes
, fup
->fu_curinodes
);
if (dqbuf
.dqb_curblocks
!= fup
->fu_curblocks
)
fprintf(stdout
, "\tblocks %d -> %d",
dqbuf
.dqb_curblocks
, fup
->fu_curblocks
);
* 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
)
if (dqbuf
.dqb_isoftlimit
&&
dqbuf
.dqb_curblocks
< dqbuf
.dqb_isoftlimit
&&
fup
->fu_curblocks
>= dqbuf
.dqb_isoftlimit
)
dqbuf
.dqb_curinodes
= fup
->fu_curinodes
;
dqbuf
.dqb_curblocks
= fup
->fu_curblocks
;
fwrite((char *)&dqbuf
, sizeof(struct dqblk
), 1, qfo
);
(void) quotactl(fsname
, QCMD(Q_SETUSE
, type
), id
,
(off_t
)((highid
[type
] + 1) * sizeof(struct dqblk
)));
* Check to see if target appears in list of size cnt.
register char *target
, *list
[];
for (i
= 0; i
< cnt
; i
++)
if (strcmp(target
, list
[i
]) == 0)
* Determine the group identifier for quota files.
if (gr
= getgrnam(quotagroup
))
* Check to see if a particular quota is to be enabled.
hasquota(fs
, type
, qfnamep
)
register struct fstab
*fs
;
char *cp
, *index(), *strtok();
static char initname
, usrname
[100], grpname
[100];
sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
strcpy(buf
, fs
->fs_mntops
);
for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
if (cp
= index(opt
, '='))
if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
(void) sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
* Routines to manage the file usage table.
* Lookup an id of a specific type.
register struct fileusage
*fup
;
for (fup
= fuhead
[type
][id
& (FUHASH
-1)]; fup
!= 0; fup
= fup
->fu_next
)
return ((struct fileusage
*)0);
* Add a new file usage id if it does not already exist.
struct fileusage
*fup
, **fhp
;
if (fup
= lookup(id
, type
))
if ((fup
= (struct fileusage
*)calloc(1, sizeof(*fup
) + len
)) == NULL
) {
fprintf(stderr
, "out of memory for fileusage structures\n");
fhp
= &fuhead
[type
][id
& (FUHASH
- 1)];
bcopy(name
, fup
->fu_name
, len
+ 1);
sprintf(fup
->fu_name
, "%u", id
);
* Special purpose version of ginode used to optimize pass
* over all the inodes in numerical order.
long readcnt
, readpercg
, fullcnt
, inobufsize
, partialcnt
, partialsize
;
#define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
static struct dinode
*dp
;
if (inumber
!= nextino
++ || inumber
> maxino
) {
fprintf(stderr
, "bad inode number %d to nextinode\n", inumber
);
if (inumber
>= lastinum
) {
dblk
= fsbtodb(&sblock
, itod(&sblock
, lastinum
));
if (readcnt
% readpercg
== 0) {
bread(dblk
, (char *)inodebuf
, size
);
* Prepare to scan a set of inodes.
inobufsize
= blkroundup(&sblock
, INOBUFSIZE
);
fullcnt
= inobufsize
/ sizeof(struct dinode
);
readpercg
= sblock
.fs_ipg
/ fullcnt
;
partialcnt
= sblock
.fs_ipg
% fullcnt
;
partialsize
= partialcnt
* sizeof(struct dinode
);
partialsize
= inobufsize
;
(inodebuf
= (struct dinode
*)malloc((unsigned)inobufsize
)) == NULL
) {
fprintf(stderr
, "Cannot allocate space for inode buffer\n");
while (nextino
< ROOTINO
)
* Free up data structures used to scan inodes.
* Read specified disk blocks.
if (lseek(fi
, bno
* dev_bsize
, 0) < 0) {
if (read(fi
, buf
, cnt
) != cnt
) {