1 /* $NetBSD: quotacheck.c,v 1.39 2008/07/21 13:36:59 lukem Exp $ */
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)quotacheck.c 8.6 (Berkeley) 4/28/95";
45 __RCSID("$NetBSD: quotacheck.c,v 1.39 2008/07/21 13:36:59 lukem Exp $");
50 * Fix up / report on disk quotas & usage
52 #include <sys/param.h>
54 #include <sys/queue.h>
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/ufs_bswap.h>
59 #include <ufs/ffs/fs.h>
60 #include <ufs/ffs/ffs_extern.h>
76 # define FS_UFS1_MAGIC FS_MAGIC /* 0x011954 */
77 # define FS_UFS1_MAGIC_SWAPPED 0x54190100 /* bswap32(0x011954) */
78 # define DINODE1_SIZE sizeof(struct dinode)
79 # define DINODE2_SIZE 0
85 # define SBLOCKSIZE SBSIZE
88 # define SBLOCKSEARCH { SBSIZE, -1 }
91 static const char *qfname
= QUOTAFILENAME
;
92 static const char *qfextension
[] = INITQFNAMES
;
93 static const char *quotagroup
= QUOTAGROUP
;
99 #define sblock un.sblk
100 static long dev_bsize
;
105 char grpqfname
[MAXPATHLEN
+ 1];
106 char usrqfname
[MAXPATHLEN
+ 1];
112 struct fileusage
*fu_next
;
115 u_int32_t fu_id
; /* uid_t, gid_t */
117 /* actually bigger */
119 #define FUHASH 1024 /* must be power of two */
120 static struct fileusage
*fuhead
[MAXQUOTAS
][FUHASH
];
125 struct ufs1_dinode dp1
;
126 struct ufs2_dinode dp2
;
132 #define DIP(dp, field) \
133 (is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
135 #define DIP(dp, field) (dp)->dp1.di_##field
139 static int aflag
; /* all file systems */
140 static int gflag
; /* check group quotas */
141 static int uflag
; /* check user quotas */
142 static int vflag
; /* verbose */
143 static int qflag
; /* quick but untidy mode */
144 static int fi
; /* open disk file descriptor */
145 static u_int32_t highid
[MAXQUOTAS
];/* highest addid()'ed identifier per type */
146 static int needswap
; /* FS is in swapped order */
147 static int got_siginfo
= 0; /* got a siginfo signal */
151 int main
__P((int, char *[]));
152 static void usage
__P((void));
153 static void *needchk
__P((struct fstab
*));
154 static int chkquota
__P((const char *, const char *, const char *, void *,
156 static int update
__P((const char *, const char *, int));
157 static u_int32_t skipforward
__P((u_int32_t
, u_int32_t
, FILE *));
158 static int oneof
__P((const char *, char *[], int));
159 static int getquotagid
__P((void));
160 static int hasquota
__P((struct fstab
*, int, char **));
161 static struct fileusage
*lookup
__P((u_int32_t
, int));
162 static struct fileusage
*addid
__P((u_int32_t
, int, const char *));
163 static u_int32_t subsequent
__P((u_int32_t
, int));
164 static union comb_dinode
*getnextinode
__P((ino_t
));
165 static void setinodebuf
__P((ino_t
));
166 static void freeinodebuf
__P((void));
167 static void bread
__P((daddr_t
, char *, long));
168 static void infohandler
__P((int sig
));
169 static void swap_dinode1(union comb_dinode
*, int);
171 static void swap_dinode2(union comb_dinode
*, int);
182 struct quotaname
*auxdata
;
183 int i
, argnum
, maxrun
, errs
;
185 int flags
= CHECK_PREEN
;
190 while ((ch
= getopt(argc
, argv
, "aguvqdl:")) != -1) {
196 flags
|= CHECK_DEBUG
;
211 maxrun
= atoi(optarg
);
219 if ((argc
== 0 && !aflag
) || (argc
> 0 && aflag
) || (!aflag
&& maxrun
))
221 if (!gflag
&& !uflag
) {
226 /* If -a, we do not want to pay the cost of processing every
227 * group and password entry if there are no filesystems with quotas
231 while ((fs
= getfsent()) != NULL
) {
236 if (!i
) /* No filesystems with quotas */
242 while ((gr
= getgrent()) != 0)
243 (void) addid((u_int32_t
)gr
->gr_gid
, GRPQUOTA
, gr
->gr_name
);
248 while ((pw
= getpwent()) != 0)
249 (void) addid((u_int32_t
)pw
->pw_uid
, USRQUOTA
, pw
->pw_name
);
253 exit(checkfstab(flags
, maxrun
, needchk
, chkquota
));
255 err(1, "%s: can't open", FSTAB
);
256 while ((fs
= getfsent()) != NULL
) {
257 if (((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
258 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) &&
259 (auxdata
= needchk(fs
)) &&
260 (name
= blockcheck(fs
->fs_spec
))) {
262 errs
+= chkquota(fs
->fs_type
, name
, fs
->fs_file
,
267 for (i
= 0; i
< argc
; i
++)
268 if ((done
& (1 << i
)) == 0)
269 fprintf(stderr
, "%s not found in %s\n",
278 (void)fprintf(stderr
,
279 "usage:\t%s -a [-gquv] [-l maxparallel]\n\t%s [-gquv] filesys ...\n", getprogname(),
288 struct quotaname
*qnp
;
291 if (strcmp(fs
->fs_vfstype
, "ffs") ||
292 strcmp(fs
->fs_type
, FSTAB_RW
))
294 if ((qnp
= malloc(sizeof(*qnp
))) == NULL
)
295 err(1, "%s", strerror(errno
));
297 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
)) {
298 strlcpy(qnp
->grpqfname
, qfnp
, sizeof(qnp
->grpqfname
));
299 qnp
->flags
|= HASGRP
;
301 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
)) {
302 strlcpy(qnp
->usrqfname
, qfnp
, sizeof(qnp
->usrqfname
));
303 qnp
->flags
|= HASUSR
;
311 off_t sblock_try
[] = SBLOCKSEARCH
;
314 * Scan the specified filesystem to check quota(s) present on it.
317 chkquota(type
, fsname
, mntpt
, v
, pid
)
318 const char *type
, *fsname
, *mntpt
;
322 struct quotaname
*qnp
= v
;
323 struct fileusage
*fup
;
324 union comb_dinode
*dp
;
325 int cg
, i
, mode
, errs
= 0, inosused
;
332 switch ((*pid
= fork())) {
338 err(1, "Cannot fork");
340 setvbuf(stdout
, msgbuf
, _IOFBF
, sizeof msgbuf
);
343 if ((fi
= open(fsname
, O_RDONLY
, 0)) < 0) {
344 warn("Cannot open %s", fsname
);
350 (void)printf("*** Checking ");
351 if (qnp
->flags
& HASUSR
)
352 (void)printf("%s%s", qfextension
[USRQUOTA
],
353 (qnp
->flags
& HASGRP
) ? " and " : "");
354 if (qnp
->flags
& HASGRP
)
355 (void)printf("%s", qfextension
[GRPQUOTA
]);
356 (void)printf(" quotas for %s (%s)\n", fsname
, mntpt
);
359 signal(SIGINFO
, infohandler
);
364 if (sblock_try
[i
] == -1) {
365 warnx("%s: superblock not found", fsname
);
370 bread(sblock_try
[i
], (char *)&sblock
, SBLOCKSIZE
);
371 switch (sblock
.fs_magic
) {
380 case FS_UFS2_MAGIC_SWAPPED
:
384 case FS_UFS1_MAGIC_SWAPPED
:
386 ffs_sb_swap(&sblock
, &sblock
);
393 if (is_ufs2
|| sblock
.fs_old_flags
& FS_FLAGS_UPDATED
) {
394 if (sblock
.fs_sblockloc
!= sblock_try
[i
])
397 if (sblock_try
[i
] == SBLOCK_UFS2
)
404 cgp
= malloc(sblock
.fs_cgsize
);
406 warn("%s: can't allocate %d bytes of cg space", fsname
,
413 dev_bsize
= sblock
.fs_fsize
/ fsbtodb(&sblock
, 1);
414 maxino
= sblock
.fs_ncg
* sblock
.fs_ipg
;
415 for (cg
= 0; cg
< sblock
.fs_ncg
; cg
++) {
416 ino
= cg
* sblock
.fs_ipg
;
419 if (sblock
.fs_magic
== FS_UFS2_MAGIC
) {
420 bread(fsbtodb(&sblock
, cgtod(&sblock
, cg
)), (char *)cgp
,
423 ffs_cg_swap(cgp
, cgp
, &sblock
);
424 inosused
= cgp
->cg_initediblk
;
427 inosused
= sblock
.fs_ipg
;
428 for (i
= 0; i
< inosused
; i
++, ino
++) {
431 "%s: cyl group %d of %d (%d%%)\n",
432 fsname
, cg
, sblock
.fs_ncg
,
433 cg
* 100 / sblock
.fs_ncg
);
438 if ((dp
= getnextinode(ino
)) == NULL
)
440 if ((mode
= DIP(dp
, mode
) & IFMT
) == 0)
442 if (qnp
->flags
& HASGRP
) {
443 fup
= addid(DIP(dp
, gid
), GRPQUOTA
,
446 if (mode
== IFREG
|| mode
== IFDIR
||
448 fup
->fu_curblocks
+= DIP(dp
, blocks
);
450 if (qnp
->flags
& HASUSR
) {
451 fup
= addid(DIP(dp
, uid
), USRQUOTA
,
454 if (mode
== IFREG
|| mode
== IFDIR
||
456 fup
->fu_curblocks
+= DIP(dp
, blocks
);
462 if (qnp
->flags
& HASUSR
)
463 errs
+= update(mntpt
, qnp
->usrqfname
, USRQUOTA
);
464 if (qnp
->flags
& HASGRP
)
465 errs
+= update(mntpt
, qnp
->grpqfname
, GRPQUOTA
);
473 * Update a specified quota file.
476 update(fsname
, quotafile
, type
)
477 const char *fsname
, *quotafile
;
480 struct fileusage
*fup
;
482 u_int32_t id
, lastid
, nextid
;
485 static int warned
= 0;
486 static struct dqblk zerodqbuf
;
487 static struct fileusage zerofileusage
;
489 if ((qfo
= fopen(quotafile
, "r+")) == NULL
) {
491 qfo
= fopen(quotafile
, "w+");
493 (void) fprintf(stderr
,
494 "quotacheck: creating quota file %s\n", quotafile
);
495 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
496 (void) fchown(fileno(qfo
), getuid(), getquotagid());
497 (void) fchmod(fileno(qfo
), MODE
);
499 (void) fprintf(stderr
,
500 "quotacheck: %s: %s\n", quotafile
, strerror(errno
));
504 if ((qfi
= fopen(quotafile
, "r")) == NULL
) {
505 (void) fprintf(stderr
,
506 "quotacheck: %s: %s\n", quotafile
, strerror(errno
));
510 if (quotactl(fsname
, QCMD(Q_SYNC
, type
), 0, (void *) NULL
) < 0 &&
511 errno
== EOPNOTSUPP
&& !warned
&& vflag
) {
513 (void)printf("*** Warning: %s\n",
514 "Quotas are not compiled into this kernel");
517 for (lastid
= highid
[type
], id
= 0; id
<= lastid
; id
= nextid
) {
518 if (fread((char *)&dqbuf
, sizeof(struct dqblk
), 1, qfi
) == 0)
520 if ((fup
= lookup(id
, type
)) == 0)
521 fup
= &zerofileusage
;
523 nextid
= subsequent(id
, type
);
524 if (nextid
> 0 && nextid
!= id
+ 1) /* watch out for id == UINT32_MAX */
525 nextid
= skipforward(id
, nextid
, qfi
);
528 /* XXX this could try to show percentage through the ID list */
530 "%s: updating %s quotas for id=%" PRIu32
" (%s)\n", fsname
,
531 qfextension
[type
< MAXQUOTAS
? type
: MAXQUOTAS
],
535 if (dqbuf
.dqb_curinodes
== fup
->fu_curinodes
&&
536 dqbuf
.dqb_curblocks
== fup
->fu_curblocks
) {
537 fup
->fu_curinodes
= 0; /* reset usage */
538 fup
->fu_curblocks
= 0; /* for next filesystem */
541 if (id
== UINT32_MAX
|| nextid
== 0) { /* infinite loop avoidance (OR do as "nextid < id"?) */
548 printf("%s: ", fsname
);
549 printf("%-8s fixed:", fup
->fu_name
);
550 if (dqbuf
.dqb_curinodes
!= fup
->fu_curinodes
)
551 (void)printf("\tinodes %d -> %ld",
552 dqbuf
.dqb_curinodes
, fup
->fu_curinodes
);
553 if (dqbuf
.dqb_curblocks
!= fup
->fu_curblocks
)
554 (void)printf("\tblocks %d -> %ld",
555 dqbuf
.dqb_curblocks
, fup
->fu_curblocks
);
559 * Reset time limit if have a soft limit and were
560 * previously under it, but are now over it.
562 if (dqbuf
.dqb_bsoftlimit
&&
563 dqbuf
.dqb_curblocks
< dqbuf
.dqb_bsoftlimit
&&
564 fup
->fu_curblocks
>= dqbuf
.dqb_bsoftlimit
)
566 if (dqbuf
.dqb_isoftlimit
&&
567 dqbuf
.dqb_curinodes
< dqbuf
.dqb_isoftlimit
&&
568 fup
->fu_curinodes
>= dqbuf
.dqb_isoftlimit
)
570 dqbuf
.dqb_curinodes
= fup
->fu_curinodes
;
571 dqbuf
.dqb_curblocks
= fup
->fu_curblocks
;
574 (void) fseeko(qfo
, (off_t
)id
* sizeof(struct dqblk
),
576 need_seek
= nextid
!= id
+ 1;
578 (void) fwrite((char *)&dqbuf
, sizeof(struct dqblk
), 1, qfo
);
581 (void) quotactl(fsname
, QCMD(Q_SETUSE
, type
), id
,
584 fup
->fu_curinodes
= 0;
585 fup
->fu_curblocks
= 0;
586 if (id
== UINT32_MAX
|| nextid
== 0) { /* infinite loop avoidance (OR do as "nextid < id"?) */
592 if (highid
[type
] != UINT32_MAX
)
593 (void) ftruncate(fileno(qfo
),
594 (off_t
)((highid
[type
] + 1) * sizeof(struct dqblk
)));
600 skipforward(cur
, to
, qfi
)
607 (void) fseeko(qfi
, (off_t
)to
* sizeof(struct dqblk
), SEEK_SET
);
613 * if EOF occurs, nothing left to read, we're done
615 if (fread((char *)&dqbuf
, sizeof(struct dqblk
), 1, qfi
) == 0)
619 * If we find an entry that shows usage, before the next
620 * id that has actual usage, we have to stop here, so the
621 * incorrect entry can be corrected in the file
623 if (dqbuf
.dqb_curinodes
!= 0 || dqbuf
.dqb_curblocks
!= 0) {
624 (void)fseek(qfi
, -(long)sizeof(struct dqblk
), SEEK_CUR
);
632 * Check to see if target appears in list of size cnt.
635 oneof(target
, list
, cnt
)
642 for (i
= 0; i
< cnt
; i
++)
643 if (strcmp(target
, list
[i
]) == 0)
649 * Determine the group identifier for quota files.
656 if ((gr
= getgrnam(quotagroup
)) != NULL
)
662 * Check to see if a particular quota is to be enabled.
665 hasquota(fs
, type
, qfnamep
)
672 static char initname
, usrname
[100], grpname
[100];
673 static char buf
[BUFSIZ
];
676 (void)snprintf(usrname
, sizeof(usrname
),
677 "%s%s", qfextension
[USRQUOTA
], qfname
);
678 (void)snprintf(grpname
, sizeof(grpname
),
679 "%s%s", qfextension
[GRPQUOTA
], qfname
);
682 (void) strlcpy(buf
, fs
->fs_mntops
, sizeof(buf
));
683 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
684 if ((cp
= strchr(opt
, '=')) != NULL
)
686 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
688 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
696 (void)snprintf(buf
, sizeof(buf
),
697 "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
704 * Routines to manage the file usage table.
706 * Lookup an id of a specific type.
708 static struct fileusage
*
713 struct fileusage
*fup
;
715 for (fup
= fuhead
[type
][id
& (FUHASH
-1)]; fup
!= 0; fup
= fup
->fu_next
)
716 if (fup
->fu_id
== id
)
722 * Add a new file usage id if it does not already exist.
724 static struct fileusage
*
725 addid(id
, type
, name
)
730 struct fileusage
*fup
, **fhp
;
733 if ((fup
= lookup(id
, type
)) != NULL
)
739 if ((fup
= calloc(1, sizeof(*fup
) + len
)) == NULL
)
740 err(1, "%s", strerror(errno
));
741 fhp
= &fuhead
[type
][id
& (FUHASH
- 1)];
745 if (id
> highid
[type
])
748 memmove(fup
->fu_name
, name
, len
+ 1);
750 (void) sprintf(fup
->fu_name
, "%" PRIu32
, id
);
759 struct fileusage
*fup
, **iup
, **cup
;
760 u_int32_t next
, offset
;
762 next
= highid
[type
] + 1;
764 cup
= iup
= &fuhead
[type
][id
& (FUHASH
-1)];
767 if (++cup
>= &fuhead
[type
][FUHASH
])
768 cup
= &fuhead
[type
][0];
769 for (fup
= *cup
; fup
!= 0; fup
= fup
->fu_next
) {
770 if (fup
->fu_id
> id
&& fup
->fu_id
<= id
+ offset
)
772 if (fup
->fu_id
> id
&& fup
->fu_id
< next
)
775 } while (cup
!= iup
);
781 * Special purpose version of ginode used to optimize first pass
782 * over all the inodes in numerical order.
784 static ino_t nextino
, lastinum
, lastvalidinum
;
785 static long readcnt
, readpercg
, fullcnt
, inobufsize
, partialcnt
, partialsize
;
786 static union comb_dinode
*inodebuf
;
787 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
790 getnextinode(inumber
)
795 static union comb_dinode
*dp
;
796 union comb_dinode
*ret
;
798 if (inumber
!= nextino
++ || inumber
> lastvalidinum
) {
799 errx(1, "bad inode number %llu to nextinode",
800 (unsigned long long)inumber
);
803 if (inumber
>= lastinum
) {
805 dblk
= fsbtodb(&sblock
, ino_to_fsba(&sblock
, lastinum
));
806 if (readcnt
% readpercg
== 0) {
808 lastinum
+= partialcnt
;
813 (void)bread(dblk
, (caddr_t
)inodebuf
, size
);
817 swap_dinode2(inodebuf
, lastinum
- inumber
);
820 swap_dinode1(inodebuf
, lastinum
- inumber
);
822 dp
= (union comb_dinode
*)inodebuf
;
825 dp
= (union comb_dinode
*)
826 ((char *)dp
+ (is_ufs2
? DINODE2_SIZE
: DINODE1_SIZE
));
835 if (inum
% sblock
.fs_ipg
!= 0)
836 errx(1, "bad inode number %llu to setinodebuf",
837 (unsigned long long)inum
);
839 lastvalidinum
= inum
+ sblock
.fs_ipg
- 1;
843 if (inodebuf
!= NULL
)
845 inobufsize
= blkroundup(&sblock
, INOBUFSIZE
);
846 fullcnt
= inobufsize
/ (is_ufs2
? DINODE2_SIZE
: DINODE1_SIZE
);
847 readpercg
= sblock
.fs_ipg
/ fullcnt
;
848 partialcnt
= sblock
.fs_ipg
% fullcnt
;
849 partialsize
= partialcnt
* (is_ufs2
? DINODE2_SIZE
: DINODE1_SIZE
);
850 if (partialcnt
!= 0) {
853 partialcnt
= fullcnt
;
854 partialsize
= inobufsize
;
856 if (inodebuf
== NULL
&&
857 (inodebuf
= malloc((unsigned)inobufsize
)) == NULL
)
858 errx(1, "Cannot allocate space for inode buffer");
859 while (nextino
< ROOTINO
)
860 getnextinode(nextino
);
867 if (inodebuf
!= NULL
)
868 free((char *)inodebuf
);
875 swap_dinode1(union comb_dinode
*dp
, int n
)
878 struct ufs1_dinode
*dp1
;
880 dp1
= (struct ufs1_dinode
*)&dp
->dp1
;
881 for (i
= 0; i
< n
; i
++, dp1
++)
882 ffs_dinode1_swap(dp1
, dp1
);
886 swap_dinode2(union comb_dinode
*dp
, int n
)
889 struct ufs2_dinode
*dp2
;
891 dp2
= (struct ufs2_dinode
*)&dp
->dp2
;
892 for (i
= 0; i
< n
; i
++, dp2
++)
893 ffs_dinode2_swap(dp2
, dp2
);
899 swap_dinode1(union comb_dinode
*dp
, int n
)
904 dp1
= (struct dinode
*) &dp
->dp1
;
905 for (i
= 0; i
< n
; i
++, dp1
++)
906 ffs_dinode_swap(dp1
, dp1
);
912 * Read specified disk blocks.
921 if (lseek(fi
, (off_t
)bno
* dev_bsize
, SEEK_SET
) < 0 ||
922 read(fi
, buf
, cnt
) != cnt
)
923 err(1, "block %lld", (long long)bno
);