2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)quotacheck.c 8.3 (Berkeley) 1/29/94
34 * $FreeBSD: src/sbin/quotacheck/quotacheck.c,v 1.11 1999/08/28 00:14:01 peter Exp $
38 * Fix up / report on disk quotas & usage
40 #include <sys/param.h>
43 #include <vfs/ufs/dinode.h>
44 #include <vfs/ufs/quota.h>
45 #include <vfs/ufs/fs.h>
58 const char *qfname
= QUOTAFILENAME
;
59 const char *qfextension
[] = INITQFNAMES
;
60 const char *quotagroup
= QUOTAGROUP
;
66 #define sblock un.sblk
72 char grpqfname
[MAXPATHLEN
+ 1];
73 char usrqfname
[MAXPATHLEN
+ 1];
79 struct fileusage
*fu_next
;
86 #define FUHASH 1024 /* must be power of two */
87 struct fileusage
*fuhead
[MAXQUOTAS
][FUHASH
];
89 int aflag
; /* all file systems */
90 int gflag
; /* check group quotas */
91 int uflag
; /* check user quotas */
92 int vflag
; /* verbose */
93 int fi
; /* open disk file descriptor */
94 u_long highid
[MAXQUOTAS
]; /* highest addid()'ed identifier per type */
97 addid(u_long
, int, char *);
98 char *blockcheck(char *);
99 void bread(daddr_t
, char *, long);
100 extern int checkfstab(int, int, void * (*)(struct fstab
*),
101 int (*)(char *, char *, struct quotaname
*));
102 int chkquota(char *, char *, struct quotaname
*);
103 void freeinodebuf(void);
106 int getquotagid(void);
107 int hasquota(struct fstab
*, int, char **);
110 void *needchk(struct fstab
*);
111 int oneof(char *, char*[], int);
112 void resetinodebuf(void);
113 int update(char *, char *, int);
117 main(int argc
, char **argv
)
122 struct quotaname
*auxdata
;
123 int i
, argnum
, maxrun
, errs
;
128 while ((ch
= getopt(argc
, argv
, "aguvl:")) != -1) {
143 maxrun
= atoi(optarg
);
151 if ((argc
== 0 && !aflag
) || (argc
> 0 && aflag
))
153 if (!gflag
&& !uflag
) {
159 while ((gr
= getgrent()) != NULL
)
160 addid((u_long
)gr
->gr_gid
, GRPQUOTA
, gr
->gr_name
);
165 while ((pw
= getpwent()) != NULL
)
166 addid((u_long
)pw
->pw_uid
, USRQUOTA
, pw
->pw_name
);
170 exit(checkfstab(1, maxrun
, needchk
, chkquota
));
172 errx(1, "%s: can't open", FSTAB
);
173 while ((fs
= getfsent()) != NULL
) {
174 if (((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
175 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) &&
176 (auxdata
= needchk(fs
)) &&
177 (name
= blockcheck(fs
->fs_spec
))) {
179 errs
+= chkquota(name
, fs
->fs_file
, auxdata
);
183 for (i
= 0; i
< argc
; i
++)
184 if ((done
& (1 << i
)) == 0)
185 fprintf(stderr
, "%s not found in %s\n",
193 fprintf(stderr
, "%s\n%s\n",
194 "usage: quotacheck -a [-guv]",
195 " quotacheck [-guv] filesys ...");
200 needchk(struct fstab
*fs
)
202 struct quotaname
*qnp
;
205 if (strcmp(fs
->fs_vfstype
, "ufs") ||
206 strcmp(fs
->fs_type
, FSTAB_RW
))
208 if ((qnp
= malloc(sizeof(*qnp
))) == NULL
)
209 errx(1, "malloc failed");
211 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
)) {
212 strcpy(qnp
->grpqfname
, qfnp
);
213 qnp
->flags
|= HASGRP
;
215 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
)) {
216 strcpy(qnp
->usrqfname
, qfnp
);
217 qnp
->flags
|= HASUSR
;
226 * Scan the specified filesystem to check quota(s) present on it.
229 chkquota(char *fsname
, char *mntpt
, struct quotaname
*qnp
)
231 struct fileusage
*fup
;
232 struct ufs1_dinode
*dp
;
233 int cg
, i
, mode
, errs
= 0;
236 if ((fi
= open(fsname
, O_RDONLY
, 0)) < 0) {
241 printf("*** Checking ");
242 if (qnp
->flags
& HASUSR
)
243 printf("%s%s", qfextension
[USRQUOTA
],
244 (qnp
->flags
& HASGRP
) ? " and " : "");
245 if (qnp
->flags
& HASGRP
)
246 printf("%s", qfextension
[GRPQUOTA
]);
247 printf(" quotas for %s (%s)\n", fsname
, mntpt
);
251 bread(SBOFF
, (char *)&sblock
, (long)SBSIZE
);
252 dev_bsize
= sblock
.fs_fsize
/ fsbtodb(&sblock
, 1);
253 quotamaxino
= sblock
.fs_ncg
* sblock
.fs_ipg
;
255 for (ino
= 0, cg
= 0; cg
< sblock
.fs_ncg
; cg
++) {
256 for (i
= 0; i
< sblock
.fs_ipg
; i
++, ino
++) {
259 if ((dp
= getnextinode(ino
)) == NULL
)
261 if ((mode
= dp
->di_mode
& IFMT
) == 0)
263 if (qnp
->flags
& HASGRP
) {
264 fup
= addid((u_long
)dp
->di_gid
, GRPQUOTA
,
267 if (mode
== IFREG
|| mode
== IFDIR
||
269 fup
->fu_curblocks
+= dp
->di_blocks
;
271 if (qnp
->flags
& HASUSR
) {
272 fup
= addid((u_long
)dp
->di_uid
, USRQUOTA
,
275 if (mode
== IFREG
|| mode
== IFDIR
||
277 fup
->fu_curblocks
+= dp
->di_blocks
;
282 if (qnp
->flags
& HASUSR
)
283 errs
+= update(mntpt
, qnp
->usrqfname
, USRQUOTA
);
284 if (qnp
->flags
& HASGRP
)
285 errs
+= update(mntpt
, qnp
->grpqfname
, GRPQUOTA
);
291 * Update a specified quota file.
294 update(char *fsname
, char *quotafile
, int type
)
296 struct fileusage
*fup
;
300 struct ufs_dqblk dqbuf
;
301 static int warned
= 0;
302 static struct ufs_dqblk zerodqbuf
;
303 static struct fileusage zerofileusage
;
305 if ((qfo
= fopen(quotafile
, "r+")) == NULL
) {
307 qfo
= fopen(quotafile
, "w+");
309 warnx("creating quota file %s", quotafile
);
310 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
311 fchown(fileno(qfo
), getuid(), getquotagid());
312 fchmod(fileno(qfo
), MODE
);
314 warn("%s", quotafile
);
318 if ((qfi
= fopen(quotafile
, "r")) == NULL
) {
319 warn("%s", quotafile
);
323 if (quotactl(fsname
, QCMD(Q_SYNC
, type
), (u_long
)0, (caddr_t
)0) < 0 &&
324 errno
== EOPNOTSUPP
&& !warned
&& vflag
) {
326 printf("*** Warning: %s\n",
327 "Quotas are not compiled into this kernel");
329 for (lastid
= highid
[type
], id
= 0, offset
= 0; id
<= lastid
;
330 id
++, offset
+= sizeof(struct ufs_dqblk
)) {
331 if (fread((char *)&dqbuf
, sizeof(struct ufs_dqblk
), 1, qfi
) == 0)
333 if ((fup
= lookup(id
, type
)) == NULL
)
334 fup
= &zerofileusage
;
335 if (dqbuf
.dqb_curinodes
== fup
->fu_curinodes
&&
336 dqbuf
.dqb_curblocks
== fup
->fu_curblocks
) {
337 fup
->fu_curinodes
= 0;
338 fup
->fu_curblocks
= 0;
343 printf("%s: ", fsname
);
344 printf("%-8s fixed:", fup
->fu_name
);
345 if (dqbuf
.dqb_curinodes
!= fup
->fu_curinodes
)
346 printf("\tinodes %lu -> %lu",
347 (u_long
)dqbuf
.dqb_curinodes
,
348 (u_long
)fup
->fu_curinodes
);
349 if (dqbuf
.dqb_curblocks
!= fup
->fu_curblocks
)
350 printf("\tblocks %lu -> %lu",
351 (u_long
)dqbuf
.dqb_curblocks
,
352 (u_long
)fup
->fu_curblocks
);
356 * Reset time limit if have a soft limit and were
357 * previously under it, but are now over it.
359 if (dqbuf
.dqb_bsoftlimit
&&
360 dqbuf
.dqb_curblocks
< dqbuf
.dqb_bsoftlimit
&&
361 fup
->fu_curblocks
>= dqbuf
.dqb_bsoftlimit
)
363 if (dqbuf
.dqb_isoftlimit
&&
364 dqbuf
.dqb_curblocks
< dqbuf
.dqb_isoftlimit
&&
365 fup
->fu_curblocks
>= dqbuf
.dqb_isoftlimit
)
367 dqbuf
.dqb_curinodes
= fup
->fu_curinodes
;
368 dqbuf
.dqb_curblocks
= fup
->fu_curblocks
;
369 if (fseek(qfo
, (long)offset
, SEEK_SET
) < 0) {
370 warn("%s: seek failed", quotafile
);
373 fwrite((char *)&dqbuf
, sizeof(struct ufs_dqblk
), 1, qfo
);
374 quotactl(fsname
, QCMD(Q_SETUSE
, type
), id
,
376 fup
->fu_curinodes
= 0;
377 fup
->fu_curblocks
= 0;
381 ftruncate(fileno(qfo
),
382 (off_t
)((highid
[type
] + 1) * sizeof(struct ufs_dqblk
)));
388 * Check to see if target appears in list of size cnt.
391 oneof(char *target
, char **list
, int cnt
)
395 for (i
= 0; i
< cnt
; i
++)
396 if (strcmp(target
, list
[i
]) == 0)
402 * Determine the group identifier for quota files.
409 if ((gr
= getgrnam(quotagroup
)) != NULL
)
415 * Check to see if a particular quota is to be enabled.
418 hasquota(struct fstab
*fs
, int type
, char **qfnamep
)
422 static char initname
, usrname
[100], grpname
[100];
423 static char buf
[BUFSIZ
];
426 snprintf(usrname
, sizeof(usrname
),
427 "%s%s", qfextension
[USRQUOTA
], qfname
);
428 snprintf(grpname
, sizeof(grpname
),
429 "%s%s", qfextension
[GRPQUOTA
], qfname
);
432 strcpy(buf
, fs
->fs_mntops
);
433 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
434 if ((cp
= strchr(opt
, '=')) != NULL
)
436 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
438 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
446 snprintf(buf
, sizeof(buf
),
447 "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
454 * Routines to manage the file usage table.
456 * Lookup an id of a specific type.
459 lookup(u_long id
, int type
)
461 struct fileusage
*fup
;
463 for (fup
= fuhead
[type
][id
& (FUHASH
-1)]; fup
!= NULL
; fup
= fup
->fu_next
)
464 if (fup
->fu_id
== id
)
470 * Add a new file usage id if it does not already exist.
473 addid(u_long id
, int type
, char *name
)
475 struct fileusage
*fup
, **fhp
;
478 if ((fup
= lookup(id
, type
)) != NULL
)
484 if ((fup
= calloc(1, sizeof(*fup
) + len
)) == NULL
)
485 errx(1, "calloc failed");
486 fhp
= &fuhead
[type
][id
& (FUHASH
- 1)];
490 if (id
> highid
[type
])
493 bcopy(name
, fup
->fu_name
, len
+ 1);
495 sprintf(fup
->fu_name
, "%lu", id
);
497 printf("unknown %cid: %lu\n",
498 type
== USRQUOTA
? 'u' : 'g', id
);
504 * Special purpose version of ginode used to optimize pass
505 * over all the inodes in numerical order.
507 ino_t nextino
, lastinum
;
508 long readcnt
, readpercg
, fullcnt
, inobufsize
, partialcnt
, partialsize
;
509 struct ufs1_dinode
*inodebuf
;
510 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
513 getnextinode(ino_t inumber
)
517 static struct ufs1_dinode
*dp
;
519 if (inumber
!= nextino
++ || inumber
> quotamaxino
) {
520 errx(1, "bad inode number %ju to nextinode",
523 if (inumber
>= lastinum
) {
525 dblk
= fsbtodb(&sblock
, ino_to_fsba(&sblock
, lastinum
));
526 if (readcnt
% readpercg
== 0) {
528 lastinum
+= partialcnt
;
533 bread(dblk
, (char *)inodebuf
, size
);
540 * Prepare to scan a set of inodes.
549 inobufsize
= blkroundup(&sblock
, INOBUFSIZE
);
550 fullcnt
= inobufsize
/ sizeof(struct ufs1_dinode
);
551 readpercg
= sblock
.fs_ipg
/ fullcnt
;
552 partialcnt
= sblock
.fs_ipg
% fullcnt
;
553 partialsize
= partialcnt
* sizeof(struct ufs1_dinode
);
554 if (partialcnt
!= 0) {
557 partialcnt
= fullcnt
;
558 partialsize
= inobufsize
;
560 if (inodebuf
== NULL
&&
561 (inodebuf
= malloc((u_int
)inobufsize
)) == NULL
)
562 errx(1, "malloc failed");
563 while (nextino
< ROOTINO
)
564 getnextinode(nextino
);
568 * Free up data structures used to scan inodes.
574 if (inodebuf
!= NULL
)
580 * Read specified disk blocks.
583 bread(daddr_t bno
, char *buf
, long cnt
)
586 if (lseek(fi
, (off_t
)bno
* dev_bsize
, SEEK_SET
) < 0 ||
587 read(fi
, buf
, cnt
) != cnt
)
588 errx(1, "block %ld", (long)bno
);